tcpip.c 15 KB

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