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