Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 19 Nov 2021 00:02:48 GMT
From:      "Bjoern A. Zeeb" <bz@FreeBSD.org>
To:        src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org
Subject:   git: f74147e26999 - stable/13 - bhyve: net_backends, automatically IFF_UP tap devices
Message-ID:  <202111190002.1AJ02meM072733@gitrepo.freebsd.org>

next in thread | raw e-mail | index | archive | help
The branch stable/13 has been updated by bz:

URL: https://cgit.FreeBSD.org/src/commit/?id=f74147e26999838e03a522bf59ea33bef470d356

commit f74147e26999838e03a522bf59ea33bef470d356
Author:     Bjoern A. Zeeb <bz@FreeBSD.org>
AuthorDate: 2021-07-28 22:53:25 +0000
Commit:     Bjoern A. Zeeb <bz@FreeBSD.org>
CommitDate: 2021-11-19 00:01:27 +0000

    bhyve: net_backends, automatically IFF_UP tap devices
    
    If you want communications with the outside world and tell bhyve to
    create an interfaces then it should be usable as well.
    Rather than relying on the sysctl net.link.tap.up_on_open automatically
    try to IFF_UP the opened tap device.
    
    (cherry picked from commit 56be282bc999cc05dcd1ccb163b108d54f3ff448)
---
 usr.sbin/bhyve/net_backends.c | 51 +++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 51 insertions(+)

diff --git a/usr.sbin/bhyve/net_backends.c b/usr.sbin/bhyve/net_backends.c
index 30c26aea458b..cb1730fc77df 100644
--- a/usr.sbin/bhyve/net_backends.c
+++ b/usr.sbin/bhyve/net_backends.c
@@ -46,6 +46,9 @@ __FBSDID("$FreeBSD$");
 #include <sys/uio.h>
 
 #include <net/if.h>
+#if defined(INET6) || defined(INET)
+#include <net/if_tap.h>
+#endif
 #include <net/netmap.h>
 #include <net/netmap_virt.h>
 #define NETMAP_WITH_LIBS
@@ -179,6 +182,17 @@ SET_DECLARE(net_backend_set, struct net_backend);
  * The tap backend
  */
 
+#if defined(INET6) || defined(INET)
+const int pf_list[] = {
+#if defined(INET6)
+	PF_INET6,
+#endif
+#if defined(INET)
+	PF_INET,
+#endif
+};
+#endif
+
 struct tap_priv {
 	struct mevent *mevp;
 	/*
@@ -211,6 +225,10 @@ tap_init(struct net_backend *be, const char *devname,
 	struct tap_priv *priv = (struct tap_priv *)be->opaque;
 	char tbuf[80];
 	int opt = 1;
+#if defined(INET6) || defined(INET)
+	struct ifreq ifrq;
+	int i, s;
+#endif
 #ifndef WITHOUT_CAPSICUM
 	cap_rights_t rights;
 #endif
@@ -238,6 +256,39 @@ tap_init(struct net_backend *be, const char *devname,
 		goto error;
 	}
 
+#if defined(INET6) || defined(INET)
+	/*
+	 * Try to UP the interface rather than relying on
+	 * net.link.tap.up_on_open.
+	  */
+	bzero(&ifrq, sizeof(ifrq));
+	if (ioctl(be->fd, TAPGIFNAME, &ifrq) < 0) {
+		WPRINTF(("Could not get interface name"));
+		goto error;
+	}
+
+	s = -1;
+	for (i = 0; s == -1 && i < nitems(pf_list); i++)
+		s = socket(pf_list[i], SOCK_DGRAM, 0);
+	if (s == -1) {
+		WPRINTF(("Could open socket"));
+		goto error;
+	}
+
+	if (ioctl(s, SIOCGIFFLAGS, &ifrq) < 0) {
+		(void)close(s);
+		WPRINTF(("Could not get interface flags"));
+		goto error;
+	}
+	ifrq.ifr_flags |= IFF_UP;
+	if (ioctl(s, SIOCSIFFLAGS, &ifrq) < 0) {
+		(void)close(s);
+		WPRINTF(("Could not set interface flags"));
+		goto error;
+	}
+	(void)close(s);
+#endif
+
 #ifndef WITHOUT_CAPSICUM
 	cap_rights_init(&rights, CAP_EVENT, CAP_READ, CAP_WRITE);
 	if (caph_rights_limit(be->fd, &rights) == -1)



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?202111190002.1AJ02meM072733>