ia64/xen-unstable

view linux-2.6-xen-sparse/drivers/xen/netfront/netfront.c @ 11196:cbdc98fc3a6f

[NET] front: Allow driver to be built against kernels which do not
have extended checksum-offload info fields in teh skbuff structure.
Based on a patch from Steven Smith <ssmith@xensource.com>
Signed-off-by: Keir Fraser <keir@xensource.com>
author kfraser@localhost.localdomain
date Thu Aug 17 17:08:19 2006 +0100 (2006-08-17)
parents 8d5e6a78d51e
children a73aa6ec4c76
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 <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/uaccess.h>
62 #include <xen/interface/grant_table.h>
63 #include <xen/gnttab.h>
65 #define RX_COPY_THRESHOLD 256
67 /* If we don't have GSO, fake things up so that we never try to use it. */
68 #ifndef NETIF_F_GSO
69 #define netif_needs_gso(dev, skb) 0
70 #define dev_disable_gso_features(dev) ((void)0)
71 #else
72 #define HAVE_GSO 1
73 static inline void dev_disable_gso_features(struct net_device *dev)
74 {
75 /* Turn off all GSO bits except ROBUST. */
76 dev->features &= (1 << NETIF_F_GSO_SHIFT) - 1;
77 dev->features |= NETIF_F_GSO_ROBUST;
78 }
79 #endif
81 #define GRANT_INVALID_REF 0
83 #define NET_TX_RING_SIZE __RING_SIZE((struct netif_tx_sring *)0, PAGE_SIZE)
84 #define NET_RX_RING_SIZE __RING_SIZE((struct netif_rx_sring *)0, PAGE_SIZE)
86 struct netfront_info {
87 struct list_head list;
88 struct net_device *netdev;
90 struct net_device_stats stats;
92 struct netif_tx_front_ring tx;
93 struct netif_rx_front_ring rx;
95 spinlock_t tx_lock;
96 spinlock_t rx_lock;
98 unsigned int handle;
99 unsigned int evtchn, irq;
100 unsigned int copying_receiver;
102 /* Receive-ring batched refills. */
103 #define RX_MIN_TARGET 8
104 #define RX_DFL_MIN_TARGET 64
105 #define RX_MAX_TARGET min_t(int, NET_RX_RING_SIZE, 256)
106 unsigned rx_min_target, rx_max_target, rx_target;
107 struct sk_buff_head rx_batch;
109 struct timer_list rx_refill_timer;
111 /*
112 * {tx,rx}_skbs store outstanding skbuffs. The first entry in tx_skbs
113 * is an index into a chain of free entries.
114 */
115 struct sk_buff *tx_skbs[NET_TX_RING_SIZE+1];
116 struct sk_buff *rx_skbs[NET_RX_RING_SIZE];
118 #define TX_MAX_TARGET min_t(int, NET_RX_RING_SIZE, 256)
119 grant_ref_t gref_tx_head;
120 grant_ref_t grant_tx_ref[NET_TX_RING_SIZE + 1];
121 grant_ref_t gref_rx_head;
122 grant_ref_t grant_rx_ref[NET_TX_RING_SIZE];
124 struct xenbus_device *xbdev;
125 int tx_ring_ref;
126 int rx_ring_ref;
127 u8 mac[ETH_ALEN];
129 unsigned long rx_pfn_array[NET_RX_RING_SIZE];
130 struct multicall_entry rx_mcl[NET_RX_RING_SIZE+1];
131 struct mmu_update rx_mmu[NET_RX_RING_SIZE];
132 };
134 struct netfront_rx_info {
135 struct netif_rx_response rx;
136 struct netif_extra_info extras[XEN_NETIF_EXTRA_TYPE_MAX - 1];
137 };
139 /*
140 * Access macros for acquiring freeing slots in tx_skbs[].
141 */
143 static inline void add_id_to_freelist(struct sk_buff **list, unsigned short id)
144 {
145 list[id] = list[0];
146 list[0] = (void *)(unsigned long)id;
147 }
149 static inline unsigned short get_id_from_freelist(struct sk_buff **list)
150 {
151 unsigned int id = (unsigned int)(unsigned long)list[0];
152 list[0] = list[id];
153 return id;
154 }
156 static inline int xennet_rxidx(RING_IDX idx)
157 {
158 return idx & (NET_RX_RING_SIZE - 1);
159 }
161 static inline struct sk_buff *xennet_get_rx_skb(struct netfront_info *np,
162 RING_IDX ri)
163 {
164 int i = xennet_rxidx(ri);
165 struct sk_buff *skb = np->rx_skbs[i];
166 np->rx_skbs[i] = NULL;
167 return skb;
168 }
170 static inline grant_ref_t xennet_get_rx_ref(struct netfront_info *np,
171 RING_IDX ri)
172 {
173 int i = xennet_rxidx(ri);
174 grant_ref_t ref = np->grant_rx_ref[i];
175 np->grant_rx_ref[i] = GRANT_INVALID_REF;
176 return ref;
177 }
179 #define DPRINTK(fmt, args...) \
180 pr_debug("netfront (%s:%d) " fmt, \
181 __FUNCTION__, __LINE__, ##args)
182 #define IPRINTK(fmt, args...) \
183 printk(KERN_INFO "netfront: " fmt, ##args)
184 #define WPRINTK(fmt, args...) \
185 printk(KERN_WARNING "netfront: " fmt, ##args)
187 static int talk_to_backend(struct xenbus_device *, struct netfront_info *);
188 static int setup_device(struct xenbus_device *, struct netfront_info *);
189 static struct net_device *create_netdev(int, int, struct xenbus_device *);
191 static void netfront_closing(struct xenbus_device *);
193 static void end_access(int, void *);
194 static void netif_disconnect_backend(struct netfront_info *);
195 static void close_netdev(struct netfront_info *);
196 static void netif_free(struct netfront_info *);
198 static void network_connect(struct net_device *);
199 static void network_tx_buf_gc(struct net_device *);
200 static void network_alloc_rx_buffers(struct net_device *);
201 static int send_fake_arp(struct net_device *);
203 static irqreturn_t netif_int(int irq, void *dev_id, struct pt_regs *ptregs);
205 #ifdef CONFIG_SYSFS
206 static int xennet_sysfs_addif(struct net_device *netdev);
207 static void xennet_sysfs_delif(struct net_device *netdev);
208 #else /* !CONFIG_SYSFS */
209 #define xennet_sysfs_addif(dev) (0)
210 #define xennet_sysfs_delif(dev) do { } while(0)
211 #endif
213 static inline int xennet_can_sg(struct net_device *dev)
214 {
215 return dev->features & NETIF_F_SG;
216 }
218 /**
219 * Entry point to this code when a new device is created. Allocate the basic
220 * structures and the ring buffers for communication with the backend, and
221 * inform the backend of the appropriate details for those. Switch to
222 * Connected state.
223 */
224 static int __devinit netfront_probe(struct xenbus_device *dev,
225 const struct xenbus_device_id *id)
226 {
227 int err;
228 struct net_device *netdev;
229 struct netfront_info *info;
230 unsigned int handle;
231 unsigned feature_rx_copy;
233 err = xenbus_scanf(XBT_NIL, dev->nodename, "handle", "%u", &handle);
234 if (err != 1) {
235 xenbus_dev_fatal(dev, err, "reading handle");
236 return err;
237 }
239 #ifndef CONFIG_XEN
240 err = xenbus_scanf(XBT_NIL, dev->otherend, "feature-rx-copy", "%u",
241 &feature_rx_copy);
242 if (err != 1) {
243 xenbus_dev_fatal(dev, err, "reading feature-rx-copy");
244 return err;
245 }
246 if (!feature_rx_copy) {
247 xenbus_dev_fatal(dev, 0, "need a copy-capable backend");
248 return -EINVAL;
249 }
250 #else
251 feature_rx_copy = 0;
252 #endif
254 netdev = create_netdev(handle, feature_rx_copy, dev);
255 if (IS_ERR(netdev)) {
256 err = PTR_ERR(netdev);
257 xenbus_dev_fatal(dev, err, "creating netdev");
258 return err;
259 }
261 info = netdev_priv(netdev);
262 dev->dev.driver_data = info;
264 err = talk_to_backend(dev, info);
265 if (err) {
266 xennet_sysfs_delif(info->netdev);
267 unregister_netdev(netdev);
268 free_netdev(netdev);
269 dev->dev.driver_data = NULL;
270 return err;
271 }
273 return 0;
274 }
277 /**
278 * We are reconnecting to the backend, due to a suspend/resume, or a backend
279 * driver restart. We tear down our netif structure and recreate it, but
280 * leave the device-layer structures intact so that this is transparent to the
281 * rest of the kernel.
282 */
283 static int netfront_resume(struct xenbus_device *dev)
284 {
285 struct netfront_info *info = dev->dev.driver_data;
287 DPRINTK("%s\n", dev->nodename);
289 netif_disconnect_backend(info);
290 return talk_to_backend(dev, info);
291 }
293 static int xen_net_read_mac(struct xenbus_device *dev, u8 mac[])
294 {
295 char *s, *e, *macstr;
296 int i;
298 macstr = s = xenbus_read(XBT_NIL, dev->nodename, "mac", NULL);
299 if (IS_ERR(macstr))
300 return PTR_ERR(macstr);
302 for (i = 0; i < ETH_ALEN; i++) {
303 mac[i] = simple_strtoul(s, &e, 16);
304 if ((s == e) || (*e != ((i == ETH_ALEN-1) ? '\0' : ':'))) {
305 kfree(macstr);
306 return -ENOENT;
307 }
308 s = e+1;
309 }
311 kfree(macstr);
312 return 0;
313 }
315 /* Common code used when first setting up, and when resuming. */
316 static int talk_to_backend(struct xenbus_device *dev,
317 struct netfront_info *info)
318 {
319 const char *message;
320 struct xenbus_transaction xbt;
321 int err;
323 err = xen_net_read_mac(dev, info->mac);
324 if (err) {
325 xenbus_dev_fatal(dev, err, "parsing %s/mac", dev->nodename);
326 goto out;
327 }
329 /* Create shared ring, alloc event channel. */
330 err = setup_device(dev, info);
331 if (err)
332 goto out;
334 again:
335 err = xenbus_transaction_start(&xbt);
336 if (err) {
337 xenbus_dev_fatal(dev, err, "starting transaction");
338 goto destroy_ring;
339 }
341 err = xenbus_printf(xbt, dev->nodename, "tx-ring-ref","%u",
342 info->tx_ring_ref);
343 if (err) {
344 message = "writing tx ring-ref";
345 goto abort_transaction;
346 }
347 err = xenbus_printf(xbt, dev->nodename, "rx-ring-ref","%u",
348 info->rx_ring_ref);
349 if (err) {
350 message = "writing rx ring-ref";
351 goto abort_transaction;
352 }
353 err = xenbus_printf(xbt, dev->nodename,
354 "event-channel", "%u", info->evtchn);
355 if (err) {
356 message = "writing event-channel";
357 goto abort_transaction;
358 }
360 err = xenbus_printf(xbt, dev->nodename, "request-rx-copy", "%u",
361 info->copying_receiver);
362 if (err) {
363 message = "writing request-rx-copy";
364 goto abort_transaction;
365 }
367 err = xenbus_printf(xbt, dev->nodename, "feature-rx-notify", "%d", 1);
368 if (err) {
369 message = "writing feature-rx-notify";
370 goto abort_transaction;
371 }
373 err = xenbus_printf(xbt, dev->nodename, "feature-sg", "%d", 1);
374 if (err) {
375 message = "writing feature-sg";
376 goto abort_transaction;
377 }
379 #ifdef HAVE_GSO
380 err = xenbus_printf(xbt, dev->nodename, "feature-gso-tcpv4", "%d", 1);
381 if (err) {
382 message = "writing feature-gso-tcpv4";
383 goto abort_transaction;
384 }
385 #endif
387 err = xenbus_transaction_end(xbt, 0);
388 if (err) {
389 if (err == -EAGAIN)
390 goto again;
391 xenbus_dev_fatal(dev, err, "completing transaction");
392 goto destroy_ring;
393 }
395 return 0;
397 abort_transaction:
398 xenbus_transaction_end(xbt, 1);
399 xenbus_dev_fatal(dev, err, "%s", message);
400 destroy_ring:
401 netif_free(info);
402 out:
403 return err;
404 }
407 static int setup_device(struct xenbus_device *dev, struct netfront_info *info)
408 {
409 struct netif_tx_sring *txs;
410 struct netif_rx_sring *rxs;
411 int err;
412 struct net_device *netdev = info->netdev;
414 info->tx_ring_ref = GRANT_INVALID_REF;
415 info->rx_ring_ref = GRANT_INVALID_REF;
416 info->rx.sring = NULL;
417 info->tx.sring = NULL;
418 info->irq = 0;
420 txs = (struct netif_tx_sring *)get_zeroed_page(GFP_KERNEL);
421 if (!txs) {
422 err = -ENOMEM;
423 xenbus_dev_fatal(dev, err, "allocating tx ring page");
424 goto fail;
425 }
426 SHARED_RING_INIT(txs);
427 FRONT_RING_INIT(&info->tx, txs, PAGE_SIZE);
429 err = xenbus_grant_ring(dev, virt_to_mfn(txs));
430 if (err < 0) {
431 free_page((unsigned long)txs);
432 goto fail;
433 }
434 info->tx_ring_ref = err;
436 rxs = (struct netif_rx_sring *)get_zeroed_page(GFP_KERNEL);
437 if (!rxs) {
438 err = -ENOMEM;
439 xenbus_dev_fatal(dev, err, "allocating rx ring page");
440 goto fail;
441 }
442 SHARED_RING_INIT(rxs);
443 FRONT_RING_INIT(&info->rx, rxs, PAGE_SIZE);
445 err = xenbus_grant_ring(dev, virt_to_mfn(rxs));
446 if (err < 0) {
447 free_page((unsigned long)rxs);
448 goto fail;
449 }
450 info->rx_ring_ref = err;
452 err = xenbus_alloc_evtchn(dev, &info->evtchn);
453 if (err)
454 goto fail;
456 memcpy(netdev->dev_addr, info->mac, ETH_ALEN);
457 err = bind_evtchn_to_irqhandler(info->evtchn, netif_int,
458 SA_SAMPLE_RANDOM, netdev->name, netdev);
459 if (err < 0)
460 goto fail;
461 info->irq = err;
462 return 0;
464 fail:
465 netif_free(info);
466 return err;
467 }
470 /**
471 * Callback received when the backend's state changes.
472 */
473 static void backend_changed(struct xenbus_device *dev,
474 enum xenbus_state backend_state)
475 {
476 struct netfront_info *np = dev->dev.driver_data;
477 struct net_device *netdev = np->netdev;
479 DPRINTK("\n");
481 switch (backend_state) {
482 case XenbusStateInitialising:
483 case XenbusStateInitialised:
484 case XenbusStateConnected:
485 case XenbusStateUnknown:
486 case XenbusStateClosed:
487 break;
489 case XenbusStateInitWait:
490 network_connect(netdev);
491 xenbus_switch_state(dev, XenbusStateConnected);
492 (void)send_fake_arp(netdev);
493 break;
495 case XenbusStateClosing:
496 netfront_closing(dev);
497 break;
498 }
499 }
502 /** Send a packet on a net device to encourage switches to learn the
503 * MAC. We send a fake ARP request.
504 *
505 * @param dev device
506 * @return 0 on success, error code otherwise
507 */
508 static int send_fake_arp(struct net_device *dev)
509 {
510 struct sk_buff *skb;
511 u32 src_ip, dst_ip;
513 dst_ip = INADDR_BROADCAST;
514 src_ip = inet_select_addr(dev, dst_ip, RT_SCOPE_LINK);
516 /* No IP? Then nothing to do. */
517 if (src_ip == 0)
518 return 0;
520 skb = arp_create(ARPOP_REPLY, ETH_P_ARP,
521 dst_ip, dev, src_ip,
522 /*dst_hw*/ NULL, /*src_hw*/ NULL,
523 /*target_hw*/ dev->dev_addr);
524 if (skb == NULL)
525 return -ENOMEM;
527 return dev_queue_xmit(skb);
528 }
531 static int network_open(struct net_device *dev)
532 {
533 struct netfront_info *np = netdev_priv(dev);
535 memset(&np->stats, 0, sizeof(np->stats));
537 network_alloc_rx_buffers(dev);
538 np->rx.sring->rsp_event = np->rx.rsp_cons + 1;
540 if (RING_HAS_UNCONSUMED_RESPONSES(&np->rx))
541 netif_rx_schedule(dev);
543 netif_start_queue(dev);
545 return 0;
546 }
548 static inline int netfront_tx_slot_available(struct netfront_info *np)
549 {
550 return RING_FREE_REQUESTS(&np->tx) >= MAX_SKB_FRAGS + 2;
551 }
553 static inline void network_maybe_wake_tx(struct net_device *dev)
554 {
555 struct netfront_info *np = netdev_priv(dev);
557 if (unlikely(netif_queue_stopped(dev)) &&
558 netfront_tx_slot_available(np) &&
559 likely(netif_running(dev)))
560 netif_wake_queue(dev);
561 }
563 static void network_tx_buf_gc(struct net_device *dev)
564 {
565 RING_IDX cons, prod;
566 unsigned short id;
567 struct netfront_info *np = netdev_priv(dev);
568 struct sk_buff *skb;
570 if (unlikely(!netif_carrier_ok(dev)))
571 return;
573 do {
574 prod = np->tx.sring->rsp_prod;
575 rmb(); /* Ensure we see responses up to 'rp'. */
577 for (cons = np->tx.rsp_cons; cons != prod; cons++) {
578 struct netif_tx_response *txrsp;
580 txrsp = RING_GET_RESPONSE(&np->tx, cons);
581 if (txrsp->status == NETIF_RSP_NULL)
582 continue;
584 id = txrsp->id;
585 skb = np->tx_skbs[id];
586 if (unlikely(gnttab_query_foreign_access(
587 np->grant_tx_ref[id]) != 0)) {
588 printk(KERN_ALERT "network_tx_buf_gc: warning "
589 "-- grant still in use by backend "
590 "domain.\n");
591 BUG();
592 }
593 gnttab_end_foreign_access_ref(
594 np->grant_tx_ref[id], GNTMAP_readonly);
595 gnttab_release_grant_reference(
596 &np->gref_tx_head, np->grant_tx_ref[id]);
597 np->grant_tx_ref[id] = GRANT_INVALID_REF;
598 add_id_to_freelist(np->tx_skbs, id);
599 dev_kfree_skb_irq(skb);
600 }
602 np->tx.rsp_cons = prod;
604 /*
605 * Set a new event, then check for race with update of tx_cons.
606 * Note that it is essential to schedule a callback, no matter
607 * how few buffers are pending. Even if there is space in the
608 * transmit ring, higher layers may be blocked because too much
609 * data is outstanding: in such cases notification from Xen is
610 * likely to be the only kick that we'll get.
611 */
612 np->tx.sring->rsp_event =
613 prod + ((np->tx.sring->req_prod - prod) >> 1) + 1;
614 mb();
615 } while ((cons == prod) && (prod != np->tx.sring->rsp_prod));
617 network_maybe_wake_tx(dev);
618 }
621 static void rx_refill_timeout(unsigned long data)
622 {
623 struct net_device *dev = (struct net_device *)data;
624 netif_rx_schedule(dev);
625 }
628 static void network_alloc_rx_buffers(struct net_device *dev)
629 {
630 unsigned short id;
631 struct netfront_info *np = netdev_priv(dev);
632 struct sk_buff *skb;
633 struct page *page;
634 int i, batch_target, notify;
635 RING_IDX req_prod = np->rx.req_prod_pvt;
636 struct xen_memory_reservation reservation;
637 grant_ref_t ref;
638 unsigned long pfn;
639 void *vaddr;
640 int nr_flips;
641 netif_rx_request_t *req;
643 if (unlikely(!netif_carrier_ok(dev)))
644 return;
646 /*
647 * Allocate skbuffs greedily, even though we batch updates to the
648 * receive ring. This creates a less bursty demand on the memory
649 * allocator, so should reduce the chance of failed allocation requests
650 * both for ourself and for other kernel subsystems.
651 */
652 batch_target = np->rx_target - (req_prod - np->rx.rsp_cons);
653 for (i = skb_queue_len(&np->rx_batch); i < batch_target; i++) {
654 /*
655 * Allocate an skb and a page. Do not use __dev_alloc_skb as
656 * that will allocate page-sized buffers which is not
657 * necessary here.
658 * 16 bytes added as necessary headroom for netif_receive_skb.
659 */
660 skb = alloc_skb(RX_COPY_THRESHOLD + 16,
661 GFP_ATOMIC | __GFP_NOWARN);
662 if (unlikely(!skb))
663 goto no_skb;
665 page = alloc_page(GFP_ATOMIC | __GFP_NOWARN);
666 if (!page) {
667 kfree_skb(skb);
668 no_skb:
669 /* Any skbuffs queued for refill? Force them out. */
670 if (i != 0)
671 goto refill;
672 /* Could not allocate any skbuffs. Try again later. */
673 mod_timer(&np->rx_refill_timer,
674 jiffies + (HZ/10));
675 break;
676 }
678 skb_reserve(skb, 16); /* mimic dev_alloc_skb() */
679 skb_shinfo(skb)->frags[0].page = page;
680 skb_shinfo(skb)->nr_frags = 1;
681 __skb_queue_tail(&np->rx_batch, skb);
682 }
684 /* Is the batch large enough to be worthwhile? */
685 if (i < (np->rx_target/2)) {
686 if (req_prod > np->rx.sring->req_prod)
687 goto push;
688 return;
689 }
691 /* Adjust our fill target if we risked running out of buffers. */
692 if (((req_prod - np->rx.sring->rsp_prod) < (np->rx_target / 4)) &&
693 ((np->rx_target *= 2) > np->rx_max_target))
694 np->rx_target = np->rx_max_target;
696 refill:
697 for (nr_flips = i = 0; ; i++) {
698 if ((skb = __skb_dequeue(&np->rx_batch)) == NULL)
699 break;
701 skb->dev = dev;
703 id = xennet_rxidx(req_prod + i);
705 BUG_ON(np->rx_skbs[id]);
706 np->rx_skbs[id] = skb;
708 ref = gnttab_claim_grant_reference(&np->gref_rx_head);
709 BUG_ON((signed short)ref < 0);
710 np->grant_rx_ref[id] = ref;
712 pfn = page_to_pfn(skb_shinfo(skb)->frags[0].page);
713 vaddr = page_address(skb_shinfo(skb)->frags[0].page);
715 req = RING_GET_REQUEST(&np->rx, req_prod + i);
716 if (!np->copying_receiver) {
717 gnttab_grant_foreign_transfer_ref(ref,
718 np->xbdev->otherend_id,
719 pfn);
720 np->rx_pfn_array[nr_flips] = pfn_to_mfn(pfn);
721 if (!xen_feature(XENFEAT_auto_translated_physmap)) {
722 /* Remove this page before passing
723 * back to Xen. */
724 set_phys_to_machine(pfn, INVALID_P2M_ENTRY);
725 MULTI_update_va_mapping(np->rx_mcl+i,
726 (unsigned long)vaddr,
727 __pte(0), 0);
728 }
729 nr_flips++;
730 } else {
731 gnttab_grant_foreign_access_ref(ref,
732 np->xbdev->otherend_id,
733 pfn,
734 0);
735 }
737 req->id = id;
738 req->gref = ref;
739 }
741 if ( nr_flips != 0 ) {
742 /* Tell the ballon driver what is going on. */
743 balloon_update_driver_allowance(i);
745 set_xen_guest_handle(reservation.extent_start,
746 np->rx_pfn_array);
747 reservation.nr_extents = nr_flips;
748 reservation.extent_order = 0;
749 reservation.address_bits = 0;
750 reservation.domid = DOMID_SELF;
752 if (!xen_feature(XENFEAT_auto_translated_physmap)) {
753 /* After all PTEs have been zapped, flush the TLB. */
754 np->rx_mcl[i-1].args[MULTI_UVMFLAGS_INDEX] =
755 UVMF_TLB_FLUSH|UVMF_ALL;
757 /* Give away a batch of pages. */
758 np->rx_mcl[i].op = __HYPERVISOR_memory_op;
759 np->rx_mcl[i].args[0] = XENMEM_decrease_reservation;
760 np->rx_mcl[i].args[1] = (unsigned long)&reservation;
762 /* Zap PTEs and give away pages in one big
763 * multicall. */
764 (void)HYPERVISOR_multicall(np->rx_mcl, i+1);
766 /* Check return status of HYPERVISOR_memory_op(). */
767 if (unlikely(np->rx_mcl[i].result != i))
768 panic("Unable to reduce memory reservation\n");
769 } else {
770 if (HYPERVISOR_memory_op(XENMEM_decrease_reservation,
771 &reservation) != i)
772 panic("Unable to reduce memory reservation\n");
773 }
774 } else {
775 wmb();
776 }
778 /* Above is a suitable barrier to ensure backend will see requests. */
779 np->rx.req_prod_pvt = req_prod + i;
780 push:
781 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&np->rx, notify);
782 if (notify)
783 notify_remote_via_irq(np->irq);
784 }
786 static void xennet_make_frags(struct sk_buff *skb, struct net_device *dev,
787 struct netif_tx_request *tx)
788 {
789 struct netfront_info *np = netdev_priv(dev);
790 char *data = skb->data;
791 unsigned long mfn;
792 RING_IDX prod = np->tx.req_prod_pvt;
793 int frags = skb_shinfo(skb)->nr_frags;
794 unsigned int offset = offset_in_page(data);
795 unsigned int len = skb_headlen(skb);
796 unsigned int id;
797 grant_ref_t ref;
798 int i;
800 while (len > PAGE_SIZE - offset) {
801 tx->size = PAGE_SIZE - offset;
802 tx->flags |= NETTXF_more_data;
803 len -= tx->size;
804 data += tx->size;
805 offset = 0;
807 id = get_id_from_freelist(np->tx_skbs);
808 np->tx_skbs[id] = skb_get(skb);
809 tx = RING_GET_REQUEST(&np->tx, prod++);
810 tx->id = id;
811 ref = gnttab_claim_grant_reference(&np->gref_tx_head);
812 BUG_ON((signed short)ref < 0);
814 mfn = virt_to_mfn(data);
815 gnttab_grant_foreign_access_ref(ref, np->xbdev->otherend_id,
816 mfn, GNTMAP_readonly);
818 tx->gref = np->grant_tx_ref[id] = ref;
819 tx->offset = offset;
820 tx->size = len;
821 tx->flags = 0;
822 }
824 for (i = 0; i < frags; i++) {
825 skb_frag_t *frag = skb_shinfo(skb)->frags + i;
827 tx->flags |= NETTXF_more_data;
829 id = get_id_from_freelist(np->tx_skbs);
830 np->tx_skbs[id] = skb_get(skb);
831 tx = RING_GET_REQUEST(&np->tx, prod++);
832 tx->id = id;
833 ref = gnttab_claim_grant_reference(&np->gref_tx_head);
834 BUG_ON((signed short)ref < 0);
836 mfn = pfn_to_mfn(page_to_pfn(frag->page));
837 gnttab_grant_foreign_access_ref(ref, np->xbdev->otherend_id,
838 mfn, GNTMAP_readonly);
840 tx->gref = np->grant_tx_ref[id] = ref;
841 tx->offset = frag->page_offset;
842 tx->size = frag->size;
843 tx->flags = 0;
844 }
846 np->tx.req_prod_pvt = prod;
847 }
849 static int network_start_xmit(struct sk_buff *skb, struct net_device *dev)
850 {
851 unsigned short id;
852 struct netfront_info *np = netdev_priv(dev);
853 struct netif_tx_request *tx;
854 struct netif_extra_info *extra;
855 char *data = skb->data;
856 RING_IDX i;
857 grant_ref_t ref;
858 unsigned long mfn;
859 int notify;
860 int frags = skb_shinfo(skb)->nr_frags;
861 unsigned int offset = offset_in_page(data);
862 unsigned int len = skb_headlen(skb);
864 frags += (offset + len + PAGE_SIZE - 1) / PAGE_SIZE;
865 if (unlikely(frags > MAX_SKB_FRAGS + 1)) {
866 printk(KERN_ALERT "xennet: skb rides the rocket: %d frags\n",
867 frags);
868 dump_stack();
869 goto drop;
870 }
872 spin_lock_irq(&np->tx_lock);
874 if (unlikely(!netif_carrier_ok(dev) ||
875 (frags > 1 && !xennet_can_sg(dev)) ||
876 netif_needs_gso(dev, skb))) {
877 spin_unlock_irq(&np->tx_lock);
878 goto drop;
879 }
881 i = np->tx.req_prod_pvt;
883 id = get_id_from_freelist(np->tx_skbs);
884 np->tx_skbs[id] = skb;
886 tx = RING_GET_REQUEST(&np->tx, i);
888 tx->id = id;
889 ref = gnttab_claim_grant_reference(&np->gref_tx_head);
890 BUG_ON((signed short)ref < 0);
891 mfn = virt_to_mfn(data);
892 gnttab_grant_foreign_access_ref(
893 ref, np->xbdev->otherend_id, mfn, GNTMAP_readonly);
894 tx->gref = np->grant_tx_ref[id] = ref;
895 tx->offset = offset;
896 tx->size = len;
898 tx->flags = 0;
899 extra = NULL;
901 if (skb->ip_summed == CHECKSUM_HW) /* local packet? */
902 tx->flags |= NETTXF_csum_blank | NETTXF_data_validated;
903 #ifdef CONFIG_XEN
904 if (skb->proto_data_valid) /* remote but checksummed? */
905 tx->flags |= NETTXF_data_validated;
906 #endif
908 #ifdef HAVE_GSO
909 if (skb_shinfo(skb)->gso_size) {
910 struct netif_extra_info *gso = (struct netif_extra_info *)
911 RING_GET_REQUEST(&np->tx, ++i);
913 if (extra)
914 extra->flags |= XEN_NETIF_EXTRA_FLAG_MORE;
915 else
916 tx->flags |= NETTXF_extra_info;
918 gso->u.gso.size = skb_shinfo(skb)->gso_size;
919 gso->u.gso.type = XEN_NETIF_GSO_TYPE_TCPV4;
920 gso->u.gso.pad = 0;
921 gso->u.gso.features = 0;
923 gso->type = XEN_NETIF_EXTRA_TYPE_GSO;
924 gso->flags = 0;
925 extra = gso;
926 }
927 #endif
929 np->tx.req_prod_pvt = i + 1;
931 xennet_make_frags(skb, dev, tx);
932 tx->size = skb->len;
934 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&np->tx, notify);
935 if (notify)
936 notify_remote_via_irq(np->irq);
938 network_tx_buf_gc(dev);
940 if (!netfront_tx_slot_available(np))
941 netif_stop_queue(dev);
943 spin_unlock_irq(&np->tx_lock);
945 np->stats.tx_bytes += skb->len;
946 np->stats.tx_packets++;
948 return 0;
950 drop:
951 np->stats.tx_dropped++;
952 dev_kfree_skb(skb);
953 return 0;
954 }
956 static irqreturn_t netif_int(int irq, void *dev_id, struct pt_regs *ptregs)
957 {
958 struct net_device *dev = dev_id;
959 struct netfront_info *np = netdev_priv(dev);
960 unsigned long flags;
962 spin_lock_irqsave(&np->tx_lock, flags);
963 network_tx_buf_gc(dev);
964 spin_unlock_irqrestore(&np->tx_lock, flags);
966 if (RING_HAS_UNCONSUMED_RESPONSES(&np->rx) &&
967 likely(netif_running(dev)))
968 netif_rx_schedule(dev);
970 return IRQ_HANDLED;
971 }
973 static void xennet_move_rx_slot(struct netfront_info *np, struct sk_buff *skb,
974 grant_ref_t ref)
975 {
976 int new = xennet_rxidx(np->rx.req_prod_pvt);
978 BUG_ON(np->rx_skbs[new]);
979 np->rx_skbs[new] = skb;
980 np->grant_rx_ref[new] = ref;
981 RING_GET_REQUEST(&np->rx, np->rx.req_prod_pvt)->id = new;
982 RING_GET_REQUEST(&np->rx, np->rx.req_prod_pvt)->gref = ref;
983 np->rx.req_prod_pvt++;
984 }
986 int xennet_get_extras(struct netfront_info *np,
987 struct netif_extra_info *extras, RING_IDX rp)
989 {
990 struct netif_extra_info *extra;
991 RING_IDX cons = np->rx.rsp_cons;
992 int err = 0;
994 do {
995 struct sk_buff *skb;
996 grant_ref_t ref;
998 if (unlikely(cons + 1 == rp)) {
999 if (net_ratelimit())
1000 WPRINTK("Missing extra info\n");
1001 err = -EBADR;
1002 break;
1005 extra = (struct netif_extra_info *)
1006 RING_GET_RESPONSE(&np->rx, ++cons);
1008 if (unlikely(!extra->type ||
1009 extra->type >= XEN_NETIF_EXTRA_TYPE_MAX)) {
1010 if (net_ratelimit())
1011 WPRINTK("Invalid extra type: %d\n",
1012 extra->type);
1013 err = -EINVAL;
1014 } else
1015 memcpy(&extras[extra->type - 1], extra, sizeof(*extra));
1017 skb = xennet_get_rx_skb(np, cons);
1018 ref = xennet_get_rx_ref(np, cons);
1019 xennet_move_rx_slot(np, skb, ref);
1020 } while (extra->flags & XEN_NETIF_EXTRA_FLAG_MORE);
1022 np->rx.rsp_cons = cons;
1023 return err;
1026 static int xennet_get_responses(struct netfront_info *np,
1027 struct netfront_rx_info *rinfo, RING_IDX rp,
1028 struct sk_buff_head *list, int *mcl_offset_p)
1030 int mcl_offset = *mcl_offset_p;
1031 struct mmu_update *mmu;
1032 struct multicall_entry *mcl;
1033 struct netif_rx_response *rx = &rinfo->rx;
1034 struct netif_extra_info *extras = rinfo->extras;
1035 RING_IDX cons = np->rx.rsp_cons;
1036 struct sk_buff *skb = xennet_get_rx_skb(np, cons);
1037 grant_ref_t ref = xennet_get_rx_ref(np, cons);
1038 int max = MAX_SKB_FRAGS + (rx->status <= RX_COPY_THRESHOLD);
1039 int frags = 1;
1040 int err = 0;
1041 unsigned long ret;
1043 if (rx->flags & NETRXF_extra_info) {
1044 err = xennet_get_extras(np, extras, rp);
1045 cons = np->rx.rsp_cons;
1048 for (;;) {
1049 unsigned long mfn;
1051 if (unlikely(rx->status < 0 ||
1052 rx->offset + rx->status > PAGE_SIZE)) {
1053 if (net_ratelimit())
1054 WPRINTK("rx->offset: %x, size: %u\n",
1055 rx->offset, rx->status);
1056 err = -EINVAL;
1057 goto next;
1060 /*
1061 * This definitely indicates a bug, either in this driver or in
1062 * the backend driver. In future this should flag the bad
1063 * situation to the system controller to reboot the backed.
1064 */
1065 if (ref == GRANT_INVALID_REF) {
1066 WPRINTK("Bad rx response id %d.\n", rx->id);
1067 err = -EINVAL;
1068 goto next;
1071 if (!np->copying_receiver) {
1072 /* Memory pressure, insufficient buffer
1073 * headroom, ... */
1074 if (!(mfn = gnttab_end_foreign_transfer_ref(ref))) {
1075 if (net_ratelimit())
1076 WPRINTK("Unfulfilled rx req (id=%d, st=%d).\n",
1077 rx->id, rx->status);
1078 xennet_move_rx_slot(np, skb, ref);
1079 err = -ENOMEM;
1080 goto next;
1083 if (!xen_feature(XENFEAT_auto_translated_physmap)) {
1084 /* Remap the page. */
1085 struct page *page =
1086 skb_shinfo(skb)->frags[0].page;
1087 unsigned long pfn = page_to_pfn(page);
1088 void *vaddr = page_address(page);
1090 mcl = np->rx_mcl + mcl_offset;
1091 mmu = np->rx_mmu + mcl_offset;
1093 MULTI_update_va_mapping(mcl,
1094 (unsigned long)vaddr,
1095 pfn_pte_ma(mfn,
1096 PAGE_KERNEL),
1097 0);
1098 mmu->ptr = ((maddr_t)mfn << PAGE_SHIFT)
1099 | MMU_MACHPHYS_UPDATE;
1100 mmu->val = pfn;
1102 mcl_offset++;
1104 set_phys_to_machine(pfn, mfn);
1106 } else {
1107 ret = gnttab_end_foreign_access_ref(ref, 0);
1108 BUG_ON(!ret);
1111 gnttab_release_grant_reference(&np->gref_rx_head, ref);
1113 __skb_queue_tail(list, skb);
1115 next:
1116 if (!(rx->flags & NETRXF_more_data))
1117 break;
1119 if (cons + frags == rp) {
1120 if (net_ratelimit())
1121 WPRINTK("Need more frags\n");
1122 err = -ENOENT;
1123 break;
1126 rx = RING_GET_RESPONSE(&np->rx, cons + frags);
1127 skb = xennet_get_rx_skb(np, cons + frags);
1128 ref = xennet_get_rx_ref(np, cons + frags);
1129 frags++;
1132 if (unlikely(frags > max)) {
1133 if (net_ratelimit())
1134 WPRINTK("Too many frags\n");
1135 err = -E2BIG;
1138 *mcl_offset_p = mcl_offset;
1140 return err;
1143 static RING_IDX xennet_fill_frags(struct netfront_info *np,
1144 struct sk_buff *skb,
1145 struct sk_buff_head *list)
1147 struct skb_shared_info *shinfo = skb_shinfo(skb);
1148 int nr_frags = shinfo->nr_frags;
1149 RING_IDX cons = np->rx.rsp_cons;
1150 skb_frag_t *frag = shinfo->frags + nr_frags;
1151 struct sk_buff *nskb;
1153 while ((nskb = __skb_dequeue(list))) {
1154 struct netif_rx_response *rx =
1155 RING_GET_RESPONSE(&np->rx, ++cons);
1157 frag->page = skb_shinfo(nskb)->frags[0].page;
1158 frag->page_offset = rx->offset;
1159 frag->size = rx->status;
1161 skb->data_len += rx->status;
1163 skb_shinfo(nskb)->nr_frags = 0;
1164 kfree_skb(nskb);
1166 frag++;
1167 nr_frags++;
1170 shinfo->nr_frags = nr_frags;
1171 return cons;
1174 static int xennet_set_skb_gso(struct sk_buff *skb,
1175 struct netif_extra_info *gso)
1177 if (!gso->u.gso.size) {
1178 if (net_ratelimit())
1179 WPRINTK("GSO size must not be zero.\n");
1180 return -EINVAL;
1183 /* Currently only TCPv4 S.O. is supported. */
1184 if (gso->u.gso.type != XEN_NETIF_GSO_TYPE_TCPV4) {
1185 if (net_ratelimit())
1186 WPRINTK("Bad GSO type %d.\n", gso->u.gso.type);
1187 return -EINVAL;
1190 #ifdef HAVE_GSO
1191 skb_shinfo(skb)->gso_size = gso->u.gso.size;
1192 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
1194 /* Header must be checked, and gso_segs computed. */
1195 skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
1196 skb_shinfo(skb)->gso_segs = 0;
1198 return 0;
1199 #else
1200 if (net_ratelimit())
1201 WPRINTK("GSO unsupported by this kernel.\n");
1202 return -EINVAL;
1203 #endif
1206 static int netif_poll(struct net_device *dev, int *pbudget)
1208 struct netfront_info *np = netdev_priv(dev);
1209 struct sk_buff *skb;
1210 struct netfront_rx_info rinfo;
1211 struct netif_rx_response *rx = &rinfo.rx;
1212 struct netif_extra_info *extras = rinfo.extras;
1213 RING_IDX i, rp;
1214 struct multicall_entry *mcl;
1215 int work_done, budget, more_to_do = 1;
1216 struct sk_buff_head rxq;
1217 struct sk_buff_head errq;
1218 struct sk_buff_head tmpq;
1219 unsigned long flags;
1220 unsigned int len;
1221 int pages_done;
1222 int err;
1224 spin_lock(&np->rx_lock);
1226 if (unlikely(!netif_carrier_ok(dev))) {
1227 spin_unlock(&np->rx_lock);
1228 return 0;
1231 skb_queue_head_init(&rxq);
1232 skb_queue_head_init(&errq);
1233 skb_queue_head_init(&tmpq);
1235 if ((budget = *pbudget) > dev->quota)
1236 budget = dev->quota;
1237 rp = np->rx.sring->rsp_prod;
1238 rmb(); /* Ensure we see queued responses up to 'rp'. */
1240 for (i = np->rx.rsp_cons, work_done = 0, pages_done = 0;
1241 (i != rp) && (work_done < budget);
1242 np->rx.rsp_cons = ++i, work_done++) {
1243 memcpy(rx, RING_GET_RESPONSE(&np->rx, i), sizeof(*rx));
1244 memset(extras, 0, sizeof(extras));
1246 err = xennet_get_responses(np, &rinfo, rp, &tmpq, &pages_done);
1248 if (unlikely(err)) {
1249 err:
1250 i = np->rx.rsp_cons + skb_queue_len(&tmpq) - 1;
1251 work_done--;
1252 while ((skb = __skb_dequeue(&tmpq)))
1253 __skb_queue_tail(&errq, skb);
1254 np->stats.rx_errors++;
1255 continue;
1258 skb = __skb_dequeue(&tmpq);
1260 if (extras[XEN_NETIF_EXTRA_TYPE_GSO - 1].type) {
1261 struct netif_extra_info *gso;
1262 gso = &extras[XEN_NETIF_EXTRA_TYPE_GSO - 1];
1264 if (unlikely(xennet_set_skb_gso(skb, gso))) {
1265 __skb_queue_head(&tmpq, skb);
1266 goto err;
1270 skb->nh.raw = (void *)skb_shinfo(skb)->frags[0].page;
1271 skb->h.raw = skb->nh.raw + rx->offset;
1273 len = rx->status;
1274 if (len > RX_COPY_THRESHOLD)
1275 len = RX_COPY_THRESHOLD;
1276 skb_put(skb, len);
1278 if (rx->status > len) {
1279 skb_shinfo(skb)->frags[0].page_offset =
1280 rx->offset + len;
1281 skb_shinfo(skb)->frags[0].size = rx->status - len;
1282 skb->data_len = rx->status - len;
1283 } else {
1284 skb_shinfo(skb)->frags[0].page = NULL;
1285 skb_shinfo(skb)->nr_frags = 0;
1288 i = xennet_fill_frags(np, skb, &tmpq);
1290 /*
1291 * Truesize must approximates the size of true data plus
1292 * any supervisor overheads. Adding hypervisor overheads
1293 * has been shown to significantly reduce achievable
1294 * bandwidth with the default receive buffer size. It is
1295 * therefore not wise to account for it here.
1297 * After alloc_skb(RX_COPY_THRESHOLD), truesize is set to
1298 * RX_COPY_THRESHOLD + the supervisor overheads. Here, we
1299 * add the size of the data pulled in xennet_fill_frags().
1301 * We also adjust for any unused space in the main data
1302 * area by subtracting (RX_COPY_THRESHOLD - len). This is
1303 * especially important with drivers which split incoming
1304 * packets into header and data, using only 66 bytes of
1305 * the main data area (see the e1000 driver for example.)
1306 * On such systems, without this last adjustement, our
1307 * achievable receive throughout using the standard receive
1308 * buffer size was cut by 25%(!!!).
1309 */
1310 skb->truesize += skb->data_len - (RX_COPY_THRESHOLD - len);
1311 skb->len += skb->data_len;
1313 /*
1314 * Old backends do not assert data_validated but we
1315 * can infer it from csum_blank so test both flags.
1316 */
1317 if (rx->flags & (NETRXF_data_validated|NETRXF_csum_blank))
1318 skb->ip_summed = CHECKSUM_UNNECESSARY;
1319 else
1320 skb->ip_summed = CHECKSUM_NONE;
1321 #ifdef CONFIG_XEN
1322 skb->proto_data_valid = (skb->ip_summed != CHECKSUM_NONE);
1323 skb->proto_csum_blank = !!(rx->flags & NETRXF_csum_blank);
1324 #endif
1325 np->stats.rx_packets++;
1326 np->stats.rx_bytes += skb->len;
1328 __skb_queue_tail(&rxq, skb);
1331 /* Some pages are no longer absent... */
1332 balloon_update_driver_allowance(-pages_done);
1334 /* Do all the remapping work, and M2P updates, in one big hypercall. */
1335 if (likely(pages_done)) {
1336 mcl = np->rx_mcl + pages_done;
1337 mcl->op = __HYPERVISOR_mmu_update;
1338 mcl->args[0] = (unsigned long)np->rx_mmu;
1339 mcl->args[1] = pages_done;
1340 mcl->args[2] = 0;
1341 mcl->args[3] = DOMID_SELF;
1342 (void)HYPERVISOR_multicall(np->rx_mcl, pages_done + 1);
1345 while ((skb = __skb_dequeue(&errq)))
1346 kfree_skb(skb);
1348 while ((skb = __skb_dequeue(&rxq)) != NULL) {
1349 struct page *page = (struct page *)skb->nh.raw;
1350 void *vaddr = page_address(page);
1352 memcpy(skb->data, vaddr + (skb->h.raw - skb->nh.raw),
1353 skb_headlen(skb));
1355 if (page != skb_shinfo(skb)->frags[0].page)
1356 __free_page(page);
1358 /* Ethernet work: Delayed to here as it peeks the header. */
1359 skb->protocol = eth_type_trans(skb, dev);
1361 /* Pass it up. */
1362 netif_receive_skb(skb);
1363 dev->last_rx = jiffies;
1366 /* If we get a callback with very few responses, reduce fill target. */
1367 /* NB. Note exponential increase, linear decrease. */
1368 if (((np->rx.req_prod_pvt - np->rx.sring->rsp_prod) >
1369 ((3*np->rx_target) / 4)) &&
1370 (--np->rx_target < np->rx_min_target))
1371 np->rx_target = np->rx_min_target;
1373 network_alloc_rx_buffers(dev);
1375 *pbudget -= work_done;
1376 dev->quota -= work_done;
1378 if (work_done < budget) {
1379 local_irq_save(flags);
1381 RING_FINAL_CHECK_FOR_RESPONSES(&np->rx, more_to_do);
1382 if (!more_to_do)
1383 __netif_rx_complete(dev);
1385 local_irq_restore(flags);
1388 spin_unlock(&np->rx_lock);
1390 return more_to_do;
1394 static int network_close(struct net_device *dev)
1396 struct netfront_info *np = netdev_priv(dev);
1397 netif_stop_queue(np->netdev);
1398 return 0;
1402 static struct net_device_stats *network_get_stats(struct net_device *dev)
1404 struct netfront_info *np = netdev_priv(dev);
1405 return &np->stats;
1408 static int xennet_change_mtu(struct net_device *dev, int mtu)
1410 int max = xennet_can_sg(dev) ? 65535 - ETH_HLEN : ETH_DATA_LEN;
1412 if (mtu > max)
1413 return -EINVAL;
1414 dev->mtu = mtu;
1415 return 0;
1418 static int xennet_set_sg(struct net_device *dev, u32 data)
1420 if (data) {
1421 struct netfront_info *np = netdev_priv(dev);
1422 int val;
1424 if (xenbus_scanf(XBT_NIL, np->xbdev->otherend, "feature-sg",
1425 "%d", &val) < 0)
1426 val = 0;
1427 if (!val)
1428 return -ENOSYS;
1429 } else if (dev->mtu > ETH_DATA_LEN)
1430 dev->mtu = ETH_DATA_LEN;
1432 return ethtool_op_set_sg(dev, data);
1435 static int xennet_set_tso(struct net_device *dev, u32 data)
1437 #ifdef HAVE_GSO
1438 if (data) {
1439 struct netfront_info *np = netdev_priv(dev);
1440 int val;
1442 if (xenbus_scanf(XBT_NIL, np->xbdev->otherend,
1443 "feature-gso-tcpv4", "%d", &val) < 0)
1444 val = 0;
1445 if (!val)
1446 return -ENOSYS;
1449 return ethtool_op_set_tso(dev, data);
1450 #else
1451 return -ENOSYS;
1452 #endif
1455 static void xennet_set_features(struct net_device *dev)
1457 dev_disable_gso_features(dev);
1458 xennet_set_sg(dev, 0);
1460 /* We need checksum offload to enable scatter/gather and TSO. */
1461 if (!(dev->features & NETIF_F_ALL_CSUM))
1462 return;
1464 if (!xennet_set_sg(dev, 1))
1465 xennet_set_tso(dev, 1);
1468 static void network_connect(struct net_device *dev)
1470 struct netfront_info *np = netdev_priv(dev);
1471 int i, requeue_idx;
1472 struct sk_buff *skb;
1473 grant_ref_t ref;
1474 netif_rx_request_t *req;
1476 xennet_set_features(dev);
1478 spin_lock_irq(&np->tx_lock);
1479 spin_lock(&np->rx_lock);
1481 /*
1482 * Recovery procedure:
1483 * NB. Freelist index entries are always going to be less than
1484 * PAGE_OFFSET, whereas pointers to skbs will always be equal or
1485 * greater than PAGE_OFFSET: we use this property to distinguish
1486 * them.
1487 */
1489 /* Step 1: Discard all pending TX packet fragments. */
1490 for (requeue_idx = 0, i = 1; i <= NET_TX_RING_SIZE; i++) {
1491 if ((unsigned long)np->tx_skbs[i] < PAGE_OFFSET)
1492 continue;
1494 skb = np->tx_skbs[i];
1495 gnttab_end_foreign_access_ref(
1496 np->grant_tx_ref[i], GNTMAP_readonly);
1497 gnttab_release_grant_reference(
1498 &np->gref_tx_head, np->grant_tx_ref[i]);
1499 np->grant_tx_ref[i] = GRANT_INVALID_REF;
1500 add_id_to_freelist(np->tx_skbs, i);
1501 dev_kfree_skb_irq(skb);
1504 /* Step 2: Rebuild the RX buffer freelist and the RX ring itself. */
1505 for (requeue_idx = 0, i = 0; i < NET_RX_RING_SIZE; i++) {
1506 if (!np->rx_skbs[i])
1507 continue;
1509 skb = np->rx_skbs[requeue_idx] = xennet_get_rx_skb(np, i);
1510 ref = np->grant_rx_ref[requeue_idx] = xennet_get_rx_ref(np, i);
1511 req = RING_GET_REQUEST(&np->rx, requeue_idx);
1513 if (!np->copying_receiver) {
1514 gnttab_grant_foreign_transfer_ref(
1515 ref, np->xbdev->otherend_id,
1516 page_to_pfn(skb_shinfo(skb)->frags->page));
1517 } else {
1518 gnttab_grant_foreign_access_ref(
1519 ref, np->xbdev->otherend_id,
1520 page_to_pfn(skb_shinfo(skb)->frags->page),
1521 0);
1523 req->gref = ref;
1524 req->id = requeue_idx;
1526 requeue_idx++;
1529 np->rx.req_prod_pvt = requeue_idx;
1531 /*
1532 * Step 3: All public and private state should now be sane. Get
1533 * ready to start sending and receiving packets and give the driver
1534 * domain a kick because we've probably just requeued some
1535 * packets.
1536 */
1537 netif_carrier_on(dev);
1538 notify_remote_via_irq(np->irq);
1539 network_tx_buf_gc(dev);
1540 network_alloc_rx_buffers(dev);
1542 spin_unlock(&np->rx_lock);
1543 spin_unlock_irq(&np->tx_lock);
1546 static void netif_uninit(struct net_device *dev)
1548 struct netfront_info *np = netdev_priv(dev);
1549 gnttab_free_grant_references(np->gref_tx_head);
1550 gnttab_free_grant_references(np->gref_rx_head);
1553 static struct ethtool_ops network_ethtool_ops =
1555 .get_tx_csum = ethtool_op_get_tx_csum,
1556 .set_tx_csum = ethtool_op_set_tx_csum,
1557 .get_sg = ethtool_op_get_sg,
1558 .set_sg = xennet_set_sg,
1559 .get_tso = ethtool_op_get_tso,
1560 .set_tso = xennet_set_tso,
1561 .get_link = ethtool_op_get_link,
1562 };
1564 #ifdef CONFIG_SYSFS
1565 static ssize_t show_rxbuf_min(struct class_device *cd, char *buf)
1567 struct net_device *netdev = container_of(cd, struct net_device,
1568 class_dev);
1569 struct netfront_info *info = netdev_priv(netdev);
1571 return sprintf(buf, "%u\n", info->rx_min_target);
1574 static ssize_t store_rxbuf_min(struct class_device *cd,
1575 const char *buf, size_t len)
1577 struct net_device *netdev = container_of(cd, struct net_device,
1578 class_dev);
1579 struct netfront_info *np = netdev_priv(netdev);
1580 char *endp;
1581 unsigned long target;
1583 if (!capable(CAP_NET_ADMIN))
1584 return -EPERM;
1586 target = simple_strtoul(buf, &endp, 0);
1587 if (endp == buf)
1588 return -EBADMSG;
1590 if (target < RX_MIN_TARGET)
1591 target = RX_MIN_TARGET;
1592 if (target > RX_MAX_TARGET)
1593 target = RX_MAX_TARGET;
1595 spin_lock(&np->rx_lock);
1596 if (target > np->rx_max_target)
1597 np->rx_max_target = target;
1598 np->rx_min_target = target;
1599 if (target > np->rx_target)
1600 np->rx_target = target;
1602 network_alloc_rx_buffers(netdev);
1604 spin_unlock(&np->rx_lock);
1605 return len;
1608 static ssize_t show_rxbuf_max(struct class_device *cd, char *buf)
1610 struct net_device *netdev = container_of(cd, struct net_device,
1611 class_dev);
1612 struct netfront_info *info = netdev_priv(netdev);
1614 return sprintf(buf, "%u\n", info->rx_max_target);
1617 static ssize_t store_rxbuf_max(struct class_device *cd,
1618 const char *buf, size_t len)
1620 struct net_device *netdev = container_of(cd, struct net_device,
1621 class_dev);
1622 struct netfront_info *np = netdev_priv(netdev);
1623 char *endp;
1624 unsigned long target;
1626 if (!capable(CAP_NET_ADMIN))
1627 return -EPERM;
1629 target = simple_strtoul(buf, &endp, 0);
1630 if (endp == buf)
1631 return -EBADMSG;
1633 if (target < RX_MIN_TARGET)
1634 target = RX_MIN_TARGET;
1635 if (target > RX_MAX_TARGET)
1636 target = RX_MAX_TARGET;
1638 spin_lock(&np->rx_lock);
1639 if (target < np->rx_min_target)
1640 np->rx_min_target = target;
1641 np->rx_max_target = target;
1642 if (target < np->rx_target)
1643 np->rx_target = target;
1645 network_alloc_rx_buffers(netdev);
1647 spin_unlock(&np->rx_lock);
1648 return len;
1651 static ssize_t show_rxbuf_cur(struct class_device *cd, char *buf)
1653 struct net_device *netdev = container_of(cd, struct net_device,
1654 class_dev);
1655 struct netfront_info *info = netdev_priv(netdev);
1657 return sprintf(buf, "%u\n", info->rx_target);
1660 static const struct class_device_attribute xennet_attrs[] = {
1661 __ATTR(rxbuf_min, S_IRUGO|S_IWUSR, show_rxbuf_min, store_rxbuf_min),
1662 __ATTR(rxbuf_max, S_IRUGO|S_IWUSR, show_rxbuf_max, store_rxbuf_max),
1663 __ATTR(rxbuf_cur, S_IRUGO, show_rxbuf_cur, NULL),
1664 };
1666 static int xennet_sysfs_addif(struct net_device *netdev)
1668 int i;
1669 int error = 0;
1671 for (i = 0; i < ARRAY_SIZE(xennet_attrs); i++) {
1672 error = class_device_create_file(&netdev->class_dev,
1673 &xennet_attrs[i]);
1674 if (error)
1675 goto fail;
1677 return 0;
1679 fail:
1680 while (--i >= 0)
1681 class_device_remove_file(&netdev->class_dev,
1682 &xennet_attrs[i]);
1683 return error;
1686 static void xennet_sysfs_delif(struct net_device *netdev)
1688 int i;
1690 for (i = 0; i < ARRAY_SIZE(xennet_attrs); i++) {
1691 class_device_remove_file(&netdev->class_dev,
1692 &xennet_attrs[i]);
1696 #endif /* CONFIG_SYSFS */
1699 /*
1700 * Nothing to do here. Virtual interface is point-to-point and the
1701 * physical interface is probably promiscuous anyway.
1702 */
1703 static void network_set_multicast_list(struct net_device *dev)
1707 static struct net_device * __devinit
1708 create_netdev(int handle, int copying_receiver, struct xenbus_device *dev)
1710 int i, err = 0;
1711 struct net_device *netdev = NULL;
1712 struct netfront_info *np = NULL;
1714 netdev = alloc_etherdev(sizeof(struct netfront_info));
1715 if (!netdev) {
1716 printk(KERN_WARNING "%s> alloc_etherdev failed.\n",
1717 __FUNCTION__);
1718 return ERR_PTR(-ENOMEM);
1721 np = netdev_priv(netdev);
1722 np->handle = handle;
1723 np->xbdev = dev;
1724 np->copying_receiver = copying_receiver;
1726 netif_carrier_off(netdev);
1728 spin_lock_init(&np->tx_lock);
1729 spin_lock_init(&np->rx_lock);
1731 skb_queue_head_init(&np->rx_batch);
1732 np->rx_target = RX_DFL_MIN_TARGET;
1733 np->rx_min_target = RX_DFL_MIN_TARGET;
1734 np->rx_max_target = RX_MAX_TARGET;
1736 init_timer(&np->rx_refill_timer);
1737 np->rx_refill_timer.data = (unsigned long)netdev;
1738 np->rx_refill_timer.function = rx_refill_timeout;
1740 /* Initialise {tx,rx}_skbs as a free chain containing every entry. */
1741 for (i = 0; i <= NET_TX_RING_SIZE; i++) {
1742 np->tx_skbs[i] = (void *)((unsigned long) i+1);
1743 np->grant_tx_ref[i] = GRANT_INVALID_REF;
1746 for (i = 0; i < NET_RX_RING_SIZE; i++) {
1747 np->rx_skbs[i] = NULL;
1748 np->grant_rx_ref[i] = GRANT_INVALID_REF;
1751 /* A grant for every tx ring slot */
1752 if (gnttab_alloc_grant_references(TX_MAX_TARGET,
1753 &np->gref_tx_head) < 0) {
1754 printk(KERN_ALERT "#### netfront can't alloc tx grant refs\n");
1755 err = -ENOMEM;
1756 goto exit;
1758 /* A grant for every rx ring slot */
1759 if (gnttab_alloc_grant_references(RX_MAX_TARGET,
1760 &np->gref_rx_head) < 0) {
1761 printk(KERN_ALERT "#### netfront can't alloc rx grant refs\n");
1762 err = -ENOMEM;
1763 goto exit_free_tx;
1766 netdev->open = network_open;
1767 netdev->hard_start_xmit = network_start_xmit;
1768 netdev->stop = network_close;
1769 netdev->get_stats = network_get_stats;
1770 netdev->poll = netif_poll;
1771 netdev->set_multicast_list = network_set_multicast_list;
1772 netdev->uninit = netif_uninit;
1773 netdev->change_mtu = xennet_change_mtu;
1774 netdev->weight = 64;
1775 netdev->features = NETIF_F_IP_CSUM;
1777 SET_ETHTOOL_OPS(netdev, &network_ethtool_ops);
1778 SET_MODULE_OWNER(netdev);
1779 SET_NETDEV_DEV(netdev, &dev->dev);
1781 err = register_netdev(netdev);
1782 if (err) {
1783 printk(KERN_WARNING "%s> register_netdev err=%d\n",
1784 __FUNCTION__, err);
1785 goto exit_free_rx;
1788 err = xennet_sysfs_addif(netdev);
1789 if (err) {
1790 /* This can be non-fatal: it only means no tuning parameters */
1791 printk(KERN_WARNING "%s> add sysfs failed err=%d\n",
1792 __FUNCTION__, err);
1795 np->netdev = netdev;
1797 return netdev;
1800 exit_free_rx:
1801 gnttab_free_grant_references(np->gref_rx_head);
1802 exit_free_tx:
1803 gnttab_free_grant_references(np->gref_tx_head);
1804 exit:
1805 free_netdev(netdev);
1806 return ERR_PTR(err);
1809 /*
1810 * We use this notifier to send out a fake ARP reply to reset switches and
1811 * router ARP caches when an IP interface is brought up on a VIF.
1812 */
1813 static int
1814 inetdev_notify(struct notifier_block *this, unsigned long event, void *ptr)
1816 struct in_ifaddr *ifa = (struct in_ifaddr *)ptr;
1817 struct net_device *dev = ifa->ifa_dev->dev;
1819 /* UP event and is it one of our devices? */
1820 if (event == NETDEV_UP && dev->open == network_open)
1821 (void)send_fake_arp(dev);
1823 return NOTIFY_DONE;
1827 /* ** Close down ** */
1830 /**
1831 * Handle the change of state of the backend to Closing. We must delete our
1832 * device-layer structures now, to ensure that writes are flushed through to
1833 * the backend. Once is this done, we can switch to Closed in
1834 * acknowledgement.
1835 */
1836 static void netfront_closing(struct xenbus_device *dev)
1838 struct netfront_info *info = dev->dev.driver_data;
1840 DPRINTK("netfront_closing: %s removed\n", dev->nodename);
1842 close_netdev(info);
1844 xenbus_switch_state(dev, XenbusStateClosed);
1848 static int __devexit netfront_remove(struct xenbus_device *dev)
1850 struct netfront_info *info = dev->dev.driver_data;
1852 DPRINTK("%s\n", dev->nodename);
1854 netif_disconnect_backend(info);
1855 free_netdev(info->netdev);
1857 return 0;
1861 static void close_netdev(struct netfront_info *info)
1863 del_timer_sync(&info->rx_refill_timer);
1865 xennet_sysfs_delif(info->netdev);
1866 unregister_netdev(info->netdev);
1870 static void netif_disconnect_backend(struct netfront_info *info)
1872 /* Stop old i/f to prevent errors whilst we rebuild the state. */
1873 spin_lock_irq(&info->tx_lock);
1874 spin_lock(&info->rx_lock);
1875 netif_carrier_off(info->netdev);
1876 spin_unlock(&info->rx_lock);
1877 spin_unlock_irq(&info->tx_lock);
1879 if (info->irq)
1880 unbind_from_irqhandler(info->irq, info->netdev);
1881 info->evtchn = info->irq = 0;
1883 end_access(info->tx_ring_ref, info->tx.sring);
1884 end_access(info->rx_ring_ref, info->rx.sring);
1885 info->tx_ring_ref = GRANT_INVALID_REF;
1886 info->rx_ring_ref = GRANT_INVALID_REF;
1887 info->tx.sring = NULL;
1888 info->rx.sring = NULL;
1892 static void netif_free(struct netfront_info *info)
1894 close_netdev(info);
1895 netif_disconnect_backend(info);
1896 free_netdev(info->netdev);
1900 static void end_access(int ref, void *page)
1902 if (ref != GRANT_INVALID_REF)
1903 gnttab_end_foreign_access(ref, 0, (unsigned long)page);
1907 /* ** Driver registration ** */
1910 static struct xenbus_device_id netfront_ids[] = {
1911 { "vif" },
1912 { "" }
1913 };
1916 static struct xenbus_driver netfront = {
1917 .name = "vif",
1918 .owner = THIS_MODULE,
1919 .ids = netfront_ids,
1920 .probe = netfront_probe,
1921 .remove = __devexit_p(netfront_remove),
1922 .resume = netfront_resume,
1923 .otherend_changed = backend_changed,
1924 };
1927 static struct notifier_block notifier_inetdev = {
1928 .notifier_call = inetdev_notify,
1929 .next = NULL,
1930 .priority = 0
1931 };
1933 static int __init netif_init(void)
1935 if (!is_running_on_xen())
1936 return -ENODEV;
1938 if (is_initial_xendomain())
1939 return 0;
1941 IPRINTK("Initialising virtual ethernet driver.\n");
1943 (void)register_inetaddr_notifier(&notifier_inetdev);
1945 return xenbus_register_frontend(&netfront);
1947 module_init(netif_init);
1950 static void __exit netif_exit(void)
1952 unregister_inetaddr_notifier(&notifier_inetdev);
1954 return xenbus_unregister_driver(&netfront);
1956 module_exit(netif_exit);
1958 MODULE_LICENSE("Dual BSD/GPL");