2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2011 NetApp, Inc.
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 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
34 #include <sys/param.h>
35 #include <sys/linker_set.h>
36 #include <sys/select.h>
38 #include <sys/ioctl.h>
39 #include <net/ethernet.h>
40 #include <net/if.h> /* IFNAMSIZ */
53 #include <pthread_np.h>
60 #include "net_utils.h"
61 #include "net_backends.h"
64 #define VTNET_RINGSZ 1024
66 #define VTNET_MAXSEGS 256
68 #define VTNET_MAX_PKT_LEN (65536 + 64)
70 #define VTNET_S_HOSTCAPS \
71 ( VIRTIO_NET_F_MAC | VIRTIO_NET_F_STATUS | \
72 VIRTIO_F_NOTIFY_ON_EMPTY | VIRTIO_RING_F_INDIRECT_DESC)
75 * PCI config-space "registers"
77 struct virtio_net_config {
87 #define VTNET_CTLQ 2 /* NB: not yet supported */
94 static int pci_vtnet_debug;
95 #define DPRINTF(params) if (pci_vtnet_debug) PRINTLN params
96 #define WPRINTF(params) PRINTLN params
101 struct pci_vtnet_softc {
102 struct virtio_softc vsc_vs;
103 struct vqueue_info vsc_queues[VTNET_MAXQ - 1];
104 pthread_mutex_t vsc_mtx;
106 net_backend_t *vsc_be;
108 int resetting; /* protected by tx_mtx */
110 uint64_t vsc_features; /* negotiated features */
112 pthread_mutex_t rx_mtx;
113 int rx_merge; /* merged rx bufs in use */
116 pthread_mutex_t tx_mtx;
117 pthread_cond_t tx_cond;
120 struct virtio_net_config vsc_config;
121 struct virtio_consts vsc_consts;
124 static void pci_vtnet_reset(void *);
125 /* static void pci_vtnet_notify(void *, struct vqueue_info *); */
126 static int pci_vtnet_cfgread(void *, int, int, uint32_t *);
127 static int pci_vtnet_cfgwrite(void *, int, int, uint32_t);
128 static void pci_vtnet_neg_features(void *, uint64_t);
130 static struct virtio_consts vtnet_vi_consts = {
131 "vtnet", /* our name */
132 VTNET_MAXQ - 1, /* we currently support 2 virtqueues */
133 sizeof(struct virtio_net_config), /* config reg size */
134 pci_vtnet_reset, /* reset */
135 NULL, /* device-wide qnotify -- not used */
136 pci_vtnet_cfgread, /* read PCI config */
137 pci_vtnet_cfgwrite, /* write PCI config */
138 pci_vtnet_neg_features, /* apply negotiated features */
139 VTNET_S_HOSTCAPS, /* our capabilities */
143 pci_vtnet_reset(void *vsc)
145 struct pci_vtnet_softc *sc = vsc;
147 DPRINTF(("vtnet: device reset requested !"));
149 /* Acquire the RX lock to block RX processing. */
150 pthread_mutex_lock(&sc->rx_mtx);
153 * Make sure receive operation is disabled at least until we
154 * re-negotiate the features, since receive operation depends
155 * on the value of sc->rx_merge and the header length, which
156 * are both set in pci_vtnet_neg_features().
157 * Receive operation will be enabled again once the guest adds
158 * the first receive buffers and kicks us.
160 netbe_rx_disable(sc->vsc_be);
162 /* Set sc->resetting and give a chance to the TX thread to stop. */
163 pthread_mutex_lock(&sc->tx_mtx);
165 while (sc->tx_in_progress) {
166 pthread_mutex_unlock(&sc->tx_mtx);
168 pthread_mutex_lock(&sc->tx_mtx);
172 * Now reset rings, MSI-X vectors, and negotiated capabilities.
173 * Do that with the TX lock held, since we need to reset
176 vi_reset_dev(&sc->vsc_vs);
179 pthread_mutex_unlock(&sc->tx_mtx);
180 pthread_mutex_unlock(&sc->rx_mtx);
183 struct virtio_mrg_rxbuf_info {
190 pci_vtnet_rx(struct pci_vtnet_softc *sc)
192 struct virtio_mrg_rxbuf_info info[VTNET_MAXSEGS];
193 struct iovec iov[VTNET_MAXSEGS + 1];
194 struct vqueue_info *vq;
195 uint32_t cur_iov_bytes;
196 struct iovec *cur_iov;
197 uint16_t cur_iov_len;
202 vq = &sc->vsc_queues[VTNET_RXQ];
205 * Get a descriptor chain to store the next ingress
206 * packet. In case of mergeable rx buffers, get as
207 * many chains as necessary in order to make room
208 * for a maximum sized LRO packet.
215 int n = vq_getchain(vq, &info[n_chains].idx, cur_iov,
216 VTNET_MAXSEGS - cur_iov_len, NULL);
220 * No rx buffers. Enable RX kicks and double
224 if (!vq_has_descs(vq)) {
226 * Still no buffers. Return the unused
227 * chains (if any), interrupt if needed
228 * (including for NOTIFY_ON_EMPTY), and
229 * disable the backend until the next
232 vq_retchains(vq, n_chains);
233 vq_endchains(vq, /*used_all_avail=*/1);
234 netbe_rx_disable(sc->vsc_be);
238 /* More rx buffers found, so keep going. */
242 assert(n >= 1 && cur_iov_len + n <= VTNET_MAXSEGS);
248 info[n_chains].len = (uint32_t)count_iov(cur_iov, n);
249 cur_iov_bytes += info[n_chains].len;
252 } while (cur_iov_bytes < VTNET_MAX_PKT_LEN &&
253 cur_iov_len < VTNET_MAXSEGS);
255 len = netbe_recv(sc->vsc_be, iov, cur_iov_len);
259 * No more packets (len == 0), or backend errored
260 * (err < 0). Return unused available buffers
263 vq_retchains(vq, n_chains);
264 /* Interrupt if needed/appropriate and stop. */
265 vq_endchains(vq, /*used_all_avail=*/0);
269 ulen = (uint32_t)len; /* avoid too many casts below */
271 /* Publish the used buffers to the guest. */
273 vq_relchain(vq, info[0].idx, ulen);
275 struct virtio_net_rxhdr *hdr = iov[0].iov_base;
279 assert(iov[0].iov_len >= sizeof(*hdr));
286 vq_relchain_prepare(vq, info[i].idx, iolen);
289 assert(i <= n_chains);
293 vq_relchain_publish(vq);
294 vq_retchains(vq, n_chains - i);
301 * Called when there is read activity on the backend file descriptor.
302 * Each buffer posted by the guest is assumed to be able to contain
303 * an entire ethernet frame + rx header.
306 pci_vtnet_rx_callback(int fd, enum ev_type type, void *param)
308 struct pci_vtnet_softc *sc = param;
310 pthread_mutex_lock(&sc->rx_mtx);
312 pthread_mutex_unlock(&sc->rx_mtx);
316 /* Called on RX kick. */
318 pci_vtnet_ping_rxq(void *vsc, struct vqueue_info *vq)
320 struct pci_vtnet_softc *sc = vsc;
323 * A qnotify means that the rx process can now begin.
325 pthread_mutex_lock(&sc->rx_mtx);
327 netbe_rx_enable(sc->vsc_be);
328 pthread_mutex_unlock(&sc->rx_mtx);
331 /* TX virtqueue processing, called by the TX thread. */
333 pci_vtnet_proctx(struct pci_vtnet_softc *sc, struct vqueue_info *vq)
335 struct iovec iov[VTNET_MAXSEGS + 1];
341 * Obtain chain of descriptors. The first descriptor also
342 * contains the virtio-net header.
344 n = vq_getchain(vq, &idx, iov, VTNET_MAXSEGS, NULL);
345 assert(n >= 1 && n <= VTNET_MAXSEGS);
347 len = netbe_send(sc->vsc_be, iov, n);
349 /* chain is processed, release it and set len */
350 vq_relchain(vq, idx, len > 0 ? len : 0);
353 /* Called on TX kick. */
355 pci_vtnet_ping_txq(void *vsc, struct vqueue_info *vq)
357 struct pci_vtnet_softc *sc = vsc;
360 * Any ring entries to process?
362 if (!vq_has_descs(vq))
365 /* Signal the tx thread for processing */
366 pthread_mutex_lock(&sc->tx_mtx);
368 if (sc->tx_in_progress == 0)
369 pthread_cond_signal(&sc->tx_cond);
370 pthread_mutex_unlock(&sc->tx_mtx);
374 * Thread which will handle processing of TX desc
377 pci_vtnet_tx_thread(void *param)
379 struct pci_vtnet_softc *sc = param;
380 struct vqueue_info *vq;
383 vq = &sc->vsc_queues[VTNET_TXQ];
386 * Let us wait till the tx queue pointers get initialised &
389 pthread_mutex_lock(&sc->tx_mtx);
390 error = pthread_cond_wait(&sc->tx_cond, &sc->tx_mtx);
394 /* note - tx mutex is locked here */
395 while (sc->resetting || !vq_has_descs(vq)) {
397 if (!sc->resetting && vq_has_descs(vq))
400 sc->tx_in_progress = 0;
401 error = pthread_cond_wait(&sc->tx_cond, &sc->tx_mtx);
405 sc->tx_in_progress = 1;
406 pthread_mutex_unlock(&sc->tx_mtx);
410 * Run through entries, placing them into
411 * iovecs and sending when an end-of-packet
414 pci_vtnet_proctx(sc, vq);
415 } while (vq_has_descs(vq));
418 * Generate an interrupt if needed.
420 vq_endchains(vq, /*used_all_avail=*/1);
422 pthread_mutex_lock(&sc->tx_mtx);
428 pci_vtnet_ping_ctlq(void *vsc, struct vqueue_info *vq)
431 DPRINTF(("vtnet: control qnotify!"));
436 pci_vtnet_init(struct vmctx *ctx, struct pci_devinst *pi, char *opts)
438 struct pci_vtnet_softc *sc;
439 char tname[MAXCOMLEN + 1];
443 * Allocate data structures for further virtio initializations.
444 * sc also contains a copy of vtnet_vi_consts, since capabilities
445 * change depending on the backend.
447 sc = calloc(1, sizeof(struct pci_vtnet_softc));
449 sc->vsc_consts = vtnet_vi_consts;
450 pthread_mutex_init(&sc->vsc_mtx, NULL);
452 sc->vsc_queues[VTNET_RXQ].vq_qsize = VTNET_RINGSZ;
453 sc->vsc_queues[VTNET_RXQ].vq_notify = pci_vtnet_ping_rxq;
454 sc->vsc_queues[VTNET_TXQ].vq_qsize = VTNET_RINGSZ;
455 sc->vsc_queues[VTNET_TXQ].vq_notify = pci_vtnet_ping_txq;
457 sc->vsc_queues[VTNET_CTLQ].vq_qsize = VTNET_RINGSZ;
458 sc->vsc_queues[VTNET_CTLQ].vq_notify = pci_vtnet_ping_ctlq;
462 * Attempt to open the backend device and read the MAC address
471 devname = vtopts = strdup(opts);
472 (void) strsep(&vtopts, ",");
474 if (vtopts != NULL) {
475 err = net_parsemac(vtopts, sc->vsc_config.mac);
484 err = netbe_init(&sc->vsc_be, devname, pci_vtnet_rx_callback,
491 sc->vsc_consts.vc_hv_caps |= netbe_get_cap(sc->vsc_be);
495 net_genmac(pi, sc->vsc_config.mac);
498 /* initialize config space */
499 pci_set_cfgdata16(pi, PCIR_DEVICE, VIRTIO_DEV_NET);
500 pci_set_cfgdata16(pi, PCIR_VENDOR, VIRTIO_VENDOR);
501 pci_set_cfgdata8(pi, PCIR_CLASS, PCIC_NETWORK);
502 pci_set_cfgdata16(pi, PCIR_SUBDEV_0, VIRTIO_TYPE_NET);
503 pci_set_cfgdata16(pi, PCIR_SUBVEND_0, VIRTIO_VENDOR);
505 /* Link is up if we managed to open backend device. */
506 sc->vsc_config.status = (opts == NULL || sc->vsc_be);
508 vi_softc_linkup(&sc->vsc_vs, &sc->vsc_consts, sc, pi, sc->vsc_queues);
509 sc->vsc_vs.vs_mtx = &sc->vsc_mtx;
511 /* use BAR 1 to map MSI-X table and PBA, if we're using MSI-X */
512 if (vi_intr_init(&sc->vsc_vs, 1, fbsdrun_virtio_msix())) {
517 /* use BAR 0 to map config regs in IO space */
518 vi_set_io_bar(&sc->vsc_vs, 0);
523 pthread_mutex_init(&sc->rx_mtx, NULL);
526 * Initialize tx semaphore & spawn TX processing thread.
527 * As of now, only one thread for TX desc processing is
530 sc->tx_in_progress = 0;
531 pthread_mutex_init(&sc->tx_mtx, NULL);
532 pthread_cond_init(&sc->tx_cond, NULL);
533 pthread_create(&sc->tx_tid, NULL, pci_vtnet_tx_thread, (void *)sc);
534 snprintf(tname, sizeof(tname), "vtnet-%d:%d tx", pi->pi_slot,
536 pthread_set_name_np(sc->tx_tid, tname);
542 pci_vtnet_cfgwrite(void *vsc, int offset, int size, uint32_t value)
544 struct pci_vtnet_softc *sc = vsc;
547 if (offset < (int)sizeof(sc->vsc_config.mac)) {
548 assert(offset + size <= (int)sizeof(sc->vsc_config.mac));
550 * The driver is allowed to change the MAC address
552 ptr = &sc->vsc_config.mac[offset];
553 memcpy(ptr, &value, size);
555 /* silently ignore other writes */
556 DPRINTF(("vtnet: write to readonly reg %d", offset));
563 pci_vtnet_cfgread(void *vsc, int offset, int size, uint32_t *retval)
565 struct pci_vtnet_softc *sc = vsc;
568 ptr = (uint8_t *)&sc->vsc_config + offset;
569 memcpy(retval, ptr, size);
574 pci_vtnet_neg_features(void *vsc, uint64_t negotiated_features)
576 struct pci_vtnet_softc *sc = vsc;
577 unsigned int rx_vhdrlen;
579 sc->vsc_features = negotiated_features;
581 if (negotiated_features & VIRTIO_NET_F_MRG_RXBUF) {
582 rx_vhdrlen = sizeof(struct virtio_net_rxhdr);
586 * Without mergeable rx buffers, virtio-net header is 2
587 * bytes shorter than sizeof(struct virtio_net_rxhdr).
589 rx_vhdrlen = sizeof(struct virtio_net_rxhdr) - 2;
593 /* Tell the backend to enable some capabilities it has advertised. */
594 netbe_set_cap(sc->vsc_be, negotiated_features, rx_vhdrlen);
597 static struct pci_devemu pci_de_vnet = {
598 .pe_emu = "virtio-net",
599 .pe_init = pci_vtnet_init,
600 .pe_barwrite = vi_pci_write,
601 .pe_barread = vi_pci_read
603 PCI_EMUL_SET(pci_de_vnet);