xref: /netbsd-src/sys/rump/net/lib/libshmif/if_shmem.c (revision 212397c69a103ae7e5eafa8731ddfae671d2dee7)
1 /*	$NetBSD: if_shmem.c,v 1.64 2016/01/26 23:12:19 pooka 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.64 2016/01/26 23:12:19 pooka 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_attach(ifp);
191 	ether_ifattach(ifp, enaddr);
192 
193 	aprint_verbose("shmif%d: Ethernet address %s\n",
194 	    unit, ether_sprintf(enaddr));
195 
196 	if (scp)
197 		*scp = sc;
198 
199 	error = 0;
200 	if (rump_threads) {
201 		error = kthread_create(PRI_NONE,
202 		    KTHREAD_MPSAFE | KTHREAD_MUSTJOIN, NULL,
203 		    shmif_rcv, ifp, &sc->sc_rcvl, "shmif");
204 	} else {
205 		printf("WARNING: threads not enabled, shmif NOT working\n");
206 	}
207 
208 	if (error) {
209 		shmif_unclone(ifp);
210 	}
211 
212 	return error;
213 }
214 
215 static int
216 initbackend(struct shmif_sc *sc, int memfd)
217 {
218 	volatile uint8_t v;
219 	volatile uint8_t *p;
220 	void *mem;
221 	int error;
222 
223 	error = rumpcomp_shmif_mmap(memfd, BUSMEM_SIZE, &mem);
224 	if (error)
225 		return error;
226 	sc->sc_busmem = mem;
227 
228 	if (sc->sc_busmem->shm_magic
229 	    && sc->sc_busmem->shm_magic != SHMIF_MAGIC) {
230 		printf("bus is not magical");
231 		rumpuser_unmap(sc->sc_busmem, BUSMEM_SIZE);
232 		return ENOEXEC;
233 	}
234 
235 	/*
236 	 * Prefault in pages to minimize runtime penalty with buslock.
237 	 * Use 512 instead of PAGE_SIZE to make sure we catch cases where
238 	 * rump kernel PAGE_SIZE > host page size.
239 	 */
240 	for (p = (uint8_t *)sc->sc_busmem;
241 	    p < (uint8_t *)sc->sc_busmem + BUSMEM_SIZE;
242 	    p += 512)
243 		v = *p;
244 
245 	shmif_lockbus(sc->sc_busmem);
246 	/* we're first?  initialize bus */
247 	if (sc->sc_busmem->shm_magic == 0) {
248 		sc->sc_busmem->shm_magic = SHMIF_MAGIC;
249 		sc->sc_busmem->shm_first = BUSMEM_DATASIZE;
250 	}
251 
252 	sc->sc_nextpacket = sc->sc_busmem->shm_last;
253 	sc->sc_devgen = sc->sc_busmem->shm_gen;
254 
255 #ifdef PREFAULT_RW
256 	for (p = (uint8_t *)sc->sc_busmem;
257 	    p < (uint8_t *)sc->sc_busmem + BUSMEM_SIZE;
258 	    p += PAGE_SIZE) {
259 		v = *p;
260 		*p = v;
261 	}
262 #endif
263 	shmif_unlockbus(sc->sc_busmem);
264 
265 	sc->sc_kq = -1;
266 	error = rumpcomp_shmif_watchsetup(&sc->sc_kq, memfd);
267 	if (error) {
268 		rumpuser_unmap(sc->sc_busmem, BUSMEM_SIZE);
269 		return error;
270 	}
271 
272 	sc->sc_memfd = memfd;
273 
274 	return error;
275 }
276 
277 static void
278 finibackend(struct shmif_sc *sc)
279 {
280 
281 	if (sc->sc_backfile == NULL)
282 		return;
283 
284 	if (sc->sc_backfile) {
285 		kmem_free(sc->sc_backfile, sc->sc_backfilelen);
286 		sc->sc_backfile = NULL;
287 		sc->sc_backfilelen = 0;
288 	}
289 
290 	rumpuser_unmap(sc->sc_busmem, BUSMEM_SIZE);
291 	rumpuser_close(sc->sc_memfd);
292 	rumpuser_close(sc->sc_kq);
293 
294 	sc->sc_memfd = -1;
295 }
296 
297 int
298 rump_shmif_create(const char *path, int *ifnum)
299 {
300 	struct shmif_sc *sc;
301 	vmem_addr_t t;
302 	int unit, error;
303 	int memfd = -1; /* XXXgcc */
304 
305 	if (path) {
306 		error = rumpuser_open(path,
307 		    RUMPUSER_OPEN_RDWR | RUMPUSER_OPEN_CREATE, &memfd);
308 		if (error)
309 			return error;
310 	}
311 
312 	error = vmem_xalloc(shmif_units, 1, 0, 0, 0,
313 	    VMEM_ADDR_MIN, VMEM_ADDR_MAX, VM_INSTANTFIT | VM_SLEEP, &t);
314 
315 	if (error != 0) {
316 		if (path)
317 			rumpuser_close(memfd);
318 		return error;
319 	}
320 
321 	unit = t - 1;
322 
323 	if ((error = allocif(unit, &sc)) != 0) {
324 		if (path)
325 			rumpuser_close(memfd);
326 		return error;
327 	}
328 
329 	if (!path)
330 		goto out;
331 
332 	error = initbackend(sc, memfd);
333 	if (error) {
334 		shmif_unclone(&sc->sc_ec.ec_if);
335 		return error;
336 	}
337 
338 	sc->sc_backfilelen = strlen(path)+1;
339 	sc->sc_backfile = kmem_alloc(sc->sc_backfilelen, KM_SLEEP);
340 	strcpy(sc->sc_backfile, path);
341 
342  out:
343 	if (ifnum)
344 		*ifnum = unit;
345 
346 	return 0;
347 }
348 
349 static int
350 shmif_clone(struct if_clone *ifc, int unit)
351 {
352 	int rc __diagused;
353 	vmem_addr_t unit2;
354 
355 	/*
356 	 * Ok, we know the unit number, but we must still reserve it.
357 	 * Otherwise the wildcard-side of things might get the same one.
358 	 * This is slightly offset-happy due to vmem.  First, we offset
359 	 * the range of unit numbers by +1 since vmem cannot deal with
360 	 * ranges starting from 0.  Talk about uuuh.
361 	 */
362 	rc = vmem_xalloc(shmif_units, 1, 0, 0, 0, unit+1, unit+1,
363 	    VM_SLEEP | VM_INSTANTFIT, &unit2);
364 	KASSERT(rc == 0 && unit2-1 == unit);
365 
366 	return allocif(unit, NULL);
367 }
368 
369 static int
370 shmif_unclone(struct ifnet *ifp)
371 {
372 	struct shmif_sc *sc = ifp->if_softc;
373 
374 	shmif_stop(ifp, 1);
375 	if_down(ifp);
376 	finibackend(sc);
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 	vmem_xfree(shmif_units, sc->sc_unit+1, 1);
388 
389 	ether_ifdetach(ifp);
390 	if_detach(ifp);
391 
392 	cv_destroy(&sc->sc_cv);
393 	mutex_destroy(&sc->sc_mtx);
394 
395 	kmem_free(sc, sizeof(*sc));
396 
397 	return 0;
398 }
399 
400 static int
401 shmif_init(struct ifnet *ifp)
402 {
403 	struct shmif_sc *sc = ifp->if_softc;
404 	int error = 0;
405 
406 	if (sc->sc_memfd == -1)
407 		return ENXIO;
408 	KASSERT(sc->sc_busmem);
409 
410 	ifp->if_flags |= IFF_RUNNING;
411 
412 	mutex_enter(&sc->sc_mtx);
413 	sc->sc_nextpacket = sc->sc_busmem->shm_last;
414 	sc->sc_devgen = sc->sc_busmem->shm_gen;
415 
416 	cv_broadcast(&sc->sc_cv);
417 	mutex_exit(&sc->sc_mtx);
418 
419 	return error;
420 }
421 
422 static int
423 shmif_ioctl(struct ifnet *ifp, u_long cmd, void *data)
424 {
425 	struct shmif_sc *sc = ifp->if_softc;
426 	struct ifdrv *ifd;
427 	char *path;
428 	int s, rv, memfd;
429 
430 	s = splnet();
431 	switch (cmd) {
432 	case SIOCGLINKSTR:
433 		ifd = data;
434 
435 		if (sc->sc_backfilelen == 0) {
436 			rv = ENOENT;
437 			break;
438 		}
439 
440 		ifd->ifd_len = sc->sc_backfilelen;
441 		if (ifd->ifd_cmd == IFLINKSTR_QUERYLEN) {
442 			rv = 0;
443 			break;
444 		}
445 
446 		if (ifd->ifd_cmd != 0) {
447 			rv = EINVAL;
448 			break;
449 		}
450 
451 		rv = copyoutstr(sc->sc_backfile, ifd->ifd_data,
452 		    MIN(sc->sc_backfilelen, ifd->ifd_len), NULL);
453 		break;
454 	case SIOCSLINKSTR:
455 		if (ifp->if_flags & IFF_UP) {
456 			rv = EBUSY;
457 			break;
458 		}
459 
460 		ifd = data;
461 		if (ifd->ifd_cmd == IFLINKSTR_UNSET) {
462 			finibackend(sc);
463 			rv = 0;
464 			break;
465 		} else if (ifd->ifd_cmd != 0) {
466 			rv = EINVAL;
467 			break;
468 		} else if (sc->sc_backfile) {
469 			rv = EBUSY;
470 			break;
471 		}
472 
473 		if (ifd->ifd_len > MAXPATHLEN) {
474 			rv = E2BIG;
475 			break;
476 		} else if (ifd->ifd_len < 1) {
477 			rv = EINVAL;
478 			break;
479 		}
480 
481 		path = kmem_alloc(ifd->ifd_len, KM_SLEEP);
482 		rv = copyinstr(ifd->ifd_data, path, ifd->ifd_len, NULL);
483 		if (rv) {
484 			kmem_free(path, ifd->ifd_len);
485 			break;
486 		}
487 		rv = rumpuser_open(path,
488 		    RUMPUSER_OPEN_RDWR | RUMPUSER_OPEN_CREATE, &memfd);
489 		if (rv) {
490 			kmem_free(path, ifd->ifd_len);
491 			break;
492 		}
493 		rv = initbackend(sc, memfd);
494 		if (rv) {
495 			kmem_free(path, ifd->ifd_len);
496 			rumpuser_close(memfd);
497 			break;
498 		}
499 		sc->sc_backfile = path;
500 		sc->sc_backfilelen = ifd->ifd_len;
501 
502 		break;
503 	default:
504 		rv = ether_ioctl(ifp, cmd, data);
505 		if (rv == ENETRESET)
506 			rv = 0;
507 		break;
508 	}
509 	splx(s);
510 
511 	return rv;
512 }
513 
514 /* send everything in-context since it's just a matter of mem-to-mem copy */
515 static void
516 shmif_start(struct ifnet *ifp)
517 {
518 	struct shmif_sc *sc = ifp->if_softc;
519 	struct shmif_mem *busmem = sc->sc_busmem;
520 	struct mbuf *m, *m0;
521 	uint32_t dataoff;
522 	uint32_t pktsize, pktwrote;
523 	bool wrote = false;
524 	bool wrap;
525 
526 	ifp->if_flags |= IFF_OACTIVE;
527 
528 	for (;;) {
529 		struct shmif_pkthdr sp;
530 		struct timeval tv;
531 
532 		IF_DEQUEUE(&ifp->if_snd, m0);
533 		if (m0 == NULL) {
534 			break;
535 		}
536 
537 		pktsize = 0;
538 		for (m = m0; m != NULL; m = m->m_next) {
539 			pktsize += m->m_len;
540 		}
541 		KASSERT(pktsize <= ETHERMTU + ETHER_HDR_LEN);
542 
543 		getmicrouptime(&tv);
544 		sp.sp_len = pktsize;
545 		sp.sp_sec = tv.tv_sec;
546 		sp.sp_usec = tv.tv_usec;
547 		sp.sp_sender = sc->sc_uuid;
548 
549 		bpf_mtap(ifp, m0);
550 
551 		shmif_lockbus(busmem);
552 		KASSERT(busmem->shm_magic == SHMIF_MAGIC);
553 		busmem->shm_last = shmif_nextpktoff(busmem, busmem->shm_last);
554 
555 		wrap = false;
556 		dataoff = shmif_buswrite(busmem,
557 		    busmem->shm_last, &sp, sizeof(sp), &wrap);
558 		pktwrote = 0;
559 		for (m = m0; m != NULL; m = m->m_next) {
560 			pktwrote += m->m_len;
561 			dataoff = shmif_buswrite(busmem, dataoff,
562 			    mtod(m, void *), m->m_len, &wrap);
563 		}
564 		KASSERT(pktwrote == pktsize);
565 		if (wrap) {
566 			busmem->shm_gen++;
567 			DPRINTF(("bus generation now %" PRIu64 "\n",
568 			    busmem->shm_gen));
569 		}
570 		shmif_unlockbus(busmem);
571 
572 		m_freem(m0);
573 		wrote = true;
574 		ifp->if_opackets++;
575 
576 		DPRINTF(("shmif_start: send %d bytes at off %d\n",
577 		    pktsize, busmem->shm_last));
578 	}
579 
580 	ifp->if_flags &= ~IFF_OACTIVE;
581 
582 	/* wakeup? */
583 	if (wrote) {
584 		dowakeup(sc);
585 	}
586 }
587 
588 static void
589 shmif_stop(struct ifnet *ifp, int disable)
590 {
591 	struct shmif_sc *sc = ifp->if_softc;
592 
593 	ifp->if_flags &= ~IFF_RUNNING;
594 	membar_producer();
595 
596 	/*
597 	 * wakeup thread.  this will of course wake up all bus
598 	 * listeners, but that's life.
599 	 */
600 	if (sc->sc_memfd != -1) {
601 		dowakeup(sc);
602 	}
603 }
604 
605 
606 /*
607  * Check if we have been sleeping too long.  Basically,
608  * our in-sc nextpkt must by first <= nextpkt <= last"+1".
609  * We use the fact that first is guaranteed to never overlap
610  * with the last frame in the ring.
611  */
612 static __inline bool
613 stillvalid_p(struct shmif_sc *sc)
614 {
615 	struct shmif_mem *busmem = sc->sc_busmem;
616 	unsigned gendiff = busmem->shm_gen - sc->sc_devgen;
617 	uint32_t lastoff, devoff;
618 
619 	KASSERT(busmem->shm_first != busmem->shm_last);
620 
621 	/* normalize onto a 2x busmem chunk */
622 	devoff = sc->sc_nextpacket;
623 	lastoff = shmif_nextpktoff(busmem, busmem->shm_last);
624 
625 	/* trivial case */
626 	if (gendiff > 1)
627 		return false;
628 	KASSERT(gendiff <= 1);
629 
630 	/* Normalize onto 2x busmem chunk */
631 	if (busmem->shm_first >= lastoff) {
632 		lastoff += BUSMEM_DATASIZE;
633 		if (gendiff == 0)
634 			devoff += BUSMEM_DATASIZE;
635 	} else {
636 		if (gendiff)
637 			return false;
638 	}
639 
640 	return devoff >= busmem->shm_first && devoff <= lastoff;
641 }
642 
643 static void
644 shmif_rcv(void *arg)
645 {
646 	struct ifnet *ifp = arg;
647 	struct shmif_sc *sc = ifp->if_softc;
648 	struct shmif_mem *busmem;
649 	struct mbuf *m = NULL;
650 	struct ether_header *eth;
651 	uint32_t nextpkt;
652 	bool wrap, passup;
653 	int error;
654 	const int align
655 	    = ALIGN(sizeof(struct ether_header)) - sizeof(struct ether_header);
656 
657  reup:
658 	mutex_enter(&sc->sc_mtx);
659 	while ((ifp->if_flags & IFF_RUNNING) == 0 && !sc->sc_dying)
660 		cv_wait(&sc->sc_cv, &sc->sc_mtx);
661 	mutex_exit(&sc->sc_mtx);
662 
663 	busmem = sc->sc_busmem;
664 
665 	while (ifp->if_flags & IFF_RUNNING) {
666 		struct shmif_pkthdr sp;
667 
668 		if (m == NULL) {
669 			m = m_gethdr(M_WAIT, MT_DATA);
670 			MCLGET(m, M_WAIT);
671 			m->m_data += align;
672 		}
673 
674 		DPRINTF(("waiting %d/%" PRIu64 "\n",
675 		    sc->sc_nextpacket, sc->sc_devgen));
676 		KASSERT(m->m_flags & M_EXT);
677 
678 		shmif_lockbus(busmem);
679 		KASSERT(busmem->shm_magic == SHMIF_MAGIC);
680 		KASSERT(busmem->shm_gen >= sc->sc_devgen);
681 
682 		/* need more data? */
683 		if (sc->sc_devgen == busmem->shm_gen &&
684 		    shmif_nextpktoff(busmem, busmem->shm_last)
685 		     == sc->sc_nextpacket) {
686 			shmif_unlockbus(busmem);
687 			error = 0;
688 			rumpcomp_shmif_watchwait(sc->sc_kq);
689 			if (__predict_false(error))
690 				printf("shmif_rcv: wait failed %d\n", error);
691 			membar_consumer();
692 			continue;
693 		}
694 
695 		if (stillvalid_p(sc)) {
696 			nextpkt = sc->sc_nextpacket;
697 		} else {
698 			KASSERT(busmem->shm_gen > 0);
699 			nextpkt = busmem->shm_first;
700 			if (busmem->shm_first > busmem->shm_last)
701 				sc->sc_devgen = busmem->shm_gen - 1;
702 			else
703 				sc->sc_devgen = busmem->shm_gen;
704 			DPRINTF(("dev %p overrun, new data: %d/%" PRIu64 "\n",
705 			    sc, nextpkt, sc->sc_devgen));
706 		}
707 
708 		/*
709 		 * If our read pointer is ahead the bus last write, our
710 		 * generation must be one behind.
711 		 */
712 		KASSERT(!(nextpkt > busmem->shm_last
713 		    && sc->sc_devgen == busmem->shm_gen));
714 
715 		wrap = false;
716 		nextpkt = shmif_busread(busmem, &sp,
717 		    nextpkt, sizeof(sp), &wrap);
718 		KASSERT(sp.sp_len <= ETHERMTU + ETHER_HDR_LEN);
719 		nextpkt = shmif_busread(busmem, mtod(m, void *),
720 		    nextpkt, sp.sp_len, &wrap);
721 
722 		DPRINTF(("shmif_rcv: read packet of length %d at %d\n",
723 		    sp.sp_len, nextpkt));
724 
725 		sc->sc_nextpacket = nextpkt;
726 		shmif_unlockbus(sc->sc_busmem);
727 
728 		if (wrap) {
729 			sc->sc_devgen++;
730 			DPRINTF(("dev %p generation now %" PRIu64 "\n",
731 			    sc, sc->sc_devgen));
732 		}
733 
734 		/*
735 		 * Ignore packets too short to possibly be valid.
736 		 * This is hit at least for the first frame on a new bus.
737 		 */
738 		if (__predict_false(sp.sp_len < ETHER_HDR_LEN)) {
739 			DPRINTF(("shmif read packet len %d < ETHER_HDR_LEN\n",
740 			    sp.sp_len));
741 			continue;
742 		}
743 
744 		m->m_len = m->m_pkthdr.len = sp.sp_len;
745 		m->m_pkthdr.rcvif = ifp;
746 
747 		/*
748 		 * Test if we want to pass the packet upwards
749 		 */
750 		eth = mtod(m, struct ether_header *);
751 		if (sp.sp_sender == sc->sc_uuid) {
752 			passup = false;
753 		} else if (memcmp(eth->ether_dhost, CLLADDR(ifp->if_sadl),
754 		    ETHER_ADDR_LEN) == 0) {
755 			passup = true;
756 		} else if (ETHER_IS_MULTICAST(eth->ether_dhost)) {
757 			passup = true;
758 		} else if (ifp->if_flags & IFF_PROMISC) {
759 			m->m_flags |= M_PROMISC;
760 			passup = true;
761 		} else {
762 			passup = false;
763 		}
764 
765 		if (passup) {
766 			ifp->if_ipackets++;
767 			KERNEL_LOCK(1, NULL);
768 			bpf_mtap(ifp, m);
769 			ifp->if_input(ifp, m);
770 			KERNEL_UNLOCK_ONE(NULL);
771 			m = NULL;
772 		}
773 		/* else: reuse mbuf for a future packet */
774 	}
775 	m_freem(m);
776 	m = NULL;
777 
778 	if (!sc->sc_dying)
779 		goto reup;
780 
781 	kthread_exit(0);
782 }
783