19 #include <netinet/in.h>
42 } __attribute__((__packed__));
45 #define IPv4(a,b,c,d) ((uint32_t)(((a) & 0xff) << 24) | \
46 (((b) & 0xff) << 16) | \
47 (((c) & 0xff) << 8) | \
51 #define IPV4_MAX_PKT_LEN 65535
54 #define IPV4_HDR_IHL_MASK (0x0f)
59 #define IPV4_IHL_MULTIPLIER (4)
62 #define IPV4_HDR_DF_SHIFT 14
63 #define IPV4_HDR_MF_SHIFT 13
64 #define IPV4_HDR_FO_SHIFT 3
66 #define IPV4_HDR_DF_FLAG (1 << IPV4_HDR_DF_SHIFT)
67 #define IPV4_HDR_MF_FLAG (1 << IPV4_HDR_MF_SHIFT)
69 #define IPV4_HDR_OFFSET_MASK ((1 << IPV4_HDR_MF_SHIFT) - 1)
71 #define IPV4_HDR_OFFSET_UNITS 8
76 #define IPV4_ANY ((uint32_t)0x00000000)
77 #define IPV4_LOOPBACK ((uint32_t)0x7f000001)
78 #define IPV4_BROADCAST ((uint32_t)0xe0000000)
79 #define IPV4_ALLHOSTS_GROUP ((uint32_t)0xe0000001)
80 #define IPV4_ALLRTRS_GROUP ((uint32_t)0xe0000002)
81 #define IPV4_MAX_LOCAL_GROUP ((uint32_t)0xe00000ff)
86 #define IPV4_MIN_MCAST IPv4(224, 0, 0, 0)
87 #define IPV4_MAX_MCAST IPv4(239, 255, 255, 255)
89 #define IS_IPV4_MCAST(x) \
90 ((x) >= IPV4_MIN_MCAST && (x) <= IPV4_MAX_MCAST)
105 static inline uint32_t
106 __rte_raw_cksum(
const void *buf,
size_t len, uint32_t sum)
109 uintptr_t ptr = (uintptr_t)buf;
110 typedef uint16_t __attribute__((__may_alias__)) u16_p;
111 const u16_p *u16_buf = (
const u16_p *)ptr;
113 while (len >= (
sizeof(*u16_buf) * 4)) {
118 len -=
sizeof(*u16_buf) * 4;
121 while (len >=
sizeof(*u16_buf)) {
123 len -=
sizeof(*u16_buf);
129 sum += *((
const uint8_t *)u16_buf);
143 static inline uint16_t
144 __rte_raw_cksum_reduce(uint32_t sum)
146 sum = ((sum & 0xffff0000) >> 16) + (sum & 0xffff);
147 sum = ((sum & 0xffff0000) >> 16) + (sum & 0xffff);
148 return (uint16_t)sum;
161 static inline uint16_t
166 sum = __rte_raw_cksum(buf, len, 0);
167 return __rte_raw_cksum_reduce(sum);
191 uint32_t seglen, done;
196 const char *, off), len);
205 for (seg = m; seg != NULL; seg = seg->
next) {
223 tmp = __rte_raw_cksum(buf, seglen, 0);
233 if (seglen > len - done)
237 *cksum = __rte_raw_cksum_reduce(sum);
251 static inline uint16_t
256 return (cksum == 0xffff) ? cksum : (uint16_t)~cksum;
277 static inline uint16_t
280 struct ipv4_psd_header {
288 psd_hdr.src_addr = ipv4_hdr->
src_addr;
289 psd_hdr.dst_addr = ipv4_hdr->
dst_addr;
297 -
sizeof(
struct ipv4_hdr)));
315 static inline uint16_t
322 sizeof(
struct ipv4_hdr));
327 cksum = ((cksum & 0xffff0000) >> 16) + (cksum & 0xffff);
328 cksum = (~cksum) & 0xffff;
332 return (uint16_t)cksum;
345 } __attribute__((__packed__));
348 #define IPV6_HDR_FL_SHIFT 0
349 #define IPV6_HDR_TC_SHIFT 20
350 #define IPV6_HDR_FL_MASK ((1u << IPV6_HDR_TC_SHIFT) - 1)
351 #define IPV6_HDR_TC_MASK (0xf << IPV6_HDR_TC_SHIFT)
369 static inline uint16_t
378 psd_hdr.proto = (uint32_t)(ipv6_hdr->
proto << 24);
385 sum = __rte_raw_cksum(ipv6_hdr->
src_addr,
388 sum = __rte_raw_cksum(&psd_hdr,
sizeof(psd_hdr), sum);
389 return __rte_raw_cksum_reduce(sum);
405 static inline uint16_t
416 cksum = ((cksum & 0xffff0000) >> 16) + (cksum & 0xffff);
417 cksum = (~cksum) & 0xffff;
421 return (uint16_t)cksum;