]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/ankh/lib/lwip/lib/arch/ankh_if.c
update
[l4.git] / l4 / pkg / ankh / lib / lwip / lib / arch / ankh_if.c
1 /**
2  * @file
3  * Ankh Interface driver
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 /*
40  * This file is a skeleton for developing Ethernet network interface
41  * drivers for lwIP. Add code to the low_level functions and do a
42  * search-and-replace for the word "ankhif" to replace it with
43  * something that better describes your network interface.
44  */
45 #include "lwip/opt.h"
46
47 #include "lwip/def.h"
48 #include "lwip/mem.h"
49 #include "lwip/pbuf.h"
50 #include "lwip/sys.h"
51 #include "lwip/dhcp.h"
52 #include <lwip/stats.h>
53 #include <lwip/snmp.h>
54 #include "netif/etharp.h"
55 #include "netif/ppp_oe.h"
56
57 #include <l4/ankh/client-c.h>
58 #include <l4/ankh/lwip-ankh.h>
59 #include <l4/sys/kdebug.h>
60 #include <l4/util/util.h>
61 #include <assert.h>
62 #include <stdlib.h>
63
64
65 /* Define those to better describe your network interface. */
66 #define IFNAME0 'a'
67 #define IFNAME1 'n'
68
69 /**
70  * Helper struct to hold private data used to operate your ethernet interface.
71  * Keeping the ethernet address of the MAC in this struct is not necessary
72  * as it is already kept in the struct netif.
73  * But this is only an example, anyway...
74  */
75 struct ankhif {
76   struct eth_addr *ethaddr;
77   /* Add whatever per-interface state that is needed here. */
78   ankh_config_info *config;
79   pthread_t         recv_thread;
80 };
81
82 /* Forward declarations. */
83 static void  ankhif_input(struct netif *netif);
84
85 static void *ankhif_recv_fn(void *arg)
86 {
87     int err;
88     struct netif *netif = (struct netif*)arg;
89     l4shmc_ringbuf_t *rb = l4ankh_get_recvbuf();
90
91     struct ankhif *ankhif = netif->state;
92     ankhif->config->recv_thread = pthread_getl4cap(pthread_self());
93     err = l4ankh_prepare_recv(ankhif->config->recv_thread);
94
95     printf("ANKHIF::Recv rb @ %p\n", rb);
96
97     for (;;)
98     {
99         err = l4shmc_rb_receiver_wait_for_data(rb, 0);
100         if (!err)
101             ankhif_input(netif);
102         else
103             l4_thread_yield();
104     }
105
106     enter_kdebug("exit loop?");
107     return NULL;
108 }
109
110 /**
111  * In this function, the hardware should be initialized.
112  * Called from ankhif_init().
113  *
114  * @param netif the already initialized lwip network interface structure
115  *        for this ankhif
116  */
117 static void
118 low_level_init(struct netif *netif)
119 {
120   struct ankhif *ankhif = netif->state;
121   short i;
122
123   int err = l4ankh_open(ankhif->config->shm_name, ankhif->config->bufsize);
124   assert(!err);
125
126   err = l4ankh_prepare_send(ankhif->config->send_thread);
127   assert(!err);
128
129   /* set MAC hardware address length */
130   netif->hwaddr_len = ETHARP_HWADDR_LEN;
131
132   /* set MAC hardware address */
133   memcpy(netif->hwaddr, l4ankh_get_info()->mac, ETHARP_HWADDR_LEN);
134   printf("--> ");
135   for (i=0; i < ETHARP_HWADDR_LEN; ++i) {
136       printf("%02X", netif->hwaddr[i]);
137       if (i < 5) printf(":");
138   }
139   printf(" <--\n");
140
141   /* maximum transfer unit */
142   netif->mtu = 1500;
143
144   /* device capabilities */
145   /* don't set NETIF_FLAG_ETHARP if this device is not an ethernet one */
146   netif->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_LINK_UP;
147
148   /* Do whatever else is needed to initialize interface. */
149 }
150
151 /**
152  * This function should do the actual transmission of the packet. The packet is
153  * contained in the pbuf that is passed to the function. This pbuf
154  * might be chained.
155  *
156  * @param netif the lwip network interface structure for this ankhif
157  * @param p the MAC packet to send (e.g. IP packet including MAC addresses and type)
158  * @return ERR_OK if the packet could be sent
159  *         an err_t value if the packet couldn't be sent
160  *
161  * @note Returning ERR_MEM here if a DMA queue of your MAC is full can lead to
162  *       strange results. You might consider waiting for space in the DMA queue
163  *       to become availale since the stack doesn't retry to send a packet
164  *       dropped because of memory failure (except for the TCP timers).
165  */
166
167 static err_t
168 low_level_output(struct netif *netif, struct pbuf *p)
169 {
170   struct ankhif *ankhif __attribute__((unused)) = netif->state;
171   struct pbuf *q = NULL;
172
173 // XXX  initiate transfer();
174
175 #if ETH_PAD_SIZE
176   pbuf_header(p, -ETH_PAD_SIZE); /* drop the padding word */
177 #endif
178
179 #if 0
180   if (p) {
181     printf("%s, %p %d ... ", __func__, p->payload, p->len);
182     __hexdump(p->payload, p->len < 12 ? p->len : 12);
183   }
184   else {
185     printf("send with NULL workload?\n");
186     enter_kdebug();
187   }
188 #endif
189
190   for(q = p; q != NULL; q = q->next) {
191     /* Send the data from the pbuf to the interface, one pbuf at a
192        time. The size of the data in each pbuf is kept in the ->len
193        variable. */
194       int x = l4ankh_send(q->payload, q->len, 1);
195       if (x)
196           printf("  ankh_send: %d\n", x);
197   }
198
199 #if ETH_PAD_SIZE
200   pbuf_header(p, ETH_PAD_SIZE); /* reclaim the padding word */
201 #endif
202
203   LINK_STATS_INC(link.xmit);
204
205   return ERR_OK;
206 }
207
208 /**
209  * Should allocate a pbuf and transfer the bytes of the incoming
210  * packet from the interface into the pbuf.
211  *
212  * @param netif the lwip network interface structure for this ankhif
213  * @return a pbuf filled with the received packet (including MAC header)
214  *         NULL on memory error
215  */
216 static struct pbuf *
217 low_level_input(struct netif *netif)
218 {
219   struct ankhif *ankhif __attribute__((unused)) = netif->state;
220   struct pbuf *p;
221   int len;
222   l4shmc_ringbuf_t *rb = l4ankh_get_recvbuf();
223
224   /* Obtain the size of the packet and put it into the "len"
225      variable. */
226   len = l4shmc_rb_receiver_read_next_size(HEAD(rb));
227   if (len <= 0)
228       return NULL;
229
230 #if ETH_PAD_SIZE
231   len += ETH_PAD_SIZE; /* allow room for Ethernet padding */
232 #endif
233
234   /* we allocate a buffer for the whole packet */
235   p = pbuf_alloc(PBUF_RAW, len, PBUF_RAM);
236
237   if (p != NULL) {
238
239 #if ETH_PAD_SIZE
240     pbuf_header(p, -ETH_PAD_SIZE); /* drop the padding word */
241 #endif
242
243   /* Read enough bytes to fill this pbuf in the chain. The
244    * available data in the pbuf is given by the p->len
245    * variable. */
246     assert(p->len == len);
247     int err = l4shmc_rb_receiver_copy_out(HEAD(rb), p->payload, (unsigned*)&p->len);
248 #if 0
249     printf("Incoming %p: ", p); __hexdump(p->payload, 12);
250 #endif
251     if (err) { /* Should not happen! */
252       printf("ERROR: l4shmc_rb_copy_out %d\n", err);
253       printf("p @ %p, len %d\n", p, p->len);
254       enter_kdebug();
255       return NULL;
256     }
257
258 #if ETH_PAD_SIZE
259       pbuf_header(p, ETH_PAD_SIZE); /* reclaim the padding word */
260 #endif
261
262       LINK_STATS_INC(link.recv);
263   } else {
264     // XXX drop packet();
265     LINK_STATS_INC(link.memerr);
266     LINK_STATS_INC(link.drop);
267   }
268
269   return p;
270 }
271
272 static inline char *type_to_string(unsigned type)
273 {
274     switch(type) {
275         case ETHTYPE_IP:
276             return "IP";
277         case ETHTYPE_ARP:
278             return "ARP";
279         case ETHTYPE_PPPOE:
280             return "PPPoE";
281     }
282     return "unknown";
283 }
284
285
286 /**
287  * This function should be called when a packet is ready to be read
288  * from the interface. It uses the function low_level_input() that
289  * should handle the actual reception of bytes from the network
290  * interface. Then the type of the received packet is determined and
291  * the appropriate input function is called.
292  *
293  * @param netif the lwip network interface structure for this ankhif
294  */
295 static void
296 ankhif_input(struct netif *netif)
297 {
298   struct ankhif *ankhif;
299   struct eth_hdr *ethhdr;
300   struct pbuf *p;
301
302   ankhif = netif->state;
303
304   /* move received packet into a new pbuf */
305   p = low_level_input(netif);
306   /* no packet could be read, silently ignore this */
307   if (p == NULL) return;
308   /* points to packet payload, which starts with an Ethernet header */
309   ethhdr = p->payload;
310
311   switch (htons(ethhdr->type)) {
312   /* IP or ARP packet? */
313   case ETHTYPE_IP:
314   case ETHTYPE_ARP:
315 #if PPPOE_SUPPORT
316   /* PPPoE packet? */
317   case ETHTYPE_PPPOEDISC:
318   case ETHTYPE_PPPOE:
319 #endif /* PPPOE_SUPPORT */
320     /* full packet send to tcpip_thread to process */
321     if (netif->input(p, netif)!=ERR_OK)
322      { LWIP_DEBUGF(NETIF_DEBUG, ("ankhif_input: IP input error\n"));
323        pbuf_free(p);
324        p = NULL;
325      }
326     break;
327
328   default:
329     pbuf_free(p);
330     p = NULL;
331     break;
332   }
333 }
334
335 /**
336  * Should be called at the beginning of the program to set up the
337  * network interface. It calls the function low_level_init() to do the
338  * actual setup of the hardware.
339  *
340  * This function should be passed as a parameter to netif_add().
341  *
342  * @param netif the lwip network interface structure for this ankhif
343  * @return ERR_OK if the loopif is initialized
344  *         ERR_MEM if private data couldn't be allocated
345  *         any other err_t on error
346  */
347 err_t ankhif_init(struct netif *netif);
348 err_t ankhif_init(struct netif *netif)
349 {
350   struct ankhif *ankhif;
351
352   LWIP_ASSERT("netif != NULL", (netif != NULL));
353
354   ankhif = mem_malloc(sizeof(struct ankhif));
355   if (ankhif == NULL) {
356     LWIP_DEBUGF(NETIF_DEBUG, ("ankhif_init: out of memory\n"));
357     return ERR_MEM;
358   }
359   ankhif->config = netif->state;
360
361 #if LWIP_NETIF_HOSTNAME
362   /* Initialize interface hostname */
363   netif->hostname = "lwip";
364 #endif /* LWIP_NETIF_HOSTNAME */
365
366   /*
367    * Initialize the snmp variables and counters inside the struct netif.
368    * The last argument should be replaced with your link speed, in units
369    * of bits per second.
370    */
371   NETIF_INIT_SNMP(netif, snmp_ifType_ethernet_csmacd, 100000000);
372
373   netif->state = ankhif;
374   netif->name[0] = IFNAME0;
375   netif->name[1] = IFNAME1;
376   /* We directly use etharp_output() here to save a function call.
377    * You can instead declare your own function an call etharp_output()
378    * from it if you have to do some checks before sending (e.g. if link
379    * is available...) */
380   netif->output = etharp_output;
381   netif->linkoutput = low_level_output;
382
383   ankhif->ethaddr = (struct eth_addr *)&(netif->hwaddr[0]);
384
385   /* initialize the hardware */
386   low_level_init(netif);
387
388   int err = pthread_create(&ankhif->recv_thread, NULL, ankhif_recv_fn, netif);
389   assert(!err);
390
391   return ERR_OK;
392 }
393
394 /******************************
395  * Client initialization code *
396  ******************************/
397 #define DEBUG_L4LWIP_INIT 0
398
399 // default configuration
400 static ankh_config_info cfg = { 2048, L4_INVALID_CAP,
401                                 L4_INVALID_CAP, "" };
402 // default interface
403 static struct netif ankh_netif;
404 static ip_addr_t ipaddr, netmask, gw;
405
406 static int try_l4re_option(int argc, char **argv, unsigned idx)
407 {
408     int remove = 0;
409
410     if (strcmp(argv[idx], "--shm") == 0) {
411         snprintf(cfg.shm_name, CFG_SHM_NAME_SIZE, "%s", argv[idx+1]);
412         remove = 2;
413     }
414     if (strcmp(argv[idx], "--bufsize") == 0) {
415         cfg.bufsize = atoi(argv[idx+1]);
416         remove = 2;
417     }
418
419     memcpy(&argv[idx], &argv[idx+remove], (argc-idx-remove) * sizeof(char*));
420     return remove;
421 }
422
423
424 int l4_lwip_init(int *argc, char **argv)
425 {
426     int i = 0;
427
428 #if DEBUG_L4LWIP_INIT
429     printf("Arguments: %d\n", *argc);
430     printf("Command line: \n");
431 #endif
432
433     while (i < *argc) {
434 #if DEBUG_L4LWIP_INIT
435         printf("  %d %s\n", i, argv[i]);
436 #endif
437         unsigned removed = try_l4re_option(*argc, argv, i);
438         if (removed)
439             *argc -= removed;
440         else
441             ++i;
442     }
443 #if DEBUG_L4LWIP_INIT
444     printf("parsed args. argc is now: %d\n", *argc);
445     printf("Initializing Ankh\n");
446 #endif
447
448     cfg.send_thread =  pthread_getl4cap(pthread_self());
449     l4ankh_init();
450
451     tcpip_init(NULL, NULL);
452
453     netif_add(&ankh_netif, &ipaddr, &netmask, &gw, &cfg,
454               ankhif_init, ethernet_input);
455     netif_set_default(&ankh_netif);
456
457 #if DEBUG_L4LWIP_INIT
458     printf("dhcp start...\n");
459 #endif
460     dhcp_start(&ankh_netif);
461     while(!netif_is_up(&ankh_netif)) {
462         printf("dhcp pending\n");
463         l4_sleep(1000);
464     }
465     printf("IP: "); print_ip(&ankh_netif.ip_addr); printf("\n");
466     printf("MASK: "); print_ip(&ankh_netif.netmask); printf("\n");
467     printf("GW: "); print_ip(&ankh_netif.gw); printf("\n");
468
469     return 0;
470 }