2 * Copyright (c) 2018 Cisco 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.
17 #include <vlib/vlib.h>
18 #include <vnet/crypto/crypto.h>
20 vnet_crypto_main_t crypto_main;
23 vnet_crypto_process_ops (vlib_main_t * vm, vnet_crypto_op_t ops[], u32 n_ops)
25 vnet_crypto_main_t *cm = &crypto_main;
28 for (i = 0; i < n_ops; i++)
30 vnet_crypto_op_type_t opt = ops[i].op;
31 vnet_crypto_op_t *opp = &ops[i];
33 if (cm->ops_handlers[opt])
34 rv += (cm->ops_handlers[opt]) (vm, &opp, 1);
36 ops[i].status = VNET_CRYPTO_OP_STATUS_FAIL_NO_HANDLER;
43 vnet_crypto_register_engine (vlib_main_t * vm, char *name, int prio,
46 vnet_crypto_main_t *cm = &crypto_main;
47 vnet_crypto_engine_t *p;
49 vec_add2 (cm->engines, p, 1);
54 return p - cm->engines;
58 vnet_crypto_register_ops_handler (vlib_main_t * vm, u32 engine_index,
59 vnet_crypto_op_type_t opt,
60 vnet_crypto_ops_handler_t * fn)
62 vnet_crypto_main_t *cm = &crypto_main;
63 vnet_crypto_engine_t *ae, *e = vec_elt_at_index (cm->engines, engine_index);
64 vnet_crypto_op_type_data_t *otd = cm->opt_data + opt;
65 vec_validate_aligned (cm->ops_handlers, VNET_CRYPTO_N_OP_TYPES - 1,
66 CLIB_CACHE_LINE_BYTES);
67 e->ops_handlers[opt] = fn;
69 if (otd->active_engine_index == ~0)
71 otd->active_engine_index = engine_index;
72 cm->ops_handlers[opt] = fn;
75 ae = vec_elt_at_index (cm->engines, otd->active_engine_index);
76 if (ae->priority < e->priority)
78 otd->active_engine_index = engine_index;
79 cm->ops_handlers[opt] = fn;
86 vnet_crypto_init (vlib_main_t * vm)
88 vnet_crypto_main_t *cm = &crypto_main;
89 vlib_thread_main_t *tm = vlib_get_thread_main ();
90 const char *enc = "encrypt";
91 const char *dec = "decrypt";
92 const char *hmac = "hmac";
94 vec_validate_aligned (cm->threads, tm->n_vlib_mains, CLIB_CACHE_LINE_BYTES);
95 vec_validate (cm->algs, VNET_CRYPTO_N_ALGS);
98 cm->algs[VNET_CRYPTO_ALG_##n].name = s; \
99 cm->opt_data[VNET_CRYPTO_OP_##n##_ENC].alg = VNET_CRYPTO_ALG_##n; \
100 cm->opt_data[VNET_CRYPTO_OP_##n##_DEC].alg = VNET_CRYPTO_ALG_##n; \
101 cm->opt_data[VNET_CRYPTO_OP_##n##_ENC].desc = enc; \
102 cm->opt_data[VNET_CRYPTO_OP_##n##_DEC].desc = dec; \
103 cm->opt_data[VNET_CRYPTO_OP_##n##_ENC].active_engine_index = ~0; \
104 cm->opt_data[VNET_CRYPTO_OP_##n##_DEC].active_engine_index = ~0;
109 cm->algs[VNET_CRYPTO_ALG_##n].name = s; \
110 cm->opt_data[VNET_CRYPTO_OP_##n##_HMAC].alg = VNET_CRYPTO_ALG_##n; \
111 cm->opt_data[VNET_CRYPTO_OP_##n##_HMAC].desc = hmac; \
112 cm->opt_data[VNET_CRYPTO_OP_##n##_HMAC].active_engine_index = ~0;
119 VLIB_INIT_FUNCTION (vnet_crypto_init);
122 * fd.io coding-style-patch-verification: ON
125 * eval: (c-set-style "gnu")