FD.io VPP  v18.01.2-1-g9b554f3
Vector Packet Processing
ip_input_acl.c
Go to the documentation of this file.
1 /*
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:
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
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.
14  */
15 #include <vnet/ip/ip.h>
18 
19 typedef struct
20 {
26 
27 /* packet trace format function */
28 static u8 *
29 format_ip_inacl_trace (u8 * s, va_list * args)
30 {
31  CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
32  CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
33  ip_inacl_trace_t *t = va_arg (*args, ip_inacl_trace_t *);
34 
35  s = format (s, "INACL: sw_if_index %d, next_index %d, table %d, offset %d",
36  t->sw_if_index, t->next_index, t->table_index, t->offset);
37  return s;
38 }
39 
42 
43 #define foreach_ip_inacl_error \
44 _(MISS, "input ACL misses") \
45 _(HIT, "input ACL hits") \
46 _(CHAIN_HIT, "input ACL hits after chain walk")
47 
48 typedef enum
49 {
50 #define _(sym,str) IP_INACL_ERROR_##sym,
52 #undef _
55 
56 static char *ip_inacl_error_strings[] = {
57 #define _(sym,string) string,
59 #undef _
60 };
61 
62 static inline uword
64  vlib_node_runtime_t * node, vlib_frame_t * frame, int is_ip4)
65 {
66  u32 n_left_from, *from, *to_next;
67  acl_next_index_t next_index;
70  f64 now = vlib_time_now (vm);
71  u32 hits = 0;
72  u32 misses = 0;
73  u32 chain_hits = 0;
75  vlib_node_runtime_t *error_node;
76  u32 n_next_nodes;
77 
78  n_next_nodes = node->n_next_nodes;
79 
80  if (is_ip4)
81  {
82  tid = INPUT_ACL_TABLE_IP4;
83  error_node = vlib_node_get_runtime (vm, ip4_input_node.index);
84  }
85  else
86  {
87  tid = INPUT_ACL_TABLE_IP6;
88  error_node = vlib_node_get_runtime (vm, ip6_input_node.index);
89  }
90 
91  from = vlib_frame_vector_args (frame);
92  n_left_from = frame->n_vectors;
93 
94  /* First pass: compute hashes */
95 
96  while (n_left_from > 2)
97  {
98  vlib_buffer_t *b0, *b1;
99  u32 bi0, bi1;
100  u8 *h0, *h1;
101  u32 sw_if_index0, sw_if_index1;
102  u32 table_index0, table_index1;
103  vnet_classify_table_t *t0, *t1;
104 
105  /* prefetch next iteration */
106  {
107  vlib_buffer_t *p1, *p2;
108 
109  p1 = vlib_get_buffer (vm, from[1]);
110  p2 = vlib_get_buffer (vm, from[2]);
111 
112  vlib_prefetch_buffer_header (p1, STORE);
114  vlib_prefetch_buffer_header (p2, STORE);
116  }
117 
118  bi0 = from[0];
119  b0 = vlib_get_buffer (vm, bi0);
120 
121  bi1 = from[1];
122  b1 = vlib_get_buffer (vm, bi1);
123 
124  sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
125  table_index0 =
126  am->classify_table_index_by_sw_if_index[tid][sw_if_index0];
127 
128  sw_if_index1 = vnet_buffer (b1)->sw_if_index[VLIB_RX];
129  table_index1 =
130  am->classify_table_index_by_sw_if_index[tid][sw_if_index1];
131 
132  t0 = pool_elt_at_index (vcm->tables, table_index0);
133 
134  t1 = pool_elt_at_index (vcm->tables, table_index1);
135 
137  h0 = (void *) vlib_buffer_get_current (b0) + t0->current_data_offset;
138  else
139  h0 = b0->data;
140 
141  vnet_buffer (b0)->l2_classify.hash =
142  vnet_classify_hash_packet (t0, (u8 *) h0);
143 
144  vnet_classify_prefetch_bucket (t0, vnet_buffer (b0)->l2_classify.hash);
145 
147  h1 = (void *) vlib_buffer_get_current (b1) + t1->current_data_offset;
148  else
149  h1 = b1->data;
150 
151  vnet_buffer (b1)->l2_classify.hash =
152  vnet_classify_hash_packet (t1, (u8 *) h1);
153 
154  vnet_classify_prefetch_bucket (t1, vnet_buffer (b1)->l2_classify.hash);
155 
156  vnet_buffer (b0)->l2_classify.table_index = table_index0;
157 
158  vnet_buffer (b1)->l2_classify.table_index = table_index1;
159 
160  from += 2;
161  n_left_from -= 2;
162  }
163 
164  while (n_left_from > 0)
165  {
166  vlib_buffer_t *b0;
167  u32 bi0;
168  u8 *h0;
169  u32 sw_if_index0;
170  u32 table_index0;
172 
173  bi0 = from[0];
174  b0 = vlib_get_buffer (vm, bi0);
175 
176  sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
177  table_index0 =
178  am->classify_table_index_by_sw_if_index[tid][sw_if_index0];
179 
180  t0 = pool_elt_at_index (vcm->tables, table_index0);
181 
183  h0 = (void *) vlib_buffer_get_current (b0) + t0->current_data_offset;
184  else
185  h0 = b0->data;
186 
187  vnet_buffer (b0)->l2_classify.hash =
188  vnet_classify_hash_packet (t0, (u8 *) h0);
189 
190  vnet_buffer (b0)->l2_classify.table_index = table_index0;
191  vnet_classify_prefetch_bucket (t0, vnet_buffer (b0)->l2_classify.hash);
192 
193  from++;
194  n_left_from--;
195  }
196 
197  next_index = node->cached_next_index;
198  from = vlib_frame_vector_args (frame);
199  n_left_from = frame->n_vectors;
200 
201  while (n_left_from > 0)
202  {
203  u32 n_left_to_next;
204 
205  vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
206 
207  /* Not enough load/store slots to dual loop... */
208  while (n_left_from > 0 && n_left_to_next > 0)
209  {
210  u32 bi0;
211  vlib_buffer_t *b0;
212  u32 next0 = ACL_NEXT_INDEX_DENY;
213  u32 table_index0;
215  vnet_classify_entry_t *e0;
216  u64 hash0;
217  u8 *h0;
218  u8 error0;
219 
220  /* Stride 3 seems to work best */
221  if (PREDICT_TRUE (n_left_from > 3))
222  {
223  vlib_buffer_t *p1 = vlib_get_buffer (vm, from[3]);
225  u32 table_index1;
226  u64 phash1;
227 
228  table_index1 = vnet_buffer (p1)->l2_classify.table_index;
229 
230  if (PREDICT_TRUE (table_index1 != ~0))
231  {
232  tp1 = pool_elt_at_index (vcm->tables, table_index1);
233  phash1 = vnet_buffer (p1)->l2_classify.hash;
234  vnet_classify_prefetch_entry (tp1, phash1);
235  }
236  }
237 
238  /* speculatively enqueue b0 to the current next frame */
239  bi0 = from[0];
240  to_next[0] = bi0;
241  from += 1;
242  to_next += 1;
243  n_left_from -= 1;
244  n_left_to_next -= 1;
245 
246  b0 = vlib_get_buffer (vm, bi0);
247  table_index0 = vnet_buffer (b0)->l2_classify.table_index;
248  e0 = 0;
249  t0 = 0;
251  &b0->current_config_index, &next0,
252  /* # bytes of config data */ 0);
253 
254  vnet_buffer (b0)->l2_classify.opaque_index = ~0;
255 
256  if (PREDICT_TRUE (table_index0 != ~0))
257  {
258  hash0 = vnet_buffer (b0)->l2_classify.hash;
259  t0 = pool_elt_at_index (vcm->tables, table_index0);
260 
262  h0 =
263  (void *) vlib_buffer_get_current (b0) +
265  else
266  h0 = b0->data;
267 
268  e0 = vnet_classify_find_entry (t0, (u8 *) h0, hash0, now);
269  if (e0)
270  {
271  vnet_buffer (b0)->l2_classify.opaque_index
272  = e0->opaque_index;
273  vlib_buffer_advance (b0, e0->advance);
274 
275  next0 = (e0->next_index < n_next_nodes) ?
276  e0->next_index : next0;
277 
278  hits++;
279 
280  if (is_ip4)
281  error0 = (next0 == ACL_NEXT_INDEX_DENY) ?
282  IP4_ERROR_INACL_SESSION_DENY : IP4_ERROR_NONE;
283  else
284  error0 = (next0 == ACL_NEXT_INDEX_DENY) ?
285  IP6_ERROR_INACL_SESSION_DENY : IP6_ERROR_NONE;
286  b0->error = error_node->errors[error0];
287 
288  if (e0->action == CLASSIFY_ACTION_SET_IP4_FIB_INDEX ||
289  e0->action == CLASSIFY_ACTION_SET_IP6_FIB_INDEX)
290  vnet_buffer (b0)->sw_if_index[VLIB_TX] = e0->metadata;
291  else if (e0->action == CLASSIFY_ACTION_SET_METADATA)
292  vnet_buffer (b0)->ip.adj_index[VLIB_TX] = e0->metadata;
293  }
294  else
295  {
296  while (1)
297  {
298  if (PREDICT_TRUE (t0->next_table_index != ~0))
299  t0 = pool_elt_at_index (vcm->tables,
300  t0->next_table_index);
301  else
302  {
303  next0 = (t0->miss_next_index < n_next_nodes) ?
304  t0->miss_next_index : next0;
305 
306  misses++;
307 
308  if (is_ip4)
309  error0 = (next0 == ACL_NEXT_INDEX_DENY) ?
310  IP4_ERROR_INACL_TABLE_MISS : IP4_ERROR_NONE;
311  else
312  error0 = (next0 == ACL_NEXT_INDEX_DENY) ?
313  IP6_ERROR_INACL_TABLE_MISS : IP6_ERROR_NONE;
314  b0->error = error_node->errors[error0];
315  break;
316  }
317 
318  if (t0->current_data_flag ==
320  h0 =
321  (void *) vlib_buffer_get_current (b0) +
323  else
324  h0 = b0->data;
325 
326  hash0 = vnet_classify_hash_packet (t0, (u8 *) h0);
328  (t0, (u8 *) h0, hash0, now);
329  if (e0)
330  {
331  vnet_buffer (b0)->l2_classify.opaque_index
332  = e0->opaque_index;
333  vlib_buffer_advance (b0, e0->advance);
334  next0 = (e0->next_index < n_next_nodes) ?
335  e0->next_index : next0;
336  hits++;
337  chain_hits++;
338 
339  if (is_ip4)
340  error0 = (next0 == ACL_NEXT_INDEX_DENY) ?
341  IP4_ERROR_INACL_SESSION_DENY : IP4_ERROR_NONE;
342  else
343  error0 = (next0 == ACL_NEXT_INDEX_DENY) ?
344  IP6_ERROR_INACL_SESSION_DENY : IP6_ERROR_NONE;
345  b0->error = error_node->errors[error0];
346 
347  if (e0->action == CLASSIFY_ACTION_SET_IP4_FIB_INDEX
348  || e0->action ==
350  vnet_buffer (b0)->sw_if_index[VLIB_TX] =
351  e0->metadata;
352  break;
353  }
354  }
355  }
356  }
357 
359  && (b0->flags & VLIB_BUFFER_IS_TRACED)))
360  {
361  ip_inacl_trace_t *t =
362  vlib_add_trace (vm, node, b0, sizeof (*t));
363  t->sw_if_index = vnet_buffer (b0)->sw_if_index[VLIB_RX];
364  t->next_index = next0;
365  t->table_index = t0 ? t0 - vcm->tables : ~0;
366  t->offset = (e0 && t0) ? vnet_classify_get_offset (t0, e0) : ~0;
367  }
368 
369  /* verify speculative enqueue, maybe switch current next frame */
370  vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
371  to_next, n_left_to_next,
372  bi0, next0);
373  }
374 
375  vlib_put_next_frame (vm, node, next_index, n_left_to_next);
376  }
377 
379  IP_INACL_ERROR_MISS, misses);
381  IP_INACL_ERROR_HIT, hits);
383  IP_INACL_ERROR_CHAIN_HIT, chain_hits);
384  return frame->n_vectors;
385 }
386 
387 static uword
389 {
390  return ip_inacl_inline (vm, node, frame, 1 /* is_ip4 */ );
391 }
392 
393 
394 /* *INDENT-OFF* */
396  .function = ip4_inacl,
397  .name = "ip4-inacl",
398  .vector_size = sizeof (u32),
399  .format_trace = format_ip_inacl_trace,
400  .n_errors = ARRAY_LEN(ip_inacl_error_strings),
401  .error_strings = ip_inacl_error_strings,
402 
403  .n_next_nodes = ACL_NEXT_INDEX_N_NEXT,
404  .next_nodes = {
405  [ACL_NEXT_INDEX_DENY] = "ip4-drop",
406  },
407 };
408 /* *INDENT-ON* */
409 
411 
412 static uword
414 {
415  return ip_inacl_inline (vm, node, frame, 0 /* is_ip4 */ );
416 }
417 
418 
419 /* *INDENT-OFF* */
421  .function = ip6_inacl,
422  .name = "ip6-inacl",
423  .vector_size = sizeof (u32),
424  .format_trace = format_ip_inacl_trace,
425  .n_errors = ARRAY_LEN(ip_inacl_error_strings),
426  .error_strings = ip_inacl_error_strings,
427 
428  .n_next_nodes = ACL_NEXT_INDEX_N_NEXT,
429  .next_nodes = {
430  [ACL_NEXT_INDEX_DENY] = "ip6-drop",
431  },
432 };
433 /* *INDENT-ON* */
434 
436 
437 static clib_error_t *
439 {
440  return 0;
441 }
442 
444 
445 
446 /*
447  * fd.io coding-style-patch-verification: ON
448  *
449  * Local Variables:
450  * eval: (c-set-style "gnu")
451  * End:
452  */
u64 vnet_classify_hash_packet(vnet_classify_table_t *t, u8 *h)
acl_next_index_t
Definition: input_acl.h:31
#define CLIB_UNUSED(x)
Definition: clib.h:79
static u8 * format_ip_inacl_trace(u8 *s, va_list *args)
Definition: ip_input_acl.c:29
u32 current_config_index
Used by feature subgraph arcs to visit enabled feature nodes.
Definition: buffer.h:116
u32 * classify_table_index_by_sw_if_index[INPUT_ACL_N_TABLES]
Definition: input_acl.h:41
#define PREDICT_TRUE(x)
Definition: clib.h:106
static f64 vlib_time_now(vlib_main_t *vm)
Definition: main.h:224
static char * ip_inacl_error_strings[]
Definition: ip_input_acl.c:56
struct _vlib_node_registration vlib_node_registration_t
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:419
vlib_error_t * errors
Vector of errors for this node.
Definition: node.h:415
vnet_classify_main_t * vnet_classify_main
Definition: input_acl.h:46
#define foreach_ip_inacl_error
Definition: ip_input_acl.c:43
#define CLASSIFY_FLAG_USE_CURR_DATA
Definition: vnet_classify.h:54
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:111
#define vlib_prefetch_buffer_header(b, type)
Prefetch buffer metadata.
Definition: buffer.h:171
unsigned long u64
Definition: types.h:89
static void vnet_classify_prefetch_bucket(vnet_classify_table_t *t, u64 hash)
vlib_node_registration_t ip4_inacl_node
(constructor) VLIB_REGISTER_NODE (ip4_inacl_node)
Definition: ip_input_acl.c:40
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:459
vlib_node_registration_t ip4_input_node
Global ip4 input node.
Definition: ip4_input.c:277
vlib_node_registration_t ip6_input_node
(constructor) VLIB_REGISTER_NODE (ip6_input_node)
Definition: ip6_input.c:228
static void * vlib_buffer_get_current(vlib_buffer_t *b)
Get pointer to current data to process.
Definition: buffer.h:195
static void * vnet_get_config_data(vnet_config_main_t *cm, u32 *config_index, u32 *next_index, u32 n_data_bytes)
Definition: config.h:122
#define PREDICT_FALSE(x)
Definition: clib.h:105
vnet_config_main_t * vnet_config_main[INPUT_ACL_N_TABLES]
Definition: input_acl.h:47
u32 node_index
Node index.
Definition: node.h:437
#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:218
#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:364
vlib_error_t error
Error code for buffers to be enqueued to error handler.
Definition: buffer.h:113
static void vlib_node_increment_counter(vlib_main_t *vm, u32 node_index, u32 counter_index, u64 increment)
Definition: node_funcs.h:1158
static uword vnet_classify_get_offset(vnet_classify_table_t *t, vnet_classify_entry_t *v)
ip_inacl_error_t
Definition: ip_input_acl.c:48
u16 n_vectors
Definition: node.h:344
#define CLIB_PREFETCH(addr, size, type)
Definition: cache.h:82
vlib_main_t * vm
Definition: buffer.c:283
vlib_node_registration_t ip6_inacl_node
(constructor) VLIB_REGISTER_NODE (ip6_inacl_node)
Definition: ip_input_acl.c:41
#define VLIB_BUFFER_IS_TRACED
Definition: buffer.h:93
static vlib_node_runtime_t * vlib_node_get_runtime(vlib_main_t *vm, u32 node_index)
Get node runtime by node index.
Definition: node_funcs.h:89
#define ARRAY_LEN(x)
Definition: clib.h:59
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:454
struct _vnet_classify_main vnet_classify_main_t
Definition: vnet_classify.h:73
input_acl_main_t input_acl_main
Definition: input_acl.c:19
u16 cached_next_index
Next frame index that vector arguments were last enqueued to last time this node ran.
Definition: node.h:456
input_acl_table_id_t
Definition: input_acl.h:23
unsigned int u32
Definition: types.h:88
static uword ip4_inacl(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
Definition: ip_input_acl.c:388
static void vlib_buffer_advance(vlib_buffer_t *b, word l)
Advance current data pointer by the supplied (signed!) amount.
Definition: buffer.h:208
#define VLIB_NODE_FLAG_TRACE
Definition: node.h:259
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
Definition: defs.h:47
double f64
Definition: types.h:142
unsigned char u8
Definition: types.h:56
static uword ip_inacl_inline(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame, int is_ip4)
Definition: ip_input_acl.c:63
static clib_error_t * ip_inacl_init(vlib_main_t *vm)
Definition: ip_input_acl.c:438
static uword ip6_inacl(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
Definition: ip_input_acl.c:413
static void * vlib_frame_vector_args(vlib_frame_t *f)
Get pointer to frame vector data.
Definition: node_funcs.h:267
#define vnet_buffer(b)
Definition: buffer.h:326
#define VLIB_REGISTER_NODE(x,...)
Definition: node.h:143
u8 data[0]
Packet data.
Definition: buffer.h:159
u16 flags
Copy of main node flags.
Definition: node.h:450
VLIB_NODE_FUNCTION_MULTIARCH(ip4_inacl_node, ip4_inacl)
#define CLIB_CACHE_LINE_BYTES
Definition: cache.h:67
u32 flags
buffer flags: VLIB_BUFFER_FREE_LIST_INDEX_MASK: bits used to store free list index, VLIB_BUFFER_IS_TRACED: trace this buffer.
Definition: buffer.h:75
static vlib_buffer_t * vlib_get_buffer(vlib_main_t *vm, u32 buffer_index)
Translate buffer index into buffer pointer.
Definition: buffer_funcs.h:57
vnet_classify_entry_t * vnet_classify_find_entry(vnet_classify_table_t *t, u8 *h, u64 hash, f64 now)
Definition: defs.h:46