DPDK  18.05.1
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 <netinet/in.h>
20 
21 #include <rte_byteorder.h>
22 #include <rte_mbuf.h>
23 
24 #ifdef __cplusplus
25 extern "C" {
26 #endif
27 
31 struct ipv4_hdr {
32  uint8_t version_ihl;
33  uint8_t type_of_service;
34  uint16_t total_length;
35  uint16_t packet_id;
36  uint16_t fragment_offset;
37  uint8_t time_to_live;
38  uint8_t next_proto_id;
39  uint16_t hdr_checksum;
40  uint32_t src_addr;
41  uint32_t dst_addr;
42 } __attribute__((__packed__));
43 
45 #define IPv4(a,b,c,d) ((uint32_t)(((a) & 0xff) << 24) | \
46  (((b) & 0xff) << 16) | \
47  (((c) & 0xff) << 8) | \
48  ((d) & 0xff))
49 
51 #define IPV4_MAX_PKT_LEN 65535
52 
54 #define IPV4_HDR_IHL_MASK (0x0f)
55 
59 #define IPV4_IHL_MULTIPLIER (4)
60 
61 /* Fragment Offset * Flags. */
62 #define IPV4_HDR_DF_SHIFT 14
63 #define IPV4_HDR_MF_SHIFT 13
64 #define IPV4_HDR_FO_SHIFT 3
65 
66 #define IPV4_HDR_DF_FLAG (1 << IPV4_HDR_DF_SHIFT)
67 #define IPV4_HDR_MF_FLAG (1 << IPV4_HDR_MF_SHIFT)
68 
69 #define IPV4_HDR_OFFSET_MASK ((1 << IPV4_HDR_MF_SHIFT) - 1)
70 
71 #define IPV4_HDR_OFFSET_UNITS 8
72 
73 /*
74  * IPv4 address types
75  */
76 #define IPV4_ANY ((uint32_t)0x00000000)
77 #define IPV4_LOOPBACK ((uint32_t)0x7f000001)
78 #define IPV4_BROADCAST ((uint32_t)0xe0000000)
79 #define IPV4_ALLHOSTS_GROUP ((uint32_t)0xe0000001)
80 #define IPV4_ALLRTRS_GROUP ((uint32_t)0xe0000002)
81 #define IPV4_MAX_LOCAL_GROUP ((uint32_t)0xe00000ff)
83 /*
84  * IPv4 Multicast-related macros
85  */
86 #define IPV4_MIN_MCAST IPv4(224, 0, 0, 0)
87 #define IPV4_MAX_MCAST IPv4(239, 255, 255, 255)
89 #define IS_IPV4_MCAST(x) \
90  ((x) >= IPV4_MIN_MCAST && (x) <= IPV4_MAX_MCAST)
105 static inline uint32_t
106 __rte_raw_cksum(const void *buf, size_t len, uint32_t sum)
107 {
108  /* workaround gcc strict-aliasing warning */
109  uintptr_t ptr = (uintptr_t)buf;
110  typedef uint16_t __attribute__((__may_alias__)) u16_p;
111  const u16_p *u16_buf = (const u16_p *)ptr;
112 
113  while (len >= (sizeof(*u16_buf) * 4)) {
114  sum += u16_buf[0];
115  sum += u16_buf[1];
116  sum += u16_buf[2];
117  sum += u16_buf[3];
118  len -= sizeof(*u16_buf) * 4;
119  u16_buf += 4;
120  }
121  while (len >= sizeof(*u16_buf)) {
122  sum += *u16_buf;
123  len -= sizeof(*u16_buf);
124  u16_buf += 1;
125  }
126 
127  /* if length is in odd bytes */
128  if (len == 1)
129  sum += *((const uint8_t *)u16_buf);
130 
131  return sum;
132 }
133 
143 static inline uint16_t
144 __rte_raw_cksum_reduce(uint32_t sum)
145 {
146  sum = ((sum & 0xffff0000) >> 16) + (sum & 0xffff);
147  sum = ((sum & 0xffff0000) >> 16) + (sum & 0xffff);
148  return (uint16_t)sum;
149 }
150 
161 static inline uint16_t
162 rte_raw_cksum(const void *buf, size_t len)
163 {
164  uint32_t sum;
165 
166  sum = __rte_raw_cksum(buf, len, 0);
167  return __rte_raw_cksum_reduce(sum);
168 }
169 
184 static inline int
185 rte_raw_cksum_mbuf(const struct rte_mbuf *m, uint32_t off, uint32_t len,
186  uint16_t *cksum)
187 {
188  const struct rte_mbuf *seg;
189  const char *buf;
190  uint32_t sum, tmp;
191  uint32_t seglen, done;
192 
193  /* easy case: all data in the first segment */
194  if (off + len <= rte_pktmbuf_data_len(m)) {
196  const char *, off), len);
197  return 0;
198  }
199 
200  if (unlikely(off + len > rte_pktmbuf_pkt_len(m)))
201  return -1; /* invalid params, return a dummy value */
202 
203  /* else browse the segment to find offset */
204  seglen = 0;
205  for (seg = m; seg != NULL; seg = seg->next) {
206  seglen = rte_pktmbuf_data_len(seg);
207  if (off < seglen)
208  break;
209  off -= seglen;
210  }
211  seglen -= off;
212  buf = rte_pktmbuf_mtod_offset(seg, const char *, off);
213  if (seglen >= len) {
214  /* all in one segment */
215  *cksum = rte_raw_cksum(buf, len);
216  return 0;
217  }
218 
219  /* hard case: process checksum of several segments */
220  sum = 0;
221  done = 0;
222  for (;;) {
223  tmp = __rte_raw_cksum(buf, seglen, 0);
224  if (done & 1)
225  tmp = rte_bswap16((uint16_t)tmp);
226  sum += tmp;
227  done += seglen;
228  if (done == len)
229  break;
230  seg = seg->next;
231  buf = rte_pktmbuf_mtod(seg, const char *);
232  seglen = rte_pktmbuf_data_len(seg);
233  if (seglen > len - done)
234  seglen = len - done;
235  }
236 
237  *cksum = __rte_raw_cksum_reduce(sum);
238  return 0;
239 }
240 
251 static inline uint16_t
252 rte_ipv4_cksum(const struct ipv4_hdr *ipv4_hdr)
253 {
254  uint16_t cksum;
255  cksum = rte_raw_cksum(ipv4_hdr, sizeof(struct ipv4_hdr));
256  return (cksum == 0xffff) ? cksum : (uint16_t)~cksum;
257 }
258 
277 static inline uint16_t
278 rte_ipv4_phdr_cksum(const struct ipv4_hdr *ipv4_hdr, uint64_t ol_flags)
279 {
280  struct ipv4_psd_header {
281  uint32_t src_addr; /* IP address of source host. */
282  uint32_t dst_addr; /* IP address of destination host. */
283  uint8_t zero; /* zero. */
284  uint8_t proto; /* L4 protocol type. */
285  uint16_t len; /* L4 length. */
286  } psd_hdr;
287 
288  psd_hdr.src_addr = ipv4_hdr->src_addr;
289  psd_hdr.dst_addr = ipv4_hdr->dst_addr;
290  psd_hdr.zero = 0;
291  psd_hdr.proto = ipv4_hdr->next_proto_id;
292  if (ol_flags & PKT_TX_TCP_SEG) {
293  psd_hdr.len = 0;
294  } else {
295  psd_hdr.len = rte_cpu_to_be_16(
296  (uint16_t)(rte_be_to_cpu_16(ipv4_hdr->total_length)
297  - sizeof(struct ipv4_hdr)));
298  }
299  return rte_raw_cksum(&psd_hdr, sizeof(psd_hdr));
300 }
301 
315 static inline uint16_t
316 rte_ipv4_udptcp_cksum(const struct ipv4_hdr *ipv4_hdr, const void *l4_hdr)
317 {
318  uint32_t cksum;
319  uint32_t l4_len;
320 
321  l4_len = (uint32_t)(rte_be_to_cpu_16(ipv4_hdr->total_length) -
322  sizeof(struct ipv4_hdr));
323 
324  cksum = rte_raw_cksum(l4_hdr, l4_len);
325  cksum += rte_ipv4_phdr_cksum(ipv4_hdr, 0);
326 
327  cksum = ((cksum & 0xffff0000) >> 16) + (cksum & 0xffff);
328  cksum = (~cksum) & 0xffff;
329  if (cksum == 0)
330  cksum = 0xffff;
331 
332  return (uint16_t)cksum;
333 }
334 
338 struct ipv6_hdr {
339  uint32_t vtc_flow;
340  uint16_t payload_len;
341  uint8_t proto;
342  uint8_t hop_limits;
343  uint8_t src_addr[16];
344  uint8_t dst_addr[16];
345 } __attribute__((__packed__));
346 
347 /* IPv6 vtc_flow: IPv / TC / flow_label */
348 #define IPV6_HDR_FL_SHIFT 0
349 #define IPV6_HDR_TC_SHIFT 20
350 #define IPV6_HDR_FL_MASK ((1u << IPV6_HDR_TC_SHIFT) - 1)
351 #define IPV6_HDR_TC_MASK (0xf << IPV6_HDR_TC_SHIFT)
352 
369 static inline uint16_t
370 rte_ipv6_phdr_cksum(const struct ipv6_hdr *ipv6_hdr, uint64_t ol_flags)
371 {
372  uint32_t sum;
373  struct {
374  uint32_t len; /* L4 length. */
375  uint32_t proto; /* L4 protocol - top 3 bytes must be zero */
376  } psd_hdr;
377 
378  psd_hdr.proto = (uint32_t)(ipv6_hdr->proto << 24);
379  if (ol_flags & PKT_TX_TCP_SEG) {
380  psd_hdr.len = 0;
381  } else {
382  psd_hdr.len = ipv6_hdr->payload_len;
383  }
384 
385  sum = __rte_raw_cksum(ipv6_hdr->src_addr,
386  sizeof(ipv6_hdr->src_addr) + sizeof(ipv6_hdr->dst_addr),
387  0);
388  sum = __rte_raw_cksum(&psd_hdr, sizeof(psd_hdr), sum);
389  return __rte_raw_cksum_reduce(sum);
390 }
391 
405 static inline uint16_t
406 rte_ipv6_udptcp_cksum(const struct ipv6_hdr *ipv6_hdr, const void *l4_hdr)
407 {
408  uint32_t cksum;
409  uint32_t l4_len;
410 
411  l4_len = rte_be_to_cpu_16(ipv6_hdr->payload_len);
412 
413  cksum = rte_raw_cksum(l4_hdr, l4_len);
414  cksum += rte_ipv6_phdr_cksum(ipv6_hdr, 0);
415 
416  cksum = ((cksum & 0xffff0000) >> 16) + (cksum & 0xffff);
417  cksum = (~cksum) & 0xffff;
418  if (cksum == 0)
419  cksum = 0xffff;
420 
421  return (uint16_t)cksum;
422 }
423 
424 #ifdef __cplusplus
425 }
426 #endif
427 
428 #endif /* _RTE_IP_H_ */