examples/cmdline: build on Windows
[dpdk.git] / lib / librte_net / rte_ip.h
index a9ffc33..8382d0f 100644 (file)
@@ -139,8 +139,11 @@ __rte_raw_cksum(const void *buf, size_t len, uint32_t sum)
        }
 
        /* if length is in odd bytes */
-       if (len == 1)
-               sum += *((const uint8_t *)u16_buf);
+       if (len == 1) {
+               uint16_t left = 0;
+               *(uint8_t *)&left = *(const uint8_t *)u16_buf;
+               sum += left;
+       }
 
        return sum;
 }
@@ -222,6 +225,9 @@ rte_raw_cksum_mbuf(const struct rte_mbuf *m, uint32_t off, uint32_t len,
                        break;
                off -= seglen;
        }
+       RTE_ASSERT(seg != NULL);
+       if (seg == NULL)
+               return -1;
        seglen -= off;
        buf = rte_pktmbuf_mtod_offset(seg, const char *, off);
        if (seglen >= len) {
@@ -266,7 +272,7 @@ static inline uint16_t
 rte_ipv4_cksum(const struct rte_ipv4_hdr *ipv4_hdr)
 {
        uint16_t cksum;
-       cksum = rte_raw_cksum(ipv4_hdr, sizeof(struct rte_ipv4_hdr));
+       cksum = rte_raw_cksum(ipv4_hdr, (ipv4_hdr->version_ihl & 0xf) * 4);
        return (uint16_t)~cksum;
 }
 
@@ -299,6 +305,9 @@ rte_ipv4_phdr_cksum(const struct rte_ipv4_hdr *ipv4_hdr, uint64_t ol_flags)
                uint16_t len;      /* L4 length. */
        } psd_hdr;
 
+       uint32_t l3_len;
+       uint8_t ip_hdr_len;
+
        psd_hdr.src_addr = ipv4_hdr->src_addr;
        psd_hdr.dst_addr = ipv4_hdr->dst_addr;
        psd_hdr.zero = 0;
@@ -306,9 +315,9 @@ rte_ipv4_phdr_cksum(const struct rte_ipv4_hdr *ipv4_hdr, uint64_t ol_flags)
        if (ol_flags & PKT_TX_TCP_SEG) {
                psd_hdr.len = 0;
        } else {
-               psd_hdr.len = rte_cpu_to_be_16(
-                       (uint16_t)(rte_be_to_cpu_16(ipv4_hdr->total_length)
-                               - sizeof(struct rte_ipv4_hdr)));
+               l3_len = rte_be_to_cpu_16(ipv4_hdr->total_length);
+               ip_hdr_len = (ipv4_hdr->version_ihl & 0xf) * 4;
+               psd_hdr.len = rte_cpu_to_be_16((uint16_t)(l3_len - ip_hdr_len));
        }
        return rte_raw_cksum(&psd_hdr, sizeof(psd_hdr));
 }
@@ -316,8 +325,8 @@ rte_ipv4_phdr_cksum(const struct rte_ipv4_hdr *ipv4_hdr, uint64_t ol_flags)
 /**
  * Process the IPv4 UDP or TCP checksum.
  *
- * The IPv4 header should not contains options. The IP and layer 4
- * checksum must be set to 0 in the packet by the caller.
+ * The IP and layer 4 checksum must be set to 0 in the packet by
+ * the caller.
  *
  * @param ipv4_hdr
  *   The pointer to the contiguous IPv4 header.
@@ -331,12 +340,14 @@ rte_ipv4_udptcp_cksum(const struct rte_ipv4_hdr *ipv4_hdr, const void *l4_hdr)
 {
        uint32_t cksum;
        uint32_t l3_len, l4_len;
+       uint8_t ip_hdr_len;
 
+       ip_hdr_len = (ipv4_hdr->version_ihl & 0xf) * 4;
        l3_len = rte_be_to_cpu_16(ipv4_hdr->total_length);
-       if (l3_len < sizeof(struct rte_ipv4_hdr))
+       if (l3_len < ip_hdr_len)
                return 0;
 
-       l4_len = l3_len - sizeof(struct rte_ipv4_hdr);
+       l4_len = l3_len - ip_hdr_len;
 
        cksum = rte_raw_cksum(l4_hdr, l4_len);
        cksum += rte_ipv4_phdr_cksum(ipv4_hdr, 0);