CNDP  22.08.0
cnet_ipv4.h
Go to the documentation of this file.
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright (c) 2016-2022 Intel Corporation
3  */
4 
5 #ifndef __CNET_IPv4_H
6 #define __CNET_IPv4_H
7 
13 #include <net/cne_ip.h> // for cne_ipv4_hdr
14 #include <cne_inet.h> // for _in_addr
15 #include <cnet_protosw.h> // for
16 #include <endian.h> // for htobe16
17 #include <netinet/in.h> // for IN_CLASSA, IN_CLASSB, IN_CLASSC, IN_CLASSD
18 #include <stdint.h> // for uint16_t, uint8_t, uint64_t, uint32_t, int...
19 #include <stdio.h> // for NULL
20 
21 #include "cne_common.h" // for __cne_packed, __cne_cache_aligned
22 #include "cnet_const.h" // for iofunc_t
23 
24 struct netif;
25 struct stk_s;
26 
27 #ifdef __cplusplus
28 extern "C" {
29 #endif
30 
31 #define IP_MAX_MTU ETHER_MAX_MTU
32 #define TTL_DEFAULT 64 /* Default TTL value */
33 #define TOS_DEFAULT 0 /* Default TOS value */
34 
35 #define DEFAULT_IPV4_HDR_SIZE 20
36 #define IPv4_VER_LEN_VALUE ((IPv4_VERSION << 4) | (sizeof(struct cne_ipv4_hdr) / 4))
37 
38 struct rt4_entry;
39 struct arp_entry;
40 struct cne_lpm;
41 struct ipfwd_info;
42 
43 struct ipv4_stats {
44  uint64_t ip_ver_error;
45  uint64_t ip_hl_error;
46  uint64_t vec_pool_empty;
47  uint64_t ip_invalid_src_addr;
48  uint64_t ip_checksum_error;
49  uint64_t route_lookup_failed;
50  uint64_t ip_forward_failed;
51  uint64_t ip_reassemble_failed;
52  uint64_t ip_proto_invalid;
53  uint64_t ip_mbuf_too_small;
54  uint64_t ip_option_invalid;
55  uint64_t ip_mforward_pkts;
56  uint64_t ip_mforward_failed;
57  uint64_t ip_mlookup_failed;
58  uint64_t ip_forwarding_disabled;
59 };
60 
61 struct ipv4_entry {
62  uint8_t ip_forwarding;
63  uint8_t do_multicast;
64  uint16_t reassem_ttl;
65  struct ipfwd_info *fwd_info;
66  struct ipv4_stats stats;
67  iofunc_t fastpath;
68  iofunc_t dhcp;
69  iofunc_t setup;
70 } __cne_cache_aligned;
71 
72 #define IPv4(a, b, c, d) \
73  ((uint32_t)(((a)&0xff) << 24) | (((b)&0xff) << 16) | (((c)&0xff) << 8) | ((d)&0xff))
74 
75 /* Internet protocol header structure */
76 /* Basic IPv4 packet header
77  *
78  * IPv4 Header Format
79  *
80  * 0 1 2 3
81  * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
82  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
83  * | Ver | hlen | TOS | Total length |
84  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
85  * | Ident |flags| fragment offset |
86  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
87  * | TTL | Protocol | Header Checksum |
88  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
89  * | Source Address (32 Bits) |
90  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
91  * | Destination Address (32 Bits) |
92  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
93  * | data |
94  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
95  */
96 
97 #define _ISFRAG(off) ((off) & (_OFF_MF | _OFF_MASK))
98 #define _OFF_MASK 0x1fff
99 #define _OFF_MF 0x2000
100 #define _OFF_DF 0x4000
101 
102 /* IP overlay header for the pseudo header */
103 struct ipv4_overlay {
104  uint32_t node[2];
105  uint8_t pad0;
106  uint8_t proto;
107  uint16_t len;
108  uint32_t src;
109  uint32_t dst;
110 } __cne_packed;
111 
112 /* Timestamp Option structure */
113 struct ipoptions {
114  uint8_t type;
115  uint8_t len;
116  uint8_t ptr;
117  uint8_t flags;
118  uint8_t data[0];
119 };
120 
121 #define MAX_TIMESTAMP_SIZE 40
122 #define MAX_OPTION_SIZE MAX_TIMESTAMP_SIZE
123 
124 /* Macro to get the network mask from an IP address in host byte order. */
125 #define __netmask(_ip) \
126  (((_ip) == 0UL) ? 0UL \
127  : IN_CLASSA((_ip)) ? IN_CLASSA_NET \
128  : IN_CLASSB((_ip)) ? IN_CLASSB_NET \
129  : IN_CLASSC((_ip)) ? IN_CLASSC_NET \
130  : IN_CLASSD((_ip)) ? IN_CLASSD_NET \
131  : 0UL)
132 
133 #define __netbytes(_ip) \
134  ((uint8_t)(((_ip) == 0UL) ? 0 \
135  : IN_CLASSA((_ip)) ? 1 \
136  : IN_CLASSB((_ip)) ? 2 \
137  : IN_CLASSC((_ip)) ? 3 \
138  : IN_CLASSD((_ip)) ? 4 \
139  : 0))
140 
141 /*
142  * Adjust the checksum to reflect that the TTL had been decremented.
143  *
144  * Flip the bits on the checksum, decrement the high byte of the checksum,
145  * fold in any carry, and then flip the bits back. Rather than convert
146  * the checksum to host byte order and then back to network byte order,
147  * just convert the increment to network byte order. Note: in 1's
148  * complement arithmetic, subtracting by x is the same as adding the 1's
149  * complement of x. So, in 16 bit arithmetic, rather than subtracting by
150  * (1<<8), we can add by (1<<8)^0xffff. Since it's all constants, that
151  * should be evaluated by the compiler at compile time.
152  *
153  * Doing the ^0xffff to initially flip the bits keeps the upper bits from
154  * also being flipped. Using the ~ operation at the end doesn't matter,
155  * because the upper bits get tossed when we assign it to the 16 bit sum
156  * field, so let the compiler do whatever is fastest.
157  */
158 static inline void
159 ipv4_adjust_cksum(struct cne_ipv4_hdr *hdr)
160 {
161  int32_t cksum;
162 
163  hdr->time_to_live--;
164 
165  /* increment checksum high byte */
166  cksum = (int32_t)(hdr->hdr_checksum ^ 0xFFFF) + (int32_t)htobe16(((1 << 8) ^ 0xFFFF));
167 
168  /* Fold the carry bit into the checksum */
169  hdr->hdr_checksum = ~(cksum + (cksum >> 16));
170 }
171 
180 CNDP_API int cnet_ipv4_stats_dump(struct stk_s *stk);
181 
192 CNDP_API void cnet_ipv4_dump(const char *msg, struct cne_ipv4_hdr *ip);
193 
194 #if CNET_IP4_DUMP_ENABLED
195 #define IP4_DUMP(ip) \
196  do { \
197  cne_printf("[cyan]([orange]%s[cyan]:[orange]%d[cyan]) ", __func__, __LINE__); \
198  cnet_ipv4_dump(NULL, ip); \
199  } while (0)
200 #else
201 #define IP4_DUMP(ip) \
202  do { \
203  } while (0)
204 #endif
205 
206 #ifdef __cplusplus
207 }
208 #endif
209 
210 #endif /* __CNET_IPv4_H */
#define __cne_packed
Definition: cne_common.h:129
CNDP_API int cnet_ipv4_stats_dump(struct stk_s *stk)
Dump the IPv4 statistics.
CNDP_API void cnet_ipv4_dump(const char *msg, struct cne_ipv4_hdr *ip)
Dump information about IPv4 header.
uint8_t time_to_live
Definition: cne_ip.h:39
cne_be16_t hdr_checksum
Definition: cne_ip.h:41