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