Received: from eggs.gnu.org ([2001:4830:134:3::10]:42793) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1YxysB-0004H0-6b for qemu-devel@nongnu.org; Thu, 28 May 2015 10:33:29 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Yxys7-00056g-R9 for qemu-devel@nongnu.org; Thu, 28 May 2015 10:33:27 -0400 Received: from prv-mh.provo.novell.com ([137.65.248.74]:60381) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Yxys7-00056V-HQ for qemu-devel@nongnu.org; Thu, 28 May 2015 10:33:23 -0400 Message-Id: <5566D2CE020000760012F687@prv-mh.provo.novell.com> Date: Thu, 28 May 2015 08:33:18 -0600 From: "Kirk Allan" References: <1427902795-12024-1-git-send-email-kallan@suse.com> <1427902795-12024-3-git-send-email-kallan@suse.com> <555CD5D1.9010103@parallels.com> In-Reply-To: <555CD5D1.9010103@parallels.com> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-15 Content-Transfer-Encoding: quoted-printable Content-Disposition: inline Subject: Re: [Qemu-devel] [PATCH v2 2/2] qga: win32 implementation of qmp_guest_network_get_interfaces List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org, Olga Krishtal Cc: sw@weilnetz.de, mdroth@linux.vnet.ibm.com Thanks for the feedback. I=27ll make the necessary adjustments and create = a new patch. Kirk >>> > On 01/04/15 18:39, Kirk Allan wrote: >> By default, IP addresses and prefixes will be derived from information >> obtained by various calls and structures. IPv4 prefixes can be found >> by matching the address to those returned by GetApaptersInfo. IPv6 >> prefixes can not be matched this way due to the unpredictable order of >> entries. > GetAdaptersInfo. I=27ll fix the typo. >> In Windows Visa/2008 guests and newer, it is possible to use inet_ntop()= >> and OnLinkPrefixLength to get IPv4 and IPv6 addresses and prefixes. >> Setting *extra-cflags in the build configuration to >> *- D_WIN32_WINNT-0x600 -DWINVER=3D0x600* or greater enables this = functionality >> for those guests. Setting *ectra-cflags is not required and if not = used, >> the default approach will be taken. >> >> Signed-off-by: Kirk Allan >> --- >> qga/commands-win32.c =7C 319=20 > ++++++++++++++++++++++++++++++++++++++++++++++++++- >> 1 file changed, 317 insertions(+), 2 deletions(-) >> >> diff --git a/qga/commands-win32.c b/qga/commands-win32.c >> index 3ef0549..635d3ca 100644 >> --- a/qga/commands-win32.c >> +++ b/qga/commands-win32.c >> =40=40 -16,11 +16,17 =40=40 >> =23include >> =23include >> =23include >> +=23include >> +=23include >> +=23include >> +=23include >> +=23include >> =23include =22qga/guest-agent-core.h=22 >> =23include =22qga/vss-win32.h=22 >> =23include =22qga-qmp-commands.h=22 >> =23include =22qapi/qmp/qerror.h=22 >> =23include =22qemu/queue.h=22 >> +=23include =22qemu/host-utils.h=22 >> =20 >> =23ifndef SHTDN_REASON_FLAG_PLANNED >> =23define SHTDN_REASON_FLAG_PLANNED 0x80000000 >> =40=40 -589,9 +595,318 =40=40 void qmp_guest_suspend_hybrid(Error = **errp) >> error_set(errp, QERR_UNSUPPORTED); >> =7D >> =20 >> +=23define WORKING_BUFFER_SIZE 15000 >> +=23define MAX_ALLOC_TRIES 2 >> + >> +static DWORD guest_get_adapter_addresses(IP_ADAPTER_ADDRESSES=20 > **adptr_addrs) >> +=7B >> + ULONG out_buf_len =3D 0; >> + int alloc_tries =3D 0; >> + DWORD ret =3D ERROR_SUCCESS; >> + >> + /* Allocate a 15 KB buffer to start with. If not enough, = out_buf_len >> + * will have the amount needed after the call to GetAdaptersAddress= es. >> + */ > may be we should not allocate memory in the beginning and just make a=20 > call with just pointer, > and get necessary size. I=27ll get the size first and make one allocation. >> + out_buf_len =3D WORKING_BUFFER_SIZE; >> + >> + do =7B >> + *adptr_addrs =3D (IP_ADAPTER_ADDRESSES *) g_malloc(out_buf_len)= ; >> + if (*adptr_addrs =3D=3D NULL) =7B >> + return ERROR_NOT_ENOUGH_MEMORY; > I think we should use error handling with win32 macro error_setg_win32(= =20 > errno, GetLastError().. I=27ll use error_setg_win32() when the api sets last error. >> + =7D >> + >> + ret =3D GetAdaptersAddresses(AF_UNSPEC, GAA_FLAG_INCLUDE_PREFIX= , >> + NULL, *adptr_addrs, &out_buf_len); >> + >> + if (ret =3D=3D ERROR_BUFFER_OVERFLOW) =7B >> + g_free(*adptr_addrs); >> + *adptr_addrs =3D NULL; >> + alloc_tries++; >> + =7D >> + >> + =7D while ((ret =3D=3D ERROR_BUFFER_OVERFLOW) && (alloc_tries <=20 > MAX_ALLOC_TRIES)); > Why we do it twice? >> + if (ret =21=3D ERROR_SUCCESS) =7B >> + if (*adptr_addrs) =7B >> + g_free(*adptr_addrs); >> + *adptr_addrs =3D NULL; >> + =7D >> + =7D > In this case we always have ERROR_SUCCESS. >> + return ret; >> +=7D >> + > The comments for this function is the same. Moreover, they look very=20 > similar, > can we merge them and have =23if (_WIN32_WINNT < 0x0600) inside the=20 > function body? I=27ll do the same as above, get the size first and do one allocation. = Since we are doing GetAdaptersAddresses and GetAdaptersInfo, I don=27t see = usable way in combining these two. Simplifying th e memory allocation should help though. >> +=23if (_WIN32_WINNT < 0x0600) >> +static DWORD guest_get_adapters_info(IP_ADAPTER_INFO **adptr_info) >> +=7B >> + ULONG out_buf_len =3D 0; >> + int alloc_tries =3D 0; >> + DWORD ret =3D ERROR_SUCCESS; >> + >> + out_buf_len =3D sizeof(IP_ADAPTER_INFO); >> + do =7B >> + *adptr_info =3D (IP_ADAPTER_INFO *)g_malloc(out_buf_len); >> + if (*adptr_info =3D=3D NULL) =7B >> + return ERROR_NOT_ENOUGH_MEMORY; >> + =7D >> + >> + ret =3D GetAdaptersInfo(*adptr_info, &out_buf_len); >> + >> + if (ret =3D=3D ERROR_BUFFER_OVERFLOW) =7B >> + g_free(*adptr_info); >> + *adptr_info =3D NULL; >> + alloc_tries++; >> + =7D >> + >> + =7D while ((ret =3D=3D ERROR_BUFFER_OVERFLOW) && (alloc_tries <=20 > MAX_ALLOC_TRIES)); >> + >> + if (ret =21=3D ERROR_SUCCESS) =7B >> + if (*adptr_info) =7B >> + g_free(*adptr_info); >> + *adptr_info =3D NULL; >> + =7D >> + =7D >> + return ret; >> +=7D >> +=23endif >> +static char *guest_wcstombs_dup(WCHAR *wstr) >> +=7B >> + char *str; >> + int i; >> + >> + i =3D wcslen(wstr) + 1; >> + str =3D g_malloc(i); >> + if (str) =7B > This is Windows-specific part of qemu-ga, so we should win32 API use=20 > WideCharToMultiByte. > I am not sure that wcstombs uses Unicode UTF-16 and afaik it is = deprecated. Will switch to WideCharToMultiByte. >> + wcstombs(str, wstr, i); >> + =7D >> + return str; >> +=7D >> + >> +static char *guest_inet_ntop(int af, void *cp, char *buf, socklen_t = len) >> +=7B >> +=23if (_WIN32_WINNT >=3D 0x0600) && defined(ARCH_x86_64) >> + /* If built for 64 bit Windows Vista/2008 or newer, inet_ntop() is >> + * available for use. Otherwise, do our best to derive it. >> + */ >> + return (char *)inet_ntop(af, cp, buf, len); >> +=23else >> + u_char *p; >> + >> + p =3D (u_char *)cp; >> + if (af =3D=3D AF_INET) =7B > May be we should do some struct IP_addr that will hold this information= =20 > in appropriate format. inet_ntop just returns a string, so I=27m not sure what else could be done = here. >> + snprintf(buf, len, =22%u.%u.%u.%u=22, p=5B0=5D, p=5B1=5D, = p=5B2=5D, p=5B3=5D); >> + =7D else if (af =3D=3D AF_INET6) =7B >> + int i, compressed_zeros, added_to_buf; >> + char t=5Bsizeof =22::ffff=22=5D; >> + >> + buf=5B0=5D =3D =27=5C0=27; >> + compressed_zeros =3D 0; >> + added_to_buf =3D 0; >> + for (i =3D 0; i < 16; i +=3D 2) =7B >> + if (p=5Bi=5D =21=3D 0 =7C=7C p=5Bi + 1=5D =21=3D 0) =7B >> + if (compressed_zeros) =7B >> + if (len > sizeof =22::=22) =7B >> + strcat(buf, =22::=22); >> + len -=3D sizeof =22::=22 - 1; >> + =7D >> + added_to_buf++; >> + =7D else =7B >> + if (added_to_buf) =7B >> + if (len > 1) =7B >> + strcat(buf, =22:=22); >> + len--; >> + =7D >> + =7D >> + added_to_buf++; >> + =7D >> + >> + /* Take into account leading zeros. */ >> + if (p=5Bi=5D) =7B >> + len -=3D snprintf(t, sizeof(t), =22%x%02x=22, = p=5Bi=5D, p=5Bi+1=5D); >> + =7D else =7B >> + len -=3D snprintf(t, sizeof(t), =22%x=22, = p=5Bi+1=5D); >> + =7D >> + >> + /* Ensure there=27s enough room for the NULL. */ >> + if (len > 0) =7B >> + strcat(buf, t); >> + =7D >> + compressed_zeros =3D 0; >> + =7D else =7B >> + compressed_zeros++; >> + =7D >> + =7D >> + if (compressed_zeros && =21added_to_buf) =7B >> + /* The address was all zeros. */ >> + strcat(buf, =22::=22); >> + =7D >> + =7D >> + return buf; >> +=23endif >> +=7D >> + >> +static int64_t guest_ip_prefix(IP_ADAPTER_UNICAST_ADDRESS *ip_addr) >> +=7B >> +=23if (_WIN32_WINNT >=3D 0x0600 ) >> + /* If built for Windows Vista/2008 or newer, use the OnLinkPrefixLe= ngth >> + * field to obtain the prefix. Otherwise, do our best to figure = it=20 > out. >> + */ >> + return ip_addr->OnLinkPrefixLength; >> +=23else >> + int64_t prefix =3D -1; /* Use for AF_INET6 and unknown/undetermined= =20 > values. */ >> + >> + if (ip_addr->Address.lpSockaddr->sa_family =3D=3D AF_INET) =7B >> + IP_ADAPTER_INFO *adptr_info, *info; >> + IP_ADDR_STRING *ip; >> + struct in_addr *p; >> + >> + if (guest_get_adapters_info(&adptr_info) =21=3D ERROR_SUCCESS) = =7B >> + return prefix; >> + =7D >> + >> + /* Match up the passed in ip_addr with one found in adaptr_info= . >> + * The matching one in adptr_info will have the netmask. >> + */ >> + p =3D &((struct sockaddr_in *)ip_addr->Address.lpSockaddr)->sin= _addr; >> + for (info =3D adptr_info; info && prefix =3D=3D -1; info =3D = info->Next) =7B >> + for (ip =3D &info->IpAddressList; ip; ip =3D ip->Next) =7B >> + if (p->S_un.S_addr =3D=3D inet_addr(ip->IpAddress.Strin= g)) =7B >> + prefix =3D ctpop32(inet_addr(ip->IpMask.String)); >> + break; >> + =7D >> + =7D >> + =7D >> + g_free(adptr_info); >> + =7D >> + return prefix; >> +=23endif >> +=7D >> + >> GuestNetworkInterfaceList *qmp_guest_network_get_interfaces(Error = **errp) >> =7B >> - error_set(errp, QERR_UNSUPPORTED); >> + IP_ADAPTER_ADDRESSES *adptr_addrs, *addr; >> + GuestNetworkInterfaceList *head =3D NULL, *cur_item =3D NULL; >> + GuestIpAddressList *head_addr, *cur_addr; >> + DWORD ret; >> + >> + ret =3D guest_get_adapter_addresses(&adptr_addrs); >> + if (ret =21=3D ERROR_SUCCESS) =7B >> + error_setg(errp, =22failed to get adapter addresses %lu=22, = ret); >> + return NULL; >> + =7D >> + >> + for (addr =3D adptr_addrs; addr; addr =3D addr->Next) =7B >> + GuestNetworkInterfaceList *info; >> + GuestIpAddressList *address_item =3D NULL; >> + char addr4=5BINET_ADDRSTRLEN=5D; >> + char addr6=5BINET6_ADDRSTRLEN=5D; >> + unsigned char *mac_addr; >> + void *p; > Variables should not be declared in function body. I think better way = is=20 > to put them in the beginning. I=27ll move the variable to the beginning. >> + IP_ADAPTER_UNICAST_ADDRESS *ip_addr =3D NULL; >> + >> + info =3D g_malloc0(sizeof(*info)); >> + if (=21info) =7B >> + error_setg(errp, =22failed to alloc a network interface = list=22); >> + goto error; >> + =7D >> + >> + if (=21cur_item) =7B >> + head =3D cur_item =3D info; >> + =7D else =7B >> + cur_item->next =3D info; >> + cur_item =3D info; >> + =7D >> + >> + info->value =3D g_malloc0(sizeof(*info->value)); >> + if (=21info->value) =7B >> + error_setg(errp, =22failed to alloc a network interface=22)= ; >> + goto error; >> + =7D >> + info->value->name =3D guest_wcstombs_dup(addr->FriendlyName); >> + >> + if (addr->PhysicalAddressLength) =7B >> + mac_addr =3D addr->PhysicalAddress; >> + >> + info->value->hardware_address =3D >> + g_strdup_printf(=22%02x:%02x:%02x:%02x:%02x:%02x=22, >> + (int) mac_addr=5B0=5D, (int) mac_addr=5B1=5D, >> + (int) mac_addr=5B2=5D, (int) mac_addr=5B3=5D, >> + (int) mac_addr=5B4=5D, (int) mac_addr=5B5=5D); >> + >> + info->value->has_hardware_address =3D true; >> + =7D >> + >> + head_addr =3D NULL; >> + cur_addr =3D NULL; >> + for (ip_addr =3D addr->FirstUnicastAddress; >> + ip_addr; >> + ip_addr =3D ip_addr->Next) =7B >> + address_item =3D g_malloc0(sizeof(*address_item)); >> + if (=21address_item) =7B >> + error_setg(errp, =22failed to alloc an Ip address = list=22); >> + goto error; >> + =7D >> + >> + if (=21cur_addr) =7B >> + head_addr =3D cur_addr =3D address_item; >> + =7D else =7B >> + cur_addr->next =3D address_item; >> + cur_addr =3D address_item; >> + =7D >> + >> + address_item->value =3D g_malloc0(sizeof(*address_item->val= ue)); >> + if (=21address_item->value) =7B >> + error_setg(errp, =22failed to alloc an Ip address=22); >> + goto error; >> + =7D >> + >> + if (ip_addr->Address.lpSockaddr->sa_family =3D=3D AF_INET) = =7B >> + p =3D &((struct sockaddr_in *) >> + ip_addr->Address.lpSockaddr)->sin_addr; >> + >> + if (=21guest_inet_ntop(AF_INET, p, addr4, sizeof(addr4)= )) =7B >> + error_setg(errp, > win32 error macro Since the error can be retrieved with WSAGetLastErorr, I=27ll switch to = error_setg_win32(errp, WSAGetLastError(), ... >> + =22failed address presentation form=20 > conversion=22); >> + goto error; >> + =7D >> + >> + address_item->value->ip_address =3D g_strdup(addr4); >> + address_item->value->ip_address_type =3D >> + GUEST_IP_ADDRESS_TYPE_IPV4; >> + =7D else if (ip_addr->Address.lpSockaddr->sa_family =3D=3D = AF_INET6) =7B >> + p =3D &((struct sockaddr_in6 *) >> + ip_addr->Address.lpSockaddr)->sin6_addr; >> + >> + if (=21guest_inet_ntop(AF_INET6, p, addr6, sizeof(addr6= ))) =7B >> + error_setg(errp, >> + =22failed address presentation form=20 > conversion=22); > win32 error macro Same as above. >> goto error; >> + =7D >> + >> + address_item->value->ip_address =3D g_strdup(addr6); >> + address_item->value->ip_address_type =3D >> + GUEST_IP_ADDRESS_TYPE_IPV6; >> + =7D >> + address_item->value->prefix =3D guest_ip_prefix(ip_addr); >> + =7D >> + if (head_addr) =7B >> + info->value->has_ip_addresses =3D true; >> + info->value->ip_addresses =3D head_addr; >> + =7D >> + =7D >> + >> + if (adptr_addrs) =7B >> + g_free(adptr_addrs); >> + =7D >> + return head; >> + >> +error: >> + if (adptr_addrs) =7B >> + g_free(adptr_addrs); >> + =7D >> + if (head) =7B >> + qapi_free_GuestNetworkInterfaceList(head); >> + =7D >> return NULL; >> =7D >> =20 >> =40=40 -707,7 +1022,7 =40=40 GuestMemoryBlockInfo=20 > *qmp_guest_get_memory_block_info(Error **errp) >> GList *ga_command_blacklist_init(GList *blacklist) >> =7B >> const char *list_unsupported=5B=5D =3D =7B >> - =22guest-suspend-hybrid=22, =22guest-network-get-interfaces=22,= >> + =22guest-suspend-hybrid=22, >> =22guest-get-vcpus=22, =22guest-set-vcpus=22, >> =22guest-set-user-password=22, >> =22guest-get-memory-blocks=22, =22guest-set-memory-blocks=22, > I think 2 last functions GuestNetworkInterfaceList=20 > *qmp_guest_network_get_interfaces(Error **errp) and static char=20 > *guest_inet_ntop(int af, void *cp, char *buf, socklen_t len)should be=20 > redesigned and refactored very attentively. Secondly you should = decide=20 > what kind of functions to use Win32 API or POSIX API. > It is bed idea to mix them. Where InetNtop is available where inet_ntop is, I=27ll switch the win32 = api. > Pls, pay attention to to error handling. For apis that can retrieve the error form GetLastError, I=27ll switch to = the=20 error_setg_win32.