net: tcp: Don't increment snd.nxt
[akaros.git] / kern / src / net / udp.c
index 787868f..da58226 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. */
+
 #define DEBUG
 #include <vfs.h>
 #include <kfs.h>
@@ -115,50 +142,57 @@ void udpkick(void *x, struct block *bp);
  */
 typedef struct Udpcb Udpcb;
 struct Udpcb {
-       qlock_t qlock;
        uint8_t headers;
 };
 
-static char *udpconnect(struct conv *c, char **argv, int argc)
+static void udpconnect(struct conv *c, char **argv, int argc)
 {
-       char *e;
        Udppriv *upriv;
 
        upriv = c->p->priv;
-       e = Fsstdconnect(c, argv, argc);
-       Fsconnected(c, e);
-       if (e != NULL)
-               return e;
+       Fsstdconnect(c, argv, argc);
+       Fsconnected(c, 0);
+
+       iphtadd(&upriv->ht, c);
+}
 
+static void udpbind(struct conv *c, char **argv, int argc)
+{
+       Udppriv *upriv;
+
+       upriv = c->p->priv;
+       Fsstdbind(c, argv, argc);
        iphtadd(&upriv->ht, c);
-       return NULL;
 }
 
 static int udpstate(struct conv *c, char *state, int n)
 {
-       return snprintf(state, n, "%s qin %d qout %d",
+       return snprintf(state, n, "%s qin %d qout %d\n",
                                        c->inuse ? "Open" : "Closed",
                                        c->rq ? qlen(c->rq) : 0, c->wq ? qlen(c->wq) : 0);
 }
 
-static char *udpannounce(struct conv *c, char **argv, int argc)
+static void udpannounce(struct conv *c, char **argv, int argc)
 {
-       char *e;
        Udppriv *upriv;
 
        upriv = c->p->priv;
-       e = Fsstdannounce(c, argv, argc);
-       if (e != NULL)
-               return e;
+       Fsstdannounce(c, argv, argc);
        Fsconnected(c, NULL);
        iphtadd(&upriv->ht, c);
+}
+
+static void udpbypass(struct conv *cv, char **argv, int argc)
+{
+       Udppriv *upriv = cv->p->priv;
 
-       return NULL;
+       Fsstdbypass(cv, argv, argc);
+       iphtadd(&upriv->ht, cv);
 }
 
 static void udpcreate(struct conv *c)
 {
-       c->rq = qopen(64 * 1024, Qmsg, 0, 0);
+       c->rq = qopen(128 * 1024, Qmsg, 0, 0);
        c->wq = qbypass(udpkick, c);
 }
 
@@ -294,8 +328,11 @@ void udpkick(void *x, struct block *bp)
                        uh4->udpcksum[0] = 0;
                        uh4->udpcksum[1] = 0;
                        hnputs(uh4->udpcksum,
-                                  ptclcsum(bp, UDP4_PHDR_OFF,
-                                                       dlen + UDP_UDPHDR_SZ + UDP4_PHDR_SZ));
+                                  ~ptclcsum(bp, UDP4_PHDR_OFF, UDP4_PHDR_SZ));
+                       bp->checksum_start = UDP4_IPHDR_SZ;
+                       bp->checksum_offset = uh4->udpcksum - uh4->udpsport;
+                       bp->transport_header_end = UDP4_IPHDR_SZ;
+                       bp->flag |= Budpck;
                        uh4->vihl = IP_VER4;
                        ipoput4(f, bp, 0, c->ttl, c->tos, rc);
                        break;
@@ -305,7 +342,7 @@ void udpkick(void *x, struct block *bp)
                        if (bp == NULL)
                                return;
 
-                       // using the v6 ip header to create pseudo header 
+                       // using the v6 ip header to create pseudo header
                        // first then reset it to the normal ip header
                        uh6 = (Udp6hdr *) (bp->rp);
                        memset(uh6, 0, 8);
@@ -333,6 +370,7 @@ void udpkick(void *x, struct block *bp)
                                   ptclcsum(bp, UDP6_PHDR_OFF,
                                                        dlen + UDP_UDPHDR_SZ + UDP6_PHDR_SZ));
                        memset(uh6, 0, 8);
+                       bp->transport_header_end = UDP6_IPHDR_SZ;
                        uh6->viclfl[0] = IP_VER6;
                        hnputs(uh6->len, ptcllen);
                        uh6->nextheader = IP_UDPPROTO;
@@ -368,7 +406,7 @@ void udpiput(struct Proto *udp, struct Ipifc *ifc, struct block *bp)
        version = ((uh4->vihl & 0xF0) == IP_VER6) ? V6 : V4;
 
        /*
-        * Put back pseudo header for checksum 
+        * Put back pseudo header for checksum
         * (remember old values for icmpnoconv())
         */
        switch (version) {
@@ -384,14 +422,15 @@ void udpiput(struct Proto *udp, struct Ipifc *ifc, struct block *bp)
                        lport = nhgets(uh4->udpdport);
                        rport = nhgets(uh4->udpsport);
 
-                       if (nhgets(uh4->udpcksum)) {
-                               if (ptclcsum(bp, UDP4_PHDR_OFF, len + UDP4_PHDR_SZ)) {
-                                       upriv->ustats.udpInErrors++;
-                                       netlog(f, Logudp, "udp: checksum error %I\n", raddr);
-                                       printd("udp: checksum error %I\n", raddr);
-                                       freeblist(bp);
-                                       return;
-                               }
+                       if (!(bp->flag & Budpck) &&
+                           (uh4->udpcksum[0] || uh4->udpcksum[1]) &&
+                           ptclcsum(bp, UDP4_PHDR_OFF, len + UDP4_PHDR_SZ)) {
+                               upriv->ustats.udpInErrors++;
+                               netlog(f, Logudp, "udp: checksum error %I\n",
+                                      raddr);
+                               printd("udp: checksum error %I\n", raddr);
+                               freeblist(bp);
+                               return;
                        }
                        uh4->Unused = ottl;
                        hnputs(uh4->udpplen, olen);
@@ -426,13 +465,10 @@ void udpiput(struct Proto *udp, struct Ipifc *ifc, struct block *bp)
                        return; /* to avoid a warning */
        }
 
-       qlock(&udp->qlock);
-
        c = iphtlook(&upriv->ht, raddr, rport, laddr, lport);
        if (c == NULL) {
                /* no converstation found */
                upriv->ustats.udpNoPorts++;
-               qunlock(&udp->qlock);
                netlog(f, Logudp, "udp: no conv %I!%d -> %I!%d\n", raddr, rport,
                           laddr, lport);
 
@@ -450,6 +486,12 @@ void udpiput(struct Proto *udp, struct Ipifc *ifc, struct block *bp)
                freeblist(bp);
                return;
        }
+
+       if (c->state == Bypass) {
+               bypass_or_drop(c, bp);
+               return;
+       }
+
        ucb = (Udpcb *) c->ptcl;
 
        if (c->state == Announced) {
@@ -467,9 +509,10 @@ void udpiput(struct Proto *udp, struct Ipifc *ifc, struct block *bp)
                                                panic("udpiput3: version %d", version);
                                }
                        }
+                       qlock(&udp->qlock);
                        c = Fsnewcall(c, raddr, rport, laddr, lport, version);
+                       qunlock(&udp->qlock);
                        if (c == NULL) {
-                               qunlock(&udp->qlock);
                                freeblist(bp);
                                return;
                        }
@@ -479,7 +522,6 @@ void udpiput(struct Proto *udp, struct Ipifc *ifc, struct block *bp)
        }
 
        qlock(&c->qlock);
-       qunlock(&udp->qlock);
 
        /*
         * Trim the packet down to data size
@@ -552,21 +594,16 @@ void udpiput(struct Proto *udp, struct Ipifc *ifc, struct block *bp)
 
 }
 
-char *udpctl(struct conv *c, char **f, int n)
+static void udpctl(struct conv *c, char **f, int n)
 {
-       Udpcb *ucb;
-
-       ucb = (Udpcb *) c->ptcl;
-       if (n == 1) {
-               if (strcmp(f[0], "oldheaders") == 0) {
-                       ucb->headers = 6;
-                       return NULL;
-               } else if (strcmp(f[0], "headers") == 0) {
-                       ucb->headers = 7;
-                       return NULL;
-               }
-       }
-       return "unknown control request";
+       Udpcb *ucb = (Udpcb*)c->ptcl;
+
+       if ((n == 1) && strcmp(f[0], "oldheaders") == 0)
+               ucb->headers = 6;
+       else if ((n == 1) && strcmp(f[0], "headers") == 0)
+               ucb->headers = 7;
+       else
+               error(EINVAL, "unknown command to %s", __func__);
 }
 
 void udpadvise(struct Proto *udp, struct block *bp, char *msg)
@@ -601,7 +638,6 @@ void udpadvise(struct Proto *udp, struct block *bp, char *msg)
        }
 
        /* Look for a connection */
-       qlock(&udp->qlock);
        for (p = udp->conv; *p; p++) {
                s = *p;
                if (s->rport == pdest)
@@ -611,7 +647,6 @@ void udpadvise(struct Proto *udp, struct block *bp, char *msg)
                                                if (s->ignoreadvice)
                                                        break;
                                                qlock(&s->qlock);
-                                               qunlock(&udp->qlock);
                                                qhangup(s->rq, msg);
                                                qhangup(s->wq, msg);
                                                qunlock(&s->qlock);
@@ -619,27 +654,22 @@ void udpadvise(struct Proto *udp, struct block *bp, char *msg)
                                                return;
                                        }
        }
-       qunlock(&udp->qlock);
        freeblist(bp);
 }
 
 int udpstats(struct Proto *udp, char *buf, int len)
 {
        Udppriv *upriv;
+       char *p, *e;
 
        upriv = udp->priv;
-       return snprintf(buf, len,
-                                       "InDatagrams: %lu\nNoPorts: %lu\nInErrors: %lu\nOutDatagrams: %lu\n",
-                                       upriv->ustats.udpInDatagrams, upriv->ustats.udpNoPorts,
-                                       upriv->ustats.udpInErrors, upriv->ustats.udpOutDatagrams);
-}
-
-void udpnewconv(struct Proto *udp, struct conv *conv)
-{
-       /* Fsprotoclone alloc'd our priv struct and attached it to conv already.
-        * Now we need to init it */
-       struct Udpcb *ucb = (struct Udpcb *)conv->ptcl;
-       qlock_init(&ucb->qlock);
+       p = buf;
+       e = p + len;
+       p = seprintf(p, e, "InDatagrams: %u\n", upriv->ustats.udpInDatagrams);
+       p = seprintf(p, e, "NoPorts: %u\n", upriv->ustats.udpNoPorts);
+       p = seprintf(p, e, "InErrors: %u\n", upriv->ustats.udpInErrors);
+       p = seprintf(p, e, "OutDatagrams: %u\n", upriv->ustats.udpOutDatagrams);
+       return p - buf;
 }
 
 void udpinit(struct Fs *fs)
@@ -650,7 +680,9 @@ void udpinit(struct Fs *fs)
        udp->priv = kzmalloc(sizeof(Udppriv), 0);
        udp->name = "udp";
        udp->connect = udpconnect;
+       udp->bind = udpbind;
        udp->announce = udpannounce;
+       udp->bypass = udpbypass;
        udp->ctl = udpctl;
        udp->state = udpstate;
        udp->create = udpcreate;
@@ -659,8 +691,7 @@ void udpinit(struct Fs *fs)
        udp->advise = udpadvise;
        udp->stats = udpstats;
        udp->ipproto = IP_UDPPROTO;
-       udp->nc = Nchans;
-       udp->newconv = udpnewconv;
+       udp->nc = 4096;
        udp->ptclsize = sizeof(Udpcb);
 
        Fsproto(fs, udp);