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 #if defined(INET6) || defined(INET)
50 #include <net/if_tap.h>
52 #include <net/netmap.h>
53 #include <net/netmap_virt.h>
54 #define NETMAP_WITH_LIBS
55 #include <net/netmap_user.h>
57 #ifndef WITHOUT_CAPSICUM
58 #include <capsicum_helpers.h>
71 #include <pthread_np.h>
76 #include <sys/param.h>
77 #include <sys/sysctl.h>
85 #include "net_backends.h"
88 #include <sys/linker_set.h>
91 * Each network backend registers a set of function pointers that are
92 * used to implement the net backends API.
93 * This might need to be exposed if we implement backends in separate files.
96 const char *prefix; /* prefix matching this backend */
99 * Routines used to initialize and cleanup the resources needed
100 * by a backend. The cleanup function is used internally,
101 * and should not be called by the frontend.
103 int (*init)(struct net_backend *be, const char *devname,
104 nvlist_t *nvl, net_be_rxeof_t cb, void *param);
105 void (*cleanup)(struct net_backend *be);
108 * Called to serve a guest transmit request. The scatter-gather
109 * vector provided by the caller has 'iovcnt' elements and contains
110 * the packet to send.
112 ssize_t (*send)(struct net_backend *be, const struct iovec *iov,
116 * Get the length of the next packet that can be received from
117 * the backend. If no packets are currently available, this
118 * function returns 0.
120 ssize_t (*peek_recvlen)(struct net_backend *be);
123 * Called to receive a packet from the backend. When the function
124 * returns a positive value 'len', the scatter-gather vector
125 * provided by the caller contains a packet with such length.
126 * The function returns 0 if the backend doesn't have a new packet to
129 ssize_t (*recv)(struct net_backend *be, const struct iovec *iov,
133 * Ask the backend to enable or disable receive operation in the
134 * backend. On return from a disable operation, it is guaranteed
135 * that the receive callback won't be called until receive is
136 * enabled again. Note however that it is up to the caller to make
137 * sure that netbe_recv() is not currently being executed by another
140 void (*recv_enable)(struct net_backend *be);
141 void (*recv_disable)(struct net_backend *be);
144 * Ask the backend for the virtio-net features it is able to
145 * support. Possible features are TSO, UFO and checksum offloading
146 * in both rx and tx direction and for both IPv4 and IPv6.
148 uint64_t (*get_cap)(struct net_backend *be);
151 * Tell the backend to enable/disable the specified virtio-net
152 * features (capabilities).
154 int (*set_cap)(struct net_backend *be, uint64_t features,
155 unsigned int vnet_hdr_len);
157 struct pci_vtnet_softc *sc;
161 * Length of the virtio-net header used by the backend and the
162 * frontend, respectively. A zero value means that the header
165 unsigned int be_vnet_hdr_len;
166 unsigned int fe_vnet_hdr_len;
168 /* Size of backend-specific private data. */
171 /* Backend-specific private data follows. */
174 #define NET_BE_PRIV(be) ((void *)((be) + 1))
175 #define NET_BE_SIZE(be) (sizeof(*be) + (be)->priv_size)
177 SET_DECLARE(net_backend_set, struct net_backend);
179 #define VNET_HDR_LEN sizeof(struct virtio_net_rxhdr)
181 #define WPRINTF(params) PRINTLN params
187 #if defined(INET6) || defined(INET)
188 static const int pf_list[] = {
201 * A bounce buffer that allows us to implement the peek_recvlen
202 * callback. In the future we may get the same information from
210 tap_cleanup(struct net_backend *be)
212 struct tap_priv *priv = NET_BE_PRIV(be);
215 mevent_delete(priv->mevp);
224 tap_init(struct net_backend *be, const char *devname,
225 nvlist_t *nvl __unused, net_be_rxeof_t cb, void *param)
227 struct tap_priv *priv = NET_BE_PRIV(be);
230 #if defined(INET6) || defined(INET)
234 #ifndef WITHOUT_CAPSICUM
239 WPRINTF(("TAP backend requires non-NULL callback"));
243 strcpy(tbuf, "/dev/");
244 strlcat(tbuf, devname, sizeof(tbuf));
246 be->fd = open(tbuf, O_RDWR);
248 WPRINTF(("open of tap device %s failed", tbuf));
253 * Set non-blocking and register for read
254 * notifications with the event loop
256 if (ioctl(be->fd, FIONBIO, &opt) < 0) {
257 WPRINTF(("tap device O_NONBLOCK failed"));
261 #if defined(INET6) || defined(INET)
263 * Try to UP the interface rather than relying on
264 * net.link.tap.up_on_open.
266 bzero(&ifrq, sizeof(ifrq));
267 if (ioctl(be->fd, TAPGIFNAME, &ifrq) < 0) {
268 WPRINTF(("Could not get interface name"));
273 for (size_t i = 0; s == -1 && i < nitems(pf_list); i++)
274 s = socket(pf_list[i], SOCK_DGRAM, 0);
276 WPRINTF(("Could open socket"));
280 if (ioctl(s, SIOCGIFFLAGS, &ifrq) < 0) {
282 WPRINTF(("Could not get interface flags"));
285 ifrq.ifr_flags |= IFF_UP;
286 if (ioctl(s, SIOCSIFFLAGS, &ifrq) < 0) {
288 WPRINTF(("Could not set interface flags"));
294 #ifndef WITHOUT_CAPSICUM
295 cap_rights_init(&rights, CAP_EVENT, CAP_READ, CAP_WRITE);
296 if (caph_rights_limit(be->fd, &rights) == -1)
297 errx(EX_OSERR, "Unable to apply rights for sandbox");
300 memset(priv->bbuf, 0, sizeof(priv->bbuf));
303 priv->mevp = mevent_add_disabled(be->fd, EVF_READ, cb, param);
304 if (priv->mevp == NULL) {
305 WPRINTF(("Could not register event"));
317 * Called to send a buffer chain out to the tap device
320 tap_send(struct net_backend *be, const struct iovec *iov, int iovcnt)
322 return (writev(be->fd, iov, iovcnt));
326 tap_peek_recvlen(struct net_backend *be)
328 struct tap_priv *priv = NET_BE_PRIV(be);
331 if (priv->bbuflen > 0) {
333 * We already have a packet in the bounce buffer.
334 * Just return its length.
336 return priv->bbuflen;
340 * Read the next packet (if any) into the bounce buffer, so
341 * that we get to know its length and we can return that
344 ret = read(be->fd, priv->bbuf, sizeof(priv->bbuf));
345 if (ret < 0 && errno == EWOULDBLOCK) {
356 tap_recv(struct net_backend *be, const struct iovec *iov, int iovcnt)
358 struct tap_priv *priv = NET_BE_PRIV(be);
361 if (priv->bbuflen > 0) {
363 * A packet is available in the bounce buffer, so
364 * we read it from there.
366 ret = buf_to_iov(priv->bbuf, priv->bbuflen,
369 /* Mark the bounce buffer as empty. */
375 ret = readv(be->fd, iov, iovcnt);
376 if (ret < 0 && errno == EWOULDBLOCK) {
384 tap_recv_enable(struct net_backend *be)
386 struct tap_priv *priv = NET_BE_PRIV(be);
388 mevent_enable(priv->mevp);
392 tap_recv_disable(struct net_backend *be)
394 struct tap_priv *priv = NET_BE_PRIV(be);
396 mevent_disable(priv->mevp);
400 tap_get_cap(struct net_backend *be __unused)
403 return (0); /* no capabilities for now */
407 tap_set_cap(struct net_backend *be __unused, uint64_t features,
408 unsigned vnet_hdr_len)
411 return ((features || vnet_hdr_len) ? -1 : 0);
414 static struct net_backend tap_backend = {
416 .priv_size = sizeof(struct tap_priv),
418 .cleanup = tap_cleanup,
420 .peek_recvlen = tap_peek_recvlen,
422 .recv_enable = tap_recv_enable,
423 .recv_disable = tap_recv_disable,
424 .get_cap = tap_get_cap,
425 .set_cap = tap_set_cap,
428 /* A clone of the tap backend, with a different prefix. */
429 static struct net_backend vmnet_backend = {
431 .priv_size = sizeof(struct tap_priv),
433 .cleanup = tap_cleanup,
435 .peek_recvlen = tap_peek_recvlen,
437 .recv_enable = tap_recv_enable,
438 .recv_disable = tap_recv_disable,
439 .get_cap = tap_get_cap,
440 .set_cap = tap_set_cap,
443 DATA_SET(net_backend_set, tap_backend);
444 DATA_SET(net_backend_set, vmnet_backend);
452 #define NG_SBUF_MAX_SIZE (4 * 1024 * 1024)
455 ng_init(struct net_backend *be, const char *devname __unused,
456 nvlist_t *nvl, net_be_rxeof_t cb, void *param)
458 struct tap_priv *p = NET_BE_PRIV(be);
459 struct ngm_connect ngc;
460 const char *value, *nodename;
464 unsigned long maxsbsz;
466 #ifndef WITHOUT_CAPSICUM
471 WPRINTF(("Netgraph backend requires non-NULL callback"));
477 memset(&ngc, 0, sizeof(ngc));
479 value = get_config_value_node(nvl, "path");
481 WPRINTF(("path must be provided"));
484 strncpy(ngc.path, value, NG_PATHSIZ - 1);
486 value = get_config_value_node(nvl, "hook");
489 strncpy(ngc.ourhook, value, NG_HOOKSIZ - 1);
491 value = get_config_value_node(nvl, "peerhook");
493 WPRINTF(("peer hook must be provided"));
496 strncpy(ngc.peerhook, value, NG_HOOKSIZ - 1);
498 nodename = get_config_value_node(nvl, "socket");
499 if (NgMkSockNode(nodename,
500 &ctrl_sock, &be->fd) < 0) {
501 WPRINTF(("can't get Netgraph sockets"));
505 if (NgSendMsg(ctrl_sock, ".",
507 NGM_CONNECT, &ngc, sizeof(ngc)) < 0) {
508 WPRINTF(("can't connect to node"));
515 flags = fcntl(be->fd, F_GETFL);
518 WPRINTF(("can't get socket flags"));
522 if (fcntl(be->fd, F_SETFL, flags | O_NONBLOCK) < 0) {
523 WPRINTF(("can't set O_NONBLOCK flag"));
528 * The default ng_socket(4) buffer's size is too low.
529 * Calculate the minimum value between NG_SBUF_MAX_SIZE
530 * and kern.ipc.maxsockbuf.
532 msbsz = sizeof(maxsbsz);
533 if (sysctlbyname("kern.ipc.maxsockbuf", &maxsbsz, &msbsz,
535 WPRINTF(("can't get 'kern.ipc.maxsockbuf' value"));
540 * We can't set the socket buffer size to kern.ipc.maxsockbuf value,
541 * as it takes into account the mbuf(9) overhead.
543 maxsbsz = maxsbsz * MCLBYTES / (MSIZE + MCLBYTES);
545 sbsz = MIN(NG_SBUF_MAX_SIZE, maxsbsz);
547 if (setsockopt(be->fd, SOL_SOCKET, SO_SNDBUF, &sbsz,
549 WPRINTF(("can't set TX buffer size"));
553 if (setsockopt(be->fd, SOL_SOCKET, SO_RCVBUF, &sbsz,
555 WPRINTF(("can't set RX buffer size"));
559 #ifndef WITHOUT_CAPSICUM
560 cap_rights_init(&rights, CAP_EVENT, CAP_READ, CAP_WRITE);
561 if (caph_rights_limit(be->fd, &rights) == -1)
562 errx(EX_OSERR, "Unable to apply rights for sandbox");
565 memset(p->bbuf, 0, sizeof(p->bbuf));
568 p->mevp = mevent_add_disabled(be->fd, EVF_READ, cb, param);
569 if (p->mevp == NULL) {
570 WPRINTF(("Could not register event"));
581 static struct net_backend ng_backend = {
582 .prefix = "netgraph",
583 .priv_size = sizeof(struct tap_priv),
585 .cleanup = tap_cleanup,
587 .peek_recvlen = tap_peek_recvlen,
589 .recv_enable = tap_recv_enable,
590 .recv_disable = tap_recv_disable,
591 .get_cap = tap_get_cap,
592 .set_cap = tap_set_cap,
595 DATA_SET(net_backend_set, ng_backend);
597 #endif /* NETGRAPH */
603 /* The virtio-net features supported by netmap. */
604 #define NETMAP_FEATURES (VIRTIO_NET_F_CSUM | VIRTIO_NET_F_HOST_TSO4 | \
605 VIRTIO_NET_F_HOST_TSO6 | VIRTIO_NET_F_HOST_UFO | \
606 VIRTIO_NET_F_GUEST_CSUM | VIRTIO_NET_F_GUEST_TSO4 | \
607 VIRTIO_NET_F_GUEST_TSO6 | VIRTIO_NET_F_GUEST_UFO)
610 char ifname[IFNAMSIZ];
613 struct netmap_ring *rx;
614 struct netmap_ring *tx;
621 nmreq_init(struct nmreq *req, char *ifname)
624 memset(req, 0, sizeof(*req));
625 strlcpy(req->nr_name, ifname, sizeof(req->nr_name));
626 req->nr_version = NETMAP_API;
630 netmap_set_vnet_hdr_len(struct net_backend *be, int vnet_hdr_len)
634 struct netmap_priv *priv = NET_BE_PRIV(be);
636 nmreq_init(&req, priv->ifname);
637 req.nr_cmd = NETMAP_BDG_VNET_HDR;
638 req.nr_arg1 = vnet_hdr_len;
639 err = ioctl(be->fd, NIOCREGIF, &req);
641 WPRINTF(("Unable to set vnet header length %d",
646 be->be_vnet_hdr_len = vnet_hdr_len;
652 netmap_has_vnet_hdr_len(struct net_backend *be, unsigned vnet_hdr_len)
654 unsigned prev_hdr_len = be->be_vnet_hdr_len;
657 if (vnet_hdr_len == prev_hdr_len) {
661 ret = netmap_set_vnet_hdr_len(be, vnet_hdr_len);
666 netmap_set_vnet_hdr_len(be, prev_hdr_len);
672 netmap_get_cap(struct net_backend *be)
675 return (netmap_has_vnet_hdr_len(be, VNET_HDR_LEN) ?
676 NETMAP_FEATURES : 0);
680 netmap_set_cap(struct net_backend *be, uint64_t features __unused,
681 unsigned vnet_hdr_len)
684 return (netmap_set_vnet_hdr_len(be, vnet_hdr_len));
688 netmap_init(struct net_backend *be, const char *devname,
689 nvlist_t *nvl __unused, net_be_rxeof_t cb, void *param)
691 struct netmap_priv *priv = NET_BE_PRIV(be);
693 strlcpy(priv->ifname, devname, sizeof(priv->ifname));
694 priv->ifname[sizeof(priv->ifname) - 1] = '\0';
696 priv->nmd = nm_open(priv->ifname, NULL, NETMAP_NO_TX_POLL, NULL);
697 if (priv->nmd == NULL) {
698 WPRINTF(("Unable to nm_open(): interface '%s', errno (%s)",
699 devname, strerror(errno)));
703 priv->memid = priv->nmd->req.nr_arg2;
704 priv->tx = NETMAP_TXRING(priv->nmd->nifp, 0);
705 priv->rx = NETMAP_RXRING(priv->nmd->nifp, 0);
707 priv->cb_param = param;
708 be->fd = priv->nmd->fd;
710 priv->mevp = mevent_add_disabled(be->fd, EVF_READ, cb, param);
711 if (priv->mevp == NULL) {
712 WPRINTF(("Could not register event"));
720 netmap_cleanup(struct net_backend *be)
722 struct netmap_priv *priv = NET_BE_PRIV(be);
725 mevent_delete(priv->mevp);
734 netmap_send(struct net_backend *be, const struct iovec *iov,
737 struct netmap_priv *priv = NET_BE_PRIV(be);
738 struct netmap_ring *ring;
748 if (head == ring->tail) {
749 WPRINTF(("No space, drop %zu bytes", count_iov(iov, iovcnt)));
752 nm_buf = NETMAP_BUF(ring, ring->slot[head].buf_idx);
753 nm_buf_size = ring->nr_buf_size;
756 for (j = 0; j < iovcnt; j++) {
757 uint8_t *iov_frag_buf = iov[j].iov_base;
758 int iov_frag_size = iov[j].iov_len;
760 totlen += iov_frag_size;
763 * Split each iovec fragment over more netmap slots, if
769 copylen = iov_frag_size < nm_buf_size ? iov_frag_size : nm_buf_size;
770 memcpy(nm_buf, iov_frag_buf, copylen);
772 iov_frag_buf += copylen;
773 iov_frag_size -= copylen;
775 nm_buf_size -= copylen;
776 nm_buf_len += copylen;
778 if (iov_frag_size == 0) {
782 ring->slot[head].len = nm_buf_len;
783 ring->slot[head].flags = NS_MOREFRAG;
784 head = nm_ring_next(ring, head);
785 if (head == ring->tail) {
787 * We ran out of netmap slots while
788 * splitting the iovec fragments.
790 WPRINTF(("No space, drop %zu bytes",
791 count_iov(iov, iovcnt)));
794 nm_buf = NETMAP_BUF(ring, ring->slot[head].buf_idx);
795 nm_buf_size = ring->nr_buf_size;
800 /* Complete the last slot, which must not have NS_MOREFRAG set. */
801 ring->slot[head].len = nm_buf_len;
802 ring->slot[head].flags = 0;
803 head = nm_ring_next(ring, head);
805 /* Now update ring->head and ring->cur. */
806 ring->head = ring->cur = head;
808 ioctl(be->fd, NIOCTXSYNC, NULL);
814 netmap_peek_recvlen(struct net_backend *be)
816 struct netmap_priv *priv = NET_BE_PRIV(be);
817 struct netmap_ring *ring = priv->rx;
818 uint32_t head = ring->head;
821 while (head != ring->tail) {
822 struct netmap_slot *slot = ring->slot + head;
825 if ((slot->flags & NS_MOREFRAG) == 0)
827 head = nm_ring_next(ring, head);
834 netmap_recv(struct net_backend *be, const struct iovec *iov, int iovcnt)
836 struct netmap_priv *priv = NET_BE_PRIV(be);
837 struct netmap_slot *slot = NULL;
838 struct netmap_ring *ring;
839 uint8_t *iov_frag_buf;
848 iov_frag_buf = iov->iov_base;
849 iov_frag_size = iov->iov_len;
855 if (head == ring->tail) {
859 slot = ring->slot + head;
860 nm_buf = NETMAP_BUF(ring, slot->buf_idx);
861 nm_buf_len = slot->len;
864 int copylen = nm_buf_len < iov_frag_size ?
865 nm_buf_len : iov_frag_size;
867 memcpy(iov_frag_buf, nm_buf, copylen);
869 nm_buf_len -= copylen;
870 iov_frag_buf += copylen;
871 iov_frag_size -= copylen;
874 if (nm_buf_len == 0) {
881 /* No space to receive. */
882 WPRINTF(("Short iov, drop %zd bytes",
886 iov_frag_buf = iov->iov_base;
887 iov_frag_size = iov->iov_len;
890 head = nm_ring_next(ring, head);
892 } while (slot->flags & NS_MOREFRAG);
894 /* Release slots to netmap. */
895 ring->head = ring->cur = head;
901 netmap_recv_enable(struct net_backend *be)
903 struct netmap_priv *priv = NET_BE_PRIV(be);
905 mevent_enable(priv->mevp);
909 netmap_recv_disable(struct net_backend *be)
911 struct netmap_priv *priv = NET_BE_PRIV(be);
913 mevent_disable(priv->mevp);
916 static struct net_backend netmap_backend = {
918 .priv_size = sizeof(struct netmap_priv),
920 .cleanup = netmap_cleanup,
922 .peek_recvlen = netmap_peek_recvlen,
924 .recv_enable = netmap_recv_enable,
925 .recv_disable = netmap_recv_disable,
926 .get_cap = netmap_get_cap,
927 .set_cap = netmap_set_cap,
930 /* A clone of the netmap backend, with a different prefix. */
931 static struct net_backend vale_backend = {
933 .priv_size = sizeof(struct netmap_priv),
935 .cleanup = netmap_cleanup,
937 .peek_recvlen = netmap_peek_recvlen,
939 .recv_enable = netmap_recv_enable,
940 .recv_disable = netmap_recv_disable,
941 .get_cap = netmap_get_cap,
942 .set_cap = netmap_set_cap,
945 DATA_SET(net_backend_set, netmap_backend);
946 DATA_SET(net_backend_set, vale_backend);
949 netbe_legacy_config(nvlist_t *nvl, const char *opts)
956 cp = strchr(opts, ',');
958 set_config_value_node(nvl, "backend", opts);
961 backend = strndup(opts, cp - opts);
962 set_config_value_node(nvl, "backend", backend);
964 return (pci_parse_legacy_config(nvl, cp + 1));
968 * Initialize a backend and attach to the frontend.
969 * This is called during frontend initialization.
970 * @ret is a pointer to the backend to be initialized
971 * @devname is the backend-name as supplied on the command line,
972 * e.g. -s 2:0,frontend-name,backend-name[,other-args]
973 * @cb is the receive callback supplied by the frontend,
974 * and it is invoked in the event loop when a receive
975 * event is generated in the hypervisor,
976 * @param is a pointer to the frontend, and normally used as
977 * the argument for the callback.
980 netbe_init(struct net_backend **ret, nvlist_t *nvl, net_be_rxeof_t cb,
983 struct net_backend **pbe, *nbe, *tbe = NULL;
984 const char *value, *type;
988 value = get_config_value_node(nvl, "backend");
992 devname = strdup(value);
995 * Use the type given by configuration if exists; otherwise
996 * use the prefix of the backend as the type.
998 type = get_config_value_node(nvl, "type");
1003 * Find the network backend that matches the user-provided
1004 * device name. net_backend_set is built using a linker set.
1006 SET_FOREACH(pbe, net_backend_set) {
1007 if (strncmp(type, (*pbe)->prefix,
1008 strlen((*pbe)->prefix)) == 0) {
1010 assert(tbe->init != NULL);
1011 assert(tbe->cleanup != NULL);
1012 assert(tbe->send != NULL);
1013 assert(tbe->recv != NULL);
1014 assert(tbe->get_cap != NULL);
1015 assert(tbe->set_cap != NULL);
1026 nbe = calloc(1, NET_BE_SIZE(tbe));
1027 *nbe = *tbe; /* copy the template */
1030 nbe->be_vnet_hdr_len = 0;
1031 nbe->fe_vnet_hdr_len = 0;
1033 /* Initialize the backend. */
1034 err = nbe->init(nbe, devname, nvl, cb, param);
1048 netbe_cleanup(struct net_backend *be)
1058 netbe_get_cap(struct net_backend *be)
1062 return (be->get_cap(be));
1066 netbe_set_cap(struct net_backend *be, uint64_t features,
1067 unsigned vnet_hdr_len)
1073 /* There are only three valid lengths, i.e., 0, 10 and 12. */
1074 if (vnet_hdr_len && vnet_hdr_len != VNET_HDR_LEN
1075 && vnet_hdr_len != (VNET_HDR_LEN - sizeof(uint16_t)))
1078 be->fe_vnet_hdr_len = vnet_hdr_len;
1080 ret = be->set_cap(be, features, vnet_hdr_len);
1081 assert(be->be_vnet_hdr_len == 0 ||
1082 be->be_vnet_hdr_len == be->fe_vnet_hdr_len);
1088 netbe_send(struct net_backend *be, const struct iovec *iov, int iovcnt)
1091 return (be->send(be, iov, iovcnt));
1095 netbe_peek_recvlen(struct net_backend *be)
1098 return (be->peek_recvlen(be));
1102 * Try to read a packet from the backend, without blocking.
1103 * If no packets are available, return 0. In case of success, return
1104 * the length of the packet just read. Return -1 in case of errors.
1107 netbe_recv(struct net_backend *be, const struct iovec *iov, int iovcnt)
1110 return (be->recv(be, iov, iovcnt));
1114 * Read a packet from the backend and discard it.
1115 * Returns the size of the discarded packet or zero if no packet was available.
1116 * A negative error code is returned in case of read error.
1119 netbe_rx_discard(struct net_backend *be)
1122 * MP note: the dummybuf is only used to discard frames,
1123 * so there is no need for it to be per-vtnet or locked.
1124 * We only make it large enough for TSO-sized segment.
1126 static uint8_t dummybuf[65536 + 64];
1129 iov.iov_base = dummybuf;
1130 iov.iov_len = sizeof(dummybuf);
1132 return netbe_recv(be, &iov, 1);
1136 netbe_rx_disable(struct net_backend *be)
1139 return be->recv_disable(be);
1143 netbe_rx_enable(struct net_backend *be)
1146 return be->recv_enable(be);
1150 netbe_get_vnet_hdr_len(struct net_backend *be)
1153 return (be->be_vnet_hdr_len);