xref: /dpdk/examples/ipsec-secgw/ipsec-secgw.h (revision b19f366c)
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright (C) 2020 Marvell International Ltd.
3  */
4 #ifndef _IPSEC_SECGW_H_
5 #define _IPSEC_SECGW_H_
6 
7 #include <stdbool.h>
8 
9 #ifndef STATS_INTERVAL
10 #define STATS_INTERVAL 0
11 #endif
12 
13 #define NB_SOCKETS 4
14 
15 #define MAX_PKT_BURST 32
16 
17 #define RTE_LOGTYPE_IPSEC RTE_LOGTYPE_USER1
18 
19 #if RTE_BYTE_ORDER != RTE_LITTLE_ENDIAN
20 #define __BYTES_TO_UINT64(a, b, c, d, e, f, g, h) \
21 	(((uint64_t)((a) & 0xff) << 56) | \
22 	((uint64_t)((b) & 0xff) << 48) | \
23 	((uint64_t)((c) & 0xff) << 40) | \
24 	((uint64_t)((d) & 0xff) << 32) | \
25 	((uint64_t)((e) & 0xff) << 24) | \
26 	((uint64_t)((f) & 0xff) << 16) | \
27 	((uint64_t)((g) & 0xff) << 8)  | \
28 	((uint64_t)(h) & 0xff))
29 #else
30 #define __BYTES_TO_UINT64(a, b, c, d, e, f, g, h) \
31 	(((uint64_t)((h) & 0xff) << 56) | \
32 	((uint64_t)((g) & 0xff) << 48) | \
33 	((uint64_t)((f) & 0xff) << 40) | \
34 	((uint64_t)((e) & 0xff) << 32) | \
35 	((uint64_t)((d) & 0xff) << 24) | \
36 	((uint64_t)((c) & 0xff) << 16) | \
37 	((uint64_t)((b) & 0xff) << 8) | \
38 	((uint64_t)(a) & 0xff))
39 #endif
40 
41 #define uint32_t_to_char(ip, a, b, c, d) do {\
42 		*a = (uint8_t)(ip >> 24 & 0xff);\
43 		*b = (uint8_t)(ip >> 16 & 0xff);\
44 		*c = (uint8_t)(ip >> 8 & 0xff);\
45 		*d = (uint8_t)(ip & 0xff);\
46 	} while (0)
47 
48 #define ETHADDR(a, b, c, d, e, f) (__BYTES_TO_UINT64(a, b, c, d, e, f, 0, 0))
49 
50 #define IPSEC_NAT_T_PORT 4500
51 #define MBUF_PTYPE_TUNNEL_ESP_IN_UDP (RTE_PTYPE_TUNNEL_ESP | RTE_PTYPE_L4_UDP)
52 
53 struct traffic_type {
54 	const uint8_t *data[MAX_PKT_BURST * 2];
55 	struct rte_mbuf *pkts[MAX_PKT_BURST * 2];
56 	void *saptr[MAX_PKT_BURST * 2];
57 	uint32_t res[MAX_PKT_BURST * 2];
58 	uint32_t num;
59 };
60 
61 struct ipsec_traffic {
62 	struct traffic_type ipsec;
63 	struct traffic_type ip4;
64 	struct traffic_type ip6;
65 };
66 
67 /* Fields optimized for devices without burst */
68 struct traffic_type_nb {
69 	const uint8_t *data;
70 	struct rte_mbuf *pkt;
71 	uint32_t res;
72 	uint32_t num;
73 };
74 
75 struct ipsec_traffic_nb {
76 	struct traffic_type_nb ipsec;
77 	struct traffic_type_nb ip4;
78 	struct traffic_type_nb ip6;
79 };
80 
81 /* port/source ethernet addr and destination ethernet addr */
82 struct ethaddr_info {
83 	uint64_t src, dst;
84 };
85 
86 #if (STATS_INTERVAL > 0)
87 struct ipsec_core_statistics {
88 	uint64_t tx;
89 	uint64_t rx;
90 	uint64_t rx_call;
91 	uint64_t tx_call;
92 	uint64_t dropped;
93 	uint64_t burst_rx;
94 } __rte_cache_aligned;
95 
96 struct ipsec_core_statistics core_statistics[RTE_MAX_LCORE];
97 #endif /* STATS_INTERVAL */
98 
99 extern struct ethaddr_info ethaddr_tbl[RTE_MAX_ETHPORTS];
100 
101 /* Port mask to identify the unprotected ports */
102 extern uint32_t unprotected_port_mask;
103 
104 /* Index of SA in single mode */
105 extern uint32_t single_sa_idx;
106 
107 extern volatile bool force_quit;
108 
109 static inline uint8_t
110 is_unprotected_port(uint16_t port_id)
111 {
112 	return unprotected_port_mask & (1 << port_id);
113 }
114 
115 static inline void
116 core_stats_update_rx(int n)
117 {
118 #if (STATS_INTERVAL > 0)
119 	int lcore_id = rte_lcore_id();
120 	core_statistics[lcore_id].rx += n;
121 	core_statistics[lcore_id].rx_call++;
122 	if (n == MAX_PKT_BURST)
123 		core_statistics[lcore_id].burst_rx += n;
124 #else
125 	RTE_SET_USED(n);
126 #endif /* STATS_INTERVAL */
127 }
128 
129 static inline void
130 core_stats_update_tx(int n)
131 {
132 #if (STATS_INTERVAL > 0)
133 	int lcore_id = rte_lcore_id();
134 	core_statistics[lcore_id].tx += n;
135 	core_statistics[lcore_id].tx_call++;
136 #else
137 	RTE_SET_USED(n);
138 #endif /* STATS_INTERVAL */
139 }
140 
141 static inline void
142 core_stats_update_drop(int n)
143 {
144 #if (STATS_INTERVAL > 0)
145 	int lcore_id = rte_lcore_id();
146 	core_statistics[lcore_id].dropped += n;
147 #else
148 	RTE_SET_USED(n);
149 #endif /* STATS_INTERVAL */
150 }
151 
152 /* helper routine to free bulk of packets */
153 static inline void
154 free_pkts(struct rte_mbuf *mb[], uint32_t n)
155 {
156 	uint32_t i;
157 
158 	for (i = 0; i != n; i++)
159 		rte_pktmbuf_free(mb[i]);
160 
161 	core_stats_update_drop(n);
162 }
163 
164 #endif /* _IPSEC_SECGW_H_ */
165