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)
32 struct xenbus_device *dev;
34 enum xenbus_state frontend_state;
37 static int connect_rings(struct backend_info *);
38 static void connect(struct backend_info *);
39 static void backend_create_netif(struct backend_info *be);
41 static int netback_remove(struct xenbus_device *dev)
43 struct backend_info *be = dev->dev.driver_data;
46 netif_disconnect(be->netif);
50 dev->dev.driver_data = NULL;
56 * Entry point to this code when a new device is created. Allocate the basic
57 * structures and switch to InitWait.
59 static int netback_probe(struct xenbus_device *dev,
60 const struct xenbus_device_id *id)
63 struct xenbus_transaction xbt;
65 struct backend_info *be = kzalloc(sizeof(struct backend_info),
68 xenbus_dev_fatal(dev, -ENOMEM,
69 "allocating backend structure");
74 dev->dev.driver_data = be;
77 err = xenbus_transaction_start(&xbt);
79 xenbus_dev_fatal(dev, err, "starting transaction");
83 err = xenbus_printf(xbt, dev->nodename, "feature-sg", "%d", 1);
85 message = "writing feature-sg";
86 goto abort_transaction;
89 err = xenbus_printf(xbt, dev->nodename, "feature-gso-tcpv4",
92 message = "writing feature-gso-tcpv4";
93 goto abort_transaction;
96 /* We support rx-copy path. */
97 err = xenbus_printf(xbt, dev->nodename,
98 "feature-rx-copy", "%d", 1);
100 message = "writing feature-rx-copy";
101 goto abort_transaction;
105 * We don't support rx-flip path (except old guests who don't
106 * grok this feature flag).
108 err = xenbus_printf(xbt, dev->nodename,
109 "feature-rx-flip", "%d", 0);
111 message = "writing feature-rx-flip";
112 goto abort_transaction;
115 err = xenbus_transaction_end(xbt, 0);
116 } while (err == -EAGAIN);
119 xenbus_dev_fatal(dev, err, "completing transaction");
123 err = xenbus_switch_state(dev, XenbusStateInitWait);
127 /* This kicks hotplug scripts, so do it immediately. */
128 backend_create_netif(be);
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);
178 static void backend_create_netif(struct backend_info *be)
182 struct xenbus_device *dev = be->dev;
184 if (be->netif != NULL)
187 err = xenbus_scanf(XBT_NIL, dev->nodename, "handle", "%li", &handle);
189 xenbus_dev_fatal(dev, err, "reading handle");
193 be->netif = netif_alloc(dev->otherend_id, handle);
194 if (IS_ERR(be->netif)) {
195 err = PTR_ERR(be->netif);
197 xenbus_dev_fatal(dev, err, "creating interface");
201 kobject_uevent(&dev->dev.kobj, KOBJ_ONLINE);
206 * Callback received when the frontend's state changes.
208 static void frontend_changed(struct xenbus_device *dev,
209 enum xenbus_state frontend_state)
211 struct backend_info *be = dev->dev.driver_data;
213 DPRINTK("%s", xenbus_strstate(frontend_state));
215 be->frontend_state = frontend_state;
217 switch (frontend_state) {
218 case XenbusStateInitialising:
219 if (dev->state == XenbusStateClosed) {
220 printk(KERN_INFO "%s: %s: prepare for reconnect\n",
221 __FUNCTION__, dev->nodename);
223 netif_disconnect(be->netif);
226 xenbus_switch_state(dev, XenbusStateInitWait);
230 case XenbusStateInitialised:
233 case XenbusStateConnected:
234 backend_create_netif(be);
239 case XenbusStateClosing:
240 xenbus_switch_state(dev, XenbusStateClosing);
243 case XenbusStateClosed:
244 xenbus_switch_state(dev, XenbusStateClosed);
245 if (xenbus_dev_is_online(dev))
247 /* fall through if not online */
248 case XenbusStateUnknown:
249 if (be->netif != NULL)
250 kobject_uevent(&dev->dev.kobj, KOBJ_OFFLINE);
251 device_unregister(&dev->dev);
255 xenbus_dev_fatal(dev, -EINVAL, "saw state %d at frontend",
262 static void xen_net_read_rate(struct xenbus_device *dev,
263 unsigned long *bytes, unsigned long *usec)
269 /* Default to unlimited bandwidth. */
273 ratestr = xenbus_read(XBT_NIL, dev->nodename, "rate", NULL);
278 b = simple_strtoul(s, &e, 10);
279 if ((s == e) || (*e != ','))
283 u = simple_strtoul(s, &e, 10);
284 if ((s == e) || (*e != '\0'))
294 WPRINTK("Failed to parse network rate limit. Traffic unlimited.\n");
298 static int xen_net_read_mac(struct xenbus_device *dev, u8 mac[])
300 char *s, *e, *macstr;
303 macstr = s = xenbus_read(XBT_NIL, dev->nodename, "mac", NULL);
305 return PTR_ERR(macstr);
307 for (i = 0; i < ETH_ALEN; i++) {
308 mac[i] = simple_strtoul(s, &e, 16);
309 if ((s == e) || (*e != ((i == ETH_ALEN-1) ? '\0' : ':'))) {
320 static void connect(struct backend_info *be)
323 struct xenbus_device *dev = be->dev;
325 err = connect_rings(be);
329 err = xen_net_read_mac(dev, be->netif->fe_dev_addr);
331 xenbus_dev_fatal(dev, err, "parsing %s/mac", dev->nodename);
335 xen_net_read_rate(dev, &be->netif->credit_bytes,
336 &be->netif->credit_usec);
337 be->netif->remaining_credit = be->netif->credit_bytes;
339 xenbus_switch_state(dev, XenbusStateConnected);
341 netif_wake_queue(be->netif->dev);
345 static int connect_rings(struct backend_info *be)
347 struct xenbus_device *dev = be->dev;
348 unsigned long tx_ring_ref, rx_ring_ref;
349 unsigned int evtchn, rx_copy;
355 err = xenbus_gather(XBT_NIL, dev->otherend,
356 "tx-ring-ref", "%lu", &tx_ring_ref,
357 "rx-ring-ref", "%lu", &rx_ring_ref,
358 "event-channel", "%u", &evtchn, NULL);
360 xenbus_dev_fatal(dev, err,
361 "reading %s/ring-ref and event-channel",
366 err = xenbus_scanf(XBT_NIL, dev->otherend, "request-rx-copy", "%u",
368 if (err == -ENOENT) {
373 xenbus_dev_fatal(dev, err, "reading %s/request-rx-copy",
377 be->netif->copying_receiver = !!rx_copy;
379 if (be->netif->dev->tx_queue_len != 0) {
380 if (xenbus_scanf(XBT_NIL, dev->otherend,
381 "feature-rx-notify", "%d", &val) < 0)
384 be->netif->can_queue = 1;
386 /* Must be non-zero for pfifo_fast to work. */
387 be->netif->dev->tx_queue_len = 1;
390 if (xenbus_scanf(XBT_NIL, dev->otherend, "feature-sg", "%d", &val) < 0)
393 be->netif->features |= NETIF_F_SG;
394 be->netif->dev->features |= NETIF_F_SG;
397 if (xenbus_scanf(XBT_NIL, dev->otherend, "feature-gso-tcpv4", "%d",
401 be->netif->features |= NETIF_F_TSO;
402 be->netif->dev->features |= NETIF_F_TSO;
405 if (xenbus_scanf(XBT_NIL, dev->otherend, "feature-no-csum-offload",
409 be->netif->features &= ~NETIF_F_IP_CSUM;
410 be->netif->dev->features &= ~NETIF_F_IP_CSUM;
413 /* Map the shared frame, irq etc. */
414 err = netif_map(be->netif, tx_ring_ref, rx_ring_ref, evtchn);
416 xenbus_dev_fatal(dev, err,
417 "mapping shared-frames %lu/%lu port %u",
418 tx_ring_ref, rx_ring_ref, evtchn);
425 /* ** Driver Registration ** */
428 static struct xenbus_device_id netback_ids[] = {
434 static struct xenbus_driver netback = {
436 .owner = THIS_MODULE,
438 .probe = netback_probe,
439 .remove = netback_remove,
440 .uevent = netback_uevent,
441 .otherend_changed = frontend_changed,
445 void netif_xenbus_init(void)
447 xenbus_register_backend(&netback);