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