2 * ipsec.c : IPSEC module functions
4 * Copyright (c) 2015 Cisco and/or its affiliates.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
18 #include <vnet/vnet.h>
19 #include <vnet/api_errno.h>
20 #include <vnet/ip/ip.h>
21 #include <vnet/interface.h>
22 #include <vnet/udp/udp_local.h>
24 #include <vnet/ipsec/ipsec.h>
25 #include <vnet/ipsec/esp.h>
26 #include <vnet/ipsec/ah.h>
27 #include <vnet/ipsec/ipsec_tun.h>
28 #include <vnet/ipsec/ipsec_itf.h>
30 /* Flow cache is sized for 1 million flows with a load factor of .25.
32 #define IPSEC4_OUT_SPD_DEFAULT_HASH_NUM_BUCKETS (1 << 22)
34 /* Flow cache is sized for 1 million flows with a load factor of .25.
36 #define IPSEC4_SPD_DEFAULT_HASH_NUM_BUCKETS (1 << 22)
38 ipsec_main_t ipsec_main;
39 esp_async_post_next_t esp_encrypt_async_next;
40 esp_async_post_next_t esp_decrypt_async_next;
43 ipsec_register_next_header (vlib_main_t *vm, u8 next_header,
44 const char *next_node)
46 ipsec_main_t *im = &ipsec_main;
47 const vlib_node_t *node = vlib_get_node_by_name (vm, (u8 *) next_node);
48 /* -post nodes (eg. esp4-decrypt-post) are siblings of non-post nodes (eg.
49 * esp4-decrypt) and will therefore have the same next index */
50 const vlib_node_t *esp_decrypt_nodes[] = {
51 vlib_get_node (vm, im->esp4_decrypt_node_index),
52 vlib_get_node (vm, im->esp6_decrypt_node_index),
53 vlib_get_node (vm, im->esp4_decrypt_tun_node_index),
54 vlib_get_node (vm, im->esp6_decrypt_tun_node_index),
59 /* looks for a next_index value that we can use for all esp decrypt nodes to
60 * avoid maintaining different next index arrays... */
62 slot = vlib_node_get_next (vm, esp_decrypt_nodes[0]->index, node->index);
63 max = vec_len (esp_decrypt_nodes[0]->next_nodes);
64 for (i = 1; i < ARRAY_LEN (esp_decrypt_nodes); i++)
66 /* if next node already exists, check it shares the same next_index */
68 vlib_node_get_next (vm, esp_decrypt_nodes[i]->index, node->index))
69 return clib_error_return (
70 0, "next node already exists with different next index");
71 /* compute a suitable slot from the max of all nodes next index */
72 max = clib_max (max, vec_len (esp_decrypt_nodes[i]->next_nodes));
77 /* next node not there yet, add it using the computed max */
79 for (i = 0; i < ARRAY_LEN (esp_decrypt_nodes); i++)
80 vlib_node_add_next_with_slot (vm, esp_decrypt_nodes[i]->index,
84 im->next_header_registrations[next_header] = slot;
89 ipsec_check_ah_support (ipsec_sa_t * sa)
91 ipsec_main_t *im = &ipsec_main;
93 if (sa->integ_alg == IPSEC_INTEG_ALG_NONE)
94 return clib_error_return (0, "unsupported none integ-alg");
96 if (!vnet_crypto_is_set_handler (im->integ_algs[sa->integ_alg].alg))
97 return clib_error_return (0, "No crypto engine support for %U",
98 format_ipsec_integ_alg, sa->integ_alg);
103 static clib_error_t *
104 ipsec_check_esp_support (ipsec_sa_t * sa)
106 ipsec_main_t *im = &ipsec_main;
108 if (IPSEC_INTEG_ALG_NONE != sa->integ_alg)
110 if (!vnet_crypto_is_set_handler (im->integ_algs[sa->integ_alg].alg))
111 return clib_error_return (0, "No crypto engine support for %U",
112 format_ipsec_integ_alg, sa->integ_alg);
114 if (IPSEC_CRYPTO_ALG_NONE != sa->crypto_alg)
116 if (!vnet_crypto_is_set_handler (im->crypto_algs[sa->crypto_alg].alg))
117 return clib_error_return (0, "No crypto engine support for %U",
118 format_ipsec_crypto_alg, sa->crypto_alg);
125 ipsec_add_del_sa_sess_cb (ipsec_main_t * im, u32 sa_index, u8 is_add)
127 ipsec_ah_backend_t *ah =
128 pool_elt_at_index (im->ah_backends, im->ah_current_backend);
129 if (ah->add_del_sa_sess_cb)
131 clib_error_t *err = ah->add_del_sa_sess_cb (sa_index, is_add);
135 ipsec_esp_backend_t *esp =
136 pool_elt_at_index (im->esp_backends, im->esp_current_backend);
137 if (esp->add_del_sa_sess_cb)
139 clib_error_t *err = esp->add_del_sa_sess_cb (sa_index, is_add);
147 ipsec_check_support_cb (ipsec_main_t * im, ipsec_sa_t * sa)
149 clib_error_t *error = 0;
151 if (PREDICT_FALSE (sa->protocol == IPSEC_PROTOCOL_AH))
153 ipsec_ah_backend_t *ah =
154 pool_elt_at_index (im->ah_backends, im->ah_current_backend);
155 ASSERT (ah->check_support_cb);
156 error = ah->check_support_cb (sa);
160 ipsec_esp_backend_t *esp =
161 pool_elt_at_index (im->esp_backends, im->esp_current_backend);
162 ASSERT (esp->check_support_cb);
163 error = esp->check_support_cb (sa);
170 ipsec_add_node (vlib_main_t * vm, const char *node_name,
171 const char *prev_node_name, u32 * out_node_index,
172 u32 * out_next_index)
174 vlib_node_t *prev_node, *node;
175 prev_node = vlib_get_node_by_name (vm, (u8 *) prev_node_name);
177 node = vlib_get_node_by_name (vm, (u8 *) node_name);
179 *out_node_index = node->index;
180 *out_next_index = vlib_node_add_next (vm, prev_node->index, node->index);
184 ipsec_unregister_udp_port (u16 port)
186 ipsec_main_t *im = &ipsec_main;
190 p = hash_get (im->udp_port_registrations, port);
198 udp_unregister_dst_port (vlib_get_main (), port, 1);
199 hash_unset (im->udp_port_registrations, port);
203 hash_unset (im->udp_port_registrations, port);
204 hash_set (im->udp_port_registrations, port, n_regs);
209 ipsec_register_udp_port (u16 port)
211 ipsec_main_t *im = &ipsec_main;
215 p = hash_get (im->udp_port_registrations, port);
217 n_regs = (p ? p[0] : 0);
220 udp_register_dst_port (vlib_get_main (), port,
221 ipsec4_tun_input_node.index, 1);
223 hash_unset (im->udp_port_registrations, port);
224 hash_set (im->udp_port_registrations, port, n_regs);
228 ipsec_register_ah_backend (vlib_main_t * vm, ipsec_main_t * im,
230 const char *ah4_encrypt_node_name,
231 const char *ah4_decrypt_node_name,
232 const char *ah6_encrypt_node_name,
233 const char *ah6_decrypt_node_name,
234 check_support_cb_t ah_check_support_cb,
235 add_del_sa_sess_cb_t ah_add_del_sa_sess_cb)
237 ipsec_ah_backend_t *b;
238 pool_get (im->ah_backends, b);
239 b->name = format (0, "%s%c", name, 0);
241 ipsec_add_node (vm, ah4_encrypt_node_name, "ipsec4-output-feature",
242 &b->ah4_encrypt_node_index, &b->ah4_encrypt_next_index);
243 ipsec_add_node (vm, ah4_decrypt_node_name, "ipsec4-input-feature",
244 &b->ah4_decrypt_node_index, &b->ah4_decrypt_next_index);
245 ipsec_add_node (vm, ah6_encrypt_node_name, "ipsec6-output-feature",
246 &b->ah6_encrypt_node_index, &b->ah6_encrypt_next_index);
247 ipsec_add_node (vm, ah6_decrypt_node_name, "ipsec6-input-feature",
248 &b->ah6_decrypt_node_index, &b->ah6_decrypt_next_index);
250 b->check_support_cb = ah_check_support_cb;
251 b->add_del_sa_sess_cb = ah_add_del_sa_sess_cb;
252 return b - im->ah_backends;
256 ipsec_register_esp_backend (
257 vlib_main_t *vm, ipsec_main_t *im, const char *name,
258 const char *esp4_encrypt_node_name, const char *esp4_encrypt_node_tun_name,
259 const char *esp4_decrypt_node_name, const char *esp4_decrypt_tun_node_name,
260 const char *esp6_encrypt_node_name, const char *esp6_encrypt_node_tun_name,
261 const char *esp6_decrypt_node_name, const char *esp6_decrypt_tun_node_name,
262 const char *esp_mpls_encrypt_node_tun_name,
263 check_support_cb_t esp_check_support_cb,
264 add_del_sa_sess_cb_t esp_add_del_sa_sess_cb,
265 enable_disable_cb_t enable_disable_cb)
267 ipsec_esp_backend_t *b;
269 pool_get (im->esp_backends, b);
270 b->name = format (0, "%s%c", name, 0);
272 ipsec_add_node (vm, esp4_encrypt_node_name, "ipsec4-output-feature",
273 &b->esp4_encrypt_node_index, &b->esp4_encrypt_next_index);
274 ipsec_add_node (vm, esp4_decrypt_node_name, "ipsec4-input-feature",
275 &b->esp4_decrypt_node_index, &b->esp4_decrypt_next_index);
276 ipsec_add_node (vm, esp6_encrypt_node_name, "ipsec6-output-feature",
277 &b->esp6_encrypt_node_index, &b->esp6_encrypt_next_index);
278 ipsec_add_node (vm, esp6_decrypt_node_name, "ipsec6-input-feature",
279 &b->esp6_decrypt_node_index, &b->esp6_decrypt_next_index);
280 ipsec_add_node (vm, esp4_decrypt_tun_node_name, "ipsec4-tun-input",
281 &b->esp4_decrypt_tun_node_index,
282 &b->esp4_decrypt_tun_next_index);
283 ipsec_add_node (vm, esp6_decrypt_tun_node_name, "ipsec6-tun-input",
284 &b->esp6_decrypt_tun_node_index,
285 &b->esp6_decrypt_tun_next_index);
287 b->esp6_encrypt_tun_node_index =
288 vlib_get_node_by_name (vm, (u8 *) esp6_encrypt_node_tun_name)->index;
289 b->esp_mpls_encrypt_tun_node_index =
290 vlib_get_node_by_name (vm, (u8 *) esp_mpls_encrypt_node_tun_name)->index;
291 b->esp4_encrypt_tun_node_index =
292 vlib_get_node_by_name (vm, (u8 *) esp4_encrypt_node_tun_name)->index;
294 b->check_support_cb = esp_check_support_cb;
295 b->add_del_sa_sess_cb = esp_add_del_sa_sess_cb;
296 b->enable_disable_cb = enable_disable_cb;
298 return b - im->esp_backends;
302 ipsec_rsc_in_use (ipsec_main_t * im)
304 /* return an error is crypto resource are in use */
305 if (pool_elts (ipsec_sa_pool) > 0)
306 return clib_error_return (0, "%d SA entries configured",
307 pool_elts (ipsec_sa_pool));
308 if (ipsec_itf_count () > 0)
309 return clib_error_return (0, "%d IPSec interface configured",
316 ipsec_select_ah_backend (ipsec_main_t * im, u32 backend_idx)
318 if (ipsec_rsc_in_use (im))
319 return VNET_API_ERROR_RSRC_IN_USE;
321 if (pool_is_free_index (im->ah_backends, backend_idx))
322 return VNET_API_ERROR_INVALID_VALUE;
324 ipsec_ah_backend_t *b = pool_elt_at_index (im->ah_backends, backend_idx);
325 im->ah_current_backend = backend_idx;
326 im->ah4_encrypt_node_index = b->ah4_encrypt_node_index;
327 im->ah4_decrypt_node_index = b->ah4_decrypt_node_index;
328 im->ah4_encrypt_next_index = b->ah4_encrypt_next_index;
329 im->ah4_decrypt_next_index = b->ah4_decrypt_next_index;
330 im->ah6_encrypt_node_index = b->ah6_encrypt_node_index;
331 im->ah6_decrypt_node_index = b->ah6_decrypt_node_index;
332 im->ah6_encrypt_next_index = b->ah6_encrypt_next_index;
333 im->ah6_decrypt_next_index = b->ah6_decrypt_next_index;
339 ipsec_select_esp_backend (ipsec_main_t * im, u32 backend_idx)
341 if (ipsec_rsc_in_use (im))
342 return VNET_API_ERROR_RSRC_IN_USE;
344 if (pool_is_free_index (im->esp_backends, backend_idx))
345 return VNET_API_ERROR_INVALID_VALUE;
347 /* disable current backend */
348 if (im->esp_current_backend != ~0)
350 ipsec_esp_backend_t *cb = pool_elt_at_index (im->esp_backends,
351 im->esp_current_backend);
352 if (cb->enable_disable_cb)
354 if ((cb->enable_disable_cb) (0) != 0)
359 ipsec_esp_backend_t *b = pool_elt_at_index (im->esp_backends, backend_idx);
360 im->esp_current_backend = backend_idx;
361 im->esp4_encrypt_node_index = b->esp4_encrypt_node_index;
362 im->esp4_decrypt_node_index = b->esp4_decrypt_node_index;
363 im->esp4_encrypt_next_index = b->esp4_encrypt_next_index;
364 im->esp4_decrypt_next_index = b->esp4_decrypt_next_index;
365 im->esp6_encrypt_node_index = b->esp6_encrypt_node_index;
366 im->esp6_decrypt_node_index = b->esp6_decrypt_node_index;
367 im->esp6_encrypt_next_index = b->esp6_encrypt_next_index;
368 im->esp6_decrypt_next_index = b->esp6_decrypt_next_index;
369 im->esp4_decrypt_tun_node_index = b->esp4_decrypt_tun_node_index;
370 im->esp4_decrypt_tun_next_index = b->esp4_decrypt_tun_next_index;
371 im->esp6_decrypt_tun_node_index = b->esp6_decrypt_tun_node_index;
372 im->esp6_decrypt_tun_next_index = b->esp6_decrypt_tun_next_index;
373 im->esp4_encrypt_tun_node_index = b->esp4_encrypt_tun_node_index;
374 im->esp6_encrypt_tun_node_index = b->esp6_encrypt_tun_node_index;
375 im->esp_mpls_encrypt_tun_node_index = b->esp_mpls_encrypt_tun_node_index;
377 if (b->enable_disable_cb)
379 if ((b->enable_disable_cb) (1) != 0)
386 ipsec_set_async_mode (u32 is_enabled)
388 ipsec_main_t *im = &ipsec_main;
391 vnet_crypto_request_async_mode (is_enabled);
393 im->async_mode = is_enabled;
395 /* change SA crypto op data */
396 pool_foreach (sa, ipsec_sa_pool)
399 (is_enabled ? sa->async_op_data.data : sa->sync_op_data.data);
404 crypto_engine_backend_register_post_node (vlib_main_t * vm)
406 esp_async_post_next_t *eit;
407 esp_async_post_next_t *dit;
409 eit = &esp_encrypt_async_next;
410 eit->esp4_post_next =
411 vnet_crypto_register_post_node (vm, "esp4-encrypt-post");
412 eit->esp6_post_next =
413 vnet_crypto_register_post_node (vm, "esp6-encrypt-post");
414 eit->esp4_tun_post_next =
415 vnet_crypto_register_post_node (vm, "esp4-encrypt-tun-post");
416 eit->esp6_tun_post_next =
417 vnet_crypto_register_post_node (vm, "esp6-encrypt-tun-post");
418 eit->esp_mpls_tun_post_next =
419 vnet_crypto_register_post_node (vm, "esp-mpls-encrypt-tun-post");
421 dit = &esp_decrypt_async_next;
422 dit->esp4_post_next =
423 vnet_crypto_register_post_node (vm, "esp4-decrypt-post");
424 dit->esp6_post_next =
425 vnet_crypto_register_post_node (vm, "esp6-decrypt-post");
426 dit->esp4_tun_post_next =
427 vnet_crypto_register_post_node (vm, "esp4-decrypt-tun-post");
428 dit->esp6_tun_post_next =
429 vnet_crypto_register_post_node (vm, "esp6-decrypt-tun-post");
432 static clib_error_t *
433 ipsec_init (vlib_main_t * vm)
436 ipsec_main_t *im = &ipsec_main;
437 ipsec_main_crypto_alg_t *a;
439 /* Backend registration requires the feature arcs to be set up */
440 if ((error = vlib_call_init_function (vm, vnet_feature_init)))
443 im->vnet_main = vnet_get_main ();
446 im->spd_index_by_spd_id = hash_create (0, sizeof (uword));
447 im->sa_index_by_sa_id = hash_create (0, sizeof (uword));
448 im->spd_index_by_sw_if_index = hash_create (0, sizeof (uword));
450 vlib_node_t *node = vlib_get_node_by_name (vm, (u8 *) "error-drop");
452 im->error_drop_node_index = node->index;
454 im->ah_current_backend = ~0;
455 im->esp_current_backend = ~0;
457 u32 idx = ipsec_register_ah_backend (vm, im, "crypto engine backend",
462 ipsec_check_ah_support,
465 im->ah_default_backend = idx;
466 int rv = ipsec_select_ah_backend (im, idx);
468 (void) (rv); // avoid warning
470 idx = ipsec_register_esp_backend (
471 vm, im, "crypto engine backend", "esp4-encrypt", "esp4-encrypt-tun",
472 "esp4-decrypt", "esp4-decrypt-tun", "esp6-encrypt", "esp6-encrypt-tun",
473 "esp6-decrypt", "esp6-decrypt-tun", "esp-mpls-encrypt-tun",
474 ipsec_check_esp_support, NULL, crypto_dispatch_enable_disable);
475 im->esp_default_backend = idx;
477 rv = ipsec_select_esp_backend (im, idx);
479 (void) (rv); // avoid warning
481 if ((error = vlib_call_init_function (vm, ipsec_cli_init)))
484 vec_validate (im->crypto_algs, IPSEC_CRYPTO_N_ALG - 1);
486 a = im->crypto_algs + IPSEC_CRYPTO_ALG_NONE;
487 a->enc_op_id = VNET_CRYPTO_OP_NONE;
488 a->dec_op_id = VNET_CRYPTO_OP_NONE;
489 a->alg = VNET_CRYPTO_ALG_NONE;
493 a = im->crypto_algs + IPSEC_CRYPTO_ALG_DES_CBC;
494 a->enc_op_id = VNET_CRYPTO_OP_DES_CBC_ENC;
495 a->dec_op_id = VNET_CRYPTO_OP_DES_CBC_DEC;
496 a->alg = VNET_CRYPTO_ALG_DES_CBC;
497 a->iv_size = a->block_align = 8;
499 a = im->crypto_algs + IPSEC_CRYPTO_ALG_3DES_CBC;
500 a->enc_op_id = VNET_CRYPTO_OP_3DES_CBC_ENC;
501 a->dec_op_id = VNET_CRYPTO_OP_3DES_CBC_DEC;
502 a->alg = VNET_CRYPTO_ALG_3DES_CBC;
503 a->iv_size = a->block_align = 8;
505 a = im->crypto_algs + IPSEC_CRYPTO_ALG_AES_CBC_128;
506 a->enc_op_id = VNET_CRYPTO_OP_AES_128_CBC_ENC;
507 a->dec_op_id = VNET_CRYPTO_OP_AES_128_CBC_DEC;
508 a->alg = VNET_CRYPTO_ALG_AES_128_CBC;
509 a->iv_size = a->block_align = 16;
511 a = im->crypto_algs + IPSEC_CRYPTO_ALG_AES_CBC_192;
512 a->enc_op_id = VNET_CRYPTO_OP_AES_192_CBC_ENC;
513 a->dec_op_id = VNET_CRYPTO_OP_AES_192_CBC_DEC;
514 a->alg = VNET_CRYPTO_ALG_AES_192_CBC;
515 a->iv_size = a->block_align = 16;
517 a = im->crypto_algs + IPSEC_CRYPTO_ALG_AES_CBC_256;
518 a->enc_op_id = VNET_CRYPTO_OP_AES_256_CBC_ENC;
519 a->dec_op_id = VNET_CRYPTO_OP_AES_256_CBC_DEC;
520 a->alg = VNET_CRYPTO_ALG_AES_256_CBC;
521 a->iv_size = a->block_align = 16;
523 a = im->crypto_algs + IPSEC_CRYPTO_ALG_AES_CTR_128;
524 a->enc_op_id = VNET_CRYPTO_OP_AES_128_CTR_ENC;
525 a->dec_op_id = VNET_CRYPTO_OP_AES_128_CTR_DEC;
526 a->alg = VNET_CRYPTO_ALG_AES_128_CTR;
530 a = im->crypto_algs + IPSEC_CRYPTO_ALG_AES_CTR_192;
531 a->enc_op_id = VNET_CRYPTO_OP_AES_192_CTR_ENC;
532 a->dec_op_id = VNET_CRYPTO_OP_AES_192_CTR_DEC;
533 a->alg = VNET_CRYPTO_ALG_AES_192_CTR;
537 a = im->crypto_algs + IPSEC_CRYPTO_ALG_AES_CTR_256;
538 a->enc_op_id = VNET_CRYPTO_OP_AES_256_CTR_ENC;
539 a->dec_op_id = VNET_CRYPTO_OP_AES_256_CTR_DEC;
540 a->alg = VNET_CRYPTO_ALG_AES_256_CTR;
544 a = im->crypto_algs + IPSEC_CRYPTO_ALG_AES_GCM_128;
545 a->enc_op_id = VNET_CRYPTO_OP_AES_128_GCM_ENC;
546 a->dec_op_id = VNET_CRYPTO_OP_AES_128_GCM_DEC;
547 a->alg = VNET_CRYPTO_ALG_AES_128_GCM;
552 a = im->crypto_algs + IPSEC_CRYPTO_ALG_AES_GCM_192;
553 a->enc_op_id = VNET_CRYPTO_OP_AES_192_GCM_ENC;
554 a->dec_op_id = VNET_CRYPTO_OP_AES_192_GCM_DEC;
555 a->alg = VNET_CRYPTO_ALG_AES_192_GCM;
560 a = im->crypto_algs + IPSEC_CRYPTO_ALG_AES_GCM_256;
561 a->enc_op_id = VNET_CRYPTO_OP_AES_256_GCM_ENC;
562 a->dec_op_id = VNET_CRYPTO_OP_AES_256_GCM_DEC;
563 a->alg = VNET_CRYPTO_ALG_AES_256_GCM;
568 vec_validate (im->integ_algs, IPSEC_INTEG_N_ALG - 1);
569 ipsec_main_integ_alg_t *i;
571 i = &im->integ_algs[IPSEC_INTEG_ALG_MD5_96];
572 i->op_id = VNET_CRYPTO_OP_MD5_HMAC;
573 i->alg = VNET_CRYPTO_ALG_HMAC_MD5;
576 i = &im->integ_algs[IPSEC_INTEG_ALG_SHA1_96];
577 i->op_id = VNET_CRYPTO_OP_SHA1_HMAC;
578 i->alg = VNET_CRYPTO_ALG_HMAC_SHA1;
581 i = &im->integ_algs[IPSEC_INTEG_ALG_SHA_256_96];
582 i->op_id = VNET_CRYPTO_OP_SHA1_HMAC;
583 i->alg = VNET_CRYPTO_ALG_HMAC_SHA256;
586 i = &im->integ_algs[IPSEC_INTEG_ALG_SHA_256_128];
587 i->op_id = VNET_CRYPTO_OP_SHA256_HMAC;
588 i->alg = VNET_CRYPTO_ALG_HMAC_SHA256;
591 i = &im->integ_algs[IPSEC_INTEG_ALG_SHA_384_192];
592 i->op_id = VNET_CRYPTO_OP_SHA384_HMAC;
593 i->alg = VNET_CRYPTO_ALG_HMAC_SHA384;
596 i = &im->integ_algs[IPSEC_INTEG_ALG_SHA_512_256];
597 i->op_id = VNET_CRYPTO_OP_SHA512_HMAC;
598 i->alg = VNET_CRYPTO_ALG_HMAC_SHA512;
601 vec_validate_aligned (im->ptd, vlib_num_workers (), CLIB_CACHE_LINE_BYTES);
604 crypto_engine_backend_register_post_node (vm);
606 im->ipsec4_out_spd_hash_tbl = NULL;
607 im->output_flow_cache_flag = 0;
608 im->ipsec4_out_spd_flow_cache_entries = 0;
610 im->ipsec4_out_spd_hash_num_buckets =
611 IPSEC4_OUT_SPD_DEFAULT_HASH_NUM_BUCKETS;
613 im->ipsec4_in_spd_hash_tbl = NULL;
614 im->input_flow_cache_flag = 0;
615 im->ipsec4_in_spd_flow_cache_entries = 0;
616 im->input_epoch_count = 0;
617 im->ipsec4_in_spd_hash_num_buckets = IPSEC4_SPD_DEFAULT_HASH_NUM_BUCKETS;
619 vec_validate_init_empty_aligned (im->next_header_registrations, 255, ~0,
620 CLIB_CACHE_LINE_BYTES);
625 VLIB_INIT_FUNCTION (ipsec_init);
627 static clib_error_t *
628 ipsec_config (vlib_main_t *vm, unformat_input_t *input)
630 ipsec_main_t *im = &ipsec_main;
631 unformat_input_t sub_input;
633 u32 ipsec4_out_spd_hash_num_buckets;
634 u32 ipsec4_in_spd_hash_num_buckets;
636 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
638 if (unformat (input, "ipv4-outbound-spd-flow-cache on"))
639 im->output_flow_cache_flag = 1;
640 else if (unformat (input, "ipv4-outbound-spd-flow-cache off"))
641 im->output_flow_cache_flag = 0;
642 else if (unformat (input, "ipv4-outbound-spd-hash-buckets %d",
643 &ipsec4_out_spd_hash_num_buckets))
645 /* Size of hash is power of 2 >= number of buckets */
646 im->ipsec4_out_spd_hash_num_buckets =
647 1ULL << max_log2 (ipsec4_out_spd_hash_num_buckets);
649 else if (unformat (input, "ipv4-inbound-spd-flow-cache on"))
650 im->input_flow_cache_flag = 1;
651 else if (unformat (input, "ipv4-inbound-spd-flow-cache off"))
652 im->input_flow_cache_flag = 0;
653 else if (unformat (input, "ipv4-inbound-spd-hash-buckets %d",
654 &ipsec4_in_spd_hash_num_buckets))
656 im->ipsec4_in_spd_hash_num_buckets =
657 1ULL << max_log2 (ipsec4_in_spd_hash_num_buckets);
659 else if (unformat (input, "ip4 %U", unformat_vlib_cli_sub_input,
662 uword table_size = ~0;
665 while (unformat_check_input (&sub_input) != UNFORMAT_END_OF_INPUT)
667 if (unformat (&sub_input, "num-buckets %u", &n_buckets))
670 return clib_error_return (0, "unknown input `%U'",
671 format_unformat_error, &sub_input);
674 ipsec_tun_table_init (AF_IP4, table_size, n_buckets);
676 else if (unformat (input, "ip6 %U", unformat_vlib_cli_sub_input,
679 uword table_size = ~0;
682 while (unformat_check_input (&sub_input) != UNFORMAT_END_OF_INPUT)
684 if (unformat (&sub_input, "num-buckets %u", &n_buckets))
687 return clib_error_return (0, "unknown input `%U'",
688 format_unformat_error, &sub_input);
691 ipsec_tun_table_init (AF_IP6, table_size, n_buckets);
694 return clib_error_return (0, "unknown input `%U'",
695 format_unformat_error, input);
697 if (im->output_flow_cache_flag)
699 vec_add2 (im->ipsec4_out_spd_hash_tbl, im->ipsec4_out_spd_hash_tbl,
700 im->ipsec4_out_spd_hash_num_buckets);
702 if (im->input_flow_cache_flag)
704 vec_add2 (im->ipsec4_in_spd_hash_tbl, im->ipsec4_in_spd_hash_tbl,
705 im->ipsec4_in_spd_hash_num_buckets);
711 VLIB_CONFIG_FUNCTION (ipsec_config, "ipsec");
714 * fd.io coding-style-patch-verification: ON
717 * eval: (c-set-style "gnu")