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, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 struct xenbus_device *dev;
28 /* This is the state that will be reflected in xenstore when any
29 * active hotplug script completes.
31 enum xenbus_state state;
33 enum xenbus_state frontend_state;
34 struct xenbus_watch hotplug_status_watch;
35 u8 have_hotplug_status_watch:1;
38 static int connect_rings(struct backend_info *);
39 static void connect(struct backend_info *);
40 static void backend_create_xenvif(struct backend_info *be);
41 static void unregister_hotplug_status_watch(struct backend_info *be);
42 static void set_backend_state(struct backend_info *be,
43 enum xenbus_state state);
45 static int netback_remove(struct xenbus_device *dev)
47 struct backend_info *be = dev_get_drvdata(&dev->dev);
49 set_backend_state(be, XenbusStateClosed);
51 unregister_hotplug_status_watch(be);
53 kobject_uevent(&dev->dev.kobj, KOBJ_OFFLINE);
54 xenbus_rm(XBT_NIL, dev->nodename, "hotplug-status");
59 dev_set_drvdata(&dev->dev, NULL);
65 * Entry point to this code when a new device is created. Allocate the basic
66 * structures and switch to InitWait.
68 static int netback_probe(struct xenbus_device *dev,
69 const struct xenbus_device_id *id)
72 struct xenbus_transaction xbt;
75 struct backend_info *be = kzalloc(sizeof(struct backend_info),
78 xenbus_dev_fatal(dev, -ENOMEM,
79 "allocating backend structure");
84 dev_set_drvdata(&dev->dev, be);
89 err = xenbus_transaction_start(&xbt);
91 xenbus_dev_fatal(dev, err, "starting transaction");
95 err = xenbus_printf(xbt, dev->nodename, "feature-sg", "%d", sg);
97 message = "writing feature-sg";
98 goto abort_transaction;
101 err = xenbus_printf(xbt, dev->nodename, "feature-gso-tcpv4",
104 message = "writing feature-gso-tcpv4";
105 goto abort_transaction;
108 err = xenbus_printf(xbt, dev->nodename, "feature-gso-tcpv6",
111 message = "writing feature-gso-tcpv6";
112 goto abort_transaction;
115 /* We support partial checksum setup for IPv6 packets */
116 err = xenbus_printf(xbt, dev->nodename,
117 "feature-ipv6-csum-offload",
120 message = "writing feature-ipv6-csum-offload";
121 goto abort_transaction;
124 /* We support rx-copy path. */
125 err = xenbus_printf(xbt, dev->nodename,
126 "feature-rx-copy", "%d", 1);
128 message = "writing feature-rx-copy";
129 goto abort_transaction;
133 * We don't support rx-flip path (except old guests who don't
134 * grok this feature flag).
136 err = xenbus_printf(xbt, dev->nodename,
137 "feature-rx-flip", "%d", 0);
139 message = "writing feature-rx-flip";
140 goto abort_transaction;
143 err = xenbus_transaction_end(xbt, 0);
144 } while (err == -EAGAIN);
147 xenbus_dev_fatal(dev, err, "completing transaction");
152 * Split event channels support, this is optional so it is not
153 * put inside the above loop.
155 err = xenbus_printf(XBT_NIL, dev->nodename,
156 "feature-split-event-channels",
157 "%u", separate_tx_rx_irq);
159 pr_debug("Error writing feature-split-event-channels\n");
161 err = xenbus_switch_state(dev, XenbusStateInitWait);
165 be->state = XenbusStateInitWait;
167 /* This kicks hotplug scripts, so do it immediately. */
168 backend_create_xenvif(be);
173 xenbus_transaction_end(xbt, 1);
174 xenbus_dev_fatal(dev, err, "%s", message);
176 pr_debug("failed\n");
183 * Handle the creation of the hotplug script environment. We add the script
184 * and vif variables to the environment, for the benefit of the vif-* hotplug
187 static int netback_uevent(struct xenbus_device *xdev,
188 struct kobj_uevent_env *env)
190 struct backend_info *be = dev_get_drvdata(&xdev->dev);
193 val = xenbus_read(XBT_NIL, xdev->nodename, "script", NULL);
195 int err = PTR_ERR(val);
196 xenbus_dev_fatal(xdev, err, "reading script");
199 if (add_uevent_var(env, "script=%s", val)) {
209 return add_uevent_var(env, "vif=%s", be->vif->dev->name);
213 static void backend_create_xenvif(struct backend_info *be)
217 struct xenbus_device *dev = be->dev;
222 err = xenbus_scanf(XBT_NIL, dev->nodename, "handle", "%li", &handle);
224 xenbus_dev_fatal(dev, err, "reading handle");
228 be->vif = xenvif_alloc(&dev->dev, dev->otherend_id, handle);
229 if (IS_ERR(be->vif)) {
230 err = PTR_ERR(be->vif);
232 xenbus_dev_fatal(dev, err, "creating interface");
236 kobject_uevent(&dev->dev.kobj, KOBJ_ONLINE);
239 static void backend_disconnect(struct backend_info *be)
242 xenvif_disconnect(be->vif);
245 static void backend_connect(struct backend_info *be)
251 static inline void backend_switch_state(struct backend_info *be,
252 enum xenbus_state state)
254 struct xenbus_device *dev = be->dev;
256 pr_debug("%s -> %s\n", dev->nodename, xenbus_strstate(state));
259 /* If we are waiting for a hotplug script then defer the
260 * actual xenbus state change.
262 if (!be->have_hotplug_status_watch)
263 xenbus_switch_state(dev, state);
266 /* Handle backend state transitions:
268 * The backend state starts in InitWait and the following transitions are
271 * InitWait -> Connected
283 * The state argument specifies the eventual state of the backend and the
284 * function transitions to that state via the shortest path.
286 static void set_backend_state(struct backend_info *be,
287 enum xenbus_state state)
289 while (be->state != state) {
291 case XenbusStateClosed:
293 case XenbusStateInitWait:
294 case XenbusStateConnected:
295 pr_info("%s: prepare for reconnect\n",
297 backend_switch_state(be, XenbusStateInitWait);
299 case XenbusStateClosing:
300 backend_switch_state(be, XenbusStateClosing);
306 case XenbusStateInitWait:
308 case XenbusStateConnected:
310 backend_switch_state(be, XenbusStateConnected);
312 case XenbusStateClosing:
313 case XenbusStateClosed:
314 backend_switch_state(be, XenbusStateClosing);
320 case XenbusStateConnected:
322 case XenbusStateInitWait:
323 case XenbusStateClosing:
324 case XenbusStateClosed:
325 backend_disconnect(be);
326 backend_switch_state(be, XenbusStateClosing);
332 case XenbusStateClosing:
334 case XenbusStateInitWait:
335 case XenbusStateConnected:
336 case XenbusStateClosed:
337 backend_switch_state(be, XenbusStateClosed);
350 * Callback received when the frontend's state changes.
352 static void frontend_changed(struct xenbus_device *dev,
353 enum xenbus_state frontend_state)
355 struct backend_info *be = dev_get_drvdata(&dev->dev);
357 pr_debug("%s -> %s\n", dev->otherend, xenbus_strstate(frontend_state));
359 be->frontend_state = frontend_state;
361 switch (frontend_state) {
362 case XenbusStateInitialising:
363 set_backend_state(be, XenbusStateInitWait);
366 case XenbusStateInitialised:
369 case XenbusStateConnected:
370 set_backend_state(be, XenbusStateConnected);
373 case XenbusStateClosing:
374 set_backend_state(be, XenbusStateClosing);
377 case XenbusStateClosed:
378 set_backend_state(be, XenbusStateClosed);
379 if (xenbus_dev_is_online(dev))
381 /* fall through if not online */
382 case XenbusStateUnknown:
383 set_backend_state(be, XenbusStateClosed);
384 device_unregister(&dev->dev);
388 xenbus_dev_fatal(dev, -EINVAL, "saw state %d at frontend",
395 static void xen_net_read_rate(struct xenbus_device *dev,
396 unsigned long *bytes, unsigned long *usec)
402 /* Default to unlimited bandwidth. */
406 ratestr = xenbus_read(XBT_NIL, dev->nodename, "rate", NULL);
411 b = simple_strtoul(s, &e, 10);
412 if ((s == e) || (*e != ','))
416 u = simple_strtoul(s, &e, 10);
417 if ((s == e) || (*e != '\0'))
427 pr_warn("Failed to parse network rate limit. Traffic unlimited.\n");
431 static int xen_net_read_mac(struct xenbus_device *dev, u8 mac[])
433 char *s, *e, *macstr;
436 macstr = s = xenbus_read(XBT_NIL, dev->nodename, "mac", NULL);
438 return PTR_ERR(macstr);
440 for (i = 0; i < ETH_ALEN; i++) {
441 mac[i] = simple_strtoul(s, &e, 16);
442 if ((s == e) || (*e != ((i == ETH_ALEN-1) ? '\0' : ':'))) {
453 static void unregister_hotplug_status_watch(struct backend_info *be)
455 if (be->have_hotplug_status_watch) {
456 unregister_xenbus_watch(&be->hotplug_status_watch);
457 kfree(be->hotplug_status_watch.node);
459 be->have_hotplug_status_watch = 0;
462 static void hotplug_status_changed(struct xenbus_watch *watch,
464 unsigned int vec_size)
466 struct backend_info *be = container_of(watch,
468 hotplug_status_watch);
472 str = xenbus_read(XBT_NIL, be->dev->nodename, "hotplug-status", &len);
475 if (len == sizeof("connected")-1 && !memcmp(str, "connected", len)) {
476 /* Complete any pending state change */
477 xenbus_switch_state(be->dev, be->state);
479 /* Not interested in this watch anymore. */
480 unregister_hotplug_status_watch(be);
485 static void connect(struct backend_info *be)
488 struct xenbus_device *dev = be->dev;
490 err = connect_rings(be);
494 err = xen_net_read_mac(dev, be->vif->fe_dev_addr);
496 xenbus_dev_fatal(dev, err, "parsing %s/mac", dev->nodename);
500 xen_net_read_rate(dev, &be->vif->credit_bytes,
501 &be->vif->credit_usec);
502 be->vif->remaining_credit = be->vif->credit_bytes;
504 unregister_hotplug_status_watch(be);
505 err = xenbus_watch_pathfmt(dev, &be->hotplug_status_watch,
506 hotplug_status_changed,
507 "%s/%s", dev->nodename, "hotplug-status");
509 be->have_hotplug_status_watch = 1;
511 netif_wake_queue(be->vif->dev);
515 static int connect_rings(struct backend_info *be)
517 struct xenvif *vif = be->vif;
518 struct xenbus_device *dev = be->dev;
519 unsigned long tx_ring_ref, rx_ring_ref;
520 unsigned int tx_evtchn, rx_evtchn, rx_copy;
524 err = xenbus_gather(XBT_NIL, dev->otherend,
525 "tx-ring-ref", "%lu", &tx_ring_ref,
526 "rx-ring-ref", "%lu", &rx_ring_ref, NULL);
528 xenbus_dev_fatal(dev, err,
529 "reading %s/ring-ref",
534 /* Try split event channels first, then single event channel. */
535 err = xenbus_gather(XBT_NIL, dev->otherend,
536 "event-channel-tx", "%u", &tx_evtchn,
537 "event-channel-rx", "%u", &rx_evtchn, NULL);
539 err = xenbus_scanf(XBT_NIL, dev->otherend,
540 "event-channel", "%u", &tx_evtchn);
542 xenbus_dev_fatal(dev, err,
543 "reading %s/event-channel(-tx/rx)",
547 rx_evtchn = tx_evtchn;
550 err = xenbus_scanf(XBT_NIL, dev->otherend, "request-rx-copy", "%u",
552 if (err == -ENOENT) {
557 xenbus_dev_fatal(dev, err, "reading %s/request-rx-copy",
564 if (vif->dev->tx_queue_len != 0) {
565 if (xenbus_scanf(XBT_NIL, dev->otherend,
566 "feature-rx-notify", "%d", &val) < 0)
571 /* Must be non-zero for pfifo_fast to work. */
572 vif->dev->tx_queue_len = 1;
575 if (xenbus_scanf(XBT_NIL, dev->otherend, "feature-sg",
581 vif->gso_prefix_mask = 0;
583 if (xenbus_scanf(XBT_NIL, dev->otherend, "feature-gso-tcpv4",
587 vif->gso_mask |= GSO_BIT(TCPV4);
589 if (xenbus_scanf(XBT_NIL, dev->otherend, "feature-gso-tcpv4-prefix",
593 vif->gso_prefix_mask |= GSO_BIT(TCPV4);
595 if (xenbus_scanf(XBT_NIL, dev->otherend, "feature-gso-tcpv6",
599 vif->gso_mask |= GSO_BIT(TCPV6);
601 if (xenbus_scanf(XBT_NIL, dev->otherend, "feature-gso-tcpv6-prefix",
605 vif->gso_prefix_mask |= GSO_BIT(TCPV6);
607 if (vif->gso_mask & vif->gso_prefix_mask) {
608 xenbus_dev_fatal(dev, err,
609 "%s: gso and gso prefix flags are not "
610 "mutually exclusive",
615 if (xenbus_scanf(XBT_NIL, dev->otherend, "feature-no-csum-offload",
620 if (xenbus_scanf(XBT_NIL, dev->otherend, "feature-ipv6-csum-offload",
623 vif->ipv6_csum = !!val;
625 /* Map the shared frame, irq etc. */
626 err = xenvif_connect(vif, tx_ring_ref, rx_ring_ref,
627 tx_evtchn, rx_evtchn);
629 xenbus_dev_fatal(dev, err,
630 "mapping shared-frames %lu/%lu port tx %u rx %u",
631 tx_ring_ref, rx_ring_ref,
632 tx_evtchn, rx_evtchn);
639 /* ** Driver Registration ** */
642 static const struct xenbus_device_id netback_ids[] = {
648 static DEFINE_XENBUS_DRIVER(netback, ,
649 .probe = netback_probe,
650 .remove = netback_remove,
651 .uevent = netback_uevent,
652 .otherend_changed = frontend_changed,
655 int xenvif_xenbus_init(void)
657 return xenbus_register_backend(&netback_driver);
660 void xenvif_xenbus_fini(void)
662 return xenbus_unregister_driver(&netback_driver);