Pavel Kotucek | d9aad29 | 2017-01-25 08:45:38 +0100 | [diff] [blame] | 1 | /* |
| 2 | *------------------------------------------------------------------ |
| 3 | * policer_api.c - policer api |
| 4 | * |
| 5 | * Copyright (c) 2016 Cisco and/or its affiliates. |
| 6 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | * you may not use this file except in compliance with the License. |
| 8 | * You may obtain a copy of the License at: |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, software |
| 13 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | * See the License for the specific language governing permissions and |
| 16 | * limitations under the License. |
| 17 | *------------------------------------------------------------------ |
| 18 | */ |
| 19 | |
| 20 | #include <vnet/vnet.h> |
| 21 | #include <vlibmemory/api.h> |
| 22 | |
| 23 | #include <vnet/interface.h> |
| 24 | #include <vnet/api_errno.h> |
| 25 | #include <vnet/policer/policer.h> |
| 26 | |
Filip Tehlar | 102e8a5 | 2021-06-22 08:37:26 +0000 | [diff] [blame] | 27 | #include <vnet/format_fns.h> |
| 28 | #include <vnet/policer/policer.api_enum.h> |
| 29 | #include <vnet/policer/policer.api_types.h> |
Pavel Kotucek | d9aad29 | 2017-01-25 08:45:38 +0100 | [diff] [blame] | 30 | |
Filip Tehlar | 102e8a5 | 2021-06-22 08:37:26 +0000 | [diff] [blame] | 31 | #define REPLY_MSG_ID_BASE vnet_policer_main.msg_id_base |
Pavel Kotucek | d9aad29 | 2017-01-25 08:45:38 +0100 | [diff] [blame] | 32 | #include <vlibapi/api_helper_macros.h> |
| 33 | |
Pavel Kotucek | d9aad29 | 2017-01-25 08:45:38 +0100 | [diff] [blame] | 34 | static void |
| 35 | vl_api_policer_add_del_t_handler (vl_api_policer_add_del_t * mp) |
| 36 | { |
| 37 | vlib_main_t *vm = vlib_get_main (); |
| 38 | vl_api_policer_add_del_reply_t *rmp; |
| 39 | int rv = 0; |
| 40 | u8 *name = NULL; |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 41 | qos_pol_cfg_params_st cfg; |
Pavel Kotucek | d9aad29 | 2017-01-25 08:45:38 +0100 | [diff] [blame] | 42 | clib_error_t *error; |
| 43 | u32 policer_index; |
| 44 | |
| 45 | name = format (0, "%s", mp->name); |
Gabriel Ganne | 3491d7f | 2017-10-19 15:10:46 +0200 | [diff] [blame] | 46 | vec_terminate_c_string (name); |
Pavel Kotucek | d9aad29 | 2017-01-25 08:45:38 +0100 | [diff] [blame] | 47 | |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 48 | clib_memset (&cfg, 0, sizeof (cfg)); |
Brian Russell | b2aae75 | 2021-02-09 11:36:31 +0000 | [diff] [blame] | 49 | cfg.rfc = (qos_policer_type_en) mp->type; |
| 50 | cfg.rnd_type = (qos_round_type_en) mp->round_type; |
| 51 | cfg.rate_type = (qos_rate_type_en) mp->rate_type; |
Neale Ranns | d91c1db | 2017-07-31 02:30:50 -0700 | [diff] [blame] | 52 | cfg.rb.kbps.cir_kbps = ntohl (mp->cir); |
| 53 | cfg.rb.kbps.eir_kbps = ntohl (mp->eir); |
| 54 | cfg.rb.kbps.cb_bytes = clib_net_to_host_u64 (mp->cb); |
| 55 | cfg.rb.kbps.eb_bytes = clib_net_to_host_u64 (mp->eb); |
Jakub Grajciar | cd01fb4 | 2020-03-02 13:16:53 +0100 | [diff] [blame] | 56 | cfg.conform_action.action_type = |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 57 | (qos_action_type_en) mp->conform_action.type; |
Jakub Grajciar | cd01fb4 | 2020-03-02 13:16:53 +0100 | [diff] [blame] | 58 | cfg.conform_action.dscp = mp->conform_action.dscp; |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 59 | cfg.exceed_action.action_type = (qos_action_type_en) mp->exceed_action.type; |
Jakub Grajciar | cd01fb4 | 2020-03-02 13:16:53 +0100 | [diff] [blame] | 60 | cfg.exceed_action.dscp = mp->exceed_action.dscp; |
| 61 | cfg.violate_action.action_type = |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 62 | (qos_action_type_en) mp->violate_action.type; |
Jakub Grajciar | cd01fb4 | 2020-03-02 13:16:53 +0100 | [diff] [blame] | 63 | cfg.violate_action.dscp = mp->violate_action.dscp; |
| 64 | |
Pavel Kotucek | d9aad29 | 2017-01-25 08:45:38 +0100 | [diff] [blame] | 65 | cfg.color_aware = mp->color_aware; |
| 66 | |
| 67 | error = policer_add_del (vm, name, &cfg, &policer_index, mp->is_add); |
| 68 | |
| 69 | if (error) |
Leung Lai Yung | afe3156 | 2021-12-27 04:14:20 +0000 | [diff] [blame] | 70 | { |
| 71 | rv = VNET_API_ERROR_UNSPECIFIED; |
| 72 | clib_error_free (error); |
| 73 | } |
Pavel Kotucek | d9aad29 | 2017-01-25 08:45:38 +0100 | [diff] [blame] | 74 | |
| 75 | /* *INDENT-OFF* */ |
| 76 | REPLY_MACRO2(VL_API_POLICER_ADD_DEL_REPLY, |
| 77 | ({ |
| 78 | if (rv == 0 && mp->is_add) |
| 79 | rmp->policer_index = ntohl(policer_index); |
| 80 | else |
| 81 | rmp->policer_index = ~0; |
| 82 | })); |
| 83 | /* *INDENT-ON* */ |
| 84 | } |
| 85 | |
| 86 | static void |
Brian Russell | 48e2636 | 2021-02-10 13:53:42 +0000 | [diff] [blame] | 87 | vl_api_policer_bind_t_handler (vl_api_policer_bind_t *mp) |
| 88 | { |
| 89 | vl_api_policer_bind_reply_t *rmp; |
| 90 | u8 *name; |
| 91 | u32 worker_index; |
| 92 | u8 bind_enable; |
| 93 | int rv; |
| 94 | |
| 95 | name = format (0, "%s", mp->name); |
| 96 | vec_terminate_c_string (name); |
| 97 | |
| 98 | worker_index = ntohl (mp->worker_index); |
| 99 | bind_enable = mp->bind_enable; |
| 100 | |
| 101 | rv = policer_bind_worker (name, worker_index, bind_enable); |
| 102 | vec_free (name); |
| 103 | REPLY_MACRO (VL_API_POLICER_BIND_REPLY); |
| 104 | } |
| 105 | |
| 106 | static void |
Brian Russell | b046830 | 2021-02-17 15:51:45 +0000 | [diff] [blame] | 107 | vl_api_policer_input_t_handler (vl_api_policer_input_t *mp) |
| 108 | { |
| 109 | vl_api_policer_bind_reply_t *rmp; |
| 110 | u8 *name; |
| 111 | u32 sw_if_index; |
| 112 | u8 apply; |
| 113 | int rv; |
| 114 | |
| 115 | VALIDATE_SW_IF_INDEX (mp); |
| 116 | |
| 117 | name = format (0, "%s", mp->name); |
| 118 | vec_terminate_c_string (name); |
| 119 | |
| 120 | sw_if_index = ntohl (mp->sw_if_index); |
| 121 | apply = mp->apply; |
| 122 | |
Stanislav Zaikin | e5a3ae0 | 2022-04-05 19:23:12 +0200 | [diff] [blame] | 123 | rv = policer_input (name, sw_if_index, VLIB_RX, apply); |
Brian Russell | b046830 | 2021-02-17 15:51:45 +0000 | [diff] [blame] | 124 | vec_free (name); |
| 125 | |
| 126 | BAD_SW_IF_INDEX_LABEL; |
| 127 | REPLY_MACRO (VL_API_POLICER_INPUT_REPLY); |
| 128 | } |
| 129 | |
| 130 | static void |
Stanislav Zaikin | e5a3ae0 | 2022-04-05 19:23:12 +0200 | [diff] [blame] | 131 | vl_api_policer_output_t_handler (vl_api_policer_input_t *mp) |
| 132 | { |
| 133 | vl_api_policer_bind_reply_t *rmp; |
| 134 | u8 *name; |
| 135 | u32 sw_if_index; |
| 136 | u8 apply; |
| 137 | int rv; |
| 138 | |
| 139 | VALIDATE_SW_IF_INDEX (mp); |
| 140 | |
| 141 | name = format (0, "%s", mp->name); |
| 142 | vec_terminate_c_string (name); |
| 143 | |
| 144 | sw_if_index = ntohl (mp->sw_if_index); |
| 145 | apply = mp->apply; |
| 146 | |
| 147 | rv = policer_input (name, sw_if_index, VLIB_TX, apply); |
| 148 | vec_free (name); |
| 149 | |
| 150 | BAD_SW_IF_INDEX_LABEL; |
| 151 | REPLY_MACRO (VL_API_POLICER_OUTPUT_REPLY); |
| 152 | } |
| 153 | |
| 154 | static void |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 155 | send_policer_details (u8 *name, qos_pol_cfg_params_st *config, |
Brian Russell | 54be0cc | 2021-02-15 11:49:42 +0000 | [diff] [blame] | 156 | policer_t *templ, vl_api_registration_t *reg, |
| 157 | u32 context) |
Pavel Kotucek | d9aad29 | 2017-01-25 08:45:38 +0100 | [diff] [blame] | 158 | { |
| 159 | vl_api_policer_details_t *mp; |
| 160 | |
| 161 | mp = vl_msg_api_alloc (sizeof (*mp)); |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 162 | clib_memset (mp, 0, sizeof (*mp)); |
Filip Tehlar | 102e8a5 | 2021-06-22 08:37:26 +0000 | [diff] [blame] | 163 | mp->_vl_msg_id = ntohs (REPLY_MSG_ID_BASE + VL_API_POLICER_DETAILS); |
Pavel Kotucek | d9aad29 | 2017-01-25 08:45:38 +0100 | [diff] [blame] | 164 | mp->context = context; |
| 165 | mp->cir = htonl (config->rb.kbps.cir_kbps); |
| 166 | mp->eir = htonl (config->rb.kbps.eir_kbps); |
Marek Gradzki | 59ed490 | 2017-03-21 11:51:54 +0100 | [diff] [blame] | 167 | mp->cb = clib_host_to_net_u64 (config->rb.kbps.cb_bytes); |
| 168 | mp->eb = clib_host_to_net_u64 (config->rb.kbps.eb_bytes); |
Brian Russell | b2aae75 | 2021-02-09 11:36:31 +0000 | [diff] [blame] | 169 | mp->rate_type = (vl_api_sse2_qos_rate_type_t) config->rate_type; |
| 170 | mp->round_type = (vl_api_sse2_qos_round_type_t) config->rnd_type; |
| 171 | mp->type = (vl_api_sse2_qos_policer_type_t) config->rfc; |
Jakub Grajciar | cd01fb4 | 2020-03-02 13:16:53 +0100 | [diff] [blame] | 172 | mp->conform_action.type = |
| 173 | (vl_api_sse2_qos_action_type_t) config->conform_action.action_type; |
| 174 | mp->conform_action.dscp = config->conform_action.dscp; |
| 175 | mp->exceed_action.type = |
| 176 | (vl_api_sse2_qos_action_type_t) config->exceed_action.action_type; |
| 177 | mp->exceed_action.dscp = config->exceed_action.dscp; |
| 178 | mp->violate_action.type = |
| 179 | (vl_api_sse2_qos_action_type_t) config->violate_action.action_type; |
| 180 | mp->violate_action.dscp = config->violate_action.dscp; |
Pavel Kotucek | d9aad29 | 2017-01-25 08:45:38 +0100 | [diff] [blame] | 181 | mp->single_rate = templ->single_rate ? 1 : 0; |
| 182 | mp->color_aware = templ->color_aware ? 1 : 0; |
| 183 | mp->scale = htonl (templ->scale); |
| 184 | mp->cir_tokens_per_period = htonl (templ->cir_tokens_per_period); |
| 185 | mp->pir_tokens_per_period = htonl (templ->pir_tokens_per_period); |
| 186 | mp->current_limit = htonl (templ->current_limit); |
| 187 | mp->current_bucket = htonl (templ->current_bucket); |
| 188 | mp->extended_limit = htonl (templ->extended_limit); |
| 189 | mp->extended_bucket = htonl (templ->extended_bucket); |
| 190 | mp->last_update_time = clib_host_to_net_u64 (templ->last_update_time); |
| 191 | |
| 192 | strncpy ((char *) mp->name, (char *) name, ARRAY_LEN (mp->name) - 1); |
| 193 | |
Florin Coras | 6c4dae2 | 2018-01-09 06:39:23 -0800 | [diff] [blame] | 194 | vl_api_send_msg (reg, (u8 *) mp); |
Pavel Kotucek | d9aad29 | 2017-01-25 08:45:38 +0100 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | static void |
| 198 | vl_api_policer_dump_t_handler (vl_api_policer_dump_t * mp) |
| 199 | { |
Florin Coras | 6c4dae2 | 2018-01-09 06:39:23 -0800 | [diff] [blame] | 200 | vl_api_registration_t *reg; |
Pavel Kotucek | d9aad29 | 2017-01-25 08:45:38 +0100 | [diff] [blame] | 201 | vnet_policer_main_t *pm = &vnet_policer_main; |
| 202 | hash_pair_t *hp; |
| 203 | uword *p; |
| 204 | u32 pool_index; |
| 205 | u8 *match_name = 0; |
| 206 | u8 *name; |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 207 | qos_pol_cfg_params_st *config; |
Brian Russell | 54be0cc | 2021-02-15 11:49:42 +0000 | [diff] [blame] | 208 | policer_t *templ; |
Pavel Kotucek | d9aad29 | 2017-01-25 08:45:38 +0100 | [diff] [blame] | 209 | |
Florin Coras | 6c4dae2 | 2018-01-09 06:39:23 -0800 | [diff] [blame] | 210 | reg = vl_api_client_index_to_registration (mp->client_index); |
| 211 | if (!reg) |
Pavel Kotucek | d9aad29 | 2017-01-25 08:45:38 +0100 | [diff] [blame] | 212 | return; |
| 213 | |
| 214 | if (mp->match_name_valid) |
| 215 | { |
| 216 | match_name = format (0, "%s%c", mp->match_name, 0); |
Gabriel Ganne | 3491d7f | 2017-10-19 15:10:46 +0200 | [diff] [blame] | 217 | vec_terminate_c_string (match_name); |
Pavel Kotucek | d9aad29 | 2017-01-25 08:45:38 +0100 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | if (mp->match_name_valid) |
| 221 | { |
| 222 | p = hash_get_mem (pm->policer_config_by_name, match_name); |
| 223 | if (p) |
| 224 | { |
| 225 | pool_index = p[0]; |
| 226 | config = pool_elt_at_index (pm->configs, pool_index); |
| 227 | templ = pool_elt_at_index (pm->policer_templates, pool_index); |
Florin Coras | 6c4dae2 | 2018-01-09 06:39:23 -0800 | [diff] [blame] | 228 | send_policer_details (match_name, config, templ, reg, mp->context); |
Pavel Kotucek | d9aad29 | 2017-01-25 08:45:38 +0100 | [diff] [blame] | 229 | } |
| 230 | } |
| 231 | else |
| 232 | { |
| 233 | /* *INDENT-OFF* */ |
| 234 | hash_foreach_pair (hp, pm->policer_config_by_name, |
| 235 | ({ |
| 236 | name = (u8 *) hp->key; |
| 237 | pool_index = hp->value[0]; |
| 238 | config = pool_elt_at_index (pm->configs, pool_index); |
| 239 | templ = pool_elt_at_index (pm->policer_templates, pool_index); |
Florin Coras | 6c4dae2 | 2018-01-09 06:39:23 -0800 | [diff] [blame] | 240 | send_policer_details(name, config, templ, reg, mp->context); |
Pavel Kotucek | d9aad29 | 2017-01-25 08:45:38 +0100 | [diff] [blame] | 241 | })); |
| 242 | /* *INDENT-ON* */ |
| 243 | } |
| 244 | } |
| 245 | |
Filip Tehlar | 102e8a5 | 2021-06-22 08:37:26 +0000 | [diff] [blame] | 246 | #include <vnet/policer/policer.api.c> |
Pavel Kotucek | d9aad29 | 2017-01-25 08:45:38 +0100 | [diff] [blame] | 247 | static clib_error_t * |
| 248 | policer_api_hookup (vlib_main_t * vm) |
| 249 | { |
Pavel Kotucek | d9aad29 | 2017-01-25 08:45:38 +0100 | [diff] [blame] | 250 | /* |
| 251 | * Set up the (msg_name, crc, message-id) table |
| 252 | */ |
Filip Tehlar | 102e8a5 | 2021-06-22 08:37:26 +0000 | [diff] [blame] | 253 | REPLY_MSG_ID_BASE = setup_message_id_table (); |
Pavel Kotucek | d9aad29 | 2017-01-25 08:45:38 +0100 | [diff] [blame] | 254 | |
| 255 | return 0; |
| 256 | } |
| 257 | |
| 258 | VLIB_API_INIT_FUNCTION (policer_api_hookup); |
| 259 | |
| 260 | /* |
| 261 | * fd.io coding-style-patch-verification: ON |
| 262 | * |
| 263 | * Local Variables: |
| 264 | * eval: (c-set-style "gnu") |
| 265 | * End: |
| 266 | */ |