ethernetif.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. /**
  2. * @file
  3. * Ethernet Interface Skeleton
  4. *
  5. */
  6. /*
  7. * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
  8. * All rights reserved.
  9. *
  10. * Redistribution and use in source and binary forms, with or without modification,
  11. * are permitted provided that the following conditions are met:
  12. *
  13. * 1. Redistributions of source code must retain the above copyright notice,
  14. * this list of conditions and the following disclaimer.
  15. * 2. Redistributions in binary form must reproduce the above copyright notice,
  16. * this list of conditions and the following disclaimer in the documentation
  17. * and/or other materials provided with the distribution.
  18. * 3. The name of the author may not be used to endorse or promote products
  19. * derived from this software without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  22. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  23. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
  24. * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  25. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
  26. * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  27. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  28. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  29. * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  30. * OF SUCH DAMAGE.
  31. *
  32. * This file is part of the lwIP TCP/IP stack.
  33. *
  34. * Author: Adam Dunkels <adam@sics.se>
  35. *
  36. */
  37. /*
  38. * This file is a skeleton for developing Ethernet network interface
  39. * drivers for lwIP. Add code to the low_level functions and do a
  40. * search-and-replace for the word "ethernetif" to replace it with
  41. * something that better describes your network interface.
  42. */
  43. #include "lwip/opt.h"
  44. #if 0 /* don't build, this is only a skeleton, see previous comment */
  45. #include "lwip/def.h"
  46. #include "lwip/mem.h"
  47. #include "lwip/pbuf.h"
  48. #include <lwip/stats.h>
  49. #include <lwip/snmp.h>
  50. #include "netif/etharp.h"
  51. #include "netif/ppp_oe.h"
  52. /* Define those to better describe your network interface. */
  53. #define IFNAME0 'e'
  54. #define IFNAME1 'n'
  55. /**
  56. * Helper struct to hold private data used to operate your ethernet interface.
  57. * Keeping the ethernet address of the MAC in this struct is not necessary
  58. * as it is already kept in the struct netif.
  59. * But this is only an example, anyway...
  60. */
  61. struct ethernetif {
  62. struct eth_addr *ethaddr;
  63. /* Add whatever per-interface state that is needed here. */
  64. };
  65. /* Forward declarations. */
  66. static void ethernetif_input(struct netif *netif);
  67. /**
  68. * In this function, the hardware should be initialized.
  69. * Called from ethernetif_init().
  70. *
  71. * @param netif the already initialized lwip network interface structure
  72. * for this ethernetif
  73. */
  74. static void
  75. low_level_init(struct netif *netif)
  76. {
  77. struct ethernetif *ethernetif = netif->state;
  78. /* set MAC hardware address length */
  79. netif->hwaddr_len = ETHARP_HWADDR_LEN;
  80. /* set MAC hardware address */
  81. netif->hwaddr[0] = ;
  82. ...
  83. netif->hwaddr[5] = ;
  84. /* maximum transfer unit */
  85. netif->mtu = 1500;
  86. /* device capabilities */
  87. /* don't set NETIF_FLAG_ETHARP if this device is not an ethernet one */
  88. netif->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_LINK_UP;
  89. /* Do whatever else is needed to initialize interface. */
  90. }
  91. /**
  92. * This function should do the actual transmission of the packet. The packet is
  93. * contained in the pbuf that is passed to the function. This pbuf
  94. * might be chained.
  95. *
  96. * @param netif the lwip network interface structure for this ethernetif
  97. * @param p the MAC packet to send (e.g. IP packet including MAC addresses and type)
  98. * @return ERR_OK if the packet could be sent
  99. * an err_t value if the packet couldn't be sent
  100. *
  101. * @note Returning ERR_MEM here if a DMA queue of your MAC is full can lead to
  102. * strange results. You might consider waiting for space in the DMA queue
  103. * to become availale since the stack doesn't retry to send a packet
  104. * dropped because of memory failure (except for the TCP timers).
  105. */
  106. static err_t
  107. low_level_output(struct netif *netif, struct pbuf *p)
  108. {
  109. struct ethernetif *ethernetif = netif->state;
  110. struct pbuf *q;
  111. initiate transfer();
  112. #if ETH_PAD_SIZE
  113. pbuf_header(p, -ETH_PAD_SIZE); /* drop the padding word */
  114. #endif
  115. for(q = p; q != NULL; q = q->next) {
  116. /* Send the data from the pbuf to the interface, one pbuf at a
  117. time. The size of the data in each pbuf is kept in the ->len
  118. variable. */
  119. send data from(q->payload, q->len);
  120. }
  121. signal that packet should be sent();
  122. #if ETH_PAD_SIZE
  123. pbuf_header(p, ETH_PAD_SIZE); /* reclaim the padding word */
  124. #endif
  125. LINK_STATS_INC(link.xmit);
  126. return ERR_OK;
  127. }
  128. /**
  129. * Should allocate a pbuf and transfer the bytes of the incoming
  130. * packet from the interface into the pbuf.
  131. *
  132. * @param netif the lwip network interface structure for this ethernetif
  133. * @return a pbuf filled with the received packet (including MAC header)
  134. * NULL on memory error
  135. */
  136. static struct pbuf *
  137. low_level_input(struct netif *netif)
  138. {
  139. struct ethernetif *ethernetif = netif->state;
  140. struct pbuf *p, *q;
  141. u16_t len;
  142. /* Obtain the size of the packet and put it into the "len"
  143. variable. */
  144. len = ;
  145. #if ETH_PAD_SIZE
  146. len += ETH_PAD_SIZE; /* allow room for Ethernet padding */
  147. #endif
  148. /* We allocate a pbuf chain of pbufs from the pool. */
  149. p = pbuf_alloc(PBUF_RAW, len, PBUF_POOL);
  150. if (p != NULL) {
  151. #if ETH_PAD_SIZE
  152. pbuf_header(p, -ETH_PAD_SIZE); /* drop the padding word */
  153. #endif
  154. /* We iterate over the pbuf chain until we have read the entire
  155. * packet into the pbuf. */
  156. for(q = p; q != NULL; q = q->next) {
  157. /* Read enough bytes to fill this pbuf in the chain. The
  158. * available data in the pbuf is given by the q->len
  159. * variable.
  160. * This does not necessarily have to be a memcpy, you can also preallocate
  161. * pbufs for a DMA-enabled MAC and after receiving truncate it to the
  162. * actually received size. In this case, ensure the tot_len member of the
  163. * pbuf is the sum of the chained pbuf len members.
  164. */
  165. read data into(q->payload, q->len);
  166. }
  167. acknowledge that packet has been read();
  168. #if ETH_PAD_SIZE
  169. pbuf_header(p, ETH_PAD_SIZE); /* reclaim the padding word */
  170. #endif
  171. LINK_STATS_INC(link.recv);
  172. } else {
  173. drop packet();
  174. LINK_STATS_INC(link.memerr);
  175. LINK_STATS_INC(link.drop);
  176. }
  177. return p;
  178. }
  179. /**
  180. * This function should be called when a packet is ready to be read
  181. * from the interface. It uses the function low_level_input() that
  182. * should handle the actual reception of bytes from the network
  183. * interface. Then the type of the received packet is determined and
  184. * the appropriate input function is called.
  185. *
  186. * @param netif the lwip network interface structure for this ethernetif
  187. */
  188. static void
  189. ethernetif_input(struct netif *netif)
  190. {
  191. struct ethernetif *ethernetif;
  192. struct eth_hdr *ethhdr;
  193. struct pbuf *p;
  194. ethernetif = netif->state;
  195. /* move received packet into a new pbuf */
  196. p = low_level_input(netif);
  197. /* no packet could be read, silently ignore this */
  198. if (p == NULL) return;
  199. /* points to packet payload, which starts with an Ethernet header */
  200. ethhdr = p->payload;
  201. switch (htons(ethhdr->type)) {
  202. /* IP or ARP packet? */
  203. case ETHTYPE_IP:
  204. case ETHTYPE_ARP:
  205. #if PPPOE_SUPPORT
  206. /* PPPoE packet? */
  207. case ETHTYPE_PPPOEDISC:
  208. case ETHTYPE_PPPOE:
  209. #endif /* PPPOE_SUPPORT */
  210. /* full packet send to tcpip_thread to process */
  211. if (netif->input(p, netif)!=ERR_OK)
  212. { LWIP_DEBUGF(NETIF_DEBUG, ("ethernetif_input: IP input error\n"));
  213. pbuf_free(p);
  214. p = NULL;
  215. }
  216. break;
  217. default:
  218. pbuf_free(p);
  219. p = NULL;
  220. break;
  221. }
  222. }
  223. /**
  224. * Should be called at the beginning of the program to set up the
  225. * network interface. It calls the function low_level_init() to do the
  226. * actual setup of the hardware.
  227. *
  228. * This function should be passed as a parameter to netif_add().
  229. *
  230. * @param netif the lwip network interface structure for this ethernetif
  231. * @return ERR_OK if the loopif is initialized
  232. * ERR_MEM if private data couldn't be allocated
  233. * any other err_t on error
  234. */
  235. err_t
  236. ethernetif_init(struct netif *netif)
  237. {
  238. struct ethernetif *ethernetif;
  239. LWIP_ASSERT("netif != NULL", (netif != NULL));
  240. ethernetif = mem_malloc(sizeof(struct ethernetif));
  241. if (ethernetif == NULL) {
  242. LWIP_DEBUGF(NETIF_DEBUG, ("ethernetif_init: out of memory\n"));
  243. return ERR_MEM;
  244. }
  245. #if LWIP_NETIF_HOSTNAME
  246. /* Initialize interface hostname */
  247. netif->hostname = "lwip";
  248. #endif /* LWIP_NETIF_HOSTNAME */
  249. /*
  250. * Initialize the snmp variables and counters inside the struct netif.
  251. * The last argument should be replaced with your link speed, in units
  252. * of bits per second.
  253. */
  254. NETIF_INIT_SNMP(netif, snmp_ifType_ethernet_csmacd, LINK_SPEED_OF_YOUR_NETIF_IN_BPS);
  255. netif->state = ethernetif;
  256. netif->name[0] = IFNAME0;
  257. netif->name[1] = IFNAME1;
  258. /* We directly use etharp_output() here to save a function call.
  259. * You can instead declare your own function an call etharp_output()
  260. * from it if you have to do some checks before sending (e.g. if link
  261. * is available...) */
  262. netif->output = etharp_output;
  263. netif->linkoutput = low_level_output;
  264. ethernetif->ethaddr = (struct eth_addr *)&(netif->hwaddr[0]);
  265. /* initialize the hardware */
  266. low_level_init(netif);
  267. return ERR_OK;
  268. }
  269. #endif /* 0 */