1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Xenbus code for netif backend 4 * 5 * Copyright (C) 2005 Rusty Russell <rusty@rustcorp.com.au> 6 * Copyright (C) 2005 XenSource Ltd 7 */ 8 9 #include "common.h" 10 #include <linux/vmalloc.h> 11 #include <linux/rtnetlink.h> 12 13 static int connect_data_rings(struct backend_info *be, 14 struct xenvif_queue *queue); 15 static void connect(struct backend_info *be); 16 static int read_xenbus_vif_flags(struct backend_info *be); 17 static int backend_create_xenvif(struct backend_info *be); 18 static void unregister_hotplug_status_watch(struct backend_info *be); 19 static void xen_unregister_watchers(struct xenvif *vif); 20 static void set_backend_state(struct backend_info *be, 21 enum xenbus_state state); 22 23 #ifdef CONFIG_DEBUG_FS 24 struct dentry *xen_netback_dbg_root = NULL; 25 26 static int xenvif_read_io_ring(struct seq_file *m, void *v) 27 { 28 struct xenvif_queue *queue = m->private; 29 struct xen_netif_tx_back_ring *tx_ring = &queue->tx; 30 struct xen_netif_rx_back_ring *rx_ring = &queue->rx; 31 struct netdev_queue *dev_queue; 32 33 if (tx_ring->sring) { 34 struct xen_netif_tx_sring *sring = tx_ring->sring; 35 36 seq_printf(m, "Queue %d\nTX: nr_ents %u\n", queue->id, 37 tx_ring->nr_ents); 38 seq_printf(m, "req prod %u (%d) cons %u (%d) event %u (%d)\n", 39 sring->req_prod, 40 sring->req_prod - sring->rsp_prod, 41 tx_ring->req_cons, 42 tx_ring->req_cons - sring->rsp_prod, 43 sring->req_event, 44 sring->req_event - sring->rsp_prod); 45 seq_printf(m, "rsp prod %u (base) pvt %u (%d) event %u (%d)\n", 46 sring->rsp_prod, 47 tx_ring->rsp_prod_pvt, 48 tx_ring->rsp_prod_pvt - sring->rsp_prod, 49 sring->rsp_event, 50 sring->rsp_event - sring->rsp_prod); 51 seq_printf(m, "pending prod %u pending cons %u nr_pending_reqs %u\n", 52 queue->pending_prod, 53 queue->pending_cons, 54 nr_pending_reqs(queue)); 55 seq_printf(m, "dealloc prod %u dealloc cons %u dealloc_queue %u\n\n", 56 queue->dealloc_prod, 57 queue->dealloc_cons, 58 queue->dealloc_prod - queue->dealloc_cons); 59 } 60 61 if (rx_ring->sring) { 62 struct xen_netif_rx_sring *sring = rx_ring->sring; 63 64 seq_printf(m, "RX: nr_ents %u\n", rx_ring->nr_ents); 65 seq_printf(m, "req prod %u (%d) cons %u (%d) event %u (%d)\n", 66 sring->req_prod, 67 sring->req_prod - sring->rsp_prod, 68 rx_ring->req_cons, 69 rx_ring->req_cons - sring->rsp_prod, 70 sring->req_event, 71 sring->req_event - sring->rsp_prod); 72 seq_printf(m, "rsp prod %u (base) pvt %u (%d) event %u (%d)\n\n", 73 sring->rsp_prod, 74 rx_ring->rsp_prod_pvt, 75 rx_ring->rsp_prod_pvt - sring->rsp_prod, 76 sring->rsp_event, 77 sring->rsp_event - sring->rsp_prod); 78 } 79 80 seq_printf(m, "NAPI state: %lx NAPI weight: %d TX queue len %u\n" 81 "Credit timer_pending: %d, credit: %lu, usec: %lu\n" 82 "remaining: %lu, expires: %lu, now: %lu\n", 83 queue->napi.state, queue->napi.weight, 84 skb_queue_len(&queue->tx_queue), 85 timer_pending(&queue->credit_timeout), 86 queue->credit_bytes, 87 queue->credit_usec, 88 queue->remaining_credit, 89 queue->credit_timeout.expires, 90 jiffies); 91 92 dev_queue = netdev_get_tx_queue(queue->vif->dev, queue->id); 93 94 seq_printf(m, "\nRx internal queue: len %u max %u pkts %u %s\n", 95 queue->rx_queue_len, queue->rx_queue_max, 96 skb_queue_len(&queue->rx_queue), 97 netif_tx_queue_stopped(dev_queue) ? "stopped" : "running"); 98 99 return 0; 100 } 101 102 #define XENVIF_KICK_STR "kick" 103 #define BUFFER_SIZE 32 104 105 static ssize_t 106 xenvif_write_io_ring(struct file *filp, const char __user *buf, size_t count, 107 loff_t *ppos) 108 { 109 struct xenvif_queue *queue = 110 ((struct seq_file *)filp->private_data)->private; 111 int len; 112 char write[BUFFER_SIZE]; 113 114 /* don't allow partial writes and check the length */ 115 if (*ppos != 0) 116 return 0; 117 if (count >= sizeof(write)) 118 return -ENOSPC; 119 120 len = simple_write_to_buffer(write, 121 sizeof(write) - 1, 122 ppos, 123 buf, 124 count); 125 if (len < 0) 126 return len; 127 128 write[len] = '\0'; 129 130 if (!strncmp(write, XENVIF_KICK_STR, sizeof(XENVIF_KICK_STR) - 1)) 131 xenvif_interrupt(0, (void *)queue); 132 else { 133 pr_warn("Unknown command to io_ring_q%d. Available: kick\n", 134 queue->id); 135 count = -EINVAL; 136 } 137 return count; 138 } 139 140 static int xenvif_io_ring_open(struct inode *inode, struct file *filp) 141 { 142 int ret; 143 void *queue = NULL; 144 145 if (inode->i_private) 146 queue = inode->i_private; 147 ret = single_open(filp, xenvif_read_io_ring, queue); 148 filp->f_mode |= FMODE_PWRITE; 149 return ret; 150 } 151 152 static const struct file_operations xenvif_dbg_io_ring_ops_fops = { 153 .owner = THIS_MODULE, 154 .open = xenvif_io_ring_open, 155 .read = seq_read, 156 .llseek = seq_lseek, 157 .release = single_release, 158 .write = xenvif_write_io_ring, 159 }; 160 161 static int xenvif_ctrl_show(struct seq_file *m, void *v) 162 { 163 struct xenvif *vif = m->private; 164 165 xenvif_dump_hash_info(vif, m); 166 167 return 0; 168 } 169 DEFINE_SHOW_ATTRIBUTE(xenvif_ctrl); 170 171 static void xenvif_debugfs_addif(struct xenvif *vif) 172 { 173 int i; 174 175 vif->xenvif_dbg_root = debugfs_create_dir(vif->dev->name, 176 xen_netback_dbg_root); 177 for (i = 0; i < vif->num_queues; ++i) { 178 char filename[sizeof("io_ring_q") + 4]; 179 180 snprintf(filename, sizeof(filename), "io_ring_q%d", i); 181 debugfs_create_file(filename, 0600, vif->xenvif_dbg_root, 182 &vif->queues[i], 183 &xenvif_dbg_io_ring_ops_fops); 184 } 185 186 if (vif->ctrl_irq) 187 debugfs_create_file("ctrl", 0400, vif->xenvif_dbg_root, vif, 188 &xenvif_ctrl_fops); 189 } 190 191 static void xenvif_debugfs_delif(struct xenvif *vif) 192 { 193 debugfs_remove_recursive(vif->xenvif_dbg_root); 194 vif->xenvif_dbg_root = NULL; 195 } 196 #endif /* CONFIG_DEBUG_FS */ 197 198 /* 199 * Handle the creation of the hotplug script environment. We add the script 200 * and vif variables to the environment, for the benefit of the vif-* hotplug 201 * scripts. 202 */ 203 static int netback_uevent(struct xenbus_device *xdev, 204 struct kobj_uevent_env *env) 205 { 206 struct backend_info *be = dev_get_drvdata(&xdev->dev); 207 208 if (!be) 209 return 0; 210 211 if (add_uevent_var(env, "script=%s", be->hotplug_script)) 212 return -ENOMEM; 213 214 if (!be->vif) 215 return 0; 216 217 return add_uevent_var(env, "vif=%s", be->vif->dev->name); 218 } 219 220 221 static int backend_create_xenvif(struct backend_info *be) 222 { 223 int err; 224 long handle; 225 struct xenbus_device *dev = be->dev; 226 struct xenvif *vif; 227 228 if (be->vif != NULL) 229 return 0; 230 231 err = xenbus_scanf(XBT_NIL, dev->nodename, "handle", "%li", &handle); 232 if (err != 1) { 233 xenbus_dev_fatal(dev, err, "reading handle"); 234 return (err < 0) ? err : -EINVAL; 235 } 236 237 vif = xenvif_alloc(&dev->dev, dev->otherend_id, handle); 238 if (IS_ERR(vif)) { 239 err = PTR_ERR(vif); 240 xenbus_dev_fatal(dev, err, "creating interface"); 241 return err; 242 } 243 be->vif = vif; 244 vif->be = be; 245 246 kobject_uevent(&dev->dev.kobj, KOBJ_ONLINE); 247 return 0; 248 } 249 250 static void backend_disconnect(struct backend_info *be) 251 { 252 struct xenvif *vif = be->vif; 253 254 if (vif) { 255 unsigned int num_queues = vif->num_queues; 256 unsigned int queue_index; 257 258 xen_unregister_watchers(vif); 259 #ifdef CONFIG_DEBUG_FS 260 xenvif_debugfs_delif(vif); 261 #endif /* CONFIG_DEBUG_FS */ 262 xenvif_disconnect_data(vif); 263 264 /* At this point some of the handlers may still be active 265 * so we need to have additional synchronization here. 266 */ 267 vif->num_queues = 0; 268 synchronize_net(); 269 270 for (queue_index = 0; queue_index < num_queues; ++queue_index) 271 xenvif_deinit_queue(&vif->queues[queue_index]); 272 273 vfree(vif->queues); 274 vif->queues = NULL; 275 276 xenvif_disconnect_ctrl(vif); 277 } 278 } 279 280 static void backend_connect(struct backend_info *be) 281 { 282 if (be->vif) 283 connect(be); 284 } 285 286 static inline void backend_switch_state(struct backend_info *be, 287 enum xenbus_state state) 288 { 289 struct xenbus_device *dev = be->dev; 290 291 pr_debug("%s -> %s\n", dev->nodename, xenbus_strstate(state)); 292 be->state = state; 293 294 /* If we are waiting for a hotplug script then defer the 295 * actual xenbus state change. 296 */ 297 if (!be->have_hotplug_status_watch) 298 xenbus_switch_state(dev, state); 299 } 300 301 /* Handle backend state transitions: 302 * 303 * The backend state starts in Initialising and the following transitions are 304 * allowed. 305 * 306 * Initialising -> InitWait -> Connected 307 * \ 308 * \ ^ \ | 309 * \ | \ | 310 * \ | \ | 311 * \ | \ | 312 * \ | \ | 313 * \ | \ | 314 * V | V V 315 * 316 * Closed <-> Closing 317 * 318 * The state argument specifies the eventual state of the backend and the 319 * function transitions to that state via the shortest path. 320 */ 321 static void set_backend_state(struct backend_info *be, 322 enum xenbus_state state) 323 { 324 while (be->state != state) { 325 switch (be->state) { 326 case XenbusStateInitialising: 327 switch (state) { 328 case XenbusStateInitWait: 329 case XenbusStateConnected: 330 case XenbusStateClosing: 331 backend_switch_state(be, XenbusStateInitWait); 332 break; 333 case XenbusStateClosed: 334 backend_switch_state(be, XenbusStateClosed); 335 break; 336 default: 337 BUG(); 338 } 339 break; 340 case XenbusStateClosed: 341 switch (state) { 342 case XenbusStateInitWait: 343 case XenbusStateConnected: 344 backend_switch_state(be, XenbusStateInitWait); 345 break; 346 case XenbusStateClosing: 347 backend_switch_state(be, XenbusStateClosing); 348 break; 349 default: 350 BUG(); 351 } 352 break; 353 case XenbusStateInitWait: 354 switch (state) { 355 case XenbusStateConnected: 356 backend_connect(be); 357 backend_switch_state(be, XenbusStateConnected); 358 break; 359 case XenbusStateClosing: 360 case XenbusStateClosed: 361 backend_switch_state(be, XenbusStateClosing); 362 break; 363 default: 364 BUG(); 365 } 366 break; 367 case XenbusStateConnected: 368 switch (state) { 369 case XenbusStateInitWait: 370 case XenbusStateClosing: 371 case XenbusStateClosed: 372 backend_disconnect(be); 373 backend_switch_state(be, XenbusStateClosing); 374 break; 375 default: 376 BUG(); 377 } 378 break; 379 case XenbusStateClosing: 380 switch (state) { 381 case XenbusStateInitWait: 382 case XenbusStateConnected: 383 case XenbusStateClosed: 384 backend_switch_state(be, XenbusStateClosed); 385 break; 386 default: 387 BUG(); 388 } 389 break; 390 default: 391 BUG(); 392 } 393 } 394 } 395 396 /** 397 * Callback received when the frontend's state changes. 398 */ 399 static void frontend_changed(struct xenbus_device *dev, 400 enum xenbus_state frontend_state) 401 { 402 struct backend_info *be = dev_get_drvdata(&dev->dev); 403 404 pr_debug("%s -> %s\n", dev->otherend, xenbus_strstate(frontend_state)); 405 406 be->frontend_state = frontend_state; 407 408 switch (frontend_state) { 409 case XenbusStateInitialising: 410 set_backend_state(be, XenbusStateInitWait); 411 break; 412 413 case XenbusStateInitialised: 414 break; 415 416 case XenbusStateConnected: 417 set_backend_state(be, XenbusStateConnected); 418 break; 419 420 case XenbusStateClosing: 421 set_backend_state(be, XenbusStateClosing); 422 break; 423 424 case XenbusStateClosed: 425 set_backend_state(be, XenbusStateClosed); 426 if (xenbus_dev_is_online(dev)) 427 break; 428 /* fall through - if not online */ 429 case XenbusStateUnknown: 430 set_backend_state(be, XenbusStateClosed); 431 device_unregister(&dev->dev); 432 break; 433 434 default: 435 xenbus_dev_fatal(dev, -EINVAL, "saw state %d at frontend", 436 frontend_state); 437 break; 438 } 439 } 440 441 442 static void xen_net_read_rate(struct xenbus_device *dev, 443 unsigned long *bytes, unsigned long *usec) 444 { 445 char *s, *e; 446 unsigned long b, u; 447 char *ratestr; 448 449 /* Default to unlimited bandwidth. */ 450 *bytes = ~0UL; 451 *usec = 0; 452 453 ratestr = xenbus_read(XBT_NIL, dev->nodename, "rate", NULL); 454 if (IS_ERR(ratestr)) 455 return; 456 457 s = ratestr; 458 b = simple_strtoul(s, &e, 10); 459 if ((s == e) || (*e != ',')) 460 goto fail; 461 462 s = e + 1; 463 u = simple_strtoul(s, &e, 10); 464 if ((s == e) || (*e != '\0')) 465 goto fail; 466 467 *bytes = b; 468 *usec = u; 469 470 kfree(ratestr); 471 return; 472 473 fail: 474 pr_warn("Failed to parse network rate limit. Traffic unlimited.\n"); 475 kfree(ratestr); 476 } 477 478 static int xen_net_read_mac(struct xenbus_device *dev, u8 mac[]) 479 { 480 char *s, *e, *macstr; 481 int i; 482 483 macstr = s = xenbus_read(XBT_NIL, dev->nodename, "mac", NULL); 484 if (IS_ERR(macstr)) 485 return PTR_ERR(macstr); 486 487 for (i = 0; i < ETH_ALEN; i++) { 488 mac[i] = simple_strtoul(s, &e, 16); 489 if ((s == e) || (*e != ((i == ETH_ALEN-1) ? '\0' : ':'))) { 490 kfree(macstr); 491 return -ENOENT; 492 } 493 s = e+1; 494 } 495 496 kfree(macstr); 497 return 0; 498 } 499 500 static void xen_net_rate_changed(struct xenbus_watch *watch, 501 const char *path, const char *token) 502 { 503 struct xenvif *vif = container_of(watch, struct xenvif, credit_watch); 504 struct xenbus_device *dev = xenvif_to_xenbus_device(vif); 505 unsigned long credit_bytes; 506 unsigned long credit_usec; 507 unsigned int queue_index; 508 509 xen_net_read_rate(dev, &credit_bytes, &credit_usec); 510 for (queue_index = 0; queue_index < vif->num_queues; queue_index++) { 511 struct xenvif_queue *queue = &vif->queues[queue_index]; 512 513 queue->credit_bytes = credit_bytes; 514 queue->credit_usec = credit_usec; 515 if (!mod_timer_pending(&queue->credit_timeout, jiffies) && 516 queue->remaining_credit > queue->credit_bytes) { 517 queue->remaining_credit = queue->credit_bytes; 518 } 519 } 520 } 521 522 static int xen_register_credit_watch(struct xenbus_device *dev, 523 struct xenvif *vif) 524 { 525 int err = 0; 526 char *node; 527 unsigned maxlen = strlen(dev->nodename) + sizeof("/rate"); 528 529 if (vif->credit_watch.node) 530 return -EADDRINUSE; 531 532 node = kmalloc(maxlen, GFP_KERNEL); 533 if (!node) 534 return -ENOMEM; 535 snprintf(node, maxlen, "%s/rate", dev->nodename); 536 vif->credit_watch.node = node; 537 vif->credit_watch.callback = xen_net_rate_changed; 538 err = register_xenbus_watch(&vif->credit_watch); 539 if (err) { 540 pr_err("Failed to set watcher %s\n", vif->credit_watch.node); 541 kfree(node); 542 vif->credit_watch.node = NULL; 543 vif->credit_watch.callback = NULL; 544 } 545 return err; 546 } 547 548 static void xen_unregister_credit_watch(struct xenvif *vif) 549 { 550 if (vif->credit_watch.node) { 551 unregister_xenbus_watch(&vif->credit_watch); 552 kfree(vif->credit_watch.node); 553 vif->credit_watch.node = NULL; 554 } 555 } 556 557 static void xen_mcast_ctrl_changed(struct xenbus_watch *watch, 558 const char *path, const char *token) 559 { 560 struct xenvif *vif = container_of(watch, struct xenvif, 561 mcast_ctrl_watch); 562 struct xenbus_device *dev = xenvif_to_xenbus_device(vif); 563 564 vif->multicast_control = !!xenbus_read_unsigned(dev->otherend, 565 "request-multicast-control", 0); 566 } 567 568 static int xen_register_mcast_ctrl_watch(struct xenbus_device *dev, 569 struct xenvif *vif) 570 { 571 int err = 0; 572 char *node; 573 unsigned maxlen = strlen(dev->otherend) + 574 sizeof("/request-multicast-control"); 575 576 if (vif->mcast_ctrl_watch.node) { 577 pr_err_ratelimited("Watch is already registered\n"); 578 return -EADDRINUSE; 579 } 580 581 node = kmalloc(maxlen, GFP_KERNEL); 582 if (!node) { 583 pr_err("Failed to allocate memory for watch\n"); 584 return -ENOMEM; 585 } 586 snprintf(node, maxlen, "%s/request-multicast-control", 587 dev->otherend); 588 vif->mcast_ctrl_watch.node = node; 589 vif->mcast_ctrl_watch.callback = xen_mcast_ctrl_changed; 590 err = register_xenbus_watch(&vif->mcast_ctrl_watch); 591 if (err) { 592 pr_err("Failed to set watcher %s\n", 593 vif->mcast_ctrl_watch.node); 594 kfree(node); 595 vif->mcast_ctrl_watch.node = NULL; 596 vif->mcast_ctrl_watch.callback = NULL; 597 } 598 return err; 599 } 600 601 static void xen_unregister_mcast_ctrl_watch(struct xenvif *vif) 602 { 603 if (vif->mcast_ctrl_watch.node) { 604 unregister_xenbus_watch(&vif->mcast_ctrl_watch); 605 kfree(vif->mcast_ctrl_watch.node); 606 vif->mcast_ctrl_watch.node = NULL; 607 } 608 } 609 610 static void xen_register_watchers(struct xenbus_device *dev, 611 struct xenvif *vif) 612 { 613 xen_register_credit_watch(dev, vif); 614 xen_register_mcast_ctrl_watch(dev, vif); 615 } 616 617 static void xen_unregister_watchers(struct xenvif *vif) 618 { 619 xen_unregister_mcast_ctrl_watch(vif); 620 xen_unregister_credit_watch(vif); 621 } 622 623 static void unregister_hotplug_status_watch(struct backend_info *be) 624 { 625 if (be->have_hotplug_status_watch) { 626 unregister_xenbus_watch(&be->hotplug_status_watch); 627 kfree(be->hotplug_status_watch.node); 628 } 629 be->have_hotplug_status_watch = 0; 630 } 631 632 static void hotplug_status_changed(struct xenbus_watch *watch, 633 const char *path, 634 const char *token) 635 { 636 struct backend_info *be = container_of(watch, 637 struct backend_info, 638 hotplug_status_watch); 639 char *str; 640 unsigned int len; 641 642 str = xenbus_read(XBT_NIL, be->dev->nodename, "hotplug-status", &len); 643 if (IS_ERR(str)) 644 return; 645 if (len == sizeof("connected")-1 && !memcmp(str, "connected", len)) { 646 /* Complete any pending state change */ 647 xenbus_switch_state(be->dev, be->state); 648 649 /* Not interested in this watch anymore. */ 650 unregister_hotplug_status_watch(be); 651 } 652 kfree(str); 653 } 654 655 static int connect_ctrl_ring(struct backend_info *be) 656 { 657 struct xenbus_device *dev = be->dev; 658 struct xenvif *vif = be->vif; 659 unsigned int val; 660 grant_ref_t ring_ref; 661 unsigned int evtchn; 662 int err; 663 664 err = xenbus_scanf(XBT_NIL, dev->otherend, 665 "ctrl-ring-ref", "%u", &val); 666 if (err < 0) 667 goto done; /* The frontend does not have a control ring */ 668 669 ring_ref = val; 670 671 err = xenbus_scanf(XBT_NIL, dev->otherend, 672 "event-channel-ctrl", "%u", &val); 673 if (err < 0) { 674 xenbus_dev_fatal(dev, err, 675 "reading %s/event-channel-ctrl", 676 dev->otherend); 677 goto fail; 678 } 679 680 evtchn = val; 681 682 err = xenvif_connect_ctrl(vif, ring_ref, evtchn); 683 if (err) { 684 xenbus_dev_fatal(dev, err, 685 "mapping shared-frame %u port %u", 686 ring_ref, evtchn); 687 goto fail; 688 } 689 690 done: 691 return 0; 692 693 fail: 694 return err; 695 } 696 697 static void connect(struct backend_info *be) 698 { 699 int err; 700 struct xenbus_device *dev = be->dev; 701 unsigned long credit_bytes, credit_usec; 702 unsigned int queue_index; 703 unsigned int requested_num_queues; 704 struct xenvif_queue *queue; 705 706 /* Check whether the frontend requested multiple queues 707 * and read the number requested. 708 */ 709 requested_num_queues = xenbus_read_unsigned(dev->otherend, 710 "multi-queue-num-queues", 1); 711 if (requested_num_queues > xenvif_max_queues) { 712 /* buggy or malicious guest */ 713 xenbus_dev_fatal(dev, -EINVAL, 714 "guest requested %u queues, exceeding the maximum of %u.", 715 requested_num_queues, xenvif_max_queues); 716 return; 717 } 718 719 err = xen_net_read_mac(dev, be->vif->fe_dev_addr); 720 if (err) { 721 xenbus_dev_fatal(dev, err, "parsing %s/mac", dev->nodename); 722 return; 723 } 724 725 xen_net_read_rate(dev, &credit_bytes, &credit_usec); 726 xen_unregister_watchers(be->vif); 727 xen_register_watchers(dev, be->vif); 728 read_xenbus_vif_flags(be); 729 730 err = connect_ctrl_ring(be); 731 if (err) { 732 xenbus_dev_fatal(dev, err, "connecting control ring"); 733 return; 734 } 735 736 /* Use the number of queues requested by the frontend */ 737 be->vif->queues = vzalloc(array_size(requested_num_queues, 738 sizeof(struct xenvif_queue))); 739 if (!be->vif->queues) { 740 xenbus_dev_fatal(dev, -ENOMEM, 741 "allocating queues"); 742 return; 743 } 744 745 be->vif->num_queues = requested_num_queues; 746 be->vif->stalled_queues = requested_num_queues; 747 748 for (queue_index = 0; queue_index < requested_num_queues; ++queue_index) { 749 queue = &be->vif->queues[queue_index]; 750 queue->vif = be->vif; 751 queue->id = queue_index; 752 snprintf(queue->name, sizeof(queue->name), "%s-q%u", 753 be->vif->dev->name, queue->id); 754 755 err = xenvif_init_queue(queue); 756 if (err) { 757 /* xenvif_init_queue() cleans up after itself on 758 * failure, but we need to clean up any previously 759 * initialised queues. Set num_queues to i so that 760 * earlier queues can be destroyed using the regular 761 * disconnect logic. 762 */ 763 be->vif->num_queues = queue_index; 764 goto err; 765 } 766 767 queue->credit_bytes = credit_bytes; 768 queue->remaining_credit = credit_bytes; 769 queue->credit_usec = credit_usec; 770 771 err = connect_data_rings(be, queue); 772 if (err) { 773 /* connect_data_rings() cleans up after itself on 774 * failure, but we need to clean up after 775 * xenvif_init_queue() here, and also clean up any 776 * previously initialised queues. 777 */ 778 xenvif_deinit_queue(queue); 779 be->vif->num_queues = queue_index; 780 goto err; 781 } 782 } 783 784 #ifdef CONFIG_DEBUG_FS 785 xenvif_debugfs_addif(be->vif); 786 #endif /* CONFIG_DEBUG_FS */ 787 788 /* Initialisation completed, tell core driver the number of 789 * active queues. 790 */ 791 rtnl_lock(); 792 netif_set_real_num_tx_queues(be->vif->dev, requested_num_queues); 793 netif_set_real_num_rx_queues(be->vif->dev, requested_num_queues); 794 rtnl_unlock(); 795 796 xenvif_carrier_on(be->vif); 797 798 unregister_hotplug_status_watch(be); 799 err = xenbus_watch_pathfmt(dev, &be->hotplug_status_watch, 800 hotplug_status_changed, 801 "%s/%s", dev->nodename, "hotplug-status"); 802 if (!err) 803 be->have_hotplug_status_watch = 1; 804 805 netif_tx_wake_all_queues(be->vif->dev); 806 807 return; 808 809 err: 810 if (be->vif->num_queues > 0) 811 xenvif_disconnect_data(be->vif); /* Clean up existing queues */ 812 for (queue_index = 0; queue_index < be->vif->num_queues; ++queue_index) 813 xenvif_deinit_queue(&be->vif->queues[queue_index]); 814 vfree(be->vif->queues); 815 be->vif->queues = NULL; 816 be->vif->num_queues = 0; 817 xenvif_disconnect_ctrl(be->vif); 818 return; 819 } 820 821 822 static int connect_data_rings(struct backend_info *be, 823 struct xenvif_queue *queue) 824 { 825 struct xenbus_device *dev = be->dev; 826 unsigned int num_queues = queue->vif->num_queues; 827 unsigned long tx_ring_ref, rx_ring_ref; 828 unsigned int tx_evtchn, rx_evtchn; 829 int err; 830 char *xspath; 831 size_t xspathsize; 832 const size_t xenstore_path_ext_size = 11; /* sufficient for "/queue-NNN" */ 833 834 /* If the frontend requested 1 queue, or we have fallen back 835 * to single queue due to lack of frontend support for multi- 836 * queue, expect the remaining XenStore keys in the toplevel 837 * directory. Otherwise, expect them in a subdirectory called 838 * queue-N. 839 */ 840 if (num_queues == 1) { 841 xspath = kzalloc(strlen(dev->otherend) + 1, GFP_KERNEL); 842 if (!xspath) { 843 xenbus_dev_fatal(dev, -ENOMEM, 844 "reading ring references"); 845 return -ENOMEM; 846 } 847 strcpy(xspath, dev->otherend); 848 } else { 849 xspathsize = strlen(dev->otherend) + xenstore_path_ext_size; 850 xspath = kzalloc(xspathsize, GFP_KERNEL); 851 if (!xspath) { 852 xenbus_dev_fatal(dev, -ENOMEM, 853 "reading ring references"); 854 return -ENOMEM; 855 } 856 snprintf(xspath, xspathsize, "%s/queue-%u", dev->otherend, 857 queue->id); 858 } 859 860 err = xenbus_gather(XBT_NIL, xspath, 861 "tx-ring-ref", "%lu", &tx_ring_ref, 862 "rx-ring-ref", "%lu", &rx_ring_ref, NULL); 863 if (err) { 864 xenbus_dev_fatal(dev, err, 865 "reading %s/ring-ref", 866 xspath); 867 goto err; 868 } 869 870 /* Try split event channels first, then single event channel. */ 871 err = xenbus_gather(XBT_NIL, xspath, 872 "event-channel-tx", "%u", &tx_evtchn, 873 "event-channel-rx", "%u", &rx_evtchn, NULL); 874 if (err < 0) { 875 err = xenbus_scanf(XBT_NIL, xspath, 876 "event-channel", "%u", &tx_evtchn); 877 if (err < 0) { 878 xenbus_dev_fatal(dev, err, 879 "reading %s/event-channel(-tx/rx)", 880 xspath); 881 goto err; 882 } 883 rx_evtchn = tx_evtchn; 884 } 885 886 /* Map the shared frame, irq etc. */ 887 err = xenvif_connect_data(queue, tx_ring_ref, rx_ring_ref, 888 tx_evtchn, rx_evtchn); 889 if (err) { 890 xenbus_dev_fatal(dev, err, 891 "mapping shared-frames %lu/%lu port tx %u rx %u", 892 tx_ring_ref, rx_ring_ref, 893 tx_evtchn, rx_evtchn); 894 goto err; 895 } 896 897 err = 0; 898 err: /* Regular return falls through with err == 0 */ 899 kfree(xspath); 900 return err; 901 } 902 903 static int read_xenbus_vif_flags(struct backend_info *be) 904 { 905 struct xenvif *vif = be->vif; 906 struct xenbus_device *dev = be->dev; 907 unsigned int rx_copy; 908 int err; 909 910 err = xenbus_scanf(XBT_NIL, dev->otherend, "request-rx-copy", "%u", 911 &rx_copy); 912 if (err == -ENOENT) { 913 err = 0; 914 rx_copy = 0; 915 } 916 if (err < 0) { 917 xenbus_dev_fatal(dev, err, "reading %s/request-rx-copy", 918 dev->otherend); 919 return err; 920 } 921 if (!rx_copy) 922 return -EOPNOTSUPP; 923 924 if (!xenbus_read_unsigned(dev->otherend, "feature-rx-notify", 0)) { 925 /* - Reduce drain timeout to poll more frequently for 926 * Rx requests. 927 * - Disable Rx stall detection. 928 */ 929 be->vif->drain_timeout = msecs_to_jiffies(30); 930 be->vif->stall_timeout = 0; 931 } 932 933 vif->can_sg = !!xenbus_read_unsigned(dev->otherend, "feature-sg", 0); 934 935 vif->gso_mask = 0; 936 937 if (xenbus_read_unsigned(dev->otherend, "feature-gso-tcpv4", 0)) 938 vif->gso_mask |= GSO_BIT(TCPV4); 939 940 if (xenbus_read_unsigned(dev->otherend, "feature-gso-tcpv6", 0)) 941 vif->gso_mask |= GSO_BIT(TCPV6); 942 943 vif->ip_csum = !xenbus_read_unsigned(dev->otherend, 944 "feature-no-csum-offload", 0); 945 946 vif->ipv6_csum = !!xenbus_read_unsigned(dev->otherend, 947 "feature-ipv6-csum-offload", 0); 948 949 return 0; 950 } 951 952 static int netback_remove(struct xenbus_device *dev) 953 { 954 struct backend_info *be = dev_get_drvdata(&dev->dev); 955 956 set_backend_state(be, XenbusStateClosed); 957 958 unregister_hotplug_status_watch(be); 959 if (be->vif) { 960 kobject_uevent(&dev->dev.kobj, KOBJ_OFFLINE); 961 xen_unregister_watchers(be->vif); 962 xenbus_rm(XBT_NIL, dev->nodename, "hotplug-status"); 963 xenvif_free(be->vif); 964 be->vif = NULL; 965 } 966 kfree(be->hotplug_script); 967 kfree(be); 968 dev_set_drvdata(&dev->dev, NULL); 969 return 0; 970 } 971 972 /** 973 * Entry point to this code when a new device is created. Allocate the basic 974 * structures and switch to InitWait. 975 */ 976 static int netback_probe(struct xenbus_device *dev, 977 const struct xenbus_device_id *id) 978 { 979 const char *message; 980 struct xenbus_transaction xbt; 981 int err; 982 int sg; 983 const char *script; 984 struct backend_info *be = kzalloc(sizeof(*be), GFP_KERNEL); 985 986 if (!be) { 987 xenbus_dev_fatal(dev, -ENOMEM, 988 "allocating backend structure"); 989 return -ENOMEM; 990 } 991 992 be->dev = dev; 993 dev_set_drvdata(&dev->dev, be); 994 995 sg = 1; 996 997 do { 998 err = xenbus_transaction_start(&xbt); 999 if (err) { 1000 xenbus_dev_fatal(dev, err, "starting transaction"); 1001 goto fail; 1002 } 1003 1004 err = xenbus_printf(xbt, dev->nodename, "feature-sg", "%d", sg); 1005 if (err) { 1006 message = "writing feature-sg"; 1007 goto abort_transaction; 1008 } 1009 1010 err = xenbus_printf(xbt, dev->nodename, "feature-gso-tcpv4", 1011 "%d", sg); 1012 if (err) { 1013 message = "writing feature-gso-tcpv4"; 1014 goto abort_transaction; 1015 } 1016 1017 err = xenbus_printf(xbt, dev->nodename, "feature-gso-tcpv6", 1018 "%d", sg); 1019 if (err) { 1020 message = "writing feature-gso-tcpv6"; 1021 goto abort_transaction; 1022 } 1023 1024 /* We support partial checksum setup for IPv6 packets */ 1025 err = xenbus_printf(xbt, dev->nodename, 1026 "feature-ipv6-csum-offload", 1027 "%d", 1); 1028 if (err) { 1029 message = "writing feature-ipv6-csum-offload"; 1030 goto abort_transaction; 1031 } 1032 1033 /* We support rx-copy path. */ 1034 err = xenbus_printf(xbt, dev->nodename, 1035 "feature-rx-copy", "%d", 1); 1036 if (err) { 1037 message = "writing feature-rx-copy"; 1038 goto abort_transaction; 1039 } 1040 1041 /* We don't support rx-flip path (except old guests who 1042 * don't grok this feature flag). 1043 */ 1044 err = xenbus_printf(xbt, dev->nodename, 1045 "feature-rx-flip", "%d", 0); 1046 if (err) { 1047 message = "writing feature-rx-flip"; 1048 goto abort_transaction; 1049 } 1050 1051 /* We support dynamic multicast-control. */ 1052 err = xenbus_printf(xbt, dev->nodename, 1053 "feature-multicast-control", "%d", 1); 1054 if (err) { 1055 message = "writing feature-multicast-control"; 1056 goto abort_transaction; 1057 } 1058 1059 err = xenbus_printf(xbt, dev->nodename, 1060 "feature-dynamic-multicast-control", 1061 "%d", 1); 1062 if (err) { 1063 message = "writing feature-dynamic-multicast-control"; 1064 goto abort_transaction; 1065 } 1066 1067 err = xenbus_transaction_end(xbt, 0); 1068 } while (err == -EAGAIN); 1069 1070 if (err) { 1071 xenbus_dev_fatal(dev, err, "completing transaction"); 1072 goto fail; 1073 } 1074 1075 /* Split event channels support, this is optional so it is not 1076 * put inside the above loop. 1077 */ 1078 err = xenbus_printf(XBT_NIL, dev->nodename, 1079 "feature-split-event-channels", 1080 "%u", separate_tx_rx_irq); 1081 if (err) 1082 pr_debug("Error writing feature-split-event-channels\n"); 1083 1084 /* Multi-queue support: This is an optional feature. */ 1085 err = xenbus_printf(XBT_NIL, dev->nodename, 1086 "multi-queue-max-queues", "%u", xenvif_max_queues); 1087 if (err) 1088 pr_debug("Error writing multi-queue-max-queues\n"); 1089 1090 err = xenbus_printf(XBT_NIL, dev->nodename, 1091 "feature-ctrl-ring", 1092 "%u", true); 1093 if (err) 1094 pr_debug("Error writing feature-ctrl-ring\n"); 1095 1096 backend_switch_state(be, XenbusStateInitWait); 1097 1098 script = xenbus_read(XBT_NIL, dev->nodename, "script", NULL); 1099 if (IS_ERR(script)) { 1100 err = PTR_ERR(script); 1101 xenbus_dev_fatal(dev, err, "reading script"); 1102 goto fail; 1103 } 1104 1105 be->hotplug_script = script; 1106 1107 /* This kicks hotplug scripts, so do it immediately. */ 1108 err = backend_create_xenvif(be); 1109 if (err) 1110 goto fail; 1111 1112 return 0; 1113 1114 abort_transaction: 1115 xenbus_transaction_end(xbt, 1); 1116 xenbus_dev_fatal(dev, err, "%s", message); 1117 fail: 1118 pr_debug("failed\n"); 1119 netback_remove(dev); 1120 return err; 1121 } 1122 1123 static const struct xenbus_device_id netback_ids[] = { 1124 { "vif" }, 1125 { "" } 1126 }; 1127 1128 static struct xenbus_driver netback_driver = { 1129 .ids = netback_ids, 1130 .probe = netback_probe, 1131 .remove = netback_remove, 1132 .uevent = netback_uevent, 1133 .otherend_changed = frontend_changed, 1134 }; 1135 1136 int xenvif_xenbus_init(void) 1137 { 1138 return xenbus_register_backend(&netback_driver); 1139 } 1140 1141 void xenvif_xenbus_fini(void) 1142 { 1143 return xenbus_unregister_driver(&netback_driver); 1144 } 1145