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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <vnet/l2/l2_classify.h>
20 #include <vnet/api_errno.h>
23 /* per-pkt trace data */
28 } l2_classify_trace_t;
31 vnet_classify_main_t * vcm;
32 l2_classify_main_t * l2cm;
33 } l2_classify_runtime_t;
35 /* packet trace format function */
36 static u8 * format_l2_classify_trace (u8 * s, va_list * args)
38 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
39 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
40 l2_classify_trace_t * t = va_arg (*args, l2_classify_trace_t *);
42 s = format (s, "l2-classify: sw_if_index %d, table %d, offset %x, next %d",
43 t->sw_if_index, t->table_index, t->session_offset, t->next_index);
47 l2_classify_main_t l2_classify_main;
49 vlib_node_registration_t l2_classify_node;
51 #define foreach_l2_classify_error \
52 _(MISS, "Classify misses") \
53 _(HIT, "Classify hits") \
54 _(CHAIN_HIT, "Classify hits after chain walk") \
55 _(DROP, "L2 Classify Drops")
58 #define _(sym,str) L2_CLASSIFY_ERROR_##sym,
59 foreach_l2_classify_error
62 } l2_classify_error_t;
64 static char * l2_classify_error_strings[] = {
65 #define _(sym,string) string,
66 foreach_l2_classify_error
71 l2_classify_node_fn (vlib_main_t * vm,
72 vlib_node_runtime_t * node,
75 u32 n_left_from, * from, * to_next;
76 l2_classify_next_t next_index;
77 l2_classify_main_t * cm = &l2_classify_main;
78 vnet_classify_main_t * vcm = cm->vnet_classify_main;
79 l2_classify_runtime_t * rt = (l2_classify_runtime_t *)node->runtime_data;
86 now = vlib_time_now(vm);
88 n_left_from = frame->n_vectors;
89 from = vlib_frame_vector_args (frame);
91 /* First pass: compute hash */
93 while (n_left_from > 2)
95 vlib_buffer_t * b0, * b1;
97 ethernet_header_t * h0, * h1;
98 u32 sw_if_index0, sw_if_index1;
100 int type_index0, type_index1;
101 vnet_classify_table_t * t0, * t1;
102 u32 table_index0, table_index1;
106 /* prefetch next iteration */
108 vlib_buffer_t * p1, * p2;
110 p1 = vlib_get_buffer (vm, from[1]);
111 p2 = vlib_get_buffer (vm, from[2]);
113 vlib_prefetch_buffer_header (p1, STORE);
114 CLIB_PREFETCH (p1->data, CLIB_CACHE_LINE_BYTES, STORE);
115 vlib_prefetch_buffer_header (p2, STORE);
116 CLIB_PREFETCH (p2->data, CLIB_CACHE_LINE_BYTES, STORE);
120 b0 = vlib_get_buffer (vm, bi0);
121 h0 = vlib_buffer_get_current (b0);
124 b1 = vlib_get_buffer (vm, bi1);
125 h1 = vlib_buffer_get_current (b1);
127 sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_RX];
128 vnet_buffer(b0)->l2_classify.table_index = ~0;
130 sw_if_index1 = vnet_buffer(b1)->sw_if_index[VLIB_RX];
131 vnet_buffer(b1)->l2_classify.table_index = ~0;
133 /* Select classifier table based on ethertype */
134 type0 = clib_net_to_host_u16 (h0->type);
135 type1 = clib_net_to_host_u16 (h1->type);
137 type_index0 = (type0 == ETHERNET_TYPE_IP4)
138 ? L2_CLASSIFY_TABLE_IP4 : L2_CLASSIFY_TABLE_OTHER;
139 type_index0 = (type0 == ETHERNET_TYPE_IP6)
140 ? L2_CLASSIFY_TABLE_IP6 : type_index0;
142 type_index1 = (type1 == ETHERNET_TYPE_IP4)
143 ? L2_CLASSIFY_TABLE_IP4 : L2_CLASSIFY_TABLE_OTHER;
144 type_index1 = (type1 == ETHERNET_TYPE_IP6)
145 ? L2_CLASSIFY_TABLE_IP6 : type_index1;
147 vnet_buffer(b0)->l2_classify.table_index =
149 rt->l2cm->classify_table_index_by_sw_if_index
150 [type_index0][sw_if_index0];
152 if (table_index0 != ~0)
154 t0 = pool_elt_at_index (vcm->tables, table_index0);
156 vnet_buffer(b0)->l2_classify.hash = hash0 =
157 vnet_classify_hash_packet (t0, (u8 *) h0);
158 vnet_classify_prefetch_bucket (t0, hash0);
161 vnet_buffer(b1)->l2_classify.table_index =
163 rt->l2cm->classify_table_index_by_sw_if_index
164 [type_index1][sw_if_index1];
166 if (table_index1 != ~0)
168 t1 = pool_elt_at_index (vcm->tables, table_index1);
170 vnet_buffer(b1)->l2_classify.hash = hash1 =
171 vnet_classify_hash_packet (t1, (u8 *) h1);
172 vnet_classify_prefetch_bucket (t1, hash1);
179 while (n_left_from > 0)
183 ethernet_header_t * h0;
187 vnet_classify_table_t * t0;
192 b0 = vlib_get_buffer (vm, bi0);
193 h0 = vlib_buffer_get_current (b0);
195 sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_RX];
196 vnet_buffer(b0)->l2_classify.table_index = ~0;
198 /* Select classifier table based on ethertype */
199 type0 = clib_net_to_host_u16 (h0->type);
201 type_index0 = (type0 == ETHERNET_TYPE_IP4)
202 ? L2_CLASSIFY_TABLE_IP4 : L2_CLASSIFY_TABLE_OTHER;
203 type_index0 = (type0 == ETHERNET_TYPE_IP6)
204 ? L2_CLASSIFY_TABLE_IP6 : type_index0;
206 vnet_buffer(b0)->l2_classify.table_index =
207 table_index0 = rt->l2cm->classify_table_index_by_sw_if_index
208 [type_index0][sw_if_index0];
210 if (table_index0 != ~0)
212 t0 = pool_elt_at_index (vcm->tables, table_index0);
214 vnet_buffer(b0)->l2_classify.hash = hash0 =
215 vnet_classify_hash_packet (t0, (u8 *) h0);
216 vnet_classify_prefetch_bucket (t0, hash0);
222 next_index = node->cached_next_index;
223 from = vlib_frame_vector_args (frame);
224 n_left_from = frame->n_vectors;
226 while (n_left_from > 0)
230 vlib_get_next_frame (vm, node, next_index,
231 to_next, n_left_to_next);
233 /* Not enough load/store slots to dual loop... */
234 while (n_left_from > 0 && n_left_to_next > 0)
238 u32 next0 = L2_CLASSIFY_NEXT_ETHERNET_INPUT;
239 ethernet_header_t * h0;
242 vnet_classify_table_t * t0;
243 vnet_classify_entry_t * e0;
245 if (PREDICT_TRUE (n_left_from > 2))
247 vlib_buffer_t * p2 = vlib_get_buffer(vm, from[2]);
250 vnet_classify_table_t * tp2;
253 * Prefetch table entry two ahead. Buffer / data
254 * were prefetched above...
256 table_index2 = vnet_buffer(p2)->l2_classify.table_index;
258 if (PREDICT_TRUE (table_index2 != ~0))
260 tp2 = pool_elt_at_index (vcm->tables, table_index2);
261 phash2 = vnet_buffer(p2)->l2_classify.hash;
262 vnet_classify_prefetch_entry (tp2, phash2);
266 /* speculatively enqueue b0 to the current next frame */
274 b0 = vlib_get_buffer (vm, bi0);
275 h0 = vlib_buffer_get_current(b0);
276 table_index0 = vnet_buffer(b0)->l2_classify.table_index;
278 vnet_buffer(b0)->l2_classify.opaque_index = ~0;
280 if (PREDICT_TRUE(table_index0 != ~0))
282 hash0 = vnet_buffer(b0)->l2_classify.hash;
283 t0 = pool_elt_at_index (vcm->tables, table_index0);
285 e0 = vnet_classify_find_entry (t0, (u8 *) h0,
289 vnet_buffer(b0)->l2_classify.opaque_index
291 vlib_buffer_advance (b0, e0->advance);
292 next0 = (e0->next_index < L2_CLASSIFY_N_NEXT)?
293 e0->next_index:next0;
300 if (t0->next_table_index != ~0)
301 t0 = pool_elt_at_index (vcm->tables,
302 t0->next_table_index);
305 next0 = (t0->miss_next_index < L2_CLASSIFY_N_NEXT)?
306 t0->miss_next_index:next0;
311 hash0 = vnet_classify_hash_packet (t0, (u8 *) h0);
312 e0 = vnet_classify_find_entry (t0, (u8 *) h0, hash0, now);
315 vnet_buffer(b0)->l2_classify.opaque_index
317 vlib_buffer_advance (b0, e0->advance);
318 next0 = (e0->next_index < L2_CLASSIFY_N_NEXT)?
319 e0->next_index:next0;
328 if (PREDICT_FALSE(next0 == 0))
329 b0->error = node->errors[L2_CLASSIFY_ERROR_DROP];
331 if (PREDICT_FALSE (next0 == ~0))
334 // Remove ourself from the feature bitmap
335 feature_bitmap = vnet_buffer(b0)->l2.feature_bitmap
336 & ~L2INPUT_FEAT_CLASSIFY;
338 // save for next feature graph nodes
339 vnet_buffer(b0)->l2.feature_bitmap = feature_bitmap;
341 // Determine the next node
342 next0 = feat_bitmap_get_next_node_index(cm->feat_next_node_index,
346 if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE)
347 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
349 l2_classify_trace_t *t =
350 vlib_add_trace (vm, node, b0, sizeof (*t));
351 t->sw_if_index = vnet_buffer(b0)->sw_if_index[VLIB_RX];
352 t->table_index = table_index0;
353 t->next_index = next0;
354 t->session_offset = e0 ? vnet_classify_get_offset (t0, e0) : 0;
357 /* verify speculative enqueue, maybe switch current next frame */
358 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
359 to_next, n_left_to_next,
363 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
366 vlib_node_increment_counter (vm, node->node_index,
367 L2_CLASSIFY_ERROR_MISS,
369 vlib_node_increment_counter (vm, node->node_index,
370 L2_CLASSIFY_ERROR_HIT,
372 vlib_node_increment_counter (vm, node->node_index,
373 L2_CLASSIFY_ERROR_CHAIN_HIT,
375 return frame->n_vectors;
378 VLIB_REGISTER_NODE (l2_classify_node) = {
379 .function = l2_classify_node_fn,
380 .name = "l2-classify",
381 .vector_size = sizeof (u32),
382 .format_trace = format_l2_classify_trace,
383 .type = VLIB_NODE_TYPE_INTERNAL,
385 .n_errors = ARRAY_LEN(l2_classify_error_strings),
386 .error_strings = l2_classify_error_strings,
388 .runtime_data_bytes = sizeof (l2_classify_runtime_t),
390 .n_next_nodes = L2_CLASSIFY_N_NEXT,
392 /* edit / add dispositions here */
394 [L2_CLASSIFY_NEXT_DROP] = "error-drop",
395 [L2_CLASSIFY_NEXT_ETHERNET_INPUT] = "ethernet-input-not-l2",
396 [L2_CLASSIFY_NEXT_IP4_INPUT] = "ip4-input",
397 [L2_CLASSIFY_NEXT_IP6_INPUT] = "ip6-input",
398 [L2_CLASSIFY_NEXT_LI] = "li-hit",
402 VLIB_NODE_FUNCTION_MULTIARCH (l2_classify_node, l2_classify_node_fn)
404 clib_error_t *l2_classify_init (vlib_main_t *vm)
406 l2_classify_main_t * cm = &l2_classify_main;
407 l2_classify_runtime_t * rt;
409 rt = vlib_node_get_runtime_data (vm, l2_classify_node.index);
412 cm->vnet_main = vnet_get_main();
413 cm->vnet_classify_main = &vnet_classify_main;
415 // Initialize the feature next-node indexes
416 feat_bitmap_init_next_nodes(vm,
417 l2_classify_node.index,
419 l2input_get_feat_names(),
420 cm->feat_next_node_index);
422 rt->vcm = cm->vnet_classify_main;
427 VLIB_INIT_FUNCTION (l2_classify_init);
430 void vnet_l2_classify_enable_disable (u32 sw_if_index,
433 vlib_main_t * vm = vlib_get_main();
434 vnet_main_t * vnm = vnet_get_main();
437 set_int_l2_mode (vm, vnm, MODE_L2_CLASSIFY, sw_if_index,
440 set_int_l2_mode (vm, vnm, MODE_L3, sw_if_index,
444 int vnet_l2_classify_set_tables (u32 sw_if_index,
447 u32 other_table_index)
449 l2_classify_main_t * cm = &l2_classify_main;
450 vnet_classify_main_t * vcm = cm->vnet_classify_main;
452 /* Assume that we've validated sw_if_index in the API layer */
454 if (ip4_table_index != ~0 &&
455 pool_is_free_index (vcm->tables, ip4_table_index))
456 return VNET_API_ERROR_NO_SUCH_TABLE;
458 if (ip6_table_index != ~0 &&
459 pool_is_free_index (vcm->tables, ip6_table_index))
460 return VNET_API_ERROR_NO_SUCH_TABLE2;
462 if (other_table_index != ~0 &&
463 pool_is_free_index (vcm->tables, other_table_index))
464 return VNET_API_ERROR_NO_SUCH_TABLE3;
467 (cm->classify_table_index_by_sw_if_index[L2_CLASSIFY_TABLE_IP4],
471 (cm->classify_table_index_by_sw_if_index[L2_CLASSIFY_TABLE_IP6],
475 (cm->classify_table_index_by_sw_if_index[L2_CLASSIFY_TABLE_OTHER],
478 cm->classify_table_index_by_sw_if_index[L2_CLASSIFY_TABLE_IP4]
479 [sw_if_index] = ip4_table_index;
481 cm->classify_table_index_by_sw_if_index[L2_CLASSIFY_TABLE_IP6]
482 [sw_if_index] = ip6_table_index;
484 cm->classify_table_index_by_sw_if_index[L2_CLASSIFY_TABLE_OTHER]
485 [sw_if_index] = other_table_index;
490 static clib_error_t *
491 int_l2_classify_command_fn (vlib_main_t * vm,
492 unformat_input_t * input,
493 vlib_cli_command_t * cmd)
495 vnet_main_t * vnm = vnet_get_main();
496 u32 sw_if_index = ~0;
497 u32 ip4_table_index = ~0;
498 u32 ip6_table_index = ~0;
499 u32 other_table_index = ~0;
502 while (unformat_check_input(input) != UNFORMAT_END_OF_INPUT)
504 if (unformat (input, "intfc %U", unformat_vnet_sw_interface,
507 else if (unformat (input, "ip4-table %d", &ip4_table_index))
509 else if (unformat (input, "ip6-table %d", &ip6_table_index))
511 else if (unformat (input, "other-table %d", &other_table_index))
517 if (sw_if_index == ~0)
518 return clib_error_return (0, "interface must be specified");
521 if (ip4_table_index == ~0 && ip6_table_index == ~0
522 && other_table_index == ~0)
524 vlib_cli_output (vm, "L2 classification disabled");
525 vnet_l2_classify_enable_disable (sw_if_index, 0 /* enable */);
529 rv = vnet_l2_classify_set_tables (sw_if_index, ip4_table_index,
530 ip6_table_index, other_table_index);
534 vnet_l2_classify_enable_disable (sw_if_index, 1 /* enable */);
538 return clib_error_return (0, "vnet_l2_classify_set_tables: %d",
546 VLIB_CLI_COMMAND (int_l2_classify_cli, static) = {
547 .path = "set interface l2 classify",
549 "set interface l2 classify intfc <int> [ip4-table <n>]\n"
550 " [ip6-table <n>] [other-table <n>]",
551 .function = int_l2_classify_command_fn,