ia64/linux-2.6.18-xen.hg

view drivers/xen/netfront/netfront.c @ 470:5baef0e18e36

xenbus: prevent warnings on unhandled enumeration values

XenbusStateReconfiguring/XenbusStateReconfigured were introduced by
c/s 437, but aren't handled in many switch statements. This c/s also
introduced a possibly un-referenced label, which also gets eliminated
here.

Signed-off-by: Jan Beulich <jbeulich@novell.com>
author Keir Fraser <keir.fraser@citrix.com>
date Wed Mar 05 17:28:41 2008 +0000 (2008-03-05)
parents 2da185c11693
children ba72914de93a
line source
1 /******************************************************************************
2 * Virtual network driver for conversing with remote driver backends.
3 *
4 * Copyright (c) 2002-2005, K A Fraser
5 * Copyright (c) 2005, XenSource Ltd
6 * Copyright (C) 2007 Solarflare Communications, Inc.
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License version 2
10 * as published by the Free Software Foundation; or, when distributed
11 * separately from the Linux kernel or incorporated into other
12 * software packages, subject to the following license:
13 *
14 * Permission is hereby granted, free of charge, to any person obtaining a copy
15 * of this source file (the "Software"), to deal in the Software without
16 * restriction, including without limitation the rights to use, copy, modify,
17 * merge, publish, distribute, sublicense, and/or sell copies of the Software,
18 * and to permit persons to whom the Software is furnished to do so, subject to
19 * the following conditions:
20 *
21 * The above copyright notice and this permission notice shall be included in
22 * all copies or substantial portions of the Software.
23 *
24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
27 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
29 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
30 * IN THE SOFTWARE.
31 */
33 #include <linux/module.h>
34 #include <linux/version.h>
35 #include <linux/kernel.h>
36 #include <linux/sched.h>
37 #include <linux/slab.h>
38 #include <linux/string.h>
39 #include <linux/errno.h>
40 #include <linux/netdevice.h>
41 #include <linux/inetdevice.h>
42 #include <linux/etherdevice.h>
43 #include <linux/skbuff.h>
44 #include <linux/init.h>
45 #include <linux/bitops.h>
46 #include <linux/ethtool.h>
47 #include <linux/in.h>
48 #include <linux/if_ether.h>
49 #include <linux/io.h>
50 #include <linux/moduleparam.h>
51 #include <net/sock.h>
52 #include <net/pkt_sched.h>
53 #include <net/arp.h>
54 #include <net/route.h>
55 #include <asm/uaccess.h>
56 #include <xen/evtchn.h>
57 #include <xen/xenbus.h>
58 #include <xen/interface/io/netif.h>
59 #include <xen/interface/memory.h>
60 #include <xen/balloon.h>
61 #include <asm/page.h>
62 #include <asm/maddr.h>
63 #include <asm/uaccess.h>
64 #include <xen/interface/grant_table.h>
65 #include <xen/gnttab.h>
67 struct netfront_cb {
68 struct page *page;
69 unsigned offset;
70 };
72 #define NETFRONT_SKB_CB(skb) ((struct netfront_cb *)((skb)->cb))
74 #include "netfront.h"
76 /*
77 * Mutually-exclusive module options to select receive data path:
78 * rx_copy : Packets are copied by network backend into local memory
79 * rx_flip : Page containing packet data is transferred to our ownership
80 * For fully-virtualised guests there is no option - copying must be used.
81 * For paravirtualised guests, flipping is the default.
82 */
83 #ifdef CONFIG_XEN
84 static int MODPARM_rx_copy = 0;
85 module_param_named(rx_copy, MODPARM_rx_copy, bool, 0);
86 MODULE_PARM_DESC(rx_copy, "Copy packets from network card (rather than flip)");
87 static int MODPARM_rx_flip = 0;
88 module_param_named(rx_flip, MODPARM_rx_flip, bool, 0);
89 MODULE_PARM_DESC(rx_flip, "Flip packets from network card (rather than copy)");
90 #else
91 static const int MODPARM_rx_copy = 1;
92 static const int MODPARM_rx_flip = 0;
93 #endif
95 #define RX_COPY_THRESHOLD 256
97 /* If we don't have GSO, fake things up so that we never try to use it. */
98 #if defined(NETIF_F_GSO)
99 #define HAVE_GSO 1
100 #define HAVE_TSO 1 /* TSO is a subset of GSO */
101 #define HAVE_CSUM_OFFLOAD 1
102 static inline void dev_disable_gso_features(struct net_device *dev)
103 {
104 /* Turn off all GSO bits except ROBUST. */
105 dev->features &= (1 << NETIF_F_GSO_SHIFT) - 1;
106 dev->features |= NETIF_F_GSO_ROBUST;
107 }
108 #elif defined(NETIF_F_TSO)
109 #define HAVE_GSO 0
110 #define HAVE_TSO 1
112 /* Some older kernels cannot cope with incorrect checksums,
113 * particularly in netfilter. I'm not sure there is 100% correlation
114 * with the presence of NETIF_F_TSO but it appears to be a good first
115 * approximiation.
116 */
117 #define HAVE_CSUM_OFFLOAD 0
119 #define gso_size tso_size
120 #define gso_segs tso_segs
121 static inline void dev_disable_gso_features(struct net_device *dev)
122 {
123 /* Turn off all TSO bits. */
124 dev->features &= ~NETIF_F_TSO;
125 }
126 static inline int skb_is_gso(const struct sk_buff *skb)
127 {
128 return skb_shinfo(skb)->tso_size;
129 }
130 static inline int skb_gso_ok(struct sk_buff *skb, int features)
131 {
132 return (features & NETIF_F_TSO);
133 }
135 static inline int netif_needs_gso(struct net_device *dev, struct sk_buff *skb)
136 {
137 return skb_is_gso(skb) &&
138 (!skb_gso_ok(skb, dev->features) ||
139 unlikely(skb->ip_summed != CHECKSUM_HW));
140 }
141 #else
142 #define HAVE_GSO 0
143 #define HAVE_TSO 0
144 #define HAVE_CSUM_OFFLOAD 0
145 #define netif_needs_gso(dev, skb) 0
146 #define dev_disable_gso_features(dev) ((void)0)
147 #define ethtool_op_set_tso(dev, data) (-ENOSYS)
148 #endif
150 #define GRANT_INVALID_REF 0
152 struct netfront_rx_info {
153 struct netif_rx_response rx;
154 struct netif_extra_info extras[XEN_NETIF_EXTRA_TYPE_MAX - 1];
155 };
157 /*
158 * Implement our own carrier flag: the network stack's version causes delays
159 * when the carrier is re-enabled (in particular, dev_activate() may not
160 * immediately be called, which can cause packet loss).
161 */
162 #define netfront_carrier_on(netif) ((netif)->carrier = 1)
163 #define netfront_carrier_off(netif) ((netif)->carrier = 0)
164 #define netfront_carrier_ok(netif) ((netif)->carrier)
166 /*
167 * Access macros for acquiring freeing slots in tx_skbs[].
168 */
170 static inline void add_id_to_freelist(struct sk_buff **list, unsigned short id)
171 {
172 list[id] = list[0];
173 list[0] = (void *)(unsigned long)id;
174 }
176 static inline unsigned short get_id_from_freelist(struct sk_buff **list)
177 {
178 unsigned int id = (unsigned int)(unsigned long)list[0];
179 list[0] = list[id];
180 return id;
181 }
183 static inline int xennet_rxidx(RING_IDX idx)
184 {
185 return idx & (NET_RX_RING_SIZE - 1);
186 }
188 static inline struct sk_buff *xennet_get_rx_skb(struct netfront_info *np,
189 RING_IDX ri)
190 {
191 int i = xennet_rxidx(ri);
192 struct sk_buff *skb = np->rx_skbs[i];
193 np->rx_skbs[i] = NULL;
194 return skb;
195 }
197 static inline grant_ref_t xennet_get_rx_ref(struct netfront_info *np,
198 RING_IDX ri)
199 {
200 int i = xennet_rxidx(ri);
201 grant_ref_t ref = np->grant_rx_ref[i];
202 np->grant_rx_ref[i] = GRANT_INVALID_REF;
203 return ref;
204 }
206 #define DPRINTK(fmt, args...) \
207 pr_debug("netfront (%s:%d) " fmt, \
208 __FUNCTION__, __LINE__, ##args)
209 #define IPRINTK(fmt, args...) \
210 printk(KERN_INFO "netfront: " fmt, ##args)
211 #define WPRINTK(fmt, args...) \
212 printk(KERN_WARNING "netfront: " fmt, ##args)
214 static int setup_device(struct xenbus_device *, struct netfront_info *);
215 static struct net_device *create_netdev(struct xenbus_device *);
217 static void end_access(int, void *);
218 static void netif_disconnect_backend(struct netfront_info *);
220 static int network_connect(struct net_device *);
221 static void network_tx_buf_gc(struct net_device *);
222 static void network_alloc_rx_buffers(struct net_device *);
223 static void send_fake_arp(struct net_device *);
225 static irqreturn_t netif_int(int irq, void *dev_id, struct pt_regs *ptregs);
227 #ifdef CONFIG_SYSFS
228 static int xennet_sysfs_addif(struct net_device *netdev);
229 static void xennet_sysfs_delif(struct net_device *netdev);
230 #else /* !CONFIG_SYSFS */
231 #define xennet_sysfs_addif(dev) (0)
232 #define xennet_sysfs_delif(dev) do { } while(0)
233 #endif
235 static inline int xennet_can_sg(struct net_device *dev)
236 {
237 return dev->features & NETIF_F_SG;
238 }
240 /**
241 * Entry point to this code when a new device is created. Allocate the basic
242 * structures and the ring buffers for communication with the backend, and
243 * inform the backend of the appropriate details for those.
244 */
245 static int __devinit netfront_probe(struct xenbus_device *dev,
246 const struct xenbus_device_id *id)
247 {
248 int err;
249 struct net_device *netdev;
250 struct netfront_info *info;
252 netdev = create_netdev(dev);
253 if (IS_ERR(netdev)) {
254 err = PTR_ERR(netdev);
255 xenbus_dev_fatal(dev, err, "creating netdev");
256 return err;
257 }
259 info = netdev_priv(netdev);
260 dev->dev.driver_data = info;
262 err = register_netdev(info->netdev);
263 if (err) {
264 printk(KERN_WARNING "%s: register_netdev err=%d\n",
265 __FUNCTION__, err);
266 goto fail;
267 }
269 err = xennet_sysfs_addif(info->netdev);
270 if (err) {
271 unregister_netdev(info->netdev);
272 printk(KERN_WARNING "%s: add sysfs failed err=%d\n",
273 __FUNCTION__, err);
274 goto fail;
275 }
277 return 0;
279 fail:
280 free_netdev(netdev);
281 dev->dev.driver_data = NULL;
282 return err;
283 }
285 static int __devexit netfront_remove(struct xenbus_device *dev)
286 {
287 struct netfront_info *info = dev->dev.driver_data;
289 DPRINTK("%s\n", dev->nodename);
291 netfront_accelerator_call_remove(info, dev);
293 netif_disconnect_backend(info);
295 del_timer_sync(&info->rx_refill_timer);
297 xennet_sysfs_delif(info->netdev);
299 unregister_netdev(info->netdev);
301 free_netdev(info->netdev);
303 return 0;
304 }
307 static int netfront_suspend(struct xenbus_device *dev)
308 {
309 struct netfront_info *info = dev->dev.driver_data;
310 return netfront_accelerator_suspend(info, dev);
311 }
314 static int netfront_suspend_cancel(struct xenbus_device *dev)
315 {
316 struct netfront_info *info = dev->dev.driver_data;
317 return netfront_accelerator_suspend_cancel(info, dev);
318 }
321 /**
322 * We are reconnecting to the backend, due to a suspend/resume, or a backend
323 * driver restart. We tear down our netif structure and recreate it, but
324 * leave the device-layer structures intact so that this is transparent to the
325 * rest of the kernel.
326 */
327 static int netfront_resume(struct xenbus_device *dev)
328 {
329 struct netfront_info *info = dev->dev.driver_data;
331 DPRINTK("%s\n", dev->nodename);
333 netfront_accelerator_resume(info, dev);
335 netif_disconnect_backend(info);
336 return 0;
337 }
339 static int xen_net_read_mac(struct xenbus_device *dev, u8 mac[])
340 {
341 char *s, *e, *macstr;
342 int i;
344 macstr = s = xenbus_read(XBT_NIL, dev->nodename, "mac", NULL);
345 if (IS_ERR(macstr))
346 return PTR_ERR(macstr);
348 for (i = 0; i < ETH_ALEN; i++) {
349 mac[i] = simple_strtoul(s, &e, 16);
350 if ((s == e) || (*e != ((i == ETH_ALEN-1) ? '\0' : ':'))) {
351 kfree(macstr);
352 return -ENOENT;
353 }
354 s = e+1;
355 }
357 kfree(macstr);
358 return 0;
359 }
361 /* Common code used when first setting up, and when resuming. */
362 static int talk_to_backend(struct xenbus_device *dev,
363 struct netfront_info *info)
364 {
365 const char *message;
366 struct xenbus_transaction xbt;
367 int err;
369 err = xen_net_read_mac(dev, info->mac);
370 if (err) {
371 xenbus_dev_fatal(dev, err, "parsing %s/mac", dev->nodename);
372 goto out;
373 }
375 /* Create shared ring, alloc event channel. */
376 err = setup_device(dev, info);
377 if (err)
378 goto out;
380 /* This will load an accelerator if one is configured when the
381 * watch fires */
382 netfront_accelerator_add_watch(info);
384 again:
385 err = xenbus_transaction_start(&xbt);
386 if (err) {
387 xenbus_dev_fatal(dev, err, "starting transaction");
388 goto destroy_ring;
389 }
391 err = xenbus_printf(xbt, dev->nodename, "tx-ring-ref","%u",
392 info->tx_ring_ref);
393 if (err) {
394 message = "writing tx ring-ref";
395 goto abort_transaction;
396 }
397 err = xenbus_printf(xbt, dev->nodename, "rx-ring-ref","%u",
398 info->rx_ring_ref);
399 if (err) {
400 message = "writing rx ring-ref";
401 goto abort_transaction;
402 }
403 err = xenbus_printf(xbt, dev->nodename,
404 "event-channel", "%u",
405 irq_to_evtchn_port(info->irq));
406 if (err) {
407 message = "writing event-channel";
408 goto abort_transaction;
409 }
411 err = xenbus_printf(xbt, dev->nodename, "request-rx-copy", "%u",
412 info->copying_receiver);
413 if (err) {
414 message = "writing request-rx-copy";
415 goto abort_transaction;
416 }
418 err = xenbus_printf(xbt, dev->nodename, "feature-rx-notify", "%d", 1);
419 if (err) {
420 message = "writing feature-rx-notify";
421 goto abort_transaction;
422 }
424 err = xenbus_printf(xbt, dev->nodename, "feature-no-csum-offload",
425 "%d", !HAVE_CSUM_OFFLOAD);
426 if (err) {
427 message = "writing feature-no-csum-offload";
428 goto abort_transaction;
429 }
431 err = xenbus_printf(xbt, dev->nodename, "feature-sg", "%d", 1);
432 if (err) {
433 message = "writing feature-sg";
434 goto abort_transaction;
435 }
437 err = xenbus_printf(xbt, dev->nodename, "feature-gso-tcpv4", "%d",
438 HAVE_TSO);
439 if (err) {
440 message = "writing feature-gso-tcpv4";
441 goto abort_transaction;
442 }
444 err = xenbus_transaction_end(xbt, 0);
445 if (err) {
446 if (err == -EAGAIN)
447 goto again;
448 xenbus_dev_fatal(dev, err, "completing transaction");
449 goto destroy_ring;
450 }
452 return 0;
454 abort_transaction:
455 xenbus_transaction_end(xbt, 1);
456 xenbus_dev_fatal(dev, err, "%s", message);
457 destroy_ring:
458 netfront_accelerator_call_remove(info, dev);
459 netif_disconnect_backend(info);
460 out:
461 return err;
462 }
464 static int setup_device(struct xenbus_device *dev, struct netfront_info *info)
465 {
466 struct netif_tx_sring *txs;
467 struct netif_rx_sring *rxs;
468 int err;
469 struct net_device *netdev = info->netdev;
471 info->tx_ring_ref = GRANT_INVALID_REF;
472 info->rx_ring_ref = GRANT_INVALID_REF;
473 info->rx.sring = NULL;
474 info->tx.sring = NULL;
475 info->irq = 0;
477 txs = (struct netif_tx_sring *)get_zeroed_page(GFP_KERNEL|__GFP_HIGH);
478 if (!txs) {
479 err = -ENOMEM;
480 xenbus_dev_fatal(dev, err, "allocating tx ring page");
481 goto fail;
482 }
483 SHARED_RING_INIT(txs);
484 FRONT_RING_INIT(&info->tx, txs, PAGE_SIZE);
486 err = xenbus_grant_ring(dev, virt_to_mfn(txs));
487 if (err < 0) {
488 free_page((unsigned long)txs);
489 goto fail;
490 }
491 info->tx_ring_ref = err;
493 rxs = (struct netif_rx_sring *)get_zeroed_page(GFP_KERNEL|__GFP_HIGH);
494 if (!rxs) {
495 err = -ENOMEM;
496 xenbus_dev_fatal(dev, err, "allocating rx ring page");
497 goto fail;
498 }
499 SHARED_RING_INIT(rxs);
500 FRONT_RING_INIT(&info->rx, rxs, PAGE_SIZE);
502 err = xenbus_grant_ring(dev, virt_to_mfn(rxs));
503 if (err < 0) {
504 free_page((unsigned long)rxs);
505 goto fail;
506 }
507 info->rx_ring_ref = err;
509 memcpy(netdev->dev_addr, info->mac, ETH_ALEN);
511 err = bind_listening_port_to_irqhandler(
512 dev->otherend_id, netif_int, SA_SAMPLE_RANDOM, netdev->name,
513 netdev);
514 if (err < 0)
515 goto fail;
516 info->irq = err;
518 return 0;
520 fail:
521 return err;
522 }
524 /**
525 * Callback received when the backend's state changes.
526 */
527 static void backend_changed(struct xenbus_device *dev,
528 enum xenbus_state backend_state)
529 {
530 struct netfront_info *np = dev->dev.driver_data;
531 struct net_device *netdev = np->netdev;
533 DPRINTK("%s\n", xenbus_strstate(backend_state));
535 switch (backend_state) {
536 case XenbusStateInitialising:
537 case XenbusStateInitialised:
538 case XenbusStateConnected:
539 case XenbusStateReconfiguring:
540 case XenbusStateReconfigured:
541 case XenbusStateUnknown:
542 case XenbusStateClosed:
543 break;
545 case XenbusStateInitWait:
546 if (dev->state != XenbusStateInitialising)
547 break;
548 if (network_connect(netdev) != 0)
549 break;
550 xenbus_switch_state(dev, XenbusStateConnected);
551 send_fake_arp(netdev);
552 break;
554 case XenbusStateClosing:
555 xenbus_frontend_closed(dev);
556 break;
557 }
558 }
560 /** Send a packet on a net device to encourage switches to learn the
561 * MAC. We send a fake ARP request.
562 *
563 * @param dev device
564 * @return 0 on success, error code otherwise
565 */
566 static void send_fake_arp(struct net_device *dev)
567 {
568 #ifdef CONFIG_INET
569 struct sk_buff *skb;
570 u32 src_ip, dst_ip;
572 dst_ip = INADDR_BROADCAST;
573 src_ip = inet_select_addr(dev, dst_ip, RT_SCOPE_LINK);
575 /* No IP? Then nothing to do. */
576 if (src_ip == 0)
577 return;
579 skb = arp_create(ARPOP_REPLY, ETH_P_ARP,
580 dst_ip, dev, src_ip,
581 /*dst_hw*/ NULL, /*src_hw*/ NULL,
582 /*target_hw*/ dev->dev_addr);
583 if (skb == NULL)
584 return;
586 dev_queue_xmit(skb);
587 #endif
588 }
590 static inline int netfront_tx_slot_available(struct netfront_info *np)
591 {
592 return ((np->tx.req_prod_pvt - np->tx.rsp_cons) <
593 (TX_MAX_TARGET - MAX_SKB_FRAGS - 2));
594 }
597 static inline void network_maybe_wake_tx(struct net_device *dev)
598 {
599 struct netfront_info *np = netdev_priv(dev);
601 if (unlikely(netif_queue_stopped(dev)) &&
602 netfront_tx_slot_available(np) &&
603 likely(netif_running(dev)) &&
604 netfront_check_accelerator_queue_ready(dev, np))
605 netif_wake_queue(dev);
606 }
609 int netfront_check_queue_ready(struct net_device *dev)
610 {
611 struct netfront_info *np = netdev_priv(dev);
613 return unlikely(netif_queue_stopped(dev)) &&
614 netfront_tx_slot_available(np) &&
615 likely(netif_running(dev));
616 }
617 EXPORT_SYMBOL(netfront_check_queue_ready);
620 static int network_open(struct net_device *dev)
621 {
622 struct netfront_info *np = netdev_priv(dev);
624 memset(&np->stats, 0, sizeof(np->stats));
626 spin_lock_bh(&np->rx_lock);
627 if (netfront_carrier_ok(np)) {
628 network_alloc_rx_buffers(dev);
629 np->rx.sring->rsp_event = np->rx.rsp_cons + 1;
630 if (RING_HAS_UNCONSUMED_RESPONSES(&np->rx)){
631 netfront_accelerator_call_stop_napi_irq(np, dev);
633 netif_rx_schedule(dev);
634 }
635 }
636 spin_unlock_bh(&np->rx_lock);
638 network_maybe_wake_tx(dev);
640 return 0;
641 }
643 static void network_tx_buf_gc(struct net_device *dev)
644 {
645 RING_IDX cons, prod;
646 unsigned short id;
647 struct netfront_info *np = netdev_priv(dev);
648 struct sk_buff *skb;
650 BUG_ON(!netfront_carrier_ok(np));
652 do {
653 prod = np->tx.sring->rsp_prod;
654 rmb(); /* Ensure we see responses up to 'rp'. */
656 for (cons = np->tx.rsp_cons; cons != prod; cons++) {
657 struct netif_tx_response *txrsp;
659 txrsp = RING_GET_RESPONSE(&np->tx, cons);
660 if (txrsp->status == NETIF_RSP_NULL)
661 continue;
663 id = txrsp->id;
664 skb = np->tx_skbs[id];
665 if (unlikely(gnttab_query_foreign_access(
666 np->grant_tx_ref[id]) != 0)) {
667 printk(KERN_ALERT "network_tx_buf_gc: warning "
668 "-- grant still in use by backend "
669 "domain.\n");
670 BUG();
671 }
672 gnttab_end_foreign_access_ref(np->grant_tx_ref[id]);
673 gnttab_release_grant_reference(
674 &np->gref_tx_head, np->grant_tx_ref[id]);
675 np->grant_tx_ref[id] = GRANT_INVALID_REF;
676 add_id_to_freelist(np->tx_skbs, id);
677 dev_kfree_skb_irq(skb);
678 }
680 np->tx.rsp_cons = prod;
682 /*
683 * Set a new event, then check for race with update of tx_cons.
684 * Note that it is essential to schedule a callback, no matter
685 * how few buffers are pending. Even if there is space in the
686 * transmit ring, higher layers may be blocked because too much
687 * data is outstanding: in such cases notification from Xen is
688 * likely to be the only kick that we'll get.
689 */
690 np->tx.sring->rsp_event =
691 prod + ((np->tx.sring->req_prod - prod) >> 1) + 1;
692 mb();
693 } while ((cons == prod) && (prod != np->tx.sring->rsp_prod));
695 network_maybe_wake_tx(dev);
696 }
698 static void rx_refill_timeout(unsigned long data)
699 {
700 struct net_device *dev = (struct net_device *)data;
701 struct netfront_info *np = netdev_priv(dev);
703 netfront_accelerator_call_stop_napi_irq(np, dev);
705 netif_rx_schedule(dev);
706 }
708 static void network_alloc_rx_buffers(struct net_device *dev)
709 {
710 unsigned short id;
711 struct netfront_info *np = netdev_priv(dev);
712 struct sk_buff *skb;
713 struct page *page;
714 int i, batch_target, notify;
715 RING_IDX req_prod = np->rx.req_prod_pvt;
716 struct xen_memory_reservation reservation;
717 grant_ref_t ref;
718 unsigned long pfn;
719 void *vaddr;
720 int nr_flips;
721 netif_rx_request_t *req;
723 if (unlikely(!netfront_carrier_ok(np)))
724 return;
726 /*
727 * Allocate skbuffs greedily, even though we batch updates to the
728 * receive ring. This creates a less bursty demand on the memory
729 * allocator, so should reduce the chance of failed allocation requests
730 * both for ourself and for other kernel subsystems.
731 */
732 batch_target = np->rx_target - (req_prod - np->rx.rsp_cons);
733 for (i = skb_queue_len(&np->rx_batch); i < batch_target; i++) {
734 /*
735 * Allocate an skb and a page. Do not use __dev_alloc_skb as
736 * that will allocate page-sized buffers which is not
737 * necessary here.
738 * 16 bytes added as necessary headroom for netif_receive_skb.
739 */
740 skb = alloc_skb(RX_COPY_THRESHOLD + 16 + NET_IP_ALIGN,
741 GFP_ATOMIC | __GFP_NOWARN);
742 if (unlikely(!skb))
743 goto no_skb;
745 page = alloc_page(GFP_ATOMIC | __GFP_NOWARN);
746 if (!page) {
747 kfree_skb(skb);
748 no_skb:
749 /* Any skbuffs queued for refill? Force them out. */
750 if (i != 0)
751 goto refill;
752 /* Could not allocate any skbuffs. Try again later. */
753 mod_timer(&np->rx_refill_timer,
754 jiffies + (HZ/10));
755 break;
756 }
758 skb_reserve(skb, 16 + NET_IP_ALIGN); /* mimic dev_alloc_skb() */
759 skb_shinfo(skb)->frags[0].page = page;
760 skb_shinfo(skb)->nr_frags = 1;
761 __skb_queue_tail(&np->rx_batch, skb);
762 }
764 /* Is the batch large enough to be worthwhile? */
765 if (i < (np->rx_target/2)) {
766 if (req_prod > np->rx.sring->req_prod)
767 goto push;
768 return;
769 }
771 /* Adjust our fill target if we risked running out of buffers. */
772 if (((req_prod - np->rx.sring->rsp_prod) < (np->rx_target / 4)) &&
773 ((np->rx_target *= 2) > np->rx_max_target))
774 np->rx_target = np->rx_max_target;
776 refill:
777 for (nr_flips = i = 0; ; i++) {
778 if ((skb = __skb_dequeue(&np->rx_batch)) == NULL)
779 break;
781 skb->dev = dev;
783 id = xennet_rxidx(req_prod + i);
785 BUG_ON(np->rx_skbs[id]);
786 np->rx_skbs[id] = skb;
788 ref = gnttab_claim_grant_reference(&np->gref_rx_head);
789 BUG_ON((signed short)ref < 0);
790 np->grant_rx_ref[id] = ref;
792 pfn = page_to_pfn(skb_shinfo(skb)->frags[0].page);
793 vaddr = page_address(skb_shinfo(skb)->frags[0].page);
795 req = RING_GET_REQUEST(&np->rx, req_prod + i);
796 if (!np->copying_receiver) {
797 gnttab_grant_foreign_transfer_ref(ref,
798 np->xbdev->otherend_id,
799 pfn);
800 np->rx_pfn_array[nr_flips] = pfn_to_mfn(pfn);
801 if (!xen_feature(XENFEAT_auto_translated_physmap)) {
802 /* Remove this page before passing
803 * back to Xen. */
804 set_phys_to_machine(pfn, INVALID_P2M_ENTRY);
805 MULTI_update_va_mapping(np->rx_mcl+i,
806 (unsigned long)vaddr,
807 __pte(0), 0);
808 }
809 nr_flips++;
810 } else {
811 gnttab_grant_foreign_access_ref(ref,
812 np->xbdev->otherend_id,
813 pfn_to_mfn(pfn),
814 0);
815 }
817 req->id = id;
818 req->gref = ref;
819 }
821 if ( nr_flips != 0 ) {
822 /* Tell the ballon driver what is going on. */
823 balloon_update_driver_allowance(i);
825 set_xen_guest_handle(reservation.extent_start,
826 np->rx_pfn_array);
827 reservation.nr_extents = nr_flips;
828 reservation.extent_order = 0;
829 reservation.address_bits = 0;
830 reservation.domid = DOMID_SELF;
832 if (!xen_feature(XENFEAT_auto_translated_physmap)) {
833 /* After all PTEs have been zapped, flush the TLB. */
834 np->rx_mcl[i-1].args[MULTI_UVMFLAGS_INDEX] =
835 UVMF_TLB_FLUSH|UVMF_ALL;
837 /* Give away a batch of pages. */
838 np->rx_mcl[i].op = __HYPERVISOR_memory_op;
839 np->rx_mcl[i].args[0] = XENMEM_decrease_reservation;
840 np->rx_mcl[i].args[1] = (unsigned long)&reservation;
842 /* Zap PTEs and give away pages in one big
843 * multicall. */
844 if (unlikely(HYPERVISOR_multicall(np->rx_mcl, i+1)))
845 BUG();
847 /* Check return status of HYPERVISOR_memory_op(). */
848 if (unlikely(np->rx_mcl[i].result != i))
849 panic("Unable to reduce memory reservation\n");
850 while (i--)
851 BUG_ON(np->rx_mcl[i].result);
852 } else {
853 if (HYPERVISOR_memory_op(XENMEM_decrease_reservation,
854 &reservation) != i)
855 panic("Unable to reduce memory reservation\n");
856 }
857 } else {
858 wmb();
859 }
861 /* Above is a suitable barrier to ensure backend will see requests. */
862 np->rx.req_prod_pvt = req_prod + i;
863 push:
864 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&np->rx, notify);
865 if (notify)
866 notify_remote_via_irq(np->irq);
867 }
869 static void xennet_make_frags(struct sk_buff *skb, struct net_device *dev,
870 struct netif_tx_request *tx)
871 {
872 struct netfront_info *np = netdev_priv(dev);
873 char *data = skb->data;
874 unsigned long mfn;
875 RING_IDX prod = np->tx.req_prod_pvt;
876 int frags = skb_shinfo(skb)->nr_frags;
877 unsigned int offset = offset_in_page(data);
878 unsigned int len = skb_headlen(skb);
879 unsigned int id;
880 grant_ref_t ref;
881 int i;
883 while (len > PAGE_SIZE - offset) {
884 tx->size = PAGE_SIZE - offset;
885 tx->flags |= NETTXF_more_data;
886 len -= tx->size;
887 data += tx->size;
888 offset = 0;
890 id = get_id_from_freelist(np->tx_skbs);
891 np->tx_skbs[id] = skb_get(skb);
892 tx = RING_GET_REQUEST(&np->tx, prod++);
893 tx->id = id;
894 ref = gnttab_claim_grant_reference(&np->gref_tx_head);
895 BUG_ON((signed short)ref < 0);
897 mfn = virt_to_mfn(data);
898 gnttab_grant_foreign_access_ref(ref, np->xbdev->otherend_id,
899 mfn, GTF_readonly);
901 tx->gref = np->grant_tx_ref[id] = ref;
902 tx->offset = offset;
903 tx->size = len;
904 tx->flags = 0;
905 }
907 for (i = 0; i < frags; i++) {
908 skb_frag_t *frag = skb_shinfo(skb)->frags + i;
910 tx->flags |= NETTXF_more_data;
912 id = get_id_from_freelist(np->tx_skbs);
913 np->tx_skbs[id] = skb_get(skb);
914 tx = RING_GET_REQUEST(&np->tx, prod++);
915 tx->id = id;
916 ref = gnttab_claim_grant_reference(&np->gref_tx_head);
917 BUG_ON((signed short)ref < 0);
919 mfn = pfn_to_mfn(page_to_pfn(frag->page));
920 gnttab_grant_foreign_access_ref(ref, np->xbdev->otherend_id,
921 mfn, GTF_readonly);
923 tx->gref = np->grant_tx_ref[id] = ref;
924 tx->offset = frag->page_offset;
925 tx->size = frag->size;
926 tx->flags = 0;
927 }
929 np->tx.req_prod_pvt = prod;
930 }
932 static int network_start_xmit(struct sk_buff *skb, struct net_device *dev)
933 {
934 unsigned short id;
935 struct netfront_info *np = netdev_priv(dev);
936 struct netif_tx_request *tx;
937 struct netif_extra_info *extra;
938 char *data = skb->data;
939 RING_IDX i;
940 grant_ref_t ref;
941 unsigned long mfn;
942 int notify;
943 int frags = skb_shinfo(skb)->nr_frags;
944 unsigned int offset = offset_in_page(data);
945 unsigned int len = skb_headlen(skb);
947 /* Check the fast path, if hooks are available */
948 if (np->accel_vif_state.hooks &&
949 np->accel_vif_state.hooks->start_xmit(skb, dev)) {
950 /* Fast path has sent this packet */
951 return 0;
952 }
954 frags += (offset + len + PAGE_SIZE - 1) / PAGE_SIZE;
955 if (unlikely(frags > MAX_SKB_FRAGS + 1)) {
956 printk(KERN_ALERT "xennet: skb rides the rocket: %d frags\n",
957 frags);
958 dump_stack();
959 goto drop;
960 }
962 spin_lock_irq(&np->tx_lock);
964 if (unlikely(!netfront_carrier_ok(np) ||
965 (frags > 1 && !xennet_can_sg(dev)) ||
966 netif_needs_gso(dev, skb))) {
967 spin_unlock_irq(&np->tx_lock);
968 goto drop;
969 }
971 i = np->tx.req_prod_pvt;
973 id = get_id_from_freelist(np->tx_skbs);
974 np->tx_skbs[id] = skb;
976 tx = RING_GET_REQUEST(&np->tx, i);
978 tx->id = id;
979 ref = gnttab_claim_grant_reference(&np->gref_tx_head);
980 BUG_ON((signed short)ref < 0);
981 mfn = virt_to_mfn(data);
982 gnttab_grant_foreign_access_ref(
983 ref, np->xbdev->otherend_id, mfn, GTF_readonly);
984 tx->gref = np->grant_tx_ref[id] = ref;
985 tx->offset = offset;
986 tx->size = len;
988 tx->flags = 0;
989 extra = NULL;
991 if (skb->ip_summed == CHECKSUM_HW) /* local packet? */
992 tx->flags |= NETTXF_csum_blank | NETTXF_data_validated;
993 #ifdef CONFIG_XEN
994 if (skb->proto_data_valid) /* remote but checksummed? */
995 tx->flags |= NETTXF_data_validated;
996 #endif
998 #if HAVE_TSO
999 if (skb_shinfo(skb)->gso_size) {
1000 struct netif_extra_info *gso = (struct netif_extra_info *)
1001 RING_GET_REQUEST(&np->tx, ++i);
1003 if (extra)
1004 extra->flags |= XEN_NETIF_EXTRA_FLAG_MORE;
1005 else
1006 tx->flags |= NETTXF_extra_info;
1008 gso->u.gso.size = skb_shinfo(skb)->gso_size;
1009 gso->u.gso.type = XEN_NETIF_GSO_TYPE_TCPV4;
1010 gso->u.gso.pad = 0;
1011 gso->u.gso.features = 0;
1013 gso->type = XEN_NETIF_EXTRA_TYPE_GSO;
1014 gso->flags = 0;
1015 extra = gso;
1017 #endif
1019 np->tx.req_prod_pvt = i + 1;
1021 xennet_make_frags(skb, dev, tx);
1022 tx->size = skb->len;
1024 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&np->tx, notify);
1025 if (notify)
1026 notify_remote_via_irq(np->irq);
1028 np->stats.tx_bytes += skb->len;
1029 np->stats.tx_packets++;
1031 /* Note: It is not safe to access skb after network_tx_buf_gc()! */
1032 network_tx_buf_gc(dev);
1034 if (!netfront_tx_slot_available(np))
1035 netif_stop_queue(dev);
1037 spin_unlock_irq(&np->tx_lock);
1039 return 0;
1041 drop:
1042 np->stats.tx_dropped++;
1043 dev_kfree_skb(skb);
1044 return 0;
1047 static irqreturn_t netif_int(int irq, void *dev_id, struct pt_regs *ptregs)
1049 struct net_device *dev = dev_id;
1050 struct netfront_info *np = netdev_priv(dev);
1051 unsigned long flags;
1053 spin_lock_irqsave(&np->tx_lock, flags);
1055 if (likely(netfront_carrier_ok(np))) {
1056 network_tx_buf_gc(dev);
1057 /* Under tx_lock: protects access to rx shared-ring indexes. */
1058 if (RING_HAS_UNCONSUMED_RESPONSES(&np->rx)) {
1059 netfront_accelerator_call_stop_napi_irq(np, dev);
1061 netif_rx_schedule(dev);
1065 spin_unlock_irqrestore(&np->tx_lock, flags);
1067 return IRQ_HANDLED;
1070 static void xennet_move_rx_slot(struct netfront_info *np, struct sk_buff *skb,
1071 grant_ref_t ref)
1073 int new = xennet_rxidx(np->rx.req_prod_pvt);
1075 BUG_ON(np->rx_skbs[new]);
1076 np->rx_skbs[new] = skb;
1077 np->grant_rx_ref[new] = ref;
1078 RING_GET_REQUEST(&np->rx, np->rx.req_prod_pvt)->id = new;
1079 RING_GET_REQUEST(&np->rx, np->rx.req_prod_pvt)->gref = ref;
1080 np->rx.req_prod_pvt++;
1083 int xennet_get_extras(struct netfront_info *np,
1084 struct netif_extra_info *extras, RING_IDX rp)
1087 struct netif_extra_info *extra;
1088 RING_IDX cons = np->rx.rsp_cons;
1089 int err = 0;
1091 do {
1092 struct sk_buff *skb;
1093 grant_ref_t ref;
1095 if (unlikely(cons + 1 == rp)) {
1096 if (net_ratelimit())
1097 WPRINTK("Missing extra info\n");
1098 err = -EBADR;
1099 break;
1102 extra = (struct netif_extra_info *)
1103 RING_GET_RESPONSE(&np->rx, ++cons);
1105 if (unlikely(!extra->type ||
1106 extra->type >= XEN_NETIF_EXTRA_TYPE_MAX)) {
1107 if (net_ratelimit())
1108 WPRINTK("Invalid extra type: %d\n",
1109 extra->type);
1110 err = -EINVAL;
1111 } else {
1112 memcpy(&extras[extra->type - 1], extra,
1113 sizeof(*extra));
1116 skb = xennet_get_rx_skb(np, cons);
1117 ref = xennet_get_rx_ref(np, cons);
1118 xennet_move_rx_slot(np, skb, ref);
1119 } while (extra->flags & XEN_NETIF_EXTRA_FLAG_MORE);
1121 np->rx.rsp_cons = cons;
1122 return err;
1125 static int xennet_get_responses(struct netfront_info *np,
1126 struct netfront_rx_info *rinfo, RING_IDX rp,
1127 struct sk_buff_head *list,
1128 int *pages_flipped_p)
1130 int pages_flipped = *pages_flipped_p;
1131 struct mmu_update *mmu;
1132 struct multicall_entry *mcl;
1133 struct netif_rx_response *rx = &rinfo->rx;
1134 struct netif_extra_info *extras = rinfo->extras;
1135 RING_IDX cons = np->rx.rsp_cons;
1136 struct sk_buff *skb = xennet_get_rx_skb(np, cons);
1137 grant_ref_t ref = xennet_get_rx_ref(np, cons);
1138 int max = MAX_SKB_FRAGS + (rx->status <= RX_COPY_THRESHOLD);
1139 int frags = 1;
1140 int err = 0;
1141 unsigned long ret;
1143 if (rx->flags & NETRXF_extra_info) {
1144 err = xennet_get_extras(np, extras, rp);
1145 cons = np->rx.rsp_cons;
1148 for (;;) {
1149 unsigned long mfn;
1151 if (unlikely(rx->status < 0 ||
1152 rx->offset + rx->status > PAGE_SIZE)) {
1153 if (net_ratelimit())
1154 WPRINTK("rx->offset: %x, size: %u\n",
1155 rx->offset, rx->status);
1156 xennet_move_rx_slot(np, skb, ref);
1157 err = -EINVAL;
1158 goto next;
1161 /*
1162 * This definitely indicates a bug, either in this driver or in
1163 * the backend driver. In future this should flag the bad
1164 * situation to the system controller to reboot the backed.
1165 */
1166 if (ref == GRANT_INVALID_REF) {
1167 if (net_ratelimit())
1168 WPRINTK("Bad rx response id %d.\n", rx->id);
1169 err = -EINVAL;
1170 goto next;
1173 if (!np->copying_receiver) {
1174 /* Memory pressure, insufficient buffer
1175 * headroom, ... */
1176 if (!(mfn = gnttab_end_foreign_transfer_ref(ref))) {
1177 if (net_ratelimit())
1178 WPRINTK("Unfulfilled rx req "
1179 "(id=%d, st=%d).\n",
1180 rx->id, rx->status);
1181 xennet_move_rx_slot(np, skb, ref);
1182 err = -ENOMEM;
1183 goto next;
1186 if (!xen_feature(XENFEAT_auto_translated_physmap)) {
1187 /* Remap the page. */
1188 struct page *page =
1189 skb_shinfo(skb)->frags[0].page;
1190 unsigned long pfn = page_to_pfn(page);
1191 void *vaddr = page_address(page);
1193 mcl = np->rx_mcl + pages_flipped;
1194 mmu = np->rx_mmu + pages_flipped;
1196 MULTI_update_va_mapping(mcl,
1197 (unsigned long)vaddr,
1198 pfn_pte_ma(mfn,
1199 PAGE_KERNEL),
1200 0);
1201 mmu->ptr = ((maddr_t)mfn << PAGE_SHIFT)
1202 | MMU_MACHPHYS_UPDATE;
1203 mmu->val = pfn;
1205 set_phys_to_machine(pfn, mfn);
1207 pages_flipped++;
1208 } else {
1209 ret = gnttab_end_foreign_access_ref(ref);
1210 BUG_ON(!ret);
1213 gnttab_release_grant_reference(&np->gref_rx_head, ref);
1215 __skb_queue_tail(list, skb);
1217 next:
1218 if (!(rx->flags & NETRXF_more_data))
1219 break;
1221 if (cons + frags == rp) {
1222 if (net_ratelimit())
1223 WPRINTK("Need more frags\n");
1224 err = -ENOENT;
1225 break;
1228 rx = RING_GET_RESPONSE(&np->rx, cons + frags);
1229 skb = xennet_get_rx_skb(np, cons + frags);
1230 ref = xennet_get_rx_ref(np, cons + frags);
1231 frags++;
1234 if (unlikely(frags > max)) {
1235 if (net_ratelimit())
1236 WPRINTK("Too many frags\n");
1237 err = -E2BIG;
1240 if (unlikely(err))
1241 np->rx.rsp_cons = cons + frags;
1243 *pages_flipped_p = pages_flipped;
1245 return err;
1248 static RING_IDX xennet_fill_frags(struct netfront_info *np,
1249 struct sk_buff *skb,
1250 struct sk_buff_head *list)
1252 struct skb_shared_info *shinfo = skb_shinfo(skb);
1253 int nr_frags = shinfo->nr_frags;
1254 RING_IDX cons = np->rx.rsp_cons;
1255 skb_frag_t *frag = shinfo->frags + nr_frags;
1256 struct sk_buff *nskb;
1258 while ((nskb = __skb_dequeue(list))) {
1259 struct netif_rx_response *rx =
1260 RING_GET_RESPONSE(&np->rx, ++cons);
1262 frag->page = skb_shinfo(nskb)->frags[0].page;
1263 frag->page_offset = rx->offset;
1264 frag->size = rx->status;
1266 skb->data_len += rx->status;
1268 skb_shinfo(nskb)->nr_frags = 0;
1269 kfree_skb(nskb);
1271 frag++;
1272 nr_frags++;
1275 shinfo->nr_frags = nr_frags;
1276 return cons;
1279 static int xennet_set_skb_gso(struct sk_buff *skb,
1280 struct netif_extra_info *gso)
1282 if (!gso->u.gso.size) {
1283 if (net_ratelimit())
1284 WPRINTK("GSO size must not be zero.\n");
1285 return -EINVAL;
1288 /* Currently only TCPv4 S.O. is supported. */
1289 if (gso->u.gso.type != XEN_NETIF_GSO_TYPE_TCPV4) {
1290 if (net_ratelimit())
1291 WPRINTK("Bad GSO type %d.\n", gso->u.gso.type);
1292 return -EINVAL;
1295 #if HAVE_TSO
1296 skb_shinfo(skb)->gso_size = gso->u.gso.size;
1297 #if HAVE_GSO
1298 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
1300 /* Header must be checked, and gso_segs computed. */
1301 skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
1302 #endif
1303 skb_shinfo(skb)->gso_segs = 0;
1305 return 0;
1306 #else
1307 if (net_ratelimit())
1308 WPRINTK("GSO unsupported by this kernel.\n");
1309 return -EINVAL;
1310 #endif
1313 static int netif_poll(struct net_device *dev, int *pbudget)
1315 struct netfront_info *np = netdev_priv(dev);
1316 struct sk_buff *skb;
1317 struct netfront_rx_info rinfo;
1318 struct netif_rx_response *rx = &rinfo.rx;
1319 struct netif_extra_info *extras = rinfo.extras;
1320 RING_IDX i, rp;
1321 struct multicall_entry *mcl;
1322 int work_done, budget, more_to_do = 1, accel_more_to_do = 1;
1323 struct sk_buff_head rxq;
1324 struct sk_buff_head errq;
1325 struct sk_buff_head tmpq;
1326 unsigned long flags;
1327 unsigned int len;
1328 int pages_flipped = 0;
1329 int err;
1331 spin_lock(&np->rx_lock); /* no need for spin_lock_bh() in ->poll() */
1333 if (unlikely(!netfront_carrier_ok(np))) {
1334 spin_unlock(&np->rx_lock);
1335 return 0;
1338 skb_queue_head_init(&rxq);
1339 skb_queue_head_init(&errq);
1340 skb_queue_head_init(&tmpq);
1342 if ((budget = *pbudget) > dev->quota)
1343 budget = dev->quota;
1344 rp = np->rx.sring->rsp_prod;
1345 rmb(); /* Ensure we see queued responses up to 'rp'. */
1347 i = np->rx.rsp_cons;
1348 work_done = 0;
1349 while ((i != rp) && (work_done < budget)) {
1350 memcpy(rx, RING_GET_RESPONSE(&np->rx, i), sizeof(*rx));
1351 memset(extras, 0, sizeof(rinfo.extras));
1353 err = xennet_get_responses(np, &rinfo, rp, &tmpq,
1354 &pages_flipped);
1356 if (unlikely(err)) {
1357 err:
1358 while ((skb = __skb_dequeue(&tmpq)))
1359 __skb_queue_tail(&errq, skb);
1360 np->stats.rx_errors++;
1361 i = np->rx.rsp_cons;
1362 continue;
1365 skb = __skb_dequeue(&tmpq);
1367 if (extras[XEN_NETIF_EXTRA_TYPE_GSO - 1].type) {
1368 struct netif_extra_info *gso;
1369 gso = &extras[XEN_NETIF_EXTRA_TYPE_GSO - 1];
1371 if (unlikely(xennet_set_skb_gso(skb, gso))) {
1372 __skb_queue_head(&tmpq, skb);
1373 np->rx.rsp_cons += skb_queue_len(&tmpq);
1374 goto err;
1378 NETFRONT_SKB_CB(skb)->page = skb_shinfo(skb)->frags[0].page;
1379 NETFRONT_SKB_CB(skb)->offset = rx->offset;
1381 len = rx->status;
1382 if (len > RX_COPY_THRESHOLD)
1383 len = RX_COPY_THRESHOLD;
1384 skb_put(skb, len);
1386 if (rx->status > len) {
1387 skb_shinfo(skb)->frags[0].page_offset =
1388 rx->offset + len;
1389 skb_shinfo(skb)->frags[0].size = rx->status - len;
1390 skb->data_len = rx->status - len;
1391 } else {
1392 skb_shinfo(skb)->frags[0].page = NULL;
1393 skb_shinfo(skb)->nr_frags = 0;
1396 i = xennet_fill_frags(np, skb, &tmpq);
1398 /*
1399 * Truesize must approximates the size of true data plus
1400 * any supervisor overheads. Adding hypervisor overheads
1401 * has been shown to significantly reduce achievable
1402 * bandwidth with the default receive buffer size. It is
1403 * therefore not wise to account for it here.
1405 * After alloc_skb(RX_COPY_THRESHOLD), truesize is set to
1406 * RX_COPY_THRESHOLD + the supervisor overheads. Here, we
1407 * add the size of the data pulled in xennet_fill_frags().
1409 * We also adjust for any unused space in the main data
1410 * area by subtracting (RX_COPY_THRESHOLD - len). This is
1411 * especially important with drivers which split incoming
1412 * packets into header and data, using only 66 bytes of
1413 * the main data area (see the e1000 driver for example.)
1414 * On such systems, without this last adjustement, our
1415 * achievable receive throughout using the standard receive
1416 * buffer size was cut by 25%(!!!).
1417 */
1418 skb->truesize += skb->data_len - (RX_COPY_THRESHOLD - len);
1419 skb->len += skb->data_len;
1421 /*
1422 * Old backends do not assert data_validated but we
1423 * can infer it from csum_blank so test both flags.
1424 */
1425 if (rx->flags & (NETRXF_data_validated|NETRXF_csum_blank))
1426 skb->ip_summed = CHECKSUM_UNNECESSARY;
1427 else
1428 skb->ip_summed = CHECKSUM_NONE;
1429 #ifdef CONFIG_XEN
1430 skb->proto_data_valid = (skb->ip_summed != CHECKSUM_NONE);
1431 skb->proto_csum_blank = !!(rx->flags & NETRXF_csum_blank);
1432 #endif
1433 np->stats.rx_packets++;
1434 np->stats.rx_bytes += skb->len;
1436 __skb_queue_tail(&rxq, skb);
1438 np->rx.rsp_cons = ++i;
1439 work_done++;
1442 if (pages_flipped) {
1443 /* Some pages are no longer absent... */
1444 balloon_update_driver_allowance(-pages_flipped);
1446 /* Do all the remapping work and M2P updates. */
1447 if (!xen_feature(XENFEAT_auto_translated_physmap)) {
1448 mcl = np->rx_mcl + pages_flipped;
1449 mcl->op = __HYPERVISOR_mmu_update;
1450 mcl->args[0] = (unsigned long)np->rx_mmu;
1451 mcl->args[1] = pages_flipped;
1452 mcl->args[2] = 0;
1453 mcl->args[3] = DOMID_SELF;
1454 err = HYPERVISOR_multicall_check(np->rx_mcl,
1455 pages_flipped + 1,
1456 NULL);
1457 BUG_ON(err);
1461 while ((skb = __skb_dequeue(&errq)))
1462 kfree_skb(skb);
1464 while ((skb = __skb_dequeue(&rxq)) != NULL) {
1465 struct page *page = NETFRONT_SKB_CB(skb)->page;
1466 void *vaddr = page_address(page);
1467 unsigned offset = NETFRONT_SKB_CB(skb)->offset;
1469 memcpy(skb->data, vaddr + offset, skb_headlen(skb));
1471 if (page != skb_shinfo(skb)->frags[0].page)
1472 __free_page(page);
1474 /* Ethernet work: Delayed to here as it peeks the header. */
1475 skb->protocol = eth_type_trans(skb, dev);
1477 /* Pass it up. */
1478 netif_receive_skb(skb);
1479 dev->last_rx = jiffies;
1482 /* If we get a callback with very few responses, reduce fill target. */
1483 /* NB. Note exponential increase, linear decrease. */
1484 if (((np->rx.req_prod_pvt - np->rx.sring->rsp_prod) >
1485 ((3*np->rx_target) / 4)) &&
1486 (--np->rx_target < np->rx_min_target))
1487 np->rx_target = np->rx_min_target;
1489 network_alloc_rx_buffers(dev);
1491 if (work_done < budget) {
1492 /* there's some spare capacity, try the accelerated path */
1493 int accel_budget = budget - work_done;
1494 int accel_budget_start = accel_budget;
1496 if (np->accel_vif_state.hooks) {
1497 accel_more_to_do =
1498 np->accel_vif_state.hooks->netdev_poll
1499 (dev, &accel_budget);
1500 work_done += (accel_budget_start - accel_budget);
1501 } else
1502 accel_more_to_do = 0;
1505 *pbudget -= work_done;
1506 dev->quota -= work_done;
1508 if (work_done < budget) {
1509 local_irq_save(flags);
1511 RING_FINAL_CHECK_FOR_RESPONSES(&np->rx, more_to_do);
1513 if (!more_to_do && !accel_more_to_do &&
1514 np->accel_vif_state.hooks) {
1515 /*
1516 * Slow path has nothing more to do, see if
1517 * fast path is likewise
1518 */
1519 accel_more_to_do =
1520 np->accel_vif_state.hooks->start_napi_irq(dev);
1523 if (!more_to_do && !accel_more_to_do)
1524 __netif_rx_complete(dev);
1526 local_irq_restore(flags);
1529 spin_unlock(&np->rx_lock);
1531 return more_to_do | accel_more_to_do;
1534 static void netif_release_tx_bufs(struct netfront_info *np)
1536 struct sk_buff *skb;
1537 int i;
1539 for (i = 1; i <= NET_TX_RING_SIZE; i++) {
1540 if ((unsigned long)np->tx_skbs[i] < PAGE_OFFSET)
1541 continue;
1543 skb = np->tx_skbs[i];
1544 gnttab_end_foreign_access_ref(np->grant_tx_ref[i]);
1545 gnttab_release_grant_reference(
1546 &np->gref_tx_head, np->grant_tx_ref[i]);
1547 np->grant_tx_ref[i] = GRANT_INVALID_REF;
1548 add_id_to_freelist(np->tx_skbs, i);
1549 dev_kfree_skb_irq(skb);
1553 static void netif_release_rx_bufs_flip(struct netfront_info *np)
1555 struct mmu_update *mmu = np->rx_mmu;
1556 struct multicall_entry *mcl = np->rx_mcl;
1557 struct sk_buff_head free_list;
1558 struct sk_buff *skb;
1559 unsigned long mfn;
1560 int xfer = 0, noxfer = 0, unused = 0;
1561 int id, ref, rc;
1563 skb_queue_head_init(&free_list);
1565 spin_lock_bh(&np->rx_lock);
1567 for (id = 0; id < NET_RX_RING_SIZE; id++) {
1568 if ((ref = np->grant_rx_ref[id]) == GRANT_INVALID_REF) {
1569 unused++;
1570 continue;
1573 skb = np->rx_skbs[id];
1574 mfn = gnttab_end_foreign_transfer_ref(ref);
1575 gnttab_release_grant_reference(&np->gref_rx_head, ref);
1576 np->grant_rx_ref[id] = GRANT_INVALID_REF;
1577 add_id_to_freelist(np->rx_skbs, id);
1579 if (0 == mfn) {
1580 struct page *page = skb_shinfo(skb)->frags[0].page;
1581 balloon_release_driver_page(page);
1582 skb_shinfo(skb)->nr_frags = 0;
1583 dev_kfree_skb(skb);
1584 noxfer++;
1585 continue;
1588 if (!xen_feature(XENFEAT_auto_translated_physmap)) {
1589 /* Remap the page. */
1590 struct page *page = skb_shinfo(skb)->frags[0].page;
1591 unsigned long pfn = page_to_pfn(page);
1592 void *vaddr = page_address(page);
1594 MULTI_update_va_mapping(mcl, (unsigned long)vaddr,
1595 pfn_pte_ma(mfn, PAGE_KERNEL),
1596 0);
1597 mcl++;
1598 mmu->ptr = ((maddr_t)mfn << PAGE_SHIFT)
1599 | MMU_MACHPHYS_UPDATE;
1600 mmu->val = pfn;
1601 mmu++;
1603 set_phys_to_machine(pfn, mfn);
1605 __skb_queue_tail(&free_list, skb);
1606 xfer++;
1609 DPRINTK("%s: %d xfer, %d noxfer, %d unused\n",
1610 __FUNCTION__, xfer, noxfer, unused);
1612 if (xfer) {
1613 /* Some pages are no longer absent... */
1614 balloon_update_driver_allowance(-xfer);
1616 if (!xen_feature(XENFEAT_auto_translated_physmap)) {
1617 /* Do all the remapping work and M2P updates. */
1618 mcl->op = __HYPERVISOR_mmu_update;
1619 mcl->args[0] = (unsigned long)np->rx_mmu;
1620 mcl->args[1] = mmu - np->rx_mmu;
1621 mcl->args[2] = 0;
1622 mcl->args[3] = DOMID_SELF;
1623 mcl++;
1624 rc = HYPERVISOR_multicall_check(
1625 np->rx_mcl, mcl - np->rx_mcl, NULL);
1626 BUG_ON(rc);
1630 while ((skb = __skb_dequeue(&free_list)) != NULL)
1631 dev_kfree_skb(skb);
1633 spin_unlock_bh(&np->rx_lock);
1636 static void netif_release_rx_bufs_copy(struct netfront_info *np)
1638 struct sk_buff *skb;
1639 int i, ref;
1640 int busy = 0, inuse = 0;
1642 spin_lock_bh(&np->rx_lock);
1644 for (i = 0; i < NET_RX_RING_SIZE; i++) {
1645 ref = np->grant_rx_ref[i];
1647 if (ref == GRANT_INVALID_REF)
1648 continue;
1650 inuse++;
1652 skb = np->rx_skbs[i];
1654 if (!gnttab_end_foreign_access_ref(ref))
1656 busy++;
1657 continue;
1660 gnttab_release_grant_reference(&np->gref_rx_head, ref);
1661 np->grant_rx_ref[i] = GRANT_INVALID_REF;
1662 add_id_to_freelist(np->rx_skbs, i);
1664 skb_shinfo(skb)->nr_frags = 0;
1665 dev_kfree_skb(skb);
1668 if (busy)
1669 DPRINTK("%s: Unable to release %d of %d inuse grant references out of %ld total.\n",
1670 __FUNCTION__, busy, inuse, NET_RX_RING_SIZE);
1672 spin_unlock_bh(&np->rx_lock);
1675 static int network_close(struct net_device *dev)
1677 struct netfront_info *np = netdev_priv(dev);
1678 netif_stop_queue(np->netdev);
1679 return 0;
1683 static struct net_device_stats *network_get_stats(struct net_device *dev)
1685 struct netfront_info *np = netdev_priv(dev);
1687 netfront_accelerator_call_get_stats(np, dev);
1688 return &np->stats;
1691 static int xennet_change_mtu(struct net_device *dev, int mtu)
1693 int max = xennet_can_sg(dev) ? 65535 - ETH_HLEN : ETH_DATA_LEN;
1695 if (mtu > max)
1696 return -EINVAL;
1697 dev->mtu = mtu;
1698 return 0;
1701 static int xennet_set_sg(struct net_device *dev, u32 data)
1703 if (data) {
1704 struct netfront_info *np = netdev_priv(dev);
1705 int val;
1707 if (xenbus_scanf(XBT_NIL, np->xbdev->otherend, "feature-sg",
1708 "%d", &val) < 0)
1709 val = 0;
1710 if (!val)
1711 return -ENOSYS;
1712 } else if (dev->mtu > ETH_DATA_LEN)
1713 dev->mtu = ETH_DATA_LEN;
1715 return ethtool_op_set_sg(dev, data);
1718 static int xennet_set_tso(struct net_device *dev, u32 data)
1720 if (data) {
1721 struct netfront_info *np = netdev_priv(dev);
1722 int val;
1724 if (xenbus_scanf(XBT_NIL, np->xbdev->otherend,
1725 "feature-gso-tcpv4", "%d", &val) < 0)
1726 val = 0;
1727 if (!val)
1728 return -ENOSYS;
1731 return ethtool_op_set_tso(dev, data);
1734 static void xennet_set_features(struct net_device *dev)
1736 dev_disable_gso_features(dev);
1737 xennet_set_sg(dev, 0);
1739 /* We need checksum offload to enable scatter/gather and TSO. */
1740 if (!(dev->features & NETIF_F_IP_CSUM))
1741 return;
1743 if (xennet_set_sg(dev, 1))
1744 return;
1746 /* Before 2.6.9 TSO seems to be unreliable so do not enable it
1747 * on older kernels.
1748 */
1749 if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,9))
1750 xennet_set_tso(dev, 1);
1753 static int network_connect(struct net_device *dev)
1755 struct netfront_info *np = netdev_priv(dev);
1756 int i, requeue_idx, err;
1757 struct sk_buff *skb;
1758 grant_ref_t ref;
1759 netif_rx_request_t *req;
1760 unsigned int feature_rx_copy, feature_rx_flip;
1762 err = xenbus_scanf(XBT_NIL, np->xbdev->otherend,
1763 "feature-rx-copy", "%u", &feature_rx_copy);
1764 if (err != 1)
1765 feature_rx_copy = 0;
1766 err = xenbus_scanf(XBT_NIL, np->xbdev->otherend,
1767 "feature-rx-flip", "%u", &feature_rx_flip);
1768 if (err != 1)
1769 feature_rx_flip = 1;
1771 /*
1772 * Copy packets on receive path if:
1773 * (a) This was requested by user, and the backend supports it; or
1774 * (b) Flipping was requested, but this is unsupported by the backend.
1775 */
1776 np->copying_receiver = ((MODPARM_rx_copy && feature_rx_copy) ||
1777 (MODPARM_rx_flip && !feature_rx_flip));
1779 err = talk_to_backend(np->xbdev, np);
1780 if (err)
1781 return err;
1783 xennet_set_features(dev);
1785 DPRINTK("device %s has %sing receive path.\n",
1786 dev->name, np->copying_receiver ? "copy" : "flipp");
1788 spin_lock_bh(&np->rx_lock);
1789 spin_lock_irq(&np->tx_lock);
1791 /*
1792 * Recovery procedure:
1793 * NB. Freelist index entries are always going to be less than
1794 * PAGE_OFFSET, whereas pointers to skbs will always be equal or
1795 * greater than PAGE_OFFSET: we use this property to distinguish
1796 * them.
1797 */
1799 /* Step 1: Discard all pending TX packet fragments. */
1800 netif_release_tx_bufs(np);
1802 /* Step 2: Rebuild the RX buffer freelist and the RX ring itself. */
1803 for (requeue_idx = 0, i = 0; i < NET_RX_RING_SIZE; i++) {
1804 if (!np->rx_skbs[i])
1805 continue;
1807 skb = np->rx_skbs[requeue_idx] = xennet_get_rx_skb(np, i);
1808 ref = np->grant_rx_ref[requeue_idx] = xennet_get_rx_ref(np, i);
1809 req = RING_GET_REQUEST(&np->rx, requeue_idx);
1811 if (!np->copying_receiver) {
1812 gnttab_grant_foreign_transfer_ref(
1813 ref, np->xbdev->otherend_id,
1814 page_to_pfn(skb_shinfo(skb)->frags->page));
1815 } else {
1816 gnttab_grant_foreign_access_ref(
1817 ref, np->xbdev->otherend_id,
1818 pfn_to_mfn(page_to_pfn(skb_shinfo(skb)->
1819 frags->page)),
1820 0);
1822 req->gref = ref;
1823 req->id = requeue_idx;
1825 requeue_idx++;
1828 np->rx.req_prod_pvt = requeue_idx;
1830 /*
1831 * Step 3: All public and private state should now be sane. Get
1832 * ready to start sending and receiving packets and give the driver
1833 * domain a kick because we've probably just requeued some
1834 * packets.
1835 */
1836 netfront_carrier_on(np);
1837 notify_remote_via_irq(np->irq);
1838 network_tx_buf_gc(dev);
1839 network_alloc_rx_buffers(dev);
1841 spin_unlock_irq(&np->tx_lock);
1842 spin_unlock_bh(&np->rx_lock);
1844 return 0;
1847 static void netif_uninit(struct net_device *dev)
1849 struct netfront_info *np = netdev_priv(dev);
1850 netif_release_tx_bufs(np);
1851 if (np->copying_receiver)
1852 netif_release_rx_bufs_copy(np);
1853 else
1854 netif_release_rx_bufs_flip(np);
1855 gnttab_free_grant_references(np->gref_tx_head);
1856 gnttab_free_grant_references(np->gref_rx_head);
1859 static struct ethtool_ops network_ethtool_ops =
1861 .get_tx_csum = ethtool_op_get_tx_csum,
1862 .set_tx_csum = ethtool_op_set_tx_csum,
1863 .get_sg = ethtool_op_get_sg,
1864 .set_sg = xennet_set_sg,
1865 #if HAVE_TSO
1866 .get_tso = ethtool_op_get_tso,
1867 .set_tso = xennet_set_tso,
1868 #endif
1869 .get_link = ethtool_op_get_link,
1870 };
1872 #ifdef CONFIG_SYSFS
1873 static ssize_t show_rxbuf_min(struct class_device *cd, char *buf)
1875 struct net_device *netdev = container_of(cd, struct net_device,
1876 class_dev);
1877 struct netfront_info *info = netdev_priv(netdev);
1879 return sprintf(buf, "%u\n", info->rx_min_target);
1882 static ssize_t store_rxbuf_min(struct class_device *cd,
1883 const char *buf, size_t len)
1885 struct net_device *netdev = container_of(cd, struct net_device,
1886 class_dev);
1887 struct netfront_info *np = netdev_priv(netdev);
1888 char *endp;
1889 unsigned long target;
1891 if (!capable(CAP_NET_ADMIN))
1892 return -EPERM;
1894 target = simple_strtoul(buf, &endp, 0);
1895 if (endp == buf)
1896 return -EBADMSG;
1898 if (target < RX_MIN_TARGET)
1899 target = RX_MIN_TARGET;
1900 if (target > RX_MAX_TARGET)
1901 target = RX_MAX_TARGET;
1903 spin_lock_bh(&np->rx_lock);
1904 if (target > np->rx_max_target)
1905 np->rx_max_target = target;
1906 np->rx_min_target = target;
1907 if (target > np->rx_target)
1908 np->rx_target = target;
1910 network_alloc_rx_buffers(netdev);
1912 spin_unlock_bh(&np->rx_lock);
1913 return len;
1916 static ssize_t show_rxbuf_max(struct class_device *cd, char *buf)
1918 struct net_device *netdev = container_of(cd, struct net_device,
1919 class_dev);
1920 struct netfront_info *info = netdev_priv(netdev);
1922 return sprintf(buf, "%u\n", info->rx_max_target);
1925 static ssize_t store_rxbuf_max(struct class_device *cd,
1926 const char *buf, size_t len)
1928 struct net_device *netdev = container_of(cd, struct net_device,
1929 class_dev);
1930 struct netfront_info *np = netdev_priv(netdev);
1931 char *endp;
1932 unsigned long target;
1934 if (!capable(CAP_NET_ADMIN))
1935 return -EPERM;
1937 target = simple_strtoul(buf, &endp, 0);
1938 if (endp == buf)
1939 return -EBADMSG;
1941 if (target < RX_MIN_TARGET)
1942 target = RX_MIN_TARGET;
1943 if (target > RX_MAX_TARGET)
1944 target = RX_MAX_TARGET;
1946 spin_lock_bh(&np->rx_lock);
1947 if (target < np->rx_min_target)
1948 np->rx_min_target = target;
1949 np->rx_max_target = target;
1950 if (target < np->rx_target)
1951 np->rx_target = target;
1953 network_alloc_rx_buffers(netdev);
1955 spin_unlock_bh(&np->rx_lock);
1956 return len;
1959 static ssize_t show_rxbuf_cur(struct class_device *cd, char *buf)
1961 struct net_device *netdev = container_of(cd, struct net_device,
1962 class_dev);
1963 struct netfront_info *info = netdev_priv(netdev);
1965 return sprintf(buf, "%u\n", info->rx_target);
1968 static const struct class_device_attribute xennet_attrs[] = {
1969 __ATTR(rxbuf_min, S_IRUGO|S_IWUSR, show_rxbuf_min, store_rxbuf_min),
1970 __ATTR(rxbuf_max, S_IRUGO|S_IWUSR, show_rxbuf_max, store_rxbuf_max),
1971 __ATTR(rxbuf_cur, S_IRUGO, show_rxbuf_cur, NULL),
1972 };
1974 static int xennet_sysfs_addif(struct net_device *netdev)
1976 int i;
1977 int error = 0;
1979 for (i = 0; i < ARRAY_SIZE(xennet_attrs); i++) {
1980 error = class_device_create_file(&netdev->class_dev,
1981 &xennet_attrs[i]);
1982 if (error)
1983 goto fail;
1985 return 0;
1987 fail:
1988 while (--i >= 0)
1989 class_device_remove_file(&netdev->class_dev,
1990 &xennet_attrs[i]);
1991 return error;
1994 static void xennet_sysfs_delif(struct net_device *netdev)
1996 int i;
1998 for (i = 0; i < ARRAY_SIZE(xennet_attrs); i++) {
1999 class_device_remove_file(&netdev->class_dev,
2000 &xennet_attrs[i]);
2004 #endif /* CONFIG_SYSFS */
2007 /*
2008 * Nothing to do here. Virtual interface is point-to-point and the
2009 * physical interface is probably promiscuous anyway.
2010 */
2011 static void network_set_multicast_list(struct net_device *dev)
2015 static struct net_device * __devinit create_netdev(struct xenbus_device *dev)
2017 int i, err = 0;
2018 struct net_device *netdev = NULL;
2019 struct netfront_info *np = NULL;
2021 netdev = alloc_etherdev(sizeof(struct netfront_info));
2022 if (!netdev) {
2023 printk(KERN_WARNING "%s> alloc_etherdev failed.\n",
2024 __FUNCTION__);
2025 return ERR_PTR(-ENOMEM);
2028 np = netdev_priv(netdev);
2029 np->xbdev = dev;
2031 spin_lock_init(&np->tx_lock);
2032 spin_lock_init(&np->rx_lock);
2034 init_accelerator_vif(np, dev);
2036 skb_queue_head_init(&np->rx_batch);
2037 np->rx_target = RX_DFL_MIN_TARGET;
2038 np->rx_min_target = RX_DFL_MIN_TARGET;
2039 np->rx_max_target = RX_MAX_TARGET;
2041 init_timer(&np->rx_refill_timer);
2042 np->rx_refill_timer.data = (unsigned long)netdev;
2043 np->rx_refill_timer.function = rx_refill_timeout;
2045 /* Initialise {tx,rx}_skbs as a free chain containing every entry. */
2046 for (i = 0; i <= NET_TX_RING_SIZE; i++) {
2047 np->tx_skbs[i] = (void *)((unsigned long) i+1);
2048 np->grant_tx_ref[i] = GRANT_INVALID_REF;
2051 for (i = 0; i < NET_RX_RING_SIZE; i++) {
2052 np->rx_skbs[i] = NULL;
2053 np->grant_rx_ref[i] = GRANT_INVALID_REF;
2056 /* A grant for every tx ring slot */
2057 if (gnttab_alloc_grant_references(TX_MAX_TARGET,
2058 &np->gref_tx_head) < 0) {
2059 printk(KERN_ALERT "#### netfront can't alloc tx grant refs\n");
2060 err = -ENOMEM;
2061 goto exit;
2063 /* A grant for every rx ring slot */
2064 if (gnttab_alloc_grant_references(RX_MAX_TARGET,
2065 &np->gref_rx_head) < 0) {
2066 printk(KERN_ALERT "#### netfront can't alloc rx grant refs\n");
2067 err = -ENOMEM;
2068 goto exit_free_tx;
2071 netdev->open = network_open;
2072 netdev->hard_start_xmit = network_start_xmit;
2073 netdev->stop = network_close;
2074 netdev->get_stats = network_get_stats;
2075 netdev->poll = netif_poll;
2076 netdev->set_multicast_list = network_set_multicast_list;
2077 netdev->uninit = netif_uninit;
2078 netdev->change_mtu = xennet_change_mtu;
2079 netdev->weight = 64;
2080 netdev->features = NETIF_F_IP_CSUM;
2082 SET_ETHTOOL_OPS(netdev, &network_ethtool_ops);
2083 SET_MODULE_OWNER(netdev);
2084 SET_NETDEV_DEV(netdev, &dev->dev);
2086 np->netdev = netdev;
2088 netfront_carrier_off(np);
2090 return netdev;
2092 exit_free_tx:
2093 gnttab_free_grant_references(np->gref_tx_head);
2094 exit:
2095 free_netdev(netdev);
2096 return ERR_PTR(err);
2099 #ifdef CONFIG_INET
2100 /*
2101 * We use this notifier to send out a fake ARP reply to reset switches and
2102 * router ARP caches when an IP interface is brought up on a VIF.
2103 */
2104 static int
2105 inetdev_notify(struct notifier_block *this, unsigned long event, void *ptr)
2107 struct in_ifaddr *ifa = (struct in_ifaddr *)ptr;
2108 struct net_device *dev = ifa->ifa_dev->dev;
2110 /* UP event and is it one of our devices? */
2111 if (event == NETDEV_UP && dev->open == network_open)
2112 send_fake_arp(dev);
2114 return NOTIFY_DONE;
2117 static struct notifier_block notifier_inetdev = {
2118 .notifier_call = inetdev_notify,
2119 .next = NULL,
2120 .priority = 0
2121 };
2122 #endif
2125 static void netif_disconnect_backend(struct netfront_info *info)
2127 /* Stop old i/f to prevent errors whilst we rebuild the state. */
2128 spin_lock_bh(&info->rx_lock);
2129 spin_lock_irq(&info->tx_lock);
2130 netfront_carrier_off(info);
2131 spin_unlock_irq(&info->tx_lock);
2132 spin_unlock_bh(&info->rx_lock);
2134 if (info->irq)
2135 unbind_from_irqhandler(info->irq, info->netdev);
2136 info->irq = 0;
2138 end_access(info->tx_ring_ref, info->tx.sring);
2139 end_access(info->rx_ring_ref, info->rx.sring);
2140 info->tx_ring_ref = GRANT_INVALID_REF;
2141 info->rx_ring_ref = GRANT_INVALID_REF;
2142 info->tx.sring = NULL;
2143 info->rx.sring = NULL;
2147 static void end_access(int ref, void *page)
2149 if (ref != GRANT_INVALID_REF)
2150 gnttab_end_foreign_access(ref, (unsigned long)page);
2154 /* ** Driver registration ** */
2157 static struct xenbus_device_id netfront_ids[] = {
2158 { "vif" },
2159 { "" }
2160 };
2161 MODULE_ALIAS("xen:vif");
2164 static struct xenbus_driver netfront = {
2165 .name = "vif",
2166 .owner = THIS_MODULE,
2167 .ids = netfront_ids,
2168 .probe = netfront_probe,
2169 .remove = __devexit_p(netfront_remove),
2170 .suspend = netfront_suspend,
2171 .suspend_cancel = netfront_suspend_cancel,
2172 .resume = netfront_resume,
2173 .otherend_changed = backend_changed,
2174 };
2177 static int __init netif_init(void)
2179 if (!is_running_on_xen())
2180 return -ENODEV;
2182 #ifdef CONFIG_XEN
2183 if (MODPARM_rx_flip && MODPARM_rx_copy) {
2184 WPRINTK("Cannot specify both rx_copy and rx_flip.\n");
2185 return -EINVAL;
2188 if (!MODPARM_rx_flip && !MODPARM_rx_copy)
2189 MODPARM_rx_flip = 1; /* Default is to flip. */
2190 #endif
2192 if (is_initial_xendomain())
2193 return 0;
2195 netif_init_accel();
2197 IPRINTK("Initialising virtual ethernet driver.\n");
2199 #ifdef CONFIG_INET
2200 (void)register_inetaddr_notifier(&notifier_inetdev);
2201 #endif
2203 return xenbus_register_frontend(&netfront);
2205 module_init(netif_init);
2208 static void __exit netif_exit(void)
2210 if (is_initial_xendomain())
2211 return;
2213 #ifdef CONFIG_INET
2214 unregister_inetaddr_notifier(&notifier_inetdev);
2215 #endif
2217 netif_exit_accel();
2219 return xenbus_unregister_driver(&netfront);
2221 module_exit(netif_exit);
2223 MODULE_LICENSE("Dual BSD/GPL");