ia64/xen-unstable

view linux-2.6-xen-sparse/drivers/xen/netback/xenbus.c @ 11421:b219ea61a357

Add support for netif frontends which don't support checksum offload.

Signed-off-by: Steven Smith <sos22@cam.ac.uk>
author ssmith@weybridge.uk.xensource.com
date Mon Sep 04 16:38:24 2006 +0100 (2006-09-04)
parents 98a81d2ccf4c
children 224b80785d02
line source
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
18 */
20 #include <stdarg.h>
21 #include <linux/module.h>
22 #include <xen/xenbus.h>
23 #include "common.h"
25 #if 0
26 #undef DPRINTK
27 #define DPRINTK(fmt, args...) \
28 printk("netback/xenbus (%s:%d) " fmt ".\n", __FUNCTION__, __LINE__, ##args)
29 #endif
31 struct backend_info
32 {
33 struct xenbus_device *dev;
34 netif_t *netif;
35 struct xenbus_watch backend_watch;
36 enum xenbus_state frontend_state;
37 };
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 **,
43 unsigned int);
45 static int netback_remove(struct xenbus_device *dev)
46 {
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;
53 }
54 if (be->netif) {
55 netif_disconnect(be->netif);
56 be->netif = NULL;
57 }
58 kfree(be);
59 dev->dev.driver_data = NULL;
60 return 0;
61 }
64 /**
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.
68 */
69 static int netback_probe(struct xenbus_device *dev,
70 const struct xenbus_device_id *id)
71 {
72 const char *message;
73 struct xenbus_transaction xbt;
74 int err;
75 struct backend_info *be = kzalloc(sizeof(struct backend_info),
76 GFP_KERNEL);
77 if (!be) {
78 xenbus_dev_fatal(dev, -ENOMEM,
79 "allocating backend structure");
80 return -ENOMEM;
81 }
83 be->dev = dev;
84 dev->dev.driver_data = be;
86 err = xenbus_watch_path2(dev, dev->nodename, "handle",
87 &be->backend_watch, backend_changed);
88 if (err)
89 goto fail;
91 do {
92 err = xenbus_transaction_start(&xbt);
93 if (err) {
94 xenbus_dev_fatal(dev, err, "starting transaction");
95 goto fail;
96 }
98 err = xenbus_printf(xbt, dev->nodename, "feature-sg", "%d", 1);
99 if (err) {
100 message = "writing feature-sg";
101 goto abort_transaction;
102 }
104 err = xenbus_printf(xbt, dev->nodename, "feature-gso-tcpv4",
105 "%d", 1);
106 if (err) {
107 message = "writing feature-gso-tcpv4";
108 goto abort_transaction;
109 }
111 err = xenbus_printf(xbt, dev->nodename, "feature-rx-copy", "%d", 1);
112 if (err) {
113 message = "writing feature-copying";
114 goto abort_transaction;
115 }
117 err = xenbus_transaction_end(xbt, 0);
118 } while (err == -EAGAIN);
120 if (err) {
121 xenbus_dev_fatal(dev, err, "completing transaction");
122 goto fail;
123 }
125 err = xenbus_switch_state(dev, XenbusStateInitWait);
126 if (err) {
127 goto fail;
128 }
130 return 0;
132 abort_transaction:
133 xenbus_transaction_end(xbt, 1);
134 xenbus_dev_fatal(dev, err, "%s", message);
135 fail:
136 DPRINTK("failed");
137 netback_remove(dev);
138 return err;
139 }
142 /**
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
145 * scripts.
146 */
147 static int netback_uevent(struct xenbus_device *xdev, char **envp,
148 int num_envp, char *buffer, int buffer_size)
149 {
150 struct backend_info *be = xdev->dev.driver_data;
151 netif_t *netif = be->netif;
152 int i = 0, length = 0;
153 char *val;
155 DPRINTK("netback_uevent");
157 val = xenbus_read(XBT_NIL, xdev->nodename, "script", NULL);
158 if (IS_ERR(val)) {
159 int err = PTR_ERR(val);
160 xenbus_dev_fatal(xdev, err, "reading script");
161 return err;
162 }
163 else {
164 add_uevent_var(envp, num_envp, &i, buffer, buffer_size,
165 &length, "script=%s", val);
166 kfree(val);
167 }
169 add_uevent_var(envp, num_envp, &i, buffer, buffer_size, &length,
170 "vif=%s", netif->dev->name);
172 envp[i] = NULL;
174 return 0;
175 }
178 /**
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.
181 */
182 static void backend_changed(struct xenbus_watch *watch,
183 const char **vec, unsigned int len)
184 {
185 int err;
186 long handle;
187 struct backend_info *be
188 = container_of(watch, struct backend_info, backend_watch);
189 struct xenbus_device *dev = be->dev;
191 DPRINTK("");
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
197 hotplug scripts. */
198 return;
199 }
200 if (err != 1) {
201 xenbus_dev_fatal(dev, err, "reading handle");
202 return;
203 }
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);
211 be->netif = NULL;
212 xenbus_dev_fatal(dev, err, "creating interface");
213 return;
214 }
216 kobject_uevent(&dev->dev.kobj, KOBJ_ONLINE);
218 maybe_connect(be);
219 }
220 }
223 /**
224 * Callback received when the frontend's state changes.
225 */
226 static void frontend_changed(struct xenbus_device *dev,
227 enum xenbus_state frontend_state)
228 {
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);
240 if (be->netif) {
241 netif_disconnect(be->netif);
242 be->netif = NULL;
243 }
244 xenbus_switch_state(dev, XenbusStateInitWait);
245 }
246 break;
248 case XenbusStateInitialised:
249 break;
251 case XenbusStateConnected:
252 if (!be->netif) {
253 /* reconnect: setup be->netif */
254 backend_changed(&be->backend_watch, NULL, 0);
255 }
256 maybe_connect(be);
257 break;
259 case XenbusStateClosing:
260 xenbus_switch_state(dev, XenbusStateClosing);
261 break;
263 case XenbusStateClosed:
264 xenbus_switch_state(dev, XenbusStateClosed);
265 if (xenbus_dev_is_online(dev))
266 break;
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);
272 break;
274 default:
275 xenbus_dev_fatal(dev, -EINVAL, "saw state %d at frontend",
276 frontend_state);
277 break;
278 }
279 }
282 /* ** Connection ** */
285 static void maybe_connect(struct backend_info *be)
286 {
287 if (be->netif && (be->frontend_state == XenbusStateConnected))
288 connect(be);
289 }
291 static void xen_net_read_rate(struct xenbus_device *dev,
292 unsigned long *bytes, unsigned long *usec)
293 {
294 char *s, *e;
295 unsigned long b, u;
296 char *ratestr;
298 /* Default to unlimited bandwidth. */
299 *bytes = ~0UL;
300 *usec = 0;
302 ratestr = xenbus_read(XBT_NIL, dev->nodename, "rate", NULL);
303 if (IS_ERR(ratestr))
304 return;
306 s = ratestr;
307 b = simple_strtoul(s, &e, 10);
308 if ((s == e) || (*e != ','))
309 goto fail;
311 s = e + 1;
312 u = simple_strtoul(s, &e, 10);
313 if ((s == e) || (*e != '\0'))
314 goto fail;
316 *bytes = b;
317 *usec = u;
319 kfree(ratestr);
320 return;
322 fail:
323 WPRINTK("Failed to parse network rate limit. Traffic unlimited.\n");
324 kfree(ratestr);
325 }
327 static int xen_net_read_mac(struct xenbus_device *dev, u8 mac[])
328 {
329 char *s, *e, *macstr;
330 int i;
332 macstr = s = xenbus_read(XBT_NIL, dev->nodename, "mac", NULL);
333 if (IS_ERR(macstr))
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' : ':'))) {
339 kfree(macstr);
340 return -ENOENT;
341 }
342 s = e+1;
343 }
345 kfree(macstr);
346 return 0;
347 }
349 static void connect(struct backend_info *be)
350 {
351 int err;
352 struct xenbus_device *dev = be->dev;
354 err = connect_rings(be);
355 if (err)
356 return;
358 err = xen_net_read_mac(dev, be->netif->fe_dev_addr);
359 if (err) {
360 xenbus_dev_fatal(dev, err, "parsing %s/mac", dev->nodename);
361 return;
362 }
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);
369 }
372 static int connect_rings(struct backend_info *be)
373 {
374 struct xenbus_device *dev = be->dev;
375 unsigned long tx_ring_ref, rx_ring_ref;
376 unsigned int evtchn, rx_copy;
377 int err;
378 int val;
380 DPRINTK("");
382 err = xenbus_gather(XBT_NIL, dev->otherend,
383 "tx-ring-ref", "%lu", &tx_ring_ref,
384 "rx-ring-ref", "%lu", &rx_ring_ref,
385 "event-channel", "%u", &evtchn, NULL);
386 if (err) {
387 xenbus_dev_fatal(dev, err,
388 "reading %s/ring-ref and event-channel",
389 dev->otherend);
390 return err;
391 }
393 err = xenbus_scanf(XBT_NIL, dev->otherend, "request-rx-copy", "%u",
394 &rx_copy);
395 if (err == -ENOENT) {
396 err = 0;
397 rx_copy = 0;
398 }
399 if (err < 0) {
400 xenbus_dev_fatal(dev, err, "reading %s/request-rx-copy",
401 dev->otherend);
402 return err;
403 }
404 be->netif->copying_receiver = !!rx_copy;
406 if (xenbus_scanf(XBT_NIL, dev->otherend, "feature-rx-notify", "%d",
407 &val) < 0)
408 val = 0;
409 if (val)
410 be->netif->can_queue = 1;
411 else
412 /* Must be non-zero for pfifo_fast to work. */
413 be->netif->dev->tx_queue_len = 1;
415 if (xenbus_scanf(XBT_NIL, dev->otherend, "feature-sg", "%d", &val) < 0)
416 val = 0;
417 if (val) {
418 be->netif->features |= NETIF_F_SG;
419 be->netif->dev->features |= NETIF_F_SG;
420 }
422 if (xenbus_scanf(XBT_NIL, dev->otherend, "feature-gso-tcpv4", "%d",
423 &val) < 0)
424 val = 0;
425 if (val) {
426 be->netif->features |= NETIF_F_TSO;
427 be->netif->dev->features |= NETIF_F_TSO;
428 }
430 if (xenbus_scanf(XBT_NIL, dev->otherend, "feature-no-csum-offload",
431 "%d", &val) < 0)
432 val = 0;
433 if (val) {
434 be->netif->features &= ~NETIF_F_IP_CSUM;
435 be->netif->dev->features &= ~NETIF_F_IP_CSUM;
436 }
438 /* Map the shared frame, irq etc. */
439 err = netif_map(be->netif, tx_ring_ref, rx_ring_ref, evtchn);
440 if (err) {
441 xenbus_dev_fatal(dev, err,
442 "mapping shared-frames %lu/%lu port %u",
443 tx_ring_ref, rx_ring_ref, evtchn);
444 return err;
445 }
446 return 0;
447 }
450 /* ** Driver Registration ** */
453 static struct xenbus_device_id netback_ids[] = {
454 { "vif" },
455 { "" }
456 };
459 static struct xenbus_driver netback = {
460 .name = "vif",
461 .owner = THIS_MODULE,
462 .ids = netback_ids,
463 .probe = netback_probe,
464 .remove = netback_remove,
465 .uevent = netback_uevent,
466 .otherend_changed = frontend_changed,
467 };
470 void netif_xenbus_init(void)
471 {
472 xenbus_register_backend(&netback);
473 }