FD.io VPP  v16.09
Vector Packet Processing
l2_input_vtr.c
Go to the documentation of this file.
1 /*
2  * l2_input_vtr.c : layer 2 input vlan tag rewrite 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/ethernet/ethernet.h>
21 #include <vnet/ethernet/packet.h>
22 #include <vnet/l2/l2_input.h>
23 #include <vnet/l2/feat_bitmap.h>
24 #include <vnet/l2/l2_vtr.h>
25 #include <vnet/l2/l2_input_vtr.h>
26 #include <vnet/l2/l2_output.h>
27 
28 #include <vppinfra/error.h>
29 #include <vppinfra/cache.h>
30 
31 
32 typedef struct
33 {
34  /* per-pkt trace data */
35  u8 src[6];
36  u8 dst[6];
37  u8 raw[12]; /* raw data (vlans) */
40 
41 /* packet trace format function */
42 static u8 *
43 format_l2_invtr_trace (u8 * s, va_list * args)
44 {
45  CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
46  CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
47  l2_invtr_trace_t *t = va_arg (*args, l2_invtr_trace_t *);
48 
49  s = format (s, "l2-input-vtr: sw_if_index %d dst %U src %U data "
50  "%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x",
51  t->sw_if_index,
54  t->raw[0], t->raw[1], t->raw[2], t->raw[3], t->raw[4],
55  t->raw[5], t->raw[6], t->raw[7], t->raw[8], t->raw[9],
56  t->raw[10], t->raw[11]);
57  return s;
58 }
59 
61 
63 
64 #define foreach_l2_invtr_error \
65 _(L2_INVTR, "L2 inverter packets") \
66 _(DROP, "L2 input tag rewrite drops")
67 
68 typedef enum
69 {
70 #define _(sym,str) L2_INVTR_ERROR_##sym,
72 #undef _
75 
76 static char *l2_invtr_error_strings[] = {
77 #define _(sym,string) string,
79 #undef _
80 };
81 
82 typedef enum
83 {
87 
88 
89 static uword
91  vlib_node_runtime_t * node, vlib_frame_t * frame)
92 {
93  u32 n_left_from, *from, *to_next;
94  l2_invtr_next_t next_index;
96 
97  from = vlib_frame_vector_args (frame);
98  n_left_from = frame->n_vectors; /* number of packets to process */
99  next_index = node->cached_next_index;
100 
101  while (n_left_from > 0)
102  {
103  u32 n_left_to_next;
104 
105  /* get space to enqueue frame to graph node "next_index" */
106  vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
107 
108  while (n_left_from >= 6 && n_left_to_next >= 2)
109  {
110  u32 bi0, bi1;
111  vlib_buffer_t *b0, *b1;
112  u32 next0, next1;
113  u32 sw_if_index0, sw_if_index1;
114  u32 feature_bitmap0, feature_bitmap1;
115 
116  /* Prefetch next iteration. */
117  {
118  vlib_buffer_t *p2, *p3, *p4, *p5;
119  u32 sw_if_index2, sw_if_index3;
120 
121  p2 = vlib_get_buffer (vm, from[2]);
122  p3 = vlib_get_buffer (vm, from[3]);
123  p4 = vlib_get_buffer (vm, from[4]);
124  p5 = vlib_get_buffer (vm, from[5]);
125 
126  /* Prefetch the buffer header and packet for the N+2 loop iteration */
127  vlib_prefetch_buffer_header (p4, LOAD);
128  vlib_prefetch_buffer_header (p5, LOAD);
129 
132 
133  /*
134  * Prefetch the input config for the N+1 loop iteration
135  * This depends on the buffer header above
136  */
137  sw_if_index2 = vnet_buffer (p2)->sw_if_index[VLIB_RX];
138  sw_if_index3 = vnet_buffer (p3)->sw_if_index[VLIB_RX];
140  (l2output_main.configs, sw_if_index2),
141  CLIB_CACHE_LINE_BYTES, LOAD);
143  (l2output_main.configs, sw_if_index3),
144  CLIB_CACHE_LINE_BYTES, LOAD);
145  }
146 
147  /* speculatively enqueue b0 and b1 to the current next frame */
148  /* bi is "buffer index", b is pointer to the buffer */
149  to_next[0] = bi0 = from[0];
150  to_next[1] = bi1 = from[1];
151  from += 2;
152  to_next += 2;
153  n_left_from -= 2;
154  n_left_to_next -= 2;
155 
156  b0 = vlib_get_buffer (vm, bi0);
157  b1 = vlib_get_buffer (vm, bi1);
158 
159  /* RX interface handles */
160  sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
161  sw_if_index1 = vnet_buffer (b1)->sw_if_index[VLIB_RX];
162 
163  /* process 2 packets */
164 
165  /* Remove ourself from the feature bitmap */
166  feature_bitmap0 =
167  vnet_buffer (b0)->l2.feature_bitmap & ~L2INPUT_FEAT_VTR;
168  feature_bitmap1 =
169  vnet_buffer (b1)->l2.feature_bitmap & ~L2INPUT_FEAT_VTR;
170 
171  /* save for next feature graph nodes */
172  vnet_buffer (b0)->l2.feature_bitmap = feature_bitmap0;
173  vnet_buffer (b1)->l2.feature_bitmap = feature_bitmap1;
174 
175  /* Determine the next node */
177  feature_bitmap0);
179  feature_bitmap1);
180 
181  /* perform the tag rewrite on two packets */
182  if (l2_vtr_process
183  (b0,
185  (l2output_main.configs, sw_if_index0)->input_vtr)))
186  {
187  /* Drop packet */
188  next0 = L2_INVTR_NEXT_DROP;
189  b0->error = node->errors[L2_INVTR_ERROR_DROP];
190  }
191  if (l2_vtr_process
192  (b1,
194  (l2output_main.configs, sw_if_index1)->input_vtr)))
195  {
196  /* Drop packet */
197  next1 = L2_INVTR_NEXT_DROP;
198  b1->error = node->errors[L2_INVTR_ERROR_DROP];
199  }
200 
201  if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)))
202  {
203  if (b0->flags & VLIB_BUFFER_IS_TRACED)
204  {
205  l2_invtr_trace_t *t =
206  vlib_add_trace (vm, node, b0, sizeof (*t));
208  t->sw_if_index = sw_if_index0;
209  clib_memcpy (t->src, h0->src_address, 6);
210  clib_memcpy (t->dst, h0->dst_address, 6);
211  clib_memcpy (t->raw, &h0->type, sizeof (t->raw));
212  }
213  if (b1->flags & VLIB_BUFFER_IS_TRACED)
214  {
215  l2_invtr_trace_t *t =
216  vlib_add_trace (vm, node, b1, sizeof (*t));
218  t->sw_if_index = sw_if_index0;
219  clib_memcpy (t->src, h1->src_address, 6);
220  clib_memcpy (t->dst, h1->dst_address, 6);
221  clib_memcpy (t->raw, &h1->type, sizeof (t->raw));
222  }
223  }
224 
225  /* verify speculative enqueues, maybe switch current next frame */
226  /* if next0==next1==next_index then nothing special needs to be done */
227  vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
228  to_next, n_left_to_next,
229  bi0, bi1, next0, next1);
230  }
231 
232  while (n_left_from > 0 && n_left_to_next > 0)
233  {
234  u32 bi0;
235  vlib_buffer_t *b0;
236  u32 next0;
237  u32 sw_if_index0;
238  u32 feature_bitmap0;
239 
240  /* speculatively enqueue b0 to the current next frame */
241  bi0 = from[0];
242  to_next[0] = bi0;
243  from += 1;
244  to_next += 1;
245  n_left_from -= 1;
246  n_left_to_next -= 1;
247 
248  b0 = vlib_get_buffer (vm, bi0);
249 
250  sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
251 
252  /* process 1 packet */
253 
254  /* Remove ourself from the feature bitmap */
255  feature_bitmap0 =
256  vnet_buffer (b0)->l2.feature_bitmap & ~L2INPUT_FEAT_VTR;
257 
258  /* save for next feature graph nodes */
259  vnet_buffer (b0)->l2.feature_bitmap = feature_bitmap0;
260 
261  /* Determine the next node */
263  feature_bitmap0);
264 
265  /* perform the tag rewrite on one packet */
266  if (l2_vtr_process
267  (b0,
269  (l2output_main.configs, sw_if_index0)->input_vtr)))
270  {
271  /* Drop packet */
272  next0 = L2_INVTR_NEXT_DROP;
273  b0->error = node->errors[L2_INVTR_ERROR_DROP];
274  }
275 
277  && (b0->flags & VLIB_BUFFER_IS_TRACED)))
278  {
279  l2_invtr_trace_t *t =
280  vlib_add_trace (vm, node, b0, sizeof (*t));
282  t->sw_if_index = sw_if_index0;
283  clib_memcpy (t->src, h0->src_address, 6);
284  clib_memcpy (t->dst, h0->dst_address, 6);
285  clib_memcpy (t->raw, &h0->type, sizeof (t->raw));
286  }
287 
288  /* verify speculative enqueue, maybe switch current next frame */
289  vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
290  to_next, n_left_to_next,
291  bi0, next0);
292  }
293 
294  vlib_put_next_frame (vm, node, next_index, n_left_to_next);
295  }
296 
297  return frame->n_vectors;
298 }
299 
300 
301 /* *INDENT-OFF* */
303  .function = l2_invtr_node_fn,
304  .name = "l2-input-vtr",
305  .vector_size = sizeof (u32),
306  .format_trace = format_l2_invtr_trace,
308 
309  .n_errors = ARRAY_LEN(l2_invtr_error_strings),
310  .error_strings = l2_invtr_error_strings,
311 
312  .n_next_nodes = L2_INVTR_N_NEXT,
313 
314  /* edit / add dispositions here */
315  .next_nodes = {
316  [L2_INVTR_NEXT_DROP] = "error-drop",
317  },
318 };
319 /* *INDENT-ON* */
320 
323 {
325 
326  mp->vlib_main = vm;
327  mp->vnet_main = vnet_get_main ();
328 
329  /* Initialize the feature next-node indexes */
331  l2_invtr_node.index,
335 
336  return 0;
337 }
338 
340 
341 
342 /*
343  * fd.io coding-style-patch-verification: ON
344  *
345  * Local Variables:
346  * eval: (c-set-style "gnu")
347  * End:
348  */
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
l2_invtr_error_t
Definition: l2_input_vtr.c:68
#define CLIB_UNUSED(x)
Definition: clib.h:79
bad routing header type(not 4)") sr_error (NO_MORE_SEGMENTS
u8 src_address[6]
Definition: packet.h:54
static char * l2_invtr_error_strings[]
Definition: l2_input_vtr.c:76
vlib_main_t * vlib_main
Definition: l2_input_vtr.h:39
struct _vlib_node_registration vlib_node_registration_t
#define foreach_l2_invtr_error
Definition: l2_input_vtr.c:64
vlib_error_t * errors
Definition: node.h:418
vnet_main_t * vnet_get_main(void)
Definition: misc.c:45
u8 * format_ethernet_address(u8 *s, va_list *args)
Definition: format.c:44
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:111
static void * vlib_buffer_get_current(vlib_buffer_t *b)
Get pointer to current data to process.
Definition: buffer.h:187
u8 dst_address[6]
Definition: packet.h:53
#define vec_elt_at_index(v, i)
Get vector value at index i checking that i is in bounds.
static u8 * format_l2_invtr_trace(u8 *s, va_list *args)
Definition: l2_input_vtr.c:43
static u32 l2_vtr_process(vlib_buffer_t *b0, vtr_config_t *config)
Perform the configured tag rewrite on the packet.
Definition: l2_vtr.h:75
clib_error_t * l2_invtr_init(vlib_main_t *vm)
Definition: l2_input_vtr.c:322
#define PREDICT_FALSE(x)
Definition: clib.h:97
l2output_main_t l2output_main
Definition: l2_output.c:43
#define vlib_validate_buffer_enqueue_x2(vm, node, next_index, to_next, n_left_to_next, bi0, bi1, next0, next1)
Finish enqueueing two buffers forward in the graph.
Definition: buffer_node.h:70
vnet_main_t * vnet_main
Definition: l2_input_vtr.h:40
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
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
#define clib_memcpy(a, b, c)
Definition: string.h:63
#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
u16 cached_next_index
Definition: node.h:462
unsigned int u32
Definition: types.h:88
#define vnet_buffer(b)
Definition: buffer.h:335
l2_invtr_main_t l2_invtr_main
Definition: l2_input_vtr.c:60
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
l2_invtr_next_t
Definition: l2_input_vtr.c:82
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
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
l2_output_config_t * configs
Definition: l2_output.h:79
#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
static vlib_node_registration_t l2_invtr_node
(constructor) VLIB_REGISTER_NODE (l2_invtr_node)
Definition: l2_input_vtr.c:62
#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
static uword l2_invtr_node_fn(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
Definition: l2_input_vtr.c:90
u32 feat_next_node_index[32]
Definition: l2_input_vtr.h:36
Definition: defs.h:46