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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
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>
29 format_mac_address (u8 * s, va_list * args)
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]);
36 /* packet trace format function */
37 static u8 * format_sample_trace (u8 * s, va_list * args)
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 *);
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",
46 format_mac_address, t->new_src_mac,
47 format_mac_address, t->new_dst_mac);
52 vlib_node_registration_t sample_node;
54 #define foreach_sample_error \
55 _(SWAPPED, "Mac swap packets processed")
58 #define _(sym,str) SAMPLE_ERROR_##sym,
64 static char * sample_error_strings[] = {
65 #define _(sym,string) string,
71 SAMPLE_NEXT_INTERFACE_OUTPUT,
75 #define foreach_mac_address_offset \
84 sample_node_fn (vlib_main_t * vm,
85 vlib_node_runtime_t * node,
88 u32 n_left_from, * from, * to_next;
89 sample_next_t next_index;
92 from = vlib_frame_vector_args (frame);
93 n_left_from = frame->n_vectors;
94 next_index = node->cached_next_index;
96 while (n_left_from > 0)
100 vlib_get_next_frame (vm, node, next_index,
101 to_next, n_left_to_next);
103 while (n_left_from >= 4 && n_left_to_next >= 2)
105 u32 next0 = SAMPLE_NEXT_INTERFACE_OUTPUT;
106 u32 next1 = SAMPLE_NEXT_INTERFACE_OUTPUT;
107 u32 sw_if_index0, sw_if_index1;
109 ethernet_header_t *en0, *en1;
111 vlib_buffer_t * b0, * b1;
113 /* Prefetch next iteration. */
115 vlib_buffer_t * p2, * p3;
117 p2 = vlib_get_buffer (vm, from[2]);
118 p3 = vlib_get_buffer (vm, from[3]);
120 vlib_prefetch_buffer_header (p2, LOAD);
121 vlib_prefetch_buffer_header (p3, LOAD);
123 CLIB_PREFETCH (p2->data, CLIB_CACHE_LINE_BYTES, STORE);
124 CLIB_PREFETCH (p3->data, CLIB_CACHE_LINE_BYTES, STORE);
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];
135 b0 = vlib_get_buffer (vm, bi0);
136 b1 = vlib_get_buffer (vm, bi1);
138 ASSERT (b0->current_data == 0);
139 ASSERT (b1->current_data == 0);
141 en0 = vlib_buffer_get_current (b0);
142 en1 = vlib_buffer_get_current (b1);
144 /* This is not the fastest way to swap src + dst mac addresses */
145 #define _(a) tmp0[a] = en0->src_address[a];
146 foreach_mac_address_offset;
148 #define _(a) en0->src_address[a] = en0->dst_address[a];
149 foreach_mac_address_offset;
151 #define _(a) en0->dst_address[a] = tmp0[a];
152 foreach_mac_address_offset;
155 #define _(a) tmp1[a] = en1->src_address[a];
156 foreach_mac_address_offset;
158 #define _(a) en1->src_address[a] = en1->dst_address[a];
159 foreach_mac_address_offset;
161 #define _(a) en1->dst_address[a] = tmp1[a];
162 foreach_mac_address_offset;
167 sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_RX];
168 sw_if_index1 = vnet_buffer(b1)->sw_if_index[VLIB_RX];
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;
176 if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE)))
178 if (b0->flags & VLIB_BUFFER_IS_TRACED)
181 vlib_add_trace (vm, node, b0, sizeof (*t));
182 t->sw_if_index = sw_if_index0;
183 t->next_index = next0;
184 clib_memcpy (t->new_src_mac, en0->src_address,
185 sizeof (t->new_src_mac));
186 clib_memcpy (t->new_dst_mac, en0->dst_address,
187 sizeof (t->new_dst_mac));
190 if (b1->flags & VLIB_BUFFER_IS_TRACED)
193 vlib_add_trace (vm, node, b1, sizeof (*t));
194 t->sw_if_index = sw_if_index1;
195 t->next_index = next1;
196 clib_memcpy (t->new_src_mac, en1->src_address,
197 sizeof (t->new_src_mac));
198 clib_memcpy (t->new_dst_mac, en1->dst_address,
199 sizeof (t->new_dst_mac));
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);
209 while (n_left_from > 0 && n_left_to_next > 0)
213 u32 next0 = SAMPLE_NEXT_INTERFACE_OUTPUT;
216 ethernet_header_t *en0;
218 /* speculatively enqueue b0 to the current next frame */
226 b0 = vlib_get_buffer (vm, bi0);
228 * Direct from the driver, we should be at offset 0
229 * aka at &b0->data[0]
231 ASSERT (b0->current_data == 0);
233 en0 = vlib_buffer_get_current (b0);
235 /* This is not the fastest way to swap src + dst mac addresses */
236 #define _(a) tmp0[a] = en0->src_address[a];
237 foreach_mac_address_offset;
239 #define _(a) en0->src_address[a] = en0->dst_address[a];
240 foreach_mac_address_offset;
242 #define _(a) en0->dst_address[a] = tmp0[a];
243 foreach_mac_address_offset;
246 sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_RX];
248 /* Send pkt back out the RX interface */
249 vnet_buffer(b0)->sw_if_index[VLIB_TX] = sw_if_index0;
251 if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE)
252 && (b0->flags & VLIB_BUFFER_IS_TRACED))) {
254 vlib_add_trace (vm, node, b0, sizeof (*t));
255 t->sw_if_index = sw_if_index0;
256 t->next_index = next0;
257 clib_memcpy (t->new_src_mac, en0->src_address,
258 sizeof (t->new_src_mac));
259 clib_memcpy (t->new_dst_mac, en0->dst_address,
260 sizeof (t->new_dst_mac));
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,
271 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
274 vlib_node_increment_counter (vm, sample_node.index,
275 SAMPLE_ERROR_SWAPPED, pkts_swapped);
276 return frame->n_vectors;
279 VLIB_REGISTER_NODE (sample_node) = {
280 .function = sample_node_fn,
282 .vector_size = sizeof (u32),
283 .format_trace = format_sample_trace,
284 .type = VLIB_NODE_TYPE_INTERNAL,
286 .n_errors = ARRAY_LEN(sample_error_strings),
287 .error_strings = sample_error_strings,
289 .n_next_nodes = SAMPLE_N_NEXT,
291 /* edit / add dispositions here */
293 [SAMPLE_NEXT_INTERFACE_OUTPUT] = "interface-output",