xref: /f-stack/dpdk/lib/librte_net/rte_ip.h (revision 2d9fd380)
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.
6  * All rights reserved.
7  */
8 
9 #ifndef _RTE_IP_H_
10 #define _RTE_IP_H_
11 
12 /**
13  * @file
14  *
15  * IP-related defines
16  */
17 
18 #include <stdint.h>
19 #include <sys/types.h>
20 #include <netinet/in.h>
21 #include <netinet/ip.h>
22 
23 #include <rte_byteorder.h>
24 #include <rte_mbuf.h>
25 
26 #ifdef __cplusplus
27 extern "C" {
28 #endif
29 
30 /**
31  * IPv4 Header
32  */
33 struct rte_ipv4_hdr {
34 	uint8_t  version_ihl;		/**< version and header length */
35 	uint8_t  type_of_service;	/**< type of service */
36 	rte_be16_t total_length;	/**< length of packet */
37 	rte_be16_t packet_id;		/**< packet ID */
38 	rte_be16_t fragment_offset;	/**< fragmentation offset */
39 	uint8_t  time_to_live;		/**< time to live */
40 	uint8_t  next_proto_id;		/**< protocol ID */
41 	rte_be16_t hdr_checksum;	/**< header checksum */
42 	rte_be32_t src_addr;		/**< source address */
43 	rte_be32_t dst_addr;		/**< destination address */
44 } __rte_packed;
45 
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)  | \
50 					   ((d) & 0xff))
51 
52 /** Maximal IPv4 packet length (including a header) */
53 #define RTE_IPV4_MAX_PKT_LEN        65535
54 
55 /** Internet header length mask for version_ihl field */
56 #define RTE_IPV4_HDR_IHL_MASK	(0x0f)
57 /**
58  * Internet header length field multiplier (IHL field specifies overall header
59  * length in number of 4-byte words)
60  */
61 #define RTE_IPV4_IHL_MULTIPLIER	(4)
62 
63 /* Type of Service fields */
64 #define RTE_IPV4_HDR_DSCP_MASK	(0xfc)
65 #define RTE_IPV4_HDR_ECN_MASK	(0x03)
66 #define RTE_IPV4_HDR_ECN_CE	RTE_IPV4_HDR_ECN_MASK
67 
68 /* Fragment Offset * Flags. */
69 #define	RTE_IPV4_HDR_DF_SHIFT	14
70 #define	RTE_IPV4_HDR_MF_SHIFT	13
71 #define	RTE_IPV4_HDR_FO_SHIFT	3
72 
73 #define	RTE_IPV4_HDR_DF_FLAG	(1 << RTE_IPV4_HDR_DF_SHIFT)
74 #define	RTE_IPV4_HDR_MF_FLAG	(1 << RTE_IPV4_HDR_MF_SHIFT)
75 
76 #define	RTE_IPV4_HDR_OFFSET_MASK	((1 << RTE_IPV4_HDR_MF_SHIFT) - 1)
77 
78 #define	RTE_IPV4_HDR_OFFSET_UNITS	8
79 
80 /*
81  * IPv4 address types
82  */
83 #define RTE_IPV4_ANY              ((uint32_t)0x00000000) /**< 0.0.0.0 */
84 #define RTE_IPV4_LOOPBACK         ((uint32_t)0x7f000001) /**< 127.0.0.1 */
85 #define RTE_IPV4_BROADCAST        ((uint32_t)0xe0000000) /**< 224.0.0.0 */
86 #define RTE_IPV4_ALLHOSTS_GROUP   ((uint32_t)0xe0000001) /**< 224.0.0.1 */
87 #define RTE_IPV4_ALLRTRS_GROUP    ((uint32_t)0xe0000002) /**< 224.0.0.2 */
88 #define RTE_IPV4_MAX_LOCAL_GROUP  ((uint32_t)0xe00000ff) /**< 224.0.0.255 */
89 
90 /*
91  * IPv4 Multicast-related macros
92  */
93 #define RTE_IPV4_MIN_MCAST \
94 	RTE_IPV4(224, 0, 0, 0)          /**< Minimal IPv4-multicast address */
95 #define RTE_IPV4_MAX_MCAST \
96 	RTE_IPV4(239, 255, 255, 255)    /**< Maximum IPv4 multicast address */
97 
98 #define RTE_IS_IPV4_MCAST(x) \
99 	((x) >= RTE_IPV4_MIN_MCAST && (x) <= RTE_IPV4_MAX_MCAST)
100 	/**< check if IPv4 address is multicast */
101 
102 /* IPv4 default fields values */
103 #define RTE_IPV4_MIN_IHL    (0x5)
104 #define RTE_IPV4_VHL_DEF    ((IPVERSION << 4) | RTE_IPV4_MIN_IHL)
105 
106 /**
107  * Get the length of an IPv4 header.
108  *
109  * @param ipv4_hdr
110  *   Pointer to the IPv4 header.
111  * @return
112  *   The length of the IPv4 header (with options if present) in bytes.
113  */
114 static inline uint8_t
rte_ipv4_hdr_len(const struct rte_ipv4_hdr * ipv4_hdr)115 rte_ipv4_hdr_len(const struct rte_ipv4_hdr *ipv4_hdr)
116 {
117 	return (uint8_t)((ipv4_hdr->version_ihl & RTE_IPV4_HDR_IHL_MASK) *
118 		RTE_IPV4_IHL_MULTIPLIER);
119 }
120 
121 /**
122  * @internal Calculate a sum of all words in the buffer.
123  * Helper routine for the rte_raw_cksum().
124  *
125  * @param buf
126  *   Pointer to the buffer.
127  * @param len
128  *   Length of the buffer.
129  * @param sum
130  *   Initial value of the sum.
131  * @return
132  *   sum += Sum of all words in the buffer.
133  */
134 static inline uint32_t
__rte_raw_cksum(const void * buf,size_t len,uint32_t sum)135 __rte_raw_cksum(const void *buf, size_t len, uint32_t sum)
136 {
137 	/* workaround gcc strict-aliasing warning */
138 	uintptr_t ptr = (uintptr_t)buf;
139 	typedef uint16_t __attribute__((__may_alias__)) u16_p;
140 	const u16_p *u16_buf = (const u16_p *)ptr;
141 
142 	while (len >= (sizeof(*u16_buf) * 4)) {
143 		sum += u16_buf[0];
144 		sum += u16_buf[1];
145 		sum += u16_buf[2];
146 		sum += u16_buf[3];
147 		len -= sizeof(*u16_buf) * 4;
148 		u16_buf += 4;
149 	}
150 	while (len >= sizeof(*u16_buf)) {
151 		sum += *u16_buf;
152 		len -= sizeof(*u16_buf);
153 		u16_buf += 1;
154 	}
155 
156 	/* if length is in odd bytes */
157 	if (len == 1) {
158 		uint16_t left = 0;
159 		*(uint8_t *)&left = *(const uint8_t *)u16_buf;
160 		sum += left;
161 	}
162 
163 	return sum;
164 }
165 
166 /**
167  * @internal Reduce a sum to the non-complemented checksum.
168  * Helper routine for the rte_raw_cksum().
169  *
170  * @param sum
171  *   Value of the sum.
172  * @return
173  *   The non-complemented checksum.
174  */
175 static inline uint16_t
__rte_raw_cksum_reduce(uint32_t sum)176 __rte_raw_cksum_reduce(uint32_t sum)
177 {
178 	sum = ((sum & 0xffff0000) >> 16) + (sum & 0xffff);
179 	sum = ((sum & 0xffff0000) >> 16) + (sum & 0xffff);
180 	return (uint16_t)sum;
181 }
182 
183 /**
184  * Process the non-complemented checksum of a buffer.
185  *
186  * @param buf
187  *   Pointer to the buffer.
188  * @param len
189  *   Length of the buffer.
190  * @return
191  *   The non-complemented checksum.
192  */
193 static inline uint16_t
rte_raw_cksum(const void * buf,size_t len)194 rte_raw_cksum(const void *buf, size_t len)
195 {
196 	uint32_t sum;
197 
198 	sum = __rte_raw_cksum(buf, len, 0);
199 	return __rte_raw_cksum_reduce(sum);
200 }
201 
202 /**
203  * Compute the raw (non complemented) checksum of a packet.
204  *
205  * @param m
206  *   The pointer to the mbuf.
207  * @param off
208  *   The offset in bytes to start the checksum.
209  * @param len
210  *   The length in bytes of the data to checksum.
211  * @param cksum
212  *   A pointer to the checksum, filled on success.
213  * @return
214  *   0 on success, -1 on error (bad length or offset).
215  */
216 static inline int
rte_raw_cksum_mbuf(const struct rte_mbuf * m,uint32_t off,uint32_t len,uint16_t * cksum)217 rte_raw_cksum_mbuf(const struct rte_mbuf *m, uint32_t off, uint32_t len,
218 	uint16_t *cksum)
219 {
220 	const struct rte_mbuf *seg;
221 	const char *buf;
222 	uint32_t sum, tmp;
223 	uint32_t seglen, done;
224 
225 	/* easy case: all data in the first segment */
226 	if (off + len <= rte_pktmbuf_data_len(m)) {
227 		*cksum = rte_raw_cksum(rte_pktmbuf_mtod_offset(m,
228 				const char *, off), len);
229 		return 0;
230 	}
231 
232 	if (unlikely(off + len > rte_pktmbuf_pkt_len(m)))
233 		return -1; /* invalid params, return a dummy value */
234 
235 	/* else browse the segment to find offset */
236 	seglen = 0;
237 	for (seg = m; seg != NULL; seg = seg->next) {
238 		seglen = rte_pktmbuf_data_len(seg);
239 		if (off < seglen)
240 			break;
241 		off -= seglen;
242 	}
243 	RTE_ASSERT(seg != NULL);
244 	if (seg == NULL)
245 		return -1;
246 	seglen -= off;
247 	buf = rte_pktmbuf_mtod_offset(seg, const char *, off);
248 	if (seglen >= len) {
249 		/* all in one segment */
250 		*cksum = rte_raw_cksum(buf, len);
251 		return 0;
252 	}
253 
254 	/* hard case: process checksum of several segments */
255 	sum = 0;
256 	done = 0;
257 	for (;;) {
258 		tmp = __rte_raw_cksum(buf, seglen, 0);
259 		if (done & 1)
260 			tmp = rte_bswap16((uint16_t)tmp);
261 		sum += tmp;
262 		done += seglen;
263 		if (done == len)
264 			break;
265 		seg = seg->next;
266 		buf = rte_pktmbuf_mtod(seg, const char *);
267 		seglen = rte_pktmbuf_data_len(seg);
268 		if (seglen > len - done)
269 			seglen = len - done;
270 	}
271 
272 	*cksum = __rte_raw_cksum_reduce(sum);
273 	return 0;
274 }
275 
276 /**
277  * Process the IPv4 checksum of an IPv4 header.
278  *
279  * The checksum field must be set to 0 by the caller.
280  *
281  * @param ipv4_hdr
282  *   The pointer to the contiguous IPv4 header.
283  * @return
284  *   The complemented checksum to set in the IP packet.
285  */
286 static inline uint16_t
rte_ipv4_cksum(const struct rte_ipv4_hdr * ipv4_hdr)287 rte_ipv4_cksum(const struct rte_ipv4_hdr *ipv4_hdr)
288 {
289 	uint16_t cksum;
290 	cksum = rte_raw_cksum(ipv4_hdr, rte_ipv4_hdr_len(ipv4_hdr));
291 	return (uint16_t)~cksum;
292 }
293 
294 /**
295  * Process the pseudo-header checksum of an IPv4 header.
296  *
297  * The checksum field must be set to 0 by the caller.
298  *
299  * Depending on the ol_flags, the pseudo-header checksum expected by the
300  * drivers is not the same. For instance, when TSO is enabled, the IP
301  * payload length must not be included in the packet.
302  *
303  * When ol_flags is 0, it computes the standard pseudo-header checksum.
304  *
305  * @param ipv4_hdr
306  *   The pointer to the contiguous IPv4 header.
307  * @param ol_flags
308  *   The ol_flags of the associated mbuf.
309  * @return
310  *   The non-complemented checksum to set in the L4 header.
311  */
312 static inline uint16_t
rte_ipv4_phdr_cksum(const struct rte_ipv4_hdr * ipv4_hdr,uint64_t ol_flags)313 rte_ipv4_phdr_cksum(const struct rte_ipv4_hdr *ipv4_hdr, uint64_t ol_flags)
314 {
315 	struct ipv4_psd_header {
316 		uint32_t src_addr; /* IP address of source host. */
317 		uint32_t dst_addr; /* IP address of destination host. */
318 		uint8_t  zero;     /* zero. */
319 		uint8_t  proto;    /* L4 protocol type. */
320 		uint16_t len;      /* L4 length. */
321 	} psd_hdr;
322 
323 	uint32_t l3_len;
324 
325 	psd_hdr.src_addr = ipv4_hdr->src_addr;
326 	psd_hdr.dst_addr = ipv4_hdr->dst_addr;
327 	psd_hdr.zero = 0;
328 	psd_hdr.proto = ipv4_hdr->next_proto_id;
329 	if (ol_flags & PKT_TX_TCP_SEG) {
330 		psd_hdr.len = 0;
331 	} else {
332 		l3_len = rte_be_to_cpu_16(ipv4_hdr->total_length);
333 		psd_hdr.len = rte_cpu_to_be_16((uint16_t)(l3_len -
334 			rte_ipv4_hdr_len(ipv4_hdr)));
335 	}
336 	return rte_raw_cksum(&psd_hdr, sizeof(psd_hdr));
337 }
338 
339 /**
340  * Process the IPv4 UDP or TCP checksum.
341  *
342  * The IP and layer 4 checksum must be set to 0 in the packet by
343  * the caller.
344  *
345  * @param ipv4_hdr
346  *   The pointer to the contiguous IPv4 header.
347  * @param l4_hdr
348  *   The pointer to the beginning of the L4 header.
349  * @return
350  *   The complemented checksum to set in the IP packet.
351  */
352 static inline uint16_t
rte_ipv4_udptcp_cksum(const struct rte_ipv4_hdr * ipv4_hdr,const void * l4_hdr)353 rte_ipv4_udptcp_cksum(const struct rte_ipv4_hdr *ipv4_hdr, const void *l4_hdr)
354 {
355 	uint32_t cksum;
356 	uint32_t l3_len, l4_len;
357 	uint8_t ip_hdr_len;
358 
359 	ip_hdr_len = rte_ipv4_hdr_len(ipv4_hdr);
360 	l3_len = rte_be_to_cpu_16(ipv4_hdr->total_length);
361 	if (l3_len < ip_hdr_len)
362 		return 0;
363 
364 	l4_len = l3_len - ip_hdr_len;
365 
366 	cksum = rte_raw_cksum(l4_hdr, l4_len);
367 	cksum += rte_ipv4_phdr_cksum(ipv4_hdr, 0);
368 
369 	cksum = ((cksum & 0xffff0000) >> 16) + (cksum & 0xffff);
370 	cksum = (~cksum) & 0xffff;
371 	/*
372 	 * Per RFC 768:If the computed checksum is zero for UDP,
373 	 * it is transmitted as all ones
374 	 * (the equivalent in one's complement arithmetic).
375 	 */
376 	if (cksum == 0 && ipv4_hdr->next_proto_id == IPPROTO_UDP)
377 		cksum = 0xffff;
378 
379 	return (uint16_t)cksum;
380 }
381 
382 /**
383  * IPv6 Header
384  */
385 struct rte_ipv6_hdr {
386 	rte_be32_t vtc_flow;	/**< IP version, traffic class & flow label. */
387 	rte_be16_t payload_len;	/**< IP packet length - includes header size */
388 	uint8_t  proto;		/**< Protocol, next header. */
389 	uint8_t  hop_limits;	/**< Hop limits. */
390 	uint8_t  src_addr[16];	/**< IP address of source host. */
391 	uint8_t  dst_addr[16];	/**< IP address of destination host(s). */
392 } __rte_packed;
393 
394 /* IPv6 vtc_flow: IPv / TC / flow_label */
395 #define RTE_IPV6_HDR_FL_SHIFT 0
396 #define RTE_IPV6_HDR_TC_SHIFT 20
397 #define RTE_IPV6_HDR_FL_MASK	((1u << RTE_IPV6_HDR_TC_SHIFT) - 1)
398 #define RTE_IPV6_HDR_TC_MASK	(0xff << RTE_IPV6_HDR_TC_SHIFT)
399 #define RTE_IPV6_HDR_DSCP_MASK	(0xfc << RTE_IPV6_HDR_TC_SHIFT)
400 #define RTE_IPV6_HDR_ECN_MASK	(0x03 << RTE_IPV6_HDR_TC_SHIFT)
401 #define RTE_IPV6_HDR_ECN_CE	RTE_IPV6_HDR_ECN_MASK
402 
403 #define RTE_IPV6_MIN_MTU 1280 /**< Minimum MTU for IPv6, see RFC 8200. */
404 
405 /**
406  * Process the pseudo-header checksum of an IPv6 header.
407  *
408  * Depending on the ol_flags, the pseudo-header checksum expected by the
409  * drivers is not the same. For instance, when TSO is enabled, the IPv6
410  * payload length must not be included in the packet.
411  *
412  * When ol_flags is 0, it computes the standard pseudo-header checksum.
413  *
414  * @param ipv6_hdr
415  *   The pointer to the contiguous IPv6 header.
416  * @param ol_flags
417  *   The ol_flags of the associated mbuf.
418  * @return
419  *   The non-complemented checksum to set in the L4 header.
420  */
421 static inline uint16_t
rte_ipv6_phdr_cksum(const struct rte_ipv6_hdr * ipv6_hdr,uint64_t ol_flags)422 rte_ipv6_phdr_cksum(const struct rte_ipv6_hdr *ipv6_hdr, uint64_t ol_flags)
423 {
424 	uint32_t sum;
425 	struct {
426 		rte_be32_t len;   /* L4 length. */
427 		rte_be32_t proto; /* L4 protocol - top 3 bytes must be zero */
428 	} psd_hdr;
429 
430 	psd_hdr.proto = (uint32_t)(ipv6_hdr->proto << 24);
431 	if (ol_flags & PKT_TX_TCP_SEG) {
432 		psd_hdr.len = 0;
433 	} else {
434 		psd_hdr.len = ipv6_hdr->payload_len;
435 	}
436 
437 	sum = __rte_raw_cksum(ipv6_hdr->src_addr,
438 		sizeof(ipv6_hdr->src_addr) + sizeof(ipv6_hdr->dst_addr),
439 		0);
440 	sum = __rte_raw_cksum(&psd_hdr, sizeof(psd_hdr), sum);
441 	return __rte_raw_cksum_reduce(sum);
442 }
443 
444 /**
445  * Process the IPv6 UDP or TCP checksum.
446  *
447  * The IPv4 header should not contains options. The layer 4 checksum
448  * must be set to 0 in the packet by the caller.
449  *
450  * @param ipv6_hdr
451  *   The pointer to the contiguous IPv6 header.
452  * @param l4_hdr
453  *   The pointer to the beginning of the L4 header.
454  * @return
455  *   The complemented checksum to set in the IP packet.
456  */
457 static inline uint16_t
rte_ipv6_udptcp_cksum(const struct rte_ipv6_hdr * ipv6_hdr,const void * l4_hdr)458 rte_ipv6_udptcp_cksum(const struct rte_ipv6_hdr *ipv6_hdr, const void *l4_hdr)
459 {
460 	uint32_t cksum;
461 	uint32_t l4_len;
462 
463 	l4_len = rte_be_to_cpu_16(ipv6_hdr->payload_len);
464 
465 	cksum = rte_raw_cksum(l4_hdr, l4_len);
466 	cksum += rte_ipv6_phdr_cksum(ipv6_hdr, 0);
467 
468 	cksum = ((cksum & 0xffff0000) >> 16) + (cksum & 0xffff);
469 	cksum = (~cksum) & 0xffff;
470 	/*
471 	 * Per RFC 768: If the computed checksum is zero for UDP,
472 	 * it is transmitted as all ones
473 	 * (the equivalent in one's complement arithmetic).
474 	 */
475 	if (cksum == 0 && ipv6_hdr->proto == IPPROTO_UDP)
476 		cksum = 0xffff;
477 
478 	return (uint16_t)cksum;
479 }
480 
481 /** IPv6 fragment extension header. */
482 #define	RTE_IPV6_EHDR_MF_SHIFT	0
483 #define	RTE_IPV6_EHDR_MF_MASK	1
484 #define	RTE_IPV6_EHDR_FO_SHIFT	3
485 #define	RTE_IPV6_EHDR_FO_MASK	(~((1 << RTE_IPV6_EHDR_FO_SHIFT) - 1))
486 #define	RTE_IPV6_EHDR_FO_ALIGN	(1 << RTE_IPV6_EHDR_FO_SHIFT)
487 
488 #define RTE_IPV6_FRAG_USED_MASK	(RTE_IPV6_EHDR_MF_MASK | RTE_IPV6_EHDR_FO_MASK)
489 
490 #define RTE_IPV6_GET_MF(x)	((x) & RTE_IPV6_EHDR_MF_MASK)
491 #define RTE_IPV6_GET_FO(x)	((x) >> RTE_IPV6_EHDR_FO_SHIFT)
492 
493 #define RTE_IPV6_SET_FRAG_DATA(fo, mf)	\
494 	(((fo) & RTE_IPV6_EHDR_FO_MASK) | ((mf) & RTE_IPV6_EHDR_MF_MASK))
495 
496 struct rte_ipv6_fragment_ext {
497 	uint8_t next_header;	/**< Next header type */
498 	uint8_t reserved;	/**< Reserved */
499 	rte_be16_t frag_data;	/**< All fragmentation data */
500 	rte_be32_t id;		/**< Packet ID */
501 } __rte_packed;
502 
503 /* IPv6 fragment extension header size */
504 #define RTE_IPV6_FRAG_HDR_SIZE	sizeof(struct rte_ipv6_fragment_ext)
505 
506 /**
507  * Parse next IPv6 header extension
508  *
509  * This function checks if proto number is an IPv6 extensions and parses its
510  * data if so, providing information on next header and extension length.
511  *
512  * @param p
513  *   Pointer to an extension raw data.
514  * @param proto
515  *   Protocol number extracted from the "next header" field from
516  *   the IPv6 header or the previous extension.
517  * @param ext_len
518  *   Extension data length.
519  * @return
520  *   next protocol number if proto is an IPv6 extension, -EINVAL otherwise
521  */
522 __rte_experimental
523 static inline int
rte_ipv6_get_next_ext(const uint8_t * p,int proto,size_t * ext_len)524 rte_ipv6_get_next_ext(const uint8_t *p, int proto, size_t *ext_len)
525 {
526 	int next_proto;
527 
528 	switch (proto) {
529 	case IPPROTO_AH:
530 		next_proto = *p++;
531 		*ext_len = (*p + 2) * sizeof(uint32_t);
532 		break;
533 
534 	case IPPROTO_HOPOPTS:
535 	case IPPROTO_ROUTING:
536 	case IPPROTO_DSTOPTS:
537 		next_proto = *p++;
538 		*ext_len = (*p + 1) * sizeof(uint64_t);
539 		break;
540 
541 	case IPPROTO_FRAGMENT:
542 		next_proto = *p;
543 		*ext_len = RTE_IPV6_FRAG_HDR_SIZE;
544 		break;
545 
546 	default:
547 		return -EINVAL;
548 	}
549 
550 	return next_proto;
551 }
552 
553 #ifdef __cplusplus
554 }
555 #endif
556 
557 #endif /* _RTE_IP_H_ */
558