FD.io VPP  v17.01-9-ge7dcee4
Vector Packet Processing
node.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016 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 
16 #include <vlib/vlib.h>
17 #include <vnet/vnet.h>
18 #include <vppinfra/error.h>
19 
20 #include <vnet/span/span.h>
21 
22 #include <vppinfra/error.h>
23 #include <vppinfra/elog.h>
24 
26 
27 /* packet trace format function */
28 u8 *
29 format_span_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  span_trace_t *t = va_arg (*args, span_trace_t *);
34 
35  vnet_main_t *vnm = &vnet_main;
36  s = format (s, "SPAN: mirrored %U -> %U",
39 
40  return s;
41 }
42 
43 #define foreach_span_error \
44 _(HITS, "SPAN incomming packets processed")
45 
46 typedef enum
47 {
48 #define _(sym,str) SPAN_ERROR_##sym,
50 #undef _
52 } span_error_t;
53 
54 static char *span_error_strings[] = {
55 #define _(sym,string) string,
57 #undef _
58 };
59 
62  vlib_frame_t ** mirror_frames, int is_rx)
63 {
64  vlib_buffer_t *c0;
65  vnet_main_t *vnm = &vnet_main;
66  u32 *to_mirror_next = 0;
67  u32 i;
68 
69  if (is_rx != 0 && si0->num_rx_mirror_ports == 0)
70  return;
71 
72  if (is_rx == 0 && si0->num_tx_mirror_ports == 0)
73  return;
74 
75  /* Don't do it again */
77  return;
78 
79  /* *INDENT-OFF* */
80  clib_bitmap_foreach (i, is_rx ? si0->rx_mirror_ports : si0->tx_mirror_ports, (
81  {
82  if (mirror_frames[i] == 0)
83  mirror_frames[i] = vnet_get_frame_to_sw_interface (vnm, i);
84  to_mirror_next = vlib_frame_vector_args (mirror_frames[i]);
85  to_mirror_next += mirror_frames[i]->n_vectors;
86  c0 = vlib_buffer_copy (vm, b0);
87  vnet_buffer (c0)->sw_if_index[VLIB_TX] = i;
88  c0->flags |= VNET_BUFFER_SPAN_CLONE;
89  to_mirror_next[0] = vlib_get_buffer_index (vm, c0);
90  mirror_frames[i]->n_vectors++;
91  }));
92  /* *INDENT-ON* */
93 }
94 
97  vlib_frame_t * frame, int is_rx)
98 {
99  span_main_t *sm = &span_main;
100  vnet_main_t *vnm = &vnet_main;
101  u32 n_left_from, *from, *to_next;
102  u32 n_span_packets = 0;
103  u32 next_index;
104  u32 sw_if_index;
105  static __thread vlib_frame_t **mirror_frames = 0;
106  vlib_rx_or_tx_t rxtx = is_rx ? VLIB_RX : VLIB_TX;
107 
108  from = vlib_frame_vector_args (frame);
109  n_left_from = frame->n_vectors;
110  next_index = node->cached_next_index;
111 
112  vec_validate_aligned (mirror_frames, sm->max_sw_if_index,
114 
115  while (n_left_from > 0)
116  {
117  u32 n_left_to_next;
118 
119  vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
120 
121  while (n_left_from >= 4 && n_left_to_next >= 2)
122  {
123  u32 bi0;
124  u32 bi1;
125  vlib_buffer_t *b0;
126  vlib_buffer_t *b1;
127  span_interface_t *si0, *si1;
128  u32 sw_if_index0;
129  u32 next0 = 0;
130  u32 sw_if_index1;
131  u32 next1 = 0;
132 
133  /* speculatively enqueue b0, b1 to the current next frame */
134  to_next[0] = bi0 = from[0];
135  to_next[1] = bi1 = from[1];
136  to_next += 2;
137  n_left_to_next -= 2;
138  from += 2;
139  n_left_from -= 2;
140 
141  b0 = vlib_get_buffer (vm, bi0);
142  b1 = vlib_get_buffer (vm, bi1);
143  sw_if_index0 = vnet_buffer (b0)->sw_if_index[rxtx];
144  sw_if_index1 = vnet_buffer (b1)->sw_if_index[rxtx];
145  si0 = vec_elt_at_index (sm->interfaces, sw_if_index0);
146  si1 = vec_elt_at_index (sm->interfaces, sw_if_index1);
147 
148  span_mirror (vm, si0, b0, mirror_frames, is_rx);
149  span_mirror (vm, si1, b1, mirror_frames, is_rx);
150 
151  vnet_feature_next (sw_if_index0, &next0, b0);
152  vnet_feature_next (sw_if_index1, &next1, b1);
153 
155  {
156  span_trace_t *t = vlib_add_trace (vm, node, b0, sizeof (*t));
157  t->src_sw_if_index = sw_if_index0;
158  //t->mirror_sw_if_index = si0->mirror_sw_if_index;
159  }
160 
162  {
163  span_trace_t *t = vlib_add_trace (vm, node, b1, sizeof (*t));
164  t->src_sw_if_index = sw_if_index1;
165  //t->mirror_sw_if_index = si1->mirror_sw_if_index;
166  }
167  /* verify speculative enqueue, maybe switch current next frame */
168  vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
169  to_next, n_left_to_next,
170  bi0, bi1, next0, next1);
171  }
172  while (n_left_from > 0 && n_left_to_next > 0)
173  {
174  u32 bi0;
175  vlib_buffer_t *b0;
176  span_interface_t *si0;
177  u32 sw_if_index0;
178  u32 next0 = 0;
179 
180  /* speculatively enqueue b0 to the current next frame */
181  to_next[0] = bi0 = from[0];
182  to_next += 1;
183  n_left_to_next -= 1;
184  from += 1;
185  n_left_from -= 1;
186 
187  b0 = vlib_get_buffer (vm, bi0);
188  sw_if_index0 = vnet_buffer (b0)->sw_if_index[rxtx];
189  si0 = vec_elt_at_index (sm->interfaces, sw_if_index0);
190  span_mirror (vm, si0, b0, mirror_frames, is_rx);
191 
192  vnet_feature_next (sw_if_index0, &next0, b0);
193 
195  {
196  span_trace_t *t = vlib_add_trace (vm, node, b0, sizeof (*t));
197  t->src_sw_if_index = sw_if_index0;
198  }
199  /* verify speculative enqueue, maybe switch current next frame */
200  vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
201  n_left_to_next, bi0, next0);
202  }
203 
204  vlib_put_next_frame (vm, node, next_index, n_left_to_next);
205  }
206 
207 
208  for (sw_if_index = 0; sw_if_index < vec_len (mirror_frames); sw_if_index++)
209  {
210  if (mirror_frames[sw_if_index] == 0)
211  continue;
212 
213  vnet_put_frame_to_sw_interface (vnm, sw_if_index,
214  mirror_frames[sw_if_index]);
215  mirror_frames[sw_if_index] = 0;
216  }
217  vlib_node_increment_counter (vm, span_node.index, SPAN_ERROR_HITS,
218  n_span_packets);
219 
220  return frame->n_vectors;
221 }
222 
223 static uword
225  vlib_frame_t * frame)
226 {
227  return span_node_inline_fn (vm, node, frame, 1);
228 }
229 
230 static uword
232  vlib_frame_t * frame)
233 {
234  return span_node_inline_fn (vm, node, frame, 0);
235 }
236 
237 /* *INDENT-OFF* */
239  .function = span_input_node_fn,
240  .name = "span-input",
241  .vector_size = sizeof (u32),
242  .format_trace = format_span_trace,
244 
245  .n_errors = ARRAY_LEN(span_error_strings),
246  .error_strings = span_error_strings,
247 
248  .n_next_nodes = 0,
249 
250  /* edit / add dispositions here */
251  .next_nodes = {
252  [0] = "error-drop",
253  },
254 };
255 
257 
259  .function = span_output_node_fn,
260  .name = "span-output",
261  .vector_size = sizeof (u32),
262  .format_trace = format_span_trace,
264 
265  .n_errors = ARRAY_LEN(span_error_strings),
266  .error_strings = span_error_strings,
267 
268  .n_next_nodes = 0,
269 
270  /* edit / add dispositions here */
271  .next_nodes = {
272  [0] = "error-drop",
273  },
274 };
275 
277 
278 /* *INDENT-ON* */
279 
280 /*
281  * fd.io coding-style-patch-verification: ON
282  *
283  * Local Variables:
284  * eval: (c-set-style "gnu")
285  * End:
286  */
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:459
sll srl srl sll sra u16x4 i
Definition: vector_sse2.h:343
#define CLIB_UNUSED(x)
Definition: clib.h:79
bad routing header type(not 4)") sr_error (NO_MORE_SEGMENTS
vlib_node_registration_t span_input_node
(constructor) VLIB_REGISTER_NODE (span_input_node)
Definition: node.c:238
u32 mirror_sw_if_index
Definition: span.h:48
vlib_node_registration_t span_node
Definition: node.c:25
struct _vlib_node_registration vlib_node_registration_t
#define vec_validate_aligned(V, I, A)
Make sure vector is long enough for given index (no header, specified alignment)
Definition: vec.h:407
u8 * format_span_trace(u8 *s, va_list *args)
Definition: node.c:29
format_function_t format_vnet_sw_if_index_name
vlib_rx_or_tx_t
Definition: defs.h:44
#define static_always_inline
Definition: clib.h:85
#define vec_elt_at_index(v, i)
Get vector value at index i checking that i is in bounds.
static char * span_error_strings[]
Definition: node.c:54
#define clib_bitmap_foreach(i, ai, body)
Macro to iterate across set bits in a bitmap.
Definition: bitmap.h:361
span_interface_t * interfaces
Definition: span.h:33
static_always_inline void span_mirror(vlib_main_t *vm, span_interface_t *si0, vlib_buffer_t *b0, vlib_frame_t **mirror_frames, int is_rx)
Definition: node.c:61
vlib_node_registration_t span_output_node
(constructor) VLIB_REGISTER_NODE (span_output_node)
Definition: node.c:258
#define PREDICT_FALSE(x)
Definition: clib.h:97
vnet_main_t vnet_main
Definition: misc.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
span_main_t span_main
Definition: span.h:43
static_always_inline void vnet_feature_next(u32 sw_if_index, u32 *next0, vlib_buffer_t *b0)
Definition: feature.h:223
#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:216
#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:350
clib_bitmap_t * tx_mirror_ports
Definition: span.h:25
static void vlib_node_increment_counter(vlib_main_t *vm, u32 node_index, u32 counter_index, u64 increment)
Definition: node_funcs.h:1113
span_error_t
Definition: node.c:46
u16 n_vectors
Definition: node.h:344
static void vnet_put_frame_to_sw_interface(vnet_main_t *vnm, u32 sw_if_index, vlib_frame_t *f)
static uword span_input_node_fn(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
Definition: node.c:224
#define VNET_BUFFER_SPAN_CLONE
Definition: buffer.h:74
#define ARRAY_LEN(x)
Definition: clib.h:59
static_always_inline uword span_node_inline_fn(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame, int is_rx)
Definition: node.c:96
u16 cached_next_index
Definition: node.h:463
unsigned int u32
Definition: types.h:88
#define vnet_buffer(b)
Definition: buffer.h:361
VLIB_NODE_FUNCTION_MULTIARCH(dpdk_input_node, dpdk_input)
#define foreach_span_error
Definition: node.c:43
u32 num_rx_mirror_ports
Definition: span.h:26
#define VLIB_BUFFER_IS_TRACED
Definition: buffer.h:95
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
u32 src_sw_if_index
Definition: span.h:47
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
unsigned char u8
Definition: types.h:56
u32 num_tx_mirror_ports
Definition: span.h:27
static void * vlib_frame_vector_args(vlib_frame_t *f)
Get pointer to frame vector data.
Definition: node_funcs.h:253
static uword span_output_node_fn(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
Definition: node.c:231
clib_bitmap_t * rx_mirror_ports
Definition: span.h:24
#define VLIB_REGISTER_NODE(x,...)
Definition: node.h:143
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:418
#define CLIB_CACHE_LINE_BYTES
Definition: cache.h:67
u32 flags
buffer flags: VLIB_BUFFER_IS_TRACED: trace this buffer.
Definition: buffer.h:85
u32 max_sw_if_index
Definition: span.h:36
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
Definition: defs.h:46