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