ia64/xen-unstable

view linux-2.6-xen-sparse/drivers/xen/netback/xenbus.c @ 12021:66cdabe97205

[NET] back: Advertise that we do not support rx-flip path any more.
This will now be used only by older guests who do not understand
the feature-rx-{copy,flip} feature flags.
Signed-off-by: Keir Fraser <keir@xensource.com>
author kfraser@localhost.localdomain
date Thu Oct 26 11:50:17 2006 +0100 (2006-10-26)
parents 637eace6d5c6
children 3f63c1825b0d
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 struct xenbus_device *dev;
33 netif_t *netif;
34 enum xenbus_state frontend_state;
35 };
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)
42 {
43 struct backend_info *be = dev->dev.driver_data;
45 if (be->netif) {
46 netif_disconnect(be->netif);
47 be->netif = NULL;
48 }
49 kfree(be);
50 dev->dev.driver_data = NULL;
51 return 0;
52 }
55 /**
56 * Entry point to this code when a new device is created. Allocate the basic
57 * structures and switch to InitWait.
58 */
59 static int netback_probe(struct xenbus_device *dev,
60 const struct xenbus_device_id *id)
61 {
62 const char *message;
63 struct xenbus_transaction xbt;
64 int err;
65 struct backend_info *be = kzalloc(sizeof(struct backend_info),
66 GFP_KERNEL);
67 if (!be) {
68 xenbus_dev_fatal(dev, -ENOMEM,
69 "allocating backend structure");
70 return -ENOMEM;
71 }
73 be->dev = dev;
74 dev->dev.driver_data = be;
76 do {
77 err = xenbus_transaction_start(&xbt);
78 if (err) {
79 xenbus_dev_fatal(dev, err, "starting transaction");
80 goto fail;
81 }
83 err = xenbus_printf(xbt, dev->nodename, "feature-sg", "%d", 1);
84 if (err) {
85 message = "writing feature-sg";
86 goto abort_transaction;
87 }
89 err = xenbus_printf(xbt, dev->nodename, "feature-gso-tcpv4",
90 "%d", 1);
91 if (err) {
92 message = "writing feature-gso-tcpv4";
93 goto abort_transaction;
94 }
96 /* We support rx-copy path. */
97 err = xenbus_printf(xbt, dev->nodename,
98 "feature-rx-copy", "%d", 1);
99 if (err) {
100 message = "writing feature-rx-copy";
101 goto abort_transaction;
102 }
104 /*
105 * We don't support rx-flip path (except old guests who don't
106 * grok this feature flag).
107 */
108 err = xenbus_printf(xbt, dev->nodename,
109 "feature-rx-flip", "%d", 0);
110 if (err) {
111 message = "writing feature-rx-flip";
112 goto abort_transaction;
113 }
115 err = xenbus_transaction_end(xbt, 0);
116 } while (err == -EAGAIN);
118 if (err) {
119 xenbus_dev_fatal(dev, err, "completing transaction");
120 goto fail;
121 }
123 err = xenbus_switch_state(dev, XenbusStateInitWait);
124 if (err)
125 goto fail;
127 /* This kicks hotplug scripts, so do it immediately. */
128 backend_create_netif(be);
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 static void backend_create_netif(struct backend_info *be)
179 {
180 int err;
181 long handle;
182 struct xenbus_device *dev = be->dev;
184 if (be->netif != NULL)
185 return;
187 err = xenbus_scanf(XBT_NIL, dev->nodename, "handle", "%li", &handle);
188 if (err != 1) {
189 xenbus_dev_fatal(dev, err, "reading handle");
190 return;
191 }
193 be->netif = netif_alloc(dev->otherend_id, handle);
194 if (IS_ERR(be->netif)) {
195 err = PTR_ERR(be->netif);
196 be->netif = NULL;
197 xenbus_dev_fatal(dev, err, "creating interface");
198 return;
199 }
201 kobject_uevent(&dev->dev.kobj, KOBJ_ONLINE);
202 }
205 /**
206 * Callback received when the frontend's state changes.
207 */
208 static void frontend_changed(struct xenbus_device *dev,
209 enum xenbus_state frontend_state)
210 {
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("%s: %s: prepare for reconnect\n",
221 __FUNCTION__, dev->nodename);
222 if (be->netif) {
223 netif_disconnect(be->netif);
224 be->netif = NULL;
225 }
226 xenbus_switch_state(dev, XenbusStateInitWait);
227 }
228 break;
230 case XenbusStateInitialised:
231 break;
233 case XenbusStateConnected:
234 backend_create_netif(be);
235 if (be->netif)
236 connect(be);
237 break;
239 case XenbusStateClosing:
240 xenbus_switch_state(dev, XenbusStateClosing);
241 break;
243 case XenbusStateClosed:
244 xenbus_switch_state(dev, XenbusStateClosed);
245 if (xenbus_dev_is_online(dev))
246 break;
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);
252 break;
254 default:
255 xenbus_dev_fatal(dev, -EINVAL, "saw state %d at frontend",
256 frontend_state);
257 break;
258 }
259 }
262 static void xen_net_read_rate(struct xenbus_device *dev,
263 unsigned long *bytes, unsigned long *usec)
264 {
265 char *s, *e;
266 unsigned long b, u;
267 char *ratestr;
269 /* Default to unlimited bandwidth. */
270 *bytes = ~0UL;
271 *usec = 0;
273 ratestr = xenbus_read(XBT_NIL, dev->nodename, "rate", NULL);
274 if (IS_ERR(ratestr))
275 return;
277 s = ratestr;
278 b = simple_strtoul(s, &e, 10);
279 if ((s == e) || (*e != ','))
280 goto fail;
282 s = e + 1;
283 u = simple_strtoul(s, &e, 10);
284 if ((s == e) || (*e != '\0'))
285 goto fail;
287 *bytes = b;
288 *usec = u;
290 kfree(ratestr);
291 return;
293 fail:
294 WPRINTK("Failed to parse network rate limit. Traffic unlimited.\n");
295 kfree(ratestr);
296 }
298 static int xen_net_read_mac(struct xenbus_device *dev, u8 mac[])
299 {
300 char *s, *e, *macstr;
301 int i;
303 macstr = s = xenbus_read(XBT_NIL, dev->nodename, "mac", NULL);
304 if (IS_ERR(macstr))
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' : ':'))) {
310 kfree(macstr);
311 return -ENOENT;
312 }
313 s = e+1;
314 }
316 kfree(macstr);
317 return 0;
318 }
320 static void connect(struct backend_info *be)
321 {
322 int err;
323 struct xenbus_device *dev = be->dev;
325 err = connect_rings(be);
326 if (err)
327 return;
329 err = xen_net_read_mac(dev, be->netif->fe_dev_addr);
330 if (err) {
331 xenbus_dev_fatal(dev, err, "parsing %s/mac", dev->nodename);
332 return;
333 }
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 /* May not get a kick from the frontend, so start the tx_queue now. */
342 if (!netbk_can_queue(be->netif->dev))
343 netif_wake_queue(be->netif->dev);
344 }
347 static int connect_rings(struct backend_info *be)
348 {
349 struct xenbus_device *dev = be->dev;
350 unsigned long tx_ring_ref, rx_ring_ref;
351 unsigned int evtchn, rx_copy;
352 int err;
353 int val;
355 DPRINTK("");
357 err = xenbus_gather(XBT_NIL, dev->otherend,
358 "tx-ring-ref", "%lu", &tx_ring_ref,
359 "rx-ring-ref", "%lu", &rx_ring_ref,
360 "event-channel", "%u", &evtchn, NULL);
361 if (err) {
362 xenbus_dev_fatal(dev, err,
363 "reading %s/ring-ref and event-channel",
364 dev->otherend);
365 return err;
366 }
368 err = xenbus_scanf(XBT_NIL, dev->otherend, "request-rx-copy", "%u",
369 &rx_copy);
370 if (err == -ENOENT) {
371 err = 0;
372 rx_copy = 0;
373 }
374 if (err < 0) {
375 xenbus_dev_fatal(dev, err, "reading %s/request-rx-copy",
376 dev->otherend);
377 return err;
378 }
379 be->netif->copying_receiver = !!rx_copy;
381 if (be->netif->dev->tx_queue_len != 0) {
382 if (xenbus_scanf(XBT_NIL, dev->otherend,
383 "feature-rx-notify", "%d", &val) < 0)
384 val = 0;
385 if (val)
386 be->netif->can_queue = 1;
387 else
388 /* Must be non-zero for pfifo_fast to work. */
389 be->netif->dev->tx_queue_len = 1;
390 }
392 if (xenbus_scanf(XBT_NIL, dev->otherend, "feature-sg", "%d", &val) < 0)
393 val = 0;
394 if (val) {
395 be->netif->features |= NETIF_F_SG;
396 be->netif->dev->features |= NETIF_F_SG;
397 }
399 if (xenbus_scanf(XBT_NIL, dev->otherend, "feature-gso-tcpv4", "%d",
400 &val) < 0)
401 val = 0;
402 if (val) {
403 be->netif->features |= NETIF_F_TSO;
404 be->netif->dev->features |= NETIF_F_TSO;
405 }
407 if (xenbus_scanf(XBT_NIL, dev->otherend, "feature-no-csum-offload",
408 "%d", &val) < 0)
409 val = 0;
410 if (val) {
411 be->netif->features &= ~NETIF_F_IP_CSUM;
412 be->netif->dev->features &= ~NETIF_F_IP_CSUM;
413 }
415 /* Map the shared frame, irq etc. */
416 err = netif_map(be->netif, tx_ring_ref, rx_ring_ref, evtchn);
417 if (err) {
418 xenbus_dev_fatal(dev, err,
419 "mapping shared-frames %lu/%lu port %u",
420 tx_ring_ref, rx_ring_ref, evtchn);
421 return err;
422 }
423 return 0;
424 }
427 /* ** Driver Registration ** */
430 static struct xenbus_device_id netback_ids[] = {
431 { "vif" },
432 { "" }
433 };
436 static struct xenbus_driver netback = {
437 .name = "vif",
438 .owner = THIS_MODULE,
439 .ids = netback_ids,
440 .probe = netback_probe,
441 .remove = netback_remove,
442 .uevent = netback_uevent,
443 .otherend_changed = frontend_changed,
444 };
447 void netif_xenbus_init(void)
448 {
449 xenbus_register_backend(&netback);
450 }