blob: 8a9e8750703f0fa47df6c3b6a9b5f717a19151ad [file] [log] [blame]
Ian Campbellf942dc22011-03-15 00:06:18 +00001/*
2 * Xenbus code for netif backend
3 *
4 * Copyright (C) 2005 Rusty Russell <rusty@rustcorp.com.au>
5 * Copyright (C) 2005 XenSource Ltd
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20*/
21
22#include "common.h"
23
24struct backend_info {
25 struct xenbus_device *dev;
26 struct xenvif *vif;
Paul Durrant7ab12332013-09-26 12:09:52 +010027
28 /* This is the state that will be reflected in xenstore when any
29 * active hotplug script completes.
30 */
31 enum xenbus_state state;
32
Ian Campbellf942dc22011-03-15 00:06:18 +000033 enum xenbus_state frontend_state;
34 struct xenbus_watch hotplug_status_watch;
Ian Campbell17938a62011-04-03 22:26:24 +000035 u8 have_hotplug_status_watch:1;
Ian Campbellf942dc22011-03-15 00:06:18 +000036};
37
38static int connect_rings(struct backend_info *);
39static void connect(struct backend_info *);
40static void backend_create_xenvif(struct backend_info *be);
41static void unregister_hotplug_status_watch(struct backend_info *be);
David Vrabel7e19d6d2013-10-07 13:55:19 +010042static void set_backend_state(struct backend_info *be,
43 enum xenbus_state state);
Ian Campbellf942dc22011-03-15 00:06:18 +000044
45static int netback_remove(struct xenbus_device *dev)
46{
47 struct backend_info *be = dev_get_drvdata(&dev->dev);
48
David Vrabel7e19d6d2013-10-07 13:55:19 +010049 set_backend_state(be, XenbusStateClosed);
50
Ian Campbellf942dc22011-03-15 00:06:18 +000051 unregister_hotplug_status_watch(be);
52 if (be->vif) {
53 kobject_uevent(&dev->dev.kobj, KOBJ_OFFLINE);
54 xenbus_rm(XBT_NIL, dev->nodename, "hotplug-status");
Paul Durrantf495ddc2013-10-08 14:22:56 +010055 xenvif_free(be->vif);
Ian Campbellf942dc22011-03-15 00:06:18 +000056 be->vif = NULL;
57 }
58 kfree(be);
59 dev_set_drvdata(&dev->dev, NULL);
60 return 0;
61}
62
63
64/**
65 * Entry point to this code when a new device is created. Allocate the basic
66 * structures and switch to InitWait.
67 */
68static int netback_probe(struct xenbus_device *dev,
69 const struct xenbus_device_id *id)
70{
71 const char *message;
72 struct xenbus_transaction xbt;
73 int err;
74 int sg;
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 }
82
83 be->dev = dev;
84 dev_set_drvdata(&dev->dev, be);
85
86 sg = 1;
87
88 do {
89 err = xenbus_transaction_start(&xbt);
90 if (err) {
91 xenbus_dev_fatal(dev, err, "starting transaction");
92 goto fail;
93 }
94
95 err = xenbus_printf(xbt, dev->nodename, "feature-sg", "%d", sg);
96 if (err) {
97 message = "writing feature-sg";
98 goto abort_transaction;
99 }
100
101 err = xenbus_printf(xbt, dev->nodename, "feature-gso-tcpv4",
102 "%d", sg);
103 if (err) {
104 message = "writing feature-gso-tcpv4";
105 goto abort_transaction;
106 }
107
108 /* We support rx-copy path. */
109 err = xenbus_printf(xbt, dev->nodename,
110 "feature-rx-copy", "%d", 1);
111 if (err) {
112 message = "writing feature-rx-copy";
113 goto abort_transaction;
114 }
115
116 /*
117 * We don't support rx-flip path (except old guests who don't
118 * grok this feature flag).
119 */
120 err = xenbus_printf(xbt, dev->nodename,
121 "feature-rx-flip", "%d", 0);
122 if (err) {
123 message = "writing feature-rx-flip";
124 goto abort_transaction;
125 }
126
127 err = xenbus_transaction_end(xbt, 0);
128 } while (err == -EAGAIN);
129
130 if (err) {
131 xenbus_dev_fatal(dev, err, "completing transaction");
132 goto fail;
133 }
134
135 err = xenbus_switch_state(dev, XenbusStateInitWait);
136 if (err)
137 goto fail;
138
Paul Durrant7ab12332013-09-26 12:09:52 +0100139 be->state = XenbusStateInitWait;
140
Ian Campbellf942dc22011-03-15 00:06:18 +0000141 /* This kicks hotplug scripts, so do it immediately. */
142 backend_create_xenvif(be);
143
144 return 0;
145
146abort_transaction:
147 xenbus_transaction_end(xbt, 1);
148 xenbus_dev_fatal(dev, err, "%s", message);
149fail:
150 pr_debug("failed");
151 netback_remove(dev);
152 return err;
153}
154
155
156/*
157 * Handle the creation of the hotplug script environment. We add the script
158 * and vif variables to the environment, for the benefit of the vif-* hotplug
159 * scripts.
160 */
161static int netback_uevent(struct xenbus_device *xdev,
162 struct kobj_uevent_env *env)
163{
164 struct backend_info *be = dev_get_drvdata(&xdev->dev);
165 char *val;
166
167 val = xenbus_read(XBT_NIL, xdev->nodename, "script", NULL);
168 if (IS_ERR(val)) {
169 int err = PTR_ERR(val);
170 xenbus_dev_fatal(xdev, err, "reading script");
171 return err;
172 } else {
173 if (add_uevent_var(env, "script=%s", val)) {
174 kfree(val);
175 return -ENOMEM;
176 }
177 kfree(val);
178 }
179
180 if (!be || !be->vif)
181 return 0;
182
183 return add_uevent_var(env, "vif=%s", be->vif->dev->name);
184}
185
186
187static void backend_create_xenvif(struct backend_info *be)
188{
189 int err;
190 long handle;
191 struct xenbus_device *dev = be->dev;
192
193 if (be->vif != NULL)
194 return;
195
196 err = xenbus_scanf(XBT_NIL, dev->nodename, "handle", "%li", &handle);
197 if (err != 1) {
198 xenbus_dev_fatal(dev, err, "reading handle");
199 return;
200 }
201
202 be->vif = xenvif_alloc(&dev->dev, dev->otherend_id, handle);
203 if (IS_ERR(be->vif)) {
204 err = PTR_ERR(be->vif);
205 be->vif = NULL;
206 xenbus_dev_fatal(dev, err, "creating interface");
207 return;
208 }
209
210 kobject_uevent(&dev->dev.kobj, KOBJ_ONLINE);
211}
212
Paul Durrant7ab12332013-09-26 12:09:52 +0100213static void backend_disconnect(struct backend_info *be)
Ian Campbellf942dc22011-03-15 00:06:18 +0000214{
Paul Durrantf495ddc2013-10-08 14:22:56 +0100215 if (be->vif)
Ian Campbellf942dc22011-03-15 00:06:18 +0000216 xenvif_disconnect(be->vif);
Paul Durrantf495ddc2013-10-08 14:22:56 +0100217}
218
Paul Durrant7ab12332013-09-26 12:09:52 +0100219static void backend_connect(struct backend_info *be)
Paul Durrantf495ddc2013-10-08 14:22:56 +0100220{
Paul Durrant7ab12332013-09-26 12:09:52 +0100221 if (be->vif)
222 connect(be);
223}
Paul Durrantf495ddc2013-10-08 14:22:56 +0100224
Paul Durrant7ab12332013-09-26 12:09:52 +0100225static inline void backend_switch_state(struct backend_info *be,
226 enum xenbus_state state)
227{
228 struct xenbus_device *dev = be->dev;
229
230 pr_debug("%s -> %s\n", dev->nodename, xenbus_strstate(state));
231 be->state = state;
232
233 /* If we are waiting for a hotplug script then defer the
234 * actual xenbus state change.
235 */
236 if (!be->have_hotplug_status_watch)
237 xenbus_switch_state(dev, state);
238}
239
240/* Handle backend state transitions:
241 *
242 * The backend state starts in InitWait and the following transitions are
243 * allowed.
244 *
245 * InitWait -> Connected
246 *
247 * ^ \ |
248 * | \ |
249 * | \ |
250 * | \ |
251 * | \ |
252 * | \ |
253 * | V V
254 *
255 * Closed <-> Closing
256 *
257 * The state argument specifies the eventual state of the backend and the
258 * function transitions to that state via the shortest path.
259 */
260static void set_backend_state(struct backend_info *be,
261 enum xenbus_state state)
262{
263 while (be->state != state) {
264 switch (be->state) {
265 case XenbusStateClosed:
266 switch (state) {
267 case XenbusStateInitWait:
268 case XenbusStateConnected:
269 pr_info("%s: prepare for reconnect\n",
270 be->dev->nodename);
271 backend_switch_state(be, XenbusStateInitWait);
272 break;
273 case XenbusStateClosing:
274 backend_switch_state(be, XenbusStateClosing);
275 break;
276 default:
277 BUG();
278 }
279 break;
280 case XenbusStateInitWait:
281 switch (state) {
282 case XenbusStateConnected:
283 backend_connect(be);
284 backend_switch_state(be, XenbusStateConnected);
285 break;
286 case XenbusStateClosing:
287 case XenbusStateClosed:
288 backend_switch_state(be, XenbusStateClosing);
289 break;
290 default:
291 BUG();
292 }
293 break;
294 case XenbusStateConnected:
295 switch (state) {
296 case XenbusStateInitWait:
297 case XenbusStateClosing:
298 case XenbusStateClosed:
299 backend_disconnect(be);
300 backend_switch_state(be, XenbusStateClosing);
301 break;
302 default:
303 BUG();
304 }
305 break;
306 case XenbusStateClosing:
307 switch (state) {
308 case XenbusStateInitWait:
309 case XenbusStateConnected:
310 case XenbusStateClosed:
311 backend_switch_state(be, XenbusStateClosed);
312 break;
313 default:
314 BUG();
315 }
316 break;
317 default:
318 BUG();
319 }
Ian Campbellf942dc22011-03-15 00:06:18 +0000320 }
321}
322
323/**
324 * Callback received when the frontend's state changes.
325 */
326static void frontend_changed(struct xenbus_device *dev,
327 enum xenbus_state frontend_state)
328{
329 struct backend_info *be = dev_get_drvdata(&dev->dev);
330
Paul Durrant7ab12332013-09-26 12:09:52 +0100331 pr_debug("%s -> %s\n", dev->otherend, xenbus_strstate(frontend_state));
Ian Campbellf942dc22011-03-15 00:06:18 +0000332
333 be->frontend_state = frontend_state;
334
335 switch (frontend_state) {
336 case XenbusStateInitialising:
Paul Durrant7ab12332013-09-26 12:09:52 +0100337 set_backend_state(be, XenbusStateInitWait);
Ian Campbellf942dc22011-03-15 00:06:18 +0000338 break;
339
340 case XenbusStateInitialised:
341 break;
342
343 case XenbusStateConnected:
Paul Durrant7ab12332013-09-26 12:09:52 +0100344 set_backend_state(be, XenbusStateConnected);
Ian Campbellf942dc22011-03-15 00:06:18 +0000345 break;
346
347 case XenbusStateClosing:
Paul Durrant7ab12332013-09-26 12:09:52 +0100348 set_backend_state(be, XenbusStateClosing);
Ian Campbellf942dc22011-03-15 00:06:18 +0000349 break;
350
351 case XenbusStateClosed:
Paul Durrant7ab12332013-09-26 12:09:52 +0100352 set_backend_state(be, XenbusStateClosed);
Ian Campbellf942dc22011-03-15 00:06:18 +0000353 if (xenbus_dev_is_online(dev))
354 break;
355 /* fall through if not online */
356 case XenbusStateUnknown:
Paul Durrant7ab12332013-09-26 12:09:52 +0100357 set_backend_state(be, XenbusStateClosed);
Ian Campbellf942dc22011-03-15 00:06:18 +0000358 device_unregister(&dev->dev);
359 break;
360
361 default:
362 xenbus_dev_fatal(dev, -EINVAL, "saw state %d at frontend",
363 frontend_state);
364 break;
365 }
366}
367
368
369static void xen_net_read_rate(struct xenbus_device *dev,
370 unsigned long *bytes, unsigned long *usec)
371{
372 char *s, *e;
373 unsigned long b, u;
374 char *ratestr;
375
376 /* Default to unlimited bandwidth. */
377 *bytes = ~0UL;
378 *usec = 0;
379
380 ratestr = xenbus_read(XBT_NIL, dev->nodename, "rate", NULL);
381 if (IS_ERR(ratestr))
382 return;
383
384 s = ratestr;
385 b = simple_strtoul(s, &e, 10);
386 if ((s == e) || (*e != ','))
387 goto fail;
388
389 s = e + 1;
390 u = simple_strtoul(s, &e, 10);
391 if ((s == e) || (*e != '\0'))
392 goto fail;
393
394 *bytes = b;
395 *usec = u;
396
397 kfree(ratestr);
398 return;
399
400 fail:
401 pr_warn("Failed to parse network rate limit. Traffic unlimited.\n");
402 kfree(ratestr);
403}
404
405static int xen_net_read_mac(struct xenbus_device *dev, u8 mac[])
406{
407 char *s, *e, *macstr;
408 int i;
409
410 macstr = s = xenbus_read(XBT_NIL, dev->nodename, "mac", NULL);
411 if (IS_ERR(macstr))
412 return PTR_ERR(macstr);
413
414 for (i = 0; i < ETH_ALEN; i++) {
415 mac[i] = simple_strtoul(s, &e, 16);
416 if ((s == e) || (*e != ((i == ETH_ALEN-1) ? '\0' : ':'))) {
417 kfree(macstr);
418 return -ENOENT;
419 }
420 s = e+1;
421 }
422
423 kfree(macstr);
424 return 0;
425}
426
427static void unregister_hotplug_status_watch(struct backend_info *be)
428{
429 if (be->have_hotplug_status_watch) {
430 unregister_xenbus_watch(&be->hotplug_status_watch);
431 kfree(be->hotplug_status_watch.node);
432 }
433 be->have_hotplug_status_watch = 0;
434}
435
436static void hotplug_status_changed(struct xenbus_watch *watch,
437 const char **vec,
438 unsigned int vec_size)
439{
440 struct backend_info *be = container_of(watch,
441 struct backend_info,
442 hotplug_status_watch);
443 char *str;
444 unsigned int len;
445
446 str = xenbus_read(XBT_NIL, be->dev->nodename, "hotplug-status", &len);
447 if (IS_ERR(str))
448 return;
449 if (len == sizeof("connected")-1 && !memcmp(str, "connected", len)) {
Paul Durrant7ab12332013-09-26 12:09:52 +0100450 /* Complete any pending state change */
451 xenbus_switch_state(be->dev, be->state);
452
Ian Campbellf942dc22011-03-15 00:06:18 +0000453 /* Not interested in this watch anymore. */
454 unregister_hotplug_status_watch(be);
455 }
456 kfree(str);
457}
458
459static void connect(struct backend_info *be)
460{
461 int err;
462 struct xenbus_device *dev = be->dev;
463
464 err = connect_rings(be);
465 if (err)
466 return;
467
468 err = xen_net_read_mac(dev, be->vif->fe_dev_addr);
469 if (err) {
470 xenbus_dev_fatal(dev, err, "parsing %s/mac", dev->nodename);
471 return;
472 }
473
474 xen_net_read_rate(dev, &be->vif->credit_bytes,
475 &be->vif->credit_usec);
476 be->vif->remaining_credit = be->vif->credit_bytes;
477
478 unregister_hotplug_status_watch(be);
479 err = xenbus_watch_pathfmt(dev, &be->hotplug_status_watch,
480 hotplug_status_changed,
481 "%s/%s", dev->nodename, "hotplug-status");
Paul Durrant7ab12332013-09-26 12:09:52 +0100482 if (!err)
Ian Campbellf942dc22011-03-15 00:06:18 +0000483 be->have_hotplug_status_watch = 1;
Ian Campbellf942dc22011-03-15 00:06:18 +0000484
485 netif_wake_queue(be->vif->dev);
486}
487
488
489static int connect_rings(struct backend_info *be)
490{
491 struct xenvif *vif = be->vif;
492 struct xenbus_device *dev = be->dev;
493 unsigned long tx_ring_ref, rx_ring_ref;
494 unsigned int evtchn, rx_copy;
495 int err;
496 int val;
497
498 err = xenbus_gather(XBT_NIL, dev->otherend,
499 "tx-ring-ref", "%lu", &tx_ring_ref,
500 "rx-ring-ref", "%lu", &rx_ring_ref,
501 "event-channel", "%u", &evtchn, NULL);
502 if (err) {
503 xenbus_dev_fatal(dev, err,
504 "reading %s/ring-ref and event-channel",
505 dev->otherend);
506 return err;
507 }
508
509 err = xenbus_scanf(XBT_NIL, dev->otherend, "request-rx-copy", "%u",
510 &rx_copy);
511 if (err == -ENOENT) {
512 err = 0;
513 rx_copy = 0;
514 }
515 if (err < 0) {
516 xenbus_dev_fatal(dev, err, "reading %s/request-rx-copy",
517 dev->otherend);
518 return err;
519 }
520 if (!rx_copy)
521 return -EOPNOTSUPP;
522
523 if (vif->dev->tx_queue_len != 0) {
524 if (xenbus_scanf(XBT_NIL, dev->otherend,
525 "feature-rx-notify", "%d", &val) < 0)
526 val = 0;
527 if (val)
528 vif->can_queue = 1;
529 else
530 /* Must be non-zero for pfifo_fast to work. */
531 vif->dev->tx_queue_len = 1;
532 }
533
534 if (xenbus_scanf(XBT_NIL, dev->otherend, "feature-sg",
535 "%d", &val) < 0)
536 val = 0;
537 vif->can_sg = !!val;
538
539 if (xenbus_scanf(XBT_NIL, dev->otherend, "feature-gso-tcpv4",
540 "%d", &val) < 0)
541 val = 0;
542 vif->gso = !!val;
543
544 if (xenbus_scanf(XBT_NIL, dev->otherend, "feature-gso-tcpv4-prefix",
545 "%d", &val) < 0)
546 val = 0;
547 vif->gso_prefix = !!val;
548
549 if (xenbus_scanf(XBT_NIL, dev->otherend, "feature-no-csum-offload",
550 "%d", &val) < 0)
551 val = 0;
552 vif->csum = !val;
553
554 /* Map the shared frame, irq etc. */
555 err = xenvif_connect(vif, tx_ring_ref, rx_ring_ref, evtchn);
556 if (err) {
557 xenbus_dev_fatal(dev, err,
558 "mapping shared-frames %lu/%lu port %u",
559 tx_ring_ref, rx_ring_ref, evtchn);
560 return err;
561 }
562 return 0;
563}
564
565
566/* ** Driver Registration ** */
567
568
569static const struct xenbus_device_id netback_ids[] = {
570 { "vif" },
571 { "" }
572};
573
574
Jan Beulich73db1442011-12-22 09:08:13 +0000575static DEFINE_XENBUS_DRIVER(netback, ,
Ian Campbellf942dc22011-03-15 00:06:18 +0000576 .probe = netback_probe,
577 .remove = netback_remove,
578 .uevent = netback_uevent,
579 .otherend_changed = frontend_changed,
Jan Beulich73db1442011-12-22 09:08:13 +0000580);
Ian Campbellf942dc22011-03-15 00:06:18 +0000581
582int xenvif_xenbus_init(void)
583{
Jan Beulich73db1442011-12-22 09:08:13 +0000584 return xenbus_register_backend(&netback_driver);
Ian Campbellf942dc22011-03-15 00:06:18 +0000585}