DPDK  20.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 #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 
33 struct rte_ipv4_hdr {
34  uint8_t version_ihl;
35  uint8_t type_of_service;
39  uint8_t time_to_live;
40  uint8_t next_proto_id;
44 } __rte_packed;
45 
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 
53 #define RTE_IPV4_MAX_PKT_LEN 65535
54 
56 #define RTE_IPV4_HDR_IHL_MASK (0x0f)
57 
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)
84 #define RTE_IPV4_LOOPBACK ((uint32_t)0x7f000001)
85 #define RTE_IPV4_BROADCAST ((uint32_t)0xe0000000)
86 #define RTE_IPV4_ALLHOSTS_GROUP ((uint32_t)0xe0000001)
87 #define RTE_IPV4_ALLRTRS_GROUP ((uint32_t)0xe0000002)
88 #define RTE_IPV4_MAX_LOCAL_GROUP ((uint32_t)0xe00000ff)
90 /*
91  * IPv4 Multicast-related macros
92  */
93 #define RTE_IPV4_MIN_MCAST \
94  RTE_IPV4(224, 0, 0, 0)
95 #define RTE_IPV4_MAX_MCAST \
96  RTE_IPV4(239, 255, 255, 255)
98 #define RTE_IS_IPV4_MCAST(x) \
99  ((x) >= RTE_IPV4_MIN_MCAST && (x) <= RTE_IPV4_MAX_MCAST)
100 
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 
114 static inline uint8_t
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) *
119 }
120 
134 static inline uint32_t
135 __rte_raw_cksum(const void *buf, size_t len, uint32_t sum)
136 {
137  const void *end;
138 
139  for (end = RTE_PTR_ADD(buf, RTE_ALIGN_FLOOR(len, sizeof(uint16_t)));
140  buf != end; buf = RTE_PTR_ADD(buf, sizeof(uint16_t))) {
141  uint16_t v;
142 
143  memcpy(&v, buf, sizeof(uint16_t));
144  sum += v;
145  }
146 
147  /* if length is odd, keeping it byte order independent */
148  if (unlikely(len % 2)) {
149  uint16_t left = 0;
150 
151  memcpy(&left, end, 1);
152  sum += left;
153  }
154 
155  return sum;
156 }
157 
167 static inline uint16_t
168 __rte_raw_cksum_reduce(uint32_t sum)
169 {
170  sum = ((sum & 0xffff0000) >> 16) + (sum & 0xffff);
171  sum = ((sum & 0xffff0000) >> 16) + (sum & 0xffff);
172  return (uint16_t)sum;
173 }
174 
185 static inline uint16_t
186 rte_raw_cksum(const void *buf, size_t len)
187 {
188  uint32_t sum;
189 
190  sum = __rte_raw_cksum(buf, len, 0);
191  return __rte_raw_cksum_reduce(sum);
192 }
193 
208 static inline int
209 rte_raw_cksum_mbuf(const struct rte_mbuf *m, uint32_t off, uint32_t len,
210  uint16_t *cksum)
211 {
212  const struct rte_mbuf *seg;
213  const char *buf;
214  uint32_t sum, tmp;
215  uint32_t seglen, done;
216 
217  /* easy case: all data in the first segment */
218  if (off + len <= rte_pktmbuf_data_len(m)) {
220  const char *, off), len);
221  return 0;
222  }
223 
224  if (unlikely(off + len > rte_pktmbuf_pkt_len(m)))
225  return -1; /* invalid params, return a dummy value */
226 
227  /* else browse the segment to find offset */
228  seglen = 0;
229  for (seg = m; seg != NULL; seg = seg->next) {
230  seglen = rte_pktmbuf_data_len(seg);
231  if (off < seglen)
232  break;
233  off -= seglen;
234  }
235  RTE_ASSERT(seg != NULL);
236  if (seg == NULL)
237  return -1;
238  seglen -= off;
239  buf = rte_pktmbuf_mtod_offset(seg, const char *, off);
240  if (seglen >= len) {
241  /* all in one segment */
242  *cksum = rte_raw_cksum(buf, len);
243  return 0;
244  }
245 
246  /* hard case: process checksum of several segments */
247  sum = 0;
248  done = 0;
249  for (;;) {
250  tmp = __rte_raw_cksum(buf, seglen, 0);
251  if (done & 1)
252  tmp = rte_bswap16((uint16_t)tmp);
253  sum += tmp;
254  done += seglen;
255  if (done == len)
256  break;
257  seg = seg->next;
258  buf = rte_pktmbuf_mtod(seg, const char *);
259  seglen = rte_pktmbuf_data_len(seg);
260  if (seglen > len - done)
261  seglen = len - done;
262  }
263 
264  *cksum = __rte_raw_cksum_reduce(sum);
265  return 0;
266 }
267 
278 static inline uint16_t
279 rte_ipv4_cksum(const struct rte_ipv4_hdr *ipv4_hdr)
280 {
281  uint16_t cksum;
282  cksum = rte_raw_cksum(ipv4_hdr, rte_ipv4_hdr_len(ipv4_hdr));
283  return (uint16_t)~cksum;
284 }
285 
304 static inline uint16_t
305 rte_ipv4_phdr_cksum(const struct rte_ipv4_hdr *ipv4_hdr, uint64_t ol_flags)
306 {
307  struct ipv4_psd_header {
308  uint32_t src_addr; /* IP address of source host. */
309  uint32_t dst_addr; /* IP address of destination host. */
310  uint8_t zero; /* zero. */
311  uint8_t proto; /* L4 protocol type. */
312  uint16_t len; /* L4 length. */
313  } psd_hdr;
314 
315  uint32_t l3_len;
316 
317  psd_hdr.src_addr = ipv4_hdr->src_addr;
318  psd_hdr.dst_addr = ipv4_hdr->dst_addr;
319  psd_hdr.zero = 0;
320  psd_hdr.proto = ipv4_hdr->next_proto_id;
321  if (ol_flags & PKT_TX_TCP_SEG) {
322  psd_hdr.len = 0;
323  } else {
324  l3_len = rte_be_to_cpu_16(ipv4_hdr->total_length);
325  psd_hdr.len = rte_cpu_to_be_16((uint16_t)(l3_len -
326  rte_ipv4_hdr_len(ipv4_hdr)));
327  }
328  return rte_raw_cksum(&psd_hdr, sizeof(psd_hdr));
329 }
330 
344 static inline uint16_t
345 rte_ipv4_udptcp_cksum(const struct rte_ipv4_hdr *ipv4_hdr, const void *l4_hdr)
346 {
347  uint32_t cksum;
348  uint32_t l3_len, l4_len;
349  uint8_t ip_hdr_len;
350 
351  ip_hdr_len = rte_ipv4_hdr_len(ipv4_hdr);
352  l3_len = rte_be_to_cpu_16(ipv4_hdr->total_length);
353  if (l3_len < ip_hdr_len)
354  return 0;
355 
356  l4_len = l3_len - ip_hdr_len;
357 
358  cksum = rte_raw_cksum(l4_hdr, l4_len);
359  cksum += rte_ipv4_phdr_cksum(ipv4_hdr, 0);
360 
361  cksum = ((cksum & 0xffff0000) >> 16) + (cksum & 0xffff);
362  cksum = (~cksum) & 0xffff;
363  /*
364  * Per RFC 768:If the computed checksum is zero for UDP,
365  * it is transmitted as all ones
366  * (the equivalent in one's complement arithmetic).
367  */
368  if (cksum == 0 && ipv4_hdr->next_proto_id == IPPROTO_UDP)
369  cksum = 0xffff;
370 
371  return (uint16_t)cksum;
372 }
373 
377 struct rte_ipv6_hdr {
378  rte_be32_t vtc_flow;
379  rte_be16_t payload_len;
380  uint8_t proto;
381  uint8_t hop_limits;
382  uint8_t src_addr[16];
383  uint8_t dst_addr[16];
386 /* IPv6 vtc_flow: IPv / TC / flow_label */
387 #define RTE_IPV6_HDR_FL_SHIFT 0
388 #define RTE_IPV6_HDR_TC_SHIFT 20
389 #define RTE_IPV6_HDR_FL_MASK ((1u << RTE_IPV6_HDR_TC_SHIFT) - 1)
390 #define RTE_IPV6_HDR_TC_MASK (0xff << RTE_IPV6_HDR_TC_SHIFT)
391 #define RTE_IPV6_HDR_DSCP_MASK (0xfc << RTE_IPV6_HDR_TC_SHIFT)
392 #define RTE_IPV6_HDR_ECN_MASK (0x03 << RTE_IPV6_HDR_TC_SHIFT)
393 #define RTE_IPV6_HDR_ECN_CE RTE_IPV6_HDR_ECN_MASK
394 
395 #define RTE_IPV6_MIN_MTU 1280
413 static inline uint16_t
414 rte_ipv6_phdr_cksum(const struct rte_ipv6_hdr *ipv6_hdr, uint64_t ol_flags)
415 {
416  uint32_t sum;
417  struct {
418  rte_be32_t len; /* L4 length. */
419  rte_be32_t proto; /* L4 protocol - top 3 bytes must be zero */
420  } psd_hdr;
421 
422  psd_hdr.proto = (uint32_t)(ipv6_hdr->proto << 24);
423  if (ol_flags & PKT_TX_TCP_SEG) {
424  psd_hdr.len = 0;
425  } else {
426  psd_hdr.len = ipv6_hdr->payload_len;
427  }
428 
429  sum = __rte_raw_cksum(ipv6_hdr->src_addr,
430  sizeof(ipv6_hdr->src_addr) + sizeof(ipv6_hdr->dst_addr),
431  0);
432  sum = __rte_raw_cksum(&psd_hdr, sizeof(psd_hdr), sum);
433  return __rte_raw_cksum_reduce(sum);
434 }
435 
449 static inline uint16_t
450 rte_ipv6_udptcp_cksum(const struct rte_ipv6_hdr *ipv6_hdr, const void *l4_hdr)
451 {
452  uint32_t cksum;
453  uint32_t l4_len;
454 
455  l4_len = rte_be_to_cpu_16(ipv6_hdr->payload_len);
456 
457  cksum = rte_raw_cksum(l4_hdr, l4_len);
458  cksum += rte_ipv6_phdr_cksum(ipv6_hdr, 0);
459 
460  cksum = ((cksum & 0xffff0000) >> 16) + (cksum & 0xffff);
461  cksum = (~cksum) & 0xffff;
462  /*
463  * Per RFC 768: If the computed checksum is zero for UDP,
464  * it is transmitted as all ones
465  * (the equivalent in one's complement arithmetic).
466  */
467  if (cksum == 0 && ipv6_hdr->proto == IPPROTO_UDP)
468  cksum = 0xffff;
469 
470  return (uint16_t)cksum;
471 }
472 
474 #define RTE_IPV6_EHDR_MF_SHIFT 0
475 #define RTE_IPV6_EHDR_MF_MASK 1
476 #define RTE_IPV6_EHDR_FO_SHIFT 3
477 #define RTE_IPV6_EHDR_FO_MASK (~((1 << RTE_IPV6_EHDR_FO_SHIFT) - 1))
478 #define RTE_IPV6_EHDR_FO_ALIGN (1 << RTE_IPV6_EHDR_FO_SHIFT)
479 
480 #define RTE_IPV6_FRAG_USED_MASK (RTE_IPV6_EHDR_MF_MASK | RTE_IPV6_EHDR_FO_MASK)
481 
482 #define RTE_IPV6_GET_MF(x) ((x) & RTE_IPV6_EHDR_MF_MASK)
483 #define RTE_IPV6_GET_FO(x) ((x) >> RTE_IPV6_EHDR_FO_SHIFT)
484 
485 #define RTE_IPV6_SET_FRAG_DATA(fo, mf) \
486  (((fo) & RTE_IPV6_EHDR_FO_MASK) | ((mf) & RTE_IPV6_EHDR_MF_MASK))
487 
488 struct rte_ipv6_fragment_ext {
489  uint8_t next_header;
490  uint8_t reserved;
491  rte_be16_t frag_data;
492  rte_be32_t id;
493 } __rte_packed;
494 
495 /* IPv6 fragment extension header size */
496 #define RTE_IPV6_FRAG_HDR_SIZE sizeof(struct rte_ipv6_fragment_ext)
497 
514 __rte_experimental
515 static inline int
516 rte_ipv6_get_next_ext(const uint8_t *p, int proto, size_t *ext_len)
517 {
518  int next_proto;
519 
520  switch (proto) {
521  case IPPROTO_AH:
522  next_proto = *p++;
523  *ext_len = (*p + 2) * sizeof(uint32_t);
524  break;
525 
526  case IPPROTO_HOPOPTS:
527  case IPPROTO_ROUTING:
528  case IPPROTO_DSTOPTS:
529  next_proto = *p++;
530  *ext_len = (*p + 1) * sizeof(uint64_t);
531  break;
532 
533  case IPPROTO_FRAGMENT:
534  next_proto = *p;
535  *ext_len = RTE_IPV6_FRAG_HDR_SIZE;
536  break;
537 
538  default:
539  return -EINVAL;
540  }
541 
542  return next_proto;
543 }
544 
545 #ifdef __cplusplus
546 }
547 #endif
548 
549 #endif /* _RTE_IP_H_ */
static uint8_t rte_ipv4_hdr_len(const struct rte_ipv4_hdr *ipv4_hdr)
Definition: rte_ip.h:117
struct rte_mbuf * next
#define __rte_packed
Definition: rte_common.h:84
static int rte_raw_cksum_mbuf(const struct rte_mbuf *m, uint32_t off, uint32_t len, uint16_t *cksum)
Definition: rte_ip.h:211
#define RTE_ALIGN_FLOOR(val, align)
Definition: rte_common.h:291
static uint16_t rte_ipv4_udptcp_cksum(const struct rte_ipv4_hdr *ipv4_hdr, const void *l4_hdr)
Definition: rte_ip.h:347
uint32_t rte_be32_t
uint8_t dst_addr[16]
Definition: rte_ip.h:385
rte_be16_t fragment_offset
Definition: rte_ip.h:38
uint8_t version_ihl
Definition: rte_ip.h:34
static uint16_t rte_bswap16(uint16_t _x)
rte_be32_t dst_addr
Definition: rte_ip.h:43
rte_be32_t src_addr
Definition: rte_ip.h:42
uint8_t src_addr[16]
Definition: rte_ip.h:384
static __rte_experimental int rte_ipv6_get_next_ext(const uint8_t *p, int proto, size_t *ext_len)
Definition: rte_ip.h:518
#define rte_pktmbuf_mtod_offset(m, t, o)
#define RTE_PTR_ADD(ptr, x)
Definition: rte_common.h:253
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:307
static uint16_t rte_ipv6_phdr_cksum(const struct rte_ipv6_hdr *ipv6_hdr, uint64_t ol_flags)
Definition: rte_ip.h:416
#define PKT_TX_TCP_SEG
#define rte_pktmbuf_mtod(m, t)
#define rte_pktmbuf_pkt_len(m)
Definition: rte_mbuf.h:1542
uint8_t type_of_service
Definition: rte_ip.h:35
uint8_t time_to_live
Definition: rte_ip.h:39
uint8_t proto
Definition: rte_ip.h:382
rte_be16_t packet_id
Definition: rte_ip.h:37
static uint16_t rte_raw_cksum(const void *buf, size_t len)
Definition: rte_ip.h:188
uint64_t ol_flags
#define rte_pktmbuf_data_len(m)
Definition: rte_mbuf.h:1552
uint8_t next_proto_id
Definition: rte_ip.h:40
rte_be16_t payload_len
Definition: rte_ip.h:381
rte_be16_t total_length
Definition: rte_ip.h:36
uint16_t rte_be16_t
#define RTE_IPV4_HDR_IHL_MASK
Definition: rte_ip.h:56
static uint16_t rte_be_to_cpu_16(rte_be16_t x)
rte_be16_t hdr_checksum
Definition: rte_ip.h:41
static uint16_t rte_ipv4_cksum(const struct rte_ipv4_hdr *ipv4_hdr)
Definition: rte_ip.h:281
static uint16_t rte_ipv6_udptcp_cksum(const struct rte_ipv6_hdr *ipv6_hdr, const void *l4_hdr)
Definition: rte_ip.h:452
#define RTE_IPV4_IHL_MULTIPLIER
Definition: rte_ip.h:61