blob: 3b1505ad4486d5b7ef891b3f816789d413be80ab [file] [log] [blame]
Damjan Marion91f17dc2019-03-18 18:59:25 +01001/*
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:
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
16#include <stdbool.h>
17#include <vlib/vlib.h>
18#include <vnet/crypto/crypto.h>
19
20vnet_crypto_main_t crypto_main;
21
Filip Tehlarefcad1a2020-02-04 09:36:04 +000022static_always_inline void
23crypto_set_op_status (vnet_crypto_op_t * ops[], u32 n_ops, int status)
24{
25 while (n_ops--)
26 {
27 ops[0]->status = status;
28 ops++;
29 }
30}
31
Damjan Marion085637f2019-04-03 18:39:27 +020032static_always_inline u32
33vnet_crypto_process_ops_call_handler (vlib_main_t * vm,
34 vnet_crypto_main_t * cm,
Damjan Marion060bfb92019-03-29 13:47:54 +010035 vnet_crypto_op_id_t opt,
Filip Tehlarefcad1a2020-02-04 09:36:04 +000036 vnet_crypto_op_t * ops[],
37 vnet_crypto_op_chunk_t * chunks,
38 u32 n_ops)
Damjan Marion085637f2019-04-03 18:39:27 +020039{
Filip Tehlarefcad1a2020-02-04 09:36:04 +000040 u32 rv = 0;
Damjan Marion085637f2019-04-03 18:39:27 +020041 if (n_ops == 0)
42 return 0;
43
Filip Tehlarefcad1a2020-02-04 09:36:04 +000044 if (chunks)
Damjan Marion085637f2019-04-03 18:39:27 +020045 {
Damjan Marion085637f2019-04-03 18:39:27 +020046
Filip Tehlarefcad1a2020-02-04 09:36:04 +000047 if (cm->chained_ops_handlers[opt] == 0)
48 crypto_set_op_status (ops, n_ops,
49 VNET_CRYPTO_OP_STATUS_FAIL_NO_HANDLER);
50 else
51 rv = (cm->chained_ops_handlers[opt]) (vm, ops, chunks, n_ops);
52 }
53 else
54 {
55 if (cm->ops_handlers[opt] == 0)
56 crypto_set_op_status (ops, n_ops,
57 VNET_CRYPTO_OP_STATUS_FAIL_NO_HANDLER);
58 else
59 rv = (cm->ops_handlers[opt]) (vm, ops, n_ops);
60 }
61 return rv;
Damjan Marion085637f2019-04-03 18:39:27 +020062}
63
Filip Tehlarefcad1a2020-02-04 09:36:04 +000064static_always_inline u32
65vnet_crypto_process_ops_inline (vlib_main_t * vm, vnet_crypto_op_t ops[],
66 vnet_crypto_op_chunk_t * chunks, u32 n_ops)
Damjan Marion91f17dc2019-03-18 18:59:25 +010067{
68 vnet_crypto_main_t *cm = &crypto_main;
Damjan Marion085637f2019-04-03 18:39:27 +020069 const int op_q_size = VLIB_FRAME_SIZE;
70 vnet_crypto_op_t *op_queue[op_q_size];
Damjan Marion060bfb92019-03-29 13:47:54 +010071 vnet_crypto_op_id_t opt, current_op_type = ~0;
Damjan Marion085637f2019-04-03 18:39:27 +020072 u32 n_op_queue = 0;
Damjan Marion91f17dc2019-03-18 18:59:25 +010073 u32 rv = 0, i;
74
Damjan Marion085637f2019-04-03 18:39:27 +020075 ASSERT (n_ops >= 1);
76
Damjan Marion91f17dc2019-03-18 18:59:25 +010077 for (i = 0; i < n_ops; i++)
78 {
Damjan Marion085637f2019-04-03 18:39:27 +020079 opt = ops[i].op;
Damjan Marion91f17dc2019-03-18 18:59:25 +010080
Damjan Marion085637f2019-04-03 18:39:27 +020081 if (current_op_type != opt || n_op_queue >= op_q_size)
82 {
83 rv += vnet_crypto_process_ops_call_handler (vm, cm, current_op_type,
Filip Tehlarefcad1a2020-02-04 09:36:04 +000084 op_queue, chunks,
85 n_op_queue);
Damjan Marion085637f2019-04-03 18:39:27 +020086 n_op_queue = 0;
87 current_op_type = opt;
88 }
89
90 op_queue[n_op_queue++] = &ops[i];
Damjan Marion91f17dc2019-03-18 18:59:25 +010091 }
92
Damjan Marion085637f2019-04-03 18:39:27 +020093 rv += vnet_crypto_process_ops_call_handler (vm, cm, current_op_type,
Filip Tehlarefcad1a2020-02-04 09:36:04 +000094 op_queue, chunks, n_op_queue);
Damjan Marion91f17dc2019-03-18 18:59:25 +010095 return rv;
96}
97
98u32
Filip Tehlarefcad1a2020-02-04 09:36:04 +000099vnet_crypto_process_ops (vlib_main_t * vm, vnet_crypto_op_t ops[], u32 n_ops)
100{
101 return vnet_crypto_process_ops_inline (vm, ops, 0, n_ops);
102}
103
104u32
105vnet_crypto_process_chained_ops (vlib_main_t * vm, vnet_crypto_op_t ops[],
106 vnet_crypto_op_chunk_t * chunks, u32 n_ops)
107{
108 return vnet_crypto_process_ops_inline (vm, ops, chunks, n_ops);
109}
110
111u32
Damjan Marion91f17dc2019-03-18 18:59:25 +0100112vnet_crypto_register_engine (vlib_main_t * vm, char *name, int prio,
113 char *desc)
114{
115 vnet_crypto_main_t *cm = &crypto_main;
116 vnet_crypto_engine_t *p;
117
118 vec_add2 (cm->engines, p, 1);
119 p->name = name;
120 p->desc = desc;
121 p->priority = prio;
122
Filip Tehlar1469d542019-03-25 09:04:41 -0700123 hash_set_mem (cm->engine_index_by_name, p->name, p - cm->engines);
124
Damjan Marion91f17dc2019-03-18 18:59:25 +0100125 return p - cm->engines;
126}
127
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000128static_always_inline void
129crypto_set_active_engine (vnet_crypto_op_data_t * od,
130 vnet_crypto_op_id_t id, u32 ei,
131 crypto_op_class_type_t oct)
132{
133 vnet_crypto_main_t *cm = &crypto_main;
134 vnet_crypto_engine_t *ce = vec_elt_at_index (cm->engines, ei);
135
136 if (oct == CRYPTO_OP_BOTH || oct == CRYPTO_OP_CHAINED)
137 {
138 if (ce->chained_ops_handlers[id])
139 {
140 od->active_engine_index_chained = ei;
141 cm->chained_ops_handlers[id] = ce->chained_ops_handlers[id];
142 }
143 }
144
145 if (oct == CRYPTO_OP_BOTH || oct == CRYPTO_OP_SIMPLE)
146 {
147 if (ce->ops_handlers[id])
148 {
149 od->active_engine_index_simple = ei;
150 cm->ops_handlers[id] = ce->ops_handlers[id];
151 }
152 }
153}
154
Filip Tehlar1469d542019-03-25 09:04:41 -0700155int
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000156vnet_crypto_set_handler2 (char *alg_name, char *engine,
157 crypto_op_class_type_t oct)
Filip Tehlar1469d542019-03-25 09:04:41 -0700158{
159 uword *p;
160 vnet_crypto_main_t *cm = &crypto_main;
Damjan Marion060bfb92019-03-29 13:47:54 +0100161 vnet_crypto_alg_data_t *ad;
Damjan Marion060bfb92019-03-29 13:47:54 +0100162 int i;
Filip Tehlar1469d542019-03-25 09:04:41 -0700163
Damjan Marion060bfb92019-03-29 13:47:54 +0100164 p = hash_get_mem (cm->alg_index_by_name, alg_name);
Filip Tehlar1469d542019-03-25 09:04:41 -0700165 if (!p)
166 return -1;
167
Damjan Marion060bfb92019-03-29 13:47:54 +0100168 ad = vec_elt_at_index (cm->algs, p[0]);
Filip Tehlar1469d542019-03-25 09:04:41 -0700169
170 p = hash_get_mem (cm->engine_index_by_name, engine);
171 if (!p)
172 return -1;
173
Filip Tehlard26b8602020-02-25 09:53:26 +0000174 for (i = 0; i < VNET_CRYPTO_OP_N_TYPES; i++)
Damjan Marion060bfb92019-03-29 13:47:54 +0100175 {
176 vnet_crypto_op_data_t *od;
177 vnet_crypto_op_id_t id = ad->op_by_type[i];
178 if (id == 0)
179 continue;
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000180
Lijian Zhangb15d7962019-09-27 16:25:35 +0800181 od = cm->opt_data + id;
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000182 crypto_set_active_engine (od, id, p[0], oct);
Damjan Marion060bfb92019-03-29 13:47:54 +0100183 }
Filip Tehlar1469d542019-03-25 09:04:41 -0700184
185 return 0;
186}
187
Neale Rannsece2ae02019-06-21 12:44:11 +0000188int
189vnet_crypto_is_set_handler (vnet_crypto_alg_t alg)
190{
191 vnet_crypto_main_t *cm = &crypto_main;
PiotrX Kleskib2525922021-01-11 08:59:31 +0000192 vnet_crypto_op_id_t opt = 0;
193 int i;
Neale Rannsece2ae02019-06-21 12:44:11 +0000194
PiotrX Kleskib2525922021-01-11 08:59:31 +0000195 if (alg > vec_len (cm->algs))
196 return 0;
197
198 for (i = 0; i < VNET_CRYPTO_OP_N_TYPES; i++)
199 if ((opt = cm->algs[alg].op_by_type[i]) != 0)
200 break;
201
202 return NULL != cm->ops_handlers[opt];
Neale Rannsece2ae02019-06-21 12:44:11 +0000203}
204
Damjan Mariond1bed682019-04-24 15:20:35 +0200205void
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000206vnet_crypto_register_ops_handler_inline (vlib_main_t * vm, u32 engine_index,
207 vnet_crypto_op_id_t opt,
208 vnet_crypto_ops_handler_t * fn,
209 vnet_crypto_chained_ops_handler_t *
210 cfn)
Damjan Marion91f17dc2019-03-18 18:59:25 +0100211{
212 vnet_crypto_main_t *cm = &crypto_main;
213 vnet_crypto_engine_t *ae, *e = vec_elt_at_index (cm->engines, engine_index);
Damjan Marion060bfb92019-03-29 13:47:54 +0100214 vnet_crypto_op_data_t *otd = cm->opt_data + opt;
215 vec_validate_aligned (cm->ops_handlers, VNET_CRYPTO_N_OP_IDS - 1,
Damjan Marion91f17dc2019-03-18 18:59:25 +0100216 CLIB_CACHE_LINE_BYTES);
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000217 vec_validate_aligned (cm->chained_ops_handlers, VNET_CRYPTO_N_OP_IDS - 1,
218 CLIB_CACHE_LINE_BYTES);
Damjan Marion91f17dc2019-03-18 18:59:25 +0100219
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000220 if (fn)
Damjan Marion91f17dc2019-03-18 18:59:25 +0100221 {
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000222 e->ops_handlers[opt] = fn;
223 if (otd->active_engine_index_simple == ~0)
224 {
225 otd->active_engine_index_simple = engine_index;
226 cm->ops_handlers[opt] = fn;
227 }
228
229 ae = vec_elt_at_index (cm->engines, otd->active_engine_index_simple);
230 if (ae->priority < e->priority)
231 crypto_set_active_engine (otd, opt, engine_index, CRYPTO_OP_SIMPLE);
Damjan Marion91f17dc2019-03-18 18:59:25 +0100232 }
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000233
234 if (cfn)
Damjan Marion91f17dc2019-03-18 18:59:25 +0100235 {
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000236 e->chained_ops_handlers[opt] = cfn;
237 if (otd->active_engine_index_chained == ~0)
238 {
239 otd->active_engine_index_chained = engine_index;
240 cm->chained_ops_handlers[opt] = cfn;
241 }
242
243 ae = vec_elt_at_index (cm->engines, otd->active_engine_index_chained);
244 if (ae->priority < e->priority)
245 crypto_set_active_engine (otd, opt, engine_index, CRYPTO_OP_CHAINED);
Damjan Marion91f17dc2019-03-18 18:59:25 +0100246 }
247
Damjan Mariond1bed682019-04-24 15:20:35 +0200248 return;
249}
250
251void
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000252vnet_crypto_register_ops_handler (vlib_main_t * vm, u32 engine_index,
253 vnet_crypto_op_id_t opt,
254 vnet_crypto_ops_handler_t * fn)
255{
256 vnet_crypto_register_ops_handler_inline (vm, engine_index, opt, fn, 0);
257}
258
259void
260vnet_crypto_register_chained_ops_handler (vlib_main_t * vm, u32 engine_index,
261 vnet_crypto_op_id_t opt,
262 vnet_crypto_chained_ops_handler_t *
263 fn)
264{
265 vnet_crypto_register_ops_handler_inline (vm, engine_index, opt, 0, fn);
266}
267
268void
269vnet_crypto_register_ops_handlers (vlib_main_t * vm, u32 engine_index,
270 vnet_crypto_op_id_t opt,
271 vnet_crypto_ops_handler_t * fn,
272 vnet_crypto_chained_ops_handler_t * cfn)
273{
274 vnet_crypto_register_ops_handler_inline (vm, engine_index, opt, fn, cfn);
275}
276
277void
Fan Zhangf5395782020-04-29 14:00:03 +0100278vnet_crypto_register_async_handler (vlib_main_t * vm, u32 engine_index,
279 vnet_crypto_async_op_id_t opt,
280 vnet_crypto_frame_enqueue_t * enqueue_hdl,
281 vnet_crypto_frame_dequeue_t * dequeue_hdl)
282{
283 vnet_crypto_main_t *cm = &crypto_main;
284 vnet_crypto_engine_t *ae, *e = vec_elt_at_index (cm->engines, engine_index);
285 vnet_crypto_async_op_data_t *otd = cm->async_opt_data + opt;
Alexander Chernavin005d1e42021-02-01 05:17:24 -0500286 vec_validate_aligned (cm->enqueue_handlers, VNET_CRYPTO_ASYNC_OP_N_IDS,
Fan Zhangf5395782020-04-29 14:00:03 +0100287 CLIB_CACHE_LINE_BYTES);
Alexander Chernavin005d1e42021-02-01 05:17:24 -0500288 vec_validate_aligned (cm->dequeue_handlers, VNET_CRYPTO_ASYNC_OP_N_IDS,
Fan Zhangf5395782020-04-29 14:00:03 +0100289 CLIB_CACHE_LINE_BYTES);
290
291 /* both enqueue hdl and dequeue hdl should present */
292 if (!enqueue_hdl && !dequeue_hdl)
293 return;
294
295 e->enqueue_handlers[opt] = enqueue_hdl;
296 e->dequeue_handlers[opt] = dequeue_hdl;
297 if (otd->active_engine_index_async == ~0)
298 {
299 otd->active_engine_index_async = engine_index;
300 cm->enqueue_handlers[opt] = enqueue_hdl;
301 cm->dequeue_handlers[opt] = dequeue_hdl;
302 }
303
304 ae = vec_elt_at_index (cm->engines, otd->active_engine_index_async);
Fan Zhange4db9452021-03-30 17:31:38 +0100305 if (ae->priority <= e->priority)
Fan Zhangf5395782020-04-29 14:00:03 +0100306 {
307 otd->active_engine_index_async = engine_index;
308 cm->enqueue_handlers[opt] = enqueue_hdl;
309 cm->dequeue_handlers[opt] = dequeue_hdl;
310 }
311
312 return;
313}
314
315void
Damjan Mariond1bed682019-04-24 15:20:35 +0200316vnet_crypto_register_key_handler (vlib_main_t * vm, u32 engine_index,
317 vnet_crypto_key_handler_t * key_handler)
318{
319 vnet_crypto_main_t *cm = &crypto_main;
320 vnet_crypto_engine_t *e = vec_elt_at_index (cm->engines, engine_index);
321 e->key_op_handler = key_handler;
322 return;
323}
324
Benoît Gannebe954442019-04-29 16:05:46 +0200325static int
326vnet_crypto_key_len_check (vnet_crypto_alg_t alg, u16 length)
327{
328 switch (alg)
329 {
330 case VNET_CRYPTO_N_ALGS:
331 return 0;
332 case VNET_CRYPTO_ALG_NONE:
333 return 1;
334
335#define _(n, s, l) \
336 case VNET_CRYPTO_ALG_##n: \
337 if ((l) == length) \
Neale Rannse6be7022019-06-04 15:37:34 +0000338 return 1; \
339 break;
Benoît Gannebe954442019-04-29 16:05:46 +0200340 foreach_crypto_cipher_alg foreach_crypto_aead_alg
341#undef _
342 /* HMAC allows any key length */
343#define _(n, s) \
344 case VNET_CRYPTO_ALG_HMAC_##n: \
345 return 1;
Filip Tehlar06111a82021-05-03 15:29:56 +0000346 foreach_crypto_hmac_alg
347#undef _
348
349#define _(n, s) \
350 case VNET_CRYPTO_ALG_HASH_##n: \
351 return 1;
352 foreach_crypto_hash_alg
Benoît Gannebe954442019-04-29 16:05:46 +0200353#undef _
354 }
355
356 return 0;
357}
358
Damjan Mariond1bed682019-04-24 15:20:35 +0200359u32
360vnet_crypto_key_add (vlib_main_t * vm, vnet_crypto_alg_t alg, u8 * data,
361 u16 length)
362{
363 u32 index;
364 vnet_crypto_main_t *cm = &crypto_main;
365 vnet_crypto_engine_t *engine;
366 vnet_crypto_key_t *key;
Benoît Gannebe954442019-04-29 16:05:46 +0200367
Benoît Gannebe954442019-04-29 16:05:46 +0200368 if (!vnet_crypto_key_len_check (alg, length))
369 return ~0;
370
Damjan Mariond1bed682019-04-24 15:20:35 +0200371 pool_get_zero (cm->keys, key);
372 index = key - cm->keys;
Fan Zhangf5395782020-04-29 14:00:03 +0100373 key->type = VNET_CRYPTO_KEY_TYPE_DATA;
Damjan Mariond1bed682019-04-24 15:20:35 +0200374 key->alg = alg;
375 vec_validate_aligned (key->data, length - 1, CLIB_CACHE_LINE_BYTES);
376 clib_memcpy (key->data, data, length);
Damjan Mariond1bed682019-04-24 15:20:35 +0200377 /* *INDENT-OFF* */
378 vec_foreach (engine, cm->engines)
379 if (engine->key_op_handler)
380 engine->key_op_handler (vm, VNET_CRYPTO_KEY_OP_ADD, index);
381 /* *INDENT-ON* */
382 return index;
383}
384
385void
386vnet_crypto_key_del (vlib_main_t * vm, vnet_crypto_key_index_t index)
387{
388 vnet_crypto_main_t *cm = &crypto_main;
389 vnet_crypto_engine_t *engine;
390 vnet_crypto_key_t *key = pool_elt_at_index (cm->keys, index);
391
392 /* *INDENT-OFF* */
393 vec_foreach (engine, cm->engines)
394 if (engine->key_op_handler)
395 engine->key_op_handler (vm, VNET_CRYPTO_KEY_OP_DEL, index);
396 /* *INDENT-ON* */
397
Fan Zhangf5395782020-04-29 14:00:03 +0100398 if (key->type == VNET_CRYPTO_KEY_TYPE_DATA)
399 {
400 clib_memset (key->data, 0, vec_len (key->data));
401 vec_free (key->data);
402 }
403 else if (key->type == VNET_CRYPTO_KEY_TYPE_LINK)
404 {
405 key->index_crypto = key->index_integ = 0;
406 }
407
Damjan Mariond1bed682019-04-24 15:20:35 +0200408 pool_put (cm->keys, key);
409}
410
Fan Zhangf5395782020-04-29 14:00:03 +0100411vnet_crypto_async_alg_t
412vnet_crypto_link_algs (vnet_crypto_alg_t crypto_alg,
413 vnet_crypto_alg_t integ_alg)
414{
415#define _(c, h, s, k ,d) \
416 if (crypto_alg == VNET_CRYPTO_ALG_##c && \
417 integ_alg == VNET_CRYPTO_ALG_HMAC_##h) \
418 return VNET_CRYPTO_ALG_##c##_##h##_TAG##d;
419 foreach_crypto_link_async_alg
420#undef _
421 return ~0;
422}
423
424u32
425vnet_crypto_key_add_linked (vlib_main_t * vm,
426 vnet_crypto_key_index_t index_crypto,
427 vnet_crypto_key_index_t index_integ)
428{
429 u32 index;
430 vnet_crypto_main_t *cm = &crypto_main;
431 vnet_crypto_engine_t *engine;
432 vnet_crypto_key_t *key_crypto, *key_integ, *key;
433 vnet_crypto_async_alg_t linked_alg;
434
435 key_crypto = pool_elt_at_index (cm->keys, index_crypto);
436 key_integ = pool_elt_at_index (cm->keys, index_integ);
437
Fan Zhangf5395782020-04-29 14:00:03 +0100438 linked_alg = vnet_crypto_link_algs (key_crypto->alg, key_integ->alg);
439 if (linked_alg == ~0)
440 return ~0;
441
442 pool_get_zero (cm->keys, key);
443 index = key - cm->keys;
444 key->type = VNET_CRYPTO_KEY_TYPE_LINK;
445 key->index_crypto = index_crypto;
446 key->index_integ = index_integ;
447 key->async_alg = linked_alg;
448
449 /* *INDENT-OFF* */
450 vec_foreach (engine, cm->engines)
451 if (engine->key_op_handler)
452 engine->key_op_handler (vm, VNET_CRYPTO_KEY_OP_ADD, index);
453 /* *INDENT-ON* */
454
455 return index;
456}
457
458clib_error_t *
459crypto_dispatch_enable_disable (int is_enable)
460{
Fan Zhangf5395782020-04-29 14:00:03 +0100461 vnet_crypto_main_t *cm = &crypto_main;
PiotrX Kleski22848172020-07-08 14:36:34 +0200462 vlib_thread_main_t *tm = vlib_get_thread_main ();
Fan Zhangf5395782020-04-29 14:00:03 +0100463 u32 skip_master = vlib_num_workers () > 0, i;
PiotrX Kleski22848172020-07-08 14:36:34 +0200464 vlib_node_state_t state = VLIB_NODE_STATE_DISABLED;
465 u8 state_change = 0;
Fan Zhangf5395782020-04-29 14:00:03 +0100466
PiotrX Kleski22848172020-07-08 14:36:34 +0200467 CLIB_MEMORY_STORE_BARRIER ();
Fan Zhangf5395782020-04-29 14:00:03 +0100468 if (is_enable && cm->async_refcnt > 0)
469 {
470 state_change = 1;
PiotrX Kleski22848172020-07-08 14:36:34 +0200471 state =
472 cm->dispatch_mode ==
473 VNET_CRYPTO_ASYNC_DISPATCH_POLLING ? VLIB_NODE_STATE_POLLING :
474 VLIB_NODE_STATE_INTERRUPT;
Fan Zhangf5395782020-04-29 14:00:03 +0100475 }
476
477 if (!is_enable && cm->async_refcnt == 0)
478 {
479 state_change = 1;
480 state = VLIB_NODE_STATE_DISABLED;
481 }
482
483 if (state_change)
484 for (i = skip_master; i < tm->n_vlib_mains; i++)
PiotrX Kleski22848172020-07-08 14:36:34 +0200485 {
Damjan Marion6ffb7c62021-03-26 13:06:13 +0100486 vlib_main_t *ovm = vlib_get_main_by_index (i);
487 if (state != vlib_node_get_state (ovm, cm->crypto_node_index))
488 vlib_node_set_state (ovm, cm->crypto_node_index, state);
PiotrX Kleski22848172020-07-08 14:36:34 +0200489 }
Fan Zhangf5395782020-04-29 14:00:03 +0100490 return 0;
491}
492
493static_always_inline void
494crypto_set_active_async_engine (vnet_crypto_async_op_data_t * od,
495 vnet_crypto_async_op_id_t id, u32 ei)
496{
497 vnet_crypto_main_t *cm = &crypto_main;
498 vnet_crypto_engine_t *ce = vec_elt_at_index (cm->engines, ei);
499
500 if (ce->enqueue_handlers[id] && ce->dequeue_handlers[id])
501 {
502 od->active_engine_index_async = ei;
503 cm->enqueue_handlers[id] = ce->enqueue_handlers[id];
504 cm->dequeue_handlers[id] = ce->dequeue_handlers[id];
505 }
506}
507
508int
509vnet_crypto_set_async_handler2 (char *alg_name, char *engine)
510{
511 uword *p;
512 vnet_crypto_main_t *cm = &crypto_main;
513 vnet_crypto_async_alg_data_t *ad;
514 int i;
515
516 p = hash_get_mem (cm->async_alg_index_by_name, alg_name);
517 if (!p)
518 return -1;
519
520 ad = vec_elt_at_index (cm->async_algs, p[0]);
521
522 p = hash_get_mem (cm->engine_index_by_name, engine);
523 if (!p)
524 return -1;
525
526 for (i = 0; i < VNET_CRYPTO_ASYNC_OP_N_TYPES; i++)
527 {
528 vnet_crypto_async_op_data_t *od;
529 vnet_crypto_async_op_id_t id = ad->op_by_type[i];
530 if (id == 0)
531 continue;
532
533 od = cm->async_opt_data + id;
534 crypto_set_active_async_engine (od, id, p[0]);
535 }
536
537 return 0;
538}
539
540u32
541vnet_crypto_register_post_node (vlib_main_t * vm, char *post_node_name)
542{
543 vnet_crypto_main_t *cm = &crypto_main;
544 vnet_crypto_async_next_node_t *nn = 0;
545 vlib_node_t *cc, *pn;
546 uword index = vec_len (cm->next_nodes);
547
548 pn = vlib_get_node_by_name (vm, (u8 *) post_node_name);
549 if (!pn)
550 return ~0;
551
552 /* *INDENT-OFF* */
553 vec_foreach (cm->next_nodes, nn)
554 {
555 if (nn->node_idx == pn->index)
556 return nn->next_idx;
557 }
558 /* *INDENT-ON* */
559
560 vec_validate (cm->next_nodes, index);
561 nn = vec_elt_at_index (cm->next_nodes, index);
562
563 cc = vlib_get_node_by_name (vm, (u8 *) "crypto-dispatch");
564 nn->next_idx = vlib_node_add_named_next (vm, cc->index, post_node_name);
565 nn->node_idx = pn->index;
566
567 return nn->next_idx;
568}
569
570void
571vnet_crypto_request_async_mode (int is_enable)
572{
Fan Zhangf5395782020-04-29 14:00:03 +0100573 vnet_crypto_main_t *cm = &crypto_main;
PiotrX Kleski22848172020-07-08 14:36:34 +0200574 vlib_thread_main_t *tm = vlib_get_thread_main ();
Fan Zhangf5395782020-04-29 14:00:03 +0100575 u32 skip_master = vlib_num_workers () > 0, i;
PiotrX Kleski22848172020-07-08 14:36:34 +0200576 vlib_node_state_t state = VLIB_NODE_STATE_DISABLED;
577 u8 state_change = 0;
Fan Zhangf5395782020-04-29 14:00:03 +0100578
PiotrX Kleski22848172020-07-08 14:36:34 +0200579 CLIB_MEMORY_STORE_BARRIER ();
Fan Zhangf5395782020-04-29 14:00:03 +0100580 if (is_enable && cm->async_refcnt == 0)
581 {
582 state_change = 1;
PiotrX Kleski22848172020-07-08 14:36:34 +0200583 state =
584 cm->dispatch_mode == VNET_CRYPTO_ASYNC_DISPATCH_POLLING ?
585 VLIB_NODE_STATE_POLLING : VLIB_NODE_STATE_INTERRUPT;
Fan Zhangf5395782020-04-29 14:00:03 +0100586 }
Fan Zhangf5395782020-04-29 14:00:03 +0100587 if (!is_enable && cm->async_refcnt == 1)
588 {
589 state_change = 1;
590 state = VLIB_NODE_STATE_DISABLED;
591 }
592
593 if (state_change)
594 for (i = skip_master; i < tm->n_vlib_mains; i++)
PiotrX Kleski22848172020-07-08 14:36:34 +0200595 {
Damjan Marion6ffb7c62021-03-26 13:06:13 +0100596 vlib_main_t *ovm = vlib_get_main_by_index (i);
597 if (state != vlib_node_get_state (ovm, cm->crypto_node_index))
598 vlib_node_set_state (ovm, cm->crypto_node_index, state);
PiotrX Kleski22848172020-07-08 14:36:34 +0200599 }
Fan Zhangf5395782020-04-29 14:00:03 +0100600
601 if (is_enable)
602 cm->async_refcnt += 1;
603 else if (cm->async_refcnt > 0)
604 cm->async_refcnt -= 1;
605}
606
PiotrX Kleski22848172020-07-08 14:36:34 +0200607void
608vnet_crypto_set_async_dispatch_mode (u8 mode)
609{
610 vnet_crypto_main_t *cm = &crypto_main;
611 u32 skip_master = vlib_num_workers () > 0, i;
612 vlib_thread_main_t *tm = vlib_get_thread_main ();
613 vlib_node_state_t state = VLIB_NODE_STATE_DISABLED;
614
615 CLIB_MEMORY_STORE_BARRIER ();
616 cm->dispatch_mode = mode;
617 if (mode == VNET_CRYPTO_ASYNC_DISPATCH_INTERRUPT)
618 {
619 state =
620 cm->async_refcnt == 0 ?
621 VLIB_NODE_STATE_DISABLED : VLIB_NODE_STATE_INTERRUPT;
622 }
623 else if (mode == VNET_CRYPTO_ASYNC_DISPATCH_POLLING)
624 {
625 state =
626 cm->async_refcnt == 0 ?
627 VLIB_NODE_STATE_DISABLED : VLIB_NODE_STATE_POLLING;
628 }
629
630 for (i = skip_master; i < tm->n_vlib_mains; i++)
631 {
Damjan Marion6ffb7c62021-03-26 13:06:13 +0100632 vlib_main_t *ovm = vlib_get_main_by_index (i);
633 if (state != vlib_node_get_state (ovm, cm->crypto_node_index))
634 vlib_node_set_state (ovm, cm->crypto_node_index, state);
PiotrX Kleski22848172020-07-08 14:36:34 +0200635 }
PiotrX Kleski22848172020-07-08 14:36:34 +0200636}
637
Fan Zhangf5395782020-04-29 14:00:03 +0100638int
639vnet_crypto_is_set_async_handler (vnet_crypto_async_op_id_t op)
640{
641 vnet_crypto_main_t *cm = &crypto_main;
642
643 return (op < vec_len (cm->enqueue_handlers) &&
644 NULL != cm->enqueue_handlers[op]);
645}
646
Damjan Marion060bfb92019-03-29 13:47:54 +0100647static void
648vnet_crypto_init_cipher_data (vnet_crypto_alg_t alg, vnet_crypto_op_id_t eid,
649 vnet_crypto_op_id_t did, char *name, u8 is_aead)
650{
651 vnet_crypto_op_type_t eopt, dopt;
652 vnet_crypto_main_t *cm = &crypto_main;
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000653
Damjan Marion060bfb92019-03-29 13:47:54 +0100654 cm->algs[alg].name = name;
655 cm->opt_data[eid].alg = cm->opt_data[did].alg = alg;
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000656 cm->opt_data[eid].active_engine_index_simple = ~0;
657 cm->opt_data[did].active_engine_index_simple = ~0;
658 cm->opt_data[eid].active_engine_index_chained = ~0;
659 cm->opt_data[did].active_engine_index_chained = ~0;
Damjan Marion060bfb92019-03-29 13:47:54 +0100660 if (is_aead)
661 {
662 eopt = VNET_CRYPTO_OP_TYPE_AEAD_ENCRYPT;
663 dopt = VNET_CRYPTO_OP_TYPE_AEAD_DECRYPT;
664 }
665 else
666 {
667 eopt = VNET_CRYPTO_OP_TYPE_ENCRYPT;
668 dopt = VNET_CRYPTO_OP_TYPE_DECRYPT;
669 }
670 cm->opt_data[eid].type = eopt;
671 cm->opt_data[did].type = dopt;
672 cm->algs[alg].op_by_type[eopt] = eid;
673 cm->algs[alg].op_by_type[dopt] = did;
674 hash_set_mem (cm->alg_index_by_name, name, alg);
675}
676
677static void
Filip Tehlar06111a82021-05-03 15:29:56 +0000678vnet_crypto_init_hash_data (vnet_crypto_alg_t alg, vnet_crypto_op_id_t id,
679 char *name)
680{
681 vnet_crypto_main_t *cm = &crypto_main;
682 cm->algs[alg].name = name;
683 cm->algs[alg].op_by_type[VNET_CRYPTO_OP_TYPE_HASH] = id;
684 cm->opt_data[id].alg = alg;
685 cm->opt_data[id].active_engine_index_simple = ~0;
686 cm->opt_data[id].active_engine_index_chained = ~0;
687 cm->opt_data[id].type = VNET_CRYPTO_OP_TYPE_HASH;
688 hash_set_mem (cm->alg_index_by_name, name, alg);
689}
690
691static void
Damjan Marion060bfb92019-03-29 13:47:54 +0100692vnet_crypto_init_hmac_data (vnet_crypto_alg_t alg,
693 vnet_crypto_op_id_t id, char *name)
694{
695 vnet_crypto_main_t *cm = &crypto_main;
696 cm->algs[alg].name = name;
697 cm->algs[alg].op_by_type[VNET_CRYPTO_OP_TYPE_HMAC] = id;
698 cm->opt_data[id].alg = alg;
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000699 cm->opt_data[id].active_engine_index_simple = ~0;
700 cm->opt_data[id].active_engine_index_chained = ~0;
Damjan Marion060bfb92019-03-29 13:47:54 +0100701 cm->opt_data[id].type = VNET_CRYPTO_OP_TYPE_HMAC;
702 hash_set_mem (cm->alg_index_by_name, name, alg);
703}
704
Fan Zhangf5395782020-04-29 14:00:03 +0100705static void
706vnet_crypto_init_async_data (vnet_crypto_async_alg_t alg,
707 vnet_crypto_async_op_id_t eid,
708 vnet_crypto_async_op_id_t did, char *name)
709{
710 vnet_crypto_main_t *cm = &crypto_main;
711
712 cm->async_algs[alg].name = name;
713 cm->async_algs[alg].op_by_type[VNET_CRYPTO_ASYNC_OP_TYPE_ENCRYPT] = eid;
714 cm->async_algs[alg].op_by_type[VNET_CRYPTO_ASYNC_OP_TYPE_DECRYPT] = did;
715 cm->async_opt_data[eid].type = VNET_CRYPTO_ASYNC_OP_TYPE_ENCRYPT;
716 cm->async_opt_data[eid].alg = alg;
717 cm->async_opt_data[eid].active_engine_index_async = ~0;
718 cm->async_opt_data[eid].active_engine_index_async = ~0;
719 cm->async_opt_data[did].type = VNET_CRYPTO_ASYNC_OP_TYPE_DECRYPT;
720 cm->async_opt_data[did].alg = alg;
721 cm->async_opt_data[did].active_engine_index_async = ~0;
722 cm->async_opt_data[did].active_engine_index_async = ~0;
723 hash_set_mem (cm->async_alg_index_by_name, name, alg);
724}
725
Damjan Marion91f17dc2019-03-18 18:59:25 +0100726clib_error_t *
727vnet_crypto_init (vlib_main_t * vm)
728{
729 vnet_crypto_main_t *cm = &crypto_main;
730 vlib_thread_main_t *tm = vlib_get_thread_main ();
Fan Zhangf5395782020-04-29 14:00:03 +0100731 vnet_crypto_thread_t *ct = 0;
PiotrX Kleski22848172020-07-08 14:36:34 +0200732
733 cm->dispatch_mode = VNET_CRYPTO_ASYNC_DISPATCH_POLLING;
Filip Tehlar1469d542019-03-25 09:04:41 -0700734 cm->engine_index_by_name = hash_create_string ( /* size */ 0,
735 sizeof (uword));
Damjan Marion060bfb92019-03-29 13:47:54 +0100736 cm->alg_index_by_name = hash_create_string (0, sizeof (uword));
Fan Zhangf5395782020-04-29 14:00:03 +0100737 cm->async_alg_index_by_name = hash_create_string (0, sizeof (uword));
Damjan Marion91f17dc2019-03-18 18:59:25 +0100738 vec_validate_aligned (cm->threads, tm->n_vlib_mains, CLIB_CACHE_LINE_BYTES);
Fan Zhangf5395782020-04-29 14:00:03 +0100739 vec_foreach (ct, cm->threads)
Fan Zhange4db9452021-03-30 17:31:38 +0100740 pool_alloc_aligned (ct->frame_pool, VNET_CRYPTO_FRAME_POOL_SIZE,
741 CLIB_CACHE_LINE_BYTES);
Damjan Marion91f17dc2019-03-18 18:59:25 +0100742 vec_validate (cm->algs, VNET_CRYPTO_N_ALGS);
Fan Zhangf5395782020-04-29 14:00:03 +0100743 vec_validate (cm->async_algs, VNET_CRYPTO_N_ASYNC_ALGS);
Alexander Chernavin005d1e42021-02-01 05:17:24 -0500744 clib_bitmap_validate (cm->async_active_ids, VNET_CRYPTO_ASYNC_OP_N_IDS);
Fan Zhangf5395782020-04-29 14:00:03 +0100745
Benoît Gannebe954442019-04-29 16:05:46 +0200746#define _(n, s, l) \
Damjan Marion060bfb92019-03-29 13:47:54 +0100747 vnet_crypto_init_cipher_data (VNET_CRYPTO_ALG_##n, \
748 VNET_CRYPTO_OP_##n##_ENC, \
749 VNET_CRYPTO_OP_##n##_DEC, s, 0);
750 foreach_crypto_cipher_alg;
Damjan Marion91f17dc2019-03-18 18:59:25 +0100751#undef _
Benoît Gannebe954442019-04-29 16:05:46 +0200752#define _(n, s, l) \
Damjan Marion060bfb92019-03-29 13:47:54 +0100753 vnet_crypto_init_cipher_data (VNET_CRYPTO_ALG_##n, \
754 VNET_CRYPTO_OP_##n##_ENC, \
755 VNET_CRYPTO_OP_##n##_DEC, s, 1);
756 foreach_crypto_aead_alg;
Damjan Marion91f17dc2019-03-18 18:59:25 +0100757#undef _
Damjan Marion060bfb92019-03-29 13:47:54 +0100758#define _(n, s) \
759 vnet_crypto_init_hmac_data (VNET_CRYPTO_ALG_HMAC_##n, \
760 VNET_CRYPTO_OP_##n##_HMAC, "hmac-" s);
761 foreach_crypto_hmac_alg;
762#undef _
Filip Tehlar06111a82021-05-03 15:29:56 +0000763#define _(n, s) \
764 vnet_crypto_init_hash_data (VNET_CRYPTO_ALG_HASH_##n, \
765 VNET_CRYPTO_OP_##n##_HASH, s);
766 foreach_crypto_hash_alg;
767#undef _
Fan Zhangf5395782020-04-29 14:00:03 +0100768#define _(n, s, k, t, a) \
769 vnet_crypto_init_async_data (VNET_CRYPTO_ALG_##n##_TAG##t##_AAD##a, \
770 VNET_CRYPTO_OP_##n##_TAG##t##_AAD##a##_ENC, \
771 VNET_CRYPTO_OP_##n##_TAG##t##_AAD##a##_DEC, \
772 s);
773 foreach_crypto_aead_async_alg
774#undef _
775#define _(c, h, s, k ,d) \
776 vnet_crypto_init_async_data (VNET_CRYPTO_ALG_##c##_##h##_TAG##d, \
777 VNET_CRYPTO_OP_##c##_##h##_TAG##d##_ENC, \
778 VNET_CRYPTO_OP_##c##_##h##_TAG##d##_DEC, \
779 s);
780 foreach_crypto_link_async_alg
781#undef _
PiotrX Kleski22848172020-07-08 14:36:34 +0200782 cm->crypto_node_index =
783 vlib_get_node_by_name (vm, (u8 *) "crypto-dispatch")->index;
784
785 return 0;
Damjan Marion91f17dc2019-03-18 18:59:25 +0100786}
787
788VLIB_INIT_FUNCTION (vnet_crypto_init);
789
790/*
791 * fd.io coding-style-patch-verification: ON
792 *
793 * Local Variables:
794 * eval: (c-set-style "gnu")
795 * End:
796 */