Home | History | Annotate | Line # | Download | only in libvirtif
if_virt.c revision 1.34
      1  1.34  pooka /*	$NetBSD: if_virt.c,v 1.34 2013/07/03 19:23:46 pooka Exp $	*/
      2   1.1  pooka 
      3   1.1  pooka /*
      4  1.32  pooka  * Copyright (c) 2008, 2013 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.34  pooka __KERNEL_RCSID(0, "$NetBSD: if_virt.c,v 1.34 2013/07/03 19:23:46 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.26    tls #include <sys/cprng.h>
     42   1.1  pooka 
     43  1.15  pooka #include <net/bpf.h>
     44   1.1  pooka #include <net/if.h>
     45   1.1  pooka #include <net/if_ether.h>
     46   1.1  pooka #include <net/if_tap.h>
     47   1.1  pooka 
     48   1.1  pooka #include <netinet/in.h>
     49   1.1  pooka #include <netinet/in_var.h>
     50   1.1  pooka 
     51   1.1  pooka #include <rump/rump.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.30  pooka #include "rumpcomp_user.h"
     57  1.30  pooka 
     58   1.1  pooka /*
     59  1.32  pooka  * Virtual interface.  Uses hypercalls to shovel packets back
     60  1.32  pooka  * and forth.  The exact method for shoveling depends on the
     61  1.32  pooka  * hypercall implementation.
     62   1.1  pooka  */
     63   1.1  pooka 
     64  1.32  pooka #ifndef VIRTIF_BASE
     65   1.1  pooka #define VIRTIF_BASE "virt"
     66  1.32  pooka #endif
     67   1.1  pooka 
     68   1.1  pooka static int	virtif_init(struct ifnet *);
     69   1.1  pooka static int	virtif_ioctl(struct ifnet *, u_long, void *);
     70   1.1  pooka static void	virtif_start(struct ifnet *);
     71   1.1  pooka static void	virtif_stop(struct ifnet *, int);
     72   1.1  pooka 
     73   1.1  pooka struct virtif_sc {
     74   1.7  pooka 	struct ethercom sc_ec;
     75  1.30  pooka 	struct virtif_user *sc_viu;
     76  1.21  pooka 	bool sc_dying;
     77  1.21  pooka 	struct lwp *sc_l_snd, *sc_l_rcv;
     78  1.21  pooka 	kmutex_t sc_mtx;
     79  1.21  pooka 	kcondvar_t sc_cv;
     80   1.1  pooka };
     81   1.1  pooka 
     82  1.21  pooka static void virtif_receiver(void *);
     83   1.8  pooka static void virtif_sender(void *);
     84  1.20  pooka static int  virtif_clone(struct if_clone *, int);
     85  1.20  pooka static int  virtif_unclone(struct ifnet *);
     86   1.1  pooka 
     87  1.20  pooka struct if_clone virtif_cloner =
     88  1.20  pooka     IF_CLONE_INITIALIZER(VIRTIF_BASE, virtif_clone, virtif_unclone);
     89   1.1  pooka 
     90  1.34  pooka static int
     91  1.34  pooka virtif_clone(struct if_clone *ifc, int unit)
     92   1.1  pooka {
     93   1.1  pooka 	struct virtif_sc *sc;
     94  1.30  pooka 	struct virtif_user *viu;
     95   1.1  pooka 	struct ifnet *ifp;
     96   1.3  pooka 	uint8_t enaddr[ETHER_ADDR_LEN] = { 0xb2, 0x0a, 0x00, 0x0b, 0x0e, 0x01 };
     97  1.30  pooka 	int error = 0;
     98  1.21  pooka 
     99  1.21  pooka 	if (num >= 0x100)
    100  1.21  pooka 		return E2BIG;
    101   1.1  pooka 
    102  1.31  pooka 	if ((error = rumpcomp_virtif_create(num, &viu)) != 0)
    103  1.31  pooka 		return error;
    104  1.30  pooka 
    105  1.26    tls 	enaddr[2] = cprng_fast32() & 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.30  pooka 	sc->sc_viu = viu;
    111   1.1  pooka 
    112  1.21  pooka 	mutex_init(&sc->sc_mtx, MUTEX_DEFAULT, IPL_NONE);
    113  1.32  pooka 	cv_init(&sc->sc_cv, VIRTIF_BASE "snd");
    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.24  rmind 		if ((error = kthread_create(PRI_NONE, KTHREAD_MUSTJOIN, NULL,
    120  1.32  pooka 		    virtif_receiver, ifp, &sc->sc_l_rcv,
    121  1.32  pooka 		    VIRTIF_BASE "ifr")) != 0)
    122  1.21  pooka 			goto out;
    123  1.21  pooka 
    124  1.21  pooka 		if ((error = kthread_create(PRI_NONE,
    125  1.24  rmind 		    KTHREAD_MUSTJOIN | KTHREAD_MPSAFE, NULL,
    126  1.32  pooka 		    virtif_sender, ifp, &sc->sc_l_snd, VIRTIF_BASE "ifs")) != 0)
    127  1.21  pooka 			goto out;
    128  1.21  pooka 	} else {
    129  1.21  pooka 		printf("WARNING: threads not enabled, receive NOT working\n");
    130  1.21  pooka 	}
    131  1.21  pooka 
    132   1.1  pooka 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
    133   1.1  pooka 	ifp->if_init = virtif_init;
    134   1.1  pooka 	ifp->if_ioctl = virtif_ioctl;
    135   1.1  pooka 	ifp->if_start = virtif_start;
    136   1.1  pooka 	ifp->if_stop = virtif_stop;
    137  1.21  pooka 	IFQ_SET_READY(&ifp->if_snd);
    138   1.1  pooka 
    139   1.1  pooka 	if_attach(ifp);
    140   1.1  pooka 	ether_ifattach(ifp, enaddr);
    141   1.1  pooka 
    142  1.21  pooka  out:
    143  1.21  pooka 	if (error) {
    144  1.21  pooka 		virtif_unclone(ifp);
    145  1.21  pooka 	}
    146  1.21  pooka 
    147  1.21  pooka 	return error;
    148   1.1  pooka }
    149   1.1  pooka 
    150   1.1  pooka static int
    151  1.20  pooka virtif_unclone(struct ifnet *ifp)
    152  1.20  pooka {
    153  1.21  pooka 	struct virtif_sc *sc = ifp->if_softc;
    154  1.20  pooka 
    155  1.21  pooka 	mutex_enter(&sc->sc_mtx);
    156  1.21  pooka 	if (sc->sc_dying) {
    157  1.21  pooka 		mutex_exit(&sc->sc_mtx);
    158  1.21  pooka 		return EINPROGRESS;
    159  1.21  pooka 	}
    160  1.21  pooka 	sc->sc_dying = true;
    161  1.21  pooka 	cv_broadcast(&sc->sc_cv);
    162  1.21  pooka 	mutex_exit(&sc->sc_mtx);
    163  1.21  pooka 
    164  1.30  pooka 	rumpcomp_virtif_dying(sc->sc_viu);
    165  1.30  pooka 
    166  1.21  pooka 	virtif_stop(ifp, 1);
    167  1.21  pooka 	if_down(ifp);
    168  1.21  pooka 
    169  1.21  pooka 	if (sc->sc_l_snd) {
    170  1.21  pooka 		kthread_join(sc->sc_l_snd);
    171  1.21  pooka 		sc->sc_l_snd = NULL;
    172  1.21  pooka 	}
    173  1.21  pooka 	if (sc->sc_l_rcv) {
    174  1.21  pooka 		kthread_join(sc->sc_l_rcv);
    175  1.21  pooka 		sc->sc_l_rcv = NULL;
    176  1.21  pooka 	}
    177  1.21  pooka 
    178  1.30  pooka 	rumpcomp_virtif_destroy(sc->sc_viu);
    179  1.21  pooka 
    180  1.21  pooka 	mutex_destroy(&sc->sc_mtx);
    181  1.21  pooka 	cv_destroy(&sc->sc_cv);
    182  1.21  pooka 	kmem_free(sc, sizeof(*sc));
    183  1.21  pooka 
    184  1.21  pooka 	ether_ifdetach(ifp);
    185  1.21  pooka 	if_detach(ifp);
    186  1.21  pooka 
    187  1.21  pooka 	return 0;
    188  1.20  pooka }
    189  1.20  pooka 
    190  1.20  pooka static int
    191   1.1  pooka virtif_init(struct ifnet *ifp)
    192   1.1  pooka {
    193  1.21  pooka 	struct virtif_sc *sc = ifp->if_softc;
    194   1.1  pooka 
    195   1.1  pooka 	ifp->if_flags |= IFF_RUNNING;
    196  1.21  pooka 
    197  1.21  pooka 	mutex_enter(&sc->sc_mtx);
    198  1.21  pooka 	cv_broadcast(&sc->sc_cv);
    199  1.21  pooka 	mutex_exit(&sc->sc_mtx);
    200   1.8  pooka 
    201   1.1  pooka 	return 0;
    202   1.1  pooka }
    203   1.1  pooka 
    204   1.1  pooka static int
    205   1.1  pooka virtif_ioctl(struct ifnet *ifp, u_long cmd, void *data)
    206   1.1  pooka {
    207   1.1  pooka 	int s, rv;
    208   1.1  pooka 
    209   1.1  pooka 	s = splnet();
    210   1.1  pooka 	rv = ether_ioctl(ifp, cmd, data);
    211   1.9  pooka 	if (rv == ENETRESET)
    212   1.9  pooka 		rv = 0;
    213   1.1  pooka 	splx(s);
    214   1.1  pooka 
    215   1.1  pooka 	return rv;
    216   1.1  pooka }
    217   1.1  pooka 
    218   1.1  pooka static void
    219   1.1  pooka virtif_start(struct ifnet *ifp)
    220   1.1  pooka {
    221   1.1  pooka 	struct virtif_sc *sc = ifp->if_softc;
    222   1.1  pooka 
    223  1.21  pooka 	mutex_enter(&sc->sc_mtx);
    224  1.21  pooka 	ifp->if_flags |= IFF_OACTIVE;
    225  1.21  pooka 	cv_broadcast(&sc->sc_cv);
    226  1.21  pooka 	mutex_exit(&sc->sc_mtx);
    227   1.1  pooka }
    228   1.1  pooka 
    229   1.1  pooka static void
    230   1.1  pooka virtif_stop(struct ifnet *ifp, int disable)
    231   1.1  pooka {
    232  1.21  pooka 	struct virtif_sc *sc = ifp->if_softc;
    233   1.1  pooka 
    234  1.21  pooka 	ifp->if_flags &= ~IFF_RUNNING;
    235  1.21  pooka 
    236  1.21  pooka 	mutex_enter(&sc->sc_mtx);
    237  1.21  pooka 	cv_broadcast(&sc->sc_cv);
    238  1.21  pooka 	mutex_exit(&sc->sc_mtx);
    239   1.1  pooka }
    240   1.1  pooka 
    241  1.21  pooka #define POLLTIMO_MS 1
    242   1.1  pooka static void
    243  1.21  pooka virtif_receiver(void *arg)
    244   1.1  pooka {
    245   1.1  pooka 	struct ifnet *ifp = arg;
    246   1.1  pooka 	struct virtif_sc *sc = ifp->if_softc;
    247   1.1  pooka 	struct mbuf *m;
    248   1.1  pooka 	size_t plen = ETHER_MAX_LEN_JUMBO+1;
    249  1.31  pooka 	size_t n;
    250  1.31  pooka 	int error;
    251  1.21  pooka 
    252   1.1  pooka 	for (;;) {
    253   1.1  pooka 		m = m_gethdr(M_WAIT, MT_DATA);
    254   1.1  pooka 		MEXTMALLOC(m, plen, M_WAIT);
    255   1.1  pooka 
    256  1.11  pooka  again:
    257  1.21  pooka 		if (sc->sc_dying) {
    258  1.21  pooka 			m_freem(m);
    259  1.21  pooka 			break;
    260  1.21  pooka 		}
    261  1.30  pooka 
    262  1.31  pooka 		error = rumpcomp_virtif_recv(sc->sc_viu,
    263  1.31  pooka 		    mtod(m, void *), plen, &n);
    264  1.31  pooka 		if (error) {
    265  1.31  pooka 			printf("%s: read hypercall failed %d. host if down?\n",
    266  1.31  pooka 			    ifp->if_xname, error);
    267  1.21  pooka 			mutex_enter(&sc->sc_mtx);
    268  1.21  pooka 			/* could check if need go, done soon anyway */
    269  1.21  pooka 			cv_timedwait(&sc->sc_cv, &sc->sc_mtx, hz);
    270  1.21  pooka 			mutex_exit(&sc->sc_mtx);
    271  1.21  pooka 			goto again;
    272   1.1  pooka 		}
    273  1.19  pooka 
    274  1.19  pooka 		/* tap sometimes returns EOF.  don't sweat it and plow on */
    275  1.19  pooka 		if (__predict_false(n == 0))
    276  1.19  pooka 			goto again;
    277  1.19  pooka 
    278  1.21  pooka 		/* discard if we're not up */
    279  1.21  pooka 		if ((ifp->if_flags & IFF_RUNNING) == 0)
    280  1.21  pooka 			goto again;
    281  1.21  pooka 
    282   1.1  pooka 		m->m_len = m->m_pkthdr.len = n;
    283   1.1  pooka 		m->m_pkthdr.rcvif = ifp;
    284  1.18  joerg 		bpf_mtap(ifp, m);
    285   1.1  pooka 		ether_input(ifp, m);
    286   1.1  pooka 	}
    287   1.1  pooka 
    288  1.21  pooka 	kthread_exit(0);
    289   1.1  pooka }
    290   1.8  pooka 
    291  1.12  pooka /* lazy bum stetson-harrison magic value */
    292  1.12  pooka #define LB_SH 32
    293   1.8  pooka static void
    294   1.8  pooka virtif_sender(void *arg)
    295   1.8  pooka {
    296   1.8  pooka 	struct ifnet *ifp = arg;
    297   1.8  pooka 	struct virtif_sc *sc = ifp->if_softc;
    298   1.8  pooka 	struct mbuf *m, *m0;
    299  1.30  pooka 	struct iovec io[LB_SH];
    300  1.30  pooka 	int i;
    301   1.8  pooka 
    302  1.21  pooka 	mutex_enter(&sc->sc_mtx);
    303  1.21  pooka 	KERNEL_LOCK(1, NULL);
    304  1.21  pooka 	while (!sc->sc_dying) {
    305  1.23    mrg 		if (!(ifp->if_flags & IFF_RUNNING)) {
    306  1.21  pooka 			cv_wait(&sc->sc_cv, &sc->sc_mtx);
    307  1.21  pooka 			continue;
    308  1.21  pooka 		}
    309   1.8  pooka 		IF_DEQUEUE(&ifp->if_snd, m0);
    310   1.8  pooka 		if (!m0) {
    311  1.21  pooka 			ifp->if_flags &= ~IFF_OACTIVE;
    312  1.21  pooka 			cv_wait(&sc->sc_cv, &sc->sc_mtx);
    313   1.8  pooka 			continue;
    314   1.8  pooka 		}
    315  1.21  pooka 		mutex_exit(&sc->sc_mtx);
    316   1.8  pooka 
    317   1.8  pooka 		m = m0;
    318  1.12  pooka 		for (i = 0; i < LB_SH && m; i++) {
    319   1.8  pooka 			io[i].iov_base = mtod(m, void *);
    320   1.8  pooka 			io[i].iov_len = m->m_len;
    321   1.8  pooka 			m = m->m_next;
    322   1.8  pooka 		}
    323  1.12  pooka 		if (i == LB_SH)
    324   1.8  pooka 			panic("lazy bum");
    325  1.17  joerg 		bpf_mtap(ifp, m0);
    326  1.21  pooka 
    327  1.30  pooka 		rumpcomp_virtif_send(sc->sc_viu, io, i);
    328  1.21  pooka 
    329   1.8  pooka 		m_freem(m0);
    330  1.21  pooka 		mutex_enter(&sc->sc_mtx);
    331   1.8  pooka 	}
    332  1.21  pooka 	KERNEL_UNLOCK_LAST(curlwp);
    333  1.21  pooka 
    334  1.21  pooka 	mutex_exit(&sc->sc_mtx);
    335   1.8  pooka 
    336  1.21  pooka 	kthread_exit(0);
    337   1.8  pooka }
    338