FreeRTOS
[armadillo_firmware.git] / FreeRTOS / Common / ethernet / lwIP_132 / doc / rawapi.txt
blob8eec6e786c731851e195f87715ea1aa7800542d4
1 Raw TCP/IP interface for lwIP
3 Authors: Adam Dunkels, Leon Woestenberg, Christiaan Simons
5 lwIP provides three Application Program's Interfaces (APIs) for programs
6 to use for communication with the TCP/IP code:
7 * low-level "core" / "callback" or "raw" API.
8 * higher-level "sequential" API.
9 * BSD-style socket API.
11 The sequential API provides a way for ordinary, sequential, programs
12 to use the lwIP stack. It is quite similar to the BSD socket API. The
13 model of execution is based on the blocking open-read-write-close
14 paradigm. Since the TCP/IP stack is event based by nature, the TCP/IP
15 code and the application program must reside in different execution
16 contexts (threads).
18 The socket API is a compatibility API for existing applications,
19 currently it is built on top of the sequential API. It is meant to
20 provide all functions needed to run socket API applications running
21 on other platforms (e.g. unix / windows etc.). However, due to limitations
22 in the specification of this API, there might be incompatibilities
23 that require small modifications of existing programs.
25 ** Threading
27 lwIP started targeting single-threaded environments. When adding multi-
28 threading support, instead of making the core thread-safe, another
29 approach was chosen: there is one main thread running the lwIP core
30 (also known as the "tcpip_thread"). The raw API may only be used from
31 this thread! Application threads using the sequential- or socket API
32 communicate with this main thread through message passing.
34       As such, the list of functions that may be called from
35       other threads or an ISR is very limited! Only functions
36       from these API header files are thread-safe:
37       - api.h
38       - netbuf.h
39       - netdb.h
40       - netifapi.h
41       - sockets.h
42       - sys.h
44       Additionaly, memory (de-)allocation functions may be
45       called from multiple threads (not ISR!) with NO_SYS=0
46       since they are protected by SYS_LIGHTWEIGHT_PROT and/or
47       semaphores.
49       Only since 1.3.0, if SYS_LIGHTWEIGHT_PROT is set to 1
50       and LWIP_ALLOW_MEM_FREE_FROM_OTHER_CONTEXT is set to 1,
51       pbuf_free() may also be called from another thread or
52       an ISR (since only then, mem_free - for PBUF_RAM - may
53       be called from an ISR: otherwise, the HEAP is only
54       protected by semaphores).
55       
57 ** The remainder of this document discusses the "raw" API. **
59 The raw TCP/IP interface allows the application program to integrate
60 better with the TCP/IP code. Program execution is event based by
61 having callback functions being called from within the TCP/IP
62 code. The TCP/IP code and the application program both run in the same
63 thread. The sequential API has a much higher overhead and is not very
64 well suited for small systems since it forces a multithreaded paradigm
65 on the application.
67 The raw TCP/IP interface is not only faster in terms of code execution
68 time but is also less memory intensive. The drawback is that program
69 development is somewhat harder and application programs written for
70 the raw TCP/IP interface are more difficult to understand. Still, this
71 is the preferred way of writing applications that should be small in
72 code size and memory usage.
74 Both APIs can be used simultaneously by different application
75 programs. In fact, the sequential API is implemented as an application
76 program using the raw TCP/IP interface.
78 --- Callbacks
80 Program execution is driven by callbacks. Each callback is an ordinary
81 C function that is called from within the TCP/IP code. Every callback
82 function is passed the current TCP or UDP connection state as an
83 argument. Also, in order to be able to keep program specific state,
84 the callback functions are called with a program specified argument
85 that is independent of the TCP/IP state.
87 The function for setting the application connection state is:
89 - void tcp_arg(struct tcp_pcb *pcb, void *arg)
91   Specifies the program specific state that should be passed to all
92   other callback functions. The "pcb" argument is the current TCP
93   connection control block, and the "arg" argument is the argument
94   that will be passed to the callbacks.
96   
97 --- TCP connection setup
99 The functions used for setting up connections is similar to that of
100 the sequential API and of the BSD socket API. A new TCP connection
101 identifier (i.e., a protocol control block - PCB) is created with the
102 tcp_new() function. This PCB can then be either set to listen for new
103 incoming connections or be explicitly connected to another host.
105 - struct tcp_pcb *tcp_new(void)
107   Creates a new connection identifier (PCB). If memory is not
108   available for creating the new pcb, NULL is returned.
110 - err_t tcp_bind(struct tcp_pcb *pcb, struct ip_addr *ipaddr,
111                  u16_t port)
113   Binds the pcb to a local IP address and port number. The IP address
114   can be specified as IP_ADDR_ANY in order to bind the connection to
115   all local IP addresses.
117   If another connection is bound to the same port, the function will
118   return ERR_USE, otherwise ERR_OK is returned.
120 - struct tcp_pcb *tcp_listen(struct tcp_pcb *pcb)
122   Commands a pcb to start listening for incoming connections. When an
123   incoming connection is accepted, the function specified with the
124   tcp_accept() function will be called. The pcb will have to be bound
125   to a local port with the tcp_bind() function.
127   The tcp_listen() function returns a new connection identifier, and
128   the one passed as an argument to the function will be
129   deallocated. The reason for this behavior is that less memory is
130   needed for a connection that is listening, so tcp_listen() will
131   reclaim the memory needed for the original connection and allocate a
132   new smaller memory block for the listening connection.
134   tcp_listen() may return NULL if no memory was available for the
135   listening connection. If so, the memory associated with the pcb
136   passed as an argument to tcp_listen() will not be deallocated.
138 - struct tcp_pcb *tcp_listen_with_backlog(struct tcp_pcb *pcb, u8_t backlog)
140   Same as tcp_listen, but limits the number of outstanding connections
141   in the listen queue to the value specified by the backlog argument.
142   To use it, your need to set TCP_LISTEN_BACKLOG=1 in your lwipopts.h.
144 - void tcp_accepted(struct tcp_pcb *pcb)
146   Inform lwIP that an incoming connection has been accepted. This would
147   usually be called from the accept callback. This allows lwIP to perform
148   housekeeping tasks, such as allowing further incoming connections to be
149   queued in the listen backlog.
151 - void tcp_accept(struct tcp_pcb *pcb,
152                   err_t (* accept)(void *arg, struct tcp_pcb *newpcb,
153                                    err_t err))
155   Specified the callback function that should be called when a new
156   connection arrives on a listening connection.
157       
158 - err_t tcp_connect(struct tcp_pcb *pcb, struct ip_addr *ipaddr,
159                     u16_t port, err_t (* connected)(void *arg,
160                                                     struct tcp_pcb *tpcb,
161                                                     err_t err));
163   Sets up the pcb to connect to the remote host and sends the
164   initial SYN segment which opens the connection. 
166   The tcp_connect() function returns immediately; it does not wait for
167   the connection to be properly setup. Instead, it will call the
168   function specified as the fourth argument (the "connected" argument)
169   when the connection is established. If the connection could not be
170   properly established, either because the other host refused the
171   connection or because the other host didn't answer, the "err"
172   callback function of this pcb (registered with tcp_err, see below)
173   will be called.
175   The tcp_connect() function can return ERR_MEM if no memory is
176   available for enqueueing the SYN segment. If the SYN indeed was
177   enqueued successfully, the tcp_connect() function returns ERR_OK.
179   
180 --- Sending TCP data
182 TCP data is sent by enqueueing the data with a call to
183 tcp_write(). When the data is successfully transmitted to the remote
184 host, the application will be notified with a call to a specified
185 callback function.
187 - err_t tcp_write(struct tcp_pcb *pcb, void *dataptr, u16_t len,
188                   u8_t copy)
190   Enqueues the data pointed to by the argument dataptr. The length of
191   the data is passed as the len parameter. The copy argument is either
192   0 or 1 and indicates whether the new memory should be allocated for
193   the data to be copied into. If the argument is 0, no new memory
194   should be allocated and the data should only be referenced by
195   pointer.
197   The tcp_write() function will fail and return ERR_MEM if the length
198   of the data exceeds the current send buffer size or if the length of
199   the queue of outgoing segment is larger than the upper limit defined
200   in lwipopts.h. The number of bytes available in the output queue can
201   be retrieved with the tcp_sndbuf() function.
203   The proper way to use this function is to call the function with at
204   most tcp_sndbuf() bytes of data. If the function returns ERR_MEM,
205   the application should wait until some of the currently enqueued
206   data has been successfully received by the other host and try again.
208 - void tcp_sent(struct tcp_pcb *pcb,
209                 err_t (* sent)(void *arg, struct tcp_pcb *tpcb,
210                 u16_t len))
212   Specifies the callback function that should be called when data has
213   successfully been received (i.e., acknowledged) by the remote
214   host. The len argument passed to the callback function gives the
215   amount bytes that was acknowledged by the last acknowledgment.
217   
218 --- Receiving TCP data
220 TCP data reception is callback based - an application specified
221 callback function is called when new data arrives. When the
222 application has taken the data, it has to call the tcp_recved()
223 function to indicate that TCP can advertise increase the receive
224 window.
226 - void tcp_recv(struct tcp_pcb *pcb,
227                 err_t (* recv)(void *arg, struct tcp_pcb *tpcb,
228                                struct pbuf *p, err_t err))
230   Sets the callback function that will be called when new data
231   arrives. The callback function will be passed a NULL pbuf to
232   indicate that the remote host has closed the connection. If
233   there are no errors and the callback function is to return
234   ERR_OK, then it must free the pbuf. Otherwise, it must not
235   free the pbuf so that lwIP core code can store it.
237 - void tcp_recved(struct tcp_pcb *pcb, u16_t len)
239   Must be called when the application has received the data. The len
240   argument indicates the length of the received data.
241     
243 --- Application polling
245 When a connection is idle (i.e., no data is either transmitted or
246 received), lwIP will repeatedly poll the application by calling a
247 specified callback function. This can be used either as a watchdog
248 timer for killing connections that have stayed idle for too long, or
249 as a method of waiting for memory to become available. For instance,
250 if a call to tcp_write() has failed because memory wasn't available,
251 the application may use the polling functionality to call tcp_write()
252 again when the connection has been idle for a while.
254 - void tcp_poll(struct tcp_pcb *pcb, u8_t interval,
255                 err_t (* poll)(void *arg, struct tcp_pcb *tpcb))
257   Specifies the polling interval and the callback function that should
258   be called to poll the application. The interval is specified in
259   number of TCP coarse grained timer shots, which typically occurs
260   twice a second. An interval of 10 means that the application would
261   be polled every 5 seconds.
264 --- Closing and aborting connections
266 - err_t tcp_close(struct tcp_pcb *pcb)
268   Closes the connection. The function may return ERR_MEM if no memory
269   was available for closing the connection. If so, the application
270   should wait and try again either by using the acknowledgment
271   callback or the polling functionality. If the close succeeds, the
272   function returns ERR_OK.
274   The pcb is deallocated by the TCP code after a call to tcp_close(). 
276 - void tcp_abort(struct tcp_pcb *pcb)
278   Aborts the connection by sending a RST (reset) segment to the remote
279   host. The pcb is deallocated. This function never fails.
281 If a connection is aborted because of an error, the application is
282 alerted of this event by the err callback. Errors that might abort a
283 connection are when there is a shortage of memory. The callback
284 function to be called is set using the tcp_err() function.
286 - void tcp_err(struct tcp_pcb *pcb, void (* err)(void *arg,
287        err_t err))
289   The error callback function does not get the pcb passed to it as a
290   parameter since the pcb may already have been deallocated.
293 --- Lower layer TCP interface
295 TCP provides a simple interface to the lower layers of the
296 system. During system initialization, the function tcp_init() has
297 to be called before any other TCP function is called. When the system
298 is running, the two timer functions tcp_fasttmr() and tcp_slowtmr()
299 must be called with regular intervals. The tcp_fasttmr() should be
300 called every TCP_FAST_INTERVAL milliseconds (defined in tcp.h) and
301 tcp_slowtmr() should be called every TCP_SLOW_INTERVAL milliseconds. 
304 --- UDP interface
306 The UDP interface is similar to that of TCP, but due to the lower
307 level of complexity of UDP, the interface is significantly simpler.
309 - struct udp_pcb *udp_new(void)
311   Creates a new UDP pcb which can be used for UDP communication. The
312   pcb is not active until it has either been bound to a local address
313   or connected to a remote address.
315 - void udp_remove(struct udp_pcb *pcb)
317   Removes and deallocates the pcb.  
318   
319 - err_t udp_bind(struct udp_pcb *pcb, struct ip_addr *ipaddr,
320                  u16_t port)
322   Binds the pcb to a local address. The IP-address argument "ipaddr"
323   can be IP_ADDR_ANY to indicate that it should listen to any local IP
324   address. The function currently always return ERR_OK.
326 - err_t udp_connect(struct udp_pcb *pcb, struct ip_addr *ipaddr,
327                     u16_t port)
329   Sets the remote end of the pcb. This function does not generate any
330   network traffic, but only set the remote address of the pcb.
332 - err_t udp_disconnect(struct udp_pcb *pcb)
334   Remove the remote end of the pcb. This function does not generate
335   any network traffic, but only removes the remote address of the pcb.
337 - err_t udp_send(struct udp_pcb *pcb, struct pbuf *p)
339   Sends the pbuf p. The pbuf is not deallocated.
341 - void udp_recv(struct udp_pcb *pcb,
342                 void (* recv)(void *arg, struct udp_pcb *upcb,
343                                          struct pbuf *p,
344                                          struct ip_addr *addr,
345                                          u16_t port),
346                               void *recv_arg)
348   Specifies a callback function that should be called when a UDP
349   datagram is received.
350   
352 --- System initalization
354 A truly complete and generic sequence for initializing the lwip stack
355 cannot be given because it depends on the build configuration (lwipopts.h)
356 and additional initializations for your runtime environment (e.g. timers).
358 We can give you some idea on how to proceed when using the raw API.
359 We assume a configuration using a single Ethernet netif and the
360 UDP and TCP transport layers, IPv4 and the DHCP client.
362 Call these functions in the order of appearance:
364 - stats_init()
366   Clears the structure where runtime statistics are gathered.
368 - sys_init()
369   
370   Not of much use since we set the NO_SYS 1 option in lwipopts.h,
371   to be called for easy configuration changes.
373 - mem_init()
375   Initializes the dynamic memory heap defined by MEM_SIZE.
377 - memp_init()
379   Initializes the memory pools defined by MEMP_NUM_x.
381 - pbuf_init()
383   Initializes the pbuf memory pool defined by PBUF_POOL_SIZE.
384   
385 - etharp_init()
387   Initializes the ARP table and queue.
388   Note: you must call etharp_tmr at a ARP_TMR_INTERVAL (5 seconds) regular interval
389   after this initialization.
391 - ip_init()
393   Doesn't do much, it should be called to handle future changes.
395 - udp_init()
397   Clears the UDP PCB list.
399 - tcp_init()
401   Clears the TCP PCB list and clears some internal TCP timers.
402   Note: you must call tcp_fasttmr() and tcp_slowtmr() at the
403   predefined regular intervals after this initialization. 
404   
405 - netif_add(struct netif *netif, struct ip_addr *ipaddr,
406             struct ip_addr *netmask, struct ip_addr *gw,
407             void *state, err_t (* init)(struct netif *netif),
408             err_t (* input)(struct pbuf *p, struct netif *netif))
410   Adds your network interface to the netif_list. Allocate a struct
411   netif and pass a pointer to this structure as the first argument.
412   Give pointers to cleared ip_addr structures when using DHCP,
413   or fill them with sane numbers otherwise. The state pointer may be NULL.
415   The init function pointer must point to a initialization function for
416   your ethernet netif interface. The following code illustrates it's use.
417   
418   err_t netif_if_init(struct netif *netif)
419   {
420     u8_t i;
421     
422     for(i = 0; i < ETHARP_HWADDR_LEN; i++) netif->hwaddr[i] = some_eth_addr[i];
423     init_my_eth_device();
424     return ERR_OK;
425   }
426   
427   For ethernet drivers, the input function pointer must point to the lwip
428   function ethernet_input() declared in "netif/etharp.h". Other drivers
429   must use ip_input() declared in "lwip/ip.h".
430   
431 - netif_set_default(struct netif *netif)
433   Registers the default network interface.
435 - netif_set_up(struct netif *netif)
437   When the netif is fully configured this function must be called.
439 - dhcp_start(struct netif *netif)
441   Creates a new DHCP client for this interface on the first call.
442   Note: you must call dhcp_fine_tmr() and dhcp_coarse_tmr() at
443   the predefined regular intervals after starting the client.
444   
445   You can peek in the netif->dhcp struct for the actual DHCP status.
448 --- Optimalization hints
450 The first thing you want to optimize is the lwip_standard_checksum()
451 routine from src/core/inet.c. You can override this standard
452 function with the #define LWIP_CHKSUM <your_checksum_routine>.
454 There are C examples given in inet.c or you might want to
455 craft an assembly function for this. RFC1071 is a good
456 introduction to this subject.
458 Other significant improvements can be made by supplying
459 assembly or inline replacements for htons() and htonl()
460 if you're using a little-endian architecture.
461 #define LWIP_PLATFORM_BYTESWAP 1
462 #define LWIP_PLATFORM_HTONS(x) <your_htons>
463 #define LWIP_PLATFORM_HTONL(x) <your_htonl>
465 Check your network interface driver if it reads at
466 a higher speed than the maximum wire-speed. If the
467 hardware isn't serviced frequently and fast enough
468 buffer overflows are likely to occur.
470 E.g. when using the cs8900 driver, call cs8900if_service(ethif)
471 as frequently as possible. When using an RTOS let the cs8900 interrupt
472 wake a high priority task that services your driver using a binary
473 semaphore or event flag. Some drivers might allow additional tuning
474 to match your application and network.
476 For a production release it is recommended to set LWIP_STATS to 0.
477 Note that speed performance isn't influenced much by simply setting
478 high values to the memory options.