DPDK  19.11.14
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  const void *end;
123 
124  for (end = RTE_PTR_ADD(buf, RTE_ALIGN_FLOOR(len, sizeof(uint16_t)));
125  buf != end; buf = RTE_PTR_ADD(buf, sizeof(uint16_t))) {
126  uint16_t v;
127 
128  memcpy(&v, buf, sizeof(uint16_t));
129  sum += v;
130  }
131 
132  /* if length is odd, keeping it byte order independent */
133  if (unlikely(len % 2)) {
134  uint16_t left = 0;
135 
136  memcpy(&left, end, 1);
137  sum += left;
138  }
139 
140  return sum;
141 }
142 
152 static inline uint16_t
153 __rte_raw_cksum_reduce(uint32_t sum)
154 {
155  sum = ((sum & 0xffff0000) >> 16) + (sum & 0xffff);
156  sum = ((sum & 0xffff0000) >> 16) + (sum & 0xffff);
157  return (uint16_t)sum;
158 }
159 
170 static inline uint16_t
171 rte_raw_cksum(const void *buf, size_t len)
172 {
173  uint32_t sum;
174 
175  sum = __rte_raw_cksum(buf, len, 0);
176  return __rte_raw_cksum_reduce(sum);
177 }
178 
193 static inline int
194 rte_raw_cksum_mbuf(const struct rte_mbuf *m, uint32_t off, uint32_t len,
195  uint16_t *cksum)
196 {
197  const struct rte_mbuf *seg;
198  const char *buf;
199  uint32_t sum, tmp;
200  uint32_t seglen, done;
201 
202  /* easy case: all data in the first segment */
203  if (off + len <= rte_pktmbuf_data_len(m)) {
205  const char *, off), len);
206  return 0;
207  }
208 
209  if (unlikely(off + len > rte_pktmbuf_pkt_len(m)))
210  return -1; /* invalid params, return a dummy value */
211 
212  /* else browse the segment to find offset */
213  seglen = 0;
214  for (seg = m; seg != NULL; seg = seg->next) {
215  seglen = rte_pktmbuf_data_len(seg);
216  if (off < seglen)
217  break;
218  off -= seglen;
219  }
220  RTE_ASSERT(seg != NULL);
221  if (seg == NULL)
222  return -1;
223  seglen -= off;
224  buf = rte_pktmbuf_mtod_offset(seg, const char *, off);
225  if (seglen >= len) {
226  /* all in one segment */
227  *cksum = rte_raw_cksum(buf, len);
228  return 0;
229  }
230 
231  /* hard case: process checksum of several segments */
232  sum = 0;
233  done = 0;
234  for (;;) {
235  tmp = __rte_raw_cksum(buf, seglen, 0);
236  if (done & 1)
237  tmp = rte_bswap16((uint16_t)tmp);
238  sum += tmp;
239  done += seglen;
240  if (done == len)
241  break;
242  seg = seg->next;
243  buf = rte_pktmbuf_mtod(seg, const char *);
244  seglen = rte_pktmbuf_data_len(seg);
245  if (seglen > len - done)
246  seglen = len - done;
247  }
248 
249  *cksum = __rte_raw_cksum_reduce(sum);
250  return 0;
251 }
252 
263 static inline uint16_t
264 rte_ipv4_cksum(const struct rte_ipv4_hdr *ipv4_hdr)
265 {
266  uint16_t cksum;
267  cksum = rte_raw_cksum(ipv4_hdr, sizeof(struct rte_ipv4_hdr));
268  return (uint16_t)~cksum;
269 }
270 
289 static inline uint16_t
290 rte_ipv4_phdr_cksum(const struct rte_ipv4_hdr *ipv4_hdr, uint64_t ol_flags)
291 {
292  struct ipv4_psd_header {
293  uint32_t src_addr; /* IP address of source host. */
294  uint32_t dst_addr; /* IP address of destination host. */
295  uint8_t zero; /* zero. */
296  uint8_t proto; /* L4 protocol type. */
297  uint16_t len; /* L4 length. */
298  } psd_hdr;
299 
300  psd_hdr.src_addr = ipv4_hdr->src_addr;
301  psd_hdr.dst_addr = ipv4_hdr->dst_addr;
302  psd_hdr.zero = 0;
303  psd_hdr.proto = ipv4_hdr->next_proto_id;
304  if (ol_flags & PKT_TX_TCP_SEG) {
305  psd_hdr.len = 0;
306  } else {
307  psd_hdr.len = rte_cpu_to_be_16(
308  (uint16_t)(rte_be_to_cpu_16(ipv4_hdr->total_length)
309  - sizeof(struct rte_ipv4_hdr)));
310  }
311  return rte_raw_cksum(&psd_hdr, sizeof(psd_hdr));
312 }
313 
326 static inline uint16_t
327 rte_ipv4_udptcp_cksum(const struct rte_ipv4_hdr *ipv4_hdr, const void *l4_hdr)
328 {
329  uint32_t cksum;
330  uint32_t l3_len, l4_len;
331 
332  l3_len = rte_be_to_cpu_16(ipv4_hdr->total_length);
333  if (l3_len < sizeof(struct rte_ipv4_hdr))
334  return 0;
335 
336  l4_len = l3_len - sizeof(struct rte_ipv4_hdr);
337 
338  cksum = rte_raw_cksum(l4_hdr, l4_len);
339  cksum += rte_ipv4_phdr_cksum(ipv4_hdr, 0);
340 
341  cksum = ((cksum & 0xffff0000) >> 16) + (cksum & 0xffff);
342  cksum = (~cksum) & 0xffff;
343  /*
344  * Per RFC 768:If the computed checksum is zero for UDP,
345  * it is transmitted as all ones
346  * (the equivalent in one's complement arithmetic).
347  */
348  if (cksum == 0 && ipv4_hdr->next_proto_id == IPPROTO_UDP)
349  cksum = 0xffff;
350 
351  return (uint16_t)cksum;
352 }
353 
357 struct rte_ipv6_hdr {
358  rte_be32_t vtc_flow;
359  rte_be16_t payload_len;
360  uint8_t proto;
361  uint8_t hop_limits;
362  uint8_t src_addr[16];
363  uint8_t dst_addr[16];
364 } __attribute__((__packed__));
366 /* IPv6 vtc_flow: IPv / TC / flow_label */
367 #define RTE_IPV6_HDR_FL_SHIFT 0
368 #define RTE_IPV6_HDR_TC_SHIFT 20
369 #define RTE_IPV6_HDR_FL_MASK ((1u << RTE_IPV6_HDR_TC_SHIFT) - 1)
370 #define RTE_IPV6_HDR_TC_MASK (0xff << RTE_IPV6_HDR_TC_SHIFT)
371 #define RTE_IPV6_HDR_DSCP_MASK (0xfc << RTE_IPV6_HDR_TC_SHIFT)
372 #define RTE_IPV6_HDR_ECN_MASK (0x03 << RTE_IPV6_HDR_TC_SHIFT)
373 #define RTE_IPV6_HDR_ECN_CE RTE_IPV6_HDR_ECN_MASK
374 
391 static inline uint16_t
392 rte_ipv6_phdr_cksum(const struct rte_ipv6_hdr *ipv6_hdr, uint64_t ol_flags)
393 {
394  uint32_t sum;
395  struct {
396  rte_be32_t len; /* L4 length. */
397  rte_be32_t proto; /* L4 protocol - top 3 bytes must be zero */
398  } psd_hdr;
399 
400  psd_hdr.proto = (uint32_t)(ipv6_hdr->proto << 24);
401  if (ol_flags & PKT_TX_TCP_SEG) {
402  psd_hdr.len = 0;
403  } else {
404  psd_hdr.len = ipv6_hdr->payload_len;
405  }
406 
407  sum = __rte_raw_cksum(ipv6_hdr->src_addr,
408  sizeof(ipv6_hdr->src_addr) + sizeof(ipv6_hdr->dst_addr),
409  0);
410  sum = __rte_raw_cksum(&psd_hdr, sizeof(psd_hdr), sum);
411  return __rte_raw_cksum_reduce(sum);
412 }
413 
427 static inline uint16_t
428 rte_ipv6_udptcp_cksum(const struct rte_ipv6_hdr *ipv6_hdr, const void *l4_hdr)
429 {
430  uint32_t cksum;
431  uint32_t l4_len;
432 
433  l4_len = rte_be_to_cpu_16(ipv6_hdr->payload_len);
434 
435  cksum = rte_raw_cksum(l4_hdr, l4_len);
436  cksum += rte_ipv6_phdr_cksum(ipv6_hdr, 0);
437 
438  cksum = ((cksum & 0xffff0000) >> 16) + (cksum & 0xffff);
439  cksum = (~cksum) & 0xffff;
440  /*
441  * Per RFC 768: If the computed checksum is zero for UDP,
442  * it is transmitted as all ones
443  * (the equivalent in one's complement arithmetic).
444  */
445  if (cksum == 0 && ipv6_hdr->proto == IPPROTO_UDP)
446  cksum = 0xffff;
447 
448  return (uint16_t)cksum;
449 }
450 
451 /* IPv6 fragmentation header size */
452 #define RTE_IPV6_FRAG_HDR_SIZE 8
453 
470 __rte_experimental
471 static inline int
472 rte_ipv6_get_next_ext(const uint8_t *p, int proto, size_t *ext_len)
473 {
474  int next_proto;
475 
476  switch (proto) {
477  case IPPROTO_AH:
478  next_proto = *p++;
479  *ext_len = (*p + 2) * sizeof(uint32_t);
480  break;
481 
482  case IPPROTO_HOPOPTS:
483  case IPPROTO_ROUTING:
484  case IPPROTO_DSTOPTS:
485  next_proto = *p++;
486  *ext_len = (*p + 1) * sizeof(uint64_t);
487  break;
488 
489  case IPPROTO_FRAGMENT:
490  next_proto = *p;
491  *ext_len = RTE_IPV6_FRAG_HDR_SIZE;
492  break;
493 
494  default:
495  return -EINVAL;
496  }
497 
498  return next_proto;
499 }
500 
501 #ifdef __cplusplus
502 }
503 #endif
504 
505 #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:196
#define RTE_ALIGN_FLOOR(val, align)
Definition: rte_common.h:208
static uint16_t rte_ipv4_udptcp_cksum(const struct rte_ipv4_hdr *ipv4_hdr, const void *l4_hdr)
Definition: rte_ip.h:329
uint32_t rte_be32_t
uint8_t dst_addr[16]
Definition: rte_ip.h:365
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:364
static __rte_experimental int rte_ipv6_get_next_ext(const uint8_t *p, int proto, size_t *ext_len)
Definition: rte_ip.h:474
#define rte_pktmbuf_mtod_offset(m, t, o)
#define RTE_PTR_ADD(ptr, x)
Definition: rte_common.h:170
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:292
static uint16_t rte_ipv6_phdr_cksum(const struct rte_ipv6_hdr *ipv6_hdr, uint64_t ol_flags)
Definition: rte_ip.h:394
#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:362
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:173
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:361
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:266
static uint16_t rte_ipv6_udptcp_cksum(const struct rte_ipv6_hdr *ipv6_hdr, const void *l4_hdr)
Definition: rte_ip.h:430