netback.c revision 255040
1230587Sken/*- 2230587Sken * Copyright (c) 2009-2011 Spectra Logic Corporation 3181643Skmacy * All rights reserved. 4181643Skmacy * 5230587Sken * Redistribution and use in source and binary forms, with or without 6230587Sken * modification, are permitted provided that the following conditions 7181643Skmacy * are met: 8230587Sken * 1. Redistributions of source code must retain the above copyright 9230587Sken * notice, this list of conditions, and the following disclaimer, 10230587Sken * without modification. 11230587Sken * 2. Redistributions in binary form must reproduce at minimum a disclaimer 12230587Sken * substantially similar to the "NO WARRANTY" disclaimer below 13230587Sken * ("Disclaimer") and any redistribution must be conditioned upon 14230587Sken * including a substantially similar Disclaimer requirement for further 15230587Sken * binary redistribution. 16181643Skmacy * 17230587Sken * NO WARRANTY 18230587Sken * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19230587Sken * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20230587Sken * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR 21230587Sken * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22230587Sken * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23230587Sken * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24230587Sken * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25230587Sken * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 26230587Sken * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 27230587Sken * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 28230587Sken * POSSIBILITY OF SUCH DAMAGES. 29181643Skmacy * 30230587Sken * Authors: Justin T. Gibbs (Spectra Logic Corporation) 31230587Sken * Alan Somers (Spectra Logic Corporation) 32230587Sken * John Suykerbuyk (Spectra Logic Corporation) 33181643Skmacy */ 34181643Skmacy 35181643Skmacy#include <sys/cdefs.h> 36181643Skmacy__FBSDID("$FreeBSD: head/sys/dev/xen/netback/netback.c 255040 2013-08-29 19:52:18Z gibbs $"); 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" 45230587Sken#include "opt_global.h" 46230587Sken 47188066Srrs#include "opt_sctp.h" 48181643Skmacy 49181643Skmacy#include <sys/param.h> 50181643Skmacy#include <sys/kernel.h> 51181643Skmacy 52230587Sken#include <sys/bus.h> 53181643Skmacy#include <sys/module.h> 54230587Sken#include <sys/rman.h> 55230587Sken#include <sys/socket.h> 56230587Sken#include <sys/sockio.h> 57181643Skmacy#include <sys/sysctl.h> 58181643Skmacy 59181643Skmacy#include <net/if.h> 60181643Skmacy#include <net/if_arp.h> 61230587Sken#include <net/ethernet.h> 62230587Sken#include <net/if_dl.h> 63230587Sken#include <net/if_media.h> 64181643Skmacy#include <net/if_types.h> 65181643Skmacy 66181643Skmacy#include <netinet/in.h> 67181643Skmacy#include <netinet/ip.h> 68230587Sken#include <netinet/if_ether.h> 69230587Sken#if __FreeBSD_version >= 700000 70181643Skmacy#include <netinet/tcp.h> 71230587Sken#endif 72230587Sken#include <netinet/ip_icmp.h> 73181643Skmacy#include <netinet/udp.h> 74230587Sken#include <machine/in_cksum.h> 75181643Skmacy 76230587Sken#include <vm/vm.h> 77230587Sken#include <vm/pmap.h> 78230916Sken#include <vm/vm_extern.h> 79230916Sken#include <vm/vm_kern.h> 80181643Skmacy 81230587Sken#include <machine/_inttypes.h> 82181643Skmacy 83255040Sgibbs#include <xen/xen-os.h> 84255040Sgibbs#include <xen/hypervisor.h> 85230587Sken#include <xen/xen_intr.h> 86230587Sken#include <xen/interface/io/netif.h> 87230587Sken#include <xen/xenbus/xenbusvar.h> 88181643Skmacy 89255040Sgibbs#include <machine/xen/xenvar.h> 90255040Sgibbs 91230587Sken/*--------------------------- Compile-time Tunables --------------------------*/ 92181643Skmacy 93230587Sken/*---------------------------------- Macros ----------------------------------*/ 94230587Sken/** 95230587Sken * Custom malloc type for all driver allocations. 96230587Sken */ 97230587Skenstatic MALLOC_DEFINE(M_XENNETBACK, "xnb", "Xen Net Back Driver Data"); 98230587Sken 99230587Sken#define XNB_SG 1 /* netback driver supports feature-sg */ 100230587Sken#define XNB_GSO_TCPV4 1 /* netback driver supports feature-gso-tcpv4 */ 101230587Sken#define XNB_RX_COPY 1 /* netback driver supports feature-rx-copy */ 102230587Sken#define XNB_RX_FLIP 0 /* netback driver does not support feature-rx-flip */ 103230587Sken 104230587Sken#undef XNB_DEBUG 105230587Sken#define XNB_DEBUG /* hardcode on during development */ 106230587Sken 107230587Sken#ifdef XNB_DEBUG 108230587Sken#define DPRINTF(fmt, args...) \ 109230587Sken printf("xnb(%s:%d): " fmt, __FUNCTION__, __LINE__, ##args) 110181643Skmacy#else 111230587Sken#define DPRINTF(fmt, args...) do {} while (0) 112181643Skmacy#endif 113181643Skmacy 114230587Sken/* Default length for stack-allocated grant tables */ 115230587Sken#define GNTTAB_LEN (64) 116181643Skmacy 117230587Sken/* Features supported by all backends. TSO and LRO can be negotiated */ 118230587Sken#define XNB_CSUM_FEATURES (CSUM_TCP | CSUM_UDP) 119181643Skmacy 120230587Sken#define NET_TX_RING_SIZE __RING_SIZE((netif_tx_sring_t *)0, PAGE_SIZE) 121230587Sken#define NET_RX_RING_SIZE __RING_SIZE((netif_rx_sring_t *)0, PAGE_SIZE) 122181643Skmacy 123230587Sken/** 124230587Sken * Two argument version of the standard macro. Second argument is a tentative 125230587Sken * value of req_cons 126230587Sken */ 127230587Sken#define RING_HAS_UNCONSUMED_REQUESTS_2(_r, cons) ({ \ 128230587Sken unsigned int req = (_r)->sring->req_prod - cons; \ 129230587Sken unsigned int rsp = RING_SIZE(_r) - \ 130230587Sken (cons - (_r)->rsp_prod_pvt); \ 131230587Sken req < rsp ? req : rsp; \ 132230587Sken}) 133181643Skmacy 134230587Sken#define virt_to_mfn(x) (vtomach(x) >> PAGE_SHIFT) 135230587Sken#define virt_to_offset(x) ((x) & (PAGE_SIZE - 1)) 136181643Skmacy 137230587Sken/** 138230587Sken * Predefined array type of grant table copy descriptors. Used to pass around 139230587Sken * statically allocated memory structures. 140230587Sken */ 141230587Skentypedef struct gnttab_copy gnttab_copy_table[GNTTAB_LEN]; 142181643Skmacy 143230587Sken/*--------------------------- Forward Declarations ---------------------------*/ 144230587Skenstruct xnb_softc; 145230587Skenstruct xnb_pkt; 146181643Skmacy 147230587Skenstatic void xnb_attach_failed(struct xnb_softc *xnb, 148230587Sken int err, const char *fmt, ...) 149230587Sken __printflike(3,4); 150230587Skenstatic int xnb_shutdown(struct xnb_softc *xnb); 151230587Skenstatic int create_netdev(device_t dev); 152230587Skenstatic int xnb_detach(device_t dev); 153230587Skenstatic int xen_net_read_mac(device_t dev, uint8_t mac[]); 154230587Skenstatic int xnb_ifmedia_upd(struct ifnet *ifp); 155230587Skenstatic void xnb_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr); 156230587Skenstatic void xnb_intr(void *arg); 157230587Skenstatic int xnb_send(netif_rx_back_ring_t *rxb, domid_t otherend, 158230587Sken const struct mbuf *mbufc, gnttab_copy_table gnttab); 159230587Skenstatic int xnb_recv(netif_tx_back_ring_t *txb, domid_t otherend, 160230587Sken struct mbuf **mbufc, struct ifnet *ifnet, 161230587Sken gnttab_copy_table gnttab); 162230587Skenstatic int xnb_ring2pkt(struct xnb_pkt *pkt, 163230587Sken const netif_tx_back_ring_t *tx_ring, 164230587Sken RING_IDX start); 165230587Skenstatic void xnb_txpkt2rsp(const struct xnb_pkt *pkt, 166230587Sken netif_tx_back_ring_t *ring, int error); 167230587Skenstatic struct mbuf *xnb_pkt2mbufc(const struct xnb_pkt *pkt, struct ifnet *ifp); 168230587Skenstatic int xnb_txpkt2gnttab(const struct xnb_pkt *pkt, 169230587Sken const struct mbuf *mbufc, 170230587Sken gnttab_copy_table gnttab, 171230587Sken const netif_tx_back_ring_t *txb, 172230587Sken domid_t otherend_id); 173230587Skenstatic void xnb_update_mbufc(struct mbuf *mbufc, 174230587Sken const gnttab_copy_table gnttab, int n_entries); 175230587Skenstatic int xnb_mbufc2pkt(const struct mbuf *mbufc, 176230587Sken struct xnb_pkt *pkt, 177230587Sken RING_IDX start, int space); 178230587Skenstatic int xnb_rxpkt2gnttab(const struct xnb_pkt *pkt, 179230587Sken const struct mbuf *mbufc, 180230587Sken gnttab_copy_table gnttab, 181230587Sken const netif_rx_back_ring_t *rxb, 182230587Sken domid_t otherend_id); 183230587Skenstatic int xnb_rxpkt2rsp(const struct xnb_pkt *pkt, 184230587Sken const gnttab_copy_table gnttab, int n_entries, 185230587Sken netif_rx_back_ring_t *ring); 186230587Skenstatic void xnb_add_mbuf_cksum(struct mbuf *mbufc); 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 197230587Sken/*------------------------------ Data Structures -----------------------------*/ 198181643Skmacy 199181643Skmacy 200230587Sken/** 201230587Sken * Representation of a xennet packet. Simplified version of a packet as 202230587Sken * stored in the Xen tx ring. Applicable to both RX and TX packets 203230587Sken */ 204230587Skenstruct xnb_pkt{ 205230587Sken /** 206230587Sken * Array index of the first data-bearing (eg, not extra info) entry 207230587Sken * for this packet 208230587Sken */ 209230587Sken RING_IDX car; 210181643Skmacy 211230587Sken /** 212230587Sken * Array index of the second data-bearing entry for this packet. 213230587Sken * Invalid if the packet has only one data-bearing entry. If the 214230587Sken * packet has more than two data-bearing entries, then the second 215230587Sken * through the last will be sequential modulo the ring size 216230587Sken */ 217230587Sken RING_IDX cdr; 218181643Skmacy 219230587Sken /** 220230587Sken * Optional extra info. Only valid if flags contains 221230587Sken * NETTXF_extra_info. Note that extra.type will always be 222230587Sken * XEN_NETIF_EXTRA_TYPE_GSO. Currently, no known netfront or netback 223230587Sken * driver will ever set XEN_NETIF_EXTRA_TYPE_MCAST_* 224230587Sken */ 225230587Sken netif_extra_info_t extra; 226181643Skmacy 227230587Sken /** Size of entire packet in bytes. */ 228230587Sken uint16_t size; 229181643Skmacy 230230587Sken /** The size of the first entry's data in bytes */ 231230587Sken uint16_t car_size; 232181643Skmacy 233230587Sken /** 234230587Sken * Either NETTXF_ or NETRXF_ flags. Note that the flag values are 235230587Sken * not the same for TX and RX packets 236230587Sken */ 237230587Sken uint16_t flags; 238181643Skmacy 239230587Sken /** 240230587Sken * The number of valid data-bearing entries (either netif_tx_request's 241230587Sken * or netif_rx_response's) in the packet. If this is 0, it means the 242230587Sken * entire packet is invalid. 243230587Sken */ 244230587Sken uint16_t list_len; 245181643Skmacy 246230587Sken /** There was an error processing the packet */ 247230587Sken uint8_t error; 248230587Sken}; 249181643Skmacy 250230587Sken/** xnb_pkt method: initialize it */ 251230587Skenstatic inline void 252230587Skenxnb_pkt_initialize(struct xnb_pkt *pxnb) 253230587Sken{ 254230587Sken bzero(pxnb, sizeof(*pxnb)); 255230587Sken} 256181643Skmacy 257230587Sken/** xnb_pkt method: mark the packet as valid */ 258230587Skenstatic inline void 259230587Skenxnb_pkt_validate(struct xnb_pkt *pxnb) 260230587Sken{ 261230587Sken pxnb->error = 0; 262230587Sken}; 263181643Skmacy 264230587Sken/** xnb_pkt method: mark the packet as invalid */ 265230587Skenstatic inline void 266230587Skenxnb_pkt_invalidate(struct xnb_pkt *pxnb) 267230587Sken{ 268230587Sken pxnb->error = 1; 269230587Sken}; 270181643Skmacy 271230587Sken/** xnb_pkt method: Check whether the packet is valid */ 272230587Skenstatic inline int 273230587Skenxnb_pkt_is_valid(const struct xnb_pkt *pxnb) 274230587Sken{ 275230587Sken return (! pxnb->error); 276230587Sken} 277181643Skmacy 278230587Sken#ifdef XNB_DEBUG 279230587Sken/** xnb_pkt method: print the packet's contents in human-readable format*/ 280230587Skenstatic void __unused 281230587Skenxnb_dump_pkt(const struct xnb_pkt *pkt) { 282230587Sken if (pkt == NULL) { 283230587Sken DPRINTF("Was passed a null pointer.\n"); 284230587Sken return; 285230587Sken } 286230587Sken DPRINTF("pkt address= %p\n", pkt); 287230587Sken DPRINTF("pkt->size=%d\n", pkt->size); 288230587Sken DPRINTF("pkt->car_size=%d\n", pkt->car_size); 289230587Sken DPRINTF("pkt->flags=0x%04x\n", pkt->flags); 290230587Sken DPRINTF("pkt->list_len=%d\n", pkt->list_len); 291230587Sken /* DPRINTF("pkt->extra"); TODO */ 292230587Sken DPRINTF("pkt->car=%d\n", pkt->car); 293230587Sken DPRINTF("pkt->cdr=%d\n", pkt->cdr); 294230587Sken DPRINTF("pkt->error=%d\n", pkt->error); 295230587Sken} 296230587Sken#endif /* XNB_DEBUG */ 297181643Skmacy 298181643Skmacystatic void 299230587Skenxnb_dump_txreq(RING_IDX idx, const struct netif_tx_request *txreq) 300181643Skmacy{ 301230587Sken if (txreq != NULL) { 302230587Sken DPRINTF("netif_tx_request index =%u\n", idx); 303230587Sken DPRINTF("netif_tx_request.gref =%u\n", txreq->gref); 304230587Sken DPRINTF("netif_tx_request.offset=%hu\n", txreq->offset); 305230587Sken DPRINTF("netif_tx_request.flags =%hu\n", txreq->flags); 306230587Sken DPRINTF("netif_tx_request.id =%hu\n", txreq->id); 307230587Sken DPRINTF("netif_tx_request.size =%hu\n", txreq->size); 308181643Skmacy } 309181643Skmacy} 310181643Skmacy 311181643Skmacy 312230587Sken/** 313230587Sken * \brief Configuration data for a shared memory request ring 314230587Sken * used to communicate with the front-end client of this 315230587Sken * this driver. 316230587Sken */ 317230587Skenstruct xnb_ring_config { 318230587Sken /** 319230587Sken * Runtime structures for ring access. Unfortunately, TX and RX rings 320230587Sken * use different data structures, and that cannot be changed since it 321230587Sken * is part of the interdomain protocol. 322230587Sken */ 323230587Sken union{ 324230587Sken netif_rx_back_ring_t rx_ring; 325230587Sken netif_tx_back_ring_t tx_ring; 326230587Sken } back_ring; 327181643Skmacy 328230587Sken /** 329230587Sken * The device bus address returned by the hypervisor when 330230587Sken * mapping the ring and required to unmap it when a connection 331230587Sken * is torn down. 332230587Sken */ 333230587Sken uint64_t bus_addr; 334181643Skmacy 335230587Sken /** The pseudo-physical address where ring memory is mapped.*/ 336230587Sken uint64_t gnt_addr; 337230587Sken 338230587Sken /** KVA address where ring memory is mapped. */ 339230587Sken vm_offset_t va; 340230587Sken 341230587Sken /** 342230587Sken * Grant table handles, one per-ring page, returned by the 343230587Sken * hyperpervisor upon mapping of the ring and required to 344230587Sken * unmap it when a connection is torn down. 345230587Sken */ 346230587Sken grant_handle_t handle; 347230587Sken 348230587Sken /** The number of ring pages mapped for the current connection. */ 349230587Sken unsigned ring_pages; 350230587Sken 351230587Sken /** 352230587Sken * The grant references, one per-ring page, supplied by the 353230587Sken * front-end, allowing us to reference the ring pages in the 354230587Sken * front-end's domain and to map these pages into our own domain. 355230587Sken */ 356230587Sken grant_ref_t ring_ref; 357230587Sken}; 358230587Sken 359230587Sken/** 360230587Sken * Per-instance connection state flags. 361230587Sken */ 362230587Skentypedef enum 363181643Skmacy{ 364230587Sken /** Communication with the front-end has been established. */ 365230587Sken XNBF_RING_CONNECTED = 0x01, 366181643Skmacy 367230587Sken /** 368230587Sken * Front-end requests exist in the ring and are waiting for 369230587Sken * xnb_xen_req objects to free up. 370230587Sken */ 371230587Sken XNBF_RESOURCE_SHORTAGE = 0x02, 372181643Skmacy 373230587Sken /** Connection teardown has started. */ 374230587Sken XNBF_SHUTDOWN = 0x04, 375181643Skmacy 376230587Sken /** A thread is already performing shutdown processing. */ 377230587Sken XNBF_IN_SHUTDOWN = 0x08 378230587Sken} xnb_flag_t; 379181643Skmacy 380230587Sken/** 381230587Sken * Types of rings. Used for array indices and to identify a ring's control 382230587Sken * data structure type 383230587Sken */ 384230587Skentypedef enum{ 385230587Sken XNB_RING_TYPE_TX = 0, /* ID of TX rings, used for array indices */ 386230587Sken XNB_RING_TYPE_RX = 1, /* ID of RX rings, used for array indices */ 387230587Sken XNB_NUM_RING_TYPES 388230587Sken} xnb_ring_type_t; 389181643Skmacy 390230587Sken/** 391230587Sken * Per-instance configuration data. 392230587Sken */ 393230587Skenstruct xnb_softc { 394230587Sken /** NewBus device corresponding to this instance. */ 395230587Sken device_t dev; 396181643Skmacy 397230587Sken /* Media related fields */ 398181643Skmacy 399230587Sken /** Generic network media state */ 400230587Sken struct ifmedia sc_media; 401181643Skmacy 402230587Sken /** Media carrier info */ 403230587Sken struct ifnet *xnb_ifp; 404181643Skmacy 405230587Sken /** Our own private carrier state */ 406230587Sken unsigned carrier; 407181643Skmacy 408230587Sken /** Device MAC Address */ 409230587Sken uint8_t mac[ETHER_ADDR_LEN]; 410181643Skmacy 411230587Sken /* Xen related fields */ 412181643Skmacy 413230587Sken /** 414230587Sken * \brief The netif protocol abi in effect. 415230587Sken * 416230587Sken * There are situations where the back and front ends can 417230587Sken * have a different, native abi (e.g. intel x86_64 and 418230587Sken * 32bit x86 domains on the same machine). The back-end 419230587Sken * always accomodates the front-end's native abi. That 420230587Sken * value is pulled from the XenStore and recorded here. 421230587Sken */ 422230587Sken int abi; 423181643Skmacy 424230587Sken /** 425230587Sken * Name of the bridge to which this VIF is connected, if any 426230587Sken * This field is dynamically allocated by xenbus and must be free()ed 427230587Sken * when no longer needed 428230587Sken */ 429230587Sken char *bridge; 430181643Skmacy 431230587Sken /** The interrupt driven even channel used to signal ring events. */ 432230587Sken evtchn_port_t evtchn; 433181643Skmacy 434230587Sken /** Xen device handle.*/ 435230587Sken long handle; 436181643Skmacy 437255040Sgibbs /** Handle to the communication ring event channel. */ 438255040Sgibbs xen_intr_handle_t xen_intr_handle; 439181643Skmacy 440230587Sken /** 441230587Sken * \brief Cached value of the front-end's domain id. 442230587Sken * 443230587Sken * This value is used at once for each mapped page in 444230587Sken * a transaction. We cache it to avoid incuring the 445230587Sken * cost of an ivar access every time this is needed. 446230587Sken */ 447230587Sken domid_t otherend_id; 448181643Skmacy 449230587Sken /** 450230587Sken * Undocumented frontend feature. Has something to do with 451230587Sken * scatter/gather IO 452230587Sken */ 453230587Sken uint8_t can_sg; 454230587Sken /** Undocumented frontend feature */ 455230587Sken uint8_t gso; 456230587Sken /** Undocumented frontend feature */ 457230587Sken uint8_t gso_prefix; 458230587Sken /** Can checksum TCP/UDP over IPv4 */ 459230587Sken uint8_t ip_csum; 460181643Skmacy 461230587Sken /* Implementation related fields */ 462230587Sken /** 463230587Sken * Preallocated grant table copy descriptor for RX operations. 464230587Sken * Access must be protected by rx_lock 465230587Sken */ 466230587Sken gnttab_copy_table rx_gnttab; 467181643Skmacy 468230587Sken /** 469230587Sken * Preallocated grant table copy descriptor for TX operations. 470230587Sken * Access must be protected by tx_lock 471230587Sken */ 472230587Sken gnttab_copy_table tx_gnttab; 473181643Skmacy 474230587Sken#ifdef XENHVM 475230587Sken /** 476230587Sken * Resource representing allocated physical address space 477230587Sken * associated with our per-instance kva region. 478230587Sken */ 479230587Sken struct resource *pseudo_phys_res; 480181643Skmacy 481230587Sken /** Resource id for allocated physical address space. */ 482230587Sken int pseudo_phys_res_id; 483230587Sken#endif 484181643Skmacy 485230587Sken /** Ring mapping and interrupt configuration data. */ 486230587Sken struct xnb_ring_config ring_configs[XNB_NUM_RING_TYPES]; 487181643Skmacy 488230587Sken /** 489230587Sken * Global pool of kva used for mapping remote domain ring 490230587Sken * and I/O transaction data. 491230587Sken */ 492230587Sken vm_offset_t kva; 493181643Skmacy 494230587Sken /** Psuedo-physical address corresponding to kva. */ 495230587Sken uint64_t gnt_base_addr; 496181643Skmacy 497230587Sken /** Various configuration and state bit flags. */ 498230587Sken xnb_flag_t flags; 499230587Sken 500230587Sken /** Mutex protecting per-instance data in the receive path. */ 501230587Sken struct mtx rx_lock; 502230587Sken 503230587Sken /** Mutex protecting per-instance data in the softc structure. */ 504230587Sken struct mtx sc_lock; 505230587Sken 506230587Sken /** Mutex protecting per-instance data in the transmit path. */ 507230587Sken struct mtx tx_lock; 508230587Sken 509230587Sken /** The size of the global kva pool. */ 510230587Sken int kva_size; 511230587Sken}; 512230587Sken 513230587Sken/*---------------------------- Debugging functions ---------------------------*/ 514230587Sken#ifdef XNB_DEBUG 515230587Skenstatic void __unused 516230587Skenxnb_dump_gnttab_copy(const struct gnttab_copy *entry) 517181643Skmacy{ 518230587Sken if (entry == NULL) { 519230587Sken printf("NULL grant table pointer\n"); 520230587Sken return; 521181643Skmacy } 522230587Sken 523230587Sken if (entry->flags & GNTCOPY_dest_gref) 524230587Sken printf("gnttab dest ref=\t%u\n", entry->dest.u.ref); 525230587Sken else 526230587Sken printf("gnttab dest gmfn=\t%lu\n", entry->dest.u.gmfn); 527230587Sken printf("gnttab dest offset=\t%hu\n", entry->dest.offset); 528230587Sken printf("gnttab dest domid=\t%hu\n", entry->dest.domid); 529230587Sken if (entry->flags & GNTCOPY_source_gref) 530230587Sken printf("gnttab source ref=\t%u\n", entry->source.u.ref); 531230587Sken else 532230587Sken printf("gnttab source gmfn=\t%lu\n", entry->source.u.gmfn); 533230587Sken printf("gnttab source offset=\t%hu\n", entry->source.offset); 534230587Sken printf("gnttab source domid=\t%hu\n", entry->source.domid); 535230587Sken printf("gnttab len=\t%hu\n", entry->len); 536230587Sken printf("gnttab flags=\t%hu\n", entry->flags); 537230587Sken printf("gnttab status=\t%hd\n", entry->status); 538181643Skmacy} 539181643Skmacy 540181643Skmacystatic int 541230587Skenxnb_dump_rings(SYSCTL_HANDLER_ARGS) 542181643Skmacy{ 543230587Sken static char results[720]; 544230587Sken struct xnb_softc const* xnb = (struct xnb_softc*)arg1; 545230587Sken netif_rx_back_ring_t const* rxb = 546230587Sken &xnb->ring_configs[XNB_RING_TYPE_RX].back_ring.rx_ring; 547230587Sken netif_tx_back_ring_t const* txb = 548230587Sken &xnb->ring_configs[XNB_RING_TYPE_TX].back_ring.tx_ring; 549181643Skmacy 550230587Sken /* empty the result strings */ 551230587Sken results[0] = 0; 552181643Skmacy 553230587Sken if ( !txb || !txb->sring || !rxb || !rxb->sring ) 554230587Sken return (SYSCTL_OUT(req, results, strnlen(results, 720))); 555181643Skmacy 556230587Sken snprintf(results, 720, 557230587Sken "\n\t%35s %18s\n" /* TX, RX */ 558230587Sken "\t%16s %18d %18d\n" /* req_cons */ 559230587Sken "\t%16s %18d %18d\n" /* nr_ents */ 560230587Sken "\t%16s %18d %18d\n" /* rsp_prod_pvt */ 561230587Sken "\t%16s %18p %18p\n" /* sring */ 562230587Sken "\t%16s %18d %18d\n" /* req_prod */ 563230587Sken "\t%16s %18d %18d\n" /* req_event */ 564230587Sken "\t%16s %18d %18d\n" /* rsp_prod */ 565230587Sken "\t%16s %18d %18d\n", /* rsp_event */ 566230587Sken "TX", "RX", 567230587Sken "req_cons", txb->req_cons, rxb->req_cons, 568230587Sken "nr_ents", txb->nr_ents, rxb->nr_ents, 569230587Sken "rsp_prod_pvt", txb->rsp_prod_pvt, rxb->rsp_prod_pvt, 570230587Sken "sring", txb->sring, rxb->sring, 571230587Sken "sring->req_prod", txb->sring->req_prod, rxb->sring->req_prod, 572230587Sken "sring->req_event", txb->sring->req_event, rxb->sring->req_event, 573230587Sken "sring->rsp_prod", txb->sring->rsp_prod, rxb->sring->rsp_prod, 574230587Sken "sring->rsp_event", txb->sring->rsp_event, rxb->sring->rsp_event); 575230587Sken 576230587Sken return (SYSCTL_OUT(req, results, strnlen(results, 720))); 577181643Skmacy} 578181643Skmacy 579230587Skenstatic void __unused 580230587Skenxnb_dump_mbuf(const struct mbuf *m) 581181643Skmacy{ 582230587Sken int len; 583230587Sken uint8_t *d; 584230587Sken if (m == NULL) 585230587Sken return; 586181643Skmacy 587230587Sken printf("xnb_dump_mbuf:\n"); 588230587Sken if (m->m_flags & M_PKTHDR) { 589230587Sken printf(" flowid=%10d, csum_flags=%#8x, csum_data=%#8x, " 590230587Sken "tso_segsz=%5hd\n", 591254910Sandre m->m_pkthdr.flowid, (int)m->m_pkthdr.csum_flags, 592230587Sken m->m_pkthdr.csum_data, m->m_pkthdr.tso_segsz); 593254910Sandre printf(" rcvif=%16p, len=%19d\n", 594254910Sandre m->m_pkthdr.rcvif, m->m_pkthdr.len); 595230587Sken } 596230587Sken printf(" m_next=%16p, m_nextpk=%16p, m_data=%16p\n", 597230587Sken m->m_next, m->m_nextpkt, m->m_data); 598254910Sandre printf(" m_len=%17d, m_flags=%#15x, m_type=%18u\n", 599230587Sken m->m_len, m->m_flags, m->m_type); 600181643Skmacy 601230587Sken len = m->m_len; 602230587Sken d = mtod(m, uint8_t*); 603230587Sken while (len > 0) { 604230587Sken int i; 605230587Sken printf(" "); 606230587Sken for (i = 0; (i < 16) && (len > 0); i++, len--) { 607230587Sken printf("%02hhx ", *(d++)); 608230587Sken } 609230587Sken printf("\n"); 610181643Skmacy } 611181643Skmacy} 612230587Sken#endif /* XNB_DEBUG */ 613181643Skmacy 614230587Sken/*------------------------ Inter-Domain Communication ------------------------*/ 615230587Sken/** 616230587Sken * Free dynamically allocated KVA or pseudo-physical address allocations. 617230587Sken * 618230587Sken * \param xnb Per-instance xnb configuration structure. 619230587Sken */ 620181643Skmacystatic void 621230587Skenxnb_free_communication_mem(struct xnb_softc *xnb) 622181643Skmacy{ 623230587Sken if (xnb->kva != 0) { 624230587Sken#ifndef XENHVM 625254025Sjeff kva_free(xnb->kva, xnb->kva_size); 626230587Sken#else 627230587Sken if (xnb->pseudo_phys_res != NULL) { 628230587Sken bus_release_resource(xnb->dev, SYS_RES_MEMORY, 629230587Sken xnb->pseudo_phys_res_id, 630230587Sken xnb->pseudo_phys_res); 631230587Sken xnb->pseudo_phys_res = NULL; 632230587Sken } 633230587Sken#endif /* XENHVM */ 634181643Skmacy } 635230587Sken xnb->kva = 0; 636230587Sken xnb->gnt_base_addr = 0; 637181643Skmacy} 638181643Skmacy 639230587Sken/** 640230587Sken * Cleanup all inter-domain communication mechanisms. 641230587Sken * 642230587Sken * \param xnb Per-instance xnb configuration structure. 643181643Skmacy */ 644230587Skenstatic int 645230587Skenxnb_disconnect(struct xnb_softc *xnb) 646181643Skmacy{ 647230587Sken struct gnttab_unmap_grant_ref gnts[XNB_NUM_RING_TYPES]; 648230587Sken int error; 649230587Sken int i; 650181643Skmacy 651255040Sgibbs xen_intr_unbind(xnb->xen_intr_handle); 652181643Skmacy 653230587Sken /* 654230587Sken * We may still have another thread currently processing requests. We 655230587Sken * must acquire the rx and tx locks to make sure those threads are done, 656230587Sken * but we can release those locks as soon as we acquire them, because no 657230587Sken * more interrupts will be arriving. 658230587Sken */ 659230587Sken mtx_lock(&xnb->tx_lock); 660230587Sken mtx_unlock(&xnb->tx_lock); 661230587Sken mtx_lock(&xnb->rx_lock); 662230587Sken mtx_unlock(&xnb->rx_lock); 663230587Sken 664230587Sken /* Free malloc'd softc member variables */ 665230587Sken if (xnb->bridge != NULL) 666230587Sken free(xnb->bridge, M_XENSTORE); 667230587Sken 668230587Sken /* All request processing has stopped, so unmap the rings */ 669230587Sken for (i=0; i < XNB_NUM_RING_TYPES; i++) { 670230587Sken gnts[i].host_addr = xnb->ring_configs[i].gnt_addr; 671230587Sken gnts[i].dev_bus_addr = xnb->ring_configs[i].bus_addr; 672230587Sken gnts[i].handle = xnb->ring_configs[i].handle; 673181643Skmacy } 674230587Sken error = HYPERVISOR_grant_table_op(GNTTABOP_unmap_grant_ref, gnts, 675230587Sken XNB_NUM_RING_TYPES); 676230587Sken KASSERT(error == 0, ("Grant table unmap op failed (%d)", error)); 677181643Skmacy 678230587Sken xnb_free_communication_mem(xnb); 679230587Sken /* 680230587Sken * Zero the ring config structs because the pointers, handles, and 681230587Sken * grant refs contained therein are no longer valid. 682230587Sken */ 683230587Sken bzero(&xnb->ring_configs[XNB_RING_TYPE_TX], 684230587Sken sizeof(struct xnb_ring_config)); 685230587Sken bzero(&xnb->ring_configs[XNB_RING_TYPE_RX], 686230587Sken sizeof(struct xnb_ring_config)); 687181643Skmacy 688230587Sken xnb->flags &= ~XNBF_RING_CONNECTED; 689230587Sken return (0); 690181643Skmacy} 691181643Skmacy 692230587Sken/** 693230587Sken * Map a single shared memory ring into domain local address space and 694230587Sken * initialize its control structure 695230587Sken * 696230587Sken * \param xnb Per-instance xnb configuration structure 697230587Sken * \param ring_type Array index of this ring in the xnb's array of rings 698230587Sken * \return An errno 699230587Sken */ 700230587Skenstatic int 701230587Skenxnb_connect_ring(struct xnb_softc *xnb, xnb_ring_type_t ring_type) 702181643Skmacy{ 703230587Sken struct gnttab_map_grant_ref gnt; 704230587Sken struct xnb_ring_config *ring = &xnb->ring_configs[ring_type]; 705230587Sken int error; 706181643Skmacy 707230587Sken /* TX ring type = 0, RX =1 */ 708230587Sken ring->va = xnb->kva + ring_type * PAGE_SIZE; 709230587Sken ring->gnt_addr = xnb->gnt_base_addr + ring_type * PAGE_SIZE; 710181643Skmacy 711230587Sken gnt.host_addr = ring->gnt_addr; 712230587Sken gnt.flags = GNTMAP_host_map; 713230587Sken gnt.ref = ring->ring_ref; 714230587Sken gnt.dom = xnb->otherend_id; 715181643Skmacy 716230587Sken error = HYPERVISOR_grant_table_op(GNTTABOP_map_grant_ref, &gnt, 1); 717230587Sken if (error != 0) 718230587Sken panic("netback: Ring page grant table op failed (%d)", error); 719230587Sken 720230587Sken if (gnt.status != 0) { 721230587Sken ring->va = 0; 722230587Sken error = EACCES; 723230587Sken xenbus_dev_fatal(xnb->dev, error, 724230587Sken "Ring shared page mapping failed. " 725230587Sken "Status %d.", gnt.status); 726230587Sken } else { 727230587Sken ring->handle = gnt.handle; 728230587Sken ring->bus_addr = gnt.dev_bus_addr; 729230587Sken 730230587Sken if (ring_type == XNB_RING_TYPE_TX) { 731230587Sken BACK_RING_INIT(&ring->back_ring.tx_ring, 732230587Sken (netif_tx_sring_t*)ring->va, 733230587Sken ring->ring_pages * PAGE_SIZE); 734230587Sken } else if (ring_type == XNB_RING_TYPE_RX) { 735230587Sken BACK_RING_INIT(&ring->back_ring.rx_ring, 736230587Sken (netif_rx_sring_t*)ring->va, 737230587Sken ring->ring_pages * PAGE_SIZE); 738230587Sken } else { 739230587Sken xenbus_dev_fatal(xnb->dev, error, 740230587Sken "Unknown ring type %d", ring_type); 741230587Sken } 742181643Skmacy } 743230587Sken 744230587Sken return error; 745181643Skmacy} 746181643Skmacy 747230587Sken/** 748230587Sken * Setup the shared memory rings and bind an interrupt to the event channel 749230587Sken * used to notify us of ring changes. 750230587Sken * 751230587Sken * \param xnb Per-instance xnb configuration structure. 752230587Sken */ 753230587Skenstatic int 754230587Skenxnb_connect_comms(struct xnb_softc *xnb) 755181643Skmacy{ 756230587Sken int error; 757230587Sken xnb_ring_type_t i; 758181643Skmacy 759230587Sken if ((xnb->flags & XNBF_RING_CONNECTED) != 0) 760230587Sken return (0); 761181643Skmacy 762181643Skmacy /* 763230587Sken * Kva for our rings are at the tail of the region of kva allocated 764230587Sken * by xnb_alloc_communication_mem(). 765181643Skmacy */ 766230587Sken for (i=0; i < XNB_NUM_RING_TYPES; i++) { 767230587Sken error = xnb_connect_ring(xnb, i); 768230587Sken if (error != 0) 769230587Sken return error; 770181643Skmacy } 771181643Skmacy 772230587Sken xnb->flags |= XNBF_RING_CONNECTED; 773181643Skmacy 774255040Sgibbs error = xen_intr_bind_remote_port(xnb->dev, 775255040Sgibbs xnb->otherend_id, 776255040Sgibbs xnb->evtchn, 777255040Sgibbs /*filter*/NULL, 778255040Sgibbs xnb_intr, /*arg*/xnb, 779255040Sgibbs INTR_TYPE_BIO | INTR_MPSAFE, 780255040Sgibbs &xnb->xen_intr_handle); 781230587Sken if (error != 0) { 782230587Sken (void)xnb_disconnect(xnb); 783230587Sken xenbus_dev_fatal(xnb->dev, error, "binding event channel"); 784230587Sken return (error); 785230587Sken } 786181643Skmacy 787230587Sken DPRINTF("rings connected!\n"); 788181643Skmacy 789230587Sken return (0); 790181643Skmacy} 791181643Skmacy 792230587Sken/** 793230587Sken * Size KVA and pseudo-physical address allocations based on negotiated 794230587Sken * values for the size and number of I/O requests, and the size of our 795230587Sken * communication ring. 796230587Sken * 797230587Sken * \param xnb Per-instance xnb configuration structure. 798230587Sken * 799230587Sken * These address spaces are used to dynamically map pages in the 800230587Sken * front-end's domain into our own. 801230587Sken */ 802230587Skenstatic int 803230587Skenxnb_alloc_communication_mem(struct xnb_softc *xnb) 804181643Skmacy{ 805230587Sken xnb_ring_type_t i; 806181643Skmacy 807230587Sken xnb->kva_size = 0; 808230587Sken for (i=0; i < XNB_NUM_RING_TYPES; i++) { 809230587Sken xnb->kva_size += xnb->ring_configs[i].ring_pages * PAGE_SIZE; 810230587Sken } 811230587Sken#ifndef XENHVM 812254025Sjeff xnb->kva = kva_alloc(xnb->kva_size); 813230587Sken if (xnb->kva == 0) 814230587Sken return (ENOMEM); 815230587Sken xnb->gnt_base_addr = xnb->kva; 816230587Sken#else /* defined XENHVM */ 817230587Sken /* 818230587Sken * Reserve a range of pseudo physical memory that we can map 819230587Sken * into kva. These pages will only be backed by machine 820230587Sken * pages ("real memory") during the lifetime of front-end requests 821230587Sken * via grant table operations. We will map the netif tx and rx rings 822230587Sken * into this space. 823230587Sken */ 824230587Sken xnb->pseudo_phys_res_id = 0; 825230587Sken xnb->pseudo_phys_res = bus_alloc_resource(xnb->dev, SYS_RES_MEMORY, 826230587Sken &xnb->pseudo_phys_res_id, 827230587Sken 0, ~0, xnb->kva_size, 828230587Sken RF_ACTIVE); 829230587Sken if (xnb->pseudo_phys_res == NULL) { 830230587Sken xnb->kva = 0; 831230587Sken return (ENOMEM); 832230587Sken } 833230587Sken xnb->kva = (vm_offset_t)rman_get_virtual(xnb->pseudo_phys_res); 834230587Sken xnb->gnt_base_addr = rman_get_start(xnb->pseudo_phys_res); 835230587Sken#endif /* !defined XENHVM */ 836230587Sken return (0); 837181643Skmacy} 838181643Skmacy 839230587Sken/** 840230587Sken * Collect information from the XenStore related to our device and its frontend 841230587Sken * 842230587Sken * \param xnb Per-instance xnb configuration structure. 843230587Sken */ 844230587Skenstatic int 845230587Skenxnb_collect_xenstore_info(struct xnb_softc *xnb) 846181643Skmacy{ 847230587Sken /** 848230587Sken * \todo Linux collects the following info. We should collect most 849230587Sken * of this, too: 850230587Sken * "feature-rx-notify" 851230587Sken */ 852230587Sken const char *otherend_path; 853230587Sken const char *our_path; 854230587Sken int err; 855230587Sken unsigned int rx_copy, bridge_len; 856230587Sken uint8_t no_csum_offload; 857181643Skmacy 858230587Sken otherend_path = xenbus_get_otherend_path(xnb->dev); 859230587Sken our_path = xenbus_get_node(xnb->dev); 860181643Skmacy 861230587Sken /* Collect the critical communication parameters */ 862230587Sken err = xs_gather(XST_NIL, otherend_path, 863230587Sken "tx-ring-ref", "%l" PRIu32, 864230587Sken &xnb->ring_configs[XNB_RING_TYPE_TX].ring_ref, 865230587Sken "rx-ring-ref", "%l" PRIu32, 866230587Sken &xnb->ring_configs[XNB_RING_TYPE_RX].ring_ref, 867230587Sken "event-channel", "%" PRIu32, &xnb->evtchn, 868230587Sken NULL); 869230587Sken if (err != 0) { 870230587Sken xenbus_dev_fatal(xnb->dev, err, 871230587Sken "Unable to retrieve ring information from " 872230587Sken "frontend %s. Unable to connect.", 873230587Sken otherend_path); 874230587Sken return (err); 875230587Sken } 876181643Skmacy 877230587Sken /* Collect the handle from xenstore */ 878230587Sken err = xs_scanf(XST_NIL, our_path, "handle", NULL, "%li", &xnb->handle); 879230587Sken if (err != 0) { 880230587Sken xenbus_dev_fatal(xnb->dev, err, 881230587Sken "Error reading handle from frontend %s. " 882230587Sken "Unable to connect.", otherend_path); 883230587Sken } 884181643Skmacy 885230587Sken /* 886230587Sken * Collect the bridgename, if any. We do not need bridge_len; we just 887230587Sken * throw it away 888230587Sken */ 889230587Sken err = xs_read(XST_NIL, our_path, "bridge", &bridge_len, 890230587Sken (void**)&xnb->bridge); 891230587Sken if (err != 0) 892230587Sken xnb->bridge = NULL; 893181643Skmacy 894230587Sken /* 895230587Sken * Does the frontend request that we use rx copy? If not, return an 896230587Sken * error because this driver only supports rx copy. 897230587Sken */ 898230587Sken err = xs_scanf(XST_NIL, otherend_path, "request-rx-copy", NULL, 899230587Sken "%" PRIu32, &rx_copy); 900230587Sken if (err == ENOENT) { 901230587Sken err = 0; 902230587Sken rx_copy = 0; 903230587Sken } 904230587Sken if (err < 0) { 905230587Sken xenbus_dev_fatal(xnb->dev, err, "reading %s/request-rx-copy", 906230587Sken otherend_path); 907230587Sken return err; 908230587Sken } 909230587Sken /** 910230587Sken * \todo: figure out the exact meaning of this feature, and when 911230587Sken * the frontend will set it to true. It should be set to true 912230587Sken * at some point 913230587Sken */ 914230587Sken/* if (!rx_copy)*/ 915230587Sken/* return EOPNOTSUPP;*/ 916181643Skmacy 917230587Sken /** \todo Collect the rx notify feature */ 918181643Skmacy 919230587Sken /* Collect the feature-sg. */ 920230587Sken if (xs_scanf(XST_NIL, otherend_path, "feature-sg", NULL, 921230587Sken "%hhu", &xnb->can_sg) < 0) 922230587Sken xnb->can_sg = 0; 923181643Skmacy 924230587Sken /* Collect remaining frontend features */ 925230587Sken if (xs_scanf(XST_NIL, otherend_path, "feature-gso-tcpv4", NULL, 926230587Sken "%hhu", &xnb->gso) < 0) 927230587Sken xnb->gso = 0; 928181643Skmacy 929230587Sken if (xs_scanf(XST_NIL, otherend_path, "feature-gso-tcpv4-prefix", NULL, 930230587Sken "%hhu", &xnb->gso_prefix) < 0) 931230587Sken xnb->gso_prefix = 0; 932181643Skmacy 933230587Sken if (xs_scanf(XST_NIL, otherend_path, "feature-no-csum-offload", NULL, 934230587Sken "%hhu", &no_csum_offload) < 0) 935230587Sken no_csum_offload = 0; 936230587Sken xnb->ip_csum = (no_csum_offload == 0); 937181643Skmacy 938230587Sken return (0); 939230587Sken} 940230587Sken 941230587Sken/** 942230587Sken * Supply information about the physical device to the frontend 943230587Sken * via XenBus. 944230587Sken * 945230587Sken * \param xnb Per-instance xnb configuration structure. 946230587Sken */ 947230587Skenstatic int 948230587Skenxnb_publish_backend_info(struct xnb_softc *xnb) 949230587Sken{ 950230587Sken struct xs_transaction xst; 951230587Sken const char *our_path; 952230587Sken int error; 953230587Sken 954230587Sken our_path = xenbus_get_node(xnb->dev); 955230587Sken 956230587Sken do { 957230587Sken error = xs_transaction_start(&xst); 958230587Sken if (error != 0) { 959230587Sken xenbus_dev_fatal(xnb->dev, error, 960230587Sken "Error publishing backend info " 961230587Sken "(start transaction)"); 962230587Sken break; 963181643Skmacy } 964181643Skmacy 965230587Sken error = xs_printf(xst, our_path, "feature-sg", 966230587Sken "%d", XNB_SG); 967230587Sken if (error != 0) 968230587Sken break; 969181643Skmacy 970230587Sken error = xs_printf(xst, our_path, "feature-gso-tcpv4", 971230587Sken "%d", XNB_GSO_TCPV4); 972230587Sken if (error != 0) 973181643Skmacy break; 974230587Sken 975230587Sken error = xs_printf(xst, our_path, "feature-rx-copy", 976230587Sken "%d", XNB_RX_COPY); 977230587Sken if (error != 0) 978230587Sken break; 979230587Sken 980230587Sken error = xs_printf(xst, our_path, "feature-rx-flip", 981230587Sken "%d", XNB_RX_FLIP); 982230587Sken if (error != 0) 983230587Sken break; 984230587Sken 985230587Sken error = xs_transaction_end(xst, 0); 986230587Sken if (error != 0 && error != EAGAIN) { 987230587Sken xenbus_dev_fatal(xnb->dev, error, "ending transaction"); 988230587Sken break; 989181643Skmacy } 990181643Skmacy 991230587Sken } while (error == EAGAIN); 992181643Skmacy 993230587Sken return (error); 994230587Sken} 995181643Skmacy 996230587Sken/** 997230587Sken * Connect to our netfront peer now that it has completed publishing 998230587Sken * its configuration into the XenStore. 999230587Sken * 1000230587Sken * \param xnb Per-instance xnb configuration structure. 1001230587Sken */ 1002230587Skenstatic void 1003230587Skenxnb_connect(struct xnb_softc *xnb) 1004230587Sken{ 1005230587Sken int error; 1006181643Skmacy 1007230587Sken if (xenbus_get_state(xnb->dev) == XenbusStateConnected) 1008230587Sken return; 1009181643Skmacy 1010230587Sken if (xnb_collect_xenstore_info(xnb) != 0) 1011230587Sken return; 1012181643Skmacy 1013230587Sken xnb->flags &= ~XNBF_SHUTDOWN; 1014230587Sken 1015230587Sken /* Read front end configuration. */ 1016230587Sken 1017230587Sken /* Allocate resources whose size depends on front-end configuration. */ 1018230587Sken error = xnb_alloc_communication_mem(xnb); 1019230587Sken if (error != 0) { 1020230587Sken xenbus_dev_fatal(xnb->dev, error, 1021230587Sken "Unable to allocate communication memory"); 1022230587Sken return; 1023181643Skmacy } 1024181643Skmacy 1025230587Sken /* 1026230587Sken * Connect communication channel. 1027230587Sken */ 1028230587Sken error = xnb_connect_comms(xnb); 1029230587Sken if (error != 0) { 1030230587Sken /* Specific errors are reported by xnb_connect_comms(). */ 1031181643Skmacy return; 1032230587Sken } 1033230587Sken xnb->carrier = 1; 1034181643Skmacy 1035230587Sken /* Ready for I/O. */ 1036230587Sken xenbus_set_state(xnb->dev, XenbusStateConnected); 1037230587Sken} 1038181643Skmacy 1039230587Sken/*-------------------------- Device Teardown Support -------------------------*/ 1040230587Sken/** 1041230587Sken * Perform device shutdown functions. 1042230587Sken * 1043230587Sken * \param xnb Per-instance xnb configuration structure. 1044230587Sken * 1045230587Sken * Mark this instance as shutting down, wait for any active requests 1046230587Sken * to drain, disconnect from the front-end, and notify any waiters (e.g. 1047230587Sken * a thread invoking our detach method) that detach can now proceed. 1048230587Sken */ 1049230587Skenstatic int 1050230587Skenxnb_shutdown(struct xnb_softc *xnb) 1051230587Sken{ 1052230587Sken /* 1053230587Sken * Due to the need to drop our mutex during some 1054230587Sken * xenbus operations, it is possible for two threads 1055230587Sken * to attempt to close out shutdown processing at 1056230587Sken * the same time. Tell the caller that hits this 1057230587Sken * race to try back later. 1058230587Sken */ 1059230587Sken if ((xnb->flags & XNBF_IN_SHUTDOWN) != 0) 1060230587Sken return (EAGAIN); 1061181643Skmacy 1062230587Sken xnb->flags |= XNBF_SHUTDOWN; 1063181643Skmacy 1064230587Sken xnb->flags |= XNBF_IN_SHUTDOWN; 1065181643Skmacy 1066230587Sken mtx_unlock(&xnb->sc_lock); 1067230587Sken /* Free the network interface */ 1068230587Sken xnb->carrier = 0; 1069230587Sken if (xnb->xnb_ifp != NULL) { 1070230587Sken ether_ifdetach(xnb->xnb_ifp); 1071230587Sken if_free(xnb->xnb_ifp); 1072230587Sken xnb->xnb_ifp = NULL; 1073230587Sken } 1074230587Sken mtx_lock(&xnb->sc_lock); 1075181643Skmacy 1076230587Sken xnb_disconnect(xnb); 1077181643Skmacy 1078230587Sken mtx_unlock(&xnb->sc_lock); 1079230587Sken if (xenbus_get_state(xnb->dev) < XenbusStateClosing) 1080230587Sken xenbus_set_state(xnb->dev, XenbusStateClosing); 1081230587Sken mtx_lock(&xnb->sc_lock); 1082181643Skmacy 1083230587Sken xnb->flags &= ~XNBF_IN_SHUTDOWN; 1084181643Skmacy 1085181643Skmacy 1086230587Sken /* Indicate to xnb_detach() that is it safe to proceed. */ 1087230587Sken wakeup(xnb); 1088181643Skmacy 1089230587Sken return (0); 1090181643Skmacy} 1091181643Skmacy 1092230587Sken/** 1093230587Sken * Report an attach time error to the console and Xen, and cleanup 1094230587Sken * this instance by forcing immediate detach processing. 1095230587Sken * 1096230587Sken * \param xnb Per-instance xnb configuration structure. 1097230587Sken * \param err Errno describing the error. 1098230587Sken * \param fmt Printf style format and arguments 1099230587Sken */ 1100181643Skmacystatic void 1101230587Skenxnb_attach_failed(struct xnb_softc *xnb, int err, const char *fmt, ...) 1102181643Skmacy{ 1103230587Sken va_list ap; 1104230587Sken va_list ap_hotplug; 1105230587Sken 1106230587Sken va_start(ap, fmt); 1107230587Sken va_copy(ap_hotplug, ap); 1108230587Sken xs_vprintf(XST_NIL, xenbus_get_node(xnb->dev), 1109230587Sken "hotplug-error", fmt, ap_hotplug); 1110230587Sken va_end(ap_hotplug); 1111230587Sken xs_printf(XST_NIL, xenbus_get_node(xnb->dev), 1112230587Sken "hotplug-status", "error"); 1113230587Sken 1114230587Sken xenbus_dev_vfatal(xnb->dev, err, fmt, ap); 1115230587Sken va_end(ap); 1116230587Sken 1117230587Sken xs_printf(XST_NIL, xenbus_get_node(xnb->dev), 1118230587Sken "online", "0"); 1119230587Sken xnb_detach(xnb->dev); 1120181643Skmacy} 1121181643Skmacy 1122230587Sken/*---------------------------- NewBus Entrypoints ----------------------------*/ 1123230587Sken/** 1124230587Sken * Inspect a XenBus device and claim it if is of the appropriate type. 1125230587Sken * 1126230587Sken * \param dev NewBus device object representing a candidate XenBus device. 1127230587Sken * 1128230587Sken * \return 0 for success, errno codes for failure. 1129230587Sken */ 1130230587Skenstatic int 1131230587Skenxnb_probe(device_t dev) 1132181643Skmacy{ 1133230587Sken if (!strcmp(xenbus_get_type(dev), "vif")) { 1134230587Sken DPRINTF("Claiming device %d, %s\n", device_get_unit(dev), 1135230587Sken devclass_get_name(device_get_devclass(dev))); 1136230587Sken device_set_desc(dev, "Backend Virtual Network Device"); 1137230587Sken device_quiet(dev); 1138230587Sken return (0); 1139181643Skmacy } 1140230587Sken return (ENXIO); 1141181643Skmacy} 1142181643Skmacy 1143230587Sken/** 1144230587Sken * Setup sysctl variables to control various Network Back parameters. 1145230587Sken * 1146230587Sken * \param xnb Xen Net Back softc. 1147230587Sken * 1148230587Sken */ 1149181643Skmacystatic void 1150230587Skenxnb_setup_sysctl(struct xnb_softc *xnb) 1151181643Skmacy{ 1152230587Sken struct sysctl_ctx_list *sysctl_ctx = NULL; 1153230587Sken struct sysctl_oid *sysctl_tree = NULL; 1154230587Sken 1155230587Sken sysctl_ctx = device_get_sysctl_ctx(xnb->dev); 1156230587Sken if (sysctl_ctx == NULL) 1157181643Skmacy return; 1158181643Skmacy 1159230587Sken sysctl_tree = device_get_sysctl_tree(xnb->dev); 1160230587Sken if (sysctl_tree == NULL) 1161230587Sken return; 1162230587Sken 1163230587Sken#ifdef XNB_DEBUG 1164230587Sken SYSCTL_ADD_PROC(sysctl_ctx, 1165230587Sken SYSCTL_CHILDREN(sysctl_tree), 1166230587Sken OID_AUTO, 1167230587Sken "unit_test_results", 1168230587Sken CTLTYPE_STRING | CTLFLAG_RD, 1169230587Sken xnb, 1170230587Sken 0, 1171230587Sken xnb_unit_test_main, 1172230587Sken "A", 1173230587Sken "Results of builtin unit tests"); 1174230587Sken 1175230587Sken SYSCTL_ADD_PROC(sysctl_ctx, 1176230587Sken SYSCTL_CHILDREN(sysctl_tree), 1177230587Sken OID_AUTO, 1178230587Sken "dump_rings", 1179230587Sken CTLTYPE_STRING | CTLFLAG_RD, 1180230587Sken xnb, 1181230587Sken 0, 1182230587Sken xnb_dump_rings, 1183230587Sken "A", 1184230587Sken "Xennet Back Rings"); 1185230587Sken#endif /* XNB_DEBUG */ 1186181643Skmacy} 1187181643Skmacy 1188230587Sken/** 1189230587Sken * Create a network device. 1190230587Sken * @param handle device handle 1191230587Sken */ 1192230587Skenint 1193230587Skencreate_netdev(device_t dev) 1194181643Skmacy{ 1195230587Sken struct ifnet *ifp; 1196230587Sken struct xnb_softc *xnb; 1197230587Sken int err = 0; 1198181643Skmacy 1199230587Sken xnb = device_get_softc(dev); 1200230587Sken mtx_init(&xnb->sc_lock, "xnb_softc", "xen netback softc lock", MTX_DEF); 1201230587Sken mtx_init(&xnb->tx_lock, "xnb_tx", "xen netback tx lock", MTX_DEF); 1202230587Sken mtx_init(&xnb->rx_lock, "xnb_rx", "xen netback rx lock", MTX_DEF); 1203181643Skmacy 1204230587Sken xnb->dev = dev; 1205181643Skmacy 1206230587Sken ifmedia_init(&xnb->sc_media, 0, xnb_ifmedia_upd, xnb_ifmedia_sts); 1207230587Sken ifmedia_add(&xnb->sc_media, IFM_ETHER|IFM_MANUAL, 0, NULL); 1208230587Sken ifmedia_set(&xnb->sc_media, IFM_ETHER|IFM_MANUAL); 1209181643Skmacy 1210230587Sken err = xen_net_read_mac(dev, xnb->mac); 1211230587Sken if (err == 0) { 1212230587Sken /* Set up ifnet structure */ 1213230587Sken ifp = xnb->xnb_ifp = if_alloc(IFT_ETHER); 1214230587Sken ifp->if_softc = xnb; 1215230587Sken if_initname(ifp, "xnb", device_get_unit(dev)); 1216230587Sken ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST; 1217230587Sken ifp->if_ioctl = xnb_ioctl; 1218230587Sken ifp->if_output = ether_output; 1219230587Sken ifp->if_start = xnb_start; 1220230587Sken#ifdef notyet 1221230587Sken ifp->if_watchdog = xnb_watchdog; 1222230587Sken#endif 1223230587Sken ifp->if_init = xnb_ifinit; 1224230587Sken ifp->if_mtu = ETHERMTU; 1225230587Sken ifp->if_snd.ifq_maxlen = NET_RX_RING_SIZE - 1; 1226230587Sken 1227230587Sken ifp->if_hwassist = XNB_CSUM_FEATURES; 1228230587Sken ifp->if_capabilities = IFCAP_HWCSUM; 1229230587Sken ifp->if_capenable = IFCAP_HWCSUM; 1230230587Sken 1231230587Sken ether_ifattach(ifp, xnb->mac); 1232230587Sken xnb->carrier = 0; 1233230587Sken } 1234230587Sken 1235230587Sken return err; 1236181643Skmacy} 1237181643Skmacy 1238230587Sken/** 1239230587Sken * Attach to a XenBus device that has been claimed by our probe routine. 1240230587Sken * 1241230587Sken * \param dev NewBus device object representing this Xen Net Back instance. 1242230587Sken * 1243230587Sken * \return 0 for success, errno codes for failure. 1244230587Sken */ 1245181643Skmacystatic int 1246230587Skenxnb_attach(device_t dev) 1247181643Skmacy{ 1248230587Sken struct xnb_softc *xnb; 1249230587Sken int error; 1250230587Sken xnb_ring_type_t i; 1251181643Skmacy 1252230587Sken error = create_netdev(dev); 1253230587Sken if (error != 0) { 1254230587Sken xenbus_dev_fatal(dev, error, "creating netdev"); 1255230587Sken return (error); 1256230587Sken } 1257181643Skmacy 1258230587Sken DPRINTF("Attaching to %s\n", xenbus_get_node(dev)); 1259181643Skmacy 1260230587Sken /* 1261230587Sken * Basic initialization. 1262230587Sken * After this block it is safe to call xnb_detach() 1263230587Sken * to clean up any allocated data for this instance. 1264230587Sken */ 1265230587Sken xnb = device_get_softc(dev); 1266230587Sken xnb->otherend_id = xenbus_get_otherend_id(dev); 1267230587Sken for (i=0; i < XNB_NUM_RING_TYPES; i++) { 1268230587Sken xnb->ring_configs[i].ring_pages = 1; 1269230587Sken } 1270181643Skmacy 1271230587Sken /* 1272230587Sken * Setup sysctl variables. 1273230587Sken */ 1274230587Sken xnb_setup_sysctl(xnb); 1275181643Skmacy 1276230587Sken /* Update hot-plug status to satisfy xend. */ 1277230587Sken error = xs_printf(XST_NIL, xenbus_get_node(xnb->dev), 1278230587Sken "hotplug-status", "connected"); 1279230587Sken if (error != 0) { 1280230587Sken xnb_attach_failed(xnb, error, "writing %s/hotplug-status", 1281230587Sken xenbus_get_node(xnb->dev)); 1282230587Sken return (error); 1283230587Sken } 1284181643Skmacy 1285230587Sken if ((error = xnb_publish_backend_info(xnb)) != 0) { 1286230587Sken /* 1287230587Sken * If we can't publish our data, we cannot participate 1288230587Sken * in this connection, and waiting for a front-end state 1289230587Sken * change will not help the situation. 1290230587Sken */ 1291230587Sken xnb_attach_failed(xnb, error, 1292230587Sken "Publishing backend status for %s", 1293230587Sken xenbus_get_node(xnb->dev)); 1294230587Sken return error; 1295230587Sken } 1296181643Skmacy 1297230587Sken /* Tell the front end that we are ready to connect. */ 1298230587Sken xenbus_set_state(dev, XenbusStateInitWait); 1299181643Skmacy 1300230587Sken return (0); 1301230587Sken} 1302181643Skmacy 1303230587Sken/** 1304230587Sken * Detach from a net back device instance. 1305230587Sken * 1306230587Sken * \param dev NewBus device object representing this Xen Net Back instance. 1307230587Sken * 1308230587Sken * \return 0 for success, errno codes for failure. 1309230587Sken * 1310230587Sken * \note A net back device may be detached at any time in its life-cycle, 1311230587Sken * including part way through the attach process. For this reason, 1312230587Sken * initialization order and the intialization state checks in this 1313230587Sken * routine must be carefully coupled so that attach time failures 1314230587Sken * are gracefully handled. 1315230587Sken */ 1316230587Skenstatic int 1317230587Skenxnb_detach(device_t dev) 1318230587Sken{ 1319230587Sken struct xnb_softc *xnb; 1320181643Skmacy 1321230587Sken DPRINTF("\n"); 1322181643Skmacy 1323230587Sken xnb = device_get_softc(dev); 1324230587Sken mtx_lock(&xnb->sc_lock); 1325230587Sken while (xnb_shutdown(xnb) == EAGAIN) { 1326230587Sken msleep(xnb, &xnb->sc_lock, /*wakeup prio unchanged*/0, 1327230587Sken "xnb_shutdown", 0); 1328230587Sken } 1329230587Sken mtx_unlock(&xnb->sc_lock); 1330230587Sken DPRINTF("\n"); 1331181643Skmacy 1332230587Sken mtx_destroy(&xnb->tx_lock); 1333230587Sken mtx_destroy(&xnb->rx_lock); 1334230587Sken mtx_destroy(&xnb->sc_lock); 1335230587Sken return (0); 1336230587Sken} 1337181643Skmacy 1338230587Sken/** 1339230587Sken * Prepare this net back device for suspension of this VM. 1340230587Sken * 1341230587Sken * \param dev NewBus device object representing this Xen net Back instance. 1342230587Sken * 1343230587Sken * \return 0 for success, errno codes for failure. 1344230587Sken */ 1345230587Skenstatic int 1346230587Skenxnb_suspend(device_t dev) 1347230587Sken{ 1348230587Sken return (0); 1349230587Sken} 1350181643Skmacy 1351230587Sken/** 1352230587Sken * Perform any processing required to recover from a suspended state. 1353230587Sken * 1354230587Sken * \param dev NewBus device object representing this Xen Net Back instance. 1355230587Sken * 1356230587Sken * \return 0 for success, errno codes for failure. 1357230587Sken */ 1358230587Skenstatic int 1359230587Skenxnb_resume(device_t dev) 1360230587Sken{ 1361230587Sken return (0); 1362230587Sken} 1363181643Skmacy 1364230587Sken/** 1365230587Sken * Handle state changes expressed via the XenStore by our front-end peer. 1366230587Sken * 1367230587Sken * \param dev NewBus device object representing this Xen 1368230587Sken * Net Back instance. 1369230587Sken * \param frontend_state The new state of the front-end. 1370230587Sken * 1371230587Sken * \return 0 for success, errno codes for failure. 1372230587Sken */ 1373230587Skenstatic void 1374230587Skenxnb_frontend_changed(device_t dev, XenbusState frontend_state) 1375230587Sken{ 1376230587Sken struct xnb_softc *xnb; 1377181643Skmacy 1378230587Sken xnb = device_get_softc(dev); 1379181643Skmacy 1380230587Sken DPRINTF("frontend_state=%s, xnb_state=%s\n", 1381230587Sken xenbus_strstate(frontend_state), 1382230587Sken xenbus_strstate(xenbus_get_state(xnb->dev))); 1383181643Skmacy 1384230587Sken switch (frontend_state) { 1385230587Sken case XenbusStateInitialising: 1386230587Sken break; 1387230587Sken case XenbusStateInitialised: 1388230587Sken case XenbusStateConnected: 1389230587Sken xnb_connect(xnb); 1390230587Sken break; 1391230587Sken case XenbusStateClosing: 1392230587Sken case XenbusStateClosed: 1393230587Sken mtx_lock(&xnb->sc_lock); 1394230587Sken xnb_shutdown(xnb); 1395230587Sken mtx_unlock(&xnb->sc_lock); 1396230587Sken if (frontend_state == XenbusStateClosed) 1397230587Sken xenbus_set_state(xnb->dev, XenbusStateClosed); 1398230587Sken break; 1399230587Sken default: 1400230587Sken xenbus_dev_fatal(xnb->dev, EINVAL, "saw state %d at frontend", 1401230587Sken frontend_state); 1402230587Sken break; 1403230587Sken } 1404230587Sken} 1405181643Skmacy 1406181643Skmacy 1407230587Sken/*---------------------------- Request Processing ----------------------------*/ 1408230587Sken/** 1409230587Sken * Interrupt handler bound to the shared ring's event channel. 1410230587Sken * Entry point for the xennet transmit path in netback 1411230587Sken * Transfers packets from the Xen ring to the host's generic networking stack 1412230587Sken * 1413230587Sken * \param arg Callback argument registerd during event channel 1414230587Sken * binding - the xnb_softc for this instance. 1415230587Sken */ 1416230587Skenstatic void 1417230587Skenxnb_intr(void *arg) 1418230587Sken{ 1419230587Sken struct xnb_softc *xnb; 1420230587Sken struct ifnet *ifp; 1421230587Sken netif_tx_back_ring_t *txb; 1422230587Sken RING_IDX req_prod_local; 1423181643Skmacy 1424230587Sken xnb = (struct xnb_softc *)arg; 1425230587Sken ifp = xnb->xnb_ifp; 1426230587Sken txb = &xnb->ring_configs[XNB_RING_TYPE_TX].back_ring.tx_ring; 1427181643Skmacy 1428230587Sken mtx_lock(&xnb->tx_lock); 1429230587Sken do { 1430230587Sken int notify; 1431230587Sken req_prod_local = txb->sring->req_prod; 1432230587Sken xen_rmb(); 1433181643Skmacy 1434230587Sken for (;;) { 1435230587Sken struct mbuf *mbufc; 1436230587Sken int err; 1437181643Skmacy 1438230587Sken err = xnb_recv(txb, xnb->otherend_id, &mbufc, ifp, 1439230587Sken xnb->tx_gnttab); 1440230587Sken if (err || (mbufc == NULL)) 1441230587Sken break; 1442181643Skmacy 1443230587Sken /* Send the packet to the generic network stack */ 1444230587Sken (*xnb->xnb_ifp->if_input)(xnb->xnb_ifp, mbufc); 1445230587Sken } 1446181643Skmacy 1447230587Sken RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(txb, notify); 1448230587Sken if (notify != 0) 1449255040Sgibbs xen_intr_signal(xnb->xen_intr_handle); 1450181643Skmacy 1451230587Sken txb->sring->req_event = txb->req_cons + 1; 1452230587Sken xen_mb(); 1453230587Sken } while (txb->sring->req_prod != req_prod_local) ; 1454230587Sken mtx_unlock(&xnb->tx_lock); 1455181643Skmacy 1456230587Sken xnb_start(ifp); 1457230587Sken} 1458181643Skmacy 1459181643Skmacy 1460230587Sken/** 1461230587Sken * Build a struct xnb_pkt based on netif_tx_request's from a netif tx ring. 1462230587Sken * Will read exactly 0 or 1 packets from the ring; never a partial packet. 1463230587Sken * \param[out] pkt The returned packet. If there is an error building 1464230587Sken * the packet, pkt.list_len will be set to 0. 1465230587Sken * \param[in] tx_ring Pointer to the Ring that is the input to this function 1466230587Sken * \param[in] start The ring index of the first potential request 1467230587Sken * \return The number of requests consumed to build this packet 1468230587Sken */ 1469230587Skenstatic int 1470230587Skenxnb_ring2pkt(struct xnb_pkt *pkt, const netif_tx_back_ring_t *tx_ring, 1471230587Sken RING_IDX start) 1472230587Sken{ 1473230587Sken /* 1474230587Sken * Outline: 1475230587Sken * 1) Initialize pkt 1476230587Sken * 2) Read the first request of the packet 1477230587Sken * 3) Read the extras 1478230587Sken * 4) Set cdr 1479230587Sken * 5) Loop on the remainder of the packet 1480230587Sken * 6) Finalize pkt (stuff like car_size and list_len) 1481230587Sken */ 1482230587Sken int idx = start; 1483230587Sken int discard = 0; /* whether to discard the packet */ 1484230587Sken int more_data = 0; /* there are more request past the last one */ 1485230587Sken uint16_t cdr_size = 0; /* accumulated size of requests 2 through n */ 1486181643Skmacy 1487230587Sken xnb_pkt_initialize(pkt); 1488181643Skmacy 1489230587Sken /* Read the first request */ 1490230587Sken if (RING_HAS_UNCONSUMED_REQUESTS_2(tx_ring, idx)) { 1491230587Sken netif_tx_request_t *tx = RING_GET_REQUEST(tx_ring, idx); 1492230587Sken pkt->size = tx->size; 1493230587Sken pkt->flags = tx->flags & ~NETTXF_more_data; 1494230587Sken more_data = tx->flags & NETTXF_more_data; 1495230587Sken pkt->list_len++; 1496230587Sken pkt->car = idx; 1497230587Sken idx++; 1498230587Sken } 1499181643Skmacy 1500230587Sken /* Read the extra info */ 1501230587Sken if ((pkt->flags & NETTXF_extra_info) && 1502230587Sken RING_HAS_UNCONSUMED_REQUESTS_2(tx_ring, idx)) { 1503230587Sken netif_extra_info_t *ext = 1504230587Sken (netif_extra_info_t*) RING_GET_REQUEST(tx_ring, idx); 1505230587Sken pkt->extra.type = ext->type; 1506230587Sken switch (pkt->extra.type) { 1507230587Sken case XEN_NETIF_EXTRA_TYPE_GSO: 1508230587Sken pkt->extra.u.gso = ext->u.gso; 1509230587Sken break; 1510230587Sken default: 1511230587Sken /* 1512230587Sken * The reference Linux netfront driver will 1513230587Sken * never set any other extra.type. So we don't 1514230587Sken * know what to do with it. Let's print an 1515230587Sken * error, then consume and discard the packet 1516230587Sken */ 1517230587Sken printf("xnb(%s:%d): Unknown extra info type %d." 1518230587Sken " Discarding packet\n", 1519230587Sken __func__, __LINE__, pkt->extra.type); 1520230587Sken xnb_dump_txreq(start, RING_GET_REQUEST(tx_ring, 1521230587Sken start)); 1522230587Sken xnb_dump_txreq(idx, RING_GET_REQUEST(tx_ring, 1523230587Sken idx)); 1524230587Sken discard = 1; 1525230587Sken break; 1526230587Sken } 1527230587Sken 1528230587Sken pkt->extra.flags = ext->flags; 1529230587Sken if (ext->flags & XEN_NETIF_EXTRA_FLAG_MORE) { 1530181643Skmacy /* 1531230587Sken * The reference linux netfront driver never sets this 1532230587Sken * flag (nor does any other known netfront). So we 1533230587Sken * will discard the packet. 1534181643Skmacy */ 1535230587Sken printf("xnb(%s:%d): Request sets " 1536230587Sken "XEN_NETIF_EXTRA_FLAG_MORE, but we can't handle " 1537230587Sken "that\n", __func__, __LINE__); 1538230587Sken xnb_dump_txreq(start, RING_GET_REQUEST(tx_ring, start)); 1539230587Sken xnb_dump_txreq(idx, RING_GET_REQUEST(tx_ring, idx)); 1540230587Sken discard = 1; 1541181643Skmacy } 1542181643Skmacy 1543230587Sken idx++; 1544181643Skmacy } 1545181643Skmacy 1546230587Sken /* Set cdr. If there is not more data, cdr is invalid */ 1547230587Sken pkt->cdr = idx; 1548181643Skmacy 1549230587Sken /* Loop on remainder of packet */ 1550230587Sken while (more_data && RING_HAS_UNCONSUMED_REQUESTS_2(tx_ring, idx)) { 1551230587Sken netif_tx_request_t *tx = RING_GET_REQUEST(tx_ring, idx); 1552230587Sken pkt->list_len++; 1553230587Sken cdr_size += tx->size; 1554230587Sken if (tx->flags & ~NETTXF_more_data) { 1555230587Sken /* There should be no other flags set at this point */ 1556230587Sken printf("xnb(%s:%d): Request sets unknown flags %d " 1557230587Sken "after the 1st request in the packet.\n", 1558230587Sken __func__, __LINE__, tx->flags); 1559230587Sken xnb_dump_txreq(start, RING_GET_REQUEST(tx_ring, start)); 1560230587Sken xnb_dump_txreq(idx, RING_GET_REQUEST(tx_ring, idx)); 1561230587Sken } 1562181643Skmacy 1563230587Sken more_data = tx->flags & NETTXF_more_data; 1564230587Sken idx++; 1565230587Sken } 1566230587Sken 1567230587Sken /* Finalize packet */ 1568230587Sken if (more_data != 0) { 1569230587Sken /* The ring ran out of requests before finishing the packet */ 1570230587Sken xnb_pkt_invalidate(pkt); 1571230587Sken idx = start; /* tell caller that we consumed no requests */ 1572230587Sken } else { 1573230587Sken /* Calculate car_size */ 1574230587Sken pkt->car_size = pkt->size - cdr_size; 1575230587Sken } 1576230587Sken if (discard != 0) { 1577230587Sken xnb_pkt_invalidate(pkt); 1578230587Sken } 1579230587Sken 1580230587Sken return idx - start; 1581181643Skmacy} 1582181643Skmacy 1583230587Sken 1584230587Sken/** 1585230587Sken * Respond to all the requests that constituted pkt. Builds the responses and 1586230587Sken * writes them to the ring, but doesn't push them to the shared ring. 1587230587Sken * \param[in] pkt the packet that needs a response 1588230587Sken * \param[in] error true if there was an error handling the packet, such 1589230587Sken * as in the hypervisor copy op or mbuf allocation 1590230587Sken * \param[out] ring Responses go here 1591230587Sken */ 1592181643Skmacystatic void 1593230587Skenxnb_txpkt2rsp(const struct xnb_pkt *pkt, netif_tx_back_ring_t *ring, 1594230587Sken int error) 1595181643Skmacy{ 1596230587Sken /* 1597230587Sken * Outline: 1598230587Sken * 1) Respond to the first request 1599230587Sken * 2) Respond to the extra info reques 1600230587Sken * Loop through every remaining request in the packet, generating 1601230587Sken * responses that copy those requests' ids and sets the status 1602230587Sken * appropriately. 1603230587Sken */ 1604230587Sken netif_tx_request_t *tx; 1605230587Sken netif_tx_response_t *rsp; 1606230587Sken int i; 1607230587Sken uint16_t status; 1608181643Skmacy 1609230587Sken status = (xnb_pkt_is_valid(pkt) == 0) || error ? 1610230587Sken NETIF_RSP_ERROR : NETIF_RSP_OKAY; 1611230587Sken KASSERT((pkt->list_len == 0) || (ring->rsp_prod_pvt == pkt->car), 1612230587Sken ("Cannot respond to ring requests out of order")); 1613181643Skmacy 1614230587Sken if (pkt->list_len >= 1) { 1615230587Sken uint16_t id; 1616230587Sken tx = RING_GET_REQUEST(ring, ring->rsp_prod_pvt); 1617230587Sken id = tx->id; 1618230587Sken rsp = RING_GET_RESPONSE(ring, ring->rsp_prod_pvt); 1619230587Sken rsp->id = id; 1620230587Sken rsp->status = status; 1621230587Sken ring->rsp_prod_pvt++; 1622181643Skmacy 1623230587Sken if (pkt->flags & NETRXF_extra_info) { 1624230587Sken rsp = RING_GET_RESPONSE(ring, ring->rsp_prod_pvt); 1625230587Sken rsp->status = NETIF_RSP_NULL; 1626230587Sken ring->rsp_prod_pvt++; 1627181643Skmacy } 1628230587Sken } 1629181643Skmacy 1630230587Sken for (i=0; i < pkt->list_len - 1; i++) { 1631230587Sken uint16_t id; 1632230587Sken tx = RING_GET_REQUEST(ring, ring->rsp_prod_pvt); 1633230587Sken id = tx->id; 1634230587Sken rsp = RING_GET_RESPONSE(ring, ring->rsp_prod_pvt); 1635230587Sken rsp->id = id; 1636230587Sken rsp->status = status; 1637230587Sken ring->rsp_prod_pvt++; 1638181643Skmacy } 1639181643Skmacy} 1640181643Skmacy 1641230587Sken/** 1642230587Sken * Create an mbuf chain to represent a packet. Initializes all of the headers 1643230587Sken * in the mbuf chain, but does not copy the data. The returned chain must be 1644230587Sken * free()'d when no longer needed 1645230587Sken * \param[in] pkt A packet to model the mbuf chain after 1646230587Sken * \return A newly allocated mbuf chain, possibly with clusters attached. 1647230587Sken * NULL on failure 1648230587Sken */ 1649230587Skenstatic struct mbuf* 1650230587Skenxnb_pkt2mbufc(const struct xnb_pkt *pkt, struct ifnet *ifp) 1651181643Skmacy{ 1652230587Sken /** 1653230587Sken * \todo consider using a memory pool for mbufs instead of 1654230587Sken * reallocating them for every packet 1655230587Sken */ 1656230587Sken /** \todo handle extra data */ 1657230587Sken struct mbuf *m; 1658181643Skmacy 1659230587Sken m = m_getm(NULL, pkt->size, M_NOWAIT, MT_DATA); 1660181643Skmacy 1661230587Sken if (m != NULL) { 1662230587Sken m->m_pkthdr.rcvif = ifp; 1663230587Sken if (pkt->flags & NETTXF_data_validated) { 1664230587Sken /* 1665230587Sken * We lie to the host OS and always tell it that the 1666230587Sken * checksums are ok, because the packet is unlikely to 1667230587Sken * get corrupted going across domains. 1668230587Sken */ 1669230587Sken m->m_pkthdr.csum_flags = ( 1670230587Sken CSUM_IP_CHECKED | 1671230587Sken CSUM_IP_VALID | 1672230587Sken CSUM_DATA_VALID | 1673230587Sken CSUM_PSEUDO_HDR 1674230587Sken ); 1675230587Sken m->m_pkthdr.csum_data = 0xffff; 1676230587Sken } 1677230587Sken } 1678230587Sken return m; 1679181643Skmacy} 1680181643Skmacy 1681230587Sken/** 1682230587Sken * Build a gnttab_copy table that can be used to copy data from a pkt 1683230587Sken * to an mbufc. Does not actually perform the copy. Always uses gref's on 1684230587Sken * the packet side. 1685230587Sken * \param[in] pkt pkt's associated requests form the src for 1686230587Sken * the copy operation 1687230587Sken * \param[in] mbufc mbufc's storage forms the dest for the copy operation 1688230587Sken * \param[out] gnttab Storage for the returned grant table 1689230587Sken * \param[in] txb Pointer to the backend ring structure 1690230587Sken * \param[in] otherend_id The domain ID of the other end of the copy 1691230587Sken * \return The number of gnttab entries filled 1692230587Sken */ 1693181643Skmacystatic int 1694230587Skenxnb_txpkt2gnttab(const struct xnb_pkt *pkt, const struct mbuf *mbufc, 1695230587Sken gnttab_copy_table gnttab, const netif_tx_back_ring_t *txb, 1696230587Sken domid_t otherend_id) 1697181643Skmacy{ 1698181643Skmacy 1699230587Sken const struct mbuf *mbuf = mbufc;/* current mbuf within the chain */ 1700230587Sken int gnt_idx = 0; /* index into grant table */ 1701230587Sken RING_IDX r_idx = pkt->car; /* index into tx ring buffer */ 1702230587Sken int r_ofs = 0; /* offset of next data within tx request's data area */ 1703230587Sken int m_ofs = 0; /* offset of next data within mbuf's data area */ 1704230587Sken /* size in bytes that still needs to be represented in the table */ 1705230587Sken uint16_t size_remaining = pkt->size; 1706181643Skmacy 1707230587Sken while (size_remaining > 0) { 1708230587Sken const netif_tx_request_t *txq = RING_GET_REQUEST(txb, r_idx); 1709230587Sken const size_t mbuf_space = M_TRAILINGSPACE(mbuf) - m_ofs; 1710230587Sken const size_t req_size = 1711230587Sken r_idx == pkt->car ? pkt->car_size : txq->size; 1712230587Sken const size_t pkt_space = req_size - r_ofs; 1713230587Sken /* 1714230587Sken * space is the largest amount of data that can be copied in the 1715230587Sken * grant table's next entry 1716230587Sken */ 1717230587Sken const size_t space = MIN(pkt_space, mbuf_space); 1718230587Sken 1719230587Sken /* TODO: handle this error condition without panicking */ 1720230587Sken KASSERT(gnt_idx < GNTTAB_LEN, ("Grant table is too short")); 1721230587Sken 1722230587Sken gnttab[gnt_idx].source.u.ref = txq->gref; 1723230587Sken gnttab[gnt_idx].source.domid = otherend_id; 1724230587Sken gnttab[gnt_idx].source.offset = txq->offset + r_ofs; 1725230587Sken gnttab[gnt_idx].dest.u.gmfn = virt_to_mfn( 1726230587Sken mtod(mbuf, vm_offset_t) + m_ofs); 1727230587Sken gnttab[gnt_idx].dest.offset = virt_to_offset( 1728230587Sken mtod(mbuf, vm_offset_t) + m_ofs); 1729230587Sken gnttab[gnt_idx].dest.domid = DOMID_SELF; 1730230587Sken gnttab[gnt_idx].len = space; 1731230587Sken gnttab[gnt_idx].flags = GNTCOPY_source_gref; 1732230587Sken 1733230587Sken gnt_idx++; 1734230587Sken r_ofs += space; 1735230587Sken m_ofs += space; 1736230587Sken size_remaining -= space; 1737230587Sken if (req_size - r_ofs <= 0) { 1738230587Sken /* Must move to the next tx request */ 1739230587Sken r_ofs = 0; 1740230587Sken r_idx = (r_idx == pkt->car) ? pkt->cdr : r_idx + 1; 1741230587Sken } 1742230587Sken if (M_TRAILINGSPACE(mbuf) - m_ofs <= 0) { 1743230587Sken /* Must move to the next mbuf */ 1744230587Sken m_ofs = 0; 1745230587Sken mbuf = mbuf->m_next; 1746230587Sken } 1747181643Skmacy } 1748181643Skmacy 1749230587Sken return gnt_idx; 1750181643Skmacy} 1751181643Skmacy 1752230587Sken/** 1753230587Sken * Check the status of the grant copy operations, and update mbufs various 1754230587Sken * non-data fields to reflect the data present. 1755230587Sken * \param[in,out] mbufc mbuf chain to update. The chain must be valid and of 1756230587Sken * the correct length, and data should already be present 1757230587Sken * \param[in] gnttab A grant table for a just completed copy op 1758230587Sken * \param[in] n_entries The number of valid entries in the grant table 1759230587Sken */ 1760181643Skmacystatic void 1761230587Skenxnb_update_mbufc(struct mbuf *mbufc, const gnttab_copy_table gnttab, 1762230587Sken int n_entries) 1763181643Skmacy{ 1764230587Sken struct mbuf *mbuf = mbufc; 1765230587Sken int i; 1766230587Sken size_t total_size = 0; 1767181643Skmacy 1768230587Sken for (i = 0; i < n_entries; i++) { 1769230587Sken KASSERT(gnttab[i].status == GNTST_okay, 1770230587Sken ("Some gnttab_copy entry had error status %hd\n", 1771230587Sken gnttab[i].status)); 1772181643Skmacy 1773230587Sken mbuf->m_len += gnttab[i].len; 1774230587Sken total_size += gnttab[i].len; 1775230587Sken if (M_TRAILINGSPACE(mbuf) <= 0) { 1776230587Sken mbuf = mbuf->m_next; 1777230587Sken } 1778230587Sken } 1779230587Sken mbufc->m_pkthdr.len = total_size; 1780230587Sken 1781230587Sken xnb_add_mbuf_cksum(mbufc); 1782181643Skmacy} 1783181643Skmacy 1784230587Sken/** 1785230587Sken * Dequeue at most one packet from the shared ring 1786230587Sken * \param[in,out] txb Netif tx ring. A packet will be removed from it, and 1787230587Sken * its private indices will be updated. But the indices 1788230587Sken * will not be pushed to the shared ring. 1789230587Sken * \param[in] ifnet Interface to which the packet will be sent 1790230587Sken * \param[in] otherend Domain ID of the other end of the ring 1791230587Sken * \param[out] mbufc The assembled mbuf chain, ready to send to the generic 1792230587Sken * networking stack 1793230587Sken * \param[in,out] gnttab Pointer to enough memory for a grant table. We make 1794230587Sken * this a function parameter so that we will take less 1795230587Sken * stack space. 1796230587Sken * \return An error code 1797230587Sken */ 1798181643Skmacystatic int 1799230587Skenxnb_recv(netif_tx_back_ring_t *txb, domid_t otherend, struct mbuf **mbufc, 1800230587Sken struct ifnet *ifnet, gnttab_copy_table gnttab) 1801181643Skmacy{ 1802230587Sken struct xnb_pkt pkt; 1803230587Sken /* number of tx requests consumed to build the last packet */ 1804230587Sken int num_consumed; 1805230587Sken int nr_ents; 1806181643Skmacy 1807230587Sken *mbufc = NULL; 1808230587Sken num_consumed = xnb_ring2pkt(&pkt, txb, txb->req_cons); 1809230587Sken if (num_consumed == 0) 1810230587Sken return 0; /* Nothing to receive */ 1811181643Skmacy 1812249588Sgabor /* update statistics independent of errors */ 1813230587Sken ifnet->if_ipackets++; 1814181643Skmacy 1815230587Sken /* 1816230587Sken * if we got here, then 1 or more requests was consumed, but the packet 1817249583Sgabor * is not necessarily valid. 1818230587Sken */ 1819230587Sken if (xnb_pkt_is_valid(&pkt) == 0) { 1820230587Sken /* got a garbage packet, respond and drop it */ 1821230587Sken xnb_txpkt2rsp(&pkt, txb, 1); 1822230587Sken txb->req_cons += num_consumed; 1823230587Sken DPRINTF("xnb_intr: garbage packet, num_consumed=%d\n", 1824230587Sken num_consumed); 1825230587Sken ifnet->if_ierrors++; 1826230587Sken return EINVAL; 1827181643Skmacy } 1828181643Skmacy 1829230587Sken *mbufc = xnb_pkt2mbufc(&pkt, ifnet); 1830230587Sken 1831230587Sken if (*mbufc == NULL) { 1832230587Sken /* 1833230587Sken * Couldn't allocate mbufs. Respond and drop the packet. Do 1834230587Sken * not consume the requests 1835230587Sken */ 1836230587Sken xnb_txpkt2rsp(&pkt, txb, 1); 1837230587Sken DPRINTF("xnb_intr: Couldn't allocate mbufs, num_consumed=%d\n", 1838230587Sken num_consumed); 1839230587Sken ifnet->if_iqdrops++; 1840230587Sken return ENOMEM; 1841181643Skmacy } 1842181643Skmacy 1843230587Sken nr_ents = xnb_txpkt2gnttab(&pkt, *mbufc, gnttab, txb, otherend); 1844181643Skmacy 1845230587Sken if (nr_ents > 0) { 1846230587Sken int __unused hv_ret = HYPERVISOR_grant_table_op(GNTTABOP_copy, 1847230587Sken gnttab, nr_ents); 1848230587Sken KASSERT(hv_ret == 0, 1849230587Sken ("HYPERVISOR_grant_table_op returned %d\n", hv_ret)); 1850230587Sken xnb_update_mbufc(*mbufc, gnttab, nr_ents); 1851230587Sken } 1852181643Skmacy 1853230587Sken xnb_txpkt2rsp(&pkt, txb, 0); 1854230587Sken txb->req_cons += num_consumed; 1855181643Skmacy return 0; 1856181643Skmacy} 1857181643Skmacy 1858230587Sken/** 1859230587Sken * Create an xnb_pkt based on the contents of an mbuf chain. 1860230587Sken * \param[in] mbufc mbuf chain to transform into a packet 1861230587Sken * \param[out] pkt Storage for the newly generated xnb_pkt 1862230587Sken * \param[in] start The ring index of the first available slot in the rx 1863230587Sken * ring 1864230587Sken * \param[in] space The number of free slots in the rx ring 1865230587Sken * \retval 0 Success 1866230587Sken * \retval EINVAL mbufc was corrupt or not convertible into a pkt 1867230587Sken * \retval EAGAIN There was not enough space in the ring to queue the 1868230587Sken * packet 1869230587Sken */ 1870230587Skenstatic int 1871230587Skenxnb_mbufc2pkt(const struct mbuf *mbufc, struct xnb_pkt *pkt, 1872230587Sken RING_IDX start, int space) 1873181643Skmacy{ 1874181643Skmacy 1875230587Sken int retval = 0; 1876181643Skmacy 1877230587Sken if ((mbufc == NULL) || 1878230587Sken ( (mbufc->m_flags & M_PKTHDR) == 0) || 1879230587Sken (mbufc->m_pkthdr.len == 0)) { 1880230587Sken xnb_pkt_invalidate(pkt); 1881230587Sken retval = EINVAL; 1882230587Sken } else { 1883230587Sken int slots_required; 1884181643Skmacy 1885230587Sken xnb_pkt_validate(pkt); 1886230587Sken pkt->flags = 0; 1887230587Sken pkt->size = mbufc->m_pkthdr.len; 1888230587Sken pkt->car = start; 1889230587Sken pkt->car_size = mbufc->m_len; 1890181643Skmacy 1891230587Sken if (mbufc->m_pkthdr.csum_flags & CSUM_TSO) { 1892230587Sken pkt->flags |= NETRXF_extra_info; 1893230587Sken pkt->extra.u.gso.size = mbufc->m_pkthdr.tso_segsz; 1894230587Sken pkt->extra.u.gso.type = XEN_NETIF_GSO_TYPE_TCPV4; 1895230587Sken pkt->extra.u.gso.pad = 0; 1896230587Sken pkt->extra.u.gso.features = 0; 1897230587Sken pkt->extra.type = XEN_NETIF_EXTRA_TYPE_GSO; 1898230587Sken pkt->extra.flags = 0; 1899230587Sken pkt->cdr = start + 2; 1900230587Sken } else { 1901230587Sken pkt->cdr = start + 1; 1902230587Sken } 1903230587Sken if (mbufc->m_pkthdr.csum_flags & (CSUM_TSO | CSUM_DELAY_DATA)) { 1904230587Sken pkt->flags |= 1905230587Sken (NETRXF_csum_blank | NETRXF_data_validated); 1906230587Sken } 1907230587Sken 1908230587Sken /* 1909230587Sken * Each ring response can have up to PAGE_SIZE of data. 1910230587Sken * Assume that we can defragment the mbuf chain efficiently 1911230587Sken * into responses so that each response but the last uses all 1912230587Sken * PAGE_SIZE bytes. 1913230587Sken */ 1914230587Sken pkt->list_len = (pkt->size + PAGE_SIZE - 1) / PAGE_SIZE; 1915230587Sken 1916230587Sken if (pkt->list_len > 1) { 1917230587Sken pkt->flags |= NETRXF_more_data; 1918230587Sken } 1919230587Sken 1920230587Sken slots_required = pkt->list_len + 1921230587Sken (pkt->flags & NETRXF_extra_info ? 1 : 0); 1922230587Sken if (slots_required > space) { 1923230587Sken xnb_pkt_invalidate(pkt); 1924230587Sken retval = EAGAIN; 1925230587Sken } 1926181643Skmacy } 1927230587Sken 1928230587Sken return retval; 1929181643Skmacy} 1930181643Skmacy 1931230587Sken/** 1932230587Sken * Build a gnttab_copy table that can be used to copy data from an mbuf chain 1933230587Sken * to the frontend's shared buffers. Does not actually perform the copy. 1934230587Sken * Always uses gref's on the other end's side. 1935230587Sken * \param[in] pkt pkt's associated responses form the dest for the copy 1936230587Sken * operatoin 1937230587Sken * \param[in] mbufc The source for the copy operation 1938230587Sken * \param[out] gnttab Storage for the returned grant table 1939230587Sken * \param[in] rxb Pointer to the backend ring structure 1940230587Sken * \param[in] otherend_id The domain ID of the other end of the copy 1941230587Sken * \return The number of gnttab entries filled 1942230587Sken */ 1943181643Skmacystatic int 1944230587Skenxnb_rxpkt2gnttab(const struct xnb_pkt *pkt, const struct mbuf *mbufc, 1945230587Sken gnttab_copy_table gnttab, const netif_rx_back_ring_t *rxb, 1946230587Sken domid_t otherend_id) 1947181643Skmacy{ 1948181643Skmacy 1949230587Sken const struct mbuf *mbuf = mbufc;/* current mbuf within the chain */ 1950230587Sken int gnt_idx = 0; /* index into grant table */ 1951230587Sken RING_IDX r_idx = pkt->car; /* index into rx ring buffer */ 1952230587Sken int r_ofs = 0; /* offset of next data within rx request's data area */ 1953230587Sken int m_ofs = 0; /* offset of next data within mbuf's data area */ 1954230587Sken /* size in bytes that still needs to be represented in the table */ 1955230587Sken uint16_t size_remaining; 1956181643Skmacy 1957230587Sken size_remaining = (xnb_pkt_is_valid(pkt) != 0) ? pkt->size : 0; 1958230587Sken 1959230587Sken while (size_remaining > 0) { 1960230587Sken const netif_rx_request_t *rxq = RING_GET_REQUEST(rxb, r_idx); 1961230587Sken const size_t mbuf_space = mbuf->m_len - m_ofs; 1962230587Sken /* Xen shared pages have an implied size of PAGE_SIZE */ 1963230587Sken const size_t req_size = PAGE_SIZE; 1964230587Sken const size_t pkt_space = req_size - r_ofs; 1965230587Sken /* 1966230587Sken * space is the largest amount of data that can be copied in the 1967230587Sken * grant table's next entry 1968230587Sken */ 1969230587Sken const size_t space = MIN(pkt_space, mbuf_space); 1970230587Sken 1971230587Sken /* TODO: handle this error condition without panicing */ 1972230587Sken KASSERT(gnt_idx < GNTTAB_LEN, ("Grant table is too short")); 1973230587Sken 1974230587Sken gnttab[gnt_idx].dest.u.ref = rxq->gref; 1975230587Sken gnttab[gnt_idx].dest.domid = otherend_id; 1976230587Sken gnttab[gnt_idx].dest.offset = r_ofs; 1977230587Sken gnttab[gnt_idx].source.u.gmfn = virt_to_mfn( 1978230587Sken mtod(mbuf, vm_offset_t) + m_ofs); 1979230587Sken gnttab[gnt_idx].source.offset = virt_to_offset( 1980230587Sken mtod(mbuf, vm_offset_t) + m_ofs); 1981230587Sken gnttab[gnt_idx].source.domid = DOMID_SELF; 1982230587Sken gnttab[gnt_idx].len = space; 1983230587Sken gnttab[gnt_idx].flags = GNTCOPY_dest_gref; 1984230587Sken 1985230587Sken gnt_idx++; 1986230587Sken 1987230587Sken r_ofs += space; 1988230587Sken m_ofs += space; 1989230587Sken size_remaining -= space; 1990230587Sken if (req_size - r_ofs <= 0) { 1991230587Sken /* Must move to the next rx request */ 1992230587Sken r_ofs = 0; 1993230587Sken r_idx = (r_idx == pkt->car) ? pkt->cdr : r_idx + 1; 1994230587Sken } 1995230587Sken if (mbuf->m_len - m_ofs <= 0) { 1996230587Sken /* Must move to the next mbuf */ 1997230587Sken m_ofs = 0; 1998230587Sken mbuf = mbuf->m_next; 1999230587Sken } 2000181643Skmacy } 2001181643Skmacy 2002230587Sken return gnt_idx; 2003181643Skmacy} 2004181643Skmacy 2005181643Skmacy/** 2006230587Sken * Generates responses for all the requests that constituted pkt. Builds 2007230587Sken * responses and writes them to the ring, but doesn't push the shared ring 2008230587Sken * indices. 2009230587Sken * \param[in] pkt the packet that needs a response 2010230587Sken * \param[in] gnttab The grant copy table corresponding to this packet. 2011230587Sken * Used to determine how many rsp->netif_rx_response_t's to 2012230587Sken * generate. 2013230587Sken * \param[in] n_entries Number of relevant entries in the grant table 2014230587Sken * \param[out] ring Responses go here 2015230587Sken * \return The number of RX requests that were consumed to generate 2016230587Sken * the responses 2017181643Skmacy */ 2018181643Skmacystatic int 2019230587Skenxnb_rxpkt2rsp(const struct xnb_pkt *pkt, const gnttab_copy_table gnttab, 2020230587Sken int n_entries, netif_rx_back_ring_t *ring) 2021181643Skmacy{ 2022230587Sken /* 2023230587Sken * This code makes the following assumptions: 2024230587Sken * * All entries in gnttab set GNTCOPY_dest_gref 2025230587Sken * * The entries in gnttab are grouped by their grefs: any two 2026230587Sken * entries with the same gref must be adjacent 2027230587Sken */ 2028230587Sken int error = 0; 2029230587Sken int gnt_idx, i; 2030230587Sken int n_responses = 0; 2031230587Sken grant_ref_t last_gref = GRANT_REF_INVALID; 2032230587Sken RING_IDX r_idx; 2033181643Skmacy 2034230587Sken KASSERT(gnttab != NULL, ("Received a null granttable copy")); 2035230587Sken 2036230587Sken /* 2037230587Sken * In the event of an error, we only need to send one response to the 2038230587Sken * netfront. In that case, we musn't write any data to the responses 2039230587Sken * after the one we send. So we must loop all the way through gnttab 2040230587Sken * looking for errors before we generate any responses 2041230587Sken * 2042230587Sken * Since we're looping through the grant table anyway, we'll count the 2043230587Sken * number of different gref's in it, which will tell us how many 2044230587Sken * responses to generate 2045230587Sken */ 2046230587Sken for (gnt_idx = 0; gnt_idx < n_entries; gnt_idx++) { 2047230587Sken int16_t status = gnttab[gnt_idx].status; 2048230587Sken if (status != GNTST_okay) { 2049230587Sken DPRINTF( 2050230587Sken "Got error %d for hypervisor gnttab_copy status\n", 2051230587Sken status); 2052230587Sken error = 1; 2053230587Sken break; 2054230587Sken } 2055230587Sken if (gnttab[gnt_idx].dest.u.ref != last_gref) { 2056230587Sken n_responses++; 2057230587Sken last_gref = gnttab[gnt_idx].dest.u.ref; 2058230587Sken } 2059181643Skmacy } 2060181643Skmacy 2061230587Sken if (error != 0) { 2062230587Sken uint16_t id; 2063230587Sken netif_rx_response_t *rsp; 2064230587Sken 2065230587Sken id = RING_GET_REQUEST(ring, ring->rsp_prod_pvt)->id; 2066230587Sken rsp = RING_GET_RESPONSE(ring, ring->rsp_prod_pvt); 2067230587Sken rsp->id = id; 2068230587Sken rsp->status = NETIF_RSP_ERROR; 2069230587Sken n_responses = 1; 2070230587Sken } else { 2071230587Sken gnt_idx = 0; 2072230587Sken const int has_extra = pkt->flags & NETRXF_extra_info; 2073230587Sken if (has_extra != 0) 2074230587Sken n_responses++; 2075181643Skmacy 2076230587Sken for (i = 0; i < n_responses; i++) { 2077230587Sken netif_rx_request_t rxq; 2078230587Sken netif_rx_response_t *rsp; 2079181643Skmacy 2080230587Sken r_idx = ring->rsp_prod_pvt + i; 2081230587Sken /* 2082230587Sken * We copy the structure of rxq instead of making a 2083230587Sken * pointer because it shares the same memory as rsp. 2084230587Sken */ 2085230587Sken rxq = *(RING_GET_REQUEST(ring, r_idx)); 2086230587Sken rsp = RING_GET_RESPONSE(ring, r_idx); 2087230587Sken if (has_extra && (i == 1)) { 2088230587Sken netif_extra_info_t *ext = 2089230587Sken (netif_extra_info_t*)rsp; 2090230587Sken ext->type = XEN_NETIF_EXTRA_TYPE_GSO; 2091230587Sken ext->flags = 0; 2092230587Sken ext->u.gso.size = pkt->extra.u.gso.size; 2093230587Sken ext->u.gso.type = XEN_NETIF_GSO_TYPE_TCPV4; 2094230587Sken ext->u.gso.pad = 0; 2095230587Sken ext->u.gso.features = 0; 2096230587Sken } else { 2097230587Sken rsp->id = rxq.id; 2098230587Sken rsp->status = GNTST_okay; 2099230587Sken rsp->offset = 0; 2100230587Sken rsp->flags = 0; 2101230587Sken if (i < pkt->list_len - 1) 2102230587Sken rsp->flags |= NETRXF_more_data; 2103230587Sken if ((i == 0) && has_extra) 2104230587Sken rsp->flags |= NETRXF_extra_info; 2105230587Sken if ((i == 0) && 2106230587Sken (pkt->flags & NETRXF_data_validated)) { 2107230587Sken rsp->flags |= NETRXF_data_validated; 2108230587Sken rsp->flags |= NETRXF_csum_blank; 2109230587Sken } 2110230587Sken rsp->status = 0; 2111230587Sken for (; gnttab[gnt_idx].dest.u.ref == rxq.gref; 2112230587Sken gnt_idx++) { 2113230587Sken rsp->status += gnttab[gnt_idx].len; 2114230587Sken } 2115230587Sken } 2116230587Sken } 2117181643Skmacy } 2118181643Skmacy 2119230587Sken ring->req_cons += n_responses; 2120230587Sken ring->rsp_prod_pvt += n_responses; 2121230587Sken return n_responses; 2122181643Skmacy} 2123181643Skmacy 2124181643Skmacy/** 2125230587Sken * Add IP, TCP, and/or UDP checksums to every mbuf in a chain. The first mbuf 2126230587Sken * in the chain must start with a struct ether_header. 2127230587Sken * 2128230587Sken * XXX This function will perform incorrectly on UDP packets that are split up 2129230587Sken * into multiple ethernet frames. 2130181643Skmacy */ 2131230587Skenstatic void 2132230587Skenxnb_add_mbuf_cksum(struct mbuf *mbufc) 2133181643Skmacy{ 2134230587Sken struct ether_header *eh; 2135230587Sken struct ip *iph; 2136230587Sken uint16_t ether_type; 2137181643Skmacy 2138230587Sken eh = mtod(mbufc, struct ether_header*); 2139230587Sken ether_type = ntohs(eh->ether_type); 2140230587Sken if (ether_type != ETHERTYPE_IP) { 2141230587Sken /* Nothing to calculate */ 2142230587Sken return; 2143230587Sken } 2144181643Skmacy 2145230587Sken iph = (struct ip*)(eh + 1); 2146230587Sken if (mbufc->m_pkthdr.csum_flags & CSUM_IP_VALID) { 2147230587Sken iph->ip_sum = 0; 2148230587Sken iph->ip_sum = in_cksum_hdr(iph); 2149230587Sken } 2150181643Skmacy 2151230587Sken switch (iph->ip_p) { 2152230587Sken case IPPROTO_TCP: 2153230587Sken if (mbufc->m_pkthdr.csum_flags & CSUM_IP_VALID) { 2154230587Sken size_t tcplen = ntohs(iph->ip_len) - sizeof(struct ip); 2155230587Sken struct tcphdr *th = (struct tcphdr*)(iph + 1); 2156230587Sken th->th_sum = in_pseudo(iph->ip_src.s_addr, 2157230587Sken iph->ip_dst.s_addr, htons(IPPROTO_TCP + tcplen)); 2158230587Sken th->th_sum = in_cksum_skip(mbufc, 2159230587Sken sizeof(struct ether_header) + ntohs(iph->ip_len), 2160230587Sken sizeof(struct ether_header) + (iph->ip_hl << 2)); 2161230587Sken } 2162181643Skmacy break; 2163230587Sken case IPPROTO_UDP: 2164230587Sken if (mbufc->m_pkthdr.csum_flags & CSUM_IP_VALID) { 2165230587Sken size_t udplen = ntohs(iph->ip_len) - sizeof(struct ip); 2166230587Sken struct udphdr *uh = (struct udphdr*)(iph + 1); 2167230587Sken uh->uh_sum = in_pseudo(iph->ip_src.s_addr, 2168230587Sken iph->ip_dst.s_addr, htons(IPPROTO_UDP + udplen)); 2169230587Sken uh->uh_sum = in_cksum_skip(mbufc, 2170230587Sken sizeof(struct ether_header) + ntohs(iph->ip_len), 2171230587Sken sizeof(struct ether_header) + (iph->ip_hl << 2)); 2172230587Sken } 2173181643Skmacy break; 2174230587Sken default: 2175181643Skmacy break; 2176181643Skmacy } 2177181643Skmacy} 2178181643Skmacy 2179181643Skmacystatic void 2180230587Skenxnb_stop(struct xnb_softc *xnb) 2181181643Skmacy{ 2182230587Sken struct ifnet *ifp; 2183181643Skmacy 2184230587Sken mtx_assert(&xnb->sc_lock, MA_OWNED); 2185230587Sken ifp = xnb->xnb_ifp; 2186230587Sken ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE); 2187230587Sken if_link_state_change(ifp, LINK_STATE_DOWN); 2188181643Skmacy} 2189181643Skmacy 2190181643Skmacystatic int 2191230587Skenxnb_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data) 2192181643Skmacy{ 2193230587Sken struct xnb_softc *xnb = ifp->if_softc; 2194230587Sken#ifdef INET 2195230587Sken struct ifreq *ifr = (struct ifreq*) data; 2196230587Sken struct ifaddr *ifa = (struct ifaddr*)data; 2197230587Sken#endif 2198230587Sken int error = 0; 2199181643Skmacy 2200230587Sken switch (cmd) { 2201230587Sken case SIOCSIFFLAGS: 2202230587Sken mtx_lock(&xnb->sc_lock); 2203230587Sken if (ifp->if_flags & IFF_UP) { 2204230587Sken xnb_ifinit_locked(xnb); 2205230587Sken } else { 2206230587Sken if (ifp->if_drv_flags & IFF_DRV_RUNNING) { 2207230587Sken xnb_stop(xnb); 2208230587Sken } 2209230587Sken } 2210230587Sken /* 2211230587Sken * Note: netfront sets a variable named xn_if_flags 2212230587Sken * here, but that variable is never read 2213230587Sken */ 2214230587Sken mtx_unlock(&xnb->sc_lock); 2215230587Sken break; 2216230587Sken case SIOCSIFADDR: 2217230587Sken case SIOCGIFADDR: 2218230587Sken#ifdef INET 2219230587Sken mtx_lock(&xnb->sc_lock); 2220230587Sken if (ifa->ifa_addr->sa_family == AF_INET) { 2221230587Sken ifp->if_flags |= IFF_UP; 2222230587Sken if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) { 2223230587Sken ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | 2224230587Sken IFF_DRV_OACTIVE); 2225230587Sken if_link_state_change(ifp, 2226230587Sken LINK_STATE_DOWN); 2227230587Sken ifp->if_drv_flags |= IFF_DRV_RUNNING; 2228230587Sken ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 2229230587Sken if_link_state_change(ifp, 2230230587Sken LINK_STATE_UP); 2231230587Sken } 2232230587Sken arp_ifinit(ifp, ifa); 2233230587Sken mtx_unlock(&xnb->sc_lock); 2234230587Sken } else { 2235230587Sken mtx_unlock(&xnb->sc_lock); 2236230587Sken#endif 2237230587Sken error = ether_ioctl(ifp, cmd, data); 2238230587Sken#ifdef INET 2239230587Sken } 2240230587Sken#endif 2241230587Sken break; 2242230587Sken case SIOCSIFCAP: 2243230587Sken mtx_lock(&xnb->sc_lock); 2244230587Sken if (ifr->ifr_reqcap & IFCAP_TXCSUM) { 2245230587Sken ifp->if_capenable |= IFCAP_TXCSUM; 2246230587Sken ifp->if_hwassist |= XNB_CSUM_FEATURES; 2247230587Sken } else { 2248230587Sken ifp->if_capenable &= ~(IFCAP_TXCSUM); 2249230587Sken ifp->if_hwassist &= ~(XNB_CSUM_FEATURES); 2250230587Sken } 2251230587Sken if ((ifr->ifr_reqcap & IFCAP_RXCSUM)) { 2252230587Sken ifp->if_capenable |= IFCAP_RXCSUM; 2253230587Sken } else { 2254230587Sken ifp->if_capenable &= ~(IFCAP_RXCSUM); 2255230587Sken } 2256230587Sken /* 2257230587Sken * TODO enable TSO4 and LRO once we no longer need 2258230587Sken * to calculate checksums in software 2259230587Sken */ 2260230587Sken#if 0 2261230587Sken if (ifr->if_reqcap |= IFCAP_TSO4) { 2262230587Sken if (IFCAP_TXCSUM & ifp->if_capenable) { 2263230587Sken printf("xnb: Xen netif requires that " 2264230587Sken "TXCSUM be enabled in order " 2265230587Sken "to use TSO4\n"); 2266230587Sken error = EINVAL; 2267230587Sken } else { 2268230587Sken ifp->if_capenable |= IFCAP_TSO4; 2269230587Sken ifp->if_hwassist |= CSUM_TSO; 2270230587Sken } 2271230587Sken } else { 2272230587Sken ifp->if_capenable &= ~(IFCAP_TSO4); 2273230587Sken ifp->if_hwassist &= ~(CSUM_TSO); 2274230587Sken } 2275230587Sken if (ifr->ifreqcap |= IFCAP_LRO) { 2276230587Sken ifp->if_capenable |= IFCAP_LRO; 2277230587Sken } else { 2278230587Sken ifp->if_capenable &= ~(IFCAP_LRO); 2279230587Sken } 2280230587Sken#endif 2281230587Sken mtx_unlock(&xnb->sc_lock); 2282230587Sken break; 2283230587Sken case SIOCSIFMTU: 2284230587Sken ifp->if_mtu = ifr->ifr_mtu; 2285230587Sken ifp->if_drv_flags &= ~IFF_DRV_RUNNING; 2286230587Sken xnb_ifinit(xnb); 2287230587Sken break; 2288230587Sken case SIOCADDMULTI: 2289230587Sken case SIOCDELMULTI: 2290230587Sken case SIOCSIFMEDIA: 2291230587Sken case SIOCGIFMEDIA: 2292230587Sken error = ifmedia_ioctl(ifp, ifr, &xnb->sc_media, cmd); 2293230587Sken break; 2294230587Sken default: 2295230587Sken error = ether_ioctl(ifp, cmd, data); 2296230587Sken break; 2297181643Skmacy } 2298230587Sken return (error); 2299230587Sken} 2300181643Skmacy 2301230587Skenstatic void 2302230587Skenxnb_start_locked(struct ifnet *ifp) 2303230587Sken{ 2304230587Sken netif_rx_back_ring_t *rxb; 2305230587Sken struct xnb_softc *xnb; 2306230587Sken struct mbuf *mbufc; 2307230587Sken RING_IDX req_prod_local; 2308181643Skmacy 2309230587Sken xnb = ifp->if_softc; 2310230587Sken rxb = &xnb->ring_configs[XNB_RING_TYPE_RX].back_ring.rx_ring; 2311181643Skmacy 2312230587Sken if (!xnb->carrier) 2313230587Sken return; 2314181643Skmacy 2315230587Sken do { 2316230587Sken int out_of_space = 0; 2317230587Sken int notify; 2318230587Sken req_prod_local = rxb->sring->req_prod; 2319230587Sken xen_rmb(); 2320230587Sken for (;;) { 2321230587Sken int error; 2322181643Skmacy 2323230587Sken IF_DEQUEUE(&ifp->if_snd, mbufc); 2324230587Sken if (mbufc == NULL) 2325230587Sken break; 2326230587Sken error = xnb_send(rxb, xnb->otherend_id, mbufc, 2327230587Sken xnb->rx_gnttab); 2328230587Sken switch (error) { 2329230587Sken case EAGAIN: 2330230587Sken /* 2331230587Sken * Insufficient space in the ring. 2332230587Sken * Requeue pkt and send when space is 2333230587Sken * available. 2334230587Sken */ 2335230587Sken IF_PREPEND(&ifp->if_snd, mbufc); 2336230587Sken /* 2337230587Sken * Perhaps the frontend missed an IRQ 2338230587Sken * and went to sleep. Notify it to wake 2339230587Sken * it up. 2340230587Sken */ 2341230587Sken out_of_space = 1; 2342230587Sken break; 2343181643Skmacy 2344230587Sken case EINVAL: 2345230587Sken /* OS gave a corrupt packet. Drop it.*/ 2346230587Sken ifp->if_oerrors++; 2347230587Sken /* FALLTHROUGH */ 2348230587Sken default: 2349230587Sken /* Send succeeded, or packet had error. 2350230587Sken * Free the packet */ 2351230587Sken ifp->if_opackets++; 2352230587Sken if (mbufc) 2353230587Sken m_freem(mbufc); 2354230587Sken break; 2355230587Sken } 2356230587Sken if (out_of_space != 0) 2357230587Sken break; 2358230587Sken } 2359181643Skmacy 2360230587Sken RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(rxb, notify); 2361230587Sken if ((notify != 0) || (out_of_space != 0)) 2362255040Sgibbs xen_intr_signal(xnb->xen_intr_handle); 2363230587Sken rxb->sring->req_event = req_prod_local + 1; 2364230587Sken xen_mb(); 2365230587Sken } while (rxb->sring->req_prod != req_prod_local) ; 2366181643Skmacy} 2367181643Skmacy 2368230587Sken/** 2369230587Sken * Sends one packet to the ring. Blocks until the packet is on the ring 2370230587Sken * \param[in] mbufc Contains one packet to send. Caller must free 2371230587Sken * \param[in,out] rxb The packet will be pushed onto this ring, but the 2372230587Sken * otherend will not be notified. 2373230587Sken * \param[in] otherend The domain ID of the other end of the connection 2374230587Sken * \retval EAGAIN The ring did not have enough space for the packet. 2375230587Sken * The ring has not been modified 2376230587Sken * \param[in,out] gnttab Pointer to enough memory for a grant table. We make 2377230587Sken * this a function parameter so that we will take less 2378230587Sken * stack space. 2379230587Sken * \retval EINVAL mbufc was corrupt or not convertible into a pkt 2380230587Sken */ 2381181643Skmacystatic int 2382230587Skenxnb_send(netif_rx_back_ring_t *ring, domid_t otherend, const struct mbuf *mbufc, 2383230587Sken gnttab_copy_table gnttab) 2384181643Skmacy{ 2385230587Sken struct xnb_pkt pkt; 2386230587Sken int error, n_entries, n_reqs; 2387230587Sken RING_IDX space; 2388181643Skmacy 2389230587Sken space = ring->sring->req_prod - ring->req_cons; 2390230587Sken error = xnb_mbufc2pkt(mbufc, &pkt, ring->rsp_prod_pvt, space); 2391230587Sken if (error != 0) 2392230587Sken return error; 2393230587Sken n_entries = xnb_rxpkt2gnttab(&pkt, mbufc, gnttab, ring, otherend); 2394230587Sken if (n_entries != 0) { 2395230587Sken int __unused hv_ret = HYPERVISOR_grant_table_op(GNTTABOP_copy, 2396230587Sken gnttab, n_entries); 2397230587Sken KASSERT(hv_ret == 0, ("HYPERVISOR_grant_table_op returned %d\n", 2398230587Sken hv_ret)); 2399181643Skmacy } 2400181643Skmacy 2401230587Sken n_reqs = xnb_rxpkt2rsp(&pkt, gnttab, n_entries, ring); 2402181643Skmacy 2403230587Sken return 0; 2404181643Skmacy} 2405181643Skmacy 2406230587Skenstatic void 2407230587Skenxnb_start(struct ifnet *ifp) 2408181643Skmacy{ 2409230587Sken struct xnb_softc *xnb; 2410230587Sken 2411230587Sken xnb = ifp->if_softc; 2412230587Sken mtx_lock(&xnb->rx_lock); 2413230587Sken xnb_start_locked(ifp); 2414230587Sken mtx_unlock(&xnb->rx_lock); 2415181643Skmacy} 2416181643Skmacy 2417230587Sken/* equivalent of network_open() in Linux */ 2418230587Skenstatic void 2419230587Skenxnb_ifinit_locked(struct xnb_softc *xnb) 2420181643Skmacy{ 2421230587Sken struct ifnet *ifp; 2422181643Skmacy 2423230587Sken ifp = xnb->xnb_ifp; 2424181643Skmacy 2425230587Sken mtx_assert(&xnb->sc_lock, MA_OWNED); 2426181643Skmacy 2427230587Sken if (ifp->if_drv_flags & IFF_DRV_RUNNING) 2428230587Sken return; 2429181643Skmacy 2430230587Sken xnb_stop(xnb); 2431181643Skmacy 2432230587Sken ifp->if_drv_flags |= IFF_DRV_RUNNING; 2433230587Sken ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 2434230587Sken if_link_state_change(ifp, LINK_STATE_UP); 2435181643Skmacy} 2436181643Skmacy 2437230587Sken 2438230587Skenstatic void 2439230587Skenxnb_ifinit(void *xsc) 2440181643Skmacy{ 2441230587Sken struct xnb_softc *xnb = xsc; 2442181643Skmacy 2443230587Sken mtx_lock(&xnb->sc_lock); 2444230587Sken xnb_ifinit_locked(xnb); 2445230587Sken mtx_unlock(&xnb->sc_lock); 2446230587Sken} 2447181643Skmacy 2448181643Skmacy 2449230587Sken/** 2450230587Sken * Read the 'mac' node at the given device's node in the store, and parse that 2451230587Sken * as colon-separated octets, placing result the given mac array. mac must be 2452230587Sken * a preallocated array of length ETHER_ADDR_LEN ETH_ALEN (as declared in 2453230587Sken * net/ethernet.h). 2454230587Sken * Return 0 on success, or errno on error. 2455230587Sken */ 2456230587Skenstatic int 2457230587Skenxen_net_read_mac(device_t dev, uint8_t mac[]) 2458230587Sken{ 2459230587Sken char *s, *e, *macstr; 2460230587Sken const char *path; 2461230587Sken int error = 0; 2462230587Sken int i; 2463181643Skmacy 2464230587Sken path = xenbus_get_node(dev); 2465230587Sken error = xs_read(XST_NIL, path, "mac", NULL, (void **) &macstr); 2466230587Sken if (error != 0) { 2467230587Sken xenbus_dev_fatal(dev, error, "parsing %s/mac", path); 2468230587Sken } else { 2469230587Sken s = macstr; 2470230587Sken for (i = 0; i < ETHER_ADDR_LEN; i++) { 2471230587Sken mac[i] = strtoul(s, &e, 16); 2472230587Sken if (s == e || (e[0] != ':' && e[0] != 0)) { 2473230587Sken error = ENOENT; 2474230587Sken break; 2475230587Sken } 2476230587Sken s = &e[1]; 2477230587Sken } 2478230587Sken free(macstr, M_XENBUS); 2479230587Sken } 2480230587Sken return error; 2481230587Sken} 2482181643Skmacy 2483181643Skmacy 2484230587Sken/** 2485230587Sken * Callback used by the generic networking code to tell us when our carrier 2486230587Sken * state has changed. Since we don't have a physical carrier, we don't care 2487230587Sken */ 2488230587Skenstatic int 2489230587Skenxnb_ifmedia_upd(struct ifnet *ifp) 2490230587Sken{ 2491230587Sken return (0); 2492230587Sken} 2493181643Skmacy 2494230587Sken/** 2495230587Sken * Callback used by the generic networking code to ask us what our carrier 2496230587Sken * state is. Since we don't have a physical carrier, this is very simple 2497230587Sken */ 2498230587Skenstatic void 2499230587Skenxnb_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr) 2500230587Sken{ 2501230587Sken ifmr->ifm_status = IFM_AVALID|IFM_ACTIVE; 2502230587Sken ifmr->ifm_active = IFM_ETHER|IFM_MANUAL; 2503181643Skmacy} 2504181643Skmacy 2505230587Sken 2506230587Sken/*---------------------------- NewBus Registration ---------------------------*/ 2507230587Skenstatic device_method_t xnb_methods[] = { 2508181643Skmacy /* Device interface */ 2509230587Sken DEVMETHOD(device_probe, xnb_probe), 2510230587Sken DEVMETHOD(device_attach, xnb_attach), 2511230587Sken DEVMETHOD(device_detach, xnb_detach), 2512181643Skmacy DEVMETHOD(device_shutdown, bus_generic_shutdown), 2513230587Sken DEVMETHOD(device_suspend, xnb_suspend), 2514230587Sken DEVMETHOD(device_resume, xnb_resume), 2515181643Skmacy 2516230587Sken /* Xenbus interface */ 2517230587Sken DEVMETHOD(xenbus_otherend_changed, xnb_frontend_changed), 2518181643Skmacy 2519230587Sken { 0, 0 } 2520181643Skmacy}; 2521181643Skmacy 2522230587Skenstatic driver_t xnb_driver = { 2523230587Sken "xnb", 2524230587Sken xnb_methods, 2525230587Sken sizeof(struct xnb_softc), 2526230587Sken}; 2527230587Skendevclass_t xnb_devclass; 2528181643Skmacy 2529230587SkenDRIVER_MODULE(xnb, xenbusb_back, xnb_driver, xnb_devclass, 0, 0); 2530181643Skmacy 2531230587Sken 2532230587Sken/*-------------------------- Unit Tests -------------------------------------*/ 2533230587Sken#ifdef XNB_DEBUG 2534230587Sken#include "netback_unit_tests.c" 2535230587Sken#endif 2536