2 * l2_in_out_acl.c : layer 2 input/output acl processing
4 * Copyright (c) 2013,2018 Cisco and/or its affiliates.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
18 #include <vlib/vlib.h>
19 #include <vnet/vnet.h>
20 #include <vnet/ethernet/ethernet.h>
21 #include <vnet/ethernet/packet.h>
22 #include <vnet/ip/ip_packet.h>
23 #include <vnet/ip/ip4_packet.h>
24 #include <vnet/ip/ip6_packet.h>
26 #include <vnet/l2/l2_input.h>
27 #include <vnet/l2/l2_output.h>
28 #include <vnet/l2/feat_bitmap.h>
30 #include <vppinfra/error.h>
31 #include <vppinfra/hash.h>
32 #include <vppinfra/cache.h>
34 #include <vnet/classify/vnet_classify.h>
35 #include <vnet/classify/in_out_acl.h>
40 /* Next nodes for each feature */
41 u32 feat_next_node_index[IN_OUT_ACL_N_TABLE_GROUPS][32];
43 /* convenience variables */
44 vlib_main_t *vlib_main;
45 vnet_main_t *vnet_main;
46 } l2_in_out_acl_main_t;
54 } l2_in_out_acl_trace_t;
56 /* packet trace format function */
58 format_l2_in_out_acl_trace (u8 * s, u32 is_output, va_list * args)
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_in_out_acl_trace_t *t = va_arg (*args, l2_in_out_acl_trace_t *);
64 s = format (s, "%s: sw_if_index %d, next_index %d, table %d, offset %d",
65 is_output ? "OUTACL" : "INACL",
66 t->sw_if_index, t->next_index, t->table_index, t->offset);
71 format_l2_inacl_trace (u8 * s, va_list * args)
73 return format_l2_in_out_acl_trace (s, IN_OUT_ACL_INPUT_TABLE_GROUP, args);
77 format_l2_outacl_trace (u8 * s, va_list * args)
79 return format_l2_in_out_acl_trace (s, IN_OUT_ACL_OUTPUT_TABLE_GROUP, args);
82 extern l2_in_out_acl_main_t l2_in_out_acl_main;
84 #ifndef CLIB_MARCH_VARIANT
85 l2_in_out_acl_main_t l2_in_out_acl_main;
86 #endif /* CLIB_MARCH_VARIANT */
88 extern vlib_node_registration_t l2_inacl_node;
89 extern vlib_node_registration_t l2_outacl_node;
91 #define foreach_l2_inacl_error \
92 _(NONE, "valid input ACL packets") \
93 _(MISS, "input ACL misses") \
94 _(HIT, "input ACL hits") \
95 _(CHAIN_HIT, "input ACL hits after chain walk") \
96 _(TABLE_MISS, "input ACL table-miss drops") \
97 _(SESSION_DENY, "input ACL session deny drops")
99 #define foreach_l2_outacl_error \
100 _(NONE, "valid output ACL packets") \
101 _(MISS, "output ACL misses") \
102 _(HIT, "output ACL hits") \
103 _(CHAIN_HIT, "output ACL hits after chain walk") \
104 _(TABLE_MISS, "output ACL table-miss drops") \
105 _(SESSION_DENY, "output ACL session deny drops")
110 #define _(sym,str) L2_INACL_ERROR_##sym,
111 foreach_l2_inacl_error
116 static char *l2_inacl_error_strings[] = {
117 #define _(sym,string) string,
118 foreach_l2_inacl_error
124 #define _(sym,str) L2_OUTACL_ERROR_##sym,
125 foreach_l2_outacl_error
130 static char *l2_outacl_error_strings[] = {
131 #define _(sym,string) string,
132 foreach_l2_outacl_error
138 l2_in_out_acl_node_fn (vlib_main_t * vm,
139 vlib_node_runtime_t * node, vlib_frame_t * frame,
142 u32 n_left_from, *from, *to_next;
143 acl_next_index_t next_index;
144 l2_in_out_acl_main_t *msm = &l2_in_out_acl_main;
145 in_out_acl_main_t *am = &in_out_acl_main;
146 vnet_classify_main_t *vcm = am->vnet_classify_main;
147 in_out_acl_table_id_t tid = IN_OUT_ACL_TABLE_L2;
148 f64 now = vlib_time_now (vm);
153 from = vlib_frame_vector_args (frame);
154 n_left_from = frame->n_vectors; /* number of packets to process */
155 next_index = node->cached_next_index;
157 /* First pass: compute hashes */
158 while (n_left_from > 2)
160 vlib_buffer_t *b0, *b1;
163 u32 sw_if_index0, sw_if_index1;
164 u32 table_index0, table_index1;
165 vnet_classify_table_t *t0, *t1;
167 /* prefetch next iteration */
169 vlib_buffer_t *p1, *p2;
171 p1 = vlib_get_buffer (vm, from[1]);
172 p2 = vlib_get_buffer (vm, from[2]);
174 vlib_prefetch_buffer_header (p1, STORE);
175 clib_prefetch_store (p1->data);
176 vlib_prefetch_buffer_header (p2, STORE);
177 clib_prefetch_store (p2->data);
181 b0 = vlib_get_buffer (vm, bi0);
184 b1 = vlib_get_buffer (vm, bi1);
187 vnet_buffer (b0)->sw_if_index[is_output ? VLIB_TX : VLIB_RX];
189 am->classify_table_index_by_sw_if_index[is_output][tid][sw_if_index0];
192 vnet_buffer (b1)->sw_if_index[is_output ? VLIB_TX : VLIB_RX];
194 am->classify_table_index_by_sw_if_index[is_output][tid][sw_if_index1];
196 t0 = pool_elt_at_index (vcm->tables, table_index0);
198 t1 = pool_elt_at_index (vcm->tables, table_index1);
200 if (t0->current_data_flag == CLASSIFY_FLAG_USE_CURR_DATA)
201 h0 = (void *) vlib_buffer_get_current (b0) + t0->current_data_offset;
203 h0 = (void *) vlib_buffer_get_current (b0);
205 vnet_buffer (b0)->l2_classify.hash =
206 vnet_classify_hash_packet (t0, (u8 *) h0);
208 vnet_classify_prefetch_bucket (t0, vnet_buffer (b0)->l2_classify.hash);
210 if (t1->current_data_flag == CLASSIFY_FLAG_USE_CURR_DATA)
211 h1 = (void *) vlib_buffer_get_current (b1) + t1->current_data_offset;
213 h1 = (void *) vlib_buffer_get_current (b1);
215 vnet_buffer (b1)->l2_classify.hash =
216 vnet_classify_hash_packet (t1, (u8 *) h1);
218 vnet_classify_prefetch_bucket (t1, vnet_buffer (b1)->l2_classify.hash);
220 vnet_buffer (b0)->l2_classify.table_index = table_index0;
222 vnet_buffer (b1)->l2_classify.table_index = table_index1;
228 while (n_left_from > 0)
235 vnet_classify_table_t *t0;
238 b0 = vlib_get_buffer (vm, bi0);
241 vnet_buffer (b0)->sw_if_index[is_output ? VLIB_TX : VLIB_RX];
243 am->classify_table_index_by_sw_if_index[is_output][tid][sw_if_index0];
245 t0 = pool_elt_at_index (vcm->tables, table_index0);
247 if (t0->current_data_flag == CLASSIFY_FLAG_USE_CURR_DATA)
248 h0 = (void *) vlib_buffer_get_current (b0) + t0->current_data_offset;
250 h0 = (void *) vlib_buffer_get_current (b0);
252 vnet_buffer (b0)->l2_classify.hash =
253 vnet_classify_hash_packet (t0, (u8 *) h0);
255 vnet_buffer (b0)->l2_classify.table_index = table_index0;
256 vnet_classify_prefetch_bucket (t0, vnet_buffer (b0)->l2_classify.hash);
262 next_index = node->cached_next_index;
263 from = vlib_frame_vector_args (frame);
264 n_left_from = frame->n_vectors;
266 while (n_left_from > 0)
270 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
272 /* Not enough load/store slots to dual loop... */
273 while (n_left_from > 0 && n_left_to_next > 0)
277 u32 next0 = ACL_NEXT_INDEX_DENY;
279 vnet_classify_table_t *t0;
280 vnet_classify_entry_t *e0;
285 /* Stride 3 seems to work best */
286 if (PREDICT_TRUE (n_left_from > 3))
288 vlib_buffer_t *p1 = vlib_get_buffer (vm, from[3]);
289 vnet_classify_table_t *tp1;
293 table_index1 = vnet_buffer (p1)->l2_classify.table_index;
295 if (PREDICT_TRUE (table_index1 != ~0))
297 tp1 = pool_elt_at_index (vcm->tables, table_index1);
298 phash1 = vnet_buffer (p1)->l2_classify.hash;
299 vnet_classify_prefetch_entry (tp1, phash1);
303 /* speculatively enqueue b0 to the current next frame */
311 b0 = vlib_get_buffer (vm, bi0);
313 table_index0 = vnet_buffer (b0)->l2_classify.table_index;
317 vnet_buffer (b0)->l2_classify.opaque_index = ~0;
319 /* Determine the next node */
321 vnet_l2_feature_next (b0, msm->feat_next_node_index[is_output],
322 is_output ? L2OUTPUT_FEAT_ACL :
325 if (PREDICT_TRUE (table_index0 != ~0))
327 hash0 = vnet_buffer (b0)->l2_classify.hash;
328 t0 = pool_elt_at_index (vcm->tables, table_index0);
330 if (t0->current_data_flag == CLASSIFY_FLAG_USE_CURR_DATA)
332 (void *) vlib_buffer_get_current (b0) +
333 t0->current_data_offset;
335 h0 = (void *) vlib_buffer_get_current (b0);
337 e0 = vnet_classify_find_entry (t0, (u8 *) h0, hash0, now);
340 vnet_buffer (b0)->l2_classify.opaque_index
342 vlib_buffer_advance (b0, e0->advance);
344 next0 = (e0->next_index < ACL_NEXT_INDEX_N_NEXT) ?
345 e0->next_index : next0;
350 error0 = (next0 == ACL_NEXT_INDEX_DENY) ?
351 L2_OUTACL_ERROR_SESSION_DENY : L2_INACL_ERROR_NONE;
353 error0 = (next0 == ACL_NEXT_INDEX_DENY) ?
354 L2_OUTACL_ERROR_SESSION_DENY : L2_OUTACL_ERROR_NONE;
355 b0->error = node->errors[error0];
361 if (PREDICT_TRUE (t0->next_table_index != ~0))
362 t0 = pool_elt_at_index (vcm->tables,
363 t0->next_table_index);
367 (t0->miss_next_index <
368 ACL_NEXT_INDEX_N_NEXT) ? t0->miss_next_index :
374 error0 = (next0 == ACL_NEXT_INDEX_DENY) ?
375 L2_OUTACL_ERROR_TABLE_MISS :
376 L2_OUTACL_ERROR_NONE;
378 error0 = (next0 == ACL_NEXT_INDEX_DENY) ?
379 L2_INACL_ERROR_TABLE_MISS : L2_INACL_ERROR_NONE;
380 b0->error = node->errors[error0];
384 if (t0->current_data_flag ==
385 CLASSIFY_FLAG_USE_CURR_DATA)
387 (void *) vlib_buffer_get_current (b0) +
388 t0->current_data_offset;
390 h0 = (void *) vlib_buffer_get_current (b0);
392 hash0 = vnet_classify_hash_packet (t0, (u8 *) h0);
393 e0 = vnet_classify_find_entry
394 (t0, (u8 *) h0, hash0, now);
397 vlib_buffer_advance (b0, e0->advance);
398 next0 = (e0->next_index < ACL_NEXT_INDEX_N_NEXT) ?
399 e0->next_index : next0;
404 error0 = (next0 == ACL_NEXT_INDEX_DENY) ?
405 L2_OUTACL_ERROR_SESSION_DENY :
406 L2_OUTACL_ERROR_NONE;
408 error0 = (next0 == ACL_NEXT_INDEX_DENY) ?
409 L2_INACL_ERROR_SESSION_DENY :
411 b0->error = node->errors[error0];
418 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
419 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
421 l2_in_out_acl_trace_t *t =
422 vlib_add_trace (vm, node, b0, sizeof (*t));
424 vnet_buffer (b0)->sw_if_index[is_output ? VLIB_TX : VLIB_RX];
425 t->next_index = next0;
426 t->table_index = t0 ? t0 - vcm->tables : ~0;
427 t->offset = (t0 && e0) ? vnet_classify_get_offset (t0, e0) : ~0;
430 /* verify speculative enqueue, maybe switch current next frame */
431 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
432 to_next, n_left_to_next,
436 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
439 vlib_node_increment_counter (vm, node->node_index,
440 is_output ? L2_OUTACL_ERROR_MISS :
441 L2_INACL_ERROR_MISS, misses);
442 vlib_node_increment_counter (vm, node->node_index,
443 is_output ? L2_OUTACL_ERROR_HIT :
444 L2_INACL_ERROR_HIT, hits);
445 vlib_node_increment_counter (vm, node->node_index,
446 is_output ? L2_OUTACL_ERROR_CHAIN_HIT :
447 L2_INACL_ERROR_CHAIN_HIT, chain_hits);
448 return frame->n_vectors;
451 VLIB_NODE_FN (l2_inacl_node) (vlib_main_t * vm,
452 vlib_node_runtime_t * node,
453 vlib_frame_t * frame)
455 return l2_in_out_acl_node_fn (vm, node, frame,
456 IN_OUT_ACL_INPUT_TABLE_GROUP);
459 VLIB_NODE_FN (l2_outacl_node) (vlib_main_t * vm,
460 vlib_node_runtime_t * node,
461 vlib_frame_t * frame)
463 return l2_in_out_acl_node_fn (vm, node, frame,
464 IN_OUT_ACL_OUTPUT_TABLE_GROUP);
468 VLIB_REGISTER_NODE (l2_inacl_node) = {
469 .name = "l2-input-acl",
470 .vector_size = sizeof (u32),
471 .format_trace = format_l2_inacl_trace,
472 .type = VLIB_NODE_TYPE_INTERNAL,
474 .n_errors = ARRAY_LEN(l2_inacl_error_strings),
475 .error_strings = l2_inacl_error_strings,
477 .n_next_nodes = ACL_NEXT_INDEX_N_NEXT,
479 /* edit / add dispositions here */
481 [ACL_NEXT_INDEX_DENY] = "error-drop",
485 VLIB_REGISTER_NODE (l2_outacl_node) = {
486 .name = "l2-output-acl",
487 .vector_size = sizeof (u32),
488 .format_trace = format_l2_outacl_trace,
489 .type = VLIB_NODE_TYPE_INTERNAL,
491 .n_errors = ARRAY_LEN(l2_outacl_error_strings),
492 .error_strings = l2_outacl_error_strings,
494 .n_next_nodes = ACL_NEXT_INDEX_N_NEXT,
496 /* edit / add dispositions here */
498 [ACL_NEXT_INDEX_DENY] = "error-drop",
504 #ifndef CLIB_MARCH_VARIANT
506 l2_in_out_acl_init (vlib_main_t * vm)
508 l2_in_out_acl_main_t *mp = &l2_in_out_acl_main;
511 mp->vnet_main = vnet_get_main ();
513 /* Initialize the feature next-node indexes */
514 feat_bitmap_init_next_nodes (vm,
517 l2input_get_feat_names (),
518 mp->feat_next_node_index
519 [IN_OUT_ACL_INPUT_TABLE_GROUP]);
520 feat_bitmap_init_next_nodes (vm, l2_outacl_node.index, L2OUTPUT_N_FEAT,
521 l2output_get_feat_names (),
522 mp->feat_next_node_index
523 [IN_OUT_ACL_OUTPUT_TABLE_GROUP]);
528 VLIB_INIT_FUNCTION (l2_in_out_acl_init);
529 #endif /* CLIB_MARCH_VARIANT */
532 * fd.io coding-style-patch-verification: ON
535 * eval: (c-set-style "gnu")