ia64/xen-unstable

view linux-2.6-xen-sparse/drivers/xen/netfront/netfront.c @ 13026:79315be2c9b9

[NET] front: Fix crashes when xenstore watches fire multiple times.
Signed-off-by: Keir Fraser <keir@xensource.com>
author kfraser@localhost.localdomain
date Thu Dec 14 12:21:28 2006 +0000 (2006-12-14)
parents 2df2acc05721
children e99ba0c6c046
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/config.h>
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 #ifdef HAVE_XEN_PLATFORM_COMPAT_H
68 #include <xen/platform-compat.h>
69 #endif
71 /*
72 * Mutually-exclusive module options to select receive data path:
73 * rx_copy : Packets are copied by network backend into local memory
74 * rx_flip : Page containing packet data is transferred to our ownership
75 * For fully-virtualised guests there is no option - copying must be used.
76 * For paravirtualised guests, flipping is the default.
77 */
78 #ifdef CONFIG_XEN
79 static int MODPARM_rx_copy = 0;
80 module_param_named(rx_copy, MODPARM_rx_copy, bool, 0);
81 MODULE_PARM_DESC(rx_copy, "Copy packets from network card (rather than flip)");
82 static int MODPARM_rx_flip = 0;
83 module_param_named(rx_flip, MODPARM_rx_flip, bool, 0);
84 MODULE_PARM_DESC(rx_flip, "Flip packets from network card (rather than copy)");
85 #else
86 static const int MODPARM_rx_copy = 1;
87 static const int MODPARM_rx_flip = 0;
88 #endif
90 #define RX_COPY_THRESHOLD 256
92 /* If we don't have GSO, fake things up so that we never try to use it. */
93 #if defined(NETIF_F_GSO)
94 #define HAVE_GSO 1
95 #define HAVE_TSO 1 /* TSO is a subset of GSO */
96 static inline void dev_disable_gso_features(struct net_device *dev)
97 {
98 /* Turn off all GSO bits except ROBUST. */
99 dev->features &= (1 << NETIF_F_GSO_SHIFT) - 1;
100 dev->features |= NETIF_F_GSO_ROBUST;
101 }
102 #elif defined(NETIF_F_TSO)
103 #define HAVE_TSO 1
105 /* Some older kernels cannot cope with incorrect checksums,
106 * particularly in netfilter. I'm not sure there is 100% correlation
107 * with the presence of NETIF_F_TSO but it appears to be a good first
108 * approximiation.
109 */
110 #define HAVE_NO_CSUM_OFFLOAD 1
112 #define gso_size tso_size
113 #define gso_segs tso_segs
114 static inline void dev_disable_gso_features(struct net_device *dev)
115 {
116 /* Turn off all TSO bits. */
117 dev->features &= ~NETIF_F_TSO;
118 }
119 static inline int skb_is_gso(const struct sk_buff *skb)
120 {
121 return skb_shinfo(skb)->tso_size;
122 }
123 static inline int skb_gso_ok(struct sk_buff *skb, int features)
124 {
125 return (features & NETIF_F_TSO);
126 }
128 static inline int netif_needs_gso(struct net_device *dev, struct sk_buff *skb)
129 {
130 return skb_is_gso(skb) &&
131 (!skb_gso_ok(skb, dev->features) ||
132 unlikely(skb->ip_summed != CHECKSUM_HW));
133 }
134 #else
135 #define netif_needs_gso(dev, skb) 0
136 #define dev_disable_gso_features(dev) ((void)0)
137 #endif
139 #define GRANT_INVALID_REF 0
141 #define NET_TX_RING_SIZE __RING_SIZE((struct netif_tx_sring *)0, PAGE_SIZE)
142 #define NET_RX_RING_SIZE __RING_SIZE((struct netif_rx_sring *)0, PAGE_SIZE)
144 struct netfront_info {
145 struct list_head list;
146 struct net_device *netdev;
148 struct net_device_stats stats;
150 struct netif_tx_front_ring tx;
151 struct netif_rx_front_ring rx;
153 spinlock_t tx_lock;
154 spinlock_t rx_lock;
156 unsigned int evtchn, irq;
157 unsigned int copying_receiver;
159 /* Receive-ring batched refills. */
160 #define RX_MIN_TARGET 8
161 #define RX_DFL_MIN_TARGET 64
162 #define RX_MAX_TARGET min_t(int, NET_RX_RING_SIZE, 256)
163 unsigned rx_min_target, rx_max_target, rx_target;
164 struct sk_buff_head rx_batch;
166 struct timer_list rx_refill_timer;
168 /*
169 * {tx,rx}_skbs store outstanding skbuffs. The first entry in tx_skbs
170 * is an index into a chain of free entries.
171 */
172 struct sk_buff *tx_skbs[NET_TX_RING_SIZE+1];
173 struct sk_buff *rx_skbs[NET_RX_RING_SIZE];
175 #define TX_MAX_TARGET min_t(int, NET_RX_RING_SIZE, 256)
176 grant_ref_t gref_tx_head;
177 grant_ref_t grant_tx_ref[NET_TX_RING_SIZE + 1];
178 grant_ref_t gref_rx_head;
179 grant_ref_t grant_rx_ref[NET_RX_RING_SIZE];
181 struct xenbus_device *xbdev;
182 int tx_ring_ref;
183 int rx_ring_ref;
184 u8 mac[ETH_ALEN];
186 unsigned long rx_pfn_array[NET_RX_RING_SIZE];
187 struct multicall_entry rx_mcl[NET_RX_RING_SIZE+1];
188 struct mmu_update rx_mmu[NET_RX_RING_SIZE];
189 };
191 struct netfront_rx_info {
192 struct netif_rx_response rx;
193 struct netif_extra_info extras[XEN_NETIF_EXTRA_TYPE_MAX - 1];
194 };
196 /*
197 * Access macros for acquiring freeing slots in tx_skbs[].
198 */
200 static inline void add_id_to_freelist(struct sk_buff **list, unsigned short id)
201 {
202 list[id] = list[0];
203 list[0] = (void *)(unsigned long)id;
204 }
206 static inline unsigned short get_id_from_freelist(struct sk_buff **list)
207 {
208 unsigned int id = (unsigned int)(unsigned long)list[0];
209 list[0] = list[id];
210 return id;
211 }
213 static inline int xennet_rxidx(RING_IDX idx)
214 {
215 return idx & (NET_RX_RING_SIZE - 1);
216 }
218 static inline struct sk_buff *xennet_get_rx_skb(struct netfront_info *np,
219 RING_IDX ri)
220 {
221 int i = xennet_rxidx(ri);
222 struct sk_buff *skb = np->rx_skbs[i];
223 np->rx_skbs[i] = NULL;
224 return skb;
225 }
227 static inline grant_ref_t xennet_get_rx_ref(struct netfront_info *np,
228 RING_IDX ri)
229 {
230 int i = xennet_rxidx(ri);
231 grant_ref_t ref = np->grant_rx_ref[i];
232 np->grant_rx_ref[i] = GRANT_INVALID_REF;
233 return ref;
234 }
236 #define DPRINTK(fmt, args...) \
237 pr_debug("netfront (%s:%d) " fmt, \
238 __FUNCTION__, __LINE__, ##args)
239 #define IPRINTK(fmt, args...) \
240 printk(KERN_INFO "netfront: " fmt, ##args)
241 #define WPRINTK(fmt, args...) \
242 printk(KERN_WARNING "netfront: " fmt, ##args)
244 static int setup_device(struct xenbus_device *, struct netfront_info *);
245 static struct net_device *create_netdev(struct xenbus_device *);
247 static void netfront_closing(struct xenbus_device *);
249 static void end_access(int, void *);
250 static void netif_disconnect_backend(struct netfront_info *);
251 static int open_netdev(struct netfront_info *);
252 static void close_netdev(struct netfront_info *);
254 static int network_connect(struct net_device *);
255 static void network_tx_buf_gc(struct net_device *);
256 static void network_alloc_rx_buffers(struct net_device *);
257 static int send_fake_arp(struct net_device *);
259 static irqreturn_t netif_int(int irq, void *dev_id, struct pt_regs *ptregs);
261 #ifdef CONFIG_SYSFS
262 static int xennet_sysfs_addif(struct net_device *netdev);
263 static void xennet_sysfs_delif(struct net_device *netdev);
264 #else /* !CONFIG_SYSFS */
265 #define xennet_sysfs_addif(dev) (0)
266 #define xennet_sysfs_delif(dev) do { } while(0)
267 #endif
269 static inline int xennet_can_sg(struct net_device *dev)
270 {
271 return dev->features & NETIF_F_SG;
272 }
274 /**
275 * Entry point to this code when a new device is created. Allocate the basic
276 * structures and the ring buffers for communication with the backend, and
277 * inform the backend of the appropriate details for those.
278 */
279 static int __devinit netfront_probe(struct xenbus_device *dev,
280 const struct xenbus_device_id *id)
281 {
282 int err;
283 struct net_device *netdev;
284 struct netfront_info *info;
286 netdev = create_netdev(dev);
287 if (IS_ERR(netdev)) {
288 err = PTR_ERR(netdev);
289 xenbus_dev_fatal(dev, err, "creating netdev");
290 return err;
291 }
293 info = netdev_priv(netdev);
294 dev->dev.driver_data = info;
296 err = open_netdev(info);
297 if (err)
298 goto fail;
300 return 0;
302 fail:
303 free_netdev(netdev);
304 dev->dev.driver_data = NULL;
305 return err;
306 }
309 /**
310 * We are reconnecting to the backend, due to a suspend/resume, or a backend
311 * driver restart. We tear down our netif structure and recreate it, but
312 * leave the device-layer structures intact so that this is transparent to the
313 * rest of the kernel.
314 */
315 static int netfront_resume(struct xenbus_device *dev)
316 {
317 struct netfront_info *info = dev->dev.driver_data;
319 DPRINTK("%s\n", dev->nodename);
321 netif_disconnect_backend(info);
322 return 0;
323 }
325 static int xen_net_read_mac(struct xenbus_device *dev, u8 mac[])
326 {
327 char *s, *e, *macstr;
328 int i;
330 macstr = s = xenbus_read(XBT_NIL, dev->nodename, "mac", NULL);
331 if (IS_ERR(macstr))
332 return PTR_ERR(macstr);
334 for (i = 0; i < ETH_ALEN; i++) {
335 mac[i] = simple_strtoul(s, &e, 16);
336 if ((s == e) || (*e != ((i == ETH_ALEN-1) ? '\0' : ':'))) {
337 kfree(macstr);
338 return -ENOENT;
339 }
340 s = e+1;
341 }
343 kfree(macstr);
344 return 0;
345 }
347 /* Common code used when first setting up, and when resuming. */
348 static int talk_to_backend(struct xenbus_device *dev,
349 struct netfront_info *info)
350 {
351 const char *message;
352 struct xenbus_transaction xbt;
353 int err;
355 err = xen_net_read_mac(dev, info->mac);
356 if (err) {
357 xenbus_dev_fatal(dev, err, "parsing %s/mac", dev->nodename);
358 goto out;
359 }
361 /* Create shared ring, alloc event channel. */
362 err = setup_device(dev, info);
363 if (err)
364 goto out;
366 again:
367 err = xenbus_transaction_start(&xbt);
368 if (err) {
369 xenbus_dev_fatal(dev, err, "starting transaction");
370 goto destroy_ring;
371 }
373 err = xenbus_printf(xbt, dev->nodename, "tx-ring-ref","%u",
374 info->tx_ring_ref);
375 if (err) {
376 message = "writing tx ring-ref";
377 goto abort_transaction;
378 }
379 err = xenbus_printf(xbt, dev->nodename, "rx-ring-ref","%u",
380 info->rx_ring_ref);
381 if (err) {
382 message = "writing rx ring-ref";
383 goto abort_transaction;
384 }
385 err = xenbus_printf(xbt, dev->nodename,
386 "event-channel", "%u", info->evtchn);
387 if (err) {
388 message = "writing event-channel";
389 goto abort_transaction;
390 }
392 err = xenbus_printf(xbt, dev->nodename, "request-rx-copy", "%u",
393 info->copying_receiver);
394 if (err) {
395 message = "writing request-rx-copy";
396 goto abort_transaction;
397 }
399 err = xenbus_printf(xbt, dev->nodename, "feature-rx-notify", "%d", 1);
400 if (err) {
401 message = "writing feature-rx-notify";
402 goto abort_transaction;
403 }
405 #ifdef HAVE_NO_CSUM_OFFLOAD
406 err = xenbus_printf(xbt, dev->nodename, "feature-no-csum-offload", "%d", 1);
407 if (err) {
408 message = "writing feature-no-csum-offload";
409 goto abort_transaction;
410 }
411 #endif
413 err = xenbus_printf(xbt, dev->nodename, "feature-sg", "%d", 1);
414 if (err) {
415 message = "writing feature-sg";
416 goto abort_transaction;
417 }
419 #ifdef HAVE_TSO
420 err = xenbus_printf(xbt, dev->nodename, "feature-gso-tcpv4", "%d", 1);
421 if (err) {
422 message = "writing feature-gso-tcpv4";
423 goto abort_transaction;
424 }
425 #endif
427 err = xenbus_transaction_end(xbt, 0);
428 if (err) {
429 if (err == -EAGAIN)
430 goto again;
431 xenbus_dev_fatal(dev, err, "completing transaction");
432 goto destroy_ring;
433 }
435 return 0;
437 abort_transaction:
438 xenbus_transaction_end(xbt, 1);
439 xenbus_dev_fatal(dev, err, "%s", message);
440 destroy_ring:
441 netif_disconnect_backend(info);
442 out:
443 return err;
444 }
446 static int setup_device(struct xenbus_device *dev, struct netfront_info *info)
447 {
448 struct netif_tx_sring *txs;
449 struct netif_rx_sring *rxs;
450 int err;
451 struct net_device *netdev = info->netdev;
453 info->tx_ring_ref = GRANT_INVALID_REF;
454 info->rx_ring_ref = GRANT_INVALID_REF;
455 info->rx.sring = NULL;
456 info->tx.sring = NULL;
457 info->irq = 0;
459 txs = (struct netif_tx_sring *)get_zeroed_page(GFP_KERNEL);
460 if (!txs) {
461 err = -ENOMEM;
462 xenbus_dev_fatal(dev, err, "allocating tx ring page");
463 goto fail;
464 }
465 SHARED_RING_INIT(txs);
466 FRONT_RING_INIT(&info->tx, txs, PAGE_SIZE);
468 err = xenbus_grant_ring(dev, virt_to_mfn(txs));
469 if (err < 0) {
470 free_page((unsigned long)txs);
471 goto fail;
472 }
473 info->tx_ring_ref = err;
475 rxs = (struct netif_rx_sring *)get_zeroed_page(GFP_KERNEL);
476 if (!rxs) {
477 err = -ENOMEM;
478 xenbus_dev_fatal(dev, err, "allocating rx ring page");
479 goto fail;
480 }
481 SHARED_RING_INIT(rxs);
482 FRONT_RING_INIT(&info->rx, rxs, PAGE_SIZE);
484 err = xenbus_grant_ring(dev, virt_to_mfn(rxs));
485 if (err < 0) {
486 free_page((unsigned long)rxs);
487 goto fail;
488 }
489 info->rx_ring_ref = err;
491 err = xenbus_alloc_evtchn(dev, &info->evtchn);
492 if (err)
493 goto fail;
495 memcpy(netdev->dev_addr, info->mac, ETH_ALEN);
496 err = bind_evtchn_to_irqhandler(info->evtchn, netif_int,
497 SA_SAMPLE_RANDOM, netdev->name,
498 netdev);
499 if (err < 0)
500 goto fail;
501 info->irq = err;
502 return 0;
504 fail:
505 return err;
506 }
508 /**
509 * Callback received when the backend's state changes.
510 */
511 static void backend_changed(struct xenbus_device *dev,
512 enum xenbus_state backend_state)
513 {
514 struct netfront_info *np = dev->dev.driver_data;
515 struct net_device *netdev = np->netdev;
517 DPRINTK("%s\n", xenbus_strstate(backend_state));
519 switch (backend_state) {
520 case XenbusStateInitialising:
521 case XenbusStateInitialised:
522 case XenbusStateConnected:
523 case XenbusStateUnknown:
524 case XenbusStateClosed:
525 break;
527 case XenbusStateInitWait:
528 if (dev->state != XenbusStateInitialising)
529 break;
530 if (network_connect(netdev) != 0)
531 break;
532 xenbus_switch_state(dev, XenbusStateConnected);
533 (void)send_fake_arp(netdev);
534 break;
536 case XenbusStateClosing:
537 if (dev->state == XenbusStateClosed)
538 break;
539 netfront_closing(dev);
540 break;
541 }
542 }
544 /** Send a packet on a net device to encourage switches to learn the
545 * MAC. We send a fake ARP request.
546 *
547 * @param dev device
548 * @return 0 on success, error code otherwise
549 */
550 static int send_fake_arp(struct net_device *dev)
551 {
552 struct sk_buff *skb;
553 u32 src_ip, dst_ip;
555 dst_ip = INADDR_BROADCAST;
556 src_ip = inet_select_addr(dev, dst_ip, RT_SCOPE_LINK);
558 /* No IP? Then nothing to do. */
559 if (src_ip == 0)
560 return 0;
562 skb = arp_create(ARPOP_REPLY, ETH_P_ARP,
563 dst_ip, dev, src_ip,
564 /*dst_hw*/ NULL, /*src_hw*/ NULL,
565 /*target_hw*/ dev->dev_addr);
566 if (skb == NULL)
567 return -ENOMEM;
569 return dev_queue_xmit(skb);
570 }
572 static int network_open(struct net_device *dev)
573 {
574 struct netfront_info *np = netdev_priv(dev);
576 memset(&np->stats, 0, sizeof(np->stats));
578 spin_lock(&np->rx_lock);
579 if (netif_carrier_ok(dev)) {
580 network_alloc_rx_buffers(dev);
581 np->rx.sring->rsp_event = np->rx.rsp_cons + 1;
582 if (RING_HAS_UNCONSUMED_RESPONSES(&np->rx))
583 netif_rx_schedule(dev);
584 }
585 spin_unlock(&np->rx_lock);
587 netif_start_queue(dev);
589 return 0;
590 }
592 static inline int netfront_tx_slot_available(struct netfront_info *np)
593 {
594 return RING_FREE_REQUESTS(&np->tx) >= MAX_SKB_FRAGS + 2;
595 }
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 netif_wake_queue(dev);
605 }
607 static void network_tx_buf_gc(struct net_device *dev)
608 {
609 RING_IDX cons, prod;
610 unsigned short id;
611 struct netfront_info *np = netdev_priv(dev);
612 struct sk_buff *skb;
614 BUG_ON(!netif_carrier_ok(dev));
616 do {
617 prod = np->tx.sring->rsp_prod;
618 rmb(); /* Ensure we see responses up to 'rp'. */
620 for (cons = np->tx.rsp_cons; cons != prod; cons++) {
621 struct netif_tx_response *txrsp;
623 txrsp = RING_GET_RESPONSE(&np->tx, cons);
624 if (txrsp->status == NETIF_RSP_NULL)
625 continue;
627 id = txrsp->id;
628 skb = np->tx_skbs[id];
629 if (unlikely(gnttab_query_foreign_access(
630 np->grant_tx_ref[id]) != 0)) {
631 printk(KERN_ALERT "network_tx_buf_gc: warning "
632 "-- grant still in use by backend "
633 "domain.\n");
634 BUG();
635 }
636 gnttab_end_foreign_access_ref(
637 np->grant_tx_ref[id], GNTMAP_readonly);
638 gnttab_release_grant_reference(
639 &np->gref_tx_head, np->grant_tx_ref[id]);
640 np->grant_tx_ref[id] = GRANT_INVALID_REF;
641 add_id_to_freelist(np->tx_skbs, id);
642 dev_kfree_skb_irq(skb);
643 }
645 np->tx.rsp_cons = prod;
647 /*
648 * Set a new event, then check for race with update of tx_cons.
649 * Note that it is essential to schedule a callback, no matter
650 * how few buffers are pending. Even if there is space in the
651 * transmit ring, higher layers may be blocked because too much
652 * data is outstanding: in such cases notification from Xen is
653 * likely to be the only kick that we'll get.
654 */
655 np->tx.sring->rsp_event =
656 prod + ((np->tx.sring->req_prod - prod) >> 1) + 1;
657 mb();
658 } while ((cons == prod) && (prod != np->tx.sring->rsp_prod));
660 network_maybe_wake_tx(dev);
661 }
663 static void rx_refill_timeout(unsigned long data)
664 {
665 struct net_device *dev = (struct net_device *)data;
666 netif_rx_schedule(dev);
667 }
669 static void network_alloc_rx_buffers(struct net_device *dev)
670 {
671 unsigned short id;
672 struct netfront_info *np = netdev_priv(dev);
673 struct sk_buff *skb;
674 struct page *page;
675 int i, batch_target, notify;
676 RING_IDX req_prod = np->rx.req_prod_pvt;
677 struct xen_memory_reservation reservation;
678 grant_ref_t ref;
679 unsigned long pfn;
680 void *vaddr;
681 int nr_flips;
682 netif_rx_request_t *req;
684 if (unlikely(!netif_carrier_ok(dev)))
685 return;
687 /*
688 * Allocate skbuffs greedily, even though we batch updates to the
689 * receive ring. This creates a less bursty demand on the memory
690 * allocator, so should reduce the chance of failed allocation requests
691 * both for ourself and for other kernel subsystems.
692 */
693 batch_target = np->rx_target - (req_prod - np->rx.rsp_cons);
694 for (i = skb_queue_len(&np->rx_batch); i < batch_target; i++) {
695 /*
696 * Allocate an skb and a page. Do not use __dev_alloc_skb as
697 * that will allocate page-sized buffers which is not
698 * necessary here.
699 * 16 bytes added as necessary headroom for netif_receive_skb.
700 */
701 skb = alloc_skb(RX_COPY_THRESHOLD + 16 + NET_IP_ALIGN,
702 GFP_ATOMIC | __GFP_NOWARN);
703 if (unlikely(!skb))
704 goto no_skb;
706 page = alloc_page(GFP_ATOMIC | __GFP_NOWARN);
707 if (!page) {
708 kfree_skb(skb);
709 no_skb:
710 /* Any skbuffs queued for refill? Force them out. */
711 if (i != 0)
712 goto refill;
713 /* Could not allocate any skbuffs. Try again later. */
714 mod_timer(&np->rx_refill_timer,
715 jiffies + (HZ/10));
716 break;
717 }
719 skb_reserve(skb, 16 + NET_IP_ALIGN); /* mimic dev_alloc_skb() */
720 skb_shinfo(skb)->frags[0].page = page;
721 skb_shinfo(skb)->nr_frags = 1;
722 __skb_queue_tail(&np->rx_batch, skb);
723 }
725 /* Is the batch large enough to be worthwhile? */
726 if (i < (np->rx_target/2)) {
727 if (req_prod > np->rx.sring->req_prod)
728 goto push;
729 return;
730 }
732 /* Adjust our fill target if we risked running out of buffers. */
733 if (((req_prod - np->rx.sring->rsp_prod) < (np->rx_target / 4)) &&
734 ((np->rx_target *= 2) > np->rx_max_target))
735 np->rx_target = np->rx_max_target;
737 refill:
738 for (nr_flips = i = 0; ; i++) {
739 if ((skb = __skb_dequeue(&np->rx_batch)) == NULL)
740 break;
742 skb->dev = dev;
744 id = xennet_rxidx(req_prod + i);
746 BUG_ON(np->rx_skbs[id]);
747 np->rx_skbs[id] = skb;
749 ref = gnttab_claim_grant_reference(&np->gref_rx_head);
750 BUG_ON((signed short)ref < 0);
751 np->grant_rx_ref[id] = ref;
753 pfn = page_to_pfn(skb_shinfo(skb)->frags[0].page);
754 vaddr = page_address(skb_shinfo(skb)->frags[0].page);
756 req = RING_GET_REQUEST(&np->rx, req_prod + i);
757 if (!np->copying_receiver) {
758 gnttab_grant_foreign_transfer_ref(ref,
759 np->xbdev->otherend_id,
760 pfn);
761 np->rx_pfn_array[nr_flips] = pfn_to_mfn(pfn);
762 if (!xen_feature(XENFEAT_auto_translated_physmap)) {
763 /* Remove this page before passing
764 * back to Xen. */
765 set_phys_to_machine(pfn, INVALID_P2M_ENTRY);
766 MULTI_update_va_mapping(np->rx_mcl+i,
767 (unsigned long)vaddr,
768 __pte(0), 0);
769 }
770 nr_flips++;
771 } else {
772 gnttab_grant_foreign_access_ref(ref,
773 np->xbdev->otherend_id,
774 pfn_to_mfn(pfn),
775 0);
776 }
778 req->id = id;
779 req->gref = ref;
780 }
782 if ( nr_flips != 0 ) {
783 /* Tell the ballon driver what is going on. */
784 balloon_update_driver_allowance(i);
786 set_xen_guest_handle(reservation.extent_start,
787 np->rx_pfn_array);
788 reservation.nr_extents = nr_flips;
789 reservation.extent_order = 0;
790 reservation.address_bits = 0;
791 reservation.domid = DOMID_SELF;
793 if (!xen_feature(XENFEAT_auto_translated_physmap)) {
794 /* After all PTEs have been zapped, flush the TLB. */
795 np->rx_mcl[i-1].args[MULTI_UVMFLAGS_INDEX] =
796 UVMF_TLB_FLUSH|UVMF_ALL;
798 /* Give away a batch of pages. */
799 np->rx_mcl[i].op = __HYPERVISOR_memory_op;
800 np->rx_mcl[i].args[0] = XENMEM_decrease_reservation;
801 np->rx_mcl[i].args[1] = (unsigned long)&reservation;
803 /* Zap PTEs and give away pages in one big
804 * multicall. */
805 (void)HYPERVISOR_multicall(np->rx_mcl, i+1);
807 /* Check return status of HYPERVISOR_memory_op(). */
808 if (unlikely(np->rx_mcl[i].result != i))
809 panic("Unable to reduce memory reservation\n");
810 } else {
811 if (HYPERVISOR_memory_op(XENMEM_decrease_reservation,
812 &reservation) != i)
813 panic("Unable to reduce memory reservation\n");
814 }
815 } else {
816 wmb();
817 }
819 /* Above is a suitable barrier to ensure backend will see requests. */
820 np->rx.req_prod_pvt = req_prod + i;
821 push:
822 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&np->rx, notify);
823 if (notify)
824 notify_remote_via_irq(np->irq);
825 }
827 static void xennet_make_frags(struct sk_buff *skb, struct net_device *dev,
828 struct netif_tx_request *tx)
829 {
830 struct netfront_info *np = netdev_priv(dev);
831 char *data = skb->data;
832 unsigned long mfn;
833 RING_IDX prod = np->tx.req_prod_pvt;
834 int frags = skb_shinfo(skb)->nr_frags;
835 unsigned int offset = offset_in_page(data);
836 unsigned int len = skb_headlen(skb);
837 unsigned int id;
838 grant_ref_t ref;
839 int i;
841 while (len > PAGE_SIZE - offset) {
842 tx->size = PAGE_SIZE - offset;
843 tx->flags |= NETTXF_more_data;
844 len -= tx->size;
845 data += tx->size;
846 offset = 0;
848 id = get_id_from_freelist(np->tx_skbs);
849 np->tx_skbs[id] = skb_get(skb);
850 tx = RING_GET_REQUEST(&np->tx, prod++);
851 tx->id = id;
852 ref = gnttab_claim_grant_reference(&np->gref_tx_head);
853 BUG_ON((signed short)ref < 0);
855 mfn = virt_to_mfn(data);
856 gnttab_grant_foreign_access_ref(ref, np->xbdev->otherend_id,
857 mfn, GNTMAP_readonly);
859 tx->gref = np->grant_tx_ref[id] = ref;
860 tx->offset = offset;
861 tx->size = len;
862 tx->flags = 0;
863 }
865 for (i = 0; i < frags; i++) {
866 skb_frag_t *frag = skb_shinfo(skb)->frags + i;
868 tx->flags |= NETTXF_more_data;
870 id = get_id_from_freelist(np->tx_skbs);
871 np->tx_skbs[id] = skb_get(skb);
872 tx = RING_GET_REQUEST(&np->tx, prod++);
873 tx->id = id;
874 ref = gnttab_claim_grant_reference(&np->gref_tx_head);
875 BUG_ON((signed short)ref < 0);
877 mfn = pfn_to_mfn(page_to_pfn(frag->page));
878 gnttab_grant_foreign_access_ref(ref, np->xbdev->otherend_id,
879 mfn, GNTMAP_readonly);
881 tx->gref = np->grant_tx_ref[id] = ref;
882 tx->offset = frag->page_offset;
883 tx->size = frag->size;
884 tx->flags = 0;
885 }
887 np->tx.req_prod_pvt = prod;
888 }
890 static int network_start_xmit(struct sk_buff *skb, struct net_device *dev)
891 {
892 unsigned short id;
893 struct netfront_info *np = netdev_priv(dev);
894 struct netif_tx_request *tx;
895 struct netif_extra_info *extra;
896 char *data = skb->data;
897 RING_IDX i;
898 grant_ref_t ref;
899 unsigned long mfn;
900 int notify;
901 int frags = skb_shinfo(skb)->nr_frags;
902 unsigned int offset = offset_in_page(data);
903 unsigned int len = skb_headlen(skb);
905 frags += (offset + len + PAGE_SIZE - 1) / PAGE_SIZE;
906 if (unlikely(frags > MAX_SKB_FRAGS + 1)) {
907 printk(KERN_ALERT "xennet: skb rides the rocket: %d frags\n",
908 frags);
909 dump_stack();
910 goto drop;
911 }
913 spin_lock_irq(&np->tx_lock);
915 if (unlikely(!netif_carrier_ok(dev) ||
916 (frags > 1 && !xennet_can_sg(dev)) ||
917 netif_needs_gso(dev, skb))) {
918 spin_unlock_irq(&np->tx_lock);
919 goto drop;
920 }
922 i = np->tx.req_prod_pvt;
924 id = get_id_from_freelist(np->tx_skbs);
925 np->tx_skbs[id] = skb;
927 tx = RING_GET_REQUEST(&np->tx, i);
929 tx->id = id;
930 ref = gnttab_claim_grant_reference(&np->gref_tx_head);
931 BUG_ON((signed short)ref < 0);
932 mfn = virt_to_mfn(data);
933 gnttab_grant_foreign_access_ref(
934 ref, np->xbdev->otherend_id, mfn, GNTMAP_readonly);
935 tx->gref = np->grant_tx_ref[id] = ref;
936 tx->offset = offset;
937 tx->size = len;
939 tx->flags = 0;
940 extra = NULL;
942 if (skb->ip_summed == CHECKSUM_HW) /* local packet? */
943 tx->flags |= NETTXF_csum_blank | NETTXF_data_validated;
944 #ifdef CONFIG_XEN
945 if (skb->proto_data_valid) /* remote but checksummed? */
946 tx->flags |= NETTXF_data_validated;
947 #endif
949 #ifdef HAVE_TSO
950 if (skb_shinfo(skb)->gso_size) {
951 struct netif_extra_info *gso = (struct netif_extra_info *)
952 RING_GET_REQUEST(&np->tx, ++i);
954 if (extra)
955 extra->flags |= XEN_NETIF_EXTRA_FLAG_MORE;
956 else
957 tx->flags |= NETTXF_extra_info;
959 gso->u.gso.size = skb_shinfo(skb)->gso_size;
960 gso->u.gso.type = XEN_NETIF_GSO_TYPE_TCPV4;
961 gso->u.gso.pad = 0;
962 gso->u.gso.features = 0;
964 gso->type = XEN_NETIF_EXTRA_TYPE_GSO;
965 gso->flags = 0;
966 extra = gso;
967 }
968 #endif
970 np->tx.req_prod_pvt = i + 1;
972 xennet_make_frags(skb, dev, tx);
973 tx->size = skb->len;
975 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&np->tx, notify);
976 if (notify)
977 notify_remote_via_irq(np->irq);
979 network_tx_buf_gc(dev);
981 if (!netfront_tx_slot_available(np))
982 netif_stop_queue(dev);
984 spin_unlock_irq(&np->tx_lock);
986 np->stats.tx_bytes += skb->len;
987 np->stats.tx_packets++;
989 return 0;
991 drop:
992 np->stats.tx_dropped++;
993 dev_kfree_skb(skb);
994 return 0;
995 }
997 static irqreturn_t netif_int(int irq, void *dev_id, struct pt_regs *ptregs)
998 {
999 struct net_device *dev = dev_id;
1000 struct netfront_info *np = netdev_priv(dev);
1001 unsigned long flags;
1003 spin_lock_irqsave(&np->tx_lock, flags);
1005 if (likely(netif_carrier_ok(dev))) {
1006 network_tx_buf_gc(dev);
1007 /* Under tx_lock: protects access to rx shared-ring indexes. */
1008 if (RING_HAS_UNCONSUMED_RESPONSES(&np->rx))
1009 netif_rx_schedule(dev);
1012 spin_unlock_irqrestore(&np->tx_lock, flags);
1014 return IRQ_HANDLED;
1017 static void xennet_move_rx_slot(struct netfront_info *np, struct sk_buff *skb,
1018 grant_ref_t ref)
1020 int new = xennet_rxidx(np->rx.req_prod_pvt);
1022 BUG_ON(np->rx_skbs[new]);
1023 np->rx_skbs[new] = skb;
1024 np->grant_rx_ref[new] = ref;
1025 RING_GET_REQUEST(&np->rx, np->rx.req_prod_pvt)->id = new;
1026 RING_GET_REQUEST(&np->rx, np->rx.req_prod_pvt)->gref = ref;
1027 np->rx.req_prod_pvt++;
1030 int xennet_get_extras(struct netfront_info *np,
1031 struct netif_extra_info *extras, RING_IDX rp)
1034 struct netif_extra_info *extra;
1035 RING_IDX cons = np->rx.rsp_cons;
1036 int err = 0;
1038 do {
1039 struct sk_buff *skb;
1040 grant_ref_t ref;
1042 if (unlikely(cons + 1 == rp)) {
1043 if (net_ratelimit())
1044 WPRINTK("Missing extra info\n");
1045 err = -EBADR;
1046 break;
1049 extra = (struct netif_extra_info *)
1050 RING_GET_RESPONSE(&np->rx, ++cons);
1052 if (unlikely(!extra->type ||
1053 extra->type >= XEN_NETIF_EXTRA_TYPE_MAX)) {
1054 if (net_ratelimit())
1055 WPRINTK("Invalid extra type: %d\n",
1056 extra->type);
1057 err = -EINVAL;
1058 } else {
1059 memcpy(&extras[extra->type - 1], extra,
1060 sizeof(*extra));
1063 skb = xennet_get_rx_skb(np, cons);
1064 ref = xennet_get_rx_ref(np, cons);
1065 xennet_move_rx_slot(np, skb, ref);
1066 } while (extra->flags & XEN_NETIF_EXTRA_FLAG_MORE);
1068 np->rx.rsp_cons = cons;
1069 return err;
1072 static int xennet_get_responses(struct netfront_info *np,
1073 struct netfront_rx_info *rinfo, RING_IDX rp,
1074 struct sk_buff_head *list,
1075 int *pages_flipped_p)
1077 int pages_flipped = *pages_flipped_p;
1078 struct mmu_update *mmu;
1079 struct multicall_entry *mcl;
1080 struct netif_rx_response *rx = &rinfo->rx;
1081 struct netif_extra_info *extras = rinfo->extras;
1082 RING_IDX cons = np->rx.rsp_cons;
1083 struct sk_buff *skb = xennet_get_rx_skb(np, cons);
1084 grant_ref_t ref = xennet_get_rx_ref(np, cons);
1085 int max = MAX_SKB_FRAGS + (rx->status <= RX_COPY_THRESHOLD);
1086 int frags = 1;
1087 int err = 0;
1088 unsigned long ret;
1090 if (rx->flags & NETRXF_extra_info) {
1091 err = xennet_get_extras(np, extras, rp);
1092 cons = np->rx.rsp_cons;
1095 for (;;) {
1096 unsigned long mfn;
1098 if (unlikely(rx->status < 0 ||
1099 rx->offset + rx->status > PAGE_SIZE)) {
1100 if (net_ratelimit())
1101 WPRINTK("rx->offset: %x, size: %u\n",
1102 rx->offset, rx->status);
1103 xennet_move_rx_slot(np, skb, ref);
1104 err = -EINVAL;
1105 goto next;
1108 /*
1109 * This definitely indicates a bug, either in this driver or in
1110 * the backend driver. In future this should flag the bad
1111 * situation to the system controller to reboot the backed.
1112 */
1113 if (ref == GRANT_INVALID_REF) {
1114 if (net_ratelimit())
1115 WPRINTK("Bad rx response id %d.\n", rx->id);
1116 err = -EINVAL;
1117 goto next;
1120 if (!np->copying_receiver) {
1121 /* Memory pressure, insufficient buffer
1122 * headroom, ... */
1123 if (!(mfn = gnttab_end_foreign_transfer_ref(ref))) {
1124 if (net_ratelimit())
1125 WPRINTK("Unfulfilled rx req "
1126 "(id=%d, st=%d).\n",
1127 rx->id, rx->status);
1128 xennet_move_rx_slot(np, skb, ref);
1129 err = -ENOMEM;
1130 goto next;
1133 if (!xen_feature(XENFEAT_auto_translated_physmap)) {
1134 /* Remap the page. */
1135 struct page *page =
1136 skb_shinfo(skb)->frags[0].page;
1137 unsigned long pfn = page_to_pfn(page);
1138 void *vaddr = page_address(page);
1140 mcl = np->rx_mcl + pages_flipped;
1141 mmu = np->rx_mmu + pages_flipped;
1143 MULTI_update_va_mapping(mcl,
1144 (unsigned long)vaddr,
1145 pfn_pte_ma(mfn,
1146 PAGE_KERNEL),
1147 0);
1148 mmu->ptr = ((maddr_t)mfn << PAGE_SHIFT)
1149 | MMU_MACHPHYS_UPDATE;
1150 mmu->val = pfn;
1152 set_phys_to_machine(pfn, mfn);
1154 pages_flipped++;
1155 } else {
1156 ret = gnttab_end_foreign_access_ref(ref, 0);
1157 BUG_ON(!ret);
1160 gnttab_release_grant_reference(&np->gref_rx_head, ref);
1162 __skb_queue_tail(list, skb);
1164 next:
1165 if (!(rx->flags & NETRXF_more_data))
1166 break;
1168 if (cons + frags == rp) {
1169 if (net_ratelimit())
1170 WPRINTK("Need more frags\n");
1171 err = -ENOENT;
1172 break;
1175 rx = RING_GET_RESPONSE(&np->rx, cons + frags);
1176 skb = xennet_get_rx_skb(np, cons + frags);
1177 ref = xennet_get_rx_ref(np, cons + frags);
1178 frags++;
1181 if (unlikely(frags > max)) {
1182 if (net_ratelimit())
1183 WPRINTK("Too many frags\n");
1184 err = -E2BIG;
1187 if (unlikely(err))
1188 np->rx.rsp_cons = cons + frags;
1190 *pages_flipped_p = pages_flipped;
1192 return err;
1195 static RING_IDX xennet_fill_frags(struct netfront_info *np,
1196 struct sk_buff *skb,
1197 struct sk_buff_head *list)
1199 struct skb_shared_info *shinfo = skb_shinfo(skb);
1200 int nr_frags = shinfo->nr_frags;
1201 RING_IDX cons = np->rx.rsp_cons;
1202 skb_frag_t *frag = shinfo->frags + nr_frags;
1203 struct sk_buff *nskb;
1205 while ((nskb = __skb_dequeue(list))) {
1206 struct netif_rx_response *rx =
1207 RING_GET_RESPONSE(&np->rx, ++cons);
1209 frag->page = skb_shinfo(nskb)->frags[0].page;
1210 frag->page_offset = rx->offset;
1211 frag->size = rx->status;
1213 skb->data_len += rx->status;
1215 skb_shinfo(nskb)->nr_frags = 0;
1216 kfree_skb(nskb);
1218 frag++;
1219 nr_frags++;
1222 shinfo->nr_frags = nr_frags;
1223 return cons;
1226 static int xennet_set_skb_gso(struct sk_buff *skb,
1227 struct netif_extra_info *gso)
1229 if (!gso->u.gso.size) {
1230 if (net_ratelimit())
1231 WPRINTK("GSO size must not be zero.\n");
1232 return -EINVAL;
1235 /* Currently only TCPv4 S.O. is supported. */
1236 if (gso->u.gso.type != XEN_NETIF_GSO_TYPE_TCPV4) {
1237 if (net_ratelimit())
1238 WPRINTK("Bad GSO type %d.\n", gso->u.gso.type);
1239 return -EINVAL;
1242 #ifdef HAVE_TSO
1243 skb_shinfo(skb)->gso_size = gso->u.gso.size;
1244 #ifdef HAVE_GSO
1245 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
1247 /* Header must be checked, and gso_segs computed. */
1248 skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
1249 #endif
1250 skb_shinfo(skb)->gso_segs = 0;
1252 return 0;
1253 #else
1254 if (net_ratelimit())
1255 WPRINTK("GSO unsupported by this kernel.\n");
1256 return -EINVAL;
1257 #endif
1260 static int netif_poll(struct net_device *dev, int *pbudget)
1262 struct netfront_info *np = netdev_priv(dev);
1263 struct sk_buff *skb;
1264 struct netfront_rx_info rinfo;
1265 struct netif_rx_response *rx = &rinfo.rx;
1266 struct netif_extra_info *extras = rinfo.extras;
1267 RING_IDX i, rp;
1268 struct multicall_entry *mcl;
1269 int work_done, budget, more_to_do = 1;
1270 struct sk_buff_head rxq;
1271 struct sk_buff_head errq;
1272 struct sk_buff_head tmpq;
1273 unsigned long flags;
1274 unsigned int len;
1275 int pages_flipped = 0;
1276 int err;
1278 spin_lock(&np->rx_lock);
1280 if (unlikely(!netif_carrier_ok(dev))) {
1281 spin_unlock(&np->rx_lock);
1282 return 0;
1285 skb_queue_head_init(&rxq);
1286 skb_queue_head_init(&errq);
1287 skb_queue_head_init(&tmpq);
1289 if ((budget = *pbudget) > dev->quota)
1290 budget = dev->quota;
1291 rp = np->rx.sring->rsp_prod;
1292 rmb(); /* Ensure we see queued responses up to 'rp'. */
1294 i = np->rx.rsp_cons;
1295 work_done = 0;
1296 while ((i != rp) && (work_done < budget)) {
1297 memcpy(rx, RING_GET_RESPONSE(&np->rx, i), sizeof(*rx));
1298 memset(extras, 0, sizeof(extras));
1300 err = xennet_get_responses(np, &rinfo, rp, &tmpq,
1301 &pages_flipped);
1303 if (unlikely(err)) {
1304 err:
1305 while ((skb = __skb_dequeue(&tmpq)))
1306 __skb_queue_tail(&errq, skb);
1307 np->stats.rx_errors++;
1308 i = np->rx.rsp_cons;
1309 continue;
1312 skb = __skb_dequeue(&tmpq);
1314 if (extras[XEN_NETIF_EXTRA_TYPE_GSO - 1].type) {
1315 struct netif_extra_info *gso;
1316 gso = &extras[XEN_NETIF_EXTRA_TYPE_GSO - 1];
1318 if (unlikely(xennet_set_skb_gso(skb, gso))) {
1319 __skb_queue_head(&tmpq, skb);
1320 np->rx.rsp_cons += skb_queue_len(&tmpq);
1321 goto err;
1325 skb->nh.raw = (void *)skb_shinfo(skb)->frags[0].page;
1326 skb->h.raw = skb->nh.raw + rx->offset;
1328 len = rx->status;
1329 if (len > RX_COPY_THRESHOLD)
1330 len = RX_COPY_THRESHOLD;
1331 skb_put(skb, len);
1333 if (rx->status > len) {
1334 skb_shinfo(skb)->frags[0].page_offset =
1335 rx->offset + len;
1336 skb_shinfo(skb)->frags[0].size = rx->status - len;
1337 skb->data_len = rx->status - len;
1338 } else {
1339 skb_shinfo(skb)->frags[0].page = NULL;
1340 skb_shinfo(skb)->nr_frags = 0;
1343 i = xennet_fill_frags(np, skb, &tmpq);
1345 /*
1346 * Truesize must approximates the size of true data plus
1347 * any supervisor overheads. Adding hypervisor overheads
1348 * has been shown to significantly reduce achievable
1349 * bandwidth with the default receive buffer size. It is
1350 * therefore not wise to account for it here.
1352 * After alloc_skb(RX_COPY_THRESHOLD), truesize is set to
1353 * RX_COPY_THRESHOLD + the supervisor overheads. Here, we
1354 * add the size of the data pulled in xennet_fill_frags().
1356 * We also adjust for any unused space in the main data
1357 * area by subtracting (RX_COPY_THRESHOLD - len). This is
1358 * especially important with drivers which split incoming
1359 * packets into header and data, using only 66 bytes of
1360 * the main data area (see the e1000 driver for example.)
1361 * On such systems, without this last adjustement, our
1362 * achievable receive throughout using the standard receive
1363 * buffer size was cut by 25%(!!!).
1364 */
1365 skb->truesize += skb->data_len - (RX_COPY_THRESHOLD - len);
1366 skb->len += skb->data_len;
1368 /*
1369 * Old backends do not assert data_validated but we
1370 * can infer it from csum_blank so test both flags.
1371 */
1372 if (rx->flags & (NETRXF_data_validated|NETRXF_csum_blank))
1373 skb->ip_summed = CHECKSUM_UNNECESSARY;
1374 else
1375 skb->ip_summed = CHECKSUM_NONE;
1376 #ifdef CONFIG_XEN
1377 skb->proto_data_valid = (skb->ip_summed != CHECKSUM_NONE);
1378 skb->proto_csum_blank = !!(rx->flags & NETRXF_csum_blank);
1379 #endif
1380 np->stats.rx_packets++;
1381 np->stats.rx_bytes += skb->len;
1383 __skb_queue_tail(&rxq, skb);
1385 np->rx.rsp_cons = ++i;
1386 work_done++;
1389 if (pages_flipped) {
1390 /* Some pages are no longer absent... */
1391 balloon_update_driver_allowance(-pages_flipped);
1393 /* Do all the remapping work and M2P updates. */
1394 if (!xen_feature(XENFEAT_auto_translated_physmap)) {
1395 mcl = np->rx_mcl + pages_flipped;
1396 mcl->op = __HYPERVISOR_mmu_update;
1397 mcl->args[0] = (unsigned long)np->rx_mmu;
1398 mcl->args[1] = pages_flipped;
1399 mcl->args[2] = 0;
1400 mcl->args[3] = DOMID_SELF;
1401 (void)HYPERVISOR_multicall(np->rx_mcl,
1402 pages_flipped + 1);
1406 while ((skb = __skb_dequeue(&errq)))
1407 kfree_skb(skb);
1409 while ((skb = __skb_dequeue(&rxq)) != NULL) {
1410 struct page *page = (struct page *)skb->nh.raw;
1411 void *vaddr = page_address(page);
1413 memcpy(skb->data, vaddr + (skb->h.raw - skb->nh.raw),
1414 skb_headlen(skb));
1416 if (page != skb_shinfo(skb)->frags[0].page)
1417 __free_page(page);
1419 /* Ethernet work: Delayed to here as it peeks the header. */
1420 skb->protocol = eth_type_trans(skb, dev);
1422 /* Pass it up. */
1423 netif_receive_skb(skb);
1424 dev->last_rx = jiffies;
1427 /* If we get a callback with very few responses, reduce fill target. */
1428 /* NB. Note exponential increase, linear decrease. */
1429 if (((np->rx.req_prod_pvt - np->rx.sring->rsp_prod) >
1430 ((3*np->rx_target) / 4)) &&
1431 (--np->rx_target < np->rx_min_target))
1432 np->rx_target = np->rx_min_target;
1434 network_alloc_rx_buffers(dev);
1436 *pbudget -= work_done;
1437 dev->quota -= work_done;
1439 if (work_done < budget) {
1440 local_irq_save(flags);
1442 RING_FINAL_CHECK_FOR_RESPONSES(&np->rx, more_to_do);
1443 if (!more_to_do)
1444 __netif_rx_complete(dev);
1446 local_irq_restore(flags);
1449 spin_unlock(&np->rx_lock);
1451 return more_to_do;
1454 static void netif_release_tx_bufs(struct netfront_info *np)
1456 struct sk_buff *skb;
1457 int i;
1459 for (i = 1; i <= NET_TX_RING_SIZE; i++) {
1460 if ((unsigned long)np->tx_skbs[i] < PAGE_OFFSET)
1461 continue;
1463 skb = np->tx_skbs[i];
1464 gnttab_end_foreign_access_ref(
1465 np->grant_tx_ref[i], GNTMAP_readonly);
1466 gnttab_release_grant_reference(
1467 &np->gref_tx_head, np->grant_tx_ref[i]);
1468 np->grant_tx_ref[i] = GRANT_INVALID_REF;
1469 add_id_to_freelist(np->tx_skbs, i);
1470 dev_kfree_skb_irq(skb);
1474 static void netif_release_rx_bufs(struct netfront_info *np)
1476 struct mmu_update *mmu = np->rx_mmu;
1477 struct multicall_entry *mcl = np->rx_mcl;
1478 struct sk_buff_head free_list;
1479 struct sk_buff *skb;
1480 unsigned long mfn;
1481 int xfer = 0, noxfer = 0, unused = 0;
1482 int id, ref;
1484 if (np->copying_receiver) {
1485 printk("%s: fix me for copying receiver.\n", __FUNCTION__);
1486 return;
1489 skb_queue_head_init(&free_list);
1491 spin_lock(&np->rx_lock);
1493 for (id = 0; id < NET_RX_RING_SIZE; id++) {
1494 if ((ref = np->grant_rx_ref[id]) == GRANT_INVALID_REF) {
1495 unused++;
1496 continue;
1499 skb = np->rx_skbs[id];
1500 mfn = gnttab_end_foreign_transfer_ref(ref);
1501 gnttab_release_grant_reference(&np->gref_rx_head, ref);
1502 np->grant_rx_ref[id] = GRANT_INVALID_REF;
1503 add_id_to_freelist(np->rx_skbs, id);
1505 if (0 == mfn) {
1506 struct page *page = skb_shinfo(skb)->frags[0].page;
1507 balloon_release_driver_page(page);
1508 skb_shinfo(skb)->nr_frags = 0;
1509 dev_kfree_skb(skb);
1510 noxfer++;
1511 continue;
1514 if (!xen_feature(XENFEAT_auto_translated_physmap)) {
1515 /* Remap the page. */
1516 struct page *page = skb_shinfo(skb)->frags[0].page;
1517 unsigned long pfn = page_to_pfn(page);
1518 void *vaddr = page_address(page);
1520 MULTI_update_va_mapping(mcl, (unsigned long)vaddr,
1521 pfn_pte_ma(mfn, PAGE_KERNEL),
1522 0);
1523 mcl++;
1524 mmu->ptr = ((maddr_t)mfn << PAGE_SHIFT)
1525 | MMU_MACHPHYS_UPDATE;
1526 mmu->val = pfn;
1527 mmu++;
1529 set_phys_to_machine(pfn, mfn);
1531 __skb_queue_tail(&free_list, skb);
1532 xfer++;
1535 printk("%s: %d xfer, %d noxfer, %d unused\n",
1536 __FUNCTION__, xfer, noxfer, unused);
1538 if (xfer) {
1539 /* Some pages are no longer absent... */
1540 balloon_update_driver_allowance(-xfer);
1542 if (!xen_feature(XENFEAT_auto_translated_physmap)) {
1543 /* Do all the remapping work and M2P updates. */
1544 mcl->op = __HYPERVISOR_mmu_update;
1545 mcl->args[0] = (unsigned long)np->rx_mmu;
1546 mcl->args[1] = mmu - np->rx_mmu;
1547 mcl->args[2] = 0;
1548 mcl->args[3] = DOMID_SELF;
1549 mcl++;
1550 HYPERVISOR_multicall(np->rx_mcl, mcl - np->rx_mcl);
1554 while ((skb = __skb_dequeue(&free_list)) != NULL)
1555 dev_kfree_skb(skb);
1557 spin_unlock(&np->rx_lock);
1560 static int network_close(struct net_device *dev)
1562 struct netfront_info *np = netdev_priv(dev);
1563 netif_stop_queue(np->netdev);
1564 return 0;
1568 static struct net_device_stats *network_get_stats(struct net_device *dev)
1570 struct netfront_info *np = netdev_priv(dev);
1571 return &np->stats;
1574 static int xennet_change_mtu(struct net_device *dev, int mtu)
1576 int max = xennet_can_sg(dev) ? 65535 - ETH_HLEN : ETH_DATA_LEN;
1578 if (mtu > max)
1579 return -EINVAL;
1580 dev->mtu = mtu;
1581 return 0;
1584 static int xennet_set_sg(struct net_device *dev, u32 data)
1586 if (data) {
1587 struct netfront_info *np = netdev_priv(dev);
1588 int val;
1590 if (xenbus_scanf(XBT_NIL, np->xbdev->otherend, "feature-sg",
1591 "%d", &val) < 0)
1592 val = 0;
1593 if (!val)
1594 return -ENOSYS;
1595 } else if (dev->mtu > ETH_DATA_LEN)
1596 dev->mtu = ETH_DATA_LEN;
1598 return ethtool_op_set_sg(dev, data);
1601 static int xennet_set_tso(struct net_device *dev, u32 data)
1603 #ifdef HAVE_TSO
1604 if (data) {
1605 struct netfront_info *np = netdev_priv(dev);
1606 int val;
1608 if (xenbus_scanf(XBT_NIL, np->xbdev->otherend,
1609 "feature-gso-tcpv4", "%d", &val) < 0)
1610 val = 0;
1611 if (!val)
1612 return -ENOSYS;
1615 return ethtool_op_set_tso(dev, data);
1616 #else
1617 return -ENOSYS;
1618 #endif
1621 static void xennet_set_features(struct net_device *dev)
1623 dev_disable_gso_features(dev);
1624 xennet_set_sg(dev, 0);
1626 /* We need checksum offload to enable scatter/gather and TSO. */
1627 if (!(dev->features & NETIF_F_IP_CSUM))
1628 return;
1630 if (xennet_set_sg(dev, 1))
1631 return;
1633 /* Before 2.6.9 TSO seems to be unreliable so do not enable it
1634 * on older kernels.
1635 */
1636 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,9)
1637 xennet_set_tso(dev, 1);
1638 #endif
1642 static int network_connect(struct net_device *dev)
1644 struct netfront_info *np = netdev_priv(dev);
1645 int i, requeue_idx, err;
1646 struct sk_buff *skb;
1647 grant_ref_t ref;
1648 netif_rx_request_t *req;
1649 unsigned int feature_rx_copy, feature_rx_flip;
1651 err = xenbus_scanf(XBT_NIL, np->xbdev->otherend,
1652 "feature-rx-copy", "%u", &feature_rx_copy);
1653 if (err != 1)
1654 feature_rx_copy = 0;
1655 err = xenbus_scanf(XBT_NIL, np->xbdev->otherend,
1656 "feature-rx-flip", "%u", &feature_rx_flip);
1657 if (err != 1)
1658 feature_rx_flip = 1;
1660 /*
1661 * Copy packets on receive path if:
1662 * (a) This was requested by user, and the backend supports it; or
1663 * (b) Flipping was requested, but this is unsupported by the backend.
1664 */
1665 np->copying_receiver = ((MODPARM_rx_copy && feature_rx_copy) ||
1666 (MODPARM_rx_flip && !feature_rx_flip));
1668 err = talk_to_backend(np->xbdev, np);
1669 if (err)
1670 return err;
1672 xennet_set_features(dev);
1674 IPRINTK("device %s has %sing receive path.\n",
1675 dev->name, np->copying_receiver ? "copy" : "flipp");
1677 spin_lock_irq(&np->tx_lock);
1678 spin_lock(&np->rx_lock);
1680 /*
1681 * Recovery procedure:
1682 * NB. Freelist index entries are always going to be less than
1683 * PAGE_OFFSET, whereas pointers to skbs will always be equal or
1684 * greater than PAGE_OFFSET: we use this property to distinguish
1685 * them.
1686 */
1688 /* Step 1: Discard all pending TX packet fragments. */
1689 netif_release_tx_bufs(np);
1691 /* Step 2: Rebuild the RX buffer freelist and the RX ring itself. */
1692 for (requeue_idx = 0, i = 0; i < NET_RX_RING_SIZE; i++) {
1693 if (!np->rx_skbs[i])
1694 continue;
1696 skb = np->rx_skbs[requeue_idx] = xennet_get_rx_skb(np, i);
1697 ref = np->grant_rx_ref[requeue_idx] = xennet_get_rx_ref(np, i);
1698 req = RING_GET_REQUEST(&np->rx, requeue_idx);
1700 if (!np->copying_receiver) {
1701 gnttab_grant_foreign_transfer_ref(
1702 ref, np->xbdev->otherend_id,
1703 page_to_pfn(skb_shinfo(skb)->frags->page));
1704 } else {
1705 gnttab_grant_foreign_access_ref(
1706 ref, np->xbdev->otherend_id,
1707 pfn_to_mfn(page_to_pfn(skb_shinfo(skb)->
1708 frags->page)),
1709 0);
1711 req->gref = ref;
1712 req->id = requeue_idx;
1714 requeue_idx++;
1717 np->rx.req_prod_pvt = requeue_idx;
1719 /*
1720 * Step 3: All public and private state should now be sane. Get
1721 * ready to start sending and receiving packets and give the driver
1722 * domain a kick because we've probably just requeued some
1723 * packets.
1724 */
1725 netif_carrier_on(dev);
1726 notify_remote_via_irq(np->irq);
1727 network_tx_buf_gc(dev);
1728 network_alloc_rx_buffers(dev);
1730 spin_unlock(&np->rx_lock);
1731 spin_unlock_irq(&np->tx_lock);
1733 return 0;
1736 static void netif_uninit(struct net_device *dev)
1738 struct netfront_info *np = netdev_priv(dev);
1739 netif_release_tx_bufs(np);
1740 netif_release_rx_bufs(np);
1741 gnttab_free_grant_references(np->gref_tx_head);
1742 gnttab_free_grant_references(np->gref_rx_head);
1745 static struct ethtool_ops network_ethtool_ops =
1747 .get_tx_csum = ethtool_op_get_tx_csum,
1748 .set_tx_csum = ethtool_op_set_tx_csum,
1749 .get_sg = ethtool_op_get_sg,
1750 .set_sg = xennet_set_sg,
1751 .get_tso = ethtool_op_get_tso,
1752 .set_tso = xennet_set_tso,
1753 .get_link = ethtool_op_get_link,
1754 };
1756 #ifdef CONFIG_SYSFS
1757 static ssize_t show_rxbuf_min(struct class_device *cd, char *buf)
1759 struct net_device *netdev = container_of(cd, struct net_device,
1760 class_dev);
1761 struct netfront_info *info = netdev_priv(netdev);
1763 return sprintf(buf, "%u\n", info->rx_min_target);
1766 static ssize_t store_rxbuf_min(struct class_device *cd,
1767 const char *buf, size_t len)
1769 struct net_device *netdev = container_of(cd, struct net_device,
1770 class_dev);
1771 struct netfront_info *np = netdev_priv(netdev);
1772 char *endp;
1773 unsigned long target;
1775 if (!capable(CAP_NET_ADMIN))
1776 return -EPERM;
1778 target = simple_strtoul(buf, &endp, 0);
1779 if (endp == buf)
1780 return -EBADMSG;
1782 if (target < RX_MIN_TARGET)
1783 target = RX_MIN_TARGET;
1784 if (target > RX_MAX_TARGET)
1785 target = RX_MAX_TARGET;
1787 spin_lock(&np->rx_lock);
1788 if (target > np->rx_max_target)
1789 np->rx_max_target = target;
1790 np->rx_min_target = target;
1791 if (target > np->rx_target)
1792 np->rx_target = target;
1794 network_alloc_rx_buffers(netdev);
1796 spin_unlock(&np->rx_lock);
1797 return len;
1800 static ssize_t show_rxbuf_max(struct class_device *cd, char *buf)
1802 struct net_device *netdev = container_of(cd, struct net_device,
1803 class_dev);
1804 struct netfront_info *info = netdev_priv(netdev);
1806 return sprintf(buf, "%u\n", info->rx_max_target);
1809 static ssize_t store_rxbuf_max(struct class_device *cd,
1810 const char *buf, size_t len)
1812 struct net_device *netdev = container_of(cd, struct net_device,
1813 class_dev);
1814 struct netfront_info *np = netdev_priv(netdev);
1815 char *endp;
1816 unsigned long target;
1818 if (!capable(CAP_NET_ADMIN))
1819 return -EPERM;
1821 target = simple_strtoul(buf, &endp, 0);
1822 if (endp == buf)
1823 return -EBADMSG;
1825 if (target < RX_MIN_TARGET)
1826 target = RX_MIN_TARGET;
1827 if (target > RX_MAX_TARGET)
1828 target = RX_MAX_TARGET;
1830 spin_lock(&np->rx_lock);
1831 if (target < np->rx_min_target)
1832 np->rx_min_target = target;
1833 np->rx_max_target = target;
1834 if (target < np->rx_target)
1835 np->rx_target = target;
1837 network_alloc_rx_buffers(netdev);
1839 spin_unlock(&np->rx_lock);
1840 return len;
1843 static ssize_t show_rxbuf_cur(struct class_device *cd, char *buf)
1845 struct net_device *netdev = container_of(cd, struct net_device,
1846 class_dev);
1847 struct netfront_info *info = netdev_priv(netdev);
1849 return sprintf(buf, "%u\n", info->rx_target);
1852 static const struct class_device_attribute xennet_attrs[] = {
1853 __ATTR(rxbuf_min, S_IRUGO|S_IWUSR, show_rxbuf_min, store_rxbuf_min),
1854 __ATTR(rxbuf_max, S_IRUGO|S_IWUSR, show_rxbuf_max, store_rxbuf_max),
1855 __ATTR(rxbuf_cur, S_IRUGO, show_rxbuf_cur, NULL),
1856 };
1858 static int xennet_sysfs_addif(struct net_device *netdev)
1860 int i;
1861 int error = 0;
1863 for (i = 0; i < ARRAY_SIZE(xennet_attrs); i++) {
1864 error = class_device_create_file(&netdev->class_dev,
1865 &xennet_attrs[i]);
1866 if (error)
1867 goto fail;
1869 return 0;
1871 fail:
1872 while (--i >= 0)
1873 class_device_remove_file(&netdev->class_dev,
1874 &xennet_attrs[i]);
1875 return error;
1878 static void xennet_sysfs_delif(struct net_device *netdev)
1880 int i;
1882 for (i = 0; i < ARRAY_SIZE(xennet_attrs); i++) {
1883 class_device_remove_file(&netdev->class_dev,
1884 &xennet_attrs[i]);
1888 #endif /* CONFIG_SYSFS */
1891 /*
1892 * Nothing to do here. Virtual interface is point-to-point and the
1893 * physical interface is probably promiscuous anyway.
1894 */
1895 static void network_set_multicast_list(struct net_device *dev)
1899 static struct net_device * __devinit create_netdev(struct xenbus_device *dev)
1901 int i, err = 0;
1902 struct net_device *netdev = NULL;
1903 struct netfront_info *np = NULL;
1905 netdev = alloc_etherdev(sizeof(struct netfront_info));
1906 if (!netdev) {
1907 printk(KERN_WARNING "%s> alloc_etherdev failed.\n",
1908 __FUNCTION__);
1909 return ERR_PTR(-ENOMEM);
1912 np = netdev_priv(netdev);
1913 np->xbdev = dev;
1915 netif_carrier_off(netdev);
1917 spin_lock_init(&np->tx_lock);
1918 spin_lock_init(&np->rx_lock);
1920 skb_queue_head_init(&np->rx_batch);
1921 np->rx_target = RX_DFL_MIN_TARGET;
1922 np->rx_min_target = RX_DFL_MIN_TARGET;
1923 np->rx_max_target = RX_MAX_TARGET;
1925 init_timer(&np->rx_refill_timer);
1926 np->rx_refill_timer.data = (unsigned long)netdev;
1927 np->rx_refill_timer.function = rx_refill_timeout;
1929 /* Initialise {tx,rx}_skbs as a free chain containing every entry. */
1930 for (i = 0; i <= NET_TX_RING_SIZE; i++) {
1931 np->tx_skbs[i] = (void *)((unsigned long) i+1);
1932 np->grant_tx_ref[i] = GRANT_INVALID_REF;
1935 for (i = 0; i < NET_RX_RING_SIZE; i++) {
1936 np->rx_skbs[i] = NULL;
1937 np->grant_rx_ref[i] = GRANT_INVALID_REF;
1940 /* A grant for every tx ring slot */
1941 if (gnttab_alloc_grant_references(TX_MAX_TARGET,
1942 &np->gref_tx_head) < 0) {
1943 printk(KERN_ALERT "#### netfront can't alloc tx grant refs\n");
1944 err = -ENOMEM;
1945 goto exit;
1947 /* A grant for every rx ring slot */
1948 if (gnttab_alloc_grant_references(RX_MAX_TARGET,
1949 &np->gref_rx_head) < 0) {
1950 printk(KERN_ALERT "#### netfront can't alloc rx grant refs\n");
1951 err = -ENOMEM;
1952 goto exit_free_tx;
1955 netdev->open = network_open;
1956 netdev->hard_start_xmit = network_start_xmit;
1957 netdev->stop = network_close;
1958 netdev->get_stats = network_get_stats;
1959 netdev->poll = netif_poll;
1960 netdev->set_multicast_list = network_set_multicast_list;
1961 netdev->uninit = netif_uninit;
1962 netdev->change_mtu = xennet_change_mtu;
1963 netdev->weight = 64;
1964 netdev->features = NETIF_F_IP_CSUM;
1966 SET_ETHTOOL_OPS(netdev, &network_ethtool_ops);
1967 SET_MODULE_OWNER(netdev);
1968 SET_NETDEV_DEV(netdev, &dev->dev);
1970 np->netdev = netdev;
1971 return netdev;
1973 exit_free_tx:
1974 gnttab_free_grant_references(np->gref_tx_head);
1975 exit:
1976 free_netdev(netdev);
1977 return ERR_PTR(err);
1980 /*
1981 * We use this notifier to send out a fake ARP reply to reset switches and
1982 * router ARP caches when an IP interface is brought up on a VIF.
1983 */
1984 static int
1985 inetdev_notify(struct notifier_block *this, unsigned long event, void *ptr)
1987 struct in_ifaddr *ifa = (struct in_ifaddr *)ptr;
1988 struct net_device *dev = ifa->ifa_dev->dev;
1990 /* UP event and is it one of our devices? */
1991 if (event == NETDEV_UP && dev->open == network_open)
1992 (void)send_fake_arp(dev);
1994 return NOTIFY_DONE;
1998 /* ** Close down ** */
2001 /**
2002 * Handle the change of state of the backend to Closing. We must delete our
2003 * device-layer structures now, to ensure that writes are flushed through to
2004 * the backend. Once is this done, we can switch to Closed in
2005 * acknowledgement.
2006 */
2007 static void netfront_closing(struct xenbus_device *dev)
2009 struct netfront_info *info = dev->dev.driver_data;
2011 DPRINTK("%s\n", dev->nodename);
2013 close_netdev(info);
2014 xenbus_frontend_closed(dev);
2018 static int __devexit netfront_remove(struct xenbus_device *dev)
2020 struct netfront_info *info = dev->dev.driver_data;
2022 DPRINTK("%s\n", dev->nodename);
2024 netif_disconnect_backend(info);
2025 free_netdev(info->netdev);
2027 return 0;
2031 static int open_netdev(struct netfront_info *info)
2033 int err;
2035 err = register_netdev(info->netdev);
2036 if (err) {
2037 printk(KERN_WARNING "%s: register_netdev err=%d\n",
2038 __FUNCTION__, err);
2039 return err;
2042 err = xennet_sysfs_addif(info->netdev);
2043 if (err) {
2044 unregister_netdev(info->netdev);
2045 printk(KERN_WARNING "%s: add sysfs failed err=%d\n",
2046 __FUNCTION__, err);
2047 return err;
2050 return 0;
2053 static void close_netdev(struct netfront_info *info)
2055 del_timer_sync(&info->rx_refill_timer);
2057 xennet_sysfs_delif(info->netdev);
2058 unregister_netdev(info->netdev);
2062 static void netif_disconnect_backend(struct netfront_info *info)
2064 /* Stop old i/f to prevent errors whilst we rebuild the state. */
2065 spin_lock_irq(&info->tx_lock);
2066 spin_lock(&info->rx_lock);
2067 netif_carrier_off(info->netdev);
2068 spin_unlock(&info->rx_lock);
2069 spin_unlock_irq(&info->tx_lock);
2071 if (info->irq)
2072 unbind_from_irqhandler(info->irq, info->netdev);
2073 info->evtchn = info->irq = 0;
2075 end_access(info->tx_ring_ref, info->tx.sring);
2076 end_access(info->rx_ring_ref, info->rx.sring);
2077 info->tx_ring_ref = GRANT_INVALID_REF;
2078 info->rx_ring_ref = GRANT_INVALID_REF;
2079 info->tx.sring = NULL;
2080 info->rx.sring = NULL;
2084 static void end_access(int ref, void *page)
2086 if (ref != GRANT_INVALID_REF)
2087 gnttab_end_foreign_access(ref, 0, (unsigned long)page);
2091 /* ** Driver registration ** */
2094 static struct xenbus_device_id netfront_ids[] = {
2095 { "vif" },
2096 { "" }
2097 };
2100 static struct xenbus_driver netfront = {
2101 .name = "vif",
2102 .owner = THIS_MODULE,
2103 .ids = netfront_ids,
2104 .probe = netfront_probe,
2105 .remove = __devexit_p(netfront_remove),
2106 .resume = netfront_resume,
2107 .otherend_changed = backend_changed,
2108 };
2111 static struct notifier_block notifier_inetdev = {
2112 .notifier_call = inetdev_notify,
2113 .next = NULL,
2114 .priority = 0
2115 };
2117 static int __init netif_init(void)
2119 if (!is_running_on_xen())
2120 return -ENODEV;
2122 #ifdef CONFIG_XEN
2123 if (MODPARM_rx_flip && MODPARM_rx_copy) {
2124 WPRINTK("Cannot specify both rx_copy and rx_flip.\n");
2125 return -EINVAL;
2128 if (!MODPARM_rx_flip && !MODPARM_rx_copy)
2129 MODPARM_rx_flip = 1; /* Default is to flip. */
2130 #endif
2132 if (is_initial_xendomain())
2133 return 0;
2135 IPRINTK("Initialising virtual ethernet driver.\n");
2137 (void)register_inetaddr_notifier(&notifier_inetdev);
2139 return xenbus_register_frontend(&netfront);
2141 module_init(netif_init);
2144 static void __exit netif_exit(void)
2146 if (is_initial_xendomain())
2147 return;
2149 unregister_inetaddr_notifier(&notifier_inetdev);
2151 return xenbus_unregister_driver(&netfront);
2153 module_exit(netif_exit);
2155 MODULE_LICENSE("Dual BSD/GPL");