Home | History | Annotate | Line # | Download | only in libvirtif
if_virt.c revision 1.7
      1 /*	$NetBSD: if_virt.c,v 1.7 2009/02/28 12:29:27 pooka Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2008 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.7 2009/02/28 12:29:27 pooka Exp $");
     30 
     31 #include <sys/param.h>
     32 #include <sys/condvar.h>
     33 #include <sys/fcntl.h>
     34 #include <sys/kmem.h>
     35 #include <sys/kthread.h>
     36 #include <sys/mutex.h>
     37 #include <sys/sockio.h>
     38 #include <sys/socketvar.h>
     39 
     40 #include <net/if.h>
     41 #include <net/if_ether.h>
     42 #include <net/if_tap.h>
     43 
     44 #include <netinet/in.h>
     45 #include <netinet/in_var.h>
     46 
     47 #include <rump/rump.h>
     48 #include <rump/rumpuser.h>
     49 
     50 #include "rump_private.h"
     51 
     52 /*
     53  * Virtual interface for userspace purposes.  Uses tap(4) to
     54  * interface with the kernel and just simply shovels data
     55  * to/from /dev/tap.
     56  */
     57 
     58 #define VIRTIF_BASE "virt"
     59 static int nunits;
     60 
     61 static int	virtif_init(struct ifnet *);
     62 static int	virtif_ioctl(struct ifnet *, u_long, void *);
     63 static void	virtif_start(struct ifnet *);
     64 static void	virtif_stop(struct ifnet *, int);
     65 
     66 struct virtif_sc {
     67 	struct ethercom sc_ec;
     68 	char sc_tapname[IFNAMSIZ];
     69 	int sc_tapfd;
     70 };
     71 
     72 static int virtif_create(struct ifaliasreq *, struct ifnet **);
     73 static void virtif_worker(void *);
     74 
     75 /* XXXXX: this prototype needs to go elsewhere */
     76 int rump_virtif_create(struct ifaliasreq *, struct ifnet **);
     77 int
     78 rump_virtif_create(struct ifaliasreq *ia, struct ifnet **ifpp)
     79 {
     80 
     81 	return virtif_create(ia, ifpp);
     82 }
     83 
     84 /*
     85  * Create a socket and call ifioctl() to configure the interface.
     86  * This trickles down to virtif_ioctl().
     87  */
     88 static int
     89 configaddr(struct ifnet *ifp, struct ifaliasreq *ia)
     90 {
     91 	struct socket *so;
     92 	int error;
     93 
     94 	strcpy(ia->ifra_name, ifp->if_xname);
     95 	error = socreate(ia->ifra_addr.sa_family, &so, SOCK_DGRAM,
     96 	    0, curlwp, NULL);
     97 	if (error)
     98 		return error;
     99 	error = ifioctl(so, SIOCAIFADDR, ia, curlwp);
    100 	soclose(so);
    101 
    102 	return error;
    103 }
    104 
    105 static int
    106 virtif_create(struct ifaliasreq *ia, struct ifnet **ifpp)
    107 {
    108 	struct virtif_sc *sc;
    109 	struct ifreq ifr;
    110 	struct ifnet *ifp;
    111 	uint8_t enaddr[ETHER_ADDR_LEN] = { 0xb2, 0x0a, 0x00, 0x0b, 0x0e, 0x01 };
    112 	int fd, rv, error;
    113 	int mynum;
    114 
    115 	/*
    116 	 * XXX: this is currently un-sane.  Need to figure out a way
    117 	 * to configure this with a bridge into a sane network without
    118 	 * hardcoding it.
    119 	 */
    120 	fd = rumpuser_open("/dev/tap0", O_RDWR, &error);
    121 	if (fd == -1) {
    122 		printf("virtif_create: can't open /dev/tap %d\n", error);
    123 		return error;
    124 	}
    125 
    126 	sc = kmem_zalloc(sizeof(*sc), KM_SLEEP);
    127 	rv = rumpuser_ioctl(fd, TAPGIFNAME, &ifr, &error);
    128 	if (rv == -1) {
    129 		kmem_free(sc, sizeof(*sc));
    130 		return error;
    131 	}
    132 	strlcpy(sc->sc_tapname, ifr.ifr_name, IFNAMSIZ);
    133 	sc->sc_tapfd = fd;
    134 
    135 	ifp = &sc->sc_ec.ec_if;
    136 	mynum = nunits++;
    137 	sprintf(ifp->if_xname, "%s%d", VIRTIF_BASE, mynum);
    138 	ifp->if_softc = sc;
    139 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
    140 	ifp->if_init = virtif_init;
    141 	ifp->if_ioctl = virtif_ioctl;
    142 	ifp->if_start = virtif_start;
    143 	ifp->if_stop = virtif_stop;
    144 
    145 	if (rump_threads) {
    146 		rv = kthread_create(PRI_NONE, 0, NULL, virtif_worker, ifp,
    147 		    NULL, "virtifi");
    148 		if (rv) {
    149 			kmem_free(sc, sizeof(*sc));
    150 			return rv;
    151 		}
    152 	} else {
    153 		printf("WARNING: threads not enabled, receive NOT working\n");
    154 	}
    155 
    156 	if_attach(ifp);
    157 	ether_ifattach(ifp, enaddr);
    158 
    159 	if (ia)
    160 		configaddr(ifp, ia);
    161 	if (ifpp)
    162 		*ifpp = ifp;
    163 
    164 	return 0;
    165 }
    166 
    167 static int
    168 virtif_init(struct ifnet *ifp)
    169 {
    170 
    171 	ifp->if_flags |= IFF_RUNNING;
    172 
    173 	return 0;
    174 }
    175 
    176 static int
    177 virtif_ioctl(struct ifnet *ifp, u_long cmd, void *data)
    178 {
    179 	int s, rv;
    180 
    181 	s = splnet();
    182 	rv = ether_ioctl(ifp, cmd, data);
    183 	splx(s);
    184 
    185 	return rv;
    186 }
    187 
    188 /* just send everything in-context */
    189 static void
    190 virtif_start(struct ifnet *ifp)
    191 {
    192 	struct virtif_sc *sc = ifp->if_softc;
    193 	struct rumpuser_iovec io[16];
    194 	struct mbuf *m, *m0;
    195 	int s, i, error;
    196 
    197 	s = splnet();
    198 	for (;;) {
    199 		IF_DEQUEUE(&ifp->if_snd, m0);
    200 		if (!m0)
    201 			break;
    202 		splx(s);
    203 
    204 		m = m0;
    205 		for (i = 0; i < 16 && m; i++) {
    206 			io[i].iov_base = mtod(m, void *);
    207 			io[i].iov_len = m->m_len;
    208 			m = m->m_next;
    209 		}
    210 		if (i == 16)
    211 			panic("lazy bum");
    212 		rumpuser_writev(sc->sc_tapfd, io, i, &error);
    213 		m_freem(m0);
    214 		s = splnet();
    215 	}
    216 
    217 	ifp->if_flags &= ~IFF_OACTIVE;
    218 	splx(s);
    219 }
    220 
    221 static void
    222 virtif_stop(struct ifnet *ifp, int disable)
    223 {
    224 
    225 	panic("%s: unimpl", __func__);
    226 }
    227 
    228 static void
    229 virtif_worker(void *arg)
    230 {
    231 	struct ifnet *ifp = arg;
    232 	struct virtif_sc *sc = ifp->if_softc;
    233 	struct mbuf *m;
    234 	size_t plen = ETHER_MAX_LEN_JUMBO+1;
    235 	ssize_t n;
    236 	int error;
    237 
    238 	for (;;) {
    239 		m = m_gethdr(M_WAIT, MT_DATA);
    240 		MEXTMALLOC(m, plen, M_WAIT);
    241 
    242 		n = rumpuser_read(sc->sc_tapfd, mtod(m, void *), plen, &error);
    243 		KASSERT(n < ETHER_MAX_LEN_JUMBO);
    244 		if (n <= 0) {
    245 			m_freem(m);
    246 			break;
    247 		}
    248 		m->m_len = m->m_pkthdr.len = n;
    249 		m->m_pkthdr.rcvif = ifp;
    250 		ether_input(ifp, m);
    251 	}
    252 
    253 	panic("virtif_workin is a lazy boy %d\n", error);
    254 }
    255