Jump stacks before unlocking semaphores
[akaros.git] / kern / drivers / dev / ether.c
index b774889..4f8e7b7 100644 (file)
@@ -1,4 +1,31 @@
-// INFERNO
+/* Copyright © 1994-1999 Lucent Technologies Inc.  All rights reserved.
+ * Portions Copyright © 1997-1999 Vita Nuova Limited
+ * Portions Copyright © 2000-2007 Vita Nuova Holdings Limited
+ *                                (www.vitanuova.com)
+ * Revisions Copyright © 2000-2007 Lucent Technologies Inc. and others
+ *
+ * Modified for the Akaros operating system:
+ * Copyright (c) 2013-2014 The Regents of the University of California
+ * Copyright (c) 2013-2015 Google Inc.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE. */
+
 #include <vfs.h>
 #include <kfs.h>
 #include <slab.h>
 #include <smp.h>
 #include <ip.h>
 
+struct dev etherdevtab;
+
+static char *devname(void)
+{
+       return etherdevtab.name;
+}
+
 enum {
        Type8021Q = 0x8100,                     /* value of type field for 802.1[pQ] tags */
 };
@@ -37,15 +71,15 @@ struct chan *etherattach(char *spec)
                /* somebody interpret this for me. */
                if (((ctlrno == 0) && (p == spec)) ||
                        (ctlrno >= MaxEther) || ((*p) && (*p != '.')))
-                       error(Ebadarg);
+                       error(EINVAL, ERROR_FIXME);
                if (*p == '.') {        /* vlan */
                        vlanid = strtoul(p + 1, &p, 0);
                        if (vlanid <= 0 || vlanid > 0xFFF || *p)
-                               error(Ebadarg);
+                               error(EINVAL, ERROR_FIXME);
                }
        }
        if ((ether = etherxx[ctlrno]) == 0)
-               error(Enodev);
+               error(ENODEV, ERROR_FIXME);
        rlock(&ether->rwlock);
        if (waserror()) {
                runlock(&ether->rwlock);
@@ -53,16 +87,16 @@ struct chan *etherattach(char *spec)
        }
        if (vlanid) {
                if (ether->maxmtu < ETHERMAXTU + 4)
-                       error("interface cannot support 802.1 tags");
+                       error(EFAIL, "interface cannot support 802.1 tags");
                vlan = vlanalloc(ether, vlanid);
-               chan = devattach('l', spec);
+               chan = devattach(devname(), spec);
                chan->dev = ctlrno + (vlanid << 8);
                chan->aux = vlan;
                poperror();
                runlock(&ether->rwlock);
                return chan;
        }
-       chan = devattach('l', spec);
+       chan = devattach(devname(), spec);
        chan->dev = ctlrno;
        chan->aux = ether;
        if (ether->attach)
@@ -236,7 +270,7 @@ static void etherrtrace(struct netfile *f, struct etherpkt *pkt, int len)
                n = 58;
        else
                n = len;
-       bp = iallocb(68);
+       bp = block_alloc(68, MEM_ATOMIC);
        if (bp == NULL)
                return;
        memmove(bp->wp, pkt->d, n);
@@ -329,18 +363,23 @@ struct block *etheriq(struct ether *ether, struct block *bp, int fromwire)
                                /* Don't want to hear bridged packets */
                                if (f->bridge && !fromwire && !fromme)
                                        continue;
-                               if (!f->headersonly) {
-                                       if (fromwire && fx == 0)
-                                               fx = f;
-                                       else if ((xbp = iallocb(len))) {
-                                               memmove(xbp->wp, pkt, len);
-                                               xbp->wp += len;
-                                               if (qpass(f->in, xbp) < 0)
-                                                       ether->soverflows++;
-                                       } else
-                                               ether->soverflows++;
-                               } else
+                               if (f->headersonly) {
                                        etherrtrace(f, pkt, len);
+                                       continue;
+                               }
+                               if (fromwire && fx == 0) {
+                                       fx = f;
+                                       continue;
+                               }
+                               xbp = block_alloc(len, MEM_ATOMIC);
+                               if (xbp == 0) {
+                                       ether->soverflows++;
+                                       continue;
+                               }
+                               memmove(xbp->wp, pkt, len);
+                               xbp->wp += len;
+                               if (qpass(f->in, xbp) < 0)
+                                       ether->soverflows++;
                        }
        }
 
@@ -380,31 +419,31 @@ static int etheroq(struct ether *ether, struct block *bp)
        pkt = (struct etherpkt *)bp->rp;
        len = BLEN(bp);
        loopback = eaddrcmp(pkt->d, ether->ea) == 0;
-       if (loopback || eaddrcmp(pkt->d, ether->bcast) == 0
-               || ether->prom) {
+       if (loopback || eaddrcmp(pkt->d, ether->bcast) == 0 || ether->prom) {
                disable_irqsave(&irq_state);
                etheriq(ether, bp, 0);
                enable_irqsave(&irq_state);
+               if (loopback) {
+                       freeb(bp);
+                       return len;
+               }
        }
 
-       if (!loopback) {
-               if (ether->vlanid) {
-                       /* add tag */
-                       bp = padblock(bp, 2 + 2);
-                       memmove(bp->rp, bp->rp + 4, 2 * Eaddrlen);
-                       hnputs(bp->rp + 2 * Eaddrlen, Type8021Q);
-                       hnputs(bp->rp + 2 * Eaddrlen + 2, ether->vlanid & 0xFFF);       /* prio:3 0:1 vid:12 */
-                       ether = ether->ctlr;
-               }
+       if (ether->vlanid) {
+               /* add tag */
+               bp = padblock(bp, 2 + 2);
+               memmove(bp->rp, bp->rp + 4, 2 * Eaddrlen);
+               hnputs(bp->rp + 2 * Eaddrlen, Type8021Q);
+               hnputs(bp->rp + 2 * Eaddrlen + 2, ether->vlanid & 0xFFF);       /* prio:3 0:1 vid:12 */
+               ether = ether->ctlr;
+       }
 
-               if ((ether->feat & NETF_PADMIN) == 0 && BLEN(bp) < ether->minmtu)
-                       bp = adjustblock(bp, ether->minmtu);
+       if ((ether->feat & NETF_PADMIN) == 0 && BLEN(bp) < ether->minmtu)
+               bp = adjustblock(bp, ether->minmtu);
 
-               qbwrite(ether->oq, bp);
-               if (ether->transmit != NULL)
-                       ether->transmit(ether);
-       } else
-               freeb(bp);
+       qbwrite(ether->oq, bp);
+       if (ether->transmit != NULL)
+               ether->transmit(ether);
 
        return len;
 }
@@ -429,13 +468,17 @@ static long etherwrite(struct chan *chan, void *buf, long n, int64_t unused)
                if (l >= 0)
                        goto out;
                cb = parsecmd(buf, n);
+               if (cb->nf < 1) {
+                       kfree(cb);
+                       error(EFAIL, "short control request");
+               }
                if (strcmp(cb->f[0], "nonblocking") == 0) {
                        if (cb->nf <= 1)
                                onoff = 1;
                        else
                                onoff = atoi(cb->f[1]);
                        if (ether->oq != NULL)
-                               qnoblock(ether->oq, onoff);
+                               qdropoverflow(ether->oq, onoff);
                        kfree(cb);
                        goto out;
                }
@@ -444,12 +487,12 @@ static long etherwrite(struct chan *chan, void *buf, long n, int64_t unused)
                        l = ether->ctl(ether, buf, n);
                        goto out;
                }
-               error(Ebadctl);
+               error(EINVAL, ERROR_FIXME);
        }
 
        if (n > ether->maxmtu + ETHERHDRSIZE)
-               error(Etoobig);
-       bp = allocb(n);
+               error(E2BIG, ERROR_FIXME);
+       bp = block_alloc(n, MEM_WAIT);
        if (waserror()) {
                freeb(bp);
                nexterror();
@@ -491,7 +534,7 @@ static long etherbwrite(struct chan *chan, struct block *bp, uint32_t unused)
        }
        if (n > ether->maxmtu + ETHERHDRSIZE && (bp->flag & Btso) == 0) {
                freeb(bp);
-               error(Etoobig);
+               error(E2BIG, ERROR_FIXME);
        }
        n = etheroq(ether, bp);
        poperror();
@@ -532,7 +575,7 @@ static long vlanctl(struct ether *ether, void *buf, long n)
                return 0;
        }
        kfree(cb);
-       error(Ebadctl);
+       error(EINVAL, ERROR_FIXME);
        return -1;      /* not reached */
 }
 
@@ -560,13 +603,13 @@ static struct ether *vlanalloc(struct ether *ether, int id)
                        fid = i;
        }
        if (fid < 0)
-               error(Enoifc);
+               error(ENOENT, ERROR_FIXME);
        snprintf(name, sizeof(name), "ether%d.%d", ether->ctlrno, id);
        vlan = ether->vlans[fid];
        if (vlan == NULL) {
                vlan = kzmalloc(sizeof(struct ether), 1);
                if (vlan == NULL)
-                       error(Enovmem);
+                       error(ENOMEM, ERROR_FIXME);
                rwinit(&vlan->rwlock);
                qlock_init(&vlan->vlq);
                netifinit(vlan, name, Ntypes, ether->limit);
@@ -794,24 +837,23 @@ uint32_t ethercrc(uint8_t * p, int len)
 }
 
 struct dev etherdevtab __devtab = {
-       'l',
-       "ether",
-
-       etherreset,
-       devinit,
-       ethershutdown,
-       etherattach,
-       etherwalk,
-       etherstat,
-       etheropen,
-       devcreate,
-       etherclose,
-       etherread,
-       etherbread,
-       etherwrite,
-       etherbwrite,
-       devremove,
-       etherwstat,
-       etherpower,
-       devchaninfo,
+       .name = "ether",
+
+       .reset = etherreset,
+       .init = devinit,
+       .shutdown = ethershutdown,
+       .attach = etherattach,
+       .walk = etherwalk,
+       .stat = etherstat,
+       .open = etheropen,
+       .create = devcreate,
+       .close = etherclose,
+       .read = etherread,
+       .bread = etherbread,
+       .write = etherwrite,
+       .bwrite = etherbwrite,
+       .remove = devremove,
+       .wstat = etherwstat,
+       .power = etherpower,
+       .chaninfo = devchaninfo,
 };