net/mlx5: add C++ include guard to public header
[dpdk.git] / drivers / net / cnxk / cn10k_ethdev_sec.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(C) 2021 Marvell.
3  */
4
5 #include <rte_cryptodev.h>
6 #include <rte_eventdev.h>
7 #include <rte_security.h>
8 #include <rte_security_driver.h>
9
10 #include <cn10k_ethdev.h>
11 #include <cnxk_security.h>
12
13 static struct rte_cryptodev_capabilities cn10k_eth_sec_crypto_caps[] = {
14         {       /* AES GCM */
15                 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
16                 {.sym = {
17                         .xform_type = RTE_CRYPTO_SYM_XFORM_AEAD,
18                         {.aead = {
19                                 .algo = RTE_CRYPTO_AEAD_AES_GCM,
20                                 .block_size = 16,
21                                 .key_size = {
22                                         .min = 16,
23                                         .max = 32,
24                                         .increment = 8
25                                 },
26                                 .digest_size = {
27                                         .min = 16,
28                                         .max = 16,
29                                         .increment = 0
30                                 },
31                                 .aad_size = {
32                                         .min = 8,
33                                         .max = 12,
34                                         .increment = 4
35                                 },
36                                 .iv_size = {
37                                         .min = 12,
38                                         .max = 12,
39                                         .increment = 0
40                                 }
41                         }, }
42                 }, }
43         },
44         {       /* AES CBC */
45                 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
46                 {.sym = {
47                         .xform_type = RTE_CRYPTO_SYM_XFORM_CIPHER,
48                         {.cipher = {
49                                 .algo = RTE_CRYPTO_CIPHER_AES_CBC,
50                                 .block_size = 16,
51                                 .key_size = {
52                                         .min = 16,
53                                         .max = 32,
54                                         .increment = 8
55                                 },
56                                 .iv_size = {
57                                         .min = 16,
58                                         .max = 16,
59                                         .increment = 0
60                                 }
61                         }, }
62                 }, }
63         },
64         {       /* SHA1 HMAC */
65                 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
66                 {.sym = {
67                         .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH,
68                         {.auth = {
69                                 .algo = RTE_CRYPTO_AUTH_SHA1_HMAC,
70                                 .block_size = 64,
71                                 .key_size = {
72                                         .min = 20,
73                                         .max = 64,
74                                         .increment = 1
75                                 },
76                                 .digest_size = {
77                                         .min = 12,
78                                         .max = 12,
79                                         .increment = 0
80                                 },
81                         }, }
82                 }, }
83         },
84         RTE_CRYPTODEV_END_OF_CAPABILITIES_LIST()
85 };
86
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,
91                 .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,
95                         .options = { 0 }
96                 },
97                 .crypto_capabilities = cn10k_eth_sec_crypto_caps,
98                 .ol_flags = RTE_SECURITY_TX_OLOAD_NEED_MDATA
99         },
100         {       /* IPsec Inline Protocol ESP Tunnel Egress */
101                 .action = RTE_SECURITY_ACTION_TYPE_INLINE_PROTOCOL,
102                 .protocol = RTE_SECURITY_PROTOCOL_IPSEC,
103                 .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,
107                         .options = { 0 }
108                 },
109                 .crypto_capabilities = cn10k_eth_sec_crypto_caps,
110                 .ol_flags = RTE_SECURITY_TX_OLOAD_NEED_MDATA
111         },
112         {       /* IPsec Inline Protocol ESP Transport Egress */
113                 .action = RTE_SECURITY_ACTION_TYPE_INLINE_PROTOCOL,
114                 .protocol = RTE_SECURITY_PROTOCOL_IPSEC,
115                 .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,
119                         .options = { 0 }
120                 },
121                 .crypto_capabilities = cn10k_eth_sec_crypto_caps,
122                 .ol_flags = RTE_SECURITY_TX_OLOAD_NEED_MDATA
123         },
124         {       /* IPsec Inline Protocol ESP Transport Ingress */
125                 .action = RTE_SECURITY_ACTION_TYPE_INLINE_PROTOCOL,
126                 .protocol = RTE_SECURITY_PROTOCOL_IPSEC,
127                 .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,
131                         .options = { 0 }
132                 },
133                 .crypto_capabilities = cn10k_eth_sec_crypto_caps,
134                 .ol_flags = RTE_SECURITY_TX_OLOAD_NEED_MDATA
135         },
136         {
137                 .action = RTE_SECURITY_ACTION_TYPE_NONE
138         }
139 };
140
141 static void
142 cn10k_eth_sec_sso_work_cb(uint64_t *gw, void *args)
143 {
144         struct rte_eth_event_ipsec_desc desc;
145         struct cn10k_sec_sess_priv sess_priv;
146         struct cn10k_outb_priv_data *priv;
147         struct roc_ot_ipsec_outb_sa *sa;
148         struct cpt_cn10k_res_s *res;
149         struct rte_eth_dev *eth_dev;
150         struct cnxk_eth_dev *dev;
151         uint16_t dlen_adj, rlen;
152         struct rte_mbuf *mbuf;
153         uintptr_t sa_base;
154         uintptr_t nixtx;
155         uint8_t port;
156
157         RTE_SET_USED(args);
158
159         switch ((gw[0] >> 28) & 0xF) {
160         case RTE_EVENT_TYPE_ETHDEV:
161                 /* Event from inbound inline dev due to IPSEC packet bad L4 */
162                 mbuf = (struct rte_mbuf *)(gw[1] - sizeof(struct rte_mbuf));
163                 plt_nix_dbg("Received mbuf %p from inline dev inbound", mbuf);
164                 rte_pktmbuf_free(mbuf);
165                 return;
166         case RTE_EVENT_TYPE_CPU:
167                 /* Check for subtype */
168                 if (((gw[0] >> 20) & 0xFF) == CNXK_ETHDEV_SEC_OUTB_EV_SUB) {
169                         /* Event from outbound inline error */
170                         mbuf = (struct rte_mbuf *)gw[1];
171                         break;
172                 }
173                 /* Fall through */
174         default:
175                 plt_err("Unknown event gw[0] = 0x%016lx, gw[1] = 0x%016lx",
176                         gw[0], gw[1]);
177                 return;
178         }
179
180         /* Get ethdev port from tag */
181         port = gw[0] & 0xFF;
182         eth_dev = &rte_eth_devices[port];
183         dev = cnxk_eth_pmd_priv(eth_dev);
184
185         sess_priv.u64 = *rte_security_dynfield(mbuf);
186         /* Calculate dlen adj */
187         dlen_adj = mbuf->pkt_len - mbuf->l2_len;
188         rlen = (dlen_adj + sess_priv.roundup_len) +
189                (sess_priv.roundup_byte - 1);
190         rlen &= ~(uint64_t)(sess_priv.roundup_byte - 1);
191         rlen += sess_priv.partial_len;
192         dlen_adj = rlen - dlen_adj;
193
194         /* Find the res area residing on next cacheline after end of data */
195         nixtx = rte_pktmbuf_mtod(mbuf, uintptr_t) + mbuf->pkt_len + dlen_adj;
196         nixtx += BIT_ULL(7);
197         nixtx = (nixtx - 1) & ~(BIT_ULL(7) - 1);
198         res = (struct cpt_cn10k_res_s *)nixtx;
199
200         plt_nix_dbg("Outbound error, mbuf %p, sa_index %u, compcode %x uc %x",
201                     mbuf, sess_priv.sa_idx, res->compcode, res->uc_compcode);
202
203         sess_priv.u64 = *rte_security_dynfield(mbuf);
204
205         sa_base = dev->outb.sa_base;
206         sa = roc_nix_inl_ot_ipsec_outb_sa(sa_base, sess_priv.sa_idx);
207         priv = roc_nix_inl_ot_ipsec_outb_sa_sw_rsvd(sa);
208
209         memset(&desc, 0, sizeof(desc));
210
211         switch (res->uc_compcode) {
212         case ROC_IE_OT_UCC_ERR_SA_OVERFLOW:
213                 desc.subtype = RTE_ETH_EVENT_IPSEC_ESN_OVERFLOW;
214                 break;
215         default:
216                 plt_warn("Outbound error, mbuf %p, sa_index %u, "
217                          "compcode %x uc %x", mbuf, sess_priv.sa_idx,
218                          res->compcode, res->uc_compcode);
219                 desc.subtype = RTE_ETH_EVENT_IPSEC_UNKNOWN;
220                 break;
221         }
222
223         desc.metadata = (uint64_t)priv->userdata;
224         rte_eth_dev_callback_process(eth_dev, RTE_ETH_EVENT_IPSEC, &desc);
225         rte_pktmbuf_free(mbuf);
226 }
227
228 static int
229 cn10k_eth_sec_session_create(void *device,
230                              struct rte_security_session_conf *conf,
231                              struct rte_security_session *sess,
232                              struct rte_mempool *mempool)
233 {
234         struct rte_eth_dev *eth_dev = (struct rte_eth_dev *)device;
235         struct cnxk_eth_dev *dev = cnxk_eth_pmd_priv(eth_dev);
236         struct rte_security_ipsec_xform *ipsec;
237         struct cn10k_sec_sess_priv sess_priv;
238         struct rte_crypto_sym_xform *crypto;
239         struct cnxk_eth_sec_sess *eth_sec;
240         bool inbound, inl_dev;
241         rte_spinlock_t *lock;
242         char tbuf[128] = {0};
243         int rc = 0;
244
245         if (conf->action_type != RTE_SECURITY_ACTION_TYPE_INLINE_PROTOCOL)
246                 return -ENOTSUP;
247
248         if (conf->protocol != RTE_SECURITY_PROTOCOL_IPSEC)
249                 return -ENOTSUP;
250
251         if (rte_security_dynfield_register() < 0)
252                 return -ENOTSUP;
253
254         if (rte_eal_process_type() == RTE_PROC_PRIMARY)
255                 roc_nix_inl_cb_register(cn10k_eth_sec_sso_work_cb, NULL);
256
257         ipsec = &conf->ipsec;
258         crypto = conf->crypto_xform;
259         inbound = !!(ipsec->direction == RTE_SECURITY_IPSEC_SA_DIR_INGRESS);
260         inl_dev = !!dev->inb.inl_dev;
261
262         /* Search if a session already exits */
263         if (cnxk_eth_sec_sess_get_by_spi(dev, ipsec->spi, inbound)) {
264                 plt_err("%s SA with SPI %u already in use",
265                         inbound ? "Inbound" : "Outbound", ipsec->spi);
266                 return -EEXIST;
267         }
268
269         if (rte_mempool_get(mempool, (void **)&eth_sec)) {
270                 plt_err("Could not allocate security session private data");
271                 return -ENOMEM;
272         }
273
274         memset(eth_sec, 0, sizeof(struct cnxk_eth_sec_sess));
275         sess_priv.u64 = 0;
276
277         lock = inbound ? &dev->inb.lock : &dev->outb.lock;
278         rte_spinlock_lock(lock);
279
280         /* Acquire lock on inline dev for inbound */
281         if (inbound && inl_dev)
282                 roc_nix_inl_dev_lock();
283
284         if (inbound) {
285                 struct roc_ot_ipsec_inb_sa *inb_sa, *inb_sa_dptr;
286                 struct cn10k_inb_priv_data *inb_priv;
287                 uintptr_t sa;
288
289                 PLT_STATIC_ASSERT(sizeof(struct cn10k_inb_priv_data) <
290                                   ROC_NIX_INL_OT_IPSEC_INB_SW_RSVD);
291
292                 /* Get Inbound SA from NIX_RX_IPSEC_SA_BASE */
293                 sa = roc_nix_inl_inb_sa_get(&dev->nix, inl_dev, ipsec->spi);
294                 if (!sa && dev->inb.inl_dev) {
295                         snprintf(tbuf, sizeof(tbuf),
296                                  "Failed to create ingress sa, inline dev "
297                                  "not found or spi not in range");
298                         rc = -ENOTSUP;
299                         goto mempool_put;
300                 } else if (!sa) {
301                         snprintf(tbuf, sizeof(tbuf),
302                                  "Failed to create ingress sa");
303                         rc = -EFAULT;
304                         goto mempool_put;
305                 }
306
307                 inb_sa = (struct roc_ot_ipsec_inb_sa *)sa;
308
309                 /* Check if SA is already in use */
310                 if (inb_sa->w2.s.valid) {
311                         snprintf(tbuf, sizeof(tbuf),
312                                  "Inbound SA with SPI %u already in use",
313                                  ipsec->spi);
314                         rc = -EBUSY;
315                         goto mempool_put;
316                 }
317
318                 inb_sa_dptr = (struct roc_ot_ipsec_inb_sa *)dev->inb.sa_dptr;
319                 memset(inb_sa_dptr, 0, sizeof(struct roc_ot_ipsec_inb_sa));
320
321                 /* Fill inbound sa params */
322                 rc = cnxk_ot_ipsec_inb_sa_fill(inb_sa_dptr, ipsec, crypto);
323                 if (rc) {
324                         snprintf(tbuf, sizeof(tbuf),
325                                  "Failed to init inbound sa, rc=%d", rc);
326                         goto mempool_put;
327                 }
328
329                 inb_priv = roc_nix_inl_ot_ipsec_inb_sa_sw_rsvd(inb_sa);
330                 /* Back pointer to get eth_sec */
331                 inb_priv->eth_sec = eth_sec;
332                 /* Save userdata in inb private area */
333                 inb_priv->userdata = conf->userdata;
334
335                 /* Save SA index/SPI in cookie for now */
336                 inb_sa_dptr->w1.s.cookie = rte_cpu_to_be_32(ipsec->spi);
337
338                 /* Prepare session priv */
339                 sess_priv.inb_sa = 1;
340                 sess_priv.sa_idx = ipsec->spi;
341
342                 /* Pointer from eth_sec -> inb_sa */
343                 eth_sec->sa = inb_sa;
344                 eth_sec->sess = sess;
345                 eth_sec->sa_idx = ipsec->spi;
346                 eth_sec->spi = ipsec->spi;
347                 eth_sec->inl_dev = !!dev->inb.inl_dev;
348                 eth_sec->inb = true;
349
350                 TAILQ_INSERT_TAIL(&dev->inb.list, eth_sec, entry);
351                 dev->inb.nb_sess++;
352                 /* Sync session in context cache */
353                 rc = roc_nix_inl_ctx_write(&dev->nix, inb_sa_dptr, eth_sec->sa,
354                                            eth_sec->inb,
355                                            sizeof(struct roc_ot_ipsec_inb_sa));
356                 if (rc)
357                         goto mempool_put;
358         } else {
359                 struct roc_ot_ipsec_outb_sa *outb_sa, *outb_sa_dptr;
360                 struct cn10k_outb_priv_data *outb_priv;
361                 struct cnxk_ipsec_outb_rlens *rlens;
362                 uint64_t sa_base = dev->outb.sa_base;
363                 uint32_t sa_idx;
364
365                 PLT_STATIC_ASSERT(sizeof(struct cn10k_outb_priv_data) <
366                                   ROC_NIX_INL_OT_IPSEC_OUTB_SW_RSVD);
367
368                 /* Alloc an sa index */
369                 rc = cnxk_eth_outb_sa_idx_get(dev, &sa_idx);
370                 if (rc)
371                         goto mempool_put;
372
373                 outb_sa = roc_nix_inl_ot_ipsec_outb_sa(sa_base, sa_idx);
374                 outb_priv = roc_nix_inl_ot_ipsec_outb_sa_sw_rsvd(outb_sa);
375                 rlens = &outb_priv->rlens;
376
377                 outb_sa_dptr = (struct roc_ot_ipsec_outb_sa *)dev->outb.sa_dptr;
378                 memset(outb_sa_dptr, 0, sizeof(struct roc_ot_ipsec_outb_sa));
379
380                 /* Fill outbound sa params */
381                 rc = cnxk_ot_ipsec_outb_sa_fill(outb_sa_dptr, ipsec, crypto);
382                 if (rc) {
383                         snprintf(tbuf, sizeof(tbuf),
384                                  "Failed to init outbound sa, rc=%d", rc);
385                         rc |= cnxk_eth_outb_sa_idx_put(dev, sa_idx);
386                         goto mempool_put;
387                 }
388
389                 /* Save userdata */
390                 outb_priv->userdata = conf->userdata;
391                 outb_priv->sa_idx = sa_idx;
392                 outb_priv->eth_sec = eth_sec;
393
394                 /* Save rlen info */
395                 cnxk_ipsec_outb_rlens_get(rlens, ipsec, crypto);
396
397                 /* Prepare session priv */
398                 sess_priv.sa_idx = outb_priv->sa_idx;
399                 sess_priv.roundup_byte = rlens->roundup_byte;
400                 sess_priv.roundup_len = rlens->roundup_len;
401                 sess_priv.partial_len = rlens->partial_len;
402                 sess_priv.mode = outb_sa_dptr->w2.s.ipsec_mode;
403                 sess_priv.outer_ip_ver = outb_sa_dptr->w2.s.outer_ip_ver;
404
405                 /* Pointer from eth_sec -> outb_sa */
406                 eth_sec->sa = outb_sa;
407                 eth_sec->sess = sess;
408                 eth_sec->sa_idx = sa_idx;
409                 eth_sec->spi = ipsec->spi;
410
411                 TAILQ_INSERT_TAIL(&dev->outb.list, eth_sec, entry);
412                 dev->outb.nb_sess++;
413                 /* Sync session in context cache */
414                 rc = roc_nix_inl_ctx_write(&dev->nix, outb_sa_dptr, eth_sec->sa,
415                                            eth_sec->inb,
416                                            sizeof(struct roc_ot_ipsec_outb_sa));
417                 if (rc)
418                         goto mempool_put;
419         }
420         if (inbound && inl_dev)
421                 roc_nix_inl_dev_unlock();
422         rte_spinlock_unlock(lock);
423
424         plt_nix_dbg("Created %s session with spi=%u, sa_idx=%u inl_dev=%u",
425                     inbound ? "inbound" : "outbound", eth_sec->spi,
426                     eth_sec->sa_idx, eth_sec->inl_dev);
427         /*
428          * Update fast path info in priv area.
429          */
430         set_sec_session_private_data(sess, (void *)sess_priv.u64);
431
432         return 0;
433 mempool_put:
434         if (inbound && inl_dev)
435                 roc_nix_inl_dev_unlock();
436         rte_spinlock_unlock(lock);
437
438         rte_mempool_put(mempool, eth_sec);
439         if (rc)
440                 plt_err("%s", tbuf);
441         return rc;
442 }
443
444 static int
445 cn10k_eth_sec_session_destroy(void *device, struct rte_security_session *sess)
446 {
447         struct rte_eth_dev *eth_dev = (struct rte_eth_dev *)device;
448         struct cnxk_eth_dev *dev = cnxk_eth_pmd_priv(eth_dev);
449         struct cnxk_eth_sec_sess *eth_sec;
450         struct rte_mempool *mp;
451         rte_spinlock_t *lock;
452         void *sa_dptr;
453
454         eth_sec = cnxk_eth_sec_sess_get_by_sess(dev, sess);
455         if (!eth_sec)
456                 return -ENOENT;
457
458         lock = eth_sec->inb ? &dev->inb.lock : &dev->outb.lock;
459         rte_spinlock_lock(lock);
460
461         if (eth_sec->inl_dev)
462                 roc_nix_inl_dev_lock();
463
464         if (eth_sec->inb) {
465                 /* Disable SA */
466                 sa_dptr = dev->inb.sa_dptr;
467                 roc_nix_inl_inb_sa_init(sa_dptr);
468
469                 roc_nix_inl_ctx_write(&dev->nix, sa_dptr, eth_sec->sa,
470                                       eth_sec->inb,
471                                       sizeof(struct roc_ot_ipsec_inb_sa));
472                 TAILQ_REMOVE(&dev->inb.list, eth_sec, entry);
473                 dev->inb.nb_sess--;
474         } else {
475                 /* Disable SA */
476                 sa_dptr = dev->outb.sa_dptr;
477                 roc_nix_inl_outb_sa_init(sa_dptr);
478
479                 roc_nix_inl_ctx_write(&dev->nix, sa_dptr, eth_sec->sa,
480                                       eth_sec->inb,
481                                       sizeof(struct roc_ot_ipsec_outb_sa));
482                 /* Release Outbound SA index */
483                 cnxk_eth_outb_sa_idx_put(dev, eth_sec->sa_idx);
484                 TAILQ_REMOVE(&dev->outb.list, eth_sec, entry);
485                 dev->outb.nb_sess--;
486         }
487         if (eth_sec->inl_dev)
488                 roc_nix_inl_dev_unlock();
489
490         rte_spinlock_unlock(lock);
491
492         plt_nix_dbg("Destroyed %s session with spi=%u, sa_idx=%u, inl_dev=%u",
493                     eth_sec->inb ? "inbound" : "outbound", eth_sec->spi,
494                     eth_sec->sa_idx, eth_sec->inl_dev);
495
496         /* Put eth_sec object back to pool */
497         mp = rte_mempool_from_obj(eth_sec);
498         set_sec_session_private_data(sess, NULL);
499         rte_mempool_put(mp, eth_sec);
500         return 0;
501 }
502
503 static const struct rte_security_capability *
504 cn10k_eth_sec_capabilities_get(void *device __rte_unused)
505 {
506         return cn10k_eth_sec_capabilities;
507 }
508
509 void
510 cn10k_eth_sec_ops_override(void)
511 {
512         static int init_once;
513
514         if (init_once)
515                 return;
516         init_once = 1;
517
518         /* Update platform specific ops */
519         cnxk_eth_sec_ops.session_create = cn10k_eth_sec_session_create;
520         cnxk_eth_sec_ops.session_destroy = cn10k_eth_sec_session_destroy;
521         cnxk_eth_sec_ops.capabilities_get = cn10k_eth_sec_capabilities_get;
522 }