DPDK  19.11.10
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 } __attribute__((__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 
119 static inline uint32_t
120 __rte_raw_cksum(const void *buf, size_t len, uint32_t sum)
121 {
122  /* workaround gcc strict-aliasing warning */
123  uintptr_t ptr = (uintptr_t)buf;
124  typedef uint16_t __attribute__((__may_alias__)) u16_p;
125  const u16_p *u16_buf = (const u16_p *)ptr;
126 
127  while (len >= (sizeof(*u16_buf) * 4)) {
128  sum += u16_buf[0];
129  sum += u16_buf[1];
130  sum += u16_buf[2];
131  sum += u16_buf[3];
132  len -= sizeof(*u16_buf) * 4;
133  u16_buf += 4;
134  }
135  while (len >= sizeof(*u16_buf)) {
136  sum += *u16_buf;
137  len -= sizeof(*u16_buf);
138  u16_buf += 1;
139  }
140 
141  /* if length is in odd bytes */
142  if (len == 1) {
143  uint16_t left = 0;
144  *(uint8_t *)&left = *(const uint8_t *)u16_buf;
145  sum += left;
146  }
147 
148  return sum;
149 }
150 
160 static inline uint16_t
161 __rte_raw_cksum_reduce(uint32_t sum)
162 {
163  sum = ((sum & 0xffff0000) >> 16) + (sum & 0xffff);
164  sum = ((sum & 0xffff0000) >> 16) + (sum & 0xffff);
165  return (uint16_t)sum;
166 }
167 
178 static inline uint16_t
179 rte_raw_cksum(const void *buf, size_t len)
180 {
181  uint32_t sum;
182 
183  sum = __rte_raw_cksum(buf, len, 0);
184  return __rte_raw_cksum_reduce(sum);
185 }
186 
201 static inline int
202 rte_raw_cksum_mbuf(const struct rte_mbuf *m, uint32_t off, uint32_t len,
203  uint16_t *cksum)
204 {
205  const struct rte_mbuf *seg;
206  const char *buf;
207  uint32_t sum, tmp;
208  uint32_t seglen, done;
209 
210  /* easy case: all data in the first segment */
211  if (off + len <= rte_pktmbuf_data_len(m)) {
213  const char *, off), len);
214  return 0;
215  }
216 
217  if (unlikely(off + len > rte_pktmbuf_pkt_len(m)))
218  return -1; /* invalid params, return a dummy value */
219 
220  /* else browse the segment to find offset */
221  seglen = 0;
222  for (seg = m; seg != NULL; seg = seg->next) {
223  seglen = rte_pktmbuf_data_len(seg);
224  if (off < seglen)
225  break;
226  off -= seglen;
227  }
228  RTE_ASSERT(seg != NULL);
229  if (seg == NULL)
230  return -1;
231  seglen -= off;
232  buf = rte_pktmbuf_mtod_offset(seg, const char *, off);
233  if (seglen >= len) {
234  /* all in one segment */
235  *cksum = rte_raw_cksum(buf, len);
236  return 0;
237  }
238 
239  /* hard case: process checksum of several segments */
240  sum = 0;
241  done = 0;
242  for (;;) {
243  tmp = __rte_raw_cksum(buf, seglen, 0);
244  if (done & 1)
245  tmp = rte_bswap16((uint16_t)tmp);
246  sum += tmp;
247  done += seglen;
248  if (done == len)
249  break;
250  seg = seg->next;
251  buf = rte_pktmbuf_mtod(seg, const char *);
252  seglen = rte_pktmbuf_data_len(seg);
253  if (seglen > len - done)
254  seglen = len - done;
255  }
256 
257  *cksum = __rte_raw_cksum_reduce(sum);
258  return 0;
259 }
260 
271 static inline uint16_t
272 rte_ipv4_cksum(const struct rte_ipv4_hdr *ipv4_hdr)
273 {
274  uint16_t cksum;
275  cksum = rte_raw_cksum(ipv4_hdr, sizeof(struct rte_ipv4_hdr));
276  return (uint16_t)~cksum;
277 }
278 
297 static inline uint16_t
298 rte_ipv4_phdr_cksum(const struct rte_ipv4_hdr *ipv4_hdr, uint64_t ol_flags)
299 {
300  struct ipv4_psd_header {
301  uint32_t src_addr; /* IP address of source host. */
302  uint32_t dst_addr; /* IP address of destination host. */
303  uint8_t zero; /* zero. */
304  uint8_t proto; /* L4 protocol type. */
305  uint16_t len; /* L4 length. */
306  } psd_hdr;
307 
308  psd_hdr.src_addr = ipv4_hdr->src_addr;
309  psd_hdr.dst_addr = ipv4_hdr->dst_addr;
310  psd_hdr.zero = 0;
311  psd_hdr.proto = ipv4_hdr->next_proto_id;
312  if (ol_flags & PKT_TX_TCP_SEG) {
313  psd_hdr.len = 0;
314  } else {
315  psd_hdr.len = rte_cpu_to_be_16(
316  (uint16_t)(rte_be_to_cpu_16(ipv4_hdr->total_length)
317  - sizeof(struct rte_ipv4_hdr)));
318  }
319  return rte_raw_cksum(&psd_hdr, sizeof(psd_hdr));
320 }
321 
335 static inline uint16_t
336 rte_ipv4_udptcp_cksum(const struct rte_ipv4_hdr *ipv4_hdr, const void *l4_hdr)
337 {
338  uint32_t cksum;
339  uint32_t l3_len, l4_len;
340 
341  l3_len = rte_be_to_cpu_16(ipv4_hdr->total_length);
342  if (l3_len < sizeof(struct rte_ipv4_hdr))
343  return 0;
344 
345  l4_len = l3_len - sizeof(struct rte_ipv4_hdr);
346 
347  cksum = rte_raw_cksum(l4_hdr, l4_len);
348  cksum += rte_ipv4_phdr_cksum(ipv4_hdr, 0);
349 
350  cksum = ((cksum & 0xffff0000) >> 16) + (cksum & 0xffff);
351  cksum = (~cksum) & 0xffff;
352  /*
353  * Per RFC 768:If the computed checksum is zero for UDP,
354  * it is transmitted as all ones
355  * (the equivalent in one's complement arithmetic).
356  */
357  if (cksum == 0 && ipv4_hdr->next_proto_id == IPPROTO_UDP)
358  cksum = 0xffff;
359 
360  return (uint16_t)cksum;
361 }
362 
366 struct rte_ipv6_hdr {
367  rte_be32_t vtc_flow;
368  rte_be16_t payload_len;
369  uint8_t proto;
370  uint8_t hop_limits;
371  uint8_t src_addr[16];
372  uint8_t dst_addr[16];
373 } __attribute__((__packed__));
375 /* IPv6 vtc_flow: IPv / TC / flow_label */
376 #define RTE_IPV6_HDR_FL_SHIFT 0
377 #define RTE_IPV6_HDR_TC_SHIFT 20
378 #define RTE_IPV6_HDR_FL_MASK ((1u << RTE_IPV6_HDR_TC_SHIFT) - 1)
379 #define RTE_IPV6_HDR_TC_MASK (0xff << RTE_IPV6_HDR_TC_SHIFT)
380 #define RTE_IPV6_HDR_DSCP_MASK (0xfc << RTE_IPV6_HDR_TC_SHIFT)
381 #define RTE_IPV6_HDR_ECN_MASK (0x03 << RTE_IPV6_HDR_TC_SHIFT)
382 #define RTE_IPV6_HDR_ECN_CE RTE_IPV6_HDR_ECN_MASK
383 
400 static inline uint16_t
401 rte_ipv6_phdr_cksum(const struct rte_ipv6_hdr *ipv6_hdr, uint64_t ol_flags)
402 {
403  uint32_t sum;
404  struct {
405  rte_be32_t len; /* L4 length. */
406  rte_be32_t proto; /* L4 protocol - top 3 bytes must be zero */
407  } psd_hdr;
408 
409  psd_hdr.proto = (uint32_t)(ipv6_hdr->proto << 24);
410  if (ol_flags & PKT_TX_TCP_SEG) {
411  psd_hdr.len = 0;
412  } else {
413  psd_hdr.len = ipv6_hdr->payload_len;
414  }
415 
416  sum = __rte_raw_cksum(ipv6_hdr->src_addr,
417  sizeof(ipv6_hdr->src_addr) + sizeof(ipv6_hdr->dst_addr),
418  0);
419  sum = __rte_raw_cksum(&psd_hdr, sizeof(psd_hdr), sum);
420  return __rte_raw_cksum_reduce(sum);
421 }
422 
436 static inline uint16_t
437 rte_ipv6_udptcp_cksum(const struct rte_ipv6_hdr *ipv6_hdr, const void *l4_hdr)
438 {
439  uint32_t cksum;
440  uint32_t l4_len;
441 
442  l4_len = rte_be_to_cpu_16(ipv6_hdr->payload_len);
443 
444  cksum = rte_raw_cksum(l4_hdr, l4_len);
445  cksum += rte_ipv6_phdr_cksum(ipv6_hdr, 0);
446 
447  cksum = ((cksum & 0xffff0000) >> 16) + (cksum & 0xffff);
448  cksum = (~cksum) & 0xffff;
449  /*
450  * Per RFC 768: If the computed checksum is zero for UDP,
451  * it is transmitted as all ones
452  * (the equivalent in one's complement arithmetic).
453  */
454  if (cksum == 0 && ipv6_hdr->proto == IPPROTO_UDP)
455  cksum = 0xffff;
456 
457  return (uint16_t)cksum;
458 }
459 
460 /* IPv6 fragmentation header size */
461 #define RTE_IPV6_FRAG_HDR_SIZE 8
462 
479 __rte_experimental
480 static inline int
481 rte_ipv6_get_next_ext(const uint8_t *p, int proto, size_t *ext_len)
482 {
483  int next_proto;
484 
485  switch (proto) {
486  case IPPROTO_AH:
487  next_proto = *p++;
488  *ext_len = (*p + 2) * sizeof(uint32_t);
489  break;
490 
491  case IPPROTO_HOPOPTS:
492  case IPPROTO_ROUTING:
493  case IPPROTO_DSTOPTS:
494  next_proto = *p++;
495  *ext_len = (*p + 1) * sizeof(uint64_t);
496  break;
497 
498  case IPPROTO_FRAGMENT:
499  next_proto = *p;
500  *ext_len = RTE_IPV6_FRAG_HDR_SIZE;
501  break;
502 
503  default:
504  return -EINVAL;
505  }
506 
507  return next_proto;
508 }
509 
510 #ifdef __cplusplus
511 }
512 #endif
513 
514 #endif /* _RTE_IP_H_ */
struct rte_mbuf * next
static int rte_raw_cksum_mbuf(const struct rte_mbuf *m, uint32_t off, uint32_t len, uint16_t *cksum)
Definition: rte_ip.h:204
static uint16_t rte_ipv4_udptcp_cksum(const struct rte_ipv4_hdr *ipv4_hdr, const void *l4_hdr)
Definition: rte_ip.h:338
uint32_t rte_be32_t
uint8_t dst_addr[16]
Definition: rte_ip.h:374
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:373
static __rte_experimental int rte_ipv6_get_next_ext(const uint8_t *p, int proto, size_t *ext_len)
Definition: rte_ip.h:483
#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:300
static uint16_t rte_ipv6_phdr_cksum(const struct rte_ipv6_hdr *ipv6_hdr, uint64_t ol_flags)
Definition: rte_ip.h:403
#define PKT_TX_TCP_SEG
#define rte_pktmbuf_mtod(m, t)
#define rte_pktmbuf_pkt_len(m)
Definition: rte_mbuf.h:1390
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:371
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:181
uint64_t ol_flags
#define rte_pktmbuf_data_len(m)
Definition: rte_mbuf.h:1400
uint8_t next_proto_id
Definition: rte_ip.h:40
rte_be16_t payload_len
Definition: rte_ip.h:370
rte_be16_t total_length
Definition: rte_ip.h:36
uint16_t rte_be16_t
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:274
static uint16_t rte_ipv6_udptcp_cksum(const struct rte_ipv6_hdr *ipv6_hdr, const void *l4_hdr)
Definition: rte_ip.h:439