]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/ankh/lib/lwip/lib/contrib/src/api/tcpip.c
update
[l4.git] / l4 / pkg / ankh / lib / lwip / lib / contrib / src / api / tcpip.c
1 /**
2  * @file
3  * Sequential API Main thread module
4  *
5  */
6
7 /*
8  * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without modification,
12  * are permitted provided that the following conditions are met:
13  *
14  * 1. Redistributions of source code must retain the above copyright notice,
15  *    this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright notice,
17  *    this list of conditions and the following disclaimer in the documentation
18  *    and/or other materials provided with the distribution.
19  * 3. The name of the author may not be used to endorse or promote products
20  *    derived from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
23  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
24  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
25  * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
26  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
27  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
30  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
31  * OF SUCH DAMAGE.
32  *
33  * This file is part of the lwIP TCP/IP stack.
34  *
35  * Author: Adam Dunkels <adam@sics.se>
36  *
37  */
38
39 #include "lwip/opt.h"
40
41 #if !NO_SYS /* don't build if not configured for use in lwipopts.h */
42
43 #include "lwip/sys.h"
44 #include "lwip/memp.h"
45 #include "lwip/mem.h"
46 #include "lwip/pbuf.h"
47 #include "lwip/tcpip.h"
48 #include "lwip/init.h"
49 #include "netif/etharp.h"
50 #include "netif/ppp_oe.h"
51
52 /* global variables */
53 static tcpip_init_done_fn tcpip_init_done;
54 static void *tcpip_init_done_arg;
55 static sys_mbox_t mbox;
56
57 #if LWIP_TCPIP_CORE_LOCKING
58 /** The global semaphore to lock the stack. */
59 sys_mutex_t lock_tcpip_core;
60 #endif /* LWIP_TCPIP_CORE_LOCKING */
61
62
63 /**
64  * The main lwIP thread. This thread has exclusive access to lwIP core functions
65  * (unless access to them is not locked). Other threads communicate with this
66  * thread using message boxes.
67  *
68  * It also starts all the timers to make sure they are running in the right
69  * thread context.
70  *
71  * @param arg unused argument
72  */
73 static void
74 tcpip_thread(void *arg)
75 {
76   struct tcpip_msg *msg;
77   LWIP_UNUSED_ARG(arg);
78
79   if (tcpip_init_done != NULL) {
80     tcpip_init_done(tcpip_init_done_arg);
81   }
82
83   LOCK_TCPIP_CORE();
84   while (1) {                          /* MAIN Loop */
85     UNLOCK_TCPIP_CORE();
86     LWIP_TCPIP_THREAD_ALIVE();
87     /* wait for a message, timeouts are processed while waiting */
88     sys_timeouts_mbox_fetch(&mbox, (void **)&msg);
89     LOCK_TCPIP_CORE();
90     switch (msg->type) {
91 #if LWIP_NETCONN
92     case TCPIP_MSG_API:
93       LWIP_DEBUGF(TCPIP_DEBUG, ("tcpip_thread: API message %p\n", (void *)msg));
94       msg->msg.apimsg->function(&(msg->msg.apimsg->msg));
95       break;
96 #endif /* LWIP_NETCONN */
97
98 #if !LWIP_TCPIP_CORE_LOCKING_INPUT
99     case TCPIP_MSG_INPKT:
100       LWIP_DEBUGF(TCPIP_DEBUG, ("tcpip_thread: PACKET %p\n", (void *)msg));
101 #if LWIP_ETHERNET
102       if (msg->msg.inp.netif->flags & (NETIF_FLAG_ETHARP | NETIF_FLAG_ETHERNET)) {
103         ethernet_input(msg->msg.inp.p, msg->msg.inp.netif);
104       } else
105 #endif /* LWIP_ETHERNET */
106 #if LWIP_IPV6
107       if ((*((unsigned char *)(msg->msg.inp.p->payload)) & 0xf0) == 0x60) {
108           ip6_input(msg->msg.inp.p, msg->msg.inp.netif);
109       } else
110 #endif /* LWIP_IPV6 */
111       {
112         ip_input(msg->msg.inp.p, msg->msg.inp.netif);
113       }
114       memp_free(MEMP_TCPIP_MSG_INPKT, msg);
115       break;
116 #endif /* LWIP_TCPIP_CORE_LOCKING_INPUT */
117
118 #if LWIP_NETIF_API
119     case TCPIP_MSG_NETIFAPI:
120       LWIP_DEBUGF(TCPIP_DEBUG, ("tcpip_thread: Netif API message %p\n", (void *)msg));
121       msg->msg.netifapimsg->function(&(msg->msg.netifapimsg->msg));
122       break;
123 #endif /* LWIP_NETIF_API */
124
125 #if LWIP_TCPIP_TIMEOUT
126     case TCPIP_MSG_TIMEOUT:
127       LWIP_DEBUGF(TCPIP_DEBUG, ("tcpip_thread: TIMEOUT %p\n", (void *)msg));
128       sys_timeout(msg->msg.tmo.msecs, msg->msg.tmo.h, msg->msg.tmo.arg);
129       memp_free(MEMP_TCPIP_MSG_API, msg);
130       break;
131     case TCPIP_MSG_UNTIMEOUT:
132       LWIP_DEBUGF(TCPIP_DEBUG, ("tcpip_thread: UNTIMEOUT %p\n", (void *)msg));
133       sys_untimeout(msg->msg.tmo.h, msg->msg.tmo.arg);
134       memp_free(MEMP_TCPIP_MSG_API, msg);
135       break;
136 #endif /* LWIP_TCPIP_TIMEOUT */
137
138     case TCPIP_MSG_CALLBACK:
139       LWIP_DEBUGF(TCPIP_DEBUG, ("tcpip_thread: CALLBACK %p\n", (void *)msg));
140       msg->msg.cb.function(msg->msg.cb.ctx);
141       memp_free(MEMP_TCPIP_MSG_API, msg);
142       break;
143
144     case TCPIP_MSG_CALLBACK_STATIC:
145       LWIP_DEBUGF(TCPIP_DEBUG, ("tcpip_thread: CALLBACK_STATIC %p\n", (void *)msg));
146       msg->msg.cb.function(msg->msg.cb.ctx);
147       break;
148
149     default:
150       LWIP_DEBUGF(TCPIP_DEBUG, ("tcpip_thread: invalid message: %d\n", msg->type));
151       LWIP_ASSERT("tcpip_thread: invalid message", 0);
152       break;
153     }
154   }
155 }
156
157 /**
158  * Pass a received packet to tcpip_thread for input processing
159  *
160  * @param p the received packet, p->payload pointing to the Ethernet header or
161  *          to an IP header (if inp doesn't have NETIF_FLAG_ETHARP or
162  *          NETIF_FLAG_ETHERNET flags)
163  * @param inp the network interface on which the packet was received
164  */
165 err_t
166 tcpip_input(struct pbuf *p, struct netif *inp)
167 {
168 #if LWIP_TCPIP_CORE_LOCKING_INPUT
169   err_t ret;
170   LWIP_DEBUGF(TCPIP_DEBUG, ("tcpip_input: PACKET %p/%p\n", (void *)p, (void *)inp));
171   LOCK_TCPIP_CORE();
172 #if LWIP_ETHERNET
173   if (inp->flags & (NETIF_FLAG_ETHARP | NETIF_FLAG_ETHERNET)) {
174     ret = ethernet_input(p, inp);
175   } else
176 #endif /* LWIP_ETHERNET */
177   {
178     ret = ip_input(p, inp);
179   }
180   UNLOCK_TCPIP_CORE();
181   return ret;
182 #else /* LWIP_TCPIP_CORE_LOCKING_INPUT */
183   struct tcpip_msg *msg;
184
185   if (!sys_mbox_valid(&mbox)) {
186     return ERR_VAL;
187   }
188   msg = (struct tcpip_msg *)memp_malloc(MEMP_TCPIP_MSG_INPKT);
189   if (msg == NULL) {
190     return ERR_MEM;
191   }
192
193   msg->type = TCPIP_MSG_INPKT;
194   msg->msg.inp.p = p;
195   msg->msg.inp.netif = inp;
196   if (sys_mbox_trypost(&mbox, msg) != ERR_OK) {
197     memp_free(MEMP_TCPIP_MSG_INPKT, msg);
198     return ERR_MEM;
199   }
200   return ERR_OK;
201 #endif /* LWIP_TCPIP_CORE_LOCKING_INPUT */
202 }
203
204 /**
205  * Call a specific function in the thread context of
206  * tcpip_thread for easy access synchronization.
207  * A function called in that way may access lwIP core code
208  * without fearing concurrent access.
209  *
210  * @param f the function to call
211  * @param ctx parameter passed to f
212  * @param block 1 to block until the request is posted, 0 to non-blocking mode
213  * @return ERR_OK if the function was called, another err_t if not
214  */
215 err_t
216 tcpip_callback_with_block(tcpip_callback_fn function, void *ctx, u8_t block)
217 {
218   struct tcpip_msg *msg;
219
220   if (sys_mbox_valid(&mbox)) {
221     msg = (struct tcpip_msg *)memp_malloc(MEMP_TCPIP_MSG_API);
222     if (msg == NULL) {
223       return ERR_MEM;
224     }
225
226     msg->type = TCPIP_MSG_CALLBACK;
227     msg->msg.cb.function = function;
228     msg->msg.cb.ctx = ctx;
229     if (block) {
230       sys_mbox_post(&mbox, msg);
231     } else {
232       if (sys_mbox_trypost(&mbox, msg) != ERR_OK) {
233         memp_free(MEMP_TCPIP_MSG_API, msg);
234         return ERR_MEM;
235       }
236     }
237     return ERR_OK;
238   }
239   return ERR_VAL;
240 }
241
242 #if LWIP_TCPIP_TIMEOUT
243 /**
244  * call sys_timeout in tcpip_thread
245  *
246  * @param msec time in milliseconds for timeout
247  * @param h function to be called on timeout
248  * @param arg argument to pass to timeout function h
249  * @return ERR_MEM on memory error, ERR_OK otherwise
250  */
251 err_t
252 tcpip_timeout(u32_t msecs, sys_timeout_handler h, void *arg)
253 {
254   struct tcpip_msg *msg;
255
256   if (sys_mbox_valid(&mbox)) {
257     msg = (struct tcpip_msg *)memp_malloc(MEMP_TCPIP_MSG_API);
258     if (msg == NULL) {
259       return ERR_MEM;
260     }
261
262     msg->type = TCPIP_MSG_TIMEOUT;
263     msg->msg.tmo.msecs = msecs;
264     msg->msg.tmo.h = h;
265     msg->msg.tmo.arg = arg;
266     sys_mbox_post(&mbox, msg);
267     return ERR_OK;
268   }
269   return ERR_VAL;
270 }
271
272 /**
273  * call sys_untimeout in tcpip_thread
274  *
275  * @param msec time in milliseconds for timeout
276  * @param h function to be called on timeout
277  * @param arg argument to pass to timeout function h
278  * @return ERR_MEM on memory error, ERR_OK otherwise
279  */
280 err_t
281 tcpip_untimeout(sys_timeout_handler h, void *arg)
282 {
283   struct tcpip_msg *msg;
284
285   if (sys_mbox_valid(&mbox)) {
286     msg = (struct tcpip_msg *)memp_malloc(MEMP_TCPIP_MSG_API);
287     if (msg == NULL) {
288       return ERR_MEM;
289     }
290
291     msg->type = TCPIP_MSG_UNTIMEOUT;
292     msg->msg.tmo.h = h;
293     msg->msg.tmo.arg = arg;
294     sys_mbox_post(&mbox, msg);
295     return ERR_OK;
296   }
297   return ERR_VAL;
298 }
299 #endif /* LWIP_TCPIP_TIMEOUT */
300
301 #if LWIP_NETCONN
302 /**
303  * Call the lower part of a netconn_* function
304  * This function is then running in the thread context
305  * of tcpip_thread and has exclusive access to lwIP core code.
306  *
307  * @param apimsg a struct containing the function to call and its parameters
308  * @return ERR_OK if the function was called, another err_t if not
309  */
310 err_t
311 tcpip_apimsg(struct api_msg *apimsg)
312 {
313   struct tcpip_msg msg;
314 #ifdef LWIP_DEBUG
315   /* catch functions that don't set err */
316   apimsg->msg.err = ERR_VAL;
317 #endif
318   
319   if (sys_mbox_valid(&mbox)) {
320     msg.type = TCPIP_MSG_API;
321     msg.msg.apimsg = apimsg;
322     sys_mbox_post(&mbox, &msg);
323     sys_arch_sem_wait(&apimsg->msg.conn->op_completed, 0);
324     return apimsg->msg.err;
325   }
326   return ERR_VAL;
327 }
328
329 #if LWIP_TCPIP_CORE_LOCKING
330 /**
331  * Call the lower part of a netconn_* function
332  * This function has exclusive access to lwIP core code by locking it
333  * before the function is called.
334  *
335  * @param apimsg a struct containing the function to call and its parameters
336  * @return ERR_OK (only for compatibility fo tcpip_apimsg())
337  */
338 err_t
339 tcpip_apimsg_lock(struct api_msg *apimsg)
340 {
341 #ifdef LWIP_DEBUG
342   /* catch functions that don't set err */
343   apimsg->msg.err = ERR_VAL;
344 #endif
345
346   LOCK_TCPIP_CORE();
347   apimsg->function(&(apimsg->msg));
348   UNLOCK_TCPIP_CORE();
349   return apimsg->msg.err;
350
351 }
352 #endif /* LWIP_TCPIP_CORE_LOCKING */
353 #endif /* LWIP_NETCONN */
354
355 #if LWIP_NETIF_API
356 #if !LWIP_TCPIP_CORE_LOCKING
357 /**
358  * Much like tcpip_apimsg, but calls the lower part of a netifapi_*
359  * function.
360  *
361  * @param netifapimsg a struct containing the function to call and its parameters
362  * @return error code given back by the function that was called
363  */
364 err_t
365 tcpip_netifapi(struct netifapi_msg* netifapimsg)
366 {
367   struct tcpip_msg msg;
368   
369   if (sys_mbox_valid(&mbox)) {
370     err_t err = sys_sem_new(&netifapimsg->msg.sem, 0);
371     if (err != ERR_OK) {
372       netifapimsg->msg.err = err;
373       return err;
374     }
375     
376     msg.type = TCPIP_MSG_NETIFAPI;
377     msg.msg.netifapimsg = netifapimsg;
378     sys_mbox_post(&mbox, &msg);
379     sys_sem_wait(&netifapimsg->msg.sem);
380     sys_sem_free(&netifapimsg->msg.sem);
381     return netifapimsg->msg.err;
382   }
383   return ERR_VAL;
384 }
385 #else /* !LWIP_TCPIP_CORE_LOCKING */
386 /**
387  * Call the lower part of a netifapi_* function
388  * This function has exclusive access to lwIP core code by locking it
389  * before the function is called.
390  *
391  * @param netifapimsg a struct containing the function to call and its parameters
392  * @return ERR_OK (only for compatibility fo tcpip_netifapi())
393  */
394 err_t
395 tcpip_netifapi_lock(struct netifapi_msg* netifapimsg)
396 {
397   LOCK_TCPIP_CORE();  
398   netifapimsg->function(&(netifapimsg->msg));
399   UNLOCK_TCPIP_CORE();
400   return netifapimsg->msg.err;
401 }
402 #endif /* !LWIP_TCPIP_CORE_LOCKING */
403 #endif /* LWIP_NETIF_API */
404
405 /**
406  * Allocate a structure for a static callback message and initialize it.
407  * This is intended to be used to send "static" messages from interrupt context.
408  *
409  * @param function the function to call
410  * @param ctx parameter passed to function
411  * @return a struct pointer to pass to tcpip_trycallback().
412  */
413 struct tcpip_callback_msg* tcpip_callbackmsg_new(tcpip_callback_fn function, void *ctx)
414 {
415   struct tcpip_msg *msg = (struct tcpip_msg *)memp_malloc(MEMP_TCPIP_MSG_API);
416   if (msg == NULL) {
417     return NULL;
418   }
419   msg->type = TCPIP_MSG_CALLBACK_STATIC;
420   msg->msg.cb.function = function;
421   msg->msg.cb.ctx = ctx;
422   return (struct tcpip_callback_msg*)msg;
423 }
424
425 /**
426  * Free a callback message allocated by tcpip_callbackmsg_new().
427  *
428  * @param msg the message to free
429  */
430 void tcpip_callbackmsg_delete(struct tcpip_callback_msg* msg)
431 {
432   memp_free(MEMP_TCPIP_MSG_API, msg);
433 }
434
435 /**
436  * Try to post a callback-message to the tcpip_thread mbox
437  * This is intended to be used to send "static" messages from interrupt context.
438  *
439  * @param msg pointer to the message to post
440  * @return sys_mbox_trypost() return code
441  */
442 err_t
443 tcpip_trycallback(struct tcpip_callback_msg* msg)
444 {
445   if (!sys_mbox_valid(&mbox)) {
446     return ERR_VAL;
447   }
448   return sys_mbox_trypost(&mbox, msg);
449 }
450
451 /**
452  * Initialize this module:
453  * - initialize all sub modules
454  * - start the tcpip_thread
455  *
456  * @param initfunc a function to call when tcpip_thread is running and finished initializing
457  * @param arg argument to pass to initfunc
458  */
459 void
460 tcpip_init(tcpip_init_done_fn initfunc, void *arg)
461 {
462   lwip_init();
463
464   tcpip_init_done = initfunc;
465   tcpip_init_done_arg = arg;
466   if(sys_mbox_new(&mbox, TCPIP_MBOX_SIZE) != ERR_OK) {
467     LWIP_ASSERT("failed to create tcpip_thread mbox", 0);
468   }
469 #if LWIP_TCPIP_CORE_LOCKING
470   if(sys_mutex_new(&lock_tcpip_core) != ERR_OK) {
471     LWIP_ASSERT("failed to create lock_tcpip_core", 0);
472   }
473 #endif /* LWIP_TCPIP_CORE_LOCKING */
474
475   sys_thread_new(TCPIP_THREAD_NAME, tcpip_thread, NULL, TCPIP_THREAD_STACKSIZE, TCPIP_THREAD_PRIO);
476 }
477
478 /**
479  * Simple callback function used with tcpip_callback to free a pbuf
480  * (pbuf_free has a wrong signature for tcpip_callback)
481  *
482  * @param p The pbuf (chain) to be dereferenced.
483  */
484 static void
485 pbuf_free_int(void *p)
486 {
487   struct pbuf *q = (struct pbuf *)p;
488   pbuf_free(q);
489 }
490
491 /**
492  * A simple wrapper function that allows you to free a pbuf from interrupt context.
493  *
494  * @param p The pbuf (chain) to be dereferenced.
495  * @return ERR_OK if callback could be enqueued, an err_t if not
496  */
497 err_t
498 pbuf_free_callback(struct pbuf *p)
499 {
500   return tcpip_callback_with_block(pbuf_free_int, p, 0);
501 }
502
503 /**
504  * A simple wrapper function that allows you to free heap memory from
505  * interrupt context.
506  *
507  * @param m the heap memory to free
508  * @return ERR_OK if callback could be enqueued, an err_t if not
509  */
510 err_t
511 mem_free_callback(void *m)
512 {
513   return tcpip_callback_with_block(mem_free, m, 0);
514 }
515
516 #endif /* !NO_SYS */