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);
229 buf = rte_pktmbuf_mtod_offset(seg, const char *, off);
231 /* all in one segment */
232 *cksum = rte_raw_cksum(buf, len);
236 /* hard case: process checksum of several segments */
240 tmp = __rte_raw_cksum(buf, seglen, 0);
242 tmp = rte_bswap16((uint16_t)tmp);
248 buf = rte_pktmbuf_mtod(seg, const char *);
249 seglen = rte_pktmbuf_data_len(seg);
250 if (seglen > len - done)
254 *cksum = __rte_raw_cksum_reduce(sum);
259 * Process the IPv4 checksum of an IPv4 header.
261 * The checksum field must be set to 0 by the caller.
264 * The pointer to the contiguous IPv4 header.
266 * The complemented checksum to set in the IP packet.
268 static inline uint16_t
269 rte_ipv4_cksum(const struct rte_ipv4_hdr *ipv4_hdr)
272 cksum = rte_raw_cksum(ipv4_hdr, (ipv4_hdr->version_ihl & 0xf) * 4);
273 return (uint16_t)~cksum;
277 * Process the pseudo-header checksum of an IPv4 header.
279 * The checksum field must be set to 0 by the caller.
281 * Depending on the ol_flags, the pseudo-header checksum expected by the
282 * drivers is not the same. For instance, when TSO is enabled, the IP
283 * payload length must not be included in the packet.
285 * When ol_flags is 0, it computes the standard pseudo-header checksum.
288 * The pointer to the contiguous IPv4 header.
290 * The ol_flags of the associated mbuf.
292 * The non-complemented checksum to set in the L4 header.
294 static inline uint16_t
295 rte_ipv4_phdr_cksum(const struct rte_ipv4_hdr *ipv4_hdr, uint64_t ol_flags)
297 struct ipv4_psd_header {
298 uint32_t src_addr; /* IP address of source host. */
299 uint32_t dst_addr; /* IP address of destination host. */
300 uint8_t zero; /* zero. */
301 uint8_t proto; /* L4 protocol type. */
302 uint16_t len; /* L4 length. */
308 psd_hdr.src_addr = ipv4_hdr->src_addr;
309 psd_hdr.dst_addr = ipv4_hdr->dst_addr;
311 psd_hdr.proto = ipv4_hdr->next_proto_id;
312 if (ol_flags & PKT_TX_TCP_SEG) {
315 l3_len = rte_be_to_cpu_16(ipv4_hdr->total_length);
316 ip_hdr_len = (ipv4_hdr->version_ihl & 0xf) * 4;
317 psd_hdr.len = rte_cpu_to_be_16((uint16_t)(l3_len - ip_hdr_len));
319 return rte_raw_cksum(&psd_hdr, sizeof(psd_hdr));
323 * Process the IPv4 UDP or TCP checksum.
325 * The IP and layer 4 checksum must be set to 0 in the packet by
329 * The pointer to the contiguous IPv4 header.
331 * The pointer to the beginning of the L4 header.
333 * The complemented checksum to set in the IP packet.
335 static inline uint16_t
336 rte_ipv4_udptcp_cksum(const struct rte_ipv4_hdr *ipv4_hdr, const void *l4_hdr)
339 uint32_t l3_len, l4_len;
342 ip_hdr_len = (ipv4_hdr->version_ihl & 0xf) * 4;
343 l3_len = rte_be_to_cpu_16(ipv4_hdr->total_length);
344 if (l3_len < ip_hdr_len)
347 l4_len = l3_len - ip_hdr_len;
349 cksum = rte_raw_cksum(l4_hdr, l4_len);
350 cksum += rte_ipv4_phdr_cksum(ipv4_hdr, 0);
352 cksum = ((cksum & 0xffff0000) >> 16) + (cksum & 0xffff);
353 cksum = (~cksum) & 0xffff;
355 * Per RFC 768:If the computed checksum is zero for UDP,
356 * it is transmitted as all ones
357 * (the equivalent in one's complement arithmetic).
359 if (cksum == 0 && ipv4_hdr->next_proto_id == IPPROTO_UDP)
362 return (uint16_t)cksum;
368 struct rte_ipv6_hdr {
369 rte_be32_t vtc_flow; /**< IP version, traffic class & flow label. */
370 rte_be16_t payload_len; /**< IP packet length - includes header size */
371 uint8_t proto; /**< Protocol, next header. */
372 uint8_t hop_limits; /**< Hop limits. */
373 uint8_t src_addr[16]; /**< IP address of source host. */
374 uint8_t dst_addr[16]; /**< IP address of destination host(s). */
377 /* IPv6 vtc_flow: IPv / TC / flow_label */
378 #define RTE_IPV6_HDR_FL_SHIFT 0
379 #define RTE_IPV6_HDR_TC_SHIFT 20
380 #define RTE_IPV6_HDR_FL_MASK ((1u << RTE_IPV6_HDR_TC_SHIFT) - 1)
381 #define RTE_IPV6_HDR_TC_MASK (0xff << RTE_IPV6_HDR_TC_SHIFT)
382 #define RTE_IPV6_HDR_DSCP_MASK (0xfc << RTE_IPV6_HDR_TC_SHIFT)
383 #define RTE_IPV6_HDR_ECN_MASK (0x03 << RTE_IPV6_HDR_TC_SHIFT)
384 #define RTE_IPV6_HDR_ECN_CE RTE_IPV6_HDR_ECN_MASK
386 #define RTE_IPV6_MIN_MTU 1280 /**< Minimum MTU for IPv6, see RFC 8200. */
389 * Process the pseudo-header checksum of an IPv6 header.
391 * Depending on the ol_flags, the pseudo-header checksum expected by the
392 * drivers is not the same. For instance, when TSO is enabled, the IPv6
393 * payload length must not be included in the packet.
395 * When ol_flags is 0, it computes the standard pseudo-header checksum.
398 * The pointer to the contiguous IPv6 header.
400 * The ol_flags of the associated mbuf.
402 * The non-complemented checksum to set in the L4 header.
404 static inline uint16_t
405 rte_ipv6_phdr_cksum(const struct rte_ipv6_hdr *ipv6_hdr, uint64_t ol_flags)
409 rte_be32_t len; /* L4 length. */
410 rte_be32_t proto; /* L4 protocol - top 3 bytes must be zero */
413 psd_hdr.proto = (uint32_t)(ipv6_hdr->proto << 24);
414 if (ol_flags & PKT_TX_TCP_SEG) {
417 psd_hdr.len = ipv6_hdr->payload_len;
420 sum = __rte_raw_cksum(ipv6_hdr->src_addr,
421 sizeof(ipv6_hdr->src_addr) + sizeof(ipv6_hdr->dst_addr),
423 sum = __rte_raw_cksum(&psd_hdr, sizeof(psd_hdr), sum);
424 return __rte_raw_cksum_reduce(sum);
428 * Process the IPv6 UDP or TCP checksum.
430 * The IPv4 header should not contains options. The layer 4 checksum
431 * must be set to 0 in the packet by the caller.
434 * The pointer to the contiguous IPv6 header.
436 * The pointer to the beginning of the L4 header.
438 * The complemented checksum to set in the IP packet.
440 static inline uint16_t
441 rte_ipv6_udptcp_cksum(const struct rte_ipv6_hdr *ipv6_hdr, const void *l4_hdr)
446 l4_len = rte_be_to_cpu_16(ipv6_hdr->payload_len);
448 cksum = rte_raw_cksum(l4_hdr, l4_len);
449 cksum += rte_ipv6_phdr_cksum(ipv6_hdr, 0);
451 cksum = ((cksum & 0xffff0000) >> 16) + (cksum & 0xffff);
452 cksum = (~cksum) & 0xffff;
454 * Per RFC 768: If the computed checksum is zero for UDP,
455 * it is transmitted as all ones
456 * (the equivalent in one's complement arithmetic).
458 if (cksum == 0 && ipv6_hdr->proto == IPPROTO_UDP)
461 return (uint16_t)cksum;
464 /* IPv6 fragmentation header size */
465 #define RTE_IPV6_FRAG_HDR_SIZE 8
468 * Parse next IPv6 header extension
470 * This function checks if proto number is an IPv6 extensions and parses its
471 * data if so, providing information on next header and extension length.
474 * Pointer to an extension raw data.
476 * Protocol number extracted from the "next header" field from
477 * the IPv6 header or the previous extension.
479 * Extension data length.
481 * next protocol number if proto is an IPv6 extension, -EINVAL otherwise
485 rte_ipv6_get_next_ext(const uint8_t *p, int proto, size_t *ext_len)
492 *ext_len = (*p + 2) * sizeof(uint32_t);
495 case IPPROTO_HOPOPTS:
496 case IPPROTO_ROUTING:
497 case IPPROTO_DSTOPTS:
499 *ext_len = (*p + 1) * sizeof(uint64_t);
502 case IPPROTO_FRAGMENT:
504 *ext_len = RTE_IPV6_FRAG_HDR_SIZE;
518 #endif /* _RTE_IP_H_ */