Loading...
1/*
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, see <http://www.gnu.org/licenses/>.
19*/
20
21#include "common.h"
22
23struct backend_info {
24 struct xenbus_device *dev;
25 struct xenvif *vif;
26
27 /* This is the state that will be reflected in xenstore when any
28 * active hotplug script completes.
29 */
30 enum xenbus_state state;
31
32 enum xenbus_state frontend_state;
33 struct xenbus_watch hotplug_status_watch;
34 u8 have_hotplug_status_watch:1;
35};
36
37static int connect_rings(struct backend_info *);
38static void connect(struct backend_info *);
39static void backend_create_xenvif(struct backend_info *be);
40static void unregister_hotplug_status_watch(struct backend_info *be);
41static void set_backend_state(struct backend_info *be,
42 enum xenbus_state state);
43
44static int netback_remove(struct xenbus_device *dev)
45{
46 struct backend_info *be = dev_get_drvdata(&dev->dev);
47
48 set_backend_state(be, XenbusStateClosed);
49
50 unregister_hotplug_status_watch(be);
51 if (be->vif) {
52 kobject_uevent(&dev->dev.kobj, KOBJ_OFFLINE);
53 xenbus_rm(XBT_NIL, dev->nodename, "hotplug-status");
54 xenvif_free(be->vif);
55 be->vif = NULL;
56 }
57 kfree(be);
58 dev_set_drvdata(&dev->dev, NULL);
59 return 0;
60}
61
62
63/**
64 * Entry point to this code when a new device is created. Allocate the basic
65 * structures and switch to InitWait.
66 */
67static int netback_probe(struct xenbus_device *dev,
68 const struct xenbus_device_id *id)
69{
70 const char *message;
71 struct xenbus_transaction xbt;
72 int err;
73 int sg;
74 struct backend_info *be = kzalloc(sizeof(struct backend_info),
75 GFP_KERNEL);
76 if (!be) {
77 xenbus_dev_fatal(dev, -ENOMEM,
78 "allocating backend structure");
79 return -ENOMEM;
80 }
81
82 be->dev = dev;
83 dev_set_drvdata(&dev->dev, be);
84
85 sg = 1;
86
87 do {
88 err = xenbus_transaction_start(&xbt);
89 if (err) {
90 xenbus_dev_fatal(dev, err, "starting transaction");
91 goto fail;
92 }
93
94 err = xenbus_printf(xbt, dev->nodename, "feature-sg", "%d", sg);
95 if (err) {
96 message = "writing feature-sg";
97 goto abort_transaction;
98 }
99
100 err = xenbus_printf(xbt, dev->nodename, "feature-gso-tcpv4",
101 "%d", sg);
102 if (err) {
103 message = "writing feature-gso-tcpv4";
104 goto abort_transaction;
105 }
106
107 err = xenbus_printf(xbt, dev->nodename, "feature-gso-tcpv6",
108 "%d", sg);
109 if (err) {
110 message = "writing feature-gso-tcpv6";
111 goto abort_transaction;
112 }
113
114 /* We support partial checksum setup for IPv6 packets */
115 err = xenbus_printf(xbt, dev->nodename,
116 "feature-ipv6-csum-offload",
117 "%d", 1);
118 if (err) {
119 message = "writing feature-ipv6-csum-offload";
120 goto abort_transaction;
121 }
122
123 /* We support rx-copy path. */
124 err = xenbus_printf(xbt, dev->nodename,
125 "feature-rx-copy", "%d", 1);
126 if (err) {
127 message = "writing feature-rx-copy";
128 goto abort_transaction;
129 }
130
131 /*
132 * We don't support rx-flip path (except old guests who don't
133 * grok this feature flag).
134 */
135 err = xenbus_printf(xbt, dev->nodename,
136 "feature-rx-flip", "%d", 0);
137 if (err) {
138 message = "writing feature-rx-flip";
139 goto abort_transaction;
140 }
141
142 err = xenbus_transaction_end(xbt, 0);
143 } while (err == -EAGAIN);
144
145 if (err) {
146 xenbus_dev_fatal(dev, err, "completing transaction");
147 goto fail;
148 }
149
150 /*
151 * Split event channels support, this is optional so it is not
152 * put inside the above loop.
153 */
154 err = xenbus_printf(XBT_NIL, dev->nodename,
155 "feature-split-event-channels",
156 "%u", separate_tx_rx_irq);
157 if (err)
158 pr_debug("Error writing feature-split-event-channels\n");
159
160 err = xenbus_switch_state(dev, XenbusStateInitWait);
161 if (err)
162 goto fail;
163
164 be->state = XenbusStateInitWait;
165
166 /* This kicks hotplug scripts, so do it immediately. */
167 backend_create_xenvif(be);
168
169 return 0;
170
171abort_transaction:
172 xenbus_transaction_end(xbt, 1);
173 xenbus_dev_fatal(dev, err, "%s", message);
174fail:
175 pr_debug("failed\n");
176 netback_remove(dev);
177 return err;
178}
179
180
181/*
182 * Handle the creation of the hotplug script environment. We add the script
183 * and vif variables to the environment, for the benefit of the vif-* hotplug
184 * scripts.
185 */
186static int netback_uevent(struct xenbus_device *xdev,
187 struct kobj_uevent_env *env)
188{
189 struct backend_info *be = dev_get_drvdata(&xdev->dev);
190 char *val;
191
192 val = xenbus_read(XBT_NIL, xdev->nodename, "script", NULL);
193 if (IS_ERR(val)) {
194 int err = PTR_ERR(val);
195 xenbus_dev_fatal(xdev, err, "reading script");
196 return err;
197 } else {
198 if (add_uevent_var(env, "script=%s", val)) {
199 kfree(val);
200 return -ENOMEM;
201 }
202 kfree(val);
203 }
204
205 if (!be || !be->vif)
206 return 0;
207
208 return add_uevent_var(env, "vif=%s", be->vif->dev->name);
209}
210
211
212static void backend_create_xenvif(struct backend_info *be)
213{
214 int err;
215 long handle;
216 struct xenbus_device *dev = be->dev;
217
218 if (be->vif != NULL)
219 return;
220
221 err = xenbus_scanf(XBT_NIL, dev->nodename, "handle", "%li", &handle);
222 if (err != 1) {
223 xenbus_dev_fatal(dev, err, "reading handle");
224 return;
225 }
226
227 be->vif = xenvif_alloc(&dev->dev, dev->otherend_id, handle);
228 if (IS_ERR(be->vif)) {
229 err = PTR_ERR(be->vif);
230 be->vif = NULL;
231 xenbus_dev_fatal(dev, err, "creating interface");
232 return;
233 }
234
235 kobject_uevent(&dev->dev.kobj, KOBJ_ONLINE);
236}
237
238static void backend_disconnect(struct backend_info *be)
239{
240 if (be->vif)
241 xenvif_disconnect(be->vif);
242}
243
244static void backend_connect(struct backend_info *be)
245{
246 if (be->vif)
247 connect(be);
248}
249
250static inline void backend_switch_state(struct backend_info *be,
251 enum xenbus_state state)
252{
253 struct xenbus_device *dev = be->dev;
254
255 pr_debug("%s -> %s\n", dev->nodename, xenbus_strstate(state));
256 be->state = state;
257
258 /* If we are waiting for a hotplug script then defer the
259 * actual xenbus state change.
260 */
261 if (!be->have_hotplug_status_watch)
262 xenbus_switch_state(dev, state);
263}
264
265/* Handle backend state transitions:
266 *
267 * The backend state starts in InitWait and the following transitions are
268 * allowed.
269 *
270 * InitWait -> Connected
271 *
272 * ^ \ |
273 * | \ |
274 * | \ |
275 * | \ |
276 * | \ |
277 * | \ |
278 * | V V
279 *
280 * Closed <-> Closing
281 *
282 * The state argument specifies the eventual state of the backend and the
283 * function transitions to that state via the shortest path.
284 */
285static void set_backend_state(struct backend_info *be,
286 enum xenbus_state state)
287{
288 while (be->state != state) {
289 switch (be->state) {
290 case XenbusStateClosed:
291 switch (state) {
292 case XenbusStateInitWait:
293 case XenbusStateConnected:
294 pr_info("%s: prepare for reconnect\n",
295 be->dev->nodename);
296 backend_switch_state(be, XenbusStateInitWait);
297 break;
298 case XenbusStateClosing:
299 backend_switch_state(be, XenbusStateClosing);
300 break;
301 default:
302 BUG();
303 }
304 break;
305 case XenbusStateInitWait:
306 switch (state) {
307 case XenbusStateConnected:
308 backend_connect(be);
309 backend_switch_state(be, XenbusStateConnected);
310 break;
311 case XenbusStateClosing:
312 case XenbusStateClosed:
313 backend_switch_state(be, XenbusStateClosing);
314 break;
315 default:
316 BUG();
317 }
318 break;
319 case XenbusStateConnected:
320 switch (state) {
321 case XenbusStateInitWait:
322 case XenbusStateClosing:
323 case XenbusStateClosed:
324 backend_disconnect(be);
325 backend_switch_state(be, XenbusStateClosing);
326 break;
327 default:
328 BUG();
329 }
330 break;
331 case XenbusStateClosing:
332 switch (state) {
333 case XenbusStateInitWait:
334 case XenbusStateConnected:
335 case XenbusStateClosed:
336 backend_switch_state(be, XenbusStateClosed);
337 break;
338 default:
339 BUG();
340 }
341 break;
342 default:
343 BUG();
344 }
345 }
346}
347
348/**
349 * Callback received when the frontend's state changes.
350 */
351static void frontend_changed(struct xenbus_device *dev,
352 enum xenbus_state frontend_state)
353{
354 struct backend_info *be = dev_get_drvdata(&dev->dev);
355
356 pr_debug("%s -> %s\n", dev->otherend, xenbus_strstate(frontend_state));
357
358 be->frontend_state = frontend_state;
359
360 switch (frontend_state) {
361 case XenbusStateInitialising:
362 set_backend_state(be, XenbusStateInitWait);
363 break;
364
365 case XenbusStateInitialised:
366 break;
367
368 case XenbusStateConnected:
369 set_backend_state(be, XenbusStateConnected);
370 break;
371
372 case XenbusStateClosing:
373 set_backend_state(be, XenbusStateClosing);
374 break;
375
376 case XenbusStateClosed:
377 set_backend_state(be, XenbusStateClosed);
378 if (xenbus_dev_is_online(dev))
379 break;
380 /* fall through if not online */
381 case XenbusStateUnknown:
382 set_backend_state(be, XenbusStateClosed);
383 device_unregister(&dev->dev);
384 break;
385
386 default:
387 xenbus_dev_fatal(dev, -EINVAL, "saw state %d at frontend",
388 frontend_state);
389 break;
390 }
391}
392
393
394static void xen_net_read_rate(struct xenbus_device *dev,
395 unsigned long *bytes, unsigned long *usec)
396{
397 char *s, *e;
398 unsigned long b, u;
399 char *ratestr;
400
401 /* Default to unlimited bandwidth. */
402 *bytes = ~0UL;
403 *usec = 0;
404
405 ratestr = xenbus_read(XBT_NIL, dev->nodename, "rate", NULL);
406 if (IS_ERR(ratestr))
407 return;
408
409 s = ratestr;
410 b = simple_strtoul(s, &e, 10);
411 if ((s == e) || (*e != ','))
412 goto fail;
413
414 s = e + 1;
415 u = simple_strtoul(s, &e, 10);
416 if ((s == e) || (*e != '\0'))
417 goto fail;
418
419 *bytes = b;
420 *usec = u;
421
422 kfree(ratestr);
423 return;
424
425 fail:
426 pr_warn("Failed to parse network rate limit. Traffic unlimited.\n");
427 kfree(ratestr);
428}
429
430static int xen_net_read_mac(struct xenbus_device *dev, u8 mac[])
431{
432 char *s, *e, *macstr;
433 int i;
434
435 macstr = s = xenbus_read(XBT_NIL, dev->nodename, "mac", NULL);
436 if (IS_ERR(macstr))
437 return PTR_ERR(macstr);
438
439 for (i = 0; i < ETH_ALEN; i++) {
440 mac[i] = simple_strtoul(s, &e, 16);
441 if ((s == e) || (*e != ((i == ETH_ALEN-1) ? '\0' : ':'))) {
442 kfree(macstr);
443 return -ENOENT;
444 }
445 s = e+1;
446 }
447
448 kfree(macstr);
449 return 0;
450}
451
452static void unregister_hotplug_status_watch(struct backend_info *be)
453{
454 if (be->have_hotplug_status_watch) {
455 unregister_xenbus_watch(&be->hotplug_status_watch);
456 kfree(be->hotplug_status_watch.node);
457 }
458 be->have_hotplug_status_watch = 0;
459}
460
461static void hotplug_status_changed(struct xenbus_watch *watch,
462 const char **vec,
463 unsigned int vec_size)
464{
465 struct backend_info *be = container_of(watch,
466 struct backend_info,
467 hotplug_status_watch);
468 char *str;
469 unsigned int len;
470
471 str = xenbus_read(XBT_NIL, be->dev->nodename, "hotplug-status", &len);
472 if (IS_ERR(str))
473 return;
474 if (len == sizeof("connected")-1 && !memcmp(str, "connected", len)) {
475 /* Complete any pending state change */
476 xenbus_switch_state(be->dev, be->state);
477
478 /* Not interested in this watch anymore. */
479 unregister_hotplug_status_watch(be);
480 }
481 kfree(str);
482}
483
484static void connect(struct backend_info *be)
485{
486 int err;
487 struct xenbus_device *dev = be->dev;
488
489 err = connect_rings(be);
490 if (err)
491 return;
492
493 err = xen_net_read_mac(dev, be->vif->fe_dev_addr);
494 if (err) {
495 xenbus_dev_fatal(dev, err, "parsing %s/mac", dev->nodename);
496 return;
497 }
498
499 xen_net_read_rate(dev, &be->vif->credit_bytes,
500 &be->vif->credit_usec);
501 be->vif->remaining_credit = be->vif->credit_bytes;
502
503 unregister_hotplug_status_watch(be);
504 err = xenbus_watch_pathfmt(dev, &be->hotplug_status_watch,
505 hotplug_status_changed,
506 "%s/%s", dev->nodename, "hotplug-status");
507 if (!err)
508 be->have_hotplug_status_watch = 1;
509
510 netif_wake_queue(be->vif->dev);
511}
512
513
514static int connect_rings(struct backend_info *be)
515{
516 struct xenvif *vif = be->vif;
517 struct xenbus_device *dev = be->dev;
518 unsigned long tx_ring_ref, rx_ring_ref;
519 unsigned int tx_evtchn, rx_evtchn, rx_copy;
520 int err;
521 int val;
522
523 err = xenbus_gather(XBT_NIL, dev->otherend,
524 "tx-ring-ref", "%lu", &tx_ring_ref,
525 "rx-ring-ref", "%lu", &rx_ring_ref, NULL);
526 if (err) {
527 xenbus_dev_fatal(dev, err,
528 "reading %s/ring-ref",
529 dev->otherend);
530 return err;
531 }
532
533 /* Try split event channels first, then single event channel. */
534 err = xenbus_gather(XBT_NIL, dev->otherend,
535 "event-channel-tx", "%u", &tx_evtchn,
536 "event-channel-rx", "%u", &rx_evtchn, NULL);
537 if (err < 0) {
538 err = xenbus_scanf(XBT_NIL, dev->otherend,
539 "event-channel", "%u", &tx_evtchn);
540 if (err < 0) {
541 xenbus_dev_fatal(dev, err,
542 "reading %s/event-channel(-tx/rx)",
543 dev->otherend);
544 return err;
545 }
546 rx_evtchn = tx_evtchn;
547 }
548
549 err = xenbus_scanf(XBT_NIL, dev->otherend, "request-rx-copy", "%u",
550 &rx_copy);
551 if (err == -ENOENT) {
552 err = 0;
553 rx_copy = 0;
554 }
555 if (err < 0) {
556 xenbus_dev_fatal(dev, err, "reading %s/request-rx-copy",
557 dev->otherend);
558 return err;
559 }
560 if (!rx_copy)
561 return -EOPNOTSUPP;
562
563 if (vif->dev->tx_queue_len != 0) {
564 if (xenbus_scanf(XBT_NIL, dev->otherend,
565 "feature-rx-notify", "%d", &val) < 0)
566 val = 0;
567 if (val)
568 vif->can_queue = 1;
569 else
570 /* Must be non-zero for pfifo_fast to work. */
571 vif->dev->tx_queue_len = 1;
572 }
573
574 if (xenbus_scanf(XBT_NIL, dev->otherend, "feature-sg",
575 "%d", &val) < 0)
576 val = 0;
577 vif->can_sg = !!val;
578
579 vif->gso_mask = 0;
580 vif->gso_prefix_mask = 0;
581
582 if (xenbus_scanf(XBT_NIL, dev->otherend, "feature-gso-tcpv4",
583 "%d", &val) < 0)
584 val = 0;
585 if (val)
586 vif->gso_mask |= GSO_BIT(TCPV4);
587
588 if (xenbus_scanf(XBT_NIL, dev->otherend, "feature-gso-tcpv4-prefix",
589 "%d", &val) < 0)
590 val = 0;
591 if (val)
592 vif->gso_prefix_mask |= GSO_BIT(TCPV4);
593
594 if (xenbus_scanf(XBT_NIL, dev->otherend, "feature-gso-tcpv6",
595 "%d", &val) < 0)
596 val = 0;
597 if (val)
598 vif->gso_mask |= GSO_BIT(TCPV6);
599
600 if (xenbus_scanf(XBT_NIL, dev->otherend, "feature-gso-tcpv6-prefix",
601 "%d", &val) < 0)
602 val = 0;
603 if (val)
604 vif->gso_prefix_mask |= GSO_BIT(TCPV6);
605
606 if (vif->gso_mask & vif->gso_prefix_mask) {
607 xenbus_dev_fatal(dev, err,
608 "%s: gso and gso prefix flags are not "
609 "mutually exclusive",
610 dev->otherend);
611 return -EOPNOTSUPP;
612 }
613
614 if (xenbus_scanf(XBT_NIL, dev->otherend, "feature-no-csum-offload",
615 "%d", &val) < 0)
616 val = 0;
617 vif->ip_csum = !val;
618
619 if (xenbus_scanf(XBT_NIL, dev->otherend, "feature-ipv6-csum-offload",
620 "%d", &val) < 0)
621 val = 0;
622 vif->ipv6_csum = !!val;
623
624 /* Map the shared frame, irq etc. */
625 err = xenvif_connect(vif, tx_ring_ref, rx_ring_ref,
626 tx_evtchn, rx_evtchn);
627 if (err) {
628 xenbus_dev_fatal(dev, err,
629 "mapping shared-frames %lu/%lu port tx %u rx %u",
630 tx_ring_ref, rx_ring_ref,
631 tx_evtchn, rx_evtchn);
632 return err;
633 }
634 return 0;
635}
636
637
638/* ** Driver Registration ** */
639
640
641static const struct xenbus_device_id netback_ids[] = {
642 { "vif" },
643 { "" }
644};
645
646
647static DEFINE_XENBUS_DRIVER(netback, ,
648 .probe = netback_probe,
649 .remove = netback_remove,
650 .uevent = netback_uevent,
651 .otherend_changed = frontend_changed,
652);
653
654int xenvif_xenbus_init(void)
655{
656 return xenbus_register_backend(&netback_driver);
657}
658
659void xenvif_xenbus_fini(void)
660{
661 return xenbus_unregister_driver(&netback_driver);
662}
1/*
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;
27 enum xenbus_state frontend_state;
28 struct xenbus_watch hotplug_status_watch;
29 u8 have_hotplug_status_watch:1;
30};
31
32static int connect_rings(struct backend_info *);
33static void connect(struct backend_info *);
34static void backend_create_xenvif(struct backend_info *be);
35static void unregister_hotplug_status_watch(struct backend_info *be);
36
37static int netback_remove(struct xenbus_device *dev)
38{
39 struct backend_info *be = dev_get_drvdata(&dev->dev);
40
41 unregister_hotplug_status_watch(be);
42 if (be->vif) {
43 kobject_uevent(&dev->dev.kobj, KOBJ_OFFLINE);
44 xenbus_rm(XBT_NIL, dev->nodename, "hotplug-status");
45 xenvif_disconnect(be->vif);
46 be->vif = NULL;
47 }
48 kfree(be);
49 dev_set_drvdata(&dev->dev, NULL);
50 return 0;
51}
52
53
54/**
55 * Entry point to this code when a new device is created. Allocate the basic
56 * structures and switch to InitWait.
57 */
58static int netback_probe(struct xenbus_device *dev,
59 const struct xenbus_device_id *id)
60{
61 const char *message;
62 struct xenbus_transaction xbt;
63 int err;
64 int sg;
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 }
72
73 be->dev = dev;
74 dev_set_drvdata(&dev->dev, be);
75
76 sg = 1;
77
78 do {
79 err = xenbus_transaction_start(&xbt);
80 if (err) {
81 xenbus_dev_fatal(dev, err, "starting transaction");
82 goto fail;
83 }
84
85 err = xenbus_printf(xbt, dev->nodename, "feature-sg", "%d", sg);
86 if (err) {
87 message = "writing feature-sg";
88 goto abort_transaction;
89 }
90
91 err = xenbus_printf(xbt, dev->nodename, "feature-gso-tcpv4",
92 "%d", sg);
93 if (err) {
94 message = "writing feature-gso-tcpv4";
95 goto abort_transaction;
96 }
97
98 /* We support rx-copy path. */
99 err = xenbus_printf(xbt, dev->nodename,
100 "feature-rx-copy", "%d", 1);
101 if (err) {
102 message = "writing feature-rx-copy";
103 goto abort_transaction;
104 }
105
106 /*
107 * We don't support rx-flip path (except old guests who don't
108 * grok this feature flag).
109 */
110 err = xenbus_printf(xbt, dev->nodename,
111 "feature-rx-flip", "%d", 0);
112 if (err) {
113 message = "writing feature-rx-flip";
114 goto abort_transaction;
115 }
116
117 err = xenbus_transaction_end(xbt, 0);
118 } while (err == -EAGAIN);
119
120 if (err) {
121 xenbus_dev_fatal(dev, err, "completing transaction");
122 goto fail;
123 }
124
125 err = xenbus_switch_state(dev, XenbusStateInitWait);
126 if (err)
127 goto fail;
128
129 /* This kicks hotplug scripts, so do it immediately. */
130 backend_create_xenvif(be);
131
132 return 0;
133
134abort_transaction:
135 xenbus_transaction_end(xbt, 1);
136 xenbus_dev_fatal(dev, err, "%s", message);
137fail:
138 pr_debug("failed");
139 netback_remove(dev);
140 return err;
141}
142
143
144/*
145 * Handle the creation of the hotplug script environment. We add the script
146 * and vif variables to the environment, for the benefit of the vif-* hotplug
147 * scripts.
148 */
149static int netback_uevent(struct xenbus_device *xdev,
150 struct kobj_uevent_env *env)
151{
152 struct backend_info *be = dev_get_drvdata(&xdev->dev);
153 char *val;
154
155 val = xenbus_read(XBT_NIL, xdev->nodename, "script", NULL);
156 if (IS_ERR(val)) {
157 int err = PTR_ERR(val);
158 xenbus_dev_fatal(xdev, err, "reading script");
159 return err;
160 } else {
161 if (add_uevent_var(env, "script=%s", val)) {
162 kfree(val);
163 return -ENOMEM;
164 }
165 kfree(val);
166 }
167
168 if (!be || !be->vif)
169 return 0;
170
171 return add_uevent_var(env, "vif=%s", be->vif->dev->name);
172}
173
174
175static void backend_create_xenvif(struct backend_info *be)
176{
177 int err;
178 long handle;
179 struct xenbus_device *dev = be->dev;
180
181 if (be->vif != NULL)
182 return;
183
184 err = xenbus_scanf(XBT_NIL, dev->nodename, "handle", "%li", &handle);
185 if (err != 1) {
186 xenbus_dev_fatal(dev, err, "reading handle");
187 return;
188 }
189
190 be->vif = xenvif_alloc(&dev->dev, dev->otherend_id, handle);
191 if (IS_ERR(be->vif)) {
192 err = PTR_ERR(be->vif);
193 be->vif = NULL;
194 xenbus_dev_fatal(dev, err, "creating interface");
195 return;
196 }
197
198 kobject_uevent(&dev->dev.kobj, KOBJ_ONLINE);
199}
200
201
202static void disconnect_backend(struct xenbus_device *dev)
203{
204 struct backend_info *be = dev_get_drvdata(&dev->dev);
205
206 if (be->vif) {
207 xenbus_rm(XBT_NIL, dev->nodename, "hotplug-status");
208 xenvif_disconnect(be->vif);
209 be->vif = NULL;
210 }
211}
212
213/**
214 * Callback received when the frontend's state changes.
215 */
216static void frontend_changed(struct xenbus_device *dev,
217 enum xenbus_state frontend_state)
218{
219 struct backend_info *be = dev_get_drvdata(&dev->dev);
220
221 pr_debug("frontend state %s", xenbus_strstate(frontend_state));
222
223 be->frontend_state = frontend_state;
224
225 switch (frontend_state) {
226 case XenbusStateInitialising:
227 if (dev->state == XenbusStateClosed) {
228 printk(KERN_INFO "%s: %s: prepare for reconnect\n",
229 __func__, dev->nodename);
230 xenbus_switch_state(dev, XenbusStateInitWait);
231 }
232 break;
233
234 case XenbusStateInitialised:
235 break;
236
237 case XenbusStateConnected:
238 if (dev->state == XenbusStateConnected)
239 break;
240 backend_create_xenvif(be);
241 if (be->vif)
242 connect(be);
243 break;
244
245 case XenbusStateClosing:
246 if (be->vif)
247 kobject_uevent(&dev->dev.kobj, KOBJ_OFFLINE);
248 disconnect_backend(dev);
249 xenbus_switch_state(dev, XenbusStateClosing);
250 break;
251
252 case XenbusStateClosed:
253 xenbus_switch_state(dev, XenbusStateClosed);
254 if (xenbus_dev_is_online(dev))
255 break;
256 /* fall through if not online */
257 case XenbusStateUnknown:
258 device_unregister(&dev->dev);
259 break;
260
261 default:
262 xenbus_dev_fatal(dev, -EINVAL, "saw state %d at frontend",
263 frontend_state);
264 break;
265 }
266}
267
268
269static void xen_net_read_rate(struct xenbus_device *dev,
270 unsigned long *bytes, unsigned long *usec)
271{
272 char *s, *e;
273 unsigned long b, u;
274 char *ratestr;
275
276 /* Default to unlimited bandwidth. */
277 *bytes = ~0UL;
278 *usec = 0;
279
280 ratestr = xenbus_read(XBT_NIL, dev->nodename, "rate", NULL);
281 if (IS_ERR(ratestr))
282 return;
283
284 s = ratestr;
285 b = simple_strtoul(s, &e, 10);
286 if ((s == e) || (*e != ','))
287 goto fail;
288
289 s = e + 1;
290 u = simple_strtoul(s, &e, 10);
291 if ((s == e) || (*e != '\0'))
292 goto fail;
293
294 *bytes = b;
295 *usec = u;
296
297 kfree(ratestr);
298 return;
299
300 fail:
301 pr_warn("Failed to parse network rate limit. Traffic unlimited.\n");
302 kfree(ratestr);
303}
304
305static int xen_net_read_mac(struct xenbus_device *dev, u8 mac[])
306{
307 char *s, *e, *macstr;
308 int i;
309
310 macstr = s = xenbus_read(XBT_NIL, dev->nodename, "mac", NULL);
311 if (IS_ERR(macstr))
312 return PTR_ERR(macstr);
313
314 for (i = 0; i < ETH_ALEN; i++) {
315 mac[i] = simple_strtoul(s, &e, 16);
316 if ((s == e) || (*e != ((i == ETH_ALEN-1) ? '\0' : ':'))) {
317 kfree(macstr);
318 return -ENOENT;
319 }
320 s = e+1;
321 }
322
323 kfree(macstr);
324 return 0;
325}
326
327static void unregister_hotplug_status_watch(struct backend_info *be)
328{
329 if (be->have_hotplug_status_watch) {
330 unregister_xenbus_watch(&be->hotplug_status_watch);
331 kfree(be->hotplug_status_watch.node);
332 }
333 be->have_hotplug_status_watch = 0;
334}
335
336static void hotplug_status_changed(struct xenbus_watch *watch,
337 const char **vec,
338 unsigned int vec_size)
339{
340 struct backend_info *be = container_of(watch,
341 struct backend_info,
342 hotplug_status_watch);
343 char *str;
344 unsigned int len;
345
346 str = xenbus_read(XBT_NIL, be->dev->nodename, "hotplug-status", &len);
347 if (IS_ERR(str))
348 return;
349 if (len == sizeof("connected")-1 && !memcmp(str, "connected", len)) {
350 xenbus_switch_state(be->dev, XenbusStateConnected);
351 /* Not interested in this watch anymore. */
352 unregister_hotplug_status_watch(be);
353 }
354 kfree(str);
355}
356
357static void connect(struct backend_info *be)
358{
359 int err;
360 struct xenbus_device *dev = be->dev;
361
362 err = connect_rings(be);
363 if (err)
364 return;
365
366 err = xen_net_read_mac(dev, be->vif->fe_dev_addr);
367 if (err) {
368 xenbus_dev_fatal(dev, err, "parsing %s/mac", dev->nodename);
369 return;
370 }
371
372 xen_net_read_rate(dev, &be->vif->credit_bytes,
373 &be->vif->credit_usec);
374 be->vif->remaining_credit = be->vif->credit_bytes;
375
376 unregister_hotplug_status_watch(be);
377 err = xenbus_watch_pathfmt(dev, &be->hotplug_status_watch,
378 hotplug_status_changed,
379 "%s/%s", dev->nodename, "hotplug-status");
380 if (err) {
381 /* Switch now, since we can't do a watch. */
382 xenbus_switch_state(dev, XenbusStateConnected);
383 } else {
384 be->have_hotplug_status_watch = 1;
385 }
386
387 netif_wake_queue(be->vif->dev);
388}
389
390
391static int connect_rings(struct backend_info *be)
392{
393 struct xenvif *vif = be->vif;
394 struct xenbus_device *dev = be->dev;
395 unsigned long tx_ring_ref, rx_ring_ref;
396 unsigned int evtchn, rx_copy;
397 int err;
398 int val;
399
400 err = xenbus_gather(XBT_NIL, dev->otherend,
401 "tx-ring-ref", "%lu", &tx_ring_ref,
402 "rx-ring-ref", "%lu", &rx_ring_ref,
403 "event-channel", "%u", &evtchn, NULL);
404 if (err) {
405 xenbus_dev_fatal(dev, err,
406 "reading %s/ring-ref and event-channel",
407 dev->otherend);
408 return err;
409 }
410
411 err = xenbus_scanf(XBT_NIL, dev->otherend, "request-rx-copy", "%u",
412 &rx_copy);
413 if (err == -ENOENT) {
414 err = 0;
415 rx_copy = 0;
416 }
417 if (err < 0) {
418 xenbus_dev_fatal(dev, err, "reading %s/request-rx-copy",
419 dev->otherend);
420 return err;
421 }
422 if (!rx_copy)
423 return -EOPNOTSUPP;
424
425 if (vif->dev->tx_queue_len != 0) {
426 if (xenbus_scanf(XBT_NIL, dev->otherend,
427 "feature-rx-notify", "%d", &val) < 0)
428 val = 0;
429 if (val)
430 vif->can_queue = 1;
431 else
432 /* Must be non-zero for pfifo_fast to work. */
433 vif->dev->tx_queue_len = 1;
434 }
435
436 if (xenbus_scanf(XBT_NIL, dev->otherend, "feature-sg",
437 "%d", &val) < 0)
438 val = 0;
439 vif->can_sg = !!val;
440
441 if (xenbus_scanf(XBT_NIL, dev->otherend, "feature-gso-tcpv4",
442 "%d", &val) < 0)
443 val = 0;
444 vif->gso = !!val;
445
446 if (xenbus_scanf(XBT_NIL, dev->otherend, "feature-gso-tcpv4-prefix",
447 "%d", &val) < 0)
448 val = 0;
449 vif->gso_prefix = !!val;
450
451 if (xenbus_scanf(XBT_NIL, dev->otherend, "feature-no-csum-offload",
452 "%d", &val) < 0)
453 val = 0;
454 vif->csum = !val;
455
456 /* Map the shared frame, irq etc. */
457 err = xenvif_connect(vif, tx_ring_ref, rx_ring_ref, evtchn);
458 if (err) {
459 xenbus_dev_fatal(dev, err,
460 "mapping shared-frames %lu/%lu port %u",
461 tx_ring_ref, rx_ring_ref, evtchn);
462 return err;
463 }
464 return 0;
465}
466
467
468/* ** Driver Registration ** */
469
470
471static const struct xenbus_device_id netback_ids[] = {
472 { "vif" },
473 { "" }
474};
475
476
477static struct xenbus_driver netback = {
478 .name = "vif",
479 .owner = THIS_MODULE,
480 .ids = netback_ids,
481 .probe = netback_probe,
482 .remove = netback_remove,
483 .uevent = netback_uevent,
484 .otherend_changed = frontend_changed,
485};
486
487int xenvif_xenbus_init(void)
488{
489 return xenbus_register_backend(&netback);
490}