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 | |
Filip Tehlar | 44f0f71 | 2019-03-11 04:26:37 -0700 | [diff] [blame] | 70 | extern l2_input_classify_main_t l2_input_classify_main; |
| 71 | |
| 72 | #ifndef CLIB_MARCH_VARIANT |
Chris Luke | 16bcf7d | 2016-09-01 14:31:46 -0400 | [diff] [blame] | 73 | /** l2 input classifier main data structure. */ |
Dave Barach | b84a3e5 | 2016-08-30 17:01:52 -0400 | [diff] [blame] | 74 | l2_input_classify_main_t l2_input_classify_main; |
Filip Tehlar | 44f0f71 | 2019-03-11 04:26:37 -0700 | [diff] [blame] | 75 | #endif /* CLIB_MARCH_VARIANT */ |
Dave Barach | b84a3e5 | 2016-08-30 17:01:52 -0400 | [diff] [blame] | 76 | |
| 77 | #define foreach_l2_input_classify_error \ |
| 78 | _(MISS, "Classify misses") \ |
| 79 | _(HIT, "Classify hits") \ |
| 80 | _(CHAIN_HIT, "Classify hits after chain walk") \ |
| 81 | _(DROP, "L2 Classify Drops") |
| 82 | |
| 83 | typedef enum |
| 84 | { |
| 85 | #define _(sym,str) L2_INPUT_CLASSIFY_ERROR_##sym, |
| 86 | foreach_l2_input_classify_error |
| 87 | #undef _ |
| 88 | L2_INPUT_CLASSIFY_N_ERROR, |
| 89 | } l2_input_classify_error_t; |
| 90 | |
| 91 | static char *l2_input_classify_error_strings[] = { |
| 92 | #define _(sym,string) string, |
| 93 | foreach_l2_input_classify_error |
| 94 | #undef _ |
| 95 | }; |
| 96 | |
| 97 | /** |
| 98 | * @brief l2 input classifier node. |
| 99 | * @node l2-input-classify |
| 100 | * |
| 101 | * This is the l2 input classifier dispatch node |
| 102 | * |
| 103 | * @param vm vlib_main_t corresponding to the current thread. |
| 104 | * @param node vlib_node_runtime_t data for this node. |
| 105 | * @param frame vlib_frame_t whose contents should be dispatched. |
| 106 | * |
| 107 | * @par Graph mechanics: buffer metadata, next index usage |
| 108 | * |
| 109 | * @em Uses: |
| 110 | * - <code>(l2_input_classify_runtime_t *) |
| 111 | * rt->classify_table_index_by_sw_if_index</code> |
Chris Luke | 16bcf7d | 2016-09-01 14:31:46 -0400 | [diff] [blame] | 112 | * - Head of the per-interface, per-protocol classifier table chain |
| 113 | * for a specific interface. |
| 114 | * - @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] | 115 | * - <code>vnet_buffer(b)->sw_if_index[VLIB_RX]</code> |
| 116 | * - Indicates the @c sw_if_index value of the interface that the |
Chris Luke | 16bcf7d | 2016-09-01 14:31:46 -0400 | [diff] [blame] | 117 | * packet was received on. |
| 118 | * - <code>vnet_buffer(b0)->l2.feature_bitmap</code> |
Dave Barach | b84a3e5 | 2016-08-30 17:01:52 -0400 | [diff] [blame] | 119 | * - Used to steer packets across l2 features enabled on the interface |
| 120 | * - <code>(vnet_classify_entry_t) e0->next_index</code> |
| 121 | * - Used to steer traffic when the classifier hits on a session |
| 122 | * - <code>(vnet_classify_entry_t) e0->advance</code> |
| 123 | * - Signed quantity applied via <code>vlib_buffer_advance</code> |
Chris Luke | 16bcf7d | 2016-09-01 14:31:46 -0400 | [diff] [blame] | 124 | * when the classifier hits on a session |
Dave Barach | b84a3e5 | 2016-08-30 17:01:52 -0400 | [diff] [blame] | 125 | * - <code>(vnet_classify_table_t) t0->miss_next_index</code> |
| 126 | * - Used to steer traffic when the classifier misses |
| 127 | * |
| 128 | * @em Sets: |
| 129 | * - <code>vnet_buffer (b0)->l2_classify.table_index</code> |
| 130 | * - Classifier table index of the first classifier table in |
| 131 | * the classifier table chain |
| 132 | * - <code>vnet_buffer (b0)->l2_classify.hash</code> |
| 133 | * - Bounded-index extensible hash corresponding to the |
| 134 | * masked fields in the current packet |
| 135 | * - <code>vnet_buffer (b0)->l2.feature_bitmap</code> |
| 136 | * - Used to steer packets across l2 features enabled on the interface |
| 137 | * - <code>vnet_buffer (b0)->l2_classify.opaque_index</code> |
| 138 | * - Copied from the classifier session object upon classifier hit |
| 139 | * |
| 140 | * @em Counters: |
| 141 | * - <code>L2_INPUT_CLASSIFY_ERROR_MISS</code> Classifier misses |
| 142 | * - <code>L2_INPUT_CLASSIFY_ERROR_HIT</code> Classifier hits |
| 143 | * - <code>L2_INPUT_CLASSIFY_ERROR_CHAIN_HIT</code> |
| 144 | * Classifier hits in other than the first table |
| 145 | */ |
| 146 | |
Filip Tehlar | 44f0f71 | 2019-03-11 04:26:37 -0700 | [diff] [blame] | 147 | VLIB_NODE_FN (l2_input_classify_node) (vlib_main_t * vm, |
| 148 | vlib_node_runtime_t * node, |
| 149 | vlib_frame_t * frame) |
Dave Barach | b84a3e5 | 2016-08-30 17:01:52 -0400 | [diff] [blame] | 150 | { |
| 151 | u32 n_left_from, *from, *to_next; |
| 152 | l2_input_classify_next_t next_index; |
| 153 | l2_input_classify_main_t *cm = &l2_input_classify_main; |
| 154 | vnet_classify_main_t *vcm = cm->vnet_classify_main; |
| 155 | l2_input_classify_runtime_t *rt = |
| 156 | (l2_input_classify_runtime_t *) node->runtime_data; |
Dave Barach | b84a3e5 | 2016-08-30 17:01:52 -0400 | [diff] [blame] | 157 | u32 hits = 0; |
| 158 | u32 misses = 0; |
| 159 | u32 chain_hits = 0; |
| 160 | f64 now; |
| 161 | u32 n_next_nodes; |
| 162 | |
| 163 | n_next_nodes = node->n_next_nodes; |
| 164 | |
| 165 | now = vlib_time_now (vm); |
| 166 | |
| 167 | n_left_from = frame->n_vectors; |
| 168 | from = vlib_frame_vector_args (frame); |
| 169 | |
| 170 | /* First pass: compute hash */ |
| 171 | |
Andrew Yourtchenko | 2ca2005 | 2018-03-07 17:35:59 +0100 | [diff] [blame] | 172 | while (n_left_from >= 4) |
Dave Barach | b84a3e5 | 2016-08-30 17:01:52 -0400 | [diff] [blame] | 173 | { |
| 174 | vlib_buffer_t *b0, *b1; |
| 175 | u32 bi0, bi1; |
| 176 | ethernet_header_t *h0, *h1; |
| 177 | u32 sw_if_index0, sw_if_index1; |
| 178 | u16 type0, type1; |
| 179 | int type_index0, type_index1; |
| 180 | vnet_classify_table_t *t0, *t1; |
| 181 | u32 table_index0, table_index1; |
| 182 | u64 hash0, hash1; |
| 183 | |
| 184 | |
| 185 | /* prefetch next iteration */ |
| 186 | { |
Andrew Yourtchenko | 2ca2005 | 2018-03-07 17:35:59 +0100 | [diff] [blame] | 187 | vlib_buffer_t *p2, *p3; |
Dave Barach | b84a3e5 | 2016-08-30 17:01:52 -0400 | [diff] [blame] | 188 | |
Dave Barach | b84a3e5 | 2016-08-30 17:01:52 -0400 | [diff] [blame] | 189 | p2 = vlib_get_buffer (vm, from[2]); |
Andrew Yourtchenko | 2ca2005 | 2018-03-07 17:35:59 +0100 | [diff] [blame] | 190 | p3 = vlib_get_buffer (vm, from[3]); |
Dave Barach | b84a3e5 | 2016-08-30 17:01:52 -0400 | [diff] [blame] | 191 | |
Dave Barach | b84a3e5 | 2016-08-30 17:01:52 -0400 | [diff] [blame] | 192 | vlib_prefetch_buffer_header (p2, STORE); |
| 193 | CLIB_PREFETCH (p2->data, CLIB_CACHE_LINE_BYTES, STORE); |
Andrew Yourtchenko | 2ca2005 | 2018-03-07 17:35:59 +0100 | [diff] [blame] | 194 | vlib_prefetch_buffer_header (p3, STORE); |
| 195 | CLIB_PREFETCH (p3->data, CLIB_CACHE_LINE_BYTES, STORE); |
Dave Barach | b84a3e5 | 2016-08-30 17:01:52 -0400 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | bi0 = from[0]; |
| 199 | b0 = vlib_get_buffer (vm, bi0); |
| 200 | h0 = vlib_buffer_get_current (b0); |
| 201 | |
| 202 | bi1 = from[1]; |
| 203 | b1 = vlib_get_buffer (vm, bi1); |
| 204 | h1 = vlib_buffer_get_current (b1); |
| 205 | |
| 206 | sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX]; |
| 207 | vnet_buffer (b0)->l2_classify.table_index = ~0; |
| 208 | |
| 209 | sw_if_index1 = vnet_buffer (b1)->sw_if_index[VLIB_RX]; |
| 210 | vnet_buffer (b1)->l2_classify.table_index = ~0; |
| 211 | |
| 212 | /* Select classifier table based on ethertype */ |
| 213 | type0 = clib_net_to_host_u16 (h0->type); |
| 214 | type1 = clib_net_to_host_u16 (h1->type); |
| 215 | |
| 216 | type_index0 = (type0 == ETHERNET_TYPE_IP4) |
| 217 | ? L2_INPUT_CLASSIFY_TABLE_IP4 : L2_INPUT_CLASSIFY_TABLE_OTHER; |
| 218 | type_index0 = (type0 == ETHERNET_TYPE_IP6) |
| 219 | ? L2_INPUT_CLASSIFY_TABLE_IP6 : type_index0; |
| 220 | |
| 221 | type_index1 = (type1 == ETHERNET_TYPE_IP4) |
| 222 | ? L2_INPUT_CLASSIFY_TABLE_IP4 : L2_INPUT_CLASSIFY_TABLE_OTHER; |
| 223 | type_index1 = (type1 == ETHERNET_TYPE_IP6) |
| 224 | ? L2_INPUT_CLASSIFY_TABLE_IP6 : type_index1; |
| 225 | |
| 226 | vnet_buffer (b0)->l2_classify.table_index = |
| 227 | table_index0 = |
| 228 | rt->l2cm->classify_table_index_by_sw_if_index |
| 229 | [type_index0][sw_if_index0]; |
| 230 | |
| 231 | if (table_index0 != ~0) |
| 232 | { |
| 233 | t0 = pool_elt_at_index (vcm->tables, table_index0); |
| 234 | |
| 235 | vnet_buffer (b0)->l2_classify.hash = hash0 = |
| 236 | vnet_classify_hash_packet (t0, (u8 *) h0); |
| 237 | vnet_classify_prefetch_bucket (t0, hash0); |
| 238 | } |
| 239 | |
| 240 | vnet_buffer (b1)->l2_classify.table_index = |
| 241 | table_index1 = |
| 242 | rt->l2cm->classify_table_index_by_sw_if_index |
| 243 | [type_index1][sw_if_index1]; |
| 244 | |
| 245 | if (table_index1 != ~0) |
| 246 | { |
| 247 | t1 = pool_elt_at_index (vcm->tables, table_index1); |
| 248 | |
| 249 | vnet_buffer (b1)->l2_classify.hash = hash1 = |
| 250 | vnet_classify_hash_packet (t1, (u8 *) h1); |
| 251 | vnet_classify_prefetch_bucket (t1, hash1); |
| 252 | } |
| 253 | |
| 254 | from += 2; |
| 255 | n_left_from -= 2; |
| 256 | } |
| 257 | |
| 258 | while (n_left_from > 0) |
| 259 | { |
| 260 | vlib_buffer_t *b0; |
| 261 | u32 bi0; |
| 262 | ethernet_header_t *h0; |
| 263 | u32 sw_if_index0; |
| 264 | u16 type0; |
| 265 | u32 type_index0; |
| 266 | vnet_classify_table_t *t0; |
| 267 | u32 table_index0; |
| 268 | u64 hash0; |
| 269 | |
| 270 | bi0 = from[0]; |
| 271 | b0 = vlib_get_buffer (vm, bi0); |
| 272 | h0 = vlib_buffer_get_current (b0); |
| 273 | |
| 274 | sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX]; |
| 275 | vnet_buffer (b0)->l2_classify.table_index = ~0; |
| 276 | |
| 277 | /* Select classifier table based on ethertype */ |
| 278 | type0 = clib_net_to_host_u16 (h0->type); |
| 279 | |
| 280 | type_index0 = (type0 == ETHERNET_TYPE_IP4) |
| 281 | ? L2_INPUT_CLASSIFY_TABLE_IP4 : L2_INPUT_CLASSIFY_TABLE_OTHER; |
| 282 | type_index0 = (type0 == ETHERNET_TYPE_IP6) |
| 283 | ? L2_INPUT_CLASSIFY_TABLE_IP6 : type_index0; |
| 284 | |
| 285 | vnet_buffer (b0)->l2_classify.table_index = |
| 286 | table_index0 = rt->l2cm->classify_table_index_by_sw_if_index |
| 287 | [type_index0][sw_if_index0]; |
| 288 | |
| 289 | if (table_index0 != ~0) |
| 290 | { |
| 291 | t0 = pool_elt_at_index (vcm->tables, table_index0); |
| 292 | |
| 293 | vnet_buffer (b0)->l2_classify.hash = hash0 = |
| 294 | vnet_classify_hash_packet (t0, (u8 *) h0); |
| 295 | vnet_classify_prefetch_bucket (t0, hash0); |
| 296 | } |
| 297 | from++; |
| 298 | n_left_from--; |
| 299 | } |
| 300 | |
| 301 | next_index = node->cached_next_index; |
| 302 | from = vlib_frame_vector_args (frame); |
| 303 | n_left_from = frame->n_vectors; |
| 304 | |
| 305 | while (n_left_from > 0) |
| 306 | { |
| 307 | u32 n_left_to_next; |
| 308 | |
| 309 | vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next); |
| 310 | |
| 311 | /* Not enough load/store slots to dual loop... */ |
| 312 | while (n_left_from > 0 && n_left_to_next > 0) |
| 313 | { |
| 314 | u32 bi0; |
| 315 | vlib_buffer_t *b0; |
| 316 | u32 next0 = ~0; /* next l2 input feature, please... */ |
| 317 | ethernet_header_t *h0; |
| 318 | u32 table_index0; |
| 319 | u64 hash0; |
| 320 | vnet_classify_table_t *t0; |
| 321 | vnet_classify_entry_t *e0; |
| 322 | |
| 323 | if (PREDICT_TRUE (n_left_from > 2)) |
| 324 | { |
| 325 | vlib_buffer_t *p2 = vlib_get_buffer (vm, from[2]); |
| 326 | u64 phash2; |
| 327 | u32 table_index2; |
| 328 | vnet_classify_table_t *tp2; |
| 329 | |
| 330 | /* |
| 331 | * Prefetch table entry two ahead. Buffer / data |
| 332 | * were prefetched above... |
| 333 | */ |
| 334 | table_index2 = vnet_buffer (p2)->l2_classify.table_index; |
| 335 | |
| 336 | if (PREDICT_TRUE (table_index2 != ~0)) |
| 337 | { |
| 338 | tp2 = pool_elt_at_index (vcm->tables, table_index2); |
| 339 | phash2 = vnet_buffer (p2)->l2_classify.hash; |
| 340 | vnet_classify_prefetch_entry (tp2, phash2); |
| 341 | } |
| 342 | } |
| 343 | |
| 344 | /* speculatively enqueue b0 to the current next frame */ |
| 345 | bi0 = from[0]; |
| 346 | to_next[0] = bi0; |
| 347 | from += 1; |
| 348 | to_next += 1; |
| 349 | n_left_from -= 1; |
| 350 | n_left_to_next -= 1; |
| 351 | |
| 352 | b0 = vlib_get_buffer (vm, bi0); |
| 353 | h0 = vlib_buffer_get_current (b0); |
| 354 | table_index0 = vnet_buffer (b0)->l2_classify.table_index; |
| 355 | e0 = 0; |
| 356 | vnet_buffer (b0)->l2_classify.opaque_index = ~0; |
| 357 | |
Dave Barach | b84a3e5 | 2016-08-30 17:01:52 -0400 | [diff] [blame] | 358 | if (PREDICT_TRUE (table_index0 != ~0)) |
| 359 | { |
| 360 | hash0 = vnet_buffer (b0)->l2_classify.hash; |
| 361 | t0 = pool_elt_at_index (vcm->tables, table_index0); |
| 362 | |
| 363 | e0 = vnet_classify_find_entry (t0, (u8 *) h0, hash0, now); |
| 364 | if (e0) |
| 365 | { |
| 366 | vnet_buffer (b0)->l2_classify.opaque_index |
| 367 | = e0->opaque_index; |
| 368 | vlib_buffer_advance (b0, e0->advance); |
| 369 | next0 = (e0->next_index < n_next_nodes) ? |
| 370 | e0->next_index : next0; |
| 371 | hits++; |
| 372 | } |
| 373 | else |
| 374 | { |
| 375 | while (1) |
| 376 | { |
| 377 | if (t0->next_table_index != ~0) |
| 378 | t0 = pool_elt_at_index (vcm->tables, |
| 379 | t0->next_table_index); |
| 380 | else |
| 381 | { |
| 382 | next0 = (t0->miss_next_index < n_next_nodes) ? |
| 383 | t0->miss_next_index : next0; |
| 384 | misses++; |
| 385 | break; |
| 386 | } |
| 387 | |
| 388 | hash0 = vnet_classify_hash_packet (t0, (u8 *) h0); |
| 389 | e0 = |
| 390 | vnet_classify_find_entry (t0, (u8 *) h0, hash0, now); |
| 391 | if (e0) |
| 392 | { |
| 393 | vnet_buffer (b0)->l2_classify.opaque_index |
| 394 | = e0->opaque_index; |
| 395 | vlib_buffer_advance (b0, e0->advance); |
| 396 | next0 = (e0->next_index < n_next_nodes) ? |
| 397 | e0->next_index : next0; |
| 398 | hits++; |
| 399 | chain_hits++; |
| 400 | break; |
| 401 | } |
| 402 | } |
| 403 | } |
| 404 | } |
| 405 | |
| 406 | if (PREDICT_FALSE (next0 == 0)) |
| 407 | b0->error = node->errors[L2_INPUT_CLASSIFY_ERROR_DROP]; |
| 408 | |
John Lo | beb0b2e | 2017-07-22 00:21:36 -0400 | [diff] [blame] | 409 | /* Determine the next node and remove ourself from bitmap */ |
Dave Barach | b84a3e5 | 2016-08-30 17:01:52 -0400 | [diff] [blame] | 410 | if (PREDICT_TRUE (next0 == ~0)) |
John Lo | beb0b2e | 2017-07-22 00:21:36 -0400 | [diff] [blame] | 411 | next0 = vnet_l2_feature_next (b0, cm->l2_inp_feat_next, |
| 412 | L2INPUT_FEAT_INPUT_CLASSIFY); |
| 413 | else |
| 414 | vnet_buffer (b0)->l2.feature_bitmap &= |
| 415 | ~L2INPUT_FEAT_INPUT_CLASSIFY; |
Dave Barach | b84a3e5 | 2016-08-30 17:01:52 -0400 | [diff] [blame] | 416 | |
| 417 | if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE) |
| 418 | && (b0->flags & VLIB_BUFFER_IS_TRACED))) |
| 419 | { |
| 420 | l2_input_classify_trace_t *t = |
| 421 | vlib_add_trace (vm, node, b0, sizeof (*t)); |
| 422 | t->sw_if_index = vnet_buffer (b0)->sw_if_index[VLIB_RX]; |
| 423 | t->table_index = table_index0; |
| 424 | t->next_index = next0; |
| 425 | t->session_offset = e0 ? vnet_classify_get_offset (t0, e0) : 0; |
| 426 | } |
| 427 | |
| 428 | /* verify speculative enqueue, maybe switch current next frame */ |
| 429 | vlib_validate_buffer_enqueue_x1 (vm, node, next_index, |
| 430 | to_next, n_left_to_next, |
| 431 | bi0, next0); |
| 432 | } |
| 433 | |
| 434 | vlib_put_next_frame (vm, node, next_index, n_left_to_next); |
| 435 | } |
| 436 | |
| 437 | vlib_node_increment_counter (vm, node->node_index, |
| 438 | L2_INPUT_CLASSIFY_ERROR_MISS, misses); |
| 439 | vlib_node_increment_counter (vm, node->node_index, |
| 440 | L2_INPUT_CLASSIFY_ERROR_HIT, hits); |
| 441 | vlib_node_increment_counter (vm, node->node_index, |
| 442 | L2_INPUT_CLASSIFY_ERROR_CHAIN_HIT, chain_hits); |
| 443 | return frame->n_vectors; |
| 444 | } |
| 445 | |
| 446 | /* *INDENT-OFF* */ |
| 447 | VLIB_REGISTER_NODE (l2_input_classify_node) = { |
Dave Barach | b84a3e5 | 2016-08-30 17:01:52 -0400 | [diff] [blame] | 448 | .name = "l2-input-classify", |
| 449 | .vector_size = sizeof (u32), |
| 450 | .format_trace = format_l2_input_classify_trace, |
| 451 | .type = VLIB_NODE_TYPE_INTERNAL, |
| 452 | |
| 453 | .n_errors = ARRAY_LEN(l2_input_classify_error_strings), |
| 454 | .error_strings = l2_input_classify_error_strings, |
| 455 | |
| 456 | .runtime_data_bytes = sizeof (l2_input_classify_runtime_t), |
| 457 | |
| 458 | .n_next_nodes = L2_INPUT_CLASSIFY_N_NEXT, |
| 459 | |
| 460 | /* edit / add dispositions here */ |
| 461 | .next_nodes = { |
| 462 | [L2_INPUT_CLASSIFY_NEXT_DROP] = "error-drop", |
| 463 | [L2_INPUT_CLASSIFY_NEXT_ETHERNET_INPUT] = "ethernet-input-not-l2", |
| 464 | [L2_INPUT_CLASSIFY_NEXT_IP4_INPUT] = "ip4-input", |
| 465 | [L2_INPUT_CLASSIFY_NEXT_IP6_INPUT] = "ip6-input", |
| 466 | [L2_INPUT_CLASSIFY_NEXT_LI] = "li-hit", |
| 467 | }, |
| 468 | }; |
| 469 | /* *INDENT-ON* */ |
| 470 | |
Filip Tehlar | 44f0f71 | 2019-03-11 04:26:37 -0700 | [diff] [blame] | 471 | #ifndef CLIB_MARCH_VARIANT |
Chris Luke | 16bcf7d | 2016-09-01 14:31:46 -0400 | [diff] [blame] | 472 | /** l2 input classsifier feature initialization. */ |
Dave Barach | b84a3e5 | 2016-08-30 17:01:52 -0400 | [diff] [blame] | 473 | clib_error_t * |
| 474 | l2_input_classify_init (vlib_main_t * vm) |
| 475 | { |
| 476 | l2_input_classify_main_t *cm = &l2_input_classify_main; |
| 477 | l2_input_classify_runtime_t *rt; |
| 478 | |
| 479 | rt = vlib_node_get_runtime_data (vm, l2_input_classify_node.index); |
| 480 | |
| 481 | cm->vlib_main = vm; |
| 482 | cm->vnet_main = vnet_get_main (); |
| 483 | cm->vnet_classify_main = &vnet_classify_main; |
| 484 | |
| 485 | /* Initialize the feature next-node indexes */ |
| 486 | feat_bitmap_init_next_nodes (vm, |
| 487 | l2_input_classify_node.index, |
| 488 | L2INPUT_N_FEAT, |
| 489 | l2input_get_feat_names (), |
John Lo | beb0b2e | 2017-07-22 00:21:36 -0400 | [diff] [blame] | 490 | cm->l2_inp_feat_next); |
Dave Barach | b84a3e5 | 2016-08-30 17:01:52 -0400 | [diff] [blame] | 491 | rt->l2cm = cm; |
| 492 | rt->vcm = cm->vnet_classify_main; |
| 493 | |
| 494 | return 0; |
| 495 | } |
| 496 | |
| 497 | VLIB_INIT_FUNCTION (l2_input_classify_init); |
| 498 | |
Damjan Marion | e9f929b | 2017-03-16 11:32:09 +0100 | [diff] [blame] | 499 | clib_error_t * |
| 500 | l2_input_classify_worker_init (vlib_main_t * vm) |
| 501 | { |
| 502 | l2_input_classify_main_t *cm = &l2_input_classify_main; |
| 503 | l2_input_classify_runtime_t *rt; |
| 504 | |
| 505 | rt = vlib_node_get_runtime_data (vm, l2_input_classify_node.index); |
| 506 | |
| 507 | rt->l2cm = cm; |
| 508 | rt->vcm = cm->vnet_classify_main; |
| 509 | |
| 510 | return 0; |
| 511 | } |
| 512 | |
| 513 | VLIB_WORKER_INIT_FUNCTION (l2_input_classify_worker_init); |
Dave Barach | b84a3e5 | 2016-08-30 17:01:52 -0400 | [diff] [blame] | 514 | |
Chris Luke | 16bcf7d | 2016-09-01 14:31:46 -0400 | [diff] [blame] | 515 | /** Enable/disable l2 input classification on a specific interface. */ |
Dave Barach | b84a3e5 | 2016-08-30 17:01:52 -0400 | [diff] [blame] | 516 | void |
| 517 | vnet_l2_input_classify_enable_disable (u32 sw_if_index, int enable_disable) |
| 518 | { |
| 519 | l2input_intf_bitmap_enable (sw_if_index, L2INPUT_FEAT_INPUT_CLASSIFY, |
| 520 | (u32) enable_disable); |
| 521 | } |
| 522 | |
Chris Luke | 16bcf7d | 2016-09-01 14:31:46 -0400 | [diff] [blame] | 523 | /** @brief Set l2 per-protocol, per-interface input classification tables. |
Dave Barach | b84a3e5 | 2016-08-30 17:01:52 -0400 | [diff] [blame] | 524 | * |
| 525 | * @param sw_if_index interface handle |
| 526 | * @param ip4_table_index ip4 classification table index, or ~0 |
| 527 | * @param ip6_table_index ip6 classification table index, or ~0 |
| 528 | * @param other_table_index non-ip4, non-ip6 classification table index, |
| 529 | * or ~0 |
| 530 | * @returns 0 on success, VNET_API_ERROR_NO_SUCH_TABLE, TABLE2, TABLE3 |
| 531 | * if the indicated (non-~0) table does not exist. |
| 532 | */ |
| 533 | |
| 534 | int |
| 535 | vnet_l2_input_classify_set_tables (u32 sw_if_index, |
| 536 | u32 ip4_table_index, |
| 537 | u32 ip6_table_index, u32 other_table_index) |
| 538 | { |
| 539 | l2_input_classify_main_t *cm = &l2_input_classify_main; |
| 540 | vnet_classify_main_t *vcm = cm->vnet_classify_main; |
| 541 | |
| 542 | /* Assume that we've validated sw_if_index in the API layer */ |
| 543 | |
| 544 | if (ip4_table_index != ~0 && |
| 545 | pool_is_free_index (vcm->tables, ip4_table_index)) |
| 546 | return VNET_API_ERROR_NO_SUCH_TABLE; |
| 547 | |
| 548 | if (ip6_table_index != ~0 && |
| 549 | pool_is_free_index (vcm->tables, ip6_table_index)) |
| 550 | return VNET_API_ERROR_NO_SUCH_TABLE2; |
| 551 | |
| 552 | if (other_table_index != ~0 && |
| 553 | pool_is_free_index (vcm->tables, other_table_index)) |
| 554 | return VNET_API_ERROR_NO_SUCH_TABLE3; |
| 555 | |
| 556 | vec_validate |
| 557 | (cm->classify_table_index_by_sw_if_index[L2_INPUT_CLASSIFY_TABLE_IP4], |
| 558 | sw_if_index); |
| 559 | |
| 560 | vec_validate |
| 561 | (cm->classify_table_index_by_sw_if_index[L2_INPUT_CLASSIFY_TABLE_IP6], |
| 562 | sw_if_index); |
| 563 | |
| 564 | vec_validate |
| 565 | (cm->classify_table_index_by_sw_if_index[L2_INPUT_CLASSIFY_TABLE_OTHER], |
| 566 | sw_if_index); |
| 567 | |
| 568 | cm->classify_table_index_by_sw_if_index[L2_INPUT_CLASSIFY_TABLE_IP4] |
| 569 | [sw_if_index] = ip4_table_index; |
| 570 | |
| 571 | cm->classify_table_index_by_sw_if_index[L2_INPUT_CLASSIFY_TABLE_IP6] |
| 572 | [sw_if_index] = ip6_table_index; |
| 573 | |
| 574 | cm->classify_table_index_by_sw_if_index[L2_INPUT_CLASSIFY_TABLE_OTHER] |
| 575 | [sw_if_index] = other_table_index; |
| 576 | |
| 577 | return 0; |
| 578 | } |
Filip Tehlar | 44f0f71 | 2019-03-11 04:26:37 -0700 | [diff] [blame] | 579 | #endif /* CLIB_MARCH_VARIANT */ |
Dave Barach | b84a3e5 | 2016-08-30 17:01:52 -0400 | [diff] [blame] | 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 | */ |