terraform-provider-libvirt
terraform-provider-libvirt copied to clipboard
Fix qemu-guest-agent address retrieval when MAC contains two zeroes
When MAC address contains two zeroes the whole octet set to zero - i.e. single zero on the qemu-ga output:
2020-03-23T19:14:54.231+0300 [DEBUG] plugin.terraform-provider-libvirt: 2020/03/23 19:14:54 [DEBUG] qemu-agent response: {"return":[{"name":"lo0","hardware-address":"0:0:0:0:0:0","ip-addresses":[{"ip-address-type":"ipv4","prefix":8,"ip-address":"127.0.0.1"},{"ip-address-type":"ipv6","prefix":128,"ip-address":"::1"},{"ip-address-type":"ipv6","prefix":64,"ip-address":"fe80::1"}]},{"name":"en1","hardware-address":"52:54:0:fa:9c:3b","ip-addresses":[{"ip-address-type":"ipv6","prefix":64,"ip-address":"fe80::104b:5f65:737c:4b7a"},{"ip-address-type":"ipv4","prefix":24,"ip-address":"10.70.131.226"}]}]}
2020-03-23T19:14:54.231+0300 [DEBUG] plugin.terraform-provider-libvirt: 2020/03/23 19:14:54 [DEBUG] Parsed response {Interfaces:[{Name:lo0 Hwaddr:0:0:0:0:0:0 IPAddresses:[{Type:ipv4 Address:127.0.0.1 Prefix:8} {Type:ipv6 Address:::1 Prefix:128} {Type:ipv6 Address:fe80::1 Prefix:64}]} {Name:en1 Hwaddr:52:54:0:fa:9c:3b IPAddresses:[{Type:ipv6 Address:fe80::104b:5f65:737c:4b7a Prefix:64} {Type:ipv4 Address:10.70.131.226 Prefix:24}]}]}
2020-03-23T19:14:54.232+0300 [DEBUG] plugin.terraform-provider-libvirt: 2020/03/23 19:14:54 [DEBUG] Interfaces obtained via qemu-agent: [{Name:lo0 Hwaddr:0:0:0:0:0:0 Addrs:[{Type:0 Addr:127.0.0.1 Prefix:8} {Type:1 Addr:::1 Prefix:128} {Type:1 Addr:fe80::1 Prefix:64}]} {Name:en1 Hwaddr:52:54:0:fa:9c:3b Addrs:[{Type:1 Addr:fe80::104b:5f65:737c:4b7a Prefix:64} {Type:0 Addr:10.70.131.226 Prefix:24}]}]
2020-03-23T19:14:54.232+0300 [DEBUG] plugin.terraform-provider-libvirt: 2020/03/23 19:14:54 [DEBUG] read: addresses for '52:54:00:FA:9C:3B': []
2020-03-23T19:14:54.232+0300 [DEBUG] plugin.terraform-provider-libvirt: 2020/03/23 19:14:54 [DEBUG] read: ifaces for 'dev-test-bridge':
2020-03-23T19:14:54.232+0300 [DEBUG] plugin.terraform-provider-libvirt: ([]map[string]interface {}) (len=1 cap=1) {
2020-03-23T19:14:54.232+0300 [DEBUG] plugin.terraform-provider-libvirt: (map[string]interface {}) (len=10) {
2020-03-23T19:14:54.232+0300 [DEBUG] plugin.terraform-provider-libvirt: (string) (len=4) "vepa": (string) "",
2020-03-23T19:14:54.232+0300 [DEBUG] plugin.terraform-provider-libvirt: (string) (len=3) "mac": (string) (len=17) "52:54:00:FA:9C:3B",
2020-03-23T19:14:54.232+0300 [DEBUG] plugin.terraform-provider-libvirt: (string) (len=14) "wait_for_lease": (bool) true,
2020-03-23T19:14:54.232+0300 [DEBUG] plugin.terraform-provider-libvirt: (string) (len=9) "addresses": ([]string) <nil>,
2020-03-23T19:14:54.232+0300 [DEBUG] plugin.terraform-provider-libvirt: (string) (len=12) "network_name": (string) "",
2020-03-23T19:14:54.232+0300 [DEBUG] plugin.terraform-provider-libvirt: (string) (len=6) "bridge": (string) (len=3) "br0",
2020-03-23T19:14:54.232+0300 [DEBUG] plugin.terraform-provider-libvirt: (string) (len=7) "macvtap": (string) "",
2020-03-23T19:14:54.232+0300 [DEBUG] plugin.terraform-provider-libvirt: (string) (len=11) "passthrough": (string) "",
2020-03-23T19:14:54.232+0300 [DEBUG] plugin.terraform-provider-libvirt: (string) (len=8) "hostname": (string) "",
2020-03-23T19:14:54.232+0300 [DEBUG] plugin.terraform-provider-libvirt: (string) (len=10) "network_id": (string) ""
2020-03-23T19:14:54.232+0300 [DEBUG] plugin.terraform-provider-libvirt: }
2020-03-23T19:14:54.232+0300 [DEBUG] plugin.terraform-provider-libvirt: }
Because of direct string comparison condition is failing:
mac == strings.ToUpper(ifaceWithAddr.Hwaddr)
Possibly fixes:
- https://github.com/dmacvicar/terraform-provider-libvirt/issues/710
Tested with agent on MacOS Catalina 10.15.3
@MalloZup As it turned qapi omits any leading 0 in hwaddr and 52:54:00:0e:93:f7 will be returned as 52:54:0:e:93:f7 which I guess is RFC compatible but Go net library doesn't supports this. I've wrapped hwaddr by normalization function.
@axmetishe thx for this PR. do you have any link upstream where I could read about this? At moment we are releasing a new 0.6.2 version so we will review it afterward. Thx for your PR and time :cupid:
@MalloZup
I didn't find where actual hwaddr modification came from unfortunately because even commands-posix.c use correct string format "%02x:%02x:%02x:%02x:%02x:%02x"
https://github.com/qemu/qemu/blob/v4.2.0/qga/commands-posix.c#L1941
I've even played with different formats for that but have no luck to reproduce
#include <stdio.h>
#include <glib.h>
#include <glib/gprintf.h>
int main() {
printf("%02X:%02X:%02X:%02X:%02X:%02X\n", 82, 84, 0, 14, 147, 247);
printf("%02x:%02x:%02x:%02x:%02x:%02x\n", 82, 84, 0, 14, 147, 247);
printf("%02x:%02x:%02x:%02x:%02x:%02x\n", (int) 82, (int) 84, (int) 0, (int) 14, (int) 147, (int) 247);
printf("%x:%x:%x:%x:%x:%x\n", (int) 82, (int) 84, (int) 0, (int) 14, (int) 147, (int) 247);
auto var = "";
var = g_strdup_printf("%02x:%02x:%02x:%02x:%02x:%02x\n", (int) 82, (int) 84, (int) 0, (int) 14, (int) 147, (int) 247);
printf(var);
}
52:54:00:0E:93:F7
52:54:00:0e:93:f7
52:54:00:0e:93:f7
52:54:0:e:93:f7
52:54:00:0e:93:f7
On the other hand qemu-agent command invocation from virsh console returns results as described at that issue:
virsh # qemu-agent-command --domain test-domain --pretty --cmd '{"execute":"guest-network-get-interfaces"}'
{
"return": [
{
"name": "lo0",
"hardware-address": "0:0:0:0:0:0",
"ip-addresses": [
{
"ip-address-type": "ipv4",
"prefix": 8,
"ip-address": "127.0.0.1"
},
{
"ip-address-type": "ipv6",
"prefix": 128,
"ip-address": "::1"
},
{
"ip-address-type": "ipv6",
"prefix": 64,
"ip-address": "fe80::1"
}
]
},
{
"name": "en1",
"hardware-address": "52:54:0:a:84:c2",
"ip-addresses": [
{
"ip-address-type": "ipv6",
"prefix": 64,
"ip-address": "fe80::18e1:7331:356b:19a"
},
{
"ip-address-type": "ipv4",
"prefix": 24,
"ip-address": "10.70.131.211"
}
]
}
]
}
/*
* getmac.c
*
* Simple Demo: Get MAC address of a specified NIC on FreeBSD
*
* To compile: gcc getmac.c -o getmac
*
* Tested on FreeBSD-4.6 RELEASE & FreeBSD-5.2-current
*/
//https://lists.freebsd.org/pipermail/freebsd-hackers/2004-June/007415.html
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <sys/sysctl.h>
#include <net/if.h>
#include <net/if_dl.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
int mib[6], len;
char *buf;
unsigned char *ptr;
struct if_msghdr *ifm;
struct sockaddr_dl *sdl;
if (argc != 2) {
fprintf(stderr, "Usage: getmac <interface>\n");
return 1;
}
mib[0] = CTL_NET;
mib[1] = AF_ROUTE;
mib[2] = 0;
mib[3] = AF_LINK;
mib[4] = NET_RT_IFLIST;
if ((mib[5] = if_nametoindex(argv[1])) == 0) {
perror("if_nametoindex error");
exit(2);
}
if (sysctl(mib, 6, NULL, &len, NULL, 0) < 0) {
perror("sysctl 1 error");
exit(3);
}
if ((buf = malloc(len)) == NULL) {
perror("malloc error");
exit(4);
}
if (sysctl(mib, 6, buf, &len, NULL, 0) < 0) {
perror("sysctl 2 error");
exit(5);
}
ifm = (struct if_msghdr *)buf;
sdl = (struct sockaddr_dl *)(ifm + 1);
ptr = (unsigned char *)LLADDR(sdl);
printf("%02x:%02x:%02x:%02x:%02x:%02x\n", *ptr, *(ptr+1), *(ptr+2),
*(ptr+3), *(ptr+4), *(ptr+5));
return 0;
}
test-domain:tmp testuser$ ./getmac en1
52:54:00:0a:84:c2
test-domain:tmp testuser$ ifconfig
lo0: flags=8049<UP,LOOPBACK,RUNNING,MULTICAST> mtu 16384
options=1203<RXCSUM,TXCSUM,TXSTATUS,SW_TIMESTAMP>
inet 127.0.0.1 netmask 0xff000000
inet6 ::1 prefixlen 128
inet6 fe80::1%lo0 prefixlen 64 scopeid 0x1
nd6 options=201<PERFORMNUD,DAD>
gif0: flags=8010<POINTOPOINT,MULTICAST> mtu 1280
stf0: flags=0<> mtu 1280
UHC4: flags=0<> mtu 0
en1: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500
options=42b<RXCSUM,TXCSUM,VLAN_HWTAGGING,TSO4,CHANNEL_IO>
ether 52:54:00:0a:84:c2
inet6 fe80::18e1:7331:356b:19a%en1 prefixlen 64 secured scopeid 0x5
inet 10.70.131.211 netmask 0xffffff00 broadcast 10.70.131.255
nd6 options=201<PERFORMNUD,DAD>
media: autoselect (1000baseT <full-duplex>)
status: active