Deleted Added
sdiff udiff text old ( 268953 ) new ( 271685 )
full compact
1/*-
2 * Copyright (c) 2011 NetApp, Inc.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright

--- 9 unchanged lines hidden (view full) ---

18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 *
26 * $FreeBSD: stable/10/usr.sbin/bhyve/pci_virtio_net.c 268953 2014-07-21 19:08:02Z jhb $
27 */
28
29#include <sys/cdefs.h>
30__FBSDID("$FreeBSD: stable/10/usr.sbin/bhyve/pci_virtio_net.c 268953 2014-07-21 19:08:02Z jhb $");
31
32#include <sys/param.h>
33#include <sys/linker_set.h>
34#include <sys/select.h>
35#include <sys/uio.h>
36#include <sys/ioctl.h>
37#include <net/ethernet.h>
38

--- 91 unchanged lines hidden (view full) ---

130 struct vqueue_info vsc_queues[VTNET_MAXQ - 1];
131 pthread_mutex_t vsc_mtx;
132 struct mevent *vsc_mevp;
133
134 int vsc_tapfd;
135 int vsc_rx_ready;
136 volatile int resetting; /* set and checked outside lock */
137
138 uint32_t vsc_features;
139 struct virtio_net_config vsc_config;
140
141 pthread_mutex_t rx_mtx;
142 int rx_in_progress;
143
144 pthread_t tx_tid;
145 pthread_mutex_t tx_mtx;
146 pthread_cond_t tx_cond;
147 int tx_in_progress;
148};
149
150static void pci_vtnet_reset(void *);
151/* static void pci_vtnet_notify(void *, struct vqueue_info *); */
152static int pci_vtnet_cfgread(void *, int, int, uint32_t *);
153static int pci_vtnet_cfgwrite(void *, int, int, uint32_t);
154
155static struct virtio_consts vtnet_vi_consts = {
156 "vtnet", /* our name */
157 VTNET_MAXQ - 1, /* we currently support 2 virtqueues */
158 sizeof(struct virtio_net_config), /* config reg size */
159 pci_vtnet_reset, /* reset */
160 NULL, /* device-wide qnotify -- not used */
161 pci_vtnet_cfgread, /* read PCI config */
162 pci_vtnet_cfgwrite, /* write PCI config */
163 VTNET_S_HOSTCAPS, /* our capabilities */
164};
165
166/*
167 * If the transmit thread is active then stall until it is done.
168 */
169static void
170pci_vtnet_txwait(struct pci_vtnet_softc *sc)

--- 36 unchanged lines hidden (view full) ---

207 /*
208 * Wait for the transmit and receive threads to finish their
209 * processing.
210 */
211 pci_vtnet_txwait(sc);
212 pci_vtnet_rxwait(sc);
213
214 sc->vsc_rx_ready = 0;
215
216 /* now reset rings, MSI-X vectors, and negotiated capabilities */
217 vi_reset_dev(&sc->vsc_vs);
218
219 sc->resetting = 0;
220}
221
222/*

--- 25 unchanged lines hidden (view full) ---

248 * Called when there is read activity on the tap file descriptor.
249 * Each buffer posted by the guest is assumed to be able to contain
250 * an entire ethernet frame + rx header.
251 * MP note: the dummybuf is only used for discarding frames, so there
252 * is no need for it to be per-vtnet or locked.
253 */
254static uint8_t dummybuf[2048];
255
256static void
257pci_vtnet_tap_rx(struct pci_vtnet_softc *sc)
258{
259 struct vqueue_info *vq;
260 struct virtio_net_rxhdr *vrx;
261 uint8_t *buf;
262 int len;
263 struct iovec iov;
264
265 /*
266 * Should never be called without a valid tap fd
267 */
268 assert(sc->vsc_tapfd != -1);
269
270 /*
271 * But, will be called when the rx ring hasn't yet

--- 19 unchanged lines hidden (view full) ---

291 */
292 (void) read(sc->vsc_tapfd, dummybuf, sizeof(dummybuf));
293 vq_endchains(vq, 1);
294 return;
295 }
296
297 do {
298 /*
299 * Get descriptor chain, which should have just
300 * one descriptor in it.
301 * ??? allow guests to use multiple descs?
302 */
303 assert(vq_getchain(vq, &iov, 1, NULL) == 1);
304
305 /*
306 * Get a pointer to the rx header, and use the
307 * data immediately following it for the packet buffer.
308 */
309 vrx = iov.iov_base;
310 buf = (uint8_t *)(vrx + 1);
311
312 len = read(sc->vsc_tapfd, buf,
313 iov.iov_len - sizeof(struct virtio_net_rxhdr));
314
315 if (len < 0 && errno == EWOULDBLOCK) {
316 /*
317 * No more packets, but still some avail ring
318 * entries. Interrupt if needed/appropriate.
319 */
320 vq_endchains(vq, 0);
321 return;
322 }
323
324 /*
325 * The only valid field in the rx packet header is the
326 * number of buffers, which is always 1 without TSO
327 * support.
328 */
329 memset(vrx, 0, sizeof(struct virtio_net_rxhdr));
330 vrx->vrh_bufs = 1;
331
332 /*
333 * Release this chain and handle more chains.
334 */
335 vq_relchain(vq, len + sizeof(struct virtio_net_rxhdr));
336 } while (vq_has_descs(vq));
337
338 /* Interrupt if needed, including for NOTIFY_ON_EMPTY. */
339 vq_endchains(vq, 1);
340}
341
342static void
343pci_vtnet_tap_callback(int fd, enum ev_type type, void *param)

--- 274 unchanged lines hidden (view full) ---

618 if (vi_intr_init(&sc->vsc_vs, 1, fbsdrun_virtio_msix()))
619 return (1);
620
621 /* use BAR 0 to map config regs in IO space */
622 vi_set_io_bar(&sc->vsc_vs, 0);
623
624 sc->resetting = 0;
625
626 sc->rx_in_progress = 0;
627 pthread_mutex_init(&sc->rx_mtx, NULL);
628
629 /*
630 * Initialize tx semaphore & spawn TX processing thread.
631 * As of now, only one thread for TX desc processing is
632 * spawned.
633 */

--- 17 unchanged lines hidden (view full) ---

651 if (offset < 6) {
652 assert(offset + size <= 6);
653 /*
654 * The driver is allowed to change the MAC address
655 */
656 ptr = &sc->vsc_config.mac[offset];
657 memcpy(ptr, &value, size);
658 } else {
659 DPRINTF(("vtnet: write to readonly reg %d\n\r", offset));
660 return (1);
661 }
662 return (0);
663}
664
665static int
666pci_vtnet_cfgread(void *vsc, int offset, int size, uint32_t *retval)
667{
668 struct pci_vtnet_softc *sc = vsc;
669 void *ptr;
670
671 ptr = (uint8_t *)&sc->vsc_config + offset;
672 memcpy(retval, ptr, size);
673 return (0);
674}
675
676struct pci_devemu pci_de_vnet = {
677 .pe_emu = "virtio-net",
678 .pe_init = pci_vtnet_init,
679 .pe_barwrite = vi_pci_write,
680 .pe_barread = vi_pci_read
681};
682PCI_EMUL_SET(pci_de_vnet);