ia64/xen-unstable

view linux-2.6-xen-sparse/drivers/xen/netfront/netfront.c @ 14874:9cf4c575b509

netfront: use skb.cb for storing private data

Netfront's use of nh.raw and h.raw for storing page+offset is a bit
hinky, and it breaks with upcoming network stack updates which reduce
these fields to sub-pointer sizes. Fortunately, skb offers the "cb"
field specifically for stashing this kind of info, so use it.

Signed-off-by: Jeremy Fitzhardinge <jeremy@xensource.com>
Cc: Herbert Xu <herbert@gondor.apana.org.au>
Cc: Chris Wright <chrisw@sous-sol.org>
Cc: Christian Limpach <Christian.Limpach@cl.cam.ac.uk>
author kfraser@localhost.localdomain
date Tue Apr 17 13:21:22 2007 +0100 (2007-04-17)
parents 22460cfaca71
children
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 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License version 2
9 * as published by the Free Software Foundation; or, when distributed
10 * separately from the Linux kernel or incorporated into other
11 * software packages, subject to the following license:
12 *
13 * Permission is hereby granted, free of charge, to any person obtaining a copy
14 * of this source file (the "Software"), to deal in the Software without
15 * restriction, including without limitation the rights to use, copy, modify,
16 * merge, publish, distribute, sublicense, and/or sell copies of the Software,
17 * and to permit persons to whom the Software is furnished to do so, subject to
18 * the following conditions:
19 *
20 * The above copyright notice and this permission notice shall be included in
21 * all copies or substantial portions of the Software.
22 *
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
28 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
29 * IN THE SOFTWARE.
30 */
32 #include <linux/module.h>
33 #include <linux/version.h>
34 #include <linux/kernel.h>
35 #include <linux/sched.h>
36 #include <linux/slab.h>
37 #include <linux/string.h>
38 #include <linux/errno.h>
39 #include <linux/netdevice.h>
40 #include <linux/inetdevice.h>
41 #include <linux/etherdevice.h>
42 #include <linux/skbuff.h>
43 #include <linux/init.h>
44 #include <linux/bitops.h>
45 #include <linux/ethtool.h>
46 #include <linux/in.h>
47 #include <linux/if_ether.h>
48 #include <linux/io.h>
49 #include <linux/moduleparam.h>
50 #include <net/sock.h>
51 #include <net/pkt_sched.h>
52 #include <net/arp.h>
53 #include <net/route.h>
54 #include <asm/uaccess.h>
55 #include <xen/evtchn.h>
56 #include <xen/xenbus.h>
57 #include <xen/interface/io/netif.h>
58 #include <xen/interface/memory.h>
59 #include <xen/balloon.h>
60 #include <asm/page.h>
61 #include <asm/maddr.h>
62 #include <asm/uaccess.h>
63 #include <xen/interface/grant_table.h>
64 #include <xen/gnttab.h>
66 #ifdef HAVE_XEN_PLATFORM_COMPAT_H
67 #include <xen/platform-compat.h>
68 #endif
70 struct netfront_cb {
71 struct page *page;
72 unsigned offset;
73 };
75 #define NETFRONT_SKB_CB(skb) ((struct netfront_cb *)((skb)->cb))
77 /*
78 * Mutually-exclusive module options to select receive data path:
79 * rx_copy : Packets are copied by network backend into local memory
80 * rx_flip : Page containing packet data is transferred to our ownership
81 * For fully-virtualised guests there is no option - copying must be used.
82 * For paravirtualised guests, flipping is the default.
83 */
84 #ifdef CONFIG_XEN
85 static int MODPARM_rx_copy = 0;
86 module_param_named(rx_copy, MODPARM_rx_copy, bool, 0);
87 MODULE_PARM_DESC(rx_copy, "Copy packets from network card (rather than flip)");
88 static int MODPARM_rx_flip = 0;
89 module_param_named(rx_flip, MODPARM_rx_flip, bool, 0);
90 MODULE_PARM_DESC(rx_flip, "Flip packets from network card (rather than copy)");
91 #else
92 static const int MODPARM_rx_copy = 1;
93 static const int MODPARM_rx_flip = 0;
94 #endif
96 #define RX_COPY_THRESHOLD 256
98 /* If we don't have GSO, fake things up so that we never try to use it. */
99 #if defined(NETIF_F_GSO)
100 #define HAVE_GSO 1
101 #define HAVE_TSO 1 /* TSO is a subset of GSO */
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_TSO 1
111 /* Some older kernels cannot cope with incorrect checksums,
112 * particularly in netfilter. I'm not sure there is 100% correlation
113 * with the presence of NETIF_F_TSO but it appears to be a good first
114 * approximiation.
115 */
116 #define HAVE_NO_CSUM_OFFLOAD 1
118 #define gso_size tso_size
119 #define gso_segs tso_segs
120 static inline void dev_disable_gso_features(struct net_device *dev)
121 {
122 /* Turn off all TSO bits. */
123 dev->features &= ~NETIF_F_TSO;
124 }
125 static inline int skb_is_gso(const struct sk_buff *skb)
126 {
127 return skb_shinfo(skb)->tso_size;
128 }
129 static inline int skb_gso_ok(struct sk_buff *skb, int features)
130 {
131 return (features & NETIF_F_TSO);
132 }
134 static inline int netif_needs_gso(struct net_device *dev, struct sk_buff *skb)
135 {
136 return skb_is_gso(skb) &&
137 (!skb_gso_ok(skb, dev->features) ||
138 unlikely(skb->ip_summed != CHECKSUM_HW));
139 }
140 #else
141 #define netif_needs_gso(dev, skb) 0
142 #define dev_disable_gso_features(dev) ((void)0)
143 #endif
145 #define GRANT_INVALID_REF 0
147 #define NET_TX_RING_SIZE __RING_SIZE((struct netif_tx_sring *)0, PAGE_SIZE)
148 #define NET_RX_RING_SIZE __RING_SIZE((struct netif_rx_sring *)0, PAGE_SIZE)
150 struct netfront_info {
151 struct list_head list;
152 struct net_device *netdev;
154 struct net_device_stats stats;
156 struct netif_tx_front_ring tx;
157 struct netif_rx_front_ring rx;
159 spinlock_t tx_lock;
160 spinlock_t rx_lock;
162 unsigned int irq;
163 unsigned int copying_receiver;
164 unsigned int carrier;
166 /* Receive-ring batched refills. */
167 #define RX_MIN_TARGET 8
168 #define RX_DFL_MIN_TARGET 64
169 #define RX_MAX_TARGET min_t(int, NET_RX_RING_SIZE, 256)
170 unsigned rx_min_target, rx_max_target, rx_target;
171 struct sk_buff_head rx_batch;
173 struct timer_list rx_refill_timer;
175 /*
176 * {tx,rx}_skbs store outstanding skbuffs. The first entry in tx_skbs
177 * is an index into a chain of free entries.
178 */
179 struct sk_buff *tx_skbs[NET_TX_RING_SIZE+1];
180 struct sk_buff *rx_skbs[NET_RX_RING_SIZE];
182 #define TX_MAX_TARGET min_t(int, NET_RX_RING_SIZE, 256)
183 grant_ref_t gref_tx_head;
184 grant_ref_t grant_tx_ref[NET_TX_RING_SIZE + 1];
185 grant_ref_t gref_rx_head;
186 grant_ref_t grant_rx_ref[NET_RX_RING_SIZE];
188 struct xenbus_device *xbdev;
189 int tx_ring_ref;
190 int rx_ring_ref;
191 u8 mac[ETH_ALEN];
193 unsigned long rx_pfn_array[NET_RX_RING_SIZE];
194 struct multicall_entry rx_mcl[NET_RX_RING_SIZE+1];
195 struct mmu_update rx_mmu[NET_RX_RING_SIZE];
196 };
198 struct netfront_rx_info {
199 struct netif_rx_response rx;
200 struct netif_extra_info extras[XEN_NETIF_EXTRA_TYPE_MAX - 1];
201 };
203 /*
204 * Implement our own carrier flag: the network stack's version causes delays
205 * when the carrier is re-enabled (in particular, dev_activate() may not
206 * immediately be called, which can cause packet loss).
207 */
208 #define netfront_carrier_on(netif) ((netif)->carrier = 1)
209 #define netfront_carrier_off(netif) ((netif)->carrier = 0)
210 #define netfront_carrier_ok(netif) ((netif)->carrier)
212 /*
213 * Access macros for acquiring freeing slots in tx_skbs[].
214 */
216 static inline void add_id_to_freelist(struct sk_buff **list, unsigned short id)
217 {
218 list[id] = list[0];
219 list[0] = (void *)(unsigned long)id;
220 }
222 static inline unsigned short get_id_from_freelist(struct sk_buff **list)
223 {
224 unsigned int id = (unsigned int)(unsigned long)list[0];
225 list[0] = list[id];
226 return id;
227 }
229 static inline int xennet_rxidx(RING_IDX idx)
230 {
231 return idx & (NET_RX_RING_SIZE - 1);
232 }
234 static inline struct sk_buff *xennet_get_rx_skb(struct netfront_info *np,
235 RING_IDX ri)
236 {
237 int i = xennet_rxidx(ri);
238 struct sk_buff *skb = np->rx_skbs[i];
239 np->rx_skbs[i] = NULL;
240 return skb;
241 }
243 static inline grant_ref_t xennet_get_rx_ref(struct netfront_info *np,
244 RING_IDX ri)
245 {
246 int i = xennet_rxidx(ri);
247 grant_ref_t ref = np->grant_rx_ref[i];
248 np->grant_rx_ref[i] = GRANT_INVALID_REF;
249 return ref;
250 }
252 #define DPRINTK(fmt, args...) \
253 pr_debug("netfront (%s:%d) " fmt, \
254 __FUNCTION__, __LINE__, ##args)
255 #define IPRINTK(fmt, args...) \
256 printk(KERN_INFO "netfront: " fmt, ##args)
257 #define WPRINTK(fmt, args...) \
258 printk(KERN_WARNING "netfront: " fmt, ##args)
260 static int setup_device(struct xenbus_device *, struct netfront_info *);
261 static struct net_device *create_netdev(struct xenbus_device *);
263 static void end_access(int, void *);
264 static void netif_disconnect_backend(struct netfront_info *);
266 static int network_connect(struct net_device *);
267 static void network_tx_buf_gc(struct net_device *);
268 static void network_alloc_rx_buffers(struct net_device *);
269 static int send_fake_arp(struct net_device *);
271 static irqreturn_t netif_int(int irq, void *dev_id, struct pt_regs *ptregs);
273 #ifdef CONFIG_SYSFS
274 static int xennet_sysfs_addif(struct net_device *netdev);
275 static void xennet_sysfs_delif(struct net_device *netdev);
276 #else /* !CONFIG_SYSFS */
277 #define xennet_sysfs_addif(dev) (0)
278 #define xennet_sysfs_delif(dev) do { } while(0)
279 #endif
281 static inline int xennet_can_sg(struct net_device *dev)
282 {
283 return dev->features & NETIF_F_SG;
284 }
286 /**
287 * Entry point to this code when a new device is created. Allocate the basic
288 * structures and the ring buffers for communication with the backend, and
289 * inform the backend of the appropriate details for those.
290 */
291 static int __devinit netfront_probe(struct xenbus_device *dev,
292 const struct xenbus_device_id *id)
293 {
294 int err;
295 struct net_device *netdev;
296 struct netfront_info *info;
298 netdev = create_netdev(dev);
299 if (IS_ERR(netdev)) {
300 err = PTR_ERR(netdev);
301 xenbus_dev_fatal(dev, err, "creating netdev");
302 return err;
303 }
305 info = netdev_priv(netdev);
306 dev->dev.driver_data = info;
308 err = register_netdev(info->netdev);
309 if (err) {
310 printk(KERN_WARNING "%s: register_netdev err=%d\n",
311 __FUNCTION__, err);
312 goto fail;
313 }
315 err = xennet_sysfs_addif(info->netdev);
316 if (err) {
317 unregister_netdev(info->netdev);
318 printk(KERN_WARNING "%s: add sysfs failed err=%d\n",
319 __FUNCTION__, err);
320 goto fail;
321 }
323 return 0;
325 fail:
326 free_netdev(netdev);
327 dev->dev.driver_data = NULL;
328 return err;
329 }
331 static int __devexit netfront_remove(struct xenbus_device *dev)
332 {
333 struct netfront_info *info = dev->dev.driver_data;
335 DPRINTK("%s\n", dev->nodename);
337 netif_disconnect_backend(info);
339 del_timer_sync(&info->rx_refill_timer);
341 xennet_sysfs_delif(info->netdev);
343 unregister_netdev(info->netdev);
345 free_netdev(info->netdev);
347 return 0;
348 }
350 /**
351 * We are reconnecting to the backend, due to a suspend/resume, or a backend
352 * driver restart. We tear down our netif structure and recreate it, but
353 * leave the device-layer structures intact so that this is transparent to the
354 * rest of the kernel.
355 */
356 static int netfront_resume(struct xenbus_device *dev)
357 {
358 struct netfront_info *info = dev->dev.driver_data;
360 DPRINTK("%s\n", dev->nodename);
362 netif_disconnect_backend(info);
363 return 0;
364 }
366 static int xen_net_read_mac(struct xenbus_device *dev, u8 mac[])
367 {
368 char *s, *e, *macstr;
369 int i;
371 macstr = s = xenbus_read(XBT_NIL, dev->nodename, "mac", NULL);
372 if (IS_ERR(macstr))
373 return PTR_ERR(macstr);
375 for (i = 0; i < ETH_ALEN; i++) {
376 mac[i] = simple_strtoul(s, &e, 16);
377 if ((s == e) || (*e != ((i == ETH_ALEN-1) ? '\0' : ':'))) {
378 kfree(macstr);
379 return -ENOENT;
380 }
381 s = e+1;
382 }
384 kfree(macstr);
385 return 0;
386 }
388 /* Common code used when first setting up, and when resuming. */
389 static int talk_to_backend(struct xenbus_device *dev,
390 struct netfront_info *info)
391 {
392 const char *message;
393 struct xenbus_transaction xbt;
394 int err;
396 err = xen_net_read_mac(dev, info->mac);
397 if (err) {
398 xenbus_dev_fatal(dev, err, "parsing %s/mac", dev->nodename);
399 goto out;
400 }
402 /* Create shared ring, alloc event channel. */
403 err = setup_device(dev, info);
404 if (err)
405 goto out;
407 again:
408 err = xenbus_transaction_start(&xbt);
409 if (err) {
410 xenbus_dev_fatal(dev, err, "starting transaction");
411 goto destroy_ring;
412 }
414 err = xenbus_printf(xbt, dev->nodename, "tx-ring-ref","%u",
415 info->tx_ring_ref);
416 if (err) {
417 message = "writing tx ring-ref";
418 goto abort_transaction;
419 }
420 err = xenbus_printf(xbt, dev->nodename, "rx-ring-ref","%u",
421 info->rx_ring_ref);
422 if (err) {
423 message = "writing rx ring-ref";
424 goto abort_transaction;
425 }
426 err = xenbus_printf(xbt, dev->nodename,
427 "event-channel", "%u",
428 irq_to_evtchn_port(info->irq));
429 if (err) {
430 message = "writing event-channel";
431 goto abort_transaction;
432 }
434 err = xenbus_printf(xbt, dev->nodename, "request-rx-copy", "%u",
435 info->copying_receiver);
436 if (err) {
437 message = "writing request-rx-copy";
438 goto abort_transaction;
439 }
441 err = xenbus_printf(xbt, dev->nodename, "feature-rx-notify", "%d", 1);
442 if (err) {
443 message = "writing feature-rx-notify";
444 goto abort_transaction;
445 }
447 #ifdef HAVE_NO_CSUM_OFFLOAD
448 err = xenbus_printf(xbt, dev->nodename, "feature-no-csum-offload", "%d", 1);
449 if (err) {
450 message = "writing feature-no-csum-offload";
451 goto abort_transaction;
452 }
453 #endif
455 err = xenbus_printf(xbt, dev->nodename, "feature-sg", "%d", 1);
456 if (err) {
457 message = "writing feature-sg";
458 goto abort_transaction;
459 }
461 #ifdef HAVE_TSO
462 err = xenbus_printf(xbt, dev->nodename, "feature-gso-tcpv4", "%d", 1);
463 if (err) {
464 message = "writing feature-gso-tcpv4";
465 goto abort_transaction;
466 }
467 #endif
469 err = xenbus_transaction_end(xbt, 0);
470 if (err) {
471 if (err == -EAGAIN)
472 goto again;
473 xenbus_dev_fatal(dev, err, "completing transaction");
474 goto destroy_ring;
475 }
477 return 0;
479 abort_transaction:
480 xenbus_transaction_end(xbt, 1);
481 xenbus_dev_fatal(dev, err, "%s", message);
482 destroy_ring:
483 netif_disconnect_backend(info);
484 out:
485 return err;
486 }
488 static int setup_device(struct xenbus_device *dev, struct netfront_info *info)
489 {
490 struct netif_tx_sring *txs;
491 struct netif_rx_sring *rxs;
492 int err;
493 struct net_device *netdev = info->netdev;
495 info->tx_ring_ref = GRANT_INVALID_REF;
496 info->rx_ring_ref = GRANT_INVALID_REF;
497 info->rx.sring = NULL;
498 info->tx.sring = NULL;
499 info->irq = 0;
501 txs = (struct netif_tx_sring *)get_zeroed_page(GFP_KERNEL);
502 if (!txs) {
503 err = -ENOMEM;
504 xenbus_dev_fatal(dev, err, "allocating tx ring page");
505 goto fail;
506 }
507 SHARED_RING_INIT(txs);
508 FRONT_RING_INIT(&info->tx, txs, PAGE_SIZE);
510 err = xenbus_grant_ring(dev, virt_to_mfn(txs));
511 if (err < 0) {
512 free_page((unsigned long)txs);
513 goto fail;
514 }
515 info->tx_ring_ref = err;
517 rxs = (struct netif_rx_sring *)get_zeroed_page(GFP_KERNEL);
518 if (!rxs) {
519 err = -ENOMEM;
520 xenbus_dev_fatal(dev, err, "allocating rx ring page");
521 goto fail;
522 }
523 SHARED_RING_INIT(rxs);
524 FRONT_RING_INIT(&info->rx, rxs, PAGE_SIZE);
526 err = xenbus_grant_ring(dev, virt_to_mfn(rxs));
527 if (err < 0) {
528 free_page((unsigned long)rxs);
529 goto fail;
530 }
531 info->rx_ring_ref = err;
533 memcpy(netdev->dev_addr, info->mac, ETH_ALEN);
535 err = bind_listening_port_to_irqhandler(
536 dev->otherend_id, netif_int, SA_SAMPLE_RANDOM, netdev->name,
537 netdev);
538 if (err < 0)
539 goto fail;
540 info->irq = err;
542 return 0;
544 fail:
545 return err;
546 }
548 /**
549 * Callback received when the backend's state changes.
550 */
551 static void backend_changed(struct xenbus_device *dev,
552 enum xenbus_state backend_state)
553 {
554 struct netfront_info *np = dev->dev.driver_data;
555 struct net_device *netdev = np->netdev;
557 DPRINTK("%s\n", xenbus_strstate(backend_state));
559 switch (backend_state) {
560 case XenbusStateInitialising:
561 case XenbusStateInitialised:
562 case XenbusStateConnected:
563 case XenbusStateUnknown:
564 case XenbusStateClosed:
565 break;
567 case XenbusStateInitWait:
568 if (dev->state != XenbusStateInitialising)
569 break;
570 if (network_connect(netdev) != 0)
571 break;
572 xenbus_switch_state(dev, XenbusStateConnected);
573 (void)send_fake_arp(netdev);
574 break;
576 case XenbusStateClosing:
577 xenbus_frontend_closed(dev);
578 break;
579 }
580 }
582 /** Send a packet on a net device to encourage switches to learn the
583 * MAC. We send a fake ARP request.
584 *
585 * @param dev device
586 * @return 0 on success, error code otherwise
587 */
588 static int send_fake_arp(struct net_device *dev)
589 {
590 struct sk_buff *skb;
591 u32 src_ip, dst_ip;
593 dst_ip = INADDR_BROADCAST;
594 src_ip = inet_select_addr(dev, dst_ip, RT_SCOPE_LINK);
596 /* No IP? Then nothing to do. */
597 if (src_ip == 0)
598 return 0;
600 skb = arp_create(ARPOP_REPLY, ETH_P_ARP,
601 dst_ip, dev, src_ip,
602 /*dst_hw*/ NULL, /*src_hw*/ NULL,
603 /*target_hw*/ dev->dev_addr);
604 if (skb == NULL)
605 return -ENOMEM;
607 return dev_queue_xmit(skb);
608 }
610 static inline int netfront_tx_slot_available(struct netfront_info *np)
611 {
612 return ((np->tx.req_prod_pvt - np->tx.rsp_cons) <
613 (TX_MAX_TARGET - MAX_SKB_FRAGS - 2));
614 }
616 static inline void network_maybe_wake_tx(struct net_device *dev)
617 {
618 struct netfront_info *np = netdev_priv(dev);
620 if (unlikely(netif_queue_stopped(dev)) &&
621 netfront_tx_slot_available(np) &&
622 likely(netif_running(dev)))
623 netif_wake_queue(dev);
624 }
626 static int network_open(struct net_device *dev)
627 {
628 struct netfront_info *np = netdev_priv(dev);
630 memset(&np->stats, 0, sizeof(np->stats));
632 spin_lock_bh(&np->rx_lock);
633 if (netfront_carrier_ok(np)) {
634 network_alloc_rx_buffers(dev);
635 np->rx.sring->rsp_event = np->rx.rsp_cons + 1;
636 if (RING_HAS_UNCONSUMED_RESPONSES(&np->rx))
637 netif_rx_schedule(dev);
638 }
639 spin_unlock_bh(&np->rx_lock);
641 network_maybe_wake_tx(dev);
643 return 0;
644 }
646 static void network_tx_buf_gc(struct net_device *dev)
647 {
648 RING_IDX cons, prod;
649 unsigned short id;
650 struct netfront_info *np = netdev_priv(dev);
651 struct sk_buff *skb;
653 BUG_ON(!netfront_carrier_ok(np));
655 do {
656 prod = np->tx.sring->rsp_prod;
657 rmb(); /* Ensure we see responses up to 'rp'. */
659 for (cons = np->tx.rsp_cons; cons != prod; cons++) {
660 struct netif_tx_response *txrsp;
662 txrsp = RING_GET_RESPONSE(&np->tx, cons);
663 if (txrsp->status == NETIF_RSP_NULL)
664 continue;
666 id = txrsp->id;
667 skb = np->tx_skbs[id];
668 if (unlikely(gnttab_query_foreign_access(
669 np->grant_tx_ref[id]) != 0)) {
670 printk(KERN_ALERT "network_tx_buf_gc: warning "
671 "-- grant still in use by backend "
672 "domain.\n");
673 BUG();
674 }
675 gnttab_end_foreign_access_ref(
676 np->grant_tx_ref[id], GNTMAP_readonly);
677 gnttab_release_grant_reference(
678 &np->gref_tx_head, np->grant_tx_ref[id]);
679 np->grant_tx_ref[id] = GRANT_INVALID_REF;
680 add_id_to_freelist(np->tx_skbs, id);
681 dev_kfree_skb_irq(skb);
682 }
684 np->tx.rsp_cons = prod;
686 /*
687 * Set a new event, then check for race with update of tx_cons.
688 * Note that it is essential to schedule a callback, no matter
689 * how few buffers are pending. Even if there is space in the
690 * transmit ring, higher layers may be blocked because too much
691 * data is outstanding: in such cases notification from Xen is
692 * likely to be the only kick that we'll get.
693 */
694 np->tx.sring->rsp_event =
695 prod + ((np->tx.sring->req_prod - prod) >> 1) + 1;
696 mb();
697 } while ((cons == prod) && (prod != np->tx.sring->rsp_prod));
699 network_maybe_wake_tx(dev);
700 }
702 static void rx_refill_timeout(unsigned long data)
703 {
704 struct net_device *dev = (struct net_device *)data;
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 (void)HYPERVISOR_multicall(np->rx_mcl, i+1);
846 /* Check return status of HYPERVISOR_memory_op(). */
847 if (unlikely(np->rx_mcl[i].result != i))
848 panic("Unable to reduce memory reservation\n");
849 } else {
850 if (HYPERVISOR_memory_op(XENMEM_decrease_reservation,
851 &reservation) != i)
852 panic("Unable to reduce memory reservation\n");
853 }
854 } else {
855 wmb();
856 }
858 /* Above is a suitable barrier to ensure backend will see requests. */
859 np->rx.req_prod_pvt = req_prod + i;
860 push:
861 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&np->rx, notify);
862 if (notify)
863 notify_remote_via_irq(np->irq);
864 }
866 static void xennet_make_frags(struct sk_buff *skb, struct net_device *dev,
867 struct netif_tx_request *tx)
868 {
869 struct netfront_info *np = netdev_priv(dev);
870 char *data = skb->data;
871 unsigned long mfn;
872 RING_IDX prod = np->tx.req_prod_pvt;
873 int frags = skb_shinfo(skb)->nr_frags;
874 unsigned int offset = offset_in_page(data);
875 unsigned int len = skb_headlen(skb);
876 unsigned int id;
877 grant_ref_t ref;
878 int i;
880 while (len > PAGE_SIZE - offset) {
881 tx->size = PAGE_SIZE - offset;
882 tx->flags |= NETTXF_more_data;
883 len -= tx->size;
884 data += tx->size;
885 offset = 0;
887 id = get_id_from_freelist(np->tx_skbs);
888 np->tx_skbs[id] = skb_get(skb);
889 tx = RING_GET_REQUEST(&np->tx, prod++);
890 tx->id = id;
891 ref = gnttab_claim_grant_reference(&np->gref_tx_head);
892 BUG_ON((signed short)ref < 0);
894 mfn = virt_to_mfn(data);
895 gnttab_grant_foreign_access_ref(ref, np->xbdev->otherend_id,
896 mfn, GNTMAP_readonly);
898 tx->gref = np->grant_tx_ref[id] = ref;
899 tx->offset = offset;
900 tx->size = len;
901 tx->flags = 0;
902 }
904 for (i = 0; i < frags; i++) {
905 skb_frag_t *frag = skb_shinfo(skb)->frags + i;
907 tx->flags |= NETTXF_more_data;
909 id = get_id_from_freelist(np->tx_skbs);
910 np->tx_skbs[id] = skb_get(skb);
911 tx = RING_GET_REQUEST(&np->tx, prod++);
912 tx->id = id;
913 ref = gnttab_claim_grant_reference(&np->gref_tx_head);
914 BUG_ON((signed short)ref < 0);
916 mfn = pfn_to_mfn(page_to_pfn(frag->page));
917 gnttab_grant_foreign_access_ref(ref, np->xbdev->otherend_id,
918 mfn, GNTMAP_readonly);
920 tx->gref = np->grant_tx_ref[id] = ref;
921 tx->offset = frag->page_offset;
922 tx->size = frag->size;
923 tx->flags = 0;
924 }
926 np->tx.req_prod_pvt = prod;
927 }
929 static int network_start_xmit(struct sk_buff *skb, struct net_device *dev)
930 {
931 unsigned short id;
932 struct netfront_info *np = netdev_priv(dev);
933 struct netif_tx_request *tx;
934 struct netif_extra_info *extra;
935 char *data = skb->data;
936 RING_IDX i;
937 grant_ref_t ref;
938 unsigned long mfn;
939 int notify;
940 int frags = skb_shinfo(skb)->nr_frags;
941 unsigned int offset = offset_in_page(data);
942 unsigned int len = skb_headlen(skb);
944 frags += (offset + len + PAGE_SIZE - 1) / PAGE_SIZE;
945 if (unlikely(frags > MAX_SKB_FRAGS + 1)) {
946 printk(KERN_ALERT "xennet: skb rides the rocket: %d frags\n",
947 frags);
948 dump_stack();
949 goto drop;
950 }
952 spin_lock_irq(&np->tx_lock);
954 if (unlikely(!netfront_carrier_ok(np) ||
955 (frags > 1 && !xennet_can_sg(dev)) ||
956 netif_needs_gso(dev, skb))) {
957 spin_unlock_irq(&np->tx_lock);
958 goto drop;
959 }
961 i = np->tx.req_prod_pvt;
963 id = get_id_from_freelist(np->tx_skbs);
964 np->tx_skbs[id] = skb;
966 tx = RING_GET_REQUEST(&np->tx, i);
968 tx->id = id;
969 ref = gnttab_claim_grant_reference(&np->gref_tx_head);
970 BUG_ON((signed short)ref < 0);
971 mfn = virt_to_mfn(data);
972 gnttab_grant_foreign_access_ref(
973 ref, np->xbdev->otherend_id, mfn, GNTMAP_readonly);
974 tx->gref = np->grant_tx_ref[id] = ref;
975 tx->offset = offset;
976 tx->size = len;
978 tx->flags = 0;
979 extra = NULL;
981 if (skb->ip_summed == CHECKSUM_HW) /* local packet? */
982 tx->flags |= NETTXF_csum_blank | NETTXF_data_validated;
983 #ifdef CONFIG_XEN
984 if (skb->proto_data_valid) /* remote but checksummed? */
985 tx->flags |= NETTXF_data_validated;
986 #endif
988 #ifdef HAVE_TSO
989 if (skb_shinfo(skb)->gso_size) {
990 struct netif_extra_info *gso = (struct netif_extra_info *)
991 RING_GET_REQUEST(&np->tx, ++i);
993 if (extra)
994 extra->flags |= XEN_NETIF_EXTRA_FLAG_MORE;
995 else
996 tx->flags |= NETTXF_extra_info;
998 gso->u.gso.size = skb_shinfo(skb)->gso_size;
999 gso->u.gso.type = XEN_NETIF_GSO_TYPE_TCPV4;
1000 gso->u.gso.pad = 0;
1001 gso->u.gso.features = 0;
1003 gso->type = XEN_NETIF_EXTRA_TYPE_GSO;
1004 gso->flags = 0;
1005 extra = gso;
1007 #endif
1009 np->tx.req_prod_pvt = i + 1;
1011 xennet_make_frags(skb, dev, tx);
1012 tx->size = skb->len;
1014 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&np->tx, notify);
1015 if (notify)
1016 notify_remote_via_irq(np->irq);
1018 network_tx_buf_gc(dev);
1020 if (!netfront_tx_slot_available(np))
1021 netif_stop_queue(dev);
1023 spin_unlock_irq(&np->tx_lock);
1025 np->stats.tx_bytes += skb->len;
1026 np->stats.tx_packets++;
1028 return 0;
1030 drop:
1031 np->stats.tx_dropped++;
1032 dev_kfree_skb(skb);
1033 return 0;
1036 static irqreturn_t netif_int(int irq, void *dev_id, struct pt_regs *ptregs)
1038 struct net_device *dev = dev_id;
1039 struct netfront_info *np = netdev_priv(dev);
1040 unsigned long flags;
1042 spin_lock_irqsave(&np->tx_lock, flags);
1044 if (likely(netfront_carrier_ok(np))) {
1045 network_tx_buf_gc(dev);
1046 /* Under tx_lock: protects access to rx shared-ring indexes. */
1047 if (RING_HAS_UNCONSUMED_RESPONSES(&np->rx))
1048 netif_rx_schedule(dev);
1051 spin_unlock_irqrestore(&np->tx_lock, flags);
1053 return IRQ_HANDLED;
1056 static void xennet_move_rx_slot(struct netfront_info *np, struct sk_buff *skb,
1057 grant_ref_t ref)
1059 int new = xennet_rxidx(np->rx.req_prod_pvt);
1061 BUG_ON(np->rx_skbs[new]);
1062 np->rx_skbs[new] = skb;
1063 np->grant_rx_ref[new] = ref;
1064 RING_GET_REQUEST(&np->rx, np->rx.req_prod_pvt)->id = new;
1065 RING_GET_REQUEST(&np->rx, np->rx.req_prod_pvt)->gref = ref;
1066 np->rx.req_prod_pvt++;
1069 int xennet_get_extras(struct netfront_info *np,
1070 struct netif_extra_info *extras, RING_IDX rp)
1073 struct netif_extra_info *extra;
1074 RING_IDX cons = np->rx.rsp_cons;
1075 int err = 0;
1077 do {
1078 struct sk_buff *skb;
1079 grant_ref_t ref;
1081 if (unlikely(cons + 1 == rp)) {
1082 if (net_ratelimit())
1083 WPRINTK("Missing extra info\n");
1084 err = -EBADR;
1085 break;
1088 extra = (struct netif_extra_info *)
1089 RING_GET_RESPONSE(&np->rx, ++cons);
1091 if (unlikely(!extra->type ||
1092 extra->type >= XEN_NETIF_EXTRA_TYPE_MAX)) {
1093 if (net_ratelimit())
1094 WPRINTK("Invalid extra type: %d\n",
1095 extra->type);
1096 err = -EINVAL;
1097 } else {
1098 memcpy(&extras[extra->type - 1], extra,
1099 sizeof(*extra));
1102 skb = xennet_get_rx_skb(np, cons);
1103 ref = xennet_get_rx_ref(np, cons);
1104 xennet_move_rx_slot(np, skb, ref);
1105 } while (extra->flags & XEN_NETIF_EXTRA_FLAG_MORE);
1107 np->rx.rsp_cons = cons;
1108 return err;
1111 static int xennet_get_responses(struct netfront_info *np,
1112 struct netfront_rx_info *rinfo, RING_IDX rp,
1113 struct sk_buff_head *list,
1114 int *pages_flipped_p)
1116 int pages_flipped = *pages_flipped_p;
1117 struct mmu_update *mmu;
1118 struct multicall_entry *mcl;
1119 struct netif_rx_response *rx = &rinfo->rx;
1120 struct netif_extra_info *extras = rinfo->extras;
1121 RING_IDX cons = np->rx.rsp_cons;
1122 struct sk_buff *skb = xennet_get_rx_skb(np, cons);
1123 grant_ref_t ref = xennet_get_rx_ref(np, cons);
1124 int max = MAX_SKB_FRAGS + (rx->status <= RX_COPY_THRESHOLD);
1125 int frags = 1;
1126 int err = 0;
1127 unsigned long ret;
1129 if (rx->flags & NETRXF_extra_info) {
1130 err = xennet_get_extras(np, extras, rp);
1131 cons = np->rx.rsp_cons;
1134 for (;;) {
1135 unsigned long mfn;
1137 if (unlikely(rx->status < 0 ||
1138 rx->offset + rx->status > PAGE_SIZE)) {
1139 if (net_ratelimit())
1140 WPRINTK("rx->offset: %x, size: %u\n",
1141 rx->offset, rx->status);
1142 xennet_move_rx_slot(np, skb, ref);
1143 err = -EINVAL;
1144 goto next;
1147 /*
1148 * This definitely indicates a bug, either in this driver or in
1149 * the backend driver. In future this should flag the bad
1150 * situation to the system controller to reboot the backed.
1151 */
1152 if (ref == GRANT_INVALID_REF) {
1153 if (net_ratelimit())
1154 WPRINTK("Bad rx response id %d.\n", rx->id);
1155 err = -EINVAL;
1156 goto next;
1159 if (!np->copying_receiver) {
1160 /* Memory pressure, insufficient buffer
1161 * headroom, ... */
1162 if (!(mfn = gnttab_end_foreign_transfer_ref(ref))) {
1163 if (net_ratelimit())
1164 WPRINTK("Unfulfilled rx req "
1165 "(id=%d, st=%d).\n",
1166 rx->id, rx->status);
1167 xennet_move_rx_slot(np, skb, ref);
1168 err = -ENOMEM;
1169 goto next;
1172 if (!xen_feature(XENFEAT_auto_translated_physmap)) {
1173 /* Remap the page. */
1174 struct page *page =
1175 skb_shinfo(skb)->frags[0].page;
1176 unsigned long pfn = page_to_pfn(page);
1177 void *vaddr = page_address(page);
1179 mcl = np->rx_mcl + pages_flipped;
1180 mmu = np->rx_mmu + pages_flipped;
1182 MULTI_update_va_mapping(mcl,
1183 (unsigned long)vaddr,
1184 pfn_pte_ma(mfn,
1185 PAGE_KERNEL),
1186 0);
1187 mmu->ptr = ((maddr_t)mfn << PAGE_SHIFT)
1188 | MMU_MACHPHYS_UPDATE;
1189 mmu->val = pfn;
1191 set_phys_to_machine(pfn, mfn);
1193 pages_flipped++;
1194 } else {
1195 ret = gnttab_end_foreign_access_ref(ref, 0);
1196 BUG_ON(!ret);
1199 gnttab_release_grant_reference(&np->gref_rx_head, ref);
1201 __skb_queue_tail(list, skb);
1203 next:
1204 if (!(rx->flags & NETRXF_more_data))
1205 break;
1207 if (cons + frags == rp) {
1208 if (net_ratelimit())
1209 WPRINTK("Need more frags\n");
1210 err = -ENOENT;
1211 break;
1214 rx = RING_GET_RESPONSE(&np->rx, cons + frags);
1215 skb = xennet_get_rx_skb(np, cons + frags);
1216 ref = xennet_get_rx_ref(np, cons + frags);
1217 frags++;
1220 if (unlikely(frags > max)) {
1221 if (net_ratelimit())
1222 WPRINTK("Too many frags\n");
1223 err = -E2BIG;
1226 if (unlikely(err))
1227 np->rx.rsp_cons = cons + frags;
1229 *pages_flipped_p = pages_flipped;
1231 return err;
1234 static RING_IDX xennet_fill_frags(struct netfront_info *np,
1235 struct sk_buff *skb,
1236 struct sk_buff_head *list)
1238 struct skb_shared_info *shinfo = skb_shinfo(skb);
1239 int nr_frags = shinfo->nr_frags;
1240 RING_IDX cons = np->rx.rsp_cons;
1241 skb_frag_t *frag = shinfo->frags + nr_frags;
1242 struct sk_buff *nskb;
1244 while ((nskb = __skb_dequeue(list))) {
1245 struct netif_rx_response *rx =
1246 RING_GET_RESPONSE(&np->rx, ++cons);
1248 frag->page = skb_shinfo(nskb)->frags[0].page;
1249 frag->page_offset = rx->offset;
1250 frag->size = rx->status;
1252 skb->data_len += rx->status;
1254 skb_shinfo(nskb)->nr_frags = 0;
1255 kfree_skb(nskb);
1257 frag++;
1258 nr_frags++;
1261 shinfo->nr_frags = nr_frags;
1262 return cons;
1265 static int xennet_set_skb_gso(struct sk_buff *skb,
1266 struct netif_extra_info *gso)
1268 if (!gso->u.gso.size) {
1269 if (net_ratelimit())
1270 WPRINTK("GSO size must not be zero.\n");
1271 return -EINVAL;
1274 /* Currently only TCPv4 S.O. is supported. */
1275 if (gso->u.gso.type != XEN_NETIF_GSO_TYPE_TCPV4) {
1276 if (net_ratelimit())
1277 WPRINTK("Bad GSO type %d.\n", gso->u.gso.type);
1278 return -EINVAL;
1281 #ifdef HAVE_TSO
1282 skb_shinfo(skb)->gso_size = gso->u.gso.size;
1283 #ifdef HAVE_GSO
1284 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
1286 /* Header must be checked, and gso_segs computed. */
1287 skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
1288 #endif
1289 skb_shinfo(skb)->gso_segs = 0;
1291 return 0;
1292 #else
1293 if (net_ratelimit())
1294 WPRINTK("GSO unsupported by this kernel.\n");
1295 return -EINVAL;
1296 #endif
1299 static int netif_poll(struct net_device *dev, int *pbudget)
1301 struct netfront_info *np = netdev_priv(dev);
1302 struct sk_buff *skb;
1303 struct netfront_rx_info rinfo;
1304 struct netif_rx_response *rx = &rinfo.rx;
1305 struct netif_extra_info *extras = rinfo.extras;
1306 RING_IDX i, rp;
1307 struct multicall_entry *mcl;
1308 int work_done, budget, more_to_do = 1;
1309 struct sk_buff_head rxq;
1310 struct sk_buff_head errq;
1311 struct sk_buff_head tmpq;
1312 unsigned long flags;
1313 unsigned int len;
1314 int pages_flipped = 0;
1315 int err;
1317 spin_lock(&np->rx_lock); /* no need for spin_lock_bh() in ->poll() */
1319 if (unlikely(!netfront_carrier_ok(np))) {
1320 spin_unlock(&np->rx_lock);
1321 return 0;
1324 skb_queue_head_init(&rxq);
1325 skb_queue_head_init(&errq);
1326 skb_queue_head_init(&tmpq);
1328 if ((budget = *pbudget) > dev->quota)
1329 budget = dev->quota;
1330 rp = np->rx.sring->rsp_prod;
1331 rmb(); /* Ensure we see queued responses up to 'rp'. */
1333 i = np->rx.rsp_cons;
1334 work_done = 0;
1335 while ((i != rp) && (work_done < budget)) {
1336 memcpy(rx, RING_GET_RESPONSE(&np->rx, i), sizeof(*rx));
1337 memset(extras, 0, sizeof(rinfo.extras));
1339 err = xennet_get_responses(np, &rinfo, rp, &tmpq,
1340 &pages_flipped);
1342 if (unlikely(err)) {
1343 err:
1344 while ((skb = __skb_dequeue(&tmpq)))
1345 __skb_queue_tail(&errq, skb);
1346 np->stats.rx_errors++;
1347 i = np->rx.rsp_cons;
1348 continue;
1351 skb = __skb_dequeue(&tmpq);
1353 if (extras[XEN_NETIF_EXTRA_TYPE_GSO - 1].type) {
1354 struct netif_extra_info *gso;
1355 gso = &extras[XEN_NETIF_EXTRA_TYPE_GSO - 1];
1357 if (unlikely(xennet_set_skb_gso(skb, gso))) {
1358 __skb_queue_head(&tmpq, skb);
1359 np->rx.rsp_cons += skb_queue_len(&tmpq);
1360 goto err;
1364 NETFRONT_SKB_CB(skb)->page = skb_shinfo(skb)->frags[0].page;
1365 NETFRONT_SKB_CB(skb)->offset = rx->offset;
1367 len = rx->status;
1368 if (len > RX_COPY_THRESHOLD)
1369 len = RX_COPY_THRESHOLD;
1370 skb_put(skb, len);
1372 if (rx->status > len) {
1373 skb_shinfo(skb)->frags[0].page_offset =
1374 rx->offset + len;
1375 skb_shinfo(skb)->frags[0].size = rx->status - len;
1376 skb->data_len = rx->status - len;
1377 } else {
1378 skb_shinfo(skb)->frags[0].page = NULL;
1379 skb_shinfo(skb)->nr_frags = 0;
1382 i = xennet_fill_frags(np, skb, &tmpq);
1384 /*
1385 * Truesize must approximates the size of true data plus
1386 * any supervisor overheads. Adding hypervisor overheads
1387 * has been shown to significantly reduce achievable
1388 * bandwidth with the default receive buffer size. It is
1389 * therefore not wise to account for it here.
1391 * After alloc_skb(RX_COPY_THRESHOLD), truesize is set to
1392 * RX_COPY_THRESHOLD + the supervisor overheads. Here, we
1393 * add the size of the data pulled in xennet_fill_frags().
1395 * We also adjust for any unused space in the main data
1396 * area by subtracting (RX_COPY_THRESHOLD - len). This is
1397 * especially important with drivers which split incoming
1398 * packets into header and data, using only 66 bytes of
1399 * the main data area (see the e1000 driver for example.)
1400 * On such systems, without this last adjustement, our
1401 * achievable receive throughout using the standard receive
1402 * buffer size was cut by 25%(!!!).
1403 */
1404 skb->truesize += skb->data_len - (RX_COPY_THRESHOLD - len);
1405 skb->len += skb->data_len;
1407 /*
1408 * Old backends do not assert data_validated but we
1409 * can infer it from csum_blank so test both flags.
1410 */
1411 if (rx->flags & (NETRXF_data_validated|NETRXF_csum_blank))
1412 skb->ip_summed = CHECKSUM_UNNECESSARY;
1413 else
1414 skb->ip_summed = CHECKSUM_NONE;
1415 #ifdef CONFIG_XEN
1416 skb->proto_data_valid = (skb->ip_summed != CHECKSUM_NONE);
1417 skb->proto_csum_blank = !!(rx->flags & NETRXF_csum_blank);
1418 #endif
1419 np->stats.rx_packets++;
1420 np->stats.rx_bytes += skb->len;
1422 __skb_queue_tail(&rxq, skb);
1424 np->rx.rsp_cons = ++i;
1425 work_done++;
1428 if (pages_flipped) {
1429 /* Some pages are no longer absent... */
1430 balloon_update_driver_allowance(-pages_flipped);
1432 /* Do all the remapping work and M2P updates. */
1433 if (!xen_feature(XENFEAT_auto_translated_physmap)) {
1434 mcl = np->rx_mcl + pages_flipped;
1435 mcl->op = __HYPERVISOR_mmu_update;
1436 mcl->args[0] = (unsigned long)np->rx_mmu;
1437 mcl->args[1] = pages_flipped;
1438 mcl->args[2] = 0;
1439 mcl->args[3] = DOMID_SELF;
1440 (void)HYPERVISOR_multicall(np->rx_mcl,
1441 pages_flipped + 1);
1445 while ((skb = __skb_dequeue(&errq)))
1446 kfree_skb(skb);
1448 while ((skb = __skb_dequeue(&rxq)) != NULL) {
1449 struct page *page = NETFRONT_SKB_CB(skb)->page;
1450 void *vaddr = page_address(page);
1451 unsigned offset = NETFRONT_SKB_CB(skb)->offset;
1453 memcpy(skb->data, vaddr + offset, skb_headlen(skb));
1455 if (page != skb_shinfo(skb)->frags[0].page)
1456 __free_page(page);
1458 /* Ethernet work: Delayed to here as it peeks the header. */
1459 skb->protocol = eth_type_trans(skb, dev);
1461 /* Pass it up. */
1462 netif_receive_skb(skb);
1463 dev->last_rx = jiffies;
1466 /* If we get a callback with very few responses, reduce fill target. */
1467 /* NB. Note exponential increase, linear decrease. */
1468 if (((np->rx.req_prod_pvt - np->rx.sring->rsp_prod) >
1469 ((3*np->rx_target) / 4)) &&
1470 (--np->rx_target < np->rx_min_target))
1471 np->rx_target = np->rx_min_target;
1473 network_alloc_rx_buffers(dev);
1475 *pbudget -= work_done;
1476 dev->quota -= work_done;
1478 if (work_done < budget) {
1479 local_irq_save(flags);
1481 RING_FINAL_CHECK_FOR_RESPONSES(&np->rx, more_to_do);
1482 if (!more_to_do)
1483 __netif_rx_complete(dev);
1485 local_irq_restore(flags);
1488 spin_unlock(&np->rx_lock);
1490 return more_to_do;
1493 static void netif_release_tx_bufs(struct netfront_info *np)
1495 struct sk_buff *skb;
1496 int i;
1498 for (i = 1; i <= NET_TX_RING_SIZE; i++) {
1499 if ((unsigned long)np->tx_skbs[i] < PAGE_OFFSET)
1500 continue;
1502 skb = np->tx_skbs[i];
1503 gnttab_end_foreign_access_ref(
1504 np->grant_tx_ref[i], GNTMAP_readonly);
1505 gnttab_release_grant_reference(
1506 &np->gref_tx_head, np->grant_tx_ref[i]);
1507 np->grant_tx_ref[i] = GRANT_INVALID_REF;
1508 add_id_to_freelist(np->tx_skbs, i);
1509 dev_kfree_skb_irq(skb);
1513 static void netif_release_rx_bufs(struct netfront_info *np)
1515 struct mmu_update *mmu = np->rx_mmu;
1516 struct multicall_entry *mcl = np->rx_mcl;
1517 struct sk_buff_head free_list;
1518 struct sk_buff *skb;
1519 unsigned long mfn;
1520 int xfer = 0, noxfer = 0, unused = 0;
1521 int id, ref, rc;
1523 if (np->copying_receiver) {
1524 WPRINTK("%s: fix me for copying receiver.\n", __FUNCTION__);
1525 return;
1528 skb_queue_head_init(&free_list);
1530 spin_lock_bh(&np->rx_lock);
1532 for (id = 0; id < NET_RX_RING_SIZE; id++) {
1533 if ((ref = np->grant_rx_ref[id]) == GRANT_INVALID_REF) {
1534 unused++;
1535 continue;
1538 skb = np->rx_skbs[id];
1539 mfn = gnttab_end_foreign_transfer_ref(ref);
1540 gnttab_release_grant_reference(&np->gref_rx_head, ref);
1541 np->grant_rx_ref[id] = GRANT_INVALID_REF;
1542 add_id_to_freelist(np->rx_skbs, id);
1544 if (0 == mfn) {
1545 struct page *page = skb_shinfo(skb)->frags[0].page;
1546 balloon_release_driver_page(page);
1547 skb_shinfo(skb)->nr_frags = 0;
1548 dev_kfree_skb(skb);
1549 noxfer++;
1550 continue;
1553 if (!xen_feature(XENFEAT_auto_translated_physmap)) {
1554 /* Remap the page. */
1555 struct page *page = skb_shinfo(skb)->frags[0].page;
1556 unsigned long pfn = page_to_pfn(page);
1557 void *vaddr = page_address(page);
1559 MULTI_update_va_mapping(mcl, (unsigned long)vaddr,
1560 pfn_pte_ma(mfn, PAGE_KERNEL),
1561 0);
1562 mcl++;
1563 mmu->ptr = ((maddr_t)mfn << PAGE_SHIFT)
1564 | MMU_MACHPHYS_UPDATE;
1565 mmu->val = pfn;
1566 mmu++;
1568 set_phys_to_machine(pfn, mfn);
1570 __skb_queue_tail(&free_list, skb);
1571 xfer++;
1574 IPRINTK("%s: %d xfer, %d noxfer, %d unused\n",
1575 __FUNCTION__, xfer, noxfer, unused);
1577 if (xfer) {
1578 /* Some pages are no longer absent... */
1579 balloon_update_driver_allowance(-xfer);
1581 if (!xen_feature(XENFEAT_auto_translated_physmap)) {
1582 /* Do all the remapping work and M2P updates. */
1583 mcl->op = __HYPERVISOR_mmu_update;
1584 mcl->args[0] = (unsigned long)np->rx_mmu;
1585 mcl->args[1] = mmu - np->rx_mmu;
1586 mcl->args[2] = 0;
1587 mcl->args[3] = DOMID_SELF;
1588 mcl++;
1589 rc = HYPERVISOR_multicall_check(
1590 np->rx_mcl, mcl - np->rx_mcl, NULL);
1591 BUG_ON(rc);
1595 while ((skb = __skb_dequeue(&free_list)) != NULL)
1596 dev_kfree_skb(skb);
1598 spin_unlock_bh(&np->rx_lock);
1601 static int network_close(struct net_device *dev)
1603 struct netfront_info *np = netdev_priv(dev);
1604 netif_stop_queue(np->netdev);
1605 return 0;
1609 static struct net_device_stats *network_get_stats(struct net_device *dev)
1611 struct netfront_info *np = netdev_priv(dev);
1612 return &np->stats;
1615 static int xennet_change_mtu(struct net_device *dev, int mtu)
1617 int max = xennet_can_sg(dev) ? 65535 - ETH_HLEN : ETH_DATA_LEN;
1619 if (mtu > max)
1620 return -EINVAL;
1621 dev->mtu = mtu;
1622 return 0;
1625 static int xennet_set_sg(struct net_device *dev, u32 data)
1627 if (data) {
1628 struct netfront_info *np = netdev_priv(dev);
1629 int val;
1631 if (xenbus_scanf(XBT_NIL, np->xbdev->otherend, "feature-sg",
1632 "%d", &val) < 0)
1633 val = 0;
1634 if (!val)
1635 return -ENOSYS;
1636 } else if (dev->mtu > ETH_DATA_LEN)
1637 dev->mtu = ETH_DATA_LEN;
1639 return ethtool_op_set_sg(dev, data);
1642 static int xennet_set_tso(struct net_device *dev, u32 data)
1644 #ifdef HAVE_TSO
1645 if (data) {
1646 struct netfront_info *np = netdev_priv(dev);
1647 int val;
1649 if (xenbus_scanf(XBT_NIL, np->xbdev->otherend,
1650 "feature-gso-tcpv4", "%d", &val) < 0)
1651 val = 0;
1652 if (!val)
1653 return -ENOSYS;
1656 return ethtool_op_set_tso(dev, data);
1657 #else
1658 return -ENOSYS;
1659 #endif
1662 static void xennet_set_features(struct net_device *dev)
1664 dev_disable_gso_features(dev);
1665 xennet_set_sg(dev, 0);
1667 /* We need checksum offload to enable scatter/gather and TSO. */
1668 if (!(dev->features & NETIF_F_IP_CSUM))
1669 return;
1671 if (xennet_set_sg(dev, 1))
1672 return;
1674 /* Before 2.6.9 TSO seems to be unreliable so do not enable it
1675 * on older kernels.
1676 */
1677 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,9)
1678 xennet_set_tso(dev, 1);
1679 #endif
1683 static int network_connect(struct net_device *dev)
1685 struct netfront_info *np = netdev_priv(dev);
1686 int i, requeue_idx, err;
1687 struct sk_buff *skb;
1688 grant_ref_t ref;
1689 netif_rx_request_t *req;
1690 unsigned int feature_rx_copy, feature_rx_flip;
1692 err = xenbus_scanf(XBT_NIL, np->xbdev->otherend,
1693 "feature-rx-copy", "%u", &feature_rx_copy);
1694 if (err != 1)
1695 feature_rx_copy = 0;
1696 err = xenbus_scanf(XBT_NIL, np->xbdev->otherend,
1697 "feature-rx-flip", "%u", &feature_rx_flip);
1698 if (err != 1)
1699 feature_rx_flip = 1;
1701 /*
1702 * Copy packets on receive path if:
1703 * (a) This was requested by user, and the backend supports it; or
1704 * (b) Flipping was requested, but this is unsupported by the backend.
1705 */
1706 np->copying_receiver = ((MODPARM_rx_copy && feature_rx_copy) ||
1707 (MODPARM_rx_flip && !feature_rx_flip));
1709 err = talk_to_backend(np->xbdev, np);
1710 if (err)
1711 return err;
1713 xennet_set_features(dev);
1715 IPRINTK("device %s has %sing receive path.\n",
1716 dev->name, np->copying_receiver ? "copy" : "flipp");
1718 spin_lock_bh(&np->rx_lock);
1719 spin_lock_irq(&np->tx_lock);
1721 /*
1722 * Recovery procedure:
1723 * NB. Freelist index entries are always going to be less than
1724 * PAGE_OFFSET, whereas pointers to skbs will always be equal or
1725 * greater than PAGE_OFFSET: we use this property to distinguish
1726 * them.
1727 */
1729 /* Step 1: Discard all pending TX packet fragments. */
1730 netif_release_tx_bufs(np);
1732 /* Step 2: Rebuild the RX buffer freelist and the RX ring itself. */
1733 for (requeue_idx = 0, i = 0; i < NET_RX_RING_SIZE; i++) {
1734 if (!np->rx_skbs[i])
1735 continue;
1737 skb = np->rx_skbs[requeue_idx] = xennet_get_rx_skb(np, i);
1738 ref = np->grant_rx_ref[requeue_idx] = xennet_get_rx_ref(np, i);
1739 req = RING_GET_REQUEST(&np->rx, requeue_idx);
1741 if (!np->copying_receiver) {
1742 gnttab_grant_foreign_transfer_ref(
1743 ref, np->xbdev->otherend_id,
1744 page_to_pfn(skb_shinfo(skb)->frags->page));
1745 } else {
1746 gnttab_grant_foreign_access_ref(
1747 ref, np->xbdev->otherend_id,
1748 pfn_to_mfn(page_to_pfn(skb_shinfo(skb)->
1749 frags->page)),
1750 0);
1752 req->gref = ref;
1753 req->id = requeue_idx;
1755 requeue_idx++;
1758 np->rx.req_prod_pvt = requeue_idx;
1760 /*
1761 * Step 3: All public and private state should now be sane. Get
1762 * ready to start sending and receiving packets and give the driver
1763 * domain a kick because we've probably just requeued some
1764 * packets.
1765 */
1766 netfront_carrier_on(np);
1767 notify_remote_via_irq(np->irq);
1768 network_tx_buf_gc(dev);
1769 network_alloc_rx_buffers(dev);
1771 spin_unlock_irq(&np->tx_lock);
1772 spin_unlock_bh(&np->rx_lock);
1774 return 0;
1777 static void netif_uninit(struct net_device *dev)
1779 struct netfront_info *np = netdev_priv(dev);
1780 netif_release_tx_bufs(np);
1781 netif_release_rx_bufs(np);
1782 gnttab_free_grant_references(np->gref_tx_head);
1783 gnttab_free_grant_references(np->gref_rx_head);
1786 static struct ethtool_ops network_ethtool_ops =
1788 .get_tx_csum = ethtool_op_get_tx_csum,
1789 .set_tx_csum = ethtool_op_set_tx_csum,
1790 .get_sg = ethtool_op_get_sg,
1791 .set_sg = xennet_set_sg,
1792 .get_tso = ethtool_op_get_tso,
1793 .set_tso = xennet_set_tso,
1794 .get_link = ethtool_op_get_link,
1795 };
1797 #ifdef CONFIG_SYSFS
1798 static ssize_t show_rxbuf_min(struct class_device *cd, char *buf)
1800 struct net_device *netdev = container_of(cd, struct net_device,
1801 class_dev);
1802 struct netfront_info *info = netdev_priv(netdev);
1804 return sprintf(buf, "%u\n", info->rx_min_target);
1807 static ssize_t store_rxbuf_min(struct class_device *cd,
1808 const char *buf, size_t len)
1810 struct net_device *netdev = container_of(cd, struct net_device,
1811 class_dev);
1812 struct netfront_info *np = netdev_priv(netdev);
1813 char *endp;
1814 unsigned long target;
1816 if (!capable(CAP_NET_ADMIN))
1817 return -EPERM;
1819 target = simple_strtoul(buf, &endp, 0);
1820 if (endp == buf)
1821 return -EBADMSG;
1823 if (target < RX_MIN_TARGET)
1824 target = RX_MIN_TARGET;
1825 if (target > RX_MAX_TARGET)
1826 target = RX_MAX_TARGET;
1828 spin_lock_bh(&np->rx_lock);
1829 if (target > np->rx_max_target)
1830 np->rx_max_target = target;
1831 np->rx_min_target = target;
1832 if (target > np->rx_target)
1833 np->rx_target = target;
1835 network_alloc_rx_buffers(netdev);
1837 spin_unlock_bh(&np->rx_lock);
1838 return len;
1841 static ssize_t show_rxbuf_max(struct class_device *cd, char *buf)
1843 struct net_device *netdev = container_of(cd, struct net_device,
1844 class_dev);
1845 struct netfront_info *info = netdev_priv(netdev);
1847 return sprintf(buf, "%u\n", info->rx_max_target);
1850 static ssize_t store_rxbuf_max(struct class_device *cd,
1851 const char *buf, size_t len)
1853 struct net_device *netdev = container_of(cd, struct net_device,
1854 class_dev);
1855 struct netfront_info *np = netdev_priv(netdev);
1856 char *endp;
1857 unsigned long target;
1859 if (!capable(CAP_NET_ADMIN))
1860 return -EPERM;
1862 target = simple_strtoul(buf, &endp, 0);
1863 if (endp == buf)
1864 return -EBADMSG;
1866 if (target < RX_MIN_TARGET)
1867 target = RX_MIN_TARGET;
1868 if (target > RX_MAX_TARGET)
1869 target = RX_MAX_TARGET;
1871 spin_lock_bh(&np->rx_lock);
1872 if (target < np->rx_min_target)
1873 np->rx_min_target = target;
1874 np->rx_max_target = target;
1875 if (target < np->rx_target)
1876 np->rx_target = target;
1878 network_alloc_rx_buffers(netdev);
1880 spin_unlock_bh(&np->rx_lock);
1881 return len;
1884 static ssize_t show_rxbuf_cur(struct class_device *cd, char *buf)
1886 struct net_device *netdev = container_of(cd, struct net_device,
1887 class_dev);
1888 struct netfront_info *info = netdev_priv(netdev);
1890 return sprintf(buf, "%u\n", info->rx_target);
1893 static const struct class_device_attribute xennet_attrs[] = {
1894 __ATTR(rxbuf_min, S_IRUGO|S_IWUSR, show_rxbuf_min, store_rxbuf_min),
1895 __ATTR(rxbuf_max, S_IRUGO|S_IWUSR, show_rxbuf_max, store_rxbuf_max),
1896 __ATTR(rxbuf_cur, S_IRUGO, show_rxbuf_cur, NULL),
1897 };
1899 static int xennet_sysfs_addif(struct net_device *netdev)
1901 int i;
1902 int error = 0;
1904 for (i = 0; i < ARRAY_SIZE(xennet_attrs); i++) {
1905 error = class_device_create_file(&netdev->class_dev,
1906 &xennet_attrs[i]);
1907 if (error)
1908 goto fail;
1910 return 0;
1912 fail:
1913 while (--i >= 0)
1914 class_device_remove_file(&netdev->class_dev,
1915 &xennet_attrs[i]);
1916 return error;
1919 static void xennet_sysfs_delif(struct net_device *netdev)
1921 int i;
1923 for (i = 0; i < ARRAY_SIZE(xennet_attrs); i++) {
1924 class_device_remove_file(&netdev->class_dev,
1925 &xennet_attrs[i]);
1929 #endif /* CONFIG_SYSFS */
1932 /*
1933 * Nothing to do here. Virtual interface is point-to-point and the
1934 * physical interface is probably promiscuous anyway.
1935 */
1936 static void network_set_multicast_list(struct net_device *dev)
1940 static struct net_device * __devinit create_netdev(struct xenbus_device *dev)
1942 int i, err = 0;
1943 struct net_device *netdev = NULL;
1944 struct netfront_info *np = NULL;
1946 netdev = alloc_etherdev(sizeof(struct netfront_info));
1947 if (!netdev) {
1948 printk(KERN_WARNING "%s> alloc_etherdev failed.\n",
1949 __FUNCTION__);
1950 return ERR_PTR(-ENOMEM);
1953 np = netdev_priv(netdev);
1954 np->xbdev = dev;
1956 spin_lock_init(&np->tx_lock);
1957 spin_lock_init(&np->rx_lock);
1959 skb_queue_head_init(&np->rx_batch);
1960 np->rx_target = RX_DFL_MIN_TARGET;
1961 np->rx_min_target = RX_DFL_MIN_TARGET;
1962 np->rx_max_target = RX_MAX_TARGET;
1964 init_timer(&np->rx_refill_timer);
1965 np->rx_refill_timer.data = (unsigned long)netdev;
1966 np->rx_refill_timer.function = rx_refill_timeout;
1968 /* Initialise {tx,rx}_skbs as a free chain containing every entry. */
1969 for (i = 0; i <= NET_TX_RING_SIZE; i++) {
1970 np->tx_skbs[i] = (void *)((unsigned long) i+1);
1971 np->grant_tx_ref[i] = GRANT_INVALID_REF;
1974 for (i = 0; i < NET_RX_RING_SIZE; i++) {
1975 np->rx_skbs[i] = NULL;
1976 np->grant_rx_ref[i] = GRANT_INVALID_REF;
1979 /* A grant for every tx ring slot */
1980 if (gnttab_alloc_grant_references(TX_MAX_TARGET,
1981 &np->gref_tx_head) < 0) {
1982 printk(KERN_ALERT "#### netfront can't alloc tx grant refs\n");
1983 err = -ENOMEM;
1984 goto exit;
1986 /* A grant for every rx ring slot */
1987 if (gnttab_alloc_grant_references(RX_MAX_TARGET,
1988 &np->gref_rx_head) < 0) {
1989 printk(KERN_ALERT "#### netfront can't alloc rx grant refs\n");
1990 err = -ENOMEM;
1991 goto exit_free_tx;
1994 netdev->open = network_open;
1995 netdev->hard_start_xmit = network_start_xmit;
1996 netdev->stop = network_close;
1997 netdev->get_stats = network_get_stats;
1998 netdev->poll = netif_poll;
1999 netdev->set_multicast_list = network_set_multicast_list;
2000 netdev->uninit = netif_uninit;
2001 netdev->change_mtu = xennet_change_mtu;
2002 netdev->weight = 64;
2003 netdev->features = NETIF_F_IP_CSUM;
2005 SET_ETHTOOL_OPS(netdev, &network_ethtool_ops);
2006 SET_MODULE_OWNER(netdev);
2007 SET_NETDEV_DEV(netdev, &dev->dev);
2009 np->netdev = netdev;
2011 netfront_carrier_off(np);
2013 return netdev;
2015 exit_free_tx:
2016 gnttab_free_grant_references(np->gref_tx_head);
2017 exit:
2018 free_netdev(netdev);
2019 return ERR_PTR(err);
2022 /*
2023 * We use this notifier to send out a fake ARP reply to reset switches and
2024 * router ARP caches when an IP interface is brought up on a VIF.
2025 */
2026 static int
2027 inetdev_notify(struct notifier_block *this, unsigned long event, void *ptr)
2029 struct in_ifaddr *ifa = (struct in_ifaddr *)ptr;
2030 struct net_device *dev = ifa->ifa_dev->dev;
2032 /* UP event and is it one of our devices? */
2033 if (event == NETDEV_UP && dev->open == network_open)
2034 (void)send_fake_arp(dev);
2036 return NOTIFY_DONE;
2040 static void netif_disconnect_backend(struct netfront_info *info)
2042 /* Stop old i/f to prevent errors whilst we rebuild the state. */
2043 spin_lock_bh(&info->rx_lock);
2044 spin_lock_irq(&info->tx_lock);
2045 netfront_carrier_off(info);
2046 spin_unlock_irq(&info->tx_lock);
2047 spin_unlock_bh(&info->rx_lock);
2049 if (info->irq)
2050 unbind_from_irqhandler(info->irq, info->netdev);
2051 info->irq = 0;
2053 end_access(info->tx_ring_ref, info->tx.sring);
2054 end_access(info->rx_ring_ref, info->rx.sring);
2055 info->tx_ring_ref = GRANT_INVALID_REF;
2056 info->rx_ring_ref = GRANT_INVALID_REF;
2057 info->tx.sring = NULL;
2058 info->rx.sring = NULL;
2062 static void end_access(int ref, void *page)
2064 if (ref != GRANT_INVALID_REF)
2065 gnttab_end_foreign_access(ref, 0, (unsigned long)page);
2069 /* ** Driver registration ** */
2072 static struct xenbus_device_id netfront_ids[] = {
2073 { "vif" },
2074 { "" }
2075 };
2078 static struct xenbus_driver netfront = {
2079 .name = "vif",
2080 .owner = THIS_MODULE,
2081 .ids = netfront_ids,
2082 .probe = netfront_probe,
2083 .remove = __devexit_p(netfront_remove),
2084 .resume = netfront_resume,
2085 .otherend_changed = backend_changed,
2086 };
2089 static struct notifier_block notifier_inetdev = {
2090 .notifier_call = inetdev_notify,
2091 .next = NULL,
2092 .priority = 0
2093 };
2095 static int __init netif_init(void)
2097 if (!is_running_on_xen())
2098 return -ENODEV;
2100 #ifdef CONFIG_XEN
2101 if (MODPARM_rx_flip && MODPARM_rx_copy) {
2102 WPRINTK("Cannot specify both rx_copy and rx_flip.\n");
2103 return -EINVAL;
2106 if (!MODPARM_rx_flip && !MODPARM_rx_copy)
2107 MODPARM_rx_flip = 1; /* Default is to flip. */
2108 #endif
2110 if (is_initial_xendomain())
2111 return 0;
2113 IPRINTK("Initialising virtual ethernet driver.\n");
2115 (void)register_inetaddr_notifier(&notifier_inetdev);
2117 return xenbus_register_frontend(&netfront);
2119 module_init(netif_init);
2122 static void __exit netif_exit(void)
2124 if (is_initial_xendomain())
2125 return;
2127 unregister_inetaddr_notifier(&notifier_inetdev);
2129 return xenbus_unregister_driver(&netfront);
2131 module_exit(netif_exit);
2133 MODULE_LICENSE("Dual BSD/GPL");