netback.c revision 271849
1230587Sken/*- 2230587Sken * Copyright (c) 2009-2011 Spectra Logic Corporation 3181643Skmacy * All rights reserved. 4181643Skmacy * 5230587Sken * Redistribution and use in source and binary forms, with or without 6230587Sken * modification, are permitted provided that the following conditions 7181643Skmacy * are met: 8230587Sken * 1. Redistributions of source code must retain the above copyright 9230587Sken * notice, this list of conditions, and the following disclaimer, 10230587Sken * without modification. 11230587Sken * 2. Redistributions in binary form must reproduce at minimum a disclaimer 12230587Sken * substantially similar to the "NO WARRANTY" disclaimer below 13230587Sken * ("Disclaimer") and any redistribution must be conditioned upon 14230587Sken * including a substantially similar Disclaimer requirement for further 15230587Sken * binary redistribution. 16181643Skmacy * 17230587Sken * NO WARRANTY 18230587Sken * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19230587Sken * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20230587Sken * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR 21230587Sken * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22230587Sken * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23230587Sken * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24230587Sken * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25230587Sken * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 26230587Sken * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 27230587Sken * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 28230587Sken * POSSIBILITY OF SUCH DAMAGES. 29181643Skmacy * 30230587Sken * Authors: Justin T. Gibbs (Spectra Logic Corporation) 31230587Sken * Alan Somers (Spectra Logic Corporation) 32230587Sken * John Suykerbuyk (Spectra Logic Corporation) 33181643Skmacy */ 34181643Skmacy 35181643Skmacy#include <sys/cdefs.h> 36181643Skmacy__FBSDID("$FreeBSD: head/sys/dev/xen/netback/netback.c 271849 2014-09-19 03:51:26Z glebius $"); 37230587Sken 38230587Sken/** 39230587Sken * \file netback.c 40230587Sken * 41230587Sken * \brief Device driver supporting the vending of network access 42230587Sken * from this FreeBSD domain to other domains. 43230587Sken */ 44230587Sken#include "opt_inet.h" 45257515Sglebius#include "opt_inet6.h" 46230587Sken#include "opt_global.h" 47230587Sken 48188066Srrs#include "opt_sctp.h" 49181643Skmacy 50181643Skmacy#include <sys/param.h> 51181643Skmacy#include <sys/kernel.h> 52181643Skmacy 53230587Sken#include <sys/bus.h> 54181643Skmacy#include <sys/module.h> 55230587Sken#include <sys/rman.h> 56230587Sken#include <sys/socket.h> 57230587Sken#include <sys/sockio.h> 58181643Skmacy#include <sys/sysctl.h> 59181643Skmacy 60181643Skmacy#include <net/if.h> 61257275Sglebius#include <net/if_var.h> 62181643Skmacy#include <net/if_arp.h> 63230587Sken#include <net/ethernet.h> 64230587Sken#include <net/if_dl.h> 65230587Sken#include <net/if_media.h> 66181643Skmacy#include <net/if_types.h> 67181643Skmacy 68181643Skmacy#include <netinet/in.h> 69181643Skmacy#include <netinet/ip.h> 70230587Sken#include <netinet/if_ether.h> 71230587Sken#if __FreeBSD_version >= 700000 72181643Skmacy#include <netinet/tcp.h> 73230587Sken#endif 74230587Sken#include <netinet/ip_icmp.h> 75181643Skmacy#include <netinet/udp.h> 76230587Sken#include <machine/in_cksum.h> 77181643Skmacy 78230587Sken#include <vm/vm.h> 79230587Sken#include <vm/pmap.h> 80230916Sken#include <vm/vm_extern.h> 81230916Sken#include <vm/vm_kern.h> 82181643Skmacy 83230587Sken#include <machine/_inttypes.h> 84181643Skmacy 85255040Sgibbs#include <xen/xen-os.h> 86255040Sgibbs#include <xen/hypervisor.h> 87230587Sken#include <xen/xen_intr.h> 88230587Sken#include <xen/interface/io/netif.h> 89230587Sken#include <xen/xenbus/xenbusvar.h> 90181643Skmacy 91255040Sgibbs#include <machine/xen/xenvar.h> 92255040Sgibbs 93230587Sken/*--------------------------- Compile-time Tunables --------------------------*/ 94181643Skmacy 95230587Sken/*---------------------------------- Macros ----------------------------------*/ 96230587Sken/** 97230587Sken * Custom malloc type for all driver allocations. 98230587Sken */ 99230587Skenstatic MALLOC_DEFINE(M_XENNETBACK, "xnb", "Xen Net Back Driver Data"); 100230587Sken 101230587Sken#define XNB_SG 1 /* netback driver supports feature-sg */ 102230587Sken#define XNB_GSO_TCPV4 1 /* netback driver supports feature-gso-tcpv4 */ 103230587Sken#define XNB_RX_COPY 1 /* netback driver supports feature-rx-copy */ 104230587Sken#define XNB_RX_FLIP 0 /* netback driver does not support feature-rx-flip */ 105230587Sken 106230587Sken#undef XNB_DEBUG 107230587Sken#define XNB_DEBUG /* hardcode on during development */ 108230587Sken 109230587Sken#ifdef XNB_DEBUG 110230587Sken#define DPRINTF(fmt, args...) \ 111230587Sken printf("xnb(%s:%d): " fmt, __FUNCTION__, __LINE__, ##args) 112181643Skmacy#else 113230587Sken#define DPRINTF(fmt, args...) do {} while (0) 114181643Skmacy#endif 115181643Skmacy 116230587Sken/* Default length for stack-allocated grant tables */ 117230587Sken#define GNTTAB_LEN (64) 118181643Skmacy 119230587Sken/* Features supported by all backends. TSO and LRO can be negotiated */ 120230587Sken#define XNB_CSUM_FEATURES (CSUM_TCP | CSUM_UDP) 121181643Skmacy 122230587Sken#define NET_TX_RING_SIZE __RING_SIZE((netif_tx_sring_t *)0, PAGE_SIZE) 123230587Sken#define NET_RX_RING_SIZE __RING_SIZE((netif_rx_sring_t *)0, PAGE_SIZE) 124181643Skmacy 125230587Sken/** 126230587Sken * Two argument version of the standard macro. Second argument is a tentative 127230587Sken * value of req_cons 128230587Sken */ 129230587Sken#define RING_HAS_UNCONSUMED_REQUESTS_2(_r, cons) ({ \ 130230587Sken unsigned int req = (_r)->sring->req_prod - cons; \ 131230587Sken unsigned int rsp = RING_SIZE(_r) - \ 132230587Sken (cons - (_r)->rsp_prod_pvt); \ 133230587Sken req < rsp ? req : rsp; \ 134230587Sken}) 135181643Skmacy 136230587Sken#define virt_to_mfn(x) (vtomach(x) >> PAGE_SHIFT) 137230587Sken#define virt_to_offset(x) ((x) & (PAGE_SIZE - 1)) 138181643Skmacy 139230587Sken/** 140230587Sken * Predefined array type of grant table copy descriptors. Used to pass around 141230587Sken * statically allocated memory structures. 142230587Sken */ 143230587Skentypedef struct gnttab_copy gnttab_copy_table[GNTTAB_LEN]; 144181643Skmacy 145230587Sken/*--------------------------- Forward Declarations ---------------------------*/ 146230587Skenstruct xnb_softc; 147230587Skenstruct xnb_pkt; 148181643Skmacy 149230587Skenstatic void xnb_attach_failed(struct xnb_softc *xnb, 150230587Sken int err, const char *fmt, ...) 151230587Sken __printflike(3,4); 152230587Skenstatic int xnb_shutdown(struct xnb_softc *xnb); 153230587Skenstatic int create_netdev(device_t dev); 154230587Skenstatic int xnb_detach(device_t dev); 155230587Skenstatic int xnb_ifmedia_upd(struct ifnet *ifp); 156230587Skenstatic void xnb_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr); 157230587Skenstatic void xnb_intr(void *arg); 158230587Skenstatic int xnb_send(netif_rx_back_ring_t *rxb, domid_t otherend, 159230587Sken const struct mbuf *mbufc, gnttab_copy_table gnttab); 160230587Skenstatic int xnb_recv(netif_tx_back_ring_t *txb, domid_t otherend, 161230587Sken struct mbuf **mbufc, struct ifnet *ifnet, 162230587Sken gnttab_copy_table gnttab); 163230587Skenstatic int xnb_ring2pkt(struct xnb_pkt *pkt, 164230587Sken const netif_tx_back_ring_t *tx_ring, 165230587Sken RING_IDX start); 166230587Skenstatic void xnb_txpkt2rsp(const struct xnb_pkt *pkt, 167230587Sken netif_tx_back_ring_t *ring, int error); 168230587Skenstatic struct mbuf *xnb_pkt2mbufc(const struct xnb_pkt *pkt, struct ifnet *ifp); 169230587Skenstatic int xnb_txpkt2gnttab(const struct xnb_pkt *pkt, 170230587Sken const struct mbuf *mbufc, 171230587Sken gnttab_copy_table gnttab, 172230587Sken const netif_tx_back_ring_t *txb, 173230587Sken domid_t otherend_id); 174230587Skenstatic void xnb_update_mbufc(struct mbuf *mbufc, 175230587Sken const gnttab_copy_table gnttab, int n_entries); 176230587Skenstatic int xnb_mbufc2pkt(const struct mbuf *mbufc, 177230587Sken struct xnb_pkt *pkt, 178230587Sken RING_IDX start, int space); 179230587Skenstatic int xnb_rxpkt2gnttab(const struct xnb_pkt *pkt, 180230587Sken const struct mbuf *mbufc, 181230587Sken gnttab_copy_table gnttab, 182230587Sken const netif_rx_back_ring_t *rxb, 183230587Sken domid_t otherend_id); 184230587Skenstatic int xnb_rxpkt2rsp(const struct xnb_pkt *pkt, 185230587Sken const gnttab_copy_table gnttab, int n_entries, 186230587Sken netif_rx_back_ring_t *ring); 187230587Skenstatic void xnb_stop(struct xnb_softc*); 188230587Skenstatic int xnb_ioctl(struct ifnet*, u_long, caddr_t); 189230587Skenstatic void xnb_start_locked(struct ifnet*); 190230587Skenstatic void xnb_start(struct ifnet*); 191230587Skenstatic void xnb_ifinit_locked(struct xnb_softc*); 192230587Skenstatic void xnb_ifinit(void*); 193230587Sken#ifdef XNB_DEBUG 194230587Skenstatic int xnb_unit_test_main(SYSCTL_HANDLER_ARGS); 195230587Skenstatic int xnb_dump_rings(SYSCTL_HANDLER_ARGS); 196230587Sken#endif 197257515Sglebius#if defined(INET) || defined(INET6) 198257515Sglebiusstatic void xnb_add_mbuf_cksum(struct mbuf *mbufc); 199257515Sglebius#endif 200230587Sken/*------------------------------ Data Structures -----------------------------*/ 201181643Skmacy 202181643Skmacy 203230587Sken/** 204230587Sken * Representation of a xennet packet. Simplified version of a packet as 205230587Sken * stored in the Xen tx ring. Applicable to both RX and TX packets 206230587Sken */ 207230587Skenstruct xnb_pkt{ 208230587Sken /** 209230587Sken * Array index of the first data-bearing (eg, not extra info) entry 210230587Sken * for this packet 211230587Sken */ 212230587Sken RING_IDX car; 213181643Skmacy 214230587Sken /** 215230587Sken * Array index of the second data-bearing entry for this packet. 216230587Sken * Invalid if the packet has only one data-bearing entry. If the 217230587Sken * packet has more than two data-bearing entries, then the second 218230587Sken * through the last will be sequential modulo the ring size 219230587Sken */ 220230587Sken RING_IDX cdr; 221181643Skmacy 222230587Sken /** 223230587Sken * Optional extra info. Only valid if flags contains 224230587Sken * NETTXF_extra_info. Note that extra.type will always be 225230587Sken * XEN_NETIF_EXTRA_TYPE_GSO. Currently, no known netfront or netback 226230587Sken * driver will ever set XEN_NETIF_EXTRA_TYPE_MCAST_* 227230587Sken */ 228230587Sken netif_extra_info_t extra; 229181643Skmacy 230230587Sken /** Size of entire packet in bytes. */ 231230587Sken uint16_t size; 232181643Skmacy 233230587Sken /** The size of the first entry's data in bytes */ 234230587Sken uint16_t car_size; 235181643Skmacy 236230587Sken /** 237230587Sken * Either NETTXF_ or NETRXF_ flags. Note that the flag values are 238230587Sken * not the same for TX and RX packets 239230587Sken */ 240230587Sken uint16_t flags; 241181643Skmacy 242230587Sken /** 243230587Sken * The number of valid data-bearing entries (either netif_tx_request's 244230587Sken * or netif_rx_response's) in the packet. If this is 0, it means the 245230587Sken * entire packet is invalid. 246230587Sken */ 247230587Sken uint16_t list_len; 248181643Skmacy 249230587Sken /** There was an error processing the packet */ 250230587Sken uint8_t error; 251230587Sken}; 252181643Skmacy 253230587Sken/** xnb_pkt method: initialize it */ 254230587Skenstatic inline void 255230587Skenxnb_pkt_initialize(struct xnb_pkt *pxnb) 256230587Sken{ 257230587Sken bzero(pxnb, sizeof(*pxnb)); 258230587Sken} 259181643Skmacy 260230587Sken/** xnb_pkt method: mark the packet as valid */ 261230587Skenstatic inline void 262230587Skenxnb_pkt_validate(struct xnb_pkt *pxnb) 263230587Sken{ 264230587Sken pxnb->error = 0; 265230587Sken}; 266181643Skmacy 267230587Sken/** xnb_pkt method: mark the packet as invalid */ 268230587Skenstatic inline void 269230587Skenxnb_pkt_invalidate(struct xnb_pkt *pxnb) 270230587Sken{ 271230587Sken pxnb->error = 1; 272230587Sken}; 273181643Skmacy 274230587Sken/** xnb_pkt method: Check whether the packet is valid */ 275230587Skenstatic inline int 276230587Skenxnb_pkt_is_valid(const struct xnb_pkt *pxnb) 277230587Sken{ 278230587Sken return (! pxnb->error); 279230587Sken} 280181643Skmacy 281230587Sken#ifdef XNB_DEBUG 282230587Sken/** xnb_pkt method: print the packet's contents in human-readable format*/ 283230587Skenstatic void __unused 284230587Skenxnb_dump_pkt(const struct xnb_pkt *pkt) { 285230587Sken if (pkt == NULL) { 286230587Sken DPRINTF("Was passed a null pointer.\n"); 287230587Sken return; 288230587Sken } 289230587Sken DPRINTF("pkt address= %p\n", pkt); 290230587Sken DPRINTF("pkt->size=%d\n", pkt->size); 291230587Sken DPRINTF("pkt->car_size=%d\n", pkt->car_size); 292230587Sken DPRINTF("pkt->flags=0x%04x\n", pkt->flags); 293230587Sken DPRINTF("pkt->list_len=%d\n", pkt->list_len); 294230587Sken /* DPRINTF("pkt->extra"); TODO */ 295230587Sken DPRINTF("pkt->car=%d\n", pkt->car); 296230587Sken DPRINTF("pkt->cdr=%d\n", pkt->cdr); 297230587Sken DPRINTF("pkt->error=%d\n", pkt->error); 298230587Sken} 299230587Sken#endif /* XNB_DEBUG */ 300181643Skmacy 301181643Skmacystatic void 302230587Skenxnb_dump_txreq(RING_IDX idx, const struct netif_tx_request *txreq) 303181643Skmacy{ 304230587Sken if (txreq != NULL) { 305230587Sken DPRINTF("netif_tx_request index =%u\n", idx); 306230587Sken DPRINTF("netif_tx_request.gref =%u\n", txreq->gref); 307230587Sken DPRINTF("netif_tx_request.offset=%hu\n", txreq->offset); 308230587Sken DPRINTF("netif_tx_request.flags =%hu\n", txreq->flags); 309230587Sken DPRINTF("netif_tx_request.id =%hu\n", txreq->id); 310230587Sken DPRINTF("netif_tx_request.size =%hu\n", txreq->size); 311181643Skmacy } 312181643Skmacy} 313181643Skmacy 314181643Skmacy 315230587Sken/** 316230587Sken * \brief Configuration data for a shared memory request ring 317230587Sken * used to communicate with the front-end client of this 318230587Sken * this driver. 319230587Sken */ 320230587Skenstruct xnb_ring_config { 321230587Sken /** 322230587Sken * Runtime structures for ring access. Unfortunately, TX and RX rings 323230587Sken * use different data structures, and that cannot be changed since it 324230587Sken * is part of the interdomain protocol. 325230587Sken */ 326230587Sken union{ 327230587Sken netif_rx_back_ring_t rx_ring; 328230587Sken netif_tx_back_ring_t tx_ring; 329230587Sken } back_ring; 330181643Skmacy 331230587Sken /** 332230587Sken * The device bus address returned by the hypervisor when 333230587Sken * mapping the ring and required to unmap it when a connection 334230587Sken * is torn down. 335230587Sken */ 336230587Sken uint64_t bus_addr; 337181643Skmacy 338230587Sken /** The pseudo-physical address where ring memory is mapped.*/ 339230587Sken uint64_t gnt_addr; 340230587Sken 341230587Sken /** KVA address where ring memory is mapped. */ 342230587Sken vm_offset_t va; 343230587Sken 344230587Sken /** 345230587Sken * Grant table handles, one per-ring page, returned by the 346230587Sken * hyperpervisor upon mapping of the ring and required to 347230587Sken * unmap it when a connection is torn down. 348230587Sken */ 349230587Sken grant_handle_t handle; 350230587Sken 351230587Sken /** The number of ring pages mapped for the current connection. */ 352230587Sken unsigned ring_pages; 353230587Sken 354230587Sken /** 355230587Sken * The grant references, one per-ring page, supplied by the 356230587Sken * front-end, allowing us to reference the ring pages in the 357230587Sken * front-end's domain and to map these pages into our own domain. 358230587Sken */ 359230587Sken grant_ref_t ring_ref; 360230587Sken}; 361230587Sken 362230587Sken/** 363230587Sken * Per-instance connection state flags. 364230587Sken */ 365230587Skentypedef enum 366181643Skmacy{ 367230587Sken /** Communication with the front-end has been established. */ 368230587Sken XNBF_RING_CONNECTED = 0x01, 369181643Skmacy 370230587Sken /** 371230587Sken * Front-end requests exist in the ring and are waiting for 372230587Sken * xnb_xen_req objects to free up. 373230587Sken */ 374230587Sken XNBF_RESOURCE_SHORTAGE = 0x02, 375181643Skmacy 376230587Sken /** Connection teardown has started. */ 377230587Sken XNBF_SHUTDOWN = 0x04, 378181643Skmacy 379230587Sken /** A thread is already performing shutdown processing. */ 380230587Sken XNBF_IN_SHUTDOWN = 0x08 381230587Sken} xnb_flag_t; 382181643Skmacy 383230587Sken/** 384230587Sken * Types of rings. Used for array indices and to identify a ring's control 385230587Sken * data structure type 386230587Sken */ 387230587Skentypedef enum{ 388230587Sken XNB_RING_TYPE_TX = 0, /* ID of TX rings, used for array indices */ 389230587Sken XNB_RING_TYPE_RX = 1, /* ID of RX rings, used for array indices */ 390230587Sken XNB_NUM_RING_TYPES 391230587Sken} xnb_ring_type_t; 392181643Skmacy 393230587Sken/** 394230587Sken * Per-instance configuration data. 395230587Sken */ 396230587Skenstruct xnb_softc { 397230587Sken /** NewBus device corresponding to this instance. */ 398230587Sken device_t dev; 399181643Skmacy 400230587Sken /* Media related fields */ 401181643Skmacy 402230587Sken /** Generic network media state */ 403230587Sken struct ifmedia sc_media; 404181643Skmacy 405230587Sken /** Media carrier info */ 406230587Sken struct ifnet *xnb_ifp; 407181643Skmacy 408230587Sken /** Our own private carrier state */ 409230587Sken unsigned carrier; 410181643Skmacy 411230587Sken /** Device MAC Address */ 412230587Sken uint8_t mac[ETHER_ADDR_LEN]; 413181643Skmacy 414230587Sken /* Xen related fields */ 415181643Skmacy 416230587Sken /** 417230587Sken * \brief The netif protocol abi in effect. 418230587Sken * 419230587Sken * There are situations where the back and front ends can 420230587Sken * have a different, native abi (e.g. intel x86_64 and 421230587Sken * 32bit x86 domains on the same machine). The back-end 422230587Sken * always accomodates the front-end's native abi. That 423230587Sken * value is pulled from the XenStore and recorded here. 424230587Sken */ 425230587Sken int abi; 426181643Skmacy 427230587Sken /** 428230587Sken * Name of the bridge to which this VIF is connected, if any 429230587Sken * This field is dynamically allocated by xenbus and must be free()ed 430230587Sken * when no longer needed 431230587Sken */ 432230587Sken char *bridge; 433181643Skmacy 434230587Sken /** The interrupt driven even channel used to signal ring events. */ 435230587Sken evtchn_port_t evtchn; 436181643Skmacy 437230587Sken /** Xen device handle.*/ 438230587Sken long handle; 439181643Skmacy 440255040Sgibbs /** Handle to the communication ring event channel. */ 441255040Sgibbs xen_intr_handle_t xen_intr_handle; 442181643Skmacy 443230587Sken /** 444230587Sken * \brief Cached value of the front-end's domain id. 445230587Sken * 446230587Sken * This value is used at once for each mapped page in 447230587Sken * a transaction. We cache it to avoid incuring the 448230587Sken * cost of an ivar access every time this is needed. 449230587Sken */ 450230587Sken domid_t otherend_id; 451181643Skmacy 452230587Sken /** 453230587Sken * Undocumented frontend feature. Has something to do with 454230587Sken * scatter/gather IO 455230587Sken */ 456230587Sken uint8_t can_sg; 457230587Sken /** Undocumented frontend feature */ 458230587Sken uint8_t gso; 459230587Sken /** Undocumented frontend feature */ 460230587Sken uint8_t gso_prefix; 461230587Sken /** Can checksum TCP/UDP over IPv4 */ 462230587Sken uint8_t ip_csum; 463181643Skmacy 464230587Sken /* Implementation related fields */ 465230587Sken /** 466230587Sken * Preallocated grant table copy descriptor for RX operations. 467230587Sken * Access must be protected by rx_lock 468230587Sken */ 469230587Sken gnttab_copy_table rx_gnttab; 470181643Skmacy 471230587Sken /** 472230587Sken * Preallocated grant table copy descriptor for TX operations. 473230587Sken * Access must be protected by tx_lock 474230587Sken */ 475230587Sken gnttab_copy_table tx_gnttab; 476181643Skmacy 477230587Sken#ifdef XENHVM 478230587Sken /** 479230587Sken * Resource representing allocated physical address space 480230587Sken * associated with our per-instance kva region. 481230587Sken */ 482230587Sken struct resource *pseudo_phys_res; 483181643Skmacy 484230587Sken /** Resource id for allocated physical address space. */ 485230587Sken int pseudo_phys_res_id; 486230587Sken#endif 487181643Skmacy 488230587Sken /** Ring mapping and interrupt configuration data. */ 489230587Sken struct xnb_ring_config ring_configs[XNB_NUM_RING_TYPES]; 490181643Skmacy 491230587Sken /** 492230587Sken * Global pool of kva used for mapping remote domain ring 493230587Sken * and I/O transaction data. 494230587Sken */ 495230587Sken vm_offset_t kva; 496181643Skmacy 497230587Sken /** Psuedo-physical address corresponding to kva. */ 498230587Sken uint64_t gnt_base_addr; 499181643Skmacy 500230587Sken /** Various configuration and state bit flags. */ 501230587Sken xnb_flag_t flags; 502230587Sken 503230587Sken /** Mutex protecting per-instance data in the receive path. */ 504230587Sken struct mtx rx_lock; 505230587Sken 506230587Sken /** Mutex protecting per-instance data in the softc structure. */ 507230587Sken struct mtx sc_lock; 508230587Sken 509230587Sken /** Mutex protecting per-instance data in the transmit path. */ 510230587Sken struct mtx tx_lock; 511230587Sken 512230587Sken /** The size of the global kva pool. */ 513230587Sken int kva_size; 514230587Sken}; 515230587Sken 516230587Sken/*---------------------------- Debugging functions ---------------------------*/ 517230587Sken#ifdef XNB_DEBUG 518230587Skenstatic void __unused 519230587Skenxnb_dump_gnttab_copy(const struct gnttab_copy *entry) 520181643Skmacy{ 521230587Sken if (entry == NULL) { 522230587Sken printf("NULL grant table pointer\n"); 523230587Sken return; 524181643Skmacy } 525230587Sken 526230587Sken if (entry->flags & GNTCOPY_dest_gref) 527230587Sken printf("gnttab dest ref=\t%u\n", entry->dest.u.ref); 528230587Sken else 529230587Sken printf("gnttab dest gmfn=\t%lu\n", entry->dest.u.gmfn); 530230587Sken printf("gnttab dest offset=\t%hu\n", entry->dest.offset); 531230587Sken printf("gnttab dest domid=\t%hu\n", entry->dest.domid); 532230587Sken if (entry->flags & GNTCOPY_source_gref) 533230587Sken printf("gnttab source ref=\t%u\n", entry->source.u.ref); 534230587Sken else 535230587Sken printf("gnttab source gmfn=\t%lu\n", entry->source.u.gmfn); 536230587Sken printf("gnttab source offset=\t%hu\n", entry->source.offset); 537230587Sken printf("gnttab source domid=\t%hu\n", entry->source.domid); 538230587Sken printf("gnttab len=\t%hu\n", entry->len); 539230587Sken printf("gnttab flags=\t%hu\n", entry->flags); 540230587Sken printf("gnttab status=\t%hd\n", entry->status); 541181643Skmacy} 542181643Skmacy 543181643Skmacystatic int 544230587Skenxnb_dump_rings(SYSCTL_HANDLER_ARGS) 545181643Skmacy{ 546230587Sken static char results[720]; 547230587Sken struct xnb_softc const* xnb = (struct xnb_softc*)arg1; 548230587Sken netif_rx_back_ring_t const* rxb = 549230587Sken &xnb->ring_configs[XNB_RING_TYPE_RX].back_ring.rx_ring; 550230587Sken netif_tx_back_ring_t const* txb = 551230587Sken &xnb->ring_configs[XNB_RING_TYPE_TX].back_ring.tx_ring; 552181643Skmacy 553230587Sken /* empty the result strings */ 554230587Sken results[0] = 0; 555181643Skmacy 556230587Sken if ( !txb || !txb->sring || !rxb || !rxb->sring ) 557230587Sken return (SYSCTL_OUT(req, results, strnlen(results, 720))); 558181643Skmacy 559230587Sken snprintf(results, 720, 560230587Sken "\n\t%35s %18s\n" /* TX, RX */ 561230587Sken "\t%16s %18d %18d\n" /* req_cons */ 562230587Sken "\t%16s %18d %18d\n" /* nr_ents */ 563230587Sken "\t%16s %18d %18d\n" /* rsp_prod_pvt */ 564230587Sken "\t%16s %18p %18p\n" /* sring */ 565230587Sken "\t%16s %18d %18d\n" /* req_prod */ 566230587Sken "\t%16s %18d %18d\n" /* req_event */ 567230587Sken "\t%16s %18d %18d\n" /* rsp_prod */ 568230587Sken "\t%16s %18d %18d\n", /* rsp_event */ 569230587Sken "TX", "RX", 570230587Sken "req_cons", txb->req_cons, rxb->req_cons, 571230587Sken "nr_ents", txb->nr_ents, rxb->nr_ents, 572230587Sken "rsp_prod_pvt", txb->rsp_prod_pvt, rxb->rsp_prod_pvt, 573230587Sken "sring", txb->sring, rxb->sring, 574230587Sken "sring->req_prod", txb->sring->req_prod, rxb->sring->req_prod, 575230587Sken "sring->req_event", txb->sring->req_event, rxb->sring->req_event, 576230587Sken "sring->rsp_prod", txb->sring->rsp_prod, rxb->sring->rsp_prod, 577230587Sken "sring->rsp_event", txb->sring->rsp_event, rxb->sring->rsp_event); 578230587Sken 579230587Sken return (SYSCTL_OUT(req, results, strnlen(results, 720))); 580181643Skmacy} 581181643Skmacy 582230587Skenstatic void __unused 583230587Skenxnb_dump_mbuf(const struct mbuf *m) 584181643Skmacy{ 585230587Sken int len; 586230587Sken uint8_t *d; 587230587Sken if (m == NULL) 588230587Sken return; 589181643Skmacy 590230587Sken printf("xnb_dump_mbuf:\n"); 591230587Sken if (m->m_flags & M_PKTHDR) { 592230587Sken printf(" flowid=%10d, csum_flags=%#8x, csum_data=%#8x, " 593230587Sken "tso_segsz=%5hd\n", 594254910Sandre m->m_pkthdr.flowid, (int)m->m_pkthdr.csum_flags, 595230587Sken m->m_pkthdr.csum_data, m->m_pkthdr.tso_segsz); 596254910Sandre printf(" rcvif=%16p, len=%19d\n", 597254910Sandre m->m_pkthdr.rcvif, m->m_pkthdr.len); 598230587Sken } 599230587Sken printf(" m_next=%16p, m_nextpk=%16p, m_data=%16p\n", 600230587Sken m->m_next, m->m_nextpkt, m->m_data); 601254910Sandre printf(" m_len=%17d, m_flags=%#15x, m_type=%18u\n", 602230587Sken m->m_len, m->m_flags, m->m_type); 603181643Skmacy 604230587Sken len = m->m_len; 605230587Sken d = mtod(m, uint8_t*); 606230587Sken while (len > 0) { 607230587Sken int i; 608230587Sken printf(" "); 609230587Sken for (i = 0; (i < 16) && (len > 0); i++, len--) { 610230587Sken printf("%02hhx ", *(d++)); 611230587Sken } 612230587Sken printf("\n"); 613181643Skmacy } 614181643Skmacy} 615230587Sken#endif /* XNB_DEBUG */ 616181643Skmacy 617230587Sken/*------------------------ Inter-Domain Communication ------------------------*/ 618230587Sken/** 619230587Sken * Free dynamically allocated KVA or pseudo-physical address allocations. 620230587Sken * 621230587Sken * \param xnb Per-instance xnb configuration structure. 622230587Sken */ 623181643Skmacystatic void 624230587Skenxnb_free_communication_mem(struct xnb_softc *xnb) 625181643Skmacy{ 626230587Sken if (xnb->kva != 0) { 627230587Sken#ifndef XENHVM 628254025Sjeff kva_free(xnb->kva, xnb->kva_size); 629230587Sken#else 630230587Sken if (xnb->pseudo_phys_res != NULL) { 631230587Sken bus_release_resource(xnb->dev, SYS_RES_MEMORY, 632230587Sken xnb->pseudo_phys_res_id, 633230587Sken xnb->pseudo_phys_res); 634230587Sken xnb->pseudo_phys_res = NULL; 635230587Sken } 636230587Sken#endif /* XENHVM */ 637181643Skmacy } 638230587Sken xnb->kva = 0; 639230587Sken xnb->gnt_base_addr = 0; 640181643Skmacy} 641181643Skmacy 642230587Sken/** 643230587Sken * Cleanup all inter-domain communication mechanisms. 644230587Sken * 645230587Sken * \param xnb Per-instance xnb configuration structure. 646181643Skmacy */ 647230587Skenstatic int 648230587Skenxnb_disconnect(struct xnb_softc *xnb) 649181643Skmacy{ 650230587Sken struct gnttab_unmap_grant_ref gnts[XNB_NUM_RING_TYPES]; 651230587Sken int error; 652230587Sken int i; 653181643Skmacy 654270333Sroyger if (xnb->xen_intr_handle != NULL) 655270333Sroyger xen_intr_unbind(&xnb->xen_intr_handle); 656181643Skmacy 657230587Sken /* 658230587Sken * We may still have another thread currently processing requests. We 659230587Sken * must acquire the rx and tx locks to make sure those threads are done, 660230587Sken * but we can release those locks as soon as we acquire them, because no 661230587Sken * more interrupts will be arriving. 662230587Sken */ 663230587Sken mtx_lock(&xnb->tx_lock); 664230587Sken mtx_unlock(&xnb->tx_lock); 665230587Sken mtx_lock(&xnb->rx_lock); 666230587Sken mtx_unlock(&xnb->rx_lock); 667230587Sken 668230587Sken /* Free malloc'd softc member variables */ 669270333Sroyger if (xnb->bridge != NULL) { 670230587Sken free(xnb->bridge, M_XENSTORE); 671270333Sroyger xnb->bridge = NULL; 672270333Sroyger } 673230587Sken 674230587Sken /* All request processing has stopped, so unmap the rings */ 675230587Sken for (i=0; i < XNB_NUM_RING_TYPES; i++) { 676230587Sken gnts[i].host_addr = xnb->ring_configs[i].gnt_addr; 677230587Sken gnts[i].dev_bus_addr = xnb->ring_configs[i].bus_addr; 678230587Sken gnts[i].handle = xnb->ring_configs[i].handle; 679181643Skmacy } 680230587Sken error = HYPERVISOR_grant_table_op(GNTTABOP_unmap_grant_ref, gnts, 681230587Sken XNB_NUM_RING_TYPES); 682230587Sken KASSERT(error == 0, ("Grant table unmap op failed (%d)", error)); 683181643Skmacy 684230587Sken xnb_free_communication_mem(xnb); 685230587Sken /* 686230587Sken * Zero the ring config structs because the pointers, handles, and 687230587Sken * grant refs contained therein are no longer valid. 688230587Sken */ 689230587Sken bzero(&xnb->ring_configs[XNB_RING_TYPE_TX], 690230587Sken sizeof(struct xnb_ring_config)); 691230587Sken bzero(&xnb->ring_configs[XNB_RING_TYPE_RX], 692230587Sken sizeof(struct xnb_ring_config)); 693181643Skmacy 694230587Sken xnb->flags &= ~XNBF_RING_CONNECTED; 695230587Sken return (0); 696181643Skmacy} 697181643Skmacy 698230587Sken/** 699230587Sken * Map a single shared memory ring into domain local address space and 700230587Sken * initialize its control structure 701230587Sken * 702230587Sken * \param xnb Per-instance xnb configuration structure 703230587Sken * \param ring_type Array index of this ring in the xnb's array of rings 704230587Sken * \return An errno 705230587Sken */ 706230587Skenstatic int 707230587Skenxnb_connect_ring(struct xnb_softc *xnb, xnb_ring_type_t ring_type) 708181643Skmacy{ 709230587Sken struct gnttab_map_grant_ref gnt; 710230587Sken struct xnb_ring_config *ring = &xnb->ring_configs[ring_type]; 711230587Sken int error; 712181643Skmacy 713230587Sken /* TX ring type = 0, RX =1 */ 714230587Sken ring->va = xnb->kva + ring_type * PAGE_SIZE; 715230587Sken ring->gnt_addr = xnb->gnt_base_addr + ring_type * PAGE_SIZE; 716181643Skmacy 717230587Sken gnt.host_addr = ring->gnt_addr; 718230587Sken gnt.flags = GNTMAP_host_map; 719230587Sken gnt.ref = ring->ring_ref; 720230587Sken gnt.dom = xnb->otherend_id; 721181643Skmacy 722230587Sken error = HYPERVISOR_grant_table_op(GNTTABOP_map_grant_ref, &gnt, 1); 723230587Sken if (error != 0) 724230587Sken panic("netback: Ring page grant table op failed (%d)", error); 725230587Sken 726230587Sken if (gnt.status != 0) { 727230587Sken ring->va = 0; 728230587Sken error = EACCES; 729230587Sken xenbus_dev_fatal(xnb->dev, error, 730230587Sken "Ring shared page mapping failed. " 731230587Sken "Status %d.", gnt.status); 732230587Sken } else { 733230587Sken ring->handle = gnt.handle; 734230587Sken ring->bus_addr = gnt.dev_bus_addr; 735230587Sken 736230587Sken if (ring_type == XNB_RING_TYPE_TX) { 737230587Sken BACK_RING_INIT(&ring->back_ring.tx_ring, 738230587Sken (netif_tx_sring_t*)ring->va, 739230587Sken ring->ring_pages * PAGE_SIZE); 740230587Sken } else if (ring_type == XNB_RING_TYPE_RX) { 741230587Sken BACK_RING_INIT(&ring->back_ring.rx_ring, 742230587Sken (netif_rx_sring_t*)ring->va, 743230587Sken ring->ring_pages * PAGE_SIZE); 744230587Sken } else { 745230587Sken xenbus_dev_fatal(xnb->dev, error, 746230587Sken "Unknown ring type %d", ring_type); 747230587Sken } 748181643Skmacy } 749230587Sken 750230587Sken return error; 751181643Skmacy} 752181643Skmacy 753230587Sken/** 754230587Sken * Setup the shared memory rings and bind an interrupt to the event channel 755230587Sken * used to notify us of ring changes. 756230587Sken * 757230587Sken * \param xnb Per-instance xnb configuration structure. 758230587Sken */ 759230587Skenstatic int 760230587Skenxnb_connect_comms(struct xnb_softc *xnb) 761181643Skmacy{ 762230587Sken int error; 763230587Sken xnb_ring_type_t i; 764181643Skmacy 765230587Sken if ((xnb->flags & XNBF_RING_CONNECTED) != 0) 766230587Sken return (0); 767181643Skmacy 768181643Skmacy /* 769230587Sken * Kva for our rings are at the tail of the region of kva allocated 770230587Sken * by xnb_alloc_communication_mem(). 771181643Skmacy */ 772230587Sken for (i=0; i < XNB_NUM_RING_TYPES; i++) { 773230587Sken error = xnb_connect_ring(xnb, i); 774230587Sken if (error != 0) 775230587Sken return error; 776181643Skmacy } 777181643Skmacy 778230587Sken xnb->flags |= XNBF_RING_CONNECTED; 779181643Skmacy 780255040Sgibbs error = xen_intr_bind_remote_port(xnb->dev, 781255040Sgibbs xnb->otherend_id, 782255040Sgibbs xnb->evtchn, 783255040Sgibbs /*filter*/NULL, 784255040Sgibbs xnb_intr, /*arg*/xnb, 785255040Sgibbs INTR_TYPE_BIO | INTR_MPSAFE, 786255040Sgibbs &xnb->xen_intr_handle); 787230587Sken if (error != 0) { 788230587Sken (void)xnb_disconnect(xnb); 789230587Sken xenbus_dev_fatal(xnb->dev, error, "binding event channel"); 790230587Sken return (error); 791230587Sken } 792181643Skmacy 793230587Sken DPRINTF("rings connected!\n"); 794181643Skmacy 795230587Sken return (0); 796181643Skmacy} 797181643Skmacy 798230587Sken/** 799230587Sken * Size KVA and pseudo-physical address allocations based on negotiated 800230587Sken * values for the size and number of I/O requests, and the size of our 801230587Sken * communication ring. 802230587Sken * 803230587Sken * \param xnb Per-instance xnb configuration structure. 804230587Sken * 805230587Sken * These address spaces are used to dynamically map pages in the 806230587Sken * front-end's domain into our own. 807230587Sken */ 808230587Skenstatic int 809230587Skenxnb_alloc_communication_mem(struct xnb_softc *xnb) 810181643Skmacy{ 811230587Sken xnb_ring_type_t i; 812181643Skmacy 813230587Sken xnb->kva_size = 0; 814230587Sken for (i=0; i < XNB_NUM_RING_TYPES; i++) { 815230587Sken xnb->kva_size += xnb->ring_configs[i].ring_pages * PAGE_SIZE; 816230587Sken } 817230587Sken#ifndef XENHVM 818254025Sjeff xnb->kva = kva_alloc(xnb->kva_size); 819230587Sken if (xnb->kva == 0) 820230587Sken return (ENOMEM); 821230587Sken xnb->gnt_base_addr = xnb->kva; 822230587Sken#else /* defined XENHVM */ 823230587Sken /* 824230587Sken * Reserve a range of pseudo physical memory that we can map 825230587Sken * into kva. These pages will only be backed by machine 826230587Sken * pages ("real memory") during the lifetime of front-end requests 827230587Sken * via grant table operations. We will map the netif tx and rx rings 828230587Sken * into this space. 829230587Sken */ 830230587Sken xnb->pseudo_phys_res_id = 0; 831230587Sken xnb->pseudo_phys_res = bus_alloc_resource(xnb->dev, SYS_RES_MEMORY, 832230587Sken &xnb->pseudo_phys_res_id, 833230587Sken 0, ~0, xnb->kva_size, 834230587Sken RF_ACTIVE); 835230587Sken if (xnb->pseudo_phys_res == NULL) { 836230587Sken xnb->kva = 0; 837230587Sken return (ENOMEM); 838230587Sken } 839230587Sken xnb->kva = (vm_offset_t)rman_get_virtual(xnb->pseudo_phys_res); 840230587Sken xnb->gnt_base_addr = rman_get_start(xnb->pseudo_phys_res); 841230587Sken#endif /* !defined XENHVM */ 842230587Sken return (0); 843181643Skmacy} 844181643Skmacy 845230587Sken/** 846230587Sken * Collect information from the XenStore related to our device and its frontend 847230587Sken * 848230587Sken * \param xnb Per-instance xnb configuration structure. 849230587Sken */ 850230587Skenstatic int 851230587Skenxnb_collect_xenstore_info(struct xnb_softc *xnb) 852181643Skmacy{ 853230587Sken /** 854230587Sken * \todo Linux collects the following info. We should collect most 855230587Sken * of this, too: 856230587Sken * "feature-rx-notify" 857230587Sken */ 858230587Sken const char *otherend_path; 859230587Sken const char *our_path; 860230587Sken int err; 861230587Sken unsigned int rx_copy, bridge_len; 862230587Sken uint8_t no_csum_offload; 863181643Skmacy 864230587Sken otherend_path = xenbus_get_otherend_path(xnb->dev); 865230587Sken our_path = xenbus_get_node(xnb->dev); 866181643Skmacy 867230587Sken /* Collect the critical communication parameters */ 868230587Sken err = xs_gather(XST_NIL, otherend_path, 869230587Sken "tx-ring-ref", "%l" PRIu32, 870230587Sken &xnb->ring_configs[XNB_RING_TYPE_TX].ring_ref, 871230587Sken "rx-ring-ref", "%l" PRIu32, 872230587Sken &xnb->ring_configs[XNB_RING_TYPE_RX].ring_ref, 873230587Sken "event-channel", "%" PRIu32, &xnb->evtchn, 874230587Sken NULL); 875230587Sken if (err != 0) { 876230587Sken xenbus_dev_fatal(xnb->dev, err, 877230587Sken "Unable to retrieve ring information from " 878230587Sken "frontend %s. Unable to connect.", 879230587Sken otherend_path); 880230587Sken return (err); 881230587Sken } 882181643Skmacy 883230587Sken /* Collect the handle from xenstore */ 884230587Sken err = xs_scanf(XST_NIL, our_path, "handle", NULL, "%li", &xnb->handle); 885230587Sken if (err != 0) { 886230587Sken xenbus_dev_fatal(xnb->dev, err, 887230587Sken "Error reading handle from frontend %s. " 888230587Sken "Unable to connect.", otherend_path); 889230587Sken } 890181643Skmacy 891230587Sken /* 892230587Sken * Collect the bridgename, if any. We do not need bridge_len; we just 893230587Sken * throw it away 894230587Sken */ 895230587Sken err = xs_read(XST_NIL, our_path, "bridge", &bridge_len, 896230587Sken (void**)&xnb->bridge); 897230587Sken if (err != 0) 898230587Sken xnb->bridge = NULL; 899181643Skmacy 900230587Sken /* 901230587Sken * Does the frontend request that we use rx copy? If not, return an 902230587Sken * error because this driver only supports rx copy. 903230587Sken */ 904230587Sken err = xs_scanf(XST_NIL, otherend_path, "request-rx-copy", NULL, 905230587Sken "%" PRIu32, &rx_copy); 906230587Sken if (err == ENOENT) { 907230587Sken err = 0; 908230587Sken rx_copy = 0; 909230587Sken } 910230587Sken if (err < 0) { 911230587Sken xenbus_dev_fatal(xnb->dev, err, "reading %s/request-rx-copy", 912230587Sken otherend_path); 913230587Sken return err; 914230587Sken } 915230587Sken /** 916230587Sken * \todo: figure out the exact meaning of this feature, and when 917230587Sken * the frontend will set it to true. It should be set to true 918230587Sken * at some point 919230587Sken */ 920230587Sken/* if (!rx_copy)*/ 921230587Sken/* return EOPNOTSUPP;*/ 922181643Skmacy 923230587Sken /** \todo Collect the rx notify feature */ 924181643Skmacy 925230587Sken /* Collect the feature-sg. */ 926230587Sken if (xs_scanf(XST_NIL, otherend_path, "feature-sg", NULL, 927230587Sken "%hhu", &xnb->can_sg) < 0) 928230587Sken xnb->can_sg = 0; 929181643Skmacy 930230587Sken /* Collect remaining frontend features */ 931230587Sken if (xs_scanf(XST_NIL, otherend_path, "feature-gso-tcpv4", NULL, 932230587Sken "%hhu", &xnb->gso) < 0) 933230587Sken xnb->gso = 0; 934181643Skmacy 935230587Sken if (xs_scanf(XST_NIL, otherend_path, "feature-gso-tcpv4-prefix", NULL, 936230587Sken "%hhu", &xnb->gso_prefix) < 0) 937230587Sken xnb->gso_prefix = 0; 938181643Skmacy 939230587Sken if (xs_scanf(XST_NIL, otherend_path, "feature-no-csum-offload", NULL, 940230587Sken "%hhu", &no_csum_offload) < 0) 941230587Sken no_csum_offload = 0; 942230587Sken xnb->ip_csum = (no_csum_offload == 0); 943181643Skmacy 944230587Sken return (0); 945230587Sken} 946230587Sken 947230587Sken/** 948230587Sken * Supply information about the physical device to the frontend 949230587Sken * via XenBus. 950230587Sken * 951230587Sken * \param xnb Per-instance xnb configuration structure. 952230587Sken */ 953230587Skenstatic int 954230587Skenxnb_publish_backend_info(struct xnb_softc *xnb) 955230587Sken{ 956230587Sken struct xs_transaction xst; 957230587Sken const char *our_path; 958230587Sken int error; 959230587Sken 960230587Sken our_path = xenbus_get_node(xnb->dev); 961230587Sken 962230587Sken do { 963230587Sken error = xs_transaction_start(&xst); 964230587Sken if (error != 0) { 965230587Sken xenbus_dev_fatal(xnb->dev, error, 966230587Sken "Error publishing backend info " 967230587Sken "(start transaction)"); 968230587Sken break; 969181643Skmacy } 970181643Skmacy 971230587Sken error = xs_printf(xst, our_path, "feature-sg", 972230587Sken "%d", XNB_SG); 973230587Sken if (error != 0) 974230587Sken break; 975181643Skmacy 976230587Sken error = xs_printf(xst, our_path, "feature-gso-tcpv4", 977230587Sken "%d", XNB_GSO_TCPV4); 978230587Sken if (error != 0) 979181643Skmacy break; 980230587Sken 981230587Sken error = xs_printf(xst, our_path, "feature-rx-copy", 982230587Sken "%d", XNB_RX_COPY); 983230587Sken if (error != 0) 984230587Sken break; 985230587Sken 986230587Sken error = xs_printf(xst, our_path, "feature-rx-flip", 987230587Sken "%d", XNB_RX_FLIP); 988230587Sken if (error != 0) 989230587Sken break; 990230587Sken 991230587Sken error = xs_transaction_end(xst, 0); 992230587Sken if (error != 0 && error != EAGAIN) { 993230587Sken xenbus_dev_fatal(xnb->dev, error, "ending transaction"); 994230587Sken break; 995181643Skmacy } 996181643Skmacy 997230587Sken } while (error == EAGAIN); 998181643Skmacy 999230587Sken return (error); 1000230587Sken} 1001181643Skmacy 1002230587Sken/** 1003230587Sken * Connect to our netfront peer now that it has completed publishing 1004230587Sken * its configuration into the XenStore. 1005230587Sken * 1006230587Sken * \param xnb Per-instance xnb configuration structure. 1007230587Sken */ 1008230587Skenstatic void 1009230587Skenxnb_connect(struct xnb_softc *xnb) 1010230587Sken{ 1011230587Sken int error; 1012181643Skmacy 1013230587Sken if (xenbus_get_state(xnb->dev) == XenbusStateConnected) 1014230587Sken return; 1015181643Skmacy 1016230587Sken if (xnb_collect_xenstore_info(xnb) != 0) 1017230587Sken return; 1018181643Skmacy 1019230587Sken xnb->flags &= ~XNBF_SHUTDOWN; 1020230587Sken 1021230587Sken /* Read front end configuration. */ 1022230587Sken 1023230587Sken /* Allocate resources whose size depends on front-end configuration. */ 1024230587Sken error = xnb_alloc_communication_mem(xnb); 1025230587Sken if (error != 0) { 1026230587Sken xenbus_dev_fatal(xnb->dev, error, 1027230587Sken "Unable to allocate communication memory"); 1028230587Sken return; 1029181643Skmacy } 1030181643Skmacy 1031230587Sken /* 1032230587Sken * Connect communication channel. 1033230587Sken */ 1034230587Sken error = xnb_connect_comms(xnb); 1035230587Sken if (error != 0) { 1036230587Sken /* Specific errors are reported by xnb_connect_comms(). */ 1037181643Skmacy return; 1038230587Sken } 1039230587Sken xnb->carrier = 1; 1040181643Skmacy 1041230587Sken /* Ready for I/O. */ 1042230587Sken xenbus_set_state(xnb->dev, XenbusStateConnected); 1043230587Sken} 1044181643Skmacy 1045230587Sken/*-------------------------- Device Teardown Support -------------------------*/ 1046230587Sken/** 1047230587Sken * Perform device shutdown functions. 1048230587Sken * 1049230587Sken * \param xnb Per-instance xnb configuration structure. 1050230587Sken * 1051230587Sken * Mark this instance as shutting down, wait for any active requests 1052230587Sken * to drain, disconnect from the front-end, and notify any waiters (e.g. 1053230587Sken * a thread invoking our detach method) that detach can now proceed. 1054230587Sken */ 1055230587Skenstatic int 1056230587Skenxnb_shutdown(struct xnb_softc *xnb) 1057230587Sken{ 1058230587Sken /* 1059230587Sken * Due to the need to drop our mutex during some 1060230587Sken * xenbus operations, it is possible for two threads 1061230587Sken * to attempt to close out shutdown processing at 1062230587Sken * the same time. Tell the caller that hits this 1063230587Sken * race to try back later. 1064230587Sken */ 1065230587Sken if ((xnb->flags & XNBF_IN_SHUTDOWN) != 0) 1066230587Sken return (EAGAIN); 1067181643Skmacy 1068230587Sken xnb->flags |= XNBF_SHUTDOWN; 1069181643Skmacy 1070230587Sken xnb->flags |= XNBF_IN_SHUTDOWN; 1071181643Skmacy 1072230587Sken mtx_unlock(&xnb->sc_lock); 1073230587Sken /* Free the network interface */ 1074230587Sken xnb->carrier = 0; 1075230587Sken if (xnb->xnb_ifp != NULL) { 1076230587Sken ether_ifdetach(xnb->xnb_ifp); 1077230587Sken if_free(xnb->xnb_ifp); 1078230587Sken xnb->xnb_ifp = NULL; 1079230587Sken } 1080230587Sken mtx_lock(&xnb->sc_lock); 1081181643Skmacy 1082230587Sken xnb_disconnect(xnb); 1083181643Skmacy 1084230587Sken mtx_unlock(&xnb->sc_lock); 1085230587Sken if (xenbus_get_state(xnb->dev) < XenbusStateClosing) 1086230587Sken xenbus_set_state(xnb->dev, XenbusStateClosing); 1087230587Sken mtx_lock(&xnb->sc_lock); 1088181643Skmacy 1089230587Sken xnb->flags &= ~XNBF_IN_SHUTDOWN; 1090181643Skmacy 1091181643Skmacy 1092230587Sken /* Indicate to xnb_detach() that is it safe to proceed. */ 1093230587Sken wakeup(xnb); 1094181643Skmacy 1095230587Sken return (0); 1096181643Skmacy} 1097181643Skmacy 1098230587Sken/** 1099230587Sken * Report an attach time error to the console and Xen, and cleanup 1100230587Sken * this instance by forcing immediate detach processing. 1101230587Sken * 1102230587Sken * \param xnb Per-instance xnb configuration structure. 1103230587Sken * \param err Errno describing the error. 1104230587Sken * \param fmt Printf style format and arguments 1105230587Sken */ 1106181643Skmacystatic void 1107230587Skenxnb_attach_failed(struct xnb_softc *xnb, int err, const char *fmt, ...) 1108181643Skmacy{ 1109230587Sken va_list ap; 1110230587Sken va_list ap_hotplug; 1111230587Sken 1112230587Sken va_start(ap, fmt); 1113230587Sken va_copy(ap_hotplug, ap); 1114230587Sken xs_vprintf(XST_NIL, xenbus_get_node(xnb->dev), 1115230587Sken "hotplug-error", fmt, ap_hotplug); 1116230587Sken va_end(ap_hotplug); 1117230587Sken xs_printf(XST_NIL, xenbus_get_node(xnb->dev), 1118230587Sken "hotplug-status", "error"); 1119230587Sken 1120230587Sken xenbus_dev_vfatal(xnb->dev, err, fmt, ap); 1121230587Sken va_end(ap); 1122230587Sken 1123230587Sken xs_printf(XST_NIL, xenbus_get_node(xnb->dev), 1124230587Sken "online", "0"); 1125230587Sken xnb_detach(xnb->dev); 1126181643Skmacy} 1127181643Skmacy 1128230587Sken/*---------------------------- NewBus Entrypoints ----------------------------*/ 1129230587Sken/** 1130230587Sken * Inspect a XenBus device and claim it if is of the appropriate type. 1131230587Sken * 1132230587Sken * \param dev NewBus device object representing a candidate XenBus device. 1133230587Sken * 1134230587Sken * \return 0 for success, errno codes for failure. 1135230587Sken */ 1136230587Skenstatic int 1137230587Skenxnb_probe(device_t dev) 1138181643Skmacy{ 1139230587Sken if (!strcmp(xenbus_get_type(dev), "vif")) { 1140230587Sken DPRINTF("Claiming device %d, %s\n", device_get_unit(dev), 1141230587Sken devclass_get_name(device_get_devclass(dev))); 1142230587Sken device_set_desc(dev, "Backend Virtual Network Device"); 1143230587Sken device_quiet(dev); 1144230587Sken return (0); 1145181643Skmacy } 1146230587Sken return (ENXIO); 1147181643Skmacy} 1148181643Skmacy 1149230587Sken/** 1150230587Sken * Setup sysctl variables to control various Network Back parameters. 1151230587Sken * 1152230587Sken * \param xnb Xen Net Back softc. 1153230587Sken * 1154230587Sken */ 1155181643Skmacystatic void 1156230587Skenxnb_setup_sysctl(struct xnb_softc *xnb) 1157181643Skmacy{ 1158230587Sken struct sysctl_ctx_list *sysctl_ctx = NULL; 1159230587Sken struct sysctl_oid *sysctl_tree = NULL; 1160230587Sken 1161230587Sken sysctl_ctx = device_get_sysctl_ctx(xnb->dev); 1162230587Sken if (sysctl_ctx == NULL) 1163181643Skmacy return; 1164181643Skmacy 1165230587Sken sysctl_tree = device_get_sysctl_tree(xnb->dev); 1166230587Sken if (sysctl_tree == NULL) 1167230587Sken return; 1168230587Sken 1169230587Sken#ifdef XNB_DEBUG 1170230587Sken SYSCTL_ADD_PROC(sysctl_ctx, 1171230587Sken SYSCTL_CHILDREN(sysctl_tree), 1172230587Sken OID_AUTO, 1173230587Sken "unit_test_results", 1174230587Sken CTLTYPE_STRING | CTLFLAG_RD, 1175230587Sken xnb, 1176230587Sken 0, 1177230587Sken xnb_unit_test_main, 1178230587Sken "A", 1179230587Sken "Results of builtin unit tests"); 1180230587Sken 1181230587Sken SYSCTL_ADD_PROC(sysctl_ctx, 1182230587Sken SYSCTL_CHILDREN(sysctl_tree), 1183230587Sken OID_AUTO, 1184230587Sken "dump_rings", 1185230587Sken CTLTYPE_STRING | CTLFLAG_RD, 1186230587Sken xnb, 1187230587Sken 0, 1188230587Sken xnb_dump_rings, 1189230587Sken "A", 1190230587Sken "Xennet Back Rings"); 1191230587Sken#endif /* XNB_DEBUG */ 1192181643Skmacy} 1193181643Skmacy 1194230587Sken/** 1195230587Sken * Create a network device. 1196230587Sken * @param handle device handle 1197230587Sken */ 1198230587Skenint 1199230587Skencreate_netdev(device_t dev) 1200181643Skmacy{ 1201230587Sken struct ifnet *ifp; 1202230587Sken struct xnb_softc *xnb; 1203230587Sken int err = 0; 1204181643Skmacy 1205230587Sken xnb = device_get_softc(dev); 1206230587Sken mtx_init(&xnb->sc_lock, "xnb_softc", "xen netback softc lock", MTX_DEF); 1207230587Sken mtx_init(&xnb->tx_lock, "xnb_tx", "xen netback tx lock", MTX_DEF); 1208230587Sken mtx_init(&xnb->rx_lock, "xnb_rx", "xen netback rx lock", MTX_DEF); 1209181643Skmacy 1210230587Sken xnb->dev = dev; 1211181643Skmacy 1212230587Sken ifmedia_init(&xnb->sc_media, 0, xnb_ifmedia_upd, xnb_ifmedia_sts); 1213230587Sken ifmedia_add(&xnb->sc_media, IFM_ETHER|IFM_MANUAL, 0, NULL); 1214230587Sken ifmedia_set(&xnb->sc_media, IFM_ETHER|IFM_MANUAL); 1215181643Skmacy 1216270333Sroyger /* 1217270333Sroyger * Set the MAC address to a dummy value (00:00:00:00:00), 1218270333Sroyger * if the MAC address of the host-facing interface is set 1219270333Sroyger * to the same as the guest-facing one (the value found in 1220270333Sroyger * xenstore), the bridge would stop delivering packets to 1221270333Sroyger * us because it would see that the destination address of 1222270333Sroyger * the packet is the same as the interface, and so the bridge 1223270333Sroyger * would expect the packet has already been delivered locally 1224270333Sroyger * (and just drop it). 1225270333Sroyger */ 1226270333Sroyger bzero(&xnb->mac[0], sizeof(xnb->mac)); 1227270333Sroyger 1228230587Sken if (err == 0) { 1229230587Sken /* Set up ifnet structure */ 1230230587Sken ifp = xnb->xnb_ifp = if_alloc(IFT_ETHER); 1231230587Sken ifp->if_softc = xnb; 1232230587Sken if_initname(ifp, "xnb", device_get_unit(dev)); 1233230587Sken ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST; 1234230587Sken ifp->if_ioctl = xnb_ioctl; 1235230587Sken ifp->if_output = ether_output; 1236230587Sken ifp->if_start = xnb_start; 1237230587Sken#ifdef notyet 1238230587Sken ifp->if_watchdog = xnb_watchdog; 1239230587Sken#endif 1240230587Sken ifp->if_init = xnb_ifinit; 1241230587Sken ifp->if_mtu = ETHERMTU; 1242230587Sken ifp->if_snd.ifq_maxlen = NET_RX_RING_SIZE - 1; 1243230587Sken 1244230587Sken ifp->if_hwassist = XNB_CSUM_FEATURES; 1245230587Sken ifp->if_capabilities = IFCAP_HWCSUM; 1246230587Sken ifp->if_capenable = IFCAP_HWCSUM; 1247230587Sken 1248230587Sken ether_ifattach(ifp, xnb->mac); 1249230587Sken xnb->carrier = 0; 1250230587Sken } 1251230587Sken 1252230587Sken return err; 1253181643Skmacy} 1254181643Skmacy 1255230587Sken/** 1256230587Sken * Attach to a XenBus device that has been claimed by our probe routine. 1257230587Sken * 1258230587Sken * \param dev NewBus device object representing this Xen Net Back instance. 1259230587Sken * 1260230587Sken * \return 0 for success, errno codes for failure. 1261230587Sken */ 1262181643Skmacystatic int 1263230587Skenxnb_attach(device_t dev) 1264181643Skmacy{ 1265230587Sken struct xnb_softc *xnb; 1266230587Sken int error; 1267230587Sken xnb_ring_type_t i; 1268181643Skmacy 1269230587Sken error = create_netdev(dev); 1270230587Sken if (error != 0) { 1271230587Sken xenbus_dev_fatal(dev, error, "creating netdev"); 1272230587Sken return (error); 1273230587Sken } 1274181643Skmacy 1275230587Sken DPRINTF("Attaching to %s\n", xenbus_get_node(dev)); 1276181643Skmacy 1277230587Sken /* 1278230587Sken * Basic initialization. 1279230587Sken * After this block it is safe to call xnb_detach() 1280230587Sken * to clean up any allocated data for this instance. 1281230587Sken */ 1282230587Sken xnb = device_get_softc(dev); 1283230587Sken xnb->otherend_id = xenbus_get_otherend_id(dev); 1284230587Sken for (i=0; i < XNB_NUM_RING_TYPES; i++) { 1285230587Sken xnb->ring_configs[i].ring_pages = 1; 1286230587Sken } 1287181643Skmacy 1288230587Sken /* 1289230587Sken * Setup sysctl variables. 1290230587Sken */ 1291230587Sken xnb_setup_sysctl(xnb); 1292181643Skmacy 1293230587Sken /* Update hot-plug status to satisfy xend. */ 1294230587Sken error = xs_printf(XST_NIL, xenbus_get_node(xnb->dev), 1295230587Sken "hotplug-status", "connected"); 1296230587Sken if (error != 0) { 1297230587Sken xnb_attach_failed(xnb, error, "writing %s/hotplug-status", 1298230587Sken xenbus_get_node(xnb->dev)); 1299230587Sken return (error); 1300230587Sken } 1301181643Skmacy 1302230587Sken if ((error = xnb_publish_backend_info(xnb)) != 0) { 1303230587Sken /* 1304230587Sken * If we can't publish our data, we cannot participate 1305230587Sken * in this connection, and waiting for a front-end state 1306230587Sken * change will not help the situation. 1307230587Sken */ 1308230587Sken xnb_attach_failed(xnb, error, 1309230587Sken "Publishing backend status for %s", 1310230587Sken xenbus_get_node(xnb->dev)); 1311230587Sken return error; 1312230587Sken } 1313181643Skmacy 1314230587Sken /* Tell the front end that we are ready to connect. */ 1315230587Sken xenbus_set_state(dev, XenbusStateInitWait); 1316181643Skmacy 1317230587Sken return (0); 1318230587Sken} 1319181643Skmacy 1320230587Sken/** 1321230587Sken * Detach from a net back device instance. 1322230587Sken * 1323230587Sken * \param dev NewBus device object representing this Xen Net Back instance. 1324230587Sken * 1325230587Sken * \return 0 for success, errno codes for failure. 1326230587Sken * 1327230587Sken * \note A net back device may be detached at any time in its life-cycle, 1328230587Sken * including part way through the attach process. For this reason, 1329230587Sken * initialization order and the intialization state checks in this 1330230587Sken * routine must be carefully coupled so that attach time failures 1331230587Sken * are gracefully handled. 1332230587Sken */ 1333230587Skenstatic int 1334230587Skenxnb_detach(device_t dev) 1335230587Sken{ 1336230587Sken struct xnb_softc *xnb; 1337181643Skmacy 1338230587Sken DPRINTF("\n"); 1339181643Skmacy 1340230587Sken xnb = device_get_softc(dev); 1341230587Sken mtx_lock(&xnb->sc_lock); 1342230587Sken while (xnb_shutdown(xnb) == EAGAIN) { 1343230587Sken msleep(xnb, &xnb->sc_lock, /*wakeup prio unchanged*/0, 1344230587Sken "xnb_shutdown", 0); 1345230587Sken } 1346230587Sken mtx_unlock(&xnb->sc_lock); 1347230587Sken DPRINTF("\n"); 1348181643Skmacy 1349230587Sken mtx_destroy(&xnb->tx_lock); 1350230587Sken mtx_destroy(&xnb->rx_lock); 1351230587Sken mtx_destroy(&xnb->sc_lock); 1352230587Sken return (0); 1353230587Sken} 1354181643Skmacy 1355230587Sken/** 1356230587Sken * Prepare this net back device for suspension of this VM. 1357230587Sken * 1358230587Sken * \param dev NewBus device object representing this Xen net Back instance. 1359230587Sken * 1360230587Sken * \return 0 for success, errno codes for failure. 1361230587Sken */ 1362230587Skenstatic int 1363230587Skenxnb_suspend(device_t dev) 1364230587Sken{ 1365230587Sken return (0); 1366230587Sken} 1367181643Skmacy 1368230587Sken/** 1369230587Sken * Perform any processing required to recover from a suspended state. 1370230587Sken * 1371230587Sken * \param dev NewBus device object representing this Xen Net Back instance. 1372230587Sken * 1373230587Sken * \return 0 for success, errno codes for failure. 1374230587Sken */ 1375230587Skenstatic int 1376230587Skenxnb_resume(device_t dev) 1377230587Sken{ 1378230587Sken return (0); 1379230587Sken} 1380181643Skmacy 1381230587Sken/** 1382230587Sken * Handle state changes expressed via the XenStore by our front-end peer. 1383230587Sken * 1384230587Sken * \param dev NewBus device object representing this Xen 1385230587Sken * Net Back instance. 1386230587Sken * \param frontend_state The new state of the front-end. 1387230587Sken * 1388230587Sken * \return 0 for success, errno codes for failure. 1389230587Sken */ 1390230587Skenstatic void 1391230587Skenxnb_frontend_changed(device_t dev, XenbusState frontend_state) 1392230587Sken{ 1393230587Sken struct xnb_softc *xnb; 1394181643Skmacy 1395230587Sken xnb = device_get_softc(dev); 1396181643Skmacy 1397230587Sken DPRINTF("frontend_state=%s, xnb_state=%s\n", 1398230587Sken xenbus_strstate(frontend_state), 1399230587Sken xenbus_strstate(xenbus_get_state(xnb->dev))); 1400181643Skmacy 1401230587Sken switch (frontend_state) { 1402230587Sken case XenbusStateInitialising: 1403230587Sken break; 1404230587Sken case XenbusStateInitialised: 1405230587Sken case XenbusStateConnected: 1406230587Sken xnb_connect(xnb); 1407230587Sken break; 1408230587Sken case XenbusStateClosing: 1409230587Sken case XenbusStateClosed: 1410230587Sken mtx_lock(&xnb->sc_lock); 1411230587Sken xnb_shutdown(xnb); 1412230587Sken mtx_unlock(&xnb->sc_lock); 1413230587Sken if (frontend_state == XenbusStateClosed) 1414230587Sken xenbus_set_state(xnb->dev, XenbusStateClosed); 1415230587Sken break; 1416230587Sken default: 1417230587Sken xenbus_dev_fatal(xnb->dev, EINVAL, "saw state %d at frontend", 1418230587Sken frontend_state); 1419230587Sken break; 1420230587Sken } 1421230587Sken} 1422181643Skmacy 1423181643Skmacy 1424230587Sken/*---------------------------- Request Processing ----------------------------*/ 1425230587Sken/** 1426230587Sken * Interrupt handler bound to the shared ring's event channel. 1427230587Sken * Entry point for the xennet transmit path in netback 1428230587Sken * Transfers packets from the Xen ring to the host's generic networking stack 1429230587Sken * 1430230587Sken * \param arg Callback argument registerd during event channel 1431230587Sken * binding - the xnb_softc for this instance. 1432230587Sken */ 1433230587Skenstatic void 1434230587Skenxnb_intr(void *arg) 1435230587Sken{ 1436230587Sken struct xnb_softc *xnb; 1437230587Sken struct ifnet *ifp; 1438230587Sken netif_tx_back_ring_t *txb; 1439230587Sken RING_IDX req_prod_local; 1440181643Skmacy 1441230587Sken xnb = (struct xnb_softc *)arg; 1442230587Sken ifp = xnb->xnb_ifp; 1443230587Sken txb = &xnb->ring_configs[XNB_RING_TYPE_TX].back_ring.tx_ring; 1444181643Skmacy 1445230587Sken mtx_lock(&xnb->tx_lock); 1446230587Sken do { 1447230587Sken int notify; 1448230587Sken req_prod_local = txb->sring->req_prod; 1449230587Sken xen_rmb(); 1450181643Skmacy 1451230587Sken for (;;) { 1452230587Sken struct mbuf *mbufc; 1453230587Sken int err; 1454181643Skmacy 1455230587Sken err = xnb_recv(txb, xnb->otherend_id, &mbufc, ifp, 1456230587Sken xnb->tx_gnttab); 1457230587Sken if (err || (mbufc == NULL)) 1458230587Sken break; 1459181643Skmacy 1460230587Sken /* Send the packet to the generic network stack */ 1461230587Sken (*xnb->xnb_ifp->if_input)(xnb->xnb_ifp, mbufc); 1462230587Sken } 1463181643Skmacy 1464230587Sken RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(txb, notify); 1465230587Sken if (notify != 0) 1466255040Sgibbs xen_intr_signal(xnb->xen_intr_handle); 1467181643Skmacy 1468230587Sken txb->sring->req_event = txb->req_cons + 1; 1469230587Sken xen_mb(); 1470230587Sken } while (txb->sring->req_prod != req_prod_local) ; 1471230587Sken mtx_unlock(&xnb->tx_lock); 1472181643Skmacy 1473230587Sken xnb_start(ifp); 1474230587Sken} 1475181643Skmacy 1476181643Skmacy 1477230587Sken/** 1478230587Sken * Build a struct xnb_pkt based on netif_tx_request's from a netif tx ring. 1479230587Sken * Will read exactly 0 or 1 packets from the ring; never a partial packet. 1480230587Sken * \param[out] pkt The returned packet. If there is an error building 1481230587Sken * the packet, pkt.list_len will be set to 0. 1482230587Sken * \param[in] tx_ring Pointer to the Ring that is the input to this function 1483230587Sken * \param[in] start The ring index of the first potential request 1484230587Sken * \return The number of requests consumed to build this packet 1485230587Sken */ 1486230587Skenstatic int 1487230587Skenxnb_ring2pkt(struct xnb_pkt *pkt, const netif_tx_back_ring_t *tx_ring, 1488230587Sken RING_IDX start) 1489230587Sken{ 1490230587Sken /* 1491230587Sken * Outline: 1492230587Sken * 1) Initialize pkt 1493230587Sken * 2) Read the first request of the packet 1494230587Sken * 3) Read the extras 1495230587Sken * 4) Set cdr 1496230587Sken * 5) Loop on the remainder of the packet 1497230587Sken * 6) Finalize pkt (stuff like car_size and list_len) 1498230587Sken */ 1499230587Sken int idx = start; 1500230587Sken int discard = 0; /* whether to discard the packet */ 1501230587Sken int more_data = 0; /* there are more request past the last one */ 1502230587Sken uint16_t cdr_size = 0; /* accumulated size of requests 2 through n */ 1503181643Skmacy 1504230587Sken xnb_pkt_initialize(pkt); 1505181643Skmacy 1506230587Sken /* Read the first request */ 1507230587Sken if (RING_HAS_UNCONSUMED_REQUESTS_2(tx_ring, idx)) { 1508230587Sken netif_tx_request_t *tx = RING_GET_REQUEST(tx_ring, idx); 1509230587Sken pkt->size = tx->size; 1510230587Sken pkt->flags = tx->flags & ~NETTXF_more_data; 1511230587Sken more_data = tx->flags & NETTXF_more_data; 1512230587Sken pkt->list_len++; 1513230587Sken pkt->car = idx; 1514230587Sken idx++; 1515230587Sken } 1516181643Skmacy 1517230587Sken /* Read the extra info */ 1518230587Sken if ((pkt->flags & NETTXF_extra_info) && 1519230587Sken RING_HAS_UNCONSUMED_REQUESTS_2(tx_ring, idx)) { 1520230587Sken netif_extra_info_t *ext = 1521230587Sken (netif_extra_info_t*) RING_GET_REQUEST(tx_ring, idx); 1522230587Sken pkt->extra.type = ext->type; 1523230587Sken switch (pkt->extra.type) { 1524230587Sken case XEN_NETIF_EXTRA_TYPE_GSO: 1525230587Sken pkt->extra.u.gso = ext->u.gso; 1526230587Sken break; 1527230587Sken default: 1528230587Sken /* 1529230587Sken * The reference Linux netfront driver will 1530230587Sken * never set any other extra.type. So we don't 1531230587Sken * know what to do with it. Let's print an 1532230587Sken * error, then consume and discard the packet 1533230587Sken */ 1534230587Sken printf("xnb(%s:%d): Unknown extra info type %d." 1535230587Sken " Discarding packet\n", 1536230587Sken __func__, __LINE__, pkt->extra.type); 1537230587Sken xnb_dump_txreq(start, RING_GET_REQUEST(tx_ring, 1538230587Sken start)); 1539230587Sken xnb_dump_txreq(idx, RING_GET_REQUEST(tx_ring, 1540230587Sken idx)); 1541230587Sken discard = 1; 1542230587Sken break; 1543230587Sken } 1544230587Sken 1545230587Sken pkt->extra.flags = ext->flags; 1546230587Sken if (ext->flags & XEN_NETIF_EXTRA_FLAG_MORE) { 1547181643Skmacy /* 1548230587Sken * The reference linux netfront driver never sets this 1549230587Sken * flag (nor does any other known netfront). So we 1550230587Sken * will discard the packet. 1551181643Skmacy */ 1552230587Sken printf("xnb(%s:%d): Request sets " 1553230587Sken "XEN_NETIF_EXTRA_FLAG_MORE, but we can't handle " 1554230587Sken "that\n", __func__, __LINE__); 1555230587Sken xnb_dump_txreq(start, RING_GET_REQUEST(tx_ring, start)); 1556230587Sken xnb_dump_txreq(idx, RING_GET_REQUEST(tx_ring, idx)); 1557230587Sken discard = 1; 1558181643Skmacy } 1559181643Skmacy 1560230587Sken idx++; 1561181643Skmacy } 1562181643Skmacy 1563230587Sken /* Set cdr. If there is not more data, cdr is invalid */ 1564230587Sken pkt->cdr = idx; 1565181643Skmacy 1566230587Sken /* Loop on remainder of packet */ 1567230587Sken while (more_data && RING_HAS_UNCONSUMED_REQUESTS_2(tx_ring, idx)) { 1568230587Sken netif_tx_request_t *tx = RING_GET_REQUEST(tx_ring, idx); 1569230587Sken pkt->list_len++; 1570230587Sken cdr_size += tx->size; 1571230587Sken if (tx->flags & ~NETTXF_more_data) { 1572230587Sken /* There should be no other flags set at this point */ 1573230587Sken printf("xnb(%s:%d): Request sets unknown flags %d " 1574230587Sken "after the 1st request in the packet.\n", 1575230587Sken __func__, __LINE__, tx->flags); 1576230587Sken xnb_dump_txreq(start, RING_GET_REQUEST(tx_ring, start)); 1577230587Sken xnb_dump_txreq(idx, RING_GET_REQUEST(tx_ring, idx)); 1578230587Sken } 1579181643Skmacy 1580230587Sken more_data = tx->flags & NETTXF_more_data; 1581230587Sken idx++; 1582230587Sken } 1583230587Sken 1584230587Sken /* Finalize packet */ 1585230587Sken if (more_data != 0) { 1586230587Sken /* The ring ran out of requests before finishing the packet */ 1587230587Sken xnb_pkt_invalidate(pkt); 1588230587Sken idx = start; /* tell caller that we consumed no requests */ 1589230587Sken } else { 1590230587Sken /* Calculate car_size */ 1591230587Sken pkt->car_size = pkt->size - cdr_size; 1592230587Sken } 1593230587Sken if (discard != 0) { 1594230587Sken xnb_pkt_invalidate(pkt); 1595230587Sken } 1596230587Sken 1597230587Sken return idx - start; 1598181643Skmacy} 1599181643Skmacy 1600230587Sken 1601230587Sken/** 1602230587Sken * Respond to all the requests that constituted pkt. Builds the responses and 1603230587Sken * writes them to the ring, but doesn't push them to the shared ring. 1604230587Sken * \param[in] pkt the packet that needs a response 1605230587Sken * \param[in] error true if there was an error handling the packet, such 1606230587Sken * as in the hypervisor copy op or mbuf allocation 1607230587Sken * \param[out] ring Responses go here 1608230587Sken */ 1609181643Skmacystatic void 1610230587Skenxnb_txpkt2rsp(const struct xnb_pkt *pkt, netif_tx_back_ring_t *ring, 1611230587Sken int error) 1612181643Skmacy{ 1613230587Sken /* 1614230587Sken * Outline: 1615230587Sken * 1) Respond to the first request 1616230587Sken * 2) Respond to the extra info reques 1617230587Sken * Loop through every remaining request in the packet, generating 1618230587Sken * responses that copy those requests' ids and sets the status 1619230587Sken * appropriately. 1620230587Sken */ 1621230587Sken netif_tx_request_t *tx; 1622230587Sken netif_tx_response_t *rsp; 1623230587Sken int i; 1624230587Sken uint16_t status; 1625181643Skmacy 1626230587Sken status = (xnb_pkt_is_valid(pkt) == 0) || error ? 1627230587Sken NETIF_RSP_ERROR : NETIF_RSP_OKAY; 1628230587Sken KASSERT((pkt->list_len == 0) || (ring->rsp_prod_pvt == pkt->car), 1629230587Sken ("Cannot respond to ring requests out of order")); 1630181643Skmacy 1631230587Sken if (pkt->list_len >= 1) { 1632230587Sken uint16_t id; 1633230587Sken tx = RING_GET_REQUEST(ring, ring->rsp_prod_pvt); 1634230587Sken id = tx->id; 1635230587Sken rsp = RING_GET_RESPONSE(ring, ring->rsp_prod_pvt); 1636230587Sken rsp->id = id; 1637230587Sken rsp->status = status; 1638230587Sken ring->rsp_prod_pvt++; 1639181643Skmacy 1640230587Sken if (pkt->flags & NETRXF_extra_info) { 1641230587Sken rsp = RING_GET_RESPONSE(ring, ring->rsp_prod_pvt); 1642230587Sken rsp->status = NETIF_RSP_NULL; 1643230587Sken ring->rsp_prod_pvt++; 1644181643Skmacy } 1645230587Sken } 1646181643Skmacy 1647230587Sken for (i=0; i < pkt->list_len - 1; i++) { 1648230587Sken uint16_t id; 1649230587Sken tx = RING_GET_REQUEST(ring, ring->rsp_prod_pvt); 1650230587Sken id = tx->id; 1651230587Sken rsp = RING_GET_RESPONSE(ring, ring->rsp_prod_pvt); 1652230587Sken rsp->id = id; 1653230587Sken rsp->status = status; 1654230587Sken ring->rsp_prod_pvt++; 1655181643Skmacy } 1656181643Skmacy} 1657181643Skmacy 1658230587Sken/** 1659230587Sken * Create an mbuf chain to represent a packet. Initializes all of the headers 1660230587Sken * in the mbuf chain, but does not copy the data. The returned chain must be 1661230587Sken * free()'d when no longer needed 1662230587Sken * \param[in] pkt A packet to model the mbuf chain after 1663230587Sken * \return A newly allocated mbuf chain, possibly with clusters attached. 1664230587Sken * NULL on failure 1665230587Sken */ 1666230587Skenstatic struct mbuf* 1667230587Skenxnb_pkt2mbufc(const struct xnb_pkt *pkt, struct ifnet *ifp) 1668181643Skmacy{ 1669230587Sken /** 1670230587Sken * \todo consider using a memory pool for mbufs instead of 1671230587Sken * reallocating them for every packet 1672230587Sken */ 1673230587Sken /** \todo handle extra data */ 1674230587Sken struct mbuf *m; 1675181643Skmacy 1676230587Sken m = m_getm(NULL, pkt->size, M_NOWAIT, MT_DATA); 1677181643Skmacy 1678230587Sken if (m != NULL) { 1679230587Sken m->m_pkthdr.rcvif = ifp; 1680230587Sken if (pkt->flags & NETTXF_data_validated) { 1681230587Sken /* 1682230587Sken * We lie to the host OS and always tell it that the 1683230587Sken * checksums are ok, because the packet is unlikely to 1684230587Sken * get corrupted going across domains. 1685230587Sken */ 1686230587Sken m->m_pkthdr.csum_flags = ( 1687230587Sken CSUM_IP_CHECKED | 1688230587Sken CSUM_IP_VALID | 1689230587Sken CSUM_DATA_VALID | 1690230587Sken CSUM_PSEUDO_HDR 1691230587Sken ); 1692230587Sken m->m_pkthdr.csum_data = 0xffff; 1693230587Sken } 1694230587Sken } 1695230587Sken return m; 1696181643Skmacy} 1697181643Skmacy 1698230587Sken/** 1699230587Sken * Build a gnttab_copy table that can be used to copy data from a pkt 1700230587Sken * to an mbufc. Does not actually perform the copy. Always uses gref's on 1701230587Sken * the packet side. 1702230587Sken * \param[in] pkt pkt's associated requests form the src for 1703230587Sken * the copy operation 1704230587Sken * \param[in] mbufc mbufc's storage forms the dest for the copy operation 1705230587Sken * \param[out] gnttab Storage for the returned grant table 1706230587Sken * \param[in] txb Pointer to the backend ring structure 1707230587Sken * \param[in] otherend_id The domain ID of the other end of the copy 1708230587Sken * \return The number of gnttab entries filled 1709230587Sken */ 1710181643Skmacystatic int 1711230587Skenxnb_txpkt2gnttab(const struct xnb_pkt *pkt, const struct mbuf *mbufc, 1712230587Sken gnttab_copy_table gnttab, const netif_tx_back_ring_t *txb, 1713230587Sken domid_t otherend_id) 1714181643Skmacy{ 1715181643Skmacy 1716230587Sken const struct mbuf *mbuf = mbufc;/* current mbuf within the chain */ 1717230587Sken int gnt_idx = 0; /* index into grant table */ 1718230587Sken RING_IDX r_idx = pkt->car; /* index into tx ring buffer */ 1719230587Sken int r_ofs = 0; /* offset of next data within tx request's data area */ 1720230587Sken int m_ofs = 0; /* offset of next data within mbuf's data area */ 1721230587Sken /* size in bytes that still needs to be represented in the table */ 1722230587Sken uint16_t size_remaining = pkt->size; 1723181643Skmacy 1724230587Sken while (size_remaining > 0) { 1725230587Sken const netif_tx_request_t *txq = RING_GET_REQUEST(txb, r_idx); 1726230587Sken const size_t mbuf_space = M_TRAILINGSPACE(mbuf) - m_ofs; 1727230587Sken const size_t req_size = 1728230587Sken r_idx == pkt->car ? pkt->car_size : txq->size; 1729230587Sken const size_t pkt_space = req_size - r_ofs; 1730230587Sken /* 1731230587Sken * space is the largest amount of data that can be copied in the 1732230587Sken * grant table's next entry 1733230587Sken */ 1734230587Sken const size_t space = MIN(pkt_space, mbuf_space); 1735230587Sken 1736230587Sken /* TODO: handle this error condition without panicking */ 1737230587Sken KASSERT(gnt_idx < GNTTAB_LEN, ("Grant table is too short")); 1738230587Sken 1739230587Sken gnttab[gnt_idx].source.u.ref = txq->gref; 1740230587Sken gnttab[gnt_idx].source.domid = otherend_id; 1741230587Sken gnttab[gnt_idx].source.offset = txq->offset + r_ofs; 1742230587Sken gnttab[gnt_idx].dest.u.gmfn = virt_to_mfn( 1743230587Sken mtod(mbuf, vm_offset_t) + m_ofs); 1744230587Sken gnttab[gnt_idx].dest.offset = virt_to_offset( 1745230587Sken mtod(mbuf, vm_offset_t) + m_ofs); 1746230587Sken gnttab[gnt_idx].dest.domid = DOMID_SELF; 1747230587Sken gnttab[gnt_idx].len = space; 1748230587Sken gnttab[gnt_idx].flags = GNTCOPY_source_gref; 1749230587Sken 1750230587Sken gnt_idx++; 1751230587Sken r_ofs += space; 1752230587Sken m_ofs += space; 1753230587Sken size_remaining -= space; 1754230587Sken if (req_size - r_ofs <= 0) { 1755230587Sken /* Must move to the next tx request */ 1756230587Sken r_ofs = 0; 1757230587Sken r_idx = (r_idx == pkt->car) ? pkt->cdr : r_idx + 1; 1758230587Sken } 1759230587Sken if (M_TRAILINGSPACE(mbuf) - m_ofs <= 0) { 1760230587Sken /* Must move to the next mbuf */ 1761230587Sken m_ofs = 0; 1762230587Sken mbuf = mbuf->m_next; 1763230587Sken } 1764181643Skmacy } 1765181643Skmacy 1766230587Sken return gnt_idx; 1767181643Skmacy} 1768181643Skmacy 1769230587Sken/** 1770230587Sken * Check the status of the grant copy operations, and update mbufs various 1771230587Sken * non-data fields to reflect the data present. 1772230587Sken * \param[in,out] mbufc mbuf chain to update. The chain must be valid and of 1773230587Sken * the correct length, and data should already be present 1774230587Sken * \param[in] gnttab A grant table for a just completed copy op 1775230587Sken * \param[in] n_entries The number of valid entries in the grant table 1776230587Sken */ 1777181643Skmacystatic void 1778230587Skenxnb_update_mbufc(struct mbuf *mbufc, const gnttab_copy_table gnttab, 1779230587Sken int n_entries) 1780181643Skmacy{ 1781230587Sken struct mbuf *mbuf = mbufc; 1782230587Sken int i; 1783230587Sken size_t total_size = 0; 1784181643Skmacy 1785230587Sken for (i = 0; i < n_entries; i++) { 1786230587Sken KASSERT(gnttab[i].status == GNTST_okay, 1787230587Sken ("Some gnttab_copy entry had error status %hd\n", 1788230587Sken gnttab[i].status)); 1789181643Skmacy 1790230587Sken mbuf->m_len += gnttab[i].len; 1791230587Sken total_size += gnttab[i].len; 1792230587Sken if (M_TRAILINGSPACE(mbuf) <= 0) { 1793230587Sken mbuf = mbuf->m_next; 1794230587Sken } 1795230587Sken } 1796230587Sken mbufc->m_pkthdr.len = total_size; 1797230587Sken 1798257515Sglebius#if defined(INET) || defined(INET6) 1799230587Sken xnb_add_mbuf_cksum(mbufc); 1800257515Sglebius#endif 1801181643Skmacy} 1802181643Skmacy 1803230587Sken/** 1804230587Sken * Dequeue at most one packet from the shared ring 1805230587Sken * \param[in,out] txb Netif tx ring. A packet will be removed from it, and 1806230587Sken * its private indices will be updated. But the indices 1807230587Sken * will not be pushed to the shared ring. 1808230587Sken * \param[in] ifnet Interface to which the packet will be sent 1809230587Sken * \param[in] otherend Domain ID of the other end of the ring 1810230587Sken * \param[out] mbufc The assembled mbuf chain, ready to send to the generic 1811230587Sken * networking stack 1812230587Sken * \param[in,out] gnttab Pointer to enough memory for a grant table. We make 1813230587Sken * this a function parameter so that we will take less 1814230587Sken * stack space. 1815230587Sken * \return An error code 1816230587Sken */ 1817181643Skmacystatic int 1818230587Skenxnb_recv(netif_tx_back_ring_t *txb, domid_t otherend, struct mbuf **mbufc, 1819230587Sken struct ifnet *ifnet, gnttab_copy_table gnttab) 1820181643Skmacy{ 1821230587Sken struct xnb_pkt pkt; 1822230587Sken /* number of tx requests consumed to build the last packet */ 1823230587Sken int num_consumed; 1824230587Sken int nr_ents; 1825181643Skmacy 1826230587Sken *mbufc = NULL; 1827230587Sken num_consumed = xnb_ring2pkt(&pkt, txb, txb->req_cons); 1828230587Sken if (num_consumed == 0) 1829230587Sken return 0; /* Nothing to receive */ 1830181643Skmacy 1831249588Sgabor /* update statistics independent of errors */ 1832271849Sglebius if_inc_counter(ifnet, IFCOUNTER_IPACKETS, 1); 1833181643Skmacy 1834230587Sken /* 1835230587Sken * if we got here, then 1 or more requests was consumed, but the packet 1836249583Sgabor * is not necessarily valid. 1837230587Sken */ 1838230587Sken if (xnb_pkt_is_valid(&pkt) == 0) { 1839230587Sken /* got a garbage packet, respond and drop it */ 1840230587Sken xnb_txpkt2rsp(&pkt, txb, 1); 1841230587Sken txb->req_cons += num_consumed; 1842230587Sken DPRINTF("xnb_intr: garbage packet, num_consumed=%d\n", 1843230587Sken num_consumed); 1844271849Sglebius if_inc_counter(ifnet, IFCOUNTER_IERRORS, 1); 1845230587Sken return EINVAL; 1846181643Skmacy } 1847181643Skmacy 1848230587Sken *mbufc = xnb_pkt2mbufc(&pkt, ifnet); 1849230587Sken 1850230587Sken if (*mbufc == NULL) { 1851230587Sken /* 1852230587Sken * Couldn't allocate mbufs. Respond and drop the packet. Do 1853230587Sken * not consume the requests 1854230587Sken */ 1855230587Sken xnb_txpkt2rsp(&pkt, txb, 1); 1856230587Sken DPRINTF("xnb_intr: Couldn't allocate mbufs, num_consumed=%d\n", 1857230587Sken num_consumed); 1858271849Sglebius if_inc_counter(ifnet, IFCOUNTER_IQDROPS, 1); 1859230587Sken return ENOMEM; 1860181643Skmacy } 1861181643Skmacy 1862230587Sken nr_ents = xnb_txpkt2gnttab(&pkt, *mbufc, gnttab, txb, otherend); 1863181643Skmacy 1864230587Sken if (nr_ents > 0) { 1865230587Sken int __unused hv_ret = HYPERVISOR_grant_table_op(GNTTABOP_copy, 1866230587Sken gnttab, nr_ents); 1867230587Sken KASSERT(hv_ret == 0, 1868230587Sken ("HYPERVISOR_grant_table_op returned %d\n", hv_ret)); 1869230587Sken xnb_update_mbufc(*mbufc, gnttab, nr_ents); 1870230587Sken } 1871181643Skmacy 1872230587Sken xnb_txpkt2rsp(&pkt, txb, 0); 1873230587Sken txb->req_cons += num_consumed; 1874181643Skmacy return 0; 1875181643Skmacy} 1876181643Skmacy 1877230587Sken/** 1878230587Sken * Create an xnb_pkt based on the contents of an mbuf chain. 1879230587Sken * \param[in] mbufc mbuf chain to transform into a packet 1880230587Sken * \param[out] pkt Storage for the newly generated xnb_pkt 1881230587Sken * \param[in] start The ring index of the first available slot in the rx 1882230587Sken * ring 1883230587Sken * \param[in] space The number of free slots in the rx ring 1884230587Sken * \retval 0 Success 1885230587Sken * \retval EINVAL mbufc was corrupt or not convertible into a pkt 1886230587Sken * \retval EAGAIN There was not enough space in the ring to queue the 1887230587Sken * packet 1888230587Sken */ 1889230587Skenstatic int 1890230587Skenxnb_mbufc2pkt(const struct mbuf *mbufc, struct xnb_pkt *pkt, 1891230587Sken RING_IDX start, int space) 1892181643Skmacy{ 1893181643Skmacy 1894230587Sken int retval = 0; 1895181643Skmacy 1896230587Sken if ((mbufc == NULL) || 1897230587Sken ( (mbufc->m_flags & M_PKTHDR) == 0) || 1898230587Sken (mbufc->m_pkthdr.len == 0)) { 1899230587Sken xnb_pkt_invalidate(pkt); 1900230587Sken retval = EINVAL; 1901230587Sken } else { 1902230587Sken int slots_required; 1903181643Skmacy 1904230587Sken xnb_pkt_validate(pkt); 1905230587Sken pkt->flags = 0; 1906230587Sken pkt->size = mbufc->m_pkthdr.len; 1907230587Sken pkt->car = start; 1908230587Sken pkt->car_size = mbufc->m_len; 1909181643Skmacy 1910230587Sken if (mbufc->m_pkthdr.csum_flags & CSUM_TSO) { 1911230587Sken pkt->flags |= NETRXF_extra_info; 1912230587Sken pkt->extra.u.gso.size = mbufc->m_pkthdr.tso_segsz; 1913230587Sken pkt->extra.u.gso.type = XEN_NETIF_GSO_TYPE_TCPV4; 1914230587Sken pkt->extra.u.gso.pad = 0; 1915230587Sken pkt->extra.u.gso.features = 0; 1916230587Sken pkt->extra.type = XEN_NETIF_EXTRA_TYPE_GSO; 1917230587Sken pkt->extra.flags = 0; 1918230587Sken pkt->cdr = start + 2; 1919230587Sken } else { 1920230587Sken pkt->cdr = start + 1; 1921230587Sken } 1922230587Sken if (mbufc->m_pkthdr.csum_flags & (CSUM_TSO | CSUM_DELAY_DATA)) { 1923230587Sken pkt->flags |= 1924230587Sken (NETRXF_csum_blank | NETRXF_data_validated); 1925230587Sken } 1926230587Sken 1927230587Sken /* 1928230587Sken * Each ring response can have up to PAGE_SIZE of data. 1929230587Sken * Assume that we can defragment the mbuf chain efficiently 1930230587Sken * into responses so that each response but the last uses all 1931230587Sken * PAGE_SIZE bytes. 1932230587Sken */ 1933230587Sken pkt->list_len = (pkt->size + PAGE_SIZE - 1) / PAGE_SIZE; 1934230587Sken 1935230587Sken if (pkt->list_len > 1) { 1936230587Sken pkt->flags |= NETRXF_more_data; 1937230587Sken } 1938230587Sken 1939230587Sken slots_required = pkt->list_len + 1940230587Sken (pkt->flags & NETRXF_extra_info ? 1 : 0); 1941230587Sken if (slots_required > space) { 1942230587Sken xnb_pkt_invalidate(pkt); 1943230587Sken retval = EAGAIN; 1944230587Sken } 1945181643Skmacy } 1946230587Sken 1947230587Sken return retval; 1948181643Skmacy} 1949181643Skmacy 1950230587Sken/** 1951230587Sken * Build a gnttab_copy table that can be used to copy data from an mbuf chain 1952230587Sken * to the frontend's shared buffers. Does not actually perform the copy. 1953230587Sken * Always uses gref's on the other end's side. 1954230587Sken * \param[in] pkt pkt's associated responses form the dest for the copy 1955230587Sken * operatoin 1956230587Sken * \param[in] mbufc The source for the copy operation 1957230587Sken * \param[out] gnttab Storage for the returned grant table 1958230587Sken * \param[in] rxb Pointer to the backend ring structure 1959230587Sken * \param[in] otherend_id The domain ID of the other end of the copy 1960230587Sken * \return The number of gnttab entries filled 1961230587Sken */ 1962181643Skmacystatic int 1963230587Skenxnb_rxpkt2gnttab(const struct xnb_pkt *pkt, const struct mbuf *mbufc, 1964230587Sken gnttab_copy_table gnttab, const netif_rx_back_ring_t *rxb, 1965230587Sken domid_t otherend_id) 1966181643Skmacy{ 1967181643Skmacy 1968230587Sken const struct mbuf *mbuf = mbufc;/* current mbuf within the chain */ 1969230587Sken int gnt_idx = 0; /* index into grant table */ 1970230587Sken RING_IDX r_idx = pkt->car; /* index into rx ring buffer */ 1971230587Sken int r_ofs = 0; /* offset of next data within rx request's data area */ 1972230587Sken int m_ofs = 0; /* offset of next data within mbuf's data area */ 1973230587Sken /* size in bytes that still needs to be represented in the table */ 1974230587Sken uint16_t size_remaining; 1975181643Skmacy 1976230587Sken size_remaining = (xnb_pkt_is_valid(pkt) != 0) ? pkt->size : 0; 1977230587Sken 1978230587Sken while (size_remaining > 0) { 1979230587Sken const netif_rx_request_t *rxq = RING_GET_REQUEST(rxb, r_idx); 1980230587Sken const size_t mbuf_space = mbuf->m_len - m_ofs; 1981230587Sken /* Xen shared pages have an implied size of PAGE_SIZE */ 1982230587Sken const size_t req_size = PAGE_SIZE; 1983230587Sken const size_t pkt_space = req_size - r_ofs; 1984230587Sken /* 1985230587Sken * space is the largest amount of data that can be copied in the 1986230587Sken * grant table's next entry 1987230587Sken */ 1988230587Sken const size_t space = MIN(pkt_space, mbuf_space); 1989230587Sken 1990230587Sken /* TODO: handle this error condition without panicing */ 1991230587Sken KASSERT(gnt_idx < GNTTAB_LEN, ("Grant table is too short")); 1992230587Sken 1993230587Sken gnttab[gnt_idx].dest.u.ref = rxq->gref; 1994230587Sken gnttab[gnt_idx].dest.domid = otherend_id; 1995230587Sken gnttab[gnt_idx].dest.offset = r_ofs; 1996230587Sken gnttab[gnt_idx].source.u.gmfn = virt_to_mfn( 1997230587Sken mtod(mbuf, vm_offset_t) + m_ofs); 1998230587Sken gnttab[gnt_idx].source.offset = virt_to_offset( 1999230587Sken mtod(mbuf, vm_offset_t) + m_ofs); 2000230587Sken gnttab[gnt_idx].source.domid = DOMID_SELF; 2001230587Sken gnttab[gnt_idx].len = space; 2002230587Sken gnttab[gnt_idx].flags = GNTCOPY_dest_gref; 2003230587Sken 2004230587Sken gnt_idx++; 2005230587Sken 2006230587Sken r_ofs += space; 2007230587Sken m_ofs += space; 2008230587Sken size_remaining -= space; 2009230587Sken if (req_size - r_ofs <= 0) { 2010230587Sken /* Must move to the next rx request */ 2011230587Sken r_ofs = 0; 2012230587Sken r_idx = (r_idx == pkt->car) ? pkt->cdr : r_idx + 1; 2013230587Sken } 2014230587Sken if (mbuf->m_len - m_ofs <= 0) { 2015230587Sken /* Must move to the next mbuf */ 2016230587Sken m_ofs = 0; 2017230587Sken mbuf = mbuf->m_next; 2018230587Sken } 2019181643Skmacy } 2020181643Skmacy 2021230587Sken return gnt_idx; 2022181643Skmacy} 2023181643Skmacy 2024181643Skmacy/** 2025230587Sken * Generates responses for all the requests that constituted pkt. Builds 2026230587Sken * responses and writes them to the ring, but doesn't push the shared ring 2027230587Sken * indices. 2028230587Sken * \param[in] pkt the packet that needs a response 2029230587Sken * \param[in] gnttab The grant copy table corresponding to this packet. 2030230587Sken * Used to determine how many rsp->netif_rx_response_t's to 2031230587Sken * generate. 2032230587Sken * \param[in] n_entries Number of relevant entries in the grant table 2033230587Sken * \param[out] ring Responses go here 2034230587Sken * \return The number of RX requests that were consumed to generate 2035230587Sken * the responses 2036181643Skmacy */ 2037181643Skmacystatic int 2038230587Skenxnb_rxpkt2rsp(const struct xnb_pkt *pkt, const gnttab_copy_table gnttab, 2039230587Sken int n_entries, netif_rx_back_ring_t *ring) 2040181643Skmacy{ 2041230587Sken /* 2042230587Sken * This code makes the following assumptions: 2043230587Sken * * All entries in gnttab set GNTCOPY_dest_gref 2044230587Sken * * The entries in gnttab are grouped by their grefs: any two 2045230587Sken * entries with the same gref must be adjacent 2046230587Sken */ 2047230587Sken int error = 0; 2048230587Sken int gnt_idx, i; 2049230587Sken int n_responses = 0; 2050230587Sken grant_ref_t last_gref = GRANT_REF_INVALID; 2051230587Sken RING_IDX r_idx; 2052181643Skmacy 2053230587Sken KASSERT(gnttab != NULL, ("Received a null granttable copy")); 2054230587Sken 2055230587Sken /* 2056230587Sken * In the event of an error, we only need to send one response to the 2057230587Sken * netfront. In that case, we musn't write any data to the responses 2058230587Sken * after the one we send. So we must loop all the way through gnttab 2059230587Sken * looking for errors before we generate any responses 2060230587Sken * 2061230587Sken * Since we're looping through the grant table anyway, we'll count the 2062230587Sken * number of different gref's in it, which will tell us how many 2063230587Sken * responses to generate 2064230587Sken */ 2065230587Sken for (gnt_idx = 0; gnt_idx < n_entries; gnt_idx++) { 2066230587Sken int16_t status = gnttab[gnt_idx].status; 2067230587Sken if (status != GNTST_okay) { 2068230587Sken DPRINTF( 2069230587Sken "Got error %d for hypervisor gnttab_copy status\n", 2070230587Sken status); 2071230587Sken error = 1; 2072230587Sken break; 2073230587Sken } 2074230587Sken if (gnttab[gnt_idx].dest.u.ref != last_gref) { 2075230587Sken n_responses++; 2076230587Sken last_gref = gnttab[gnt_idx].dest.u.ref; 2077230587Sken } 2078181643Skmacy } 2079181643Skmacy 2080230587Sken if (error != 0) { 2081230587Sken uint16_t id; 2082230587Sken netif_rx_response_t *rsp; 2083230587Sken 2084230587Sken id = RING_GET_REQUEST(ring, ring->rsp_prod_pvt)->id; 2085230587Sken rsp = RING_GET_RESPONSE(ring, ring->rsp_prod_pvt); 2086230587Sken rsp->id = id; 2087230587Sken rsp->status = NETIF_RSP_ERROR; 2088230587Sken n_responses = 1; 2089230587Sken } else { 2090230587Sken gnt_idx = 0; 2091230587Sken const int has_extra = pkt->flags & NETRXF_extra_info; 2092230587Sken if (has_extra != 0) 2093230587Sken n_responses++; 2094181643Skmacy 2095230587Sken for (i = 0; i < n_responses; i++) { 2096230587Sken netif_rx_request_t rxq; 2097230587Sken netif_rx_response_t *rsp; 2098181643Skmacy 2099230587Sken r_idx = ring->rsp_prod_pvt + i; 2100230587Sken /* 2101230587Sken * We copy the structure of rxq instead of making a 2102230587Sken * pointer because it shares the same memory as rsp. 2103230587Sken */ 2104230587Sken rxq = *(RING_GET_REQUEST(ring, r_idx)); 2105230587Sken rsp = RING_GET_RESPONSE(ring, r_idx); 2106230587Sken if (has_extra && (i == 1)) { 2107230587Sken netif_extra_info_t *ext = 2108230587Sken (netif_extra_info_t*)rsp; 2109230587Sken ext->type = XEN_NETIF_EXTRA_TYPE_GSO; 2110230587Sken ext->flags = 0; 2111230587Sken ext->u.gso.size = pkt->extra.u.gso.size; 2112230587Sken ext->u.gso.type = XEN_NETIF_GSO_TYPE_TCPV4; 2113230587Sken ext->u.gso.pad = 0; 2114230587Sken ext->u.gso.features = 0; 2115230587Sken } else { 2116230587Sken rsp->id = rxq.id; 2117230587Sken rsp->status = GNTST_okay; 2118230587Sken rsp->offset = 0; 2119230587Sken rsp->flags = 0; 2120230587Sken if (i < pkt->list_len - 1) 2121230587Sken rsp->flags |= NETRXF_more_data; 2122230587Sken if ((i == 0) && has_extra) 2123230587Sken rsp->flags |= NETRXF_extra_info; 2124230587Sken if ((i == 0) && 2125230587Sken (pkt->flags & NETRXF_data_validated)) { 2126230587Sken rsp->flags |= NETRXF_data_validated; 2127230587Sken rsp->flags |= NETRXF_csum_blank; 2128230587Sken } 2129230587Sken rsp->status = 0; 2130230587Sken for (; gnttab[gnt_idx].dest.u.ref == rxq.gref; 2131230587Sken gnt_idx++) { 2132230587Sken rsp->status += gnttab[gnt_idx].len; 2133230587Sken } 2134230587Sken } 2135230587Sken } 2136181643Skmacy } 2137181643Skmacy 2138230587Sken ring->req_cons += n_responses; 2139230587Sken ring->rsp_prod_pvt += n_responses; 2140230587Sken return n_responses; 2141181643Skmacy} 2142181643Skmacy 2143257515Sglebius#if defined(INET) || defined(INET6) 2144181643Skmacy/** 2145230587Sken * Add IP, TCP, and/or UDP checksums to every mbuf in a chain. The first mbuf 2146230587Sken * in the chain must start with a struct ether_header. 2147230587Sken * 2148230587Sken * XXX This function will perform incorrectly on UDP packets that are split up 2149230587Sken * into multiple ethernet frames. 2150181643Skmacy */ 2151230587Skenstatic void 2152230587Skenxnb_add_mbuf_cksum(struct mbuf *mbufc) 2153181643Skmacy{ 2154230587Sken struct ether_header *eh; 2155230587Sken struct ip *iph; 2156230587Sken uint16_t ether_type; 2157181643Skmacy 2158230587Sken eh = mtod(mbufc, struct ether_header*); 2159230587Sken ether_type = ntohs(eh->ether_type); 2160230587Sken if (ether_type != ETHERTYPE_IP) { 2161230587Sken /* Nothing to calculate */ 2162230587Sken return; 2163230587Sken } 2164181643Skmacy 2165230587Sken iph = (struct ip*)(eh + 1); 2166230587Sken if (mbufc->m_pkthdr.csum_flags & CSUM_IP_VALID) { 2167230587Sken iph->ip_sum = 0; 2168230587Sken iph->ip_sum = in_cksum_hdr(iph); 2169230587Sken } 2170181643Skmacy 2171230587Sken switch (iph->ip_p) { 2172230587Sken case IPPROTO_TCP: 2173230587Sken if (mbufc->m_pkthdr.csum_flags & CSUM_IP_VALID) { 2174230587Sken size_t tcplen = ntohs(iph->ip_len) - sizeof(struct ip); 2175230587Sken struct tcphdr *th = (struct tcphdr*)(iph + 1); 2176230587Sken th->th_sum = in_pseudo(iph->ip_src.s_addr, 2177230587Sken iph->ip_dst.s_addr, htons(IPPROTO_TCP + tcplen)); 2178230587Sken th->th_sum = in_cksum_skip(mbufc, 2179230587Sken sizeof(struct ether_header) + ntohs(iph->ip_len), 2180230587Sken sizeof(struct ether_header) + (iph->ip_hl << 2)); 2181230587Sken } 2182181643Skmacy break; 2183230587Sken case IPPROTO_UDP: 2184230587Sken if (mbufc->m_pkthdr.csum_flags & CSUM_IP_VALID) { 2185230587Sken size_t udplen = ntohs(iph->ip_len) - sizeof(struct ip); 2186230587Sken struct udphdr *uh = (struct udphdr*)(iph + 1); 2187230587Sken uh->uh_sum = in_pseudo(iph->ip_src.s_addr, 2188230587Sken iph->ip_dst.s_addr, htons(IPPROTO_UDP + udplen)); 2189230587Sken uh->uh_sum = in_cksum_skip(mbufc, 2190230587Sken sizeof(struct ether_header) + ntohs(iph->ip_len), 2191230587Sken sizeof(struct ether_header) + (iph->ip_hl << 2)); 2192230587Sken } 2193181643Skmacy break; 2194230587Sken default: 2195181643Skmacy break; 2196181643Skmacy } 2197181643Skmacy} 2198257515Sglebius#endif /* INET || INET6 */ 2199181643Skmacy 2200181643Skmacystatic void 2201230587Skenxnb_stop(struct xnb_softc *xnb) 2202181643Skmacy{ 2203230587Sken struct ifnet *ifp; 2204181643Skmacy 2205230587Sken mtx_assert(&xnb->sc_lock, MA_OWNED); 2206230587Sken ifp = xnb->xnb_ifp; 2207230587Sken ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE); 2208230587Sken if_link_state_change(ifp, LINK_STATE_DOWN); 2209181643Skmacy} 2210181643Skmacy 2211181643Skmacystatic int 2212230587Skenxnb_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data) 2213181643Skmacy{ 2214230587Sken struct xnb_softc *xnb = ifp->if_softc; 2215256868Sbz struct ifreq *ifr = (struct ifreq*) data; 2216230587Sken#ifdef INET 2217230587Sken struct ifaddr *ifa = (struct ifaddr*)data; 2218230587Sken#endif 2219230587Sken int error = 0; 2220181643Skmacy 2221230587Sken switch (cmd) { 2222230587Sken case SIOCSIFFLAGS: 2223230587Sken mtx_lock(&xnb->sc_lock); 2224230587Sken if (ifp->if_flags & IFF_UP) { 2225230587Sken xnb_ifinit_locked(xnb); 2226230587Sken } else { 2227230587Sken if (ifp->if_drv_flags & IFF_DRV_RUNNING) { 2228230587Sken xnb_stop(xnb); 2229230587Sken } 2230230587Sken } 2231230587Sken /* 2232230587Sken * Note: netfront sets a variable named xn_if_flags 2233230587Sken * here, but that variable is never read 2234230587Sken */ 2235230587Sken mtx_unlock(&xnb->sc_lock); 2236230587Sken break; 2237230587Sken case SIOCSIFADDR: 2238230587Sken case SIOCGIFADDR: 2239230587Sken#ifdef INET 2240230587Sken mtx_lock(&xnb->sc_lock); 2241230587Sken if (ifa->ifa_addr->sa_family == AF_INET) { 2242230587Sken ifp->if_flags |= IFF_UP; 2243230587Sken if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) { 2244230587Sken ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | 2245230587Sken IFF_DRV_OACTIVE); 2246230587Sken if_link_state_change(ifp, 2247230587Sken LINK_STATE_DOWN); 2248230587Sken ifp->if_drv_flags |= IFF_DRV_RUNNING; 2249230587Sken ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 2250230587Sken if_link_state_change(ifp, 2251230587Sken LINK_STATE_UP); 2252230587Sken } 2253230587Sken arp_ifinit(ifp, ifa); 2254230587Sken mtx_unlock(&xnb->sc_lock); 2255230587Sken } else { 2256230587Sken mtx_unlock(&xnb->sc_lock); 2257230587Sken#endif 2258230587Sken error = ether_ioctl(ifp, cmd, data); 2259230587Sken#ifdef INET 2260230587Sken } 2261230587Sken#endif 2262230587Sken break; 2263230587Sken case SIOCSIFCAP: 2264230587Sken mtx_lock(&xnb->sc_lock); 2265230587Sken if (ifr->ifr_reqcap & IFCAP_TXCSUM) { 2266230587Sken ifp->if_capenable |= IFCAP_TXCSUM; 2267230587Sken ifp->if_hwassist |= XNB_CSUM_FEATURES; 2268230587Sken } else { 2269230587Sken ifp->if_capenable &= ~(IFCAP_TXCSUM); 2270230587Sken ifp->if_hwassist &= ~(XNB_CSUM_FEATURES); 2271230587Sken } 2272230587Sken if ((ifr->ifr_reqcap & IFCAP_RXCSUM)) { 2273230587Sken ifp->if_capenable |= IFCAP_RXCSUM; 2274230587Sken } else { 2275230587Sken ifp->if_capenable &= ~(IFCAP_RXCSUM); 2276230587Sken } 2277230587Sken /* 2278230587Sken * TODO enable TSO4 and LRO once we no longer need 2279230587Sken * to calculate checksums in software 2280230587Sken */ 2281230587Sken#if 0 2282230587Sken if (ifr->if_reqcap |= IFCAP_TSO4) { 2283230587Sken if (IFCAP_TXCSUM & ifp->if_capenable) { 2284230587Sken printf("xnb: Xen netif requires that " 2285230587Sken "TXCSUM be enabled in order " 2286230587Sken "to use TSO4\n"); 2287230587Sken error = EINVAL; 2288230587Sken } else { 2289230587Sken ifp->if_capenable |= IFCAP_TSO4; 2290230587Sken ifp->if_hwassist |= CSUM_TSO; 2291230587Sken } 2292230587Sken } else { 2293230587Sken ifp->if_capenable &= ~(IFCAP_TSO4); 2294230587Sken ifp->if_hwassist &= ~(CSUM_TSO); 2295230587Sken } 2296230587Sken if (ifr->ifreqcap |= IFCAP_LRO) { 2297230587Sken ifp->if_capenable |= IFCAP_LRO; 2298230587Sken } else { 2299230587Sken ifp->if_capenable &= ~(IFCAP_LRO); 2300230587Sken } 2301230587Sken#endif 2302230587Sken mtx_unlock(&xnb->sc_lock); 2303230587Sken break; 2304230587Sken case SIOCSIFMTU: 2305230587Sken ifp->if_mtu = ifr->ifr_mtu; 2306230587Sken ifp->if_drv_flags &= ~IFF_DRV_RUNNING; 2307230587Sken xnb_ifinit(xnb); 2308230587Sken break; 2309230587Sken case SIOCADDMULTI: 2310230587Sken case SIOCDELMULTI: 2311230587Sken case SIOCSIFMEDIA: 2312230587Sken case SIOCGIFMEDIA: 2313230587Sken error = ifmedia_ioctl(ifp, ifr, &xnb->sc_media, cmd); 2314230587Sken break; 2315230587Sken default: 2316230587Sken error = ether_ioctl(ifp, cmd, data); 2317230587Sken break; 2318181643Skmacy } 2319230587Sken return (error); 2320230587Sken} 2321181643Skmacy 2322230587Skenstatic void 2323230587Skenxnb_start_locked(struct ifnet *ifp) 2324230587Sken{ 2325230587Sken netif_rx_back_ring_t *rxb; 2326230587Sken struct xnb_softc *xnb; 2327230587Sken struct mbuf *mbufc; 2328230587Sken RING_IDX req_prod_local; 2329181643Skmacy 2330230587Sken xnb = ifp->if_softc; 2331230587Sken rxb = &xnb->ring_configs[XNB_RING_TYPE_RX].back_ring.rx_ring; 2332181643Skmacy 2333230587Sken if (!xnb->carrier) 2334230587Sken return; 2335181643Skmacy 2336230587Sken do { 2337230587Sken int out_of_space = 0; 2338230587Sken int notify; 2339230587Sken req_prod_local = rxb->sring->req_prod; 2340230587Sken xen_rmb(); 2341230587Sken for (;;) { 2342230587Sken int error; 2343181643Skmacy 2344230587Sken IF_DEQUEUE(&ifp->if_snd, mbufc); 2345230587Sken if (mbufc == NULL) 2346230587Sken break; 2347230587Sken error = xnb_send(rxb, xnb->otherend_id, mbufc, 2348230587Sken xnb->rx_gnttab); 2349230587Sken switch (error) { 2350230587Sken case EAGAIN: 2351230587Sken /* 2352230587Sken * Insufficient space in the ring. 2353230587Sken * Requeue pkt and send when space is 2354230587Sken * available. 2355230587Sken */ 2356230587Sken IF_PREPEND(&ifp->if_snd, mbufc); 2357230587Sken /* 2358230587Sken * Perhaps the frontend missed an IRQ 2359230587Sken * and went to sleep. Notify it to wake 2360230587Sken * it up. 2361230587Sken */ 2362230587Sken out_of_space = 1; 2363230587Sken break; 2364181643Skmacy 2365230587Sken case EINVAL: 2366230587Sken /* OS gave a corrupt packet. Drop it.*/ 2367271849Sglebius if_inc_counter(ifp, IFCOUNTER_OERRORS, 1); 2368230587Sken /* FALLTHROUGH */ 2369230587Sken default: 2370230587Sken /* Send succeeded, or packet had error. 2371230587Sken * Free the packet */ 2372271849Sglebius if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1); 2373230587Sken if (mbufc) 2374230587Sken m_freem(mbufc); 2375230587Sken break; 2376230587Sken } 2377230587Sken if (out_of_space != 0) 2378230587Sken break; 2379230587Sken } 2380181643Skmacy 2381230587Sken RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(rxb, notify); 2382230587Sken if ((notify != 0) || (out_of_space != 0)) 2383255040Sgibbs xen_intr_signal(xnb->xen_intr_handle); 2384230587Sken rxb->sring->req_event = req_prod_local + 1; 2385230587Sken xen_mb(); 2386230587Sken } while (rxb->sring->req_prod != req_prod_local) ; 2387181643Skmacy} 2388181643Skmacy 2389230587Sken/** 2390230587Sken * Sends one packet to the ring. Blocks until the packet is on the ring 2391230587Sken * \param[in] mbufc Contains one packet to send. Caller must free 2392230587Sken * \param[in,out] rxb The packet will be pushed onto this ring, but the 2393230587Sken * otherend will not be notified. 2394230587Sken * \param[in] otherend The domain ID of the other end of the connection 2395230587Sken * \retval EAGAIN The ring did not have enough space for the packet. 2396230587Sken * The ring has not been modified 2397230587Sken * \param[in,out] gnttab Pointer to enough memory for a grant table. We make 2398230587Sken * this a function parameter so that we will take less 2399230587Sken * stack space. 2400230587Sken * \retval EINVAL mbufc was corrupt or not convertible into a pkt 2401230587Sken */ 2402181643Skmacystatic int 2403230587Skenxnb_send(netif_rx_back_ring_t *ring, domid_t otherend, const struct mbuf *mbufc, 2404230587Sken gnttab_copy_table gnttab) 2405181643Skmacy{ 2406230587Sken struct xnb_pkt pkt; 2407230587Sken int error, n_entries, n_reqs; 2408230587Sken RING_IDX space; 2409181643Skmacy 2410230587Sken space = ring->sring->req_prod - ring->req_cons; 2411230587Sken error = xnb_mbufc2pkt(mbufc, &pkt, ring->rsp_prod_pvt, space); 2412230587Sken if (error != 0) 2413230587Sken return error; 2414230587Sken n_entries = xnb_rxpkt2gnttab(&pkt, mbufc, gnttab, ring, otherend); 2415230587Sken if (n_entries != 0) { 2416230587Sken int __unused hv_ret = HYPERVISOR_grant_table_op(GNTTABOP_copy, 2417230587Sken gnttab, n_entries); 2418230587Sken KASSERT(hv_ret == 0, ("HYPERVISOR_grant_table_op returned %d\n", 2419230587Sken hv_ret)); 2420181643Skmacy } 2421181643Skmacy 2422230587Sken n_reqs = xnb_rxpkt2rsp(&pkt, gnttab, n_entries, ring); 2423181643Skmacy 2424230587Sken return 0; 2425181643Skmacy} 2426181643Skmacy 2427230587Skenstatic void 2428230587Skenxnb_start(struct ifnet *ifp) 2429181643Skmacy{ 2430230587Sken struct xnb_softc *xnb; 2431230587Sken 2432230587Sken xnb = ifp->if_softc; 2433230587Sken mtx_lock(&xnb->rx_lock); 2434230587Sken xnb_start_locked(ifp); 2435230587Sken mtx_unlock(&xnb->rx_lock); 2436181643Skmacy} 2437181643Skmacy 2438230587Sken/* equivalent of network_open() in Linux */ 2439230587Skenstatic void 2440230587Skenxnb_ifinit_locked(struct xnb_softc *xnb) 2441181643Skmacy{ 2442230587Sken struct ifnet *ifp; 2443181643Skmacy 2444230587Sken ifp = xnb->xnb_ifp; 2445181643Skmacy 2446230587Sken mtx_assert(&xnb->sc_lock, MA_OWNED); 2447181643Skmacy 2448230587Sken if (ifp->if_drv_flags & IFF_DRV_RUNNING) 2449230587Sken return; 2450181643Skmacy 2451230587Sken xnb_stop(xnb); 2452181643Skmacy 2453230587Sken ifp->if_drv_flags |= IFF_DRV_RUNNING; 2454230587Sken ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 2455230587Sken if_link_state_change(ifp, LINK_STATE_UP); 2456181643Skmacy} 2457181643Skmacy 2458230587Sken 2459230587Skenstatic void 2460230587Skenxnb_ifinit(void *xsc) 2461181643Skmacy{ 2462230587Sken struct xnb_softc *xnb = xsc; 2463181643Skmacy 2464230587Sken mtx_lock(&xnb->sc_lock); 2465230587Sken xnb_ifinit_locked(xnb); 2466230587Sken mtx_unlock(&xnb->sc_lock); 2467230587Sken} 2468181643Skmacy 2469230587Sken/** 2470230587Sken * Callback used by the generic networking code to tell us when our carrier 2471230587Sken * state has changed. Since we don't have a physical carrier, we don't care 2472230587Sken */ 2473230587Skenstatic int 2474230587Skenxnb_ifmedia_upd(struct ifnet *ifp) 2475230587Sken{ 2476230587Sken return (0); 2477230587Sken} 2478181643Skmacy 2479230587Sken/** 2480230587Sken * Callback used by the generic networking code to ask us what our carrier 2481230587Sken * state is. Since we don't have a physical carrier, this is very simple 2482230587Sken */ 2483230587Skenstatic void 2484230587Skenxnb_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr) 2485230587Sken{ 2486230587Sken ifmr->ifm_status = IFM_AVALID|IFM_ACTIVE; 2487230587Sken ifmr->ifm_active = IFM_ETHER|IFM_MANUAL; 2488181643Skmacy} 2489181643Skmacy 2490230587Sken 2491230587Sken/*---------------------------- NewBus Registration ---------------------------*/ 2492230587Skenstatic device_method_t xnb_methods[] = { 2493181643Skmacy /* Device interface */ 2494230587Sken DEVMETHOD(device_probe, xnb_probe), 2495230587Sken DEVMETHOD(device_attach, xnb_attach), 2496230587Sken DEVMETHOD(device_detach, xnb_detach), 2497181643Skmacy DEVMETHOD(device_shutdown, bus_generic_shutdown), 2498230587Sken DEVMETHOD(device_suspend, xnb_suspend), 2499230587Sken DEVMETHOD(device_resume, xnb_resume), 2500181643Skmacy 2501230587Sken /* Xenbus interface */ 2502230587Sken DEVMETHOD(xenbus_otherend_changed, xnb_frontend_changed), 2503181643Skmacy 2504230587Sken { 0, 0 } 2505181643Skmacy}; 2506181643Skmacy 2507230587Skenstatic driver_t xnb_driver = { 2508230587Sken "xnb", 2509230587Sken xnb_methods, 2510230587Sken sizeof(struct xnb_softc), 2511230587Sken}; 2512230587Skendevclass_t xnb_devclass; 2513181643Skmacy 2514230587SkenDRIVER_MODULE(xnb, xenbusb_back, xnb_driver, xnb_devclass, 0, 0); 2515181643Skmacy 2516230587Sken 2517230587Sken/*-------------------------- Unit Tests -------------------------------------*/ 2518230587Sken#ifdef XNB_DEBUG 2519230587Sken#include "netback_unit_tests.c" 2520230587Sken#endif 2521