[LWIP]
[reactos.git] / lib / drivers / lwip / 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 "lwip/ip.h"
50 #include "netif/etharp.h"
51 #include "netif/ppp_oe.h"
52
53 /* global variables */
54 static tcpip_init_done_fn tcpip_init_done;
55 static void *tcpip_init_done_arg;
56 static sys_mbox_t mbox;
57
58 #if LWIP_TCPIP_CORE_LOCKING
59 /** The global semaphore to lock the stack. */
60 sys_mutex_t lock_tcpip_core;
61 #endif /* LWIP_TCPIP_CORE_LOCKING */
62
63
64 /**
65 * The main lwIP thread. This thread has exclusive access to lwIP core functions
66 * (unless access to them is not locked). Other threads communicate with this
67 * thread using message boxes.
68 *
69 * It also starts all the timers to make sure they are running in the right
70 * thread context.
71 *
72 * @param arg unused argument
73 */
74 static void
75 tcpip_thread(void *arg)
76 {
77 struct tcpip_msg *msg;
78 LWIP_UNUSED_ARG(arg);
79
80 if (tcpip_init_done != NULL) {
81 tcpip_init_done(tcpip_init_done_arg);
82 }
83
84 LOCK_TCPIP_CORE();
85 while (1) { /* MAIN Loop */
86 UNLOCK_TCPIP_CORE();
87 LWIP_TCPIP_THREAD_ALIVE();
88 /* wait for a message, timeouts are processed while waiting */
89 sys_timeouts_mbox_fetch(&mbox, (void **)&msg);
90 LOCK_TCPIP_CORE();
91 switch (msg->type) {
92 #if LWIP_NETCONN
93 case TCPIP_MSG_API:
94 LWIP_DEBUGF(TCPIP_DEBUG, ("tcpip_thread: API message %p\n", (void *)msg));
95 msg->msg.apimsg->function(&(msg->msg.apimsg->msg));
96 break;
97 #endif /* LWIP_NETCONN */
98
99 #if !LWIP_TCPIP_CORE_LOCKING_INPUT
100 case TCPIP_MSG_INPKT:
101 LWIP_DEBUGF(TCPIP_DEBUG, ("tcpip_thread: PACKET %p\n", (void *)msg));
102 #if LWIP_ETHERNET
103 if (msg->msg.inp.netif->flags & (NETIF_FLAG_ETHARP | NETIF_FLAG_ETHERNET)) {
104 ethernet_input(msg->msg.inp.p, msg->msg.inp.netif);
105 } else
106 #endif /* LWIP_ETHERNET */
107 {
108 ip_input(msg->msg.inp.p, msg->msg.inp.netif);
109 }
110 memp_free(MEMP_TCPIP_MSG_INPKT, msg);
111 break;
112 #endif /* LWIP_TCPIP_CORE_LOCKING_INPUT */
113
114 #if LWIP_NETIF_API
115 case TCPIP_MSG_NETIFAPI:
116 LWIP_DEBUGF(TCPIP_DEBUG, ("tcpip_thread: Netif API message %p\n", (void *)msg));
117 msg->msg.netifapimsg->function(&(msg->msg.netifapimsg->msg));
118 break;
119 #endif /* LWIP_NETIF_API */
120
121 case TCPIP_MSG_CALLBACK:
122 LWIP_DEBUGF(TCPIP_DEBUG, ("tcpip_thread: CALLBACK %p\n", (void *)msg));
123 msg->msg.cb.function(msg->msg.cb.ctx);
124 memp_free(MEMP_TCPIP_MSG_API, msg);
125 break;
126
127 #if LWIP_TCPIP_TIMEOUT
128 case TCPIP_MSG_TIMEOUT:
129 LWIP_DEBUGF(TCPIP_DEBUG, ("tcpip_thread: TIMEOUT %p\n", (void *)msg));
130 sys_timeout(msg->msg.tmo.msecs, msg->msg.tmo.h, msg->msg.tmo.arg);
131 memp_free(MEMP_TCPIP_MSG_API, msg);
132 break;
133 case TCPIP_MSG_UNTIMEOUT:
134 LWIP_DEBUGF(TCPIP_DEBUG, ("tcpip_thread: UNTIMEOUT %p\n", (void *)msg));
135 sys_untimeout(msg->msg.tmo.h, msg->msg.tmo.arg);
136 memp_free(MEMP_TCPIP_MSG_API, msg);
137 break;
138 #endif /* LWIP_TCPIP_TIMEOUT */
139
140 default:
141 LWIP_DEBUGF(TCPIP_DEBUG, ("tcpip_thread: invalid message: %d\n", msg->type));
142 LWIP_ASSERT("tcpip_thread: invalid message", 0);
143 break;
144 }
145 }
146 }
147
148 /**
149 * Pass a received packet to tcpip_thread for input processing
150 *
151 * @param p the received packet, p->payload pointing to the Ethernet header or
152 * to an IP header (if inp doesn't have NETIF_FLAG_ETHARP or
153 * NETIF_FLAG_ETHERNET flags)
154 * @param inp the network interface on which the packet was received
155 */
156 err_t
157 tcpip_input(struct pbuf *p, struct netif *inp)
158 {
159 #if LWIP_TCPIP_CORE_LOCKING_INPUT
160 err_t ret;
161 LWIP_DEBUGF(TCPIP_DEBUG, ("tcpip_input: PACKET %p/%p\n", (void *)p, (void *)inp));
162 LOCK_TCPIP_CORE();
163 #if LWIP_ETHERNET
164 if (inp->flags & (NETIF_FLAG_ETHARP | NETIF_FLAG_ETHERNET)) {
165 ret = ethernet_input(p, inp);
166 } else
167 #endif /* LWIP_ETHERNET */
168 {
169 ret = ip_input(p, inp);
170 }
171 UNLOCK_TCPIP_CORE();
172 return ret;
173 #else /* LWIP_TCPIP_CORE_LOCKING_INPUT */
174 struct tcpip_msg *msg;
175
176 if (sys_mbox_valid(&mbox)) {
177 msg = (struct tcpip_msg *)memp_malloc(MEMP_TCPIP_MSG_INPKT);
178 if (msg == NULL) {
179 return ERR_MEM;
180 }
181
182 msg->type = TCPIP_MSG_INPKT;
183 msg->msg.inp.p = p;
184 msg->msg.inp.netif = inp;
185 if (sys_mbox_trypost(&mbox, msg) != ERR_OK) {
186 memp_free(MEMP_TCPIP_MSG_INPKT, msg);
187 return ERR_MEM;
188 }
189 return ERR_OK;
190 }
191 return ERR_VAL;
192 #endif /* LWIP_TCPIP_CORE_LOCKING_INPUT */
193 }
194
195 /**
196 * Call a specific function in the thread context of
197 * tcpip_thread for easy access synchronization.
198 * A function called in that way may access lwIP core code
199 * without fearing concurrent access.
200 *
201 * @param f the function to call
202 * @param ctx parameter passed to f
203 * @param block 1 to block until the request is posted, 0 to non-blocking mode
204 * @return ERR_OK if the function was called, another err_t if not
205 */
206 err_t
207 tcpip_callback_with_block(tcpip_callback_fn function, void *ctx, u8_t block)
208 {
209 struct tcpip_msg *msg;
210
211 if (sys_mbox_valid(&mbox)) {
212 msg = (struct tcpip_msg *)memp_malloc(MEMP_TCPIP_MSG_API);
213 if (msg == NULL) {
214 return ERR_MEM;
215 }
216
217 msg->type = TCPIP_MSG_CALLBACK;
218 msg->msg.cb.function = function;
219 msg->msg.cb.ctx = ctx;
220 if (block) {
221 sys_mbox_post(&mbox, msg);
222 } else {
223 if (sys_mbox_trypost(&mbox, msg) != ERR_OK) {
224 memp_free(MEMP_TCPIP_MSG_API, msg);
225 return ERR_MEM;
226 }
227 }
228 return ERR_OK;
229 }
230 return ERR_VAL;
231 }
232
233 #if LWIP_TCPIP_TIMEOUT
234 /**
235 * call sys_timeout in tcpip_thread
236 *
237 * @param msec time in milliseconds for timeout
238 * @param h function to be called on timeout
239 * @param arg argument to pass to timeout function h
240 * @return ERR_MEM on memory error, ERR_OK otherwise
241 */
242 err_t
243 tcpip_timeout(u32_t msecs, sys_timeout_handler h, void *arg)
244 {
245 struct tcpip_msg *msg;
246
247 if (sys_mbox_valid(&mbox)) {
248 msg = (struct tcpip_msg *)memp_malloc(MEMP_TCPIP_MSG_API);
249 if (msg == NULL) {
250 return ERR_MEM;
251 }
252
253 msg->type = TCPIP_MSG_TIMEOUT;
254 msg->msg.tmo.msecs = msecs;
255 msg->msg.tmo.h = h;
256 msg->msg.tmo.arg = arg;
257 sys_mbox_post(&mbox, msg);
258 return ERR_OK;
259 }
260 return ERR_VAL;
261 }
262
263 /**
264 * call sys_untimeout in tcpip_thread
265 *
266 * @param msec time in milliseconds for timeout
267 * @param h function to be called on timeout
268 * @param arg argument to pass to timeout function h
269 * @return ERR_MEM on memory error, ERR_OK otherwise
270 */
271 err_t
272 tcpip_untimeout(sys_timeout_handler h, void *arg)
273 {
274 struct tcpip_msg *msg;
275
276 if (sys_mbox_valid(&mbox)) {
277 msg = (struct tcpip_msg *)memp_malloc(MEMP_TCPIP_MSG_API);
278 if (msg == NULL) {
279 return ERR_MEM;
280 }
281
282 msg->type = TCPIP_MSG_UNTIMEOUT;
283 msg->msg.tmo.h = h;
284 msg->msg.tmo.arg = arg;
285 sys_mbox_post(&mbox, msg);
286 return ERR_OK;
287 }
288 return ERR_VAL;
289 }
290 #endif /* LWIP_TCPIP_TIMEOUT */
291
292 #if LWIP_NETCONN
293 /**
294 * Call the lower part of a netconn_* function
295 * This function is then running in the thread context
296 * of tcpip_thread and has exclusive access to lwIP core code.
297 *
298 * @param apimsg a struct containing the function to call and its parameters
299 * @return ERR_OK if the function was called, another err_t if not
300 */
301 err_t
302 tcpip_apimsg(struct api_msg *apimsg)
303 {
304 struct tcpip_msg msg;
305 #ifdef LWIP_DEBUG
306 /* catch functions that don't set err */
307 apimsg->msg.err = ERR_VAL;
308 #endif
309
310 if (sys_mbox_valid(&mbox)) {
311 msg.type = TCPIP_MSG_API;
312 msg.msg.apimsg = apimsg;
313 sys_mbox_post(&mbox, &msg);
314 sys_arch_sem_wait(&apimsg->msg.conn->op_completed, 0);
315 return apimsg->msg.err;
316 }
317 return ERR_VAL;
318 }
319
320 #if LWIP_TCPIP_CORE_LOCKING
321 /**
322 * Call the lower part of a netconn_* function
323 * This function has exclusive access to lwIP core code by locking it
324 * before the function is called.
325 *
326 * @param apimsg a struct containing the function to call and its parameters
327 * @return ERR_OK (only for compatibility fo tcpip_apimsg())
328 */
329 err_t
330 tcpip_apimsg_lock(struct api_msg *apimsg)
331 {
332 #ifdef LWIP_DEBUG
333 /* catch functions that don't set err */
334 apimsg->msg.err = ERR_VAL;
335 #endif
336
337 LOCK_TCPIP_CORE();
338 apimsg->function(&(apimsg->msg));
339 UNLOCK_TCPIP_CORE();
340 return apimsg->msg.err;
341
342 }
343 #endif /* LWIP_TCPIP_CORE_LOCKING */
344 #endif /* LWIP_NETCONN */
345
346 #if LWIP_NETIF_API
347 #if !LWIP_TCPIP_CORE_LOCKING
348 /**
349 * Much like tcpip_apimsg, but calls the lower part of a netifapi_*
350 * function.
351 *
352 * @param netifapimsg a struct containing the function to call and its parameters
353 * @return error code given back by the function that was called
354 */
355 err_t
356 tcpip_netifapi(struct netifapi_msg* netifapimsg)
357 {
358 struct tcpip_msg msg;
359
360 if (sys_mbox_valid(&mbox)) {
361 err_t err = sys_sem_new(&netifapimsg->msg.sem, 0);
362 if (err != ERR_OK) {
363 netifapimsg->msg.err = err;
364 return err;
365 }
366
367 msg.type = TCPIP_MSG_NETIFAPI;
368 msg.msg.netifapimsg = netifapimsg;
369 sys_mbox_post(&mbox, &msg);
370 sys_sem_wait(&netifapimsg->msg.sem);
371 sys_sem_free(&netifapimsg->msg.sem);
372 return netifapimsg->msg.err;
373 }
374 return ERR_VAL;
375 }
376 #else /* !LWIP_TCPIP_CORE_LOCKING */
377 /**
378 * Call the lower part of a netifapi_* function
379 * This function has exclusive access to lwIP core code by locking it
380 * before the function is called.
381 *
382 * @param netifapimsg a struct containing the function to call and its parameters
383 * @return ERR_OK (only for compatibility fo tcpip_netifapi())
384 */
385 err_t
386 tcpip_netifapi_lock(struct netifapi_msg* netifapimsg)
387 {
388 LOCK_TCPIP_CORE();
389 netifapimsg->function(&(netifapimsg->msg));
390 UNLOCK_TCPIP_CORE();
391 return netifapimsg->msg.err;
392 }
393 #endif /* !LWIP_TCPIP_CORE_LOCKING */
394 #endif /* LWIP_NETIF_API */
395
396 /**
397 * Initialize this module:
398 * - initialize all sub modules
399 * - start the tcpip_thread
400 *
401 * @param initfunc a function to call when tcpip_thread is running and finished initializing
402 * @param arg argument to pass to initfunc
403 */
404 void
405 tcpip_init(tcpip_init_done_fn initfunc, void *arg)
406 {
407 lwip_init();
408
409 tcpip_init_done = initfunc;
410 tcpip_init_done_arg = arg;
411 if(sys_mbox_new(&mbox, TCPIP_MBOX_SIZE) != ERR_OK) {
412 LWIP_ASSERT("failed to create tcpip_thread mbox", 0);
413 }
414 #if LWIP_TCPIP_CORE_LOCKING
415 if(sys_mutex_new(&lock_tcpip_core) != ERR_OK) {
416 LWIP_ASSERT("failed to create lock_tcpip_core", 0);
417 }
418 #endif /* LWIP_TCPIP_CORE_LOCKING */
419
420 sys_thread_new(TCPIP_THREAD_NAME, tcpip_thread, NULL, TCPIP_THREAD_STACKSIZE, TCPIP_THREAD_PRIO);
421 }
422
423 /**
424 * Simple callback function used with tcpip_callback to free a pbuf
425 * (pbuf_free has a wrong signature for tcpip_callback)
426 *
427 * @param p The pbuf (chain) to be dereferenced.
428 */
429 static void
430 pbuf_free_int(void *p)
431 {
432 struct pbuf *q = (struct pbuf *)p;
433 pbuf_free(q);
434 }
435
436 /**
437 * A simple wrapper function that allows you to free a pbuf from interrupt context.
438 *
439 * @param p The pbuf (chain) to be dereferenced.
440 * @return ERR_OK if callback could be enqueued, an err_t if not
441 */
442 err_t
443 pbuf_free_callback(struct pbuf *p)
444 {
445 return tcpip_callback_with_block(pbuf_free_int, p, 0);
446 }
447
448 /**
449 * A simple wrapper function that allows you to free heap memory from
450 * interrupt context.
451 *
452 * @param m the heap memory to free
453 * @return ERR_OK if callback could be enqueued, an err_t if not
454 */
455 err_t
456 mem_free_callback(void *m)
457 {
458 return tcpip_callback_with_block(mem_free, m, 0);
459 }
460
461 #endif /* !NO_SYS */