From owner-svn-soc-all@freebsd.org Thu Jun 25 20:38:08 2015 Return-Path: Delivered-To: svn-soc-all@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id 5F3F898DA5F for ; Thu, 25 Jun 2015 20:38:08 +0000 (UTC) (envelope-from iateaca@FreeBSD.org) Received: from socsvn.freebsd.org (socsvn.freebsd.org [IPv6:2001:1900:2254:206a::50:2]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id 4FE9210E1 for ; Thu, 25 Jun 2015 20:38:08 +0000 (UTC) (envelope-from iateaca@FreeBSD.org) Received: from socsvn.freebsd.org ([127.0.1.124]) by socsvn.freebsd.org (8.14.9/8.14.9) with ESMTP id t5PKc8kB048194 for ; Thu, 25 Jun 2015 20:38:08 GMT (envelope-from iateaca@FreeBSD.org) Received: (from www@localhost) by socsvn.freebsd.org (8.14.9/8.14.9/Submit) id t5PKc7C9048186 for svn-soc-all@FreeBSD.org; Thu, 25 Jun 2015 20:38:07 GMT (envelope-from iateaca@FreeBSD.org) Date: Thu, 25 Jun 2015 20:38:07 GMT Message-Id: <201506252038.t5PKc7C9048186@socsvn.freebsd.org> X-Authentication-Warning: socsvn.freebsd.org: www set sender to iateaca@FreeBSD.org using -f From: iateaca@FreeBSD.org To: svn-soc-all@FreeBSD.org Subject: socsvn commit: r287634 - soc2015/iateaca/bhyve-ne2000-head/usr.sbin/bhyve MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-soc-all@freebsd.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: SVN commit messages for the entire Summer of Code repository List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 25 Jun 2015 20:38:08 -0000 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 #include #include +#include + +#include #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; }