Skip to content

Commit f479eeb

Browse files
nplanelciscodmarion
authored andcommitted
crypto: make configurable crypto engines
Add a configuration sections crypto-engines, it works like plugins syntax : The following configuration will load and register only openssl engine ''' crypto-engines { default {disable} openssl {enable} } ''' And this one will load all engines except openssl ''' crypto-engines { default {enable} openssl {disable} } ''' Type: feature Change-Id: Ia637db93b497d0c4333704f3c024e85de3941791 Signed-off-by: Nicolas PLANEL <nplanel@cisco.com> Signed-off-by: Nicolas PLANEL <nplanel@gmail.com> Signed-off-by: Nicolas PLANEL <nplanel@cisco.com>
1 parent 6fa7a0e commit f479eeb

File tree

4 files changed

+147
-0
lines changed

4 files changed

+147
-0
lines changed

src/vnet/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -522,6 +522,7 @@ list(APPEND VNET_API_FILES bfd/bfd.api)
522522

523523
list(APPEND VNET_SOURCES
524524
crypto/cli.c
525+
crypto/config.c
525526
crypto/crypto.c
526527
crypto/format.c
527528
crypto/main.c

src/vnet/crypto/config.c

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
/*
2+
* config.c: crypto engines configuration
3+
*
4+
* Copyright (c) 2025 Cisco and/or its affiliates.
5+
* SPDX-License-Identifier: Apache-2.0
6+
* https://spdx.org/licenses/Apache-2.0.html
7+
*/
8+
9+
#include <vlib/vlib.h>
10+
#include <vnet/crypto/crypto.h>
11+
12+
static clib_error_t *
13+
config_one_crypto (vlib_main_t *vm, char *name, unformat_input_t *input)
14+
{
15+
vnet_crypto_main_t *cm = &crypto_main;
16+
vnet_crypto_config_t *pc;
17+
clib_error_t *error = 0;
18+
uword *p;
19+
int is_enable = 0;
20+
int is_disable = 0;
21+
22+
if (cm->config_index_by_name == 0)
23+
cm->config_index_by_name = hash_create_string (0, sizeof (uword));
24+
25+
p = hash_get_mem (cm->config_index_by_name, name);
26+
if (p)
27+
{
28+
error = clib_error_return (0, "crypto '%s' already configured", name);
29+
goto done;
30+
}
31+
32+
while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
33+
{
34+
if (unformat (input, "enable"))
35+
is_enable = 1;
36+
else if (unformat (input, "disable"))
37+
is_disable = 1;
38+
else
39+
{
40+
error = clib_error_return (0, "unknown input '%U'",
41+
format_unformat_error, input);
42+
goto done;
43+
}
44+
}
45+
46+
if (is_enable && is_disable)
47+
{
48+
error = clib_error_return (0,
49+
"please specify either enable or disable"
50+
" for crypto '%s'",
51+
name);
52+
goto done;
53+
}
54+
55+
vec_add2 (cm->configs, pc, 1);
56+
pc->is_enabled = is_enable;
57+
pc->is_disabled = is_disable;
58+
pc->name = vec_dup (name);
59+
hash_set_mem (cm->config_index_by_name, pc->name, pc - cm->configs);
60+
61+
done:
62+
return error;
63+
}
64+
65+
static clib_error_t *
66+
crypto_engines_config (vlib_main_t *vm, unformat_input_t *input)
67+
{
68+
vnet_crypto_main_t *cm = &crypto_main;
69+
clib_error_t *error = 0;
70+
71+
while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
72+
{
73+
unformat_input_t sub_input;
74+
u8 *s = 0;
75+
if (unformat (input, "default %U", unformat_vlib_cli_sub_input,
76+
&sub_input))
77+
{
78+
cm->default_disabled = unformat (&sub_input, "disable") ? 1 : 0;
79+
unformat_free (&sub_input);
80+
}
81+
else if (unformat (input, "%s %U", &s, unformat_vlib_cli_sub_input,
82+
&sub_input))
83+
{
84+
error = config_one_crypto (vm, (char *) s, &sub_input);
85+
vec_free (s);
86+
unformat_free (&sub_input);
87+
if (error)
88+
goto done;
89+
}
90+
else
91+
{
92+
error = clib_error_return (0, "unknown input '%U'",
93+
format_unformat_error, input);
94+
{
95+
vec_free (s);
96+
goto done;
97+
}
98+
}
99+
}
100+
101+
done:
102+
return error;
103+
}
104+
105+
VLIB_EARLY_CONFIG_FUNCTION (crypto_engines_config, "crypto-engines");

src/vnet/crypto/crypto.c

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ VLIB_REGISTER_LOG_CLASS (crypto_main_log, static) = {
1818

1919
#define log_debug(f, ...) \
2020
vlib_log (VLIB_LOG_LEVEL_DEBUG, crypto_main_log.class, f, ##__VA_ARGS__)
21+
#define log_notice(f, ...) \
22+
vlib_log (VLIB_LOG_LEVEL_NOTICE, crypto_main_log.class, f, ##__VA_ARGS__)
2123
#define log_err(f, ...) \
2224
vlib_log (VLIB_LOG_LEVEL_ERR, crypto_main_log.class, f, ##__VA_ARGS__)
2325

@@ -564,11 +566,14 @@ static void
564566
vnet_crypto_load_engines (vlib_main_t *vm)
565567
{
566568
vlib_thread_main_t *tm = vlib_get_thread_main ();
569+
vnet_crypto_main_t *cm = &crypto_main;
570+
vnet_crypto_config_t *pc;
567571
u8 *path;
568572
char *p;
569573
u32 path_len;
570574
struct dirent *entry;
571575
DIR *dp;
576+
uword *config_index;
572577

573578
path = os_get_exec_path ();
574579
log_debug ("exec path is %s", path);
@@ -623,6 +628,31 @@ vnet_crypto_load_engines (vlib_main_t *vm)
623628
continue;
624629
}
625630

631+
/* follow crypto-engines config section directive */
632+
config_index = hash_get_mem (cm->config_index_by_name, r->name);
633+
if (config_index)
634+
{
635+
pc = vec_elt_at_index (cm->configs, config_index[0]);
636+
if (pc->is_disabled)
637+
{
638+
log_notice ("crypto disabled: %s", r->name);
639+
dlclose (handle);
640+
continue;
641+
}
642+
if (cm->default_disabled && pc->is_enabled == 0)
643+
{
644+
log_notice ("crypto disabled (default): %s", r->name);
645+
dlclose (handle);
646+
continue;
647+
}
648+
}
649+
else if (cm->default_disabled)
650+
{
651+
log_notice ("crypto disabled (default): %s", r->name);
652+
dlclose (handle);
653+
continue;
654+
}
655+
626656
if (r->per_thread_data_sz)
627657
{
628658
u64 sz =

src/vnet/crypto/crypto.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,13 @@ typedef struct
418418
void *handlers[VNET_CRYPTO_HANDLER_N_TYPES];
419419
} vnet_crypto_op_data_t;
420420

421+
typedef struct
422+
{
423+
char *name;
424+
u8 is_disabled;
425+
u8 is_enabled;
426+
} vnet_crypto_config_t;
427+
421428
typedef struct
422429
{
423430
vnet_crypto_key_t **keys;
@@ -426,11 +433,15 @@ typedef struct
426433
vnet_crypto_thread_t *threads;
427434
vnet_crypto_frame_dequeue_t **dequeue_handlers;
428435
vnet_crypto_engine_t *engines;
436+
/* configs and hash by name */
437+
vnet_crypto_config_t *configs;
438+
uword *config_index_by_name;
429439
uword *engine_index_by_name;
430440
uword *alg_index_by_name;
431441
vnet_crypto_async_next_node_t *next_nodes;
432442
vnet_crypto_alg_data_t algs[VNET_CRYPTO_N_ALGS];
433443
vnet_crypto_op_data_t opt_data[VNET_CRYPTO_N_OP_IDS];
444+
u8 default_disabled;
434445
} vnet_crypto_main_t;
435446

436447
extern vnet_crypto_main_t crypto_main;

0 commit comments

Comments
 (0)