Dave Barach | b84a3e5 | 2016-08-30 17:01:52 -0400 | [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 | */ |
| 15 | /* |
| 16 | * l2_classify.c |
| 17 | */ |
| 18 | |
| 19 | #include <vnet/l2/l2_classify.h> |
| 20 | #include <vnet/api_errno.h> |
| 21 | |
| 22 | /** |
| 23 | * @file |
Chris Luke | 16bcf7d | 2016-09-01 14:31:46 -0400 | [diff] [blame] | 24 | * @brief L2 input classifier. |
Dave Barach | b84a3e5 | 2016-08-30 17:01:52 -0400 | [diff] [blame] | 25 | * |
Chris Luke | 16bcf7d | 2016-09-01 14:31:46 -0400 | [diff] [blame] | 26 | * @sa @ref vnet/vnet/classify/vnet_classify.c |
| 27 | * @sa @ref vnet/vnet/classify/vnet_classify.h |
Dave Barach | b84a3e5 | 2016-08-30 17:01:52 -0400 | [diff] [blame] | 28 | */ |
| 29 | |
| 30 | /** |
Chris Luke | 16bcf7d | 2016-09-01 14:31:46 -0400 | [diff] [blame] | 31 | * @brief l2_input_classifier packet trace record. |
Dave Barach | b84a3e5 | 2016-08-30 17:01:52 -0400 | [diff] [blame] | 32 | */ |
| 33 | typedef struct |
| 34 | { |
| 35 | /** interface handle for the ith packet */ |
| 36 | u32 sw_if_index; |
| 37 | /** graph arc index selected for this packet */ |
| 38 | u32 next_index; |
| 39 | /** classifier table which provided the final result */ |
| 40 | u32 table_index; |
| 41 | /** offset in classifier heap of the corresponding session */ |
| 42 | u32 session_offset; |
| 43 | } l2_input_classify_trace_t; |
| 44 | |
| 45 | /** |
Chris Luke | 16bcf7d | 2016-09-01 14:31:46 -0400 | [diff] [blame] | 46 | * @brief vlib node runtime. |
Dave Barach | b84a3e5 | 2016-08-30 17:01:52 -0400 | [diff] [blame] | 47 | */ |
| 48 | typedef struct |
| 49 | { |
| 50 | /** use-case independent main object pointer */ |
| 51 | vnet_classify_main_t *vcm; |
| 52 | /** l2 input classifier main object pointer */ |
| 53 | l2_input_classify_main_t *l2cm; |
| 54 | } l2_input_classify_runtime_t; |
| 55 | |
Chris Luke | 16bcf7d | 2016-09-01 14:31:46 -0400 | [diff] [blame] | 56 | /** Packet trace format function. */ |
Dave Barach | b84a3e5 | 2016-08-30 17:01:52 -0400 | [diff] [blame] | 57 | static u8 * |
| 58 | format_l2_input_classify_trace (u8 * s, va_list * args) |
| 59 | { |
| 60 | CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *); |
| 61 | CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *); |
| 62 | l2_input_classify_trace_t *t = va_arg (*args, l2_input_classify_trace_t *); |
| 63 | |
| 64 | s = format (s, "l2-classify: sw_if_index %d, table %d, offset %x, next %d", |
| 65 | t->sw_if_index, t->table_index, t->session_offset, |
| 66 | t->next_index); |
| 67 | return s; |
| 68 | } |
| 69 | |
Chris Luke | 16bcf7d | 2016-09-01 14:31:46 -0400 | [diff] [blame] | 70 | /** l2 input classifier main data structure. */ |
Dave Barach | b84a3e5 | 2016-08-30 17:01:52 -0400 | [diff] [blame] | 71 | l2_input_classify_main_t l2_input_classify_main; |
| 72 | |
| 73 | vlib_node_registration_t l2_input_classify_node; |
| 74 | |
| 75 | #define foreach_l2_input_classify_error \ |
| 76 | _(MISS, "Classify misses") \ |
| 77 | _(HIT, "Classify hits") \ |
| 78 | _(CHAIN_HIT, "Classify hits after chain walk") \ |
| 79 | _(DROP, "L2 Classify Drops") |
| 80 | |
| 81 | typedef enum |
| 82 | { |
| 83 | #define _(sym,str) L2_INPUT_CLASSIFY_ERROR_##sym, |
| 84 | foreach_l2_input_classify_error |
| 85 | #undef _ |
| 86 | L2_INPUT_CLASSIFY_N_ERROR, |
| 87 | } l2_input_classify_error_t; |
| 88 | |
| 89 | static char *l2_input_classify_error_strings[] = { |
| 90 | #define _(sym,string) string, |
| 91 | foreach_l2_input_classify_error |
| 92 | #undef _ |
| 93 | }; |
| 94 | |
| 95 | /** |
| 96 | * @brief l2 input classifier node. |
| 97 | * @node l2-input-classify |
| 98 | * |
| 99 | * This is the l2 input classifier dispatch node |
| 100 | * |
| 101 | * @param vm vlib_main_t corresponding to the current thread. |
| 102 | * @param node vlib_node_runtime_t data for this node. |
| 103 | * @param frame vlib_frame_t whose contents should be dispatched. |
| 104 | * |
| 105 | * @par Graph mechanics: buffer metadata, next index usage |
| 106 | * |
| 107 | * @em Uses: |
| 108 | * - <code>(l2_input_classify_runtime_t *) |
| 109 | * rt->classify_table_index_by_sw_if_index</code> |
Chris Luke | 16bcf7d | 2016-09-01 14:31:46 -0400 | [diff] [blame] | 110 | * - Head of the per-interface, per-protocol classifier table chain |
| 111 | * for a specific interface. |
| 112 | * - @c ~0 => send pkts to the next feature in the L2 feature chain. |
Dave Barach | b84a3e5 | 2016-08-30 17:01:52 -0400 | [diff] [blame] | 113 | * - <code>vnet_buffer(b)->sw_if_index[VLIB_RX]</code> |
| 114 | * - Indicates the @c sw_if_index value of the interface that the |
Chris Luke | 16bcf7d | 2016-09-01 14:31:46 -0400 | [diff] [blame] | 115 | * packet was received on. |
| 116 | * - <code>vnet_buffer(b0)->l2.feature_bitmap</code> |
Dave Barach | b84a3e5 | 2016-08-30 17:01:52 -0400 | [diff] [blame] | 117 | * - Used to steer packets across l2 features enabled on the interface |
| 118 | * - <code>(vnet_classify_entry_t) e0->next_index</code> |
| 119 | * - Used to steer traffic when the classifier hits on a session |
| 120 | * - <code>(vnet_classify_entry_t) e0->advance</code> |
| 121 | * - Signed quantity applied via <code>vlib_buffer_advance</code> |
Chris Luke | 16bcf7d | 2016-09-01 14:31:46 -0400 | [diff] [blame] | 122 | * when the classifier hits on a session |
Dave Barach | b84a3e5 | 2016-08-30 17:01:52 -0400 | [diff] [blame] | 123 | * - <code>(vnet_classify_table_t) t0->miss_next_index</code> |
| 124 | * - Used to steer traffic when the classifier misses |
| 125 | * |
| 126 | * @em Sets: |
| 127 | * - <code>vnet_buffer (b0)->l2_classify.table_index</code> |
| 128 | * - Classifier table index of the first classifier table in |
| 129 | * the classifier table chain |
| 130 | * - <code>vnet_buffer (b0)->l2_classify.hash</code> |
| 131 | * - Bounded-index extensible hash corresponding to the |
| 132 | * masked fields in the current packet |
| 133 | * - <code>vnet_buffer (b0)->l2.feature_bitmap</code> |
| 134 | * - Used to steer packets across l2 features enabled on the interface |
| 135 | * - <code>vnet_buffer (b0)->l2_classify.opaque_index</code> |
| 136 | * - Copied from the classifier session object upon classifier hit |
| 137 | * |
| 138 | * @em Counters: |
| 139 | * - <code>L2_INPUT_CLASSIFY_ERROR_MISS</code> Classifier misses |
| 140 | * - <code>L2_INPUT_CLASSIFY_ERROR_HIT</code> Classifier hits |
| 141 | * - <code>L2_INPUT_CLASSIFY_ERROR_CHAIN_HIT</code> |
| 142 | * Classifier hits in other than the first table |
| 143 | */ |
| 144 | |
| 145 | static uword |
| 146 | l2_input_classify_node_fn (vlib_main_t * vm, |
| 147 | vlib_node_runtime_t * node, vlib_frame_t * frame) |
| 148 | { |
| 149 | u32 n_left_from, *from, *to_next; |
| 150 | l2_input_classify_next_t next_index; |
| 151 | l2_input_classify_main_t *cm = &l2_input_classify_main; |
| 152 | vnet_classify_main_t *vcm = cm->vnet_classify_main; |
| 153 | l2_input_classify_runtime_t *rt = |
| 154 | (l2_input_classify_runtime_t *) node->runtime_data; |
Dave Barach | b84a3e5 | 2016-08-30 17:01:52 -0400 | [diff] [blame] | 155 | u32 hits = 0; |
| 156 | u32 misses = 0; |
| 157 | u32 chain_hits = 0; |
| 158 | f64 now; |
| 159 | u32 n_next_nodes; |
| 160 | |
| 161 | n_next_nodes = node->n_next_nodes; |
| 162 | |
| 163 | now = vlib_time_now (vm); |
| 164 | |
| 165 | n_left_from = frame->n_vectors; |
| 166 | from = vlib_frame_vector_args (frame); |
| 167 | |
| 168 | /* First pass: compute hash */ |
| 169 | |
| 170 | while (n_left_from > 2) |
| 171 | { |
| 172 | vlib_buffer_t *b0, *b1; |
| 173 | u32 bi0, bi1; |
| 174 | ethernet_header_t *h0, *h1; |
| 175 | u32 sw_if_index0, sw_if_index1; |
| 176 | u16 type0, type1; |
| 177 | int type_index0, type_index1; |
| 178 | vnet_classify_table_t *t0, *t1; |
| 179 | u32 table_index0, table_index1; |
| 180 | u64 hash0, hash1; |
| 181 | |
| 182 | |
| 183 | /* prefetch next iteration */ |
| 184 | { |
| 185 | vlib_buffer_t *p1, *p2; |
| 186 | |
| 187 | p1 = vlib_get_buffer (vm, from[1]); |
| 188 | p2 = vlib_get_buffer (vm, from[2]); |
| 189 | |
| 190 | vlib_prefetch_buffer_header (p1, STORE); |
| 191 | CLIB_PREFETCH (p1->data, CLIB_CACHE_LINE_BYTES, STORE); |
| 192 | vlib_prefetch_buffer_header (p2, STORE); |
| 193 | CLIB_PREFETCH (p2->data, CLIB_CACHE_LINE_BYTES, STORE); |
| 194 | } |
| 195 | |
| 196 | bi0 = from[0]; |
| 197 | b0 = vlib_get_buffer (vm, bi0); |
| 198 | h0 = vlib_buffer_get_current (b0); |
| 199 | |
| 200 | bi1 = from[1]; |
| 201 | b1 = vlib_get_buffer (vm, bi1); |
| 202 | h1 = vlib_buffer_get_current (b1); |
| 203 | |
| 204 | sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX]; |
| 205 | vnet_buffer (b0)->l2_classify.table_index = ~0; |
| 206 | |
| 207 | sw_if_index1 = vnet_buffer (b1)->sw_if_index[VLIB_RX]; |
| 208 | vnet_buffer (b1)->l2_classify.table_index = ~0; |
| 209 | |
| 210 | /* Select classifier table based on ethertype */ |
| 211 | type0 = clib_net_to_host_u16 (h0->type); |
| 212 | type1 = clib_net_to_host_u16 (h1->type); |
| 213 | |
| 214 | type_index0 = (type0 == ETHERNET_TYPE_IP4) |
| 215 | ? L2_INPUT_CLASSIFY_TABLE_IP4 : L2_INPUT_CLASSIFY_TABLE_OTHER; |
| 216 | type_index0 = (type0 == ETHERNET_TYPE_IP6) |
| 217 | ? L2_INPUT_CLASSIFY_TABLE_IP6 : type_index0; |
| 218 | |
| 219 | type_index1 = (type1 == ETHERNET_TYPE_IP4) |
| 220 | ? L2_INPUT_CLASSIFY_TABLE_IP4 : L2_INPUT_CLASSIFY_TABLE_OTHER; |
| 221 | type_index1 = (type1 == ETHERNET_TYPE_IP6) |
| 222 | ? L2_INPUT_CLASSIFY_TABLE_IP6 : type_index1; |
| 223 | |
| 224 | vnet_buffer (b0)->l2_classify.table_index = |
| 225 | table_index0 = |
| 226 | rt->l2cm->classify_table_index_by_sw_if_index |
| 227 | [type_index0][sw_if_index0]; |
| 228 | |
| 229 | if (table_index0 != ~0) |
| 230 | { |
| 231 | t0 = pool_elt_at_index (vcm->tables, table_index0); |
| 232 | |
| 233 | vnet_buffer (b0)->l2_classify.hash = hash0 = |
| 234 | vnet_classify_hash_packet (t0, (u8 *) h0); |
| 235 | vnet_classify_prefetch_bucket (t0, hash0); |
| 236 | } |
| 237 | |
| 238 | vnet_buffer (b1)->l2_classify.table_index = |
| 239 | table_index1 = |
| 240 | rt->l2cm->classify_table_index_by_sw_if_index |
| 241 | [type_index1][sw_if_index1]; |
| 242 | |
| 243 | if (table_index1 != ~0) |
| 244 | { |
| 245 | t1 = pool_elt_at_index (vcm->tables, table_index1); |
| 246 | |
| 247 | vnet_buffer (b1)->l2_classify.hash = hash1 = |
| 248 | vnet_classify_hash_packet (t1, (u8 *) h1); |
| 249 | vnet_classify_prefetch_bucket (t1, hash1); |
| 250 | } |
| 251 | |
| 252 | from += 2; |
| 253 | n_left_from -= 2; |
| 254 | } |
| 255 | |
| 256 | while (n_left_from > 0) |
| 257 | { |
| 258 | vlib_buffer_t *b0; |
| 259 | u32 bi0; |
| 260 | ethernet_header_t *h0; |
| 261 | u32 sw_if_index0; |
| 262 | u16 type0; |
| 263 | u32 type_index0; |
| 264 | vnet_classify_table_t *t0; |
| 265 | u32 table_index0; |
| 266 | u64 hash0; |
| 267 | |
| 268 | bi0 = from[0]; |
| 269 | b0 = vlib_get_buffer (vm, bi0); |
| 270 | h0 = vlib_buffer_get_current (b0); |
| 271 | |
| 272 | sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX]; |
| 273 | vnet_buffer (b0)->l2_classify.table_index = ~0; |
| 274 | |
| 275 | /* Select classifier table based on ethertype */ |
| 276 | type0 = clib_net_to_host_u16 (h0->type); |
| 277 | |
| 278 | type_index0 = (type0 == ETHERNET_TYPE_IP4) |
| 279 | ? L2_INPUT_CLASSIFY_TABLE_IP4 : L2_INPUT_CLASSIFY_TABLE_OTHER; |
| 280 | type_index0 = (type0 == ETHERNET_TYPE_IP6) |
| 281 | ? L2_INPUT_CLASSIFY_TABLE_IP6 : type_index0; |
| 282 | |
| 283 | vnet_buffer (b0)->l2_classify.table_index = |
| 284 | table_index0 = rt->l2cm->classify_table_index_by_sw_if_index |
| 285 | [type_index0][sw_if_index0]; |
| 286 | |
| 287 | if (table_index0 != ~0) |
| 288 | { |
| 289 | t0 = pool_elt_at_index (vcm->tables, table_index0); |
| 290 | |
| 291 | vnet_buffer (b0)->l2_classify.hash = hash0 = |
| 292 | vnet_classify_hash_packet (t0, (u8 *) h0); |
| 293 | vnet_classify_prefetch_bucket (t0, hash0); |
| 294 | } |
| 295 | from++; |
| 296 | n_left_from--; |
| 297 | } |
| 298 | |
| 299 | next_index = node->cached_next_index; |
| 300 | from = vlib_frame_vector_args (frame); |
| 301 | n_left_from = frame->n_vectors; |
| 302 | |
| 303 | while (n_left_from > 0) |
| 304 | { |
| 305 | u32 n_left_to_next; |
| 306 | |
| 307 | vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next); |
| 308 | |
| 309 | /* Not enough load/store slots to dual loop... */ |
| 310 | while (n_left_from > 0 && n_left_to_next > 0) |
| 311 | { |
| 312 | u32 bi0; |
| 313 | vlib_buffer_t *b0; |
| 314 | u32 next0 = ~0; /* next l2 input feature, please... */ |
| 315 | ethernet_header_t *h0; |
| 316 | u32 table_index0; |
| 317 | u64 hash0; |
| 318 | vnet_classify_table_t *t0; |
| 319 | vnet_classify_entry_t *e0; |
| 320 | |
| 321 | if (PREDICT_TRUE (n_left_from > 2)) |
| 322 | { |
| 323 | vlib_buffer_t *p2 = vlib_get_buffer (vm, from[2]); |
| 324 | u64 phash2; |
| 325 | u32 table_index2; |
| 326 | vnet_classify_table_t *tp2; |
| 327 | |
| 328 | /* |
| 329 | * Prefetch table entry two ahead. Buffer / data |
| 330 | * were prefetched above... |
| 331 | */ |
| 332 | table_index2 = vnet_buffer (p2)->l2_classify.table_index; |
| 333 | |
| 334 | if (PREDICT_TRUE (table_index2 != ~0)) |
| 335 | { |
| 336 | tp2 = pool_elt_at_index (vcm->tables, table_index2); |
| 337 | phash2 = vnet_buffer (p2)->l2_classify.hash; |
| 338 | vnet_classify_prefetch_entry (tp2, phash2); |
| 339 | } |
| 340 | } |
| 341 | |
| 342 | /* speculatively enqueue b0 to the current next frame */ |
| 343 | bi0 = from[0]; |
| 344 | to_next[0] = bi0; |
| 345 | from += 1; |
| 346 | to_next += 1; |
| 347 | n_left_from -= 1; |
| 348 | n_left_to_next -= 1; |
| 349 | |
| 350 | b0 = vlib_get_buffer (vm, bi0); |
| 351 | h0 = vlib_buffer_get_current (b0); |
| 352 | table_index0 = vnet_buffer (b0)->l2_classify.table_index; |
| 353 | e0 = 0; |
| 354 | vnet_buffer (b0)->l2_classify.opaque_index = ~0; |
| 355 | |
Dave Barach | b84a3e5 | 2016-08-30 17:01:52 -0400 | [diff] [blame] | 356 | if (PREDICT_TRUE (table_index0 != ~0)) |
| 357 | { |
| 358 | hash0 = vnet_buffer (b0)->l2_classify.hash; |
| 359 | t0 = pool_elt_at_index (vcm->tables, table_index0); |
| 360 | |
| 361 | e0 = vnet_classify_find_entry (t0, (u8 *) h0, hash0, now); |
| 362 | if (e0) |
| 363 | { |
| 364 | vnet_buffer (b0)->l2_classify.opaque_index |
| 365 | = e0->opaque_index; |
| 366 | vlib_buffer_advance (b0, e0->advance); |
| 367 | next0 = (e0->next_index < n_next_nodes) ? |
| 368 | e0->next_index : next0; |
| 369 | hits++; |
| 370 | } |
| 371 | else |
| 372 | { |
| 373 | while (1) |
| 374 | { |
| 375 | if (t0->next_table_index != ~0) |
| 376 | t0 = pool_elt_at_index (vcm->tables, |
| 377 | t0->next_table_index); |
| 378 | else |
| 379 | { |
| 380 | next0 = (t0->miss_next_index < n_next_nodes) ? |
| 381 | t0->miss_next_index : next0; |
| 382 | misses++; |
| 383 | break; |
| 384 | } |
| 385 | |
| 386 | hash0 = vnet_classify_hash_packet (t0, (u8 *) h0); |
| 387 | e0 = |
| 388 | vnet_classify_find_entry (t0, (u8 *) h0, hash0, now); |
| 389 | if (e0) |
| 390 | { |
| 391 | vnet_buffer (b0)->l2_classify.opaque_index |
| 392 | = e0->opaque_index; |
| 393 | vlib_buffer_advance (b0, e0->advance); |
| 394 | next0 = (e0->next_index < n_next_nodes) ? |
| 395 | e0->next_index : next0; |
| 396 | hits++; |
| 397 | chain_hits++; |
| 398 | break; |
| 399 | } |
| 400 | } |
| 401 | } |
| 402 | } |
| 403 | |
| 404 | if (PREDICT_FALSE (next0 == 0)) |
| 405 | b0->error = node->errors[L2_INPUT_CLASSIFY_ERROR_DROP]; |
| 406 | |
John Lo | beb0b2e | 2017-07-22 00:21:36 -0400 | [diff] [blame] | 407 | /* Determine the next node and remove ourself from bitmap */ |
Dave Barach | b84a3e5 | 2016-08-30 17:01:52 -0400 | [diff] [blame] | 408 | if (PREDICT_TRUE (next0 == ~0)) |
John Lo | beb0b2e | 2017-07-22 00:21:36 -0400 | [diff] [blame] | 409 | next0 = vnet_l2_feature_next (b0, cm->l2_inp_feat_next, |
| 410 | L2INPUT_FEAT_INPUT_CLASSIFY); |
| 411 | else |
| 412 | vnet_buffer (b0)->l2.feature_bitmap &= |
| 413 | ~L2INPUT_FEAT_INPUT_CLASSIFY; |
Dave Barach | b84a3e5 | 2016-08-30 17:01:52 -0400 | [diff] [blame] | 414 | |
| 415 | if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE) |
| 416 | && (b0->flags & VLIB_BUFFER_IS_TRACED))) |
| 417 | { |
| 418 | l2_input_classify_trace_t *t = |
| 419 | vlib_add_trace (vm, node, b0, sizeof (*t)); |
| 420 | t->sw_if_index = vnet_buffer (b0)->sw_if_index[VLIB_RX]; |
| 421 | t->table_index = table_index0; |
| 422 | t->next_index = next0; |
| 423 | t->session_offset = e0 ? vnet_classify_get_offset (t0, e0) : 0; |
| 424 | } |
| 425 | |
| 426 | /* verify speculative enqueue, maybe switch current next frame */ |
| 427 | vlib_validate_buffer_enqueue_x1 (vm, node, next_index, |
| 428 | to_next, n_left_to_next, |
| 429 | bi0, next0); |
| 430 | } |
| 431 | |
| 432 | vlib_put_next_frame (vm, node, next_index, n_left_to_next); |
| 433 | } |
| 434 | |
| 435 | vlib_node_increment_counter (vm, node->node_index, |
| 436 | L2_INPUT_CLASSIFY_ERROR_MISS, misses); |
| 437 | vlib_node_increment_counter (vm, node->node_index, |
| 438 | L2_INPUT_CLASSIFY_ERROR_HIT, hits); |
| 439 | vlib_node_increment_counter (vm, node->node_index, |
| 440 | L2_INPUT_CLASSIFY_ERROR_CHAIN_HIT, chain_hits); |
| 441 | return frame->n_vectors; |
| 442 | } |
| 443 | |
| 444 | /* *INDENT-OFF* */ |
| 445 | VLIB_REGISTER_NODE (l2_input_classify_node) = { |
| 446 | .function = l2_input_classify_node_fn, |
| 447 | .name = "l2-input-classify", |
| 448 | .vector_size = sizeof (u32), |
| 449 | .format_trace = format_l2_input_classify_trace, |
| 450 | .type = VLIB_NODE_TYPE_INTERNAL, |
| 451 | |
| 452 | .n_errors = ARRAY_LEN(l2_input_classify_error_strings), |
| 453 | .error_strings = l2_input_classify_error_strings, |
| 454 | |
| 455 | .runtime_data_bytes = sizeof (l2_input_classify_runtime_t), |
| 456 | |
| 457 | .n_next_nodes = L2_INPUT_CLASSIFY_N_NEXT, |
| 458 | |
| 459 | /* edit / add dispositions here */ |
| 460 | .next_nodes = { |
| 461 | [L2_INPUT_CLASSIFY_NEXT_DROP] = "error-drop", |
| 462 | [L2_INPUT_CLASSIFY_NEXT_ETHERNET_INPUT] = "ethernet-input-not-l2", |
| 463 | [L2_INPUT_CLASSIFY_NEXT_IP4_INPUT] = "ip4-input", |
| 464 | [L2_INPUT_CLASSIFY_NEXT_IP6_INPUT] = "ip6-input", |
| 465 | [L2_INPUT_CLASSIFY_NEXT_LI] = "li-hit", |
| 466 | }, |
| 467 | }; |
| 468 | /* *INDENT-ON* */ |
| 469 | |
| 470 | VLIB_NODE_FUNCTION_MULTIARCH (l2_input_classify_node, |
| 471 | l2_input_classify_node_fn); |
| 472 | |
Chris Luke | 16bcf7d | 2016-09-01 14:31:46 -0400 | [diff] [blame] | 473 | /** l2 input classsifier feature initialization. */ |
Dave Barach | b84a3e5 | 2016-08-30 17:01:52 -0400 | [diff] [blame] | 474 | clib_error_t * |
| 475 | l2_input_classify_init (vlib_main_t * vm) |
| 476 | { |
| 477 | l2_input_classify_main_t *cm = &l2_input_classify_main; |
| 478 | l2_input_classify_runtime_t *rt; |
| 479 | |
| 480 | rt = vlib_node_get_runtime_data (vm, l2_input_classify_node.index); |
| 481 | |
| 482 | cm->vlib_main = vm; |
| 483 | cm->vnet_main = vnet_get_main (); |
| 484 | cm->vnet_classify_main = &vnet_classify_main; |
| 485 | |
| 486 | /* Initialize the feature next-node indexes */ |
| 487 | feat_bitmap_init_next_nodes (vm, |
| 488 | l2_input_classify_node.index, |
| 489 | L2INPUT_N_FEAT, |
| 490 | l2input_get_feat_names (), |
John Lo | beb0b2e | 2017-07-22 00:21:36 -0400 | [diff] [blame] | 491 | cm->l2_inp_feat_next); |
Dave Barach | b84a3e5 | 2016-08-30 17:01:52 -0400 | [diff] [blame] | 492 | rt->l2cm = cm; |
| 493 | rt->vcm = cm->vnet_classify_main; |
| 494 | |
| 495 | return 0; |
| 496 | } |
| 497 | |
| 498 | VLIB_INIT_FUNCTION (l2_input_classify_init); |
| 499 | |
Damjan Marion | e9f929b | 2017-03-16 11:32:09 +0100 | [diff] [blame] | 500 | clib_error_t * |
| 501 | l2_input_classify_worker_init (vlib_main_t * vm) |
| 502 | { |
| 503 | l2_input_classify_main_t *cm = &l2_input_classify_main; |
| 504 | l2_input_classify_runtime_t *rt; |
| 505 | |
| 506 | rt = vlib_node_get_runtime_data (vm, l2_input_classify_node.index); |
| 507 | |
| 508 | rt->l2cm = cm; |
| 509 | rt->vcm = cm->vnet_classify_main; |
| 510 | |
| 511 | return 0; |
| 512 | } |
| 513 | |
| 514 | VLIB_WORKER_INIT_FUNCTION (l2_input_classify_worker_init); |
Dave Barach | b84a3e5 | 2016-08-30 17:01:52 -0400 | [diff] [blame] | 515 | |
Chris Luke | 16bcf7d | 2016-09-01 14:31:46 -0400 | [diff] [blame] | 516 | /** Enable/disable l2 input classification on a specific interface. */ |
Dave Barach | b84a3e5 | 2016-08-30 17:01:52 -0400 | [diff] [blame] | 517 | void |
| 518 | vnet_l2_input_classify_enable_disable (u32 sw_if_index, int enable_disable) |
| 519 | { |
| 520 | l2input_intf_bitmap_enable (sw_if_index, L2INPUT_FEAT_INPUT_CLASSIFY, |
| 521 | (u32) enable_disable); |
| 522 | } |
| 523 | |
Chris Luke | 16bcf7d | 2016-09-01 14:31:46 -0400 | [diff] [blame] | 524 | /** @brief Set l2 per-protocol, per-interface input classification tables. |
Dave Barach | b84a3e5 | 2016-08-30 17:01:52 -0400 | [diff] [blame] | 525 | * |
| 526 | * @param sw_if_index interface handle |
| 527 | * @param ip4_table_index ip4 classification table index, or ~0 |
| 528 | * @param ip6_table_index ip6 classification table index, or ~0 |
| 529 | * @param other_table_index non-ip4, non-ip6 classification table index, |
| 530 | * or ~0 |
| 531 | * @returns 0 on success, VNET_API_ERROR_NO_SUCH_TABLE, TABLE2, TABLE3 |
| 532 | * if the indicated (non-~0) table does not exist. |
| 533 | */ |
| 534 | |
| 535 | int |
| 536 | vnet_l2_input_classify_set_tables (u32 sw_if_index, |
| 537 | u32 ip4_table_index, |
| 538 | u32 ip6_table_index, u32 other_table_index) |
| 539 | { |
| 540 | l2_input_classify_main_t *cm = &l2_input_classify_main; |
| 541 | vnet_classify_main_t *vcm = cm->vnet_classify_main; |
| 542 | |
| 543 | /* Assume that we've validated sw_if_index in the API layer */ |
| 544 | |
| 545 | if (ip4_table_index != ~0 && |
| 546 | pool_is_free_index (vcm->tables, ip4_table_index)) |
| 547 | return VNET_API_ERROR_NO_SUCH_TABLE; |
| 548 | |
| 549 | if (ip6_table_index != ~0 && |
| 550 | pool_is_free_index (vcm->tables, ip6_table_index)) |
| 551 | return VNET_API_ERROR_NO_SUCH_TABLE2; |
| 552 | |
| 553 | if (other_table_index != ~0 && |
| 554 | pool_is_free_index (vcm->tables, other_table_index)) |
| 555 | return VNET_API_ERROR_NO_SUCH_TABLE3; |
| 556 | |
| 557 | vec_validate |
| 558 | (cm->classify_table_index_by_sw_if_index[L2_INPUT_CLASSIFY_TABLE_IP4], |
| 559 | sw_if_index); |
| 560 | |
| 561 | vec_validate |
| 562 | (cm->classify_table_index_by_sw_if_index[L2_INPUT_CLASSIFY_TABLE_IP6], |
| 563 | sw_if_index); |
| 564 | |
| 565 | vec_validate |
| 566 | (cm->classify_table_index_by_sw_if_index[L2_INPUT_CLASSIFY_TABLE_OTHER], |
| 567 | sw_if_index); |
| 568 | |
| 569 | cm->classify_table_index_by_sw_if_index[L2_INPUT_CLASSIFY_TABLE_IP4] |
| 570 | [sw_if_index] = ip4_table_index; |
| 571 | |
| 572 | cm->classify_table_index_by_sw_if_index[L2_INPUT_CLASSIFY_TABLE_IP6] |
| 573 | [sw_if_index] = ip6_table_index; |
| 574 | |
| 575 | cm->classify_table_index_by_sw_if_index[L2_INPUT_CLASSIFY_TABLE_OTHER] |
| 576 | [sw_if_index] = other_table_index; |
| 577 | |
| 578 | return 0; |
| 579 | } |
| 580 | |
| 581 | static clib_error_t * |
| 582 | int_l2_input_classify_command_fn (vlib_main_t * vm, |
| 583 | unformat_input_t * input, |
| 584 | vlib_cli_command_t * cmd) |
| 585 | { |
| 586 | vnet_main_t *vnm = vnet_get_main (); |
| 587 | u32 sw_if_index = ~0; |
| 588 | u32 ip4_table_index = ~0; |
| 589 | u32 ip6_table_index = ~0; |
| 590 | u32 other_table_index = ~0; |
| 591 | int rv; |
| 592 | |
| 593 | while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) |
| 594 | { |
| 595 | if (unformat (input, "intfc %U", unformat_vnet_sw_interface, |
| 596 | vnm, &sw_if_index)) |
| 597 | ; |
| 598 | else if (unformat (input, "ip4-table %d", &ip4_table_index)) |
| 599 | ; |
| 600 | else if (unformat (input, "ip6-table %d", &ip6_table_index)) |
| 601 | ; |
| 602 | else if (unformat (input, "other-table %d", &other_table_index)) |
| 603 | ; |
| 604 | else |
| 605 | break; |
| 606 | } |
| 607 | |
| 608 | if (sw_if_index == ~0) |
| 609 | return clib_error_return (0, "interface must be specified"); |
| 610 | |
| 611 | |
| 612 | if (ip4_table_index == ~0 && ip6_table_index == ~0 |
| 613 | && other_table_index == ~0) |
| 614 | { |
| 615 | vlib_cli_output (vm, "L2 classification disabled"); |
| 616 | vnet_l2_input_classify_enable_disable (sw_if_index, 0 /* enable */ ); |
| 617 | return 0; |
| 618 | } |
| 619 | |
| 620 | rv = vnet_l2_input_classify_set_tables (sw_if_index, ip4_table_index, |
| 621 | ip6_table_index, other_table_index); |
| 622 | switch (rv) |
| 623 | { |
| 624 | case 0: |
| 625 | vnet_l2_input_classify_enable_disable (sw_if_index, 1 /* enable */ ); |
| 626 | break; |
| 627 | |
| 628 | default: |
| 629 | return clib_error_return (0, "vnet_l2_input_classify_set_tables: %d", |
| 630 | rv); |
| 631 | break; |
| 632 | } |
| 633 | |
| 634 | return 0; |
| 635 | } |
| 636 | |
| 637 | /*? |
| 638 | * Configure l2 input classification. |
| 639 | * |
| 640 | * @cliexpar |
Billy McFall | 22aa3e9 | 2016-09-09 08:46:40 -0400 | [diff] [blame] | 641 | * @cliexstart{set interface l2 input classify intfc <interface-name> [ip4-table <index>] [ip6-table <index>] [other-table <index>]} |
Dave Barach | b84a3e5 | 2016-08-30 17:01:52 -0400 | [diff] [blame] | 642 | * @cliexend |
Billy McFall | 22aa3e9 | 2016-09-09 08:46:40 -0400 | [diff] [blame] | 643 | * @todo This is incomplete. This needs a detailed description and a |
| 644 | * practical example. |
Dave Barach | b84a3e5 | 2016-08-30 17:01:52 -0400 | [diff] [blame] | 645 | ?*/ |
Dave Barach | b84a3e5 | 2016-08-30 17:01:52 -0400 | [diff] [blame] | 646 | /* *INDENT-OFF* */ |
| 647 | VLIB_CLI_COMMAND (int_l2_input_classify_cli, static) = { |
| 648 | .path = "set interface l2 input classify", |
| 649 | .short_help = |
Billy McFall | 22aa3e9 | 2016-09-09 08:46:40 -0400 | [diff] [blame] | 650 | "set interface l2 input classify intfc <interface-name> [ip4-table <n>]\n" |
Dave Barach | b84a3e5 | 2016-08-30 17:01:52 -0400 | [diff] [blame] | 651 | " [ip6-table <n>] [other-table <n>]", |
| 652 | .function = int_l2_input_classify_command_fn, |
| 653 | }; |
| 654 | /* *INDENT-ON* */ |
| 655 | |
| 656 | /* |
| 657 | * fd.io coding-style-patch-verification: ON |
| 658 | * |
| 659 | * Local Variables: |
| 660 | * eval: (c-set-style "gnu") |
| 661 | * End: |
| 662 | */ |