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