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