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