FD.io VPP  v16.09
Vector Packet Processing
l2_input_acl.c
Go to the documentation of this file.
1 /*
2  * l2_input_acl.c : layer 2 input acl processing
3  *
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:
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  */
17 
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>
26 #include <vlib/cli.h>
27 #include <vnet/l2/l2_input.h>
28 #include <vnet/l2/feat_bitmap.h>
29 
30 #include <vppinfra/error.h>
31 #include <vppinfra/hash.h>
32 #include <vppinfra/cache.h>
33 
36 
37 typedef struct
38 {
39 
40  /* Next nodes for each feature */
41  u32 feat_next_node_index[32];
42 
43  /* convenience variables */
47 
48 typedef struct
49 {
55 
56 /* packet trace format function */
57 static u8 *
58 format_l2_inacl_trace (u8 * s, va_list * args)
59 {
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 *);
63 
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);
66  return s;
67 }
68 
70 
72 
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")
80 
81 
82 typedef enum
83 {
84 #define _(sym,str) L2_INACL_ERROR_##sym,
86 #undef _
89 
90 static char *l2_inacl_error_strings[] = {
91 #define _(sym,string) string,
93 #undef _
94 };
95 
96 static uword
98  vlib_node_runtime_t * node, vlib_frame_t * frame)
99 {
100  u32 n_left_from, *from, *to_next;
101  acl_next_index_t next_index;
106  f64 now = vlib_time_now (vm);
107  u32 hits = 0;
108  u32 misses = 0;
109  u32 chain_hits = 0;
110 
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;
114 
115  /* First pass: compute hashes */
116  while (n_left_from > 2)
117  {
118  vlib_buffer_t *b0, *b1;
119  u32 bi0, bi1;
120  u8 *h0, *h1;
121  u32 sw_if_index0, sw_if_index1;
122  u32 table_index0, table_index1;
123  vnet_classify_table_t *t0, *t1;
124 
125  /* prefetch next iteration */
126  {
127  vlib_buffer_t *p1, *p2;
128 
129  p1 = vlib_get_buffer (vm, from[1]);
130  p2 = vlib_get_buffer (vm, from[2]);
131 
132  vlib_prefetch_buffer_header (p1, STORE);
134  vlib_prefetch_buffer_header (p2, STORE);
136  }
137 
138  bi0 = from[0];
139  b0 = vlib_get_buffer (vm, bi0);
140  h0 = b0->data;
141 
142  bi1 = from[1];
143  b1 = vlib_get_buffer (vm, bi1);
144  h1 = b1->data;
145 
146  sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
147  table_index0 =
148  am->classify_table_index_by_sw_if_index[tid][sw_if_index0];
149 
150  sw_if_index1 = vnet_buffer (b1)->sw_if_index[VLIB_RX];
151  table_index1 =
152  am->classify_table_index_by_sw_if_index[tid][sw_if_index1];
153 
154  t0 = pool_elt_at_index (vcm->tables, table_index0);
155 
156  t1 = pool_elt_at_index (vcm->tables, table_index1);
157 
158  vnet_buffer (b0)->l2_classify.hash =
159  vnet_classify_hash_packet (t0, (u8 *) h0);
160 
161  vnet_classify_prefetch_bucket (t0, vnet_buffer (b0)->l2_classify.hash);
162 
163  vnet_buffer (b1)->l2_classify.hash =
164  vnet_classify_hash_packet (t1, (u8 *) h1);
165 
166  vnet_classify_prefetch_bucket (t1, vnet_buffer (b1)->l2_classify.hash);
167 
168  vnet_buffer (b0)->l2_classify.table_index = table_index0;
169 
170  vnet_buffer (b1)->l2_classify.table_index = table_index1;
171 
172  from += 2;
173  n_left_from -= 2;
174  }
175 
176  while (n_left_from > 0)
177  {
178  vlib_buffer_t *b0;
179  u32 bi0;
180  u8 *h0;
181  u32 sw_if_index0;
182  u32 table_index0;
184 
185  bi0 = from[0];
186  b0 = vlib_get_buffer (vm, bi0);
187  h0 = b0->data;
188 
189  sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
190  table_index0 =
191  am->classify_table_index_by_sw_if_index[tid][sw_if_index0];
192 
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);
196 
197  vnet_buffer (b0)->l2_classify.table_index = table_index0;
198  vnet_classify_prefetch_bucket (t0, vnet_buffer (b0)->l2_classify.hash);
199 
200  from++;
201  n_left_from--;
202  }
203 
204  next_index = node->cached_next_index;
205  from = vlib_frame_vector_args (frame);
206  n_left_from = frame->n_vectors;
207 
208  while (n_left_from > 0)
209  {
210  u32 n_left_to_next;
211 
212  vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
213 
214  /* Not enough load/store slots to dual loop... */
215  while (n_left_from > 0 && n_left_to_next > 0)
216  {
217  u32 bi0;
218  vlib_buffer_t *b0;
219  u32 next0 = ACL_NEXT_INDEX_DENY;
220  u32 table_index0;
222  vnet_classify_entry_t *e0;
223  u64 hash0;
224  u8 *h0;
225  u8 error0;
226 
227  /* Stride 3 seems to work best */
228  if (PREDICT_TRUE (n_left_from > 3))
229  {
230  vlib_buffer_t *p1 = vlib_get_buffer (vm, from[3]);
232  u32 table_index1;
233  u64 phash1;
234 
235  table_index1 = vnet_buffer (p1)->l2_classify.table_index;
236 
237  if (PREDICT_TRUE (table_index1 != ~0))
238  {
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);
242  }
243  }
244 
245  /* speculatively enqueue b0 to the current next frame */
246  bi0 = from[0];
247  to_next[0] = bi0;
248  from += 1;
249  to_next += 1;
250  n_left_from -= 1;
251  n_left_to_next -= 1;
252 
253  b0 = vlib_get_buffer (vm, bi0);
254  h0 = b0->data;
255  table_index0 = vnet_buffer (b0)->l2_classify.table_index;
256  e0 = 0;
257  t0 = 0;
258 
259  /* Feature bitmap update */
260  vnet_buffer (b0)->l2.feature_bitmap &= ~L2INPUT_FEAT_ACL;
261 
262  vnet_buffer (b0)->l2_classify.opaque_index = ~0;
263  /* Determine the next node */
265  vnet_buffer (b0)->
266  l2.feature_bitmap);
267 
268  if (PREDICT_TRUE (table_index0 != ~0))
269  {
270  hash0 = vnet_buffer (b0)->l2_classify.hash;
271  t0 = pool_elt_at_index (vcm->tables, table_index0);
272 
273  e0 = vnet_classify_find_entry (t0, (u8 *) h0, hash0, now);
274  if (e0)
275  {
276  vnet_buffer (b0)->l2_classify.opaque_index
277  = e0->opaque_index;
278  vlib_buffer_advance (b0, e0->advance);
279 
280  next0 = (e0->next_index < ACL_NEXT_INDEX_N_NEXT) ?
281  e0->next_index : next0;
282 
283  hits++;
284 
285  error0 = (next0 == ACL_NEXT_INDEX_DENY) ?
286  L2_INACL_ERROR_SESSION_DENY : L2_INACL_ERROR_NONE;
287  b0->error = node->errors[error0];
288  }
289  else
290  {
291  while (1)
292  {
293  if (PREDICT_TRUE (t0->next_table_index != ~0))
294  t0 = pool_elt_at_index (vcm->tables,
295  t0->next_table_index);
296  else
297  {
298  next0 =
299  (t0->miss_next_index <
301  next0;
302 
303  misses++;
304 
305  error0 = (next0 == ACL_NEXT_INDEX_DENY) ?
306  L2_INACL_ERROR_TABLE_MISS : L2_INACL_ERROR_NONE;
307  b0->error = node->errors[error0];
308  break;
309  }
310 
311  hash0 = vnet_classify_hash_packet (t0, (u8 *) h0);
313  (t0, (u8 *) h0, hash0, now);
314  if (e0)
315  {
316  vlib_buffer_advance (b0, e0->advance);
317  next0 = (e0->next_index < ACL_NEXT_INDEX_N_NEXT) ?
318  e0->next_index : next0;
319  hits++;
320  chain_hits++;
321 
322  error0 = (next0 == ACL_NEXT_INDEX_DENY) ?
323  L2_INACL_ERROR_SESSION_DENY : L2_INACL_ERROR_NONE;
324  b0->error = node->errors[error0];
325  break;
326  }
327  }
328  }
329  }
330 
332  && (b0->flags & VLIB_BUFFER_IS_TRACED)))
333  {
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;
340  }
341 
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,
345  bi0, next0);
346  }
347 
348  vlib_put_next_frame (vm, node, next_index, n_left_to_next);
349  }
350 
352  L2_INACL_ERROR_MISS, misses);
354  L2_INACL_ERROR_HIT, hits);
356  L2_INACL_ERROR_CHAIN_HIT, chain_hits);
357  return frame->n_vectors;
358 }
359 
360 /* *INDENT-OFF* */
362  .function = l2_inacl_node_fn,
363  .name = "l2-input-acl",
364  .vector_size = sizeof (u32),
365  .format_trace = format_l2_inacl_trace,
367 
368  .n_errors = ARRAY_LEN(l2_inacl_error_strings),
369  .error_strings = l2_inacl_error_strings,
370 
371  .n_next_nodes = ACL_NEXT_INDEX_N_NEXT,
372 
373  /* edit / add dispositions here */
374  .next_nodes = {
375  [ACL_NEXT_INDEX_DENY] = "error-drop",
376  },
377 };
378 /* *INDENT-ON* */
379 
382 {
384 
385  mp->vlib_main = vm;
386  mp->vnet_main = vnet_get_main ();
387 
388  /* Initialize the feature next-node indexes */
390  l2_inacl_node.index,
394 
395  return 0;
396 }
397 
399 
400 /*
401  * fd.io coding-style-patch-verification: ON
402  *
403  * Local Variables:
404  * eval: (c-set-style "gnu")
405  * End:
406  */
u64 vnet_classify_hash_packet(vnet_classify_table_t *t, u8 *h)
void vlib_put_next_frame(vlib_main_t *vm, vlib_node_runtime_t *r, u32 next_index, u32 n_vectors_left)
Release pointer to next frame vector data.
Definition: main.c:457
acl_next_index_t
Definition: input_acl.h:30
#define CLIB_UNUSED(x)
Definition: clib.h:79
bad routing header type(not 4)") sr_error (NO_MORE_SEGMENTS
u32 * classify_table_index_by_sw_if_index[INPUT_ACL_N_TABLES]
Definition: input_acl.h:38
#define PREDICT_TRUE(x)
Definition: clib.h:98
static f64 vlib_time_now(vlib_main_t *vm)
Definition: main.h:182
struct _vlib_node_registration vlib_node_registration_t
static u8 * format_l2_inacl_trace(u8 *s, va_list *args)
Definition: l2_input_acl.c:58
vlib_main_t * vlib_main
Definition: l2_input_acl.c:44
vlib_error_t * errors
Definition: node.h:418
vnet_classify_main_t * vnet_classify_main
Definition: input_acl.h:43
vnet_main_t * vnet_get_main(void)
Definition: misc.c:45
l2_inacl_main_t l2_inacl_main
Definition: l2_input_acl.c:69
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:111
static char * l2_inacl_error_strings[]
Definition: l2_input_acl.c:90
unsigned long u64
Definition: types.h:89
static void vnet_classify_prefetch_bucket(vnet_classify_table_t *t, u64 hash)
clib_error_t * l2_inacl_init(vlib_main_t *vm)
Definition: l2_input_acl.c:381
static void vnet_classify_prefetch_entry(vnet_classify_table_t *t, u64 hash)
#define pool_elt_at_index(p, i)
Returns pointer to element at given index.
Definition: pool.h:369
#define foreach_l2_inacl_error
Definition: l2_input_acl.c:73
#define PREDICT_FALSE(x)
Definition: clib.h:97
l2_inacl_error_t
Definition: l2_input_acl.c:82
static u32 feat_bitmap_get_next_node_index(u32 *next_nodes, u32 bitmap)
Return the graph node index for the feature corresponding to the first set bit in the bitmap...
Definition: feat_bitmap.h:79
#define vlib_validate_buffer_enqueue_x1(vm, node, next_index, to_next, n_left_to_next, bi0, next0)
Finish enqueueing one buffer forward in the graph.
Definition: buffer_node.h:130
#define vlib_get_next_frame(vm, node, next_index, vectors, n_vectors_left)
Get pointer to next frame vector data by (vlib_node_runtime_t, next_index).
Definition: node_funcs.h:348
vlib_error_t error
Error code for buffers to be enqueued to error handler.
Definition: buffer.h:118
static void vlib_node_increment_counter(vlib_main_t *vm, u32 node_index, u32 counter_index, u64 increment)
Definition: node_funcs.h:1111
static uword vnet_classify_get_offset(vnet_classify_table_t *t, vnet_classify_entry_t *v)
u32 feat_next_node_index[32]
Definition: l2_input_acl.c:41
u16 n_vectors
Definition: node.h:344
#define CLIB_PREFETCH(addr, size, type)
Definition: cache.h:82
static void feat_bitmap_init_next_nodes(vlib_main_t *vm, u32 node_index, u32 num_features, char **feat_names, u32 *next_nodes)
Initialize the feature next-node indexes of a graph node.
Definition: feat_bitmap.h:43
static void vlib_buffer_advance(vlib_buffer_t *b, word l)
Advance current data pointer by the supplied (signed!) amount.
Definition: buffer.h:200
#define ARRAY_LEN(x)
Definition: clib.h:59
char ** l2input_get_feat_names(void)
Return an array of strings containing graph node names of each feature.
Definition: l2_input.c:46
struct _vnet_classify_main vnet_classify_main_t
Definition: vnet_classify.h:50
input_acl_main_t input_acl_main
Definition: input_acl.c:19
u16 cached_next_index
Definition: node.h:462
input_acl_table_id_t
Definition: input_acl.h:23
unsigned int u32
Definition: types.h:88
#define vnet_buffer(b)
Definition: buffer.h:335
u8 * format(u8 *s, char *fmt,...)
Definition: format.c:418
#define VLIB_NODE_FLAG_TRACE
Definition: node.h:259
#define VLIB_BUFFER_IS_TRACED
Definition: buffer.h:93
u64 uword
Definition: types.h:112
static void * vlib_add_trace(vlib_main_t *vm, vlib_node_runtime_t *r, vlib_buffer_t *b, u32 n_data_bytes)
Definition: trace_funcs.h:55
double f64
Definition: types.h:142
unsigned char u8
Definition: types.h:56
static void * vlib_frame_vector_args(vlib_frame_t *f)
Get pointer to frame vector data.
Definition: node_funcs.h:251
vnet_main_t * vnet_main
Definition: l2_input_acl.c:45
static uword l2_inacl_node_fn(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
Definition: l2_input_acl.c:97
static vlib_node_registration_t l2_inacl_node
(constructor) VLIB_REGISTER_NODE (l2_inacl_node)
Definition: l2_input_acl.c:71
#define vlib_prefetch_buffer_header(b, type)
Prefetch buffer metadata.
Definition: buffer.h:163
#define VLIB_NODE_FUNCTION_MULTIARCH(node, fn)
Definition: node.h:158
#define VLIB_REGISTER_NODE(x,...)
Definition: node.h:143
u8 data[0]
Packet data.
Definition: buffer.h:151
#define CLIB_CACHE_LINE_BYTES
Definition: cache.h:67
u32 flags
buffer flags: VLIB_BUFFER_IS_TRACED: trace this buffer.
Definition: buffer.h:85
static vlib_buffer_t * vlib_get_buffer(vlib_main_t *vm, u32 buffer_index)
Translate buffer index into buffer pointer.
Definition: buffer_funcs.h:69
vnet_classify_entry_t * vnet_classify_find_entry(vnet_classify_table_t *t, u8 *h, u64 hash, f64 now)
Definition: defs.h:46