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