1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Xenbus code for netif backend
6 * Copyright (C) 2005 XenSource Ltd
10 #include <linux/vmalloc.h>
11 #include <linux/rtnetlink.h>
13 static int connect_data_rings(struct backend_info *be,
14 struct xenvif_queue *queue);
15 static void connect(struct backend_info *be);
16 static int read_xenbus_vif_flags(struct backend_info *be);
17 static int backend_create_xenvif(struct backend_info *be);
18 static void unregister_hotplug_status_watch(struct backend_info *be);
19 static void xen_unregister_watchers(struct xenvif *vif);
20 static void set_backend_state(struct backend_info *be,
21 enum xenbus_state state);
23 #ifdef CONFIG_DEBUG_FS
24 struct dentry *xen_netback_dbg_root = NULL;
26 static int xenvif_read_io_ring(struct seq_file *m, void *v)
28 struct xenvif_queue *queue = m->private;
29 struct xen_netif_tx_back_ring *tx_ring = &queue->tx;
30 struct xen_netif_rx_back_ring *rx_ring = &queue->rx;
31 struct netdev_queue *dev_queue;
34 struct xen_netif_tx_sring *sring = tx_ring->sring;
36 seq_printf(m, "Queue %d\nTX: nr_ents %u\n", queue->id,
38 seq_printf(m, "req prod %u (%d) cons %u (%d) event %u (%d)\n",
40 sring->req_prod - sring->rsp_prod,
42 tx_ring->req_cons - sring->rsp_prod,
44 sring->req_event - sring->rsp_prod);
45 seq_printf(m, "rsp prod %u (base) pvt %u (%d) event %u (%d)\n",
47 tx_ring->rsp_prod_pvt,
48 tx_ring->rsp_prod_pvt - sring->rsp_prod,
50 sring->rsp_event - sring->rsp_prod);
51 seq_printf(m, "pending prod %u pending cons %u nr_pending_reqs %u\n",
54 nr_pending_reqs(queue));
55 seq_printf(m, "dealloc prod %u dealloc cons %u dealloc_queue %u\n\n",
58 queue->dealloc_prod - queue->dealloc_cons);
62 struct xen_netif_rx_sring *sring = rx_ring->sring;
64 seq_printf(m, "RX: nr_ents %u\n", rx_ring->nr_ents);
65 seq_printf(m, "req prod %u (%d) cons %u (%d) event %u (%d)\n",
67 sring->req_prod - sring->rsp_prod,
69 rx_ring->req_cons - sring->rsp_prod,
71 sring->req_event - sring->rsp_prod);
72 seq_printf(m, "rsp prod %u (base) pvt %u (%d) event %u (%d)\n\n",
74 rx_ring->rsp_prod_pvt,
75 rx_ring->rsp_prod_pvt - sring->rsp_prod,
77 sring->rsp_event - sring->rsp_prod);
80 seq_printf(m, "NAPI state: %lx NAPI weight: %d TX queue len %u\n"
81 "Credit timer_pending: %d, credit: %lu, usec: %lu\n"
82 "remaining: %lu, expires: %lu, now: %lu\n",
83 queue->napi.state, queue->napi.weight,
84 skb_queue_len(&queue->tx_queue),
85 timer_pending(&queue->credit_timeout),
88 queue->remaining_credit,
89 queue->credit_timeout.expires,
92 dev_queue = netdev_get_tx_queue(queue->vif->dev, queue->id);
94 seq_printf(m, "\nRx internal queue: len %u max %u pkts %u %s\n",
95 queue->rx_queue_len, queue->rx_queue_max,
96 skb_queue_len(&queue->rx_queue),
97 netif_tx_queue_stopped(dev_queue) ? "stopped" : "running");
102 #define XENVIF_KICK_STR "kick"
103 #define BUFFER_SIZE 32
106 xenvif_write_io_ring(struct file *filp, const char __user *buf, size_t count,
109 struct xenvif_queue *queue =
110 ((struct seq_file *)filp->private_data)->private;
112 char write[BUFFER_SIZE];
114 /* don't allow partial writes and check the length */
117 if (count >= sizeof(write))
120 len = simple_write_to_buffer(write,
130 if (!strncmp(write, XENVIF_KICK_STR, sizeof(XENVIF_KICK_STR) - 1))
131 xenvif_interrupt(0, (void *)queue);
133 pr_warn("Unknown command to io_ring_q%d. Available: kick\n",
140 static int xenvif_io_ring_open(struct inode *inode, struct file *filp)
145 if (inode->i_private)
146 queue = inode->i_private;
147 ret = single_open(filp, xenvif_read_io_ring, queue);
148 filp->f_mode |= FMODE_PWRITE;
152 static const struct file_operations xenvif_dbg_io_ring_ops_fops = {
153 .owner = THIS_MODULE,
154 .open = xenvif_io_ring_open,
157 .release = single_release,
158 .write = xenvif_write_io_ring,
161 static int xenvif_ctrl_show(struct seq_file *m, void *v)
163 struct xenvif *vif = m->private;
165 xenvif_dump_hash_info(vif, m);
169 DEFINE_SHOW_ATTRIBUTE(xenvif_ctrl);
171 static void xenvif_debugfs_addif(struct xenvif *vif)
175 vif->xenvif_dbg_root = debugfs_create_dir(vif->dev->name,
176 xen_netback_dbg_root);
177 for (i = 0; i < vif->num_queues; ++i) {
178 char filename[sizeof("io_ring_q") + 4];
180 snprintf(filename, sizeof(filename), "io_ring_q%d", i);
181 debugfs_create_file(filename, 0600, vif->xenvif_dbg_root,
183 &xenvif_dbg_io_ring_ops_fops);
187 debugfs_create_file("ctrl", 0400, vif->xenvif_dbg_root, vif,
191 static void xenvif_debugfs_delif(struct xenvif *vif)
193 debugfs_remove_recursive(vif->xenvif_dbg_root);
194 vif->xenvif_dbg_root = NULL;
196 #endif /* CONFIG_DEBUG_FS */
199 * Handle the creation of the hotplug script environment. We add the script
200 * and vif variables to the environment, for the benefit of the vif-* hotplug
203 static int netback_uevent(struct xenbus_device *xdev,
204 struct kobj_uevent_env *env)
206 struct backend_info *be = dev_get_drvdata(&xdev->dev);
211 if (add_uevent_var(env, "script=%s", be->hotplug_script))
217 return add_uevent_var(env, "vif=%s", be->vif->dev->name);
221 static int backend_create_xenvif(struct backend_info *be)
225 struct xenbus_device *dev = be->dev;
231 err = xenbus_scanf(XBT_NIL, dev->nodename, "handle", "%li", &handle);
233 xenbus_dev_fatal(dev, err, "reading handle");
234 return (err < 0) ? err : -EINVAL;
237 vif = xenvif_alloc(&dev->dev, dev->otherend_id, handle);
240 xenbus_dev_fatal(dev, err, "creating interface");
246 kobject_uevent(&dev->dev.kobj, KOBJ_ONLINE);
250 static void backend_disconnect(struct backend_info *be)
252 struct xenvif *vif = be->vif;
255 unsigned int num_queues = vif->num_queues;
256 unsigned int queue_index;
258 xen_unregister_watchers(vif);
259 #ifdef CONFIG_DEBUG_FS
260 xenvif_debugfs_delif(vif);
261 #endif /* CONFIG_DEBUG_FS */
262 xenvif_disconnect_data(vif);
264 /* At this point some of the handlers may still be active
265 * so we need to have additional synchronization here.
270 for (queue_index = 0; queue_index < num_queues; ++queue_index)
271 xenvif_deinit_queue(&vif->queues[queue_index]);
276 xenvif_disconnect_ctrl(vif);
280 static void backend_connect(struct backend_info *be)
286 static inline void backend_switch_state(struct backend_info *be,
287 enum xenbus_state state)
289 struct xenbus_device *dev = be->dev;
291 pr_debug("%s -> %s\n", dev->nodename, xenbus_strstate(state));
294 /* If we are waiting for a hotplug script then defer the
295 * actual xenbus state change.
297 if (!be->have_hotplug_status_watch)
298 xenbus_switch_state(dev, state);
301 /* Handle backend state transitions:
303 * The backend state starts in Initialising and the following transitions are
306 * Initialising -> InitWait -> Connected
318 * The state argument specifies the eventual state of the backend and the
319 * function transitions to that state via the shortest path.
321 static void set_backend_state(struct backend_info *be,
322 enum xenbus_state state)
324 while (be->state != state) {
326 case XenbusStateInitialising:
328 case XenbusStateInitWait:
329 case XenbusStateConnected:
330 case XenbusStateClosing:
331 backend_switch_state(be, XenbusStateInitWait);
333 case XenbusStateClosed:
334 backend_switch_state(be, XenbusStateClosed);
340 case XenbusStateClosed:
342 case XenbusStateInitWait:
343 case XenbusStateConnected:
344 backend_switch_state(be, XenbusStateInitWait);
346 case XenbusStateClosing:
347 backend_switch_state(be, XenbusStateClosing);
353 case XenbusStateInitWait:
355 case XenbusStateConnected:
357 backend_switch_state(be, XenbusStateConnected);
359 case XenbusStateClosing:
360 case XenbusStateClosed:
361 backend_switch_state(be, XenbusStateClosing);
367 case XenbusStateConnected:
369 case XenbusStateInitWait:
370 case XenbusStateClosing:
371 case XenbusStateClosed:
372 backend_disconnect(be);
373 backend_switch_state(be, XenbusStateClosing);
379 case XenbusStateClosing:
381 case XenbusStateInitWait:
382 case XenbusStateConnected:
383 case XenbusStateClosed:
384 backend_switch_state(be, XenbusStateClosed);
396 static void read_xenbus_frontend_xdp(struct backend_info *be,
397 struct xenbus_device *dev)
399 struct xenvif *vif = be->vif;
403 err = xenbus_scanf(XBT_NIL, dev->otherend,
404 "xdp-headroom", "%hu", &headroom);
406 vif->xdp_headroom = 0;
409 if (headroom > XEN_NETIF_MAX_XDP_HEADROOM)
410 headroom = XEN_NETIF_MAX_XDP_HEADROOM;
411 vif->xdp_headroom = headroom;
415 * Callback received when the frontend's state changes.
417 static void frontend_changed(struct xenbus_device *dev,
418 enum xenbus_state frontend_state)
420 struct backend_info *be = dev_get_drvdata(&dev->dev);
422 pr_debug("%s -> %s\n", dev->otherend, xenbus_strstate(frontend_state));
424 be->frontend_state = frontend_state;
426 switch (frontend_state) {
427 case XenbusStateInitialising:
428 set_backend_state(be, XenbusStateInitWait);
431 case XenbusStateInitialised:
434 case XenbusStateConnected:
435 set_backend_state(be, XenbusStateConnected);
438 case XenbusStateReconfiguring:
439 read_xenbus_frontend_xdp(be, dev);
440 xenbus_switch_state(dev, XenbusStateReconfigured);
443 case XenbusStateClosing:
444 set_backend_state(be, XenbusStateClosing);
447 case XenbusStateClosed:
448 set_backend_state(be, XenbusStateClosed);
449 if (xenbus_dev_is_online(dev))
451 /* fall through - if not online */
452 case XenbusStateUnknown:
453 set_backend_state(be, XenbusStateClosed);
454 device_unregister(&dev->dev);
458 xenbus_dev_fatal(dev, -EINVAL, "saw state %d at frontend",
465 static void xen_net_read_rate(struct xenbus_device *dev,
466 unsigned long *bytes, unsigned long *usec)
472 /* Default to unlimited bandwidth. */
476 ratestr = xenbus_read(XBT_NIL, dev->nodename, "rate", NULL);
481 b = simple_strtoul(s, &e, 10);
482 if ((s == e) || (*e != ','))
486 u = simple_strtoul(s, &e, 10);
487 if ((s == e) || (*e != '\0'))
497 pr_warn("Failed to parse network rate limit. Traffic unlimited.\n");
501 static int xen_net_read_mac(struct xenbus_device *dev, u8 mac[])
503 char *s, *e, *macstr;
506 macstr = s = xenbus_read(XBT_NIL, dev->nodename, "mac", NULL);
508 return PTR_ERR(macstr);
510 for (i = 0; i < ETH_ALEN; i++) {
511 mac[i] = simple_strtoul(s, &e, 16);
512 if ((s == e) || (*e != ((i == ETH_ALEN-1) ? '\0' : ':'))) {
523 static void xen_net_rate_changed(struct xenbus_watch *watch,
524 const char *path, const char *token)
526 struct xenvif *vif = container_of(watch, struct xenvif, credit_watch);
527 struct xenbus_device *dev = xenvif_to_xenbus_device(vif);
528 unsigned long credit_bytes;
529 unsigned long credit_usec;
530 unsigned int queue_index;
532 xen_net_read_rate(dev, &credit_bytes, &credit_usec);
533 for (queue_index = 0; queue_index < vif->num_queues; queue_index++) {
534 struct xenvif_queue *queue = &vif->queues[queue_index];
536 queue->credit_bytes = credit_bytes;
537 queue->credit_usec = credit_usec;
538 if (!mod_timer_pending(&queue->credit_timeout, jiffies) &&
539 queue->remaining_credit > queue->credit_bytes) {
540 queue->remaining_credit = queue->credit_bytes;
545 static int xen_register_credit_watch(struct xenbus_device *dev,
550 unsigned maxlen = strlen(dev->nodename) + sizeof("/rate");
552 if (vif->credit_watch.node)
555 node = kmalloc(maxlen, GFP_KERNEL);
558 snprintf(node, maxlen, "%s/rate", dev->nodename);
559 vif->credit_watch.node = node;
560 vif->credit_watch.callback = xen_net_rate_changed;
561 err = register_xenbus_watch(&vif->credit_watch);
563 pr_err("Failed to set watcher %s\n", vif->credit_watch.node);
565 vif->credit_watch.node = NULL;
566 vif->credit_watch.callback = NULL;
571 static void xen_unregister_credit_watch(struct xenvif *vif)
573 if (vif->credit_watch.node) {
574 unregister_xenbus_watch(&vif->credit_watch);
575 kfree(vif->credit_watch.node);
576 vif->credit_watch.node = NULL;
580 static void xen_mcast_ctrl_changed(struct xenbus_watch *watch,
581 const char *path, const char *token)
583 struct xenvif *vif = container_of(watch, struct xenvif,
585 struct xenbus_device *dev = xenvif_to_xenbus_device(vif);
587 vif->multicast_control = !!xenbus_read_unsigned(dev->otherend,
588 "request-multicast-control", 0);
591 static int xen_register_mcast_ctrl_watch(struct xenbus_device *dev,
596 unsigned maxlen = strlen(dev->otherend) +
597 sizeof("/request-multicast-control");
599 if (vif->mcast_ctrl_watch.node) {
600 pr_err_ratelimited("Watch is already registered\n");
604 node = kmalloc(maxlen, GFP_KERNEL);
606 pr_err("Failed to allocate memory for watch\n");
609 snprintf(node, maxlen, "%s/request-multicast-control",
611 vif->mcast_ctrl_watch.node = node;
612 vif->mcast_ctrl_watch.callback = xen_mcast_ctrl_changed;
613 err = register_xenbus_watch(&vif->mcast_ctrl_watch);
615 pr_err("Failed to set watcher %s\n",
616 vif->mcast_ctrl_watch.node);
618 vif->mcast_ctrl_watch.node = NULL;
619 vif->mcast_ctrl_watch.callback = NULL;
624 static void xen_unregister_mcast_ctrl_watch(struct xenvif *vif)
626 if (vif->mcast_ctrl_watch.node) {
627 unregister_xenbus_watch(&vif->mcast_ctrl_watch);
628 kfree(vif->mcast_ctrl_watch.node);
629 vif->mcast_ctrl_watch.node = NULL;
633 static void xen_register_watchers(struct xenbus_device *dev,
636 xen_register_credit_watch(dev, vif);
637 xen_register_mcast_ctrl_watch(dev, vif);
640 static void xen_unregister_watchers(struct xenvif *vif)
642 xen_unregister_mcast_ctrl_watch(vif);
643 xen_unregister_credit_watch(vif);
646 static void unregister_hotplug_status_watch(struct backend_info *be)
648 if (be->have_hotplug_status_watch) {
649 unregister_xenbus_watch(&be->hotplug_status_watch);
650 kfree(be->hotplug_status_watch.node);
652 be->have_hotplug_status_watch = 0;
655 static void hotplug_status_changed(struct xenbus_watch *watch,
659 struct backend_info *be = container_of(watch,
661 hotplug_status_watch);
665 str = xenbus_read(XBT_NIL, be->dev->nodename, "hotplug-status", &len);
668 if (len == sizeof("connected")-1 && !memcmp(str, "connected", len)) {
669 /* Complete any pending state change */
670 xenbus_switch_state(be->dev, be->state);
672 /* Not interested in this watch anymore. */
673 unregister_hotplug_status_watch(be);
674 xenbus_rm(XBT_NIL, be->dev->nodename, "hotplug-status");
679 static int connect_ctrl_ring(struct backend_info *be)
681 struct xenbus_device *dev = be->dev;
682 struct xenvif *vif = be->vif;
684 grant_ref_t ring_ref;
688 err = xenbus_scanf(XBT_NIL, dev->otherend,
689 "ctrl-ring-ref", "%u", &val);
691 goto done; /* The frontend does not have a control ring */
695 err = xenbus_scanf(XBT_NIL, dev->otherend,
696 "event-channel-ctrl", "%u", &val);
698 xenbus_dev_fatal(dev, err,
699 "reading %s/event-channel-ctrl",
706 err = xenvif_connect_ctrl(vif, ring_ref, evtchn);
708 xenbus_dev_fatal(dev, err,
709 "mapping shared-frame %u port %u",
721 static void connect(struct backend_info *be)
724 struct xenbus_device *dev = be->dev;
725 unsigned long credit_bytes, credit_usec;
726 unsigned int queue_index;
727 unsigned int requested_num_queues;
728 struct xenvif_queue *queue;
730 /* Check whether the frontend requested multiple queues
731 * and read the number requested.
733 requested_num_queues = xenbus_read_unsigned(dev->otherend,
734 "multi-queue-num-queues", 1);
735 if (requested_num_queues > xenvif_max_queues) {
736 /* buggy or malicious guest */
737 xenbus_dev_fatal(dev, -EINVAL,
738 "guest requested %u queues, exceeding the maximum of %u.",
739 requested_num_queues, xenvif_max_queues);
743 err = xen_net_read_mac(dev, be->vif->fe_dev_addr);
745 xenbus_dev_fatal(dev, err, "parsing %s/mac", dev->nodename);
749 xen_net_read_rate(dev, &credit_bytes, &credit_usec);
750 xen_unregister_watchers(be->vif);
751 xen_register_watchers(dev, be->vif);
752 read_xenbus_vif_flags(be);
754 err = connect_ctrl_ring(be);
756 xenbus_dev_fatal(dev, err, "connecting control ring");
760 /* Use the number of queues requested by the frontend */
761 be->vif->queues = vzalloc(array_size(requested_num_queues,
762 sizeof(struct xenvif_queue)));
763 if (!be->vif->queues) {
764 xenbus_dev_fatal(dev, -ENOMEM,
765 "allocating queues");
769 be->vif->num_queues = requested_num_queues;
770 be->vif->stalled_queues = requested_num_queues;
772 for (queue_index = 0; queue_index < requested_num_queues; ++queue_index) {
773 queue = &be->vif->queues[queue_index];
774 queue->vif = be->vif;
775 queue->id = queue_index;
776 snprintf(queue->name, sizeof(queue->name), "%s-q%u",
777 be->vif->dev->name, queue->id);
779 err = xenvif_init_queue(queue);
781 /* xenvif_init_queue() cleans up after itself on
782 * failure, but we need to clean up any previously
783 * initialised queues. Set num_queues to i so that
784 * earlier queues can be destroyed using the regular
787 be->vif->num_queues = queue_index;
791 queue->credit_bytes = credit_bytes;
792 queue->remaining_credit = credit_bytes;
793 queue->credit_usec = credit_usec;
795 err = connect_data_rings(be, queue);
797 /* connect_data_rings() cleans up after itself on
798 * failure, but we need to clean up after
799 * xenvif_init_queue() here, and also clean up any
800 * previously initialised queues.
802 xenvif_deinit_queue(queue);
803 be->vif->num_queues = queue_index;
808 #ifdef CONFIG_DEBUG_FS
809 xenvif_debugfs_addif(be->vif);
810 #endif /* CONFIG_DEBUG_FS */
812 /* Initialisation completed, tell core driver the number of
816 netif_set_real_num_tx_queues(be->vif->dev, requested_num_queues);
817 netif_set_real_num_rx_queues(be->vif->dev, requested_num_queues);
820 xenvif_carrier_on(be->vif);
822 unregister_hotplug_status_watch(be);
823 err = xenbus_watch_pathfmt(dev, &be->hotplug_status_watch,
824 hotplug_status_changed,
825 "%s/%s", dev->nodename, "hotplug-status");
827 be->have_hotplug_status_watch = 1;
829 netif_tx_wake_all_queues(be->vif->dev);
834 if (be->vif->num_queues > 0)
835 xenvif_disconnect_data(be->vif); /* Clean up existing queues */
836 for (queue_index = 0; queue_index < be->vif->num_queues; ++queue_index)
837 xenvif_deinit_queue(&be->vif->queues[queue_index]);
838 vfree(be->vif->queues);
839 be->vif->queues = NULL;
840 be->vif->num_queues = 0;
841 xenvif_disconnect_ctrl(be->vif);
846 static int connect_data_rings(struct backend_info *be,
847 struct xenvif_queue *queue)
849 struct xenbus_device *dev = be->dev;
850 unsigned int num_queues = queue->vif->num_queues;
851 unsigned long tx_ring_ref, rx_ring_ref;
852 unsigned int tx_evtchn, rx_evtchn;
856 const size_t xenstore_path_ext_size = 11; /* sufficient for "/queue-NNN" */
858 /* If the frontend requested 1 queue, or we have fallen back
859 * to single queue due to lack of frontend support for multi-
860 * queue, expect the remaining XenStore keys in the toplevel
861 * directory. Otherwise, expect them in a subdirectory called
864 if (num_queues == 1) {
865 xspath = kzalloc(strlen(dev->otherend) + 1, GFP_KERNEL);
867 xenbus_dev_fatal(dev, -ENOMEM,
868 "reading ring references");
871 strcpy(xspath, dev->otherend);
873 xspathsize = strlen(dev->otherend) + xenstore_path_ext_size;
874 xspath = kzalloc(xspathsize, GFP_KERNEL);
876 xenbus_dev_fatal(dev, -ENOMEM,
877 "reading ring references");
880 snprintf(xspath, xspathsize, "%s/queue-%u", dev->otherend,
884 err = xenbus_gather(XBT_NIL, xspath,
885 "tx-ring-ref", "%lu", &tx_ring_ref,
886 "rx-ring-ref", "%lu", &rx_ring_ref, NULL);
888 xenbus_dev_fatal(dev, err,
889 "reading %s/ring-ref",
894 /* Try split event channels first, then single event channel. */
895 err = xenbus_gather(XBT_NIL, xspath,
896 "event-channel-tx", "%u", &tx_evtchn,
897 "event-channel-rx", "%u", &rx_evtchn, NULL);
899 err = xenbus_scanf(XBT_NIL, xspath,
900 "event-channel", "%u", &tx_evtchn);
902 xenbus_dev_fatal(dev, err,
903 "reading %s/event-channel(-tx/rx)",
907 rx_evtchn = tx_evtchn;
910 /* Map the shared frame, irq etc. */
911 err = xenvif_connect_data(queue, tx_ring_ref, rx_ring_ref,
912 tx_evtchn, rx_evtchn);
914 xenbus_dev_fatal(dev, err,
915 "mapping shared-frames %lu/%lu port tx %u rx %u",
916 tx_ring_ref, rx_ring_ref,
917 tx_evtchn, rx_evtchn);
922 err: /* Regular return falls through with err == 0 */
927 static int read_xenbus_vif_flags(struct backend_info *be)
929 struct xenvif *vif = be->vif;
930 struct xenbus_device *dev = be->dev;
931 unsigned int rx_copy;
934 err = xenbus_scanf(XBT_NIL, dev->otherend, "request-rx-copy", "%u",
936 if (err == -ENOENT) {
941 xenbus_dev_fatal(dev, err, "reading %s/request-rx-copy",
948 if (!xenbus_read_unsigned(dev->otherend, "feature-rx-notify", 0)) {
949 /* - Reduce drain timeout to poll more frequently for
951 * - Disable Rx stall detection.
953 be->vif->drain_timeout = msecs_to_jiffies(30);
954 be->vif->stall_timeout = 0;
957 vif->can_sg = !!xenbus_read_unsigned(dev->otherend, "feature-sg", 0);
961 if (xenbus_read_unsigned(dev->otherend, "feature-gso-tcpv4", 0))
962 vif->gso_mask |= GSO_BIT(TCPV4);
964 if (xenbus_read_unsigned(dev->otherend, "feature-gso-tcpv6", 0))
965 vif->gso_mask |= GSO_BIT(TCPV6);
967 vif->ip_csum = !xenbus_read_unsigned(dev->otherend,
968 "feature-no-csum-offload", 0);
970 vif->ipv6_csum = !!xenbus_read_unsigned(dev->otherend,
971 "feature-ipv6-csum-offload", 0);
973 read_xenbus_frontend_xdp(be, dev);
978 static int netback_remove(struct xenbus_device *dev)
980 struct backend_info *be = dev_get_drvdata(&dev->dev);
982 unregister_hotplug_status_watch(be);
984 kobject_uevent(&dev->dev.kobj, KOBJ_OFFLINE);
985 backend_disconnect(be);
986 xenvif_free(be->vif);
989 kfree(be->hotplug_script);
991 dev_set_drvdata(&dev->dev, NULL);
996 * Entry point to this code when a new device is created. Allocate the basic
997 * structures and switch to InitWait.
999 static int netback_probe(struct xenbus_device *dev,
1000 const struct xenbus_device_id *id)
1002 const char *message;
1003 struct xenbus_transaction xbt;
1007 struct backend_info *be = kzalloc(sizeof(*be), GFP_KERNEL);
1010 xenbus_dev_fatal(dev, -ENOMEM,
1011 "allocating backend structure");
1016 dev_set_drvdata(&dev->dev, be);
1021 err = xenbus_transaction_start(&xbt);
1023 xenbus_dev_fatal(dev, err, "starting transaction");
1027 err = xenbus_printf(xbt, dev->nodename, "feature-sg", "%d", sg);
1029 message = "writing feature-sg";
1030 goto abort_transaction;
1033 err = xenbus_printf(xbt, dev->nodename, "feature-gso-tcpv4",
1036 message = "writing feature-gso-tcpv4";
1037 goto abort_transaction;
1040 err = xenbus_printf(xbt, dev->nodename, "feature-gso-tcpv6",
1043 message = "writing feature-gso-tcpv6";
1044 goto abort_transaction;
1047 /* We support partial checksum setup for IPv6 packets */
1048 err = xenbus_printf(xbt, dev->nodename,
1049 "feature-ipv6-csum-offload",
1052 message = "writing feature-ipv6-csum-offload";
1053 goto abort_transaction;
1056 /* We support rx-copy path. */
1057 err = xenbus_printf(xbt, dev->nodename,
1058 "feature-rx-copy", "%d", 1);
1060 message = "writing feature-rx-copy";
1061 goto abort_transaction;
1064 /* we can adjust a headroom for netfront XDP processing */
1065 err = xenbus_printf(xbt, dev->nodename,
1066 "feature-xdp-headroom", "%d",
1067 provides_xdp_headroom);
1069 message = "writing feature-xdp-headroom";
1070 goto abort_transaction;
1073 /* We don't support rx-flip path (except old guests who
1074 * don't grok this feature flag).
1076 err = xenbus_printf(xbt, dev->nodename,
1077 "feature-rx-flip", "%d", 0);
1079 message = "writing feature-rx-flip";
1080 goto abort_transaction;
1083 /* We support dynamic multicast-control. */
1084 err = xenbus_printf(xbt, dev->nodename,
1085 "feature-multicast-control", "%d", 1);
1087 message = "writing feature-multicast-control";
1088 goto abort_transaction;
1091 err = xenbus_printf(xbt, dev->nodename,
1092 "feature-dynamic-multicast-control",
1095 message = "writing feature-dynamic-multicast-control";
1096 goto abort_transaction;
1099 err = xenbus_transaction_end(xbt, 0);
1100 } while (err == -EAGAIN);
1103 xenbus_dev_fatal(dev, err, "completing transaction");
1107 /* Split event channels support, this is optional so it is not
1108 * put inside the above loop.
1110 err = xenbus_printf(XBT_NIL, dev->nodename,
1111 "feature-split-event-channels",
1112 "%u", separate_tx_rx_irq);
1114 pr_debug("Error writing feature-split-event-channels\n");
1116 /* Multi-queue support: This is an optional feature. */
1117 err = xenbus_printf(XBT_NIL, dev->nodename,
1118 "multi-queue-max-queues", "%u", xenvif_max_queues);
1120 pr_debug("Error writing multi-queue-max-queues\n");
1122 err = xenbus_printf(XBT_NIL, dev->nodename,
1123 "feature-ctrl-ring",
1126 pr_debug("Error writing feature-ctrl-ring\n");
1128 backend_switch_state(be, XenbusStateInitWait);
1130 script = xenbus_read(XBT_NIL, dev->nodename, "script", NULL);
1131 if (IS_ERR(script)) {
1132 err = PTR_ERR(script);
1133 xenbus_dev_fatal(dev, err, "reading script");
1137 be->hotplug_script = script;
1139 /* This kicks hotplug scripts, so do it immediately. */
1140 err = backend_create_xenvif(be);
1147 xenbus_transaction_end(xbt, 1);
1148 xenbus_dev_fatal(dev, err, "%s", message);
1150 pr_debug("failed\n");
1151 netback_remove(dev);
1155 static const struct xenbus_device_id netback_ids[] = {
1160 static struct xenbus_driver netback_driver = {
1162 .probe = netback_probe,
1163 .remove = netback_remove,
1164 .uevent = netback_uevent,
1165 .otherend_changed = frontend_changed,
1166 .allow_rebind = true,
1169 int xenvif_xenbus_init(void)
1171 return xenbus_register_backend(&netback_driver);
1174 void xenvif_xenbus_fini(void)
1176 return xenbus_unregister_driver(&netback_driver);