Matus Fabian | 70e6a8d | 2016-06-20 08:10:42 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2016 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 | */ |
| 15 | #include <vnet/classify/policer_classify.h> |
| 16 | |
Dave Wallace | 71612d6 | 2017-10-24 01:32:41 -0400 | [diff] [blame] | 17 | policer_classify_main_t policer_classify_main; |
| 18 | |
Matus Fabian | 70e6a8d | 2016-06-20 08:10:42 -0700 | [diff] [blame] | 19 | static void |
| 20 | vnet_policer_classify_feature_enable (vlib_main_t * vnm, |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 21 | policer_classify_main_t * pcm, |
| 22 | u32 sw_if_index, |
| 23 | policer_classify_table_id_t tid, |
| 24 | int feature_enable) |
Matus Fabian | 70e6a8d | 2016-06-20 08:10:42 -0700 | [diff] [blame] | 25 | { |
| 26 | if (tid == POLICER_CLASSIFY_TABLE_L2) |
| 27 | { |
| 28 | l2input_intf_bitmap_enable (sw_if_index, L2INPUT_FEAT_POLICER_CLAS, |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 29 | feature_enable); |
Matus Fabian | 70e6a8d | 2016-06-20 08:10:42 -0700 | [diff] [blame] | 30 | } |
| 31 | else |
| 32 | { |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 33 | vnet_feature_config_main_t *fcm; |
Damjan Marion | 8b3191e | 2016-11-09 19:54:20 +0100 | [diff] [blame] | 34 | u8 arc; |
Matus Fabian | 70e6a8d | 2016-06-20 08:10:42 -0700 | [diff] [blame] | 35 | |
| 36 | if (tid == POLICER_CLASSIFY_TABLE_IP4) |
Damjan Marion | 8b3191e | 2016-11-09 19:54:20 +0100 | [diff] [blame] | 37 | { |
| 38 | vnet_feature_enable_disable ("ip4-unicast", "ip4-policer-classify", |
| 39 | sw_if_index, feature_enable, 0, 0); |
| 40 | arc = vnet_get_feature_arc_index ("ip4-unicast"); |
| 41 | } |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 42 | |
Matus Fabian | 70e6a8d | 2016-06-20 08:10:42 -0700 | [diff] [blame] | 43 | else |
Damjan Marion | 8b3191e | 2016-11-09 19:54:20 +0100 | [diff] [blame] | 44 | { |
| 45 | vnet_feature_enable_disable ("ip6-unicast", "ip6-policer-classify", |
| 46 | sw_if_index, feature_enable, 0, 0); |
| 47 | arc = vnet_get_feature_arc_index ("ip6-unicast"); |
| 48 | } |
Matus Fabian | 70e6a8d | 2016-06-20 08:10:42 -0700 | [diff] [blame] | 49 | |
Damjan Marion | 8b3191e | 2016-11-09 19:54:20 +0100 | [diff] [blame] | 50 | fcm = vnet_get_feature_arc_config_main (arc); |
| 51 | pcm->vnet_config_main[tid] = &fcm->config_main; |
Matus Fabian | 70e6a8d | 2016-06-20 08:10:42 -0700 | [diff] [blame] | 52 | } |
| 53 | } |
| 54 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 55 | int |
| 56 | vnet_set_policer_classify_intfc (vlib_main_t * vm, u32 sw_if_index, |
| 57 | u32 ip4_table_index, u32 ip6_table_index, |
| 58 | u32 l2_table_index, u32 is_add) |
Matus Fabian | 70e6a8d | 2016-06-20 08:10:42 -0700 | [diff] [blame] | 59 | { |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 60 | policer_classify_main_t *pcm = &policer_classify_main; |
| 61 | vnet_classify_main_t *vcm = pcm->vnet_classify_main; |
| 62 | u32 pct[POLICER_CLASSIFY_N_TABLES] = { ip4_table_index, ip6_table_index, |
| 63 | l2_table_index |
| 64 | }; |
Matus Fabian | 70e6a8d | 2016-06-20 08:10:42 -0700 | [diff] [blame] | 65 | u32 ti; |
| 66 | |
| 67 | /* Assume that we've validated sw_if_index in the API layer */ |
| 68 | |
| 69 | for (ti = 0; ti < POLICER_CLASSIFY_N_TABLES; ti++) |
| 70 | { |
| 71 | if (pct[ti] == ~0) |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 72 | continue; |
Matus Fabian | 70e6a8d | 2016-06-20 08:10:42 -0700 | [diff] [blame] | 73 | |
| 74 | if (pool_is_free_index (vcm->tables, pct[ti])) |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 75 | return VNET_API_ERROR_NO_SUCH_TABLE; |
Matus Fabian | 70e6a8d | 2016-06-20 08:10:42 -0700 | [diff] [blame] | 76 | |
| 77 | vec_validate_init_empty |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 78 | (pcm->classify_table_index_by_sw_if_index[ti], sw_if_index, ~0); |
Matus Fabian | 70e6a8d | 2016-06-20 08:10:42 -0700 | [diff] [blame] | 79 | |
| 80 | /* Reject any DEL operation with wrong sw_if_index */ |
| 81 | if (!is_add && |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 82 | (pct[ti] != |
| 83 | pcm->classify_table_index_by_sw_if_index[ti][sw_if_index])) |
| 84 | { |
| 85 | clib_warning |
| 86 | ("Non-existent intf_idx=%d with table_index=%d for delete", |
| 87 | sw_if_index, pct[ti]); |
| 88 | return VNET_API_ERROR_NO_SUCH_TABLE; |
| 89 | } |
Matus Fabian | 70e6a8d | 2016-06-20 08:10:42 -0700 | [diff] [blame] | 90 | |
| 91 | /* Return ok on ADD operaton if feature is already enabled */ |
| 92 | if (is_add && |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 93 | pcm->classify_table_index_by_sw_if_index[ti][sw_if_index] != ~0) |
| 94 | return 0; |
Matus Fabian | 70e6a8d | 2016-06-20 08:10:42 -0700 | [diff] [blame] | 95 | |
| 96 | vnet_policer_classify_feature_enable (vm, pcm, sw_if_index, ti, is_add); |
| 97 | |
| 98 | if (is_add) |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 99 | pcm->classify_table_index_by_sw_if_index[ti][sw_if_index] = pct[ti]; |
Matus Fabian | 70e6a8d | 2016-06-20 08:10:42 -0700 | [diff] [blame] | 100 | else |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 101 | pcm->classify_table_index_by_sw_if_index[ti][sw_if_index] = ~0; |
Matus Fabian | 70e6a8d | 2016-06-20 08:10:42 -0700 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | |
| 105 | return 0; |
| 106 | } |
| 107 | |
| 108 | static clib_error_t * |
| 109 | set_policer_classify_command_fn (vlib_main_t * vm, |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 110 | unformat_input_t * input, |
| 111 | vlib_cli_command_t * cmd) |
Matus Fabian | 70e6a8d | 2016-06-20 08:10:42 -0700 | [diff] [blame] | 112 | { |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 113 | vnet_main_t *vnm = vnet_get_main (); |
Matus Fabian | 70e6a8d | 2016-06-20 08:10:42 -0700 | [diff] [blame] | 114 | u32 sw_if_index = ~0; |
| 115 | u32 ip4_table_index = ~0; |
| 116 | u32 ip6_table_index = ~0; |
| 117 | u32 l2_table_index = ~0; |
| 118 | u32 is_add = 1; |
| 119 | u32 idx_cnt = 0; |
| 120 | int rv; |
| 121 | |
| 122 | while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) |
| 123 | { |
| 124 | if (unformat (input, "interface %U", unformat_vnet_sw_interface, |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 125 | vnm, &sw_if_index)) |
| 126 | ; |
Matus Fabian | 70e6a8d | 2016-06-20 08:10:42 -0700 | [diff] [blame] | 127 | else if (unformat (input, "ip4-table %d", &ip4_table_index)) |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 128 | idx_cnt++; |
Matus Fabian | 70e6a8d | 2016-06-20 08:10:42 -0700 | [diff] [blame] | 129 | else if (unformat (input, "ip6-table %d", &ip6_table_index)) |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 130 | idx_cnt++; |
Matus Fabian | 70e6a8d | 2016-06-20 08:10:42 -0700 | [diff] [blame] | 131 | else if (unformat (input, "l2-table %d", &l2_table_index)) |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 132 | idx_cnt++; |
Matus Fabian | 70e6a8d | 2016-06-20 08:10:42 -0700 | [diff] [blame] | 133 | else if (unformat (input, "del")) |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 134 | is_add = 0; |
Matus Fabian | 70e6a8d | 2016-06-20 08:10:42 -0700 | [diff] [blame] | 135 | else |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 136 | break; |
Matus Fabian | 70e6a8d | 2016-06-20 08:10:42 -0700 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | if (sw_if_index == ~0) |
| 140 | return clib_error_return (0, "Interface must be specified."); |
| 141 | |
| 142 | if (!idx_cnt) |
| 143 | return clib_error_return (0, "Table index should be specified."); |
| 144 | |
| 145 | if (idx_cnt > 1) |
| 146 | return clib_error_return (0, "Only one table index per API is allowed."); |
| 147 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 148 | rv = vnet_set_policer_classify_intfc (vm, sw_if_index, ip4_table_index, |
| 149 | ip6_table_index, l2_table_index, |
| 150 | is_add); |
Matus Fabian | 70e6a8d | 2016-06-20 08:10:42 -0700 | [diff] [blame] | 151 | |
| 152 | switch (rv) |
| 153 | { |
| 154 | case 0: |
| 155 | break; |
| 156 | |
| 157 | case VNET_API_ERROR_NO_MATCHING_INTERFACE: |
| 158 | return clib_error_return (0, "No such interface"); |
| 159 | |
| 160 | case VNET_API_ERROR_NO_SUCH_ENTRY: |
| 161 | return clib_error_return (0, "No such classifier table"); |
| 162 | } |
| 163 | return 0; |
| 164 | } |
| 165 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 166 | /* *INDENT-OFF* */ |
Chris Luke | d4024f5 | 2016-09-06 09:32:36 -0400 | [diff] [blame] | 167 | VLIB_CLI_COMMAND (set_policer_classify_command, static) = { |
Matus Fabian | 70e6a8d | 2016-06-20 08:10:42 -0700 | [diff] [blame] | 168 | .path = "set policer classify", |
| 169 | .short_help = |
| 170 | "set policer classify interface <int> [ip4-table <index>]\n" |
| 171 | " [ip6-table <index>] [l2-table <index>] [del]", |
| 172 | .function = set_policer_classify_command_fn, |
| 173 | }; |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 174 | /* *INDENT-ON* */ |
Matus Fabian | 70e6a8d | 2016-06-20 08:10:42 -0700 | [diff] [blame] | 175 | |
| 176 | static uword |
| 177 | unformat_table_type (unformat_input_t * input, va_list * va) |
| 178 | { |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 179 | u32 *r = va_arg (*va, u32 *); |
Matus Fabian | 70e6a8d | 2016-06-20 08:10:42 -0700 | [diff] [blame] | 180 | u32 tid; |
| 181 | |
| 182 | if (unformat (input, "ip4")) |
| 183 | tid = POLICER_CLASSIFY_TABLE_IP4; |
| 184 | else if (unformat (input, "ip6")) |
| 185 | tid = POLICER_CLASSIFY_TABLE_IP6; |
| 186 | else if (unformat (input, "l2")) |
| 187 | tid = POLICER_CLASSIFY_TABLE_L2; |
| 188 | else |
| 189 | return 0; |
| 190 | |
| 191 | *r = tid; |
| 192 | return 1; |
| 193 | } |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 194 | |
Matus Fabian | 70e6a8d | 2016-06-20 08:10:42 -0700 | [diff] [blame] | 195 | static clib_error_t * |
| 196 | show_policer_classify_command_fn (vlib_main_t * vm, |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 197 | unformat_input_t * input, |
| 198 | vlib_cli_command_t * cmd) |
Matus Fabian | 70e6a8d | 2016-06-20 08:10:42 -0700 | [diff] [blame] | 199 | { |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 200 | policer_classify_main_t *pcm = &policer_classify_main; |
Matus Fabian | 70e6a8d | 2016-06-20 08:10:42 -0700 | [diff] [blame] | 201 | u32 type = POLICER_CLASSIFY_N_TABLES; |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 202 | u32 *vec_tbl; |
Matus Fabian | 70e6a8d | 2016-06-20 08:10:42 -0700 | [diff] [blame] | 203 | int i; |
| 204 | |
| 205 | if (unformat (input, "type %U", unformat_table_type, &type)) |
| 206 | ; |
| 207 | else |
| 208 | return clib_error_return (0, "Type must be specified.");; |
| 209 | |
| 210 | if (type == POLICER_CLASSIFY_N_TABLES) |
| 211 | return clib_error_return (0, "Invalid table type."); |
| 212 | |
| 213 | vec_tbl = pcm->classify_table_index_by_sw_if_index[type]; |
| 214 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 215 | if (vec_len (vec_tbl)) |
| 216 | vlib_cli_output (vm, "%10s%20s\t\t%s", "Intfc idx", "Classify table", |
| 217 | "Interface name"); |
Matus Fabian | 70e6a8d | 2016-06-20 08:10:42 -0700 | [diff] [blame] | 218 | else |
| 219 | vlib_cli_output (vm, "No tables configured."); |
| 220 | |
| 221 | for (i = 0; i < vec_len (vec_tbl); i++) |
| 222 | { |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 223 | if (vec_elt (vec_tbl, i) == ~0) |
| 224 | continue; |
Matus Fabian | 70e6a8d | 2016-06-20 08:10:42 -0700 | [diff] [blame] | 225 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 226 | vlib_cli_output (vm, "%10d%20d\t\t%U", i, vec_elt (vec_tbl, i), |
| 227 | format_vnet_sw_if_index_name, pcm->vnet_main, i); |
Matus Fabian | 70e6a8d | 2016-06-20 08:10:42 -0700 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | return 0; |
| 231 | } |
| 232 | |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 233 | /* *INDENT-OFF* */ |
Matus Fabian | 70e6a8d | 2016-06-20 08:10:42 -0700 | [diff] [blame] | 234 | VLIB_CLI_COMMAND (show_policer_classify_command, static) = { |
| 235 | .path = "show classify policer", |
| 236 | .short_help = "show classify policer type [ip4|ip6|l2]", |
| 237 | .function = show_policer_classify_command_fn, |
| 238 | }; |
khemendra kumar | d7bfa0e | 2017-11-27 15:15:53 +0530 | [diff] [blame] | 239 | /* *INDENT-ON* */ |
| 240 | |
| 241 | /* |
| 242 | * fd.io coding-style-patch-verification: ON |
| 243 | * |
| 244 | * Local Variables: |
| 245 | * eval: (c-set-style "gnu") |
| 246 | * End: |
| 247 | */ |