2 * Xenbus code for netif backend
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 void 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;
57 struct xen_netif_tx_sring *sring = tx_ring->sring;
59 seq_printf(m, "Queue %d\nTX: nr_ents %u\n", queue->id,
61 seq_printf(m, "req prod %u (%d) cons %u (%d) event %u (%d)\n",
63 sring->req_prod - sring->rsp_prod,
65 tx_ring->req_cons - sring->rsp_prod,
67 sring->req_event - sring->rsp_prod);
68 seq_printf(m, "rsp prod %u (base) pvt %u (%d) event %u (%d)\n",
70 tx_ring->rsp_prod_pvt,
71 tx_ring->rsp_prod_pvt - sring->rsp_prod,
73 sring->rsp_event - sring->rsp_prod);
74 seq_printf(m, "pending prod %u pending cons %u nr_pending_reqs %u\n",
77 nr_pending_reqs(queue));
78 seq_printf(m, "dealloc prod %u dealloc cons %u dealloc_queue %u\n\n",
81 queue->dealloc_prod - queue->dealloc_cons);
85 struct xen_netif_rx_sring *sring = rx_ring->sring;
87 seq_printf(m, "RX: nr_ents %u\n", rx_ring->nr_ents);
88 seq_printf(m, "req prod %u (%d) cons %u (%d) event %u (%d)\n",
90 sring->req_prod - sring->rsp_prod,
92 rx_ring->req_cons - sring->rsp_prod,
94 sring->req_event - sring->rsp_prod);
95 seq_printf(m, "rsp prod %u (base) pvt %u (%d) event %u (%d)\n\n",
97 rx_ring->rsp_prod_pvt,
98 rx_ring->rsp_prod_pvt - sring->rsp_prod,
100 sring->rsp_event - sring->rsp_prod);
103 seq_printf(m, "NAPI state: %lx NAPI weight: %d TX queue len %u\n"
104 "Credit timer_pending: %d, credit: %lu, usec: %lu\n"
105 "remaining: %lu, expires: %lu, now: %lu\n",
106 queue->napi.state, queue->napi.weight,
107 skb_queue_len(&queue->tx_queue),
108 timer_pending(&queue->credit_timeout),
111 queue->remaining_credit,
112 queue->credit_timeout.expires,
118 #define XENVIF_KICK_STR "kick"
119 #define BUFFER_SIZE 32
122 xenvif_write_io_ring(struct file *filp, const char __user *buf, size_t count,
125 struct xenvif_queue *queue =
126 ((struct seq_file *)filp->private_data)->private;
128 char write[BUFFER_SIZE];
130 /* don't allow partial writes and check the length */
133 if (count >= sizeof(write))
136 len = simple_write_to_buffer(write,
146 if (!strncmp(write, XENVIF_KICK_STR, sizeof(XENVIF_KICK_STR) - 1))
147 xenvif_interrupt(0, (void *)queue);
149 pr_warn("Unknown command to io_ring_q%d. Available: kick\n",
156 static int xenvif_dump_open(struct inode *inode, struct file *filp)
161 if (inode->i_private)
162 queue = inode->i_private;
163 ret = single_open(filp, xenvif_read_io_ring, queue);
164 filp->f_mode |= FMODE_PWRITE;
168 static const struct file_operations xenvif_dbg_io_ring_ops_fops = {
169 .owner = THIS_MODULE,
170 .open = xenvif_dump_open,
173 .release = single_release,
174 .write = xenvif_write_io_ring,
177 static void xenvif_debugfs_addif(struct xenvif *vif)
179 struct dentry *pfile;
182 if (IS_ERR_OR_NULL(xen_netback_dbg_root))
185 vif->xenvif_dbg_root = debugfs_create_dir(vif->dev->name,
186 xen_netback_dbg_root);
187 if (!IS_ERR_OR_NULL(vif->xenvif_dbg_root)) {
188 for (i = 0; i < vif->num_queues; ++i) {
189 char filename[sizeof("io_ring_q") + 4];
191 snprintf(filename, sizeof(filename), "io_ring_q%d", i);
192 pfile = debugfs_create_file(filename,
194 vif->xenvif_dbg_root,
196 &xenvif_dbg_io_ring_ops_fops);
197 if (IS_ERR_OR_NULL(pfile))
198 pr_warn("Creation of io_ring file returned %ld!\n",
202 netdev_warn(vif->dev,
203 "Creation of vif debugfs dir returned %ld!\n",
204 PTR_ERR(vif->xenvif_dbg_root));
207 static void xenvif_debugfs_delif(struct xenvif *vif)
209 if (IS_ERR_OR_NULL(xen_netback_dbg_root))
212 if (!IS_ERR_OR_NULL(vif->xenvif_dbg_root))
213 debugfs_remove_recursive(vif->xenvif_dbg_root);
214 vif->xenvif_dbg_root = NULL;
216 #endif /* CONFIG_DEBUG_FS */
218 static int netback_remove(struct xenbus_device *dev)
220 struct backend_info *be = dev_get_drvdata(&dev->dev);
222 set_backend_state(be, XenbusStateClosed);
224 unregister_hotplug_status_watch(be);
226 kobject_uevent(&dev->dev.kobj, KOBJ_OFFLINE);
227 xenbus_rm(XBT_NIL, dev->nodename, "hotplug-status");
228 xenvif_free(be->vif);
232 dev_set_drvdata(&dev->dev, NULL);
238 * Entry point to this code when a new device is created. Allocate the basic
239 * structures and switch to InitWait.
241 static int netback_probe(struct xenbus_device *dev,
242 const struct xenbus_device_id *id)
245 struct xenbus_transaction xbt;
248 struct backend_info *be = kzalloc(sizeof(struct backend_info),
251 xenbus_dev_fatal(dev, -ENOMEM,
252 "allocating backend structure");
257 dev_set_drvdata(&dev->dev, be);
262 err = xenbus_transaction_start(&xbt);
264 xenbus_dev_fatal(dev, err, "starting transaction");
268 err = xenbus_printf(xbt, dev->nodename, "feature-sg", "%d", sg);
270 message = "writing feature-sg";
271 goto abort_transaction;
274 err = xenbus_printf(xbt, dev->nodename, "feature-gso-tcpv4",
277 message = "writing feature-gso-tcpv4";
278 goto abort_transaction;
281 err = xenbus_printf(xbt, dev->nodename, "feature-gso-tcpv6",
284 message = "writing feature-gso-tcpv6";
285 goto abort_transaction;
288 /* We support partial checksum setup for IPv6 packets */
289 err = xenbus_printf(xbt, dev->nodename,
290 "feature-ipv6-csum-offload",
293 message = "writing feature-ipv6-csum-offload";
294 goto abort_transaction;
297 /* We support rx-copy path. */
298 err = xenbus_printf(xbt, dev->nodename,
299 "feature-rx-copy", "%d", 1);
301 message = "writing feature-rx-copy";
302 goto abort_transaction;
306 * We don't support rx-flip path (except old guests who don't
307 * grok this feature flag).
309 err = xenbus_printf(xbt, dev->nodename,
310 "feature-rx-flip", "%d", 0);
312 message = "writing feature-rx-flip";
313 goto abort_transaction;
316 err = xenbus_transaction_end(xbt, 0);
317 } while (err == -EAGAIN);
320 xenbus_dev_fatal(dev, err, "completing transaction");
325 * Split event channels support, this is optional so it is not
326 * put inside the above loop.
328 err = xenbus_printf(XBT_NIL, dev->nodename,
329 "feature-split-event-channels",
330 "%u", separate_tx_rx_irq);
332 pr_debug("Error writing feature-split-event-channels\n");
334 /* Multi-queue support: This is an optional feature. */
335 err = xenbus_printf(XBT_NIL, dev->nodename,
336 "multi-queue-max-queues", "%u", xenvif_max_queues);
338 pr_debug("Error writing multi-queue-max-queues\n");
340 err = xenbus_switch_state(dev, XenbusStateInitWait);
344 be->state = XenbusStateInitWait;
346 /* This kicks hotplug scripts, so do it immediately. */
347 backend_create_xenvif(be);
352 xenbus_transaction_end(xbt, 1);
353 xenbus_dev_fatal(dev, err, "%s", message);
355 pr_debug("failed\n");
362 * Handle the creation of the hotplug script environment. We add the script
363 * and vif variables to the environment, for the benefit of the vif-* hotplug
366 static int netback_uevent(struct xenbus_device *xdev,
367 struct kobj_uevent_env *env)
369 struct backend_info *be = dev_get_drvdata(&xdev->dev);
372 val = xenbus_read(XBT_NIL, xdev->nodename, "script", NULL);
374 int err = PTR_ERR(val);
375 xenbus_dev_fatal(xdev, err, "reading script");
378 if (add_uevent_var(env, "script=%s", val)) {
388 return add_uevent_var(env, "vif=%s", be->vif->dev->name);
392 static void backend_create_xenvif(struct backend_info *be)
396 struct xenbus_device *dev = be->dev;
401 err = xenbus_scanf(XBT_NIL, dev->nodename, "handle", "%li", &handle);
403 xenbus_dev_fatal(dev, err, "reading handle");
407 be->vif = xenvif_alloc(&dev->dev, dev->otherend_id, handle);
408 if (IS_ERR(be->vif)) {
409 err = PTR_ERR(be->vif);
411 xenbus_dev_fatal(dev, err, "creating interface");
415 kobject_uevent(&dev->dev.kobj, KOBJ_ONLINE);
418 static void backend_disconnect(struct backend_info *be)
421 #ifdef CONFIG_DEBUG_FS
422 xenvif_debugfs_delif(be->vif);
423 #endif /* CONFIG_DEBUG_FS */
424 xenvif_disconnect(be->vif);
428 static void backend_connect(struct backend_info *be)
434 static inline void backend_switch_state(struct backend_info *be,
435 enum xenbus_state state)
437 struct xenbus_device *dev = be->dev;
439 pr_debug("%s -> %s\n", dev->nodename, xenbus_strstate(state));
442 /* If we are waiting for a hotplug script then defer the
443 * actual xenbus state change.
445 if (!be->have_hotplug_status_watch)
446 xenbus_switch_state(dev, state);
449 /* Handle backend state transitions:
451 * The backend state starts in InitWait and the following transitions are
454 * InitWait -> Connected
466 * The state argument specifies the eventual state of the backend and the
467 * function transitions to that state via the shortest path.
469 static void set_backend_state(struct backend_info *be,
470 enum xenbus_state state)
472 while (be->state != state) {
474 case XenbusStateClosed:
476 case XenbusStateInitWait:
477 case XenbusStateConnected:
478 pr_info("%s: prepare for reconnect\n",
480 backend_switch_state(be, XenbusStateInitWait);
482 case XenbusStateClosing:
483 backend_switch_state(be, XenbusStateClosing);
489 case XenbusStateInitWait:
491 case XenbusStateConnected:
493 backend_switch_state(be, XenbusStateConnected);
495 case XenbusStateClosing:
496 case XenbusStateClosed:
497 backend_switch_state(be, XenbusStateClosing);
503 case XenbusStateConnected:
505 case XenbusStateInitWait:
506 case XenbusStateClosing:
507 case XenbusStateClosed:
508 backend_disconnect(be);
509 backend_switch_state(be, XenbusStateClosing);
515 case XenbusStateClosing:
517 case XenbusStateInitWait:
518 case XenbusStateConnected:
519 case XenbusStateClosed:
520 backend_switch_state(be, XenbusStateClosed);
533 * Callback received when the frontend's state changes.
535 static void frontend_changed(struct xenbus_device *dev,
536 enum xenbus_state frontend_state)
538 struct backend_info *be = dev_get_drvdata(&dev->dev);
540 pr_debug("%s -> %s\n", dev->otherend, xenbus_strstate(frontend_state));
542 be->frontend_state = frontend_state;
544 switch (frontend_state) {
545 case XenbusStateInitialising:
546 set_backend_state(be, XenbusStateInitWait);
549 case XenbusStateInitialised:
552 case XenbusStateConnected:
553 set_backend_state(be, XenbusStateConnected);
556 case XenbusStateClosing:
557 set_backend_state(be, XenbusStateClosing);
560 case XenbusStateClosed:
561 set_backend_state(be, XenbusStateClosed);
562 if (xenbus_dev_is_online(dev))
564 /* fall through if not online */
565 case XenbusStateUnknown:
566 set_backend_state(be, XenbusStateClosed);
567 device_unregister(&dev->dev);
571 xenbus_dev_fatal(dev, -EINVAL, "saw state %d at frontend",
578 static void xen_net_read_rate(struct xenbus_device *dev,
579 unsigned long *bytes, unsigned long *usec)
585 /* Default to unlimited bandwidth. */
589 ratestr = xenbus_read(XBT_NIL, dev->nodename, "rate", NULL);
594 b = simple_strtoul(s, &e, 10);
595 if ((s == e) || (*e != ','))
599 u = simple_strtoul(s, &e, 10);
600 if ((s == e) || (*e != '\0'))
610 pr_warn("Failed to parse network rate limit. Traffic unlimited.\n");
614 static int xen_net_read_mac(struct xenbus_device *dev, u8 mac[])
616 char *s, *e, *macstr;
619 macstr = s = xenbus_read(XBT_NIL, dev->nodename, "mac", NULL);
621 return PTR_ERR(macstr);
623 for (i = 0; i < ETH_ALEN; i++) {
624 mac[i] = simple_strtoul(s, &e, 16);
625 if ((s == e) || (*e != ((i == ETH_ALEN-1) ? '\0' : ':'))) {
636 static void unregister_hotplug_status_watch(struct backend_info *be)
638 if (be->have_hotplug_status_watch) {
639 unregister_xenbus_watch(&be->hotplug_status_watch);
640 kfree(be->hotplug_status_watch.node);
642 be->have_hotplug_status_watch = 0;
645 static void hotplug_status_changed(struct xenbus_watch *watch,
647 unsigned int vec_size)
649 struct backend_info *be = container_of(watch,
651 hotplug_status_watch);
655 str = xenbus_read(XBT_NIL, be->dev->nodename, "hotplug-status", &len);
658 if (len == sizeof("connected")-1 && !memcmp(str, "connected", len)) {
659 /* Complete any pending state change */
660 xenbus_switch_state(be->dev, be->state);
662 /* Not interested in this watch anymore. */
663 unregister_hotplug_status_watch(be);
668 static void connect(struct backend_info *be)
671 struct xenbus_device *dev = be->dev;
672 unsigned long credit_bytes, credit_usec;
673 unsigned int queue_index;
674 unsigned int requested_num_queues;
675 struct xenvif_queue *queue;
677 /* Check whether the frontend requested multiple queues
678 * and read the number requested.
680 err = xenbus_scanf(XBT_NIL, dev->otherend,
681 "multi-queue-num-queues",
682 "%u", &requested_num_queues);
684 requested_num_queues = 1; /* Fall back to single queue */
685 } else if (requested_num_queues > xenvif_max_queues) {
686 /* buggy or malicious guest */
687 xenbus_dev_fatal(dev, err,
688 "guest requested %u queues, exceeding the maximum of %u.",
689 requested_num_queues, xenvif_max_queues);
693 err = xen_net_read_mac(dev, be->vif->fe_dev_addr);
695 xenbus_dev_fatal(dev, err, "parsing %s/mac", dev->nodename);
699 xen_net_read_rate(dev, &credit_bytes, &credit_usec);
700 read_xenbus_vif_flags(be);
702 /* Use the number of queues requested by the frontend */
703 be->vif->queues = vzalloc(requested_num_queues *
704 sizeof(struct xenvif_queue));
705 be->vif->num_queues = requested_num_queues;
707 for (queue_index = 0; queue_index < requested_num_queues; ++queue_index) {
708 queue = &be->vif->queues[queue_index];
709 queue->vif = be->vif;
710 queue->id = queue_index;
711 snprintf(queue->name, sizeof(queue->name), "%s-q%u",
712 be->vif->dev->name, queue->id);
714 err = xenvif_init_queue(queue);
716 /* xenvif_init_queue() cleans up after itself on
717 * failure, but we need to clean up any previously
718 * initialised queues. Set num_queues to i so that
719 * earlier queues can be destroyed using the regular
722 be->vif->num_queues = queue_index;
726 queue->remaining_credit = credit_bytes;
728 err = connect_rings(be, queue);
730 /* connect_rings() cleans up after itself on failure,
731 * but we need to clean up after xenvif_init_queue() here,
732 * and also clean up any previously initialised queues.
734 xenvif_deinit_queue(queue);
735 be->vif->num_queues = queue_index;
740 #ifdef CONFIG_DEBUG_FS
741 xenvif_debugfs_addif(be->vif);
742 #endif /* CONFIG_DEBUG_FS */
744 /* Initialisation completed, tell core driver the number of
748 netif_set_real_num_tx_queues(be->vif->dev, requested_num_queues);
749 netif_set_real_num_rx_queues(be->vif->dev, requested_num_queues);
752 xenvif_carrier_on(be->vif);
754 unregister_hotplug_status_watch(be);
755 err = xenbus_watch_pathfmt(dev, &be->hotplug_status_watch,
756 hotplug_status_changed,
757 "%s/%s", dev->nodename, "hotplug-status");
759 be->have_hotplug_status_watch = 1;
761 netif_tx_wake_all_queues(be->vif->dev);
766 if (be->vif->num_queues > 0)
767 xenvif_disconnect(be->vif); /* Clean up existing queues */
768 vfree(be->vif->queues);
769 be->vif->queues = NULL;
770 be->vif->num_queues = 0;
775 static int connect_rings(struct backend_info *be, struct xenvif_queue *queue)
777 struct xenbus_device *dev = be->dev;
778 unsigned int num_queues = queue->vif->num_queues;
779 unsigned long tx_ring_ref, rx_ring_ref;
780 unsigned int tx_evtchn, rx_evtchn;
784 const size_t xenstore_path_ext_size = 11; /* sufficient for "/queue-NNN" */
786 /* If the frontend requested 1 queue, or we have fallen back
787 * to single queue due to lack of frontend support for multi-
788 * queue, expect the remaining XenStore keys in the toplevel
789 * directory. Otherwise, expect them in a subdirectory called
792 if (num_queues == 1) {
793 xspath = kzalloc(strlen(dev->otherend) + 1, GFP_KERNEL);
795 xenbus_dev_fatal(dev, -ENOMEM,
796 "reading ring references");
799 strcpy(xspath, dev->otherend);
801 xspathsize = strlen(dev->otherend) + xenstore_path_ext_size;
802 xspath = kzalloc(xspathsize, GFP_KERNEL);
804 xenbus_dev_fatal(dev, -ENOMEM,
805 "reading ring references");
808 snprintf(xspath, xspathsize, "%s/queue-%u", dev->otherend,
812 err = xenbus_gather(XBT_NIL, xspath,
813 "tx-ring-ref", "%lu", &tx_ring_ref,
814 "rx-ring-ref", "%lu", &rx_ring_ref, NULL);
816 xenbus_dev_fatal(dev, err,
817 "reading %s/ring-ref",
822 /* Try split event channels first, then single event channel. */
823 err = xenbus_gather(XBT_NIL, xspath,
824 "event-channel-tx", "%u", &tx_evtchn,
825 "event-channel-rx", "%u", &rx_evtchn, NULL);
827 err = xenbus_scanf(XBT_NIL, xspath,
828 "event-channel", "%u", &tx_evtchn);
830 xenbus_dev_fatal(dev, err,
831 "reading %s/event-channel(-tx/rx)",
835 rx_evtchn = tx_evtchn;
838 /* Map the shared frame, irq etc. */
839 err = xenvif_connect(queue, tx_ring_ref, rx_ring_ref,
840 tx_evtchn, rx_evtchn);
842 xenbus_dev_fatal(dev, err,
843 "mapping shared-frames %lu/%lu port tx %u rx %u",
844 tx_ring_ref, rx_ring_ref,
845 tx_evtchn, rx_evtchn);
850 err: /* Regular return falls through with err == 0 */
855 static int read_xenbus_vif_flags(struct backend_info *be)
857 struct xenvif *vif = be->vif;
858 struct xenbus_device *dev = be->dev;
859 unsigned int rx_copy;
862 err = xenbus_scanf(XBT_NIL, dev->otherend, "request-rx-copy", "%u",
864 if (err == -ENOENT) {
869 xenbus_dev_fatal(dev, err, "reading %s/request-rx-copy",
876 if (vif->dev->tx_queue_len != 0) {
877 if (xenbus_scanf(XBT_NIL, dev->otherend,
878 "feature-rx-notify", "%d", &val) < 0)
883 /* Must be non-zero for pfifo_fast to work. */
884 vif->dev->tx_queue_len = 1;
887 if (xenbus_scanf(XBT_NIL, dev->otherend, "feature-sg",
893 vif->gso_prefix_mask = 0;
895 if (xenbus_scanf(XBT_NIL, dev->otherend, "feature-gso-tcpv4",
899 vif->gso_mask |= GSO_BIT(TCPV4);
901 if (xenbus_scanf(XBT_NIL, dev->otherend, "feature-gso-tcpv4-prefix",
905 vif->gso_prefix_mask |= GSO_BIT(TCPV4);
907 if (xenbus_scanf(XBT_NIL, dev->otherend, "feature-gso-tcpv6",
911 vif->gso_mask |= GSO_BIT(TCPV6);
913 if (xenbus_scanf(XBT_NIL, dev->otherend, "feature-gso-tcpv6-prefix",
917 vif->gso_prefix_mask |= GSO_BIT(TCPV6);
919 if (vif->gso_mask & vif->gso_prefix_mask) {
920 xenbus_dev_fatal(dev, err,
921 "%s: gso and gso prefix flags are not "
922 "mutually exclusive",
927 if (xenbus_scanf(XBT_NIL, dev->otherend, "feature-no-csum-offload",
932 if (xenbus_scanf(XBT_NIL, dev->otherend, "feature-ipv6-csum-offload",
935 vif->ipv6_csum = !!val;
940 static const struct xenbus_device_id netback_ids[] = {
945 static struct xenbus_driver netback_driver = {
947 .probe = netback_probe,
948 .remove = netback_remove,
949 .uevent = netback_uevent,
950 .otherend_changed = frontend_changed,
953 int xenvif_xenbus_init(void)
955 return xenbus_register_backend(&netback_driver);
958 void xenvif_xenbus_fini(void)
960 return xenbus_unregister_driver(&netback_driver);