DPDK logo

Elixir Cross Referencer

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
/* SPDX-License-Identifier: BSD-3-Clause
 * Copyright (C) 2020 Marvell International Ltd.
 */

#include <rte_cryptodev.h>
#include <rte_esp.h>
#include <rte_ethdev.h>
#include <rte_ip.h>
#include <rte_malloc.h>
#include <rte_security.h>
#include <rte_security_driver.h>
#include <rte_udp.h>

#include "otx2_cryptodev.h"
#include "otx2_cryptodev_capabilities.h"
#include "otx2_cryptodev_hw_access.h"
#include "otx2_cryptodev_ops.h"
#include "otx2_cryptodev_sec.h"
#include "otx2_security.h"

static int
ipsec_lp_len_precalc(struct rte_security_ipsec_xform *ipsec,
		struct rte_crypto_sym_xform *xform,
		struct otx2_sec_session_ipsec_lp *lp)
{
	struct rte_crypto_sym_xform *cipher_xform, *auth_xform;

	lp->partial_len = sizeof(struct rte_ipv4_hdr);

	if (ipsec->proto == RTE_SECURITY_IPSEC_SA_PROTO_ESP) {
		lp->partial_len += sizeof(struct rte_esp_hdr);
		lp->roundup_len = sizeof(struct rte_esp_tail);
	} else if (ipsec->proto == RTE_SECURITY_IPSEC_SA_PROTO_AH) {
		lp->partial_len += OTX2_SEC_AH_HDR_LEN;
	} else {
		return -EINVAL;
	}

	if (ipsec->options.udp_encap)
		lp->partial_len += sizeof(struct rte_udp_hdr);

	if (xform->type == RTE_CRYPTO_SYM_XFORM_AEAD) {
		if (xform->aead.algo == RTE_CRYPTO_AEAD_AES_GCM) {
			lp->partial_len += OTX2_SEC_AES_GCM_IV_LEN;
			lp->partial_len += OTX2_SEC_AES_GCM_MAC_LEN;
			lp->roundup_byte = OTX2_SEC_AES_GCM_ROUNDUP_BYTE_LEN;
			return 0;
		} else {
			return -EINVAL;
		}
	}

	if (ipsec->direction == RTE_SECURITY_IPSEC_SA_DIR_EGRESS) {
		cipher_xform = xform;
		auth_xform = xform->next;
	} else if (ipsec->direction == RTE_SECURITY_IPSEC_SA_DIR_INGRESS) {
		auth_xform = xform;
		cipher_xform = xform->next;
	} else {
		return -EINVAL;
	}

	if (cipher_xform->cipher.algo == RTE_CRYPTO_CIPHER_AES_CBC) {
		lp->partial_len += OTX2_SEC_AES_CBC_IV_LEN;
		lp->roundup_byte = OTX2_SEC_AES_CBC_ROUNDUP_BYTE_LEN;
	} else {
		return -EINVAL;
	}

	if (auth_xform->auth.algo == RTE_CRYPTO_AUTH_SHA1_HMAC)
		lp->partial_len += OTX2_SEC_SHA1_HMAC_LEN;
	else
		return -EINVAL;

	return 0;
}

static int
otx2_cpt_enq_sa_write(struct otx2_sec_session_ipsec_lp *lp,
		      struct otx2_cpt_qp *qptr, uint8_t opcode)
{
	uint64_t lmt_status, time_out;
	void *lmtline = qptr->lmtline;
	struct otx2_cpt_inst_s inst;
	struct otx2_cpt_res *res;
	uint64_t *mdata;
	int ret = 0;

	if (unlikely(rte_mempool_get(qptr->meta_info.pool,
				     (void **)&mdata) < 0))
		return -ENOMEM;

	res = (struct otx2_cpt_res *)RTE_PTR_ALIGN(mdata, 16);
	res->compcode = CPT_9X_COMP_E_NOTDONE;

	inst.opcode = opcode | (lp->ctx_len << 8);
	inst.param1 = 0;
	inst.param2 = 0;
	inst.dlen = lp->ctx_len << 3;
	inst.dptr = rte_mempool_virt2iova(lp);
	inst.rptr = 0;
	inst.cptr = rte_mempool_virt2iova(lp);
	inst.egrp  = OTX2_CPT_EGRP_SE;

	inst.u64[0] = 0;
	inst.u64[2] = 0;
	inst.u64[3] = 0;
	inst.res_addr = rte_mempool_virt2iova(res);

	rte_cio_wmb();

	do {
		/* Copy CPT command to LMTLINE */
		otx2_lmt_mov(lmtline, &inst, 2);
		lmt_status = otx2_lmt_submit(qptr->lf_nq_reg);
	} while (lmt_status == 0);

	time_out = rte_get_timer_cycles() +
			DEFAULT_COMMAND_TIMEOUT * rte_get_timer_hz();

	while (res->compcode == CPT_9X_COMP_E_NOTDONE) {
		if (rte_get_timer_cycles() > time_out) {
			rte_mempool_put(qptr->meta_info.pool, mdata);
			otx2_err("Request timed out");
			return -ETIMEDOUT;
		}
	    rte_cio_rmb();
	}

	if (unlikely(res->compcode != CPT_9X_COMP_E_GOOD)) {
		ret = res->compcode;
		switch (ret) {
		case CPT_9X_COMP_E_INSTERR:
			otx2_err("Request failed with instruction error");
			break;
		case CPT_9X_COMP_E_FAULT:
			otx2_err("Request failed with DMA fault");
			break;
		case CPT_9X_COMP_E_HWERR:
			otx2_err("Request failed with hardware error");
			break;
		default:
			otx2_err("Request failed with unknown hardware "
				 "completion code : 0x%x", ret);
		}
		goto mempool_put;
	}

	if (unlikely(res->uc_compcode != OTX2_IPSEC_PO_CC_SUCCESS)) {
		ret = res->uc_compcode;
		switch (ret) {
		case OTX2_IPSEC_PO_CC_AUTH_UNSUPPORTED:
			otx2_err("Invalid auth type");
			break;
		case OTX2_IPSEC_PO_CC_ENCRYPT_UNSUPPORTED:
			otx2_err("Invalid encrypt type");
			break;
		default:
			otx2_err("Request failed with unknown microcode "
				 "completion code : 0x%x", ret);
		}
	}

mempool_put:
	rte_mempool_put(qptr->meta_info.pool, mdata);
	return ret;
}

static void
set_session_misc_attributes(struct otx2_sec_session_ipsec_lp *sess,
			    struct rte_crypto_sym_xform *crypto_xform,
			    struct rte_crypto_sym_xform *auth_xform,
			    struct rte_crypto_sym_xform *cipher_xform)
{
	if (crypto_xform->type == RTE_CRYPTO_SYM_XFORM_AEAD) {
		sess->iv_offset = crypto_xform->aead.iv.offset;
		sess->iv_length = crypto_xform->aead.iv.length;
		sess->aad_length = crypto_xform->aead.aad_length;
		sess->mac_len = crypto_xform->aead.digest_length;
	} else {
		sess->iv_offset = cipher_xform->cipher.iv.offset;
		sess->iv_length = cipher_xform->cipher.iv.length;
		sess->auth_iv_offset = auth_xform->auth.iv.offset;
		sess->auth_iv_length = auth_xform->auth.iv.length;
		sess->mac_len = auth_xform->auth.digest_length;
	}

	sess->ucmd_param1 = OTX2_IPSEC_PO_PER_PKT_IV;
	sess->ucmd_param2 = 0;
}

static int
crypto_sec_ipsec_outb_session_create(struct rte_cryptodev *crypto_dev,
				     struct rte_security_ipsec_xform *ipsec,
				     struct rte_crypto_sym_xform *crypto_xform,
				     struct rte_security_session *sec_sess)
{
	struct rte_crypto_sym_xform *auth_xform, *cipher_xform;
	const uint8_t *cipher_key, *auth_key;
	struct otx2_sec_session_ipsec_lp *lp;
	struct otx2_ipsec_po_sa_ctl *ctl;
	int cipher_key_len, auth_key_len;
	struct otx2_ipsec_po_out_sa *sa;
	struct otx2_sec_session *sess;
	struct otx2_cpt_inst_s inst;
	struct rte_ipv4_hdr *ip;
	int ret;

	sess = get_sec_session_private_data(sec_sess);
	lp = &sess->ipsec.lp;

	sa = &lp->out_sa;
	ctl = &sa->ctl;
	if (ctl->valid) {
		otx2_err("SA already registered");
		return -EINVAL;
	}

	memset(sa, 0, sizeof(struct otx2_ipsec_po_out_sa));

	/* Initialize lookaside ipsec private data */
	lp->ip_id = 0;
	lp->seq_lo = 1;
	lp->seq_hi = 0;

	ret = ipsec_po_sa_ctl_set(ipsec, crypto_xform, ctl);
	if (ret)
		return ret;

	ret = ipsec_lp_len_precalc(ipsec, crypto_xform, lp);
	if (ret)
		return ret;

	memcpy(sa->iv.gcm.nonce, &ipsec->salt, 4);

	if (ipsec->options.udp_encap) {
		sa->udp_src = 4500;
		sa->udp_dst = 4500;
	}

	if (ipsec->mode == RTE_SECURITY_IPSEC_SA_MODE_TUNNEL) {
		/* Start ip id from 1 */
		lp->ip_id = 1;

		if (ipsec->tunnel.type == RTE_SECURITY_IPSEC_TUNNEL_IPV4) {
			ip = &sa->template.ipv4_hdr;
			ip->version_ihl = RTE_IPV4_VHL_DEF;
			ip->next_proto_id = IPPROTO_ESP;
			ip->time_to_live = ipsec->tunnel.ipv4.ttl;
			ip->type_of_service |= (ipsec->tunnel.ipv4.dscp << 2);
			if (ipsec->tunnel.ipv4.df)
				ip->fragment_offset = BIT(14);
			memcpy(&ip->src_addr, &ipsec->tunnel.ipv4.src_ip,
				sizeof(struct in_addr));
			memcpy(&ip->dst_addr, &ipsec->tunnel.ipv4.dst_ip,
				sizeof(struct in_addr));
		} else {
			return -EINVAL;
		}
	} else {
		return -EINVAL;
	}

	cipher_xform = crypto_xform;
	auth_xform = crypto_xform->next;

	cipher_key_len = 0;
	auth_key_len = 0;

	if (crypto_xform->type == RTE_CRYPTO_SYM_XFORM_AEAD) {
		cipher_key = crypto_xform->aead.key.data;
		cipher_key_len = crypto_xform->aead.key.length;

		lp->ctx_len = sizeof(struct otx2_ipsec_po_out_sa);
		lp->ctx_len >>= 3;
		RTE_ASSERT(lp->ctx_len == OTX2_IPSEC_PO_AES_GCM_OUTB_CTX_LEN);
	} else {
		cipher_key = cipher_xform->cipher.key.data;
		cipher_key_len = cipher_xform->cipher.key.length;
		auth_key = auth_xform->auth.key.data;
		auth_key_len = auth_xform->auth.key.length;

		/* TODO: check the ctx len for supporting ALGO */
		lp->ctx_len = sizeof(struct otx2_ipsec_po_out_sa) >> 3;
		RTE_ASSERT(lp->ctx_len == OTX2_IPSEC_PO_MAX_OUTB_CTX_LEN);
	}

	if (cipher_key_len != 0)
		memcpy(sa->cipher_key, cipher_key, cipher_key_len);
	else
		return -EINVAL;

	/* Use OPAD & IPAD */
	RTE_SET_USED(auth_key);
	RTE_SET_USED(auth_key_len);

	inst.u64[7] = 0;
	inst.egrp = OTX2_CPT_EGRP_SE;
	inst.cptr = rte_mempool_virt2iova(sa);

	lp->ucmd_w3 = inst.u64[7];
	lp->ucmd_opcode = (lp->ctx_len << 8) |
				(OTX2_IPSEC_PO_PROCESS_IPSEC_OUTB);

	set_session_misc_attributes(lp, crypto_xform,
				    auth_xform, cipher_xform);

	return otx2_cpt_enq_sa_write(lp, crypto_dev->data->queue_pairs[0],
				     OTX2_IPSEC_PO_WRITE_IPSEC_OUTB);
}

static int
crypto_sec_ipsec_inb_session_create(struct rte_cryptodev *crypto_dev,
				    struct rte_security_ipsec_xform *ipsec,
				    struct rte_crypto_sym_xform *crypto_xform,
				    struct rte_security_session *sec_sess)
{
	struct rte_crypto_sym_xform *auth_xform, *cipher_xform;
	struct otx2_sec_session_ipsec_lp *lp;
	struct otx2_ipsec_po_sa_ctl *ctl;
	const uint8_t *cipher_key, *auth_key;
	int cipher_key_len, auth_key_len;
	struct otx2_ipsec_po_in_sa *sa;
	struct otx2_sec_session *sess;
	struct otx2_cpt_inst_s inst;
	int ret;

	sess = get_sec_session_private_data(sec_sess);
	lp = &sess->ipsec.lp;

	sa = &lp->in_sa;
	ctl = &sa->ctl;

	if (ctl->valid) {
		otx2_err("SA already registered");
		return -EINVAL;
	}

	memset(sa, 0, sizeof(struct otx2_ipsec_po_in_sa));

	ret = ipsec_po_sa_ctl_set(ipsec, crypto_xform, ctl);
	if (ret)
		return ret;

	auth_xform = crypto_xform;
	cipher_xform = crypto_xform->next;

	cipher_key_len = 0;
	auth_key_len = 0;

	if (crypto_xform->type == RTE_CRYPTO_SYM_XFORM_AEAD) {
		if (crypto_xform->aead.algo == RTE_CRYPTO_AEAD_AES_GCM)
			memcpy(sa->iv.gcm.nonce, &ipsec->salt, 4);
		cipher_key = crypto_xform->aead.key.data;
		cipher_key_len = crypto_xform->aead.key.length;

		lp->ctx_len = offsetof(struct otx2_ipsec_po_in_sa,
					    aes_gcm.hmac_key[0]) >> 3;
		RTE_ASSERT(lp->ctx_len == OTX2_IPSEC_PO_AES_GCM_INB_CTX_LEN);
	} else {
		cipher_key = cipher_xform->cipher.key.data;
		cipher_key_len = cipher_xform->cipher.key.length;
		auth_key = auth_xform->auth.key.data;
		auth_key_len = auth_xform->auth.key.length;

		/* TODO: check the ctx len for supporting ALGO */
		lp->ctx_len = sizeof(struct otx2_ipsec_po_in_sa) >> 2;
		RTE_ASSERT(lp->ctx_len == OTX2_IPSEC_PO_MAX_INB_CTX_LEN);
	}

	if (cipher_key_len != 0)
		memcpy(sa->cipher_key, cipher_key, cipher_key_len);
	else
		return -EINVAL;

	/* Use OPAD & IPAD */
	RTE_SET_USED(auth_key);
	RTE_SET_USED(auth_key_len);

	inst.u64[7] = 0;
	inst.egrp = OTX2_CPT_EGRP_SE;
	inst.cptr = rte_mempool_virt2iova(sa);

	lp->ucmd_w3 = inst.u64[7];
	lp->ucmd_opcode = (lp->ctx_len << 8) |
				(OTX2_IPSEC_PO_PROCESS_IPSEC_INB);

	set_session_misc_attributes(lp, crypto_xform,
				    auth_xform, cipher_xform);

	return otx2_cpt_enq_sa_write(lp, crypto_dev->data->queue_pairs[0],
				     OTX2_IPSEC_PO_WRITE_IPSEC_INB);
}

static int
crypto_sec_ipsec_session_create(struct rte_cryptodev *crypto_dev,
				struct rte_security_ipsec_xform *ipsec,
				struct rte_crypto_sym_xform *crypto_xform,
				struct rte_security_session *sess)
{
	int ret;

	if (crypto_dev->data->queue_pairs[0] == NULL) {
		otx2_err("Setup cpt queue pair before creating sec session");
		return -EPERM;
	}

	ret = ipsec_po_xform_verify(ipsec, crypto_xform);
	if (ret)
		return ret;

	if (ipsec->direction == RTE_SECURITY_IPSEC_SA_DIR_INGRESS)
		return crypto_sec_ipsec_inb_session_create(crypto_dev, ipsec,
							   crypto_xform, sess);
	else
		return crypto_sec_ipsec_outb_session_create(crypto_dev, ipsec,
							    crypto_xform, sess);
}

static int
otx2_crypto_sec_session_create(void *device,
			       struct rte_security_session_conf *conf,
			       struct rte_security_session *sess,
			       struct rte_mempool *mempool)
{
	struct otx2_sec_session *priv;
	int ret;

	if (conf->action_type != RTE_SECURITY_ACTION_TYPE_LOOKASIDE_PROTOCOL)
		return -ENOTSUP;

	if (rte_mempool_get(mempool, (void **)&priv)) {
		otx2_err("Could not allocate security session private data");
		return -ENOMEM;
	}

	set_sec_session_private_data(sess, priv);

	priv->userdata = conf->userdata;

	if (conf->protocol == RTE_SECURITY_PROTOCOL_IPSEC)
		ret = crypto_sec_ipsec_session_create(device, &conf->ipsec,
						      conf->crypto_xform,
						      sess);
	else
		ret = -ENOTSUP;

	if (ret)
		goto mempool_put;

	return 0;

mempool_put:
	rte_mempool_put(mempool, priv);
	set_sec_session_private_data(sess, NULL);
	return ret;
}

static int
otx2_crypto_sec_session_destroy(void *device __rte_unused,
				struct rte_security_session *sess)
{
	struct otx2_sec_session *priv;
	struct rte_mempool *sess_mp;

	priv = get_sec_session_private_data(sess);

	if (priv == NULL)
		return 0;

	sess_mp = rte_mempool_from_obj(priv);

	set_sec_session_private_data(sess, NULL);
	rte_mempool_put(sess_mp, priv);

	return 0;
}

static unsigned int
otx2_crypto_sec_session_get_size(void *device __rte_unused)
{
	return sizeof(struct otx2_sec_session);
}

static int
otx2_crypto_sec_set_pkt_mdata(void *device __rte_unused,
			      struct rte_security_session *session,
			      struct rte_mbuf *m, void *params __rte_unused)
{
	/* Set security session as the pkt metadata */
	m->udata64 = (uint64_t)session;

	return 0;
}

static int
otx2_crypto_sec_get_userdata(void *device __rte_unused, uint64_t md,
			     void **userdata)
{
	/* Retrieve userdata  */
	*userdata = (void *)md;

	return 0;
}

static struct rte_security_ops otx2_crypto_sec_ops = {
	.session_create		= otx2_crypto_sec_session_create,
	.session_destroy	= otx2_crypto_sec_session_destroy,
	.session_get_size	= otx2_crypto_sec_session_get_size,
	.set_pkt_metadata	= otx2_crypto_sec_set_pkt_mdata,
	.get_userdata		= otx2_crypto_sec_get_userdata,
	.capabilities_get	= otx2_crypto_sec_capabilities_get
};

int
otx2_crypto_sec_ctx_create(struct rte_cryptodev *cdev)
{
	struct rte_security_ctx *ctx;

	ctx = rte_malloc("otx2_cpt_dev_sec_ctx",
			 sizeof(struct rte_security_ctx), 0);

	if (ctx == NULL)
		return -ENOMEM;

	/* Populate ctx */
	ctx->device = cdev;
	ctx->ops = &otx2_crypto_sec_ops;
	ctx->sess_cnt = 0;

	cdev->security_ctx = ctx;

	return 0;
}

void
otx2_crypto_sec_ctx_destroy(struct rte_cryptodev *cdev)
{
	rte_free(cdev->security_ctx);
}