2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2019 Vincenzo Maffione <vmaffione@FreeBSD.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS
19 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
20 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
21 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
22 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
24 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
25 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 * This file implements multiple network backends (tap, netmap, ...),
32 * to be used by network frontends such as virtio-net and e1000.
33 * The API to access the backend (e.g. send/receive packets, negotiate
34 * features) is exported by net_backends.h.
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
40 #include <sys/types.h> /* u_short etc */
41 #ifndef WITHOUT_CAPSICUM
42 #include <sys/capsicum.h>
44 #include <sys/ioctl.h>
49 #include <net/netmap.h>
50 #include <net/netmap_virt.h>
51 #define NETMAP_WITH_LIBS
52 #include <net/netmap_user.h>
54 #ifndef WITHOUT_CAPSICUM
55 #include <capsicum_helpers.h>
68 #include <pthread_np.h>
76 #include "net_backends.h"
78 #include <sys/linker_set.h>
81 * Each network backend registers a set of function pointers that are
82 * used to implement the net backends API.
83 * This might need to be exposed if we implement backends in separate files.
86 const char *prefix; /* prefix matching this backend */
89 * Routines used to initialize and cleanup the resources needed
90 * by a backend. The cleanup function is used internally,
91 * and should not be called by the frontend.
93 int (*init)(struct net_backend *be, const char *devname,
94 net_be_rxeof_t cb, void *param);
95 void (*cleanup)(struct net_backend *be);
98 * Called to serve a guest transmit request. The scatter-gather
99 * vector provided by the caller has 'iovcnt' elements and contains
100 * the packet to send.
102 ssize_t (*send)(struct net_backend *be, struct iovec *iov, int iovcnt);
105 * Called to receive a packet from the backend. When the function
106 * returns a positive value 'len', the scatter-gather vector
107 * provided by the caller contains a packet with such length.
108 * The function returns 0 if the backend doesn't have a new packet to
111 ssize_t (*recv)(struct net_backend *be, struct iovec *iov, int iovcnt);
114 * Ask the backend to enable or disable receive operation in the
115 * backend. On return from a disable operation, it is guaranteed
116 * that the receive callback won't be called until receive is
117 * enabled again. Note however that it is up to the caller to make
118 * sure that netbe_recv() is not currently being executed by another
121 void (*recv_enable)(struct net_backend *be);
122 void (*recv_disable)(struct net_backend *be);
125 * Ask the backend for the virtio-net features it is able to
126 * support. Possible features are TSO, UFO and checksum offloading
127 * in both rx and tx direction and for both IPv4 and IPv6.
129 uint64_t (*get_cap)(struct net_backend *be);
132 * Tell the backend to enable/disable the specified virtio-net
133 * features (capabilities).
135 int (*set_cap)(struct net_backend *be, uint64_t features,
136 unsigned int vnet_hdr_len);
138 struct pci_vtnet_softc *sc;
142 * Length of the virtio-net header used by the backend and the
143 * frontend, respectively. A zero value means that the header
146 unsigned int be_vnet_hdr_len;
147 unsigned int fe_vnet_hdr_len;
149 /* Size of backend-specific private data. */
152 /* Room for backend-specific data. */
156 SET_DECLARE(net_backend_set, struct net_backend);
158 #define VNET_HDR_LEN sizeof(struct virtio_net_rxhdr)
160 #define WPRINTF(params) PRINTLN params
171 tap_cleanup(struct net_backend *be)
173 struct tap_priv *priv = (struct tap_priv *)be->opaque;
176 mevent_delete(priv->mevp);
185 tap_init(struct net_backend *be, const char *devname,
186 net_be_rxeof_t cb, void *param)
188 struct tap_priv *priv = (struct tap_priv *)be->opaque;
191 #ifndef WITHOUT_CAPSICUM
196 WPRINTF(("TAP backend requires non-NULL callback"));
200 strcpy(tbuf, "/dev/");
201 strlcat(tbuf, devname, sizeof(tbuf));
203 be->fd = open(tbuf, O_RDWR);
205 WPRINTF(("open of tap device %s failed", tbuf));
210 * Set non-blocking and register for read
211 * notifications with the event loop
213 if (ioctl(be->fd, FIONBIO, &opt) < 0) {
214 WPRINTF(("tap device O_NONBLOCK failed"));
218 #ifndef WITHOUT_CAPSICUM
219 cap_rights_init(&rights, CAP_EVENT, CAP_READ, CAP_WRITE);
220 if (caph_rights_limit(be->fd, &rights) == -1)
221 errx(EX_OSERR, "Unable to apply rights for sandbox");
224 priv->mevp = mevent_add_disabled(be->fd, EVF_READ, cb, param);
225 if (priv->mevp == NULL) {
226 WPRINTF(("Could not register event"));
238 * Called to send a buffer chain out to the tap device
241 tap_send(struct net_backend *be, struct iovec *iov, int iovcnt)
243 return (writev(be->fd, iov, iovcnt));
247 tap_recv(struct net_backend *be, struct iovec *iov, int iovcnt)
251 /* Should never be called without a valid tap fd */
252 assert(be->fd != -1);
254 ret = readv(be->fd, iov, iovcnt);
256 if (ret < 0 && errno == EWOULDBLOCK) {
264 tap_recv_enable(struct net_backend *be)
266 struct tap_priv *priv = (struct tap_priv *)be->opaque;
268 mevent_enable(priv->mevp);
272 tap_recv_disable(struct net_backend *be)
274 struct tap_priv *priv = (struct tap_priv *)be->opaque;
276 mevent_disable(priv->mevp);
280 tap_get_cap(struct net_backend *be)
283 return (0); /* no capabilities for now */
287 tap_set_cap(struct net_backend *be, uint64_t features,
288 unsigned vnet_hdr_len)
291 return ((features || vnet_hdr_len) ? -1 : 0);
294 static struct net_backend tap_backend = {
296 .priv_size = sizeof(struct tap_priv),
298 .cleanup = tap_cleanup,
301 .recv_enable = tap_recv_enable,
302 .recv_disable = tap_recv_disable,
303 .get_cap = tap_get_cap,
304 .set_cap = tap_set_cap,
307 /* A clone of the tap backend, with a different prefix. */
308 static struct net_backend vmnet_backend = {
310 .priv_size = sizeof(struct tap_priv),
312 .cleanup = tap_cleanup,
315 .recv_enable = tap_recv_enable,
316 .recv_disable = tap_recv_disable,
317 .get_cap = tap_get_cap,
318 .set_cap = tap_set_cap,
321 DATA_SET(net_backend_set, tap_backend);
322 DATA_SET(net_backend_set, vmnet_backend);
328 /* The virtio-net features supported by netmap. */
329 #define NETMAP_FEATURES (VIRTIO_NET_F_CSUM | VIRTIO_NET_F_HOST_TSO4 | \
330 VIRTIO_NET_F_HOST_TSO6 | VIRTIO_NET_F_HOST_UFO | \
331 VIRTIO_NET_F_GUEST_CSUM | VIRTIO_NET_F_GUEST_TSO4 | \
332 VIRTIO_NET_F_GUEST_TSO6 | VIRTIO_NET_F_GUEST_UFO | \
333 VIRTIO_NET_F_MRG_RXBUF)
336 char ifname[IFNAMSIZ];
339 struct netmap_ring *rx;
340 struct netmap_ring *tx;
347 nmreq_init(struct nmreq *req, char *ifname)
350 memset(req, 0, sizeof(*req));
351 strlcpy(req->nr_name, ifname, sizeof(req->nr_name));
352 req->nr_version = NETMAP_API;
356 netmap_set_vnet_hdr_len(struct net_backend *be, int vnet_hdr_len)
360 struct netmap_priv *priv = (struct netmap_priv *)be->opaque;
362 nmreq_init(&req, priv->ifname);
363 req.nr_cmd = NETMAP_BDG_VNET_HDR;
364 req.nr_arg1 = vnet_hdr_len;
365 err = ioctl(be->fd, NIOCREGIF, &req);
367 WPRINTF(("Unable to set vnet header length %d",
372 be->be_vnet_hdr_len = vnet_hdr_len;
378 netmap_has_vnet_hdr_len(struct net_backend *be, unsigned vnet_hdr_len)
380 int prev_hdr_len = be->be_vnet_hdr_len;
383 if (vnet_hdr_len == prev_hdr_len) {
387 ret = netmap_set_vnet_hdr_len(be, vnet_hdr_len);
392 netmap_set_vnet_hdr_len(be, prev_hdr_len);
398 netmap_get_cap(struct net_backend *be)
401 return (netmap_has_vnet_hdr_len(be, VNET_HDR_LEN) ?
402 NETMAP_FEATURES : 0);
406 netmap_set_cap(struct net_backend *be, uint64_t features,
407 unsigned vnet_hdr_len)
410 return (netmap_set_vnet_hdr_len(be, vnet_hdr_len));
414 netmap_init(struct net_backend *be, const char *devname,
415 net_be_rxeof_t cb, void *param)
417 struct netmap_priv *priv = (struct netmap_priv *)be->opaque;
419 strlcpy(priv->ifname, devname, sizeof(priv->ifname));
420 priv->ifname[sizeof(priv->ifname) - 1] = '\0';
422 priv->nmd = nm_open(priv->ifname, NULL, NETMAP_NO_TX_POLL, NULL);
423 if (priv->nmd == NULL) {
424 WPRINTF(("Unable to nm_open(): interface '%s', errno (%s)",
425 devname, strerror(errno)));
430 priv->memid = priv->nmd->req.nr_arg2;
431 priv->tx = NETMAP_TXRING(priv->nmd->nifp, 0);
432 priv->rx = NETMAP_RXRING(priv->nmd->nifp, 0);
434 priv->cb_param = param;
435 be->fd = priv->nmd->fd;
437 priv->mevp = mevent_add_disabled(be->fd, EVF_READ, cb, param);
438 if (priv->mevp == NULL) {
439 WPRINTF(("Could not register event"));
447 netmap_cleanup(struct net_backend *be)
449 struct netmap_priv *priv = (struct netmap_priv *)be->opaque;
452 mevent_delete(priv->mevp);
461 netmap_send(struct net_backend *be, struct iovec *iov,
464 struct netmap_priv *priv = (struct netmap_priv *)be->opaque;
465 struct netmap_ring *ring;
475 if (head == ring->tail) {
476 WPRINTF(("No space, drop %zu bytes", count_iov(iov, iovcnt)));
479 nm_buf = NETMAP_BUF(ring, ring->slot[head].buf_idx);
480 nm_buf_size = ring->nr_buf_size;
483 for (j = 0; j < iovcnt; j++) {
484 int iov_frag_size = iov[j].iov_len;
485 void *iov_frag_buf = iov[j].iov_base;
487 totlen += iov_frag_size;
490 * Split each iovec fragment over more netmap slots, if
496 copylen = iov_frag_size < nm_buf_size ? iov_frag_size : nm_buf_size;
497 memcpy(nm_buf, iov_frag_buf, copylen);
499 iov_frag_buf += copylen;
500 iov_frag_size -= copylen;
502 nm_buf_size -= copylen;
503 nm_buf_len += copylen;
505 if (iov_frag_size == 0) {
509 ring->slot[head].len = nm_buf_len;
510 ring->slot[head].flags = NS_MOREFRAG;
511 head = nm_ring_next(ring, head);
512 if (head == ring->tail) {
514 * We ran out of netmap slots while
515 * splitting the iovec fragments.
517 WPRINTF(("No space, drop %zu bytes",
518 count_iov(iov, iovcnt)));
521 nm_buf = NETMAP_BUF(ring, ring->slot[head].buf_idx);
522 nm_buf_size = ring->nr_buf_size;
527 /* Complete the last slot, which must not have NS_MOREFRAG set. */
528 ring->slot[head].len = nm_buf_len;
529 ring->slot[head].flags = 0;
530 head = nm_ring_next(ring, head);
532 /* Now update ring->head and ring->cur. */
533 ring->head = ring->cur = head;
535 ioctl(be->fd, NIOCTXSYNC, NULL);
541 netmap_recv(struct net_backend *be, struct iovec *iov, int iovcnt)
543 struct netmap_priv *priv = (struct netmap_priv *)be->opaque;
544 struct netmap_slot *slot = NULL;
545 struct netmap_ring *ring;
555 iov_frag_buf = iov->iov_base;
556 iov_frag_size = iov->iov_len;
562 if (head == ring->tail) {
566 slot = ring->slot + head;
567 nm_buf = NETMAP_BUF(ring, slot->buf_idx);
568 nm_buf_len = slot->len;
571 int copylen = nm_buf_len < iov_frag_size ?
572 nm_buf_len : iov_frag_size;
574 memcpy(iov_frag_buf, nm_buf, copylen);
576 nm_buf_len -= copylen;
577 iov_frag_buf += copylen;
578 iov_frag_size -= copylen;
581 if (nm_buf_len == 0) {
588 /* No space to receive. */
589 WPRINTF(("Short iov, drop %zd bytes",
593 iov_frag_buf = iov->iov_base;
594 iov_frag_size = iov->iov_len;
597 head = nm_ring_next(ring, head);
599 } while (slot->flags & NS_MOREFRAG);
601 /* Release slots to netmap. */
602 ring->head = ring->cur = head;
608 netmap_recv_enable(struct net_backend *be)
610 struct netmap_priv *priv = (struct netmap_priv *)be->opaque;
612 mevent_enable(priv->mevp);
616 netmap_recv_disable(struct net_backend *be)
618 struct netmap_priv *priv = (struct netmap_priv *)be->opaque;
620 mevent_disable(priv->mevp);
623 static struct net_backend netmap_backend = {
625 .priv_size = sizeof(struct netmap_priv),
627 .cleanup = netmap_cleanup,
630 .recv_enable = netmap_recv_enable,
631 .recv_disable = netmap_recv_disable,
632 .get_cap = netmap_get_cap,
633 .set_cap = netmap_set_cap,
636 /* A clone of the netmap backend, with a different prefix. */
637 static struct net_backend vale_backend = {
639 .priv_size = sizeof(struct netmap_priv),
641 .cleanup = netmap_cleanup,
644 .recv_enable = netmap_recv_enable,
645 .recv_disable = netmap_recv_disable,
646 .get_cap = netmap_get_cap,
647 .set_cap = netmap_set_cap,
650 DATA_SET(net_backend_set, netmap_backend);
651 DATA_SET(net_backend_set, vale_backend);
654 * Initialize a backend and attach to the frontend.
655 * This is called during frontend initialization.
656 * @pbe is a pointer to the backend to be initialized
657 * @devname is the backend-name as supplied on the command line,
658 * e.g. -s 2:0,frontend-name,backend-name[,other-args]
659 * @cb is the receive callback supplied by the frontend,
660 * and it is invoked in the event loop when a receive
661 * event is generated in the hypervisor,
662 * @param is a pointer to the frontend, and normally used as
663 * the argument for the callback.
666 netbe_init(struct net_backend **ret, const char *devname, net_be_rxeof_t cb,
669 struct net_backend **pbe, *nbe, *tbe = NULL;
673 * Find the network backend that matches the user-provided
674 * device name. net_backend_set is built using a linker set.
676 SET_FOREACH(pbe, net_backend_set) {
677 if (strncmp(devname, (*pbe)->prefix,
678 strlen((*pbe)->prefix)) == 0) {
680 assert(tbe->init != NULL);
681 assert(tbe->cleanup != NULL);
682 assert(tbe->send != NULL);
683 assert(tbe->recv != NULL);
684 assert(tbe->get_cap != NULL);
685 assert(tbe->set_cap != NULL);
693 nbe = calloc(1, sizeof(*nbe) + tbe->priv_size);
694 *nbe = *tbe; /* copy the template */
697 nbe->be_vnet_hdr_len = 0;
698 nbe->fe_vnet_hdr_len = 0;
700 /* Initialize the backend. */
701 err = nbe->init(nbe, devname, cb, param);
713 netbe_cleanup(struct net_backend *be)
723 netbe_get_cap(struct net_backend *be)
727 return (be->get_cap(be));
731 netbe_set_cap(struct net_backend *be, uint64_t features,
732 unsigned vnet_hdr_len)
738 /* There are only three valid lengths, i.e., 0, 10 and 12. */
739 if (vnet_hdr_len && vnet_hdr_len != VNET_HDR_LEN
740 && vnet_hdr_len != (VNET_HDR_LEN - sizeof(uint16_t)))
743 be->fe_vnet_hdr_len = vnet_hdr_len;
745 ret = be->set_cap(be, features, vnet_hdr_len);
746 assert(be->be_vnet_hdr_len == 0 ||
747 be->be_vnet_hdr_len == be->fe_vnet_hdr_len);
752 static __inline struct iovec *
753 iov_trim(struct iovec *iov, int *iovcnt, unsigned int tlen)
757 /* XXX short-cut: assume first segment is >= tlen */
758 assert(iov[0].iov_len >= tlen);
760 iov[0].iov_len -= tlen;
761 if (iov[0].iov_len == 0) {
766 iov[0].iov_base = (void *)((uintptr_t)iov[0].iov_base + tlen);
774 netbe_send(struct net_backend *be, struct iovec *iov, int iovcnt)
778 if (be->be_vnet_hdr_len != be->fe_vnet_hdr_len) {
780 * The frontend uses a virtio-net header, but the backend
781 * does not. We ignore it (as it must be all zeroes) and
784 assert(be->be_vnet_hdr_len == 0);
785 iov = iov_trim(iov, &iovcnt, be->fe_vnet_hdr_len);
788 return (be->send(be, iov, iovcnt));
792 * Try to read a packet from the backend, without blocking.
793 * If no packets are available, return 0. In case of success, return
794 * the length of the packet just read. Return -1 in case of errors.
797 netbe_recv(struct net_backend *be, struct iovec *iov, int iovcnt)
799 /* Length of prepended virtio-net header. */
800 unsigned int hlen = be->fe_vnet_hdr_len;
805 if (hlen && hlen != be->be_vnet_hdr_len) {
807 * The frontend uses a virtio-net header, but the backend
808 * does not. We need to prepend a zeroed header.
810 struct virtio_net_rxhdr *vh;
812 assert(be->be_vnet_hdr_len == 0);
815 * Get a pointer to the rx header, and use the
816 * data immediately following it for the packet buffer.
818 vh = iov[0].iov_base;
819 iov = iov_trim(iov, &iovcnt, hlen);
822 * The only valid field in the rx packet header is the
823 * number of buffers if merged rx bufs were negotiated.
826 if (hlen == VNET_HDR_LEN) {
831 ret = be->recv(be, iov, iovcnt);
840 * Read a packet from the backend and discard it.
841 * Returns the size of the discarded packet or zero if no packet was available.
842 * A negative error code is returned in case of read error.
845 netbe_rx_discard(struct net_backend *be)
848 * MP note: the dummybuf is only used to discard frames,
849 * so there is no need for it to be per-vtnet or locked.
850 * We only make it large enough for TSO-sized segment.
852 static uint8_t dummybuf[65536 + 64];
855 iov.iov_base = dummybuf;
856 iov.iov_len = sizeof(dummybuf);
858 return netbe_recv(be, &iov, 1);
862 netbe_rx_disable(struct net_backend *be)
865 return be->recv_disable(be);
869 netbe_rx_enable(struct net_backend *be)
872 return be->recv_enable(be);