DPDK  18.11.11
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  uint16_t left = 0;
136  *(uint8_t *)&left = *(const uint8_t *)u16_buf;
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 ipv4_hdr *ipv4_hdr)
265 {
266  uint16_t cksum;
267  cksum = rte_raw_cksum(ipv4_hdr, sizeof(struct ipv4_hdr));
268  return (uint16_t)~cksum;
269 }
270 
289 static inline uint16_t
290 rte_ipv4_phdr_cksum(const struct 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 ipv4_hdr)));
310  }
311  return rte_raw_cksum(&psd_hdr, sizeof(psd_hdr));
312 }
313 
327 static inline uint16_t
328 rte_ipv4_udptcp_cksum(const struct ipv4_hdr *ipv4_hdr, const void *l4_hdr)
329 {
330  uint32_t cksum;
331  uint32_t l3_len, l4_len;
332 
333  l3_len = rte_be_to_cpu_16(ipv4_hdr->total_length);
334  if (l3_len < sizeof(struct ipv4_hdr))
335  return 0;
336 
337  l4_len = l3_len - sizeof(struct ipv4_hdr);
338 
339  cksum = rte_raw_cksum(l4_hdr, l4_len);
340  cksum += rte_ipv4_phdr_cksum(ipv4_hdr, 0);
341 
342  cksum = ((cksum & 0xffff0000) >> 16) + (cksum & 0xffff);
343  cksum = (~cksum) & 0xffff;
344  /*
345  * Per RFC 768:If the computed checksum is zero for UDP,
346  * it is transmitted as all ones
347  * (the equivalent in one's complement arithmetic).
348  */
349  if (cksum == 0 && ipv4_hdr->next_proto_id == IPPROTO_UDP)
350  cksum = 0xffff;
351 
352  return (uint16_t)cksum;
353 }
354 
358 struct ipv6_hdr {
359  uint32_t vtc_flow;
360  uint16_t payload_len;
361  uint8_t proto;
362  uint8_t hop_limits;
363  uint8_t src_addr[16];
364  uint8_t dst_addr[16];
365 } __attribute__((__packed__));
366 
367 /* IPv6 vtc_flow: IPv / TC / flow_label */
368 #define IPV6_HDR_FL_SHIFT 0
369 #define IPV6_HDR_TC_SHIFT 20
370 #define IPV6_HDR_FL_MASK ((1u << IPV6_HDR_TC_SHIFT) - 1)
371 #define IPV6_HDR_TC_MASK (0xff << IPV6_HDR_TC_SHIFT)
372 
389 static inline uint16_t
390 rte_ipv6_phdr_cksum(const struct ipv6_hdr *ipv6_hdr, uint64_t ol_flags)
391 {
392  uint32_t sum;
393  struct {
394  uint32_t len; /* L4 length. */
395  uint32_t proto; /* L4 protocol - top 3 bytes must be zero */
396  } psd_hdr;
397 
398  psd_hdr.proto = (uint32_t)(ipv6_hdr->proto << 24);
399  if (ol_flags & PKT_TX_TCP_SEG) {
400  psd_hdr.len = 0;
401  } else {
402  psd_hdr.len = ipv6_hdr->payload_len;
403  }
404 
405  sum = __rte_raw_cksum(ipv6_hdr->src_addr,
406  sizeof(ipv6_hdr->src_addr) + sizeof(ipv6_hdr->dst_addr),
407  0);
408  sum = __rte_raw_cksum(&psd_hdr, sizeof(psd_hdr), sum);
409  return __rte_raw_cksum_reduce(sum);
410 }
411 
425 static inline uint16_t
426 rte_ipv6_udptcp_cksum(const struct ipv6_hdr *ipv6_hdr, const void *l4_hdr)
427 {
428  uint32_t cksum;
429  uint32_t l4_len;
430 
431  l4_len = rte_be_to_cpu_16(ipv6_hdr->payload_len);
432 
433  cksum = rte_raw_cksum(l4_hdr, l4_len);
434  cksum += rte_ipv6_phdr_cksum(ipv6_hdr, 0);
435 
436  cksum = ((cksum & 0xffff0000) >> 16) + (cksum & 0xffff);
437  cksum = (~cksum) & 0xffff;
438  /*
439  * Per RFC 768: If the computed checksum is zero for UDP,
440  * it is transmitted as all ones
441  * (the equivalent in one's complement arithmetic).
442  */
443  if (cksum == 0 && ipv6_hdr->proto == IPPROTO_UDP)
444  cksum = 0xffff;
445 
446  return (uint16_t)cksum;
447 }
448 
449 #ifdef __cplusplus
450 }
451 #endif
452 
453 #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:195
static uint16_t rte_ipv4_phdr_cksum(const struct ipv4_hdr *ipv4_hdr, uint64_t ol_flags)
Definition: rte_ip.h:291
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:329
uint8_t proto
Definition: rte_ip.h:362
uint8_t type_of_service
Definition: rte_ip.h:35
uint8_t dst_addr[16]
Definition: rte_ip.h:365
uint8_t hop_limits
Definition: rte_ip.h:363
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:265
uint16_t payload_len
Definition: rte_ip.h:361
static uint16_t rte_ipv6_phdr_cksum(const struct ipv6_hdr *ipv6_hdr, uint64_t ol_flags)
Definition: rte_ip.h:391
uint8_t src_addr[16]
Definition: rte_ip.h:364
#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:172
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
uint32_t vtc_flow
Definition: rte_ip.h:360
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:427