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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
15 #ifndef __DPDK_IPSEC_H__
16 #define __DPDK_IPSEC_H__
18 #include <vnet/vnet.h>
19 #include <vppinfra/cache.h>
20 #include <vnet/ipsec/ipsec.h>
23 #include <rte_config.h>
24 #include <rte_crypto.h>
25 #include <rte_cryptodev.h>
28 #define always_inline static inline
30 #define always_inline static inline __attribute__ ((__always_inline__))
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")
43 #define _(f,s) DPDK_CRYPTO_INPUT_NEXT_##f,
44 foreach_dpdk_crypto_input_next
46 DPDK_CRYPTO_INPUT_N_NEXT,
47 } dpdk_crypto_input_next_t;
49 #define MAX_QP_PER_LCORE 16
62 dpdk_gcm_cnt_blk cb __attribute__ ((aligned (16)));
70 struct rte_crypto_op **ops;
71 u16 cipher_resource_idx[IPSEC_CRYPTO_N_ALG];
72 u16 auth_resource_idx[IPSEC_INTEG_N_ALG];
73 CLIB_CACHE_LINE_ALIGN_MARK (pad);
74 } crypto_worker_main_t __attribute__ ((aligned (CLIB_CACHE_LINE_BYTES)));
78 CLIB_ALIGN_MARK (pad, 8); /* align up to 8 bytes for 32bit builds */
80 enum rte_crypto_sym_xform_type type;
94 u8 cipher_support[IPSEC_CRYPTO_N_ALG];
95 u8 auth_support[IPSEC_INTEG_N_ALG];
121 struct rte_crypto_op *ops[VLIB_FRAME_SIZE];
122 u32 bi[VLIB_FRAME_SIZE];
123 } crypto_resource_t __attribute__ ((aligned (CLIB_CACHE_LINE_BYTES)));
128 struct rte_cryptodev_sym_session *session;
129 } crypto_session_disposal_t;
133 CLIB_ALIGN_MARK (pad, 16); /* align up to 16 bytes for 32bit builds */
134 struct rte_cryptodev_sym_session *session;
136 } crypto_session_by_drv_t;
140 /* Required for vec_validate_aligned */
141 CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
142 struct rte_mempool *crypto_op;
143 struct rte_mempool *session_h;
144 struct rte_mempool **session_drv;
145 crypto_session_disposal_t *session_disposal;
146 uword *session_by_sa_index;
147 u64 crypto_op_get_failed;
148 u64 session_h_failed;
149 u64 *session_drv_failed;
150 crypto_session_by_drv_t *session_by_drv_id_and_sa_index;
151 clib_spinlock_t lockp;
156 crypto_worker_main_t *workers_main;
158 crypto_resource_t *resource;
159 crypto_alg_t *cipher_algs;
160 crypto_alg_t *auth_algs;
163 u64 session_timeout; /* nsec */
165 } dpdk_crypto_main_t;
167 extern dpdk_crypto_main_t dpdk_crypto_main;
169 static const u8 pad_data[] =
170 { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 0 };
172 void crypto_auto_placement (void);
174 clib_error_t *create_sym_session (struct rte_cryptodev_sym_session **session,
175 u32 sa_idx, crypto_resource_t * res,
176 crypto_worker_main_t * cwm, u8 is_outbound);
178 static_always_inline u32
183 sizeof (struct rte_crypto_op) + sizeof (struct rte_crypto_sym_op);
185 return ((op_size + align - 1) & ~(align - 1)) + sizeof (dpdk_op_priv_t);
188 static_always_inline u32
189 crypto_op_get_priv_offset (void)
191 const u32 align = 16;
194 offset = sizeof (struct rte_crypto_op) + sizeof (struct rte_crypto_sym_op);
195 offset = (offset + align - 1) & ~(align - 1);
200 static_always_inline dpdk_op_priv_t *
201 crypto_op_get_priv (struct rte_crypto_op * op)
203 return (dpdk_op_priv_t *) (((u8 *) op) + crypto_op_get_priv_offset ());
207 static_always_inline void
208 add_session_by_drv_and_sa_idx (struct rte_cryptodev_sym_session *session,
209 crypto_data_t * data, u32 drv_id, u32 sa_idx)
211 crypto_session_by_drv_t *sbd;
212 vec_validate_aligned (data->session_by_drv_id_and_sa_index, sa_idx,
213 CLIB_CACHE_LINE_BYTES);
214 sbd = vec_elt_at_index (data->session_by_drv_id_and_sa_index, sa_idx);
215 sbd->dev_mask |= 1L << drv_id;
216 sbd->session = session;
219 static_always_inline struct rte_cryptodev_sym_session *
220 get_session_by_drv_and_sa_idx (crypto_data_t * data, u32 drv_id, u32 sa_idx)
222 crypto_session_by_drv_t *sess_by_sa;
223 if (_vec_len (data->session_by_drv_id_and_sa_index) <= sa_idx)
226 vec_elt_at_index (data->session_by_drv_id_and_sa_index, sa_idx);
227 return (sess_by_sa->dev_mask & (1L << drv_id)) ? sess_by_sa->session : NULL;
230 static_always_inline clib_error_t *
231 crypto_get_session (struct rte_cryptodev_sym_session ** session,
233 crypto_resource_t * res,
234 crypto_worker_main_t * cwm, u8 is_outbound)
236 dpdk_crypto_main_t *dcm = &dpdk_crypto_main;
238 struct rte_cryptodev_sym_session *sess;
240 data = vec_elt_at_index (dcm->data, res->numa);
241 sess = get_session_by_drv_and_sa_idx (data, res->drv_id, sa_idx);
243 if (PREDICT_FALSE (!sess))
244 return create_sym_session (session, sa_idx, res, cwm, is_outbound);
251 static_always_inline u16
252 get_resource (crypto_worker_main_t * cwm, ipsec_sa_t * sa)
254 u16 cipher_res = cwm->cipher_resource_idx[sa->crypto_alg];
255 u16 auth_res = cwm->auth_resource_idx[sa->integ_alg];
258 /* Not allowed to setup SA with no-aead-cipher/NULL or NULL/NULL */
260 is_aead = ((sa->crypto_alg == IPSEC_CRYPTO_ALG_AES_GCM_128) ||
261 (sa->crypto_alg == IPSEC_CRYPTO_ALG_AES_GCM_192) ||
262 (sa->crypto_alg == IPSEC_CRYPTO_ALG_AES_GCM_256));
264 if (sa->crypto_alg == IPSEC_CRYPTO_ALG_NONE)
267 if (cipher_res == auth_res)
276 static_always_inline i32
277 crypto_alloc_ops (u8 numa, struct rte_crypto_op ** ops, u32 n)
279 dpdk_crypto_main_t *dcm = &dpdk_crypto_main;
280 crypto_data_t *data = vec_elt_at_index (dcm->data, numa);
283 ret = rte_mempool_get_bulk (data->crypto_op, (void **) ops, n);
286 data->crypto_op_get_failed += ! !ret;
292 static_always_inline void
293 crypto_free_ops (u8 numa, struct rte_crypto_op **ops, u32 n)
295 dpdk_crypto_main_t *dcm = &dpdk_crypto_main;
296 crypto_data_t *data = vec_elt_at_index (dcm->data, numa);
301 rte_mempool_put_bulk (data->crypto_op, (void **) ops, n);
304 static_always_inline void
305 crypto_enqueue_ops (vlib_main_t * vm, crypto_worker_main_t * cwm,
306 u32 node_index, u32 error, u8 numa)
308 dpdk_crypto_main_t *dcm = &dpdk_crypto_main;
309 crypto_resource_t *res;
313 vec_foreach (res_idx, cwm->resource_idx)
316 res = vec_elt_at_index (dcm->resource, res_idx[0]);
321 enq = rte_cryptodev_enqueue_burst (res->dev_id, res->qp_id,
322 res->ops, res->n_ops);
323 res->inflights += enq;
325 if (PREDICT_FALSE (enq < res->n_ops))
327 crypto_free_ops (numa, &res->ops[enq], res->n_ops - enq);
328 vlib_buffer_free (vm, &res->bi[enq], res->n_ops - enq);
330 vlib_node_increment_counter (vm, node_index, error,
338 static_always_inline void
339 crypto_set_icb (dpdk_gcm_cnt_blk * icb, u32 salt, u32 seq, u32 seq_hi)
346 static_always_inline void
347 crypto_op_setup (u8 is_aead, struct rte_mbuf *mb0,
348 struct rte_crypto_op *op, void *session,
349 u32 cipher_off, u32 cipher_len,
350 u32 auth_off, u32 auth_len,
351 u8 * aad, u8 * digest, u64 digest_paddr)
353 struct rte_crypto_sym_op *sym_op;
355 sym_op = (struct rte_crypto_sym_op *) (op + 1);
358 sym_op->session = session;
362 sym_op->aead.data.offset = cipher_off;
363 sym_op->aead.data.length = cipher_len;
365 sym_op->aead.aad.data = aad;
366 sym_op->aead.aad.phys_addr =
367 op->phys_addr + (uintptr_t) aad - (uintptr_t) op;
369 sym_op->aead.digest.data = digest;
370 sym_op->aead.digest.phys_addr = digest_paddr;
374 sym_op->cipher.data.offset = cipher_off;
375 sym_op->cipher.data.length = cipher_len;
377 sym_op->auth.data.offset = auth_off;
378 sym_op->auth.data.length = auth_len;
380 sym_op->auth.digest.data = digest;
381 sym_op->auth.digest.phys_addr = digest_paddr;
385 #endif /* __DPDK_IPSEC_H__ */
388 * fd.io coding-style-patch-verification: ON
391 * eval: (c-set-style "gnu")