3 * Ethernet Interface Skeleton
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 "ethernetif" to replace it with
43 * something that better describes your network interface.
48 #if 0 /* don't build, this is only a skeleton, see previous comment */
52 #include "lwip/pbuf.h"
53 #include "lwip/stats.h"
54 #include "lwip/snmp.h"
55 #include "lwip/ethip6.h"
56 #include "lwip/etharp.h"
57 #include "netif/ppp/pppoe.h"
59 /* Define those to better describe your network interface. */
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...
70 struct eth_addr
*ethaddr
;
71 /* Add whatever per-interface state that is needed here. */
74 /* Forward declarations. */
75 static void ethernetif_input(struct netif
*netif
);
78 * In this function, the hardware should be initialized.
79 * Called from ethernetif_init().
81 * @param netif the already initialized lwip network interface structure
85 low_level_init(struct netif
*netif
)
87 struct ethernetif
*ethernetif
= netif
->state
;
89 /* set MAC hardware address length */
90 netif
->hwaddr_len
= ETHARP_HWADDR_LEN
;
92 /* set MAC hardware address */
97 /* maximum transfer unit */
100 /* device capabilities */
101 /* don't set NETIF_FLAG_ETHARP if this device is not an ethernet one */
102 netif
->flags
= NETIF_FLAG_BROADCAST
| NETIF_FLAG_ETHARP
| NETIF_FLAG_LINK_UP
;
104 #if LWIP_IPV6 && LWIP_IPV6_MLD
106 * For hardware/netifs that implement MAC filtering.
107 * All-nodes link-local is handled by default, so we must let the hardware know
108 * to allow multicast packets in.
109 * Should set mld_mac_filter previously. */
110 if (netif
->mld_mac_filter
!= NULL
) {
111 ip6_addr_t ip6_allnodes_ll
;
112 ip6_addr_set_allnodes_linklocal(&ip6_allnodes_ll
);
113 netif
->mld_mac_filter(netif
, &ip6_allnodes_ll
, NETIF_ADD_MAC_FILTER
);
115 #endif /* LWIP_IPV6 && LWIP_IPV6_MLD */
117 /* Do whatever else is needed to initialize interface. */
121 * This function should do the actual transmission of the packet. The packet is
122 * contained in the pbuf that is passed to the function. This pbuf
125 * @param netif the lwip network interface structure for this ethernetif
126 * @param p the MAC packet to send (e.g. IP packet including MAC addresses and type)
127 * @return ERR_OK if the packet could be sent
128 * an err_t value if the packet couldn't be sent
130 * @note Returning ERR_MEM here if a DMA queue of your MAC is full can lead to
131 * strange results. You might consider waiting for space in the DMA queue
132 * to become available since the stack doesn't retry to send a packet
133 * dropped because of memory failure (except for the TCP timers).
137 low_level_output(struct netif
*netif
, struct pbuf
*p
)
139 struct ethernetif
*ethernetif
= netif
->state
;
145 pbuf_header(p
, -ETH_PAD_SIZE
); /* drop the padding word */
148 for (q
= p
; q
!= NULL
; q
= q
->next
) {
149 /* Send the data from the pbuf to the interface, one pbuf at a
150 time. The size of the data in each pbuf is kept in the ->len
152 send data
from(q
->payload
, q
->len
);
155 signal that packet should be
sent();
157 MIB2_STATS_NETIF_ADD(netif
, ifoutoctets
, p
->tot_len
);
158 if (((u8_t
*)p
->payload
)[0] & 1) {
159 /* broadcast or multicast packet*/
160 MIB2_STATS_NETIF_INC(netif
, ifoutnucastpkts
);
163 MIB2_STATS_NETIF_INC(netif
, ifoutucastpkts
);
165 /* increase ifoutdiscards or ifouterrors on error */
168 pbuf_header(p
, ETH_PAD_SIZE
); /* reclaim the padding word */
171 LINK_STATS_INC(link
.xmit
);
177 * Should allocate a pbuf and transfer the bytes of the incoming
178 * packet from the interface into the pbuf.
180 * @param netif the lwip network interface structure for this ethernetif
181 * @return a pbuf filled with the received packet (including MAC header)
182 * NULL on memory error
185 low_level_input(struct netif
*netif
)
187 struct ethernetif
*ethernetif
= netif
->state
;
191 /* Obtain the size of the packet and put it into the "len"
196 len
+= ETH_PAD_SIZE
; /* allow room for Ethernet padding */
199 /* We allocate a pbuf chain of pbufs from the pool. */
200 p
= pbuf_alloc(PBUF_RAW
, len
, PBUF_POOL
);
205 pbuf_header(p
, -ETH_PAD_SIZE
); /* drop the padding word */
208 /* We iterate over the pbuf chain until we have read the entire
209 * packet into the pbuf. */
210 for (q
= p
; q
!= NULL
; q
= q
->next
) {
211 /* Read enough bytes to fill this pbuf in the chain. The
212 * available data in the pbuf is given by the q->len
214 * This does not necessarily have to be a memcpy, you can also preallocate
215 * pbufs for a DMA-enabled MAC and after receiving truncate it to the
216 * actually received size. In this case, ensure the tot_len member of the
217 * pbuf is the sum of the chained pbuf len members.
219 read data
into(q
->payload
, q
->len
);
221 acknowledge that packet has been
read();
223 MIB2_STATS_NETIF_ADD(netif
, ifinoctets
, p
->tot_len
);
224 if (((u8_t
*)p
->payload
)[0] & 1) {
225 /* broadcast or multicast packet*/
226 MIB2_STATS_NETIF_INC(netif
, ifinnucastpkts
);
229 MIB2_STATS_NETIF_INC(netif
, ifinucastpkts
);
232 pbuf_header(p
, ETH_PAD_SIZE
); /* reclaim the padding word */
235 LINK_STATS_INC(link
.recv
);
238 LINK_STATS_INC(link
.memerr
);
239 LINK_STATS_INC(link
.drop
);
240 MIB2_STATS_NETIF_INC(netif
, ifindiscards
);
247 * This function should be called when a packet is ready to be read
248 * from the interface. It uses the function low_level_input() that
249 * should handle the actual reception of bytes from the network
250 * interface. Then the type of the received packet is determined and
251 * the appropriate input function is called.
253 * @param netif the lwip network interface structure for this ethernetif
256 ethernetif_input(struct netif
*netif
)
258 struct ethernetif
*ethernetif
;
259 struct eth_hdr
*ethhdr
;
262 ethernetif
= netif
->state
;
264 /* move received packet into a new pbuf */
265 p
= low_level_input(netif
);
266 /* if no packet could be read, silently ignore this */
268 /* pass all packets to ethernet_input, which decides what packets it supports */
269 if (netif
->input(p
, netif
) != ERR_OK
) {
270 LWIP_DEBUGF(NETIF_DEBUG
, ("ethernetif_input: IP input error\n"));
278 * Should be called at the beginning of the program to set up the
279 * network interface. It calls the function low_level_init() to do the
280 * actual setup of the hardware.
282 * This function should be passed as a parameter to netif_add().
284 * @param netif the lwip network interface structure for this ethernetif
285 * @return ERR_OK if the loopif is initialized
286 * ERR_MEM if private data couldn't be allocated
287 * any other err_t on error
290 ethernetif_init(struct netif
*netif
)
292 struct ethernetif
*ethernetif
;
294 LWIP_ASSERT("netif != NULL", (netif
!= NULL
));
296 ethernetif
= mem_malloc(sizeof(struct ethernetif
));
297 if (ethernetif
== NULL
) {
298 LWIP_DEBUGF(NETIF_DEBUG
, ("ethernetif_init: out of memory\n"));
302 #if LWIP_NETIF_HOSTNAME
303 /* Initialize interface hostname */
304 netif
->hostname
= "lwip";
305 #endif /* LWIP_NETIF_HOSTNAME */
308 * Initialize the snmp variables and counters inside the struct netif.
309 * The last argument should be replaced with your link speed, in units
310 * of bits per second.
312 MIB2_INIT_NETIF(netif
, snmp_ifType_ethernet_csmacd
, LINK_SPEED_OF_YOUR_NETIF_IN_BPS
);
314 netif
->state
= ethernetif
;
315 netif
->name
[0] = IFNAME0
;
316 netif
->name
[1] = IFNAME1
;
317 /* We directly use etharp_output() here to save a function call.
318 * You can instead declare your own function an call etharp_output()
319 * from it if you have to do some checks before sending (e.g. if link
320 * is available...) */
321 netif
->output
= etharp_output
;
323 netif
->output_ip6
= ethip6_output
;
324 #endif /* LWIP_IPV6 */
325 netif
->linkoutput
= low_level_output
;
327 ethernetif
->ethaddr
= (struct eth_addr
*)&(netif
->hwaddr
[0]);
329 /* initialize the hardware */
330 low_level_init(netif
);