Damjan Marion | 91f17dc | 2019-03-18 18:59:25 +0100 | [diff] [blame^] | 1 | /* |
| 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 | |
| 20 | vnet_crypto_main_t crypto_main; |
| 21 | |
| 22 | u32 |
| 23 | vnet_crypto_process_ops (vlib_main_t * vm, vnet_crypto_op_t ops[], u32 n_ops) |
| 24 | { |
| 25 | vnet_crypto_main_t *cm = &crypto_main; |
| 26 | u32 rv = 0, i; |
| 27 | |
| 28 | for (i = 0; i < n_ops; i++) |
| 29 | { |
| 30 | vnet_crypto_op_type_t opt = ops[i].op; |
| 31 | vnet_crypto_op_t *opp = &ops[i]; |
| 32 | |
| 33 | if (cm->ops_handlers[opt]) |
| 34 | rv += (cm->ops_handlers[opt]) (vm, &opp, 1); |
| 35 | else |
| 36 | ops[i].status = VNET_CRYPTO_OP_STATUS_FAIL_NO_HANDLER; |
| 37 | } |
| 38 | |
| 39 | return rv; |
| 40 | } |
| 41 | |
| 42 | u32 |
| 43 | vnet_crypto_register_engine (vlib_main_t * vm, char *name, int prio, |
| 44 | char *desc) |
| 45 | { |
| 46 | vnet_crypto_main_t *cm = &crypto_main; |
| 47 | vnet_crypto_engine_t *p; |
| 48 | |
| 49 | vec_add2 (cm->engines, p, 1); |
| 50 | p->name = name; |
| 51 | p->desc = desc; |
| 52 | p->priority = prio; |
| 53 | |
| 54 | return p - cm->engines; |
| 55 | } |
| 56 | |
| 57 | vlib_error_t * |
| 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) |
| 61 | { |
| 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; |
| 68 | |
| 69 | if (otd->active_engine_index == ~0) |
| 70 | { |
| 71 | otd->active_engine_index = engine_index; |
| 72 | cm->ops_handlers[opt] = fn; |
| 73 | return 0; |
| 74 | } |
| 75 | ae = vec_elt_at_index (cm->engines, otd->active_engine_index); |
| 76 | if (ae->priority < e->priority) |
| 77 | { |
| 78 | otd->active_engine_index = engine_index; |
| 79 | cm->ops_handlers[opt] = fn; |
| 80 | } |
| 81 | |
| 82 | return 0; |
| 83 | } |
| 84 | |
| 85 | clib_error_t * |
| 86 | vnet_crypto_init (vlib_main_t * vm) |
| 87 | { |
| 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"; |
| 93 | |
| 94 | vec_validate_aligned (cm->threads, tm->n_vlib_mains, CLIB_CACHE_LINE_BYTES); |
| 95 | vec_validate (cm->algs, VNET_CRYPTO_N_ALGS); |
| 96 | |
| 97 | #define _(n, s) \ |
| 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; |
| 105 | foreach_crypto_alg; |
| 106 | #undef _ |
| 107 | |
| 108 | #define _(n, s) \ |
| 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; |
| 113 | foreach_hmac_alg; |
| 114 | #undef _ |
| 115 | |
| 116 | return 0; |
| 117 | } |
| 118 | |
| 119 | VLIB_INIT_FUNCTION (vnet_crypto_init); |
| 120 | |
| 121 | /* |
| 122 | * fd.io coding-style-patch-verification: ON |
| 123 | * |
| 124 | * Local Variables: |
| 125 | * eval: (c-set-style "gnu") |
| 126 | * End: |
| 127 | */ |