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, const struct iovec *iov,
106 * Called to receive a packet from the backend. When the function
107 * returns a positive value 'len', the scatter-gather vector
108 * provided by the caller contains a packet with such length.
109 * The function returns 0 if the backend doesn't have a new packet to
112 ssize_t (*recv)(struct net_backend *be, const struct iovec *iov,
116 * Ask the backend to enable or disable receive operation in the
117 * backend. On return from a disable operation, it is guaranteed
118 * that the receive callback won't be called until receive is
119 * enabled again. Note however that it is up to the caller to make
120 * sure that netbe_recv() is not currently being executed by another
123 void (*recv_enable)(struct net_backend *be);
124 void (*recv_disable)(struct net_backend *be);
127 * Ask the backend for the virtio-net features it is able to
128 * support. Possible features are TSO, UFO and checksum offloading
129 * in both rx and tx direction and for both IPv4 and IPv6.
131 uint64_t (*get_cap)(struct net_backend *be);
134 * Tell the backend to enable/disable the specified virtio-net
135 * features (capabilities).
137 int (*set_cap)(struct net_backend *be, uint64_t features,
138 unsigned int vnet_hdr_len);
140 struct pci_vtnet_softc *sc;
144 * Length of the virtio-net header used by the backend and the
145 * frontend, respectively. A zero value means that the header
148 unsigned int be_vnet_hdr_len;
149 unsigned int fe_vnet_hdr_len;
151 /* Size of backend-specific private data. */
154 /* Room for backend-specific data. */
158 SET_DECLARE(net_backend_set, struct net_backend);
160 #define VNET_HDR_LEN sizeof(struct virtio_net_rxhdr)
162 #define WPRINTF(params) PRINTLN params
173 tap_cleanup(struct net_backend *be)
175 struct tap_priv *priv = (struct tap_priv *)be->opaque;
178 mevent_delete(priv->mevp);
187 tap_init(struct net_backend *be, const char *devname,
188 net_be_rxeof_t cb, void *param)
190 struct tap_priv *priv = (struct tap_priv *)be->opaque;
193 #ifndef WITHOUT_CAPSICUM
198 WPRINTF(("TAP backend requires non-NULL callback"));
202 strcpy(tbuf, "/dev/");
203 strlcat(tbuf, devname, sizeof(tbuf));
205 be->fd = open(tbuf, O_RDWR);
207 WPRINTF(("open of tap device %s failed", tbuf));
212 * Set non-blocking and register for read
213 * notifications with the event loop
215 if (ioctl(be->fd, FIONBIO, &opt) < 0) {
216 WPRINTF(("tap device O_NONBLOCK failed"));
220 #ifndef WITHOUT_CAPSICUM
221 cap_rights_init(&rights, CAP_EVENT, CAP_READ, CAP_WRITE);
222 if (caph_rights_limit(be->fd, &rights) == -1)
223 errx(EX_OSERR, "Unable to apply rights for sandbox");
226 priv->mevp = mevent_add_disabled(be->fd, EVF_READ, cb, param);
227 if (priv->mevp == NULL) {
228 WPRINTF(("Could not register event"));
240 * Called to send a buffer chain out to the tap device
243 tap_send(struct net_backend *be, const struct iovec *iov, int iovcnt)
245 return (writev(be->fd, iov, iovcnt));
249 tap_recv(struct net_backend *be, const struct iovec *iov, int iovcnt)
253 /* Should never be called without a valid tap fd */
254 assert(be->fd != -1);
256 ret = readv(be->fd, iov, iovcnt);
258 if (ret < 0 && errno == EWOULDBLOCK) {
266 tap_recv_enable(struct net_backend *be)
268 struct tap_priv *priv = (struct tap_priv *)be->opaque;
270 mevent_enable(priv->mevp);
274 tap_recv_disable(struct net_backend *be)
276 struct tap_priv *priv = (struct tap_priv *)be->opaque;
278 mevent_disable(priv->mevp);
282 tap_get_cap(struct net_backend *be)
285 return (0); /* no capabilities for now */
289 tap_set_cap(struct net_backend *be, uint64_t features,
290 unsigned vnet_hdr_len)
293 return ((features || vnet_hdr_len) ? -1 : 0);
296 static struct net_backend tap_backend = {
298 .priv_size = sizeof(struct tap_priv),
300 .cleanup = tap_cleanup,
303 .recv_enable = tap_recv_enable,
304 .recv_disable = tap_recv_disable,
305 .get_cap = tap_get_cap,
306 .set_cap = tap_set_cap,
309 /* A clone of the tap backend, with a different prefix. */
310 static struct net_backend vmnet_backend = {
312 .priv_size = sizeof(struct tap_priv),
314 .cleanup = tap_cleanup,
317 .recv_enable = tap_recv_enable,
318 .recv_disable = tap_recv_disable,
319 .get_cap = tap_get_cap,
320 .set_cap = tap_set_cap,
323 DATA_SET(net_backend_set, tap_backend);
324 DATA_SET(net_backend_set, vmnet_backend);
330 /* The virtio-net features supported by netmap. */
331 #define NETMAP_FEATURES (VIRTIO_NET_F_CSUM | VIRTIO_NET_F_HOST_TSO4 | \
332 VIRTIO_NET_F_HOST_TSO6 | VIRTIO_NET_F_HOST_UFO | \
333 VIRTIO_NET_F_GUEST_CSUM | VIRTIO_NET_F_GUEST_TSO4 | \
334 VIRTIO_NET_F_GUEST_TSO6 | VIRTIO_NET_F_GUEST_UFO | \
335 VIRTIO_NET_F_MRG_RXBUF)
338 char ifname[IFNAMSIZ];
341 struct netmap_ring *rx;
342 struct netmap_ring *tx;
349 nmreq_init(struct nmreq *req, char *ifname)
352 memset(req, 0, sizeof(*req));
353 strlcpy(req->nr_name, ifname, sizeof(req->nr_name));
354 req->nr_version = NETMAP_API;
358 netmap_set_vnet_hdr_len(struct net_backend *be, int vnet_hdr_len)
362 struct netmap_priv *priv = (struct netmap_priv *)be->opaque;
364 nmreq_init(&req, priv->ifname);
365 req.nr_cmd = NETMAP_BDG_VNET_HDR;
366 req.nr_arg1 = vnet_hdr_len;
367 err = ioctl(be->fd, NIOCREGIF, &req);
369 WPRINTF(("Unable to set vnet header length %d",
374 be->be_vnet_hdr_len = vnet_hdr_len;
380 netmap_has_vnet_hdr_len(struct net_backend *be, unsigned vnet_hdr_len)
382 int prev_hdr_len = be->be_vnet_hdr_len;
385 if (vnet_hdr_len == prev_hdr_len) {
389 ret = netmap_set_vnet_hdr_len(be, vnet_hdr_len);
394 netmap_set_vnet_hdr_len(be, prev_hdr_len);
400 netmap_get_cap(struct net_backend *be)
403 return (netmap_has_vnet_hdr_len(be, VNET_HDR_LEN) ?
404 NETMAP_FEATURES : 0);
408 netmap_set_cap(struct net_backend *be, uint64_t features,
409 unsigned vnet_hdr_len)
412 return (netmap_set_vnet_hdr_len(be, vnet_hdr_len));
416 netmap_init(struct net_backend *be, const char *devname,
417 net_be_rxeof_t cb, void *param)
419 struct netmap_priv *priv = (struct netmap_priv *)be->opaque;
421 strlcpy(priv->ifname, devname, sizeof(priv->ifname));
422 priv->ifname[sizeof(priv->ifname) - 1] = '\0';
424 priv->nmd = nm_open(priv->ifname, NULL, NETMAP_NO_TX_POLL, NULL);
425 if (priv->nmd == NULL) {
426 WPRINTF(("Unable to nm_open(): interface '%s', errno (%s)",
427 devname, strerror(errno)));
432 priv->memid = priv->nmd->req.nr_arg2;
433 priv->tx = NETMAP_TXRING(priv->nmd->nifp, 0);
434 priv->rx = NETMAP_RXRING(priv->nmd->nifp, 0);
436 priv->cb_param = param;
437 be->fd = priv->nmd->fd;
439 priv->mevp = mevent_add_disabled(be->fd, EVF_READ, cb, param);
440 if (priv->mevp == NULL) {
441 WPRINTF(("Could not register event"));
449 netmap_cleanup(struct net_backend *be)
451 struct netmap_priv *priv = (struct netmap_priv *)be->opaque;
454 mevent_delete(priv->mevp);
463 netmap_send(struct net_backend *be, const struct iovec *iov,
466 struct netmap_priv *priv = (struct netmap_priv *)be->opaque;
467 struct netmap_ring *ring;
477 if (head == ring->tail) {
478 WPRINTF(("No space, drop %zu bytes", count_iov(iov, iovcnt)));
481 nm_buf = NETMAP_BUF(ring, ring->slot[head].buf_idx);
482 nm_buf_size = ring->nr_buf_size;
485 for (j = 0; j < iovcnt; j++) {
486 int iov_frag_size = iov[j].iov_len;
487 void *iov_frag_buf = iov[j].iov_base;
489 totlen += iov_frag_size;
492 * Split each iovec fragment over more netmap slots, if
498 copylen = iov_frag_size < nm_buf_size ? iov_frag_size : nm_buf_size;
499 memcpy(nm_buf, iov_frag_buf, copylen);
501 iov_frag_buf += copylen;
502 iov_frag_size -= copylen;
504 nm_buf_size -= copylen;
505 nm_buf_len += copylen;
507 if (iov_frag_size == 0) {
511 ring->slot[head].len = nm_buf_len;
512 ring->slot[head].flags = NS_MOREFRAG;
513 head = nm_ring_next(ring, head);
514 if (head == ring->tail) {
516 * We ran out of netmap slots while
517 * splitting the iovec fragments.
519 WPRINTF(("No space, drop %zu bytes",
520 count_iov(iov, iovcnt)));
523 nm_buf = NETMAP_BUF(ring, ring->slot[head].buf_idx);
524 nm_buf_size = ring->nr_buf_size;
529 /* Complete the last slot, which must not have NS_MOREFRAG set. */
530 ring->slot[head].len = nm_buf_len;
531 ring->slot[head].flags = 0;
532 head = nm_ring_next(ring, head);
534 /* Now update ring->head and ring->cur. */
535 ring->head = ring->cur = head;
537 ioctl(be->fd, NIOCTXSYNC, NULL);
543 netmap_recv(struct net_backend *be, const struct iovec *iov, int iovcnt)
545 struct netmap_priv *priv = (struct netmap_priv *)be->opaque;
546 struct netmap_slot *slot = NULL;
547 struct netmap_ring *ring;
557 iov_frag_buf = iov->iov_base;
558 iov_frag_size = iov->iov_len;
564 if (head == ring->tail) {
568 slot = ring->slot + head;
569 nm_buf = NETMAP_BUF(ring, slot->buf_idx);
570 nm_buf_len = slot->len;
573 int copylen = nm_buf_len < iov_frag_size ?
574 nm_buf_len : iov_frag_size;
576 memcpy(iov_frag_buf, nm_buf, copylen);
578 nm_buf_len -= copylen;
579 iov_frag_buf += copylen;
580 iov_frag_size -= copylen;
583 if (nm_buf_len == 0) {
590 /* No space to receive. */
591 WPRINTF(("Short iov, drop %zd bytes",
595 iov_frag_buf = iov->iov_base;
596 iov_frag_size = iov->iov_len;
599 head = nm_ring_next(ring, head);
601 } while (slot->flags & NS_MOREFRAG);
603 /* Release slots to netmap. */
604 ring->head = ring->cur = head;
610 netmap_recv_enable(struct net_backend *be)
612 struct netmap_priv *priv = (struct netmap_priv *)be->opaque;
614 mevent_enable(priv->mevp);
618 netmap_recv_disable(struct net_backend *be)
620 struct netmap_priv *priv = (struct netmap_priv *)be->opaque;
622 mevent_disable(priv->mevp);
625 static struct net_backend netmap_backend = {
627 .priv_size = sizeof(struct netmap_priv),
629 .cleanup = netmap_cleanup,
632 .recv_enable = netmap_recv_enable,
633 .recv_disable = netmap_recv_disable,
634 .get_cap = netmap_get_cap,
635 .set_cap = netmap_set_cap,
638 /* A clone of the netmap backend, with a different prefix. */
639 static struct net_backend vale_backend = {
641 .priv_size = sizeof(struct netmap_priv),
643 .cleanup = netmap_cleanup,
646 .recv_enable = netmap_recv_enable,
647 .recv_disable = netmap_recv_disable,
648 .get_cap = netmap_get_cap,
649 .set_cap = netmap_set_cap,
652 DATA_SET(net_backend_set, netmap_backend);
653 DATA_SET(net_backend_set, vale_backend);
656 * Initialize a backend and attach to the frontend.
657 * This is called during frontend initialization.
658 * @pbe is a pointer to the backend to be initialized
659 * @devname is the backend-name as supplied on the command line,
660 * e.g. -s 2:0,frontend-name,backend-name[,other-args]
661 * @cb is the receive callback supplied by the frontend,
662 * and it is invoked in the event loop when a receive
663 * event is generated in the hypervisor,
664 * @param is a pointer to the frontend, and normally used as
665 * the argument for the callback.
668 netbe_init(struct net_backend **ret, const char *devname, net_be_rxeof_t cb,
671 struct net_backend **pbe, *nbe, *tbe = NULL;
675 * Find the network backend that matches the user-provided
676 * device name. net_backend_set is built using a linker set.
678 SET_FOREACH(pbe, net_backend_set) {
679 if (strncmp(devname, (*pbe)->prefix,
680 strlen((*pbe)->prefix)) == 0) {
682 assert(tbe->init != NULL);
683 assert(tbe->cleanup != NULL);
684 assert(tbe->send != NULL);
685 assert(tbe->recv != NULL);
686 assert(tbe->get_cap != NULL);
687 assert(tbe->set_cap != NULL);
695 nbe = calloc(1, sizeof(*nbe) + tbe->priv_size);
696 *nbe = *tbe; /* copy the template */
699 nbe->be_vnet_hdr_len = 0;
700 nbe->fe_vnet_hdr_len = 0;
702 /* Initialize the backend. */
703 err = nbe->init(nbe, devname, cb, param);
715 netbe_cleanup(struct net_backend *be)
725 netbe_get_cap(struct net_backend *be)
729 return (be->get_cap(be));
733 netbe_set_cap(struct net_backend *be, uint64_t features,
734 unsigned vnet_hdr_len)
740 /* There are only three valid lengths, i.e., 0, 10 and 12. */
741 if (vnet_hdr_len && vnet_hdr_len != VNET_HDR_LEN
742 && vnet_hdr_len != (VNET_HDR_LEN - sizeof(uint16_t)))
745 be->fe_vnet_hdr_len = vnet_hdr_len;
747 ret = be->set_cap(be, features, vnet_hdr_len);
748 assert(be->be_vnet_hdr_len == 0 ||
749 be->be_vnet_hdr_len == be->fe_vnet_hdr_len);
755 netbe_send(struct net_backend *be, const struct iovec *iov, int iovcnt)
758 return (be->send(be, iov, iovcnt));
762 * Try to read a packet from the backend, without blocking.
763 * If no packets are available, return 0. In case of success, return
764 * the length of the packet just read. Return -1 in case of errors.
767 netbe_recv(struct net_backend *be, const struct iovec *iov, int iovcnt)
770 return (be->recv(be, iov, iovcnt));
774 * Read a packet from the backend and discard it.
775 * Returns the size of the discarded packet or zero if no packet was available.
776 * A negative error code is returned in case of read error.
779 netbe_rx_discard(struct net_backend *be)
782 * MP note: the dummybuf is only used to discard frames,
783 * so there is no need for it to be per-vtnet or locked.
784 * We only make it large enough for TSO-sized segment.
786 static uint8_t dummybuf[65536 + 64];
789 iov.iov_base = dummybuf;
790 iov.iov_len = sizeof(dummybuf);
792 return netbe_recv(be, &iov, 1);
796 netbe_rx_disable(struct net_backend *be)
799 return be->recv_disable(be);
803 netbe_rx_enable(struct net_backend *be)
806 return be->recv_enable(be);
810 netbe_get_vnet_hdr_len(struct net_backend *be)
813 return (be->be_vnet_hdr_len);