source: rtems-docs/bsp_howto/networking.rst @ 489740f

4.115
Last change on this file since 489740f was 489740f, checked in by Chris Johns <chrisj@…>, on 05/20/16 at 02:47:09

Set SPDX License Identifier in each source file.

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