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 static int connect_rings(struct backend_info *);
33 static void connect(struct backend_info *);
34 static void backend_create_netif(struct backend_info *be);
36 static int netback_remove(struct xenbus_device *dev)
38 struct backend_info *be = dev->dev.driver_data;
40 netback_remove_accelerators(be, dev);
43 netif_disconnect(be->netif);
47 dev->dev.driver_data = NULL;
53 * Entry point to this code when a new device is created. Allocate the basic
54 * structures and switch to InitWait.
56 static int netback_probe(struct xenbus_device *dev,
57 const struct xenbus_device_id *id)
60 struct xenbus_transaction xbt;
63 struct backend_info *be = kzalloc(sizeof(struct backend_info),
66 xenbus_dev_fatal(dev, -ENOMEM,
67 "allocating backend structure");
72 dev->dev.driver_data = be;
75 if (netbk_copy_skb_mode == NETBK_ALWAYS_COPY_SKB)
79 err = xenbus_transaction_start(&xbt);
81 xenbus_dev_fatal(dev, err, "starting transaction");
85 err = xenbus_printf(xbt, dev->nodename, "feature-sg", "%d", sg);
87 message = "writing feature-sg";
88 goto abort_transaction;
91 err = xenbus_printf(xbt, dev->nodename, "feature-gso-tcpv4",
94 message = "writing feature-gso-tcpv4";
95 goto abort_transaction;
98 /* We support rx-copy path. */
99 err = xenbus_printf(xbt, dev->nodename,
100 "feature-rx-copy", "%d", 1);
102 message = "writing feature-rx-copy";
103 goto abort_transaction;
107 * We don't support rx-flip path (except old guests who don't
108 * grok this feature flag).
110 err = xenbus_printf(xbt, dev->nodename,
111 "feature-rx-flip", "%d", 0);
113 message = "writing feature-rx-flip";
114 goto abort_transaction;
117 err = xenbus_transaction_end(xbt, 0);
118 } while (err == -EAGAIN);
121 xenbus_dev_fatal(dev, err, "completing transaction");
125 netback_probe_accelerators(be, dev);
127 err = xenbus_switch_state(dev, XenbusStateInitWait);
131 /* This kicks hotplug scripts, so do it immediately. */
132 backend_create_netif(be);
137 xenbus_transaction_end(xbt, 1);
138 xenbus_dev_fatal(dev, err, "%s", message);
147 * Handle the creation of the hotplug script environment. We add the script
148 * and vif variables to the environment, for the benefit of the vif-* hotplug
151 static int netback_uevent(struct xenbus_device *xdev, struct kobj_uevent_env *env)
153 struct backend_info *be = xdev->dev.driver_data;
154 netif_t *netif = be->netif;
157 DPRINTK("netback_uevent");
159 val = xenbus_read(XBT_NIL, xdev->nodename, "script", NULL);
161 int err = PTR_ERR(val);
162 xenbus_dev_fatal(xdev, err, "reading script");
166 add_uevent_var(env, "script=%s", val);
170 add_uevent_var(env, "vif=%s", netif->dev->name);
176 static void backend_create_netif(struct backend_info *be)
180 struct xenbus_device *dev = be->dev;
182 if (be->netif != NULL)
185 err = xenbus_scanf(XBT_NIL, dev->nodename, "handle", "%li", &handle);
187 xenbus_dev_fatal(dev, err, "reading handle");
191 be->netif = netif_alloc(dev->otherend_id, handle);
192 if (IS_ERR(be->netif)) {
193 err = PTR_ERR(be->netif);
195 xenbus_dev_fatal(dev, err, "creating interface");
199 kobject_uevent(&dev->dev.kobj, KOBJ_ONLINE);
204 * Callback received when the frontend's state changes.
206 static void frontend_changed(struct xenbus_device *dev,
207 enum xenbus_state frontend_state)
209 struct backend_info *be = dev->dev.driver_data;
211 DPRINTK("%s", xenbus_strstate(frontend_state));
213 be->frontend_state = frontend_state;
215 switch (frontend_state) {
216 case XenbusStateInitialising:
217 if (dev->state == XenbusStateClosed) {
218 printk(KERN_INFO "%s: %s: prepare for reconnect\n",
219 __FUNCTION__, dev->nodename);
220 xenbus_switch_state(dev, XenbusStateInitWait);
224 case XenbusStateInitialised:
227 case XenbusStateConnected:
228 backend_create_netif(be);
233 case XenbusStateClosing:
235 netif_disconnect(be->netif);
238 xenbus_switch_state(dev, XenbusStateClosing);
241 case XenbusStateClosed:
242 xenbus_switch_state(dev, XenbusStateClosed);
243 if (xenbus_dev_is_online(dev))
245 /* fall through if not online */
246 case XenbusStateUnknown:
247 if (be->netif != NULL)
248 kobject_uevent(&dev->dev.kobj, KOBJ_OFFLINE);
249 device_unregister(&dev->dev);
253 xenbus_dev_fatal(dev, -EINVAL, "saw state %d at frontend",
260 static void xen_net_read_rate(struct xenbus_device *dev,
261 unsigned long *bytes, unsigned long *usec)
267 /* Default to unlimited bandwidth. */
271 ratestr = xenbus_read(XBT_NIL, dev->nodename, "rate", NULL);
276 b = simple_strtoul(s, &e, 10);
277 if ((s == e) || (*e != ','))
281 u = simple_strtoul(s, &e, 10);
282 if ((s == e) || (*e != '\0'))
292 WPRINTK("Failed to parse network rate limit. Traffic unlimited.\n");
296 static int xen_net_read_mac(struct xenbus_device *dev, u8 mac[])
298 char *s, *e, *macstr;
301 macstr = s = xenbus_read(XBT_NIL, dev->nodename, "mac", NULL);
303 return PTR_ERR(macstr);
305 for (i = 0; i < ETH_ALEN; i++) {
306 mac[i] = simple_strtoul(s, &e, 16);
307 if ((s == e) || (*e != ((i == ETH_ALEN-1) ? '\0' : ':'))) {
318 static void connect(struct backend_info *be)
321 struct xenbus_device *dev = be->dev;
323 err = connect_rings(be);
327 err = xen_net_read_mac(dev, be->netif->fe_dev_addr);
329 xenbus_dev_fatal(dev, err, "parsing %s/mac", dev->nodename);
333 xen_net_read_rate(dev, &be->netif->credit_bytes,
334 &be->netif->credit_usec);
335 be->netif->remaining_credit = be->netif->credit_bytes;
337 xenbus_switch_state(dev, XenbusStateConnected);
339 netif_wake_queue(be->netif->dev);
343 static int connect_rings(struct backend_info *be)
345 struct xenbus_device *dev = be->dev;
346 unsigned long tx_ring_ref, rx_ring_ref;
347 unsigned int evtchn, rx_copy;
353 err = xenbus_gather(XBT_NIL, dev->otherend,
354 "tx-ring-ref", "%lu", &tx_ring_ref,
355 "rx-ring-ref", "%lu", &rx_ring_ref,
356 "event-channel", "%u", &evtchn, NULL);
358 xenbus_dev_fatal(dev, err,
359 "reading %s/ring-ref and event-channel",
364 err = xenbus_scanf(XBT_NIL, dev->otherend, "request-rx-copy", "%u",
366 if (err == -ENOENT) {
371 xenbus_dev_fatal(dev, err, "reading %s/request-rx-copy",
375 be->netif->copying_receiver = !!rx_copy;
377 if (be->netif->dev->tx_queue_len != 0) {
378 if (xenbus_scanf(XBT_NIL, dev->otherend,
379 "feature-rx-notify", "%d", &val) < 0)
382 be->netif->can_queue = 1;
384 /* Must be non-zero for pfifo_fast to work. */
385 be->netif->dev->tx_queue_len = 1;
388 if (xenbus_scanf(XBT_NIL, dev->otherend, "feature-sg", "%d", &val) < 0)
391 be->netif->features |= NETIF_F_SG;
392 be->netif->dev->features |= NETIF_F_SG;
395 if (xenbus_scanf(XBT_NIL, dev->otherend, "feature-gso-tcpv4", "%d",
399 be->netif->features |= NETIF_F_TSO;
400 be->netif->dev->features |= NETIF_F_TSO;
403 if (xenbus_scanf(XBT_NIL, dev->otherend, "feature-no-csum-offload",
407 be->netif->features &= ~NETIF_F_IP_CSUM;
408 be->netif->dev->features &= ~NETIF_F_IP_CSUM;
411 /* Map the shared frame, irq etc. */
412 err = netif_map(be->netif, tx_ring_ref, rx_ring_ref, evtchn);
414 xenbus_dev_fatal(dev, err,
415 "mapping shared-frames %lu/%lu port %u",
416 tx_ring_ref, rx_ring_ref, evtchn);
423 /* ** Driver Registration ** */
426 static struct xenbus_device_id netback_ids[] = {
432 static struct xenbus_driver netback = {
435 .probe = netback_probe,
436 .remove = netback_remove,
437 .uevent = netback_uevent,
438 .otherend_changed = frontend_changed,
442 void netif_xenbus_init(void)
444 if (xenbus_register_backend(&netback))