DPDK  18.11.6
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 ipv4_hdr {
34  uint8_t version_ihl;
35  uint8_t type_of_service;
36  uint16_t total_length;
37  uint16_t packet_id;
38  uint16_t fragment_offset;
39  uint8_t time_to_live;
40  uint8_t next_proto_id;
41  uint16_t hdr_checksum;
42  uint32_t src_addr;
43  uint32_t dst_addr;
44 } __attribute__((__packed__));
45 
47 #define IPv4(a,b,c,d) ((uint32_t)(((a) & 0xff) << 24) | \
48  (((b) & 0xff) << 16) | \
49  (((c) & 0xff) << 8) | \
50  ((d) & 0xff))
51 
53 #define IPV4_MAX_PKT_LEN 65535
54 
56 #define IPV4_HDR_IHL_MASK (0x0f)
57 
61 #define IPV4_IHL_MULTIPLIER (4)
62 
63 /* Fragment Offset * Flags. */
64 #define IPV4_HDR_DF_SHIFT 14
65 #define IPV4_HDR_MF_SHIFT 13
66 #define IPV4_HDR_FO_SHIFT 3
67 
68 #define IPV4_HDR_DF_FLAG (1 << IPV4_HDR_DF_SHIFT)
69 #define IPV4_HDR_MF_FLAG (1 << IPV4_HDR_MF_SHIFT)
70 
71 #define IPV4_HDR_OFFSET_MASK ((1 << IPV4_HDR_MF_SHIFT) - 1)
72 
73 #define IPV4_HDR_OFFSET_UNITS 8
74 
75 /*
76  * IPv4 address types
77  */
78 #define IPV4_ANY ((uint32_t)0x00000000)
79 #define IPV4_LOOPBACK ((uint32_t)0x7f000001)
80 #define IPV4_BROADCAST ((uint32_t)0xe0000000)
81 #define IPV4_ALLHOSTS_GROUP ((uint32_t)0xe0000001)
82 #define IPV4_ALLRTRS_GROUP ((uint32_t)0xe0000002)
83 #define IPV4_MAX_LOCAL_GROUP ((uint32_t)0xe00000ff)
85 /*
86  * IPv4 Multicast-related macros
87  */
88 #define IPV4_MIN_MCAST IPv4(224, 0, 0, 0)
89 #define IPV4_MAX_MCAST IPv4(239, 255, 255, 255)
91 #define IS_IPV4_MCAST(x) \
92  ((x) >= IPV4_MIN_MCAST && (x) <= IPV4_MAX_MCAST)
94 /* IPv4 default fields values */
95 #define IPV4_MIN_IHL (0x5)
96 #define IPV4_VHL_DEF (IPVERSION | IPV4_MIN_IHL)
97 
111 static inline uint32_t
112 __rte_raw_cksum(const void *buf, size_t len, uint32_t sum)
113 {
114  /* workaround gcc strict-aliasing warning */
115  uintptr_t ptr = (uintptr_t)buf;
116  typedef uint16_t __attribute__((__may_alias__)) u16_p;
117  const u16_p *u16_buf = (const u16_p *)ptr;
118 
119  while (len >= (sizeof(*u16_buf) * 4)) {
120  sum += u16_buf[0];
121  sum += u16_buf[1];
122  sum += u16_buf[2];
123  sum += u16_buf[3];
124  len -= sizeof(*u16_buf) * 4;
125  u16_buf += 4;
126  }
127  while (len >= sizeof(*u16_buf)) {
128  sum += *u16_buf;
129  len -= sizeof(*u16_buf);
130  u16_buf += 1;
131  }
132 
133  /* if length is in odd bytes */
134  if (len == 1)
135  sum += *((const uint8_t *)u16_buf);
136 
137  return sum;
138 }
139 
149 static inline uint16_t
150 __rte_raw_cksum_reduce(uint32_t sum)
151 {
152  sum = ((sum & 0xffff0000) >> 16) + (sum & 0xffff);
153  sum = ((sum & 0xffff0000) >> 16) + (sum & 0xffff);
154  return (uint16_t)sum;
155 }
156 
167 static inline uint16_t
168 rte_raw_cksum(const void *buf, size_t len)
169 {
170  uint32_t sum;
171 
172  sum = __rte_raw_cksum(buf, len, 0);
173  return __rte_raw_cksum_reduce(sum);
174 }
175 
190 static inline int
191 rte_raw_cksum_mbuf(const struct rte_mbuf *m, uint32_t off, uint32_t len,
192  uint16_t *cksum)
193 {
194  const struct rte_mbuf *seg;
195  const char *buf;
196  uint32_t sum, tmp;
197  uint32_t seglen, done;
198 
199  /* easy case: all data in the first segment */
200  if (off + len <= rte_pktmbuf_data_len(m)) {
202  const char *, off), len);
203  return 0;
204  }
205 
206  if (unlikely(off + len > rte_pktmbuf_pkt_len(m)))
207  return -1; /* invalid params, return a dummy value */
208 
209  /* else browse the segment to find offset */
210  seglen = 0;
211  for (seg = m; seg != NULL; seg = seg->next) {
212  seglen = rte_pktmbuf_data_len(seg);
213  if (off < seglen)
214  break;
215  off -= seglen;
216  }
217  seglen -= off;
218  buf = rte_pktmbuf_mtod_offset(seg, const char *, off);
219  if (seglen >= len) {
220  /* all in one segment */
221  *cksum = rte_raw_cksum(buf, len);
222  return 0;
223  }
224 
225  /* hard case: process checksum of several segments */
226  sum = 0;
227  done = 0;
228  for (;;) {
229  tmp = __rte_raw_cksum(buf, seglen, 0);
230  if (done & 1)
231  tmp = rte_bswap16((uint16_t)tmp);
232  sum += tmp;
233  done += seglen;
234  if (done == len)
235  break;
236  seg = seg->next;
237  buf = rte_pktmbuf_mtod(seg, const char *);
238  seglen = rte_pktmbuf_data_len(seg);
239  if (seglen > len - done)
240  seglen = len - done;
241  }
242 
243  *cksum = __rte_raw_cksum_reduce(sum);
244  return 0;
245 }
246 
257 static inline uint16_t
258 rte_ipv4_cksum(const struct ipv4_hdr *ipv4_hdr)
259 {
260  uint16_t cksum;
261  cksum = rte_raw_cksum(ipv4_hdr, sizeof(struct ipv4_hdr));
262  return (cksum == 0xffff) ? cksum : (uint16_t)~cksum;
263 }
264 
283 static inline uint16_t
284 rte_ipv4_phdr_cksum(const struct ipv4_hdr *ipv4_hdr, uint64_t ol_flags)
285 {
286  struct ipv4_psd_header {
287  uint32_t src_addr; /* IP address of source host. */
288  uint32_t dst_addr; /* IP address of destination host. */
289  uint8_t zero; /* zero. */
290  uint8_t proto; /* L4 protocol type. */
291  uint16_t len; /* L4 length. */
292  } psd_hdr;
293 
294  psd_hdr.src_addr = ipv4_hdr->src_addr;
295  psd_hdr.dst_addr = ipv4_hdr->dst_addr;
296  psd_hdr.zero = 0;
297  psd_hdr.proto = ipv4_hdr->next_proto_id;
298  if (ol_flags & PKT_TX_TCP_SEG) {
299  psd_hdr.len = 0;
300  } else {
301  psd_hdr.len = rte_cpu_to_be_16(
302  (uint16_t)(rte_be_to_cpu_16(ipv4_hdr->total_length)
303  - sizeof(struct ipv4_hdr)));
304  }
305  return rte_raw_cksum(&psd_hdr, sizeof(psd_hdr));
306 }
307 
322 static inline uint16_t
323 rte_ipv4_udptcp_cksum(const struct ipv4_hdr *ipv4_hdr, const void *l4_hdr)
324 {
325  uint32_t cksum;
326  uint32_t l3_len, l4_len;
327 
328  l3_len = rte_be_to_cpu_16(ipv4_hdr->total_length);
329  if (l3_len < sizeof(struct ipv4_hdr))
330  return 0;
331 
332  l4_len = l3_len - sizeof(struct ipv4_hdr);
333 
334  cksum = rte_raw_cksum(l4_hdr, l4_len);
335  cksum += rte_ipv4_phdr_cksum(ipv4_hdr, 0);
336 
337  cksum = ((cksum & 0xffff0000) >> 16) + (cksum & 0xffff);
338  cksum = (~cksum) & 0xffff;
339  if (cksum == 0)
340  cksum = 0xffff;
341 
342  return (uint16_t)cksum;
343 }
344 
348 struct ipv6_hdr {
349  uint32_t vtc_flow;
350  uint16_t payload_len;
351  uint8_t proto;
352  uint8_t hop_limits;
353  uint8_t src_addr[16];
354  uint8_t dst_addr[16];
355 } __attribute__((__packed__));
356 
357 /* IPv6 vtc_flow: IPv / TC / flow_label */
358 #define IPV6_HDR_FL_SHIFT 0
359 #define IPV6_HDR_TC_SHIFT 20
360 #define IPV6_HDR_FL_MASK ((1u << IPV6_HDR_TC_SHIFT) - 1)
361 #define IPV6_HDR_TC_MASK (0xff << IPV6_HDR_TC_SHIFT)
362 
379 static inline uint16_t
380 rte_ipv6_phdr_cksum(const struct ipv6_hdr *ipv6_hdr, uint64_t ol_flags)
381 {
382  uint32_t sum;
383  struct {
384  uint32_t len; /* L4 length. */
385  uint32_t proto; /* L4 protocol - top 3 bytes must be zero */
386  } psd_hdr;
387 
388  psd_hdr.proto = (uint32_t)(ipv6_hdr->proto << 24);
389  if (ol_flags & PKT_TX_TCP_SEG) {
390  psd_hdr.len = 0;
391  } else {
392  psd_hdr.len = ipv6_hdr->payload_len;
393  }
394 
395  sum = __rte_raw_cksum(ipv6_hdr->src_addr,
396  sizeof(ipv6_hdr->src_addr) + sizeof(ipv6_hdr->dst_addr),
397  0);
398  sum = __rte_raw_cksum(&psd_hdr, sizeof(psd_hdr), sum);
399  return __rte_raw_cksum_reduce(sum);
400 }
401 
415 static inline uint16_t
416 rte_ipv6_udptcp_cksum(const struct ipv6_hdr *ipv6_hdr, const void *l4_hdr)
417 {
418  uint32_t cksum;
419  uint32_t l4_len;
420 
421  l4_len = rte_be_to_cpu_16(ipv6_hdr->payload_len);
422 
423  cksum = rte_raw_cksum(l4_hdr, l4_len);
424  cksum += rte_ipv6_phdr_cksum(ipv6_hdr, 0);
425 
426  cksum = ((cksum & 0xffff0000) >> 16) + (cksum & 0xffff);
427  cksum = (~cksum) & 0xffff;
428  if (cksum == 0)
429  cksum = 0xffff;
430 
431  return (uint16_t)cksum;
432 }
433 
434 #ifdef __cplusplus
435 }
436 #endif
437 
438 #endif /* _RTE_IP_H_ */
struct rte_mbuf * next
Definition: rte_mbuf.h:625
static int rte_raw_cksum_mbuf(const struct rte_mbuf *m, uint32_t off, uint32_t len, uint16_t *cksum)
Definition: rte_ip.h:192
static uint16_t rte_ipv4_phdr_cksum(const struct ipv4_hdr *ipv4_hdr, uint64_t ol_flags)
Definition: rte_ip.h:285
uint16_t hdr_checksum
Definition: rte_ip.h:41
uint32_t src_addr
Definition: rte_ip.h:42
uint32_t dst_addr
Definition: rte_ip.h:43
static uint16_t rte_bswap16(uint16_t _x)
static uint16_t rte_ipv4_udptcp_cksum(const struct ipv4_hdr *ipv4_hdr, const void *l4_hdr)
Definition: rte_ip.h:324
uint8_t proto
Definition: rte_ip.h:352
uint8_t type_of_service
Definition: rte_ip.h:35
uint8_t dst_addr[16]
Definition: rte_ip.h:355
uint8_t next_proto_id
Definition: rte_ip.h:40
static rte_be16_t rte_cpu_to_be_16(uint16_t x)
#define PKT_TX_TCP_SEG
Definition: rte_mbuf.h:299
#define unlikely(x)
uint16_t total_length
Definition: rte_ip.h:36
static uint16_t rte_ipv4_cksum(const struct ipv4_hdr *ipv4_hdr)
Definition: rte_ip.h:259
uint16_t payload_len
Definition: rte_ip.h:351
static uint16_t rte_ipv6_phdr_cksum(const struct ipv6_hdr *ipv6_hdr, uint64_t ol_flags)
Definition: rte_ip.h:381
uint8_t src_addr[16]
Definition: rte_ip.h:354
#define rte_pktmbuf_pkt_len(m)
Definition: rte_mbuf.h:1947
static uint16_t rte_raw_cksum(const void *buf, size_t len)
Definition: rte_ip.h:169
uint64_t ol_flags
Definition: rte_mbuf.h:519
#define rte_pktmbuf_data_len(m)
Definition: rte_mbuf.h:1957
#define rte_pktmbuf_mtod(m, t)
Definition: rte_mbuf.h:1909
uint8_t time_to_live
Definition: rte_ip.h:39
uint16_t packet_id
Definition: rte_ip.h:37
static uint16_t rte_be_to_cpu_16(rte_be16_t x)
uint16_t fragment_offset
Definition: rte_ip.h:38
uint8_t version_ihl
Definition: rte_ip.h:34
#define rte_pktmbuf_mtod_offset(m, t, o)
Definition: rte_mbuf.h:1894
static uint16_t rte_ipv6_udptcp_cksum(const struct ipv6_hdr *ipv6_hdr, const void *l4_hdr)
Definition: rte_ip.h:417