FD.io VPP  v17.10-9-gd594711
Vector Packet Processing
node.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 <vlib/vlib.h>
16 #include <vnet/vnet.h>
17 #include <vnet/pg/pg.h>
18 #include <vppinfra/error.h>
19 #include <sample/sample.h>
20 
21 typedef struct {
24  u8 new_src_mac[6];
25  u8 new_dst_mac[6];
27 
28 static u8 *
29 format_mac_address (u8 * s, va_list * args)
30 {
31  u8 *a = va_arg (*args, u8 *);
32  return format (s, "%02x:%02x:%02x:%02x:%02x:%02x",
33  a[0], a[1], a[2], a[3], a[4], a[5]);
34 }
35 
36 /* packet trace format function */
37 static u8 * format_sample_trace (u8 * s, va_list * args)
38 {
39  CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
40  CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
41  sample_trace_t * t = va_arg (*args, sample_trace_t *);
42 
43  s = format (s, "SAMPLE: sw_if_index %d, next index %d\n",
44  t->sw_if_index, t->next_index);
45  s = format (s, " new src %U -> new dst %U",
48 
49  return s;
50 }
51 
53 
54 #define foreach_sample_error \
55 _(SWAPPED, "Mac swap packets processed")
56 
57 typedef enum {
58 #define _(sym,str) SAMPLE_ERROR_##sym,
60 #undef _
63 
64 static char * sample_error_strings[] = {
65 #define _(sym,string) string,
67 #undef _
68 };
69 
70 typedef enum {
74 
75 #define foreach_mac_address_offset \
76 _(0) \
77 _(1) \
78 _(2) \
79 _(3) \
80 _(4) \
81 _(5)
82 
83 static uword
85  vlib_node_runtime_t * node,
86  vlib_frame_t * frame)
87 {
88  u32 n_left_from, * from, * to_next;
89  sample_next_t next_index;
90  u32 pkts_swapped = 0;
91 
92  from = vlib_frame_vector_args (frame);
93  n_left_from = frame->n_vectors;
94  next_index = node->cached_next_index;
95 
96  while (n_left_from > 0)
97  {
98  u32 n_left_to_next;
99 
100  vlib_get_next_frame (vm, node, next_index,
101  to_next, n_left_to_next);
102 
103  while (n_left_from >= 4 && n_left_to_next >= 2)
104  {
107  u32 sw_if_index0, sw_if_index1;
108  u8 tmp0[6], tmp1[6];
109  ethernet_header_t *en0, *en1;
110  u32 bi0, bi1;
111  vlib_buffer_t * b0, * b1;
112 
113  /* Prefetch next iteration. */
114  {
115  vlib_buffer_t * p2, * p3;
116 
117  p2 = vlib_get_buffer (vm, from[2]);
118  p3 = vlib_get_buffer (vm, from[3]);
119 
120  vlib_prefetch_buffer_header (p2, LOAD);
121  vlib_prefetch_buffer_header (p3, LOAD);
122 
125  }
126 
127  /* speculatively enqueue b0 and b1 to the current next frame */
128  to_next[0] = bi0 = from[0];
129  to_next[1] = bi1 = from[1];
130  from += 2;
131  to_next += 2;
132  n_left_from -= 2;
133  n_left_to_next -= 2;
134 
135  b0 = vlib_get_buffer (vm, bi0);
136  b1 = vlib_get_buffer (vm, bi1);
137 
138  ASSERT (b0->current_data == 0);
139  ASSERT (b1->current_data == 0);
140 
141  en0 = vlib_buffer_get_current (b0);
142  en1 = vlib_buffer_get_current (b1);
143 
144  /* This is not the fastest way to swap src + dst mac addresses */
145 #define _(a) tmp0[a] = en0->src_address[a];
147 #undef _
148 #define _(a) en0->src_address[a] = en0->dst_address[a];
150 #undef _
151 #define _(a) en0->dst_address[a] = tmp0[a];
153 #undef _
154 
155 #define _(a) tmp1[a] = en1->src_address[a];
157 #undef _
158 #define _(a) en1->src_address[a] = en1->dst_address[a];
160 #undef _
161 #define _(a) en1->dst_address[a] = tmp1[a];
163 #undef _
164 
165 
166 
167  sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_RX];
168  sw_if_index1 = vnet_buffer(b1)->sw_if_index[VLIB_RX];
169 
170  /* Send pkt back out the RX interface */
171  vnet_buffer(b0)->sw_if_index[VLIB_TX] = sw_if_index0;
172  vnet_buffer(b1)->sw_if_index[VLIB_TX] = sw_if_index1;
173 
174  pkts_swapped += 2;
175 
177  {
178  if (b0->flags & VLIB_BUFFER_IS_TRACED)
179  {
180  sample_trace_t *t =
181  vlib_add_trace (vm, node, b0, sizeof (*t));
182  t->sw_if_index = sw_if_index0;
183  t->next_index = next0;
185  sizeof (t->new_src_mac));
187  sizeof (t->new_dst_mac));
188 
189  }
190  if (b1->flags & VLIB_BUFFER_IS_TRACED)
191  {
192  sample_trace_t *t =
193  vlib_add_trace (vm, node, b1, sizeof (*t));
194  t->sw_if_index = sw_if_index1;
195  t->next_index = next1;
197  sizeof (t->new_src_mac));
199  sizeof (t->new_dst_mac));
200  }
201  }
202 
203  /* verify speculative enqueues, maybe switch current next frame */
204  vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
205  to_next, n_left_to_next,
206  bi0, bi1, next0, next1);
207  }
208 
209  while (n_left_from > 0 && n_left_to_next > 0)
210  {
211  u32 bi0;
212  vlib_buffer_t * b0;
214  u32 sw_if_index0;
215  u8 tmp0[6];
216  ethernet_header_t *en0;
217 
218  /* speculatively enqueue b0 to the current next frame */
219  bi0 = from[0];
220  to_next[0] = bi0;
221  from += 1;
222  to_next += 1;
223  n_left_from -= 1;
224  n_left_to_next -= 1;
225 
226  b0 = vlib_get_buffer (vm, bi0);
227  /*
228  * Direct from the driver, we should be at offset 0
229  * aka at &b0->data[0]
230  */
231  ASSERT (b0->current_data == 0);
232 
233  en0 = vlib_buffer_get_current (b0);
234 
235  /* This is not the fastest way to swap src + dst mac addresses */
236 #define _(a) tmp0[a] = en0->src_address[a];
238 #undef _
239 #define _(a) en0->src_address[a] = en0->dst_address[a];
241 #undef _
242 #define _(a) en0->dst_address[a] = tmp0[a];
244 #undef _
245 
246  sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_RX];
247 
248  /* Send pkt back out the RX interface */
249  vnet_buffer(b0)->sw_if_index[VLIB_TX] = sw_if_index0;
250 
252  && (b0->flags & VLIB_BUFFER_IS_TRACED))) {
253  sample_trace_t *t =
254  vlib_add_trace (vm, node, b0, sizeof (*t));
255  t->sw_if_index = sw_if_index0;
256  t->next_index = next0;
258  sizeof (t->new_src_mac));
260  sizeof (t->new_dst_mac));
261  }
262 
263  pkts_swapped += 1;
264 
265  /* verify speculative enqueue, maybe switch current next frame */
266  vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
267  to_next, n_left_to_next,
268  bi0, next0);
269  }
270 
271  vlib_put_next_frame (vm, node, next_index, n_left_to_next);
272  }
273 
275  SAMPLE_ERROR_SWAPPED, pkts_swapped);
276  return frame->n_vectors;
277 }
278 
280  .function = sample_node_fn,
281  .name = "sample",
282  .vector_size = sizeof (u32),
283  .format_trace = format_sample_trace,
284  .type = VLIB_NODE_TYPE_INTERNAL,
285 
286  .n_errors = ARRAY_LEN(sample_error_strings),
287  .error_strings = sample_error_strings,
288 
289  .n_next_nodes = SAMPLE_N_NEXT,
290 
291  /* edit / add dispositions here */
292  .next_nodes = {
293  [SAMPLE_NEXT_INTERFACE_OUTPUT] = "interface-output",
294  },
295 };
vlib_node_registration_t sample_node
(constructor) VLIB_REGISTER_NODE (sample_node)
Definition: node.c:52
#define CLIB_UNUSED(x)
Definition: clib.h:79
a
Definition: bitmap.h:516
u32 next_index
Definition: node.c:22
sample_error_t
Definition: node.c:57
u8 src_address[6]
Definition: packet.h:54
struct _vlib_node_registration vlib_node_registration_t
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:419
static u8 * format_mac_address(u8 *s, va_list *args)
Definition: node.c:29
i16 current_data
signed offset in data[], pre_data[] that we are currently processing.
Definition: buffer.h:68
#define foreach_sample_error
Definition: node.c:54
u8 dst_address[6]
Definition: packet.h:53
sample_next_t
Definition: node.c:70
#define vlib_prefetch_buffer_header(b, type)
Prefetch buffer metadata.
Definition: buffer.h:169
static u8 * format_sample_trace(u8 *s, va_list *args)
Definition: node.c:37
u8 new_dst_mac[6]
Definition: node.c:25
static void * vlib_buffer_get_current(vlib_buffer_t *b)
Get pointer to current data to process.
Definition: buffer.h:193
#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)
Finish enqueueing two buffers forward in the graph.
Definition: buffer_node.h:70
#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:364
static void vlib_node_increment_counter(vlib_main_t *vm, u32 node_index, u32 counter_index, u64 increment)
Definition: node_funcs.h:1158
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
#define VLIB_BUFFER_IS_TRACED
Definition: buffer.h:93
#define clib_memcpy(a, b, c)
Definition: string.h:69
#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
static uword sample_node_fn(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
Definition: node.c:84
u16 cached_next_index
Next frame index that vector arguments were last enqueued to last time this node ran.
Definition: node.h:456
#define ASSERT(truth)
unsigned int u32
Definition: types.h:88
u8 new_src_mac[6]
Definition: node.c:24
#define VLIB_NODE_FLAG_TRACE
Definition: node.h:259
static char * sample_error_strings[]
Definition: node.c:64
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
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:267
#define vnet_buffer(b)
Definition: buffer.h:306
#define VLIB_REGISTER_NODE(x,...)
Definition: node.h:143
u8 data[0]
Packet data.
Definition: buffer.h:157
u16 flags
Copy of main node flags.
Definition: node.h:450
u32 sw_if_index
Definition: node.c:23
#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
Definition: defs.h:46
#define foreach_mac_address_offset
Definition: node.c:75