source: rtems/doc/networking/networkapp.t @ 6449498

4.104.114.84.95
Last change on this file since 6449498 was 6449498, checked in by Joel Sherrill <joel.sherrill@…>, on 01/17/02 at 21:47:47

2001-01-17 Joel Sherrill <joel@…>

  • SUPPORT, LICENSE: New files.
  • Numerous files touched as part of merging the 4.5 branch onto the mainline development trunk and ensuring that the script that cuts snapshots and releases works on the documentation.
  • Property mode set to 100644
File size: 14.4 KB
RevLine 
[ab0c689]1@c
2@c  Written by Eric Norum
3@c
[6449498]4@c  COPYRIGHT (c) 1988-2002.
[ab0c689]5@c  On-Line Applications Research Corporation (OAR).
6@c  All rights reserved.
7@c
8@c  $Id$
9@c
10
[071536e]11@chapter Using Networking in an RTEMS Application
[ab0c689]12
13@section Makefile changes
14@subsection Including the required managers
[4b8bf95]15The FreeBSD networking code requires several RTEMS managers
[ab0c689]16in the application:
17
18@example
19MANAGERS = io event semaphore
20@end example
21
22@subsection Increasing the size of the heap
23The networking tasks allocate a lot of memory.  For most applications
24the heap should be at least 256 kbytes.
25The amount of memory set aside for the heap can be adjusted by setting
26the @code{CFLAGS_LD} definition as shown below:
27
28@example
29CFLAGS_LD += -Wl,--defsym -Wl,HeapSize=0x80000
30@end example
31
32This sets aside 512 kbytes of memory for the heap.
33
34@section System Configuration
35
36The networking tasks allocate some RTEMS objects.  These
37must be accounted for in the application configuration table.  The following
38lists the requirements.
39
40@table @b
41@item TASKS
42One network task plus a receive and transmit task for each device.
43
44@item SEMAPHORES
45One network semaphore plus one syslog mutex semaphore if the application uses
46openlog/syslog.
47
48@item EVENTS
49The network stack uses @code{RTEMS_EVENT_24} and @code{RTEMS_EVENT_25}.
50This has no effect on the application configuration, but
51application tasks which call the network functions should not
52use these events for other purposes.
53
54@end table
55
56@section Initialization
57@subsection Additional include files
58The source file which declares the network configuration
59structures and calls the network initialization function must include
60
61@example
[d778b4a]62#include <rtems/rtems_bsdnet.h>
[ab0c689]63@end example
64
[33bbd74]65@subsection Network Configuration
[ab0c689]66The network configuration is specified by declaring
[33bbd74]67and initializing the @code{rtems_bsdnet_config}
[d9de76f]68structure.
[ab0c689]69
[33bbd74]70@example
71@group
72struct rtems_bsdnet_config @{
73  /*
74   * This entry points to the head of the ifconfig chain.
75   */
76  struct rtems_bsdnet_ifconfig *ifconfig;
77
78  /*
79   * This entry should be rtems_bsdnet_do_bootp if BOOTP
80   * is being used to configure the network, and NULL
81   * if BOOTP is not being used.
82   */
83  void                    (*bootp)(void);
84
85  /*
86   * The remaining items can be initialized to 0, in
87   * which case the default value will be used.
88   */
89  rtems_task_priority  network_task_priority;  /* 100        */
90  unsigned long        mbuf_bytecount;         /* 64 kbytes  */
91  unsigned long        mbuf_cluster_bytecount; /* 128 kbytes */
92  char                *hostname;               /* BOOTP      */
93  char                *domainname;             /* BOOTP      */
94  char                *gateway;                /* BOOTP      */
95  char                *log_host;               /* BOOTP      */
96  char                *name_server[3];         /* BOOTP      */
97  char                *ntp_server[3];          /* BOOTP      */
98@};
99@end group
100@end example
101
[ab0c689]102The structure entries are described in the following table.
[df4eb76e]103If your application uses BOOTP/DHCP to obtain network configuration
[ab0c689]104information and if you are happy with the default values described
105below, you need to provide only the first two entries in this structure.
106
107@table @code
108
109@item struct rtems_bsdnet_ifconfig *ifconfig
110A pointer to the first configuration structure of the first network
111device.  This structure is described in the following section.
112You must provide a value for this entry since there is no default value for it.
113
114
115@item void (*bootp)(void)
116This entry should be set to @code{rtems_bsdnet_do_bootp}
[df4eb76e]117if your application will use BOOTP/DHCP
118to obtain network configuration information.
[ab0c689]119It should be set to @code{NULL}
[df4eb76e]120if your application does not use BOOTP/DHCP.
[ab0c689]121
122
123@item int network_task_priority
124The priority at which the network task and network device
125receive and transmit tasks will run.
126If a value of 0 is specified the tasks will run at priority 100.
127
128@item unsigned long mbuf_bytecount
129The number of bytes to allocate from the heap for use as mbufs. 
130If a value of 0 is specified, 64 kbytes will be allocated.
131
132@item unsigned long mbuf_cluster_bytecount
133The number of bytes to allocate from the heap for use as mbuf clusters. 
134If a value of 0 is specified, 128 kbytes will be allocated.
135
136@item char *hostname
137The host name of the system.
[a83dd861]138If this, or any of the following, entries are @code{NULL} the value
[df4eb76e]139may be obtained from a BOOTP/DHCP server.
[ab0c689]140
141@item char *domainname
142The name of the Internet domain to which the system belongs.
143
144@item char *gateway
145The Internet host number of the network gateway machine,
146specified in `dotted decimal' (@code{129.128.4.1}) form.
147
148@item char *log_host
149The Internet host number of the machine to which @code{syslog} messages
150will be sent.
151
[a83dd861]152@item char *name_server[3]
153The Internet host numbers of up to three machines to be used as
[ab0c689]154Internet Domain Name Servers.
155
[19e87bbb]156@item char *ntp_server[3]
[33bbd74]157The Internet host numbers of up to three machines to be used as
158Network Time Protocol (NTP) Servers.
159
160@end table
161
162In addition, the following fields in the @code{rtems_bsdnet_ifconfig}
163are of interest.
164
165@table @b
166
[4b8bf95]167@item int port
168The I/O port number (ex: 0x240) on which the external Ethernet
169can be accessed.
170
171@item int irno
172The interrupt number of the external Ethernet controller.
173
174@item int bpar
175The address of the shared memory on the external Ethernet controller.
176
177
[ab0c689]178@end table
179
180@subsection Network device configuration
181Network devices are specified and configured by declaring and initializing a
[19e87bbb]182@code{struct rtems_bsdnet_ifconfig} structure for each network device.
[ab0c689]183
184The structure entries are described in the following table.  An application
185which uses a single network interface, gets network configuration information
[df4eb76e]186from a BOOTP/DHCP server, and uses the default values for all driver
[ab0c689]187parameters needs to initialize only the first two entries in the
188structure.
189
190@table @code
191@item char *name
192The full name of the network device.  This name consists of the
193driver name and the unit number (e.g. @code{"scc1"}).
[d9de76f]194The @code{bsp.h} include file usually defines RTEMS_BSP_NETWORK_DRIVER_NAME as
195the name of the primary (or only) network driver.
[ab0c689]196
197@item int (*attach)(struct rtems_bsdnet_ifconfig *conf)
198The address of the driver @code{attach} function.   The network
199initialization function calls this function to configure the driver and
200attach it to the network stack.
[d9de76f]201The @code{bsp.h} include file usually defines RTEMS_BSP_NETWORK_DRIVER_ATTACH as
202the name of the  attach function of the primary (or only) network driver.
[ab0c689]203
204@item struct rtems_bsdnet_ifconfig *next
205A pointer to the network device configuration structure for the next network
206interface, or @code{NULL} if this is the configuration structure of the
207last network interface.
208
209@item char *ip_address
210The Internet address of the device,
211specified in `dotted decimal' (@code{129.128.4.2}) form, or @code{NULL}
212if the device configuration information is being obtained from a
[df4eb76e]213BOOTP/DHCP server.
[ab0c689]214
215@item char *ip_netmask
216The Internet inetwork mask of the device,
217specified in `dotted decimal' (@code{255.255.255.0}) form, or @code{NULL}
218if the device configuration information is being obtained from a
[df4eb76e]219BOOTP/DHCP server.
[ab0c689]220
221
222@item void *hardware_address
223The hardware address of the device, or @code{NULL} if the driver is
224to obtain the hardware address in some other way (usually  by reading
225it from the device or from the bootstrap ROM).
226
227@item int ignore_broadcast
228Zero if the device is to accept broadcast packets, non-zero if the device
229is to ignore broadcast packets.
230
231@item int mtu
232The maximum transmission unit of the device, or zero if the driver
233is to choose a default value (typically 1500 for Ethernet devices).
234
235@item int rbuf_count
236The number of receive buffers to use, or zero if the driver is to
237choose a default value
238
239@item int xbuf_count
240The number of transmit buffers to use, or zero if the driver is to
241choose a default value
242Keep in mind that some network devices may use 4 or more
243transmit descriptors for a single transmit buffer.
244
245@end table
246
[d9de76f]247A complete network configuration specification can be as simple as the one
248shown in the following example.
249This configuration uses a single network interface, gets
250network configuration information
[df4eb76e]251from a BOOTP/DHCP server, and uses the default values for all driver
[d9de76f]252parameters.
253
254@example
255static struct rtems_bsdnet_ifconfig netdriver_config = @{
256        RTEMS_BSP_NETWORK_DRIVER_NAME,
257        RTEMS_BSP_NETWORK_DRIVER_ATTACH
258@};
259struct rtems_bsdnet_config rtems_bsdnet_config = @{
260        &netdriver_config,
261        rtems_bsdnet_do_bootp,
262@};
263@end example
264
[ab0c689]265
266@subsection Network initialization
[d7f33012]267The networking tasks must be started before any network I/O operations
268can be performed. This is done by calling:
269
270
[ab0c689]271@example
272rtems_bsdnet_initialize_network ();
273@end example
274
[a83dd861]275This function is declared in @code{rtems/rtems_bsdnet.h}.
[d7f33012]276t returns 0 on success and -1 on failure with an error code
277in @code{errno}.  It is not possible to undo the effects of
278a partial initialization, though, so the function can be
279called only once irregardless of the return code.  Consequently,
280if the condition for the failure can be corrected, the
281system must be reset to permit another network initialization
282attempt.
[a83dd861]283
284
[ab0c689]285
[f3482e3]286@section Application Programming Interface
287
[ab0c689]288The RTEMS network package provides almost a complete set of BSD network
289services.  The network functions work like their BSD counterparts
290with the following exceptions:
291
[071536e]292@itemize @bullet
[ab0c689]293@item A given socket can be read or written by only one task at a time.
[f3482e3]294
[0ed9ac7]295@item The @code{select} function only works for file descriptors associated
296with sockets.
[f3482e3]297
[ab0c689]298@item You must call @code{openlog} before calling any of the @code{syslog} functions.
[f3482e3]299
[ab0c689]300@item @b{Some of the network functions are not thread-safe.}
301For example the following functions return a pointer to a static
302buffer which remains valid only until the next call:
303
304@table @code
305@item gethostbyaddr
306@item gethostbyname
307@item inet_ntoa
308(@code{inet_ntop} is thread-safe, though).
309@end table
[f3482e3]310
311@item The RTEMS network package gathers statistics.
312
313@item Addition of a mechanism to "tap onto" an interface
314and monitor every packet received and transmitted.
315
316@item Addition of @code{SO_SNDWAKEUP} and @code{SO_RCVWAKEUP} socket options.
317
[ab0c689]318@end itemize
319
[f3482e3]320Some of the new features are discussed in more detail in the following
321sections.
322
[071536e]323@subsection Network Statistics
[f3482e3]324
325There are a number of functions to print statistics gathered by
326the network stack.
[a83dd861]327These function are declared in @code{rtems/rtems_bsdnet.h}.
[f3482e3]328
[ab0c689]329@table @code
330@item rtems_bsdnet_show_if_stats
331Display statistics gathered by network interfaces.
332
333@item rtems_bsdnet_show_ip_stats
334Display IP packet statistics.
335
336@item rtems_bsdnet_show_icmp_stats
337Display ICMP packet statistics.
338
339@item rtems_bsdnet_show_tcp_stats
340Display TCP packet statistics.
341
342@item rtems_bsdnet_show_udp_stats
343Display UDP packet statistics.
344
345@item rtems_bsdnet_show_mbuf_stats
346Display mbuf statistics.
347
348@item rtems_bsdnet_show_inet_routes
349Display the routing table.
350
351@end table
[cdd26ca2]352
[f3482e3]353@subsection Tapping Into an Interface
354
355RTEMS add two new ioctls to the BSD networking code:
[cdd26ca2]356SIOCSIFTAP and SIOCGIFTAP.  These may be used to set and get a
357@i{tap function}.  The tap function will be called for every
358Ethernet packet received by the interface.
359
360These are called like other interface ioctls, such as SIOCSIFADDR.
361When setting the tap function with SIOCSIFTAP, set the ifr_tap field
362of the ifreq struct to the tap function.  When retrieving the tap
363function with SIOCGIFTAP, the current tap function will be returned in
364the ifr_tap field.  To stop tapping packets, call SIOCSIFTAP with a
365ifr_tap field of 0.
366
367The tap function is called like this:
368
369@example
370int tap (struct ifnet *, struct ether_header *, struct mbuf *)
371@end example
372
373The tap function should return 1 if the packet was fully handled, in
374which case the caller will simply discard the mbuf.  The tap function
375should return 0 if the packet should be passed up to the higher
376networking layers.
377
378The tap function is called with the network semaphore locked.  It must
379not make any calls on the application levels of the networking level
380itself.  It is safe to call other non-networking RTEMS functions.
381
[f3482e3]382@subsection Socket Options
383
384RTEMS adds two new @code{SOL_SOCKET} level options for @code{setsockopt} and
385@code{getsockopt}: @code{SO_SNDWAKEUP} and @code{SO_RCVWAKEUP}.  For both, the
386option value should point to a sockwakeup structure.  The sockwakeup
387structure has the following fields:
388
389@example
390@group
391  void    (*sw_pfn) (struct socket *, caddr_t);
392  caddr_t sw_arg;
393@end group
394@end example
395
396These options are used to set a function to be called when there is
[461caa5c]397data available from the socket (@code{SO_RCVWAKEUP}) and when there is space
398available to accept data written to the socket (@code{SO_SNDWAKEUP}).
[f3482e3]399
[461caa5c]400If @code{setsockopt} is called with the @code{SO_RCVWAKEUP} option, and the
[f3482e3]401@code{sw_pfn} field is not zero, then when there is data
402available to be read from
403the socket, the function pointed to by the @code{sw_pfn} field will be
404called.  A pointer to the socket structure will be passed as the first
[461caa5c]405argument to the function.  The @code{sw_arg} field set by the
406@code{SO_RCVWAKEUP} call will be passed as the second argument to the function.
407
408If @code{setsockopt} is called with the @code{SO_SNDWAKEUP}
409function, and the @code{sw_pfn} field is not zero, then when
410there is space available to accept data written to the socket,
411the function pointed to by the @code{sw_pfn} field
[f3482e3]412will be called.  The arguments passed to the function will be as with
413@code{SO_SNDWAKEUP}.
414
415When the function is called, the network semaphore will be locked.
416The function must be careful not to call any networking functions.  It
417is OK to call an RTEMS function; for example, it is OK to send an
418RTEMS event.
419
420The purpose of these functions is to permit a more efficient
421alternative to the select call when dealing with a large number of
422sockets.
423
[33bbd74]424@subsection Time Synchronization Using NTP
425
426@example
427int rtems_bsdnet_synchronize_ntp (int interval, rtems_task_priority priority);
428@end example
429
430If the interval argument is 0 the routine synchronizes the RTEMS time-of-day
431clock with the first NTP server in the rtems_bsdnet_ntpserve array and
432returns.  The priority argument is ignored.
433
434If the interval argument is greater than 0, the routine also starts an
435RTEMS task at the specified priority and polls the NTP server every
436`interval' seconds.  NOTE: This mode of operation has not yet been
437implemented.
438
439On successful synchronization of the RTEMS time-of-day clock the routine
440returns 0.  If an error occurs a message is printed and the routine returns -1
441with an error code in errno.
442There is no timeout -- if there is no response from an NTP server the
443routine will wait forever.
444
445
446
[f3482e3]447
Note: See TracBrowser for help on using the repository browser.