1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(C) 2021 Marvell.
5 #include <rte_cryptodev.h>
6 #include <rte_eventdev.h>
7 #include <rte_security.h>
8 #include <rte_security_driver.h>
10 #include <cn10k_ethdev.h>
11 #include <cnxk_security.h>
13 static struct rte_cryptodev_capabilities cn10k_eth_sec_crypto_caps[] = {
15 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
17 .xform_type = RTE_CRYPTO_SYM_XFORM_AEAD,
19 .algo = RTE_CRYPTO_AEAD_AES_GCM,
45 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
47 .xform_type = RTE_CRYPTO_SYM_XFORM_CIPHER,
49 .algo = RTE_CRYPTO_CIPHER_AES_CBC,
65 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
67 .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH,
69 .algo = RTE_CRYPTO_AUTH_SHA1_HMAC,
84 RTE_CRYPTODEV_END_OF_CAPABILITIES_LIST()
87 static const struct rte_security_capability cn10k_eth_sec_capabilities[] = {
88 { /* IPsec Inline Protocol ESP Tunnel Ingress */
89 .action = RTE_SECURITY_ACTION_TYPE_INLINE_PROTOCOL,
90 .protocol = RTE_SECURITY_PROTOCOL_IPSEC,
92 .proto = RTE_SECURITY_IPSEC_SA_PROTO_ESP,
93 .mode = RTE_SECURITY_IPSEC_SA_MODE_TUNNEL,
94 .direction = RTE_SECURITY_IPSEC_SA_DIR_INGRESS,
97 .crypto_capabilities = cn10k_eth_sec_crypto_caps,
98 .ol_flags = RTE_SECURITY_TX_OLOAD_NEED_MDATA
100 { /* IPsec Inline Protocol ESP Tunnel Egress */
101 .action = RTE_SECURITY_ACTION_TYPE_INLINE_PROTOCOL,
102 .protocol = RTE_SECURITY_PROTOCOL_IPSEC,
104 .proto = RTE_SECURITY_IPSEC_SA_PROTO_ESP,
105 .mode = RTE_SECURITY_IPSEC_SA_MODE_TUNNEL,
106 .direction = RTE_SECURITY_IPSEC_SA_DIR_EGRESS,
109 .crypto_capabilities = cn10k_eth_sec_crypto_caps,
110 .ol_flags = RTE_SECURITY_TX_OLOAD_NEED_MDATA
112 { /* IPsec Inline Protocol ESP Transport Egress */
113 .action = RTE_SECURITY_ACTION_TYPE_INLINE_PROTOCOL,
114 .protocol = RTE_SECURITY_PROTOCOL_IPSEC,
116 .proto = RTE_SECURITY_IPSEC_SA_PROTO_ESP,
117 .mode = RTE_SECURITY_IPSEC_SA_MODE_TRANSPORT,
118 .direction = RTE_SECURITY_IPSEC_SA_DIR_EGRESS,
121 .crypto_capabilities = cn10k_eth_sec_crypto_caps,
122 .ol_flags = RTE_SECURITY_TX_OLOAD_NEED_MDATA
124 { /* IPsec Inline Protocol ESP Transport Ingress */
125 .action = RTE_SECURITY_ACTION_TYPE_INLINE_PROTOCOL,
126 .protocol = RTE_SECURITY_PROTOCOL_IPSEC,
128 .proto = RTE_SECURITY_IPSEC_SA_PROTO_ESP,
129 .mode = RTE_SECURITY_IPSEC_SA_MODE_TRANSPORT,
130 .direction = RTE_SECURITY_IPSEC_SA_DIR_INGRESS,
133 .crypto_capabilities = cn10k_eth_sec_crypto_caps,
134 .ol_flags = RTE_SECURITY_TX_OLOAD_NEED_MDATA
137 .action = RTE_SECURITY_ACTION_TYPE_NONE
142 cnxk_pktmbuf_free_no_cache(struct rte_mbuf *mbuf)
144 struct rte_mbuf *next;
150 roc_npa_aura_op_free(mbuf->pool->pool_id, 1, (rte_iova_t)mbuf);
152 } while (mbuf != NULL);
156 cn10k_eth_sec_sso_work_cb(uint64_t *gw, void *args)
158 struct rte_eth_event_ipsec_desc desc;
159 struct cn10k_sec_sess_priv sess_priv;
160 struct cn10k_outb_priv_data *priv;
161 struct roc_ot_ipsec_outb_sa *sa;
162 struct cpt_cn10k_res_s *res;
163 struct rte_eth_dev *eth_dev;
164 struct cnxk_eth_dev *dev;
165 static uint64_t warn_cnt;
166 uint16_t dlen_adj, rlen;
167 struct rte_mbuf *mbuf;
174 switch ((gw[0] >> 28) & 0xF) {
175 case RTE_EVENT_TYPE_ETHDEV:
176 /* Event from inbound inline dev due to IPSEC packet bad L4 */
177 mbuf = (struct rte_mbuf *)(gw[1] - sizeof(struct rte_mbuf));
178 plt_nix_dbg("Received mbuf %p from inline dev inbound", mbuf);
179 cnxk_pktmbuf_free_no_cache(mbuf);
181 case RTE_EVENT_TYPE_CPU:
182 /* Check for subtype */
183 if (((gw[0] >> 20) & 0xFF) == CNXK_ETHDEV_SEC_OUTB_EV_SUB) {
184 /* Event from outbound inline error */
185 mbuf = (struct rte_mbuf *)gw[1];
190 plt_err("Unknown event gw[0] = 0x%016lx, gw[1] = 0x%016lx",
195 /* Get ethdev port from tag */
197 eth_dev = &rte_eth_devices[port];
198 dev = cnxk_eth_pmd_priv(eth_dev);
200 sess_priv.u64 = *rte_security_dynfield(mbuf);
201 /* Calculate dlen adj */
202 dlen_adj = mbuf->pkt_len - mbuf->l2_len;
203 rlen = (dlen_adj + sess_priv.roundup_len) +
204 (sess_priv.roundup_byte - 1);
205 rlen &= ~(uint64_t)(sess_priv.roundup_byte - 1);
206 rlen += sess_priv.partial_len;
207 dlen_adj = rlen - dlen_adj;
209 /* Find the res area residing on next cacheline after end of data */
210 nixtx = rte_pktmbuf_mtod(mbuf, uintptr_t) + mbuf->pkt_len + dlen_adj;
212 nixtx = (nixtx - 1) & ~(BIT_ULL(7) - 1);
213 res = (struct cpt_cn10k_res_s *)nixtx;
215 plt_nix_dbg("Outbound error, mbuf %p, sa_index %u, compcode %x uc %x",
216 mbuf, sess_priv.sa_idx, res->compcode, res->uc_compcode);
218 sess_priv.u64 = *rte_security_dynfield(mbuf);
220 sa_base = dev->outb.sa_base;
221 sa = roc_nix_inl_ot_ipsec_outb_sa(sa_base, sess_priv.sa_idx);
222 priv = roc_nix_inl_ot_ipsec_outb_sa_sw_rsvd(sa);
224 memset(&desc, 0, sizeof(desc));
226 switch (res->uc_compcode) {
227 case ROC_IE_OT_UCC_ERR_SA_OVERFLOW:
228 desc.subtype = RTE_ETH_EVENT_IPSEC_ESN_OVERFLOW;
230 case ROC_IE_OT_UCC_ERR_PKT_IP:
232 if (warn_cnt % 10000 == 0)
233 plt_warn("Outbound error, bad ip pkt, mbuf %p,"
234 " sa_index %u (total warnings %" PRIu64 ")",
235 mbuf, sess_priv.sa_idx, warn_cnt);
236 desc.subtype = RTE_ETH_EVENT_IPSEC_UNKNOWN;
240 if (warn_cnt % 10000 == 0)
241 plt_warn("Outbound error, mbuf %p, sa_index %u,"
242 " compcode %x uc %x,"
243 " (total warnings %" PRIu64 ")",
244 mbuf, sess_priv.sa_idx, res->compcode,
245 res->uc_compcode, warn_cnt);
246 desc.subtype = RTE_ETH_EVENT_IPSEC_UNKNOWN;
250 desc.metadata = (uint64_t)priv->userdata;
251 rte_eth_dev_callback_process(eth_dev, RTE_ETH_EVENT_IPSEC, &desc);
252 cnxk_pktmbuf_free_no_cache(mbuf);
256 cn10k_eth_sec_session_create(void *device,
257 struct rte_security_session_conf *conf,
258 struct rte_security_session *sess,
259 struct rte_mempool *mempool)
261 struct rte_eth_dev *eth_dev = (struct rte_eth_dev *)device;
262 struct cnxk_eth_dev *dev = cnxk_eth_pmd_priv(eth_dev);
263 struct rte_security_ipsec_xform *ipsec;
264 struct cn10k_sec_sess_priv sess_priv;
265 struct rte_crypto_sym_xform *crypto;
266 struct cnxk_eth_sec_sess *eth_sec;
267 bool inbound, inl_dev;
268 rte_spinlock_t *lock;
269 char tbuf[128] = {0};
272 if (conf->action_type != RTE_SECURITY_ACTION_TYPE_INLINE_PROTOCOL)
275 if (conf->protocol != RTE_SECURITY_PROTOCOL_IPSEC)
278 if (rte_security_dynfield_register() < 0)
281 ipsec = &conf->ipsec;
282 crypto = conf->crypto_xform;
283 inbound = !!(ipsec->direction == RTE_SECURITY_IPSEC_SA_DIR_INGRESS);
284 inl_dev = !!dev->inb.inl_dev;
286 /* Search if a session already exits */
287 if (cnxk_eth_sec_sess_get_by_spi(dev, ipsec->spi, inbound)) {
288 plt_err("%s SA with SPI %u already in use",
289 inbound ? "Inbound" : "Outbound", ipsec->spi);
293 if (rte_mempool_get(mempool, (void **)ð_sec)) {
294 plt_err("Could not allocate security session private data");
298 memset(eth_sec, 0, sizeof(struct cnxk_eth_sec_sess));
301 lock = inbound ? &dev->inb.lock : &dev->outb.lock;
302 rte_spinlock_lock(lock);
304 /* Acquire lock on inline dev for inbound */
305 if (inbound && inl_dev)
306 roc_nix_inl_dev_lock();
309 struct roc_ot_ipsec_inb_sa *inb_sa, *inb_sa_dptr;
310 struct cn10k_inb_priv_data *inb_priv;
313 PLT_STATIC_ASSERT(sizeof(struct cn10k_inb_priv_data) <
314 ROC_NIX_INL_OT_IPSEC_INB_SW_RSVD);
316 /* Get Inbound SA from NIX_RX_IPSEC_SA_BASE */
317 sa = roc_nix_inl_inb_sa_get(&dev->nix, inl_dev, ipsec->spi);
318 if (!sa && dev->inb.inl_dev) {
319 snprintf(tbuf, sizeof(tbuf),
320 "Failed to create ingress sa, inline dev "
321 "not found or spi not in range");
325 snprintf(tbuf, sizeof(tbuf),
326 "Failed to create ingress sa");
331 inb_sa = (struct roc_ot_ipsec_inb_sa *)sa;
333 /* Check if SA is already in use */
334 if (inb_sa->w2.s.valid) {
335 snprintf(tbuf, sizeof(tbuf),
336 "Inbound SA with SPI %u already in use",
342 inb_sa_dptr = (struct roc_ot_ipsec_inb_sa *)dev->inb.sa_dptr;
343 memset(inb_sa_dptr, 0, sizeof(struct roc_ot_ipsec_inb_sa));
345 /* Fill inbound sa params */
346 rc = cnxk_ot_ipsec_inb_sa_fill(inb_sa_dptr, ipsec, crypto,
349 snprintf(tbuf, sizeof(tbuf),
350 "Failed to init inbound sa, rc=%d", rc);
354 inb_priv = roc_nix_inl_ot_ipsec_inb_sa_sw_rsvd(inb_sa);
355 /* Back pointer to get eth_sec */
356 inb_priv->eth_sec = eth_sec;
357 /* Save userdata in inb private area */
358 inb_priv->userdata = conf->userdata;
360 /* Save SA index/SPI in cookie for now */
361 inb_sa_dptr->w1.s.cookie = rte_cpu_to_be_32(ipsec->spi);
363 /* Prepare session priv */
364 sess_priv.inb_sa = 1;
365 sess_priv.sa_idx = ipsec->spi;
367 /* Pointer from eth_sec -> inb_sa */
368 eth_sec->sa = inb_sa;
369 eth_sec->sess = sess;
370 eth_sec->sa_idx = ipsec->spi;
371 eth_sec->spi = ipsec->spi;
372 eth_sec->inl_dev = !!dev->inb.inl_dev;
375 TAILQ_INSERT_TAIL(&dev->inb.list, eth_sec, entry);
377 /* Sync session in context cache */
378 rc = roc_nix_inl_ctx_write(&dev->nix, inb_sa_dptr, eth_sec->sa,
380 sizeof(struct roc_ot_ipsec_inb_sa));
384 struct roc_ot_ipsec_outb_sa *outb_sa, *outb_sa_dptr;
385 struct cn10k_outb_priv_data *outb_priv;
386 struct cnxk_ipsec_outb_rlens *rlens;
387 uint64_t sa_base = dev->outb.sa_base;
390 PLT_STATIC_ASSERT(sizeof(struct cn10k_outb_priv_data) <
391 ROC_NIX_INL_OT_IPSEC_OUTB_SW_RSVD);
393 /* Alloc an sa index */
394 rc = cnxk_eth_outb_sa_idx_get(dev, &sa_idx);
398 outb_sa = roc_nix_inl_ot_ipsec_outb_sa(sa_base, sa_idx);
399 outb_priv = roc_nix_inl_ot_ipsec_outb_sa_sw_rsvd(outb_sa);
400 rlens = &outb_priv->rlens;
402 outb_sa_dptr = (struct roc_ot_ipsec_outb_sa *)dev->outb.sa_dptr;
403 memset(outb_sa_dptr, 0, sizeof(struct roc_ot_ipsec_outb_sa));
405 /* Fill outbound sa params */
406 rc = cnxk_ot_ipsec_outb_sa_fill(outb_sa_dptr, ipsec, crypto);
408 snprintf(tbuf, sizeof(tbuf),
409 "Failed to init outbound sa, rc=%d", rc);
410 rc |= cnxk_eth_outb_sa_idx_put(dev, sa_idx);
415 outb_priv->userdata = conf->userdata;
416 outb_priv->sa_idx = sa_idx;
417 outb_priv->eth_sec = eth_sec;
420 cnxk_ipsec_outb_rlens_get(rlens, ipsec, crypto);
422 /* Prepare session priv */
423 sess_priv.sa_idx = outb_priv->sa_idx;
424 sess_priv.roundup_byte = rlens->roundup_byte;
425 sess_priv.roundup_len = rlens->roundup_len;
426 sess_priv.partial_len = rlens->partial_len;
427 sess_priv.mode = outb_sa_dptr->w2.s.ipsec_mode;
428 sess_priv.outer_ip_ver = outb_sa_dptr->w2.s.outer_ip_ver;
430 /* Pointer from eth_sec -> outb_sa */
431 eth_sec->sa = outb_sa;
432 eth_sec->sess = sess;
433 eth_sec->sa_idx = sa_idx;
434 eth_sec->spi = ipsec->spi;
436 TAILQ_INSERT_TAIL(&dev->outb.list, eth_sec, entry);
438 /* Sync session in context cache */
439 rc = roc_nix_inl_ctx_write(&dev->nix, outb_sa_dptr, eth_sec->sa,
441 sizeof(struct roc_ot_ipsec_outb_sa));
445 if (inbound && inl_dev)
446 roc_nix_inl_dev_unlock();
447 rte_spinlock_unlock(lock);
449 plt_nix_dbg("Created %s session with spi=%u, sa_idx=%u inl_dev=%u",
450 inbound ? "inbound" : "outbound", eth_sec->spi,
451 eth_sec->sa_idx, eth_sec->inl_dev);
453 * Update fast path info in priv area.
455 set_sec_session_private_data(sess, (void *)sess_priv.u64);
459 if (inbound && inl_dev)
460 roc_nix_inl_dev_unlock();
461 rte_spinlock_unlock(lock);
463 rte_mempool_put(mempool, eth_sec);
470 cn10k_eth_sec_session_destroy(void *device, struct rte_security_session *sess)
472 struct rte_eth_dev *eth_dev = (struct rte_eth_dev *)device;
473 struct cnxk_eth_dev *dev = cnxk_eth_pmd_priv(eth_dev);
474 struct cnxk_eth_sec_sess *eth_sec;
475 struct rte_mempool *mp;
476 rte_spinlock_t *lock;
479 eth_sec = cnxk_eth_sec_sess_get_by_sess(dev, sess);
483 lock = eth_sec->inb ? &dev->inb.lock : &dev->outb.lock;
484 rte_spinlock_lock(lock);
486 if (eth_sec->inl_dev)
487 roc_nix_inl_dev_lock();
491 sa_dptr = dev->inb.sa_dptr;
492 roc_ot_ipsec_inb_sa_init(sa_dptr, true);
494 roc_nix_inl_ctx_write(&dev->nix, sa_dptr, eth_sec->sa,
496 sizeof(struct roc_ot_ipsec_inb_sa));
497 TAILQ_REMOVE(&dev->inb.list, eth_sec, entry);
501 sa_dptr = dev->outb.sa_dptr;
502 roc_ot_ipsec_outb_sa_init(sa_dptr);
504 roc_nix_inl_ctx_write(&dev->nix, sa_dptr, eth_sec->sa,
506 sizeof(struct roc_ot_ipsec_outb_sa));
507 /* Release Outbound SA index */
508 cnxk_eth_outb_sa_idx_put(dev, eth_sec->sa_idx);
509 TAILQ_REMOVE(&dev->outb.list, eth_sec, entry);
512 if (eth_sec->inl_dev)
513 roc_nix_inl_dev_unlock();
515 rte_spinlock_unlock(lock);
517 plt_nix_dbg("Destroyed %s session with spi=%u, sa_idx=%u, inl_dev=%u",
518 eth_sec->inb ? "inbound" : "outbound", eth_sec->spi,
519 eth_sec->sa_idx, eth_sec->inl_dev);
521 /* Put eth_sec object back to pool */
522 mp = rte_mempool_from_obj(eth_sec);
523 set_sec_session_private_data(sess, NULL);
524 rte_mempool_put(mp, eth_sec);
528 static const struct rte_security_capability *
529 cn10k_eth_sec_capabilities_get(void *device __rte_unused)
531 return cn10k_eth_sec_capabilities;
535 cn10k_eth_sec_ops_override(void)
537 static int init_once;
543 /* Update platform specific ops */
544 cnxk_eth_sec_ops.session_create = cn10k_eth_sec_session_create;
545 cnxk_eth_sec_ops.session_destroy = cn10k_eth_sec_session_destroy;
546 cnxk_eth_sec_ops.capabilities_get = cn10k_eth_sec_capabilities_get;