1 /*- 2 * Copyright (c) 2004-2006 Kip Macy 3 * Copyright (c) 2015 Wei Liu <wei.liu2@citrix.com> 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 */ 27 28 #include <sys/cdefs.h> 29 __FBSDID("$FreeBSD$"); 30 31 #include "opt_inet.h" 32 #include "opt_inet6.h" 33 34 #include <sys/param.h> 35 #include <sys/sockio.h> 36 #include <sys/limits.h> 37 #include <sys/mbuf.h> 38 #include <sys/malloc.h> 39 #include <sys/module.h> 40 #include <sys/kernel.h> 41 #include <sys/socket.h> 42 #include <sys/sysctl.h> 43 #include <sys/taskqueue.h> 44 45 #include <net/if.h> 46 #include <net/if_var.h> 47 #include <net/if_arp.h> 48 #include <net/ethernet.h> 49 #include <net/if_media.h> 50 #include <net/bpf.h> 51 #include <net/if_types.h> 52 53 #include <netinet/in.h> 54 #include <netinet/ip.h> 55 #include <netinet/if_ether.h> 56 #include <netinet/tcp.h> 57 #include <netinet/tcp_lro.h> 58 59 #include <vm/vm.h> 60 #include <vm/pmap.h> 61 62 #include <sys/bus.h> 63 64 #include <xen/xen-os.h> 65 #include <xen/hypervisor.h> 66 #include <xen/xen_intr.h> 67 #include <xen/gnttab.h> 68 #include <xen/interface/memory.h> 69 #include <xen/interface/io/netif.h> 70 #include <xen/xenbus/xenbusvar.h> 71 72 #include "xenbus_if.h" 73 74 /* Features supported by all backends. TSO and LRO can be negotiated */ 75 #define XN_CSUM_FEATURES (CSUM_TCP | CSUM_UDP) 76 77 #define NET_TX_RING_SIZE __RING_SIZE((netif_tx_sring_t *)0, PAGE_SIZE) 78 #define NET_RX_RING_SIZE __RING_SIZE((netif_rx_sring_t *)0, PAGE_SIZE) 79 80 #define NET_RX_SLOTS_MIN (XEN_NETIF_NR_SLOTS_MIN + 1) 81 82 /* 83 * Should the driver do LRO on the RX end 84 * this can be toggled on the fly, but the 85 * interface must be reset (down/up) for it 86 * to take effect. 87 */ 88 static int xn_enable_lro = 1; 89 TUNABLE_INT("hw.xn.enable_lro", &xn_enable_lro); 90 91 /* 92 * Number of pairs of queues. 93 */ 94 static unsigned long xn_num_queues = 4; 95 TUNABLE_ULONG("hw.xn.num_queues", &xn_num_queues); 96 97 /** 98 * \brief The maximum allowed data fragments in a single transmit 99 * request. 100 * 101 * This limit is imposed by the backend driver. We assume here that 102 * we are dealing with a Linux driver domain and have set our limit 103 * to mirror the Linux MAX_SKB_FRAGS constant. 104 */ 105 #define MAX_TX_REQ_FRAGS (65536 / PAGE_SIZE + 2) 106 107 #define RX_COPY_THRESHOLD 256 108 109 #define net_ratelimit() 0 110 111 struct netfront_rxq; 112 struct netfront_txq; 113 struct netfront_info; 114 struct netfront_rx_info; 115 116 static void xn_txeof(struct netfront_txq *); 117 static void xn_rxeof(struct netfront_rxq *); 118 static void xn_alloc_rx_buffers(struct netfront_rxq *); 119 static void xn_alloc_rx_buffers_callout(void *arg); 120 121 static void xn_release_rx_bufs(struct netfront_rxq *); 122 static void xn_release_tx_bufs(struct netfront_txq *); 123 124 static void xn_rxq_intr(struct netfront_rxq *); 125 static void xn_txq_intr(struct netfront_txq *); 126 static void xn_intr(void *); 127 static inline int xn_count_frags(struct mbuf *m); 128 static int xn_assemble_tx_request(struct netfront_txq *, struct mbuf *); 129 static int xn_ioctl(struct ifnet *, u_long, caddr_t); 130 static void xn_ifinit_locked(struct netfront_info *); 131 static void xn_ifinit(void *); 132 static void xn_stop(struct netfront_info *); 133 static void xn_query_features(struct netfront_info *np); 134 static int xn_configure_features(struct netfront_info *np); 135 static void netif_free(struct netfront_info *info); 136 static int netfront_detach(device_t dev); 137 138 static int xn_txq_mq_start_locked(struct netfront_txq *, struct mbuf *); 139 static int xn_txq_mq_start(struct ifnet *, struct mbuf *); 140 141 static int talk_to_backend(device_t dev, struct netfront_info *info); 142 static int create_netdev(device_t dev); 143 static void netif_disconnect_backend(struct netfront_info *info); 144 static int setup_device(device_t dev, struct netfront_info *info, 145 unsigned long); 146 static int xn_ifmedia_upd(struct ifnet *ifp); 147 static void xn_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr); 148 149 static int xn_connect(struct netfront_info *); 150 static void xn_kick_rings(struct netfront_info *); 151 152 static int xn_get_responses(struct netfront_rxq *, 153 struct netfront_rx_info *, RING_IDX, RING_IDX *, 154 struct mbuf **); 155 156 #define virt_to_mfn(x) (vtophys(x) >> PAGE_SHIFT) 157 158 #define INVALID_P2M_ENTRY (~0UL) 159 #define XN_QUEUE_NAME_LEN 8 /* xn{t,r}x_%u, allow for two digits */ 160 struct netfront_rxq { 161 struct netfront_info *info; 162 u_int id; 163 char name[XN_QUEUE_NAME_LEN]; 164 struct mtx lock; 165 166 int ring_ref; 167 netif_rx_front_ring_t ring; 168 xen_intr_handle_t xen_intr_handle; 169 170 grant_ref_t gref_head; 171 grant_ref_t grant_ref[NET_RX_RING_SIZE + 1]; 172 173 struct mbuf *mbufs[NET_RX_RING_SIZE + 1]; 174 175 struct lro_ctrl lro; 176 177 struct callout rx_refill; 178 }; 179 180 struct netfront_txq { 181 struct netfront_info *info; 182 u_int id; 183 char name[XN_QUEUE_NAME_LEN]; 184 struct mtx lock; 185 186 int ring_ref; 187 netif_tx_front_ring_t ring; 188 xen_intr_handle_t xen_intr_handle; 189 190 grant_ref_t gref_head; 191 grant_ref_t grant_ref[NET_TX_RING_SIZE + 1]; 192 193 struct mbuf *mbufs[NET_TX_RING_SIZE + 1]; 194 int mbufs_cnt; 195 struct buf_ring *br; 196 197 struct taskqueue *tq; 198 struct task defrtask; 199 200 bool full; 201 }; 202 203 struct netfront_info { 204 struct ifnet *xn_ifp; 205 206 struct mtx sc_lock; 207 208 u_int num_queues; 209 struct netfront_rxq *rxq; 210 struct netfront_txq *txq; 211 212 u_int carrier; 213 u_int maxfrags; 214 215 device_t xbdev; 216 uint8_t mac[ETHER_ADDR_LEN]; 217 218 int xn_if_flags; 219 220 struct ifmedia sc_media; 221 222 bool xn_reset; 223 }; 224 225 struct netfront_rx_info { 226 struct netif_rx_response rx; 227 struct netif_extra_info extras[XEN_NETIF_EXTRA_TYPE_MAX - 1]; 228 }; 229 230 #define XN_RX_LOCK(_q) mtx_lock(&(_q)->lock) 231 #define XN_RX_UNLOCK(_q) mtx_unlock(&(_q)->lock) 232 233 #define XN_TX_LOCK(_q) mtx_lock(&(_q)->lock) 234 #define XN_TX_TRYLOCK(_q) mtx_trylock(&(_q)->lock) 235 #define XN_TX_UNLOCK(_q) mtx_unlock(&(_q)->lock) 236 237 #define XN_LOCK(_sc) mtx_lock(&(_sc)->sc_lock); 238 #define XN_UNLOCK(_sc) mtx_unlock(&(_sc)->sc_lock); 239 240 #define XN_LOCK_ASSERT(_sc) mtx_assert(&(_sc)->sc_lock, MA_OWNED); 241 #define XN_RX_LOCK_ASSERT(_q) mtx_assert(&(_q)->lock, MA_OWNED); 242 #define XN_TX_LOCK_ASSERT(_q) mtx_assert(&(_q)->lock, MA_OWNED); 243 244 #define netfront_carrier_on(netif) ((netif)->carrier = 1) 245 #define netfront_carrier_off(netif) ((netif)->carrier = 0) 246 #define netfront_carrier_ok(netif) ((netif)->carrier) 247 248 /* Access macros for acquiring freeing slots in xn_free_{tx,rx}_idxs[]. */ 249 250 static inline void 251 add_id_to_freelist(struct mbuf **list, uintptr_t id) 252 { 253 254 KASSERT(id != 0, 255 ("%s: the head item (0) must always be free.", __func__)); 256 list[id] = list[0]; 257 list[0] = (struct mbuf *)id; 258 } 259 260 static inline unsigned short 261 get_id_from_freelist(struct mbuf **list) 262 { 263 uintptr_t id; 264 265 id = (uintptr_t)list[0]; 266 KASSERT(id != 0, 267 ("%s: the head item (0) must always remain free.", __func__)); 268 list[0] = list[id]; 269 return (id); 270 } 271 272 static inline int 273 xn_rxidx(RING_IDX idx) 274 { 275 276 return idx & (NET_RX_RING_SIZE - 1); 277 } 278 279 static inline struct mbuf * 280 xn_get_rx_mbuf(struct netfront_rxq *rxq, RING_IDX ri) 281 { 282 int i; 283 struct mbuf *m; 284 285 i = xn_rxidx(ri); 286 m = rxq->mbufs[i]; 287 rxq->mbufs[i] = NULL; 288 return (m); 289 } 290 291 static inline grant_ref_t 292 xn_get_rx_ref(struct netfront_rxq *rxq, RING_IDX ri) 293 { 294 int i = xn_rxidx(ri); 295 grant_ref_t ref = rxq->grant_ref[i]; 296 297 KASSERT(ref != GRANT_REF_INVALID, ("Invalid grant reference!\n")); 298 rxq->grant_ref[i] = GRANT_REF_INVALID; 299 return (ref); 300 } 301 302 #define IPRINTK(fmt, args...) \ 303 printf("[XEN] " fmt, ##args) 304 #ifdef INVARIANTS 305 #define WPRINTK(fmt, args...) \ 306 printf("[XEN] " fmt, ##args) 307 #else 308 #define WPRINTK(fmt, args...) 309 #endif 310 #ifdef DEBUG 311 #define DPRINTK(fmt, args...) \ 312 printf("[XEN] %s: " fmt, __func__, ##args) 313 #else 314 #define DPRINTK(fmt, args...) 315 #endif 316 317 /** 318 * Read the 'mac' node at the given device's node in the store, and parse that 319 * as colon-separated octets, placing result the given mac array. mac must be 320 * a preallocated array of length ETH_ALEN (as declared in linux/if_ether.h). 321 * Return 0 on success, or errno on error. 322 */ 323 static int 324 xen_net_read_mac(device_t dev, uint8_t mac[]) 325 { 326 int error, i; 327 char *s, *e, *macstr; 328 const char *path; 329 330 path = xenbus_get_node(dev); 331 error = xs_read(XST_NIL, path, "mac", NULL, (void **) &macstr); 332 if (error == ENOENT) { 333 /* 334 * Deal with missing mac XenStore nodes on devices with 335 * HVM emulation (the 'ioemu' configuration attribute) 336 * enabled. 337 * 338 * The HVM emulator may execute in a stub device model 339 * domain which lacks the permission, only given to Dom0, 340 * to update the guest's XenStore tree. For this reason, 341 * the HVM emulator doesn't even attempt to write the 342 * front-side mac node, even when operating in Dom0. 343 * However, there should always be a mac listed in the 344 * backend tree. Fallback to this version if our query 345 * of the front side XenStore location doesn't find 346 * anything. 347 */ 348 path = xenbus_get_otherend_path(dev); 349 error = xs_read(XST_NIL, path, "mac", NULL, (void **) &macstr); 350 } 351 if (error != 0) { 352 xenbus_dev_fatal(dev, error, "parsing %s/mac", path); 353 return (error); 354 } 355 356 s = macstr; 357 for (i = 0; i < ETHER_ADDR_LEN; i++) { 358 mac[i] = strtoul(s, &e, 16); 359 if (s == e || (e[0] != ':' && e[0] != 0)) { 360 free(macstr, M_XENBUS); 361 return (ENOENT); 362 } 363 s = &e[1]; 364 } 365 free(macstr, M_XENBUS); 366 return (0); 367 } 368 369 /** 370 * Entry point to this code when a new device is created. Allocate the basic 371 * structures and the ring buffers for communication with the backend, and 372 * inform the backend of the appropriate details for those. Switch to 373 * Connected state. 374 */ 375 static int 376 netfront_probe(device_t dev) 377 { 378 379 if (xen_hvm_domain() && xen_disable_pv_nics != 0) 380 return (ENXIO); 381 382 if (!strcmp(xenbus_get_type(dev), "vif")) { 383 device_set_desc(dev, "Virtual Network Interface"); 384 return (0); 385 } 386 387 return (ENXIO); 388 } 389 390 static int 391 netfront_attach(device_t dev) 392 { 393 int err; 394 395 err = create_netdev(dev); 396 if (err != 0) { 397 xenbus_dev_fatal(dev, err, "creating netdev"); 398 return (err); 399 } 400 401 SYSCTL_ADD_INT(device_get_sysctl_ctx(dev), 402 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), 403 OID_AUTO, "enable_lro", CTLFLAG_RW, 404 &xn_enable_lro, 0, "Large Receive Offload"); 405 406 SYSCTL_ADD_ULONG(device_get_sysctl_ctx(dev), 407 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), 408 OID_AUTO, "num_queues", CTLFLAG_RD, 409 &xn_num_queues, "Number of pairs of queues"); 410 411 return (0); 412 } 413 414 static int 415 netfront_suspend(device_t dev) 416 { 417 struct netfront_info *np = device_get_softc(dev); 418 u_int i; 419 420 for (i = 0; i < np->num_queues; i++) { 421 XN_RX_LOCK(&np->rxq[i]); 422 XN_TX_LOCK(&np->txq[i]); 423 } 424 netfront_carrier_off(np); 425 for (i = 0; i < np->num_queues; i++) { 426 XN_RX_UNLOCK(&np->rxq[i]); 427 XN_TX_UNLOCK(&np->txq[i]); 428 } 429 return (0); 430 } 431 432 /** 433 * We are reconnecting to the backend, due to a suspend/resume, or a backend 434 * driver restart. We tear down our netif structure and recreate it, but 435 * leave the device-layer structures intact so that this is transparent to the 436 * rest of the kernel. 437 */ 438 static int 439 netfront_resume(device_t dev) 440 { 441 struct netfront_info *info = device_get_softc(dev); 442 u_int i; 443 444 if (xen_suspend_cancelled) { 445 for (i = 0; i < info->num_queues; i++) { 446 XN_RX_LOCK(&info->rxq[i]); 447 XN_TX_LOCK(&info->txq[i]); 448 } 449 netfront_carrier_on(info); 450 for (i = 0; i < info->num_queues; i++) { 451 XN_RX_UNLOCK(&info->rxq[i]); 452 XN_TX_UNLOCK(&info->txq[i]); 453 } 454 return (0); 455 } 456 457 netif_disconnect_backend(info); 458 return (0); 459 } 460 461 static int 462 write_queue_xenstore_keys(device_t dev, 463 struct netfront_rxq *rxq, 464 struct netfront_txq *txq, 465 struct xs_transaction *xst, bool hierarchy) 466 { 467 int err; 468 const char *message; 469 const char *node = xenbus_get_node(dev); 470 char *path; 471 size_t path_size; 472 473 KASSERT(rxq->id == txq->id, ("Mismatch between RX and TX queue ids")); 474 /* Split event channel support is not yet there. */ 475 KASSERT(rxq->xen_intr_handle == txq->xen_intr_handle, 476 ("Split event channels are not supported")); 477 478 if (hierarchy) { 479 path_size = strlen(node) + 10; 480 path = malloc(path_size, M_DEVBUF, M_WAITOK|M_ZERO); 481 snprintf(path, path_size, "%s/queue-%u", node, rxq->id); 482 } else { 483 path_size = strlen(node) + 1; 484 path = malloc(path_size, M_DEVBUF, M_WAITOK|M_ZERO); 485 snprintf(path, path_size, "%s", node); 486 } 487 488 err = xs_printf(*xst, path, "tx-ring-ref","%u", txq->ring_ref); 489 if (err != 0) { 490 message = "writing tx ring-ref"; 491 goto error; 492 } 493 err = xs_printf(*xst, path, "rx-ring-ref","%u", rxq->ring_ref); 494 if (err != 0) { 495 message = "writing rx ring-ref"; 496 goto error; 497 } 498 err = xs_printf(*xst, path, "event-channel", "%u", 499 xen_intr_port(rxq->xen_intr_handle)); 500 if (err != 0) { 501 message = "writing event-channel"; 502 goto error; 503 } 504 505 free(path, M_DEVBUF); 506 507 return (0); 508 509 error: 510 free(path, M_DEVBUF); 511 xenbus_dev_fatal(dev, err, "%s", message); 512 513 return (err); 514 } 515 516 /* Common code used when first setting up, and when resuming. */ 517 static int 518 talk_to_backend(device_t dev, struct netfront_info *info) 519 { 520 const char *message; 521 struct xs_transaction xst; 522 const char *node = xenbus_get_node(dev); 523 int err; 524 unsigned long num_queues, max_queues = 0; 525 unsigned int i; 526 527 err = xen_net_read_mac(dev, info->mac); 528 if (err != 0) { 529 xenbus_dev_fatal(dev, err, "parsing %s/mac", node); 530 goto out; 531 } 532 533 err = xs_scanf(XST_NIL, xenbus_get_otherend_path(info->xbdev), 534 "multi-queue-max-queues", NULL, "%lu", &max_queues); 535 if (err != 0) 536 max_queues = 1; 537 num_queues = xn_num_queues; 538 if (num_queues > max_queues) 539 num_queues = max_queues; 540 541 err = setup_device(dev, info, num_queues); 542 if (err != 0) 543 goto out; 544 545 again: 546 err = xs_transaction_start(&xst); 547 if (err != 0) { 548 xenbus_dev_fatal(dev, err, "starting transaction"); 549 goto free; 550 } 551 552 if (info->num_queues == 1) { 553 err = write_queue_xenstore_keys(dev, &info->rxq[0], 554 &info->txq[0], &xst, false); 555 if (err != 0) 556 goto abort_transaction_no_def_error; 557 } else { 558 err = xs_printf(xst, node, "multi-queue-num-queues", 559 "%u", info->num_queues); 560 if (err != 0) { 561 message = "writing multi-queue-num-queues"; 562 goto abort_transaction; 563 } 564 565 for (i = 0; i < info->num_queues; i++) { 566 err = write_queue_xenstore_keys(dev, &info->rxq[i], 567 &info->txq[i], &xst, true); 568 if (err != 0) 569 goto abort_transaction_no_def_error; 570 } 571 } 572 573 err = xs_printf(xst, node, "request-rx-copy", "%u", 1); 574 if (err != 0) { 575 message = "writing request-rx-copy"; 576 goto abort_transaction; 577 } 578 err = xs_printf(xst, node, "feature-rx-notify", "%d", 1); 579 if (err != 0) { 580 message = "writing feature-rx-notify"; 581 goto abort_transaction; 582 } 583 err = xs_printf(xst, node, "feature-sg", "%d", 1); 584 if (err != 0) { 585 message = "writing feature-sg"; 586 goto abort_transaction; 587 } 588 if ((info->xn_ifp->if_capenable & IFCAP_LRO) != 0) { 589 err = xs_printf(xst, node, "feature-gso-tcpv4", "%d", 1); 590 if (err != 0) { 591 message = "writing feature-gso-tcpv4"; 592 goto abort_transaction; 593 } 594 } 595 if ((info->xn_ifp->if_capenable & IFCAP_RXCSUM) == 0) { 596 err = xs_printf(xst, node, "feature-no-csum-offload", "%d", 1); 597 if (err != 0) { 598 message = "writing feature-no-csum-offload"; 599 goto abort_transaction; 600 } 601 } 602 603 err = xs_transaction_end(xst, 0); 604 if (err != 0) { 605 if (err == EAGAIN) 606 goto again; 607 xenbus_dev_fatal(dev, err, "completing transaction"); 608 goto free; 609 } 610 611 return 0; 612 613 abort_transaction: 614 xenbus_dev_fatal(dev, err, "%s", message); 615 abort_transaction_no_def_error: 616 xs_transaction_end(xst, 1); 617 free: 618 netif_free(info); 619 out: 620 return (err); 621 } 622 623 static void 624 xn_rxq_intr(struct netfront_rxq *rxq) 625 { 626 627 XN_RX_LOCK(rxq); 628 xn_rxeof(rxq); 629 XN_RX_UNLOCK(rxq); 630 } 631 632 static void 633 xn_txq_start(struct netfront_txq *txq) 634 { 635 struct netfront_info *np = txq->info; 636 struct ifnet *ifp = np->xn_ifp; 637 638 XN_TX_LOCK_ASSERT(txq); 639 if (!drbr_empty(ifp, txq->br)) 640 xn_txq_mq_start_locked(txq, NULL); 641 } 642 643 static void 644 xn_txq_intr(struct netfront_txq *txq) 645 { 646 647 XN_TX_LOCK(txq); 648 if (RING_HAS_UNCONSUMED_RESPONSES(&txq->ring)) 649 xn_txeof(txq); 650 xn_txq_start(txq); 651 XN_TX_UNLOCK(txq); 652 } 653 654 static void 655 xn_txq_tq_deferred(void *xtxq, int pending) 656 { 657 struct netfront_txq *txq = xtxq; 658 659 XN_TX_LOCK(txq); 660 xn_txq_start(txq); 661 XN_TX_UNLOCK(txq); 662 } 663 664 static void 665 disconnect_rxq(struct netfront_rxq *rxq) 666 { 667 668 xn_release_rx_bufs(rxq); 669 gnttab_free_grant_references(rxq->gref_head); 670 gnttab_end_foreign_access(rxq->ring_ref, NULL); 671 /* 672 * No split event channel support at the moment, handle will 673 * be unbound in tx. So no need to call xen_intr_unbind here, 674 * but we do want to reset the handler to 0. 675 */ 676 rxq->xen_intr_handle = 0; 677 } 678 679 static void 680 destroy_rxq(struct netfront_rxq *rxq) 681 { 682 683 callout_drain(&rxq->rx_refill); 684 free(rxq->ring.sring, M_DEVBUF); 685 } 686 687 static void 688 destroy_rxqs(struct netfront_info *np) 689 { 690 int i; 691 692 for (i = 0; i < np->num_queues; i++) 693 destroy_rxq(&np->rxq[i]); 694 695 free(np->rxq, M_DEVBUF); 696 np->rxq = NULL; 697 } 698 699 static int 700 setup_rxqs(device_t dev, struct netfront_info *info, 701 unsigned long num_queues) 702 { 703 int q, i; 704 int error; 705 netif_rx_sring_t *rxs; 706 struct netfront_rxq *rxq; 707 708 info->rxq = malloc(sizeof(struct netfront_rxq) * num_queues, 709 M_DEVBUF, M_WAITOK|M_ZERO); 710 711 for (q = 0; q < num_queues; q++) { 712 rxq = &info->rxq[q]; 713 714 rxq->id = q; 715 rxq->info = info; 716 rxq->ring_ref = GRANT_REF_INVALID; 717 rxq->ring.sring = NULL; 718 snprintf(rxq->name, XN_QUEUE_NAME_LEN, "xnrx_%u", q); 719 mtx_init(&rxq->lock, rxq->name, "netfront receive lock", 720 MTX_DEF); 721 722 for (i = 0; i <= NET_RX_RING_SIZE; i++) { 723 rxq->mbufs[i] = NULL; 724 rxq->grant_ref[i] = GRANT_REF_INVALID; 725 } 726 727 /* Start resources allocation */ 728 729 if (gnttab_alloc_grant_references(NET_RX_RING_SIZE, 730 &rxq->gref_head) != 0) { 731 device_printf(dev, "allocating rx gref"); 732 error = ENOMEM; 733 goto fail; 734 } 735 736 rxs = (netif_rx_sring_t *)malloc(PAGE_SIZE, M_DEVBUF, 737 M_WAITOK|M_ZERO); 738 SHARED_RING_INIT(rxs); 739 FRONT_RING_INIT(&rxq->ring, rxs, PAGE_SIZE); 740 741 error = xenbus_grant_ring(dev, virt_to_mfn(rxs), 742 &rxq->ring_ref); 743 if (error != 0) { 744 device_printf(dev, "granting rx ring page"); 745 goto fail_grant_ring; 746 } 747 748 callout_init(&rxq->rx_refill, 1); 749 } 750 751 return (0); 752 753 fail_grant_ring: 754 gnttab_free_grant_references(rxq->gref_head); 755 free(rxq->ring.sring, M_DEVBUF); 756 fail: 757 for (; q >= 0; q--) { 758 disconnect_rxq(&info->rxq[q]); 759 destroy_rxq(&info->rxq[q]); 760 } 761 762 free(info->rxq, M_DEVBUF); 763 return (error); 764 } 765 766 static void 767 disconnect_txq(struct netfront_txq *txq) 768 { 769 770 xn_release_tx_bufs(txq); 771 gnttab_free_grant_references(txq->gref_head); 772 gnttab_end_foreign_access(txq->ring_ref, NULL); 773 xen_intr_unbind(&txq->xen_intr_handle); 774 } 775 776 static void 777 destroy_txq(struct netfront_txq *txq) 778 { 779 780 free(txq->ring.sring, M_DEVBUF); 781 buf_ring_free(txq->br, M_DEVBUF); 782 taskqueue_drain_all(txq->tq); 783 taskqueue_free(txq->tq); 784 } 785 786 static void 787 destroy_txqs(struct netfront_info *np) 788 { 789 int i; 790 791 for (i = 0; i < np->num_queues; i++) 792 destroy_txq(&np->txq[i]); 793 794 free(np->txq, M_DEVBUF); 795 np->txq = NULL; 796 } 797 798 static int 799 setup_txqs(device_t dev, struct netfront_info *info, 800 unsigned long num_queues) 801 { 802 int q, i; 803 int error; 804 netif_tx_sring_t *txs; 805 struct netfront_txq *txq; 806 807 info->txq = malloc(sizeof(struct netfront_txq) * num_queues, 808 M_DEVBUF, M_WAITOK|M_ZERO); 809 810 for (q = 0; q < num_queues; q++) { 811 txq = &info->txq[q]; 812 813 txq->id = q; 814 txq->info = info; 815 816 txq->ring_ref = GRANT_REF_INVALID; 817 txq->ring.sring = NULL; 818 819 snprintf(txq->name, XN_QUEUE_NAME_LEN, "xntx_%u", q); 820 821 mtx_init(&txq->lock, txq->name, "netfront transmit lock", 822 MTX_DEF); 823 824 for (i = 0; i <= NET_TX_RING_SIZE; i++) { 825 txq->mbufs[i] = (void *) ((u_long) i+1); 826 txq->grant_ref[i] = GRANT_REF_INVALID; 827 } 828 txq->mbufs[NET_TX_RING_SIZE] = (void *)0; 829 830 /* Start resources allocation. */ 831 832 if (gnttab_alloc_grant_references(NET_TX_RING_SIZE, 833 &txq->gref_head) != 0) { 834 device_printf(dev, "failed to allocate tx grant refs\n"); 835 error = ENOMEM; 836 goto fail; 837 } 838 839 txs = (netif_tx_sring_t *)malloc(PAGE_SIZE, M_DEVBUF, 840 M_WAITOK|M_ZERO); 841 SHARED_RING_INIT(txs); 842 FRONT_RING_INIT(&txq->ring, txs, PAGE_SIZE); 843 844 error = xenbus_grant_ring(dev, virt_to_mfn(txs), 845 &txq->ring_ref); 846 if (error != 0) { 847 device_printf(dev, "failed to grant tx ring\n"); 848 goto fail_grant_ring; 849 } 850 851 txq->br = buf_ring_alloc(NET_TX_RING_SIZE, M_DEVBUF, 852 M_WAITOK, &txq->lock); 853 TASK_INIT(&txq->defrtask, 0, xn_txq_tq_deferred, txq); 854 855 txq->tq = taskqueue_create(txq->name, M_WAITOK, 856 taskqueue_thread_enqueue, &txq->tq); 857 858 error = taskqueue_start_threads(&txq->tq, 1, PI_NET, 859 "%s txq %d", device_get_nameunit(dev), txq->id); 860 if (error != 0) { 861 device_printf(dev, "failed to start tx taskq %d\n", 862 txq->id); 863 goto fail_start_thread; 864 } 865 866 error = xen_intr_alloc_and_bind_local_port(dev, 867 xenbus_get_otherend_id(dev), /* filter */ NULL, xn_intr, 868 &info->txq[q], INTR_TYPE_NET | INTR_MPSAFE | INTR_ENTROPY, 869 &txq->xen_intr_handle); 870 871 if (error != 0) { 872 device_printf(dev, "xen_intr_alloc_and_bind_local_port failed\n"); 873 goto fail_bind_port; 874 } 875 } 876 877 return (0); 878 879 fail_bind_port: 880 taskqueue_drain_all(txq->tq); 881 fail_start_thread: 882 buf_ring_free(txq->br, M_DEVBUF); 883 taskqueue_free(txq->tq); 884 gnttab_end_foreign_access(txq->ring_ref, NULL); 885 fail_grant_ring: 886 gnttab_free_grant_references(txq->gref_head); 887 free(txq->ring.sring, M_DEVBUF); 888 fail: 889 for (; q >= 0; q--) { 890 disconnect_txq(&info->txq[q]); 891 destroy_txq(&info->txq[q]); 892 } 893 894 free(info->txq, M_DEVBUF); 895 return (error); 896 } 897 898 static int 899 setup_device(device_t dev, struct netfront_info *info, 900 unsigned long num_queues) 901 { 902 int error; 903 int q; 904 905 if (info->txq) 906 destroy_txqs(info); 907 908 if (info->rxq) 909 destroy_rxqs(info); 910 911 info->num_queues = 0; 912 913 error = setup_rxqs(dev, info, num_queues); 914 if (error != 0) 915 goto out; 916 error = setup_txqs(dev, info, num_queues); 917 if (error != 0) 918 goto out; 919 920 info->num_queues = num_queues; 921 922 /* No split event channel at the moment. */ 923 for (q = 0; q < num_queues; q++) 924 info->rxq[q].xen_intr_handle = info->txq[q].xen_intr_handle; 925 926 return (0); 927 928 out: 929 KASSERT(error != 0, ("Error path taken without providing an error code")); 930 return (error); 931 } 932 933 #ifdef INET 934 /** 935 * If this interface has an ipv4 address, send an arp for it. This 936 * helps to get the network going again after migrating hosts. 937 */ 938 static void 939 netfront_send_fake_arp(device_t dev, struct netfront_info *info) 940 { 941 struct ifnet *ifp; 942 struct ifaddr *ifa; 943 944 ifp = info->xn_ifp; 945 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 946 if (ifa->ifa_addr->sa_family == AF_INET) { 947 arp_ifinit(ifp, ifa); 948 } 949 } 950 } 951 #endif 952 953 /** 954 * Callback received when the backend's state changes. 955 */ 956 static void 957 netfront_backend_changed(device_t dev, XenbusState newstate) 958 { 959 struct netfront_info *sc = device_get_softc(dev); 960 961 DPRINTK("newstate=%d\n", newstate); 962 963 switch (newstate) { 964 case XenbusStateInitialising: 965 case XenbusStateInitialised: 966 case XenbusStateUnknown: 967 case XenbusStateReconfigured: 968 case XenbusStateReconfiguring: 969 break; 970 case XenbusStateInitWait: 971 if (xenbus_get_state(dev) != XenbusStateInitialising) 972 break; 973 if (xn_connect(sc) != 0) 974 break; 975 /* Switch to connected state before kicking the rings. */ 976 xenbus_set_state(sc->xbdev, XenbusStateConnected); 977 xn_kick_rings(sc); 978 break; 979 case XenbusStateClosing: 980 xenbus_set_state(dev, XenbusStateClosed); 981 break; 982 case XenbusStateClosed: 983 if (sc->xn_reset) { 984 netif_disconnect_backend(sc); 985 xenbus_set_state(dev, XenbusStateInitialising); 986 sc->xn_reset = false; 987 } 988 break; 989 case XenbusStateConnected: 990 #ifdef INET 991 netfront_send_fake_arp(dev, sc); 992 #endif 993 break; 994 } 995 } 996 997 /** 998 * \brief Verify that there is sufficient space in the Tx ring 999 * buffer for a maximally sized request to be enqueued. 1000 * 1001 * A transmit request requires a transmit descriptor for each packet 1002 * fragment, plus up to 2 entries for "options" (e.g. TSO). 1003 */ 1004 static inline int 1005 xn_tx_slot_available(struct netfront_txq *txq) 1006 { 1007 1008 return (RING_FREE_REQUESTS(&txq->ring) > (MAX_TX_REQ_FRAGS + 2)); 1009 } 1010 1011 static void 1012 xn_release_tx_bufs(struct netfront_txq *txq) 1013 { 1014 int i; 1015 1016 for (i = 1; i <= NET_TX_RING_SIZE; i++) { 1017 struct mbuf *m; 1018 1019 m = txq->mbufs[i]; 1020 1021 /* 1022 * We assume that no kernel addresses are 1023 * less than NET_TX_RING_SIZE. Any entry 1024 * in the table that is below this number 1025 * must be an index from free-list tracking. 1026 */ 1027 if (((uintptr_t)m) <= NET_TX_RING_SIZE) 1028 continue; 1029 gnttab_end_foreign_access_ref(txq->grant_ref[i]); 1030 gnttab_release_grant_reference(&txq->gref_head, 1031 txq->grant_ref[i]); 1032 txq->grant_ref[i] = GRANT_REF_INVALID; 1033 add_id_to_freelist(txq->mbufs, i); 1034 txq->mbufs_cnt--; 1035 if (txq->mbufs_cnt < 0) { 1036 panic("%s: tx_chain_cnt must be >= 0", __func__); 1037 } 1038 m_free(m); 1039 } 1040 } 1041 1042 static struct mbuf * 1043 xn_alloc_one_rx_buffer(struct netfront_rxq *rxq) 1044 { 1045 struct mbuf *m; 1046 1047 m = m_getjcl(M_NOWAIT, MT_DATA, M_PKTHDR, MJUMPAGESIZE); 1048 if (m == NULL) 1049 return NULL; 1050 m->m_len = m->m_pkthdr.len = MJUMPAGESIZE; 1051 1052 return (m); 1053 } 1054 1055 static void 1056 xn_alloc_rx_buffers(struct netfront_rxq *rxq) 1057 { 1058 RING_IDX req_prod; 1059 int notify; 1060 1061 XN_RX_LOCK_ASSERT(rxq); 1062 1063 if (__predict_false(rxq->info->carrier == 0)) 1064 return; 1065 1066 for (req_prod = rxq->ring.req_prod_pvt; 1067 req_prod - rxq->ring.rsp_cons < NET_RX_RING_SIZE; 1068 req_prod++) { 1069 struct mbuf *m; 1070 unsigned short id; 1071 grant_ref_t ref; 1072 struct netif_rx_request *req; 1073 unsigned long pfn; 1074 1075 m = xn_alloc_one_rx_buffer(rxq); 1076 if (m == NULL) 1077 break; 1078 1079 id = xn_rxidx(req_prod); 1080 1081 KASSERT(rxq->mbufs[id] == NULL, ("non-NULL xn_rx_chain")); 1082 rxq->mbufs[id] = m; 1083 1084 ref = gnttab_claim_grant_reference(&rxq->gref_head); 1085 KASSERT(ref != GNTTAB_LIST_END, 1086 ("reserved grant references exhuasted")); 1087 rxq->grant_ref[id] = ref; 1088 1089 pfn = atop(vtophys(mtod(m, vm_offset_t))); 1090 req = RING_GET_REQUEST(&rxq->ring, req_prod); 1091 1092 gnttab_grant_foreign_access_ref(ref, 1093 xenbus_get_otherend_id(rxq->info->xbdev), pfn, 0); 1094 req->id = id; 1095 req->gref = ref; 1096 } 1097 1098 rxq->ring.req_prod_pvt = req_prod; 1099 1100 /* Not enough requests? Try again later. */ 1101 if (req_prod - rxq->ring.rsp_cons < NET_RX_SLOTS_MIN) { 1102 callout_reset_curcpu(&rxq->rx_refill, hz/10, 1103 xn_alloc_rx_buffers_callout, rxq); 1104 return; 1105 } 1106 1107 wmb(); /* barrier so backend seens requests */ 1108 1109 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&rxq->ring, notify); 1110 if (notify) 1111 xen_intr_signal(rxq->xen_intr_handle); 1112 } 1113 1114 static void xn_alloc_rx_buffers_callout(void *arg) 1115 { 1116 struct netfront_rxq *rxq; 1117 1118 rxq = (struct netfront_rxq *)arg; 1119 XN_RX_LOCK(rxq); 1120 xn_alloc_rx_buffers(rxq); 1121 XN_RX_UNLOCK(rxq); 1122 } 1123 1124 static void 1125 xn_release_rx_bufs(struct netfront_rxq *rxq) 1126 { 1127 int i, ref; 1128 struct mbuf *m; 1129 1130 for (i = 0; i < NET_RX_RING_SIZE; i++) { 1131 m = rxq->mbufs[i]; 1132 1133 if (m == NULL) 1134 continue; 1135 1136 ref = rxq->grant_ref[i]; 1137 if (ref == GRANT_REF_INVALID) 1138 continue; 1139 1140 gnttab_end_foreign_access_ref(ref); 1141 gnttab_release_grant_reference(&rxq->gref_head, ref); 1142 rxq->mbufs[i] = NULL; 1143 rxq->grant_ref[i] = GRANT_REF_INVALID; 1144 m_freem(m); 1145 } 1146 } 1147 1148 static void 1149 xn_rxeof(struct netfront_rxq *rxq) 1150 { 1151 struct ifnet *ifp; 1152 struct netfront_info *np = rxq->info; 1153 #if (defined(INET) || defined(INET6)) 1154 struct lro_ctrl *lro = &rxq->lro; 1155 #endif 1156 struct netfront_rx_info rinfo; 1157 struct netif_rx_response *rx = &rinfo.rx; 1158 struct netif_extra_info *extras = rinfo.extras; 1159 RING_IDX i, rp; 1160 struct mbuf *m; 1161 struct mbufq mbufq_rxq, mbufq_errq; 1162 int err, work_to_do; 1163 1164 XN_RX_LOCK_ASSERT(rxq); 1165 1166 if (!netfront_carrier_ok(np)) 1167 return; 1168 1169 /* XXX: there should be some sane limit. */ 1170 mbufq_init(&mbufq_errq, INT_MAX); 1171 mbufq_init(&mbufq_rxq, INT_MAX); 1172 1173 ifp = np->xn_ifp; 1174 1175 do { 1176 rp = rxq->ring.sring->rsp_prod; 1177 rmb(); /* Ensure we see queued responses up to 'rp'. */ 1178 1179 i = rxq->ring.rsp_cons; 1180 while ((i != rp)) { 1181 memcpy(rx, RING_GET_RESPONSE(&rxq->ring, i), sizeof(*rx)); 1182 memset(extras, 0, sizeof(rinfo.extras)); 1183 1184 m = NULL; 1185 err = xn_get_responses(rxq, &rinfo, rp, &i, &m); 1186 1187 if (__predict_false(err)) { 1188 if (m) 1189 (void )mbufq_enqueue(&mbufq_errq, m); 1190 if_inc_counter(ifp, IFCOUNTER_IQDROPS, 1); 1191 continue; 1192 } 1193 1194 m->m_pkthdr.rcvif = ifp; 1195 if (rx->flags & NETRXF_data_validated) { 1196 /* 1197 * According to mbuf(9) the correct way to tell 1198 * the stack that the checksum of an inbound 1199 * packet is correct, without it actually being 1200 * present (because the underlying interface 1201 * doesn't provide it), is to set the 1202 * CSUM_DATA_VALID and CSUM_PSEUDO_HDR flags, 1203 * and the csum_data field to 0xffff. 1204 */ 1205 m->m_pkthdr.csum_flags |= (CSUM_DATA_VALID 1206 | CSUM_PSEUDO_HDR); 1207 m->m_pkthdr.csum_data = 0xffff; 1208 } 1209 if ((rx->flags & NETRXF_extra_info) != 0 && 1210 (extras[XEN_NETIF_EXTRA_TYPE_GSO - 1].type == 1211 XEN_NETIF_EXTRA_TYPE_GSO)) { 1212 m->m_pkthdr.tso_segsz = 1213 extras[XEN_NETIF_EXTRA_TYPE_GSO - 1].u.gso.size; 1214 m->m_pkthdr.csum_flags |= CSUM_TSO; 1215 } 1216 1217 (void )mbufq_enqueue(&mbufq_rxq, m); 1218 } 1219 1220 rxq->ring.rsp_cons = i; 1221 1222 xn_alloc_rx_buffers(rxq); 1223 1224 RING_FINAL_CHECK_FOR_RESPONSES(&rxq->ring, work_to_do); 1225 } while (work_to_do); 1226 1227 XN_RX_UNLOCK(rxq); 1228 mbufq_drain(&mbufq_errq); 1229 /* 1230 * Process all the mbufs after the remapping is complete. 1231 * Break the mbuf chain first though. 1232 */ 1233 while ((m = mbufq_dequeue(&mbufq_rxq)) != NULL) { 1234 if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1); 1235 #if (defined(INET) || defined(INET6)) 1236 /* Use LRO if possible */ 1237 if ((ifp->if_capenable & IFCAP_LRO) == 0 || 1238 lro->lro_cnt == 0 || tcp_lro_rx(lro, m, 0)) { 1239 /* 1240 * If LRO fails, pass up to the stack 1241 * directly. 1242 */ 1243 (*ifp->if_input)(ifp, m); 1244 } 1245 #else 1246 (*ifp->if_input)(ifp, m); 1247 #endif 1248 } 1249 1250 #if (defined(INET) || defined(INET6)) 1251 /* 1252 * Flush any outstanding LRO work 1253 */ 1254 tcp_lro_flush_all(lro); 1255 #endif 1256 XN_RX_LOCK(rxq); 1257 } 1258 1259 static void 1260 xn_txeof(struct netfront_txq *txq) 1261 { 1262 RING_IDX i, prod; 1263 unsigned short id; 1264 struct ifnet *ifp; 1265 netif_tx_response_t *txr; 1266 struct mbuf *m; 1267 struct netfront_info *np = txq->info; 1268 1269 XN_TX_LOCK_ASSERT(txq); 1270 1271 if (!netfront_carrier_ok(np)) 1272 return; 1273 1274 ifp = np->xn_ifp; 1275 1276 do { 1277 prod = txq->ring.sring->rsp_prod; 1278 rmb(); /* Ensure we see responses up to 'rp'. */ 1279 1280 for (i = txq->ring.rsp_cons; i != prod; i++) { 1281 txr = RING_GET_RESPONSE(&txq->ring, i); 1282 if (txr->status == NETIF_RSP_NULL) 1283 continue; 1284 1285 if (txr->status != NETIF_RSP_OKAY) { 1286 printf("%s: WARNING: response is %d!\n", 1287 __func__, txr->status); 1288 } 1289 id = txr->id; 1290 m = txq->mbufs[id]; 1291 KASSERT(m != NULL, ("mbuf not found in chain")); 1292 KASSERT((uintptr_t)m > NET_TX_RING_SIZE, 1293 ("mbuf already on the free list, but we're " 1294 "trying to free it again!")); 1295 M_ASSERTVALID(m); 1296 1297 if (__predict_false(gnttab_query_foreign_access( 1298 txq->grant_ref[id]) != 0)) { 1299 panic("%s: grant id %u still in use by the " 1300 "backend", __func__, id); 1301 } 1302 gnttab_end_foreign_access_ref(txq->grant_ref[id]); 1303 gnttab_release_grant_reference( 1304 &txq->gref_head, txq->grant_ref[id]); 1305 txq->grant_ref[id] = GRANT_REF_INVALID; 1306 1307 txq->mbufs[id] = NULL; 1308 add_id_to_freelist(txq->mbufs, id); 1309 txq->mbufs_cnt--; 1310 m_free(m); 1311 /* Only mark the txq active if we've freed up at least one slot to try */ 1312 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 1313 } 1314 txq->ring.rsp_cons = prod; 1315 1316 /* 1317 * Set a new event, then check for race with update of 1318 * tx_cons. Note that it is essential to schedule a 1319 * callback, no matter how few buffers are pending. Even if 1320 * there is space in the transmit ring, higher layers may 1321 * be blocked because too much data is outstanding: in such 1322 * cases notification from Xen is likely to be the only kick 1323 * that we'll get. 1324 */ 1325 txq->ring.sring->rsp_event = 1326 prod + ((txq->ring.sring->req_prod - prod) >> 1) + 1; 1327 1328 mb(); 1329 } while (prod != txq->ring.sring->rsp_prod); 1330 1331 if (txq->full && 1332 ((txq->ring.sring->req_prod - prod) < NET_TX_RING_SIZE)) { 1333 txq->full = false; 1334 xn_txq_start(txq); 1335 } 1336 } 1337 1338 static void 1339 xn_intr(void *xsc) 1340 { 1341 struct netfront_txq *txq = xsc; 1342 struct netfront_info *np = txq->info; 1343 struct netfront_rxq *rxq = &np->rxq[txq->id]; 1344 1345 /* kick both tx and rx */ 1346 xn_rxq_intr(rxq); 1347 xn_txq_intr(txq); 1348 } 1349 1350 static void 1351 xn_move_rx_slot(struct netfront_rxq *rxq, struct mbuf *m, 1352 grant_ref_t ref) 1353 { 1354 int new = xn_rxidx(rxq->ring.req_prod_pvt); 1355 1356 KASSERT(rxq->mbufs[new] == NULL, ("mbufs != NULL")); 1357 rxq->mbufs[new] = m; 1358 rxq->grant_ref[new] = ref; 1359 RING_GET_REQUEST(&rxq->ring, rxq->ring.req_prod_pvt)->id = new; 1360 RING_GET_REQUEST(&rxq->ring, rxq->ring.req_prod_pvt)->gref = ref; 1361 rxq->ring.req_prod_pvt++; 1362 } 1363 1364 static int 1365 xn_get_extras(struct netfront_rxq *rxq, 1366 struct netif_extra_info *extras, RING_IDX rp, RING_IDX *cons) 1367 { 1368 struct netif_extra_info *extra; 1369 1370 int err = 0; 1371 1372 do { 1373 struct mbuf *m; 1374 grant_ref_t ref; 1375 1376 if (__predict_false(*cons + 1 == rp)) { 1377 err = EINVAL; 1378 break; 1379 } 1380 1381 extra = (struct netif_extra_info *) 1382 RING_GET_RESPONSE(&rxq->ring, ++(*cons)); 1383 1384 if (__predict_false(!extra->type || 1385 extra->type >= XEN_NETIF_EXTRA_TYPE_MAX)) { 1386 err = EINVAL; 1387 } else { 1388 memcpy(&extras[extra->type - 1], extra, sizeof(*extra)); 1389 } 1390 1391 m = xn_get_rx_mbuf(rxq, *cons); 1392 ref = xn_get_rx_ref(rxq, *cons); 1393 xn_move_rx_slot(rxq, m, ref); 1394 } while (extra->flags & XEN_NETIF_EXTRA_FLAG_MORE); 1395 1396 return err; 1397 } 1398 1399 static int 1400 xn_get_responses(struct netfront_rxq *rxq, 1401 struct netfront_rx_info *rinfo, RING_IDX rp, RING_IDX *cons, 1402 struct mbuf **list) 1403 { 1404 struct netif_rx_response *rx = &rinfo->rx; 1405 struct netif_extra_info *extras = rinfo->extras; 1406 struct mbuf *m, *m0, *m_prev; 1407 grant_ref_t ref = xn_get_rx_ref(rxq, *cons); 1408 RING_IDX ref_cons = *cons; 1409 int frags = 1; 1410 int err = 0; 1411 u_long ret; 1412 1413 m0 = m = m_prev = xn_get_rx_mbuf(rxq, *cons); 1414 1415 if (rx->flags & NETRXF_extra_info) { 1416 err = xn_get_extras(rxq, extras, rp, cons); 1417 } 1418 1419 if (m0 != NULL) { 1420 m0->m_pkthdr.len = 0; 1421 m0->m_next = NULL; 1422 } 1423 1424 for (;;) { 1425 #if 0 1426 DPRINTK("rx->status=%hd rx->offset=%hu frags=%u\n", 1427 rx->status, rx->offset, frags); 1428 #endif 1429 if (__predict_false(rx->status < 0 || 1430 rx->offset + rx->status > PAGE_SIZE)) { 1431 1432 xn_move_rx_slot(rxq, m, ref); 1433 if (m0 == m) 1434 m0 = NULL; 1435 m = NULL; 1436 err = EINVAL; 1437 goto next_skip_queue; 1438 } 1439 1440 /* 1441 * This definitely indicates a bug, either in this driver or in 1442 * the backend driver. In future this should flag the bad 1443 * situation to the system controller to reboot the backed. 1444 */ 1445 if (ref == GRANT_REF_INVALID) { 1446 printf("%s: Bad rx response id %d.\n", __func__, rx->id); 1447 err = EINVAL; 1448 goto next; 1449 } 1450 1451 ret = gnttab_end_foreign_access_ref(ref); 1452 KASSERT(ret, ("Unable to end access to grant references")); 1453 1454 gnttab_release_grant_reference(&rxq->gref_head, ref); 1455 1456 next: 1457 if (m == NULL) 1458 break; 1459 1460 m->m_len = rx->status; 1461 m->m_data += rx->offset; 1462 m0->m_pkthdr.len += rx->status; 1463 1464 next_skip_queue: 1465 if (!(rx->flags & NETRXF_more_data)) 1466 break; 1467 1468 if (*cons + frags == rp) { 1469 if (net_ratelimit()) 1470 WPRINTK("Need more frags\n"); 1471 err = ENOENT; 1472 printf("%s: cons %u frags %u rp %u, not enough frags\n", 1473 __func__, *cons, frags, rp); 1474 break; 1475 } 1476 /* 1477 * Note that m can be NULL, if rx->status < 0 or if 1478 * rx->offset + rx->status > PAGE_SIZE above. 1479 */ 1480 m_prev = m; 1481 1482 rx = RING_GET_RESPONSE(&rxq->ring, *cons + frags); 1483 m = xn_get_rx_mbuf(rxq, *cons + frags); 1484 1485 /* 1486 * m_prev == NULL can happen if rx->status < 0 or if 1487 * rx->offset + * rx->status > PAGE_SIZE above. 1488 */ 1489 if (m_prev != NULL) 1490 m_prev->m_next = m; 1491 1492 /* 1493 * m0 can be NULL if rx->status < 0 or if * rx->offset + 1494 * rx->status > PAGE_SIZE above. 1495 */ 1496 if (m0 == NULL) 1497 m0 = m; 1498 m->m_next = NULL; 1499 ref = xn_get_rx_ref(rxq, *cons + frags); 1500 ref_cons = *cons + frags; 1501 frags++; 1502 } 1503 *list = m0; 1504 *cons += frags; 1505 1506 return (err); 1507 } 1508 1509 /** 1510 * \brief Count the number of fragments in an mbuf chain. 1511 * 1512 * Surprisingly, there isn't an M* macro for this. 1513 */ 1514 static inline int 1515 xn_count_frags(struct mbuf *m) 1516 { 1517 int nfrags; 1518 1519 for (nfrags = 0; m != NULL; m = m->m_next) 1520 nfrags++; 1521 1522 return (nfrags); 1523 } 1524 1525 /** 1526 * Given an mbuf chain, make sure we have enough room and then push 1527 * it onto the transmit ring. 1528 */ 1529 static int 1530 xn_assemble_tx_request(struct netfront_txq *txq, struct mbuf *m_head) 1531 { 1532 struct mbuf *m; 1533 struct netfront_info *np = txq->info; 1534 struct ifnet *ifp = np->xn_ifp; 1535 u_int nfrags; 1536 int otherend_id; 1537 1538 /** 1539 * Defragment the mbuf if necessary. 1540 */ 1541 nfrags = xn_count_frags(m_head); 1542 1543 /* 1544 * Check to see whether this request is longer than netback 1545 * can handle, and try to defrag it. 1546 */ 1547 /** 1548 * It is a bit lame, but the netback driver in Linux can't 1549 * deal with nfrags > MAX_TX_REQ_FRAGS, which is a quirk of 1550 * the Linux network stack. 1551 */ 1552 if (nfrags > np->maxfrags) { 1553 m = m_defrag(m_head, M_NOWAIT); 1554 if (!m) { 1555 /* 1556 * Defrag failed, so free the mbuf and 1557 * therefore drop the packet. 1558 */ 1559 m_freem(m_head); 1560 return (EMSGSIZE); 1561 } 1562 m_head = m; 1563 } 1564 1565 /* Determine how many fragments now exist */ 1566 nfrags = xn_count_frags(m_head); 1567 1568 /* 1569 * Check to see whether the defragmented packet has too many 1570 * segments for the Linux netback driver. 1571 */ 1572 /** 1573 * The FreeBSD TCP stack, with TSO enabled, can produce a chain 1574 * of mbufs longer than Linux can handle. Make sure we don't 1575 * pass a too-long chain over to the other side by dropping the 1576 * packet. It doesn't look like there is currently a way to 1577 * tell the TCP stack to generate a shorter chain of packets. 1578 */ 1579 if (nfrags > MAX_TX_REQ_FRAGS) { 1580 #ifdef DEBUG 1581 printf("%s: nfrags %d > MAX_TX_REQ_FRAGS %d, netback " 1582 "won't be able to handle it, dropping\n", 1583 __func__, nfrags, MAX_TX_REQ_FRAGS); 1584 #endif 1585 m_freem(m_head); 1586 return (EMSGSIZE); 1587 } 1588 1589 /* 1590 * This check should be redundant. We've already verified that we 1591 * have enough slots in the ring to handle a packet of maximum 1592 * size, and that our packet is less than the maximum size. Keep 1593 * it in here as an assert for now just to make certain that 1594 * chain_cnt is accurate. 1595 */ 1596 KASSERT((txq->mbufs_cnt + nfrags) <= NET_TX_RING_SIZE, 1597 ("%s: chain_cnt (%d) + nfrags (%d) > NET_TX_RING_SIZE " 1598 "(%d)!", __func__, (int) txq->mbufs_cnt, 1599 (int) nfrags, (int) NET_TX_RING_SIZE)); 1600 1601 /* 1602 * Start packing the mbufs in this chain into 1603 * the fragment pointers. Stop when we run out 1604 * of fragments or hit the end of the mbuf chain. 1605 */ 1606 m = m_head; 1607 otherend_id = xenbus_get_otherend_id(np->xbdev); 1608 for (m = m_head; m; m = m->m_next) { 1609 netif_tx_request_t *tx; 1610 uintptr_t id; 1611 grant_ref_t ref; 1612 u_long mfn; /* XXX Wrong type? */ 1613 1614 tx = RING_GET_REQUEST(&txq->ring, txq->ring.req_prod_pvt); 1615 id = get_id_from_freelist(txq->mbufs); 1616 if (id == 0) 1617 panic("%s: was allocated the freelist head!\n", 1618 __func__); 1619 txq->mbufs_cnt++; 1620 if (txq->mbufs_cnt > NET_TX_RING_SIZE) 1621 panic("%s: tx_chain_cnt must be <= NET_TX_RING_SIZE\n", 1622 __func__); 1623 txq->mbufs[id] = m; 1624 tx->id = id; 1625 ref = gnttab_claim_grant_reference(&txq->gref_head); 1626 KASSERT((short)ref >= 0, ("Negative ref")); 1627 mfn = virt_to_mfn(mtod(m, vm_offset_t)); 1628 gnttab_grant_foreign_access_ref(ref, otherend_id, 1629 mfn, GNTMAP_readonly); 1630 tx->gref = txq->grant_ref[id] = ref; 1631 tx->offset = mtod(m, vm_offset_t) & (PAGE_SIZE - 1); 1632 tx->flags = 0; 1633 if (m == m_head) { 1634 /* 1635 * The first fragment has the entire packet 1636 * size, subsequent fragments have just the 1637 * fragment size. The backend works out the 1638 * true size of the first fragment by 1639 * subtracting the sizes of the other 1640 * fragments. 1641 */ 1642 tx->size = m->m_pkthdr.len; 1643 1644 /* 1645 * The first fragment contains the checksum flags 1646 * and is optionally followed by extra data for 1647 * TSO etc. 1648 */ 1649 /** 1650 * CSUM_TSO requires checksum offloading. 1651 * Some versions of FreeBSD fail to 1652 * set CSUM_TCP in the CSUM_TSO case, 1653 * so we have to test for CSUM_TSO 1654 * explicitly. 1655 */ 1656 if (m->m_pkthdr.csum_flags 1657 & (CSUM_DELAY_DATA | CSUM_TSO)) { 1658 tx->flags |= (NETTXF_csum_blank 1659 | NETTXF_data_validated); 1660 } 1661 if (m->m_pkthdr.csum_flags & CSUM_TSO) { 1662 struct netif_extra_info *gso = 1663 (struct netif_extra_info *) 1664 RING_GET_REQUEST(&txq->ring, 1665 ++txq->ring.req_prod_pvt); 1666 1667 tx->flags |= NETTXF_extra_info; 1668 1669 gso->u.gso.size = m->m_pkthdr.tso_segsz; 1670 gso->u.gso.type = 1671 XEN_NETIF_GSO_TYPE_TCPV4; 1672 gso->u.gso.pad = 0; 1673 gso->u.gso.features = 0; 1674 1675 gso->type = XEN_NETIF_EXTRA_TYPE_GSO; 1676 gso->flags = 0; 1677 } 1678 } else { 1679 tx->size = m->m_len; 1680 } 1681 if (m->m_next) 1682 tx->flags |= NETTXF_more_data; 1683 1684 txq->ring.req_prod_pvt++; 1685 } 1686 BPF_MTAP(ifp, m_head); 1687 1688 if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1); 1689 if_inc_counter(ifp, IFCOUNTER_OBYTES, m_head->m_pkthdr.len); 1690 if (m_head->m_flags & M_MCAST) 1691 if_inc_counter(ifp, IFCOUNTER_OMCASTS, 1); 1692 1693 xn_txeof(txq); 1694 1695 return (0); 1696 } 1697 1698 /* equivalent of network_open() in Linux */ 1699 static void 1700 xn_ifinit_locked(struct netfront_info *np) 1701 { 1702 struct ifnet *ifp; 1703 int i; 1704 struct netfront_rxq *rxq; 1705 1706 XN_LOCK_ASSERT(np); 1707 1708 ifp = np->xn_ifp; 1709 1710 if (ifp->if_drv_flags & IFF_DRV_RUNNING || !netfront_carrier_ok(np)) 1711 return; 1712 1713 xn_stop(np); 1714 1715 for (i = 0; i < np->num_queues; i++) { 1716 rxq = &np->rxq[i]; 1717 XN_RX_LOCK(rxq); 1718 xn_alloc_rx_buffers(rxq); 1719 rxq->ring.sring->rsp_event = rxq->ring.rsp_cons + 1; 1720 if (RING_HAS_UNCONSUMED_RESPONSES(&rxq->ring)) 1721 xn_rxeof(rxq); 1722 XN_RX_UNLOCK(rxq); 1723 } 1724 1725 ifp->if_drv_flags |= IFF_DRV_RUNNING; 1726 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 1727 if_link_state_change(ifp, LINK_STATE_UP); 1728 } 1729 1730 static void 1731 xn_ifinit(void *xsc) 1732 { 1733 struct netfront_info *sc = xsc; 1734 1735 XN_LOCK(sc); 1736 xn_ifinit_locked(sc); 1737 XN_UNLOCK(sc); 1738 } 1739 1740 static int 1741 xn_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data) 1742 { 1743 struct netfront_info *sc = ifp->if_softc; 1744 struct ifreq *ifr = (struct ifreq *) data; 1745 device_t dev; 1746 #ifdef INET 1747 struct ifaddr *ifa = (struct ifaddr *)data; 1748 #endif 1749 int mask, error = 0, reinit; 1750 1751 dev = sc->xbdev; 1752 1753 switch(cmd) { 1754 case SIOCSIFADDR: 1755 #ifdef INET 1756 XN_LOCK(sc); 1757 if (ifa->ifa_addr->sa_family == AF_INET) { 1758 ifp->if_flags |= IFF_UP; 1759 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) 1760 xn_ifinit_locked(sc); 1761 arp_ifinit(ifp, ifa); 1762 XN_UNLOCK(sc); 1763 } else { 1764 XN_UNLOCK(sc); 1765 #endif 1766 error = ether_ioctl(ifp, cmd, data); 1767 #ifdef INET 1768 } 1769 #endif 1770 break; 1771 case SIOCSIFMTU: 1772 ifp->if_mtu = ifr->ifr_mtu; 1773 ifp->if_drv_flags &= ~IFF_DRV_RUNNING; 1774 xn_ifinit(sc); 1775 break; 1776 case SIOCSIFFLAGS: 1777 XN_LOCK(sc); 1778 if (ifp->if_flags & IFF_UP) { 1779 /* 1780 * If only the state of the PROMISC flag changed, 1781 * then just use the 'set promisc mode' command 1782 * instead of reinitializing the entire NIC. Doing 1783 * a full re-init means reloading the firmware and 1784 * waiting for it to start up, which may take a 1785 * second or two. 1786 */ 1787 xn_ifinit_locked(sc); 1788 } else { 1789 if (ifp->if_drv_flags & IFF_DRV_RUNNING) { 1790 xn_stop(sc); 1791 } 1792 } 1793 sc->xn_if_flags = ifp->if_flags; 1794 XN_UNLOCK(sc); 1795 break; 1796 case SIOCSIFCAP: 1797 mask = ifr->ifr_reqcap ^ ifp->if_capenable; 1798 reinit = 0; 1799 1800 if (mask & IFCAP_TXCSUM) { 1801 ifp->if_capenable ^= IFCAP_TXCSUM; 1802 ifp->if_hwassist ^= XN_CSUM_FEATURES; 1803 } 1804 if (mask & IFCAP_TSO4) { 1805 ifp->if_capenable ^= IFCAP_TSO4; 1806 ifp->if_hwassist ^= CSUM_TSO; 1807 } 1808 1809 if (mask & (IFCAP_RXCSUM | IFCAP_LRO)) { 1810 /* These Rx features require us to renegotiate. */ 1811 reinit = 1; 1812 1813 if (mask & IFCAP_RXCSUM) 1814 ifp->if_capenable ^= IFCAP_RXCSUM; 1815 if (mask & IFCAP_LRO) 1816 ifp->if_capenable ^= IFCAP_LRO; 1817 } 1818 1819 if (reinit == 0) 1820 break; 1821 1822 /* 1823 * We must reset the interface so the backend picks up the 1824 * new features. 1825 */ 1826 device_printf(sc->xbdev, 1827 "performing interface reset due to feature change\n"); 1828 XN_LOCK(sc); 1829 netfront_carrier_off(sc); 1830 sc->xn_reset = true; 1831 /* 1832 * NB: the pending packet queue is not flushed, since 1833 * the interface should still support the old options. 1834 */ 1835 XN_UNLOCK(sc); 1836 /* 1837 * Delete the xenstore nodes that export features. 1838 * 1839 * NB: There's a xenbus state called 1840 * "XenbusStateReconfiguring", which is what we should set 1841 * here. Sadly none of the backends know how to handle it, 1842 * and simply disconnect from the frontend, so we will just 1843 * switch back to XenbusStateInitialising in order to force 1844 * a reconnection. 1845 */ 1846 xs_rm(XST_NIL, xenbus_get_node(dev), "feature-gso-tcpv4"); 1847 xs_rm(XST_NIL, xenbus_get_node(dev), "feature-no-csum-offload"); 1848 xenbus_set_state(dev, XenbusStateClosing); 1849 1850 /* 1851 * Wait for the frontend to reconnect before returning 1852 * from the ioctl. 30s should be more than enough for any 1853 * sane backend to reconnect. 1854 */ 1855 error = tsleep(sc, 0, "xn_rst", 30*hz); 1856 break; 1857 case SIOCADDMULTI: 1858 case SIOCDELMULTI: 1859 break; 1860 case SIOCSIFMEDIA: 1861 case SIOCGIFMEDIA: 1862 error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, cmd); 1863 break; 1864 default: 1865 error = ether_ioctl(ifp, cmd, data); 1866 } 1867 1868 return (error); 1869 } 1870 1871 static void 1872 xn_stop(struct netfront_info *sc) 1873 { 1874 struct ifnet *ifp; 1875 1876 XN_LOCK_ASSERT(sc); 1877 1878 ifp = sc->xn_ifp; 1879 1880 ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE); 1881 if_link_state_change(ifp, LINK_STATE_DOWN); 1882 } 1883 1884 static void 1885 xn_rebuild_rx_bufs(struct netfront_rxq *rxq) 1886 { 1887 int requeue_idx, i; 1888 grant_ref_t ref; 1889 netif_rx_request_t *req; 1890 1891 for (requeue_idx = 0, i = 0; i < NET_RX_RING_SIZE; i++) { 1892 struct mbuf *m; 1893 u_long pfn; 1894 1895 if (rxq->mbufs[i] == NULL) 1896 continue; 1897 1898 m = rxq->mbufs[requeue_idx] = xn_get_rx_mbuf(rxq, i); 1899 ref = rxq->grant_ref[requeue_idx] = xn_get_rx_ref(rxq, i); 1900 1901 req = RING_GET_REQUEST(&rxq->ring, requeue_idx); 1902 pfn = vtophys(mtod(m, vm_offset_t)) >> PAGE_SHIFT; 1903 1904 gnttab_grant_foreign_access_ref(ref, 1905 xenbus_get_otherend_id(rxq->info->xbdev), 1906 pfn, 0); 1907 1908 req->gref = ref; 1909 req->id = requeue_idx; 1910 1911 requeue_idx++; 1912 } 1913 1914 rxq->ring.req_prod_pvt = requeue_idx; 1915 } 1916 1917 /* START of Xenolinux helper functions adapted to FreeBSD */ 1918 static int 1919 xn_connect(struct netfront_info *np) 1920 { 1921 int i, error; 1922 u_int feature_rx_copy; 1923 struct netfront_rxq *rxq; 1924 struct netfront_txq *txq; 1925 1926 error = xs_scanf(XST_NIL, xenbus_get_otherend_path(np->xbdev), 1927 "feature-rx-copy", NULL, "%u", &feature_rx_copy); 1928 if (error != 0) 1929 feature_rx_copy = 0; 1930 1931 /* We only support rx copy. */ 1932 if (!feature_rx_copy) 1933 return (EPROTONOSUPPORT); 1934 1935 /* Recovery procedure: */ 1936 error = talk_to_backend(np->xbdev, np); 1937 if (error != 0) 1938 return (error); 1939 1940 /* Step 1: Reinitialise variables. */ 1941 xn_query_features(np); 1942 xn_configure_features(np); 1943 1944 /* Step 2: Release TX buffer */ 1945 for (i = 0; i < np->num_queues; i++) { 1946 txq = &np->txq[i]; 1947 xn_release_tx_bufs(txq); 1948 } 1949 1950 /* Step 3: Rebuild the RX buffer freelist and the RX ring itself. */ 1951 for (i = 0; i < np->num_queues; i++) { 1952 rxq = &np->rxq[i]; 1953 xn_rebuild_rx_bufs(rxq); 1954 } 1955 1956 /* Step 4: All public and private state should now be sane. Get 1957 * ready to start sending and receiving packets and give the driver 1958 * domain a kick because we've probably just requeued some 1959 * packets. 1960 */ 1961 netfront_carrier_on(np); 1962 wakeup(np); 1963 1964 return (0); 1965 } 1966 1967 static void 1968 xn_kick_rings(struct netfront_info *np) 1969 { 1970 struct netfront_rxq *rxq; 1971 struct netfront_txq *txq; 1972 int i; 1973 1974 for (i = 0; i < np->num_queues; i++) { 1975 txq = &np->txq[i]; 1976 rxq = &np->rxq[i]; 1977 xen_intr_signal(txq->xen_intr_handle); 1978 XN_TX_LOCK(txq); 1979 xn_txeof(txq); 1980 XN_TX_UNLOCK(txq); 1981 XN_RX_LOCK(rxq); 1982 xn_alloc_rx_buffers(rxq); 1983 XN_RX_UNLOCK(rxq); 1984 } 1985 } 1986 1987 static void 1988 xn_query_features(struct netfront_info *np) 1989 { 1990 int val; 1991 1992 device_printf(np->xbdev, "backend features:"); 1993 1994 if (xs_scanf(XST_NIL, xenbus_get_otherend_path(np->xbdev), 1995 "feature-sg", NULL, "%d", &val) != 0) 1996 val = 0; 1997 1998 np->maxfrags = 1; 1999 if (val) { 2000 np->maxfrags = MAX_TX_REQ_FRAGS; 2001 printf(" feature-sg"); 2002 } 2003 2004 if (xs_scanf(XST_NIL, xenbus_get_otherend_path(np->xbdev), 2005 "feature-gso-tcpv4", NULL, "%d", &val) != 0) 2006 val = 0; 2007 2008 np->xn_ifp->if_capabilities &= ~(IFCAP_TSO4|IFCAP_LRO); 2009 if (val) { 2010 np->xn_ifp->if_capabilities |= IFCAP_TSO4|IFCAP_LRO; 2011 printf(" feature-gso-tcp4"); 2012 } 2013 2014 /* 2015 * HW CSUM offload is assumed to be available unless 2016 * feature-no-csum-offload is set in xenstore. 2017 */ 2018 if (xs_scanf(XST_NIL, xenbus_get_otherend_path(np->xbdev), 2019 "feature-no-csum-offload", NULL, "%d", &val) != 0) 2020 val = 0; 2021 2022 np->xn_ifp->if_capabilities |= IFCAP_HWCSUM; 2023 if (val) { 2024 np->xn_ifp->if_capabilities &= ~(IFCAP_HWCSUM); 2025 printf(" feature-no-csum-offload"); 2026 } 2027 2028 printf("\n"); 2029 } 2030 2031 static int 2032 xn_configure_features(struct netfront_info *np) 2033 { 2034 int err, cap_enabled; 2035 #if (defined(INET) || defined(INET6)) 2036 int i; 2037 #endif 2038 struct ifnet *ifp; 2039 2040 ifp = np->xn_ifp; 2041 err = 0; 2042 2043 if ((ifp->if_capenable & ifp->if_capabilities) == ifp->if_capenable) { 2044 /* Current options are available, no need to do anything. */ 2045 return (0); 2046 } 2047 2048 /* Try to preserve as many options as possible. */ 2049 cap_enabled = ifp->if_capenable; 2050 ifp->if_capenable = ifp->if_hwassist = 0; 2051 2052 #if (defined(INET) || defined(INET6)) 2053 if ((cap_enabled & IFCAP_LRO) != 0) 2054 for (i = 0; i < np->num_queues; i++) 2055 tcp_lro_free(&np->rxq[i].lro); 2056 if (xn_enable_lro && 2057 (ifp->if_capabilities & cap_enabled & IFCAP_LRO) != 0) { 2058 ifp->if_capenable |= IFCAP_LRO; 2059 for (i = 0; i < np->num_queues; i++) { 2060 err = tcp_lro_init(&np->rxq[i].lro); 2061 if (err != 0) { 2062 device_printf(np->xbdev, 2063 "LRO initialization failed\n"); 2064 ifp->if_capenable &= ~IFCAP_LRO; 2065 break; 2066 } 2067 np->rxq[i].lro.ifp = ifp; 2068 } 2069 } 2070 if ((ifp->if_capabilities & cap_enabled & IFCAP_TSO4) != 0) { 2071 ifp->if_capenable |= IFCAP_TSO4; 2072 ifp->if_hwassist |= CSUM_TSO; 2073 } 2074 #endif 2075 if ((ifp->if_capabilities & cap_enabled & IFCAP_TXCSUM) != 0) { 2076 ifp->if_capenable |= IFCAP_TXCSUM; 2077 ifp->if_hwassist |= XN_CSUM_FEATURES; 2078 } 2079 if ((ifp->if_capabilities & cap_enabled & IFCAP_RXCSUM) != 0) 2080 ifp->if_capenable |= IFCAP_RXCSUM; 2081 2082 return (err); 2083 } 2084 2085 static int 2086 xn_txq_mq_start_locked(struct netfront_txq *txq, struct mbuf *m) 2087 { 2088 struct netfront_info *np; 2089 struct ifnet *ifp; 2090 struct buf_ring *br; 2091 int error, notify; 2092 2093 np = txq->info; 2094 br = txq->br; 2095 ifp = np->xn_ifp; 2096 error = 0; 2097 2098 XN_TX_LOCK_ASSERT(txq); 2099 2100 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0 || 2101 !netfront_carrier_ok(np)) { 2102 if (m != NULL) 2103 error = drbr_enqueue(ifp, br, m); 2104 return (error); 2105 } 2106 2107 if (m != NULL) { 2108 error = drbr_enqueue(ifp, br, m); 2109 if (error != 0) 2110 return (error); 2111 } 2112 2113 while ((m = drbr_peek(ifp, br)) != NULL) { 2114 if (!xn_tx_slot_available(txq)) { 2115 drbr_putback(ifp, br, m); 2116 break; 2117 } 2118 2119 error = xn_assemble_tx_request(txq, m); 2120 /* xn_assemble_tx_request always consumes the mbuf*/ 2121 if (error != 0) { 2122 drbr_advance(ifp, br); 2123 break; 2124 } 2125 2126 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&txq->ring, notify); 2127 if (notify) 2128 xen_intr_signal(txq->xen_intr_handle); 2129 2130 drbr_advance(ifp, br); 2131 } 2132 2133 if (RING_FULL(&txq->ring)) 2134 txq->full = true; 2135 2136 return (0); 2137 } 2138 2139 static int 2140 xn_txq_mq_start(struct ifnet *ifp, struct mbuf *m) 2141 { 2142 struct netfront_info *np; 2143 struct netfront_txq *txq; 2144 int i, npairs, error; 2145 2146 np = ifp->if_softc; 2147 npairs = np->num_queues; 2148 2149 if (!netfront_carrier_ok(np)) 2150 return (ENOBUFS); 2151 2152 KASSERT(npairs != 0, ("called with 0 available queues")); 2153 2154 /* check if flowid is set */ 2155 if (M_HASHTYPE_GET(m) != M_HASHTYPE_NONE) 2156 i = m->m_pkthdr.flowid % npairs; 2157 else 2158 i = curcpu % npairs; 2159 2160 txq = &np->txq[i]; 2161 2162 if (XN_TX_TRYLOCK(txq) != 0) { 2163 error = xn_txq_mq_start_locked(txq, m); 2164 XN_TX_UNLOCK(txq); 2165 } else { 2166 error = drbr_enqueue(ifp, txq->br, m); 2167 taskqueue_enqueue(txq->tq, &txq->defrtask); 2168 } 2169 2170 return (error); 2171 } 2172 2173 static void 2174 xn_qflush(struct ifnet *ifp) 2175 { 2176 struct netfront_info *np; 2177 struct netfront_txq *txq; 2178 struct mbuf *m; 2179 int i; 2180 2181 np = ifp->if_softc; 2182 2183 for (i = 0; i < np->num_queues; i++) { 2184 txq = &np->txq[i]; 2185 2186 XN_TX_LOCK(txq); 2187 while ((m = buf_ring_dequeue_sc(txq->br)) != NULL) 2188 m_freem(m); 2189 XN_TX_UNLOCK(txq); 2190 } 2191 2192 if_qflush(ifp); 2193 } 2194 2195 /** 2196 * Create a network device. 2197 * @param dev Newbus device representing this virtual NIC. 2198 */ 2199 int 2200 create_netdev(device_t dev) 2201 { 2202 struct netfront_info *np; 2203 int err; 2204 struct ifnet *ifp; 2205 2206 np = device_get_softc(dev); 2207 2208 np->xbdev = dev; 2209 2210 mtx_init(&np->sc_lock, "xnsc", "netfront softc lock", MTX_DEF); 2211 2212 ifmedia_init(&np->sc_media, 0, xn_ifmedia_upd, xn_ifmedia_sts); 2213 ifmedia_add(&np->sc_media, IFM_ETHER|IFM_MANUAL, 0, NULL); 2214 ifmedia_set(&np->sc_media, IFM_ETHER|IFM_MANUAL); 2215 2216 err = xen_net_read_mac(dev, np->mac); 2217 if (err != 0) 2218 goto error; 2219 2220 /* Set up ifnet structure */ 2221 ifp = np->xn_ifp = if_alloc(IFT_ETHER); 2222 ifp->if_softc = np; 2223 if_initname(ifp, "xn", device_get_unit(dev)); 2224 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST; 2225 ifp->if_ioctl = xn_ioctl; 2226 2227 ifp->if_transmit = xn_txq_mq_start; 2228 ifp->if_qflush = xn_qflush; 2229 2230 ifp->if_init = xn_ifinit; 2231 2232 ifp->if_hwassist = XN_CSUM_FEATURES; 2233 /* Enable all supported features at device creation. */ 2234 ifp->if_capenable = ifp->if_capabilities = 2235 IFCAP_HWCSUM|IFCAP_TSO4|IFCAP_LRO; 2236 ifp->if_hw_tsomax = 65536 - (ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN); 2237 ifp->if_hw_tsomaxsegcount = MAX_TX_REQ_FRAGS; 2238 ifp->if_hw_tsomaxsegsize = PAGE_SIZE; 2239 2240 ether_ifattach(ifp, np->mac); 2241 netfront_carrier_off(np); 2242 2243 return (0); 2244 2245 error: 2246 KASSERT(err != 0, ("Error path with no error code specified")); 2247 return (err); 2248 } 2249 2250 static int 2251 netfront_detach(device_t dev) 2252 { 2253 struct netfront_info *info = device_get_softc(dev); 2254 2255 DPRINTK("%s\n", xenbus_get_node(dev)); 2256 2257 netif_free(info); 2258 2259 return 0; 2260 } 2261 2262 static void 2263 netif_free(struct netfront_info *np) 2264 { 2265 2266 XN_LOCK(np); 2267 xn_stop(np); 2268 XN_UNLOCK(np); 2269 netif_disconnect_backend(np); 2270 ether_ifdetach(np->xn_ifp); 2271 free(np->rxq, M_DEVBUF); 2272 free(np->txq, M_DEVBUF); 2273 if_free(np->xn_ifp); 2274 np->xn_ifp = NULL; 2275 ifmedia_removeall(&np->sc_media); 2276 } 2277 2278 static void 2279 netif_disconnect_backend(struct netfront_info *np) 2280 { 2281 u_int i; 2282 2283 for (i = 0; i < np->num_queues; i++) { 2284 XN_RX_LOCK(&np->rxq[i]); 2285 XN_TX_LOCK(&np->txq[i]); 2286 } 2287 netfront_carrier_off(np); 2288 for (i = 0; i < np->num_queues; i++) { 2289 XN_RX_UNLOCK(&np->rxq[i]); 2290 XN_TX_UNLOCK(&np->txq[i]); 2291 } 2292 2293 for (i = 0; i < np->num_queues; i++) { 2294 disconnect_rxq(&np->rxq[i]); 2295 disconnect_txq(&np->txq[i]); 2296 } 2297 } 2298 2299 static int 2300 xn_ifmedia_upd(struct ifnet *ifp) 2301 { 2302 2303 return (0); 2304 } 2305 2306 static void 2307 xn_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr) 2308 { 2309 2310 ifmr->ifm_status = IFM_AVALID|IFM_ACTIVE; 2311 ifmr->ifm_active = IFM_ETHER|IFM_MANUAL; 2312 } 2313 2314 /* ** Driver registration ** */ 2315 static device_method_t netfront_methods[] = { 2316 /* Device interface */ 2317 DEVMETHOD(device_probe, netfront_probe), 2318 DEVMETHOD(device_attach, netfront_attach), 2319 DEVMETHOD(device_detach, netfront_detach), 2320 DEVMETHOD(device_shutdown, bus_generic_shutdown), 2321 DEVMETHOD(device_suspend, netfront_suspend), 2322 DEVMETHOD(device_resume, netfront_resume), 2323 2324 /* Xenbus interface */ 2325 DEVMETHOD(xenbus_otherend_changed, netfront_backend_changed), 2326 2327 DEVMETHOD_END 2328 }; 2329 2330 static driver_t netfront_driver = { 2331 "xn", 2332 netfront_methods, 2333 sizeof(struct netfront_info), 2334 }; 2335 devclass_t netfront_devclass; 2336 2337 DRIVER_MODULE(xe, xenbusb_front, netfront_driver, netfront_devclass, NULL, 2338 NULL); 2339