CNDP 22.08.0
Loading...
Searching...
No Matches
cne_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) 2019-2022 Intel Corporation.
5 * Copyright (c) 2014 6WIND S.A.
6 * All rights reserved.
7 */
8
9#ifndef _CNE_IP_H_
10#define _CNE_IP_H_
11
17
18#include <stdint.h>
19#include <sys/types.h>
20#include <netinet/in.h>
21#include <netinet/ip.h>
22
23#include <cne_byteorder.h>
24#include <pktmbuf.h>
25
26#ifdef __cplusplus
27extern "C" {
28#endif
29
45
47#define CNE_IPV4(a, b, c, d) \
48 ((uint32_t)(((a)&0xff) << 24) | (((b)&0xff) << 16) | (((c)&0xff) << 8) | ((d)&0xff))
49
51#define CNE_IPV4_MAX_PKT_LEN 65535
52
54#define CNE_IPV4_HDR_IHL_MASK (0x0f)
59#define CNE_IPV4_IHL_MULTIPLIER (4)
60
61/* Type of Service fields */
62#define CNE_IPV4_HDR_DSCP_MASK (0xfc)
63#define CNE_IPV4_HDR_ECN_MASK (0x03)
64#define CNE_IPV4_HDR_ECN_CE CNE_IPV4_HDR_ECN_MASK
65
66/* Fragment Offset * Flags. */
67#define CNE_IPV4_HDR_DF_SHIFT 14
68#define CNE_IPV4_HDR_MF_SHIFT 13
69#define CNE_IPV4_HDR_FO_SHIFT 3
70
71#define CNE_IPV4_HDR_DF_FLAG (1 << CNE_IPV4_HDR_DF_SHIFT)
72#define CNE_IPV4_HDR_MF_FLAG (1 << CNE_IPV4_HDR_MF_SHIFT)
73
74#define CNE_IPV4_HDR_OFFSET_MASK ((1 << CNE_IPV4_HDR_MF_SHIFT) - 1)
75
76#define CNE_IPV4_HDR_OFFSET_UNITS 8
77
78/*
79 * IPv4 address types
80 */
81#define CNE_IPV4_ANY ((uint32_t)0x00000000)
82#define CNE_IPV4_LOOPBACK ((uint32_t)0x7f000001)
83#define CNE_IPV4_BROADCAST ((uint32_t)0xe0000000)
84#define CNE_IPV4_ALLHOSTS_GROUP ((uint32_t)0xe0000001)
85#define CNE_IPV4_ALLRTRS_GROUP ((uint32_t)0xe0000002)
86#define CNE_IPV4_MAX_LOCAL_GROUP ((uint32_t)0xe00000ff)
87
88/*
89 * IPv4 Multicast-related macros
90 */
91#define CNE_IPV4_MIN_MCAST CNE_IPV4(224, 0, 0, 0)
92#define CNE_IPV4_MAX_MCAST \
93 CNE_IPV4(239, 255, 255, 255)
94
95
96#define CNE_IS_IPV4_MCAST(x) ((x) >= CNE_IPV4_MIN_MCAST && (x) <= CNE_IPV4_MAX_MCAST)
98
99/* IPv4 default fields values */
100#define CNE_IPV4_MIN_IHL (0x5)
101#define CNE_IPV4_VHL_DEF ((IPVERSION << 4) | CNE_IPV4_MIN_IHL)
102
111static inline uint8_t
112cne_ipv4_hdr_len(const struct cne_ipv4_hdr *ipv4_hdr)
114 return (uint8_t)((ipv4_hdr->version_ihl & CNE_IPV4_HDR_IHL_MASK) * CNE_IPV4_IHL_MULTIPLIER);
115}
116
117
130static inline uint32_t
131__cne_raw_cksum(const void *buf, size_t len, uint32_t sum)
132{
133 /* workaround gcc strict-aliasing warning */
134 uintptr_t ptr = (uintptr_t)buf;
135 typedef uint16_t __attribute__((__may_alias__)) u16_p;
136 const u16_p *u16_buf = (const u16_p *)ptr;
137
138 while (len >= (sizeof(*u16_buf) * 4)) {
139 sum += u16_buf[0];
140 sum += u16_buf[1];
141 sum += u16_buf[2];
142 sum += u16_buf[3];
143 len -= sizeof(*u16_buf) * 4;
144 u16_buf += 4;
145 }
146 while (len >= sizeof(*u16_buf)) {
147 sum += *u16_buf;
148 len -= sizeof(*u16_buf);
149 u16_buf += 1;
150 }
151
152 /* if length is in odd bytes */
153 if (len == 1) {
154 uint16_t left = 0;
155 *(uint8_t *)&left = *(const uint8_t *)u16_buf;
156 sum += left;
157 }
158
159 return sum;
160}
161
171static inline uint16_t
172__cne_raw_cksum_reduce(uint32_t sum)
173{
174 sum = ((sum & 0xffff0000) >> 16) + (sum & 0xffff);
175 sum = ((sum & 0xffff0000) >> 16) + (sum & 0xffff);
176 return (uint16_t)sum;
177}
178
189static inline uint16_t
190cne_raw_cksum(const void *buf, size_t len)
192 uint32_t sum;
193
194 sum = __cne_raw_cksum(buf, len, 0);
195 return __cne_raw_cksum_reduce(sum);
196}
197
198
210static inline uint16_t
211cne_raw_cksum_mbuf(const pktmbuf_t *m, uint32_t off, uint32_t len)
213 return cne_raw_cksum(pktmbuf_mtod_offset(m, const char *, off), len);
214}
215
216
226static inline uint16_t
227cne_ipv4_cksum(const struct cne_ipv4_hdr *ipv4_hdr)
229 uint16_t cksum;
230 cksum = cne_raw_cksum(ipv4_hdr, cne_ipv4_hdr_len(ipv4_hdr));
231 return (uint16_t)~cksum;
232}
233
234
252static inline uint16_t
253cne_ipv4_phdr_cksum(const struct cne_ipv4_hdr *ipv4_hdr, uint64_t ol_flags)
255 struct ipv4_psd_header {
256 uint32_t src_addr; /* IP address of source host. */
257 uint32_t dst_addr; /* IP address of destination host. */
258 uint8_t zero; /* zero. */
259 uint8_t proto; /* L4 protocol type. */
260 uint16_t len; /* L4 length. */
261 } psd_hdr;
262 uint32_t l3_len;
263
264 psd_hdr.src_addr = ipv4_hdr->src_addr;
265 psd_hdr.dst_addr = ipv4_hdr->dst_addr;
266 psd_hdr.zero = 0;
267 psd_hdr.proto = ipv4_hdr->next_proto_id;
268
269 if (ol_flags & CNE_MBUF_F_TX_TCP_SEG)
270 psd_hdr.len = 0;
271 else {
272 l3_len = be16toh(ipv4_hdr->total_length);
273 psd_hdr.len = htobe16((uint16_t)(l3_len - cne_ipv4_hdr_len(ipv4_hdr)));
274 }
275
276 return cne_raw_cksum(&psd_hdr, sizeof(psd_hdr));
277}
278
279
292static inline uint16_t
293__ipv4_udptcp_cksum(const struct cne_ipv4_hdr *ipv4_hdr, const void *l4_hdr)
295 uint32_t cksum;
296 uint32_t l3_len, l4_len;
297 uint8_t ip_hdr_len;
298
299 ip_hdr_len = cne_ipv4_hdr_len(ipv4_hdr);
300 l3_len = be16toh(ipv4_hdr->total_length);
301 if (l3_len < ip_hdr_len)
302 return 0;
303
304 l4_len = l3_len - ip_hdr_len;
305
306 cksum = cne_raw_cksum(l4_hdr, l4_len);
307 cksum += cne_ipv4_phdr_cksum(ipv4_hdr, 0);
308
309 cksum = ((cksum & 0xffff0000) >> 16) + (cksum & 0xffff);
310
311 return (uint16_t)cksum;
312}
313
314
327static inline uint16_t
328cne_ipv4_udptcp_cksum(const struct cne_ipv4_hdr *ipv4_hdr, const void *l4_hdr)
330 uint16_t cksum = __ipv4_udptcp_cksum(ipv4_hdr, l4_hdr);
331
332 cksum = ~cksum;
333 /*
334 * Per RFC 768:If the computed checksum is zero for UDP,
335 * it is transmitted as all ones
336 * (the equivalent in one's complement arithmetic).
337 */
338 if (cksum == 0 && ipv4_hdr->next_proto_id == IPPROTO_UDP)
339 cksum = 0xffff;
340
341 return cksum;
342}
343
344
357static inline int
358cne_ipv4_udptcp_cksum_verify(const struct cne_ipv4_hdr *ipv4_hdr, const void *l4_hdr)
360 uint16_t cksum = __ipv4_udptcp_cksum(ipv4_hdr, l4_hdr);
361
362 if (cksum != 0xffff)
363 return -1;
364
365 return 0;
366}
367
368
371struct cne_ipv6_hdr {
380/* IPv6 vtc_flow: IPv / TC / flow_label */
381#define CNE_IPV6_HDR_FL_SHIFT 0
382#define CNE_IPV6_HDR_TC_SHIFT 20
383#define CNE_IPV6_HDR_FL_MASK ((1u << CNE_IPV6_HDR_TC_SHIFT) - 1)
384#define CNE_IPV6_HDR_TC_MASK (0xff << CNE_IPV6_HDR_TC_SHIFT)
385#define CNE_IPV6_HDR_DSCP_MASK (0xfc << CNE_IPV6_HDR_TC_SHIFT)
386#define CNE_IPV6_HDR_ECN_MASK (0x03 << CNE_IPV6_HDR_TC_SHIFT)
387#define CNE_IPV6_HDR_ECN_CE CNE_IPV6_HDR_ECN_MASK
388
389#define CNE_IPV6_MIN_MTU 1280
407static inline uint16_t
408cne_ipv6_phdr_cksum(const struct cne_ipv6_hdr *ipv6_hdr, uint64_t ol_flags __cne_unused)
410 uint32_t sum;
411 struct {
412 cne_be32_t len; /* L4 length. */
413 cne_be32_t proto; /* L4 protocol - top 3 bytes must be zero */
414 } psd_hdr;
415
416 psd_hdr.proto = (uint32_t)(ipv6_hdr->proto << 24);
417 psd_hdr.len = ipv6_hdr->payload_len;
418
419 sum = __cne_raw_cksum(ipv6_hdr->src_addr,
420 sizeof(ipv6_hdr->src_addr) + sizeof(ipv6_hdr->dst_addr), 0);
421 sum = __cne_raw_cksum(&psd_hdr, sizeof(psd_hdr), sum);
422 return __cne_raw_cksum_reduce(sum);
423}
424
425
438static inline uint16_t
439cne_ipv6_udptcp_cksum(const struct cne_ipv6_hdr *ipv6_hdr, const void *l4_hdr)
441 uint32_t cksum;
442 uint32_t l4_len;
443
444 l4_len = be16toh(ipv6_hdr->payload_len);
445
446 cksum = cne_raw_cksum(l4_hdr, l4_len);
447 cksum += cne_ipv6_phdr_cksum(ipv6_hdr, 0);
448
449 cksum = ((cksum & 0xffff0000) >> 16) + (cksum & 0xffff);
450 cksum = (~cksum) & 0xffff;
451 if (cksum == 0)
452 cksum = 0xffff;
453
454 return (uint16_t)cksum;
455}
456
457
458#define CNE_IPV6_EHDR_MF_SHIFT 0
459#define CNE_IPV6_EHDR_MF_MASK 1
460#define CNE_IPV6_EHDR_FO_SHIFT 3
461#define CNE_IPV6_EHDR_FO_MASK (~((1 << CNE_IPV6_EHDR_FO_SHIFT) - 1))
462#define CNE_IPV6_EHDR_FO_ALIGN (1 << CNE_IPV6_EHDR_FO_SHIFT)
463
464#define CNE_IPV6_FRAG_USED_MASK (CNE_IPV6_EHDR_MF_MASK | CNE_IPV6_EHDR_FO_MASK)
465
466#define CNE_IPV6_GET_MF(x) ((x)&CNE_IPV6_EHDR_MF_MASK)
467#define CNE_IPV6_GET_FO(x) ((x) >> CNE_IPV6_EHDR_FO_SHIFT)
468
469#define CNE_IPV6_SET_FRAG_DATA(fo, mf) (((fo)&CNE_IPV6_EHDR_FO_MASK) | ((mf)&CNE_IPV6_EHDR_MF_MASK))
470
471struct cne_ipv6_fragment_ext {
472 uint8_t next_header;
473 uint8_t reserved;
474 cne_be16_t frag_data;
475 cne_be32_t id;
477
478/* IPv6 fragment extension header size */
479#define CNE_IPV6_FRAG_HDR_SIZE sizeof(struct cne_ipv6_fragment_ext)
480
497
498static inline int
499cne_ipv6_get_next_ext(const uint8_t *p, int proto, size_t *ext_len)
501 int next_proto;
502
503 switch (proto) {
504 case IPPROTO_AH:
505 next_proto = *p++;
506 *ext_len = (*p + 2) * sizeof(uint32_t);
507 break;
508
509 case IPPROTO_HOPOPTS:
510 case IPPROTO_ROUTING:
511 case IPPROTO_DSTOPTS:
512 next_proto = *p++;
513 *ext_len = (*p + 1) * sizeof(uint64_t);
514 break;
515
516 case IPPROTO_FRAGMENT:
517 next_proto = *p;
518 *ext_len = CNE_IPV6_FRAG_HDR_SIZE;
519 break;
520
521 default:
522 return -EINVAL;
523 }
524
525 return next_proto;
526}
527
528#ifdef __cplusplus
529}
530#endif
531
532#endif /* _CNE_IP_H_ */
uint16_t cne_be16_t
uint32_t cne_be32_t
#define __cne_unused
Definition cne_common.h:144
#define __cne_packed
Definition cne_common.h:129
static int cne_ipv6_get_next_ext(const uint8_t *p, int proto, size_t *ext_len)
Definition cne_ip.h:500
static uint16_t cne_ipv4_udptcp_cksum(const struct cne_ipv4_hdr *ipv4_hdr, const void *l4_hdr)
Definition cne_ip.h:329
#define CNE_IPV4_IHL_MULTIPLIER
Definition cne_ip.h:59
static int cne_ipv4_udptcp_cksum_verify(const struct cne_ipv4_hdr *ipv4_hdr, const void *l4_hdr)
Definition cne_ip.h:359
static uint16_t cne_ipv4_phdr_cksum(const struct cne_ipv4_hdr *ipv4_hdr, uint64_t ol_flags)
Definition cne_ip.h:254
static uint16_t cne_ipv4_cksum(const struct cne_ipv4_hdr *ipv4_hdr)
Definition cne_ip.h:228
static uint16_t cne_ipv6_phdr_cksum(const struct cne_ipv6_hdr *ipv6_hdr, uint64_t ol_flags)
Definition cne_ip.h:409
static uint16_t __ipv4_udptcp_cksum(const struct cne_ipv4_hdr *ipv4_hdr, const void *l4_hdr)
Definition cne_ip.h:294
static uint16_t cne_raw_cksum(const void *buf, size_t len)
Definition cne_ip.h:191
#define CNE_IPV4_HDR_IHL_MASK
Definition cne_ip.h:54
static uint16_t cne_ipv6_udptcp_cksum(const struct cne_ipv6_hdr *ipv6_hdr, const void *l4_hdr)
Definition cne_ip.h:440
static uint8_t cne_ipv4_hdr_len(const struct cne_ipv4_hdr *ipv4_hdr)
Definition cne_ip.h:113
static uint16_t cne_raw_cksum_mbuf(const pktmbuf_t *m, uint32_t off, uint32_t len)
Definition cne_ip.h:212
#define pktmbuf_mtod_offset(m, t, o)
Definition pktmbuf.h:402
#define CNE_MBUF_F_TX_TCP_SEG
uint8_t time_to_live
Definition cne_ip.h:39
uint8_t version_ihl
Definition cne_ip.h:34
uint8_t next_proto_id
Definition cne_ip.h:40
cne_be16_t total_length
Definition cne_ip.h:36
cne_be16_t packet_id
Definition cne_ip.h:37
cne_be32_t src_addr
Definition cne_ip.h:42
cne_be32_t dst_addr
Definition cne_ip.h:43
cne_be16_t hdr_checksum
Definition cne_ip.h:41
uint8_t type_of_service
Definition cne_ip.h:35
cne_be16_t fragment_offset
Definition cne_ip.h:38
uint8_t hop_limits
Definition cne_ip.h:376
cne_be32_t vtc_flow
Definition cne_ip.h:373
uint8_t dst_addr[16]
Definition cne_ip.h:378
uint8_t src_addr[16]
Definition cne_ip.h:377
uint8_t proto
Definition cne_ip.h:375
cne_be16_t payload_len
Definition cne_ip.h:374