1/** 2 * @file 3 * Ethernet Interface Skeleton 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 "ethernetif" to replace it with 43 * something that better describes your network interface. 44 */ 45 46#include "lwip/opt.h" 47 48#if 0 /* don't build, this is only a skeleton, see previous comment */ 49 50#include "lwip/def.h" 51#include "lwip/mem.h" 52#include "lwip/pbuf.h" 53#include "lwip/sys.h" 54#include <lwip/stats.h> 55#include <lwip/snmp.h> 56#include "netif/etharp.h" 57#include "netif/ppp_oe.h" 58 59/* Define those to better describe your network interface. */ 60#define IFNAME0 'e' 61#define IFNAME1 'n' 62 63/** 64 * Helper struct to hold private data used to operate your ethernet interface. 65 * Keeping the ethernet address of the MAC in this struct is not necessary 66 * as it is already kept in the struct netif. 67 * But this is only an example, anyway... 68 */ 69struct ethernetif { 70 struct eth_addr *ethaddr; 71 /* Add whatever per-interface state that is needed here. */ 72}; 73 74/* Forward declarations. */ 75static void ethernetif_input(struct netif *netif); 76 77/** 78 * In this function, the hardware should be initialized. 79 * Called from ethernetif_init(). 80 * 81 * @param netif the already initialized lwip network interface structure 82 * for this ethernetif 83 */ 84static void low_level_init(struct netif *netif) 85{ 86 struct ethernetif *ethernetif = netif->state; 87 88 /* set MAC hardware address length */ 89 netif->hwaddr_len = ETHARP_HWADDR_LEN; 90 91 /* set MAC hardware address */ 92 netif->hwaddr[0] =; 93 ... netif->hwaddr[5] =; 94 95 /* maximum transfer unit */ 96 netif->mtu = 1500; 97 98 /* device capabilities */ 99 /* don't set NETIF_FLAG_ETHARP if this device is not an ethernet one */ 100 netif->flags = 101 NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_LINK_UP; 102 103 /* Do whatever else is needed to initialize interface. */ 104} 105 106/** 107 * This function should do the actual transmission of the packet. The packet is 108 * contained in the pbuf that is passed to the function. This pbuf 109 * might be chained. 110 * 111 * @param netif the lwip network interface structure for this ethernetif 112 * @param p the MAC packet to send (e.g. IP packet including MAC addresses and type) 113 * @return ERR_OK if the packet could be sent 114 * an err_t value if the packet couldn't be sent 115 * 116 * @note Returning ERR_MEM here if a DMA queue of your MAC is full can lead to 117 * strange results. You might consider waiting for space in the DMA queue 118 * to become availale since the stack doesn't retry to send a packet 119 * dropped because of memory failure (except for the TCP timers). 120 */ 121 122static err_t low_level_output(struct netif *netif, struct pbuf *p) 123{ 124 struct ethernetif *ethernetif = netif->state; 125 struct pbuf *q; 126 127 initiate transfer(); 128 129#if ETH_PAD_SIZE 130 pbuf_header(p, -ETH_PAD_SIZE); /* drop the padding word */ 131#endif 132 133 for (q = p; q != NULL; q = q->next) { 134 /* Send the data from the pbuf to the interface, one pbuf at a 135 time. The size of the data in each pbuf is kept in the ->len 136 variable. */ 137 send data from(q->payload, q->len); 138 } 139 140 signal that packet should be sent(); 141 142#if ETH_PAD_SIZE 143 pbuf_header(p, ETH_PAD_SIZE); /* reclaim the padding word */ 144#endif 145 146 LINK_STATS_INC(link.xmit); 147 148 return ERR_OK; 149} 150 151/** 152 * Should allocate a pbuf and transfer the bytes of the incoming 153 * packet from the interface into the pbuf. 154 * 155 * @param netif the lwip network interface structure for this ethernetif 156 * @return a pbuf filled with the received packet (including MAC header) 157 * NULL on memory error 158 */ 159static struct pbuf *low_level_input(struct netif *netif) 160{ 161 struct ethernetif *ethernetif = netif->state; 162 struct pbuf *p, *q; 163 u16_t len; 164 165 /* Obtain the size of the packet and put it into the "len" 166 variable. */ 167 len =; 168 169#if ETH_PAD_SIZE 170 len += ETH_PAD_SIZE; /* allow room for Ethernet padding */ 171#endif 172 173 /* We allocate a pbuf chain of pbufs from the pool. */ 174 p = pbuf_alloc(PBUF_RAW, len, PBUF_POOL); 175 176 if (p != NULL) { 177 178#if ETH_PAD_SIZE 179 pbuf_header(p, -ETH_PAD_SIZE); /* drop the padding word */ 180#endif 181 182 /* We iterate over the pbuf chain until we have read the entire 183 * packet into the pbuf. */ 184 for (q = p; q != NULL; q = q->next) { 185 /* Read enough bytes to fill this pbuf in the chain. The 186 * available data in the pbuf is given by the q->len 187 * variable. */ 188 read data into(q->payload, q->len); 189 } 190 acknowledge that packet has been read(); 191 192#if ETH_PAD_SIZE 193 pbuf_header(p, ETH_PAD_SIZE); /* reclaim the padding word */ 194#endif 195 196 LINK_STATS_INC(link.recv); 197 } else { 198 drop packet(); 199 200 LINK_STATS_INC(link.memerr); 201 LINK_STATS_INC(link.drop); 202 } 203 204 return p; 205} 206 207/** 208 * This function should be called when a packet is ready to be read 209 * from the interface. It uses the function low_level_input() that 210 * should handle the actual reception of bytes from the network 211 * interface. Then the type of the received packet is determined and 212 * the appropriate input function is called. 213 * 214 * @param netif the lwip network interface structure for this ethernetif 215 */ 216static void ethernetif_input(struct netif *netif) 217{ 218 struct ethernetif *ethernetif; 219 struct eth_hdr *ethhdr; 220 struct pbuf *p; 221 222 ethernetif = netif->state; 223 224 /* move received packet into a new pbuf */ 225 p = low_level_input(netif); 226 /* no packet could be read, silently ignore this */ 227 if (p == NULL) 228 return; 229 /* points to packet payload, which starts with an Ethernet header */ 230 ethhdr = p->payload; 231 232 switch (htons(ethhdr->type)) { 233 /* IP or ARP packet? */ 234 case ETHTYPE_IP: 235 case ETHTYPE_ARP: 236#if PPPOE_SUPPORT 237 /* PPPoE packet? */ 238 case ETHTYPE_PPPOEDISC: 239 case ETHTYPE_PPPOE: 240#endif /* PPPOE_SUPPORT */ 241 /* full packet send to tcpip_thread to process */ 242 if (netif->input(p, netif) != ERR_OK) { 243 LWIP_DEBUGF(NETIF_DEBUG, 244 ("ethernetif_input: IP input error\n")); 245 pbuf_free(p); 246 p = NULL; 247 } 248 break; 249 250 default: 251 pbuf_free(p); 252 p = NULL; 253 break; 254 } 255} 256 257/** 258 * Should be called at the beginning of the program to set up the 259 * network interface. It calls the function low_level_init() to do the 260 * actual setup of the hardware. 261 * 262 * This function should be passed as a parameter to netif_add(). 263 * 264 * @param netif the lwip network interface structure for this ethernetif 265 * @return ERR_OK if the loopif is initialized 266 * ERR_MEM if private data couldn't be allocated 267 * any other err_t on error 268 */ 269err_t ethernetif_init(struct netif *netif) 270{ 271 struct ethernetif *ethernetif; 272 273 LWIP_ASSERT("netif != NULL", (netif != NULL)); 274 275 ethernetif = mem_malloc(sizeof(struct ethernetif)); 276 if (ethernetif == NULL) { 277 LWIP_DEBUGF(NETIF_DEBUG, ("ethernetif_init: out of memory\n")); 278 return ERR_MEM; 279 } 280#if LWIP_NETIF_HOSTNAME 281 /* Initialize interface hostname */ 282 netif->hostname = "lwip"; 283#endif /* LWIP_NETIF_HOSTNAME */ 284 285 /* 286 * Initialize the snmp variables and counters inside the struct netif. 287 * The last argument should be replaced with your link speed, in units 288 * of bits per second. 289 */ 290 NETIF_INIT_SNMP(netif, snmp_ifType_ethernet_csmacd, 291 LINK_SPEED_OF_YOUR_NETIF_IN_BPS); 292 293 netif->state = ethernetif; 294 netif->name[0] = IFNAME0; 295 netif->name[1] = IFNAME1; 296 /* We directly use etharp_output() here to save a function call. 297 * You can instead declare your own function an call etharp_output() 298 * from it if you have to do some checks before sending (e.g. if link 299 * is available...) */ 300 netif->output = etharp_output; 301 netif->linkoutput = low_level_output; 302 303 ethernetif->ethaddr = (struct eth_addr *) &(netif->hwaddr[0]); 304 305 /* initialize the hardware */ 306 low_level_init(netif); 307 308 return ERR_OK; 309} 310 311#endif /* 0 */ 312