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