blob: fab0d4b42f58fca511dc447b62ea91607732ba81 [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);
Alexey Khoroshilov2dd34332014-11-24 13:58:00 +030042static int backend_create_xenvif(struct backend_info *be);
Ian Campbellf942dc22011-03-15 00:06:18 +000043static 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;
David Vrabelf48da8b2014-10-22 14:08:54 +010055 struct netdev_queue *dev_queue;
Zoltan Kissf51de242014-07-08 19:49:14 +010056
57 if (tx_ring->sring) {
58 struct xen_netif_tx_sring *sring = tx_ring->sring;
59
60 seq_printf(m, "Queue %d\nTX: nr_ents %u\n", queue->id,
61 tx_ring->nr_ents);
62 seq_printf(m, "req prod %u (%d) cons %u (%d) event %u (%d)\n",
63 sring->req_prod,
64 sring->req_prod - sring->rsp_prod,
65 tx_ring->req_cons,
66 tx_ring->req_cons - sring->rsp_prod,
67 sring->req_event,
68 sring->req_event - sring->rsp_prod);
69 seq_printf(m, "rsp prod %u (base) pvt %u (%d) event %u (%d)\n",
70 sring->rsp_prod,
71 tx_ring->rsp_prod_pvt,
72 tx_ring->rsp_prod_pvt - sring->rsp_prod,
73 sring->rsp_event,
74 sring->rsp_event - sring->rsp_prod);
75 seq_printf(m, "pending prod %u pending cons %u nr_pending_reqs %u\n",
76 queue->pending_prod,
77 queue->pending_cons,
78 nr_pending_reqs(queue));
79 seq_printf(m, "dealloc prod %u dealloc cons %u dealloc_queue %u\n\n",
80 queue->dealloc_prod,
81 queue->dealloc_cons,
82 queue->dealloc_prod - queue->dealloc_cons);
83 }
84
85 if (rx_ring->sring) {
86 struct xen_netif_rx_sring *sring = rx_ring->sring;
87
88 seq_printf(m, "RX: nr_ents %u\n", rx_ring->nr_ents);
89 seq_printf(m, "req prod %u (%d) cons %u (%d) event %u (%d)\n",
90 sring->req_prod,
91 sring->req_prod - sring->rsp_prod,
92 rx_ring->req_cons,
93 rx_ring->req_cons - sring->rsp_prod,
94 sring->req_event,
95 sring->req_event - sring->rsp_prod);
96 seq_printf(m, "rsp prod %u (base) pvt %u (%d) event %u (%d)\n\n",
97 sring->rsp_prod,
98 rx_ring->rsp_prod_pvt,
99 rx_ring->rsp_prod_pvt - sring->rsp_prod,
100 sring->rsp_event,
101 sring->rsp_event - sring->rsp_prod);
102 }
103
104 seq_printf(m, "NAPI state: %lx NAPI weight: %d TX queue len %u\n"
105 "Credit timer_pending: %d, credit: %lu, usec: %lu\n"
106 "remaining: %lu, expires: %lu, now: %lu\n",
107 queue->napi.state, queue->napi.weight,
108 skb_queue_len(&queue->tx_queue),
109 timer_pending(&queue->credit_timeout),
110 queue->credit_bytes,
111 queue->credit_usec,
112 queue->remaining_credit,
113 queue->credit_timeout.expires,
114 jiffies);
115
David Vrabelf48da8b2014-10-22 14:08:54 +0100116 dev_queue = netdev_get_tx_queue(queue->vif->dev, queue->id);
117
118 seq_printf(m, "\nRx internal queue: len %u max %u pkts %u %s\n",
119 queue->rx_queue_len, queue->rx_queue_max,
120 skb_queue_len(&queue->rx_queue),
121 netif_tx_queue_stopped(dev_queue) ? "stopped" : "running");
122
Zoltan Kissf51de242014-07-08 19:49:14 +0100123 return 0;
124}
125
126#define XENVIF_KICK_STR "kick"
Wei Liu5c807002014-08-12 11:59:29 +0100127#define BUFFER_SIZE 32
Zoltan Kissf51de242014-07-08 19:49:14 +0100128
129static ssize_t
130xenvif_write_io_ring(struct file *filp, const char __user *buf, size_t count,
131 loff_t *ppos)
132{
133 struct xenvif_queue *queue =
134 ((struct seq_file *)filp->private_data)->private;
135 int len;
Wei Liu5c807002014-08-12 11:59:29 +0100136 char write[BUFFER_SIZE];
Zoltan Kissf51de242014-07-08 19:49:14 +0100137
138 /* don't allow partial writes and check the length */
139 if (*ppos != 0)
140 return 0;
Wei Liu5c807002014-08-12 11:59:29 +0100141 if (count >= sizeof(write))
Zoltan Kissf51de242014-07-08 19:49:14 +0100142 return -ENOSPC;
143
144 len = simple_write_to_buffer(write,
Wei Liu5c807002014-08-12 11:59:29 +0100145 sizeof(write) - 1,
Zoltan Kissf51de242014-07-08 19:49:14 +0100146 ppos,
147 buf,
148 count);
149 if (len < 0)
150 return len;
151
Wei Liu5c807002014-08-12 11:59:29 +0100152 write[len] = '\0';
153
Zoltan Kissf51de242014-07-08 19:49:14 +0100154 if (!strncmp(write, XENVIF_KICK_STR, sizeof(XENVIF_KICK_STR) - 1))
155 xenvif_interrupt(0, (void *)queue);
156 else {
157 pr_warn("Unknown command to io_ring_q%d. Available: kick\n",
158 queue->id);
159 count = -EINVAL;
160 }
161 return count;
162}
163
164static int xenvif_dump_open(struct inode *inode, struct file *filp)
165{
166 int ret;
167 void *queue = NULL;
168
169 if (inode->i_private)
170 queue = inode->i_private;
171 ret = single_open(filp, xenvif_read_io_ring, queue);
172 filp->f_mode |= FMODE_PWRITE;
173 return ret;
174}
175
176static const struct file_operations xenvif_dbg_io_ring_ops_fops = {
177 .owner = THIS_MODULE,
178 .open = xenvif_dump_open,
179 .read = seq_read,
180 .llseek = seq_lseek,
181 .release = single_release,
182 .write = xenvif_write_io_ring,
183};
184
Wei Liu628fa762014-08-12 11:59:30 +0100185static void xenvif_debugfs_addif(struct xenvif *vif)
Zoltan Kissf51de242014-07-08 19:49:14 +0100186{
187 struct dentry *pfile;
Zoltan Kissf51de242014-07-08 19:49:14 +0100188 int i;
189
190 if (IS_ERR_OR_NULL(xen_netback_dbg_root))
191 return;
192
193 vif->xenvif_dbg_root = debugfs_create_dir(vif->dev->name,
194 xen_netback_dbg_root);
195 if (!IS_ERR_OR_NULL(vif->xenvif_dbg_root)) {
196 for (i = 0; i < vif->num_queues; ++i) {
197 char filename[sizeof("io_ring_q") + 4];
198
199 snprintf(filename, sizeof(filename), "io_ring_q%d", i);
200 pfile = debugfs_create_file(filename,
201 S_IRUSR | S_IWUSR,
202 vif->xenvif_dbg_root,
203 &vif->queues[i],
204 &xenvif_dbg_io_ring_ops_fops);
205 if (IS_ERR_OR_NULL(pfile))
206 pr_warn("Creation of io_ring file returned %ld!\n",
207 PTR_ERR(pfile));
208 }
209 } else
210 netdev_warn(vif->dev,
211 "Creation of vif debugfs dir returned %ld!\n",
212 PTR_ERR(vif->xenvif_dbg_root));
213}
214
215static void xenvif_debugfs_delif(struct xenvif *vif)
216{
217 if (IS_ERR_OR_NULL(xen_netback_dbg_root))
218 return;
219
220 if (!IS_ERR_OR_NULL(vif->xenvif_dbg_root))
221 debugfs_remove_recursive(vif->xenvif_dbg_root);
222 vif->xenvif_dbg_root = NULL;
223}
224#endif /* CONFIG_DEBUG_FS */
225
Ian Campbellf942dc22011-03-15 00:06:18 +0000226static int netback_remove(struct xenbus_device *dev)
227{
228 struct backend_info *be = dev_get_drvdata(&dev->dev);
229
David Vrabeldc62cca2013-10-07 13:55:19 +0100230 set_backend_state(be, XenbusStateClosed);
231
Ian Campbellf942dc22011-03-15 00:06:18 +0000232 unregister_hotplug_status_watch(be);
233 if (be->vif) {
234 kobject_uevent(&dev->dev.kobj, KOBJ_OFFLINE);
235 xenbus_rm(XBT_NIL, dev->nodename, "hotplug-status");
Paul Durrant279f4382013-09-17 17:46:08 +0100236 xenvif_free(be->vif);
Ian Campbellf942dc22011-03-15 00:06:18 +0000237 be->vif = NULL;
238 }
239 kfree(be);
240 dev_set_drvdata(&dev->dev, NULL);
241 return 0;
242}
243
244
245/**
246 * Entry point to this code when a new device is created. Allocate the basic
247 * structures and switch to InitWait.
248 */
249static int netback_probe(struct xenbus_device *dev,
250 const struct xenbus_device_id *id)
251{
252 const char *message;
253 struct xenbus_transaction xbt;
254 int err;
255 int sg;
256 struct backend_info *be = kzalloc(sizeof(struct backend_info),
257 GFP_KERNEL);
258 if (!be) {
259 xenbus_dev_fatal(dev, -ENOMEM,
260 "allocating backend structure");
261 return -ENOMEM;
262 }
263
264 be->dev = dev;
265 dev_set_drvdata(&dev->dev, be);
266
267 sg = 1;
268
269 do {
270 err = xenbus_transaction_start(&xbt);
271 if (err) {
272 xenbus_dev_fatal(dev, err, "starting transaction");
273 goto fail;
274 }
275
276 err = xenbus_printf(xbt, dev->nodename, "feature-sg", "%d", sg);
277 if (err) {
278 message = "writing feature-sg";
279 goto abort_transaction;
280 }
281
282 err = xenbus_printf(xbt, dev->nodename, "feature-gso-tcpv4",
283 "%d", sg);
284 if (err) {
285 message = "writing feature-gso-tcpv4";
286 goto abort_transaction;
287 }
288
Paul Durranta9468582013-10-16 17:50:31 +0100289 err = xenbus_printf(xbt, dev->nodename, "feature-gso-tcpv6",
290 "%d", sg);
291 if (err) {
292 message = "writing feature-gso-tcpv6";
293 goto abort_transaction;
294 }
295
Paul Durrant2eba61d2013-10-16 17:50:29 +0100296 /* We support partial checksum setup for IPv6 packets */
297 err = xenbus_printf(xbt, dev->nodename,
298 "feature-ipv6-csum-offload",
299 "%d", 1);
300 if (err) {
301 message = "writing feature-ipv6-csum-offload";
302 goto abort_transaction;
303 }
304
Ian Campbellf942dc22011-03-15 00:06:18 +0000305 /* We support rx-copy path. */
306 err = xenbus_printf(xbt, dev->nodename,
307 "feature-rx-copy", "%d", 1);
308 if (err) {
309 message = "writing feature-rx-copy";
310 goto abort_transaction;
311 }
312
313 /*
314 * We don't support rx-flip path (except old guests who don't
315 * grok this feature flag).
316 */
317 err = xenbus_printf(xbt, dev->nodename,
318 "feature-rx-flip", "%d", 0);
319 if (err) {
320 message = "writing feature-rx-flip";
321 goto abort_transaction;
322 }
323
324 err = xenbus_transaction_end(xbt, 0);
325 } while (err == -EAGAIN);
326
327 if (err) {
328 xenbus_dev_fatal(dev, err, "completing transaction");
329 goto fail;
330 }
331
Wei Liue1f00a692013-05-22 06:34:45 +0000332 /*
333 * Split event channels support, this is optional so it is not
334 * put inside the above loop.
335 */
336 err = xenbus_printf(XBT_NIL, dev->nodename,
337 "feature-split-event-channels",
338 "%u", separate_tx_rx_irq);
339 if (err)
Wei Liu8ef2c3b2013-07-02 00:08:54 +0100340 pr_debug("Error writing feature-split-event-channels\n");
Wei Liue1f00a692013-05-22 06:34:45 +0000341
Andrew J. Bennieston8d3d53b2014-06-04 10:30:43 +0100342 /* Multi-queue support: This is an optional feature. */
343 err = xenbus_printf(XBT_NIL, dev->nodename,
344 "multi-queue-max-queues", "%u", xenvif_max_queues);
345 if (err)
346 pr_debug("Error writing multi-queue-max-queues\n");
347
Ian Campbellf942dc22011-03-15 00:06:18 +0000348 err = xenbus_switch_state(dev, XenbusStateInitWait);
349 if (err)
350 goto fail;
351
Paul Durrantea732df2013-09-26 12:09:52 +0100352 be->state = XenbusStateInitWait;
353
Ian Campbellf942dc22011-03-15 00:06:18 +0000354 /* This kicks hotplug scripts, so do it immediately. */
Alexey Khoroshilov2dd34332014-11-24 13:58:00 +0300355 err = backend_create_xenvif(be);
356 if (err)
357 goto fail;
Ian Campbellf942dc22011-03-15 00:06:18 +0000358
359 return 0;
360
361abort_transaction:
362 xenbus_transaction_end(xbt, 1);
363 xenbus_dev_fatal(dev, err, "%s", message);
364fail:
Wei Liu8ef2c3b2013-07-02 00:08:54 +0100365 pr_debug("failed\n");
Ian Campbellf942dc22011-03-15 00:06:18 +0000366 netback_remove(dev);
367 return err;
368}
369
370
371/*
372 * Handle the creation of the hotplug script environment. We add the script
373 * and vif variables to the environment, for the benefit of the vif-* hotplug
374 * scripts.
375 */
376static int netback_uevent(struct xenbus_device *xdev,
377 struct kobj_uevent_env *env)
378{
379 struct backend_info *be = dev_get_drvdata(&xdev->dev);
380 char *val;
381
382 val = xenbus_read(XBT_NIL, xdev->nodename, "script", NULL);
383 if (IS_ERR(val)) {
384 int err = PTR_ERR(val);
385 xenbus_dev_fatal(xdev, err, "reading script");
386 return err;
387 } else {
388 if (add_uevent_var(env, "script=%s", val)) {
389 kfree(val);
390 return -ENOMEM;
391 }
392 kfree(val);
393 }
394
395 if (!be || !be->vif)
396 return 0;
397
398 return add_uevent_var(env, "vif=%s", be->vif->dev->name);
399}
400
401
Alexey Khoroshilov2dd34332014-11-24 13:58:00 +0300402static int backend_create_xenvif(struct backend_info *be)
Ian Campbellf942dc22011-03-15 00:06:18 +0000403{
404 int err;
405 long handle;
406 struct xenbus_device *dev = be->dev;
407
408 if (be->vif != NULL)
Alexey Khoroshilov2dd34332014-11-24 13:58:00 +0300409 return 0;
Ian Campbellf942dc22011-03-15 00:06:18 +0000410
411 err = xenbus_scanf(XBT_NIL, dev->nodename, "handle", "%li", &handle);
412 if (err != 1) {
413 xenbus_dev_fatal(dev, err, "reading handle");
Alexey Khoroshilov2dd34332014-11-24 13:58:00 +0300414 return (err < 0) ? err : -EINVAL;
Ian Campbellf942dc22011-03-15 00:06:18 +0000415 }
416
417 be->vif = xenvif_alloc(&dev->dev, dev->otherend_id, handle);
418 if (IS_ERR(be->vif)) {
419 err = PTR_ERR(be->vif);
420 be->vif = NULL;
421 xenbus_dev_fatal(dev, err, "creating interface");
Alexey Khoroshilov2dd34332014-11-24 13:58:00 +0300422 return err;
Ian Campbellf942dc22011-03-15 00:06:18 +0000423 }
424
425 kobject_uevent(&dev->dev.kobj, KOBJ_ONLINE);
Alexey Khoroshilov2dd34332014-11-24 13:58:00 +0300426 return 0;
Ian Campbellf942dc22011-03-15 00:06:18 +0000427}
428
Paul Durrantea732df2013-09-26 12:09:52 +0100429static void backend_disconnect(struct backend_info *be)
Ian Campbellf942dc22011-03-15 00:06:18 +0000430{
Zoltan Kissf51de242014-07-08 19:49:14 +0100431 if (be->vif) {
432#ifdef CONFIG_DEBUG_FS
433 xenvif_debugfs_delif(be->vif);
434#endif /* CONFIG_DEBUG_FS */
Ian Campbellf942dc22011-03-15 00:06:18 +0000435 xenvif_disconnect(be->vif);
Zoltan Kissf51de242014-07-08 19:49:14 +0100436 }
Paul Durrant279f4382013-09-17 17:46:08 +0100437}
438
Paul Durrantea732df2013-09-26 12:09:52 +0100439static void backend_connect(struct backend_info *be)
Paul Durrant279f4382013-09-17 17:46:08 +0100440{
Paul Durrantea732df2013-09-26 12:09:52 +0100441 if (be->vif)
442 connect(be);
443}
Paul Durrant279f4382013-09-17 17:46:08 +0100444
Paul Durrantea732df2013-09-26 12:09:52 +0100445static inline void backend_switch_state(struct backend_info *be,
446 enum xenbus_state state)
447{
448 struct xenbus_device *dev = be->dev;
449
450 pr_debug("%s -> %s\n", dev->nodename, xenbus_strstate(state));
451 be->state = state;
452
453 /* If we are waiting for a hotplug script then defer the
454 * actual xenbus state change.
455 */
456 if (!be->have_hotplug_status_watch)
457 xenbus_switch_state(dev, state);
458}
459
460/* Handle backend state transitions:
461 *
462 * The backend state starts in InitWait and the following transitions are
463 * allowed.
464 *
465 * InitWait -> Connected
466 *
467 * ^ \ |
468 * | \ |
469 * | \ |
470 * | \ |
471 * | \ |
472 * | \ |
473 * | V V
474 *
475 * Closed <-> Closing
476 *
477 * The state argument specifies the eventual state of the backend and the
478 * function transitions to that state via the shortest path.
479 */
480static void set_backend_state(struct backend_info *be,
481 enum xenbus_state state)
482{
483 while (be->state != state) {
484 switch (be->state) {
485 case XenbusStateClosed:
486 switch (state) {
487 case XenbusStateInitWait:
488 case XenbusStateConnected:
489 pr_info("%s: prepare for reconnect\n",
490 be->dev->nodename);
491 backend_switch_state(be, XenbusStateInitWait);
492 break;
493 case XenbusStateClosing:
494 backend_switch_state(be, XenbusStateClosing);
495 break;
496 default:
497 BUG();
498 }
499 break;
500 case XenbusStateInitWait:
501 switch (state) {
502 case XenbusStateConnected:
503 backend_connect(be);
504 backend_switch_state(be, XenbusStateConnected);
505 break;
506 case XenbusStateClosing:
507 case XenbusStateClosed:
508 backend_switch_state(be, XenbusStateClosing);
509 break;
510 default:
511 BUG();
512 }
513 break;
514 case XenbusStateConnected:
515 switch (state) {
516 case XenbusStateInitWait:
517 case XenbusStateClosing:
518 case XenbusStateClosed:
519 backend_disconnect(be);
520 backend_switch_state(be, XenbusStateClosing);
521 break;
522 default:
523 BUG();
524 }
525 break;
526 case XenbusStateClosing:
527 switch (state) {
528 case XenbusStateInitWait:
529 case XenbusStateConnected:
530 case XenbusStateClosed:
531 backend_switch_state(be, XenbusStateClosed);
532 break;
533 default:
534 BUG();
535 }
536 break;
537 default:
538 BUG();
539 }
Ian Campbellf942dc22011-03-15 00:06:18 +0000540 }
541}
542
543/**
544 * Callback received when the frontend's state changes.
545 */
546static void frontend_changed(struct xenbus_device *dev,
547 enum xenbus_state frontend_state)
548{
549 struct backend_info *be = dev_get_drvdata(&dev->dev);
550
Paul Durrantea732df2013-09-26 12:09:52 +0100551 pr_debug("%s -> %s\n", dev->otherend, xenbus_strstate(frontend_state));
Ian Campbellf942dc22011-03-15 00:06:18 +0000552
553 be->frontend_state = frontend_state;
554
555 switch (frontend_state) {
556 case XenbusStateInitialising:
Paul Durrantea732df2013-09-26 12:09:52 +0100557 set_backend_state(be, XenbusStateInitWait);
Ian Campbellf942dc22011-03-15 00:06:18 +0000558 break;
559
560 case XenbusStateInitialised:
561 break;
562
563 case XenbusStateConnected:
Paul Durrantea732df2013-09-26 12:09:52 +0100564 set_backend_state(be, XenbusStateConnected);
Ian Campbellf942dc22011-03-15 00:06:18 +0000565 break;
566
567 case XenbusStateClosing:
Paul Durrantea732df2013-09-26 12:09:52 +0100568 set_backend_state(be, XenbusStateClosing);
Ian Campbellf942dc22011-03-15 00:06:18 +0000569 break;
570
571 case XenbusStateClosed:
Paul Durrantea732df2013-09-26 12:09:52 +0100572 set_backend_state(be, XenbusStateClosed);
Ian Campbellf942dc22011-03-15 00:06:18 +0000573 if (xenbus_dev_is_online(dev))
574 break;
575 /* fall through if not online */
576 case XenbusStateUnknown:
Paul Durrantea732df2013-09-26 12:09:52 +0100577 set_backend_state(be, XenbusStateClosed);
Ian Campbellf942dc22011-03-15 00:06:18 +0000578 device_unregister(&dev->dev);
579 break;
580
581 default:
582 xenbus_dev_fatal(dev, -EINVAL, "saw state %d at frontend",
583 frontend_state);
584 break;
585 }
586}
587
588
589static void xen_net_read_rate(struct xenbus_device *dev,
590 unsigned long *bytes, unsigned long *usec)
591{
592 char *s, *e;
593 unsigned long b, u;
594 char *ratestr;
595
596 /* Default to unlimited bandwidth. */
597 *bytes = ~0UL;
598 *usec = 0;
599
600 ratestr = xenbus_read(XBT_NIL, dev->nodename, "rate", NULL);
601 if (IS_ERR(ratestr))
602 return;
603
604 s = ratestr;
605 b = simple_strtoul(s, &e, 10);
606 if ((s == e) || (*e != ','))
607 goto fail;
608
609 s = e + 1;
610 u = simple_strtoul(s, &e, 10);
611 if ((s == e) || (*e != '\0'))
612 goto fail;
613
614 *bytes = b;
615 *usec = u;
616
617 kfree(ratestr);
618 return;
619
620 fail:
621 pr_warn("Failed to parse network rate limit. Traffic unlimited.\n");
622 kfree(ratestr);
623}
624
625static int xen_net_read_mac(struct xenbus_device *dev, u8 mac[])
626{
627 char *s, *e, *macstr;
628 int i;
629
630 macstr = s = xenbus_read(XBT_NIL, dev->nodename, "mac", NULL);
631 if (IS_ERR(macstr))
632 return PTR_ERR(macstr);
633
634 for (i = 0; i < ETH_ALEN; i++) {
635 mac[i] = simple_strtoul(s, &e, 16);
636 if ((s == e) || (*e != ((i == ETH_ALEN-1) ? '\0' : ':'))) {
637 kfree(macstr);
638 return -ENOENT;
639 }
640 s = e+1;
641 }
642
643 kfree(macstr);
644 return 0;
645}
646
647static void unregister_hotplug_status_watch(struct backend_info *be)
648{
649 if (be->have_hotplug_status_watch) {
650 unregister_xenbus_watch(&be->hotplug_status_watch);
651 kfree(be->hotplug_status_watch.node);
652 }
653 be->have_hotplug_status_watch = 0;
654}
655
656static void hotplug_status_changed(struct xenbus_watch *watch,
657 const char **vec,
658 unsigned int vec_size)
659{
660 struct backend_info *be = container_of(watch,
661 struct backend_info,
662 hotplug_status_watch);
663 char *str;
664 unsigned int len;
665
666 str = xenbus_read(XBT_NIL, be->dev->nodename, "hotplug-status", &len);
667 if (IS_ERR(str))
668 return;
669 if (len == sizeof("connected")-1 && !memcmp(str, "connected", len)) {
Paul Durrantea732df2013-09-26 12:09:52 +0100670 /* Complete any pending state change */
671 xenbus_switch_state(be->dev, be->state);
672
Ian Campbellf942dc22011-03-15 00:06:18 +0000673 /* Not interested in this watch anymore. */
674 unregister_hotplug_status_watch(be);
675 }
676 kfree(str);
677}
678
679static void connect(struct backend_info *be)
680{
681 int err;
682 struct xenbus_device *dev = be->dev;
Wei Liue9ce7cb2014-06-04 10:30:42 +0100683 unsigned long credit_bytes, credit_usec;
684 unsigned int queue_index;
Andrew J. Bennieston8d3d53b2014-06-04 10:30:43 +0100685 unsigned int requested_num_queues;
Wei Liue9ce7cb2014-06-04 10:30:42 +0100686 struct xenvif_queue *queue;
Ian Campbellf942dc22011-03-15 00:06:18 +0000687
Andrew J. Bennieston8d3d53b2014-06-04 10:30:43 +0100688 /* Check whether the frontend requested multiple queues
689 * and read the number requested.
690 */
691 err = xenbus_scanf(XBT_NIL, dev->otherend,
692 "multi-queue-num-queues",
693 "%u", &requested_num_queues);
694 if (err < 0) {
695 requested_num_queues = 1; /* Fall back to single queue */
696 } else if (requested_num_queues > xenvif_max_queues) {
697 /* buggy or malicious guest */
698 xenbus_dev_fatal(dev, err,
699 "guest requested %u queues, exceeding the maximum of %u.",
700 requested_num_queues, xenvif_max_queues);
701 return;
702 }
703
Ian Campbellf942dc22011-03-15 00:06:18 +0000704 err = xen_net_read_mac(dev, be->vif->fe_dev_addr);
705 if (err) {
706 xenbus_dev_fatal(dev, err, "parsing %s/mac", dev->nodename);
707 return;
708 }
709
Wei Liue9ce7cb2014-06-04 10:30:42 +0100710 xen_net_read_rate(dev, &credit_bytes, &credit_usec);
711 read_xenbus_vif_flags(be);
712
Andrew J. Bennieston8d3d53b2014-06-04 10:30:43 +0100713 /* Use the number of queues requested by the frontend */
Wei Liue9ce7cb2014-06-04 10:30:42 +0100714 be->vif->queues = vzalloc(requested_num_queues *
715 sizeof(struct xenvif_queue));
Wei Liuf7b50c42014-06-23 10:50:17 +0100716 be->vif->num_queues = requested_num_queues;
David Vrabelecf08d22014-10-22 14:08:55 +0100717 be->vif->stalled_queues = requested_num_queues;
Wei Liue9ce7cb2014-06-04 10:30:42 +0100718
719 for (queue_index = 0; queue_index < requested_num_queues; ++queue_index) {
720 queue = &be->vif->queues[queue_index];
721 queue->vif = be->vif;
722 queue->id = queue_index;
723 snprintf(queue->name, sizeof(queue->name), "%s-q%u",
724 be->vif->dev->name, queue->id);
725
726 err = xenvif_init_queue(queue);
Andrew J. Bennieston8d3d53b2014-06-04 10:30:43 +0100727 if (err) {
728 /* xenvif_init_queue() cleans up after itself on
729 * failure, but we need to clean up any previously
730 * initialised queues. Set num_queues to i so that
731 * earlier queues can be destroyed using the regular
732 * disconnect logic.
733 */
Wei Liuf7b50c42014-06-23 10:50:17 +0100734 be->vif->num_queues = queue_index;
Wei Liue9ce7cb2014-06-04 10:30:42 +0100735 goto err;
Andrew J. Bennieston8d3d53b2014-06-04 10:30:43 +0100736 }
Wei Liue9ce7cb2014-06-04 10:30:42 +0100737
738 queue->remaining_credit = credit_bytes;
739
740 err = connect_rings(be, queue);
Andrew J. Bennieston8d3d53b2014-06-04 10:30:43 +0100741 if (err) {
742 /* connect_rings() cleans up after itself on failure,
743 * but we need to clean up after xenvif_init_queue() here,
744 * and also clean up any previously initialised queues.
745 */
746 xenvif_deinit_queue(queue);
Wei Liuf7b50c42014-06-23 10:50:17 +0100747 be->vif->num_queues = queue_index;
Wei Liue9ce7cb2014-06-04 10:30:42 +0100748 goto err;
Andrew J. Bennieston8d3d53b2014-06-04 10:30:43 +0100749 }
Wei Liue9ce7cb2014-06-04 10:30:42 +0100750 }
751
Wei Liu628fa762014-08-12 11:59:30 +0100752#ifdef CONFIG_DEBUG_FS
753 xenvif_debugfs_addif(be->vif);
754#endif /* CONFIG_DEBUG_FS */
755
Wei Liuf7b50c42014-06-23 10:50:17 +0100756 /* Initialisation completed, tell core driver the number of
757 * active queues.
758 */
759 rtnl_lock();
760 netif_set_real_num_tx_queues(be->vif->dev, requested_num_queues);
761 netif_set_real_num_rx_queues(be->vif->dev, requested_num_queues);
762 rtnl_unlock();
763
Wei Liue9ce7cb2014-06-04 10:30:42 +0100764 xenvif_carrier_on(be->vif);
Ian Campbellf942dc22011-03-15 00:06:18 +0000765
766 unregister_hotplug_status_watch(be);
767 err = xenbus_watch_pathfmt(dev, &be->hotplug_status_watch,
768 hotplug_status_changed,
769 "%s/%s", dev->nodename, "hotplug-status");
Paul Durrantea732df2013-09-26 12:09:52 +0100770 if (!err)
Ian Campbellf942dc22011-03-15 00:06:18 +0000771 be->have_hotplug_status_watch = 1;
Ian Campbellf942dc22011-03-15 00:06:18 +0000772
Wei Liue9ce7cb2014-06-04 10:30:42 +0100773 netif_tx_wake_all_queues(be->vif->dev);
774
775 return;
776
777err:
Wei Liuf7b50c42014-06-23 10:50:17 +0100778 if (be->vif->num_queues > 0)
Andrew J. Bennieston8d3d53b2014-06-04 10:30:43 +0100779 xenvif_disconnect(be->vif); /* Clean up existing queues */
Wei Liue9ce7cb2014-06-04 10:30:42 +0100780 vfree(be->vif->queues);
781 be->vif->queues = NULL;
Wei Liuf7b50c42014-06-23 10:50:17 +0100782 be->vif->num_queues = 0;
Wei Liue9ce7cb2014-06-04 10:30:42 +0100783 return;
Ian Campbellf942dc22011-03-15 00:06:18 +0000784}
785
786
Wei Liue9ce7cb2014-06-04 10:30:42 +0100787static int connect_rings(struct backend_info *be, struct xenvif_queue *queue)
Ian Campbellf942dc22011-03-15 00:06:18 +0000788{
Ian Campbellf942dc22011-03-15 00:06:18 +0000789 struct xenbus_device *dev = be->dev;
Wei Liuf7b50c42014-06-23 10:50:17 +0100790 unsigned int num_queues = queue->vif->num_queues;
Ian Campbellf942dc22011-03-15 00:06:18 +0000791 unsigned long tx_ring_ref, rx_ring_ref;
Wei Liue9ce7cb2014-06-04 10:30:42 +0100792 unsigned int tx_evtchn, rx_evtchn;
Ian Campbellf942dc22011-03-15 00:06:18 +0000793 int err;
Andrew J. Bennieston8d3d53b2014-06-04 10:30:43 +0100794 char *xspath;
795 size_t xspathsize;
796 const size_t xenstore_path_ext_size = 11; /* sufficient for "/queue-NNN" */
Ian Campbellf942dc22011-03-15 00:06:18 +0000797
Andrew J. Bennieston8d3d53b2014-06-04 10:30:43 +0100798 /* If the frontend requested 1 queue, or we have fallen back
799 * to single queue due to lack of frontend support for multi-
800 * queue, expect the remaining XenStore keys in the toplevel
801 * directory. Otherwise, expect them in a subdirectory called
802 * queue-N.
803 */
804 if (num_queues == 1) {
805 xspath = kzalloc(strlen(dev->otherend) + 1, GFP_KERNEL);
806 if (!xspath) {
807 xenbus_dev_fatal(dev, -ENOMEM,
808 "reading ring references");
809 return -ENOMEM;
810 }
811 strcpy(xspath, dev->otherend);
812 } else {
813 xspathsize = strlen(dev->otherend) + xenstore_path_ext_size;
814 xspath = kzalloc(xspathsize, GFP_KERNEL);
815 if (!xspath) {
816 xenbus_dev_fatal(dev, -ENOMEM,
817 "reading ring references");
818 return -ENOMEM;
819 }
820 snprintf(xspath, xspathsize, "%s/queue-%u", dev->otherend,
821 queue->id);
822 }
823
824 err = xenbus_gather(XBT_NIL, xspath,
Ian Campbellf942dc22011-03-15 00:06:18 +0000825 "tx-ring-ref", "%lu", &tx_ring_ref,
Wei Liue1f00a692013-05-22 06:34:45 +0000826 "rx-ring-ref", "%lu", &rx_ring_ref, NULL);
Ian Campbellf942dc22011-03-15 00:06:18 +0000827 if (err) {
828 xenbus_dev_fatal(dev, err,
Wei Liue1f00a692013-05-22 06:34:45 +0000829 "reading %s/ring-ref",
Andrew J. Bennieston8d3d53b2014-06-04 10:30:43 +0100830 xspath);
831 goto err;
Ian Campbellf942dc22011-03-15 00:06:18 +0000832 }
833
Wei Liue1f00a692013-05-22 06:34:45 +0000834 /* Try split event channels first, then single event channel. */
Andrew J. Bennieston8d3d53b2014-06-04 10:30:43 +0100835 err = xenbus_gather(XBT_NIL, xspath,
Wei Liue1f00a692013-05-22 06:34:45 +0000836 "event-channel-tx", "%u", &tx_evtchn,
837 "event-channel-rx", "%u", &rx_evtchn, NULL);
838 if (err < 0) {
Andrew J. Bennieston8d3d53b2014-06-04 10:30:43 +0100839 err = xenbus_scanf(XBT_NIL, xspath,
Wei Liue1f00a692013-05-22 06:34:45 +0000840 "event-channel", "%u", &tx_evtchn);
841 if (err < 0) {
842 xenbus_dev_fatal(dev, err,
843 "reading %s/event-channel(-tx/rx)",
Andrew J. Bennieston8d3d53b2014-06-04 10:30:43 +0100844 xspath);
845 goto err;
Wei Liue1f00a692013-05-22 06:34:45 +0000846 }
847 rx_evtchn = tx_evtchn;
848 }
849
Wei Liue9ce7cb2014-06-04 10:30:42 +0100850 /* Map the shared frame, irq etc. */
851 err = xenvif_connect(queue, tx_ring_ref, rx_ring_ref,
852 tx_evtchn, rx_evtchn);
853 if (err) {
854 xenbus_dev_fatal(dev, err,
855 "mapping shared-frames %lu/%lu port tx %u rx %u",
856 tx_ring_ref, rx_ring_ref,
857 tx_evtchn, rx_evtchn);
Andrew J. Bennieston8d3d53b2014-06-04 10:30:43 +0100858 goto err;
Wei Liue9ce7cb2014-06-04 10:30:42 +0100859 }
860
Andrew J. Bennieston8d3d53b2014-06-04 10:30:43 +0100861 err = 0;
862err: /* Regular return falls through with err == 0 */
863 kfree(xspath);
864 return err;
Wei Liue9ce7cb2014-06-04 10:30:42 +0100865}
866
867static int read_xenbus_vif_flags(struct backend_info *be)
868{
869 struct xenvif *vif = be->vif;
870 struct xenbus_device *dev = be->dev;
871 unsigned int rx_copy;
872 int err, val;
873
Ian Campbellf942dc22011-03-15 00:06:18 +0000874 err = xenbus_scanf(XBT_NIL, dev->otherend, "request-rx-copy", "%u",
875 &rx_copy);
876 if (err == -ENOENT) {
877 err = 0;
878 rx_copy = 0;
879 }
880 if (err < 0) {
881 xenbus_dev_fatal(dev, err, "reading %s/request-rx-copy",
882 dev->otherend);
883 return err;
884 }
885 if (!rx_copy)
886 return -EOPNOTSUPP;
887
David Vrabelbc96f642014-10-22 14:08:53 +0100888 if (xenbus_scanf(XBT_NIL, dev->otherend,
889 "feature-rx-notify", "%d", &val) < 0 || val == 0) {
890 xenbus_dev_fatal(dev, -EINVAL, "feature-rx-notify is mandatory");
891 return -EINVAL;
Ian Campbellf942dc22011-03-15 00:06:18 +0000892 }
893
894 if (xenbus_scanf(XBT_NIL, dev->otherend, "feature-sg",
895 "%d", &val) < 0)
896 val = 0;
897 vif->can_sg = !!val;
898
Paul Durrant82cada22013-10-16 17:50:32 +0100899 vif->gso_mask = 0;
900 vif->gso_prefix_mask = 0;
901
Ian Campbellf942dc22011-03-15 00:06:18 +0000902 if (xenbus_scanf(XBT_NIL, dev->otherend, "feature-gso-tcpv4",
903 "%d", &val) < 0)
904 val = 0;
Paul Durrant82cada22013-10-16 17:50:32 +0100905 if (val)
906 vif->gso_mask |= GSO_BIT(TCPV4);
Ian Campbellf942dc22011-03-15 00:06:18 +0000907
908 if (xenbus_scanf(XBT_NIL, dev->otherend, "feature-gso-tcpv4-prefix",
909 "%d", &val) < 0)
910 val = 0;
Paul Durrant82cada22013-10-16 17:50:32 +0100911 if (val)
912 vif->gso_prefix_mask |= GSO_BIT(TCPV4);
913
914 if (xenbus_scanf(XBT_NIL, dev->otherend, "feature-gso-tcpv6",
915 "%d", &val) < 0)
916 val = 0;
917 if (val)
918 vif->gso_mask |= GSO_BIT(TCPV6);
919
920 if (xenbus_scanf(XBT_NIL, dev->otherend, "feature-gso-tcpv6-prefix",
921 "%d", &val) < 0)
922 val = 0;
923 if (val)
924 vif->gso_prefix_mask |= GSO_BIT(TCPV6);
925
926 if (vif->gso_mask & vif->gso_prefix_mask) {
927 xenbus_dev_fatal(dev, err,
928 "%s: gso and gso prefix flags are not "
929 "mutually exclusive",
930 dev->otherend);
931 return -EOPNOTSUPP;
932 }
Ian Campbellf942dc22011-03-15 00:06:18 +0000933
934 if (xenbus_scanf(XBT_NIL, dev->otherend, "feature-no-csum-offload",
935 "%d", &val) < 0)
936 val = 0;
Paul Durrant146c8a72013-10-16 17:50:28 +0100937 vif->ip_csum = !val;
938
939 if (xenbus_scanf(XBT_NIL, dev->otherend, "feature-ipv6-csum-offload",
940 "%d", &val) < 0)
941 val = 0;
942 vif->ipv6_csum = !!val;
Ian Campbellf942dc22011-03-15 00:06:18 +0000943
Ian Campbellf942dc22011-03-15 00:06:18 +0000944 return 0;
945}
946
Ian Campbellf942dc22011-03-15 00:06:18 +0000947static const struct xenbus_device_id netback_ids[] = {
948 { "vif" },
949 { "" }
950};
951
David Vrabel95afae42014-09-08 17:30:41 +0100952static struct xenbus_driver netback_driver = {
953 .ids = netback_ids,
Ian Campbellf942dc22011-03-15 00:06:18 +0000954 .probe = netback_probe,
955 .remove = netback_remove,
956 .uevent = netback_uevent,
957 .otherend_changed = frontend_changed,
David Vrabel95afae42014-09-08 17:30:41 +0100958};
Ian Campbellf942dc22011-03-15 00:06:18 +0000959
960int xenvif_xenbus_init(void)
961{
Jan Beulich73db1442011-12-22 09:08:13 +0000962 return xenbus_register_backend(&netback_driver);
Ian Campbellf942dc22011-03-15 00:06:18 +0000963}
Wei Liub103f352013-05-16 23:26:11 +0000964
965void xenvif_xenbus_fini(void)
966{
967 return xenbus_unregister_driver(&netback_driver);
968}