On 03/25/2013 10:25 AM, Paolo Bonzini wrote:
This will be used on a tap file descriptor returned by the bridge
helper
to populate the <target> element, because the helper does not provide
the interface name.
Signed-off-by: Paolo Bonzini <pbonzini(a)redhat.com>
---
src/libvirt_private.syms | 1 +
src/util/virnetdevtap.c | 33 +++++++++++++++++++++++++++++++++
src/util/virnetdevtap.h | 3 +++
3 files changed, 37 insertions(+)
diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index f241ec4..06085ba 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -1515,6 +1515,7 @@ virNetDevOpenvswitchSetMigrateData;
virNetDevTapCreate;
virNetDevTapCreateInBridgePort;
virNetDevTapDelete;
+virNetDevTapGetName;
# util/virnetdevveth.h
diff --git a/src/util/virnetdevtap.c b/src/util/virnetdevtap.c
index a884de1..e9fddf1 100644
--- a/src/util/virnetdevtap.c
+++ b/src/util/virnetdevtap.c
@@ -44,6 +44,39 @@
#define VIR_FROM_THIS VIR_FROM_NONE
/**
+ * virNetDevTapGetName:
+ * @tapfd: a tun/tap file descriptor
+ * @ifname: a pointer that will receive the interface name
+ *
+ * Retrieve the interface name given a file descriptor for a tun/tap
+ * interface.
+ *
+ * Returns 0 if the interface name is successfully queried, -1 otherwise
+ */
+int
+virNetDevTapGetName(int tapfd, char **ifname)
+{
+#ifdef TUNGETIFF
+ struct ifreq ifr;
+
+ /* The kernel will always return -1 at this point.
+ * If TUNGETIFF is not implemented then errno == EBADFD.
+ */
+ if (ioctl(tapfd, TUNGETIFF, &ifr) < 0) {
+ virReportSystemError(errno, "%s",
+ _("Unable to query tap interface name"));
+ return -1;
+ }
+
+ *ifname = strdup(ifr.ifr_name);
You need to check for OOM here. (actually there are patches pending
somewhere to replace strdup() with a VIR_STRDUP() or something like that
which does the error reporting for you, but you'll still need to return
-1 if it fails).
+ return 0;
+#else
+ return -1;
+#endif
+}
+
+
+/**
* virNetDevProbeVnetHdr:
* @tapfd: a tun/tap file descriptor
*
diff --git a/src/util/virnetdevtap.h b/src/util/virnetdevtap.h
index 980db61..6bfc80c 100644
--- a/src/util/virnetdevtap.h
+++ b/src/util/virnetdevtap.h
@@ -35,6 +35,9 @@ int virNetDevTapCreate(char **ifname,
int virNetDevTapDelete(const char *ifname)
ATTRIBUTE_NONNULL(1) ATTRIBUTE_RETURN_CHECK;
+int virNetDevTapGetName(int tapfd, char **ifname)
+ ATTRIBUTE_NONNULL(2) ATTRIBUTE_RETURN_CHECK;
+
typedef enum {
VIR_NETDEV_TAP_CREATE_NONE = 0,
/* Bring the interface up */