Added explicit errno reporting from error() API.
[akaros.git] / kern / src / net / devip.c
index ef3cd30..92d89f8 100644 (file)
 #include <smp.h>
 #include <ip.h>
 
+struct dev ipdevtab;
+
+static char *devname(void)
+{
+       return ipdevtab.name;
+}
+
 enum {
        Qtopdir = 1,                            /* top level directory */
        Qtopbase,
@@ -67,6 +74,23 @@ extern char *eve;
 static long ndbwrite(struct Fs *, char *unused_char_p_t, uint32_t, int);
 static void closeconv(struct conv *);
 
+static inline int founddevdir(struct chan *c, struct qid q, char *n,
+                                                         int64_t length, char *user, long perm,
+                                                         struct dir *db)
+{
+       devdir(c, q, n, length, user, perm, db);
+       return 1;
+}
+
+static int topdirgen(struct chan *c, struct dir *dp)
+{
+       struct qid q;
+       mkqid(&q, QID(0, 0, Qtopdir), 0, QTDIR);
+       snprintf(get_cur_genbuf(), GENBUF_SZ, "#%s%lu", devname(), c->dev);
+       return founddevdir(c, q, get_cur_genbuf(), 0, network, 0555, dp);
+}
+
+
 static int ip3gen(struct chan *c, int i, struct dir *dp)
 {
        struct qid q;
@@ -82,17 +106,16 @@ static int ip3gen(struct chan *c, int i, struct dir *dp)
                default:
                        return -1;
                case Qctl:
-                       devdir(c, q, "ctl", 0, cv->owner, cv->perm, dp);
-                       return 1;
+                       return founddevdir(c, q, "ctl", 0,
+                                                  cv->owner, cv->perm, dp);
                case Qdata:
-                       devdir(c, q, "data", qlen(cv->rq), cv->owner, cv->perm, dp);
-                       return 1;
+                       return founddevdir(c, q, "data", qlen(cv->rq),
+                                                          cv->owner, cv->perm, dp);
                case Qerr:
-                       devdir(c, q, "err", qlen(cv->eq), cv->owner, cv->perm, dp);
-                       return 1;
+                       return founddevdir(c, q, "err", qlen(cv->eq),
+                                                          cv->owner, cv->perm, dp);
                case Qlisten:
-                       devdir(c, q, "listen", 0, cv->owner, cv->perm, dp);
-                       return 1;
+                       return founddevdir(c, q, "listen", 0, cv->owner, cv->perm, dp);
                case Qlocal:
                        p = "local";
                        break;
@@ -102,29 +125,24 @@ static int ip3gen(struct chan *c, int i, struct dir *dp)
                case Qsnoop:
                        if (strcmp(cv->p->name, "ipifc") != 0)
                                return -1;
-                       devdir(c, q, "snoop", qlen(cv->sq), cv->owner, 0400, dp);
-                       return 1;
+                       return founddevdir(c, q, "snoop", qlen(cv->sq),
+                                                          cv->owner, 0400, dp);
                case Qstatus:
                        p = "status";
                        break;
        }
-       devdir(c, q, p, 0, cv->owner, 0444, dp);
-       return 1;
+       return founddevdir(c, q, p, 0, cv->owner, 0444, dp);
 }
 
 static int ip2gen(struct chan *c, int i, struct dir *dp)
 {
        struct qid q;
-
+       mkqid(&q, QID(PROTO(c->qid), 0, i), 0, QTFILE);
        switch (i) {
                case Qclone:
-                       mkqid(&q, QID(PROTO(c->qid), 0, Qclone), 0, QTFILE);
-                       devdir(c, q, "clone", 0, network, 0666, dp);
-                       return 1;
+                       return founddevdir(c, q, "clone", 0, network, 0666, dp);
                case Qstats:
-                       mkqid(&q, QID(PROTO(c->qid), 0, Qstats), 0, QTFILE);
-                       devdir(c, q, "stats", 0, network, 0444, dp);
-                       return 1;
+                       return founddevdir(c, q, "stats", 0, network, 0444, dp);
        }
        return -1;
 }
@@ -149,9 +167,9 @@ static int ip1gen(struct chan *c, int i, struct dir *dp)
                        p = "arp";
                        break;
                case Qbootp:
-                       p = "bootp";
                        if (bootp == NULL)
                                return 0;
+                       p = "bootp";
                        break;
                case Qndb:
                        p = "ndb";
@@ -190,18 +208,13 @@ ipgen(struct chan *c, char *unused_char_p_t, struct dirtab *d, int unused_int,
 
        switch (TYPE(c->qid)) {
                case Qtopdir:
-                       if (s == DEVDOTDOT) {
-                               mkqid(&q, QID(0, 0, Qtopdir), 0, QTDIR);
-                               snprintf(get_cur_genbuf(), GENBUF_SZ, "#I%lu", c->dev);
-                               devdir(c, q, get_cur_genbuf(), 0, network, 0555, dp);
-                               return 1;
-                       }
+                       if (s == DEVDOTDOT)
+                               return topdirgen(c, dp);
                        if (s < f->np) {
                                if (f->p[s]->connect == NULL)
                                        return 0;       /* protocol with no user interface */
                                mkqid(&q, QID(s, 0, Qprotodir), 0, QTDIR);
-                               devdir(c, q, f->p[s]->name, 0, network, 0555, dp);
-                               return 1;
+                               return founddevdir(c, q, f->p[s]->name, 0, network, 0555, dp);
                        }
                        s -= f->np;
                        return ip1gen(c, s + Qtopbase, dp);
@@ -214,18 +227,14 @@ ipgen(struct chan *c, char *unused_char_p_t, struct dirtab *d, int unused_int,
                case Qipselftab:
                        return ip1gen(c, TYPE(c->qid), dp);
                case Qprotodir:
-                       if (s == DEVDOTDOT) {
-                               mkqid(&q, QID(0, 0, Qtopdir), 0, QTDIR);
-                               snprintf(get_cur_genbuf(), GENBUF_SZ, "#I%lu", c->dev);
-                               devdir(c, q, get_cur_genbuf(), 0, network, 0555, dp);
-                               return 1;
-                       }
-                       if (s < f->p[PROTO(c->qid)]->ac) {
+                       if (s == DEVDOTDOT)
+                               return topdirgen(c, dp);
+                       else if (s < f->p[PROTO(c->qid)]->ac) {
                                cv = f->p[PROTO(c->qid)]->conv[s];
                                snprintf(get_cur_genbuf(), GENBUF_SZ, "%d", s);
                                mkqid(&q, QID(PROTO(c->qid), s, Qconvdir), 0, QTDIR);
-                               devdir(c, q, get_cur_genbuf(), 0, cv->owner, 0555, dp);
-                               return 1;
+                               return
+                                       founddevdir(c, q, get_cur_genbuf(), 0, cv->owner, 0555, dp);
                        }
                        s -= f->p[PROTO(c->qid)]->ac;
                        return ip2gen(c, s + Qprotobase, dp);
@@ -322,10 +331,10 @@ static struct chan *ipattach(char *spec)
 
        dev = atoi(spec);
        if (dev >= Nfs)
-               error("bad specification");
+               error(EFAIL, "bad specification");
 
        ipgetfs(dev);
-       c = devattach('I', spec);
+       c = devattach(devname(), spec);
        mkqid(&c->qid, QID(0, 0, Qtopdir), 0, QTDIR);
        c->dev = dev;
 
@@ -360,12 +369,6 @@ static int should_wake(void *arg)
        return cv->incall != NULL;
 }
 
-static int m2p[] = {
-       [OREAD] 4,
-       [OWRITE] 2,
-       [ORDWR] 6
-};
-
 static struct chan *ipopen(struct chan *c, int omode)
 {
        ERRSTACK(2);
@@ -374,7 +377,8 @@ static struct chan *ipopen(struct chan *c, int omode)
        int perm;
        struct Fs *f;
 
-       perm = m2p[omode & 3];
+       /* perm is a lone rwx, not the rwx------ from the conversion */
+       perm = omode_to_rwx(omode) >> 6;
 
        f = ipfs[c->dev];
 
@@ -382,9 +386,9 @@ static struct chan *ipopen(struct chan *c, int omode)
                default:
                        break;
                case Qndb:
-                       if (omode & (OWRITE | OTRUNC) && !iseve())
-                               error(Eperm);
-                       if ((omode & (OWRITE | OTRUNC)) == (OWRITE | OTRUNC))
+                       if (omode & (O_WRITE | O_TRUNC) && !iseve())
+                               error(EPERM, NULL);
+                       if ((omode & (O_WRITE | O_TRUNC)) == (O_WRITE | O_TRUNC))
                                f->ndb[0] = 0;
                        break;
                case Qlog:
@@ -404,16 +408,16 @@ static struct chan *ipopen(struct chan *c, int omode)
                case Qstats:
                case Qbootp:
                case Qipselftab:
-                       if (!IS_RDONLY(omode))
-                               error(Eperm);
+                       if (omode & O_WRITE)
+                               error(EPERM, NULL);
                        break;
                case Qsnoop:
-                       if (!IS_RDONLY(omode))
-                               error(Eperm);
+                       if (omode & O_WRITE)
+                               error(EPERM, NULL);
                        p = f->p[PROTO(c->qid)];
                        cv = p->conv[CONV(c->qid)];
                        if (strcmp(ATTACHER(c), cv->owner) != 0 && !iseve())
-                               error(Eperm);
+                               error(EPERM, NULL);
                        atomic_inc(&cv->snoopers);
                        break;
                case Qclone:
@@ -427,9 +431,12 @@ static struct chan *ipopen(struct chan *c, int omode)
                        qunlock(&p->qlock);
                        poperror();
                        if (cv == NULL) {
-                               error(Enodev);
+                               error(ENODEV, NULL);
                                break;
                        }
+                       /* we only honor nonblock on a clone */
+                       if (c->flag & O_NONBLOCK)
+                               Fsconvnonblock(cv, TRUE);
                        mkqid(&c->qid, QID(p->x, cv->x, Qctl), 0, QTFILE);
                        break;
                case Qdata:
@@ -446,9 +453,9 @@ static struct chan *ipopen(struct chan *c, int omode)
                        }
                        if ((perm & (cv->perm >> 6)) != perm) {
                                if (strcmp(ATTACHER(c), cv->owner) != 0)
-                                       error(Eperm);
+                                       error(EPERM, NULL);
                                if ((perm & cv->perm) != perm)
-                                       error(Eperm);
+                                       error(EPERM, NULL);
 
                        }
                        cv->inuse++;
@@ -462,16 +469,32 @@ static struct chan *ipopen(struct chan *c, int omode)
                        break;
                case Qlisten:
                        cv = f->p[PROTO(c->qid)]->conv[CONV(c->qid)];
+                       /* No permissions or Announce checks required.  We'll see if that's
+                        * a good idea or not. (the perm check would do nothing, as is,
+                        * since an O_PATH perm is 0).
+                        *
+                        * But we probably want to incref to keep the conversation around
+                        * until this FD/chan is closed.  #ip is a little weird in that
+                        * objects never really go away (high water mark for convs, you can
+                        * always find them in the ns).  I think it is possible to
+                        * namec/ipgen a chan, then have that conv close, then have that
+                        * chan be opened.  You can probably do this with a data file. */
+                       if (omode & O_PATH) {
+                               qlock(&cv->qlock);
+                               cv->inuse++;
+                               qunlock(&cv->qlock);
+                               break;
+                       }
                        if ((perm & (cv->perm >> 6)) != perm) {
                                if (strcmp(ATTACHER(c), cv->owner) != 0)
-                                       error(Eperm);
+                                       error(EPERM, NULL);
                                if ((perm & cv->perm) != perm)
-                                       error(Eperm);
+                                       error(EPERM, NULL);
 
                        }
 
                        if (cv->state != Announced)
-                               error("not announced");
+                               error(EFAIL, "not announced");
 
                        if (waserror()) {
                                closeconv(cv);
@@ -485,14 +508,18 @@ static struct chan *ipopen(struct chan *c, int omode)
                        while (nc == NULL) {
                                /* give up if we got a hangup */
                                if (qisclosed(cv->rq))
-                                       error("listen hungup");
+                                       error(EFAIL, "listen hungup");
 
                                qlock(&cv->listenq);
                                if (waserror()) {
                                        qunlock(&cv->listenq);
                                        nexterror();
                                }
-
+                               /* we can peek at incall without grabbing the cv qlock.  if
+                                * anything is there, it'll remain there until we dequeue it.
+                                * no one else can, since we hold the listenq lock */
+                               if (cv->nonblock && !cv->incall)
+                                       error(EAGAIN, "listen queue empty");
                                /* wait for a connect */
                                rendez_sleep(&cv->listenr, should_wake, cv);
 
@@ -504,6 +531,10 @@ static struct chan *ipopen(struct chan *c, int omode)
                                        cv->incall = nc->next;
                                        mkqid(&c->qid, QID(PROTO(c->qid), nc->x, Qctl), 0, QTFILE);
                                        kstrdup(&cv->owner, ATTACHER(c));
+                                       /* O_NONBLOCK/CNONBLOCK when opening listen means the *new*
+                                        * conv is already non-blocking, like accept4() in Linux */
+                                       if (c->flag & O_NONBLOCK)
+                                               Fsconvnonblock(nc, TRUE);
                                }
                                qunlock(&cv->qlock);
 
@@ -531,7 +562,7 @@ static int ipwstat(struct chan *c, uint8_t * dp, int n)
        f = ipfs[c->dev];
        switch (TYPE(c->qid)) {
                default:
-                       error(Eperm);
+                       error(EPERM, NULL);
                        break;
                case Qctl:
                case Qdata:
@@ -545,11 +576,11 @@ static int ipwstat(struct chan *c, uint8_t * dp, int n)
        }
        n = convM2D(dp, n, d, (char *)&d[1]);
        if (n == 0)
-               error(Eshortstat);
+               error(ENODATA, NULL);
        p = f->p[PROTO(c->qid)];
        cv = p->conv[CONV(c->qid)];
        if (!iseve() && strcmp(ATTACHER(c), cv->owner) != 0)
-               error(Eperm);
+               error(EPERM, NULL);
        if (!emptystr(d->uid))
                kstrdup(&cv->owner, d->uid);
        if (d->mode != ~0UL)
@@ -585,6 +616,12 @@ static char *ipchaninfo(struct chan *ch, char *ret, size_t ret_l)
                case Qiproute:
                        ret = "Qiproute";
                        break;
+               case Qlisten:
+                       proto = f->p[PROTO(ch->qid)];
+                       conv = proto->conv[CONV(ch->qid)];
+                       snprintf(ret, ret_l, "Qlisten, proto %s, conv idx %d", proto->name,
+                                        conv->x);
+                       break;
                case Qlog:
                        ret = "Qlog";
                        break;
@@ -683,7 +720,7 @@ static long ipread(struct chan *ch, void *a, long n, int64_t off)
        p = a;
        switch (TYPE(ch->qid)) {
                default:
-                       error(Eperm);
+                       error(EPERM, NULL);
                case Qtopdir:
                case Qprotodir:
                case Qconvdir:
@@ -739,6 +776,8 @@ static long ipread(struct chan *ch, void *a, long n, int64_t off)
                        x = f->p[PROTO(ch->qid)];
                        c = x->conv[CONV(ch->qid)];
                        sofar = (*x->state) (c, buf, Statelen - 2);
+                       sofar += snprintf(buf + sofar, Statelen - 2 - sofar, "nonblock %s\n",
+                                         c->nonblock ? "on" : "off");
                        rv = readstr(offset, p, n, buf);
                        kfree(buf);
                        return rv;
@@ -754,7 +793,7 @@ static long ipread(struct chan *ch, void *a, long n, int64_t off)
                case Qstats:
                        x = f->p[PROTO(ch->qid)];
                        if (x->stats == NULL)
-                               error("stats not implemented");
+                               error(EFAIL, "stats not implemented");
                        buf = kzmalloc(Statelen, 0);
                        (*x->stats) (x, buf, Statelen);
                        rv = readstr(offset, p, n, buf);
@@ -913,7 +952,7 @@ static char *setladdrport(struct conv *c, char *str, int announcing)
        /* one process can get all connections */
        if (announcing && strcmp(p, "*") == 0) {
                if (!iseve())
-                       error(Eperm);
+                       error(EPERM, NULL);
                return setluniqueport(c, 0);
        }
 
@@ -993,14 +1032,14 @@ static void connectctlmsg(struct Proto *x, struct conv *c, struct cmdbuf *cb)
        char *p;
 
        if (c->state != 0)
-               error(Econinuse);
+               error(EBUSY, NULL);
        c->state = Connecting;
        c->cerr[0] = '\0';
        if (x->connect == NULL)
-               error("connect not supported");
+               error(EFAIL, "connect not supported");
        p = x->connect(c, cb->f, cb->nf);
        if (p != NULL)
-               error(p);
+               error(EFAIL, p);
 
        qunlock(&c->qlock);
        if (waserror()) {
@@ -1012,7 +1051,7 @@ static void connectctlmsg(struct Proto *x, struct conv *c, struct cmdbuf *cb)
        poperror();
 
        if (c->cerr[0] != '\0')
-               error(c->cerr);
+               error(EFAIL, c->cerr);
 }
 
 /*
@@ -1044,14 +1083,14 @@ static void announcectlmsg(struct Proto *x, struct conv *c, struct cmdbuf *cb)
        char *p;
 
        if (c->state != 0)
-               error(Econinuse);
+               error(EBUSY, NULL);
        c->state = Announcing;
        c->cerr[0] = '\0';
        if (x->announce == NULL)
-               error("announce not supported");
+               error(EFAIL, "announce not supported");
        p = x->announce(c, cb->f, cb->nf);
        if (p != NULL)
-               error(p);
+               error(EFAIL, p);
 
        qunlock(&c->qlock);
        if (waserror()) {
@@ -1063,7 +1102,7 @@ static void announcectlmsg(struct Proto *x, struct conv *c, struct cmdbuf *cb)
        poperror();
 
        if (c->cerr[0] != '\0')
-               error(c->cerr);
+               error(EFAIL, c->cerr);
 }
 
 /*
@@ -1079,6 +1118,13 @@ char *Fsstdbind(struct conv *c, char *argv[], int argc)
        }
 }
 
+void Fsconvnonblock(struct conv *cv, bool onoff)
+{
+       qnonblock(cv->wq, onoff);
+       qnonblock(cv->rq, onoff);
+       cv->nonblock = onoff;
+}
+
 static void bindctlmsg(struct Proto *x, struct conv *c, struct cmdbuf *cb)
 {
        char *p;
@@ -1088,7 +1134,22 @@ static void bindctlmsg(struct Proto *x, struct conv *c, struct cmdbuf *cb)
        else
                p = x->bind(c, cb->f, cb->nf);
        if (p != NULL)
-               error(p);
+               error(EFAIL, p);
+}
+
+static void nonblockctlmsg(struct conv *c, struct cmdbuf *cb)
+{
+       if (cb->nf < 2)
+               goto err;
+       if (!strcmp(cb->f[1], "on"))
+               Fsconvnonblock(c, TRUE);
+       else if (!strcmp(cb->f[1], "off"))
+               Fsconvnonblock(c, FALSE);
+       else
+               goto err;
+       return;
+err:
+       error(EINVAL, "nonblock [on|off]");
 }
 
 static void tosctlmsg(struct conv *c, struct cmdbuf *cb)
@@ -1123,14 +1184,10 @@ static long ipwrite(struct chan *ch, void *v, long n, int64_t off)
 
        switch (TYPE(ch->qid)) {
                default:
-                       error(Eperm);
+                       error(EPERM, NULL);
                case Qdata:
                        x = f->p[PROTO(ch->qid)];
                        c = x->conv[CONV(ch->qid)];
-
-                       if (c->wq == NULL)
-                               error(Eperm);
-
                        qwrite(c->wq, a, n);
                        break;
                case Qarp:
@@ -1154,13 +1211,15 @@ static long ipwrite(struct chan *ch, void *v, long n, int64_t off)
                                nexterror();
                        }
                        if (cb->nf < 1)
-                               error("short control request");
+                               error(EFAIL, "short control request");
                        if (strcmp(cb->f[0], "connect") == 0)
                                connectctlmsg(x, c, cb);
                        else if (strcmp(cb->f[0], "announce") == 0)
                                announcectlmsg(x, c, cb);
                        else if (strcmp(cb->f[0], "bind") == 0)
                                bindctlmsg(x, c, cb);
+                       else if (strcmp(cb->f[0], "nonblock") == 0)
+                               nonblockctlmsg(c, cb);
                        else if (strcmp(cb->f[0], "ttl") == 0)
                                ttlctlmsg(c, cb);
                        else if (strcmp(cb->f[0], "tos") == 0)
@@ -1169,32 +1228,32 @@ static long ipwrite(struct chan *ch, void *v, long n, int64_t off)
                                c->ignoreadvice = 1;
                        else if (strcmp(cb->f[0], "addmulti") == 0) {
                                if (cb->nf < 2)
-                                       error("addmulti needs interface address");
+                                       error(EFAIL, "addmulti needs interface address");
                                if (cb->nf == 2) {
                                        if (!ipismulticast(c->raddr))
-                                               error("addmulti for a non multicast address");
+                                               error(EFAIL, "addmulti for a non multicast address");
                                        parseip(ia, cb->f[1]);
                                        ipifcaddmulti(c, c->raddr, ia);
                                } else {
                                        parseip(ma, cb->f[2]);
                                        if (!ipismulticast(ma))
-                                               error("addmulti for a non multicast address");
+                                               error(EFAIL, "addmulti for a non multicast address");
                                        parseip(ia, cb->f[1]);
                                        ipifcaddmulti(c, ma, ia);
                                }
                        } else if (strcmp(cb->f[0], "remmulti") == 0) {
                                if (cb->nf < 2)
-                                       error("remmulti needs interface address");
+                                       error(EFAIL, "remmulti needs interface address");
                                if (!ipismulticast(c->raddr))
-                                       error("remmulti for a non multicast address");
+                                       error(EFAIL, "remmulti for a non multicast address");
                                parseip(ia, cb->f[1]);
                                ipifcremmulti(c, c->raddr, ia);
                        } else if (x->ctl != NULL) {
                                p = x->ctl(c, cb->f, cb->nf);
                                if (p != NULL)
-                                       error(p);
+                                       error(EFAIL, p);
                        } else
-                               error("unknown control request");
+                               error(EFAIL, "unknown control request");
                        qunlock(&c->qlock);
                        kfree(cb);
                        poperror();
@@ -1214,10 +1273,6 @@ static long ipbwrite(struct chan *ch, struct block *bp, uint32_t offset)
                        f = ipfs[ch->dev];
                        x = f->p[PROTO(ch->qid)];
                        c = x->conv[CONV(ch->qid)];
-
-                       if (c->wq == NULL)
-                               error(Eperm);
-
                        if (bp->next)
                                bp = concatblock(bp);
                        n = BLEN(bp);
@@ -1228,27 +1283,146 @@ static long ipbwrite(struct chan *ch, struct block *bp, uint32_t offset)
        }
 }
 
+static void ip_wake_cb(struct queue *q, void *data, int filter)
+{
+       struct conv *conv = (struct conv*)data;
+       struct fd_tap *tap_i;
+       /* For these two, we want to ignore events on the opposite end of the
+        * queues.  For instance, we want to know when the WQ is writable.  Our
+        * writes will actually make it readable - we don't want to trigger a tap
+        * for that.  However, qio doesn't know how/why we are using a queue, or
+        * even who the ends are (hence the callbacks) */
+       if ((filter & FDTAP_FILT_READABLE) && (q == conv->wq))
+               return;
+       if ((filter & FDTAP_FILT_WRITABLE) && (q == conv->rq))
+               return;
+       /* At this point, we have an event we want to send to our taps (if any).
+        * The lock protects list integrity and the existence of the tap.
+        *
+        * Previously, I thought of using the conv qlock.  That actually breaks, due
+        * to weird usages of the qlock (someone holds it for a long time, blocking
+        * the inbound wakeup from etherread4).
+        *
+        * I opted for a spinlock for a couple reasons:
+        * - fire_tap should not block.  ideally it'll be fast too (it's mostly a
+        * send_event).
+        * - our callers might not want to block.  A lot of network wakeups will
+        * come network processes (etherread4) or otherwise unrelated to this
+        * particular conversation.  I'd rather do something like fire off a KMSG
+        * than block those.
+        * - if fire_tap takes a while, holding the lock only slows down other
+        * events on this *same* conversation, or other tap registration.  not a
+        * huge deal. */
+       spin_lock(&conv->tap_lock);
+       SLIST_FOREACH(tap_i, &conv->data_taps, link)
+               fire_tap(tap_i, filter);
+       spin_unlock(&conv->tap_lock);
+}
+
+int iptapfd(struct chan *chan, struct fd_tap *tap, int cmd)
+{
+       struct conv *conv;
+       struct Proto *x;
+       struct Fs *f;
+       int ret;
+
+       #define DEVIP_LEGAL_DATA_TAPS (FDTAP_FILT_READABLE | FDTAP_FILT_WRITABLE | \
+                                      FDTAP_FILT_HANGUP)
+       #define DEVIP_LEGAL_LISTEN_TAPS (FDTAP_FILT_READABLE | FDTAP_FILT_HANGUP)
+
+       /* That's a lot of pointers to get to the conv! */
+       f = ipfs[chan->dev];
+       x = f->p[PROTO(chan->qid)];
+       conv = x->conv[CONV(chan->qid)];
+
+       switch (TYPE(chan->qid)) {
+               case Qdata:
+                       if (tap->filter & ~DEVIP_LEGAL_DATA_TAPS) {
+                               set_errno(ENOSYS);
+                               set_errstr("Unsupported #%s data tap, must be %p", devname(),
+                                          DEVIP_LEGAL_DATA_TAPS);
+                               return -1;
+                       }
+                       spin_lock(&conv->tap_lock);
+                       switch (cmd) {
+                               case (FDTAP_CMD_ADD):
+                                       if (SLIST_EMPTY(&conv->data_taps)) {
+                                               qio_set_wake_cb(conv->rq, ip_wake_cb, conv);
+                                               qio_set_wake_cb(conv->wq, ip_wake_cb, conv);
+                                       }
+                                       SLIST_INSERT_HEAD(&conv->data_taps, tap, link);
+                                       ret = 0;
+                                       break;
+                               case (FDTAP_CMD_REM):
+                                       SLIST_REMOVE(&conv->data_taps, tap, fd_tap, link);
+                                       if (SLIST_EMPTY(&conv->data_taps)) {
+                                               qio_set_wake_cb(conv->rq, 0, conv);
+                                               qio_set_wake_cb(conv->wq, 0, conv);
+                                       }
+                                       ret = 0;
+                                       break;
+                               default:
+                                       set_errno(ENOSYS);
+                                       set_errstr("Unsupported #%s data tap command %p",
+                                                  devname(), cmd);
+                                       ret = -1;
+                       }
+                       spin_unlock(&conv->tap_lock);
+                       return ret;
+               case Qlisten:
+                       if (tap->filter & ~DEVIP_LEGAL_LISTEN_TAPS) {
+                               set_errno(ENOSYS);
+                               set_errstr("Unsupported #%s listen tap, must be %p", devname(),
+                                          DEVIP_LEGAL_LISTEN_TAPS);
+                               return -1;
+                       }
+                       spin_lock(&conv->tap_lock);
+                       switch (cmd) {
+                               case (FDTAP_CMD_ADD):
+                                       SLIST_INSERT_HEAD(&conv->listen_taps, tap, link);
+                                       ret = 0;
+                                       break;
+                               case (FDTAP_CMD_REM):
+                                       SLIST_REMOVE(&conv->listen_taps, tap, fd_tap, link);
+                                       ret = 0;
+                                       break;
+                               default:
+                                       set_errno(ENOSYS);
+                                       set_errstr("Unsupported #%s listen tap command %p",
+                                                  devname(), cmd);
+                                       ret = -1;
+                       }
+                       spin_unlock(&conv->tap_lock);
+                       return ret;
+               default:
+                       set_errno(ENOSYS);
+                       set_errstr("Can't tap #%s file type %d", devname(),
+                                  TYPE(chan->qid));
+                       return -1;
+       }
+}
+
 struct dev ipdevtab __devtab = {
-       'I',
-       "ip",
-
-       ipreset,
-       ipinit,
-       devshutdown,
-       ipattach,
-       ipwalk,
-       ipstat,
-       ipopen,
-       devcreate,
-       ipclose,
-       ipread,
-       ipbread,
-       ipwrite,
-       ipbwrite,
-       devremove,
-       ipwstat,
-       devpower,
-       ipchaninfo,
+       .name = "ip",
+
+       .reset = ipreset,
+       .init = ipinit,
+       .shutdown = devshutdown,
+       .attach = ipattach,
+       .walk = ipwalk,
+       .stat = ipstat,
+       .open = ipopen,
+       .create = devcreate,
+       .close = ipclose,
+       .read = ipread,
+       .bread = ipbread,
+       .write = ipwrite,
+       .bwrite = ipbwrite,
+       .remove = devremove,
+       .wstat = ipwstat,
+       .power = devpower,
+       .chaninfo = ipchaninfo,
+       .tapfd = iptapfd,
 };
 
 int Fsproto(struct Fs *f, struct Proto *p)
@@ -1303,11 +1477,14 @@ retry:
                if (c == NULL) {
                        c = kzmalloc(sizeof(struct conv), 0);
                        if (c == NULL)
-                               error(Enomem);
+                               error(ENOMEM, NULL);
                        qlock_init(&c->qlock);
                        qlock_init(&c->listenq);
                        rendez_init(&c->cr);
                        rendez_init(&c->listenr);
+                       SLIST_INIT(&c->data_taps);      /* already = 0; set to be futureproof */
+                       SLIST_INIT(&c->listen_taps);
+                       spinlock_init(&c->tap_lock);
                        qlock(&c->qlock);
                        c->p = p;
                        c->x = pp - p->conv;
@@ -1315,13 +1492,14 @@ retry:
                                c->ptcl = kzmalloc(p->ptclsize, 0);
                                if (c->ptcl == NULL) {
                                        kfree(c);
-                                       error(Enomem);
+                                       error(ENOMEM, NULL);
                                }
                        }
                        *pp = c;
                        p->ac++;
                        c->eq = qopen(1024, Qmsg, 0, 0);
                        (*p->create) (c);
+                       assert(c->rq && c->wq);
                        break;
                }
                if (canqlock(&c->qlock)) {
@@ -1354,6 +1532,7 @@ retry:
        c->restricted = 0;
        c->ttl = MAXTTL;
        c->tos = DFLTTOS;
+       c->nonblock = FALSE;
        qreopen(c->rq);
        qreopen(c->wq);
        qreopen(c->eq);
@@ -1395,6 +1574,17 @@ struct Proto *Fsrcvpcolx(struct Fs *f, uint8_t proto)
        return f->t2p[proto];
 }
 
+static void fire_listener_taps(struct conv *conv)
+{
+       struct fd_tap *tap_i;
+       if (SLIST_EMPTY(&conv->listen_taps))
+               return;
+       spin_lock(&conv->tap_lock);
+       SLIST_FOREACH(tap_i, &conv->listen_taps, link)
+               fire_tap(tap_i, FDTAP_FILT_READABLE);
+       spin_unlock(&conv->tap_lock);
+}
+
 /*
  *  called with protocol locked
  */
@@ -1432,6 +1622,7 @@ struct conv *Fsnewcall(struct conv *c, uint8_t * raddr, uint16_t rport,
        qunlock(&c->qlock);
 
        rendez_wakeup(&c->listenr);
+       fire_listener_taps(c);
 
        return nc;
 }
@@ -1439,9 +1630,9 @@ struct conv *Fsnewcall(struct conv *c, uint8_t * raddr, uint16_t rport,
 static long ndbwrite(struct Fs *f, char *a, uint32_t off, int n)
 {
        if (off > strlen(f->ndb))
-               error(Eio);
+               error(EIO, NULL);
        if (off + n >= sizeof(f->ndb) - 1)
-               error(Eio);
+               error(EIO, NULL);
        memmove(f->ndb + off, a, n);
        f->ndb[off + n] = 0;
        f->ndbvers++;