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 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 IPV4_MAX_PKT_LEN 65535
53 /** Internet header length mask for version_ihl field */
54 #define 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 IPV4_IHL_MULTIPLIER (4)
61 /* Fragment Offset * Flags. */
62 #define IPV4_HDR_DF_SHIFT 14
63 #define IPV4_HDR_MF_SHIFT 13
64 #define IPV4_HDR_FO_SHIFT 3
66 #define IPV4_HDR_DF_FLAG (1 << IPV4_HDR_DF_SHIFT)
67 #define IPV4_HDR_MF_FLAG (1 << IPV4_HDR_MF_SHIFT)
69 #define IPV4_HDR_OFFSET_MASK ((1 << IPV4_HDR_MF_SHIFT) - 1)
71 #define IPV4_HDR_OFFSET_UNITS 8
76 #define IPV4_ANY ((uint32_t)0x00000000) /**< 0.0.0.0 */
77 #define IPV4_LOOPBACK ((uint32_t)0x7f000001) /**< 127.0.0.1 */
78 #define IPV4_BROADCAST ((uint32_t)0xe0000000) /**< 224.0.0.0 */
79 #define IPV4_ALLHOSTS_GROUP ((uint32_t)0xe0000001) /**< 224.0.0.1 */
80 #define IPV4_ALLRTRS_GROUP ((uint32_t)0xe0000002) /**< 224.0.0.2 */
81 #define IPV4_MAX_LOCAL_GROUP ((uint32_t)0xe00000ff) /**< 224.0.0.255 */
84 * IPv4 Multicast-related macros
86 #define IPV4_MIN_MCAST IPv4(224, 0, 0, 0) /**< Minimal IPv4-multicast address */
87 #define IPV4_MAX_MCAST IPv4(239, 255, 255, 255) /**< Maximum IPv4 multicast address */
89 #define IS_IPV4_MCAST(x) \
90 ((x) >= IPV4_MIN_MCAST && (x) <= IPV4_MAX_MCAST) /**< check if IPv4 address is multicast */
93 * @internal Calculate a sum of all words in the buffer.
94 * Helper routine for the rte_raw_cksum().
97 * Pointer to the buffer.
99 * Length of the buffer.
101 * Initial value of the sum.
103 * sum += Sum of all words in the buffer.
105 static inline uint32_t
106 __rte_raw_cksum(const void *buf, size_t len, uint32_t sum)
108 /* workaround gcc strict-aliasing warning */
109 uintptr_t ptr = (uintptr_t)buf;
110 typedef uint16_t __attribute__((__may_alias__)) u16_p;
111 const u16_p *u16 = (const u16_p *)ptr;
113 while (len >= (sizeof(*u16) * 4)) {
118 len -= sizeof(*u16) * 4;
121 while (len >= sizeof(*u16)) {
127 /* if length is in odd bytes */
129 sum += *((const uint8_t *)u16);
135 * @internal Reduce a sum to the non-complemented checksum.
136 * Helper routine for the rte_raw_cksum().
141 * The non-complemented checksum.
143 static inline uint16_t
144 __rte_raw_cksum_reduce(uint32_t sum)
146 sum = ((sum & 0xffff0000) >> 16) + (sum & 0xffff);
147 sum = ((sum & 0xffff0000) >> 16) + (sum & 0xffff);
148 return (uint16_t)sum;
152 * Process the non-complemented checksum of a buffer.
155 * Pointer to the buffer.
157 * Length of the buffer.
159 * The non-complemented checksum.
161 static inline uint16_t
162 rte_raw_cksum(const void *buf, size_t len)
166 sum = __rte_raw_cksum(buf, len, 0);
167 return __rte_raw_cksum_reduce(sum);
171 * Compute the raw (non complemented) checksum of a packet.
174 * The pointer to the mbuf.
176 * The offset in bytes to start the checksum.
178 * The length in bytes of the data to checksum.
180 * A pointer to the checksum, filled on success.
182 * 0 on success, -1 on error (bad length or offset).
185 rte_raw_cksum_mbuf(const struct rte_mbuf *m, uint32_t off, uint32_t len,
188 const struct rte_mbuf *seg;
191 uint32_t seglen, done;
193 /* easy case: all data in the first segment */
194 if (off + len <= rte_pktmbuf_data_len(m)) {
195 *cksum = rte_raw_cksum(rte_pktmbuf_mtod_offset(m,
196 const char *, off), len);
200 if (unlikely(off + len > rte_pktmbuf_pkt_len(m)))
201 return -1; /* invalid params, return a dummy value */
203 /* else browse the segment to find offset */
205 for (seg = m; seg != NULL; seg = seg->next) {
206 seglen = rte_pktmbuf_data_len(seg);
212 buf = rte_pktmbuf_mtod_offset(seg, const char *, off);
214 /* all in one segment */
215 *cksum = rte_raw_cksum(buf, len);
219 /* hard case: process checksum of several segments */
223 tmp = __rte_raw_cksum(buf, seglen, 0);
225 tmp = rte_bswap16((uint16_t)tmp);
231 buf = rte_pktmbuf_mtod(seg, const char *);
232 seglen = rte_pktmbuf_data_len(seg);
233 if (seglen > len - done)
237 *cksum = __rte_raw_cksum_reduce(sum);
242 * Process the IPv4 checksum of an IPv4 header.
244 * The checksum field must be set to 0 by the caller.
247 * The pointer to the contiguous IPv4 header.
249 * The complemented checksum to set in the IP packet.
251 static inline uint16_t
252 rte_ipv4_cksum(const struct ipv4_hdr *ipv4_hdr)
255 cksum = rte_raw_cksum(ipv4_hdr, sizeof(struct ipv4_hdr));
256 return (cksum == 0xffff) ? cksum : (uint16_t)~cksum;
260 * Process the pseudo-header checksum of an IPv4 header.
262 * The checksum field must be set to 0 by the caller.
264 * Depending on the ol_flags, the pseudo-header checksum expected by the
265 * drivers is not the same. For instance, when TSO is enabled, the IP
266 * payload length must not be included in the packet.
268 * When ol_flags is 0, it computes the standard pseudo-header checksum.
271 * The pointer to the contiguous IPv4 header.
273 * The ol_flags of the associated mbuf.
275 * The non-complemented checksum to set in the L4 header.
277 static inline uint16_t
278 rte_ipv4_phdr_cksum(const struct ipv4_hdr *ipv4_hdr, uint64_t ol_flags)
280 struct ipv4_psd_header {
281 uint32_t src_addr; /* IP address of source host. */
282 uint32_t dst_addr; /* IP address of destination host. */
283 uint8_t zero; /* zero. */
284 uint8_t proto; /* L4 protocol type. */
285 uint16_t len; /* L4 length. */
288 psd_hdr.src_addr = ipv4_hdr->src_addr;
289 psd_hdr.dst_addr = ipv4_hdr->dst_addr;
291 psd_hdr.proto = ipv4_hdr->next_proto_id;
292 if (ol_flags & PKT_TX_TCP_SEG) {
295 psd_hdr.len = rte_cpu_to_be_16(
296 (uint16_t)(rte_be_to_cpu_16(ipv4_hdr->total_length)
297 - sizeof(struct ipv4_hdr)));
299 return rte_raw_cksum(&psd_hdr, sizeof(psd_hdr));
303 * Process the IPv4 UDP or TCP checksum.
305 * The IPv4 header should not contains options. The IP and layer 4
306 * checksum must be set to 0 in the packet by the caller.
309 * The pointer to the contiguous IPv4 header.
311 * The pointer to the beginning of the L4 header.
313 * The complemented checksum to set in the IP packet.
315 static inline uint16_t
316 rte_ipv4_udptcp_cksum(const struct ipv4_hdr *ipv4_hdr, const void *l4_hdr)
321 l4_len = (uint32_t)(rte_be_to_cpu_16(ipv4_hdr->total_length) -
322 sizeof(struct ipv4_hdr));
324 cksum = rte_raw_cksum(l4_hdr, l4_len);
325 cksum += rte_ipv4_phdr_cksum(ipv4_hdr, 0);
327 cksum = ((cksum & 0xffff0000) >> 16) + (cksum & 0xffff);
328 cksum = (~cksum) & 0xffff;
332 return (uint16_t)cksum;
339 uint32_t vtc_flow; /**< IP version, traffic class & flow label. */
340 uint16_t payload_len; /**< IP packet length - includes sizeof(ip_header). */
341 uint8_t proto; /**< Protocol, next header. */
342 uint8_t hop_limits; /**< Hop limits. */
343 uint8_t src_addr[16]; /**< IP address of source host. */
344 uint8_t dst_addr[16]; /**< IP address of destination host(s). */
345 } __attribute__((__packed__));
347 /* IPv6 vtc_flow: IPv / TC / flow_label */
348 #define IPV6_HDR_FL_SHIFT 0
349 #define IPV6_HDR_TC_SHIFT 20
350 #define IPV6_HDR_FL_MASK ((1u << IPV6_HDR_TC_SHIFT) - 1)
351 #define IPV6_HDR_TC_MASK (0xf << IPV6_HDR_TC_SHIFT)
354 * Process the pseudo-header checksum of an IPv6 header.
356 * Depending on the ol_flags, the pseudo-header checksum expected by the
357 * drivers is not the same. For instance, when TSO is enabled, the IPv6
358 * payload length must not be included in the packet.
360 * When ol_flags is 0, it computes the standard pseudo-header checksum.
363 * The pointer to the contiguous IPv6 header.
365 * The ol_flags of the associated mbuf.
367 * The non-complemented checksum to set in the L4 header.
369 static inline uint16_t
370 rte_ipv6_phdr_cksum(const struct ipv6_hdr *ipv6_hdr, uint64_t ol_flags)
374 uint32_t len; /* L4 length. */
375 uint32_t proto; /* L4 protocol - top 3 bytes must be zero */
378 psd_hdr.proto = (uint32_t)(ipv6_hdr->proto << 24);
379 if (ol_flags & PKT_TX_TCP_SEG) {
382 psd_hdr.len = ipv6_hdr->payload_len;
385 sum = __rte_raw_cksum(ipv6_hdr->src_addr,
386 sizeof(ipv6_hdr->src_addr) + sizeof(ipv6_hdr->dst_addr),
388 sum = __rte_raw_cksum(&psd_hdr, sizeof(psd_hdr), sum);
389 return __rte_raw_cksum_reduce(sum);
393 * Process the IPv6 UDP or TCP checksum.
395 * The IPv4 header should not contains options. The layer 4 checksum
396 * must be set to 0 in the packet by the caller.
399 * The pointer to the contiguous IPv6 header.
401 * The pointer to the beginning of the L4 header.
403 * The complemented checksum to set in the IP packet.
405 static inline uint16_t
406 rte_ipv6_udptcp_cksum(const struct ipv6_hdr *ipv6_hdr, const void *l4_hdr)
411 l4_len = rte_be_to_cpu_16(ipv6_hdr->payload_len);
413 cksum = rte_raw_cksum(l4_hdr, l4_len);
414 cksum += rte_ipv6_phdr_cksum(ipv6_hdr, 0);
416 cksum = ((cksum & 0xffff0000) >> 16) + (cksum & 0xffff);
417 cksum = (~cksum) & 0xffff;
421 return (uint16_t)cksum;
428 #endif /* _RTE_IP_H_ */