FD.io VPP  v16.09
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 {
25 
26 /* packet trace format function */
27 static u8 * format_ip_inacl_trace (u8 * s, va_list * args)
28 {
29  CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
30  CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
31  ip_inacl_trace_t * t = va_arg (*args, ip_inacl_trace_t *);
32 
33  s = format (s, "INACL: sw_if_index %d, next_index %d, table %d, offset %d",
34  t->sw_if_index, t->next_index, t->table_index, t->offset);
35  return s;
36 }
37 
40 
41 #define foreach_ip_inacl_error \
42 _(MISS, "input ACL misses") \
43 _(HIT, "input ACL hits") \
44 _(CHAIN_HIT, "input ACL hits after chain walk")
45 
46 typedef enum {
47 #define _(sym,str) IP_INACL_ERROR_##sym,
49 #undef _
52 
53 static char * ip_inacl_error_strings[] = {
54 #define _(sym,string) string,
56 #undef _
57 };
58 
59 static inline uword
61  vlib_node_runtime_t * node,
62  vlib_frame_t * frame, int is_ip4)
63 {
64  u32 n_left_from, * from, * to_next;
65  acl_next_index_t next_index;
68  f64 now = vlib_time_now (vm);
69  u32 hits = 0;
70  u32 misses = 0;
71  u32 chain_hits = 0;
73  vlib_node_runtime_t * error_node;
74  u32 n_next_nodes;
75 
76  n_next_nodes = node->n_next_nodes;
77 
78  if (is_ip4)
79  {
80  tid = INPUT_ACL_TABLE_IP4;
81  error_node = vlib_node_get_runtime (vm, ip4_input_node.index);
82  }
83  else
84  {
85  tid = INPUT_ACL_TABLE_IP6;
86  error_node = vlib_node_get_runtime (vm, ip6_input_node.index);
87  }
88 
89  from = vlib_frame_vector_args (frame);
90  n_left_from = frame->n_vectors;
91 
92  /* First pass: compute hashes */
93 
94  while (n_left_from > 2)
95  {
96  vlib_buffer_t * b0, * b1;
97  u32 bi0, bi1;
98  u8 * h0, * h1;
99  u32 sw_if_index0, sw_if_index1;
100  u32 table_index0, table_index1;
101  vnet_classify_table_t * t0, * t1;
102 
103  /* prefetch next iteration */
104  {
105  vlib_buffer_t * p1, * p2;
106 
107  p1 = vlib_get_buffer (vm, from[1]);
108  p2 = vlib_get_buffer (vm, from[2]);
109 
110  vlib_prefetch_buffer_header (p1, STORE);
112  vlib_prefetch_buffer_header (p2, STORE);
114  }
115 
116  bi0 = from[0];
117  b0 = vlib_get_buffer (vm, bi0);
118  h0 = b0->data;
119 
120  bi1 = from[1];
121  b1 = vlib_get_buffer (vm, bi1);
122  h1 = b1->data;
123 
124  sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
125  table_index0 = am->classify_table_index_by_sw_if_index[tid][sw_if_index0];
126 
127  sw_if_index1 = vnet_buffer (b1)->sw_if_index[VLIB_RX];
128  table_index1 = am->classify_table_index_by_sw_if_index[tid][sw_if_index1];
129 
130  t0 = pool_elt_at_index (vcm->tables, table_index0);
131 
132  t1 = pool_elt_at_index (vcm->tables, table_index1);
133 
134  vnet_buffer(b0)->l2_classify.hash =
135  vnet_classify_hash_packet (t0, (u8 *) h0);
136 
137  vnet_classify_prefetch_bucket (t0, vnet_buffer(b0)->l2_classify.hash);
138 
139  vnet_buffer(b1)->l2_classify.hash =
140  vnet_classify_hash_packet (t1, (u8 *) h1);
141 
142  vnet_classify_prefetch_bucket (t1, vnet_buffer(b1)->l2_classify.hash);
143 
144  vnet_buffer(b0)->l2_classify.table_index = table_index0;
145 
146  vnet_buffer(b1)->l2_classify.table_index = table_index1;
147 
148  from += 2;
149  n_left_from -= 2;
150  }
151 
152  while (n_left_from > 0)
153  {
154  vlib_buffer_t * b0;
155  u32 bi0;
156  u8 * h0;
157  u32 sw_if_index0;
158  u32 table_index0;
160 
161  bi0 = from[0];
162  b0 = vlib_get_buffer (vm, bi0);
163  h0 = b0->data;
164 
165  sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
166  table_index0 = am->classify_table_index_by_sw_if_index[tid][sw_if_index0];
167 
168  t0 = pool_elt_at_index (vcm->tables, table_index0);
169  vnet_buffer(b0)->l2_classify.hash =
170  vnet_classify_hash_packet (t0, (u8 *) h0);
171 
172  vnet_buffer(b0)->l2_classify.table_index = table_index0;
173  vnet_classify_prefetch_bucket (t0, vnet_buffer(b0)->l2_classify.hash);
174 
175  from++;
176  n_left_from--;
177  }
178 
179  next_index = node->cached_next_index;
180  from = vlib_frame_vector_args (frame);
181  n_left_from = frame->n_vectors;
182 
183  while (n_left_from > 0)
184  {
185  u32 n_left_to_next;
186 
187  vlib_get_next_frame (vm, node, next_index,
188  to_next, n_left_to_next);
189 
190  /* Not enough load/store slots to dual loop... */
191  while (n_left_from > 0 && n_left_to_next > 0)
192  {
193  u32 bi0;
194  vlib_buffer_t * b0;
195  u32 next0 = ACL_NEXT_INDEX_DENY;
196  u32 table_index0;
198  vnet_classify_entry_t * e0;
199  u64 hash0;
200  u8 * h0;
201  u8 error0;
202 
203  /* Stride 3 seems to work best */
204  if (PREDICT_TRUE (n_left_from > 3))
205  {
206  vlib_buffer_t * p1 = vlib_get_buffer(vm, from[3]);
207  vnet_classify_table_t * tp1;
208  u32 table_index1;
209  u64 phash1;
210 
211  table_index1 = vnet_buffer(p1)->l2_classify.table_index;
212 
213  if (PREDICT_TRUE (table_index1 != ~0))
214  {
215  tp1 = pool_elt_at_index (vcm->tables, table_index1);
216  phash1 = vnet_buffer(p1)->l2_classify.hash;
217  vnet_classify_prefetch_entry (tp1, phash1);
218  }
219  }
220 
221  /* speculatively enqueue b0 to the current next frame */
222  bi0 = from[0];
223  to_next[0] = bi0;
224  from += 1;
225  to_next += 1;
226  n_left_from -= 1;
227  n_left_to_next -= 1;
228 
229  b0 = vlib_get_buffer (vm, bi0);
230  h0 = b0->data;
231  table_index0 = vnet_buffer(b0)->l2_classify.table_index;
232  e0 = 0;
233  t0 = 0;
236  &next0,
237  /* # bytes of config data */ 0);
238 
239  vnet_buffer(b0)->l2_classify.opaque_index = ~0;
240 
241  if (PREDICT_TRUE(table_index0 != ~0))
242  {
243  hash0 = vnet_buffer(b0)->l2_classify.hash;
244  t0 = pool_elt_at_index (vcm->tables, table_index0);
245 
246  e0 = vnet_classify_find_entry (t0, (u8 *) h0, hash0,
247  now);
248  if (e0)
249  {
250  vnet_buffer(b0)->l2_classify.opaque_index
251  = e0->opaque_index;
252  vlib_buffer_advance (b0, e0->advance);
253 
254  next0 = (e0->next_index < n_next_nodes)?
255  e0->next_index:next0;
256 
257  hits++;
258 
259  if (is_ip4)
260  error0 = (next0 == ACL_NEXT_INDEX_DENY)?
261  IP4_ERROR_INACL_SESSION_DENY:IP4_ERROR_NONE;
262  else
263  error0 = (next0 == ACL_NEXT_INDEX_DENY)?
264  IP6_ERROR_INACL_SESSION_DENY:IP6_ERROR_NONE;
265  b0->error = error_node->errors[error0];
266  }
267  else
268  {
269  while (1)
270  {
271  if (PREDICT_TRUE(t0->next_table_index != ~0))
272  t0 = pool_elt_at_index (vcm->tables,
273  t0->next_table_index);
274  else
275  {
276  next0 = (t0->miss_next_index < n_next_nodes)?
277  t0->miss_next_index:next0;
278 
279  misses++;
280 
281  if (is_ip4)
282  error0 = (next0 == ACL_NEXT_INDEX_DENY)?
283  IP4_ERROR_INACL_TABLE_MISS:IP4_ERROR_NONE;
284  else
285  error0 = (next0 == ACL_NEXT_INDEX_DENY)?
286  IP6_ERROR_INACL_TABLE_MISS:IP6_ERROR_NONE;
287  b0->error = error_node->errors[error0];
288  break;
289  }
290 
291  hash0 = vnet_classify_hash_packet (t0, (u8 *) h0);
293  (t0, (u8 *) h0, hash0, now);
294  if (e0)
295  {
296  vnet_buffer(b0)->l2_classify.opaque_index
297  = e0->opaque_index;
298  vlib_buffer_advance (b0, e0->advance);
299  next0 = (e0->next_index < n_next_nodes)?
300  e0->next_index:next0;
301  hits++;
302  chain_hits++;
303 
304  if (is_ip4)
305  error0 = (next0 == ACL_NEXT_INDEX_DENY)?
306  IP4_ERROR_INACL_SESSION_DENY:IP4_ERROR_NONE;
307  else
308  error0 = (next0 == ACL_NEXT_INDEX_DENY)?
309  IP6_ERROR_INACL_SESSION_DENY:IP6_ERROR_NONE;
310  b0->error = error_node->errors[error0];
311  break;
312  }
313  }
314  }
315  }
316 
318  && (b0->flags & VLIB_BUFFER_IS_TRACED)))
319  {
320  ip_inacl_trace_t *t =
321  vlib_add_trace (vm, node, b0, sizeof (*t));
322  t->sw_if_index = vnet_buffer(b0)->sw_if_index[VLIB_RX];
323  t->next_index = next0;
324  t->table_index = t0 ? t0 - vcm->tables : ~0;
325  t->offset = (e0 && t0) ? vnet_classify_get_offset (t0, e0): ~0;
326  }
327 
328  /* verify speculative enqueue, maybe switch current next frame */
329  vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
330  to_next, n_left_to_next,
331  bi0, next0);
332  }
333 
334  vlib_put_next_frame (vm, node, next_index, n_left_to_next);
335  }
336 
338  IP_INACL_ERROR_MISS,
339  misses);
341  IP_INACL_ERROR_HIT,
342  hits);
344  IP_INACL_ERROR_CHAIN_HIT,
345  chain_hits);
346  return frame->n_vectors;
347 }
348 
349 static uword
351  vlib_node_runtime_t * node,
352  vlib_frame_t * frame)
353 {
354  return ip_inacl_inline (vm, node, frame, 1 /* is_ip4 */);
355 }
356 
357 
359  .function = ip4_inacl,
360  .name = "ip4-inacl",
361  .vector_size = sizeof (u32),
362  .format_trace = format_ip_inacl_trace,
363  .n_errors = ARRAY_LEN(ip_inacl_error_strings),
364  .error_strings = ip_inacl_error_strings,
365 
366  .n_next_nodes = ACL_NEXT_INDEX_N_NEXT,
367  .next_nodes = {
368  [ACL_NEXT_INDEX_DENY] = "error-drop",
369  },
370 };
371 
373 
374 static uword
376  vlib_node_runtime_t * node,
377  vlib_frame_t * frame)
378 {
379  return ip_inacl_inline (vm, node, frame, 0 /* is_ip4 */);
380 }
381 
382 
384  .function = ip6_inacl,
385  .name = "ip6-inacl",
386  .vector_size = sizeof (u32),
387  .format_trace = format_ip_inacl_trace,
388  .n_errors = ARRAY_LEN(ip_inacl_error_strings),
389  .error_strings = ip_inacl_error_strings,
390 
391  .n_next_nodes = ACL_NEXT_INDEX_N_NEXT,
392  .next_nodes = {
393  [ACL_NEXT_INDEX_DENY] = "error-drop",
394  },
395 };
396 
398 
399 static clib_error_t *
401 {
402  return 0;
403 }
404 
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
static u8 * format_ip_inacl_trace(u8 *s, va_list *args)
Definition: ip_input_acl.c:27
u32 current_config_index
Used by feature subgraph arcs to visit enabled feature nodes.
Definition: buffer.h:121
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
static char * ip_inacl_error_strings[]
Definition: ip_input_acl.c:53
struct _vlib_node_registration vlib_node_registration_t
vlib_error_t * errors
Definition: node.h:418
vnet_classify_main_t * vnet_classify_main
Definition: input_acl.h:43
#define foreach_ip_inacl_error
Definition: ip_input_acl.c:41
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:111
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:38
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
vlib_node_registration_t ip4_input_node
Global ip4 input node.
Definition: ip4_input.c:388
vlib_node_registration_t ip6_input_node
(constructor) VLIB_REGISTER_NODE (ip6_input_node)
Definition: ip6_input.c:289
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:97
vnet_config_main_t * vnet_config_main[INPUT_ACL_N_TABLES]
Definition: input_acl.h:44
#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)
ip_inacl_error_t
Definition: ip_input_acl.c:46
u16 n_vectors
Definition: node.h:344
#define CLIB_PREFETCH(addr, size, type)
Definition: cache.h:82
vlib_node_registration_t ip6_inacl_node
(constructor) VLIB_REGISTER_NODE (ip6_inacl_node)
Definition: ip_input_acl.c:39
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:88
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
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
static uword ip4_inacl(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
Definition: ip_input_acl.c:350
#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 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:60
static clib_error_t * ip_inacl_init(vlib_main_t *vm)
Definition: ip_input_acl.c:400
static uword ip6_inacl(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
Definition: ip_input_acl.c:375
static void * vlib_frame_vector_args(vlib_frame_t *f)
Get pointer to frame vector data.
Definition: node_funcs.h:251
#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