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"
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>
65 /* Define those to better describe your network interface. */
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...
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;
82 /* Forward declarations. */
83 static void ankhif_input(struct netif *netif);
85 static void *ankhif_recv_fn(void *arg)
88 struct netif *netif = (struct netif*)arg;
89 l4shmc_ringbuf_t *rb = l4ankh_get_recvbuf();
91 struct ankhif *ankhif = netif->state;
92 ankhif->config->recv_thread = pthread_getl4cap(pthread_self());
93 err = l4ankh_prepare_recv(ankhif->config->recv_thread);
95 printf("ANKHIF::Recv rb @ %p\n", rb);
99 err = l4shmc_rb_receiver_wait_for_data(rb, 0);
106 enter_kdebug("exit loop?");
111 * In this function, the hardware should be initialized.
112 * Called from ankhif_init().
114 * @param netif the already initialized lwip network interface structure
118 low_level_init(struct netif *netif)
120 struct ankhif *ankhif = netif->state;
123 int err = l4ankh_open(ankhif->config->shm_name, ankhif->config->bufsize);
126 err = l4ankh_prepare_send(ankhif->config->send_thread);
129 /* set MAC hardware address length */
130 netif->hwaddr_len = ETHARP_HWADDR_LEN;
132 /* set MAC hardware address */
133 memcpy(netif->hwaddr, l4ankh_get_info()->mac, ETHARP_HWADDR_LEN);
135 for (i=0; i < ETHARP_HWADDR_LEN; ++i) {
136 printf("%02X", netif->hwaddr[i]);
137 if (i < 5) printf(":");
141 /* maximum transfer unit */
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;
148 /* Do whatever else is needed to initialize interface. */
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
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
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).
168 low_level_output(struct netif *netif, struct pbuf *p)
170 struct ankhif *ankhif __attribute__((unused)) = netif->state;
171 struct pbuf *q = NULL;
173 // XXX initiate transfer();
176 pbuf_header(p, -ETH_PAD_SIZE); /* drop the padding word */
181 printf("%s, %p %d ... ", __func__, p->payload, p->len);
182 __hexdump(p->payload, p->len < 12 ? p->len : 12);
185 printf("send with NULL workload?\n");
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
194 int x = l4ankh_send(q->payload, q->len, 1);
196 printf(" ankh_send: %d\n", x);
200 pbuf_header(p, ETH_PAD_SIZE); /* reclaim the padding word */
203 LINK_STATS_INC(link.xmit);
209 * Should allocate a pbuf and transfer the bytes of the incoming
210 * packet from the interface into the pbuf.
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
217 low_level_input(struct netif *netif)
219 struct ankhif *ankhif __attribute__((unused)) = netif->state;
222 l4shmc_ringbuf_t *rb = l4ankh_get_recvbuf();
224 /* Obtain the size of the packet and put it into the "len"
226 len = l4shmc_rb_receiver_read_next_size(HEAD(rb));
231 len += ETH_PAD_SIZE; /* allow room for Ethernet padding */
234 /* we allocate a buffer for the whole packet */
235 p = pbuf_alloc(PBUF_RAW, len, PBUF_RAM);
240 pbuf_header(p, -ETH_PAD_SIZE); /* drop the padding word */
243 /* Read enough bytes to fill this pbuf in the chain. The
244 * available data in the pbuf is given by the p->len
246 assert(p->len == len);
247 int err = l4shmc_rb_receiver_copy_out(HEAD(rb), p->payload, (unsigned*)&p->len);
249 printf("Incoming %p: ", p); __hexdump(p->payload, 12);
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);
259 pbuf_header(p, ETH_PAD_SIZE); /* reclaim the padding word */
262 LINK_STATS_INC(link.recv);
264 // XXX drop packet();
265 LINK_STATS_INC(link.memerr);
266 LINK_STATS_INC(link.drop);
272 static inline char *type_to_string(unsigned type)
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.
293 * @param netif the lwip network interface structure for this ankhif
296 ankhif_input(struct netif *netif)
298 struct ankhif *ankhif;
299 struct eth_hdr *ethhdr;
302 ankhif = netif->state;
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 */
311 switch (htons(ethhdr->type)) {
312 /* IP or ARP packet? */
317 case ETHTYPE_PPPOEDISC:
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"));
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.
340 * This function should be passed as a parameter to netif_add().
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
347 err_t ankhif_init(struct netif *netif);
348 err_t ankhif_init(struct netif *netif)
350 struct ankhif *ankhif;
352 LWIP_ASSERT("netif != NULL", (netif != NULL));
354 ankhif = mem_malloc(sizeof(struct ankhif));
355 if (ankhif == NULL) {
356 LWIP_DEBUGF(NETIF_DEBUG, ("ankhif_init: out of memory\n"));
359 ankhif->config = netif->state;
361 #if LWIP_NETIF_HOSTNAME
362 /* Initialize interface hostname */
363 netif->hostname = "lwip";
364 #endif /* LWIP_NETIF_HOSTNAME */
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.
371 NETIF_INIT_SNMP(netif, snmp_ifType_ethernet_csmacd, 100000000);
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;
383 ankhif->ethaddr = (struct eth_addr *)&(netif->hwaddr[0]);
385 /* initialize the hardware */
386 low_level_init(netif);
388 int err = pthread_create(&ankhif->recv_thread, NULL, ankhif_recv_fn, netif);
394 /******************************
395 * Client initialization code *
396 ******************************/
397 #define DEBUG_L4LWIP_INIT 0
399 // default configuration
400 static ankh_config_info cfg = { 2048, L4_INVALID_CAP,
401 L4_INVALID_CAP, "" };
403 static struct netif ankh_netif;
404 static ip_addr_t ipaddr, netmask, gw;
406 static int try_l4re_option(int argc, char **argv, unsigned idx)
410 if (strcmp(argv[idx], "--shm") == 0) {
411 snprintf(cfg.shm_name, CFG_SHM_NAME_SIZE, "%s", argv[idx+1]);
414 if (strcmp(argv[idx], "--bufsize") == 0) {
415 cfg.bufsize = atoi(argv[idx+1]);
419 memcpy(&argv[idx], &argv[idx+remove], (argc-idx-remove) * sizeof(char*));
424 int l4_lwip_init(int *argc, char **argv)
428 #if DEBUG_L4LWIP_INIT
429 printf("Arguments: %d\n", *argc);
430 printf("Command line: \n");
434 #if DEBUG_L4LWIP_INIT
435 printf(" %d %s\n", i, argv[i]);
437 unsigned removed = try_l4re_option(*argc, argv, i);
443 #if DEBUG_L4LWIP_INIT
444 printf("parsed args. argc is now: %d\n", *argc);
445 printf("Initializing Ankh\n");
448 cfg.send_thread = pthread_getl4cap(pthread_self());
451 tcpip_init(NULL, NULL);
453 netif_add(&ankh_netif, &ipaddr, &netmask, &gw, &cfg,
454 ankhif_init, ethernet_input);
455 netif_set_default(&ankh_netif);
457 #if DEBUG_L4LWIP_INIT
458 printf("dhcp start...\n");
460 dhcp_start(&ankh_netif);
461 while(!netif_is_up(&ankh_netif)) {
462 printf("dhcp pending\n");
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");