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>
24 /* per-pkt trace data */
29 } l2_classify_trace_t;
33 vnet_classify_main_t *vcm;
34 l2_classify_main_t *l2cm;
35 } l2_classify_runtime_t;
37 /* packet trace format function */
39 format_l2_classify_trace (u8 * s, va_list * args)
41 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
42 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
43 l2_classify_trace_t *t = va_arg (*args, l2_classify_trace_t *);
45 s = format (s, "l2-classify: sw_if_index %d, table %d, offset %x, next %d",
46 t->sw_if_index, t->table_index, t->session_offset,
51 l2_classify_main_t l2_classify_main;
53 vlib_node_registration_t l2_classify_node;
55 #define foreach_l2_classify_error \
56 _(MISS, "Classify misses") \
57 _(HIT, "Classify hits") \
58 _(CHAIN_HIT, "Classify hits after chain walk") \
59 _(DROP, "L2 Classify Drops")
63 #define _(sym,str) L2_CLASSIFY_ERROR_##sym,
64 foreach_l2_classify_error
67 } l2_classify_error_t;
69 static char *l2_classify_error_strings[] = {
70 #define _(sym,string) string,
71 foreach_l2_classify_error
76 l2_classify_node_fn (vlib_main_t * vm,
77 vlib_node_runtime_t * node, vlib_frame_t * frame)
79 u32 n_left_from, *from, *to_next;
80 l2_classify_next_t next_index;
81 l2_classify_main_t *cm = &l2_classify_main;
82 vnet_classify_main_t *vcm = cm->vnet_classify_main;
83 l2_classify_runtime_t *rt = (l2_classify_runtime_t *) node->runtime_data;
91 n_next_nodes = node->n_next_nodes;
93 now = vlib_time_now (vm);
95 n_left_from = frame->n_vectors;
96 from = vlib_frame_vector_args (frame);
98 /* First pass: compute hash */
100 while (n_left_from > 2)
102 vlib_buffer_t *b0, *b1;
104 ethernet_header_t *h0, *h1;
105 u32 sw_if_index0, sw_if_index1;
107 int type_index0, type_index1;
108 vnet_classify_table_t *t0, *t1;
109 u32 table_index0, table_index1;
113 /* prefetch next iteration */
115 vlib_buffer_t *p1, *p2;
117 p1 = vlib_get_buffer (vm, from[1]);
118 p2 = vlib_get_buffer (vm, from[2]);
120 vlib_prefetch_buffer_header (p1, STORE);
121 CLIB_PREFETCH (p1->data, CLIB_CACHE_LINE_BYTES, STORE);
122 vlib_prefetch_buffer_header (p2, STORE);
123 CLIB_PREFETCH (p2->data, CLIB_CACHE_LINE_BYTES, STORE);
127 b0 = vlib_get_buffer (vm, bi0);
128 h0 = vlib_buffer_get_current (b0);
131 b1 = vlib_get_buffer (vm, bi1);
132 h1 = vlib_buffer_get_current (b1);
134 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
135 vnet_buffer (b0)->l2_classify.table_index = ~0;
137 sw_if_index1 = vnet_buffer (b1)->sw_if_index[VLIB_RX];
138 vnet_buffer (b1)->l2_classify.table_index = ~0;
140 /* Select classifier table based on ethertype */
141 type0 = clib_net_to_host_u16 (h0->type);
142 type1 = clib_net_to_host_u16 (h1->type);
144 type_index0 = (type0 == ETHERNET_TYPE_IP4)
145 ? L2_CLASSIFY_TABLE_IP4 : L2_CLASSIFY_TABLE_OTHER;
146 type_index0 = (type0 == ETHERNET_TYPE_IP6)
147 ? L2_CLASSIFY_TABLE_IP6 : type_index0;
149 type_index1 = (type1 == ETHERNET_TYPE_IP4)
150 ? L2_CLASSIFY_TABLE_IP4 : L2_CLASSIFY_TABLE_OTHER;
151 type_index1 = (type1 == ETHERNET_TYPE_IP6)
152 ? L2_CLASSIFY_TABLE_IP6 : type_index1;
154 vnet_buffer (b0)->l2_classify.table_index =
156 rt->l2cm->classify_table_index_by_sw_if_index
157 [type_index0][sw_if_index0];
159 if (table_index0 != ~0)
161 t0 = pool_elt_at_index (vcm->tables, table_index0);
163 vnet_buffer (b0)->l2_classify.hash = hash0 =
164 vnet_classify_hash_packet (t0, (u8 *) h0);
165 vnet_classify_prefetch_bucket (t0, hash0);
168 vnet_buffer (b1)->l2_classify.table_index =
170 rt->l2cm->classify_table_index_by_sw_if_index
171 [type_index1][sw_if_index1];
173 if (table_index1 != ~0)
175 t1 = pool_elt_at_index (vcm->tables, table_index1);
177 vnet_buffer (b1)->l2_classify.hash = hash1 =
178 vnet_classify_hash_packet (t1, (u8 *) h1);
179 vnet_classify_prefetch_bucket (t1, hash1);
186 while (n_left_from > 0)
190 ethernet_header_t *h0;
194 vnet_classify_table_t *t0;
199 b0 = vlib_get_buffer (vm, bi0);
200 h0 = vlib_buffer_get_current (b0);
202 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
203 vnet_buffer (b0)->l2_classify.table_index = ~0;
205 /* Select classifier table based on ethertype */
206 type0 = clib_net_to_host_u16 (h0->type);
208 type_index0 = (type0 == ETHERNET_TYPE_IP4)
209 ? L2_CLASSIFY_TABLE_IP4 : L2_CLASSIFY_TABLE_OTHER;
210 type_index0 = (type0 == ETHERNET_TYPE_IP6)
211 ? L2_CLASSIFY_TABLE_IP6 : type_index0;
213 vnet_buffer (b0)->l2_classify.table_index =
214 table_index0 = rt->l2cm->classify_table_index_by_sw_if_index
215 [type_index0][sw_if_index0];
217 if (table_index0 != ~0)
219 t0 = pool_elt_at_index (vcm->tables, table_index0);
221 vnet_buffer (b0)->l2_classify.hash = hash0 =
222 vnet_classify_hash_packet (t0, (u8 *) h0);
223 vnet_classify_prefetch_bucket (t0, hash0);
229 next_index = node->cached_next_index;
230 from = vlib_frame_vector_args (frame);
231 n_left_from = frame->n_vectors;
233 while (n_left_from > 0)
237 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
239 /* Not enough load/store slots to dual loop... */
240 while (n_left_from > 0 && n_left_to_next > 0)
244 u32 next0 = L2_CLASSIFY_NEXT_ETHERNET_INPUT;
245 ethernet_header_t *h0;
248 vnet_classify_table_t *t0;
249 vnet_classify_entry_t *e0;
251 if (PREDICT_TRUE (n_left_from > 2))
253 vlib_buffer_t *p2 = vlib_get_buffer (vm, from[2]);
256 vnet_classify_table_t *tp2;
259 * Prefetch table entry two ahead. Buffer / data
260 * were prefetched above...
262 table_index2 = vnet_buffer (p2)->l2_classify.table_index;
264 if (PREDICT_TRUE (table_index2 != ~0))
266 tp2 = pool_elt_at_index (vcm->tables, table_index2);
267 phash2 = vnet_buffer (p2)->l2_classify.hash;
268 vnet_classify_prefetch_entry (tp2, phash2);
272 /* speculatively enqueue b0 to the current next frame */
280 b0 = vlib_get_buffer (vm, bi0);
281 h0 = vlib_buffer_get_current (b0);
282 table_index0 = vnet_buffer (b0)->l2_classify.table_index;
284 vnet_buffer (b0)->l2_classify.opaque_index = ~0;
286 if (PREDICT_TRUE (table_index0 != ~0))
288 hash0 = vnet_buffer (b0)->l2_classify.hash;
289 t0 = pool_elt_at_index (vcm->tables, table_index0);
291 e0 = vnet_classify_find_entry (t0, (u8 *) h0, hash0, now);
294 vnet_buffer (b0)->l2_classify.opaque_index
296 vlib_buffer_advance (b0, e0->advance);
297 next0 = (e0->next_index < n_next_nodes) ?
298 e0->next_index : next0;
305 if (t0->next_table_index != ~0)
306 t0 = pool_elt_at_index (vcm->tables,
307 t0->next_table_index);
310 next0 = (t0->miss_next_index < n_next_nodes) ?
311 t0->miss_next_index : next0;
316 hash0 = vnet_classify_hash_packet (t0, (u8 *) h0);
318 vnet_classify_find_entry (t0, (u8 *) h0, hash0, now);
321 vnet_buffer (b0)->l2_classify.opaque_index
323 vlib_buffer_advance (b0, e0->advance);
324 next0 = (e0->next_index < n_next_nodes) ?
325 e0->next_index : next0;
334 if (PREDICT_FALSE (next0 == 0))
335 b0->error = node->errors[L2_CLASSIFY_ERROR_DROP];
337 if (PREDICT_FALSE (next0 == ~0))
340 // Remove ourself from the feature bitmap
341 feature_bitmap = vnet_buffer (b0)->l2.feature_bitmap
342 & ~L2INPUT_FEAT_CLASSIFY;
344 // save for next feature graph nodes
345 vnet_buffer (b0)->l2.feature_bitmap = feature_bitmap;
347 // Determine the next node
349 feat_bitmap_get_next_node_index (cm->feat_next_node_index,
353 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
354 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
356 l2_classify_trace_t *t =
357 vlib_add_trace (vm, node, b0, sizeof (*t));
358 t->sw_if_index = vnet_buffer (b0)->sw_if_index[VLIB_RX];
359 t->table_index = table_index0;
360 t->next_index = next0;
361 t->session_offset = e0 ? vnet_classify_get_offset (t0, e0) : 0;
364 /* verify speculative enqueue, maybe switch current next frame */
365 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
366 to_next, n_left_to_next,
370 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
373 vlib_node_increment_counter (vm, node->node_index,
374 L2_CLASSIFY_ERROR_MISS, misses);
375 vlib_node_increment_counter (vm, node->node_index,
376 L2_CLASSIFY_ERROR_HIT, hits);
377 vlib_node_increment_counter (vm, node->node_index,
378 L2_CLASSIFY_ERROR_CHAIN_HIT, chain_hits);
379 return frame->n_vectors;
383 VLIB_REGISTER_NODE (l2_classify_node) = {
384 .function = l2_classify_node_fn,
385 .name = "l2-classify",
386 .vector_size = sizeof (u32),
387 .format_trace = format_l2_classify_trace,
388 .type = VLIB_NODE_TYPE_INTERNAL,
390 .n_errors = ARRAY_LEN(l2_classify_error_strings),
391 .error_strings = l2_classify_error_strings,
393 .runtime_data_bytes = sizeof (l2_classify_runtime_t),
395 .n_next_nodes = L2_CLASSIFY_N_NEXT,
397 /* edit / add dispositions here */
399 [L2_CLASSIFY_NEXT_DROP] = "error-drop",
400 [L2_CLASSIFY_NEXT_ETHERNET_INPUT] = "ethernet-input-not-l2",
401 [L2_CLASSIFY_NEXT_IP4_INPUT] = "ip4-input",
402 [L2_CLASSIFY_NEXT_IP6_INPUT] = "ip6-input",
403 [L2_CLASSIFY_NEXT_LI] = "li-hit",
408 VLIB_NODE_FUNCTION_MULTIARCH (l2_classify_node, l2_classify_node_fn)
409 clib_error_t *l2_classify_init (vlib_main_t * vm)
411 l2_classify_main_t *cm = &l2_classify_main;
412 l2_classify_runtime_t *rt;
414 rt = vlib_node_get_runtime_data (vm, l2_classify_node.index);
417 cm->vnet_main = vnet_get_main ();
418 cm->vnet_classify_main = &vnet_classify_main;
420 /* Initialize the feature next-node indexes */
421 feat_bitmap_init_next_nodes (vm,
422 l2_classify_node.index,
424 l2input_get_feat_names (),
425 cm->feat_next_node_index);
427 rt->vcm = cm->vnet_classify_main;
432 VLIB_INIT_FUNCTION (l2_classify_init);
436 vnet_l2_classify_enable_disable (u32 sw_if_index, int enable_disable)
438 vlib_main_t *vm = vlib_get_main ();
439 vnet_main_t *vnm = vnet_get_main ();
442 set_int_l2_mode (vm, vnm, MODE_L2_CLASSIFY, sw_if_index, 0, 0, 0, 0);
444 set_int_l2_mode (vm, vnm, MODE_L3, sw_if_index, 0, 0, 0, 0);
448 vnet_l2_classify_set_tables (u32 sw_if_index,
450 u32 ip6_table_index, u32 other_table_index)
452 l2_classify_main_t *cm = &l2_classify_main;
453 vnet_classify_main_t *vcm = cm->vnet_classify_main;
455 /* Assume that we've validated sw_if_index in the API layer */
457 if (ip4_table_index != ~0 &&
458 pool_is_free_index (vcm->tables, ip4_table_index))
459 return VNET_API_ERROR_NO_SUCH_TABLE;
461 if (ip6_table_index != ~0 &&
462 pool_is_free_index (vcm->tables, ip6_table_index))
463 return VNET_API_ERROR_NO_SUCH_TABLE2;
465 if (other_table_index != ~0 &&
466 pool_is_free_index (vcm->tables, other_table_index))
467 return VNET_API_ERROR_NO_SUCH_TABLE3;
470 (cm->classify_table_index_by_sw_if_index[L2_CLASSIFY_TABLE_IP4],
474 (cm->classify_table_index_by_sw_if_index[L2_CLASSIFY_TABLE_IP6],
478 (cm->classify_table_index_by_sw_if_index[L2_CLASSIFY_TABLE_OTHER],
481 cm->classify_table_index_by_sw_if_index[L2_CLASSIFY_TABLE_IP4]
482 [sw_if_index] = ip4_table_index;
484 cm->classify_table_index_by_sw_if_index[L2_CLASSIFY_TABLE_IP6]
485 [sw_if_index] = ip6_table_index;
487 cm->classify_table_index_by_sw_if_index[L2_CLASSIFY_TABLE_OTHER]
488 [sw_if_index] = other_table_index;
493 static clib_error_t *
494 int_l2_classify_command_fn (vlib_main_t * vm,
495 unformat_input_t * input,
496 vlib_cli_command_t * cmd)
498 vnet_main_t *vnm = vnet_get_main ();
499 u32 sw_if_index = ~0;
500 u32 ip4_table_index = ~0;
501 u32 ip6_table_index = ~0;
502 u32 other_table_index = ~0;
505 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
507 if (unformat (input, "intfc %U", unformat_vnet_sw_interface,
510 else if (unformat (input, "ip4-table %d", &ip4_table_index))
512 else if (unformat (input, "ip6-table %d", &ip6_table_index))
514 else if (unformat (input, "other-table %d", &other_table_index))
520 if (sw_if_index == ~0)
521 return clib_error_return (0, "interface must be specified");
524 if (ip4_table_index == ~0 && ip6_table_index == ~0
525 && other_table_index == ~0)
527 vlib_cli_output (vm, "L2 classification disabled");
528 vnet_l2_classify_enable_disable (sw_if_index, 0 /* enable */ );
532 rv = vnet_l2_classify_set_tables (sw_if_index, ip4_table_index,
533 ip6_table_index, other_table_index);
537 vnet_l2_classify_enable_disable (sw_if_index, 1 /* enable */ );
541 return clib_error_return (0, "vnet_l2_classify_set_tables: %d", rv);
549 VLIB_CLI_COMMAND (int_l2_classify_cli, static) = {
550 .path = "set interface l2 classify",
552 "set interface l2 classify intfc <int> [ip4-table <n>]\n"
553 " [ip6-table <n>] [other-table <n>]",
554 .function = int_l2_classify_command_fn,
561 * fd.io coding-style-patch-verification: ON
564 * eval: (c-set-style "gnu")