Home | History | Annotate | Line # | Download | only in libvirtif
if_virt.c revision 1.8
      1  1.8  pooka /*	$NetBSD: if_virt.c,v 1.8 2009/03/27 13:46:35 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.8  pooka __KERNEL_RCSID(0, "$NetBSD: if_virt.c,v 1.8 2009/03/27 13:46:35 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 
     60  1.1  pooka static int	virtif_init(struct ifnet *);
     61  1.1  pooka static int	virtif_ioctl(struct ifnet *, u_long, void *);
     62  1.1  pooka static void	virtif_start(struct ifnet *);
     63  1.1  pooka static void	virtif_stop(struct ifnet *, int);
     64  1.1  pooka 
     65  1.1  pooka struct virtif_sc {
     66  1.7  pooka 	struct ethercom sc_ec;
     67  1.1  pooka 	int sc_tapfd;
     68  1.8  pooka 	kmutex_t sc_sendmtx;
     69  1.8  pooka 	kcondvar_t sc_sendcv;
     70  1.1  pooka };
     71  1.1  pooka 
     72  1.1  pooka static void virtif_worker(void *);
     73  1.8  pooka static void virtif_sender(void *);
     74  1.1  pooka 
     75  1.8  pooka #if 0
     76  1.1  pooka /*
     77  1.1  pooka  * Create a socket and call ifioctl() to configure the interface.
     78  1.1  pooka  * This trickles down to virtif_ioctl().
     79  1.1  pooka  */
     80  1.1  pooka static int
     81  1.1  pooka configaddr(struct ifnet *ifp, struct ifaliasreq *ia)
     82  1.1  pooka {
     83  1.1  pooka 	struct socket *so;
     84  1.1  pooka 	int error;
     85  1.1  pooka 
     86  1.1  pooka 	strcpy(ia->ifra_name, ifp->if_xname);
     87  1.1  pooka 	error = socreate(ia->ifra_addr.sa_family, &so, SOCK_DGRAM,
     88  1.1  pooka 	    0, curlwp, NULL);
     89  1.1  pooka 	if (error)
     90  1.1  pooka 		return error;
     91  1.1  pooka 	error = ifioctl(so, SIOCAIFADDR, ia, curlwp);
     92  1.1  pooka 	soclose(so);
     93  1.1  pooka 
     94  1.1  pooka 	return error;
     95  1.1  pooka }
     96  1.8  pooka #endif
     97  1.1  pooka 
     98  1.8  pooka int
     99  1.8  pooka rump_virtif_create(int num)
    100  1.1  pooka {
    101  1.1  pooka 	struct virtif_sc *sc;
    102  1.1  pooka 	struct ifnet *ifp;
    103  1.3  pooka 	uint8_t enaddr[ETHER_ADDR_LEN] = { 0xb2, 0x0a, 0x00, 0x0b, 0x0e, 0x01 };
    104  1.8  pooka 	char tapdev[16];
    105  1.8  pooka 	int fd, error;
    106  1.1  pooka 
    107  1.8  pooka 	snprintf(tapdev, sizeof(tapdev), "/dev/tap%d", num);
    108  1.8  pooka 	fd = rumpuser_open(tapdev, O_RDWR, &error);
    109  1.1  pooka 	if (fd == -1) {
    110  1.1  pooka 		printf("virtif_create: can't open /dev/tap %d\n", error);
    111  1.1  pooka 		return error;
    112  1.1  pooka 	}
    113  1.8  pooka 	KASSERT(num < 0x100);
    114  1.8  pooka 	enaddr[2] = arc4random() & 0xff;
    115  1.8  pooka 	enaddr[5] = num;
    116  1.1  pooka 
    117  1.1  pooka 	sc = kmem_zalloc(sizeof(*sc), KM_SLEEP);
    118  1.1  pooka 	sc->sc_tapfd = fd;
    119  1.1  pooka 
    120  1.7  pooka 	ifp = &sc->sc_ec.ec_if;
    121  1.8  pooka 	sprintf(ifp->if_xname, "%s%d", VIRTIF_BASE, num);
    122  1.1  pooka 	ifp->if_softc = sc;
    123  1.1  pooka 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
    124  1.1  pooka 	ifp->if_init = virtif_init;
    125  1.1  pooka 	ifp->if_ioctl = virtif_ioctl;
    126  1.1  pooka 	ifp->if_start = virtif_start;
    127  1.1  pooka 	ifp->if_stop = virtif_stop;
    128  1.1  pooka 
    129  1.8  pooka 	mutex_init(&sc->sc_sendmtx, MUTEX_DEFAULT, IPL_NONE);
    130  1.8  pooka 	cv_init(&sc->sc_sendcv, "virtsnd");
    131  1.1  pooka 
    132  1.1  pooka 	if_attach(ifp);
    133  1.1  pooka 	ether_ifattach(ifp, enaddr);
    134  1.1  pooka 
    135  1.1  pooka 	return 0;
    136  1.1  pooka }
    137  1.1  pooka 
    138  1.1  pooka static int
    139  1.1  pooka virtif_init(struct ifnet *ifp)
    140  1.1  pooka {
    141  1.8  pooka 	int rv;
    142  1.1  pooka 
    143  1.8  pooka 	if (rump_threads) {
    144  1.8  pooka 		rv = kthread_create(PRI_NONE, 0, NULL, virtif_worker, ifp,
    145  1.8  pooka 		    NULL, "virtifi");
    146  1.8  pooka 		/* XXX: should do proper cleanup */
    147  1.8  pooka 		if (rv) {
    148  1.8  pooka 			panic("if_virt: can't create worker");
    149  1.8  pooka 		}
    150  1.8  pooka 		rv = kthread_create(PRI_NONE, 0, NULL, virtif_sender, ifp,
    151  1.8  pooka 		    NULL, "virtifs");
    152  1.8  pooka 		if (rv) {
    153  1.8  pooka 			panic("if_virt: can't create sender");
    154  1.8  pooka 		}
    155  1.8  pooka 	} else {
    156  1.8  pooka 		printf("WARNING: threads not enabled, receive NOT working\n");
    157  1.8  pooka 	}
    158  1.1  pooka 	ifp->if_flags |= IFF_RUNNING;
    159  1.8  pooka 
    160  1.1  pooka 	return 0;
    161  1.1  pooka }
    162  1.1  pooka 
    163  1.1  pooka static int
    164  1.1  pooka virtif_ioctl(struct ifnet *ifp, u_long cmd, void *data)
    165  1.1  pooka {
    166  1.1  pooka 	int s, rv;
    167  1.1  pooka 
    168  1.1  pooka 	s = splnet();
    169  1.1  pooka 	rv = ether_ioctl(ifp, cmd, data);
    170  1.1  pooka 	splx(s);
    171  1.1  pooka 
    172  1.1  pooka 	return rv;
    173  1.1  pooka }
    174  1.1  pooka 
    175  1.1  pooka /* just send everything in-context */
    176  1.1  pooka static void
    177  1.1  pooka virtif_start(struct ifnet *ifp)
    178  1.1  pooka {
    179  1.1  pooka 	struct virtif_sc *sc = ifp->if_softc;
    180  1.1  pooka 
    181  1.8  pooka 	mutex_enter(&sc->sc_sendmtx);
    182  1.8  pooka 	cv_signal(&sc->sc_sendcv);
    183  1.8  pooka 	mutex_exit(&sc->sc_sendmtx);
    184  1.1  pooka }
    185  1.1  pooka 
    186  1.1  pooka static void
    187  1.1  pooka virtif_stop(struct ifnet *ifp, int disable)
    188  1.1  pooka {
    189  1.1  pooka 
    190  1.1  pooka 	panic("%s: unimpl", __func__);
    191  1.1  pooka }
    192  1.1  pooka 
    193  1.1  pooka static void
    194  1.1  pooka virtif_worker(void *arg)
    195  1.1  pooka {
    196  1.1  pooka 	struct ifnet *ifp = arg;
    197  1.1  pooka 	struct virtif_sc *sc = ifp->if_softc;
    198  1.1  pooka 	struct mbuf *m;
    199  1.1  pooka 	size_t plen = ETHER_MAX_LEN_JUMBO+1;
    200  1.1  pooka 	ssize_t n;
    201  1.1  pooka 	int error;
    202  1.1  pooka 
    203  1.1  pooka 	for (;;) {
    204  1.1  pooka 		m = m_gethdr(M_WAIT, MT_DATA);
    205  1.1  pooka 		MEXTMALLOC(m, plen, M_WAIT);
    206  1.1  pooka 
    207  1.1  pooka 		n = rumpuser_read(sc->sc_tapfd, mtod(m, void *), plen, &error);
    208  1.1  pooka 		KASSERT(n < ETHER_MAX_LEN_JUMBO);
    209  1.1  pooka 		if (n <= 0) {
    210  1.1  pooka 			m_freem(m);
    211  1.1  pooka 			break;
    212  1.1  pooka 		}
    213  1.1  pooka 		m->m_len = m->m_pkthdr.len = n;
    214  1.1  pooka 		m->m_pkthdr.rcvif = ifp;
    215  1.1  pooka 		ether_input(ifp, m);
    216  1.1  pooka 	}
    217  1.1  pooka 
    218  1.1  pooka 	panic("virtif_workin is a lazy boy %d\n", error);
    219  1.1  pooka }
    220  1.8  pooka 
    221  1.8  pooka static void
    222  1.8  pooka virtif_sender(void *arg)
    223  1.8  pooka {
    224  1.8  pooka 	struct ifnet *ifp = arg;
    225  1.8  pooka 	struct virtif_sc *sc = ifp->if_softc;
    226  1.8  pooka 	struct mbuf *m, *m0;
    227  1.8  pooka 	struct rumpuser_iovec io[16];
    228  1.8  pooka 	int i, error;
    229  1.8  pooka 
    230  1.8  pooka 	mutex_enter(&sc->sc_sendmtx);
    231  1.8  pooka 	for (;;) {
    232  1.8  pooka 		IF_DEQUEUE(&ifp->if_snd, m0);
    233  1.8  pooka 		if (!m0) {
    234  1.8  pooka 			cv_wait(&sc->sc_sendcv, &sc->sc_sendmtx);
    235  1.8  pooka 			continue;
    236  1.8  pooka 		}
    237  1.8  pooka 		mutex_exit(&sc->sc_sendmtx);
    238  1.8  pooka 
    239  1.8  pooka 		m = m0;
    240  1.8  pooka 		for (i = 0; i < 16 && m; i++) {
    241  1.8  pooka 			io[i].iov_base = mtod(m, void *);
    242  1.8  pooka 			io[i].iov_len = m->m_len;
    243  1.8  pooka 			m = m->m_next;
    244  1.8  pooka 		}
    245  1.8  pooka 		if (i == 16)
    246  1.8  pooka 			panic("lazy bum");
    247  1.8  pooka 		rumpuser_writev(sc->sc_tapfd, io, i, &error);
    248  1.8  pooka 		m_freem(m0);
    249  1.8  pooka 		mutex_enter(&sc->sc_sendmtx);
    250  1.8  pooka 	}
    251  1.8  pooka 
    252  1.8  pooka 	mutex_exit(softnet_lock);
    253  1.8  pooka }
    254