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 err = xenbus_printf(xbt, dev->nodename,
97 "feature-rx-copy", "%d", 1);
99 message = "writing feature-copying";
100 goto abort_transaction;
103 err = xenbus_transaction_end(xbt, 0);
104 } while (err == -EAGAIN);
107 xenbus_dev_fatal(dev, err, "completing transaction");
111 err = xenbus_switch_state(dev, XenbusStateInitWait);
115 /* This kicks hotplug scripts, so do it immediately. */
116 backend_create_netif(be);
121 xenbus_transaction_end(xbt, 1);
122 xenbus_dev_fatal(dev, err, "%s", message);
131 * Handle the creation of the hotplug script environment. We add the script
132 * and vif variables to the environment, for the benefit of the vif-* hotplug
135 static int netback_uevent(struct xenbus_device *xdev, char **envp,
136 int num_envp, char *buffer, int buffer_size)
138 struct backend_info *be = xdev->dev.driver_data;
139 netif_t *netif = be->netif;
140 int i = 0, length = 0;
143 DPRINTK("netback_uevent");
145 val = xenbus_read(XBT_NIL, xdev->nodename, "script", NULL);
147 int err = PTR_ERR(val);
148 xenbus_dev_fatal(xdev, err, "reading script");
152 add_uevent_var(envp, num_envp, &i, buffer, buffer_size,
153 &length, "script=%s", val);
157 add_uevent_var(envp, num_envp, &i, buffer, buffer_size, &length,
158 "vif=%s", netif->dev->name);
166 static void backend_create_netif(struct backend_info *be)
170 struct xenbus_device *dev = be->dev;
172 if (be->netif != NULL)
175 err = xenbus_scanf(XBT_NIL, dev->nodename, "handle", "%li", &handle);
177 xenbus_dev_fatal(dev, err, "reading handle");
181 be->netif = netif_alloc(dev->otherend_id, handle);
182 if (IS_ERR(be->netif)) {
183 err = PTR_ERR(be->netif);
185 xenbus_dev_fatal(dev, err, "creating interface");
189 kobject_uevent(&dev->dev.kobj, KOBJ_ONLINE);
194 * Callback received when the frontend's state changes.
196 static void frontend_changed(struct xenbus_device *dev,
197 enum xenbus_state frontend_state)
199 struct backend_info *be = dev->dev.driver_data;
201 DPRINTK("%s", xenbus_strstate(frontend_state));
203 be->frontend_state = frontend_state;
205 switch (frontend_state) {
206 case XenbusStateInitialising:
207 if (dev->state == XenbusStateClosed) {
208 printk("%s: %s: prepare for reconnect\n",
209 __FUNCTION__, dev->nodename);
211 netif_disconnect(be->netif);
214 xenbus_switch_state(dev, XenbusStateInitWait);
218 case XenbusStateInitialised:
221 case XenbusStateConnected:
222 backend_create_netif(be);
227 case XenbusStateClosing:
228 xenbus_switch_state(dev, XenbusStateClosing);
231 case XenbusStateClosed:
232 xenbus_switch_state(dev, XenbusStateClosed);
233 if (xenbus_dev_is_online(dev))
235 /* fall through if not online */
236 case XenbusStateUnknown:
237 if (be->netif != NULL)
238 kobject_uevent(&dev->dev.kobj, KOBJ_OFFLINE);
239 device_unregister(&dev->dev);
243 xenbus_dev_fatal(dev, -EINVAL, "saw state %d at frontend",
250 static void xen_net_read_rate(struct xenbus_device *dev,
251 unsigned long *bytes, unsigned long *usec)
257 /* Default to unlimited bandwidth. */
261 ratestr = xenbus_read(XBT_NIL, dev->nodename, "rate", NULL);
266 b = simple_strtoul(s, &e, 10);
267 if ((s == e) || (*e != ','))
271 u = simple_strtoul(s, &e, 10);
272 if ((s == e) || (*e != '\0'))
282 WPRINTK("Failed to parse network rate limit. Traffic unlimited.\n");
286 static int xen_net_read_mac(struct xenbus_device *dev, u8 mac[])
288 char *s, *e, *macstr;
291 macstr = s = xenbus_read(XBT_NIL, dev->nodename, "mac", NULL);
293 return PTR_ERR(macstr);
295 for (i = 0; i < ETH_ALEN; i++) {
296 mac[i] = simple_strtoul(s, &e, 16);
297 if ((s == e) || (*e != ((i == ETH_ALEN-1) ? '\0' : ':'))) {
308 static void connect(struct backend_info *be)
311 struct xenbus_device *dev = be->dev;
313 err = connect_rings(be);
317 err = xen_net_read_mac(dev, be->netif->fe_dev_addr);
319 xenbus_dev_fatal(dev, err, "parsing %s/mac", dev->nodename);
323 xen_net_read_rate(dev, &be->netif->credit_bytes,
324 &be->netif->credit_usec);
325 be->netif->remaining_credit = be->netif->credit_bytes;
327 xenbus_switch_state(dev, XenbusStateConnected);
329 /* May not get a kick from the frontend, so start the tx_queue now. */
330 if (!netbk_can_queue(be->netif->dev))
331 netif_wake_queue(be->netif->dev);
335 static int connect_rings(struct backend_info *be)
337 struct xenbus_device *dev = be->dev;
338 unsigned long tx_ring_ref, rx_ring_ref;
339 unsigned int evtchn, rx_copy;
345 err = xenbus_gather(XBT_NIL, dev->otherend,
346 "tx-ring-ref", "%lu", &tx_ring_ref,
347 "rx-ring-ref", "%lu", &rx_ring_ref,
348 "event-channel", "%u", &evtchn, NULL);
350 xenbus_dev_fatal(dev, err,
351 "reading %s/ring-ref and event-channel",
356 err = xenbus_scanf(XBT_NIL, dev->otherend, "request-rx-copy", "%u",
358 if (err == -ENOENT) {
363 xenbus_dev_fatal(dev, err, "reading %s/request-rx-copy",
367 be->netif->copying_receiver = !!rx_copy;
369 if (be->netif->dev->tx_queue_len != 0) {
370 if (xenbus_scanf(XBT_NIL, dev->otherend,
371 "feature-rx-notify", "%d", &val) < 0)
374 be->netif->can_queue = 1;
376 /* Must be non-zero for pfifo_fast to work. */
377 be->netif->dev->tx_queue_len = 1;
380 if (xenbus_scanf(XBT_NIL, dev->otherend, "feature-sg", "%d", &val) < 0)
383 be->netif->features |= NETIF_F_SG;
384 be->netif->dev->features |= NETIF_F_SG;
387 if (xenbus_scanf(XBT_NIL, dev->otherend, "feature-gso-tcpv4", "%d",
391 be->netif->features |= NETIF_F_TSO;
392 be->netif->dev->features |= NETIF_F_TSO;
395 if (xenbus_scanf(XBT_NIL, dev->otherend, "feature-no-csum-offload",
399 be->netif->features &= ~NETIF_F_IP_CSUM;
400 be->netif->dev->features &= ~NETIF_F_IP_CSUM;
403 /* Map the shared frame, irq etc. */
404 err = netif_map(be->netif, tx_ring_ref, rx_ring_ref, evtchn);
406 xenbus_dev_fatal(dev, err,
407 "mapping shared-frames %lu/%lu port %u",
408 tx_ring_ref, rx_ring_ref, evtchn);
415 /* ** Driver Registration ** */
418 static struct xenbus_device_id netback_ids[] = {
424 static struct xenbus_driver netback = {
426 .owner = THIS_MODULE,
428 .probe = netback_probe,
429 .remove = netback_remove,
430 .uevent = netback_uevent,
431 .otherend_changed = frontend_changed,
435 void netif_xenbus_init(void)
437 xenbus_register_backend(&netback);