KNOTE still needs mplock, so tapifstart() could be called in MPSAFE theads.
[dragonfly.git] / sys / net / tap / if_tap.c
blobf1e6a2a4bbb5b376b6dfe457a1e22baabd6d346a
1 /*
2 * Copyright (C) 1999-2000 by Maksim Yevmenkin <m_evmenkin@yahoo.com>
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
26 * BASED ON:
27 * -------------------------------------------------------------------------
29 * Copyright (c) 1988, Julian Onions <jpo@cs.nott.ac.uk>
30 * Nottingham University 1987.
34 * $FreeBSD: src/sys/net/if_tap.c,v 1.3.2.3 2002/04/14 21:41:48 luigi Exp $
35 * $DragonFly: src/sys/net/tap/if_tap.c,v 1.39 2008/05/18 02:40:41 sephe Exp $
36 * $Id: if_tap.c,v 0.21 2000/07/23 21:46:02 max Exp $
39 #include "opt_inet.h"
41 #include <sys/param.h>
42 #include <sys/conf.h>
43 #include <sys/device.h>
44 #include <sys/filedesc.h>
45 #include <sys/filio.h>
46 #include <sys/kernel.h>
47 #include <sys/malloc.h>
48 #include <sys/mbuf.h>
49 #include <sys/poll.h>
50 #include <sys/proc.h>
51 #include <sys/signalvar.h>
52 #include <sys/socket.h>
53 #include <sys/sockio.h>
54 #include <sys/sysctl.h>
55 #include <sys/systm.h>
56 #include <sys/thread2.h>
57 #include <sys/ttycom.h>
58 #include <sys/uio.h>
59 #include <sys/vnode.h>
60 #include <sys/serialize.h>
62 #include <net/bpf.h>
63 #include <net/ethernet.h>
64 #include <net/if.h>
65 #include <net/ifq_var.h>
66 #include <net/if_arp.h>
67 #include <net/route.h>
69 #include <netinet/in.h>
71 #include "if_tapvar.h"
72 #include "if_tap.h"
75 #define CDEV_NAME "tap"
76 #define CDEV_MAJOR 149
77 #define TAPDEBUG if (tapdebug) if_printf
79 #define TAP "tap"
80 #define VMNET "vmnet"
81 #define VMNET_DEV_MASK 0x00010000
83 /* module */
84 static int tapmodevent (module_t, int, void *);
86 /* device */
87 static void tapcreate (cdev_t);
89 /* network interface */
90 static void tapifstart (struct ifnet *);
91 static int tapifioctl (struct ifnet *, u_long, caddr_t,
92 struct ucred *);
93 static void tapifinit (void *);
95 /* character device */
96 static d_open_t tapopen;
97 static d_close_t tapclose;
98 static d_read_t tapread;
99 static d_write_t tapwrite;
100 static d_ioctl_t tapioctl;
101 static d_poll_t tappoll;
102 static d_kqfilter_t tapkqfilter;
104 static struct dev_ops tap_ops = {
105 { CDEV_NAME, CDEV_MAJOR, 0 },
106 .d_open = tapopen,
107 .d_close = tapclose,
108 .d_read = tapread,
109 .d_write = tapwrite,
110 .d_ioctl = tapioctl,
111 .d_poll = tappoll,
112 .d_kqfilter = tapkqfilter
115 static int taprefcnt = 0; /* module ref. counter */
116 static int taplastunit = -1; /* max. open unit number */
117 static int tapdebug = 0; /* debug flag */
119 MALLOC_DECLARE(M_TAP);
120 MALLOC_DEFINE(M_TAP, CDEV_NAME, "Ethernet tunnel interface");
121 SYSCTL_INT(_debug, OID_AUTO, if_tap_debug, CTLFLAG_RW, &tapdebug, 0, "");
122 DEV_MODULE(if_tap, tapmodevent, NULL);
125 * tapmodevent
127 * module event handler
129 static int
130 tapmodevent(module_t mod, int type, void *data)
132 static int attached = 0;
133 struct ifnet *ifp = NULL;
134 int unit;
136 switch (type) {
137 case MOD_LOAD:
138 if (attached)
139 return (EEXIST);
141 dev_ops_add(&tap_ops, 0, 0);
142 attached = 1;
143 break;
145 case MOD_UNLOAD:
146 if (taprefcnt > 0)
147 return (EBUSY);
149 dev_ops_remove(&tap_ops, 0, 0);
151 /* XXX: maintain tap ifs in a local list */
152 unit = 0;
153 while (unit <= taplastunit) {
154 TAILQ_FOREACH(ifp, &ifnet, if_link) {
155 if ((strcmp(ifp->if_dname, TAP) == 0) ||
156 (strcmp(ifp->if_dname, VMNET) == 0)) {
157 if (ifp->if_dunit == unit)
158 break;
162 if (ifp != NULL) {
163 struct tap_softc *tp = ifp->if_softc;
165 TAPDEBUG(ifp, "detached. minor = %#x, " \
166 "taplastunit = %d\n",
167 minor(tp->tap_dev), taplastunit);
169 ether_ifdetach(ifp);
170 destroy_dev(tp->tap_dev);
171 kfree(tp, M_TAP);
173 else
174 unit ++;
177 attached = 0;
178 break;
180 default:
181 return (EOPNOTSUPP);
184 return (0);
185 } /* tapmodevent */
189 * tapcreate
191 * to create interface
193 static void
194 tapcreate(cdev_t dev)
196 struct ifnet *ifp = NULL;
197 struct tap_softc *tp = NULL;
198 uint8_t ether_addr[ETHER_ADDR_LEN];
199 int unit;
200 char *name = NULL;
202 /* allocate driver storage and create device */
203 MALLOC(tp, struct tap_softc *, sizeof(*tp), M_TAP, M_WAITOK | M_ZERO);
205 /* select device: tap or vmnet */
206 if (minor(dev) & VMNET_DEV_MASK) {
207 name = VMNET;
208 unit = lminor(dev) & 0xff;
209 tp->tap_flags |= TAP_VMNET;
211 else {
212 name = TAP;
213 unit = lminor(dev);
216 tp->tap_dev = make_dev(&tap_ops, minor(dev), UID_ROOT, GID_WHEEL,
217 0600, "%s%d", name, unit);
218 tp->tap_dev->si_drv1 = dev->si_drv1 = tp;
219 reference_dev(tp->tap_dev); /* so we can destroy it later */
221 /* generate fake MAC address: 00 bd xx xx xx unit_no */
222 ether_addr[0] = 0x00;
223 ether_addr[1] = 0xbd;
224 bcopy(&ticks, &ether_addr[2], 3);
225 ether_addr[5] = (u_char)unit;
227 /* fill the rest and attach interface */
228 ifp = &tp->tap_if;
229 ifp->if_softc = tp;
231 if_initname(ifp, name, unit);
232 if (unit > taplastunit)
233 taplastunit = unit;
235 ifp->if_init = tapifinit;
236 ifp->if_start = tapifstart;
237 ifp->if_ioctl = tapifioctl;
238 ifp->if_mtu = ETHERMTU;
239 ifp->if_flags = (IFF_BROADCAST|IFF_SIMPLEX|IFF_MULTICAST);
240 ifq_set_maxlen(&ifp->if_snd, ifqmaxlen);
241 ifq_set_ready(&ifp->if_snd);
243 ether_ifattach(ifp, ether_addr, NULL);
245 tp->tap_flags |= TAP_INITED;
247 TAPDEBUG(ifp, "created. minor = %#x\n", minor(tp->tap_dev));
248 } /* tapcreate */
252 * tapopen
254 * to open tunnel. must be superuser
256 static int
257 tapopen(struct dev_open_args *ap)
259 cdev_t dev = ap->a_head.a_dev;
260 struct tap_softc *tp = NULL;
261 struct ifnet *ifp = NULL;
262 int error;
264 if ((error = suser_cred(ap->a_cred, 0)) != 0)
265 return (error);
267 get_mplock();
268 tp = dev->si_drv1;
269 if (tp == NULL) {
270 tapcreate(dev);
271 tp = dev->si_drv1;
272 ifp = &tp->arpcom.ac_if;
273 } else {
274 ifp = &tp->arpcom.ac_if;
276 EVENTHANDLER_INVOKE(ifnet_attach_event, ifp);
278 /* Announce the return of the interface. */
279 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
282 if (tp->tap_flags & TAP_OPEN) {
283 rel_mplock();
284 return (EBUSY);
287 bcopy(tp->arpcom.ac_enaddr, tp->ether_addr, sizeof(tp->ether_addr));
289 tp->tap_td = curthread;
290 tp->tap_flags |= TAP_OPEN;
291 taprefcnt ++;
293 TAPDEBUG(ifp, "opened. minor = %#x, refcnt = %d, taplastunit = %d\n",
294 minor(tp->tap_dev), taprefcnt, taplastunit);
296 rel_mplock();
297 return (0);
302 * tapclose
304 * close the device - mark i/f down & delete routing info
306 static int
307 tapclose(struct dev_close_args *ap)
309 cdev_t dev = ap->a_head.a_dev;
310 struct tap_softc *tp = dev->si_drv1;
311 struct ifnet *ifp = &tp->tap_if;
313 /* junk all pending output */
315 get_mplock();
316 lwkt_serialize_enter(ifp->if_serializer);
317 ifq_purge(&ifp->if_snd);
318 lwkt_serialize_exit(ifp->if_serializer);
321 * do not bring the interface down, and do not anything with
322 * interface, if we are in VMnet mode. just close the device.
325 if ((tp->tap_flags & TAP_VMNET) == 0) {
326 if (ifp->if_flags & IFF_UP) {
327 lwkt_serialize_enter(ifp->if_serializer);
328 if_down(ifp);
329 lwkt_serialize_exit(ifp->if_serializer);
331 ifp->if_flags &= ~IFF_RUNNING;
333 if_purgeaddrs_nolink(ifp);
335 EVENTHANDLER_INVOKE(ifnet_detach_event, ifp);
337 /* Announce the departure of the interface. */
338 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
340 funsetown(tp->tap_sigio);
341 selwakeup(&tp->tap_rsel);
343 tp->tap_flags &= ~TAP_OPEN;
344 tp->tap_td = NULL;
346 taprefcnt --;
347 if (taprefcnt < 0) {
348 taprefcnt = 0;
349 if_printf(ifp, "minor = %#x, refcnt = %d is out of sync. "
350 "set refcnt to 0\n", minor(tp->tap_dev), taprefcnt);
353 TAPDEBUG(ifp, "closed. minor = %#x, refcnt = %d, taplastunit = %d\n",
354 minor(tp->tap_dev), taprefcnt, taplastunit);
356 rel_mplock();
357 return (0);
362 * tapifinit
364 * Network interface initialization function (called with if serializer held)
366 * MPSAFE
368 static void
369 tapifinit(void *xtp)
371 struct tap_softc *tp = (struct tap_softc *)xtp;
372 struct ifnet *ifp = &tp->tap_if;
374 TAPDEBUG(ifp, "initializing, minor = %#x\n", minor(tp->tap_dev));
376 ifp->if_flags |= IFF_RUNNING;
377 ifp->if_flags &= ~IFF_OACTIVE;
379 /* attempt to start output */
380 tapifstart(ifp);
385 * tapifioctl
387 * Process an ioctl request on network interface (called with if serializer
388 * held).
390 * MPSAFE
392 static int
393 tapifioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr)
395 struct tap_softc *tp = (struct tap_softc *)(ifp->if_softc);
396 struct ifstat *ifs = NULL;
397 int dummy;
399 switch (cmd) {
400 case SIOCSIFADDR:
401 case SIOCGIFADDR:
402 case SIOCSIFMTU:
403 dummy = ether_ioctl(ifp, cmd, data);
404 return (dummy);
406 case SIOCSIFFLAGS:
407 if ((tp->tap_flags & TAP_VMNET) == 0) {
409 * Only for non-vmnet tap(4)
411 if (ifp->if_flags & IFF_UP) {
412 if ((ifp->if_flags & IFF_RUNNING) == 0)
413 tapifinit(tp);
414 } else {
415 /* We don't have a tapifstop() */
416 ifp->if_flags &= ~IFF_RUNNING;
419 break;
420 case SIOCADDMULTI: /* XXX -- just like vmnet does */
421 case SIOCDELMULTI:
422 break;
424 case SIOCGIFSTATUS:
425 ifs = (struct ifstat *)data;
426 dummy = strlen(ifs->ascii);
427 if (tp->tap_td != NULL && dummy < sizeof(ifs->ascii)) {
428 if (tp->tap_td->td_proc) {
429 ksnprintf(ifs->ascii + dummy,
430 sizeof(ifs->ascii) - dummy,
431 "\tOpened by pid %d\n",
432 (int)tp->tap_td->td_proc->p_pid);
433 } else {
434 ksnprintf(ifs->ascii + dummy,
435 sizeof(ifs->ascii) - dummy,
436 "\tOpened by td %p\n", tp->tap_td);
439 break;
441 default:
442 return (EINVAL);
445 return (0);
450 * tapifstart
452 * Queue packets from higher level ready to put out (called with if serializer
453 * held)
455 * MPSAFE
457 static void
458 tapifstart(struct ifnet *ifp)
460 struct tap_softc *tp = ifp->if_softc;
462 TAPDEBUG(ifp, "starting, minor = %#x\n", minor(tp->tap_dev));
465 * do not junk pending output if we are in VMnet mode.
466 * XXX: can this do any harm because of queue overflow?
469 if (((tp->tap_flags & TAP_VMNET) == 0) &&
470 ((tp->tap_flags & TAP_READY) != TAP_READY)) {
471 TAPDEBUG(ifp, "not ready. minor = %#x, tap_flags = 0x%x\n",
472 minor(tp->tap_dev), tp->tap_flags);
474 ifq_purge(&ifp->if_snd);
475 return;
478 ifp->if_flags |= IFF_OACTIVE;
480 if (!ifq_is_empty(&ifp->if_snd)) {
481 if (tp->tap_flags & TAP_RWAIT) {
482 tp->tap_flags &= ~TAP_RWAIT;
483 wakeup((caddr_t)tp);
486 get_mplock();
487 KNOTE(&tp->tap_rsel.si_note, 0);
488 rel_mplock();
490 if ((tp->tap_flags & TAP_ASYNC) && (tp->tap_sigio != NULL)) {
491 get_mplock();
492 pgsigio(tp->tap_sigio, SIGIO, 0);
493 rel_mplock();
497 * selwakeup is not MPSAFE. tapifstart is.
499 get_mplock();
500 selwakeup(&tp->tap_rsel);
501 rel_mplock();
502 ifp->if_opackets ++; /* obytes are counted in ether_output */
505 ifp->if_flags &= ~IFF_OACTIVE;
510 * tapioctl
512 * The ops interface is now pretty minimal. Called via fileops with nothing
513 * held.
515 * MPSAFE
517 static int
518 tapioctl(struct dev_ioctl_args *ap)
520 cdev_t dev = ap->a_head.a_dev;
521 caddr_t data = ap->a_data;
522 struct tap_softc *tp = dev->si_drv1;
523 struct ifnet *ifp = &tp->tap_if;
524 struct tapinfo *tapp = NULL;
525 struct mbuf *mb;
526 short f;
527 int error;
529 lwkt_serialize_enter(ifp->if_serializer);
530 error = 0;
532 switch (ap->a_cmd) {
533 case TAPSIFINFO:
534 tapp = (struct tapinfo *)data;
535 ifp->if_mtu = tapp->mtu;
536 ifp->if_type = tapp->type;
537 ifp->if_baudrate = tapp->baudrate;
538 break;
540 case TAPGIFINFO:
541 tapp = (struct tapinfo *)data;
542 tapp->mtu = ifp->if_mtu;
543 tapp->type = ifp->if_type;
544 tapp->baudrate = ifp->if_baudrate;
545 break;
547 case TAPSDEBUG:
548 tapdebug = *(int *)data;
549 break;
551 case TAPGDEBUG:
552 *(int *)data = tapdebug;
553 break;
555 case FIOASYNC:
556 if (*(int *)data)
557 tp->tap_flags |= TAP_ASYNC;
558 else
559 tp->tap_flags &= ~TAP_ASYNC;
560 break;
562 case FIONREAD:
563 *(int *)data = 0;
564 if ((mb = ifq_poll(&ifp->if_snd)) != NULL) {
565 for(; mb != NULL; mb = mb->m_next)
566 *(int *)data += mb->m_len;
568 break;
570 case FIOSETOWN:
571 error = fsetown(*(int *)data, &tp->tap_sigio);
572 break;
574 case FIOGETOWN:
575 *(int *)data = fgetown(tp->tap_sigio);
576 break;
578 /* this is deprecated, FIOSETOWN should be used instead */
579 case TIOCSPGRP:
580 error = fsetown(-(*(int *)data), &tp->tap_sigio);
581 break;
583 /* this is deprecated, FIOGETOWN should be used instead */
584 case TIOCGPGRP:
585 *(int *)data = -fgetown(tp->tap_sigio);
586 break;
588 /* VMware/VMnet port ioctl's */
590 case SIOCGIFFLAGS: /* get ifnet flags */
591 bcopy(&ifp->if_flags, data, sizeof(ifp->if_flags));
592 break;
594 case VMIO_SIOCSIFFLAGS: /* VMware/VMnet SIOCSIFFLAGS */
595 f = *(short *)data;
596 f &= 0x0fff;
597 f &= ~IFF_CANTCHANGE;
598 f |= IFF_UP;
599 ifp->if_flags = f | (ifp->if_flags & IFF_CANTCHANGE);
600 break;
602 case OSIOCGIFADDR: /* get MAC address of the remote side */
603 case SIOCGIFADDR:
604 bcopy(tp->ether_addr, data, sizeof(tp->ether_addr));
605 break;
607 case SIOCSIFADDR: /* set MAC address of the remote side */
608 bcopy(data, tp->ether_addr, sizeof(tp->ether_addr));
609 break;
611 default:
612 error = ENOTTY;
613 break;
615 lwkt_serialize_exit(ifp->if_serializer);
616 return (error);
621 * tapread
623 * The ops read interface - reads a packet at a time, or at
624 * least as much of a packet as can be read.
626 * Called from the fileops interface with nothing held.
628 * MPSAFE
630 static int
631 tapread(struct dev_read_args *ap)
633 cdev_t dev = ap->a_head.a_dev;
634 struct uio *uio = ap->a_uio;
635 struct tap_softc *tp = dev->si_drv1;
636 struct ifnet *ifp = &tp->tap_if;
637 struct mbuf *m0 = NULL;
638 int error = 0, len;
640 TAPDEBUG(ifp, "reading, minor = %#x\n", minor(tp->tap_dev));
642 if ((tp->tap_flags & TAP_READY) != TAP_READY) {
643 TAPDEBUG(ifp, "not ready. minor = %#x, tap_flags = 0x%x\n",
644 minor(tp->tap_dev), tp->tap_flags);
646 return (EHOSTDOWN);
649 tp->tap_flags &= ~TAP_RWAIT;
651 /* sleep until we get a packet */
652 do {
653 lwkt_serialize_enter(ifp->if_serializer);
654 m0 = ifq_dequeue(&ifp->if_snd, NULL);
655 if (m0 == NULL) {
656 if (ap->a_ioflag & IO_NDELAY) {
657 lwkt_serialize_exit(ifp->if_serializer);
658 return (EWOULDBLOCK);
660 tp->tap_flags |= TAP_RWAIT;
661 crit_enter();
662 tsleep_interlock(tp);
663 lwkt_serialize_exit(ifp->if_serializer);
664 error = tsleep(tp, PCATCH, "taprd", 0);
665 crit_exit();
666 if (error)
667 return (error);
668 } else {
669 lwkt_serialize_exit(ifp->if_serializer);
671 } while (m0 == NULL);
673 BPF_MTAP(ifp, m0);
675 /* xfer packet to user space */
676 while ((m0 != NULL) && (uio->uio_resid > 0) && (error == 0)) {
677 len = min(uio->uio_resid, m0->m_len);
678 if (len == 0)
679 break;
681 error = uiomove(mtod(m0, caddr_t), len, uio);
682 m0 = m_free(m0);
685 if (m0 != NULL) {
686 TAPDEBUG(ifp, "dropping mbuf, minor = %#x\n",
687 minor(tp->tap_dev));
688 m_freem(m0);
691 return (error);
695 * tapwrite
697 * The ops write interface - an atomic write is a packet - or else!
699 * Called from the fileops interface with nothing held.
701 * MPSAFE
703 static int
704 tapwrite(struct dev_write_args *ap)
706 cdev_t dev = ap->a_head.a_dev;
707 struct uio *uio = ap->a_uio;
708 struct tap_softc *tp = dev->si_drv1;
709 struct ifnet *ifp = &tp->tap_if;
710 struct mbuf *top = NULL, **mp = NULL, *m = NULL;
711 int error = 0, tlen, mlen;
713 TAPDEBUG(ifp, "writing, minor = %#x\n", minor(tp->tap_dev));
715 if (uio->uio_resid == 0)
716 return (0);
718 if ((uio->uio_resid < 0) || (uio->uio_resid > TAPMRU)) {
719 TAPDEBUG(ifp, "invalid packet len = %d, minor = %#x\n",
720 uio->uio_resid, minor(tp->tap_dev));
722 return (EIO);
724 tlen = uio->uio_resid;
726 /* get a header mbuf */
727 MGETHDR(m, MB_DONTWAIT, MT_DATA);
728 if (m == NULL)
729 return (ENOBUFS);
730 mlen = MHLEN;
732 top = 0;
733 mp = &top;
734 while ((error == 0) && (uio->uio_resid > 0)) {
735 m->m_len = min(mlen, uio->uio_resid);
736 error = uiomove(mtod(m, caddr_t), m->m_len, uio);
737 *mp = m;
738 mp = &m->m_next;
739 if (uio->uio_resid > 0) {
740 MGET(m, MB_DONTWAIT, MT_DATA);
741 if (m == NULL) {
742 error = ENOBUFS;
743 break;
745 mlen = MLEN;
748 if (error) {
749 ifp->if_ierrors ++;
750 if (top)
751 m_freem(top);
752 return (error);
755 top->m_pkthdr.len = tlen;
756 top->m_pkthdr.rcvif = ifp;
759 * Ethernet bridge and bpf are handled in ether_input
761 * adjust mbuf and give packet to the ether_input
763 lwkt_serialize_enter(ifp->if_serializer);
764 ifp->if_input(ifp, top);
765 ifp->if_ipackets ++; /* ibytes are counted in ether_input */
766 lwkt_serialize_exit(ifp->if_serializer);
768 return (0);
772 * tappoll
774 * The poll interface, this is only useful on reads really. The write
775 * detect always returns true, write never blocks anyway, it either
776 * accepts the packet or drops it
778 * Called from the fileops interface with nothing held.
780 * MPSAFE
782 static int
783 tappoll(struct dev_poll_args *ap)
785 cdev_t dev = ap->a_head.a_dev;
786 struct tap_softc *tp = dev->si_drv1;
787 struct ifnet *ifp = &tp->tap_if;
788 int revents = 0;
790 TAPDEBUG(ifp, "polling, minor = %#x\n", minor(tp->tap_dev));
792 lwkt_serialize_enter(ifp->if_serializer);
793 if (ap->a_events & (POLLIN | POLLRDNORM)) {
794 if (!ifq_is_empty(&ifp->if_snd)) {
795 TAPDEBUG(ifp,
796 "has data in queue. minor = %#x\n",
797 minor(tp->tap_dev));
799 revents |= (ap->a_events & (POLLIN | POLLRDNORM));
800 } else {
801 TAPDEBUG(ifp, "waiting for data, minor = %#x\n",
802 minor(tp->tap_dev));
804 get_mplock();
805 selrecord(curthread, &tp->tap_rsel);
806 rel_mplock();
809 lwkt_serialize_exit(ifp->if_serializer);
811 if (ap->a_events & (POLLOUT | POLLWRNORM))
812 revents |= (ap->a_events & (POLLOUT | POLLWRNORM));
813 ap->a_events = revents;
814 return(0);
818 * tapkqfilter - called from the fileops interface with nothing held
820 * MPSAFE
822 static int filt_tapread(struct knote *kn, long hint);
823 static void filt_tapdetach(struct knote *kn);
824 static struct filterops tapread_filtops =
825 { 1, NULL, filt_tapdetach, filt_tapread };
827 static int
828 tapkqfilter(struct dev_kqfilter_args *ap)
830 cdev_t dev = ap->a_head.a_dev;
831 struct knote *kn = ap->a_kn;
832 struct tap_softc *tp;
833 struct klist *list;
834 struct ifnet *ifp;
836 get_mplock();
837 tp = dev->si_drv1;
838 ifp = &tp->tap_if;
839 ap->a_result =0;
841 switch(kn->kn_filter) {
842 case EVFILT_READ:
843 list = &tp->tap_rsel.si_note;
844 kn->kn_fop = &tapread_filtops;
845 kn->kn_hook = (void *)tp;
846 break;
847 case EVFILT_WRITE:
848 /* fall through */
849 default:
850 ap->a_result = 1;
851 rel_mplock();
852 return(0);
854 crit_enter();
855 SLIST_INSERT_HEAD(list, kn, kn_selnext);
856 crit_exit();
857 rel_mplock();
858 return(0);
861 static int
862 filt_tapread(struct knote *kn, long hint)
864 struct tap_softc *tp = (void *)kn->kn_hook;
865 struct ifnet *ifp = &tp->tap_if;
867 if (ifq_is_empty(&ifp->if_snd) == 0) {
868 return(1);
869 } else {
870 return(0);
874 static void
875 filt_tapdetach(struct knote *kn)
877 struct tap_softc *tp = (void *)kn->kn_hook;
879 SLIST_REMOVE(&tp->tap_rsel.si_note, kn, knote, kn_selnext);