FreeRTOS
[armadillo_firmware.git] / FreeRTOS / Common / ethernet / lwIP_130 / doc / rawapi.txt
blobd511280f8918b4ddfb733ca3951bbc5339378898
1 Raw TCP/IP interface for lwIP\r
2 \r
3 Authors: Adam Dunkels, Leon Woestenberg, Christiaan Simons\r
4 \r
5 lwIP provides two Application Program's Interfaces (APIs) for programs\r
6 to use for communication with the TCP/IP code:\r
7 * low-level "core" / "callback" or "raw" API.\r
8 * higher-level "sequential" API.\r
9 \r
10 The sequential API provides a way for ordinary, sequential, programs\r
11 to use the lwIP stack. It is quite similar to the BSD socket API. The\r
12 model of execution is based on the blocking open-read-write-close\r
13 paradigm. Since the TCP/IP stack is event based by nature, the TCP/IP\r
14 code and the application program must reside in different execution\r
15 contexts (threads).\r
17 ** The remainder of this document discusses the "raw" API. **\r
19 The raw TCP/IP interface allows the application program to integrate\r
20 better with the TCP/IP code. Program execution is event based by\r
21 having callback functions being called from within the TCP/IP\r
22 code. The TCP/IP code and the application program both run in the same\r
23 thread. The sequential API has a much higher overhead and is not very\r
24 well suited for small systems since it forces a multithreaded paradigm\r
25 on the application.\r
27 The raw TCP/IP interface is not only faster in terms of code execution\r
28 time but is also less memory intensive. The drawback is that program\r
29 development is somewhat harder and application programs written for\r
30 the raw TCP/IP interface are more difficult to understand. Still, this\r
31 is the preferred way of writing applications that should be small in\r
32 code size and memory usage.\r
34 Both APIs can be used simultaneously by different application\r
35 programs. In fact, the sequential API is implemented as an application\r
36 program using the raw TCP/IP interface.\r
38 --- Callbacks\r
40 Program execution is driven by callbacks. Each callback is an ordinary\r
41 C function that is called from within the TCP/IP code. Every callback\r
42 function is passed the current TCP or UDP connection state as an\r
43 argument. Also, in order to be able to keep program specific state,\r
44 the callback functions are called with a program specified argument\r
45 that is independent of the TCP/IP state.\r
47 The function for setting the application connection state is:\r
49 - void tcp_arg(struct tcp_pcb *pcb, void *arg)\r
51   Specifies the program specific state that should be passed to all\r
52   other callback functions. The "pcb" argument is the current TCP\r
53   connection control block, and the "arg" argument is the argument\r
54   that will be passed to the callbacks.\r
56   \r
57 --- TCP connection setup\r
59 The functions used for setting up connections is similar to that of\r
60 the sequential API and of the BSD socket API. A new TCP connection\r
61 identifier (i.e., a protocol control block - PCB) is created with the\r
62 tcp_new() function. This PCB can then be either set to listen for new\r
63 incoming connections or be explicitly connected to another host.\r
65 - struct tcp_pcb *tcp_new(void)\r
67   Creates a new connection identifier (PCB). If memory is not\r
68   available for creating the new pcb, NULL is returned.\r
70 - err_t tcp_bind(struct tcp_pcb *pcb, struct ip_addr *ipaddr,\r
71                  u16_t port)\r
73   Binds the pcb to a local IP address and port number. The IP address\r
74   can be specified as IP_ADDR_ANY in order to bind the connection to\r
75   all local IP addresses.\r
77   If another connection is bound to the same port, the function will\r
78   return ERR_USE, otherwise ERR_OK is returned.\r
80 - struct tcp_pcb *tcp_listen(struct tcp_pcb *pcb)\r
82   Commands a pcb to start listening for incoming connections. When an\r
83   incoming connection is accepted, the function specified with the\r
84   tcp_accept() function will be called. The pcb will have to be bound\r
85   to a local port with the tcp_bind() function.\r
87   The tcp_listen() function returns a new connection identifier, and\r
88   the one passed as an argument to the function will be\r
89   deallocated. The reason for this behavior is that less memory is\r
90   needed for a connection that is listening, so tcp_listen() will\r
91   reclaim the memory needed for the original connection and allocate a\r
92   new smaller memory block for the listening connection.\r
94   tcp_listen() may return NULL if no memory was available for the\r
95   listening connection. If so, the memory associated with the pcb\r
96   passed as an argument to tcp_listen() will not be deallocated.\r
98 - struct tcp_pcb *tcp_listen_with_backlog(struct tcp_pcb *pcb, u8_t backlog)\r
100   Same as tcp_listen, but limits the number of outstanding connections\r
101   in the listen queue to the value specified by the backlog argument.\r
102   To use it, your need to set TCP_LISTEN_BACKLOG=1 in your lwipopts.h.\r
104 - void tcp_accepted(struct tcp_pcb *pcb)\r
106   Inform lwIP that an incoming connection has been accepted. This would\r
107   usually be called from the accept callback. This allows lwIP to perform\r
108   housekeeping tasks, such as allowing further incoming connections to be\r
109   queued in the listen backlog.\r
111 - void tcp_accept(struct tcp_pcb *pcb,\r
112                   err_t (* accept)(void *arg, struct tcp_pcb *newpcb,\r
113                                    err_t err))\r
115   Specified the callback function that should be called when a new\r
116   connection arrives on a listening connection.\r
117       \r
118 - err_t tcp_connect(struct tcp_pcb *pcb, struct ip_addr *ipaddr,\r
119                     u16_t port, err_t (* connected)(void *arg,\r
120                                                     struct tcp_pcb *tpcb,\r
121                                                     err_t err));\r
123   Sets up the pcb to connect to the remote host and sends the\r
124   initial SYN segment which opens the connection. \r
126   The tcp_connect() function returns immediately; it does not wait for\r
127   the connection to be properly setup. Instead, it will call the\r
128   function specified as the fourth argument (the "connected" argument)\r
129   when the connection is established. If the connection could not be\r
130   properly established, either because the other host refused the\r
131   connection or because the other host didn't answer, the "connected"\r
132   function will be called with an the "err" argument set accordingly.\r
134   The tcp_connect() function can return ERR_MEM if no memory is\r
135   available for enqueueing the SYN segment. If the SYN indeed was\r
136   enqueued successfully, the tcp_connect() function returns ERR_OK.\r
138   \r
139 --- Sending TCP data\r
141 TCP data is sent by enqueueing the data with a call to\r
142 tcp_write(). When the data is successfully transmitted to the remote\r
143 host, the application will be notified with a call to a specified\r
144 callback function.\r
146 - err_t tcp_write(struct tcp_pcb *pcb, void *dataptr, u16_t len,\r
147                   u8_t copy)\r
149   Enqueues the data pointed to by the argument dataptr. The length of\r
150   the data is passed as the len parameter. The copy argument is either\r
151   0 or 1 and indicates whether the new memory should be allocated for\r
152   the data to be copied into. If the argument is 0, no new memory\r
153   should be allocated and the data should only be referenced by\r
154   pointer.\r
156   The tcp_write() function will fail and return ERR_MEM if the length\r
157   of the data exceeds the current send buffer size or if the length of\r
158   the queue of outgoing segment is larger than the upper limit defined\r
159   in lwipopts.h. The number of bytes available in the output queue can\r
160   be retrieved with the tcp_sndbuf() function.\r
162   The proper way to use this function is to call the function with at\r
163   most tcp_sndbuf() bytes of data. If the function returns ERR_MEM,\r
164   the application should wait until some of the currently enqueued\r
165   data has been successfully received by the other host and try again.\r
167 - void tcp_sent(struct tcp_pcb *pcb,\r
168                 err_t (* sent)(void *arg, struct tcp_pcb *tpcb,\r
169                 u16_t len))\r
171   Specifies the callback function that should be called when data has\r
172   successfully been received (i.e., acknowledged) by the remote\r
173   host. The len argument passed to the callback function gives the\r
174   amount bytes that was acknowledged by the last acknowledgment.\r
176   \r
177 --- Receiving TCP data\r
179 TCP data reception is callback based - an application specified\r
180 callback function is called when new data arrives. When the\r
181 application has taken the data, it has to call the tcp_recved()\r
182 function to indicate that TCP can advertise increase the receive\r
183 window.\r
185 - void tcp_recv(struct tcp_pcb *pcb,\r
186                 err_t (* recv)(void *arg, struct tcp_pcb *tpcb,\r
187                                struct pbuf *p, err_t err))\r
189   Sets the callback function that will be called when new data\r
190   arrives. The callback function will be passed a NULL pbuf to\r
191   indicate that the remote host has closed the connection. If\r
192   there are no errors and the callback function is to return\r
193   ERR_OK, then it must free the pbuf. Otherwise, it must not\r
194   free the pbuf so that lwIP core code can store it.\r
196 - void tcp_recved(struct tcp_pcb *pcb, u16_t len)\r
198   Must be called when the application has received the data. The len\r
199   argument indicates the length of the received data.\r
200     \r
202 --- Application polling\r
204 When a connection is idle (i.e., no data is either transmitted or\r
205 received), lwIP will repeatedly poll the application by calling a\r
206 specified callback function. This can be used either as a watchdog\r
207 timer for killing connections that have stayed idle for too long, or\r
208 as a method of waiting for memory to become available. For instance,\r
209 if a call to tcp_write() has failed because memory wasn't available,\r
210 the application may use the polling functionality to call tcp_write()\r
211 again when the connection has been idle for a while.\r
213 - void tcp_poll(struct tcp_pcb *pcb, u8_t interval,\r
214                 err_t (* poll)(void *arg, struct tcp_pcb *tpcb))\r
216   Specifies the polling interval and the callback function that should\r
217   be called to poll the application. The interval is specified in\r
218   number of TCP coarse grained timer shots, which typically occurs\r
219   twice a second. An interval of 10 means that the application would\r
220   be polled every 5 seconds.\r
223 --- Closing and aborting connections\r
225 - err_t tcp_close(struct tcp_pcb *pcb)\r
227   Closes the connection. The function may return ERR_MEM if no memory\r
228   was available for closing the connection. If so, the application\r
229   should wait and try again either by using the acknowledgment\r
230   callback or the polling functionality. If the close succeeds, the\r
231   function returns ERR_OK.\r
233   The pcb is deallocated by the TCP code after a call to tcp_close(). \r
235 - void tcp_abort(struct tcp_pcb *pcb)\r
237   Aborts the connection by sending a RST (reset) segment to the remote\r
238   host. The pcb is deallocated. This function never fails.\r
240 If a connection is aborted because of an error, the application is\r
241 alerted of this event by the err callback. Errors that might abort a\r
242 connection are when there is a shortage of memory. The callback\r
243 function to be called is set using the tcp_err() function.\r
245 - void tcp_err(struct tcp_pcb *pcb, void (* err)(void *arg,\r
246        err_t err))\r
248   The error callback function does not get the pcb passed to it as a\r
249   parameter since the pcb may already have been deallocated.\r
252 --- Lower layer TCP interface\r
254 TCP provides a simple interface to the lower layers of the\r
255 system. During system initialization, the function tcp_init() has\r
256 to be called before any other TCP function is called. When the system\r
257 is running, the two timer functions tcp_fasttmr() and tcp_slowtmr()\r
258 must be called with regular intervals. The tcp_fasttmr() should be\r
259 called every TCP_FAST_INTERVAL milliseconds (defined in tcp.h) and\r
260 tcp_slowtmr() should be called every TCP_SLOW_INTERVAL milliseconds. \r
263 --- UDP interface\r
265 The UDP interface is similar to that of TCP, but due to the lower\r
266 level of complexity of UDP, the interface is significantly simpler.\r
268 - struct udp_pcb *udp_new(void)\r
270   Creates a new UDP pcb which can be used for UDP communication. The\r
271   pcb is not active until it has either been bound to a local address\r
272   or connected to a remote address.\r
274 - void udp_remove(struct udp_pcb *pcb)\r
276   Removes and deallocates the pcb.  \r
277   \r
278 - err_t udp_bind(struct udp_pcb *pcb, struct ip_addr *ipaddr,\r
279                  u16_t port)\r
281   Binds the pcb to a local address. The IP-address argument "ipaddr"\r
282   can be IP_ADDR_ANY to indicate that it should listen to any local IP\r
283   address. The function currently always return ERR_OK.\r
285 - err_t udp_connect(struct udp_pcb *pcb, struct ip_addr *ipaddr,\r
286                     u16_t port)\r
288   Sets the remote end of the pcb. This function does not generate any\r
289   network traffic, but only set the remote address of the pcb.\r
291 - err_t udp_disconnect(struct udp_pcb *pcb)\r
293   Remove the remote end of the pcb. This function does not generate\r
294   any network traffic, but only removes the remote address of the pcb.\r
296 - err_t udp_send(struct udp_pcb *pcb, struct pbuf *p)\r
298   Sends the pbuf p. The pbuf is not deallocated.\r
300 - void udp_recv(struct udp_pcb *pcb,\r
301                 void (* recv)(void *arg, struct udp_pcb *upcb,\r
302                                          struct pbuf *p,\r
303                                          struct ip_addr *addr,\r
304                                          u16_t port),\r
305                               void *recv_arg)\r
307   Specifies a callback function that should be called when a UDP\r
308   datagram is received.\r
309   \r
311 --- System initalization\r
313 A truly complete and generic sequence for initializing the lwip stack\r
314 cannot be given because it depends on the build configuration (lwipopts.h)\r
315 and additional initializations for your runtime environment (e.g. timers).\r
317 We can give you some idea on how to proceed when using the raw API.\r
318 We assume a configuration using a single Ethernet netif and the\r
319 UDP and TCP transport layers, IPv4 and the DHCP client.\r
321 Call these functions in the order of appearance:\r
323 - stats_init()\r
325   Clears the structure where runtime statistics are gathered.\r
327 - sys_init()\r
328   \r
329   Not of much use since we set the NO_SYS 1 option in lwipopts.h,\r
330   to be called for easy configuration changes.\r
332 - mem_init()\r
334   Initializes the dynamic memory heap defined by MEM_SIZE.\r
336 - memp_init()\r
338   Initializes the memory pools defined by MEMP_NUM_x.\r
340 - pbuf_init()\r
342   Initializes the pbuf memory pool defined by PBUF_POOL_SIZE.\r
343   \r
344 - etharp_init()\r
346   Initializes the ARP table and queue.\r
347   Note: you must call etharp_tmr at a ARP_TMR_INTERVAL (5 seconds) regular interval\r
348   after this initialization.\r
350 - ip_init()\r
352   Doesn't do much, it should be called to handle future changes.\r
354 - udp_init()\r
356   Clears the UDP PCB list.\r
358 - tcp_init()\r
360   Clears the TCP PCB list and clears some internal TCP timers.\r
361   Note: you must call tcp_fasttmr() and tcp_slowtmr() at the\r
362   predefined regular intervals after this initialization. \r
363   \r
364 - netif_add(struct netif *netif, struct ip_addr *ipaddr,\r
365             struct ip_addr *netmask, struct ip_addr *gw,\r
366             void *state, err_t (* init)(struct netif *netif),\r
367             err_t (* input)(struct pbuf *p, struct netif *netif))\r
369   Adds your network interface to the netif_list. Allocate a struct\r
370   netif and pass a pointer to this structure as the first argument.\r
371   Give pointers to cleared ip_addr structures when using DHCP,\r
372   or fill them with sane numbers otherwise. The state pointer may be NULL.\r
374   The init function pointer must point to a initialization function for\r
375   your ethernet netif interface. The following code illustrates it's use.\r
376   \r
377   err_t netif_if_init(struct netif *netif)\r
378   {\r
379     u8_t i;\r
380     \r
381     for(i = 0; i < ETHARP_HWADDR_LEN; i++) netif->hwaddr[i] = some_eth_addr[i];\r
382     init_my_eth_device();\r
383     return ERR_OK;\r
384   }\r
385   \r
386   For ethernet drivers, the input function pointer must point to the lwip\r
387   function ethernet_input() declared in "netif/etharp.h". Other drivers\r
388   must use ip_input() declared in "lwip/ip.h".\r
389   \r
390 - netif_set_default(struct netif *netif)\r
392   Registers the default network interface.\r
394 - netif_set_up(struct netif *netif)\r
396   When the netif is fully configured this function must be called.\r
398 - dhcp_start(struct netif *netif)\r
400   Creates a new DHCP client for this interface on the first call.\r
401   Note: you must call dhcp_fine_tmr() and dhcp_coarse_tmr() at\r
402   the predefined regular intervals after starting the client.\r
403   \r
404   You can peek in the netif->dhcp struct for the actual DHCP status.\r
407 --- Optimalization hints\r
409 The first thing you want to optimize is the lwip_standard_checksum()\r
410 routine from src/core/inet.c. You can override this standard\r
411 function with the #define LWIP_CHKSUM <your_checksum_routine>.\r
413 There are C examples given in inet.c or you might want to\r
414 craft an assembly function for this. RFC1071 is a good\r
415 introduction to this subject.\r
417 Other significant improvements can be made by supplying\r
418 assembly or inline replacements for htons() and htonl()\r
419 if you're using a little-endian architecture.\r
420 #define LWIP_PLATFORM_BYTESWAP 1\r
421 #define LWIP_PLATFORM_HTONS(x) <your_htons>\r
422 #define LWIP_PLATFORM_HTONL(x) <your_htonl>\r
424 Check your network interface driver if it reads at\r
425 a higher speed than the maximum wire-speed. If the\r
426 hardware isn't serviced frequently and fast enough\r
427 buffer overflows are likely to occur.\r
429 E.g. when using the cs8900 driver, call cs8900if_service(ethif)\r
430 as frequently as possible. When using an RTOS let the cs8900 interrupt\r
431 wake a high priority task that services your driver using a binary\r
432 semaphore or event flag. Some drivers might allow additional tuning\r
433 to match your application and network.\r
435 For a production release it is recommended to set LWIP_STATS to 0.\r
436 Note that speed performance isn't influenced much by simply setting\r
437 high values to the memory options.\r