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> |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 16 | #include <vnet/policer/policer.h> |
Brian Russell | 97de8a2 | 2021-01-19 16:50:56 +0000 | [diff] [blame] | 17 | #include <vnet/policer/police_inlines.h> |
Matus Fabian | 70e6a8d | 2016-06-20 08:10:42 -0700 | [diff] [blame] | 18 | #include <vnet/classify/vnet_classify.h> |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 19 | |
Dave Wallace | 71612d6 | 2017-10-24 01:32:41 -0400 | [diff] [blame] | 20 | vnet_policer_main_t vnet_policer_main; |
| 21 | |
Brian Russell | 97de8a2 | 2021-01-19 16:50:56 +0000 | [diff] [blame] | 22 | u8 * |
| 23 | format_policer_handoff_trace (u8 *s, va_list *args) |
| 24 | { |
| 25 | CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *); |
| 26 | CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *); |
| 27 | policer_handoff_trace_t *t = va_arg (*args, policer_handoff_trace_t *); |
| 28 | |
| 29 | s = format (s, "policer %d, handoff thread %d to %d", t->policer_index, |
| 30 | t->current_worker_index, t->next_worker_index); |
| 31 | |
| 32 | return s; |
| 33 | } |
| 34 | |
Matus Fabian | 65fcd4d | 2016-05-13 05:44:48 -0700 | [diff] [blame] | 35 | clib_error_t * |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 36 | policer_add_del (vlib_main_t * vm, |
| 37 | u8 * name, |
| 38 | sse2_qos_pol_cfg_params_st * cfg, |
| 39 | u32 * policer_index, u8 is_add) |
Matus Fabian | 65fcd4d | 2016-05-13 05:44:48 -0700 | [diff] [blame] | 40 | { |
| 41 | vnet_policer_main_t *pm = &vnet_policer_main; |
| 42 | policer_read_response_type_st test_policer; |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 43 | policer_read_response_type_st *policer; |
| 44 | uword *p; |
Matus Fabian | 70e6a8d | 2016-06-20 08:10:42 -0700 | [diff] [blame] | 45 | u32 pi; |
Matus Fabian | 65fcd4d | 2016-05-13 05:44:48 -0700 | [diff] [blame] | 46 | int rv; |
| 47 | |
Matus Fabian | 70e6a8d | 2016-06-20 08:10:42 -0700 | [diff] [blame] | 48 | p = hash_get_mem (pm->policer_config_by_name, name); |
| 49 | |
Matus Fabian | 65fcd4d | 2016-05-13 05:44:48 -0700 | [diff] [blame] | 50 | if (is_add == 0) |
| 51 | { |
Chaoyu Jin | 913b873 | 2017-08-08 13:36:23 -0700 | [diff] [blame] | 52 | /* free policer config and template */ |
Matus Fabian | 65fcd4d | 2016-05-13 05:44:48 -0700 | [diff] [blame] | 53 | if (p == 0) |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 54 | { |
| 55 | vec_free (name); |
| 56 | return clib_error_return (0, "No such policer configuration"); |
| 57 | } |
Chaoyu Jin | 913b873 | 2017-08-08 13:36:23 -0700 | [diff] [blame] | 58 | pool_put_index (pm->configs, p[0]); |
| 59 | pool_put_index (pm->policer_templates, p[0]); |
Matus Fabian | 65fcd4d | 2016-05-13 05:44:48 -0700 | [diff] [blame] | 60 | hash_unset_mem (pm->policer_config_by_name, name); |
Chaoyu Jin | 913b873 | 2017-08-08 13:36:23 -0700 | [diff] [blame] | 61 | |
| 62 | /* free policer */ |
| 63 | p = hash_get_mem (pm->policer_index_by_name, name); |
| 64 | if (p == 0) |
| 65 | { |
| 66 | vec_free (name); |
| 67 | return clib_error_return (0, "No such policer"); |
| 68 | } |
| 69 | pool_put_index (pm->policers, p[0]); |
Matus Fabian | 70e6a8d | 2016-06-20 08:10:42 -0700 | [diff] [blame] | 70 | hash_unset_mem (pm->policer_index_by_name, name); |
Chaoyu Jin | 913b873 | 2017-08-08 13:36:23 -0700 | [diff] [blame] | 71 | |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 72 | vec_free (name); |
Matus Fabian | 65fcd4d | 2016-05-13 05:44:48 -0700 | [diff] [blame] | 73 | return 0; |
| 74 | } |
| 75 | |
Matus Fabian | 70e6a8d | 2016-06-20 08:10:42 -0700 | [diff] [blame] | 76 | if (p != 0) |
| 77 | { |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 78 | vec_free (name); |
Matus Fabian | 70e6a8d | 2016-06-20 08:10:42 -0700 | [diff] [blame] | 79 | return clib_error_return (0, "Policer already exists"); |
| 80 | } |
| 81 | |
Matus Fabian | 65fcd4d | 2016-05-13 05:44:48 -0700 | [diff] [blame] | 82 | /* Vet the configuration before adding it to the table */ |
| 83 | rv = sse2_pol_logical_2_physical (cfg, &test_policer); |
| 84 | |
| 85 | if (rv == 0) |
| 86 | { |
| 87 | policer_read_response_type_st *pp; |
| 88 | sse2_qos_pol_cfg_params_st *cp; |
| 89 | |
| 90 | pool_get (pm->configs, cp); |
| 91 | pool_get (pm->policer_templates, pp); |
| 92 | |
| 93 | ASSERT (cp - pm->configs == pp - pm->policer_templates); |
| 94 | |
| 95 | clib_memcpy (cp, cfg, sizeof (*cp)); |
| 96 | clib_memcpy (pp, &test_policer, sizeof (*pp)); |
| 97 | |
| 98 | hash_set_mem (pm->policer_config_by_name, name, cp - pm->configs); |
Matus Fabian | 70e6a8d | 2016-06-20 08:10:42 -0700 | [diff] [blame] | 99 | pool_get_aligned (pm->policers, policer, CLIB_CACHE_LINE_BYTES); |
| 100 | policer[0] = pp[0]; |
| 101 | pi = policer - pm->policers; |
| 102 | hash_set_mem (pm->policer_index_by_name, name, pi); |
| 103 | *policer_index = pi; |
Brian Russell | 89cd744 | 2021-01-19 16:46:08 +0000 | [diff] [blame] | 104 | policer->thread_index = ~0; |
Matus Fabian | 65fcd4d | 2016-05-13 05:44:48 -0700 | [diff] [blame] | 105 | } |
| 106 | else |
| 107 | { |
| 108 | vec_free (name); |
| 109 | return clib_error_return (0, "Config failed sanity check"); |
| 110 | } |
| 111 | |
| 112 | return 0; |
| 113 | } |
| 114 | |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 115 | u8 * |
| 116 | format_policer_instance (u8 * s, va_list * va) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 117 | { |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 118 | policer_read_response_type_st *i |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 119 | = va_arg (*va, policer_read_response_type_st *); |
| 120 | |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 121 | s = format (s, "policer at %llx: %s rate, %s color-aware\n", |
| 122 | i, i->single_rate ? "single" : "dual", |
| 123 | i->color_aware ? "is" : "not"); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 124 | 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] | 125 | i->cir_tokens_per_period, i->pir_tokens_per_period, i->scale); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 126 | 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] | 127 | i->current_limit, |
| 128 | i->current_bucket, i->extended_limit, i->extended_bucket); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 129 | s = format (s, "last update %llu\n", i->last_update_time); |
| 130 | return s; |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 131 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 132 | |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 133 | static u8 * |
| 134 | format_policer_round_type (u8 * s, va_list * va) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 135 | { |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 136 | sse2_qos_pol_cfg_params_st *c = va_arg (*va, sse2_qos_pol_cfg_params_st *); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 137 | |
| 138 | if (c->rnd_type == SSE2_QOS_ROUND_TO_CLOSEST) |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 139 | s = format (s, "closest"); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 140 | else if (c->rnd_type == SSE2_QOS_ROUND_TO_UP) |
| 141 | s = format (s, "up"); |
| 142 | else if (c->rnd_type == SSE2_QOS_ROUND_TO_DOWN) |
| 143 | s = format (s, "down"); |
| 144 | else |
| 145 | s = format (s, "ILLEGAL"); |
| 146 | return s; |
| 147 | } |
| 148 | |
| 149 | |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 150 | static u8 * |
| 151 | format_policer_rate_type (u8 * s, va_list * va) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 152 | { |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 153 | sse2_qos_pol_cfg_params_st *c = va_arg (*va, sse2_qos_pol_cfg_params_st *); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 154 | |
| 155 | if (c->rate_type == SSE2_QOS_RATE_KBPS) |
| 156 | s = format (s, "kbps"); |
| 157 | else if (c->rate_type == SSE2_QOS_RATE_PPS) |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 158 | s = format (s, "pps"); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 159 | else |
| 160 | s = format (s, "ILLEGAL"); |
| 161 | return s; |
| 162 | } |
| 163 | |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 164 | static u8 * |
| 165 | format_policer_type (u8 * s, va_list * va) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 166 | { |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 167 | sse2_qos_pol_cfg_params_st *c = va_arg (*va, sse2_qos_pol_cfg_params_st *); |
| 168 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 169 | if (c->rfc == SSE2_QOS_POLICER_TYPE_1R2C) |
| 170 | s = format (s, "1r2c"); |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 171 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 172 | else if (c->rfc == SSE2_QOS_POLICER_TYPE_1R3C_RFC_2697) |
| 173 | s = format (s, "1r3c"); |
| 174 | |
| 175 | else if (c->rfc == SSE2_QOS_POLICER_TYPE_2R3C_RFC_2698) |
| 176 | s = format (s, "2r3c-2698"); |
| 177 | |
| 178 | else if (c->rfc == SSE2_QOS_POLICER_TYPE_2R3C_RFC_4115) |
| 179 | s = format (s, "2r3c-4115"); |
| 180 | |
| 181 | else if (c->rfc == SSE2_QOS_POLICER_TYPE_2R3C_RFC_MEF5CF1) |
| 182 | s = format (s, "2r3c-mef5cf1"); |
| 183 | else |
| 184 | s = format (s, "ILLEGAL"); |
| 185 | return s; |
| 186 | } |
| 187 | |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 188 | static u8 * |
| 189 | format_dscp (u8 * s, va_list * va) |
Matus Fabian | 4ac74c9 | 2016-05-31 07:33:29 -0700 | [diff] [blame] | 190 | { |
| 191 | u32 i = va_arg (*va, u32); |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 192 | char *t = 0; |
Matus Fabian | 4ac74c9 | 2016-05-31 07:33:29 -0700 | [diff] [blame] | 193 | |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 194 | switch (i) |
| 195 | { |
| 196 | #define _(v,f,str) case VNET_DSCP_##f: t = str; break; |
| 197 | foreach_vnet_dscp |
| 198 | #undef _ |
Matus Fabian | 4ac74c9 | 2016-05-31 07:33:29 -0700 | [diff] [blame] | 199 | default: |
| 200 | return format (s, "ILLEGAL"); |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 201 | } |
Matus Fabian | 4ac74c9 | 2016-05-31 07:33:29 -0700 | [diff] [blame] | 202 | s = format (s, "%s", t); |
| 203 | return s; |
| 204 | } |
| 205 | |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 206 | static u8 * |
| 207 | format_policer_action_type (u8 * s, va_list * va) |
Matus Fabian | 4ac74c9 | 2016-05-31 07:33:29 -0700 | [diff] [blame] | 208 | { |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 209 | sse2_qos_pol_action_params_st *a |
Matus Fabian | 4ac74c9 | 2016-05-31 07:33:29 -0700 | [diff] [blame] | 210 | = va_arg (*va, sse2_qos_pol_action_params_st *); |
| 211 | |
| 212 | if (a->action_type == SSE2_QOS_ACTION_DROP) |
| 213 | s = format (s, "drop"); |
| 214 | else if (a->action_type == SSE2_QOS_ACTION_TRANSMIT) |
| 215 | s = format (s, "transmit"); |
| 216 | else if (a->action_type == SSE2_QOS_ACTION_MARK_AND_TRANSMIT) |
| 217 | s = format (s, "mark-and-transmit %U", format_dscp, a->dscp); |
| 218 | else |
| 219 | s = format (s, "ILLEGAL"); |
| 220 | return s; |
| 221 | } |
| 222 | |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 223 | u8 * |
| 224 | format_policer_config (u8 * s, va_list * va) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 225 | { |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 226 | sse2_qos_pol_cfg_params_st *c = va_arg (*va, sse2_qos_pol_cfg_params_st *); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 227 | |
| 228 | 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] | 229 | format_policer_type, c, |
| 230 | c->rb.kbps.cir_kbps, |
| 231 | 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] | 232 | s = format (s, "rate type %U, round type %U\n", |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 233 | format_policer_rate_type, c, format_policer_round_type, c); |
Matus Fabian | 4ac74c9 | 2016-05-31 07:33:29 -0700 | [diff] [blame] | 234 | 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] | 235 | format_policer_action_type, &c->conform_action, |
| 236 | format_policer_action_type, &c->exceed_action, |
| 237 | format_policer_action_type, &c->violate_action); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 238 | return s; |
| 239 | } |
| 240 | |
| 241 | static uword |
| 242 | unformat_policer_type (unformat_input_t * input, va_list * va) |
| 243 | { |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 244 | sse2_qos_pol_cfg_params_st *c = va_arg (*va, sse2_qos_pol_cfg_params_st *); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 245 | |
| 246 | if (!unformat (input, "type")) |
| 247 | return 0; |
| 248 | |
| 249 | if (unformat (input, "1r2c")) |
| 250 | c->rfc = SSE2_QOS_POLICER_TYPE_1R2C; |
| 251 | else if (unformat (input, "1r3c")) |
| 252 | c->rfc = SSE2_QOS_POLICER_TYPE_1R3C_RFC_2697; |
| 253 | else if (unformat (input, "2r3c-2698")) |
| 254 | c->rfc = SSE2_QOS_POLICER_TYPE_2R3C_RFC_2698; |
| 255 | else if (unformat (input, "2r3c-4115")) |
| 256 | c->rfc = SSE2_QOS_POLICER_TYPE_2R3C_RFC_4115; |
| 257 | else if (unformat (input, "2r3c-mef5cf1")) |
| 258 | c->rfc = SSE2_QOS_POLICER_TYPE_2R3C_RFC_MEF5CF1; |
| 259 | else |
| 260 | return 0; |
| 261 | return 1; |
| 262 | } |
| 263 | |
| 264 | static uword |
| 265 | unformat_policer_round_type (unformat_input_t * input, va_list * va) |
| 266 | { |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 267 | sse2_qos_pol_cfg_params_st *c = va_arg (*va, sse2_qos_pol_cfg_params_st *); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 268 | |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 269 | if (!unformat (input, "round")) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 270 | return 0; |
| 271 | |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 272 | if (unformat (input, "closest")) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 273 | c->rnd_type = SSE2_QOS_ROUND_TO_CLOSEST; |
| 274 | else if (unformat (input, "up")) |
| 275 | c->rnd_type = SSE2_QOS_ROUND_TO_UP; |
| 276 | else if (unformat (input, "down")) |
| 277 | c->rnd_type = SSE2_QOS_ROUND_TO_DOWN; |
| 278 | else |
| 279 | return 0; |
| 280 | return 1; |
| 281 | } |
| 282 | |
| 283 | static uword |
| 284 | unformat_policer_rate_type (unformat_input_t * input, va_list * va) |
| 285 | { |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 286 | sse2_qos_pol_cfg_params_st *c = va_arg (*va, sse2_qos_pol_cfg_params_st *); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 287 | |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 288 | if (!unformat (input, "rate")) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 289 | return 0; |
| 290 | |
| 291 | if (unformat (input, "kbps")) |
| 292 | c->rate_type = SSE2_QOS_RATE_KBPS; |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 293 | else if (unformat (input, "pps")) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 294 | c->rate_type = SSE2_QOS_RATE_PPS; |
| 295 | else |
| 296 | return 0; |
| 297 | return 1; |
| 298 | } |
| 299 | |
| 300 | static uword |
| 301 | unformat_policer_cir (unformat_input_t * input, va_list * va) |
| 302 | { |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 303 | sse2_qos_pol_cfg_params_st *c = va_arg (*va, sse2_qos_pol_cfg_params_st *); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 304 | |
| 305 | if (unformat (input, "cir %u", &c->rb.kbps.cir_kbps)) |
| 306 | return 1; |
| 307 | return 0; |
| 308 | } |
| 309 | |
| 310 | static uword |
| 311 | unformat_policer_eir (unformat_input_t * input, va_list * va) |
| 312 | { |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 313 | sse2_qos_pol_cfg_params_st *c = va_arg (*va, sse2_qos_pol_cfg_params_st *); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 314 | |
| 315 | if (unformat (input, "eir %u", &c->rb.kbps.eir_kbps)) |
| 316 | return 1; |
| 317 | return 0; |
| 318 | } |
| 319 | |
| 320 | static uword |
| 321 | unformat_policer_cb (unformat_input_t * input, va_list * va) |
| 322 | { |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 323 | sse2_qos_pol_cfg_params_st *c = va_arg (*va, sse2_qos_pol_cfg_params_st *); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 324 | |
| 325 | if (unformat (input, "cb %u", &c->rb.kbps.cb_bytes)) |
| 326 | return 1; |
| 327 | return 0; |
| 328 | } |
| 329 | |
| 330 | static uword |
| 331 | unformat_policer_eb (unformat_input_t * input, va_list * va) |
| 332 | { |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 333 | sse2_qos_pol_cfg_params_st *c = va_arg (*va, sse2_qos_pol_cfg_params_st *); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 334 | |
| 335 | if (unformat (input, "eb %u", &c->rb.kbps.eb_bytes)) |
| 336 | return 1; |
| 337 | return 0; |
| 338 | } |
| 339 | |
Matus Fabian | 4ac74c9 | 2016-05-31 07:33:29 -0700 | [diff] [blame] | 340 | static uword |
| 341 | unformat_dscp (unformat_input_t * input, va_list * va) |
| 342 | { |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 343 | u8 *r = va_arg (*va, u8 *); |
Matus Fabian | 4ac74c9 | 2016-05-31 07:33:29 -0700 | [diff] [blame] | 344 | |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 345 | if (0); |
Matus Fabian | 4ac74c9 | 2016-05-31 07:33:29 -0700 | [diff] [blame] | 346 | #define _(v,f,str) else if (unformat (input, str)) *r = VNET_DSCP_##f; |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 347 | foreach_vnet_dscp |
Matus Fabian | 4ac74c9 | 2016-05-31 07:33:29 -0700 | [diff] [blame] | 348 | #undef _ |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 349 | else |
Matus Fabian | 4ac74c9 | 2016-05-31 07:33:29 -0700 | [diff] [blame] | 350 | return 0; |
| 351 | return 1; |
| 352 | } |
| 353 | |
| 354 | static uword |
| 355 | unformat_policer_action_type (unformat_input_t * input, va_list * va) |
| 356 | { |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 357 | sse2_qos_pol_action_params_st *a |
Matus Fabian | 4ac74c9 | 2016-05-31 07:33:29 -0700 | [diff] [blame] | 358 | = va_arg (*va, sse2_qos_pol_action_params_st *); |
| 359 | |
| 360 | if (unformat (input, "drop")) |
| 361 | a->action_type = SSE2_QOS_ACTION_DROP; |
| 362 | else if (unformat (input, "transmit")) |
| 363 | a->action_type = SSE2_QOS_ACTION_TRANSMIT; |
| 364 | else if (unformat (input, "mark-and-transmit %U", unformat_dscp, &a->dscp)) |
| 365 | a->action_type = SSE2_QOS_ACTION_MARK_AND_TRANSMIT; |
| 366 | else |
| 367 | return 0; |
| 368 | return 1; |
| 369 | } |
| 370 | |
| 371 | static uword |
| 372 | unformat_policer_action (unformat_input_t * input, va_list * va) |
| 373 | { |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 374 | sse2_qos_pol_cfg_params_st *c = va_arg (*va, sse2_qos_pol_cfg_params_st *); |
Matus Fabian | 4ac74c9 | 2016-05-31 07:33:29 -0700 | [diff] [blame] | 375 | |
| 376 | if (unformat (input, "conform-action %U", unformat_policer_action_type, |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 377 | &c->conform_action)) |
Matus Fabian | 4ac74c9 | 2016-05-31 07:33:29 -0700 | [diff] [blame] | 378 | return 1; |
| 379 | else if (unformat (input, "exceed-action %U", unformat_policer_action_type, |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 380 | &c->exceed_action)) |
Matus Fabian | 4ac74c9 | 2016-05-31 07:33:29 -0700 | [diff] [blame] | 381 | return 1; |
| 382 | else if (unformat (input, "violate-action %U", unformat_policer_action_type, |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 383 | &c->violate_action)) |
Matus Fabian | 4ac74c9 | 2016-05-31 07:33:29 -0700 | [diff] [blame] | 384 | return 1; |
| 385 | return 0; |
| 386 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 387 | |
Matus Fabian | 70e6a8d | 2016-06-20 08:10:42 -0700 | [diff] [blame] | 388 | static uword |
| 389 | unformat_policer_classify_next_index (unformat_input_t * input, va_list * va) |
| 390 | { |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 391 | u32 *r = va_arg (*va, u32 *); |
Matus Fabian | 70e6a8d | 2016-06-20 08:10:42 -0700 | [diff] [blame] | 392 | vnet_policer_main_t *pm = &vnet_policer_main; |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 393 | uword *p; |
| 394 | u8 *match_name = 0; |
Matus Fabian | 70e6a8d | 2016-06-20 08:10:42 -0700 | [diff] [blame] | 395 | |
| 396 | if (unformat (input, "%s", &match_name)) |
| 397 | ; |
| 398 | else |
| 399 | return 0; |
| 400 | |
| 401 | p = hash_get_mem (pm->policer_index_by_name, match_name); |
| 402 | |
| 403 | if (p == 0) |
| 404 | return 0; |
| 405 | |
| 406 | *r = p[0]; |
| 407 | |
| 408 | return 1; |
| 409 | } |
| 410 | |
| 411 | static uword |
| 412 | unformat_policer_classify_precolor (unformat_input_t * input, va_list * va) |
| 413 | { |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 414 | u32 *r = va_arg (*va, u32 *); |
Matus Fabian | 70e6a8d | 2016-06-20 08:10:42 -0700 | [diff] [blame] | 415 | |
| 416 | if (unformat (input, "conform-color")) |
| 417 | *r = POLICE_CONFORM; |
| 418 | else if (unformat (input, "exceed-color")) |
| 419 | *r = POLICE_EXCEED; |
| 420 | else |
| 421 | return 0; |
| 422 | |
| 423 | return 1; |
| 424 | } |
| 425 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 426 | #define foreach_config_param \ |
| 427 | _(eb) \ |
| 428 | _(cb) \ |
| 429 | _(eir) \ |
| 430 | _(cir) \ |
| 431 | _(rate_type) \ |
| 432 | _(round_type) \ |
Matus Fabian | 4ac74c9 | 2016-05-31 07:33:29 -0700 | [diff] [blame] | 433 | _(type) \ |
| 434 | _(action) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 435 | |
| 436 | static clib_error_t * |
| 437 | configure_policer_command_fn (vlib_main_t * vm, |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 438 | unformat_input_t * input, |
| 439 | vlib_cli_command_t * cmd) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 440 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 441 | sse2_qos_pol_cfg_params_st c; |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 442 | unformat_input_t _line_input, *line_input = &_line_input; |
Matus Fabian | 65fcd4d | 2016-05-13 05:44:48 -0700 | [diff] [blame] | 443 | u8 is_add = 1; |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 444 | u8 *name = 0; |
Matus Fabian | 70e6a8d | 2016-06-20 08:10:42 -0700 | [diff] [blame] | 445 | u32 pi; |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 446 | clib_error_t *error = NULL; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 447 | |
| 448 | /* Get a line of input. */ |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 449 | if (!unformat_user (input, unformat_line_input, line_input)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 450 | return 0; |
| 451 | |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 452 | clib_memset (&c, 0, sizeof (c)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 453 | |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 454 | while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 455 | { |
| 456 | if (unformat (line_input, "del")) |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 457 | is_add = 0; |
| 458 | else if (unformat (line_input, "name %s", &name)) |
| 459 | ; |
| 460 | else if (unformat (line_input, "color-aware")) |
| 461 | c.color_aware = 1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 462 | |
| 463 | #define _(a) else if (unformat (line_input, "%U", unformat_policer_##a, &c)) ; |
| 464 | foreach_config_param |
| 465 | #undef _ |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 466 | else |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 467 | { |
| 468 | error = clib_error_return (0, "unknown input `%U'", |
| 469 | format_unformat_error, line_input); |
| 470 | goto done; |
| 471 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 472 | } |
| 473 | |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 474 | error = policer_add_del (vm, name, &c, &pi, is_add); |
| 475 | |
| 476 | done: |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 477 | unformat_free (line_input); |
| 478 | |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 479 | return error; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 480 | } |
| 481 | |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 482 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 483 | VLIB_CLI_COMMAND (configure_policer_command, static) = { |
| 484 | .path = "configure policer", |
| 485 | .short_help = "configure policer name <name> <params> ", |
| 486 | .function = configure_policer_command_fn, |
| 487 | }; |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 488 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 489 | |
| 490 | static clib_error_t * |
| 491 | show_policer_command_fn (vlib_main_t * vm, |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 492 | unformat_input_t * input, vlib_cli_command_t * cmd) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 493 | { |
| 494 | vnet_policer_main_t *pm = &vnet_policer_main; |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 495 | hash_pair_t *p; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 496 | u32 pool_index; |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 497 | u8 *match_name = 0; |
| 498 | u8 *name; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 499 | sse2_qos_pol_cfg_params_st *config; |
| 500 | policer_read_response_type_st *templ; |
| 501 | |
| 502 | (void) unformat (input, "name %s", &match_name); |
| 503 | |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 504 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 505 | hash_foreach_pair (p, pm->policer_config_by_name, |
| 506 | ({ |
| 507 | name = (u8 *) p->key; |
| 508 | if (match_name == 0 || !strcmp((char *) name, (char *) match_name)) |
| 509 | { |
| 510 | pool_index = p->value[0]; |
| 511 | config = pool_elt_at_index (pm->configs, pool_index); |
| 512 | templ = pool_elt_at_index (pm->policer_templates, pool_index); |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 513 | vlib_cli_output (vm, "Name \"%s\" %U ", |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 514 | name, format_policer_config, config); |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 515 | vlib_cli_output (vm, "Template %U", |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 516 | format_policer_instance, templ); |
| 517 | vlib_cli_output (vm, "-----------"); |
| 518 | } |
| 519 | })); |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 520 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 521 | return 0; |
| 522 | } |
| 523 | |
| 524 | |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 525 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 526 | VLIB_CLI_COMMAND (show_policer_command, static) = { |
| 527 | .path = "show policer", |
| 528 | .short_help = "show policer [name]", |
| 529 | .function = show_policer_command_fn, |
| 530 | }; |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 531 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 532 | |
Chaoyu Jin | 913b873 | 2017-08-08 13:36:23 -0700 | [diff] [blame] | 533 | static clib_error_t * |
| 534 | show_policer_pools_command_fn (vlib_main_t * vm, |
| 535 | unformat_input_t * input, |
| 536 | vlib_cli_command_t * cmd) |
| 537 | { |
| 538 | vnet_policer_main_t *pm = &vnet_policer_main; |
| 539 | |
| 540 | vlib_cli_output (vm, "pool sizes: configs=%d templates=%d policers=%d", |
| 541 | pool_elts (pm->configs), |
| 542 | pool_elts (pm->policer_templates), |
| 543 | pool_elts (pm->policers)); |
| 544 | return 0; |
| 545 | } |
| 546 | /* *INDENT-OFF* */ |
| 547 | VLIB_CLI_COMMAND (show_policer_pools_command, static) = { |
| 548 | .path = "show policer pools", |
| 549 | .short_help = "show policer pools", |
| 550 | .function = show_policer_pools_command_fn, |
| 551 | }; |
| 552 | /* *INDENT-ON* */ |
| 553 | |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 554 | clib_error_t * |
| 555 | policer_init (vlib_main_t * vm) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 556 | { |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 557 | vnet_policer_main_t *pm = &vnet_policer_main; |
| 558 | void vnet_policer_node_funcs_reference (void); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 559 | |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 560 | vnet_policer_node_funcs_reference (); |
| 561 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 562 | pm->vlib_main = vm; |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 563 | pm->vnet_main = vnet_get_main (); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 564 | |
| 565 | pm->policer_config_by_name = hash_create_string (0, sizeof (uword)); |
Matus Fabian | 70e6a8d | 2016-06-20 08:10:42 -0700 | [diff] [blame] | 566 | pm->policer_index_by_name = hash_create_string (0, sizeof (uword)); |
| 567 | |
| 568 | vnet_classify_register_unformat_policer_next_index_fn |
| 569 | (unformat_policer_classify_next_index); |
| 570 | vnet_classify_register_unformat_opaque_index_fn |
| 571 | (unformat_policer_classify_precolor); |
| 572 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 573 | return 0; |
| 574 | } |
| 575 | |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 576 | VLIB_INIT_FUNCTION (policer_init); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 577 | |
| 578 | |
Damjan Marion | 3891cd8 | 2016-10-27 10:27:00 +0200 | [diff] [blame] | 579 | |
| 580 | /* |
| 581 | * fd.io coding-style-patch-verification: ON |
| 582 | * |
| 583 | * Local Variables: |
| 584 | * eval: (c-set-style "gnu") |
| 585 | * End: |
| 586 | */ |