3 * Ankh Interface driver
8 * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
11 * Redistribution and use in source and binary forms, with or without modification,
12 * are permitted provided that the following conditions are met:
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.
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
33 * This file is part of the lwIP TCP/IP stack.
35 * Author: Adam Dunkels <adam@sics.se>
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.
49 #include "lwip/pbuf.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>
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>
66 /* Define those to better describe your network interface. */
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...
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;
83 /* Forward declarations. */
84 static void ankhif_input(struct netif *netif);
86 static void *ankhif_recv_fn(void *arg)
89 struct netif *netif = (struct netif*)arg;
90 l4shmc_ringbuf_t *rb = l4ankh_get_recvbuf();
92 struct ankhif *ankhif = netif->state;
93 ankhif->config->recv_thread = pthread_getl4cap(pthread_self());
94 err = l4ankh_prepare_recv(ankhif->config->recv_thread);
96 printf("ANKHIF::Recv rb @ %p\n", rb);
100 err = l4shmc_rb_receiver_wait_for_data(rb, 1);
107 enter_kdebug("exit loop?");
112 * In this function, the hardware should be initialized.
113 * Called from ankhif_init().
115 * @param netif the already initialized lwip network interface structure
119 low_level_init(struct netif *netif)
121 struct ankhif *ankhif = netif->state;
124 int err = l4ankh_open(ankhif->config->shm_name, ankhif->config->bufsize);
127 err = l4ankh_prepare_send(ankhif->config->send_thread);
130 /* set MAC hardware address length */
131 netif->hwaddr_len = ETHARP_HWADDR_LEN;
133 /* set MAC hardware address */
134 memcpy(netif->hwaddr, l4ankh_get_info()->mac, ETHARP_HWADDR_LEN);
136 for (i=0; i < ETHARP_HWADDR_LEN; ++i) {
137 printf("%02X", netif->hwaddr[i]);
138 if (i < 5) printf(":");
142 /* maximum transfer unit */
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;
149 /* Do whatever else is needed to initialize interface. */
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
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
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).
169 low_level_output(struct netif *netif, struct pbuf *p)
171 struct ankhif *ankhif __attribute__((unused)) = netif->state;
172 struct pbuf *q = NULL;
174 // XXX initiate transfer();
177 pbuf_header(p, -ETH_PAD_SIZE); /* drop the padding word */
182 printf("%s, %p %d ... ", __func__, p->payload, p->len);
183 __hexdump(p->payload, p->len < 12 ? p->len : 12);
186 printf("send with NULL workload?\n");
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
195 int x = l4ankh_send(q->payload, q->len, 1);
197 printf(" ankh_send: %d\n", x);
201 pbuf_header(p, ETH_PAD_SIZE); /* reclaim the padding word */
204 LINK_STATS_INC(link.xmit);
210 * Should allocate a pbuf and transfer the bytes of the incoming
211 * packet from the interface into the pbuf.
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
218 low_level_input(struct netif *netif)
220 struct ankhif *ankhif __attribute__((unused)) = netif->state;
223 l4shmc_ringbuf_t *rb = l4ankh_get_recvbuf();
225 /* Obtain the size of the packet and put it into the "len"
227 len = l4shmc_rb_receiver_read_next_size(L4SHMC_RINGBUF_HEAD(rb));
232 len += ETH_PAD_SIZE; /* allow room for Ethernet padding */
235 /* we allocate a buffer for the whole packet */
236 p = pbuf_alloc(PBUF_RAW, len, PBUF_RAM);
241 pbuf_header(p, -ETH_PAD_SIZE); /* drop the padding word */
244 /* Read enough bytes to fill this pbuf in the chain. The
245 * available data in the pbuf is given by the p->len
247 assert(p->len == len);
248 int err = l4shmc_rb_receiver_copy_out(L4SHMC_RINGBUF_HEAD(rb), p->payload, (unsigned*)&p->len);
250 printf("Incoming %p: ", p); __hexdump(p->payload, 12);
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);
260 pbuf_header(p, ETH_PAD_SIZE); /* reclaim the padding word */
263 LINK_STATS_INC(link.recv);
265 // XXX drop packet();
266 LINK_STATS_INC(link.memerr);
267 LINK_STATS_INC(link.drop);
273 static inline char *type_to_string(unsigned type)
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.
294 * @param netif the lwip network interface structure for this ankhif
297 ankhif_input(struct netif *netif)
299 struct ankhif *ankhif;
300 struct eth_hdr *ethhdr;
304 ankhif = netif->state;
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 */
313 switch (htons(ethhdr->type)) {
314 /* IP or ARP packet? */
319 case ETHTYPE_PPPOEDISC:
321 #endif /* PPPOE_SUPPORT */
322 /* full packet send to tcpip_thread to process */
323 x = netif->input(p, netif);
325 { LWIP_DEBUGF(NETIF_DEBUG, ("ankhif_input: IP input error\n"));
332 printf("unknown ethtype %lx\n", htons(ethhdr->type));
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.
344 * This function should be passed as a parameter to netif_add().
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
351 err_t ankhif_init(struct netif *netif);
352 err_t ankhif_init(struct netif *netif)
354 struct ankhif *ankhif;
356 LWIP_ASSERT("netif != NULL", (netif != NULL));
358 ankhif = mem_malloc(sizeof(struct ankhif));
359 if (ankhif == NULL) {
360 LWIP_DEBUGF(NETIF_DEBUG, ("ankhif_init: out of memory\n"));
363 ankhif->config = netif->state;
365 #if LWIP_NETIF_HOSTNAME
366 /* Initialize interface hostname */
367 netif->hostname = "lwip";
368 #endif /* LWIP_NETIF_HOSTNAME */
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.
375 NETIF_INIT_SNMP(netif, snmp_ifType_ethernet_csmacd, 100000000);
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;
387 ankhif->ethaddr = (struct eth_addr *)&(netif->hwaddr[0]);
389 /* initialize the hardware */
390 low_level_init(netif);
392 int err = pthread_create(&ankhif->recv_thread, NULL, ankhif_recv_fn, netif);
398 /******************************
399 * Client initialization code *
400 ******************************/
401 #define DEBUG_L4LWIP_INIT 0
403 // default configuration
404 static ankh_config_info cfg = { 2048, L4_INVALID_CAP,
405 L4_INVALID_CAP, "" };
407 struct netif ankh_netif;
408 static ip_addr_t ipaddr, netmask, gw;
410 static int try_l4re_option(int argc, char **argv, unsigned idx)
414 if (strcmp(argv[idx], "--shm") == 0) {
415 snprintf(cfg.shm_name, CFG_SHM_NAME_SIZE, "%s", argv[idx+1]);
418 if (strcmp(argv[idx], "--bufsize") == 0) {
419 cfg.bufsize = atoi(argv[idx+1]);
423 memcpy(&argv[idx], &argv[idx+remove], (argc-idx-remove) * sizeof(char*));
428 int l4_lwip_init(int *argc, char **argv)
432 #if DEBUG_L4LWIP_INIT
433 printf("Arguments: %d\n", *argc);
434 printf("Command line: \n");
438 #if DEBUG_L4LWIP_INIT
439 printf(" %d %s\n", i, argv[i]);
441 unsigned removed = try_l4re_option(*argc, argv, i);
447 #if DEBUG_L4LWIP_INIT
448 printf("parsed args. argc is now: %d\n", *argc);
449 printf("Initializing Ankh\n");
452 cfg.send_thread = pthread_getl4cap(pthread_self());
455 tcpip_init(NULL, NULL);
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);
462 #if DEBUG_L4LWIP_INIT
463 printf("dhcp start...\n");
465 dhcp_start(&ankh_netif);
466 while(!netif_is_up(&ankh_netif)) {
467 #if DEBUG_L4LWIP_INIT
468 //printf("dhcp pending\n");
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");