2 * Xenbus code for netif backend
4 * Copyright (C) 2005 Rusty Russell <rusty@rustcorp.com.au>
5 * Copyright (C) 2005 XenSource Ltd
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, see <http://www.gnu.org/licenses/>.
22 #include <linux/vmalloc.h>
23 #include <linux/rtnetlink.h>
26 struct xenbus_device *dev;
29 /* This is the state that will be reflected in xenstore when any
30 * active hotplug script completes.
32 enum xenbus_state state;
34 enum xenbus_state frontend_state;
35 struct xenbus_watch hotplug_status_watch;
36 u8 have_hotplug_status_watch:1;
39 static int connect_rings(struct backend_info *be, struct xenvif_queue *queue);
40 static void connect(struct backend_info *be);
41 static int read_xenbus_vif_flags(struct backend_info *be);
42 static int backend_create_xenvif(struct backend_info *be);
43 static void unregister_hotplug_status_watch(struct backend_info *be);
44 static void set_backend_state(struct backend_info *be,
45 enum xenbus_state state);
47 #ifdef CONFIG_DEBUG_FS
48 struct dentry *xen_netback_dbg_root = NULL;
50 static int xenvif_read_io_ring(struct seq_file *m, void *v)
52 struct xenvif_queue *queue = m->private;
53 struct xen_netif_tx_back_ring *tx_ring = &queue->tx;
54 struct xen_netif_rx_back_ring *rx_ring = &queue->rx;
55 struct netdev_queue *dev_queue;
58 struct xen_netif_tx_sring *sring = tx_ring->sring;
60 seq_printf(m, "Queue %d\nTX: nr_ents %u\n", queue->id,
62 seq_printf(m, "req prod %u (%d) cons %u (%d) event %u (%d)\n",
64 sring->req_prod - sring->rsp_prod,
66 tx_ring->req_cons - sring->rsp_prod,
68 sring->req_event - sring->rsp_prod);
69 seq_printf(m, "rsp prod %u (base) pvt %u (%d) event %u (%d)\n",
71 tx_ring->rsp_prod_pvt,
72 tx_ring->rsp_prod_pvt - sring->rsp_prod,
74 sring->rsp_event - sring->rsp_prod);
75 seq_printf(m, "pending prod %u pending cons %u nr_pending_reqs %u\n",
78 nr_pending_reqs(queue));
79 seq_printf(m, "dealloc prod %u dealloc cons %u dealloc_queue %u\n\n",
82 queue->dealloc_prod - queue->dealloc_cons);
86 struct xen_netif_rx_sring *sring = rx_ring->sring;
88 seq_printf(m, "RX: nr_ents %u\n", rx_ring->nr_ents);
89 seq_printf(m, "req prod %u (%d) cons %u (%d) event %u (%d)\n",
91 sring->req_prod - sring->rsp_prod,
93 rx_ring->req_cons - sring->rsp_prod,
95 sring->req_event - sring->rsp_prod);
96 seq_printf(m, "rsp prod %u (base) pvt %u (%d) event %u (%d)\n\n",
98 rx_ring->rsp_prod_pvt,
99 rx_ring->rsp_prod_pvt - sring->rsp_prod,
101 sring->rsp_event - sring->rsp_prod);
104 seq_printf(m, "NAPI state: %lx NAPI weight: %d TX queue len %u\n"
105 "Credit timer_pending: %d, credit: %lu, usec: %lu\n"
106 "remaining: %lu, expires: %lu, now: %lu\n",
107 queue->napi.state, queue->napi.weight,
108 skb_queue_len(&queue->tx_queue),
109 timer_pending(&queue->credit_timeout),
112 queue->remaining_credit,
113 queue->credit_timeout.expires,
116 dev_queue = netdev_get_tx_queue(queue->vif->dev, queue->id);
118 seq_printf(m, "\nRx internal queue: len %u max %u pkts %u %s\n",
119 queue->rx_queue_len, queue->rx_queue_max,
120 skb_queue_len(&queue->rx_queue),
121 netif_tx_queue_stopped(dev_queue) ? "stopped" : "running");
126 #define XENVIF_KICK_STR "kick"
127 #define BUFFER_SIZE 32
130 xenvif_write_io_ring(struct file *filp, const char __user *buf, size_t count,
133 struct xenvif_queue *queue =
134 ((struct seq_file *)filp->private_data)->private;
136 char write[BUFFER_SIZE];
138 /* don't allow partial writes and check the length */
141 if (count >= sizeof(write))
144 len = simple_write_to_buffer(write,
154 if (!strncmp(write, XENVIF_KICK_STR, sizeof(XENVIF_KICK_STR) - 1))
155 xenvif_interrupt(0, (void *)queue);
157 pr_warn("Unknown command to io_ring_q%d. Available: kick\n",
164 static int xenvif_dump_open(struct inode *inode, struct file *filp)
169 if (inode->i_private)
170 queue = inode->i_private;
171 ret = single_open(filp, xenvif_read_io_ring, queue);
172 filp->f_mode |= FMODE_PWRITE;
176 static const struct file_operations xenvif_dbg_io_ring_ops_fops = {
177 .owner = THIS_MODULE,
178 .open = xenvif_dump_open,
181 .release = single_release,
182 .write = xenvif_write_io_ring,
185 static void xenvif_debugfs_addif(struct xenvif *vif)
187 struct dentry *pfile;
190 if (IS_ERR_OR_NULL(xen_netback_dbg_root))
193 vif->xenvif_dbg_root = debugfs_create_dir(vif->dev->name,
194 xen_netback_dbg_root);
195 if (!IS_ERR_OR_NULL(vif->xenvif_dbg_root)) {
196 for (i = 0; i < vif->num_queues; ++i) {
197 char filename[sizeof("io_ring_q") + 4];
199 snprintf(filename, sizeof(filename), "io_ring_q%d", i);
200 pfile = debugfs_create_file(filename,
202 vif->xenvif_dbg_root,
204 &xenvif_dbg_io_ring_ops_fops);
205 if (IS_ERR_OR_NULL(pfile))
206 pr_warn("Creation of io_ring file returned %ld!\n",
210 netdev_warn(vif->dev,
211 "Creation of vif debugfs dir returned %ld!\n",
212 PTR_ERR(vif->xenvif_dbg_root));
215 static void xenvif_debugfs_delif(struct xenvif *vif)
217 if (IS_ERR_OR_NULL(xen_netback_dbg_root))
220 if (!IS_ERR_OR_NULL(vif->xenvif_dbg_root))
221 debugfs_remove_recursive(vif->xenvif_dbg_root);
222 vif->xenvif_dbg_root = NULL;
224 #endif /* CONFIG_DEBUG_FS */
226 static int netback_remove(struct xenbus_device *dev)
228 struct backend_info *be = dev_get_drvdata(&dev->dev);
230 set_backend_state(be, XenbusStateClosed);
232 unregister_hotplug_status_watch(be);
234 kobject_uevent(&dev->dev.kobj, KOBJ_OFFLINE);
235 xenbus_rm(XBT_NIL, dev->nodename, "hotplug-status");
236 xenvif_free(be->vif);
240 dev_set_drvdata(&dev->dev, NULL);
246 * Entry point to this code when a new device is created. Allocate the basic
247 * structures and switch to InitWait.
249 static int netback_probe(struct xenbus_device *dev,
250 const struct xenbus_device_id *id)
253 struct xenbus_transaction xbt;
256 struct backend_info *be = kzalloc(sizeof(struct backend_info),
259 xenbus_dev_fatal(dev, -ENOMEM,
260 "allocating backend structure");
265 dev_set_drvdata(&dev->dev, be);
270 err = xenbus_transaction_start(&xbt);
272 xenbus_dev_fatal(dev, err, "starting transaction");
276 err = xenbus_printf(xbt, dev->nodename, "feature-sg", "%d", sg);
278 message = "writing feature-sg";
279 goto abort_transaction;
282 err = xenbus_printf(xbt, dev->nodename, "feature-gso-tcpv4",
285 message = "writing feature-gso-tcpv4";
286 goto abort_transaction;
289 err = xenbus_printf(xbt, dev->nodename, "feature-gso-tcpv6",
292 message = "writing feature-gso-tcpv6";
293 goto abort_transaction;
296 /* We support partial checksum setup for IPv6 packets */
297 err = xenbus_printf(xbt, dev->nodename,
298 "feature-ipv6-csum-offload",
301 message = "writing feature-ipv6-csum-offload";
302 goto abort_transaction;
305 /* We support rx-copy path. */
306 err = xenbus_printf(xbt, dev->nodename,
307 "feature-rx-copy", "%d", 1);
309 message = "writing feature-rx-copy";
310 goto abort_transaction;
314 * We don't support rx-flip path (except old guests who don't
315 * grok this feature flag).
317 err = xenbus_printf(xbt, dev->nodename,
318 "feature-rx-flip", "%d", 0);
320 message = "writing feature-rx-flip";
321 goto abort_transaction;
324 err = xenbus_transaction_end(xbt, 0);
325 } while (err == -EAGAIN);
328 xenbus_dev_fatal(dev, err, "completing transaction");
333 * Split event channels support, this is optional so it is not
334 * put inside the above loop.
336 err = xenbus_printf(XBT_NIL, dev->nodename,
337 "feature-split-event-channels",
338 "%u", separate_tx_rx_irq);
340 pr_debug("Error writing feature-split-event-channels\n");
342 /* Multi-queue support: This is an optional feature. */
343 err = xenbus_printf(XBT_NIL, dev->nodename,
344 "multi-queue-max-queues", "%u", xenvif_max_queues);
346 pr_debug("Error writing multi-queue-max-queues\n");
348 err = xenbus_switch_state(dev, XenbusStateInitWait);
352 be->state = XenbusStateInitWait;
354 /* This kicks hotplug scripts, so do it immediately. */
355 err = backend_create_xenvif(be);
362 xenbus_transaction_end(xbt, 1);
363 xenbus_dev_fatal(dev, err, "%s", message);
365 pr_debug("failed\n");
372 * Handle the creation of the hotplug script environment. We add the script
373 * and vif variables to the environment, for the benefit of the vif-* hotplug
376 static int netback_uevent(struct xenbus_device *xdev,
377 struct kobj_uevent_env *env)
379 struct backend_info *be = dev_get_drvdata(&xdev->dev);
382 val = xenbus_read(XBT_NIL, xdev->nodename, "script", NULL);
384 int err = PTR_ERR(val);
385 xenbus_dev_fatal(xdev, err, "reading script");
388 if (add_uevent_var(env, "script=%s", val)) {
398 return add_uevent_var(env, "vif=%s", be->vif->dev->name);
402 static int backend_create_xenvif(struct backend_info *be)
406 struct xenbus_device *dev = be->dev;
411 err = xenbus_scanf(XBT_NIL, dev->nodename, "handle", "%li", &handle);
413 xenbus_dev_fatal(dev, err, "reading handle");
414 return (err < 0) ? err : -EINVAL;
417 be->vif = xenvif_alloc(&dev->dev, dev->otherend_id, handle);
418 if (IS_ERR(be->vif)) {
419 err = PTR_ERR(be->vif);
421 xenbus_dev_fatal(dev, err, "creating interface");
425 kobject_uevent(&dev->dev.kobj, KOBJ_ONLINE);
429 static void backend_disconnect(struct backend_info *be)
432 #ifdef CONFIG_DEBUG_FS
433 xenvif_debugfs_delif(be->vif);
434 #endif /* CONFIG_DEBUG_FS */
435 xenvif_disconnect(be->vif);
439 static void backend_connect(struct backend_info *be)
445 static inline void backend_switch_state(struct backend_info *be,
446 enum xenbus_state state)
448 struct xenbus_device *dev = be->dev;
450 pr_debug("%s -> %s\n", dev->nodename, xenbus_strstate(state));
453 /* If we are waiting for a hotplug script then defer the
454 * actual xenbus state change.
456 if (!be->have_hotplug_status_watch)
457 xenbus_switch_state(dev, state);
460 /* Handle backend state transitions:
462 * The backend state starts in InitWait and the following transitions are
465 * InitWait -> Connected
477 * The state argument specifies the eventual state of the backend and the
478 * function transitions to that state via the shortest path.
480 static void set_backend_state(struct backend_info *be,
481 enum xenbus_state state)
483 while (be->state != state) {
485 case XenbusStateClosed:
487 case XenbusStateInitWait:
488 case XenbusStateConnected:
489 pr_info("%s: prepare for reconnect\n",
491 backend_switch_state(be, XenbusStateInitWait);
493 case XenbusStateClosing:
494 backend_switch_state(be, XenbusStateClosing);
500 case XenbusStateInitWait:
502 case XenbusStateConnected:
504 backend_switch_state(be, XenbusStateConnected);
506 case XenbusStateClosing:
507 case XenbusStateClosed:
508 backend_switch_state(be, XenbusStateClosing);
514 case XenbusStateConnected:
516 case XenbusStateInitWait:
517 case XenbusStateClosing:
518 case XenbusStateClosed:
519 backend_disconnect(be);
520 backend_switch_state(be, XenbusStateClosing);
526 case XenbusStateClosing:
528 case XenbusStateInitWait:
529 case XenbusStateConnected:
530 case XenbusStateClosed:
531 backend_switch_state(be, XenbusStateClosed);
544 * Callback received when the frontend's state changes.
546 static void frontend_changed(struct xenbus_device *dev,
547 enum xenbus_state frontend_state)
549 struct backend_info *be = dev_get_drvdata(&dev->dev);
551 pr_debug("%s -> %s\n", dev->otherend, xenbus_strstate(frontend_state));
553 be->frontend_state = frontend_state;
555 switch (frontend_state) {
556 case XenbusStateInitialising:
557 set_backend_state(be, XenbusStateInitWait);
560 case XenbusStateInitialised:
563 case XenbusStateConnected:
564 set_backend_state(be, XenbusStateConnected);
567 case XenbusStateClosing:
568 set_backend_state(be, XenbusStateClosing);
571 case XenbusStateClosed:
572 set_backend_state(be, XenbusStateClosed);
573 if (xenbus_dev_is_online(dev))
575 /* fall through if not online */
576 case XenbusStateUnknown:
577 set_backend_state(be, XenbusStateClosed);
578 device_unregister(&dev->dev);
582 xenbus_dev_fatal(dev, -EINVAL, "saw state %d at frontend",
589 static void xen_net_read_rate(struct xenbus_device *dev,
590 unsigned long *bytes, unsigned long *usec)
596 /* Default to unlimited bandwidth. */
600 ratestr = xenbus_read(XBT_NIL, dev->nodename, "rate", NULL);
605 b = simple_strtoul(s, &e, 10);
606 if ((s == e) || (*e != ','))
610 u = simple_strtoul(s, &e, 10);
611 if ((s == e) || (*e != '\0'))
621 pr_warn("Failed to parse network rate limit. Traffic unlimited.\n");
625 static int xen_net_read_mac(struct xenbus_device *dev, u8 mac[])
627 char *s, *e, *macstr;
630 macstr = s = xenbus_read(XBT_NIL, dev->nodename, "mac", NULL);
632 return PTR_ERR(macstr);
634 for (i = 0; i < ETH_ALEN; i++) {
635 mac[i] = simple_strtoul(s, &e, 16);
636 if ((s == e) || (*e != ((i == ETH_ALEN-1) ? '\0' : ':'))) {
647 static void unregister_hotplug_status_watch(struct backend_info *be)
649 if (be->have_hotplug_status_watch) {
650 unregister_xenbus_watch(&be->hotplug_status_watch);
651 kfree(be->hotplug_status_watch.node);
653 be->have_hotplug_status_watch = 0;
656 static void hotplug_status_changed(struct xenbus_watch *watch,
658 unsigned int vec_size)
660 struct backend_info *be = container_of(watch,
662 hotplug_status_watch);
666 str = xenbus_read(XBT_NIL, be->dev->nodename, "hotplug-status", &len);
669 if (len == sizeof("connected")-1 && !memcmp(str, "connected", len)) {
670 /* Complete any pending state change */
671 xenbus_switch_state(be->dev, be->state);
673 /* Not interested in this watch anymore. */
674 unregister_hotplug_status_watch(be);
679 static void connect(struct backend_info *be)
682 struct xenbus_device *dev = be->dev;
683 unsigned long credit_bytes, credit_usec;
684 unsigned int queue_index;
685 unsigned int requested_num_queues;
686 struct xenvif_queue *queue;
688 /* Check whether the frontend requested multiple queues
689 * and read the number requested.
691 err = xenbus_scanf(XBT_NIL, dev->otherend,
692 "multi-queue-num-queues",
693 "%u", &requested_num_queues);
695 requested_num_queues = 1; /* Fall back to single queue */
696 } else if (requested_num_queues > xenvif_max_queues) {
697 /* buggy or malicious guest */
698 xenbus_dev_fatal(dev, err,
699 "guest requested %u queues, exceeding the maximum of %u.",
700 requested_num_queues, xenvif_max_queues);
704 err = xen_net_read_mac(dev, be->vif->fe_dev_addr);
706 xenbus_dev_fatal(dev, err, "parsing %s/mac", dev->nodename);
710 xen_net_read_rate(dev, &credit_bytes, &credit_usec);
711 read_xenbus_vif_flags(be);
713 /* Use the number of queues requested by the frontend */
714 be->vif->queues = vzalloc(requested_num_queues *
715 sizeof(struct xenvif_queue));
716 be->vif->num_queues = requested_num_queues;
717 be->vif->stalled_queues = requested_num_queues;
719 for (queue_index = 0; queue_index < requested_num_queues; ++queue_index) {
720 queue = &be->vif->queues[queue_index];
721 queue->vif = be->vif;
722 queue->id = queue_index;
723 snprintf(queue->name, sizeof(queue->name), "%s-q%u",
724 be->vif->dev->name, queue->id);
726 err = xenvif_init_queue(queue);
728 /* xenvif_init_queue() cleans up after itself on
729 * failure, but we need to clean up any previously
730 * initialised queues. Set num_queues to i so that
731 * earlier queues can be destroyed using the regular
734 be->vif->num_queues = queue_index;
738 queue->remaining_credit = credit_bytes;
740 err = connect_rings(be, queue);
742 /* connect_rings() cleans up after itself on failure,
743 * but we need to clean up after xenvif_init_queue() here,
744 * and also clean up any previously initialised queues.
746 xenvif_deinit_queue(queue);
747 be->vif->num_queues = queue_index;
752 #ifdef CONFIG_DEBUG_FS
753 xenvif_debugfs_addif(be->vif);
754 #endif /* CONFIG_DEBUG_FS */
756 /* Initialisation completed, tell core driver the number of
760 netif_set_real_num_tx_queues(be->vif->dev, requested_num_queues);
761 netif_set_real_num_rx_queues(be->vif->dev, requested_num_queues);
764 xenvif_carrier_on(be->vif);
766 unregister_hotplug_status_watch(be);
767 err = xenbus_watch_pathfmt(dev, &be->hotplug_status_watch,
768 hotplug_status_changed,
769 "%s/%s", dev->nodename, "hotplug-status");
771 be->have_hotplug_status_watch = 1;
773 netif_tx_wake_all_queues(be->vif->dev);
778 if (be->vif->num_queues > 0)
779 xenvif_disconnect(be->vif); /* Clean up existing queues */
780 vfree(be->vif->queues);
781 be->vif->queues = NULL;
782 be->vif->num_queues = 0;
787 static int connect_rings(struct backend_info *be, struct xenvif_queue *queue)
789 struct xenbus_device *dev = be->dev;
790 unsigned int num_queues = queue->vif->num_queues;
791 unsigned long tx_ring_ref, rx_ring_ref;
792 unsigned int tx_evtchn, rx_evtchn;
796 const size_t xenstore_path_ext_size = 11; /* sufficient for "/queue-NNN" */
798 /* If the frontend requested 1 queue, or we have fallen back
799 * to single queue due to lack of frontend support for multi-
800 * queue, expect the remaining XenStore keys in the toplevel
801 * directory. Otherwise, expect them in a subdirectory called
804 if (num_queues == 1) {
805 xspath = kzalloc(strlen(dev->otherend) + 1, GFP_KERNEL);
807 xenbus_dev_fatal(dev, -ENOMEM,
808 "reading ring references");
811 strcpy(xspath, dev->otherend);
813 xspathsize = strlen(dev->otherend) + xenstore_path_ext_size;
814 xspath = kzalloc(xspathsize, GFP_KERNEL);
816 xenbus_dev_fatal(dev, -ENOMEM,
817 "reading ring references");
820 snprintf(xspath, xspathsize, "%s/queue-%u", dev->otherend,
824 err = xenbus_gather(XBT_NIL, xspath,
825 "tx-ring-ref", "%lu", &tx_ring_ref,
826 "rx-ring-ref", "%lu", &rx_ring_ref, NULL);
828 xenbus_dev_fatal(dev, err,
829 "reading %s/ring-ref",
834 /* Try split event channels first, then single event channel. */
835 err = xenbus_gather(XBT_NIL, xspath,
836 "event-channel-tx", "%u", &tx_evtchn,
837 "event-channel-rx", "%u", &rx_evtchn, NULL);
839 err = xenbus_scanf(XBT_NIL, xspath,
840 "event-channel", "%u", &tx_evtchn);
842 xenbus_dev_fatal(dev, err,
843 "reading %s/event-channel(-tx/rx)",
847 rx_evtchn = tx_evtchn;
850 /* Map the shared frame, irq etc. */
851 err = xenvif_connect(queue, tx_ring_ref, rx_ring_ref,
852 tx_evtchn, rx_evtchn);
854 xenbus_dev_fatal(dev, err,
855 "mapping shared-frames %lu/%lu port tx %u rx %u",
856 tx_ring_ref, rx_ring_ref,
857 tx_evtchn, rx_evtchn);
862 err: /* Regular return falls through with err == 0 */
867 static int read_xenbus_vif_flags(struct backend_info *be)
869 struct xenvif *vif = be->vif;
870 struct xenbus_device *dev = be->dev;
871 unsigned int rx_copy;
874 err = xenbus_scanf(XBT_NIL, dev->otherend, "request-rx-copy", "%u",
876 if (err == -ENOENT) {
881 xenbus_dev_fatal(dev, err, "reading %s/request-rx-copy",
888 if (xenbus_scanf(XBT_NIL, dev->otherend,
889 "feature-rx-notify", "%d", &val) < 0 || val == 0) {
890 xenbus_dev_fatal(dev, -EINVAL, "feature-rx-notify is mandatory");
894 if (xenbus_scanf(XBT_NIL, dev->otherend, "feature-sg",
900 vif->gso_prefix_mask = 0;
902 if (xenbus_scanf(XBT_NIL, dev->otherend, "feature-gso-tcpv4",
906 vif->gso_mask |= GSO_BIT(TCPV4);
908 if (xenbus_scanf(XBT_NIL, dev->otherend, "feature-gso-tcpv4-prefix",
912 vif->gso_prefix_mask |= GSO_BIT(TCPV4);
914 if (xenbus_scanf(XBT_NIL, dev->otherend, "feature-gso-tcpv6",
918 vif->gso_mask |= GSO_BIT(TCPV6);
920 if (xenbus_scanf(XBT_NIL, dev->otherend, "feature-gso-tcpv6-prefix",
924 vif->gso_prefix_mask |= GSO_BIT(TCPV6);
926 if (vif->gso_mask & vif->gso_prefix_mask) {
927 xenbus_dev_fatal(dev, err,
928 "%s: gso and gso prefix flags are not "
929 "mutually exclusive",
934 if (xenbus_scanf(XBT_NIL, dev->otherend, "feature-no-csum-offload",
939 if (xenbus_scanf(XBT_NIL, dev->otherend, "feature-ipv6-csum-offload",
942 vif->ipv6_csum = !!val;
947 static const struct xenbus_device_id netback_ids[] = {
952 static struct xenbus_driver netback_driver = {
954 .probe = netback_probe,
955 .remove = netback_remove,
956 .uevent = netback_uevent,
957 .otherend_changed = frontend_changed,
960 int xenvif_xenbus_init(void)
962 return xenbus_register_backend(&netback_driver);
965 void xenvif_xenbus_fini(void)
967 return xenbus_unregister_driver(&netback_driver);