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