DPDK  21.11.7
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 #include <netinet/ip6.h>
29 #endif
30 
31 #include <rte_byteorder.h>
32 #include <rte_mbuf.h>
33 
34 #ifdef __cplusplus
35 extern "C" {
36 #endif
37 
41 struct rte_ipv4_hdr {
42  __extension__
43  union {
44  uint8_t version_ihl;
45  struct {
46 #if RTE_BYTE_ORDER == RTE_LITTLE_ENDIAN
47  uint8_t ihl:4;
48  uint8_t version:4;
49 #elif RTE_BYTE_ORDER == RTE_BIG_ENDIAN
50  uint8_t version:4;
51  uint8_t ihl:4;
52 #endif
53  };
54  };
55  uint8_t type_of_service;
59  uint8_t time_to_live;
60  uint8_t next_proto_id;
64 } __rte_packed;
65 
67 #define RTE_IPV4(a, b, c, d) ((uint32_t)(((a) & 0xff) << 24) | \
68  (((b) & 0xff) << 16) | \
69  (((c) & 0xff) << 8) | \
70  ((d) & 0xff))
71 
73 #define RTE_IPV4_MAX_PKT_LEN 65535
74 
76 #define RTE_IPV4_HDR_IHL_MASK (0x0f)
77 
81 #define RTE_IPV4_IHL_MULTIPLIER (4)
82 
83 /* Type of Service fields */
84 #define RTE_IPV4_HDR_DSCP_MASK (0xfc)
85 #define RTE_IPV4_HDR_ECN_MASK (0x03)
86 #define RTE_IPV4_HDR_ECN_CE RTE_IPV4_HDR_ECN_MASK
87 
88 /* Fragment Offset * Flags. */
89 #define RTE_IPV4_HDR_DF_SHIFT 14
90 #define RTE_IPV4_HDR_MF_SHIFT 13
91 #define RTE_IPV4_HDR_FO_SHIFT 3
92 
93 #define RTE_IPV4_HDR_DF_FLAG (1 << RTE_IPV4_HDR_DF_SHIFT)
94 #define RTE_IPV4_HDR_MF_FLAG (1 << RTE_IPV4_HDR_MF_SHIFT)
95 
96 #define RTE_IPV4_HDR_OFFSET_MASK ((1 << RTE_IPV4_HDR_MF_SHIFT) - 1)
97 
98 #define RTE_IPV4_HDR_OFFSET_UNITS 8
99 
100 /*
101  * IPv4 address types
102  */
103 #define RTE_IPV4_ANY ((uint32_t)0x00000000)
104 #define RTE_IPV4_LOOPBACK ((uint32_t)0x7f000001)
105 #define RTE_IPV4_BROADCAST ((uint32_t)0xe0000000)
106 #define RTE_IPV4_ALLHOSTS_GROUP ((uint32_t)0xe0000001)
107 #define RTE_IPV4_ALLRTRS_GROUP ((uint32_t)0xe0000002)
108 #define RTE_IPV4_MAX_LOCAL_GROUP ((uint32_t)0xe00000ff)
110 /*
111  * IPv4 Multicast-related macros
112  */
113 #define RTE_IPV4_MIN_MCAST \
114  RTE_IPV4(224, 0, 0, 0)
115 #define RTE_IPV4_MAX_MCAST \
116  RTE_IPV4(239, 255, 255, 255)
118 #define RTE_IS_IPV4_MCAST(x) \
119  ((x) >= RTE_IPV4_MIN_MCAST && (x) <= RTE_IPV4_MAX_MCAST)
120 
122 /* IPv4 default fields values */
123 #define RTE_IPV4_MIN_IHL (0x5)
124 #define RTE_IPV4_VHL_DEF ((IPVERSION << 4) | RTE_IPV4_MIN_IHL)
125 
134 static inline uint8_t
135 rte_ipv4_hdr_len(const struct rte_ipv4_hdr *ipv4_hdr)
136 {
137  return (uint8_t)((ipv4_hdr->version_ihl & RTE_IPV4_HDR_IHL_MASK) *
139 }
140 
154 static inline uint32_t
155 __rte_raw_cksum(const void *buf, size_t len, uint32_t sum)
156 {
157  const void *end;
158 
159  for (end = RTE_PTR_ADD(buf, RTE_ALIGN_FLOOR(len, sizeof(uint16_t)));
160  buf != end; buf = RTE_PTR_ADD(buf, sizeof(uint16_t))) {
161  uint16_t v;
162 
163  memcpy(&v, buf, sizeof(uint16_t));
164  sum += v;
165  }
166 
167  /* if length is odd, keeping it byte order independent */
168  if (unlikely(len % 2)) {
169  uint16_t left = 0;
170 
171  memcpy(&left, end, 1);
172  sum += left;
173  }
174 
175  return sum;
176 }
177 
187 static inline uint16_t
188 __rte_raw_cksum_reduce(uint32_t sum)
189 {
190  sum = ((sum & 0xffff0000) >> 16) + (sum & 0xffff);
191  sum = ((sum & 0xffff0000) >> 16) + (sum & 0xffff);
192  return (uint16_t)sum;
193 }
194 
205 static inline uint16_t
206 rte_raw_cksum(const void *buf, size_t len)
207 {
208  uint32_t sum;
209 
210  sum = __rte_raw_cksum(buf, len, 0);
211  return __rte_raw_cksum_reduce(sum);
212 }
213 
228 static inline int
229 rte_raw_cksum_mbuf(const struct rte_mbuf *m, uint32_t off, uint32_t len,
230  uint16_t *cksum)
231 {
232  const struct rte_mbuf *seg;
233  const char *buf;
234  uint32_t sum, tmp;
235  uint32_t seglen, done;
236 
237  /* easy case: all data in the first segment */
238  if (off + len <= rte_pktmbuf_data_len(m)) {
240  const char *, off), len);
241  return 0;
242  }
243 
244  if (unlikely(off + len > rte_pktmbuf_pkt_len(m)))
245  return -1; /* invalid params, return a dummy value */
246 
247  /* else browse the segment to find offset */
248  seglen = 0;
249  for (seg = m; seg != NULL; seg = seg->next) {
250  seglen = rte_pktmbuf_data_len(seg);
251  if (off < seglen)
252  break;
253  off -= seglen;
254  }
255  RTE_ASSERT(seg != NULL);
256  if (seg == NULL)
257  return -1;
258  seglen -= off;
259  buf = rte_pktmbuf_mtod_offset(seg, const char *, off);
260  if (seglen >= len) {
261  /* all in one segment */
262  *cksum = rte_raw_cksum(buf, len);
263  return 0;
264  }
265 
266  /* hard case: process checksum of several segments */
267  sum = 0;
268  done = 0;
269  for (;;) {
270  tmp = __rte_raw_cksum(buf, seglen, 0);
271  if (done & 1)
272  tmp = rte_bswap16((uint16_t)tmp);
273  sum += tmp;
274  done += seglen;
275  if (done == len)
276  break;
277  seg = seg->next;
278  buf = rte_pktmbuf_mtod(seg, const char *);
279  seglen = rte_pktmbuf_data_len(seg);
280  if (seglen > len - done)
281  seglen = len - done;
282  }
283 
284  *cksum = __rte_raw_cksum_reduce(sum);
285  return 0;
286 }
287 
298 static inline uint16_t
299 rte_ipv4_cksum(const struct rte_ipv4_hdr *ipv4_hdr)
300 {
301  uint16_t cksum;
302  cksum = rte_raw_cksum(ipv4_hdr, rte_ipv4_hdr_len(ipv4_hdr));
303  return (uint16_t)~cksum;
304 }
305 
324 static inline uint16_t
325 rte_ipv4_phdr_cksum(const struct rte_ipv4_hdr *ipv4_hdr, uint64_t ol_flags)
326 {
327  struct ipv4_psd_header {
328  uint32_t src_addr; /* IP address of source host. */
329  uint32_t dst_addr; /* IP address of destination host. */
330  uint8_t zero; /* zero. */
331  uint8_t proto; /* L4 protocol type. */
332  uint16_t len; /* L4 length. */
333  } psd_hdr;
334 
335  uint32_t l3_len;
336 
337  psd_hdr.src_addr = ipv4_hdr->src_addr;
338  psd_hdr.dst_addr = ipv4_hdr->dst_addr;
339  psd_hdr.zero = 0;
340  psd_hdr.proto = ipv4_hdr->next_proto_id;
341  if (ol_flags & RTE_MBUF_F_TX_TCP_SEG) {
342  psd_hdr.len = 0;
343  } else {
344  l3_len = rte_be_to_cpu_16(ipv4_hdr->total_length);
345  psd_hdr.len = rte_cpu_to_be_16((uint16_t)(l3_len -
346  rte_ipv4_hdr_len(ipv4_hdr)));
347  }
348  return rte_raw_cksum(&psd_hdr, sizeof(psd_hdr));
349 }
350 
354 static inline uint16_t
355 __rte_ipv4_udptcp_cksum(const struct rte_ipv4_hdr *ipv4_hdr, const void *l4_hdr)
356 {
357  uint32_t cksum;
358  uint32_t l3_len, l4_len;
359  uint8_t ip_hdr_len;
360 
361  ip_hdr_len = rte_ipv4_hdr_len(ipv4_hdr);
362  l3_len = rte_be_to_cpu_16(ipv4_hdr->total_length);
363  if (l3_len < ip_hdr_len)
364  return 0;
365 
366  l4_len = l3_len - ip_hdr_len;
367 
368  cksum = rte_raw_cksum(l4_hdr, l4_len);
369  cksum += rte_ipv4_phdr_cksum(ipv4_hdr, 0);
370 
371  cksum = ((cksum & 0xffff0000) >> 16) + (cksum & 0xffff);
372 
373  return (uint16_t)cksum;
374 }
375 
388 static inline uint16_t
389 rte_ipv4_udptcp_cksum(const struct rte_ipv4_hdr *ipv4_hdr, const void *l4_hdr)
390 {
391  uint16_t cksum = __rte_ipv4_udptcp_cksum(ipv4_hdr, l4_hdr);
392 
393  cksum = ~cksum;
394 
395  /*
396  * Per RFC 768: If the computed checksum is zero for UDP,
397  * it is transmitted as all ones
398  * (the equivalent in one's complement arithmetic).
399  */
400  if (cksum == 0 && ipv4_hdr->next_proto_id == IPPROTO_UDP)
401  cksum = 0xffff;
402 
403  return cksum;
404 }
405 
419 __rte_experimental
420 static inline int
421 rte_ipv4_udptcp_cksum_verify(const struct rte_ipv4_hdr *ipv4_hdr,
422  const void *l4_hdr)
423 {
424  uint16_t cksum = __rte_ipv4_udptcp_cksum(ipv4_hdr, l4_hdr);
425 
426  if (cksum != 0xffff)
427  return -1;
428 
429  return 0;
430 }
431 
435 struct rte_ipv6_hdr {
436  rte_be32_t vtc_flow;
437  rte_be16_t payload_len;
438  uint8_t proto;
439  uint8_t hop_limits;
440  uint8_t src_addr[16];
441  uint8_t dst_addr[16];
444 /* IPv6 vtc_flow: IPv / TC / flow_label */
445 #define RTE_IPV6_HDR_FL_SHIFT 0
446 #define RTE_IPV6_HDR_TC_SHIFT 20
447 #define RTE_IPV6_HDR_FL_MASK ((1u << RTE_IPV6_HDR_TC_SHIFT) - 1)
448 #define RTE_IPV6_HDR_TC_MASK (0xff << RTE_IPV6_HDR_TC_SHIFT)
449 #define RTE_IPV6_HDR_DSCP_MASK (0xfc << RTE_IPV6_HDR_TC_SHIFT)
450 #define RTE_IPV6_HDR_ECN_MASK (0x03 << RTE_IPV6_HDR_TC_SHIFT)
451 #define RTE_IPV6_HDR_ECN_CE RTE_IPV6_HDR_ECN_MASK
452 
453 #define RTE_IPV6_MIN_MTU 1280
471 static inline uint16_t
472 rte_ipv6_phdr_cksum(const struct rte_ipv6_hdr *ipv6_hdr, uint64_t ol_flags)
473 {
474  uint32_t sum;
475  struct {
476  rte_be32_t len; /* L4 length. */
477  rte_be32_t proto; /* L4 protocol - top 3 bytes must be zero */
478  } psd_hdr;
479 
480  psd_hdr.proto = (uint32_t)(ipv6_hdr->proto << 24);
481  if (ol_flags & RTE_MBUF_F_TX_TCP_SEG) {
482  psd_hdr.len = 0;
483  } else {
484  psd_hdr.len = ipv6_hdr->payload_len;
485  }
486 
487  sum = __rte_raw_cksum(ipv6_hdr->src_addr,
488  sizeof(ipv6_hdr->src_addr) + sizeof(ipv6_hdr->dst_addr),
489  0);
490  sum = __rte_raw_cksum(&psd_hdr, sizeof(psd_hdr), sum);
491  return __rte_raw_cksum_reduce(sum);
492 }
493 
497 static inline uint16_t
498 __rte_ipv6_udptcp_cksum(const struct rte_ipv6_hdr *ipv6_hdr, const void *l4_hdr)
499 {
500  uint32_t cksum;
501  uint32_t l4_len;
502 
503  l4_len = rte_be_to_cpu_16(ipv6_hdr->payload_len);
504 
505  cksum = rte_raw_cksum(l4_hdr, l4_len);
506  cksum += rte_ipv6_phdr_cksum(ipv6_hdr, 0);
507 
508  cksum = ((cksum & 0xffff0000) >> 16) + (cksum & 0xffff);
509 
510  return (uint16_t)cksum;
511 }
512 
526 static inline uint16_t
527 rte_ipv6_udptcp_cksum(const struct rte_ipv6_hdr *ipv6_hdr, const void *l4_hdr)
528 {
529  uint16_t cksum = __rte_ipv6_udptcp_cksum(ipv6_hdr, l4_hdr);
530 
531  cksum = ~cksum;
532 
533  /*
534  * Per RFC 768: If the computed checksum is zero for UDP,
535  * it is transmitted as all ones
536  * (the equivalent in one's complement arithmetic).
537  */
538  if (cksum == 0 && ipv6_hdr->proto == IPPROTO_UDP)
539  cksum = 0xffff;
540 
541  return cksum;
542 }
543 
558 __rte_experimental
559 static inline int
560 rte_ipv6_udptcp_cksum_verify(const struct rte_ipv6_hdr *ipv6_hdr,
561  const void *l4_hdr)
562 {
563  uint16_t cksum = __rte_ipv6_udptcp_cksum(ipv6_hdr, l4_hdr);
564 
565  if (cksum != 0xffff)
566  return -1;
567 
568  return 0;
569 }
570 
572 #define RTE_IPV6_EHDR_MF_SHIFT 0
573 #define RTE_IPV6_EHDR_MF_MASK 1
574 #define RTE_IPV6_EHDR_FO_SHIFT 3
575 #define RTE_IPV6_EHDR_FO_MASK (~((1 << RTE_IPV6_EHDR_FO_SHIFT) - 1))
576 #define RTE_IPV6_EHDR_FO_ALIGN (1 << RTE_IPV6_EHDR_FO_SHIFT)
577 
578 #define RTE_IPV6_FRAG_USED_MASK (RTE_IPV6_EHDR_MF_MASK | RTE_IPV6_EHDR_FO_MASK)
579 
580 #define RTE_IPV6_GET_MF(x) ((x) & RTE_IPV6_EHDR_MF_MASK)
581 #define RTE_IPV6_GET_FO(x) ((x) >> RTE_IPV6_EHDR_FO_SHIFT)
582 
583 #define RTE_IPV6_SET_FRAG_DATA(fo, mf) \
584  (((fo) & RTE_IPV6_EHDR_FO_MASK) | ((mf) & RTE_IPV6_EHDR_MF_MASK))
585 
586 struct rte_ipv6_fragment_ext {
587  uint8_t next_header;
588  uint8_t reserved;
589  rte_be16_t frag_data;
590  rte_be32_t id;
591 } __rte_packed;
592 
593 /* IPv6 fragment extension header size */
594 #define RTE_IPV6_FRAG_HDR_SIZE sizeof(struct rte_ipv6_fragment_ext)
595 
612 __rte_experimental
613 static inline int
614 rte_ipv6_get_next_ext(const uint8_t *p, int proto, size_t *ext_len)
615 {
616  int next_proto;
617 
618  switch (proto) {
619  case IPPROTO_AH:
620  next_proto = *p++;
621  *ext_len = (*p + 2) * sizeof(uint32_t);
622  break;
623 
624  case IPPROTO_HOPOPTS:
625  case IPPROTO_ROUTING:
626  case IPPROTO_DSTOPTS:
627  next_proto = *p++;
628  *ext_len = (*p + 1) * sizeof(uint64_t);
629  break;
630 
631  case IPPROTO_FRAGMENT:
632  next_proto = *p;
633  *ext_len = RTE_IPV6_FRAG_HDR_SIZE;
634  break;
635 
636  default:
637  return -EINVAL;
638  }
639 
640  return next_proto;
641 }
642 
643 #ifdef __cplusplus
644 }
645 #endif
646 
647 #endif /* _RTE_IP_H_ */
static uint8_t rte_ipv4_hdr_len(const struct rte_ipv4_hdr *ipv4_hdr)
Definition: rte_ip.h:137
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:231
#define RTE_ALIGN_FLOOR(val, align)
Definition: rte_common.h:306
static uint16_t rte_ipv4_udptcp_cksum(const struct rte_ipv4_hdr *ipv4_hdr, const void *l4_hdr)
Definition: rte_ip.h:391
uint32_t rte_be32_t
uint8_t dst_addr[16]
Definition: rte_ip.h:443
rte_be16_t fragment_offset
Definition: rte_ip.h:58
uint8_t version_ihl
Definition: rte_ip.h:44
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:562
rte_be32_t dst_addr
Definition: rte_ip.h:63
rte_be32_t src_addr
Definition: rte_ip.h:62
uint8_t src_addr[16]
Definition: rte_ip.h:442
static __rte_experimental int rte_ipv6_get_next_ext(const uint8_t *p, int proto, size_t *ext_len)
Definition: rte_ip.h:616
#define rte_pktmbuf_mtod_offset(m, t, o)
#define RTE_PTR_ADD(ptr, x)
Definition: rte_common.h:268
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:327
static uint16_t rte_ipv6_phdr_cksum(const struct rte_ipv6_hdr *ipv6_hdr, uint64_t ol_flags)
Definition: rte_ip.h:474
#define rte_pktmbuf_mtod(m, t)
#define rte_pktmbuf_pkt_len(m)
Definition: rte_mbuf.h:1521
uint8_t type_of_service
Definition: rte_ip.h:55
uint8_t time_to_live
Definition: rte_ip.h:59
uint8_t proto
Definition: rte_ip.h:440
rte_be16_t packet_id
Definition: rte_ip.h:57
static uint16_t rte_raw_cksum(const void *buf, size_t len)
Definition: rte_ip.h:208
uint64_t ol_flags
#define rte_pktmbuf_data_len(m)
Definition: rte_mbuf.h:1531
#define RTE_MBUF_F_TX_TCP_SEG
uint8_t next_proto_id
Definition: rte_ip.h:60
rte_be16_t payload_len
Definition: rte_ip.h:439
rte_be16_t total_length
Definition: rte_ip.h:56
uint8_t ihl
Definition: rte_ip.h:47
uint16_t rte_be16_t
#define RTE_IPV4_HDR_IHL_MASK
Definition: rte_ip.h:76
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:423
rte_be16_t hdr_checksum
Definition: rte_ip.h:61
static uint16_t rte_ipv4_cksum(const struct rte_ipv4_hdr *ipv4_hdr)
Definition: rte_ip.h:301
uint8_t version
Definition: rte_ip.h:48
static uint16_t rte_ipv6_udptcp_cksum(const struct rte_ipv6_hdr *ipv6_hdr, const void *l4_hdr)
Definition: rte_ip.h:529
#define RTE_IPV4_IHL_MULTIPLIER
Definition: rte_ip.h:81