2 * Copyright (c) 2009-2011 Spectra Logic Corporation
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions, and the following disclaimer,
10 * without modification.
11 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
12 * substantially similar to the "NO WARRANTY" disclaimer below
13 * ("Disclaimer") and any redistribution must be conditioned upon
14 * including a substantially similar Disclaimer requirement for further
15 * binary redistribution.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
27 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGES.
30 * Authors: Justin T. Gibbs (Spectra Logic Corporation)
31 * Alan Somers (Spectra Logic Corporation)
32 * John Suykerbuyk (Spectra Logic Corporation)
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
41 * \brief Device driver supporting the vending of network access
42 * from this FreeBSD domain to other domains.
45 #include "opt_inet6.h"
46 #include "opt_global.h"
50 #include <sys/param.h>
51 #include <sys/kernel.h>
54 #include <sys/module.h>
56 #include <sys/socket.h>
57 #include <sys/sockio.h>
58 #include <sys/sysctl.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 #include <machine/xen/xenvar.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 1 /* 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) (vtomach(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 xen_net_read_mac(device_t dev, uint8_t mac[]);
155 static int xnb_ifmedia_upd(struct ifnet *ifp);
156 static void xnb_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr);
157 static void xnb_intr(void *arg);
158 static int xnb_send(netif_rx_back_ring_t *rxb, domid_t otherend,
159 const struct mbuf *mbufc, gnttab_copy_table gnttab);
160 static int xnb_recv(netif_tx_back_ring_t *txb, domid_t otherend,
161 struct mbuf **mbufc, struct ifnet *ifnet,
162 gnttab_copy_table gnttab);
163 static int xnb_ring2pkt(struct xnb_pkt *pkt,
164 const netif_tx_back_ring_t *tx_ring,
166 static void xnb_txpkt2rsp(const struct xnb_pkt *pkt,
167 netif_tx_back_ring_t *ring, int error);
168 static struct mbuf *xnb_pkt2mbufc(const struct xnb_pkt *pkt, struct ifnet *ifp);
169 static int xnb_txpkt2gnttab(const struct xnb_pkt *pkt,
170 const struct mbuf *mbufc,
171 gnttab_copy_table gnttab,
172 const netif_tx_back_ring_t *txb,
173 domid_t otherend_id);
174 static void xnb_update_mbufc(struct mbuf *mbufc,
175 const gnttab_copy_table gnttab, int n_entries);
176 static int xnb_mbufc2pkt(const struct mbuf *mbufc,
178 RING_IDX start, int space);
179 static int xnb_rxpkt2gnttab(const struct xnb_pkt *pkt,
180 const struct mbuf *mbufc,
181 gnttab_copy_table gnttab,
182 const netif_rx_back_ring_t *rxb,
183 domid_t otherend_id);
184 static int xnb_rxpkt2rsp(const struct xnb_pkt *pkt,
185 const gnttab_copy_table gnttab, int n_entries,
186 netif_rx_back_ring_t *ring);
187 static void xnb_stop(struct xnb_softc*);
188 static int xnb_ioctl(struct ifnet*, u_long, caddr_t);
189 static void xnb_start_locked(struct ifnet*);
190 static void xnb_start(struct ifnet*);
191 static void xnb_ifinit_locked(struct xnb_softc*);
192 static void xnb_ifinit(void*);
194 static int xnb_unit_test_main(SYSCTL_HANDLER_ARGS);
195 static int xnb_dump_rings(SYSCTL_HANDLER_ARGS);
197 #if defined(INET) || defined(INET6)
198 static void xnb_add_mbuf_cksum(struct mbuf *mbufc);
200 /*------------------------------ Data Structures -----------------------------*/
204 * Representation of a xennet packet. Simplified version of a packet as
205 * stored in the Xen tx ring. Applicable to both RX and TX packets
209 * Array index of the first data-bearing (eg, not extra info) entry
215 * Array index of the second data-bearing entry for this packet.
216 * Invalid if the packet has only one data-bearing entry. If the
217 * packet has more than two data-bearing entries, then the second
218 * through the last will be sequential modulo the ring size
223 * Optional extra info. Only valid if flags contains
224 * NETTXF_extra_info. Note that extra.type will always be
225 * XEN_NETIF_EXTRA_TYPE_GSO. Currently, no known netfront or netback
226 * driver will ever set XEN_NETIF_EXTRA_TYPE_MCAST_*
228 netif_extra_info_t extra;
230 /** Size of entire packet in bytes. */
233 /** The size of the first entry's data in bytes */
237 * Either NETTXF_ or NETRXF_ flags. Note that the flag values are
238 * not the same for TX and RX packets
243 * The number of valid data-bearing entries (either netif_tx_request's
244 * or netif_rx_response's) in the packet. If this is 0, it means the
245 * entire packet is invalid.
249 /** There was an error processing the packet */
253 /** xnb_pkt method: initialize it */
255 xnb_pkt_initialize(struct xnb_pkt *pxnb)
257 bzero(pxnb, sizeof(*pxnb));
260 /** xnb_pkt method: mark the packet as valid */
262 xnb_pkt_validate(struct xnb_pkt *pxnb)
267 /** xnb_pkt method: mark the packet as invalid */
269 xnb_pkt_invalidate(struct xnb_pkt *pxnb)
274 /** xnb_pkt method: Check whether the packet is valid */
276 xnb_pkt_is_valid(const struct xnb_pkt *pxnb)
278 return (! pxnb->error);
282 /** xnb_pkt method: print the packet's contents in human-readable format*/
284 xnb_dump_pkt(const struct xnb_pkt *pkt) {
286 DPRINTF("Was passed a null pointer.\n");
289 DPRINTF("pkt address= %p\n", pkt);
290 DPRINTF("pkt->size=%d\n", pkt->size);
291 DPRINTF("pkt->car_size=%d\n", pkt->car_size);
292 DPRINTF("pkt->flags=0x%04x\n", pkt->flags);
293 DPRINTF("pkt->list_len=%d\n", pkt->list_len);
294 /* DPRINTF("pkt->extra"); TODO */
295 DPRINTF("pkt->car=%d\n", pkt->car);
296 DPRINTF("pkt->cdr=%d\n", pkt->cdr);
297 DPRINTF("pkt->error=%d\n", pkt->error);
299 #endif /* XNB_DEBUG */
302 xnb_dump_txreq(RING_IDX idx, const struct netif_tx_request *txreq)
305 DPRINTF("netif_tx_request index =%u\n", idx);
306 DPRINTF("netif_tx_request.gref =%u\n", txreq->gref);
307 DPRINTF("netif_tx_request.offset=%hu\n", txreq->offset);
308 DPRINTF("netif_tx_request.flags =%hu\n", txreq->flags);
309 DPRINTF("netif_tx_request.id =%hu\n", txreq->id);
310 DPRINTF("netif_tx_request.size =%hu\n", txreq->size);
316 * \brief Configuration data for a shared memory request ring
317 * used to communicate with the front-end client of this
320 struct xnb_ring_config {
322 * Runtime structures for ring access. Unfortunately, TX and RX rings
323 * use different data structures, and that cannot be changed since it
324 * is part of the interdomain protocol.
327 netif_rx_back_ring_t rx_ring;
328 netif_tx_back_ring_t tx_ring;
332 * The device bus address returned by the hypervisor when
333 * mapping the ring and required to unmap it when a connection
338 /** The pseudo-physical address where ring memory is mapped.*/
341 /** KVA address where ring memory is mapped. */
345 * Grant table handles, one per-ring page, returned by the
346 * hyperpervisor upon mapping of the ring and required to
347 * unmap it when a connection is torn down.
349 grant_handle_t handle;
351 /** The number of ring pages mapped for the current connection. */
355 * The grant references, one per-ring page, supplied by the
356 * front-end, allowing us to reference the ring pages in the
357 * front-end's domain and to map these pages into our own domain.
359 grant_ref_t ring_ref;
363 * Per-instance connection state flags.
367 /** Communication with the front-end has been established. */
368 XNBF_RING_CONNECTED = 0x01,
371 * Front-end requests exist in the ring and are waiting for
372 * xnb_xen_req objects to free up.
374 XNBF_RESOURCE_SHORTAGE = 0x02,
376 /** Connection teardown has started. */
377 XNBF_SHUTDOWN = 0x04,
379 /** A thread is already performing shutdown processing. */
380 XNBF_IN_SHUTDOWN = 0x08
384 * Types of rings. Used for array indices and to identify a ring's control
385 * data structure type
388 XNB_RING_TYPE_TX = 0, /* ID of TX rings, used for array indices */
389 XNB_RING_TYPE_RX = 1, /* ID of RX rings, used for array indices */
394 * Per-instance configuration data.
397 /** NewBus device corresponding to this instance. */
400 /* Media related fields */
402 /** Generic network media state */
403 struct ifmedia sc_media;
405 /** Media carrier info */
406 struct ifnet *xnb_ifp;
408 /** Our own private carrier state */
411 /** Device MAC Address */
412 uint8_t mac[ETHER_ADDR_LEN];
414 /* Xen related fields */
417 * \brief The netif protocol abi in effect.
419 * There are situations where the back and front ends can
420 * have a different, native abi (e.g. intel x86_64 and
421 * 32bit x86 domains on the same machine). The back-end
422 * always accomodates the front-end's native abi. That
423 * value is pulled from the XenStore and recorded here.
428 * Name of the bridge to which this VIF is connected, if any
429 * This field is dynamically allocated by xenbus and must be free()ed
430 * when no longer needed
434 /** The interrupt driven even channel used to signal ring events. */
435 evtchn_port_t evtchn;
437 /** Xen device handle.*/
440 /** Handle to the communication ring event channel. */
441 xen_intr_handle_t xen_intr_handle;
444 * \brief Cached value of the front-end's domain id.
446 * This value is used at once for each mapped page in
447 * a transaction. We cache it to avoid incuring the
448 * cost of an ivar access every time this is needed.
453 * Undocumented frontend feature. Has something to do with
457 /** Undocumented frontend feature */
459 /** Undocumented frontend feature */
461 /** Can checksum TCP/UDP over IPv4 */
464 /* Implementation related fields */
466 * Preallocated grant table copy descriptor for RX operations.
467 * Access must be protected by rx_lock
469 gnttab_copy_table rx_gnttab;
472 * Preallocated grant table copy descriptor for TX operations.
473 * Access must be protected by tx_lock
475 gnttab_copy_table tx_gnttab;
479 * Resource representing allocated physical address space
480 * associated with our per-instance kva region.
482 struct resource *pseudo_phys_res;
484 /** Resource id for allocated physical address space. */
485 int pseudo_phys_res_id;
488 /** Ring mapping and interrupt configuration data. */
489 struct xnb_ring_config ring_configs[XNB_NUM_RING_TYPES];
492 * Global pool of kva used for mapping remote domain ring
493 * and I/O transaction data.
497 /** Psuedo-physical address corresponding to kva. */
498 uint64_t gnt_base_addr;
500 /** Various configuration and state bit flags. */
503 /** Mutex protecting per-instance data in the receive path. */
506 /** Mutex protecting per-instance data in the softc structure. */
509 /** Mutex protecting per-instance data in the transmit path. */
512 /** The size of the global kva pool. */
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%lu\n", entry->dest.u.gmfn);
530 printf("gnttab dest offset=\t%hu\n", entry->dest.offset);
531 printf("gnttab dest domid=\t%hu\n", entry->dest.domid);
532 if (entry->flags & GNTCOPY_source_gref)
533 printf("gnttab source ref=\t%u\n", entry->source.u.ref);
535 printf("gnttab source gmfn=\t%lu\n", 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)
628 kva_free(xnb->kva, xnb->kva_size);
630 if (xnb->pseudo_phys_res != NULL) {
631 bus_release_resource(xnb->dev, SYS_RES_MEMORY,
632 xnb->pseudo_phys_res_id,
633 xnb->pseudo_phys_res);
634 xnb->pseudo_phys_res = NULL;
639 xnb->gnt_base_addr = 0;
643 * Cleanup all inter-domain communication mechanisms.
645 * \param xnb Per-instance xnb configuration structure.
648 xnb_disconnect(struct xnb_softc *xnb)
650 struct gnttab_unmap_grant_ref gnts[XNB_NUM_RING_TYPES];
654 xen_intr_unbind(xnb->xen_intr_handle);
657 * We may still have another thread currently processing requests. We
658 * must acquire the rx and tx locks to make sure those threads are done,
659 * but we can release those locks as soon as we acquire them, because no
660 * more interrupts will be arriving.
662 mtx_lock(&xnb->tx_lock);
663 mtx_unlock(&xnb->tx_lock);
664 mtx_lock(&xnb->rx_lock);
665 mtx_unlock(&xnb->rx_lock);
667 /* Free malloc'd softc member variables */
668 if (xnb->bridge != NULL)
669 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;
815 xnb->kva = kva_alloc(xnb->kva_size);
818 xnb->gnt_base_addr = xnb->kva;
819 #else /* defined XENHVM */
821 * Reserve a range of pseudo physical memory that we can map
822 * into kva. These pages will only be backed by machine
823 * pages ("real memory") during the lifetime of front-end requests
824 * via grant table operations. We will map the netif tx and rx rings
827 xnb->pseudo_phys_res_id = 0;
828 xnb->pseudo_phys_res = bus_alloc_resource(xnb->dev, SYS_RES_MEMORY,
829 &xnb->pseudo_phys_res_id,
830 0, ~0, xnb->kva_size,
832 if (xnb->pseudo_phys_res == NULL) {
836 xnb->kva = (vm_offset_t)rman_get_virtual(xnb->pseudo_phys_res);
837 xnb->gnt_base_addr = rman_get_start(xnb->pseudo_phys_res);
838 #endif /* !defined XENHVM */
843 * Collect information from the XenStore related to our device and its frontend
845 * \param xnb Per-instance xnb configuration structure.
848 xnb_collect_xenstore_info(struct xnb_softc *xnb)
851 * \todo Linux collects the following info. We should collect most
853 * "feature-rx-notify"
855 const char *otherend_path;
856 const char *our_path;
858 unsigned int rx_copy, bridge_len;
859 uint8_t no_csum_offload;
861 otherend_path = xenbus_get_otherend_path(xnb->dev);
862 our_path = xenbus_get_node(xnb->dev);
864 /* Collect the critical communication parameters */
865 err = xs_gather(XST_NIL, otherend_path,
866 "tx-ring-ref", "%l" PRIu32,
867 &xnb->ring_configs[XNB_RING_TYPE_TX].ring_ref,
868 "rx-ring-ref", "%l" PRIu32,
869 &xnb->ring_configs[XNB_RING_TYPE_RX].ring_ref,
870 "event-channel", "%" PRIu32, &xnb->evtchn,
873 xenbus_dev_fatal(xnb->dev, err,
874 "Unable to retrieve ring information from "
875 "frontend %s. Unable to connect.",
880 /* Collect the handle from xenstore */
881 err = xs_scanf(XST_NIL, our_path, "handle", NULL, "%li", &xnb->handle);
883 xenbus_dev_fatal(xnb->dev, err,
884 "Error reading handle from frontend %s. "
885 "Unable to connect.", otherend_path);
889 * Collect the bridgename, if any. We do not need bridge_len; we just
892 err = xs_read(XST_NIL, our_path, "bridge", &bridge_len,
893 (void**)&xnb->bridge);
898 * Does the frontend request that we use rx copy? If not, return an
899 * error because this driver only supports rx copy.
901 err = xs_scanf(XST_NIL, otherend_path, "request-rx-copy", NULL,
902 "%" PRIu32, &rx_copy);
908 xenbus_dev_fatal(xnb->dev, err, "reading %s/request-rx-copy",
913 * \todo: figure out the exact meaning of this feature, and when
914 * the frontend will set it to true. It should be set to true
918 /* return EOPNOTSUPP;*/
920 /** \todo Collect the rx notify feature */
922 /* Collect the feature-sg. */
923 if (xs_scanf(XST_NIL, otherend_path, "feature-sg", NULL,
924 "%hhu", &xnb->can_sg) < 0)
927 /* Collect remaining frontend features */
928 if (xs_scanf(XST_NIL, otherend_path, "feature-gso-tcpv4", NULL,
929 "%hhu", &xnb->gso) < 0)
932 if (xs_scanf(XST_NIL, otherend_path, "feature-gso-tcpv4-prefix", NULL,
933 "%hhu", &xnb->gso_prefix) < 0)
936 if (xs_scanf(XST_NIL, otherend_path, "feature-no-csum-offload", NULL,
937 "%hhu", &no_csum_offload) < 0)
939 xnb->ip_csum = (no_csum_offload == 0);
945 * Supply information about the physical device to the frontend
948 * \param xnb Per-instance xnb configuration structure.
951 xnb_publish_backend_info(struct xnb_softc *xnb)
953 struct xs_transaction xst;
954 const char *our_path;
957 our_path = xenbus_get_node(xnb->dev);
960 error = xs_transaction_start(&xst);
962 xenbus_dev_fatal(xnb->dev, error,
963 "Error publishing backend info "
964 "(start transaction)");
968 error = xs_printf(xst, our_path, "feature-sg",
973 error = xs_printf(xst, our_path, "feature-gso-tcpv4",
974 "%d", XNB_GSO_TCPV4);
978 error = xs_printf(xst, our_path, "feature-rx-copy",
983 error = xs_printf(xst, our_path, "feature-rx-flip",
988 error = xs_transaction_end(xst, 0);
989 if (error != 0 && error != EAGAIN) {
990 xenbus_dev_fatal(xnb->dev, error, "ending transaction");
994 } while (error == EAGAIN);
1000 * Connect to our netfront peer now that it has completed publishing
1001 * its configuration into the XenStore.
1003 * \param xnb Per-instance xnb configuration structure.
1006 xnb_connect(struct xnb_softc *xnb)
1010 if (xenbus_get_state(xnb->dev) == XenbusStateConnected)
1013 if (xnb_collect_xenstore_info(xnb) != 0)
1016 xnb->flags &= ~XNBF_SHUTDOWN;
1018 /* Read front end configuration. */
1020 /* Allocate resources whose size depends on front-end configuration. */
1021 error = xnb_alloc_communication_mem(xnb);
1023 xenbus_dev_fatal(xnb->dev, error,
1024 "Unable to allocate communication memory");
1029 * Connect communication channel.
1031 error = xnb_connect_comms(xnb);
1033 /* Specific errors are reported by xnb_connect_comms(). */
1038 /* Ready for I/O. */
1039 xenbus_set_state(xnb->dev, XenbusStateConnected);
1042 /*-------------------------- Device Teardown Support -------------------------*/
1044 * Perform device shutdown functions.
1046 * \param xnb Per-instance xnb configuration structure.
1048 * Mark this instance as shutting down, wait for any active requests
1049 * to drain, disconnect from the front-end, and notify any waiters (e.g.
1050 * a thread invoking our detach method) that detach can now proceed.
1053 xnb_shutdown(struct xnb_softc *xnb)
1056 * Due to the need to drop our mutex during some
1057 * xenbus operations, it is possible for two threads
1058 * to attempt to close out shutdown processing at
1059 * the same time. Tell the caller that hits this
1060 * race to try back later.
1062 if ((xnb->flags & XNBF_IN_SHUTDOWN) != 0)
1065 xnb->flags |= XNBF_SHUTDOWN;
1067 xnb->flags |= XNBF_IN_SHUTDOWN;
1069 mtx_unlock(&xnb->sc_lock);
1070 /* Free the network interface */
1072 if (xnb->xnb_ifp != NULL) {
1073 ether_ifdetach(xnb->xnb_ifp);
1074 if_free(xnb->xnb_ifp);
1075 xnb->xnb_ifp = NULL;
1077 mtx_lock(&xnb->sc_lock);
1079 xnb_disconnect(xnb);
1081 mtx_unlock(&xnb->sc_lock);
1082 if (xenbus_get_state(xnb->dev) < XenbusStateClosing)
1083 xenbus_set_state(xnb->dev, XenbusStateClosing);
1084 mtx_lock(&xnb->sc_lock);
1086 xnb->flags &= ~XNBF_IN_SHUTDOWN;
1089 /* Indicate to xnb_detach() that is it safe to proceed. */
1096 * Report an attach time error to the console and Xen, and cleanup
1097 * this instance by forcing immediate detach processing.
1099 * \param xnb Per-instance xnb configuration structure.
1100 * \param err Errno describing the error.
1101 * \param fmt Printf style format and arguments
1104 xnb_attach_failed(struct xnb_softc *xnb, int err, const char *fmt, ...)
1110 va_copy(ap_hotplug, ap);
1111 xs_vprintf(XST_NIL, xenbus_get_node(xnb->dev),
1112 "hotplug-error", fmt, ap_hotplug);
1114 xs_printf(XST_NIL, xenbus_get_node(xnb->dev),
1115 "hotplug-status", "error");
1117 xenbus_dev_vfatal(xnb->dev, err, fmt, ap);
1120 xs_printf(XST_NIL, xenbus_get_node(xnb->dev),
1122 xnb_detach(xnb->dev);
1125 /*---------------------------- NewBus Entrypoints ----------------------------*/
1127 * Inspect a XenBus device and claim it if is of the appropriate type.
1129 * \param dev NewBus device object representing a candidate XenBus device.
1131 * \return 0 for success, errno codes for failure.
1134 xnb_probe(device_t dev)
1136 if (!strcmp(xenbus_get_type(dev), "vif")) {
1137 DPRINTF("Claiming device %d, %s\n", device_get_unit(dev),
1138 devclass_get_name(device_get_devclass(dev)));
1139 device_set_desc(dev, "Backend Virtual Network Device");
1147 * Setup sysctl variables to control various Network Back parameters.
1149 * \param xnb Xen Net Back softc.
1153 xnb_setup_sysctl(struct xnb_softc *xnb)
1155 struct sysctl_ctx_list *sysctl_ctx = NULL;
1156 struct sysctl_oid *sysctl_tree = NULL;
1158 sysctl_ctx = device_get_sysctl_ctx(xnb->dev);
1159 if (sysctl_ctx == NULL)
1162 sysctl_tree = device_get_sysctl_tree(xnb->dev);
1163 if (sysctl_tree == NULL)
1167 SYSCTL_ADD_PROC(sysctl_ctx,
1168 SYSCTL_CHILDREN(sysctl_tree),
1170 "unit_test_results",
1171 CTLTYPE_STRING | CTLFLAG_RD,
1176 "Results of builtin unit tests");
1178 SYSCTL_ADD_PROC(sysctl_ctx,
1179 SYSCTL_CHILDREN(sysctl_tree),
1182 CTLTYPE_STRING | CTLFLAG_RD,
1187 "Xennet Back Rings");
1188 #endif /* XNB_DEBUG */
1192 * Create a network device.
1193 * @param handle device handle
1196 create_netdev(device_t dev)
1199 struct xnb_softc *xnb;
1202 xnb = device_get_softc(dev);
1203 mtx_init(&xnb->sc_lock, "xnb_softc", "xen netback softc lock", MTX_DEF);
1204 mtx_init(&xnb->tx_lock, "xnb_tx", "xen netback tx lock", MTX_DEF);
1205 mtx_init(&xnb->rx_lock, "xnb_rx", "xen netback rx lock", MTX_DEF);
1209 ifmedia_init(&xnb->sc_media, 0, xnb_ifmedia_upd, xnb_ifmedia_sts);
1210 ifmedia_add(&xnb->sc_media, IFM_ETHER|IFM_MANUAL, 0, NULL);
1211 ifmedia_set(&xnb->sc_media, IFM_ETHER|IFM_MANUAL);
1213 err = xen_net_read_mac(dev, xnb->mac);
1215 /* Set up ifnet structure */
1216 ifp = xnb->xnb_ifp = if_alloc(IFT_ETHER);
1217 ifp->if_softc = xnb;
1218 if_initname(ifp, "xnb", device_get_unit(dev));
1219 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
1220 ifp->if_ioctl = xnb_ioctl;
1221 ifp->if_output = ether_output;
1222 ifp->if_start = xnb_start;
1224 ifp->if_watchdog = xnb_watchdog;
1226 ifp->if_init = xnb_ifinit;
1227 ifp->if_mtu = ETHERMTU;
1228 ifp->if_snd.ifq_maxlen = NET_RX_RING_SIZE - 1;
1230 ifp->if_hwassist = XNB_CSUM_FEATURES;
1231 ifp->if_capabilities = IFCAP_HWCSUM;
1232 ifp->if_capenable = IFCAP_HWCSUM;
1234 ether_ifattach(ifp, xnb->mac);
1242 * Attach to a XenBus device that has been claimed by our probe routine.
1244 * \param dev NewBus device object representing this Xen Net Back instance.
1246 * \return 0 for success, errno codes for failure.
1249 xnb_attach(device_t dev)
1251 struct xnb_softc *xnb;
1255 error = create_netdev(dev);
1257 xenbus_dev_fatal(dev, error, "creating netdev");
1261 DPRINTF("Attaching to %s\n", xenbus_get_node(dev));
1264 * Basic initialization.
1265 * After this block it is safe to call xnb_detach()
1266 * to clean up any allocated data for this instance.
1268 xnb = device_get_softc(dev);
1269 xnb->otherend_id = xenbus_get_otherend_id(dev);
1270 for (i=0; i < XNB_NUM_RING_TYPES; i++) {
1271 xnb->ring_configs[i].ring_pages = 1;
1275 * Setup sysctl variables.
1277 xnb_setup_sysctl(xnb);
1279 /* Update hot-plug status to satisfy xend. */
1280 error = xs_printf(XST_NIL, xenbus_get_node(xnb->dev),
1281 "hotplug-status", "connected");
1283 xnb_attach_failed(xnb, error, "writing %s/hotplug-status",
1284 xenbus_get_node(xnb->dev));
1288 if ((error = xnb_publish_backend_info(xnb)) != 0) {
1290 * If we can't publish our data, we cannot participate
1291 * in this connection, and waiting for a front-end state
1292 * change will not help the situation.
1294 xnb_attach_failed(xnb, error,
1295 "Publishing backend status for %s",
1296 xenbus_get_node(xnb->dev));
1300 /* Tell the front end that we are ready to connect. */
1301 xenbus_set_state(dev, XenbusStateInitWait);
1307 * Detach from a net back device instance.
1309 * \param dev NewBus device object representing this Xen Net Back instance.
1311 * \return 0 for success, errno codes for failure.
1313 * \note A net back device may be detached at any time in its life-cycle,
1314 * including part way through the attach process. For this reason,
1315 * initialization order and the intialization state checks in this
1316 * routine must be carefully coupled so that attach time failures
1317 * are gracefully handled.
1320 xnb_detach(device_t dev)
1322 struct xnb_softc *xnb;
1326 xnb = device_get_softc(dev);
1327 mtx_lock(&xnb->sc_lock);
1328 while (xnb_shutdown(xnb) == EAGAIN) {
1329 msleep(xnb, &xnb->sc_lock, /*wakeup prio unchanged*/0,
1332 mtx_unlock(&xnb->sc_lock);
1335 mtx_destroy(&xnb->tx_lock);
1336 mtx_destroy(&xnb->rx_lock);
1337 mtx_destroy(&xnb->sc_lock);
1342 * Prepare this net back device for suspension of this VM.
1344 * \param dev NewBus device object representing this Xen net Back instance.
1346 * \return 0 for success, errno codes for failure.
1349 xnb_suspend(device_t dev)
1355 * Perform any processing required to recover from a suspended state.
1357 * \param dev NewBus device object representing this Xen Net Back instance.
1359 * \return 0 for success, errno codes for failure.
1362 xnb_resume(device_t dev)
1368 * Handle state changes expressed via the XenStore by our front-end peer.
1370 * \param dev NewBus device object representing this Xen
1371 * Net Back instance.
1372 * \param frontend_state The new state of the front-end.
1374 * \return 0 for success, errno codes for failure.
1377 xnb_frontend_changed(device_t dev, XenbusState frontend_state)
1379 struct xnb_softc *xnb;
1381 xnb = device_get_softc(dev);
1383 DPRINTF("frontend_state=%s, xnb_state=%s\n",
1384 xenbus_strstate(frontend_state),
1385 xenbus_strstate(xenbus_get_state(xnb->dev)));
1387 switch (frontend_state) {
1388 case XenbusStateInitialising:
1390 case XenbusStateInitialised:
1391 case XenbusStateConnected:
1394 case XenbusStateClosing:
1395 case XenbusStateClosed:
1396 mtx_lock(&xnb->sc_lock);
1398 mtx_unlock(&xnb->sc_lock);
1399 if (frontend_state == XenbusStateClosed)
1400 xenbus_set_state(xnb->dev, XenbusStateClosed);
1403 xenbus_dev_fatal(xnb->dev, EINVAL, "saw state %d at frontend",
1410 /*---------------------------- Request Processing ----------------------------*/
1412 * Interrupt handler bound to the shared ring's event channel.
1413 * Entry point for the xennet transmit path in netback
1414 * Transfers packets from the Xen ring to the host's generic networking stack
1416 * \param arg Callback argument registerd during event channel
1417 * binding - the xnb_softc for this instance.
1422 struct xnb_softc *xnb;
1424 netif_tx_back_ring_t *txb;
1425 RING_IDX req_prod_local;
1427 xnb = (struct xnb_softc *)arg;
1429 txb = &xnb->ring_configs[XNB_RING_TYPE_TX].back_ring.tx_ring;
1431 mtx_lock(&xnb->tx_lock);
1434 req_prod_local = txb->sring->req_prod;
1441 err = xnb_recv(txb, xnb->otherend_id, &mbufc, ifp,
1443 if (err || (mbufc == NULL))
1446 /* Send the packet to the generic network stack */
1447 (*xnb->xnb_ifp->if_input)(xnb->xnb_ifp, mbufc);
1450 RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(txb, notify);
1452 xen_intr_signal(xnb->xen_intr_handle);
1454 txb->sring->req_event = txb->req_cons + 1;
1456 } while (txb->sring->req_prod != req_prod_local) ;
1457 mtx_unlock(&xnb->tx_lock);
1464 * Build a struct xnb_pkt based on netif_tx_request's from a netif tx ring.
1465 * Will read exactly 0 or 1 packets from the ring; never a partial packet.
1466 * \param[out] pkt The returned packet. If there is an error building
1467 * the packet, pkt.list_len will be set to 0.
1468 * \param[in] tx_ring Pointer to the Ring that is the input to this function
1469 * \param[in] start The ring index of the first potential request
1470 * \return The number of requests consumed to build this packet
1473 xnb_ring2pkt(struct xnb_pkt *pkt, const netif_tx_back_ring_t *tx_ring,
1479 * 2) Read the first request of the packet
1480 * 3) Read the extras
1482 * 5) Loop on the remainder of the packet
1483 * 6) Finalize pkt (stuff like car_size and list_len)
1486 int discard = 0; /* whether to discard the packet */
1487 int more_data = 0; /* there are more request past the last one */
1488 uint16_t cdr_size = 0; /* accumulated size of requests 2 through n */
1490 xnb_pkt_initialize(pkt);
1492 /* Read the first request */
1493 if (RING_HAS_UNCONSUMED_REQUESTS_2(tx_ring, idx)) {
1494 netif_tx_request_t *tx = RING_GET_REQUEST(tx_ring, idx);
1495 pkt->size = tx->size;
1496 pkt->flags = tx->flags & ~NETTXF_more_data;
1497 more_data = tx->flags & NETTXF_more_data;
1503 /* Read the extra info */
1504 if ((pkt->flags & NETTXF_extra_info) &&
1505 RING_HAS_UNCONSUMED_REQUESTS_2(tx_ring, idx)) {
1506 netif_extra_info_t *ext =
1507 (netif_extra_info_t*) RING_GET_REQUEST(tx_ring, idx);
1508 pkt->extra.type = ext->type;
1509 switch (pkt->extra.type) {
1510 case XEN_NETIF_EXTRA_TYPE_GSO:
1511 pkt->extra.u.gso = ext->u.gso;
1515 * The reference Linux netfront driver will
1516 * never set any other extra.type. So we don't
1517 * know what to do with it. Let's print an
1518 * error, then consume and discard the packet
1520 printf("xnb(%s:%d): Unknown extra info type %d."
1521 " Discarding packet\n",
1522 __func__, __LINE__, pkt->extra.type);
1523 xnb_dump_txreq(start, RING_GET_REQUEST(tx_ring,
1525 xnb_dump_txreq(idx, RING_GET_REQUEST(tx_ring,
1531 pkt->extra.flags = ext->flags;
1532 if (ext->flags & XEN_NETIF_EXTRA_FLAG_MORE) {
1534 * The reference linux netfront driver never sets this
1535 * flag (nor does any other known netfront). So we
1536 * will discard the packet.
1538 printf("xnb(%s:%d): Request sets "
1539 "XEN_NETIF_EXTRA_FLAG_MORE, but we can't handle "
1540 "that\n", __func__, __LINE__);
1541 xnb_dump_txreq(start, RING_GET_REQUEST(tx_ring, start));
1542 xnb_dump_txreq(idx, RING_GET_REQUEST(tx_ring, idx));
1549 /* Set cdr. If there is not more data, cdr is invalid */
1552 /* Loop on remainder of packet */
1553 while (more_data && RING_HAS_UNCONSUMED_REQUESTS_2(tx_ring, idx)) {
1554 netif_tx_request_t *tx = RING_GET_REQUEST(tx_ring, idx);
1556 cdr_size += tx->size;
1557 if (tx->flags & ~NETTXF_more_data) {
1558 /* There should be no other flags set at this point */
1559 printf("xnb(%s:%d): Request sets unknown flags %d "
1560 "after the 1st request in the packet.\n",
1561 __func__, __LINE__, tx->flags);
1562 xnb_dump_txreq(start, RING_GET_REQUEST(tx_ring, start));
1563 xnb_dump_txreq(idx, RING_GET_REQUEST(tx_ring, idx));
1566 more_data = tx->flags & NETTXF_more_data;
1570 /* Finalize packet */
1571 if (more_data != 0) {
1572 /* The ring ran out of requests before finishing the packet */
1573 xnb_pkt_invalidate(pkt);
1574 idx = start; /* tell caller that we consumed no requests */
1576 /* Calculate car_size */
1577 pkt->car_size = pkt->size - cdr_size;
1580 xnb_pkt_invalidate(pkt);
1588 * Respond to all the requests that constituted pkt. Builds the responses and
1589 * writes them to the ring, but doesn't push them to the shared ring.
1590 * \param[in] pkt the packet that needs a response
1591 * \param[in] error true if there was an error handling the packet, such
1592 * as in the hypervisor copy op or mbuf allocation
1593 * \param[out] ring Responses go here
1596 xnb_txpkt2rsp(const struct xnb_pkt *pkt, netif_tx_back_ring_t *ring,
1601 * 1) Respond to the first request
1602 * 2) Respond to the extra info reques
1603 * Loop through every remaining request in the packet, generating
1604 * responses that copy those requests' ids and sets the status
1607 netif_tx_request_t *tx;
1608 netif_tx_response_t *rsp;
1612 status = (xnb_pkt_is_valid(pkt) == 0) || error ?
1613 NETIF_RSP_ERROR : NETIF_RSP_OKAY;
1614 KASSERT((pkt->list_len == 0) || (ring->rsp_prod_pvt == pkt->car),
1615 ("Cannot respond to ring requests out of order"));
1617 if (pkt->list_len >= 1) {
1619 tx = RING_GET_REQUEST(ring, ring->rsp_prod_pvt);
1621 rsp = RING_GET_RESPONSE(ring, ring->rsp_prod_pvt);
1623 rsp->status = status;
1624 ring->rsp_prod_pvt++;
1626 if (pkt->flags & NETRXF_extra_info) {
1627 rsp = RING_GET_RESPONSE(ring, ring->rsp_prod_pvt);
1628 rsp->status = NETIF_RSP_NULL;
1629 ring->rsp_prod_pvt++;
1633 for (i=0; i < pkt->list_len - 1; i++) {
1635 tx = RING_GET_REQUEST(ring, ring->rsp_prod_pvt);
1637 rsp = RING_GET_RESPONSE(ring, ring->rsp_prod_pvt);
1639 rsp->status = status;
1640 ring->rsp_prod_pvt++;
1645 * Create an mbuf chain to represent a packet. Initializes all of the headers
1646 * in the mbuf chain, but does not copy the data. The returned chain must be
1647 * free()'d when no longer needed
1648 * \param[in] pkt A packet to model the mbuf chain after
1649 * \return A newly allocated mbuf chain, possibly with clusters attached.
1653 xnb_pkt2mbufc(const struct xnb_pkt *pkt, struct ifnet *ifp)
1656 * \todo consider using a memory pool for mbufs instead of
1657 * reallocating them for every packet
1659 /** \todo handle extra data */
1662 m = m_getm(NULL, pkt->size, M_NOWAIT, MT_DATA);
1665 m->m_pkthdr.rcvif = ifp;
1666 if (pkt->flags & NETTXF_data_validated) {
1668 * We lie to the host OS and always tell it that the
1669 * checksums are ok, because the packet is unlikely to
1670 * get corrupted going across domains.
1672 m->m_pkthdr.csum_flags = (
1678 m->m_pkthdr.csum_data = 0xffff;
1685 * Build a gnttab_copy table that can be used to copy data from a pkt
1686 * to an mbufc. Does not actually perform the copy. Always uses gref's on
1688 * \param[in] pkt pkt's associated requests form the src for
1689 * the copy operation
1690 * \param[in] mbufc mbufc's storage forms the dest for the copy operation
1691 * \param[out] gnttab Storage for the returned grant table
1692 * \param[in] txb Pointer to the backend ring structure
1693 * \param[in] otherend_id The domain ID of the other end of the copy
1694 * \return The number of gnttab entries filled
1697 xnb_txpkt2gnttab(const struct xnb_pkt *pkt, const struct mbuf *mbufc,
1698 gnttab_copy_table gnttab, const netif_tx_back_ring_t *txb,
1699 domid_t otherend_id)
1702 const struct mbuf *mbuf = mbufc;/* current mbuf within the chain */
1703 int gnt_idx = 0; /* index into grant table */
1704 RING_IDX r_idx = pkt->car; /* index into tx ring buffer */
1705 int r_ofs = 0; /* offset of next data within tx request's data area */
1706 int m_ofs = 0; /* offset of next data within mbuf's data area */
1707 /* size in bytes that still needs to be represented in the table */
1708 uint16_t size_remaining = pkt->size;
1710 while (size_remaining > 0) {
1711 const netif_tx_request_t *txq = RING_GET_REQUEST(txb, r_idx);
1712 const size_t mbuf_space = M_TRAILINGSPACE(mbuf) - m_ofs;
1713 const size_t req_size =
1714 r_idx == pkt->car ? pkt->car_size : txq->size;
1715 const size_t pkt_space = req_size - r_ofs;
1717 * space is the largest amount of data that can be copied in the
1718 * grant table's next entry
1720 const size_t space = MIN(pkt_space, mbuf_space);
1722 /* TODO: handle this error condition without panicking */
1723 KASSERT(gnt_idx < GNTTAB_LEN, ("Grant table is too short"));
1725 gnttab[gnt_idx].source.u.ref = txq->gref;
1726 gnttab[gnt_idx].source.domid = otherend_id;
1727 gnttab[gnt_idx].source.offset = txq->offset + r_ofs;
1728 gnttab[gnt_idx].dest.u.gmfn = virt_to_mfn(
1729 mtod(mbuf, vm_offset_t) + m_ofs);
1730 gnttab[gnt_idx].dest.offset = virt_to_offset(
1731 mtod(mbuf, vm_offset_t) + m_ofs);
1732 gnttab[gnt_idx].dest.domid = DOMID_SELF;
1733 gnttab[gnt_idx].len = space;
1734 gnttab[gnt_idx].flags = GNTCOPY_source_gref;
1739 size_remaining -= space;
1740 if (req_size - r_ofs <= 0) {
1741 /* Must move to the next tx request */
1743 r_idx = (r_idx == pkt->car) ? pkt->cdr : r_idx + 1;
1745 if (M_TRAILINGSPACE(mbuf) - m_ofs <= 0) {
1746 /* Must move to the next mbuf */
1748 mbuf = mbuf->m_next;
1756 * Check the status of the grant copy operations, and update mbufs various
1757 * non-data fields to reflect the data present.
1758 * \param[in,out] mbufc mbuf chain to update. The chain must be valid and of
1759 * the correct length, and data should already be present
1760 * \param[in] gnttab A grant table for a just completed copy op
1761 * \param[in] n_entries The number of valid entries in the grant table
1764 xnb_update_mbufc(struct mbuf *mbufc, const gnttab_copy_table gnttab,
1767 struct mbuf *mbuf = mbufc;
1769 size_t total_size = 0;
1771 for (i = 0; i < n_entries; i++) {
1772 KASSERT(gnttab[i].status == GNTST_okay,
1773 ("Some gnttab_copy entry had error status %hd\n",
1776 mbuf->m_len += gnttab[i].len;
1777 total_size += gnttab[i].len;
1778 if (M_TRAILINGSPACE(mbuf) <= 0) {
1779 mbuf = mbuf->m_next;
1782 mbufc->m_pkthdr.len = total_size;
1784 #if defined(INET) || defined(INET6)
1785 xnb_add_mbuf_cksum(mbufc);
1790 * Dequeue at most one packet from the shared ring
1791 * \param[in,out] txb Netif tx ring. A packet will be removed from it, and
1792 * its private indices will be updated. But the indices
1793 * will not be pushed to the shared ring.
1794 * \param[in] ifnet Interface to which the packet will be sent
1795 * \param[in] otherend Domain ID of the other end of the ring
1796 * \param[out] mbufc The assembled mbuf chain, ready to send to the generic
1798 * \param[in,out] gnttab Pointer to enough memory for a grant table. We make
1799 * this a function parameter so that we will take less
1801 * \return An error code
1804 xnb_recv(netif_tx_back_ring_t *txb, domid_t otherend, struct mbuf **mbufc,
1805 struct ifnet *ifnet, gnttab_copy_table gnttab)
1808 /* number of tx requests consumed to build the last packet */
1813 num_consumed = xnb_ring2pkt(&pkt, txb, txb->req_cons);
1814 if (num_consumed == 0)
1815 return 0; /* Nothing to receive */
1817 /* update statistics independent of errors */
1818 ifnet->if_ipackets++;
1821 * if we got here, then 1 or more requests was consumed, but the packet
1822 * is not necessarily valid.
1824 if (xnb_pkt_is_valid(&pkt) == 0) {
1825 /* got a garbage packet, respond and drop it */
1826 xnb_txpkt2rsp(&pkt, txb, 1);
1827 txb->req_cons += num_consumed;
1828 DPRINTF("xnb_intr: garbage packet, num_consumed=%d\n",
1830 ifnet->if_ierrors++;
1834 *mbufc = xnb_pkt2mbufc(&pkt, ifnet);
1836 if (*mbufc == NULL) {
1838 * Couldn't allocate mbufs. Respond and drop the packet. Do
1839 * not consume the requests
1841 xnb_txpkt2rsp(&pkt, txb, 1);
1842 DPRINTF("xnb_intr: Couldn't allocate mbufs, num_consumed=%d\n",
1844 ifnet->if_iqdrops++;
1848 nr_ents = xnb_txpkt2gnttab(&pkt, *mbufc, gnttab, txb, otherend);
1851 int __unused hv_ret = HYPERVISOR_grant_table_op(GNTTABOP_copy,
1853 KASSERT(hv_ret == 0,
1854 ("HYPERVISOR_grant_table_op returned %d\n", hv_ret));
1855 xnb_update_mbufc(*mbufc, gnttab, nr_ents);
1858 xnb_txpkt2rsp(&pkt, txb, 0);
1859 txb->req_cons += num_consumed;
1864 * Create an xnb_pkt based on the contents of an mbuf chain.
1865 * \param[in] mbufc mbuf chain to transform into a packet
1866 * \param[out] pkt Storage for the newly generated xnb_pkt
1867 * \param[in] start The ring index of the first available slot in the rx
1869 * \param[in] space The number of free slots in the rx ring
1871 * \retval EINVAL mbufc was corrupt or not convertible into a pkt
1872 * \retval EAGAIN There was not enough space in the ring to queue the
1876 xnb_mbufc2pkt(const struct mbuf *mbufc, struct xnb_pkt *pkt,
1877 RING_IDX start, int space)
1882 if ((mbufc == NULL) ||
1883 ( (mbufc->m_flags & M_PKTHDR) == 0) ||
1884 (mbufc->m_pkthdr.len == 0)) {
1885 xnb_pkt_invalidate(pkt);
1890 xnb_pkt_validate(pkt);
1892 pkt->size = mbufc->m_pkthdr.len;
1894 pkt->car_size = mbufc->m_len;
1896 if (mbufc->m_pkthdr.csum_flags & CSUM_TSO) {
1897 pkt->flags |= NETRXF_extra_info;
1898 pkt->extra.u.gso.size = mbufc->m_pkthdr.tso_segsz;
1899 pkt->extra.u.gso.type = XEN_NETIF_GSO_TYPE_TCPV4;
1900 pkt->extra.u.gso.pad = 0;
1901 pkt->extra.u.gso.features = 0;
1902 pkt->extra.type = XEN_NETIF_EXTRA_TYPE_GSO;
1903 pkt->extra.flags = 0;
1904 pkt->cdr = start + 2;
1906 pkt->cdr = start + 1;
1908 if (mbufc->m_pkthdr.csum_flags & (CSUM_TSO | CSUM_DELAY_DATA)) {
1910 (NETRXF_csum_blank | NETRXF_data_validated);
1914 * Each ring response can have up to PAGE_SIZE of data.
1915 * Assume that we can defragment the mbuf chain efficiently
1916 * into responses so that each response but the last uses all
1919 pkt->list_len = (pkt->size + PAGE_SIZE - 1) / PAGE_SIZE;
1921 if (pkt->list_len > 1) {
1922 pkt->flags |= NETRXF_more_data;
1925 slots_required = pkt->list_len +
1926 (pkt->flags & NETRXF_extra_info ? 1 : 0);
1927 if (slots_required > space) {
1928 xnb_pkt_invalidate(pkt);
1937 * Build a gnttab_copy table that can be used to copy data from an mbuf chain
1938 * to the frontend's shared buffers. Does not actually perform the copy.
1939 * Always uses gref's on the other end's side.
1940 * \param[in] pkt pkt's associated responses form the dest for the copy
1942 * \param[in] mbufc The source for the copy operation
1943 * \param[out] gnttab Storage for the returned grant table
1944 * \param[in] rxb Pointer to the backend ring structure
1945 * \param[in] otherend_id The domain ID of the other end of the copy
1946 * \return The number of gnttab entries filled
1949 xnb_rxpkt2gnttab(const struct xnb_pkt *pkt, const struct mbuf *mbufc,
1950 gnttab_copy_table gnttab, const netif_rx_back_ring_t *rxb,
1951 domid_t otherend_id)
1954 const struct mbuf *mbuf = mbufc;/* current mbuf within the chain */
1955 int gnt_idx = 0; /* index into grant table */
1956 RING_IDX r_idx = pkt->car; /* index into rx ring buffer */
1957 int r_ofs = 0; /* offset of next data within rx request's data area */
1958 int m_ofs = 0; /* offset of next data within mbuf's data area */
1959 /* size in bytes that still needs to be represented in the table */
1960 uint16_t size_remaining;
1962 size_remaining = (xnb_pkt_is_valid(pkt) != 0) ? pkt->size : 0;
1964 while (size_remaining > 0) {
1965 const netif_rx_request_t *rxq = RING_GET_REQUEST(rxb, r_idx);
1966 const size_t mbuf_space = mbuf->m_len - m_ofs;
1967 /* Xen shared pages have an implied size of PAGE_SIZE */
1968 const size_t req_size = PAGE_SIZE;
1969 const size_t pkt_space = req_size - r_ofs;
1971 * space is the largest amount of data that can be copied in the
1972 * grant table's next entry
1974 const size_t space = MIN(pkt_space, mbuf_space);
1976 /* TODO: handle this error condition without panicing */
1977 KASSERT(gnt_idx < GNTTAB_LEN, ("Grant table is too short"));
1979 gnttab[gnt_idx].dest.u.ref = rxq->gref;
1980 gnttab[gnt_idx].dest.domid = otherend_id;
1981 gnttab[gnt_idx].dest.offset = r_ofs;
1982 gnttab[gnt_idx].source.u.gmfn = virt_to_mfn(
1983 mtod(mbuf, vm_offset_t) + m_ofs);
1984 gnttab[gnt_idx].source.offset = virt_to_offset(
1985 mtod(mbuf, vm_offset_t) + m_ofs);
1986 gnttab[gnt_idx].source.domid = DOMID_SELF;
1987 gnttab[gnt_idx].len = space;
1988 gnttab[gnt_idx].flags = GNTCOPY_dest_gref;
1994 size_remaining -= space;
1995 if (req_size - r_ofs <= 0) {
1996 /* Must move to the next rx request */
1998 r_idx = (r_idx == pkt->car) ? pkt->cdr : r_idx + 1;
2000 if (mbuf->m_len - m_ofs <= 0) {
2001 /* Must move to the next mbuf */
2003 mbuf = mbuf->m_next;
2011 * Generates responses for all the requests that constituted pkt. Builds
2012 * responses and writes them to the ring, but doesn't push the shared ring
2014 * \param[in] pkt the packet that needs a response
2015 * \param[in] gnttab The grant copy table corresponding to this packet.
2016 * Used to determine how many rsp->netif_rx_response_t's to
2018 * \param[in] n_entries Number of relevant entries in the grant table
2019 * \param[out] ring Responses go here
2020 * \return The number of RX requests that were consumed to generate
2024 xnb_rxpkt2rsp(const struct xnb_pkt *pkt, const gnttab_copy_table gnttab,
2025 int n_entries, netif_rx_back_ring_t *ring)
2028 * This code makes the following assumptions:
2029 * * All entries in gnttab set GNTCOPY_dest_gref
2030 * * The entries in gnttab are grouped by their grefs: any two
2031 * entries with the same gref must be adjacent
2035 int n_responses = 0;
2036 grant_ref_t last_gref = GRANT_REF_INVALID;
2039 KASSERT(gnttab != NULL, ("Received a null granttable copy"));
2042 * In the event of an error, we only need to send one response to the
2043 * netfront. In that case, we musn't write any data to the responses
2044 * after the one we send. So we must loop all the way through gnttab
2045 * looking for errors before we generate any responses
2047 * Since we're looping through the grant table anyway, we'll count the
2048 * number of different gref's in it, which will tell us how many
2049 * responses to generate
2051 for (gnt_idx = 0; gnt_idx < n_entries; gnt_idx++) {
2052 int16_t status = gnttab[gnt_idx].status;
2053 if (status != GNTST_okay) {
2055 "Got error %d for hypervisor gnttab_copy status\n",
2060 if (gnttab[gnt_idx].dest.u.ref != last_gref) {
2062 last_gref = gnttab[gnt_idx].dest.u.ref;
2068 netif_rx_response_t *rsp;
2070 id = RING_GET_REQUEST(ring, ring->rsp_prod_pvt)->id;
2071 rsp = RING_GET_RESPONSE(ring, ring->rsp_prod_pvt);
2073 rsp->status = NETIF_RSP_ERROR;
2077 const int has_extra = pkt->flags & NETRXF_extra_info;
2081 for (i = 0; i < n_responses; i++) {
2082 netif_rx_request_t rxq;
2083 netif_rx_response_t *rsp;
2085 r_idx = ring->rsp_prod_pvt + i;
2087 * We copy the structure of rxq instead of making a
2088 * pointer because it shares the same memory as rsp.
2090 rxq = *(RING_GET_REQUEST(ring, r_idx));
2091 rsp = RING_GET_RESPONSE(ring, r_idx);
2092 if (has_extra && (i == 1)) {
2093 netif_extra_info_t *ext =
2094 (netif_extra_info_t*)rsp;
2095 ext->type = XEN_NETIF_EXTRA_TYPE_GSO;
2097 ext->u.gso.size = pkt->extra.u.gso.size;
2098 ext->u.gso.type = XEN_NETIF_GSO_TYPE_TCPV4;
2100 ext->u.gso.features = 0;
2103 rsp->status = GNTST_okay;
2106 if (i < pkt->list_len - 1)
2107 rsp->flags |= NETRXF_more_data;
2108 if ((i == 0) && has_extra)
2109 rsp->flags |= NETRXF_extra_info;
2111 (pkt->flags & NETRXF_data_validated)) {
2112 rsp->flags |= NETRXF_data_validated;
2113 rsp->flags |= NETRXF_csum_blank;
2116 for (; gnttab[gnt_idx].dest.u.ref == rxq.gref;
2118 rsp->status += gnttab[gnt_idx].len;
2124 ring->req_cons += n_responses;
2125 ring->rsp_prod_pvt += n_responses;
2129 #if defined(INET) || defined(INET6)
2131 * Add IP, TCP, and/or UDP checksums to every mbuf in a chain. The first mbuf
2132 * in the chain must start with a struct ether_header.
2134 * XXX This function will perform incorrectly on UDP packets that are split up
2135 * into multiple ethernet frames.
2138 xnb_add_mbuf_cksum(struct mbuf *mbufc)
2140 struct ether_header *eh;
2142 uint16_t ether_type;
2144 eh = mtod(mbufc, struct ether_header*);
2145 ether_type = ntohs(eh->ether_type);
2146 if (ether_type != ETHERTYPE_IP) {
2147 /* Nothing to calculate */
2151 iph = (struct ip*)(eh + 1);
2152 if (mbufc->m_pkthdr.csum_flags & CSUM_IP_VALID) {
2154 iph->ip_sum = in_cksum_hdr(iph);
2157 switch (iph->ip_p) {
2159 if (mbufc->m_pkthdr.csum_flags & CSUM_IP_VALID) {
2160 size_t tcplen = ntohs(iph->ip_len) - sizeof(struct ip);
2161 struct tcphdr *th = (struct tcphdr*)(iph + 1);
2162 th->th_sum = in_pseudo(iph->ip_src.s_addr,
2163 iph->ip_dst.s_addr, htons(IPPROTO_TCP + tcplen));
2164 th->th_sum = in_cksum_skip(mbufc,
2165 sizeof(struct ether_header) + ntohs(iph->ip_len),
2166 sizeof(struct ether_header) + (iph->ip_hl << 2));
2170 if (mbufc->m_pkthdr.csum_flags & CSUM_IP_VALID) {
2171 size_t udplen = ntohs(iph->ip_len) - sizeof(struct ip);
2172 struct udphdr *uh = (struct udphdr*)(iph + 1);
2173 uh->uh_sum = in_pseudo(iph->ip_src.s_addr,
2174 iph->ip_dst.s_addr, htons(IPPROTO_UDP + udplen));
2175 uh->uh_sum = in_cksum_skip(mbufc,
2176 sizeof(struct ether_header) + ntohs(iph->ip_len),
2177 sizeof(struct ether_header) + (iph->ip_hl << 2));
2184 #endif /* INET || INET6 */
2187 xnb_stop(struct xnb_softc *xnb)
2191 mtx_assert(&xnb->sc_lock, MA_OWNED);
2193 ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
2194 if_link_state_change(ifp, LINK_STATE_DOWN);
2198 xnb_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
2200 struct xnb_softc *xnb = ifp->if_softc;
2201 struct ifreq *ifr = (struct ifreq*) data;
2203 struct ifaddr *ifa = (struct ifaddr*)data;
2209 mtx_lock(&xnb->sc_lock);
2210 if (ifp->if_flags & IFF_UP) {
2211 xnb_ifinit_locked(xnb);
2213 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
2218 * Note: netfront sets a variable named xn_if_flags
2219 * here, but that variable is never read
2221 mtx_unlock(&xnb->sc_lock);
2226 mtx_lock(&xnb->sc_lock);
2227 if (ifa->ifa_addr->sa_family == AF_INET) {
2228 ifp->if_flags |= IFF_UP;
2229 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
2230 ifp->if_drv_flags &= ~(IFF_DRV_RUNNING |
2232 if_link_state_change(ifp,
2234 ifp->if_drv_flags |= IFF_DRV_RUNNING;
2235 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
2236 if_link_state_change(ifp,
2239 arp_ifinit(ifp, ifa);
2240 mtx_unlock(&xnb->sc_lock);
2242 mtx_unlock(&xnb->sc_lock);
2244 error = ether_ioctl(ifp, cmd, data);
2250 mtx_lock(&xnb->sc_lock);
2251 if (ifr->ifr_reqcap & IFCAP_TXCSUM) {
2252 ifp->if_capenable |= IFCAP_TXCSUM;
2253 ifp->if_hwassist |= XNB_CSUM_FEATURES;
2255 ifp->if_capenable &= ~(IFCAP_TXCSUM);
2256 ifp->if_hwassist &= ~(XNB_CSUM_FEATURES);
2258 if ((ifr->ifr_reqcap & IFCAP_RXCSUM)) {
2259 ifp->if_capenable |= IFCAP_RXCSUM;
2261 ifp->if_capenable &= ~(IFCAP_RXCSUM);
2264 * TODO enable TSO4 and LRO once we no longer need
2265 * to calculate checksums in software
2268 if (ifr->if_reqcap |= IFCAP_TSO4) {
2269 if (IFCAP_TXCSUM & ifp->if_capenable) {
2270 printf("xnb: Xen netif requires that "
2271 "TXCSUM be enabled in order "
2275 ifp->if_capenable |= IFCAP_TSO4;
2276 ifp->if_hwassist |= CSUM_TSO;
2279 ifp->if_capenable &= ~(IFCAP_TSO4);
2280 ifp->if_hwassist &= ~(CSUM_TSO);
2282 if (ifr->ifreqcap |= IFCAP_LRO) {
2283 ifp->if_capenable |= IFCAP_LRO;
2285 ifp->if_capenable &= ~(IFCAP_LRO);
2288 mtx_unlock(&xnb->sc_lock);
2291 ifp->if_mtu = ifr->ifr_mtu;
2292 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
2299 error = ifmedia_ioctl(ifp, ifr, &xnb->sc_media, cmd);
2302 error = ether_ioctl(ifp, cmd, data);
2309 xnb_start_locked(struct ifnet *ifp)
2311 netif_rx_back_ring_t *rxb;
2312 struct xnb_softc *xnb;
2314 RING_IDX req_prod_local;
2316 xnb = ifp->if_softc;
2317 rxb = &xnb->ring_configs[XNB_RING_TYPE_RX].back_ring.rx_ring;
2323 int out_of_space = 0;
2325 req_prod_local = rxb->sring->req_prod;
2330 IF_DEQUEUE(&ifp->if_snd, mbufc);
2333 error = xnb_send(rxb, xnb->otherend_id, mbufc,
2338 * Insufficient space in the ring.
2339 * Requeue pkt and send when space is
2342 IF_PREPEND(&ifp->if_snd, mbufc);
2344 * Perhaps the frontend missed an IRQ
2345 * and went to sleep. Notify it to wake
2352 /* OS gave a corrupt packet. Drop it.*/
2356 /* Send succeeded, or packet had error.
2357 * Free the packet */
2363 if (out_of_space != 0)
2367 RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(rxb, notify);
2368 if ((notify != 0) || (out_of_space != 0))
2369 xen_intr_signal(xnb->xen_intr_handle);
2370 rxb->sring->req_event = req_prod_local + 1;
2372 } while (rxb->sring->req_prod != req_prod_local) ;
2376 * Sends one packet to the ring. Blocks until the packet is on the ring
2377 * \param[in] mbufc Contains one packet to send. Caller must free
2378 * \param[in,out] rxb The packet will be pushed onto this ring, but the
2379 * otherend will not be notified.
2380 * \param[in] otherend The domain ID of the other end of the connection
2381 * \retval EAGAIN The ring did not have enough space for the packet.
2382 * The ring has not been modified
2383 * \param[in,out] gnttab Pointer to enough memory for a grant table. We make
2384 * this a function parameter so that we will take less
2386 * \retval EINVAL mbufc was corrupt or not convertible into a pkt
2389 xnb_send(netif_rx_back_ring_t *ring, domid_t otherend, const struct mbuf *mbufc,
2390 gnttab_copy_table gnttab)
2393 int error, n_entries, n_reqs;
2396 space = ring->sring->req_prod - ring->req_cons;
2397 error = xnb_mbufc2pkt(mbufc, &pkt, ring->rsp_prod_pvt, space);
2400 n_entries = xnb_rxpkt2gnttab(&pkt, mbufc, gnttab, ring, otherend);
2401 if (n_entries != 0) {
2402 int __unused hv_ret = HYPERVISOR_grant_table_op(GNTTABOP_copy,
2404 KASSERT(hv_ret == 0, ("HYPERVISOR_grant_table_op returned %d\n",
2408 n_reqs = xnb_rxpkt2rsp(&pkt, gnttab, n_entries, ring);
2414 xnb_start(struct ifnet *ifp)
2416 struct xnb_softc *xnb;
2418 xnb = ifp->if_softc;
2419 mtx_lock(&xnb->rx_lock);
2420 xnb_start_locked(ifp);
2421 mtx_unlock(&xnb->rx_lock);
2424 /* equivalent of network_open() in Linux */
2426 xnb_ifinit_locked(struct xnb_softc *xnb)
2432 mtx_assert(&xnb->sc_lock, MA_OWNED);
2434 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
2439 ifp->if_drv_flags |= IFF_DRV_RUNNING;
2440 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
2441 if_link_state_change(ifp, LINK_STATE_UP);
2446 xnb_ifinit(void *xsc)
2448 struct xnb_softc *xnb = xsc;
2450 mtx_lock(&xnb->sc_lock);
2451 xnb_ifinit_locked(xnb);
2452 mtx_unlock(&xnb->sc_lock);
2457 * Read the 'mac' node at the given device's node in the store, and parse that
2458 * as colon-separated octets, placing result the given mac array. mac must be
2459 * a preallocated array of length ETHER_ADDR_LEN ETH_ALEN (as declared in
2461 * Return 0 on success, or errno on error.
2464 xen_net_read_mac(device_t dev, uint8_t mac[])
2466 char *s, *e, *macstr;
2471 path = xenbus_get_node(dev);
2472 error = xs_read(XST_NIL, path, "mac", NULL, (void **) &macstr);
2474 xenbus_dev_fatal(dev, error, "parsing %s/mac", path);
2477 for (i = 0; i < ETHER_ADDR_LEN; i++) {
2478 mac[i] = strtoul(s, &e, 16);
2479 if (s == e || (e[0] != ':' && e[0] != 0)) {
2485 free(macstr, M_XENBUS);
2492 * Callback used by the generic networking code to tell us when our carrier
2493 * state has changed. Since we don't have a physical carrier, we don't care
2496 xnb_ifmedia_upd(struct ifnet *ifp)
2502 * Callback used by the generic networking code to ask us what our carrier
2503 * state is. Since we don't have a physical carrier, this is very simple
2506 xnb_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
2508 ifmr->ifm_status = IFM_AVALID|IFM_ACTIVE;
2509 ifmr->ifm_active = IFM_ETHER|IFM_MANUAL;
2513 /*---------------------------- NewBus Registration ---------------------------*/
2514 static device_method_t xnb_methods[] = {
2515 /* Device interface */
2516 DEVMETHOD(device_probe, xnb_probe),
2517 DEVMETHOD(device_attach, xnb_attach),
2518 DEVMETHOD(device_detach, xnb_detach),
2519 DEVMETHOD(device_shutdown, bus_generic_shutdown),
2520 DEVMETHOD(device_suspend, xnb_suspend),
2521 DEVMETHOD(device_resume, xnb_resume),
2523 /* Xenbus interface */
2524 DEVMETHOD(xenbus_otherend_changed, xnb_frontend_changed),
2529 static driver_t xnb_driver = {
2532 sizeof(struct xnb_softc),
2534 devclass_t xnb_devclass;
2536 DRIVER_MODULE(xnb, xenbusb_back, xnb_driver, xnb_devclass, 0, 0);
2539 /*-------------------------- Unit Tests -------------------------------------*/
2541 #include "netback_unit_tests.c"