diff mbox series

[7/7] qga: Add HW address getting for FreeBSD

Message ID 20220929075239.1675374-8-alexander.ivanov@virtuozzo.com (mailing list archive)
State New, archived
Headers show
Series qga: Add FreeBSD support | expand

Commit Message

Alexander Ivanov Sept. 29, 2022, 7:52 a.m. UTC
Replace a dumb function in commands-bsd.c by the code of HW address
getting.

Signed-off-by: Alexander Ivanov <alexander.ivanov@virtuozzo.com>
---
 qga/commands-bsd.c | 17 ++++++++++++-----
 1 file changed, 12 insertions(+), 5 deletions(-)

Comments

Marc-André Lureau Sept. 29, 2022, 11:28 a.m. UTC | #1
Hi

On Thu, Sep 29, 2022 at 11:54 AM Alexander Ivanov <
alexander.ivanov@virtuozzo.com> wrote:

> Replace a dumb function in commands-bsd.c by the code of HW address
> getting.
>
> Signed-off-by: Alexander Ivanov <alexander.ivanov@virtuozzo.com>
>

lgtm
Reviewed-by: Marc-André Lureau <marcandre.lureau@redhat.com>



> ---
>  qga/commands-bsd.c | 17 ++++++++++++-----
>  1 file changed, 12 insertions(+), 5 deletions(-)
>
> diff --git a/qga/commands-bsd.c b/qga/commands-bsd.c
> index ca81114171..f9997ee1ec 100644
> --- a/qga/commands-bsd.c
> +++ b/qga/commands-bsd.c
> @@ -20,6 +20,8 @@
>  #include <sys/param.h>
>  #include <sys/ucred.h>
>  #include <sys/mount.h>
> +#include <net/if_dl.h>
> +#include <net/ethernet.h>
>  #include <paths.h>
>
>  #if defined(CONFIG_FSFREEZE) || defined(CONFIG_FSTRIM)
> @@ -178,10 +180,15 @@ GuestCpuStatsList *qmp_guest_get_cpustats(Error
> **errp)
>   */
>  int guest_get_hw_addr(struct ifaddrs *ifa, unsigned char *buf, Error
> **errp)
>  {
> -    /*
> -     * We can't get the hw addr of this interface,
> -     * but that's not a fatal error.
> -     */
> -    return 0;
> +    struct sockaddr_dl *sdp;
> +
> +    if (ifa->ifa_addr->sa_family != AF_LINK) {
> +        /* We can get HW address only for AF_LINK family */
> +        g_debug("failed to get MAC address of %s", ifa->ifa_name);
> +        return 0;
> +    }
> +    sdp = (struct sockaddr_dl *)ifa->ifa_addr;
> +    memcpy(buf, sdp->sdl_data + sdp->sdl_nlen, ETHER_ADDR_LEN);
> +    return 1;
>  }
>  #endif /* HAVE_GETIFADDRS */
> --
> 2.34.1
>
>
>
diff mbox series

Patch

diff --git a/qga/commands-bsd.c b/qga/commands-bsd.c
index ca81114171..f9997ee1ec 100644
--- a/qga/commands-bsd.c
+++ b/qga/commands-bsd.c
@@ -20,6 +20,8 @@ 
 #include <sys/param.h>
 #include <sys/ucred.h>
 #include <sys/mount.h>
+#include <net/if_dl.h>
+#include <net/ethernet.h>
 #include <paths.h>
 
 #if defined(CONFIG_FSFREEZE) || defined(CONFIG_FSTRIM)
@@ -178,10 +180,15 @@  GuestCpuStatsList *qmp_guest_get_cpustats(Error **errp)
  */
 int guest_get_hw_addr(struct ifaddrs *ifa, unsigned char *buf, Error **errp)
 {
-    /*
-     * We can't get the hw addr of this interface,
-     * but that's not a fatal error.
-     */
-    return 0;
+    struct sockaddr_dl *sdp;
+
+    if (ifa->ifa_addr->sa_family != AF_LINK) {
+        /* We can get HW address only for AF_LINK family */
+        g_debug("failed to get MAC address of %s", ifa->ifa_name);
+        return 0;
+    }
+    sdp = (struct sockaddr_dl *)ifa->ifa_addr;
+    memcpy(buf, sdp->sdl_data + sdp->sdl_nlen, ETHER_ADDR_LEN);
+    return 1;
 }
 #endif /* HAVE_GETIFADDRS */