Marco Zecchini
/
Example_RTOS
Rtos API example
Embed:
(wiki syntax)
Show/hide line numbers
lwip_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/stats.h" 00054 #include "lwip/snmp.h" 00055 #include "lwip/ethip6.h" 00056 #include "lwip/etharp.h" 00057 #include "netif/ppp/pppoe.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 #if LWIP_IPV6 && LWIP_IPV6_MLD 00105 /* 00106 * For hardware/netifs that implement MAC filtering. 00107 * All-nodes link-local is handled by default, so we must let the hardware know 00108 * to allow multicast packets in. 00109 * Should set mld_mac_filter previously. */ 00110 if (netif->mld_mac_filter != NULL) { 00111 ip6_addr_t ip6_allnodes_ll; 00112 ip6_addr_set_allnodes_linklocal(&ip6_allnodes_ll); 00113 netif->mld_mac_filter(netif, &ip6_allnodes_ll, NETIF_ADD_MAC_FILTER); 00114 } 00115 #endif /* LWIP_IPV6 && LWIP_IPV6_MLD */ 00116 00117 /* Do whatever else is needed to initialize interface. */ 00118 } 00119 00120 /** 00121 * This function should do the actual transmission of the packet. The packet is 00122 * contained in the pbuf that is passed to the function. This pbuf 00123 * might be chained. 00124 * 00125 * @param netif the lwip network interface structure for this ethernetif 00126 * @param p the MAC packet to send (e.g. IP packet including MAC addresses and type) 00127 * @return ERR_OK if the packet could be sent 00128 * an err_t value if the packet couldn't be sent 00129 * 00130 * @note Returning ERR_MEM here if a DMA queue of your MAC is full can lead to 00131 * strange results. You might consider waiting for space in the DMA queue 00132 * to become available since the stack doesn't retry to send a packet 00133 * dropped because of memory failure (except for the TCP timers). 00134 */ 00135 00136 static err_t 00137 low_level_output(struct netif *netif, struct pbuf *p) 00138 { 00139 struct ethernetif *ethernetif = netif->state; 00140 struct pbuf *q; 00141 00142 initiate transfer(); 00143 00144 #if ETH_PAD_SIZE 00145 pbuf_header(p, -ETH_PAD_SIZE); /* drop the padding word */ 00146 #endif 00147 00148 for (q = p; q != NULL; q = q->next) { 00149 /* Send the data from the pbuf to the interface, one pbuf at a 00150 time. The size of the data in each pbuf is kept in the ->len 00151 variable. */ 00152 send data from(q->payload, q->len); 00153 } 00154 00155 signal that packet should be sent(); 00156 00157 MIB2_STATS_NETIF_ADD(netif, ifoutoctets, p->tot_len); 00158 if (((u8_t*)p->payload)[0] & 1) { 00159 /* broadcast or multicast packet*/ 00160 MIB2_STATS_NETIF_INC(netif, ifoutnucastpkts); 00161 } else { 00162 /* unicast packet */ 00163 MIB2_STATS_NETIF_INC(netif, ifoutucastpkts); 00164 } 00165 /* increase ifoutdiscards or ifouterrors on error */ 00166 00167 #if ETH_PAD_SIZE 00168 pbuf_header(p, ETH_PAD_SIZE); /* reclaim the padding word */ 00169 #endif 00170 00171 LINK_STATS_INC(link.xmit); 00172 00173 return ERR_OK; 00174 } 00175 00176 /** 00177 * Should allocate a pbuf and transfer the bytes of the incoming 00178 * packet from the interface into the pbuf. 00179 * 00180 * @param netif the lwip network interface structure for this ethernetif 00181 * @return a pbuf filled with the received packet (including MAC header) 00182 * NULL on memory error 00183 */ 00184 static struct pbuf * 00185 low_level_input(struct netif *netif) 00186 { 00187 struct ethernetif *ethernetif = netif->state; 00188 struct pbuf *p, *q; 00189 u16_t len; 00190 00191 /* Obtain the size of the packet and put it into the "len" 00192 variable. */ 00193 len = ; 00194 00195 #if ETH_PAD_SIZE 00196 len += ETH_PAD_SIZE; /* allow room for Ethernet padding */ 00197 #endif 00198 00199 /* We allocate a pbuf chain of pbufs from the pool. */ 00200 p = pbuf_alloc(PBUF_RAW, len, PBUF_POOL); 00201 00202 if (p != NULL) { 00203 00204 #if ETH_PAD_SIZE 00205 pbuf_header(p, -ETH_PAD_SIZE); /* drop the padding word */ 00206 #endif 00207 00208 /* We iterate over the pbuf chain until we have read the entire 00209 * packet into the pbuf. */ 00210 for (q = p; q != NULL; q = q->next) { 00211 /* Read enough bytes to fill this pbuf in the chain. The 00212 * available data in the pbuf is given by the q->len 00213 * variable. 00214 * This does not necessarily have to be a memcpy, you can also preallocate 00215 * pbufs for a DMA-enabled MAC and after receiving truncate it to the 00216 * actually received size. In this case, ensure the tot_len member of the 00217 * pbuf is the sum of the chained pbuf len members. 00218 */ 00219 read data into(q->payload, q->len); 00220 } 00221 acknowledge that packet has been read(); 00222 00223 MIB2_STATS_NETIF_ADD(netif, ifinoctets, p->tot_len); 00224 if (((u8_t*)p->payload)[0] & 1) { 00225 /* broadcast or multicast packet*/ 00226 MIB2_STATS_NETIF_INC(netif, ifinnucastpkts); 00227 } else { 00228 /* unicast packet*/ 00229 MIB2_STATS_NETIF_INC(netif, ifinucastpkts); 00230 } 00231 #if ETH_PAD_SIZE 00232 pbuf_header(p, ETH_PAD_SIZE); /* reclaim the padding word */ 00233 #endif 00234 00235 LINK_STATS_INC(link.recv); 00236 } else { 00237 drop packet(); 00238 LINK_STATS_INC(link.memerr); 00239 LINK_STATS_INC(link.drop); 00240 MIB2_STATS_NETIF_INC(netif, ifindiscards); 00241 } 00242 00243 return p; 00244 } 00245 00246 /** 00247 * This function should be called when a packet is ready to be read 00248 * from the interface. It uses the function low_level_input() that 00249 * should handle the actual reception of bytes from the network 00250 * interface. Then the type of the received packet is determined and 00251 * the appropriate input function is called. 00252 * 00253 * @param netif the lwip network interface structure for this ethernetif 00254 */ 00255 static void 00256 ethernetif_input(struct netif *netif) 00257 { 00258 struct ethernetif *ethernetif; 00259 struct eth_hdr *ethhdr; 00260 struct pbuf *p; 00261 00262 ethernetif = netif->state; 00263 00264 /* move received packet into a new pbuf */ 00265 p = low_level_input(netif); 00266 /* if no packet could be read, silently ignore this */ 00267 if (p != NULL) { 00268 /* pass all packets to ethernet_input, which decides what packets it supports */ 00269 if (netif->input(p, netif) != ERR_OK) { 00270 LWIP_DEBUGF(NETIF_DEBUG, ("ethernetif_input: IP input error\n")); 00271 pbuf_free(p); 00272 p = NULL; 00273 } 00274 } 00275 } 00276 00277 /** 00278 * Should be called at the beginning of the program to set up the 00279 * network interface. It calls the function low_level_init() to do the 00280 * actual setup of the hardware. 00281 * 00282 * This function should be passed as a parameter to netif_add(). 00283 * 00284 * @param netif the lwip network interface structure for this ethernetif 00285 * @return ERR_OK if the loopif is initialized 00286 * ERR_MEM if private data couldn't be allocated 00287 * any other err_t on error 00288 */ 00289 err_t 00290 ethernetif_init(struct netif *netif) 00291 { 00292 struct ethernetif *ethernetif; 00293 00294 LWIP_ASSERT("netif != NULL", (netif != NULL)); 00295 00296 ethernetif = mem_malloc(sizeof(struct ethernetif)); 00297 if (ethernetif == NULL) { 00298 LWIP_DEBUGF(NETIF_DEBUG, ("ethernetif_init: out of memory\n")); 00299 return ERR_MEM; 00300 } 00301 00302 #if LWIP_NETIF_HOSTNAME 00303 /* Initialize interface hostname */ 00304 netif->hostname = "lwip"; 00305 #endif /* LWIP_NETIF_HOSTNAME */ 00306 00307 /* 00308 * Initialize the snmp variables and counters inside the struct netif. 00309 * The last argument should be replaced with your link speed, in units 00310 * of bits per second. 00311 */ 00312 MIB2_INIT_NETIF(netif, snmp_ifType_ethernet_csmacd, LINK_SPEED_OF_YOUR_NETIF_IN_BPS); 00313 00314 netif->state = ethernetif; 00315 netif->name[0] = IFNAME0; 00316 netif->name[1] = IFNAME1; 00317 /* We directly use etharp_output() here to save a function call. 00318 * You can instead declare your own function an call etharp_output() 00319 * from it if you have to do some checks before sending (e.g. if link 00320 * is available...) */ 00321 netif->output = etharp_output; 00322 #if LWIP_IPV6 00323 netif->output_ip6 = ethip6_output; 00324 #endif /* LWIP_IPV6 */ 00325 netif->linkoutput = low_level_output; 00326 00327 ethernetif->ethaddr = (struct eth_addr *)&(netif->hwaddr[0]); 00328 00329 /* initialize the hardware */ 00330 low_level_init(netif); 00331 00332 return ERR_OK; 00333 } 00334 00335 #endif /* 0 */
Generated on Sun Jul 17 2022 08:25:24 by 1.7.2