FD.io VPP  v16.06
Vector Packet Processing
l2_output_acl.c
Go to the documentation of this file.
1 /*
2  * l2_output_acl.c : layer 2 output 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/feat_bitmap.h>
28 #include <vnet/l2/l2_output.h>
29 
30 #include <vppinfra/error.h>
31 #include <vppinfra/hash.h>
32 #include <vppinfra/cache.h>
33 
34 
35 typedef struct {
36  // Next nodes for features and output interfaces
38 
39  /* convenience variables */
43 
44 
45 
46 typedef struct {
47  /* per-pkt trace data */
48  u8 src[6];
49  u8 dst[6];
53 
54 /* packet trace format function */
55 static u8 * format_l2_outacl_trace (u8 * s, va_list * args)
56 {
57  CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
58  CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
59  l2_outacl_trace_t * t = va_arg (*args, l2_outacl_trace_t *);
60 
61  s = format (s, "l2-output-acl: sw_if_index %d dst %U src %U",
62  t->sw_if_index,
65  return s;
66 }
67 
69 
71 
72 #define foreach_l2_outacl_error \
73 _(L2_OUTACL, "L2 output ACL packets") \
74 _(DROP, "L2 output drops")
75 
76 typedef enum {
77 #define _(sym,str) L2_OUTACL_ERROR_##sym,
79 #undef _
82 
83 static char * l2_outacl_error_strings[] = {
84 #define _(sym,string) string,
86 #undef _
87 };
88 
89 typedef enum {
93 
94 
95 
96 static uword
98  vlib_node_runtime_t * node,
99  vlib_frame_t * frame)
100 {
101  u32 n_left_from, * from, * to_next;
102  l2_outacl_next_t next_index;
104  vlib_node_t *n = vlib_get_node (vm, l2_outacl_node.index);
105  u32 node_counter_base_index = n->error_heap_index;
106  vlib_error_main_t * em = &vm->error_main;
107  u32 cached_sw_if_index = (u32)~0;
108  u32 cached_next_index = (u32)~0;
109 
110  from = vlib_frame_vector_args (frame);
111  n_left_from = frame->n_vectors; /* number of packets to process */
112  next_index = node->cached_next_index;
113 
114  while (n_left_from > 0)
115  {
116  u32 n_left_to_next;
117 
118  /* get space to enqueue frame to graph node "next_index" */
119  vlib_get_next_frame (vm, node, next_index,
120  to_next, n_left_to_next);
121 
122  while (0 && n_left_from >= 4 && n_left_to_next >= 2)
123  {
124  u32 bi0, bi1;
125  vlib_buffer_t * b0, * b1;
126  u32 next0, next1;
127  u32 sw_if_index0, sw_if_index1;
128  ethernet_header_t * h0, * h1;
129 
130  /* Prefetch next iteration. */
131  {
132  vlib_buffer_t * p2, * p3;
133 
134  p2 = vlib_get_buffer (vm, from[2]);
135  p3 = vlib_get_buffer (vm, from[3]);
136 
137  vlib_prefetch_buffer_header (p2, LOAD);
138  vlib_prefetch_buffer_header (p3, LOAD);
139 
142  }
143 
144  /* speculatively enqueue b0 and b1 to the current next frame */
145  /* bi is "buffer index", b is pointer to the buffer */
146  to_next[0] = bi0 = from[0];
147  to_next[1] = bi1 = from[1];
148  from += 2;
149  to_next += 2;
150  n_left_from -= 2;
151  n_left_to_next -= 2;
152 
153  b0 = vlib_get_buffer (vm, bi0);
154  b1 = vlib_get_buffer (vm, bi1);
155 
156  /* TX interface handles */
157  sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_TX];
158  sw_if_index1 = vnet_buffer(b1)->sw_if_index[VLIB_TX];
159 
161  {
162  if (b0->flags & VLIB_BUFFER_IS_TRACED)
163  {
164  l2_outacl_trace_t *t =
165  vlib_add_trace (vm, node, b0, sizeof (*t));
166  t->sw_if_index = sw_if_index0;
167  t->next_index = next0;
168  clib_memcpy(t->src, h0->src_address, 6);
169  clib_memcpy(t->dst, h0->dst_address, 6);
170  }
171  if (b1->flags & VLIB_BUFFER_IS_TRACED)
172  {
173  l2_outacl_trace_t *t =
174  vlib_add_trace (vm, node, b1, sizeof (*t));
175  t->sw_if_index = sw_if_index1;
176  t->next_index = next1;
177  clib_memcpy(t->src, h1->src_address, 6);
178  clib_memcpy(t->dst, h1->dst_address, 6);
179  }
180  }
181 
182  em->counters[node_counter_base_index + L2_OUTACL_ERROR_L2_OUTACL] += 2;
183 
184  /* add core loop code here */
185 
186  /* verify speculative enqueues, maybe switch current next frame */
187  /* if next0==next1==next_index then nothing special needs to be done */
188  vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
189  to_next, n_left_to_next,
190  bi0, bi1, next0, next1);
191  }
192 
193  while (n_left_from > 0 && n_left_to_next > 0)
194  {
195  u32 bi0;
196  vlib_buffer_t * b0;
197  u32 next0;
198  u32 sw_if_index0;
199  ethernet_header_t * h0;
200  u32 feature_bitmap0;
201 
202  /* speculatively enqueue b0 to the current next frame */
203  bi0 = from[0];
204  to_next[0] = bi0;
205  from += 1;
206  to_next += 1;
207  n_left_from -= 1;
208  n_left_to_next -= 1;
209 
210  b0 = vlib_get_buffer (vm, bi0);
211  h0 = vlib_buffer_get_current (b0);
212 
213  sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_TX];
214 
216  && (b0->flags & VLIB_BUFFER_IS_TRACED))) {
217  l2_outacl_trace_t *t =
218  vlib_add_trace (vm, node, b0, sizeof (*t));
219  t->sw_if_index = sw_if_index0;
220  clib_memcpy(t->src, h0->src_address, 6);
221  clib_memcpy(t->dst, h0->dst_address, 6);
222  }
223 
224  em->counters[node_counter_base_index + L2_OUTACL_ERROR_L2_OUTACL] += 1;
225 
226  // L2_OUTACL code
227  // Dummy for now, just go to next feature node
228 
229 
230  // Remove ourself from the feature bitmap
231  feature_bitmap0 = vnet_buffer(b0)->l2.feature_bitmap & ~L2OUTPUT_FEAT_ACL;
232 
233  // Determine next node
235  msm->vnet_main,
236  node,
237  l2_outacl_node.index,
238  &cached_sw_if_index,
239  &cached_next_index,
240  &msm->next_nodes,
241  b0,
242  sw_if_index0,
243  feature_bitmap0,
244  &next0);
245 
246  /* verify speculative enqueue, maybe switch current next frame */
247  vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
248  to_next, n_left_to_next,
249  bi0, next0);
250  }
251 
252  vlib_put_next_frame (vm, node, next_index, n_left_to_next);
253  }
254 
255  return frame->n_vectors;
256 }
257 
258 
260  .function = l2_outacl_node_fn,
261  .name = "l2-output-acl",
262  .vector_size = sizeof (u32),
263  .format_trace = format_l2_outacl_trace,
265 
266  .n_errors = ARRAY_LEN(l2_outacl_error_strings),
267  .error_strings = l2_outacl_error_strings,
268 
269  .n_next_nodes = L2_OUTACL_N_NEXT,
270 
271  /* edit / add dispositions here */
272  .next_nodes = {
273  [L2_OUTACL_NEXT_DROP] = "error-drop",
274  },
275 };
276 
278 {
280 
281  mp->vlib_main = vm;
282  mp->vnet_main = vnet_get_main();
283 
284  // Initialize the feature next-node indexes
286  l2_outacl_node.index,
290 
291  // Initialize the output node mapping table
293 
294  return 0;
295 }
296 
298 
299 #if 0
300 /** @todo maybe someone will add output ACL's in the future */
301 // set subinterface outacl enable/disable
302 // The CLI format is:
303 // set interface acl output <interface> [disable]
304 static clib_error_t *
305 int_l2_outacl (vlib_main_t * vm,
306  unformat_input_t * input,
307  vlib_cli_command_t * cmd)
308 {
309  vnet_main_t * vnm = vnet_get_main();
310  clib_error_t * error = 0;
311  u32 sw_if_index;
312  u32 enable;
313 
314  if (! unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
315  {
316  error = clib_error_return (0, "unknown interface `%U'",
317  format_unformat_error, input);
318  goto done;
319  }
320 
321  enable = 1;
322  if (unformat (input, "disable")) {
323  enable = 0;
324  }
325 
326  // set the interface flag
327  l2output_intf_bitmap_enable(sw_if_index, L2OUTPUT_FEAT_ACL, enable);
328 
329  done:
330  return error;
331 }
332 
333 VLIB_CLI_COMMAND (int_l2_outacl_cli, static) = {
334  .path = "set interface acl output",
335  .short_help = "set interface acl output <interface> [disable]",
336  .function = int_l2_outacl,
337 };
338 #endif
vlib_main_t * vlib_main
Definition: l2_output_acl.c:40
void vlib_put_next_frame(vlib_main_t *vm, vlib_node_runtime_t *r, u32 next_index, u32 n_vectors_left)
Definition: main.c:459
always_inline void l2_output_dispatch(vlib_main_t *vlib_main, vnet_main_t *vnet_main, vlib_node_runtime_t *node, u32 node_index, u32 *cached_sw_if_index, u32 *cached_next_index, l2_output_next_nodes_st *next_nodes, vlib_buffer_t *b0, u32 sw_if_index, u32 feature_bitmap, u32 *next0)
Definition: l2_output.h:166
u32 error_heap_index
Definition: node.h:244
#define CLIB_UNUSED(x)
Definition: clib.h:79
char ** l2output_get_feat_names(void)
Definition: l2_output.c:37
uword unformat(unformat_input_t *i, char *fmt,...)
Definition: unformat.c:942
always_inline vlib_node_t * vlib_get_node(vlib_main_t *vm, u32 i)
Definition: node_funcs.h:46
bad routing header type(not 4)") sr_error (NO_MORE_SEGMENTS
l2_outacl_next_t
Definition: l2_output_acl.c:89
u8 src_address[6]
Definition: packet.h:52
struct _vlib_node_registration vlib_node_registration_t
unformat_function_t unformat_vnet_sw_interface
static uword l2_outacl_node_fn(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
Definition: l2_output_acl.c:97
always_inline void l2output_init_output_node_vec(u32 **output_node_index_vec)
Definition: l2_output.h:126
static vlib_node_registration_t l2_outacl_node
(constructor) VLIB_REGISTER_NODE (l2_outacl_node)
Definition: l2_output_acl.c:70
always_inline void * vlib_buffer_get_current(vlib_buffer_t *b)
Get pointer to current data to process.
Definition: buffer.h:184
vnet_main_t * vnet_get_main(void)
Definition: misc.c:45
u8 * format_ethernet_address(u8 *s, va_list *args)
Definition: format.c:43
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:109
u8 dst_address[6]
Definition: packet.h:51
uword unformat_user(unformat_input_t *input, unformat_function_t *func,...)
Definition: unformat.c:953
always_inline void * vlib_frame_vector_args(vlib_frame_t *f)
Definition: node_funcs.h:202
void l2output_intf_bitmap_enable(u32 sw_if_index, u32 feature_bitmap, u32 enable)
Definition: l2_output.c:538
u32 feat_next_node_index[32]
Definition: l2_output.h:58
vlib_error_main_t error_main
Definition: main.h:124
#define PREDICT_FALSE(x)
Definition: clib.h:97
#define vlib_validate_buffer_enqueue_x2(vm, node, next_index, to_next, n_left_to_next, bi0, bi1, next0, next1)
Definition: buffer_node.h:43
#define vlib_validate_buffer_enqueue_x1(vm, node, next_index, to_next, n_left_to_next, bi0, next0)
Definition: buffer_node.h:83
#define vlib_get_next_frame(vm, node, next_index, vectors, n_vectors_left)
Definition: node_funcs.h:265
u64 * counters
Definition: error.h:73
u16 n_vectors
Definition: node.h:307
#define CLIB_PREFETCH(addr, size, type)
Definition: cache.h:82
static u8 * format_l2_outacl_trace(u8 *s, va_list *args)
Definition: l2_output_acl.c:55
#define clib_memcpy(a, b, c)
Definition: string.h:63
#define ARRAY_LEN(x)
Definition: clib.h:59
l2_output_next_nodes_st next_nodes
Definition: l2_output_acl.c:37
#define VLIB_CLI_COMMAND(x,...)
Definition: cli.h:150
l2_outacl_error_t
Definition: l2_output_acl.c:76
u16 cached_next_index
Definition: node.h:422
unsigned int u32
Definition: types.h:88
u8 * format_unformat_error(u8 *s, va_list *va)
Definition: unformat.c:87
#define vnet_buffer(b)
Definition: buffer.h:300
u8 * format(u8 *s, char *fmt,...)
Definition: format.c:405
#define VLIB_NODE_FLAG_TRACE
Definition: node.h:225
#define VLIB_BUFFER_IS_TRACED
Definition: buffer.h:91
u64 uword
Definition: types.h:112
Definition: defs.h:46
clib_error_t * l2_outacl_init(vlib_main_t *vm)
unsigned char u8
Definition: types.h:56
static char * l2_outacl_error_strings[]
Definition: l2_output_acl.c:83
always_inline 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
#define vlib_prefetch_buffer_header(b, type)
Prefetch buffer metadata.
Definition: buffer.h:162
#define VLIB_REGISTER_NODE(x,...)
Definition: node.h:140
vnet_main_t * vnet_main
Definition: l2_output_acl.c:41
l2_outacl_main_t l2_outacl_main
Definition: l2_output_acl.c:68
u8 data[0]
Packet data.
Definition: buffer.h:150
#define clib_error_return(e, args...)
Definition: error.h:112
struct _unformat_input_t unformat_input_t
#define CLIB_CACHE_LINE_BYTES
Definition: cache.h:67
u32 flags
buffer flags: VLIB_BUFFER_IS_TRACED: trace this buffer.
Definition: buffer.h:84
always_inline vlib_buffer_t * vlib_get_buffer(vlib_main_t *vm, u32 buffer_index)
Translate buffer index into buffer pointer.
Definition: buffer_funcs.h:69
#define foreach_l2_outacl_error
Definition: l2_output_acl.c:72
always_inline void feat_bitmap_init_next_nodes(vlib_main_t *vm, u32 node_index, u32 num_features, char **feat_names, u32 *next_nodes)
Definition: feat_bitmap.h:41