Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause
2 : : * Copyright(C) 2021 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 "cn10k_ipsec.h"
14 : : #include "cnxk_cryptodev.h"
15 : : #include "cnxk_cryptodev_ops.h"
16 : : #include "cnxk_ipsec.h"
17 : : #include "cnxk_security.h"
18 : :
19 : : #include "roc_api.h"
20 : :
21 : : static uint64_t
22 : : ipsec_cpt_inst_w7_get(struct roc_cpt *roc_cpt, void *sa)
23 : : {
24 : : union cpt_inst_w7 w7;
25 : :
26 : 0 : w7.u64 = 0;
27 : 0 : w7.s.egrp = roc_cpt->eng_grp[CPT_ENG_TYPE_IE];
28 : 0 : w7.s.ctx_val = 1;
29 : 0 : w7.s.cptr = (uint64_t)sa;
30 : : rte_mb();
31 : :
32 : 0 : return w7.u64;
33 : : }
34 : :
35 : : static int
36 : 0 : cn10k_ipsec_outb_sa_create(struct roc_cpt *roc_cpt, struct roc_cpt_lf *lf,
37 : : struct rte_security_ipsec_xform *ipsec_xfrm,
38 : : struct rte_crypto_sym_xform *crypto_xfrm,
39 : : struct cn10k_sec_session *sec_sess)
40 : : {
41 : : union roc_ot_ipsec_outb_param1 param1;
42 : : struct roc_ot_ipsec_outb_sa *sa_dptr;
43 : : struct cnxk_ipsec_outb_rlens rlens;
44 : : struct cn10k_ipsec_sa *sa;
45 : : union cpt_inst_w4 inst_w4;
46 : : void *out_sa;
47 : : int ret = 0;
48 : :
49 : : sa = &sec_sess->sa;
50 : 0 : out_sa = &sa->out_sa;
51 : :
52 : : /* Allocate memory to be used as dptr for CPT ucode WRITE_SA op */
53 : 0 : sa_dptr = plt_zmalloc(sizeof(struct roc_ot_ipsec_outb_sa), 8);
54 [ # # ]: 0 : if (sa_dptr == NULL) {
55 : 0 : plt_err("Couldn't allocate memory for SA dptr");
56 : 0 : return -ENOMEM;
57 : : }
58 : :
59 : : /* Translate security parameters to SA */
60 : 0 : ret = cnxk_ot_ipsec_outb_sa_fill(sa_dptr, ipsec_xfrm, crypto_xfrm);
61 [ # # ]: 0 : if (ret) {
62 : 0 : plt_err("Could not fill outbound session parameters");
63 : 0 : goto sa_dptr_free;
64 : : }
65 : :
66 : 0 : sec_sess->inst.w7 = ipsec_cpt_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_OT_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_dptr_free;
88 : : }
89 : : #endif
90 : :
91 : 0 : sec_sess->is_outbound = true;
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_dptr_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_OT_MAJOR_OP_PROCESS_OUTBOUND_IPSEC | ROC_IE_OT_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_OT_SA_INNER_PKT_IP_CSUM_DISABLE;
110 : :
111 [ # # ]: 0 : if (ipsec_xfrm->options.ip_csum_enable) {
112 : 0 : param1.s.ip_csum_disable =
113 : : ROC_IE_OT_SA_INNER_PKT_IP_CSUM_ENABLE;
114 : : }
115 : :
116 : : /* Disable L4 checksum computation by default */
117 : 0 : param1.s.l4_csum_disable = ROC_IE_OT_SA_INNER_PKT_L4_CSUM_DISABLE;
118 : :
119 [ # # ]: 0 : if (ipsec_xfrm->options.l4_csum_enable) {
120 : 0 : param1.s.l4_csum_disable =
121 : : ROC_IE_OT_SA_INNER_PKT_L4_CSUM_ENABLE;
122 : : }
123 : :
124 : 0 : inst_w4.s.param1 = param1.u16;
125 : :
126 : 0 : sec_sess->inst.w4 = inst_w4.u64;
127 : :
128 [ # # ]: 0 : if (ipsec_xfrm->options.stats == 1) {
129 : : /* Enable mib counters */
130 : 0 : sa_dptr->w0.s.count_mib_bytes = 1;
131 : 0 : sa_dptr->w0.s.count_mib_pkts = 1;
132 : : }
133 : :
134 : : memset(out_sa, 0, sizeof(struct roc_ot_ipsec_outb_sa));
135 : :
136 : : /* Copy word0 from sa_dptr to populate ctx_push_sz ctx_size fields */
137 : : memcpy(out_sa, sa_dptr, 8);
138 : :
139 : : plt_atomic_thread_fence(__ATOMIC_SEQ_CST);
140 : :
141 : : /* Write session using microcode opcode */
142 : 0 : ret = roc_cpt_ctx_write(lf, sa_dptr, out_sa,
143 : : sizeof(struct roc_ot_ipsec_outb_sa));
144 [ # # ]: 0 : if (ret) {
145 : 0 : plt_err("Could not write outbound session to hardware");
146 : 0 : goto sa_dptr_free;
147 : : }
148 : :
149 : : /* Trigger CTX flush so that data is written back to DRAM */
150 : 0 : roc_cpt_lf_ctx_flush(lf, out_sa, false);
151 : :
152 : : plt_atomic_thread_fence(__ATOMIC_SEQ_CST);
153 : :
154 : 0 : sa_dptr_free:
155 : 0 : plt_free(sa_dptr);
156 : :
157 : 0 : return ret;
158 : : }
159 : :
160 : : static int
161 : 0 : cn10k_ipsec_inb_sa_create(struct roc_cpt *roc_cpt, struct roc_cpt_lf *lf,
162 : : struct rte_security_ipsec_xform *ipsec_xfrm,
163 : : struct rte_crypto_sym_xform *crypto_xfrm,
164 : : struct cn10k_sec_session *sec_sess)
165 : : {
166 : : union roc_ot_ipsec_inb_param1 param1;
167 : : struct roc_ot_ipsec_inb_sa *sa_dptr;
168 : : struct cn10k_ipsec_sa *sa;
169 : : union cpt_inst_w4 inst_w4;
170 : : void *in_sa;
171 : : int ret = 0;
172 : :
173 : : sa = &sec_sess->sa;
174 : 0 : in_sa = &sa->in_sa;
175 : :
176 : : /* Allocate memory to be used as dptr for CPT ucode WRITE_SA op */
177 : 0 : sa_dptr = plt_zmalloc(sizeof(struct roc_ot_ipsec_inb_sa), 8);
178 [ # # ]: 0 : if (sa_dptr == NULL) {
179 : 0 : plt_err("Couldn't allocate memory for SA dptr");
180 : 0 : return -ENOMEM;
181 : : }
182 : :
183 : : /* Translate security parameters to SA */
184 : 0 : ret = cnxk_ot_ipsec_inb_sa_fill(sa_dptr, ipsec_xfrm, crypto_xfrm,
185 : : false);
186 [ # # ]: 0 : if (ret) {
187 : 0 : plt_err("Could not fill inbound session parameters");
188 : 0 : goto sa_dptr_free;
189 : : }
190 : :
191 : 0 : sec_sess->is_outbound = false;
192 : 0 : sec_sess->inst.w7 = ipsec_cpt_inst_w7_get(roc_cpt, in_sa);
193 : :
194 : : /* pre-populate CPT INST word 4 */
195 : 0 : inst_w4.u64 = 0;
196 : 0 : inst_w4.s.opcode_major = ROC_IE_OT_MAJOR_OP_PROCESS_INBOUND_IPSEC | ROC_IE_OT_INPLACE_BIT;
197 : :
198 : 0 : param1.u16 = 0;
199 : :
200 : : /* Disable IP checksum verification by default */
201 : 0 : param1.s.ip_csum_disable = ROC_IE_OT_SA_INNER_PKT_IP_CSUM_DISABLE;
202 : :
203 : : /* Set the ip chksum flag in mbuf before enqueue.
204 : : * Reset the flag in post process in case of errors
205 : : */
206 [ # # ]: 0 : if (ipsec_xfrm->options.ip_csum_enable) {
207 : 0 : param1.s.ip_csum_disable = ROC_IE_OT_SA_INNER_PKT_IP_CSUM_ENABLE;
208 : 0 : sec_sess->ip_csum = RTE_MBUF_F_RX_IP_CKSUM_GOOD;
209 : : }
210 : :
211 : : /* Disable L4 checksum verification by default */
212 : 0 : param1.s.l4_csum_disable = ROC_IE_OT_SA_INNER_PKT_L4_CSUM_DISABLE;
213 : :
214 [ # # ]: 0 : if (ipsec_xfrm->options.l4_csum_enable) {
215 : 0 : param1.s.l4_csum_disable =
216 : : ROC_IE_OT_SA_INNER_PKT_L4_CSUM_ENABLE;
217 : : }
218 : :
219 : 0 : param1.s.esp_trailer_disable = 1;
220 : :
221 : 0 : inst_w4.s.param1 = param1.u16;
222 : :
223 : 0 : sec_sess->inst.w4 = inst_w4.u64;
224 : :
225 [ # # ]: 0 : if (ipsec_xfrm->options.stats == 1) {
226 : : /* Enable mib counters */
227 : 0 : sa_dptr->w0.s.count_mib_bytes = 1;
228 : 0 : sa_dptr->w0.s.count_mib_pkts = 1;
229 : : }
230 : :
231 : : memset(in_sa, 0, sizeof(struct roc_ot_ipsec_inb_sa));
232 : :
233 : : /* Copy word0 from sa_dptr to populate ctx_push_sz ctx_size fields */
234 : : memcpy(in_sa, sa_dptr, 8);
235 : :
236 : : plt_atomic_thread_fence(__ATOMIC_SEQ_CST);
237 : :
238 : : /* Write session using microcode opcode */
239 : 0 : ret = roc_cpt_ctx_write(lf, sa_dptr, in_sa,
240 : : sizeof(struct roc_ot_ipsec_inb_sa));
241 [ # # ]: 0 : if (ret) {
242 : 0 : plt_err("Could not write inbound session to hardware");
243 : 0 : goto sa_dptr_free;
244 : : }
245 : :
246 : : /* Trigger CTX flush so that data is written back to DRAM */
247 : 0 : roc_cpt_lf_ctx_flush(lf, in_sa, true);
248 : :
249 : : plt_atomic_thread_fence(__ATOMIC_SEQ_CST);
250 : :
251 : 0 : sa_dptr_free:
252 : 0 : plt_free(sa_dptr);
253 : :
254 : 0 : return ret;
255 : : }
256 : :
257 : : static int
258 : 0 : cn10k_ipsec_session_create(void *dev,
259 : : struct rte_security_ipsec_xform *ipsec_xfrm,
260 : : struct rte_crypto_sym_xform *crypto_xfrm,
261 : : struct rte_security_session *sess)
262 : : {
263 : : struct rte_cryptodev *crypto_dev = dev;
264 : : struct roc_cpt *roc_cpt;
265 : : struct cnxk_cpt_vf *vf;
266 : : struct cnxk_cpt_qp *qp;
267 : : int ret;
268 : :
269 : 0 : qp = crypto_dev->data->queue_pairs[0];
270 [ # # ]: 0 : if (qp == NULL) {
271 : 0 : plt_err("Setup cpt queue pair before creating security session");
272 : 0 : return -EPERM;
273 : : }
274 : :
275 : 0 : ret = cnxk_ipsec_xform_verify(ipsec_xfrm, crypto_xfrm);
276 [ # # ]: 0 : if (ret)
277 : : return ret;
278 : :
279 : 0 : vf = crypto_dev->data->dev_private;
280 : 0 : roc_cpt = &vf->cpt;
281 : :
282 [ # # ]: 0 : if (ipsec_xfrm->direction == RTE_SECURITY_IPSEC_SA_DIR_INGRESS)
283 : 0 : return cn10k_ipsec_inb_sa_create(roc_cpt, &qp->lf, ipsec_xfrm, crypto_xfrm,
284 : : (struct cn10k_sec_session *)sess);
285 : : else
286 : 0 : return cn10k_ipsec_outb_sa_create(roc_cpt, &qp->lf, ipsec_xfrm, crypto_xfrm,
287 : : (struct cn10k_sec_session *)sess);
288 : : }
289 : :
290 : : static int
291 : 0 : cn10k_sec_session_create(void *device, struct rte_security_session_conf *conf,
292 : : struct rte_security_session *sess)
293 : : {
294 [ # # ]: 0 : if (conf->action_type != RTE_SECURITY_ACTION_TYPE_LOOKASIDE_PROTOCOL)
295 : : return -EINVAL;
296 : :
297 [ # # ]: 0 : if (conf->protocol != RTE_SECURITY_PROTOCOL_IPSEC)
298 : : return -ENOTSUP;
299 : :
300 : 0 : return cn10k_ipsec_session_create(device, &conf->ipsec,
301 : : conf->crypto_xform, sess);
302 : : }
303 : :
304 : : static int
305 : 0 : cn10k_sec_session_destroy(void *dev, struct rte_security_session *sec_sess)
306 : : {
307 : : struct rte_cryptodev *crypto_dev = dev;
308 : : union roc_ot_ipsec_sa_word2 *w2;
309 : : struct cn10k_sec_session *sess;
310 : : struct cn10k_ipsec_sa *sa;
311 : : struct cnxk_cpt_qp *qp;
312 : : struct roc_cpt_lf *lf;
313 : : void *sa_dptr = NULL;
314 : : int ret;
315 : :
316 [ # # ]: 0 : if (unlikely(sec_sess == NULL))
317 : : return -EINVAL;
318 : :
319 : : sess = (struct cn10k_sec_session *)sec_sess;
320 : :
321 : 0 : qp = crypto_dev->data->queue_pairs[0];
322 [ # # ]: 0 : if (unlikely(qp == NULL))
323 : : return -ENOTSUP;
324 : :
325 : 0 : lf = &qp->lf;
326 : :
327 : : sa = &sess->sa;
328 : :
329 : : /* Trigger CTX flush to write dirty data back to DRAM */
330 : 0 : roc_cpt_lf_ctx_flush(lf, &sa->in_sa, false);
331 : :
332 : : ret = -1;
333 : :
334 [ # # ]: 0 : if (sess->is_outbound) {
335 : 0 : sa_dptr = plt_zmalloc(sizeof(struct roc_ot_ipsec_outb_sa), 8);
336 [ # # ]: 0 : if (sa_dptr != NULL) {
337 : 0 : roc_ot_ipsec_outb_sa_init(sa_dptr);
338 : :
339 : 0 : ret = roc_cpt_ctx_write(
340 : 0 : lf, sa_dptr, &sa->out_sa,
341 : : sizeof(struct roc_ot_ipsec_outb_sa));
342 : : }
343 : : } else {
344 : 0 : sa_dptr = plt_zmalloc(sizeof(struct roc_ot_ipsec_inb_sa), 8);
345 [ # # ]: 0 : if (sa_dptr != NULL) {
346 : 0 : roc_ot_ipsec_inb_sa_init(sa_dptr, false);
347 : :
348 : 0 : ret = roc_cpt_ctx_write(
349 : : lf, sa_dptr, &sa->in_sa,
350 : : sizeof(struct roc_ot_ipsec_inb_sa));
351 : : }
352 : : }
353 : :
354 : 0 : plt_free(sa_dptr);
355 : :
356 [ # # ]: 0 : if (ret) {
357 : : /* MC write_ctx failed. Attempt reload of CTX */
358 : :
359 : : /* Wait for 1 ms so that flush is complete */
360 : : rte_delay_ms(1);
361 : :
362 : : w2 = (union roc_ot_ipsec_sa_word2 *)&sa->in_sa.w2;
363 : 0 : w2->s.valid = 0;
364 : :
365 : : plt_atomic_thread_fence(__ATOMIC_SEQ_CST);
366 : :
367 : : /* Trigger CTX reload to fetch new data from DRAM */
368 : 0 : roc_cpt_lf_ctx_reload(lf, &sa->in_sa);
369 : : }
370 : :
371 : : return 0;
372 : : }
373 : :
374 : : static unsigned int
375 : 0 : cn10k_sec_session_get_size(void *device __rte_unused)
376 : : {
377 : 0 : return sizeof(struct cn10k_sec_session) - sizeof(struct rte_security_session);
378 : : }
379 : :
380 : : static int
381 : 0 : cn10k_sec_session_stats_get(void *device, struct rte_security_session *sess,
382 : : struct rte_security_stats *stats)
383 : : {
384 : : struct rte_cryptodev *crypto_dev = device;
385 : : struct roc_ot_ipsec_outb_sa *out_sa;
386 : : struct roc_ot_ipsec_inb_sa *in_sa;
387 : : struct cn10k_sec_session *priv;
388 : : struct cn10k_ipsec_sa *sa;
389 : : struct cnxk_cpt_qp *qp;
390 : :
391 [ # # ]: 0 : if (unlikely(sess == NULL))
392 : : return -EINVAL;
393 : :
394 : : priv = (struct cn10k_sec_session *)sess;
395 : :
396 : 0 : qp = crypto_dev->data->queue_pairs[0];
397 [ # # ]: 0 : if (qp == NULL)
398 : : return -EINVAL;
399 : :
400 : 0 : stats->protocol = RTE_SECURITY_PROTOCOL_IPSEC;
401 : : sa = &priv->sa;
402 : :
403 [ # # ]: 0 : if (priv->is_outbound) {
404 : 0 : out_sa = &sa->out_sa;
405 : 0 : roc_cpt_lf_ctx_flush(&qp->lf, out_sa, false);
406 : : rte_delay_ms(1);
407 : 0 : stats->ipsec.opackets = out_sa->ctx.mib_pkts;
408 : 0 : stats->ipsec.obytes = out_sa->ctx.mib_octs;
409 : : } else {
410 : 0 : in_sa = &sa->in_sa;
411 : 0 : roc_cpt_lf_ctx_flush(&qp->lf, in_sa, false);
412 : : rte_delay_ms(1);
413 : 0 : stats->ipsec.ipackets = in_sa->ctx.mib_pkts;
414 : 0 : stats->ipsec.ibytes = in_sa->ctx.mib_octs;
415 : : }
416 : :
417 : : return 0;
418 : : }
419 : :
420 : : static int
421 : 0 : cn10k_sec_session_update(void *device, struct rte_security_session *sess,
422 : : struct rte_security_session_conf *conf)
423 : : {
424 : : struct rte_cryptodev *crypto_dev = device;
425 : : struct roc_cpt *roc_cpt;
426 : : struct cnxk_cpt_qp *qp;
427 : : struct cnxk_cpt_vf *vf;
428 : : int ret;
429 : :
430 [ # # ]: 0 : if (sess == NULL)
431 : : return -EINVAL;
432 : :
433 : 0 : qp = crypto_dev->data->queue_pairs[0];
434 [ # # ]: 0 : if (qp == NULL)
435 : : return -EINVAL;
436 : :
437 [ # # ]: 0 : if (conf->ipsec.direction == RTE_SECURITY_IPSEC_SA_DIR_INGRESS)
438 : : return -ENOTSUP;
439 : :
440 : 0 : ret = cnxk_ipsec_xform_verify(&conf->ipsec, conf->crypto_xform);
441 [ # # ]: 0 : if (ret)
442 : : return ret;
443 : :
444 : 0 : vf = crypto_dev->data->dev_private;
445 : 0 : roc_cpt = &vf->cpt;
446 : :
447 : 0 : return cn10k_ipsec_outb_sa_create(roc_cpt, &qp->lf, &conf->ipsec, conf->crypto_xform,
448 : : (struct cn10k_sec_session *)sess);
449 : : }
450 : :
451 : : /* Update platform specific security ops */
452 : : void
453 : 0 : cn10k_sec_ops_override(void)
454 : : {
455 : : /* Update platform specific ops */
456 : 0 : cnxk_sec_ops.session_create = cn10k_sec_session_create;
457 : 0 : cnxk_sec_ops.session_destroy = cn10k_sec_session_destroy;
458 : 0 : cnxk_sec_ops.session_get_size = cn10k_sec_session_get_size;
459 : 0 : cnxk_sec_ops.session_stats_get = cn10k_sec_session_stats_get;
460 : 0 : cnxk_sec_ops.session_update = cn10k_sec_session_update;
461 : 0 : }
|