Harmonize vec/pool_get_aligned object sizes and alignment requests
[vpp.git] / src / plugins / dpdk / ipsec / ipsec.h
1 /*
2  * Copyright (c) 2017 Intel and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #ifndef __DPDK_IPSEC_H__
16 #define __DPDK_IPSEC_H__
17
18 #include <vnet/vnet.h>
19 #include <vppinfra/cache.h>
20 #include <vnet/ipsec/ipsec.h>
21
22 #undef always_inline
23 #include <rte_config.h>
24 #include <rte_crypto.h>
25 #include <rte_cryptodev.h>
26
27 #if CLIB_DEBUG > 0
28 #define always_inline static inline
29 #else
30 #define always_inline static inline __attribute__ ((__always_inline__))
31 #endif
32
33 #define foreach_dpdk_crypto_input_next          \
34   _(DROP, "error-drop")                         \
35   _(IP4_LOOKUP, "ip4-lookup")                   \
36   _(IP6_LOOKUP, "ip6-lookup")                   \
37   _(INTERFACE_OUTPUT, "interface-output")       \
38   _(DECRYPT_POST, "dpdk-esp-decrypt-post")
39
40 typedef enum
41 {
42 #define _(f,s) DPDK_CRYPTO_INPUT_NEXT_##f,
43   foreach_dpdk_crypto_input_next
44 #undef _
45     DPDK_CRYPTO_INPUT_N_NEXT,
46 } dpdk_crypto_input_next_t;
47
48 #define MAX_QP_PER_LCORE 16
49
50 typedef struct
51 {
52   u32 salt;
53   u32 iv[2];
54   u32 cnt;
55 } dpdk_gcm_cnt_blk;
56
57 typedef struct
58 {
59   u32 next;
60   dpdk_gcm_cnt_blk cb __attribute__ ((aligned (16)));
61   u8 aad[16];
62   u8 icv[32];
63 } dpdk_op_priv_t;
64
65 typedef struct
66 {
67   u16 *resource_idx;
68   struct rte_crypto_op **ops;
69   u16 cipher_resource_idx[IPSEC_CRYPTO_N_ALG];
70   u16 auth_resource_idx[IPSEC_INTEG_N_ALG];
71     CLIB_CACHE_LINE_ALIGN_MARK (pad);
72 } crypto_worker_main_t __attribute__ ((aligned (CLIB_CACHE_LINE_BYTES)));
73
74 typedef struct
75 {
76   char *name;
77   enum rte_crypto_sym_xform_type type;
78   u32 alg;
79   u8 key_len;
80   u8 iv_len;
81   u8 trunc_size;
82   u8 boundary;
83   u8 disabled;
84   u8 resources;
85 } crypto_alg_t __attribute__ ((aligned (8)));
86
87 typedef struct
88 {
89   u16 *free_resources;
90   u16 *used_resources;
91   u8 cipher_support[IPSEC_CRYPTO_N_ALG];
92   u8 auth_support[IPSEC_INTEG_N_ALG];
93   u8 drv_id;
94   u8 numa;
95   u16 id;
96   const i8 *name;
97   u32 max_qp;
98   u64 features;
99 } crypto_dev_t;
100
101 typedef struct
102 {
103   const i8 *name;
104   u16 *devs;
105 } crypto_drv_t;
106
107 typedef struct
108 {
109   u16 thread_idx;
110   u8 remove;
111   u8 drv_id;
112   u8 dev_id;
113   u8 numa;
114   u16 qp_id;
115   u16 inflights[2];
116   u16 n_ops;
117   u16 __unused;
118   struct rte_crypto_op *ops[VLIB_FRAME_SIZE];
119   u32 bi[VLIB_FRAME_SIZE];
120 } crypto_resource_t __attribute__ ((aligned (CLIB_CACHE_LINE_BYTES)));
121
122 typedef struct
123 {
124   u64 ts;
125   struct rte_cryptodev_sym_session *session;
126 } crypto_session_disposal_t;
127
128 typedef struct
129 {
130   /* Required for vec_validate_aligned */
131   CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
132   struct rte_mempool *crypto_op;
133   struct rte_mempool *session_h;
134   struct rte_mempool **session_drv;
135   crypto_session_disposal_t *session_disposal;
136   uword *session_by_sa_index;
137   uword *session_by_drv_id_and_sa_index;
138   u64 crypto_op_get_failed;
139   u64 session_h_failed;
140   u64 *session_drv_failed;
141   clib_spinlock_t lockp;
142 } crypto_data_t;
143
144 typedef struct
145 {
146   crypto_worker_main_t *workers_main;
147   struct rte_cryptodev_sym_session **sa_session;
148   crypto_dev_t *dev;
149   crypto_resource_t *resource;
150   crypto_alg_t *cipher_algs;
151   crypto_alg_t *auth_algs;
152   crypto_data_t *data;
153   crypto_drv_t *drv;
154   u64 session_timeout;          /* nsec */
155   u8 enabled;
156 } dpdk_crypto_main_t;
157
158 extern dpdk_crypto_main_t dpdk_crypto_main;
159
160 static const u8 pad_data[] =
161   { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 0 };
162
163 void crypto_auto_placement (void);
164
165 clib_error_t *create_sym_session (struct rte_cryptodev_sym_session **session,
166                                   u32 sa_idx, crypto_resource_t * res,
167                                   crypto_worker_main_t * cwm, u8 is_outbound);
168
169 static_always_inline u32
170 crypto_op_len (void)
171 {
172   const u32 align = 4;
173   u32 op_size =
174     sizeof (struct rte_crypto_op) + sizeof (struct rte_crypto_sym_op);
175
176   return ((op_size + align - 1) & ~(align - 1)) + sizeof (dpdk_op_priv_t);
177 }
178
179 static_always_inline u32
180 crypto_op_get_priv_offset (void)
181 {
182   const u32 align = 16;
183   u32 offset;
184
185   offset = sizeof (struct rte_crypto_op) + sizeof (struct rte_crypto_sym_op);
186   offset = (offset + align - 1) & ~(align - 1);
187
188   return offset;
189 }
190
191 static_always_inline dpdk_op_priv_t *
192 crypto_op_get_priv (struct rte_crypto_op * op)
193 {
194   return (dpdk_op_priv_t *) (((u8 *) op) + crypto_op_get_priv_offset ());
195 }
196
197 /* XXX this requires 64 bit builds so hash_xxx macros use u64 key */
198 typedef union
199 {
200   u64 val;
201   struct
202   {
203     u32 drv_id;
204     u32 sa_idx;
205   };
206 } crypto_session_key_t;
207
208 static_always_inline clib_error_t *
209 crypto_get_session (struct rte_cryptodev_sym_session **session,
210                     u32 sa_idx,
211                     crypto_resource_t * res,
212                     crypto_worker_main_t * cwm, u8 is_outbound)
213 {
214   dpdk_crypto_main_t *dcm = &dpdk_crypto_main;
215   crypto_data_t *data;
216   uword *val;
217   crypto_session_key_t key = { 0 };
218
219   key.drv_id = res->drv_id;
220   key.sa_idx = sa_idx;
221
222   data = vec_elt_at_index (dcm->data, res->numa);
223   val = hash_get (data->session_by_drv_id_and_sa_index, key.val);
224
225   if (PREDICT_FALSE (!val))
226     return create_sym_session (session, sa_idx, res, cwm, is_outbound);
227
228   session[0] = (struct rte_cryptodev_sym_session *) val[0];
229
230   return NULL;
231 }
232
233 static_always_inline u16
234 get_resource (crypto_worker_main_t * cwm, ipsec_sa_t * sa)
235 {
236   u16 cipher_res = cwm->cipher_resource_idx[sa->crypto_alg];
237   u16 auth_res = cwm->auth_resource_idx[sa->integ_alg];
238   u8 is_aead;
239
240   /* Not allowed to setup SA with no-aead-cipher/NULL or NULL/NULL */
241
242   is_aead = ((sa->crypto_alg == IPSEC_CRYPTO_ALG_AES_GCM_128) |
243              (sa->crypto_alg == IPSEC_CRYPTO_ALG_AES_GCM_192) |
244              (sa->crypto_alg == IPSEC_CRYPTO_ALG_AES_GCM_256));
245
246   if (sa->crypto_alg == IPSEC_CRYPTO_ALG_NONE)
247     return auth_res;
248
249   if (cipher_res == auth_res)
250     return cipher_res;
251
252   if (is_aead)
253     return cipher_res;
254
255   return (u16) ~ 0;
256 }
257
258 static_always_inline i32
259 crypto_alloc_ops (u8 numa, struct rte_crypto_op ** ops, u32 n)
260 {
261   dpdk_crypto_main_t *dcm = &dpdk_crypto_main;
262   crypto_data_t *data = vec_elt_at_index (dcm->data, numa);
263   i32 ret;
264
265   ret = rte_mempool_get_bulk (data->crypto_op, (void **) ops, n);
266
267   /* *INDENT-OFF* */
268   data->crypto_op_get_failed += ! !ret;
269   /* *INDENT-ON* */
270
271   return ret;
272 }
273
274 static_always_inline void
275 crypto_free_ops (u8 numa, struct rte_crypto_op **ops, u32 n)
276 {
277   dpdk_crypto_main_t *dcm = &dpdk_crypto_main;
278   crypto_data_t *data = vec_elt_at_index (dcm->data, numa);
279
280   if (!n)
281     return;
282
283   rte_mempool_put_bulk (data->crypto_op, (void **) ops, n);
284 }
285
286 static_always_inline void
287 crypto_enqueue_ops (vlib_main_t * vm, crypto_worker_main_t * cwm, u8 outbound,
288                     u32 node_index, u32 error, u8 numa)
289 {
290   dpdk_crypto_main_t *dcm = &dpdk_crypto_main;
291   crypto_resource_t *res;
292   u16 *res_idx;
293
294   /* *INDENT-OFF* */
295   vec_foreach (res_idx, cwm->resource_idx)
296     {
297       u16 enq;
298       res = vec_elt_at_index (dcm->resource, res_idx[0]);
299
300       if (!res->n_ops)
301         continue;
302
303       enq = rte_cryptodev_enqueue_burst (res->dev_id, res->qp_id + outbound,
304                                          res->ops, res->n_ops);
305       res->inflights[outbound] += enq;
306
307       if (PREDICT_FALSE (enq < res->n_ops))
308         {
309           crypto_free_ops (numa, &res->ops[enq], res->n_ops - enq);
310           vlib_buffer_free (vm, &res->bi[enq], res->n_ops - enq);
311
312           vlib_node_increment_counter (vm, node_index, error,
313                                        res->n_ops - enq);
314         }
315       res->n_ops = 0;
316     }
317   /* *INDENT-ON* */
318 }
319
320 static_always_inline void
321 crypto_set_icb (dpdk_gcm_cnt_blk * icb, u32 salt, u32 seq, u32 seq_hi)
322 {
323   icb->salt = salt;
324   icb->iv[0] = seq;
325   icb->iv[1] = seq_hi;
326 }
327
328 static_always_inline void
329 crypto_op_setup (u8 is_aead, struct rte_mbuf *mb0,
330                  struct rte_crypto_op *op, void *session,
331                  u32 cipher_off, u32 cipher_len,
332                  u32 auth_off, u32 auth_len,
333                  u8 * aad, u8 * digest, u64 digest_paddr)
334 {
335   struct rte_crypto_sym_op *sym_op;
336
337   sym_op = (struct rte_crypto_sym_op *) (op + 1);
338
339   sym_op->m_src = mb0;
340   sym_op->session = session;
341
342   if (is_aead)
343     {
344       sym_op->aead.data.offset = cipher_off;
345       sym_op->aead.data.length = cipher_len;
346
347       sym_op->aead.aad.data = aad;
348       sym_op->aead.aad.phys_addr =
349         op->phys_addr + (uintptr_t) aad - (uintptr_t) op;
350
351       sym_op->aead.digest.data = digest;
352       sym_op->aead.digest.phys_addr = digest_paddr;
353     }
354   else
355     {
356       sym_op->cipher.data.offset = cipher_off;
357       sym_op->cipher.data.length = cipher_len;
358
359       sym_op->auth.data.offset = auth_off;
360       sym_op->auth.data.length = auth_len;
361
362       sym_op->auth.digest.data = digest;
363       sym_op->auth.digest.phys_addr = digest_paddr;
364     }
365 }
366
367 #endif /* __DPDK_IPSEC_H__ */
368
369 /*
370  * fd.io coding-style-patch-verification: ON
371  *
372  * Local Variables:
373  * eval: (c-set-style "gnu")
374  * End:
375  */