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