DPDK  21.02.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 #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  /* 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 
175 static inline uint16_t
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 
193 static inline uint16_t
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 
216 static inline int
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)) {
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 
286 static inline uint16_t
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 
312 static inline uint16_t
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 
352 static inline uint16_t
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 
385 struct rte_ipv6_hdr {
388  uint8_t proto;
389  uint8_t hop_limits;
390  uint8_t src_addr[16];
391  uint8_t dst_addr[16];
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
421 static inline uint16_t
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 
457 static inline uint16_t
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 
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;
498  uint8_t reserved;
499  rte_be16_t frag_data;
500  rte_be32_t 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 
522 __rte_experimental
523 static inline int
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_ */
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:219
static uint16_t rte_ipv4_udptcp_cksum(const struct rte_ipv4_hdr *ipv4_hdr, const void *l4_hdr)
Definition: rte_ip.h:355
uint32_t rte_be32_t
uint8_t dst_addr[16]
Definition: rte_ip.h:393
rte_be16_t fragment_offset
Definition: rte_ip.h:38
uint8_t hop_limits
Definition: rte_ip.h:391
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 vtc_flow
Definition: rte_ip.h:388
rte_be32_t src_addr
Definition: rte_ip.h:42
uint8_t src_addr[16]
Definition: rte_ip.h:392
static __rte_experimental int rte_ipv6_get_next_ext(const uint8_t *p, int proto, size_t *ext_len)
Definition: rte_ip.h:526
#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:315
static uint16_t rte_ipv6_phdr_cksum(const struct rte_ipv6_hdr *ipv6_hdr, uint64_t ol_flags)
Definition: rte_ip.h:424
#define PKT_TX_TCP_SEG
#define rte_pktmbuf_mtod(m, t)
#define rte_pktmbuf_pkt_len(m)
Definition: rte_mbuf.h:1545
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:390
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:196
uint64_t ol_flags
#define rte_pktmbuf_data_len(m)
Definition: rte_mbuf.h:1555
uint8_t next_proto_id
Definition: rte_ip.h:40
rte_be16_t payload_len
Definition: rte_ip.h:389
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:289
static uint16_t rte_ipv6_udptcp_cksum(const struct rte_ipv6_hdr *ipv6_hdr, const void *l4_hdr)
Definition: rte_ip.h:460
#define RTE_IPV4_IHL_MULTIPLIER
Definition: rte_ip.h:61