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_transaction_end(xbt, 0);
112 } while (err == -EAGAIN);
115 xenbus_dev_fatal(dev, err, "completing transaction");
119 err = xenbus_switch_state(dev, XenbusStateInitWait);
127 xenbus_transaction_end(xbt, 1);
128 xenbus_dev_fatal(dev, err, "%s", message);
137 * Handle the creation of the hotplug script environment. We add the script
138 * and vif variables to the environment, for the benefit of the vif-* hotplug
141 static int netback_uevent(struct xenbus_device *xdev, char **envp,
142 int num_envp, char *buffer, int buffer_size)
144 struct backend_info *be = xdev->dev.driver_data;
145 netif_t *netif = be->netif;
146 int i = 0, length = 0;
149 DPRINTK("netback_uevent");
151 val = xenbus_read(XBT_NIL, xdev->nodename, "script", NULL);
153 int err = PTR_ERR(val);
154 xenbus_dev_fatal(xdev, err, "reading script");
158 add_uevent_var(envp, num_envp, &i, buffer, buffer_size,
159 &length, "script=%s", val);
163 add_uevent_var(envp, num_envp, &i, buffer, buffer_size, &length,
164 "vif=%s", netif->dev->name);
173 * Callback received when the hotplug scripts have placed the handle node.
174 * Read it, and create a netif structure. If the frontend is ready, connect.
176 static void backend_changed(struct xenbus_watch *watch,
177 const char **vec, unsigned int len)
181 struct backend_info *be
182 = container_of(watch, struct backend_info, backend_watch);
183 struct xenbus_device *dev = be->dev;
187 err = xenbus_scanf(XBT_NIL, dev->nodename, "handle", "%li", &handle);
188 if (XENBUS_EXIST_ERR(err)) {
189 /* Since this watch will fire once immediately after it is
190 registered, we expect this. Ignore it, and wait for the
195 xenbus_dev_fatal(dev, err, "reading handle");
199 if (be->netif == NULL) {
200 u8 be_mac[ETH_ALEN] = { 0, 0, 0, 0, 0, 0 };
202 be->netif = netif_alloc(dev->otherend_id, handle, be_mac);
203 if (IS_ERR(be->netif)) {
204 err = PTR_ERR(be->netif);
206 xenbus_dev_fatal(dev, err, "creating interface");
210 kobject_uevent(&dev->dev.kobj, KOBJ_ONLINE);
218 * Callback received when the frontend's state changes.
220 static void frontend_changed(struct xenbus_device *dev,
221 enum xenbus_state frontend_state)
223 struct backend_info *be = dev->dev.driver_data;
227 be->frontend_state = frontend_state;
229 switch (frontend_state) {
230 case XenbusStateInitialising:
231 case XenbusStateInitialised:
234 case XenbusStateConnected:
238 case XenbusStateClosing:
239 xenbus_switch_state(dev, XenbusStateClosing);
242 case XenbusStateClosed:
243 if (be->netif != NULL)
244 kobject_uevent(&dev->dev.kobj, KOBJ_OFFLINE);
245 device_unregister(&dev->dev);
248 case XenbusStateUnknown:
249 case XenbusStateInitWait:
251 xenbus_dev_fatal(dev, -EINVAL, "saw state %d at frontend",
258 /* ** Connection ** */
261 static void maybe_connect(struct backend_info *be)
263 if (be->netif && (be->frontend_state == XenbusStateConnected))
267 static void xen_net_read_rate(struct xenbus_device *dev,
268 unsigned long *bytes, unsigned long *usec)
274 /* Default to unlimited bandwidth. */
278 ratestr = xenbus_read(XBT_NIL, dev->nodename, "rate", NULL);
283 b = simple_strtoul(s, &e, 10);
284 if ((s == e) || (*e != ','))
288 u = simple_strtoul(s, &e, 10);
289 if ((s == e) || (*e != '\0'))
299 WPRINTK("Failed to parse network rate limit. Traffic unlimited.\n");
303 static int xen_net_read_mac(struct xenbus_device *dev, u8 mac[])
305 char *s, *e, *macstr;
308 macstr = s = xenbus_read(XBT_NIL, dev->nodename, "mac", NULL);
310 return PTR_ERR(macstr);
312 for (i = 0; i < ETH_ALEN; i++) {
313 mac[i] = simple_strtoul(s, &e, 16);
314 if ((s == e) || (*e != ((i == ETH_ALEN-1) ? '\0' : ':'))) {
325 static void connect(struct backend_info *be)
328 struct xenbus_device *dev = be->dev;
330 err = connect_rings(be);
334 err = xen_net_read_mac(dev, be->netif->fe_dev_addr);
336 xenbus_dev_fatal(dev, err, "parsing %s/mac", dev->nodename);
340 xen_net_read_rate(dev, &be->netif->credit_bytes,
341 &be->netif->credit_usec);
342 be->netif->remaining_credit = be->netif->credit_bytes;
344 xenbus_switch_state(dev, XenbusStateConnected);
348 static int connect_rings(struct backend_info *be)
350 struct xenbus_device *dev = be->dev;
351 unsigned long tx_ring_ref, rx_ring_ref;
358 err = xenbus_gather(XBT_NIL, dev->otherend,
359 "tx-ring-ref", "%lu", &tx_ring_ref,
360 "rx-ring-ref", "%lu", &rx_ring_ref,
361 "event-channel", "%u", &evtchn, NULL);
363 xenbus_dev_fatal(dev, err,
364 "reading %s/ring-ref and event-channel",
369 if (xenbus_scanf(XBT_NIL, dev->otherend, "feature-rx-notify", "%d",
373 be->netif->can_queue = 1;
375 /* Must be non-zero for pfifo_fast to work. */
376 be->netif->dev->tx_queue_len = 1;
378 if (xenbus_scanf(XBT_NIL, dev->otherend, "feature-sg", "%d", &val) < 0)
381 be->netif->features |= NETIF_F_SG;
382 be->netif->dev->features |= NETIF_F_SG;
385 if (xenbus_scanf(XBT_NIL, dev->otherend, "feature-gso-tcpv4", "%d",
389 be->netif->features |= NETIF_F_TSO;
390 be->netif->dev->features |= NETIF_F_TSO;
393 /* Map the shared frame, irq etc. */
394 err = netif_map(be->netif, tx_ring_ref, rx_ring_ref, evtchn);
396 xenbus_dev_fatal(dev, err,
397 "mapping shared-frames %lu/%lu port %u",
398 tx_ring_ref, rx_ring_ref, evtchn);
405 /* ** Driver Registration ** */
408 static struct xenbus_device_id netback_ids[] = {
414 static struct xenbus_driver netback = {
416 .owner = THIS_MODULE,
418 .probe = netback_probe,
419 .remove = netback_remove,
420 .uevent = netback_uevent,
421 .otherend_changed = frontend_changed,
425 void netif_xenbus_init(void)
427 xenbus_register_backend(&netback);