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"
22 #include "otx2_security.h"
25 #define AES_GCM_IV_LEN 8
26 #define AES_GCM_MAC_LEN 16
27 #define AES_CBC_IV_LEN 16
28 #define SHA1_HMAC_LEN 12
30 #define AES_GCM_ROUNDUP_BYTE_LEN 4
31 #define AES_CBC_ROUNDUP_BYTE_LEN 16
33 struct eth_sec_tag_const {
37 uint32_t rsvd_11_0 : 12;
39 uint32_t event_type : 4;
40 uint32_t rsvd_31_24 : 8;
46 static struct rte_cryptodev_capabilities otx2_eth_sec_crypto_caps[] = {
48 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
50 .xform_type = RTE_CRYPTO_SYM_XFORM_AEAD,
52 .algo = RTE_CRYPTO_AEAD_AES_GCM,
78 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
80 .xform_type = RTE_CRYPTO_SYM_XFORM_CIPHER,
82 .algo = RTE_CRYPTO_CIPHER_AES_CBC,
98 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
100 .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH,
102 .algo = RTE_CRYPTO_AUTH_SHA1_HMAC,
117 RTE_CRYPTODEV_END_OF_CAPABILITIES_LIST()
120 static const struct rte_security_capability otx2_eth_sec_capabilities[] = {
121 { /* IPsec Inline Protocol ESP Tunnel Ingress */
122 .action = RTE_SECURITY_ACTION_TYPE_INLINE_PROTOCOL,
123 .protocol = RTE_SECURITY_PROTOCOL_IPSEC,
125 .proto = RTE_SECURITY_IPSEC_SA_PROTO_ESP,
126 .mode = RTE_SECURITY_IPSEC_SA_MODE_TUNNEL,
127 .direction = RTE_SECURITY_IPSEC_SA_DIR_INGRESS,
130 .crypto_capabilities = otx2_eth_sec_crypto_caps,
131 .ol_flags = RTE_SECURITY_TX_OLOAD_NEED_MDATA
133 { /* IPsec Inline Protocol ESP Tunnel Egress */
134 .action = RTE_SECURITY_ACTION_TYPE_INLINE_PROTOCOL,
135 .protocol = RTE_SECURITY_PROTOCOL_IPSEC,
137 .proto = RTE_SECURITY_IPSEC_SA_PROTO_ESP,
138 .mode = RTE_SECURITY_IPSEC_SA_MODE_TUNNEL,
139 .direction = RTE_SECURITY_IPSEC_SA_DIR_EGRESS,
142 .crypto_capabilities = otx2_eth_sec_crypto_caps,
143 .ol_flags = RTE_SECURITY_TX_OLOAD_NEED_MDATA
146 .action = RTE_SECURITY_ACTION_TYPE_NONE
151 lookup_mem_sa_tbl_clear(struct rte_eth_dev *eth_dev)
153 static const char name[] = OTX2_NIX_FASTPATH_LOOKUP_MEM;
154 uint16_t port = eth_dev->data->port_id;
155 const struct rte_memzone *mz;
159 mz = rte_memzone_lookup(name);
165 sa_tbl = (uint64_t **)RTE_PTR_ADD(mem, OTX2_NIX_SA_TBL_START);
166 if (sa_tbl[port] == NULL)
169 rte_free(sa_tbl[port]);
174 lookup_mem_sa_index_update(struct rte_eth_dev *eth_dev, int spi, void *sa)
176 static const char name[] = OTX2_NIX_FASTPATH_LOOKUP_MEM;
177 struct otx2_eth_dev *dev = otx2_eth_pmd_priv(eth_dev);
178 uint16_t port = eth_dev->data->port_id;
179 const struct rte_memzone *mz;
183 mz = rte_memzone_lookup(name);
185 otx2_err("Could not find fastpath lookup table");
191 sa_tbl = (uint64_t **)RTE_PTR_ADD(mem, OTX2_NIX_SA_TBL_START);
193 if (sa_tbl[port] == NULL) {
194 sa_tbl[port] = rte_malloc(NULL, dev->ipsec_in_max_spi *
195 sizeof(uint64_t), 0);
198 sa_tbl[port][spi] = (uint64_t)sa;
204 in_sa_mz_name_get(char *name, int size, uint16_t port)
206 snprintf(name, size, "otx2_ipsec_in_sadb_%u", port);
209 static struct otx2_ipsec_fp_in_sa *
210 in_sa_get(uint16_t port, int sa_index)
212 char name[RTE_MEMZONE_NAMESIZE];
213 struct otx2_ipsec_fp_in_sa *sa;
214 const struct rte_memzone *mz;
216 in_sa_mz_name_get(name, RTE_MEMZONE_NAMESIZE, port);
217 mz = rte_memzone_lookup(name);
219 otx2_err("Could not get the memzone reserved for IN SA DB");
225 return sa + sa_index;
229 ipsec_sa_const_set(struct rte_security_ipsec_xform *ipsec,
230 struct rte_crypto_sym_xform *xform,
231 struct otx2_sec_session_ipsec_ip *sess)
233 struct rte_crypto_sym_xform *cipher_xform, *auth_xform;
235 sess->partial_len = sizeof(struct rte_ipv4_hdr);
237 if (ipsec->proto == RTE_SECURITY_IPSEC_SA_PROTO_ESP) {
238 sess->partial_len += sizeof(struct rte_esp_hdr);
239 sess->roundup_len = sizeof(struct rte_esp_tail);
240 } else if (ipsec->proto == RTE_SECURITY_IPSEC_SA_PROTO_AH) {
241 sess->partial_len += AH_HDR_LEN;
246 if (ipsec->options.udp_encap)
247 sess->partial_len += sizeof(struct rte_udp_hdr);
249 if (xform->type == RTE_CRYPTO_SYM_XFORM_AEAD) {
250 if (xform->aead.algo == RTE_CRYPTO_AEAD_AES_GCM) {
251 sess->partial_len += AES_GCM_IV_LEN;
252 sess->partial_len += AES_GCM_MAC_LEN;
253 sess->roundup_byte = AES_GCM_ROUNDUP_BYTE_LEN;
258 if (ipsec->direction == RTE_SECURITY_IPSEC_SA_DIR_EGRESS) {
259 cipher_xform = xform;
260 auth_xform = xform->next;
261 } else if (ipsec->direction == RTE_SECURITY_IPSEC_SA_DIR_INGRESS) {
263 cipher_xform = xform->next;
267 if (cipher_xform->cipher.algo == RTE_CRYPTO_CIPHER_AES_CBC) {
268 sess->partial_len += AES_CBC_IV_LEN;
269 sess->roundup_byte = AES_CBC_ROUNDUP_BYTE_LEN;
274 if (auth_xform->auth.algo == RTE_CRYPTO_AUTH_SHA1_HMAC)
275 sess->partial_len += SHA1_HMAC_LEN;
283 hmac_init(struct otx2_ipsec_fp_sa_ctl *ctl, struct otx2_cpt_qp *qp,
284 const uint8_t *auth_key, int len, uint8_t *hmac_key)
287 struct otx2_cpt_res cpt_res;
291 volatile struct otx2_cpt_res *res;
292 uint64_t timeout, lmt_status;
293 struct otx2_cpt_inst_s inst;
297 memset(&inst, 0, sizeof(struct otx2_cpt_inst_s));
299 md = rte_zmalloc(NULL, sizeof(struct inst_data), OTX2_CPT_RES_ALIGN);
303 memcpy(md->buffer, auth_key, len);
305 md_iova = rte_malloc_virt2iova(md);
306 if (md_iova == RTE_BAD_IOVA) {
311 inst.res_addr = md_iova + offsetof(struct inst_data, cpt_res);
312 inst.opcode = OTX2_CPT_OP_WRITE_HMAC_IPAD_OPAD;
313 inst.param2 = ctl->auth_type;
315 inst.dptr = md_iova + offsetof(struct inst_data, buffer);
316 inst.rptr = inst.dptr;
317 inst.egrp = OTX2_CPT_EGRP_INLINE_IPSEC;
319 md->cpt_res.compcode = 0;
320 md->cpt_res.uc_compcode = 0xff;
322 timeout = rte_get_timer_cycles() + 5 * rte_get_timer_hz();
327 otx2_lmt_mov(qp->lmtline, &inst, 2);
328 lmt_status = otx2_lmt_submit(qp->lf_nq_reg);
329 } while (lmt_status == 0);
331 res = (volatile struct otx2_cpt_res *)&md->cpt_res;
333 /* Wait until instruction completes or times out */
334 while (res->uc_compcode == 0xff) {
335 if (rte_get_timer_cycles() > timeout)
339 if (res->u16[0] != OTX2_SEC_COMP_GOOD) {
344 /* Retrieve the ipad and opad from rptr */
345 memcpy(hmac_key, md->buffer, 48);
355 eth_sec_ipsec_out_sess_create(struct rte_eth_dev *eth_dev,
356 struct rte_security_ipsec_xform *ipsec,
357 struct rte_crypto_sym_xform *crypto_xform,
358 struct rte_security_session *sec_sess)
360 struct rte_crypto_sym_xform *auth_xform, *cipher_xform;
361 struct otx2_sec_session_ipsec_ip *sess;
362 uint16_t port = eth_dev->data->port_id;
363 int cipher_key_len, auth_key_len, ret;
364 const uint8_t *cipher_key, *auth_key;
365 struct otx2_ipsec_fp_sa_ctl *ctl;
366 struct otx2_ipsec_fp_out_sa *sa;
367 struct otx2_sec_session *priv;
368 struct otx2_cpt_inst_s inst;
369 struct otx2_cpt_qp *qp;
371 priv = get_sec_session_private_data(sec_sess);
372 sess = &priv->ipsec.ip;
377 otx2_err("SA already registered");
381 memset(sess, 0, sizeof(struct otx2_sec_session_ipsec_ip));
385 ret = ipsec_sa_const_set(ipsec, crypto_xform, sess);
389 if (crypto_xform->type == RTE_CRYPTO_SYM_XFORM_AEAD)
390 memcpy(sa->nonce, &ipsec->salt, 4);
392 if (ipsec->options.udp_encap == 1) {
397 if (ipsec->mode == RTE_SECURITY_IPSEC_SA_MODE_TUNNEL) {
398 /* Start ip id from 1 */
401 if (ipsec->tunnel.type == RTE_SECURITY_IPSEC_TUNNEL_IPV4) {
402 memcpy(&sa->ip_src, &ipsec->tunnel.ipv4.src_ip,
403 sizeof(struct in_addr));
404 memcpy(&sa->ip_dst, &ipsec->tunnel.ipv4.dst_ip,
405 sizeof(struct in_addr));
413 cipher_xform = crypto_xform;
414 auth_xform = crypto_xform->next;
420 if (crypto_xform->type == RTE_CRYPTO_SYM_XFORM_AEAD) {
421 cipher_key = crypto_xform->aead.key.data;
422 cipher_key_len = crypto_xform->aead.key.length;
424 cipher_key = cipher_xform->cipher.key.data;
425 cipher_key_len = cipher_xform->cipher.key.length;
426 auth_key = auth_xform->auth.key.data;
427 auth_key_len = auth_xform->auth.key.length;
430 if (cipher_key_len != 0)
431 memcpy(sa->cipher_key, cipher_key, cipher_key_len);
435 /* Determine word 7 of CPT instruction */
437 inst.egrp = OTX2_CPT_EGRP_INLINE_IPSEC;
438 inst.cptr = rte_mempool_virt2iova(sa);
439 sess->inst_w7 = inst.u64[7];
441 /* Get CPT QP to be used for this SA */
442 ret = otx2_sec_idev_tx_cpt_qp_get(port, &qp);
448 sess->cpt_lmtline = qp->lmtline;
449 sess->cpt_nq_reg = qp->lf_nq_reg;
451 /* Populate control word */
452 ret = ipsec_fp_sa_ctl_set(ipsec, crypto_xform, ctl);
456 if (auth_key_len && auth_key) {
457 ret = hmac_init(ctl, qp, auth_key, auth_key_len, sa->hmac_key);
464 otx2_sec_idev_tx_cpt_qp_put(sess->qp);
469 eth_sec_ipsec_in_sess_create(struct rte_eth_dev *eth_dev,
470 struct rte_security_ipsec_xform *ipsec,
471 struct rte_crypto_sym_xform *crypto_xform,
472 struct rte_security_session *sec_sess)
474 struct rte_crypto_sym_xform *auth_xform, *cipher_xform;
475 struct otx2_eth_dev *dev = otx2_eth_pmd_priv(eth_dev);
476 struct otx2_sec_session_ipsec_ip *sess;
477 uint16_t port = eth_dev->data->port_id;
478 int cipher_key_len, auth_key_len, ret;
479 const uint8_t *cipher_key, *auth_key;
480 struct otx2_ipsec_fp_sa_ctl *ctl;
481 struct otx2_ipsec_fp_in_sa *sa;
482 struct otx2_sec_session *priv;
483 struct otx2_cpt_qp *qp;
485 if (ipsec->spi >= dev->ipsec_in_max_spi) {
486 otx2_err("SPI exceeds max supported");
490 sa = in_sa_get(port, ipsec->spi);
493 priv = get_sec_session_private_data(sec_sess);
494 sess = &priv->ipsec.ip;
497 otx2_err("SA already registered");
501 memset(sa, 0, sizeof(struct otx2_ipsec_fp_in_sa));
503 auth_xform = crypto_xform;
504 cipher_xform = crypto_xform->next;
510 if (crypto_xform->type == RTE_CRYPTO_SYM_XFORM_AEAD) {
511 if (crypto_xform->aead.algo == RTE_CRYPTO_AEAD_AES_GCM)
512 memcpy(sa->nonce, &ipsec->salt, 4);
513 cipher_key = crypto_xform->aead.key.data;
514 cipher_key_len = crypto_xform->aead.key.length;
516 cipher_key = cipher_xform->cipher.key.data;
517 cipher_key_len = cipher_xform->cipher.key.length;
518 auth_key = auth_xform->auth.key.data;
519 auth_key_len = auth_xform->auth.key.length;
522 if (cipher_key_len != 0)
523 memcpy(sa->cipher_key, cipher_key, cipher_key_len);
529 sa->userdata = priv->userdata;
531 if (lookup_mem_sa_index_update(eth_dev, ipsec->spi, sa))
534 ret = ipsec_fp_sa_ctl_set(ipsec, crypto_xform, ctl);
538 if (auth_key_len && auth_key) {
539 /* Get a queue pair for HMAC init */
540 ret = otx2_sec_idev_tx_cpt_qp_get(port, &qp);
543 ret = hmac_init(ctl, qp, auth_key, auth_key_len, sa->hmac_key);
544 otx2_sec_idev_tx_cpt_qp_put(qp);
550 eth_sec_ipsec_sess_create(struct rte_eth_dev *eth_dev,
551 struct rte_security_ipsec_xform *ipsec,
552 struct rte_crypto_sym_xform *crypto_xform,
553 struct rte_security_session *sess)
557 ret = ipsec_fp_xform_verify(ipsec, crypto_xform);
561 if (ipsec->direction == RTE_SECURITY_IPSEC_SA_DIR_INGRESS)
562 return eth_sec_ipsec_in_sess_create(eth_dev, ipsec,
565 return eth_sec_ipsec_out_sess_create(eth_dev, ipsec,
570 otx2_eth_sec_session_create(void *device,
571 struct rte_security_session_conf *conf,
572 struct rte_security_session *sess,
573 struct rte_mempool *mempool)
575 struct otx2_sec_session *priv;
578 if (conf->action_type != RTE_SECURITY_ACTION_TYPE_INLINE_PROTOCOL)
581 if (rte_mempool_get(mempool, (void **)&priv)) {
582 otx2_err("Could not allocate security session private data");
586 set_sec_session_private_data(sess, priv);
589 * Save userdata provided by the application. For ingress packets, this
590 * could be used to identify the SA.
592 priv->userdata = conf->userdata;
594 if (conf->protocol == RTE_SECURITY_PROTOCOL_IPSEC)
595 ret = eth_sec_ipsec_sess_create(device, &conf->ipsec,
607 rte_mempool_put(mempool, priv);
608 set_sec_session_private_data(sess, NULL);
613 otx2_eth_sec_session_destroy(void *device __rte_unused,
614 struct rte_security_session *sess)
616 struct otx2_sec_session_ipsec_ip *sess_ip;
617 struct otx2_sec_session *priv;
618 struct rte_mempool *sess_mp;
621 priv = get_sec_session_private_data(sess);
625 sess_ip = &priv->ipsec.ip;
627 /* Release CPT LF used for this session */
628 if (sess_ip->qp != NULL) {
629 ret = otx2_sec_idev_tx_cpt_qp_put(sess_ip->qp);
634 sess_mp = rte_mempool_from_obj(priv);
636 set_sec_session_private_data(sess, NULL);
637 rte_mempool_put(sess_mp, priv);
643 otx2_eth_sec_session_get_size(void *device __rte_unused)
645 return sizeof(struct otx2_sec_session);
649 otx2_eth_sec_set_pkt_mdata(void *device __rte_unused,
650 struct rte_security_session *session,
651 struct rte_mbuf *m, void *params __rte_unused)
653 /* Set security session as the pkt metadata */
654 m->udata64 = (uint64_t)session;
660 otx2_eth_sec_get_userdata(void *device __rte_unused, uint64_t md,
663 /* Retrieve userdata */
664 *userdata = (void *)md;
669 static const struct rte_security_capability *
670 otx2_eth_sec_capabilities_get(void *device __rte_unused)
672 return otx2_eth_sec_capabilities;
675 static struct rte_security_ops otx2_eth_sec_ops = {
676 .session_create = otx2_eth_sec_session_create,
677 .session_destroy = otx2_eth_sec_session_destroy,
678 .session_get_size = otx2_eth_sec_session_get_size,
679 .set_pkt_metadata = otx2_eth_sec_set_pkt_mdata,
680 .get_userdata = otx2_eth_sec_get_userdata,
681 .capabilities_get = otx2_eth_sec_capabilities_get
685 otx2_eth_sec_ctx_create(struct rte_eth_dev *eth_dev)
687 struct rte_security_ctx *ctx;
690 ctx = rte_malloc("otx2_eth_sec_ctx",
691 sizeof(struct rte_security_ctx), 0);
695 ret = otx2_sec_idev_cfg_init(eth_dev->data->port_id);
703 ctx->device = eth_dev;
704 ctx->ops = &otx2_eth_sec_ops;
707 eth_dev->security_ctx = ctx;
713 otx2_eth_sec_ctx_destroy(struct rte_eth_dev *eth_dev)
715 rte_free(eth_dev->security_ctx);
719 eth_sec_ipsec_cfg(struct rte_eth_dev *eth_dev, uint8_t tt)
721 struct otx2_eth_dev *dev = otx2_eth_pmd_priv(eth_dev);
722 uint16_t port = eth_dev->data->port_id;
723 struct nix_inline_ipsec_lf_cfg *req;
724 struct otx2_mbox *mbox = dev->mbox;
725 struct eth_sec_tag_const tag_const;
726 char name[RTE_MEMZONE_NAMESIZE];
727 const struct rte_memzone *mz;
729 in_sa_mz_name_get(name, RTE_MEMZONE_NAMESIZE, port);
730 mz = rte_memzone_lookup(name);
734 req = otx2_mbox_alloc_msg_nix_inline_ipsec_lf_cfg(mbox);
736 req->sa_base_addr = mz->iova;
738 req->ipsec_cfg0.tt = tt;
741 tag_const.event_type = RTE_EVENT_TYPE_ETHDEV;
742 tag_const.port = port;
743 req->ipsec_cfg0.tag_const = tag_const.u32;
745 req->ipsec_cfg0.sa_pow2_size =
746 rte_log2_u32(sizeof(struct otx2_ipsec_fp_in_sa));
747 req->ipsec_cfg0.lenm1_max = NIX_MAX_FRS - 1;
749 req->ipsec_cfg1.sa_idx_w = rte_log2_u32(dev->ipsec_in_max_spi);
750 req->ipsec_cfg1.sa_idx_max = dev->ipsec_in_max_spi - 1;
752 return otx2_mbox_process(mbox);
756 otx2_eth_sec_update_tag_type(struct rte_eth_dev *eth_dev)
758 struct otx2_eth_dev *dev = otx2_eth_pmd_priv(eth_dev);
759 struct otx2_mbox *mbox = dev->mbox;
760 struct nix_aq_enq_rsp *rsp;
761 struct nix_aq_enq_req *aq;
764 aq = otx2_mbox_alloc_msg_nix_aq_enq(mbox);
765 aq->qidx = 0; /* Read RQ:0 context */
766 aq->ctype = NIX_AQ_CTYPE_RQ;
767 aq->op = NIX_AQ_INSTOP_READ;
769 ret = otx2_mbox_process_msg(mbox, (void *)&rsp);
771 otx2_err("Could not read RQ context");
775 /* Update tag type */
776 ret = eth_sec_ipsec_cfg(eth_dev, rsp->rq.sso_tt);
778 otx2_err("Could not update sec eth tag type");
784 otx2_eth_sec_init(struct rte_eth_dev *eth_dev)
786 const size_t sa_width = sizeof(struct otx2_ipsec_fp_in_sa);
787 struct otx2_eth_dev *dev = otx2_eth_pmd_priv(eth_dev);
788 uint16_t port = eth_dev->data->port_id;
789 char name[RTE_MEMZONE_NAMESIZE];
790 const struct rte_memzone *mz;
794 RTE_BUILD_BUG_ON(sa_width < 32 || sa_width > 512 ||
795 !RTE_IS_POWER_OF_2(sa_width));
797 if (!(dev->tx_offloads & DEV_TX_OFFLOAD_SECURITY) &&
798 !(dev->rx_offloads & DEV_RX_OFFLOAD_SECURITY))
801 nb_sa = dev->ipsec_in_max_spi;
802 mz_sz = nb_sa * sa_width;
803 in_sa_mz_name_get(name, RTE_MEMZONE_NAMESIZE, port);
804 mz = rte_memzone_reserve_aligned(name, mz_sz, rte_socket_id(),
805 RTE_MEMZONE_IOVA_CONTIG, OTX2_ALIGN);
808 otx2_err("Could not allocate inbound SA DB");
812 memset(mz->addr, 0, mz_sz);
814 ret = eth_sec_ipsec_cfg(eth_dev, SSO_TT_ORDERED);
816 otx2_err("Could not configure inline IPsec");
823 otx2_err("Could not configure device for security");
824 otx2_eth_sec_fini(eth_dev);
829 otx2_eth_sec_fini(struct rte_eth_dev *eth_dev)
831 struct otx2_eth_dev *dev = otx2_eth_pmd_priv(eth_dev);
832 uint16_t port = eth_dev->data->port_id;
833 char name[RTE_MEMZONE_NAMESIZE];
835 if (!(dev->tx_offloads & DEV_TX_OFFLOAD_SECURITY) &&
836 !(dev->rx_offloads & DEV_RX_OFFLOAD_SECURITY))
839 lookup_mem_sa_tbl_clear(eth_dev);
841 in_sa_mz_name_get(name, RTE_MEMZONE_NAMESIZE, port);
842 rte_memzone_free(rte_memzone_lookup(name));