[libvirt] [PATCH] domain_conf: set ifname for ovs vhostuser ifname

Mehdi Abaakouk sileht at sileht.net
Fri Dec 9 12:24:53 UTC 2016


This change adds a new helper function that
return a ifname from the vhostuser unix-socket path but only
if the interface is managed by openvswitch

This new function is issue to autodetect the ifname for openvswitch
managed vhostuser interface.
---
 src/conf/domain_conf.c          |  5 +++++
 src/libvirt_private.syms        |  1 +
 src/util/virnetdevopenvswitch.c | 41 +++++++++++++++++++++++++++++++++++++++++
 src/util/virnetdevopenvswitch.h |  3 +++
 4 files changed, 50 insertions(+)

diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c
index 7715cad..178208e 100644
--- a/src/conf/domain_conf.c
+++ b/src/conf/domain_conf.c
@@ -55,6 +55,7 @@
 #include "virtpm.h"
 #include "virstring.h"
 #include "virnetdev.h"
+#include "virnetdevopenvswitch.h"
 #include "virhostdev.h"
 
 #define VIR_FROM_THIS VIR_FROM_DOMAIN
@@ -9582,6 +9583,10 @@ virDomainNetDefParseXML(virDomainXMLOptionPtr xmlopt,
 
         def->data.vhostuser->type = VIR_DOMAIN_CHR_TYPE_UNIX;
         def->data.vhostuser->data.nix.path = vhostuser_path;
+
+        if (!ifname)
+            ifname = virNetDevOpenvswitchGetVhostuserIfname(vhostuser_path);
+
         vhostuser_path = NULL;
 
         if (STREQ(vhostuser_mode, "server")) {
diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index 867acdb..1b3d8d4 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -2047,6 +2047,7 @@ virNetDevMidonetUnbindPort;
 # util/virnetdevopenvswitch.h
 virNetDevOpenvswitchAddPort;
 virNetDevOpenvswitchGetMigrateData;
+virNetDevOpenvswitchGetVhostuserIfname;
 virNetDevOpenvswitchInterfaceStats;
 virNetDevOpenvswitchRemovePort;
 virNetDevOpenvswitchSetMigrateData;
diff --git a/src/util/virnetdevopenvswitch.c b/src/util/virnetdevopenvswitch.c
index f003b3b..94b730b 100644
--- a/src/util/virnetdevopenvswitch.c
+++ b/src/util/virnetdevopenvswitch.c
@@ -377,3 +377,44 @@ virNetDevOpenvswitchInterfaceStats(const char *ifname,
     virCommandFree(cmd);
     return ret;
 }
+
+/**
+ * virNetDevOpenvswitchVhostuserGetIfname:
+ * @path: the path of the unix socket
+ *
+ * Retreives the ovs ifname from vhostuser unix socket path
+ *
+ * Returns a new string which must be deallocated by caller or NULL
+ */
+char *
+virNetDevOpenvswitchGetVhostuserIfname(const char *path)
+{
+    virCommandPtr cmd = NULL;
+    char *ifname = NULL;
+    char **tokens = NULL;
+    size_t ntokens = 0;
+
+    /* Openvswitch vhostuser path are hardcoded to
+     * /<runstatedir>/openvswitch/<ifname>
+     * for example: /var/run/openvswitch/dpdkvhostuser0
+     *
+     * so we pick the filename and check it's a openvswitch interface
+     */
+    if ((tokens = virStringSplitCount(path, "/", 0, &ntokens))) {
+         if (VIR_STRDUP(ifname, tokens[ntokens - 1]) < 0)
+             goto cleanup;
+    }
+    if (ifname != NULL) {
+        cmd = virCommandNewArgList(OVSVSCTL, "--timeout=5", "get", "Interface",
+                                   ifname, "name", NULL);
+        if (virCommandRun(cmd, NULL) < 0) {
+            /* it's not a openvswitch vhostuser interface */
+            VIR_FREE(ifname);
+        }
+    }
+
+ cleanup:
+    virStringListFreeCount(tokens, ntokens);
+    return ifname;
+
+}
diff --git a/src/util/virnetdevopenvswitch.h b/src/util/virnetdevopenvswitch.h
index 0f9e1df..fe86e28 100644
--- a/src/util/virnetdevopenvswitch.h
+++ b/src/util/virnetdevopenvswitch.h
@@ -52,4 +52,7 @@ int virNetDevOpenvswitchInterfaceStats(const char *ifname,
                                        virDomainInterfaceStatsPtr stats)
     ATTRIBUTE_NONNULL(1) ATTRIBUTE_RETURN_CHECK;
 
+char *virNetDevOpenvswitchGetVhostuserIfname(const char *path)
+    ATTRIBUTE_NONNULL(1) ATTRIBUTE_RETURN_CHECK;
+
 #endif /* __VIR_NETDEV_OPENVSWITCH_H__ */
-- 
2.10.2




More information about the libvir-list mailing list