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