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>
59 #include "net_utils.h"
60 #include "net_backends.h"
63 #define VTNET_RINGSZ 1024
65 #define VTNET_MAXSEGS 256
67 #define VTNET_MAX_PKT_LEN (65536 + 64)
69 #define VTNET_S_HOSTCAPS \
70 ( VIRTIO_NET_F_MAC | VIRTIO_NET_F_STATUS | \
71 VIRTIO_F_NOTIFY_ON_EMPTY | VIRTIO_RING_F_INDIRECT_DESC)
74 * PCI config-space "registers"
76 struct virtio_net_config {
86 #define VTNET_CTLQ 2 /* NB: not yet supported */
93 static int pci_vtnet_debug;
94 #define DPRINTF(params) if (pci_vtnet_debug) printf params
95 #define WPRINTF(params) printf params
100 struct pci_vtnet_softc {
101 struct virtio_softc vsc_vs;
102 struct vqueue_info vsc_queues[VTNET_MAXQ - 1];
103 pthread_mutex_t vsc_mtx;
105 net_backend_t *vsc_be;
107 int resetting; /* protected by tx_mtx */
109 uint64_t vsc_features; /* negotiated features */
111 pthread_mutex_t rx_mtx;
112 int rx_merge; /* merged rx bufs in use */
115 pthread_mutex_t tx_mtx;
116 pthread_cond_t tx_cond;
119 struct virtio_net_config vsc_config;
120 struct virtio_consts vsc_consts;
123 static void pci_vtnet_reset(void *);
124 /* static void pci_vtnet_notify(void *, struct vqueue_info *); */
125 static int pci_vtnet_cfgread(void *, int, int, uint32_t *);
126 static int pci_vtnet_cfgwrite(void *, int, int, uint32_t);
127 static void pci_vtnet_neg_features(void *, uint64_t);
129 static struct virtio_consts vtnet_vi_consts = {
130 "vtnet", /* our name */
131 VTNET_MAXQ - 1, /* we currently support 2 virtqueues */
132 sizeof(struct virtio_net_config), /* config reg size */
133 pci_vtnet_reset, /* reset */
134 NULL, /* device-wide qnotify -- not used */
135 pci_vtnet_cfgread, /* read PCI config */
136 pci_vtnet_cfgwrite, /* write PCI config */
137 pci_vtnet_neg_features, /* apply negotiated features */
138 VTNET_S_HOSTCAPS, /* our capabilities */
142 pci_vtnet_reset(void *vsc)
144 struct pci_vtnet_softc *sc = vsc;
146 DPRINTF(("vtnet: device reset requested !\n"));
148 /* Acquire the RX lock to block RX processing. */
149 pthread_mutex_lock(&sc->rx_mtx);
152 * Make sure receive operation is disabled at least until we
153 * re-negotiate the features, since receive operation depends
154 * on the value of sc->rx_merge and the header length, which
155 * are both set in pci_vtnet_neg_features().
156 * Receive operation will be enabled again once the guest adds
157 * the first receive buffers and kicks us.
159 netbe_rx_disable(sc->vsc_be);
161 /* Set sc->resetting and give a chance to the TX thread to stop. */
162 pthread_mutex_lock(&sc->tx_mtx);
164 while (sc->tx_in_progress) {
165 pthread_mutex_unlock(&sc->tx_mtx);
167 pthread_mutex_lock(&sc->tx_mtx);
171 * Now reset rings, MSI-X vectors, and negotiated capabilities.
172 * Do that with the TX lock held, since we need to reset
175 vi_reset_dev(&sc->vsc_vs);
178 pthread_mutex_unlock(&sc->tx_mtx);
179 pthread_mutex_unlock(&sc->rx_mtx);
182 struct virtio_mrg_rxbuf_info {
189 pci_vtnet_rx(struct pci_vtnet_softc *sc)
191 struct virtio_mrg_rxbuf_info info[VTNET_MAXSEGS];
192 struct iovec iov[VTNET_MAXSEGS + 1];
193 struct vqueue_info *vq;
194 uint32_t cur_iov_bytes;
195 struct iovec *cur_iov;
196 uint16_t cur_iov_len;
201 vq = &sc->vsc_queues[VTNET_RXQ];
204 * Get a descriptor chain to store the next ingress
205 * packet. In case of mergeable rx buffers, get as
206 * many chains as necessary in order to make room
207 * for a maximum sized LRO packet.
214 int n = vq_getchain(vq, &info[n_chains].idx, cur_iov,
215 VTNET_MAXSEGS - cur_iov_len, NULL);
219 * No rx buffers. Enable RX kicks and double
223 if (!vq_has_descs(vq)) {
225 * Still no buffers. Return the unused
226 * chains (if any), interrupt if needed
227 * (including for NOTIFY_ON_EMPTY), and
228 * disable the backend until the next
231 vq_retchains(vq, n_chains);
232 vq_endchains(vq, /*used_all_avail=*/1);
233 netbe_rx_disable(sc->vsc_be);
237 /* More rx buffers found, so keep going. */
241 assert(n >= 1 && cur_iov_len + n <= VTNET_MAXSEGS);
247 info[n_chains].len = (uint32_t)count_iov(cur_iov, n);
248 cur_iov_bytes += info[n_chains].len;
251 } while (cur_iov_bytes < VTNET_MAX_PKT_LEN &&
252 cur_iov_len < VTNET_MAXSEGS);
254 len = netbe_recv(sc->vsc_be, iov, cur_iov_len);
258 * No more packets (len == 0), or backend errored
259 * (err < 0). Return unused available buffers
262 vq_retchains(vq, n_chains);
263 /* Interrupt if needed/appropriate and stop. */
264 vq_endchains(vq, /*used_all_avail=*/0);
268 ulen = (uint32_t)len; /* avoid too many casts below */
270 /* Publish the used buffers to the guest. */
272 vq_relchain(vq, info[0].idx, ulen);
274 struct virtio_net_rxhdr *hdr = iov[0].iov_base;
278 assert(iov[0].iov_len >= sizeof(*hdr));
285 vq_relchain_prepare(vq, info[i].idx, iolen);
288 assert(i <= n_chains);
292 vq_relchain_publish(vq);
293 vq_retchains(vq, n_chains - i);
300 * Called when there is read activity on the backend file descriptor.
301 * Each buffer posted by the guest is assumed to be able to contain
302 * an entire ethernet frame + rx header.
305 pci_vtnet_rx_callback(int fd, enum ev_type type, void *param)
307 struct pci_vtnet_softc *sc = param;
309 pthread_mutex_lock(&sc->rx_mtx);
311 pthread_mutex_unlock(&sc->rx_mtx);
315 /* Called on RX kick. */
317 pci_vtnet_ping_rxq(void *vsc, struct vqueue_info *vq)
319 struct pci_vtnet_softc *sc = vsc;
322 * A qnotify means that the rx process can now begin.
324 pthread_mutex_lock(&sc->rx_mtx);
326 netbe_rx_enable(sc->vsc_be);
327 pthread_mutex_unlock(&sc->rx_mtx);
330 /* TX virtqueue processing, called by the TX thread. */
332 pci_vtnet_proctx(struct pci_vtnet_softc *sc, struct vqueue_info *vq)
334 struct iovec iov[VTNET_MAXSEGS + 1];
340 * Obtain chain of descriptors. The first descriptor also
341 * contains the virtio-net header.
343 n = vq_getchain(vq, &idx, iov, VTNET_MAXSEGS, NULL);
344 assert(n >= 1 && n <= VTNET_MAXSEGS);
346 len = netbe_send(sc->vsc_be, iov, n);
348 /* chain is processed, release it and set len */
349 vq_relchain(vq, idx, len > 0 ? len : 0);
352 /* Called on TX kick. */
354 pci_vtnet_ping_txq(void *vsc, struct vqueue_info *vq)
356 struct pci_vtnet_softc *sc = vsc;
359 * Any ring entries to process?
361 if (!vq_has_descs(vq))
364 /* Signal the tx thread for processing */
365 pthread_mutex_lock(&sc->tx_mtx);
367 if (sc->tx_in_progress == 0)
368 pthread_cond_signal(&sc->tx_cond);
369 pthread_mutex_unlock(&sc->tx_mtx);
373 * Thread which will handle processing of TX desc
376 pci_vtnet_tx_thread(void *param)
378 struct pci_vtnet_softc *sc = param;
379 struct vqueue_info *vq;
382 vq = &sc->vsc_queues[VTNET_TXQ];
385 * Let us wait till the tx queue pointers get initialised &
388 pthread_mutex_lock(&sc->tx_mtx);
389 error = pthread_cond_wait(&sc->tx_cond, &sc->tx_mtx);
393 /* note - tx mutex is locked here */
394 while (sc->resetting || !vq_has_descs(vq)) {
396 if (!sc->resetting && vq_has_descs(vq))
399 sc->tx_in_progress = 0;
400 error = pthread_cond_wait(&sc->tx_cond, &sc->tx_mtx);
404 sc->tx_in_progress = 1;
405 pthread_mutex_unlock(&sc->tx_mtx);
409 * Run through entries, placing them into
410 * iovecs and sending when an end-of-packet
413 pci_vtnet_proctx(sc, vq);
414 } while (vq_has_descs(vq));
417 * Generate an interrupt if needed.
419 vq_endchains(vq, /*used_all_avail=*/1);
421 pthread_mutex_lock(&sc->tx_mtx);
427 pci_vtnet_ping_ctlq(void *vsc, struct vqueue_info *vq)
430 DPRINTF(("vtnet: control qnotify!\n\r"));
435 pci_vtnet_init(struct vmctx *ctx, struct pci_devinst *pi, char *opts)
437 struct pci_vtnet_softc *sc;
438 char tname[MAXCOMLEN + 1];
442 * Allocate data structures for further virtio initializations.
443 * sc also contains a copy of vtnet_vi_consts, since capabilities
444 * change depending on the backend.
446 sc = calloc(1, sizeof(struct pci_vtnet_softc));
448 sc->vsc_consts = vtnet_vi_consts;
449 pthread_mutex_init(&sc->vsc_mtx, NULL);
451 sc->vsc_queues[VTNET_RXQ].vq_qsize = VTNET_RINGSZ;
452 sc->vsc_queues[VTNET_RXQ].vq_notify = pci_vtnet_ping_rxq;
453 sc->vsc_queues[VTNET_TXQ].vq_qsize = VTNET_RINGSZ;
454 sc->vsc_queues[VTNET_TXQ].vq_notify = pci_vtnet_ping_txq;
456 sc->vsc_queues[VTNET_CTLQ].vq_qsize = VTNET_RINGSZ;
457 sc->vsc_queues[VTNET_CTLQ].vq_notify = pci_vtnet_ping_ctlq;
461 * Attempt to open the backend device and read the MAC address
470 devname = vtopts = strdup(opts);
471 (void) strsep(&vtopts, ",");
473 if (vtopts != NULL) {
474 err = net_parsemac(vtopts, sc->vsc_config.mac);
483 err = netbe_init(&sc->vsc_be, devname, pci_vtnet_rx_callback,
490 sc->vsc_consts.vc_hv_caps |= netbe_get_cap(sc->vsc_be);
494 net_genmac(pi, sc->vsc_config.mac);
497 /* initialize config space */
498 pci_set_cfgdata16(pi, PCIR_DEVICE, VIRTIO_DEV_NET);
499 pci_set_cfgdata16(pi, PCIR_VENDOR, VIRTIO_VENDOR);
500 pci_set_cfgdata8(pi, PCIR_CLASS, PCIC_NETWORK);
501 pci_set_cfgdata16(pi, PCIR_SUBDEV_0, VIRTIO_TYPE_NET);
502 pci_set_cfgdata16(pi, PCIR_SUBVEND_0, VIRTIO_VENDOR);
504 /* Link is up if we managed to open backend device. */
505 sc->vsc_config.status = (opts == NULL || sc->vsc_be);
507 vi_softc_linkup(&sc->vsc_vs, &sc->vsc_consts, sc, pi, sc->vsc_queues);
508 sc->vsc_vs.vs_mtx = &sc->vsc_mtx;
510 /* use BAR 1 to map MSI-X table and PBA, if we're using MSI-X */
511 if (vi_intr_init(&sc->vsc_vs, 1, fbsdrun_virtio_msix())) {
516 /* use BAR 0 to map config regs in IO space */
517 vi_set_io_bar(&sc->vsc_vs, 0);
522 pthread_mutex_init(&sc->rx_mtx, NULL);
525 * Initialize tx semaphore & spawn TX processing thread.
526 * As of now, only one thread for TX desc processing is
529 sc->tx_in_progress = 0;
530 pthread_mutex_init(&sc->tx_mtx, NULL);
531 pthread_cond_init(&sc->tx_cond, NULL);
532 pthread_create(&sc->tx_tid, NULL, pci_vtnet_tx_thread, (void *)sc);
533 snprintf(tname, sizeof(tname), "vtnet-%d:%d tx", pi->pi_slot,
535 pthread_set_name_np(sc->tx_tid, tname);
541 pci_vtnet_cfgwrite(void *vsc, int offset, int size, uint32_t value)
543 struct pci_vtnet_softc *sc = vsc;
546 if (offset < (int)sizeof(sc->vsc_config.mac)) {
547 assert(offset + size <= (int)sizeof(sc->vsc_config.mac));
549 * The driver is allowed to change the MAC address
551 ptr = &sc->vsc_config.mac[offset];
552 memcpy(ptr, &value, size);
554 /* silently ignore other writes */
555 DPRINTF(("vtnet: write to readonly reg %d\n\r", offset));
562 pci_vtnet_cfgread(void *vsc, int offset, int size, uint32_t *retval)
564 struct pci_vtnet_softc *sc = vsc;
567 ptr = (uint8_t *)&sc->vsc_config + offset;
568 memcpy(retval, ptr, size);
573 pci_vtnet_neg_features(void *vsc, uint64_t negotiated_features)
575 struct pci_vtnet_softc *sc = vsc;
576 unsigned int rx_vhdrlen;
578 sc->vsc_features = negotiated_features;
580 if (negotiated_features & VIRTIO_NET_F_MRG_RXBUF) {
581 rx_vhdrlen = sizeof(struct virtio_net_rxhdr);
585 * Without mergeable rx buffers, virtio-net header is 2
586 * bytes shorter than sizeof(struct virtio_net_rxhdr).
588 rx_vhdrlen = sizeof(struct virtio_net_rxhdr) - 2;
592 /* Tell the backend to enable some capabilities it has advertised. */
593 netbe_set_cap(sc->vsc_be, negotiated_features, rx_vhdrlen);
596 static struct pci_devemu pci_de_vnet = {
597 .pe_emu = "virtio-net",
598 .pe_init = pci_vtnet_init,
599 .pe_barwrite = vi_pci_write,
600 .pe_barread = vi_pci_read
602 PCI_EMUL_SET(pci_de_vnet);