]> rtime.felk.cvut.cz Git - pes-rpp/rpp-lwip.git/blob - src/api/tcpip.c
Add LWIP_TCPIP_CORE_LOCKING option (0 as default value) to experiment "locking" as...
[pes-rpp/rpp-lwip.git] / src / api / tcpip.c
1 /*
2  * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without modification,
6  * are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice,
9  *    this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright notice,
11  *    this list of conditions and the following disclaimer in the documentation
12  *    and/or other materials provided with the distribution.
13  * 3. The name of the author may not be used to endorse or promote products
14  *    derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
19  * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
21  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
24  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
25  * OF SUCH DAMAGE.
26  *
27  * This file is part of the lwIP TCP/IP stack.
28  *
29  * Author: Adam Dunkels <adam@sics.se>
30  *
31  */
32
33 #include "lwip/opt.h"
34
35 #include "lwip/sys.h"
36
37 #include "lwip/memp.h"
38 #include "lwip/pbuf.h"
39
40 #include "netif/etharp.h"
41
42 #include "lwip/ip.h"
43 #include "lwip/ip_frag.h"
44 #include "lwip/udp.h"
45 #include "lwip/tcp.h"
46
47 #include "lwip/tcpip.h"
48 #include "lwip/igmp.h"
49
50 #if !NO_SYS
51
52 static void (* tcpip_init_done)(void *arg) = NULL;
53 static void *tcpip_init_done_arg           = NULL;
54 static sys_mbox_t mbox                     = SYS_MBOX_NULL;
55
56 #if LWIP_TCPIP_CORE_LOCKING
57 /** The global semaphore to lock the stack. */
58 sys_sem_t lock_tcpip_core = 0;
59 #endif /* LWIP_TCPIP_CORE_LOCKING */
60
61 #if LWIP_TCP
62 static int tcpip_tcp_timer_active = 0;
63
64 static void
65 tcpip_tcp_timer(void *arg)
66 {
67   LWIP_UNUSED_ARG(arg);
68
69   /* call TCP timer handler */
70   tcp_tmr();
71   /* timer still needed? */
72   if (tcp_active_pcbs || tcp_tw_pcbs) {
73     /* restart timer */
74     sys_timeout( TCP_TMR_INTERVAL, tcpip_tcp_timer, NULL);
75   } else {
76     /* disable timer */
77     tcpip_tcp_timer_active = 0;
78   }
79 }
80
81 #if !NO_SYS
82 void
83 tcp_timer_needed(void)
84 {
85   /* timer is off but needed again? */
86   if (!tcpip_tcp_timer_active && (tcp_active_pcbs || tcp_tw_pcbs)) {
87     /* enable and start timer */
88     tcpip_tcp_timer_active = 1;
89     sys_timeout( TCP_TMR_INTERVAL, tcpip_tcp_timer, NULL);
90   }
91 }
92 #endif /* !NO_SYS */
93 #endif /* LWIP_TCP */
94
95 #if IP_REASSEMBLY
96 static void
97 ip_timer(void *arg)
98 {
99   LWIP_UNUSED_ARG(arg);
100   LWIP_DEBUGF(TCPIP_DEBUG, ("tcpip: ip_reass_tmr()\n"));
101   ip_reass_tmr();
102   sys_timeout( IP_TMR_INTERVAL, ip_timer, NULL);
103 }
104 #endif /* IP_REASSEMBLY */
105
106 #if LWIP_ARP
107 static void
108 arp_timer(void *arg)
109 {
110   LWIP_UNUSED_ARG(arg);
111   LWIP_DEBUGF(TCPIP_DEBUG, ("tcpip: etharp_tmr()\n"));
112   etharp_tmr();
113   sys_timeout( ARP_TMR_INTERVAL, arp_timer, NULL);
114 }
115 #endif /* LWIP_ARP */
116
117 #if LWIP_DHCP
118 static void
119 dhcp_timer_coarse(void *arg)
120 {
121   LWIP_UNUSED_ARG(arg);
122   LWIP_DEBUGF(TCPIP_DEBUG, ("tcpip: dhcp_coarse_tmr()\n"));
123   dhcp_coarse_tmr();
124   sys_timeout(DHCP_COARSE_TIMER_SECS*1000, dhcp_timer_coarse, NULL);
125 }
126
127 static void
128 dhcp_timer_fine(void *arg)
129 {
130   LWIP_UNUSED_ARG(arg);
131   LWIP_DEBUGF(TCPIP_DEBUG, ("tcpip: dhcp_fine_tmr()\n"));
132   dhcp_fine_tmr();
133   sys_timeout(DHCP_FINE_TIMER_MSECS, dhcp_timer_fine, NULL);
134 }
135 #endif /* LWIP_DHCP */
136
137 #if LWIP_IGMP
138 static void
139 igmp_timer(void *arg)
140 {
141   LWIP_UNUSED_ARG(arg);
142   LWIP_DEBUGF(TCPIP_DEBUG, ("tcpip: igmp_tmr()\n"));
143   igmp_tmr();
144   sys_timeout( IGMP_TMR_INTERVAL, igmp_timer, NULL);
145 }
146 #endif /* LWIP_IGMP */
147
148 #if ETHARP_TCPIP_ETHINPUT
149 static void
150 ethernet_input(struct pbuf *p, struct netif *netif)
151 {
152   struct eth_hdr* ethhdr;
153
154   /* points to packet payload, which starts with an Ethernet header */
155   ethhdr = p->payload;
156   
157   switch (htons(ethhdr->type)) {
158     /* IP packet? */
159     case ETHTYPE_IP:
160 #if ETHARP_TRUST_IP_MAC
161       /* update ARP table */
162       etharp_ip_input( netif, p);
163 #endif
164       /* skip Ethernet header */
165       if(pbuf_header(p, -(s16_t)sizeof(struct eth_hdr))) {
166         LWIP_ASSERT("Can't move over header in packet", 0);
167         pbuf_free(p);
168         p = NULL;
169       }
170       else
171         /* pass to IP layer */
172         ip_input(p, netif);
173       break;
174       
175     case ETHTYPE_ARP:
176       /* pass p to ARP module  */
177       etharp_arp_input(netif, (struct eth_addr*)(netif->hwaddr), p);
178       break;
179
180     default:
181       pbuf_free(p);
182       p = NULL;
183       break;
184   }
185 }
186 #endif /* ETHARP_TCPIP_ETHINPUT */
187
188 static void
189 tcpip_thread(void *arg)
190 {
191   struct tcpip_msg *msg;
192   LWIP_UNUSED_ARG(arg);
193
194 #if IP_REASSEMBLY
195   sys_timeout( IP_TMR_INTERVAL, ip_timer, NULL);
196 #endif /* IP_REASSEMBLY */
197 #if LWIP_ARP
198   sys_timeout( ARP_TMR_INTERVAL, arp_timer, NULL);
199 #endif /* LWIP_ARP */
200 #if LWIP_DHCP
201   sys_timeout( DHCP_COARSE_TIMER_SECS*1000, dhcp_timer_coarse, NULL);
202   sys_timeout( DHCP_FINE_TIMER_MSECS, dhcp_timer_fine, NULL);
203 #endif /* LWIP_DHCP */
204
205   if (tcpip_init_done != NULL) {
206     tcpip_init_done(tcpip_init_done_arg);
207   }
208
209 #if LWIP_IGMP
210   igmp_init();
211   sys_timeout( IGMP_TMR_INTERVAL, igmp_timer, NULL);
212 #endif /* LWIP_IGMP */
213
214   LOCK_TCPIP_CORE();
215   while (1) {                          /* MAIN Loop */
216     sys_mbox_fetch(mbox, (void *)&msg);
217     switch (msg->type) {
218     case TCPIP_MSG_API:
219       LWIP_DEBUGF(TCPIP_DEBUG, ("tcpip_thread: API message %p\n", (void *)msg));
220       msg->msg.apimsg->function(&(msg->msg.apimsg->msg));
221       break;
222
223 #if ETHARP_TCPIP_INPUT      
224     case TCPIP_MSG_INPUT:
225       LWIP_DEBUGF(TCPIP_DEBUG, ("tcpip_thread: IP packet %p\n", (void *)msg));
226       ip_input(msg->msg.inp.p, msg->msg.inp.netif);
227       memp_free(MEMP_TCPIP_MSG, msg);
228       break;
229 #endif /* ETHARP_TCPIP_INPUT */
230
231 #if ETHARP_TCPIP_ETHINPUT
232     case TCPIP_MSG_ETHINPUT:
233       LWIP_DEBUGF(TCPIP_DEBUG, ("tcpip_thread: Ethernet packet %p\n", (void *)msg));
234       ethernet_input(msg->msg.inp.p, msg->msg.inp.netif);
235       memp_free(MEMP_TCPIP_MSG, msg);
236       break;
237 #endif /* ETHARP_TCPIP_ETHINPUT */
238
239 #if LWIP_NETIF_API
240     case TCPIP_MSG_NETIFAPI:
241       LWIP_DEBUGF(TCPIP_DEBUG, ("tcpip_thread: Netif API message %p\n", (void *)msg));
242       netifapi_msg_input(msg->msg.netifapimsg);
243       break;
244 #endif /* LWIP_NETIF_API */
245
246     case TCPIP_MSG_CALLBACK:
247       LWIP_DEBUGF(TCPIP_DEBUG, ("tcpip_thread: CALLBACK %p\n", (void *)msg));
248       msg->msg.cb.f(msg->msg.cb.ctx);
249       memp_free(MEMP_TCPIP_MSG, msg);
250       break;
251     default:
252       break;
253     }
254   }
255 }
256
257 #if ETHARP_TCPIP_INPUT
258 err_t
259 tcpip_input(struct pbuf *p, struct netif *inp)
260 {
261   struct tcpip_msg *msg;
262
263   if (mbox != SYS_MBOX_NULL) {
264     msg = memp_malloc(MEMP_TCPIP_MSG);
265     if (msg == NULL) {
266       return ERR_MEM;
267     }
268
269     msg->type = TCPIP_MSG_INPUT;
270     msg->msg.inp.p = p;
271     msg->msg.inp.netif = inp;
272     sys_mbox_post(mbox, msg);
273     return ERR_OK;
274   }
275   return ERR_VAL;
276 }
277 #endif /* ETHARP_TCPIP_INPUT */
278
279 #if ETHARP_TCPIP_ETHINPUT
280 err_t
281 tcpip_ethinput(struct pbuf *p, struct netif *inp)
282 {
283   struct tcpip_msg *msg;
284
285   if (mbox != SYS_MBOX_NULL) {
286     msg = memp_malloc(MEMP_TCPIP_MSG);
287     if (msg == NULL) {
288       return ERR_MEM;
289     }
290     
291     msg->type = TCPIP_MSG_ETHINPUT;
292     msg->msg.inp.p = p;
293     msg->msg.inp.netif = inp;
294     sys_mbox_post(mbox, msg);
295     return ERR_OK;
296   }
297   return ERR_VAL;
298 }
299 #endif /* ETHARP_TCPIP_ETHINPUT */
300
301 err_t
302 tcpip_callback(void (*f)(void *ctx), void *ctx)
303 {
304   struct tcpip_msg *msg;
305
306   if (mbox != SYS_MBOX_NULL) {
307     msg = memp_malloc(MEMP_TCPIP_MSG);
308     if (msg == NULL) {
309       return ERR_MEM;
310     }
311
312     msg->type = TCPIP_MSG_CALLBACK;
313     msg->msg.cb.f = f;
314     msg->msg.cb.ctx = ctx;
315     sys_mbox_post(mbox, msg);
316     return ERR_OK;
317   }
318   return ERR_VAL;
319 }
320
321 err_t
322 tcpip_apimsg(struct api_msg *apimsg)
323 {
324   struct tcpip_msg msg;
325   
326   if (mbox != SYS_MBOX_NULL) {
327     msg.type = TCPIP_MSG_API;
328     msg.msg.apimsg = apimsg;
329     sys_mbox_post(mbox, &msg);
330     sys_arch_mbox_fetch(apimsg->msg.conn->mbox, NULL, 0);
331     return ERR_OK;
332   }
333   return ERR_VAL;
334 }
335
336 #if LWIP_TCPIP_CORE_LOCKING
337 err_t
338 tcpip_apimsg_lock(struct api_msg *apimsg)
339 {
340   LOCK_TCPIP_CORE();
341   apimsg->function(&(apimsg->msg));
342   UNLOCK_TCPIP_CORE();
343   return ERR_OK;
344
345 }
346 #endif /* LWIP_TCPIP_CORE_LOCKING */
347
348 #if LWIP_NETIF_API
349 err_t tcpip_netifapi(struct netifapi_msg* netifapimsg)
350 {
351   struct tcpip_msg msg;
352   
353   if (mbox != SYS_MBOX_NULL) {
354     netifapimsg->sem = sys_sem_new(0);
355     if (netifapimsg->sem == SYS_SEM_NULL) {
356       netifapimsg->err = ERR_MEM;
357       return netifapimsg->err;
358     }
359     
360     msg.type = TCPIP_MSG_NETIFAPI;
361     msg.msg.netifapimsg = netifapimsg;
362     sys_mbox_post(mbox, &msg);
363     sys_sem_wait(netifapimsg->sem);
364     sys_sem_free(netifapimsg->sem);
365     return netifapimsg->err;
366   }
367   return ERR_VAL;
368 }
369 #endif /* LWIP_NETIF_API */
370
371 void
372 tcpip_init(void (* initfunc)(void *), void *arg)
373 {
374 #if LWIP_ARP
375   etharp_init();
376 #endif /*LWIP_ARP */
377   ip_init();
378 #if LWIP_UDP
379   udp_init();
380 #endif /* LWIP_UDP */
381 #if LWIP_TCP
382   tcp_init();
383 #endif /* LWIP_TCP */
384
385   tcpip_init_done = initfunc;
386   tcpip_init_done_arg = arg;
387   mbox = sys_mbox_new();
388 #if LWIP_TCPIP_CORE_LOCKING
389   lock_tcpip_core = sys_sem_new(1);
390 #endif /* LWIP_TCPIP_CORE_LOCKING */
391
392   sys_thread_new(tcpip_thread, NULL, TCPIP_THREAD_PRIO);
393 }
394
395 #endif /* !NO_SYS */