7d7ba564544236e78255e59d19f2ba294ff1f1e9
[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   _(DECRYPT4_POST, "dpdk-esp4-decrypt-post")     \
39   _(DECRYPT6_POST, "dpdk-esp6-decrypt-post")
40
41 typedef enum
42 {
43 #define _(f,s) DPDK_CRYPTO_INPUT_NEXT_##f,
44   foreach_dpdk_crypto_input_next
45 #undef _
46     DPDK_CRYPTO_INPUT_N_NEXT,
47 } dpdk_crypto_input_next_t;
48
49 #define MAX_QP_PER_LCORE 16
50
51 typedef struct
52 {
53   u32 salt;
54   u32 iv[2];
55   u32 cnt;
56 } dpdk_gcm_cnt_blk;
57
58 typedef struct
59 {
60   u32 next;
61   dpdk_gcm_cnt_blk cb __attribute__ ((aligned (16)));
62   u8 aad[16];
63   u8 icv[32];
64 } dpdk_op_priv_t;
65
66 typedef struct
67 {
68   u16 *resource_idx;
69   struct rte_crypto_op **ops;
70   u16 cipher_resource_idx[IPSEC_CRYPTO_N_ALG];
71   u16 auth_resource_idx[IPSEC_INTEG_N_ALG];
72     CLIB_CACHE_LINE_ALIGN_MARK (pad);
73 } crypto_worker_main_t __attribute__ ((aligned (CLIB_CACHE_LINE_BYTES)));
74
75 typedef struct
76 {
77   char *name;
78   enum rte_crypto_sym_xform_type type;
79   u32 alg;
80   u8 key_len;
81   u8 iv_len;
82   u8 trunc_size;
83   u8 boundary;
84   u8 disabled;
85   u8 resources;
86 } crypto_alg_t __attribute__ ((aligned (8)));
87
88 typedef struct
89 {
90   u16 *free_resources;
91   u16 *used_resources;
92   u8 cipher_support[IPSEC_CRYPTO_N_ALG];
93   u8 auth_support[IPSEC_INTEG_N_ALG];
94   u8 drv_id;
95   u8 numa;
96   u16 id;
97   const char *name;
98   u32 max_qp;
99   u64 features;
100 } crypto_dev_t;
101
102 typedef struct
103 {
104   const char *name;
105   u16 *devs;
106 } crypto_drv_t;
107
108 typedef struct
109 {
110   u16 thread_idx;
111   u8 remove;
112   u8 drv_id;
113   u8 dev_id;
114   u8 numa;
115   u16 qp_id;
116   u16 inflights[2];
117   u16 n_ops;
118   u16 __unused;
119   struct rte_crypto_op *ops[VLIB_FRAME_SIZE];
120   u32 bi[VLIB_FRAME_SIZE];
121 } crypto_resource_t __attribute__ ((aligned (CLIB_CACHE_LINE_BYTES)));
122
123 typedef struct
124 {
125   u64 ts;
126   struct rte_cryptodev_sym_session *session;
127 } crypto_session_disposal_t;
128
129 typedef struct
130 {
131   struct rte_cryptodev_sym_session *session;
132   u64 dev_mask;
133 } crypto_session_by_drv_t;
134
135 typedef struct
136 {
137   /* Required for vec_validate_aligned */
138   CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
139   struct rte_mempool *crypto_op;
140   struct rte_mempool *session_h;
141   struct rte_mempool **session_drv;
142   crypto_session_disposal_t *session_disposal;
143   uword *session_by_sa_index;
144   u64 crypto_op_get_failed;
145   u64 session_h_failed;
146   u64 *session_drv_failed;
147   crypto_session_by_drv_t *session_by_drv_id_and_sa_index;
148   clib_spinlock_t lockp;
149 } crypto_data_t;
150
151 typedef struct
152 {
153   crypto_worker_main_t *workers_main;
154   crypto_dev_t *dev;
155   crypto_resource_t *resource;
156   crypto_alg_t *cipher_algs;
157   crypto_alg_t *auth_algs;
158   crypto_data_t *data;
159   crypto_drv_t *drv;
160   u64 session_timeout;          /* nsec */
161   u8 enabled;
162 } dpdk_crypto_main_t;
163
164 extern dpdk_crypto_main_t dpdk_crypto_main;
165
166 static const u8 pad_data[] =
167   { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 0 };
168
169 void crypto_auto_placement (void);
170
171 clib_error_t *create_sym_session (struct rte_cryptodev_sym_session **session,
172                                   u32 sa_idx, crypto_resource_t * res,
173                                   crypto_worker_main_t * cwm, u8 is_outbound);
174
175 static_always_inline u32
176 crypto_op_len (void)
177 {
178   const u32 align = 4;
179   u32 op_size =
180     sizeof (struct rte_crypto_op) + sizeof (struct rte_crypto_sym_op);
181
182   return ((op_size + align - 1) & ~(align - 1)) + sizeof (dpdk_op_priv_t);
183 }
184
185 static_always_inline u32
186 crypto_op_get_priv_offset (void)
187 {
188   const u32 align = 16;
189   u32 offset;
190
191   offset = sizeof (struct rte_crypto_op) + sizeof (struct rte_crypto_sym_op);
192   offset = (offset + align - 1) & ~(align - 1);
193
194   return offset;
195 }
196
197 static_always_inline dpdk_op_priv_t *
198 crypto_op_get_priv (struct rte_crypto_op * op)
199 {
200   return (dpdk_op_priv_t *) (((u8 *) op) + crypto_op_get_priv_offset ());
201 }
202
203
204 static_always_inline void
205 add_session_by_drv_and_sa_idx (struct rte_cryptodev_sym_session *session,
206                                crypto_data_t * data, u32 drv_id, u32 sa_idx)
207 {
208   crypto_session_by_drv_t *sbd;
209   vec_validate_aligned (data->session_by_drv_id_and_sa_index, sa_idx,
210                         CLIB_CACHE_LINE_BYTES);
211   sbd = vec_elt_at_index (data->session_by_drv_id_and_sa_index, sa_idx);
212   sbd->dev_mask |= 1L << drv_id;
213   sbd->session = session;
214 }
215
216 static_always_inline struct rte_cryptodev_sym_session *
217 get_session_by_drv_and_sa_idx (crypto_data_t * data, u32 drv_id, u32 sa_idx)
218 {
219   crypto_session_by_drv_t *sess_by_sa;
220   if (_vec_len (data->session_by_drv_id_and_sa_index) <= sa_idx)
221     return NULL;
222   sess_by_sa =
223     vec_elt_at_index (data->session_by_drv_id_and_sa_index, sa_idx);
224   return (sess_by_sa->dev_mask & (1L << drv_id)) ? sess_by_sa->session : NULL;
225 }
226
227 static_always_inline clib_error_t *
228 crypto_get_session (struct rte_cryptodev_sym_session ** session,
229                     u32 sa_idx,
230                     crypto_resource_t * res,
231                     crypto_worker_main_t * cwm, u8 is_outbound)
232 {
233   dpdk_crypto_main_t *dcm = &dpdk_crypto_main;
234   crypto_data_t *data;
235   struct rte_cryptodev_sym_session *sess;
236
237   data = vec_elt_at_index (dcm->data, res->numa);
238   sess = get_session_by_drv_and_sa_idx (data, res->drv_id, sa_idx);
239
240   if (PREDICT_FALSE (!sess))
241     return create_sym_session (session, sa_idx, res, cwm, is_outbound);
242
243   session[0] = sess;
244
245   return NULL;
246 }
247
248 static_always_inline u16
249 get_resource (crypto_worker_main_t * cwm, ipsec_sa_t * sa)
250 {
251   u16 cipher_res = cwm->cipher_resource_idx[sa->crypto_alg];
252   u16 auth_res = cwm->auth_resource_idx[sa->integ_alg];
253   u8 is_aead;
254
255   /* Not allowed to setup SA with no-aead-cipher/NULL or NULL/NULL */
256
257   is_aead = ((sa->crypto_alg == IPSEC_CRYPTO_ALG_AES_GCM_128) ||
258              (sa->crypto_alg == IPSEC_CRYPTO_ALG_AES_GCM_192) ||
259              (sa->crypto_alg == IPSEC_CRYPTO_ALG_AES_GCM_256));
260
261   if (sa->crypto_alg == IPSEC_CRYPTO_ALG_NONE)
262     return auth_res;
263
264   if (cipher_res == auth_res)
265     return cipher_res;
266
267   if (is_aead)
268     return cipher_res;
269
270   return (u16) ~ 0;
271 }
272
273 static_always_inline i32
274 crypto_alloc_ops (u8 numa, struct rte_crypto_op ** ops, u32 n)
275 {
276   dpdk_crypto_main_t *dcm = &dpdk_crypto_main;
277   crypto_data_t *data = vec_elt_at_index (dcm->data, numa);
278   i32 ret;
279
280   ret = rte_mempool_get_bulk (data->crypto_op, (void **) ops, n);
281
282   /* *INDENT-OFF* */
283   data->crypto_op_get_failed += ! !ret;
284   /* *INDENT-ON* */
285
286   return ret;
287 }
288
289 static_always_inline void
290 crypto_free_ops (u8 numa, struct rte_crypto_op **ops, u32 n)
291 {
292   dpdk_crypto_main_t *dcm = &dpdk_crypto_main;
293   crypto_data_t *data = vec_elt_at_index (dcm->data, numa);
294
295   if (!n)
296     return;
297
298   rte_mempool_put_bulk (data->crypto_op, (void **) ops, n);
299 }
300
301 static_always_inline void
302 crypto_enqueue_ops (vlib_main_t * vm, crypto_worker_main_t * cwm, u8 outbound,
303                     u32 node_index, u32 error, u8 numa)
304 {
305   dpdk_crypto_main_t *dcm = &dpdk_crypto_main;
306   crypto_resource_t *res;
307   u16 *res_idx;
308
309   /* *INDENT-OFF* */
310   vec_foreach (res_idx, cwm->resource_idx)
311     {
312       u16 enq;
313       res = vec_elt_at_index (dcm->resource, res_idx[0]);
314
315       if (!res->n_ops)
316         continue;
317
318       enq = rte_cryptodev_enqueue_burst (res->dev_id, res->qp_id + outbound,
319                                          res->ops, res->n_ops);
320       res->inflights[outbound] += enq;
321
322       if (PREDICT_FALSE (enq < res->n_ops))
323         {
324           crypto_free_ops (numa, &res->ops[enq], res->n_ops - enq);
325           vlib_buffer_free (vm, &res->bi[enq], res->n_ops - enq);
326
327           vlib_node_increment_counter (vm, node_index, error,
328                                        res->n_ops - enq);
329         }
330       res->n_ops = 0;
331     }
332   /* *INDENT-ON* */
333 }
334
335 static_always_inline void
336 crypto_set_icb (dpdk_gcm_cnt_blk * icb, u32 salt, u32 seq, u32 seq_hi)
337 {
338   icb->salt = salt;
339   icb->iv[0] = seq;
340   icb->iv[1] = seq_hi;
341 }
342
343 static_always_inline void
344 crypto_op_setup (u8 is_aead, struct rte_mbuf *mb0,
345                  struct rte_crypto_op *op, void *session,
346                  u32 cipher_off, u32 cipher_len,
347                  u32 auth_off, u32 auth_len,
348                  u8 * aad, u8 * digest, u64 digest_paddr)
349 {
350   struct rte_crypto_sym_op *sym_op;
351
352   sym_op = (struct rte_crypto_sym_op *) (op + 1);
353
354   sym_op->m_src = mb0;
355   sym_op->session = session;
356
357   if (is_aead)
358     {
359       sym_op->aead.data.offset = cipher_off;
360       sym_op->aead.data.length = cipher_len;
361
362       sym_op->aead.aad.data = aad;
363       sym_op->aead.aad.phys_addr =
364         op->phys_addr + (uintptr_t) aad - (uintptr_t) op;
365
366       sym_op->aead.digest.data = digest;
367       sym_op->aead.digest.phys_addr = digest_paddr;
368     }
369   else
370     {
371       sym_op->cipher.data.offset = cipher_off;
372       sym_op->cipher.data.length = cipher_len;
373
374       sym_op->auth.data.offset = auth_off;
375       sym_op->auth.data.length = auth_len;
376
377       sym_op->auth.digest.data = digest;
378       sym_op->auth.digest.phys_addr = digest_paddr;
379     }
380 }
381
382 #endif /* __DPDK_IPSEC_H__ */
383
384 /*
385  * fd.io coding-style-patch-verification: ON
386  *
387  * Local Variables:
388  * eval: (c-set-style "gnu")
389  * End:
390  */