ia64/xen-unstable

view extras/mini-os/lwip-net.c @ 17275:7fc9767f966a

minios: Automatically set IP from XenStore information

Signed-off-by: Samuel Thibault <samuel.thibault@eu.citrix.com>
author Keir Fraser <keir.fraser@citrix.com>
date Wed Mar 19 16:20:14 2008 +0000 (2008-03-19)
parents 3c49ae5641b0
children 01686f69f48b
line source
1 /*
2 * lwip-net.c
3 *
4 * interface between lwIP's ethernet and Mini-os's netfront.
5 * For now, support only one network interface, as mini-os does.
6 *
7 * Tim Deegan <Tim.Deegan@eu.citrix.net>, July 2007
8 * based on lwIP's ethernetif.c skeleton file, copyrights as below.
9 */
12 /*
13 * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
14 * All rights reserved.
15 *
16 * Redistribution and use in source and binary forms, with or without modification,
17 * are permitted provided that the following conditions are met:
18 *
19 * 1. Redistributions of source code must retain the above copyright notice,
20 * this list of conditions and the following disclaimer.
21 * 2. Redistributions in binary form must reproduce the above copyright notice,
22 * this list of conditions and the following disclaimer in the documentation
23 * and/or other materials provided with the distribution.
24 * 3. The name of the author may not be used to endorse or promote products
25 * derived from this software without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
28 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
29 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
30 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
32 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
35 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
36 * OF SUCH DAMAGE.
37 *
38 * This file is part of the lwIP TCP/IP stack.
39 *
40 * Author: Adam Dunkels <adam@sics.se>
41 *
42 */
44 #include <os.h>
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"
52 #include <lwip/stats.h>
53 #include <lwip/sys.h>
54 #include <lwip/mem.h>
55 #include <lwip/memp.h>
56 #include <lwip/pbuf.h>
57 #include <netif/etharp.h>
58 #include <lwip/tcpip.h>
59 #include <lwip/tcp.h>
60 #include <lwip/netif.h>
61 #include <lwip/dhcp.h>
63 #include "netif/etharp.h"
65 #include <netfront.h>
67 /* Define those to better describe your network interface. */
68 #define IFNAME0 'e'
69 #define IFNAME1 'n'
71 #define IF_IPADDR 0x00000000
72 #define IF_NETMASK 0x00000000
74 /* Only have one network interface at a time. */
75 static struct netif *the_interface = NULL;
77 static unsigned char rawmac[6];
78 static struct netfront_dev *dev;
80 /* Forward declarations. */
81 static err_t netfront_output(struct netif *netif, struct pbuf *p,
82 struct ip_addr *ipaddr);
84 /*
85 * low_level_output():
86 *
87 * Should do the actual transmission of the packet. The packet is
88 * contained in the pbuf that is passed to the function. This pbuf
89 * might be chained.
90 *
91 */
93 static err_t
94 low_level_output(struct netif *netif, struct pbuf *p)
95 {
96 #ifdef ETH_PAD_SIZE
97 pbuf_header(p, -ETH_PAD_SIZE); /* drop the padding word */
98 #endif
100 /* Send the data from the pbuf to the interface, one pbuf at a
101 time. The size of the data in each pbuf is kept in the ->len
102 variable. */
103 if (!p->next) {
104 /* Only one fragment, can send it directly */
105 netfront_xmit(dev, p->payload, p->len);
106 } else {
107 unsigned char data[p->tot_len], *cur;
108 struct pbuf *q;
110 for(q = p, cur = data; q != NULL; cur += q->len, q = q->next)
111 memcpy(cur, q->payload, q->len);
112 netfront_xmit(dev, data, p->tot_len);
113 }
115 #if ETH_PAD_SIZE
116 pbuf_header(p, ETH_PAD_SIZE); /* reclaim the padding word */
117 #endif
119 LINK_STATS_INC(link.xmit);
121 return ERR_OK;
122 }
126 /*
127 * netfront_output():
128 *
129 * This function is called by the TCP/IP stack when an IP packet
130 * should be sent. It calls the function called low_level_output() to
131 * do the actual transmission of the packet.
132 *
133 */
135 static err_t
136 netfront_output(struct netif *netif, struct pbuf *p,
137 struct ip_addr *ipaddr)
138 {
140 /* resolve hardware address, then send (or queue) packet */
141 return etharp_output(netif, p, ipaddr);
143 }
145 /*
146 * netfront_input():
147 *
148 * This function should be called when a packet is ready to be read
149 * from the interface.
150 *
151 */
153 static void
154 netfront_input(struct netif *netif, unsigned char* data, int len)
155 {
156 struct eth_hdr *ethhdr;
157 struct pbuf *p, *q;
159 #if ETH_PAD_SIZE
160 len += ETH_PAD_SIZE; /* allow room for Ethernet padding */
161 #endif
163 /* move received packet into a new pbuf */
164 p = pbuf_alloc(PBUF_RAW, len, PBUF_POOL);
165 if (p == NULL) {
166 LINK_STATS_INC(link.memerr);
167 LINK_STATS_INC(link.drop);
168 return;
169 }
171 #if ETH_PAD_SIZE
172 pbuf_header(p, -ETH_PAD_SIZE); /* drop the padding word */
173 #endif
175 /* We iterate over the pbuf chain until we have read the entire
176 * packet into the pbuf. */
177 for(q = p; q != NULL && len > 0; q = q->next) {
178 /* Read enough bytes to fill this pbuf in the chain. The
179 * available data in the pbuf is given by the q->len
180 * variable. */
181 memcpy(q->payload, data, len < q->len ? len : q->len);
182 data += q->len;
183 len -= q->len;
184 }
186 #if ETH_PAD_SIZE
187 pbuf_header(p, ETH_PAD_SIZE); /* reclaim the padding word */
188 #endif
190 LINK_STATS_INC(link.recv);
192 /* points to packet payload, which starts with an Ethernet header */
193 ethhdr = p->payload;
195 ethhdr = p->payload;
197 switch (htons(ethhdr->type)) {
198 /* IP packet? */
199 case ETHTYPE_IP:
200 #if 0
201 /* CSi disabled ARP table update on ingress IP packets.
202 This seems to work but needs thorough testing. */
203 /* update ARP table */
204 etharp_ip_input(netif, p);
205 #endif
206 /* skip Ethernet header */
207 pbuf_header(p, -(s16)sizeof(struct eth_hdr));
208 /* pass to network layer */
209 tcpip_input(p, netif);
210 break;
212 case ETHTYPE_ARP:
213 /* pass p to ARP module */
214 etharp_arp_input(netif, (struct eth_addr *) netif->hwaddr, p);
215 break;
216 default:
217 pbuf_free(p);
218 p = NULL;
219 break;
220 }
221 }
224 /*
225 * netif_rx(): overrides the default netif_rx behaviour in the netfront driver.
226 *
227 * Pull received packets into a pbuf queue for the low_level_input()
228 * function to pass up to lwIP.
229 */
231 void netif_rx(unsigned char* data, int len)
232 {
233 if (the_interface != NULL) {
234 netfront_input(the_interface, data, len);
235 wake_up(&netfront_queue);
236 }
237 /* By returning, we ack the packet and relinquish the RX ring slot */
238 }
240 /*
241 * Set the IP, mask and gateway of the IF
242 */
243 void networking_set_addr(struct ip_addr *ipaddr, struct ip_addr *netmask, struct ip_addr *gw)
244 {
245 netif_set_ipaddr(the_interface, ipaddr);
246 netif_set_netmask(the_interface, netmask);
247 netif_set_gw(the_interface, gw);
248 }
251 static void
252 arp_timer(void *arg)
253 {
254 etharp_tmr();
255 sys_timeout(ARP_TMR_INTERVAL, arp_timer, NULL);
256 }
258 /*
259 * netif_netfront_init():
260 *
261 * Should be called at the beginning of the program to set up the
262 * network interface. It calls the function low_level_init() to do the
263 * actual setup of the hardware.
264 *
265 */
267 err_t
268 netif_netfront_init(struct netif *netif)
269 {
270 unsigned char *mac = netif->state;
272 #if LWIP_SNMP
273 /* ifType ethernetCsmacd(6) @see RFC1213 */
274 netif->link_type = 6;
275 /* your link speed here */
276 netif->link_speed = ;
277 netif->ts = 0;
278 netif->ifinoctets = 0;
279 netif->ifinucastpkts = 0;
280 netif->ifinnucastpkts = 0;
281 netif->ifindiscards = 0;
282 netif->ifoutoctets = 0;
283 netif->ifoutucastpkts = 0;
284 netif->ifoutnucastpkts = 0;
285 netif->ifoutdiscards = 0;
286 #endif
288 netif->name[0] = IFNAME0;
289 netif->name[1] = IFNAME1;
290 netif->output = netfront_output;
291 netif->linkoutput = low_level_output;
293 the_interface = netif;
295 /* set MAC hardware address */
296 netif->hwaddr_len = 6;
297 netif->hwaddr[0] = mac[0];
298 netif->hwaddr[1] = mac[1];
299 netif->hwaddr[2] = mac[2];
300 netif->hwaddr[3] = mac[3];
301 netif->hwaddr[4] = mac[4];
302 netif->hwaddr[5] = mac[5];
304 /* No interesting per-interface state */
305 netif->state = NULL;
307 /* maximum transfer unit */
308 netif->mtu = 1500;
310 /* broadcast capability */
311 netif->flags = NETIF_FLAG_BROADCAST;
313 etharp_init();
315 sys_timeout(ARP_TMR_INTERVAL, arp_timer, NULL);
317 return ERR_OK;
318 }
320 /*
321 * Thread run by netfront: bring up the IP address and fire lwIP timers.
322 */
323 static __DECLARE_SEMAPHORE_GENERIC(tcpip_is_up, 0);
324 static void tcpip_bringup_finished(void *p)
325 {
326 tprintk("TCP/IP bringup ends.\n");
327 up(&tcpip_is_up);
328 }
330 /*
331 * Utility function to bring the whole lot up. Call this from app_main()
332 * or similar -- it starts netfront and have lwIP start its thread,
333 * which calls back to tcpip_bringup_finished(), which
334 * lets us know it's OK to continue.
335 */
336 void start_networking(void)
337 {
338 struct netif *netif;
339 struct ip_addr ipaddr = { htonl(IF_IPADDR) };
340 struct ip_addr netmask = { htonl(IF_NETMASK) };
341 struct ip_addr gw = { 0 };
342 char *ip;
344 tprintk("Waiting for network.\n");
346 dev = init_netfront(NULL, NULL, rawmac, &ip);
348 if (ip) {
349 ipaddr.addr = inet_addr(ip);
350 if (IN_CLASSA(ntohl(ipaddr.addr)))
351 netmask.addr = htonl(IN_CLASSA_NET);
352 else if (IN_CLASSB(ntohl(ipaddr.addr)))
353 netmask.addr = htonl(IN_CLASSB_NET);
354 else if (IN_CLASSC(ntohl(ipaddr.addr)))
355 netmask.addr = htonl(IN_CLASSC_NET);
356 else
357 tprintk("Strange IP %s, leaving netmask to 0.\n", ip);
358 }
359 tprintk("IP %x netmask %x gateway %x.\n",
360 ntohl(ipaddr.addr), ntohl(netmask.addr), ntohl(gw.addr));
362 tprintk("TCP/IP bringup begins.\n");
364 netif = xmalloc(struct netif);
365 tcpip_init(tcpip_bringup_finished, netif);
367 netif_add(netif, &ipaddr, &netmask, &gw, rawmac,
368 netif_netfront_init, ip_input);
369 netif_set_default(netif);
370 netif_set_up(netif);
372 down(&tcpip_is_up);
374 tprintk("Network is ready.\n");
375 }