[libvirt] [PATCH v2 4/5] util: add virNetDevGetName() function

Cédric Bosdonnat posted 5 patches 8 years, 11 months ago
[libvirt] [PATCH v2 4/5] util: add virNetDevGetName() function
Posted by Cédric Bosdonnat 8 years, 11 months ago
Add a function getting the name of a network interface out of its index.
---
 src/libvirt_private.syms |  1 +
 src/util/virnetdev.c     | 19 +++++++++++++++++++
 src/util/virnetdev.h     |  2 ++
 3 files changed, 22 insertions(+)

diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index 1f25e42d8..0fe88c3fa 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -1982,6 +1982,7 @@ virNetDevFeatureTypeToString;
 virNetDevGetFeatures;
 virNetDevGetIndex;
 virNetDevGetLinkInfo;
+virNetDevGetName;
 virNetDevGetMAC;
 virNetDevGetMTU;
 virNetDevGetOnline;
diff --git a/src/util/virnetdev.c b/src/util/virnetdev.c
index d12324878..91a5274aa 100644
--- a/src/util/virnetdev.c
+++ b/src/util/virnetdev.c
@@ -899,6 +899,25 @@ virNetDevGetRcvAllMulti(const char *ifname,
     return virNetDevGetIFFlag(ifname, VIR_IFF_ALLMULTI, receive);
 }
 
+char *virNetDevGetName(int ifindex)
+{
+    char name[IFNAMSIZ];
+    char *ifname = NULL;
+
+    memset(&name, 0, sizeof(name));
+
+    if (!if_indextoname(ifindex, name)) {
+        virReportSystemError(errno,
+                             _("Failed to convert interface index %d to a name"),
+                             ifindex);
+        goto cleanup;
+    }
+
+   ignore_value(VIR_STRDUP(ifname, name));
+
+ cleanup:
+     return ifname;
+}
 
 /**
  * virNetDevGetIndex:
diff --git a/src/util/virnetdev.h b/src/util/virnetdev.h
index 236cf83ef..01e9c5b95 100644
--- a/src/util/virnetdev.h
+++ b/src/util/virnetdev.h
@@ -157,6 +157,8 @@ int virNetDevSetNamespace(const char *ifname, pid_t pidInNs)
 int virNetDevSetName(const char *ifname, const char *newifname)
     ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_RETURN_CHECK;
 
+char *virNetDevGetName(int ifindex)
+    ATTRIBUTE_NONNULL(1) ATTRIBUTE_RETURN_CHECK;
 int virNetDevGetIndex(const char *ifname, int *ifindex)
     ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_RETURN_CHECK;
 
-- 
2.11.0

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list
Re: [libvirt] [PATCH v2 4/5] util: add virNetDevGetName() function
Posted by Laine Stump 8 years, 10 months ago
On 03/15/2017 10:45 AM, Cédric Bosdonnat wrote:
> Add a function getting the name of a network interface out of its index.
> ---
>  src/libvirt_private.syms |  1 +
>  src/util/virnetdev.c     | 19 +++++++++++++++++++
>  src/util/virnetdev.h     |  2 ++
>  3 files changed, 22 insertions(+)
> 
> diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
> index 1f25e42d8..0fe88c3fa 100644
> --- a/src/libvirt_private.syms
> +++ b/src/libvirt_private.syms
> @@ -1982,6 +1982,7 @@ virNetDevFeatureTypeToString;
>  virNetDevGetFeatures;
>  virNetDevGetIndex;
>  virNetDevGetLinkInfo;
> +virNetDevGetName;
>  virNetDevGetMAC;
>  virNetDevGetMTU;
>  virNetDevGetOnline;
> diff --git a/src/util/virnetdev.c b/src/util/virnetdev.c
> index d12324878..91a5274aa 100644
> --- a/src/util/virnetdev.c
> +++ b/src/util/virnetdev.c
> @@ -899,6 +899,25 @@ virNetDevGetRcvAllMulti(const char *ifname,
>      return virNetDevGetIFFlag(ifname, VIR_IFF_ALLMULTI, receive);
>  }
>  
> +char *virNetDevGetName(int ifindex)
> +{
> +    char name[IFNAMSIZ];
> +    char *ifname = NULL;
> +
> +    memset(&name, 0, sizeof(name));
> +
> +    if (!if_indextoname(ifindex, name)) {
> +        virReportSystemError(errno,
> +                             _("Failed to convert interface index %d to a name"),
> +                             ifindex);
> +        goto cleanup;
> +    }
> +
> +   ignore_value(VIR_STRDUP(ifname, name));
> +
> + cleanup:
> +     return ifname;
> +}


This is defined outside of any #ifdefs, which I *think* is okay, since
the function exists in both Linux and FreeBSD. I guess if anybody
complains then we can think about testing for the function at configure
time, but no sense in complicating configure.ac if we don't need to...

ACK.

(BTW, although I didn't have a need for this function when I suggested
it, just today I was writing some code that will need it! :-)


>  
>  /**
>   * virNetDevGetIndex:
> diff --git a/src/util/virnetdev.h b/src/util/virnetdev.h
> index 236cf83ef..01e9c5b95 100644
> --- a/src/util/virnetdev.h
> +++ b/src/util/virnetdev.h
> @@ -157,6 +157,8 @@ int virNetDevSetNamespace(const char *ifname, pid_t pidInNs)
>  int virNetDevSetName(const char *ifname, const char *newifname)
>      ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_RETURN_CHECK;
>  
> +char *virNetDevGetName(int ifindex)
> +    ATTRIBUTE_NONNULL(1) ATTRIBUTE_RETURN_CHECK;
>  int virNetDevGetIndex(const char *ifname, int *ifindex)
>      ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_RETURN_CHECK;
>  
> 

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list
Re: [libvirt] [PATCH v2 4/5] util: add virNetDevGetName() function
Posted by Laine Stump 8 years, 10 months ago
On 03/15/2017 10:45 AM, Cédric Bosdonnat wrote:
> Add a function getting the name of a network interface out of its index.
> ---
>  src/libvirt_private.syms |  1 +
>  src/util/virnetdev.c     | 19 +++++++++++++++++++
>  src/util/virnetdev.h     |  2 ++
>  3 files changed, 22 insertions(+)
> 
> diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
> index 1f25e42d8..0fe88c3fa 100644
> --- a/src/libvirt_private.syms
> +++ b/src/libvirt_private.syms
> @@ -1982,6 +1982,7 @@ virNetDevFeatureTypeToString;
>  virNetDevGetFeatures;
>  virNetDevGetIndex;
>  virNetDevGetLinkInfo;
> +virNetDevGetName;
>  virNetDevGetMAC;
>  virNetDevGetMTU;
>  virNetDevGetOnline;

Oops! I just now noticed (when I ran make check after adding this patch
locally) that you have the ordering mixed up here - virNetDevGetName
should be just after virNetDevGetMTU. Please fix that before pushing.


> diff --git a/src/util/virnetdev.c b/src/util/virnetdev.c
> index d12324878..91a5274aa 100644
> --- a/src/util/virnetdev.c
> +++ b/src/util/virnetdev.c
> @@ -899,6 +899,25 @@ virNetDevGetRcvAllMulti(const char *ifname,
>      return virNetDevGetIFFlag(ifname, VIR_IFF_ALLMULTI, receive);
>  }
>  
> +char *virNetDevGetName(int ifindex)
> +{
> +    char name[IFNAMSIZ];
> +    char *ifname = NULL;
> +
> +    memset(&name, 0, sizeof(name));
> +
> +    if (!if_indextoname(ifindex, name)) {
> +        virReportSystemError(errno,
> +                             _("Failed to convert interface index %d to a name"),
> +                             ifindex);
> +        goto cleanup;
> +    }
> +
> +   ignore_value(VIR_STRDUP(ifname, name));
> +
> + cleanup:
> +     return ifname;
> +}
>  
>  /**
>   * virNetDevGetIndex:
> diff --git a/src/util/virnetdev.h b/src/util/virnetdev.h
> index 236cf83ef..01e9c5b95 100644
> --- a/src/util/virnetdev.h
> +++ b/src/util/virnetdev.h
> @@ -157,6 +157,8 @@ int virNetDevSetNamespace(const char *ifname, pid_t pidInNs)
>  int virNetDevSetName(const char *ifname, const char *newifname)
>      ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_RETURN_CHECK;
>  
> +char *virNetDevGetName(int ifindex)
> +    ATTRIBUTE_NONNULL(1) ATTRIBUTE_RETURN_CHECK;
>  int virNetDevGetIndex(const char *ifname, int *ifindex)
>      ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_RETURN_CHECK;
>  
> 

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list
Re: [libvirt] [PATCH v2 4/5] util: add virNetDevGetName() function
Posted by John Ferlan 8 years, 10 months ago

On 03/15/2017 10:45 AM, Cédric Bosdonnat wrote:

[...]

>  
>  /**
>   * virNetDevGetIndex:
> diff --git a/src/util/virnetdev.h b/src/util/virnetdev.h
> index 236cf83ef..01e9c5b95 100644
> --- a/src/util/virnetdev.h
> +++ b/src/util/virnetdev.h
> @@ -157,6 +157,8 @@ int virNetDevSetNamespace(const char *ifname, pid_t pidInNs)
>  int virNetDevSetName(const char *ifname, const char *newifname)
>      ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_RETURN_CHECK;
>  
> +char *virNetDevGetName(int ifindex)
> +    ATTRIBUTE_NONNULL(1) ATTRIBUTE_RETURN_CHECK;

ATTRIBUTE_NONNULL should have been removed - (un)fortunately not seen in
builds, but my Coverity build fails.

John

>  int virNetDevGetIndex(const char *ifname, int *ifindex)
>      ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_RETURN_CHECK;
>  
> 

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