Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause
2 : : * Copyright (C), 2025, Wuxi Stars Micro System Technologies Co., Ltd.
3 : : */
4 : :
5 : : #include <rte_malloc.h>
6 : :
7 : : #include "sxe2_ethdev.h"
8 : : #include "sxe2_security.h"
9 : : #include "sxe2_ipsec.h"
10 : : #include "sxe2_common_log.h"
11 : :
12 : : static unsigned int
13 : 0 : sxe2_security_session_size_get(void *device __rte_unused)
14 : : {
15 : 0 : return sizeof(struct sxe2_security_session);
16 : : }
17 : :
18 : : static int
19 : 0 : sxe2_security_session_create(void *device,
20 : : struct rte_security_session_conf *conf,
21 : : struct rte_security_session *session)
22 : : {
23 : 0 : int32_t ret = -1;
24 : 0 : struct sxe2_security_session *sxe2_sess = NULL;
25 : 0 : sxe2_sess = SECURITY_GET_SESS_PRIV(session);
26 : :
27 [ # # ]: 0 : switch (conf->protocol) {
28 : 0 : case RTE_SECURITY_PROTOCOL_IPSEC:
29 : 0 : ret = sxe2_ipsec_session_create(device, conf, sxe2_sess);
30 : 0 : break;
31 : 0 : default:
32 : 0 : PMD_LOG_ERR(DRV, "Invalid security protocol.");
33 : 0 : ret = -EINVAL;
34 : 0 : break;
35 : : }
36 : :
37 : 0 : return ret;
38 : : }
39 : :
40 : : static int
41 : 0 : sxe2_security_session_destroy(void *device, struct rte_security_session *session)
42 : : {
43 : 0 : int32_t ret = -1;
44 : 0 : struct sxe2_security_session *sxe2_sess = NULL;
45 : 0 : sxe2_sess = SECURITY_GET_SESS_PRIV(session);
46 : :
47 [ # # ]: 0 : switch (sxe2_sess->protocol) {
48 : 0 : case RTE_SECURITY_PROTOCOL_IPSEC:
49 : 0 : ret = sxe2_ipsec_session_destroy(device, session);
50 : 0 : break;
51 : 0 : default:
52 : 0 : PMD_LOG_ERR(DRV, "Invalid security protocol.");
53 : 0 : ret = -EINVAL;
54 : 0 : break;
55 : : }
56 : 0 : return ret;
57 : : }
58 : :
59 : : static int
60 : 0 : sxe2_security_pkt_metadata_set(void *device,
61 : : struct rte_security_session *session,
62 : : struct rte_mbuf *m, void *params)
63 : : {
64 : 0 : struct sxe2_security_session *sxe2_sess = NULL;
65 : 0 : sxe2_sess = SECURITY_GET_SESS_PRIV(session);
66 : 0 : int32_t ret = -1;
67 : :
68 [ # # ]: 0 : switch (sxe2_sess->protocol) {
69 : 0 : case RTE_SECURITY_PROTOCOL_IPSEC:
70 : 0 : ret = sxe2_ipsec_pkt_metadata_set(device, session, m, params);
71 : 0 : break;
72 : 0 : default:
73 : 0 : PMD_LOG_ERR(DRV, "Invalid security protocol.");
74 : 0 : ret = -EINVAL;
75 : 0 : break;
76 : : }
77 : :
78 : 0 : return ret;
79 : : }
80 : :
81 : : static const struct rte_security_capability *
82 : 0 : sxe2_security_capabilities_get(void *device __rte_unused)
83 : : {
84 : 0 : static const struct rte_cryptodev_capabilities
85 : : ipsec_crypto_capabilities[] = {
86 : : {
87 : : .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
88 : : {.sym = {
89 : : .xform_type = RTE_CRYPTO_SYM_XFORM_CIPHER,
90 : : {.cipher = {
91 : : .algo = SXE2_RTE_CRYPTO_CIPHER_AES_CBC,
92 : : .block_size = SXE2_SECURITY_BLOCK_SIZE_16,
93 : : .key_size = {
94 : : .min = SXE2_IPSEC_AES_KEY_MIN,
95 : : .max = SXE2_IPSEC_AES_KEY_MAX,
96 : : .increment = SXE2_IPSEC_AES_KEY_INC
97 : : },
98 : : .iv_size = {
99 : : .min = SXE2_IPSEC_AES_IV_MIN,
100 : : .max = SXE2_IPSEC_AES_IV_MAX,
101 : : .increment = SXE2_IPSEC_AES_IV_INC
102 : : },
103 : : .dataunit_set = RTE_CRYPTO_CIPHER_DATA_UNIT_LEN_512_BYTES,
104 : : }, }
105 : : }, }
106 : : },
107 : : {
108 : : .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
109 : : {.sym = {
110 : : .xform_type = RTE_CRYPTO_SYM_XFORM_CIPHER,
111 : : {.cipher = {
112 : : .algo = SXE2_RTE_RTE_CRYPTO_CIPHER_SM4_CBC,
113 : : .block_size = SXE2_SECURITY_BLOCK_SIZE_16,
114 : : .key_size = {
115 : : .min = SXE2_IPSEC_SM4_KEY_MIN,
116 : : .max = SXE2_IPSEC_SM4_KEY_MAX,
117 : : .increment = SXE2_IPSEC_SM4_KEY_INC
118 : : },
119 : : .iv_size = {
120 : : .min = SXE2_IPSEC_SM4_IV_MIN,
121 : : .max = SXE2_IPSEC_SM4_IV_MAX,
122 : : .increment = SXE2_IPSEC_SM4_IV_INC
123 : : },
124 : : .dataunit_set = RTE_CRYPTO_CIPHER_DATA_UNIT_LEN_512_BYTES,
125 : : }, }
126 : : }, }
127 : : },
128 : : {
129 : : .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
130 : : {.sym = {
131 : : .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH,
132 : : {.auth = {
133 : : .algo = SXE2_RTE_CRYPTO_AUTH_SHA256_HMAC,
134 : : .block_size = SXE2_SECURITY_BLOCK_SIZE_64,
135 : : .key_size = {
136 : : .min = SXE2_IPSEC_SHA_KEY_MIN,
137 : : .max = SXE2_IPSEC_SHA_KEY_MAX,
138 : : .increment = SXE2_IPSEC_SHA_KEY_INC
139 : : },
140 : : .digest_size = {
141 : : .min = SXE2_IPSEC_SHA_DIGEST_MIN,
142 : : .max = SXE2_IPSEC_SHA_DIGEST_MAX,
143 : : .increment = SXE2_IPSEC_SHA_DIGEST_INC
144 : : },
145 : : .iv_size = {
146 : : .min = SXE2_IPSEC_SHA_IV_MIN,
147 : : .max = SXE2_IPSEC_SHA_IV_MAX,
148 : : .increment = SXE2_IPSEC_SHA_IV_INC
149 : : },
150 : : .aad_size = {
151 : : .min = SXE2_IPSEC_AAD_MIN,
152 : : .max = SXE2_IPSEC_AAD_MAX,
153 : : .increment = SXE2_IPSEC_AAD_INC
154 : : }
155 : : }, }
156 : : }, }
157 : : },
158 : : {
159 : : .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
160 : : {.sym = {
161 : : .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH,
162 : : {.auth = {
163 : : .algo = SXE2_RTE_CRYPTO_AUTH_SM3_HMAC,
164 : : .block_size = SXE2_SECURITY_BLOCK_SIZE_64,
165 : : .key_size = {
166 : : .min = SXE2_IPSEC_SM3_KEY_MIN,
167 : : .max = SXE2_IPSEC_SM3_KEY_MAX,
168 : : .increment = SXE2_IPSEC_SM3_KEY_INC
169 : : },
170 : : .digest_size = {
171 : : .min = SXE2_IPSEC_SM3_DIGEST_MIN,
172 : : .max = SXE2_IPSEC_SM3_DIGEST_MAX,
173 : : .increment = SXE2_IPSEC_SM3_DIGEST_INC
174 : : },
175 : : .iv_size = {
176 : : .min = SXE2_IPSEC_SM3_IV_MIN,
177 : : .max = SXE2_IPSEC_SM3_IV_MAX,
178 : : .increment = SXE2_IPSEC_SM3_IV_INC
179 : : },
180 : : .aad_size = {
181 : : .min = SXE2_IPSEC_AAD_MIN,
182 : : .max = SXE2_IPSEC_AAD_MAX,
183 : : .increment = SXE2_IPSEC_AAD_INC
184 : : }
185 : : }, }
186 : : }, }
187 : : },
188 : : {
189 : : .op = RTE_CRYPTO_OP_TYPE_UNDEFINED,
190 : : {.sym = {
191 : : .xform_type = RTE_CRYPTO_SYM_XFORM_NOT_SPECIFIED
192 : : }, }
193 : : }
194 : : };
195 : :
196 : 0 : static const struct rte_security_capability
197 : : sxe2_security_capabilities[] = {
198 : : {
199 : : .action = RTE_SECURITY_ACTION_TYPE_INLINE_CRYPTO,
200 : : .protocol = RTE_SECURITY_PROTOCOL_IPSEC,
201 : : {.ipsec = {
202 : : .proto = RTE_SECURITY_IPSEC_SA_PROTO_ESP,
203 : : .mode = RTE_SECURITY_IPSEC_SA_MODE_TUNNEL,
204 : : .direction = RTE_SECURITY_IPSEC_SA_DIR_EGRESS,
205 : : .options = {
206 : : .esn = 0,
207 : : .udp_encap = 1,
208 : : .copy_dscp = 0,
209 : : .copy_flabel = 0,
210 : : .copy_df = 0,
211 : : .dec_ttl = 0,
212 : : .ecn = 0,
213 : : .stats = 1,
214 : : .iv_gen_disable = 0,
215 : : .tunnel_hdr_verify = 1,
216 : : .udp_ports_verify = 1,
217 : : .ip_csum_enable = 0,
218 : : .l4_csum_enable = 0,
219 : : .ip_reassembly_en = 0,
220 : : .ingress_oop = 0
221 : : } } },
222 : : .crypto_capabilities = ipsec_crypto_capabilities,
223 : : .ol_flags = RTE_SECURITY_TX_OLOAD_NEED_MDATA
224 : : },
225 : : {
226 : : .action = RTE_SECURITY_ACTION_TYPE_INLINE_CRYPTO,
227 : : .protocol = RTE_SECURITY_PROTOCOL_IPSEC,
228 : : {.ipsec = {
229 : : .proto = RTE_SECURITY_IPSEC_SA_PROTO_ESP,
230 : : .mode = RTE_SECURITY_IPSEC_SA_MODE_TUNNEL,
231 : : .direction = RTE_SECURITY_IPSEC_SA_DIR_INGRESS,
232 : : .options = {
233 : : .esn = 0,
234 : : .udp_encap = 1,
235 : : .copy_dscp = 0,
236 : : .copy_flabel = 0,
237 : : .copy_df = 0,
238 : : .dec_ttl = 0,
239 : : .ecn = 0,
240 : : .stats = 1,
241 : : .iv_gen_disable = 0,
242 : : .tunnel_hdr_verify = 1,
243 : : .udp_ports_verify = 1,
244 : : .ip_csum_enable = 0,
245 : : .l4_csum_enable = 0,
246 : : .ip_reassembly_en = 0,
247 : : .ingress_oop = 0
248 : : } } },
249 : : .crypto_capabilities = ipsec_crypto_capabilities,
250 : : .ol_flags = 0
251 : : },
252 : : {
253 : : .action = RTE_SECURITY_ACTION_TYPE_NONE
254 : : }
255 : : };
256 : :
257 : 0 : return sxe2_security_capabilities;
258 : : }
259 : :
260 : : static struct rte_security_ops sxe2_security_ops = {
261 : : .session_get_size = sxe2_security_session_size_get,
262 : : .session_create = sxe2_security_session_create,
263 : : .session_destroy = sxe2_security_session_destroy,
264 : : .set_pkt_metadata = sxe2_security_pkt_metadata_set,
265 : : .capabilities_get = sxe2_security_capabilities_get,
266 : : };
267 : :
268 : 0 : int32_t sxe2_security_init(struct rte_eth_dev *dev)
269 : : {
270 : 0 : struct sxe2_adapter *adapter = SXE2_DEV_PRIVATE_TO_ADAPTER(dev);
271 : 0 : struct rte_security_ctx *sctx = NULL;
272 : 0 : struct sxe2_security_ctx *sxe2_sctx = &adapter->security_ctx;
273 : 0 : int32_t ret = -1;
274 : :
275 [ # # ]: 0 : if (!sxe2_ipsec_supported(adapter)) {
276 : 0 : ret = 0;
277 : 0 : PMD_LOG_INFO(INIT, "Not support security feature.");
278 : 0 : goto l_end;
279 : : }
280 : :
281 : 0 : PMD_LOG_INFO(INIT, "Init security feature.");
282 : :
283 : 0 : sctx = rte_zmalloc("security_ctx", sizeof(struct rte_security_ctx), 0);
284 [ # # ]: 0 : if (sctx == NULL) {
285 : 0 : ret = -ENOMEM;
286 : 0 : goto l_end;
287 : : }
288 : :
289 : 0 : sctx->device = dev;
290 : 0 : sctx->ops = &sxe2_security_ops;
291 : 0 : sctx->sess_cnt = 0;
292 : 0 : sctx->flags = 0;
293 : 0 : dev->security_ctx = (void *)sctx;
294 : :
295 : 0 : rte_spinlock_init(&sxe2_sctx->security_lock);
296 : 0 : sxe2_sctx->adapter = adapter;
297 : :
298 [ # # ]: 0 : if (sxe2_ipsec_supported(adapter)) {
299 : 0 : ret = sxe2_ipsec_init(adapter);
300 [ # # ]: 0 : if (ret) {
301 : 0 : rte_free(sctx);
302 : 0 : sctx = NULL;
303 : 0 : dev->security_ctx = NULL;
304 : 0 : goto l_end;
305 : : }
306 : : }
307 : :
308 : : ret = 0;
309 : :
310 : 0 : l_end:
311 : 0 : return ret;
312 : : }
313 : :
314 : 0 : void sxe2_security_uinit(struct rte_eth_dev *dev)
315 : : {
316 : 0 : struct sxe2_adapter *adapter = SXE2_DEV_PRIVATE_TO_ADAPTER(dev);
317 : 0 : struct rte_security_ctx *sctx = dev->security_ctx;
318 : :
319 [ # # ]: 0 : if (!sxe2_ipsec_supported(adapter)) {
320 : 0 : PMD_LOG_INFO(INIT, "Not support security feature.");
321 : 0 : goto l_end;
322 : : }
323 : :
324 : 0 : PMD_LOG_INFO(INIT, "Uinit security feature.");
325 : :
326 [ # # ]: 0 : if (sctx != NULL) {
327 : 0 : rte_free(sctx);
328 : 0 : sctx = NULL;
329 : : }
330 : :
331 : 0 : sxe2_ipsec_uinit(adapter);
332 : :
333 : 0 : l_end:
334 : 0 : return;
335 : : }
|