kvm tools: Add option to specify guest MAC

Add --guest-mac to specify the MAC of the guest.

Signed-off-by: Sasha Levin <levinsasha928@gmail.com>
Signed-off-by: Pekka Enberg <penberg@kernel.org>
This commit is contained in:
Sasha Levin
2015-06-01 16:39:42 +01:00
committed by Will Deacon
parent 3b02f580cb
commit a4e724ddbd
3 changed files with 20 additions and 1 deletions
+1
View File
@@ -6,6 +6,7 @@ struct kvm;
struct virtio_net_parameters {
struct kvm *self;
const char *host_ip;
char guest_mac[6];
};
void virtio_net__init(const struct virtio_net_parameters *params);
+15 -1
View File
@@ -32,6 +32,7 @@
#define DEFAULT_CONSOLE "serial"
#define DEFAULT_NETWORK "virtio"
#define DEFAULT_HOST_ADDR "192.168.33.2"
#define DEFAULT_GUEST_MAC "00:11:22:33:44:55"
#define MB_SHIFT (20)
#define MIN_RAM_SIZE_MB (64ULL)
@@ -63,6 +64,7 @@ static const char *console;
static const char *kvm_dev;
static const char *network;
static const char *host_ip_addr;
static const char *guest_mac;
static bool single_step;
static bool readonly_image;
extern bool ioport_debug;
@@ -98,7 +100,8 @@ static const struct option options[] = {
"Network to use"),
OPT_STRING('\0', "host-ip-addr", &host_ip_addr, "a.b.c.d",
"Assign this address to the host side networking"),
OPT_STRING('\0', "guest-mac", &guest_mac, "aa:bb:cc:dd:ee:ff",
"Assign this address to the guest side NIC"),
OPT_GROUP("Debug options:"),
OPT_STRING('d', "kvm-dev", &kvm_dev, "kvm-dev", "KVM device file"),
OPT_BOOLEAN('s', "single-step", &single_step,
@@ -271,6 +274,9 @@ int kvm_cmd_run(int argc, const char **argv, const char *prefix)
if (!host_ip_addr)
host_ip_addr = DEFAULT_HOST_ADDR;
if (!guest_mac)
guest_mac = DEFAULT_GUEST_MAC;
term_init();
kvm = kvm__init(kvm_dev, ram_size);
@@ -316,6 +322,14 @@ int kvm_cmd_run(int argc, const char **argv, const char *prefix)
.host_ip = host_ip_addr,
.self = kvm
};
sscanf(guest_mac, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx",
net_params.guest_mac,
net_params.guest_mac+1,
net_params.guest_mac+2,
net_params.guest_mac+3,
net_params.guest_mac+4,
net_params.guest_mac+5);
virtio_net__init(&net_params);
}
+4
View File
@@ -280,8 +280,12 @@ static bool virtio_net__tap_init(const struct virtio_net_parameters *params)
{
struct ifreq ifr;
int sock = socket(AF_INET, SOCK_STREAM, 0);
int i;
struct sockaddr_in sin = {0};
for (i = 0 ; i < 6 ; i++)
net_device.net_config.mac[i] = params->guest_mac[i];
net_device.tap_fd = open("/dev/net/tun", O_RDWR);
if (net_device.tap_fd < 0) {
warning("Unable to open /dev/net/tun\n");