main_page.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  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.<br>
  77. * Thread-safe, to be called from non-TCPIP threads only.<br>
  78. * Can be activated by defining @ref LWIP_SOCKET to 1.<br>
  79. * Header is in posix/sys/socket.h<br>
  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. * @mainpage Overview
  93. * @verbinclude "README"
  94. */
  95. /**
  96. * @page upgrading Upgrading
  97. * @verbinclude "UPGRADING"
  98. */
  99. /**
  100. * @page changelog Changelog
  101. *
  102. * 2.1.0
  103. * -----
  104. * * Support TLS via new @ref altcp_api connection API (https, smtps, mqtt over TLS)
  105. * * Switch to cmake as the main build system (Makefile file lists are still
  106. * maintained for now)
  107. * * Improve IPv6 support: support address scopes, support stateless DHCPv6, bugfixes
  108. * * Add debug helper asserts to ensure threading/locking requirements are met
  109. * * Add sys_mbox_trypost_fromisr() and tcpip_callbackmsg_trycallback_fromisr()
  110. * (for FreeRTOS, mainly)
  111. * * socket API: support poll(), sendmsg() and recvmsg(); fix problems on close
  112. *
  113. * Detailed Changelog
  114. * ------------------
  115. * @verbinclude "CHANGELOG"
  116. */
  117. /**
  118. * @page contrib How to contribute to lwIP
  119. * @verbinclude "contrib.txt"
  120. */
  121. /**
  122. * @page cmake CMake build system
  123. * @verbinclude "BUILDING"
  124. */
  125. /**
  126. * @page pitfalls Common pitfalls
  127. *
  128. * Multiple Execution Contexts in lwIP code
  129. * ========================================
  130. *
  131. * The most common source of lwIP problems is to have multiple execution contexts
  132. * inside the lwIP code.
  133. *
  134. * lwIP can be used in two basic modes: @ref lwip_nosys (no OS/RTOS
  135. * running on target system) or @ref lwip_os (there is an OS running
  136. * on the target system).
  137. *
  138. * See also: @ref multithreading (especially the part about @ref LWIP_ASSERT_CORE_LOCKED()!)
  139. *
  140. * Mainloop Mode
  141. * -------------
  142. * In mainloop mode, only @ref callbackstyle_api can be used.
  143. * The user has two possibilities to ensure there is only one
  144. * execution context at a time in lwIP:
  145. *
  146. * 1) Deliver RX ethernet packets directly in interrupt context to lwIP
  147. * by calling netif->input directly in interrupt. This implies all lwIP
  148. * callback functions are called in IRQ context, which may cause further
  149. * problems in application code: IRQ is blocked for a long time, multiple
  150. * execution contexts in application code etc. When the application wants
  151. * to call lwIP, it only needs to disable interrupts during the call.
  152. * If timers are involved, even more locking code is needed to lock out
  153. * timer IRQ and ethernet IRQ from each other, assuming these may be nested.
  154. *
  155. * 2) Run lwIP in a mainloop. There is example code here: @ref lwip_nosys.
  156. * lwIP is _ONLY_ called from mainloop callstacks here. The ethernet IRQ
  157. * has to put received telegrams into a queue which is polled in the
  158. * mainloop. Ensure lwIP is _NEVER_ called from an interrupt, e.g.
  159. * some SPI IRQ wants to forward data to udp_send() or tcp_write()!
  160. *
  161. * OS Mode
  162. * -------
  163. * In OS mode, @ref callbackstyle_api AND @ref sequential_api can be used.
  164. * @ref sequential_api are designed to be called from threads other than
  165. * the TCPIP thread, so there is nothing to consider here.
  166. * But @ref callbackstyle_api functions must _ONLY_ be called from
  167. * TCPIP thread. It is a common error to call these from other threads
  168. * or from IRQ contexts. ​Ethernet RX needs to deliver incoming packets
  169. * in the correct way by sending a message to TCPIP thread, this is
  170. * implemented in tcpip_input().​​
  171. * Again, ensure lwIP is _NEVER_ called from an interrupt, e.g.
  172. * some SPI IRQ wants to forward data to udp_send() or tcp_write()!
  173. *
  174. * 1) tcpip_callback() can be used get called back from TCPIP thread,
  175. * it is safe to call any @ref callbackstyle_api from there.
  176. *
  177. * 2) Use @ref LWIP_TCPIP_CORE_LOCKING. All @ref callbackstyle_api
  178. * functions can be called when lwIP core lock is acquired, see
  179. * @ref LOCK_TCPIP_CORE() and @ref UNLOCK_TCPIP_CORE().
  180. * These macros cannot be used in an interrupt context!
  181. * Note the OS must correctly handle priority inversion for this.
  182. *
  183. * Cache / DMA issues
  184. * ==================
  185. *
  186. * DMA-capable ethernet hardware and zero-copy RX
  187. * ----------------------------------------------
  188. *
  189. * lwIP changes the content of RECEIVED pbufs in the TCP code path.
  190. * This implies one or more cacheline(s) of the RX pbuf become dirty
  191. * and need to be flushed before the memory is handed over to the
  192. * DMA ethernet hardware for the next telegram to be received.
  193. * See http://lists.nongnu.org/archive/html/lwip-devel/2017-12/msg00070.html
  194. * for a more detailed explanation.
  195. * Also keep in mind the user application may also write into pbufs,
  196. * so it is generally a bug not to flush the data cache before handing
  197. * a buffer to DMA hardware.
  198. *
  199. * DMA-capable ethernet hardware and cacheline alignment
  200. * -----------------------------------------------------
  201. * Nice description about DMA capable hardware and buffer handling:
  202. * http://www.pebblebay.com/a-guide-to-using-direct-memory-access-in-embedded-systems-part-two/
  203. * Read especially sections "Cache coherency" and "Buffer alignment".
  204. */
  205. /**
  206. * @page mem_err Debugging memory pool sizes
  207. * If you have issues with lwIP and you are using memory pools, check that your pools
  208. * are correctly sized.<br>
  209. * To debug pool sizes, \#define LWIP_STATS and MEMP_STATS to 1. Check the global variable
  210. * lwip_stats.memp[] using a debugger. If the "err" member of a pool is > 0, the pool
  211. * may be too small for your application and you need to increase its size.
  212. */
  213. /**
  214. * @page bugs Reporting bugs
  215. * Please report bugs in the lwIP bug tracker at savannah.<br>
  216. * BEFORE submitting, please check if the bug has already been reported!<br>
  217. * https://savannah.nongnu.org/bugs/?group=lwip
  218. */
  219. /**
  220. * @page zerocopyrx Zero-copy RX
  221. * The following code is an example for zero-copy RX ethernet driver:
  222. * @include ZeroCopyRx.c
  223. */
  224. /**
  225. * @defgroup lwip_nosys Mainloop mode ("NO_SYS")
  226. * @ingroup lwip
  227. * Use this mode if you do not run an OS on your system. \#define NO_SYS to 1.
  228. * Feed incoming packets to netif->input(pbuf, netif) function from mainloop,
  229. * *not* *from* *interrupt* *context*. You can allocate a @ref pbuf in interrupt
  230. * context and put them into a queue which is processed from mainloop.<br>
  231. * Call sys_check_timeouts() periodically in the mainloop.<br>
  232. * Porting: implement all functions in @ref sys_time, @ref sys_prot and
  233. * @ref compiler_abstraction.<br>
  234. * You can only use @ref callbackstyle_api in this mode.<br>
  235. * Sample code:
  236. * @include NO_SYS_SampleCode.c
  237. */
  238. /**
  239. * @defgroup lwip_os OS mode (TCPIP thread)
  240. * @ingroup lwip
  241. * Use this mode if you run an OS on your system. It is recommended to
  242. * use an RTOS that correctly handles priority inversion and
  243. * to use @ref LWIP_TCPIP_CORE_LOCKING.<br>
  244. * Porting: implement all functions in @ref sys_layer.<br>
  245. * You can use @ref callbackstyle_api together with @ref tcpip_callback,
  246. * and all @ref sequential_api.
  247. */
  248. /**
  249. * @page sys_init System initialization
  250. A truly complete and generic sequence for initializing the lwIP stack
  251. cannot be given because it depends on additional initializations for
  252. your runtime environment (e.g. timers).
  253. We can give you some idea on how to proceed when using the raw API.
  254. We assume a configuration using a single Ethernet netif and the
  255. UDP and TCP transport layers, IPv4 and the DHCP client.
  256. Call these functions in the order of appearance:
  257. - lwip_init(): Initialize the lwIP stack and all of its subsystems.
  258. - netif_add(struct netif *netif, ...):
  259. Adds your network interface to the netif_list. Allocate a struct
  260. netif and pass a pointer to this structure as the first argument.
  261. Give pointers to cleared ip_addr structures when using DHCP,
  262. or fill them with sane numbers otherwise. The state pointer may be NULL.
  263. The init function pointer must point to a initialization function for
  264. your Ethernet netif interface. The following code illustrates its use.
  265. @code{.c}
  266. err_t netif_if_init(struct netif *netif)
  267. {
  268. u8_t i;
  269. for (i = 0; i < ETHARP_HWADDR_LEN; i++) {
  270. netif->hwaddr[i] = some_eth_addr[i];
  271. }
  272. init_my_eth_device();
  273. return ERR_OK;
  274. }
  275. @endcode
  276. For Ethernet drivers, the input function pointer must point to the lwIP
  277. function ethernet_input() declared in "netif/etharp.h". Other drivers
  278. must use ip_input() declared in "lwip/ip.h".
  279. - netif_set_default(struct netif *netif)
  280. Registers the default network interface.
  281. - netif_set_link_up(struct netif *netif)
  282. This is the hardware link state; e.g. whether cable is plugged for wired
  283. Ethernet interface. This function must be called even if you don't know
  284. the current state. Having link up and link down events is optional but
  285. DHCP and IPv6 discover benefit well from those events.
  286. - netif_set_up(struct netif *netif)
  287. This is the administrative (= software) state of the netif, when the
  288. netif is fully configured this function must be called.
  289. - dhcp_start(struct netif *netif)
  290. Creates a new DHCP client for this interface on the first call.
  291. You can peek in the netif->dhcp struct for the actual DHCP status.
  292. - sys_check_timeouts()
  293. When the system is running, you have to periodically call
  294. sys_check_timeouts() which will handle all timers for all protocols in
  295. the stack; add this to your main loop or equivalent.
  296. */
  297. /**
  298. * @page multithreading Multithreading
  299. * lwIP started targeting single-threaded environments. When adding multi-
  300. * threading support, instead of making the core thread-safe, another
  301. * approach was chosen: there is one main thread running the lwIP core
  302. * (also known as the "tcpip_thread"). When running in a multithreaded
  303. * environment, raw API functions MUST only be called from the core thread
  304. * since raw API functions are not protected from concurrent access (aside
  305. * from pbuf- and memory management functions). Application threads using
  306. * the sequential- or socket API communicate with this main thread through
  307. * message passing.
  308. *
  309. * As such, the list of functions that may be called from
  310. * other threads or an ISR is very limited! Only functions
  311. * from these API header files are thread-safe:
  312. * - api.h
  313. * - netbuf.h
  314. * - netdb.h
  315. * - netifapi.h
  316. * - pppapi.h
  317. * - sockets.h
  318. * - sys.h
  319. *
  320. * Additionally, memory (de-)allocation functions may be
  321. * called from multiple threads (not ISR!) with NO_SYS=0
  322. * since they are protected by @ref SYS_LIGHTWEIGHT_PROT and/or
  323. * semaphores.
  324. *
  325. * Netconn or Socket API functions are thread safe against the
  326. * core thread but they are not reentrant at the control block
  327. * granularity level. That is, a UDP or TCP control block must
  328. * not be shared among multiple threads without proper locking.
  329. *
  330. * If @ref SYS_LIGHTWEIGHT_PROT is set to 1 and
  331. * @ref LWIP_ALLOW_MEM_FREE_FROM_OTHER_CONTEXT is set to 1,
  332. * pbuf_free() may also be called from another thread or
  333. * an ISR (since only then, mem_free - for PBUF_RAM - may
  334. * be called from an ISR: otherwise, the HEAP is only
  335. * protected by semaphores).
  336. *
  337. * How to get threading done right
  338. * -------------------------------
  339. *
  340. * It is strongly recommended to implement the LWIP_ASSERT_CORE_LOCKED()
  341. * macro in an application that uses multithreading. lwIP code has
  342. * several places where a check for a correct thread context is
  343. * implemented which greatly helps the user to get threading done right.
  344. * See the example sys_arch.c files in unix and Win32 port
  345. * in the lwIP/contrib subdirectory.
  346. *
  347. * In short: Copy the functions sys_mark_tcpip_thread() and
  348. * sys_check_core_locking() to your port and modify them to work with your OS.
  349. * Then let @ref LWIP_ASSERT_CORE_LOCKED() and @ref LWIP_MARK_TCPIP_THREAD()
  350. * point to these functions.
  351. *
  352. * If you use @ref LWIP_TCPIP_CORE_LOCKING, you also need to copy and adapt
  353. * the functions sys_lock_tcpip_core() and sys_unlock_tcpip_core().
  354. * Let @ref LOCK_TCPIP_CORE() and @ref UNLOCK_TCPIP_CORE() point
  355. * to these functions.
  356. */
  357. /**
  358. * @page optimization Optimization hints
  359. The first thing you want to optimize is the lwip_standard_checksum()
  360. routine from src/core/inet.c. You can override this standard
  361. function with the \#define LWIP_CHKSUM your_checksum_routine().
  362. There are C examples given in inet.c or you might want to
  363. craft an assembly function for this. RFC1071 is a good
  364. introduction to this subject.
  365. Other significant improvements can be made by supplying
  366. assembly or inline replacements for htons() and htonl()
  367. if you're using a little-endian architecture.
  368. \#define lwip_htons(x) your_htons()
  369. \#define lwip_htonl(x) your_htonl()
  370. If you \#define them to htons() and htonl(), you should
  371. \#define LWIP_DONT_PROVIDE_BYTEORDER_FUNCTIONS to prevent lwIP from
  372. defining htonx / ntohx compatibility macros.
  373. Check your network interface driver if it reads at
  374. a higher speed than the maximum wire-speed. If the
  375. hardware isn't serviced frequently and fast enough
  376. buffer overflows are likely to occur.
  377. E.g. when using the cs8900 driver, call cs8900if_service(ethif)
  378. as frequently as possible. When using an RTOS let the cs8900 interrupt
  379. wake a high priority task that services your driver using a binary
  380. semaphore or event flag. Some drivers might allow additional tuning
  381. to match your application and network.
  382. For a production release it is recommended to set LWIP_STATS to 0.
  383. Note that speed performance isn't influenced much by simply setting
  384. high values to the memory options.
  385. */