2 * Network-device interface management.
4 * Copyright (c) 2004-2005, Keir Fraser
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License version 2
8 * as published by the Free Software Foundation; or, when distributed
9 * separately from the Linux kernel or incorporated into other
10 * software packages, subject to the following license:
12 * Permission is hereby granted, free of charge, to any person obtaining a copy
13 * of this source file (the "Software"), to deal in the Software without
14 * restriction, including without limitation the rights to use, copy, modify,
15 * merge, publish, distribute, sublicense, and/or sell copies of the Software,
16 * and to permit persons to whom the Software is furnished to do so, subject to
17 * the following conditions:
19 * The above copyright notice and this permission notice shall be included in
20 * all copies or substantial portions of the Software.
22 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
27 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
33 #include <linux/kthread.h>
34 #include <linux/ethtool.h>
35 #include <linux/rtnetlink.h>
36 #include <linux/if_vlan.h>
37 #include <linux/vmalloc.h>
39 #include <xen/events.h>
40 #include <asm/xen/hypercall.h>
41 #include <xen/balloon.h>
43 #define XENVIF_QUEUE_LENGTH 32
44 #define XENVIF_NAPI_WEIGHT 64
46 int xenvif_schedulable(struct xenvif *vif)
48 return netif_running(vif->dev) && netif_carrier_ok(vif->dev);
51 static irqreturn_t xenvif_tx_interrupt(int irq, void *dev_id)
53 struct xenvif *vif = dev_id;
55 if (RING_HAS_UNCONSUMED_REQUESTS(&vif->tx))
56 napi_schedule(&vif->napi);
61 static int xenvif_poll(struct napi_struct *napi, int budget)
63 struct xenvif *vif = container_of(napi, struct xenvif, napi);
66 work_done = xenvif_tx_action(vif, budget);
68 if (work_done < budget) {
72 /* It is necessary to disable IRQ before calling
73 * RING_HAS_UNCONSUMED_REQUESTS. Otherwise we might
74 * lose event from the frontend.
77 * RING_HAS_UNCONSUMED_REQUESTS
78 * <frontend generates event to trigger napi_schedule>
81 * This handler is still in scheduled state so the
82 * event has no effect at all. After __napi_complete
83 * this handler is descheduled and cannot get
84 * scheduled again. We lose event in this case and the ring
85 * will be completely stalled.
88 local_irq_save(flags);
90 RING_FINAL_CHECK_FOR_REQUESTS(&vif->tx, more_to_do);
92 xenvif_tx_pending_slots_available(vif)))
93 __napi_complete(napi);
95 local_irq_restore(flags);
101 static irqreturn_t xenvif_rx_interrupt(int irq, void *dev_id)
103 struct xenvif *vif = dev_id;
105 xenvif_kick_thread(vif);
110 static irqreturn_t xenvif_interrupt(int irq, void *dev_id)
112 xenvif_tx_interrupt(irq, dev_id);
113 xenvif_rx_interrupt(irq, dev_id);
118 static void xenvif_wake_queue(unsigned long data)
120 struct xenvif *vif = (struct xenvif *)data;
122 if (netif_queue_stopped(vif->dev)) {
123 netdev_err(vif->dev, "draining TX queue\n");
124 vif->rx_queue_purge = true;
125 xenvif_kick_thread(vif);
126 netif_wake_queue(vif->dev);
130 static int xenvif_start_xmit(struct sk_buff *skb, struct net_device *dev)
132 struct xenvif *vif = netdev_priv(dev);
133 int min_slots_needed;
135 BUG_ON(skb->dev != dev);
137 /* Drop the packet if vif is not ready */
138 if (vif->task == NULL ||
139 vif->dealloc_task == NULL ||
140 !xenvif_schedulable(vif))
143 /* At best we'll need one slot for the header and one for each
146 min_slots_needed = 1 + skb_shinfo(skb)->nr_frags;
148 /* If the skb is GSO then we'll also need an extra slot for the
151 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4 ||
152 skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6)
155 /* If the skb can't possibly fit in the remaining slots
156 * then turn off the queue to give the ring a chance to
159 if (!xenvif_rx_ring_slots_available(vif, min_slots_needed)) {
160 vif->wake_queue.function = xenvif_wake_queue;
161 vif->wake_queue.data = (unsigned long)vif;
162 xenvif_stop_queue(vif);
163 mod_timer(&vif->wake_queue,
164 jiffies + rx_drain_timeout_jiffies);
167 skb_queue_tail(&vif->rx_queue, skb);
168 xenvif_kick_thread(vif);
173 vif->dev->stats.tx_dropped++;
178 static struct net_device_stats *xenvif_get_stats(struct net_device *dev)
180 struct xenvif *vif = netdev_priv(dev);
181 return &vif->dev->stats;
184 static void xenvif_up(struct xenvif *vif)
186 napi_enable(&vif->napi);
187 enable_irq(vif->tx_irq);
188 if (vif->tx_irq != vif->rx_irq)
189 enable_irq(vif->rx_irq);
190 xenvif_check_rx_xenvif(vif);
193 static void xenvif_down(struct xenvif *vif)
195 napi_disable(&vif->napi);
196 disable_irq(vif->tx_irq);
197 if (vif->tx_irq != vif->rx_irq)
198 disable_irq(vif->rx_irq);
199 del_timer_sync(&vif->credit_timeout);
202 static int xenvif_open(struct net_device *dev)
204 struct xenvif *vif = netdev_priv(dev);
205 if (netif_carrier_ok(dev))
207 netif_start_queue(dev);
211 static int xenvif_close(struct net_device *dev)
213 struct xenvif *vif = netdev_priv(dev);
214 if (netif_carrier_ok(dev))
216 netif_stop_queue(dev);
220 static int xenvif_change_mtu(struct net_device *dev, int mtu)
222 struct xenvif *vif = netdev_priv(dev);
223 int max = vif->can_sg ? 65535 - VLAN_ETH_HLEN : ETH_DATA_LEN;
231 static netdev_features_t xenvif_fix_features(struct net_device *dev,
232 netdev_features_t features)
234 struct xenvif *vif = netdev_priv(dev);
237 features &= ~NETIF_F_SG;
238 if (~(vif->gso_mask | vif->gso_prefix_mask) & GSO_BIT(TCPV4))
239 features &= ~NETIF_F_TSO;
240 if (~(vif->gso_mask | vif->gso_prefix_mask) & GSO_BIT(TCPV6))
241 features &= ~NETIF_F_TSO6;
243 features &= ~NETIF_F_IP_CSUM;
245 features &= ~NETIF_F_IPV6_CSUM;
250 static const struct xenvif_stat {
251 char name[ETH_GSTRING_LEN];
255 "rx_gso_checksum_fixup",
256 offsetof(struct xenvif, rx_gso_checksum_fixup)
258 /* If (sent != success + fail), there are probably packets never
263 offsetof(struct xenvif, tx_zerocopy_sent),
266 "tx_zerocopy_success",
267 offsetof(struct xenvif, tx_zerocopy_success),
271 offsetof(struct xenvif, tx_zerocopy_fail)
273 /* Number of packets exceeding MAX_SKB_FRAG slots. You should use
274 * a guest with the same MAX_SKB_FRAG
278 offsetof(struct xenvif, tx_frag_overflow)
282 static int xenvif_get_sset_count(struct net_device *dev, int string_set)
284 switch (string_set) {
286 return ARRAY_SIZE(xenvif_stats);
292 static void xenvif_get_ethtool_stats(struct net_device *dev,
293 struct ethtool_stats *stats, u64 * data)
295 void *vif = netdev_priv(dev);
298 for (i = 0; i < ARRAY_SIZE(xenvif_stats); i++)
299 data[i] = *(unsigned long *)(vif + xenvif_stats[i].offset);
302 static void xenvif_get_strings(struct net_device *dev, u32 stringset, u8 * data)
308 for (i = 0; i < ARRAY_SIZE(xenvif_stats); i++)
309 memcpy(data + i * ETH_GSTRING_LEN,
310 xenvif_stats[i].name, ETH_GSTRING_LEN);
315 static const struct ethtool_ops xenvif_ethtool_ops = {
316 .get_link = ethtool_op_get_link,
318 .get_sset_count = xenvif_get_sset_count,
319 .get_ethtool_stats = xenvif_get_ethtool_stats,
320 .get_strings = xenvif_get_strings,
323 static const struct net_device_ops xenvif_netdev_ops = {
324 .ndo_start_xmit = xenvif_start_xmit,
325 .ndo_get_stats = xenvif_get_stats,
326 .ndo_open = xenvif_open,
327 .ndo_stop = xenvif_close,
328 .ndo_change_mtu = xenvif_change_mtu,
329 .ndo_fix_features = xenvif_fix_features,
330 .ndo_set_mac_address = eth_mac_addr,
331 .ndo_validate_addr = eth_validate_addr,
334 struct xenvif *xenvif_alloc(struct device *parent, domid_t domid,
338 struct net_device *dev;
340 char name[IFNAMSIZ] = {};
343 snprintf(name, IFNAMSIZ - 1, "vif%u.%u", domid, handle);
344 dev = alloc_netdev(sizeof(struct xenvif), name, ether_setup);
346 pr_warn("Could not allocate netdev for %s\n", name);
347 return ERR_PTR(-ENOMEM);
350 SET_NETDEV_DEV(dev, parent);
352 vif = netdev_priv(dev);
354 vif->grant_copy_op = vmalloc(sizeof(struct gnttab_copy) *
356 if (vif->grant_copy_op == NULL) {
357 pr_warn("Could not allocate grant copy space for %s\n", name);
359 return ERR_PTR(-ENOMEM);
363 vif->handle = handle;
368 vif->credit_bytes = vif->remaining_credit = ~0UL;
369 vif->credit_usec = 0UL;
370 init_timer(&vif->credit_timeout);
371 vif->credit_window_start = get_jiffies_64();
373 init_timer(&vif->wake_queue);
375 dev->netdev_ops = &xenvif_netdev_ops;
376 dev->hw_features = NETIF_F_SG |
377 NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
378 NETIF_F_TSO | NETIF_F_TSO6;
379 dev->features = dev->hw_features | NETIF_F_RXCSUM;
380 SET_ETHTOOL_OPS(dev, &xenvif_ethtool_ops);
382 dev->tx_queue_len = XENVIF_QUEUE_LENGTH;
384 skb_queue_head_init(&vif->rx_queue);
385 skb_queue_head_init(&vif->tx_queue);
387 vif->pending_cons = 0;
388 vif->pending_prod = MAX_PENDING_REQS;
389 for (i = 0; i < MAX_PENDING_REQS; i++)
390 vif->pending_ring[i] = i;
391 spin_lock_init(&vif->callback_lock);
392 spin_lock_init(&vif->response_lock);
393 /* If ballooning is disabled, this will consume real memory, so you
394 * better enable it. The long term solution would be to use just a
395 * bunch of valid page descriptors, without dependency on ballooning
397 err = alloc_xenballooned_pages(MAX_PENDING_REQS,
401 netdev_err(dev, "Could not reserve mmap_pages\n");
402 return ERR_PTR(-ENOMEM);
404 for (i = 0; i < MAX_PENDING_REQS; i++) {
405 vif->pending_tx_info[i].callback_struct = (struct ubuf_info)
406 { .callback = xenvif_zerocopy_callback,
409 vif->grant_tx_handle[i] = NETBACK_INVALID_HANDLE;
411 init_timer(&vif->dealloc_delay);
414 * Initialise a dummy MAC address. We choose the numerically
415 * largest non-broadcast address to prevent the address getting
416 * stolen by an Ethernet bridge for STP purposes.
417 * (FE:FF:FF:FF:FF:FF)
419 memset(dev->dev_addr, 0xFF, ETH_ALEN);
420 dev->dev_addr[0] &= ~0x01;
422 netif_napi_add(dev, &vif->napi, xenvif_poll, XENVIF_NAPI_WEIGHT);
424 netif_carrier_off(dev);
426 err = register_netdev(dev);
428 netdev_warn(dev, "Could not register device: err=%d\n", err);
433 netdev_dbg(dev, "Successfully created xenvif\n");
435 __module_get(THIS_MODULE);
440 int xenvif_connect(struct xenvif *vif, unsigned long tx_ring_ref,
441 unsigned long rx_ring_ref, unsigned int tx_evtchn,
442 unsigned int rx_evtchn)
444 struct task_struct *task;
449 BUG_ON(vif->dealloc_task);
451 err = xenvif_map_frontend_rings(vif, tx_ring_ref, rx_ring_ref);
455 init_waitqueue_head(&vif->wq);
456 init_waitqueue_head(&vif->dealloc_wq);
458 if (tx_evtchn == rx_evtchn) {
459 /* feature-split-event-channels == 0 */
460 err = bind_interdomain_evtchn_to_irqhandler(
461 vif->domid, tx_evtchn, xenvif_interrupt, 0,
462 vif->dev->name, vif);
465 vif->tx_irq = vif->rx_irq = err;
466 disable_irq(vif->tx_irq);
468 /* feature-split-event-channels == 1 */
469 snprintf(vif->tx_irq_name, sizeof(vif->tx_irq_name),
470 "%s-tx", vif->dev->name);
471 err = bind_interdomain_evtchn_to_irqhandler(
472 vif->domid, tx_evtchn, xenvif_tx_interrupt, 0,
473 vif->tx_irq_name, vif);
477 disable_irq(vif->tx_irq);
479 snprintf(vif->rx_irq_name, sizeof(vif->rx_irq_name),
480 "%s-rx", vif->dev->name);
481 err = bind_interdomain_evtchn_to_irqhandler(
482 vif->domid, rx_evtchn, xenvif_rx_interrupt, 0,
483 vif->rx_irq_name, vif);
487 disable_irq(vif->rx_irq);
490 task = kthread_create(xenvif_kthread_guest_rx,
491 (void *)vif, "%s-guest-rx", vif->dev->name);
493 pr_warn("Could not allocate kthread for %s\n", vif->dev->name);
500 task = kthread_create(xenvif_dealloc_kthread,
501 (void *)vif, "%s-dealloc", vif->dev->name);
503 pr_warn("Could not allocate kthread for %s\n", vif->dev->name);
508 vif->dealloc_task = task;
511 if (!vif->can_sg && vif->dev->mtu > ETH_DATA_LEN)
512 dev_set_mtu(vif->dev, ETH_DATA_LEN);
513 netdev_update_features(vif->dev);
514 netif_carrier_on(vif->dev);
515 if (netif_running(vif->dev))
519 wake_up_process(vif->task);
520 wake_up_process(vif->dealloc_task);
525 unbind_from_irqhandler(vif->rx_irq, vif);
528 unbind_from_irqhandler(vif->tx_irq, vif);
531 xenvif_unmap_frontend_rings(vif);
533 module_put(THIS_MODULE);
537 void xenvif_carrier_off(struct xenvif *vif)
539 struct net_device *dev = vif->dev;
542 netif_carrier_off(dev); /* discard queued packets */
543 if (netif_running(dev))
548 void xenvif_disconnect(struct xenvif *vif)
550 if (netif_carrier_ok(vif->dev))
551 xenvif_carrier_off(vif);
554 del_timer_sync(&vif->wake_queue);
555 kthread_stop(vif->task);
559 if (vif->dealloc_task) {
560 del_timer_sync(&vif->dealloc_delay);
561 kthread_stop(vif->dealloc_task);
562 vif->dealloc_task = NULL;
566 if (vif->tx_irq == vif->rx_irq)
567 unbind_from_irqhandler(vif->tx_irq, vif);
569 unbind_from_irqhandler(vif->tx_irq, vif);
570 unbind_from_irqhandler(vif->rx_irq, vif);
575 xenvif_unmap_frontend_rings(vif);
578 void xenvif_free(struct xenvif *vif)
580 int i, unmap_timeout = 0;
581 /* Here we want to avoid timeout messages if an skb can be legitimatly
582 * stucked somewhere else. Realisticly this could be an another vif's
583 * internal or QDisc queue. That another vif also has this
584 * rx_drain_timeout_msecs timeout, but the timer only ditches the
585 * internal queue. After that, the QDisc queue can put in worst case
586 * XEN_NETIF_RX_RING_SIZE / MAX_SKB_FRAGS skbs into that another vif's
587 * internal queue, so we need several rounds of such timeouts until we
588 * can be sure that no another vif should have skb's from us. We are
589 * not sending more skb's, so newly stucked packets are not interesting
592 unsigned int worst_case_skb_lifetime = (rx_drain_timeout_msecs/1000) *
593 DIV_ROUND_UP(XENVIF_QUEUE_LENGTH, (XEN_NETIF_RX_RING_SIZE / MAX_SKB_FRAGS));
595 for (i = 0; i < MAX_PENDING_REQS; ++i) {
596 if (vif->grant_tx_handle[i] != NETBACK_INVALID_HANDLE) {
598 schedule_timeout(msecs_to_jiffies(1000));
599 if (unmap_timeout > worst_case_skb_lifetime &&
602 "Page still granted! Index: %x\n",
608 free_xenballooned_pages(MAX_PENDING_REQS, vif->mmap_pages);
610 netif_napi_del(&vif->napi);
612 unregister_netdev(vif->dev);
614 vfree(vif->grant_copy_op);
615 free_netdev(vif->dev);
617 module_put(THIS_MODULE);