UbixOS  2.0
ethernetif.c
Go to the documentation of this file.
1 
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 #include <pci/lnc.h>
46 #include "net/opt.h"
47 
48 #include <net/tcpip.h> //TMP
49 
50 #include "net/def.h"
51 #include "net/mem.h"
52 #include "net/pbuf.h"
53 #include "net/stats.h"
54 #include "net/snmp.h"
55 #include "net/ethip6.h"
56 #include "net/etharp.h"
57 //#include "net/ppp/pppoe.h"
58 
59 /* Define those to better describe your network interface. */
60 #define IFNAME0 'e'
61 #define IFNAME1 'n'
62 
63 struct nicBuffer *tmpBuf = 0x0;
64 
71 struct ethernetif {
72  struct eth_addr *ethaddr;
73  /* Add whatever per-interface state that is needed here. */
74 };
75 
76 /* Forward declarations. */
77 //MrOlsen 2017-12-17 LWIP static void ethernetif_input(struct netif *netif);
78 
86 static void
87 low_level_init(struct netif *netif)
88 {
89  struct ethernetif *ethernetif = netif->state;
90 
91  /* set MAC hardware address length */
93 
94  /* set MAC hardware address */
95  netif->hwaddr[0] = 0x08;
96  netif->hwaddr[1] = 0x00;
97  netif->hwaddr[2] = 0x27;
98  netif->hwaddr[3] = 0x73;
99  netif->hwaddr[4] = 0xC1;
100  netif->hwaddr[5] = 0xB6;
101 
102  /* maximum transfer unit */
103  netif->mtu = 1500;
104 
105  /* device capabilities */
106  /* don't set NETIF_FLAG_ETHARP if this device is not an ethernet one */
108 
109 #if LWIP_IPV6 && LWIP_IPV6_MLD
110  /*
111  * For hardware/netifs that implement MAC filtering.
112  * All-nodes link-local is handled by default, so we must let the hardware know
113  * to allow multicast packets in.
114  * Should set mld_mac_filter previously. */
115  if (netif->mld_mac_filter != NULL) {
116  ip6_addr_t ip6_allnodes_ll;
117  ip6_addr_set_allnodes_linklocal(&ip6_allnodes_ll);
118  netif->mld_mac_filter(netif, &ip6_allnodes_ll, NETIF_ADD_MAC_FILTER);
119  }
120 #endif /* LWIP_IPV6 && LWIP_IPV6_MLD */
121 
122  /* Do whatever else is needed to initialize interface. */
123 }
124 
141 static err_t
142 low_level_output(struct netif *netif, struct pbuf *p)
143 {
144  struct ethernetif *ethernetif = netif->state;
145  struct pbuf *q;
146 
147  //MrOlsen 2017-12-17 LWIP initiate transfer();
148 
149 #if ETH_PAD_SIZE
150  pbuf_header(p, -ETH_PAD_SIZE); /* drop the padding word */
151 #endif
152 
153  for (q = p; q != NULL; q = q->next) {
154  /* Send the data from the pbuf to the interface, one pbuf at a
155  time. The size of the data in each pbuf is kept in the ->len
156  variable. */
157  //send data from(q->payload, q->len);
158  lnc_sendPacket(lnc, q->payload, q->len,0x0);
159  }
160 
161  //signal that packet should be sent();
162 
163  MIB2_STATS_NETIF_ADD(netif, ifoutoctets, p->tot_len);
164  if (((u8_t*)p->payload)[0] & 1) {
165  /* broadcast or multicast packet*/
166  MIB2_STATS_NETIF_INC(netif, ifoutnucastpkts);
167  }
168  else {
169  /* unicast packet */
170  MIB2_STATS_NETIF_INC(netif, ifoutucastpkts);
171  }
172  /* increase ifoutdiscards or ifouterrors on error */
173 
174 #if ETH_PAD_SIZE
175  pbuf_header(p, ETH_PAD_SIZE); /* reclaim the padding word */
176 #endif
177 
178  LINK_STATS_INC(link.xmit);
179 
180  return ERR_OK;
181 }
182 
191 static struct pbuf *
192 low_level_input(struct netif *netif)
193 {
194  struct ethernetif *ethernetif = netif->state;
195  struct pbuf *p, *q;
196  uint16_t len;
197  char *bufPtr;
198 
199  /* Obtain the size of the packet and put it into the "len"
200  variable. */
201  len = tmpBuf->length;
202  bufPtr = tmpBuf->buffer;
203 
204 #if ETH_PAD_SIZE
205  len += ETH_PAD_SIZE; /* allow room for Ethernet padding */
206 #endif
207 
208  /* We allocate a pbuf chain of pbufs from the pool. */
210 
211  if (p != NULL) {
212 
213 #if ETH_PAD_SIZE
214  pbuf_header(p, -ETH_PAD_SIZE); /* drop the padding word */
215 #endif
216 
217  /* We iterate over the pbuf chain until we have read the entire
218  * packet into the pbuf. */
219  for (q = p; q != NULL; q = q->next) {
220  /* Read enough bytes to fill this pbuf in the chain. The
221  * available data in the pbuf is given by the q->len
222  * variable.
223  * This does not necessarily have to be a memcpy, you can also preallocate
224  * pbufs for a DMA-enabled MAC and after receiving truncate it to the
225  * actually received size. In this case, ensure the tot_len member of the
226  * pbuf is the sum of the chained pbuf len members.
227  */
228  //read data into(q->payload, q->len);
229  bcopy(bufPtr, q->payload, q->len);
230  bufPtr += q->len;
231  }
232  //acknowledge that packet has been read();
233 
234  MIB2_STATS_NETIF_ADD(netif, ifinoctets, p->tot_len);
235  if (((u8_t*)p->payload)[0] & 1) {
236  /* broadcast or multicast packet*/
237  MIB2_STATS_NETIF_INC(netif, ifinnucastpkts);
238  }
239  else {
240  /* unicast packet*/
241  MIB2_STATS_NETIF_INC(netif, ifinucastpkts);
242  }
243 #if ETH_PAD_SIZE
244  pbuf_header(p, ETH_PAD_SIZE); /* reclaim the padding word */
245 #endif
246 
247  LINK_STATS_INC(link.recv);
248  }
249  else {
250  //MrOlsen 2017-12-17 LWIP drop packet();
251  LINK_STATS_INC(link.memerr);
252  LINK_STATS_INC(link.drop);
253  MIB2_STATS_NETIF_INC(netif, ifindiscards);
254  }
255 
256  return p;
257 }
258 
268 /* MrOlsen 2017-12-17 LWIP */
269 //static void ethernetif_input(struct netif *netif) {
270 void ethernetif_input(struct netif *netif) {
271  struct ethernetif *ethernetif;
272  struct eth_hdr *ethhdr;
273  struct pbuf *p;
274 
276 
277  /* move received packet into a new pbuf */
278  p = low_level_input(netif);
279  /* if no packet could be read, silently ignore this */
280  if (p != NULL) {
281  /* pass all packets to ethernet_input, which decides what packets it supports */
282  if (netif->input(p, netif) != ERR_OK) {
283  LWIP_DEBUGF(NETIF_DEBUG, ("ethernetif_input: IP input error\n"));
284  pbuf_free(p);
285  p = NULL;
286  }
287  }
288 }
289 
303  struct ethernetif *ethernetif;
304 
305  LWIP_ASSERT("netif != NULL", (netif != NULL));
306 
307  ethernetif = mem_malloc(sizeof(struct ethernetif));
308  if (ethernetif == NULL) {
309  LWIP_DEBUGF(NETIF_DEBUG, ("ethernetif_init: out of memory\n"));
310  return ERR_MEM;
311  }
312 
313 #if LWIP_NETIF_HOSTNAME
314  /* Initialize interface hostname */
315  netif->hostname = "lwip";
316 #endif /* LWIP_NETIF_HOSTNAME */
317 
318  /*
319  * Initialize the snmp variables and counters inside the struct netif.
320  * The last argument should be replaced with your link speed, in units
321  * of bits per second.
322  */
323  //MrOlsen 2017-12-17 LWIP MIB2_INIT_NETIF(netif, snmp_ifType_ethernet_csmacd, LINK_SPEED_OF_YOUR_NETIF_IN_BPS);
324 
326  netif->name[0] = IFNAME0;
327  netif->name[1] = IFNAME1;
328  /* We directly use etharp_output() here to save a function call.
329  * You can instead declare your own function an call etharp_output()
330  * from it if you have to do some checks before sending (e.g. if link
331  * is available...) */
332  netif->output = etharp_output;
333 #if LWIP_IPV6
334  netif->output_ip6 = ethip6_output;
335 #endif /* LWIP_IPV6 */
336  netif->linkoutput = low_level_output;
337 
338  ethernetif->ethaddr = (struct eth_addr *)&(netif->hwaddr[0]);
339 
340  /* initialize the hardware */
341  low_level_init(netif);
342 
343  return ERR_OK;
344 }
PBUF_POOL
Definition: pbuf.h:123
MIB2_STATS_NETIF_ADD
#define MIB2_STATS_NETIF_ADD(n, x, val)
Definition: snmp.h:140
opt.h
pbuf::len
u16_t len
Definition: pbuf.h:159
def.h
ethernetif::ethaddr
struct eth_addr * ethaddr
Definition: ethernetif.c:72
LWIP_ASSERT
#define LWIP_ASSERT(message, assertion)
Definition: debug.h:116
tcpip.h
MIB2_STATS_NETIF_INC
#define MIB2_STATS_NETIF_INC(n, x)
Definition: snmp.h:139
pbuf.h
PBUF_RAW
Definition: pbuf.h:94
nicBuffer::buffer
char * buffer
Definition: netif.h:55
netif::input
netif_input_fn input
Definition: netif.h:252
lnc_sendPacket
int lnc_sendPacket(struct lncInfo *lnc, void *packet, size_t len, uInt8 *dest)
Definition: lnc.c:546
pbuf::tot_len
u16_t tot_len
Definition: pbuf.h:156
ethernetif_init
err_t ethernetif_init(struct netif *netif)
Definition: ethernetif.c:302
NETIF_FLAG_ETHARP
#define NETIF_FLAG_ETHARP
Definition: netif.h:99
NETIF_DEBUG
#define NETIF_DEBUG
Definition: lwipopts.h:433
netif::state
void * state
Definition: netif.h:287
pbuf::next
struct pbuf * next
Definition: pbuf.h:144
LINK_STATS_INC
#define LINK_STATS_INC(x)
Definition: stats.h:384
pbuf_free
u8_t pbuf_free(struct pbuf *p)
Definition: pbuf.c:715
bcopy
#define bcopy(src, dst, len)
Definition: gpt.h:36
netif::mtu
u16_t mtu
Definition: netif.h:307
ethernetif
Definition: ethernetif.c:71
netif::flags
u8_t flags
Definition: netif.h:313
netif::linkoutput
netif_linkoutput_fn linkoutput
Definition: netif.h:263
NETIF_ADD_MAC_FILTER
Definition: netif.h:155
uint16_t
__uint16_t uint16_t
Definition: types.h:45
snmp.h
stats.h
ERR_MEM
Definition: err.h:65
netif::hwaddr
u8_t hwaddr[6U]
Definition: netif.h:311
pbuf_alloc
struct pbuf * pbuf_alloc(pbuf_layer l, u16_t length, pbuf_type type)
Definition: pbuf.c:248
u8_t
uint8_t u8_t
Definition: arch.h:122
lnc
struct lncInfo * lnc
Definition: lnc.c:44
nicBuffer
Definition: netif.h:52
eth_hdr
Definition: ethernet.h:71
netif
Definition: netif.h:233
ethernetif_input
void ethernetif_input(struct netif *netif)
Definition: ethernetif.c:270
netif::hwaddr_len
u8_t hwaddr_len
Definition: netif.h:309
netif::name
char name[2]
Definition: netif.h:315
IFNAME1
#define IFNAME1
Definition: ethernetif.c:61
ethip6.h
tmpBuf
struct nicBuffer * tmpBuf
Definition: ethernetif.c:63
ERR_OK
Definition: err.h:63
err_t
s8_t err_t
Definition: err.h:57
eth_addr
Definition: ethernet.h:58
IFNAME0
#define IFNAME0
Definition: ethernetif.c:60
nicBuffer::length
int length
Definition: netif.h:54
ETH_PAD_SIZE
#define ETH_PAD_SIZE
Definition: lwipopts.h:103
mem_malloc
void * mem_malloc(mem_size_t size)
Definition: mem.c:603
etharp.h
NETIF_FLAG_LINK_UP
#define NETIF_FLAG_LINK_UP
Definition: netif.h:95
mem.h
pbuf
Definition: pbuf.h:142
LWIP_DEBUGF
#define LWIP_DEBUGF(debug, message)
Definition: debug.h:164
pbuf_header
u8_t pbuf_header(struct pbuf *p, s16_t header_size)
Definition: pbuf.c:665
pbuf::payload
void * payload
Definition: pbuf.h:147
NETIF_FLAG_BROADCAST
#define NETIF_FLAG_BROADCAST
Definition: netif.h:89
lnc.h
ETHARP_HWADDR_LEN
#define ETHARP_HWADDR_LEN
Definition: etharp.h:49
NULL
#define NULL
Definition: fat_string.h:17