mirror of
https://github.com/qemu/qemu.git
synced 2025-08-15 13:47:03 +00:00
qga: Utilize QAPI_LIST_APPEND in qmp_guest_network_get_interfaces
I found another spot that can benefit from using our macros instead of open-coding qapi list creation. Signed-off-by: Eric Blake <eblake@redhat.com> Message-Id: <20210205171634.1491258-1-eblake@redhat.com> Reviewed-by: Markus Armbruster <armbru@redhat.com> Signed-off-by: Markus Armbruster <armbru@redhat.com>
This commit is contained in:
parent
d71a243220
commit
96291f1343
@ -2153,17 +2153,17 @@ void qmp_guest_suspend_hybrid(Error **errp)
|
|||||||
guest_suspend(SUSPEND_MODE_HYBRID, errp);
|
guest_suspend(SUSPEND_MODE_HYBRID, errp);
|
||||||
}
|
}
|
||||||
|
|
||||||
static GuestNetworkInterfaceList *
|
static GuestNetworkInterface *
|
||||||
guest_find_interface(GuestNetworkInterfaceList *head,
|
guest_find_interface(GuestNetworkInterfaceList *head,
|
||||||
const char *name)
|
const char *name)
|
||||||
{
|
{
|
||||||
for (; head; head = head->next) {
|
for (; head; head = head->next) {
|
||||||
if (strcmp(head->value->name, name) == 0) {
|
if (strcmp(head->value->name, name) == 0) {
|
||||||
break;
|
return head->value;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return head;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int guest_get_network_stats(const char *name,
|
static int guest_get_network_stats(const char *name,
|
||||||
@ -2232,7 +2232,7 @@ static int guest_get_network_stats(const char *name,
|
|||||||
*/
|
*/
|
||||||
GuestNetworkInterfaceList *qmp_guest_network_get_interfaces(Error **errp)
|
GuestNetworkInterfaceList *qmp_guest_network_get_interfaces(Error **errp)
|
||||||
{
|
{
|
||||||
GuestNetworkInterfaceList *head = NULL, *cur_item = NULL;
|
GuestNetworkInterfaceList *head = NULL, **tail = &head;
|
||||||
struct ifaddrs *ifap, *ifa;
|
struct ifaddrs *ifap, *ifa;
|
||||||
|
|
||||||
if (getifaddrs(&ifap) < 0) {
|
if (getifaddrs(&ifap) < 0) {
|
||||||
@ -2241,8 +2241,9 @@ GuestNetworkInterfaceList *qmp_guest_network_get_interfaces(Error **errp)
|
|||||||
}
|
}
|
||||||
|
|
||||||
for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
|
for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
|
||||||
GuestNetworkInterfaceList *info;
|
GuestNetworkInterface *info;
|
||||||
GuestIpAddressList **address_list = NULL, *address_item = NULL;
|
GuestIpAddressList **address_tail;
|
||||||
|
GuestIpAddress *address_item = NULL;
|
||||||
GuestNetworkInterfaceStat *interface_stat = NULL;
|
GuestNetworkInterfaceStat *interface_stat = NULL;
|
||||||
char addr4[INET_ADDRSTRLEN];
|
char addr4[INET_ADDRSTRLEN];
|
||||||
char addr6[INET6_ADDRSTRLEN];
|
char addr6[INET6_ADDRSTRLEN];
|
||||||
@ -2257,19 +2258,12 @@ GuestNetworkInterfaceList *qmp_guest_network_get_interfaces(Error **errp)
|
|||||||
|
|
||||||
if (!info) {
|
if (!info) {
|
||||||
info = g_malloc0(sizeof(*info));
|
info = g_malloc0(sizeof(*info));
|
||||||
info->value = g_malloc0(sizeof(*info->value));
|
info->name = g_strdup(ifa->ifa_name);
|
||||||
info->value->name = g_strdup(ifa->ifa_name);
|
|
||||||
|
|
||||||
if (!cur_item) {
|
QAPI_LIST_APPEND(tail, info);
|
||||||
head = cur_item = info;
|
|
||||||
} else {
|
|
||||||
cur_item->next = info;
|
|
||||||
cur_item = info;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!info->value->has_hardware_address &&
|
if (!info->has_hardware_address && ifa->ifa_flags & SIOCGIFHWADDR) {
|
||||||
ifa->ifa_flags & SIOCGIFHWADDR) {
|
|
||||||
/* we haven't obtained HW address yet */
|
/* we haven't obtained HW address yet */
|
||||||
sock = socket(PF_INET, SOCK_STREAM, 0);
|
sock = socket(PF_INET, SOCK_STREAM, 0);
|
||||||
if (sock == -1) {
|
if (sock == -1) {
|
||||||
@ -2278,7 +2272,7 @@ GuestNetworkInterfaceList *qmp_guest_network_get_interfaces(Error **errp)
|
|||||||
}
|
}
|
||||||
|
|
||||||
memset(&ifr, 0, sizeof(ifr));
|
memset(&ifr, 0, sizeof(ifr));
|
||||||
pstrcpy(ifr.ifr_name, IF_NAMESIZE, info->value->name);
|
pstrcpy(ifr.ifr_name, IF_NAMESIZE, info->name);
|
||||||
if (ioctl(sock, SIOCGIFHWADDR, &ifr) == -1) {
|
if (ioctl(sock, SIOCGIFHWADDR, &ifr) == -1) {
|
||||||
error_setg_errno(errp, errno,
|
error_setg_errno(errp, errno,
|
||||||
"failed to get MAC address of %s",
|
"failed to get MAC address of %s",
|
||||||
@ -2290,13 +2284,13 @@ GuestNetworkInterfaceList *qmp_guest_network_get_interfaces(Error **errp)
|
|||||||
close(sock);
|
close(sock);
|
||||||
mac_addr = (unsigned char *) &ifr.ifr_hwaddr.sa_data;
|
mac_addr = (unsigned char *) &ifr.ifr_hwaddr.sa_data;
|
||||||
|
|
||||||
info->value->hardware_address =
|
info->hardware_address =
|
||||||
g_strdup_printf("%02x:%02x:%02x:%02x:%02x:%02x",
|
g_strdup_printf("%02x:%02x:%02x:%02x:%02x:%02x",
|
||||||
(int) mac_addr[0], (int) mac_addr[1],
|
(int) mac_addr[0], (int) mac_addr[1],
|
||||||
(int) mac_addr[2], (int) mac_addr[3],
|
(int) mac_addr[2], (int) mac_addr[3],
|
||||||
(int) mac_addr[4], (int) mac_addr[5]);
|
(int) mac_addr[4], (int) mac_addr[5]);
|
||||||
|
|
||||||
info->value->has_hardware_address = true;
|
info->has_hardware_address = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ifa->ifa_addr &&
|
if (ifa->ifa_addr &&
|
||||||
@ -2309,15 +2303,14 @@ GuestNetworkInterfaceList *qmp_guest_network_get_interfaces(Error **errp)
|
|||||||
}
|
}
|
||||||
|
|
||||||
address_item = g_malloc0(sizeof(*address_item));
|
address_item = g_malloc0(sizeof(*address_item));
|
||||||
address_item->value = g_malloc0(sizeof(*address_item->value));
|
address_item->ip_address = g_strdup(addr4);
|
||||||
address_item->value->ip_address = g_strdup(addr4);
|
address_item->ip_address_type = GUEST_IP_ADDRESS_TYPE_IPV4;
|
||||||
address_item->value->ip_address_type = GUEST_IP_ADDRESS_TYPE_IPV4;
|
|
||||||
|
|
||||||
if (ifa->ifa_netmask) {
|
if (ifa->ifa_netmask) {
|
||||||
/* Count the number of set bits in netmask.
|
/* Count the number of set bits in netmask.
|
||||||
* This is safe as '1' and '0' cannot be shuffled in netmask. */
|
* This is safe as '1' and '0' cannot be shuffled in netmask. */
|
||||||
p = &((struct sockaddr_in *)ifa->ifa_netmask)->sin_addr;
|
p = &((struct sockaddr_in *)ifa->ifa_netmask)->sin_addr;
|
||||||
address_item->value->prefix = ctpop32(((uint32_t *) p)[0]);
|
address_item->prefix = ctpop32(((uint32_t *) p)[0]);
|
||||||
}
|
}
|
||||||
} else if (ifa->ifa_addr &&
|
} else if (ifa->ifa_addr &&
|
||||||
ifa->ifa_addr->sa_family == AF_INET6) {
|
ifa->ifa_addr->sa_family == AF_INET6) {
|
||||||
@ -2329,15 +2322,14 @@ GuestNetworkInterfaceList *qmp_guest_network_get_interfaces(Error **errp)
|
|||||||
}
|
}
|
||||||
|
|
||||||
address_item = g_malloc0(sizeof(*address_item));
|
address_item = g_malloc0(sizeof(*address_item));
|
||||||
address_item->value = g_malloc0(sizeof(*address_item->value));
|
address_item->ip_address = g_strdup(addr6);
|
||||||
address_item->value->ip_address = g_strdup(addr6);
|
address_item->ip_address_type = GUEST_IP_ADDRESS_TYPE_IPV6;
|
||||||
address_item->value->ip_address_type = GUEST_IP_ADDRESS_TYPE_IPV6;
|
|
||||||
|
|
||||||
if (ifa->ifa_netmask) {
|
if (ifa->ifa_netmask) {
|
||||||
/* Count the number of set bits in netmask.
|
/* Count the number of set bits in netmask.
|
||||||
* This is safe as '1' and '0' cannot be shuffled in netmask. */
|
* This is safe as '1' and '0' cannot be shuffled in netmask. */
|
||||||
p = &((struct sockaddr_in6 *)ifa->ifa_netmask)->sin6_addr;
|
p = &((struct sockaddr_in6 *)ifa->ifa_netmask)->sin6_addr;
|
||||||
address_item->value->prefix =
|
address_item->prefix =
|
||||||
ctpop32(((uint32_t *) p)[0]) +
|
ctpop32(((uint32_t *) p)[0]) +
|
||||||
ctpop32(((uint32_t *) p)[1]) +
|
ctpop32(((uint32_t *) p)[1]) +
|
||||||
ctpop32(((uint32_t *) p)[2]) +
|
ctpop32(((uint32_t *) p)[2]) +
|
||||||
@ -2349,29 +2341,22 @@ GuestNetworkInterfaceList *qmp_guest_network_get_interfaces(Error **errp)
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
address_list = &info->value->ip_addresses;
|
address_tail = &info->ip_addresses;
|
||||||
|
while (*address_tail) {
|
||||||
while (*address_list && (*address_list)->next) {
|
address_tail = &(*address_tail)->next;
|
||||||
address_list = &(*address_list)->next;
|
|
||||||
}
|
}
|
||||||
|
QAPI_LIST_APPEND(address_tail, address_item);
|
||||||
|
|
||||||
if (!*address_list) {
|
info->has_ip_addresses = true;
|
||||||
*address_list = address_item;
|
|
||||||
} else {
|
|
||||||
(*address_list)->next = address_item;
|
|
||||||
}
|
|
||||||
|
|
||||||
info->value->has_ip_addresses = true;
|
if (!info->has_statistics) {
|
||||||
|
|
||||||
if (!info->value->has_statistics) {
|
|
||||||
interface_stat = g_malloc0(sizeof(*interface_stat));
|
interface_stat = g_malloc0(sizeof(*interface_stat));
|
||||||
if (guest_get_network_stats(info->value->name,
|
if (guest_get_network_stats(info->name, interface_stat) == -1) {
|
||||||
interface_stat) == -1) {
|
info->has_statistics = false;
|
||||||
info->value->has_statistics = false;
|
|
||||||
g_free(interface_stat);
|
g_free(interface_stat);
|
||||||
} else {
|
} else {
|
||||||
info->value->statistics = interface_stat;
|
info->statistics = interface_stat;
|
||||||
info->value->has_statistics = true;
|
info->has_statistics = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user