FD.io VPP  v16.09
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 {
25 
26 /* packet trace format function */
27 static u8 * format_sample_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  sample_trace_t * t = va_arg (*args, sample_trace_t *);
32 
33  s = format (s, "SAMPLE: sw_if_index %d, next index %d",
34  t->sw_if_index, t->next_index);
35  return s;
36 }
37 
39 
40 #define foreach_sample_error \
41 _(SWAPPED, "Mac swap packets processed")
42 
43 typedef enum {
44 #define _(sym,str) SAMPLE_ERROR_##sym,
46 #undef _
49 
50 static char * sample_error_strings[] = {
51 #define _(sym,string) string,
53 #undef _
54 };
55 
56 typedef enum {
60 
61 #define foreach_mac_address_offset \
62 _(0) \
63 _(1) \
64 _(2) \
65 _(3) \
66 _(4) \
67 _(5)
68 
69 static uword
71  vlib_node_runtime_t * node,
72  vlib_frame_t * frame)
73 {
74  u32 n_left_from, * from, * to_next;
75  sample_next_t next_index;
76  u32 pkts_swapped = 0;
77 
78  from = vlib_frame_vector_args (frame);
79  n_left_from = frame->n_vectors;
80  next_index = node->cached_next_index;
81 
82  while (n_left_from > 0)
83  {
84  u32 n_left_to_next;
85 
86  vlib_get_next_frame (vm, node, next_index,
87  to_next, n_left_to_next);
88 
89  while (n_left_from >= 4 && n_left_to_next >= 2)
90  {
93  u32 sw_if_index0, sw_if_index1;
94  u8 tmp0[6], tmp1[6];
95  ethernet_header_t *en0, *en1;
96  u32 bi0, bi1;
97  vlib_buffer_t * b0, * b1;
98 
99  /* Prefetch next iteration. */
100  {
101  vlib_buffer_t * p2, * p3;
102 
103  p2 = vlib_get_buffer (vm, from[2]);
104  p3 = vlib_get_buffer (vm, from[3]);
105 
106  vlib_prefetch_buffer_header (p2, LOAD);
107  vlib_prefetch_buffer_header (p3, LOAD);
108 
111  }
112 
113  /* speculatively enqueue b0 and b1 to the current next frame */
114  to_next[0] = bi0 = from[0];
115  to_next[1] = bi1 = from[1];
116  from += 2;
117  to_next += 2;
118  n_left_from -= 2;
119  n_left_to_next -= 2;
120 
121  b0 = vlib_get_buffer (vm, bi0);
122  b1 = vlib_get_buffer (vm, bi1);
123 
124  ASSERT (b0->current_data == 0);
125  ASSERT (b1->current_data == 0);
126 
127  en0 = vlib_buffer_get_current (b0);
128  en1 = vlib_buffer_get_current (b1);
129 
130  /* This is not the fastest way to swap src + dst mac addresses */
131 #define _(a) tmp0[a] = en0->src_address[a];
133 #undef _
134 #define _(a) en0->src_address[a] = en0->dst_address[a];
136 #undef _
137 #define _(a) en0->dst_address[a] = tmp0[a];
139 #undef _
140 
141 #define _(a) tmp1[a] = en1->src_address[a];
143 #undef _
144 #define _(a) en1->src_address[a] = en1->dst_address[a];
146 #undef _
147 #define _(a) en1->dst_address[a] = tmp1[a];
149 #undef _
150 
151 
152 
153  sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_RX];
154  sw_if_index1 = vnet_buffer(b1)->sw_if_index[VLIB_RX];
155 
156  /* Send pkt back out the RX interface */
157  vnet_buffer(b0)->sw_if_index[VLIB_TX] = sw_if_index0;
158  vnet_buffer(b1)->sw_if_index[VLIB_TX] = sw_if_index1;
159 
160  pkts_swapped += 2;
161 
163  {
164  if (b0->flags & VLIB_BUFFER_IS_TRACED)
165  {
166  sample_trace_t *t =
167  vlib_add_trace (vm, node, b0, sizeof (*t));
168  t->sw_if_index = sw_if_index0;
169  t->next_index = next0;
170  }
171  if (b1->flags & VLIB_BUFFER_IS_TRACED)
172  {
173  sample_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  }
178  }
179 
180  /* verify speculative enqueues, maybe switch current next frame */
181  vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
182  to_next, n_left_to_next,
183  bi0, bi1, next0, next1);
184  }
185 
186  while (n_left_from > 0 && n_left_to_next > 0)
187  {
188  u32 bi0;
189  vlib_buffer_t * b0;
191  u32 sw_if_index0;
192  u8 tmp0[6];
193  ethernet_header_t *en0;
194 
195  /* speculatively enqueue b0 to the current next frame */
196  bi0 = from[0];
197  to_next[0] = bi0;
198  from += 1;
199  to_next += 1;
200  n_left_from -= 1;
201  n_left_to_next -= 1;
202 
203  b0 = vlib_get_buffer (vm, bi0);
204  /*
205  * Direct from the driver, we should be at offset 0
206  * aka at &b0->data[0]
207  */
208  ASSERT (b0->current_data == 0);
209 
210  en0 = vlib_buffer_get_current (b0);
211 
212  /* This is not the fastest way to swap src + dst mac addresses */
213 #define _(a) tmp0[a] = en0->src_address[a];
215 #undef _
216 #define _(a) en0->src_address[a] = en0->dst_address[a];
218 #undef _
219 #define _(a) en0->dst_address[a] = tmp0[a];
221 #undef _
222 
223  sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_RX];
224 
225  /* Send pkt back out the RX interface */
226  vnet_buffer(b0)->sw_if_index[VLIB_TX] = sw_if_index0;
227 
229  && (b0->flags & VLIB_BUFFER_IS_TRACED))) {
230  sample_trace_t *t =
231  vlib_add_trace (vm, node, b0, sizeof (*t));
232  t->sw_if_index = sw_if_index0;
233  t->next_index = next0;
234  }
235 
236  pkts_swapped += 1;
237 
238  /* verify speculative enqueue, maybe switch current next frame */
239  vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
240  to_next, n_left_to_next,
241  bi0, next0);
242  }
243 
244  vlib_put_next_frame (vm, node, next_index, n_left_to_next);
245  }
246 
248  SAMPLE_ERROR_SWAPPED, pkts_swapped);
249  return frame->n_vectors;
250 }
251 
253  .function = sample_node_fn,
254  .name = "sample",
255  .vector_size = sizeof (u32),
256  .format_trace = format_sample_trace,
258 
259  .n_errors = ARRAY_LEN(sample_error_strings),
260  .error_strings = sample_error_strings,
261 
262  .n_next_nodes = SAMPLE_N_NEXT,
263 
264  /* edit / add dispositions here */
265  .next_nodes = {
266  [SAMPLE_NEXT_INTERFACE_OUTPUT] = "interface-output",
267  },
268 };
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
#define CLIB_UNUSED(x)
Definition: clib.h:79
#define foreach_sample_error
Definition: node.c:40
bad routing header type(not 4)") sr_error (NO_MORE_SEGMENTS
u32 next_index
Definition: node.c:22
static uword sample_node_fn(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
Definition: node.c:70
struct _vlib_node_registration vlib_node_registration_t
#define foreach_mac_address_offset
Definition: node.c:61
i16 current_data
signed offset in data[], pre_data[] that we are currently processing.
Definition: buffer.h:78
static void * vlib_buffer_get_current(vlib_buffer_t *b)
Get pointer to current data to process.
Definition: buffer.h:187
vlib_node_registration_t sample_node
(constructor) VLIB_REGISTER_NODE (sample_node)
Definition: node.c:38
sample_error_t
Definition: node.c:43
#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: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
static void vlib_node_increment_counter(vlib_main_t *vm, u32 node_index, u32 counter_index, u64 increment)
Definition: node_funcs.h:1111
static char * sample_error_strings[]
Definition: node.c:50
u16 n_vectors
Definition: node.h:344
#define CLIB_PREFETCH(addr, size, type)
Definition: cache.h:82
#define ARRAY_LEN(x)
Definition: clib.h:59
u16 cached_next_index
Definition: node.h:462
#define ASSERT(truth)
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
#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
Definition: defs.h:47
unsigned char u8
Definition: types.h:56
static u8 * format_sample_trace(u8 *s, va_list *args)
Definition: node.c:27
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_REGISTER_NODE(x,...)
Definition: node.h:143
u8 data[0]
Packet data.
Definition: buffer.h:151
u32 sw_if_index
Definition: node.c:23
#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
sample_next_t
Definition: node.c:56
Definition: defs.h:46