Home | History | Annotate | Line # | Download | only in libvirtif
if_virt.c revision 1.35
      1 /*	$NetBSD: if_virt.c,v 1.35 2013/07/03 20:17:07 pooka Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2008, 2013 Antti Kantee.  All Rights Reserved.
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions
      8  * are met:
      9  * 1. Redistributions of source code must retain the above copyright
     10  *    notice, this list of conditions and the following disclaimer.
     11  * 2. Redistributions in binary form must reproduce the above copyright
     12  *    notice, this list of conditions and the following disclaimer in the
     13  *    documentation and/or other materials provided with the distribution.
     14  *
     15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
     16  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     17  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     18  * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     21  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     25  * SUCH DAMAGE.
     26  */
     27 
     28 #include <sys/cdefs.h>
     29 __KERNEL_RCSID(0, "$NetBSD: if_virt.c,v 1.35 2013/07/03 20:17:07 pooka Exp $");
     30 
     31 #include <sys/param.h>
     32 #include <sys/condvar.h>
     33 #include <sys/fcntl.h>
     34 #include <sys/kernel.h>
     35 #include <sys/kmem.h>
     36 #include <sys/kthread.h>
     37 #include <sys/mutex.h>
     38 #include <sys/poll.h>
     39 #include <sys/sockio.h>
     40 #include <sys/socketvar.h>
     41 #include <sys/cprng.h>
     42 
     43 #include <net/bpf.h>
     44 #include <net/if.h>
     45 #include <net/if_ether.h>
     46 #include <net/if_tap.h>
     47 
     48 #include <netinet/in.h>
     49 #include <netinet/in_var.h>
     50 
     51 #include <rump/rump.h>
     52 
     53 #include "rump_private.h"
     54 #include "rump_net_private.h"
     55 
     56 #include "rumpcomp_user.h"
     57 
     58 /*
     59  * Virtual interface.  Uses hypercalls to shovel packets back
     60  * and forth.  The exact method for shoveling depends on the
     61  * hypercall implementation.
     62  */
     63 
     64 #ifndef VIRTIF_BASE
     65 #define VIRTIF_BASE "virt"
     66 #endif
     67 
     68 static int	virtif_init(struct ifnet *);
     69 static int	virtif_ioctl(struct ifnet *, u_long, void *);
     70 static void	virtif_start(struct ifnet *);
     71 static void	virtif_stop(struct ifnet *, int);
     72 
     73 struct virtif_sc {
     74 	struct ethercom sc_ec;
     75 	struct virtif_user *sc_viu;
     76 	bool sc_dying;
     77 	struct lwp *sc_l_snd, *sc_l_rcv;
     78 	kmutex_t sc_mtx;
     79 	kcondvar_t sc_cv;
     80 };
     81 
     82 static void virtif_receiver(void *);
     83 static void virtif_sender(void *);
     84 static int  virtif_clone(struct if_clone *, int);
     85 static int  virtif_unclone(struct ifnet *);
     86 
     87 struct if_clone virtif_cloner =
     88     IF_CLONE_INITIALIZER(VIRTIF_BASE, virtif_clone, virtif_unclone);
     89 
     90 static int
     91 virtif_clone(struct if_clone *ifc, int num)
     92 {
     93 	struct virtif_sc *sc;
     94 	struct virtif_user *viu;
     95 	struct ifnet *ifp;
     96 	uint8_t enaddr[ETHER_ADDR_LEN] = { 0xb2, 0x0a, 0x00, 0x0b, 0x0e, 0x01 };
     97 	int error = 0;
     98 
     99 	if (num >= 0x100)
    100 		return E2BIG;
    101 
    102 	if ((error = rumpcomp_virtif_create(num, &viu)) != 0)
    103 		return error;
    104 
    105 	enaddr[2] = cprng_fast32() & 0xff;
    106 	enaddr[5] = num;
    107 
    108 	sc = kmem_zalloc(sizeof(*sc), KM_SLEEP);
    109 	sc->sc_dying = false;
    110 	sc->sc_viu = viu;
    111 
    112 	mutex_init(&sc->sc_mtx, MUTEX_DEFAULT, IPL_NONE);
    113 	cv_init(&sc->sc_cv, VIRTIF_BASE "snd");
    114 	ifp = &sc->sc_ec.ec_if;
    115 	sprintf(ifp->if_xname, "%s%d", VIRTIF_BASE, num);
    116 	ifp->if_softc = sc;
    117 
    118 	if (rump_threads) {
    119 		if ((error = kthread_create(PRI_NONE, KTHREAD_MUSTJOIN, NULL,
    120 		    virtif_receiver, ifp, &sc->sc_l_rcv,
    121 		    VIRTIF_BASE "ifr")) != 0)
    122 			goto out;
    123 
    124 		if ((error = kthread_create(PRI_NONE,
    125 		    KTHREAD_MUSTJOIN | KTHREAD_MPSAFE, NULL,
    126 		    virtif_sender, ifp, &sc->sc_l_snd, VIRTIF_BASE "ifs")) != 0)
    127 			goto out;
    128 	} else {
    129 		printf("WARNING: threads not enabled, receive NOT working\n");
    130 	}
    131 
    132 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
    133 	ifp->if_init = virtif_init;
    134 	ifp->if_ioctl = virtif_ioctl;
    135 	ifp->if_start = virtif_start;
    136 	ifp->if_stop = virtif_stop;
    137 	IFQ_SET_READY(&ifp->if_snd);
    138 
    139 	if_attach(ifp);
    140 	ether_ifattach(ifp, enaddr);
    141 
    142  out:
    143 	if (error) {
    144 		virtif_unclone(ifp);
    145 	}
    146 
    147 	return error;
    148 }
    149 
    150 static int
    151 virtif_unclone(struct ifnet *ifp)
    152 {
    153 	struct virtif_sc *sc = ifp->if_softc;
    154 
    155 	mutex_enter(&sc->sc_mtx);
    156 	if (sc->sc_dying) {
    157 		mutex_exit(&sc->sc_mtx);
    158 		return EINPROGRESS;
    159 	}
    160 	sc->sc_dying = true;
    161 	cv_broadcast(&sc->sc_cv);
    162 	mutex_exit(&sc->sc_mtx);
    163 
    164 	rumpcomp_virtif_dying(sc->sc_viu);
    165 
    166 	virtif_stop(ifp, 1);
    167 	if_down(ifp);
    168 
    169 	if (sc->sc_l_snd) {
    170 		kthread_join(sc->sc_l_snd);
    171 		sc->sc_l_snd = NULL;
    172 	}
    173 	if (sc->sc_l_rcv) {
    174 		kthread_join(sc->sc_l_rcv);
    175 		sc->sc_l_rcv = NULL;
    176 	}
    177 
    178 	rumpcomp_virtif_destroy(sc->sc_viu);
    179 
    180 	mutex_destroy(&sc->sc_mtx);
    181 	cv_destroy(&sc->sc_cv);
    182 	kmem_free(sc, sizeof(*sc));
    183 
    184 	ether_ifdetach(ifp);
    185 	if_detach(ifp);
    186 
    187 	return 0;
    188 }
    189 
    190 static int
    191 virtif_init(struct ifnet *ifp)
    192 {
    193 	struct virtif_sc *sc = ifp->if_softc;
    194 
    195 	ifp->if_flags |= IFF_RUNNING;
    196 
    197 	mutex_enter(&sc->sc_mtx);
    198 	cv_broadcast(&sc->sc_cv);
    199 	mutex_exit(&sc->sc_mtx);
    200 
    201 	return 0;
    202 }
    203 
    204 static int
    205 virtif_ioctl(struct ifnet *ifp, u_long cmd, void *data)
    206 {
    207 	int s, rv;
    208 
    209 	s = splnet();
    210 	rv = ether_ioctl(ifp, cmd, data);
    211 	if (rv == ENETRESET)
    212 		rv = 0;
    213 	splx(s);
    214 
    215 	return rv;
    216 }
    217 
    218 static void
    219 virtif_start(struct ifnet *ifp)
    220 {
    221 	struct virtif_sc *sc = ifp->if_softc;
    222 
    223 	mutex_enter(&sc->sc_mtx);
    224 	ifp->if_flags |= IFF_OACTIVE;
    225 	cv_broadcast(&sc->sc_cv);
    226 	mutex_exit(&sc->sc_mtx);
    227 }
    228 
    229 static void
    230 virtif_stop(struct ifnet *ifp, int disable)
    231 {
    232 	struct virtif_sc *sc = ifp->if_softc;
    233 
    234 	ifp->if_flags &= ~IFF_RUNNING;
    235 
    236 	mutex_enter(&sc->sc_mtx);
    237 	cv_broadcast(&sc->sc_cv);
    238 	mutex_exit(&sc->sc_mtx);
    239 }
    240 
    241 #define POLLTIMO_MS 1
    242 static void
    243 virtif_receiver(void *arg)
    244 {
    245 	struct ifnet *ifp = arg;
    246 	struct virtif_sc *sc = ifp->if_softc;
    247 	struct mbuf *m;
    248 	size_t plen = ETHER_MAX_LEN_JUMBO+1;
    249 	size_t n;
    250 	int error;
    251 
    252 	for (;;) {
    253 		m = m_gethdr(M_WAIT, MT_DATA);
    254 		MEXTMALLOC(m, plen, M_WAIT);
    255 
    256  again:
    257 		if (sc->sc_dying) {
    258 			m_freem(m);
    259 			break;
    260 		}
    261 
    262 		error = rumpcomp_virtif_recv(sc->sc_viu,
    263 		    mtod(m, void *), plen, &n);
    264 		if (error) {
    265 			printf("%s: read hypercall failed %d. host if down?\n",
    266 			    ifp->if_xname, error);
    267 			mutex_enter(&sc->sc_mtx);
    268 			/* could check if need go, done soon anyway */
    269 			cv_timedwait(&sc->sc_cv, &sc->sc_mtx, hz);
    270 			mutex_exit(&sc->sc_mtx);
    271 			goto again;
    272 		}
    273 
    274 		/* tap sometimes returns EOF.  don't sweat it and plow on */
    275 		if (__predict_false(n == 0))
    276 			goto again;
    277 
    278 		/* discard if we're not up */
    279 		if ((ifp->if_flags & IFF_RUNNING) == 0)
    280 			goto again;
    281 
    282 		m->m_len = m->m_pkthdr.len = n;
    283 		m->m_pkthdr.rcvif = ifp;
    284 		bpf_mtap(ifp, m);
    285 		ether_input(ifp, m);
    286 	}
    287 
    288 	kthread_exit(0);
    289 }
    290 
    291 /* lazy bum stetson-harrison magic value */
    292 #define LB_SH 32
    293 static void
    294 virtif_sender(void *arg)
    295 {
    296 	struct ifnet *ifp = arg;
    297 	struct virtif_sc *sc = ifp->if_softc;
    298 	struct mbuf *m, *m0;
    299 	struct iovec io[LB_SH];
    300 	int i;
    301 
    302 	mutex_enter(&sc->sc_mtx);
    303 	KERNEL_LOCK(1, NULL);
    304 	while (!sc->sc_dying) {
    305 		if (!(ifp->if_flags & IFF_RUNNING)) {
    306 			cv_wait(&sc->sc_cv, &sc->sc_mtx);
    307 			continue;
    308 		}
    309 		IF_DEQUEUE(&ifp->if_snd, m0);
    310 		if (!m0) {
    311 			ifp->if_flags &= ~IFF_OACTIVE;
    312 			cv_wait(&sc->sc_cv, &sc->sc_mtx);
    313 			continue;
    314 		}
    315 		mutex_exit(&sc->sc_mtx);
    316 
    317 		m = m0;
    318 		for (i = 0; i < LB_SH && m; i++) {
    319 			io[i].iov_base = mtod(m, void *);
    320 			io[i].iov_len = m->m_len;
    321 			m = m->m_next;
    322 		}
    323 		if (i == LB_SH)
    324 			panic("lazy bum");
    325 		bpf_mtap(ifp, m0);
    326 
    327 		rumpcomp_virtif_send(sc->sc_viu, io, i);
    328 
    329 		m_freem(m0);
    330 		mutex_enter(&sc->sc_mtx);
    331 	}
    332 	KERNEL_UNLOCK_LAST(curlwp);
    333 
    334 	mutex_exit(&sc->sc_mtx);
    335 
    336 	kthread_exit(0);
    337 }
    338