xenbus.c 29.5 KB
Newer Older
I
Ian Campbell 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * Xenbus code for netif backend
 *
 * Copyright (C) 2005 Rusty Russell <rusty@rustcorp.com.au>
 * Copyright (C) 2005 XenSource Ltd
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * 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/>.
I
Ian Campbell 已提交
19 20 21
*/

#include "common.h"
22 23
#include <linux/vmalloc.h>
#include <linux/rtnetlink.h>
I
Ian Campbell 已提交
24

25 26
static int connect_data_rings(struct backend_info *be,
			      struct xenvif_queue *queue);
27 28
static void connect(struct backend_info *be);
static int read_xenbus_vif_flags(struct backend_info *be);
29
static int backend_create_xenvif(struct backend_info *be);
I
Ian Campbell 已提交
30
static void unregister_hotplug_status_watch(struct backend_info *be);
31
static void xen_unregister_watchers(struct xenvif *vif);
32 33
static void set_backend_state(struct backend_info *be,
			      enum xenbus_state state);
I
Ian Campbell 已提交
34

35 36 37 38 39 40 41 42
#ifdef CONFIG_DEBUG_FS
struct dentry *xen_netback_dbg_root = NULL;

static int xenvif_read_io_ring(struct seq_file *m, void *v)
{
	struct xenvif_queue *queue = m->private;
	struct xen_netif_tx_back_ring *tx_ring = &queue->tx;
	struct xen_netif_rx_back_ring *rx_ring = &queue->rx;
43
	struct netdev_queue *dev_queue;
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103

	if (tx_ring->sring) {
		struct xen_netif_tx_sring *sring = tx_ring->sring;

		seq_printf(m, "Queue %d\nTX: nr_ents %u\n", queue->id,
			   tx_ring->nr_ents);
		seq_printf(m, "req prod %u (%d) cons %u (%d) event %u (%d)\n",
			   sring->req_prod,
			   sring->req_prod - sring->rsp_prod,
			   tx_ring->req_cons,
			   tx_ring->req_cons - sring->rsp_prod,
			   sring->req_event,
			   sring->req_event - sring->rsp_prod);
		seq_printf(m, "rsp prod %u (base) pvt %u (%d) event %u (%d)\n",
			   sring->rsp_prod,
			   tx_ring->rsp_prod_pvt,
			   tx_ring->rsp_prod_pvt - sring->rsp_prod,
			   sring->rsp_event,
			   sring->rsp_event - sring->rsp_prod);
		seq_printf(m, "pending prod %u pending cons %u nr_pending_reqs %u\n",
			   queue->pending_prod,
			   queue->pending_cons,
			   nr_pending_reqs(queue));
		seq_printf(m, "dealloc prod %u dealloc cons %u dealloc_queue %u\n\n",
			   queue->dealloc_prod,
			   queue->dealloc_cons,
			   queue->dealloc_prod - queue->dealloc_cons);
	}

	if (rx_ring->sring) {
		struct xen_netif_rx_sring *sring = rx_ring->sring;

		seq_printf(m, "RX: nr_ents %u\n", rx_ring->nr_ents);
		seq_printf(m, "req prod %u (%d) cons %u (%d) event %u (%d)\n",
			   sring->req_prod,
			   sring->req_prod - sring->rsp_prod,
			   rx_ring->req_cons,
			   rx_ring->req_cons - sring->rsp_prod,
			   sring->req_event,
			   sring->req_event - sring->rsp_prod);
		seq_printf(m, "rsp prod %u (base) pvt %u (%d) event %u (%d)\n\n",
			   sring->rsp_prod,
			   rx_ring->rsp_prod_pvt,
			   rx_ring->rsp_prod_pvt - sring->rsp_prod,
			   sring->rsp_event,
			   sring->rsp_event - sring->rsp_prod);
	}

	seq_printf(m, "NAPI state: %lx NAPI weight: %d TX queue len %u\n"
		   "Credit timer_pending: %d, credit: %lu, usec: %lu\n"
		   "remaining: %lu, expires: %lu, now: %lu\n",
		   queue->napi.state, queue->napi.weight,
		   skb_queue_len(&queue->tx_queue),
		   timer_pending(&queue->credit_timeout),
		   queue->credit_bytes,
		   queue->credit_usec,
		   queue->remaining_credit,
		   queue->credit_timeout.expires,
		   jiffies);

104 105 106 107 108 109 110
	dev_queue = netdev_get_tx_queue(queue->vif->dev, queue->id);

	seq_printf(m, "\nRx internal queue: len %u max %u pkts %u %s\n",
		   queue->rx_queue_len, queue->rx_queue_max,
		   skb_queue_len(&queue->rx_queue),
		   netif_tx_queue_stopped(dev_queue) ? "stopped" : "running");

111 112 113 114
	return 0;
}

#define XENVIF_KICK_STR "kick"
115
#define BUFFER_SIZE     32
116 117 118 119 120 121 122 123

static ssize_t
xenvif_write_io_ring(struct file *filp, const char __user *buf, size_t count,
		     loff_t *ppos)
{
	struct xenvif_queue *queue =
		((struct seq_file *)filp->private_data)->private;
	int len;
124
	char write[BUFFER_SIZE];
125 126 127 128

	/* don't allow partial writes and check the length */
	if (*ppos != 0)
		return 0;
129
	if (count >= sizeof(write))
130 131 132
		return -ENOSPC;

	len = simple_write_to_buffer(write,
133
				     sizeof(write) - 1,
134 135 136 137 138 139
				     ppos,
				     buf,
				     count);
	if (len < 0)
		return len;

140 141
	write[len] = '\0';

142 143 144 145 146 147 148 149 150 151
	if (!strncmp(write, XENVIF_KICK_STR, sizeof(XENVIF_KICK_STR) - 1))
		xenvif_interrupt(0, (void *)queue);
	else {
		pr_warn("Unknown command to io_ring_q%d. Available: kick\n",
			queue->id);
		count = -EINVAL;
	}
	return count;
}

152
static int xenvif_io_ring_open(struct inode *inode, struct file *filp)
153 154 155 156 157 158 159 160 161 162 163 164 165
{
	int ret;
	void *queue = NULL;

	if (inode->i_private)
		queue = inode->i_private;
	ret = single_open(filp, xenvif_read_io_ring, queue);
	filp->f_mode |= FMODE_PWRITE;
	return ret;
}

static const struct file_operations xenvif_dbg_io_ring_ops_fops = {
	.owner = THIS_MODULE,
166
	.open = xenvif_io_ring_open,
167 168 169 170 171 172
	.read = seq_read,
	.llseek = seq_lseek,
	.release = single_release,
	.write = xenvif_write_io_ring,
};

173
static int xenvif_ctrl_show(struct seq_file *m, void *v)
174 175 176 177 178 179 180
{
	struct xenvif *vif = m->private;

	xenvif_dump_hash_info(vif, m);

	return 0;
}
181
DEFINE_SHOW_ATTRIBUTE(xenvif_ctrl);
182

183
static void xenvif_debugfs_addif(struct xenvif *vif)
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
{
	struct dentry *pfile;
	int i;

	if (IS_ERR_OR_NULL(xen_netback_dbg_root))
		return;

	vif->xenvif_dbg_root = debugfs_create_dir(vif->dev->name,
						  xen_netback_dbg_root);
	if (!IS_ERR_OR_NULL(vif->xenvif_dbg_root)) {
		for (i = 0; i < vif->num_queues; ++i) {
			char filename[sizeof("io_ring_q") + 4];

			snprintf(filename, sizeof(filename), "io_ring_q%d", i);
			pfile = debugfs_create_file(filename,
199
						    0600,
200 201 202 203 204 205 206
						    vif->xenvif_dbg_root,
						    &vif->queues[i],
						    &xenvif_dbg_io_ring_ops_fops);
			if (IS_ERR_OR_NULL(pfile))
				pr_warn("Creation of io_ring file returned %ld!\n",
					PTR_ERR(pfile));
		}
207 208 209

		if (vif->ctrl_irq) {
			pfile = debugfs_create_file("ctrl",
210
						    0400,
211 212
						    vif->xenvif_dbg_root,
						    vif,
213
						    &xenvif_ctrl_fops);
214 215 216 217
			if (IS_ERR_OR_NULL(pfile))
				pr_warn("Creation of ctrl file returned %ld!\n",
					PTR_ERR(pfile));
		}
218 219 220 221 222 223 224 225 226 227 228
	} else
		netdev_warn(vif->dev,
			    "Creation of vif debugfs dir returned %ld!\n",
			    PTR_ERR(vif->xenvif_dbg_root));
}

static void xenvif_debugfs_delif(struct xenvif *vif)
{
	if (IS_ERR_OR_NULL(xen_netback_dbg_root))
		return;

229
	debugfs_remove_recursive(vif->xenvif_dbg_root);
230 231 232 233
	vif->xenvif_dbg_root = NULL;
}
#endif /* CONFIG_DEBUG_FS */

I
Ian Campbell 已提交
234 235 236 237
static int netback_remove(struct xenbus_device *dev)
{
	struct backend_info *be = dev_get_drvdata(&dev->dev);

238 239
	set_backend_state(be, XenbusStateClosed);

I
Ian Campbell 已提交
240 241 242
	unregister_hotplug_status_watch(be);
	if (be->vif) {
		kobject_uevent(&dev->dev.kobj, KOBJ_OFFLINE);
243
		xen_unregister_watchers(be->vif);
I
Ian Campbell 已提交
244
		xenbus_rm(XBT_NIL, dev->nodename, "hotplug-status");
245
		xenvif_free(be->vif);
I
Ian Campbell 已提交
246 247
		be->vif = NULL;
	}
248
	kfree(be->hotplug_script);
I
Ian Campbell 已提交
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
	kfree(be);
	dev_set_drvdata(&dev->dev, NULL);
	return 0;
}


/**
 * Entry point to this code when a new device is created.  Allocate the basic
 * structures and switch to InitWait.
 */
static int netback_probe(struct xenbus_device *dev,
			 const struct xenbus_device_id *id)
{
	const char *message;
	struct xenbus_transaction xbt;
	int err;
	int sg;
266
	const char *script;
I
Ian Campbell 已提交
267 268 269 270 271 272 273 274 275 276 277
	struct backend_info *be = kzalloc(sizeof(struct backend_info),
					  GFP_KERNEL);
	if (!be) {
		xenbus_dev_fatal(dev, -ENOMEM,
				 "allocating backend structure");
		return -ENOMEM;
	}

	be->dev = dev;
	dev_set_drvdata(&dev->dev, be);

278 279 280 281 282
	be->state = XenbusStateInitialising;
	err = xenbus_switch_state(dev, XenbusStateInitialising);
	if (err)
		goto fail;

I
Ian Campbell 已提交
283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
	sg = 1;

	do {
		err = xenbus_transaction_start(&xbt);
		if (err) {
			xenbus_dev_fatal(dev, err, "starting transaction");
			goto fail;
		}

		err = xenbus_printf(xbt, dev->nodename, "feature-sg", "%d", sg);
		if (err) {
			message = "writing feature-sg";
			goto abort_transaction;
		}

		err = xenbus_printf(xbt, dev->nodename, "feature-gso-tcpv4",
				    "%d", sg);
		if (err) {
			message = "writing feature-gso-tcpv4";
			goto abort_transaction;
		}

305 306 307 308 309 310 311
		err = xenbus_printf(xbt, dev->nodename, "feature-gso-tcpv6",
				    "%d", sg);
		if (err) {
			message = "writing feature-gso-tcpv6";
			goto abort_transaction;
		}

312 313 314 315 316 317 318 319 320
		/* We support partial checksum setup for IPv6 packets */
		err = xenbus_printf(xbt, dev->nodename,
				    "feature-ipv6-csum-offload",
				    "%d", 1);
		if (err) {
			message = "writing feature-ipv6-csum-offload";
			goto abort_transaction;
		}

I
Ian Campbell 已提交
321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
		/* We support rx-copy path. */
		err = xenbus_printf(xbt, dev->nodename,
				    "feature-rx-copy", "%d", 1);
		if (err) {
			message = "writing feature-rx-copy";
			goto abort_transaction;
		}

		/*
		 * We don't support rx-flip path (except old guests who don't
		 * grok this feature flag).
		 */
		err = xenbus_printf(xbt, dev->nodename,
				    "feature-rx-flip", "%d", 0);
		if (err) {
			message = "writing feature-rx-flip";
			goto abort_transaction;
		}

340
		/* We support dynamic multicast-control. */
341 342 343 344 345 346 347
		err = xenbus_printf(xbt, dev->nodename,
				    "feature-multicast-control", "%d", 1);
		if (err) {
			message = "writing feature-multicast-control";
			goto abort_transaction;
		}

348 349 350 351 352 353 354 355
		err = xenbus_printf(xbt, dev->nodename,
				    "feature-dynamic-multicast-control",
				    "%d", 1);
		if (err) {
			message = "writing feature-dynamic-multicast-control";
			goto abort_transaction;
		}

I
Ian Campbell 已提交
356 357 358 359 360 361 362 363
		err = xenbus_transaction_end(xbt, 0);
	} while (err == -EAGAIN);

	if (err) {
		xenbus_dev_fatal(dev, err, "completing transaction");
		goto fail;
	}

364 365 366 367 368 369 370 371
	/*
	 * Split event channels support, this is optional so it is not
	 * put inside the above loop.
	 */
	err = xenbus_printf(XBT_NIL, dev->nodename,
			    "feature-split-event-channels",
			    "%u", separate_tx_rx_irq);
	if (err)
372
		pr_debug("Error writing feature-split-event-channels\n");
373

374 375 376 377 378 379
	/* Multi-queue support: This is an optional feature. */
	err = xenbus_printf(XBT_NIL, dev->nodename,
			    "multi-queue-max-queues", "%u", xenvif_max_queues);
	if (err)
		pr_debug("Error writing multi-queue-max-queues\n");

380 381 382 383 384 385
	err = xenbus_printf(XBT_NIL, dev->nodename,
			    "feature-ctrl-ring",
			    "%u", true);
	if (err)
		pr_debug("Error writing feature-ctrl-ring\n");

386 387 388 389 390 391 392 393 394
	script = xenbus_read(XBT_NIL, dev->nodename, "script", NULL);
	if (IS_ERR(script)) {
		err = PTR_ERR(script);
		xenbus_dev_fatal(dev, err, "reading script");
		goto fail;
	}

	be->hotplug_script = script;

395

I
Ian Campbell 已提交
396
	/* This kicks hotplug scripts, so do it immediately. */
397 398 399
	err = backend_create_xenvif(be);
	if (err)
		goto fail;
I
Ian Campbell 已提交
400 401 402 403 404 405 406

	return 0;

abort_transaction:
	xenbus_transaction_end(xbt, 1);
	xenbus_dev_fatal(dev, err, "%s", message);
fail:
407
	pr_debug("failed\n");
I
Ian Campbell 已提交
408 409 410 411 412 413 414 415 416 417 418 419 420 421 422
	netback_remove(dev);
	return err;
}


/*
 * Handle the creation of the hotplug script environment.  We add the script
 * and vif variables to the environment, for the benefit of the vif-* hotplug
 * scripts.
 */
static int netback_uevent(struct xenbus_device *xdev,
			  struct kobj_uevent_env *env)
{
	struct backend_info *be = dev_get_drvdata(&xdev->dev);

423 424 425 426 427 428 429
	if (!be)
		return 0;

	if (add_uevent_var(env, "script=%s", be->hotplug_script))
		return -ENOMEM;

	if (!be->vif)
I
Ian Campbell 已提交
430 431 432 433 434 435
		return 0;

	return add_uevent_var(env, "vif=%s", be->vif->dev->name);
}


436
static int backend_create_xenvif(struct backend_info *be)
I
Ian Campbell 已提交
437 438 439 440
{
	int err;
	long handle;
	struct xenbus_device *dev = be->dev;
441
	struct xenvif *vif;
I
Ian Campbell 已提交
442 443

	if (be->vif != NULL)
444
		return 0;
I
Ian Campbell 已提交
445 446 447 448

	err = xenbus_scanf(XBT_NIL, dev->nodename, "handle", "%li", &handle);
	if (err != 1) {
		xenbus_dev_fatal(dev, err, "reading handle");
449
		return (err < 0) ? err : -EINVAL;
I
Ian Campbell 已提交
450 451
	}

452 453 454
	vif = xenvif_alloc(&dev->dev, dev->otherend_id, handle);
	if (IS_ERR(vif)) {
		err = PTR_ERR(vif);
I
Ian Campbell 已提交
455
		xenbus_dev_fatal(dev, err, "creating interface");
456
		return err;
I
Ian Campbell 已提交
457
	}
458
	be->vif = vif;
459
	vif->be = be;
I
Ian Campbell 已提交
460 461

	kobject_uevent(&dev->dev.kobj, KOBJ_ONLINE);
462
	return 0;
I
Ian Campbell 已提交
463 464
}

465
static void backend_disconnect(struct backend_info *be)
I
Ian Campbell 已提交
466
{
467 468 469
	struct xenvif *vif = be->vif;

	if (vif) {
470
		unsigned int num_queues = vif->num_queues;
471 472
		unsigned int queue_index;

473
		xen_unregister_watchers(vif);
474
#ifdef CONFIG_DEBUG_FS
475
		xenvif_debugfs_delif(vif);
476
#endif /* CONFIG_DEBUG_FS */
477 478
		xenvif_disconnect_data(vif);

479 480 481
		/* At this point some of the handlers may still be active
		 * so we need to have additional synchronization here.
		 */
482
		vif->num_queues = 0;
483
		synchronize_net();
484

485 486 487 488 489
		for (queue_index = 0; queue_index < num_queues; ++queue_index)
			xenvif_deinit_queue(&vif->queues[queue_index]);

		vfree(vif->queues);
		vif->queues = NULL;
490

491
		xenvif_disconnect_ctrl(vif);
492
	}
493 494
}

495
static void backend_connect(struct backend_info *be)
496
{
497 498 499
	if (be->vif)
		connect(be);
}
500

501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517
static inline void backend_switch_state(struct backend_info *be,
					enum xenbus_state state)
{
	struct xenbus_device *dev = be->dev;

	pr_debug("%s -> %s\n", dev->nodename, xenbus_strstate(state));
	be->state = state;

	/* If we are waiting for a hotplug script then defer the
	 * actual xenbus state change.
	 */
	if (!be->have_hotplug_status_watch)
		xenbus_switch_state(dev, state);
}

/* Handle backend state transitions:
 *
518
 * The backend state starts in Initialising and the following transitions are
519 520
 * allowed.
 *
521 522 523 524 525 526 527 528 529
 * Initialising -> InitWait -> Connected
 *          \
 *           \        ^    \         |
 *            \       |     \        |
 *             \      |      \       |
 *              \     |       \      |
 *               \    |        \     |
 *                \   |         \    |
 *                 V  |          V   V
530
 *
531
 *                  Closed  <-> Closing
532 533 534 535 536 537 538 539 540
 *
 * The state argument specifies the eventual state of the backend and the
 * function transitions to that state via the shortest path.
 */
static void set_backend_state(struct backend_info *be,
			      enum xenbus_state state)
{
	while (be->state != state) {
		switch (be->state) {
541 542 543 544 545 546 547 548 549 550 551 552 553 554
		case XenbusStateInitialising:
			switch (state) {
			case XenbusStateInitWait:
			case XenbusStateConnected:
			case XenbusStateClosing:
				backend_switch_state(be, XenbusStateInitWait);
				break;
			case XenbusStateClosed:
				backend_switch_state(be, XenbusStateClosed);
				break;
			default:
				BUG();
			}
			break;
555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607
		case XenbusStateClosed:
			switch (state) {
			case XenbusStateInitWait:
			case XenbusStateConnected:
				backend_switch_state(be, XenbusStateInitWait);
				break;
			case XenbusStateClosing:
				backend_switch_state(be, XenbusStateClosing);
				break;
			default:
				BUG();
			}
			break;
		case XenbusStateInitWait:
			switch (state) {
			case XenbusStateConnected:
				backend_connect(be);
				backend_switch_state(be, XenbusStateConnected);
				break;
			case XenbusStateClosing:
			case XenbusStateClosed:
				backend_switch_state(be, XenbusStateClosing);
				break;
			default:
				BUG();
			}
			break;
		case XenbusStateConnected:
			switch (state) {
			case XenbusStateInitWait:
			case XenbusStateClosing:
			case XenbusStateClosed:
				backend_disconnect(be);
				backend_switch_state(be, XenbusStateClosing);
				break;
			default:
				BUG();
			}
			break;
		case XenbusStateClosing:
			switch (state) {
			case XenbusStateInitWait:
			case XenbusStateConnected:
			case XenbusStateClosed:
				backend_switch_state(be, XenbusStateClosed);
				break;
			default:
				BUG();
			}
			break;
		default:
			BUG();
		}
I
Ian Campbell 已提交
608 609 610 611 612 613 614 615 616 617 618
	}
}

/**
 * Callback received when the frontend's state changes.
 */
static void frontend_changed(struct xenbus_device *dev,
			     enum xenbus_state frontend_state)
{
	struct backend_info *be = dev_get_drvdata(&dev->dev);

619
	pr_debug("%s -> %s\n", dev->otherend, xenbus_strstate(frontend_state));
I
Ian Campbell 已提交
620 621 622 623 624

	be->frontend_state = frontend_state;

	switch (frontend_state) {
	case XenbusStateInitialising:
625
		set_backend_state(be, XenbusStateInitWait);
I
Ian Campbell 已提交
626 627 628 629 630 631
		break;

	case XenbusStateInitialised:
		break;

	case XenbusStateConnected:
632
		set_backend_state(be, XenbusStateConnected);
I
Ian Campbell 已提交
633 634 635
		break;

	case XenbusStateClosing:
636
		set_backend_state(be, XenbusStateClosing);
I
Ian Campbell 已提交
637 638 639
		break;

	case XenbusStateClosed:
640
		set_backend_state(be, XenbusStateClosed);
I
Ian Campbell 已提交
641 642
		if (xenbus_dev_is_online(dev))
			break;
643
		/* fall through - if not online */
I
Ian Campbell 已提交
644
	case XenbusStateUnknown:
645
		set_backend_state(be, XenbusStateClosed);
I
Ian Campbell 已提交
646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714
		device_unregister(&dev->dev);
		break;

	default:
		xenbus_dev_fatal(dev, -EINVAL, "saw state %d at frontend",
				 frontend_state);
		break;
	}
}


static void xen_net_read_rate(struct xenbus_device *dev,
			      unsigned long *bytes, unsigned long *usec)
{
	char *s, *e;
	unsigned long b, u;
	char *ratestr;

	/* Default to unlimited bandwidth. */
	*bytes = ~0UL;
	*usec = 0;

	ratestr = xenbus_read(XBT_NIL, dev->nodename, "rate", NULL);
	if (IS_ERR(ratestr))
		return;

	s = ratestr;
	b = simple_strtoul(s, &e, 10);
	if ((s == e) || (*e != ','))
		goto fail;

	s = e + 1;
	u = simple_strtoul(s, &e, 10);
	if ((s == e) || (*e != '\0'))
		goto fail;

	*bytes = b;
	*usec = u;

	kfree(ratestr);
	return;

 fail:
	pr_warn("Failed to parse network rate limit. Traffic unlimited.\n");
	kfree(ratestr);
}

static int xen_net_read_mac(struct xenbus_device *dev, u8 mac[])
{
	char *s, *e, *macstr;
	int i;

	macstr = s = xenbus_read(XBT_NIL, dev->nodename, "mac", NULL);
	if (IS_ERR(macstr))
		return PTR_ERR(macstr);

	for (i = 0; i < ETH_ALEN; i++) {
		mac[i] = simple_strtoul(s, &e, 16);
		if ((s == e) || (*e != ((i == ETH_ALEN-1) ? '\0' : ':'))) {
			kfree(macstr);
			return -ENOENT;
		}
		s = e+1;
	}

	kfree(macstr);
	return 0;
}

715
static void xen_net_rate_changed(struct xenbus_watch *watch,
716
				 const char *path, const char *token)
717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736
{
	struct xenvif *vif = container_of(watch, struct xenvif, credit_watch);
	struct xenbus_device *dev = xenvif_to_xenbus_device(vif);
	unsigned long   credit_bytes;
	unsigned long   credit_usec;
	unsigned int queue_index;

	xen_net_read_rate(dev, &credit_bytes, &credit_usec);
	for (queue_index = 0; queue_index < vif->num_queues; queue_index++) {
		struct xenvif_queue *queue = &vif->queues[queue_index];

		queue->credit_bytes = credit_bytes;
		queue->credit_usec = credit_usec;
		if (!mod_timer_pending(&queue->credit_timeout, jiffies) &&
			queue->remaining_credit > queue->credit_bytes) {
			queue->remaining_credit = queue->credit_bytes;
		}
	}
}

737 738
static int xen_register_credit_watch(struct xenbus_device *dev,
				     struct xenvif *vif)
739 740 741 742 743
{
	int err = 0;
	char *node;
	unsigned maxlen = strlen(dev->nodename) + sizeof("/rate");

744 745 746
	if (vif->credit_watch.node)
		return -EADDRINUSE;

747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762
	node = kmalloc(maxlen, GFP_KERNEL);
	if (!node)
		return -ENOMEM;
	snprintf(node, maxlen, "%s/rate", dev->nodename);
	vif->credit_watch.node = node;
	vif->credit_watch.callback = xen_net_rate_changed;
	err = register_xenbus_watch(&vif->credit_watch);
	if (err) {
		pr_err("Failed to set watcher %s\n", vif->credit_watch.node);
		kfree(node);
		vif->credit_watch.node = NULL;
		vif->credit_watch.callback = NULL;
	}
	return err;
}

763
static void xen_unregister_credit_watch(struct xenvif *vif)
764 765 766 767 768 769 770 771
{
	if (vif->credit_watch.node) {
		unregister_xenbus_watch(&vif->credit_watch);
		kfree(vif->credit_watch.node);
		vif->credit_watch.node = NULL;
	}
}

772
static void xen_mcast_ctrl_changed(struct xenbus_watch *watch,
773
				   const char *path, const char *token)
774 775 776 777 778
{
	struct xenvif *vif = container_of(watch, struct xenvif,
					  mcast_ctrl_watch);
	struct xenbus_device *dev = xenvif_to_xenbus_device(vif);

779 780
	vif->multicast_control = !!xenbus_read_unsigned(dev->otherend,
					"request-multicast-control", 0);
781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837
}

static int xen_register_mcast_ctrl_watch(struct xenbus_device *dev,
					 struct xenvif *vif)
{
	int err = 0;
	char *node;
	unsigned maxlen = strlen(dev->otherend) +
		sizeof("/request-multicast-control");

	if (vif->mcast_ctrl_watch.node) {
		pr_err_ratelimited("Watch is already registered\n");
		return -EADDRINUSE;
	}

	node = kmalloc(maxlen, GFP_KERNEL);
	if (!node) {
		pr_err("Failed to allocate memory for watch\n");
		return -ENOMEM;
	}
	snprintf(node, maxlen, "%s/request-multicast-control",
		 dev->otherend);
	vif->mcast_ctrl_watch.node = node;
	vif->mcast_ctrl_watch.callback = xen_mcast_ctrl_changed;
	err = register_xenbus_watch(&vif->mcast_ctrl_watch);
	if (err) {
		pr_err("Failed to set watcher %s\n",
		       vif->mcast_ctrl_watch.node);
		kfree(node);
		vif->mcast_ctrl_watch.node = NULL;
		vif->mcast_ctrl_watch.callback = NULL;
	}
	return err;
}

static void xen_unregister_mcast_ctrl_watch(struct xenvif *vif)
{
	if (vif->mcast_ctrl_watch.node) {
		unregister_xenbus_watch(&vif->mcast_ctrl_watch);
		kfree(vif->mcast_ctrl_watch.node);
		vif->mcast_ctrl_watch.node = NULL;
	}
}

static void xen_register_watchers(struct xenbus_device *dev,
				  struct xenvif *vif)
{
	xen_register_credit_watch(dev, vif);
	xen_register_mcast_ctrl_watch(dev, vif);
}

static void xen_unregister_watchers(struct xenvif *vif)
{
	xen_unregister_mcast_ctrl_watch(vif);
	xen_unregister_credit_watch(vif);
}

I
Ian Campbell 已提交
838 839 840 841 842 843 844 845 846 847
static void unregister_hotplug_status_watch(struct backend_info *be)
{
	if (be->have_hotplug_status_watch) {
		unregister_xenbus_watch(&be->hotplug_status_watch);
		kfree(be->hotplug_status_watch.node);
	}
	be->have_hotplug_status_watch = 0;
}

static void hotplug_status_changed(struct xenbus_watch *watch,
848 849
				   const char *path,
				   const char *token)
I
Ian Campbell 已提交
850 851 852 853 854 855 856 857 858 859 860
{
	struct backend_info *be = container_of(watch,
					       struct backend_info,
					       hotplug_status_watch);
	char *str;
	unsigned int len;

	str = xenbus_read(XBT_NIL, be->dev->nodename, "hotplug-status", &len);
	if (IS_ERR(str))
		return;
	if (len == sizeof("connected")-1 && !memcmp(str, "connected", len)) {
861 862 863
		/* Complete any pending state change */
		xenbus_switch_state(be->dev, be->state);

I
Ian Campbell 已提交
864 865 866 867 868 869
		/* Not interested in this watch anymore. */
		unregister_hotplug_status_watch(be);
	}
	kfree(str);
}

870 871 872 873 874 875 876 877 878
static int connect_ctrl_ring(struct backend_info *be)
{
	struct xenbus_device *dev = be->dev;
	struct xenvif *vif = be->vif;
	unsigned int val;
	grant_ref_t ring_ref;
	unsigned int evtchn;
	int err;

879 880 881
	err = xenbus_scanf(XBT_NIL, dev->otherend,
			   "ctrl-ring-ref", "%u", &val);
	if (err < 0)
882 883 884 885
		goto done; /* The frontend does not have a control ring */

	ring_ref = val;

886 887 888
	err = xenbus_scanf(XBT_NIL, dev->otherend,
			   "event-channel-ctrl", "%u", &val);
	if (err < 0) {
889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911
		xenbus_dev_fatal(dev, err,
				 "reading %s/event-channel-ctrl",
				 dev->otherend);
		goto fail;
	}

	evtchn = val;

	err = xenvif_connect_ctrl(vif, ring_ref, evtchn);
	if (err) {
		xenbus_dev_fatal(dev, err,
				 "mapping shared-frame %u port %u",
				 ring_ref, evtchn);
		goto fail;
	}

done:
	return 0;

fail:
	return err;
}

I
Ian Campbell 已提交
912 913 914 915
static void connect(struct backend_info *be)
{
	int err;
	struct xenbus_device *dev = be->dev;
916 917
	unsigned long credit_bytes, credit_usec;
	unsigned int queue_index;
918
	unsigned int requested_num_queues;
919
	struct xenvif_queue *queue;
I
Ian Campbell 已提交
920

921 922 923
	/* Check whether the frontend requested multiple queues
	 * and read the number requested.
	 */
924 925 926
	requested_num_queues = xenbus_read_unsigned(dev->otherend,
					"multi-queue-num-queues", 1);
	if (requested_num_queues > xenvif_max_queues) {
927
		/* buggy or malicious guest */
928
		xenbus_dev_fatal(dev, -EINVAL,
929 930 931 932 933
				 "guest requested %u queues, exceeding the maximum of %u.",
				 requested_num_queues, xenvif_max_queues);
		return;
	}

I
Ian Campbell 已提交
934 935 936 937 938 939
	err = xen_net_read_mac(dev, be->vif->fe_dev_addr);
	if (err) {
		xenbus_dev_fatal(dev, err, "parsing %s/mac", dev->nodename);
		return;
	}

940
	xen_net_read_rate(dev, &credit_bytes, &credit_usec);
941
	xen_unregister_watchers(be->vif);
942
	xen_register_watchers(dev, be->vif);
943 944
	read_xenbus_vif_flags(be);

945 946 947 948 949 950
	err = connect_ctrl_ring(be);
	if (err) {
		xenbus_dev_fatal(dev, err, "connecting control ring");
		return;
	}

951
	/* Use the number of queues requested by the frontend */
952 953
	be->vif->queues = vzalloc(array_size(requested_num_queues,
					     sizeof(struct xenvif_queue)));
954 955 956 957 958 959
	if (!be->vif->queues) {
		xenbus_dev_fatal(dev, -ENOMEM,
				 "allocating queues");
		return;
	}

960
	be->vif->num_queues = requested_num_queues;
961
	be->vif->stalled_queues = requested_num_queues;
962 963 964 965 966 967 968 969 970

	for (queue_index = 0; queue_index < requested_num_queues; ++queue_index) {
		queue = &be->vif->queues[queue_index];
		queue->vif = be->vif;
		queue->id = queue_index;
		snprintf(queue->name, sizeof(queue->name), "%s-q%u",
				be->vif->dev->name, queue->id);

		err = xenvif_init_queue(queue);
971 972 973 974 975 976 977
		if (err) {
			/* xenvif_init_queue() cleans up after itself on
			 * failure, but we need to clean up any previously
			 * initialised queues. Set num_queues to i so that
			 * earlier queues can be destroyed using the regular
			 * disconnect logic.
			 */
978
			be->vif->num_queues = queue_index;
979
			goto err;
980
		}
981

982
		queue->credit_bytes = credit_bytes;
983
		queue->remaining_credit = credit_bytes;
984
		queue->credit_usec = credit_usec;
985

986
		err = connect_data_rings(be, queue);
987
		if (err) {
988 989 990 991
			/* connect_data_rings() cleans up after itself on
			 * failure, but we need to clean up after
			 * xenvif_init_queue() here, and also clean up any
			 * previously initialised queues.
992 993
			 */
			xenvif_deinit_queue(queue);
994
			be->vif->num_queues = queue_index;
995
			goto err;
996
		}
997 998
	}

999
#ifdef CONFIG_DEBUG_FS
1000
	xenvif_debugfs_addif(be->vif);
1001
#endif /* CONFIG_DEBUG_FS */
1002

1003 1004 1005 1006 1007 1008 1009 1010
	/* Initialisation completed, tell core driver the number of
	 * active queues.
	 */
	rtnl_lock();
	netif_set_real_num_tx_queues(be->vif->dev, requested_num_queues);
	netif_set_real_num_rx_queues(be->vif->dev, requested_num_queues);
	rtnl_unlock();

1011
	xenvif_carrier_on(be->vif);
I
Ian Campbell 已提交
1012 1013 1014 1015 1016

	unregister_hotplug_status_watch(be);
	err = xenbus_watch_pathfmt(dev, &be->hotplug_status_watch,
				   hotplug_status_changed,
				   "%s/%s", dev->nodename, "hotplug-status");
1017
	if (!err)
I
Ian Campbell 已提交
1018 1019
		be->have_hotplug_status_watch = 1;

1020 1021 1022 1023 1024
	netif_tx_wake_all_queues(be->vif->dev);

	return;

err:
1025
	if (be->vif->num_queues > 0)
1026
		xenvif_disconnect_data(be->vif); /* Clean up existing queues */
1027 1028
	for (queue_index = 0; queue_index < be->vif->num_queues; ++queue_index)
		xenvif_deinit_queue(&be->vif->queues[queue_index]);
1029 1030
	vfree(be->vif->queues);
	be->vif->queues = NULL;
1031
	be->vif->num_queues = 0;
1032
	xenvif_disconnect_ctrl(be->vif);
1033
	return;
I
Ian Campbell 已提交
1034 1035 1036
}


1037 1038
static int connect_data_rings(struct backend_info *be,
			      struct xenvif_queue *queue)
I
Ian Campbell 已提交
1039 1040
{
	struct xenbus_device *dev = be->dev;
1041
	unsigned int num_queues = queue->vif->num_queues;
I
Ian Campbell 已提交
1042
	unsigned long tx_ring_ref, rx_ring_ref;
1043
	unsigned int tx_evtchn, rx_evtchn;
I
Ian Campbell 已提交
1044
	int err;
1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073
	char *xspath;
	size_t xspathsize;
	const size_t xenstore_path_ext_size = 11; /* sufficient for "/queue-NNN" */

	/* If the frontend requested 1 queue, or we have fallen back
	 * to single queue due to lack of frontend support for multi-
	 * queue, expect the remaining XenStore keys in the toplevel
	 * directory. Otherwise, expect them in a subdirectory called
	 * queue-N.
	 */
	if (num_queues == 1) {
		xspath = kzalloc(strlen(dev->otherend) + 1, GFP_KERNEL);
		if (!xspath) {
			xenbus_dev_fatal(dev, -ENOMEM,
					 "reading ring references");
			return -ENOMEM;
		}
		strcpy(xspath, dev->otherend);
	} else {
		xspathsize = strlen(dev->otherend) + xenstore_path_ext_size;
		xspath = kzalloc(xspathsize, GFP_KERNEL);
		if (!xspath) {
			xenbus_dev_fatal(dev, -ENOMEM,
					 "reading ring references");
			return -ENOMEM;
		}
		snprintf(xspath, xspathsize, "%s/queue-%u", dev->otherend,
			 queue->id);
	}
I
Ian Campbell 已提交
1074

1075
	err = xenbus_gather(XBT_NIL, xspath,
I
Ian Campbell 已提交
1076
			    "tx-ring-ref", "%lu", &tx_ring_ref,
1077
			    "rx-ring-ref", "%lu", &rx_ring_ref, NULL);
I
Ian Campbell 已提交
1078 1079
	if (err) {
		xenbus_dev_fatal(dev, err,
1080
				 "reading %s/ring-ref",
1081 1082
				 xspath);
		goto err;
I
Ian Campbell 已提交
1083 1084
	}

1085
	/* Try split event channels first, then single event channel. */
1086
	err = xenbus_gather(XBT_NIL, xspath,
1087 1088 1089
			    "event-channel-tx", "%u", &tx_evtchn,
			    "event-channel-rx", "%u", &rx_evtchn, NULL);
	if (err < 0) {
1090
		err = xenbus_scanf(XBT_NIL, xspath,
1091 1092 1093 1094
				   "event-channel", "%u", &tx_evtchn);
		if (err < 0) {
			xenbus_dev_fatal(dev, err,
					 "reading %s/event-channel(-tx/rx)",
1095 1096
					 xspath);
			goto err;
1097 1098 1099 1100
		}
		rx_evtchn = tx_evtchn;
	}

1101
	/* Map the shared frame, irq etc. */
1102 1103
	err = xenvif_connect_data(queue, tx_ring_ref, rx_ring_ref,
				  tx_evtchn, rx_evtchn);
1104 1105 1106 1107 1108
	if (err) {
		xenbus_dev_fatal(dev, err,
				 "mapping shared-frames %lu/%lu port tx %u rx %u",
				 tx_ring_ref, rx_ring_ref,
				 tx_evtchn, rx_evtchn);
1109
		goto err;
1110 1111
	}

1112 1113 1114 1115
	err = 0;
err: /* Regular return falls through with err == 0 */
	kfree(xspath);
	return err;
1116 1117 1118 1119 1120 1121 1122
}

static int read_xenbus_vif_flags(struct backend_info *be)
{
	struct xenvif *vif = be->vif;
	struct xenbus_device *dev = be->dev;
	unsigned int rx_copy;
1123
	int err;
1124

I
Ian Campbell 已提交
1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138
	err = xenbus_scanf(XBT_NIL, dev->otherend, "request-rx-copy", "%u",
			   &rx_copy);
	if (err == -ENOENT) {
		err = 0;
		rx_copy = 0;
	}
	if (err < 0) {
		xenbus_dev_fatal(dev, err, "reading %s/request-rx-copy",
				 dev->otherend);
		return err;
	}
	if (!rx_copy)
		return -EOPNOTSUPP;

1139
	if (!xenbus_read_unsigned(dev->otherend, "feature-rx-notify", 0)) {
1140 1141 1142 1143 1144 1145
		/* - Reduce drain timeout to poll more frequently for
		 *   Rx requests.
		 * - Disable Rx stall detection.
		 */
		be->vif->drain_timeout = msecs_to_jiffies(30);
		be->vif->stall_timeout = 0;
I
Ian Campbell 已提交
1146 1147
	}

1148
	vif->can_sg = !!xenbus_read_unsigned(dev->otherend, "feature-sg", 0);
I
Ian Campbell 已提交
1149

1150 1151
	vif->gso_mask = 0;

1152
	if (xenbus_read_unsigned(dev->otherend, "feature-gso-tcpv4", 0))
1153
		vif->gso_mask |= GSO_BIT(TCPV4);
I
Ian Campbell 已提交
1154

1155
	if (xenbus_read_unsigned(dev->otherend, "feature-gso-tcpv6", 0))
1156 1157
		vif->gso_mask |= GSO_BIT(TCPV6);

1158 1159
	vif->ip_csum = !xenbus_read_unsigned(dev->otherend,
					     "feature-no-csum-offload", 0);
1160

1161 1162
	vif->ipv6_csum = !!xenbus_read_unsigned(dev->otherend,
						"feature-ipv6-csum-offload", 0);
I
Ian Campbell 已提交
1163 1164 1165 1166 1167 1168 1169 1170 1171

	return 0;
}

static const struct xenbus_device_id netback_ids[] = {
	{ "vif" },
	{ "" }
};

1172 1173
static struct xenbus_driver netback_driver = {
	.ids = netback_ids,
I
Ian Campbell 已提交
1174 1175 1176 1177
	.probe = netback_probe,
	.remove = netback_remove,
	.uevent = netback_uevent,
	.otherend_changed = frontend_changed,
1178
};
I
Ian Campbell 已提交
1179 1180 1181

int xenvif_xenbus_init(void)
{
1182
	return xenbus_register_backend(&netback_driver);
I
Ian Campbell 已提交
1183
}
1184 1185 1186 1187 1188

void xenvif_xenbus_fini(void)
{
	return xenbus_unregister_driver(&netback_driver);
}