FD.io VPP  v17.04-9-g99c0734
Vector Packet Processing
ipsec.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016 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 #include <vnet/vnet.h>
16 #include <vnet/ip/ip.h>
17 #include <vnet/api_errno.h>
18 #include <vnet/ipsec/ipsec.h>
19 #include <vlib/node_funcs.h>
20 
21 #include <dpdk/device/dpdk.h>
22 #include <dpdk/ipsec/ipsec.h>
23 #include <dpdk/ipsec/esp.h>
24 
25 #define DPDK_CRYPTO_NB_SESS_OBJS 20000
26 #define DPDK_CRYPTO_CACHE_SIZE 512
27 #define DPDK_CRYPTO_PRIV_SIZE 128
28 #define DPDK_CRYPTO_N_QUEUE_DESC 1024
29 #define DPDK_CRYPTO_NB_COPS (1024 * 4)
30 
31 static int
32 add_del_sa_sess (u32 sa_index, u8 is_add)
33 {
36  u8 skip_master = vlib_num_workers () > 0;
37 
38  /* *INDENT-OFF* */
39  vec_foreach (cwm, dcm->workers_main)
40  {
41  crypto_sa_session_t *sa_sess;
42  u8 is_outbound;
43 
44  if (skip_master)
45  {
46  skip_master = 0;
47  continue;
48  }
49 
50  for (is_outbound = 0; is_outbound < 2; is_outbound++)
51  {
52  if (is_add)
53  {
54  pool_get (cwm->sa_sess_d[is_outbound], sa_sess);
55  }
56  else
57  {
58  u8 dev_id;
59 
60  sa_sess = pool_elt_at_index (cwm->sa_sess_d[is_outbound], sa_index);
61  dev_id = cwm->qp_data[sa_sess->qp_index].dev_id;
62 
63  if (!sa_sess->sess)
64  continue;
65 
66  if (rte_cryptodev_sym_session_free(dev_id, sa_sess->sess))
67  {
68  clib_warning("failed to free session");
69  return -1;
70  }
71  memset(sa_sess, 0, sizeof(sa_sess[0]));
72  }
73  }
74  }
75  /* *INDENT-OFF* */
76 
77  return 0;
78 }
79 
80 static void
82  u8 cdev_id, u16 qp_id, u8 is_outbound, u16 * idx)
83 {
84  crypto_qp_data_t *qpd;
85 
86  /* *INDENT-OFF* */
87  vec_foreach_index (*idx, cwm->qp_data)
88  {
89  qpd = vec_elt_at_index(cwm->qp_data, *idx);
90 
91  if (qpd->dev_id == cdev_id && qpd->qp_id == qp_id &&
92  qpd->is_outbound == is_outbound)
93  return;
94  }
95  /* *INDENT-ON* */
96 
97  vec_add2 (cwm->qp_data, qpd, 1);
98 
99  qpd->dev_id = cdev_id;
100  qpd->qp_id = qp_id;
101  qpd->is_outbound = is_outbound;
102 }
103 
104 /*
105  * return:
106  * 0: already exist
107  * 1: mapped
108  */
109 static int
111  u8 cdev_id, u16 qp, u8 is_outbound,
112  const struct rte_cryptodev_capabilities *cipher_cap,
113  const struct rte_cryptodev_capabilities *auth_cap)
114 {
115  u16 qp_index;
116  uword key = 0, data, *ret;
118 
119  p_key->cipher_algo = (u8) cipher_cap->sym.cipher.algo;
120  p_key->auth_algo = (u8) auth_cap->sym.auth.algo;
121  p_key->is_outbound = is_outbound;
122 
123  ret = hash_get (cwm->algo_qp_map, key);
124  if (ret)
125  return 0;
126 
127  update_qp_data (cwm, cdev_id, qp, is_outbound, &qp_index);
128 
129  data = (uword) qp_index;
130  hash_set (cwm->algo_qp_map, key, data);
131 
132  return 1;
133 }
134 
135 /*
136  * return:
137  * 0: already exist
138  * 1: mapped
139  */
140 static int
142  struct rte_cryptodev_info *dev_info, u8 cdev_id,
143  u16 qp, u8 is_outbound)
144 {
145  const struct rte_cryptodev_capabilities *i, *j;
146  u32 mapped = 0;
147 
148  for (i = dev_info->capabilities; i->op != RTE_CRYPTO_OP_TYPE_UNDEFINED; i++)
149  {
150  if (i->sym.xform_type != RTE_CRYPTO_SYM_XFORM_CIPHER)
151  continue;
152 
153  if (check_algo_is_supported (i, NULL) != 0)
154  continue;
155 
156  for (j = dev_info->capabilities; j->op != RTE_CRYPTO_OP_TYPE_UNDEFINED;
157  j++)
158  {
159  if (j->sym.xform_type != RTE_CRYPTO_SYM_XFORM_AUTH)
160  continue;
161 
162  if (check_algo_is_supported (j, NULL) != 0)
163  continue;
164 
165  mapped |= add_mapping (cwm, cdev_id, qp, is_outbound, i, j);
166  }
167  }
168 
169  return mapped;
170 }
171 
172 static int
174 {
175  u32 n_qs = 0;
176  u8 cdev_id;
177  u32 n_req_qs = 2;
178 
179  if (vlib_num_workers () > 0)
180  n_req_qs = vlib_num_workers () * 2;
181 
182  for (cdev_id = 0; cdev_id < rte_cryptodev_count (); cdev_id++)
183  {
184  struct rte_cryptodev_info cdev_info;
185 
186  rte_cryptodev_info_get (cdev_id, &cdev_info);
187 
188  if (!
189  (cdev_info.feature_flags & RTE_CRYPTODEV_FF_SYM_OPERATION_CHAINING))
190  continue;
191 
192  n_qs += cdev_info.max_nb_queue_pairs;
193  }
194 
195  if (n_qs >= n_req_qs)
196  return 0;
197  else
198  return -1;
199 }
200 
201 static clib_error_t *
203 {
204  if (sa->crypto_alg == IPSEC_CRYPTO_ALG_AES_GCM_128)
205  {
206  if (sa->integ_alg != IPSEC_INTEG_ALG_NONE)
207  return clib_error_return (0, "unsupported integ-alg %U with "
208  "crypto-algo aes-gcm-128",
210  sa->integ_alg = IPSEC_INTEG_ALG_AES_GCM_128;
211  }
212  else
213  {
214  if (sa->integ_alg == IPSEC_INTEG_ALG_NONE ||
215  sa->integ_alg == IPSEC_INTEG_ALG_AES_GCM_128)
216  return clib_error_return (0, "unsupported integ-alg %U",
218  }
219 
220  return 0;
221 }
222 
223 static uword
225  vlib_frame_t * f)
226 {
228  ipsec_main_t *im = &ipsec_main;
231  struct rte_cryptodev_config dev_conf;
232  struct rte_cryptodev_qp_conf qp_conf;
233  struct rte_cryptodev_info cdev_info;
234  struct rte_mempool *rmp;
235  i32 dev_id, ret;
236  u32 i, skip_master;
237 
238  if (!conf->cryptodev)
239  {
240  clib_warning ("DPDK Cryptodev support is disabled, "
241  "default to OpenSSL IPsec");
242  return 0;
243  }
244 
245  if (check_cryptodev_queues () < 0)
246  {
247  conf->cryptodev = 0;
248  clib_warning ("not enough Cryptodevs, default to OpenSSL IPsec");
249  return 0;
250  }
251 
252  vec_alloc (dcm->workers_main, tm->n_vlib_mains);
253  _vec_len (dcm->workers_main) = tm->n_vlib_mains;
254 
255  fprintf (stdout, "DPDK Cryptodevs info:\n");
256  fprintf (stdout, "dev_id\tn_qp\tnb_obj\tcache_size\n");
257  /* HW cryptodevs have higher dev_id, use HW first */
258  for (dev_id = rte_cryptodev_count () - 1; dev_id >= 0; dev_id--)
259  {
260  u16 max_nb_qp, qp = 0;
261  skip_master = vlib_num_workers () > 0;
262 
263  rte_cryptodev_info_get (dev_id, &cdev_info);
264 
265  if (!
266  (cdev_info.feature_flags & RTE_CRYPTODEV_FF_SYM_OPERATION_CHAINING))
267  continue;
268 
269  max_nb_qp = cdev_info.max_nb_queue_pairs;
270 
271  for (i = 0; i < tm->n_vlib_mains; i++)
272  {
273  u8 is_outbound;
275  uword *map;
276 
277  if (skip_master)
278  {
279  skip_master = 0;
280  continue;
281  }
282 
283  cwm = vec_elt_at_index (dcm->workers_main, i);
284  map = cwm->algo_qp_map;
285 
286  if (!map)
287  {
288  map = hash_create (0, sizeof (crypto_worker_qp_key_t));
289  if (!map)
290  {
291  clib_warning ("unable to create hash table for worker %u",
292  vlib_mains[i]->cpu_index);
293  goto error;
294  }
295  cwm->algo_qp_map = map;
296  }
297 
298  for (is_outbound = 0; is_outbound < 2 && qp < max_nb_qp;
299  is_outbound++)
300  qp += add_cdev_mapping (cwm, &cdev_info, dev_id, qp, is_outbound);
301  }
302 
303  if (qp == 0)
304  continue;
305 
306  dev_conf.socket_id = rte_cryptodev_socket_id (dev_id);
307  dev_conf.nb_queue_pairs = cdev_info.max_nb_queue_pairs;
308  dev_conf.session_mp.nb_objs = DPDK_CRYPTO_NB_SESS_OBJS;
309  dev_conf.session_mp.cache_size = DPDK_CRYPTO_CACHE_SIZE;
310 
311  ret = rte_cryptodev_configure (dev_id, &dev_conf);
312  if (ret < 0)
313  {
314  clib_warning ("cryptodev %u config error", dev_id);
315  goto error;
316  }
317 
318  qp_conf.nb_descriptors = DPDK_CRYPTO_N_QUEUE_DESC;
319  for (qp = 0; qp < dev_conf.nb_queue_pairs; qp++)
320  {
321  ret = rte_cryptodev_queue_pair_setup (dev_id, qp, &qp_conf,
322  dev_conf.socket_id);
323  if (ret < 0)
324  {
325  clib_warning ("cryptodev %u qp %u setup error", dev_id, qp);
326  goto error;
327  }
328  }
329  vec_validate_aligned (dcm->cop_pools, dev_conf.socket_id,
331 
332  if (!vec_elt (dcm->cop_pools, dev_conf.socket_id))
333  {
334  u8 *pool_name = format (0, "crypto_op_pool_socket%u%c",
335  dev_conf.socket_id, 0);
336 
337  rmp = rte_crypto_op_pool_create ((char *) pool_name,
338  RTE_CRYPTO_OP_TYPE_SYMMETRIC,
340  (1 + vlib_num_workers ()),
343  dev_conf.socket_id);
344  vec_free (pool_name);
345 
346  if (!rmp)
347  {
348  clib_warning ("failed to allocate mempool on socket %u",
349  dev_conf.socket_id);
350  goto error;
351  }
352  vec_elt (dcm->cop_pools, dev_conf.socket_id) = rmp;
353  }
354 
355  fprintf (stdout, "%u\t%u\t%u\t%u\n", dev_id, dev_conf.nb_queue_pairs,
357  }
358 
359  dpdk_esp_init ();
360 
361  /* Add new next node and set as default */
362  vlib_node_t *node, *next_node;
363 
364  next_node = vlib_get_node_by_name (vm, (u8 *) "dpdk-esp-encrypt");
365  ASSERT (next_node);
366  node = vlib_get_node_by_name (vm, (u8 *) "ipsec-output-ip4");
367  ASSERT (node);
368  im->esp_encrypt_node_index = next_node->index;
370  vlib_node_add_next (vm, node->index, next_node->index);
371 
372  next_node = vlib_get_node_by_name (vm, (u8 *) "dpdk-esp-decrypt");
373  ASSERT (next_node);
374  node = vlib_get_node_by_name (vm, (u8 *) "ipsec-input-ip4");
375  ASSERT (node);
376  im->esp_decrypt_node_index = next_node->index;
378  vlib_node_add_next (vm, node->index, next_node->index);
379 
382 
383  for (i = 1; i < tm->n_vlib_mains; i++)
385  VLIB_NODE_STATE_POLLING);
386 
387  /* TODO cryptodev counters */
388 
389  return 0;
390 
391 error:
392  ;
394  struct rte_mempool **mp;
395  /* *INDENT-OFF* */
396  vec_foreach (cwm, dcm->workers_main)
397  hash_free (cwm->algo_qp_map);
398 
399  vec_foreach (mp, dcm->cop_pools)
400  {
401  if (mp)
402  rte_mempool_free (mp[0]);
403  }
404  /* *INDENT-ON* */
405  vec_free (dcm->workers_main);
406  vec_free (dcm->cop_pools);
407 
408  return 0;
409 }
410 
411 /* *INDENT-OFF* */
413  .function = dpdk_ipsec_process,
414  .type = VLIB_NODE_TYPE_PROCESS,
415  .name = "dpdk-ipsec-process",
416  .process_log2_n_stack_bytes = 17,
417 };
418 /* *INDENT-ON* */
419 
420 /*
421  * fd.io coding-style-patch-verification: ON
422  *
423  * Local Variables:
424  * eval: (c-set-style "gnu")
425  * End:
426  */
static int add_mapping(crypto_worker_main_t *cwm, u8 cdev_id, u16 qp, u8 is_outbound, const struct rte_cryptodev_capabilities *cipher_cap, const struct rte_cryptodev_capabilities *auth_cap)
Definition: ipsec.c:110
#define vec_foreach_index(var, v)
Iterate over vector indices.
#define hash_set(h, key, value)
Definition: hash.h:254
sll srl srl sll sra u16x4 i
Definition: vector_sse2.h:343
i32(* add_del_sa_sess_cb)(u32 sa_index, u8 is_add)
Definition: ipsec.h:239
#define NULL
Definition: clib.h:55
u32 index
Definition: node.h:237
u16 is_outbound
Definition: ipsec.h:62
#define DPDK_CRYPTO_NB_SESS_OBJS
Definition: ipsec.c:25
ipsec_integ_alg_t integ_alg
Definition: ipsec.h:110
#define vec_add2(V, P, N)
Add N elements to end of vector V, return pointer to new elements in P.
Definition: vec.h:561
#define DPDK_CRYPTO_PRIV_SIZE
Definition: ipsec.c:27
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:418
#define vec_validate_aligned(V, I, A)
Make sure vector is long enough for given index (no header, specified alignment)
Definition: vec.h:447
vlib_node_registration_t dpdk_crypto_input_node
(constructor) VLIB_REGISTER_NODE (dpdk_crypto_input_node)
Definition: crypto_node.c:60
#define pool_get(P, E)
Allocate an object E from a pool P (unspecified alignment).
Definition: pool.h:200
#define vec_alloc(V, N)
Allocate space for N more elements (no header, unspecified alignment)
Definition: vec.h:279
vlib_main_t ** vlib_mains
Definition: buffer.c:285
static uword vlib_node_add_next(vlib_main_t *vm, uword node, uword next_node)
Definition: node_funcs.h:1062
dpdk_crypto_main_t dpdk_crypto_main
Definition: ipsec.h:88
static void update_qp_data(crypto_worker_main_t *cwm, u8 cdev_id, u16 qp_id, u8 is_outbound, u16 *idx)
Definition: ipsec.c:81
static_always_inline void dpdk_esp_init()
Definition: esp.h:44
uword * algo_qp_map
Definition: ipsec.h:79
u32 esp_encrypt_next_index
Definition: ipsec.h:275
dpdk_config_main_t dpdk_config_main
Definition: dpdk.h:349
ipsec_main_t ipsec_main
Definition: ipsec.h:282
int i32
Definition: types.h:81
#define vec_elt_at_index(v, i)
Get vector value at index i checking that i is in bounds.
#define clib_error_return(e, args...)
Definition: error.h:111
ipsec_main_callbacks_t cb
Definition: ipsec.h:279
#define hash_get(h, key)
Definition: hash.h:248
#define pool_elt_at_index(p, i)
Returns pointer to element at given index.
Definition: pool.h:397
clib_error_t *(* check_support_cb)(ipsec_sa_t *sa)
Definition: ipsec.h:240
#define hash_free(h)
Definition: hash.h:286
#define DPDK_CRYPTO_NB_COPS
Definition: ipsec.c:29
static int check_cryptodev_queues()
Definition: ipsec.c:173
vlib node functions
vlib_main_t * vm
Definition: buffer.c:276
u32 esp_encrypt_node_index
Definition: ipsec.h:272
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:340
u32 esp_decrypt_next_index
Definition: ipsec.h:276
#define clib_warning(format, args...)
Definition: error.h:59
static int add_del_sa_sess(u32 sa_index, u8 is_add)
Definition: ipsec.c:32
vlib_node_t * vlib_get_node_by_name(vlib_main_t *vm, u8 *name)
Definition: node.c:45
static int add_cdev_mapping(crypto_worker_main_t *cwm, struct rte_cryptodev_info *dev_info, u8 cdev_id, u16 qp, u8 is_outbound)
Definition: ipsec.c:141
#define hash_create(elts, value_bytes)
Definition: hash.h:658
#define ASSERT(truth)
unsigned int u32
Definition: types.h:88
struct rte_mempool ** cop_pools
Definition: ipsec.h:84
crypto_worker_main_t * workers_main
Definition: ipsec.h:85
#define DPDK_CRYPTO_CACHE_SIZE
Definition: ipsec.c:26
static void vlib_node_set_state(vlib_main_t *vm, u32 node_index, vlib_node_state_t new_state)
Set node dispatch state.
Definition: node_funcs.h:146
crypto_qp_data_t * qp_data
Definition: ipsec.h:78
crypto_sa_session_t * sa_sess_d[2]
Definition: ipsec.h:77
u64 uword
Definition: types.h:112
#define vec_elt(v, i)
Get vector value at index i.
u8 * format_ipsec_integ_alg(u8 *s, va_list *args)
Definition: ipsec_format.c:90
unsigned short u16
Definition: types.h:57
unsigned char u8
Definition: types.h:56
static vlib_node_registration_t dpdk_ipsec_process_node
(constructor) VLIB_REGISTER_NODE (dpdk_ipsec_process_node)
Definition: ipsec.c:412
static_always_inline int check_algo_is_supported(const struct rte_cryptodev_capabilities *cap, char *name)
Definition: ipsec.h:143
#define DPDK_CRYPTO_N_QUEUE_DESC
Definition: ipsec.c:28
ipsec_crypto_alg_t crypto_alg
Definition: ipsec.h:106
#define VLIB_REGISTER_NODE(x,...)
Definition: node.h:143
static vlib_thread_main_t * vlib_get_thread_main()
Definition: global_funcs.h:32
static u32 vlib_num_workers()
Definition: threads.h:345
static uword dpdk_ipsec_process(vlib_main_t *vm, vlib_node_runtime_t *rt, vlib_frame_t *f)
Definition: ipsec.c:224
#define vec_foreach(var, vec)
Vector iterator.
#define CLIB_CACHE_LINE_BYTES
Definition: cache.h:67
static clib_error_t * dpdk_ipsec_check_support(ipsec_sa_t *sa)
Definition: ipsec.c:202
u32 esp_decrypt_node_index
Definition: ipsec.h:273