shithub: riscv

Download patch

ref: 8a01033efcdc72f8b63b5a7afcc699ca20edfdc0
parent: 6728a5ec2351cac7cc27333e0d984c5c2b029b87
author: cinap_lenrek <cinap_lenrek@felloff.net>
date: Sun Feb 25 16:11:41 EST 2018

vmx: allow setting mac address of using ea:nnnnnnnnnnnn! prefix, use genrandom() to generate mac globally unique mac address

--- a/sys/man/1/vmx
+++ b/sys/man/1/vmx
@@ -100,6 +100,8 @@
 It can also be prefixed by \fLfile!\fR to interpret the argument as a file instead and it can be prefixed by \fLhdr!\fR to enable headers matching the binary
 .IR snoopy (8)
 format.
+The MAC address can be specified with the \fLea:\fInnnnnnnnnnnn\fL!\fR prefix,
+otherwise a random address is used.
 .PP
 A
 .B -d
--- a/sys/src/cmd/vmx/virtio.c
+++ b/sys/src/cmd/vmx/virtio.c
@@ -4,6 +4,9 @@
 #include "dat.h"
 #include "fns.h"
 
+#include <ip.h>		/* parseether() */
+#include <libsec.h>	/* genrandom() */
+
 typedef struct VIODev VIODev;
 typedef struct VIOQueue VIOQueue;
 typedef struct VIOBuf VIOBuf;
@@ -633,10 +636,11 @@
 {
 	int fd, cfd;
 	VIODev *d;
-	int i;
+	char *ea;
 	int flags;
 	enum { VNETFILE = 1 };
 
+	ea = nil;
 	flags = 0;
 	for(;;){
 		if(strncmp(net, "hdr!", 4) == 0){
@@ -645,6 +649,12 @@
 		}else if(strncmp(net, "file!", 5) == 0){
 			net += 5;
 			flags |= VNETFILE;
+		}else if(strncmp(net, "ea:", 3) == 0){
+			net = strchr(ea = net+3, '!');
+			if(net++ == nil){
+				werrstr("missing: !");
+				return -1;
+			}
 		}else
 			break;
 	}
@@ -665,9 +675,10 @@
 	mkvioqueue(d, 1024, viowakeup);
 	mkvioqueue(d, 1024, viowakeup);
 	mkvioqueue(d, 32, vionetcmd);
-	for(i = 0; i < 6; i++)
-		d->net.mac[i] = rand();
-	d->net.mac[0] = d->net.mac[0] & ~1 | 2;
+	if(ea == nil || parseether(d->net.mac, ea)){
+		genrandom(d->net.mac, 6);
+		d->net.mac[0] = d->net.mac[0] & ~1 | 2;
+	}
 	d->net.flags = flags;
 	d->devfeat = 1<<5|1<<16|1<<17|1<<18|1<<20;
 	d->io = vionetio;
--- a/sys/src/cmd/vmx/vmx.c
+++ b/sys/src/cmd/vmx/vmx.c
@@ -87,7 +87,6 @@
 	rc = read(ctlfd, name, sizeof(name) - 1);
 	if(rc < 0) sysfatal("read: %r");
 	name[rc] = 0;
-	srand(atoi(name));
 	if(segname == nil){
 		segname = smprint("vm.%s", name);
 		segrclose = ORCLOSE;