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