1 /* Xenbus code for netif backend
2 Copyright (C) 2005 Rusty Russell <rusty@rustcorp.com.au>
3 Copyright (C) 2005 XenSource Ltd
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include <linux/module.h>
22 #include <xen/xenbus.h>
27 #define DPRINTK(fmt, args...) \
28 printk("netback/xenbus (%s:%d) " fmt ".\n", __FUNCTION__, __LINE__, ##args)
33 struct xenbus_device *dev;
35 struct xenbus_watch backend_watch;
36 enum xenbus_state frontend_state;
39 static int connect_rings(struct backend_info *);
40 static void connect(struct backend_info *);
41 static void maybe_connect(struct backend_info *);
42 static void backend_changed(struct xenbus_watch *, const char **,
45 static int netback_remove(struct xenbus_device *dev)
47 struct backend_info *be = dev->dev.driver_data;
49 if (be->backend_watch.node) {
50 unregister_xenbus_watch(&be->backend_watch);
51 kfree(be->backend_watch.node);
52 be->backend_watch.node = NULL;
55 netif_disconnect(be->netif);
59 dev->dev.driver_data = NULL;
65 * Entry point to this code when a new device is created. Allocate the basic
66 * structures, and watch the store waiting for the hotplug scripts to tell us
67 * the device's handle. Switch to InitWait.
69 static int netback_probe(struct xenbus_device *dev,
70 const struct xenbus_device_id *id)
73 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->dev.driver_data = be;
86 err = xenbus_watch_path2(dev, dev->nodename, "handle",
87 &be->backend_watch, backend_changed);
92 err = xenbus_transaction_start(&xbt);
94 xenbus_dev_fatal(dev, err, "starting transaction");
98 err = xenbus_printf(xbt, dev->nodename, "feature-sg", "%d", 1);
100 message = "writing feature-sg";
101 goto abort_transaction;
104 err = xenbus_printf(xbt, dev->nodename, "feature-gso-tcpv4",
107 message = "writing feature-gso-tcpv4";
108 goto abort_transaction;
111 err = xenbus_printf(xbt, dev->nodename, "feature-rx-copy", "%d", 1);
113 message = "writing feature-copying";
114 goto abort_transaction;
117 err = xenbus_transaction_end(xbt, 0);
118 } while (err == -EAGAIN);
121 xenbus_dev_fatal(dev, err, "completing transaction");
125 err = xenbus_switch_state(dev, XenbusStateInitWait);
133 xenbus_transaction_end(xbt, 1);
134 xenbus_dev_fatal(dev, err, "%s", message);
143 * Handle the creation of the hotplug script environment. We add the script
144 * and vif variables to the environment, for the benefit of the vif-* hotplug
147 static int netback_uevent(struct xenbus_device *xdev, char **envp,
148 int num_envp, char *buffer, int buffer_size)
150 struct backend_info *be = xdev->dev.driver_data;
151 netif_t *netif = be->netif;
152 int i = 0, length = 0;
155 DPRINTK("netback_uevent");
157 val = xenbus_read(XBT_NIL, xdev->nodename, "script", NULL);
159 int err = PTR_ERR(val);
160 xenbus_dev_fatal(xdev, err, "reading script");
164 add_uevent_var(envp, num_envp, &i, buffer, buffer_size,
165 &length, "script=%s", val);
169 add_uevent_var(envp, num_envp, &i, buffer, buffer_size, &length,
170 "vif=%s", netif->dev->name);
179 * Callback received when the hotplug scripts have placed the handle node.
180 * Read it, and create a netif structure. If the frontend is ready, connect.
182 static void backend_changed(struct xenbus_watch *watch,
183 const char **vec, unsigned int len)
187 struct backend_info *be
188 = container_of(watch, struct backend_info, backend_watch);
189 struct xenbus_device *dev = be->dev;
193 err = xenbus_scanf(XBT_NIL, dev->nodename, "handle", "%li", &handle);
194 if (XENBUS_EXIST_ERR(err)) {
195 /* Since this watch will fire once immediately after it is
196 registered, we expect this. Ignore it, and wait for the
201 xenbus_dev_fatal(dev, err, "reading handle");
205 if (be->netif == NULL) {
206 u8 be_mac[ETH_ALEN] = { 0, 0, 0, 0, 0, 0 };
208 be->netif = netif_alloc(dev->otherend_id, handle, be_mac);
209 if (IS_ERR(be->netif)) {
210 err = PTR_ERR(be->netif);
212 xenbus_dev_fatal(dev, err, "creating interface");
216 kobject_uevent(&dev->dev.kobj, KOBJ_ONLINE);
224 * Callback received when the frontend's state changes.
226 static void frontend_changed(struct xenbus_device *dev,
227 enum xenbus_state frontend_state)
229 struct backend_info *be = dev->dev.driver_data;
231 DPRINTK("%s", xenbus_strstate(frontend_state));
233 be->frontend_state = frontend_state;
235 switch (frontend_state) {
236 case XenbusStateInitialising:
237 if (dev->state == XenbusStateClosed) {
238 printk("%s: %s: prepare for reconnect\n",
239 __FUNCTION__, dev->nodename);
241 netif_disconnect(be->netif);
244 xenbus_switch_state(dev, XenbusStateInitWait);
248 case XenbusStateInitialised:
251 case XenbusStateConnected:
253 /* reconnect: setup be->netif */
254 backend_changed(&be->backend_watch, NULL, 0);
259 case XenbusStateClosing:
260 xenbus_switch_state(dev, XenbusStateClosing);
263 case XenbusStateClosed:
264 xenbus_switch_state(dev, XenbusStateClosed);
265 if (xenbus_dev_is_online(dev))
267 /* fall through if not online */
268 case XenbusStateUnknown:
269 if (be->netif != NULL)
270 kobject_uevent(&dev->dev.kobj, KOBJ_OFFLINE);
271 device_unregister(&dev->dev);
275 xenbus_dev_fatal(dev, -EINVAL, "saw state %d at frontend",
282 /* ** Connection ** */
285 static void maybe_connect(struct backend_info *be)
287 if (be->netif && (be->frontend_state == XenbusStateConnected))
291 static void xen_net_read_rate(struct xenbus_device *dev,
292 unsigned long *bytes, unsigned long *usec)
298 /* Default to unlimited bandwidth. */
302 ratestr = xenbus_read(XBT_NIL, dev->nodename, "rate", NULL);
307 b = simple_strtoul(s, &e, 10);
308 if ((s == e) || (*e != ','))
312 u = simple_strtoul(s, &e, 10);
313 if ((s == e) || (*e != '\0'))
323 WPRINTK("Failed to parse network rate limit. Traffic unlimited.\n");
327 static int xen_net_read_mac(struct xenbus_device *dev, u8 mac[])
329 char *s, *e, *macstr;
332 macstr = s = xenbus_read(XBT_NIL, dev->nodename, "mac", NULL);
334 return PTR_ERR(macstr);
336 for (i = 0; i < ETH_ALEN; i++) {
337 mac[i] = simple_strtoul(s, &e, 16);
338 if ((s == e) || (*e != ((i == ETH_ALEN-1) ? '\0' : ':'))) {
349 static void connect(struct backend_info *be)
352 struct xenbus_device *dev = be->dev;
354 err = connect_rings(be);
358 err = xen_net_read_mac(dev, be->netif->fe_dev_addr);
360 xenbus_dev_fatal(dev, err, "parsing %s/mac", dev->nodename);
364 xen_net_read_rate(dev, &be->netif->credit_bytes,
365 &be->netif->credit_usec);
366 be->netif->remaining_credit = be->netif->credit_bytes;
368 xenbus_switch_state(dev, XenbusStateConnected);
370 /* May not get a kick from the frontend, so start the tx_queue now. */
371 if (!netbk_can_queue(be->netif->dev))
372 netif_start_queue(be->netif->dev);
376 static int connect_rings(struct backend_info *be)
378 struct xenbus_device *dev = be->dev;
379 unsigned long tx_ring_ref, rx_ring_ref;
380 unsigned int evtchn, rx_copy;
386 err = xenbus_gather(XBT_NIL, dev->otherend,
387 "tx-ring-ref", "%lu", &tx_ring_ref,
388 "rx-ring-ref", "%lu", &rx_ring_ref,
389 "event-channel", "%u", &evtchn, NULL);
391 xenbus_dev_fatal(dev, err,
392 "reading %s/ring-ref and event-channel",
397 err = xenbus_scanf(XBT_NIL, dev->otherend, "request-rx-copy", "%u",
399 if (err == -ENOENT) {
404 xenbus_dev_fatal(dev, err, "reading %s/request-rx-copy",
408 be->netif->copying_receiver = !!rx_copy;
410 if (be->netif->dev->tx_queue_len != 0) {
411 if (xenbus_scanf(XBT_NIL, dev->otherend,
412 "feature-rx-notify", "%d", &val) < 0)
415 be->netif->can_queue = 1;
417 /* Must be non-zero for pfifo_fast to work. */
418 be->netif->dev->tx_queue_len = 1;
421 if (xenbus_scanf(XBT_NIL, dev->otherend, "feature-sg", "%d", &val) < 0)
424 be->netif->features |= NETIF_F_SG;
425 be->netif->dev->features |= NETIF_F_SG;
428 if (xenbus_scanf(XBT_NIL, dev->otherend, "feature-gso-tcpv4", "%d",
432 be->netif->features |= NETIF_F_TSO;
433 be->netif->dev->features |= NETIF_F_TSO;
436 if (xenbus_scanf(XBT_NIL, dev->otherend, "feature-no-csum-offload",
440 be->netif->features &= ~NETIF_F_IP_CSUM;
441 be->netif->dev->features &= ~NETIF_F_IP_CSUM;
444 /* Map the shared frame, irq etc. */
445 err = netif_map(be->netif, tx_ring_ref, rx_ring_ref, evtchn);
447 xenbus_dev_fatal(dev, err,
448 "mapping shared-frames %lu/%lu port %u",
449 tx_ring_ref, rx_ring_ref, evtchn);
456 /* ** Driver Registration ** */
459 static struct xenbus_device_id netback_ids[] = {
465 static struct xenbus_driver netback = {
467 .owner = THIS_MODULE,
469 .probe = netback_probe,
470 .remove = netback_remove,
471 .uevent = netback_uevent,
472 .otherend_changed = frontend_changed,
476 void netif_xenbus_init(void)
478 xenbus_register_backend(&netback);