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"
49 #include <sys/param.h>
50 #include <sys/kernel.h>
53 #include <sys/module.h>
55 #include <sys/socket.h>
56 #include <sys/sockio.h>
57 #include <sys/sysctl.h>
60 #include <net/if_var.h>
61 #include <net/if_arp.h>
62 #include <net/ethernet.h>
63 #include <net/if_dl.h>
64 #include <net/if_media.h>
65 #include <net/if_types.h>
67 #include <netinet/in.h>
68 #include <netinet/ip.h>
69 #include <netinet/if_ether.h>
70 #if __FreeBSD_version >= 700000
71 #include <netinet/tcp.h>
73 #include <netinet/ip_icmp.h>
74 #include <netinet/udp.h>
75 #include <machine/in_cksum.h>
79 #include <vm/vm_extern.h>
80 #include <vm/vm_kern.h>
82 #include <machine/_inttypes.h>
84 #include <xen/xen-os.h>
85 #include <xen/hypervisor.h>
86 #include <xen/xen_intr.h>
87 #include <xen/interface/io/netif.h>
88 #include <xen/xenbus/xenbusvar.h>
90 /*--------------------------- Compile-time Tunables --------------------------*/
92 /*---------------------------------- Macros ----------------------------------*/
94 * Custom malloc type for all driver allocations.
96 static MALLOC_DEFINE(M_XENNETBACK, "xnb", "Xen Net Back Driver Data");
98 #define XNB_SG 1 /* netback driver supports feature-sg */
99 #define XNB_GSO_TCPV4 0 /* netback driver supports feature-gso-tcpv4 */
100 #define XNB_RX_COPY 1 /* netback driver supports feature-rx-copy */
101 #define XNB_RX_FLIP 0 /* netback driver does not support feature-rx-flip */
104 #define XNB_DEBUG /* hardcode on during development */
107 #define DPRINTF(fmt, args...) \
108 printf("xnb(%s:%d): " fmt, __FUNCTION__, __LINE__, ##args)
110 #define DPRINTF(fmt, args...) do {} while (0)
113 /* Default length for stack-allocated grant tables */
114 #define GNTTAB_LEN (64)
116 /* Features supported by all backends. TSO and LRO can be negotiated */
117 #define XNB_CSUM_FEATURES (CSUM_TCP | CSUM_UDP)
119 #define NET_TX_RING_SIZE __RING_SIZE((netif_tx_sring_t *)0, PAGE_SIZE)
120 #define NET_RX_RING_SIZE __RING_SIZE((netif_rx_sring_t *)0, PAGE_SIZE)
123 * Two argument version of the standard macro. Second argument is a tentative
126 #define RING_HAS_UNCONSUMED_REQUESTS_2(_r, cons) ({ \
127 unsigned int req = (_r)->sring->req_prod - cons; \
128 unsigned int rsp = RING_SIZE(_r) - \
129 (cons - (_r)->rsp_prod_pvt); \
130 req < rsp ? req : rsp; \
133 #define virt_to_mfn(x) (vtophys(x) >> PAGE_SHIFT)
134 #define virt_to_offset(x) ((x) & (PAGE_SIZE - 1))
137 * Predefined array type of grant table copy descriptors. Used to pass around
138 * statically allocated memory structures.
140 typedef struct gnttab_copy gnttab_copy_table[GNTTAB_LEN];
142 /*--------------------------- Forward Declarations ---------------------------*/
146 static void xnb_attach_failed(struct xnb_softc *xnb,
147 int err, const char *fmt, ...)
149 static int xnb_shutdown(struct xnb_softc *xnb);
150 static int create_netdev(device_t dev);
151 static int xnb_detach(device_t dev);
152 static int xnb_ifmedia_upd(struct ifnet *ifp);
153 static void xnb_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr);
154 static void xnb_intr(void *arg);
155 static int xnb_send(netif_rx_back_ring_t *rxb, domid_t otherend,
156 const struct mbuf *mbufc, gnttab_copy_table gnttab);
157 static int xnb_recv(netif_tx_back_ring_t *txb, domid_t otherend,
158 struct mbuf **mbufc, struct ifnet *ifnet,
159 gnttab_copy_table gnttab);
160 static int xnb_ring2pkt(struct xnb_pkt *pkt,
161 const netif_tx_back_ring_t *tx_ring,
163 static void xnb_txpkt2rsp(const struct xnb_pkt *pkt,
164 netif_tx_back_ring_t *ring, int error);
165 static struct mbuf *xnb_pkt2mbufc(const struct xnb_pkt *pkt, struct ifnet *ifp);
166 static int xnb_txpkt2gnttab(const struct xnb_pkt *pkt,
168 gnttab_copy_table gnttab,
169 const netif_tx_back_ring_t *txb,
170 domid_t otherend_id);
171 static void xnb_update_mbufc(struct mbuf *mbufc,
172 const gnttab_copy_table gnttab, int n_entries);
173 static int xnb_mbufc2pkt(const struct mbuf *mbufc,
175 RING_IDX start, int space);
176 static int xnb_rxpkt2gnttab(const struct xnb_pkt *pkt,
177 const struct mbuf *mbufc,
178 gnttab_copy_table gnttab,
179 const netif_rx_back_ring_t *rxb,
180 domid_t otherend_id);
181 static int xnb_rxpkt2rsp(const struct xnb_pkt *pkt,
182 const gnttab_copy_table gnttab, int n_entries,
183 netif_rx_back_ring_t *ring);
184 static void xnb_stop(struct xnb_softc*);
185 static int xnb_ioctl(struct ifnet*, u_long, caddr_t);
186 static void xnb_start_locked(struct ifnet*);
187 static void xnb_start(struct ifnet*);
188 static void xnb_ifinit_locked(struct xnb_softc*);
189 static void xnb_ifinit(void*);
191 static int xnb_unit_test_main(SYSCTL_HANDLER_ARGS);
192 static int xnb_dump_rings(SYSCTL_HANDLER_ARGS);
194 #if defined(INET) || defined(INET6)
195 static void xnb_add_mbuf_cksum(struct mbuf *mbufc);
197 /*------------------------------ Data Structures -----------------------------*/
201 * Representation of a xennet packet. Simplified version of a packet as
202 * stored in the Xen tx ring. Applicable to both RX and TX packets
206 * Array index of the first data-bearing (eg, not extra info) entry
212 * Array index of the second data-bearing entry for this packet.
213 * Invalid if the packet has only one data-bearing entry. If the
214 * packet has more than two data-bearing entries, then the second
215 * through the last will be sequential modulo the ring size
220 * Optional extra info. Only valid if flags contains
221 * NETTXF_extra_info. Note that extra.type will always be
222 * XEN_NETIF_EXTRA_TYPE_GSO. Currently, no known netfront or netback
223 * driver will ever set XEN_NETIF_EXTRA_TYPE_MCAST_*
225 netif_extra_info_t extra;
227 /** Size of entire packet in bytes. */
230 /** The size of the first entry's data in bytes */
234 * Either NETTXF_ or NETRXF_ flags. Note that the flag values are
235 * not the same for TX and RX packets
240 * The number of valid data-bearing entries (either netif_tx_request's
241 * or netif_rx_response's) in the packet. If this is 0, it means the
242 * entire packet is invalid.
246 /** There was an error processing the packet */
250 /** xnb_pkt method: initialize it */
252 xnb_pkt_initialize(struct xnb_pkt *pxnb)
254 bzero(pxnb, sizeof(*pxnb));
257 /** xnb_pkt method: mark the packet as valid */
259 xnb_pkt_validate(struct xnb_pkt *pxnb)
264 /** xnb_pkt method: mark the packet as invalid */
266 xnb_pkt_invalidate(struct xnb_pkt *pxnb)
271 /** xnb_pkt method: Check whether the packet is valid */
273 xnb_pkt_is_valid(const struct xnb_pkt *pxnb)
275 return (! pxnb->error);
279 /** xnb_pkt method: print the packet's contents in human-readable format*/
281 xnb_dump_pkt(const struct xnb_pkt *pkt) {
283 DPRINTF("Was passed a null pointer.\n");
286 DPRINTF("pkt address= %p\n", pkt);
287 DPRINTF("pkt->size=%d\n", pkt->size);
288 DPRINTF("pkt->car_size=%d\n", pkt->car_size);
289 DPRINTF("pkt->flags=0x%04x\n", pkt->flags);
290 DPRINTF("pkt->list_len=%d\n", pkt->list_len);
291 /* DPRINTF("pkt->extra"); TODO */
292 DPRINTF("pkt->car=%d\n", pkt->car);
293 DPRINTF("pkt->cdr=%d\n", pkt->cdr);
294 DPRINTF("pkt->error=%d\n", pkt->error);
296 #endif /* XNB_DEBUG */
299 xnb_dump_txreq(RING_IDX idx, const struct netif_tx_request *txreq)
302 DPRINTF("netif_tx_request index =%u\n", idx);
303 DPRINTF("netif_tx_request.gref =%u\n", txreq->gref);
304 DPRINTF("netif_tx_request.offset=%hu\n", txreq->offset);
305 DPRINTF("netif_tx_request.flags =%hu\n", txreq->flags);
306 DPRINTF("netif_tx_request.id =%hu\n", txreq->id);
307 DPRINTF("netif_tx_request.size =%hu\n", txreq->size);
313 * \brief Configuration data for a shared memory request ring
314 * used to communicate with the front-end client of this
317 struct xnb_ring_config {
319 * Runtime structures for ring access. Unfortunately, TX and RX rings
320 * use different data structures, and that cannot be changed since it
321 * is part of the interdomain protocol.
324 netif_rx_back_ring_t rx_ring;
325 netif_tx_back_ring_t tx_ring;
329 * The device bus address returned by the hypervisor when
330 * mapping the ring and required to unmap it when a connection
335 /** The pseudo-physical address where ring memory is mapped.*/
338 /** KVA address where ring memory is mapped. */
342 * Grant table handles, one per-ring page, returned by the
343 * hyperpervisor upon mapping of the ring and required to
344 * unmap it when a connection is torn down.
346 grant_handle_t handle;
348 /** The number of ring pages mapped for the current connection. */
352 * The grant references, one per-ring page, supplied by the
353 * front-end, allowing us to reference the ring pages in the
354 * front-end's domain and to map these pages into our own domain.
356 grant_ref_t ring_ref;
360 * Per-instance connection state flags.
364 /** Communication with the front-end has been established. */
365 XNBF_RING_CONNECTED = 0x01,
368 * Front-end requests exist in the ring and are waiting for
369 * xnb_xen_req objects to free up.
371 XNBF_RESOURCE_SHORTAGE = 0x02,
373 /** Connection teardown has started. */
374 XNBF_SHUTDOWN = 0x04,
376 /** A thread is already performing shutdown processing. */
377 XNBF_IN_SHUTDOWN = 0x08
381 * Types of rings. Used for array indices and to identify a ring's control
382 * data structure type
385 XNB_RING_TYPE_TX = 0, /* ID of TX rings, used for array indices */
386 XNB_RING_TYPE_RX = 1, /* ID of RX rings, used for array indices */
391 * Per-instance configuration data.
394 /** NewBus device corresponding to this instance. */
397 /* Media related fields */
399 /** Generic network media state */
400 struct ifmedia sc_media;
402 /** Media carrier info */
403 struct ifnet *xnb_ifp;
405 /** Our own private carrier state */
408 /** Device MAC Address */
409 uint8_t mac[ETHER_ADDR_LEN];
411 /* Xen related fields */
414 * \brief The netif protocol abi in effect.
416 * There are situations where the back and front ends can
417 * have a different, native abi (e.g. intel x86_64 and
418 * 32bit x86 domains on the same machine). The back-end
419 * always accommodates the front-end's native abi. That
420 * value is pulled from the XenStore and recorded here.
425 * Name of the bridge to which this VIF is connected, if any
426 * This field is dynamically allocated by xenbus and must be free()ed
427 * when no longer needed
431 /** The interrupt driven even channel used to signal ring events. */
432 evtchn_port_t evtchn;
434 /** Xen device handle.*/
437 /** Handle to the communication ring event channel. */
438 xen_intr_handle_t xen_intr_handle;
441 * \brief Cached value of the front-end's domain id.
443 * This value is used at once for each mapped page in
444 * a transaction. We cache it to avoid incuring the
445 * cost of an ivar access every time this is needed.
450 * Undocumented frontend feature. Has something to do with
454 /** Undocumented frontend feature */
456 /** Undocumented frontend feature */
458 /** Can checksum TCP/UDP over IPv4 */
461 /* Implementation related fields */
463 * Preallocated grant table copy descriptor for RX operations.
464 * Access must be protected by rx_lock
466 gnttab_copy_table rx_gnttab;
469 * Preallocated grant table copy descriptor for TX operations.
470 * Access must be protected by tx_lock
472 gnttab_copy_table tx_gnttab;
475 * Resource representing allocated physical address space
476 * associated with our per-instance kva region.
478 struct resource *pseudo_phys_res;
480 /** Resource id for allocated physical address space. */
481 int pseudo_phys_res_id;
483 /** Ring mapping and interrupt configuration data. */
484 struct xnb_ring_config ring_configs[XNB_NUM_RING_TYPES];
487 * Global pool of kva used for mapping remote domain ring
488 * and I/O transaction data.
492 /** Pseudo-physical address corresponding to kva. */
493 uint64_t gnt_base_addr;
495 /** Various configuration and state bit flags. */
498 /** Mutex protecting per-instance data in the receive path. */
501 /** Mutex protecting per-instance data in the softc structure. */
504 /** Mutex protecting per-instance data in the transmit path. */
507 /** The size of the global kva pool. */
510 /** Name of the interface */
511 char if_name[IFNAMSIZ];
514 /*---------------------------- Debugging functions ---------------------------*/
517 xnb_dump_gnttab_copy(const struct gnttab_copy *entry)
520 printf("NULL grant table pointer\n");
524 if (entry->flags & GNTCOPY_dest_gref)
525 printf("gnttab dest ref=\t%u\n", entry->dest.u.ref);
527 printf("gnttab dest gmfn=\t%"PRI_xen_pfn"\n",
529 printf("gnttab dest offset=\t%hu\n", entry->dest.offset);
530 printf("gnttab dest domid=\t%hu\n", entry->dest.domid);
531 if (entry->flags & GNTCOPY_source_gref)
532 printf("gnttab source ref=\t%u\n", entry->source.u.ref);
534 printf("gnttab source gmfn=\t%"PRI_xen_pfn"\n",
535 entry->source.u.gmfn);
536 printf("gnttab source offset=\t%hu\n", entry->source.offset);
537 printf("gnttab source domid=\t%hu\n", entry->source.domid);
538 printf("gnttab len=\t%hu\n", entry->len);
539 printf("gnttab flags=\t%hu\n", entry->flags);
540 printf("gnttab status=\t%hd\n", entry->status);
544 xnb_dump_rings(SYSCTL_HANDLER_ARGS)
546 static char results[720];
547 struct xnb_softc const* xnb = (struct xnb_softc*)arg1;
548 netif_rx_back_ring_t const* rxb =
549 &xnb->ring_configs[XNB_RING_TYPE_RX].back_ring.rx_ring;
550 netif_tx_back_ring_t const* txb =
551 &xnb->ring_configs[XNB_RING_TYPE_TX].back_ring.tx_ring;
553 /* empty the result strings */
556 if ( !txb || !txb->sring || !rxb || !rxb->sring )
557 return (SYSCTL_OUT(req, results, strnlen(results, 720)));
559 snprintf(results, 720,
560 "\n\t%35s %18s\n" /* TX, RX */
561 "\t%16s %18d %18d\n" /* req_cons */
562 "\t%16s %18d %18d\n" /* nr_ents */
563 "\t%16s %18d %18d\n" /* rsp_prod_pvt */
564 "\t%16s %18p %18p\n" /* sring */
565 "\t%16s %18d %18d\n" /* req_prod */
566 "\t%16s %18d %18d\n" /* req_event */
567 "\t%16s %18d %18d\n" /* rsp_prod */
568 "\t%16s %18d %18d\n", /* rsp_event */
570 "req_cons", txb->req_cons, rxb->req_cons,
571 "nr_ents", txb->nr_ents, rxb->nr_ents,
572 "rsp_prod_pvt", txb->rsp_prod_pvt, rxb->rsp_prod_pvt,
573 "sring", txb->sring, rxb->sring,
574 "sring->req_prod", txb->sring->req_prod, rxb->sring->req_prod,
575 "sring->req_event", txb->sring->req_event, rxb->sring->req_event,
576 "sring->rsp_prod", txb->sring->rsp_prod, rxb->sring->rsp_prod,
577 "sring->rsp_event", txb->sring->rsp_event, rxb->sring->rsp_event);
579 return (SYSCTL_OUT(req, results, strnlen(results, 720)));
583 xnb_dump_mbuf(const struct mbuf *m)
590 printf("xnb_dump_mbuf:\n");
591 if (m->m_flags & M_PKTHDR) {
592 printf(" flowid=%10d, csum_flags=%#8x, csum_data=%#8x, "
594 m->m_pkthdr.flowid, (int)m->m_pkthdr.csum_flags,
595 m->m_pkthdr.csum_data, m->m_pkthdr.tso_segsz);
596 printf(" rcvif=%16p, len=%19d\n",
597 m->m_pkthdr.rcvif, m->m_pkthdr.len);
599 printf(" m_next=%16p, m_nextpk=%16p, m_data=%16p\n",
600 m->m_next, m->m_nextpkt, m->m_data);
601 printf(" m_len=%17d, m_flags=%#15x, m_type=%18u\n",
602 m->m_len, m->m_flags, m->m_type);
605 d = mtod(m, uint8_t*);
609 for (i = 0; (i < 16) && (len > 0); i++, len--) {
610 printf("%02hhx ", *(d++));
615 #endif /* XNB_DEBUG */
617 /*------------------------ Inter-Domain Communication ------------------------*/
619 * Free dynamically allocated KVA or pseudo-physical address allocations.
621 * \param xnb Per-instance xnb configuration structure.
624 xnb_free_communication_mem(struct xnb_softc *xnb)
627 if (xnb->pseudo_phys_res != NULL) {
628 xenmem_free(xnb->dev, xnb->pseudo_phys_res_id,
629 xnb->pseudo_phys_res);
630 xnb->pseudo_phys_res = NULL;
634 xnb->gnt_base_addr = 0;
638 * Cleanup all inter-domain communication mechanisms.
640 * \param xnb Per-instance xnb configuration structure.
643 xnb_disconnect(struct xnb_softc *xnb)
645 struct gnttab_unmap_grant_ref gnts[XNB_NUM_RING_TYPES];
649 if (xnb->xen_intr_handle != NULL)
650 xen_intr_unbind(&xnb->xen_intr_handle);
653 * We may still have another thread currently processing requests. We
654 * must acquire the rx and tx locks to make sure those threads are done,
655 * but we can release those locks as soon as we acquire them, because no
656 * more interrupts will be arriving.
658 mtx_lock(&xnb->tx_lock);
659 mtx_unlock(&xnb->tx_lock);
660 mtx_lock(&xnb->rx_lock);
661 mtx_unlock(&xnb->rx_lock);
663 mtx_lock(&xnb->sc_lock);
664 /* Free malloc'd softc member variables */
665 if (xnb->bridge != NULL) {
666 free(xnb->bridge, M_XENSTORE);
670 /* All request processing has stopped, so unmap the rings */
671 for (i=0; i < XNB_NUM_RING_TYPES; i++) {
672 gnts[i].host_addr = xnb->ring_configs[i].gnt_addr;
673 gnts[i].dev_bus_addr = xnb->ring_configs[i].bus_addr;
674 gnts[i].handle = xnb->ring_configs[i].handle;
676 error = HYPERVISOR_grant_table_op(GNTTABOP_unmap_grant_ref, gnts,
678 KASSERT(error == 0, ("Grant table unmap op failed (%d)", error));
680 xnb_free_communication_mem(xnb);
682 * Zero the ring config structs because the pointers, handles, and
683 * grant refs contained therein are no longer valid.
685 bzero(&xnb->ring_configs[XNB_RING_TYPE_TX],
686 sizeof(struct xnb_ring_config));
687 bzero(&xnb->ring_configs[XNB_RING_TYPE_RX],
688 sizeof(struct xnb_ring_config));
690 xnb->flags &= ~XNBF_RING_CONNECTED;
691 mtx_unlock(&xnb->sc_lock);
697 * Map a single shared memory ring into domain local address space and
698 * initialize its control structure
700 * \param xnb Per-instance xnb configuration structure
701 * \param ring_type Array index of this ring in the xnb's array of rings
705 xnb_connect_ring(struct xnb_softc *xnb, xnb_ring_type_t ring_type)
707 struct gnttab_map_grant_ref gnt;
708 struct xnb_ring_config *ring = &xnb->ring_configs[ring_type];
711 /* TX ring type = 0, RX =1 */
712 ring->va = xnb->kva + ring_type * PAGE_SIZE;
713 ring->gnt_addr = xnb->gnt_base_addr + ring_type * PAGE_SIZE;
715 gnt.host_addr = ring->gnt_addr;
716 gnt.flags = GNTMAP_host_map;
717 gnt.ref = ring->ring_ref;
718 gnt.dom = xnb->otherend_id;
720 error = HYPERVISOR_grant_table_op(GNTTABOP_map_grant_ref, &gnt, 1);
722 panic("netback: Ring page grant table op failed (%d)", error);
724 if (gnt.status != 0) {
727 xenbus_dev_fatal(xnb->dev, error,
728 "Ring shared page mapping failed. "
729 "Status %d.", gnt.status);
731 ring->handle = gnt.handle;
732 ring->bus_addr = gnt.dev_bus_addr;
734 if (ring_type == XNB_RING_TYPE_TX) {
735 BACK_RING_INIT(&ring->back_ring.tx_ring,
736 (netif_tx_sring_t*)ring->va,
737 ring->ring_pages * PAGE_SIZE);
738 } else if (ring_type == XNB_RING_TYPE_RX) {
739 BACK_RING_INIT(&ring->back_ring.rx_ring,
740 (netif_rx_sring_t*)ring->va,
741 ring->ring_pages * PAGE_SIZE);
743 xenbus_dev_fatal(xnb->dev, error,
744 "Unknown ring type %d", ring_type);
752 * Setup the shared memory rings and bind an interrupt to the event channel
753 * used to notify us of ring changes.
755 * \param xnb Per-instance xnb configuration structure.
758 xnb_connect_comms(struct xnb_softc *xnb)
763 if ((xnb->flags & XNBF_RING_CONNECTED) != 0)
767 * Kva for our rings are at the tail of the region of kva allocated
768 * by xnb_alloc_communication_mem().
770 for (i=0; i < XNB_NUM_RING_TYPES; i++) {
771 error = xnb_connect_ring(xnb, i);
776 xnb->flags |= XNBF_RING_CONNECTED;
778 error = xen_intr_bind_remote_port(xnb->dev,
782 xnb_intr, /*arg*/xnb,
783 INTR_TYPE_NET | INTR_MPSAFE,
784 &xnb->xen_intr_handle);
786 (void)xnb_disconnect(xnb);
787 xenbus_dev_fatal(xnb->dev, error, "binding event channel");
791 DPRINTF("rings connected!\n");
797 * Size KVA and pseudo-physical address allocations based on negotiated
798 * values for the size and number of I/O requests, and the size of our
799 * communication ring.
801 * \param xnb Per-instance xnb configuration structure.
803 * These address spaces are used to dynamically map pages in the
804 * front-end's domain into our own.
807 xnb_alloc_communication_mem(struct xnb_softc *xnb)
812 for (i=0; i < XNB_NUM_RING_TYPES; i++) {
813 xnb->kva_size += xnb->ring_configs[i].ring_pages * PAGE_SIZE;
817 * Reserve a range of pseudo physical memory that we can map
818 * into kva. These pages will only be backed by machine
819 * pages ("real memory") during the lifetime of front-end requests
820 * via grant table operations. We will map the netif tx and rx rings
823 xnb->pseudo_phys_res_id = 0;
824 xnb->pseudo_phys_res = xenmem_alloc(xnb->dev, &xnb->pseudo_phys_res_id,
826 if (xnb->pseudo_phys_res == NULL) {
830 xnb->kva = (vm_offset_t)rman_get_virtual(xnb->pseudo_phys_res);
831 xnb->gnt_base_addr = rman_get_start(xnb->pseudo_phys_res);
836 * Collect information from the XenStore related to our device and its frontend
838 * \param xnb Per-instance xnb configuration structure.
841 xnb_collect_xenstore_info(struct xnb_softc *xnb)
844 * \todo Linux collects the following info. We should collect most
846 * "feature-rx-notify"
848 const char *otherend_path;
849 const char *our_path;
851 unsigned int rx_copy, bridge_len;
852 uint8_t no_csum_offload;
854 otherend_path = xenbus_get_otherend_path(xnb->dev);
855 our_path = xenbus_get_node(xnb->dev);
857 /* Collect the critical communication parameters */
858 err = xs_gather(XST_NIL, otherend_path,
859 "tx-ring-ref", "%l" PRIu32,
860 &xnb->ring_configs[XNB_RING_TYPE_TX].ring_ref,
861 "rx-ring-ref", "%l" PRIu32,
862 &xnb->ring_configs[XNB_RING_TYPE_RX].ring_ref,
863 "event-channel", "%" PRIu32, &xnb->evtchn,
866 xenbus_dev_fatal(xnb->dev, err,
867 "Unable to retrieve ring information from "
868 "frontend %s. Unable to connect.",
873 /* Collect the handle from xenstore */
874 err = xs_scanf(XST_NIL, our_path, "handle", NULL, "%li", &xnb->handle);
876 xenbus_dev_fatal(xnb->dev, err,
877 "Error reading handle from frontend %s. "
878 "Unable to connect.", otherend_path);
882 * Collect the bridgename, if any. We do not need bridge_len; we just
885 err = xs_read(XST_NIL, our_path, "bridge", &bridge_len,
886 (void**)&xnb->bridge);
891 * Does the frontend request that we use rx copy? If not, return an
892 * error because this driver only supports rx copy.
894 err = xs_scanf(XST_NIL, otherend_path, "request-rx-copy", NULL,
895 "%" PRIu32, &rx_copy);
901 xenbus_dev_fatal(xnb->dev, err, "reading %s/request-rx-copy",
906 * \todo: figure out the exact meaning of this feature, and when
907 * the frontend will set it to true. It should be set to true
911 /* return EOPNOTSUPP;*/
913 /** \todo Collect the rx notify feature */
915 /* Collect the feature-sg. */
916 if (xs_scanf(XST_NIL, otherend_path, "feature-sg", NULL,
917 "%hhu", &xnb->can_sg) < 0)
920 /* Collect remaining frontend features */
921 if (xs_scanf(XST_NIL, otherend_path, "feature-gso-tcpv4", NULL,
922 "%hhu", &xnb->gso) < 0)
925 if (xs_scanf(XST_NIL, otherend_path, "feature-gso-tcpv4-prefix", NULL,
926 "%hhu", &xnb->gso_prefix) < 0)
929 if (xs_scanf(XST_NIL, otherend_path, "feature-no-csum-offload", NULL,
930 "%hhu", &no_csum_offload) < 0)
932 xnb->ip_csum = (no_csum_offload == 0);
938 * Supply information about the physical device to the frontend
941 * \param xnb Per-instance xnb configuration structure.
944 xnb_publish_backend_info(struct xnb_softc *xnb)
946 struct xs_transaction xst;
947 const char *our_path;
950 our_path = xenbus_get_node(xnb->dev);
953 error = xs_transaction_start(&xst);
955 xenbus_dev_fatal(xnb->dev, error,
956 "Error publishing backend info "
957 "(start transaction)");
961 error = xs_printf(xst, our_path, "feature-sg",
966 error = xs_printf(xst, our_path, "feature-gso-tcpv4",
967 "%d", XNB_GSO_TCPV4);
971 error = xs_printf(xst, our_path, "feature-rx-copy",
976 error = xs_printf(xst, our_path, "feature-rx-flip",
981 error = xs_transaction_end(xst, 0);
982 if (error != 0 && error != EAGAIN) {
983 xenbus_dev_fatal(xnb->dev, error, "ending transaction");
987 } while (error == EAGAIN);
993 * Connect to our netfront peer now that it has completed publishing
994 * its configuration into the XenStore.
996 * \param xnb Per-instance xnb configuration structure.
999 xnb_connect(struct xnb_softc *xnb)
1003 if (xenbus_get_state(xnb->dev) == XenbusStateConnected)
1006 if (xnb_collect_xenstore_info(xnb) != 0)
1009 xnb->flags &= ~XNBF_SHUTDOWN;
1011 /* Read front end configuration. */
1013 /* Allocate resources whose size depends on front-end configuration. */
1014 error = xnb_alloc_communication_mem(xnb);
1016 xenbus_dev_fatal(xnb->dev, error,
1017 "Unable to allocate communication memory");
1022 * Connect communication channel.
1024 error = xnb_connect_comms(xnb);
1026 /* Specific errors are reported by xnb_connect_comms(). */
1031 /* Ready for I/O. */
1032 xenbus_set_state(xnb->dev, XenbusStateConnected);
1035 /*-------------------------- Device Teardown Support -------------------------*/
1037 * Perform device shutdown functions.
1039 * \param xnb Per-instance xnb configuration structure.
1041 * Mark this instance as shutting down, wait for any active requests
1042 * to drain, disconnect from the front-end, and notify any waiters (e.g.
1043 * a thread invoking our detach method) that detach can now proceed.
1046 xnb_shutdown(struct xnb_softc *xnb)
1049 * Due to the need to drop our mutex during some
1050 * xenbus operations, it is possible for two threads
1051 * to attempt to close out shutdown processing at
1052 * the same time. Tell the caller that hits this
1053 * race to try back later.
1055 if ((xnb->flags & XNBF_IN_SHUTDOWN) != 0)
1058 xnb->flags |= XNBF_SHUTDOWN;
1060 xnb->flags |= XNBF_IN_SHUTDOWN;
1062 mtx_unlock(&xnb->sc_lock);
1063 /* Free the network interface */
1065 if (xnb->xnb_ifp != NULL) {
1066 ether_ifdetach(xnb->xnb_ifp);
1067 if_free(xnb->xnb_ifp);
1068 xnb->xnb_ifp = NULL;
1071 xnb_disconnect(xnb);
1073 if (xenbus_get_state(xnb->dev) < XenbusStateClosing)
1074 xenbus_set_state(xnb->dev, XenbusStateClosing);
1075 mtx_lock(&xnb->sc_lock);
1077 xnb->flags &= ~XNBF_IN_SHUTDOWN;
1079 /* Indicate to xnb_detach() that is it safe to proceed. */
1086 * Report an attach time error to the console and Xen, and cleanup
1087 * this instance by forcing immediate detach processing.
1089 * \param xnb Per-instance xnb configuration structure.
1090 * \param err Errno describing the error.
1091 * \param fmt Printf style format and arguments
1094 xnb_attach_failed(struct xnb_softc *xnb, int err, const char *fmt, ...)
1100 va_copy(ap_hotplug, ap);
1101 xs_vprintf(XST_NIL, xenbus_get_node(xnb->dev),
1102 "hotplug-error", fmt, ap_hotplug);
1104 (void)xs_printf(XST_NIL, xenbus_get_node(xnb->dev),
1105 "hotplug-status", "error");
1107 xenbus_dev_vfatal(xnb->dev, err, fmt, ap);
1110 (void)xs_printf(XST_NIL, xenbus_get_node(xnb->dev), "online", "0");
1111 xnb_detach(xnb->dev);
1114 /*---------------------------- NewBus Entrypoints ----------------------------*/
1116 * Inspect a XenBus device and claim it if is of the appropriate type.
1118 * \param dev NewBus device object representing a candidate XenBus device.
1120 * \return 0 for success, errno codes for failure.
1123 xnb_probe(device_t dev)
1125 if (!strcmp(xenbus_get_type(dev), "vif")) {
1126 DPRINTF("Claiming device %d, %s\n", device_get_unit(dev),
1127 devclass_get_name(device_get_devclass(dev)));
1128 device_set_desc(dev, "Backend Virtual Network Device");
1136 * Setup sysctl variables to control various Network Back parameters.
1138 * \param xnb Xen Net Back softc.
1142 xnb_setup_sysctl(struct xnb_softc *xnb)
1144 struct sysctl_ctx_list *sysctl_ctx = NULL;
1145 struct sysctl_oid *sysctl_tree = NULL;
1147 sysctl_ctx = device_get_sysctl_ctx(xnb->dev);
1148 if (sysctl_ctx == NULL)
1151 sysctl_tree = device_get_sysctl_tree(xnb->dev);
1152 if (sysctl_tree == NULL)
1156 SYSCTL_ADD_PROC(sysctl_ctx,
1157 SYSCTL_CHILDREN(sysctl_tree),
1159 "unit_test_results",
1160 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_NEEDGIANT,
1165 "Results of builtin unit tests");
1167 SYSCTL_ADD_PROC(sysctl_ctx,
1168 SYSCTL_CHILDREN(sysctl_tree),
1171 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_NEEDGIANT,
1176 "Xennet Back Rings");
1177 #endif /* XNB_DEBUG */
1181 * Create a network device.
1182 * @param handle device handle
1185 create_netdev(device_t dev)
1188 struct xnb_softc *xnb;
1192 xnb = device_get_softc(dev);
1193 mtx_init(&xnb->sc_lock, "xnb_softc", "xen netback softc lock", MTX_DEF);
1194 mtx_init(&xnb->tx_lock, "xnb_tx", "xen netback tx lock", MTX_DEF);
1195 mtx_init(&xnb->rx_lock, "xnb_rx", "xen netback rx lock", MTX_DEF);
1199 ifmedia_init(&xnb->sc_media, 0, xnb_ifmedia_upd, xnb_ifmedia_sts);
1200 ifmedia_add(&xnb->sc_media, IFM_ETHER|IFM_MANUAL, 0, NULL);
1201 ifmedia_set(&xnb->sc_media, IFM_ETHER|IFM_MANUAL);
1204 * Set the MAC address to a dummy value (00:00:00:00:00),
1205 * if the MAC address of the host-facing interface is set
1206 * to the same as the guest-facing one (the value found in
1207 * xenstore), the bridge would stop delivering packets to
1208 * us because it would see that the destination address of
1209 * the packet is the same as the interface, and so the bridge
1210 * would expect the packet has already been delivered locally
1211 * (and just drop it).
1213 bzero(&xnb->mac[0], sizeof(xnb->mac));
1215 /* The interface will be named using the following nomenclature:
1217 * xnb<domid>.<handle>
1219 * Where handle is the oder of the interface referred to the guest.
1221 err = xs_scanf(XST_NIL, xenbus_get_node(xnb->dev), "handle", NULL,
1222 "%" PRIu32, &handle);
1225 snprintf(xnb->if_name, IFNAMSIZ, "xnb%" PRIu16 ".%" PRIu32,
1226 xenbus_get_otherend_id(dev), handle);
1229 /* Set up ifnet structure */
1230 ifp = xnb->xnb_ifp = if_alloc(IFT_ETHER);
1231 ifp->if_softc = xnb;
1232 if_initname(ifp, xnb->if_name, IF_DUNIT_NONE);
1233 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
1234 ifp->if_ioctl = xnb_ioctl;
1235 ifp->if_start = xnb_start;
1236 ifp->if_init = xnb_ifinit;
1237 ifp->if_mtu = ETHERMTU;
1238 ifp->if_snd.ifq_maxlen = NET_RX_RING_SIZE - 1;
1240 ifp->if_hwassist = XNB_CSUM_FEATURES;
1241 ifp->if_capabilities = IFCAP_HWCSUM;
1242 ifp->if_capenable = IFCAP_HWCSUM;
1244 ether_ifattach(ifp, xnb->mac);
1252 * Attach to a XenBus device that has been claimed by our probe routine.
1254 * \param dev NewBus device object representing this Xen Net Back instance.
1256 * \return 0 for success, errno codes for failure.
1259 xnb_attach(device_t dev)
1261 struct xnb_softc *xnb;
1265 error = create_netdev(dev);
1267 xenbus_dev_fatal(dev, error, "creating netdev");
1271 DPRINTF("Attaching to %s\n", xenbus_get_node(dev));
1274 * Basic initialization.
1275 * After this block it is safe to call xnb_detach()
1276 * to clean up any allocated data for this instance.
1278 xnb = device_get_softc(dev);
1279 xnb->otherend_id = xenbus_get_otherend_id(dev);
1280 for (i=0; i < XNB_NUM_RING_TYPES; i++) {
1281 xnb->ring_configs[i].ring_pages = 1;
1285 * Setup sysctl variables.
1287 xnb_setup_sysctl(xnb);
1289 /* Update hot-plug status to satisfy xend. */
1290 error = xs_printf(XST_NIL, xenbus_get_node(xnb->dev),
1291 "hotplug-status", "connected");
1293 xnb_attach_failed(xnb, error, "writing %s/hotplug-status",
1294 xenbus_get_node(xnb->dev));
1298 if ((error = xnb_publish_backend_info(xnb)) != 0) {
1300 * If we can't publish our data, we cannot participate
1301 * in this connection, and waiting for a front-end state
1302 * change will not help the situation.
1304 xnb_attach_failed(xnb, error,
1305 "Publishing backend status for %s",
1306 xenbus_get_node(xnb->dev));
1310 /* Tell the front end that we are ready to connect. */
1311 xenbus_set_state(dev, XenbusStateInitWait);
1317 * Detach from a net back device instance.
1319 * \param dev NewBus device object representing this Xen Net Back instance.
1321 * \return 0 for success, errno codes for failure.
1323 * \note A net back device may be detached at any time in its life-cycle,
1324 * including part way through the attach process. For this reason,
1325 * initialization order and the initialization state checks in this
1326 * routine must be carefully coupled so that attach time failures
1327 * are gracefully handled.
1330 xnb_detach(device_t dev)
1332 struct xnb_softc *xnb;
1336 xnb = device_get_softc(dev);
1337 mtx_lock(&xnb->sc_lock);
1338 while (xnb_shutdown(xnb) == EAGAIN) {
1339 msleep(xnb, &xnb->sc_lock, /*wakeup prio unchanged*/0,
1342 mtx_unlock(&xnb->sc_lock);
1345 mtx_destroy(&xnb->tx_lock);
1346 mtx_destroy(&xnb->rx_lock);
1347 mtx_destroy(&xnb->sc_lock);
1352 * Prepare this net back device for suspension of this VM.
1354 * \param dev NewBus device object representing this Xen net Back instance.
1356 * \return 0 for success, errno codes for failure.
1359 xnb_suspend(device_t dev)
1365 * Perform any processing required to recover from a suspended state.
1367 * \param dev NewBus device object representing this Xen Net Back instance.
1369 * \return 0 for success, errno codes for failure.
1372 xnb_resume(device_t dev)
1378 * Handle state changes expressed via the XenStore by our front-end peer.
1380 * \param dev NewBus device object representing this Xen
1381 * Net Back instance.
1382 * \param frontend_state The new state of the front-end.
1384 * \return 0 for success, errno codes for failure.
1387 xnb_frontend_changed(device_t dev, XenbusState frontend_state)
1389 struct xnb_softc *xnb;
1391 xnb = device_get_softc(dev);
1393 DPRINTF("frontend_state=%s, xnb_state=%s\n",
1394 xenbus_strstate(frontend_state),
1395 xenbus_strstate(xenbus_get_state(xnb->dev)));
1397 switch (frontend_state) {
1398 case XenbusStateInitialising:
1400 case XenbusStateInitialised:
1401 case XenbusStateConnected:
1404 case XenbusStateClosing:
1405 case XenbusStateClosed:
1406 mtx_lock(&xnb->sc_lock);
1408 mtx_unlock(&xnb->sc_lock);
1409 if (frontend_state == XenbusStateClosed)
1410 xenbus_set_state(xnb->dev, XenbusStateClosed);
1413 xenbus_dev_fatal(xnb->dev, EINVAL, "saw state %d at frontend",
1420 /*---------------------------- Request Processing ----------------------------*/
1422 * Interrupt handler bound to the shared ring's event channel.
1423 * Entry point for the xennet transmit path in netback
1424 * Transfers packets from the Xen ring to the host's generic networking stack
1426 * \param arg Callback argument registerd during event channel
1427 * binding - the xnb_softc for this instance.
1432 struct xnb_softc *xnb;
1434 netif_tx_back_ring_t *txb;
1435 RING_IDX req_prod_local;
1437 xnb = (struct xnb_softc *)arg;
1439 txb = &xnb->ring_configs[XNB_RING_TYPE_TX].back_ring.tx_ring;
1441 mtx_lock(&xnb->tx_lock);
1444 req_prod_local = txb->sring->req_prod;
1451 err = xnb_recv(txb, xnb->otherend_id, &mbufc, ifp,
1453 if (err || (mbufc == NULL))
1456 /* Send the packet to the generic network stack */
1457 (*xnb->xnb_ifp->if_input)(xnb->xnb_ifp, mbufc);
1460 RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(txb, notify);
1462 xen_intr_signal(xnb->xen_intr_handle);
1464 txb->sring->req_event = txb->req_cons + 1;
1466 } while (txb->sring->req_prod != req_prod_local) ;
1467 mtx_unlock(&xnb->tx_lock);
1474 * Build a struct xnb_pkt based on netif_tx_request's from a netif tx ring.
1475 * Will read exactly 0 or 1 packets from the ring; never a partial packet.
1476 * \param[out] pkt The returned packet. If there is an error building
1477 * the packet, pkt.list_len will be set to 0.
1478 * \param[in] tx_ring Pointer to the Ring that is the input to this function
1479 * \param[in] start The ring index of the first potential request
1480 * \return The number of requests consumed to build this packet
1483 xnb_ring2pkt(struct xnb_pkt *pkt, const netif_tx_back_ring_t *tx_ring,
1489 * 2) Read the first request of the packet
1490 * 3) Read the extras
1492 * 5) Loop on the remainder of the packet
1493 * 6) Finalize pkt (stuff like car_size and list_len)
1496 int discard = 0; /* whether to discard the packet */
1497 int more_data = 0; /* there are more request past the last one */
1498 uint16_t cdr_size = 0; /* accumulated size of requests 2 through n */
1500 xnb_pkt_initialize(pkt);
1502 /* Read the first request */
1503 if (RING_HAS_UNCONSUMED_REQUESTS_2(tx_ring, idx)) {
1504 netif_tx_request_t *tx = RING_GET_REQUEST(tx_ring, idx);
1505 pkt->size = tx->size;
1506 pkt->flags = tx->flags & ~NETTXF_more_data;
1507 more_data = tx->flags & NETTXF_more_data;
1513 /* Read the extra info */
1514 if ((pkt->flags & NETTXF_extra_info) &&
1515 RING_HAS_UNCONSUMED_REQUESTS_2(tx_ring, idx)) {
1516 netif_extra_info_t *ext =
1517 (netif_extra_info_t*) RING_GET_REQUEST(tx_ring, idx);
1518 pkt->extra.type = ext->type;
1519 switch (pkt->extra.type) {
1520 case XEN_NETIF_EXTRA_TYPE_GSO:
1521 pkt->extra.u.gso = ext->u.gso;
1525 * The reference Linux netfront driver will
1526 * never set any other extra.type. So we don't
1527 * know what to do with it. Let's print an
1528 * error, then consume and discard the packet
1530 printf("xnb(%s:%d): Unknown extra info type %d."
1531 " Discarding packet\n",
1532 __func__, __LINE__, pkt->extra.type);
1533 xnb_dump_txreq(start, RING_GET_REQUEST(tx_ring,
1535 xnb_dump_txreq(idx, RING_GET_REQUEST(tx_ring,
1541 pkt->extra.flags = ext->flags;
1542 if (ext->flags & XEN_NETIF_EXTRA_FLAG_MORE) {
1544 * The reference linux netfront driver never sets this
1545 * flag (nor does any other known netfront). So we
1546 * will discard the packet.
1548 printf("xnb(%s:%d): Request sets "
1549 "XEN_NETIF_EXTRA_FLAG_MORE, but we can't handle "
1550 "that\n", __func__, __LINE__);
1551 xnb_dump_txreq(start, RING_GET_REQUEST(tx_ring, start));
1552 xnb_dump_txreq(idx, RING_GET_REQUEST(tx_ring, idx));
1559 /* Set cdr. If there is not more data, cdr is invalid */
1562 /* Loop on remainder of packet */
1563 while (more_data && RING_HAS_UNCONSUMED_REQUESTS_2(tx_ring, idx)) {
1564 netif_tx_request_t *tx = RING_GET_REQUEST(tx_ring, idx);
1566 cdr_size += tx->size;
1567 if (tx->flags & ~NETTXF_more_data) {
1568 /* There should be no other flags set at this point */
1569 printf("xnb(%s:%d): Request sets unknown flags %d "
1570 "after the 1st request in the packet.\n",
1571 __func__, __LINE__, tx->flags);
1572 xnb_dump_txreq(start, RING_GET_REQUEST(tx_ring, start));
1573 xnb_dump_txreq(idx, RING_GET_REQUEST(tx_ring, idx));
1576 more_data = tx->flags & NETTXF_more_data;
1580 /* Finalize packet */
1581 if (more_data != 0) {
1582 /* The ring ran out of requests before finishing the packet */
1583 xnb_pkt_invalidate(pkt);
1584 idx = start; /* tell caller that we consumed no requests */
1586 /* Calculate car_size */
1587 pkt->car_size = pkt->size - cdr_size;
1590 xnb_pkt_invalidate(pkt);
1598 * Respond to all the requests that constituted pkt. Builds the responses and
1599 * writes them to the ring, but doesn't push them to the shared ring.
1600 * \param[in] pkt the packet that needs a response
1601 * \param[in] error true if there was an error handling the packet, such
1602 * as in the hypervisor copy op or mbuf allocation
1603 * \param[out] ring Responses go here
1606 xnb_txpkt2rsp(const struct xnb_pkt *pkt, netif_tx_back_ring_t *ring,
1611 * 1) Respond to the first request
1612 * 2) Respond to the extra info reques
1613 * Loop through every remaining request in the packet, generating
1614 * responses that copy those requests' ids and sets the status
1617 netif_tx_request_t *tx;
1618 netif_tx_response_t *rsp;
1622 status = (xnb_pkt_is_valid(pkt) == 0) || error ?
1623 NETIF_RSP_ERROR : NETIF_RSP_OKAY;
1624 KASSERT((pkt->list_len == 0) || (ring->rsp_prod_pvt == pkt->car),
1625 ("Cannot respond to ring requests out of order"));
1627 if (pkt->list_len >= 1) {
1629 tx = RING_GET_REQUEST(ring, ring->rsp_prod_pvt);
1631 rsp = RING_GET_RESPONSE(ring, ring->rsp_prod_pvt);
1633 rsp->status = status;
1634 ring->rsp_prod_pvt++;
1636 if (pkt->flags & NETRXF_extra_info) {
1637 rsp = RING_GET_RESPONSE(ring, ring->rsp_prod_pvt);
1638 rsp->status = NETIF_RSP_NULL;
1639 ring->rsp_prod_pvt++;
1643 for (i=0; i < pkt->list_len - 1; i++) {
1645 tx = RING_GET_REQUEST(ring, ring->rsp_prod_pvt);
1647 rsp = RING_GET_RESPONSE(ring, ring->rsp_prod_pvt);
1649 rsp->status = status;
1650 ring->rsp_prod_pvt++;
1655 * Create an mbuf chain to represent a packet. Initializes all of the headers
1656 * in the mbuf chain, but does not copy the data. The returned chain must be
1657 * free()'d when no longer needed
1658 * \param[in] pkt A packet to model the mbuf chain after
1659 * \return A newly allocated mbuf chain, possibly with clusters attached.
1663 xnb_pkt2mbufc(const struct xnb_pkt *pkt, struct ifnet *ifp)
1666 * \todo consider using a memory pool for mbufs instead of
1667 * reallocating them for every packet
1669 /** \todo handle extra data */
1672 m = m_getm(NULL, pkt->size, M_NOWAIT, MT_DATA);
1675 m->m_pkthdr.rcvif = ifp;
1676 if (pkt->flags & NETTXF_data_validated) {
1678 * We lie to the host OS and always tell it that the
1679 * checksums are ok, because the packet is unlikely to
1680 * get corrupted going across domains.
1682 m->m_pkthdr.csum_flags = (
1688 m->m_pkthdr.csum_data = 0xffff;
1695 * Build a gnttab_copy table that can be used to copy data from a pkt
1696 * to an mbufc. Does not actually perform the copy. Always uses gref's on
1698 * \param[in] pkt pkt's associated requests form the src for
1699 * the copy operation
1700 * \param[in] mbufc mbufc's storage forms the dest for the copy operation
1701 * \param[out] gnttab Storage for the returned grant table
1702 * \param[in] txb Pointer to the backend ring structure
1703 * \param[in] otherend_id The domain ID of the other end of the copy
1704 * \return The number of gnttab entries filled
1707 xnb_txpkt2gnttab(const struct xnb_pkt *pkt, struct mbuf *mbufc,
1708 gnttab_copy_table gnttab, const netif_tx_back_ring_t *txb,
1709 domid_t otherend_id)
1712 struct mbuf *mbuf = mbufc;/* current mbuf within the chain */
1713 int gnt_idx = 0; /* index into grant table */
1714 RING_IDX r_idx = pkt->car; /* index into tx ring buffer */
1715 int r_ofs = 0; /* offset of next data within tx request's data area */
1716 int m_ofs = 0; /* offset of next data within mbuf's data area */
1717 /* size in bytes that still needs to be represented in the table */
1718 uint16_t size_remaining = pkt->size;
1720 while (size_remaining > 0) {
1721 const netif_tx_request_t *txq = RING_GET_REQUEST(txb, r_idx);
1722 const size_t mbuf_space = M_TRAILINGSPACE(mbuf) - m_ofs;
1723 const size_t req_size =
1724 r_idx == pkt->car ? pkt->car_size : txq->size;
1725 const size_t pkt_space = req_size - r_ofs;
1727 * space is the largest amount of data that can be copied in the
1728 * grant table's next entry
1730 const size_t space = MIN(pkt_space, mbuf_space);
1732 /* TODO: handle this error condition without panicking */
1733 KASSERT(gnt_idx < GNTTAB_LEN, ("Grant table is too short"));
1735 gnttab[gnt_idx].source.u.ref = txq->gref;
1736 gnttab[gnt_idx].source.domid = otherend_id;
1737 gnttab[gnt_idx].source.offset = txq->offset + r_ofs;
1738 gnttab[gnt_idx].dest.u.gmfn = virt_to_mfn(
1739 mtod(mbuf, vm_offset_t) + m_ofs);
1740 gnttab[gnt_idx].dest.offset = virt_to_offset(
1741 mtod(mbuf, vm_offset_t) + m_ofs);
1742 gnttab[gnt_idx].dest.domid = DOMID_SELF;
1743 gnttab[gnt_idx].len = space;
1744 gnttab[gnt_idx].flags = GNTCOPY_source_gref;
1749 size_remaining -= space;
1750 if (req_size - r_ofs <= 0) {
1751 /* Must move to the next tx request */
1753 r_idx = (r_idx == pkt->car) ? pkt->cdr : r_idx + 1;
1755 if (M_TRAILINGSPACE(mbuf) - m_ofs <= 0) {
1756 /* Must move to the next mbuf */
1758 mbuf = mbuf->m_next;
1766 * Check the status of the grant copy operations, and update mbufs various
1767 * non-data fields to reflect the data present.
1768 * \param[in,out] mbufc mbuf chain to update. The chain must be valid and of
1769 * the correct length, and data should already be present
1770 * \param[in] gnttab A grant table for a just completed copy op
1771 * \param[in] n_entries The number of valid entries in the grant table
1774 xnb_update_mbufc(struct mbuf *mbufc, const gnttab_copy_table gnttab,
1777 struct mbuf *mbuf = mbufc;
1779 size_t total_size = 0;
1781 for (i = 0; i < n_entries; i++) {
1782 KASSERT(gnttab[i].status == GNTST_okay,
1783 ("Some gnttab_copy entry had error status %hd\n",
1786 mbuf->m_len += gnttab[i].len;
1787 total_size += gnttab[i].len;
1788 if (M_TRAILINGSPACE(mbuf) <= 0) {
1789 mbuf = mbuf->m_next;
1792 mbufc->m_pkthdr.len = total_size;
1794 #if defined(INET) || defined(INET6)
1795 xnb_add_mbuf_cksum(mbufc);
1800 * Dequeue at most one packet from the shared ring
1801 * \param[in,out] txb Netif tx ring. A packet will be removed from it, and
1802 * its private indices will be updated. But the indices
1803 * will not be pushed to the shared ring.
1804 * \param[in] ifnet Interface to which the packet will be sent
1805 * \param[in] otherend Domain ID of the other end of the ring
1806 * \param[out] mbufc The assembled mbuf chain, ready to send to the generic
1808 * \param[in,out] gnttab Pointer to enough memory for a grant table. We make
1809 * this a function parameter so that we will take less
1811 * \return An error code
1814 xnb_recv(netif_tx_back_ring_t *txb, domid_t otherend, struct mbuf **mbufc,
1815 struct ifnet *ifnet, gnttab_copy_table gnttab)
1818 /* number of tx requests consumed to build the last packet */
1823 num_consumed = xnb_ring2pkt(&pkt, txb, txb->req_cons);
1824 if (num_consumed == 0)
1825 return 0; /* Nothing to receive */
1827 /* update statistics independent of errors */
1828 if_inc_counter(ifnet, IFCOUNTER_IPACKETS, 1);
1831 * if we got here, then 1 or more requests was consumed, but the packet
1832 * is not necessarily valid.
1834 if (xnb_pkt_is_valid(&pkt) == 0) {
1835 /* got a garbage packet, respond and drop it */
1836 xnb_txpkt2rsp(&pkt, txb, 1);
1837 txb->req_cons += num_consumed;
1838 DPRINTF("xnb_intr: garbage packet, num_consumed=%d\n",
1840 if_inc_counter(ifnet, IFCOUNTER_IERRORS, 1);
1844 *mbufc = xnb_pkt2mbufc(&pkt, ifnet);
1846 if (*mbufc == NULL) {
1848 * Couldn't allocate mbufs. Respond and drop the packet. Do
1849 * not consume the requests
1851 xnb_txpkt2rsp(&pkt, txb, 1);
1852 DPRINTF("xnb_intr: Couldn't allocate mbufs, num_consumed=%d\n",
1854 if_inc_counter(ifnet, IFCOUNTER_IQDROPS, 1);
1858 nr_ents = xnb_txpkt2gnttab(&pkt, *mbufc, gnttab, txb, otherend);
1861 int __unused hv_ret = HYPERVISOR_grant_table_op(GNTTABOP_copy,
1863 KASSERT(hv_ret == 0,
1864 ("HYPERVISOR_grant_table_op returned %d\n", hv_ret));
1865 xnb_update_mbufc(*mbufc, gnttab, nr_ents);
1868 xnb_txpkt2rsp(&pkt, txb, 0);
1869 txb->req_cons += num_consumed;
1874 * Create an xnb_pkt based on the contents of an mbuf chain.
1875 * \param[in] mbufc mbuf chain to transform into a packet
1876 * \param[out] pkt Storage for the newly generated xnb_pkt
1877 * \param[in] start The ring index of the first available slot in the rx
1879 * \param[in] space The number of free slots in the rx ring
1881 * \retval EINVAL mbufc was corrupt or not convertible into a pkt
1882 * \retval EAGAIN There was not enough space in the ring to queue the
1886 xnb_mbufc2pkt(const struct mbuf *mbufc, struct xnb_pkt *pkt,
1887 RING_IDX start, int space)
1892 if ((mbufc == NULL) ||
1893 ( (mbufc->m_flags & M_PKTHDR) == 0) ||
1894 (mbufc->m_pkthdr.len == 0)) {
1895 xnb_pkt_invalidate(pkt);
1900 xnb_pkt_validate(pkt);
1902 pkt->size = mbufc->m_pkthdr.len;
1904 pkt->car_size = mbufc->m_len;
1906 if (mbufc->m_pkthdr.csum_flags & CSUM_TSO) {
1907 pkt->flags |= NETRXF_extra_info;
1908 pkt->extra.u.gso.size = mbufc->m_pkthdr.tso_segsz;
1909 pkt->extra.u.gso.type = XEN_NETIF_GSO_TYPE_TCPV4;
1910 pkt->extra.u.gso.pad = 0;
1911 pkt->extra.u.gso.features = 0;
1912 pkt->extra.type = XEN_NETIF_EXTRA_TYPE_GSO;
1913 pkt->extra.flags = 0;
1914 pkt->cdr = start + 2;
1916 pkt->cdr = start + 1;
1918 if (mbufc->m_pkthdr.csum_flags & (CSUM_TSO | CSUM_DELAY_DATA)) {
1920 (NETRXF_csum_blank | NETRXF_data_validated);
1924 * Each ring response can have up to PAGE_SIZE of data.
1925 * Assume that we can defragment the mbuf chain efficiently
1926 * into responses so that each response but the last uses all
1929 pkt->list_len = howmany(pkt->size, PAGE_SIZE);
1931 if (pkt->list_len > 1) {
1932 pkt->flags |= NETRXF_more_data;
1935 slots_required = pkt->list_len +
1936 (pkt->flags & NETRXF_extra_info ? 1 : 0);
1937 if (slots_required > space) {
1938 xnb_pkt_invalidate(pkt);
1947 * Build a gnttab_copy table that can be used to copy data from an mbuf chain
1948 * to the frontend's shared buffers. Does not actually perform the copy.
1949 * Always uses gref's on the other end's side.
1950 * \param[in] pkt pkt's associated responses form the dest for the copy
1952 * \param[in] mbufc The source for the copy operation
1953 * \param[out] gnttab Storage for the returned grant table
1954 * \param[in] rxb Pointer to the backend ring structure
1955 * \param[in] otherend_id The domain ID of the other end of the copy
1956 * \return The number of gnttab entries filled
1959 xnb_rxpkt2gnttab(const struct xnb_pkt *pkt, const struct mbuf *mbufc,
1960 gnttab_copy_table gnttab, const netif_rx_back_ring_t *rxb,
1961 domid_t otherend_id)
1964 const struct mbuf *mbuf = mbufc;/* current mbuf within the chain */
1965 int gnt_idx = 0; /* index into grant table */
1966 RING_IDX r_idx = pkt->car; /* index into rx ring buffer */
1967 int r_ofs = 0; /* offset of next data within rx request's data area */
1968 int m_ofs = 0; /* offset of next data within mbuf's data area */
1969 /* size in bytes that still needs to be represented in the table */
1970 uint16_t size_remaining;
1972 size_remaining = (xnb_pkt_is_valid(pkt) != 0) ? pkt->size : 0;
1974 while (size_remaining > 0) {
1975 const netif_rx_request_t *rxq = RING_GET_REQUEST(rxb, r_idx);
1976 const size_t mbuf_space = mbuf->m_len - m_ofs;
1977 /* Xen shared pages have an implied size of PAGE_SIZE */
1978 const size_t req_size = PAGE_SIZE;
1979 const size_t pkt_space = req_size - r_ofs;
1981 * space is the largest amount of data that can be copied in the
1982 * grant table's next entry
1984 const size_t space = MIN(pkt_space, mbuf_space);
1986 /* TODO: handle this error condition without panicing */
1987 KASSERT(gnt_idx < GNTTAB_LEN, ("Grant table is too short"));
1989 gnttab[gnt_idx].dest.u.ref = rxq->gref;
1990 gnttab[gnt_idx].dest.domid = otherend_id;
1991 gnttab[gnt_idx].dest.offset = r_ofs;
1992 gnttab[gnt_idx].source.u.gmfn = virt_to_mfn(
1993 mtod(mbuf, vm_offset_t) + m_ofs);
1994 gnttab[gnt_idx].source.offset = virt_to_offset(
1995 mtod(mbuf, vm_offset_t) + m_ofs);
1996 gnttab[gnt_idx].source.domid = DOMID_SELF;
1997 gnttab[gnt_idx].len = space;
1998 gnttab[gnt_idx].flags = GNTCOPY_dest_gref;
2004 size_remaining -= space;
2005 if (req_size - r_ofs <= 0) {
2006 /* Must move to the next rx request */
2008 r_idx = (r_idx == pkt->car) ? pkt->cdr : r_idx + 1;
2010 if (mbuf->m_len - m_ofs <= 0) {
2011 /* Must move to the next mbuf */
2013 mbuf = mbuf->m_next;
2021 * Generates responses for all the requests that constituted pkt. Builds
2022 * responses and writes them to the ring, but doesn't push the shared ring
2024 * \param[in] pkt the packet that needs a response
2025 * \param[in] gnttab The grant copy table corresponding to this packet.
2026 * Used to determine how many rsp->netif_rx_response_t's to
2028 * \param[in] n_entries Number of relevant entries in the grant table
2029 * \param[out] ring Responses go here
2030 * \return The number of RX requests that were consumed to generate
2034 xnb_rxpkt2rsp(const struct xnb_pkt *pkt, const gnttab_copy_table gnttab,
2035 int n_entries, netif_rx_back_ring_t *ring)
2038 * This code makes the following assumptions:
2039 * * All entries in gnttab set GNTCOPY_dest_gref
2040 * * The entries in gnttab are grouped by their grefs: any two
2041 * entries with the same gref must be adjacent
2045 int n_responses = 0;
2046 grant_ref_t last_gref = GRANT_REF_INVALID;
2049 KASSERT(gnttab != NULL, ("Received a null granttable copy"));
2052 * In the event of an error, we only need to send one response to the
2053 * netfront. In that case, we musn't write any data to the responses
2054 * after the one we send. So we must loop all the way through gnttab
2055 * looking for errors before we generate any responses
2057 * Since we're looping through the grant table anyway, we'll count the
2058 * number of different gref's in it, which will tell us how many
2059 * responses to generate
2061 for (gnt_idx = 0; gnt_idx < n_entries; gnt_idx++) {
2062 int16_t status = gnttab[gnt_idx].status;
2063 if (status != GNTST_okay) {
2065 "Got error %d for hypervisor gnttab_copy status\n",
2070 if (gnttab[gnt_idx].dest.u.ref != last_gref) {
2072 last_gref = gnttab[gnt_idx].dest.u.ref;
2078 netif_rx_response_t *rsp;
2080 id = RING_GET_REQUEST(ring, ring->rsp_prod_pvt)->id;
2081 rsp = RING_GET_RESPONSE(ring, ring->rsp_prod_pvt);
2083 rsp->status = NETIF_RSP_ERROR;
2087 const int has_extra = pkt->flags & NETRXF_extra_info;
2091 for (i = 0; i < n_responses; i++) {
2092 netif_rx_request_t rxq;
2093 netif_rx_response_t *rsp;
2095 r_idx = ring->rsp_prod_pvt + i;
2097 * We copy the structure of rxq instead of making a
2098 * pointer because it shares the same memory as rsp.
2100 rxq = *(RING_GET_REQUEST(ring, r_idx));
2101 rsp = RING_GET_RESPONSE(ring, r_idx);
2102 if (has_extra && (i == 1)) {
2103 netif_extra_info_t *ext =
2104 (netif_extra_info_t*)rsp;
2105 ext->type = XEN_NETIF_EXTRA_TYPE_GSO;
2107 ext->u.gso.size = pkt->extra.u.gso.size;
2108 ext->u.gso.type = XEN_NETIF_GSO_TYPE_TCPV4;
2110 ext->u.gso.features = 0;
2113 rsp->status = GNTST_okay;
2116 if (i < pkt->list_len - 1)
2117 rsp->flags |= NETRXF_more_data;
2118 if ((i == 0) && has_extra)
2119 rsp->flags |= NETRXF_extra_info;
2121 (pkt->flags & NETRXF_data_validated)) {
2122 rsp->flags |= NETRXF_data_validated;
2123 rsp->flags |= NETRXF_csum_blank;
2126 for (; gnttab[gnt_idx].dest.u.ref == rxq.gref;
2128 rsp->status += gnttab[gnt_idx].len;
2134 ring->req_cons += n_responses;
2135 ring->rsp_prod_pvt += n_responses;
2139 #if defined(INET) || defined(INET6)
2141 * Add IP, TCP, and/or UDP checksums to every mbuf in a chain. The first mbuf
2142 * in the chain must start with a struct ether_header.
2144 * XXX This function will perform incorrectly on UDP packets that are split up
2145 * into multiple ethernet frames.
2148 xnb_add_mbuf_cksum(struct mbuf *mbufc)
2150 struct ether_header *eh;
2152 uint16_t ether_type;
2154 eh = mtod(mbufc, struct ether_header*);
2155 ether_type = ntohs(eh->ether_type);
2156 if (ether_type != ETHERTYPE_IP) {
2157 /* Nothing to calculate */
2161 iph = (struct ip*)(eh + 1);
2162 if (mbufc->m_pkthdr.csum_flags & CSUM_IP_VALID) {
2164 iph->ip_sum = in_cksum_hdr(iph);
2167 switch (iph->ip_p) {
2169 if (mbufc->m_pkthdr.csum_flags & CSUM_IP_VALID) {
2170 size_t tcplen = ntohs(iph->ip_len) - sizeof(struct ip);
2171 struct tcphdr *th = (struct tcphdr*)(iph + 1);
2172 th->th_sum = in_pseudo(iph->ip_src.s_addr,
2173 iph->ip_dst.s_addr, htons(IPPROTO_TCP + tcplen));
2174 th->th_sum = in_cksum_skip(mbufc,
2175 sizeof(struct ether_header) + ntohs(iph->ip_len),
2176 sizeof(struct ether_header) + (iph->ip_hl << 2));
2180 if (mbufc->m_pkthdr.csum_flags & CSUM_IP_VALID) {
2181 size_t udplen = ntohs(iph->ip_len) - sizeof(struct ip);
2182 struct udphdr *uh = (struct udphdr*)(iph + 1);
2183 uh->uh_sum = in_pseudo(iph->ip_src.s_addr,
2184 iph->ip_dst.s_addr, htons(IPPROTO_UDP + udplen));
2185 uh->uh_sum = in_cksum_skip(mbufc,
2186 sizeof(struct ether_header) + ntohs(iph->ip_len),
2187 sizeof(struct ether_header) + (iph->ip_hl << 2));
2194 #endif /* INET || INET6 */
2197 xnb_stop(struct xnb_softc *xnb)
2201 mtx_assert(&xnb->sc_lock, MA_OWNED);
2203 ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
2204 if_link_state_change(ifp, LINK_STATE_DOWN);
2208 xnb_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
2210 struct xnb_softc *xnb = ifp->if_softc;
2211 struct ifreq *ifr = (struct ifreq*) data;
2213 struct ifaddr *ifa = (struct ifaddr*)data;
2219 mtx_lock(&xnb->sc_lock);
2220 if (ifp->if_flags & IFF_UP) {
2221 xnb_ifinit_locked(xnb);
2223 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
2228 * Note: netfront sets a variable named xn_if_flags
2229 * here, but that variable is never read
2231 mtx_unlock(&xnb->sc_lock);
2235 mtx_lock(&xnb->sc_lock);
2236 if (ifa->ifa_addr->sa_family == AF_INET) {
2237 ifp->if_flags |= IFF_UP;
2238 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
2239 ifp->if_drv_flags &= ~(IFF_DRV_RUNNING |
2241 if_link_state_change(ifp,
2243 ifp->if_drv_flags |= IFF_DRV_RUNNING;
2244 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
2245 if_link_state_change(ifp,
2248 arp_ifinit(ifp, ifa);
2249 mtx_unlock(&xnb->sc_lock);
2251 mtx_unlock(&xnb->sc_lock);
2253 error = ether_ioctl(ifp, cmd, data);
2259 mtx_lock(&xnb->sc_lock);
2260 if (ifr->ifr_reqcap & IFCAP_TXCSUM) {
2261 ifp->if_capenable |= IFCAP_TXCSUM;
2262 ifp->if_hwassist |= XNB_CSUM_FEATURES;
2264 ifp->if_capenable &= ~(IFCAP_TXCSUM);
2265 ifp->if_hwassist &= ~(XNB_CSUM_FEATURES);
2267 if ((ifr->ifr_reqcap & IFCAP_RXCSUM)) {
2268 ifp->if_capenable |= IFCAP_RXCSUM;
2270 ifp->if_capenable &= ~(IFCAP_RXCSUM);
2273 * TODO enable TSO4 and LRO once we no longer need
2274 * to calculate checksums in software
2277 if (ifr->if_reqcap |= IFCAP_TSO4) {
2278 if (IFCAP_TXCSUM & ifp->if_capenable) {
2279 printf("xnb: Xen netif requires that "
2280 "TXCSUM be enabled in order "
2284 ifp->if_capenable |= IFCAP_TSO4;
2285 ifp->if_hwassist |= CSUM_TSO;
2288 ifp->if_capenable &= ~(IFCAP_TSO4);
2289 ifp->if_hwassist &= ~(CSUM_TSO);
2291 if (ifr->ifreqcap |= IFCAP_LRO) {
2292 ifp->if_capenable |= IFCAP_LRO;
2294 ifp->if_capenable &= ~(IFCAP_LRO);
2297 mtx_unlock(&xnb->sc_lock);
2300 ifp->if_mtu = ifr->ifr_mtu;
2301 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
2308 error = ifmedia_ioctl(ifp, ifr, &xnb->sc_media, cmd);
2311 error = ether_ioctl(ifp, cmd, data);
2318 xnb_start_locked(struct ifnet *ifp)
2320 netif_rx_back_ring_t *rxb;
2321 struct xnb_softc *xnb;
2323 RING_IDX req_prod_local;
2325 xnb = ifp->if_softc;
2326 rxb = &xnb->ring_configs[XNB_RING_TYPE_RX].back_ring.rx_ring;
2332 int out_of_space = 0;
2334 req_prod_local = rxb->sring->req_prod;
2339 IF_DEQUEUE(&ifp->if_snd, mbufc);
2342 error = xnb_send(rxb, xnb->otherend_id, mbufc,
2347 * Insufficient space in the ring.
2348 * Requeue pkt and send when space is
2351 IF_PREPEND(&ifp->if_snd, mbufc);
2353 * Perhaps the frontend missed an IRQ
2354 * and went to sleep. Notify it to wake
2361 /* OS gave a corrupt packet. Drop it.*/
2362 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
2365 /* Send succeeded, or packet had error.
2366 * Free the packet */
2367 if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
2372 if (out_of_space != 0)
2376 RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(rxb, notify);
2377 if ((notify != 0) || (out_of_space != 0))
2378 xen_intr_signal(xnb->xen_intr_handle);
2379 rxb->sring->req_event = req_prod_local + 1;
2381 } while (rxb->sring->req_prod != req_prod_local) ;
2385 * Sends one packet to the ring. Blocks until the packet is on the ring
2386 * \param[in] mbufc Contains one packet to send. Caller must free
2387 * \param[in,out] rxb The packet will be pushed onto this ring, but the
2388 * otherend will not be notified.
2389 * \param[in] otherend The domain ID of the other end of the connection
2390 * \retval EAGAIN The ring did not have enough space for the packet.
2391 * The ring has not been modified
2392 * \param[in,out] gnttab Pointer to enough memory for a grant table. We make
2393 * this a function parameter so that we will take less
2395 * \retval EINVAL mbufc was corrupt or not convertible into a pkt
2398 xnb_send(netif_rx_back_ring_t *ring, domid_t otherend, const struct mbuf *mbufc,
2399 gnttab_copy_table gnttab)
2402 int error, n_entries, n_reqs;
2405 space = ring->sring->req_prod - ring->req_cons;
2406 error = xnb_mbufc2pkt(mbufc, &pkt, ring->rsp_prod_pvt, space);
2409 n_entries = xnb_rxpkt2gnttab(&pkt, mbufc, gnttab, ring, otherend);
2410 if (n_entries != 0) {
2411 int __unused hv_ret = HYPERVISOR_grant_table_op(GNTTABOP_copy,
2413 KASSERT(hv_ret == 0, ("HYPERVISOR_grant_table_op returned %d\n",
2417 n_reqs = xnb_rxpkt2rsp(&pkt, gnttab, n_entries, ring);
2423 xnb_start(struct ifnet *ifp)
2425 struct xnb_softc *xnb;
2427 xnb = ifp->if_softc;
2428 mtx_lock(&xnb->rx_lock);
2429 xnb_start_locked(ifp);
2430 mtx_unlock(&xnb->rx_lock);
2433 /* equivalent of network_open() in Linux */
2435 xnb_ifinit_locked(struct xnb_softc *xnb)
2441 mtx_assert(&xnb->sc_lock, MA_OWNED);
2443 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
2448 ifp->if_drv_flags |= IFF_DRV_RUNNING;
2449 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
2450 if_link_state_change(ifp, LINK_STATE_UP);
2455 xnb_ifinit(void *xsc)
2457 struct xnb_softc *xnb = xsc;
2459 mtx_lock(&xnb->sc_lock);
2460 xnb_ifinit_locked(xnb);
2461 mtx_unlock(&xnb->sc_lock);
2465 * Callback used by the generic networking code to tell us when our carrier
2466 * state has changed. Since we don't have a physical carrier, we don't care
2469 xnb_ifmedia_upd(struct ifnet *ifp)
2475 * Callback used by the generic networking code to ask us what our carrier
2476 * state is. Since we don't have a physical carrier, this is very simple
2479 xnb_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
2481 ifmr->ifm_status = IFM_AVALID|IFM_ACTIVE;
2482 ifmr->ifm_active = IFM_ETHER|IFM_MANUAL;
2486 /*---------------------------- NewBus Registration ---------------------------*/
2487 static device_method_t xnb_methods[] = {
2488 /* Device interface */
2489 DEVMETHOD(device_probe, xnb_probe),
2490 DEVMETHOD(device_attach, xnb_attach),
2491 DEVMETHOD(device_detach, xnb_detach),
2492 DEVMETHOD(device_shutdown, bus_generic_shutdown),
2493 DEVMETHOD(device_suspend, xnb_suspend),
2494 DEVMETHOD(device_resume, xnb_resume),
2496 /* Xenbus interface */
2497 DEVMETHOD(xenbus_otherend_changed, xnb_frontend_changed),
2502 static driver_t xnb_driver = {
2505 sizeof(struct xnb_softc),
2507 devclass_t xnb_devclass;
2509 DRIVER_MODULE(xnb, xenbusb_back, xnb_driver, xnb_devclass, 0, 0);
2512 /*-------------------------- Unit Tests -------------------------------------*/
2514 #include "netback_unit_tests.c"