DPDK  21.08.0
rte_ip.h
Go to the documentation of this file.
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 
18 #include <stdint.h>
19 
20 #ifdef RTE_EXEC_ENV_WINDOWS
21 #include <ws2tcpip.h>
22 #else
23 #include <sys/socket.h>
24 #include <sys/types.h>
25 #include <netinet/in.h>
26 #include <arpa/inet.h>
27 #include <netinet/ip.h>
28 #endif
29 
30 #include <rte_byteorder.h>
31 #include <rte_mbuf.h>
32 
33 #ifdef __cplusplus
34 extern "C" {
35 #endif
36 
40 struct rte_ipv4_hdr {
41  uint8_t version_ihl;
42  uint8_t type_of_service;
46  uint8_t time_to_live;
47  uint8_t next_proto_id;
51 } __rte_packed;
52 
54 #define RTE_IPV4(a, b, c, d) ((uint32_t)(((a) & 0xff) << 24) | \
55  (((b) & 0xff) << 16) | \
56  (((c) & 0xff) << 8) | \
57  ((d) & 0xff))
58 
60 #define RTE_IPV4_MAX_PKT_LEN 65535
61 
63 #define RTE_IPV4_HDR_IHL_MASK (0x0f)
64 
68 #define RTE_IPV4_IHL_MULTIPLIER (4)
69 
70 /* Type of Service fields */
71 #define RTE_IPV4_HDR_DSCP_MASK (0xfc)
72 #define RTE_IPV4_HDR_ECN_MASK (0x03)
73 #define RTE_IPV4_HDR_ECN_CE RTE_IPV4_HDR_ECN_MASK
74 
75 /* Fragment Offset * Flags. */
76 #define RTE_IPV4_HDR_DF_SHIFT 14
77 #define RTE_IPV4_HDR_MF_SHIFT 13
78 #define RTE_IPV4_HDR_FO_SHIFT 3
79 
80 #define RTE_IPV4_HDR_DF_FLAG (1 << RTE_IPV4_HDR_DF_SHIFT)
81 #define RTE_IPV4_HDR_MF_FLAG (1 << RTE_IPV4_HDR_MF_SHIFT)
82 
83 #define RTE_IPV4_HDR_OFFSET_MASK ((1 << RTE_IPV4_HDR_MF_SHIFT) - 1)
84 
85 #define RTE_IPV4_HDR_OFFSET_UNITS 8
86 
87 /*
88  * IPv4 address types
89  */
90 #define RTE_IPV4_ANY ((uint32_t)0x00000000)
91 #define RTE_IPV4_LOOPBACK ((uint32_t)0x7f000001)
92 #define RTE_IPV4_BROADCAST ((uint32_t)0xe0000000)
93 #define RTE_IPV4_ALLHOSTS_GROUP ((uint32_t)0xe0000001)
94 #define RTE_IPV4_ALLRTRS_GROUP ((uint32_t)0xe0000002)
95 #define RTE_IPV4_MAX_LOCAL_GROUP ((uint32_t)0xe00000ff)
97 /*
98  * IPv4 Multicast-related macros
99  */
100 #define RTE_IPV4_MIN_MCAST \
101  RTE_IPV4(224, 0, 0, 0)
102 #define RTE_IPV4_MAX_MCAST \
103  RTE_IPV4(239, 255, 255, 255)
105 #define RTE_IS_IPV4_MCAST(x) \
106  ((x) >= RTE_IPV4_MIN_MCAST && (x) <= RTE_IPV4_MAX_MCAST)
107 
109 /* IPv4 default fields values */
110 #define RTE_IPV4_MIN_IHL (0x5)
111 #define RTE_IPV4_VHL_DEF ((IPVERSION << 4) | RTE_IPV4_MIN_IHL)
112 
121 static inline uint8_t
122 rte_ipv4_hdr_len(const struct rte_ipv4_hdr *ipv4_hdr)
123 {
124  return (uint8_t)((ipv4_hdr->version_ihl & RTE_IPV4_HDR_IHL_MASK) *
126 }
127 
141 static inline uint32_t
142 __rte_raw_cksum(const void *buf, size_t len, uint32_t sum)
143 {
144  /* workaround gcc strict-aliasing warning */
145  uintptr_t ptr = (uintptr_t)buf;
146  typedef uint16_t __attribute__((__may_alias__)) u16_p;
147  const u16_p *u16_buf = (const u16_p *)ptr;
148 
149  while (len >= (sizeof(*u16_buf) * 4)) {
150  sum += u16_buf[0];
151  sum += u16_buf[1];
152  sum += u16_buf[2];
153  sum += u16_buf[3];
154  len -= sizeof(*u16_buf) * 4;
155  u16_buf += 4;
156  }
157  while (len >= sizeof(*u16_buf)) {
158  sum += *u16_buf;
159  len -= sizeof(*u16_buf);
160  u16_buf += 1;
161  }
162 
163  /* if length is in odd bytes */
164  if (len == 1) {
165  uint16_t left = 0;
166  *(uint8_t *)&left = *(const uint8_t *)u16_buf;
167  sum += left;
168  }
169 
170  return sum;
171 }
172 
182 static inline uint16_t
183 __rte_raw_cksum_reduce(uint32_t sum)
184 {
185  sum = ((sum & 0xffff0000) >> 16) + (sum & 0xffff);
186  sum = ((sum & 0xffff0000) >> 16) + (sum & 0xffff);
187  return (uint16_t)sum;
188 }
189 
200 static inline uint16_t
201 rte_raw_cksum(const void *buf, size_t len)
202 {
203  uint32_t sum;
204 
205  sum = __rte_raw_cksum(buf, len, 0);
206  return __rte_raw_cksum_reduce(sum);
207 }
208 
223 static inline int
224 rte_raw_cksum_mbuf(const struct rte_mbuf *m, uint32_t off, uint32_t len,
225  uint16_t *cksum)
226 {
227  const struct rte_mbuf *seg;
228  const char *buf;
229  uint32_t sum, tmp;
230  uint32_t seglen, done;
231 
232  /* easy case: all data in the first segment */
233  if (off + len <= rte_pktmbuf_data_len(m)) {
235  const char *, off), len);
236  return 0;
237  }
238 
239  if (unlikely(off + len > rte_pktmbuf_pkt_len(m)))
240  return -1; /* invalid params, return a dummy value */
241 
242  /* else browse the segment to find offset */
243  seglen = 0;
244  for (seg = m; seg != NULL; seg = seg->next) {
245  seglen = rte_pktmbuf_data_len(seg);
246  if (off < seglen)
247  break;
248  off -= seglen;
249  }
250  RTE_ASSERT(seg != NULL);
251  if (seg == NULL)
252  return -1;
253  seglen -= off;
254  buf = rte_pktmbuf_mtod_offset(seg, const char *, off);
255  if (seglen >= len) {
256  /* all in one segment */
257  *cksum = rte_raw_cksum(buf, len);
258  return 0;
259  }
260 
261  /* hard case: process checksum of several segments */
262  sum = 0;
263  done = 0;
264  for (;;) {
265  tmp = __rte_raw_cksum(buf, seglen, 0);
266  if (done & 1)
267  tmp = rte_bswap16((uint16_t)tmp);
268  sum += tmp;
269  done += seglen;
270  if (done == len)
271  break;
272  seg = seg->next;
273  buf = rte_pktmbuf_mtod(seg, const char *);
274  seglen = rte_pktmbuf_data_len(seg);
275  if (seglen > len - done)
276  seglen = len - done;
277  }
278 
279  *cksum = __rte_raw_cksum_reduce(sum);
280  return 0;
281 }
282 
293 static inline uint16_t
294 rte_ipv4_cksum(const struct rte_ipv4_hdr *ipv4_hdr)
295 {
296  uint16_t cksum;
297  cksum = rte_raw_cksum(ipv4_hdr, rte_ipv4_hdr_len(ipv4_hdr));
298  return (uint16_t)~cksum;
299 }
300 
319 static inline uint16_t
320 rte_ipv4_phdr_cksum(const struct rte_ipv4_hdr *ipv4_hdr, uint64_t ol_flags)
321 {
322  struct ipv4_psd_header {
323  uint32_t src_addr; /* IP address of source host. */
324  uint32_t dst_addr; /* IP address of destination host. */
325  uint8_t zero; /* zero. */
326  uint8_t proto; /* L4 protocol type. */
327  uint16_t len; /* L4 length. */
328  } psd_hdr;
329 
330  uint32_t l3_len;
331 
332  psd_hdr.src_addr = ipv4_hdr->src_addr;
333  psd_hdr.dst_addr = ipv4_hdr->dst_addr;
334  psd_hdr.zero = 0;
335  psd_hdr.proto = ipv4_hdr->next_proto_id;
336  if (ol_flags & PKT_TX_TCP_SEG) {
337  psd_hdr.len = 0;
338  } else {
339  l3_len = rte_be_to_cpu_16(ipv4_hdr->total_length);
340  psd_hdr.len = rte_cpu_to_be_16((uint16_t)(l3_len -
341  rte_ipv4_hdr_len(ipv4_hdr)));
342  }
343  return rte_raw_cksum(&psd_hdr, sizeof(psd_hdr));
344 }
345 
349 static inline uint16_t
350 __rte_ipv4_udptcp_cksum(const struct rte_ipv4_hdr *ipv4_hdr, const void *l4_hdr)
351 {
352  uint32_t cksum;
353  uint32_t l3_len, l4_len;
354  uint8_t ip_hdr_len;
355 
356  ip_hdr_len = rte_ipv4_hdr_len(ipv4_hdr);
357  l3_len = rte_be_to_cpu_16(ipv4_hdr->total_length);
358  if (l3_len < ip_hdr_len)
359  return 0;
360 
361  l4_len = l3_len - ip_hdr_len;
362 
363  cksum = rte_raw_cksum(l4_hdr, l4_len);
364  cksum += rte_ipv4_phdr_cksum(ipv4_hdr, 0);
365 
366  cksum = ((cksum & 0xffff0000) >> 16) + (cksum & 0xffff);
367 
368  return (uint16_t)cksum;
369 }
370 
384 static inline uint16_t
385 rte_ipv4_udptcp_cksum(const struct rte_ipv4_hdr *ipv4_hdr, const void *l4_hdr)
386 {
387  uint16_t cksum = __rte_ipv4_udptcp_cksum(ipv4_hdr, l4_hdr);
388 
389  cksum = ~cksum;
390 
391  /*
392  * Per RFC 768: If the computed checksum is zero for UDP,
393  * it is transmitted as all ones
394  * (the equivalent in one's complement arithmetic).
395  */
396  if (cksum == 0 && ipv4_hdr->next_proto_id == IPPROTO_UDP)
397  cksum = 0xffff;
398 
399  return cksum;
400 }
401 
415 __rte_experimental
416 static inline int
417 rte_ipv4_udptcp_cksum_verify(const struct rte_ipv4_hdr *ipv4_hdr,
418  const void *l4_hdr)
419 {
420  uint16_t cksum = __rte_ipv4_udptcp_cksum(ipv4_hdr, l4_hdr);
421 
422  if (cksum != 0xffff)
423  return -1;
424 
425  return 0;
426 }
427 
431 struct rte_ipv6_hdr {
432  rte_be32_t vtc_flow;
433  rte_be16_t payload_len;
434  uint8_t proto;
435  uint8_t hop_limits;
436  uint8_t src_addr[16];
437  uint8_t dst_addr[16];
440 /* IPv6 vtc_flow: IPv / TC / flow_label */
441 #define RTE_IPV6_HDR_FL_SHIFT 0
442 #define RTE_IPV6_HDR_TC_SHIFT 20
443 #define RTE_IPV6_HDR_FL_MASK ((1u << RTE_IPV6_HDR_TC_SHIFT) - 1)
444 #define RTE_IPV6_HDR_TC_MASK (0xff << RTE_IPV6_HDR_TC_SHIFT)
445 #define RTE_IPV6_HDR_DSCP_MASK (0xfc << RTE_IPV6_HDR_TC_SHIFT)
446 #define RTE_IPV6_HDR_ECN_MASK (0x03 << RTE_IPV6_HDR_TC_SHIFT)
447 #define RTE_IPV6_HDR_ECN_CE RTE_IPV6_HDR_ECN_MASK
448 
449 #define RTE_IPV6_MIN_MTU 1280
467 static inline uint16_t
468 rte_ipv6_phdr_cksum(const struct rte_ipv6_hdr *ipv6_hdr, uint64_t ol_flags)
469 {
470  uint32_t sum;
471  struct {
472  rte_be32_t len; /* L4 length. */
473  rte_be32_t proto; /* L4 protocol - top 3 bytes must be zero */
474  } psd_hdr;
475 
476  psd_hdr.proto = (uint32_t)(ipv6_hdr->proto << 24);
477  if (ol_flags & PKT_TX_TCP_SEG) {
478  psd_hdr.len = 0;
479  } else {
480  psd_hdr.len = ipv6_hdr->payload_len;
481  }
482 
483  sum = __rte_raw_cksum(ipv6_hdr->src_addr,
484  sizeof(ipv6_hdr->src_addr) + sizeof(ipv6_hdr->dst_addr),
485  0);
486  sum = __rte_raw_cksum(&psd_hdr, sizeof(psd_hdr), sum);
487  return __rte_raw_cksum_reduce(sum);
488 }
489 
493 static inline uint16_t
494 __rte_ipv6_udptcp_cksum(const struct rte_ipv6_hdr *ipv6_hdr, const void *l4_hdr)
495 {
496  uint32_t cksum;
497  uint32_t l4_len;
498 
499  l4_len = rte_be_to_cpu_16(ipv6_hdr->payload_len);
500 
501  cksum = rte_raw_cksum(l4_hdr, l4_len);
502  cksum += rte_ipv6_phdr_cksum(ipv6_hdr, 0);
503 
504  cksum = ((cksum & 0xffff0000) >> 16) + (cksum & 0xffff);
505 
506  return (uint16_t)cksum;
507 }
508 
522 static inline uint16_t
523 rte_ipv6_udptcp_cksum(const struct rte_ipv6_hdr *ipv6_hdr, const void *l4_hdr)
524 {
525  uint16_t cksum = __rte_ipv6_udptcp_cksum(ipv6_hdr, l4_hdr);
526 
527  cksum = ~cksum;
528 
529  /*
530  * Per RFC 768: If the computed checksum is zero for UDP,
531  * it is transmitted as all ones
532  * (the equivalent in one's complement arithmetic).
533  */
534  if (cksum == 0 && ipv6_hdr->proto == IPPROTO_UDP)
535  cksum = 0xffff;
536 
537  return cksum;
538 }
539 
554 __rte_experimental
555 static inline int
556 rte_ipv6_udptcp_cksum_verify(const struct rte_ipv6_hdr *ipv6_hdr,
557  const void *l4_hdr)
558 {
559  uint16_t cksum = __rte_ipv6_udptcp_cksum(ipv6_hdr, l4_hdr);
560 
561  if (cksum != 0xffff)
562  return -1;
563 
564  return 0;
565 }
566 
568 #define RTE_IPV6_EHDR_MF_SHIFT 0
569 #define RTE_IPV6_EHDR_MF_MASK 1
570 #define RTE_IPV6_EHDR_FO_SHIFT 3
571 #define RTE_IPV6_EHDR_FO_MASK (~((1 << RTE_IPV6_EHDR_FO_SHIFT) - 1))
572 #define RTE_IPV6_EHDR_FO_ALIGN (1 << RTE_IPV6_EHDR_FO_SHIFT)
573 
574 #define RTE_IPV6_FRAG_USED_MASK (RTE_IPV6_EHDR_MF_MASK | RTE_IPV6_EHDR_FO_MASK)
575 
576 #define RTE_IPV6_GET_MF(x) ((x) & RTE_IPV6_EHDR_MF_MASK)
577 #define RTE_IPV6_GET_FO(x) ((x) >> RTE_IPV6_EHDR_FO_SHIFT)
578 
579 #define RTE_IPV6_SET_FRAG_DATA(fo, mf) \
580  (((fo) & RTE_IPV6_EHDR_FO_MASK) | ((mf) & RTE_IPV6_EHDR_MF_MASK))
581 
582 struct rte_ipv6_fragment_ext {
583  uint8_t next_header;
584  uint8_t reserved;
585  rte_be16_t frag_data;
586  rte_be32_t id;
587 } __rte_packed;
588 
589 /* IPv6 fragment extension header size */
590 #define RTE_IPV6_FRAG_HDR_SIZE sizeof(struct rte_ipv6_fragment_ext)
591 
608 __rte_experimental
609 static inline int
610 rte_ipv6_get_next_ext(const uint8_t *p, int proto, size_t *ext_len)
611 {
612  int next_proto;
613 
614  switch (proto) {
615  case IPPROTO_AH:
616  next_proto = *p++;
617  *ext_len = (*p + 2) * sizeof(uint32_t);
618  break;
619 
620  case IPPROTO_HOPOPTS:
621  case IPPROTO_ROUTING:
622  case IPPROTO_DSTOPTS:
623  next_proto = *p++;
624  *ext_len = (*p + 1) * sizeof(uint64_t);
625  break;
626 
627  case IPPROTO_FRAGMENT:
628  next_proto = *p;
629  *ext_len = RTE_IPV6_FRAG_HDR_SIZE;
630  break;
631 
632  default:
633  return -EINVAL;
634  }
635 
636  return next_proto;
637 }
638 
639 #ifdef __cplusplus
640 }
641 #endif
642 
643 #endif /* _RTE_IP_H_ */
static uint8_t rte_ipv4_hdr_len(const struct rte_ipv4_hdr *ipv4_hdr)
Definition: rte_ip.h:124
struct rte_mbuf * next
#define __rte_packed
Definition: rte_common.h:86
static int rte_raw_cksum_mbuf(const struct rte_mbuf *m, uint32_t off, uint32_t len, uint16_t *cksum)
Definition: rte_ip.h:226
static uint16_t rte_ipv4_udptcp_cksum(const struct rte_ipv4_hdr *ipv4_hdr, const void *l4_hdr)
Definition: rte_ip.h:387
uint32_t rte_be32_t
uint8_t dst_addr[16]
Definition: rte_ip.h:439
rte_be16_t fragment_offset
Definition: rte_ip.h:45
uint8_t version_ihl
Definition: rte_ip.h:41
static uint16_t rte_bswap16(uint16_t _x)
static __rte_experimental int rte_ipv6_udptcp_cksum_verify(const struct rte_ipv6_hdr *ipv6_hdr, const void *l4_hdr)
Definition: rte_ip.h:558
rte_be32_t dst_addr
Definition: rte_ip.h:50
rte_be32_t src_addr
Definition: rte_ip.h:49
uint8_t src_addr[16]
Definition: rte_ip.h:438
static __rte_experimental int rte_ipv6_get_next_ext(const uint8_t *p, int proto, size_t *ext_len)
Definition: rte_ip.h:612
#define rte_pktmbuf_mtod_offset(m, t, o)
static rte_be16_t rte_cpu_to_be_16(uint16_t x)
#define unlikely(x)
static uint16_t rte_ipv4_phdr_cksum(const struct rte_ipv4_hdr *ipv4_hdr, uint64_t ol_flags)
Definition: rte_ip.h:322
static uint16_t rte_ipv6_phdr_cksum(const struct rte_ipv6_hdr *ipv6_hdr, uint64_t ol_flags)
Definition: rte_ip.h:470
#define PKT_TX_TCP_SEG
#define rte_pktmbuf_mtod(m, t)
#define rte_pktmbuf_pkt_len(m)
Definition: rte_mbuf.h:1551
uint8_t type_of_service
Definition: rte_ip.h:42
uint8_t time_to_live
Definition: rte_ip.h:46
uint8_t proto
Definition: rte_ip.h:436
rte_be16_t packet_id
Definition: rte_ip.h:44
static uint16_t rte_raw_cksum(const void *buf, size_t len)
Definition: rte_ip.h:203
uint64_t ol_flags
#define rte_pktmbuf_data_len(m)
Definition: rte_mbuf.h:1561
uint8_t next_proto_id
Definition: rte_ip.h:47
rte_be16_t payload_len
Definition: rte_ip.h:435
rte_be16_t total_length
Definition: rte_ip.h:43
uint16_t rte_be16_t
#define RTE_IPV4_HDR_IHL_MASK
Definition: rte_ip.h:63
static uint16_t rte_be_to_cpu_16(rte_be16_t x)
static __rte_experimental int rte_ipv4_udptcp_cksum_verify(const struct rte_ipv4_hdr *ipv4_hdr, const void *l4_hdr)
Definition: rte_ip.h:419
rte_be16_t hdr_checksum
Definition: rte_ip.h:48
static uint16_t rte_ipv4_cksum(const struct rte_ipv4_hdr *ipv4_hdr)
Definition: rte_ip.h:296
static uint16_t rte_ipv6_udptcp_cksum(const struct rte_ipv6_hdr *ipv6_hdr, const void *l4_hdr)
Definition: rte_ip.h:525
#define RTE_IPV4_IHL_MULTIPLIER
Definition: rte_ip.h:68