[PATCH v2 3/5] FreeBSD: implement virNetDevTapCreate() and virNetDevTapDelete().

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



---
 src/util/virnetdevtap.c | 112 ++++++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 109 insertions(+), 3 deletions(-)

diff --git a/src/util/virnetdevtap.c b/src/util/virnetdevtap.c
index a884de1..1b02e1f 100644
--- a/src/util/virnetdevtap.c
+++ b/src/util/virnetdevtap.c
@@ -103,7 +103,7 @@ virNetDevProbeVnetHdr(int tapfd)
 #endif
 
 
-#ifdef TUNSETIFF
+#if defined(TUNSETIFF)
 /**
  * virNetDevTapCreate:
  * @ifname: the interface name
@@ -230,7 +230,113 @@ cleanup:
     VIR_FORCE_CLOSE(fd);
     return ret;
 }
-#else /* ! TUNSETIFF */
+#elif defined(__FreeBSD__)
+int virNetDevTapCreate(char **ifname,
+                       int *tapfd,
+                       unsigned int flags ATTRIBUTE_UNUSED)
+{
+    int s;
+    struct ifreq ifr;
+    int ret = -1;
+    char *newifname = NULL;
+
+    /* As FreeBSD determines interface type by name,
+     * we have to create 'tap' interface first and
+     * then rename it to 'vnet'
+     */
+    if ((s = virNetDevSetupControl("tap", &ifr)) < 0)
+        return -1;
+
+    if (ioctl(s, SIOCIFCREATE2, &ifr) < 0) {
+        virReportSystemError(errno, "%s",
+                             _("Unable to create tap device"));
+        goto cleanup;
+    }
+
+    /* In case we were given exact interface name (e.g. 'vnetN'),
+     * we just rename to it. If we have format string like
+     * 'vnet%d', we need to find the first available name that
+     * matches this pattern
+     */
+    if (strstr(*ifname, "%d") == NULL) {
+         newifname = strdup(*ifname);
+    } else {
+        int i;
+        for (i = 0; i <= 255; i++) {
+            virBuffer newname = VIR_BUFFER_INITIALIZER;
+            virBufferAsprintf(&newname, *ifname, i);
+
+            if (virBufferError(&newname)) {
+                virBufferFreeAndReset(&newname);
+                virReportOOMError();
+                goto cleanup;
+            }
+
+            if (virNetDevExists(virBufferCurrentContent(&newname)) == 0) {
+                newifname = strdup(virBufferContentAndReset(&newname));
+                break;
+            }
+        }
+    }
+
+    VIR_FREE(*ifname);
+
+    if (newifname == NULL) {
+        virReportError(VIR_ERR_INTERNAL_ERROR,
+                       _("Failed to generate new name for interface %s"),
+                       ifr.ifr_name);
+        goto cleanup;
+    }
+
+    if (virNetDevSetName(ifr.ifr_name, newifname) == -1) {
+        goto cleanup;
+    }
+
+    *ifname = newifname;
+
+    if (tapfd) {
+        char *dev_path = NULL;
+        if (virAsprintf(&dev_path, "/dev/%s", *ifname) < 0) {
+            virReportOOMError();
+            goto cleanup;
+        }
+
+        *tapfd = open(dev_path, O_RDWR);
+
+        VIR_FREE(dev_path);
+    }
+
+    ret = 0;
+cleanup:
+    if (ret < 0)
+        VIR_FORCE_CLOSE(s);
+
+    return ret;
+}
+
+int virNetDevTapDelete(const char *ifname)
+{
+    int s;
+    struct ifreq ifr;
+    int ret = -1;
+
+    if ((s = virNetDevSetupControl(ifname, &ifr)) < 0)
+        return -1;
+
+    if (ioctl(s, SIOCIFDESTROY, &ifr) < 0) {
+        virReportSystemError(errno,
+                             _("Unable to remove tap device %s"),
+                             ifname);
+        goto cleanup;
+    }
+
+    ret = 0;
+cleanup:
+    VIR_FORCE_CLOSE(s);
+    return ret;
+}
+
+#else /* !defined(__FreeBSD__) */
 int virNetDevTapCreate(char **ifname ATTRIBUTE_UNUSED,
                        int *tapfd ATTRIBUTE_UNUSED,
                        unsigned int flags ATTRIBUTE_UNUSED)
@@ -245,7 +351,7 @@ int virNetDevTapDelete(const char *ifname ATTRIBUTE_UNUSED)
                          _("Unable to delete TAP devices on this platform"));
     return -1;
 }
-#endif /* ! TUNSETIFF */
+#endif
 
 
 /**
-- 
1.8.0

--
libvir-list mailing list
libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list


[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]