c24284d60eeeb0fae94c3ffcc6342d520e1cbff6
[dpdk.git] / examples / ipsec-secgw / ipsec.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2016-2017 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 #include <sys/types.h>
34 #include <netinet/in.h>
35 #include <netinet/ip.h>
36
37 #include <rte_branch_prediction.h>
38 #include <rte_log.h>
39 #include <rte_crypto.h>
40 #include <rte_security.h>
41 #include <rte_cryptodev.h>
42 #include <rte_ethdev.h>
43 #include <rte_mbuf.h>
44 #include <rte_hash.h>
45
46 #include "ipsec.h"
47 #include "esp.h"
48
49 static inline int
50 create_session(struct ipsec_ctx *ipsec_ctx, struct ipsec_sa *sa)
51 {
52         struct rte_cryptodev_info cdev_info;
53         unsigned long cdev_id_qp = 0;
54         int32_t ret = 0;
55         struct cdev_key key = { 0 };
56
57         key.lcore_id = (uint8_t)rte_lcore_id();
58
59         key.cipher_algo = (uint8_t)sa->cipher_algo;
60         key.auth_algo = (uint8_t)sa->auth_algo;
61         key.aead_algo = (uint8_t)sa->aead_algo;
62
63         if (sa->type == RTE_SECURITY_ACTION_TYPE_NONE) {
64                 ret = rte_hash_lookup_data(ipsec_ctx->cdev_map, &key,
65                                 (void **)&cdev_id_qp);
66                 if (ret < 0) {
67                         RTE_LOG(ERR, IPSEC,
68                                 "No cryptodev: core %u, cipher_algo %u, "
69                                 "auth_algo %u, aead_algo %u\n",
70                                 key.lcore_id,
71                                 key.cipher_algo,
72                                 key.auth_algo,
73                                 key.aead_algo);
74                         return -1;
75                 }
76         }
77
78         RTE_LOG_DP(DEBUG, IPSEC, "Create session for SA spi %u on cryptodev "
79                         "%u qp %u\n", sa->spi,
80                         ipsec_ctx->tbl[cdev_id_qp].id,
81                         ipsec_ctx->tbl[cdev_id_qp].qp);
82
83         if (sa->type != RTE_SECURITY_ACTION_TYPE_NONE) {
84                 struct rte_security_session_conf sess_conf = {
85                         .action_type = sa->type,
86                         .protocol = RTE_SECURITY_PROTOCOL_IPSEC,
87                         .ipsec = {
88                                 .spi = sa->spi,
89                                 .salt = sa->salt,
90                                 .options = { 0 },
91                                 .direction = sa->direction,
92                                 .proto = RTE_SECURITY_IPSEC_SA_PROTO_ESP,
93                                 .mode = (sa->flags == IP4_TUNNEL ||
94                                                 sa->flags == IP6_TUNNEL) ?
95                                         RTE_SECURITY_IPSEC_SA_MODE_TUNNEL :
96                                         RTE_SECURITY_IPSEC_SA_MODE_TRANSPORT,
97                         },
98                         .crypto_xform = sa->xforms
99
100                 };
101
102                 if (sa->type == RTE_SECURITY_ACTION_TYPE_LOOKASIDE_PROTOCOL) {
103                         struct rte_security_ctx *ctx = (struct rte_security_ctx *)
104                                                         rte_cryptodev_get_sec_ctx(
105                                                         ipsec_ctx->tbl[cdev_id_qp].id);
106
107                         if (sess_conf.ipsec.mode ==
108                                         RTE_SECURITY_IPSEC_SA_MODE_TUNNEL) {
109                                 struct rte_security_ipsec_tunnel_param *tunnel =
110                                                 &sess_conf.ipsec.tunnel;
111                                 if (sa->flags == IP4_TUNNEL) {
112                                         tunnel->type =
113                                                 RTE_SECURITY_IPSEC_TUNNEL_IPV4;
114                                         tunnel->ipv4.ttl = IPDEFTTL;
115
116                                         memcpy((uint8_t *)&tunnel->ipv4.src_ip,
117                                                 (uint8_t *)&sa->src.ip.ip4, 4);
118
119                                         memcpy((uint8_t *)&tunnel->ipv4.dst_ip,
120                                                 (uint8_t *)&sa->dst.ip.ip4, 4);
121                                 }
122                                 /* TODO support for Transport and IPV6 tunnel */
123                         }
124
125                         sa->sec_session = rte_security_session_create(ctx,
126                                         &sess_conf, ipsec_ctx->session_pool);
127                         if (sa->sec_session == NULL) {
128                                 RTE_LOG(ERR, IPSEC,
129                                 "SEC Session init failed: err: %d\n", ret);
130                                 return -1;
131                         }
132                 } else if (sa->type == RTE_SECURITY_ACTION_TYPE_INLINE_CRYPTO) {
133                         struct rte_flow_error err;
134                         struct rte_security_ctx *ctx = (struct rte_security_ctx *)
135                                                         rte_eth_dev_get_sec_ctx(
136                                                         sa->portid);
137                         const struct rte_security_capability *sec_cap;
138
139                         sa->sec_session = rte_security_session_create(ctx,
140                                         &sess_conf, ipsec_ctx->session_pool);
141                         if (sa->sec_session == NULL) {
142                                 RTE_LOG(ERR, IPSEC,
143                                 "SEC Session init failed: err: %d\n", ret);
144                                 return -1;
145                         }
146
147                         sec_cap = rte_security_capabilities_get(ctx);
148
149                         /* iterate until ESP tunnel*/
150                         while (sec_cap->action !=
151                                         RTE_SECURITY_ACTION_TYPE_NONE) {
152
153                                 if (sec_cap->action == sa->type &&
154                                     sec_cap->protocol ==
155                                         RTE_SECURITY_PROTOCOL_IPSEC &&
156                                     sec_cap->ipsec.mode ==
157                                         RTE_SECURITY_IPSEC_SA_MODE_TUNNEL &&
158                                     sec_cap->ipsec.direction == sa->direction)
159                                         break;
160                                 sec_cap++;
161                         }
162
163                         if (sec_cap->action == RTE_SECURITY_ACTION_TYPE_NONE) {
164                                 RTE_LOG(ERR, IPSEC,
165                                 "No suitable security capability found\n");
166                                 return -1;
167                         }
168
169                         sa->ol_flags = sec_cap->ol_flags;
170                         sa->security_ctx = ctx;
171                         sa->pattern[0].type = RTE_FLOW_ITEM_TYPE_ETH;
172
173                         sa->pattern[1].type = RTE_FLOW_ITEM_TYPE_IPV4;
174                         sa->pattern[1].mask = &rte_flow_item_ipv4_mask;
175                         if (sa->flags & IP6_TUNNEL) {
176                                 sa->pattern[1].spec = &sa->ipv6_spec;
177                                 memcpy(sa->ipv6_spec.hdr.dst_addr,
178                                         sa->dst.ip.ip6.ip6_b, 16);
179                                 memcpy(sa->ipv6_spec.hdr.src_addr,
180                                        sa->src.ip.ip6.ip6_b, 16);
181                         } else {
182                                 sa->pattern[1].spec = &sa->ipv4_spec;
183                                 sa->ipv4_spec.hdr.dst_addr = sa->dst.ip.ip4;
184                                 sa->ipv4_spec.hdr.src_addr = sa->src.ip.ip4;
185                         }
186
187                         sa->pattern[2].type = RTE_FLOW_ITEM_TYPE_ESP;
188                         sa->pattern[2].spec = &sa->esp_spec;
189                         sa->pattern[2].mask = &rte_flow_item_esp_mask;
190                         sa->esp_spec.hdr.spi = sa->spi;
191
192                         sa->pattern[3].type = RTE_FLOW_ITEM_TYPE_END;
193
194                         sa->action[0].type = RTE_FLOW_ACTION_TYPE_SECURITY;
195                         sa->action[0].conf = sa->sec_session;
196
197                         sa->action[1].type = RTE_FLOW_ACTION_TYPE_END;
198
199                         sa->attr.egress = (sa->direction ==
200                                         RTE_SECURITY_IPSEC_SA_DIR_EGRESS);
201                         sa->flow = rte_flow_create(sa->portid,
202                                 &sa->attr, sa->pattern, sa->action, &err);
203                         if (sa->flow == NULL) {
204                                 RTE_LOG(ERR, IPSEC,
205                                         "Failed to create ipsec flow msg: %s\n",
206                                         err.message);
207                                 return -1;
208                         }
209                 }
210         } else {
211                 sa->crypto_session = rte_cryptodev_sym_session_create(
212                                 ipsec_ctx->session_pool);
213                 rte_cryptodev_sym_session_init(ipsec_ctx->tbl[cdev_id_qp].id,
214                                 sa->crypto_session, sa->xforms,
215                                 ipsec_ctx->session_pool);
216
217                 rte_cryptodev_info_get(ipsec_ctx->tbl[cdev_id_qp].id,
218                                 &cdev_info);
219                 if (cdev_info.sym.max_nb_sessions_per_qp > 0) {
220                         ret = rte_cryptodev_queue_pair_attach_sym_session(
221                                         ipsec_ctx->tbl[cdev_id_qp].id,
222                                         ipsec_ctx->tbl[cdev_id_qp].qp,
223                                         sa->crypto_session);
224                         if (ret < 0) {
225                                 RTE_LOG(ERR, IPSEC,
226                                         "Session cannot be attached to qp %u\n",
227                                         ipsec_ctx->tbl[cdev_id_qp].qp);
228                                 return -1;
229                         }
230                 }
231         }
232         sa->cdev_id_qp = cdev_id_qp;
233
234         return 0;
235 }
236
237 static inline void
238 enqueue_cop(struct cdev_qp *cqp, struct rte_crypto_op *cop)
239 {
240         int32_t ret, i;
241
242         cqp->buf[cqp->len++] = cop;
243
244         if (cqp->len == MAX_PKT_BURST) {
245                 ret = rte_cryptodev_enqueue_burst(cqp->id, cqp->qp,
246                                 cqp->buf, cqp->len);
247                 if (ret < cqp->len) {
248                         RTE_LOG_DP(DEBUG, IPSEC, "Cryptodev %u queue %u:"
249                                         " enqueued %u crypto ops out of %u\n",
250                                          cqp->id, cqp->qp,
251                                          ret, cqp->len);
252                         for (i = ret; i < cqp->len; i++)
253                                 rte_pktmbuf_free(cqp->buf[i]->sym->m_src);
254                 }
255                 cqp->in_flight += ret;
256                 cqp->len = 0;
257         }
258 }
259
260 static inline void
261 ipsec_enqueue(ipsec_xform_fn xform_func, struct ipsec_ctx *ipsec_ctx,
262                 struct rte_mbuf *pkts[], struct ipsec_sa *sas[],
263                 uint16_t nb_pkts)
264 {
265         int32_t ret = 0, i;
266         struct ipsec_mbuf_metadata *priv;
267         struct rte_crypto_sym_op *sym_cop;
268         struct ipsec_sa *sa;
269         struct cdev_qp *cqp;
270
271         for (i = 0; i < nb_pkts; i++) {
272                 if (unlikely(sas[i] == NULL)) {
273                         rte_pktmbuf_free(pkts[i]);
274                         continue;
275                 }
276
277                 rte_prefetch0(sas[i]);
278                 rte_prefetch0(pkts[i]);
279
280                 priv = get_priv(pkts[i]);
281                 sa = sas[i];
282                 priv->sa = sa;
283
284                 switch (sa->type) {
285                 case RTE_SECURITY_ACTION_TYPE_LOOKASIDE_PROTOCOL:
286                         priv->cop.type = RTE_CRYPTO_OP_TYPE_SYMMETRIC;
287                         priv->cop.status = RTE_CRYPTO_OP_STATUS_NOT_PROCESSED;
288
289                         rte_prefetch0(&priv->sym_cop);
290
291                         if ((unlikely(sa->sec_session == NULL)) &&
292                                         create_session(ipsec_ctx, sa)) {
293                                 rte_pktmbuf_free(pkts[i]);
294                                 continue;
295                         }
296
297                         sym_cop = get_sym_cop(&priv->cop);
298                         sym_cop->m_src = pkts[i];
299
300                         rte_security_attach_session(&priv->cop,
301                                         sa->sec_session);
302                         break;
303                 case RTE_SECURITY_ACTION_TYPE_NONE:
304
305                         priv->cop.type = RTE_CRYPTO_OP_TYPE_SYMMETRIC;
306                         priv->cop.status = RTE_CRYPTO_OP_STATUS_NOT_PROCESSED;
307
308                         rte_prefetch0(&priv->sym_cop);
309
310                         if ((unlikely(sa->crypto_session == NULL)) &&
311                                         create_session(ipsec_ctx, sa)) {
312                                 rte_pktmbuf_free(pkts[i]);
313                                 continue;
314                         }
315
316                         rte_crypto_op_attach_sym_session(&priv->cop,
317                                         sa->crypto_session);
318
319                         ret = xform_func(pkts[i], sa, &priv->cop);
320                         if (unlikely(ret)) {
321                                 rte_pktmbuf_free(pkts[i]);
322                                 continue;
323                         }
324                         break;
325                 case RTE_SECURITY_ACTION_TYPE_INLINE_PROTOCOL:
326                         break;
327                 case RTE_SECURITY_ACTION_TYPE_INLINE_CRYPTO:
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                         rte_security_attach_session(&priv->cop,
340                                         sa->sec_session);
341
342                         ret = xform_func(pkts[i], sa, &priv->cop);
343                         if (unlikely(ret)) {
344                                 rte_pktmbuf_free(pkts[i]);
345                                 continue;
346                         }
347
348                         cqp = &ipsec_ctx->tbl[sa->cdev_id_qp];
349                         cqp->ol_pkts[cqp->ol_pkts_cnt++] = pkts[i];
350                         if (sa->ol_flags & RTE_SECURITY_TX_OLOAD_NEED_MDATA)
351                                 rte_security_set_pkt_metadata(
352                                                 sa->security_ctx,
353                                                 sa->sec_session, pkts[i], NULL);
354                         continue;
355                 }
356
357                 RTE_ASSERT(sa->cdev_id_qp < ipsec_ctx->nb_qps);
358                 enqueue_cop(&ipsec_ctx->tbl[sa->cdev_id_qp], &priv->cop);
359         }
360 }
361
362 static inline int
363 ipsec_dequeue(ipsec_xform_fn xform_func, struct ipsec_ctx *ipsec_ctx,
364               struct rte_mbuf *pkts[], uint16_t max_pkts)
365 {
366         int32_t nb_pkts = 0, ret = 0, i, j, nb_cops;
367         struct ipsec_mbuf_metadata *priv;
368         struct rte_crypto_op *cops[max_pkts];
369         struct ipsec_sa *sa;
370         struct rte_mbuf *pkt;
371
372         for (i = 0; i < ipsec_ctx->nb_qps && nb_pkts < max_pkts; i++) {
373                 struct cdev_qp *cqp;
374
375                 cqp = &ipsec_ctx->tbl[ipsec_ctx->last_qp++];
376                 if (ipsec_ctx->last_qp == ipsec_ctx->nb_qps)
377                         ipsec_ctx->last_qp %= ipsec_ctx->nb_qps;
378
379                 while (cqp->ol_pkts_cnt > 0 && nb_pkts < max_pkts) {
380                         pkt = cqp->ol_pkts[--cqp->ol_pkts_cnt];
381                         rte_prefetch0(pkt);
382                         priv = get_priv(pkt);
383                         sa = priv->sa;
384                         ret = xform_func(pkt, sa, &priv->cop);
385                         if (unlikely(ret)) {
386                                 rte_pktmbuf_free(pkt);
387                                 continue;
388                         }
389                         pkts[nb_pkts++] = pkt;
390                 }
391
392                 if (cqp->in_flight == 0)
393                         continue;
394
395                 nb_cops = rte_cryptodev_dequeue_burst(cqp->id, cqp->qp,
396                                 cops, max_pkts - nb_pkts);
397
398                 cqp->in_flight -= nb_cops;
399
400                 for (j = 0; j < nb_cops; j++) {
401                         pkt = cops[j]->sym->m_src;
402                         rte_prefetch0(pkt);
403
404                         priv = get_priv(pkt);
405                         sa = priv->sa;
406
407                         RTE_ASSERT(sa != NULL);
408
409                         if (sa->type == RTE_SECURITY_ACTION_TYPE_NONE) {
410                                 ret = xform_func(pkt, sa, cops[j]);
411                                 if (unlikely(ret)) {
412                                         rte_pktmbuf_free(pkt);
413                                         continue;
414                                 }
415                         }
416                         pkts[nb_pkts++] = pkt;
417                 }
418         }
419
420         /* return packets */
421         return nb_pkts;
422 }
423
424 uint16_t
425 ipsec_inbound(struct ipsec_ctx *ctx, struct rte_mbuf *pkts[],
426                 uint16_t nb_pkts, uint16_t len)
427 {
428         struct ipsec_sa *sas[nb_pkts];
429
430         inbound_sa_lookup(ctx->sa_ctx, pkts, sas, nb_pkts);
431
432         ipsec_enqueue(esp_inbound, ctx, pkts, sas, nb_pkts);
433
434         return ipsec_dequeue(esp_inbound_post, ctx, pkts, len);
435 }
436
437 uint16_t
438 ipsec_outbound(struct ipsec_ctx *ctx, struct rte_mbuf *pkts[],
439                 uint32_t sa_idx[], uint16_t nb_pkts, uint16_t len)
440 {
441         struct ipsec_sa *sas[nb_pkts];
442
443         outbound_sa_lookup(ctx->sa_ctx, sa_idx, sas, nb_pkts);
444
445         ipsec_enqueue(esp_outbound, ctx, pkts, sas, nb_pkts);
446
447         return ipsec_dequeue(esp_outbound_post, ctx, pkts, len);
448 }