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