ccc840f2bcc9d98bafcb7853779cbee71c866730
[dpdk.git] / examples / ipsec-secgw / ipsec.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2016 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_cryptodev.h>
41 #include <rte_mbuf.h>
42 #include <rte_hash.h>
43
44 #include "ipsec.h"
45 #include "esp.h"
46
47 static inline int
48 create_session(struct ipsec_ctx *ipsec_ctx __rte_unused, struct ipsec_sa *sa)
49 {
50         unsigned long cdev_id_qp = 0;
51         int32_t ret;
52         struct cdev_key key = { 0 };
53
54         key.lcore_id = (uint8_t)rte_lcore_id();
55
56         key.cipher_algo = (uint8_t)sa->cipher_algo;
57         key.auth_algo = (uint8_t)sa->auth_algo;
58
59         ret = rte_hash_lookup_data(ipsec_ctx->cdev_map, &key,
60                         (void **)&cdev_id_qp);
61         if (ret < 0) {
62                 RTE_LOG(ERR, IPSEC, "No cryptodev: core %u, cipher_algo %u, "
63                                 "auth_algo %u\n", key.lcore_id, key.cipher_algo,
64                                 key.auth_algo);
65                 return -1;
66         }
67
68         RTE_LOG(DEBUG, IPSEC, "Create session for SA spi %u on cryptodev "
69                         "%u qp %u\n", sa->spi, ipsec_ctx->tbl[cdev_id_qp].id,
70                         ipsec_ctx->tbl[cdev_id_qp].qp);
71
72         sa->crypto_session = rte_cryptodev_sym_session_create(
73                         ipsec_ctx->tbl[cdev_id_qp].id, sa->xforms);
74
75         sa->cdev_id_qp = cdev_id_qp;
76
77         return 0;
78 }
79
80 static inline void
81 enqueue_cop(struct cdev_qp *cqp, struct rte_crypto_op *cop)
82 {
83         int ret, i;
84
85         cqp->buf[cqp->len++] = cop;
86
87         if (cqp->len == MAX_PKT_BURST) {
88                 ret = rte_cryptodev_enqueue_burst(cqp->id, cqp->qp,
89                                 cqp->buf, cqp->len);
90                 if (ret < cqp->len) {
91                         RTE_LOG(DEBUG, IPSEC, "Cryptodev %u queue %u:"
92                                         " enqueued %u crypto ops out of %u\n",
93                                          cqp->id, cqp->qp,
94                                          ret, cqp->len);
95                         for (i = ret; i < cqp->len; i++)
96                                 rte_pktmbuf_free(cqp->buf[i]->sym->m_src);
97                 }
98                 cqp->in_flight += ret;
99                 cqp->len = 0;
100         }
101 }
102
103 static inline void
104 ipsec_enqueue(ipsec_xform_fn xform_func, struct ipsec_ctx *ipsec_ctx,
105                 struct rte_mbuf *pkts[], struct ipsec_sa *sas[],
106                 uint16_t nb_pkts)
107 {
108         int ret = 0, i;
109         struct ipsec_mbuf_metadata *priv;
110         struct ipsec_sa *sa;
111
112         for (i = 0; i < nb_pkts; i++) {
113                 if (unlikely(sas[i] == NULL)) {
114                         rte_pktmbuf_free(pkts[i]);
115                         continue;
116                 }
117
118                 rte_prefetch0(sas[i]);
119                 rte_prefetch0(pkts[i]);
120
121                 priv = get_priv(pkts[i]);
122                 sa = sas[i];
123                 priv->sa = sa;
124
125                 priv->cop.type = RTE_CRYPTO_OP_TYPE_SYMMETRIC;
126
127                 rte_prefetch0(&priv->sym_cop);
128                 priv->cop.sym = &priv->sym_cop;
129
130                 if ((unlikely(sa->crypto_session == NULL)) &&
131                                 create_session(ipsec_ctx, sa)) {
132                         rte_pktmbuf_free(pkts[i]);
133                         continue;
134                 }
135
136                 rte_crypto_op_attach_sym_session(&priv->cop,
137                                 sa->crypto_session);
138
139                 ret = xform_func(pkts[i], sa, &priv->cop);
140                 if (unlikely(ret)) {
141                         rte_pktmbuf_free(pkts[i]);
142                         continue;
143                 }
144
145                 RTE_ASSERT(sa->cdev_id_qp < ipsec_ctx->nb_qps);
146                 enqueue_cop(&ipsec_ctx->tbl[sa->cdev_id_qp], &priv->cop);
147         }
148 }
149
150 static inline int
151 ipsec_dequeue(ipsec_xform_fn xform_func, struct ipsec_ctx *ipsec_ctx,
152                 struct rte_mbuf *pkts[], uint16_t max_pkts)
153 {
154         int nb_pkts = 0, ret = 0, i, j, nb_cops;
155         struct ipsec_mbuf_metadata *priv;
156         struct rte_crypto_op *cops[max_pkts];
157         struct ipsec_sa *sa;
158         struct rte_mbuf *pkt;
159
160         for (i = 0; i < ipsec_ctx->nb_qps && nb_pkts < max_pkts; i++) {
161                 struct cdev_qp *cqp;
162
163                 cqp = &ipsec_ctx->tbl[ipsec_ctx->last_qp++];
164                 if (ipsec_ctx->last_qp == ipsec_ctx->nb_qps)
165                         ipsec_ctx->last_qp %= ipsec_ctx->nb_qps;
166
167                 if (cqp->in_flight == 0)
168                         continue;
169
170                 nb_cops = rte_cryptodev_dequeue_burst(cqp->id, cqp->qp,
171                                 cops, max_pkts - nb_pkts);
172
173                 cqp->in_flight -= nb_cops;
174
175                 for (j = 0; j < nb_cops; j++) {
176                         pkt = cops[j]->sym->m_src;
177                         rte_prefetch0(pkt);
178
179                         priv = get_priv(pkt);
180                         sa = priv->sa;
181
182                         RTE_ASSERT(sa != NULL);
183
184                         ret = xform_func(pkt, sa, cops[j]);
185                         if (unlikely(ret))
186                                 rte_pktmbuf_free(pkt);
187                         else
188                                 pkts[nb_pkts++] = pkt;
189                 }
190         }
191
192         /* return packets */
193         return nb_pkts;
194 }
195
196 uint16_t
197 ipsec_inbound(struct ipsec_ctx *ctx, struct rte_mbuf *pkts[],
198                 uint16_t nb_pkts, uint16_t len)
199 {
200         struct ipsec_sa *sas[nb_pkts];
201
202         inbound_sa_lookup(ctx->sa_ctx, pkts, sas, nb_pkts);
203
204         ipsec_enqueue(esp_inbound, ctx, pkts, sas, nb_pkts);
205
206         return ipsec_dequeue(esp_inbound_post, ctx, pkts, len);
207 }
208
209 uint16_t
210 ipsec_outbound(struct ipsec_ctx *ctx, struct rte_mbuf *pkts[],
211                 uint32_t sa_idx[], uint16_t nb_pkts, uint16_t len)
212 {
213         struct ipsec_sa *sas[nb_pkts];
214
215         outbound_sa_lookup(ctx->sa_ctx, sa_idx, sas, nb_pkts);
216
217         ipsec_enqueue(esp_outbound, ctx, pkts, sas, nb_pkts);
218
219         return ipsec_dequeue(esp_outbound_post, ctx, pkts, len);
220 }