blob: 8079c31ac5e64372fe978405c8faa72981998320 [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
Jeff Kirsheradf8d3f2013-12-06 06:28:47 -080018 * along with this program; if not, see <http://www.gnu.org/licenses/>.
Ian Campbellf942dc22011-03-15 00:06:18 +000019*/
20
21#include "common.h"
Wei Liue9ce7cb2014-06-04 10:30:42 +010022#include <linux/vmalloc.h>
23#include <linux/rtnetlink.h>
Ian Campbellf942dc22011-03-15 00:06:18 +000024
25struct backend_info {
26 struct xenbus_device *dev;
27 struct xenvif *vif;
Paul Durrantea732df2013-09-26 12:09:52 +010028
29 /* This is the state that will be reflected in xenstore when any
30 * active hotplug script completes.
31 */
32 enum xenbus_state state;
33
Ian Campbellf942dc22011-03-15 00:06:18 +000034 enum xenbus_state frontend_state;
35 struct xenbus_watch hotplug_status_watch;
Ian Campbell17938a62011-04-03 22:26:24 +000036 u8 have_hotplug_status_watch:1;
Ian Campbellf942dc22011-03-15 00:06:18 +000037};
38
Wei Liue9ce7cb2014-06-04 10:30:42 +010039static int connect_rings(struct backend_info *be, struct xenvif_queue *queue);
40static void connect(struct backend_info *be);
41static int read_xenbus_vif_flags(struct backend_info *be);
Ian Campbellf942dc22011-03-15 00:06:18 +000042static void backend_create_xenvif(struct backend_info *be);
43static void unregister_hotplug_status_watch(struct backend_info *be);
David Vrabeldc62cca2013-10-07 13:55:19 +010044static void set_backend_state(struct backend_info *be,
45 enum xenbus_state state);
Ian Campbellf942dc22011-03-15 00:06:18 +000046
Zoltan Kissf51de242014-07-08 19:49:14 +010047#ifdef CONFIG_DEBUG_FS
48struct dentry *xen_netback_dbg_root = NULL;
49
50static int xenvif_read_io_ring(struct seq_file *m, void *v)
51{
52 struct xenvif_queue *queue = m->private;
53 struct xen_netif_tx_back_ring *tx_ring = &queue->tx;
54 struct xen_netif_rx_back_ring *rx_ring = &queue->rx;
55
56 if (tx_ring->sring) {
57 struct xen_netif_tx_sring *sring = tx_ring->sring;
58
59 seq_printf(m, "Queue %d\nTX: nr_ents %u\n", queue->id,
60 tx_ring->nr_ents);
61 seq_printf(m, "req prod %u (%d) cons %u (%d) event %u (%d)\n",
62 sring->req_prod,
63 sring->req_prod - sring->rsp_prod,
64 tx_ring->req_cons,
65 tx_ring->req_cons - sring->rsp_prod,
66 sring->req_event,
67 sring->req_event - sring->rsp_prod);
68 seq_printf(m, "rsp prod %u (base) pvt %u (%d) event %u (%d)\n",
69 sring->rsp_prod,
70 tx_ring->rsp_prod_pvt,
71 tx_ring->rsp_prod_pvt - sring->rsp_prod,
72 sring->rsp_event,
73 sring->rsp_event - sring->rsp_prod);
74 seq_printf(m, "pending prod %u pending cons %u nr_pending_reqs %u\n",
75 queue->pending_prod,
76 queue->pending_cons,
77 nr_pending_reqs(queue));
78 seq_printf(m, "dealloc prod %u dealloc cons %u dealloc_queue %u\n\n",
79 queue->dealloc_prod,
80 queue->dealloc_cons,
81 queue->dealloc_prod - queue->dealloc_cons);
82 }
83
84 if (rx_ring->sring) {
85 struct xen_netif_rx_sring *sring = rx_ring->sring;
86
87 seq_printf(m, "RX: nr_ents %u\n", rx_ring->nr_ents);
88 seq_printf(m, "req prod %u (%d) cons %u (%d) event %u (%d)\n",
89 sring->req_prod,
90 sring->req_prod - sring->rsp_prod,
91 rx_ring->req_cons,
92 rx_ring->req_cons - sring->rsp_prod,
93 sring->req_event,
94 sring->req_event - sring->rsp_prod);
95 seq_printf(m, "rsp prod %u (base) pvt %u (%d) event %u (%d)\n\n",
96 sring->rsp_prod,
97 rx_ring->rsp_prod_pvt,
98 rx_ring->rsp_prod_pvt - sring->rsp_prod,
99 sring->rsp_event,
100 sring->rsp_event - sring->rsp_prod);
101 }
102
103 seq_printf(m, "NAPI state: %lx NAPI weight: %d TX queue len %u\n"
104 "Credit timer_pending: %d, credit: %lu, usec: %lu\n"
105 "remaining: %lu, expires: %lu, now: %lu\n",
106 queue->napi.state, queue->napi.weight,
107 skb_queue_len(&queue->tx_queue),
108 timer_pending(&queue->credit_timeout),
109 queue->credit_bytes,
110 queue->credit_usec,
111 queue->remaining_credit,
112 queue->credit_timeout.expires,
113 jiffies);
114
115 return 0;
116}
117
118#define XENVIF_KICK_STR "kick"
Wei Liu5c807002014-08-12 11:59:29 +0100119#define BUFFER_SIZE 32
Zoltan Kissf51de242014-07-08 19:49:14 +0100120
121static ssize_t
122xenvif_write_io_ring(struct file *filp, const char __user *buf, size_t count,
123 loff_t *ppos)
124{
125 struct xenvif_queue *queue =
126 ((struct seq_file *)filp->private_data)->private;
127 int len;
Wei Liu5c807002014-08-12 11:59:29 +0100128 char write[BUFFER_SIZE];
Zoltan Kissf51de242014-07-08 19:49:14 +0100129
130 /* don't allow partial writes and check the length */
131 if (*ppos != 0)
132 return 0;
Wei Liu5c807002014-08-12 11:59:29 +0100133 if (count >= sizeof(write))
Zoltan Kissf51de242014-07-08 19:49:14 +0100134 return -ENOSPC;
135
136 len = simple_write_to_buffer(write,
Wei Liu5c807002014-08-12 11:59:29 +0100137 sizeof(write) - 1,
Zoltan Kissf51de242014-07-08 19:49:14 +0100138 ppos,
139 buf,
140 count);
141 if (len < 0)
142 return len;
143
Wei Liu5c807002014-08-12 11:59:29 +0100144 write[len] = '\0';
145
Zoltan Kissf51de242014-07-08 19:49:14 +0100146 if (!strncmp(write, XENVIF_KICK_STR, sizeof(XENVIF_KICK_STR) - 1))
147 xenvif_interrupt(0, (void *)queue);
148 else {
149 pr_warn("Unknown command to io_ring_q%d. Available: kick\n",
150 queue->id);
151 count = -EINVAL;
152 }
153 return count;
154}
155
156static int xenvif_dump_open(struct inode *inode, struct file *filp)
157{
158 int ret;
159 void *queue = NULL;
160
161 if (inode->i_private)
162 queue = inode->i_private;
163 ret = single_open(filp, xenvif_read_io_ring, queue);
164 filp->f_mode |= FMODE_PWRITE;
165 return ret;
166}
167
168static const struct file_operations xenvif_dbg_io_ring_ops_fops = {
169 .owner = THIS_MODULE,
170 .open = xenvif_dump_open,
171 .read = seq_read,
172 .llseek = seq_lseek,
173 .release = single_release,
174 .write = xenvif_write_io_ring,
175};
176
Wei Liu628fa762014-08-12 11:59:30 +0100177static void xenvif_debugfs_addif(struct xenvif *vif)
Zoltan Kissf51de242014-07-08 19:49:14 +0100178{
179 struct dentry *pfile;
Zoltan Kissf51de242014-07-08 19:49:14 +0100180 int i;
181
182 if (IS_ERR_OR_NULL(xen_netback_dbg_root))
183 return;
184
185 vif->xenvif_dbg_root = debugfs_create_dir(vif->dev->name,
186 xen_netback_dbg_root);
187 if (!IS_ERR_OR_NULL(vif->xenvif_dbg_root)) {
188 for (i = 0; i < vif->num_queues; ++i) {
189 char filename[sizeof("io_ring_q") + 4];
190
191 snprintf(filename, sizeof(filename), "io_ring_q%d", i);
192 pfile = debugfs_create_file(filename,
193 S_IRUSR | S_IWUSR,
194 vif->xenvif_dbg_root,
195 &vif->queues[i],
196 &xenvif_dbg_io_ring_ops_fops);
197 if (IS_ERR_OR_NULL(pfile))
198 pr_warn("Creation of io_ring file returned %ld!\n",
199 PTR_ERR(pfile));
200 }
201 } else
202 netdev_warn(vif->dev,
203 "Creation of vif debugfs dir returned %ld!\n",
204 PTR_ERR(vif->xenvif_dbg_root));
205}
206
207static void xenvif_debugfs_delif(struct xenvif *vif)
208{
209 if (IS_ERR_OR_NULL(xen_netback_dbg_root))
210 return;
211
212 if (!IS_ERR_OR_NULL(vif->xenvif_dbg_root))
213 debugfs_remove_recursive(vif->xenvif_dbg_root);
214 vif->xenvif_dbg_root = NULL;
215}
216#endif /* CONFIG_DEBUG_FS */
217
Ian Campbellf942dc22011-03-15 00:06:18 +0000218static int netback_remove(struct xenbus_device *dev)
219{
220 struct backend_info *be = dev_get_drvdata(&dev->dev);
221
David Vrabeldc62cca2013-10-07 13:55:19 +0100222 set_backend_state(be, XenbusStateClosed);
223
Ian Campbellf942dc22011-03-15 00:06:18 +0000224 unregister_hotplug_status_watch(be);
225 if (be->vif) {
226 kobject_uevent(&dev->dev.kobj, KOBJ_OFFLINE);
227 xenbus_rm(XBT_NIL, dev->nodename, "hotplug-status");
Paul Durrant279f4382013-09-17 17:46:08 +0100228 xenvif_free(be->vif);
Ian Campbellf942dc22011-03-15 00:06:18 +0000229 be->vif = NULL;
230 }
231 kfree(be);
232 dev_set_drvdata(&dev->dev, NULL);
233 return 0;
234}
235
236
237/**
238 * Entry point to this code when a new device is created. Allocate the basic
239 * structures and switch to InitWait.
240 */
241static int netback_probe(struct xenbus_device *dev,
242 const struct xenbus_device_id *id)
243{
244 const char *message;
245 struct xenbus_transaction xbt;
246 int err;
247 int sg;
248 struct backend_info *be = kzalloc(sizeof(struct backend_info),
249 GFP_KERNEL);
250 if (!be) {
251 xenbus_dev_fatal(dev, -ENOMEM,
252 "allocating backend structure");
253 return -ENOMEM;
254 }
255
256 be->dev = dev;
257 dev_set_drvdata(&dev->dev, be);
258
259 sg = 1;
260
261 do {
262 err = xenbus_transaction_start(&xbt);
263 if (err) {
264 xenbus_dev_fatal(dev, err, "starting transaction");
265 goto fail;
266 }
267
268 err = xenbus_printf(xbt, dev->nodename, "feature-sg", "%d", sg);
269 if (err) {
270 message = "writing feature-sg";
271 goto abort_transaction;
272 }
273
274 err = xenbus_printf(xbt, dev->nodename, "feature-gso-tcpv4",
275 "%d", sg);
276 if (err) {
277 message = "writing feature-gso-tcpv4";
278 goto abort_transaction;
279 }
280
Paul Durranta9468582013-10-16 17:50:31 +0100281 err = xenbus_printf(xbt, dev->nodename, "feature-gso-tcpv6",
282 "%d", sg);
283 if (err) {
284 message = "writing feature-gso-tcpv6";
285 goto abort_transaction;
286 }
287
Paul Durrant2eba61d2013-10-16 17:50:29 +0100288 /* We support partial checksum setup for IPv6 packets */
289 err = xenbus_printf(xbt, dev->nodename,
290 "feature-ipv6-csum-offload",
291 "%d", 1);
292 if (err) {
293 message = "writing feature-ipv6-csum-offload";
294 goto abort_transaction;
295 }
296
Ian Campbellf942dc22011-03-15 00:06:18 +0000297 /* We support rx-copy path. */
298 err = xenbus_printf(xbt, dev->nodename,
299 "feature-rx-copy", "%d", 1);
300 if (err) {
301 message = "writing feature-rx-copy";
302 goto abort_transaction;
303 }
304
305 /*
306 * We don't support rx-flip path (except old guests who don't
307 * grok this feature flag).
308 */
309 err = xenbus_printf(xbt, dev->nodename,
310 "feature-rx-flip", "%d", 0);
311 if (err) {
312 message = "writing feature-rx-flip";
313 goto abort_transaction;
314 }
315
316 err = xenbus_transaction_end(xbt, 0);
317 } while (err == -EAGAIN);
318
319 if (err) {
320 xenbus_dev_fatal(dev, err, "completing transaction");
321 goto fail;
322 }
323
Wei Liue1f00a692013-05-22 06:34:45 +0000324 /*
325 * Split event channels support, this is optional so it is not
326 * put inside the above loop.
327 */
328 err = xenbus_printf(XBT_NIL, dev->nodename,
329 "feature-split-event-channels",
330 "%u", separate_tx_rx_irq);
331 if (err)
Wei Liu8ef2c3b2013-07-02 00:08:54 +0100332 pr_debug("Error writing feature-split-event-channels\n");
Wei Liue1f00a692013-05-22 06:34:45 +0000333
Andrew J. Bennieston8d3d53b2014-06-04 10:30:43 +0100334 /* Multi-queue support: This is an optional feature. */
335 err = xenbus_printf(XBT_NIL, dev->nodename,
336 "multi-queue-max-queues", "%u", xenvif_max_queues);
337 if (err)
338 pr_debug("Error writing multi-queue-max-queues\n");
339
Ian Campbellf942dc22011-03-15 00:06:18 +0000340 err = xenbus_switch_state(dev, XenbusStateInitWait);
341 if (err)
342 goto fail;
343
Paul Durrantea732df2013-09-26 12:09:52 +0100344 be->state = XenbusStateInitWait;
345
Ian Campbellf942dc22011-03-15 00:06:18 +0000346 /* This kicks hotplug scripts, so do it immediately. */
347 backend_create_xenvif(be);
348
349 return 0;
350
351abort_transaction:
352 xenbus_transaction_end(xbt, 1);
353 xenbus_dev_fatal(dev, err, "%s", message);
354fail:
Wei Liu8ef2c3b2013-07-02 00:08:54 +0100355 pr_debug("failed\n");
Ian Campbellf942dc22011-03-15 00:06:18 +0000356 netback_remove(dev);
357 return err;
358}
359
360
361/*
362 * Handle the creation of the hotplug script environment. We add the script
363 * and vif variables to the environment, for the benefit of the vif-* hotplug
364 * scripts.
365 */
366static int netback_uevent(struct xenbus_device *xdev,
367 struct kobj_uevent_env *env)
368{
369 struct backend_info *be = dev_get_drvdata(&xdev->dev);
370 char *val;
371
372 val = xenbus_read(XBT_NIL, xdev->nodename, "script", NULL);
373 if (IS_ERR(val)) {
374 int err = PTR_ERR(val);
375 xenbus_dev_fatal(xdev, err, "reading script");
376 return err;
377 } else {
378 if (add_uevent_var(env, "script=%s", val)) {
379 kfree(val);
380 return -ENOMEM;
381 }
382 kfree(val);
383 }
384
385 if (!be || !be->vif)
386 return 0;
387
388 return add_uevent_var(env, "vif=%s", be->vif->dev->name);
389}
390
391
392static void backend_create_xenvif(struct backend_info *be)
393{
394 int err;
395 long handle;
396 struct xenbus_device *dev = be->dev;
397
398 if (be->vif != NULL)
399 return;
400
401 err = xenbus_scanf(XBT_NIL, dev->nodename, "handle", "%li", &handle);
402 if (err != 1) {
403 xenbus_dev_fatal(dev, err, "reading handle");
404 return;
405 }
406
407 be->vif = xenvif_alloc(&dev->dev, dev->otherend_id, handle);
408 if (IS_ERR(be->vif)) {
409 err = PTR_ERR(be->vif);
410 be->vif = NULL;
411 xenbus_dev_fatal(dev, err, "creating interface");
412 return;
413 }
414
415 kobject_uevent(&dev->dev.kobj, KOBJ_ONLINE);
416}
417
Paul Durrantea732df2013-09-26 12:09:52 +0100418static void backend_disconnect(struct backend_info *be)
Ian Campbellf942dc22011-03-15 00:06:18 +0000419{
Zoltan Kissf51de242014-07-08 19:49:14 +0100420 if (be->vif) {
421#ifdef CONFIG_DEBUG_FS
422 xenvif_debugfs_delif(be->vif);
423#endif /* CONFIG_DEBUG_FS */
Ian Campbellf942dc22011-03-15 00:06:18 +0000424 xenvif_disconnect(be->vif);
Zoltan Kissf51de242014-07-08 19:49:14 +0100425 }
Paul Durrant279f4382013-09-17 17:46:08 +0100426}
427
Paul Durrantea732df2013-09-26 12:09:52 +0100428static void backend_connect(struct backend_info *be)
Paul Durrant279f4382013-09-17 17:46:08 +0100429{
Paul Durrantea732df2013-09-26 12:09:52 +0100430 if (be->vif)
431 connect(be);
432}
Paul Durrant279f4382013-09-17 17:46:08 +0100433
Paul Durrantea732df2013-09-26 12:09:52 +0100434static inline void backend_switch_state(struct backend_info *be,
435 enum xenbus_state state)
436{
437 struct xenbus_device *dev = be->dev;
438
439 pr_debug("%s -> %s\n", dev->nodename, xenbus_strstate(state));
440 be->state = state;
441
442 /* If we are waiting for a hotplug script then defer the
443 * actual xenbus state change.
444 */
445 if (!be->have_hotplug_status_watch)
446 xenbus_switch_state(dev, state);
447}
448
449/* Handle backend state transitions:
450 *
451 * The backend state starts in InitWait and the following transitions are
452 * allowed.
453 *
454 * InitWait -> Connected
455 *
456 * ^ \ |
457 * | \ |
458 * | \ |
459 * | \ |
460 * | \ |
461 * | \ |
462 * | V V
463 *
464 * Closed <-> Closing
465 *
466 * The state argument specifies the eventual state of the backend and the
467 * function transitions to that state via the shortest path.
468 */
469static void set_backend_state(struct backend_info *be,
470 enum xenbus_state state)
471{
472 while (be->state != state) {
473 switch (be->state) {
474 case XenbusStateClosed:
475 switch (state) {
476 case XenbusStateInitWait:
477 case XenbusStateConnected:
478 pr_info("%s: prepare for reconnect\n",
479 be->dev->nodename);
480 backend_switch_state(be, XenbusStateInitWait);
481 break;
482 case XenbusStateClosing:
483 backend_switch_state(be, XenbusStateClosing);
484 break;
485 default:
486 BUG();
487 }
488 break;
489 case XenbusStateInitWait:
490 switch (state) {
491 case XenbusStateConnected:
492 backend_connect(be);
493 backend_switch_state(be, XenbusStateConnected);
494 break;
495 case XenbusStateClosing:
496 case XenbusStateClosed:
497 backend_switch_state(be, XenbusStateClosing);
498 break;
499 default:
500 BUG();
501 }
502 break;
503 case XenbusStateConnected:
504 switch (state) {
505 case XenbusStateInitWait:
506 case XenbusStateClosing:
507 case XenbusStateClosed:
508 backend_disconnect(be);
509 backend_switch_state(be, XenbusStateClosing);
510 break;
511 default:
512 BUG();
513 }
514 break;
515 case XenbusStateClosing:
516 switch (state) {
517 case XenbusStateInitWait:
518 case XenbusStateConnected:
519 case XenbusStateClosed:
520 backend_switch_state(be, XenbusStateClosed);
521 break;
522 default:
523 BUG();
524 }
525 break;
526 default:
527 BUG();
528 }
Ian Campbellf942dc22011-03-15 00:06:18 +0000529 }
530}
531
532/**
533 * Callback received when the frontend's state changes.
534 */
535static void frontend_changed(struct xenbus_device *dev,
536 enum xenbus_state frontend_state)
537{
538 struct backend_info *be = dev_get_drvdata(&dev->dev);
539
Paul Durrantea732df2013-09-26 12:09:52 +0100540 pr_debug("%s -> %s\n", dev->otherend, xenbus_strstate(frontend_state));
Ian Campbellf942dc22011-03-15 00:06:18 +0000541
542 be->frontend_state = frontend_state;
543
544 switch (frontend_state) {
545 case XenbusStateInitialising:
Paul Durrantea732df2013-09-26 12:09:52 +0100546 set_backend_state(be, XenbusStateInitWait);
Ian Campbellf942dc22011-03-15 00:06:18 +0000547 break;
548
549 case XenbusStateInitialised:
550 break;
551
552 case XenbusStateConnected:
Paul Durrantea732df2013-09-26 12:09:52 +0100553 set_backend_state(be, XenbusStateConnected);
Ian Campbellf942dc22011-03-15 00:06:18 +0000554 break;
555
556 case XenbusStateClosing:
Paul Durrantea732df2013-09-26 12:09:52 +0100557 set_backend_state(be, XenbusStateClosing);
Ian Campbellf942dc22011-03-15 00:06:18 +0000558 break;
559
560 case XenbusStateClosed:
Paul Durrantea732df2013-09-26 12:09:52 +0100561 set_backend_state(be, XenbusStateClosed);
Ian Campbellf942dc22011-03-15 00:06:18 +0000562 if (xenbus_dev_is_online(dev))
563 break;
564 /* fall through if not online */
565 case XenbusStateUnknown:
Paul Durrantea732df2013-09-26 12:09:52 +0100566 set_backend_state(be, XenbusStateClosed);
Ian Campbellf942dc22011-03-15 00:06:18 +0000567 device_unregister(&dev->dev);
568 break;
569
570 default:
571 xenbus_dev_fatal(dev, -EINVAL, "saw state %d at frontend",
572 frontend_state);
573 break;
574 }
575}
576
577
578static void xen_net_read_rate(struct xenbus_device *dev,
579 unsigned long *bytes, unsigned long *usec)
580{
581 char *s, *e;
582 unsigned long b, u;
583 char *ratestr;
584
585 /* Default to unlimited bandwidth. */
586 *bytes = ~0UL;
587 *usec = 0;
588
589 ratestr = xenbus_read(XBT_NIL, dev->nodename, "rate", NULL);
590 if (IS_ERR(ratestr))
591 return;
592
593 s = ratestr;
594 b = simple_strtoul(s, &e, 10);
595 if ((s == e) || (*e != ','))
596 goto fail;
597
598 s = e + 1;
599 u = simple_strtoul(s, &e, 10);
600 if ((s == e) || (*e != '\0'))
601 goto fail;
602
603 *bytes = b;
604 *usec = u;
605
606 kfree(ratestr);
607 return;
608
609 fail:
610 pr_warn("Failed to parse network rate limit. Traffic unlimited.\n");
611 kfree(ratestr);
612}
613
614static int xen_net_read_mac(struct xenbus_device *dev, u8 mac[])
615{
616 char *s, *e, *macstr;
617 int i;
618
619 macstr = s = xenbus_read(XBT_NIL, dev->nodename, "mac", NULL);
620 if (IS_ERR(macstr))
621 return PTR_ERR(macstr);
622
623 for (i = 0; i < ETH_ALEN; i++) {
624 mac[i] = simple_strtoul(s, &e, 16);
625 if ((s == e) || (*e != ((i == ETH_ALEN-1) ? '\0' : ':'))) {
626 kfree(macstr);
627 return -ENOENT;
628 }
629 s = e+1;
630 }
631
632 kfree(macstr);
633 return 0;
634}
635
636static void unregister_hotplug_status_watch(struct backend_info *be)
637{
638 if (be->have_hotplug_status_watch) {
639 unregister_xenbus_watch(&be->hotplug_status_watch);
640 kfree(be->hotplug_status_watch.node);
641 }
642 be->have_hotplug_status_watch = 0;
643}
644
645static void hotplug_status_changed(struct xenbus_watch *watch,
646 const char **vec,
647 unsigned int vec_size)
648{
649 struct backend_info *be = container_of(watch,
650 struct backend_info,
651 hotplug_status_watch);
652 char *str;
653 unsigned int len;
654
655 str = xenbus_read(XBT_NIL, be->dev->nodename, "hotplug-status", &len);
656 if (IS_ERR(str))
657 return;
658 if (len == sizeof("connected")-1 && !memcmp(str, "connected", len)) {
Paul Durrantea732df2013-09-26 12:09:52 +0100659 /* Complete any pending state change */
660 xenbus_switch_state(be->dev, be->state);
661
Ian Campbellf942dc22011-03-15 00:06:18 +0000662 /* Not interested in this watch anymore. */
663 unregister_hotplug_status_watch(be);
664 }
665 kfree(str);
666}
667
668static void connect(struct backend_info *be)
669{
670 int err;
671 struct xenbus_device *dev = be->dev;
Wei Liue9ce7cb2014-06-04 10:30:42 +0100672 unsigned long credit_bytes, credit_usec;
673 unsigned int queue_index;
Andrew J. Bennieston8d3d53b2014-06-04 10:30:43 +0100674 unsigned int requested_num_queues;
Wei Liue9ce7cb2014-06-04 10:30:42 +0100675 struct xenvif_queue *queue;
Ian Campbellf942dc22011-03-15 00:06:18 +0000676
Andrew J. Bennieston8d3d53b2014-06-04 10:30:43 +0100677 /* Check whether the frontend requested multiple queues
678 * and read the number requested.
679 */
680 err = xenbus_scanf(XBT_NIL, dev->otherend,
681 "multi-queue-num-queues",
682 "%u", &requested_num_queues);
683 if (err < 0) {
684 requested_num_queues = 1; /* Fall back to single queue */
685 } else if (requested_num_queues > xenvif_max_queues) {
686 /* buggy or malicious guest */
687 xenbus_dev_fatal(dev, err,
688 "guest requested %u queues, exceeding the maximum of %u.",
689 requested_num_queues, xenvif_max_queues);
690 return;
691 }
692
Ian Campbellf942dc22011-03-15 00:06:18 +0000693 err = xen_net_read_mac(dev, be->vif->fe_dev_addr);
694 if (err) {
695 xenbus_dev_fatal(dev, err, "parsing %s/mac", dev->nodename);
696 return;
697 }
698
Wei Liue9ce7cb2014-06-04 10:30:42 +0100699 xen_net_read_rate(dev, &credit_bytes, &credit_usec);
700 read_xenbus_vif_flags(be);
701
Andrew J. Bennieston8d3d53b2014-06-04 10:30:43 +0100702 /* Use the number of queues requested by the frontend */
Wei Liue9ce7cb2014-06-04 10:30:42 +0100703 be->vif->queues = vzalloc(requested_num_queues *
704 sizeof(struct xenvif_queue));
Wei Liuf7b50c42014-06-23 10:50:17 +0100705 be->vif->num_queues = requested_num_queues;
Wei Liue9ce7cb2014-06-04 10:30:42 +0100706
707 for (queue_index = 0; queue_index < requested_num_queues; ++queue_index) {
708 queue = &be->vif->queues[queue_index];
709 queue->vif = be->vif;
710 queue->id = queue_index;
711 snprintf(queue->name, sizeof(queue->name), "%s-q%u",
712 be->vif->dev->name, queue->id);
713
714 err = xenvif_init_queue(queue);
Andrew J. Bennieston8d3d53b2014-06-04 10:30:43 +0100715 if (err) {
716 /* xenvif_init_queue() cleans up after itself on
717 * failure, but we need to clean up any previously
718 * initialised queues. Set num_queues to i so that
719 * earlier queues can be destroyed using the regular
720 * disconnect logic.
721 */
Wei Liuf7b50c42014-06-23 10:50:17 +0100722 be->vif->num_queues = queue_index;
Wei Liue9ce7cb2014-06-04 10:30:42 +0100723 goto err;
Andrew J. Bennieston8d3d53b2014-06-04 10:30:43 +0100724 }
Wei Liue9ce7cb2014-06-04 10:30:42 +0100725
726 queue->remaining_credit = credit_bytes;
727
728 err = connect_rings(be, queue);
Andrew J. Bennieston8d3d53b2014-06-04 10:30:43 +0100729 if (err) {
730 /* connect_rings() cleans up after itself on failure,
731 * but we need to clean up after xenvif_init_queue() here,
732 * and also clean up any previously initialised queues.
733 */
734 xenvif_deinit_queue(queue);
Wei Liuf7b50c42014-06-23 10:50:17 +0100735 be->vif->num_queues = queue_index;
Wei Liue9ce7cb2014-06-04 10:30:42 +0100736 goto err;
Andrew J. Bennieston8d3d53b2014-06-04 10:30:43 +0100737 }
Wei Liue9ce7cb2014-06-04 10:30:42 +0100738 }
739
Wei Liu628fa762014-08-12 11:59:30 +0100740#ifdef CONFIG_DEBUG_FS
741 xenvif_debugfs_addif(be->vif);
742#endif /* CONFIG_DEBUG_FS */
743
Wei Liuf7b50c42014-06-23 10:50:17 +0100744 /* Initialisation completed, tell core driver the number of
745 * active queues.
746 */
747 rtnl_lock();
748 netif_set_real_num_tx_queues(be->vif->dev, requested_num_queues);
749 netif_set_real_num_rx_queues(be->vif->dev, requested_num_queues);
750 rtnl_unlock();
751
Wei Liue9ce7cb2014-06-04 10:30:42 +0100752 xenvif_carrier_on(be->vif);
Ian Campbellf942dc22011-03-15 00:06:18 +0000753
754 unregister_hotplug_status_watch(be);
755 err = xenbus_watch_pathfmt(dev, &be->hotplug_status_watch,
756 hotplug_status_changed,
757 "%s/%s", dev->nodename, "hotplug-status");
Paul Durrantea732df2013-09-26 12:09:52 +0100758 if (!err)
Ian Campbellf942dc22011-03-15 00:06:18 +0000759 be->have_hotplug_status_watch = 1;
Ian Campbellf942dc22011-03-15 00:06:18 +0000760
Wei Liue9ce7cb2014-06-04 10:30:42 +0100761 netif_tx_wake_all_queues(be->vif->dev);
762
763 return;
764
765err:
Wei Liuf7b50c42014-06-23 10:50:17 +0100766 if (be->vif->num_queues > 0)
Andrew J. Bennieston8d3d53b2014-06-04 10:30:43 +0100767 xenvif_disconnect(be->vif); /* Clean up existing queues */
Wei Liue9ce7cb2014-06-04 10:30:42 +0100768 vfree(be->vif->queues);
769 be->vif->queues = NULL;
Wei Liuf7b50c42014-06-23 10:50:17 +0100770 be->vif->num_queues = 0;
Wei Liue9ce7cb2014-06-04 10:30:42 +0100771 return;
Ian Campbellf942dc22011-03-15 00:06:18 +0000772}
773
774
Wei Liue9ce7cb2014-06-04 10:30:42 +0100775static int connect_rings(struct backend_info *be, struct xenvif_queue *queue)
Ian Campbellf942dc22011-03-15 00:06:18 +0000776{
Ian Campbellf942dc22011-03-15 00:06:18 +0000777 struct xenbus_device *dev = be->dev;
Wei Liuf7b50c42014-06-23 10:50:17 +0100778 unsigned int num_queues = queue->vif->num_queues;
Ian Campbellf942dc22011-03-15 00:06:18 +0000779 unsigned long tx_ring_ref, rx_ring_ref;
Wei Liue9ce7cb2014-06-04 10:30:42 +0100780 unsigned int tx_evtchn, rx_evtchn;
Ian Campbellf942dc22011-03-15 00:06:18 +0000781 int err;
Andrew J. Bennieston8d3d53b2014-06-04 10:30:43 +0100782 char *xspath;
783 size_t xspathsize;
784 const size_t xenstore_path_ext_size = 11; /* sufficient for "/queue-NNN" */
Ian Campbellf942dc22011-03-15 00:06:18 +0000785
Andrew J. Bennieston8d3d53b2014-06-04 10:30:43 +0100786 /* If the frontend requested 1 queue, or we have fallen back
787 * to single queue due to lack of frontend support for multi-
788 * queue, expect the remaining XenStore keys in the toplevel
789 * directory. Otherwise, expect them in a subdirectory called
790 * queue-N.
791 */
792 if (num_queues == 1) {
793 xspath = kzalloc(strlen(dev->otherend) + 1, GFP_KERNEL);
794 if (!xspath) {
795 xenbus_dev_fatal(dev, -ENOMEM,
796 "reading ring references");
797 return -ENOMEM;
798 }
799 strcpy(xspath, dev->otherend);
800 } else {
801 xspathsize = strlen(dev->otherend) + xenstore_path_ext_size;
802 xspath = kzalloc(xspathsize, GFP_KERNEL);
803 if (!xspath) {
804 xenbus_dev_fatal(dev, -ENOMEM,
805 "reading ring references");
806 return -ENOMEM;
807 }
808 snprintf(xspath, xspathsize, "%s/queue-%u", dev->otherend,
809 queue->id);
810 }
811
812 err = xenbus_gather(XBT_NIL, xspath,
Ian Campbellf942dc22011-03-15 00:06:18 +0000813 "tx-ring-ref", "%lu", &tx_ring_ref,
Wei Liue1f00a692013-05-22 06:34:45 +0000814 "rx-ring-ref", "%lu", &rx_ring_ref, NULL);
Ian Campbellf942dc22011-03-15 00:06:18 +0000815 if (err) {
816 xenbus_dev_fatal(dev, err,
Wei Liue1f00a692013-05-22 06:34:45 +0000817 "reading %s/ring-ref",
Andrew J. Bennieston8d3d53b2014-06-04 10:30:43 +0100818 xspath);
819 goto err;
Ian Campbellf942dc22011-03-15 00:06:18 +0000820 }
821
Wei Liue1f00a692013-05-22 06:34:45 +0000822 /* Try split event channels first, then single event channel. */
Andrew J. Bennieston8d3d53b2014-06-04 10:30:43 +0100823 err = xenbus_gather(XBT_NIL, xspath,
Wei Liue1f00a692013-05-22 06:34:45 +0000824 "event-channel-tx", "%u", &tx_evtchn,
825 "event-channel-rx", "%u", &rx_evtchn, NULL);
826 if (err < 0) {
Andrew J. Bennieston8d3d53b2014-06-04 10:30:43 +0100827 err = xenbus_scanf(XBT_NIL, xspath,
Wei Liue1f00a692013-05-22 06:34:45 +0000828 "event-channel", "%u", &tx_evtchn);
829 if (err < 0) {
830 xenbus_dev_fatal(dev, err,
831 "reading %s/event-channel(-tx/rx)",
Andrew J. Bennieston8d3d53b2014-06-04 10:30:43 +0100832 xspath);
833 goto err;
Wei Liue1f00a692013-05-22 06:34:45 +0000834 }
835 rx_evtchn = tx_evtchn;
836 }
837
Wei Liue9ce7cb2014-06-04 10:30:42 +0100838 /* Map the shared frame, irq etc. */
839 err = xenvif_connect(queue, tx_ring_ref, rx_ring_ref,
840 tx_evtchn, rx_evtchn);
841 if (err) {
842 xenbus_dev_fatal(dev, err,
843 "mapping shared-frames %lu/%lu port tx %u rx %u",
844 tx_ring_ref, rx_ring_ref,
845 tx_evtchn, rx_evtchn);
Andrew J. Bennieston8d3d53b2014-06-04 10:30:43 +0100846 goto err;
Wei Liue9ce7cb2014-06-04 10:30:42 +0100847 }
848
Andrew J. Bennieston8d3d53b2014-06-04 10:30:43 +0100849 err = 0;
850err: /* Regular return falls through with err == 0 */
851 kfree(xspath);
852 return err;
Wei Liue9ce7cb2014-06-04 10:30:42 +0100853}
854
855static int read_xenbus_vif_flags(struct backend_info *be)
856{
857 struct xenvif *vif = be->vif;
858 struct xenbus_device *dev = be->dev;
859 unsigned int rx_copy;
860 int err, val;
861
Ian Campbellf942dc22011-03-15 00:06:18 +0000862 err = xenbus_scanf(XBT_NIL, dev->otherend, "request-rx-copy", "%u",
863 &rx_copy);
864 if (err == -ENOENT) {
865 err = 0;
866 rx_copy = 0;
867 }
868 if (err < 0) {
869 xenbus_dev_fatal(dev, err, "reading %s/request-rx-copy",
870 dev->otherend);
871 return err;
872 }
873 if (!rx_copy)
874 return -EOPNOTSUPP;
875
876 if (vif->dev->tx_queue_len != 0) {
877 if (xenbus_scanf(XBT_NIL, dev->otherend,
878 "feature-rx-notify", "%d", &val) < 0)
879 val = 0;
880 if (val)
881 vif->can_queue = 1;
882 else
883 /* Must be non-zero for pfifo_fast to work. */
884 vif->dev->tx_queue_len = 1;
885 }
886
887 if (xenbus_scanf(XBT_NIL, dev->otherend, "feature-sg",
888 "%d", &val) < 0)
889 val = 0;
890 vif->can_sg = !!val;
891
Paul Durrant82cada22013-10-16 17:50:32 +0100892 vif->gso_mask = 0;
893 vif->gso_prefix_mask = 0;
894
Ian Campbellf942dc22011-03-15 00:06:18 +0000895 if (xenbus_scanf(XBT_NIL, dev->otherend, "feature-gso-tcpv4",
896 "%d", &val) < 0)
897 val = 0;
Paul Durrant82cada22013-10-16 17:50:32 +0100898 if (val)
899 vif->gso_mask |= GSO_BIT(TCPV4);
Ian Campbellf942dc22011-03-15 00:06:18 +0000900
901 if (xenbus_scanf(XBT_NIL, dev->otherend, "feature-gso-tcpv4-prefix",
902 "%d", &val) < 0)
903 val = 0;
Paul Durrant82cada22013-10-16 17:50:32 +0100904 if (val)
905 vif->gso_prefix_mask |= GSO_BIT(TCPV4);
906
907 if (xenbus_scanf(XBT_NIL, dev->otherend, "feature-gso-tcpv6",
908 "%d", &val) < 0)
909 val = 0;
910 if (val)
911 vif->gso_mask |= GSO_BIT(TCPV6);
912
913 if (xenbus_scanf(XBT_NIL, dev->otherend, "feature-gso-tcpv6-prefix",
914 "%d", &val) < 0)
915 val = 0;
916 if (val)
917 vif->gso_prefix_mask |= GSO_BIT(TCPV6);
918
919 if (vif->gso_mask & vif->gso_prefix_mask) {
920 xenbus_dev_fatal(dev, err,
921 "%s: gso and gso prefix flags are not "
922 "mutually exclusive",
923 dev->otherend);
924 return -EOPNOTSUPP;
925 }
Ian Campbellf942dc22011-03-15 00:06:18 +0000926
927 if (xenbus_scanf(XBT_NIL, dev->otherend, "feature-no-csum-offload",
928 "%d", &val) < 0)
929 val = 0;
Paul Durrant146c8a72013-10-16 17:50:28 +0100930 vif->ip_csum = !val;
931
932 if (xenbus_scanf(XBT_NIL, dev->otherend, "feature-ipv6-csum-offload",
933 "%d", &val) < 0)
934 val = 0;
935 vif->ipv6_csum = !!val;
Ian Campbellf942dc22011-03-15 00:06:18 +0000936
Ian Campbellf942dc22011-03-15 00:06:18 +0000937 return 0;
938}
939
Ian Campbellf942dc22011-03-15 00:06:18 +0000940static const struct xenbus_device_id netback_ids[] = {
941 { "vif" },
942 { "" }
943};
944
David Vrabel95afae42014-09-08 17:30:41 +0100945static struct xenbus_driver netback_driver = {
946 .ids = netback_ids,
Ian Campbellf942dc22011-03-15 00:06:18 +0000947 .probe = netback_probe,
948 .remove = netback_remove,
949 .uevent = netback_uevent,
950 .otherend_changed = frontend_changed,
David Vrabel95afae42014-09-08 17:30:41 +0100951};
Ian Campbellf942dc22011-03-15 00:06:18 +0000952
953int xenvif_xenbus_init(void)
954{
Jan Beulich73db1442011-12-22 09:08:13 +0000955 return xenbus_register_backend(&netback_driver);
Ian Campbellf942dc22011-03-15 00:06:18 +0000956}
Wei Liub103f352013-05-16 23:26:11 +0000957
958void xenvif_xenbus_fini(void)
959{
960 return xenbus_unregister_driver(&netback_driver);
961}