Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2015 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 | */ |
Damjan Marion | e936bbe | 2016-02-25 23:17:38 +0100 | [diff] [blame] | 15 | #include <stdint.h> |
Brian Russell | 48e2636 | 2021-02-10 13:53:42 +0000 | [diff] [blame] | 16 | #include <stdbool.h> |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 17 | #include <vnet/policer/policer.h> |
Brian Russell | 97de8a2 | 2021-01-19 16:50:56 +0000 | [diff] [blame] | 18 | #include <vnet/policer/police_inlines.h> |
Matus Fabian | 70e6a8d | 2016-06-20 08:10:42 -0700 | [diff] [blame] | 19 | #include <vnet/classify/vnet_classify.h> |
Brian Russell | e3845d7 | 2021-02-08 15:33:18 +0000 | [diff] [blame] | 20 | #include <vnet/ip/ip_packet.h> |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 21 | |
Dave Wallace | 71612d6 | 2017-10-24 01:32:41 -0400 | [diff] [blame] | 22 | vnet_policer_main_t vnet_policer_main; |
| 23 | |
Brian Russell | 97de8a2 | 2021-01-19 16:50:56 +0000 | [diff] [blame] | 24 | u8 * |
| 25 | format_policer_handoff_trace (u8 *s, va_list *args) |
| 26 | { |
| 27 | CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *); |
| 28 | CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *); |
| 29 | policer_handoff_trace_t *t = va_arg (*args, policer_handoff_trace_t *); |
| 30 | |
| 31 | s = format (s, "policer %d, handoff thread %d to %d", t->policer_index, |
| 32 | t->current_worker_index, t->next_worker_index); |
| 33 | |
| 34 | return s; |
| 35 | } |
| 36 | |
Brian Russell | a71ed78 | 2021-01-27 11:34:33 +0000 | [diff] [blame] | 37 | vlib_combined_counter_main_t policer_counters[] = { |
| 38 | { |
| 39 | .name = "Policer-Conform", |
| 40 | .stat_segment_name = "/net/policer/conform", |
| 41 | }, |
| 42 | { |
| 43 | .name = "Policer-Exceed", |
| 44 | .stat_segment_name = "/net/policer/exceed", |
| 45 | }, |
| 46 | { |
| 47 | .name = "Policer-Violate", |
| 48 | .stat_segment_name = "/net/policer/violate", |
| 49 | }, |
| 50 | }; |
| 51 | |
Matus Fabian | 65fcd4d | 2016-05-13 05:44:48 -0700 | [diff] [blame] | 52 | clib_error_t * |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 53 | policer_add_del (vlib_main_t *vm, u8 *name, qos_pol_cfg_params_st *cfg, |
| 54 | u32 *policer_index, u8 is_add) |
Matus Fabian | 65fcd4d | 2016-05-13 05:44:48 -0700 | [diff] [blame] | 55 | { |
| 56 | vnet_policer_main_t *pm = &vnet_policer_main; |
Brian Russell | 54be0cc | 2021-02-15 11:49:42 +0000 | [diff] [blame] | 57 | policer_t test_policer; |
| 58 | policer_t *policer; |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 59 | uword *p; |
Matus Fabian | 70e6a8d | 2016-06-20 08:10:42 -0700 | [diff] [blame] | 60 | u32 pi; |
Matus Fabian | 65fcd4d | 2016-05-13 05:44:48 -0700 | [diff] [blame] | 61 | int rv; |
| 62 | |
Matus Fabian | 70e6a8d | 2016-06-20 08:10:42 -0700 | [diff] [blame] | 63 | p = hash_get_mem (pm->policer_config_by_name, name); |
| 64 | |
Matus Fabian | 65fcd4d | 2016-05-13 05:44:48 -0700 | [diff] [blame] | 65 | if (is_add == 0) |
| 66 | { |
Chaoyu Jin | 913b873 | 2017-08-08 13:36:23 -0700 | [diff] [blame] | 67 | /* free policer config and template */ |
Matus Fabian | 65fcd4d | 2016-05-13 05:44:48 -0700 | [diff] [blame] | 68 | if (p == 0) |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 69 | { |
| 70 | vec_free (name); |
| 71 | return clib_error_return (0, "No such policer configuration"); |
| 72 | } |
Chaoyu Jin | 913b873 | 2017-08-08 13:36:23 -0700 | [diff] [blame] | 73 | pool_put_index (pm->configs, p[0]); |
| 74 | pool_put_index (pm->policer_templates, p[0]); |
Matus Fabian | 65fcd4d | 2016-05-13 05:44:48 -0700 | [diff] [blame] | 75 | hash_unset_mem (pm->policer_config_by_name, name); |
Chaoyu Jin | 913b873 | 2017-08-08 13:36:23 -0700 | [diff] [blame] | 76 | |
| 77 | /* free policer */ |
| 78 | p = hash_get_mem (pm->policer_index_by_name, name); |
| 79 | if (p == 0) |
| 80 | { |
| 81 | vec_free (name); |
| 82 | return clib_error_return (0, "No such policer"); |
| 83 | } |
| 84 | pool_put_index (pm->policers, p[0]); |
Matus Fabian | 70e6a8d | 2016-06-20 08:10:42 -0700 | [diff] [blame] | 85 | hash_unset_mem (pm->policer_index_by_name, name); |
Chaoyu Jin | 913b873 | 2017-08-08 13:36:23 -0700 | [diff] [blame] | 86 | |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 87 | vec_free (name); |
Matus Fabian | 65fcd4d | 2016-05-13 05:44:48 -0700 | [diff] [blame] | 88 | return 0; |
| 89 | } |
| 90 | |
Matus Fabian | 70e6a8d | 2016-06-20 08:10:42 -0700 | [diff] [blame] | 91 | if (p != 0) |
| 92 | { |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 93 | vec_free (name); |
Matus Fabian | 70e6a8d | 2016-06-20 08:10:42 -0700 | [diff] [blame] | 94 | return clib_error_return (0, "Policer already exists"); |
| 95 | } |
| 96 | |
Matus Fabian | 65fcd4d | 2016-05-13 05:44:48 -0700 | [diff] [blame] | 97 | /* Vet the configuration before adding it to the table */ |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 98 | rv = pol_logical_2_physical (cfg, &test_policer); |
Matus Fabian | 65fcd4d | 2016-05-13 05:44:48 -0700 | [diff] [blame] | 99 | |
| 100 | if (rv == 0) |
| 101 | { |
Brian Russell | 54be0cc | 2021-02-15 11:49:42 +0000 | [diff] [blame] | 102 | policer_t *pp; |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 103 | qos_pol_cfg_params_st *cp; |
Brian Russell | a71ed78 | 2021-01-27 11:34:33 +0000 | [diff] [blame] | 104 | int i; |
Matus Fabian | 65fcd4d | 2016-05-13 05:44:48 -0700 | [diff] [blame] | 105 | |
| 106 | pool_get (pm->configs, cp); |
| 107 | pool_get (pm->policer_templates, pp); |
| 108 | |
| 109 | ASSERT (cp - pm->configs == pp - pm->policer_templates); |
| 110 | |
| 111 | clib_memcpy (cp, cfg, sizeof (*cp)); |
| 112 | clib_memcpy (pp, &test_policer, sizeof (*pp)); |
| 113 | |
| 114 | hash_set_mem (pm->policer_config_by_name, name, cp - pm->configs); |
Matus Fabian | 70e6a8d | 2016-06-20 08:10:42 -0700 | [diff] [blame] | 115 | pool_get_aligned (pm->policers, policer, CLIB_CACHE_LINE_BYTES); |
| 116 | policer[0] = pp[0]; |
| 117 | pi = policer - pm->policers; |
| 118 | hash_set_mem (pm->policer_index_by_name, name, pi); |
| 119 | *policer_index = pi; |
Brian Russell | 89cd744 | 2021-01-19 16:46:08 +0000 | [diff] [blame] | 120 | policer->thread_index = ~0; |
Brian Russell | a71ed78 | 2021-01-27 11:34:33 +0000 | [diff] [blame] | 121 | |
| 122 | for (i = 0; i < NUM_POLICE_RESULTS; i++) |
| 123 | { |
| 124 | vlib_validate_combined_counter (&policer_counters[i], pi); |
| 125 | vlib_zero_combined_counter (&policer_counters[i], pi); |
| 126 | } |
Matus Fabian | 65fcd4d | 2016-05-13 05:44:48 -0700 | [diff] [blame] | 127 | } |
| 128 | else |
| 129 | { |
| 130 | vec_free (name); |
| 131 | return clib_error_return (0, "Config failed sanity check"); |
| 132 | } |
| 133 | |
| 134 | return 0; |
| 135 | } |
| 136 | |
Brian Russell | 48e2636 | 2021-02-10 13:53:42 +0000 | [diff] [blame] | 137 | int |
| 138 | policer_bind_worker (u8 *name, u32 worker, bool bind) |
| 139 | { |
| 140 | vnet_policer_main_t *pm = &vnet_policer_main; |
Brian Russell | 54be0cc | 2021-02-15 11:49:42 +0000 | [diff] [blame] | 141 | policer_t *policer; |
Brian Russell | 48e2636 | 2021-02-10 13:53:42 +0000 | [diff] [blame] | 142 | uword *p; |
| 143 | |
| 144 | p = hash_get_mem (pm->policer_index_by_name, name); |
| 145 | if (p == 0) |
| 146 | { |
| 147 | return VNET_API_ERROR_NO_SUCH_ENTRY; |
| 148 | } |
| 149 | |
| 150 | policer = &pm->policers[p[0]]; |
| 151 | |
| 152 | if (bind) |
| 153 | { |
| 154 | if (worker >= vlib_num_workers ()) |
| 155 | { |
| 156 | return VNET_API_ERROR_INVALID_WORKER; |
| 157 | } |
| 158 | |
| 159 | policer->thread_index = vlib_get_worker_thread_index (worker); |
| 160 | } |
| 161 | else |
| 162 | { |
| 163 | policer->thread_index = ~0; |
| 164 | } |
| 165 | return 0; |
| 166 | } |
| 167 | |
Brian Russell | b046830 | 2021-02-17 15:51:45 +0000 | [diff] [blame] | 168 | int |
Stanislav Zaikin | e5a3ae0 | 2022-04-05 19:23:12 +0200 | [diff] [blame] | 169 | policer_input (u8 *name, u32 sw_if_index, vlib_dir_t dir, bool apply) |
Brian Russell | b046830 | 2021-02-17 15:51:45 +0000 | [diff] [blame] | 170 | { |
| 171 | vnet_policer_main_t *pm = &vnet_policer_main; |
| 172 | policer_t *policer; |
| 173 | u32 policer_index; |
| 174 | uword *p; |
| 175 | |
| 176 | p = hash_get_mem (pm->policer_index_by_name, name); |
| 177 | if (p == 0) |
| 178 | { |
| 179 | return VNET_API_ERROR_NO_SUCH_ENTRY; |
| 180 | } |
| 181 | |
| 182 | policer = &pm->policers[p[0]]; |
| 183 | policer_index = policer - pm->policers; |
| 184 | |
| 185 | if (apply) |
| 186 | { |
Stanislav Zaikin | e5a3ae0 | 2022-04-05 19:23:12 +0200 | [diff] [blame] | 187 | vec_validate (pm->policer_index_by_sw_if_index[dir], sw_if_index); |
| 188 | pm->policer_index_by_sw_if_index[dir][sw_if_index] = policer_index; |
Brian Russell | b046830 | 2021-02-17 15:51:45 +0000 | [diff] [blame] | 189 | } |
| 190 | else |
| 191 | { |
Stanislav Zaikin | e5a3ae0 | 2022-04-05 19:23:12 +0200 | [diff] [blame] | 192 | pm->policer_index_by_sw_if_index[dir][sw_if_index] = ~0; |
Brian Russell | b046830 | 2021-02-17 15:51:45 +0000 | [diff] [blame] | 193 | } |
| 194 | |
Stanislav Zaikin | e5a3ae0 | 2022-04-05 19:23:12 +0200 | [diff] [blame] | 195 | if (dir == VLIB_RX) |
| 196 | { |
| 197 | vnet_feature_enable_disable ("device-input", "policer-input", |
| 198 | sw_if_index, apply, 0, 0); |
| 199 | } |
| 200 | else |
| 201 | { |
| 202 | vnet_feature_enable_disable ("ip4-output", "policer-output", sw_if_index, |
| 203 | apply, 0, 0); |
| 204 | vnet_feature_enable_disable ("ip6-output", "policer-output", sw_if_index, |
| 205 | apply, 0, 0); |
| 206 | } |
Brian Russell | b046830 | 2021-02-17 15:51:45 +0000 | [diff] [blame] | 207 | return 0; |
| 208 | } |
| 209 | |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 210 | u8 * |
| 211 | format_policer_instance (u8 * s, va_list * va) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 212 | { |
Brian Russell | 54be0cc | 2021-02-15 11:49:42 +0000 | [diff] [blame] | 213 | policer_t *i = va_arg (*va, policer_t *); |
Brian Russell | a71ed78 | 2021-01-27 11:34:33 +0000 | [diff] [blame] | 214 | uword pi = va_arg (*va, uword); |
| 215 | int result; |
| 216 | vlib_counter_t counts[NUM_POLICE_RESULTS]; |
| 217 | |
| 218 | for (result = 0; result < NUM_POLICE_RESULTS; result++) |
| 219 | { |
| 220 | vlib_get_combined_counter (&policer_counters[result], pi, |
| 221 | &counts[result]); |
| 222 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 223 | |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 224 | s = format (s, "policer at %llx: %s rate, %s color-aware\n", |
| 225 | i, i->single_rate ? "single" : "dual", |
| 226 | i->color_aware ? "is" : "not"); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 227 | s = format (s, "cir %u tok/period, pir %u tok/period, scale %u\n", |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 228 | i->cir_tokens_per_period, i->pir_tokens_per_period, i->scale); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 229 | s = format (s, "cur lim %u, cur bkt %u, ext lim %u, ext bkt %u\n", |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 230 | i->current_limit, |
| 231 | i->current_bucket, i->extended_limit, i->extended_bucket); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 232 | s = format (s, "last update %llu\n", i->last_update_time); |
Brian Russell | a71ed78 | 2021-01-27 11:34:33 +0000 | [diff] [blame] | 233 | s = format (s, "conform %llu packets, %llu bytes\n", |
| 234 | counts[POLICE_CONFORM].packets, counts[POLICE_CONFORM].bytes); |
| 235 | s = format (s, "exceed %llu packets, %llu bytes\n", |
| 236 | counts[POLICE_EXCEED].packets, counts[POLICE_EXCEED].bytes); |
| 237 | s = format (s, "violate %llu packets, %llu bytes\n", |
| 238 | counts[POLICE_VIOLATE].packets, counts[POLICE_VIOLATE].bytes); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 239 | return s; |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 240 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 241 | |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 242 | static u8 * |
| 243 | format_policer_round_type (u8 * s, va_list * va) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 244 | { |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 245 | qos_pol_cfg_params_st *c = va_arg (*va, qos_pol_cfg_params_st *); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 246 | |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 247 | if (c->rnd_type == QOS_ROUND_TO_CLOSEST) |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 248 | s = format (s, "closest"); |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 249 | else if (c->rnd_type == QOS_ROUND_TO_UP) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 250 | s = format (s, "up"); |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 251 | else if (c->rnd_type == QOS_ROUND_TO_DOWN) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 252 | s = format (s, "down"); |
| 253 | else |
| 254 | s = format (s, "ILLEGAL"); |
| 255 | return s; |
| 256 | } |
| 257 | |
| 258 | |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 259 | static u8 * |
| 260 | format_policer_rate_type (u8 * s, va_list * va) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 261 | { |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 262 | qos_pol_cfg_params_st *c = va_arg (*va, qos_pol_cfg_params_st *); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 263 | |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 264 | if (c->rate_type == QOS_RATE_KBPS) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 265 | s = format (s, "kbps"); |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 266 | else if (c->rate_type == QOS_RATE_PPS) |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 267 | s = format (s, "pps"); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 268 | else |
| 269 | s = format (s, "ILLEGAL"); |
| 270 | return s; |
| 271 | } |
| 272 | |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 273 | static u8 * |
| 274 | format_policer_type (u8 * s, va_list * va) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 275 | { |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 276 | qos_pol_cfg_params_st *c = va_arg (*va, qos_pol_cfg_params_st *); |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 277 | |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 278 | if (c->rfc == QOS_POLICER_TYPE_1R2C) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 279 | s = format (s, "1r2c"); |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 280 | |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 281 | else if (c->rfc == QOS_POLICER_TYPE_1R3C_RFC_2697) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 282 | s = format (s, "1r3c"); |
| 283 | |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 284 | else if (c->rfc == QOS_POLICER_TYPE_2R3C_RFC_2698) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 285 | s = format (s, "2r3c-2698"); |
| 286 | |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 287 | else if (c->rfc == QOS_POLICER_TYPE_2R3C_RFC_4115) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 288 | s = format (s, "2r3c-4115"); |
| 289 | |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 290 | else if (c->rfc == QOS_POLICER_TYPE_2R3C_RFC_MEF5CF1) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 291 | s = format (s, "2r3c-mef5cf1"); |
| 292 | else |
| 293 | s = format (s, "ILLEGAL"); |
| 294 | return s; |
| 295 | } |
| 296 | |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 297 | static u8 * |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 298 | format_policer_action_type (u8 * s, va_list * va) |
Matus Fabian | 4ac74c9 | 2016-05-31 07:33:29 -0700 | [diff] [blame] | 299 | { |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 300 | qos_pol_action_params_st *a = va_arg (*va, qos_pol_action_params_st *); |
Matus Fabian | 4ac74c9 | 2016-05-31 07:33:29 -0700 | [diff] [blame] | 301 | |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 302 | if (a->action_type == QOS_ACTION_DROP) |
Matus Fabian | 4ac74c9 | 2016-05-31 07:33:29 -0700 | [diff] [blame] | 303 | s = format (s, "drop"); |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 304 | else if (a->action_type == QOS_ACTION_TRANSMIT) |
Matus Fabian | 4ac74c9 | 2016-05-31 07:33:29 -0700 | [diff] [blame] | 305 | s = format (s, "transmit"); |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 306 | else if (a->action_type == QOS_ACTION_MARK_AND_TRANSMIT) |
Brian Russell | e3845d7 | 2021-02-08 15:33:18 +0000 | [diff] [blame] | 307 | s = format (s, "mark-and-transmit %U", format_ip_dscp, a->dscp); |
Matus Fabian | 4ac74c9 | 2016-05-31 07:33:29 -0700 | [diff] [blame] | 308 | else |
| 309 | s = format (s, "ILLEGAL"); |
| 310 | return s; |
| 311 | } |
| 312 | |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 313 | u8 * |
| 314 | format_policer_config (u8 * s, va_list * va) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 315 | { |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 316 | qos_pol_cfg_params_st *c = va_arg (*va, qos_pol_cfg_params_st *); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 317 | |
| 318 | s = format (s, "type %U cir %u eir %u cb %u eb %u\n", |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 319 | format_policer_type, c, |
| 320 | c->rb.kbps.cir_kbps, |
| 321 | c->rb.kbps.eir_kbps, c->rb.kbps.cb_bytes, c->rb.kbps.eb_bytes); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 322 | s = format (s, "rate type %U, round type %U\n", |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 323 | format_policer_rate_type, c, format_policer_round_type, c); |
Matus Fabian | 4ac74c9 | 2016-05-31 07:33:29 -0700 | [diff] [blame] | 324 | s = format (s, "conform action %U, exceed action %U, violate action %U\n", |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 325 | format_policer_action_type, &c->conform_action, |
| 326 | format_policer_action_type, &c->exceed_action, |
| 327 | format_policer_action_type, &c->violate_action); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 328 | return s; |
| 329 | } |
| 330 | |
| 331 | static uword |
| 332 | unformat_policer_type (unformat_input_t * input, va_list * va) |
| 333 | { |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 334 | qos_pol_cfg_params_st *c = va_arg (*va, qos_pol_cfg_params_st *); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 335 | |
| 336 | if (!unformat (input, "type")) |
| 337 | return 0; |
| 338 | |
| 339 | if (unformat (input, "1r2c")) |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 340 | c->rfc = QOS_POLICER_TYPE_1R2C; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 341 | else if (unformat (input, "1r3c")) |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 342 | c->rfc = QOS_POLICER_TYPE_1R3C_RFC_2697; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 343 | else if (unformat (input, "2r3c-2698")) |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 344 | c->rfc = QOS_POLICER_TYPE_2R3C_RFC_2698; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 345 | else if (unformat (input, "2r3c-4115")) |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 346 | c->rfc = QOS_POLICER_TYPE_2R3C_RFC_4115; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 347 | else if (unformat (input, "2r3c-mef5cf1")) |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 348 | c->rfc = QOS_POLICER_TYPE_2R3C_RFC_MEF5CF1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 349 | else |
| 350 | return 0; |
| 351 | return 1; |
| 352 | } |
| 353 | |
| 354 | static uword |
| 355 | unformat_policer_round_type (unformat_input_t * input, va_list * va) |
| 356 | { |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 357 | qos_pol_cfg_params_st *c = va_arg (*va, qos_pol_cfg_params_st *); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 358 | |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 359 | if (!unformat (input, "round")) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 360 | return 0; |
| 361 | |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 362 | if (unformat (input, "closest")) |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 363 | c->rnd_type = QOS_ROUND_TO_CLOSEST; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 364 | else if (unformat (input, "up")) |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 365 | c->rnd_type = QOS_ROUND_TO_UP; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 366 | else if (unformat (input, "down")) |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 367 | c->rnd_type = QOS_ROUND_TO_DOWN; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 368 | else |
| 369 | return 0; |
| 370 | return 1; |
| 371 | } |
| 372 | |
| 373 | static uword |
| 374 | unformat_policer_rate_type (unformat_input_t * input, va_list * va) |
| 375 | { |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 376 | qos_pol_cfg_params_st *c = va_arg (*va, qos_pol_cfg_params_st *); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 377 | |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 378 | if (!unformat (input, "rate")) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 379 | return 0; |
| 380 | |
| 381 | if (unformat (input, "kbps")) |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 382 | c->rate_type = QOS_RATE_KBPS; |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 383 | else if (unformat (input, "pps")) |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 384 | c->rate_type = QOS_RATE_PPS; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 385 | else |
| 386 | return 0; |
| 387 | return 1; |
| 388 | } |
| 389 | |
| 390 | static uword |
| 391 | unformat_policer_cir (unformat_input_t * input, va_list * va) |
| 392 | { |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 393 | qos_pol_cfg_params_st *c = va_arg (*va, qos_pol_cfg_params_st *); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 394 | |
| 395 | if (unformat (input, "cir %u", &c->rb.kbps.cir_kbps)) |
| 396 | return 1; |
| 397 | return 0; |
| 398 | } |
| 399 | |
| 400 | static uword |
| 401 | unformat_policer_eir (unformat_input_t * input, va_list * va) |
| 402 | { |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 403 | qos_pol_cfg_params_st *c = va_arg (*va, qos_pol_cfg_params_st *); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 404 | |
| 405 | if (unformat (input, "eir %u", &c->rb.kbps.eir_kbps)) |
| 406 | return 1; |
| 407 | return 0; |
| 408 | } |
| 409 | |
| 410 | static uword |
| 411 | unformat_policer_cb (unformat_input_t * input, va_list * va) |
| 412 | { |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 413 | qos_pol_cfg_params_st *c = va_arg (*va, qos_pol_cfg_params_st *); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 414 | |
| 415 | if (unformat (input, "cb %u", &c->rb.kbps.cb_bytes)) |
| 416 | return 1; |
| 417 | return 0; |
| 418 | } |
| 419 | |
| 420 | static uword |
| 421 | unformat_policer_eb (unformat_input_t * input, va_list * va) |
| 422 | { |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 423 | qos_pol_cfg_params_st *c = va_arg (*va, qos_pol_cfg_params_st *); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 424 | |
| 425 | if (unformat (input, "eb %u", &c->rb.kbps.eb_bytes)) |
| 426 | return 1; |
| 427 | return 0; |
| 428 | } |
| 429 | |
Matus Fabian | 4ac74c9 | 2016-05-31 07:33:29 -0700 | [diff] [blame] | 430 | static uword |
Matus Fabian | 4ac74c9 | 2016-05-31 07:33:29 -0700 | [diff] [blame] | 431 | unformat_policer_action_type (unformat_input_t * input, va_list * va) |
| 432 | { |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 433 | qos_pol_action_params_st *a = va_arg (*va, qos_pol_action_params_st *); |
Matus Fabian | 4ac74c9 | 2016-05-31 07:33:29 -0700 | [diff] [blame] | 434 | |
| 435 | if (unformat (input, "drop")) |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 436 | a->action_type = QOS_ACTION_DROP; |
Matus Fabian | 4ac74c9 | 2016-05-31 07:33:29 -0700 | [diff] [blame] | 437 | else if (unformat (input, "transmit")) |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 438 | a->action_type = QOS_ACTION_TRANSMIT; |
Brian Russell | e3845d7 | 2021-02-08 15:33:18 +0000 | [diff] [blame] | 439 | else if (unformat (input, "mark-and-transmit %U", unformat_ip_dscp, |
| 440 | &a->dscp)) |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 441 | a->action_type = QOS_ACTION_MARK_AND_TRANSMIT; |
Matus Fabian | 4ac74c9 | 2016-05-31 07:33:29 -0700 | [diff] [blame] | 442 | else |
| 443 | return 0; |
| 444 | return 1; |
| 445 | } |
| 446 | |
| 447 | static uword |
| 448 | unformat_policer_action (unformat_input_t * input, va_list * va) |
| 449 | { |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 450 | qos_pol_cfg_params_st *c = va_arg (*va, qos_pol_cfg_params_st *); |
Matus Fabian | 4ac74c9 | 2016-05-31 07:33:29 -0700 | [diff] [blame] | 451 | |
| 452 | if (unformat (input, "conform-action %U", unformat_policer_action_type, |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 453 | &c->conform_action)) |
Matus Fabian | 4ac74c9 | 2016-05-31 07:33:29 -0700 | [diff] [blame] | 454 | return 1; |
| 455 | else if (unformat (input, "exceed-action %U", unformat_policer_action_type, |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 456 | &c->exceed_action)) |
Matus Fabian | 4ac74c9 | 2016-05-31 07:33:29 -0700 | [diff] [blame] | 457 | return 1; |
| 458 | else if (unformat (input, "violate-action %U", unformat_policer_action_type, |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 459 | &c->violate_action)) |
Matus Fabian | 4ac74c9 | 2016-05-31 07:33:29 -0700 | [diff] [blame] | 460 | return 1; |
| 461 | return 0; |
| 462 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 463 | |
Matus Fabian | 70e6a8d | 2016-06-20 08:10:42 -0700 | [diff] [blame] | 464 | static uword |
| 465 | unformat_policer_classify_next_index (unformat_input_t * input, va_list * va) |
| 466 | { |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 467 | u32 *r = va_arg (*va, u32 *); |
Matus Fabian | 70e6a8d | 2016-06-20 08:10:42 -0700 | [diff] [blame] | 468 | vnet_policer_main_t *pm = &vnet_policer_main; |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 469 | uword *p; |
| 470 | u8 *match_name = 0; |
Matus Fabian | 70e6a8d | 2016-06-20 08:10:42 -0700 | [diff] [blame] | 471 | |
| 472 | if (unformat (input, "%s", &match_name)) |
| 473 | ; |
| 474 | else |
| 475 | return 0; |
| 476 | |
| 477 | p = hash_get_mem (pm->policer_index_by_name, match_name); |
| 478 | |
| 479 | if (p == 0) |
| 480 | return 0; |
| 481 | |
| 482 | *r = p[0]; |
| 483 | |
| 484 | return 1; |
| 485 | } |
| 486 | |
| 487 | static uword |
| 488 | unformat_policer_classify_precolor (unformat_input_t * input, va_list * va) |
| 489 | { |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 490 | u32 *r = va_arg (*va, u32 *); |
Matus Fabian | 70e6a8d | 2016-06-20 08:10:42 -0700 | [diff] [blame] | 491 | |
| 492 | if (unformat (input, "conform-color")) |
| 493 | *r = POLICE_CONFORM; |
| 494 | else if (unformat (input, "exceed-color")) |
| 495 | *r = POLICE_EXCEED; |
| 496 | else |
| 497 | return 0; |
| 498 | |
| 499 | return 1; |
| 500 | } |
| 501 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 502 | #define foreach_config_param \ |
| 503 | _(eb) \ |
| 504 | _(cb) \ |
| 505 | _(eir) \ |
| 506 | _(cir) \ |
| 507 | _(rate_type) \ |
| 508 | _(round_type) \ |
Matus Fabian | 4ac74c9 | 2016-05-31 07:33:29 -0700 | [diff] [blame] | 509 | _(type) \ |
| 510 | _(action) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 511 | |
| 512 | static clib_error_t * |
Neale Ranns | 415cd67 | 2021-03-19 08:54:38 +0000 | [diff] [blame] | 513 | policer_add_command_fn (vlib_main_t *vm, unformat_input_t *input, |
| 514 | vlib_cli_command_t *cmd) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 515 | { |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 516 | qos_pol_cfg_params_st c; |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 517 | unformat_input_t _line_input, *line_input = &_line_input; |
Matus Fabian | 65fcd4d | 2016-05-13 05:44:48 -0700 | [diff] [blame] | 518 | u8 is_add = 1; |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 519 | u8 *name = 0; |
Matus Fabian | 70e6a8d | 2016-06-20 08:10:42 -0700 | [diff] [blame] | 520 | u32 pi; |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 521 | clib_error_t *error = NULL; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 522 | |
| 523 | /* Get a line of input. */ |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 524 | if (!unformat_user (input, unformat_line_input, line_input)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 525 | return 0; |
| 526 | |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 527 | clib_memset (&c, 0, sizeof (c)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 528 | |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 529 | while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 530 | { |
| 531 | if (unformat (line_input, "del")) |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 532 | is_add = 0; |
| 533 | else if (unformat (line_input, "name %s", &name)) |
| 534 | ; |
| 535 | else if (unformat (line_input, "color-aware")) |
| 536 | c.color_aware = 1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 537 | |
| 538 | #define _(a) else if (unformat (line_input, "%U", unformat_policer_##a, &c)) ; |
| 539 | foreach_config_param |
| 540 | #undef _ |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 541 | else |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 542 | { |
| 543 | error = clib_error_return (0, "unknown input `%U'", |
| 544 | format_unformat_error, line_input); |
| 545 | goto done; |
| 546 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 547 | } |
| 548 | |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 549 | error = policer_add_del (vm, name, &c, &pi, is_add); |
| 550 | |
| 551 | done: |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 552 | unformat_free (line_input); |
| 553 | |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 554 | return error; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 555 | } |
| 556 | |
Neale Ranns | 415cd67 | 2021-03-19 08:54:38 +0000 | [diff] [blame] | 557 | static clib_error_t * |
| 558 | policer_del_command_fn (vlib_main_t *vm, unformat_input_t *input, |
| 559 | vlib_cli_command_t *cmd) |
| 560 | { |
| 561 | unformat_input_t _line_input, *line_input = &_line_input; |
| 562 | clib_error_t *error = NULL; |
| 563 | u8 *name = 0; |
| 564 | |
| 565 | /* Get a line of input. */ |
| 566 | if (!unformat_user (input, unformat_line_input, line_input)) |
| 567 | return 0; |
| 568 | |
| 569 | while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) |
| 570 | { |
| 571 | if (unformat (line_input, "name %s", &name)) |
| 572 | ; |
| 573 | else |
| 574 | { |
| 575 | error = clib_error_return (0, "unknown input `%U'", |
| 576 | format_unformat_error, line_input); |
| 577 | goto done; |
| 578 | } |
| 579 | } |
| 580 | |
| 581 | error = policer_add_del (vm, name, NULL, NULL, 0); |
| 582 | |
| 583 | done: |
| 584 | unformat_free (line_input); |
| 585 | |
| 586 | return error; |
| 587 | } |
| 588 | |
| 589 | static clib_error_t * |
| 590 | policer_bind_command_fn (vlib_main_t *vm, unformat_input_t *input, |
| 591 | vlib_cli_command_t *cmd) |
| 592 | { |
| 593 | unformat_input_t _line_input, *line_input = &_line_input; |
| 594 | clib_error_t *error = NULL; |
| 595 | u8 bind, *name = 0; |
| 596 | u32 worker; |
| 597 | int rv; |
| 598 | |
| 599 | bind = 1; |
| 600 | worker = ~0; |
| 601 | |
| 602 | /* Get a line of input. */ |
| 603 | if (!unformat_user (input, unformat_line_input, line_input)) |
| 604 | return 0; |
| 605 | |
| 606 | while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) |
| 607 | { |
| 608 | if (unformat (line_input, "name %s", &name)) |
| 609 | ; |
jinshaohui | dcd7e24 | 2021-05-30 05:40:56 +0100 | [diff] [blame] | 610 | else if (unformat (line_input, "unbind")) |
Neale Ranns | 415cd67 | 2021-03-19 08:54:38 +0000 | [diff] [blame] | 611 | bind = 0; |
jinshaohui | dcd7e24 | 2021-05-30 05:40:56 +0100 | [diff] [blame] | 612 | else if (unformat (line_input, "%d", &worker)) |
Neale Ranns | 415cd67 | 2021-03-19 08:54:38 +0000 | [diff] [blame] | 613 | ; |
| 614 | else |
| 615 | { |
| 616 | error = clib_error_return (0, "unknown input `%U'", |
| 617 | format_unformat_error, line_input); |
| 618 | goto done; |
| 619 | } |
| 620 | } |
| 621 | |
| 622 | if (bind && ~0 == worker) |
| 623 | { |
| 624 | error = clib_error_return (0, "specify worker to bind to: `%U'", |
| 625 | format_unformat_error, line_input); |
| 626 | } |
| 627 | else |
| 628 | { |
| 629 | rv = policer_bind_worker (name, worker, bind); |
| 630 | |
| 631 | if (rv) |
| 632 | error = clib_error_return (0, "failed: `%d'", rv); |
| 633 | } |
| 634 | |
| 635 | done: |
| 636 | unformat_free (line_input); |
| 637 | |
| 638 | return error; |
| 639 | } |
| 640 | |
| 641 | static clib_error_t * |
| 642 | policer_input_command_fn (vlib_main_t *vm, unformat_input_t *input, |
| 643 | vlib_cli_command_t *cmd) |
| 644 | { |
| 645 | unformat_input_t _line_input, *line_input = &_line_input; |
| 646 | clib_error_t *error = NULL; |
| 647 | u8 apply, *name = 0; |
| 648 | u32 sw_if_index; |
| 649 | int rv; |
Stanislav Zaikin | e5a3ae0 | 2022-04-05 19:23:12 +0200 | [diff] [blame] | 650 | vlib_dir_t dir = cmd->function_arg; |
Neale Ranns | 415cd67 | 2021-03-19 08:54:38 +0000 | [diff] [blame] | 651 | |
| 652 | apply = 1; |
| 653 | sw_if_index = ~0; |
| 654 | |
| 655 | /* Get a line of input. */ |
| 656 | if (!unformat_user (input, unformat_line_input, line_input)) |
| 657 | return 0; |
| 658 | |
| 659 | while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) |
| 660 | { |
| 661 | if (unformat (line_input, "name %s", &name)) |
| 662 | ; |
| 663 | else if (unformat (line_input, "unapply")) |
| 664 | apply = 0; |
| 665 | else if (unformat (line_input, "%U", unformat_vnet_sw_interface, |
| 666 | vnet_get_main (), &sw_if_index)) |
| 667 | ; |
| 668 | else |
| 669 | { |
| 670 | error = clib_error_return (0, "unknown input `%U'", |
| 671 | format_unformat_error, line_input); |
| 672 | goto done; |
| 673 | } |
| 674 | } |
| 675 | |
| 676 | if (~0 == sw_if_index) |
| 677 | { |
| 678 | error = clib_error_return (0, "specify interface to apply to: `%U'", |
| 679 | format_unformat_error, line_input); |
| 680 | } |
| 681 | else |
| 682 | { |
Stanislav Zaikin | e5a3ae0 | 2022-04-05 19:23:12 +0200 | [diff] [blame] | 683 | rv = policer_input (name, sw_if_index, dir, apply); |
Neale Ranns | 415cd67 | 2021-03-19 08:54:38 +0000 | [diff] [blame] | 684 | |
| 685 | if (rv) |
| 686 | error = clib_error_return (0, "failed: `%d'", rv); |
| 687 | } |
| 688 | |
| 689 | done: |
| 690 | unformat_free (line_input); |
| 691 | |
| 692 | return error; |
| 693 | } |
| 694 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 695 | VLIB_CLI_COMMAND (configure_policer_command, static) = { |
Neale Ranns | 415cd67 | 2021-03-19 08:54:38 +0000 | [diff] [blame] | 696 | .path = "configure policer", |
Maxime Peim | ed5f291 | 2022-11-17 15:29:10 +0000 | [diff] [blame^] | 697 | .short_help = "configure policer name <name> [type 1r2c | 1r3c | 2r3c-2698 " |
| 698 | "| 2r3c-4115] [color-aware] [cir <cir>] [cb <cb>] [eir <eir>] " |
| 699 | "[eb <eb>] [rate kbps | pps] [round closest | up | down] " |
| 700 | "[conform-action drop | transmit | mark-and-transmit <dscp>] " |
| 701 | "[exceed-action drop | transmit | mark-and-transmit <dscp>] " |
| 702 | "[violate-action drop | transmit | mark-and-transmit <dscp>]", |
Neale Ranns | 415cd67 | 2021-03-19 08:54:38 +0000 | [diff] [blame] | 703 | .function = policer_add_command_fn, |
| 704 | }; |
Stanislav Zaikin | e5a3ae0 | 2022-04-05 19:23:12 +0200 | [diff] [blame] | 705 | |
Neale Ranns | 415cd67 | 2021-03-19 08:54:38 +0000 | [diff] [blame] | 706 | VLIB_CLI_COMMAND (policer_add_command, static) = { |
| 707 | .path = "policer add", |
Maxime Peim | ed5f291 | 2022-11-17 15:29:10 +0000 | [diff] [blame^] | 708 | .short_help = "policer name <name> [type 1r2c | 1r3c | 2r3c-2698 | " |
| 709 | "2r3c-4115] [color-aware] [cir <cir>] [cb <cb>] [eir <eir>] " |
| 710 | "[eb <eb>] [rate kbps | pps] [round closest | up | down] " |
| 711 | "[conform-action drop | transmit | mark-and-transmit <dscp>] " |
| 712 | "[exceed-action drop | transmit | mark-and-transmit <dscp>] " |
| 713 | "[violate-action drop | transmit | mark-and-transmit <dscp>]", |
Neale Ranns | 415cd67 | 2021-03-19 08:54:38 +0000 | [diff] [blame] | 714 | .function = policer_add_command_fn, |
| 715 | }; |
Stanislav Zaikin | e5a3ae0 | 2022-04-05 19:23:12 +0200 | [diff] [blame] | 716 | |
Neale Ranns | 415cd67 | 2021-03-19 08:54:38 +0000 | [diff] [blame] | 717 | VLIB_CLI_COMMAND (policer_del_command, static) = { |
| 718 | .path = "policer del", |
| 719 | .short_help = "policer del name <name> ", |
| 720 | .function = policer_del_command_fn, |
| 721 | }; |
Stanislav Zaikin | e5a3ae0 | 2022-04-05 19:23:12 +0200 | [diff] [blame] | 722 | |
Neale Ranns | 415cd67 | 2021-03-19 08:54:38 +0000 | [diff] [blame] | 723 | VLIB_CLI_COMMAND (policer_bind_command, static) = { |
| 724 | .path = "policer bind", |
| 725 | .short_help = "policer bind [unbind] name <name> <worker>", |
| 726 | .function = policer_bind_command_fn, |
| 727 | }; |
Stanislav Zaikin | e5a3ae0 | 2022-04-05 19:23:12 +0200 | [diff] [blame] | 728 | |
Neale Ranns | 415cd67 | 2021-03-19 08:54:38 +0000 | [diff] [blame] | 729 | VLIB_CLI_COMMAND (policer_input_command, static) = { |
| 730 | .path = "policer input", |
Maxime Peim | ed5f291 | 2022-11-17 15:29:10 +0000 | [diff] [blame^] | 731 | .short_help = "policer input [unapply] name <name> <interface>", |
Neale Ranns | 415cd67 | 2021-03-19 08:54:38 +0000 | [diff] [blame] | 732 | .function = policer_input_command_fn, |
Stanislav Zaikin | e5a3ae0 | 2022-04-05 19:23:12 +0200 | [diff] [blame] | 733 | .function_arg = VLIB_RX, |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 734 | }; |
Stanislav Zaikin | e5a3ae0 | 2022-04-05 19:23:12 +0200 | [diff] [blame] | 735 | |
| 736 | VLIB_CLI_COMMAND (policer_output_command, static) = { |
| 737 | .path = "policer output", |
Maxime Peim | ed5f291 | 2022-11-17 15:29:10 +0000 | [diff] [blame^] | 738 | .short_help = "policer output [unapply] name <name> <interface>", |
Stanislav Zaikin | e5a3ae0 | 2022-04-05 19:23:12 +0200 | [diff] [blame] | 739 | .function = policer_input_command_fn, |
| 740 | .function_arg = VLIB_TX, |
| 741 | }; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 742 | |
| 743 | static clib_error_t * |
| 744 | show_policer_command_fn (vlib_main_t * vm, |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 745 | unformat_input_t * input, vlib_cli_command_t * cmd) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 746 | { |
| 747 | vnet_policer_main_t *pm = &vnet_policer_main; |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 748 | hash_pair_t *p; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 749 | u32 pool_index; |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 750 | u8 *match_name = 0; |
| 751 | u8 *name; |
Brian Russell | a71ed78 | 2021-01-27 11:34:33 +0000 | [diff] [blame] | 752 | uword *pi; |
Brian Russell | c5299ff | 2021-02-09 10:16:58 +0000 | [diff] [blame] | 753 | qos_pol_cfg_params_st *config; |
Brian Russell | 54be0cc | 2021-02-15 11:49:42 +0000 | [diff] [blame] | 754 | policer_t *templ; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 755 | |
| 756 | (void) unformat (input, "name %s", &match_name); |
| 757 | |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 758 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 759 | hash_foreach_pair (p, pm->policer_config_by_name, |
| 760 | ({ |
| 761 | name = (u8 *) p->key; |
| 762 | if (match_name == 0 || !strcmp((char *) name, (char *) match_name)) |
| 763 | { |
Brian Russell | a71ed78 | 2021-01-27 11:34:33 +0000 | [diff] [blame] | 764 | pi = hash_get_mem (pm->policer_index_by_name, name); |
| 765 | |
| 766 | pool_index = p->value[0]; |
| 767 | config = pool_elt_at_index (pm->configs, pool_index); |
| 768 | templ = pool_elt_at_index (pm->policer_templates, pool_index); |
| 769 | vlib_cli_output (vm, "Name \"%s\" %U ", name, format_policer_config, |
| 770 | config); |
Klement Sekera | ba87563 | 2021-10-27 10:56:38 +0200 | [diff] [blame] | 771 | if (pi) |
| 772 | { |
| 773 | vlib_cli_output (vm, "Template %U", format_policer_instance, templ, |
| 774 | pi[0]); |
| 775 | } |
| 776 | else |
| 777 | { |
| 778 | vlib_cli_output ( |
| 779 | vm, "Cannot print template - policer index hash lookup failed"); |
| 780 | } |
Brian Russell | a71ed78 | 2021-01-27 11:34:33 +0000 | [diff] [blame] | 781 | vlib_cli_output (vm, "-----------"); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 782 | } |
| 783 | })); |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 784 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 785 | return 0; |
| 786 | } |
| 787 | |
| 788 | |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 789 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 790 | VLIB_CLI_COMMAND (show_policer_command, static) = { |
Huawei LI | 1f4b651 | 2022-11-05 02:50:53 +0800 | [diff] [blame] | 791 | .path = "show policer", |
| 792 | .short_help = "show policer [name <name>]", |
| 793 | .function = show_policer_command_fn, |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 794 | }; |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 795 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 796 | |
Chaoyu Jin | 913b873 | 2017-08-08 13:36:23 -0700 | [diff] [blame] | 797 | static clib_error_t * |
| 798 | show_policer_pools_command_fn (vlib_main_t * vm, |
| 799 | unformat_input_t * input, |
| 800 | vlib_cli_command_t * cmd) |
| 801 | { |
| 802 | vnet_policer_main_t *pm = &vnet_policer_main; |
| 803 | |
| 804 | vlib_cli_output (vm, "pool sizes: configs=%d templates=%d policers=%d", |
| 805 | pool_elts (pm->configs), |
| 806 | pool_elts (pm->policer_templates), |
| 807 | pool_elts (pm->policers)); |
| 808 | return 0; |
| 809 | } |
| 810 | /* *INDENT-OFF* */ |
| 811 | VLIB_CLI_COMMAND (show_policer_pools_command, static) = { |
| 812 | .path = "show policer pools", |
| 813 | .short_help = "show policer pools", |
| 814 | .function = show_policer_pools_command_fn, |
| 815 | }; |
| 816 | /* *INDENT-ON* */ |
| 817 | |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 818 | clib_error_t * |
| 819 | policer_init (vlib_main_t * vm) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 820 | { |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 821 | vnet_policer_main_t *pm = &vnet_policer_main; |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 822 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 823 | pm->vlib_main = vm; |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 824 | pm->vnet_main = vnet_get_main (); |
Brian Russell | bbccdc5 | 2021-02-10 18:34:48 +0000 | [diff] [blame] | 825 | pm->log_class = vlib_log_register_class ("policer", 0); |
Stanislav Zaikin | e5a3ae0 | 2022-04-05 19:23:12 +0200 | [diff] [blame] | 826 | pm->fq_index[VLIB_RX] = |
| 827 | vlib_frame_queue_main_init (policer_input_node.index, 0); |
| 828 | pm->fq_index[VLIB_TX] = |
| 829 | vlib_frame_queue_main_init (policer_output_node.index, 0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 830 | |
| 831 | pm->policer_config_by_name = hash_create_string (0, sizeof (uword)); |
Matus Fabian | 70e6a8d | 2016-06-20 08:10:42 -0700 | [diff] [blame] | 832 | pm->policer_index_by_name = hash_create_string (0, sizeof (uword)); |
| 833 | |
| 834 | vnet_classify_register_unformat_policer_next_index_fn |
| 835 | (unformat_policer_classify_next_index); |
| 836 | vnet_classify_register_unformat_opaque_index_fn |
| 837 | (unformat_policer_classify_precolor); |
| 838 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 839 | return 0; |
| 840 | } |
| 841 | |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 842 | VLIB_INIT_FUNCTION (policer_init); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 843 | |
| 844 | |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 845 | |
| 846 | /* |
| 847 | * fd.io coding-style-patch-verification: ON |
| 848 | * |
| 849 | * Local Variables: |
| 850 | * eval: (c-set-style "gnu") |
| 851 | * End: |
| 852 | */ |