1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Xenbus code for netif backend
5 * Copyright (C) 2005 Rusty Russell <rusty@rustcorp.com.au>
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
);
397 * Callback received when the frontend's state changes.
399 static void frontend_changed(struct xenbus_device
*dev
,
400 enum xenbus_state frontend_state
)
402 struct backend_info
*be
= dev_get_drvdata(&dev
->dev
);
404 pr_debug("%s -> %s\n", dev
->otherend
, xenbus_strstate(frontend_state
));
406 be
->frontend_state
= frontend_state
;
408 switch (frontend_state
) {
409 case XenbusStateInitialising
:
410 set_backend_state(be
, XenbusStateInitWait
);
413 case XenbusStateInitialised
:
416 case XenbusStateConnected
:
417 set_backend_state(be
, XenbusStateConnected
);
420 case XenbusStateClosing
:
421 set_backend_state(be
, XenbusStateClosing
);
424 case XenbusStateClosed
:
425 set_backend_state(be
, XenbusStateClosed
);
426 if (xenbus_dev_is_online(dev
))
428 /* fall through - if not online */
429 case XenbusStateUnknown
:
430 set_backend_state(be
, XenbusStateClosed
);
431 device_unregister(&dev
->dev
);
435 xenbus_dev_fatal(dev
, -EINVAL
, "saw state %d at frontend",
442 static void xen_net_read_rate(struct xenbus_device
*dev
,
443 unsigned long *bytes
, unsigned long *usec
)
449 /* Default to unlimited bandwidth. */
453 ratestr
= xenbus_read(XBT_NIL
, dev
->nodename
, "rate", NULL
);
458 b
= simple_strtoul(s
, &e
, 10);
459 if ((s
== e
) || (*e
!= ','))
463 u
= simple_strtoul(s
, &e
, 10);
464 if ((s
== e
) || (*e
!= '\0'))
474 pr_warn("Failed to parse network rate limit. Traffic unlimited.\n");
478 static int xen_net_read_mac(struct xenbus_device
*dev
, u8 mac
[])
480 char *s
, *e
, *macstr
;
483 macstr
= s
= xenbus_read(XBT_NIL
, dev
->nodename
, "mac", NULL
);
485 return PTR_ERR(macstr
);
487 for (i
= 0; i
< ETH_ALEN
; i
++) {
488 mac
[i
] = simple_strtoul(s
, &e
, 16);
489 if ((s
== e
) || (*e
!= ((i
== ETH_ALEN
-1) ? '\0' : ':'))) {
500 static void xen_net_rate_changed(struct xenbus_watch
*watch
,
501 const char *path
, const char *token
)
503 struct xenvif
*vif
= container_of(watch
, struct xenvif
, credit_watch
);
504 struct xenbus_device
*dev
= xenvif_to_xenbus_device(vif
);
505 unsigned long credit_bytes
;
506 unsigned long credit_usec
;
507 unsigned int queue_index
;
509 xen_net_read_rate(dev
, &credit_bytes
, &credit_usec
);
510 for (queue_index
= 0; queue_index
< vif
->num_queues
; queue_index
++) {
511 struct xenvif_queue
*queue
= &vif
->queues
[queue_index
];
513 queue
->credit_bytes
= credit_bytes
;
514 queue
->credit_usec
= credit_usec
;
515 if (!mod_timer_pending(&queue
->credit_timeout
, jiffies
) &&
516 queue
->remaining_credit
> queue
->credit_bytes
) {
517 queue
->remaining_credit
= queue
->credit_bytes
;
522 static int xen_register_credit_watch(struct xenbus_device
*dev
,
527 unsigned maxlen
= strlen(dev
->nodename
) + sizeof("/rate");
529 if (vif
->credit_watch
.node
)
532 node
= kmalloc(maxlen
, GFP_KERNEL
);
535 snprintf(node
, maxlen
, "%s/rate", dev
->nodename
);
536 vif
->credit_watch
.node
= node
;
537 vif
->credit_watch
.callback
= xen_net_rate_changed
;
538 err
= register_xenbus_watch(&vif
->credit_watch
);
540 pr_err("Failed to set watcher %s\n", vif
->credit_watch
.node
);
542 vif
->credit_watch
.node
= NULL
;
543 vif
->credit_watch
.callback
= NULL
;
548 static void xen_unregister_credit_watch(struct xenvif
*vif
)
550 if (vif
->credit_watch
.node
) {
551 unregister_xenbus_watch(&vif
->credit_watch
);
552 kfree(vif
->credit_watch
.node
);
553 vif
->credit_watch
.node
= NULL
;
557 static void xen_mcast_ctrl_changed(struct xenbus_watch
*watch
,
558 const char *path
, const char *token
)
560 struct xenvif
*vif
= container_of(watch
, struct xenvif
,
562 struct xenbus_device
*dev
= xenvif_to_xenbus_device(vif
);
564 vif
->multicast_control
= !!xenbus_read_unsigned(dev
->otherend
,
565 "request-multicast-control", 0);
568 static int xen_register_mcast_ctrl_watch(struct xenbus_device
*dev
,
573 unsigned maxlen
= strlen(dev
->otherend
) +
574 sizeof("/request-multicast-control");
576 if (vif
->mcast_ctrl_watch
.node
) {
577 pr_err_ratelimited("Watch is already registered\n");
581 node
= kmalloc(maxlen
, GFP_KERNEL
);
583 pr_err("Failed to allocate memory for watch\n");
586 snprintf(node
, maxlen
, "%s/request-multicast-control",
588 vif
->mcast_ctrl_watch
.node
= node
;
589 vif
->mcast_ctrl_watch
.callback
= xen_mcast_ctrl_changed
;
590 err
= register_xenbus_watch(&vif
->mcast_ctrl_watch
);
592 pr_err("Failed to set watcher %s\n",
593 vif
->mcast_ctrl_watch
.node
);
595 vif
->mcast_ctrl_watch
.node
= NULL
;
596 vif
->mcast_ctrl_watch
.callback
= NULL
;
601 static void xen_unregister_mcast_ctrl_watch(struct xenvif
*vif
)
603 if (vif
->mcast_ctrl_watch
.node
) {
604 unregister_xenbus_watch(&vif
->mcast_ctrl_watch
);
605 kfree(vif
->mcast_ctrl_watch
.node
);
606 vif
->mcast_ctrl_watch
.node
= NULL
;
610 static void xen_register_watchers(struct xenbus_device
*dev
,
613 xen_register_credit_watch(dev
, vif
);
614 xen_register_mcast_ctrl_watch(dev
, vif
);
617 static void xen_unregister_watchers(struct xenvif
*vif
)
619 xen_unregister_mcast_ctrl_watch(vif
);
620 xen_unregister_credit_watch(vif
);
623 static void unregister_hotplug_status_watch(struct backend_info
*be
)
625 if (be
->have_hotplug_status_watch
) {
626 unregister_xenbus_watch(&be
->hotplug_status_watch
);
627 kfree(be
->hotplug_status_watch
.node
);
629 be
->have_hotplug_status_watch
= 0;
632 static void hotplug_status_changed(struct xenbus_watch
*watch
,
636 struct backend_info
*be
= container_of(watch
,
638 hotplug_status_watch
);
642 str
= xenbus_read(XBT_NIL
, be
->dev
->nodename
, "hotplug-status", &len
);
645 if (len
== sizeof("connected")-1 && !memcmp(str
, "connected", len
)) {
646 /* Complete any pending state change */
647 xenbus_switch_state(be
->dev
, be
->state
);
649 /* Not interested in this watch anymore. */
650 unregister_hotplug_status_watch(be
);
651 xenbus_rm(XBT_NIL
, be
->dev
->nodename
, "hotplug-status");
656 static int connect_ctrl_ring(struct backend_info
*be
)
658 struct xenbus_device
*dev
= be
->dev
;
659 struct xenvif
*vif
= be
->vif
;
661 grant_ref_t ring_ref
;
665 err
= xenbus_scanf(XBT_NIL
, dev
->otherend
,
666 "ctrl-ring-ref", "%u", &val
);
668 goto done
; /* The frontend does not have a control ring */
672 err
= xenbus_scanf(XBT_NIL
, dev
->otherend
,
673 "event-channel-ctrl", "%u", &val
);
675 xenbus_dev_fatal(dev
, err
,
676 "reading %s/event-channel-ctrl",
683 err
= xenvif_connect_ctrl(vif
, ring_ref
, evtchn
);
685 xenbus_dev_fatal(dev
, err
,
686 "mapping shared-frame %u port %u",
698 static void connect(struct backend_info
*be
)
701 struct xenbus_device
*dev
= be
->dev
;
702 unsigned long credit_bytes
, credit_usec
;
703 unsigned int queue_index
;
704 unsigned int requested_num_queues
;
705 struct xenvif_queue
*queue
;
707 /* Check whether the frontend requested multiple queues
708 * and read the number requested.
710 requested_num_queues
= xenbus_read_unsigned(dev
->otherend
,
711 "multi-queue-num-queues", 1);
712 if (requested_num_queues
> xenvif_max_queues
) {
713 /* buggy or malicious guest */
714 xenbus_dev_fatal(dev
, -EINVAL
,
715 "guest requested %u queues, exceeding the maximum of %u.",
716 requested_num_queues
, xenvif_max_queues
);
720 err
= xen_net_read_mac(dev
, be
->vif
->fe_dev_addr
);
722 xenbus_dev_fatal(dev
, err
, "parsing %s/mac", dev
->nodename
);
726 xen_net_read_rate(dev
, &credit_bytes
, &credit_usec
);
727 xen_unregister_watchers(be
->vif
);
728 xen_register_watchers(dev
, be
->vif
);
729 read_xenbus_vif_flags(be
);
731 err
= connect_ctrl_ring(be
);
733 xenbus_dev_fatal(dev
, err
, "connecting control ring");
737 /* Use the number of queues requested by the frontend */
738 be
->vif
->queues
= vzalloc(array_size(requested_num_queues
,
739 sizeof(struct xenvif_queue
)));
740 if (!be
->vif
->queues
) {
741 xenbus_dev_fatal(dev
, -ENOMEM
,
742 "allocating queues");
746 be
->vif
->num_queues
= requested_num_queues
;
747 be
->vif
->stalled_queues
= requested_num_queues
;
749 for (queue_index
= 0; queue_index
< requested_num_queues
; ++queue_index
) {
750 queue
= &be
->vif
->queues
[queue_index
];
751 queue
->vif
= be
->vif
;
752 queue
->id
= queue_index
;
753 snprintf(queue
->name
, sizeof(queue
->name
), "%s-q%u",
754 be
->vif
->dev
->name
, queue
->id
);
756 err
= xenvif_init_queue(queue
);
758 /* xenvif_init_queue() cleans up after itself on
759 * failure, but we need to clean up any previously
760 * initialised queues. Set num_queues to i so that
761 * earlier queues can be destroyed using the regular
764 be
->vif
->num_queues
= queue_index
;
768 queue
->credit_bytes
= credit_bytes
;
769 queue
->remaining_credit
= credit_bytes
;
770 queue
->credit_usec
= credit_usec
;
772 err
= connect_data_rings(be
, queue
);
774 /* connect_data_rings() cleans up after itself on
775 * failure, but we need to clean up after
776 * xenvif_init_queue() here, and also clean up any
777 * previously initialised queues.
779 xenvif_deinit_queue(queue
);
780 be
->vif
->num_queues
= queue_index
;
785 #ifdef CONFIG_DEBUG_FS
786 xenvif_debugfs_addif(be
->vif
);
787 #endif /* CONFIG_DEBUG_FS */
789 /* Initialisation completed, tell core driver the number of
793 netif_set_real_num_tx_queues(be
->vif
->dev
, requested_num_queues
);
794 netif_set_real_num_rx_queues(be
->vif
->dev
, requested_num_queues
);
797 xenvif_carrier_on(be
->vif
);
799 unregister_hotplug_status_watch(be
);
800 err
= xenbus_watch_pathfmt(dev
, &be
->hotplug_status_watch
,
801 hotplug_status_changed
,
802 "%s/%s", dev
->nodename
, "hotplug-status");
804 be
->have_hotplug_status_watch
= 1;
806 netif_tx_wake_all_queues(be
->vif
->dev
);
811 if (be
->vif
->num_queues
> 0)
812 xenvif_disconnect_data(be
->vif
); /* Clean up existing queues */
813 for (queue_index
= 0; queue_index
< be
->vif
->num_queues
; ++queue_index
)
814 xenvif_deinit_queue(&be
->vif
->queues
[queue_index
]);
815 vfree(be
->vif
->queues
);
816 be
->vif
->queues
= NULL
;
817 be
->vif
->num_queues
= 0;
818 xenvif_disconnect_ctrl(be
->vif
);
823 static int connect_data_rings(struct backend_info
*be
,
824 struct xenvif_queue
*queue
)
826 struct xenbus_device
*dev
= be
->dev
;
827 unsigned int num_queues
= queue
->vif
->num_queues
;
828 unsigned long tx_ring_ref
, rx_ring_ref
;
829 unsigned int tx_evtchn
, rx_evtchn
;
833 const size_t xenstore_path_ext_size
= 11; /* sufficient for "/queue-NNN" */
835 /* If the frontend requested 1 queue, or we have fallen back
836 * to single queue due to lack of frontend support for multi-
837 * queue, expect the remaining XenStore keys in the toplevel
838 * directory. Otherwise, expect them in a subdirectory called
841 if (num_queues
== 1) {
842 xspath
= kzalloc(strlen(dev
->otherend
) + 1, GFP_KERNEL
);
844 xenbus_dev_fatal(dev
, -ENOMEM
,
845 "reading ring references");
848 strcpy(xspath
, dev
->otherend
);
850 xspathsize
= strlen(dev
->otherend
) + xenstore_path_ext_size
;
851 xspath
= kzalloc(xspathsize
, GFP_KERNEL
);
853 xenbus_dev_fatal(dev
, -ENOMEM
,
854 "reading ring references");
857 snprintf(xspath
, xspathsize
, "%s/queue-%u", dev
->otherend
,
861 err
= xenbus_gather(XBT_NIL
, xspath
,
862 "tx-ring-ref", "%lu", &tx_ring_ref
,
863 "rx-ring-ref", "%lu", &rx_ring_ref
, NULL
);
865 xenbus_dev_fatal(dev
, err
,
866 "reading %s/ring-ref",
871 /* Try split event channels first, then single event channel. */
872 err
= xenbus_gather(XBT_NIL
, xspath
,
873 "event-channel-tx", "%u", &tx_evtchn
,
874 "event-channel-rx", "%u", &rx_evtchn
, NULL
);
876 err
= xenbus_scanf(XBT_NIL
, xspath
,
877 "event-channel", "%u", &tx_evtchn
);
879 xenbus_dev_fatal(dev
, err
,
880 "reading %s/event-channel(-tx/rx)",
884 rx_evtchn
= tx_evtchn
;
887 /* Map the shared frame, irq etc. */
888 err
= xenvif_connect_data(queue
, tx_ring_ref
, rx_ring_ref
,
889 tx_evtchn
, rx_evtchn
);
891 xenbus_dev_fatal(dev
, err
,
892 "mapping shared-frames %lu/%lu port tx %u rx %u",
893 tx_ring_ref
, rx_ring_ref
,
894 tx_evtchn
, rx_evtchn
);
899 err
: /* Regular return falls through with err == 0 */
904 static int read_xenbus_vif_flags(struct backend_info
*be
)
906 struct xenvif
*vif
= be
->vif
;
907 struct xenbus_device
*dev
= be
->dev
;
908 unsigned int rx_copy
;
911 err
= xenbus_scanf(XBT_NIL
, dev
->otherend
, "request-rx-copy", "%u",
913 if (err
== -ENOENT
) {
918 xenbus_dev_fatal(dev
, err
, "reading %s/request-rx-copy",
925 if (!xenbus_read_unsigned(dev
->otherend
, "feature-rx-notify", 0)) {
926 /* - Reduce drain timeout to poll more frequently for
928 * - Disable Rx stall detection.
930 be
->vif
->drain_timeout
= msecs_to_jiffies(30);
931 be
->vif
->stall_timeout
= 0;
934 vif
->can_sg
= !!xenbus_read_unsigned(dev
->otherend
, "feature-sg", 0);
938 if (xenbus_read_unsigned(dev
->otherend
, "feature-gso-tcpv4", 0))
939 vif
->gso_mask
|= GSO_BIT(TCPV4
);
941 if (xenbus_read_unsigned(dev
->otherend
, "feature-gso-tcpv6", 0))
942 vif
->gso_mask
|= GSO_BIT(TCPV6
);
944 vif
->ip_csum
= !xenbus_read_unsigned(dev
->otherend
,
945 "feature-no-csum-offload", 0);
947 vif
->ipv6_csum
= !!xenbus_read_unsigned(dev
->otherend
,
948 "feature-ipv6-csum-offload", 0);
953 static int netback_remove(struct xenbus_device
*dev
)
955 struct backend_info
*be
= dev_get_drvdata(&dev
->dev
);
957 unregister_hotplug_status_watch(be
);
959 kobject_uevent(&dev
->dev
.kobj
, KOBJ_OFFLINE
);
960 backend_disconnect(be
);
961 xenvif_free(be
->vif
);
964 kfree(be
->hotplug_script
);
966 dev_set_drvdata(&dev
->dev
, NULL
);
971 * Entry point to this code when a new device is created. Allocate the basic
972 * structures and switch to InitWait.
974 static int netback_probe(struct xenbus_device
*dev
,
975 const struct xenbus_device_id
*id
)
978 struct xenbus_transaction xbt
;
982 struct backend_info
*be
= kzalloc(sizeof(*be
), GFP_KERNEL
);
985 xenbus_dev_fatal(dev
, -ENOMEM
,
986 "allocating backend structure");
991 dev_set_drvdata(&dev
->dev
, be
);
996 err
= xenbus_transaction_start(&xbt
);
998 xenbus_dev_fatal(dev
, err
, "starting transaction");
1002 err
= xenbus_printf(xbt
, dev
->nodename
, "feature-sg", "%d", sg
);
1004 message
= "writing feature-sg";
1005 goto abort_transaction
;
1008 err
= xenbus_printf(xbt
, dev
->nodename
, "feature-gso-tcpv4",
1011 message
= "writing feature-gso-tcpv4";
1012 goto abort_transaction
;
1015 err
= xenbus_printf(xbt
, dev
->nodename
, "feature-gso-tcpv6",
1018 message
= "writing feature-gso-tcpv6";
1019 goto abort_transaction
;
1022 /* We support partial checksum setup for IPv6 packets */
1023 err
= xenbus_printf(xbt
, dev
->nodename
,
1024 "feature-ipv6-csum-offload",
1027 message
= "writing feature-ipv6-csum-offload";
1028 goto abort_transaction
;
1031 /* We support rx-copy path. */
1032 err
= xenbus_printf(xbt
, dev
->nodename
,
1033 "feature-rx-copy", "%d", 1);
1035 message
= "writing feature-rx-copy";
1036 goto abort_transaction
;
1039 /* We don't support rx-flip path (except old guests who
1040 * don't grok this feature flag).
1042 err
= xenbus_printf(xbt
, dev
->nodename
,
1043 "feature-rx-flip", "%d", 0);
1045 message
= "writing feature-rx-flip";
1046 goto abort_transaction
;
1049 /* We support dynamic multicast-control. */
1050 err
= xenbus_printf(xbt
, dev
->nodename
,
1051 "feature-multicast-control", "%d", 1);
1053 message
= "writing feature-multicast-control";
1054 goto abort_transaction
;
1057 err
= xenbus_printf(xbt
, dev
->nodename
,
1058 "feature-dynamic-multicast-control",
1061 message
= "writing feature-dynamic-multicast-control";
1062 goto abort_transaction
;
1065 err
= xenbus_transaction_end(xbt
, 0);
1066 } while (err
== -EAGAIN
);
1069 xenbus_dev_fatal(dev
, err
, "completing transaction");
1073 /* Split event channels support, this is optional so it is not
1074 * put inside the above loop.
1076 err
= xenbus_printf(XBT_NIL
, dev
->nodename
,
1077 "feature-split-event-channels",
1078 "%u", separate_tx_rx_irq
);
1080 pr_debug("Error writing feature-split-event-channels\n");
1082 /* Multi-queue support: This is an optional feature. */
1083 err
= xenbus_printf(XBT_NIL
, dev
->nodename
,
1084 "multi-queue-max-queues", "%u", xenvif_max_queues
);
1086 pr_debug("Error writing multi-queue-max-queues\n");
1088 err
= xenbus_printf(XBT_NIL
, dev
->nodename
,
1089 "feature-ctrl-ring",
1092 pr_debug("Error writing feature-ctrl-ring\n");
1094 backend_switch_state(be
, XenbusStateInitWait
);
1096 script
= xenbus_read(XBT_NIL
, dev
->nodename
, "script", NULL
);
1097 if (IS_ERR(script
)) {
1098 err
= PTR_ERR(script
);
1099 xenbus_dev_fatal(dev
, err
, "reading script");
1103 be
->hotplug_script
= script
;
1105 /* This kicks hotplug scripts, so do it immediately. */
1106 err
= backend_create_xenvif(be
);
1113 xenbus_transaction_end(xbt
, 1);
1114 xenbus_dev_fatal(dev
, err
, "%s", message
);
1116 pr_debug("failed\n");
1117 netback_remove(dev
);
1121 static const struct xenbus_device_id netback_ids
[] = {
1126 static struct xenbus_driver netback_driver
= {
1128 .probe
= netback_probe
,
1129 .remove
= netback_remove
,
1130 .uevent
= netback_uevent
,
1131 .otherend_changed
= frontend_changed
,
1132 .allow_rebind
= true,
1135 int xenvif_xenbus_init(void)
1137 return xenbus_register_backend(&netback_driver
);
1140 void xenvif_xenbus_fini(void)
1142 return xenbus_unregister_driver(&netback_driver
);