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/priv/tcpip_priv.h"
44#include "lwip/sys.h"
45#include "lwip/memp.h"
46#include "lwip/mem.h"
47#include "lwip/init.h"
48#include "lwip/ip.h"
49#include "lwip/pbuf.h"
50#include "lwip/etharp.h"
51#include "netif/ethernet.h"
52
53#define TCPIP_MSG_VAR_REF(name)     API_VAR_REF(name)
54#define TCPIP_MSG_VAR_DECLARE(name) API_VAR_DECLARE(struct tcpip_msg, name)
55#define TCPIP_MSG_VAR_ALLOC(name)   API_VAR_ALLOC(struct tcpip_msg, MEMP_TCPIP_MSG_API, name, ERR_MEM)
56#define TCPIP_MSG_VAR_FREE(name)    API_VAR_FREE(MEMP_TCPIP_MSG_API, name)
57
58/* global variables */
59static tcpip_init_done_fn tcpip_init_done;
60static void *tcpip_init_done_arg;
61static sys_mbox_t mbox;
62
63#if LWIP_TCPIP_CORE_LOCKING
64/** The global semaphore to lock the stack. */
65sys_mutex_t lock_tcpip_core;
66#endif /* LWIP_TCPIP_CORE_LOCKING */
67
68#if LWIP_TIMERS
69/* wait for a message, timeouts are processed while waiting */
70#define TCPIP_MBOX_FETCH(mbox, msg) sys_timeouts_mbox_fetch(mbox, msg)
71#else /* LWIP_TIMERS */
72/* wait for a message with timers disabled (e.g. pass a timer-check trigger into tcpip_thread) */
73#define TCPIP_MBOX_FETCH(mbox, msg) sys_mbox_fetch(mbox, msg)
74#endif /* LWIP_TIMERS */
75
76/**
77 * The main lwIP thread. This thread has exclusive access to lwIP core functions
78 * (unless access to them is not locked). Other threads communicate with this
79 * thread using message boxes.
80 *
81 * It also starts all the timers to make sure they are running in the right
82 * thread context.
83 *
84 * @param arg unused argument
85 */
86static void
87tcpip_thread(void *arg)
88{
89  struct tcpip_msg *msg;
90  LWIP_UNUSED_ARG(arg);
91
92  if (tcpip_init_done != NULL) {
93    tcpip_init_done(tcpip_init_done_arg);
94  }
95
96  LOCK_TCPIP_CORE();
97  while (1) {                          /* MAIN Loop */
98    UNLOCK_TCPIP_CORE();
99    LWIP_TCPIP_THREAD_ALIVE();
100    /* wait for a message, timeouts are processed while waiting */
101    TCPIP_MBOX_FETCH(&mbox, (void **)&msg);
102    LOCK_TCPIP_CORE();
103    if (msg == NULL) {
104      LWIP_DEBUGF(TCPIP_DEBUG, ("tcpip_thread: invalid message: NULL\n"));
105      LWIP_ASSERT("tcpip_thread: invalid message", 0);
106      continue;
107    }
108    switch (msg->type) {
109#if !LWIP_TCPIP_CORE_LOCKING
110    case TCPIP_MSG_API:
111      LWIP_DEBUGF(TCPIP_DEBUG, ("tcpip_thread: API message %p\n", (void *)msg));
112      msg->msg.api_msg.function(msg->msg.api_msg.msg);
113      break;
114    case TCPIP_MSG_API_CALL:
115      LWIP_DEBUGF(TCPIP_DEBUG, ("tcpip_thread: API CALL message %p\n", (void *)msg));
116      msg->msg.api_call.arg->err = msg->msg.api_call.function(msg->msg.api_call.arg);
117      sys_sem_signal(msg->msg.api_call.sem);
118      break;
119#endif /* !LWIP_TCPIP_CORE_LOCKING */
120
121#if !LWIP_TCPIP_CORE_LOCKING_INPUT
122    case TCPIP_MSG_INPKT:
123      LWIP_DEBUGF(TCPIP_DEBUG, ("tcpip_thread: PACKET %p\n", (void *)msg));
124      msg->msg.inp.input_fn(msg->msg.inp.p, msg->msg.inp.netif);
125      memp_free(MEMP_TCPIP_MSG_INPKT, msg);
126      break;
127#endif /* !LWIP_TCPIP_CORE_LOCKING_INPUT */
128
129#if LWIP_TCPIP_TIMEOUT && LWIP_TIMERS
130    case TCPIP_MSG_TIMEOUT:
131      LWIP_DEBUGF(TCPIP_DEBUG, ("tcpip_thread: TIMEOUT %p\n", (void *)msg));
132      sys_timeout(msg->msg.tmo.msecs, msg->msg.tmo.h, msg->msg.tmo.arg);
133      memp_free(MEMP_TCPIP_MSG_API, msg);
134      break;
135    case TCPIP_MSG_UNTIMEOUT:
136      LWIP_DEBUGF(TCPIP_DEBUG, ("tcpip_thread: UNTIMEOUT %p\n", (void *)msg));
137      sys_untimeout(msg->msg.tmo.h, msg->msg.tmo.arg);
138      memp_free(MEMP_TCPIP_MSG_API, msg);
139      break;
140#endif /* LWIP_TCPIP_TIMEOUT && LWIP_TIMERS */
141
142    case TCPIP_MSG_CALLBACK:
143      LWIP_DEBUGF(TCPIP_DEBUG, ("tcpip_thread: CALLBACK %p\n", (void *)msg));
144      msg->msg.cb.function(msg->msg.cb.ctx);
145      memp_free(MEMP_TCPIP_MSG_API, msg);
146      break;
147
148    case TCPIP_MSG_CALLBACK_STATIC:
149      LWIP_DEBUGF(TCPIP_DEBUG, ("tcpip_thread: CALLBACK_STATIC %p\n", (void *)msg));
150      msg->msg.cb.function(msg->msg.cb.ctx);
151      break;
152
153    default:
154      LWIP_DEBUGF(TCPIP_DEBUG, ("tcpip_thread: invalid message: %d\n", msg->type));
155      LWIP_ASSERT("tcpip_thread: invalid message", 0);
156      break;
157    }
158  }
159}
160
161/**
162 * Pass a received packet to tcpip_thread for input processing
163 *
164 * @param p the received packet
165 * @param inp the network interface on which the packet was received
166 * @param input_fn input function to call
167 */
168err_t
169tcpip_inpkt(struct pbuf *p, struct netif *inp, netif_input_fn input_fn)
170{
171#if LWIP_TCPIP_CORE_LOCKING_INPUT
172  err_t ret;
173  LWIP_DEBUGF(TCPIP_DEBUG, ("tcpip_inpkt: PACKET %p/%p\n", (void *)p, (void *)inp));
174  LOCK_TCPIP_CORE();
175  ret = input_fn(p, inp);
176  UNLOCK_TCPIP_CORE();
177  return ret;
178#else /* LWIP_TCPIP_CORE_LOCKING_INPUT */
179  struct tcpip_msg *msg;
180
181  LWIP_ASSERT("Invalid mbox", sys_mbox_valid_val(mbox));
182
183  msg = (struct tcpip_msg *)memp_malloc(MEMP_TCPIP_MSG_INPKT);
184  if (msg == NULL) {
185    return ERR_MEM;
186  }
187
188  msg->type = TCPIP_MSG_INPKT;
189  msg->msg.inp.p = p;
190  msg->msg.inp.netif = inp;
191  msg->msg.inp.input_fn = input_fn;
192  if (sys_mbox_trypost(&mbox, msg) != ERR_OK) {
193    memp_free(MEMP_TCPIP_MSG_INPKT, msg);
194    return ERR_MEM;
195  }
196  return ERR_OK;
197#endif /* LWIP_TCPIP_CORE_LOCKING_INPUT */
198}
199
200/**
201 * @ingroup lwip_os
202 * Pass a received packet to tcpip_thread for input processing with
203 * ethernet_input or ip_input. Don't call directly, pass to netif_add()
204 * and call netif->input().
205 *
206 * @param p the received packet, p->payload pointing to the Ethernet header or
207 *          to an IP header (if inp doesn't have NETIF_FLAG_ETHARP or
208 *          NETIF_FLAG_ETHERNET flags)
209 * @param inp the network interface on which the packet was received
210 */
211err_t
212tcpip_input(struct pbuf *p, struct netif *inp)
213{
214#if LWIP_ETHERNET
215  if (inp->flags & (NETIF_FLAG_ETHARP | NETIF_FLAG_ETHERNET)) {
216    return tcpip_inpkt(p, inp, ethernet_input);
217  } else
218#endif /* LWIP_ETHERNET */
219  return tcpip_inpkt(p, inp, ip_input);
220}
221
222/**
223 * Call a specific function in the thread context of
224 * tcpip_thread for easy access synchronization.
225 * A function called in that way may access lwIP core code
226 * without fearing concurrent access.
227 *
228 * @param function the function to call
229 * @param ctx parameter passed to f
230 * @param block 1 to block until the request is posted, 0 to non-blocking mode
231 * @return ERR_OK if the function was called, another err_t if not
232 */
233err_t
234tcpip_callback_with_block(tcpip_callback_fn function, void *ctx, u8_t block)
235{
236  struct tcpip_msg *msg;
237
238  LWIP_ASSERT("Invalid mbox", sys_mbox_valid_val(mbox));
239
240  msg = (struct tcpip_msg *)memp_malloc(MEMP_TCPIP_MSG_API);
241  if (msg == NULL) {
242    return ERR_MEM;
243  }
244
245  msg->type = TCPIP_MSG_CALLBACK;
246  msg->msg.cb.function = function;
247  msg->msg.cb.ctx = ctx;
248  if (block) {
249    sys_mbox_post(&mbox, msg);
250  } else {
251    if (sys_mbox_trypost(&mbox, msg) != ERR_OK) {
252      memp_free(MEMP_TCPIP_MSG_API, msg);
253      return ERR_MEM;
254    }
255  }
256  return ERR_OK;
257}
258
259#if LWIP_TCPIP_TIMEOUT && LWIP_TIMERS
260/**
261 * call sys_timeout in tcpip_thread
262 *
263 * @param msecs time in milliseconds for timeout
264 * @param h function to be called on timeout
265 * @param arg argument to pass to timeout function h
266 * @return ERR_MEM on memory error, ERR_OK otherwise
267 */
268err_t
269tcpip_timeout(u32_t msecs, sys_timeout_handler h, void *arg)
270{
271  struct tcpip_msg *msg;
272
273  LWIP_ASSERT("Invalid mbox", sys_mbox_valid_val(mbox));
274
275  msg = (struct tcpip_msg *)memp_malloc(MEMP_TCPIP_MSG_API);
276  if (msg == NULL) {
277    return ERR_MEM;
278  }
279
280  msg->type = TCPIP_MSG_TIMEOUT;
281  msg->msg.tmo.msecs = msecs;
282  msg->msg.tmo.h = h;
283  msg->msg.tmo.arg = arg;
284  sys_mbox_post(&mbox, msg);
285  return ERR_OK;
286}
287
288/**
289 * call sys_untimeout in tcpip_thread
290 *
291 * @param h function to be called on timeout
292 * @param arg argument to pass to timeout function h
293 * @return ERR_MEM on memory error, ERR_OK otherwise
294 */
295err_t
296tcpip_untimeout(sys_timeout_handler h, void *arg)
297{
298  struct tcpip_msg *msg;
299
300  LWIP_ASSERT("Invalid mbox", sys_mbox_valid_val(mbox));
301
302  msg = (struct tcpip_msg *)memp_malloc(MEMP_TCPIP_MSG_API);
303  if (msg == NULL) {
304    return ERR_MEM;
305  }
306
307  msg->type = TCPIP_MSG_UNTIMEOUT;
308  msg->msg.tmo.h = h;
309  msg->msg.tmo.arg = arg;
310  sys_mbox_post(&mbox, msg);
311  return ERR_OK;
312}
313#endif /* LWIP_TCPIP_TIMEOUT && LWIP_TIMERS */
314
315
316/**
317 * Sends a message to TCPIP thread to call a function. Caller thread blocks on
318 * on a provided semaphore, which ist NOT automatically signalled by TCPIP thread,
319 * this has to be done by the user.
320 * It is recommended to use LWIP_TCPIP_CORE_LOCKING since this is the way
321 * with least runtime overhead.
322 *
323 * @param fn function to be called from TCPIP thread
324 * @param apimsg argument to API function
325 * @param sem semaphore to wait on
326 * @return ERR_OK if the function was called, another err_t if not
327 */
328err_t
329tcpip_send_msg_wait_sem(tcpip_callback_fn fn, void *apimsg, sys_sem_t* sem)
330{
331#if LWIP_TCPIP_CORE_LOCKING
332  LWIP_UNUSED_ARG(sem);
333  LOCK_TCPIP_CORE();
334  fn(apimsg);
335  UNLOCK_TCPIP_CORE();
336  return ERR_OK;
337#else /* LWIP_TCPIP_CORE_LOCKING */
338  TCPIP_MSG_VAR_DECLARE(msg);
339
340  LWIP_ASSERT("semaphore not initialized", sys_sem_valid(sem));
341  LWIP_ASSERT("Invalid mbox", sys_mbox_valid_val(mbox));
342
343  TCPIP_MSG_VAR_ALLOC(msg);
344  TCPIP_MSG_VAR_REF(msg).type = TCPIP_MSG_API;
345  TCPIP_MSG_VAR_REF(msg).msg.api_msg.function = fn;
346  TCPIP_MSG_VAR_REF(msg).msg.api_msg.msg = apimsg;
347  sys_mbox_post(&mbox, &TCPIP_MSG_VAR_REF(msg));
348  sys_arch_sem_wait(sem, 0);
349  TCPIP_MSG_VAR_FREE(msg);
350  return ERR_OK;
351#endif /* LWIP_TCPIP_CORE_LOCKING */
352}
353
354/**
355 * Synchronously calls function in TCPIP thread and waits for its completion.
356 * It is recommended to use LWIP_TCPIP_CORE_LOCKING (preferred) or
357 * LWIP_NETCONN_SEM_PER_THREAD.
358 * If not, a semaphore is created and destroyed on every call which is usually
359 * an expensive/slow operation.
360 * @param fn Function to call
361 * @param call Call parameters
362 * @return Return value from tcpip_api_call_fn
363 */
364err_t
365tcpip_api_call(tcpip_api_call_fn fn, struct tcpip_api_call_data *call)
366{
367#if LWIP_TCPIP_CORE_LOCKING
368  err_t err;
369  LOCK_TCPIP_CORE();
370  err = fn(call);
371  UNLOCK_TCPIP_CORE();
372  return err;
373#else /* LWIP_TCPIP_CORE_LOCKING */
374  TCPIP_MSG_VAR_DECLARE(msg);
375
376#if !LWIP_NETCONN_SEM_PER_THREAD
377  err_t err = sys_sem_new(&call->sem, 0);
378  if (err != ERR_OK) {
379    return err;
380  }
381#endif /* LWIP_NETCONN_SEM_PER_THREAD */
382
383  LWIP_ASSERT("Invalid mbox", sys_mbox_valid_val(mbox));
384
385  TCPIP_MSG_VAR_ALLOC(msg);
386  TCPIP_MSG_VAR_REF(msg).type = TCPIP_MSG_API_CALL;
387  TCPIP_MSG_VAR_REF(msg).msg.api_call.arg = call;
388  TCPIP_MSG_VAR_REF(msg).msg.api_call.function = fn;
389#if LWIP_NETCONN_SEM_PER_THREAD
390  TCPIP_MSG_VAR_REF(msg).msg.api_call.sem = LWIP_NETCONN_THREAD_SEM_GET();
391#else /* LWIP_NETCONN_SEM_PER_THREAD */
392  TCPIP_MSG_VAR_REF(msg).msg.api_call.sem = &call->sem;
393#endif /* LWIP_NETCONN_SEM_PER_THREAD */
394  sys_mbox_post(&mbox, &TCPIP_MSG_VAR_REF(msg));
395  sys_arch_sem_wait(TCPIP_MSG_VAR_REF(msg).msg.api_call.sem, 0);
396  TCPIP_MSG_VAR_FREE(msg);
397
398#if !LWIP_NETCONN_SEM_PER_THREAD
399  sys_sem_free(&call->sem);
400#endif /* LWIP_NETCONN_SEM_PER_THREAD */
401
402  return call->err;
403#endif /* LWIP_TCPIP_CORE_LOCKING */
404}
405
406/**
407 * Allocate a structure for a static callback message and initialize it.
408 * This is intended to be used to send "static" messages from interrupt context.
409 *
410 * @param function the function to call
411 * @param ctx parameter passed to function
412 * @return a struct pointer to pass to tcpip_trycallback().
413 */
414struct tcpip_callback_msg*
415tcpip_callbackmsg_new(tcpip_callback_fn function, void *ctx)
416{
417  struct tcpip_msg *msg = (struct tcpip_msg *)memp_malloc(MEMP_TCPIP_MSG_API);
418  if (msg == NULL) {
419    return NULL;
420  }
421  msg->type = TCPIP_MSG_CALLBACK_STATIC;
422  msg->msg.cb.function = function;
423  msg->msg.cb.ctx = ctx;
424  return (struct tcpip_callback_msg*)msg;
425}
426
427/**
428 * Free a callback message allocated by tcpip_callbackmsg_new().
429 *
430 * @param msg the message to free
431 */
432void
433tcpip_callbackmsg_delete(struct tcpip_callback_msg* msg)
434{
435  memp_free(MEMP_TCPIP_MSG_API, msg);
436}
437
438/**
439 * Try to post a callback-message to the tcpip_thread mbox
440 * This is intended to be used to send "static" messages from interrupt context.
441 *
442 * @param msg pointer to the message to post
443 * @return sys_mbox_trypost() return code
444 */
445err_t
446tcpip_trycallback(struct tcpip_callback_msg* msg)
447{
448  LWIP_ASSERT("Invalid mbox", sys_mbox_valid_val(mbox));
449  return sys_mbox_trypost(&mbox, msg);
450}
451
452/**
453 * @ingroup lwip_os
454 * Initialize this module:
455 * - initialize all sub modules
456 * - start the tcpip_thread
457 *
458 * @param initfunc a function to call when tcpip_thread is running and finished initializing
459 * @param arg argument to pass to initfunc
460 */
461void
462tcpip_init(tcpip_init_done_fn initfunc, void *arg)
463{
464  lwip_init();
465
466  tcpip_init_done = initfunc;
467  tcpip_init_done_arg = arg;
468  if (sys_mbox_new(&mbox, TCPIP_MBOX_SIZE) != ERR_OK) {
469    LWIP_ASSERT("failed to create tcpip_thread mbox", 0);
470  }
471#if LWIP_TCPIP_CORE_LOCKING
472  if (sys_mutex_new(&lock_tcpip_core) != ERR_OK) {
473    LWIP_ASSERT("failed to create lock_tcpip_core", 0);
474  }
475#endif /* LWIP_TCPIP_CORE_LOCKING */
476
477  sys_thread_new(TCPIP_THREAD_NAME, tcpip_thread, NULL, TCPIP_THREAD_STACKSIZE, TCPIP_THREAD_PRIO);
478}
479
480/**
481 * Simple callback function used with tcpip_callback to free a pbuf
482 * (pbuf_free has a wrong signature for tcpip_callback)
483 *
484 * @param p The pbuf (chain) to be dereferenced.
485 */
486static void
487pbuf_free_int(void *p)
488{
489  struct pbuf *q = (struct pbuf *)p;
490  pbuf_free(q);
491}
492
493/**
494 * A simple wrapper function that allows you to free a pbuf from interrupt context.
495 *
496 * @param p The pbuf (chain) to be dereferenced.
497 * @return ERR_OK if callback could be enqueued, an err_t if not
498 */
499err_t
500pbuf_free_callback(struct pbuf *p)
501{
502  return tcpip_callback_with_block(pbuf_free_int, p, 0);
503}
504
505/**
506 * A simple wrapper function that allows you to free heap memory from
507 * interrupt context.
508 *
509 * @param m the heap memory to free
510 * @return ERR_OK if callback could be enqueued, an err_t if not
511 */
512err_t
513mem_free_callback(void *m)
514{
515  return tcpip_callback_with_block(mem_free, m, 0);
516}
517
518#endif /* !NO_SYS */
519