2 * l2_input_acl.c : layer 2 input acl processing
4 * Copyright (c) 2013 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/pg/pg.h>
21 #include <vnet/ethernet/ethernet.h>
22 #include <vnet/ethernet/packet.h>
23 #include <vnet/ip/ip_packet.h>
24 #include <vnet/ip/ip4_packet.h>
25 #include <vnet/ip/ip6_packet.h>
27 #include <vnet/l2/l2_input.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/input_acl.h>
40 /* Next nodes for each feature */
41 u32 feat_next_node_index[32];
43 /* convenience variables */
44 vlib_main_t *vlib_main;
45 vnet_main_t *vnet_main;
56 /* packet trace format function */
58 format_l2_inacl_trace (u8 * s, 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_inacl_trace_t *t = va_arg (*args, l2_inacl_trace_t *);
64 s = format (s, "INACL: sw_if_index %d, next_index %d, table %d, offset %d",
65 t->sw_if_index, t->next_index, t->table_index, t->offset);
69 l2_inacl_main_t l2_inacl_main;
71 static vlib_node_registration_t l2_inacl_node;
73 #define foreach_l2_inacl_error \
74 _(NONE, "valid input ACL packets") \
75 _(MISS, "input ACL misses") \
76 _(HIT, "input ACL hits") \
77 _(CHAIN_HIT, "input ACL hits after chain walk") \
78 _(TABLE_MISS, "input ACL table-miss drops") \
79 _(SESSION_DENY, "input ACL session deny drops")
84 #define _(sym,str) L2_INACL_ERROR_##sym,
85 foreach_l2_inacl_error
90 static char *l2_inacl_error_strings[] = {
91 #define _(sym,string) string,
92 foreach_l2_inacl_error
97 l2_inacl_node_fn (vlib_main_t * vm,
98 vlib_node_runtime_t * node, vlib_frame_t * frame)
100 u32 n_left_from, *from, *to_next;
101 acl_next_index_t next_index;
102 l2_inacl_main_t *msm = &l2_inacl_main;
103 input_acl_main_t *am = &input_acl_main;
104 vnet_classify_main_t *vcm = am->vnet_classify_main;
105 input_acl_table_id_t tid = INPUT_ACL_TABLE_L2;
106 f64 now = vlib_time_now (vm);
111 from = vlib_frame_vector_args (frame);
112 n_left_from = frame->n_vectors; /* number of packets to process */
113 next_index = node->cached_next_index;
115 /* First pass: compute hashes */
116 while (n_left_from > 2)
118 vlib_buffer_t *b0, *b1;
121 u32 sw_if_index0, sw_if_index1;
122 u32 table_index0, table_index1;
123 vnet_classify_table_t *t0, *t1;
125 /* prefetch next iteration */
127 vlib_buffer_t *p1, *p2;
129 p1 = vlib_get_buffer (vm, from[1]);
130 p2 = vlib_get_buffer (vm, from[2]);
132 vlib_prefetch_buffer_header (p1, STORE);
133 CLIB_PREFETCH (p1->data, CLIB_CACHE_LINE_BYTES, STORE);
134 vlib_prefetch_buffer_header (p2, STORE);
135 CLIB_PREFETCH (p2->data, CLIB_CACHE_LINE_BYTES, STORE);
139 b0 = vlib_get_buffer (vm, bi0);
143 b1 = vlib_get_buffer (vm, bi1);
146 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
148 am->classify_table_index_by_sw_if_index[tid][sw_if_index0];
150 sw_if_index1 = vnet_buffer (b1)->sw_if_index[VLIB_RX];
152 am->classify_table_index_by_sw_if_index[tid][sw_if_index1];
154 t0 = pool_elt_at_index (vcm->tables, table_index0);
156 t1 = pool_elt_at_index (vcm->tables, table_index1);
158 vnet_buffer (b0)->l2_classify.hash =
159 vnet_classify_hash_packet (t0, (u8 *) h0);
161 vnet_classify_prefetch_bucket (t0, vnet_buffer (b0)->l2_classify.hash);
163 vnet_buffer (b1)->l2_classify.hash =
164 vnet_classify_hash_packet (t1, (u8 *) h1);
166 vnet_classify_prefetch_bucket (t1, vnet_buffer (b1)->l2_classify.hash);
168 vnet_buffer (b0)->l2_classify.table_index = table_index0;
170 vnet_buffer (b1)->l2_classify.table_index = table_index1;
176 while (n_left_from > 0)
183 vnet_classify_table_t *t0;
186 b0 = vlib_get_buffer (vm, bi0);
189 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
191 am->classify_table_index_by_sw_if_index[tid][sw_if_index0];
193 t0 = pool_elt_at_index (vcm->tables, table_index0);
194 vnet_buffer (b0)->l2_classify.hash =
195 vnet_classify_hash_packet (t0, (u8 *) h0);
197 vnet_buffer (b0)->l2_classify.table_index = table_index0;
198 vnet_classify_prefetch_bucket (t0, vnet_buffer (b0)->l2_classify.hash);
204 next_index = node->cached_next_index;
205 from = vlib_frame_vector_args (frame);
206 n_left_from = frame->n_vectors;
208 while (n_left_from > 0)
212 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
214 /* Not enough load/store slots to dual loop... */
215 while (n_left_from > 0 && n_left_to_next > 0)
219 u32 next0 = ACL_NEXT_INDEX_DENY;
221 vnet_classify_table_t *t0;
222 vnet_classify_entry_t *e0;
227 /* Stride 3 seems to work best */
228 if (PREDICT_TRUE (n_left_from > 3))
230 vlib_buffer_t *p1 = vlib_get_buffer (vm, from[3]);
231 vnet_classify_table_t *tp1;
235 table_index1 = vnet_buffer (p1)->l2_classify.table_index;
237 if (PREDICT_TRUE (table_index1 != ~0))
239 tp1 = pool_elt_at_index (vcm->tables, table_index1);
240 phash1 = vnet_buffer (p1)->l2_classify.hash;
241 vnet_classify_prefetch_entry (tp1, phash1);
245 /* speculatively enqueue b0 to the current next frame */
253 b0 = vlib_get_buffer (vm, bi0);
255 table_index0 = vnet_buffer (b0)->l2_classify.table_index;
259 /* Feature bitmap update */
260 vnet_buffer (b0)->l2.feature_bitmap &= ~L2INPUT_FEAT_ACL;
262 vnet_buffer (b0)->l2_classify.opaque_index = ~0;
263 /* Determine the next node */
264 next0 = feat_bitmap_get_next_node_index (msm->feat_next_node_index,
268 if (PREDICT_TRUE (table_index0 != ~0))
270 hash0 = vnet_buffer (b0)->l2_classify.hash;
271 t0 = pool_elt_at_index (vcm->tables, table_index0);
273 e0 = vnet_classify_find_entry (t0, (u8 *) h0, hash0, now);
276 vnet_buffer (b0)->l2_classify.opaque_index
278 vlib_buffer_advance (b0, e0->advance);
280 next0 = (e0->next_index < ACL_NEXT_INDEX_N_NEXT) ?
281 e0->next_index : next0;
285 error0 = (next0 == ACL_NEXT_INDEX_DENY) ?
286 L2_INACL_ERROR_SESSION_DENY : L2_INACL_ERROR_NONE;
287 b0->error = node->errors[error0];
293 if (PREDICT_TRUE (t0->next_table_index != ~0))
294 t0 = pool_elt_at_index (vcm->tables,
295 t0->next_table_index);
299 (t0->miss_next_index <
300 ACL_NEXT_INDEX_N_NEXT) ? t0->miss_next_index :
305 error0 = (next0 == ACL_NEXT_INDEX_DENY) ?
306 L2_INACL_ERROR_TABLE_MISS : L2_INACL_ERROR_NONE;
307 b0->error = node->errors[error0];
311 hash0 = vnet_classify_hash_packet (t0, (u8 *) h0);
312 e0 = vnet_classify_find_entry
313 (t0, (u8 *) h0, hash0, now);
316 vlib_buffer_advance (b0, e0->advance);
317 next0 = (e0->next_index < ACL_NEXT_INDEX_N_NEXT) ?
318 e0->next_index : next0;
322 error0 = (next0 == ACL_NEXT_INDEX_DENY) ?
323 L2_INACL_ERROR_SESSION_DENY : L2_INACL_ERROR_NONE;
324 b0->error = node->errors[error0];
331 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
332 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
334 l2_inacl_trace_t *t =
335 vlib_add_trace (vm, node, b0, sizeof (*t));
336 t->sw_if_index = vnet_buffer (b0)->sw_if_index[VLIB_RX];
337 t->next_index = next0;
338 t->table_index = t0 ? t0 - vcm->tables : ~0;
339 t->offset = (t0 && e0) ? vnet_classify_get_offset (t0, e0) : ~0;
342 /* verify speculative enqueue, maybe switch current next frame */
343 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
344 to_next, n_left_to_next,
348 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
351 vlib_node_increment_counter (vm, node->node_index,
352 L2_INACL_ERROR_MISS, misses);
353 vlib_node_increment_counter (vm, node->node_index,
354 L2_INACL_ERROR_HIT, hits);
355 vlib_node_increment_counter (vm, node->node_index,
356 L2_INACL_ERROR_CHAIN_HIT, chain_hits);
357 return frame->n_vectors;
361 VLIB_REGISTER_NODE (l2_inacl_node,static) = {
362 .function = l2_inacl_node_fn,
363 .name = "l2-input-acl",
364 .vector_size = sizeof (u32),
365 .format_trace = format_l2_inacl_trace,
366 .type = VLIB_NODE_TYPE_INTERNAL,
368 .n_errors = ARRAY_LEN(l2_inacl_error_strings),
369 .error_strings = l2_inacl_error_strings,
371 .n_next_nodes = ACL_NEXT_INDEX_N_NEXT,
373 /* edit / add dispositions here */
375 [ACL_NEXT_INDEX_DENY] = "error-drop",
380 VLIB_NODE_FUNCTION_MULTIARCH (l2_inacl_node, l2_inacl_node_fn)
381 clib_error_t *l2_inacl_init (vlib_main_t * vm)
383 l2_inacl_main_t *mp = &l2_inacl_main;
386 mp->vnet_main = vnet_get_main ();
388 /* Initialize the feature next-node indexes */
389 feat_bitmap_init_next_nodes (vm,
392 l2input_get_feat_names (),
393 mp->feat_next_node_index);
398 VLIB_INIT_FUNCTION (l2_inacl_init);
401 * fd.io coding-style-patch-verification: ON
404 * eval: (c-set-style "gnu")