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 /* Room for backend-specific data. */
175 SET_DECLARE(net_backend_set, struct net_backend);
177 #define VNET_HDR_LEN sizeof(struct virtio_net_rxhdr)
179 #define WPRINTF(params) PRINTLN params
185 #if defined(INET6) || defined(INET)
186 static const int pf_list[] = {
199 * A bounce buffer that allows us to implement the peek_recvlen
200 * callback. In the future we may get the same information from
208 tap_cleanup(struct net_backend *be)
210 struct tap_priv *priv = (struct tap_priv *)be->opaque;
213 mevent_delete(priv->mevp);
222 tap_init(struct net_backend *be, const char *devname,
223 nvlist_t *nvl, net_be_rxeof_t cb, void *param)
225 struct tap_priv *priv = (struct tap_priv *)be->opaque;
228 #if defined(INET6) || defined(INET)
232 #ifndef WITHOUT_CAPSICUM
237 WPRINTF(("TAP backend requires non-NULL callback"));
241 strcpy(tbuf, "/dev/");
242 strlcat(tbuf, devname, sizeof(tbuf));
244 be->fd = open(tbuf, O_RDWR);
246 WPRINTF(("open of tap device %s failed", tbuf));
251 * Set non-blocking and register for read
252 * notifications with the event loop
254 if (ioctl(be->fd, FIONBIO, &opt) < 0) {
255 WPRINTF(("tap device O_NONBLOCK failed"));
259 #if defined(INET6) || defined(INET)
261 * Try to UP the interface rather than relying on
262 * net.link.tap.up_on_open.
264 bzero(&ifrq, sizeof(ifrq));
265 if (ioctl(be->fd, TAPGIFNAME, &ifrq) < 0) {
266 WPRINTF(("Could not get interface name"));
271 for (i = 0; s == -1 && i < nitems(pf_list); i++)
272 s = socket(pf_list[i], SOCK_DGRAM, 0);
274 WPRINTF(("Could open socket"));
278 if (ioctl(s, SIOCGIFFLAGS, &ifrq) < 0) {
280 WPRINTF(("Could not get interface flags"));
283 ifrq.ifr_flags |= IFF_UP;
284 if (ioctl(s, SIOCSIFFLAGS, &ifrq) < 0) {
286 WPRINTF(("Could not set interface flags"));
292 #ifndef WITHOUT_CAPSICUM
293 cap_rights_init(&rights, CAP_EVENT, CAP_READ, CAP_WRITE);
294 if (caph_rights_limit(be->fd, &rights) == -1)
295 errx(EX_OSERR, "Unable to apply rights for sandbox");
298 memset(priv->bbuf, 0, sizeof(priv->bbuf));
301 priv->mevp = mevent_add_disabled(be->fd, EVF_READ, cb, param);
302 if (priv->mevp == NULL) {
303 WPRINTF(("Could not register event"));
315 * Called to send a buffer chain out to the tap device
318 tap_send(struct net_backend *be, const struct iovec *iov, int iovcnt)
320 return (writev(be->fd, iov, iovcnt));
324 tap_peek_recvlen(struct net_backend *be)
326 struct tap_priv *priv = (struct tap_priv *)be->opaque;
329 if (priv->bbuflen > 0) {
331 * We already have a packet in the bounce buffer.
332 * Just return its length.
334 return priv->bbuflen;
338 * Read the next packet (if any) into the bounce buffer, so
339 * that we get to know its length and we can return that
342 ret = read(be->fd, priv->bbuf, sizeof(priv->bbuf));
343 if (ret < 0 && errno == EWOULDBLOCK) {
354 tap_recv(struct net_backend *be, const struct iovec *iov, int iovcnt)
356 struct tap_priv *priv = (struct tap_priv *)be->opaque;
359 if (priv->bbuflen > 0) {
361 * A packet is available in the bounce buffer, so
362 * we read it from there.
364 ret = buf_to_iov(priv->bbuf, priv->bbuflen,
367 /* Mark the bounce buffer as empty. */
373 ret = readv(be->fd, iov, iovcnt);
374 if (ret < 0 && errno == EWOULDBLOCK) {
382 tap_recv_enable(struct net_backend *be)
384 struct tap_priv *priv = (struct tap_priv *)be->opaque;
386 mevent_enable(priv->mevp);
390 tap_recv_disable(struct net_backend *be)
392 struct tap_priv *priv = (struct tap_priv *)be->opaque;
394 mevent_disable(priv->mevp);
398 tap_get_cap(struct net_backend *be)
401 return (0); /* no capabilities for now */
405 tap_set_cap(struct net_backend *be, uint64_t features,
406 unsigned vnet_hdr_len)
409 return ((features || vnet_hdr_len) ? -1 : 0);
412 static struct net_backend tap_backend = {
414 .priv_size = sizeof(struct tap_priv),
416 .cleanup = tap_cleanup,
418 .peek_recvlen = tap_peek_recvlen,
420 .recv_enable = tap_recv_enable,
421 .recv_disable = tap_recv_disable,
422 .get_cap = tap_get_cap,
423 .set_cap = tap_set_cap,
426 /* A clone of the tap backend, with a different prefix. */
427 static struct net_backend vmnet_backend = {
429 .priv_size = sizeof(struct tap_priv),
431 .cleanup = tap_cleanup,
433 .peek_recvlen = tap_peek_recvlen,
435 .recv_enable = tap_recv_enable,
436 .recv_disable = tap_recv_disable,
437 .get_cap = tap_get_cap,
438 .set_cap = tap_set_cap,
441 DATA_SET(net_backend_set, tap_backend);
442 DATA_SET(net_backend_set, vmnet_backend);
450 #define NG_SBUF_MAX_SIZE (4 * 1024 * 1024)
453 ng_init(struct net_backend *be, const char *devname,
454 nvlist_t *nvl, net_be_rxeof_t cb, void *param)
456 struct tap_priv *p = (struct tap_priv *)be->opaque;
457 struct ngm_connect ngc;
458 const char *value, *nodename;
462 unsigned long maxsbsz;
464 #ifndef WITHOUT_CAPSICUM
469 WPRINTF(("Netgraph backend requires non-NULL callback"));
475 memset(&ngc, 0, sizeof(ngc));
477 value = get_config_value_node(nvl, "path");
479 WPRINTF(("path must be provided"));
482 strncpy(ngc.path, value, NG_PATHSIZ - 1);
484 value = get_config_value_node(nvl, "hook");
487 strncpy(ngc.ourhook, value, NG_HOOKSIZ - 1);
489 value = get_config_value_node(nvl, "peerhook");
491 WPRINTF(("peer hook must be provided"));
494 strncpy(ngc.peerhook, value, NG_HOOKSIZ - 1);
496 nodename = get_config_value_node(nvl, "socket");
497 if (NgMkSockNode(nodename,
498 &ctrl_sock, &be->fd) < 0) {
499 WPRINTF(("can't get Netgraph sockets"));
503 if (NgSendMsg(ctrl_sock, ".",
505 NGM_CONNECT, &ngc, sizeof(ngc)) < 0) {
506 WPRINTF(("can't connect to node"));
513 flags = fcntl(be->fd, F_GETFL);
516 WPRINTF(("can't get socket flags"));
520 if (fcntl(be->fd, F_SETFL, flags | O_NONBLOCK) < 0) {
521 WPRINTF(("can't set O_NONBLOCK flag"));
526 * The default ng_socket(4) buffer's size is too low.
527 * Calculate the minimum value between NG_SBUF_MAX_SIZE
528 * and kern.ipc.maxsockbuf.
530 msbsz = sizeof(maxsbsz);
531 if (sysctlbyname("kern.ipc.maxsockbuf", &maxsbsz, &msbsz,
533 WPRINTF(("can't get 'kern.ipc.maxsockbuf' value"));
538 * We can't set the socket buffer size to kern.ipc.maxsockbuf value,
539 * as it takes into account the mbuf(9) overhead.
541 maxsbsz = maxsbsz * MCLBYTES / (MSIZE + MCLBYTES);
543 sbsz = MIN(NG_SBUF_MAX_SIZE, maxsbsz);
545 if (setsockopt(be->fd, SOL_SOCKET, SO_SNDBUF, &sbsz,
547 WPRINTF(("can't set TX buffer size"));
551 if (setsockopt(be->fd, SOL_SOCKET, SO_RCVBUF, &sbsz,
553 WPRINTF(("can't set RX buffer size"));
557 #ifndef WITHOUT_CAPSICUM
558 cap_rights_init(&rights, CAP_EVENT, CAP_READ, CAP_WRITE);
559 if (caph_rights_limit(be->fd, &rights) == -1)
560 errx(EX_OSERR, "Unable to apply rights for sandbox");
563 memset(p->bbuf, 0, sizeof(p->bbuf));
566 p->mevp = mevent_add_disabled(be->fd, EVF_READ, cb, param);
567 if (p->mevp == NULL) {
568 WPRINTF(("Could not register event"));
579 static struct net_backend ng_backend = {
580 .prefix = "netgraph",
581 .priv_size = sizeof(struct tap_priv),
583 .cleanup = tap_cleanup,
585 .peek_recvlen = tap_peek_recvlen,
587 .recv_enable = tap_recv_enable,
588 .recv_disable = tap_recv_disable,
589 .get_cap = tap_get_cap,
590 .set_cap = tap_set_cap,
593 DATA_SET(net_backend_set, ng_backend);
595 #endif /* NETGRAPH */
601 /* The virtio-net features supported by netmap. */
602 #define NETMAP_FEATURES (VIRTIO_NET_F_CSUM | VIRTIO_NET_F_HOST_TSO4 | \
603 VIRTIO_NET_F_HOST_TSO6 | VIRTIO_NET_F_HOST_UFO | \
604 VIRTIO_NET_F_GUEST_CSUM | VIRTIO_NET_F_GUEST_TSO4 | \
605 VIRTIO_NET_F_GUEST_TSO6 | VIRTIO_NET_F_GUEST_UFO)
608 char ifname[IFNAMSIZ];
611 struct netmap_ring *rx;
612 struct netmap_ring *tx;
619 nmreq_init(struct nmreq *req, char *ifname)
622 memset(req, 0, sizeof(*req));
623 strlcpy(req->nr_name, ifname, sizeof(req->nr_name));
624 req->nr_version = NETMAP_API;
628 netmap_set_vnet_hdr_len(struct net_backend *be, int vnet_hdr_len)
632 struct netmap_priv *priv = (struct netmap_priv *)be->opaque;
634 nmreq_init(&req, priv->ifname);
635 req.nr_cmd = NETMAP_BDG_VNET_HDR;
636 req.nr_arg1 = vnet_hdr_len;
637 err = ioctl(be->fd, NIOCREGIF, &req);
639 WPRINTF(("Unable to set vnet header length %d",
644 be->be_vnet_hdr_len = vnet_hdr_len;
650 netmap_has_vnet_hdr_len(struct net_backend *be, unsigned vnet_hdr_len)
652 int prev_hdr_len = be->be_vnet_hdr_len;
655 if (vnet_hdr_len == prev_hdr_len) {
659 ret = netmap_set_vnet_hdr_len(be, vnet_hdr_len);
664 netmap_set_vnet_hdr_len(be, prev_hdr_len);
670 netmap_get_cap(struct net_backend *be)
673 return (netmap_has_vnet_hdr_len(be, VNET_HDR_LEN) ?
674 NETMAP_FEATURES : 0);
678 netmap_set_cap(struct net_backend *be, uint64_t features,
679 unsigned vnet_hdr_len)
682 return (netmap_set_vnet_hdr_len(be, vnet_hdr_len));
686 netmap_init(struct net_backend *be, const char *devname,
687 nvlist_t *nvl, net_be_rxeof_t cb, void *param)
689 struct netmap_priv *priv = (struct netmap_priv *)be->opaque;
691 strlcpy(priv->ifname, devname, sizeof(priv->ifname));
692 priv->ifname[sizeof(priv->ifname) - 1] = '\0';
694 priv->nmd = nm_open(priv->ifname, NULL, NETMAP_NO_TX_POLL, NULL);
695 if (priv->nmd == NULL) {
696 WPRINTF(("Unable to nm_open(): interface '%s', errno (%s)",
697 devname, strerror(errno)));
702 priv->memid = priv->nmd->req.nr_arg2;
703 priv->tx = NETMAP_TXRING(priv->nmd->nifp, 0);
704 priv->rx = NETMAP_RXRING(priv->nmd->nifp, 0);
706 priv->cb_param = param;
707 be->fd = priv->nmd->fd;
709 priv->mevp = mevent_add_disabled(be->fd, EVF_READ, cb, param);
710 if (priv->mevp == NULL) {
711 WPRINTF(("Could not register event"));
719 netmap_cleanup(struct net_backend *be)
721 struct netmap_priv *priv = (struct netmap_priv *)be->opaque;
724 mevent_delete(priv->mevp);
733 netmap_send(struct net_backend *be, const struct iovec *iov,
736 struct netmap_priv *priv = (struct netmap_priv *)be->opaque;
737 struct netmap_ring *ring;
747 if (head == ring->tail) {
748 WPRINTF(("No space, drop %zu bytes", count_iov(iov, iovcnt)));
751 nm_buf = NETMAP_BUF(ring, ring->slot[head].buf_idx);
752 nm_buf_size = ring->nr_buf_size;
755 for (j = 0; j < iovcnt; j++) {
756 int iov_frag_size = iov[j].iov_len;
757 void *iov_frag_buf = iov[j].iov_base;
759 totlen += iov_frag_size;
762 * Split each iovec fragment over more netmap slots, if
768 copylen = iov_frag_size < nm_buf_size ? iov_frag_size : nm_buf_size;
769 memcpy(nm_buf, iov_frag_buf, copylen);
771 iov_frag_buf += copylen;
772 iov_frag_size -= copylen;
774 nm_buf_size -= copylen;
775 nm_buf_len += copylen;
777 if (iov_frag_size == 0) {
781 ring->slot[head].len = nm_buf_len;
782 ring->slot[head].flags = NS_MOREFRAG;
783 head = nm_ring_next(ring, head);
784 if (head == ring->tail) {
786 * We ran out of netmap slots while
787 * splitting the iovec fragments.
789 WPRINTF(("No space, drop %zu bytes",
790 count_iov(iov, iovcnt)));
793 nm_buf = NETMAP_BUF(ring, ring->slot[head].buf_idx);
794 nm_buf_size = ring->nr_buf_size;
799 /* Complete the last slot, which must not have NS_MOREFRAG set. */
800 ring->slot[head].len = nm_buf_len;
801 ring->slot[head].flags = 0;
802 head = nm_ring_next(ring, head);
804 /* Now update ring->head and ring->cur. */
805 ring->head = ring->cur = head;
807 ioctl(be->fd, NIOCTXSYNC, NULL);
813 netmap_peek_recvlen(struct net_backend *be)
815 struct netmap_priv *priv = (struct netmap_priv *)be->opaque;
816 struct netmap_ring *ring = priv->rx;
817 uint32_t head = ring->head;
820 while (head != ring->tail) {
821 struct netmap_slot *slot = ring->slot + head;
824 if ((slot->flags & NS_MOREFRAG) == 0)
826 head = nm_ring_next(ring, head);
833 netmap_recv(struct net_backend *be, const struct iovec *iov, int iovcnt)
835 struct netmap_priv *priv = (struct netmap_priv *)be->opaque;
836 struct netmap_slot *slot = NULL;
837 struct netmap_ring *ring;
847 iov_frag_buf = iov->iov_base;
848 iov_frag_size = iov->iov_len;
854 if (head == ring->tail) {
858 slot = ring->slot + head;
859 nm_buf = NETMAP_BUF(ring, slot->buf_idx);
860 nm_buf_len = slot->len;
863 int copylen = nm_buf_len < iov_frag_size ?
864 nm_buf_len : iov_frag_size;
866 memcpy(iov_frag_buf, nm_buf, copylen);
868 nm_buf_len -= copylen;
869 iov_frag_buf += copylen;
870 iov_frag_size -= copylen;
873 if (nm_buf_len == 0) {
880 /* No space to receive. */
881 WPRINTF(("Short iov, drop %zd bytes",
885 iov_frag_buf = iov->iov_base;
886 iov_frag_size = iov->iov_len;
889 head = nm_ring_next(ring, head);
891 } while (slot->flags & NS_MOREFRAG);
893 /* Release slots to netmap. */
894 ring->head = ring->cur = head;
900 netmap_recv_enable(struct net_backend *be)
902 struct netmap_priv *priv = (struct netmap_priv *)be->opaque;
904 mevent_enable(priv->mevp);
908 netmap_recv_disable(struct net_backend *be)
910 struct netmap_priv *priv = (struct netmap_priv *)be->opaque;
912 mevent_disable(priv->mevp);
915 static struct net_backend netmap_backend = {
917 .priv_size = sizeof(struct netmap_priv),
919 .cleanup = netmap_cleanup,
921 .peek_recvlen = netmap_peek_recvlen,
923 .recv_enable = netmap_recv_enable,
924 .recv_disable = netmap_recv_disable,
925 .get_cap = netmap_get_cap,
926 .set_cap = netmap_set_cap,
929 /* A clone of the netmap backend, with a different prefix. */
930 static struct net_backend vale_backend = {
932 .priv_size = sizeof(struct netmap_priv),
934 .cleanup = netmap_cleanup,
936 .peek_recvlen = netmap_peek_recvlen,
938 .recv_enable = netmap_recv_enable,
939 .recv_disable = netmap_recv_disable,
940 .get_cap = netmap_get_cap,
941 .set_cap = netmap_set_cap,
944 DATA_SET(net_backend_set, netmap_backend);
945 DATA_SET(net_backend_set, vale_backend);
948 netbe_legacy_config(nvlist_t *nvl, const char *opts)
955 cp = strchr(opts, ',');
957 set_config_value_node(nvl, "backend", opts);
960 backend = strndup(opts, cp - opts);
961 set_config_value_node(nvl, "backend", backend);
963 return (pci_parse_legacy_config(nvl, cp + 1));
967 * Initialize a backend and attach to the frontend.
968 * This is called during frontend initialization.
969 * @ret is a pointer to the backend to be initialized
970 * @devname is the backend-name as supplied on the command line,
971 * e.g. -s 2:0,frontend-name,backend-name[,other-args]
972 * @cb is the receive callback supplied by the frontend,
973 * and it is invoked in the event loop when a receive
974 * event is generated in the hypervisor,
975 * @param is a pointer to the frontend, and normally used as
976 * the argument for the callback.
979 netbe_init(struct net_backend **ret, nvlist_t *nvl, net_be_rxeof_t cb,
982 struct net_backend **pbe, *nbe, *tbe = NULL;
983 const char *value, *type;
987 value = get_config_value_node(nvl, "backend");
991 devname = strdup(value);
994 * Use the type given by configuration if exists; otherwise
995 * use the prefix of the backend as the type.
997 type = get_config_value_node(nvl, "type");
1002 * Find the network backend that matches the user-provided
1003 * device name. net_backend_set is built using a linker set.
1005 SET_FOREACH(pbe, net_backend_set) {
1006 if (strncmp(type, (*pbe)->prefix,
1007 strlen((*pbe)->prefix)) == 0) {
1009 assert(tbe->init != NULL);
1010 assert(tbe->cleanup != NULL);
1011 assert(tbe->send != NULL);
1012 assert(tbe->recv != NULL);
1013 assert(tbe->get_cap != NULL);
1014 assert(tbe->set_cap != NULL);
1025 nbe = calloc(1, sizeof(*nbe) + tbe->priv_size);
1026 *nbe = *tbe; /* copy the template */
1029 nbe->be_vnet_hdr_len = 0;
1030 nbe->fe_vnet_hdr_len = 0;
1032 /* Initialize the backend. */
1033 err = nbe->init(nbe, devname, nvl, cb, param);
1047 netbe_cleanup(struct net_backend *be)
1057 netbe_get_cap(struct net_backend *be)
1061 return (be->get_cap(be));
1065 netbe_set_cap(struct net_backend *be, uint64_t features,
1066 unsigned vnet_hdr_len)
1072 /* There are only three valid lengths, i.e., 0, 10 and 12. */
1073 if (vnet_hdr_len && vnet_hdr_len != VNET_HDR_LEN
1074 && vnet_hdr_len != (VNET_HDR_LEN - sizeof(uint16_t)))
1077 be->fe_vnet_hdr_len = vnet_hdr_len;
1079 ret = be->set_cap(be, features, vnet_hdr_len);
1080 assert(be->be_vnet_hdr_len == 0 ||
1081 be->be_vnet_hdr_len == be->fe_vnet_hdr_len);
1087 netbe_send(struct net_backend *be, const struct iovec *iov, int iovcnt)
1090 return (be->send(be, iov, iovcnt));
1094 netbe_peek_recvlen(struct net_backend *be)
1097 return (be->peek_recvlen(be));
1101 * Try to read a packet from the backend, without blocking.
1102 * If no packets are available, return 0. In case of success, return
1103 * the length of the packet just read. Return -1 in case of errors.
1106 netbe_recv(struct net_backend *be, const struct iovec *iov, int iovcnt)
1109 return (be->recv(be, iov, iovcnt));
1113 * Read a packet from the backend and discard it.
1114 * Returns the size of the discarded packet or zero if no packet was available.
1115 * A negative error code is returned in case of read error.
1118 netbe_rx_discard(struct net_backend *be)
1121 * MP note: the dummybuf is only used to discard frames,
1122 * so there is no need for it to be per-vtnet or locked.
1123 * We only make it large enough for TSO-sized segment.
1125 static uint8_t dummybuf[65536 + 64];
1128 iov.iov_base = dummybuf;
1129 iov.iov_len = sizeof(dummybuf);
1131 return netbe_recv(be, &iov, 1);
1135 netbe_rx_disable(struct net_backend *be)
1138 return be->recv_disable(be);
1142 netbe_rx_enable(struct net_backend *be)
1145 return be->recv_enable(be);
1149 netbe_get_vnet_hdr_len(struct net_backend *be)
1152 return (be->be_vnet_hdr_len);