netback.c revision 259637
1/*- 2 * Copyright (c) 2009-2011 Spectra Logic Corporation 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 * without modification. 11 * 2. Redistributions in binary form must reproduce at minimum a disclaimer 12 * substantially similar to the "NO WARRANTY" disclaimer below 13 * ("Disclaimer") and any redistribution must be conditioned upon 14 * including a substantially similar Disclaimer requirement for further 15 * binary redistribution. 16 * 17 * NO WARRANTY 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR 21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 26 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 27 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 28 * POSSIBILITY OF SUCH DAMAGES. 29 * 30 * Authors: Justin T. Gibbs (Spectra Logic Corporation) 31 * Alan Somers (Spectra Logic Corporation) 32 * John Suykerbuyk (Spectra Logic Corporation) 33 */ 34 35#include <sys/cdefs.h> 36__FBSDID("$FreeBSD: releng/10.0/sys/dev/xen/netback/netback.c 259637 2013-12-20 00:09:14Z glebius $"); 37 38/** 39 * \file netback.c 40 * 41 * \brief Device driver supporting the vending of network access 42 * from this FreeBSD domain to other domains. 43 */ 44#include "opt_inet.h" 45#include "opt_inet6.h" 46#include "opt_global.h" 47 48#include "opt_sctp.h" 49 50#include <sys/param.h> 51#include <sys/kernel.h> 52 53#include <sys/bus.h> 54#include <sys/module.h> 55#include <sys/rman.h> 56#include <sys/socket.h> 57#include <sys/sockio.h> 58#include <sys/sysctl.h> 59 60#include <net/if.h> 61#include <net/if_arp.h> 62#include <net/ethernet.h> 63#include <net/if_dl.h> 64#include <net/if_media.h> 65#include <net/if_types.h> 66 67#include <netinet/in.h> 68#include <netinet/ip.h> 69#include <netinet/if_ether.h> 70#if __FreeBSD_version >= 700000 71#include <netinet/tcp.h> 72#endif 73#include <netinet/ip_icmp.h> 74#include <netinet/udp.h> 75#include <machine/in_cksum.h> 76 77#include <vm/vm.h> 78#include <vm/pmap.h> 79#include <vm/vm_extern.h> 80#include <vm/vm_kern.h> 81 82#include <machine/_inttypes.h> 83 84#include <xen/xen-os.h> 85#include <xen/hypervisor.h> 86#include <xen/xen_intr.h> 87#include <xen/interface/io/netif.h> 88#include <xen/xenbus/xenbusvar.h> 89 90#include <machine/xen/xenvar.h> 91 92/*--------------------------- Compile-time Tunables --------------------------*/ 93 94/*---------------------------------- Macros ----------------------------------*/ 95/** 96 * Custom malloc type for all driver allocations. 97 */ 98static MALLOC_DEFINE(M_XENNETBACK, "xnb", "Xen Net Back Driver Data"); 99 100#define XNB_SG 1 /* netback driver supports feature-sg */ 101#define XNB_GSO_TCPV4 1 /* netback driver supports feature-gso-tcpv4 */ 102#define XNB_RX_COPY 1 /* netback driver supports feature-rx-copy */ 103#define XNB_RX_FLIP 0 /* netback driver does not support feature-rx-flip */ 104 105#undef XNB_DEBUG 106#define XNB_DEBUG /* hardcode on during development */ 107 108#ifdef XNB_DEBUG 109#define DPRINTF(fmt, args...) \ 110 printf("xnb(%s:%d): " fmt, __FUNCTION__, __LINE__, ##args) 111#else 112#define DPRINTF(fmt, args...) do {} while (0) 113#endif 114 115/* Default length for stack-allocated grant tables */ 116#define GNTTAB_LEN (64) 117 118/* Features supported by all backends. TSO and LRO can be negotiated */ 119#define XNB_CSUM_FEATURES (CSUM_TCP | CSUM_UDP) 120 121#define NET_TX_RING_SIZE __RING_SIZE((netif_tx_sring_t *)0, PAGE_SIZE) 122#define NET_RX_RING_SIZE __RING_SIZE((netif_rx_sring_t *)0, PAGE_SIZE) 123 124/** 125 * Two argument version of the standard macro. Second argument is a tentative 126 * value of req_cons 127 */ 128#define RING_HAS_UNCONSUMED_REQUESTS_2(_r, cons) ({ \ 129 unsigned int req = (_r)->sring->req_prod - cons; \ 130 unsigned int rsp = RING_SIZE(_r) - \ 131 (cons - (_r)->rsp_prod_pvt); \ 132 req < rsp ? req : rsp; \ 133}) 134 135#define virt_to_mfn(x) (vtomach(x) >> PAGE_SHIFT) 136#define virt_to_offset(x) ((x) & (PAGE_SIZE - 1)) 137 138/** 139 * Predefined array type of grant table copy descriptors. Used to pass around 140 * statically allocated memory structures. 141 */ 142typedef struct gnttab_copy gnttab_copy_table[GNTTAB_LEN]; 143 144/*--------------------------- Forward Declarations ---------------------------*/ 145struct xnb_softc; 146struct xnb_pkt; 147 148static void xnb_attach_failed(struct xnb_softc *xnb, 149 int err, const char *fmt, ...) 150 __printflike(3,4); 151static int xnb_shutdown(struct xnb_softc *xnb); 152static int create_netdev(device_t dev); 153static int xnb_detach(device_t dev); 154static int xen_net_read_mac(device_t dev, uint8_t mac[]); 155static int xnb_ifmedia_upd(struct ifnet *ifp); 156static void xnb_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr); 157static void xnb_intr(void *arg); 158static int xnb_send(netif_rx_back_ring_t *rxb, domid_t otherend, 159 const struct mbuf *mbufc, gnttab_copy_table gnttab); 160static int xnb_recv(netif_tx_back_ring_t *txb, domid_t otherend, 161 struct mbuf **mbufc, struct ifnet *ifnet, 162 gnttab_copy_table gnttab); 163static int xnb_ring2pkt(struct xnb_pkt *pkt, 164 const netif_tx_back_ring_t *tx_ring, 165 RING_IDX start); 166static void xnb_txpkt2rsp(const struct xnb_pkt *pkt, 167 netif_tx_back_ring_t *ring, int error); 168static struct mbuf *xnb_pkt2mbufc(const struct xnb_pkt *pkt, struct ifnet *ifp); 169static int xnb_txpkt2gnttab(const struct xnb_pkt *pkt, 170 const struct mbuf *mbufc, 171 gnttab_copy_table gnttab, 172 const netif_tx_back_ring_t *txb, 173 domid_t otherend_id); 174static void xnb_update_mbufc(struct mbuf *mbufc, 175 const gnttab_copy_table gnttab, int n_entries); 176static int xnb_mbufc2pkt(const struct mbuf *mbufc, 177 struct xnb_pkt *pkt, 178 RING_IDX start, int space); 179static int xnb_rxpkt2gnttab(const struct xnb_pkt *pkt, 180 const struct mbuf *mbufc, 181 gnttab_copy_table gnttab, 182 const netif_rx_back_ring_t *rxb, 183 domid_t otherend_id); 184static int xnb_rxpkt2rsp(const struct xnb_pkt *pkt, 185 const gnttab_copy_table gnttab, int n_entries, 186 netif_rx_back_ring_t *ring); 187static void xnb_stop(struct xnb_softc*); 188static int xnb_ioctl(struct ifnet*, u_long, caddr_t); 189static void xnb_start_locked(struct ifnet*); 190static void xnb_start(struct ifnet*); 191static void xnb_ifinit_locked(struct xnb_softc*); 192static void xnb_ifinit(void*); 193#ifdef XNB_DEBUG 194static int xnb_unit_test_main(SYSCTL_HANDLER_ARGS); 195static int xnb_dump_rings(SYSCTL_HANDLER_ARGS); 196#endif 197#if defined(INET) || defined(INET6) 198static void xnb_add_mbuf_cksum(struct mbuf *mbufc); 199#endif 200/*------------------------------ Data Structures -----------------------------*/ 201 202 203/** 204 * Representation of a xennet packet. Simplified version of a packet as 205 * stored in the Xen tx ring. Applicable to both RX and TX packets 206 */ 207struct xnb_pkt{ 208 /** 209 * Array index of the first data-bearing (eg, not extra info) entry 210 * for this packet 211 */ 212 RING_IDX car; 213 214 /** 215 * Array index of the second data-bearing entry for this packet. 216 * Invalid if the packet has only one data-bearing entry. If the 217 * packet has more than two data-bearing entries, then the second 218 * through the last will be sequential modulo the ring size 219 */ 220 RING_IDX cdr; 221 222 /** 223 * Optional extra info. Only valid if flags contains 224 * NETTXF_extra_info. Note that extra.type will always be 225 * XEN_NETIF_EXTRA_TYPE_GSO. Currently, no known netfront or netback 226 * driver will ever set XEN_NETIF_EXTRA_TYPE_MCAST_* 227 */ 228 netif_extra_info_t extra; 229 230 /** Size of entire packet in bytes. */ 231 uint16_t size; 232 233 /** The size of the first entry's data in bytes */ 234 uint16_t car_size; 235 236 /** 237 * Either NETTXF_ or NETRXF_ flags. Note that the flag values are 238 * not the same for TX and RX packets 239 */ 240 uint16_t flags; 241 242 /** 243 * The number of valid data-bearing entries (either netif_tx_request's 244 * or netif_rx_response's) in the packet. If this is 0, it means the 245 * entire packet is invalid. 246 */ 247 uint16_t list_len; 248 249 /** There was an error processing the packet */ 250 uint8_t error; 251}; 252 253/** xnb_pkt method: initialize it */ 254static inline void 255xnb_pkt_initialize(struct xnb_pkt *pxnb) 256{ 257 bzero(pxnb, sizeof(*pxnb)); 258} 259 260/** xnb_pkt method: mark the packet as valid */ 261static inline void 262xnb_pkt_validate(struct xnb_pkt *pxnb) 263{ 264 pxnb->error = 0; 265}; 266 267/** xnb_pkt method: mark the packet as invalid */ 268static inline void 269xnb_pkt_invalidate(struct xnb_pkt *pxnb) 270{ 271 pxnb->error = 1; 272}; 273 274/** xnb_pkt method: Check whether the packet is valid */ 275static inline int 276xnb_pkt_is_valid(const struct xnb_pkt *pxnb) 277{ 278 return (! pxnb->error); 279} 280 281#ifdef XNB_DEBUG 282/** xnb_pkt method: print the packet's contents in human-readable format*/ 283static void __unused 284xnb_dump_pkt(const struct xnb_pkt *pkt) { 285 if (pkt == NULL) { 286 DPRINTF("Was passed a null pointer.\n"); 287 return; 288 } 289 DPRINTF("pkt address= %p\n", pkt); 290 DPRINTF("pkt->size=%d\n", pkt->size); 291 DPRINTF("pkt->car_size=%d\n", pkt->car_size); 292 DPRINTF("pkt->flags=0x%04x\n", pkt->flags); 293 DPRINTF("pkt->list_len=%d\n", pkt->list_len); 294 /* DPRINTF("pkt->extra"); TODO */ 295 DPRINTF("pkt->car=%d\n", pkt->car); 296 DPRINTF("pkt->cdr=%d\n", pkt->cdr); 297 DPRINTF("pkt->error=%d\n", pkt->error); 298} 299#endif /* XNB_DEBUG */ 300 301static void 302xnb_dump_txreq(RING_IDX idx, const struct netif_tx_request *txreq) 303{ 304 if (txreq != NULL) { 305 DPRINTF("netif_tx_request index =%u\n", idx); 306 DPRINTF("netif_tx_request.gref =%u\n", txreq->gref); 307 DPRINTF("netif_tx_request.offset=%hu\n", txreq->offset); 308 DPRINTF("netif_tx_request.flags =%hu\n", txreq->flags); 309 DPRINTF("netif_tx_request.id =%hu\n", txreq->id); 310 DPRINTF("netif_tx_request.size =%hu\n", txreq->size); 311 } 312} 313 314 315/** 316 * \brief Configuration data for a shared memory request ring 317 * used to communicate with the front-end client of this 318 * this driver. 319 */ 320struct xnb_ring_config { 321 /** 322 * Runtime structures for ring access. Unfortunately, TX and RX rings 323 * use different data structures, and that cannot be changed since it 324 * is part of the interdomain protocol. 325 */ 326 union{ 327 netif_rx_back_ring_t rx_ring; 328 netif_tx_back_ring_t tx_ring; 329 } back_ring; 330 331 /** 332 * The device bus address returned by the hypervisor when 333 * mapping the ring and required to unmap it when a connection 334 * is torn down. 335 */ 336 uint64_t bus_addr; 337 338 /** The pseudo-physical address where ring memory is mapped.*/ 339 uint64_t gnt_addr; 340 341 /** KVA address where ring memory is mapped. */ 342 vm_offset_t va; 343 344 /** 345 * Grant table handles, one per-ring page, returned by the 346 * hyperpervisor upon mapping of the ring and required to 347 * unmap it when a connection is torn down. 348 */ 349 grant_handle_t handle; 350 351 /** The number of ring pages mapped for the current connection. */ 352 unsigned ring_pages; 353 354 /** 355 * The grant references, one per-ring page, supplied by the 356 * front-end, allowing us to reference the ring pages in the 357 * front-end's domain and to map these pages into our own domain. 358 */ 359 grant_ref_t ring_ref; 360}; 361 362/** 363 * Per-instance connection state flags. 364 */ 365typedef enum 366{ 367 /** Communication with the front-end has been established. */ 368 XNBF_RING_CONNECTED = 0x01, 369 370 /** 371 * Front-end requests exist in the ring and are waiting for 372 * xnb_xen_req objects to free up. 373 */ 374 XNBF_RESOURCE_SHORTAGE = 0x02, 375 376 /** Connection teardown has started. */ 377 XNBF_SHUTDOWN = 0x04, 378 379 /** A thread is already performing shutdown processing. */ 380 XNBF_IN_SHUTDOWN = 0x08 381} xnb_flag_t; 382 383/** 384 * Types of rings. Used for array indices and to identify a ring's control 385 * data structure type 386 */ 387typedef enum{ 388 XNB_RING_TYPE_TX = 0, /* ID of TX rings, used for array indices */ 389 XNB_RING_TYPE_RX = 1, /* ID of RX rings, used for array indices */ 390 XNB_NUM_RING_TYPES 391} xnb_ring_type_t; 392 393/** 394 * Per-instance configuration data. 395 */ 396struct xnb_softc { 397 /** NewBus device corresponding to this instance. */ 398 device_t dev; 399 400 /* Media related fields */ 401 402 /** Generic network media state */ 403 struct ifmedia sc_media; 404 405 /** Media carrier info */ 406 struct ifnet *xnb_ifp; 407 408 /** Our own private carrier state */ 409 unsigned carrier; 410 411 /** Device MAC Address */ 412 uint8_t mac[ETHER_ADDR_LEN]; 413 414 /* Xen related fields */ 415 416 /** 417 * \brief The netif protocol abi in effect. 418 * 419 * There are situations where the back and front ends can 420 * have a different, native abi (e.g. intel x86_64 and 421 * 32bit x86 domains on the same machine). The back-end 422 * always accomodates the front-end's native abi. That 423 * value is pulled from the XenStore and recorded here. 424 */ 425 int abi; 426 427 /** 428 * Name of the bridge to which this VIF is connected, if any 429 * This field is dynamically allocated by xenbus and must be free()ed 430 * when no longer needed 431 */ 432 char *bridge; 433 434 /** The interrupt driven even channel used to signal ring events. */ 435 evtchn_port_t evtchn; 436 437 /** Xen device handle.*/ 438 long handle; 439 440 /** Handle to the communication ring event channel. */ 441 xen_intr_handle_t xen_intr_handle; 442 443 /** 444 * \brief Cached value of the front-end's domain id. 445 * 446 * This value is used at once for each mapped page in 447 * a transaction. We cache it to avoid incuring the 448 * cost of an ivar access every time this is needed. 449 */ 450 domid_t otherend_id; 451 452 /** 453 * Undocumented frontend feature. Has something to do with 454 * scatter/gather IO 455 */ 456 uint8_t can_sg; 457 /** Undocumented frontend feature */ 458 uint8_t gso; 459 /** Undocumented frontend feature */ 460 uint8_t gso_prefix; 461 /** Can checksum TCP/UDP over IPv4 */ 462 uint8_t ip_csum; 463 464 /* Implementation related fields */ 465 /** 466 * Preallocated grant table copy descriptor for RX operations. 467 * Access must be protected by rx_lock 468 */ 469 gnttab_copy_table rx_gnttab; 470 471 /** 472 * Preallocated grant table copy descriptor for TX operations. 473 * Access must be protected by tx_lock 474 */ 475 gnttab_copy_table tx_gnttab; 476 477#ifdef XENHVM 478 /** 479 * Resource representing allocated physical address space 480 * associated with our per-instance kva region. 481 */ 482 struct resource *pseudo_phys_res; 483 484 /** Resource id for allocated physical address space. */ 485 int pseudo_phys_res_id; 486#endif 487 488 /** Ring mapping and interrupt configuration data. */ 489 struct xnb_ring_config ring_configs[XNB_NUM_RING_TYPES]; 490 491 /** 492 * Global pool of kva used for mapping remote domain ring 493 * and I/O transaction data. 494 */ 495 vm_offset_t kva; 496 497 /** Psuedo-physical address corresponding to kva. */ 498 uint64_t gnt_base_addr; 499 500 /** Various configuration and state bit flags. */ 501 xnb_flag_t flags; 502 503 /** Mutex protecting per-instance data in the receive path. */ 504 struct mtx rx_lock; 505 506 /** Mutex protecting per-instance data in the softc structure. */ 507 struct mtx sc_lock; 508 509 /** Mutex protecting per-instance data in the transmit path. */ 510 struct mtx tx_lock; 511 512 /** The size of the global kva pool. */ 513 int kva_size; 514}; 515 516/*---------------------------- Debugging functions ---------------------------*/ 517#ifdef XNB_DEBUG 518static void __unused 519xnb_dump_gnttab_copy(const struct gnttab_copy *entry) 520{ 521 if (entry == NULL) { 522 printf("NULL grant table pointer\n"); 523 return; 524 } 525 526 if (entry->flags & GNTCOPY_dest_gref) 527 printf("gnttab dest ref=\t%u\n", entry->dest.u.ref); 528 else 529 printf("gnttab dest gmfn=\t%lu\n", entry->dest.u.gmfn); 530 printf("gnttab dest offset=\t%hu\n", entry->dest.offset); 531 printf("gnttab dest domid=\t%hu\n", entry->dest.domid); 532 if (entry->flags & GNTCOPY_source_gref) 533 printf("gnttab source ref=\t%u\n", entry->source.u.ref); 534 else 535 printf("gnttab source gmfn=\t%lu\n", entry->source.u.gmfn); 536 printf("gnttab source offset=\t%hu\n", entry->source.offset); 537 printf("gnttab source domid=\t%hu\n", entry->source.domid); 538 printf("gnttab len=\t%hu\n", entry->len); 539 printf("gnttab flags=\t%hu\n", entry->flags); 540 printf("gnttab status=\t%hd\n", entry->status); 541} 542 543static int 544xnb_dump_rings(SYSCTL_HANDLER_ARGS) 545{ 546 static char results[720]; 547 struct xnb_softc const* xnb = (struct xnb_softc*)arg1; 548 netif_rx_back_ring_t const* rxb = 549 &xnb->ring_configs[XNB_RING_TYPE_RX].back_ring.rx_ring; 550 netif_tx_back_ring_t const* txb = 551 &xnb->ring_configs[XNB_RING_TYPE_TX].back_ring.tx_ring; 552 553 /* empty the result strings */ 554 results[0] = 0; 555 556 if ( !txb || !txb->sring || !rxb || !rxb->sring ) 557 return (SYSCTL_OUT(req, results, strnlen(results, 720))); 558 559 snprintf(results, 720, 560 "\n\t%35s %18s\n" /* TX, RX */ 561 "\t%16s %18d %18d\n" /* req_cons */ 562 "\t%16s %18d %18d\n" /* nr_ents */ 563 "\t%16s %18d %18d\n" /* rsp_prod_pvt */ 564 "\t%16s %18p %18p\n" /* sring */ 565 "\t%16s %18d %18d\n" /* req_prod */ 566 "\t%16s %18d %18d\n" /* req_event */ 567 "\t%16s %18d %18d\n" /* rsp_prod */ 568 "\t%16s %18d %18d\n", /* rsp_event */ 569 "TX", "RX", 570 "req_cons", txb->req_cons, rxb->req_cons, 571 "nr_ents", txb->nr_ents, rxb->nr_ents, 572 "rsp_prod_pvt", txb->rsp_prod_pvt, rxb->rsp_prod_pvt, 573 "sring", txb->sring, rxb->sring, 574 "sring->req_prod", txb->sring->req_prod, rxb->sring->req_prod, 575 "sring->req_event", txb->sring->req_event, rxb->sring->req_event, 576 "sring->rsp_prod", txb->sring->rsp_prod, rxb->sring->rsp_prod, 577 "sring->rsp_event", txb->sring->rsp_event, rxb->sring->rsp_event); 578 579 return (SYSCTL_OUT(req, results, strnlen(results, 720))); 580} 581 582static void __unused 583xnb_dump_mbuf(const struct mbuf *m) 584{ 585 int len; 586 uint8_t *d; 587 if (m == NULL) 588 return; 589 590 printf("xnb_dump_mbuf:\n"); 591 if (m->m_flags & M_PKTHDR) { 592 printf(" flowid=%10d, csum_flags=%#8x, csum_data=%#8x, " 593 "tso_segsz=%5hd\n", 594 m->m_pkthdr.flowid, (int)m->m_pkthdr.csum_flags, 595 m->m_pkthdr.csum_data, m->m_pkthdr.tso_segsz); 596 printf(" rcvif=%16p, len=%19d\n", 597 m->m_pkthdr.rcvif, m->m_pkthdr.len); 598 } 599 printf(" m_next=%16p, m_nextpk=%16p, m_data=%16p\n", 600 m->m_next, m->m_nextpkt, m->m_data); 601 printf(" m_len=%17d, m_flags=%#15x, m_type=%18u\n", 602 m->m_len, m->m_flags, m->m_type); 603 604 len = m->m_len; 605 d = mtod(m, uint8_t*); 606 while (len > 0) { 607 int i; 608 printf(" "); 609 for (i = 0; (i < 16) && (len > 0); i++, len--) { 610 printf("%02hhx ", *(d++)); 611 } 612 printf("\n"); 613 } 614} 615#endif /* XNB_DEBUG */ 616 617/*------------------------ Inter-Domain Communication ------------------------*/ 618/** 619 * Free dynamically allocated KVA or pseudo-physical address allocations. 620 * 621 * \param xnb Per-instance xnb configuration structure. 622 */ 623static void 624xnb_free_communication_mem(struct xnb_softc *xnb) 625{ 626 if (xnb->kva != 0) { 627#ifndef XENHVM 628 kva_free(xnb->kva, xnb->kva_size); 629#else 630 if (xnb->pseudo_phys_res != NULL) { 631 bus_release_resource(xnb->dev, SYS_RES_MEMORY, 632 xnb->pseudo_phys_res_id, 633 xnb->pseudo_phys_res); 634 xnb->pseudo_phys_res = NULL; 635 } 636#endif /* XENHVM */ 637 } 638 xnb->kva = 0; 639 xnb->gnt_base_addr = 0; 640} 641 642/** 643 * Cleanup all inter-domain communication mechanisms. 644 * 645 * \param xnb Per-instance xnb configuration structure. 646 */ 647static int 648xnb_disconnect(struct xnb_softc *xnb) 649{ 650 struct gnttab_unmap_grant_ref gnts[XNB_NUM_RING_TYPES]; 651 int error; 652 int i; 653 654 xen_intr_unbind(xnb->xen_intr_handle); 655 656 /* 657 * We may still have another thread currently processing requests. We 658 * must acquire the rx and tx locks to make sure those threads are done, 659 * but we can release those locks as soon as we acquire them, because no 660 * more interrupts will be arriving. 661 */ 662 mtx_lock(&xnb->tx_lock); 663 mtx_unlock(&xnb->tx_lock); 664 mtx_lock(&xnb->rx_lock); 665 mtx_unlock(&xnb->rx_lock); 666 667 /* Free malloc'd softc member variables */ 668 if (xnb->bridge != NULL) 669 free(xnb->bridge, M_XENSTORE); 670 671 /* All request processing has stopped, so unmap the rings */ 672 for (i=0; i < XNB_NUM_RING_TYPES; i++) { 673 gnts[i].host_addr = xnb->ring_configs[i].gnt_addr; 674 gnts[i].dev_bus_addr = xnb->ring_configs[i].bus_addr; 675 gnts[i].handle = xnb->ring_configs[i].handle; 676 } 677 error = HYPERVISOR_grant_table_op(GNTTABOP_unmap_grant_ref, gnts, 678 XNB_NUM_RING_TYPES); 679 KASSERT(error == 0, ("Grant table unmap op failed (%d)", error)); 680 681 xnb_free_communication_mem(xnb); 682 /* 683 * Zero the ring config structs because the pointers, handles, and 684 * grant refs contained therein are no longer valid. 685 */ 686 bzero(&xnb->ring_configs[XNB_RING_TYPE_TX], 687 sizeof(struct xnb_ring_config)); 688 bzero(&xnb->ring_configs[XNB_RING_TYPE_RX], 689 sizeof(struct xnb_ring_config)); 690 691 xnb->flags &= ~XNBF_RING_CONNECTED; 692 return (0); 693} 694 695/** 696 * Map a single shared memory ring into domain local address space and 697 * initialize its control structure 698 * 699 * \param xnb Per-instance xnb configuration structure 700 * \param ring_type Array index of this ring in the xnb's array of rings 701 * \return An errno 702 */ 703static int 704xnb_connect_ring(struct xnb_softc *xnb, xnb_ring_type_t ring_type) 705{ 706 struct gnttab_map_grant_ref gnt; 707 struct xnb_ring_config *ring = &xnb->ring_configs[ring_type]; 708 int error; 709 710 /* TX ring type = 0, RX =1 */ 711 ring->va = xnb->kva + ring_type * PAGE_SIZE; 712 ring->gnt_addr = xnb->gnt_base_addr + ring_type * PAGE_SIZE; 713 714 gnt.host_addr = ring->gnt_addr; 715 gnt.flags = GNTMAP_host_map; 716 gnt.ref = ring->ring_ref; 717 gnt.dom = xnb->otherend_id; 718 719 error = HYPERVISOR_grant_table_op(GNTTABOP_map_grant_ref, &gnt, 1); 720 if (error != 0) 721 panic("netback: Ring page grant table op failed (%d)", error); 722 723 if (gnt.status != 0) { 724 ring->va = 0; 725 error = EACCES; 726 xenbus_dev_fatal(xnb->dev, error, 727 "Ring shared page mapping failed. " 728 "Status %d.", gnt.status); 729 } else { 730 ring->handle = gnt.handle; 731 ring->bus_addr = gnt.dev_bus_addr; 732 733 if (ring_type == XNB_RING_TYPE_TX) { 734 BACK_RING_INIT(&ring->back_ring.tx_ring, 735 (netif_tx_sring_t*)ring->va, 736 ring->ring_pages * PAGE_SIZE); 737 } else if (ring_type == XNB_RING_TYPE_RX) { 738 BACK_RING_INIT(&ring->back_ring.rx_ring, 739 (netif_rx_sring_t*)ring->va, 740 ring->ring_pages * PAGE_SIZE); 741 } else { 742 xenbus_dev_fatal(xnb->dev, error, 743 "Unknown ring type %d", ring_type); 744 } 745 } 746 747 return error; 748} 749 750/** 751 * Setup the shared memory rings and bind an interrupt to the event channel 752 * used to notify us of ring changes. 753 * 754 * \param xnb Per-instance xnb configuration structure. 755 */ 756static int 757xnb_connect_comms(struct xnb_softc *xnb) 758{ 759 int error; 760 xnb_ring_type_t i; 761 762 if ((xnb->flags & XNBF_RING_CONNECTED) != 0) 763 return (0); 764 765 /* 766 * Kva for our rings are at the tail of the region of kva allocated 767 * by xnb_alloc_communication_mem(). 768 */ 769 for (i=0; i < XNB_NUM_RING_TYPES; i++) { 770 error = xnb_connect_ring(xnb, i); 771 if (error != 0) 772 return error; 773 } 774 775 xnb->flags |= XNBF_RING_CONNECTED; 776 777 error = xen_intr_bind_remote_port(xnb->dev, 778 xnb->otherend_id, 779 xnb->evtchn, 780 /*filter*/NULL, 781 xnb_intr, /*arg*/xnb, 782 INTR_TYPE_BIO | INTR_MPSAFE, 783 &xnb->xen_intr_handle); 784 if (error != 0) { 785 (void)xnb_disconnect(xnb); 786 xenbus_dev_fatal(xnb->dev, error, "binding event channel"); 787 return (error); 788 } 789 790 DPRINTF("rings connected!\n"); 791 792 return (0); 793} 794 795/** 796 * Size KVA and pseudo-physical address allocations based on negotiated 797 * values for the size and number of I/O requests, and the size of our 798 * communication ring. 799 * 800 * \param xnb Per-instance xnb configuration structure. 801 * 802 * These address spaces are used to dynamically map pages in the 803 * front-end's domain into our own. 804 */ 805static int 806xnb_alloc_communication_mem(struct xnb_softc *xnb) 807{ 808 xnb_ring_type_t i; 809 810 xnb->kva_size = 0; 811 for (i=0; i < XNB_NUM_RING_TYPES; i++) { 812 xnb->kva_size += xnb->ring_configs[i].ring_pages * PAGE_SIZE; 813 } 814#ifndef XENHVM 815 xnb->kva = kva_alloc(xnb->kva_size); 816 if (xnb->kva == 0) 817 return (ENOMEM); 818 xnb->gnt_base_addr = xnb->kva; 819#else /* defined XENHVM */ 820 /* 821 * Reserve a range of pseudo physical memory that we can map 822 * into kva. These pages will only be backed by machine 823 * pages ("real memory") during the lifetime of front-end requests 824 * via grant table operations. We will map the netif tx and rx rings 825 * into this space. 826 */ 827 xnb->pseudo_phys_res_id = 0; 828 xnb->pseudo_phys_res = bus_alloc_resource(xnb->dev, SYS_RES_MEMORY, 829 &xnb->pseudo_phys_res_id, 830 0, ~0, xnb->kva_size, 831 RF_ACTIVE); 832 if (xnb->pseudo_phys_res == NULL) { 833 xnb->kva = 0; 834 return (ENOMEM); 835 } 836 xnb->kva = (vm_offset_t)rman_get_virtual(xnb->pseudo_phys_res); 837 xnb->gnt_base_addr = rman_get_start(xnb->pseudo_phys_res); 838#endif /* !defined XENHVM */ 839 return (0); 840} 841 842/** 843 * Collect information from the XenStore related to our device and its frontend 844 * 845 * \param xnb Per-instance xnb configuration structure. 846 */ 847static int 848xnb_collect_xenstore_info(struct xnb_softc *xnb) 849{ 850 /** 851 * \todo Linux collects the following info. We should collect most 852 * of this, too: 853 * "feature-rx-notify" 854 */ 855 const char *otherend_path; 856 const char *our_path; 857 int err; 858 unsigned int rx_copy, bridge_len; 859 uint8_t no_csum_offload; 860 861 otherend_path = xenbus_get_otherend_path(xnb->dev); 862 our_path = xenbus_get_node(xnb->dev); 863 864 /* Collect the critical communication parameters */ 865 err = xs_gather(XST_NIL, otherend_path, 866 "tx-ring-ref", "%l" PRIu32, 867 &xnb->ring_configs[XNB_RING_TYPE_TX].ring_ref, 868 "rx-ring-ref", "%l" PRIu32, 869 &xnb->ring_configs[XNB_RING_TYPE_RX].ring_ref, 870 "event-channel", "%" PRIu32, &xnb->evtchn, 871 NULL); 872 if (err != 0) { 873 xenbus_dev_fatal(xnb->dev, err, 874 "Unable to retrieve ring information from " 875 "frontend %s. Unable to connect.", 876 otherend_path); 877 return (err); 878 } 879 880 /* Collect the handle from xenstore */ 881 err = xs_scanf(XST_NIL, our_path, "handle", NULL, "%li", &xnb->handle); 882 if (err != 0) { 883 xenbus_dev_fatal(xnb->dev, err, 884 "Error reading handle from frontend %s. " 885 "Unable to connect.", otherend_path); 886 } 887 888 /* 889 * Collect the bridgename, if any. We do not need bridge_len; we just 890 * throw it away 891 */ 892 err = xs_read(XST_NIL, our_path, "bridge", &bridge_len, 893 (void**)&xnb->bridge); 894 if (err != 0) 895 xnb->bridge = NULL; 896 897 /* 898 * Does the frontend request that we use rx copy? If not, return an 899 * error because this driver only supports rx copy. 900 */ 901 err = xs_scanf(XST_NIL, otherend_path, "request-rx-copy", NULL, 902 "%" PRIu32, &rx_copy); 903 if (err == ENOENT) { 904 err = 0; 905 rx_copy = 0; 906 } 907 if (err < 0) { 908 xenbus_dev_fatal(xnb->dev, err, "reading %s/request-rx-copy", 909 otherend_path); 910 return err; 911 } 912 /** 913 * \todo: figure out the exact meaning of this feature, and when 914 * the frontend will set it to true. It should be set to true 915 * at some point 916 */ 917/* if (!rx_copy)*/ 918/* return EOPNOTSUPP;*/ 919 920 /** \todo Collect the rx notify feature */ 921 922 /* Collect the feature-sg. */ 923 if (xs_scanf(XST_NIL, otherend_path, "feature-sg", NULL, 924 "%hhu", &xnb->can_sg) < 0) 925 xnb->can_sg = 0; 926 927 /* Collect remaining frontend features */ 928 if (xs_scanf(XST_NIL, otherend_path, "feature-gso-tcpv4", NULL, 929 "%hhu", &xnb->gso) < 0) 930 xnb->gso = 0; 931 932 if (xs_scanf(XST_NIL, otherend_path, "feature-gso-tcpv4-prefix", NULL, 933 "%hhu", &xnb->gso_prefix) < 0) 934 xnb->gso_prefix = 0; 935 936 if (xs_scanf(XST_NIL, otherend_path, "feature-no-csum-offload", NULL, 937 "%hhu", &no_csum_offload) < 0) 938 no_csum_offload = 0; 939 xnb->ip_csum = (no_csum_offload == 0); 940 941 return (0); 942} 943 944/** 945 * Supply information about the physical device to the frontend 946 * via XenBus. 947 * 948 * \param xnb Per-instance xnb configuration structure. 949 */ 950static int 951xnb_publish_backend_info(struct xnb_softc *xnb) 952{ 953 struct xs_transaction xst; 954 const char *our_path; 955 int error; 956 957 our_path = xenbus_get_node(xnb->dev); 958 959 do { 960 error = xs_transaction_start(&xst); 961 if (error != 0) { 962 xenbus_dev_fatal(xnb->dev, error, 963 "Error publishing backend info " 964 "(start transaction)"); 965 break; 966 } 967 968 error = xs_printf(xst, our_path, "feature-sg", 969 "%d", XNB_SG); 970 if (error != 0) 971 break; 972 973 error = xs_printf(xst, our_path, "feature-gso-tcpv4", 974 "%d", XNB_GSO_TCPV4); 975 if (error != 0) 976 break; 977 978 error = xs_printf(xst, our_path, "feature-rx-copy", 979 "%d", XNB_RX_COPY); 980 if (error != 0) 981 break; 982 983 error = xs_printf(xst, our_path, "feature-rx-flip", 984 "%d", XNB_RX_FLIP); 985 if (error != 0) 986 break; 987 988 error = xs_transaction_end(xst, 0); 989 if (error != 0 && error != EAGAIN) { 990 xenbus_dev_fatal(xnb->dev, error, "ending transaction"); 991 break; 992 } 993 994 } while (error == EAGAIN); 995 996 return (error); 997} 998 999/** 1000 * Connect to our netfront peer now that it has completed publishing 1001 * its configuration into the XenStore. 1002 * 1003 * \param xnb Per-instance xnb configuration structure. 1004 */ 1005static void 1006xnb_connect(struct xnb_softc *xnb) 1007{ 1008 int error; 1009 1010 if (xenbus_get_state(xnb->dev) == XenbusStateConnected) 1011 return; 1012 1013 if (xnb_collect_xenstore_info(xnb) != 0) 1014 return; 1015 1016 xnb->flags &= ~XNBF_SHUTDOWN; 1017 1018 /* Read front end configuration. */ 1019 1020 /* Allocate resources whose size depends on front-end configuration. */ 1021 error = xnb_alloc_communication_mem(xnb); 1022 if (error != 0) { 1023 xenbus_dev_fatal(xnb->dev, error, 1024 "Unable to allocate communication memory"); 1025 return; 1026 } 1027 1028 /* 1029 * Connect communication channel. 1030 */ 1031 error = xnb_connect_comms(xnb); 1032 if (error != 0) { 1033 /* Specific errors are reported by xnb_connect_comms(). */ 1034 return; 1035 } 1036 xnb->carrier = 1; 1037 1038 /* Ready for I/O. */ 1039 xenbus_set_state(xnb->dev, XenbusStateConnected); 1040} 1041 1042/*-------------------------- Device Teardown Support -------------------------*/ 1043/** 1044 * Perform device shutdown functions. 1045 * 1046 * \param xnb Per-instance xnb configuration structure. 1047 * 1048 * Mark this instance as shutting down, wait for any active requests 1049 * to drain, disconnect from the front-end, and notify any waiters (e.g. 1050 * a thread invoking our detach method) that detach can now proceed. 1051 */ 1052static int 1053xnb_shutdown(struct xnb_softc *xnb) 1054{ 1055 /* 1056 * Due to the need to drop our mutex during some 1057 * xenbus operations, it is possible for two threads 1058 * to attempt to close out shutdown processing at 1059 * the same time. Tell the caller that hits this 1060 * race to try back later. 1061 */ 1062 if ((xnb->flags & XNBF_IN_SHUTDOWN) != 0) 1063 return (EAGAIN); 1064 1065 xnb->flags |= XNBF_SHUTDOWN; 1066 1067 xnb->flags |= XNBF_IN_SHUTDOWN; 1068 1069 mtx_unlock(&xnb->sc_lock); 1070 /* Free the network interface */ 1071 xnb->carrier = 0; 1072 if (xnb->xnb_ifp != NULL) { 1073 ether_ifdetach(xnb->xnb_ifp); 1074 if_free(xnb->xnb_ifp); 1075 xnb->xnb_ifp = NULL; 1076 } 1077 mtx_lock(&xnb->sc_lock); 1078 1079 xnb_disconnect(xnb); 1080 1081 mtx_unlock(&xnb->sc_lock); 1082 if (xenbus_get_state(xnb->dev) < XenbusStateClosing) 1083 xenbus_set_state(xnb->dev, XenbusStateClosing); 1084 mtx_lock(&xnb->sc_lock); 1085 1086 xnb->flags &= ~XNBF_IN_SHUTDOWN; 1087 1088 1089 /* Indicate to xnb_detach() that is it safe to proceed. */ 1090 wakeup(xnb); 1091 1092 return (0); 1093} 1094 1095/** 1096 * Report an attach time error to the console and Xen, and cleanup 1097 * this instance by forcing immediate detach processing. 1098 * 1099 * \param xnb Per-instance xnb configuration structure. 1100 * \param err Errno describing the error. 1101 * \param fmt Printf style format and arguments 1102 */ 1103static void 1104xnb_attach_failed(struct xnb_softc *xnb, int err, const char *fmt, ...) 1105{ 1106 va_list ap; 1107 va_list ap_hotplug; 1108 1109 va_start(ap, fmt); 1110 va_copy(ap_hotplug, ap); 1111 xs_vprintf(XST_NIL, xenbus_get_node(xnb->dev), 1112 "hotplug-error", fmt, ap_hotplug); 1113 va_end(ap_hotplug); 1114 xs_printf(XST_NIL, xenbus_get_node(xnb->dev), 1115 "hotplug-status", "error"); 1116 1117 xenbus_dev_vfatal(xnb->dev, err, fmt, ap); 1118 va_end(ap); 1119 1120 xs_printf(XST_NIL, xenbus_get_node(xnb->dev), 1121 "online", "0"); 1122 xnb_detach(xnb->dev); 1123} 1124 1125/*---------------------------- NewBus Entrypoints ----------------------------*/ 1126/** 1127 * Inspect a XenBus device and claim it if is of the appropriate type. 1128 * 1129 * \param dev NewBus device object representing a candidate XenBus device. 1130 * 1131 * \return 0 for success, errno codes for failure. 1132 */ 1133static int 1134xnb_probe(device_t dev) 1135{ 1136 if (!strcmp(xenbus_get_type(dev), "vif")) { 1137 DPRINTF("Claiming device %d, %s\n", device_get_unit(dev), 1138 devclass_get_name(device_get_devclass(dev))); 1139 device_set_desc(dev, "Backend Virtual Network Device"); 1140 device_quiet(dev); 1141 return (0); 1142 } 1143 return (ENXIO); 1144} 1145 1146/** 1147 * Setup sysctl variables to control various Network Back parameters. 1148 * 1149 * \param xnb Xen Net Back softc. 1150 * 1151 */ 1152static void 1153xnb_setup_sysctl(struct xnb_softc *xnb) 1154{ 1155 struct sysctl_ctx_list *sysctl_ctx = NULL; 1156 struct sysctl_oid *sysctl_tree = NULL; 1157 1158 sysctl_ctx = device_get_sysctl_ctx(xnb->dev); 1159 if (sysctl_ctx == NULL) 1160 return; 1161 1162 sysctl_tree = device_get_sysctl_tree(xnb->dev); 1163 if (sysctl_tree == NULL) 1164 return; 1165 1166#ifdef XNB_DEBUG 1167 SYSCTL_ADD_PROC(sysctl_ctx, 1168 SYSCTL_CHILDREN(sysctl_tree), 1169 OID_AUTO, 1170 "unit_test_results", 1171 CTLTYPE_STRING | CTLFLAG_RD, 1172 xnb, 1173 0, 1174 xnb_unit_test_main, 1175 "A", 1176 "Results of builtin unit tests"); 1177 1178 SYSCTL_ADD_PROC(sysctl_ctx, 1179 SYSCTL_CHILDREN(sysctl_tree), 1180 OID_AUTO, 1181 "dump_rings", 1182 CTLTYPE_STRING | CTLFLAG_RD, 1183 xnb, 1184 0, 1185 xnb_dump_rings, 1186 "A", 1187 "Xennet Back Rings"); 1188#endif /* XNB_DEBUG */ 1189} 1190 1191/** 1192 * Create a network device. 1193 * @param handle device handle 1194 */ 1195int 1196create_netdev(device_t dev) 1197{ 1198 struct ifnet *ifp; 1199 struct xnb_softc *xnb; 1200 int err = 0; 1201 1202 xnb = device_get_softc(dev); 1203 mtx_init(&xnb->sc_lock, "xnb_softc", "xen netback softc lock", MTX_DEF); 1204 mtx_init(&xnb->tx_lock, "xnb_tx", "xen netback tx lock", MTX_DEF); 1205 mtx_init(&xnb->rx_lock, "xnb_rx", "xen netback rx lock", MTX_DEF); 1206 1207 xnb->dev = dev; 1208 1209 ifmedia_init(&xnb->sc_media, 0, xnb_ifmedia_upd, xnb_ifmedia_sts); 1210 ifmedia_add(&xnb->sc_media, IFM_ETHER|IFM_MANUAL, 0, NULL); 1211 ifmedia_set(&xnb->sc_media, IFM_ETHER|IFM_MANUAL); 1212 1213 err = xen_net_read_mac(dev, xnb->mac); 1214 if (err == 0) { 1215 /* Set up ifnet structure */ 1216 ifp = xnb->xnb_ifp = if_alloc(IFT_ETHER); 1217 ifp->if_softc = xnb; 1218 if_initname(ifp, "xnb", device_get_unit(dev)); 1219 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST; 1220 ifp->if_ioctl = xnb_ioctl; 1221 ifp->if_output = ether_output; 1222 ifp->if_start = xnb_start; 1223#ifdef notyet 1224 ifp->if_watchdog = xnb_watchdog; 1225#endif 1226 ifp->if_init = xnb_ifinit; 1227 ifp->if_mtu = ETHERMTU; 1228 ifp->if_snd.ifq_maxlen = NET_RX_RING_SIZE - 1; 1229 1230 ifp->if_hwassist = XNB_CSUM_FEATURES; 1231 ifp->if_capabilities = IFCAP_HWCSUM; 1232 ifp->if_capenable = IFCAP_HWCSUM; 1233 1234 ether_ifattach(ifp, xnb->mac); 1235 xnb->carrier = 0; 1236 } 1237 1238 return err; 1239} 1240 1241/** 1242 * Attach to a XenBus device that has been claimed by our probe routine. 1243 * 1244 * \param dev NewBus device object representing this Xen Net Back instance. 1245 * 1246 * \return 0 for success, errno codes for failure. 1247 */ 1248static int 1249xnb_attach(device_t dev) 1250{ 1251 struct xnb_softc *xnb; 1252 int error; 1253 xnb_ring_type_t i; 1254 1255 error = create_netdev(dev); 1256 if (error != 0) { 1257 xenbus_dev_fatal(dev, error, "creating netdev"); 1258 return (error); 1259 } 1260 1261 DPRINTF("Attaching to %s\n", xenbus_get_node(dev)); 1262 1263 /* 1264 * Basic initialization. 1265 * After this block it is safe to call xnb_detach() 1266 * to clean up any allocated data for this instance. 1267 */ 1268 xnb = device_get_softc(dev); 1269 xnb->otherend_id = xenbus_get_otherend_id(dev); 1270 for (i=0; i < XNB_NUM_RING_TYPES; i++) { 1271 xnb->ring_configs[i].ring_pages = 1; 1272 } 1273 1274 /* 1275 * Setup sysctl variables. 1276 */ 1277 xnb_setup_sysctl(xnb); 1278 1279 /* Update hot-plug status to satisfy xend. */ 1280 error = xs_printf(XST_NIL, xenbus_get_node(xnb->dev), 1281 "hotplug-status", "connected"); 1282 if (error != 0) { 1283 xnb_attach_failed(xnb, error, "writing %s/hotplug-status", 1284 xenbus_get_node(xnb->dev)); 1285 return (error); 1286 } 1287 1288 if ((error = xnb_publish_backend_info(xnb)) != 0) { 1289 /* 1290 * If we can't publish our data, we cannot participate 1291 * in this connection, and waiting for a front-end state 1292 * change will not help the situation. 1293 */ 1294 xnb_attach_failed(xnb, error, 1295 "Publishing backend status for %s", 1296 xenbus_get_node(xnb->dev)); 1297 return error; 1298 } 1299 1300 /* Tell the front end that we are ready to connect. */ 1301 xenbus_set_state(dev, XenbusStateInitWait); 1302 1303 return (0); 1304} 1305 1306/** 1307 * Detach from a net back device instance. 1308 * 1309 * \param dev NewBus device object representing this Xen Net Back instance. 1310 * 1311 * \return 0 for success, errno codes for failure. 1312 * 1313 * \note A net back device may be detached at any time in its life-cycle, 1314 * including part way through the attach process. For this reason, 1315 * initialization order and the intialization state checks in this 1316 * routine must be carefully coupled so that attach time failures 1317 * are gracefully handled. 1318 */ 1319static int 1320xnb_detach(device_t dev) 1321{ 1322 struct xnb_softc *xnb; 1323 1324 DPRINTF("\n"); 1325 1326 xnb = device_get_softc(dev); 1327 mtx_lock(&xnb->sc_lock); 1328 while (xnb_shutdown(xnb) == EAGAIN) { 1329 msleep(xnb, &xnb->sc_lock, /*wakeup prio unchanged*/0, 1330 "xnb_shutdown", 0); 1331 } 1332 mtx_unlock(&xnb->sc_lock); 1333 DPRINTF("\n"); 1334 1335 mtx_destroy(&xnb->tx_lock); 1336 mtx_destroy(&xnb->rx_lock); 1337 mtx_destroy(&xnb->sc_lock); 1338 return (0); 1339} 1340 1341/** 1342 * Prepare this net back device for suspension of this VM. 1343 * 1344 * \param dev NewBus device object representing this Xen net Back instance. 1345 * 1346 * \return 0 for success, errno codes for failure. 1347 */ 1348static int 1349xnb_suspend(device_t dev) 1350{ 1351 return (0); 1352} 1353 1354/** 1355 * Perform any processing required to recover from a suspended state. 1356 * 1357 * \param dev NewBus device object representing this Xen Net Back instance. 1358 * 1359 * \return 0 for success, errno codes for failure. 1360 */ 1361static int 1362xnb_resume(device_t dev) 1363{ 1364 return (0); 1365} 1366 1367/** 1368 * Handle state changes expressed via the XenStore by our front-end peer. 1369 * 1370 * \param dev NewBus device object representing this Xen 1371 * Net Back instance. 1372 * \param frontend_state The new state of the front-end. 1373 * 1374 * \return 0 for success, errno codes for failure. 1375 */ 1376static void 1377xnb_frontend_changed(device_t dev, XenbusState frontend_state) 1378{ 1379 struct xnb_softc *xnb; 1380 1381 xnb = device_get_softc(dev); 1382 1383 DPRINTF("frontend_state=%s, xnb_state=%s\n", 1384 xenbus_strstate(frontend_state), 1385 xenbus_strstate(xenbus_get_state(xnb->dev))); 1386 1387 switch (frontend_state) { 1388 case XenbusStateInitialising: 1389 break; 1390 case XenbusStateInitialised: 1391 case XenbusStateConnected: 1392 xnb_connect(xnb); 1393 break; 1394 case XenbusStateClosing: 1395 case XenbusStateClosed: 1396 mtx_lock(&xnb->sc_lock); 1397 xnb_shutdown(xnb); 1398 mtx_unlock(&xnb->sc_lock); 1399 if (frontend_state == XenbusStateClosed) 1400 xenbus_set_state(xnb->dev, XenbusStateClosed); 1401 break; 1402 default: 1403 xenbus_dev_fatal(xnb->dev, EINVAL, "saw state %d at frontend", 1404 frontend_state); 1405 break; 1406 } 1407} 1408 1409 1410/*---------------------------- Request Processing ----------------------------*/ 1411/** 1412 * Interrupt handler bound to the shared ring's event channel. 1413 * Entry point for the xennet transmit path in netback 1414 * Transfers packets from the Xen ring to the host's generic networking stack 1415 * 1416 * \param arg Callback argument registerd during event channel 1417 * binding - the xnb_softc for this instance. 1418 */ 1419static void 1420xnb_intr(void *arg) 1421{ 1422 struct xnb_softc *xnb; 1423 struct ifnet *ifp; 1424 netif_tx_back_ring_t *txb; 1425 RING_IDX req_prod_local; 1426 1427 xnb = (struct xnb_softc *)arg; 1428 ifp = xnb->xnb_ifp; 1429 txb = &xnb->ring_configs[XNB_RING_TYPE_TX].back_ring.tx_ring; 1430 1431 mtx_lock(&xnb->tx_lock); 1432 do { 1433 int notify; 1434 req_prod_local = txb->sring->req_prod; 1435 xen_rmb(); 1436 1437 for (;;) { 1438 struct mbuf *mbufc; 1439 int err; 1440 1441 err = xnb_recv(txb, xnb->otherend_id, &mbufc, ifp, 1442 xnb->tx_gnttab); 1443 if (err || (mbufc == NULL)) 1444 break; 1445 1446 /* Send the packet to the generic network stack */ 1447 (*xnb->xnb_ifp->if_input)(xnb->xnb_ifp, mbufc); 1448 } 1449 1450 RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(txb, notify); 1451 if (notify != 0) 1452 xen_intr_signal(xnb->xen_intr_handle); 1453 1454 txb->sring->req_event = txb->req_cons + 1; 1455 xen_mb(); 1456 } while (txb->sring->req_prod != req_prod_local) ; 1457 mtx_unlock(&xnb->tx_lock); 1458 1459 xnb_start(ifp); 1460} 1461 1462 1463/** 1464 * Build a struct xnb_pkt based on netif_tx_request's from a netif tx ring. 1465 * Will read exactly 0 or 1 packets from the ring; never a partial packet. 1466 * \param[out] pkt The returned packet. If there is an error building 1467 * the packet, pkt.list_len will be set to 0. 1468 * \param[in] tx_ring Pointer to the Ring that is the input to this function 1469 * \param[in] start The ring index of the first potential request 1470 * \return The number of requests consumed to build this packet 1471 */ 1472static int 1473xnb_ring2pkt(struct xnb_pkt *pkt, const netif_tx_back_ring_t *tx_ring, 1474 RING_IDX start) 1475{ 1476 /* 1477 * Outline: 1478 * 1) Initialize pkt 1479 * 2) Read the first request of the packet 1480 * 3) Read the extras 1481 * 4) Set cdr 1482 * 5) Loop on the remainder of the packet 1483 * 6) Finalize pkt (stuff like car_size and list_len) 1484 */ 1485 int idx = start; 1486 int discard = 0; /* whether to discard the packet */ 1487 int more_data = 0; /* there are more request past the last one */ 1488 uint16_t cdr_size = 0; /* accumulated size of requests 2 through n */ 1489 1490 xnb_pkt_initialize(pkt); 1491 1492 /* Read the first request */ 1493 if (RING_HAS_UNCONSUMED_REQUESTS_2(tx_ring, idx)) { 1494 netif_tx_request_t *tx = RING_GET_REQUEST(tx_ring, idx); 1495 pkt->size = tx->size; 1496 pkt->flags = tx->flags & ~NETTXF_more_data; 1497 more_data = tx->flags & NETTXF_more_data; 1498 pkt->list_len++; 1499 pkt->car = idx; 1500 idx++; 1501 } 1502 1503 /* Read the extra info */ 1504 if ((pkt->flags & NETTXF_extra_info) && 1505 RING_HAS_UNCONSUMED_REQUESTS_2(tx_ring, idx)) { 1506 netif_extra_info_t *ext = 1507 (netif_extra_info_t*) RING_GET_REQUEST(tx_ring, idx); 1508 pkt->extra.type = ext->type; 1509 switch (pkt->extra.type) { 1510 case XEN_NETIF_EXTRA_TYPE_GSO: 1511 pkt->extra.u.gso = ext->u.gso; 1512 break; 1513 default: 1514 /* 1515 * The reference Linux netfront driver will 1516 * never set any other extra.type. So we don't 1517 * know what to do with it. Let's print an 1518 * error, then consume and discard the packet 1519 */ 1520 printf("xnb(%s:%d): Unknown extra info type %d." 1521 " Discarding packet\n", 1522 __func__, __LINE__, pkt->extra.type); 1523 xnb_dump_txreq(start, RING_GET_REQUEST(tx_ring, 1524 start)); 1525 xnb_dump_txreq(idx, RING_GET_REQUEST(tx_ring, 1526 idx)); 1527 discard = 1; 1528 break; 1529 } 1530 1531 pkt->extra.flags = ext->flags; 1532 if (ext->flags & XEN_NETIF_EXTRA_FLAG_MORE) { 1533 /* 1534 * The reference linux netfront driver never sets this 1535 * flag (nor does any other known netfront). So we 1536 * will discard the packet. 1537 */ 1538 printf("xnb(%s:%d): Request sets " 1539 "XEN_NETIF_EXTRA_FLAG_MORE, but we can't handle " 1540 "that\n", __func__, __LINE__); 1541 xnb_dump_txreq(start, RING_GET_REQUEST(tx_ring, start)); 1542 xnb_dump_txreq(idx, RING_GET_REQUEST(tx_ring, idx)); 1543 discard = 1; 1544 } 1545 1546 idx++; 1547 } 1548 1549 /* Set cdr. If there is not more data, cdr is invalid */ 1550 pkt->cdr = idx; 1551 1552 /* Loop on remainder of packet */ 1553 while (more_data && RING_HAS_UNCONSUMED_REQUESTS_2(tx_ring, idx)) { 1554 netif_tx_request_t *tx = RING_GET_REQUEST(tx_ring, idx); 1555 pkt->list_len++; 1556 cdr_size += tx->size; 1557 if (tx->flags & ~NETTXF_more_data) { 1558 /* There should be no other flags set at this point */ 1559 printf("xnb(%s:%d): Request sets unknown flags %d " 1560 "after the 1st request in the packet.\n", 1561 __func__, __LINE__, tx->flags); 1562 xnb_dump_txreq(start, RING_GET_REQUEST(tx_ring, start)); 1563 xnb_dump_txreq(idx, RING_GET_REQUEST(tx_ring, idx)); 1564 } 1565 1566 more_data = tx->flags & NETTXF_more_data; 1567 idx++; 1568 } 1569 1570 /* Finalize packet */ 1571 if (more_data != 0) { 1572 /* The ring ran out of requests before finishing the packet */ 1573 xnb_pkt_invalidate(pkt); 1574 idx = start; /* tell caller that we consumed no requests */ 1575 } else { 1576 /* Calculate car_size */ 1577 pkt->car_size = pkt->size - cdr_size; 1578 } 1579 if (discard != 0) { 1580 xnb_pkt_invalidate(pkt); 1581 } 1582 1583 return idx - start; 1584} 1585 1586 1587/** 1588 * Respond to all the requests that constituted pkt. Builds the responses and 1589 * writes them to the ring, but doesn't push them to the shared ring. 1590 * \param[in] pkt the packet that needs a response 1591 * \param[in] error true if there was an error handling the packet, such 1592 * as in the hypervisor copy op or mbuf allocation 1593 * \param[out] ring Responses go here 1594 */ 1595static void 1596xnb_txpkt2rsp(const struct xnb_pkt *pkt, netif_tx_back_ring_t *ring, 1597 int error) 1598{ 1599 /* 1600 * Outline: 1601 * 1) Respond to the first request 1602 * 2) Respond to the extra info reques 1603 * Loop through every remaining request in the packet, generating 1604 * responses that copy those requests' ids and sets the status 1605 * appropriately. 1606 */ 1607 netif_tx_request_t *tx; 1608 netif_tx_response_t *rsp; 1609 int i; 1610 uint16_t status; 1611 1612 status = (xnb_pkt_is_valid(pkt) == 0) || error ? 1613 NETIF_RSP_ERROR : NETIF_RSP_OKAY; 1614 KASSERT((pkt->list_len == 0) || (ring->rsp_prod_pvt == pkt->car), 1615 ("Cannot respond to ring requests out of order")); 1616 1617 if (pkt->list_len >= 1) { 1618 uint16_t id; 1619 tx = RING_GET_REQUEST(ring, ring->rsp_prod_pvt); 1620 id = tx->id; 1621 rsp = RING_GET_RESPONSE(ring, ring->rsp_prod_pvt); 1622 rsp->id = id; 1623 rsp->status = status; 1624 ring->rsp_prod_pvt++; 1625 1626 if (pkt->flags & NETRXF_extra_info) { 1627 rsp = RING_GET_RESPONSE(ring, ring->rsp_prod_pvt); 1628 rsp->status = NETIF_RSP_NULL; 1629 ring->rsp_prod_pvt++; 1630 } 1631 } 1632 1633 for (i=0; i < pkt->list_len - 1; i++) { 1634 uint16_t id; 1635 tx = RING_GET_REQUEST(ring, ring->rsp_prod_pvt); 1636 id = tx->id; 1637 rsp = RING_GET_RESPONSE(ring, ring->rsp_prod_pvt); 1638 rsp->id = id; 1639 rsp->status = status; 1640 ring->rsp_prod_pvt++; 1641 } 1642} 1643 1644/** 1645 * Create an mbuf chain to represent a packet. Initializes all of the headers 1646 * in the mbuf chain, but does not copy the data. The returned chain must be 1647 * free()'d when no longer needed 1648 * \param[in] pkt A packet to model the mbuf chain after 1649 * \return A newly allocated mbuf chain, possibly with clusters attached. 1650 * NULL on failure 1651 */ 1652static struct mbuf* 1653xnb_pkt2mbufc(const struct xnb_pkt *pkt, struct ifnet *ifp) 1654{ 1655 /** 1656 * \todo consider using a memory pool for mbufs instead of 1657 * reallocating them for every packet 1658 */ 1659 /** \todo handle extra data */ 1660 struct mbuf *m; 1661 1662 m = m_getm(NULL, pkt->size, M_NOWAIT, MT_DATA); 1663 1664 if (m != NULL) { 1665 m->m_pkthdr.rcvif = ifp; 1666 if (pkt->flags & NETTXF_data_validated) { 1667 /* 1668 * We lie to the host OS and always tell it that the 1669 * checksums are ok, because the packet is unlikely to 1670 * get corrupted going across domains. 1671 */ 1672 m->m_pkthdr.csum_flags = ( 1673 CSUM_IP_CHECKED | 1674 CSUM_IP_VALID | 1675 CSUM_DATA_VALID | 1676 CSUM_PSEUDO_HDR 1677 ); 1678 m->m_pkthdr.csum_data = 0xffff; 1679 } 1680 } 1681 return m; 1682} 1683 1684/** 1685 * Build a gnttab_copy table that can be used to copy data from a pkt 1686 * to an mbufc. Does not actually perform the copy. Always uses gref's on 1687 * the packet side. 1688 * \param[in] pkt pkt's associated requests form the src for 1689 * the copy operation 1690 * \param[in] mbufc mbufc's storage forms the dest for the copy operation 1691 * \param[out] gnttab Storage for the returned grant table 1692 * \param[in] txb Pointer to the backend ring structure 1693 * \param[in] otherend_id The domain ID of the other end of the copy 1694 * \return The number of gnttab entries filled 1695 */ 1696static int 1697xnb_txpkt2gnttab(const struct xnb_pkt *pkt, const struct mbuf *mbufc, 1698 gnttab_copy_table gnttab, const netif_tx_back_ring_t *txb, 1699 domid_t otherend_id) 1700{ 1701 1702 const struct mbuf *mbuf = mbufc;/* current mbuf within the chain */ 1703 int gnt_idx = 0; /* index into grant table */ 1704 RING_IDX r_idx = pkt->car; /* index into tx ring buffer */ 1705 int r_ofs = 0; /* offset of next data within tx request's data area */ 1706 int m_ofs = 0; /* offset of next data within mbuf's data area */ 1707 /* size in bytes that still needs to be represented in the table */ 1708 uint16_t size_remaining = pkt->size; 1709 1710 while (size_remaining > 0) { 1711 const netif_tx_request_t *txq = RING_GET_REQUEST(txb, r_idx); 1712 const size_t mbuf_space = M_TRAILINGSPACE(mbuf) - m_ofs; 1713 const size_t req_size = 1714 r_idx == pkt->car ? pkt->car_size : txq->size; 1715 const size_t pkt_space = req_size - r_ofs; 1716 /* 1717 * space is the largest amount of data that can be copied in the 1718 * grant table's next entry 1719 */ 1720 const size_t space = MIN(pkt_space, mbuf_space); 1721 1722 /* TODO: handle this error condition without panicking */ 1723 KASSERT(gnt_idx < GNTTAB_LEN, ("Grant table is too short")); 1724 1725 gnttab[gnt_idx].source.u.ref = txq->gref; 1726 gnttab[gnt_idx].source.domid = otherend_id; 1727 gnttab[gnt_idx].source.offset = txq->offset + r_ofs; 1728 gnttab[gnt_idx].dest.u.gmfn = virt_to_mfn( 1729 mtod(mbuf, vm_offset_t) + m_ofs); 1730 gnttab[gnt_idx].dest.offset = virt_to_offset( 1731 mtod(mbuf, vm_offset_t) + m_ofs); 1732 gnttab[gnt_idx].dest.domid = DOMID_SELF; 1733 gnttab[gnt_idx].len = space; 1734 gnttab[gnt_idx].flags = GNTCOPY_source_gref; 1735 1736 gnt_idx++; 1737 r_ofs += space; 1738 m_ofs += space; 1739 size_remaining -= space; 1740 if (req_size - r_ofs <= 0) { 1741 /* Must move to the next tx request */ 1742 r_ofs = 0; 1743 r_idx = (r_idx == pkt->car) ? pkt->cdr : r_idx + 1; 1744 } 1745 if (M_TRAILINGSPACE(mbuf) - m_ofs <= 0) { 1746 /* Must move to the next mbuf */ 1747 m_ofs = 0; 1748 mbuf = mbuf->m_next; 1749 } 1750 } 1751 1752 return gnt_idx; 1753} 1754 1755/** 1756 * Check the status of the grant copy operations, and update mbufs various 1757 * non-data fields to reflect the data present. 1758 * \param[in,out] mbufc mbuf chain to update. The chain must be valid and of 1759 * the correct length, and data should already be present 1760 * \param[in] gnttab A grant table for a just completed copy op 1761 * \param[in] n_entries The number of valid entries in the grant table 1762 */ 1763static void 1764xnb_update_mbufc(struct mbuf *mbufc, const gnttab_copy_table gnttab, 1765 int n_entries) 1766{ 1767 struct mbuf *mbuf = mbufc; 1768 int i; 1769 size_t total_size = 0; 1770 1771 for (i = 0; i < n_entries; i++) { 1772 KASSERT(gnttab[i].status == GNTST_okay, 1773 ("Some gnttab_copy entry had error status %hd\n", 1774 gnttab[i].status)); 1775 1776 mbuf->m_len += gnttab[i].len; 1777 total_size += gnttab[i].len; 1778 if (M_TRAILINGSPACE(mbuf) <= 0) { 1779 mbuf = mbuf->m_next; 1780 } 1781 } 1782 mbufc->m_pkthdr.len = total_size; 1783 1784#if defined(INET) || defined(INET6) 1785 xnb_add_mbuf_cksum(mbufc); 1786#endif 1787} 1788 1789/** 1790 * Dequeue at most one packet from the shared ring 1791 * \param[in,out] txb Netif tx ring. A packet will be removed from it, and 1792 * its private indices will be updated. But the indices 1793 * will not be pushed to the shared ring. 1794 * \param[in] ifnet Interface to which the packet will be sent 1795 * \param[in] otherend Domain ID of the other end of the ring 1796 * \param[out] mbufc The assembled mbuf chain, ready to send to the generic 1797 * networking stack 1798 * \param[in,out] gnttab Pointer to enough memory for a grant table. We make 1799 * this a function parameter so that we will take less 1800 * stack space. 1801 * \return An error code 1802 */ 1803static int 1804xnb_recv(netif_tx_back_ring_t *txb, domid_t otherend, struct mbuf **mbufc, 1805 struct ifnet *ifnet, gnttab_copy_table gnttab) 1806{ 1807 struct xnb_pkt pkt; 1808 /* number of tx requests consumed to build the last packet */ 1809 int num_consumed; 1810 int nr_ents; 1811 1812 *mbufc = NULL; 1813 num_consumed = xnb_ring2pkt(&pkt, txb, txb->req_cons); 1814 if (num_consumed == 0) 1815 return 0; /* Nothing to receive */ 1816 1817 /* update statistics independent of errors */ 1818 ifnet->if_ipackets++; 1819 1820 /* 1821 * if we got here, then 1 or more requests was consumed, but the packet 1822 * is not necessarily valid. 1823 */ 1824 if (xnb_pkt_is_valid(&pkt) == 0) { 1825 /* got a garbage packet, respond and drop it */ 1826 xnb_txpkt2rsp(&pkt, txb, 1); 1827 txb->req_cons += num_consumed; 1828 DPRINTF("xnb_intr: garbage packet, num_consumed=%d\n", 1829 num_consumed); 1830 ifnet->if_ierrors++; 1831 return EINVAL; 1832 } 1833 1834 *mbufc = xnb_pkt2mbufc(&pkt, ifnet); 1835 1836 if (*mbufc == NULL) { 1837 /* 1838 * Couldn't allocate mbufs. Respond and drop the packet. Do 1839 * not consume the requests 1840 */ 1841 xnb_txpkt2rsp(&pkt, txb, 1); 1842 DPRINTF("xnb_intr: Couldn't allocate mbufs, num_consumed=%d\n", 1843 num_consumed); 1844 ifnet->if_iqdrops++; 1845 return ENOMEM; 1846 } 1847 1848 nr_ents = xnb_txpkt2gnttab(&pkt, *mbufc, gnttab, txb, otherend); 1849 1850 if (nr_ents > 0) { 1851 int __unused hv_ret = HYPERVISOR_grant_table_op(GNTTABOP_copy, 1852 gnttab, nr_ents); 1853 KASSERT(hv_ret == 0, 1854 ("HYPERVISOR_grant_table_op returned %d\n", hv_ret)); 1855 xnb_update_mbufc(*mbufc, gnttab, nr_ents); 1856 } 1857 1858 xnb_txpkt2rsp(&pkt, txb, 0); 1859 txb->req_cons += num_consumed; 1860 return 0; 1861} 1862 1863/** 1864 * Create an xnb_pkt based on the contents of an mbuf chain. 1865 * \param[in] mbufc mbuf chain to transform into a packet 1866 * \param[out] pkt Storage for the newly generated xnb_pkt 1867 * \param[in] start The ring index of the first available slot in the rx 1868 * ring 1869 * \param[in] space The number of free slots in the rx ring 1870 * \retval 0 Success 1871 * \retval EINVAL mbufc was corrupt or not convertible into a pkt 1872 * \retval EAGAIN There was not enough space in the ring to queue the 1873 * packet 1874 */ 1875static int 1876xnb_mbufc2pkt(const struct mbuf *mbufc, struct xnb_pkt *pkt, 1877 RING_IDX start, int space) 1878{ 1879 1880 int retval = 0; 1881 1882 if ((mbufc == NULL) || 1883 ( (mbufc->m_flags & M_PKTHDR) == 0) || 1884 (mbufc->m_pkthdr.len == 0)) { 1885 xnb_pkt_invalidate(pkt); 1886 retval = EINVAL; 1887 } else { 1888 int slots_required; 1889 1890 xnb_pkt_validate(pkt); 1891 pkt->flags = 0; 1892 pkt->size = mbufc->m_pkthdr.len; 1893 pkt->car = start; 1894 pkt->car_size = mbufc->m_len; 1895 1896 if (mbufc->m_pkthdr.csum_flags & CSUM_TSO) { 1897 pkt->flags |= NETRXF_extra_info; 1898 pkt->extra.u.gso.size = mbufc->m_pkthdr.tso_segsz; 1899 pkt->extra.u.gso.type = XEN_NETIF_GSO_TYPE_TCPV4; 1900 pkt->extra.u.gso.pad = 0; 1901 pkt->extra.u.gso.features = 0; 1902 pkt->extra.type = XEN_NETIF_EXTRA_TYPE_GSO; 1903 pkt->extra.flags = 0; 1904 pkt->cdr = start + 2; 1905 } else { 1906 pkt->cdr = start + 1; 1907 } 1908 if (mbufc->m_pkthdr.csum_flags & (CSUM_TSO | CSUM_DELAY_DATA)) { 1909 pkt->flags |= 1910 (NETRXF_csum_blank | NETRXF_data_validated); 1911 } 1912 1913 /* 1914 * Each ring response can have up to PAGE_SIZE of data. 1915 * Assume that we can defragment the mbuf chain efficiently 1916 * into responses so that each response but the last uses all 1917 * PAGE_SIZE bytes. 1918 */ 1919 pkt->list_len = (pkt->size + PAGE_SIZE - 1) / PAGE_SIZE; 1920 1921 if (pkt->list_len > 1) { 1922 pkt->flags |= NETRXF_more_data; 1923 } 1924 1925 slots_required = pkt->list_len + 1926 (pkt->flags & NETRXF_extra_info ? 1 : 0); 1927 if (slots_required > space) { 1928 xnb_pkt_invalidate(pkt); 1929 retval = EAGAIN; 1930 } 1931 } 1932 1933 return retval; 1934} 1935 1936/** 1937 * Build a gnttab_copy table that can be used to copy data from an mbuf chain 1938 * to the frontend's shared buffers. Does not actually perform the copy. 1939 * Always uses gref's on the other end's side. 1940 * \param[in] pkt pkt's associated responses form the dest for the copy 1941 * operatoin 1942 * \param[in] mbufc The source for the copy operation 1943 * \param[out] gnttab Storage for the returned grant table 1944 * \param[in] rxb Pointer to the backend ring structure 1945 * \param[in] otherend_id The domain ID of the other end of the copy 1946 * \return The number of gnttab entries filled 1947 */ 1948static int 1949xnb_rxpkt2gnttab(const struct xnb_pkt *pkt, const struct mbuf *mbufc, 1950 gnttab_copy_table gnttab, const netif_rx_back_ring_t *rxb, 1951 domid_t otherend_id) 1952{ 1953 1954 const struct mbuf *mbuf = mbufc;/* current mbuf within the chain */ 1955 int gnt_idx = 0; /* index into grant table */ 1956 RING_IDX r_idx = pkt->car; /* index into rx ring buffer */ 1957 int r_ofs = 0; /* offset of next data within rx request's data area */ 1958 int m_ofs = 0; /* offset of next data within mbuf's data area */ 1959 /* size in bytes that still needs to be represented in the table */ 1960 uint16_t size_remaining; 1961 1962 size_remaining = (xnb_pkt_is_valid(pkt) != 0) ? pkt->size : 0; 1963 1964 while (size_remaining > 0) { 1965 const netif_rx_request_t *rxq = RING_GET_REQUEST(rxb, r_idx); 1966 const size_t mbuf_space = mbuf->m_len - m_ofs; 1967 /* Xen shared pages have an implied size of PAGE_SIZE */ 1968 const size_t req_size = PAGE_SIZE; 1969 const size_t pkt_space = req_size - r_ofs; 1970 /* 1971 * space is the largest amount of data that can be copied in the 1972 * grant table's next entry 1973 */ 1974 const size_t space = MIN(pkt_space, mbuf_space); 1975 1976 /* TODO: handle this error condition without panicing */ 1977 KASSERT(gnt_idx < GNTTAB_LEN, ("Grant table is too short")); 1978 1979 gnttab[gnt_idx].dest.u.ref = rxq->gref; 1980 gnttab[gnt_idx].dest.domid = otherend_id; 1981 gnttab[gnt_idx].dest.offset = r_ofs; 1982 gnttab[gnt_idx].source.u.gmfn = virt_to_mfn( 1983 mtod(mbuf, vm_offset_t) + m_ofs); 1984 gnttab[gnt_idx].source.offset = virt_to_offset( 1985 mtod(mbuf, vm_offset_t) + m_ofs); 1986 gnttab[gnt_idx].source.domid = DOMID_SELF; 1987 gnttab[gnt_idx].len = space; 1988 gnttab[gnt_idx].flags = GNTCOPY_dest_gref; 1989 1990 gnt_idx++; 1991 1992 r_ofs += space; 1993 m_ofs += space; 1994 size_remaining -= space; 1995 if (req_size - r_ofs <= 0) { 1996 /* Must move to the next rx request */ 1997 r_ofs = 0; 1998 r_idx = (r_idx == pkt->car) ? pkt->cdr : r_idx + 1; 1999 } 2000 if (mbuf->m_len - m_ofs <= 0) { 2001 /* Must move to the next mbuf */ 2002 m_ofs = 0; 2003 mbuf = mbuf->m_next; 2004 } 2005 } 2006 2007 return gnt_idx; 2008} 2009 2010/** 2011 * Generates responses for all the requests that constituted pkt. Builds 2012 * responses and writes them to the ring, but doesn't push the shared ring 2013 * indices. 2014 * \param[in] pkt the packet that needs a response 2015 * \param[in] gnttab The grant copy table corresponding to this packet. 2016 * Used to determine how many rsp->netif_rx_response_t's to 2017 * generate. 2018 * \param[in] n_entries Number of relevant entries in the grant table 2019 * \param[out] ring Responses go here 2020 * \return The number of RX requests that were consumed to generate 2021 * the responses 2022 */ 2023static int 2024xnb_rxpkt2rsp(const struct xnb_pkt *pkt, const gnttab_copy_table gnttab, 2025 int n_entries, netif_rx_back_ring_t *ring) 2026{ 2027 /* 2028 * This code makes the following assumptions: 2029 * * All entries in gnttab set GNTCOPY_dest_gref 2030 * * The entries in gnttab are grouped by their grefs: any two 2031 * entries with the same gref must be adjacent 2032 */ 2033 int error = 0; 2034 int gnt_idx, i; 2035 int n_responses = 0; 2036 grant_ref_t last_gref = GRANT_REF_INVALID; 2037 RING_IDX r_idx; 2038 2039 KASSERT(gnttab != NULL, ("Received a null granttable copy")); 2040 2041 /* 2042 * In the event of an error, we only need to send one response to the 2043 * netfront. In that case, we musn't write any data to the responses 2044 * after the one we send. So we must loop all the way through gnttab 2045 * looking for errors before we generate any responses 2046 * 2047 * Since we're looping through the grant table anyway, we'll count the 2048 * number of different gref's in it, which will tell us how many 2049 * responses to generate 2050 */ 2051 for (gnt_idx = 0; gnt_idx < n_entries; gnt_idx++) { 2052 int16_t status = gnttab[gnt_idx].status; 2053 if (status != GNTST_okay) { 2054 DPRINTF( 2055 "Got error %d for hypervisor gnttab_copy status\n", 2056 status); 2057 error = 1; 2058 break; 2059 } 2060 if (gnttab[gnt_idx].dest.u.ref != last_gref) { 2061 n_responses++; 2062 last_gref = gnttab[gnt_idx].dest.u.ref; 2063 } 2064 } 2065 2066 if (error != 0) { 2067 uint16_t id; 2068 netif_rx_response_t *rsp; 2069 2070 id = RING_GET_REQUEST(ring, ring->rsp_prod_pvt)->id; 2071 rsp = RING_GET_RESPONSE(ring, ring->rsp_prod_pvt); 2072 rsp->id = id; 2073 rsp->status = NETIF_RSP_ERROR; 2074 n_responses = 1; 2075 } else { 2076 gnt_idx = 0; 2077 const int has_extra = pkt->flags & NETRXF_extra_info; 2078 if (has_extra != 0) 2079 n_responses++; 2080 2081 for (i = 0; i < n_responses; i++) { 2082 netif_rx_request_t rxq; 2083 netif_rx_response_t *rsp; 2084 2085 r_idx = ring->rsp_prod_pvt + i; 2086 /* 2087 * We copy the structure of rxq instead of making a 2088 * pointer because it shares the same memory as rsp. 2089 */ 2090 rxq = *(RING_GET_REQUEST(ring, r_idx)); 2091 rsp = RING_GET_RESPONSE(ring, r_idx); 2092 if (has_extra && (i == 1)) { 2093 netif_extra_info_t *ext = 2094 (netif_extra_info_t*)rsp; 2095 ext->type = XEN_NETIF_EXTRA_TYPE_GSO; 2096 ext->flags = 0; 2097 ext->u.gso.size = pkt->extra.u.gso.size; 2098 ext->u.gso.type = XEN_NETIF_GSO_TYPE_TCPV4; 2099 ext->u.gso.pad = 0; 2100 ext->u.gso.features = 0; 2101 } else { 2102 rsp->id = rxq.id; 2103 rsp->status = GNTST_okay; 2104 rsp->offset = 0; 2105 rsp->flags = 0; 2106 if (i < pkt->list_len - 1) 2107 rsp->flags |= NETRXF_more_data; 2108 if ((i == 0) && has_extra) 2109 rsp->flags |= NETRXF_extra_info; 2110 if ((i == 0) && 2111 (pkt->flags & NETRXF_data_validated)) { 2112 rsp->flags |= NETRXF_data_validated; 2113 rsp->flags |= NETRXF_csum_blank; 2114 } 2115 rsp->status = 0; 2116 for (; gnttab[gnt_idx].dest.u.ref == rxq.gref; 2117 gnt_idx++) { 2118 rsp->status += gnttab[gnt_idx].len; 2119 } 2120 } 2121 } 2122 } 2123 2124 ring->req_cons += n_responses; 2125 ring->rsp_prod_pvt += n_responses; 2126 return n_responses; 2127} 2128 2129#if defined(INET) || defined(INET6) 2130/** 2131 * Add IP, TCP, and/or UDP checksums to every mbuf in a chain. The first mbuf 2132 * in the chain must start with a struct ether_header. 2133 * 2134 * XXX This function will perform incorrectly on UDP packets that are split up 2135 * into multiple ethernet frames. 2136 */ 2137static void 2138xnb_add_mbuf_cksum(struct mbuf *mbufc) 2139{ 2140 struct ether_header *eh; 2141 struct ip *iph; 2142 uint16_t ether_type; 2143 2144 eh = mtod(mbufc, struct ether_header*); 2145 ether_type = ntohs(eh->ether_type); 2146 if (ether_type != ETHERTYPE_IP) { 2147 /* Nothing to calculate */ 2148 return; 2149 } 2150 2151 iph = (struct ip*)(eh + 1); 2152 if (mbufc->m_pkthdr.csum_flags & CSUM_IP_VALID) { 2153 iph->ip_sum = 0; 2154 iph->ip_sum = in_cksum_hdr(iph); 2155 } 2156 2157 switch (iph->ip_p) { 2158 case IPPROTO_TCP: 2159 if (mbufc->m_pkthdr.csum_flags & CSUM_IP_VALID) { 2160 size_t tcplen = ntohs(iph->ip_len) - sizeof(struct ip); 2161 struct tcphdr *th = (struct tcphdr*)(iph + 1); 2162 th->th_sum = in_pseudo(iph->ip_src.s_addr, 2163 iph->ip_dst.s_addr, htons(IPPROTO_TCP + tcplen)); 2164 th->th_sum = in_cksum_skip(mbufc, 2165 sizeof(struct ether_header) + ntohs(iph->ip_len), 2166 sizeof(struct ether_header) + (iph->ip_hl << 2)); 2167 } 2168 break; 2169 case IPPROTO_UDP: 2170 if (mbufc->m_pkthdr.csum_flags & CSUM_IP_VALID) { 2171 size_t udplen = ntohs(iph->ip_len) - sizeof(struct ip); 2172 struct udphdr *uh = (struct udphdr*)(iph + 1); 2173 uh->uh_sum = in_pseudo(iph->ip_src.s_addr, 2174 iph->ip_dst.s_addr, htons(IPPROTO_UDP + udplen)); 2175 uh->uh_sum = in_cksum_skip(mbufc, 2176 sizeof(struct ether_header) + ntohs(iph->ip_len), 2177 sizeof(struct ether_header) + (iph->ip_hl << 2)); 2178 } 2179 break; 2180 default: 2181 break; 2182 } 2183} 2184#endif /* INET || INET6 */ 2185 2186static void 2187xnb_stop(struct xnb_softc *xnb) 2188{ 2189 struct ifnet *ifp; 2190 2191 mtx_assert(&xnb->sc_lock, MA_OWNED); 2192 ifp = xnb->xnb_ifp; 2193 ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE); 2194 if_link_state_change(ifp, LINK_STATE_DOWN); 2195} 2196 2197static int 2198xnb_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data) 2199{ 2200 struct xnb_softc *xnb = ifp->if_softc; 2201 struct ifreq *ifr = (struct ifreq*) data; 2202#ifdef INET 2203 struct ifaddr *ifa = (struct ifaddr*)data; 2204#endif 2205 int error = 0; 2206 2207 switch (cmd) { 2208 case SIOCSIFFLAGS: 2209 mtx_lock(&xnb->sc_lock); 2210 if (ifp->if_flags & IFF_UP) { 2211 xnb_ifinit_locked(xnb); 2212 } else { 2213 if (ifp->if_drv_flags & IFF_DRV_RUNNING) { 2214 xnb_stop(xnb); 2215 } 2216 } 2217 /* 2218 * Note: netfront sets a variable named xn_if_flags 2219 * here, but that variable is never read 2220 */ 2221 mtx_unlock(&xnb->sc_lock); 2222 break; 2223 case SIOCSIFADDR: 2224 case SIOCGIFADDR: 2225#ifdef INET 2226 mtx_lock(&xnb->sc_lock); 2227 if (ifa->ifa_addr->sa_family == AF_INET) { 2228 ifp->if_flags |= IFF_UP; 2229 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) { 2230 ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | 2231 IFF_DRV_OACTIVE); 2232 if_link_state_change(ifp, 2233 LINK_STATE_DOWN); 2234 ifp->if_drv_flags |= IFF_DRV_RUNNING; 2235 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 2236 if_link_state_change(ifp, 2237 LINK_STATE_UP); 2238 } 2239 arp_ifinit(ifp, ifa); 2240 mtx_unlock(&xnb->sc_lock); 2241 } else { 2242 mtx_unlock(&xnb->sc_lock); 2243#endif 2244 error = ether_ioctl(ifp, cmd, data); 2245#ifdef INET 2246 } 2247#endif 2248 break; 2249 case SIOCSIFCAP: 2250 mtx_lock(&xnb->sc_lock); 2251 if (ifr->ifr_reqcap & IFCAP_TXCSUM) { 2252 ifp->if_capenable |= IFCAP_TXCSUM; 2253 ifp->if_hwassist |= XNB_CSUM_FEATURES; 2254 } else { 2255 ifp->if_capenable &= ~(IFCAP_TXCSUM); 2256 ifp->if_hwassist &= ~(XNB_CSUM_FEATURES); 2257 } 2258 if ((ifr->ifr_reqcap & IFCAP_RXCSUM)) { 2259 ifp->if_capenable |= IFCAP_RXCSUM; 2260 } else { 2261 ifp->if_capenable &= ~(IFCAP_RXCSUM); 2262 } 2263 /* 2264 * TODO enable TSO4 and LRO once we no longer need 2265 * to calculate checksums in software 2266 */ 2267#if 0 2268 if (ifr->if_reqcap |= IFCAP_TSO4) { 2269 if (IFCAP_TXCSUM & ifp->if_capenable) { 2270 printf("xnb: Xen netif requires that " 2271 "TXCSUM be enabled in order " 2272 "to use TSO4\n"); 2273 error = EINVAL; 2274 } else { 2275 ifp->if_capenable |= IFCAP_TSO4; 2276 ifp->if_hwassist |= CSUM_TSO; 2277 } 2278 } else { 2279 ifp->if_capenable &= ~(IFCAP_TSO4); 2280 ifp->if_hwassist &= ~(CSUM_TSO); 2281 } 2282 if (ifr->ifreqcap |= IFCAP_LRO) { 2283 ifp->if_capenable |= IFCAP_LRO; 2284 } else { 2285 ifp->if_capenable &= ~(IFCAP_LRO); 2286 } 2287#endif 2288 mtx_unlock(&xnb->sc_lock); 2289 break; 2290 case SIOCSIFMTU: 2291 ifp->if_mtu = ifr->ifr_mtu; 2292 ifp->if_drv_flags &= ~IFF_DRV_RUNNING; 2293 xnb_ifinit(xnb); 2294 break; 2295 case SIOCADDMULTI: 2296 case SIOCDELMULTI: 2297 case SIOCSIFMEDIA: 2298 case SIOCGIFMEDIA: 2299 error = ifmedia_ioctl(ifp, ifr, &xnb->sc_media, cmd); 2300 break; 2301 default: 2302 error = ether_ioctl(ifp, cmd, data); 2303 break; 2304 } 2305 return (error); 2306} 2307 2308static void 2309xnb_start_locked(struct ifnet *ifp) 2310{ 2311 netif_rx_back_ring_t *rxb; 2312 struct xnb_softc *xnb; 2313 struct mbuf *mbufc; 2314 RING_IDX req_prod_local; 2315 2316 xnb = ifp->if_softc; 2317 rxb = &xnb->ring_configs[XNB_RING_TYPE_RX].back_ring.rx_ring; 2318 2319 if (!xnb->carrier) 2320 return; 2321 2322 do { 2323 int out_of_space = 0; 2324 int notify; 2325 req_prod_local = rxb->sring->req_prod; 2326 xen_rmb(); 2327 for (;;) { 2328 int error; 2329 2330 IF_DEQUEUE(&ifp->if_snd, mbufc); 2331 if (mbufc == NULL) 2332 break; 2333 error = xnb_send(rxb, xnb->otherend_id, mbufc, 2334 xnb->rx_gnttab); 2335 switch (error) { 2336 case EAGAIN: 2337 /* 2338 * Insufficient space in the ring. 2339 * Requeue pkt and send when space is 2340 * available. 2341 */ 2342 IF_PREPEND(&ifp->if_snd, mbufc); 2343 /* 2344 * Perhaps the frontend missed an IRQ 2345 * and went to sleep. Notify it to wake 2346 * it up. 2347 */ 2348 out_of_space = 1; 2349 break; 2350 2351 case EINVAL: 2352 /* OS gave a corrupt packet. Drop it.*/ 2353 ifp->if_oerrors++; 2354 /* FALLTHROUGH */ 2355 default: 2356 /* Send succeeded, or packet had error. 2357 * Free the packet */ 2358 ifp->if_opackets++; 2359 if (mbufc) 2360 m_freem(mbufc); 2361 break; 2362 } 2363 if (out_of_space != 0) 2364 break; 2365 } 2366 2367 RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(rxb, notify); 2368 if ((notify != 0) || (out_of_space != 0)) 2369 xen_intr_signal(xnb->xen_intr_handle); 2370 rxb->sring->req_event = req_prod_local + 1; 2371 xen_mb(); 2372 } while (rxb->sring->req_prod != req_prod_local) ; 2373} 2374 2375/** 2376 * Sends one packet to the ring. Blocks until the packet is on the ring 2377 * \param[in] mbufc Contains one packet to send. Caller must free 2378 * \param[in,out] rxb The packet will be pushed onto this ring, but the 2379 * otherend will not be notified. 2380 * \param[in] otherend The domain ID of the other end of the connection 2381 * \retval EAGAIN The ring did not have enough space for the packet. 2382 * The ring has not been modified 2383 * \param[in,out] gnttab Pointer to enough memory for a grant table. We make 2384 * this a function parameter so that we will take less 2385 * stack space. 2386 * \retval EINVAL mbufc was corrupt or not convertible into a pkt 2387 */ 2388static int 2389xnb_send(netif_rx_back_ring_t *ring, domid_t otherend, const struct mbuf *mbufc, 2390 gnttab_copy_table gnttab) 2391{ 2392 struct xnb_pkt pkt; 2393 int error, n_entries, n_reqs; 2394 RING_IDX space; 2395 2396 space = ring->sring->req_prod - ring->req_cons; 2397 error = xnb_mbufc2pkt(mbufc, &pkt, ring->rsp_prod_pvt, space); 2398 if (error != 0) 2399 return error; 2400 n_entries = xnb_rxpkt2gnttab(&pkt, mbufc, gnttab, ring, otherend); 2401 if (n_entries != 0) { 2402 int __unused hv_ret = HYPERVISOR_grant_table_op(GNTTABOP_copy, 2403 gnttab, n_entries); 2404 KASSERT(hv_ret == 0, ("HYPERVISOR_grant_table_op returned %d\n", 2405 hv_ret)); 2406 } 2407 2408 n_reqs = xnb_rxpkt2rsp(&pkt, gnttab, n_entries, ring); 2409 2410 return 0; 2411} 2412 2413static void 2414xnb_start(struct ifnet *ifp) 2415{ 2416 struct xnb_softc *xnb; 2417 2418 xnb = ifp->if_softc; 2419 mtx_lock(&xnb->rx_lock); 2420 xnb_start_locked(ifp); 2421 mtx_unlock(&xnb->rx_lock); 2422} 2423 2424/* equivalent of network_open() in Linux */ 2425static void 2426xnb_ifinit_locked(struct xnb_softc *xnb) 2427{ 2428 struct ifnet *ifp; 2429 2430 ifp = xnb->xnb_ifp; 2431 2432 mtx_assert(&xnb->sc_lock, MA_OWNED); 2433 2434 if (ifp->if_drv_flags & IFF_DRV_RUNNING) 2435 return; 2436 2437 xnb_stop(xnb); 2438 2439 ifp->if_drv_flags |= IFF_DRV_RUNNING; 2440 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 2441 if_link_state_change(ifp, LINK_STATE_UP); 2442} 2443 2444 2445static void 2446xnb_ifinit(void *xsc) 2447{ 2448 struct xnb_softc *xnb = xsc; 2449 2450 mtx_lock(&xnb->sc_lock); 2451 xnb_ifinit_locked(xnb); 2452 mtx_unlock(&xnb->sc_lock); 2453} 2454 2455 2456/** 2457 * Read the 'mac' node at the given device's node in the store, and parse that 2458 * as colon-separated octets, placing result the given mac array. mac must be 2459 * a preallocated array of length ETHER_ADDR_LEN ETH_ALEN (as declared in 2460 * net/ethernet.h). 2461 * Return 0 on success, or errno on error. 2462 */ 2463static int 2464xen_net_read_mac(device_t dev, uint8_t mac[]) 2465{ 2466 char *s, *e, *macstr; 2467 const char *path; 2468 int error = 0; 2469 int i; 2470 2471 path = xenbus_get_node(dev); 2472 error = xs_read(XST_NIL, path, "mac", NULL, (void **) &macstr); 2473 if (error != 0) { 2474 xenbus_dev_fatal(dev, error, "parsing %s/mac", path); 2475 } else { 2476 s = macstr; 2477 for (i = 0; i < ETHER_ADDR_LEN; i++) { 2478 mac[i] = strtoul(s, &e, 16); 2479 if (s == e || (e[0] != ':' && e[0] != 0)) { 2480 error = ENOENT; 2481 break; 2482 } 2483 s = &e[1]; 2484 } 2485 free(macstr, M_XENBUS); 2486 } 2487 return error; 2488} 2489 2490 2491/** 2492 * Callback used by the generic networking code to tell us when our carrier 2493 * state has changed. Since we don't have a physical carrier, we don't care 2494 */ 2495static int 2496xnb_ifmedia_upd(struct ifnet *ifp) 2497{ 2498 return (0); 2499} 2500 2501/** 2502 * Callback used by the generic networking code to ask us what our carrier 2503 * state is. Since we don't have a physical carrier, this is very simple 2504 */ 2505static void 2506xnb_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr) 2507{ 2508 ifmr->ifm_status = IFM_AVALID|IFM_ACTIVE; 2509 ifmr->ifm_active = IFM_ETHER|IFM_MANUAL; 2510} 2511 2512 2513/*---------------------------- NewBus Registration ---------------------------*/ 2514static device_method_t xnb_methods[] = { 2515 /* Device interface */ 2516 DEVMETHOD(device_probe, xnb_probe), 2517 DEVMETHOD(device_attach, xnb_attach), 2518 DEVMETHOD(device_detach, xnb_detach), 2519 DEVMETHOD(device_shutdown, bus_generic_shutdown), 2520 DEVMETHOD(device_suspend, xnb_suspend), 2521 DEVMETHOD(device_resume, xnb_resume), 2522 2523 /* Xenbus interface */ 2524 DEVMETHOD(xenbus_otherend_changed, xnb_frontend_changed), 2525 2526 { 0, 0 } 2527}; 2528 2529static driver_t xnb_driver = { 2530 "xnb", 2531 xnb_methods, 2532 sizeof(struct xnb_softc), 2533}; 2534devclass_t xnb_devclass; 2535 2536DRIVER_MODULE(xnb, xenbusb_back, xnb_driver, xnb_devclass, 0, 0); 2537 2538 2539/*-------------------------- Unit Tests -------------------------------------*/ 2540#ifdef XNB_DEBUG 2541#include "netback_unit_tests.c" 2542#endif 2543