Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause
2 : : * Copyright(C) 2025 Marvell.
3 : : */
4 : :
5 : : #include <cryptodev_pmd.h>
6 : : #include <rte_esp.h>
7 : : #include <rte_ip.h>
8 : : #include <rte_malloc.h>
9 : : #include <rte_security.h>
10 : : #include <rte_security_driver.h>
11 : : #include <rte_udp.h>
12 : :
13 : : #include "cn20k_cryptodev_ops.h"
14 : : #include "cn20k_cryptodev_sec.h"
15 : : #include "cn20k_ipsec.h"
16 : : #include "cnxk_cryptodev.h"
17 : : #include "cnxk_cryptodev_ops.h"
18 : : #include "cnxk_ipsec.h"
19 : : #include "cnxk_security.h"
20 : :
21 : : #include "roc_api.h"
22 : :
23 : : static int
24 : 0 : cn20k_ipsec_outb_sa_create(struct roc_cpt *roc_cpt, struct roc_cpt_lf *lf,
25 : : struct rte_security_ipsec_xform *ipsec_xfrm,
26 : : struct rte_crypto_sym_xform *crypto_xfrm,
27 : : struct cn20k_sec_session *sec_sess)
28 : : {
29 : : union roc_ow_ipsec_outb_param1 param1;
30 : : struct roc_ow_ipsec_outb_sa *sa_dptr;
31 : : struct cnxk_ipsec_outb_rlens rlens;
32 : : struct cn20k_ipsec_sa *sa;
33 : : union cpt_inst_w4 inst_w4;
34 : : void *out_sa;
35 : : int ret = 0;
36 : :
37 : : sa = &sec_sess->sa;
38 : :
39 : 0 : out_sa = rte_zmalloc("cn20k_cpt", sizeof(struct roc_ow_ipsec_outb_sa), ROC_CPTR_ALIGN);
40 [ # # ]: 0 : if (out_sa == NULL) {
41 : 0 : plt_err("Couldn't allocate memory for outbound SA");
42 : 0 : return -ENOMEM;
43 : : }
44 : 0 : sa->out_sa = out_sa;
45 : :
46 : : /* Allocate memory to be used as dptr for CPT ucode WRITE_SA op */
47 : 0 : sa_dptr = plt_zmalloc(sizeof(struct roc_ow_ipsec_outb_sa), 8);
48 [ # # ]: 0 : if (sa_dptr == NULL) {
49 : 0 : plt_err("Could not allocate memory for SA dptr");
50 : : ret = -ENOMEM;
51 : 0 : goto sa_cptr_free;
52 : : }
53 : :
54 : : /* Translate security parameters to SA */
55 : 0 : ret = cnxk_ow_ipsec_outb_sa_fill(sa_dptr, ipsec_xfrm, crypto_xfrm, roc_cpt->ctx_ilen);
56 [ # # ]: 0 : if (ret) {
57 : 0 : plt_err("Could not fill outbound session parameters");
58 : 0 : goto sa_free;
59 : : }
60 : :
61 : 0 : sec_sess->inst.w7 = cnxk_cpt_sec_inst_w7_get(roc_cpt, out_sa);
62 : :
63 : : #ifdef LA_IPSEC_DEBUG
64 : : /* Use IV from application in debug mode */
65 : : if (ipsec_xfrm->options.iv_gen_disable == 1) {
66 : : sa_dptr->w2.s.iv_src = ROC_IE_OW_SA_IV_SRC_FROM_SA;
67 : : if (crypto_xfrm->type == RTE_CRYPTO_SYM_XFORM_AEAD) {
68 : : sec_sess->iv_offset = crypto_xfrm->aead.iv.offset;
69 : : sec_sess->iv_length = crypto_xfrm->aead.iv.length;
70 : : } else if (crypto_xfrm->type == RTE_CRYPTO_SYM_XFORM_CIPHER) {
71 : : sec_sess->iv_offset = crypto_xfrm->cipher.iv.offset;
72 : : sec_sess->iv_length = crypto_xfrm->cipher.iv.length;
73 : : } else {
74 : : sec_sess->iv_offset = crypto_xfrm->auth.iv.offset;
75 : : sec_sess->iv_length = crypto_xfrm->auth.iv.length;
76 : : }
77 : : }
78 : : #else
79 [ # # ]: 0 : if (ipsec_xfrm->options.iv_gen_disable != 0) {
80 : 0 : plt_err("Application provided IV not supported");
81 : : ret = -ENOTSUP;
82 : 0 : goto sa_free;
83 : : }
84 : : #endif
85 : :
86 : 0 : sec_sess->ipsec.is_outbound = 1;
87 : :
88 : : /* Get Rlen calculation data */
89 : 0 : ret = cnxk_ipsec_outb_rlens_get(&rlens, ipsec_xfrm, crypto_xfrm);
90 [ # # ]: 0 : if (ret)
91 : 0 : goto sa_free;
92 : :
93 : 0 : sec_sess->max_extended_len = rlens.max_extended_len;
94 : :
95 : : /* pre-populate CPT INST word 4 */
96 : 0 : inst_w4.u64 = 0;
97 : 0 : inst_w4.s.opcode_major = ROC_IE_OW_MAJOR_OP_PROCESS_OUTBOUND_IPSEC | ROC_IE_OW_INPLACE_BIT;
98 : :
99 : 0 : param1.u16 = 0;
100 : :
101 : 0 : param1.s.ttl_or_hop_limit = ipsec_xfrm->options.dec_ttl;
102 : :
103 : : /* Disable IP checksum computation by default */
104 : 0 : param1.s.ip_csum_disable = ROC_IE_OW_SA_INNER_PKT_IP_CSUM_DISABLE;
105 : :
106 [ # # ]: 0 : if (ipsec_xfrm->options.ip_csum_enable)
107 : 0 : param1.s.ip_csum_disable = ROC_IE_OW_SA_INNER_PKT_IP_CSUM_ENABLE;
108 : :
109 : : /* Disable L4 checksum computation by default */
110 : 0 : param1.s.l4_csum_disable = ROC_IE_OW_SA_INNER_PKT_L4_CSUM_DISABLE;
111 : :
112 [ # # ]: 0 : if (ipsec_xfrm->options.l4_csum_enable)
113 : 0 : param1.s.l4_csum_disable = ROC_IE_OW_SA_INNER_PKT_L4_CSUM_ENABLE;
114 : :
115 : 0 : inst_w4.s.param1 = param1.u16;
116 : :
117 : 0 : sec_sess->inst.w4 = inst_w4.u64;
118 : :
119 [ # # ]: 0 : if (ipsec_xfrm->options.stats == 1) {
120 : : /* Enable mib counters */
121 : 0 : sa_dptr->w0.s.count_mib_bytes = 1;
122 : 0 : sa_dptr->w0.s.count_mib_pkts = 1;
123 : 0 : sa_dptr->w0.s.count_glb_pkts = 1;
124 : 0 : sa_dptr->w0.s.count_glb_octets = 1;
125 : : }
126 : :
127 : : memset(out_sa, 0, sizeof(struct roc_ow_ipsec_outb_sa));
128 : :
129 : : /* Copy word0 from sa_dptr to populate ctx_push_sz ctx_size fields */
130 : : memcpy(out_sa, sa_dptr, 8);
131 : :
132 : : rte_atomic_thread_fence(rte_memory_order_seq_cst);
133 : :
134 : : /* Write session using microcode opcode */
135 : 0 : ret = roc_cpt_ctx_write(lf, sa_dptr, out_sa, sizeof(struct roc_ow_ipsec_outb_sa));
136 [ # # ]: 0 : if (ret) {
137 : 0 : plt_err("Could not write outbound session to hardware");
138 : 0 : goto sa_free;
139 : : }
140 : :
141 : : /* Trigger CTX flush so that data is written back to DRAM */
142 : 0 : ret = roc_cpt_lf_ctx_flush(lf, out_sa, false);
143 [ # # ]: 0 : if (ret == -EFAULT) {
144 : 0 : plt_err("Could not flush outbound session");
145 : 0 : goto sa_free;
146 : : }
147 : :
148 : 0 : sec_sess->proto = RTE_SECURITY_PROTOCOL_IPSEC;
149 : : rte_atomic_thread_fence(rte_memory_order_seq_cst);
150 : :
151 : 0 : sa_free:
152 : 0 : plt_free(sa_dptr);
153 : : sa_cptr_free:
154 [ # # ]: 0 : if (ret != 0) {
155 : 0 : rte_free(out_sa);
156 : : out_sa = NULL;
157 : : }
158 : :
159 : : return ret;
160 : : }
161 : :
162 : : static int
163 : 0 : cn20k_ipsec_inb_sa_create(struct roc_cpt *roc_cpt, struct roc_cpt_lf *lf,
164 : : struct rte_security_ipsec_xform *ipsec_xfrm,
165 : : struct rte_crypto_sym_xform *crypto_xfrm,
166 : : struct cn20k_sec_session *sec_sess)
167 : : {
168 : : union roc_ow_ipsec_inb_param1 param1;
169 : : struct roc_ow_ipsec_inb_sa *sa_dptr;
170 : : struct cn20k_ipsec_sa *sa;
171 : : union cpt_inst_w4 inst_w4;
172 : : void *in_sa;
173 : : int ret = 0;
174 : :
175 : : sa = &sec_sess->sa;
176 : :
177 : 0 : in_sa = rte_zmalloc("cn20k_cpt", sizeof(struct roc_ow_ipsec_inb_sa), ROC_CPTR_ALIGN);
178 [ # # ]: 0 : if (in_sa == NULL) {
179 : 0 : plt_err("Couldn't allocate memory for inbound SA");
180 : 0 : return -ENOMEM;
181 : : }
182 : 0 : sa->in_sa = in_sa;
183 : :
184 : : /* Allocate memory to be used as dptr for CPT ucode WRITE_SA op */
185 : 0 : sa_dptr = plt_zmalloc(sizeof(struct roc_ow_ipsec_inb_sa), 8);
186 [ # # ]: 0 : if (sa_dptr == NULL) {
187 : 0 : plt_err("Could not allocate memory for SA dptr");
188 : : ret = -ENOMEM;
189 : 0 : goto sa_cptr_free;
190 : : }
191 : :
192 : : /* Translate security parameters to SA */
193 : 0 : ret = cnxk_ow_ipsec_inb_sa_fill(sa_dptr, ipsec_xfrm, crypto_xfrm, roc_cpt->ctx_ilen);
194 [ # # ]: 0 : if (ret) {
195 : 0 : plt_err("Could not fill inbound session parameters");
196 : 0 : goto sa_free;
197 : : }
198 : :
199 [ # # ]: 0 : sec_sess->ipsec.is_outbound = 0;
200 : 0 : sec_sess->inst.w7 = cnxk_cpt_sec_inst_w7_get(roc_cpt, in_sa);
201 : :
202 : : /* Save index/SPI in cookie, requirement for Rx Inject */
203 : 0 : sa_dptr->w1.s.cookie = 0xFFFFFFFF;
204 : :
205 : : /* pre-populate CPT INST word 4 */
206 : 0 : inst_w4.u64 = 0;
207 : 0 : inst_w4.s.opcode_major = ROC_IE_OW_MAJOR_OP_PROCESS_INBOUND_IPSEC | ROC_IE_OW_INPLACE_BIT;
208 : :
209 : 0 : param1.u16 = 0;
210 : :
211 : : /* Disable IP checksum verification by default */
212 : 0 : param1.s.ip_csum_disable = ROC_IE_OW_SA_INNER_PKT_IP_CSUM_DISABLE;
213 : :
214 : : /* Set the ip chksum flag in mbuf before enqueue.
215 : : * Reset the flag in post process in case of errors
216 : : */
217 [ # # ]: 0 : if (ipsec_xfrm->options.ip_csum_enable) {
218 : 0 : param1.s.ip_csum_disable = ROC_IE_OW_SA_INNER_PKT_IP_CSUM_ENABLE;
219 : 0 : sec_sess->ipsec.ip_csum = RTE_MBUF_F_RX_IP_CKSUM_GOOD;
220 : : }
221 : :
222 : : /* Disable L4 checksum verification by default */
223 : 0 : param1.s.l4_csum_disable = ROC_IE_OW_SA_INNER_PKT_L4_CSUM_DISABLE;
224 : :
225 [ # # ]: 0 : if (ipsec_xfrm->options.l4_csum_enable)
226 : 0 : param1.s.l4_csum_disable = ROC_IE_OW_SA_INNER_PKT_L4_CSUM_ENABLE;
227 : :
228 : 0 : param1.s.esp_trailer_disable = 1;
229 : :
230 : 0 : inst_w4.s.param1 = param1.u16;
231 : :
232 : 0 : sec_sess->inst.w4 = inst_w4.u64;
233 : :
234 [ # # ]: 0 : if (ipsec_xfrm->options.stats == 1) {
235 : : /* Enable mib counters */
236 : 0 : sa_dptr->w0.s.count_mib_bytes = 1;
237 : 0 : sa_dptr->w0.s.count_mib_pkts = 1;
238 : 0 : sa_dptr->w0.s.count_glb_pkts = 1;
239 : 0 : sa_dptr->w0.s.count_glb_octets = 1;
240 : : }
241 : :
242 : : memset(in_sa, 0, sizeof(struct roc_ow_ipsec_inb_sa));
243 : :
244 : : /* Copy word0 from sa_dptr to populate ctx_push_sz ctx_size fields */
245 : : memcpy(in_sa, sa_dptr, 8);
246 : :
247 : : rte_atomic_thread_fence(rte_memory_order_seq_cst);
248 : :
249 : : /* Write session using microcode opcode */
250 : 0 : ret = roc_cpt_ctx_write(lf, sa_dptr, in_sa, sizeof(struct roc_ow_ipsec_inb_sa));
251 [ # # ]: 0 : if (ret) {
252 : 0 : plt_err("Could not write inbound session to hardware");
253 : 0 : goto sa_free;
254 : : }
255 : :
256 : : /* Trigger CTX flush so that data is written back to DRAM */
257 : 0 : ret = roc_cpt_lf_ctx_flush(lf, in_sa, true);
258 [ # # ]: 0 : if (ret == -EFAULT) {
259 : 0 : plt_err("Could not flush inbound session");
260 : 0 : goto sa_free;
261 : : }
262 : :
263 : 0 : sec_sess->proto = RTE_SECURITY_PROTOCOL_IPSEC;
264 : : rte_atomic_thread_fence(rte_memory_order_seq_cst);
265 : :
266 : 0 : sa_free:
267 : 0 : plt_free(sa_dptr);
268 : : sa_cptr_free:
269 [ # # ]: 0 : if (ret != 0) {
270 : 0 : rte_free(in_sa);
271 : : in_sa = NULL;
272 : : }
273 : :
274 : : return ret;
275 : : }
276 : :
277 : : int
278 : 0 : cn20k_ipsec_session_create(struct cnxk_cpt_vf *vf, struct cnxk_cpt_qp *qp,
279 : : struct rte_security_ipsec_xform *ipsec_xfrm,
280 : : struct rte_crypto_sym_xform *crypto_xfrm,
281 : : struct rte_security_session *sess)
282 : : {
283 : : struct roc_cpt *roc_cpt;
284 : : int ret;
285 : :
286 : 0 : ret = cnxk_ipsec_xform_verify(ipsec_xfrm, crypto_xfrm);
287 [ # # ]: 0 : if (ret)
288 : : return ret;
289 : :
290 : 0 : roc_cpt = &vf->cpt;
291 : :
292 [ # # ]: 0 : if (ipsec_xfrm->direction == RTE_SECURITY_IPSEC_SA_DIR_INGRESS)
293 : 0 : return cn20k_ipsec_inb_sa_create(roc_cpt, &qp->lf, ipsec_xfrm, crypto_xfrm,
294 : : (struct cn20k_sec_session *)sess);
295 : : else
296 : 0 : return cn20k_ipsec_outb_sa_create(roc_cpt, &qp->lf, ipsec_xfrm, crypto_xfrm,
297 : : (struct cn20k_sec_session *)sess);
298 : : }
299 : :
300 : : int
301 : 0 : cn20k_sec_ipsec_session_destroy(struct cnxk_cpt_qp *qp, struct cn20k_sec_session *sess)
302 : : {
303 : : union roc_ow_ipsec_sa_word2 *w2;
304 : : struct cn20k_ipsec_sa *sa;
305 : : struct roc_cpt_lf *lf;
306 : : void *sa_dptr = NULL;
307 : : int ret;
308 : :
309 : 0 : lf = &qp->lf;
310 : :
311 : : sa = &sess->sa;
312 : :
313 [ # # ]: 0 : if (sa->sa_ptr == NULL)
314 : : return -EINVAL;
315 : :
316 : : /* Trigger CTX flush to write dirty data back to DRAM */
317 : 0 : roc_cpt_lf_ctx_flush(lf, sa->in_sa, false);
318 : :
319 : : ret = -1;
320 : :
321 [ # # ]: 0 : if (sess->ipsec.is_outbound) {
322 : 0 : sa_dptr = plt_zmalloc(sizeof(struct roc_ow_ipsec_outb_sa), 8);
323 [ # # ]: 0 : if (sa_dptr != NULL) {
324 : 0 : roc_ow_ipsec_outb_sa_init(sa_dptr);
325 : :
326 : 0 : ret = roc_cpt_ctx_write(lf, sa_dptr, sa->out_sa,
327 : : sizeof(struct roc_ow_ipsec_outb_sa));
328 : : }
329 : : } else {
330 : 0 : sa_dptr = plt_zmalloc(sizeof(struct roc_ow_ipsec_inb_sa), 8);
331 [ # # ]: 0 : if (sa_dptr != NULL) {
332 : 0 : roc_ow_ipsec_inb_sa_init(sa_dptr);
333 : :
334 : 0 : ret = roc_cpt_ctx_write(lf, sa_dptr, sa->in_sa,
335 : : sizeof(struct roc_ow_ipsec_inb_sa));
336 : : }
337 : : }
338 : :
339 : 0 : plt_free(sa_dptr);
340 : :
341 [ # # ]: 0 : if (ret) {
342 : : /* MC write_ctx failed. Attempt reload of CTX */
343 : :
344 : : /* Wait for 1 ms so that flush is complete */
345 : : rte_delay_ms(1);
346 : :
347 : 0 : w2 = (union roc_ow_ipsec_sa_word2 *)&sa->in_sa->w2;
348 : 0 : w2->s.valid = 0;
349 : :
350 : : rte_atomic_thread_fence(rte_memory_order_seq_cst);
351 : :
352 : : /* Trigger CTX reload to fetch new data from DRAM */
353 : 0 : roc_cpt_lf_ctx_reload(lf, sa->in_sa);
354 : : }
355 : :
356 : 0 : rte_free(sa->sa_ptr);
357 : :
358 : 0 : return 0;
359 : : }
360 : :
361 : : int
362 : 0 : cn20k_ipsec_stats_get(struct cnxk_cpt_qp *qp, struct cn20k_sec_session *sess,
363 : : struct rte_security_stats *stats)
364 : : {
365 : : struct roc_ow_ipsec_outb_sa *out_sa;
366 : : struct roc_ow_ipsec_inb_sa *in_sa;
367 : : struct cn20k_ipsec_sa *sa;
368 : :
369 : 0 : stats->protocol = RTE_SECURITY_PROTOCOL_IPSEC;
370 : : sa = &sess->sa;
371 : :
372 [ # # ]: 0 : if (sa->sa_ptr == NULL)
373 : : return -EINVAL;
374 : :
375 [ # # ]: 0 : if (sess->ipsec.is_outbound) {
376 : : out_sa = sa->out_sa;
377 : 0 : roc_cpt_lf_ctx_flush(&qp->lf, out_sa, false);
378 : 0 : stats->ipsec.opackets = out_sa->ctx.mib_pkts;
379 : 0 : stats->ipsec.obytes = out_sa->ctx.mib_octs;
380 : : } else {
381 : : in_sa = sa->in_sa;
382 : 0 : roc_cpt_lf_ctx_flush(&qp->lf, in_sa, false);
383 : 0 : stats->ipsec.ipackets = in_sa->ctx.mib_pkts;
384 : 0 : stats->ipsec.ibytes = in_sa->ctx.mib_octs;
385 : : }
386 : :
387 : : return 0;
388 : : }
389 : :
390 : : int
391 : 0 : cn20k_ipsec_session_update(struct cnxk_cpt_vf *vf, struct cnxk_cpt_qp *qp,
392 : : struct cn20k_sec_session *sess, struct rte_security_session_conf *conf)
393 : : {
394 : : struct roc_cpt *roc_cpt;
395 : : int ret;
396 : :
397 [ # # ]: 0 : if (conf->ipsec.direction == RTE_SECURITY_IPSEC_SA_DIR_INGRESS)
398 : : return -ENOTSUP;
399 : :
400 : 0 : ret = cnxk_ipsec_xform_verify(&conf->ipsec, conf->crypto_xform);
401 [ # # ]: 0 : if (ret)
402 : : return ret;
403 : :
404 : 0 : roc_cpt = &vf->cpt;
405 : :
406 : 0 : return cn20k_ipsec_outb_sa_create(roc_cpt, &qp->lf, &conf->ipsec, conf->crypto_xform,
407 : : (struct cn20k_sec_session *)sess);
408 : : }
|