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 * Get the length of an IPv4 header.
110 * Pointer to the IPv4 header.
112 * The length of the IPv4 header (with options if present) in bytes.
114 static inline uint8_t
115 rte_ipv4_hdr_len(const struct rte_ipv4_hdr *ipv4_hdr)
117 return (uint8_t)((ipv4_hdr->version_ihl & RTE_IPV4_HDR_IHL_MASK) *
118 RTE_IPV4_IHL_MULTIPLIER);
122 * @internal Calculate a sum of all words in the buffer.
123 * Helper routine for the rte_raw_cksum().
126 * Pointer to the buffer.
128 * Length of the buffer.
130 * Initial value of the sum.
132 * sum += Sum of all words in the buffer.
134 static inline uint32_t
135 __rte_raw_cksum(const void *buf, size_t len, uint32_t sum)
137 /* workaround gcc strict-aliasing warning */
138 uintptr_t ptr = (uintptr_t)buf;
139 typedef uint16_t __attribute__((__may_alias__)) u16_p;
140 const u16_p *u16_buf = (const u16_p *)ptr;
142 while (len >= (sizeof(*u16_buf) * 4)) {
147 len -= sizeof(*u16_buf) * 4;
150 while (len >= sizeof(*u16_buf)) {
152 len -= sizeof(*u16_buf);
156 /* if length is in odd bytes */
159 *(uint8_t *)&left = *(const uint8_t *)u16_buf;
167 * @internal Reduce a sum to the non-complemented checksum.
168 * Helper routine for the rte_raw_cksum().
173 * The non-complemented checksum.
175 static inline uint16_t
176 __rte_raw_cksum_reduce(uint32_t sum)
178 sum = ((sum & 0xffff0000) >> 16) + (sum & 0xffff);
179 sum = ((sum & 0xffff0000) >> 16) + (sum & 0xffff);
180 return (uint16_t)sum;
184 * Process the non-complemented checksum of a buffer.
187 * Pointer to the buffer.
189 * Length of the buffer.
191 * The non-complemented checksum.
193 static inline uint16_t
194 rte_raw_cksum(const void *buf, size_t len)
198 sum = __rte_raw_cksum(buf, len, 0);
199 return __rte_raw_cksum_reduce(sum);
203 * Compute the raw (non complemented) checksum of a packet.
206 * The pointer to the mbuf.
208 * The offset in bytes to start the checksum.
210 * The length in bytes of the data to checksum.
212 * A pointer to the checksum, filled on success.
214 * 0 on success, -1 on error (bad length or offset).
217 rte_raw_cksum_mbuf(const struct rte_mbuf *m, uint32_t off, uint32_t len,
220 const struct rte_mbuf *seg;
223 uint32_t seglen, done;
225 /* easy case: all data in the first segment */
226 if (off + len <= rte_pktmbuf_data_len(m)) {
227 *cksum = rte_raw_cksum(rte_pktmbuf_mtod_offset(m,
228 const char *, off), len);
232 if (unlikely(off + len > rte_pktmbuf_pkt_len(m)))
233 return -1; /* invalid params, return a dummy value */
235 /* else browse the segment to find offset */
237 for (seg = m; seg != NULL; seg = seg->next) {
238 seglen = rte_pktmbuf_data_len(seg);
243 RTE_ASSERT(seg != NULL);
247 buf = rte_pktmbuf_mtod_offset(seg, const char *, off);
249 /* all in one segment */
250 *cksum = rte_raw_cksum(buf, len);
254 /* hard case: process checksum of several segments */
258 tmp = __rte_raw_cksum(buf, seglen, 0);
260 tmp = rte_bswap16((uint16_t)tmp);
266 buf = rte_pktmbuf_mtod(seg, const char *);
267 seglen = rte_pktmbuf_data_len(seg);
268 if (seglen > len - done)
272 *cksum = __rte_raw_cksum_reduce(sum);
277 * Process the IPv4 checksum of an IPv4 header.
279 * The checksum field must be set to 0 by the caller.
282 * The pointer to the contiguous IPv4 header.
284 * The complemented checksum to set in the IP packet.
286 static inline uint16_t
287 rte_ipv4_cksum(const struct rte_ipv4_hdr *ipv4_hdr)
290 cksum = rte_raw_cksum(ipv4_hdr, rte_ipv4_hdr_len(ipv4_hdr));
291 return (uint16_t)~cksum;
295 * Process the pseudo-header checksum of an IPv4 header.
297 * The checksum field must be set to 0 by the caller.
299 * Depending on the ol_flags, the pseudo-header checksum expected by the
300 * drivers is not the same. For instance, when TSO is enabled, the IP
301 * payload length must not be included in the packet.
303 * When ol_flags is 0, it computes the standard pseudo-header checksum.
306 * The pointer to the contiguous IPv4 header.
308 * The ol_flags of the associated mbuf.
310 * The non-complemented checksum to set in the L4 header.
312 static inline uint16_t
313 rte_ipv4_phdr_cksum(const struct rte_ipv4_hdr *ipv4_hdr, uint64_t ol_flags)
315 struct ipv4_psd_header {
316 uint32_t src_addr; /* IP address of source host. */
317 uint32_t dst_addr; /* IP address of destination host. */
318 uint8_t zero; /* zero. */
319 uint8_t proto; /* L4 protocol type. */
320 uint16_t len; /* L4 length. */
325 psd_hdr.src_addr = ipv4_hdr->src_addr;
326 psd_hdr.dst_addr = ipv4_hdr->dst_addr;
328 psd_hdr.proto = ipv4_hdr->next_proto_id;
329 if (ol_flags & PKT_TX_TCP_SEG) {
332 l3_len = rte_be_to_cpu_16(ipv4_hdr->total_length);
333 psd_hdr.len = rte_cpu_to_be_16((uint16_t)(l3_len -
334 rte_ipv4_hdr_len(ipv4_hdr)));
336 return rte_raw_cksum(&psd_hdr, sizeof(psd_hdr));
340 * Process the IPv4 UDP or TCP checksum.
342 * The IP and layer 4 checksum must be set to 0 in the packet by
346 * The pointer to the contiguous IPv4 header.
348 * The pointer to the beginning of the L4 header.
350 * The complemented checksum to set in the IP packet.
352 static inline uint16_t
353 rte_ipv4_udptcp_cksum(const struct rte_ipv4_hdr *ipv4_hdr, const void *l4_hdr)
356 uint32_t l3_len, l4_len;
359 ip_hdr_len = rte_ipv4_hdr_len(ipv4_hdr);
360 l3_len = rte_be_to_cpu_16(ipv4_hdr->total_length);
361 if (l3_len < ip_hdr_len)
364 l4_len = l3_len - ip_hdr_len;
366 cksum = rte_raw_cksum(l4_hdr, l4_len);
367 cksum += rte_ipv4_phdr_cksum(ipv4_hdr, 0);
369 cksum = ((cksum & 0xffff0000) >> 16) + (cksum & 0xffff);
370 cksum = (~cksum) & 0xffff;
372 * Per RFC 768:If the computed checksum is zero for UDP,
373 * it is transmitted as all ones
374 * (the equivalent in one's complement arithmetic).
376 if (cksum == 0 && ipv4_hdr->next_proto_id == IPPROTO_UDP)
379 return (uint16_t)cksum;
385 struct rte_ipv6_hdr {
386 rte_be32_t vtc_flow; /**< IP version, traffic class & flow label. */
387 rte_be16_t payload_len; /**< IP packet length - includes header size */
388 uint8_t proto; /**< Protocol, next header. */
389 uint8_t hop_limits; /**< Hop limits. */
390 uint8_t src_addr[16]; /**< IP address of source host. */
391 uint8_t dst_addr[16]; /**< IP address of destination host(s). */
394 /* IPv6 vtc_flow: IPv / TC / flow_label */
395 #define RTE_IPV6_HDR_FL_SHIFT 0
396 #define RTE_IPV6_HDR_TC_SHIFT 20
397 #define RTE_IPV6_HDR_FL_MASK ((1u << RTE_IPV6_HDR_TC_SHIFT) - 1)
398 #define RTE_IPV6_HDR_TC_MASK (0xff << RTE_IPV6_HDR_TC_SHIFT)
399 #define RTE_IPV6_HDR_DSCP_MASK (0xfc << RTE_IPV6_HDR_TC_SHIFT)
400 #define RTE_IPV6_HDR_ECN_MASK (0x03 << RTE_IPV6_HDR_TC_SHIFT)
401 #define RTE_IPV6_HDR_ECN_CE RTE_IPV6_HDR_ECN_MASK
403 #define RTE_IPV6_MIN_MTU 1280 /**< Minimum MTU for IPv6, see RFC 8200. */
406 * Process the pseudo-header checksum of an IPv6 header.
408 * Depending on the ol_flags, the pseudo-header checksum expected by the
409 * drivers is not the same. For instance, when TSO is enabled, the IPv6
410 * payload length must not be included in the packet.
412 * When ol_flags is 0, it computes the standard pseudo-header checksum.
415 * The pointer to the contiguous IPv6 header.
417 * The ol_flags of the associated mbuf.
419 * The non-complemented checksum to set in the L4 header.
421 static inline uint16_t
422 rte_ipv6_phdr_cksum(const struct rte_ipv6_hdr *ipv6_hdr, uint64_t ol_flags)
426 rte_be32_t len; /* L4 length. */
427 rte_be32_t proto; /* L4 protocol - top 3 bytes must be zero */
430 psd_hdr.proto = (uint32_t)(ipv6_hdr->proto << 24);
431 if (ol_flags & PKT_TX_TCP_SEG) {
434 psd_hdr.len = ipv6_hdr->payload_len;
437 sum = __rte_raw_cksum(ipv6_hdr->src_addr,
438 sizeof(ipv6_hdr->src_addr) + sizeof(ipv6_hdr->dst_addr),
440 sum = __rte_raw_cksum(&psd_hdr, sizeof(psd_hdr), sum);
441 return __rte_raw_cksum_reduce(sum);
445 * Process the IPv6 UDP or TCP checksum.
447 * The IPv4 header should not contains options. The layer 4 checksum
448 * must be set to 0 in the packet by the caller.
451 * The pointer to the contiguous IPv6 header.
453 * The pointer to the beginning of the L4 header.
455 * The complemented checksum to set in the IP packet.
457 static inline uint16_t
458 rte_ipv6_udptcp_cksum(const struct rte_ipv6_hdr *ipv6_hdr, const void *l4_hdr)
463 l4_len = rte_be_to_cpu_16(ipv6_hdr->payload_len);
465 cksum = rte_raw_cksum(l4_hdr, l4_len);
466 cksum += rte_ipv6_phdr_cksum(ipv6_hdr, 0);
468 cksum = ((cksum & 0xffff0000) >> 16) + (cksum & 0xffff);
469 cksum = (~cksum) & 0xffff;
471 * Per RFC 768: If the computed checksum is zero for UDP,
472 * it is transmitted as all ones
473 * (the equivalent in one's complement arithmetic).
475 if (cksum == 0 && ipv6_hdr->proto == IPPROTO_UDP)
478 return (uint16_t)cksum;
481 /** IPv6 fragment extension header. */
482 #define RTE_IPV6_EHDR_MF_SHIFT 0
483 #define RTE_IPV6_EHDR_MF_MASK 1
484 #define RTE_IPV6_EHDR_FO_SHIFT 3
485 #define RTE_IPV6_EHDR_FO_MASK (~((1 << RTE_IPV6_EHDR_FO_SHIFT) - 1))
486 #define RTE_IPV6_EHDR_FO_ALIGN (1 << RTE_IPV6_EHDR_FO_SHIFT)
488 #define RTE_IPV6_FRAG_USED_MASK (RTE_IPV6_EHDR_MF_MASK | RTE_IPV6_EHDR_FO_MASK)
490 #define RTE_IPV6_GET_MF(x) ((x) & RTE_IPV6_EHDR_MF_MASK)
491 #define RTE_IPV6_GET_FO(x) ((x) >> RTE_IPV6_EHDR_FO_SHIFT)
493 #define RTE_IPV6_SET_FRAG_DATA(fo, mf) \
494 (((fo) & RTE_IPV6_EHDR_FO_MASK) | ((mf) & RTE_IPV6_EHDR_MF_MASK))
496 struct rte_ipv6_fragment_ext {
497 uint8_t next_header; /**< Next header type */
498 uint8_t reserved; /**< Reserved */
499 rte_be16_t frag_data; /**< All fragmentation data */
500 rte_be32_t id; /**< Packet ID */
503 /* IPv6 fragment extension header size */
504 #define RTE_IPV6_FRAG_HDR_SIZE sizeof(struct rte_ipv6_fragment_ext)
507 * Parse next IPv6 header extension
509 * This function checks if proto number is an IPv6 extensions and parses its
510 * data if so, providing information on next header and extension length.
513 * Pointer to an extension raw data.
515 * Protocol number extracted from the "next header" field from
516 * the IPv6 header or the previous extension.
518 * Extension data length.
520 * next protocol number if proto is an IPv6 extension, -EINVAL otherwise
524 rte_ipv6_get_next_ext(const uint8_t *p, int proto, size_t *ext_len)
531 *ext_len = (*p + 2) * sizeof(uint32_t);
534 case IPPROTO_HOPOPTS:
535 case IPPROTO_ROUTING:
536 case IPPROTO_DSTOPTS:
538 *ext_len = (*p + 1) * sizeof(uint64_t);
541 case IPPROTO_FRAGMENT:
543 *ext_len = RTE_IPV6_FRAG_HDR_SIZE;
557 #endif /* _RTE_IP_H_ */