DPDK  18.11.10
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  seglen -= off;
221  buf = rte_pktmbuf_mtod_offset(seg, const char *, off);
222  if (seglen >= len) {
223  /* all in one segment */
224  *cksum = rte_raw_cksum(buf, len);
225  return 0;
226  }
227 
228  /* hard case: process checksum of several segments */
229  sum = 0;
230  done = 0;
231  for (;;) {
232  tmp = __rte_raw_cksum(buf, seglen, 0);
233  if (done & 1)
234  tmp = rte_bswap16((uint16_t)tmp);
235  sum += tmp;
236  done += seglen;
237  if (done == len)
238  break;
239  seg = seg->next;
240  buf = rte_pktmbuf_mtod(seg, const char *);
241  seglen = rte_pktmbuf_data_len(seg);
242  if (seglen > len - done)
243  seglen = len - done;
244  }
245 
246  *cksum = __rte_raw_cksum_reduce(sum);
247  return 0;
248 }
249 
260 static inline uint16_t
261 rte_ipv4_cksum(const struct ipv4_hdr *ipv4_hdr)
262 {
263  uint16_t cksum;
264  cksum = rte_raw_cksum(ipv4_hdr, sizeof(struct ipv4_hdr));
265  return (uint16_t)~cksum;
266 }
267 
286 static inline uint16_t
287 rte_ipv4_phdr_cksum(const struct ipv4_hdr *ipv4_hdr, uint64_t ol_flags)
288 {
289  struct ipv4_psd_header {
290  uint32_t src_addr; /* IP address of source host. */
291  uint32_t dst_addr; /* IP address of destination host. */
292  uint8_t zero; /* zero. */
293  uint8_t proto; /* L4 protocol type. */
294  uint16_t len; /* L4 length. */
295  } psd_hdr;
296 
297  psd_hdr.src_addr = ipv4_hdr->src_addr;
298  psd_hdr.dst_addr = ipv4_hdr->dst_addr;
299  psd_hdr.zero = 0;
300  psd_hdr.proto = ipv4_hdr->next_proto_id;
301  if (ol_flags & PKT_TX_TCP_SEG) {
302  psd_hdr.len = 0;
303  } else {
304  psd_hdr.len = rte_cpu_to_be_16(
305  (uint16_t)(rte_be_to_cpu_16(ipv4_hdr->total_length)
306  - sizeof(struct ipv4_hdr)));
307  }
308  return rte_raw_cksum(&psd_hdr, sizeof(psd_hdr));
309 }
310 
324 static inline uint16_t
325 rte_ipv4_udptcp_cksum(const struct ipv4_hdr *ipv4_hdr, const void *l4_hdr)
326 {
327  uint32_t cksum;
328  uint32_t l3_len, l4_len;
329 
330  l3_len = rte_be_to_cpu_16(ipv4_hdr->total_length);
331  if (l3_len < sizeof(struct ipv4_hdr))
332  return 0;
333 
334  l4_len = l3_len - sizeof(struct ipv4_hdr);
335 
336  cksum = rte_raw_cksum(l4_hdr, l4_len);
337  cksum += rte_ipv4_phdr_cksum(ipv4_hdr, 0);
338 
339  cksum = ((cksum & 0xffff0000) >> 16) + (cksum & 0xffff);
340  cksum = (~cksum) & 0xffff;
341  /*
342  * Per RFC 768:If the computed checksum is zero for UDP,
343  * it is transmitted as all ones
344  * (the equivalent in one's complement arithmetic).
345  */
346  if (cksum == 0 && ipv4_hdr->next_proto_id == IPPROTO_UDP)
347  cksum = 0xffff;
348 
349  return (uint16_t)cksum;
350 }
351 
355 struct ipv6_hdr {
356  uint32_t vtc_flow;
357  uint16_t payload_len;
358  uint8_t proto;
359  uint8_t hop_limits;
360  uint8_t src_addr[16];
361  uint8_t dst_addr[16];
362 } __attribute__((__packed__));
363 
364 /* IPv6 vtc_flow: IPv / TC / flow_label */
365 #define IPV6_HDR_FL_SHIFT 0
366 #define IPV6_HDR_TC_SHIFT 20
367 #define IPV6_HDR_FL_MASK ((1u << IPV6_HDR_TC_SHIFT) - 1)
368 #define IPV6_HDR_TC_MASK (0xff << IPV6_HDR_TC_SHIFT)
369 
386 static inline uint16_t
387 rte_ipv6_phdr_cksum(const struct ipv6_hdr *ipv6_hdr, uint64_t ol_flags)
388 {
389  uint32_t sum;
390  struct {
391  uint32_t len; /* L4 length. */
392  uint32_t proto; /* L4 protocol - top 3 bytes must be zero */
393  } psd_hdr;
394 
395  psd_hdr.proto = (uint32_t)(ipv6_hdr->proto << 24);
396  if (ol_flags & PKT_TX_TCP_SEG) {
397  psd_hdr.len = 0;
398  } else {
399  psd_hdr.len = ipv6_hdr->payload_len;
400  }
401 
402  sum = __rte_raw_cksum(ipv6_hdr->src_addr,
403  sizeof(ipv6_hdr->src_addr) + sizeof(ipv6_hdr->dst_addr),
404  0);
405  sum = __rte_raw_cksum(&psd_hdr, sizeof(psd_hdr), sum);
406  return __rte_raw_cksum_reduce(sum);
407 }
408 
422 static inline uint16_t
423 rte_ipv6_udptcp_cksum(const struct ipv6_hdr *ipv6_hdr, const void *l4_hdr)
424 {
425  uint32_t cksum;
426  uint32_t l4_len;
427 
428  l4_len = rte_be_to_cpu_16(ipv6_hdr->payload_len);
429 
430  cksum = rte_raw_cksum(l4_hdr, l4_len);
431  cksum += rte_ipv6_phdr_cksum(ipv6_hdr, 0);
432 
433  cksum = ((cksum & 0xffff0000) >> 16) + (cksum & 0xffff);
434  cksum = (~cksum) & 0xffff;
435  /*
436  * Per RFC 768: If the computed checksum is zero for UDP,
437  * it is transmitted as all ones
438  * (the equivalent in one's complement arithmetic).
439  */
440  if (cksum == 0 && ipv6_hdr->proto == IPPROTO_UDP)
441  cksum = 0xffff;
442 
443  return (uint16_t)cksum;
444 }
445 
446 #ifdef __cplusplus
447 }
448 #endif
449 
450 #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:288
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:326
uint8_t proto
Definition: rte_ip.h:359
uint8_t type_of_service
Definition: rte_ip.h:35
uint8_t dst_addr[16]
Definition: rte_ip.h:362
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:262
uint16_t payload_len
Definition: rte_ip.h:358
static uint16_t rte_ipv6_phdr_cksum(const struct ipv6_hdr *ipv6_hdr, uint64_t ol_flags)
Definition: rte_ip.h:388
uint8_t src_addr[16]
Definition: rte_ip.h:361
#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
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:424