Home | History | Annotate | Line # | Download | only in libvirtif
if_virt.c revision 1.21
      1  1.21  pooka /*	$NetBSD: if_virt.c,v 1.21 2010/11/15 20:23:11 pooka Exp $	*/
      2   1.1  pooka 
      3   1.1  pooka /*
      4   1.1  pooka  * Copyright (c) 2008 Antti Kantee.  All Rights Reserved.
      5   1.1  pooka  *
      6   1.1  pooka  * Redistribution and use in source and binary forms, with or without
      7   1.1  pooka  * modification, are permitted provided that the following conditions
      8   1.1  pooka  * are met:
      9   1.1  pooka  * 1. Redistributions of source code must retain the above copyright
     10   1.1  pooka  *    notice, this list of conditions and the following disclaimer.
     11   1.1  pooka  * 2. Redistributions in binary form must reproduce the above copyright
     12   1.1  pooka  *    notice, this list of conditions and the following disclaimer in the
     13   1.1  pooka  *    documentation and/or other materials provided with the distribution.
     14   1.1  pooka  *
     15   1.1  pooka  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
     16   1.1  pooka  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     17   1.1  pooka  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     18   1.1  pooka  * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     19   1.1  pooka  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     20   1.1  pooka  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     21   1.1  pooka  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     22   1.1  pooka  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     23   1.1  pooka  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     24   1.1  pooka  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     25   1.1  pooka  * SUCH DAMAGE.
     26   1.1  pooka  */
     27   1.1  pooka 
     28   1.5  pooka #include <sys/cdefs.h>
     29  1.21  pooka __KERNEL_RCSID(0, "$NetBSD: if_virt.c,v 1.21 2010/11/15 20:23:11 pooka Exp $");
     30   1.5  pooka 
     31   1.1  pooka #include <sys/param.h>
     32   1.1  pooka #include <sys/condvar.h>
     33   1.1  pooka #include <sys/fcntl.h>
     34  1.21  pooka #include <sys/kernel.h>
     35   1.1  pooka #include <sys/kmem.h>
     36   1.1  pooka #include <sys/kthread.h>
     37   1.1  pooka #include <sys/mutex.h>
     38  1.11  pooka #include <sys/poll.h>
     39   1.1  pooka #include <sys/sockio.h>
     40   1.1  pooka #include <sys/socketvar.h>
     41   1.1  pooka 
     42  1.15  pooka #include <net/bpf.h>
     43   1.1  pooka #include <net/if.h>
     44   1.1  pooka #include <net/if_ether.h>
     45   1.1  pooka #include <net/if_tap.h>
     46   1.1  pooka 
     47   1.1  pooka #include <netinet/in.h>
     48   1.1  pooka #include <netinet/in_var.h>
     49   1.1  pooka 
     50   1.1  pooka #include <rump/rump.h>
     51   1.1  pooka #include <rump/rumpuser.h>
     52   1.1  pooka 
     53   1.1  pooka #include "rump_private.h"
     54  1.10  pooka #include "rump_net_private.h"
     55   1.1  pooka 
     56   1.1  pooka /*
     57   1.1  pooka  * Virtual interface for userspace purposes.  Uses tap(4) to
     58   1.1  pooka  * interface with the kernel and just simply shovels data
     59   1.1  pooka  * to/from /dev/tap.
     60   1.1  pooka  */
     61   1.1  pooka 
     62   1.1  pooka #define VIRTIF_BASE "virt"
     63   1.1  pooka 
     64   1.1  pooka static int	virtif_init(struct ifnet *);
     65   1.1  pooka static int	virtif_ioctl(struct ifnet *, u_long, void *);
     66   1.1  pooka static void	virtif_start(struct ifnet *);
     67   1.1  pooka static void	virtif_stop(struct ifnet *, int);
     68   1.1  pooka 
     69   1.1  pooka struct virtif_sc {
     70   1.7  pooka 	struct ethercom sc_ec;
     71   1.1  pooka 	int sc_tapfd;
     72  1.21  pooka 	bool sc_dying;
     73  1.21  pooka 	struct lwp *sc_l_snd, *sc_l_rcv;
     74  1.21  pooka 	kmutex_t sc_mtx;
     75  1.21  pooka 	kcondvar_t sc_cv;
     76   1.1  pooka };
     77   1.1  pooka 
     78  1.21  pooka static void virtif_receiver(void *);
     79   1.8  pooka static void virtif_sender(void *);
     80  1.20  pooka static int  virtif_clone(struct if_clone *, int);
     81  1.20  pooka static int  virtif_unclone(struct ifnet *);
     82   1.1  pooka 
     83  1.20  pooka struct if_clone virtif_cloner =
     84  1.20  pooka     IF_CLONE_INITIALIZER(VIRTIF_BASE, virtif_clone, virtif_unclone);
     85   1.1  pooka 
     86   1.8  pooka int
     87  1.14  pooka rump_virtif_create(int num)
     88   1.1  pooka {
     89   1.1  pooka 	struct virtif_sc *sc;
     90   1.1  pooka 	struct ifnet *ifp;
     91   1.3  pooka 	uint8_t enaddr[ETHER_ADDR_LEN] = { 0xb2, 0x0a, 0x00, 0x0b, 0x0e, 0x01 };
     92   1.8  pooka 	char tapdev[16];
     93  1.21  pooka 	int fd, error = 0;
     94  1.21  pooka 
     95  1.21  pooka 	if (num >= 0x100)
     96  1.21  pooka 		return E2BIG;
     97   1.1  pooka 
     98   1.8  pooka 	snprintf(tapdev, sizeof(tapdev), "/dev/tap%d", num);
     99   1.8  pooka 	fd = rumpuser_open(tapdev, O_RDWR, &error);
    100   1.1  pooka 	if (fd == -1) {
    101  1.19  pooka 		printf("virtif_create: can't open /dev/tap%d: %d\n",
    102  1.19  pooka 		    num, error);
    103   1.1  pooka 		return error;
    104   1.1  pooka 	}
    105   1.8  pooka 	enaddr[2] = arc4random() & 0xff;
    106   1.8  pooka 	enaddr[5] = num;
    107   1.1  pooka 
    108   1.1  pooka 	sc = kmem_zalloc(sizeof(*sc), KM_SLEEP);
    109  1.21  pooka 	sc->sc_dying = false;
    110   1.1  pooka 	sc->sc_tapfd = fd;
    111   1.1  pooka 
    112  1.21  pooka 	mutex_init(&sc->sc_mtx, MUTEX_DEFAULT, IPL_NONE);
    113  1.21  pooka 	cv_init(&sc->sc_cv, "virtsnd");
    114   1.7  pooka 	ifp = &sc->sc_ec.ec_if;
    115   1.8  pooka 	sprintf(ifp->if_xname, "%s%d", VIRTIF_BASE, num);
    116   1.1  pooka 	ifp->if_softc = sc;
    117  1.21  pooka 
    118  1.21  pooka 	if (rump_threads) {
    119  1.21  pooka 		if ((error = kthread_create(PRI_NONE, KTHREAD_JOINABLE, NULL,
    120  1.21  pooka 		    virtif_receiver, ifp, &sc->sc_l_rcv, "virtifr")) != 0)
    121  1.21  pooka 			goto out;
    122  1.21  pooka 
    123  1.21  pooka 		if ((error = kthread_create(PRI_NONE,
    124  1.21  pooka 		    KTHREAD_JOINABLE | KTHREAD_MPSAFE, NULL,
    125  1.21  pooka 		    virtif_sender, ifp, &sc->sc_l_snd, "virtifs")) != 0)
    126  1.21  pooka 			goto out;
    127  1.21  pooka 	} else {
    128  1.21  pooka 		printf("WARNING: threads not enabled, receive NOT working\n");
    129  1.21  pooka 	}
    130  1.21  pooka 
    131   1.1  pooka 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
    132   1.1  pooka 	ifp->if_init = virtif_init;
    133   1.1  pooka 	ifp->if_ioctl = virtif_ioctl;
    134   1.1  pooka 	ifp->if_start = virtif_start;
    135   1.1  pooka 	ifp->if_stop = virtif_stop;
    136  1.21  pooka 	IFQ_SET_READY(&ifp->if_snd);
    137   1.1  pooka 
    138   1.1  pooka 	if_attach(ifp);
    139   1.1  pooka 	ether_ifattach(ifp, enaddr);
    140   1.1  pooka 
    141  1.21  pooka  out:
    142  1.21  pooka 	if (error) {
    143  1.21  pooka 		virtif_unclone(ifp);
    144  1.21  pooka 	}
    145  1.21  pooka 
    146  1.21  pooka 	return error;
    147   1.1  pooka }
    148   1.1  pooka 
    149   1.1  pooka static int
    150  1.20  pooka virtif_clone(struct if_clone *ifc, int unit)
    151  1.20  pooka {
    152  1.20  pooka 
    153  1.20  pooka 	return rump_virtif_create(unit);
    154  1.20  pooka }
    155  1.20  pooka 
    156  1.20  pooka static int
    157  1.20  pooka virtif_unclone(struct ifnet *ifp)
    158  1.20  pooka {
    159  1.21  pooka 	struct virtif_sc *sc = ifp->if_softc;
    160  1.20  pooka 
    161  1.21  pooka 	mutex_enter(&sc->sc_mtx);
    162  1.21  pooka 	if (sc->sc_dying) {
    163  1.21  pooka 		mutex_exit(&sc->sc_mtx);
    164  1.21  pooka 		return EINPROGRESS;
    165  1.21  pooka 	}
    166  1.21  pooka 	sc->sc_dying = true;
    167  1.21  pooka 	cv_broadcast(&sc->sc_cv);
    168  1.21  pooka 	mutex_exit(&sc->sc_mtx);
    169  1.21  pooka 
    170  1.21  pooka 	virtif_stop(ifp, 1);
    171  1.21  pooka 	if_down(ifp);
    172  1.21  pooka 
    173  1.21  pooka 	if (sc->sc_l_snd) {
    174  1.21  pooka 		kthread_join(sc->sc_l_snd);
    175  1.21  pooka 		sc->sc_l_snd = NULL;
    176  1.21  pooka 	}
    177  1.21  pooka 	if (sc->sc_l_rcv) {
    178  1.21  pooka 		kthread_join(sc->sc_l_rcv);
    179  1.21  pooka 		sc->sc_l_rcv = NULL;
    180  1.21  pooka 	}
    181  1.21  pooka 
    182  1.21  pooka 	rumpuser_close(sc->sc_tapfd, NULL);
    183  1.21  pooka 
    184  1.21  pooka 	mutex_destroy(&sc->sc_mtx);
    185  1.21  pooka 	cv_destroy(&sc->sc_cv);
    186  1.21  pooka 	kmem_free(sc, sizeof(*sc));
    187  1.21  pooka 
    188  1.21  pooka 	ether_ifdetach(ifp);
    189  1.21  pooka 	if_detach(ifp);
    190  1.21  pooka 
    191  1.21  pooka 	return 0;
    192  1.20  pooka }
    193  1.20  pooka 
    194  1.20  pooka static int
    195   1.1  pooka virtif_init(struct ifnet *ifp)
    196   1.1  pooka {
    197  1.21  pooka 	struct virtif_sc *sc = ifp->if_softc;
    198   1.1  pooka 
    199   1.1  pooka 	ifp->if_flags |= IFF_RUNNING;
    200  1.21  pooka 
    201  1.21  pooka 	mutex_enter(&sc->sc_mtx);
    202  1.21  pooka 	cv_broadcast(&sc->sc_cv);
    203  1.21  pooka 	mutex_exit(&sc->sc_mtx);
    204   1.8  pooka 
    205   1.1  pooka 	return 0;
    206   1.1  pooka }
    207   1.1  pooka 
    208   1.1  pooka static int
    209   1.1  pooka virtif_ioctl(struct ifnet *ifp, u_long cmd, void *data)
    210   1.1  pooka {
    211   1.1  pooka 	int s, rv;
    212   1.1  pooka 
    213   1.1  pooka 	s = splnet();
    214   1.1  pooka 	rv = ether_ioctl(ifp, cmd, data);
    215   1.9  pooka 	if (rv == ENETRESET)
    216   1.9  pooka 		rv = 0;
    217   1.1  pooka 	splx(s);
    218   1.1  pooka 
    219   1.1  pooka 	return rv;
    220   1.1  pooka }
    221   1.1  pooka 
    222   1.1  pooka /* just send everything in-context */
    223   1.1  pooka static void
    224   1.1  pooka virtif_start(struct ifnet *ifp)
    225   1.1  pooka {
    226   1.1  pooka 	struct virtif_sc *sc = ifp->if_softc;
    227   1.1  pooka 
    228  1.21  pooka 	mutex_enter(&sc->sc_mtx);
    229  1.21  pooka 	ifp->if_flags |= IFF_OACTIVE;
    230  1.21  pooka 	cv_broadcast(&sc->sc_cv);
    231  1.21  pooka 	mutex_exit(&sc->sc_mtx);
    232   1.1  pooka }
    233   1.1  pooka 
    234   1.1  pooka static void
    235   1.1  pooka virtif_stop(struct ifnet *ifp, int disable)
    236   1.1  pooka {
    237  1.21  pooka 	struct virtif_sc *sc = ifp->if_softc;
    238   1.1  pooka 
    239  1.21  pooka 	ifp->if_flags &= ~IFF_RUNNING;
    240  1.21  pooka 
    241  1.21  pooka 	mutex_enter(&sc->sc_mtx);
    242  1.21  pooka 	cv_broadcast(&sc->sc_cv);
    243  1.21  pooka 	mutex_exit(&sc->sc_mtx);
    244   1.1  pooka }
    245   1.1  pooka 
    246  1.21  pooka #define POLLTIMO_MS 1
    247   1.1  pooka static void
    248  1.21  pooka virtif_receiver(void *arg)
    249   1.1  pooka {
    250   1.1  pooka 	struct ifnet *ifp = arg;
    251   1.1  pooka 	struct virtif_sc *sc = ifp->if_softc;
    252   1.1  pooka 	struct mbuf *m;
    253   1.1  pooka 	size_t plen = ETHER_MAX_LEN_JUMBO+1;
    254  1.21  pooka 	struct pollfd pfd;
    255   1.1  pooka 	ssize_t n;
    256  1.21  pooka 	int error, rv;
    257  1.21  pooka 
    258  1.21  pooka 	pfd.fd = sc->sc_tapfd;
    259  1.21  pooka 	pfd.events = POLLIN;
    260  1.21  pooka 
    261  1.21  pooka 	KASSERT(rump_kernel_isbiglocked());
    262   1.1  pooka 
    263   1.1  pooka 	for (;;) {
    264   1.1  pooka 		m = m_gethdr(M_WAIT, MT_DATA);
    265   1.1  pooka 		MEXTMALLOC(m, plen, M_WAIT);
    266   1.1  pooka 
    267  1.11  pooka  again:
    268  1.21  pooka 		/* poll, but periodically check if we should die */
    269  1.21  pooka 		rv = rumpuser_poll(&pfd, 1, POLLTIMO_MS, &error);
    270  1.21  pooka 		if (sc->sc_dying) {
    271  1.21  pooka 			m_freem(m);
    272  1.21  pooka 			break;
    273  1.21  pooka 		}
    274  1.21  pooka 		if (rv == 0)
    275  1.21  pooka 			goto again;
    276  1.21  pooka 
    277   1.1  pooka 		n = rumpuser_read(sc->sc_tapfd, mtod(m, void *), plen, &error);
    278   1.1  pooka 		KASSERT(n < ETHER_MAX_LEN_JUMBO);
    279  1.19  pooka 		if (__predict_false(n < 0)) {
    280  1.11  pooka 			if (n == -1 && error == EAGAIN) {
    281  1.11  pooka 				goto again;
    282  1.11  pooka 			}
    283  1.19  pooka 
    284  1.21  pooka 			printf("%s: read from /dev/tap failed. host if down?\n",
    285  1.21  pooka 			    ifp->if_xname);
    286  1.21  pooka 			mutex_enter(&sc->sc_mtx);
    287  1.21  pooka 			/* could check if need go, done soon anyway */
    288  1.21  pooka 			cv_timedwait(&sc->sc_cv, &sc->sc_mtx, hz);
    289  1.21  pooka 			mutex_exit(&sc->sc_mtx);
    290  1.21  pooka 			goto again;
    291   1.1  pooka 		}
    292  1.19  pooka 
    293  1.19  pooka 		/* tap sometimes returns EOF.  don't sweat it and plow on */
    294  1.19  pooka 		if (__predict_false(n == 0))
    295  1.19  pooka 			goto again;
    296  1.19  pooka 
    297  1.21  pooka 		/* discard if we're not up */
    298  1.21  pooka 		if ((ifp->if_flags & IFF_RUNNING) == 0)
    299  1.21  pooka 			goto again;
    300  1.21  pooka 
    301   1.1  pooka 		m->m_len = m->m_pkthdr.len = n;
    302   1.1  pooka 		m->m_pkthdr.rcvif = ifp;
    303  1.18  joerg 		bpf_mtap(ifp, m);
    304   1.1  pooka 		ether_input(ifp, m);
    305   1.1  pooka 	}
    306   1.1  pooka 
    307  1.21  pooka 	kthread_exit(0);
    308   1.1  pooka }
    309   1.8  pooka 
    310  1.12  pooka /* lazy bum stetson-harrison magic value */
    311  1.12  pooka #define LB_SH 32
    312   1.8  pooka static void
    313   1.8  pooka virtif_sender(void *arg)
    314   1.8  pooka {
    315   1.8  pooka 	struct ifnet *ifp = arg;
    316   1.8  pooka 	struct virtif_sc *sc = ifp->if_softc;
    317   1.8  pooka 	struct mbuf *m, *m0;
    318  1.12  pooka 	struct rumpuser_iovec io[LB_SH];
    319   1.8  pooka 	int i, error;
    320   1.8  pooka 
    321  1.21  pooka 	mutex_enter(&sc->sc_mtx);
    322  1.21  pooka 	KERNEL_LOCK(1, NULL);
    323  1.21  pooka 	while (!sc->sc_dying) {
    324  1.21  pooka 		if (!ifp->if_flags & IFF_RUNNING) {
    325  1.21  pooka 			cv_wait(&sc->sc_cv, &sc->sc_mtx);
    326  1.21  pooka 			continue;
    327  1.21  pooka 		}
    328   1.8  pooka 		IF_DEQUEUE(&ifp->if_snd, m0);
    329   1.8  pooka 		if (!m0) {
    330  1.21  pooka 			ifp->if_flags &= ~IFF_OACTIVE;
    331  1.21  pooka 			cv_wait(&sc->sc_cv, &sc->sc_mtx);
    332   1.8  pooka 			continue;
    333   1.8  pooka 		}
    334  1.21  pooka 		mutex_exit(&sc->sc_mtx);
    335   1.8  pooka 
    336   1.8  pooka 		m = m0;
    337  1.12  pooka 		for (i = 0; i < LB_SH && m; i++) {
    338   1.8  pooka 			io[i].iov_base = mtod(m, void *);
    339   1.8  pooka 			io[i].iov_len = m->m_len;
    340   1.8  pooka 			m = m->m_next;
    341   1.8  pooka 		}
    342  1.12  pooka 		if (i == LB_SH)
    343   1.8  pooka 			panic("lazy bum");
    344  1.17  joerg 		bpf_mtap(ifp, m0);
    345  1.21  pooka 		KERNEL_UNLOCK_LAST(curlwp);
    346  1.21  pooka 
    347   1.8  pooka 		rumpuser_writev(sc->sc_tapfd, io, i, &error);
    348  1.21  pooka 
    349  1.21  pooka 		KERNEL_LOCK(1, NULL);
    350   1.8  pooka 		m_freem(m0);
    351  1.21  pooka 		mutex_enter(&sc->sc_mtx);
    352   1.8  pooka 	}
    353  1.21  pooka 	KERNEL_UNLOCK_LAST(curlwp);
    354  1.21  pooka 
    355  1.21  pooka 	mutex_exit(&sc->sc_mtx);
    356   1.8  pooka 
    357  1.21  pooka 	kthread_exit(0);
    358   1.8  pooka }
    359  1.10  pooka 
    360  1.10  pooka /*
    361  1.10  pooka  * dummyif is a nada-interface.
    362  1.10  pooka  * As it requires nothing external, it can be used for testing
    363  1.10  pooka  * interface configuration.
    364  1.10  pooka  */
    365  1.10  pooka static int	dummyif_init(struct ifnet *);
    366  1.10  pooka static void	dummyif_start(struct ifnet *);
    367  1.10  pooka 
    368  1.10  pooka void
    369  1.10  pooka rump_dummyif_create()
    370  1.10  pooka {
    371  1.10  pooka 	struct ifnet *ifp;
    372  1.10  pooka 	struct ethercom *ec;
    373  1.10  pooka 	uint8_t enaddr[ETHER_ADDR_LEN] = { 0xb2, 0x0a, 0x00, 0x0b, 0x0e, 0x01 };
    374  1.10  pooka 
    375  1.10  pooka 	enaddr[2] = arc4random() & 0xff;
    376  1.10  pooka 	enaddr[5] = arc4random() & 0xff;
    377  1.10  pooka 
    378  1.10  pooka 	ec = kmem_zalloc(sizeof(*ec), KM_SLEEP);
    379  1.10  pooka 
    380  1.10  pooka 	ifp = &ec->ec_if;
    381  1.10  pooka 	strlcpy(ifp->if_xname, "dummy0", sizeof(ifp->if_xname));
    382  1.10  pooka 	ifp->if_softc = ifp;
    383  1.10  pooka 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
    384  1.10  pooka 	ifp->if_init = dummyif_init;
    385  1.10  pooka 	ifp->if_ioctl = virtif_ioctl;
    386  1.10  pooka 	ifp->if_start = dummyif_start;
    387  1.10  pooka 
    388  1.10  pooka 	if_attach(ifp);
    389  1.10  pooka 	ether_ifattach(ifp, enaddr);
    390  1.10  pooka }
    391  1.10  pooka 
    392  1.10  pooka static int
    393  1.10  pooka dummyif_init(struct ifnet *ifp)
    394  1.10  pooka {
    395  1.10  pooka 
    396  1.10  pooka 	ifp->if_flags |= IFF_RUNNING;
    397  1.10  pooka 	return 0;
    398  1.10  pooka }
    399  1.10  pooka 
    400  1.10  pooka static void
    401  1.10  pooka dummyif_start(struct ifnet *ifp)
    402  1.10  pooka {
    403  1.10  pooka 
    404  1.10  pooka }
    405