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/>.
24 struct xenbus_device
*dev
;
27 /* This is the state that will be reflected in xenstore when any
28 * active hotplug script completes.
30 enum xenbus_state state
;
32 enum xenbus_state frontend_state
;
33 struct xenbus_watch hotplug_status_watch
;
34 u8 have_hotplug_status_watch
:1;
37 static int connect_rings(struct backend_info
*);
38 static void connect(struct backend_info
*);
39 static void backend_create_xenvif(struct backend_info
*be
);
40 static void unregister_hotplug_status_watch(struct backend_info
*be
);
41 static void set_backend_state(struct backend_info
*be
,
42 enum xenbus_state state
);
44 static int netback_remove(struct xenbus_device
*dev
)
46 struct backend_info
*be
= dev_get_drvdata(&dev
->dev
);
48 set_backend_state(be
, XenbusStateClosed
);
50 unregister_hotplug_status_watch(be
);
52 kobject_uevent(&dev
->dev
.kobj
, KOBJ_OFFLINE
);
53 xenbus_rm(XBT_NIL
, dev
->nodename
, "hotplug-status");
58 dev_set_drvdata(&dev
->dev
, NULL
);
64 * Entry point to this code when a new device is created. Allocate the basic
65 * structures and switch to InitWait.
67 static int netback_probe(struct xenbus_device
*dev
,
68 const struct xenbus_device_id
*id
)
71 struct xenbus_transaction xbt
;
74 struct backend_info
*be
= kzalloc(sizeof(struct backend_info
),
77 xenbus_dev_fatal(dev
, -ENOMEM
,
78 "allocating backend structure");
83 dev_set_drvdata(&dev
->dev
, be
);
88 err
= xenbus_transaction_start(&xbt
);
90 xenbus_dev_fatal(dev
, err
, "starting transaction");
94 err
= xenbus_printf(xbt
, dev
->nodename
, "feature-sg", "%d", sg
);
96 message
= "writing feature-sg";
97 goto abort_transaction
;
100 err
= xenbus_printf(xbt
, dev
->nodename
, "feature-gso-tcpv4",
103 message
= "writing feature-gso-tcpv4";
104 goto abort_transaction
;
107 err
= xenbus_printf(xbt
, dev
->nodename
, "feature-gso-tcpv6",
110 message
= "writing feature-gso-tcpv6";
111 goto abort_transaction
;
114 /* We support partial checksum setup for IPv6 packets */
115 err
= xenbus_printf(xbt
, dev
->nodename
,
116 "feature-ipv6-csum-offload",
119 message
= "writing feature-ipv6-csum-offload";
120 goto abort_transaction
;
123 /* We support rx-copy path. */
124 err
= xenbus_printf(xbt
, dev
->nodename
,
125 "feature-rx-copy", "%d", 1);
127 message
= "writing feature-rx-copy";
128 goto abort_transaction
;
132 * We don't support rx-flip path (except old guests who don't
133 * grok this feature flag).
135 err
= xenbus_printf(xbt
, dev
->nodename
,
136 "feature-rx-flip", "%d", 0);
138 message
= "writing feature-rx-flip";
139 goto abort_transaction
;
142 err
= xenbus_transaction_end(xbt
, 0);
143 } while (err
== -EAGAIN
);
146 xenbus_dev_fatal(dev
, err
, "completing transaction");
151 * Split event channels support, this is optional so it is not
152 * put inside the above loop.
154 err
= xenbus_printf(XBT_NIL
, dev
->nodename
,
155 "feature-split-event-channels",
156 "%u", separate_tx_rx_irq
);
158 pr_debug("Error writing feature-split-event-channels\n");
160 err
= xenbus_switch_state(dev
, XenbusStateInitWait
);
164 be
->state
= XenbusStateInitWait
;
166 /* This kicks hotplug scripts, so do it immediately. */
167 backend_create_xenvif(be
);
172 xenbus_transaction_end(xbt
, 1);
173 xenbus_dev_fatal(dev
, err
, "%s", message
);
175 pr_debug("failed\n");
182 * Handle the creation of the hotplug script environment. We add the script
183 * and vif variables to the environment, for the benefit of the vif-* hotplug
186 static int netback_uevent(struct xenbus_device
*xdev
,
187 struct kobj_uevent_env
*env
)
189 struct backend_info
*be
= dev_get_drvdata(&xdev
->dev
);
192 val
= xenbus_read(XBT_NIL
, xdev
->nodename
, "script", NULL
);
194 int err
= PTR_ERR(val
);
195 xenbus_dev_fatal(xdev
, err
, "reading script");
198 if (add_uevent_var(env
, "script=%s", val
)) {
208 return add_uevent_var(env
, "vif=%s", be
->vif
->dev
->name
);
212 static void backend_create_xenvif(struct backend_info
*be
)
216 struct xenbus_device
*dev
= be
->dev
;
221 err
= xenbus_scanf(XBT_NIL
, dev
->nodename
, "handle", "%li", &handle
);
223 xenbus_dev_fatal(dev
, err
, "reading handle");
227 be
->vif
= xenvif_alloc(&dev
->dev
, dev
->otherend_id
, handle
);
228 if (IS_ERR(be
->vif
)) {
229 err
= PTR_ERR(be
->vif
);
231 xenbus_dev_fatal(dev
, err
, "creating interface");
235 kobject_uevent(&dev
->dev
.kobj
, KOBJ_ONLINE
);
238 static void backend_disconnect(struct backend_info
*be
)
241 xenvif_disconnect(be
->vif
);
244 static void backend_connect(struct backend_info
*be
)
250 static inline void backend_switch_state(struct backend_info
*be
,
251 enum xenbus_state state
)
253 struct xenbus_device
*dev
= be
->dev
;
255 pr_debug("%s -> %s\n", dev
->nodename
, xenbus_strstate(state
));
258 /* If we are waiting for a hotplug script then defer the
259 * actual xenbus state change.
261 if (!be
->have_hotplug_status_watch
)
262 xenbus_switch_state(dev
, state
);
265 /* Handle backend state transitions:
267 * The backend state starts in InitWait and the following transitions are
270 * InitWait -> Connected
282 * The state argument specifies the eventual state of the backend and the
283 * function transitions to that state via the shortest path.
285 static void set_backend_state(struct backend_info
*be
,
286 enum xenbus_state state
)
288 while (be
->state
!= state
) {
290 case XenbusStateClosed
:
292 case XenbusStateInitWait
:
293 case XenbusStateConnected
:
294 pr_info("%s: prepare for reconnect\n",
296 backend_switch_state(be
, XenbusStateInitWait
);
298 case XenbusStateClosing
:
299 backend_switch_state(be
, XenbusStateClosing
);
305 case XenbusStateInitWait
:
307 case XenbusStateConnected
:
309 backend_switch_state(be
, XenbusStateConnected
);
311 case XenbusStateClosing
:
312 case XenbusStateClosed
:
313 backend_switch_state(be
, XenbusStateClosing
);
319 case XenbusStateConnected
:
321 case XenbusStateInitWait
:
322 case XenbusStateClosing
:
323 case XenbusStateClosed
:
324 backend_disconnect(be
);
325 backend_switch_state(be
, XenbusStateClosing
);
331 case XenbusStateClosing
:
333 case XenbusStateInitWait
:
334 case XenbusStateConnected
:
335 case XenbusStateClosed
:
336 backend_switch_state(be
, XenbusStateClosed
);
349 * Callback received when the frontend's state changes.
351 static void frontend_changed(struct xenbus_device
*dev
,
352 enum xenbus_state frontend_state
)
354 struct backend_info
*be
= dev_get_drvdata(&dev
->dev
);
356 pr_debug("%s -> %s\n", dev
->otherend
, xenbus_strstate(frontend_state
));
358 be
->frontend_state
= frontend_state
;
360 switch (frontend_state
) {
361 case XenbusStateInitialising
:
362 set_backend_state(be
, XenbusStateInitWait
);
365 case XenbusStateInitialised
:
368 case XenbusStateConnected
:
369 set_backend_state(be
, XenbusStateConnected
);
372 case XenbusStateClosing
:
373 set_backend_state(be
, XenbusStateClosing
);
376 case XenbusStateClosed
:
377 set_backend_state(be
, XenbusStateClosed
);
378 if (xenbus_dev_is_online(dev
))
380 /* fall through if not online */
381 case XenbusStateUnknown
:
382 set_backend_state(be
, XenbusStateClosed
);
383 device_unregister(&dev
->dev
);
387 xenbus_dev_fatal(dev
, -EINVAL
, "saw state %d at frontend",
394 static void xen_net_read_rate(struct xenbus_device
*dev
,
395 unsigned long *bytes
, unsigned long *usec
)
401 /* Default to unlimited bandwidth. */
405 ratestr
= xenbus_read(XBT_NIL
, dev
->nodename
, "rate", NULL
);
410 b
= simple_strtoul(s
, &e
, 10);
411 if ((s
== e
) || (*e
!= ','))
415 u
= simple_strtoul(s
, &e
, 10);
416 if ((s
== e
) || (*e
!= '\0'))
426 pr_warn("Failed to parse network rate limit. Traffic unlimited.\n");
430 static int xen_net_read_mac(struct xenbus_device
*dev
, u8 mac
[])
432 char *s
, *e
, *macstr
;
435 macstr
= s
= xenbus_read(XBT_NIL
, dev
->nodename
, "mac", NULL
);
437 return PTR_ERR(macstr
);
439 for (i
= 0; i
< ETH_ALEN
; i
++) {
440 mac
[i
] = simple_strtoul(s
, &e
, 16);
441 if ((s
== e
) || (*e
!= ((i
== ETH_ALEN
-1) ? '\0' : ':'))) {
452 static void unregister_hotplug_status_watch(struct backend_info
*be
)
454 if (be
->have_hotplug_status_watch
) {
455 unregister_xenbus_watch(&be
->hotplug_status_watch
);
456 kfree(be
->hotplug_status_watch
.node
);
458 be
->have_hotplug_status_watch
= 0;
461 static void hotplug_status_changed(struct xenbus_watch
*watch
,
463 unsigned int vec_size
)
465 struct backend_info
*be
= container_of(watch
,
467 hotplug_status_watch
);
471 str
= xenbus_read(XBT_NIL
, be
->dev
->nodename
, "hotplug-status", &len
);
474 if (len
== sizeof("connected")-1 && !memcmp(str
, "connected", len
)) {
475 /* Complete any pending state change */
476 xenbus_switch_state(be
->dev
, be
->state
);
478 /* Not interested in this watch anymore. */
479 unregister_hotplug_status_watch(be
);
484 static void connect(struct backend_info
*be
)
487 struct xenbus_device
*dev
= be
->dev
;
489 err
= connect_rings(be
);
493 err
= xen_net_read_mac(dev
, be
->vif
->fe_dev_addr
);
495 xenbus_dev_fatal(dev
, err
, "parsing %s/mac", dev
->nodename
);
499 xen_net_read_rate(dev
, &be
->vif
->credit_bytes
,
500 &be
->vif
->credit_usec
);
501 be
->vif
->remaining_credit
= be
->vif
->credit_bytes
;
503 unregister_hotplug_status_watch(be
);
504 err
= xenbus_watch_pathfmt(dev
, &be
->hotplug_status_watch
,
505 hotplug_status_changed
,
506 "%s/%s", dev
->nodename
, "hotplug-status");
508 be
->have_hotplug_status_watch
= 1;
510 netif_wake_queue(be
->vif
->dev
);
514 static int connect_rings(struct backend_info
*be
)
516 struct xenvif
*vif
= be
->vif
;
517 struct xenbus_device
*dev
= be
->dev
;
518 unsigned long tx_ring_ref
, rx_ring_ref
;
519 unsigned int tx_evtchn
, rx_evtchn
, rx_copy
;
523 err
= xenbus_gather(XBT_NIL
, dev
->otherend
,
524 "tx-ring-ref", "%lu", &tx_ring_ref
,
525 "rx-ring-ref", "%lu", &rx_ring_ref
, NULL
);
527 xenbus_dev_fatal(dev
, err
,
528 "reading %s/ring-ref",
533 /* Try split event channels first, then single event channel. */
534 err
= xenbus_gather(XBT_NIL
, dev
->otherend
,
535 "event-channel-tx", "%u", &tx_evtchn
,
536 "event-channel-rx", "%u", &rx_evtchn
, NULL
);
538 err
= xenbus_scanf(XBT_NIL
, dev
->otherend
,
539 "event-channel", "%u", &tx_evtchn
);
541 xenbus_dev_fatal(dev
, err
,
542 "reading %s/event-channel(-tx/rx)",
546 rx_evtchn
= tx_evtchn
;
549 err
= xenbus_scanf(XBT_NIL
, dev
->otherend
, "request-rx-copy", "%u",
551 if (err
== -ENOENT
) {
556 xenbus_dev_fatal(dev
, err
, "reading %s/request-rx-copy",
563 if (vif
->dev
->tx_queue_len
!= 0) {
564 if (xenbus_scanf(XBT_NIL
, dev
->otherend
,
565 "feature-rx-notify", "%d", &val
) < 0)
570 /* Must be non-zero for pfifo_fast to work. */
571 vif
->dev
->tx_queue_len
= 1;
574 if (xenbus_scanf(XBT_NIL
, dev
->otherend
, "feature-sg",
580 vif
->gso_prefix_mask
= 0;
582 if (xenbus_scanf(XBT_NIL
, dev
->otherend
, "feature-gso-tcpv4",
586 vif
->gso_mask
|= GSO_BIT(TCPV4
);
588 if (xenbus_scanf(XBT_NIL
, dev
->otherend
, "feature-gso-tcpv4-prefix",
592 vif
->gso_prefix_mask
|= GSO_BIT(TCPV4
);
594 if (xenbus_scanf(XBT_NIL
, dev
->otherend
, "feature-gso-tcpv6",
598 vif
->gso_mask
|= GSO_BIT(TCPV6
);
600 if (xenbus_scanf(XBT_NIL
, dev
->otherend
, "feature-gso-tcpv6-prefix",
604 vif
->gso_prefix_mask
|= GSO_BIT(TCPV6
);
606 if (vif
->gso_mask
& vif
->gso_prefix_mask
) {
607 xenbus_dev_fatal(dev
, err
,
608 "%s: gso and gso prefix flags are not "
609 "mutually exclusive",
614 if (xenbus_scanf(XBT_NIL
, dev
->otherend
, "feature-no-csum-offload",
619 if (xenbus_scanf(XBT_NIL
, dev
->otherend
, "feature-ipv6-csum-offload",
622 vif
->ipv6_csum
= !!val
;
624 /* Map the shared frame, irq etc. */
625 err
= xenvif_connect(vif
, tx_ring_ref
, rx_ring_ref
,
626 tx_evtchn
, rx_evtchn
);
628 xenbus_dev_fatal(dev
, err
,
629 "mapping shared-frames %lu/%lu port tx %u rx %u",
630 tx_ring_ref
, rx_ring_ref
,
631 tx_evtchn
, rx_evtchn
);
638 /* ** Driver Registration ** */
641 static const struct xenbus_device_id netback_ids
[] = {
647 static DEFINE_XENBUS_DRIVER(netback
, ,
648 .probe
= netback_probe
,
649 .remove
= netback_remove
,
650 .uevent
= netback_uevent
,
651 .otherend_changed
= frontend_changed
,
654 int xenvif_xenbus_init(void)
656 return xenbus_register_backend(&netback_driver
);
659 void xenvif_xenbus_fini(void)
661 return xenbus_unregister_driver(&netback_driver
);