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: stable/11/sys/dev/xen/netback/netback.c 346817 2019-04-28 13:21:01Z dchagin $"); 37230587Sken 38230587Sken/** 39230587Sken * \file netback.c 40230587Sken * 41230587Sken * \brief Device driver supporting the vending of network access 42230587Sken * from this FreeBSD domain to other domains. 43230587Sken */ 44230587Sken#include "opt_inet.h" 45257515Sglebius#include "opt_inet6.h" 46230587Sken 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> 60257275Sglebius#include <net/if_var.h> 61181643Skmacy#include <net/if_arp.h> 62230587Sken#include <net/ethernet.h> 63230587Sken#include <net/if_dl.h> 64230587Sken#include <net/if_media.h> 65181643Skmacy#include <net/if_types.h> 66181643Skmacy 67181643Skmacy#include <netinet/in.h> 68181643Skmacy#include <netinet/ip.h> 69230587Sken#include <netinet/if_ether.h> 70230587Sken#if __FreeBSD_version >= 700000 71181643Skmacy#include <netinet/tcp.h> 72230587Sken#endif 73230587Sken#include <netinet/ip_icmp.h> 74181643Skmacy#include <netinet/udp.h> 75230587Sken#include <machine/in_cksum.h> 76181643Skmacy 77230587Sken#include <vm/vm.h> 78230587Sken#include <vm/pmap.h> 79230916Sken#include <vm/vm_extern.h> 80230916Sken#include <vm/vm_kern.h> 81181643Skmacy 82230587Sken#include <machine/_inttypes.h> 83181643Skmacy 84255040Sgibbs#include <xen/xen-os.h> 85255040Sgibbs#include <xen/hypervisor.h> 86230587Sken#include <xen/xen_intr.h> 87230587Sken#include <xen/interface/io/netif.h> 88230587Sken#include <xen/xenbus/xenbusvar.h> 89181643Skmacy 90230587Sken/*--------------------------- Compile-time Tunables --------------------------*/ 91181643Skmacy 92230587Sken/*---------------------------------- Macros ----------------------------------*/ 93230587Sken/** 94230587Sken * Custom malloc type for all driver allocations. 95230587Sken */ 96230587Skenstatic MALLOC_DEFINE(M_XENNETBACK, "xnb", "Xen Net Back Driver Data"); 97230587Sken 98230587Sken#define XNB_SG 1 /* netback driver supports feature-sg */ 99279394Sroyger#define XNB_GSO_TCPV4 0 /* netback driver supports feature-gso-tcpv4 */ 100230587Sken#define XNB_RX_COPY 1 /* netback driver supports feature-rx-copy */ 101230587Sken#define XNB_RX_FLIP 0 /* netback driver does not support feature-rx-flip */ 102230587Sken 103230587Sken#undef XNB_DEBUG 104230587Sken#define XNB_DEBUG /* hardcode on during development */ 105230587Sken 106230587Sken#ifdef XNB_DEBUG 107230587Sken#define DPRINTF(fmt, args...) \ 108230587Sken printf("xnb(%s:%d): " fmt, __FUNCTION__, __LINE__, ##args) 109181643Skmacy#else 110230587Sken#define DPRINTF(fmt, args...) do {} while (0) 111181643Skmacy#endif 112181643Skmacy 113230587Sken/* Default length for stack-allocated grant tables */ 114230587Sken#define GNTTAB_LEN (64) 115181643Skmacy 116230587Sken/* Features supported by all backends. TSO and LRO can be negotiated */ 117230587Sken#define XNB_CSUM_FEATURES (CSUM_TCP | CSUM_UDP) 118181643Skmacy 119230587Sken#define NET_TX_RING_SIZE __RING_SIZE((netif_tx_sring_t *)0, PAGE_SIZE) 120230587Sken#define NET_RX_RING_SIZE __RING_SIZE((netif_rx_sring_t *)0, PAGE_SIZE) 121181643Skmacy 122230587Sken/** 123230587Sken * Two argument version of the standard macro. Second argument is a tentative 124230587Sken * value of req_cons 125230587Sken */ 126230587Sken#define RING_HAS_UNCONSUMED_REQUESTS_2(_r, cons) ({ \ 127230587Sken unsigned int req = (_r)->sring->req_prod - cons; \ 128230587Sken unsigned int rsp = RING_SIZE(_r) - \ 129230587Sken (cons - (_r)->rsp_prod_pvt); \ 130230587Sken req < rsp ? req : rsp; \ 131230587Sken}) 132181643Skmacy 133286372Sjhb#define virt_to_mfn(x) (vtophys(x) >> PAGE_SHIFT) 134230587Sken#define virt_to_offset(x) ((x) & (PAGE_SIZE - 1)) 135181643Skmacy 136230587Sken/** 137230587Sken * Predefined array type of grant table copy descriptors. Used to pass around 138230587Sken * statically allocated memory structures. 139230587Sken */ 140230587Skentypedef struct gnttab_copy gnttab_copy_table[GNTTAB_LEN]; 141181643Skmacy 142230587Sken/*--------------------------- Forward Declarations ---------------------------*/ 143230587Skenstruct xnb_softc; 144230587Skenstruct xnb_pkt; 145181643Skmacy 146230587Skenstatic void xnb_attach_failed(struct xnb_softc *xnb, 147230587Sken int err, const char *fmt, ...) 148230587Sken __printflike(3,4); 149230587Skenstatic int xnb_shutdown(struct xnb_softc *xnb); 150230587Skenstatic int create_netdev(device_t dev); 151230587Skenstatic int xnb_detach(device_t dev); 152230587Skenstatic int xnb_ifmedia_upd(struct ifnet *ifp); 153230587Skenstatic void xnb_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr); 154230587Skenstatic void xnb_intr(void *arg); 155230587Skenstatic int xnb_send(netif_rx_back_ring_t *rxb, domid_t otherend, 156230587Sken const struct mbuf *mbufc, gnttab_copy_table gnttab); 157230587Skenstatic int xnb_recv(netif_tx_back_ring_t *txb, domid_t otherend, 158230587Sken struct mbuf **mbufc, struct ifnet *ifnet, 159230587Sken gnttab_copy_table gnttab); 160230587Skenstatic int xnb_ring2pkt(struct xnb_pkt *pkt, 161230587Sken const netif_tx_back_ring_t *tx_ring, 162230587Sken RING_IDX start); 163230587Skenstatic void xnb_txpkt2rsp(const struct xnb_pkt *pkt, 164230587Sken netif_tx_back_ring_t *ring, int error); 165230587Skenstatic struct mbuf *xnb_pkt2mbufc(const struct xnb_pkt *pkt, struct ifnet *ifp); 166230587Skenstatic int xnb_txpkt2gnttab(const struct xnb_pkt *pkt, 167296242Sglebius struct mbuf *mbufc, 168230587Sken gnttab_copy_table gnttab, 169230587Sken const netif_tx_back_ring_t *txb, 170230587Sken domid_t otherend_id); 171230587Skenstatic void xnb_update_mbufc(struct mbuf *mbufc, 172230587Sken const gnttab_copy_table gnttab, int n_entries); 173230587Skenstatic int xnb_mbufc2pkt(const struct mbuf *mbufc, 174230587Sken struct xnb_pkt *pkt, 175230587Sken RING_IDX start, int space); 176230587Skenstatic int xnb_rxpkt2gnttab(const struct xnb_pkt *pkt, 177230587Sken const struct mbuf *mbufc, 178230587Sken gnttab_copy_table gnttab, 179230587Sken const netif_rx_back_ring_t *rxb, 180230587Sken domid_t otherend_id); 181230587Skenstatic int xnb_rxpkt2rsp(const struct xnb_pkt *pkt, 182230587Sken const gnttab_copy_table gnttab, int n_entries, 183230587Sken netif_rx_back_ring_t *ring); 184230587Skenstatic void xnb_stop(struct xnb_softc*); 185230587Skenstatic int xnb_ioctl(struct ifnet*, u_long, caddr_t); 186230587Skenstatic void xnb_start_locked(struct ifnet*); 187230587Skenstatic void xnb_start(struct ifnet*); 188230587Skenstatic void xnb_ifinit_locked(struct xnb_softc*); 189230587Skenstatic void xnb_ifinit(void*); 190230587Sken#ifdef XNB_DEBUG 191230587Skenstatic int xnb_unit_test_main(SYSCTL_HANDLER_ARGS); 192230587Skenstatic int xnb_dump_rings(SYSCTL_HANDLER_ARGS); 193230587Sken#endif 194257515Sglebius#if defined(INET) || defined(INET6) 195257515Sglebiusstatic void xnb_add_mbuf_cksum(struct mbuf *mbufc); 196257515Sglebius#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 419298955Spfg * always accommodates 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 /** 475230587Sken * Resource representing allocated physical address space 476230587Sken * associated with our per-instance kva region. 477230587Sken */ 478230587Sken struct resource *pseudo_phys_res; 479181643Skmacy 480230587Sken /** Resource id for allocated physical address space. */ 481230587Sken int pseudo_phys_res_id; 482181643Skmacy 483230587Sken /** Ring mapping and interrupt configuration data. */ 484230587Sken struct xnb_ring_config ring_configs[XNB_NUM_RING_TYPES]; 485181643Skmacy 486230587Sken /** 487230587Sken * Global pool of kva used for mapping remote domain ring 488230587Sken * and I/O transaction data. 489230587Sken */ 490230587Sken vm_offset_t kva; 491181643Skmacy 492346817Sdchagin /** Pseudo-physical address corresponding to kva. */ 493230587Sken uint64_t gnt_base_addr; 494181643Skmacy 495230587Sken /** Various configuration and state bit flags. */ 496230587Sken xnb_flag_t flags; 497230587Sken 498230587Sken /** Mutex protecting per-instance data in the receive path. */ 499230587Sken struct mtx rx_lock; 500230587Sken 501230587Sken /** Mutex protecting per-instance data in the softc structure. */ 502230587Sken struct mtx sc_lock; 503230587Sken 504230587Sken /** Mutex protecting per-instance data in the transmit path. */ 505230587Sken struct mtx tx_lock; 506230587Sken 507230587Sken /** The size of the global kva pool. */ 508230587Sken int kva_size; 509273477Sroyger 510273477Sroyger /** Name of the interface */ 511273477Sroyger char if_name[IFNAMSIZ]; 512230587Sken}; 513230587Sken 514230587Sken/*---------------------------- Debugging functions ---------------------------*/ 515230587Sken#ifdef XNB_DEBUG 516230587Skenstatic void __unused 517230587Skenxnb_dump_gnttab_copy(const struct gnttab_copy *entry) 518181643Skmacy{ 519230587Sken if (entry == NULL) { 520230587Sken printf("NULL grant table pointer\n"); 521230587Sken return; 522181643Skmacy } 523230587Sken 524230587Sken if (entry->flags & GNTCOPY_dest_gref) 525230587Sken printf("gnttab dest ref=\t%u\n", entry->dest.u.ref); 526230587Sken else 527289686Sroyger printf("gnttab dest gmfn=\t%"PRI_xen_pfn"\n", 528289686Sroyger entry->dest.u.gmfn); 529230587Sken printf("gnttab dest offset=\t%hu\n", entry->dest.offset); 530230587Sken printf("gnttab dest domid=\t%hu\n", entry->dest.domid); 531230587Sken if (entry->flags & GNTCOPY_source_gref) 532230587Sken printf("gnttab source ref=\t%u\n", entry->source.u.ref); 533230587Sken else 534289686Sroyger printf("gnttab source gmfn=\t%"PRI_xen_pfn"\n", 535289686Sroyger entry->source.u.gmfn); 536230587Sken printf("gnttab source offset=\t%hu\n", entry->source.offset); 537230587Sken printf("gnttab source domid=\t%hu\n", entry->source.domid); 538230587Sken printf("gnttab len=\t%hu\n", entry->len); 539230587Sken printf("gnttab flags=\t%hu\n", entry->flags); 540230587Sken printf("gnttab status=\t%hd\n", entry->status); 541181643Skmacy} 542181643Skmacy 543181643Skmacystatic int 544230587Skenxnb_dump_rings(SYSCTL_HANDLER_ARGS) 545181643Skmacy{ 546230587Sken static char results[720]; 547230587Sken struct xnb_softc const* xnb = (struct xnb_softc*)arg1; 548230587Sken netif_rx_back_ring_t const* rxb = 549230587Sken &xnb->ring_configs[XNB_RING_TYPE_RX].back_ring.rx_ring; 550230587Sken netif_tx_back_ring_t const* txb = 551230587Sken &xnb->ring_configs[XNB_RING_TYPE_TX].back_ring.tx_ring; 552181643Skmacy 553230587Sken /* empty the result strings */ 554230587Sken results[0] = 0; 555181643Skmacy 556230587Sken if ( !txb || !txb->sring || !rxb || !rxb->sring ) 557230587Sken return (SYSCTL_OUT(req, results, strnlen(results, 720))); 558181643Skmacy 559230587Sken snprintf(results, 720, 560230587Sken "\n\t%35s %18s\n" /* TX, RX */ 561230587Sken "\t%16s %18d %18d\n" /* req_cons */ 562230587Sken "\t%16s %18d %18d\n" /* nr_ents */ 563230587Sken "\t%16s %18d %18d\n" /* rsp_prod_pvt */ 564230587Sken "\t%16s %18p %18p\n" /* sring */ 565230587Sken "\t%16s %18d %18d\n" /* req_prod */ 566230587Sken "\t%16s %18d %18d\n" /* req_event */ 567230587Sken "\t%16s %18d %18d\n" /* rsp_prod */ 568230587Sken "\t%16s %18d %18d\n", /* rsp_event */ 569230587Sken "TX", "RX", 570230587Sken "req_cons", txb->req_cons, rxb->req_cons, 571230587Sken "nr_ents", txb->nr_ents, rxb->nr_ents, 572230587Sken "rsp_prod_pvt", txb->rsp_prod_pvt, rxb->rsp_prod_pvt, 573230587Sken "sring", txb->sring, rxb->sring, 574230587Sken "sring->req_prod", txb->sring->req_prod, rxb->sring->req_prod, 575230587Sken "sring->req_event", txb->sring->req_event, rxb->sring->req_event, 576230587Sken "sring->rsp_prod", txb->sring->rsp_prod, rxb->sring->rsp_prod, 577230587Sken "sring->rsp_event", txb->sring->rsp_event, rxb->sring->rsp_event); 578230587Sken 579230587Sken return (SYSCTL_OUT(req, results, strnlen(results, 720))); 580181643Skmacy} 581181643Skmacy 582230587Skenstatic void __unused 583230587Skenxnb_dump_mbuf(const struct mbuf *m) 584181643Skmacy{ 585230587Sken int len; 586230587Sken uint8_t *d; 587230587Sken if (m == NULL) 588230587Sken return; 589181643Skmacy 590230587Sken printf("xnb_dump_mbuf:\n"); 591230587Sken if (m->m_flags & M_PKTHDR) { 592230587Sken printf(" flowid=%10d, csum_flags=%#8x, csum_data=%#8x, " 593230587Sken "tso_segsz=%5hd\n", 594254910Sandre m->m_pkthdr.flowid, (int)m->m_pkthdr.csum_flags, 595230587Sken m->m_pkthdr.csum_data, m->m_pkthdr.tso_segsz); 596254910Sandre printf(" rcvif=%16p, len=%19d\n", 597254910Sandre m->m_pkthdr.rcvif, m->m_pkthdr.len); 598230587Sken } 599230587Sken printf(" m_next=%16p, m_nextpk=%16p, m_data=%16p\n", 600230587Sken m->m_next, m->m_nextpkt, m->m_data); 601254910Sandre printf(" m_len=%17d, m_flags=%#15x, m_type=%18u\n", 602230587Sken m->m_len, m->m_flags, m->m_type); 603181643Skmacy 604230587Sken len = m->m_len; 605230587Sken d = mtod(m, uint8_t*); 606230587Sken while (len > 0) { 607230587Sken int i; 608230587Sken printf(" "); 609230587Sken for (i = 0; (i < 16) && (len > 0); i++, len--) { 610230587Sken printf("%02hhx ", *(d++)); 611230587Sken } 612230587Sken printf("\n"); 613181643Skmacy } 614181643Skmacy} 615230587Sken#endif /* XNB_DEBUG */ 616181643Skmacy 617230587Sken/*------------------------ Inter-Domain Communication ------------------------*/ 618230587Sken/** 619230587Sken * Free dynamically allocated KVA or pseudo-physical address allocations. 620230587Sken * 621230587Sken * \param xnb Per-instance xnb configuration structure. 622230587Sken */ 623181643Skmacystatic void 624230587Skenxnb_free_communication_mem(struct xnb_softc *xnb) 625181643Skmacy{ 626230587Sken if (xnb->kva != 0) { 627230587Sken if (xnb->pseudo_phys_res != NULL) { 628282634Sroyger xenmem_free(xnb->dev, xnb->pseudo_phys_res_id, 629230587Sken xnb->pseudo_phys_res); 630230587Sken xnb->pseudo_phys_res = NULL; 631230587Sken } 632181643Skmacy } 633230587Sken xnb->kva = 0; 634230587Sken xnb->gnt_base_addr = 0; 635181643Skmacy} 636181643Skmacy 637230587Sken/** 638230587Sken * Cleanup all inter-domain communication mechanisms. 639230587Sken * 640230587Sken * \param xnb Per-instance xnb configuration structure. 641181643Skmacy */ 642230587Skenstatic int 643230587Skenxnb_disconnect(struct xnb_softc *xnb) 644181643Skmacy{ 645230587Sken struct gnttab_unmap_grant_ref gnts[XNB_NUM_RING_TYPES]; 646230587Sken int error; 647230587Sken int i; 648181643Skmacy 649270333Sroyger if (xnb->xen_intr_handle != NULL) 650270333Sroyger xen_intr_unbind(&xnb->xen_intr_handle); 651181643Skmacy 652230587Sken /* 653230587Sken * We may still have another thread currently processing requests. We 654230587Sken * must acquire the rx and tx locks to make sure those threads are done, 655230587Sken * but we can release those locks as soon as we acquire them, because no 656230587Sken * more interrupts will be arriving. 657230587Sken */ 658230587Sken mtx_lock(&xnb->tx_lock); 659230587Sken mtx_unlock(&xnb->tx_lock); 660230587Sken mtx_lock(&xnb->rx_lock); 661230587Sken mtx_unlock(&xnb->rx_lock); 662230587Sken 663230587Sken /* Free malloc'd softc member variables */ 664270333Sroyger if (xnb->bridge != NULL) { 665230587Sken free(xnb->bridge, M_XENSTORE); 666270333Sroyger xnb->bridge = NULL; 667270333Sroyger } 668230587Sken 669230587Sken /* All request processing has stopped, so unmap the rings */ 670230587Sken for (i=0; i < XNB_NUM_RING_TYPES; i++) { 671230587Sken gnts[i].host_addr = xnb->ring_configs[i].gnt_addr; 672230587Sken gnts[i].dev_bus_addr = xnb->ring_configs[i].bus_addr; 673230587Sken gnts[i].handle = xnb->ring_configs[i].handle; 674181643Skmacy } 675230587Sken error = HYPERVISOR_grant_table_op(GNTTABOP_unmap_grant_ref, gnts, 676230587Sken XNB_NUM_RING_TYPES); 677230587Sken KASSERT(error == 0, ("Grant table unmap op failed (%d)", error)); 678181643Skmacy 679230587Sken xnb_free_communication_mem(xnb); 680230587Sken /* 681230587Sken * Zero the ring config structs because the pointers, handles, and 682230587Sken * grant refs contained therein are no longer valid. 683230587Sken */ 684230587Sken bzero(&xnb->ring_configs[XNB_RING_TYPE_TX], 685230587Sken sizeof(struct xnb_ring_config)); 686230587Sken bzero(&xnb->ring_configs[XNB_RING_TYPE_RX], 687230587Sken sizeof(struct xnb_ring_config)); 688181643Skmacy 689230587Sken xnb->flags &= ~XNBF_RING_CONNECTED; 690230587Sken return (0); 691181643Skmacy} 692181643Skmacy 693230587Sken/** 694230587Sken * Map a single shared memory ring into domain local address space and 695230587Sken * initialize its control structure 696230587Sken * 697230587Sken * \param xnb Per-instance xnb configuration structure 698230587Sken * \param ring_type Array index of this ring in the xnb's array of rings 699230587Sken * \return An errno 700230587Sken */ 701230587Skenstatic int 702230587Skenxnb_connect_ring(struct xnb_softc *xnb, xnb_ring_type_t ring_type) 703181643Skmacy{ 704230587Sken struct gnttab_map_grant_ref gnt; 705230587Sken struct xnb_ring_config *ring = &xnb->ring_configs[ring_type]; 706230587Sken int error; 707181643Skmacy 708230587Sken /* TX ring type = 0, RX =1 */ 709230587Sken ring->va = xnb->kva + ring_type * PAGE_SIZE; 710230587Sken ring->gnt_addr = xnb->gnt_base_addr + ring_type * PAGE_SIZE; 711181643Skmacy 712230587Sken gnt.host_addr = ring->gnt_addr; 713230587Sken gnt.flags = GNTMAP_host_map; 714230587Sken gnt.ref = ring->ring_ref; 715230587Sken gnt.dom = xnb->otherend_id; 716181643Skmacy 717230587Sken error = HYPERVISOR_grant_table_op(GNTTABOP_map_grant_ref, &gnt, 1); 718230587Sken if (error != 0) 719230587Sken panic("netback: Ring page grant table op failed (%d)", error); 720230587Sken 721230587Sken if (gnt.status != 0) { 722230587Sken ring->va = 0; 723230587Sken error = EACCES; 724230587Sken xenbus_dev_fatal(xnb->dev, error, 725230587Sken "Ring shared page mapping failed. " 726230587Sken "Status %d.", gnt.status); 727230587Sken } else { 728230587Sken ring->handle = gnt.handle; 729230587Sken ring->bus_addr = gnt.dev_bus_addr; 730230587Sken 731230587Sken if (ring_type == XNB_RING_TYPE_TX) { 732230587Sken BACK_RING_INIT(&ring->back_ring.tx_ring, 733230587Sken (netif_tx_sring_t*)ring->va, 734230587Sken ring->ring_pages * PAGE_SIZE); 735230587Sken } else if (ring_type == XNB_RING_TYPE_RX) { 736230587Sken BACK_RING_INIT(&ring->back_ring.rx_ring, 737230587Sken (netif_rx_sring_t*)ring->va, 738230587Sken ring->ring_pages * PAGE_SIZE); 739230587Sken } else { 740230587Sken xenbus_dev_fatal(xnb->dev, error, 741230587Sken "Unknown ring type %d", ring_type); 742230587Sken } 743181643Skmacy } 744230587Sken 745230587Sken return error; 746181643Skmacy} 747181643Skmacy 748230587Sken/** 749230587Sken * Setup the shared memory rings and bind an interrupt to the event channel 750230587Sken * used to notify us of ring changes. 751230587Sken * 752230587Sken * \param xnb Per-instance xnb configuration structure. 753230587Sken */ 754230587Skenstatic int 755230587Skenxnb_connect_comms(struct xnb_softc *xnb) 756181643Skmacy{ 757230587Sken int error; 758230587Sken xnb_ring_type_t i; 759181643Skmacy 760230587Sken if ((xnb->flags & XNBF_RING_CONNECTED) != 0) 761230587Sken return (0); 762181643Skmacy 763181643Skmacy /* 764230587Sken * Kva for our rings are at the tail of the region of kva allocated 765230587Sken * by xnb_alloc_communication_mem(). 766181643Skmacy */ 767230587Sken for (i=0; i < XNB_NUM_RING_TYPES; i++) { 768230587Sken error = xnb_connect_ring(xnb, i); 769230587Sken if (error != 0) 770230587Sken return error; 771181643Skmacy } 772181643Skmacy 773230587Sken xnb->flags |= XNBF_RING_CONNECTED; 774181643Skmacy 775255040Sgibbs error = xen_intr_bind_remote_port(xnb->dev, 776255040Sgibbs xnb->otherend_id, 777255040Sgibbs xnb->evtchn, 778255040Sgibbs /*filter*/NULL, 779255040Sgibbs xnb_intr, /*arg*/xnb, 780255040Sgibbs INTR_TYPE_BIO | INTR_MPSAFE, 781255040Sgibbs &xnb->xen_intr_handle); 782230587Sken if (error != 0) { 783230587Sken (void)xnb_disconnect(xnb); 784230587Sken xenbus_dev_fatal(xnb->dev, error, "binding event channel"); 785230587Sken return (error); 786230587Sken } 787181643Skmacy 788230587Sken DPRINTF("rings connected!\n"); 789181643Skmacy 790230587Sken return (0); 791181643Skmacy} 792181643Skmacy 793230587Sken/** 794230587Sken * Size KVA and pseudo-physical address allocations based on negotiated 795230587Sken * values for the size and number of I/O requests, and the size of our 796230587Sken * communication ring. 797230587Sken * 798230587Sken * \param xnb Per-instance xnb configuration structure. 799230587Sken * 800230587Sken * These address spaces are used to dynamically map pages in the 801230587Sken * front-end's domain into our own. 802230587Sken */ 803230587Skenstatic int 804230587Skenxnb_alloc_communication_mem(struct xnb_softc *xnb) 805181643Skmacy{ 806230587Sken xnb_ring_type_t i; 807181643Skmacy 808230587Sken xnb->kva_size = 0; 809230587Sken for (i=0; i < XNB_NUM_RING_TYPES; i++) { 810230587Sken xnb->kva_size += xnb->ring_configs[i].ring_pages * PAGE_SIZE; 811230587Sken } 812282274Sjhb 813230587Sken /* 814230587Sken * Reserve a range of pseudo physical memory that we can map 815230587Sken * into kva. These pages will only be backed by machine 816230587Sken * pages ("real memory") during the lifetime of front-end requests 817230587Sken * via grant table operations. We will map the netif tx and rx rings 818230587Sken * into this space. 819230587Sken */ 820230587Sken xnb->pseudo_phys_res_id = 0; 821282634Sroyger xnb->pseudo_phys_res = xenmem_alloc(xnb->dev, &xnb->pseudo_phys_res_id, 822282634Sroyger xnb->kva_size); 823230587Sken if (xnb->pseudo_phys_res == NULL) { 824230587Sken xnb->kva = 0; 825230587Sken return (ENOMEM); 826230587Sken } 827230587Sken xnb->kva = (vm_offset_t)rman_get_virtual(xnb->pseudo_phys_res); 828230587Sken xnb->gnt_base_addr = rman_get_start(xnb->pseudo_phys_res); 829230587Sken return (0); 830181643Skmacy} 831181643Skmacy 832230587Sken/** 833230587Sken * Collect information from the XenStore related to our device and its frontend 834230587Sken * 835230587Sken * \param xnb Per-instance xnb configuration structure. 836230587Sken */ 837230587Skenstatic int 838230587Skenxnb_collect_xenstore_info(struct xnb_softc *xnb) 839181643Skmacy{ 840230587Sken /** 841230587Sken * \todo Linux collects the following info. We should collect most 842230587Sken * of this, too: 843230587Sken * "feature-rx-notify" 844230587Sken */ 845230587Sken const char *otherend_path; 846230587Sken const char *our_path; 847230587Sken int err; 848230587Sken unsigned int rx_copy, bridge_len; 849230587Sken uint8_t no_csum_offload; 850181643Skmacy 851230587Sken otherend_path = xenbus_get_otherend_path(xnb->dev); 852230587Sken our_path = xenbus_get_node(xnb->dev); 853181643Skmacy 854230587Sken /* Collect the critical communication parameters */ 855230587Sken err = xs_gather(XST_NIL, otherend_path, 856230587Sken "tx-ring-ref", "%l" PRIu32, 857230587Sken &xnb->ring_configs[XNB_RING_TYPE_TX].ring_ref, 858230587Sken "rx-ring-ref", "%l" PRIu32, 859230587Sken &xnb->ring_configs[XNB_RING_TYPE_RX].ring_ref, 860230587Sken "event-channel", "%" PRIu32, &xnb->evtchn, 861230587Sken NULL); 862230587Sken if (err != 0) { 863230587Sken xenbus_dev_fatal(xnb->dev, err, 864230587Sken "Unable to retrieve ring information from " 865230587Sken "frontend %s. Unable to connect.", 866230587Sken otherend_path); 867230587Sken return (err); 868230587Sken } 869181643Skmacy 870230587Sken /* Collect the handle from xenstore */ 871230587Sken err = xs_scanf(XST_NIL, our_path, "handle", NULL, "%li", &xnb->handle); 872230587Sken if (err != 0) { 873230587Sken xenbus_dev_fatal(xnb->dev, err, 874230587Sken "Error reading handle from frontend %s. " 875230587Sken "Unable to connect.", otherend_path); 876230587Sken } 877181643Skmacy 878230587Sken /* 879230587Sken * Collect the bridgename, if any. We do not need bridge_len; we just 880230587Sken * throw it away 881230587Sken */ 882230587Sken err = xs_read(XST_NIL, our_path, "bridge", &bridge_len, 883230587Sken (void**)&xnb->bridge); 884230587Sken if (err != 0) 885230587Sken xnb->bridge = NULL; 886181643Skmacy 887230587Sken /* 888230587Sken * Does the frontend request that we use rx copy? If not, return an 889230587Sken * error because this driver only supports rx copy. 890230587Sken */ 891230587Sken err = xs_scanf(XST_NIL, otherend_path, "request-rx-copy", NULL, 892230587Sken "%" PRIu32, &rx_copy); 893230587Sken if (err == ENOENT) { 894230587Sken err = 0; 895230587Sken rx_copy = 0; 896230587Sken } 897230587Sken if (err < 0) { 898230587Sken xenbus_dev_fatal(xnb->dev, err, "reading %s/request-rx-copy", 899230587Sken otherend_path); 900230587Sken return err; 901230587Sken } 902230587Sken /** 903230587Sken * \todo: figure out the exact meaning of this feature, and when 904230587Sken * the frontend will set it to true. It should be set to true 905230587Sken * at some point 906230587Sken */ 907230587Sken/* if (!rx_copy)*/ 908230587Sken/* return EOPNOTSUPP;*/ 909181643Skmacy 910230587Sken /** \todo Collect the rx notify feature */ 911181643Skmacy 912230587Sken /* Collect the feature-sg. */ 913230587Sken if (xs_scanf(XST_NIL, otherend_path, "feature-sg", NULL, 914230587Sken "%hhu", &xnb->can_sg) < 0) 915230587Sken xnb->can_sg = 0; 916181643Skmacy 917230587Sken /* Collect remaining frontend features */ 918230587Sken if (xs_scanf(XST_NIL, otherend_path, "feature-gso-tcpv4", NULL, 919230587Sken "%hhu", &xnb->gso) < 0) 920230587Sken xnb->gso = 0; 921181643Skmacy 922230587Sken if (xs_scanf(XST_NIL, otherend_path, "feature-gso-tcpv4-prefix", NULL, 923230587Sken "%hhu", &xnb->gso_prefix) < 0) 924230587Sken xnb->gso_prefix = 0; 925181643Skmacy 926230587Sken if (xs_scanf(XST_NIL, otherend_path, "feature-no-csum-offload", NULL, 927230587Sken "%hhu", &no_csum_offload) < 0) 928230587Sken no_csum_offload = 0; 929230587Sken xnb->ip_csum = (no_csum_offload == 0); 930181643Skmacy 931230587Sken return (0); 932230587Sken} 933230587Sken 934230587Sken/** 935230587Sken * Supply information about the physical device to the frontend 936230587Sken * via XenBus. 937230587Sken * 938230587Sken * \param xnb Per-instance xnb configuration structure. 939230587Sken */ 940230587Skenstatic int 941230587Skenxnb_publish_backend_info(struct xnb_softc *xnb) 942230587Sken{ 943230587Sken struct xs_transaction xst; 944230587Sken const char *our_path; 945230587Sken int error; 946230587Sken 947230587Sken our_path = xenbus_get_node(xnb->dev); 948230587Sken 949230587Sken do { 950230587Sken error = xs_transaction_start(&xst); 951230587Sken if (error != 0) { 952230587Sken xenbus_dev_fatal(xnb->dev, error, 953230587Sken "Error publishing backend info " 954230587Sken "(start transaction)"); 955230587Sken break; 956181643Skmacy } 957181643Skmacy 958230587Sken error = xs_printf(xst, our_path, "feature-sg", 959230587Sken "%d", XNB_SG); 960230587Sken if (error != 0) 961230587Sken break; 962181643Skmacy 963230587Sken error = xs_printf(xst, our_path, "feature-gso-tcpv4", 964230587Sken "%d", XNB_GSO_TCPV4); 965230587Sken if (error != 0) 966181643Skmacy break; 967230587Sken 968230587Sken error = xs_printf(xst, our_path, "feature-rx-copy", 969230587Sken "%d", XNB_RX_COPY); 970230587Sken if (error != 0) 971230587Sken break; 972230587Sken 973230587Sken error = xs_printf(xst, our_path, "feature-rx-flip", 974230587Sken "%d", XNB_RX_FLIP); 975230587Sken if (error != 0) 976230587Sken break; 977230587Sken 978230587Sken error = xs_transaction_end(xst, 0); 979230587Sken if (error != 0 && error != EAGAIN) { 980230587Sken xenbus_dev_fatal(xnb->dev, error, "ending transaction"); 981230587Sken break; 982181643Skmacy } 983181643Skmacy 984230587Sken } while (error == EAGAIN); 985181643Skmacy 986230587Sken return (error); 987230587Sken} 988181643Skmacy 989230587Sken/** 990230587Sken * Connect to our netfront peer now that it has completed publishing 991230587Sken * its configuration into the XenStore. 992230587Sken * 993230587Sken * \param xnb Per-instance xnb configuration structure. 994230587Sken */ 995230587Skenstatic void 996230587Skenxnb_connect(struct xnb_softc *xnb) 997230587Sken{ 998230587Sken int error; 999181643Skmacy 1000230587Sken if (xenbus_get_state(xnb->dev) == XenbusStateConnected) 1001230587Sken return; 1002181643Skmacy 1003230587Sken if (xnb_collect_xenstore_info(xnb) != 0) 1004230587Sken return; 1005181643Skmacy 1006230587Sken xnb->flags &= ~XNBF_SHUTDOWN; 1007230587Sken 1008230587Sken /* Read front end configuration. */ 1009230587Sken 1010230587Sken /* Allocate resources whose size depends on front-end configuration. */ 1011230587Sken error = xnb_alloc_communication_mem(xnb); 1012230587Sken if (error != 0) { 1013230587Sken xenbus_dev_fatal(xnb->dev, error, 1014230587Sken "Unable to allocate communication memory"); 1015230587Sken return; 1016181643Skmacy } 1017181643Skmacy 1018230587Sken /* 1019230587Sken * Connect communication channel. 1020230587Sken */ 1021230587Sken error = xnb_connect_comms(xnb); 1022230587Sken if (error != 0) { 1023230587Sken /* Specific errors are reported by xnb_connect_comms(). */ 1024181643Skmacy return; 1025230587Sken } 1026230587Sken xnb->carrier = 1; 1027181643Skmacy 1028230587Sken /* Ready for I/O. */ 1029230587Sken xenbus_set_state(xnb->dev, XenbusStateConnected); 1030230587Sken} 1031181643Skmacy 1032230587Sken/*-------------------------- Device Teardown Support -------------------------*/ 1033230587Sken/** 1034230587Sken * Perform device shutdown functions. 1035230587Sken * 1036230587Sken * \param xnb Per-instance xnb configuration structure. 1037230587Sken * 1038230587Sken * Mark this instance as shutting down, wait for any active requests 1039230587Sken * to drain, disconnect from the front-end, and notify any waiters (e.g. 1040230587Sken * a thread invoking our detach method) that detach can now proceed. 1041230587Sken */ 1042230587Skenstatic int 1043230587Skenxnb_shutdown(struct xnb_softc *xnb) 1044230587Sken{ 1045230587Sken /* 1046230587Sken * Due to the need to drop our mutex during some 1047230587Sken * xenbus operations, it is possible for two threads 1048230587Sken * to attempt to close out shutdown processing at 1049230587Sken * the same time. Tell the caller that hits this 1050230587Sken * race to try back later. 1051230587Sken */ 1052230587Sken if ((xnb->flags & XNBF_IN_SHUTDOWN) != 0) 1053230587Sken return (EAGAIN); 1054181643Skmacy 1055230587Sken xnb->flags |= XNBF_SHUTDOWN; 1056181643Skmacy 1057230587Sken xnb->flags |= XNBF_IN_SHUTDOWN; 1058181643Skmacy 1059230587Sken mtx_unlock(&xnb->sc_lock); 1060230587Sken /* Free the network interface */ 1061230587Sken xnb->carrier = 0; 1062230587Sken if (xnb->xnb_ifp != NULL) { 1063230587Sken ether_ifdetach(xnb->xnb_ifp); 1064230587Sken if_free(xnb->xnb_ifp); 1065230587Sken xnb->xnb_ifp = NULL; 1066230587Sken } 1067230587Sken mtx_lock(&xnb->sc_lock); 1068181643Skmacy 1069230587Sken xnb_disconnect(xnb); 1070181643Skmacy 1071230587Sken mtx_unlock(&xnb->sc_lock); 1072230587Sken if (xenbus_get_state(xnb->dev) < XenbusStateClosing) 1073230587Sken xenbus_set_state(xnb->dev, XenbusStateClosing); 1074230587Sken mtx_lock(&xnb->sc_lock); 1075181643Skmacy 1076230587Sken xnb->flags &= ~XNBF_IN_SHUTDOWN; 1077181643Skmacy 1078181643Skmacy 1079230587Sken /* Indicate to xnb_detach() that is it safe to proceed. */ 1080230587Sken wakeup(xnb); 1081181643Skmacy 1082230587Sken return (0); 1083181643Skmacy} 1084181643Skmacy 1085230587Sken/** 1086230587Sken * Report an attach time error to the console and Xen, and cleanup 1087230587Sken * this instance by forcing immediate detach processing. 1088230587Sken * 1089230587Sken * \param xnb Per-instance xnb configuration structure. 1090230587Sken * \param err Errno describing the error. 1091230587Sken * \param fmt Printf style format and arguments 1092230587Sken */ 1093181643Skmacystatic void 1094230587Skenxnb_attach_failed(struct xnb_softc *xnb, int err, const char *fmt, ...) 1095181643Skmacy{ 1096230587Sken va_list ap; 1097230587Sken va_list ap_hotplug; 1098230587Sken 1099230587Sken va_start(ap, fmt); 1100230587Sken va_copy(ap_hotplug, ap); 1101230587Sken xs_vprintf(XST_NIL, xenbus_get_node(xnb->dev), 1102230587Sken "hotplug-error", fmt, ap_hotplug); 1103230587Sken va_end(ap_hotplug); 1104316362Sasomers (void)xs_printf(XST_NIL, xenbus_get_node(xnb->dev), 1105230587Sken "hotplug-status", "error"); 1106230587Sken 1107230587Sken xenbus_dev_vfatal(xnb->dev, err, fmt, ap); 1108230587Sken va_end(ap); 1109230587Sken 1110316362Sasomers (void)xs_printf(XST_NIL, xenbus_get_node(xnb->dev), "online", "0"); 1111230587Sken xnb_detach(xnb->dev); 1112181643Skmacy} 1113181643Skmacy 1114230587Sken/*---------------------------- NewBus Entrypoints ----------------------------*/ 1115230587Sken/** 1116230587Sken * Inspect a XenBus device and claim it if is of the appropriate type. 1117230587Sken * 1118230587Sken * \param dev NewBus device object representing a candidate XenBus device. 1119230587Sken * 1120230587Sken * \return 0 for success, errno codes for failure. 1121230587Sken */ 1122230587Skenstatic int 1123230587Skenxnb_probe(device_t dev) 1124181643Skmacy{ 1125230587Sken if (!strcmp(xenbus_get_type(dev), "vif")) { 1126230587Sken DPRINTF("Claiming device %d, %s\n", device_get_unit(dev), 1127230587Sken devclass_get_name(device_get_devclass(dev))); 1128230587Sken device_set_desc(dev, "Backend Virtual Network Device"); 1129230587Sken device_quiet(dev); 1130230587Sken return (0); 1131181643Skmacy } 1132230587Sken return (ENXIO); 1133181643Skmacy} 1134181643Skmacy 1135230587Sken/** 1136230587Sken * Setup sysctl variables to control various Network Back parameters. 1137230587Sken * 1138230587Sken * \param xnb Xen Net Back softc. 1139230587Sken * 1140230587Sken */ 1141181643Skmacystatic void 1142230587Skenxnb_setup_sysctl(struct xnb_softc *xnb) 1143181643Skmacy{ 1144230587Sken struct sysctl_ctx_list *sysctl_ctx = NULL; 1145230587Sken struct sysctl_oid *sysctl_tree = NULL; 1146230587Sken 1147230587Sken sysctl_ctx = device_get_sysctl_ctx(xnb->dev); 1148230587Sken if (sysctl_ctx == NULL) 1149181643Skmacy return; 1150181643Skmacy 1151230587Sken sysctl_tree = device_get_sysctl_tree(xnb->dev); 1152230587Sken if (sysctl_tree == NULL) 1153230587Sken return; 1154230587Sken 1155230587Sken#ifdef XNB_DEBUG 1156230587Sken SYSCTL_ADD_PROC(sysctl_ctx, 1157230587Sken SYSCTL_CHILDREN(sysctl_tree), 1158230587Sken OID_AUTO, 1159230587Sken "unit_test_results", 1160230587Sken CTLTYPE_STRING | CTLFLAG_RD, 1161230587Sken xnb, 1162230587Sken 0, 1163230587Sken xnb_unit_test_main, 1164230587Sken "A", 1165230587Sken "Results of builtin unit tests"); 1166230587Sken 1167230587Sken SYSCTL_ADD_PROC(sysctl_ctx, 1168230587Sken SYSCTL_CHILDREN(sysctl_tree), 1169230587Sken OID_AUTO, 1170230587Sken "dump_rings", 1171230587Sken CTLTYPE_STRING | CTLFLAG_RD, 1172230587Sken xnb, 1173230587Sken 0, 1174230587Sken xnb_dump_rings, 1175230587Sken "A", 1176230587Sken "Xennet Back Rings"); 1177230587Sken#endif /* XNB_DEBUG */ 1178181643Skmacy} 1179181643Skmacy 1180230587Sken/** 1181230587Sken * Create a network device. 1182230587Sken * @param handle device handle 1183230587Sken */ 1184230587Skenint 1185230587Skencreate_netdev(device_t dev) 1186181643Skmacy{ 1187230587Sken struct ifnet *ifp; 1188230587Sken struct xnb_softc *xnb; 1189230587Sken int err = 0; 1190273477Sroyger uint32_t handle; 1191181643Skmacy 1192230587Sken xnb = device_get_softc(dev); 1193230587Sken mtx_init(&xnb->sc_lock, "xnb_softc", "xen netback softc lock", MTX_DEF); 1194230587Sken mtx_init(&xnb->tx_lock, "xnb_tx", "xen netback tx lock", MTX_DEF); 1195230587Sken mtx_init(&xnb->rx_lock, "xnb_rx", "xen netback rx lock", MTX_DEF); 1196181643Skmacy 1197230587Sken xnb->dev = dev; 1198181643Skmacy 1199230587Sken ifmedia_init(&xnb->sc_media, 0, xnb_ifmedia_upd, xnb_ifmedia_sts); 1200230587Sken ifmedia_add(&xnb->sc_media, IFM_ETHER|IFM_MANUAL, 0, NULL); 1201230587Sken ifmedia_set(&xnb->sc_media, IFM_ETHER|IFM_MANUAL); 1202181643Skmacy 1203270333Sroyger /* 1204270333Sroyger * Set the MAC address to a dummy value (00:00:00:00:00), 1205270333Sroyger * if the MAC address of the host-facing interface is set 1206270333Sroyger * to the same as the guest-facing one (the value found in 1207270333Sroyger * xenstore), the bridge would stop delivering packets to 1208270333Sroyger * us because it would see that the destination address of 1209270333Sroyger * the packet is the same as the interface, and so the bridge 1210270333Sroyger * would expect the packet has already been delivered locally 1211270333Sroyger * (and just drop it). 1212270333Sroyger */ 1213270333Sroyger bzero(&xnb->mac[0], sizeof(xnb->mac)); 1214270333Sroyger 1215273477Sroyger /* The interface will be named using the following nomenclature: 1216273477Sroyger * 1217273477Sroyger * xnb<domid>.<handle> 1218273477Sroyger * 1219273477Sroyger * Where handle is the oder of the interface referred to the guest. 1220273477Sroyger */ 1221273477Sroyger err = xs_scanf(XST_NIL, xenbus_get_node(xnb->dev), "handle", NULL, 1222273477Sroyger "%" PRIu32, &handle); 1223273477Sroyger if (err != 0) 1224273477Sroyger return (err); 1225273477Sroyger snprintf(xnb->if_name, IFNAMSIZ, "xnb%" PRIu16 ".%" PRIu32, 1226273477Sroyger xenbus_get_otherend_id(dev), handle); 1227273477Sroyger 1228230587Sken if (err == 0) { 1229230587Sken /* Set up ifnet structure */ 1230230587Sken ifp = xnb->xnb_ifp = if_alloc(IFT_ETHER); 1231230587Sken ifp->if_softc = xnb; 1232273477Sroyger if_initname(ifp, xnb->if_name, IF_DUNIT_NONE); 1233230587Sken ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST; 1234230587Sken ifp->if_ioctl = xnb_ioctl; 1235230587Sken ifp->if_start = xnb_start; 1236230587Sken ifp->if_init = xnb_ifinit; 1237230587Sken ifp->if_mtu = ETHERMTU; 1238230587Sken ifp->if_snd.ifq_maxlen = NET_RX_RING_SIZE - 1; 1239230587Sken 1240230587Sken ifp->if_hwassist = XNB_CSUM_FEATURES; 1241230587Sken ifp->if_capabilities = IFCAP_HWCSUM; 1242230587Sken ifp->if_capenable = IFCAP_HWCSUM; 1243230587Sken 1244230587Sken ether_ifattach(ifp, xnb->mac); 1245230587Sken xnb->carrier = 0; 1246230587Sken } 1247230587Sken 1248230587Sken return err; 1249181643Skmacy} 1250181643Skmacy 1251230587Sken/** 1252230587Sken * Attach to a XenBus device that has been claimed by our probe routine. 1253230587Sken * 1254230587Sken * \param dev NewBus device object representing this Xen Net Back instance. 1255230587Sken * 1256230587Sken * \return 0 for success, errno codes for failure. 1257230587Sken */ 1258181643Skmacystatic int 1259230587Skenxnb_attach(device_t dev) 1260181643Skmacy{ 1261230587Sken struct xnb_softc *xnb; 1262230587Sken int error; 1263230587Sken xnb_ring_type_t i; 1264181643Skmacy 1265230587Sken error = create_netdev(dev); 1266230587Sken if (error != 0) { 1267230587Sken xenbus_dev_fatal(dev, error, "creating netdev"); 1268230587Sken return (error); 1269230587Sken } 1270181643Skmacy 1271230587Sken DPRINTF("Attaching to %s\n", xenbus_get_node(dev)); 1272181643Skmacy 1273230587Sken /* 1274230587Sken * Basic initialization. 1275230587Sken * After this block it is safe to call xnb_detach() 1276230587Sken * to clean up any allocated data for this instance. 1277230587Sken */ 1278230587Sken xnb = device_get_softc(dev); 1279230587Sken xnb->otherend_id = xenbus_get_otherend_id(dev); 1280230587Sken for (i=0; i < XNB_NUM_RING_TYPES; i++) { 1281230587Sken xnb->ring_configs[i].ring_pages = 1; 1282230587Sken } 1283181643Skmacy 1284230587Sken /* 1285230587Sken * Setup sysctl variables. 1286230587Sken */ 1287230587Sken xnb_setup_sysctl(xnb); 1288181643Skmacy 1289230587Sken /* Update hot-plug status to satisfy xend. */ 1290230587Sken error = xs_printf(XST_NIL, xenbus_get_node(xnb->dev), 1291230587Sken "hotplug-status", "connected"); 1292230587Sken if (error != 0) { 1293230587Sken xnb_attach_failed(xnb, error, "writing %s/hotplug-status", 1294230587Sken xenbus_get_node(xnb->dev)); 1295230587Sken return (error); 1296230587Sken } 1297181643Skmacy 1298230587Sken if ((error = xnb_publish_backend_info(xnb)) != 0) { 1299230587Sken /* 1300230587Sken * If we can't publish our data, we cannot participate 1301230587Sken * in this connection, and waiting for a front-end state 1302230587Sken * change will not help the situation. 1303230587Sken */ 1304230587Sken xnb_attach_failed(xnb, error, 1305230587Sken "Publishing backend status for %s", 1306230587Sken xenbus_get_node(xnb->dev)); 1307230587Sken return error; 1308230587Sken } 1309181643Skmacy 1310230587Sken /* Tell the front end that we are ready to connect. */ 1311230587Sken xenbus_set_state(dev, XenbusStateInitWait); 1312181643Skmacy 1313230587Sken return (0); 1314230587Sken} 1315181643Skmacy 1316230587Sken/** 1317230587Sken * Detach from a net back device instance. 1318230587Sken * 1319230587Sken * \param dev NewBus device object representing this Xen Net Back instance. 1320230587Sken * 1321230587Sken * \return 0 for success, errno codes for failure. 1322230587Sken * 1323230587Sken * \note A net back device may be detached at any time in its life-cycle, 1324230587Sken * including part way through the attach process. For this reason, 1325298955Spfg * initialization order and the initialization state checks in this 1326230587Sken * routine must be carefully coupled so that attach time failures 1327230587Sken * are gracefully handled. 1328230587Sken */ 1329230587Skenstatic int 1330230587Skenxnb_detach(device_t dev) 1331230587Sken{ 1332230587Sken struct xnb_softc *xnb; 1333181643Skmacy 1334230587Sken DPRINTF("\n"); 1335181643Skmacy 1336230587Sken xnb = device_get_softc(dev); 1337230587Sken mtx_lock(&xnb->sc_lock); 1338230587Sken while (xnb_shutdown(xnb) == EAGAIN) { 1339230587Sken msleep(xnb, &xnb->sc_lock, /*wakeup prio unchanged*/0, 1340230587Sken "xnb_shutdown", 0); 1341230587Sken } 1342230587Sken mtx_unlock(&xnb->sc_lock); 1343230587Sken DPRINTF("\n"); 1344181643Skmacy 1345230587Sken mtx_destroy(&xnb->tx_lock); 1346230587Sken mtx_destroy(&xnb->rx_lock); 1347230587Sken mtx_destroy(&xnb->sc_lock); 1348230587Sken return (0); 1349230587Sken} 1350181643Skmacy 1351230587Sken/** 1352230587Sken * Prepare this net back device for suspension of this VM. 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_suspend(device_t dev) 1360230587Sken{ 1361230587Sken return (0); 1362230587Sken} 1363181643Skmacy 1364230587Sken/** 1365230587Sken * Perform any processing required to recover from a suspended state. 1366230587Sken * 1367230587Sken * \param dev NewBus device object representing this Xen Net Back instance. 1368230587Sken * 1369230587Sken * \return 0 for success, errno codes for failure. 1370230587Sken */ 1371230587Skenstatic int 1372230587Skenxnb_resume(device_t dev) 1373230587Sken{ 1374230587Sken return (0); 1375230587Sken} 1376181643Skmacy 1377230587Sken/** 1378230587Sken * Handle state changes expressed via the XenStore by our front-end peer. 1379230587Sken * 1380230587Sken * \param dev NewBus device object representing this Xen 1381230587Sken * Net Back instance. 1382230587Sken * \param frontend_state The new state of the front-end. 1383230587Sken * 1384230587Sken * \return 0 for success, errno codes for failure. 1385230587Sken */ 1386230587Skenstatic void 1387230587Skenxnb_frontend_changed(device_t dev, XenbusState frontend_state) 1388230587Sken{ 1389230587Sken struct xnb_softc *xnb; 1390181643Skmacy 1391230587Sken xnb = device_get_softc(dev); 1392181643Skmacy 1393230587Sken DPRINTF("frontend_state=%s, xnb_state=%s\n", 1394230587Sken xenbus_strstate(frontend_state), 1395230587Sken xenbus_strstate(xenbus_get_state(xnb->dev))); 1396181643Skmacy 1397230587Sken switch (frontend_state) { 1398230587Sken case XenbusStateInitialising: 1399230587Sken break; 1400230587Sken case XenbusStateInitialised: 1401230587Sken case XenbusStateConnected: 1402230587Sken xnb_connect(xnb); 1403230587Sken break; 1404230587Sken case XenbusStateClosing: 1405230587Sken case XenbusStateClosed: 1406230587Sken mtx_lock(&xnb->sc_lock); 1407230587Sken xnb_shutdown(xnb); 1408230587Sken mtx_unlock(&xnb->sc_lock); 1409230587Sken if (frontend_state == XenbusStateClosed) 1410230587Sken xenbus_set_state(xnb->dev, XenbusStateClosed); 1411230587Sken break; 1412230587Sken default: 1413230587Sken xenbus_dev_fatal(xnb->dev, EINVAL, "saw state %d at frontend", 1414230587Sken frontend_state); 1415230587Sken break; 1416230587Sken } 1417230587Sken} 1418181643Skmacy 1419181643Skmacy 1420230587Sken/*---------------------------- Request Processing ----------------------------*/ 1421230587Sken/** 1422230587Sken * Interrupt handler bound to the shared ring's event channel. 1423230587Sken * Entry point for the xennet transmit path in netback 1424230587Sken * Transfers packets from the Xen ring to the host's generic networking stack 1425230587Sken * 1426230587Sken * \param arg Callback argument registerd during event channel 1427230587Sken * binding - the xnb_softc for this instance. 1428230587Sken */ 1429230587Skenstatic void 1430230587Skenxnb_intr(void *arg) 1431230587Sken{ 1432230587Sken struct xnb_softc *xnb; 1433230587Sken struct ifnet *ifp; 1434230587Sken netif_tx_back_ring_t *txb; 1435230587Sken RING_IDX req_prod_local; 1436181643Skmacy 1437230587Sken xnb = (struct xnb_softc *)arg; 1438230587Sken ifp = xnb->xnb_ifp; 1439230587Sken txb = &xnb->ring_configs[XNB_RING_TYPE_TX].back_ring.tx_ring; 1440181643Skmacy 1441230587Sken mtx_lock(&xnb->tx_lock); 1442230587Sken do { 1443230587Sken int notify; 1444230587Sken req_prod_local = txb->sring->req_prod; 1445230587Sken xen_rmb(); 1446181643Skmacy 1447230587Sken for (;;) { 1448230587Sken struct mbuf *mbufc; 1449230587Sken int err; 1450181643Skmacy 1451230587Sken err = xnb_recv(txb, xnb->otherend_id, &mbufc, ifp, 1452230587Sken xnb->tx_gnttab); 1453230587Sken if (err || (mbufc == NULL)) 1454230587Sken break; 1455181643Skmacy 1456230587Sken /* Send the packet to the generic network stack */ 1457230587Sken (*xnb->xnb_ifp->if_input)(xnb->xnb_ifp, mbufc); 1458230587Sken } 1459181643Skmacy 1460230587Sken RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(txb, notify); 1461230587Sken if (notify != 0) 1462255040Sgibbs xen_intr_signal(xnb->xen_intr_handle); 1463181643Skmacy 1464230587Sken txb->sring->req_event = txb->req_cons + 1; 1465230587Sken xen_mb(); 1466230587Sken } while (txb->sring->req_prod != req_prod_local) ; 1467230587Sken mtx_unlock(&xnb->tx_lock); 1468181643Skmacy 1469230587Sken xnb_start(ifp); 1470230587Sken} 1471181643Skmacy 1472181643Skmacy 1473230587Sken/** 1474230587Sken * Build a struct xnb_pkt based on netif_tx_request's from a netif tx ring. 1475230587Sken * Will read exactly 0 or 1 packets from the ring; never a partial packet. 1476230587Sken * \param[out] pkt The returned packet. If there is an error building 1477230587Sken * the packet, pkt.list_len will be set to 0. 1478230587Sken * \param[in] tx_ring Pointer to the Ring that is the input to this function 1479230587Sken * \param[in] start The ring index of the first potential request 1480230587Sken * \return The number of requests consumed to build this packet 1481230587Sken */ 1482230587Skenstatic int 1483230587Skenxnb_ring2pkt(struct xnb_pkt *pkt, const netif_tx_back_ring_t *tx_ring, 1484230587Sken RING_IDX start) 1485230587Sken{ 1486230587Sken /* 1487230587Sken * Outline: 1488230587Sken * 1) Initialize pkt 1489230587Sken * 2) Read the first request of the packet 1490230587Sken * 3) Read the extras 1491230587Sken * 4) Set cdr 1492230587Sken * 5) Loop on the remainder of the packet 1493230587Sken * 6) Finalize pkt (stuff like car_size and list_len) 1494230587Sken */ 1495230587Sken int idx = start; 1496230587Sken int discard = 0; /* whether to discard the packet */ 1497230587Sken int more_data = 0; /* there are more request past the last one */ 1498230587Sken uint16_t cdr_size = 0; /* accumulated size of requests 2 through n */ 1499181643Skmacy 1500230587Sken xnb_pkt_initialize(pkt); 1501181643Skmacy 1502230587Sken /* Read the first request */ 1503230587Sken if (RING_HAS_UNCONSUMED_REQUESTS_2(tx_ring, idx)) { 1504230587Sken netif_tx_request_t *tx = RING_GET_REQUEST(tx_ring, idx); 1505230587Sken pkt->size = tx->size; 1506230587Sken pkt->flags = tx->flags & ~NETTXF_more_data; 1507230587Sken more_data = tx->flags & NETTXF_more_data; 1508230587Sken pkt->list_len++; 1509230587Sken pkt->car = idx; 1510230587Sken idx++; 1511230587Sken } 1512181643Skmacy 1513230587Sken /* Read the extra info */ 1514230587Sken if ((pkt->flags & NETTXF_extra_info) && 1515230587Sken RING_HAS_UNCONSUMED_REQUESTS_2(tx_ring, idx)) { 1516230587Sken netif_extra_info_t *ext = 1517230587Sken (netif_extra_info_t*) RING_GET_REQUEST(tx_ring, idx); 1518230587Sken pkt->extra.type = ext->type; 1519230587Sken switch (pkt->extra.type) { 1520230587Sken case XEN_NETIF_EXTRA_TYPE_GSO: 1521230587Sken pkt->extra.u.gso = ext->u.gso; 1522230587Sken break; 1523230587Sken default: 1524230587Sken /* 1525230587Sken * The reference Linux netfront driver will 1526230587Sken * never set any other extra.type. So we don't 1527230587Sken * know what to do with it. Let's print an 1528230587Sken * error, then consume and discard the packet 1529230587Sken */ 1530230587Sken printf("xnb(%s:%d): Unknown extra info type %d." 1531230587Sken " Discarding packet\n", 1532230587Sken __func__, __LINE__, pkt->extra.type); 1533230587Sken xnb_dump_txreq(start, RING_GET_REQUEST(tx_ring, 1534230587Sken start)); 1535230587Sken xnb_dump_txreq(idx, RING_GET_REQUEST(tx_ring, 1536230587Sken idx)); 1537230587Sken discard = 1; 1538230587Sken break; 1539230587Sken } 1540230587Sken 1541230587Sken pkt->extra.flags = ext->flags; 1542230587Sken if (ext->flags & XEN_NETIF_EXTRA_FLAG_MORE) { 1543181643Skmacy /* 1544230587Sken * The reference linux netfront driver never sets this 1545230587Sken * flag (nor does any other known netfront). So we 1546230587Sken * will discard the packet. 1547181643Skmacy */ 1548230587Sken printf("xnb(%s:%d): Request sets " 1549230587Sken "XEN_NETIF_EXTRA_FLAG_MORE, but we can't handle " 1550230587Sken "that\n", __func__, __LINE__); 1551230587Sken xnb_dump_txreq(start, RING_GET_REQUEST(tx_ring, start)); 1552230587Sken xnb_dump_txreq(idx, RING_GET_REQUEST(tx_ring, idx)); 1553230587Sken discard = 1; 1554181643Skmacy } 1555181643Skmacy 1556230587Sken idx++; 1557181643Skmacy } 1558181643Skmacy 1559230587Sken /* Set cdr. If there is not more data, cdr is invalid */ 1560230587Sken pkt->cdr = idx; 1561181643Skmacy 1562230587Sken /* Loop on remainder of packet */ 1563230587Sken while (more_data && RING_HAS_UNCONSUMED_REQUESTS_2(tx_ring, idx)) { 1564230587Sken netif_tx_request_t *tx = RING_GET_REQUEST(tx_ring, idx); 1565230587Sken pkt->list_len++; 1566230587Sken cdr_size += tx->size; 1567230587Sken if (tx->flags & ~NETTXF_more_data) { 1568230587Sken /* There should be no other flags set at this point */ 1569230587Sken printf("xnb(%s:%d): Request sets unknown flags %d " 1570230587Sken "after the 1st request in the packet.\n", 1571230587Sken __func__, __LINE__, tx->flags); 1572230587Sken xnb_dump_txreq(start, RING_GET_REQUEST(tx_ring, start)); 1573230587Sken xnb_dump_txreq(idx, RING_GET_REQUEST(tx_ring, idx)); 1574230587Sken } 1575181643Skmacy 1576230587Sken more_data = tx->flags & NETTXF_more_data; 1577230587Sken idx++; 1578230587Sken } 1579230587Sken 1580230587Sken /* Finalize packet */ 1581230587Sken if (more_data != 0) { 1582230587Sken /* The ring ran out of requests before finishing the packet */ 1583230587Sken xnb_pkt_invalidate(pkt); 1584230587Sken idx = start; /* tell caller that we consumed no requests */ 1585230587Sken } else { 1586230587Sken /* Calculate car_size */ 1587230587Sken pkt->car_size = pkt->size - cdr_size; 1588230587Sken } 1589230587Sken if (discard != 0) { 1590230587Sken xnb_pkt_invalidate(pkt); 1591230587Sken } 1592230587Sken 1593230587Sken return idx - start; 1594181643Skmacy} 1595181643Skmacy 1596230587Sken 1597230587Sken/** 1598230587Sken * Respond to all the requests that constituted pkt. Builds the responses and 1599230587Sken * writes them to the ring, but doesn't push them to the shared ring. 1600230587Sken * \param[in] pkt the packet that needs a response 1601230587Sken * \param[in] error true if there was an error handling the packet, such 1602230587Sken * as in the hypervisor copy op or mbuf allocation 1603230587Sken * \param[out] ring Responses go here 1604230587Sken */ 1605181643Skmacystatic void 1606230587Skenxnb_txpkt2rsp(const struct xnb_pkt *pkt, netif_tx_back_ring_t *ring, 1607230587Sken int error) 1608181643Skmacy{ 1609230587Sken /* 1610230587Sken * Outline: 1611230587Sken * 1) Respond to the first request 1612230587Sken * 2) Respond to the extra info reques 1613230587Sken * Loop through every remaining request in the packet, generating 1614230587Sken * responses that copy those requests' ids and sets the status 1615230587Sken * appropriately. 1616230587Sken */ 1617230587Sken netif_tx_request_t *tx; 1618230587Sken netif_tx_response_t *rsp; 1619230587Sken int i; 1620230587Sken uint16_t status; 1621181643Skmacy 1622230587Sken status = (xnb_pkt_is_valid(pkt) == 0) || error ? 1623230587Sken NETIF_RSP_ERROR : NETIF_RSP_OKAY; 1624230587Sken KASSERT((pkt->list_len == 0) || (ring->rsp_prod_pvt == pkt->car), 1625230587Sken ("Cannot respond to ring requests out of order")); 1626181643Skmacy 1627230587Sken if (pkt->list_len >= 1) { 1628230587Sken uint16_t id; 1629230587Sken tx = RING_GET_REQUEST(ring, ring->rsp_prod_pvt); 1630230587Sken id = tx->id; 1631230587Sken rsp = RING_GET_RESPONSE(ring, ring->rsp_prod_pvt); 1632230587Sken rsp->id = id; 1633230587Sken rsp->status = status; 1634230587Sken ring->rsp_prod_pvt++; 1635181643Skmacy 1636230587Sken if (pkt->flags & NETRXF_extra_info) { 1637230587Sken rsp = RING_GET_RESPONSE(ring, ring->rsp_prod_pvt); 1638230587Sken rsp->status = NETIF_RSP_NULL; 1639230587Sken ring->rsp_prod_pvt++; 1640181643Skmacy } 1641230587Sken } 1642181643Skmacy 1643230587Sken for (i=0; i < pkt->list_len - 1; i++) { 1644230587Sken uint16_t id; 1645230587Sken tx = RING_GET_REQUEST(ring, ring->rsp_prod_pvt); 1646230587Sken id = tx->id; 1647230587Sken rsp = RING_GET_RESPONSE(ring, ring->rsp_prod_pvt); 1648230587Sken rsp->id = id; 1649230587Sken rsp->status = status; 1650230587Sken ring->rsp_prod_pvt++; 1651181643Skmacy } 1652181643Skmacy} 1653181643Skmacy 1654230587Sken/** 1655230587Sken * Create an mbuf chain to represent a packet. Initializes all of the headers 1656230587Sken * in the mbuf chain, but does not copy the data. The returned chain must be 1657230587Sken * free()'d when no longer needed 1658230587Sken * \param[in] pkt A packet to model the mbuf chain after 1659230587Sken * \return A newly allocated mbuf chain, possibly with clusters attached. 1660230587Sken * NULL on failure 1661230587Sken */ 1662230587Skenstatic struct mbuf* 1663230587Skenxnb_pkt2mbufc(const struct xnb_pkt *pkt, struct ifnet *ifp) 1664181643Skmacy{ 1665230587Sken /** 1666230587Sken * \todo consider using a memory pool for mbufs instead of 1667230587Sken * reallocating them for every packet 1668230587Sken */ 1669230587Sken /** \todo handle extra data */ 1670230587Sken struct mbuf *m; 1671181643Skmacy 1672230587Sken m = m_getm(NULL, pkt->size, M_NOWAIT, MT_DATA); 1673181643Skmacy 1674230587Sken if (m != NULL) { 1675230587Sken m->m_pkthdr.rcvif = ifp; 1676230587Sken if (pkt->flags & NETTXF_data_validated) { 1677230587Sken /* 1678230587Sken * We lie to the host OS and always tell it that the 1679230587Sken * checksums are ok, because the packet is unlikely to 1680230587Sken * get corrupted going across domains. 1681230587Sken */ 1682230587Sken m->m_pkthdr.csum_flags = ( 1683230587Sken CSUM_IP_CHECKED | 1684230587Sken CSUM_IP_VALID | 1685230587Sken CSUM_DATA_VALID | 1686230587Sken CSUM_PSEUDO_HDR 1687230587Sken ); 1688230587Sken m->m_pkthdr.csum_data = 0xffff; 1689230587Sken } 1690230587Sken } 1691230587Sken return m; 1692181643Skmacy} 1693181643Skmacy 1694230587Sken/** 1695230587Sken * Build a gnttab_copy table that can be used to copy data from a pkt 1696230587Sken * to an mbufc. Does not actually perform the copy. Always uses gref's on 1697230587Sken * the packet side. 1698230587Sken * \param[in] pkt pkt's associated requests form the src for 1699230587Sken * the copy operation 1700230587Sken * \param[in] mbufc mbufc's storage forms the dest for the copy operation 1701230587Sken * \param[out] gnttab Storage for the returned grant table 1702230587Sken * \param[in] txb Pointer to the backend ring structure 1703230587Sken * \param[in] otherend_id The domain ID of the other end of the copy 1704230587Sken * \return The number of gnttab entries filled 1705230587Sken */ 1706181643Skmacystatic int 1707296242Sglebiusxnb_txpkt2gnttab(const struct xnb_pkt *pkt, struct mbuf *mbufc, 1708230587Sken gnttab_copy_table gnttab, const netif_tx_back_ring_t *txb, 1709230587Sken domid_t otherend_id) 1710181643Skmacy{ 1711181643Skmacy 1712296242Sglebius struct mbuf *mbuf = mbufc;/* current mbuf within the chain */ 1713230587Sken int gnt_idx = 0; /* index into grant table */ 1714230587Sken RING_IDX r_idx = pkt->car; /* index into tx ring buffer */ 1715230587Sken int r_ofs = 0; /* offset of next data within tx request's data area */ 1716230587Sken int m_ofs = 0; /* offset of next data within mbuf's data area */ 1717230587Sken /* size in bytes that still needs to be represented in the table */ 1718230587Sken uint16_t size_remaining = pkt->size; 1719181643Skmacy 1720230587Sken while (size_remaining > 0) { 1721230587Sken const netif_tx_request_t *txq = RING_GET_REQUEST(txb, r_idx); 1722230587Sken const size_t mbuf_space = M_TRAILINGSPACE(mbuf) - m_ofs; 1723230587Sken const size_t req_size = 1724230587Sken r_idx == pkt->car ? pkt->car_size : txq->size; 1725230587Sken const size_t pkt_space = req_size - r_ofs; 1726230587Sken /* 1727230587Sken * space is the largest amount of data that can be copied in the 1728230587Sken * grant table's next entry 1729230587Sken */ 1730230587Sken const size_t space = MIN(pkt_space, mbuf_space); 1731230587Sken 1732230587Sken /* TODO: handle this error condition without panicking */ 1733230587Sken KASSERT(gnt_idx < GNTTAB_LEN, ("Grant table is too short")); 1734230587Sken 1735230587Sken gnttab[gnt_idx].source.u.ref = txq->gref; 1736230587Sken gnttab[gnt_idx].source.domid = otherend_id; 1737230587Sken gnttab[gnt_idx].source.offset = txq->offset + r_ofs; 1738230587Sken gnttab[gnt_idx].dest.u.gmfn = virt_to_mfn( 1739230587Sken mtod(mbuf, vm_offset_t) + m_ofs); 1740230587Sken gnttab[gnt_idx].dest.offset = virt_to_offset( 1741230587Sken mtod(mbuf, vm_offset_t) + m_ofs); 1742230587Sken gnttab[gnt_idx].dest.domid = DOMID_SELF; 1743230587Sken gnttab[gnt_idx].len = space; 1744230587Sken gnttab[gnt_idx].flags = GNTCOPY_source_gref; 1745230587Sken 1746230587Sken gnt_idx++; 1747230587Sken r_ofs += space; 1748230587Sken m_ofs += space; 1749230587Sken size_remaining -= space; 1750230587Sken if (req_size - r_ofs <= 0) { 1751230587Sken /* Must move to the next tx request */ 1752230587Sken r_ofs = 0; 1753230587Sken r_idx = (r_idx == pkt->car) ? pkt->cdr : r_idx + 1; 1754230587Sken } 1755230587Sken if (M_TRAILINGSPACE(mbuf) - m_ofs <= 0) { 1756230587Sken /* Must move to the next mbuf */ 1757230587Sken m_ofs = 0; 1758230587Sken mbuf = mbuf->m_next; 1759230587Sken } 1760181643Skmacy } 1761181643Skmacy 1762230587Sken return gnt_idx; 1763181643Skmacy} 1764181643Skmacy 1765230587Sken/** 1766230587Sken * Check the status of the grant copy operations, and update mbufs various 1767230587Sken * non-data fields to reflect the data present. 1768230587Sken * \param[in,out] mbufc mbuf chain to update. The chain must be valid and of 1769230587Sken * the correct length, and data should already be present 1770230587Sken * \param[in] gnttab A grant table for a just completed copy op 1771230587Sken * \param[in] n_entries The number of valid entries in the grant table 1772230587Sken */ 1773181643Skmacystatic void 1774230587Skenxnb_update_mbufc(struct mbuf *mbufc, const gnttab_copy_table gnttab, 1775230587Sken int n_entries) 1776181643Skmacy{ 1777230587Sken struct mbuf *mbuf = mbufc; 1778230587Sken int i; 1779230587Sken size_t total_size = 0; 1780181643Skmacy 1781230587Sken for (i = 0; i < n_entries; i++) { 1782230587Sken KASSERT(gnttab[i].status == GNTST_okay, 1783230587Sken ("Some gnttab_copy entry had error status %hd\n", 1784230587Sken gnttab[i].status)); 1785181643Skmacy 1786230587Sken mbuf->m_len += gnttab[i].len; 1787230587Sken total_size += gnttab[i].len; 1788230587Sken if (M_TRAILINGSPACE(mbuf) <= 0) { 1789230587Sken mbuf = mbuf->m_next; 1790230587Sken } 1791230587Sken } 1792230587Sken mbufc->m_pkthdr.len = total_size; 1793230587Sken 1794257515Sglebius#if defined(INET) || defined(INET6) 1795230587Sken xnb_add_mbuf_cksum(mbufc); 1796257515Sglebius#endif 1797181643Skmacy} 1798181643Skmacy 1799230587Sken/** 1800230587Sken * Dequeue at most one packet from the shared ring 1801230587Sken * \param[in,out] txb Netif tx ring. A packet will be removed from it, and 1802230587Sken * its private indices will be updated. But the indices 1803230587Sken * will not be pushed to the shared ring. 1804230587Sken * \param[in] ifnet Interface to which the packet will be sent 1805230587Sken * \param[in] otherend Domain ID of the other end of the ring 1806230587Sken * \param[out] mbufc The assembled mbuf chain, ready to send to the generic 1807230587Sken * networking stack 1808230587Sken * \param[in,out] gnttab Pointer to enough memory for a grant table. We make 1809230587Sken * this a function parameter so that we will take less 1810230587Sken * stack space. 1811230587Sken * \return An error code 1812230587Sken */ 1813181643Skmacystatic int 1814230587Skenxnb_recv(netif_tx_back_ring_t *txb, domid_t otherend, struct mbuf **mbufc, 1815230587Sken struct ifnet *ifnet, gnttab_copy_table gnttab) 1816181643Skmacy{ 1817230587Sken struct xnb_pkt pkt; 1818230587Sken /* number of tx requests consumed to build the last packet */ 1819230587Sken int num_consumed; 1820230587Sken int nr_ents; 1821181643Skmacy 1822230587Sken *mbufc = NULL; 1823230587Sken num_consumed = xnb_ring2pkt(&pkt, txb, txb->req_cons); 1824230587Sken if (num_consumed == 0) 1825230587Sken return 0; /* Nothing to receive */ 1826181643Skmacy 1827249588Sgabor /* update statistics independent of errors */ 1828271849Sglebius if_inc_counter(ifnet, IFCOUNTER_IPACKETS, 1); 1829181643Skmacy 1830230587Sken /* 1831230587Sken * if we got here, then 1 or more requests was consumed, but the packet 1832249583Sgabor * is not necessarily valid. 1833230587Sken */ 1834230587Sken if (xnb_pkt_is_valid(&pkt) == 0) { 1835230587Sken /* got a garbage packet, respond and drop it */ 1836230587Sken xnb_txpkt2rsp(&pkt, txb, 1); 1837230587Sken txb->req_cons += num_consumed; 1838230587Sken DPRINTF("xnb_intr: garbage packet, num_consumed=%d\n", 1839230587Sken num_consumed); 1840271849Sglebius if_inc_counter(ifnet, IFCOUNTER_IERRORS, 1); 1841230587Sken return EINVAL; 1842181643Skmacy } 1843181643Skmacy 1844230587Sken *mbufc = xnb_pkt2mbufc(&pkt, ifnet); 1845230587Sken 1846230587Sken if (*mbufc == NULL) { 1847230587Sken /* 1848230587Sken * Couldn't allocate mbufs. Respond and drop the packet. Do 1849230587Sken * not consume the requests 1850230587Sken */ 1851230587Sken xnb_txpkt2rsp(&pkt, txb, 1); 1852230587Sken DPRINTF("xnb_intr: Couldn't allocate mbufs, num_consumed=%d\n", 1853230587Sken num_consumed); 1854271849Sglebius if_inc_counter(ifnet, IFCOUNTER_IQDROPS, 1); 1855230587Sken return ENOMEM; 1856181643Skmacy } 1857181643Skmacy 1858230587Sken nr_ents = xnb_txpkt2gnttab(&pkt, *mbufc, gnttab, txb, otherend); 1859181643Skmacy 1860230587Sken if (nr_ents > 0) { 1861230587Sken int __unused hv_ret = HYPERVISOR_grant_table_op(GNTTABOP_copy, 1862230587Sken gnttab, nr_ents); 1863230587Sken KASSERT(hv_ret == 0, 1864230587Sken ("HYPERVISOR_grant_table_op returned %d\n", hv_ret)); 1865230587Sken xnb_update_mbufc(*mbufc, gnttab, nr_ents); 1866230587Sken } 1867181643Skmacy 1868230587Sken xnb_txpkt2rsp(&pkt, txb, 0); 1869230587Sken txb->req_cons += num_consumed; 1870181643Skmacy return 0; 1871181643Skmacy} 1872181643Skmacy 1873230587Sken/** 1874230587Sken * Create an xnb_pkt based on the contents of an mbuf chain. 1875230587Sken * \param[in] mbufc mbuf chain to transform into a packet 1876230587Sken * \param[out] pkt Storage for the newly generated xnb_pkt 1877230587Sken * \param[in] start The ring index of the first available slot in the rx 1878230587Sken * ring 1879230587Sken * \param[in] space The number of free slots in the rx ring 1880230587Sken * \retval 0 Success 1881230587Sken * \retval EINVAL mbufc was corrupt or not convertible into a pkt 1882230587Sken * \retval EAGAIN There was not enough space in the ring to queue the 1883230587Sken * packet 1884230587Sken */ 1885230587Skenstatic int 1886230587Skenxnb_mbufc2pkt(const struct mbuf *mbufc, struct xnb_pkt *pkt, 1887230587Sken RING_IDX start, int space) 1888181643Skmacy{ 1889181643Skmacy 1890230587Sken int retval = 0; 1891181643Skmacy 1892230587Sken if ((mbufc == NULL) || 1893230587Sken ( (mbufc->m_flags & M_PKTHDR) == 0) || 1894230587Sken (mbufc->m_pkthdr.len == 0)) { 1895230587Sken xnb_pkt_invalidate(pkt); 1896230587Sken retval = EINVAL; 1897230587Sken } else { 1898230587Sken int slots_required; 1899181643Skmacy 1900230587Sken xnb_pkt_validate(pkt); 1901230587Sken pkt->flags = 0; 1902230587Sken pkt->size = mbufc->m_pkthdr.len; 1903230587Sken pkt->car = start; 1904230587Sken pkt->car_size = mbufc->m_len; 1905181643Skmacy 1906230587Sken if (mbufc->m_pkthdr.csum_flags & CSUM_TSO) { 1907230587Sken pkt->flags |= NETRXF_extra_info; 1908230587Sken pkt->extra.u.gso.size = mbufc->m_pkthdr.tso_segsz; 1909230587Sken pkt->extra.u.gso.type = XEN_NETIF_GSO_TYPE_TCPV4; 1910230587Sken pkt->extra.u.gso.pad = 0; 1911230587Sken pkt->extra.u.gso.features = 0; 1912230587Sken pkt->extra.type = XEN_NETIF_EXTRA_TYPE_GSO; 1913230587Sken pkt->extra.flags = 0; 1914230587Sken pkt->cdr = start + 2; 1915230587Sken } else { 1916230587Sken pkt->cdr = start + 1; 1917230587Sken } 1918230587Sken if (mbufc->m_pkthdr.csum_flags & (CSUM_TSO | CSUM_DELAY_DATA)) { 1919230587Sken pkt->flags |= 1920230587Sken (NETRXF_csum_blank | NETRXF_data_validated); 1921230587Sken } 1922230587Sken 1923230587Sken /* 1924230587Sken * Each ring response can have up to PAGE_SIZE of data. 1925230587Sken * Assume that we can defragment the mbuf chain efficiently 1926230587Sken * into responses so that each response but the last uses all 1927230587Sken * PAGE_SIZE bytes. 1928230587Sken */ 1929298646Spfg pkt->list_len = howmany(pkt->size, PAGE_SIZE); 1930230587Sken 1931230587Sken if (pkt->list_len > 1) { 1932230587Sken pkt->flags |= NETRXF_more_data; 1933230587Sken } 1934230587Sken 1935230587Sken slots_required = pkt->list_len + 1936230587Sken (pkt->flags & NETRXF_extra_info ? 1 : 0); 1937230587Sken if (slots_required > space) { 1938230587Sken xnb_pkt_invalidate(pkt); 1939230587Sken retval = EAGAIN; 1940230587Sken } 1941181643Skmacy } 1942230587Sken 1943230587Sken return retval; 1944181643Skmacy} 1945181643Skmacy 1946230587Sken/** 1947230587Sken * Build a gnttab_copy table that can be used to copy data from an mbuf chain 1948230587Sken * to the frontend's shared buffers. Does not actually perform the copy. 1949230587Sken * Always uses gref's on the other end's side. 1950230587Sken * \param[in] pkt pkt's associated responses form the dest for the copy 1951230587Sken * operatoin 1952230587Sken * \param[in] mbufc The source for the copy operation 1953230587Sken * \param[out] gnttab Storage for the returned grant table 1954230587Sken * \param[in] rxb Pointer to the backend ring structure 1955230587Sken * \param[in] otherend_id The domain ID of the other end of the copy 1956230587Sken * \return The number of gnttab entries filled 1957230587Sken */ 1958181643Skmacystatic int 1959230587Skenxnb_rxpkt2gnttab(const struct xnb_pkt *pkt, const struct mbuf *mbufc, 1960230587Sken gnttab_copy_table gnttab, const netif_rx_back_ring_t *rxb, 1961230587Sken domid_t otherend_id) 1962181643Skmacy{ 1963181643Skmacy 1964230587Sken const struct mbuf *mbuf = mbufc;/* current mbuf within the chain */ 1965230587Sken int gnt_idx = 0; /* index into grant table */ 1966230587Sken RING_IDX r_idx = pkt->car; /* index into rx ring buffer */ 1967230587Sken int r_ofs = 0; /* offset of next data within rx request's data area */ 1968230587Sken int m_ofs = 0; /* offset of next data within mbuf's data area */ 1969230587Sken /* size in bytes that still needs to be represented in the table */ 1970230587Sken uint16_t size_remaining; 1971181643Skmacy 1972230587Sken size_remaining = (xnb_pkt_is_valid(pkt) != 0) ? pkt->size : 0; 1973230587Sken 1974230587Sken while (size_remaining > 0) { 1975230587Sken const netif_rx_request_t *rxq = RING_GET_REQUEST(rxb, r_idx); 1976230587Sken const size_t mbuf_space = mbuf->m_len - m_ofs; 1977230587Sken /* Xen shared pages have an implied size of PAGE_SIZE */ 1978230587Sken const size_t req_size = PAGE_SIZE; 1979230587Sken const size_t pkt_space = req_size - r_ofs; 1980230587Sken /* 1981230587Sken * space is the largest amount of data that can be copied in the 1982230587Sken * grant table's next entry 1983230587Sken */ 1984230587Sken const size_t space = MIN(pkt_space, mbuf_space); 1985230587Sken 1986230587Sken /* TODO: handle this error condition without panicing */ 1987230587Sken KASSERT(gnt_idx < GNTTAB_LEN, ("Grant table is too short")); 1988230587Sken 1989230587Sken gnttab[gnt_idx].dest.u.ref = rxq->gref; 1990230587Sken gnttab[gnt_idx].dest.domid = otherend_id; 1991230587Sken gnttab[gnt_idx].dest.offset = r_ofs; 1992230587Sken gnttab[gnt_idx].source.u.gmfn = virt_to_mfn( 1993230587Sken mtod(mbuf, vm_offset_t) + m_ofs); 1994230587Sken gnttab[gnt_idx].source.offset = virt_to_offset( 1995230587Sken mtod(mbuf, vm_offset_t) + m_ofs); 1996230587Sken gnttab[gnt_idx].source.domid = DOMID_SELF; 1997230587Sken gnttab[gnt_idx].len = space; 1998230587Sken gnttab[gnt_idx].flags = GNTCOPY_dest_gref; 1999230587Sken 2000230587Sken gnt_idx++; 2001230587Sken 2002230587Sken r_ofs += space; 2003230587Sken m_ofs += space; 2004230587Sken size_remaining -= space; 2005230587Sken if (req_size - r_ofs <= 0) { 2006230587Sken /* Must move to the next rx request */ 2007230587Sken r_ofs = 0; 2008230587Sken r_idx = (r_idx == pkt->car) ? pkt->cdr : r_idx + 1; 2009230587Sken } 2010230587Sken if (mbuf->m_len - m_ofs <= 0) { 2011230587Sken /* Must move to the next mbuf */ 2012230587Sken m_ofs = 0; 2013230587Sken mbuf = mbuf->m_next; 2014230587Sken } 2015181643Skmacy } 2016181643Skmacy 2017230587Sken return gnt_idx; 2018181643Skmacy} 2019181643Skmacy 2020181643Skmacy/** 2021230587Sken * Generates responses for all the requests that constituted pkt. Builds 2022230587Sken * responses and writes them to the ring, but doesn't push the shared ring 2023230587Sken * indices. 2024230587Sken * \param[in] pkt the packet that needs a response 2025230587Sken * \param[in] gnttab The grant copy table corresponding to this packet. 2026230587Sken * Used to determine how many rsp->netif_rx_response_t's to 2027230587Sken * generate. 2028230587Sken * \param[in] n_entries Number of relevant entries in the grant table 2029230587Sken * \param[out] ring Responses go here 2030230587Sken * \return The number of RX requests that were consumed to generate 2031230587Sken * the responses 2032181643Skmacy */ 2033181643Skmacystatic int 2034230587Skenxnb_rxpkt2rsp(const struct xnb_pkt *pkt, const gnttab_copy_table gnttab, 2035230587Sken int n_entries, netif_rx_back_ring_t *ring) 2036181643Skmacy{ 2037230587Sken /* 2038230587Sken * This code makes the following assumptions: 2039230587Sken * * All entries in gnttab set GNTCOPY_dest_gref 2040230587Sken * * The entries in gnttab are grouped by their grefs: any two 2041230587Sken * entries with the same gref must be adjacent 2042230587Sken */ 2043230587Sken int error = 0; 2044230587Sken int gnt_idx, i; 2045230587Sken int n_responses = 0; 2046230587Sken grant_ref_t last_gref = GRANT_REF_INVALID; 2047230587Sken RING_IDX r_idx; 2048181643Skmacy 2049230587Sken KASSERT(gnttab != NULL, ("Received a null granttable copy")); 2050230587Sken 2051230587Sken /* 2052230587Sken * In the event of an error, we only need to send one response to the 2053230587Sken * netfront. In that case, we musn't write any data to the responses 2054230587Sken * after the one we send. So we must loop all the way through gnttab 2055230587Sken * looking for errors before we generate any responses 2056230587Sken * 2057230587Sken * Since we're looping through the grant table anyway, we'll count the 2058230587Sken * number of different gref's in it, which will tell us how many 2059230587Sken * responses to generate 2060230587Sken */ 2061230587Sken for (gnt_idx = 0; gnt_idx < n_entries; gnt_idx++) { 2062230587Sken int16_t status = gnttab[gnt_idx].status; 2063230587Sken if (status != GNTST_okay) { 2064230587Sken DPRINTF( 2065230587Sken "Got error %d for hypervisor gnttab_copy status\n", 2066230587Sken status); 2067230587Sken error = 1; 2068230587Sken break; 2069230587Sken } 2070230587Sken if (gnttab[gnt_idx].dest.u.ref != last_gref) { 2071230587Sken n_responses++; 2072230587Sken last_gref = gnttab[gnt_idx].dest.u.ref; 2073230587Sken } 2074181643Skmacy } 2075181643Skmacy 2076230587Sken if (error != 0) { 2077230587Sken uint16_t id; 2078230587Sken netif_rx_response_t *rsp; 2079230587Sken 2080230587Sken id = RING_GET_REQUEST(ring, ring->rsp_prod_pvt)->id; 2081230587Sken rsp = RING_GET_RESPONSE(ring, ring->rsp_prod_pvt); 2082230587Sken rsp->id = id; 2083230587Sken rsp->status = NETIF_RSP_ERROR; 2084230587Sken n_responses = 1; 2085230587Sken } else { 2086230587Sken gnt_idx = 0; 2087230587Sken const int has_extra = pkt->flags & NETRXF_extra_info; 2088230587Sken if (has_extra != 0) 2089230587Sken n_responses++; 2090181643Skmacy 2091230587Sken for (i = 0; i < n_responses; i++) { 2092230587Sken netif_rx_request_t rxq; 2093230587Sken netif_rx_response_t *rsp; 2094181643Skmacy 2095230587Sken r_idx = ring->rsp_prod_pvt + i; 2096230587Sken /* 2097230587Sken * We copy the structure of rxq instead of making a 2098230587Sken * pointer because it shares the same memory as rsp. 2099230587Sken */ 2100230587Sken rxq = *(RING_GET_REQUEST(ring, r_idx)); 2101230587Sken rsp = RING_GET_RESPONSE(ring, r_idx); 2102230587Sken if (has_extra && (i == 1)) { 2103230587Sken netif_extra_info_t *ext = 2104230587Sken (netif_extra_info_t*)rsp; 2105230587Sken ext->type = XEN_NETIF_EXTRA_TYPE_GSO; 2106230587Sken ext->flags = 0; 2107230587Sken ext->u.gso.size = pkt->extra.u.gso.size; 2108230587Sken ext->u.gso.type = XEN_NETIF_GSO_TYPE_TCPV4; 2109230587Sken ext->u.gso.pad = 0; 2110230587Sken ext->u.gso.features = 0; 2111230587Sken } else { 2112230587Sken rsp->id = rxq.id; 2113230587Sken rsp->status = GNTST_okay; 2114230587Sken rsp->offset = 0; 2115230587Sken rsp->flags = 0; 2116230587Sken if (i < pkt->list_len - 1) 2117230587Sken rsp->flags |= NETRXF_more_data; 2118230587Sken if ((i == 0) && has_extra) 2119230587Sken rsp->flags |= NETRXF_extra_info; 2120230587Sken if ((i == 0) && 2121230587Sken (pkt->flags & NETRXF_data_validated)) { 2122230587Sken rsp->flags |= NETRXF_data_validated; 2123230587Sken rsp->flags |= NETRXF_csum_blank; 2124230587Sken } 2125230587Sken rsp->status = 0; 2126230587Sken for (; gnttab[gnt_idx].dest.u.ref == rxq.gref; 2127230587Sken gnt_idx++) { 2128230587Sken rsp->status += gnttab[gnt_idx].len; 2129230587Sken } 2130230587Sken } 2131230587Sken } 2132181643Skmacy } 2133181643Skmacy 2134230587Sken ring->req_cons += n_responses; 2135230587Sken ring->rsp_prod_pvt += n_responses; 2136230587Sken return n_responses; 2137181643Skmacy} 2138181643Skmacy 2139257515Sglebius#if defined(INET) || defined(INET6) 2140181643Skmacy/** 2141230587Sken * Add IP, TCP, and/or UDP checksums to every mbuf in a chain. The first mbuf 2142230587Sken * in the chain must start with a struct ether_header. 2143230587Sken * 2144230587Sken * XXX This function will perform incorrectly on UDP packets that are split up 2145230587Sken * into multiple ethernet frames. 2146181643Skmacy */ 2147230587Skenstatic void 2148230587Skenxnb_add_mbuf_cksum(struct mbuf *mbufc) 2149181643Skmacy{ 2150230587Sken struct ether_header *eh; 2151230587Sken struct ip *iph; 2152230587Sken uint16_t ether_type; 2153181643Skmacy 2154230587Sken eh = mtod(mbufc, struct ether_header*); 2155230587Sken ether_type = ntohs(eh->ether_type); 2156230587Sken if (ether_type != ETHERTYPE_IP) { 2157230587Sken /* Nothing to calculate */ 2158230587Sken return; 2159230587Sken } 2160181643Skmacy 2161230587Sken iph = (struct ip*)(eh + 1); 2162230587Sken if (mbufc->m_pkthdr.csum_flags & CSUM_IP_VALID) { 2163230587Sken iph->ip_sum = 0; 2164230587Sken iph->ip_sum = in_cksum_hdr(iph); 2165230587Sken } 2166181643Skmacy 2167230587Sken switch (iph->ip_p) { 2168230587Sken case IPPROTO_TCP: 2169230587Sken if (mbufc->m_pkthdr.csum_flags & CSUM_IP_VALID) { 2170230587Sken size_t tcplen = ntohs(iph->ip_len) - sizeof(struct ip); 2171230587Sken struct tcphdr *th = (struct tcphdr*)(iph + 1); 2172230587Sken th->th_sum = in_pseudo(iph->ip_src.s_addr, 2173230587Sken iph->ip_dst.s_addr, htons(IPPROTO_TCP + tcplen)); 2174230587Sken th->th_sum = in_cksum_skip(mbufc, 2175230587Sken sizeof(struct ether_header) + ntohs(iph->ip_len), 2176230587Sken sizeof(struct ether_header) + (iph->ip_hl << 2)); 2177230587Sken } 2178181643Skmacy break; 2179230587Sken case IPPROTO_UDP: 2180230587Sken if (mbufc->m_pkthdr.csum_flags & CSUM_IP_VALID) { 2181230587Sken size_t udplen = ntohs(iph->ip_len) - sizeof(struct ip); 2182230587Sken struct udphdr *uh = (struct udphdr*)(iph + 1); 2183230587Sken uh->uh_sum = in_pseudo(iph->ip_src.s_addr, 2184230587Sken iph->ip_dst.s_addr, htons(IPPROTO_UDP + udplen)); 2185230587Sken uh->uh_sum = in_cksum_skip(mbufc, 2186230587Sken sizeof(struct ether_header) + ntohs(iph->ip_len), 2187230587Sken sizeof(struct ether_header) + (iph->ip_hl << 2)); 2188230587Sken } 2189181643Skmacy break; 2190230587Sken default: 2191181643Skmacy break; 2192181643Skmacy } 2193181643Skmacy} 2194257515Sglebius#endif /* INET || INET6 */ 2195181643Skmacy 2196181643Skmacystatic void 2197230587Skenxnb_stop(struct xnb_softc *xnb) 2198181643Skmacy{ 2199230587Sken struct ifnet *ifp; 2200181643Skmacy 2201230587Sken mtx_assert(&xnb->sc_lock, MA_OWNED); 2202230587Sken ifp = xnb->xnb_ifp; 2203230587Sken ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE); 2204230587Sken if_link_state_change(ifp, LINK_STATE_DOWN); 2205181643Skmacy} 2206181643Skmacy 2207181643Skmacystatic int 2208230587Skenxnb_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data) 2209181643Skmacy{ 2210230587Sken struct xnb_softc *xnb = ifp->if_softc; 2211256868Sbz struct ifreq *ifr = (struct ifreq*) data; 2212230587Sken#ifdef INET 2213230587Sken struct ifaddr *ifa = (struct ifaddr*)data; 2214230587Sken#endif 2215230587Sken int error = 0; 2216181643Skmacy 2217230587Sken switch (cmd) { 2218230587Sken case SIOCSIFFLAGS: 2219230587Sken mtx_lock(&xnb->sc_lock); 2220230587Sken if (ifp->if_flags & IFF_UP) { 2221230587Sken xnb_ifinit_locked(xnb); 2222230587Sken } else { 2223230587Sken if (ifp->if_drv_flags & IFF_DRV_RUNNING) { 2224230587Sken xnb_stop(xnb); 2225230587Sken } 2226230587Sken } 2227230587Sken /* 2228230587Sken * Note: netfront sets a variable named xn_if_flags 2229230587Sken * here, but that variable is never read 2230230587Sken */ 2231230587Sken mtx_unlock(&xnb->sc_lock); 2232230587Sken break; 2233230587Sken case SIOCSIFADDR: 2234230587Sken#ifdef INET 2235230587Sken mtx_lock(&xnb->sc_lock); 2236230587Sken if (ifa->ifa_addr->sa_family == AF_INET) { 2237230587Sken ifp->if_flags |= IFF_UP; 2238230587Sken if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) { 2239230587Sken ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | 2240230587Sken IFF_DRV_OACTIVE); 2241230587Sken if_link_state_change(ifp, 2242230587Sken LINK_STATE_DOWN); 2243230587Sken ifp->if_drv_flags |= IFF_DRV_RUNNING; 2244230587Sken ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 2245230587Sken if_link_state_change(ifp, 2246230587Sken LINK_STATE_UP); 2247230587Sken } 2248230587Sken arp_ifinit(ifp, ifa); 2249230587Sken mtx_unlock(&xnb->sc_lock); 2250230587Sken } else { 2251230587Sken mtx_unlock(&xnb->sc_lock); 2252230587Sken#endif 2253230587Sken error = ether_ioctl(ifp, cmd, data); 2254230587Sken#ifdef INET 2255230587Sken } 2256230587Sken#endif 2257230587Sken break; 2258230587Sken case SIOCSIFCAP: 2259230587Sken mtx_lock(&xnb->sc_lock); 2260230587Sken if (ifr->ifr_reqcap & IFCAP_TXCSUM) { 2261230587Sken ifp->if_capenable |= IFCAP_TXCSUM; 2262230587Sken ifp->if_hwassist |= XNB_CSUM_FEATURES; 2263230587Sken } else { 2264230587Sken ifp->if_capenable &= ~(IFCAP_TXCSUM); 2265230587Sken ifp->if_hwassist &= ~(XNB_CSUM_FEATURES); 2266230587Sken } 2267230587Sken if ((ifr->ifr_reqcap & IFCAP_RXCSUM)) { 2268230587Sken ifp->if_capenable |= IFCAP_RXCSUM; 2269230587Sken } else { 2270230587Sken ifp->if_capenable &= ~(IFCAP_RXCSUM); 2271230587Sken } 2272230587Sken /* 2273230587Sken * TODO enable TSO4 and LRO once we no longer need 2274230587Sken * to calculate checksums in software 2275230587Sken */ 2276230587Sken#if 0 2277230587Sken if (ifr->if_reqcap |= IFCAP_TSO4) { 2278230587Sken if (IFCAP_TXCSUM & ifp->if_capenable) { 2279230587Sken printf("xnb: Xen netif requires that " 2280230587Sken "TXCSUM be enabled in order " 2281230587Sken "to use TSO4\n"); 2282230587Sken error = EINVAL; 2283230587Sken } else { 2284230587Sken ifp->if_capenable |= IFCAP_TSO4; 2285230587Sken ifp->if_hwassist |= CSUM_TSO; 2286230587Sken } 2287230587Sken } else { 2288230587Sken ifp->if_capenable &= ~(IFCAP_TSO4); 2289230587Sken ifp->if_hwassist &= ~(CSUM_TSO); 2290230587Sken } 2291230587Sken if (ifr->ifreqcap |= IFCAP_LRO) { 2292230587Sken ifp->if_capenable |= IFCAP_LRO; 2293230587Sken } else { 2294230587Sken ifp->if_capenable &= ~(IFCAP_LRO); 2295230587Sken } 2296230587Sken#endif 2297230587Sken mtx_unlock(&xnb->sc_lock); 2298230587Sken break; 2299230587Sken case SIOCSIFMTU: 2300230587Sken ifp->if_mtu = ifr->ifr_mtu; 2301230587Sken ifp->if_drv_flags &= ~IFF_DRV_RUNNING; 2302230587Sken xnb_ifinit(xnb); 2303230587Sken break; 2304230587Sken case SIOCADDMULTI: 2305230587Sken case SIOCDELMULTI: 2306230587Sken case SIOCSIFMEDIA: 2307230587Sken case SIOCGIFMEDIA: 2308230587Sken error = ifmedia_ioctl(ifp, ifr, &xnb->sc_media, cmd); 2309230587Sken break; 2310230587Sken default: 2311230587Sken error = ether_ioctl(ifp, cmd, data); 2312230587Sken break; 2313181643Skmacy } 2314230587Sken return (error); 2315230587Sken} 2316181643Skmacy 2317230587Skenstatic void 2318230587Skenxnb_start_locked(struct ifnet *ifp) 2319230587Sken{ 2320230587Sken netif_rx_back_ring_t *rxb; 2321230587Sken struct xnb_softc *xnb; 2322230587Sken struct mbuf *mbufc; 2323230587Sken RING_IDX req_prod_local; 2324181643Skmacy 2325230587Sken xnb = ifp->if_softc; 2326230587Sken rxb = &xnb->ring_configs[XNB_RING_TYPE_RX].back_ring.rx_ring; 2327181643Skmacy 2328230587Sken if (!xnb->carrier) 2329230587Sken return; 2330181643Skmacy 2331230587Sken do { 2332230587Sken int out_of_space = 0; 2333230587Sken int notify; 2334230587Sken req_prod_local = rxb->sring->req_prod; 2335230587Sken xen_rmb(); 2336230587Sken for (;;) { 2337230587Sken int error; 2338181643Skmacy 2339230587Sken IF_DEQUEUE(&ifp->if_snd, mbufc); 2340230587Sken if (mbufc == NULL) 2341230587Sken break; 2342230587Sken error = xnb_send(rxb, xnb->otherend_id, mbufc, 2343230587Sken xnb->rx_gnttab); 2344230587Sken switch (error) { 2345230587Sken case EAGAIN: 2346230587Sken /* 2347230587Sken * Insufficient space in the ring. 2348230587Sken * Requeue pkt and send when space is 2349230587Sken * available. 2350230587Sken */ 2351230587Sken IF_PREPEND(&ifp->if_snd, mbufc); 2352230587Sken /* 2353230587Sken * Perhaps the frontend missed an IRQ 2354230587Sken * and went to sleep. Notify it to wake 2355230587Sken * it up. 2356230587Sken */ 2357230587Sken out_of_space = 1; 2358230587Sken break; 2359181643Skmacy 2360230587Sken case EINVAL: 2361230587Sken /* OS gave a corrupt packet. Drop it.*/ 2362271849Sglebius if_inc_counter(ifp, IFCOUNTER_OERRORS, 1); 2363230587Sken /* FALLTHROUGH */ 2364230587Sken default: 2365230587Sken /* Send succeeded, or packet had error. 2366230587Sken * Free the packet */ 2367271849Sglebius if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1); 2368230587Sken if (mbufc) 2369230587Sken m_freem(mbufc); 2370230587Sken break; 2371230587Sken } 2372230587Sken if (out_of_space != 0) 2373230587Sken break; 2374230587Sken } 2375181643Skmacy 2376230587Sken RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(rxb, notify); 2377230587Sken if ((notify != 0) || (out_of_space != 0)) 2378255040Sgibbs xen_intr_signal(xnb->xen_intr_handle); 2379230587Sken rxb->sring->req_event = req_prod_local + 1; 2380230587Sken xen_mb(); 2381230587Sken } while (rxb->sring->req_prod != req_prod_local) ; 2382181643Skmacy} 2383181643Skmacy 2384230587Sken/** 2385230587Sken * Sends one packet to the ring. Blocks until the packet is on the ring 2386230587Sken * \param[in] mbufc Contains one packet to send. Caller must free 2387230587Sken * \param[in,out] rxb The packet will be pushed onto this ring, but the 2388230587Sken * otherend will not be notified. 2389230587Sken * \param[in] otherend The domain ID of the other end of the connection 2390230587Sken * \retval EAGAIN The ring did not have enough space for the packet. 2391230587Sken * The ring has not been modified 2392230587Sken * \param[in,out] gnttab Pointer to enough memory for a grant table. We make 2393230587Sken * this a function parameter so that we will take less 2394230587Sken * stack space. 2395230587Sken * \retval EINVAL mbufc was corrupt or not convertible into a pkt 2396230587Sken */ 2397181643Skmacystatic int 2398230587Skenxnb_send(netif_rx_back_ring_t *ring, domid_t otherend, const struct mbuf *mbufc, 2399230587Sken gnttab_copy_table gnttab) 2400181643Skmacy{ 2401230587Sken struct xnb_pkt pkt; 2402230587Sken int error, n_entries, n_reqs; 2403230587Sken RING_IDX space; 2404181643Skmacy 2405230587Sken space = ring->sring->req_prod - ring->req_cons; 2406230587Sken error = xnb_mbufc2pkt(mbufc, &pkt, ring->rsp_prod_pvt, space); 2407230587Sken if (error != 0) 2408230587Sken return error; 2409230587Sken n_entries = xnb_rxpkt2gnttab(&pkt, mbufc, gnttab, ring, otherend); 2410230587Sken if (n_entries != 0) { 2411230587Sken int __unused hv_ret = HYPERVISOR_grant_table_op(GNTTABOP_copy, 2412230587Sken gnttab, n_entries); 2413230587Sken KASSERT(hv_ret == 0, ("HYPERVISOR_grant_table_op returned %d\n", 2414230587Sken hv_ret)); 2415181643Skmacy } 2416181643Skmacy 2417230587Sken n_reqs = xnb_rxpkt2rsp(&pkt, gnttab, n_entries, ring); 2418181643Skmacy 2419230587Sken return 0; 2420181643Skmacy} 2421181643Skmacy 2422230587Skenstatic void 2423230587Skenxnb_start(struct ifnet *ifp) 2424181643Skmacy{ 2425230587Sken struct xnb_softc *xnb; 2426230587Sken 2427230587Sken xnb = ifp->if_softc; 2428230587Sken mtx_lock(&xnb->rx_lock); 2429230587Sken xnb_start_locked(ifp); 2430230587Sken mtx_unlock(&xnb->rx_lock); 2431181643Skmacy} 2432181643Skmacy 2433230587Sken/* equivalent of network_open() in Linux */ 2434230587Skenstatic void 2435230587Skenxnb_ifinit_locked(struct xnb_softc *xnb) 2436181643Skmacy{ 2437230587Sken struct ifnet *ifp; 2438181643Skmacy 2439230587Sken ifp = xnb->xnb_ifp; 2440181643Skmacy 2441230587Sken mtx_assert(&xnb->sc_lock, MA_OWNED); 2442181643Skmacy 2443230587Sken if (ifp->if_drv_flags & IFF_DRV_RUNNING) 2444230587Sken return; 2445181643Skmacy 2446230587Sken xnb_stop(xnb); 2447181643Skmacy 2448230587Sken ifp->if_drv_flags |= IFF_DRV_RUNNING; 2449230587Sken ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 2450230587Sken if_link_state_change(ifp, LINK_STATE_UP); 2451181643Skmacy} 2452181643Skmacy 2453230587Sken 2454230587Skenstatic void 2455230587Skenxnb_ifinit(void *xsc) 2456181643Skmacy{ 2457230587Sken struct xnb_softc *xnb = xsc; 2458181643Skmacy 2459230587Sken mtx_lock(&xnb->sc_lock); 2460230587Sken xnb_ifinit_locked(xnb); 2461230587Sken mtx_unlock(&xnb->sc_lock); 2462230587Sken} 2463181643Skmacy 2464230587Sken/** 2465230587Sken * Callback used by the generic networking code to tell us when our carrier 2466230587Sken * state has changed. Since we don't have a physical carrier, we don't care 2467230587Sken */ 2468230587Skenstatic int 2469230587Skenxnb_ifmedia_upd(struct ifnet *ifp) 2470230587Sken{ 2471230587Sken return (0); 2472230587Sken} 2473181643Skmacy 2474230587Sken/** 2475230587Sken * Callback used by the generic networking code to ask us what our carrier 2476230587Sken * state is. Since we don't have a physical carrier, this is very simple 2477230587Sken */ 2478230587Skenstatic void 2479230587Skenxnb_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr) 2480230587Sken{ 2481230587Sken ifmr->ifm_status = IFM_AVALID|IFM_ACTIVE; 2482230587Sken ifmr->ifm_active = IFM_ETHER|IFM_MANUAL; 2483181643Skmacy} 2484181643Skmacy 2485230587Sken 2486230587Sken/*---------------------------- NewBus Registration ---------------------------*/ 2487230587Skenstatic device_method_t xnb_methods[] = { 2488181643Skmacy /* Device interface */ 2489230587Sken DEVMETHOD(device_probe, xnb_probe), 2490230587Sken DEVMETHOD(device_attach, xnb_attach), 2491230587Sken DEVMETHOD(device_detach, xnb_detach), 2492181643Skmacy DEVMETHOD(device_shutdown, bus_generic_shutdown), 2493230587Sken DEVMETHOD(device_suspend, xnb_suspend), 2494230587Sken DEVMETHOD(device_resume, xnb_resume), 2495181643Skmacy 2496230587Sken /* Xenbus interface */ 2497230587Sken DEVMETHOD(xenbus_otherend_changed, xnb_frontend_changed), 2498181643Skmacy 2499230587Sken { 0, 0 } 2500181643Skmacy}; 2501181643Skmacy 2502230587Skenstatic driver_t xnb_driver = { 2503230587Sken "xnb", 2504230587Sken xnb_methods, 2505230587Sken sizeof(struct xnb_softc), 2506230587Sken}; 2507230587Skendevclass_t xnb_devclass; 2508181643Skmacy 2509230587SkenDRIVER_MODULE(xnb, xenbusb_back, xnb_driver, xnb_devclass, 0, 0); 2510181643Skmacy 2511230587Sken 2512230587Sken/*-------------------------- Unit Tests -------------------------------------*/ 2513230587Sken#ifdef XNB_DEBUG 2514230587Sken#include "netback_unit_tests.c" 2515230587Sken#endif 2516