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>
32 uint8_t version_ihl; /**< version and header length */
33 uint8_t type_of_service; /**< type of service */
34 uint16_t total_length; /**< length of packet */
35 uint16_t packet_id; /**< packet ID */
36 uint16_t fragment_offset; /**< fragmentation offset */
37 uint8_t time_to_live; /**< time to live */
38 uint8_t next_proto_id; /**< protocol ID */
39 uint16_t hdr_checksum; /**< header checksum */
40 uint32_t src_addr; /**< source address */
41 uint32_t dst_addr; /**< destination address */
42 } __attribute__((__packed__));
44 /** Create IPv4 address */
45 #define RTE_IPV4(a, b, c, d) ((uint32_t)(((a) & 0xff) << 24) | \
46 (((b) & 0xff) << 16) | \
47 (((c) & 0xff) << 8) | \
50 /** Maximal IPv4 packet length (including a header) */
51 #define RTE_IPV4_MAX_PKT_LEN 65535
53 /** Internet header length mask for version_ihl field */
54 #define RTE_IPV4_HDR_IHL_MASK (0x0f)
56 * Internet header length field multiplier (IHL field specifies overall header
57 * length in number of 4-byte words)
59 #define RTE_IPV4_IHL_MULTIPLIER (4)
61 /* Fragment Offset * Flags. */
62 #define RTE_IPV4_HDR_DF_SHIFT 14
63 #define RTE_IPV4_HDR_MF_SHIFT 13
64 #define RTE_IPV4_HDR_FO_SHIFT 3
66 #define RTE_IPV4_HDR_DF_FLAG (1 << RTE_IPV4_HDR_DF_SHIFT)
67 #define RTE_IPV4_HDR_MF_FLAG (1 << RTE_IPV4_HDR_MF_SHIFT)
69 #define RTE_IPV4_HDR_OFFSET_MASK ((1 << RTE_IPV4_HDR_MF_SHIFT) - 1)
71 #define RTE_IPV4_HDR_OFFSET_UNITS 8
76 #define RTE_IPV4_ANY ((uint32_t)0x00000000) /**< 0.0.0.0 */
77 #define RTE_IPV4_LOOPBACK ((uint32_t)0x7f000001) /**< 127.0.0.1 */
78 #define RTE_IPV4_BROADCAST ((uint32_t)0xe0000000) /**< 224.0.0.0 */
79 #define RTE_IPV4_ALLHOSTS_GROUP ((uint32_t)0xe0000001) /**< 224.0.0.1 */
80 #define RTE_IPV4_ALLRTRS_GROUP ((uint32_t)0xe0000002) /**< 224.0.0.2 */
81 #define RTE_IPV4_MAX_LOCAL_GROUP ((uint32_t)0xe00000ff) /**< 224.0.0.255 */
84 * IPv4 Multicast-related macros
86 #define RTE_IPV4_MIN_MCAST \
87 RTE_IPV4(224, 0, 0, 0) /**< Minimal IPv4-multicast address */
88 #define RTE_IPV4_MAX_MCAST \
89 RTE_IPV4(239, 255, 255, 255) /**< Maximum IPv4 multicast address */
91 #define RTE_IS_IPV4_MCAST(x) \
92 ((x) >= RTE_IPV4_MIN_MCAST && (x) <= RTE_IPV4_MAX_MCAST)
93 /**< check if IPv4 address is multicast */
96 * @internal Calculate a sum of all words in the buffer.
97 * Helper routine for the rte_raw_cksum().
100 * Pointer to the buffer.
102 * Length of the buffer.
104 * Initial value of the sum.
106 * sum += Sum of all words in the buffer.
108 static inline uint32_t
109 __rte_raw_cksum(const void *buf, size_t len, uint32_t sum)
111 /* workaround gcc strict-aliasing warning */
112 uintptr_t ptr = (uintptr_t)buf;
113 typedef uint16_t __attribute__((__may_alias__)) u16_p;
114 const u16_p *u16_buf = (const u16_p *)ptr;
116 while (len >= (sizeof(*u16_buf) * 4)) {
121 len -= sizeof(*u16_buf) * 4;
124 while (len >= sizeof(*u16_buf)) {
126 len -= sizeof(*u16_buf);
130 /* if length is in odd bytes */
132 sum += *((const uint8_t *)u16_buf);
138 * @internal Reduce a sum to the non-complemented checksum.
139 * Helper routine for the rte_raw_cksum().
144 * The non-complemented checksum.
146 static inline uint16_t
147 __rte_raw_cksum_reduce(uint32_t sum)
149 sum = ((sum & 0xffff0000) >> 16) + (sum & 0xffff);
150 sum = ((sum & 0xffff0000) >> 16) + (sum & 0xffff);
151 return (uint16_t)sum;
155 * Process the non-complemented checksum of a buffer.
158 * Pointer to the buffer.
160 * Length of the buffer.
162 * The non-complemented checksum.
164 static inline uint16_t
165 rte_raw_cksum(const void *buf, size_t len)
169 sum = __rte_raw_cksum(buf, len, 0);
170 return __rte_raw_cksum_reduce(sum);
174 * Compute the raw (non complemented) checksum of a packet.
177 * The pointer to the mbuf.
179 * The offset in bytes to start the checksum.
181 * The length in bytes of the data to checksum.
183 * A pointer to the checksum, filled on success.
185 * 0 on success, -1 on error (bad length or offset).
188 rte_raw_cksum_mbuf(const struct rte_mbuf *m, uint32_t off, uint32_t len,
191 const struct rte_mbuf *seg;
194 uint32_t seglen, done;
196 /* easy case: all data in the first segment */
197 if (off + len <= rte_pktmbuf_data_len(m)) {
198 *cksum = rte_raw_cksum(rte_pktmbuf_mtod_offset(m,
199 const char *, off), len);
203 if (unlikely(off + len > rte_pktmbuf_pkt_len(m)))
204 return -1; /* invalid params, return a dummy value */
206 /* else browse the segment to find offset */
208 for (seg = m; seg != NULL; seg = seg->next) {
209 seglen = rte_pktmbuf_data_len(seg);
215 buf = rte_pktmbuf_mtod_offset(seg, const char *, off);
217 /* all in one segment */
218 *cksum = rte_raw_cksum(buf, len);
222 /* hard case: process checksum of several segments */
226 tmp = __rte_raw_cksum(buf, seglen, 0);
228 tmp = rte_bswap16((uint16_t)tmp);
234 buf = rte_pktmbuf_mtod(seg, const char *);
235 seglen = rte_pktmbuf_data_len(seg);
236 if (seglen > len - done)
240 *cksum = __rte_raw_cksum_reduce(sum);
245 * Process the IPv4 checksum of an IPv4 header.
247 * The checksum field must be set to 0 by the caller.
250 * The pointer to the contiguous IPv4 header.
252 * The complemented checksum to set in the IP packet.
254 static inline uint16_t
255 rte_ipv4_cksum(const struct rte_ipv4_hdr *ipv4_hdr)
258 cksum = rte_raw_cksum(ipv4_hdr, sizeof(struct rte_ipv4_hdr));
259 return (cksum == 0xffff) ? cksum : (uint16_t)~cksum;
263 * Process the pseudo-header checksum of an IPv4 header.
265 * The checksum field must be set to 0 by the caller.
267 * Depending on the ol_flags, the pseudo-header checksum expected by the
268 * drivers is not the same. For instance, when TSO is enabled, the IP
269 * payload length must not be included in the packet.
271 * When ol_flags is 0, it computes the standard pseudo-header checksum.
274 * The pointer to the contiguous IPv4 header.
276 * The ol_flags of the associated mbuf.
278 * The non-complemented checksum to set in the L4 header.
280 static inline uint16_t
281 rte_ipv4_phdr_cksum(const struct rte_ipv4_hdr *ipv4_hdr, uint64_t ol_flags)
283 struct ipv4_psd_header {
284 uint32_t src_addr; /* IP address of source host. */
285 uint32_t dst_addr; /* IP address of destination host. */
286 uint8_t zero; /* zero. */
287 uint8_t proto; /* L4 protocol type. */
288 uint16_t len; /* L4 length. */
291 psd_hdr.src_addr = ipv4_hdr->src_addr;
292 psd_hdr.dst_addr = ipv4_hdr->dst_addr;
294 psd_hdr.proto = ipv4_hdr->next_proto_id;
295 if (ol_flags & PKT_TX_TCP_SEG) {
298 psd_hdr.len = rte_cpu_to_be_16(
299 (uint16_t)(rte_be_to_cpu_16(ipv4_hdr->total_length)
300 - sizeof(struct rte_ipv4_hdr)));
302 return rte_raw_cksum(&psd_hdr, sizeof(psd_hdr));
306 * Process the IPv4 UDP or TCP checksum.
308 * The IPv4 header should not contains options. The IP and layer 4
309 * checksum must be set to 0 in the packet by the caller.
312 * The pointer to the contiguous IPv4 header.
314 * The pointer to the beginning of the L4 header.
316 * 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)
323 uint32_t l3_len, l4_len;
325 l3_len = rte_be_to_cpu_16(ipv4_hdr->total_length);
326 if (l3_len < sizeof(struct rte_ipv4_hdr))
329 l4_len = l3_len - sizeof(struct rte_ipv4_hdr);
331 cksum = rte_raw_cksum(l4_hdr, l4_len);
332 cksum += rte_ipv4_phdr_cksum(ipv4_hdr, 0);
334 cksum = ((cksum & 0xffff0000) >> 16) + (cksum & 0xffff);
335 cksum = (~cksum) & 0xffff;
339 return (uint16_t)cksum;
345 struct rte_ipv6_hdr {
346 uint32_t vtc_flow; /**< IP version, traffic class & flow label. */
347 uint16_t payload_len; /**< IP packet length - includes sizeof(ip_header). */
348 uint8_t proto; /**< Protocol, next header. */
349 uint8_t hop_limits; /**< Hop limits. */
350 uint8_t src_addr[16]; /**< IP address of source host. */
351 uint8_t dst_addr[16]; /**< IP address of destination host(s). */
352 } __attribute__((__packed__));
354 /* IPv6 vtc_flow: IPv / TC / flow_label */
355 #define RTE_IPV6_HDR_FL_SHIFT 0
356 #define RTE_IPV6_HDR_TC_SHIFT 20
357 #define RTE_IPV6_HDR_FL_MASK ((1u << RTE_IPV6_HDR_TC_SHIFT) - 1)
358 #define RTE_IPV6_HDR_TC_MASK (0xf << RTE_IPV6_HDR_TC_SHIFT)
361 * Process the pseudo-header checksum of an IPv6 header.
363 * Depending on the ol_flags, the pseudo-header checksum expected by the
364 * drivers is not the same. For instance, when TSO is enabled, the IPv6
365 * payload length must not be included in the packet.
367 * When ol_flags is 0, it computes the standard pseudo-header checksum.
370 * The pointer to the contiguous IPv6 header.
372 * The ol_flags of the associated mbuf.
374 * The non-complemented checksum to set in the L4 header.
376 static inline uint16_t
377 rte_ipv6_phdr_cksum(const struct rte_ipv6_hdr *ipv6_hdr, uint64_t ol_flags)
381 uint32_t len; /* L4 length. */
382 uint32_t proto; /* L4 protocol - top 3 bytes must be zero */
385 psd_hdr.proto = (uint32_t)(ipv6_hdr->proto << 24);
386 if (ol_flags & PKT_TX_TCP_SEG) {
389 psd_hdr.len = ipv6_hdr->payload_len;
392 sum = __rte_raw_cksum(ipv6_hdr->src_addr,
393 sizeof(ipv6_hdr->src_addr) + sizeof(ipv6_hdr->dst_addr),
395 sum = __rte_raw_cksum(&psd_hdr, sizeof(psd_hdr), sum);
396 return __rte_raw_cksum_reduce(sum);
400 * Process the IPv6 UDP or TCP checksum.
402 * The IPv4 header should not contains options. The layer 4 checksum
403 * must be set to 0 in the packet by the caller.
406 * The pointer to the contiguous IPv6 header.
408 * The pointer to the beginning of the L4 header.
410 * The complemented checksum to set in the IP packet.
412 static inline uint16_t
413 rte_ipv6_udptcp_cksum(const struct rte_ipv6_hdr *ipv6_hdr, const void *l4_hdr)
418 l4_len = rte_be_to_cpu_16(ipv6_hdr->payload_len);
420 cksum = rte_raw_cksum(l4_hdr, l4_len);
421 cksum += rte_ipv6_phdr_cksum(ipv6_hdr, 0);
423 cksum = ((cksum & 0xffff0000) >> 16) + (cksum & 0xffff);
424 cksum = (~cksum) & 0xffff;
428 return (uint16_t)cksum;
435 #endif /* _RTE_IP_H_ */