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 <netinet/in.h>
21 #include <rte_byteorder.h>
24 #ifndef RTE_NET_NO_COMPAT
25 #include <rte_ip_compat.h>
36 uint8_t version_ihl; /**< version and header length */
37 uint8_t type_of_service; /**< type of service */
38 uint16_t total_length; /**< length of packet */
39 uint16_t packet_id; /**< packet ID */
40 uint16_t fragment_offset; /**< fragmentation offset */
41 uint8_t time_to_live; /**< time to live */
42 uint8_t next_proto_id; /**< protocol ID */
43 uint16_t hdr_checksum; /**< header checksum */
44 uint32_t src_addr; /**< source address */
45 uint32_t dst_addr; /**< destination address */
46 } __attribute__((__packed__));
48 /** Create IPv4 address */
49 #define RTE_IPv4(a,b,c,d) ((uint32_t)(((a) & 0xff) << 24) | \
50 (((b) & 0xff) << 16) | \
51 (((c) & 0xff) << 8) | \
54 /** Maximal IPv4 packet length (including a header) */
55 #define RTE_IPV4_MAX_PKT_LEN 65535
57 /** Internet header length mask for version_ihl field */
58 #define RTE_IPV4_HDR_IHL_MASK (0x0f)
60 * Internet header length field multiplier (IHL field specifies overall header
61 * length in number of 4-byte words)
63 #define RTE_IPV4_IHL_MULTIPLIER (4)
65 /* Fragment Offset * Flags. */
66 #define RTE_IPV4_HDR_DF_SHIFT 14
67 #define RTE_IPV4_HDR_MF_SHIFT 13
68 #define RTE_IPV4_HDR_FO_SHIFT 3
70 #define RTE_IPV4_HDR_DF_FLAG (1 << RTE_IPV4_HDR_DF_SHIFT)
71 #define RTE_IPV4_HDR_MF_FLAG (1 << RTE_IPV4_HDR_MF_SHIFT)
73 #define RTE_IPV4_HDR_OFFSET_MASK ((1 << RTE_IPV4_HDR_MF_SHIFT) - 1)
75 #define RTE_IPV4_HDR_OFFSET_UNITS 8
80 #define RTE_IPV4_ANY ((uint32_t)0x00000000) /**< 0.0.0.0 */
81 #define RTE_IPV4_LOOPBACK ((uint32_t)0x7f000001) /**< 127.0.0.1 */
82 #define RTE_IPV4_BROADCAST ((uint32_t)0xe0000000) /**< 224.0.0.0 */
83 #define RTE_IPV4_ALLHOSTS_GROUP ((uint32_t)0xe0000001) /**< 224.0.0.1 */
84 #define RTE_IPV4_ALLRTRS_GROUP ((uint32_t)0xe0000002) /**< 224.0.0.2 */
85 #define RTE_IPV4_MAX_LOCAL_GROUP ((uint32_t)0xe00000ff) /**< 224.0.0.255 */
88 * IPv4 Multicast-related macros
90 #define RTE_IPV4_MIN_MCAST RTE_IPv4(224, 0, 0, 0) /**< Minimal IPv4-multicast address */
91 #define RTE_IPV4_MAX_MCAST RTE_IPv4(239, 255, 255, 255) /**< Maximum IPv4 multicast address */
93 #define RTE_IS_IPV4_MCAST(x) \
94 ((x) >= RTE_IPV4_MIN_MCAST && (x) <= RTE_IPV4_MAX_MCAST) /**< check if IPv4 address is multicast */
97 * @internal Calculate a sum of all words in the buffer.
98 * Helper routine for the rte_raw_cksum().
101 * Pointer to the buffer.
103 * Length of the buffer.
105 * Initial value of the sum.
107 * sum += Sum of all words in the buffer.
109 static inline uint32_t
110 __rte_raw_cksum(const void *buf, size_t len, uint32_t sum)
112 /* workaround gcc strict-aliasing warning */
113 uintptr_t ptr = (uintptr_t)buf;
114 typedef uint16_t __attribute__((__may_alias__)) u16_p;
115 const u16_p *u16_buf = (const u16_p *)ptr;
117 while (len >= (sizeof(*u16_buf) * 4)) {
122 len -= sizeof(*u16_buf) * 4;
125 while (len >= sizeof(*u16_buf)) {
127 len -= sizeof(*u16_buf);
131 /* if length is in odd bytes */
133 sum += *((const uint8_t *)u16_buf);
139 * @internal Reduce a sum to the non-complemented checksum.
140 * Helper routine for the rte_raw_cksum().
145 * The non-complemented checksum.
147 static inline uint16_t
148 __rte_raw_cksum_reduce(uint32_t sum)
150 sum = ((sum & 0xffff0000) >> 16) + (sum & 0xffff);
151 sum = ((sum & 0xffff0000) >> 16) + (sum & 0xffff);
152 return (uint16_t)sum;
156 * Process the non-complemented checksum of a buffer.
159 * Pointer to the buffer.
161 * Length of the buffer.
163 * The non-complemented checksum.
165 static inline uint16_t
166 rte_raw_cksum(const void *buf, size_t len)
170 sum = __rte_raw_cksum(buf, len, 0);
171 return __rte_raw_cksum_reduce(sum);
175 * Compute the raw (non complemented) checksum of a packet.
178 * The pointer to the mbuf.
180 * The offset in bytes to start the checksum.
182 * The length in bytes of the data to checksum.
184 * A pointer to the checksum, filled on success.
186 * 0 on success, -1 on error (bad length or offset).
189 rte_raw_cksum_mbuf(const struct rte_mbuf *m, uint32_t off, uint32_t len,
192 const struct rte_mbuf *seg;
195 uint32_t seglen, done;
197 /* easy case: all data in the first segment */
198 if (off + len <= rte_pktmbuf_data_len(m)) {
199 *cksum = rte_raw_cksum(rte_pktmbuf_mtod_offset(m,
200 const char *, off), len);
204 if (unlikely(off + len > rte_pktmbuf_pkt_len(m)))
205 return -1; /* invalid params, return a dummy value */
207 /* else browse the segment to find offset */
209 for (seg = m; seg != NULL; seg = seg->next) {
210 seglen = rte_pktmbuf_data_len(seg);
216 buf = rte_pktmbuf_mtod_offset(seg, const char *, off);
218 /* all in one segment */
219 *cksum = rte_raw_cksum(buf, len);
223 /* hard case: process checksum of several segments */
227 tmp = __rte_raw_cksum(buf, seglen, 0);
229 tmp = rte_bswap16((uint16_t)tmp);
235 buf = rte_pktmbuf_mtod(seg, const char *);
236 seglen = rte_pktmbuf_data_len(seg);
237 if (seglen > len - done)
241 *cksum = __rte_raw_cksum_reduce(sum);
246 * Process the IPv4 checksum of an IPv4 header.
248 * The checksum field must be set to 0 by the caller.
251 * The pointer to the contiguous IPv4 header.
253 * The complemented checksum to set in the IP packet.
255 static inline uint16_t
256 rte_ipv4_cksum(const struct rte_ipv4_hdr *ipv4_hdr)
259 cksum = rte_raw_cksum(ipv4_hdr, sizeof(struct rte_ipv4_hdr));
260 return (cksum == 0xffff) ? cksum : (uint16_t)~cksum;
264 * Process the pseudo-header checksum of an IPv4 header.
266 * The checksum field must be set to 0 by the caller.
268 * Depending on the ol_flags, the pseudo-header checksum expected by the
269 * drivers is not the same. For instance, when TSO is enabled, the IP
270 * payload length must not be included in the packet.
272 * When ol_flags is 0, it computes the standard pseudo-header checksum.
275 * The pointer to the contiguous IPv4 header.
277 * The ol_flags of the associated mbuf.
279 * The non-complemented checksum to set in the L4 header.
281 static inline uint16_t
282 rte_ipv4_phdr_cksum(const struct rte_ipv4_hdr *ipv4_hdr, uint64_t ol_flags)
284 struct ipv4_psd_header {
285 uint32_t src_addr; /* IP address of source host. */
286 uint32_t dst_addr; /* IP address of destination host. */
287 uint8_t zero; /* zero. */
288 uint8_t proto; /* L4 protocol type. */
289 uint16_t len; /* L4 length. */
292 psd_hdr.src_addr = ipv4_hdr->src_addr;
293 psd_hdr.dst_addr = ipv4_hdr->dst_addr;
295 psd_hdr.proto = ipv4_hdr->next_proto_id;
296 if (ol_flags & PKT_TX_TCP_SEG) {
299 psd_hdr.len = rte_cpu_to_be_16(
300 (uint16_t)(rte_be_to_cpu_16(ipv4_hdr->total_length)
301 - sizeof(struct rte_ipv4_hdr)));
303 return rte_raw_cksum(&psd_hdr, sizeof(psd_hdr));
307 * Process the IPv4 UDP or TCP checksum.
309 * The IPv4 header should not contains options. The IP and layer 4
310 * checksum must be set to 0 in the packet by the caller.
313 * The pointer to the contiguous IPv4 header.
315 * The pointer to the beginning of the L4 header.
317 * The complemented checksum to set in the IP packet.
319 static inline uint16_t
320 rte_ipv4_udptcp_cksum(const struct rte_ipv4_hdr *ipv4_hdr, const void *l4_hdr)
325 l4_len = (uint32_t)(rte_be_to_cpu_16(ipv4_hdr->total_length) -
326 sizeof(struct rte_ipv4_hdr));
328 cksum = rte_raw_cksum(l4_hdr, l4_len);
329 cksum += rte_ipv4_phdr_cksum(ipv4_hdr, 0);
331 cksum = ((cksum & 0xffff0000) >> 16) + (cksum & 0xffff);
332 cksum = (~cksum) & 0xffff;
336 return (uint16_t)cksum;
342 struct rte_ipv6_hdr {
343 uint32_t vtc_flow; /**< IP version, traffic class & flow label. */
344 uint16_t payload_len; /**< IP packet length - includes sizeof(ip_header). */
345 uint8_t proto; /**< Protocol, next header. */
346 uint8_t hop_limits; /**< Hop limits. */
347 uint8_t src_addr[16]; /**< IP address of source host. */
348 uint8_t dst_addr[16]; /**< IP address of destination host(s). */
349 } __attribute__((__packed__));
351 /* IPv6 vtc_flow: IPv / TC / flow_label */
352 #define RTE_IPV6_HDR_FL_SHIFT 0
353 #define RTE_IPV6_HDR_TC_SHIFT 20
354 #define RTE_IPV6_HDR_FL_MASK ((1u << RTE_IPV6_HDR_TC_SHIFT) - 1)
355 #define RTE_IPV6_HDR_TC_MASK (0xf << RTE_IPV6_HDR_TC_SHIFT)
358 * Process the pseudo-header checksum of an IPv6 header.
360 * Depending on the ol_flags, the pseudo-header checksum expected by the
361 * drivers is not the same. For instance, when TSO is enabled, the IPv6
362 * payload length must not be included in the packet.
364 * When ol_flags is 0, it computes the standard pseudo-header checksum.
367 * The pointer to the contiguous IPv6 header.
369 * The ol_flags of the associated mbuf.
371 * The non-complemented checksum to set in the L4 header.
373 static inline uint16_t
374 rte_ipv6_phdr_cksum(const struct rte_ipv6_hdr *ipv6_hdr, uint64_t ol_flags)
378 uint32_t len; /* L4 length. */
379 uint32_t proto; /* L4 protocol - top 3 bytes must be zero */
382 psd_hdr.proto = (uint32_t)(ipv6_hdr->proto << 24);
383 if (ol_flags & PKT_TX_TCP_SEG) {
386 psd_hdr.len = ipv6_hdr->payload_len;
389 sum = __rte_raw_cksum(ipv6_hdr->src_addr,
390 sizeof(ipv6_hdr->src_addr) + sizeof(ipv6_hdr->dst_addr),
392 sum = __rte_raw_cksum(&psd_hdr, sizeof(psd_hdr), sum);
393 return __rte_raw_cksum_reduce(sum);
397 * Process the IPv6 UDP or TCP checksum.
399 * The IPv4 header should not contains options. The layer 4 checksum
400 * must be set to 0 in the packet by the caller.
403 * The pointer to the contiguous IPv6 header.
405 * The pointer to the beginning of the L4 header.
407 * The complemented checksum to set in the IP packet.
409 static inline uint16_t
410 rte_ipv6_udptcp_cksum(const struct rte_ipv6_hdr *ipv6_hdr, const void *l4_hdr)
415 l4_len = rte_be_to_cpu_16(ipv6_hdr->payload_len);
417 cksum = rte_raw_cksum(l4_hdr, l4_len);
418 cksum += rte_ipv6_phdr_cksum(ipv6_hdr, 0);
420 cksum = ((cksum & 0xffff0000) >> 16) + (cksum & 0xffff);
421 cksum = (~cksum) & 0xffff;
425 return (uint16_t)cksum;
432 #endif /* _RTE_IP_H_ */