19 #include <sys/types.h>
20 #include <netinet/in.h>
21 #include <netinet/ip.h>
44 } __attribute__((__packed__));
47 #define IPv4(a,b,c,d) ((uint32_t)(((a) & 0xff) << 24) | \
48 (((b) & 0xff) << 16) | \
49 (((c) & 0xff) << 8) | \
53 #define IPV4_MAX_PKT_LEN 65535
56 #define IPV4_HDR_IHL_MASK (0x0f)
61 #define IPV4_IHL_MULTIPLIER (4)
64 #define IPV4_HDR_DF_SHIFT 14
65 #define IPV4_HDR_MF_SHIFT 13
66 #define IPV4_HDR_FO_SHIFT 3
68 #define IPV4_HDR_DF_FLAG (1 << IPV4_HDR_DF_SHIFT)
69 #define IPV4_HDR_MF_FLAG (1 << IPV4_HDR_MF_SHIFT)
71 #define IPV4_HDR_OFFSET_MASK ((1 << IPV4_HDR_MF_SHIFT) - 1)
73 #define IPV4_HDR_OFFSET_UNITS 8
78 #define IPV4_ANY ((uint32_t)0x00000000)
79 #define IPV4_LOOPBACK ((uint32_t)0x7f000001)
80 #define IPV4_BROADCAST ((uint32_t)0xe0000000)
81 #define IPV4_ALLHOSTS_GROUP ((uint32_t)0xe0000001)
82 #define IPV4_ALLRTRS_GROUP ((uint32_t)0xe0000002)
83 #define IPV4_MAX_LOCAL_GROUP ((uint32_t)0xe00000ff)
88 #define IPV4_MIN_MCAST IPv4(224, 0, 0, 0)
89 #define IPV4_MAX_MCAST IPv4(239, 255, 255, 255)
91 #define IS_IPV4_MCAST(x) \
92 ((x) >= IPV4_MIN_MCAST && (x) <= IPV4_MAX_MCAST)
95 #define IPV4_MIN_IHL (0x5)
96 #define IPV4_VHL_DEF (IPVERSION | IPV4_MIN_IHL)
111 static inline uint32_t
112 __rte_raw_cksum(
const void *buf,
size_t len, uint32_t sum)
115 uintptr_t ptr = (uintptr_t)buf;
116 typedef uint16_t __attribute__((__may_alias__)) u16_p;
117 const u16_p *u16_buf = (
const u16_p *)ptr;
119 while (len >= (
sizeof(*u16_buf) * 4)) {
124 len -=
sizeof(*u16_buf) * 4;
127 while (len >=
sizeof(*u16_buf)) {
129 len -=
sizeof(*u16_buf);
135 sum += *((
const uint8_t *)u16_buf);
149 static inline uint16_t
150 __rte_raw_cksum_reduce(uint32_t sum)
152 sum = ((sum & 0xffff0000) >> 16) + (sum & 0xffff);
153 sum = ((sum & 0xffff0000) >> 16) + (sum & 0xffff);
154 return (uint16_t)sum;
167 static inline uint16_t
172 sum = __rte_raw_cksum(buf, len, 0);
173 return __rte_raw_cksum_reduce(sum);
197 uint32_t seglen, done;
202 const char *, off), len);
211 for (seg = m; seg != NULL; seg = seg->
next) {
229 tmp = __rte_raw_cksum(buf, seglen, 0);
239 if (seglen > len - done)
243 *cksum = __rte_raw_cksum_reduce(sum);
257 static inline uint16_t
262 return (cksum == 0xffff) ? cksum : (uint16_t)~cksum;
283 static inline uint16_t
286 struct ipv4_psd_header {
294 psd_hdr.src_addr = ipv4_hdr->
src_addr;
295 psd_hdr.dst_addr = ipv4_hdr->
dst_addr;
303 -
sizeof(
struct ipv4_hdr)));
322 static inline uint16_t
326 uint32_t l3_len, l4_len;
329 if (l3_len <
sizeof(
struct ipv4_hdr))
332 l4_len = l3_len -
sizeof(
struct ipv4_hdr);
337 cksum = ((cksum & 0xffff0000) >> 16) + (cksum & 0xffff);
338 cksum = (~cksum) & 0xffff;
342 return (uint16_t)cksum;
355 } __attribute__((__packed__));
358 #define IPV6_HDR_FL_SHIFT 0
359 #define IPV6_HDR_TC_SHIFT 20
360 #define IPV6_HDR_FL_MASK ((1u << IPV6_HDR_TC_SHIFT) - 1)
361 #define IPV6_HDR_TC_MASK (0xff << IPV6_HDR_TC_SHIFT)
379 static inline uint16_t
388 psd_hdr.proto = (uint32_t)(ipv6_hdr->
proto << 24);
395 sum = __rte_raw_cksum(ipv6_hdr->
src_addr,
398 sum = __rte_raw_cksum(&psd_hdr,
sizeof(psd_hdr), sum);
399 return __rte_raw_cksum_reduce(sum);
415 static inline uint16_t
426 cksum = ((cksum & 0xffff0000) >> 16) + (cksum & 0xffff);
427 cksum = (~cksum) & 0xffff;
431 return (uint16_t)cksum;
static int rte_raw_cksum_mbuf(const struct rte_mbuf *m, uint32_t off, uint32_t len, uint16_t *cksum)
static uint16_t rte_ipv4_phdr_cksum(const struct ipv4_hdr *ipv4_hdr, uint64_t ol_flags)
static uint16_t rte_bswap16(uint16_t _x)
static uint16_t rte_ipv4_udptcp_cksum(const struct ipv4_hdr *ipv4_hdr, const void *l4_hdr)
static rte_be16_t rte_cpu_to_be_16(uint16_t x)
static uint16_t rte_ipv4_cksum(const struct ipv4_hdr *ipv4_hdr)
static uint16_t rte_ipv6_phdr_cksum(const struct ipv6_hdr *ipv6_hdr, uint64_t ol_flags)
#define rte_pktmbuf_pkt_len(m)
static uint16_t rte_raw_cksum(const void *buf, size_t len)
#define rte_pktmbuf_data_len(m)
#define rte_pktmbuf_mtod(m, t)
static uint16_t rte_be_to_cpu_16(rte_be16_t x)
#define rte_pktmbuf_mtod_offset(m, t, o)
static uint16_t rte_ipv6_udptcp_cksum(const struct ipv6_hdr *ipv6_hdr, const void *l4_hdr)