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