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: releng/10.3/sys/dev/xen/netback/netback.c 259541 2013-12-18 05:20:53Z 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" 45259541Sglebius#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> 61181643Skmacy#include <net/if_arp.h> 62230587Sken#include <net/ethernet.h> 63230587Sken#include <net/if_dl.h> 64230587Sken#include <net/if_media.h> 65181643Skmacy#include <net/if_types.h> 66181643Skmacy 67181643Skmacy#include <netinet/in.h> 68181643Skmacy#include <netinet/ip.h> 69230587Sken#include <netinet/if_ether.h> 70230587Sken#if __FreeBSD_version >= 700000 71181643Skmacy#include <netinet/tcp.h> 72230587Sken#endif 73230587Sken#include <netinet/ip_icmp.h> 74181643Skmacy#include <netinet/udp.h> 75230587Sken#include <machine/in_cksum.h> 76181643Skmacy 77230587Sken#include <vm/vm.h> 78230587Sken#include <vm/pmap.h> 79230916Sken#include <vm/vm_extern.h> 80230916Sken#include <vm/vm_kern.h> 81181643Skmacy 82230587Sken#include <machine/_inttypes.h> 83181643Skmacy 84255040Sgibbs#include <xen/xen-os.h> 85255040Sgibbs#include <xen/hypervisor.h> 86230587Sken#include <xen/xen_intr.h> 87230587Sken#include <xen/interface/io/netif.h> 88230587Sken#include <xen/xenbus/xenbusvar.h> 89181643Skmacy 90255040Sgibbs#include <machine/xen/xenvar.h> 91255040Sgibbs 92230587Sken/*--------------------------- Compile-time Tunables --------------------------*/ 93181643Skmacy 94230587Sken/*---------------------------------- Macros ----------------------------------*/ 95230587Sken/** 96230587Sken * Custom malloc type for all driver allocations. 97230587Sken */ 98230587Skenstatic MALLOC_DEFINE(M_XENNETBACK, "xnb", "Xen Net Back Driver Data"); 99230587Sken 100230587Sken#define XNB_SG 1 /* netback driver supports feature-sg */ 101230587Sken#define XNB_GSO_TCPV4 1 /* netback driver supports feature-gso-tcpv4 */ 102230587Sken#define XNB_RX_COPY 1 /* netback driver supports feature-rx-copy */ 103230587Sken#define XNB_RX_FLIP 0 /* netback driver does not support feature-rx-flip */ 104230587Sken 105230587Sken#undef XNB_DEBUG 106230587Sken#define XNB_DEBUG /* hardcode on during development */ 107230587Sken 108230587Sken#ifdef XNB_DEBUG 109230587Sken#define DPRINTF(fmt, args...) \ 110230587Sken printf("xnb(%s:%d): " fmt, __FUNCTION__, __LINE__, ##args) 111181643Skmacy#else 112230587Sken#define DPRINTF(fmt, args...) do {} while (0) 113181643Skmacy#endif 114181643Skmacy 115230587Sken/* Default length for stack-allocated grant tables */ 116230587Sken#define GNTTAB_LEN (64) 117181643Skmacy 118230587Sken/* Features supported by all backends. TSO and LRO can be negotiated */ 119230587Sken#define XNB_CSUM_FEATURES (CSUM_TCP | CSUM_UDP) 120181643Skmacy 121230587Sken#define NET_TX_RING_SIZE __RING_SIZE((netif_tx_sring_t *)0, PAGE_SIZE) 122230587Sken#define NET_RX_RING_SIZE __RING_SIZE((netif_rx_sring_t *)0, PAGE_SIZE) 123181643Skmacy 124230587Sken/** 125230587Sken * Two argument version of the standard macro. Second argument is a tentative 126230587Sken * value of req_cons 127230587Sken */ 128230587Sken#define RING_HAS_UNCONSUMED_REQUESTS_2(_r, cons) ({ \ 129230587Sken unsigned int req = (_r)->sring->req_prod - cons; \ 130230587Sken unsigned int rsp = RING_SIZE(_r) - \ 131230587Sken (cons - (_r)->rsp_prod_pvt); \ 132230587Sken req < rsp ? req : rsp; \ 133230587Sken}) 134181643Skmacy 135230587Sken#define virt_to_mfn(x) (vtomach(x) >> PAGE_SHIFT) 136230587Sken#define virt_to_offset(x) ((x) & (PAGE_SIZE - 1)) 137181643Skmacy 138230587Sken/** 139230587Sken * Predefined array type of grant table copy descriptors. Used to pass around 140230587Sken * statically allocated memory structures. 141230587Sken */ 142230587Skentypedef struct gnttab_copy gnttab_copy_table[GNTTAB_LEN]; 143181643Skmacy 144230587Sken/*--------------------------- Forward Declarations ---------------------------*/ 145230587Skenstruct xnb_softc; 146230587Skenstruct xnb_pkt; 147181643Skmacy 148230587Skenstatic void xnb_attach_failed(struct xnb_softc *xnb, 149230587Sken int err, const char *fmt, ...) 150230587Sken __printflike(3,4); 151230587Skenstatic int xnb_shutdown(struct xnb_softc *xnb); 152230587Skenstatic int create_netdev(device_t dev); 153230587Skenstatic int xnb_detach(device_t dev); 154230587Skenstatic int xen_net_read_mac(device_t dev, uint8_t mac[]); 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 197259541Sglebius#if defined(INET) || defined(INET6) 198259541Sglebiusstatic void xnb_add_mbuf_cksum(struct mbuf *mbufc); 199259541Sglebius#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 654255040Sgibbs xen_intr_unbind(xnb->xen_intr_handle); 655181643Skmacy 656230587Sken /* 657230587Sken * We may still have another thread currently processing requests. We 658230587Sken * must acquire the rx and tx locks to make sure those threads are done, 659230587Sken * but we can release those locks as soon as we acquire them, because no 660230587Sken * more interrupts will be arriving. 661230587Sken */ 662230587Sken mtx_lock(&xnb->tx_lock); 663230587Sken mtx_unlock(&xnb->tx_lock); 664230587Sken mtx_lock(&xnb->rx_lock); 665230587Sken mtx_unlock(&xnb->rx_lock); 666230587Sken 667230587Sken /* Free malloc'd softc member variables */ 668230587Sken if (xnb->bridge != NULL) 669230587Sken free(xnb->bridge, M_XENSTORE); 670230587Sken 671230587Sken /* All request processing has stopped, so unmap the rings */ 672230587Sken for (i=0; i < XNB_NUM_RING_TYPES; i++) { 673230587Sken gnts[i].host_addr = xnb->ring_configs[i].gnt_addr; 674230587Sken gnts[i].dev_bus_addr = xnb->ring_configs[i].bus_addr; 675230587Sken gnts[i].handle = xnb->ring_configs[i].handle; 676181643Skmacy } 677230587Sken error = HYPERVISOR_grant_table_op(GNTTABOP_unmap_grant_ref, gnts, 678230587Sken XNB_NUM_RING_TYPES); 679230587Sken KASSERT(error == 0, ("Grant table unmap op failed (%d)", error)); 680181643Skmacy 681230587Sken xnb_free_communication_mem(xnb); 682230587Sken /* 683230587Sken * Zero the ring config structs because the pointers, handles, and 684230587Sken * grant refs contained therein are no longer valid. 685230587Sken */ 686230587Sken bzero(&xnb->ring_configs[XNB_RING_TYPE_TX], 687230587Sken sizeof(struct xnb_ring_config)); 688230587Sken bzero(&xnb->ring_configs[XNB_RING_TYPE_RX], 689230587Sken sizeof(struct xnb_ring_config)); 690181643Skmacy 691230587Sken xnb->flags &= ~XNBF_RING_CONNECTED; 692230587Sken return (0); 693181643Skmacy} 694181643Skmacy 695230587Sken/** 696230587Sken * Map a single shared memory ring into domain local address space and 697230587Sken * initialize its control structure 698230587Sken * 699230587Sken * \param xnb Per-instance xnb configuration structure 700230587Sken * \param ring_type Array index of this ring in the xnb's array of rings 701230587Sken * \return An errno 702230587Sken */ 703230587Skenstatic int 704230587Skenxnb_connect_ring(struct xnb_softc *xnb, xnb_ring_type_t ring_type) 705181643Skmacy{ 706230587Sken struct gnttab_map_grant_ref gnt; 707230587Sken struct xnb_ring_config *ring = &xnb->ring_configs[ring_type]; 708230587Sken int error; 709181643Skmacy 710230587Sken /* TX ring type = 0, RX =1 */ 711230587Sken ring->va = xnb->kva + ring_type * PAGE_SIZE; 712230587Sken ring->gnt_addr = xnb->gnt_base_addr + ring_type * PAGE_SIZE; 713181643Skmacy 714230587Sken gnt.host_addr = ring->gnt_addr; 715230587Sken gnt.flags = GNTMAP_host_map; 716230587Sken gnt.ref = ring->ring_ref; 717230587Sken gnt.dom = xnb->otherend_id; 718181643Skmacy 719230587Sken error = HYPERVISOR_grant_table_op(GNTTABOP_map_grant_ref, &gnt, 1); 720230587Sken if (error != 0) 721230587Sken panic("netback: Ring page grant table op failed (%d)", error); 722230587Sken 723230587Sken if (gnt.status != 0) { 724230587Sken ring->va = 0; 725230587Sken error = EACCES; 726230587Sken xenbus_dev_fatal(xnb->dev, error, 727230587Sken "Ring shared page mapping failed. " 728230587Sken "Status %d.", gnt.status); 729230587Sken } else { 730230587Sken ring->handle = gnt.handle; 731230587Sken ring->bus_addr = gnt.dev_bus_addr; 732230587Sken 733230587Sken if (ring_type == XNB_RING_TYPE_TX) { 734230587Sken BACK_RING_INIT(&ring->back_ring.tx_ring, 735230587Sken (netif_tx_sring_t*)ring->va, 736230587Sken ring->ring_pages * PAGE_SIZE); 737230587Sken } else if (ring_type == XNB_RING_TYPE_RX) { 738230587Sken BACK_RING_INIT(&ring->back_ring.rx_ring, 739230587Sken (netif_rx_sring_t*)ring->va, 740230587Sken ring->ring_pages * PAGE_SIZE); 741230587Sken } else { 742230587Sken xenbus_dev_fatal(xnb->dev, error, 743230587Sken "Unknown ring type %d", ring_type); 744230587Sken } 745181643Skmacy } 746230587Sken 747230587Sken return error; 748181643Skmacy} 749181643Skmacy 750230587Sken/** 751230587Sken * Setup the shared memory rings and bind an interrupt to the event channel 752230587Sken * used to notify us of ring changes. 753230587Sken * 754230587Sken * \param xnb Per-instance xnb configuration structure. 755230587Sken */ 756230587Skenstatic int 757230587Skenxnb_connect_comms(struct xnb_softc *xnb) 758181643Skmacy{ 759230587Sken int error; 760230587Sken xnb_ring_type_t i; 761181643Skmacy 762230587Sken if ((xnb->flags & XNBF_RING_CONNECTED) != 0) 763230587Sken return (0); 764181643Skmacy 765181643Skmacy /* 766230587Sken * Kva for our rings are at the tail of the region of kva allocated 767230587Sken * by xnb_alloc_communication_mem(). 768181643Skmacy */ 769230587Sken for (i=0; i < XNB_NUM_RING_TYPES; i++) { 770230587Sken error = xnb_connect_ring(xnb, i); 771230587Sken if (error != 0) 772230587Sken return error; 773181643Skmacy } 774181643Skmacy 775230587Sken xnb->flags |= XNBF_RING_CONNECTED; 776181643Skmacy 777255040Sgibbs error = xen_intr_bind_remote_port(xnb->dev, 778255040Sgibbs xnb->otherend_id, 779255040Sgibbs xnb->evtchn, 780255040Sgibbs /*filter*/NULL, 781255040Sgibbs xnb_intr, /*arg*/xnb, 782255040Sgibbs INTR_TYPE_BIO | INTR_MPSAFE, 783255040Sgibbs &xnb->xen_intr_handle); 784230587Sken if (error != 0) { 785230587Sken (void)xnb_disconnect(xnb); 786230587Sken xenbus_dev_fatal(xnb->dev, error, "binding event channel"); 787230587Sken return (error); 788230587Sken } 789181643Skmacy 790230587Sken DPRINTF("rings connected!\n"); 791181643Skmacy 792230587Sken return (0); 793181643Skmacy} 794181643Skmacy 795230587Sken/** 796230587Sken * Size KVA and pseudo-physical address allocations based on negotiated 797230587Sken * values for the size and number of I/O requests, and the size of our 798230587Sken * communication ring. 799230587Sken * 800230587Sken * \param xnb Per-instance xnb configuration structure. 801230587Sken * 802230587Sken * These address spaces are used to dynamically map pages in the 803230587Sken * front-end's domain into our own. 804230587Sken */ 805230587Skenstatic int 806230587Skenxnb_alloc_communication_mem(struct xnb_softc *xnb) 807181643Skmacy{ 808230587Sken xnb_ring_type_t i; 809181643Skmacy 810230587Sken xnb->kva_size = 0; 811230587Sken for (i=0; i < XNB_NUM_RING_TYPES; i++) { 812230587Sken xnb->kva_size += xnb->ring_configs[i].ring_pages * PAGE_SIZE; 813230587Sken } 814230587Sken#ifndef XENHVM 815254025Sjeff xnb->kva = kva_alloc(xnb->kva_size); 816230587Sken if (xnb->kva == 0) 817230587Sken return (ENOMEM); 818230587Sken xnb->gnt_base_addr = xnb->kva; 819230587Sken#else /* defined XENHVM */ 820230587Sken /* 821230587Sken * Reserve a range of pseudo physical memory that we can map 822230587Sken * into kva. These pages will only be backed by machine 823230587Sken * pages ("real memory") during the lifetime of front-end requests 824230587Sken * via grant table operations. We will map the netif tx and rx rings 825230587Sken * into this space. 826230587Sken */ 827230587Sken xnb->pseudo_phys_res_id = 0; 828230587Sken xnb->pseudo_phys_res = bus_alloc_resource(xnb->dev, SYS_RES_MEMORY, 829230587Sken &xnb->pseudo_phys_res_id, 830230587Sken 0, ~0, xnb->kva_size, 831230587Sken RF_ACTIVE); 832230587Sken if (xnb->pseudo_phys_res == NULL) { 833230587Sken xnb->kva = 0; 834230587Sken return (ENOMEM); 835230587Sken } 836230587Sken xnb->kva = (vm_offset_t)rman_get_virtual(xnb->pseudo_phys_res); 837230587Sken xnb->gnt_base_addr = rman_get_start(xnb->pseudo_phys_res); 838230587Sken#endif /* !defined XENHVM */ 839230587Sken return (0); 840181643Skmacy} 841181643Skmacy 842230587Sken/** 843230587Sken * Collect information from the XenStore related to our device and its frontend 844230587Sken * 845230587Sken * \param xnb Per-instance xnb configuration structure. 846230587Sken */ 847230587Skenstatic int 848230587Skenxnb_collect_xenstore_info(struct xnb_softc *xnb) 849181643Skmacy{ 850230587Sken /** 851230587Sken * \todo Linux collects the following info. We should collect most 852230587Sken * of this, too: 853230587Sken * "feature-rx-notify" 854230587Sken */ 855230587Sken const char *otherend_path; 856230587Sken const char *our_path; 857230587Sken int err; 858230587Sken unsigned int rx_copy, bridge_len; 859230587Sken uint8_t no_csum_offload; 860181643Skmacy 861230587Sken otherend_path = xenbus_get_otherend_path(xnb->dev); 862230587Sken our_path = xenbus_get_node(xnb->dev); 863181643Skmacy 864230587Sken /* Collect the critical communication parameters */ 865230587Sken err = xs_gather(XST_NIL, otherend_path, 866230587Sken "tx-ring-ref", "%l" PRIu32, 867230587Sken &xnb->ring_configs[XNB_RING_TYPE_TX].ring_ref, 868230587Sken "rx-ring-ref", "%l" PRIu32, 869230587Sken &xnb->ring_configs[XNB_RING_TYPE_RX].ring_ref, 870230587Sken "event-channel", "%" PRIu32, &xnb->evtchn, 871230587Sken NULL); 872230587Sken if (err != 0) { 873230587Sken xenbus_dev_fatal(xnb->dev, err, 874230587Sken "Unable to retrieve ring information from " 875230587Sken "frontend %s. Unable to connect.", 876230587Sken otherend_path); 877230587Sken return (err); 878230587Sken } 879181643Skmacy 880230587Sken /* Collect the handle from xenstore */ 881230587Sken err = xs_scanf(XST_NIL, our_path, "handle", NULL, "%li", &xnb->handle); 882230587Sken if (err != 0) { 883230587Sken xenbus_dev_fatal(xnb->dev, err, 884230587Sken "Error reading handle from frontend %s. " 885230587Sken "Unable to connect.", otherend_path); 886230587Sken } 887181643Skmacy 888230587Sken /* 889230587Sken * Collect the bridgename, if any. We do not need bridge_len; we just 890230587Sken * throw it away 891230587Sken */ 892230587Sken err = xs_read(XST_NIL, our_path, "bridge", &bridge_len, 893230587Sken (void**)&xnb->bridge); 894230587Sken if (err != 0) 895230587Sken xnb->bridge = NULL; 896181643Skmacy 897230587Sken /* 898230587Sken * Does the frontend request that we use rx copy? If not, return an 899230587Sken * error because this driver only supports rx copy. 900230587Sken */ 901230587Sken err = xs_scanf(XST_NIL, otherend_path, "request-rx-copy", NULL, 902230587Sken "%" PRIu32, &rx_copy); 903230587Sken if (err == ENOENT) { 904230587Sken err = 0; 905230587Sken rx_copy = 0; 906230587Sken } 907230587Sken if (err < 0) { 908230587Sken xenbus_dev_fatal(xnb->dev, err, "reading %s/request-rx-copy", 909230587Sken otherend_path); 910230587Sken return err; 911230587Sken } 912230587Sken /** 913230587Sken * \todo: figure out the exact meaning of this feature, and when 914230587Sken * the frontend will set it to true. It should be set to true 915230587Sken * at some point 916230587Sken */ 917230587Sken/* if (!rx_copy)*/ 918230587Sken/* return EOPNOTSUPP;*/ 919181643Skmacy 920230587Sken /** \todo Collect the rx notify feature */ 921181643Skmacy 922230587Sken /* Collect the feature-sg. */ 923230587Sken if (xs_scanf(XST_NIL, otherend_path, "feature-sg", NULL, 924230587Sken "%hhu", &xnb->can_sg) < 0) 925230587Sken xnb->can_sg = 0; 926181643Skmacy 927230587Sken /* Collect remaining frontend features */ 928230587Sken if (xs_scanf(XST_NIL, otherend_path, "feature-gso-tcpv4", NULL, 929230587Sken "%hhu", &xnb->gso) < 0) 930230587Sken xnb->gso = 0; 931181643Skmacy 932230587Sken if (xs_scanf(XST_NIL, otherend_path, "feature-gso-tcpv4-prefix", NULL, 933230587Sken "%hhu", &xnb->gso_prefix) < 0) 934230587Sken xnb->gso_prefix = 0; 935181643Skmacy 936230587Sken if (xs_scanf(XST_NIL, otherend_path, "feature-no-csum-offload", NULL, 937230587Sken "%hhu", &no_csum_offload) < 0) 938230587Sken no_csum_offload = 0; 939230587Sken xnb->ip_csum = (no_csum_offload == 0); 940181643Skmacy 941230587Sken return (0); 942230587Sken} 943230587Sken 944230587Sken/** 945230587Sken * Supply information about the physical device to the frontend 946230587Sken * via XenBus. 947230587Sken * 948230587Sken * \param xnb Per-instance xnb configuration structure. 949230587Sken */ 950230587Skenstatic int 951230587Skenxnb_publish_backend_info(struct xnb_softc *xnb) 952230587Sken{ 953230587Sken struct xs_transaction xst; 954230587Sken const char *our_path; 955230587Sken int error; 956230587Sken 957230587Sken our_path = xenbus_get_node(xnb->dev); 958230587Sken 959230587Sken do { 960230587Sken error = xs_transaction_start(&xst); 961230587Sken if (error != 0) { 962230587Sken xenbus_dev_fatal(xnb->dev, error, 963230587Sken "Error publishing backend info " 964230587Sken "(start transaction)"); 965230587Sken break; 966181643Skmacy } 967181643Skmacy 968230587Sken error = xs_printf(xst, our_path, "feature-sg", 969230587Sken "%d", XNB_SG); 970230587Sken if (error != 0) 971230587Sken break; 972181643Skmacy 973230587Sken error = xs_printf(xst, our_path, "feature-gso-tcpv4", 974230587Sken "%d", XNB_GSO_TCPV4); 975230587Sken if (error != 0) 976181643Skmacy break; 977230587Sken 978230587Sken error = xs_printf(xst, our_path, "feature-rx-copy", 979230587Sken "%d", XNB_RX_COPY); 980230587Sken if (error != 0) 981230587Sken break; 982230587Sken 983230587Sken error = xs_printf(xst, our_path, "feature-rx-flip", 984230587Sken "%d", XNB_RX_FLIP); 985230587Sken if (error != 0) 986230587Sken break; 987230587Sken 988230587Sken error = xs_transaction_end(xst, 0); 989230587Sken if (error != 0 && error != EAGAIN) { 990230587Sken xenbus_dev_fatal(xnb->dev, error, "ending transaction"); 991230587Sken break; 992181643Skmacy } 993181643Skmacy 994230587Sken } while (error == EAGAIN); 995181643Skmacy 996230587Sken return (error); 997230587Sken} 998181643Skmacy 999230587Sken/** 1000230587Sken * Connect to our netfront peer now that it has completed publishing 1001230587Sken * its configuration into the XenStore. 1002230587Sken * 1003230587Sken * \param xnb Per-instance xnb configuration structure. 1004230587Sken */ 1005230587Skenstatic void 1006230587Skenxnb_connect(struct xnb_softc *xnb) 1007230587Sken{ 1008230587Sken int error; 1009181643Skmacy 1010230587Sken if (xenbus_get_state(xnb->dev) == XenbusStateConnected) 1011230587Sken return; 1012181643Skmacy 1013230587Sken if (xnb_collect_xenstore_info(xnb) != 0) 1014230587Sken return; 1015181643Skmacy 1016230587Sken xnb->flags &= ~XNBF_SHUTDOWN; 1017230587Sken 1018230587Sken /* Read front end configuration. */ 1019230587Sken 1020230587Sken /* Allocate resources whose size depends on front-end configuration. */ 1021230587Sken error = xnb_alloc_communication_mem(xnb); 1022230587Sken if (error != 0) { 1023230587Sken xenbus_dev_fatal(xnb->dev, error, 1024230587Sken "Unable to allocate communication memory"); 1025230587Sken return; 1026181643Skmacy } 1027181643Skmacy 1028230587Sken /* 1029230587Sken * Connect communication channel. 1030230587Sken */ 1031230587Sken error = xnb_connect_comms(xnb); 1032230587Sken if (error != 0) { 1033230587Sken /* Specific errors are reported by xnb_connect_comms(). */ 1034181643Skmacy return; 1035230587Sken } 1036230587Sken xnb->carrier = 1; 1037181643Skmacy 1038230587Sken /* Ready for I/O. */ 1039230587Sken xenbus_set_state(xnb->dev, XenbusStateConnected); 1040230587Sken} 1041181643Skmacy 1042230587Sken/*-------------------------- Device Teardown Support -------------------------*/ 1043230587Sken/** 1044230587Sken * Perform device shutdown functions. 1045230587Sken * 1046230587Sken * \param xnb Per-instance xnb configuration structure. 1047230587Sken * 1048230587Sken * Mark this instance as shutting down, wait for any active requests 1049230587Sken * to drain, disconnect from the front-end, and notify any waiters (e.g. 1050230587Sken * a thread invoking our detach method) that detach can now proceed. 1051230587Sken */ 1052230587Skenstatic int 1053230587Skenxnb_shutdown(struct xnb_softc *xnb) 1054230587Sken{ 1055230587Sken /* 1056230587Sken * Due to the need to drop our mutex during some 1057230587Sken * xenbus operations, it is possible for two threads 1058230587Sken * to attempt to close out shutdown processing at 1059230587Sken * the same time. Tell the caller that hits this 1060230587Sken * race to try back later. 1061230587Sken */ 1062230587Sken if ((xnb->flags & XNBF_IN_SHUTDOWN) != 0) 1063230587Sken return (EAGAIN); 1064181643Skmacy 1065230587Sken xnb->flags |= XNBF_SHUTDOWN; 1066181643Skmacy 1067230587Sken xnb->flags |= XNBF_IN_SHUTDOWN; 1068181643Skmacy 1069230587Sken mtx_unlock(&xnb->sc_lock); 1070230587Sken /* Free the network interface */ 1071230587Sken xnb->carrier = 0; 1072230587Sken if (xnb->xnb_ifp != NULL) { 1073230587Sken ether_ifdetach(xnb->xnb_ifp); 1074230587Sken if_free(xnb->xnb_ifp); 1075230587Sken xnb->xnb_ifp = NULL; 1076230587Sken } 1077230587Sken mtx_lock(&xnb->sc_lock); 1078181643Skmacy 1079230587Sken xnb_disconnect(xnb); 1080181643Skmacy 1081230587Sken mtx_unlock(&xnb->sc_lock); 1082230587Sken if (xenbus_get_state(xnb->dev) < XenbusStateClosing) 1083230587Sken xenbus_set_state(xnb->dev, XenbusStateClosing); 1084230587Sken mtx_lock(&xnb->sc_lock); 1085181643Skmacy 1086230587Sken xnb->flags &= ~XNBF_IN_SHUTDOWN; 1087181643Skmacy 1088181643Skmacy 1089230587Sken /* Indicate to xnb_detach() that is it safe to proceed. */ 1090230587Sken wakeup(xnb); 1091181643Skmacy 1092230587Sken return (0); 1093181643Skmacy} 1094181643Skmacy 1095230587Sken/** 1096230587Sken * Report an attach time error to the console and Xen, and cleanup 1097230587Sken * this instance by forcing immediate detach processing. 1098230587Sken * 1099230587Sken * \param xnb Per-instance xnb configuration structure. 1100230587Sken * \param err Errno describing the error. 1101230587Sken * \param fmt Printf style format and arguments 1102230587Sken */ 1103181643Skmacystatic void 1104230587Skenxnb_attach_failed(struct xnb_softc *xnb, int err, const char *fmt, ...) 1105181643Skmacy{ 1106230587Sken va_list ap; 1107230587Sken va_list ap_hotplug; 1108230587Sken 1109230587Sken va_start(ap, fmt); 1110230587Sken va_copy(ap_hotplug, ap); 1111230587Sken xs_vprintf(XST_NIL, xenbus_get_node(xnb->dev), 1112230587Sken "hotplug-error", fmt, ap_hotplug); 1113230587Sken va_end(ap_hotplug); 1114230587Sken xs_printf(XST_NIL, xenbus_get_node(xnb->dev), 1115230587Sken "hotplug-status", "error"); 1116230587Sken 1117230587Sken xenbus_dev_vfatal(xnb->dev, err, fmt, ap); 1118230587Sken va_end(ap); 1119230587Sken 1120230587Sken xs_printf(XST_NIL, xenbus_get_node(xnb->dev), 1121230587Sken "online", "0"); 1122230587Sken xnb_detach(xnb->dev); 1123181643Skmacy} 1124181643Skmacy 1125230587Sken/*---------------------------- NewBus Entrypoints ----------------------------*/ 1126230587Sken/** 1127230587Sken * Inspect a XenBus device and claim it if is of the appropriate type. 1128230587Sken * 1129230587Sken * \param dev NewBus device object representing a candidate XenBus device. 1130230587Sken * 1131230587Sken * \return 0 for success, errno codes for failure. 1132230587Sken */ 1133230587Skenstatic int 1134230587Skenxnb_probe(device_t dev) 1135181643Skmacy{ 1136230587Sken if (!strcmp(xenbus_get_type(dev), "vif")) { 1137230587Sken DPRINTF("Claiming device %d, %s\n", device_get_unit(dev), 1138230587Sken devclass_get_name(device_get_devclass(dev))); 1139230587Sken device_set_desc(dev, "Backend Virtual Network Device"); 1140230587Sken device_quiet(dev); 1141230587Sken return (0); 1142181643Skmacy } 1143230587Sken return (ENXIO); 1144181643Skmacy} 1145181643Skmacy 1146230587Sken/** 1147230587Sken * Setup sysctl variables to control various Network Back parameters. 1148230587Sken * 1149230587Sken * \param xnb Xen Net Back softc. 1150230587Sken * 1151230587Sken */ 1152181643Skmacystatic void 1153230587Skenxnb_setup_sysctl(struct xnb_softc *xnb) 1154181643Skmacy{ 1155230587Sken struct sysctl_ctx_list *sysctl_ctx = NULL; 1156230587Sken struct sysctl_oid *sysctl_tree = NULL; 1157230587Sken 1158230587Sken sysctl_ctx = device_get_sysctl_ctx(xnb->dev); 1159230587Sken if (sysctl_ctx == NULL) 1160181643Skmacy return; 1161181643Skmacy 1162230587Sken sysctl_tree = device_get_sysctl_tree(xnb->dev); 1163230587Sken if (sysctl_tree == NULL) 1164230587Sken return; 1165230587Sken 1166230587Sken#ifdef XNB_DEBUG 1167230587Sken SYSCTL_ADD_PROC(sysctl_ctx, 1168230587Sken SYSCTL_CHILDREN(sysctl_tree), 1169230587Sken OID_AUTO, 1170230587Sken "unit_test_results", 1171230587Sken CTLTYPE_STRING | CTLFLAG_RD, 1172230587Sken xnb, 1173230587Sken 0, 1174230587Sken xnb_unit_test_main, 1175230587Sken "A", 1176230587Sken "Results of builtin unit tests"); 1177230587Sken 1178230587Sken SYSCTL_ADD_PROC(sysctl_ctx, 1179230587Sken SYSCTL_CHILDREN(sysctl_tree), 1180230587Sken OID_AUTO, 1181230587Sken "dump_rings", 1182230587Sken CTLTYPE_STRING | CTLFLAG_RD, 1183230587Sken xnb, 1184230587Sken 0, 1185230587Sken xnb_dump_rings, 1186230587Sken "A", 1187230587Sken "Xennet Back Rings"); 1188230587Sken#endif /* XNB_DEBUG */ 1189181643Skmacy} 1190181643Skmacy 1191230587Sken/** 1192230587Sken * Create a network device. 1193230587Sken * @param handle device handle 1194230587Sken */ 1195230587Skenint 1196230587Skencreate_netdev(device_t dev) 1197181643Skmacy{ 1198230587Sken struct ifnet *ifp; 1199230587Sken struct xnb_softc *xnb; 1200230587Sken int err = 0; 1201181643Skmacy 1202230587Sken xnb = device_get_softc(dev); 1203230587Sken mtx_init(&xnb->sc_lock, "xnb_softc", "xen netback softc lock", MTX_DEF); 1204230587Sken mtx_init(&xnb->tx_lock, "xnb_tx", "xen netback tx lock", MTX_DEF); 1205230587Sken mtx_init(&xnb->rx_lock, "xnb_rx", "xen netback rx lock", MTX_DEF); 1206181643Skmacy 1207230587Sken xnb->dev = dev; 1208181643Skmacy 1209230587Sken ifmedia_init(&xnb->sc_media, 0, xnb_ifmedia_upd, xnb_ifmedia_sts); 1210230587Sken ifmedia_add(&xnb->sc_media, IFM_ETHER|IFM_MANUAL, 0, NULL); 1211230587Sken ifmedia_set(&xnb->sc_media, IFM_ETHER|IFM_MANUAL); 1212181643Skmacy 1213230587Sken err = xen_net_read_mac(dev, xnb->mac); 1214230587Sken if (err == 0) { 1215230587Sken /* Set up ifnet structure */ 1216230587Sken ifp = xnb->xnb_ifp = if_alloc(IFT_ETHER); 1217230587Sken ifp->if_softc = xnb; 1218230587Sken if_initname(ifp, "xnb", device_get_unit(dev)); 1219230587Sken ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST; 1220230587Sken ifp->if_ioctl = xnb_ioctl; 1221230587Sken ifp->if_output = ether_output; 1222230587Sken ifp->if_start = xnb_start; 1223230587Sken#ifdef notyet 1224230587Sken ifp->if_watchdog = xnb_watchdog; 1225230587Sken#endif 1226230587Sken ifp->if_init = xnb_ifinit; 1227230587Sken ifp->if_mtu = ETHERMTU; 1228230587Sken ifp->if_snd.ifq_maxlen = NET_RX_RING_SIZE - 1; 1229230587Sken 1230230587Sken ifp->if_hwassist = XNB_CSUM_FEATURES; 1231230587Sken ifp->if_capabilities = IFCAP_HWCSUM; 1232230587Sken ifp->if_capenable = IFCAP_HWCSUM; 1233230587Sken 1234230587Sken ether_ifattach(ifp, xnb->mac); 1235230587Sken xnb->carrier = 0; 1236230587Sken } 1237230587Sken 1238230587Sken return err; 1239181643Skmacy} 1240181643Skmacy 1241230587Sken/** 1242230587Sken * Attach to a XenBus device that has been claimed by our probe routine. 1243230587Sken * 1244230587Sken * \param dev NewBus device object representing this Xen Net Back instance. 1245230587Sken * 1246230587Sken * \return 0 for success, errno codes for failure. 1247230587Sken */ 1248181643Skmacystatic int 1249230587Skenxnb_attach(device_t dev) 1250181643Skmacy{ 1251230587Sken struct xnb_softc *xnb; 1252230587Sken int error; 1253230587Sken xnb_ring_type_t i; 1254181643Skmacy 1255230587Sken error = create_netdev(dev); 1256230587Sken if (error != 0) { 1257230587Sken xenbus_dev_fatal(dev, error, "creating netdev"); 1258230587Sken return (error); 1259230587Sken } 1260181643Skmacy 1261230587Sken DPRINTF("Attaching to %s\n", xenbus_get_node(dev)); 1262181643Skmacy 1263230587Sken /* 1264230587Sken * Basic initialization. 1265230587Sken * After this block it is safe to call xnb_detach() 1266230587Sken * to clean up any allocated data for this instance. 1267230587Sken */ 1268230587Sken xnb = device_get_softc(dev); 1269230587Sken xnb->otherend_id = xenbus_get_otherend_id(dev); 1270230587Sken for (i=0; i < XNB_NUM_RING_TYPES; i++) { 1271230587Sken xnb->ring_configs[i].ring_pages = 1; 1272230587Sken } 1273181643Skmacy 1274230587Sken /* 1275230587Sken * Setup sysctl variables. 1276230587Sken */ 1277230587Sken xnb_setup_sysctl(xnb); 1278181643Skmacy 1279230587Sken /* Update hot-plug status to satisfy xend. */ 1280230587Sken error = xs_printf(XST_NIL, xenbus_get_node(xnb->dev), 1281230587Sken "hotplug-status", "connected"); 1282230587Sken if (error != 0) { 1283230587Sken xnb_attach_failed(xnb, error, "writing %s/hotplug-status", 1284230587Sken xenbus_get_node(xnb->dev)); 1285230587Sken return (error); 1286230587Sken } 1287181643Skmacy 1288230587Sken if ((error = xnb_publish_backend_info(xnb)) != 0) { 1289230587Sken /* 1290230587Sken * If we can't publish our data, we cannot participate 1291230587Sken * in this connection, and waiting for a front-end state 1292230587Sken * change will not help the situation. 1293230587Sken */ 1294230587Sken xnb_attach_failed(xnb, error, 1295230587Sken "Publishing backend status for %s", 1296230587Sken xenbus_get_node(xnb->dev)); 1297230587Sken return error; 1298230587Sken } 1299181643Skmacy 1300230587Sken /* Tell the front end that we are ready to connect. */ 1301230587Sken xenbus_set_state(dev, XenbusStateInitWait); 1302181643Skmacy 1303230587Sken return (0); 1304230587Sken} 1305181643Skmacy 1306230587Sken/** 1307230587Sken * Detach from a net back device instance. 1308230587Sken * 1309230587Sken * \param dev NewBus device object representing this Xen Net Back instance. 1310230587Sken * 1311230587Sken * \return 0 for success, errno codes for failure. 1312230587Sken * 1313230587Sken * \note A net back device may be detached at any time in its life-cycle, 1314230587Sken * including part way through the attach process. For this reason, 1315230587Sken * initialization order and the intialization state checks in this 1316230587Sken * routine must be carefully coupled so that attach time failures 1317230587Sken * are gracefully handled. 1318230587Sken */ 1319230587Skenstatic int 1320230587Skenxnb_detach(device_t dev) 1321230587Sken{ 1322230587Sken struct xnb_softc *xnb; 1323181643Skmacy 1324230587Sken DPRINTF("\n"); 1325181643Skmacy 1326230587Sken xnb = device_get_softc(dev); 1327230587Sken mtx_lock(&xnb->sc_lock); 1328230587Sken while (xnb_shutdown(xnb) == EAGAIN) { 1329230587Sken msleep(xnb, &xnb->sc_lock, /*wakeup prio unchanged*/0, 1330230587Sken "xnb_shutdown", 0); 1331230587Sken } 1332230587Sken mtx_unlock(&xnb->sc_lock); 1333230587Sken DPRINTF("\n"); 1334181643Skmacy 1335230587Sken mtx_destroy(&xnb->tx_lock); 1336230587Sken mtx_destroy(&xnb->rx_lock); 1337230587Sken mtx_destroy(&xnb->sc_lock); 1338230587Sken return (0); 1339230587Sken} 1340181643Skmacy 1341230587Sken/** 1342230587Sken * Prepare this net back device for suspension of this VM. 1343230587Sken * 1344230587Sken * \param dev NewBus device object representing this Xen net Back instance. 1345230587Sken * 1346230587Sken * \return 0 for success, errno codes for failure. 1347230587Sken */ 1348230587Skenstatic int 1349230587Skenxnb_suspend(device_t dev) 1350230587Sken{ 1351230587Sken return (0); 1352230587Sken} 1353181643Skmacy 1354230587Sken/** 1355230587Sken * Perform any processing required to recover from a suspended state. 1356230587Sken * 1357230587Sken * \param dev NewBus device object representing this Xen Net Back instance. 1358230587Sken * 1359230587Sken * \return 0 for success, errno codes for failure. 1360230587Sken */ 1361230587Skenstatic int 1362230587Skenxnb_resume(device_t dev) 1363230587Sken{ 1364230587Sken return (0); 1365230587Sken} 1366181643Skmacy 1367230587Sken/** 1368230587Sken * Handle state changes expressed via the XenStore by our front-end peer. 1369230587Sken * 1370230587Sken * \param dev NewBus device object representing this Xen 1371230587Sken * Net Back instance. 1372230587Sken * \param frontend_state The new state of the front-end. 1373230587Sken * 1374230587Sken * \return 0 for success, errno codes for failure. 1375230587Sken */ 1376230587Skenstatic void 1377230587Skenxnb_frontend_changed(device_t dev, XenbusState frontend_state) 1378230587Sken{ 1379230587Sken struct xnb_softc *xnb; 1380181643Skmacy 1381230587Sken xnb = device_get_softc(dev); 1382181643Skmacy 1383230587Sken DPRINTF("frontend_state=%s, xnb_state=%s\n", 1384230587Sken xenbus_strstate(frontend_state), 1385230587Sken xenbus_strstate(xenbus_get_state(xnb->dev))); 1386181643Skmacy 1387230587Sken switch (frontend_state) { 1388230587Sken case XenbusStateInitialising: 1389230587Sken break; 1390230587Sken case XenbusStateInitialised: 1391230587Sken case XenbusStateConnected: 1392230587Sken xnb_connect(xnb); 1393230587Sken break; 1394230587Sken case XenbusStateClosing: 1395230587Sken case XenbusStateClosed: 1396230587Sken mtx_lock(&xnb->sc_lock); 1397230587Sken xnb_shutdown(xnb); 1398230587Sken mtx_unlock(&xnb->sc_lock); 1399230587Sken if (frontend_state == XenbusStateClosed) 1400230587Sken xenbus_set_state(xnb->dev, XenbusStateClosed); 1401230587Sken break; 1402230587Sken default: 1403230587Sken xenbus_dev_fatal(xnb->dev, EINVAL, "saw state %d at frontend", 1404230587Sken frontend_state); 1405230587Sken break; 1406230587Sken } 1407230587Sken} 1408181643Skmacy 1409181643Skmacy 1410230587Sken/*---------------------------- Request Processing ----------------------------*/ 1411230587Sken/** 1412230587Sken * Interrupt handler bound to the shared ring's event channel. 1413230587Sken * Entry point for the xennet transmit path in netback 1414230587Sken * Transfers packets from the Xen ring to the host's generic networking stack 1415230587Sken * 1416230587Sken * \param arg Callback argument registerd during event channel 1417230587Sken * binding - the xnb_softc for this instance. 1418230587Sken */ 1419230587Skenstatic void 1420230587Skenxnb_intr(void *arg) 1421230587Sken{ 1422230587Sken struct xnb_softc *xnb; 1423230587Sken struct ifnet *ifp; 1424230587Sken netif_tx_back_ring_t *txb; 1425230587Sken RING_IDX req_prod_local; 1426181643Skmacy 1427230587Sken xnb = (struct xnb_softc *)arg; 1428230587Sken ifp = xnb->xnb_ifp; 1429230587Sken txb = &xnb->ring_configs[XNB_RING_TYPE_TX].back_ring.tx_ring; 1430181643Skmacy 1431230587Sken mtx_lock(&xnb->tx_lock); 1432230587Sken do { 1433230587Sken int notify; 1434230587Sken req_prod_local = txb->sring->req_prod; 1435230587Sken xen_rmb(); 1436181643Skmacy 1437230587Sken for (;;) { 1438230587Sken struct mbuf *mbufc; 1439230587Sken int err; 1440181643Skmacy 1441230587Sken err = xnb_recv(txb, xnb->otherend_id, &mbufc, ifp, 1442230587Sken xnb->tx_gnttab); 1443230587Sken if (err || (mbufc == NULL)) 1444230587Sken break; 1445181643Skmacy 1446230587Sken /* Send the packet to the generic network stack */ 1447230587Sken (*xnb->xnb_ifp->if_input)(xnb->xnb_ifp, mbufc); 1448230587Sken } 1449181643Skmacy 1450230587Sken RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(txb, notify); 1451230587Sken if (notify != 0) 1452255040Sgibbs xen_intr_signal(xnb->xen_intr_handle); 1453181643Skmacy 1454230587Sken txb->sring->req_event = txb->req_cons + 1; 1455230587Sken xen_mb(); 1456230587Sken } while (txb->sring->req_prod != req_prod_local) ; 1457230587Sken mtx_unlock(&xnb->tx_lock); 1458181643Skmacy 1459230587Sken xnb_start(ifp); 1460230587Sken} 1461181643Skmacy 1462181643Skmacy 1463230587Sken/** 1464230587Sken * Build a struct xnb_pkt based on netif_tx_request's from a netif tx ring. 1465230587Sken * Will read exactly 0 or 1 packets from the ring; never a partial packet. 1466230587Sken * \param[out] pkt The returned packet. If there is an error building 1467230587Sken * the packet, pkt.list_len will be set to 0. 1468230587Sken * \param[in] tx_ring Pointer to the Ring that is the input to this function 1469230587Sken * \param[in] start The ring index of the first potential request 1470230587Sken * \return The number of requests consumed to build this packet 1471230587Sken */ 1472230587Skenstatic int 1473230587Skenxnb_ring2pkt(struct xnb_pkt *pkt, const netif_tx_back_ring_t *tx_ring, 1474230587Sken RING_IDX start) 1475230587Sken{ 1476230587Sken /* 1477230587Sken * Outline: 1478230587Sken * 1) Initialize pkt 1479230587Sken * 2) Read the first request of the packet 1480230587Sken * 3) Read the extras 1481230587Sken * 4) Set cdr 1482230587Sken * 5) Loop on the remainder of the packet 1483230587Sken * 6) Finalize pkt (stuff like car_size and list_len) 1484230587Sken */ 1485230587Sken int idx = start; 1486230587Sken int discard = 0; /* whether to discard the packet */ 1487230587Sken int more_data = 0; /* there are more request past the last one */ 1488230587Sken uint16_t cdr_size = 0; /* accumulated size of requests 2 through n */ 1489181643Skmacy 1490230587Sken xnb_pkt_initialize(pkt); 1491181643Skmacy 1492230587Sken /* Read the first request */ 1493230587Sken if (RING_HAS_UNCONSUMED_REQUESTS_2(tx_ring, idx)) { 1494230587Sken netif_tx_request_t *tx = RING_GET_REQUEST(tx_ring, idx); 1495230587Sken pkt->size = tx->size; 1496230587Sken pkt->flags = tx->flags & ~NETTXF_more_data; 1497230587Sken more_data = tx->flags & NETTXF_more_data; 1498230587Sken pkt->list_len++; 1499230587Sken pkt->car = idx; 1500230587Sken idx++; 1501230587Sken } 1502181643Skmacy 1503230587Sken /* Read the extra info */ 1504230587Sken if ((pkt->flags & NETTXF_extra_info) && 1505230587Sken RING_HAS_UNCONSUMED_REQUESTS_2(tx_ring, idx)) { 1506230587Sken netif_extra_info_t *ext = 1507230587Sken (netif_extra_info_t*) RING_GET_REQUEST(tx_ring, idx); 1508230587Sken pkt->extra.type = ext->type; 1509230587Sken switch (pkt->extra.type) { 1510230587Sken case XEN_NETIF_EXTRA_TYPE_GSO: 1511230587Sken pkt->extra.u.gso = ext->u.gso; 1512230587Sken break; 1513230587Sken default: 1514230587Sken /* 1515230587Sken * The reference Linux netfront driver will 1516230587Sken * never set any other extra.type. So we don't 1517230587Sken * know what to do with it. Let's print an 1518230587Sken * error, then consume and discard the packet 1519230587Sken */ 1520230587Sken printf("xnb(%s:%d): Unknown extra info type %d." 1521230587Sken " Discarding packet\n", 1522230587Sken __func__, __LINE__, pkt->extra.type); 1523230587Sken xnb_dump_txreq(start, RING_GET_REQUEST(tx_ring, 1524230587Sken start)); 1525230587Sken xnb_dump_txreq(idx, RING_GET_REQUEST(tx_ring, 1526230587Sken idx)); 1527230587Sken discard = 1; 1528230587Sken break; 1529230587Sken } 1530230587Sken 1531230587Sken pkt->extra.flags = ext->flags; 1532230587Sken if (ext->flags & XEN_NETIF_EXTRA_FLAG_MORE) { 1533181643Skmacy /* 1534230587Sken * The reference linux netfront driver never sets this 1535230587Sken * flag (nor does any other known netfront). So we 1536230587Sken * will discard the packet. 1537181643Skmacy */ 1538230587Sken printf("xnb(%s:%d): Request sets " 1539230587Sken "XEN_NETIF_EXTRA_FLAG_MORE, but we can't handle " 1540230587Sken "that\n", __func__, __LINE__); 1541230587Sken xnb_dump_txreq(start, RING_GET_REQUEST(tx_ring, start)); 1542230587Sken xnb_dump_txreq(idx, RING_GET_REQUEST(tx_ring, idx)); 1543230587Sken discard = 1; 1544181643Skmacy } 1545181643Skmacy 1546230587Sken idx++; 1547181643Skmacy } 1548181643Skmacy 1549230587Sken /* Set cdr. If there is not more data, cdr is invalid */ 1550230587Sken pkt->cdr = idx; 1551181643Skmacy 1552230587Sken /* Loop on remainder of packet */ 1553230587Sken while (more_data && RING_HAS_UNCONSUMED_REQUESTS_2(tx_ring, idx)) { 1554230587Sken netif_tx_request_t *tx = RING_GET_REQUEST(tx_ring, idx); 1555230587Sken pkt->list_len++; 1556230587Sken cdr_size += tx->size; 1557230587Sken if (tx->flags & ~NETTXF_more_data) { 1558230587Sken /* There should be no other flags set at this point */ 1559230587Sken printf("xnb(%s:%d): Request sets unknown flags %d " 1560230587Sken "after the 1st request in the packet.\n", 1561230587Sken __func__, __LINE__, tx->flags); 1562230587Sken xnb_dump_txreq(start, RING_GET_REQUEST(tx_ring, start)); 1563230587Sken xnb_dump_txreq(idx, RING_GET_REQUEST(tx_ring, idx)); 1564230587Sken } 1565181643Skmacy 1566230587Sken more_data = tx->flags & NETTXF_more_data; 1567230587Sken idx++; 1568230587Sken } 1569230587Sken 1570230587Sken /* Finalize packet */ 1571230587Sken if (more_data != 0) { 1572230587Sken /* The ring ran out of requests before finishing the packet */ 1573230587Sken xnb_pkt_invalidate(pkt); 1574230587Sken idx = start; /* tell caller that we consumed no requests */ 1575230587Sken } else { 1576230587Sken /* Calculate car_size */ 1577230587Sken pkt->car_size = pkt->size - cdr_size; 1578230587Sken } 1579230587Sken if (discard != 0) { 1580230587Sken xnb_pkt_invalidate(pkt); 1581230587Sken } 1582230587Sken 1583230587Sken return idx - start; 1584181643Skmacy} 1585181643Skmacy 1586230587Sken 1587230587Sken/** 1588230587Sken * Respond to all the requests that constituted pkt. Builds the responses and 1589230587Sken * writes them to the ring, but doesn't push them to the shared ring. 1590230587Sken * \param[in] pkt the packet that needs a response 1591230587Sken * \param[in] error true if there was an error handling the packet, such 1592230587Sken * as in the hypervisor copy op or mbuf allocation 1593230587Sken * \param[out] ring Responses go here 1594230587Sken */ 1595181643Skmacystatic void 1596230587Skenxnb_txpkt2rsp(const struct xnb_pkt *pkt, netif_tx_back_ring_t *ring, 1597230587Sken int error) 1598181643Skmacy{ 1599230587Sken /* 1600230587Sken * Outline: 1601230587Sken * 1) Respond to the first request 1602230587Sken * 2) Respond to the extra info reques 1603230587Sken * Loop through every remaining request in the packet, generating 1604230587Sken * responses that copy those requests' ids and sets the status 1605230587Sken * appropriately. 1606230587Sken */ 1607230587Sken netif_tx_request_t *tx; 1608230587Sken netif_tx_response_t *rsp; 1609230587Sken int i; 1610230587Sken uint16_t status; 1611181643Skmacy 1612230587Sken status = (xnb_pkt_is_valid(pkt) == 0) || error ? 1613230587Sken NETIF_RSP_ERROR : NETIF_RSP_OKAY; 1614230587Sken KASSERT((pkt->list_len == 0) || (ring->rsp_prod_pvt == pkt->car), 1615230587Sken ("Cannot respond to ring requests out of order")); 1616181643Skmacy 1617230587Sken if (pkt->list_len >= 1) { 1618230587Sken uint16_t id; 1619230587Sken tx = RING_GET_REQUEST(ring, ring->rsp_prod_pvt); 1620230587Sken id = tx->id; 1621230587Sken rsp = RING_GET_RESPONSE(ring, ring->rsp_prod_pvt); 1622230587Sken rsp->id = id; 1623230587Sken rsp->status = status; 1624230587Sken ring->rsp_prod_pvt++; 1625181643Skmacy 1626230587Sken if (pkt->flags & NETRXF_extra_info) { 1627230587Sken rsp = RING_GET_RESPONSE(ring, ring->rsp_prod_pvt); 1628230587Sken rsp->status = NETIF_RSP_NULL; 1629230587Sken ring->rsp_prod_pvt++; 1630181643Skmacy } 1631230587Sken } 1632181643Skmacy 1633230587Sken for (i=0; i < pkt->list_len - 1; i++) { 1634230587Sken uint16_t id; 1635230587Sken tx = RING_GET_REQUEST(ring, ring->rsp_prod_pvt); 1636230587Sken id = tx->id; 1637230587Sken rsp = RING_GET_RESPONSE(ring, ring->rsp_prod_pvt); 1638230587Sken rsp->id = id; 1639230587Sken rsp->status = status; 1640230587Sken ring->rsp_prod_pvt++; 1641181643Skmacy } 1642181643Skmacy} 1643181643Skmacy 1644230587Sken/** 1645230587Sken * Create an mbuf chain to represent a packet. Initializes all of the headers 1646230587Sken * in the mbuf chain, but does not copy the data. The returned chain must be 1647230587Sken * free()'d when no longer needed 1648230587Sken * \param[in] pkt A packet to model the mbuf chain after 1649230587Sken * \return A newly allocated mbuf chain, possibly with clusters attached. 1650230587Sken * NULL on failure 1651230587Sken */ 1652230587Skenstatic struct mbuf* 1653230587Skenxnb_pkt2mbufc(const struct xnb_pkt *pkt, struct ifnet *ifp) 1654181643Skmacy{ 1655230587Sken /** 1656230587Sken * \todo consider using a memory pool for mbufs instead of 1657230587Sken * reallocating them for every packet 1658230587Sken */ 1659230587Sken /** \todo handle extra data */ 1660230587Sken struct mbuf *m; 1661181643Skmacy 1662230587Sken m = m_getm(NULL, pkt->size, M_NOWAIT, MT_DATA); 1663181643Skmacy 1664230587Sken if (m != NULL) { 1665230587Sken m->m_pkthdr.rcvif = ifp; 1666230587Sken if (pkt->flags & NETTXF_data_validated) { 1667230587Sken /* 1668230587Sken * We lie to the host OS and always tell it that the 1669230587Sken * checksums are ok, because the packet is unlikely to 1670230587Sken * get corrupted going across domains. 1671230587Sken */ 1672230587Sken m->m_pkthdr.csum_flags = ( 1673230587Sken CSUM_IP_CHECKED | 1674230587Sken CSUM_IP_VALID | 1675230587Sken CSUM_DATA_VALID | 1676230587Sken CSUM_PSEUDO_HDR 1677230587Sken ); 1678230587Sken m->m_pkthdr.csum_data = 0xffff; 1679230587Sken } 1680230587Sken } 1681230587Sken return m; 1682181643Skmacy} 1683181643Skmacy 1684230587Sken/** 1685230587Sken * Build a gnttab_copy table that can be used to copy data from a pkt 1686230587Sken * to an mbufc. Does not actually perform the copy. Always uses gref's on 1687230587Sken * the packet side. 1688230587Sken * \param[in] pkt pkt's associated requests form the src for 1689230587Sken * the copy operation 1690230587Sken * \param[in] mbufc mbufc's storage forms the dest for the copy operation 1691230587Sken * \param[out] gnttab Storage for the returned grant table 1692230587Sken * \param[in] txb Pointer to the backend ring structure 1693230587Sken * \param[in] otherend_id The domain ID of the other end of the copy 1694230587Sken * \return The number of gnttab entries filled 1695230587Sken */ 1696181643Skmacystatic int 1697230587Skenxnb_txpkt2gnttab(const struct xnb_pkt *pkt, const struct mbuf *mbufc, 1698230587Sken gnttab_copy_table gnttab, const netif_tx_back_ring_t *txb, 1699230587Sken domid_t otherend_id) 1700181643Skmacy{ 1701181643Skmacy 1702230587Sken const struct mbuf *mbuf = mbufc;/* current mbuf within the chain */ 1703230587Sken int gnt_idx = 0; /* index into grant table */ 1704230587Sken RING_IDX r_idx = pkt->car; /* index into tx ring buffer */ 1705230587Sken int r_ofs = 0; /* offset of next data within tx request's data area */ 1706230587Sken int m_ofs = 0; /* offset of next data within mbuf's data area */ 1707230587Sken /* size in bytes that still needs to be represented in the table */ 1708230587Sken uint16_t size_remaining = pkt->size; 1709181643Skmacy 1710230587Sken while (size_remaining > 0) { 1711230587Sken const netif_tx_request_t *txq = RING_GET_REQUEST(txb, r_idx); 1712230587Sken const size_t mbuf_space = M_TRAILINGSPACE(mbuf) - m_ofs; 1713230587Sken const size_t req_size = 1714230587Sken r_idx == pkt->car ? pkt->car_size : txq->size; 1715230587Sken const size_t pkt_space = req_size - r_ofs; 1716230587Sken /* 1717230587Sken * space is the largest amount of data that can be copied in the 1718230587Sken * grant table's next entry 1719230587Sken */ 1720230587Sken const size_t space = MIN(pkt_space, mbuf_space); 1721230587Sken 1722230587Sken /* TODO: handle this error condition without panicking */ 1723230587Sken KASSERT(gnt_idx < GNTTAB_LEN, ("Grant table is too short")); 1724230587Sken 1725230587Sken gnttab[gnt_idx].source.u.ref = txq->gref; 1726230587Sken gnttab[gnt_idx].source.domid = otherend_id; 1727230587Sken gnttab[gnt_idx].source.offset = txq->offset + r_ofs; 1728230587Sken gnttab[gnt_idx].dest.u.gmfn = virt_to_mfn( 1729230587Sken mtod(mbuf, vm_offset_t) + m_ofs); 1730230587Sken gnttab[gnt_idx].dest.offset = virt_to_offset( 1731230587Sken mtod(mbuf, vm_offset_t) + m_ofs); 1732230587Sken gnttab[gnt_idx].dest.domid = DOMID_SELF; 1733230587Sken gnttab[gnt_idx].len = space; 1734230587Sken gnttab[gnt_idx].flags = GNTCOPY_source_gref; 1735230587Sken 1736230587Sken gnt_idx++; 1737230587Sken r_ofs += space; 1738230587Sken m_ofs += space; 1739230587Sken size_remaining -= space; 1740230587Sken if (req_size - r_ofs <= 0) { 1741230587Sken /* Must move to the next tx request */ 1742230587Sken r_ofs = 0; 1743230587Sken r_idx = (r_idx == pkt->car) ? pkt->cdr : r_idx + 1; 1744230587Sken } 1745230587Sken if (M_TRAILINGSPACE(mbuf) - m_ofs <= 0) { 1746230587Sken /* Must move to the next mbuf */ 1747230587Sken m_ofs = 0; 1748230587Sken mbuf = mbuf->m_next; 1749230587Sken } 1750181643Skmacy } 1751181643Skmacy 1752230587Sken return gnt_idx; 1753181643Skmacy} 1754181643Skmacy 1755230587Sken/** 1756230587Sken * Check the status of the grant copy operations, and update mbufs various 1757230587Sken * non-data fields to reflect the data present. 1758230587Sken * \param[in,out] mbufc mbuf chain to update. The chain must be valid and of 1759230587Sken * the correct length, and data should already be present 1760230587Sken * \param[in] gnttab A grant table for a just completed copy op 1761230587Sken * \param[in] n_entries The number of valid entries in the grant table 1762230587Sken */ 1763181643Skmacystatic void 1764230587Skenxnb_update_mbufc(struct mbuf *mbufc, const gnttab_copy_table gnttab, 1765230587Sken int n_entries) 1766181643Skmacy{ 1767230587Sken struct mbuf *mbuf = mbufc; 1768230587Sken int i; 1769230587Sken size_t total_size = 0; 1770181643Skmacy 1771230587Sken for (i = 0; i < n_entries; i++) { 1772230587Sken KASSERT(gnttab[i].status == GNTST_okay, 1773230587Sken ("Some gnttab_copy entry had error status %hd\n", 1774230587Sken gnttab[i].status)); 1775181643Skmacy 1776230587Sken mbuf->m_len += gnttab[i].len; 1777230587Sken total_size += gnttab[i].len; 1778230587Sken if (M_TRAILINGSPACE(mbuf) <= 0) { 1779230587Sken mbuf = mbuf->m_next; 1780230587Sken } 1781230587Sken } 1782230587Sken mbufc->m_pkthdr.len = total_size; 1783230587Sken 1784259541Sglebius#if defined(INET) || defined(INET6) 1785230587Sken xnb_add_mbuf_cksum(mbufc); 1786259541Sglebius#endif 1787181643Skmacy} 1788181643Skmacy 1789230587Sken/** 1790230587Sken * Dequeue at most one packet from the shared ring 1791230587Sken * \param[in,out] txb Netif tx ring. A packet will be removed from it, and 1792230587Sken * its private indices will be updated. But the indices 1793230587Sken * will not be pushed to the shared ring. 1794230587Sken * \param[in] ifnet Interface to which the packet will be sent 1795230587Sken * \param[in] otherend Domain ID of the other end of the ring 1796230587Sken * \param[out] mbufc The assembled mbuf chain, ready to send to the generic 1797230587Sken * networking stack 1798230587Sken * \param[in,out] gnttab Pointer to enough memory for a grant table. We make 1799230587Sken * this a function parameter so that we will take less 1800230587Sken * stack space. 1801230587Sken * \return An error code 1802230587Sken */ 1803181643Skmacystatic int 1804230587Skenxnb_recv(netif_tx_back_ring_t *txb, domid_t otherend, struct mbuf **mbufc, 1805230587Sken struct ifnet *ifnet, gnttab_copy_table gnttab) 1806181643Skmacy{ 1807230587Sken struct xnb_pkt pkt; 1808230587Sken /* number of tx requests consumed to build the last packet */ 1809230587Sken int num_consumed; 1810230587Sken int nr_ents; 1811181643Skmacy 1812230587Sken *mbufc = NULL; 1813230587Sken num_consumed = xnb_ring2pkt(&pkt, txb, txb->req_cons); 1814230587Sken if (num_consumed == 0) 1815230587Sken return 0; /* Nothing to receive */ 1816181643Skmacy 1817249588Sgabor /* update statistics independent of errors */ 1818230587Sken ifnet->if_ipackets++; 1819181643Skmacy 1820230587Sken /* 1821230587Sken * if we got here, then 1 or more requests was consumed, but the packet 1822249583Sgabor * is not necessarily valid. 1823230587Sken */ 1824230587Sken if (xnb_pkt_is_valid(&pkt) == 0) { 1825230587Sken /* got a garbage packet, respond and drop it */ 1826230587Sken xnb_txpkt2rsp(&pkt, txb, 1); 1827230587Sken txb->req_cons += num_consumed; 1828230587Sken DPRINTF("xnb_intr: garbage packet, num_consumed=%d\n", 1829230587Sken num_consumed); 1830230587Sken ifnet->if_ierrors++; 1831230587Sken return EINVAL; 1832181643Skmacy } 1833181643Skmacy 1834230587Sken *mbufc = xnb_pkt2mbufc(&pkt, ifnet); 1835230587Sken 1836230587Sken if (*mbufc == NULL) { 1837230587Sken /* 1838230587Sken * Couldn't allocate mbufs. Respond and drop the packet. Do 1839230587Sken * not consume the requests 1840230587Sken */ 1841230587Sken xnb_txpkt2rsp(&pkt, txb, 1); 1842230587Sken DPRINTF("xnb_intr: Couldn't allocate mbufs, num_consumed=%d\n", 1843230587Sken num_consumed); 1844230587Sken ifnet->if_iqdrops++; 1845230587Sken return ENOMEM; 1846181643Skmacy } 1847181643Skmacy 1848230587Sken nr_ents = xnb_txpkt2gnttab(&pkt, *mbufc, gnttab, txb, otherend); 1849181643Skmacy 1850230587Sken if (nr_ents > 0) { 1851230587Sken int __unused hv_ret = HYPERVISOR_grant_table_op(GNTTABOP_copy, 1852230587Sken gnttab, nr_ents); 1853230587Sken KASSERT(hv_ret == 0, 1854230587Sken ("HYPERVISOR_grant_table_op returned %d\n", hv_ret)); 1855230587Sken xnb_update_mbufc(*mbufc, gnttab, nr_ents); 1856230587Sken } 1857181643Skmacy 1858230587Sken xnb_txpkt2rsp(&pkt, txb, 0); 1859230587Sken txb->req_cons += num_consumed; 1860181643Skmacy return 0; 1861181643Skmacy} 1862181643Skmacy 1863230587Sken/** 1864230587Sken * Create an xnb_pkt based on the contents of an mbuf chain. 1865230587Sken * \param[in] mbufc mbuf chain to transform into a packet 1866230587Sken * \param[out] pkt Storage for the newly generated xnb_pkt 1867230587Sken * \param[in] start The ring index of the first available slot in the rx 1868230587Sken * ring 1869230587Sken * \param[in] space The number of free slots in the rx ring 1870230587Sken * \retval 0 Success 1871230587Sken * \retval EINVAL mbufc was corrupt or not convertible into a pkt 1872230587Sken * \retval EAGAIN There was not enough space in the ring to queue the 1873230587Sken * packet 1874230587Sken */ 1875230587Skenstatic int 1876230587Skenxnb_mbufc2pkt(const struct mbuf *mbufc, struct xnb_pkt *pkt, 1877230587Sken RING_IDX start, int space) 1878181643Skmacy{ 1879181643Skmacy 1880230587Sken int retval = 0; 1881181643Skmacy 1882230587Sken if ((mbufc == NULL) || 1883230587Sken ( (mbufc->m_flags & M_PKTHDR) == 0) || 1884230587Sken (mbufc->m_pkthdr.len == 0)) { 1885230587Sken xnb_pkt_invalidate(pkt); 1886230587Sken retval = EINVAL; 1887230587Sken } else { 1888230587Sken int slots_required; 1889181643Skmacy 1890230587Sken xnb_pkt_validate(pkt); 1891230587Sken pkt->flags = 0; 1892230587Sken pkt->size = mbufc->m_pkthdr.len; 1893230587Sken pkt->car = start; 1894230587Sken pkt->car_size = mbufc->m_len; 1895181643Skmacy 1896230587Sken if (mbufc->m_pkthdr.csum_flags & CSUM_TSO) { 1897230587Sken pkt->flags |= NETRXF_extra_info; 1898230587Sken pkt->extra.u.gso.size = mbufc->m_pkthdr.tso_segsz; 1899230587Sken pkt->extra.u.gso.type = XEN_NETIF_GSO_TYPE_TCPV4; 1900230587Sken pkt->extra.u.gso.pad = 0; 1901230587Sken pkt->extra.u.gso.features = 0; 1902230587Sken pkt->extra.type = XEN_NETIF_EXTRA_TYPE_GSO; 1903230587Sken pkt->extra.flags = 0; 1904230587Sken pkt->cdr = start + 2; 1905230587Sken } else { 1906230587Sken pkt->cdr = start + 1; 1907230587Sken } 1908230587Sken if (mbufc->m_pkthdr.csum_flags & (CSUM_TSO | CSUM_DELAY_DATA)) { 1909230587Sken pkt->flags |= 1910230587Sken (NETRXF_csum_blank | NETRXF_data_validated); 1911230587Sken } 1912230587Sken 1913230587Sken /* 1914230587Sken * Each ring response can have up to PAGE_SIZE of data. 1915230587Sken * Assume that we can defragment the mbuf chain efficiently 1916230587Sken * into responses so that each response but the last uses all 1917230587Sken * PAGE_SIZE bytes. 1918230587Sken */ 1919230587Sken pkt->list_len = (pkt->size + PAGE_SIZE - 1) / PAGE_SIZE; 1920230587Sken 1921230587Sken if (pkt->list_len > 1) { 1922230587Sken pkt->flags |= NETRXF_more_data; 1923230587Sken } 1924230587Sken 1925230587Sken slots_required = pkt->list_len + 1926230587Sken (pkt->flags & NETRXF_extra_info ? 1 : 0); 1927230587Sken if (slots_required > space) { 1928230587Sken xnb_pkt_invalidate(pkt); 1929230587Sken retval = EAGAIN; 1930230587Sken } 1931181643Skmacy } 1932230587Sken 1933230587Sken return retval; 1934181643Skmacy} 1935181643Skmacy 1936230587Sken/** 1937230587Sken * Build a gnttab_copy table that can be used to copy data from an mbuf chain 1938230587Sken * to the frontend's shared buffers. Does not actually perform the copy. 1939230587Sken * Always uses gref's on the other end's side. 1940230587Sken * \param[in] pkt pkt's associated responses form the dest for the copy 1941230587Sken * operatoin 1942230587Sken * \param[in] mbufc The source for the copy operation 1943230587Sken * \param[out] gnttab Storage for the returned grant table 1944230587Sken * \param[in] rxb Pointer to the backend ring structure 1945230587Sken * \param[in] otherend_id The domain ID of the other end of the copy 1946230587Sken * \return The number of gnttab entries filled 1947230587Sken */ 1948181643Skmacystatic int 1949230587Skenxnb_rxpkt2gnttab(const struct xnb_pkt *pkt, const struct mbuf *mbufc, 1950230587Sken gnttab_copy_table gnttab, const netif_rx_back_ring_t *rxb, 1951230587Sken domid_t otherend_id) 1952181643Skmacy{ 1953181643Skmacy 1954230587Sken const struct mbuf *mbuf = mbufc;/* current mbuf within the chain */ 1955230587Sken int gnt_idx = 0; /* index into grant table */ 1956230587Sken RING_IDX r_idx = pkt->car; /* index into rx ring buffer */ 1957230587Sken int r_ofs = 0; /* offset of next data within rx request's data area */ 1958230587Sken int m_ofs = 0; /* offset of next data within mbuf's data area */ 1959230587Sken /* size in bytes that still needs to be represented in the table */ 1960230587Sken uint16_t size_remaining; 1961181643Skmacy 1962230587Sken size_remaining = (xnb_pkt_is_valid(pkt) != 0) ? pkt->size : 0; 1963230587Sken 1964230587Sken while (size_remaining > 0) { 1965230587Sken const netif_rx_request_t *rxq = RING_GET_REQUEST(rxb, r_idx); 1966230587Sken const size_t mbuf_space = mbuf->m_len - m_ofs; 1967230587Sken /* Xen shared pages have an implied size of PAGE_SIZE */ 1968230587Sken const size_t req_size = PAGE_SIZE; 1969230587Sken const size_t pkt_space = req_size - r_ofs; 1970230587Sken /* 1971230587Sken * space is the largest amount of data that can be copied in the 1972230587Sken * grant table's next entry 1973230587Sken */ 1974230587Sken const size_t space = MIN(pkt_space, mbuf_space); 1975230587Sken 1976230587Sken /* TODO: handle this error condition without panicing */ 1977230587Sken KASSERT(gnt_idx < GNTTAB_LEN, ("Grant table is too short")); 1978230587Sken 1979230587Sken gnttab[gnt_idx].dest.u.ref = rxq->gref; 1980230587Sken gnttab[gnt_idx].dest.domid = otherend_id; 1981230587Sken gnttab[gnt_idx].dest.offset = r_ofs; 1982230587Sken gnttab[gnt_idx].source.u.gmfn = virt_to_mfn( 1983230587Sken mtod(mbuf, vm_offset_t) + m_ofs); 1984230587Sken gnttab[gnt_idx].source.offset = virt_to_offset( 1985230587Sken mtod(mbuf, vm_offset_t) + m_ofs); 1986230587Sken gnttab[gnt_idx].source.domid = DOMID_SELF; 1987230587Sken gnttab[gnt_idx].len = space; 1988230587Sken gnttab[gnt_idx].flags = GNTCOPY_dest_gref; 1989230587Sken 1990230587Sken gnt_idx++; 1991230587Sken 1992230587Sken r_ofs += space; 1993230587Sken m_ofs += space; 1994230587Sken size_remaining -= space; 1995230587Sken if (req_size - r_ofs <= 0) { 1996230587Sken /* Must move to the next rx request */ 1997230587Sken r_ofs = 0; 1998230587Sken r_idx = (r_idx == pkt->car) ? pkt->cdr : r_idx + 1; 1999230587Sken } 2000230587Sken if (mbuf->m_len - m_ofs <= 0) { 2001230587Sken /* Must move to the next mbuf */ 2002230587Sken m_ofs = 0; 2003230587Sken mbuf = mbuf->m_next; 2004230587Sken } 2005181643Skmacy } 2006181643Skmacy 2007230587Sken return gnt_idx; 2008181643Skmacy} 2009181643Skmacy 2010181643Skmacy/** 2011230587Sken * Generates responses for all the requests that constituted pkt. Builds 2012230587Sken * responses and writes them to the ring, but doesn't push the shared ring 2013230587Sken * indices. 2014230587Sken * \param[in] pkt the packet that needs a response 2015230587Sken * \param[in] gnttab The grant copy table corresponding to this packet. 2016230587Sken * Used to determine how many rsp->netif_rx_response_t's to 2017230587Sken * generate. 2018230587Sken * \param[in] n_entries Number of relevant entries in the grant table 2019230587Sken * \param[out] ring Responses go here 2020230587Sken * \return The number of RX requests that were consumed to generate 2021230587Sken * the responses 2022181643Skmacy */ 2023181643Skmacystatic int 2024230587Skenxnb_rxpkt2rsp(const struct xnb_pkt *pkt, const gnttab_copy_table gnttab, 2025230587Sken int n_entries, netif_rx_back_ring_t *ring) 2026181643Skmacy{ 2027230587Sken /* 2028230587Sken * This code makes the following assumptions: 2029230587Sken * * All entries in gnttab set GNTCOPY_dest_gref 2030230587Sken * * The entries in gnttab are grouped by their grefs: any two 2031230587Sken * entries with the same gref must be adjacent 2032230587Sken */ 2033230587Sken int error = 0; 2034230587Sken int gnt_idx, i; 2035230587Sken int n_responses = 0; 2036230587Sken grant_ref_t last_gref = GRANT_REF_INVALID; 2037230587Sken RING_IDX r_idx; 2038181643Skmacy 2039230587Sken KASSERT(gnttab != NULL, ("Received a null granttable copy")); 2040230587Sken 2041230587Sken /* 2042230587Sken * In the event of an error, we only need to send one response to the 2043230587Sken * netfront. In that case, we musn't write any data to the responses 2044230587Sken * after the one we send. So we must loop all the way through gnttab 2045230587Sken * looking for errors before we generate any responses 2046230587Sken * 2047230587Sken * Since we're looping through the grant table anyway, we'll count the 2048230587Sken * number of different gref's in it, which will tell us how many 2049230587Sken * responses to generate 2050230587Sken */ 2051230587Sken for (gnt_idx = 0; gnt_idx < n_entries; gnt_idx++) { 2052230587Sken int16_t status = gnttab[gnt_idx].status; 2053230587Sken if (status != GNTST_okay) { 2054230587Sken DPRINTF( 2055230587Sken "Got error %d for hypervisor gnttab_copy status\n", 2056230587Sken status); 2057230587Sken error = 1; 2058230587Sken break; 2059230587Sken } 2060230587Sken if (gnttab[gnt_idx].dest.u.ref != last_gref) { 2061230587Sken n_responses++; 2062230587Sken last_gref = gnttab[gnt_idx].dest.u.ref; 2063230587Sken } 2064181643Skmacy } 2065181643Skmacy 2066230587Sken if (error != 0) { 2067230587Sken uint16_t id; 2068230587Sken netif_rx_response_t *rsp; 2069230587Sken 2070230587Sken id = RING_GET_REQUEST(ring, ring->rsp_prod_pvt)->id; 2071230587Sken rsp = RING_GET_RESPONSE(ring, ring->rsp_prod_pvt); 2072230587Sken rsp->id = id; 2073230587Sken rsp->status = NETIF_RSP_ERROR; 2074230587Sken n_responses = 1; 2075230587Sken } else { 2076230587Sken gnt_idx = 0; 2077230587Sken const int has_extra = pkt->flags & NETRXF_extra_info; 2078230587Sken if (has_extra != 0) 2079230587Sken n_responses++; 2080181643Skmacy 2081230587Sken for (i = 0; i < n_responses; i++) { 2082230587Sken netif_rx_request_t rxq; 2083230587Sken netif_rx_response_t *rsp; 2084181643Skmacy 2085230587Sken r_idx = ring->rsp_prod_pvt + i; 2086230587Sken /* 2087230587Sken * We copy the structure of rxq instead of making a 2088230587Sken * pointer because it shares the same memory as rsp. 2089230587Sken */ 2090230587Sken rxq = *(RING_GET_REQUEST(ring, r_idx)); 2091230587Sken rsp = RING_GET_RESPONSE(ring, r_idx); 2092230587Sken if (has_extra && (i == 1)) { 2093230587Sken netif_extra_info_t *ext = 2094230587Sken (netif_extra_info_t*)rsp; 2095230587Sken ext->type = XEN_NETIF_EXTRA_TYPE_GSO; 2096230587Sken ext->flags = 0; 2097230587Sken ext->u.gso.size = pkt->extra.u.gso.size; 2098230587Sken ext->u.gso.type = XEN_NETIF_GSO_TYPE_TCPV4; 2099230587Sken ext->u.gso.pad = 0; 2100230587Sken ext->u.gso.features = 0; 2101230587Sken } else { 2102230587Sken rsp->id = rxq.id; 2103230587Sken rsp->status = GNTST_okay; 2104230587Sken rsp->offset = 0; 2105230587Sken rsp->flags = 0; 2106230587Sken if (i < pkt->list_len - 1) 2107230587Sken rsp->flags |= NETRXF_more_data; 2108230587Sken if ((i == 0) && has_extra) 2109230587Sken rsp->flags |= NETRXF_extra_info; 2110230587Sken if ((i == 0) && 2111230587Sken (pkt->flags & NETRXF_data_validated)) { 2112230587Sken rsp->flags |= NETRXF_data_validated; 2113230587Sken rsp->flags |= NETRXF_csum_blank; 2114230587Sken } 2115230587Sken rsp->status = 0; 2116230587Sken for (; gnttab[gnt_idx].dest.u.ref == rxq.gref; 2117230587Sken gnt_idx++) { 2118230587Sken rsp->status += gnttab[gnt_idx].len; 2119230587Sken } 2120230587Sken } 2121230587Sken } 2122181643Skmacy } 2123181643Skmacy 2124230587Sken ring->req_cons += n_responses; 2125230587Sken ring->rsp_prod_pvt += n_responses; 2126230587Sken return n_responses; 2127181643Skmacy} 2128181643Skmacy 2129259541Sglebius#if defined(INET) || defined(INET6) 2130181643Skmacy/** 2131230587Sken * Add IP, TCP, and/or UDP checksums to every mbuf in a chain. The first mbuf 2132230587Sken * in the chain must start with a struct ether_header. 2133230587Sken * 2134230587Sken * XXX This function will perform incorrectly on UDP packets that are split up 2135230587Sken * into multiple ethernet frames. 2136181643Skmacy */ 2137230587Skenstatic void 2138230587Skenxnb_add_mbuf_cksum(struct mbuf *mbufc) 2139181643Skmacy{ 2140230587Sken struct ether_header *eh; 2141230587Sken struct ip *iph; 2142230587Sken uint16_t ether_type; 2143181643Skmacy 2144230587Sken eh = mtod(mbufc, struct ether_header*); 2145230587Sken ether_type = ntohs(eh->ether_type); 2146230587Sken if (ether_type != ETHERTYPE_IP) { 2147230587Sken /* Nothing to calculate */ 2148230587Sken return; 2149230587Sken } 2150181643Skmacy 2151230587Sken iph = (struct ip*)(eh + 1); 2152230587Sken if (mbufc->m_pkthdr.csum_flags & CSUM_IP_VALID) { 2153230587Sken iph->ip_sum = 0; 2154230587Sken iph->ip_sum = in_cksum_hdr(iph); 2155230587Sken } 2156181643Skmacy 2157230587Sken switch (iph->ip_p) { 2158230587Sken case IPPROTO_TCP: 2159230587Sken if (mbufc->m_pkthdr.csum_flags & CSUM_IP_VALID) { 2160230587Sken size_t tcplen = ntohs(iph->ip_len) - sizeof(struct ip); 2161230587Sken struct tcphdr *th = (struct tcphdr*)(iph + 1); 2162230587Sken th->th_sum = in_pseudo(iph->ip_src.s_addr, 2163230587Sken iph->ip_dst.s_addr, htons(IPPROTO_TCP + tcplen)); 2164230587Sken th->th_sum = in_cksum_skip(mbufc, 2165230587Sken sizeof(struct ether_header) + ntohs(iph->ip_len), 2166230587Sken sizeof(struct ether_header) + (iph->ip_hl << 2)); 2167230587Sken } 2168181643Skmacy break; 2169230587Sken case IPPROTO_UDP: 2170230587Sken if (mbufc->m_pkthdr.csum_flags & CSUM_IP_VALID) { 2171230587Sken size_t udplen = ntohs(iph->ip_len) - sizeof(struct ip); 2172230587Sken struct udphdr *uh = (struct udphdr*)(iph + 1); 2173230587Sken uh->uh_sum = in_pseudo(iph->ip_src.s_addr, 2174230587Sken iph->ip_dst.s_addr, htons(IPPROTO_UDP + udplen)); 2175230587Sken uh->uh_sum = in_cksum_skip(mbufc, 2176230587Sken sizeof(struct ether_header) + ntohs(iph->ip_len), 2177230587Sken sizeof(struct ether_header) + (iph->ip_hl << 2)); 2178230587Sken } 2179181643Skmacy break; 2180230587Sken default: 2181181643Skmacy break; 2182181643Skmacy } 2183181643Skmacy} 2184259541Sglebius#endif /* INET || INET6 */ 2185181643Skmacy 2186181643Skmacystatic void 2187230587Skenxnb_stop(struct xnb_softc *xnb) 2188181643Skmacy{ 2189230587Sken struct ifnet *ifp; 2190181643Skmacy 2191230587Sken mtx_assert(&xnb->sc_lock, MA_OWNED); 2192230587Sken ifp = xnb->xnb_ifp; 2193230587Sken ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE); 2194230587Sken if_link_state_change(ifp, LINK_STATE_DOWN); 2195181643Skmacy} 2196181643Skmacy 2197181643Skmacystatic int 2198230587Skenxnb_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data) 2199181643Skmacy{ 2200230587Sken struct xnb_softc *xnb = ifp->if_softc; 2201259541Sglebius struct ifreq *ifr = (struct ifreq*) data; 2202230587Sken#ifdef INET 2203230587Sken struct ifaddr *ifa = (struct ifaddr*)data; 2204230587Sken#endif 2205230587Sken int error = 0; 2206181643Skmacy 2207230587Sken switch (cmd) { 2208230587Sken case SIOCSIFFLAGS: 2209230587Sken mtx_lock(&xnb->sc_lock); 2210230587Sken if (ifp->if_flags & IFF_UP) { 2211230587Sken xnb_ifinit_locked(xnb); 2212230587Sken } else { 2213230587Sken if (ifp->if_drv_flags & IFF_DRV_RUNNING) { 2214230587Sken xnb_stop(xnb); 2215230587Sken } 2216230587Sken } 2217230587Sken /* 2218230587Sken * Note: netfront sets a variable named xn_if_flags 2219230587Sken * here, but that variable is never read 2220230587Sken */ 2221230587Sken mtx_unlock(&xnb->sc_lock); 2222230587Sken break; 2223230587Sken case SIOCSIFADDR: 2224230587Sken case SIOCGIFADDR: 2225230587Sken#ifdef INET 2226230587Sken mtx_lock(&xnb->sc_lock); 2227230587Sken if (ifa->ifa_addr->sa_family == AF_INET) { 2228230587Sken ifp->if_flags |= IFF_UP; 2229230587Sken if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) { 2230230587Sken ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | 2231230587Sken IFF_DRV_OACTIVE); 2232230587Sken if_link_state_change(ifp, 2233230587Sken LINK_STATE_DOWN); 2234230587Sken ifp->if_drv_flags |= IFF_DRV_RUNNING; 2235230587Sken ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 2236230587Sken if_link_state_change(ifp, 2237230587Sken LINK_STATE_UP); 2238230587Sken } 2239230587Sken arp_ifinit(ifp, ifa); 2240230587Sken mtx_unlock(&xnb->sc_lock); 2241230587Sken } else { 2242230587Sken mtx_unlock(&xnb->sc_lock); 2243230587Sken#endif 2244230587Sken error = ether_ioctl(ifp, cmd, data); 2245230587Sken#ifdef INET 2246230587Sken } 2247230587Sken#endif 2248230587Sken break; 2249230587Sken case SIOCSIFCAP: 2250230587Sken mtx_lock(&xnb->sc_lock); 2251230587Sken if (ifr->ifr_reqcap & IFCAP_TXCSUM) { 2252230587Sken ifp->if_capenable |= IFCAP_TXCSUM; 2253230587Sken ifp->if_hwassist |= XNB_CSUM_FEATURES; 2254230587Sken } else { 2255230587Sken ifp->if_capenable &= ~(IFCAP_TXCSUM); 2256230587Sken ifp->if_hwassist &= ~(XNB_CSUM_FEATURES); 2257230587Sken } 2258230587Sken if ((ifr->ifr_reqcap & IFCAP_RXCSUM)) { 2259230587Sken ifp->if_capenable |= IFCAP_RXCSUM; 2260230587Sken } else { 2261230587Sken ifp->if_capenable &= ~(IFCAP_RXCSUM); 2262230587Sken } 2263230587Sken /* 2264230587Sken * TODO enable TSO4 and LRO once we no longer need 2265230587Sken * to calculate checksums in software 2266230587Sken */ 2267230587Sken#if 0 2268230587Sken if (ifr->if_reqcap |= IFCAP_TSO4) { 2269230587Sken if (IFCAP_TXCSUM & ifp->if_capenable) { 2270230587Sken printf("xnb: Xen netif requires that " 2271230587Sken "TXCSUM be enabled in order " 2272230587Sken "to use TSO4\n"); 2273230587Sken error = EINVAL; 2274230587Sken } else { 2275230587Sken ifp->if_capenable |= IFCAP_TSO4; 2276230587Sken ifp->if_hwassist |= CSUM_TSO; 2277230587Sken } 2278230587Sken } else { 2279230587Sken ifp->if_capenable &= ~(IFCAP_TSO4); 2280230587Sken ifp->if_hwassist &= ~(CSUM_TSO); 2281230587Sken } 2282230587Sken if (ifr->ifreqcap |= IFCAP_LRO) { 2283230587Sken ifp->if_capenable |= IFCAP_LRO; 2284230587Sken } else { 2285230587Sken ifp->if_capenable &= ~(IFCAP_LRO); 2286230587Sken } 2287230587Sken#endif 2288230587Sken mtx_unlock(&xnb->sc_lock); 2289230587Sken break; 2290230587Sken case SIOCSIFMTU: 2291230587Sken ifp->if_mtu = ifr->ifr_mtu; 2292230587Sken ifp->if_drv_flags &= ~IFF_DRV_RUNNING; 2293230587Sken xnb_ifinit(xnb); 2294230587Sken break; 2295230587Sken case SIOCADDMULTI: 2296230587Sken case SIOCDELMULTI: 2297230587Sken case SIOCSIFMEDIA: 2298230587Sken case SIOCGIFMEDIA: 2299230587Sken error = ifmedia_ioctl(ifp, ifr, &xnb->sc_media, cmd); 2300230587Sken break; 2301230587Sken default: 2302230587Sken error = ether_ioctl(ifp, cmd, data); 2303230587Sken break; 2304181643Skmacy } 2305230587Sken return (error); 2306230587Sken} 2307181643Skmacy 2308230587Skenstatic void 2309230587Skenxnb_start_locked(struct ifnet *ifp) 2310230587Sken{ 2311230587Sken netif_rx_back_ring_t *rxb; 2312230587Sken struct xnb_softc *xnb; 2313230587Sken struct mbuf *mbufc; 2314230587Sken RING_IDX req_prod_local; 2315181643Skmacy 2316230587Sken xnb = ifp->if_softc; 2317230587Sken rxb = &xnb->ring_configs[XNB_RING_TYPE_RX].back_ring.rx_ring; 2318181643Skmacy 2319230587Sken if (!xnb->carrier) 2320230587Sken return; 2321181643Skmacy 2322230587Sken do { 2323230587Sken int out_of_space = 0; 2324230587Sken int notify; 2325230587Sken req_prod_local = rxb->sring->req_prod; 2326230587Sken xen_rmb(); 2327230587Sken for (;;) { 2328230587Sken int error; 2329181643Skmacy 2330230587Sken IF_DEQUEUE(&ifp->if_snd, mbufc); 2331230587Sken if (mbufc == NULL) 2332230587Sken break; 2333230587Sken error = xnb_send(rxb, xnb->otherend_id, mbufc, 2334230587Sken xnb->rx_gnttab); 2335230587Sken switch (error) { 2336230587Sken case EAGAIN: 2337230587Sken /* 2338230587Sken * Insufficient space in the ring. 2339230587Sken * Requeue pkt and send when space is 2340230587Sken * available. 2341230587Sken */ 2342230587Sken IF_PREPEND(&ifp->if_snd, mbufc); 2343230587Sken /* 2344230587Sken * Perhaps the frontend missed an IRQ 2345230587Sken * and went to sleep. Notify it to wake 2346230587Sken * it up. 2347230587Sken */ 2348230587Sken out_of_space = 1; 2349230587Sken break; 2350181643Skmacy 2351230587Sken case EINVAL: 2352230587Sken /* OS gave a corrupt packet. Drop it.*/ 2353230587Sken ifp->if_oerrors++; 2354230587Sken /* FALLTHROUGH */ 2355230587Sken default: 2356230587Sken /* Send succeeded, or packet had error. 2357230587Sken * Free the packet */ 2358230587Sken ifp->if_opackets++; 2359230587Sken if (mbufc) 2360230587Sken m_freem(mbufc); 2361230587Sken break; 2362230587Sken } 2363230587Sken if (out_of_space != 0) 2364230587Sken break; 2365230587Sken } 2366181643Skmacy 2367230587Sken RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(rxb, notify); 2368230587Sken if ((notify != 0) || (out_of_space != 0)) 2369255040Sgibbs xen_intr_signal(xnb->xen_intr_handle); 2370230587Sken rxb->sring->req_event = req_prod_local + 1; 2371230587Sken xen_mb(); 2372230587Sken } while (rxb->sring->req_prod != req_prod_local) ; 2373181643Skmacy} 2374181643Skmacy 2375230587Sken/** 2376230587Sken * Sends one packet to the ring. Blocks until the packet is on the ring 2377230587Sken * \param[in] mbufc Contains one packet to send. Caller must free 2378230587Sken * \param[in,out] rxb The packet will be pushed onto this ring, but the 2379230587Sken * otherend will not be notified. 2380230587Sken * \param[in] otherend The domain ID of the other end of the connection 2381230587Sken * \retval EAGAIN The ring did not have enough space for the packet. 2382230587Sken * The ring has not been modified 2383230587Sken * \param[in,out] gnttab Pointer to enough memory for a grant table. We make 2384230587Sken * this a function parameter so that we will take less 2385230587Sken * stack space. 2386230587Sken * \retval EINVAL mbufc was corrupt or not convertible into a pkt 2387230587Sken */ 2388181643Skmacystatic int 2389230587Skenxnb_send(netif_rx_back_ring_t *ring, domid_t otherend, const struct mbuf *mbufc, 2390230587Sken gnttab_copy_table gnttab) 2391181643Skmacy{ 2392230587Sken struct xnb_pkt pkt; 2393230587Sken int error, n_entries, n_reqs; 2394230587Sken RING_IDX space; 2395181643Skmacy 2396230587Sken space = ring->sring->req_prod - ring->req_cons; 2397230587Sken error = xnb_mbufc2pkt(mbufc, &pkt, ring->rsp_prod_pvt, space); 2398230587Sken if (error != 0) 2399230587Sken return error; 2400230587Sken n_entries = xnb_rxpkt2gnttab(&pkt, mbufc, gnttab, ring, otherend); 2401230587Sken if (n_entries != 0) { 2402230587Sken int __unused hv_ret = HYPERVISOR_grant_table_op(GNTTABOP_copy, 2403230587Sken gnttab, n_entries); 2404230587Sken KASSERT(hv_ret == 0, ("HYPERVISOR_grant_table_op returned %d\n", 2405230587Sken hv_ret)); 2406181643Skmacy } 2407181643Skmacy 2408230587Sken n_reqs = xnb_rxpkt2rsp(&pkt, gnttab, n_entries, ring); 2409181643Skmacy 2410230587Sken return 0; 2411181643Skmacy} 2412181643Skmacy 2413230587Skenstatic void 2414230587Skenxnb_start(struct ifnet *ifp) 2415181643Skmacy{ 2416230587Sken struct xnb_softc *xnb; 2417230587Sken 2418230587Sken xnb = ifp->if_softc; 2419230587Sken mtx_lock(&xnb->rx_lock); 2420230587Sken xnb_start_locked(ifp); 2421230587Sken mtx_unlock(&xnb->rx_lock); 2422181643Skmacy} 2423181643Skmacy 2424230587Sken/* equivalent of network_open() in Linux */ 2425230587Skenstatic void 2426230587Skenxnb_ifinit_locked(struct xnb_softc *xnb) 2427181643Skmacy{ 2428230587Sken struct ifnet *ifp; 2429181643Skmacy 2430230587Sken ifp = xnb->xnb_ifp; 2431181643Skmacy 2432230587Sken mtx_assert(&xnb->sc_lock, MA_OWNED); 2433181643Skmacy 2434230587Sken if (ifp->if_drv_flags & IFF_DRV_RUNNING) 2435230587Sken return; 2436181643Skmacy 2437230587Sken xnb_stop(xnb); 2438181643Skmacy 2439230587Sken ifp->if_drv_flags |= IFF_DRV_RUNNING; 2440230587Sken ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 2441230587Sken if_link_state_change(ifp, LINK_STATE_UP); 2442181643Skmacy} 2443181643Skmacy 2444230587Sken 2445230587Skenstatic void 2446230587Skenxnb_ifinit(void *xsc) 2447181643Skmacy{ 2448230587Sken struct xnb_softc *xnb = xsc; 2449181643Skmacy 2450230587Sken mtx_lock(&xnb->sc_lock); 2451230587Sken xnb_ifinit_locked(xnb); 2452230587Sken mtx_unlock(&xnb->sc_lock); 2453230587Sken} 2454181643Skmacy 2455181643Skmacy 2456230587Sken/** 2457230587Sken * Read the 'mac' node at the given device's node in the store, and parse that 2458230587Sken * as colon-separated octets, placing result the given mac array. mac must be 2459230587Sken * a preallocated array of length ETHER_ADDR_LEN ETH_ALEN (as declared in 2460230587Sken * net/ethernet.h). 2461230587Sken * Return 0 on success, or errno on error. 2462230587Sken */ 2463230587Skenstatic int 2464230587Skenxen_net_read_mac(device_t dev, uint8_t mac[]) 2465230587Sken{ 2466230587Sken char *s, *e, *macstr; 2467230587Sken const char *path; 2468230587Sken int error = 0; 2469230587Sken int i; 2470181643Skmacy 2471230587Sken path = xenbus_get_node(dev); 2472230587Sken error = xs_read(XST_NIL, path, "mac", NULL, (void **) &macstr); 2473230587Sken if (error != 0) { 2474230587Sken xenbus_dev_fatal(dev, error, "parsing %s/mac", path); 2475230587Sken } else { 2476230587Sken s = macstr; 2477230587Sken for (i = 0; i < ETHER_ADDR_LEN; i++) { 2478230587Sken mac[i] = strtoul(s, &e, 16); 2479230587Sken if (s == e || (e[0] != ':' && e[0] != 0)) { 2480230587Sken error = ENOENT; 2481230587Sken break; 2482230587Sken } 2483230587Sken s = &e[1]; 2484230587Sken } 2485230587Sken free(macstr, M_XENBUS); 2486230587Sken } 2487230587Sken return error; 2488230587Sken} 2489181643Skmacy 2490181643Skmacy 2491230587Sken/** 2492230587Sken * Callback used by the generic networking code to tell us when our carrier 2493230587Sken * state has changed. Since we don't have a physical carrier, we don't care 2494230587Sken */ 2495230587Skenstatic int 2496230587Skenxnb_ifmedia_upd(struct ifnet *ifp) 2497230587Sken{ 2498230587Sken return (0); 2499230587Sken} 2500181643Skmacy 2501230587Sken/** 2502230587Sken * Callback used by the generic networking code to ask us what our carrier 2503230587Sken * state is. Since we don't have a physical carrier, this is very simple 2504230587Sken */ 2505230587Skenstatic void 2506230587Skenxnb_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr) 2507230587Sken{ 2508230587Sken ifmr->ifm_status = IFM_AVALID|IFM_ACTIVE; 2509230587Sken ifmr->ifm_active = IFM_ETHER|IFM_MANUAL; 2510181643Skmacy} 2511181643Skmacy 2512230587Sken 2513230587Sken/*---------------------------- NewBus Registration ---------------------------*/ 2514230587Skenstatic device_method_t xnb_methods[] = { 2515181643Skmacy /* Device interface */ 2516230587Sken DEVMETHOD(device_probe, xnb_probe), 2517230587Sken DEVMETHOD(device_attach, xnb_attach), 2518230587Sken DEVMETHOD(device_detach, xnb_detach), 2519181643Skmacy DEVMETHOD(device_shutdown, bus_generic_shutdown), 2520230587Sken DEVMETHOD(device_suspend, xnb_suspend), 2521230587Sken DEVMETHOD(device_resume, xnb_resume), 2522181643Skmacy 2523230587Sken /* Xenbus interface */ 2524230587Sken DEVMETHOD(xenbus_otherend_changed, xnb_frontend_changed), 2525181643Skmacy 2526230587Sken { 0, 0 } 2527181643Skmacy}; 2528181643Skmacy 2529230587Skenstatic driver_t xnb_driver = { 2530230587Sken "xnb", 2531230587Sken xnb_methods, 2532230587Sken sizeof(struct xnb_softc), 2533230587Sken}; 2534230587Skendevclass_t xnb_devclass; 2535181643Skmacy 2536230587SkenDRIVER_MODULE(xnb, xenbusb_back, xnb_driver, xnb_devclass, 0, 0); 2537181643Skmacy 2538230587Sken 2539230587Sken/*-------------------------- Unit Tests -------------------------------------*/ 2540230587Sken#ifdef XNB_DEBUG 2541230587Sken#include "netback_unit_tests.c" 2542230587Sken#endif 2543