ia64/xen-unstable

annotate extras/mini-os/lwip-net.c @ 18811:390ef36eb596

Remove Xen-private definitions from kexec public header.

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