qga: Implement guest-network-get-interfaces for Solaris
cover qga: Implement guest-network-get-interfaces for Solaris
This implements the guest agent guest-network-get-interfaces command on Solaris. Solaris provides a getifaddrs() that's very similar to the Linux one, so the implementation is mostly the same.
Andrew Deason (3):
qga/commands-posix: Use getifaddrs when available qga/commands-posix: Fix iface hw address detection qga/commands-posix: Fix listing ifaces for Solaris
meson.build | 1 + qga/commands-posix.c | 488 +++++++++++++++++++++++++++------------------------ 2 files changed, 260 insertions(+), 229 deletions(-)
Signature · 1 line
2.11.0
PATCH 1/3 qga/commands-posix: Use getifaddrs when available
Currently, commands-posix.c assumes that getifaddrs() is only available on Linux, and so the related guest agent command guest-network-get-interfaces is only implemented for #ifdef __linux__. This function does exist on other platforms, though, such as Solaris. So, add a meson check for getifaddrs(), and move the code for guest-network-get-interfaces to be built whenever getifaddrs() is available.
The implementation for guest-network-get-interfaces still has some Linux-specific code, which is not fixed in this commit. This commit moves the relevant big chunks of code around without changing them, so a future commit can change the code in place.
--- meson.build | 1 + qga/commands-posix.c | 474 ++++++++++++++++++++++++++------------------------- 2 files changed, 246 insertions(+), 229 deletions(-)
meson.build +1 -0
@@ -1633,20 +1633,21 @@ config_host_data.set('CONFIG_MEMALIGN', cc.has_function('memalign')) config_host_data.set('CONFIG_PPOLL', cc.has_function('ppoll')) config_host_data.set('CONFIG_PREADV', cc.has_function('preadv', prefix: '#include <sys/uio.h>')) config_host_data.set('CONFIG_PTHREAD_FCHDIR_NP', cc.has_function('pthread_fchdir_np')) config_host_data.set('CONFIG_SEM_TIMEDWAIT', cc.has_function('sem_timedwait', dependencies: threads)) config_host_data.set('CONFIG_SENDFILE', cc.has_function('sendfile')) config_host_data.set('CONFIG_SETNS', cc.has_function('setns') and cc.has_function('unshare')) config_host_data.set('CONFIG_SYNCFS', cc.has_function('syncfs')) config_host_data.set('CONFIG_SYNC_FILE_RANGE', cc.has_function('sync_file_range')) config_host_data.set('CONFIG_TIMERFD', cc.has_function('timerfd_create')) config_host_data.set('HAVE_COPY_FILE_RANGE', cc.has_function('copy_file_range')) +config_host_data.set('HAVE_GETIFADDRS', cc.has_function('getifaddrs')) config_host_data.set('HAVE_OPENPTY', cc.has_function('openpty', dependencies: util)) config_host_data.set('HAVE_STRCHRNUL', cc.has_function('strchrnul')) config_host_data.set('HAVE_SYSTEM_FUNCTION', cc.has_function('system', prefix: '#include <stdlib.h>')) if rdma.found() config_host_data.set('HAVE_IBV_ADVISE_MR', cc.has_function('ibv_advise_mr', args: config_host['RDMA_LIBS'].split(), prefix: '#include <infiniband/verbs.h>')) endif
qga/commands-posix.c +245 -229
599 lines not on this page · raw message
Signature · 1 line
2.11.0
PATCH 2/3 qga/commands-posix: Fix iface hw address detection
In reply to Andrew DeasonSince its introduction in commit 3424fc9f16a1 ("qemu-ga: add guest-network-get-interfaces command"), guest-network-get-interfaces seems to check if a given interface has a hardware address by checking 'ifa->ifa_flags & SIOCGIFHWADDR'. But ifa_flags is a field for IFF_* flags (IFF_UP, IFF_LOOPBACK, etc), and comparing it to an ioctl like SIOCGIFHWADDR doesn't make sense.
On Linux, this isn't a big deal, since SIOCGIFHWADDR has so many bits set (0x8927), 'ifa->ifa_flags & SIOCGIFHWADDR' will usually have a nonzero result for any 'normal'-looking interfaces: anything with IFF_UP (0x1) or IFF_BROADCAST (0x2) set, as well as several less-common flags. This means we'll try to get the hardware address for most/all interfaces, even those that don't really have one (like the loopback device). For those interfaces, Linux just returns a hardware address of all zeroes.
On Solaris, however, trying to get the hardware address for a loopback device returns an EADDRNOTAVAIL error. This causes us to return an error and the entire guest-network-get-interfaces call fails.
Change this logic to always try to get the hardware address for each interface, and don't return an error if we fail to get it. Instead, just don't include the 'hardware-address' field in the result if we can't get the hardware address.
---
As implemented, this commit results in a behavior difference between Linux and Solaris. It's fixable, but I'm not sure which behavior we should have, or if it's okay that they behave differently:
On Linux, we've always returned the MAC address for loopback as all zeroes. In this commit, on Solaris we don't return a MAC address at all for loopback (because trying to get the MAC for lo results in an error).
It doesn't have to be like this; the behavior could be made the same if we wanted, by either:
- Making it so failing to get the hw address falls back to providing an all-zeroes address (making Solaris look more like Linux).
- Stop providing a 'fake' all-zeroes hw address on Linux (arguably more accurate, but harder to detect, and different than past behavior).
Or we could just leave the behavior as it is and have the two behave slightly differently; we're merely reflecting what the underlying platform does, and I'm not sure anyone cares about the difference. I assume this applies to any network interface that doesn't have a hardware address, but loopback is the only one I've seen on the simple test VMs I've been using so far.
qga/commands-posix.c | 39 ++++++++++++++++++++++++--------------- 1 file changed, 24 insertions(+), 15 deletions(-)
qga/commands-posix.c +24 -15
@@ -2875,48 +2875,57 @@ GuestNetworkInterfaceList *qmp_guest_network_get_interfaces(Error **errp) info =guest_find_interface (head, ifa->ifa_name);if (!info) { info =g_malloc0 (sizeof (*info)); info->name =g_strdup (ifa->ifa_name);QAPI_LIST_APPEND (tail, info); } -if (!info->has_hardware_address && ifa->ifa_flags &SIOCGIFHWADDR ) { +if (!info->has_hardware_address) {/* we haven't obtained HW address yet */ sock =socket (PF_INET ,SOCK_STREAM ,0 );if (sock ==-1 ) {error_setg_errno (errp, errno,"failed to create socket" ); goto error; }memset (&ifr,0 ,sizeof (ifr));pstrcpy (ifr.ifr_name,IF_NAMESIZE , info->name);if (ioctl (sock,SIOCGIFHWADDR , &ifr) ==-1 ) { -error_setg_errno (errp, errno, -"failed to get MAC address of %s" , - ifa->ifa_name); -close (sock); - goto error; - } +/* +* We can't get the hw addr of this interface, but that's not a +* fatal error. Don't set info->hardware_address, but keep +* going. +*/ +if (errno ==EADDRNOTAVAIL ) { +/* The interface doesn't have a hw addr (e.g. loopback). */ +g_debug ("failed to get MAC address of %s: %s" , + ifa->ifa_name,strerror (errno)); + }else { +g_warning ("failed to get MAC address of %s: %s" , + ifa->ifa_name,strerror (errno)); + } -close (sock); - mac_addr = (unsigned char *) &ifr.ifr_hwaddr.sa_data; + }else { + mac_addr = (unsigned char *) &ifr.ifr_hwaddr.sa_data; - info->hardware_address = -g_strdup_printf ("%02x:%02x:%02x:%02x:%02x:%02x" , - (int ) mac_addr[0 ], (int ) mac_addr[1 ], - (int ) mac_addr[2 ], (int ) mac_addr[3 ], - (int ) mac_addr[4 ], (int ) mac_addr[5 ]); + info->hardware_address = +g_strdup_printf ("%02x:%02x:%02x:%02x:%02x:%02x" , + (int ) mac_addr[0 ], (int ) mac_addr[1 ], + (int ) mac_addr[2 ], (int ) mac_addr[3 ], + (int ) mac_addr[4 ], (int ) mac_addr[5 ]); - info->has_hardware_address = true; + info->has_hardware_address = true; + } +close (sock); }if (ifa->ifa_addr && ifa->ifa_addr->sa_family ==AF_INET ) {/* interface with IPv4 address */ p = &((struct sockaddr_in *)ifa->ifa_addr)->sin_addr;if (!inet_ntop (AF_INET , p, addr4,sizeof (addr4))) {error_setg_errno (errp, errno,"inet_ntop failed" ); goto error; }
Signature · 1 line
2.11.0
PATCH 3/3 qga/commands-posix: Fix listing ifaces for Solaris
In reply to Andrew DeasonThe code for guest-network-get-interfaces needs a couple of small adjustments for Solaris:
- The results from SIOCGIFHWADDR are documented as being in ifr_addr, not ifr_hwaddr (ifr_hwaddr doesn't exist on Solaris).
- The implementation of guest_get_network_stats is Linux-specific, so hide it under #ifdef CONFIG_LINUX. On non-Linux, we just won't provide network interface stats.
--- qga/commands-posix.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)
qga/commands-posix.c +6 -1
@@ -2781,20 +2781,21 @@ guest_find_interface(GuestNetworkInterfaceList *head,return head->value; } }return NULL ; }static int guest_get_network_stats (const char *name,GuestNetworkInterfaceStat *stats) { +#ifdef CONFIG_LINUX int name_len;char const *devinfo ="/proc/net/dev" ;FILE *fp;char *line =NULL , *colon;size_t n =0 ; fp =fopen (devinfo,"r" );if (!fp) {return -1 ; } name_len =strlen (name);
@@ -2836,20 +2837,21 @@ static int guest_get_network_stats(const char *name, stats->tx_errs = tx_errs; stats->tx_dropped = tx_dropped;fclose (fp);g_free (line); return 0; } }fclose (fp);g_free (line);g_debug ("/proc/net/dev: Interface '%s' not found" , name); +#endif /* CONFIG_LINUX */ return -1 ; }/* * Build information about guest interfaces */ GuestNetworkInterfaceList *qmp_guest_network_get_interfaces (Error **errp) {GuestNetworkInterfaceList *head =NULL , **tail = &head;struct ifaddrs *ifap, *ifa;
@@ -2901,22 +2903,25 @@ GuestNetworkInterfaceList *qmp_guest_network_get_interfaces(Error **errp)if (errno ==EADDRNOTAVAIL ) {/* The interface doesn't have a hw addr (e.g. loopback). */ g_debug ("failed to get MAC address of %s: %s" , ifa->ifa_name,strerror (errno)); }else {g_warning ("failed to get MAC address of %s: %s" , ifa->ifa_name,strerror (errno)); } }else { +#ifdef CONFIG_SOLARIS + mac_addr = (unsigned char *) &ifr.ifr_addr.sa_data; +#else mac_addr = (unsigned char *) &ifr.ifr_hwaddr.sa_data; - +#endif info->hardware_address =g_strdup_printf ("%02x:%02x:%02x:%02x:%02x:%02x" , (int ) mac_addr[0 ], (int ) mac_addr[1 ], (int ) mac_addr[2 ], (int ) mac_addr[3 ], (int ) mac_addr[4 ], (int ) mac_addr[5 ]); info->has_hardware_address = true; }close (sock); }
Signature · 1 line
2.11.0
Re: [PATCH 3/3] qga/commands-posix: Fix listing ifaces for Solaris
Quoted 43 lines · Andrew Deason
The code for guest-network-get-interfaces needs a couple of small adjustments for Solaris:
- The results from SIOCGIFHWADDR are documented as being in
ifr_addr, notifr_hwaddr(ifr_hwaddrdoesn't exist on Solaris).- The implementation of
guest_get_network_statsis Linux-specific, so hide it under #ifdefCONFIG_LINUX. On non-Linux, we just won't provide network interface stats.Signed-off-by Andrew Deason <[email protected]>--- qga/commands-posix.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)qga/commands-posix.c +1
-0@@ -2781,20 +2781,21 @@ guest_find_interface(GuestNetworkInterfaceList *head,return head->value; } }return NULL ; }static int guest_get_network_stats (const char *name,GuestNetworkInterfaceStat *stats) { +#ifdef CONFIG_LINUX int name_len;char const *devinfo ="/proc/net/dev" ;FILE *fp;char *line =NULL , *colon;size_t n =0 ; fp =fopen (devinfo,"r" );if (!fp) {return -1 ; } name_len =strlen (name);@@ -2836,20 +2837,21 @@ static int guest_get_network_stats(const char *name, stats->tx_errs = tx_errs;
qga/commands-posix.c static int guest_get_network_stats(const char *name, · line 2847
Quoted 10 lines
qga/commands-posix.c +1
-0stats->tx_dropped = tx_dropped;fclose (fp);g_free (line); return 0; } }fclose (fp);g_free (line);g_debug ("/proc/net/dev: Interface '%s' not found" , name); +#endif /* CONFIG_LINUX */
I wonder whether we should signal this somehow. I mean, have something like this:
#else /* !CONFIG_LINUX */
g_debug("Stats reporting available only for Linux");
#endif /* !CONFIG_LINUX */
qga/commands-posix.c static int guest_get_network_stats(const char *name, · line 2849
Quoted 2 lines
qga/commands-posix.c +0
-0return -1 ; }
A counter argument is that if fopen() above fails then -1 is returned without any error/debug message reported. And stats fetching is best effort anyway.
Michal
Re: [PATCH 3/3] qga/commands-posix: Fix listing ifaces for Solaris
Quoted 62 lines · On Mon, 21 Mar 2022 10:14:57 +0100 Michal Prívozník <[email protected]> wrote:
Quoted 43 lines · Andrew Deason
The code for guest-network-get-interfaces needs a couple of small adjustments for Solaris:
- The results from SIOCGIFHWADDR are documented as being in
ifr_addr, notifr_hwaddr(ifr_hwaddrdoesn't exist on Solaris).- The implementation of
guest_get_network_statsis Linux-specific, so hide it under #ifdefCONFIG_LINUX. On non-Linux, we just won't provide network interface stats.Signed-off-by Andrew Deason <[email protected]>--- qga/commands-posix.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)qga/commands-posix.c +1
-0@@ -2781,20 +2781,21 @@ guest_find_interface(GuestNetworkInterfaceList *head,return head->value; } }return NULL ; }static int guest_get_network_stats (const char *name,GuestNetworkInterfaceStat *stats) { +#ifdef CONFIG_LINUX int name_len;char const *devinfo ="/proc/net/dev" ;FILE *fp;char *line =NULL , *colon;size_t n =0 ; fp =fopen (devinfo,"r" );if (!fp) {return -1 ; } name_len =strlen (name);@@ -2836,20 +2837,21 @@ static int guest_get_network_stats(const char *name, stats->tx_errs = tx_errs;qga/commands-posix.c static int guest_get_network_stats(const char *name, · line 2847
Quoted 10 lines
qga/commands-posix.c +1
-0stats->tx_dropped = tx_dropped;fclose (fp);g_free (line); return 0; } }fclose (fp);g_free (line);g_debug ("/proc/net/dev: Interface '%s' not found" , name); +#endif /* CONFIG_LINUX */ I wonder whether we should signal this somehow. I mean, have something like this:
#else /* !CONFIG_LINUX */
g_debug("Stats reporting available only for Linux");
#endif /* !CONFIG_LINUX */
Quoted 6 lines
qga/commands-posix.c static int guest_get_network_stats(const char *name, · line 2849
Quoted 2 lines
qga/commands-posix.c +0
-0return -1 ; }A counter argument is that if
fopen()above fails then -1 is returned without any error/debug message reported. And stats fetching is best effort anyway.
Ping for this stack. Should I just go ahead and add the above? Sorry if I was expected to respond to this; I don't disagree but I also saw the existing silent-failure code path so it doesn't seem like it matters.
I could add debug messages for both silent-failure code paths, maybe as a separate commit afterwards?
Signature · 2 lines
Andrew Deason [email protected]
Quoted 4 lines · Andrew Deason
This implements the guest agent guest-network-get-interfaces command on Solaris. Solaris provides a
getifaddrs()that's very similar to the Linux one, so the implementation is mostly the same.
Quoted 9 lines
Andrew Deason (3):
qga/commands-posix: Use getifaddrs when available qga/commands-posix: Fix iface hw address detection qga/commands-posix: Fix listing ifaces for Solarismeson.build | 1 + qga/commands-posix.c | 488 +++++++++++++++++++++++++++------------------------ 2 files changed, 260 insertions(+), 229 deletions(-)
Michal