Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 25 Jun 2015 20:38:07 GMT
From:      iateaca@FreeBSD.org
To:        svn-soc-all@FreeBSD.org
Subject:   socsvn commit: r287634 - soc2015/iateaca/bhyve-ne2000-head/usr.sbin/bhyve
Message-ID:  <201506252038.t5PKc7C9048186@socsvn.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: iateaca
Date: Thu Jun 25 20:38:07 2015
New Revision: 287634
URL: http://svnweb.FreeBSD.org/socsvn/?view=rev&rev=287634

Log:
  parse the input string parameter and get the tap name and mac address

Modified:
  soc2015/iateaca/bhyve-ne2000-head/usr.sbin/bhyve/pci_ne2000.c

Modified: soc2015/iateaca/bhyve-ne2000-head/usr.sbin/bhyve/pci_ne2000.c
==============================================================================
--- soc2015/iateaca/bhyve-ne2000-head/usr.sbin/bhyve/pci_ne2000.c	Thu Jun 25 20:01:44 2015	(r287633)
+++ soc2015/iateaca/bhyve-ne2000-head/usr.sbin/bhyve/pci_ne2000.c	Thu Jun 25 20:38:07 2015	(r287634)
@@ -6,6 +6,9 @@
 #include <stdlib.h>
 #include <fcntl.h>
 #include <unistd.h>
+#include <string.h>
+
+#include <net/ethernet.h>
 
 #include "pci_emul.h"
 #include "mevent.h"
@@ -48,6 +51,7 @@
 #define ED_RTL80X9_80X9ID1	0x0b
 #define ED_RTL8029_ID1			0x43
 
+#define MAX_INPUT_LEN		32
 
 /*
  * NE2000 data structures
@@ -126,6 +130,9 @@
 static void
 ne2000_tap_callback(int fd, enum ev_type type, void *param);
 
+static int
+ne2000_parse_input(char *opts, char *tap_name, uint8_t *mac);
+
 /*
  * NE2000 module function definitions
  */
@@ -253,26 +260,77 @@
 }
 
 static int
+ne2000_parse_input(char *opts, char *tap_name, uint8_t *mac)
+{
+	uint8_t len = 0;
+	char *delim = NULL;
+	char *p_mac = NULL;
+	struct ether_addr *addr = NULL;
+
+	if (opts == NULL)
+		return 1;
+
+	len = strlen(opts);
+	if (len >= MAX_INPUT_LEN) {
+		DPRINTF("The input len should be less than %d", MAX_INPUT_LEN);
+		return 1;
+	}
+
+	/* search for mac address in the input string */
+	delim = strchr(opts, ',');
+
+	if (delim != NULL) {
+		/* mark the end of the tap name */
+		*delim = 0;
+
+		/* point to the start of the mac address */
+		p_mac = delim + 1;
+
+		/* parse the mac addres */
+		addr = ether_aton(p_mac);
+
+		/* if the mac address is valid overwrite the default one */
+		if (addr != NULL)
+			memcpy(mac, addr, ETHER_ADDR_LEN);
+		else
+			DPRINTF("Invalid mac");
+	}
+
+	/* copy the tap name */
+	strcpy(tap_name, opts);
+
+	return 0;
+}
+
+static int
 pci_ne2000_init(struct vmctx *ctx, struct pci_devinst *pi, char *opts)
 {
 	struct pci_ne2000_softc *sc = NULL;
 	int err;
 
+	/* the default mac address is 00:a0:98:4a:0e:ee */
+	uint8_t mac[ETHER_ADDR_LEN] = {0x00, 0xa0, 0x98, 0x4a, 0x0e, 0xee};
+	char tap_name[MAX_INPUT_LEN];
+
 #if DEBUG_NE2000 == 1
 	dbg = fopen("/tmp/bhyve_ne2000.log", "w+");
 #endif
 
 	assert(ctx != NULL);
 	assert(pi != NULL);
-	assert(opts != NULL);
 
 	sc = calloc(1, sizeof(struct pci_ne2000_softc));
 	pi->pi_arg = sc;
 	sc->asc_pi = pi;
 
-	/* TODO - implement a better parsing of the input opts and get the name
-	 * of the tap interface */
-	err = ne2000_tap_init(sc, "/dev/tap0");
+	err = ne2000_parse_input(opts, tap_name, mac);
+	if (err != 0) {
+		printf("Use input param like: -s x:y,ne2000-net,tap_name[,mac address]");
+		free(sc);
+		return 1;
+	}
+
+	err = ne2000_tap_init(sc, tap_name);
 	assert(err == 0);
 
 	/* probe a RTL8029 PCI card as a generic NE2000 device */
@@ -293,12 +351,12 @@
 			ED_RTL80X9_CONFIG3, ED_RTL80X9_CF3_FUDUP);
 
 	/* the NE2000 card has his MAC address located in the first 6 words of the RAM memory */
-	sc->ram[0] = 0x00;
-	sc->ram[2] = 0xa0;
-	sc->ram[4] = 0x98;
-	sc->ram[6] = 0x4a;
-	sc->ram[8] = 0x0e;
-	sc->ram[10] = 0xee;
+	sc->ram[0] = mac[0];
+	sc->ram[2] = mac[1];
+	sc->ram[4] = mac[2];
+	sc->ram[6] = mac[3];
+	sc->ram[8] = mac[4];
+	sc->ram[10] = mac[5];
 
 	return 0;
 }



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201506252038.t5PKc7C9048186>