blob: 1b08d879837231256f4cc954bbe8fbd0c4aa9391 [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 Durrantea732df2013-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 Vrabeldc62cca2013-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 Vrabeldc62cca2013-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 Durrant279f4382013-09-17 17:46:08 +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
Wei Liue1f00a692013-05-22 06:34:45 +0000135 /*
136 * Split event channels support, this is optional so it is not
137 * put inside the above loop.
138 */
139 err = xenbus_printf(XBT_NIL, dev->nodename,
140 "feature-split-event-channels",
141 "%u", separate_tx_rx_irq);
142 if (err)
Wei Liu8ef2c3b2013-07-02 00:08:54 +0100143 pr_debug("Error writing feature-split-event-channels\n");
Wei Liue1f00a692013-05-22 06:34:45 +0000144
Ian Campbellf942dc22011-03-15 00:06:18 +0000145 err = xenbus_switch_state(dev, XenbusStateInitWait);
146 if (err)
147 goto fail;
148
Paul Durrantea732df2013-09-26 12:09:52 +0100149 be->state = XenbusStateInitWait;
150
Ian Campbellf942dc22011-03-15 00:06:18 +0000151 /* This kicks hotplug scripts, so do it immediately. */
152 backend_create_xenvif(be);
153
154 return 0;
155
156abort_transaction:
157 xenbus_transaction_end(xbt, 1);
158 xenbus_dev_fatal(dev, err, "%s", message);
159fail:
Wei Liu8ef2c3b2013-07-02 00:08:54 +0100160 pr_debug("failed\n");
Ian Campbellf942dc22011-03-15 00:06:18 +0000161 netback_remove(dev);
162 return err;
163}
164
165
166/*
167 * Handle the creation of the hotplug script environment. We add the script
168 * and vif variables to the environment, for the benefit of the vif-* hotplug
169 * scripts.
170 */
171static int netback_uevent(struct xenbus_device *xdev,
172 struct kobj_uevent_env *env)
173{
174 struct backend_info *be = dev_get_drvdata(&xdev->dev);
175 char *val;
176
177 val = xenbus_read(XBT_NIL, xdev->nodename, "script", NULL);
178 if (IS_ERR(val)) {
179 int err = PTR_ERR(val);
180 xenbus_dev_fatal(xdev, err, "reading script");
181 return err;
182 } else {
183 if (add_uevent_var(env, "script=%s", val)) {
184 kfree(val);
185 return -ENOMEM;
186 }
187 kfree(val);
188 }
189
190 if (!be || !be->vif)
191 return 0;
192
193 return add_uevent_var(env, "vif=%s", be->vif->dev->name);
194}
195
196
197static void backend_create_xenvif(struct backend_info *be)
198{
199 int err;
200 long handle;
201 struct xenbus_device *dev = be->dev;
202
203 if (be->vif != NULL)
204 return;
205
206 err = xenbus_scanf(XBT_NIL, dev->nodename, "handle", "%li", &handle);
207 if (err != 1) {
208 xenbus_dev_fatal(dev, err, "reading handle");
209 return;
210 }
211
212 be->vif = xenvif_alloc(&dev->dev, dev->otherend_id, handle);
213 if (IS_ERR(be->vif)) {
214 err = PTR_ERR(be->vif);
215 be->vif = NULL;
216 xenbus_dev_fatal(dev, err, "creating interface");
217 return;
218 }
219
220 kobject_uevent(&dev->dev.kobj, KOBJ_ONLINE);
221}
222
Paul Durrantea732df2013-09-26 12:09:52 +0100223static void backend_disconnect(struct backend_info *be)
Ian Campbellf942dc22011-03-15 00:06:18 +0000224{
Paul Durrant279f4382013-09-17 17:46:08 +0100225 if (be->vif)
Ian Campbellf942dc22011-03-15 00:06:18 +0000226 xenvif_disconnect(be->vif);
Paul Durrant279f4382013-09-17 17:46:08 +0100227}
228
Paul Durrantea732df2013-09-26 12:09:52 +0100229static void backend_connect(struct backend_info *be)
Paul Durrant279f4382013-09-17 17:46:08 +0100230{
Paul Durrantea732df2013-09-26 12:09:52 +0100231 if (be->vif)
232 connect(be);
233}
Paul Durrant279f4382013-09-17 17:46:08 +0100234
Paul Durrantea732df2013-09-26 12:09:52 +0100235static inline void backend_switch_state(struct backend_info *be,
236 enum xenbus_state state)
237{
238 struct xenbus_device *dev = be->dev;
239
240 pr_debug("%s -> %s\n", dev->nodename, xenbus_strstate(state));
241 be->state = state;
242
243 /* If we are waiting for a hotplug script then defer the
244 * actual xenbus state change.
245 */
246 if (!be->have_hotplug_status_watch)
247 xenbus_switch_state(dev, state);
248}
249
250/* Handle backend state transitions:
251 *
252 * The backend state starts in InitWait and the following transitions are
253 * allowed.
254 *
255 * InitWait -> Connected
256 *
257 * ^ \ |
258 * | \ |
259 * | \ |
260 * | \ |
261 * | \ |
262 * | \ |
263 * | V V
264 *
265 * Closed <-> Closing
266 *
267 * The state argument specifies the eventual state of the backend and the
268 * function transitions to that state via the shortest path.
269 */
270static void set_backend_state(struct backend_info *be,
271 enum xenbus_state state)
272{
273 while (be->state != state) {
274 switch (be->state) {
275 case XenbusStateClosed:
276 switch (state) {
277 case XenbusStateInitWait:
278 case XenbusStateConnected:
279 pr_info("%s: prepare for reconnect\n",
280 be->dev->nodename);
281 backend_switch_state(be, XenbusStateInitWait);
282 break;
283 case XenbusStateClosing:
284 backend_switch_state(be, XenbusStateClosing);
285 break;
286 default:
287 BUG();
288 }
289 break;
290 case XenbusStateInitWait:
291 switch (state) {
292 case XenbusStateConnected:
293 backend_connect(be);
294 backend_switch_state(be, XenbusStateConnected);
295 break;
296 case XenbusStateClosing:
297 case XenbusStateClosed:
298 backend_switch_state(be, XenbusStateClosing);
299 break;
300 default:
301 BUG();
302 }
303 break;
304 case XenbusStateConnected:
305 switch (state) {
306 case XenbusStateInitWait:
307 case XenbusStateClosing:
308 case XenbusStateClosed:
309 backend_disconnect(be);
310 backend_switch_state(be, XenbusStateClosing);
311 break;
312 default:
313 BUG();
314 }
315 break;
316 case XenbusStateClosing:
317 switch (state) {
318 case XenbusStateInitWait:
319 case XenbusStateConnected:
320 case XenbusStateClosed:
321 backend_switch_state(be, XenbusStateClosed);
322 break;
323 default:
324 BUG();
325 }
326 break;
327 default:
328 BUG();
329 }
Ian Campbellf942dc22011-03-15 00:06:18 +0000330 }
331}
332
333/**
334 * Callback received when the frontend's state changes.
335 */
336static void frontend_changed(struct xenbus_device *dev,
337 enum xenbus_state frontend_state)
338{
339 struct backend_info *be = dev_get_drvdata(&dev->dev);
340
Paul Durrantea732df2013-09-26 12:09:52 +0100341 pr_debug("%s -> %s\n", dev->otherend, xenbus_strstate(frontend_state));
Ian Campbellf942dc22011-03-15 00:06:18 +0000342
343 be->frontend_state = frontend_state;
344
345 switch (frontend_state) {
346 case XenbusStateInitialising:
Paul Durrantea732df2013-09-26 12:09:52 +0100347 set_backend_state(be, XenbusStateInitWait);
Ian Campbellf942dc22011-03-15 00:06:18 +0000348 break;
349
350 case XenbusStateInitialised:
351 break;
352
353 case XenbusStateConnected:
Paul Durrantea732df2013-09-26 12:09:52 +0100354 set_backend_state(be, XenbusStateConnected);
Ian Campbellf942dc22011-03-15 00:06:18 +0000355 break;
356
357 case XenbusStateClosing:
Paul Durrantea732df2013-09-26 12:09:52 +0100358 set_backend_state(be, XenbusStateClosing);
Ian Campbellf942dc22011-03-15 00:06:18 +0000359 break;
360
361 case XenbusStateClosed:
Paul Durrantea732df2013-09-26 12:09:52 +0100362 set_backend_state(be, XenbusStateClosed);
Ian Campbellf942dc22011-03-15 00:06:18 +0000363 if (xenbus_dev_is_online(dev))
364 break;
365 /* fall through if not online */
366 case XenbusStateUnknown:
Paul Durrantea732df2013-09-26 12:09:52 +0100367 set_backend_state(be, XenbusStateClosed);
Ian Campbellf942dc22011-03-15 00:06:18 +0000368 device_unregister(&dev->dev);
369 break;
370
371 default:
372 xenbus_dev_fatal(dev, -EINVAL, "saw state %d at frontend",
373 frontend_state);
374 break;
375 }
376}
377
378
379static void xen_net_read_rate(struct xenbus_device *dev,
380 unsigned long *bytes, unsigned long *usec)
381{
382 char *s, *e;
383 unsigned long b, u;
384 char *ratestr;
385
386 /* Default to unlimited bandwidth. */
387 *bytes = ~0UL;
388 *usec = 0;
389
390 ratestr = xenbus_read(XBT_NIL, dev->nodename, "rate", NULL);
391 if (IS_ERR(ratestr))
392 return;
393
394 s = ratestr;
395 b = simple_strtoul(s, &e, 10);
396 if ((s == e) || (*e != ','))
397 goto fail;
398
399 s = e + 1;
400 u = simple_strtoul(s, &e, 10);
401 if ((s == e) || (*e != '\0'))
402 goto fail;
403
404 *bytes = b;
405 *usec = u;
406
407 kfree(ratestr);
408 return;
409
410 fail:
411 pr_warn("Failed to parse network rate limit. Traffic unlimited.\n");
412 kfree(ratestr);
413}
414
415static int xen_net_read_mac(struct xenbus_device *dev, u8 mac[])
416{
417 char *s, *e, *macstr;
418 int i;
419
420 macstr = s = xenbus_read(XBT_NIL, dev->nodename, "mac", NULL);
421 if (IS_ERR(macstr))
422 return PTR_ERR(macstr);
423
424 for (i = 0; i < ETH_ALEN; i++) {
425 mac[i] = simple_strtoul(s, &e, 16);
426 if ((s == e) || (*e != ((i == ETH_ALEN-1) ? '\0' : ':'))) {
427 kfree(macstr);
428 return -ENOENT;
429 }
430 s = e+1;
431 }
432
433 kfree(macstr);
434 return 0;
435}
436
437static void unregister_hotplug_status_watch(struct backend_info *be)
438{
439 if (be->have_hotplug_status_watch) {
440 unregister_xenbus_watch(&be->hotplug_status_watch);
441 kfree(be->hotplug_status_watch.node);
442 }
443 be->have_hotplug_status_watch = 0;
444}
445
446static void hotplug_status_changed(struct xenbus_watch *watch,
447 const char **vec,
448 unsigned int vec_size)
449{
450 struct backend_info *be = container_of(watch,
451 struct backend_info,
452 hotplug_status_watch);
453 char *str;
454 unsigned int len;
455
456 str = xenbus_read(XBT_NIL, be->dev->nodename, "hotplug-status", &len);
457 if (IS_ERR(str))
458 return;
459 if (len == sizeof("connected")-1 && !memcmp(str, "connected", len)) {
Paul Durrantea732df2013-09-26 12:09:52 +0100460 /* Complete any pending state change */
461 xenbus_switch_state(be->dev, be->state);
462
Ian Campbellf942dc22011-03-15 00:06:18 +0000463 /* Not interested in this watch anymore. */
464 unregister_hotplug_status_watch(be);
465 }
466 kfree(str);
467}
468
469static void connect(struct backend_info *be)
470{
471 int err;
472 struct xenbus_device *dev = be->dev;
473
474 err = connect_rings(be);
475 if (err)
476 return;
477
478 err = xen_net_read_mac(dev, be->vif->fe_dev_addr);
479 if (err) {
480 xenbus_dev_fatal(dev, err, "parsing %s/mac", dev->nodename);
481 return;
482 }
483
484 xen_net_read_rate(dev, &be->vif->credit_bytes,
485 &be->vif->credit_usec);
486 be->vif->remaining_credit = be->vif->credit_bytes;
487
488 unregister_hotplug_status_watch(be);
489 err = xenbus_watch_pathfmt(dev, &be->hotplug_status_watch,
490 hotplug_status_changed,
491 "%s/%s", dev->nodename, "hotplug-status");
Paul Durrantea732df2013-09-26 12:09:52 +0100492 if (!err)
Ian Campbellf942dc22011-03-15 00:06:18 +0000493 be->have_hotplug_status_watch = 1;
Ian Campbellf942dc22011-03-15 00:06:18 +0000494
495 netif_wake_queue(be->vif->dev);
496}
497
498
499static int connect_rings(struct backend_info *be)
500{
501 struct xenvif *vif = be->vif;
502 struct xenbus_device *dev = be->dev;
503 unsigned long tx_ring_ref, rx_ring_ref;
Wei Liue1f00a692013-05-22 06:34:45 +0000504 unsigned int tx_evtchn, rx_evtchn, rx_copy;
Ian Campbellf942dc22011-03-15 00:06:18 +0000505 int err;
506 int val;
507
508 err = xenbus_gather(XBT_NIL, dev->otherend,
509 "tx-ring-ref", "%lu", &tx_ring_ref,
Wei Liue1f00a692013-05-22 06:34:45 +0000510 "rx-ring-ref", "%lu", &rx_ring_ref, NULL);
Ian Campbellf942dc22011-03-15 00:06:18 +0000511 if (err) {
512 xenbus_dev_fatal(dev, err,
Wei Liue1f00a692013-05-22 06:34:45 +0000513 "reading %s/ring-ref",
Ian Campbellf942dc22011-03-15 00:06:18 +0000514 dev->otherend);
515 return err;
516 }
517
Wei Liue1f00a692013-05-22 06:34:45 +0000518 /* Try split event channels first, then single event channel. */
519 err = xenbus_gather(XBT_NIL, dev->otherend,
520 "event-channel-tx", "%u", &tx_evtchn,
521 "event-channel-rx", "%u", &rx_evtchn, NULL);
522 if (err < 0) {
523 err = xenbus_scanf(XBT_NIL, dev->otherend,
524 "event-channel", "%u", &tx_evtchn);
525 if (err < 0) {
526 xenbus_dev_fatal(dev, err,
527 "reading %s/event-channel(-tx/rx)",
528 dev->otherend);
529 return err;
530 }
531 rx_evtchn = tx_evtchn;
532 }
533
Ian Campbellf942dc22011-03-15 00:06:18 +0000534 err = xenbus_scanf(XBT_NIL, dev->otherend, "request-rx-copy", "%u",
535 &rx_copy);
536 if (err == -ENOENT) {
537 err = 0;
538 rx_copy = 0;
539 }
540 if (err < 0) {
541 xenbus_dev_fatal(dev, err, "reading %s/request-rx-copy",
542 dev->otherend);
543 return err;
544 }
545 if (!rx_copy)
546 return -EOPNOTSUPP;
547
548 if (vif->dev->tx_queue_len != 0) {
549 if (xenbus_scanf(XBT_NIL, dev->otherend,
550 "feature-rx-notify", "%d", &val) < 0)
551 val = 0;
552 if (val)
553 vif->can_queue = 1;
554 else
555 /* Must be non-zero for pfifo_fast to work. */
556 vif->dev->tx_queue_len = 1;
557 }
558
559 if (xenbus_scanf(XBT_NIL, dev->otherend, "feature-sg",
560 "%d", &val) < 0)
561 val = 0;
562 vif->can_sg = !!val;
563
564 if (xenbus_scanf(XBT_NIL, dev->otherend, "feature-gso-tcpv4",
565 "%d", &val) < 0)
566 val = 0;
567 vif->gso = !!val;
568
569 if (xenbus_scanf(XBT_NIL, dev->otherend, "feature-gso-tcpv4-prefix",
570 "%d", &val) < 0)
571 val = 0;
572 vif->gso_prefix = !!val;
573
574 if (xenbus_scanf(XBT_NIL, dev->otherend, "feature-no-csum-offload",
575 "%d", &val) < 0)
576 val = 0;
577 vif->csum = !val;
578
579 /* Map the shared frame, irq etc. */
Wei Liue1f00a692013-05-22 06:34:45 +0000580 err = xenvif_connect(vif, tx_ring_ref, rx_ring_ref,
581 tx_evtchn, rx_evtchn);
Ian Campbellf942dc22011-03-15 00:06:18 +0000582 if (err) {
583 xenbus_dev_fatal(dev, err,
Wei Liue1f00a692013-05-22 06:34:45 +0000584 "mapping shared-frames %lu/%lu port tx %u rx %u",
585 tx_ring_ref, rx_ring_ref,
586 tx_evtchn, rx_evtchn);
Ian Campbellf942dc22011-03-15 00:06:18 +0000587 return err;
588 }
589 return 0;
590}
591
592
593/* ** Driver Registration ** */
594
595
596static const struct xenbus_device_id netback_ids[] = {
597 { "vif" },
598 { "" }
599};
600
601
Jan Beulich73db1442011-12-22 09:08:13 +0000602static DEFINE_XENBUS_DRIVER(netback, ,
Ian Campbellf942dc22011-03-15 00:06:18 +0000603 .probe = netback_probe,
604 .remove = netback_remove,
605 .uevent = netback_uevent,
606 .otherend_changed = frontend_changed,
Jan Beulich73db1442011-12-22 09:08:13 +0000607);
Ian Campbellf942dc22011-03-15 00:06:18 +0000608
609int xenvif_xenbus_init(void)
610{
Jan Beulich73db1442011-12-22 09:08:13 +0000611 return xenbus_register_backend(&netback_driver);
Ian Campbellf942dc22011-03-15 00:06:18 +0000612}
Wei Liub103f352013-05-16 23:26:11 +0000613
614void xenvif_xenbus_fini(void)
615{
616 return xenbus_unregister_driver(&netback_driver);
617}