1 /* $NetBSD: if_virt.c,v 1.36 2013/07/04 11:46:51 pooka Exp $ */ 2 3 /* 4 * Copyright (c) 2008, 2013 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.36 2013/07/04 11:46:51 pooka Exp $"); 30 31 #include <sys/param.h> 32 #include <sys/condvar.h> 33 #include <sys/fcntl.h> 34 #include <sys/kernel.h> 35 #include <sys/kmem.h> 36 #include <sys/kthread.h> 37 #include <sys/mutex.h> 38 #include <sys/poll.h> 39 #include <sys/sockio.h> 40 #include <sys/socketvar.h> 41 #include <sys/cprng.h> 42 43 #include <net/bpf.h> 44 #include <net/if.h> 45 #include <net/if_ether.h> 46 #include <net/if_tap.h> 47 48 #include <netinet/in.h> 49 #include <netinet/in_var.h> 50 51 #include <rump/rump.h> 52 53 #include "rump_private.h" 54 #include "rump_net_private.h" 55 56 #include "if_virt.h" 57 #include "rumpcomp_user.h" 58 59 /* 60 * Virtual interface. Uses hypercalls to shovel packets back 61 * and forth. The exact method for shoveling depends on the 62 * hypercall implementation. 63 */ 64 65 static int virtif_init(struct ifnet *); 66 static int virtif_ioctl(struct ifnet *, u_long, void *); 67 static void virtif_start(struct ifnet *); 68 static void virtif_stop(struct ifnet *, int); 69 70 struct virtif_sc { 71 struct ethercom sc_ec; 72 struct virtif_user *sc_viu; 73 bool sc_dying; 74 struct lwp *sc_l_snd, *sc_l_rcv; 75 kmutex_t sc_mtx; 76 kcondvar_t sc_cv; 77 }; 78 79 static void virtif_receiver(void *); 80 static void virtif_sender(void *); 81 static int virtif_clone(struct if_clone *, int); 82 static int virtif_unclone(struct ifnet *); 83 84 struct if_clone VIF_CLONER = 85 IF_CLONE_INITIALIZER(VIF_NAME, virtif_clone, virtif_unclone); 86 87 static int 88 virtif_clone(struct if_clone *ifc, int num) 89 { 90 struct virtif_sc *sc; 91 struct virtif_user *viu; 92 struct ifnet *ifp; 93 uint8_t enaddr[ETHER_ADDR_LEN] = { 0xb2, 0x0a, 0x00, 0x0b, 0x0e, 0x01 }; 94 int error = 0; 95 96 if (num >= 0x100) 97 return E2BIG; 98 99 if ((error = VIFHYPER_CREATE(num, &viu)) != 0) 100 return error; 101 102 enaddr[2] = cprng_fast32() & 0xff; 103 enaddr[5] = num; 104 105 sc = kmem_zalloc(sizeof(*sc), KM_SLEEP); 106 sc->sc_dying = false; 107 sc->sc_viu = viu; 108 109 mutex_init(&sc->sc_mtx, MUTEX_DEFAULT, IPL_NONE); 110 cv_init(&sc->sc_cv, VIF_NAME "snd"); 111 ifp = &sc->sc_ec.ec_if; 112 sprintf(ifp->if_xname, "%s%d", VIF_NAME, num); 113 ifp->if_softc = sc; 114 115 if (rump_threads) { 116 if ((error = kthread_create(PRI_NONE, KTHREAD_MUSTJOIN, NULL, 117 virtif_receiver, ifp, &sc->sc_l_rcv, VIF_NAME "ifr")) != 0) 118 goto out; 119 120 if ((error = kthread_create(PRI_NONE, 121 KTHREAD_MUSTJOIN | KTHREAD_MPSAFE, NULL, 122 virtif_sender, ifp, &sc->sc_l_snd, VIF_NAME "ifs")) != 0) 123 goto out; 124 } else { 125 printf("WARNING: threads not enabled, receive NOT working\n"); 126 } 127 128 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST; 129 ifp->if_init = virtif_init; 130 ifp->if_ioctl = virtif_ioctl; 131 ifp->if_start = virtif_start; 132 ifp->if_stop = virtif_stop; 133 IFQ_SET_READY(&ifp->if_snd); 134 135 if_attach(ifp); 136 ether_ifattach(ifp, enaddr); 137 138 out: 139 if (error) { 140 virtif_unclone(ifp); 141 } 142 143 return error; 144 } 145 146 static int 147 virtif_unclone(struct ifnet *ifp) 148 { 149 struct virtif_sc *sc = ifp->if_softc; 150 151 mutex_enter(&sc->sc_mtx); 152 if (sc->sc_dying) { 153 mutex_exit(&sc->sc_mtx); 154 return EINPROGRESS; 155 } 156 sc->sc_dying = true; 157 cv_broadcast(&sc->sc_cv); 158 mutex_exit(&sc->sc_mtx); 159 160 VIFHYPER_DYING(sc->sc_viu); 161 162 virtif_stop(ifp, 1); 163 if_down(ifp); 164 165 if (sc->sc_l_snd) { 166 kthread_join(sc->sc_l_snd); 167 sc->sc_l_snd = NULL; 168 } 169 if (sc->sc_l_rcv) { 170 kthread_join(sc->sc_l_rcv); 171 sc->sc_l_rcv = NULL; 172 } 173 174 VIFHYPER_DESTROY(sc->sc_viu); 175 176 mutex_destroy(&sc->sc_mtx); 177 cv_destroy(&sc->sc_cv); 178 kmem_free(sc, sizeof(*sc)); 179 180 ether_ifdetach(ifp); 181 if_detach(ifp); 182 183 return 0; 184 } 185 186 static int 187 virtif_init(struct ifnet *ifp) 188 { 189 struct virtif_sc *sc = ifp->if_softc; 190 191 ifp->if_flags |= IFF_RUNNING; 192 193 mutex_enter(&sc->sc_mtx); 194 cv_broadcast(&sc->sc_cv); 195 mutex_exit(&sc->sc_mtx); 196 197 return 0; 198 } 199 200 static int 201 virtif_ioctl(struct ifnet *ifp, u_long cmd, void *data) 202 { 203 int s, rv; 204 205 s = splnet(); 206 rv = ether_ioctl(ifp, cmd, data); 207 if (rv == ENETRESET) 208 rv = 0; 209 splx(s); 210 211 return rv; 212 } 213 214 static void 215 virtif_start(struct ifnet *ifp) 216 { 217 struct virtif_sc *sc = ifp->if_softc; 218 219 mutex_enter(&sc->sc_mtx); 220 ifp->if_flags |= IFF_OACTIVE; 221 cv_broadcast(&sc->sc_cv); 222 mutex_exit(&sc->sc_mtx); 223 } 224 225 static void 226 virtif_stop(struct ifnet *ifp, int disable) 227 { 228 struct virtif_sc *sc = ifp->if_softc; 229 230 ifp->if_flags &= ~IFF_RUNNING; 231 232 mutex_enter(&sc->sc_mtx); 233 cv_broadcast(&sc->sc_cv); 234 mutex_exit(&sc->sc_mtx); 235 } 236 237 #define POLLTIMO_MS 1 238 static void 239 virtif_receiver(void *arg) 240 { 241 struct ifnet *ifp = arg; 242 struct virtif_sc *sc = ifp->if_softc; 243 struct mbuf *m; 244 size_t plen = ETHER_MAX_LEN_JUMBO+1; 245 size_t n; 246 int error; 247 248 for (;;) { 249 m = m_gethdr(M_WAIT, MT_DATA); 250 MEXTMALLOC(m, plen, M_WAIT); 251 252 again: 253 if (sc->sc_dying) { 254 m_freem(m); 255 break; 256 } 257 258 error = VIFHYPER_RECV(sc->sc_viu, 259 mtod(m, void *), plen, &n); 260 if (error) { 261 printf("%s: read hypercall failed %d. host if down?\n", 262 ifp->if_xname, error); 263 mutex_enter(&sc->sc_mtx); 264 /* could check if need go, done soon anyway */ 265 cv_timedwait(&sc->sc_cv, &sc->sc_mtx, hz); 266 mutex_exit(&sc->sc_mtx); 267 goto again; 268 } 269 270 /* tap sometimes returns EOF. don't sweat it and plow on */ 271 if (__predict_false(n == 0)) 272 goto again; 273 274 /* discard if we're not up */ 275 if ((ifp->if_flags & IFF_RUNNING) == 0) 276 goto again; 277 278 m->m_len = m->m_pkthdr.len = n; 279 m->m_pkthdr.rcvif = ifp; 280 bpf_mtap(ifp, m); 281 ether_input(ifp, m); 282 } 283 284 kthread_exit(0); 285 } 286 287 /* lazy bum stetson-harrison magic value */ 288 #define LB_SH 32 289 static void 290 virtif_sender(void *arg) 291 { 292 struct ifnet *ifp = arg; 293 struct virtif_sc *sc = ifp->if_softc; 294 struct mbuf *m, *m0; 295 struct iovec io[LB_SH]; 296 int i; 297 298 mutex_enter(&sc->sc_mtx); 299 KERNEL_LOCK(1, NULL); 300 while (!sc->sc_dying) { 301 if (!(ifp->if_flags & IFF_RUNNING)) { 302 cv_wait(&sc->sc_cv, &sc->sc_mtx); 303 continue; 304 } 305 IF_DEQUEUE(&ifp->if_snd, m0); 306 if (!m0) { 307 ifp->if_flags &= ~IFF_OACTIVE; 308 cv_wait(&sc->sc_cv, &sc->sc_mtx); 309 continue; 310 } 311 mutex_exit(&sc->sc_mtx); 312 313 m = m0; 314 for (i = 0; i < LB_SH && m; i++) { 315 io[i].iov_base = mtod(m, void *); 316 io[i].iov_len = m->m_len; 317 m = m->m_next; 318 } 319 if (i == LB_SH) 320 panic("lazy bum"); 321 bpf_mtap(ifp, m0); 322 323 VIFHYPER_SEND(sc->sc_viu, io, i); 324 325 m_freem(m0); 326 mutex_enter(&sc->sc_mtx); 327 } 328 KERNEL_UNLOCK_LAST(curlwp); 329 330 mutex_exit(&sc->sc_mtx); 331 332 kthread_exit(0); 333 } 334