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.
20 #ifdef RTE_EXEC_ENV_WINDOWS
23 #include <sys/socket.h>
24 #include <sys/types.h>
25 #include <netinet/in.h>
26 #include <arpa/inet.h>
27 #include <netinet/ip.h>
28 #include <netinet/ip6.h>
31 #include <rte_byteorder.h>
44 uint8_t version_ihl; /**< version and header length */
46 #if RTE_BYTE_ORDER == RTE_LITTLE_ENDIAN
47 uint8_t ihl:4; /**< header length */
48 uint8_t version:4; /**< version */
49 #elif RTE_BYTE_ORDER == RTE_BIG_ENDIAN
50 uint8_t version:4; /**< version */
51 uint8_t ihl:4; /**< header length */
55 uint8_t type_of_service; /**< type of service */
56 rte_be16_t total_length; /**< length of packet */
57 rte_be16_t packet_id; /**< packet ID */
58 rte_be16_t fragment_offset; /**< fragmentation offset */
59 uint8_t time_to_live; /**< time to live */
60 uint8_t next_proto_id; /**< protocol ID */
61 rte_be16_t hdr_checksum; /**< header checksum */
62 rte_be32_t src_addr; /**< source address */
63 rte_be32_t dst_addr; /**< destination address */
66 /** Create IPv4 address */
67 #define RTE_IPV4(a, b, c, d) ((uint32_t)(((a) & 0xff) << 24) | \
68 (((b) & 0xff) << 16) | \
69 (((c) & 0xff) << 8) | \
72 /** Maximal IPv4 packet length (including a header) */
73 #define RTE_IPV4_MAX_PKT_LEN 65535
75 /** Internet header length mask for version_ihl field */
76 #define RTE_IPV4_HDR_IHL_MASK (0x0f)
78 * Internet header length field multiplier (IHL field specifies overall header
79 * length in number of 4-byte words)
81 #define RTE_IPV4_IHL_MULTIPLIER (4)
83 /* Type of Service fields */
84 #define RTE_IPV4_HDR_DSCP_MASK (0xfc)
85 #define RTE_IPV4_HDR_ECN_MASK (0x03)
86 #define RTE_IPV4_HDR_ECN_CE RTE_IPV4_HDR_ECN_MASK
88 /* Fragment Offset * Flags. */
89 #define RTE_IPV4_HDR_DF_SHIFT 14
90 #define RTE_IPV4_HDR_MF_SHIFT 13
91 #define RTE_IPV4_HDR_FO_SHIFT 3
93 #define RTE_IPV4_HDR_DF_FLAG (1 << RTE_IPV4_HDR_DF_SHIFT)
94 #define RTE_IPV4_HDR_MF_FLAG (1 << RTE_IPV4_HDR_MF_SHIFT)
96 #define RTE_IPV4_HDR_OFFSET_MASK ((1 << RTE_IPV4_HDR_MF_SHIFT) - 1)
98 #define RTE_IPV4_HDR_OFFSET_UNITS 8
103 #define RTE_IPV4_ANY ((uint32_t)0x00000000) /**< 0.0.0.0 */
104 #define RTE_IPV4_LOOPBACK ((uint32_t)0x7f000001) /**< 127.0.0.1 */
105 #define RTE_IPV4_BROADCAST ((uint32_t)0xe0000000) /**< 224.0.0.0 */
106 #define RTE_IPV4_ALLHOSTS_GROUP ((uint32_t)0xe0000001) /**< 224.0.0.1 */
107 #define RTE_IPV4_ALLRTRS_GROUP ((uint32_t)0xe0000002) /**< 224.0.0.2 */
108 #define RTE_IPV4_MAX_LOCAL_GROUP ((uint32_t)0xe00000ff) /**< 224.0.0.255 */
111 * IPv4 Multicast-related macros
113 #define RTE_IPV4_MIN_MCAST \
114 RTE_IPV4(224, 0, 0, 0) /**< Minimal IPv4-multicast address */
115 #define RTE_IPV4_MAX_MCAST \
116 RTE_IPV4(239, 255, 255, 255) /**< Maximum IPv4 multicast address */
118 #define RTE_IS_IPV4_MCAST(x) \
119 ((x) >= RTE_IPV4_MIN_MCAST && (x) <= RTE_IPV4_MAX_MCAST)
120 /**< check if IPv4 address is multicast */
122 /* IPv4 default fields values */
123 #define RTE_IPV4_MIN_IHL (0x5)
124 #define RTE_IPV4_VHL_DEF ((IPVERSION << 4) | RTE_IPV4_MIN_IHL)
127 * Get the length of an IPv4 header.
130 * Pointer to the IPv4 header.
132 * The length of the IPv4 header (with options if present) in bytes.
134 static inline uint8_t
135 rte_ipv4_hdr_len(const struct rte_ipv4_hdr *ipv4_hdr)
137 return (uint8_t)((ipv4_hdr->version_ihl & RTE_IPV4_HDR_IHL_MASK) *
138 RTE_IPV4_IHL_MULTIPLIER);
142 * @internal Calculate a sum of all words in the buffer.
143 * Helper routine for the rte_raw_cksum().
146 * Pointer to the buffer.
148 * Length of the buffer.
150 * Initial value of the sum.
152 * sum += Sum of all words in the buffer.
154 static inline uint32_t
155 __rte_raw_cksum(const void *buf, size_t len, uint32_t sum)
157 /* extend strict-aliasing rules */
158 typedef uint16_t __attribute__((__may_alias__)) u16_p;
159 const u16_p *u16_buf = (const u16_p *)buf;
160 const u16_p *end = u16_buf + len / sizeof(*u16_buf);
162 for (; u16_buf != end; ++u16_buf)
165 /* if length is odd, keeping it byte order independent */
166 if (unlikely(len % 2)) {
168 *(unsigned char *)&left = *(const unsigned char *)end;
176 * @internal Reduce a sum to the non-complemented checksum.
177 * Helper routine for the rte_raw_cksum().
182 * The non-complemented checksum.
184 static inline uint16_t
185 __rte_raw_cksum_reduce(uint32_t sum)
187 sum = ((sum & 0xffff0000) >> 16) + (sum & 0xffff);
188 sum = ((sum & 0xffff0000) >> 16) + (sum & 0xffff);
189 return (uint16_t)sum;
193 * Process the non-complemented checksum of a buffer.
196 * Pointer to the buffer.
198 * Length of the buffer.
200 * The non-complemented checksum.
202 static inline uint16_t
203 rte_raw_cksum(const void *buf, size_t len)
207 sum = __rte_raw_cksum(buf, len, 0);
208 return __rte_raw_cksum_reduce(sum);
212 * Compute the raw (non complemented) checksum of a packet.
215 * The pointer to the mbuf.
217 * The offset in bytes to start the checksum.
219 * The length in bytes of the data to checksum.
221 * A pointer to the checksum, filled on success.
223 * 0 on success, -1 on error (bad length or offset).
226 rte_raw_cksum_mbuf(const struct rte_mbuf *m, uint32_t off, uint32_t len,
229 const struct rte_mbuf *seg;
232 uint32_t seglen, done;
234 /* easy case: all data in the first segment */
235 if (off + len <= rte_pktmbuf_data_len(m)) {
236 *cksum = rte_raw_cksum(rte_pktmbuf_mtod_offset(m,
237 const char *, off), len);
241 if (unlikely(off + len > rte_pktmbuf_pkt_len(m)))
242 return -1; /* invalid params, return a dummy value */
244 /* else browse the segment to find offset */
246 for (seg = m; seg != NULL; seg = seg->next) {
247 seglen = rte_pktmbuf_data_len(seg);
252 RTE_ASSERT(seg != NULL);
256 buf = rte_pktmbuf_mtod_offset(seg, const char *, off);
258 /* all in one segment */
259 *cksum = rte_raw_cksum(buf, len);
263 /* hard case: process checksum of several segments */
267 tmp = __rte_raw_cksum(buf, seglen, 0);
269 tmp = rte_bswap16((uint16_t)tmp);
275 buf = rte_pktmbuf_mtod(seg, const char *);
276 seglen = rte_pktmbuf_data_len(seg);
277 if (seglen > len - done)
281 *cksum = __rte_raw_cksum_reduce(sum);
286 * Process the IPv4 checksum of an IPv4 header.
288 * The checksum field must be set to 0 by the caller.
291 * The pointer to the contiguous IPv4 header.
293 * The complemented checksum to set in the IP packet.
295 static inline uint16_t
296 rte_ipv4_cksum(const struct rte_ipv4_hdr *ipv4_hdr)
299 cksum = rte_raw_cksum(ipv4_hdr, rte_ipv4_hdr_len(ipv4_hdr));
300 return (uint16_t)~cksum;
304 * Process the pseudo-header checksum of an IPv4 header.
306 * The checksum field must be set to 0 by the caller.
308 * Depending on the ol_flags, the pseudo-header checksum expected by the
309 * drivers is not the same. For instance, when TSO is enabled, the IP
310 * payload length must not be included in the packet.
312 * When ol_flags is 0, it computes the standard pseudo-header checksum.
315 * The pointer to the contiguous IPv4 header.
317 * The ol_flags of the associated mbuf.
319 * The non-complemented checksum to set in the L4 header.
321 static inline uint16_t
322 rte_ipv4_phdr_cksum(const struct rte_ipv4_hdr *ipv4_hdr, uint64_t ol_flags)
324 struct ipv4_psd_header {
325 uint32_t src_addr; /* IP address of source host. */
326 uint32_t dst_addr; /* IP address of destination host. */
327 uint8_t zero; /* zero. */
328 uint8_t proto; /* L4 protocol type. */
329 uint16_t len; /* L4 length. */
334 psd_hdr.src_addr = ipv4_hdr->src_addr;
335 psd_hdr.dst_addr = ipv4_hdr->dst_addr;
337 psd_hdr.proto = ipv4_hdr->next_proto_id;
338 if (ol_flags & RTE_MBUF_F_TX_TCP_SEG) {
341 l3_len = rte_be_to_cpu_16(ipv4_hdr->total_length);
342 psd_hdr.len = rte_cpu_to_be_16((uint16_t)(l3_len -
343 rte_ipv4_hdr_len(ipv4_hdr)));
345 return rte_raw_cksum(&psd_hdr, sizeof(psd_hdr));
349 * @internal Calculate the non-complemented IPv4 L4 checksum
351 static inline uint16_t
352 __rte_ipv4_udptcp_cksum(const struct rte_ipv4_hdr *ipv4_hdr, const void *l4_hdr)
355 uint32_t l3_len, l4_len;
358 ip_hdr_len = rte_ipv4_hdr_len(ipv4_hdr);
359 l3_len = rte_be_to_cpu_16(ipv4_hdr->total_length);
360 if (l3_len < ip_hdr_len)
363 l4_len = l3_len - ip_hdr_len;
365 cksum = rte_raw_cksum(l4_hdr, l4_len);
366 cksum += rte_ipv4_phdr_cksum(ipv4_hdr, 0);
368 cksum = ((cksum & 0xffff0000) >> 16) + (cksum & 0xffff);
370 return (uint16_t)cksum;
374 * Process the IPv4 UDP or TCP checksum.
376 * The layer 4 checksum must be set to 0 in the L4 header by the caller.
379 * The pointer to the contiguous IPv4 header.
381 * The pointer to the beginning of the L4 header.
383 * The complemented checksum to set in the L4 header.
385 static inline uint16_t
386 rte_ipv4_udptcp_cksum(const struct rte_ipv4_hdr *ipv4_hdr, const void *l4_hdr)
388 uint16_t cksum = __rte_ipv4_udptcp_cksum(ipv4_hdr, l4_hdr);
393 * Per RFC 768: If the computed checksum is zero for UDP,
394 * it is transmitted as all ones
395 * (the equivalent in one's complement arithmetic).
397 if (cksum == 0 && ipv4_hdr->next_proto_id == IPPROTO_UDP)
404 * @internal Calculate the non-complemented IPv4 L4 checksum of a packet
406 static inline uint16_t
407 __rte_ipv4_udptcp_cksum_mbuf(const struct rte_mbuf *m,
408 const struct rte_ipv4_hdr *ipv4_hdr,
414 if (l4_off > m->pkt_len)
417 if (rte_raw_cksum_mbuf(m, l4_off, m->pkt_len - l4_off, &raw_cksum))
420 cksum = raw_cksum + rte_ipv4_phdr_cksum(ipv4_hdr, 0);
422 cksum = ((cksum & 0xffff0000) >> 16) + (cksum & 0xffff);
424 return (uint16_t)cksum;
429 * @b EXPERIMENTAL: this API may change without prior notice.
431 * Compute the IPv4 UDP/TCP checksum of a packet.
434 * The pointer to the mbuf.
436 * The pointer to the contiguous IPv4 header.
438 * The offset in bytes to start L4 checksum.
440 * The complemented checksum to set in the L4 header.
443 static inline uint16_t
444 rte_ipv4_udptcp_cksum_mbuf(const struct rte_mbuf *m,
445 const struct rte_ipv4_hdr *ipv4_hdr, uint16_t l4_off)
447 uint16_t cksum = __rte_ipv4_udptcp_cksum_mbuf(m, ipv4_hdr, l4_off);
452 * Per RFC 768: If the computed checksum is zero for UDP,
453 * it is transmitted as all ones
454 * (the equivalent in one's complement arithmetic).
456 if (cksum == 0 && ipv4_hdr->next_proto_id == IPPROTO_UDP)
463 * Validate the IPv4 UDP or TCP checksum.
465 * In case of UDP, the caller must first check if udp_hdr->dgram_cksum is 0
466 * (i.e. no checksum).
469 * The pointer to the contiguous IPv4 header.
471 * The pointer to the beginning of the L4 header.
473 * Return 0 if the checksum is correct, else -1.
477 rte_ipv4_udptcp_cksum_verify(const struct rte_ipv4_hdr *ipv4_hdr,
480 uint16_t cksum = __rte_ipv4_udptcp_cksum(ipv4_hdr, l4_hdr);
490 * @b EXPERIMENTAL: this API may change without prior notice.
492 * Verify the IPv4 UDP/TCP checksum of a packet.
494 * In case of UDP, the caller must first check if udp_hdr->dgram_cksum is 0
495 * (i.e. no checksum).
498 * The pointer to the mbuf.
500 * The pointer to the contiguous IPv4 header.
502 * The offset in bytes to start L4 checksum.
504 * Return 0 if the checksum is correct, else -1.
507 static inline uint16_t
508 rte_ipv4_udptcp_cksum_mbuf_verify(const struct rte_mbuf *m,
509 const struct rte_ipv4_hdr *ipv4_hdr,
512 uint16_t cksum = __rte_ipv4_udptcp_cksum_mbuf(m, ipv4_hdr, l4_off);
523 struct rte_ipv6_hdr {
524 rte_be32_t vtc_flow; /**< IP version, traffic class & flow label. */
525 rte_be16_t payload_len; /**< IP payload size, including ext. headers */
526 uint8_t proto; /**< Protocol, next header. */
527 uint8_t hop_limits; /**< Hop limits. */
528 uint8_t src_addr[16]; /**< IP address of source host. */
529 uint8_t dst_addr[16]; /**< IP address of destination host(s). */
532 /* IPv6 vtc_flow: IPv / TC / flow_label */
533 #define RTE_IPV6_HDR_FL_SHIFT 0
534 #define RTE_IPV6_HDR_TC_SHIFT 20
535 #define RTE_IPV6_HDR_FL_MASK ((1u << RTE_IPV6_HDR_TC_SHIFT) - 1)
536 #define RTE_IPV6_HDR_TC_MASK (0xff << RTE_IPV6_HDR_TC_SHIFT)
537 #define RTE_IPV6_HDR_DSCP_MASK (0xfc << RTE_IPV6_HDR_TC_SHIFT)
538 #define RTE_IPV6_HDR_ECN_MASK (0x03 << RTE_IPV6_HDR_TC_SHIFT)
539 #define RTE_IPV6_HDR_ECN_CE RTE_IPV6_HDR_ECN_MASK
541 #define RTE_IPV6_MIN_MTU 1280 /**< Minimum MTU for IPv6, see RFC 8200. */
544 * Process the pseudo-header checksum of an IPv6 header.
546 * Depending on the ol_flags, the pseudo-header checksum expected by the
547 * drivers is not the same. For instance, when TSO is enabled, the IPv6
548 * payload length must not be included in the packet.
550 * When ol_flags is 0, it computes the standard pseudo-header checksum.
553 * The pointer to the contiguous IPv6 header.
555 * The ol_flags of the associated mbuf.
557 * The non-complemented checksum to set in the L4 header.
559 static inline uint16_t
560 rte_ipv6_phdr_cksum(const struct rte_ipv6_hdr *ipv6_hdr, uint64_t ol_flags)
564 rte_be32_t len; /* L4 length. */
565 rte_be32_t proto; /* L4 protocol - top 3 bytes must be zero */
568 psd_hdr.proto = (uint32_t)(ipv6_hdr->proto << 24);
569 if (ol_flags & RTE_MBUF_F_TX_TCP_SEG) {
572 psd_hdr.len = ipv6_hdr->payload_len;
575 sum = __rte_raw_cksum(ipv6_hdr->src_addr,
576 sizeof(ipv6_hdr->src_addr) + sizeof(ipv6_hdr->dst_addr),
578 sum = __rte_raw_cksum(&psd_hdr, sizeof(psd_hdr), sum);
579 return __rte_raw_cksum_reduce(sum);
583 * @internal Calculate the non-complemented IPv6 L4 checksum
585 static inline uint16_t
586 __rte_ipv6_udptcp_cksum(const struct rte_ipv6_hdr *ipv6_hdr, const void *l4_hdr)
591 l4_len = rte_be_to_cpu_16(ipv6_hdr->payload_len);
593 cksum = rte_raw_cksum(l4_hdr, l4_len);
594 cksum += rte_ipv6_phdr_cksum(ipv6_hdr, 0);
596 cksum = ((cksum & 0xffff0000) >> 16) + (cksum & 0xffff);
598 return (uint16_t)cksum;
602 * Process the IPv6 UDP or TCP checksum.
604 * The IPv6 header must not be followed by extension headers. The layer 4
605 * checksum must be set to 0 in the L4 header by the caller.
608 * The pointer to the contiguous IPv6 header.
610 * The pointer to the beginning of the L4 header.
612 * The complemented checksum to set in the L4 header.
614 static inline uint16_t
615 rte_ipv6_udptcp_cksum(const struct rte_ipv6_hdr *ipv6_hdr, const void *l4_hdr)
617 uint16_t cksum = __rte_ipv6_udptcp_cksum(ipv6_hdr, l4_hdr);
622 * Per RFC 768: If the computed checksum is zero for UDP,
623 * it is transmitted as all ones
624 * (the equivalent in one's complement arithmetic).
626 if (cksum == 0 && ipv6_hdr->proto == IPPROTO_UDP)
633 * @internal Calculate the non-complemented IPv6 L4 checksum of a packet
635 static inline uint16_t
636 __rte_ipv6_udptcp_cksum_mbuf(const struct rte_mbuf *m,
637 const struct rte_ipv6_hdr *ipv6_hdr,
643 if (l4_off > m->pkt_len)
646 if (rte_raw_cksum_mbuf(m, l4_off, m->pkt_len - l4_off, &raw_cksum))
649 cksum = raw_cksum + rte_ipv6_phdr_cksum(ipv6_hdr, 0);
651 cksum = ((cksum & 0xffff0000) >> 16) + (cksum & 0xffff);
653 return (uint16_t)cksum;
658 * @b EXPERIMENTAL: this API may change without prior notice.
660 * Process the IPv6 UDP or TCP checksum of a packet.
662 * The IPv6 header must not be followed by extension headers. The layer 4
663 * checksum must be set to 0 in the L4 header by the caller.
666 * The pointer to the mbuf.
668 * The pointer to the contiguous IPv6 header.
670 * The offset in bytes to start L4 checksum.
672 * The complemented checksum to set in the L4 header.
675 static inline uint16_t
676 rte_ipv6_udptcp_cksum_mbuf(const struct rte_mbuf *m,
677 const struct rte_ipv6_hdr *ipv6_hdr, uint16_t l4_off)
679 uint16_t cksum = __rte_ipv6_udptcp_cksum_mbuf(m, ipv6_hdr, l4_off);
684 * Per RFC 768: If the computed checksum is zero for UDP,
685 * it is transmitted as all ones
686 * (the equivalent in one's complement arithmetic).
688 if (cksum == 0 && ipv6_hdr->proto == IPPROTO_UDP)
695 * Validate the IPv6 UDP or TCP checksum.
697 * In case of UDP, the caller must first check if udp_hdr->dgram_cksum is 0:
698 * this is either invalid or means no checksum in some situations. See 8.1
699 * (Upper-Layer Checksums) in RFC 8200.
702 * The pointer to the contiguous IPv6 header.
704 * The pointer to the beginning of the L4 header.
706 * Return 0 if the checksum is correct, else -1.
710 rte_ipv6_udptcp_cksum_verify(const struct rte_ipv6_hdr *ipv6_hdr,
713 uint16_t cksum = __rte_ipv6_udptcp_cksum(ipv6_hdr, l4_hdr);
723 * @b EXPERIMENTAL: this API may change without prior notice.
725 * Validate the IPv6 UDP or TCP checksum of a packet.
727 * In case of UDP, the caller must first check if udp_hdr->dgram_cksum is 0:
728 * this is either invalid or means no checksum in some situations. See 8.1
729 * (Upper-Layer Checksums) in RFC 8200.
732 * The pointer to the mbuf.
734 * The pointer to the contiguous IPv6 header.
736 * The offset in bytes to start L4 checksum.
738 * Return 0 if the checksum is correct, else -1.
742 rte_ipv6_udptcp_cksum_mbuf_verify(const struct rte_mbuf *m,
743 const struct rte_ipv6_hdr *ipv6_hdr,
746 uint16_t cksum = __rte_ipv6_udptcp_cksum_mbuf(m, ipv6_hdr, l4_off);
754 /** IPv6 fragment extension header. */
755 #define RTE_IPV6_EHDR_MF_SHIFT 0
756 #define RTE_IPV6_EHDR_MF_MASK 1
757 #define RTE_IPV6_EHDR_FO_SHIFT 3
758 #define RTE_IPV6_EHDR_FO_MASK (~((1 << RTE_IPV6_EHDR_FO_SHIFT) - 1))
759 #define RTE_IPV6_EHDR_FO_ALIGN (1 << RTE_IPV6_EHDR_FO_SHIFT)
761 #define RTE_IPV6_FRAG_USED_MASK (RTE_IPV6_EHDR_MF_MASK | RTE_IPV6_EHDR_FO_MASK)
763 #define RTE_IPV6_GET_MF(x) ((x) & RTE_IPV6_EHDR_MF_MASK)
764 #define RTE_IPV6_GET_FO(x) ((x) >> RTE_IPV6_EHDR_FO_SHIFT)
766 #define RTE_IPV6_SET_FRAG_DATA(fo, mf) \
767 (((fo) & RTE_IPV6_EHDR_FO_MASK) | ((mf) & RTE_IPV6_EHDR_MF_MASK))
769 struct rte_ipv6_fragment_ext {
770 uint8_t next_header; /**< Next header type */
771 uint8_t reserved; /**< Reserved */
772 rte_be16_t frag_data; /**< All fragmentation data */
773 rte_be32_t id; /**< Packet ID */
776 /* IPv6 fragment extension header size */
777 #define RTE_IPV6_FRAG_HDR_SIZE sizeof(struct rte_ipv6_fragment_ext)
780 * Parse next IPv6 header extension
782 * This function checks if proto number is an IPv6 extensions and parses its
783 * data if so, providing information on next header and extension length.
786 * Pointer to an extension raw data.
788 * Protocol number extracted from the "next header" field from
789 * the IPv6 header or the previous extension.
791 * Extension data length.
793 * next protocol number if proto is an IPv6 extension, -EINVAL otherwise
797 rte_ipv6_get_next_ext(const uint8_t *p, int proto, size_t *ext_len)
804 *ext_len = (*p + 2) * sizeof(uint32_t);
807 case IPPROTO_HOPOPTS:
808 case IPPROTO_ROUTING:
809 case IPPROTO_DSTOPTS:
811 *ext_len = (*p + 1) * sizeof(uint64_t);
814 case IPPROTO_FRAGMENT:
816 *ext_len = RTE_IPV6_FRAG_HDR_SIZE;
830 #endif /* _RTE_IP_H_ */