1 /* $NetBSD: if_virt.c,v 1.18 2010/04/05 16:35:30 joerg 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.18 2010/04/05 16:35:30 joerg 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/poll.h> 38 #include <sys/sockio.h> 39 #include <sys/socketvar.h> 40 41 #include <net/bpf.h> 42 #include <net/if.h> 43 #include <net/if_ether.h> 44 #include <net/if_tap.h> 45 46 #include <netinet/in.h> 47 #include <netinet/in_var.h> 48 49 #include <rump/rump.h> 50 #include <rump/rumpuser.h> 51 52 #include "rump_private.h" 53 #include "rump_net_private.h" 54 55 /* 56 * Virtual interface for userspace purposes. Uses tap(4) to 57 * interface with the kernel and just simply shovels data 58 * to/from /dev/tap. 59 */ 60 61 #define VIRTIF_BASE "virt" 62 63 static int virtif_init(struct ifnet *); 64 static int virtif_ioctl(struct ifnet *, u_long, void *); 65 static void virtif_start(struct ifnet *); 66 static void virtif_stop(struct ifnet *, int); 67 68 struct virtif_sc { 69 struct ethercom sc_ec; 70 int sc_tapfd; 71 kmutex_t sc_sendmtx; 72 kcondvar_t sc_sendcv; 73 }; 74 75 static void virtif_worker(void *); 76 static void virtif_sender(void *); 77 78 #if 0 79 /* 80 * Create a socket and call ifioctl() to configure the interface. 81 * This trickles down to virtif_ioctl(). 82 */ 83 static int 84 configaddr(struct ifnet *ifp, struct ifaliasreq *ia) 85 { 86 struct socket *so; 87 int error; 88 89 strcpy(ia->ifra_name, ifp->if_xname); 90 error = socreate(ia->ifra_addr.sa_family, &so, SOCK_DGRAM, 91 0, curlwp, NULL); 92 if (error) 93 return error; 94 error = ifioctl(so, SIOCAIFADDR, ia, curlwp); 95 soclose(so); 96 97 return error; 98 } 99 #endif 100 101 int 102 rump_virtif_create(int num) 103 { 104 struct virtif_sc *sc; 105 struct ifnet *ifp; 106 uint8_t enaddr[ETHER_ADDR_LEN] = { 0xb2, 0x0a, 0x00, 0x0b, 0x0e, 0x01 }; 107 char tapdev[16]; 108 int fd, error; 109 110 snprintf(tapdev, sizeof(tapdev), "/dev/tap%d", num); 111 fd = rumpuser_open(tapdev, O_RDWR, &error); 112 if (fd == -1) { 113 printf("virtif_create: can't open /dev/tap %d\n", error); 114 return error; 115 } 116 KASSERT(num < 0x100); 117 enaddr[2] = arc4random() & 0xff; 118 enaddr[5] = num; 119 120 sc = kmem_zalloc(sizeof(*sc), KM_SLEEP); 121 sc->sc_tapfd = fd; 122 123 ifp = &sc->sc_ec.ec_if; 124 sprintf(ifp->if_xname, "%s%d", VIRTIF_BASE, num); 125 ifp->if_softc = sc; 126 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST; 127 ifp->if_init = virtif_init; 128 ifp->if_ioctl = virtif_ioctl; 129 ifp->if_start = virtif_start; 130 ifp->if_stop = virtif_stop; 131 132 mutex_init(&sc->sc_sendmtx, MUTEX_DEFAULT, IPL_NONE); 133 cv_init(&sc->sc_sendcv, "virtsnd"); 134 135 if_attach(ifp); 136 ether_ifattach(ifp, enaddr); 137 138 return 0; 139 } 140 141 static int 142 virtif_init(struct ifnet *ifp) 143 { 144 int rv; 145 146 if (rump_threads) { 147 rv = kthread_create(PRI_NONE, 0, NULL, virtif_worker, ifp, 148 NULL, "virtifi"); 149 /* XXX: should do proper cleanup */ 150 if (rv) { 151 panic("if_virt: can't create worker"); 152 } 153 rv = kthread_create(PRI_NONE, 0, NULL, virtif_sender, ifp, 154 NULL, "virtifs"); 155 if (rv) { 156 panic("if_virt: can't create sender"); 157 } 158 } else { 159 printf("WARNING: threads not enabled, receive NOT working\n"); 160 } 161 ifp->if_flags |= IFF_RUNNING; 162 163 return 0; 164 } 165 166 static int 167 virtif_ioctl(struct ifnet *ifp, u_long cmd, void *data) 168 { 169 int s, rv; 170 171 s = splnet(); 172 rv = ether_ioctl(ifp, cmd, data); 173 if (rv == ENETRESET) 174 rv = 0; 175 splx(s); 176 177 return rv; 178 } 179 180 /* just send everything in-context */ 181 static void 182 virtif_start(struct ifnet *ifp) 183 { 184 struct virtif_sc *sc = ifp->if_softc; 185 186 mutex_enter(&sc->sc_sendmtx); 187 cv_signal(&sc->sc_sendcv); 188 mutex_exit(&sc->sc_sendmtx); 189 } 190 191 static void 192 virtif_stop(struct ifnet *ifp, int disable) 193 { 194 195 panic("%s: unimpl", __func__); 196 } 197 198 static void 199 virtif_worker(void *arg) 200 { 201 struct ifnet *ifp = arg; 202 struct virtif_sc *sc = ifp->if_softc; 203 struct mbuf *m; 204 size_t plen = ETHER_MAX_LEN_JUMBO+1; 205 ssize_t n; 206 int error; 207 208 for (;;) { 209 m = m_gethdr(M_WAIT, MT_DATA); 210 MEXTMALLOC(m, plen, M_WAIT); 211 212 again: 213 n = rumpuser_read(sc->sc_tapfd, mtod(m, void *), plen, &error); 214 KASSERT(n < ETHER_MAX_LEN_JUMBO); 215 if (n <= 0) { 216 /* 217 * work around tap bug: /dev/tap is opened in 218 * non-blocking mode if it previously was 219 * non-blocking. 220 */ 221 if (n == -1 && error == EAGAIN) { 222 struct pollfd pfd; 223 224 pfd.fd = sc->sc_tapfd; 225 pfd.events = POLLIN; 226 227 rumpuser_poll(&pfd, 1, INFTIM, &error); 228 goto again; 229 } 230 m_freem(m); 231 break; 232 } 233 m->m_len = m->m_pkthdr.len = n; 234 m->m_pkthdr.rcvif = ifp; 235 bpf_mtap(ifp, m); 236 ether_input(ifp, m); 237 } 238 239 panic("virtif_workin is a lazy boy %d\n", error); 240 } 241 242 /* lazy bum stetson-harrison magic value */ 243 #define LB_SH 32 244 static void 245 virtif_sender(void *arg) 246 { 247 struct ifnet *ifp = arg; 248 struct virtif_sc *sc = ifp->if_softc; 249 struct mbuf *m, *m0; 250 struct rumpuser_iovec io[LB_SH]; 251 int i, error; 252 253 mutex_enter(&sc->sc_sendmtx); 254 for (;;) { 255 IF_DEQUEUE(&ifp->if_snd, m0); 256 if (!m0) { 257 cv_wait(&sc->sc_sendcv, &sc->sc_sendmtx); 258 continue; 259 } 260 mutex_exit(&sc->sc_sendmtx); 261 262 m = m0; 263 for (i = 0; i < LB_SH && m; i++) { 264 io[i].iov_base = mtod(m, void *); 265 io[i].iov_len = m->m_len; 266 m = m->m_next; 267 } 268 if (i == LB_SH) 269 panic("lazy bum"); 270 bpf_mtap(ifp, m0); 271 rumpuser_writev(sc->sc_tapfd, io, i, &error); 272 m_freem(m0); 273 mutex_enter(&sc->sc_sendmtx); 274 } 275 276 mutex_exit(softnet_lock); 277 } 278 279 /* 280 * dummyif is a nada-interface. 281 * As it requires nothing external, it can be used for testing 282 * interface configuration. 283 */ 284 static int dummyif_init(struct ifnet *); 285 static void dummyif_start(struct ifnet *); 286 287 void 288 rump_dummyif_create() 289 { 290 struct ifnet *ifp; 291 struct ethercom *ec; 292 uint8_t enaddr[ETHER_ADDR_LEN] = { 0xb2, 0x0a, 0x00, 0x0b, 0x0e, 0x01 }; 293 294 enaddr[2] = arc4random() & 0xff; 295 enaddr[5] = arc4random() & 0xff; 296 297 ec = kmem_zalloc(sizeof(*ec), KM_SLEEP); 298 299 ifp = &ec->ec_if; 300 strlcpy(ifp->if_xname, "dummy0", sizeof(ifp->if_xname)); 301 ifp->if_softc = ifp; 302 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST; 303 ifp->if_init = dummyif_init; 304 ifp->if_ioctl = virtif_ioctl; 305 ifp->if_start = dummyif_start; 306 307 if_attach(ifp); 308 ether_ifattach(ifp, enaddr); 309 } 310 311 static int 312 dummyif_init(struct ifnet *ifp) 313 { 314 315 ifp->if_flags |= IFF_RUNNING; 316 return 0; 317 } 318 319 static void 320 dummyif_start(struct ifnet *ifp) 321 { 322 323 } 324