examples/ipsec-secgw: support inline protocol
[dpdk.git] / examples / ipsec-secgw / ipsec.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2016-2017 Intel Corporation
3  */
4 #include <sys/types.h>
5 #include <netinet/in.h>
6 #include <netinet/ip.h>
7
8 #include <rte_branch_prediction.h>
9 #include <rte_log.h>
10 #include <rte_crypto.h>
11 #include <rte_security.h>
12 #include <rte_cryptodev.h>
13 #include <rte_ethdev.h>
14 #include <rte_mbuf.h>
15 #include <rte_hash.h>
16
17 #include "ipsec.h"
18 #include "esp.h"
19
20 static inline void
21 set_ipsec_conf(struct ipsec_sa *sa, struct rte_security_ipsec_xform *ipsec)
22 {
23         if (ipsec->mode == RTE_SECURITY_IPSEC_SA_MODE_TUNNEL) {
24                 struct rte_security_ipsec_tunnel_param *tunnel =
25                                 &ipsec->tunnel;
26                 if (sa->flags == IP4_TUNNEL) {
27                         tunnel->type =
28                                 RTE_SECURITY_IPSEC_TUNNEL_IPV4;
29                         tunnel->ipv4.ttl = IPDEFTTL;
30
31                         memcpy((uint8_t *)&tunnel->ipv4.src_ip,
32                                 (uint8_t *)&sa->src.ip.ip4, 4);
33
34                         memcpy((uint8_t *)&tunnel->ipv4.dst_ip,
35                                 (uint8_t *)&sa->dst.ip.ip4, 4);
36                 }
37                 /* TODO support for Transport and IPV6 tunnel */
38         }
39 }
40
41 static inline int
42 create_session(struct ipsec_ctx *ipsec_ctx, struct ipsec_sa *sa)
43 {
44         struct rte_cryptodev_info cdev_info;
45         unsigned long cdev_id_qp = 0;
46         int32_t ret = 0;
47         struct cdev_key key = { 0 };
48
49         key.lcore_id = (uint8_t)rte_lcore_id();
50
51         key.cipher_algo = (uint8_t)sa->cipher_algo;
52         key.auth_algo = (uint8_t)sa->auth_algo;
53         key.aead_algo = (uint8_t)sa->aead_algo;
54
55         if (sa->type == RTE_SECURITY_ACTION_TYPE_NONE) {
56                 ret = rte_hash_lookup_data(ipsec_ctx->cdev_map, &key,
57                                 (void **)&cdev_id_qp);
58                 if (ret < 0) {
59                         RTE_LOG(ERR, IPSEC,
60                                 "No cryptodev: core %u, cipher_algo %u, "
61                                 "auth_algo %u, aead_algo %u\n",
62                                 key.lcore_id,
63                                 key.cipher_algo,
64                                 key.auth_algo,
65                                 key.aead_algo);
66                         return -1;
67                 }
68         }
69
70         RTE_LOG_DP(DEBUG, IPSEC, "Create session for SA spi %u on cryptodev "
71                         "%u qp %u\n", sa->spi,
72                         ipsec_ctx->tbl[cdev_id_qp].id,
73                         ipsec_ctx->tbl[cdev_id_qp].qp);
74
75         if (sa->type != RTE_SECURITY_ACTION_TYPE_NONE) {
76                 struct rte_security_session_conf sess_conf = {
77                         .action_type = sa->type,
78                         .protocol = RTE_SECURITY_PROTOCOL_IPSEC,
79                         {.ipsec = {
80                                 .spi = sa->spi,
81                                 .salt = sa->salt,
82                                 .options = { 0 },
83                                 .direction = sa->direction,
84                                 .proto = RTE_SECURITY_IPSEC_SA_PROTO_ESP,
85                                 .mode = (sa->flags == IP4_TUNNEL ||
86                                                 sa->flags == IP6_TUNNEL) ?
87                                         RTE_SECURITY_IPSEC_SA_MODE_TUNNEL :
88                                         RTE_SECURITY_IPSEC_SA_MODE_TRANSPORT,
89                         } },
90                         .crypto_xform = sa->xforms,
91                         .userdata = NULL,
92
93                 };
94
95                 if (sa->type == RTE_SECURITY_ACTION_TYPE_LOOKASIDE_PROTOCOL) {
96                         struct rte_security_ctx *ctx = (struct rte_security_ctx *)
97                                                         rte_cryptodev_get_sec_ctx(
98                                                         ipsec_ctx->tbl[cdev_id_qp].id);
99
100                         /* Set IPsec parameters in conf */
101                         set_ipsec_conf(sa, &(sess_conf.ipsec));
102
103                         sa->sec_session = rte_security_session_create(ctx,
104                                         &sess_conf, ipsec_ctx->session_pool);
105                         if (sa->sec_session == NULL) {
106                                 RTE_LOG(ERR, IPSEC,
107                                 "SEC Session init failed: err: %d\n", ret);
108                                 return -1;
109                         }
110                 } else if (sa->type == RTE_SECURITY_ACTION_TYPE_INLINE_CRYPTO) {
111                         struct rte_flow_error err;
112                         struct rte_security_ctx *ctx = (struct rte_security_ctx *)
113                                                         rte_eth_dev_get_sec_ctx(
114                                                         sa->portid);
115                         const struct rte_security_capability *sec_cap;
116
117                         sa->sec_session = rte_security_session_create(ctx,
118                                         &sess_conf, ipsec_ctx->session_pool);
119                         if (sa->sec_session == NULL) {
120                                 RTE_LOG(ERR, IPSEC,
121                                 "SEC Session init failed: err: %d\n", ret);
122                                 return -1;
123                         }
124
125                         sec_cap = rte_security_capabilities_get(ctx);
126
127                         /* iterate until ESP tunnel*/
128                         while (sec_cap->action !=
129                                         RTE_SECURITY_ACTION_TYPE_NONE) {
130
131                                 if (sec_cap->action == sa->type &&
132                                     sec_cap->protocol ==
133                                         RTE_SECURITY_PROTOCOL_IPSEC &&
134                                     sec_cap->ipsec.mode ==
135                                         RTE_SECURITY_IPSEC_SA_MODE_TUNNEL &&
136                                     sec_cap->ipsec.direction == sa->direction)
137                                         break;
138                                 sec_cap++;
139                         }
140
141                         if (sec_cap->action == RTE_SECURITY_ACTION_TYPE_NONE) {
142                                 RTE_LOG(ERR, IPSEC,
143                                 "No suitable security capability found\n");
144                                 return -1;
145                         }
146
147                         sa->ol_flags = sec_cap->ol_flags;
148                         sa->security_ctx = ctx;
149                         sa->pattern[0].type = RTE_FLOW_ITEM_TYPE_ETH;
150
151                         sa->pattern[1].type = RTE_FLOW_ITEM_TYPE_IPV4;
152                         sa->pattern[1].mask = &rte_flow_item_ipv4_mask;
153                         if (sa->flags & IP6_TUNNEL) {
154                                 sa->pattern[1].spec = &sa->ipv6_spec;
155                                 memcpy(sa->ipv6_spec.hdr.dst_addr,
156                                         sa->dst.ip.ip6.ip6_b, 16);
157                                 memcpy(sa->ipv6_spec.hdr.src_addr,
158                                        sa->src.ip.ip6.ip6_b, 16);
159                         } else {
160                                 sa->pattern[1].spec = &sa->ipv4_spec;
161                                 sa->ipv4_spec.hdr.dst_addr = sa->dst.ip.ip4;
162                                 sa->ipv4_spec.hdr.src_addr = sa->src.ip.ip4;
163                         }
164
165                         sa->pattern[2].type = RTE_FLOW_ITEM_TYPE_ESP;
166                         sa->pattern[2].spec = &sa->esp_spec;
167                         sa->pattern[2].mask = &rte_flow_item_esp_mask;
168                         sa->esp_spec.hdr.spi = sa->spi;
169
170                         sa->pattern[3].type = RTE_FLOW_ITEM_TYPE_END;
171
172                         sa->action[0].type = RTE_FLOW_ACTION_TYPE_SECURITY;
173                         sa->action[0].conf = sa->sec_session;
174
175                         sa->action[1].type = RTE_FLOW_ACTION_TYPE_END;
176
177                         sa->attr.egress = (sa->direction ==
178                                         RTE_SECURITY_IPSEC_SA_DIR_EGRESS);
179                         sa->flow = rte_flow_create(sa->portid,
180                                 &sa->attr, sa->pattern, sa->action, &err);
181                         if (sa->flow == NULL) {
182                                 RTE_LOG(ERR, IPSEC,
183                                         "Failed to create ipsec flow msg: %s\n",
184                                         err.message);
185                                 return -1;
186                         }
187                 } else if (sa->type ==
188                                 RTE_SECURITY_ACTION_TYPE_INLINE_PROTOCOL) {
189                         struct rte_security_ctx *ctx =
190                                         (struct rte_security_ctx *)
191                                         rte_eth_dev_get_sec_ctx(sa->portid);
192                         const struct rte_security_capability *sec_cap;
193
194                         if (ctx == NULL) {
195                                 RTE_LOG(ERR, IPSEC,
196                                 "Ethernet device doesn't have security features registered\n");
197                                 return -1;
198                         }
199
200                         /* Set IPsec parameters in conf */
201                         set_ipsec_conf(sa, &(sess_conf.ipsec));
202
203                         /* Save SA as userdata for the security session. When
204                          * the packet is received, this userdata will be
205                          * retrieved using the metadata from the packet.
206                          *
207                          * This is required only for inbound SAs.
208                          */
209
210                         if (sa->direction == RTE_SECURITY_IPSEC_SA_DIR_INGRESS)
211                                 sess_conf.userdata = (void *) sa;
212
213                         sa->sec_session = rte_security_session_create(ctx,
214                                         &sess_conf, ipsec_ctx->session_pool);
215                         if (sa->sec_session == NULL) {
216                                 RTE_LOG(ERR, IPSEC,
217                                 "SEC Session init failed: err: %d\n", ret);
218                                 return -1;
219                         }
220
221                         sec_cap = rte_security_capabilities_get(ctx);
222
223                         if (sec_cap == NULL) {
224                                 RTE_LOG(ERR, IPSEC,
225                                 "No capabilities registered\n");
226                                 return -1;
227                         }
228
229                         /* iterate until ESP tunnel*/
230                         while (sec_cap->action !=
231                                         RTE_SECURITY_ACTION_TYPE_NONE) {
232
233                                 if (sec_cap->action == sa->type &&
234                                     sec_cap->protocol ==
235                                         RTE_SECURITY_PROTOCOL_IPSEC &&
236                                     sec_cap->ipsec.mode ==
237                                         RTE_SECURITY_IPSEC_SA_MODE_TUNNEL &&
238                                     sec_cap->ipsec.direction == sa->direction)
239                                         break;
240                                 sec_cap++;
241                         }
242
243                         if (sec_cap->action == RTE_SECURITY_ACTION_TYPE_NONE) {
244                                 RTE_LOG(ERR, IPSEC,
245                                 "No suitable security capability found\n");
246                                 return -1;
247                         }
248
249                         sa->ol_flags = sec_cap->ol_flags;
250                         sa->security_ctx = ctx;
251                 }
252         } else {
253                 sa->crypto_session = rte_cryptodev_sym_session_create(
254                                 ipsec_ctx->session_pool);
255                 rte_cryptodev_sym_session_init(ipsec_ctx->tbl[cdev_id_qp].id,
256                                 sa->crypto_session, sa->xforms,
257                                 ipsec_ctx->session_pool);
258
259                 rte_cryptodev_info_get(ipsec_ctx->tbl[cdev_id_qp].id,
260                                 &cdev_info);
261                 if (cdev_info.sym.max_nb_sessions_per_qp > 0) {
262                         ret = rte_cryptodev_queue_pair_attach_sym_session(
263                                         ipsec_ctx->tbl[cdev_id_qp].id,
264                                         ipsec_ctx->tbl[cdev_id_qp].qp,
265                                         sa->crypto_session);
266                         if (ret < 0) {
267                                 RTE_LOG(ERR, IPSEC,
268                                         "Session cannot be attached to qp %u\n",
269                                         ipsec_ctx->tbl[cdev_id_qp].qp);
270                                 return -1;
271                         }
272                 }
273         }
274         sa->cdev_id_qp = cdev_id_qp;
275
276         return 0;
277 }
278
279 static inline void
280 enqueue_cop(struct cdev_qp *cqp, struct rte_crypto_op *cop)
281 {
282         int32_t ret, i;
283
284         cqp->buf[cqp->len++] = cop;
285
286         if (cqp->len == MAX_PKT_BURST) {
287                 ret = rte_cryptodev_enqueue_burst(cqp->id, cqp->qp,
288                                 cqp->buf, cqp->len);
289                 if (ret < cqp->len) {
290                         RTE_LOG_DP(DEBUG, IPSEC, "Cryptodev %u queue %u:"
291                                         " enqueued %u crypto ops out of %u\n",
292                                          cqp->id, cqp->qp,
293                                          ret, cqp->len);
294                         for (i = ret; i < cqp->len; i++)
295                                 rte_pktmbuf_free(cqp->buf[i]->sym->m_src);
296                 }
297                 cqp->in_flight += ret;
298                 cqp->len = 0;
299         }
300 }
301
302 static inline void
303 ipsec_enqueue(ipsec_xform_fn xform_func, struct ipsec_ctx *ipsec_ctx,
304                 struct rte_mbuf *pkts[], struct ipsec_sa *sas[],
305                 uint16_t nb_pkts)
306 {
307         int32_t ret = 0, i;
308         struct ipsec_mbuf_metadata *priv;
309         struct rte_crypto_sym_op *sym_cop;
310         struct ipsec_sa *sa;
311         struct cdev_qp *cqp;
312
313         for (i = 0; i < nb_pkts; i++) {
314                 if (unlikely(sas[i] == NULL)) {
315                         rte_pktmbuf_free(pkts[i]);
316                         continue;
317                 }
318
319                 rte_prefetch0(sas[i]);
320                 rte_prefetch0(pkts[i]);
321
322                 priv = get_priv(pkts[i]);
323                 sa = sas[i];
324                 priv->sa = sa;
325
326                 switch (sa->type) {
327                 case RTE_SECURITY_ACTION_TYPE_LOOKASIDE_PROTOCOL:
328                         priv->cop.type = RTE_CRYPTO_OP_TYPE_SYMMETRIC;
329                         priv->cop.status = RTE_CRYPTO_OP_STATUS_NOT_PROCESSED;
330
331                         rte_prefetch0(&priv->sym_cop);
332
333                         if ((unlikely(sa->sec_session == NULL)) &&
334                                         create_session(ipsec_ctx, sa)) {
335                                 rte_pktmbuf_free(pkts[i]);
336                                 continue;
337                         }
338
339                         sym_cop = get_sym_cop(&priv->cop);
340                         sym_cop->m_src = pkts[i];
341
342                         rte_security_attach_session(&priv->cop,
343                                         sa->sec_session);
344                         break;
345                 case RTE_SECURITY_ACTION_TYPE_NONE:
346
347                         priv->cop.type = RTE_CRYPTO_OP_TYPE_SYMMETRIC;
348                         priv->cop.status = RTE_CRYPTO_OP_STATUS_NOT_PROCESSED;
349
350                         rte_prefetch0(&priv->sym_cop);
351
352                         if ((unlikely(sa->crypto_session == NULL)) &&
353                                         create_session(ipsec_ctx, sa)) {
354                                 rte_pktmbuf_free(pkts[i]);
355                                 continue;
356                         }
357
358                         rte_crypto_op_attach_sym_session(&priv->cop,
359                                         sa->crypto_session);
360
361                         ret = xform_func(pkts[i], sa, &priv->cop);
362                         if (unlikely(ret)) {
363                                 rte_pktmbuf_free(pkts[i]);
364                                 continue;
365                         }
366                         break;
367                 case RTE_SECURITY_ACTION_TYPE_INLINE_PROTOCOL:
368                         if ((unlikely(sa->sec_session == NULL)) &&
369                                         create_session(ipsec_ctx, sa)) {
370                                 rte_pktmbuf_free(pkts[i]);
371                                 continue;
372                         }
373
374                         cqp = &ipsec_ctx->tbl[sa->cdev_id_qp];
375                         cqp->ol_pkts[cqp->ol_pkts_cnt++] = pkts[i];
376                         if (sa->ol_flags & RTE_SECURITY_TX_OLOAD_NEED_MDATA)
377                                 rte_security_set_pkt_metadata(
378                                                 sa->security_ctx,
379                                                 sa->sec_session, pkts[i], NULL);
380                         continue;
381                 case RTE_SECURITY_ACTION_TYPE_INLINE_CRYPTO:
382                         priv->cop.type = RTE_CRYPTO_OP_TYPE_SYMMETRIC;
383                         priv->cop.status = RTE_CRYPTO_OP_STATUS_NOT_PROCESSED;
384
385                         rte_prefetch0(&priv->sym_cop);
386
387                         if ((unlikely(sa->sec_session == NULL)) &&
388                                         create_session(ipsec_ctx, sa)) {
389                                 rte_pktmbuf_free(pkts[i]);
390                                 continue;
391                         }
392
393                         rte_security_attach_session(&priv->cop,
394                                         sa->sec_session);
395
396                         ret = xform_func(pkts[i], sa, &priv->cop);
397                         if (unlikely(ret)) {
398                                 rte_pktmbuf_free(pkts[i]);
399                                 continue;
400                         }
401
402                         cqp = &ipsec_ctx->tbl[sa->cdev_id_qp];
403                         cqp->ol_pkts[cqp->ol_pkts_cnt++] = pkts[i];
404                         if (sa->ol_flags & RTE_SECURITY_TX_OLOAD_NEED_MDATA)
405                                 rte_security_set_pkt_metadata(
406                                                 sa->security_ctx,
407                                                 sa->sec_session, pkts[i], NULL);
408                         continue;
409                 }
410
411                 RTE_ASSERT(sa->cdev_id_qp < ipsec_ctx->nb_qps);
412                 enqueue_cop(&ipsec_ctx->tbl[sa->cdev_id_qp], &priv->cop);
413         }
414 }
415
416 static inline int
417 ipsec_dequeue(ipsec_xform_fn xform_func, struct ipsec_ctx *ipsec_ctx,
418               struct rte_mbuf *pkts[], uint16_t max_pkts)
419 {
420         int32_t nb_pkts = 0, ret = 0, i, j, nb_cops;
421         struct ipsec_mbuf_metadata *priv;
422         struct rte_crypto_op *cops[max_pkts];
423         struct ipsec_sa *sa;
424         struct rte_mbuf *pkt;
425
426         for (i = 0; i < ipsec_ctx->nb_qps && nb_pkts < max_pkts; i++) {
427                 struct cdev_qp *cqp;
428
429                 cqp = &ipsec_ctx->tbl[ipsec_ctx->last_qp++];
430                 if (ipsec_ctx->last_qp == ipsec_ctx->nb_qps)
431                         ipsec_ctx->last_qp %= ipsec_ctx->nb_qps;
432
433                 while (cqp->ol_pkts_cnt > 0 && nb_pkts < max_pkts) {
434                         pkt = cqp->ol_pkts[--cqp->ol_pkts_cnt];
435                         rte_prefetch0(pkt);
436                         priv = get_priv(pkt);
437                         sa = priv->sa;
438                         ret = xform_func(pkt, sa, &priv->cop);
439                         if (unlikely(ret)) {
440                                 rte_pktmbuf_free(pkt);
441                                 continue;
442                         }
443                         pkts[nb_pkts++] = pkt;
444                 }
445
446                 if (cqp->in_flight == 0)
447                         continue;
448
449                 nb_cops = rte_cryptodev_dequeue_burst(cqp->id, cqp->qp,
450                                 cops, max_pkts - nb_pkts);
451
452                 cqp->in_flight -= nb_cops;
453
454                 for (j = 0; j < nb_cops; j++) {
455                         pkt = cops[j]->sym->m_src;
456                         rte_prefetch0(pkt);
457
458                         priv = get_priv(pkt);
459                         sa = priv->sa;
460
461                         RTE_ASSERT(sa != NULL);
462
463                         if (sa->type == RTE_SECURITY_ACTION_TYPE_NONE) {
464                                 ret = xform_func(pkt, sa, cops[j]);
465                                 if (unlikely(ret)) {
466                                         rte_pktmbuf_free(pkt);
467                                         continue;
468                                 }
469                         }
470                         pkts[nb_pkts++] = pkt;
471                 }
472         }
473
474         /* return packets */
475         return nb_pkts;
476 }
477
478 uint16_t
479 ipsec_inbound(struct ipsec_ctx *ctx, struct rte_mbuf *pkts[],
480                 uint16_t nb_pkts, uint16_t len)
481 {
482         struct ipsec_sa *sas[nb_pkts];
483
484         inbound_sa_lookup(ctx->sa_ctx, pkts, sas, nb_pkts);
485
486         ipsec_enqueue(esp_inbound, ctx, pkts, sas, nb_pkts);
487
488         return ipsec_dequeue(esp_inbound_post, ctx, pkts, len);
489 }
490
491 uint16_t
492 ipsec_outbound(struct ipsec_ctx *ctx, struct rte_mbuf *pkts[],
493                 uint32_t sa_idx[], uint16_t nb_pkts, uint16_t len)
494 {
495         struct ipsec_sa *sas[nb_pkts];
496
497         outbound_sa_lookup(ctx->sa_ctx, sa_idx, sas, nb_pkts);
498
499         ipsec_enqueue(esp_outbound, ctx, pkts, sas, nb_pkts);
500
501         return ipsec_dequeue(esp_outbound_post, ctx, pkts, len);
502 }