Module Name:    src
Committed By:   pooka
Date:           Fri Mar 27 13:46:35 UTC 2009

Modified Files:
        src/sys/rump/net/lib/libvirtif: if_virt.c
        src/sys/rump/net/rumptest: rumptest_net.c

Log Message:
* make interface creation open /dev/tapn for interface n
* create "unique" enaddr
* do send in async context


To generate a diff of this commit:
cvs rdiff -u -r1.7 -r1.8 src/sys/rump/net/lib/libvirtif/if_virt.c
cvs rdiff -u -r1.9 -r1.10 src/sys/rump/net/rumptest/rumptest_net.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/rump/net/lib/libvirtif/if_virt.c
diff -u src/sys/rump/net/lib/libvirtif/if_virt.c:1.7 src/sys/rump/net/lib/libvirtif/if_virt.c:1.8
--- src/sys/rump/net/lib/libvirtif/if_virt.c:1.7	Sat Feb 28 12:29:27 2009
+++ src/sys/rump/net/lib/libvirtif/if_virt.c	Fri Mar 27 13:46:35 2009
@@ -1,4 +1,4 @@
-/*	$NetBSD: if_virt.c,v 1.7 2009/02/28 12:29:27 pooka Exp $	*/
+/*	$NetBSD: if_virt.c,v 1.8 2009/03/27 13:46:35 pooka Exp $	*/
 
 /*
  * Copyright (c) 2008 Antti Kantee.  All Rights Reserved.
@@ -26,7 +26,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: if_virt.c,v 1.7 2009/02/28 12:29:27 pooka Exp $");
+__KERNEL_RCSID(0, "$NetBSD: if_virt.c,v 1.8 2009/03/27 13:46:35 pooka Exp $");
 
 #include <sys/param.h>
 #include <sys/condvar.h>
@@ -56,7 +56,6 @@
  */
 
 #define VIRTIF_BASE "virt"
-static int nunits;
 
 static int	virtif_init(struct ifnet *);
 static int	virtif_ioctl(struct ifnet *, u_long, void *);
@@ -65,22 +64,15 @@
 
 struct virtif_sc {
 	struct ethercom sc_ec;
-	char sc_tapname[IFNAMSIZ];
 	int sc_tapfd;
+	kmutex_t sc_sendmtx;
+	kcondvar_t sc_sendcv;
 };
 
-static int virtif_create(struct ifaliasreq *, struct ifnet **);
 static void virtif_worker(void *);
+static void virtif_sender(void *);
 
-/* XXXXX: this prototype needs to go elsewhere */
-int rump_virtif_create(struct ifaliasreq *, struct ifnet **);
-int
-rump_virtif_create(struct ifaliasreq *ia, struct ifnet **ifpp)
-{
-
-	return virtif_create(ia, ifpp);
-}
-
+#if 0
 /*
  * Create a socket and call ifioctl() to configure the interface.
  * This trickles down to virtif_ioctl().
@@ -101,40 +93,32 @@
 
 	return error;
 }
+#endif
 
-static int
-virtif_create(struct ifaliasreq *ia, struct ifnet **ifpp)
+int
+rump_virtif_create(int num)
 {
 	struct virtif_sc *sc;
-	struct ifreq ifr;
 	struct ifnet *ifp;
 	uint8_t enaddr[ETHER_ADDR_LEN] = { 0xb2, 0x0a, 0x00, 0x0b, 0x0e, 0x01 };
-	int fd, rv, error;
-	int mynum;
+	char tapdev[16];
+	int fd, error;
 
-	/*
-	 * XXX: this is currently un-sane.  Need to figure out a way
-	 * to configure this with a bridge into a sane network without
-	 * hardcoding it.
-	 */
-	fd = rumpuser_open("/dev/tap0", O_RDWR, &error);
+	snprintf(tapdev, sizeof(tapdev), "/dev/tap%d", num);
+	fd = rumpuser_open(tapdev, O_RDWR, &error);
 	if (fd == -1) {
 		printf("virtif_create: can't open /dev/tap %d\n", error);
 		return error;
 	}
+	KASSERT(num < 0x100);
+	enaddr[2] = arc4random() & 0xff;
+	enaddr[5] = num;
 
 	sc = kmem_zalloc(sizeof(*sc), KM_SLEEP);
-	rv = rumpuser_ioctl(fd, TAPGIFNAME, &ifr, &error);
-	if (rv == -1) {
-		kmem_free(sc, sizeof(*sc));
-		return error;
-	}
-	strlcpy(sc->sc_tapname, ifr.ifr_name, IFNAMSIZ);
 	sc->sc_tapfd = fd;
 
 	ifp = &sc->sc_ec.ec_if;
-	mynum = nunits++;
-	sprintf(ifp->if_xname, "%s%d", VIRTIF_BASE, mynum);
+	sprintf(ifp->if_xname, "%s%d", VIRTIF_BASE, num);
 	ifp->if_softc = sc;
 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
 	ifp->if_init = virtif_init;
@@ -142,34 +126,37 @@
 	ifp->if_start = virtif_start;
 	ifp->if_stop = virtif_stop;
 
-	if (rump_threads) {
-		rv = kthread_create(PRI_NONE, 0, NULL, virtif_worker, ifp,
-		    NULL, "virtifi");
-		if (rv) {
-			kmem_free(sc, sizeof(*sc));
-			return rv;
-		}
-	} else {
-		printf("WARNING: threads not enabled, receive NOT working\n");
-	}
+	mutex_init(&sc->sc_sendmtx, MUTEX_DEFAULT, IPL_NONE);
+	cv_init(&sc->sc_sendcv, "virtsnd");
 
 	if_attach(ifp);
 	ether_ifattach(ifp, enaddr);
 
-	if (ia)
-		configaddr(ifp, ia);
-	if (ifpp)
-		*ifpp = ifp;
-
 	return 0;
 }
 
 static int
 virtif_init(struct ifnet *ifp)
 {
+	int rv;
 
+	if (rump_threads) {
+		rv = kthread_create(PRI_NONE, 0, NULL, virtif_worker, ifp,
+		    NULL, "virtifi");
+		/* XXX: should do proper cleanup */
+		if (rv) {
+			panic("if_virt: can't create worker");
+		}
+		rv = kthread_create(PRI_NONE, 0, NULL, virtif_sender, ifp,
+		    NULL, "virtifs");
+		if (rv) {
+			panic("if_virt: can't create sender");
+		}
+	} else {
+		printf("WARNING: threads not enabled, receive NOT working\n");
+	}
 	ifp->if_flags |= IFF_RUNNING;
-
+	
 	return 0;
 }
 
@@ -190,32 +177,10 @@
 virtif_start(struct ifnet *ifp)
 {
 	struct virtif_sc *sc = ifp->if_softc;
-	struct rumpuser_iovec io[16];
-	struct mbuf *m, *m0;
-	int s, i, error;
-
-	s = splnet();
-	for (;;) {
-		IF_DEQUEUE(&ifp->if_snd, m0);
-		if (!m0)
-			break;
-		splx(s);
-
-		m = m0;
-		for (i = 0; i < 16 && m; i++) {
-			io[i].iov_base = mtod(m, void *);
-			io[i].iov_len = m->m_len;
-			m = m->m_next;
-		}
-		if (i == 16)
-			panic("lazy bum");
-		rumpuser_writev(sc->sc_tapfd, io, i, &error);
-		m_freem(m0);
-		s = splnet();
-	}
 
-	ifp->if_flags &= ~IFF_OACTIVE;
-	splx(s);
+	mutex_enter(&sc->sc_sendmtx);
+	cv_signal(&sc->sc_sendcv);
+	mutex_exit(&sc->sc_sendmtx);
 }
 
 static void
@@ -252,3 +217,37 @@
 
 	panic("virtif_workin is a lazy boy %d\n", error);
 }
+
+static void
+virtif_sender(void *arg)
+{
+	struct ifnet *ifp = arg;
+	struct virtif_sc *sc = ifp->if_softc;
+	struct mbuf *m, *m0;
+	struct rumpuser_iovec io[16];
+	int i, error;
+
+	mutex_enter(&sc->sc_sendmtx);
+	for (;;) {
+		IF_DEQUEUE(&ifp->if_snd, m0);
+		if (!m0) {
+			cv_wait(&sc->sc_sendcv, &sc->sc_sendmtx);
+			continue;
+		}
+		mutex_exit(&sc->sc_sendmtx);
+
+		m = m0;
+		for (i = 0; i < 16 && m; i++) {
+			io[i].iov_base = mtod(m, void *);
+			io[i].iov_len = m->m_len;
+			m = m->m_next;
+		}
+		if (i == 16)
+			panic("lazy bum");
+		rumpuser_writev(sc->sc_tapfd, io, i, &error);
+		m_freem(m0);
+		mutex_enter(&sc->sc_sendmtx);
+	}
+
+	mutex_exit(softnet_lock);
+}

Index: src/sys/rump/net/rumptest/rumptest_net.c
diff -u src/sys/rump/net/rumptest/rumptest_net.c:1.9 src/sys/rump/net/rumptest/rumptest_net.c:1.10
--- src/sys/rump/net/rumptest/rumptest_net.c:1.9	Fri Jan 23 19:36:01 2009
+++ src/sys/rump/net/rumptest/rumptest_net.c	Fri Mar 27 13:46:34 2009
@@ -1,4 +1,4 @@
-/*	$NetBSD: rumptest_net.c,v 1.9 2009/01/23 19:36:01 pooka Exp $	*/
+/*	$NetBSD: rumptest_net.c,v 1.10 2009/03/27 13:46:34 pooka Exp $	*/
 
 /*
  * Copyright (c) 2008 Antti Kantee.  All Rights Reserved.
@@ -69,7 +69,6 @@
 #define MYGW "10.181.181.1"
 #define IFNAME "virt0" /* XXX: hardcoded */
 
-int rump_virtif_create(void *, void *); /* XXX: bad hack, prototype is wrong */
 static void
 configure_interface(void)
 {
@@ -85,7 +84,7 @@
 	uint8_t *bp = &m_rtmsg.m_space;
 	int s, rv;
 
-	if ((rv = rump_virtif_create(NULL, NULL)) != 0) {
+	if ((rv = rump_virtif_create(0)) != 0) {
 		printf("could not configure interface %d\n", rv);
 		exit(1);
 	}

Reply via email to