source: rtems-docs/bsp-howto/networking.rst @ cc1d3fa

4.115
Last change on this file since cc1d3fa was cc1d3fa, checked in by Chris Johns <chrisj@…>, on 11/09/16 at 01:33:08

bsp-howto: Fix header levels.

  • Property mode set to 100644
File size: 12.6 KB
Line 
1.. comment SPDX-License-Identifier: CC-BY-SA-4.0
2
3.. COMMENT: COPYRIGHT (c) 1988-2002.
4.. COMMENT: On-Line Applications Research Corporation (OAR).
5.. COMMENT: All rights reserved.
6
7Networking Driver
8*****************
9
10Introduction
11============
12
13This chapter is intended to provide an introduction to the procedure for
14writing RTEMS network device drivers.  The example code is taken from the
15'Generic 68360' network device driver.  The source code for this driver is
16located in the ``c/src/lib/libbsp/m68k/gen68360/network`` directory in the
17RTEMS source code distribution.  Having a copy of this driver at hand when
18reading the following notes will help significantly.
19
20.. sidebar:: *Legacy Networking Stack*
21
22  This docuemntation is for the legacy FreeBSD networking stack in the RTEMS
23  source tree.
24
25Learn about the network device
26==============================
27
28Before starting to write the network driver become completely familiar with the
29programmer's view of the device.  The following points list some of the details
30of the device that must be understood before a driver can be written.
31
32- Does the device use DMA to transfer packets to and from memory or does the
33  processor have to copy packets to and from memory on the device?
34
35- If the device uses DMA, is it capable of forming a single outgoing packet
36  from multiple fragments scattered in separate memory buffers?
37
38- If the device uses DMA, is it capable of chaining multiple outgoing packets,
39  or does each outgoing packet require intervention by the driver?
40
41- Does the device automatically pad short frames to the minimum 64 bytes or
42  does the driver have to supply the padding?
43
44- Does the device automatically retry a transmission on detection of a
45  collision?
46
47- If the device uses DMA, is it capable of buffering multiple packets to
48  memory, or does the receiver have to be restarted after the arrival of each
49  packet?
50
51- How are packets that are too short, too long, or received with CRC errors
52  handled?  Does the device automatically continue reception or does the driver
53  have to intervene?
54
55- How is the device Ethernet address set?  How is the device programmed to
56  accept or reject broadcast and multicast packets?
57
58- What interrupts does the device generate?  Does it generate an interrupt for
59  each incoming packet, or only for packets received without error?  Does it
60  generate an interrupt for each packet transmitted, or only when the transmit
61  queue is empty?  What happens when a transmit error is detected?
62
63In addition, some controllers have specific questions regarding board specific
64configuration.  For example, the SONIC Ethernet controller has a very
65configurable data bus interface.  It can even be configured for sixteen and
66thirty-two bit data buses.  This type of information should be obtained from
67the board vendor.
68
69Understand the network scheduling conventions
70=============================================
71
72When writing code for the driver transmit and receive tasks, take care to
73follow the network scheduling conventions.  All tasks which are associated with
74networking share various data structures and resources.  To ensure the
75consistency of these structures the tasks execute only when they hold the
76network semaphore (``rtems_bsdnet_semaphore``).  The transmit and receive tasks
77must abide by this protocol.  Be very careful to avoid 'deadly embraces' with
78the other network tasks.  A number of routines are provided to make it easier
79for the network driver code to conform to the network task scheduling
80conventions.
81
82- ``void rtems_bsdnet_semaphore_release(void)``
83  This function releases the network semaphore.  The network driver tasks must
84  call this function immediately before making any blocking RTEMS request.
85
86- ``void rtems_bsdnet_semaphore_obtain(void)``
87  This function obtains the network semaphore.  If a network driver task has
88  released the network semaphore to allow other network-related tasks to run
89  while the task blocks, then this function must be called to reobtain the
90  semaphore immediately after the return from the blocking RTEMS request.
91
92- ``rtems_bsdnet_event_receive(rtems_event_set, rtems_option, rtems_interval,
93  rtems_event_set *)``
94  The network driver task should call this function when it wishes to wait for
95  an event.  This function releases the network semaphore, calls
96  ``rtems_event_receive`` to wait for the specified event or events and
97  reobtains the semaphore.  The value returned is the value returned by the
98  ``rtems_event_receive``.
99
100Network Driver Makefile
101=======================
102
103Network drivers are considered part of the BSD network package and as such are
104to be compiled with the appropriate flags.  This can be accomplished by adding
105``-D__INSIDE_RTEMS_BSD_TCPIP_STACK__`` to the ``command line``.  If the driver
106is inside the RTEMS source tree or is built using the RTEMS application
107Makefiles, then adding the following line accomplishes this:
108
109.. code-block:: makefile
110
111    DEFINES += -D__INSIDE_RTEMS_BSD_TCPIP_STACK__
112
113This is equivalent to the following list of definitions.  Early versions of the
114RTEMS BSD network stack required that all of these be defined.
115
116.. code-block:: makefile
117
118    -D_COMPILING_BSD_KERNEL_ -DKERNEL -DINET -DNFS -DDIAGNOSTIC -DBOOTP_COMPAT
119
120Defining these macros tells the network header files that the driver is to be
121compiled with extended visibility into the network stack.  This is in sharp
122contrast to applications that simply use the network stack.  Applications do
123not require this level of visibility and should stick to the portable
124application level API.
125
126As a direct result of being logically internal to the network stack, network
127drivers use the BSD memory allocation routines This means, for example, that
128malloc takes three arguments.  See the SONIC device driver
129(``c/src/lib/libchip/network/sonic.c``) for an example of this.  Because of
130this, network drivers should not include ``<stdlib.h>``.  Doing so will result
131in conflicting definitions of ``malloc()``.
132
133*Application level* code including network servers such as the FTP daemon are
134*not* part of the BSD kernel network code and should not be compiled with the
135BSD network flags.  They should include ``<stdlib.h>`` and not define the
136network stack visibility macros.
137
138Write the Driver Attach Function
139================================
140
141The driver attach function is responsible for configuring the driver and making
142the connection between the network stack and the driver.
143
144Driver attach functions take a pointer to an ``rtems_bsdnet_ifconfig``
145structure as their only argument.  and set the driver parameters based on the
146values in this structure.  If an entry in the configuration structure is zero
147the attach function chooses an appropriate default value for that parameter.
148
149The driver should then set up several fields in the ifnet structure in the
150device-dependent data structure supplied and maintained by the driver:
151
152``ifp->if_softc``
153    Pointer to the device-dependent data.  The first entry in the
154    device-dependent data structure must be an ``arpcom`` structure.
155
156``ifp->if_name``
157    The name of the device.  The network stack uses this string and the device
158    number for device name lookups.  The device name should be obtained from
159    the ``name`` entry in the configuration structure.
160
161``ifp->if_unit``
162    The device number.  The network stack uses this number and the device name
163    for device name lookups.  For example, if ``ifp->if_name`` is ``scc`` and
164    ``ifp->if_unit`` is ``1``, the full device name would be ``scc1``.  The
165    unit number should be obtained from the ``name`` entry in the configuration
166    structure.
167
168``ifp->if_mtu``
169    The maximum transmission unit for the device.  For Ethernet devices this
170    value should almost always be 1500.
171
172``ifp->if_flags``
173    The device flags.  Ethernet devices should set the flags to
174    ``IFF_BROADCAST|IFF_SIMPLEX``, indicating that the device can broadcast
175    packets to multiple destinations and does not receive and transmit at the
176    same time.
177
178``ifp->if_snd.ifq_maxlen``
179    The maximum length of the queue of packets waiting to be sent to the
180    driver.  This is normally set to ``ifqmaxlen``.
181
182``ifp->if_init``
183    The address of the driver initialization function.
184
185``ifp->if_start``
186    The address of the driver start function.
187
188``ifp->if_ioctl``
189    The address of the driver ioctl function.
190
191``ifp->if_output``
192    The address of the output function.  Ethernet devices should set this to
193    ``ether_output``.
194
195RTEMS provides a function to parse the driver name in the configuration
196structure into a device name and unit number.
197
198.. code-block:: c
199
200    int rtems_bsdnet_parse_driver_name (
201      const struct rtems_bsdnet_ifconfig  *config,
202      char                               **namep
203    );
204
205The function takes two arguments; a pointer to the configuration structure and
206a pointer to a pointer to a character.  The function parses the configuration
207name entry, allocates memory for the driver name, places the driver name in
208this memory, sets the second argument to point to the name and returns the unit
209number.  On error, a message is printed and ``-1`` is returned.
210
211Once the attach function has set up the above entries it must link the driver
212data structure onto the list of devices by calling ``if_attach``.  Ethernet
213devices should then call ``ether_ifattach``.  Both functions take a pointer to
214the device's ``ifnet`` structure as their only argument.
215
216The attach function should return a non-zero value to indicate that the driver
217has been successfully configured and attached.
218
219Write the Driver Start Function.
220================================
221
222This function is called each time the network stack wants to start the
223transmitter.  This occures whenever the network stack adds a packet to a
224device's send queue and the ``IFF_OACTIVE`` bit in the device's ``if_flags`` is
225not set.
226
227For many devices this function need only set the ``IFF_OACTIVE`` bit in the
228``if_flags`` and send an event to the transmit task indicating that a packet is
229in the driver transmit queue.
230
231Write the Driver Initialization Function.
232=========================================
233
234This function should initialize the device, attach to interrupt handler, and
235start the driver transmit and receive tasks.  The function:
236
237.. code-block:: c
238
239    rtems_id rtems_bsdnet_newproc(
240      char *name,
241      int   stacksize,
242      void  (*entry)(void *),
243      void *arg
244    );
245
246should be used to start the driver tasks.
247
248Note that the network stack may call the driver initialization function more
249than once.  Make sure multiple versions of the receive and transmit tasks are
250not accidentally started.
251
252Write the Driver Transmit Task
253==============================
254
255This task is reponsible for removing packets from the driver send queue and
256sending them to the device.  The task should block waiting for an event from
257the driver start function indicating that packets are waiting to be
258transmitted.  When the transmit task has drained the driver send queue the task
259should clear the ``IFF_OACTIVE`` bit in ``if_flags`` and block until another
260outgoing packet is queued.
261
262Write the Driver Receive Task
263=============================
264
265This task should block until a packet arrives from the device.  If the device
266is an Ethernet interface the function ``ether_input`` should be called to
267forward the packet to the network stack.  The arguments to ``ether_input`` are
268a pointer to the interface data structure, a pointer to the ethernet header and
269a pointer to an mbuf containing the packet itself.
270
271Write the Driver Interrupt Handler
272==================================
273
274A typical interrupt handler will do nothing more than the hardware manipulation
275required to acknowledge the interrupt and send an RTEMS event to wake up the
276driver receive or transmit task waiting for the event.  Network interface
277interrupt handlers must not make any calls to other network routines.
278
279Write the Driver IOCTL Function
280===============================
281
282This function handles ioctl requests directed at the device.  The ioctl
283commands which must be handled are:
284
285``SIOCGIFADDR``, ``SIOCSIFADDR``
286    If the device is an Ethernet interface these commands should be passed on
287    to ``ether_ioctl``.
288
289``SIOCSIFFLAGS``
290    This command should be used to start or stop the device, depending on the
291    state of the interface ``IFF_UP`` and``IFF_RUNNING`` bits in ``if_flags``:
292
293    ``IFF_RUNNING``
294        Stop the device.
295
296    ``IFF_UP``
297        Start the device.
298
299    ``IFF_UP|IFF_RUNNING``
300        Stop then start the device.
301
302    ``0``
303        Do nothing.
304
305Write the Driver Statistic-Printing Function
306============================================
307
308This function should print the values of any statistic/diagnostic counters the
309network driver may use.  The driver ioctl function should call the
310statistic-printing function when the ioctl command is ``SIO_RTEMS_SHOW_STATS``.
Note: See TracBrowser for help on using the repository browser.