mbuf: generic support for TCP segmentation offload
[dpdk.git] / lib / librte_net / rte_ip.h
index 331a01d..20c3ae1 100644 (file)
@@ -1,13 +1,14 @@
 /*-
  *   BSD LICENSE
- * 
+ *
  *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
+ *   Copyright 2014 6WIND S.A.
  *   All rights reserved.
- * 
+ *
  *   Redistribution and use in source and binary forms, with or without
  *   modification, are permitted provided that the following conditions
  *   are met:
- * 
+ *
  *     * Redistributions of source code must retain the above copyright
  *       notice, this list of conditions and the following disclaimer.
  *     * Redistributions in binary form must reproduce the above copyright
@@ -17,7 +18,7 @@
  *     * Neither the name of Intel Corporation nor the names of its
  *       contributors may be used to endorse or promote products derived
  *       from this software without specific prior written permission.
- * 
+ *
  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 
 #include <stdint.h>
 
+#include <rte_memcpy.h>
+#include <rte_byteorder.h>
+#include <rte_mbuf.h>
+
 #ifdef __cplusplus
 extern "C" {
 #endif
@@ -246,6 +251,139 @@ struct ipv4_hdr {
 #define IS_IPV4_MCAST(x) \
        ((x) >= IPV4_MIN_MCAST && (x) <= IPV4_MAX_MCAST) /**< check if IPv4 address is multicast */
 
+/**
+ * Process the non-complemented checksum of a buffer.
+ *
+ * @param buf
+ *   Pointer to the buffer.
+ * @param len
+ *   Length of the buffer.
+ * @return
+ *   The non-complemented checksum.
+ */
+static inline uint16_t
+rte_raw_cksum(const char *buf, size_t len)
+{
+       const uint16_t *u16 = (const uint16_t *)buf;
+       uint32_t sum = 0;
+
+       while (len >= (sizeof(*u16) * 4)) {
+               sum += u16[0];
+               sum += u16[1];
+               sum += u16[2];
+               sum += u16[3];
+               len -= sizeof(*u16) * 4;
+               u16 += 4;
+       }
+       while (len >= sizeof(*u16)) {
+               sum += *u16;
+               len -= sizeof(*u16);
+               u16 += 1;
+       }
+
+       /* if length is in odd bytes */
+       if (len == 1)
+               sum += *((const uint8_t *)u16);
+
+       sum = ((sum & 0xffff0000) >> 16) + (sum & 0xffff);
+       sum = ((sum & 0xffff0000) >> 16) + (sum & 0xffff);
+       return (uint16_t)sum;
+}
+
+/**
+ * Process the IPv4 checksum of an IPv4 header.
+ *
+ * The checksum field must be set to 0 by the caller.
+ *
+ * @param ipv4_hdr
+ *   The pointer to the contiguous IPv4 header.
+ * @return
+ *   The complemented checksum to set in the IP packet.
+ */
+static inline uint16_t
+rte_ipv4_cksum(const struct ipv4_hdr *ipv4_hdr)
+{
+       uint16_t cksum;
+       cksum = rte_raw_cksum((const char *)ipv4_hdr, sizeof(struct ipv4_hdr));
+       return ((cksum == 0xffff) ? cksum : ~cksum);
+}
+
+/**
+ * Process the pseudo-header checksum of an IPv4 header.
+ *
+ * The checksum field must be set to 0 by the caller.
+ *
+ * Depending on the ol_flags, the pseudo-header checksum expected by the
+ * drivers is not the same. For instance, when TSO is enabled, the IP
+ * payload length must not be included in the packet.
+ *
+ * When ol_flags is 0, it computes the standard pseudo-header checksum.
+ *
+ * @param ipv4_hdr
+ *   The pointer to the contiguous IPv4 header.
+ * @param ol_flags
+ *   The ol_flags of the associated mbuf.
+ * @return
+ *   The non-complemented checksum to set in the L4 header.
+ */
+static inline uint16_t
+rte_ipv4_phdr_cksum(const struct ipv4_hdr *ipv4_hdr, uint64_t ol_flags)
+{
+       struct ipv4_psd_header {
+               uint32_t src_addr; /* IP address of source host. */
+               uint32_t dst_addr; /* IP address of destination host. */
+               uint8_t  zero;     /* zero. */
+               uint8_t  proto;    /* L4 protocol type. */
+               uint16_t len;      /* L4 length. */
+       } psd_hdr;
+
+       psd_hdr.src_addr = ipv4_hdr->src_addr;
+       psd_hdr.dst_addr = ipv4_hdr->dst_addr;
+       psd_hdr.zero = 0;
+       psd_hdr.proto = ipv4_hdr->next_proto_id;
+       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 ipv4_hdr)));
+       }
+       return rte_raw_cksum((const char *)&psd_hdr, sizeof(psd_hdr));
+}
+
+/**
+ * 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.
+ *
+ * @param ipv4_hdr
+ *   The pointer to the contiguous IPv4 header.
+ * @param l4_hdr
+ *   The pointer to the beginning of the L4 header.
+ * @return
+ *   The complemented checksum to set in the IP packet.
+ */
+static inline uint16_t
+rte_ipv4_udptcp_cksum(const struct ipv4_hdr *ipv4_hdr, const void *l4_hdr)
+{
+       uint32_t cksum;
+       uint32_t l4_len;
+
+       l4_len = rte_be_to_cpu_16(ipv4_hdr->total_length) -
+               sizeof(struct ipv4_hdr);
+
+       cksum = rte_raw_cksum(l4_hdr, l4_len);
+       cksum += rte_ipv4_phdr_cksum(ipv4_hdr, 0);
+
+       cksum = ((cksum & 0xffff0000) >> 16) + (cksum & 0xffff);
+       cksum = (~cksum) & 0xffff;
+       if (cksum == 0)
+               cksum = 0xffff;
+
+       return cksum;
+}
+
 /**
  * IPv6 Header
  */
@@ -258,6 +396,76 @@ struct ipv6_hdr {
        uint8_t  dst_addr[16]; /**< IP address of destination host(s). */
 } __attribute__((__packed__));
 
+/**
+ * Process the pseudo-header checksum of an IPv6 header.
+ *
+ * Depending on the ol_flags, the pseudo-header checksum expected by the
+ * drivers is not the same. For instance, when TSO is enabled, the IPv6
+ * payload length must not be included in the packet.
+ *
+ * When ol_flags is 0, it computes the standard pseudo-header checksum.
+ *
+ * @param ipv6_hdr
+ *   The pointer to the contiguous IPv6 header.
+ * @param ol_flags
+ *   The ol_flags of the associated mbuf.
+ * @return
+ *   The non-complemented checksum to set in the L4 header.
+ */
+static inline uint16_t
+rte_ipv6_phdr_cksum(const struct ipv6_hdr *ipv6_hdr, uint64_t ol_flags)
+{
+       struct ipv6_psd_header {
+               uint8_t src_addr[16]; /* IP address of source host. */
+               uint8_t dst_addr[16]; /* IP address of destination host. */
+               uint32_t len;         /* L4 length. */
+               uint32_t proto;       /* L4 protocol - top 3 bytes must be zero */
+       } psd_hdr;
+
+       rte_memcpy(&psd_hdr.src_addr, ipv6_hdr->src_addr,
+               sizeof(ipv6_hdr->src_addr) + sizeof(ipv6_hdr->dst_addr));
+       psd_hdr.proto = (ipv6_hdr->proto << 24);
+       if (ol_flags & PKT_TX_TCP_SEG) {
+               psd_hdr.len = 0;
+       } else {
+               psd_hdr.len = ipv6_hdr->payload_len;
+       }
+
+       return rte_raw_cksum((const char *)&psd_hdr, sizeof(psd_hdr));
+}
+
+/**
+ * Process the IPv6 UDP or TCP checksum.
+ *
+ * The IPv4 header should not contains options. The layer 4 checksum
+ * must be set to 0 in the packet by the caller.
+ *
+ * @param ipv6_hdr
+ *   The pointer to the contiguous IPv6 header.
+ * @param l4_hdr
+ *   The pointer to the beginning of the L4 header.
+ * @return
+ *   The complemented checksum to set in the IP packet.
+ */
+static inline uint16_t
+rte_ipv6_udptcp_cksum(const struct ipv6_hdr *ipv6_hdr, const void *l4_hdr)
+{
+       uint32_t cksum;
+       uint32_t l4_len;
+
+       l4_len = rte_be_to_cpu_16(ipv6_hdr->payload_len);
+
+       cksum = rte_raw_cksum(l4_hdr, l4_len);
+       cksum += rte_ipv6_phdr_cksum(ipv6_hdr, 0);
+
+       cksum = ((cksum & 0xffff0000) >> 16) + (cksum & 0xffff);
+       cksum = (~cksum) & 0xffff;
+       if (cksum == 0)
+               cksum = 0xffff;
+
+       return cksum;
+}
+
 #ifdef __cplusplus
 }
 #endif