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