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