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