DPDK  24.11.0-rc1
rte_ip4.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_IP4_H_
10 #define _RTE_IP4_H_
11 
18 #include <stdint.h>
19 
20 #ifdef RTE_EXEC_ENV_WINDOWS
21 #include <ws2tcpip.h>
22 #else
23 #include <sys/socket.h>
24 #include <sys/types.h>
25 #include <netinet/in.h>
26 #include <arpa/inet.h>
27 #include <netinet/ip.h>
28 #include <netinet/ip6.h>
29 #endif
30 
31 #include <rte_byteorder.h>
32 #include <rte_cksum.h>
33 #include <rte_mbuf.h>
34 
35 #ifdef __cplusplus
36 extern "C" {
37 #endif
38 
42 struct rte_ipv4_hdr {
43  __extension__
44  union {
45  uint8_t version_ihl;
46  struct {
47 #if RTE_BYTE_ORDER == RTE_LITTLE_ENDIAN
48  uint8_t ihl:4;
49  uint8_t version:4;
50 #elif RTE_BYTE_ORDER == RTE_BIG_ENDIAN
51  uint8_t version:4;
52  uint8_t ihl:4;
53 #endif
54  };
55  };
56  uint8_t type_of_service;
60  uint8_t time_to_live;
61  uint8_t next_proto_id;
65 } __rte_packed;
66 
68 #define RTE_IPV4(a, b, c, d) ((uint32_t)(((a) & 0xff) << 24) | \
69  (((b) & 0xff) << 16) | \
70  (((c) & 0xff) << 8) | \
71  ((d) & 0xff))
72 
74 #define RTE_IPV4_MAX_PKT_LEN 65535
75 
77 #define RTE_IPV4_HDR_IHL_MASK (0x0f)
78 
82 #define RTE_IPV4_IHL_MULTIPLIER (4)
83 
84 /* Type of Service fields */
85 #define RTE_IPV4_HDR_DSCP_MASK (0xfc)
86 #define RTE_IPV4_HDR_ECN_MASK (0x03)
87 #define RTE_IPV4_HDR_ECN_CE RTE_IPV4_HDR_ECN_MASK
88 
89 /* Fragment Offset * Flags. */
90 #define RTE_IPV4_HDR_DF_SHIFT 14
91 #define RTE_IPV4_HDR_MF_SHIFT 13
92 #define RTE_IPV4_HDR_FO_SHIFT 3
93 
94 #define RTE_IPV4_HDR_DF_FLAG (1 << RTE_IPV4_HDR_DF_SHIFT)
95 #define RTE_IPV4_HDR_MF_FLAG (1 << RTE_IPV4_HDR_MF_SHIFT)
96 
97 #define RTE_IPV4_HDR_OFFSET_MASK ((1 << RTE_IPV4_HDR_MF_SHIFT) - 1)
98 
99 #define RTE_IPV4_HDR_OFFSET_UNITS 8
100 
101 /* IPv4 options */
102 #define RTE_IPV4_HDR_OPT_EOL 0
103 #define RTE_IPV4_HDR_OPT_NOP 1
104 #define RTE_IPV4_HDR_OPT_COPIED(v) ((v) & 0x80)
105 #define RTE_IPV4_HDR_OPT_MAX_LEN 40
106 
107 /*
108  * IPv4 address types
109  */
110 #define RTE_IPV4_ANY ((uint32_t)0x00000000)
111 #define RTE_IPV4_LOOPBACK ((uint32_t)0x7f000001)
112 #define RTE_IPV4_BROADCAST ((uint32_t)0xe0000000)
113 #define RTE_IPV4_ALLHOSTS_GROUP ((uint32_t)0xe0000001)
114 #define RTE_IPV4_ALLRTRS_GROUP ((uint32_t)0xe0000002)
115 #define RTE_IPV4_MAX_LOCAL_GROUP ((uint32_t)0xe00000ff)
117 /*
118  * IPv4 Multicast-related macros
119  */
120 #define RTE_IPV4_MIN_MCAST \
121  RTE_IPV4(224, 0, 0, 0)
122 #define RTE_IPV4_MAX_MCAST \
123  RTE_IPV4(239, 255, 255, 255)
125 #define RTE_IS_IPV4_MCAST(x) \
126  ((x) >= RTE_IPV4_MIN_MCAST && (x) <= RTE_IPV4_MAX_MCAST)
127 
129 /* IPv4 default fields values */
130 #define RTE_IPV4_MIN_IHL (0x5)
131 #define RTE_IPV4_VHL_DEF ((IPVERSION << 4) | RTE_IPV4_MIN_IHL)
132 
141 static inline uint8_t
142 rte_ipv4_hdr_len(const struct rte_ipv4_hdr *ipv4_hdr)
143 {
144  return (uint8_t)((ipv4_hdr->version_ihl & RTE_IPV4_HDR_IHL_MASK) *
146 }
147 
158 static inline uint16_t
159 rte_ipv4_cksum(const struct rte_ipv4_hdr *ipv4_hdr)
160 {
161  uint16_t cksum;
162  cksum = rte_raw_cksum(ipv4_hdr, rte_ipv4_hdr_len(ipv4_hdr));
163  return (uint16_t)~cksum;
164 }
165 
184 static inline uint16_t
185 rte_ipv4_phdr_cksum(const struct rte_ipv4_hdr *ipv4_hdr, uint64_t ol_flags)
186 {
187  struct ipv4_psd_header {
188  uint32_t src_addr; /* IP address of source host. */
189  uint32_t dst_addr; /* IP address of destination host. */
190  uint8_t zero; /* zero. */
191  uint8_t proto; /* L4 protocol type. */
192  uint16_t len; /* L4 length. */
193  } psd_hdr;
194 
195  uint32_t l3_len;
196 
197  psd_hdr.src_addr = ipv4_hdr->src_addr;
198  psd_hdr.dst_addr = ipv4_hdr->dst_addr;
199  psd_hdr.zero = 0;
200  psd_hdr.proto = ipv4_hdr->next_proto_id;
201  if (ol_flags & (RTE_MBUF_F_TX_TCP_SEG | RTE_MBUF_F_TX_UDP_SEG)) {
202  psd_hdr.len = 0;
203  } else {
204  l3_len = rte_be_to_cpu_16(ipv4_hdr->total_length);
205  psd_hdr.len = rte_cpu_to_be_16((uint16_t)(l3_len -
206  rte_ipv4_hdr_len(ipv4_hdr)));
207  }
208  return rte_raw_cksum(&psd_hdr, sizeof(psd_hdr));
209 }
210 
214 static inline uint16_t
215 __rte_ipv4_udptcp_cksum(const struct rte_ipv4_hdr *ipv4_hdr, const void *l4_hdr)
216 {
217  uint32_t cksum;
218  uint32_t l3_len, l4_len;
219  uint8_t ip_hdr_len;
220 
221  ip_hdr_len = rte_ipv4_hdr_len(ipv4_hdr);
222  l3_len = rte_be_to_cpu_16(ipv4_hdr->total_length);
223  if (l3_len < ip_hdr_len)
224  return 0;
225 
226  l4_len = l3_len - ip_hdr_len;
227 
228  cksum = rte_raw_cksum(l4_hdr, l4_len);
229  cksum += rte_ipv4_phdr_cksum(ipv4_hdr, 0);
230 
231  cksum = ((cksum & 0xffff0000) >> 16) + (cksum & 0xffff);
232 
233  return (uint16_t)cksum;
234 }
235 
248 static inline uint16_t
249 rte_ipv4_udptcp_cksum(const struct rte_ipv4_hdr *ipv4_hdr, const void *l4_hdr)
250 {
251  uint16_t cksum = __rte_ipv4_udptcp_cksum(ipv4_hdr, l4_hdr);
252 
253  cksum = ~cksum;
254 
255  /*
256  * Per RFC 768: If the computed checksum is zero for UDP,
257  * it is transmitted as all ones
258  * (the equivalent in one's complement arithmetic).
259  */
260  if (cksum == 0 && ipv4_hdr->next_proto_id == IPPROTO_UDP)
261  cksum = 0xffff;
262 
263  return cksum;
264 }
265 
269 static inline uint16_t
270 __rte_ipv4_udptcp_cksum_mbuf(const struct rte_mbuf *m,
271  const struct rte_ipv4_hdr *ipv4_hdr,
272  uint16_t l4_off)
273 {
274  uint16_t raw_cksum;
275  uint32_t cksum;
276  uint16_t len;
277 
278  if (unlikely(l4_off > m->pkt_len))
279  return 0; /* invalid params, return a dummy value */
280 
281  len = rte_be_to_cpu_16(ipv4_hdr->total_length) - (uint16_t)rte_ipv4_hdr_len(ipv4_hdr);
282 
283  if (rte_raw_cksum_mbuf(m, l4_off, len, &raw_cksum))
284  return 0;
285 
286  cksum = raw_cksum + rte_ipv4_phdr_cksum(ipv4_hdr, 0);
287 
288  cksum = ((cksum & 0xffff0000) >> 16) + (cksum & 0xffff);
289 
290  return (uint16_t)cksum;
291 }
292 
305 static inline uint16_t
306 rte_ipv4_udptcp_cksum_mbuf(const struct rte_mbuf *m,
307  const struct rte_ipv4_hdr *ipv4_hdr, uint16_t l4_off)
308 {
309  uint16_t cksum = __rte_ipv4_udptcp_cksum_mbuf(m, ipv4_hdr, l4_off);
310 
311  cksum = ~cksum;
312 
313  /*
314  * Per RFC 768: If the computed checksum is zero for UDP,
315  * it is transmitted as all ones
316  * (the equivalent in one's complement arithmetic).
317  */
318  if (cksum == 0 && ipv4_hdr->next_proto_id == IPPROTO_UDP)
319  cksum = 0xffff;
320 
321  return cksum;
322 }
323 
337 static inline int
338 rte_ipv4_udptcp_cksum_verify(const struct rte_ipv4_hdr *ipv4_hdr,
339  const void *l4_hdr)
340 {
341  uint16_t cksum = __rte_ipv4_udptcp_cksum(ipv4_hdr, l4_hdr);
342 
343  if (cksum != 0xffff)
344  return -1;
345 
346  return 0;
347 }
348 
364 static inline int
366  const struct rte_ipv4_hdr *ipv4_hdr,
367  uint16_t l4_off)
368 {
369  uint16_t cksum = __rte_ipv4_udptcp_cksum_mbuf(m, ipv4_hdr, l4_off);
370 
371  if (cksum != 0xffff)
372  return -1;
373 
374  return 0;
375 }
376 
377 #ifdef __cplusplus
378 }
379 #endif
380 
381 #endif /* _RTE_IP_H_ */
uint32_t rte_be32_t
#define RTE_IPV4_HDR_IHL_MASK
Definition: rte_ip4.h:77
rte_be16_t fragment_offset
Definition: rte_ip4.h:59
uint8_t version_ihl
Definition: rte_ip4.h:45
rte_be32_t dst_addr
Definition: rte_ip4.h:64
static int rte_ipv4_udptcp_cksum_verify(const struct rte_ipv4_hdr *ipv4_hdr, const void *l4_hdr)
Definition: rte_ip4.h:340
rte_be32_t src_addr
Definition: rte_ip4.h:63
#define RTE_MBUF_F_TX_UDP_SEG
#define __rte_packed
Definition: rte_common.h:108
static uint8_t rte_ipv4_hdr_len(const struct rte_ipv4_hdr *ipv4_hdr)
Definition: rte_ip4.h:144
static rte_be16_t rte_cpu_to_be_16(uint16_t x)
#define unlikely(x)
static uint16_t rte_ipv4_cksum(const struct rte_ipv4_hdr *ipv4_hdr)
Definition: rte_ip4.h:161
static uint16_t rte_raw_cksum(const void *buf, size_t len)
Definition: rte_cksum.h:94
uint8_t type_of_service
Definition: rte_ip4.h:56
uint8_t time_to_live
Definition: rte_ip4.h:60
rte_be16_t packet_id
Definition: rte_ip4.h:58
uint32_t pkt_len
#define RTE_MBUF_F_TX_TCP_SEG
uint8_t next_proto_id
Definition: rte_ip4.h:61
rte_be16_t total_length
Definition: rte_ip4.h:57
uint8_t ihl
Definition: rte_ip4.h:48
static uint16_t rte_ipv4_udptcp_cksum(const struct rte_ipv4_hdr *ipv4_hdr, const void *l4_hdr)
Definition: rte_ip4.h:251
uint16_t rte_be16_t
static int rte_ipv4_udptcp_cksum_mbuf_verify(const struct rte_mbuf *m, const struct rte_ipv4_hdr *ipv4_hdr, uint16_t l4_off)
Definition: rte_ip4.h:367
static uint16_t rte_ipv4_udptcp_cksum_mbuf(const struct rte_mbuf *m, const struct rte_ipv4_hdr *ipv4_hdr, uint16_t l4_off)
Definition: rte_ip4.h:308
static int rte_raw_cksum_mbuf(const struct rte_mbuf *m, uint32_t off, uint32_t len, uint16_t *cksum)
Definition: rte_cksum.h:117
static uint16_t rte_ipv4_phdr_cksum(const struct rte_ipv4_hdr *ipv4_hdr, uint64_t ol_flags)
Definition: rte_ip4.h:187
#define RTE_IPV4_IHL_MULTIPLIER
Definition: rte_ip4.h:82
static uint16_t rte_be_to_cpu_16(rte_be16_t x)
rte_be16_t hdr_checksum
Definition: rte_ip4.h:62
uint8_t version
Definition: rte_ip4.h:49