Mistake on this page?
Report an issue in GitHub or email us
main_page.h
1 /**
2  * @defgroup lwip lwIP
3  *
4  * @defgroup infrastructure Infrastructure
5  *
6  * @defgroup api APIs
7  * lwIP provides three Application Program's Interfaces (APIs) for programs
8  * to use for communication with the TCP/IP code:
9  * - low-level "core" / "callback" or @ref callbackstyle_api.
10  * - higher-level @ref sequential_api.
11  * - BSD-style @ref socket.
12  *
13  * The raw TCP/IP interface allows the application program to integrate
14  * better with the TCP/IP code. Program execution is event based by
15  * having callback functions being called from within the TCP/IP
16  * code. The TCP/IP code and the application program both run in the same
17  * thread. The sequential API has a much higher overhead and is not very
18  * well suited for small systems since it forces a multithreaded paradigm
19  * on the application.
20  *
21  * The raw TCP/IP interface is not only faster in terms of code execution
22  * time but is also less memory intensive. The drawback is that program
23  * development is somewhat harder and application programs written for
24  * the raw TCP/IP interface are more difficult to understand. Still, this
25  * is the preferred way of writing applications that should be small in
26  * code size and memory usage.
27  *
28  * All APIs can be used simultaneously by different application
29  * programs. In fact, the sequential API is implemented as an application
30  * program using the raw TCP/IP interface.
31  *
32  * Do not confuse the lwIP raw API with raw Ethernet or IP sockets.
33  * The former is a way of interfacing the lwIP network stack (including
34  * TCP and UDP), the latter refers to processing raw Ethernet or IP data
35  * instead of TCP connections or UDP packets.
36  *
37  * Raw API applications may never block since all packet processing
38  * (input and output) as well as timer processing (TCP mainly) is done
39  * in a single execution context.
40  *
41  * @defgroup callbackstyle_api "raw" APIs
42  * @ingroup api
43  * Non thread-safe APIs, callback style for maximum performance and minimum
44  * memory footprint.
45  * Program execution is driven by callbacks functions, which are then
46  * invoked by the lwIP core when activity related to that application
47  * occurs. A particular application may register to be notified via a
48  * callback function for events such as incoming data available, outgoing
49  * data sent, error notifications, poll timer expiration, connection
50  * closed, etc. An application can provide a callback function to perform
51  * processing for any or all of these events. Each callback is an ordinary
52  * C function that is called from within the TCP/IP code. Every callback
53  * function is passed the current TCP or UDP connection state as an
54  * argument. Also, in order to be able to keep program specific state,
55  * the callback functions are called with a program specified argument
56  * that is independent of the TCP/IP state.
57  * The raw API (sometimes called native API) is an event-driven API designed
58  * to be used without an operating system that implements zero-copy send and
59  * receive. This API is also used by the core stack for interaction between
60  * the various protocols. It is the only API available when running lwIP
61  * without an operating system.
62  *
63  * @defgroup sequential_api Sequential-style APIs
64  * @ingroup api
65  * Sequential-style APIs, blocking functions. More overhead, but can be called
66  * from any thread except TCPIP thread.
67  * The sequential API provides a way for ordinary, sequential, programs
68  * to use the lwIP stack. It is quite similar to the BSD socket API. The
69  * model of execution is based on the blocking open-read-write-close
70  * paradigm. Since the TCP/IP stack is event based by nature, the TCP/IP
71  * code and the application program must reside in different execution
72  * contexts (threads).
73  *
74  * @defgroup socket Socket API
75  * @ingroup api
76  * BSD-style socket API.\n
77  * Thread-safe, to be called from non-TCPIP threads only.\n
78  * Can be activated by defining @ref LWIP_SOCKET to 1.\n
79  * Header is in posix/sys/socket.h\n
80  * The socket API is a compatibility API for existing applications,
81  * currently it is built on top of the sequential API. It is meant to
82  * provide all functions needed to run socket API applications running
83  * on other platforms (e.g. unix / windows etc.). However, due to limitations
84  * in the specification of this API, there might be incompatibilities
85  * that require small modifications of existing programs.
86  *
87  * @defgroup netifs NETIFs
88  *
89  * @defgroup apps Applications
90  */
91 
92 /**
93  * @mainpage Overview
94  * @verbinclude "README"
95  */
96 
97 /**
98  * @page upgrading Upgrading
99  * @verbinclude "UPGRADING"
100  */
101 
102 /**
103  * @page changelog Changelog
104  *
105  * 2.1.0
106  * -----
107  * * Support TLS via new @ref altcp_api connection API (https, smtps, mqtt over TLS)
108  * * Switch to cmake as the main build system (Makefile file lists are still
109  * maintained for now)
110  * * Improve IPv6 support: support address scopes, support stateless DHCPv6, bugfixes
111  * * Add debug helper asserts to ensure threading/locking requirements are met
112  * * Add sys_mbox_trypost_fromisr() and tcpip_callbackmsg_trycallback_fromisr()
113  * (for FreeRTOS, mainly)
114  * * socket API: support poll(), sendmsg() and recvmsg(); fix problems on close
115  *
116  * Detailed Changelog
117  * ------------------
118  * @verbinclude "CHANGELOG"
119  */
120 
121 /**
122  * @page contrib How to contribute to lwIP
123  * @verbinclude "contrib.txt"
124  */
125 
126 /**
127  * @page pitfalls Common pitfalls
128  *
129  * Multiple Execution Contexts in lwIP code
130  * ========================================
131  *
132  * The most common source of lwIP problems is to have multiple execution contexts
133  * inside the lwIP code.
134  *
135  * lwIP can be used in two basic modes: @ref lwip_nosys (no OS/RTOS
136  * running on target system) or @ref lwip_os (there is an OS running
137  * on the target system).
138  *
139  * See also: @ref multithreading (especially the part about @ref LWIP_ASSERT_CORE_LOCKED()!)
140  *
141  * Mainloop Mode
142  * -------------
143  * In mainloop mode, only @ref callbackstyle_api can be used.
144  * The user has two possibilities to ensure there is only one
145  * exection context at a time in lwIP:
146  *
147  * 1) Deliver RX ethernet packets directly in interrupt context to lwIP
148  * by calling netif->input directly in interrupt. This implies all lwIP
149  * callback functions are called in IRQ context, which may cause further
150  * problems in application code: IRQ is blocked for a long time, multiple
151  * execution contexts in application code etc. When the application wants
152  * to call lwIP, it only needs to disable interrupts during the call.
153  * If timers are involved, even more locking code is needed to lock out
154  * timer IRQ and ethernet IRQ from each other, assuming these may be nested.
155  *
156  * 2) Run lwIP in a mainloop. There is example code here: @ref lwip_nosys.
157  * lwIP is _ONLY_ called from mainloop callstacks here. The ethernet IRQ
158  * has to put received telegrams into a queue which is polled in the
159  * mainloop. Ensure lwIP is _NEVER_ called from an interrupt, e.g.
160  * some SPI IRQ wants to forward data to udp_send() or tcp_write()!
161  *
162  * OS Mode
163  * -------
164  * In OS mode, @ref callbackstyle_api AND @ref sequential_api can be used.
165  * @ref sequential_api are designed to be called from threads other than
166  * the TCPIP thread, so there is nothing to consider here.
167  * But @ref callbackstyle_api functions must _ONLY_ be called from
168  * TCPIP thread. It is a common error to call these from other threads
169  * or from IRQ contexts. ​Ethernet RX needs to deliver incoming packets
170  * in the correct way by sending a message to TCPIP thread, this is
171  * implemented in tcpip_input().​​
172  * Again, ensure lwIP is _NEVER_ called from an interrupt, e.g.
173  * some SPI IRQ wants to forward data to udp_send() or tcp_write()!
174  *
175  * 1) tcpip_callback() can be used get called back from TCPIP thread,
176  * it is safe to call any @ref callbackstyle_api from there.
177  *
178  * 2) Use @ref LWIP_TCPIP_CORE_LOCKING. All @ref callbackstyle_api
179  * functions can be called when lwIP core lock is aquired, see
180  * @ref LOCK_TCPIP_CORE() and @ref UNLOCK_TCPIP_CORE().
181  * These macros cannot be used in an interrupt context!
182  * Note the OS must correctly handle priority inversion for this.
183  *
184  * Cache / DMA issues
185  * ==================
186  *
187  * DMA-capable ethernet hardware and zero-copy RX
188  * ----------------------------------------------
189  *
190  * lwIP changes the content of RECEIVED pbufs in the TCP code path.
191  * This implies one or more cacheline(s) of the RX pbuf become dirty
192  * and need to be flushed before the memory is handed over to the
193  * DMA ethernet hardware for the next telegram to be received.
194  * See http://lists.nongnu.org/archive/html/lwip-devel/2017-12/msg00070.html
195  * for a more detailed explanation.
196  * Also keep in mind the user application may also write into pbufs,
197  * so it is generally a bug not to flush the data cache before handing
198  * a buffer to DMA hardware.
199  *
200  * DMA-capable ethernet hardware and cacheline alignment
201  * -----------------------------------------------------
202  * Nice description about DMA capable hardware and buffer handling:
203  * http://www.pebblebay.com/a-guide-to-using-direct-memory-access-in-embedded-systems-part-two/
204  * Read especially sections "Cache coherency" and "Buffer alignment".
205  */
206 
207 /**
208  * @page bugs Reporting bugs
209  * Please report bugs in the lwIP bug tracker at savannah.\n
210  * BEFORE submitting, please check if the bug has already been reported!\n
211  * https://savannah.nongnu.org/bugs/?group=lwip
212  */
213 
214 /**
215  * @page zerocopyrx Zero-copy RX
216  * The following code is an example for zero-copy RX ethernet driver:
217  * @include ZeroCopyRx.c
218  */
219 
220 /**
221  * @defgroup lwip_nosys Mainloop mode ("NO_SYS")
222  * @ingroup lwip
223  * Use this mode if you do not run an OS on your system. \#define NO_SYS to 1.
224  * Feed incoming packets to netif->input(pbuf, netif) function from mainloop,
225  * *not* *from* *interrupt* *context*. You can allocate a @ref pbuf in interrupt
226  * context and put them into a queue which is processed from mainloop.\n
227  * Call sys_check_timeouts() periodically in the mainloop.\n
228  * Porting: implement all functions in @ref sys_time, @ref sys_prot and
229  * @ref compiler_abstraction.\n
230  * You can only use @ref callbackstyle_api in this mode.\n
231  * Sample code:\n
232  * @include NO_SYS_SampleCode.c
233  */
234 
235 /**
236  * @defgroup lwip_os OS mode (TCPIP thread)
237  * @ingroup lwip
238  * Use this mode if you run an OS on your system. It is recommended to
239  * use an RTOS that correctly handles priority inversion and
240  * to use @ref LWIP_TCPIP_CORE_LOCKING.\n
241  * Porting: implement all functions in @ref sys_layer.\n
242  * You can use @ref callbackstyle_api together with @ref tcpip_callback,
243  * and all @ref sequential_api.
244  */
245 
246 /**
247  * @page sys_init System initalization
248 A truly complete and generic sequence for initializing the lwIP stack
249 cannot be given because it depends on additional initializations for
250 your runtime environment (e.g. timers).
251 
252 We can give you some idea on how to proceed when using the raw API.
253 We assume a configuration using a single Ethernet netif and the
254 UDP and TCP transport layers, IPv4 and the DHCP client.
255 
256 Call these functions in the order of appearance:
257 
258 - lwip_init(): Initialize the lwIP stack and all of its subsystems.
259 
260 - netif_add(struct netif *netif, ...):
261  Adds your network interface to the netif_list. Allocate a struct
262  netif and pass a pointer to this structure as the first argument.
263  Give pointers to cleared ip_addr structures when using DHCP,
264  or fill them with sane numbers otherwise. The state pointer may be NULL.
265 
266  The init function pointer must point to a initialization function for
267  your Ethernet netif interface. The following code illustrates its use.
268 
269 @code{.c}
270  err_t netif_if_init(struct netif *netif)
271  {
272  u8_t i;
273 
274  for (i = 0; i < ETHARP_HWADDR_LEN; i++) {
275  netif->hwaddr[i] = some_eth_addr[i];
276  }
277  init_my_eth_device();
278  return ERR_OK;
279  }
280 @endcode
281 
282  For Ethernet drivers, the input function pointer must point to the lwIP
283  function ethernet_input() declared in "netif/etharp.h". Other drivers
284  must use ip_input() declared in "lwip/ip.h".
285 
286 - netif_set_default(struct netif *netif)
287  Registers the default network interface.
288 
289 - netif_set_link_up(struct netif *netif)
290  This is the hardware link state; e.g. whether cable is plugged for wired
291  Ethernet interface. This function must be called even if you don't know
292  the current state. Having link up and link down events is optional but
293  DHCP and IPv6 discover benefit well from those events.
294 
295 - netif_set_up(struct netif *netif)
296  This is the administrative (= software) state of the netif, when the
297  netif is fully configured this function must be called.
298 
299 - dhcp_start(struct netif *netif)
300  Creates a new DHCP client for this interface on the first call.
301  You can peek in the netif->dhcp struct for the actual DHCP status.
302 
303 - sys_check_timeouts()
304  When the system is running, you have to periodically call
305  sys_check_timeouts() which will handle all timers for all protocols in
306  the stack; add this to your main loop or equivalent.
307  */
308 
309 /**
310  * @page multithreading Multithreading
311  * lwIP started targeting single-threaded environments. When adding multi-
312  * threading support, instead of making the core thread-safe, another
313  * approach was chosen: there is one main thread running the lwIP core
314  * (also known as the "tcpip_thread"). When running in a multithreaded
315  * environment, raw API functions MUST only be called from the core thread
316  * since raw API functions are not protected from concurrent access (aside
317  * from pbuf- and memory management functions). Application threads using
318  * the sequential- or socket API communicate with this main thread through
319  * message passing.
320  *
321  * As such, the list of functions that may be called from
322  * other threads or an ISR is very limited! Only functions
323  * from these API header files are thread-safe:
324  * - api.h
325  * - netbuf.h
326  * - netdb.h
327  * - netifapi.h
328  * - pppapi.h
329  * - sockets.h
330  * - sys.h
331  *
332  * Additionaly, memory (de-)allocation functions may be
333  * called from multiple threads (not ISR!) with NO_SYS=0
334  * since they are protected by @ref SYS_LIGHTWEIGHT_PROT and/or
335  * semaphores.
336  *
337  * Netconn or Socket API functions are thread safe against the
338  * core thread but they are not reentrant at the control block
339  * granularity level. That is, a UDP or TCP control block must
340  * not be shared among multiple threads without proper locking.
341  *
342  * If @ref SYS_LIGHTWEIGHT_PROT is set to 1 and
343  * @ref LWIP_ALLOW_MEM_FREE_FROM_OTHER_CONTEXT is set to 1,
344  * pbuf_free() may also be called from another thread or
345  * an ISR (since only then, mem_free - for PBUF_RAM - may
346  * be called from an ISR: otherwise, the HEAP is only
347  * protected by semaphores).
348  *
349  * How to get threading done right
350  * -------------------------------
351  *
352  * It is strongly recommended to implement the LWIP_ASSERT_CORE_LOCKED()
353  * macro in an application that uses multithreading. lwIP code has
354  * several places where a check for a correct thread context is
355  * implemented which greatly helps the user to get threading done right.
356  * See the example sys_arch.c files in unix and Win32 port
357  * in the contrib repository.
358  *
359  * In short: Copy the functions sys_mark_tcpip_thread() and
360  * sys_check_core_locking() to your port and modify them to work with your OS.
361  * Then let @ref LWIP_ASSERT_CORE_LOCKED() and @ref LWIP_MARK_TCPIP_THREAD()
362  * point to these functions.
363  *
364  * If you use @ref LWIP_TCPIP_CORE_LOCKING, you also need to copy and adapt
365  * the functions sys_lock_tcpip_core() and sys_unlock_tcpip_core().
366  * Let @ref LOCK_TCPIP_CORE() and @ref UNLOCK_TCPIP_CORE() point
367  * to these functions.
368  */
369 
370 /**
371  * @page optimization Optimization hints
372 The first thing you want to optimize is the lwip_standard_checksum()
373 routine from src/core/inet.c. You can override this standard
374 function with the \#define LWIP_CHKSUM your_checksum_routine().
375 
376 There are C examples given in inet.c or you might want to
377 craft an assembly function for this. RFC1071 is a good
378 introduction to this subject.
379 
380 Other significant improvements can be made by supplying
381 assembly or inline replacements for htons() and htonl()
382 if you're using a little-endian architecture.
383 \#define lwip_htons(x) your_htons()
384 \#define lwip_htonl(x) your_htonl()
385 If you \#define them to htons() and htonl(), you should
386 \#define LWIP_DONT_PROVIDE_BYTEORDER_FUNCTIONS to prevent lwIP from
387 defining htonx / ntohx compatibility macros.
388 
389 Check your network interface driver if it reads at
390 a higher speed than the maximum wire-speed. If the
391 hardware isn't serviced frequently and fast enough
392 buffer overflows are likely to occur.
393 
394 E.g. when using the cs8900 driver, call cs8900if_service(ethif)
395 as frequently as possible. When using an RTOS let the cs8900 interrupt
396 wake a high priority task that services your driver using a binary
397 semaphore or event flag. Some drivers might allow additional tuning
398 to match your application and network.
399 
400 For a production release it is recommended to set LWIP_STATS to 0.
401 Note that speed performance isn't influenced much by simply setting
402 high values to the memory options.
403  */
Important Information for this Arm website

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies. If you are not happy with the use of these cookies, please review our Cookie Policy to learn how they can be disabled. By disabling cookies, some features of the site will not work.