NetServices Stack source

Dependents:   HelloWorld ServoInterfaceBoardExample1 4180_Lab4

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers ethernetif.c Source File

ethernetif.c

Go to the documentation of this file.
00001 /**
00002  * @file
00003  * Ethernet Interface Skeleton
00004  *
00005  */
00006 
00007 /*
00008  * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
00009  * All rights reserved. 
00010  * 
00011  * Redistribution and use in source and binary forms, with or without modification, 
00012  * are permitted provided that the following conditions are met:
00013  *
00014  * 1. Redistributions of source code must retain the above copyright notice,
00015  *    this list of conditions and the following disclaimer.
00016  * 2. Redistributions in binary form must reproduce the above copyright notice,
00017  *    this list of conditions and the following disclaimer in the documentation
00018  *    and/or other materials provided with the distribution.
00019  * 3. The name of the author may not be used to endorse or promote products
00020  *    derived from this software without specific prior written permission. 
00021  *
00022  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 
00023  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 
00024  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 
00025  * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
00026  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 
00027  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
00028  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
00029  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 
00030  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 
00031  * OF SUCH DAMAGE.
00032  *
00033  * This file is part of the lwIP TCP/IP stack.
00034  * 
00035  * Author: Adam Dunkels <adam@sics.se>
00036  *
00037  */
00038 
00039 /*
00040  * This file is a skeleton for developing Ethernet network interface
00041  * drivers for lwIP. Add code to the low_level functions and do a
00042  * search-and-replace for the word "ethernetif" to replace it with
00043  * something that better describes your network interface.
00044  */
00045 
00046 #include "lwip/opt.h"
00047 
00048 #if 0 /* don't build, this is only a skeleton, see previous comment */
00049 
00050 #include "lwip/def.h"
00051 #include "lwip/mem.h"
00052 #include "lwip/pbuf.h"
00053 #include "lwip/sys.h"
00054 #include <lwip/stats.h>
00055 #include <lwip/snmp.h>
00056 #include "netif/etharp.h"
00057 #include "netif/ppp_oe.h"
00058 
00059 /* Define those to better describe your network interface. */
00060 #define IFNAME0 'e'
00061 #define IFNAME1 'n'
00062 
00063 /**
00064  * Helper struct to hold private data used to operate your ethernet interface.
00065  * Keeping the ethernet address of the MAC in this struct is not necessary
00066  * as it is already kept in the struct netif.
00067  * But this is only an example, anyway...
00068  */
00069 struct ethernetif {
00070   struct eth_addr *ethaddr;
00071   /* Add whatever per-interface state that is needed here. */
00072 };
00073 
00074 /* Forward declarations. */
00075 static void  ethernetif_input(struct netif *netif);
00076 
00077 /**
00078  * In this function, the hardware should be initialized.
00079  * Called from ethernetif_init().
00080  *
00081  * @param netif the already initialized lwip network interface structure
00082  *        for this ethernetif
00083  */
00084 static void
00085 low_level_init(struct netif *netif)
00086 {
00087   struct ethernetif *ethernetif = netif->state;
00088   
00089   /* set MAC hardware address length */
00090   netif->hwaddr_len = ETHARP_HWADDR_LEN;
00091 
00092   /* set MAC hardware address */
00093   netif->hwaddr[0] = ;
00094   ...
00095   netif->hwaddr[5] = ;
00096 
00097   /* maximum transfer unit */
00098   netif->mtu = 1500;
00099   
00100   /* device capabilities */
00101   /* don't set NETIF_FLAG_ETHARP if this device is not an ethernet one */
00102   netif->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_LINK_UP;
00103  
00104   /* Do whatever else is needed to initialize interface. */  
00105 }
00106 
00107 /**
00108  * This function should do the actual transmission of the packet. The packet is
00109  * contained in the pbuf that is passed to the function. This pbuf
00110  * might be chained.
00111  *
00112  * @param netif the lwip network interface structure for this ethernetif
00113  * @param p the MAC packet to send (e.g. IP packet including MAC addresses and type)
00114  * @return ERR_OK if the packet could be sent
00115  *         an err_t value if the packet couldn't be sent
00116  *
00117  * @note Returning ERR_MEM here if a DMA queue of your MAC is full can lead to
00118  *       strange results. You might consider waiting for space in the DMA queue
00119  *       to become availale since the stack doesn't retry to send a packet
00120  *       dropped because of memory failure (except for the TCP timers).
00121  */
00122 
00123 static err_t
00124 low_level_output(struct netif *netif, struct pbuf *p)
00125 {
00126   struct ethernetif *ethernetif = netif->state;
00127   struct pbuf *q;
00128 
00129   initiate transfer();
00130   
00131 #if ETH_PAD_SIZE
00132   pbuf_header(p, -ETH_PAD_SIZE); /* drop the padding word */
00133 #endif
00134 
00135   for(q = p; q != NULL; q = q->next) {
00136     /* Send the data from the pbuf to the interface, one pbuf at a
00137        time. The size of the data in each pbuf is kept in the ->len
00138        variable. */
00139     send data from(q->payload, q->len);
00140   }
00141 
00142   signal that packet should be sent();
00143 
00144 #if ETH_PAD_SIZE
00145   pbuf_header(p, ETH_PAD_SIZE); /* reclaim the padding word */
00146 #endif
00147   
00148   LINK_STATS_INC(link.xmit);
00149 
00150   return ERR_OK;
00151 }
00152 
00153 /**
00154  * Should allocate a pbuf and transfer the bytes of the incoming
00155  * packet from the interface into the pbuf.
00156  *
00157  * @param netif the lwip network interface structure for this ethernetif
00158  * @return a pbuf filled with the received packet (including MAC header)
00159  *         NULL on memory error
00160  */
00161 static struct pbuf *
00162 low_level_input(struct netif *netif)
00163 {
00164   struct ethernetif *ethernetif = netif->state;
00165   struct pbuf *p, *q;
00166   u16_t len;
00167 
00168   /* Obtain the size of the packet and put it into the "len"
00169      variable. */
00170   len = ;
00171 
00172 #if ETH_PAD_SIZE
00173   len += ETH_PAD_SIZE; /* allow room for Ethernet padding */
00174 #endif
00175 
00176   /* We allocate a pbuf chain of pbufs from the pool. */
00177   p = pbuf_alloc(PBUF_RAW, len, PBUF_POOL);
00178   
00179   if (p != NULL) {
00180 
00181 #if ETH_PAD_SIZE
00182     pbuf_header(p, -ETH_PAD_SIZE); /* drop the padding word */
00183 #endif
00184 
00185     /* We iterate over the pbuf chain until we have read the entire
00186      * packet into the pbuf. */
00187     for(q = p; q != NULL; q = q->next) {
00188       /* Read enough bytes to fill this pbuf in the chain. The
00189        * available data in the pbuf is given by the q->len
00190        * variable.
00191        * This does not necessarily have to be a memcpy, you can also preallocate
00192        * pbufs for a DMA-enabled MAC and after receiving truncate it to the
00193        * actually received size. In this case, ensure the tot_len member of the
00194        * pbuf is the sum of the chained pbuf len members.
00195        */
00196       read data into(q->payload, q->len);
00197     }
00198     acknowledge that packet has been read();
00199 
00200 #if ETH_PAD_SIZE
00201     pbuf_header(p, ETH_PAD_SIZE); /* reclaim the padding word */
00202 #endif
00203 
00204     LINK_STATS_INC(link.recv);
00205   } else {
00206     drop packet();
00207     LINK_STATS_INC(link.memerr);
00208     LINK_STATS_INC(link.drop);
00209   }
00210 
00211   return p;  
00212 }
00213 
00214 /**
00215  * This function should be called when a packet is ready to be read
00216  * from the interface. It uses the function low_level_input() that
00217  * should handle the actual reception of bytes from the network
00218  * interface. Then the type of the received packet is determined and
00219  * the appropriate input function is called.
00220  *
00221  * @param netif the lwip network interface structure for this ethernetif
00222  */
00223 static void
00224 ethernetif_input(struct netif *netif)
00225 {
00226   struct ethernetif *ethernetif;
00227   struct eth_hdr *ethhdr;
00228   struct pbuf *p;
00229 
00230   ethernetif = netif->state;
00231 
00232   /* move received packet into a new pbuf */
00233   p = low_level_input(netif);
00234   /* no packet could be read, silently ignore this */
00235   if (p == NULL) return;
00236   /* points to packet payload, which starts with an Ethernet header */
00237   ethhdr = p->payload;
00238 
00239   switch (htons(ethhdr->type)) {
00240   /* IP or ARP packet? */
00241   case ETHTYPE_IP:
00242   case ETHTYPE_ARP:
00243 #if PPPOE_SUPPORT
00244   /* PPPoE packet? */
00245   case ETHTYPE_PPPOEDISC:
00246   case ETHTYPE_PPPOE:
00247 #endif /* PPPOE_SUPPORT */
00248     /* full packet send to tcpip_thread to process */
00249     if (netif->input(p, netif)!=ERR_OK)
00250      { LWIP_DEBUGF(NETIF_DEBUG, ("ethernetif_input: IP input error\n"));
00251        pbuf_free(p);
00252        p = NULL;
00253      }
00254     break;
00255 
00256   default:
00257     pbuf_free(p);
00258     p = NULL;
00259     break;
00260   }
00261 }
00262 
00263 /**
00264  * Should be called at the beginning of the program to set up the
00265  * network interface. It calls the function low_level_init() to do the
00266  * actual setup of the hardware.
00267  *
00268  * This function should be passed as a parameter to netif_add().
00269  *
00270  * @param netif the lwip network interface structure for this ethernetif
00271  * @return ERR_OK if the loopif is initialized
00272  *         ERR_MEM if private data couldn't be allocated
00273  *         any other err_t on error
00274  */
00275 err_t
00276 ethernetif_init(struct netif *netif)
00277 {
00278   struct ethernetif *ethernetif;
00279 
00280   LWIP_ASSERT("netif != NULL", (netif != NULL));
00281     
00282   ethernetif = mem_malloc(sizeof(struct ethernetif));
00283   if (ethernetif == NULL) {
00284     LWIP_DEBUGF(NETIF_DEBUG, ("ethernetif_init: out of memory\n"));
00285     return ERR_MEM;
00286   }
00287 
00288 #if LWIP_NETIF_HOSTNAME
00289   /* Initialize interface hostname */
00290   netif->hostname = "lwip";
00291 #endif /* LWIP_NETIF_HOSTNAME */
00292 
00293   /*
00294    * Initialize the snmp variables and counters inside the struct netif.
00295    * The last argument should be replaced with your link speed, in units
00296    * of bits per second.
00297    */
00298   NETIF_INIT_SNMP(netif, snmp_ifType_ethernet_csmacd, LINK_SPEED_OF_YOUR_NETIF_IN_BPS);
00299 
00300   netif->state = ethernetif;
00301   netif->name[0] = IFNAME0;
00302   netif->name[1] = IFNAME1;
00303   /* We directly use etharp_output() here to save a function call.
00304    * You can instead declare your own function an call etharp_output()
00305    * from it if you have to do some checks before sending (e.g. if link
00306    * is available...) */
00307   netif->output = etharp_output;
00308   netif->linkoutput = low_level_output;
00309   
00310   ethernetif->ethaddr = (struct eth_addr *)&(netif->hwaddr[0]);
00311   
00312   /* initialize the hardware */
00313   low_level_init(netif);
00314 
00315   return ERR_OK;
00316 }
00317 
00318 #endif /* 0 */