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