1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright (C) 2020 Marvell International Ltd.
5 #include <rte_cryptodev.h>
7 #include <rte_ethdev.h>
8 #include <rte_eventdev.h>
10 #include <rte_malloc.h>
11 #include <rte_memzone.h>
12 #include <rte_security.h>
13 #include <rte_security_driver.h>
16 #include "otx2_common.h"
17 #include "otx2_cryptodev_qp.h"
18 #include "otx2_ethdev.h"
19 #include "otx2_ethdev_sec.h"
20 #include "otx2_ipsec_fp.h"
21 #include "otx2_sec_idev.h"
24 #define AES_GCM_IV_LEN 8
25 #define AES_GCM_MAC_LEN 16
26 #define AES_CBC_IV_LEN 16
27 #define SHA1_HMAC_LEN 12
29 #define AES_GCM_ROUNDUP_BYTE_LEN 4
30 #define AES_CBC_ROUNDUP_BYTE_LEN 16
32 struct eth_sec_tag_const {
36 uint32_t rsvd_11_0 : 12;
38 uint32_t event_type : 4;
39 uint32_t rsvd_31_24 : 8;
45 static struct rte_cryptodev_capabilities otx2_eth_sec_crypto_caps[] = {
47 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
49 .xform_type = RTE_CRYPTO_SYM_XFORM_AEAD,
51 .algo = RTE_CRYPTO_AEAD_AES_GCM,
77 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
79 .xform_type = RTE_CRYPTO_SYM_XFORM_CIPHER,
81 .algo = RTE_CRYPTO_CIPHER_AES_CBC,
97 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
99 .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH,
101 .algo = RTE_CRYPTO_AUTH_SHA1_HMAC,
116 RTE_CRYPTODEV_END_OF_CAPABILITIES_LIST()
119 static const struct rte_security_capability otx2_eth_sec_capabilities[] = {
120 { /* IPsec Inline Protocol ESP Tunnel Ingress */
121 .action = RTE_SECURITY_ACTION_TYPE_INLINE_PROTOCOL,
122 .protocol = RTE_SECURITY_PROTOCOL_IPSEC,
124 .proto = RTE_SECURITY_IPSEC_SA_PROTO_ESP,
125 .mode = RTE_SECURITY_IPSEC_SA_MODE_TUNNEL,
126 .direction = RTE_SECURITY_IPSEC_SA_DIR_INGRESS,
129 .crypto_capabilities = otx2_eth_sec_crypto_caps,
130 .ol_flags = RTE_SECURITY_TX_OLOAD_NEED_MDATA
132 { /* IPsec Inline Protocol ESP Tunnel Egress */
133 .action = RTE_SECURITY_ACTION_TYPE_INLINE_PROTOCOL,
134 .protocol = RTE_SECURITY_PROTOCOL_IPSEC,
136 .proto = RTE_SECURITY_IPSEC_SA_PROTO_ESP,
137 .mode = RTE_SECURITY_IPSEC_SA_MODE_TUNNEL,
138 .direction = RTE_SECURITY_IPSEC_SA_DIR_EGRESS,
141 .crypto_capabilities = otx2_eth_sec_crypto_caps,
142 .ol_flags = RTE_SECURITY_TX_OLOAD_NEED_MDATA
145 .action = RTE_SECURITY_ACTION_TYPE_NONE
150 lookup_mem_sa_tbl_clear(struct rte_eth_dev *eth_dev)
152 static const char name[] = OTX2_NIX_FASTPATH_LOOKUP_MEM;
153 uint16_t port = eth_dev->data->port_id;
154 const struct rte_memzone *mz;
158 mz = rte_memzone_lookup(name);
164 sa_tbl = (uint64_t **)RTE_PTR_ADD(mem, OTX2_NIX_SA_TBL_START);
165 if (sa_tbl[port] == NULL)
168 rte_free(sa_tbl[port]);
173 lookup_mem_sa_index_update(struct rte_eth_dev *eth_dev, int spi, void *sa)
175 static const char name[] = OTX2_NIX_FASTPATH_LOOKUP_MEM;
176 struct otx2_eth_dev *dev = otx2_eth_pmd_priv(eth_dev);
177 uint16_t port = eth_dev->data->port_id;
178 const struct rte_memzone *mz;
182 mz = rte_memzone_lookup(name);
184 otx2_err("Could not find fastpath lookup table");
190 sa_tbl = (uint64_t **)RTE_PTR_ADD(mem, OTX2_NIX_SA_TBL_START);
192 if (sa_tbl[port] == NULL) {
193 sa_tbl[port] = rte_malloc(NULL, dev->ipsec_in_max_spi *
194 sizeof(uint64_t), 0);
197 sa_tbl[port][spi] = (uint64_t)sa;
203 in_sa_mz_name_get(char *name, int size, uint16_t port)
205 snprintf(name, size, "otx2_ipsec_in_sadb_%u", port);
208 static struct otx2_ipsec_fp_in_sa *
209 in_sa_get(uint16_t port, int sa_index)
211 char name[RTE_MEMZONE_NAMESIZE];
212 struct otx2_ipsec_fp_in_sa *sa;
213 const struct rte_memzone *mz;
215 in_sa_mz_name_get(name, RTE_MEMZONE_NAMESIZE, port);
216 mz = rte_memzone_lookup(name);
218 otx2_err("Could not get the memzone reserved for IN SA DB");
224 return sa + sa_index;
228 ipsec_sa_const_set(struct rte_security_ipsec_xform *ipsec,
229 struct rte_crypto_sym_xform *xform,
230 struct otx2_sec_session_ipsec_ip *sess)
232 struct rte_crypto_sym_xform *cipher_xform, *auth_xform;
234 sess->partial_len = sizeof(struct rte_ipv4_hdr);
236 if (ipsec->proto == RTE_SECURITY_IPSEC_SA_PROTO_ESP) {
237 sess->partial_len += sizeof(struct rte_esp_hdr);
238 sess->roundup_len = sizeof(struct rte_esp_tail);
239 } else if (ipsec->proto == RTE_SECURITY_IPSEC_SA_PROTO_AH) {
240 sess->partial_len += AH_HDR_LEN;
245 if (ipsec->options.udp_encap)
246 sess->partial_len += sizeof(struct rte_udp_hdr);
248 if (xform->type == RTE_CRYPTO_SYM_XFORM_AEAD) {
249 if (xform->aead.algo == RTE_CRYPTO_AEAD_AES_GCM) {
250 sess->partial_len += AES_GCM_IV_LEN;
251 sess->partial_len += AES_GCM_MAC_LEN;
252 sess->roundup_byte = AES_GCM_ROUNDUP_BYTE_LEN;
257 if (ipsec->direction == RTE_SECURITY_IPSEC_SA_DIR_EGRESS) {
258 cipher_xform = xform;
259 auth_xform = xform->next;
260 } else if (ipsec->direction == RTE_SECURITY_IPSEC_SA_DIR_INGRESS) {
262 cipher_xform = xform->next;
266 if (cipher_xform->cipher.algo == RTE_CRYPTO_CIPHER_AES_CBC) {
267 sess->partial_len += AES_CBC_IV_LEN;
268 sess->roundup_byte = AES_CBC_ROUNDUP_BYTE_LEN;
273 if (auth_xform->auth.algo == RTE_CRYPTO_AUTH_SHA1_HMAC)
274 sess->partial_len += SHA1_HMAC_LEN;
282 hmac_init(struct otx2_ipsec_fp_sa_ctl *ctl, struct otx2_cpt_qp *qp,
283 const uint8_t *auth_key, int len, uint8_t *hmac_key)
286 struct otx2_cpt_res cpt_res;
290 volatile struct otx2_cpt_res *res;
291 uint64_t timeout, lmt_status;
292 struct otx2_cpt_inst_s inst;
296 memset(&inst, 0, sizeof(struct otx2_cpt_inst_s));
298 md = rte_zmalloc(NULL, sizeof(struct inst_data), OTX2_CPT_RES_ALIGN);
302 memcpy(md->buffer, auth_key, len);
304 md_iova = rte_malloc_virt2iova(md);
305 if (md_iova == RTE_BAD_IOVA) {
310 inst.res_addr = md_iova + offsetof(struct inst_data, cpt_res);
311 inst.opcode = OTX2_CPT_OP_WRITE_HMAC_IPAD_OPAD;
312 inst.param2 = ctl->auth_type;
314 inst.dptr = md_iova + offsetof(struct inst_data, buffer);
315 inst.rptr = inst.dptr;
316 inst.egrp = OTX2_CPT_EGRP_INLINE_IPSEC;
318 md->cpt_res.compcode = 0;
319 md->cpt_res.uc_compcode = 0xff;
321 timeout = rte_get_timer_cycles() + 5 * rte_get_timer_hz();
326 otx2_lmt_mov(qp->lmtline, &inst, 2);
327 lmt_status = otx2_lmt_submit(qp->lf_nq_reg);
328 } while (lmt_status == 0);
330 res = (volatile struct otx2_cpt_res *)&md->cpt_res;
332 /* Wait until instruction completes or times out */
333 while (res->uc_compcode == 0xff) {
334 if (rte_get_timer_cycles() > timeout)
338 if (res->u16[0] != OTX2_SEC_COMP_GOOD) {
343 /* Retrieve the ipad and opad from rptr */
344 memcpy(hmac_key, md->buffer, 48);
354 eth_sec_ipsec_out_sess_create(struct rte_eth_dev *eth_dev,
355 struct rte_security_ipsec_xform *ipsec,
356 struct rte_crypto_sym_xform *crypto_xform,
357 struct rte_security_session *sec_sess)
359 struct rte_crypto_sym_xform *auth_xform, *cipher_xform;
360 struct otx2_sec_session_ipsec_ip *sess;
361 uint16_t port = eth_dev->data->port_id;
362 int cipher_key_len, auth_key_len, ret;
363 const uint8_t *cipher_key, *auth_key;
364 struct otx2_ipsec_fp_sa_ctl *ctl;
365 struct otx2_ipsec_fp_out_sa *sa;
366 struct otx2_sec_session *priv;
367 struct otx2_cpt_inst_s inst;
368 struct otx2_cpt_qp *qp;
370 priv = get_sec_session_private_data(sec_sess);
371 sess = &priv->ipsec.ip;
376 otx2_err("SA already registered");
380 memset(sess, 0, sizeof(struct otx2_sec_session_ipsec_ip));
384 ret = ipsec_sa_const_set(ipsec, crypto_xform, sess);
388 if (crypto_xform->type == RTE_CRYPTO_SYM_XFORM_AEAD)
389 memcpy(sa->nonce, &ipsec->salt, 4);
391 if (ipsec->options.udp_encap == 1) {
396 if (ipsec->mode == RTE_SECURITY_IPSEC_SA_MODE_TUNNEL) {
397 /* Start ip id from 1 */
400 if (ipsec->tunnel.type == RTE_SECURITY_IPSEC_TUNNEL_IPV4) {
401 memcpy(&sa->ip_src, &ipsec->tunnel.ipv4.src_ip,
402 sizeof(struct in_addr));
403 memcpy(&sa->ip_dst, &ipsec->tunnel.ipv4.dst_ip,
404 sizeof(struct in_addr));
412 cipher_xform = crypto_xform;
413 auth_xform = crypto_xform->next;
419 if (crypto_xform->type == RTE_CRYPTO_SYM_XFORM_AEAD) {
420 cipher_key = crypto_xform->aead.key.data;
421 cipher_key_len = crypto_xform->aead.key.length;
423 cipher_key = cipher_xform->cipher.key.data;
424 cipher_key_len = cipher_xform->cipher.key.length;
425 auth_key = auth_xform->auth.key.data;
426 auth_key_len = auth_xform->auth.key.length;
429 if (cipher_key_len != 0)
430 memcpy(sa->cipher_key, cipher_key, cipher_key_len);
434 /* Determine word 7 of CPT instruction */
436 inst.egrp = OTX2_CPT_EGRP_INLINE_IPSEC;
437 inst.cptr = rte_mempool_virt2iova(sa);
438 sess->inst_w7 = inst.u64[7];
440 /* Get CPT QP to be used for this SA */
441 ret = otx2_sec_idev_tx_cpt_qp_get(port, &qp);
447 sess->cpt_lmtline = qp->lmtline;
448 sess->cpt_nq_reg = qp->lf_nq_reg;
450 /* Populate control word */
451 ret = ipsec_fp_sa_ctl_set(ipsec, crypto_xform, ctl);
455 if (auth_key_len && auth_key) {
456 ret = hmac_init(ctl, qp, auth_key, auth_key_len, sa->hmac_key);
463 otx2_sec_idev_tx_cpt_qp_put(sess->qp);
468 eth_sec_ipsec_in_sess_create(struct rte_eth_dev *eth_dev,
469 struct rte_security_ipsec_xform *ipsec,
470 struct rte_crypto_sym_xform *crypto_xform,
471 struct rte_security_session *sec_sess)
473 struct rte_crypto_sym_xform *auth_xform, *cipher_xform;
474 struct otx2_eth_dev *dev = otx2_eth_pmd_priv(eth_dev);
475 struct otx2_sec_session_ipsec_ip *sess;
476 uint16_t port = eth_dev->data->port_id;
477 int cipher_key_len, auth_key_len, ret;
478 const uint8_t *cipher_key, *auth_key;
479 struct otx2_ipsec_fp_sa_ctl *ctl;
480 struct otx2_ipsec_fp_in_sa *sa;
481 struct otx2_sec_session *priv;
482 struct otx2_cpt_qp *qp;
484 if (ipsec->spi >= dev->ipsec_in_max_spi) {
485 otx2_err("SPI exceeds max supported");
489 sa = in_sa_get(port, ipsec->spi);
492 priv = get_sec_session_private_data(sec_sess);
493 sess = &priv->ipsec.ip;
496 otx2_err("SA already registered");
500 memset(sa, 0, sizeof(struct otx2_ipsec_fp_in_sa));
502 auth_xform = crypto_xform;
503 cipher_xform = crypto_xform->next;
509 if (crypto_xform->type == RTE_CRYPTO_SYM_XFORM_AEAD) {
510 if (crypto_xform->aead.algo == RTE_CRYPTO_AEAD_AES_GCM)
511 memcpy(sa->nonce, &ipsec->salt, 4);
512 cipher_key = crypto_xform->aead.key.data;
513 cipher_key_len = crypto_xform->aead.key.length;
515 cipher_key = cipher_xform->cipher.key.data;
516 cipher_key_len = cipher_xform->cipher.key.length;
517 auth_key = auth_xform->auth.key.data;
518 auth_key_len = auth_xform->auth.key.length;
521 if (cipher_key_len != 0)
522 memcpy(sa->cipher_key, cipher_key, cipher_key_len);
528 sa->userdata = priv->userdata;
530 if (lookup_mem_sa_index_update(eth_dev, ipsec->spi, sa))
533 ret = ipsec_fp_sa_ctl_set(ipsec, crypto_xform, ctl);
537 if (auth_key_len && auth_key) {
538 /* Get a queue pair for HMAC init */
539 ret = otx2_sec_idev_tx_cpt_qp_get(port, &qp);
542 ret = hmac_init(ctl, qp, auth_key, auth_key_len, sa->hmac_key);
543 otx2_sec_idev_tx_cpt_qp_put(qp);
549 eth_sec_ipsec_sess_create(struct rte_eth_dev *eth_dev,
550 struct rte_security_ipsec_xform *ipsec,
551 struct rte_crypto_sym_xform *crypto_xform,
552 struct rte_security_session *sess)
556 ret = ipsec_fp_xform_verify(ipsec, crypto_xform);
560 if (ipsec->direction == RTE_SECURITY_IPSEC_SA_DIR_INGRESS)
561 return eth_sec_ipsec_in_sess_create(eth_dev, ipsec,
564 return eth_sec_ipsec_out_sess_create(eth_dev, ipsec,
569 otx2_eth_sec_session_create(void *device,
570 struct rte_security_session_conf *conf,
571 struct rte_security_session *sess,
572 struct rte_mempool *mempool)
574 struct otx2_sec_session *priv;
577 if (conf->action_type != RTE_SECURITY_ACTION_TYPE_INLINE_PROTOCOL)
580 if (rte_mempool_get(mempool, (void **)&priv)) {
581 otx2_err("Could not allocate security session private data");
585 set_sec_session_private_data(sess, priv);
588 * Save userdata provided by the application. For ingress packets, this
589 * could be used to identify the SA.
591 priv->userdata = conf->userdata;
593 if (conf->protocol == RTE_SECURITY_PROTOCOL_IPSEC)
594 ret = eth_sec_ipsec_sess_create(device, &conf->ipsec,
606 rte_mempool_put(mempool, priv);
607 set_sec_session_private_data(sess, NULL);
612 otx2_eth_sec_session_destroy(void *device __rte_unused,
613 struct rte_security_session *sess)
615 struct otx2_sec_session_ipsec_ip *sess_ip;
616 struct otx2_sec_session *priv;
617 struct rte_mempool *sess_mp;
620 priv = get_sec_session_private_data(sess);
624 sess_ip = &priv->ipsec.ip;
626 /* Release CPT LF used for this session */
627 if (sess_ip->qp != NULL) {
628 ret = otx2_sec_idev_tx_cpt_qp_put(sess_ip->qp);
633 sess_mp = rte_mempool_from_obj(priv);
635 set_sec_session_private_data(sess, NULL);
636 rte_mempool_put(sess_mp, priv);
642 otx2_eth_sec_session_get_size(void *device __rte_unused)
644 return sizeof(struct otx2_sec_session);
648 otx2_eth_sec_set_pkt_mdata(void *device __rte_unused,
649 struct rte_security_session *session,
650 struct rte_mbuf *m, void *params __rte_unused)
652 /* Set security session as the pkt metadata */
653 m->udata64 = (uint64_t)session;
659 otx2_eth_sec_get_userdata(void *device __rte_unused, uint64_t md,
662 /* Retrieve userdata */
663 *userdata = (void *)md;
668 static const struct rte_security_capability *
669 otx2_eth_sec_capabilities_get(void *device __rte_unused)
671 return otx2_eth_sec_capabilities;
674 static struct rte_security_ops otx2_eth_sec_ops = {
675 .session_create = otx2_eth_sec_session_create,
676 .session_destroy = otx2_eth_sec_session_destroy,
677 .session_get_size = otx2_eth_sec_session_get_size,
678 .set_pkt_metadata = otx2_eth_sec_set_pkt_mdata,
679 .get_userdata = otx2_eth_sec_get_userdata,
680 .capabilities_get = otx2_eth_sec_capabilities_get
684 otx2_eth_sec_ctx_create(struct rte_eth_dev *eth_dev)
686 struct rte_security_ctx *ctx;
689 ctx = rte_malloc("otx2_eth_sec_ctx",
690 sizeof(struct rte_security_ctx), 0);
694 ret = otx2_sec_idev_cfg_init(eth_dev->data->port_id);
702 ctx->device = eth_dev;
703 ctx->ops = &otx2_eth_sec_ops;
706 eth_dev->security_ctx = ctx;
712 otx2_eth_sec_ctx_destroy(struct rte_eth_dev *eth_dev)
714 rte_free(eth_dev->security_ctx);
718 eth_sec_ipsec_cfg(struct rte_eth_dev *eth_dev, uint8_t tt)
720 struct otx2_eth_dev *dev = otx2_eth_pmd_priv(eth_dev);
721 uint16_t port = eth_dev->data->port_id;
722 struct nix_inline_ipsec_lf_cfg *req;
723 struct otx2_mbox *mbox = dev->mbox;
724 struct eth_sec_tag_const tag_const;
725 char name[RTE_MEMZONE_NAMESIZE];
726 const struct rte_memzone *mz;
728 in_sa_mz_name_get(name, RTE_MEMZONE_NAMESIZE, port);
729 mz = rte_memzone_lookup(name);
733 req = otx2_mbox_alloc_msg_nix_inline_ipsec_lf_cfg(mbox);
735 req->sa_base_addr = mz->iova;
737 req->ipsec_cfg0.tt = tt;
740 tag_const.event_type = RTE_EVENT_TYPE_ETHDEV;
741 tag_const.port = port;
742 req->ipsec_cfg0.tag_const = tag_const.u32;
744 req->ipsec_cfg0.sa_pow2_size =
745 rte_log2_u32(sizeof(struct otx2_ipsec_fp_in_sa));
746 req->ipsec_cfg0.lenm1_max = NIX_MAX_FRS - 1;
748 req->ipsec_cfg1.sa_idx_w = rte_log2_u32(dev->ipsec_in_max_spi);
749 req->ipsec_cfg1.sa_idx_max = dev->ipsec_in_max_spi - 1;
751 return otx2_mbox_process(mbox);
755 otx2_eth_sec_update_tag_type(struct rte_eth_dev *eth_dev)
757 struct otx2_eth_dev *dev = otx2_eth_pmd_priv(eth_dev);
758 struct otx2_mbox *mbox = dev->mbox;
759 struct nix_aq_enq_rsp *rsp;
760 struct nix_aq_enq_req *aq;
763 aq = otx2_mbox_alloc_msg_nix_aq_enq(mbox);
764 aq->qidx = 0; /* Read RQ:0 context */
765 aq->ctype = NIX_AQ_CTYPE_RQ;
766 aq->op = NIX_AQ_INSTOP_READ;
768 ret = otx2_mbox_process_msg(mbox, (void *)&rsp);
770 otx2_err("Could not read RQ context");
774 /* Update tag type */
775 ret = eth_sec_ipsec_cfg(eth_dev, rsp->rq.sso_tt);
777 otx2_err("Could not update sec eth tag type");
783 otx2_eth_sec_init(struct rte_eth_dev *eth_dev)
785 const size_t sa_width = sizeof(struct otx2_ipsec_fp_in_sa);
786 struct otx2_eth_dev *dev = otx2_eth_pmd_priv(eth_dev);
787 uint16_t port = eth_dev->data->port_id;
788 char name[RTE_MEMZONE_NAMESIZE];
789 const struct rte_memzone *mz;
793 RTE_BUILD_BUG_ON(sa_width < 32 || sa_width > 512 ||
794 !RTE_IS_POWER_OF_2(sa_width));
796 if (!(dev->tx_offloads & DEV_TX_OFFLOAD_SECURITY) &&
797 !(dev->rx_offloads & DEV_RX_OFFLOAD_SECURITY))
800 nb_sa = dev->ipsec_in_max_spi;
801 mz_sz = nb_sa * sa_width;
802 in_sa_mz_name_get(name, RTE_MEMZONE_NAMESIZE, port);
803 mz = rte_memzone_reserve_aligned(name, mz_sz, rte_socket_id(),
804 RTE_MEMZONE_IOVA_CONTIG, OTX2_ALIGN);
807 otx2_err("Could not allocate inbound SA DB");
811 memset(mz->addr, 0, mz_sz);
813 ret = eth_sec_ipsec_cfg(eth_dev, SSO_TT_ORDERED);
815 otx2_err("Could not configure inline IPsec");
822 otx2_err("Could not configure device for security");
823 otx2_eth_sec_fini(eth_dev);
828 otx2_eth_sec_fini(struct rte_eth_dev *eth_dev)
830 struct otx2_eth_dev *dev = otx2_eth_pmd_priv(eth_dev);
831 uint16_t port = eth_dev->data->port_id;
832 char name[RTE_MEMZONE_NAMESIZE];
834 if (!(dev->tx_offloads & DEV_TX_OFFLOAD_SECURITY) &&
835 !(dev->rx_offloads & DEV_RX_OFFLOAD_SECURITY))
838 lookup_mem_sa_tbl_clear(eth_dev);
840 in_sa_mz_name_get(name, RTE_MEMZONE_NAMESIZE, port);
841 rte_memzone_free(rte_memzone_lookup(name));