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