net: Add network_offset to blocks
authorBarret Rhoden <brho@cs.berkeley.edu>
Wed, 8 Nov 2017 16:44:18 +0000 (11:44 -0500)
committerBarret Rhoden <brho@cs.berkeley.edu>
Thu, 16 Nov 2017 15:46:56 +0000 (10:46 -0500)
Similar to transport_offset, this is the offset from bp->rp to the
beginning of the network layer header, e.g. IP.

Signed-off-by: Barret Rhoden <brho@cs.berkeley.edu>
kern/include/net/ip.h
kern/include/net/tcp.h
kern/include/ns.h
kern/src/net/ip.c
kern/src/net/tcp.c
kern/src/net/udp.c
kern/src/ns/allocb.c
kern/src/ns/qio.c

index 7acadfa..4e376c6 100644 (file)
@@ -732,11 +732,25 @@ extern void (*igmpreportfn) (struct Ipifc * unused_ipifc,
 
 /* from RFC 2460 */
 
-typedef struct Ip6hdr Ip6hdr;
+typedef struct Ip4hdr Ip4hdr;
+typedef struct ip6hdr Ip6hdr;
 typedef struct Opthdr Opthdr;
 typedef struct Routinghdr Routinghdr;
 typedef struct Fraghdr6 Fraghdr6;
 
+struct Ip4hdr {
+       uint8_t vihl;                           /* Version and header length */
+       uint8_t tos;                            /* Type of service */
+       uint8_t length[2];                      /* packet length */
+       uint8_t id[2];                          /* ip->identification */
+       uint8_t frag[2];                        /* Fragment information */
+       uint8_t ttl;                            /* Time to live */
+       uint8_t proto;                          /* Protocol */
+       uint8_t cksum[2];                       /* Header checksum */
+       uint8_t src[4];                         /* IP source */
+       uint8_t dst[4];                         /* IP destination */
+};
+
 struct ip6hdr {
        uint8_t vcf[4];                         // version:4, traffic class:8, flow label:20
        uint8_t ploadlen[2];            // payload length: packet length - 40
@@ -866,6 +880,23 @@ enum {
 
 };
 
+static inline struct Ip4hdr *ipv4_hdr(struct block *bp)
+{
+       return (struct Ip4hdr*)(bp->rp + bp->network_offset);
+}
+
+static inline struct ip6hdr *ipv6_hdr(struct block *bp)
+{
+       return (struct ip6hdr*)(bp->rp + bp->network_offset);
+}
+
+static inline unsigned int ip_version(struct block *bp)
+{
+       struct Ip4hdr *hdr = ipv4_hdr(bp);
+
+       return hdr->vihl >> 4;
+}
+
 extern void ipv62smcast(uint8_t *, uint8_t *);
 extern void icmpns(struct Fs *f, uint8_t * src, int suni, uint8_t * targ,
                                   int tuni, uint8_t * mac);
index 8675cef..4530879 100644 (file)
@@ -417,6 +417,6 @@ static inline size_t tcp_hdrlen(struct block *bp)
        struct tcphdr *hdr = tcp_hdr(bp);
        uint8_t data_offset;
 
-       data_offset = hdr->tcpflag[0] & 0x0f;
+       data_offset = hdr->tcpflag[0] >> 4;
        return data_offset * 4;
 }
index d76a332..af6367f 100644 (file)
@@ -387,6 +387,7 @@ struct block {
        uint16_t checksum_start;                /* off from start of block to start csum */
        uint16_t checksum_offset;               /* off from checksum_start to store csum */
        uint16_t mss;               /* TCP MSS for TSO */
+       uint16_t network_offset;        /* offset from start */
        uint16_t transport_offset;      /* offset from start */
        /* might want something to track the next free extra_data slot */
        size_t extra_len;
index 71c0bf0..37842e9 100644 (file)
@@ -40,7 +40,6 @@
 #include <smp.h>
 #include <net/ip.h>
 
-typedef struct Ip4hdr Ip4hdr;
 typedef struct IP IP;
 typedef struct Fragment4 Fragment4;
 typedef struct Fragment6 Fragment6;
@@ -59,19 +58,6 @@ enum {
 #define BLKIPVER(xp)   (((struct Ip4hdr*)((xp)->rp))->vihl&0xF0)
 #define NEXT_ID(x) (__sync_add_and_fetch(&(x), 1))
 
-struct Ip4hdr {
-       uint8_t vihl;                           /* Version and header length */
-       uint8_t tos;                            /* Type of service */
-       uint8_t length[2];                      /* packet length */
-       uint8_t id[2];                          /* ip->identification */
-       uint8_t frag[2];                        /* Fragment information */
-       uint8_t ttl;                            /* Time to live */
-       uint8_t proto;                          /* Protocol */
-       uint8_t cksum[2];                       /* Header checksum */
-       uint8_t src[4];                         /* IP source */
-       uint8_t dst[4];                         /* IP destination */
-};
-
 /* MIB II counters */
 enum {
        Forwarding,
index 203b13b..208b1ce 100644 (file)
@@ -777,6 +777,7 @@ static struct block *htontcp6(Tcp *tcph, struct block *data, Tcp6hdr *ph,
        if (data == NULL)
                return NULL;
        /* relative to the block start (bp->rp).  Note TCP structs include IP. */
+       data->network_offset = 0;
        data->transport_offset = offsetof(Tcp6hdr, tcpsport);
 
        /* copy in pseudo ip header plus port numbers */
@@ -827,6 +828,7 @@ static struct block *htontcp4(Tcp *tcph, struct block *data, Tcp4hdr *ph,
        if (data == NULL)
                return NULL;
        /* relative to the block start (bp->rp).  Note TCP structs include IP. */
+       data->network_offset = 0;
        data->transport_offset = offsetof(Tcp4hdr, tcpsport);
 
        /* copy in pseudo ip header plus port numbers */
index 62105c4..146a6ce 100644 (file)
@@ -331,6 +331,7 @@ void udpkick(void *x, struct block *bp)
                                   ~ptclcsum(bp, UDP4_PHDR_OFF, UDP4_PHDR_SZ));
                        bp->checksum_start = UDP4_IPHDR_SZ;
                        bp->checksum_offset = uh4->udpcksum - uh4->udpsport;
+                       bp->network_offset = 0;
                        bp->transport_offset = offsetof(Udp4hdr, udpsport);
                        bp->flag |= Budpck;
                        uh4->vihl = IP_VER4;
@@ -370,6 +371,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->network_offset = 0;
                        bp->transport_offset = offsetof(Udp6hdr, udpsport);
                        uh6->viclfl[0] = IP_VER6;
                        hnputs(uh6->len, ptcllen);
index 6a081af..092c529 100644 (file)
@@ -74,6 +74,9 @@ struct block *block_alloc(size_t size, int mem_flags)
        b->extra_len = 0;
        b->nr_extra_bufs = 0;
        b->extra_data = 0;
+       b->mss = 0;
+       b->network_offset = 0;
+       b->transport_offset = 0;
 
        addr = (uintptr_t) b;
        addr = ROUNDUP(addr + sizeof(struct block), BLOCKALIGN);
index 41105f2..c25b47e 100644 (file)
@@ -135,12 +135,14 @@ struct block *padblock(struct block *bp, int size)
        uint16_t checksum_start = bp->checksum_start;
        uint16_t checksum_offset = bp->checksum_offset;
        uint16_t mss = bp->mss;
+       uint16_t network_offset = bp->network_offset;
        uint16_t transport_offset = bp->transport_offset;
 
        QDEBUG checkb(bp, "padblock 1");
        if (size >= 0) {
                if (bp->rp - bp->base >= size) {
                        bp->checksum_start += size;
+                       bp->network_offset += size;
                        bp->transport_offset += size;
                        bp->rp -= size;
                        return bp;
@@ -181,6 +183,7 @@ struct block *padblock(struct block *bp, int size)
                nbp->checksum_start = checksum_start;
                nbp->checksum_offset = checksum_offset;
                nbp->mss = mss;
+               nbp->network_offset = network_offset;
                nbp->transport_offset = transport_offset;
        }
        QDEBUG checkb(nbp, "padblock 1");
@@ -272,6 +275,7 @@ struct block *copyblock(struct block *bp, int mem_flags)
                newb->checksum_start = bp->checksum_start;
                newb->checksum_offset = bp->checksum_offset;
                newb->mss = bp->mss;
+               newb->network_offset = bp->network_offset;
                newb->transport_offset = bp->transport_offset;
        }
        copyblockcnt++;