Added explicit errno reporting from error() API.
[akaros.git] / kern / src / net / devip.c
index 88f46c3..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,
@@ -79,7 +86,7 @@ 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, "#I%lu", c->dev);
+       snprintf(get_cur_genbuf(), GENBUF_SZ, "#%s%lu", devname(), c->dev);
        return founddevdir(c, q, get_cur_genbuf(), 0, network, 0555, dp);
 }
 
@@ -324,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;
 
@@ -362,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);
@@ -376,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];
 
@@ -384,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:
@@ -406,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:
@@ -429,11 +431,11 @@ 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 & CNONBLOCK)
+                       if (c->flag & O_NONBLOCK)
                                Fsconvnonblock(cv, TRUE);
                        mkqid(&c->qid, QID(p->x, cv->x, Qctl), 0, QTFILE);
                        break;
@@ -451,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++;
@@ -467,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);
@@ -490,7 +508,7 @@ 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()) {
@@ -500,10 +518,8 @@ static struct chan *ipopen(struct chan *c, int omode)
                                /* 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) {
-                                       set_errno(EAGAIN);
-                                       error("listen queue empty");
-                               }
+                               if (cv->nonblock && !cv->incall)
+                                       error(EAGAIN, "listen queue empty");
                                /* wait for a connect */
                                rendez_sleep(&cv->listenr, should_wake, cv);
 
@@ -515,9 +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));
-                                       /* TODO: If we want to support something like accept4(),
-                                        * where the new conversations are nonblocking right away,
-                                        * we can do so here. */
+                                       /* 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);
 
@@ -545,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:
@@ -559,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)
@@ -599,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;
@@ -697,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:
@@ -770,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);
@@ -929,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);
        }
 
@@ -1009,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()) {
@@ -1028,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);
 }
 
 /*
@@ -1060,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()) {
@@ -1079,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);
 }
 
 /*
@@ -1111,7 +1134,7 @@ 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)
@@ -1126,8 +1149,7 @@ static void nonblockctlmsg(struct conv *c, struct cmdbuf *cb)
                goto err;
        return;
 err:
-       set_errno(EINVAL);
-       error("nonblock [on|off]");
+       error(EINVAL, "nonblock [on|off]");
 }
 
 static void tosctlmsg(struct conv *c, struct cmdbuf *cb)
@@ -1162,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:
@@ -1193,7 +1211,7 @@ 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)
@@ -1210,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();
@@ -1255,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);
@@ -1269,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)
@@ -1344,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;
@@ -1356,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)) {
@@ -1437,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
  */
@@ -1474,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;
 }
@@ -1481,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++;