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 mem_err Debugging memory pool sizes 209 * If you have issues with lwIP and you are using memory pools, check that your pools 210 * are correctly sized.\n 211 * To debug pool sizes, \#define LWIP_STATS and MEMP_STATS to 1. Check the global variable 212 * lwip_stats.memp[] using a debugger. If the "err" member of a pool is > 0, the pool 213 * may be too small for your application and you need to increase its size. 214 */ 215 216 /** 217 * @page bugs Reporting bugs 218 * Please report bugs in the lwIP bug tracker at savannah.\n 219 * BEFORE submitting, please check if the bug has already been reported!\n 220 * https://savannah.nongnu.org/bugs/?group=lwip 221 */ 222 223 /** 224 * @page zerocopyrx Zero-copy RX 225 * The following code is an example for zero-copy RX ethernet driver: 226 * @include ZeroCopyRx.c 227 */ 228 229 /** 230 * @defgroup lwip_nosys Mainloop mode ("NO_SYS") 231 * @ingroup lwip 232 * Use this mode if you do not run an OS on your system. \#define NO_SYS to 1. 233 * Feed incoming packets to netif->input(pbuf, netif) function from mainloop, 234 * *not* *from* *interrupt* *context*. You can allocate a @ref pbuf in interrupt 235 * context and put them into a queue which is processed from mainloop.\n 236 * Call sys_check_timeouts() periodically in the mainloop.\n 237 * Porting: implement all functions in @ref sys_time, @ref sys_prot and 238 * @ref compiler_abstraction.\n 239 * You can only use @ref callbackstyle_api in this mode.\n 240 * Sample code:\n 241 * @include NO_SYS_SampleCode.c 242 */ 243 244 /** 245 * @defgroup lwip_os OS mode (TCPIP thread) 246 * @ingroup lwip 247 * Use this mode if you run an OS on your system. It is recommended to 248 * use an RTOS that correctly handles priority inversion and 249 * to use @ref LWIP_TCPIP_CORE_LOCKING.\n 250 * Porting: implement all functions in @ref sys_layer.\n 251 * You can use @ref callbackstyle_api together with @ref tcpip_callback, 252 * and all @ref sequential_api. 253 */ 254 255 /** 256 * @page sys_init System initalization 257 A truly complete and generic sequence for initializing the lwIP stack 258 cannot be given because it depends on additional initializations for 259 your runtime environment (e.g. timers). 260 261 We can give you some idea on how to proceed when using the raw API. 262 We assume a configuration using a single Ethernet netif and the 263 UDP and TCP transport layers, IPv4 and the DHCP client. 264 265 Call these functions in the order of appearance: 266 267 - lwip_init(): Initialize the lwIP stack and all of its subsystems. 268 269 - netif_add(struct netif *netif, ...): 270 Adds your network interface to the netif_list. Allocate a struct 271 netif and pass a pointer to this structure as the first argument. 272 Give pointers to cleared ip_addr structures when using DHCP, 273 or fill them with sane numbers otherwise. The state pointer may be NULL. 274 275 The init function pointer must point to a initialization function for 276 your Ethernet netif interface. The following code illustrates its use. 277 278 @code{.c} 279 err_t netif_if_init(struct netif *netif) 280 { 281 u8_t i; 282 283 for (i = 0; i < ETHARP_HWADDR_LEN; i++) { 284 netif->hwaddr[i] = some_eth_addr[i]; 285 } 286 init_my_eth_device(); 287 return ERR_OK; 288 } 289 @endcode 290 291 For Ethernet drivers, the input function pointer must point to the lwIP 292 function ethernet_input() declared in "netif/etharp.h". Other drivers 293 must use ip_input() declared in "lwip/ip.h". 294 295 - netif_set_default(struct netif *netif) 296 Registers the default network interface. 297 298 - netif_set_link_up(struct netif *netif) 299 This is the hardware link state; e.g. whether cable is plugged for wired 300 Ethernet interface. This function must be called even if you don't know 301 the current state. Having link up and link down events is optional but 302 DHCP and IPv6 discover benefit well from those events. 303 304 - netif_set_up(struct netif *netif) 305 This is the administrative (= software) state of the netif, when the 306 netif is fully configured this function must be called. 307 308 - dhcp_start(struct netif *netif) 309 Creates a new DHCP client for this interface on the first call. 310 You can peek in the netif->dhcp struct for the actual DHCP status. 311 312 - sys_check_timeouts() 313 When the system is running, you have to periodically call 314 sys_check_timeouts() which will handle all timers for all protocols in 315 the stack; add this to your main loop or equivalent. 316 */ 317 318 /** 319 * @page multithreading Multithreading 320 * lwIP started targeting single-threaded environments. When adding multi- 321 * threading support, instead of making the core thread-safe, another 322 * approach was chosen: there is one main thread running the lwIP core 323 * (also known as the "tcpip_thread"). When running in a multithreaded 324 * environment, raw API functions MUST only be called from the core thread 325 * since raw API functions are not protected from concurrent access (aside 326 * from pbuf- and memory management functions). Application threads using 327 * the sequential- or socket API communicate with this main thread through 328 * message passing. 329 * 330 * As such, the list of functions that may be called from 331 * other threads or an ISR is very limited! Only functions 332 * from these API header files are thread-safe: 333 * - api.h 334 * - netbuf.h 335 * - netdb.h 336 * - netifapi.h 337 * - pppapi.h 338 * - sockets.h 339 * - sys.h 340 * 341 * Additionaly, memory (de-)allocation functions may be 342 * called from multiple threads (not ISR!) with NO_SYS=0 343 * since they are protected by @ref SYS_LIGHTWEIGHT_PROT and/or 344 * semaphores. 345 * 346 * Netconn or Socket API functions are thread safe against the 347 * core thread but they are not reentrant at the control block 348 * granularity level. That is, a UDP or TCP control block must 349 * not be shared among multiple threads without proper locking. 350 * 351 * If @ref SYS_LIGHTWEIGHT_PROT is set to 1 and 352 * @ref LWIP_ALLOW_MEM_FREE_FROM_OTHER_CONTEXT is set to 1, 353 * pbuf_free() may also be called from another thread or 354 * an ISR (since only then, mem_free - for PBUF_RAM - may 355 * be called from an ISR: otherwise, the HEAP is only 356 * protected by semaphores). 357 * 358 * How to get threading done right 359 * ------------------------------- 360 * 361 * It is strongly recommended to implement the LWIP_ASSERT_CORE_LOCKED() 362 * macro in an application that uses multithreading. lwIP code has 363 * several places where a check for a correct thread context is 364 * implemented which greatly helps the user to get threading done right. 365 * See the example sys_arch.c files in unix and Win32 port 366 * in the contrib repository. 367 * 368 * In short: Copy the functions sys_mark_tcpip_thread() and 369 * sys_check_core_locking() to your port and modify them to work with your OS. 370 * Then let @ref LWIP_ASSERT_CORE_LOCKED() and @ref LWIP_MARK_TCPIP_THREAD() 371 * point to these functions. 372 * 373 * If you use @ref LWIP_TCPIP_CORE_LOCKING, you also need to copy and adapt 374 * the functions sys_lock_tcpip_core() and sys_unlock_tcpip_core(). 375 * Let @ref LOCK_TCPIP_CORE() and @ref UNLOCK_TCPIP_CORE() point 376 * to these functions. 377 */ 378 379 /** 380 * @page optimization Optimization hints 381 The first thing you want to optimize is the lwip_standard_checksum() 382 routine from src/core/inet.c. You can override this standard 383 function with the \#define LWIP_CHKSUM your_checksum_routine(). 384 385 There are C examples given in inet.c or you might want to 386 craft an assembly function for this. RFC1071 is a good 387 introduction to this subject. 388 389 Other significant improvements can be made by supplying 390 assembly or inline replacements for htons() and htonl() 391 if you're using a little-endian architecture. 392 \#define lwip_htons(x) your_htons() 393 \#define lwip_htonl(x) your_htonl() 394 If you \#define them to htons() and htonl(), you should 395 \#define LWIP_DONT_PROVIDE_BYTEORDER_FUNCTIONS to prevent lwIP from 396 defining htonx / ntohx compatibility macros. 397 398 Check your network interface driver if it reads at 399 a higher speed than the maximum wire-speed. If the 400 hardware isn't serviced frequently and fast enough 401 buffer overflows are likely to occur. 402 403 E.g. when using the cs8900 driver, call cs8900if_service(ethif) 404 as frequently as possible. When using an RTOS let the cs8900 interrupt 405 wake a high priority task that services your driver using a binary 406 semaphore or event flag. Some drivers might allow additional tuning 407 to match your application and network. 408 409 For a production release it is recommended to set LWIP_STATS to 0. 410 Note that speed performance isn't influenced much by simply setting 411 high values to the memory options. 412 */ 413