tcpip.c 13 KB

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