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>
75 #include "net_backends.h"
77 #include <sys/linker_set.h>
80 * Each network backend registers a set of function pointers that are
81 * used to implement the net backends API.
82 * This might need to be exposed if we implement backends in separate files.
85 const char *prefix; /* prefix matching this backend */
88 * Routines used to initialize and cleanup the resources needed
89 * by a backend. The cleanup function is used internally,
90 * and should not be called by the frontend.
92 int (*init)(struct net_backend *be, const char *devname,
93 net_be_rxeof_t cb, void *param);
94 void (*cleanup)(struct net_backend *be);
97 * Called to serve a guest transmit request. The scatter-gather
98 * vector provided by the caller has 'iovcnt' elements and contains
101 ssize_t (*send)(struct net_backend *be, struct iovec *iov, int iovcnt);
104 * Called to receive a packet from the backend. When the function
105 * returns a positive value 'len', the scatter-gather vector
106 * provided by the caller contains a packet with such length.
107 * The function returns 0 if the backend doesn't have a new packet to
110 ssize_t (*recv)(struct net_backend *be, struct iovec *iov, int iovcnt);
113 * Ask the backend for the virtio-net features it is able to
114 * support. Possible features are TSO, UFO and checksum offloading
115 * in both rx and tx direction and for both IPv4 and IPv6.
117 uint64_t (*get_cap)(struct net_backend *be);
120 * Tell the backend to enable/disable the specified virtio-net
121 * features (capabilities).
123 int (*set_cap)(struct net_backend *be, uint64_t features,
124 unsigned int vnet_hdr_len);
126 struct pci_vtnet_softc *sc;
130 * Length of the virtio-net header used by the backend and the
131 * frontend, respectively. A zero value means that the header
134 unsigned int be_vnet_hdr_len;
135 unsigned int fe_vnet_hdr_len;
137 /* Size of backend-specific private data. */
140 /* Room for backend-specific data. */
144 SET_DECLARE(net_backend_set, struct net_backend);
146 #define VNET_HDR_LEN sizeof(struct virtio_net_rxhdr)
148 #define WPRINTF(params) printf params
159 tap_cleanup(struct net_backend *be)
161 struct tap_priv *priv = (struct tap_priv *)be->opaque;
164 mevent_delete(priv->mevp);
173 tap_init(struct net_backend *be, const char *devname,
174 net_be_rxeof_t cb, void *param)
176 struct tap_priv *priv = (struct tap_priv *)be->opaque;
179 #ifndef WITHOUT_CAPSICUM
184 WPRINTF(("TAP backend requires non-NULL callback\n"));
188 strcpy(tbuf, "/dev/");
189 strlcat(tbuf, devname, sizeof(tbuf));
191 be->fd = open(tbuf, O_RDWR);
193 WPRINTF(("open of tap device %s failed\n", tbuf));
198 * Set non-blocking and register for read
199 * notifications with the event loop
201 if (ioctl(be->fd, FIONBIO, &opt) < 0) {
202 WPRINTF(("tap device O_NONBLOCK failed\n"));
206 #ifndef WITHOUT_CAPSICUM
207 cap_rights_init(&rights, CAP_EVENT, CAP_READ, CAP_WRITE);
208 if (caph_rights_limit(be->fd, &rights) == -1)
209 errx(EX_OSERR, "Unable to apply rights for sandbox");
212 priv->mevp = mevent_add(be->fd, EVF_READ, cb, param);
213 if (priv->mevp == NULL) {
214 WPRINTF(("Could not register event\n"));
226 * Called to send a buffer chain out to the tap device
229 tap_send(struct net_backend *be, struct iovec *iov, int iovcnt)
231 return (writev(be->fd, iov, iovcnt));
235 tap_recv(struct net_backend *be, struct iovec *iov, int iovcnt)
239 /* Should never be called without a valid tap fd */
240 assert(be->fd != -1);
242 ret = readv(be->fd, iov, iovcnt);
244 if (ret < 0 && errno == EWOULDBLOCK) {
252 tap_get_cap(struct net_backend *be)
255 return (0); /* no capabilities for now */
259 tap_set_cap(struct net_backend *be, uint64_t features,
260 unsigned vnet_hdr_len)
263 return ((features || vnet_hdr_len) ? -1 : 0);
266 static struct net_backend tap_backend = {
268 .priv_size = sizeof(struct tap_priv),
270 .cleanup = tap_cleanup,
273 .get_cap = tap_get_cap,
274 .set_cap = tap_set_cap,
277 /* A clone of the tap backend, with a different prefix. */
278 static struct net_backend vmnet_backend = {
280 .priv_size = sizeof(struct tap_priv),
282 .cleanup = tap_cleanup,
285 .get_cap = tap_get_cap,
286 .set_cap = tap_set_cap,
289 DATA_SET(net_backend_set, tap_backend);
290 DATA_SET(net_backend_set, vmnet_backend);
296 /* The virtio-net features supported by netmap. */
297 #define NETMAP_FEATURES (VIRTIO_NET_F_CSUM | VIRTIO_NET_F_HOST_TSO4 | \
298 VIRTIO_NET_F_HOST_TSO6 | VIRTIO_NET_F_HOST_UFO | \
299 VIRTIO_NET_F_GUEST_CSUM | VIRTIO_NET_F_GUEST_TSO4 | \
300 VIRTIO_NET_F_GUEST_TSO6 | VIRTIO_NET_F_GUEST_UFO)
303 char ifname[IFNAMSIZ];
306 struct netmap_ring *rx;
307 struct netmap_ring *tx;
314 nmreq_init(struct nmreq *req, char *ifname)
317 memset(req, 0, sizeof(*req));
318 strlcpy(req->nr_name, ifname, sizeof(req->nr_name));
319 req->nr_version = NETMAP_API;
323 netmap_set_vnet_hdr_len(struct net_backend *be, int vnet_hdr_len)
327 struct netmap_priv *priv = (struct netmap_priv *)be->opaque;
329 nmreq_init(&req, priv->ifname);
330 req.nr_cmd = NETMAP_BDG_VNET_HDR;
331 req.nr_arg1 = vnet_hdr_len;
332 err = ioctl(be->fd, NIOCREGIF, &req);
334 WPRINTF(("Unable to set vnet header length %d\n",
339 be->be_vnet_hdr_len = vnet_hdr_len;
345 netmap_has_vnet_hdr_len(struct net_backend *be, unsigned vnet_hdr_len)
347 int prev_hdr_len = be->be_vnet_hdr_len;
350 if (vnet_hdr_len == prev_hdr_len) {
354 ret = netmap_set_vnet_hdr_len(be, vnet_hdr_len);
359 netmap_set_vnet_hdr_len(be, prev_hdr_len);
365 netmap_get_cap(struct net_backend *be)
368 return (netmap_has_vnet_hdr_len(be, VNET_HDR_LEN) ?
369 NETMAP_FEATURES : 0);
373 netmap_set_cap(struct net_backend *be, uint64_t features,
374 unsigned vnet_hdr_len)
377 return (netmap_set_vnet_hdr_len(be, vnet_hdr_len));
381 netmap_init(struct net_backend *be, const char *devname,
382 net_be_rxeof_t cb, void *param)
384 struct netmap_priv *priv = (struct netmap_priv *)be->opaque;
386 strlcpy(priv->ifname, devname, sizeof(priv->ifname));
387 priv->ifname[sizeof(priv->ifname) - 1] = '\0';
389 priv->nmd = nm_open(priv->ifname, NULL, NETMAP_NO_TX_POLL, NULL);
390 if (priv->nmd == NULL) {
391 WPRINTF(("Unable to nm_open(): interface '%s', errno (%s)\n",
392 devname, strerror(errno)));
397 priv->memid = priv->nmd->req.nr_arg2;
398 priv->tx = NETMAP_TXRING(priv->nmd->nifp, 0);
399 priv->rx = NETMAP_RXRING(priv->nmd->nifp, 0);
401 priv->cb_param = param;
402 be->fd = priv->nmd->fd;
404 priv->mevp = mevent_add(be->fd, EVF_READ, cb, param);
405 if (priv->mevp == NULL) {
406 WPRINTF(("Could not register event\n"));
414 netmap_cleanup(struct net_backend *be)
416 struct netmap_priv *priv = (struct netmap_priv *)be->opaque;
419 mevent_delete(priv->mevp);
428 netmap_send(struct net_backend *be, struct iovec *iov,
431 struct netmap_priv *priv = (struct netmap_priv *)be->opaque;
432 struct netmap_ring *ring;
442 if (head == ring->tail) {
443 WPRINTF(("No space, drop %zu bytes\n", count_iov(iov, iovcnt)));
446 nm_buf = NETMAP_BUF(ring, ring->slot[head].buf_idx);
447 nm_buf_size = ring->nr_buf_size;
450 for (j = 0; j < iovcnt; j++) {
451 int iov_frag_size = iov[j].iov_len;
452 void *iov_frag_buf = iov[j].iov_base;
454 totlen += iov_frag_size;
457 * Split each iovec fragment over more netmap slots, if
463 copylen = iov_frag_size < nm_buf_size ? iov_frag_size : nm_buf_size;
464 memcpy(nm_buf, iov_frag_buf, copylen);
466 iov_frag_buf += copylen;
467 iov_frag_size -= copylen;
469 nm_buf_size -= copylen;
470 nm_buf_len += copylen;
472 if (iov_frag_size == 0) {
476 ring->slot[head].len = nm_buf_len;
477 ring->slot[head].flags = NS_MOREFRAG;
478 head = nm_ring_next(ring, head);
479 if (head == ring->tail) {
481 * We ran out of netmap slots while
482 * splitting the iovec fragments.
484 WPRINTF(("No space, drop %zu bytes\n",
485 count_iov(iov, iovcnt)));
488 nm_buf = NETMAP_BUF(ring, ring->slot[head].buf_idx);
489 nm_buf_size = ring->nr_buf_size;
494 /* Complete the last slot, which must not have NS_MOREFRAG set. */
495 ring->slot[head].len = nm_buf_len;
496 ring->slot[head].flags = 0;
497 head = nm_ring_next(ring, head);
499 /* Now update ring->head and ring->cur. */
500 ring->head = ring->cur = head;
502 ioctl(be->fd, NIOCTXSYNC, NULL);
508 netmap_recv(struct net_backend *be, struct iovec *iov, int iovcnt)
510 struct netmap_priv *priv = (struct netmap_priv *)be->opaque;
511 struct netmap_slot *slot = NULL;
512 struct netmap_ring *ring;
522 iov_frag_buf = iov->iov_base;
523 iov_frag_size = iov->iov_len;
529 if (head == ring->tail) {
533 slot = ring->slot + head;
534 nm_buf = NETMAP_BUF(ring, slot->buf_idx);
535 nm_buf_len = slot->len;
538 int copylen = nm_buf_len < iov_frag_size ?
539 nm_buf_len : iov_frag_size;
541 memcpy(iov_frag_buf, nm_buf, copylen);
543 nm_buf_len -= copylen;
544 iov_frag_buf += copylen;
545 iov_frag_size -= copylen;
548 if (nm_buf_len == 0) {
555 /* No space to receive. */
556 WPRINTF(("Short iov, drop %zd bytes\n",
560 iov_frag_buf = iov->iov_base;
561 iov_frag_size = iov->iov_len;
564 head = nm_ring_next(ring, head);
566 } while (slot->flags & NS_MOREFRAG);
568 /* Release slots to netmap. */
569 ring->head = ring->cur = head;
574 static struct net_backend netmap_backend = {
576 .priv_size = sizeof(struct netmap_priv),
578 .cleanup = netmap_cleanup,
581 .get_cap = netmap_get_cap,
582 .set_cap = netmap_set_cap,
585 /* A clone of the netmap backend, with a different prefix. */
586 static struct net_backend vale_backend = {
588 .priv_size = sizeof(struct netmap_priv),
590 .cleanup = netmap_cleanup,
593 .get_cap = netmap_get_cap,
594 .set_cap = netmap_set_cap,
597 DATA_SET(net_backend_set, netmap_backend);
598 DATA_SET(net_backend_set, vale_backend);
601 * Initialize a backend and attach to the frontend.
602 * This is called during frontend initialization.
603 * @pbe is a pointer to the backend to be initialized
604 * @devname is the backend-name as supplied on the command line,
605 * e.g. -s 2:0,frontend-name,backend-name[,other-args]
606 * @cb is the receive callback supplied by the frontend,
607 * and it is invoked in the event loop when a receive
608 * event is generated in the hypervisor,
609 * @param is a pointer to the frontend, and normally used as
610 * the argument for the callback.
613 netbe_init(struct net_backend **ret, const char *devname, net_be_rxeof_t cb,
616 struct net_backend **pbe, *nbe, *tbe = NULL;
620 * Find the network backend that matches the user-provided
621 * device name. net_backend_set is built using a linker set.
623 SET_FOREACH(pbe, net_backend_set) {
624 if (strncmp(devname, (*pbe)->prefix,
625 strlen((*pbe)->prefix)) == 0) {
627 assert(tbe->init != NULL);
628 assert(tbe->cleanup != NULL);
629 assert(tbe->send != NULL);
630 assert(tbe->recv != NULL);
631 assert(tbe->get_cap != NULL);
632 assert(tbe->set_cap != NULL);
640 nbe = calloc(1, sizeof(*nbe) + tbe->priv_size);
641 *nbe = *tbe; /* copy the template */
644 nbe->be_vnet_hdr_len = 0;
645 nbe->fe_vnet_hdr_len = 0;
647 /* Initialize the backend. */
648 err = nbe->init(nbe, devname, cb, param);
660 netbe_cleanup(struct net_backend *be)
670 netbe_get_cap(struct net_backend *be)
674 return (be->get_cap(be));
678 netbe_set_cap(struct net_backend *be, uint64_t features,
679 unsigned vnet_hdr_len)
685 /* There are only three valid lengths, i.e., 0, 10 and 12. */
686 if (vnet_hdr_len && vnet_hdr_len != VNET_HDR_LEN
687 && vnet_hdr_len != (VNET_HDR_LEN - sizeof(uint16_t)))
690 be->fe_vnet_hdr_len = vnet_hdr_len;
692 ret = be->set_cap(be, features, vnet_hdr_len);
693 assert(be->be_vnet_hdr_len == 0 ||
694 be->be_vnet_hdr_len == be->fe_vnet_hdr_len);
699 static __inline struct iovec *
700 iov_trim(struct iovec *iov, int *iovcnt, unsigned int tlen)
704 /* XXX short-cut: assume first segment is >= tlen */
705 assert(iov[0].iov_len >= tlen);
707 iov[0].iov_len -= tlen;
708 if (iov[0].iov_len == 0) {
713 iov[0].iov_base = (void *)((uintptr_t)iov[0].iov_base + tlen);
721 netbe_send(struct net_backend *be, struct iovec *iov, int iovcnt)
725 if (be->be_vnet_hdr_len != be->fe_vnet_hdr_len) {
727 * The frontend uses a virtio-net header, but the backend
728 * does not. We ignore it (as it must be all zeroes) and
731 assert(be->be_vnet_hdr_len == 0);
732 iov = iov_trim(iov, &iovcnt, be->fe_vnet_hdr_len);
735 return (be->send(be, iov, iovcnt));
739 * Try to read a packet from the backend, without blocking.
740 * If no packets are available, return 0. In case of success, return
741 * the length of the packet just read. Return -1 in case of errors.
744 netbe_recv(struct net_backend *be, struct iovec *iov, int iovcnt)
746 /* Length of prepended virtio-net header. */
747 unsigned int hlen = be->fe_vnet_hdr_len;
752 if (hlen && hlen != be->be_vnet_hdr_len) {
754 * The frontend uses a virtio-net header, but the backend
755 * does not. We need to prepend a zeroed header.
757 struct virtio_net_rxhdr *vh;
759 assert(be->be_vnet_hdr_len == 0);
762 * Get a pointer to the rx header, and use the
763 * data immediately following it for the packet buffer.
765 vh = iov[0].iov_base;
766 iov = iov_trim(iov, &iovcnt, hlen);
769 * The only valid field in the rx packet header is the
770 * number of buffers if merged rx bufs were negotiated.
773 if (hlen == VNET_HDR_LEN) {
778 ret = be->recv(be, iov, iovcnt);
787 * Read a packet from the backend and discard it.
788 * Returns the size of the discarded packet or zero if no packet was available.
789 * A negative error code is returned in case of read error.
792 netbe_rx_discard(struct net_backend *be)
795 * MP note: the dummybuf is only used to discard frames,
796 * so there is no need for it to be per-vtnet or locked.
797 * We only make it large enough for TSO-sized segment.
799 static uint8_t dummybuf[65536 + 64];
802 iov.iov_base = dummybuf;
803 iov.iov_len = sizeof(dummybuf);
805 return netbe_recv(be, &iov, 1);