SAMV71 Xplained Ultra Software Package 1.4

tcpip.c

Go to the documentation of this file.
00001  /**
00002  * @file
00003  * Sequential API Main thread module
00004  *
00005  */
00006 
00007 /*
00008  * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
00009  * All rights reserved.
00010  *
00011  * Redistribution and use in source and binary forms, with or without modification,
00012  * are permitted provided that the following conditions are met:
00013  *
00014  * 1. Redistributions of source code must retain the above copyright notice,
00015  *    this list of conditions and the following disclaimer.
00016  * 2. Redistributions in binary form must reproduce the above copyright notice,
00017  *    this list of conditions and the following disclaimer in the documentation
00018  *    and/or other materials provided with the distribution.
00019  * 3. The name of the author may not be used to endorse or promote products
00020  *    derived from this software without specific prior written permission.
00021  *
00022  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
00023  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
00024  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
00025  * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00026  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
00027  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00028  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00029  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
00030  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
00031  * OF SUCH DAMAGE.
00032  *
00033  * This file is part of the lwIP TCP/IP stack.
00034  *
00035  * Author: Adam Dunkels <adam@sics.se>
00036  *
00037  */
00038 
00039 #include "lwip/opt.h"
00040 
00041 #if !NO_SYS /* don't build if not configured for use in lwipopts.h */
00042 
00043 #include "lwip/sys.h"
00044 #include "lwip/memp.h"
00045 #include "lwip/pbuf.h"
00046 #include "lwip/ip_frag.h"
00047 #include "lwip/tcp.h"
00048 #include "lwip/autoip.h"
00049 #include "lwip/dhcp.h"
00050 #include "lwip/igmp.h"
00051 #include "lwip/dns.h"
00052 #include "lwip/tcpip.h"
00053 #include "lwip/init.h"
00054 #include "netif/etharp.h"
00055 #include "netif/ppp_oe.h"
00056 
00057 /* global variables */
00058 static void (* tcpip_init_done)(void *arg);
00059 static void *tcpip_init_done_arg;
00060 static sys_mbox_t mbox = SYS_MBOX_NULL;
00061 
00062 #if LWIP_TCPIP_CORE_LOCKING
00063 /** The global semaphore to lock the stack. */
00064 sys_sem_t lock_tcpip_core;
00065 #endif /* LWIP_TCPIP_CORE_LOCKING */
00066 
00067 #if LWIP_TCP
00068 /* global variable that shows if the tcp timer is currently scheduled or not */
00069 static int tcpip_tcp_timer_active;
00070 
00071 /**
00072  * Timer callback function that calls tcp_tmr() and reschedules itself.
00073  *
00074  * @param arg unused argument
00075  */
00076 static void
00077 tcpip_tcp_timer(void *arg)
00078 {
00079   LWIP_UNUSED_ARG(arg);
00080 
00081   /* call TCP timer handler */
00082   tcp_tmr();
00083   /* timer still needed? */
00084   if (tcp_active_pcbs || tcp_tw_pcbs) {
00085     /* restart timer */
00086     sys_timeout(TCP_TMR_INTERVAL, tcpip_tcp_timer, NULL);
00087   } else {
00088     /* disable timer */
00089     tcpip_tcp_timer_active = 0;
00090   }
00091 }
00092 
00093 #if !NO_SYS
00094 /**
00095  * Called from TCP_REG when registering a new PCB:
00096  * the reason is to have the TCP timer only running when
00097  * there are active (or time-wait) PCBs.
00098  */
00099 void
00100 tcp_timer_needed(void)
00101 {
00102   /* timer is off but needed again? */
00103   if (!tcpip_tcp_timer_active && (tcp_active_pcbs || tcp_tw_pcbs)) {
00104     /* enable and start timer */
00105     tcpip_tcp_timer_active = 1;
00106     sys_timeout(TCP_TMR_INTERVAL, tcpip_tcp_timer, NULL);
00107   }
00108 }
00109 #endif /* !NO_SYS */
00110 #endif /* LWIP_TCP */
00111 
00112 #if IP_REASSEMBLY
00113 /**
00114  * Timer callback function that calls ip_reass_tmr() and reschedules itself.
00115  *
00116  * @param arg unused argument
00117  */
00118 static void
00119 ip_reass_timer(void *arg)
00120 {
00121   LWIP_UNUSED_ARG(arg);
00122   LWIP_DEBUGF(TCPIP_DEBUG, ("tcpip: ip_reass_tmr()\n"));
00123   ip_reass_tmr();
00124   sys_timeout(IP_TMR_INTERVAL, ip_reass_timer, NULL);
00125 }
00126 #endif /* IP_REASSEMBLY */
00127 
00128 #if LWIP_ARP
00129 /**
00130  * Timer callback function that calls etharp_tmr() and reschedules itself.
00131  *
00132  * @param arg unused argument
00133  */
00134 static void
00135 arp_timer(void *arg)
00136 {
00137   LWIP_UNUSED_ARG(arg);
00138   LWIP_DEBUGF(TCPIP_DEBUG, ("tcpip: etharp_tmr()\n"));
00139   etharp_tmr();
00140   sys_timeout(ARP_TMR_INTERVAL, arp_timer, NULL);
00141 }
00142 #endif /* LWIP_ARP */
00143 
00144 #if LWIP_DHCP
00145 /**
00146  * Timer callback function that calls dhcp_coarse_tmr() and reschedules itself.
00147  *
00148  * @param arg unused argument
00149  */
00150 static void
00151 dhcp_timer_coarse(void *arg)
00152 {
00153   LWIP_UNUSED_ARG(arg);
00154   LWIP_DEBUGF(TCPIP_DEBUG, ("tcpip: dhcp_coarse_tmr()\n"));
00155   dhcp_coarse_tmr();
00156   sys_timeout(DHCP_COARSE_TIMER_MSECS, dhcp_timer_coarse, NULL);
00157 }
00158 
00159 /**
00160  * Timer callback function that calls dhcp_fine_tmr() and reschedules itself.
00161  *
00162  * @param arg unused argument
00163  */
00164 static void
00165 dhcp_timer_fine(void *arg)
00166 {
00167   LWIP_UNUSED_ARG(arg);
00168   LWIP_DEBUGF(TCPIP_DEBUG, ("tcpip: dhcp_fine_tmr()\n"));
00169   dhcp_fine_tmr();
00170   sys_timeout(DHCP_FINE_TIMER_MSECS, dhcp_timer_fine, NULL);
00171 }
00172 #endif /* LWIP_DHCP */
00173 
00174 #if LWIP_AUTOIP
00175 /**
00176  * Timer callback function that calls autoip_tmr() and reschedules itself.
00177  *
00178  * @param arg unused argument
00179  */
00180 static void
00181 autoip_timer(void *arg)
00182 {
00183   LWIP_UNUSED_ARG(arg);
00184   LWIP_DEBUGF(TCPIP_DEBUG, ("tcpip: autoip_tmr()\n"));
00185   autoip_tmr();
00186   sys_timeout(AUTOIP_TMR_INTERVAL, autoip_timer, NULL);
00187 }
00188 #endif /* LWIP_AUTOIP */
00189 
00190 #if LWIP_IGMP
00191 /**
00192  * Timer callback function that calls igmp_tmr() and reschedules itself.
00193  *
00194  * @param arg unused argument
00195  */
00196 static void
00197 igmp_timer(void *arg)
00198 {
00199   LWIP_UNUSED_ARG(arg);
00200   LWIP_DEBUGF(TCPIP_DEBUG, ("tcpip: igmp_tmr()\n"));
00201   igmp_tmr();
00202   sys_timeout(IGMP_TMR_INTERVAL, igmp_timer, NULL);
00203 }
00204 #endif /* LWIP_IGMP */
00205 
00206 #if LWIP_DNS
00207 /**
00208  * Timer callback function that calls dns_tmr() and reschedules itself.
00209  *
00210  * @param arg unused argument
00211  */
00212 static void
00213 dns_timer(void *arg)
00214 {
00215   LWIP_UNUSED_ARG(arg);
00216   LWIP_DEBUGF(TCPIP_DEBUG, ("tcpip: dns_tmr()\n"));
00217   dns_tmr();
00218   sys_timeout(DNS_TMR_INTERVAL, dns_timer, NULL);
00219 }
00220 #endif /* LWIP_DNS */
00221 
00222 /**
00223  * The main lwIP thread. This thread has exclusive access to lwIP core functions
00224  * (unless access to them is not locked). Other threads communicate with this
00225  * thread using message boxes.
00226  *
00227  * It also starts all the timers to make sure they are running in the right
00228  * thread context.
00229  *
00230  * @param arg unused argument
00231  */
00232 static void
00233 tcpip_thread(void *arg)
00234 {
00235   struct tcpip_msg *msg;
00236   LWIP_UNUSED_ARG(arg);
00237 
00238 #if IP_REASSEMBLY
00239   sys_timeout(IP_TMR_INTERVAL, ip_reass_timer, NULL);
00240 #endif /* IP_REASSEMBLY */
00241 #if LWIP_ARP
00242   sys_timeout(ARP_TMR_INTERVAL, arp_timer, NULL);
00243 #endif /* LWIP_ARP */
00244 #if LWIP_DHCP
00245   sys_timeout(DHCP_COARSE_TIMER_MSECS, dhcp_timer_coarse, NULL);
00246   sys_timeout(DHCP_FINE_TIMER_MSECS, dhcp_timer_fine, NULL);
00247 #endif /* LWIP_DHCP */
00248 #if LWIP_AUTOIP
00249   sys_timeout(AUTOIP_TMR_INTERVAL, autoip_timer, NULL);
00250 #endif /* LWIP_AUTOIP */
00251 #if LWIP_IGMP
00252   sys_timeout(IGMP_TMR_INTERVAL, igmp_timer, NULL);
00253 #endif /* LWIP_IGMP */
00254 #if LWIP_DNS
00255   sys_timeout(DNS_TMR_INTERVAL, dns_timer, NULL);
00256 #endif /* LWIP_DNS */
00257 
00258   if (tcpip_init_done != NULL) {
00259     tcpip_init_done(tcpip_init_done_arg);
00260   }
00261 
00262   LOCK_TCPIP_CORE();
00263   while (1) {                          /* MAIN Loop */
00264     sys_mbox_fetch(mbox, (void *)&msg);
00265     switch (msg->type) {
00266 #if LWIP_NETCONN
00267     case TCPIP_MSG_API:
00268       LWIP_DEBUGF(TCPIP_DEBUG, ("tcpip_thread: API message %p\n", (void *)msg));
00269       msg->msg.apimsg->function(&(msg->msg.apimsg->msg));
00270       break;
00271 #endif /* LWIP_NETCONN */
00272 
00273     case TCPIP_MSG_INPKT:
00274       LWIP_DEBUGF(TCPIP_DEBUG, ("tcpip_thread: PACKET %p\n", (void *)msg));
00275 #if LWIP_ARP
00276       if (msg->msg.inp.netif->flags & NETIF_FLAG_ETHARP) {
00277         ethernet_input(msg->msg.inp.p, msg->msg.inp.netif);
00278       } else
00279 #endif /* LWIP_ARP */
00280       { ip_input(msg->msg.inp.p, msg->msg.inp.netif);
00281       }
00282       memp_free(MEMP_TCPIP_MSG_INPKT, msg);
00283       break;
00284 
00285 #if LWIP_NETIF_API
00286     case TCPIP_MSG_NETIFAPI:
00287       LWIP_DEBUGF(TCPIP_DEBUG, ("tcpip_thread: Netif API message %p\n", (void *)msg));
00288       msg->msg.netifapimsg->function(&(msg->msg.netifapimsg->msg));
00289       break;
00290 #endif /* LWIP_NETIF_API */
00291 
00292     case TCPIP_MSG_CALLBACK:
00293       LWIP_DEBUGF(TCPIP_DEBUG, ("tcpip_thread: CALLBACK %p\n", (void *)msg));
00294       msg->msg.cb.f(msg->msg.cb.ctx);
00295       memp_free(MEMP_TCPIP_MSG_API, msg);
00296       break;
00297 
00298     case TCPIP_MSG_TIMEOUT:
00299       LWIP_DEBUGF(TCPIP_DEBUG, ("tcpip_thread: TIMEOUT %p\n", (void *)msg));
00300       sys_timeout(msg->msg.tmo.msecs, msg->msg.tmo.h, msg->msg.tmo.arg);
00301       memp_free(MEMP_TCPIP_MSG_API, msg);
00302       break;
00303     case TCPIP_MSG_UNTIMEOUT:
00304       LWIP_DEBUGF(TCPIP_DEBUG, ("tcpip_thread: UNTIMEOUT %p\n", (void *)msg));
00305       sys_untimeout(msg->msg.tmo.h, msg->msg.tmo.arg);
00306       memp_free(MEMP_TCPIP_MSG_API, msg);
00307       break;
00308 
00309     default:
00310       break;
00311     }
00312   }
00313 }
00314 
00315 /**
00316  * Pass a received packet to tcpip_thread for input processing
00317  *
00318  * @param p the received packet, p->payload pointing to the Ethernet header or
00319  *          to an IP header (if netif doesn't got NETIF_FLAG_ETHARP flag)
00320  * @param inp the network interface on which the packet was received
00321  */
00322 err_t
00323 tcpip_input(struct pbuf *p, struct netif *inp)
00324 {
00325   struct tcpip_msg *msg;
00326 
00327   if (mbox != SYS_MBOX_NULL) {
00328     msg = memp_malloc(MEMP_TCPIP_MSG_INPKT);
00329     if (msg == NULL) {
00330       return ERR_MEM;
00331     }
00332 
00333     msg->type = TCPIP_MSG_INPKT;
00334     msg->msg.inp.p = p;
00335     msg->msg.inp.netif = inp;
00336     if (sys_mbox_trypost(mbox, msg) != ERR_OK) {
00337       memp_free(MEMP_TCPIP_MSG_INPKT, msg);
00338       return ERR_MEM;
00339     }
00340     return ERR_OK;
00341   }
00342   return ERR_VAL;
00343 }
00344 
00345 /**
00346  * Call a specific function in the thread context of
00347  * tcpip_thread for easy access synchronization.
00348  * A function called in that way may access lwIP core code
00349  * without fearing concurrent access.
00350  *
00351  * @param f the function to call
00352  * @param ctx parameter passed to f
00353  * @param block 1 to block until the request is posted, 0 to non-blocking mode
00354  * @return ERR_OK if the function was called, another err_t if not
00355  */
00356 err_t
00357 tcpip_callback_with_block(void (*f)(void *ctx), void *ctx, u8_t block)
00358 {
00359   struct tcpip_msg *msg;
00360 
00361   if (mbox != SYS_MBOX_NULL) {
00362     msg = memp_malloc(MEMP_TCPIP_MSG_API);
00363     if (msg == NULL) {
00364       return ERR_MEM;
00365     }
00366 
00367     msg->type = TCPIP_MSG_CALLBACK;
00368     msg->msg.cb.f = f;
00369     msg->msg.cb.ctx = ctx;
00370     if (block) {
00371       sys_mbox_post(mbox, msg);
00372     } else {
00373       if (sys_mbox_trypost(mbox, msg) != ERR_OK) {
00374         memp_free(MEMP_TCPIP_MSG_API, msg);
00375         return ERR_MEM;
00376       }
00377     }
00378     return ERR_OK;
00379   }
00380   return ERR_VAL;
00381 }
00382 
00383 /**
00384  * call sys_timeout in tcpip_thread
00385  *
00386  * @param msec time in miliseconds for timeout
00387  * @param h function to be called on timeout
00388  * @param arg argument to pass to timeout function h
00389  * @return ERR_MEM on memory error, ERR_OK otherwise
00390  */
00391 err_t
00392 tcpip_timeout(u32_t msecs, sys_timeout_handler h, void *arg)
00393 {
00394   struct tcpip_msg *msg;
00395 
00396   if (mbox != SYS_MBOX_NULL) {
00397     msg = memp_malloc(MEMP_TCPIP_MSG_API);
00398     if (msg == NULL) {
00399       return ERR_MEM;
00400     }
00401 
00402     msg->type = TCPIP_MSG_TIMEOUT;
00403     msg->msg.tmo.msecs = msecs;
00404     msg->msg.tmo.h = h;
00405     msg->msg.tmo.arg = arg;
00406     sys_mbox_post(mbox, msg);
00407     return ERR_OK;
00408   }
00409   return ERR_VAL;
00410 }
00411 
00412 /**
00413  * call sys_untimeout in tcpip_thread
00414  *
00415  * @param msec time in miliseconds for timeout
00416  * @param h function to be called on timeout
00417  * @param arg argument to pass to timeout function h
00418  * @return ERR_MEM on memory error, ERR_OK otherwise
00419  */
00420 err_t
00421 tcpip_untimeout(sys_timeout_handler h, void *arg)
00422 {
00423   struct tcpip_msg *msg;
00424 
00425   if (mbox != SYS_MBOX_NULL) {
00426     msg = memp_malloc(MEMP_TCPIP_MSG_API);
00427     if (msg == NULL) {
00428       return ERR_MEM;
00429     }
00430 
00431     msg->type = TCPIP_MSG_UNTIMEOUT;
00432     msg->msg.tmo.h = h;
00433     msg->msg.tmo.arg = arg;
00434     sys_mbox_post(mbox, msg);
00435     return ERR_OK;
00436   }
00437   return ERR_VAL;
00438 }
00439 
00440 #if LWIP_NETCONN
00441 /**
00442  * Call the lower part of a netconn_* function
00443  * This function is then running in the thread context
00444  * of tcpip_thread and has exclusive access to lwIP core code.
00445  *
00446  * @param apimsg a struct containing the function to call and its parameters
00447  * @return ERR_OK if the function was called, another err_t if not
00448  */
00449 err_t
00450 tcpip_apimsg(struct api_msg *apimsg)
00451 {
00452   struct tcpip_msg msg;
00453   
00454   if (mbox != SYS_MBOX_NULL) {
00455     msg.type = TCPIP_MSG_API;
00456     msg.msg.apimsg = apimsg;
00457     sys_mbox_post(mbox, &msg);
00458     sys_arch_sem_wait(apimsg->msg.conn->op_completed, 0);
00459     return ERR_OK;
00460   }
00461   return ERR_VAL;
00462 }
00463 
00464 #if LWIP_TCPIP_CORE_LOCKING
00465 /**
00466  * Call the lower part of a netconn_* function
00467  * This function has exclusive access to lwIP core code by locking it
00468  * before the function is called.
00469  *
00470  * @param apimsg a struct containing the function to call and its parameters
00471  * @return ERR_OK (only for compatibility fo tcpip_apimsg())
00472  */
00473 err_t
00474 tcpip_apimsg_lock(struct api_msg *apimsg)
00475 {
00476   LOCK_TCPIP_CORE();
00477   apimsg->function(&(apimsg->msg));
00478   UNLOCK_TCPIP_CORE();
00479   return ERR_OK;
00480 
00481 }
00482 #endif /* LWIP_TCPIP_CORE_LOCKING */
00483 #endif /* LWIP_NETCONN */
00484 
00485 #if LWIP_NETIF_API
00486 #if !LWIP_TCPIP_CORE_LOCKING
00487 /**
00488  * Much like tcpip_apimsg, but calls the lower part of a netifapi_*
00489  * function.
00490  *
00491  * @param netifapimsg a struct containing the function to call and its parameters
00492  * @return error code given back by the function that was called
00493  */
00494 err_t
00495 tcpip_netifapi(struct netifapi_msg* netifapimsg)
00496 {
00497   struct tcpip_msg msg;
00498   
00499   if (mbox != SYS_MBOX_NULL) {
00500     netifapimsg->msg.sem = sys_sem_new(0);
00501     if (netifapimsg->msg.sem == SYS_SEM_NULL) {
00502       netifapimsg->msg.err = ERR_MEM;
00503       return netifapimsg->msg.err;
00504     }
00505     
00506     msg.type = TCPIP_MSG_NETIFAPI;
00507     msg.msg.netifapimsg = netifapimsg;
00508     sys_mbox_post(mbox, &msg);
00509     sys_sem_wait(netifapimsg->msg.sem);
00510     sys_sem_free(netifapimsg->msg.sem);
00511     return netifapimsg->msg.err;
00512   }
00513   return ERR_VAL;
00514 }
00515 #else /* !LWIP_TCPIP_CORE_LOCKING */
00516 /**
00517  * Call the lower part of a netifapi_* function
00518  * This function has exclusive access to lwIP core code by locking it
00519  * before the function is called.
00520  *
00521  * @param netifapimsg a struct containing the function to call and its parameters
00522  * @return ERR_OK (only for compatibility fo tcpip_netifapi())
00523  */
00524 err_t
00525 tcpip_netifapi_lock(struct netifapi_msg* netifapimsg)
00526 {
00527   LOCK_TCPIP_CORE();  
00528   netifapimsg->function(&(netifapimsg->msg));
00529   UNLOCK_TCPIP_CORE();
00530   return netifapimsg->msg.err;
00531 }
00532 #endif /* !LWIP_TCPIP_CORE_LOCKING */
00533 #endif /* LWIP_NETIF_API */
00534 
00535 /**
00536  * Initialize this module:
00537  * - initialize all sub modules
00538  * - start the tcpip_thread
00539  *
00540  * @param initfunc a function to call when tcpip_thread is running and finished initializing
00541  * @param arg argument to pass to initfunc
00542  */
00543 void
00544 tcpip_init(void (* initfunc)(void *), void *arg)
00545 {
00546   lwip_init();
00547 
00548   tcpip_init_done = initfunc;
00549   tcpip_init_done_arg = arg;
00550   mbox = sys_mbox_new(TCPIP_MBOX_SIZE);
00551 #if LWIP_TCPIP_CORE_LOCKING
00552   lock_tcpip_core = sys_sem_new(1);
00553 #endif /* LWIP_TCPIP_CORE_LOCKING */
00554 
00555   sys_thread_new(TCPIP_THREAD_NAME, tcpip_thread, NULL, TCPIP_THREAD_STACKSIZE, TCPIP_THREAD_PRIO);
00556 }
00557 
00558 /**
00559  * Simple callback function used with tcpip_callback to free a pbuf
00560  * (pbuf_free has a wrong signature for tcpip_callback)
00561  *
00562  * @param p The pbuf (chain) to be dereferenced.
00563  */
00564 static void
00565 pbuf_free_int(void *p)
00566 {
00567   struct pbuf *q = p;
00568   pbuf_free(q);
00569 }
00570 
00571 /**
00572  * A simple wrapper function that allows you to free a pbuf from interrupt context.
00573  *
00574  * @param p The pbuf (chain) to be dereferenced.
00575  * @return ERR_OK if callback could be enqueued, an err_t if not
00576  */
00577 err_t
00578 pbuf_free_callback(struct pbuf *p)
00579 {
00580   return tcpip_callback_with_block(pbuf_free_int, p, 0);
00581 }
00582 
00583 /**
00584  * A simple wrapper function that allows you to free heap memory from
00585  * interrupt context.
00586  *
00587  * @param m the heap memory to free
00588  * @return ERR_OK if callback could be enqueued, an err_t if not
00589  */
00590 err_t
00591 mem_free_callback(void *m)
00592 {
00593   return tcpip_callback_with_block(mem_free, m, 0);
00594 }
00595 
00596 #endif /* !NO_SYS */
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines