2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2009-2011 Spectra Logic Corporation
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions, and the following disclaimer,
12 * without modification.
13 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
14 * substantially similar to the "NO WARRANTY" disclaimer below
15 * ("Disclaimer") and any redistribution must be conditioned upon
16 * including a substantially similar Disclaimer requirement for further
17 * binary redistribution.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
28 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
29 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGES.
32 * Authors: Justin T. Gibbs (Spectra Logic Corporation)
33 * Alan Somers (Spectra Logic Corporation)
34 * John Suykerbuyk (Spectra Logic Corporation)
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
43 * \brief Device driver supporting the vending of network access
44 * from this FreeBSD domain to other domains.
47 #include "opt_inet6.h"
51 #include <sys/param.h>
52 #include <sys/kernel.h>
55 #include <sys/module.h>
57 #include <sys/socket.h>
58 #include <sys/sockio.h>
59 #include <sys/sysctl.h>
62 #include <net/if_var.h>
63 #include <net/if_arp.h>
64 #include <net/ethernet.h>
65 #include <net/if_dl.h>
66 #include <net/if_media.h>
67 #include <net/if_types.h>
69 #include <netinet/in.h>
70 #include <netinet/ip.h>
71 #include <netinet/if_ether.h>
72 #if __FreeBSD_version >= 700000
73 #include <netinet/tcp.h>
75 #include <netinet/ip_icmp.h>
76 #include <netinet/udp.h>
77 #include <machine/in_cksum.h>
81 #include <vm/vm_extern.h>
82 #include <vm/vm_kern.h>
84 #include <machine/_inttypes.h>
86 #include <xen/xen-os.h>
87 #include <xen/hypervisor.h>
88 #include <xen/xen_intr.h>
89 #include <xen/interface/io/netif.h>
90 #include <xen/xenbus/xenbusvar.h>
92 /*--------------------------- Compile-time Tunables --------------------------*/
94 /*---------------------------------- Macros ----------------------------------*/
96 * Custom malloc type for all driver allocations.
98 static MALLOC_DEFINE(M_XENNETBACK, "xnb", "Xen Net Back Driver Data");
100 #define XNB_SG 1 /* netback driver supports feature-sg */
101 #define XNB_GSO_TCPV4 0 /* netback driver supports feature-gso-tcpv4 */
102 #define XNB_RX_COPY 1 /* netback driver supports feature-rx-copy */
103 #define XNB_RX_FLIP 0 /* netback driver does not support feature-rx-flip */
106 #define XNB_DEBUG /* hardcode on during development */
109 #define DPRINTF(fmt, args...) \
110 printf("xnb(%s:%d): " fmt, __FUNCTION__, __LINE__, ##args)
112 #define DPRINTF(fmt, args...) do {} while (0)
115 /* Default length for stack-allocated grant tables */
116 #define GNTTAB_LEN (64)
118 /* Features supported by all backends. TSO and LRO can be negotiated */
119 #define XNB_CSUM_FEATURES (CSUM_TCP | CSUM_UDP)
121 #define NET_TX_RING_SIZE __RING_SIZE((netif_tx_sring_t *)0, PAGE_SIZE)
122 #define NET_RX_RING_SIZE __RING_SIZE((netif_rx_sring_t *)0, PAGE_SIZE)
125 * Two argument version of the standard macro. Second argument is a tentative
128 #define RING_HAS_UNCONSUMED_REQUESTS_2(_r, cons) ({ \
129 unsigned int req = (_r)->sring->req_prod - cons; \
130 unsigned int rsp = RING_SIZE(_r) - \
131 (cons - (_r)->rsp_prod_pvt); \
132 req < rsp ? req : rsp; \
135 #define virt_to_mfn(x) (vtophys(x) >> PAGE_SHIFT)
136 #define virt_to_offset(x) ((x) & (PAGE_SIZE - 1))
139 * Predefined array type of grant table copy descriptors. Used to pass around
140 * statically allocated memory structures.
142 typedef struct gnttab_copy gnttab_copy_table[GNTTAB_LEN];
144 /*--------------------------- Forward Declarations ---------------------------*/
148 static void xnb_attach_failed(struct xnb_softc *xnb,
149 int err, const char *fmt, ...)
151 static int xnb_shutdown(struct xnb_softc *xnb);
152 static int create_netdev(device_t dev);
153 static int xnb_detach(device_t dev);
154 static int xnb_ifmedia_upd(struct ifnet *ifp);
155 static void xnb_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr);
156 static void xnb_intr(void *arg);
157 static int xnb_send(netif_rx_back_ring_t *rxb, domid_t otherend,
158 const struct mbuf *mbufc, gnttab_copy_table gnttab);
159 static int xnb_recv(netif_tx_back_ring_t *txb, domid_t otherend,
160 struct mbuf **mbufc, struct ifnet *ifnet,
161 gnttab_copy_table gnttab);
162 static int xnb_ring2pkt(struct xnb_pkt *pkt,
163 const netif_tx_back_ring_t *tx_ring,
165 static void xnb_txpkt2rsp(const struct xnb_pkt *pkt,
166 netif_tx_back_ring_t *ring, int error);
167 static struct mbuf *xnb_pkt2mbufc(const struct xnb_pkt *pkt, struct ifnet *ifp);
168 static int xnb_txpkt2gnttab(const struct xnb_pkt *pkt,
170 gnttab_copy_table gnttab,
171 const netif_tx_back_ring_t *txb,
172 domid_t otherend_id);
173 static void xnb_update_mbufc(struct mbuf *mbufc,
174 const gnttab_copy_table gnttab, int n_entries);
175 static int xnb_mbufc2pkt(const struct mbuf *mbufc,
177 RING_IDX start, int space);
178 static int xnb_rxpkt2gnttab(const struct xnb_pkt *pkt,
179 const struct mbuf *mbufc,
180 gnttab_copy_table gnttab,
181 const netif_rx_back_ring_t *rxb,
182 domid_t otherend_id);
183 static int xnb_rxpkt2rsp(const struct xnb_pkt *pkt,
184 const gnttab_copy_table gnttab, int n_entries,
185 netif_rx_back_ring_t *ring);
186 static void xnb_stop(struct xnb_softc*);
187 static int xnb_ioctl(struct ifnet*, u_long, caddr_t);
188 static void xnb_start_locked(struct ifnet*);
189 static void xnb_start(struct ifnet*);
190 static void xnb_ifinit_locked(struct xnb_softc*);
191 static void xnb_ifinit(void*);
193 static int xnb_unit_test_main(SYSCTL_HANDLER_ARGS);
194 static int xnb_dump_rings(SYSCTL_HANDLER_ARGS);
196 #if defined(INET) || defined(INET6)
197 static void xnb_add_mbuf_cksum(struct mbuf *mbufc);
199 /*------------------------------ Data Structures -----------------------------*/
203 * Representation of a xennet packet. Simplified version of a packet as
204 * stored in the Xen tx ring. Applicable to both RX and TX packets
208 * Array index of the first data-bearing (eg, not extra info) entry
214 * Array index of the second data-bearing entry for this packet.
215 * Invalid if the packet has only one data-bearing entry. If the
216 * packet has more than two data-bearing entries, then the second
217 * through the last will be sequential modulo the ring size
222 * Optional extra info. Only valid if flags contains
223 * NETTXF_extra_info. Note that extra.type will always be
224 * XEN_NETIF_EXTRA_TYPE_GSO. Currently, no known netfront or netback
225 * driver will ever set XEN_NETIF_EXTRA_TYPE_MCAST_*
227 netif_extra_info_t extra;
229 /** Size of entire packet in bytes. */
232 /** The size of the first entry's data in bytes */
236 * Either NETTXF_ or NETRXF_ flags. Note that the flag values are
237 * not the same for TX and RX packets
242 * The number of valid data-bearing entries (either netif_tx_request's
243 * or netif_rx_response's) in the packet. If this is 0, it means the
244 * entire packet is invalid.
248 /** There was an error processing the packet */
252 /** xnb_pkt method: initialize it */
254 xnb_pkt_initialize(struct xnb_pkt *pxnb)
256 bzero(pxnb, sizeof(*pxnb));
259 /** xnb_pkt method: mark the packet as valid */
261 xnb_pkt_validate(struct xnb_pkt *pxnb)
266 /** xnb_pkt method: mark the packet as invalid */
268 xnb_pkt_invalidate(struct xnb_pkt *pxnb)
273 /** xnb_pkt method: Check whether the packet is valid */
275 xnb_pkt_is_valid(const struct xnb_pkt *pxnb)
277 return (! pxnb->error);
281 /** xnb_pkt method: print the packet's contents in human-readable format*/
283 xnb_dump_pkt(const struct xnb_pkt *pkt) {
285 DPRINTF("Was passed a null pointer.\n");
288 DPRINTF("pkt address= %p\n", pkt);
289 DPRINTF("pkt->size=%d\n", pkt->size);
290 DPRINTF("pkt->car_size=%d\n", pkt->car_size);
291 DPRINTF("pkt->flags=0x%04x\n", pkt->flags);
292 DPRINTF("pkt->list_len=%d\n", pkt->list_len);
293 /* DPRINTF("pkt->extra"); TODO */
294 DPRINTF("pkt->car=%d\n", pkt->car);
295 DPRINTF("pkt->cdr=%d\n", pkt->cdr);
296 DPRINTF("pkt->error=%d\n", pkt->error);
298 #endif /* XNB_DEBUG */
301 xnb_dump_txreq(RING_IDX idx, const struct netif_tx_request *txreq)
304 DPRINTF("netif_tx_request index =%u\n", idx);
305 DPRINTF("netif_tx_request.gref =%u\n", txreq->gref);
306 DPRINTF("netif_tx_request.offset=%hu\n", txreq->offset);
307 DPRINTF("netif_tx_request.flags =%hu\n", txreq->flags);
308 DPRINTF("netif_tx_request.id =%hu\n", txreq->id);
309 DPRINTF("netif_tx_request.size =%hu\n", txreq->size);
315 * \brief Configuration data for a shared memory request ring
316 * used to communicate with the front-end client of this
319 struct xnb_ring_config {
321 * Runtime structures for ring access. Unfortunately, TX and RX rings
322 * use different data structures, and that cannot be changed since it
323 * is part of the interdomain protocol.
326 netif_rx_back_ring_t rx_ring;
327 netif_tx_back_ring_t tx_ring;
331 * The device bus address returned by the hypervisor when
332 * mapping the ring and required to unmap it when a connection
337 /** The pseudo-physical address where ring memory is mapped.*/
340 /** KVA address where ring memory is mapped. */
344 * Grant table handles, one per-ring page, returned by the
345 * hyperpervisor upon mapping of the ring and required to
346 * unmap it when a connection is torn down.
348 grant_handle_t handle;
350 /** The number of ring pages mapped for the current connection. */
354 * The grant references, one per-ring page, supplied by the
355 * front-end, allowing us to reference the ring pages in the
356 * front-end's domain and to map these pages into our own domain.
358 grant_ref_t ring_ref;
362 * Per-instance connection state flags.
366 /** Communication with the front-end has been established. */
367 XNBF_RING_CONNECTED = 0x01,
370 * Front-end requests exist in the ring and are waiting for
371 * xnb_xen_req objects to free up.
373 XNBF_RESOURCE_SHORTAGE = 0x02,
375 /** Connection teardown has started. */
376 XNBF_SHUTDOWN = 0x04,
378 /** A thread is already performing shutdown processing. */
379 XNBF_IN_SHUTDOWN = 0x08
383 * Types of rings. Used for array indices and to identify a ring's control
384 * data structure type
387 XNB_RING_TYPE_TX = 0, /* ID of TX rings, used for array indices */
388 XNB_RING_TYPE_RX = 1, /* ID of RX rings, used for array indices */
393 * Per-instance configuration data.
396 /** NewBus device corresponding to this instance. */
399 /* Media related fields */
401 /** Generic network media state */
402 struct ifmedia sc_media;
404 /** Media carrier info */
405 struct ifnet *xnb_ifp;
407 /** Our own private carrier state */
410 /** Device MAC Address */
411 uint8_t mac[ETHER_ADDR_LEN];
413 /* Xen related fields */
416 * \brief The netif protocol abi in effect.
418 * There are situations where the back and front ends can
419 * have a different, native abi (e.g. intel x86_64 and
420 * 32bit x86 domains on the same machine). The back-end
421 * always accommodates the front-end's native abi. That
422 * value is pulled from the XenStore and recorded here.
427 * Name of the bridge to which this VIF is connected, if any
428 * This field is dynamically allocated by xenbus and must be free()ed
429 * when no longer needed
433 /** The interrupt driven even channel used to signal ring events. */
434 evtchn_port_t evtchn;
436 /** Xen device handle.*/
439 /** Handle to the communication ring event channel. */
440 xen_intr_handle_t xen_intr_handle;
443 * \brief Cached value of the front-end's domain id.
445 * This value is used at once for each mapped page in
446 * a transaction. We cache it to avoid incuring the
447 * cost of an ivar access every time this is needed.
452 * Undocumented frontend feature. Has something to do with
456 /** Undocumented frontend feature */
458 /** Undocumented frontend feature */
460 /** Can checksum TCP/UDP over IPv4 */
463 /* Implementation related fields */
465 * Preallocated grant table copy descriptor for RX operations.
466 * Access must be protected by rx_lock
468 gnttab_copy_table rx_gnttab;
471 * Preallocated grant table copy descriptor for TX operations.
472 * Access must be protected by tx_lock
474 gnttab_copy_table tx_gnttab;
477 * Resource representing allocated physical address space
478 * associated with our per-instance kva region.
480 struct resource *pseudo_phys_res;
482 /** Resource id for allocated physical address space. */
483 int pseudo_phys_res_id;
485 /** Ring mapping and interrupt configuration data. */
486 struct xnb_ring_config ring_configs[XNB_NUM_RING_TYPES];
489 * Global pool of kva used for mapping remote domain ring
490 * and I/O transaction data.
494 /** Pseudo-physical address corresponding to kva. */
495 uint64_t gnt_base_addr;
497 /** Various configuration and state bit flags. */
500 /** Mutex protecting per-instance data in the receive path. */
503 /** Mutex protecting per-instance data in the softc structure. */
506 /** Mutex protecting per-instance data in the transmit path. */
509 /** The size of the global kva pool. */
512 /** Name of the interface */
513 char if_name[IFNAMSIZ];
516 /*---------------------------- Debugging functions ---------------------------*/
519 xnb_dump_gnttab_copy(const struct gnttab_copy *entry)
522 printf("NULL grant table pointer\n");
526 if (entry->flags & GNTCOPY_dest_gref)
527 printf("gnttab dest ref=\t%u\n", entry->dest.u.ref);
529 printf("gnttab dest gmfn=\t%"PRI_xen_pfn"\n",
531 printf("gnttab dest offset=\t%hu\n", entry->dest.offset);
532 printf("gnttab dest domid=\t%hu\n", entry->dest.domid);
533 if (entry->flags & GNTCOPY_source_gref)
534 printf("gnttab source ref=\t%u\n", entry->source.u.ref);
536 printf("gnttab source gmfn=\t%"PRI_xen_pfn"\n",
537 entry->source.u.gmfn);
538 printf("gnttab source offset=\t%hu\n", entry->source.offset);
539 printf("gnttab source domid=\t%hu\n", entry->source.domid);
540 printf("gnttab len=\t%hu\n", entry->len);
541 printf("gnttab flags=\t%hu\n", entry->flags);
542 printf("gnttab status=\t%hd\n", entry->status);
546 xnb_dump_rings(SYSCTL_HANDLER_ARGS)
548 static char results[720];
549 struct xnb_softc const* xnb = (struct xnb_softc*)arg1;
550 netif_rx_back_ring_t const* rxb =
551 &xnb->ring_configs[XNB_RING_TYPE_RX].back_ring.rx_ring;
552 netif_tx_back_ring_t const* txb =
553 &xnb->ring_configs[XNB_RING_TYPE_TX].back_ring.tx_ring;
555 /* empty the result strings */
558 if ( !txb || !txb->sring || !rxb || !rxb->sring )
559 return (SYSCTL_OUT(req, results, strnlen(results, 720)));
561 snprintf(results, 720,
562 "\n\t%35s %18s\n" /* TX, RX */
563 "\t%16s %18d %18d\n" /* req_cons */
564 "\t%16s %18d %18d\n" /* nr_ents */
565 "\t%16s %18d %18d\n" /* rsp_prod_pvt */
566 "\t%16s %18p %18p\n" /* sring */
567 "\t%16s %18d %18d\n" /* req_prod */
568 "\t%16s %18d %18d\n" /* req_event */
569 "\t%16s %18d %18d\n" /* rsp_prod */
570 "\t%16s %18d %18d\n", /* rsp_event */
572 "req_cons", txb->req_cons, rxb->req_cons,
573 "nr_ents", txb->nr_ents, rxb->nr_ents,
574 "rsp_prod_pvt", txb->rsp_prod_pvt, rxb->rsp_prod_pvt,
575 "sring", txb->sring, rxb->sring,
576 "sring->req_prod", txb->sring->req_prod, rxb->sring->req_prod,
577 "sring->req_event", txb->sring->req_event, rxb->sring->req_event,
578 "sring->rsp_prod", txb->sring->rsp_prod, rxb->sring->rsp_prod,
579 "sring->rsp_event", txb->sring->rsp_event, rxb->sring->rsp_event);
581 return (SYSCTL_OUT(req, results, strnlen(results, 720)));
585 xnb_dump_mbuf(const struct mbuf *m)
592 printf("xnb_dump_mbuf:\n");
593 if (m->m_flags & M_PKTHDR) {
594 printf(" flowid=%10d, csum_flags=%#8x, csum_data=%#8x, "
596 m->m_pkthdr.flowid, (int)m->m_pkthdr.csum_flags,
597 m->m_pkthdr.csum_data, m->m_pkthdr.tso_segsz);
598 printf(" rcvif=%16p, len=%19d\n",
599 m->m_pkthdr.rcvif, m->m_pkthdr.len);
601 printf(" m_next=%16p, m_nextpk=%16p, m_data=%16p\n",
602 m->m_next, m->m_nextpkt, m->m_data);
603 printf(" m_len=%17d, m_flags=%#15x, m_type=%18u\n",
604 m->m_len, m->m_flags, m->m_type);
607 d = mtod(m, uint8_t*);
611 for (i = 0; (i < 16) && (len > 0); i++, len--) {
612 printf("%02hhx ", *(d++));
617 #endif /* XNB_DEBUG */
619 /*------------------------ Inter-Domain Communication ------------------------*/
621 * Free dynamically allocated KVA or pseudo-physical address allocations.
623 * \param xnb Per-instance xnb configuration structure.
626 xnb_free_communication_mem(struct xnb_softc *xnb)
629 if (xnb->pseudo_phys_res != NULL) {
630 xenmem_free(xnb->dev, xnb->pseudo_phys_res_id,
631 xnb->pseudo_phys_res);
632 xnb->pseudo_phys_res = NULL;
636 xnb->gnt_base_addr = 0;
640 * Cleanup all inter-domain communication mechanisms.
642 * \param xnb Per-instance xnb configuration structure.
645 xnb_disconnect(struct xnb_softc *xnb)
647 struct gnttab_unmap_grant_ref gnts[XNB_NUM_RING_TYPES];
651 if (xnb->xen_intr_handle != NULL)
652 xen_intr_unbind(&xnb->xen_intr_handle);
655 * We may still have another thread currently processing requests. We
656 * must acquire the rx and tx locks to make sure those threads are done,
657 * but we can release those locks as soon as we acquire them, because no
658 * more interrupts will be arriving.
660 mtx_lock(&xnb->tx_lock);
661 mtx_unlock(&xnb->tx_lock);
662 mtx_lock(&xnb->rx_lock);
663 mtx_unlock(&xnb->rx_lock);
665 /* Free malloc'd softc member variables */
666 if (xnb->bridge != NULL) {
667 free(xnb->bridge, M_XENSTORE);
671 /* All request processing has stopped, so unmap the rings */
672 for (i=0; i < XNB_NUM_RING_TYPES; i++) {
673 gnts[i].host_addr = xnb->ring_configs[i].gnt_addr;
674 gnts[i].dev_bus_addr = xnb->ring_configs[i].bus_addr;
675 gnts[i].handle = xnb->ring_configs[i].handle;
677 error = HYPERVISOR_grant_table_op(GNTTABOP_unmap_grant_ref, gnts,
679 KASSERT(error == 0, ("Grant table unmap op failed (%d)", error));
681 xnb_free_communication_mem(xnb);
683 * Zero the ring config structs because the pointers, handles, and
684 * grant refs contained therein are no longer valid.
686 bzero(&xnb->ring_configs[XNB_RING_TYPE_TX],
687 sizeof(struct xnb_ring_config));
688 bzero(&xnb->ring_configs[XNB_RING_TYPE_RX],
689 sizeof(struct xnb_ring_config));
691 xnb->flags &= ~XNBF_RING_CONNECTED;
696 * Map a single shared memory ring into domain local address space and
697 * initialize its control structure
699 * \param xnb Per-instance xnb configuration structure
700 * \param ring_type Array index of this ring in the xnb's array of rings
704 xnb_connect_ring(struct xnb_softc *xnb, xnb_ring_type_t ring_type)
706 struct gnttab_map_grant_ref gnt;
707 struct xnb_ring_config *ring = &xnb->ring_configs[ring_type];
710 /* TX ring type = 0, RX =1 */
711 ring->va = xnb->kva + ring_type * PAGE_SIZE;
712 ring->gnt_addr = xnb->gnt_base_addr + ring_type * PAGE_SIZE;
714 gnt.host_addr = ring->gnt_addr;
715 gnt.flags = GNTMAP_host_map;
716 gnt.ref = ring->ring_ref;
717 gnt.dom = xnb->otherend_id;
719 error = HYPERVISOR_grant_table_op(GNTTABOP_map_grant_ref, &gnt, 1);
721 panic("netback: Ring page grant table op failed (%d)", error);
723 if (gnt.status != 0) {
726 xenbus_dev_fatal(xnb->dev, error,
727 "Ring shared page mapping failed. "
728 "Status %d.", gnt.status);
730 ring->handle = gnt.handle;
731 ring->bus_addr = gnt.dev_bus_addr;
733 if (ring_type == XNB_RING_TYPE_TX) {
734 BACK_RING_INIT(&ring->back_ring.tx_ring,
735 (netif_tx_sring_t*)ring->va,
736 ring->ring_pages * PAGE_SIZE);
737 } else if (ring_type == XNB_RING_TYPE_RX) {
738 BACK_RING_INIT(&ring->back_ring.rx_ring,
739 (netif_rx_sring_t*)ring->va,
740 ring->ring_pages * PAGE_SIZE);
742 xenbus_dev_fatal(xnb->dev, error,
743 "Unknown ring type %d", ring_type);
751 * Setup the shared memory rings and bind an interrupt to the event channel
752 * used to notify us of ring changes.
754 * \param xnb Per-instance xnb configuration structure.
757 xnb_connect_comms(struct xnb_softc *xnb)
762 if ((xnb->flags & XNBF_RING_CONNECTED) != 0)
766 * Kva for our rings are at the tail of the region of kva allocated
767 * by xnb_alloc_communication_mem().
769 for (i=0; i < XNB_NUM_RING_TYPES; i++) {
770 error = xnb_connect_ring(xnb, i);
775 xnb->flags |= XNBF_RING_CONNECTED;
777 error = xen_intr_bind_remote_port(xnb->dev,
781 xnb_intr, /*arg*/xnb,
782 INTR_TYPE_BIO | INTR_MPSAFE,
783 &xnb->xen_intr_handle);
785 (void)xnb_disconnect(xnb);
786 xenbus_dev_fatal(xnb->dev, error, "binding event channel");
790 DPRINTF("rings connected!\n");
796 * Size KVA and pseudo-physical address allocations based on negotiated
797 * values for the size and number of I/O requests, and the size of our
798 * communication ring.
800 * \param xnb Per-instance xnb configuration structure.
802 * These address spaces are used to dynamically map pages in the
803 * front-end's domain into our own.
806 xnb_alloc_communication_mem(struct xnb_softc *xnb)
811 for (i=0; i < XNB_NUM_RING_TYPES; i++) {
812 xnb->kva_size += xnb->ring_configs[i].ring_pages * PAGE_SIZE;
816 * Reserve a range of pseudo physical memory that we can map
817 * into kva. These pages will only be backed by machine
818 * pages ("real memory") during the lifetime of front-end requests
819 * via grant table operations. We will map the netif tx and rx rings
822 xnb->pseudo_phys_res_id = 0;
823 xnb->pseudo_phys_res = xenmem_alloc(xnb->dev, &xnb->pseudo_phys_res_id,
825 if (xnb->pseudo_phys_res == NULL) {
829 xnb->kva = (vm_offset_t)rman_get_virtual(xnb->pseudo_phys_res);
830 xnb->gnt_base_addr = rman_get_start(xnb->pseudo_phys_res);
835 * Collect information from the XenStore related to our device and its frontend
837 * \param xnb Per-instance xnb configuration structure.
840 xnb_collect_xenstore_info(struct xnb_softc *xnb)
843 * \todo Linux collects the following info. We should collect most
845 * "feature-rx-notify"
847 const char *otherend_path;
848 const char *our_path;
850 unsigned int rx_copy, bridge_len;
851 uint8_t no_csum_offload;
853 otherend_path = xenbus_get_otherend_path(xnb->dev);
854 our_path = xenbus_get_node(xnb->dev);
856 /* Collect the critical communication parameters */
857 err = xs_gather(XST_NIL, otherend_path,
858 "tx-ring-ref", "%l" PRIu32,
859 &xnb->ring_configs[XNB_RING_TYPE_TX].ring_ref,
860 "rx-ring-ref", "%l" PRIu32,
861 &xnb->ring_configs[XNB_RING_TYPE_RX].ring_ref,
862 "event-channel", "%" PRIu32, &xnb->evtchn,
865 xenbus_dev_fatal(xnb->dev, err,
866 "Unable to retrieve ring information from "
867 "frontend %s. Unable to connect.",
872 /* Collect the handle from xenstore */
873 err = xs_scanf(XST_NIL, our_path, "handle", NULL, "%li", &xnb->handle);
875 xenbus_dev_fatal(xnb->dev, err,
876 "Error reading handle from frontend %s. "
877 "Unable to connect.", otherend_path);
881 * Collect the bridgename, if any. We do not need bridge_len; we just
884 err = xs_read(XST_NIL, our_path, "bridge", &bridge_len,
885 (void**)&xnb->bridge);
890 * Does the frontend request that we use rx copy? If not, return an
891 * error because this driver only supports rx copy.
893 err = xs_scanf(XST_NIL, otherend_path, "request-rx-copy", NULL,
894 "%" PRIu32, &rx_copy);
900 xenbus_dev_fatal(xnb->dev, err, "reading %s/request-rx-copy",
905 * \todo: figure out the exact meaning of this feature, and when
906 * the frontend will set it to true. It should be set to true
910 /* return EOPNOTSUPP;*/
912 /** \todo Collect the rx notify feature */
914 /* Collect the feature-sg. */
915 if (xs_scanf(XST_NIL, otherend_path, "feature-sg", NULL,
916 "%hhu", &xnb->can_sg) < 0)
919 /* Collect remaining frontend features */
920 if (xs_scanf(XST_NIL, otherend_path, "feature-gso-tcpv4", NULL,
921 "%hhu", &xnb->gso) < 0)
924 if (xs_scanf(XST_NIL, otherend_path, "feature-gso-tcpv4-prefix", NULL,
925 "%hhu", &xnb->gso_prefix) < 0)
928 if (xs_scanf(XST_NIL, otherend_path, "feature-no-csum-offload", NULL,
929 "%hhu", &no_csum_offload) < 0)
931 xnb->ip_csum = (no_csum_offload == 0);
937 * Supply information about the physical device to the frontend
940 * \param xnb Per-instance xnb configuration structure.
943 xnb_publish_backend_info(struct xnb_softc *xnb)
945 struct xs_transaction xst;
946 const char *our_path;
949 our_path = xenbus_get_node(xnb->dev);
952 error = xs_transaction_start(&xst);
954 xenbus_dev_fatal(xnb->dev, error,
955 "Error publishing backend info "
956 "(start transaction)");
960 error = xs_printf(xst, our_path, "feature-sg",
965 error = xs_printf(xst, our_path, "feature-gso-tcpv4",
966 "%d", XNB_GSO_TCPV4);
970 error = xs_printf(xst, our_path, "feature-rx-copy",
975 error = xs_printf(xst, our_path, "feature-rx-flip",
980 error = xs_transaction_end(xst, 0);
981 if (error != 0 && error != EAGAIN) {
982 xenbus_dev_fatal(xnb->dev, error, "ending transaction");
986 } while (error == EAGAIN);
992 * Connect to our netfront peer now that it has completed publishing
993 * its configuration into the XenStore.
995 * \param xnb Per-instance xnb configuration structure.
998 xnb_connect(struct xnb_softc *xnb)
1002 if (xenbus_get_state(xnb->dev) == XenbusStateConnected)
1005 if (xnb_collect_xenstore_info(xnb) != 0)
1008 xnb->flags &= ~XNBF_SHUTDOWN;
1010 /* Read front end configuration. */
1012 /* Allocate resources whose size depends on front-end configuration. */
1013 error = xnb_alloc_communication_mem(xnb);
1015 xenbus_dev_fatal(xnb->dev, error,
1016 "Unable to allocate communication memory");
1021 * Connect communication channel.
1023 error = xnb_connect_comms(xnb);
1025 /* Specific errors are reported by xnb_connect_comms(). */
1030 /* Ready for I/O. */
1031 xenbus_set_state(xnb->dev, XenbusStateConnected);
1034 /*-------------------------- Device Teardown Support -------------------------*/
1036 * Perform device shutdown functions.
1038 * \param xnb Per-instance xnb configuration structure.
1040 * Mark this instance as shutting down, wait for any active requests
1041 * to drain, disconnect from the front-end, and notify any waiters (e.g.
1042 * a thread invoking our detach method) that detach can now proceed.
1045 xnb_shutdown(struct xnb_softc *xnb)
1048 * Due to the need to drop our mutex during some
1049 * xenbus operations, it is possible for two threads
1050 * to attempt to close out shutdown processing at
1051 * the same time. Tell the caller that hits this
1052 * race to try back later.
1054 if ((xnb->flags & XNBF_IN_SHUTDOWN) != 0)
1057 xnb->flags |= XNBF_SHUTDOWN;
1059 xnb->flags |= XNBF_IN_SHUTDOWN;
1061 mtx_unlock(&xnb->sc_lock);
1062 /* Free the network interface */
1064 if (xnb->xnb_ifp != NULL) {
1065 ether_ifdetach(xnb->xnb_ifp);
1066 if_free(xnb->xnb_ifp);
1067 xnb->xnb_ifp = NULL;
1069 mtx_lock(&xnb->sc_lock);
1071 xnb_disconnect(xnb);
1073 mtx_unlock(&xnb->sc_lock);
1074 if (xenbus_get_state(xnb->dev) < XenbusStateClosing)
1075 xenbus_set_state(xnb->dev, XenbusStateClosing);
1076 mtx_lock(&xnb->sc_lock);
1078 xnb->flags &= ~XNBF_IN_SHUTDOWN;
1081 /* Indicate to xnb_detach() that is it safe to proceed. */
1088 * Report an attach time error to the console and Xen, and cleanup
1089 * this instance by forcing immediate detach processing.
1091 * \param xnb Per-instance xnb configuration structure.
1092 * \param err Errno describing the error.
1093 * \param fmt Printf style format and arguments
1096 xnb_attach_failed(struct xnb_softc *xnb, int err, const char *fmt, ...)
1102 va_copy(ap_hotplug, ap);
1103 xs_vprintf(XST_NIL, xenbus_get_node(xnb->dev),
1104 "hotplug-error", fmt, ap_hotplug);
1106 (void)xs_printf(XST_NIL, xenbus_get_node(xnb->dev),
1107 "hotplug-status", "error");
1109 xenbus_dev_vfatal(xnb->dev, err, fmt, ap);
1112 (void)xs_printf(XST_NIL, xenbus_get_node(xnb->dev), "online", "0");
1113 xnb_detach(xnb->dev);
1116 /*---------------------------- NewBus Entrypoints ----------------------------*/
1118 * Inspect a XenBus device and claim it if is of the appropriate type.
1120 * \param dev NewBus device object representing a candidate XenBus device.
1122 * \return 0 for success, errno codes for failure.
1125 xnb_probe(device_t dev)
1127 if (!strcmp(xenbus_get_type(dev), "vif")) {
1128 DPRINTF("Claiming device %d, %s\n", device_get_unit(dev),
1129 devclass_get_name(device_get_devclass(dev)));
1130 device_set_desc(dev, "Backend Virtual Network Device");
1138 * Setup sysctl variables to control various Network Back parameters.
1140 * \param xnb Xen Net Back softc.
1144 xnb_setup_sysctl(struct xnb_softc *xnb)
1146 struct sysctl_ctx_list *sysctl_ctx = NULL;
1147 struct sysctl_oid *sysctl_tree = NULL;
1149 sysctl_ctx = device_get_sysctl_ctx(xnb->dev);
1150 if (sysctl_ctx == NULL)
1153 sysctl_tree = device_get_sysctl_tree(xnb->dev);
1154 if (sysctl_tree == NULL)
1158 SYSCTL_ADD_PROC(sysctl_ctx,
1159 SYSCTL_CHILDREN(sysctl_tree),
1161 "unit_test_results",
1162 CTLTYPE_STRING | CTLFLAG_RD,
1167 "Results of builtin unit tests");
1169 SYSCTL_ADD_PROC(sysctl_ctx,
1170 SYSCTL_CHILDREN(sysctl_tree),
1173 CTLTYPE_STRING | CTLFLAG_RD,
1178 "Xennet Back Rings");
1179 #endif /* XNB_DEBUG */
1183 * Create a network device.
1184 * @param handle device handle
1187 create_netdev(device_t dev)
1190 struct xnb_softc *xnb;
1194 xnb = device_get_softc(dev);
1195 mtx_init(&xnb->sc_lock, "xnb_softc", "xen netback softc lock", MTX_DEF);
1196 mtx_init(&xnb->tx_lock, "xnb_tx", "xen netback tx lock", MTX_DEF);
1197 mtx_init(&xnb->rx_lock, "xnb_rx", "xen netback rx lock", MTX_DEF);
1201 ifmedia_init(&xnb->sc_media, 0, xnb_ifmedia_upd, xnb_ifmedia_sts);
1202 ifmedia_add(&xnb->sc_media, IFM_ETHER|IFM_MANUAL, 0, NULL);
1203 ifmedia_set(&xnb->sc_media, IFM_ETHER|IFM_MANUAL);
1206 * Set the MAC address to a dummy value (00:00:00:00:00),
1207 * if the MAC address of the host-facing interface is set
1208 * to the same as the guest-facing one (the value found in
1209 * xenstore), the bridge would stop delivering packets to
1210 * us because it would see that the destination address of
1211 * the packet is the same as the interface, and so the bridge
1212 * would expect the packet has already been delivered locally
1213 * (and just drop it).
1215 bzero(&xnb->mac[0], sizeof(xnb->mac));
1217 /* The interface will be named using the following nomenclature:
1219 * xnb<domid>.<handle>
1221 * Where handle is the oder of the interface referred to the guest.
1223 err = xs_scanf(XST_NIL, xenbus_get_node(xnb->dev), "handle", NULL,
1224 "%" PRIu32, &handle);
1227 snprintf(xnb->if_name, IFNAMSIZ, "xnb%" PRIu16 ".%" PRIu32,
1228 xenbus_get_otherend_id(dev), handle);
1231 /* Set up ifnet structure */
1232 ifp = xnb->xnb_ifp = if_alloc(IFT_ETHER);
1233 ifp->if_softc = xnb;
1234 if_initname(ifp, xnb->if_name, IF_DUNIT_NONE);
1235 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
1236 ifp->if_ioctl = xnb_ioctl;
1237 ifp->if_start = xnb_start;
1238 ifp->if_init = xnb_ifinit;
1239 ifp->if_mtu = ETHERMTU;
1240 ifp->if_snd.ifq_maxlen = NET_RX_RING_SIZE - 1;
1242 ifp->if_hwassist = XNB_CSUM_FEATURES;
1243 ifp->if_capabilities = IFCAP_HWCSUM;
1244 ifp->if_capenable = IFCAP_HWCSUM;
1246 ether_ifattach(ifp, xnb->mac);
1254 * Attach to a XenBus device that has been claimed by our probe routine.
1256 * \param dev NewBus device object representing this Xen Net Back instance.
1258 * \return 0 for success, errno codes for failure.
1261 xnb_attach(device_t dev)
1263 struct xnb_softc *xnb;
1267 error = create_netdev(dev);
1269 xenbus_dev_fatal(dev, error, "creating netdev");
1273 DPRINTF("Attaching to %s\n", xenbus_get_node(dev));
1276 * Basic initialization.
1277 * After this block it is safe to call xnb_detach()
1278 * to clean up any allocated data for this instance.
1280 xnb = device_get_softc(dev);
1281 xnb->otherend_id = xenbus_get_otherend_id(dev);
1282 for (i=0; i < XNB_NUM_RING_TYPES; i++) {
1283 xnb->ring_configs[i].ring_pages = 1;
1287 * Setup sysctl variables.
1289 xnb_setup_sysctl(xnb);
1291 /* Update hot-plug status to satisfy xend. */
1292 error = xs_printf(XST_NIL, xenbus_get_node(xnb->dev),
1293 "hotplug-status", "connected");
1295 xnb_attach_failed(xnb, error, "writing %s/hotplug-status",
1296 xenbus_get_node(xnb->dev));
1300 if ((error = xnb_publish_backend_info(xnb)) != 0) {
1302 * If we can't publish our data, we cannot participate
1303 * in this connection, and waiting for a front-end state
1304 * change will not help the situation.
1306 xnb_attach_failed(xnb, error,
1307 "Publishing backend status for %s",
1308 xenbus_get_node(xnb->dev));
1312 /* Tell the front end that we are ready to connect. */
1313 xenbus_set_state(dev, XenbusStateInitWait);
1319 * Detach from a net back device instance.
1321 * \param dev NewBus device object representing this Xen Net Back instance.
1323 * \return 0 for success, errno codes for failure.
1325 * \note A net back device may be detached at any time in its life-cycle,
1326 * including part way through the attach process. For this reason,
1327 * initialization order and the initialization state checks in this
1328 * routine must be carefully coupled so that attach time failures
1329 * are gracefully handled.
1332 xnb_detach(device_t dev)
1334 struct xnb_softc *xnb;
1338 xnb = device_get_softc(dev);
1339 mtx_lock(&xnb->sc_lock);
1340 while (xnb_shutdown(xnb) == EAGAIN) {
1341 msleep(xnb, &xnb->sc_lock, /*wakeup prio unchanged*/0,
1344 mtx_unlock(&xnb->sc_lock);
1347 mtx_destroy(&xnb->tx_lock);
1348 mtx_destroy(&xnb->rx_lock);
1349 mtx_destroy(&xnb->sc_lock);
1354 * Prepare this net back device for suspension of this VM.
1356 * \param dev NewBus device object representing this Xen net Back instance.
1358 * \return 0 for success, errno codes for failure.
1361 xnb_suspend(device_t dev)
1367 * Perform any processing required to recover from a suspended state.
1369 * \param dev NewBus device object representing this Xen Net Back instance.
1371 * \return 0 for success, errno codes for failure.
1374 xnb_resume(device_t dev)
1380 * Handle state changes expressed via the XenStore by our front-end peer.
1382 * \param dev NewBus device object representing this Xen
1383 * Net Back instance.
1384 * \param frontend_state The new state of the front-end.
1386 * \return 0 for success, errno codes for failure.
1389 xnb_frontend_changed(device_t dev, XenbusState frontend_state)
1391 struct xnb_softc *xnb;
1393 xnb = device_get_softc(dev);
1395 DPRINTF("frontend_state=%s, xnb_state=%s\n",
1396 xenbus_strstate(frontend_state),
1397 xenbus_strstate(xenbus_get_state(xnb->dev)));
1399 switch (frontend_state) {
1400 case XenbusStateInitialising:
1402 case XenbusStateInitialised:
1403 case XenbusStateConnected:
1406 case XenbusStateClosing:
1407 case XenbusStateClosed:
1408 mtx_lock(&xnb->sc_lock);
1410 mtx_unlock(&xnb->sc_lock);
1411 if (frontend_state == XenbusStateClosed)
1412 xenbus_set_state(xnb->dev, XenbusStateClosed);
1415 xenbus_dev_fatal(xnb->dev, EINVAL, "saw state %d at frontend",
1422 /*---------------------------- Request Processing ----------------------------*/
1424 * Interrupt handler bound to the shared ring's event channel.
1425 * Entry point for the xennet transmit path in netback
1426 * Transfers packets from the Xen ring to the host's generic networking stack
1428 * \param arg Callback argument registerd during event channel
1429 * binding - the xnb_softc for this instance.
1434 struct xnb_softc *xnb;
1436 netif_tx_back_ring_t *txb;
1437 RING_IDX req_prod_local;
1439 xnb = (struct xnb_softc *)arg;
1441 txb = &xnb->ring_configs[XNB_RING_TYPE_TX].back_ring.tx_ring;
1443 mtx_lock(&xnb->tx_lock);
1446 req_prod_local = txb->sring->req_prod;
1453 err = xnb_recv(txb, xnb->otherend_id, &mbufc, ifp,
1455 if (err || (mbufc == NULL))
1458 /* Send the packet to the generic network stack */
1459 (*xnb->xnb_ifp->if_input)(xnb->xnb_ifp, mbufc);
1462 RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(txb, notify);
1464 xen_intr_signal(xnb->xen_intr_handle);
1466 txb->sring->req_event = txb->req_cons + 1;
1468 } while (txb->sring->req_prod != req_prod_local) ;
1469 mtx_unlock(&xnb->tx_lock);
1476 * Build a struct xnb_pkt based on netif_tx_request's from a netif tx ring.
1477 * Will read exactly 0 or 1 packets from the ring; never a partial packet.
1478 * \param[out] pkt The returned packet. If there is an error building
1479 * the packet, pkt.list_len will be set to 0.
1480 * \param[in] tx_ring Pointer to the Ring that is the input to this function
1481 * \param[in] start The ring index of the first potential request
1482 * \return The number of requests consumed to build this packet
1485 xnb_ring2pkt(struct xnb_pkt *pkt, const netif_tx_back_ring_t *tx_ring,
1491 * 2) Read the first request of the packet
1492 * 3) Read the extras
1494 * 5) Loop on the remainder of the packet
1495 * 6) Finalize pkt (stuff like car_size and list_len)
1498 int discard = 0; /* whether to discard the packet */
1499 int more_data = 0; /* there are more request past the last one */
1500 uint16_t cdr_size = 0; /* accumulated size of requests 2 through n */
1502 xnb_pkt_initialize(pkt);
1504 /* Read the first request */
1505 if (RING_HAS_UNCONSUMED_REQUESTS_2(tx_ring, idx)) {
1506 netif_tx_request_t *tx = RING_GET_REQUEST(tx_ring, idx);
1507 pkt->size = tx->size;
1508 pkt->flags = tx->flags & ~NETTXF_more_data;
1509 more_data = tx->flags & NETTXF_more_data;
1515 /* Read the extra info */
1516 if ((pkt->flags & NETTXF_extra_info) &&
1517 RING_HAS_UNCONSUMED_REQUESTS_2(tx_ring, idx)) {
1518 netif_extra_info_t *ext =
1519 (netif_extra_info_t*) RING_GET_REQUEST(tx_ring, idx);
1520 pkt->extra.type = ext->type;
1521 switch (pkt->extra.type) {
1522 case XEN_NETIF_EXTRA_TYPE_GSO:
1523 pkt->extra.u.gso = ext->u.gso;
1527 * The reference Linux netfront driver will
1528 * never set any other extra.type. So we don't
1529 * know what to do with it. Let's print an
1530 * error, then consume and discard the packet
1532 printf("xnb(%s:%d): Unknown extra info type %d."
1533 " Discarding packet\n",
1534 __func__, __LINE__, pkt->extra.type);
1535 xnb_dump_txreq(start, RING_GET_REQUEST(tx_ring,
1537 xnb_dump_txreq(idx, RING_GET_REQUEST(tx_ring,
1543 pkt->extra.flags = ext->flags;
1544 if (ext->flags & XEN_NETIF_EXTRA_FLAG_MORE) {
1546 * The reference linux netfront driver never sets this
1547 * flag (nor does any other known netfront). So we
1548 * will discard the packet.
1550 printf("xnb(%s:%d): Request sets "
1551 "XEN_NETIF_EXTRA_FLAG_MORE, but we can't handle "
1552 "that\n", __func__, __LINE__);
1553 xnb_dump_txreq(start, RING_GET_REQUEST(tx_ring, start));
1554 xnb_dump_txreq(idx, RING_GET_REQUEST(tx_ring, idx));
1561 /* Set cdr. If there is not more data, cdr is invalid */
1564 /* Loop on remainder of packet */
1565 while (more_data && RING_HAS_UNCONSUMED_REQUESTS_2(tx_ring, idx)) {
1566 netif_tx_request_t *tx = RING_GET_REQUEST(tx_ring, idx);
1568 cdr_size += tx->size;
1569 if (tx->flags & ~NETTXF_more_data) {
1570 /* There should be no other flags set at this point */
1571 printf("xnb(%s:%d): Request sets unknown flags %d "
1572 "after the 1st request in the packet.\n",
1573 __func__, __LINE__, tx->flags);
1574 xnb_dump_txreq(start, RING_GET_REQUEST(tx_ring, start));
1575 xnb_dump_txreq(idx, RING_GET_REQUEST(tx_ring, idx));
1578 more_data = tx->flags & NETTXF_more_data;
1582 /* Finalize packet */
1583 if (more_data != 0) {
1584 /* The ring ran out of requests before finishing the packet */
1585 xnb_pkt_invalidate(pkt);
1586 idx = start; /* tell caller that we consumed no requests */
1588 /* Calculate car_size */
1589 pkt->car_size = pkt->size - cdr_size;
1592 xnb_pkt_invalidate(pkt);
1600 * Respond to all the requests that constituted pkt. Builds the responses and
1601 * writes them to the ring, but doesn't push them to the shared ring.
1602 * \param[in] pkt the packet that needs a response
1603 * \param[in] error true if there was an error handling the packet, such
1604 * as in the hypervisor copy op or mbuf allocation
1605 * \param[out] ring Responses go here
1608 xnb_txpkt2rsp(const struct xnb_pkt *pkt, netif_tx_back_ring_t *ring,
1613 * 1) Respond to the first request
1614 * 2) Respond to the extra info reques
1615 * Loop through every remaining request in the packet, generating
1616 * responses that copy those requests' ids and sets the status
1619 netif_tx_request_t *tx;
1620 netif_tx_response_t *rsp;
1624 status = (xnb_pkt_is_valid(pkt) == 0) || error ?
1625 NETIF_RSP_ERROR : NETIF_RSP_OKAY;
1626 KASSERT((pkt->list_len == 0) || (ring->rsp_prod_pvt == pkt->car),
1627 ("Cannot respond to ring requests out of order"));
1629 if (pkt->list_len >= 1) {
1631 tx = RING_GET_REQUEST(ring, ring->rsp_prod_pvt);
1633 rsp = RING_GET_RESPONSE(ring, ring->rsp_prod_pvt);
1635 rsp->status = status;
1636 ring->rsp_prod_pvt++;
1638 if (pkt->flags & NETRXF_extra_info) {
1639 rsp = RING_GET_RESPONSE(ring, ring->rsp_prod_pvt);
1640 rsp->status = NETIF_RSP_NULL;
1641 ring->rsp_prod_pvt++;
1645 for (i=0; i < pkt->list_len - 1; i++) {
1647 tx = RING_GET_REQUEST(ring, ring->rsp_prod_pvt);
1649 rsp = RING_GET_RESPONSE(ring, ring->rsp_prod_pvt);
1651 rsp->status = status;
1652 ring->rsp_prod_pvt++;
1657 * Create an mbuf chain to represent a packet. Initializes all of the headers
1658 * in the mbuf chain, but does not copy the data. The returned chain must be
1659 * free()'d when no longer needed
1660 * \param[in] pkt A packet to model the mbuf chain after
1661 * \return A newly allocated mbuf chain, possibly with clusters attached.
1665 xnb_pkt2mbufc(const struct xnb_pkt *pkt, struct ifnet *ifp)
1668 * \todo consider using a memory pool for mbufs instead of
1669 * reallocating them for every packet
1671 /** \todo handle extra data */
1674 m = m_getm(NULL, pkt->size, M_NOWAIT, MT_DATA);
1677 m->m_pkthdr.rcvif = ifp;
1678 if (pkt->flags & NETTXF_data_validated) {
1680 * We lie to the host OS and always tell it that the
1681 * checksums are ok, because the packet is unlikely to
1682 * get corrupted going across domains.
1684 m->m_pkthdr.csum_flags = (
1690 m->m_pkthdr.csum_data = 0xffff;
1697 * Build a gnttab_copy table that can be used to copy data from a pkt
1698 * to an mbufc. Does not actually perform the copy. Always uses gref's on
1700 * \param[in] pkt pkt's associated requests form the src for
1701 * the copy operation
1702 * \param[in] mbufc mbufc's storage forms the dest for the copy operation
1703 * \param[out] gnttab Storage for the returned grant table
1704 * \param[in] txb Pointer to the backend ring structure
1705 * \param[in] otherend_id The domain ID of the other end of the copy
1706 * \return The number of gnttab entries filled
1709 xnb_txpkt2gnttab(const struct xnb_pkt *pkt, struct mbuf *mbufc,
1710 gnttab_copy_table gnttab, const netif_tx_back_ring_t *txb,
1711 domid_t otherend_id)
1714 struct mbuf *mbuf = mbufc;/* current mbuf within the chain */
1715 int gnt_idx = 0; /* index into grant table */
1716 RING_IDX r_idx = pkt->car; /* index into tx ring buffer */
1717 int r_ofs = 0; /* offset of next data within tx request's data area */
1718 int m_ofs = 0; /* offset of next data within mbuf's data area */
1719 /* size in bytes that still needs to be represented in the table */
1720 uint16_t size_remaining = pkt->size;
1722 while (size_remaining > 0) {
1723 const netif_tx_request_t *txq = RING_GET_REQUEST(txb, r_idx);
1724 const size_t mbuf_space = M_TRAILINGSPACE(mbuf) - m_ofs;
1725 const size_t req_size =
1726 r_idx == pkt->car ? pkt->car_size : txq->size;
1727 const size_t pkt_space = req_size - r_ofs;
1729 * space is the largest amount of data that can be copied in the
1730 * grant table's next entry
1732 const size_t space = MIN(pkt_space, mbuf_space);
1734 /* TODO: handle this error condition without panicking */
1735 KASSERT(gnt_idx < GNTTAB_LEN, ("Grant table is too short"));
1737 gnttab[gnt_idx].source.u.ref = txq->gref;
1738 gnttab[gnt_idx].source.domid = otherend_id;
1739 gnttab[gnt_idx].source.offset = txq->offset + r_ofs;
1740 gnttab[gnt_idx].dest.u.gmfn = virt_to_mfn(
1741 mtod(mbuf, vm_offset_t) + m_ofs);
1742 gnttab[gnt_idx].dest.offset = virt_to_offset(
1743 mtod(mbuf, vm_offset_t) + m_ofs);
1744 gnttab[gnt_idx].dest.domid = DOMID_SELF;
1745 gnttab[gnt_idx].len = space;
1746 gnttab[gnt_idx].flags = GNTCOPY_source_gref;
1751 size_remaining -= space;
1752 if (req_size - r_ofs <= 0) {
1753 /* Must move to the next tx request */
1755 r_idx = (r_idx == pkt->car) ? pkt->cdr : r_idx + 1;
1757 if (M_TRAILINGSPACE(mbuf) - m_ofs <= 0) {
1758 /* Must move to the next mbuf */
1760 mbuf = mbuf->m_next;
1768 * Check the status of the grant copy operations, and update mbufs various
1769 * non-data fields to reflect the data present.
1770 * \param[in,out] mbufc mbuf chain to update. The chain must be valid and of
1771 * the correct length, and data should already be present
1772 * \param[in] gnttab A grant table for a just completed copy op
1773 * \param[in] n_entries The number of valid entries in the grant table
1776 xnb_update_mbufc(struct mbuf *mbufc, const gnttab_copy_table gnttab,
1779 struct mbuf *mbuf = mbufc;
1781 size_t total_size = 0;
1783 for (i = 0; i < n_entries; i++) {
1784 KASSERT(gnttab[i].status == GNTST_okay,
1785 ("Some gnttab_copy entry had error status %hd\n",
1788 mbuf->m_len += gnttab[i].len;
1789 total_size += gnttab[i].len;
1790 if (M_TRAILINGSPACE(mbuf) <= 0) {
1791 mbuf = mbuf->m_next;
1794 mbufc->m_pkthdr.len = total_size;
1796 #if defined(INET) || defined(INET6)
1797 xnb_add_mbuf_cksum(mbufc);
1802 * Dequeue at most one packet from the shared ring
1803 * \param[in,out] txb Netif tx ring. A packet will be removed from it, and
1804 * its private indices will be updated. But the indices
1805 * will not be pushed to the shared ring.
1806 * \param[in] ifnet Interface to which the packet will be sent
1807 * \param[in] otherend Domain ID of the other end of the ring
1808 * \param[out] mbufc The assembled mbuf chain, ready to send to the generic
1810 * \param[in,out] gnttab Pointer to enough memory for a grant table. We make
1811 * this a function parameter so that we will take less
1813 * \return An error code
1816 xnb_recv(netif_tx_back_ring_t *txb, domid_t otherend, struct mbuf **mbufc,
1817 struct ifnet *ifnet, gnttab_copy_table gnttab)
1820 /* number of tx requests consumed to build the last packet */
1825 num_consumed = xnb_ring2pkt(&pkt, txb, txb->req_cons);
1826 if (num_consumed == 0)
1827 return 0; /* Nothing to receive */
1829 /* update statistics independent of errors */
1830 if_inc_counter(ifnet, IFCOUNTER_IPACKETS, 1);
1833 * if we got here, then 1 or more requests was consumed, but the packet
1834 * is not necessarily valid.
1836 if (xnb_pkt_is_valid(&pkt) == 0) {
1837 /* got a garbage packet, respond and drop it */
1838 xnb_txpkt2rsp(&pkt, txb, 1);
1839 txb->req_cons += num_consumed;
1840 DPRINTF("xnb_intr: garbage packet, num_consumed=%d\n",
1842 if_inc_counter(ifnet, IFCOUNTER_IERRORS, 1);
1846 *mbufc = xnb_pkt2mbufc(&pkt, ifnet);
1848 if (*mbufc == NULL) {
1850 * Couldn't allocate mbufs. Respond and drop the packet. Do
1851 * not consume the requests
1853 xnb_txpkt2rsp(&pkt, txb, 1);
1854 DPRINTF("xnb_intr: Couldn't allocate mbufs, num_consumed=%d\n",
1856 if_inc_counter(ifnet, IFCOUNTER_IQDROPS, 1);
1860 nr_ents = xnb_txpkt2gnttab(&pkt, *mbufc, gnttab, txb, otherend);
1863 int __unused hv_ret = HYPERVISOR_grant_table_op(GNTTABOP_copy,
1865 KASSERT(hv_ret == 0,
1866 ("HYPERVISOR_grant_table_op returned %d\n", hv_ret));
1867 xnb_update_mbufc(*mbufc, gnttab, nr_ents);
1870 xnb_txpkt2rsp(&pkt, txb, 0);
1871 txb->req_cons += num_consumed;
1876 * Create an xnb_pkt based on the contents of an mbuf chain.
1877 * \param[in] mbufc mbuf chain to transform into a packet
1878 * \param[out] pkt Storage for the newly generated xnb_pkt
1879 * \param[in] start The ring index of the first available slot in the rx
1881 * \param[in] space The number of free slots in the rx ring
1883 * \retval EINVAL mbufc was corrupt or not convertible into a pkt
1884 * \retval EAGAIN There was not enough space in the ring to queue the
1888 xnb_mbufc2pkt(const struct mbuf *mbufc, struct xnb_pkt *pkt,
1889 RING_IDX start, int space)
1894 if ((mbufc == NULL) ||
1895 ( (mbufc->m_flags & M_PKTHDR) == 0) ||
1896 (mbufc->m_pkthdr.len == 0)) {
1897 xnb_pkt_invalidate(pkt);
1902 xnb_pkt_validate(pkt);
1904 pkt->size = mbufc->m_pkthdr.len;
1906 pkt->car_size = mbufc->m_len;
1908 if (mbufc->m_pkthdr.csum_flags & CSUM_TSO) {
1909 pkt->flags |= NETRXF_extra_info;
1910 pkt->extra.u.gso.size = mbufc->m_pkthdr.tso_segsz;
1911 pkt->extra.u.gso.type = XEN_NETIF_GSO_TYPE_TCPV4;
1912 pkt->extra.u.gso.pad = 0;
1913 pkt->extra.u.gso.features = 0;
1914 pkt->extra.type = XEN_NETIF_EXTRA_TYPE_GSO;
1915 pkt->extra.flags = 0;
1916 pkt->cdr = start + 2;
1918 pkt->cdr = start + 1;
1920 if (mbufc->m_pkthdr.csum_flags & (CSUM_TSO | CSUM_DELAY_DATA)) {
1922 (NETRXF_csum_blank | NETRXF_data_validated);
1926 * Each ring response can have up to PAGE_SIZE of data.
1927 * Assume that we can defragment the mbuf chain efficiently
1928 * into responses so that each response but the last uses all
1931 pkt->list_len = howmany(pkt->size, PAGE_SIZE);
1933 if (pkt->list_len > 1) {
1934 pkt->flags |= NETRXF_more_data;
1937 slots_required = pkt->list_len +
1938 (pkt->flags & NETRXF_extra_info ? 1 : 0);
1939 if (slots_required > space) {
1940 xnb_pkt_invalidate(pkt);
1949 * Build a gnttab_copy table that can be used to copy data from an mbuf chain
1950 * to the frontend's shared buffers. Does not actually perform the copy.
1951 * Always uses gref's on the other end's side.
1952 * \param[in] pkt pkt's associated responses form the dest for the copy
1954 * \param[in] mbufc The source for the copy operation
1955 * \param[out] gnttab Storage for the returned grant table
1956 * \param[in] rxb Pointer to the backend ring structure
1957 * \param[in] otherend_id The domain ID of the other end of the copy
1958 * \return The number of gnttab entries filled
1961 xnb_rxpkt2gnttab(const struct xnb_pkt *pkt, const struct mbuf *mbufc,
1962 gnttab_copy_table gnttab, const netif_rx_back_ring_t *rxb,
1963 domid_t otherend_id)
1966 const struct mbuf *mbuf = mbufc;/* current mbuf within the chain */
1967 int gnt_idx = 0; /* index into grant table */
1968 RING_IDX r_idx = pkt->car; /* index into rx ring buffer */
1969 int r_ofs = 0; /* offset of next data within rx request's data area */
1970 int m_ofs = 0; /* offset of next data within mbuf's data area */
1971 /* size in bytes that still needs to be represented in the table */
1972 uint16_t size_remaining;
1974 size_remaining = (xnb_pkt_is_valid(pkt) != 0) ? pkt->size : 0;
1976 while (size_remaining > 0) {
1977 const netif_rx_request_t *rxq = RING_GET_REQUEST(rxb, r_idx);
1978 const size_t mbuf_space = mbuf->m_len - m_ofs;
1979 /* Xen shared pages have an implied size of PAGE_SIZE */
1980 const size_t req_size = PAGE_SIZE;
1981 const size_t pkt_space = req_size - r_ofs;
1983 * space is the largest amount of data that can be copied in the
1984 * grant table's next entry
1986 const size_t space = MIN(pkt_space, mbuf_space);
1988 /* TODO: handle this error condition without panicing */
1989 KASSERT(gnt_idx < GNTTAB_LEN, ("Grant table is too short"));
1991 gnttab[gnt_idx].dest.u.ref = rxq->gref;
1992 gnttab[gnt_idx].dest.domid = otherend_id;
1993 gnttab[gnt_idx].dest.offset = r_ofs;
1994 gnttab[gnt_idx].source.u.gmfn = virt_to_mfn(
1995 mtod(mbuf, vm_offset_t) + m_ofs);
1996 gnttab[gnt_idx].source.offset = virt_to_offset(
1997 mtod(mbuf, vm_offset_t) + m_ofs);
1998 gnttab[gnt_idx].source.domid = DOMID_SELF;
1999 gnttab[gnt_idx].len = space;
2000 gnttab[gnt_idx].flags = GNTCOPY_dest_gref;
2006 size_remaining -= space;
2007 if (req_size - r_ofs <= 0) {
2008 /* Must move to the next rx request */
2010 r_idx = (r_idx == pkt->car) ? pkt->cdr : r_idx + 1;
2012 if (mbuf->m_len - m_ofs <= 0) {
2013 /* Must move to the next mbuf */
2015 mbuf = mbuf->m_next;
2023 * Generates responses for all the requests that constituted pkt. Builds
2024 * responses and writes them to the ring, but doesn't push the shared ring
2026 * \param[in] pkt the packet that needs a response
2027 * \param[in] gnttab The grant copy table corresponding to this packet.
2028 * Used to determine how many rsp->netif_rx_response_t's to
2030 * \param[in] n_entries Number of relevant entries in the grant table
2031 * \param[out] ring Responses go here
2032 * \return The number of RX requests that were consumed to generate
2036 xnb_rxpkt2rsp(const struct xnb_pkt *pkt, const gnttab_copy_table gnttab,
2037 int n_entries, netif_rx_back_ring_t *ring)
2040 * This code makes the following assumptions:
2041 * * All entries in gnttab set GNTCOPY_dest_gref
2042 * * The entries in gnttab are grouped by their grefs: any two
2043 * entries with the same gref must be adjacent
2047 int n_responses = 0;
2048 grant_ref_t last_gref = GRANT_REF_INVALID;
2051 KASSERT(gnttab != NULL, ("Received a null granttable copy"));
2054 * In the event of an error, we only need to send one response to the
2055 * netfront. In that case, we musn't write any data to the responses
2056 * after the one we send. So we must loop all the way through gnttab
2057 * looking for errors before we generate any responses
2059 * Since we're looping through the grant table anyway, we'll count the
2060 * number of different gref's in it, which will tell us how many
2061 * responses to generate
2063 for (gnt_idx = 0; gnt_idx < n_entries; gnt_idx++) {
2064 int16_t status = gnttab[gnt_idx].status;
2065 if (status != GNTST_okay) {
2067 "Got error %d for hypervisor gnttab_copy status\n",
2072 if (gnttab[gnt_idx].dest.u.ref != last_gref) {
2074 last_gref = gnttab[gnt_idx].dest.u.ref;
2080 netif_rx_response_t *rsp;
2082 id = RING_GET_REQUEST(ring, ring->rsp_prod_pvt)->id;
2083 rsp = RING_GET_RESPONSE(ring, ring->rsp_prod_pvt);
2085 rsp->status = NETIF_RSP_ERROR;
2089 const int has_extra = pkt->flags & NETRXF_extra_info;
2093 for (i = 0; i < n_responses; i++) {
2094 netif_rx_request_t rxq;
2095 netif_rx_response_t *rsp;
2097 r_idx = ring->rsp_prod_pvt + i;
2099 * We copy the structure of rxq instead of making a
2100 * pointer because it shares the same memory as rsp.
2102 rxq = *(RING_GET_REQUEST(ring, r_idx));
2103 rsp = RING_GET_RESPONSE(ring, r_idx);
2104 if (has_extra && (i == 1)) {
2105 netif_extra_info_t *ext =
2106 (netif_extra_info_t*)rsp;
2107 ext->type = XEN_NETIF_EXTRA_TYPE_GSO;
2109 ext->u.gso.size = pkt->extra.u.gso.size;
2110 ext->u.gso.type = XEN_NETIF_GSO_TYPE_TCPV4;
2112 ext->u.gso.features = 0;
2115 rsp->status = GNTST_okay;
2118 if (i < pkt->list_len - 1)
2119 rsp->flags |= NETRXF_more_data;
2120 if ((i == 0) && has_extra)
2121 rsp->flags |= NETRXF_extra_info;
2123 (pkt->flags & NETRXF_data_validated)) {
2124 rsp->flags |= NETRXF_data_validated;
2125 rsp->flags |= NETRXF_csum_blank;
2128 for (; gnttab[gnt_idx].dest.u.ref == rxq.gref;
2130 rsp->status += gnttab[gnt_idx].len;
2136 ring->req_cons += n_responses;
2137 ring->rsp_prod_pvt += n_responses;
2141 #if defined(INET) || defined(INET6)
2143 * Add IP, TCP, and/or UDP checksums to every mbuf in a chain. The first mbuf
2144 * in the chain must start with a struct ether_header.
2146 * XXX This function will perform incorrectly on UDP packets that are split up
2147 * into multiple ethernet frames.
2150 xnb_add_mbuf_cksum(struct mbuf *mbufc)
2152 struct ether_header *eh;
2154 uint16_t ether_type;
2156 eh = mtod(mbufc, struct ether_header*);
2157 ether_type = ntohs(eh->ether_type);
2158 if (ether_type != ETHERTYPE_IP) {
2159 /* Nothing to calculate */
2163 iph = (struct ip*)(eh + 1);
2164 if (mbufc->m_pkthdr.csum_flags & CSUM_IP_VALID) {
2166 iph->ip_sum = in_cksum_hdr(iph);
2169 switch (iph->ip_p) {
2171 if (mbufc->m_pkthdr.csum_flags & CSUM_IP_VALID) {
2172 size_t tcplen = ntohs(iph->ip_len) - sizeof(struct ip);
2173 struct tcphdr *th = (struct tcphdr*)(iph + 1);
2174 th->th_sum = in_pseudo(iph->ip_src.s_addr,
2175 iph->ip_dst.s_addr, htons(IPPROTO_TCP + tcplen));
2176 th->th_sum = in_cksum_skip(mbufc,
2177 sizeof(struct ether_header) + ntohs(iph->ip_len),
2178 sizeof(struct ether_header) + (iph->ip_hl << 2));
2182 if (mbufc->m_pkthdr.csum_flags & CSUM_IP_VALID) {
2183 size_t udplen = ntohs(iph->ip_len) - sizeof(struct ip);
2184 struct udphdr *uh = (struct udphdr*)(iph + 1);
2185 uh->uh_sum = in_pseudo(iph->ip_src.s_addr,
2186 iph->ip_dst.s_addr, htons(IPPROTO_UDP + udplen));
2187 uh->uh_sum = in_cksum_skip(mbufc,
2188 sizeof(struct ether_header) + ntohs(iph->ip_len),
2189 sizeof(struct ether_header) + (iph->ip_hl << 2));
2196 #endif /* INET || INET6 */
2199 xnb_stop(struct xnb_softc *xnb)
2203 mtx_assert(&xnb->sc_lock, MA_OWNED);
2205 ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
2206 if_link_state_change(ifp, LINK_STATE_DOWN);
2210 xnb_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
2212 struct xnb_softc *xnb = ifp->if_softc;
2213 struct ifreq *ifr = (struct ifreq*) data;
2215 struct ifaddr *ifa = (struct ifaddr*)data;
2221 mtx_lock(&xnb->sc_lock);
2222 if (ifp->if_flags & IFF_UP) {
2223 xnb_ifinit_locked(xnb);
2225 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
2230 * Note: netfront sets a variable named xn_if_flags
2231 * here, but that variable is never read
2233 mtx_unlock(&xnb->sc_lock);
2237 mtx_lock(&xnb->sc_lock);
2238 if (ifa->ifa_addr->sa_family == AF_INET) {
2239 ifp->if_flags |= IFF_UP;
2240 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
2241 ifp->if_drv_flags &= ~(IFF_DRV_RUNNING |
2243 if_link_state_change(ifp,
2245 ifp->if_drv_flags |= IFF_DRV_RUNNING;
2246 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
2247 if_link_state_change(ifp,
2250 arp_ifinit(ifp, ifa);
2251 mtx_unlock(&xnb->sc_lock);
2253 mtx_unlock(&xnb->sc_lock);
2255 error = ether_ioctl(ifp, cmd, data);
2261 mtx_lock(&xnb->sc_lock);
2262 if (ifr->ifr_reqcap & IFCAP_TXCSUM) {
2263 ifp->if_capenable |= IFCAP_TXCSUM;
2264 ifp->if_hwassist |= XNB_CSUM_FEATURES;
2266 ifp->if_capenable &= ~(IFCAP_TXCSUM);
2267 ifp->if_hwassist &= ~(XNB_CSUM_FEATURES);
2269 if ((ifr->ifr_reqcap & IFCAP_RXCSUM)) {
2270 ifp->if_capenable |= IFCAP_RXCSUM;
2272 ifp->if_capenable &= ~(IFCAP_RXCSUM);
2275 * TODO enable TSO4 and LRO once we no longer need
2276 * to calculate checksums in software
2279 if (ifr->if_reqcap |= IFCAP_TSO4) {
2280 if (IFCAP_TXCSUM & ifp->if_capenable) {
2281 printf("xnb: Xen netif requires that "
2282 "TXCSUM be enabled in order "
2286 ifp->if_capenable |= IFCAP_TSO4;
2287 ifp->if_hwassist |= CSUM_TSO;
2290 ifp->if_capenable &= ~(IFCAP_TSO4);
2291 ifp->if_hwassist &= ~(CSUM_TSO);
2293 if (ifr->ifreqcap |= IFCAP_LRO) {
2294 ifp->if_capenable |= IFCAP_LRO;
2296 ifp->if_capenable &= ~(IFCAP_LRO);
2299 mtx_unlock(&xnb->sc_lock);
2302 ifp->if_mtu = ifr->ifr_mtu;
2303 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
2310 error = ifmedia_ioctl(ifp, ifr, &xnb->sc_media, cmd);
2313 error = ether_ioctl(ifp, cmd, data);
2320 xnb_start_locked(struct ifnet *ifp)
2322 netif_rx_back_ring_t *rxb;
2323 struct xnb_softc *xnb;
2325 RING_IDX req_prod_local;
2327 xnb = ifp->if_softc;
2328 rxb = &xnb->ring_configs[XNB_RING_TYPE_RX].back_ring.rx_ring;
2334 int out_of_space = 0;
2336 req_prod_local = rxb->sring->req_prod;
2341 IF_DEQUEUE(&ifp->if_snd, mbufc);
2344 error = xnb_send(rxb, xnb->otherend_id, mbufc,
2349 * Insufficient space in the ring.
2350 * Requeue pkt and send when space is
2353 IF_PREPEND(&ifp->if_snd, mbufc);
2355 * Perhaps the frontend missed an IRQ
2356 * and went to sleep. Notify it to wake
2363 /* OS gave a corrupt packet. Drop it.*/
2364 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
2367 /* Send succeeded, or packet had error.
2368 * Free the packet */
2369 if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
2374 if (out_of_space != 0)
2378 RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(rxb, notify);
2379 if ((notify != 0) || (out_of_space != 0))
2380 xen_intr_signal(xnb->xen_intr_handle);
2381 rxb->sring->req_event = req_prod_local + 1;
2383 } while (rxb->sring->req_prod != req_prod_local) ;
2387 * Sends one packet to the ring. Blocks until the packet is on the ring
2388 * \param[in] mbufc Contains one packet to send. Caller must free
2389 * \param[in,out] rxb The packet will be pushed onto this ring, but the
2390 * otherend will not be notified.
2391 * \param[in] otherend The domain ID of the other end of the connection
2392 * \retval EAGAIN The ring did not have enough space for the packet.
2393 * The ring has not been modified
2394 * \param[in,out] gnttab Pointer to enough memory for a grant table. We make
2395 * this a function parameter so that we will take less
2397 * \retval EINVAL mbufc was corrupt or not convertible into a pkt
2400 xnb_send(netif_rx_back_ring_t *ring, domid_t otherend, const struct mbuf *mbufc,
2401 gnttab_copy_table gnttab)
2404 int error, n_entries, n_reqs;
2407 space = ring->sring->req_prod - ring->req_cons;
2408 error = xnb_mbufc2pkt(mbufc, &pkt, ring->rsp_prod_pvt, space);
2411 n_entries = xnb_rxpkt2gnttab(&pkt, mbufc, gnttab, ring, otherend);
2412 if (n_entries != 0) {
2413 int __unused hv_ret = HYPERVISOR_grant_table_op(GNTTABOP_copy,
2415 KASSERT(hv_ret == 0, ("HYPERVISOR_grant_table_op returned %d\n",
2419 n_reqs = xnb_rxpkt2rsp(&pkt, gnttab, n_entries, ring);
2425 xnb_start(struct ifnet *ifp)
2427 struct xnb_softc *xnb;
2429 xnb = ifp->if_softc;
2430 mtx_lock(&xnb->rx_lock);
2431 xnb_start_locked(ifp);
2432 mtx_unlock(&xnb->rx_lock);
2435 /* equivalent of network_open() in Linux */
2437 xnb_ifinit_locked(struct xnb_softc *xnb)
2443 mtx_assert(&xnb->sc_lock, MA_OWNED);
2445 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
2450 ifp->if_drv_flags |= IFF_DRV_RUNNING;
2451 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
2452 if_link_state_change(ifp, LINK_STATE_UP);
2457 xnb_ifinit(void *xsc)
2459 struct xnb_softc *xnb = xsc;
2461 mtx_lock(&xnb->sc_lock);
2462 xnb_ifinit_locked(xnb);
2463 mtx_unlock(&xnb->sc_lock);
2467 * Callback used by the generic networking code to tell us when our carrier
2468 * state has changed. Since we don't have a physical carrier, we don't care
2471 xnb_ifmedia_upd(struct ifnet *ifp)
2477 * Callback used by the generic networking code to ask us what our carrier
2478 * state is. Since we don't have a physical carrier, this is very simple
2481 xnb_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
2483 ifmr->ifm_status = IFM_AVALID|IFM_ACTIVE;
2484 ifmr->ifm_active = IFM_ETHER|IFM_MANUAL;
2488 /*---------------------------- NewBus Registration ---------------------------*/
2489 static device_method_t xnb_methods[] = {
2490 /* Device interface */
2491 DEVMETHOD(device_probe, xnb_probe),
2492 DEVMETHOD(device_attach, xnb_attach),
2493 DEVMETHOD(device_detach, xnb_detach),
2494 DEVMETHOD(device_shutdown, bus_generic_shutdown),
2495 DEVMETHOD(device_suspend, xnb_suspend),
2496 DEVMETHOD(device_resume, xnb_resume),
2498 /* Xenbus interface */
2499 DEVMETHOD(xenbus_otherend_changed, xnb_frontend_changed),
2504 static driver_t xnb_driver = {
2507 sizeof(struct xnb_softc),
2509 devclass_t xnb_devclass;
2511 DRIVER_MODULE(xnb, xenbusb_back, xnb_driver, xnb_devclass, 0, 0);
2514 /*-------------------------- Unit Tests -------------------------------------*/
2516 #include "netback_unit_tests.c"