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;
279 if (PREDICT_TRUE(table_index0 != ~0))
281 hash0 = vnet_buffer(b0)->l2_classify.hash;
282 t0 = pool_elt_at_index (vcm->tables, table_index0);
284 e0 = vnet_classify_find_entry (t0, (u8 *) h0,
288 vnet_buffer(b0)->l2_classify.opaque_index
290 vlib_buffer_advance (b0, e0->advance);
291 next0 = (e0->next_index < L2_CLASSIFY_N_NEXT)?
292 e0->next_index:next0;
299 if (t0->next_table_index != ~0)
300 t0 = pool_elt_at_index (vcm->tables,
301 t0->next_table_index);
304 next0 = (t0->miss_next_index < L2_CLASSIFY_N_NEXT)?
305 t0->miss_next_index:next0;
310 hash0 = vnet_classify_hash_packet (t0, (u8 *) h0);
311 e0 = vnet_classify_find_entry (t0, (u8 *) h0, hash0, now);
314 vnet_buffer(b0)->l2_classify.opaque_index
316 vlib_buffer_advance (b0, e0->advance);
317 next0 = (e0->next_index < L2_CLASSIFY_N_NEXT)?
318 e0->next_index:next0;
327 if (PREDICT_FALSE(next0 == 0))
328 b0->error = node->errors[L2_CLASSIFY_ERROR_DROP];
330 if (PREDICT_FALSE (next0 == ~0))
333 // Remove ourself from the feature bitmap
334 feature_bitmap = vnet_buffer(b0)->l2.feature_bitmap
335 & ~L2INPUT_FEAT_CLASSIFY;
337 // save for next feature graph nodes
338 vnet_buffer(b0)->l2.feature_bitmap = feature_bitmap;
340 // Determine the next node
341 next0 = feat_bitmap_get_next_node_index(cm->feat_next_node_index,
345 if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE)
346 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
348 l2_classify_trace_t *t =
349 vlib_add_trace (vm, node, b0, sizeof (*t));
350 t->sw_if_index = vnet_buffer(b0)->sw_if_index[VLIB_RX];
351 t->table_index = table_index0;
352 t->next_index = next0;
353 t->session_offset = e0 ? vnet_classify_get_offset (t0, e0) : 0;
356 /* verify speculative enqueue, maybe switch current next frame */
357 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
358 to_next, n_left_to_next,
362 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
365 vlib_node_increment_counter (vm, node->node_index,
366 L2_CLASSIFY_ERROR_MISS,
368 vlib_node_increment_counter (vm, node->node_index,
369 L2_CLASSIFY_ERROR_HIT,
371 vlib_node_increment_counter (vm, node->node_index,
372 L2_CLASSIFY_ERROR_CHAIN_HIT,
374 return frame->n_vectors;
377 VLIB_REGISTER_NODE (l2_classify_node) = {
378 .function = l2_classify_node_fn,
379 .name = "l2-classify",
380 .vector_size = sizeof (u32),
381 .format_trace = format_l2_classify_trace,
382 .type = VLIB_NODE_TYPE_INTERNAL,
384 .n_errors = ARRAY_LEN(l2_classify_error_strings),
385 .error_strings = l2_classify_error_strings,
387 .runtime_data_bytes = sizeof (l2_classify_runtime_t),
389 .n_next_nodes = L2_CLASSIFY_N_NEXT,
391 /* edit / add dispositions here */
393 [L2_CLASSIFY_NEXT_DROP] = "error-drop",
394 [L2_CLASSIFY_NEXT_ETHERNET_INPUT] = "ethernet-input-not-l2",
395 [L2_CLASSIFY_NEXT_IP4_INPUT] = "ip4-input",
396 [L2_CLASSIFY_NEXT_IP6_INPUT] = "ip6-input",
397 [L2_CLASSIFY_NEXT_LI] = "li-hit",
401 clib_error_t *l2_classify_init (vlib_main_t *vm)
403 l2_classify_main_t * cm = &l2_classify_main;
404 l2_classify_runtime_t * rt;
406 rt = vlib_node_get_runtime_data (vm, l2_classify_node.index);
409 cm->vnet_main = vnet_get_main();
410 cm->vnet_classify_main = &vnet_classify_main;
412 // Initialize the feature next-node indexes
413 feat_bitmap_init_next_nodes(vm,
414 l2_classify_node.index,
416 l2input_get_feat_names(),
417 cm->feat_next_node_index);
419 rt->vcm = cm->vnet_classify_main;
424 VLIB_INIT_FUNCTION (l2_classify_init);
427 void vnet_l2_classify_enable_disable (u32 sw_if_index,
430 vlib_main_t * vm = vlib_get_main();
431 vnet_main_t * vnm = vnet_get_main();
434 set_int_l2_mode (vm, vnm, MODE_L2_CLASSIFY, sw_if_index,
437 set_int_l2_mode (vm, vnm, MODE_L3, sw_if_index,
441 int vnet_l2_classify_set_tables (u32 sw_if_index,
444 u32 other_table_index)
446 l2_classify_main_t * cm = &l2_classify_main;
447 vnet_classify_main_t * vcm = cm->vnet_classify_main;
449 /* Assume that we've validated sw_if_index in the API layer */
451 if (ip4_table_index != ~0 &&
452 pool_is_free_index (vcm->tables, ip4_table_index))
453 return VNET_API_ERROR_NO_SUCH_TABLE;
455 if (ip6_table_index != ~0 &&
456 pool_is_free_index (vcm->tables, ip6_table_index))
457 return VNET_API_ERROR_NO_SUCH_TABLE2;
459 if (other_table_index != ~0 &&
460 pool_is_free_index (vcm->tables, other_table_index))
461 return VNET_API_ERROR_NO_SUCH_TABLE3;
464 (cm->classify_table_index_by_sw_if_index[L2_CLASSIFY_TABLE_IP4],
468 (cm->classify_table_index_by_sw_if_index[L2_CLASSIFY_TABLE_IP6],
472 (cm->classify_table_index_by_sw_if_index[L2_CLASSIFY_TABLE_OTHER],
475 cm->classify_table_index_by_sw_if_index[L2_CLASSIFY_TABLE_IP4]
476 [sw_if_index] = ip4_table_index;
478 cm->classify_table_index_by_sw_if_index[L2_CLASSIFY_TABLE_IP6]
479 [sw_if_index] = ip6_table_index;
481 cm->classify_table_index_by_sw_if_index[L2_CLASSIFY_TABLE_OTHER]
482 [sw_if_index] = other_table_index;
487 static clib_error_t *
488 int_l2_classify_command_fn (vlib_main_t * vm,
489 unformat_input_t * input,
490 vlib_cli_command_t * cmd)
492 vnet_main_t * vnm = vnet_get_main();
493 u32 sw_if_index = ~0;
494 u32 ip4_table_index = ~0;
495 u32 ip6_table_index = ~0;
496 u32 other_table_index = ~0;
499 while (unformat_check_input(input) != UNFORMAT_END_OF_INPUT)
501 if (unformat (input, "intfc %U", unformat_vnet_sw_interface,
504 else if (unformat (input, "ip4-table %d", &ip4_table_index))
506 else if (unformat (input, "ip6-table %d", &ip6_table_index))
508 else if (unformat (input, "other-table %d", &other_table_index))
514 if (sw_if_index == ~0)
515 return clib_error_return (0, "interface must be specified");
518 if (ip4_table_index == ~0 && ip6_table_index == ~0
519 && other_table_index == ~0)
521 vlib_cli_output (vm, "L2 classification disabled");
522 vnet_l2_classify_enable_disable (sw_if_index, 0 /* enable */);
526 rv = vnet_l2_classify_set_tables (sw_if_index, ip4_table_index,
527 ip6_table_index, other_table_index);
531 vnet_l2_classify_enable_disable (sw_if_index, 1 /* enable */);
535 return clib_error_return (0, "vnet_l2_classify_set_tables: %d",
543 VLIB_CLI_COMMAND (int_l2_classify_cli, static) = {
544 .path = "set interface l2 classify",
546 "set interface l2 classify intfc <int> [ip4-table <n>]\n"
547 " [ip6-table <n>] [other-table <n>]",
548 .function = int_l2_classify_command_fn,