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 uint16_t total_length; /**< length of packet */
37 uint16_t packet_id; /**< packet ID */
38 uint16_t fragment_offset; /**< fragmentation offset */
39 uint8_t time_to_live; /**< time to live */
40 uint8_t next_proto_id; /**< protocol ID */
41 uint16_t hdr_checksum; /**< header checksum */
42 uint32_t src_addr; /**< source address */
43 uint32_t dst_addr; /**< destination address */
44 } __attribute__((__packed__));
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)
67 /* Fragment Offset * Flags. */
68 #define RTE_IPV4_HDR_DF_SHIFT 14
69 #define RTE_IPV4_HDR_MF_SHIFT 13
70 #define RTE_IPV4_HDR_FO_SHIFT 3
72 #define RTE_IPV4_HDR_DF_FLAG (1 << RTE_IPV4_HDR_DF_SHIFT)
73 #define RTE_IPV4_HDR_MF_FLAG (1 << RTE_IPV4_HDR_MF_SHIFT)
75 #define RTE_IPV4_HDR_OFFSET_MASK ((1 << RTE_IPV4_HDR_MF_SHIFT) - 1)
77 #define RTE_IPV4_HDR_OFFSET_UNITS 8
82 #define RTE_IPV4_ANY ((uint32_t)0x00000000) /**< 0.0.0.0 */
83 #define RTE_IPV4_LOOPBACK ((uint32_t)0x7f000001) /**< 127.0.0.1 */
84 #define RTE_IPV4_BROADCAST ((uint32_t)0xe0000000) /**< 224.0.0.0 */
85 #define RTE_IPV4_ALLHOSTS_GROUP ((uint32_t)0xe0000001) /**< 224.0.0.1 */
86 #define RTE_IPV4_ALLRTRS_GROUP ((uint32_t)0xe0000002) /**< 224.0.0.2 */
87 #define RTE_IPV4_MAX_LOCAL_GROUP ((uint32_t)0xe00000ff) /**< 224.0.0.255 */
90 * IPv4 Multicast-related macros
92 #define RTE_IPV4_MIN_MCAST \
93 RTE_IPV4(224, 0, 0, 0) /**< Minimal IPv4-multicast address */
94 #define RTE_IPV4_MAX_MCAST \
95 RTE_IPV4(239, 255, 255, 255) /**< Maximum IPv4 multicast address */
97 #define RTE_IS_IPV4_MCAST(x) \
98 ((x) >= RTE_IPV4_MIN_MCAST && (x) <= RTE_IPV4_MAX_MCAST)
99 /**< check if IPv4 address is multicast */
101 /* IPv4 default fields values */
102 #define RTE_IPV4_MIN_IHL (0x5)
103 #define RTE_IPV4_VHL_DEF (IPVERSION | RTE_IPV4_MIN_IHL)
106 * @internal Calculate a sum of all words in the buffer.
107 * Helper routine for the rte_raw_cksum().
110 * Pointer to the buffer.
112 * Length of the buffer.
114 * Initial value of the sum.
116 * sum += Sum of all words in the buffer.
118 static inline uint32_t
119 __rte_raw_cksum(const void *buf, size_t len, uint32_t sum)
121 /* workaround gcc strict-aliasing warning */
122 uintptr_t ptr = (uintptr_t)buf;
123 typedef uint16_t __attribute__((__may_alias__)) u16_p;
124 const u16_p *u16_buf = (const u16_p *)ptr;
126 while (len >= (sizeof(*u16_buf) * 4)) {
131 len -= sizeof(*u16_buf) * 4;
134 while (len >= sizeof(*u16_buf)) {
136 len -= sizeof(*u16_buf);
140 /* if length is in odd bytes */
142 sum += *((const uint8_t *)u16_buf);
148 * @internal Reduce a sum to the non-complemented checksum.
149 * Helper routine for the rte_raw_cksum().
154 * The non-complemented checksum.
156 static inline uint16_t
157 __rte_raw_cksum_reduce(uint32_t sum)
159 sum = ((sum & 0xffff0000) >> 16) + (sum & 0xffff);
160 sum = ((sum & 0xffff0000) >> 16) + (sum & 0xffff);
161 return (uint16_t)sum;
165 * Process the non-complemented checksum of a buffer.
168 * Pointer to the buffer.
170 * Length of the buffer.
172 * The non-complemented checksum.
174 static inline uint16_t
175 rte_raw_cksum(const void *buf, size_t len)
179 sum = __rte_raw_cksum(buf, len, 0);
180 return __rte_raw_cksum_reduce(sum);
184 * Compute the raw (non complemented) checksum of a packet.
187 * The pointer to the mbuf.
189 * The offset in bytes to start the checksum.
191 * The length in bytes of the data to checksum.
193 * A pointer to the checksum, filled on success.
195 * 0 on success, -1 on error (bad length or offset).
198 rte_raw_cksum_mbuf(const struct rte_mbuf *m, uint32_t off, uint32_t len,
201 const struct rte_mbuf *seg;
204 uint32_t seglen, done;
206 /* easy case: all data in the first segment */
207 if (off + len <= rte_pktmbuf_data_len(m)) {
208 *cksum = rte_raw_cksum(rte_pktmbuf_mtod_offset(m,
209 const char *, off), len);
213 if (unlikely(off + len > rte_pktmbuf_pkt_len(m)))
214 return -1; /* invalid params, return a dummy value */
216 /* else browse the segment to find offset */
218 for (seg = m; seg != NULL; seg = seg->next) {
219 seglen = rte_pktmbuf_data_len(seg);
225 buf = rte_pktmbuf_mtod_offset(seg, const char *, off);
227 /* all in one segment */
228 *cksum = rte_raw_cksum(buf, len);
232 /* hard case: process checksum of several segments */
236 tmp = __rte_raw_cksum(buf, seglen, 0);
238 tmp = rte_bswap16((uint16_t)tmp);
244 buf = rte_pktmbuf_mtod(seg, const char *);
245 seglen = rte_pktmbuf_data_len(seg);
246 if (seglen > len - done)
250 *cksum = __rte_raw_cksum_reduce(sum);
255 * Process the IPv4 checksum of an IPv4 header.
257 * The checksum field must be set to 0 by the caller.
260 * The pointer to the contiguous IPv4 header.
262 * The complemented checksum to set in the IP packet.
264 static inline uint16_t
265 rte_ipv4_cksum(const struct rte_ipv4_hdr *ipv4_hdr)
268 cksum = rte_raw_cksum(ipv4_hdr, sizeof(struct rte_ipv4_hdr));
269 return (cksum == 0xffff) ? cksum : (uint16_t)~cksum;
273 * Process the pseudo-header checksum of an IPv4 header.
275 * The checksum field must be set to 0 by the caller.
277 * Depending on the ol_flags, the pseudo-header checksum expected by the
278 * drivers is not the same. For instance, when TSO is enabled, the IP
279 * payload length must not be included in the packet.
281 * When ol_flags is 0, it computes the standard pseudo-header checksum.
284 * The pointer to the contiguous IPv4 header.
286 * The ol_flags of the associated mbuf.
288 * The non-complemented checksum to set in the L4 header.
290 static inline uint16_t
291 rte_ipv4_phdr_cksum(const struct rte_ipv4_hdr *ipv4_hdr, uint64_t ol_flags)
293 struct ipv4_psd_header {
294 uint32_t src_addr; /* IP address of source host. */
295 uint32_t dst_addr; /* IP address of destination host. */
296 uint8_t zero; /* zero. */
297 uint8_t proto; /* L4 protocol type. */
298 uint16_t len; /* L4 length. */
301 psd_hdr.src_addr = ipv4_hdr->src_addr;
302 psd_hdr.dst_addr = ipv4_hdr->dst_addr;
304 psd_hdr.proto = ipv4_hdr->next_proto_id;
305 if (ol_flags & PKT_TX_TCP_SEG) {
308 psd_hdr.len = rte_cpu_to_be_16(
309 (uint16_t)(rte_be_to_cpu_16(ipv4_hdr->total_length)
310 - sizeof(struct rte_ipv4_hdr)));
312 return rte_raw_cksum(&psd_hdr, sizeof(psd_hdr));
316 * Process the IPv4 UDP or TCP checksum.
318 * The IPv4 header should not contains options. The IP and layer 4
319 * checksum must be set to 0 in the packet by the caller.
322 * The pointer to the contiguous IPv4 header.
324 * The pointer to the beginning of the L4 header.
326 * The complemented checksum to set in the IP packet
329 static inline uint16_t
330 rte_ipv4_udptcp_cksum(const struct rte_ipv4_hdr *ipv4_hdr, const void *l4_hdr)
333 uint32_t l3_len, l4_len;
335 l3_len = rte_be_to_cpu_16(ipv4_hdr->total_length);
336 if (l3_len < sizeof(struct rte_ipv4_hdr))
339 l4_len = l3_len - sizeof(struct rte_ipv4_hdr);
341 cksum = rte_raw_cksum(l4_hdr, l4_len);
342 cksum += rte_ipv4_phdr_cksum(ipv4_hdr, 0);
344 cksum = ((cksum & 0xffff0000) >> 16) + (cksum & 0xffff);
345 cksum = (~cksum) & 0xffff;
349 return (uint16_t)cksum;
355 struct rte_ipv6_hdr {
356 uint32_t vtc_flow; /**< IP version, traffic class & flow label. */
357 uint16_t payload_len; /**< IP packet length - includes sizeof(ip_header). */
358 uint8_t proto; /**< Protocol, next header. */
359 uint8_t hop_limits; /**< Hop limits. */
360 uint8_t src_addr[16]; /**< IP address of source host. */
361 uint8_t dst_addr[16]; /**< IP address of destination host(s). */
362 } __attribute__((__packed__));
364 /* IPv6 vtc_flow: IPv / TC / flow_label */
365 #define RTE_IPV6_HDR_FL_SHIFT 0
366 #define RTE_IPV6_HDR_TC_SHIFT 20
367 #define RTE_IPV6_HDR_FL_MASK ((1u << RTE_IPV6_HDR_TC_SHIFT) - 1)
368 #define RTE_IPV6_HDR_TC_MASK (0xff << RTE_IPV6_HDR_TC_SHIFT)
369 #define RTE_IPV6_HDR_DSCP_MASK (0xfc << RTE_IPV6_HDR_TC_SHIFT)
370 #define RTE_IPV6_HDR_ECN_MASK (0x03 << RTE_IPV6_HDR_TC_SHIFT)
373 * Process the pseudo-header checksum of an IPv6 header.
375 * Depending on the ol_flags, the pseudo-header checksum expected by the
376 * drivers is not the same. For instance, when TSO is enabled, the IPv6
377 * payload length must not be included in the packet.
379 * When ol_flags is 0, it computes the standard pseudo-header checksum.
382 * The pointer to the contiguous IPv6 header.
384 * The ol_flags of the associated mbuf.
386 * The non-complemented checksum to set in the L4 header.
388 static inline uint16_t
389 rte_ipv6_phdr_cksum(const struct rte_ipv6_hdr *ipv6_hdr, uint64_t ol_flags)
393 uint32_t len; /* L4 length. */
394 uint32_t proto; /* L4 protocol - top 3 bytes must be zero */
397 psd_hdr.proto = (uint32_t)(ipv6_hdr->proto << 24);
398 if (ol_flags & PKT_TX_TCP_SEG) {
401 psd_hdr.len = ipv6_hdr->payload_len;
404 sum = __rte_raw_cksum(ipv6_hdr->src_addr,
405 sizeof(ipv6_hdr->src_addr) + sizeof(ipv6_hdr->dst_addr),
407 sum = __rte_raw_cksum(&psd_hdr, sizeof(psd_hdr), sum);
408 return __rte_raw_cksum_reduce(sum);
412 * Process the IPv6 UDP or TCP checksum.
414 * The IPv4 header should not contains options. The layer 4 checksum
415 * must be set to 0 in the packet by the caller.
418 * The pointer to the contiguous IPv6 header.
420 * The pointer to the beginning of the L4 header.
422 * The complemented checksum to set in the IP packet.
424 static inline uint16_t
425 rte_ipv6_udptcp_cksum(const struct rte_ipv6_hdr *ipv6_hdr, const void *l4_hdr)
430 l4_len = rte_be_to_cpu_16(ipv6_hdr->payload_len);
432 cksum = rte_raw_cksum(l4_hdr, l4_len);
433 cksum += rte_ipv6_phdr_cksum(ipv6_hdr, 0);
435 cksum = ((cksum & 0xffff0000) >> 16) + (cksum & 0xffff);
436 cksum = (~cksum) & 0xffff;
440 return (uint16_t)cksum;
447 #endif /* _RTE_IP_H_ */