From 1b46566eed0c9c23251d05c8fedb3522d711f5c0 Mon Sep 17 00:00:00 2001 From: Laine Stump Date: Mon, 26 Aug 2019 01:24:08 -0400 Subject: [PATCH] util: new function virNetDevMacVLanIsMacvtap() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This function returns T if the given name is a macvtap device. This is determined by 1) getting the ifindex of the device with that name (if there is one), and 2) checking for existence of /dev/tapXX, where "XX" is the ifindex learned in (1). It's also possible to learn this by getting a netlink dump of the interface and parsing through it to look for some attributes, but that is complicated to figure out, takes longer to execute, and I'm lazy. Signed-off-by: Laine Stump Reviewed-by: Daniel P. Berrangé --- src/libvirt_private.syms | 1 + src/util/virnetdevmacvlan.c | 23 +++++++++++++++++++++++ src/util/virnetdevmacvlan.h | 3 +++ 3 files changed, 27 insertions(+) diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index f1fe7259f9..7adb07aac0 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -2524,6 +2524,7 @@ virNetDevMacVLanCreate; virNetDevMacVLanCreateWithVPortProfile; virNetDevMacVLanDelete; virNetDevMacVLanDeleteWithVPortProfile; +virNetDevMacVLanIsMacvtap; virNetDevMacVLanModeTypeFromString; virNetDevMacVLanReleaseName; virNetDevMacVLanReserveName; diff --git a/src/util/virnetdevmacvlan.c b/src/util/virnetdevmacvlan.c index 3302522289..79aa7ed5ac 100644 --- a/src/util/virnetdevmacvlan.c +++ b/src/util/virnetdevmacvlan.c @@ -278,6 +278,29 @@ virNetDevMacVLanReleaseName(const char *name) } +/** + * virNetDevMacVLanIsMacvtap: + * @ifname: Name of the interface + * + * Return T if the named netdev exists and is a macvtap device + * F in all other cases. + */ +bool +virNetDevMacVLanIsMacvtap(const char *ifname) +{ + int ifindex; + VIR_AUTOFREE(char *) tapname = NULL; + + if (virNetDevGetIndex(ifname, &ifindex) < 0) + return false; + + if (virAsprintf(&tapname, "/dev/tap%d", ifindex) < 0) + return false; + + return virFileExists(tapname); +} + + /** * virNetDevMacVLanCreate: * diff --git a/src/util/virnetdevmacvlan.h b/src/util/virnetdevmacvlan.h index d1b479ed9f..8ac7643e49 100644 --- a/src/util/virnetdevmacvlan.h +++ b/src/util/virnetdevmacvlan.h @@ -57,6 +57,9 @@ typedef enum { int virNetDevMacVLanReserveName(const char *name, bool quietfail); int virNetDevMacVLanReleaseName(const char *name); +bool virNetDevMacVLanIsMacvtap(const char *ifname) + ATTRIBUTE_NONNULL(1) ATTRIBUTE_RETURN_CHECK ATTRIBUTE_NOINLINE; + int virNetDevMacVLanCreate(const char *ifname, const char *type, const virMacAddr *macaddress,