1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 1982, 1986, 1990, 1993
3 * The Regents of the University of California.
4 * Copyright(c) 2010-2014 Intel Corporation.
5 * Copyright(c) 2014 6WIND S.A.
19 #include <sys/types.h>
20 #include <netinet/in.h>
21 #include <netinet/ip.h>
23 #include <rte_byteorder.h>
34 uint8_t version_ihl; /**< version and header length */
35 uint8_t type_of_service; /**< type of service */
36 rte_be16_t total_length; /**< length of packet */
37 rte_be16_t packet_id; /**< packet ID */
38 rte_be16_t fragment_offset; /**< fragmentation offset */
39 uint8_t time_to_live; /**< time to live */
40 uint8_t next_proto_id; /**< protocol ID */
41 rte_be16_t hdr_checksum; /**< header checksum */
42 rte_be32_t src_addr; /**< source address */
43 rte_be32_t dst_addr; /**< destination address */
46 /** Create IPv4 address */
47 #define RTE_IPV4(a, b, c, d) ((uint32_t)(((a) & 0xff) << 24) | \
48 (((b) & 0xff) << 16) | \
49 (((c) & 0xff) << 8) | \
52 /** Maximal IPv4 packet length (including a header) */
53 #define RTE_IPV4_MAX_PKT_LEN 65535
55 /** Internet header length mask for version_ihl field */
56 #define RTE_IPV4_HDR_IHL_MASK (0x0f)
58 * Internet header length field multiplier (IHL field specifies overall header
59 * length in number of 4-byte words)
61 #define RTE_IPV4_IHL_MULTIPLIER (4)
63 /* Type of Service fields */
64 #define RTE_IPV4_HDR_DSCP_MASK (0xfc)
65 #define RTE_IPV4_HDR_ECN_MASK (0x03)
66 #define RTE_IPV4_HDR_ECN_CE RTE_IPV4_HDR_ECN_MASK
68 /* Fragment Offset * Flags. */
69 #define RTE_IPV4_HDR_DF_SHIFT 14
70 #define RTE_IPV4_HDR_MF_SHIFT 13
71 #define RTE_IPV4_HDR_FO_SHIFT 3
73 #define RTE_IPV4_HDR_DF_FLAG (1 << RTE_IPV4_HDR_DF_SHIFT)
74 #define RTE_IPV4_HDR_MF_FLAG (1 << RTE_IPV4_HDR_MF_SHIFT)
76 #define RTE_IPV4_HDR_OFFSET_MASK ((1 << RTE_IPV4_HDR_MF_SHIFT) - 1)
78 #define RTE_IPV4_HDR_OFFSET_UNITS 8
83 #define RTE_IPV4_ANY ((uint32_t)0x00000000) /**< 0.0.0.0 */
84 #define RTE_IPV4_LOOPBACK ((uint32_t)0x7f000001) /**< 127.0.0.1 */
85 #define RTE_IPV4_BROADCAST ((uint32_t)0xe0000000) /**< 224.0.0.0 */
86 #define RTE_IPV4_ALLHOSTS_GROUP ((uint32_t)0xe0000001) /**< 224.0.0.1 */
87 #define RTE_IPV4_ALLRTRS_GROUP ((uint32_t)0xe0000002) /**< 224.0.0.2 */
88 #define RTE_IPV4_MAX_LOCAL_GROUP ((uint32_t)0xe00000ff) /**< 224.0.0.255 */
91 * IPv4 Multicast-related macros
93 #define RTE_IPV4_MIN_MCAST \
94 RTE_IPV4(224, 0, 0, 0) /**< Minimal IPv4-multicast address */
95 #define RTE_IPV4_MAX_MCAST \
96 RTE_IPV4(239, 255, 255, 255) /**< Maximum IPv4 multicast address */
98 #define RTE_IS_IPV4_MCAST(x) \
99 ((x) >= RTE_IPV4_MIN_MCAST && (x) <= RTE_IPV4_MAX_MCAST)
100 /**< check if IPv4 address is multicast */
102 /* IPv4 default fields values */
103 #define RTE_IPV4_MIN_IHL (0x5)
104 #define RTE_IPV4_VHL_DEF ((IPVERSION << 4) | RTE_IPV4_MIN_IHL)
107 * @internal Calculate a sum of all words in the buffer.
108 * Helper routine for the rte_raw_cksum().
111 * Pointer to the buffer.
113 * Length of the buffer.
115 * Initial value of the sum.
117 * sum += Sum of all words in the buffer.
119 static inline uint32_t
120 __rte_raw_cksum(const void *buf, size_t len, uint32_t sum)
122 /* workaround gcc strict-aliasing warning */
123 uintptr_t ptr = (uintptr_t)buf;
124 typedef uint16_t __attribute__((__may_alias__)) u16_p;
125 const u16_p *u16_buf = (const u16_p *)ptr;
127 while (len >= (sizeof(*u16_buf) * 4)) {
132 len -= sizeof(*u16_buf) * 4;
135 while (len >= sizeof(*u16_buf)) {
137 len -= sizeof(*u16_buf);
141 /* if length is in odd bytes */
144 *(uint8_t *)&left = *(const uint8_t *)u16_buf;
152 * @internal Reduce a sum to the non-complemented checksum.
153 * Helper routine for the rte_raw_cksum().
158 * The non-complemented checksum.
160 static inline uint16_t
161 __rte_raw_cksum_reduce(uint32_t sum)
163 sum = ((sum & 0xffff0000) >> 16) + (sum & 0xffff);
164 sum = ((sum & 0xffff0000) >> 16) + (sum & 0xffff);
165 return (uint16_t)sum;
169 * Process the non-complemented checksum of a buffer.
172 * Pointer to the buffer.
174 * Length of the buffer.
176 * The non-complemented checksum.
178 static inline uint16_t
179 rte_raw_cksum(const void *buf, size_t len)
183 sum = __rte_raw_cksum(buf, len, 0);
184 return __rte_raw_cksum_reduce(sum);
188 * Compute the raw (non complemented) checksum of a packet.
191 * The pointer to the mbuf.
193 * The offset in bytes to start the checksum.
195 * The length in bytes of the data to checksum.
197 * A pointer to the checksum, filled on success.
199 * 0 on success, -1 on error (bad length or offset).
202 rte_raw_cksum_mbuf(const struct rte_mbuf *m, uint32_t off, uint32_t len,
205 const struct rte_mbuf *seg;
208 uint32_t seglen, done;
210 /* easy case: all data in the first segment */
211 if (off + len <= rte_pktmbuf_data_len(m)) {
212 *cksum = rte_raw_cksum(rte_pktmbuf_mtod_offset(m,
213 const char *, off), len);
217 if (unlikely(off + len > rte_pktmbuf_pkt_len(m)))
218 return -1; /* invalid params, return a dummy value */
220 /* else browse the segment to find offset */
222 for (seg = m; seg != NULL; seg = seg->next) {
223 seglen = rte_pktmbuf_data_len(seg);
228 RTE_ASSERT(seg != NULL);
232 buf = rte_pktmbuf_mtod_offset(seg, const char *, off);
234 /* all in one segment */
235 *cksum = rte_raw_cksum(buf, len);
239 /* hard case: process checksum of several segments */
243 tmp = __rte_raw_cksum(buf, seglen, 0);
245 tmp = rte_bswap16((uint16_t)tmp);
251 buf = rte_pktmbuf_mtod(seg, const char *);
252 seglen = rte_pktmbuf_data_len(seg);
253 if (seglen > len - done)
257 *cksum = __rte_raw_cksum_reduce(sum);
262 * Process the IPv4 checksum of an IPv4 header.
264 * The checksum field must be set to 0 by the caller.
267 * The pointer to the contiguous IPv4 header.
269 * The complemented checksum to set in the IP packet.
271 static inline uint16_t
272 rte_ipv4_cksum(const struct rte_ipv4_hdr *ipv4_hdr)
275 cksum = rte_raw_cksum(ipv4_hdr, (ipv4_hdr->version_ihl & 0xf) * 4);
276 return (uint16_t)~cksum;
280 * Process the pseudo-header checksum of an IPv4 header.
282 * The checksum field must be set to 0 by the caller.
284 * Depending on the ol_flags, the pseudo-header checksum expected by the
285 * drivers is not the same. For instance, when TSO is enabled, the IP
286 * payload length must not be included in the packet.
288 * When ol_flags is 0, it computes the standard pseudo-header checksum.
291 * The pointer to the contiguous IPv4 header.
293 * The ol_flags of the associated mbuf.
295 * The non-complemented checksum to set in the L4 header.
297 static inline uint16_t
298 rte_ipv4_phdr_cksum(const struct rte_ipv4_hdr *ipv4_hdr, uint64_t ol_flags)
300 struct ipv4_psd_header {
301 uint32_t src_addr; /* IP address of source host. */
302 uint32_t dst_addr; /* IP address of destination host. */
303 uint8_t zero; /* zero. */
304 uint8_t proto; /* L4 protocol type. */
305 uint16_t len; /* L4 length. */
311 psd_hdr.src_addr = ipv4_hdr->src_addr;
312 psd_hdr.dst_addr = ipv4_hdr->dst_addr;
314 psd_hdr.proto = ipv4_hdr->next_proto_id;
315 if (ol_flags & PKT_TX_TCP_SEG) {
318 l3_len = rte_be_to_cpu_16(ipv4_hdr->total_length);
319 ip_hdr_len = (ipv4_hdr->version_ihl & 0xf) * 4;
320 psd_hdr.len = rte_cpu_to_be_16((uint16_t)(l3_len - ip_hdr_len));
322 return rte_raw_cksum(&psd_hdr, sizeof(psd_hdr));
326 * Process the IPv4 UDP or TCP checksum.
328 * The IP and layer 4 checksum must be set to 0 in the packet by
332 * The pointer to the contiguous IPv4 header.
334 * The pointer to the beginning of the L4 header.
336 * The complemented checksum to set in the IP packet.
338 static inline uint16_t
339 rte_ipv4_udptcp_cksum(const struct rte_ipv4_hdr *ipv4_hdr, const void *l4_hdr)
342 uint32_t l3_len, l4_len;
345 ip_hdr_len = (ipv4_hdr->version_ihl & 0xf) * 4;
346 l3_len = rte_be_to_cpu_16(ipv4_hdr->total_length);
347 if (l3_len < ip_hdr_len)
350 l4_len = l3_len - ip_hdr_len;
352 cksum = rte_raw_cksum(l4_hdr, l4_len);
353 cksum += rte_ipv4_phdr_cksum(ipv4_hdr, 0);
355 cksum = ((cksum & 0xffff0000) >> 16) + (cksum & 0xffff);
356 cksum = (~cksum) & 0xffff;
358 * Per RFC 768:If the computed checksum is zero for UDP,
359 * it is transmitted as all ones
360 * (the equivalent in one's complement arithmetic).
362 if (cksum == 0 && ipv4_hdr->next_proto_id == IPPROTO_UDP)
365 return (uint16_t)cksum;
371 struct rte_ipv6_hdr {
372 rte_be32_t vtc_flow; /**< IP version, traffic class & flow label. */
373 rte_be16_t payload_len; /**< IP packet length - includes header size */
374 uint8_t proto; /**< Protocol, next header. */
375 uint8_t hop_limits; /**< Hop limits. */
376 uint8_t src_addr[16]; /**< IP address of source host. */
377 uint8_t dst_addr[16]; /**< IP address of destination host(s). */
380 /* IPv6 vtc_flow: IPv / TC / flow_label */
381 #define RTE_IPV6_HDR_FL_SHIFT 0
382 #define RTE_IPV6_HDR_TC_SHIFT 20
383 #define RTE_IPV6_HDR_FL_MASK ((1u << RTE_IPV6_HDR_TC_SHIFT) - 1)
384 #define RTE_IPV6_HDR_TC_MASK (0xff << RTE_IPV6_HDR_TC_SHIFT)
385 #define RTE_IPV6_HDR_DSCP_MASK (0xfc << RTE_IPV6_HDR_TC_SHIFT)
386 #define RTE_IPV6_HDR_ECN_MASK (0x03 << RTE_IPV6_HDR_TC_SHIFT)
387 #define RTE_IPV6_HDR_ECN_CE RTE_IPV6_HDR_ECN_MASK
389 #define RTE_IPV6_MIN_MTU 1280 /**< Minimum MTU for IPv6, see RFC 8200. */
392 * Process the pseudo-header checksum of an IPv6 header.
394 * Depending on the ol_flags, the pseudo-header checksum expected by the
395 * drivers is not the same. For instance, when TSO is enabled, the IPv6
396 * payload length must not be included in the packet.
398 * When ol_flags is 0, it computes the standard pseudo-header checksum.
401 * The pointer to the contiguous IPv6 header.
403 * The ol_flags of the associated mbuf.
405 * The non-complemented checksum to set in the L4 header.
407 static inline uint16_t
408 rte_ipv6_phdr_cksum(const struct rte_ipv6_hdr *ipv6_hdr, uint64_t ol_flags)
412 rte_be32_t len; /* L4 length. */
413 rte_be32_t proto; /* L4 protocol - top 3 bytes must be zero */
416 psd_hdr.proto = (uint32_t)(ipv6_hdr->proto << 24);
417 if (ol_flags & PKT_TX_TCP_SEG) {
420 psd_hdr.len = ipv6_hdr->payload_len;
423 sum = __rte_raw_cksum(ipv6_hdr->src_addr,
424 sizeof(ipv6_hdr->src_addr) + sizeof(ipv6_hdr->dst_addr),
426 sum = __rte_raw_cksum(&psd_hdr, sizeof(psd_hdr), sum);
427 return __rte_raw_cksum_reduce(sum);
431 * Process the IPv6 UDP or TCP checksum.
433 * The IPv4 header should not contains options. The layer 4 checksum
434 * must be set to 0 in the packet by the caller.
437 * The pointer to the contiguous IPv6 header.
439 * The pointer to the beginning of the L4 header.
441 * The complemented checksum to set in the IP packet.
443 static inline uint16_t
444 rte_ipv6_udptcp_cksum(const struct rte_ipv6_hdr *ipv6_hdr, const void *l4_hdr)
449 l4_len = rte_be_to_cpu_16(ipv6_hdr->payload_len);
451 cksum = rte_raw_cksum(l4_hdr, l4_len);
452 cksum += rte_ipv6_phdr_cksum(ipv6_hdr, 0);
454 cksum = ((cksum & 0xffff0000) >> 16) + (cksum & 0xffff);
455 cksum = (~cksum) & 0xffff;
457 * Per RFC 768: If the computed checksum is zero for UDP,
458 * it is transmitted as all ones
459 * (the equivalent in one's complement arithmetic).
461 if (cksum == 0 && ipv6_hdr->proto == IPPROTO_UDP)
464 return (uint16_t)cksum;
467 /* IPv6 fragmentation header size */
468 #define RTE_IPV6_FRAG_HDR_SIZE 8
471 * Parse next IPv6 header extension
473 * This function checks if proto number is an IPv6 extensions and parses its
474 * data if so, providing information on next header and extension length.
477 * Pointer to an extension raw data.
479 * Protocol number extracted from the "next header" field from
480 * the IPv6 header or the previous extension.
482 * Extension data length.
484 * next protocol number if proto is an IPv6 extension, -EINVAL otherwise
488 rte_ipv6_get_next_ext(const uint8_t *p, int proto, size_t *ext_len)
495 *ext_len = (*p + 2) * sizeof(uint32_t);
498 case IPPROTO_HOPOPTS:
499 case IPPROTO_ROUTING:
500 case IPPROTO_DSTOPTS:
502 *ext_len = (*p + 1) * sizeof(uint64_t);
505 case IPPROTO_FRAGMENT:
507 *ext_len = RTE_IPV6_FRAG_HDR_SIZE;
521 #endif /* _RTE_IP_H_ */