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/libvirt_private.syms | 1 +
src/qemu/qemu_domain.c | 21 +++++++++++++++------
src/util/virnetdevopenvswitch.c | 42 +++++++++++++++++++++++++++++++++++++++++
src/util/virnetdevopenvswitch.h | 3 +++
4 files changed, 61 insertions(+), 6 deletions(-)
diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index 2d23e462d..9e4e8f83f 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -2057,6 +2057,7 @@ virNetDevMidonetUnbindPort;
# util/virnetdevopenvswitch.h
virNetDevOpenvswitchAddPort;
virNetDevOpenvswitchGetMigrateData;
+virNetDevOpenvswitchGetVhostuserIfname;
virNetDevOpenvswitchInterfaceStats;
virNetDevOpenvswitchRemovePort;
virNetDevOpenvswitchSetMigrateData;
diff --git a/src/qemu/qemu_domain.c b/src/qemu/qemu_domain.c
index acfa69589..7ae487821 100644
--- a/src/qemu/qemu_domain.c
+++ b/src/qemu/qemu_domain.c
@@ -41,6 +41,7 @@
#include "domain_addr.h"
#include "domain_event.h"
#include "virtime.h"
+#include "virnetdevopenvswitch.h"
#include "virstoragefile.h"
#include "virstring.h"
#include "virthreadjob.h"
@@ -3028,12 +3029,20 @@ qemuDomainDeviceDefPostParse(virDomainDeviceDefPtr dev,
def->emulator);
}
- if (dev->type == VIR_DOMAIN_DEVICE_NET &&
- dev->data.net->type != VIR_DOMAIN_NET_TYPE_HOSTDEV &&
- !dev->data.net->model) {
- if (VIR_STRDUP(dev->data.net->model,
- qemuDomainDefaultNetModel(def, qemuCaps)) < 0)
- goto cleanup;
+ if (dev->type == VIR_DOMAIN_DEVICE_NET) {
+ if (dev->data.net->type != VIR_DOMAIN_NET_TYPE_HOSTDEV &&
+ !dev->data.net->model) {
+ if (VIR_STRDUP(dev->data.net->model,
+ qemuDomainDefaultNetModel(def, qemuCaps)) < 0)
+ goto cleanup;
+ }
+ if (dev->data.net->type == VIR_DOMAIN_NET_TYPE_VHOSTUSER &&
+ !dev->data.net->ifname) {
+ if (VIR_STRDUP(dev->data.net->ifname,
+ virNetDevOpenvswitchGetVhostuserIfname(
+ dev->data.net->data.vhostuser->data.nix.path)) < 0)
+ goto cleanup;
+ }
}
/* set default disk types and drivers */
diff --git a/src/util/virnetdevopenvswitch.c b/src/util/virnetdevopenvswitch.c
index f003b3b13..41933ce28 100644
--- a/src/util/virnetdevopenvswitch.c
+++ b/src/util/virnetdevopenvswitch.c
@@ -377,3 +377,45 @@ 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);
+ virCommandFree(cmd);
+ return ifname;
+
+}
diff --git a/src/util/virnetdevopenvswitch.h b/src/util/virnetdevopenvswitch.h
index 0f9e1dfa6..fe86e286f 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.11.0