2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright(c) 1982, 1986, 1990, 1993
5 * The Regents of the University of California.
6 * Copyright(c) 2010-2014 Intel Corporation.
7 * Copyright(c) 2014 6WIND S.A.
21 #include <netinet/in.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 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 IPV4_MAX_PKT_LEN 65535
55 /** Internet header length mask for version_ihl field */
56 #define 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 IPV4_IHL_MULTIPLIER (4)
63 /* Fragment Offset * Flags. */
64 #define IPV4_HDR_DF_SHIFT 14
65 #define IPV4_HDR_MF_SHIFT 13
66 #define IPV4_HDR_FO_SHIFT 3
68 #define IPV4_HDR_DF_FLAG (1 << IPV4_HDR_DF_SHIFT)
69 #define IPV4_HDR_MF_FLAG (1 << IPV4_HDR_MF_SHIFT)
71 #define IPV4_HDR_OFFSET_MASK ((1 << IPV4_HDR_MF_SHIFT) - 1)
73 #define IPV4_HDR_OFFSET_UNITS 8
78 #define IPV4_ANY ((uint32_t)0x00000000) /**< 0.0.0.0 */
79 #define IPV4_LOOPBACK ((uint32_t)0x7f000001) /**< 127.0.0.1 */
80 #define IPV4_BROADCAST ((uint32_t)0xe0000000) /**< 224.0.0.0 */
81 #define IPV4_ALLHOSTS_GROUP ((uint32_t)0xe0000001) /**< 224.0.0.1 */
82 #define IPV4_ALLRTRS_GROUP ((uint32_t)0xe0000002) /**< 224.0.0.2 */
83 #define IPV4_MAX_LOCAL_GROUP ((uint32_t)0xe00000ff) /**< 224.0.0.255 */
86 * IPv4 Multicast-related macros
88 #define IPV4_MIN_MCAST IPv4(224, 0, 0, 0) /**< Minimal IPv4-multicast address */
89 #define IPV4_MAX_MCAST IPv4(239, 255, 255, 255) /**< Maximum IPv4 multicast address */
91 #define IS_IPV4_MCAST(x) \
92 ((x) >= IPV4_MIN_MCAST && (x) <= IPV4_MAX_MCAST) /**< check if IPv4 address is multicast */
95 * @internal Calculate a sum of all words in the buffer.
96 * Helper routine for the rte_raw_cksum().
99 * Pointer to the buffer.
101 * Length of the buffer.
103 * Initial value of the sum.
105 * sum += Sum of all words in the buffer.
107 static inline uint32_t
108 __rte_raw_cksum(const void *buf, size_t len, uint32_t sum)
110 /* workaround gcc strict-aliasing warning */
111 uintptr_t ptr = (uintptr_t)buf;
112 typedef uint16_t __attribute__((__may_alias__)) u16_p;
113 const u16_p *u16 = (const u16_p *)ptr;
115 while (len >= (sizeof(*u16) * 4)) {
120 len -= sizeof(*u16) * 4;
123 while (len >= sizeof(*u16)) {
129 /* if length is in odd bytes */
131 sum += *((const uint8_t *)u16);
137 * @internal Reduce a sum to the non-complemented checksum.
138 * Helper routine for the rte_raw_cksum().
143 * The non-complemented checksum.
145 static inline uint16_t
146 __rte_raw_cksum_reduce(uint32_t sum)
148 sum = ((sum & 0xffff0000) >> 16) + (sum & 0xffff);
149 sum = ((sum & 0xffff0000) >> 16) + (sum & 0xffff);
150 return (uint16_t)sum;
154 * Process the non-complemented checksum of a buffer.
157 * Pointer to the buffer.
159 * Length of the buffer.
161 * The non-complemented checksum.
163 static inline uint16_t
164 rte_raw_cksum(const void *buf, size_t len)
168 sum = __rte_raw_cksum(buf, len, 0);
169 return __rte_raw_cksum_reduce(sum);
173 * Compute the raw (non complemented) checksum of a packet.
176 * The pointer to the mbuf.
178 * The offset in bytes to start the checksum.
180 * The length in bytes of the data to checksum.
182 * A pointer to the checksum, filled on success.
184 * 0 on success, -1 on error (bad length or offset).
187 rte_raw_cksum_mbuf(const struct rte_mbuf *m, uint32_t off, uint32_t len,
190 const struct rte_mbuf *seg;
193 uint32_t seglen, done;
195 /* easy case: all data in the first segment */
196 if (off + len <= rte_pktmbuf_data_len(m)) {
197 *cksum = rte_raw_cksum(rte_pktmbuf_mtod_offset(m,
198 const char *, off), len);
202 if (unlikely(off + len > rte_pktmbuf_pkt_len(m)))
203 return -1; /* invalid params, return a dummy value */
205 /* else browse the segment to find offset */
207 for (seg = m; seg != NULL; seg = seg->next) {
208 seglen = rte_pktmbuf_data_len(seg);
214 buf = rte_pktmbuf_mtod_offset(seg, const char *, off);
216 /* all in one segment */
217 *cksum = rte_raw_cksum(buf, len);
221 /* hard case: process checksum of several segments */
225 tmp = __rte_raw_cksum(buf, seglen, 0);
227 tmp = rte_bswap16(tmp);
233 buf = rte_pktmbuf_mtod(seg, const char *);
234 seglen = rte_pktmbuf_data_len(seg);
235 if (seglen > len - done)
239 *cksum = __rte_raw_cksum_reduce(sum);
244 * Process the IPv4 checksum of an IPv4 header.
246 * The checksum field must be set to 0 by the caller.
249 * The pointer to the contiguous IPv4 header.
251 * The complemented checksum to set in the IP packet.
253 static inline uint16_t
254 rte_ipv4_cksum(const struct ipv4_hdr *ipv4_hdr)
257 cksum = rte_raw_cksum(ipv4_hdr, sizeof(struct ipv4_hdr));
258 return (cksum == 0xffff) ? cksum : ~cksum;
262 * Process the pseudo-header checksum of an IPv4 header.
264 * The checksum field must be set to 0 by the caller.
266 * Depending on the ol_flags, the pseudo-header checksum expected by the
267 * drivers is not the same. For instance, when TSO is enabled, the IP
268 * payload length must not be included in the packet.
270 * When ol_flags is 0, it computes the standard pseudo-header checksum.
273 * The pointer to the contiguous IPv4 header.
275 * The ol_flags of the associated mbuf.
277 * The non-complemented checksum to set in the L4 header.
279 static inline uint16_t
280 rte_ipv4_phdr_cksum(const struct ipv4_hdr *ipv4_hdr, uint64_t ol_flags)
282 struct ipv4_psd_header {
283 uint32_t src_addr; /* IP address of source host. */
284 uint32_t dst_addr; /* IP address of destination host. */
285 uint8_t zero; /* zero. */
286 uint8_t proto; /* L4 protocol type. */
287 uint16_t len; /* L4 length. */
290 psd_hdr.src_addr = ipv4_hdr->src_addr;
291 psd_hdr.dst_addr = ipv4_hdr->dst_addr;
293 psd_hdr.proto = ipv4_hdr->next_proto_id;
294 if (ol_flags & PKT_TX_TCP_SEG) {
297 psd_hdr.len = rte_cpu_to_be_16(
298 (uint16_t)(rte_be_to_cpu_16(ipv4_hdr->total_length)
299 - sizeof(struct ipv4_hdr)));
301 return rte_raw_cksum(&psd_hdr, sizeof(psd_hdr));
305 * Process the IPv4 UDP or TCP checksum.
307 * The IPv4 header should not contains options. The IP and layer 4
308 * checksum must be set to 0 in the packet by the caller.
311 * The pointer to the contiguous IPv4 header.
313 * The pointer to the beginning of the L4 header.
315 * The complemented checksum to set in the IP packet.
317 static inline uint16_t
318 rte_ipv4_udptcp_cksum(const struct ipv4_hdr *ipv4_hdr, const void *l4_hdr)
323 l4_len = rte_be_to_cpu_16(ipv4_hdr->total_length) -
324 sizeof(struct ipv4_hdr);
326 cksum = rte_raw_cksum(l4_hdr, l4_len);
327 cksum += rte_ipv4_phdr_cksum(ipv4_hdr, 0);
329 cksum = ((cksum & 0xffff0000) >> 16) + (cksum & 0xffff);
330 cksum = (~cksum) & 0xffff;
341 uint32_t vtc_flow; /**< IP version, traffic class & flow label. */
342 uint16_t payload_len; /**< IP packet length - includes sizeof(ip_header). */
343 uint8_t proto; /**< Protocol, next header. */
344 uint8_t hop_limits; /**< Hop limits. */
345 uint8_t src_addr[16]; /**< IP address of source host. */
346 uint8_t dst_addr[16]; /**< IP address of destination host(s). */
347 } __attribute__((__packed__));
350 * Process the pseudo-header checksum of an IPv6 header.
352 * Depending on the ol_flags, the pseudo-header checksum expected by the
353 * drivers is not the same. For instance, when TSO is enabled, the IPv6
354 * payload length must not be included in the packet.
356 * When ol_flags is 0, it computes the standard pseudo-header checksum.
359 * The pointer to the contiguous IPv6 header.
361 * The ol_flags of the associated mbuf.
363 * The non-complemented checksum to set in the L4 header.
365 static inline uint16_t
366 rte_ipv6_phdr_cksum(const struct ipv6_hdr *ipv6_hdr, uint64_t ol_flags)
370 uint32_t len; /* L4 length. */
371 uint32_t proto; /* L4 protocol - top 3 bytes must be zero */
374 psd_hdr.proto = (ipv6_hdr->proto << 24);
375 if (ol_flags & PKT_TX_TCP_SEG) {
378 psd_hdr.len = ipv6_hdr->payload_len;
381 sum = __rte_raw_cksum(ipv6_hdr->src_addr,
382 sizeof(ipv6_hdr->src_addr) + sizeof(ipv6_hdr->dst_addr),
384 sum = __rte_raw_cksum(&psd_hdr, sizeof(psd_hdr), sum);
385 return __rte_raw_cksum_reduce(sum);
389 * Process the IPv6 UDP or TCP checksum.
391 * The IPv4 header should not contains options. The layer 4 checksum
392 * must be set to 0 in the packet by the caller.
395 * The pointer to the contiguous IPv6 header.
397 * The pointer to the beginning of the L4 header.
399 * The complemented checksum to set in the IP packet.
401 static inline uint16_t
402 rte_ipv6_udptcp_cksum(const struct ipv6_hdr *ipv6_hdr, const void *l4_hdr)
407 l4_len = rte_be_to_cpu_16(ipv6_hdr->payload_len);
409 cksum = rte_raw_cksum(l4_hdr, l4_len);
410 cksum += rte_ipv6_phdr_cksum(ipv6_hdr, 0);
412 cksum = ((cksum & 0xffff0000) >> 16) + (cksum & 0xffff);
413 cksum = (~cksum) & 0xffff;
424 #endif /* _RTE_IP_H_ */