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 /* Type of Service fields */
62 #define RTE_IPV4_HDR_DSCP_MASK (0xfc)
63 #define RTE_IPV4_HDR_ECN_MASK (0x03)
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 \
91 RTE_IPV4(224, 0, 0, 0) /**< Minimal IPv4-multicast address */
92 #define RTE_IPV4_MAX_MCAST \
93 RTE_IPV4(239, 255, 255, 255) /**< Maximum IPv4 multicast address */
95 #define RTE_IS_IPV4_MCAST(x) \
96 ((x) >= RTE_IPV4_MIN_MCAST && (x) <= RTE_IPV4_MAX_MCAST)
97 /**< check if IPv4 address is multicast */
100 * @internal Calculate a sum of all words in the buffer.
101 * Helper routine for the rte_raw_cksum().
104 * Pointer to the buffer.
106 * Length of the buffer.
108 * Initial value of the sum.
110 * sum += Sum of all words in the buffer.
112 static inline uint32_t
113 __rte_raw_cksum(const void *buf, size_t len, uint32_t sum)
115 /* workaround gcc strict-aliasing warning */
116 uintptr_t ptr = (uintptr_t)buf;
117 typedef uint16_t __attribute__((__may_alias__)) u16_p;
118 const u16_p *u16_buf = (const u16_p *)ptr;
120 while (len >= (sizeof(*u16_buf) * 4)) {
125 len -= sizeof(*u16_buf) * 4;
128 while (len >= sizeof(*u16_buf)) {
130 len -= sizeof(*u16_buf);
134 /* if length is in odd bytes */
136 sum += *((const uint8_t *)u16_buf);
142 * @internal Reduce a sum to the non-complemented checksum.
143 * Helper routine for the rte_raw_cksum().
148 * The non-complemented checksum.
150 static inline uint16_t
151 __rte_raw_cksum_reduce(uint32_t sum)
153 sum = ((sum & 0xffff0000) >> 16) + (sum & 0xffff);
154 sum = ((sum & 0xffff0000) >> 16) + (sum & 0xffff);
155 return (uint16_t)sum;
159 * Process the non-complemented checksum of a buffer.
162 * Pointer to the buffer.
164 * Length of the buffer.
166 * The non-complemented checksum.
168 static inline uint16_t
169 rte_raw_cksum(const void *buf, size_t len)
173 sum = __rte_raw_cksum(buf, len, 0);
174 return __rte_raw_cksum_reduce(sum);
178 * Compute the raw (non complemented) checksum of a packet.
181 * The pointer to the mbuf.
183 * The offset in bytes to start the checksum.
185 * The length in bytes of the data to checksum.
187 * A pointer to the checksum, filled on success.
189 * 0 on success, -1 on error (bad length or offset).
192 rte_raw_cksum_mbuf(const struct rte_mbuf *m, uint32_t off, uint32_t len,
195 const struct rte_mbuf *seg;
198 uint32_t seglen, done;
200 /* easy case: all data in the first segment */
201 if (off + len <= rte_pktmbuf_data_len(m)) {
202 *cksum = rte_raw_cksum(rte_pktmbuf_mtod_offset(m,
203 const char *, off), len);
207 if (unlikely(off + len > rte_pktmbuf_pkt_len(m)))
208 return -1; /* invalid params, return a dummy value */
210 /* else browse the segment to find offset */
212 for (seg = m; seg != NULL; seg = seg->next) {
213 seglen = rte_pktmbuf_data_len(seg);
219 buf = rte_pktmbuf_mtod_offset(seg, const char *, off);
221 /* all in one segment */
222 *cksum = rte_raw_cksum(buf, len);
226 /* hard case: process checksum of several segments */
230 tmp = __rte_raw_cksum(buf, seglen, 0);
232 tmp = rte_bswap16((uint16_t)tmp);
238 buf = rte_pktmbuf_mtod(seg, const char *);
239 seglen = rte_pktmbuf_data_len(seg);
240 if (seglen > len - done)
244 *cksum = __rte_raw_cksum_reduce(sum);
249 * Process the IPv4 checksum of an IPv4 header.
251 * The checksum field must be set to 0 by the caller.
254 * The pointer to the contiguous IPv4 header.
256 * The complemented checksum to set in the IP packet.
258 static inline uint16_t
259 rte_ipv4_cksum(const struct rte_ipv4_hdr *ipv4_hdr)
262 cksum = rte_raw_cksum(ipv4_hdr, sizeof(struct rte_ipv4_hdr));
263 return (cksum == 0xffff) ? cksum : (uint16_t)~cksum;
267 * Process the pseudo-header checksum of an IPv4 header.
269 * The checksum field must be set to 0 by the caller.
271 * Depending on the ol_flags, the pseudo-header checksum expected by the
272 * drivers is not the same. For instance, when TSO is enabled, the IP
273 * payload length must not be included in the packet.
275 * When ol_flags is 0, it computes the standard pseudo-header checksum.
278 * The pointer to the contiguous IPv4 header.
280 * The ol_flags of the associated mbuf.
282 * The non-complemented checksum to set in the L4 header.
284 static inline uint16_t
285 rte_ipv4_phdr_cksum(const struct rte_ipv4_hdr *ipv4_hdr, uint64_t ol_flags)
287 struct ipv4_psd_header {
288 uint32_t src_addr; /* IP address of source host. */
289 uint32_t dst_addr; /* IP address of destination host. */
290 uint8_t zero; /* zero. */
291 uint8_t proto; /* L4 protocol type. */
292 uint16_t len; /* L4 length. */
295 psd_hdr.src_addr = ipv4_hdr->src_addr;
296 psd_hdr.dst_addr = ipv4_hdr->dst_addr;
298 psd_hdr.proto = ipv4_hdr->next_proto_id;
299 if (ol_flags & PKT_TX_TCP_SEG) {
302 psd_hdr.len = rte_cpu_to_be_16(
303 (uint16_t)(rte_be_to_cpu_16(ipv4_hdr->total_length)
304 - sizeof(struct rte_ipv4_hdr)));
306 return rte_raw_cksum(&psd_hdr, sizeof(psd_hdr));
310 * Process the IPv4 UDP or TCP checksum.
312 * The IPv4 header should not contains options. The IP and layer 4
313 * checksum must be set to 0 in the packet by the caller.
316 * The pointer to the contiguous IPv4 header.
318 * The pointer to the beginning of the L4 header.
320 * The complemented checksum to set in the IP packet
323 static inline uint16_t
324 rte_ipv4_udptcp_cksum(const struct rte_ipv4_hdr *ipv4_hdr, const void *l4_hdr)
327 uint32_t l3_len, l4_len;
329 l3_len = rte_be_to_cpu_16(ipv4_hdr->total_length);
330 if (l3_len < sizeof(struct rte_ipv4_hdr))
333 l4_len = l3_len - sizeof(struct rte_ipv4_hdr);
335 cksum = rte_raw_cksum(l4_hdr, l4_len);
336 cksum += rte_ipv4_phdr_cksum(ipv4_hdr, 0);
338 cksum = ((cksum & 0xffff0000) >> 16) + (cksum & 0xffff);
339 cksum = (~cksum) & 0xffff;
343 return (uint16_t)cksum;
349 struct rte_ipv6_hdr {
350 uint32_t vtc_flow; /**< IP version, traffic class & flow label. */
351 uint16_t payload_len; /**< IP packet length - includes sizeof(ip_header). */
352 uint8_t proto; /**< Protocol, next header. */
353 uint8_t hop_limits; /**< Hop limits. */
354 uint8_t src_addr[16]; /**< IP address of source host. */
355 uint8_t dst_addr[16]; /**< IP address of destination host(s). */
356 } __attribute__((__packed__));
358 /* IPv6 vtc_flow: IPv / TC / flow_label */
359 #define RTE_IPV6_HDR_FL_SHIFT 0
360 #define RTE_IPV6_HDR_TC_SHIFT 20
361 #define RTE_IPV6_HDR_FL_MASK ((1u << RTE_IPV6_HDR_TC_SHIFT) - 1)
362 #define RTE_IPV6_HDR_TC_MASK (0xff << RTE_IPV6_HDR_TC_SHIFT)
363 #define RTE_IPV6_HDR_DSCP_MASK (0xfc << RTE_IPV6_HDR_TC_SHIFT)
364 #define RTE_IPV6_HDR_ECN_MASK (0x03 << RTE_IPV6_HDR_TC_SHIFT)
367 * Process the pseudo-header checksum of an IPv6 header.
369 * Depending on the ol_flags, the pseudo-header checksum expected by the
370 * drivers is not the same. For instance, when TSO is enabled, the IPv6
371 * payload length must not be included in the packet.
373 * When ol_flags is 0, it computes the standard pseudo-header checksum.
376 * The pointer to the contiguous IPv6 header.
378 * The ol_flags of the associated mbuf.
380 * The non-complemented checksum to set in the L4 header.
382 static inline uint16_t
383 rte_ipv6_phdr_cksum(const struct rte_ipv6_hdr *ipv6_hdr, uint64_t ol_flags)
387 uint32_t len; /* L4 length. */
388 uint32_t proto; /* L4 protocol - top 3 bytes must be zero */
391 psd_hdr.proto = (uint32_t)(ipv6_hdr->proto << 24);
392 if (ol_flags & PKT_TX_TCP_SEG) {
395 psd_hdr.len = ipv6_hdr->payload_len;
398 sum = __rte_raw_cksum(ipv6_hdr->src_addr,
399 sizeof(ipv6_hdr->src_addr) + sizeof(ipv6_hdr->dst_addr),
401 sum = __rte_raw_cksum(&psd_hdr, sizeof(psd_hdr), sum);
402 return __rte_raw_cksum_reduce(sum);
406 * Process the IPv6 UDP or TCP checksum.
408 * The IPv4 header should not contains options. The layer 4 checksum
409 * must be set to 0 in the packet by the caller.
412 * The pointer to the contiguous IPv6 header.
414 * The pointer to the beginning of the L4 header.
416 * The complemented checksum to set in the IP packet.
418 static inline uint16_t
419 rte_ipv6_udptcp_cksum(const struct rte_ipv6_hdr *ipv6_hdr, const void *l4_hdr)
424 l4_len = rte_be_to_cpu_16(ipv6_hdr->payload_len);
426 cksum = rte_raw_cksum(l4_hdr, l4_len);
427 cksum += rte_ipv6_phdr_cksum(ipv6_hdr, 0);
429 cksum = ((cksum & 0xffff0000) >> 16) + (cksum & 0xffff);
430 cksum = (~cksum) & 0xffff;
434 return (uint16_t)cksum;
441 #endif /* _RTE_IP_H_ */