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