1 1.89 rin /* $NetBSD: if_shmem.c,v 1.89 2024/10/01 08:55:58 rin Exp $ */ 2 1.1 pooka 3 1.1 pooka /* 4 1.39 pooka * Copyright (c) 2009, 2010 Antti Kantee. All Rights Reserved. 5 1.1 pooka * 6 1.1 pooka * Development of this software was supported by The Nokia Foundation. 7 1.1 pooka * 8 1.1 pooka * Redistribution and use in source and binary forms, with or without 9 1.1 pooka * modification, are permitted provided that the following conditions 10 1.1 pooka * are met: 11 1.1 pooka * 1. Redistributions of source code must retain the above copyright 12 1.1 pooka * notice, this list of conditions and the following disclaimer. 13 1.1 pooka * 2. Redistributions in binary form must reproduce the above copyright 14 1.1 pooka * notice, this list of conditions and the following disclaimer in the 15 1.1 pooka * documentation and/or other materials provided with the distribution. 16 1.1 pooka * 17 1.1 pooka * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS 18 1.1 pooka * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 19 1.1 pooka * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 20 1.1 pooka * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21 1.1 pooka * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 1.1 pooka * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 23 1.1 pooka * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 1.1 pooka * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 1.1 pooka * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 1.1 pooka * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 1.1 pooka * SUCH DAMAGE. 28 1.1 pooka */ 29 1.1 pooka 30 1.1 pooka #include <sys/cdefs.h> 31 1.89 rin __KERNEL_RCSID(0, "$NetBSD: if_shmem.c,v 1.89 2024/10/01 08:55:58 rin Exp $"); 32 1.1 pooka 33 1.1 pooka #include <sys/param.h> 34 1.13 pooka #include <sys/atomic.h> 35 1.1 pooka #include <sys/fcntl.h> 36 1.1 pooka #include <sys/kmem.h> 37 1.1 pooka #include <sys/kthread.h> 38 1.1 pooka #include <sys/lock.h> 39 1.31 pooka #include <sys/vmem.h> 40 1.44 tls #include <sys/cprng.h> 41 1.1 pooka 42 1.35 pooka #include <net/bpf.h> 43 1.1 pooka #include <net/if.h> 44 1.37 pooka #include <net/if_dl.h> 45 1.1 pooka #include <net/if_ether.h> 46 1.86 ozaki #include <net/if_media.h> 47 1.76 rin #include <net/ether_sw_offload.h> 48 1.1 pooka 49 1.1 pooka #include <netinet/in.h> 50 1.1 pooka #include <netinet/in_var.h> 51 1.1 pooka 52 1.64 pooka #include <rump-sys/kern.h> 53 1.64 pooka #include <rump-sys/net.h> 54 1.64 pooka 55 1.1 pooka #include <rump/rump.h> 56 1.1 pooka #include <rump/rumpuser.h> 57 1.1 pooka 58 1.59 pooka #include "shmif_user.h" 59 1.1 pooka 60 1.29 pooka static int shmif_clone(struct if_clone *, int); 61 1.29 pooka static int shmif_unclone(struct ifnet *); 62 1.29 pooka 63 1.86 ozaki static int shmif_mediachange(struct ifnet *); 64 1.86 ozaki static void shmif_mediastatus(struct ifnet *, struct ifmediareq *); 65 1.86 ozaki 66 1.29 pooka struct if_clone shmif_cloner = 67 1.29 pooka IF_CLONE_INITIALIZER("shmif", shmif_clone, shmif_unclone); 68 1.29 pooka 69 1.1 pooka /* 70 1.28 pooka * Do r/w prefault for backend pages when attaching the interface. 71 1.29 pooka * At least logically thinking improves performance (although no 72 1.29 pooka * mlocking is done, so they might go away). 73 1.28 pooka */ 74 1.28 pooka #define PREFAULT_RW 75 1.28 pooka 76 1.28 pooka /* 77 1.1 pooka * A virtual ethernet interface which uses shared memory from a 78 1.1 pooka * memory mapped file as the bus. 79 1.1 pooka */ 80 1.1 pooka 81 1.1 pooka static int shmif_init(struct ifnet *); 82 1.1 pooka static int shmif_ioctl(struct ifnet *, u_long, void *); 83 1.1 pooka static void shmif_start(struct ifnet *); 84 1.76 rin static void shmif_snd(struct ifnet *, struct mbuf *); 85 1.1 pooka static void shmif_stop(struct ifnet *, int); 86 1.1 pooka 87 1.16 pooka #include "shmifvar.h" 88 1.16 pooka 89 1.1 pooka struct shmif_sc { 90 1.1 pooka struct ethercom sc_ec; 91 1.86 ozaki struct ifmedia sc_im; 92 1.16 pooka struct shmif_mem *sc_busmem; 93 1.1 pooka int sc_memfd; 94 1.1 pooka int sc_kq; 95 1.32 pooka int sc_unit; 96 1.1 pooka 97 1.29 pooka char *sc_backfile; 98 1.29 pooka size_t sc_backfilelen; 99 1.29 pooka 100 1.26 pooka uint64_t sc_devgen; 101 1.1 pooka uint32_t sc_nextpacket; 102 1.32 pooka 103 1.32 pooka kmutex_t sc_mtx; 104 1.32 pooka kcondvar_t sc_cv; 105 1.32 pooka 106 1.32 pooka struct lwp *sc_rcvl; 107 1.32 pooka bool sc_dying; 108 1.63 ozaki 109 1.80 ozaki uint64_t sc_uid; 110 1.1 pooka }; 111 1.1 pooka 112 1.1 pooka static void shmif_rcv(void *); 113 1.1 pooka 114 1.31 pooka vmem_t *shmif_units; 115 1.31 pooka 116 1.52 pooka static void 117 1.52 pooka dowakeup(struct shmif_sc *sc) 118 1.52 pooka { 119 1.52 pooka struct rumpuser_iovec iov; 120 1.52 pooka uint32_t ver = SHMIF_VERSION; 121 1.53 pooka size_t n; 122 1.52 pooka 123 1.52 pooka iov.iov_base = &ver; 124 1.52 pooka iov.iov_len = sizeof(ver); 125 1.53 pooka rumpuser_iovwrite(sc->sc_memfd, &iov, 1, IFMEM_WAKEUP, &n); 126 1.52 pooka } 127 1.52 pooka 128 1.23 pooka /* 129 1.23 pooka * This locking needs work and will misbehave severely if: 130 1.23 pooka * 1) the backing memory has to be paged in 131 1.23 pooka * 2) some lockholder exits while holding the lock 132 1.23 pooka */ 133 1.23 pooka static void 134 1.23 pooka shmif_lockbus(struct shmif_mem *busmem) 135 1.23 pooka { 136 1.23 pooka int i = 0; 137 1.23 pooka 138 1.23 pooka while (__predict_false(atomic_cas_32(&busmem->shm_lock, 139 1.23 pooka LOCK_UNLOCKED, LOCK_LOCKED) == LOCK_LOCKED)) { 140 1.23 pooka if (__predict_false(++i > LOCK_COOLDOWN)) { 141 1.50 pooka /* wait 1ms */ 142 1.54 pooka rumpuser_clock_sleep(RUMPUSER_CLOCK_RELWALL, 143 1.54 pooka 0, 1000*1000); 144 1.23 pooka i = 0; 145 1.23 pooka } 146 1.23 pooka continue; 147 1.23 pooka } 148 1.84 riastrad membar_acquire(); 149 1.23 pooka } 150 1.23 pooka 151 1.23 pooka static void 152 1.23 pooka shmif_unlockbus(struct shmif_mem *busmem) 153 1.23 pooka { 154 1.61 justin unsigned int old __diagused; 155 1.23 pooka 156 1.84 riastrad membar_release(); 157 1.23 pooka old = atomic_swap_32(&busmem->shm_lock, LOCK_UNLOCKED); 158 1.23 pooka KASSERT(old == LOCK_LOCKED); 159 1.23 pooka } 160 1.23 pooka 161 1.29 pooka static int 162 1.29 pooka allocif(int unit, struct shmif_sc **scp) 163 1.1 pooka { 164 1.29 pooka uint8_t enaddr[ETHER_ADDR_LEN] = { 0xb2, 0xa0, 0x00, 0x00, 0x00, 0x00 }; 165 1.1 pooka struct shmif_sc *sc; 166 1.1 pooka struct ifnet *ifp; 167 1.81 ozaki uint64_t randnum; 168 1.82 riastrad int error = 0; 169 1.1 pooka 170 1.81 ozaki randnum = cprng_strong64(); 171 1.81 ozaki memcpy(&enaddr[2], &randnum, 4); 172 1.1 pooka 173 1.1 pooka sc = kmem_zalloc(sizeof(*sc), KM_SLEEP); 174 1.29 pooka sc->sc_memfd = -1; 175 1.32 pooka sc->sc_unit = unit; 176 1.81 ozaki sc->sc_uid = randnum; 177 1.29 pooka 178 1.1 pooka ifp = &sc->sc_ec.ec_if; 179 1.1 pooka 180 1.86 ozaki ifmedia_init(&sc->sc_im, 0, shmif_mediachange, shmif_mediastatus); 181 1.86 ozaki ifmedia_add(&sc->sc_im, IFM_ETHER|IFM_AUTO, 0, NULL); 182 1.86 ozaki ifmedia_add(&sc->sc_im, IFM_ETHER|IFM_NONE, 0, NULL); 183 1.86 ozaki ifmedia_set(&sc->sc_im, IFM_ETHER|IFM_AUTO); 184 1.86 ozaki 185 1.60 christos snprintf(ifp->if_xname, sizeof(ifp->if_xname), "shmif%d", unit); 186 1.29 pooka ifp->if_softc = sc; 187 1.63 ozaki ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST; 188 1.29 pooka ifp->if_init = shmif_init; 189 1.29 pooka ifp->if_ioctl = shmif_ioctl; 190 1.29 pooka ifp->if_start = shmif_start; 191 1.29 pooka ifp->if_stop = shmif_stop; 192 1.29 pooka ifp->if_mtu = ETHERMTU; 193 1.37 pooka ifp->if_dlt = DLT_EN10MB; 194 1.76 rin ifp->if_capabilities = IFCAP_TSOv4 | IFCAP_TSOv6 | 195 1.76 rin IFCAP_CSUM_IPv4_Rx | IFCAP_CSUM_IPv4_Tx | 196 1.76 rin IFCAP_CSUM_TCPv4_Rx | IFCAP_CSUM_TCPv4_Tx | 197 1.76 rin IFCAP_CSUM_UDPv4_Rx | IFCAP_CSUM_UDPv4_Tx | 198 1.76 rin IFCAP_CSUM_TCPv6_Rx | IFCAP_CSUM_TCPv6_Tx | 199 1.76 rin IFCAP_CSUM_UDPv6_Rx | IFCAP_CSUM_UDPv6_Tx; 200 1.83 ozaki IFQ_SET_READY(&ifp->if_snd); 201 1.29 pooka 202 1.32 pooka mutex_init(&sc->sc_mtx, MUTEX_DEFAULT, IPL_NONE); 203 1.32 pooka cv_init(&sc->sc_cv, "shmifcv"); 204 1.32 pooka 205 1.82 riastrad if_initialize(ifp); 206 1.76 rin #if 1 207 1.76 rin char buf[256]; 208 1.76 rin 209 1.76 rin if (rumpuser_getparam("RUMP_SHMIF_CAPENABLE", buf, sizeof(buf)) == 0) { 210 1.76 rin uint64_t capen = strtoul(buf, NULL, 0); 211 1.76 rin 212 1.76 rin ifp->if_capenable = capen & ifp->if_capabilities; 213 1.76 rin } 214 1.76 rin #endif 215 1.76 rin 216 1.83 ozaki if_deferred_start_init(ifp, NULL); 217 1.29 pooka ether_ifattach(ifp, enaddr); 218 1.65 ozaki if_register(ifp); 219 1.29 pooka 220 1.29 pooka aprint_verbose("shmif%d: Ethernet address %s\n", 221 1.32 pooka unit, ether_sprintf(enaddr)); 222 1.29 pooka 223 1.29 pooka if (scp) 224 1.29 pooka *scp = sc; 225 1.29 pooka 226 1.32 pooka if (rump_threads) { 227 1.32 pooka error = kthread_create(PRI_NONE, 228 1.40 rmind KTHREAD_MPSAFE | KTHREAD_MUSTJOIN, NULL, 229 1.32 pooka shmif_rcv, ifp, &sc->sc_rcvl, "shmif"); 230 1.32 pooka } else { 231 1.32 pooka printf("WARNING: threads not enabled, shmif NOT working\n"); 232 1.32 pooka } 233 1.32 pooka 234 1.32 pooka if (error) { 235 1.32 pooka shmif_unclone(ifp); 236 1.32 pooka } 237 1.32 pooka 238 1.82 riastrad return 0; 239 1.29 pooka } 240 1.29 pooka 241 1.29 pooka static int 242 1.29 pooka initbackend(struct shmif_sc *sc, int memfd) 243 1.29 pooka { 244 1.29 pooka volatile uint8_t v; 245 1.29 pooka volatile uint8_t *p; 246 1.53 pooka void *mem; 247 1.29 pooka int error; 248 1.29 pooka 249 1.53 pooka error = rumpcomp_shmif_mmap(memfd, BUSMEM_SIZE, &mem); 250 1.1 pooka if (error) 251 1.29 pooka return error; 252 1.53 pooka sc->sc_busmem = mem; 253 1.17 pooka 254 1.29 pooka if (sc->sc_busmem->shm_magic 255 1.29 pooka && sc->sc_busmem->shm_magic != SHMIF_MAGIC) { 256 1.29 pooka printf("bus is not magical"); 257 1.29 pooka rumpuser_unmap(sc->sc_busmem, BUSMEM_SIZE); 258 1.69 msaitoh return ENOEXEC; 259 1.29 pooka } 260 1.28 pooka 261 1.36 pooka /* 262 1.36 pooka * Prefault in pages to minimize runtime penalty with buslock. 263 1.36 pooka * Use 512 instead of PAGE_SIZE to make sure we catch cases where 264 1.36 pooka * rump kernel PAGE_SIZE > host page size. 265 1.36 pooka */ 266 1.28 pooka for (p = (uint8_t *)sc->sc_busmem; 267 1.28 pooka p < (uint8_t *)sc->sc_busmem + BUSMEM_SIZE; 268 1.36 pooka p += 512) 269 1.28 pooka v = *p; 270 1.28 pooka 271 1.19 pooka shmif_lockbus(sc->sc_busmem); 272 1.19 pooka /* we're first? initialize bus */ 273 1.19 pooka if (sc->sc_busmem->shm_magic == 0) { 274 1.19 pooka sc->sc_busmem->shm_magic = SHMIF_MAGIC; 275 1.19 pooka sc->sc_busmem->shm_first = BUSMEM_DATASIZE; 276 1.19 pooka } 277 1.19 pooka 278 1.16 pooka sc->sc_nextpacket = sc->sc_busmem->shm_last; 279 1.26 pooka sc->sc_devgen = sc->sc_busmem->shm_gen; 280 1.28 pooka 281 1.28 pooka #ifdef PREFAULT_RW 282 1.28 pooka for (p = (uint8_t *)sc->sc_busmem; 283 1.28 pooka p < (uint8_t *)sc->sc_busmem + BUSMEM_SIZE; 284 1.28 pooka p += PAGE_SIZE) { 285 1.28 pooka v = *p; 286 1.28 pooka *p = v; 287 1.28 pooka } 288 1.28 pooka #endif 289 1.19 pooka shmif_unlockbus(sc->sc_busmem); 290 1.1 pooka 291 1.53 pooka sc->sc_kq = -1; 292 1.53 pooka error = rumpcomp_shmif_watchsetup(&sc->sc_kq, memfd); 293 1.53 pooka if (error) { 294 1.32 pooka rumpuser_unmap(sc->sc_busmem, BUSMEM_SIZE); 295 1.29 pooka return error; 296 1.32 pooka } 297 1.1 pooka 298 1.29 pooka sc->sc_memfd = memfd; 299 1.32 pooka 300 1.32 pooka return error; 301 1.29 pooka } 302 1.29 pooka 303 1.29 pooka static void 304 1.29 pooka finibackend(struct shmif_sc *sc) 305 1.29 pooka { 306 1.29 pooka 307 1.32 pooka if (sc->sc_backfile == NULL) 308 1.32 pooka return; 309 1.32 pooka 310 1.32 pooka if (sc->sc_backfile) { 311 1.32 pooka kmem_free(sc->sc_backfile, sc->sc_backfilelen); 312 1.32 pooka sc->sc_backfile = NULL; 313 1.32 pooka sc->sc_backfilelen = 0; 314 1.32 pooka } 315 1.29 pooka 316 1.29 pooka rumpuser_unmap(sc->sc_busmem, BUSMEM_SIZE); 317 1.53 pooka rumpuser_close(sc->sc_memfd); 318 1.53 pooka rumpuser_close(sc->sc_kq); 319 1.32 pooka 320 1.32 pooka sc->sc_memfd = -1; 321 1.29 pooka } 322 1.29 pooka 323 1.29 pooka int 324 1.29 pooka rump_shmif_create(const char *path, int *ifnum) 325 1.29 pooka { 326 1.29 pooka struct shmif_sc *sc; 327 1.43 dyoung vmem_addr_t t; 328 1.33 pooka int unit, error; 329 1.33 pooka int memfd = -1; /* XXXgcc */ 330 1.29 pooka 331 1.33 pooka if (path) { 332 1.53 pooka error = rumpuser_open(path, 333 1.53 pooka RUMPUSER_OPEN_RDWR | RUMPUSER_OPEN_CREATE, &memfd); 334 1.53 pooka if (error) 335 1.33 pooka return error; 336 1.33 pooka } 337 1.1 pooka 338 1.43 dyoung error = vmem_xalloc(shmif_units, 1, 0, 0, 0, 339 1.43 dyoung VMEM_ADDR_MIN, VMEM_ADDR_MAX, VM_INSTANTFIT | VM_SLEEP, &t); 340 1.43 dyoung 341 1.43 dyoung if (error != 0) { 342 1.43 dyoung if (path) 343 1.53 pooka rumpuser_close(memfd); 344 1.43 dyoung return error; 345 1.43 dyoung } 346 1.43 dyoung 347 1.43 dyoung unit = t - 1; 348 1.31 pooka 349 1.32 pooka if ((error = allocif(unit, &sc)) != 0) { 350 1.33 pooka if (path) 351 1.53 pooka rumpuser_close(memfd); 352 1.29 pooka return error; 353 1.29 pooka } 354 1.33 pooka 355 1.33 pooka if (!path) 356 1.33 pooka goto out; 357 1.33 pooka 358 1.29 pooka error = initbackend(sc, memfd); 359 1.29 pooka if (error) { 360 1.32 pooka shmif_unclone(&sc->sc_ec.ec_if); 361 1.29 pooka return error; 362 1.29 pooka } 363 1.1 pooka 364 1.29 pooka sc->sc_backfilelen = strlen(path)+1; 365 1.29 pooka sc->sc_backfile = kmem_alloc(sc->sc_backfilelen, KM_SLEEP); 366 1.29 pooka strcpy(sc->sc_backfile, path); 367 1.12 pooka 368 1.33 pooka out: 369 1.2 pooka if (ifnum) 370 1.32 pooka *ifnum = unit; 371 1.29 pooka 372 1.1 pooka return 0; 373 1.29 pooka } 374 1.1 pooka 375 1.29 pooka static int 376 1.29 pooka shmif_clone(struct if_clone *ifc, int unit) 377 1.29 pooka { 378 1.61 justin int rc __diagused; 379 1.43 dyoung vmem_addr_t unit2; 380 1.29 pooka 381 1.31 pooka /* 382 1.31 pooka * Ok, we know the unit number, but we must still reserve it. 383 1.31 pooka * Otherwise the wildcard-side of things might get the same one. 384 1.31 pooka * This is slightly offset-happy due to vmem. First, we offset 385 1.31 pooka * the range of unit numbers by +1 since vmem cannot deal with 386 1.41 dyoung * ranges starting from 0. Talk about uuuh. 387 1.31 pooka */ 388 1.43 dyoung rc = vmem_xalloc(shmif_units, 1, 0, 0, 0, unit+1, unit+1, 389 1.43 dyoung VM_SLEEP | VM_INSTANTFIT, &unit2); 390 1.43 dyoung KASSERT(rc == 0 && unit2-1 == unit); 391 1.29 pooka 392 1.29 pooka return allocif(unit, NULL); 393 1.29 pooka } 394 1.29 pooka 395 1.29 pooka static int 396 1.29 pooka shmif_unclone(struct ifnet *ifp) 397 1.29 pooka { 398 1.32 pooka struct shmif_sc *sc = ifp->if_softc; 399 1.32 pooka 400 1.32 pooka shmif_stop(ifp, 1); 401 1.32 pooka if_down(ifp); 402 1.32 pooka 403 1.32 pooka mutex_enter(&sc->sc_mtx); 404 1.32 pooka sc->sc_dying = true; 405 1.32 pooka cv_broadcast(&sc->sc_cv); 406 1.32 pooka mutex_exit(&sc->sc_mtx); 407 1.29 pooka 408 1.32 pooka if (sc->sc_rcvl) 409 1.32 pooka kthread_join(sc->sc_rcvl); 410 1.32 pooka sc->sc_rcvl = NULL; 411 1.32 pooka 412 1.72 ozaki /* 413 1.72 ozaki * Need to be called after the kthread left, otherwise closing kqueue 414 1.72 ozaki * (sc_kq) hangs sometimes perhaps because of a race condition between 415 1.72 ozaki * close and kevent in the kthread on the kqueue. 416 1.72 ozaki */ 417 1.72 ozaki finibackend(sc); 418 1.72 ozaki 419 1.32 pooka vmem_xfree(shmif_units, sc->sc_unit+1, 1); 420 1.32 pooka 421 1.32 pooka ether_ifdetach(ifp); 422 1.32 pooka if_detach(ifp); 423 1.32 pooka 424 1.32 pooka cv_destroy(&sc->sc_cv); 425 1.32 pooka mutex_destroy(&sc->sc_mtx); 426 1.32 pooka 427 1.32 pooka kmem_free(sc, sizeof(*sc)); 428 1.32 pooka 429 1.32 pooka return 0; 430 1.1 pooka } 431 1.1 pooka 432 1.1 pooka static int 433 1.1 pooka shmif_init(struct ifnet *ifp) 434 1.1 pooka { 435 1.29 pooka struct shmif_sc *sc = ifp->if_softc; 436 1.4 pooka int error = 0; 437 1.4 pooka 438 1.29 pooka if (sc->sc_memfd == -1) 439 1.29 pooka return ENXIO; 440 1.32 pooka KASSERT(sc->sc_busmem); 441 1.29 pooka 442 1.32 pooka ifp->if_flags |= IFF_RUNNING; 443 1.32 pooka 444 1.32 pooka mutex_enter(&sc->sc_mtx); 445 1.32 pooka sc->sc_nextpacket = sc->sc_busmem->shm_last; 446 1.32 pooka sc->sc_devgen = sc->sc_busmem->shm_gen; 447 1.32 pooka 448 1.32 pooka cv_broadcast(&sc->sc_cv); 449 1.32 pooka mutex_exit(&sc->sc_mtx); 450 1.1 pooka 451 1.4 pooka return error; 452 1.1 pooka } 453 1.1 pooka 454 1.1 pooka static int 455 1.86 ozaki shmif_mediachange(struct ifnet *ifp) 456 1.86 ozaki { 457 1.86 ozaki struct shmif_sc *sc = ifp->if_softc; 458 1.89 rin int link_state; 459 1.86 ozaki 460 1.89 rin if (IFM_SUBTYPE(sc->sc_im.ifm_cur->ifm_media) == IFM_NONE) 461 1.89 rin link_state = LINK_STATE_DOWN; 462 1.89 rin else 463 1.89 rin link_state = LINK_STATE_UP; 464 1.89 rin 465 1.89 rin if_link_state_change(ifp, link_state); 466 1.86 ozaki return 0; 467 1.86 ozaki } 468 1.86 ozaki 469 1.86 ozaki static void 470 1.86 ozaki shmif_mediastatus(struct ifnet *ifp, struct ifmediareq *imr) 471 1.86 ozaki { 472 1.86 ozaki struct shmif_sc *sc = ifp->if_softc; 473 1.89 rin 474 1.86 ozaki imr->ifm_active = sc->sc_im.ifm_cur->ifm_media; 475 1.89 rin 476 1.89 rin imr->ifm_status = IFM_AVALID; 477 1.89 rin if (IFM_SUBTYPE(imr->ifm_active) != IFM_NONE) 478 1.89 rin imr->ifm_status |= IFM_ACTIVE; 479 1.86 ozaki } 480 1.86 ozaki 481 1.86 ozaki static int 482 1.1 pooka shmif_ioctl(struct ifnet *ifp, u_long cmd, void *data) 483 1.1 pooka { 484 1.29 pooka struct shmif_sc *sc = ifp->if_softc; 485 1.29 pooka struct ifdrv *ifd; 486 1.29 pooka char *path; 487 1.32 pooka int s, rv, memfd; 488 1.1 pooka 489 1.1 pooka s = splnet(); 490 1.29 pooka switch (cmd) { 491 1.29 pooka case SIOCGLINKSTR: 492 1.29 pooka ifd = data; 493 1.29 pooka 494 1.29 pooka if (sc->sc_backfilelen == 0) { 495 1.29 pooka rv = ENOENT; 496 1.29 pooka break; 497 1.29 pooka } 498 1.29 pooka 499 1.29 pooka ifd->ifd_len = sc->sc_backfilelen; 500 1.29 pooka if (ifd->ifd_cmd == IFLINKSTR_QUERYLEN) { 501 1.29 pooka rv = 0; 502 1.29 pooka break; 503 1.29 pooka } 504 1.29 pooka 505 1.29 pooka if (ifd->ifd_cmd != 0) { 506 1.29 pooka rv = EINVAL; 507 1.29 pooka break; 508 1.29 pooka } 509 1.29 pooka 510 1.29 pooka rv = copyoutstr(sc->sc_backfile, ifd->ifd_data, 511 1.29 pooka MIN(sc->sc_backfilelen, ifd->ifd_len), NULL); 512 1.29 pooka break; 513 1.29 pooka case SIOCSLINKSTR: 514 1.29 pooka if (ifp->if_flags & IFF_UP) { 515 1.29 pooka rv = EBUSY; 516 1.29 pooka break; 517 1.29 pooka } 518 1.29 pooka 519 1.29 pooka ifd = data; 520 1.29 pooka if (ifd->ifd_cmd == IFLINKSTR_UNSET) { 521 1.29 pooka finibackend(sc); 522 1.85 ozaki /* Back to the default just in case */ 523 1.85 ozaki ifp->if_link_state = LINK_STATE_UNKNOWN; 524 1.29 pooka rv = 0; 525 1.29 pooka break; 526 1.29 pooka } else if (ifd->ifd_cmd != 0) { 527 1.29 pooka rv = EINVAL; 528 1.29 pooka break; 529 1.29 pooka } else if (sc->sc_backfile) { 530 1.29 pooka rv = EBUSY; 531 1.29 pooka break; 532 1.29 pooka } 533 1.29 pooka 534 1.29 pooka if (ifd->ifd_len > MAXPATHLEN) { 535 1.29 pooka rv = E2BIG; 536 1.29 pooka break; 537 1.29 pooka } else if (ifd->ifd_len < 1) { 538 1.29 pooka rv = EINVAL; 539 1.29 pooka break; 540 1.29 pooka } 541 1.29 pooka 542 1.29 pooka path = kmem_alloc(ifd->ifd_len, KM_SLEEP); 543 1.29 pooka rv = copyinstr(ifd->ifd_data, path, ifd->ifd_len, NULL); 544 1.29 pooka if (rv) { 545 1.29 pooka kmem_free(path, ifd->ifd_len); 546 1.29 pooka break; 547 1.29 pooka } 548 1.53 pooka rv = rumpuser_open(path, 549 1.53 pooka RUMPUSER_OPEN_RDWR | RUMPUSER_OPEN_CREATE, &memfd); 550 1.53 pooka if (rv) { 551 1.29 pooka kmem_free(path, ifd->ifd_len); 552 1.29 pooka break; 553 1.29 pooka } 554 1.29 pooka rv = initbackend(sc, memfd); 555 1.29 pooka if (rv) { 556 1.29 pooka kmem_free(path, ifd->ifd_len); 557 1.53 pooka rumpuser_close(memfd); 558 1.29 pooka break; 559 1.29 pooka } 560 1.29 pooka sc->sc_backfile = path; 561 1.29 pooka sc->sc_backfilelen = ifd->ifd_len; 562 1.29 pooka 563 1.85 ozaki if_link_state_change(ifp, LINK_STATE_UP); 564 1.29 pooka break; 565 1.86 ozaki 566 1.86 ozaki #ifdef OSIOCSIFMEDIA 567 1.86 ozaki case OSIOCSIFMEDIA: 568 1.86 ozaki #endif 569 1.86 ozaki case SIOCSIFMEDIA: 570 1.86 ozaki case SIOCGIFMEDIA: 571 1.86 ozaki rv = ifmedia_ioctl(ifp, data, &sc->sc_im, cmd); 572 1.86 ozaki break; 573 1.86 ozaki 574 1.29 pooka default: 575 1.29 pooka rv = ether_ioctl(ifp, cmd, data); 576 1.29 pooka if (rv == ENETRESET) 577 1.29 pooka rv = 0; 578 1.29 pooka break; 579 1.29 pooka } 580 1.1 pooka splx(s); 581 1.1 pooka 582 1.1 pooka return rv; 583 1.1 pooka } 584 1.1 pooka 585 1.1 pooka static void 586 1.1 pooka shmif_start(struct ifnet *ifp) 587 1.1 pooka { 588 1.1 pooka struct shmif_sc *sc = ifp->if_softc; 589 1.76 rin struct mbuf *m, *n; 590 1.1 pooka bool wrote = false; 591 1.1 pooka 592 1.26 pooka ifp->if_flags |= IFF_OACTIVE; 593 1.26 pooka 594 1.1 pooka for (;;) { 595 1.83 ozaki IFQ_DEQUEUE(&ifp->if_snd, m); 596 1.76 rin if (m == NULL) 597 1.76 rin break; 598 1.20 pooka 599 1.76 rin m = ether_sw_offload_tx(ifp, m); 600 1.76 rin if (m == NULL) { 601 1.77 thorpej if_statinc(ifp, if_oerrors); 602 1.1 pooka break; 603 1.1 pooka } 604 1.1 pooka 605 1.76 rin do { 606 1.76 rin n = m->m_nextpkt; 607 1.76 rin shmif_snd(ifp, m); 608 1.76 rin m = n; 609 1.76 rin } while (m != NULL); 610 1.1 pooka 611 1.1 pooka wrote = true; 612 1.1 pooka } 613 1.26 pooka 614 1.26 pooka ifp->if_flags &= ~IFF_OACTIVE; 615 1.26 pooka 616 1.32 pooka /* wakeup? */ 617 1.52 pooka if (wrote) { 618 1.52 pooka dowakeup(sc); 619 1.52 pooka } 620 1.1 pooka } 621 1.1 pooka 622 1.76 rin /* send everything in-context since it's just a matter of mem-to-mem copy */ 623 1.76 rin static void 624 1.76 rin shmif_snd(struct ifnet *ifp, struct mbuf *m0) 625 1.76 rin { 626 1.76 rin struct shmif_sc *sc = ifp->if_softc; 627 1.76 rin struct shmif_mem *busmem = sc->sc_busmem; 628 1.76 rin struct shmif_pkthdr sp; 629 1.76 rin struct timeval tv; 630 1.76 rin struct mbuf *m; 631 1.76 rin uint32_t dataoff; 632 1.76 rin uint32_t pktsize, pktwrote; 633 1.76 rin bool wrap; 634 1.76 rin 635 1.76 rin pktsize = 0; 636 1.76 rin for (m = m0; m != NULL; m = m->m_next) { 637 1.76 rin pktsize += m->m_len; 638 1.76 rin } 639 1.76 rin KASSERT(pktsize <= ETHERMTU + ETHER_HDR_LEN); 640 1.76 rin 641 1.76 rin getmicrouptime(&tv); 642 1.76 rin sp.sp_len = pktsize; 643 1.76 rin sp.sp_sec = tv.tv_sec; 644 1.76 rin sp.sp_usec = tv.tv_usec; 645 1.80 ozaki sp.sp_sender = sc->sc_uid; 646 1.76 rin 647 1.76 rin bpf_mtap(ifp, m0, BPF_D_OUT); 648 1.76 rin 649 1.85 ozaki /* 650 1.85 ozaki * Compare with DOWN to allow UNKNOWN (the default value), 651 1.87 riastrad * which is required by some ATF tests using rump servers 652 1.87 riastrad * written in C. 653 1.85 ozaki */ 654 1.85 ozaki if (ifp->if_link_state == LINK_STATE_DOWN) 655 1.85 ozaki goto dontsend; 656 1.85 ozaki 657 1.76 rin shmif_lockbus(busmem); 658 1.76 rin KASSERT(busmem->shm_magic == SHMIF_MAGIC); 659 1.76 rin busmem->shm_last = shmif_nextpktoff(busmem, busmem->shm_last); 660 1.76 rin 661 1.76 rin wrap = false; 662 1.76 rin dataoff = 663 1.76 rin shmif_buswrite(busmem, busmem->shm_last, &sp, sizeof(sp), &wrap); 664 1.76 rin pktwrote = 0; 665 1.76 rin for (m = m0; m != NULL; m = m->m_next) { 666 1.76 rin pktwrote += m->m_len; 667 1.76 rin dataoff = shmif_buswrite(busmem, dataoff, mtod(m, void *), 668 1.76 rin m->m_len, &wrap); 669 1.76 rin } 670 1.76 rin KASSERT(pktwrote == pktsize); 671 1.76 rin if (wrap) { 672 1.76 rin busmem->shm_gen++; 673 1.76 rin DPRINTF(("bus generation now %" PRIu64 "\n", busmem->shm_gen)); 674 1.76 rin } 675 1.76 rin shmif_unlockbus(busmem); 676 1.76 rin 677 1.85 ozaki dontsend: 678 1.76 rin m_freem(m0); 679 1.77 thorpej if_statinc(ifp, if_opackets); 680 1.76 rin 681 1.76 rin DPRINTF(("shmif_start: send %d bytes at off %d\n", pktsize, 682 1.76 rin busmem->shm_last)); 683 1.76 rin } 684 1.76 rin 685 1.1 pooka static void 686 1.1 pooka shmif_stop(struct ifnet *ifp, int disable) 687 1.1 pooka { 688 1.32 pooka struct shmif_sc *sc = ifp->if_softc; 689 1.1 pooka 690 1.32 pooka ifp->if_flags &= ~IFF_RUNNING; 691 1.32 pooka membar_producer(); 692 1.32 pooka 693 1.32 pooka /* 694 1.32 pooka * wakeup thread. this will of course wake up all bus 695 1.32 pooka * listeners, but that's life. 696 1.32 pooka */ 697 1.52 pooka if (sc->sc_memfd != -1) { 698 1.52 pooka dowakeup(sc); 699 1.52 pooka } 700 1.1 pooka } 701 1.1 pooka 702 1.27 pooka 703 1.27 pooka /* 704 1.27 pooka * Check if we have been sleeping too long. Basically, 705 1.27 pooka * our in-sc nextpkt must by first <= nextpkt <= last"+1". 706 1.27 pooka * We use the fact that first is guaranteed to never overlap 707 1.27 pooka * with the last frame in the ring. 708 1.27 pooka */ 709 1.27 pooka static __inline bool 710 1.27 pooka stillvalid_p(struct shmif_sc *sc) 711 1.27 pooka { 712 1.27 pooka struct shmif_mem *busmem = sc->sc_busmem; 713 1.27 pooka unsigned gendiff = busmem->shm_gen - sc->sc_devgen; 714 1.27 pooka uint32_t lastoff, devoff; 715 1.27 pooka 716 1.27 pooka KASSERT(busmem->shm_first != busmem->shm_last); 717 1.27 pooka 718 1.27 pooka /* normalize onto a 2x busmem chunk */ 719 1.27 pooka devoff = sc->sc_nextpacket; 720 1.27 pooka lastoff = shmif_nextpktoff(busmem, busmem->shm_last); 721 1.27 pooka 722 1.27 pooka /* trivial case */ 723 1.27 pooka if (gendiff > 1) 724 1.27 pooka return false; 725 1.27 pooka KASSERT(gendiff <= 1); 726 1.27 pooka 727 1.27 pooka /* Normalize onto 2x busmem chunk */ 728 1.27 pooka if (busmem->shm_first >= lastoff) { 729 1.27 pooka lastoff += BUSMEM_DATASIZE; 730 1.27 pooka if (gendiff == 0) 731 1.27 pooka devoff += BUSMEM_DATASIZE; 732 1.27 pooka } else { 733 1.27 pooka if (gendiff) 734 1.27 pooka return false; 735 1.27 pooka } 736 1.27 pooka 737 1.27 pooka return devoff >= busmem->shm_first && devoff <= lastoff; 738 1.27 pooka } 739 1.27 pooka 740 1.1 pooka static void 741 1.1 pooka shmif_rcv(void *arg) 742 1.1 pooka { 743 1.1 pooka struct ifnet *ifp = arg; 744 1.1 pooka struct shmif_sc *sc = ifp->if_softc; 745 1.32 pooka struct shmif_mem *busmem; 746 1.1 pooka struct mbuf *m = NULL; 747 1.1 pooka struct ether_header *eth; 748 1.27 pooka uint32_t nextpkt; 749 1.37 pooka bool wrap, passup; 750 1.1 pooka int error; 751 1.57 pooka const int align 752 1.57 pooka = ALIGN(sizeof(struct ether_header)) - sizeof(struct ether_header); 753 1.1 pooka 754 1.32 pooka reup: 755 1.32 pooka mutex_enter(&sc->sc_mtx); 756 1.32 pooka while ((ifp->if_flags & IFF_RUNNING) == 0 && !sc->sc_dying) 757 1.32 pooka cv_wait(&sc->sc_cv, &sc->sc_mtx); 758 1.32 pooka mutex_exit(&sc->sc_mtx); 759 1.32 pooka 760 1.32 pooka busmem = sc->sc_busmem; 761 1.32 pooka 762 1.32 pooka while (ifp->if_flags & IFF_RUNNING) { 763 1.20 pooka struct shmif_pkthdr sp; 764 1.20 pooka 765 1.1 pooka if (m == NULL) { 766 1.1 pooka m = m_gethdr(M_WAIT, MT_DATA); 767 1.1 pooka MCLGET(m, M_WAIT); 768 1.57 pooka m->m_data += align; 769 1.1 pooka } 770 1.1 pooka 771 1.47 pooka DPRINTF(("waiting %d/%" PRIu64 "\n", 772 1.47 pooka sc->sc_nextpacket, sc->sc_devgen)); 773 1.26 pooka KASSERT(m->m_flags & M_EXT); 774 1.1 pooka 775 1.26 pooka shmif_lockbus(busmem); 776 1.26 pooka KASSERT(busmem->shm_magic == SHMIF_MAGIC); 777 1.27 pooka KASSERT(busmem->shm_gen >= sc->sc_devgen); 778 1.1 pooka 779 1.1 pooka /* need more data? */ 780 1.27 pooka if (sc->sc_devgen == busmem->shm_gen && 781 1.26 pooka shmif_nextpktoff(busmem, busmem->shm_last) 782 1.26 pooka == sc->sc_nextpacket) { 783 1.26 pooka shmif_unlockbus(busmem); 784 1.71 ozaki error = rumpcomp_shmif_watchwait(sc->sc_kq); 785 1.1 pooka if (__predict_false(error)) 786 1.1 pooka printf("shmif_rcv: wait failed %d\n", error); 787 1.32 pooka membar_consumer(); 788 1.1 pooka continue; 789 1.1 pooka } 790 1.1 pooka 791 1.27 pooka if (stillvalid_p(sc)) { 792 1.27 pooka nextpkt = sc->sc_nextpacket; 793 1.27 pooka } else { 794 1.27 pooka KASSERT(busmem->shm_gen > 0); 795 1.26 pooka nextpkt = busmem->shm_first; 796 1.26 pooka if (busmem->shm_first > busmem->shm_last) 797 1.27 pooka sc->sc_devgen = busmem->shm_gen - 1; 798 1.26 pooka else 799 1.27 pooka sc->sc_devgen = busmem->shm_gen; 800 1.47 pooka DPRINTF(("dev %p overrun, new data: %d/%" PRIu64 "\n", 801 1.27 pooka sc, nextpkt, sc->sc_devgen)); 802 1.26 pooka } 803 1.26 pooka 804 1.26 pooka /* 805 1.26 pooka * If our read pointer is ahead the bus last write, our 806 1.26 pooka * generation must be one behind. 807 1.26 pooka */ 808 1.26 pooka KASSERT(!(nextpkt > busmem->shm_last 809 1.27 pooka && sc->sc_devgen == busmem->shm_gen)); 810 1.26 pooka 811 1.24 pooka wrap = false; 812 1.26 pooka nextpkt = shmif_busread(busmem, &sp, 813 1.26 pooka nextpkt, sizeof(sp), &wrap); 814 1.26 pooka KASSERT(sp.sp_len <= ETHERMTU + ETHER_HDR_LEN); 815 1.26 pooka nextpkt = shmif_busread(busmem, mtod(m, void *), 816 1.26 pooka nextpkt, sp.sp_len, &wrap); 817 1.1 pooka 818 1.1 pooka DPRINTF(("shmif_rcv: read packet of length %d at %d\n", 819 1.20 pooka sp.sp_len, nextpkt)); 820 1.1 pooka 821 1.26 pooka sc->sc_nextpacket = nextpkt; 822 1.19 pooka shmif_unlockbus(sc->sc_busmem); 823 1.1 pooka 824 1.27 pooka if (wrap) { 825 1.26 pooka sc->sc_devgen++; 826 1.47 pooka DPRINTF(("dev %p generation now %" PRIu64 "\n", 827 1.27 pooka sc, sc->sc_devgen)); 828 1.27 pooka } 829 1.26 pooka 830 1.56 pooka /* 831 1.56 pooka * Ignore packets too short to possibly be valid. 832 1.56 pooka * This is hit at least for the first frame on a new bus. 833 1.56 pooka */ 834 1.55 pooka if (__predict_false(sp.sp_len < ETHER_HDR_LEN)) { 835 1.55 pooka DPRINTF(("shmif read packet len %d < ETHER_HDR_LEN\n", 836 1.55 pooka sp.sp_len)); 837 1.55 pooka continue; 838 1.55 pooka } 839 1.55 pooka 840 1.20 pooka m->m_len = m->m_pkthdr.len = sp.sp_len; 841 1.67 ozaki m_set_rcvif(m, ifp); 842 1.1 pooka 843 1.37 pooka /* 844 1.37 pooka * Test if we want to pass the packet upwards 845 1.37 pooka */ 846 1.1 pooka eth = mtod(m, struct ether_header *); 847 1.85 ozaki /* 848 1.85 ozaki * Compare with DOWN to allow UNKNOWN (the default value), 849 1.87 riastrad * which is required by some ATF tests using rump servers 850 1.87 riastrad * written in C. 851 1.85 ozaki */ 852 1.85 ozaki if (ifp->if_link_state == LINK_STATE_DOWN) { 853 1.85 ozaki passup = false; 854 1.85 ozaki } else if (sp.sp_sender == sc->sc_uid) { 855 1.63 ozaki passup = false; 856 1.63 ozaki } else if (memcmp(eth->ether_dhost, CLLADDR(ifp->if_sadl), 857 1.37 pooka ETHER_ADDR_LEN) == 0) { 858 1.37 pooka passup = true; 859 1.46 pooka } else if (ETHER_IS_MULTICAST(eth->ether_dhost)) { 860 1.37 pooka passup = true; 861 1.37 pooka } else if (ifp->if_flags & IFF_PROMISC) { 862 1.37 pooka m->m_flags |= M_PROMISC; 863 1.37 pooka passup = true; 864 1.38 pooka } else { 865 1.38 pooka passup = false; 866 1.37 pooka } 867 1.37 pooka 868 1.37 pooka if (passup) { 869 1.68 ozaki int bound; 870 1.76 rin 871 1.76 rin m = ether_sw_offload_rx(ifp, m); 872 1.76 rin 873 1.22 pooka KERNEL_LOCK(1, NULL); 874 1.66 ozaki /* Prevent LWP migrations between CPUs for psref(9) */ 875 1.68 ozaki bound = curlwp_bind(); 876 1.65 ozaki if_input(ifp, m); 877 1.68 ozaki curlwp_bindx(bound); 878 1.22 pooka KERNEL_UNLOCK_ONE(NULL); 879 1.76 rin 880 1.1 pooka m = NULL; 881 1.1 pooka } 882 1.37 pooka /* else: reuse mbuf for a future packet */ 883 1.1 pooka } 884 1.32 pooka m_freem(m); 885 1.32 pooka m = NULL; 886 1.32 pooka 887 1.32 pooka if (!sc->sc_dying) 888 1.32 pooka goto reup; 889 1.1 pooka 890 1.32 pooka kthread_exit(0); 891 1.1 pooka } 892