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>
26 /* packet trace format function */
27 static u8 * format_sample_trace (u8 * s, va_list * args)
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 *);
33 s = format (s, "SAMPLE: sw_if_index %d, next index %d",
34 t->sw_if_index, t->next_index);
38 vlib_node_registration_t sample_node;
40 #define foreach_sample_error \
41 _(SWAPPED, "Mac swap packets processed")
44 #define _(sym,str) SAMPLE_ERROR_##sym,
50 static char * sample_error_strings[] = {
51 #define _(sym,string) string,
57 SAMPLE_NEXT_INTERFACE_OUTPUT,
61 #define foreach_mac_address_offset \
70 sample_node_fn (vlib_main_t * vm,
71 vlib_node_runtime_t * node,
74 u32 n_left_from, * from, * to_next;
75 sample_next_t next_index;
78 from = vlib_frame_vector_args (frame);
79 n_left_from = frame->n_vectors;
80 next_index = node->cached_next_index;
82 while (n_left_from > 0)
86 vlib_get_next_frame (vm, node, next_index,
87 to_next, n_left_to_next);
89 while (n_left_from >= 4 && n_left_to_next >= 2)
91 u32 next0 = SAMPLE_NEXT_INTERFACE_OUTPUT;
92 u32 next1 = SAMPLE_NEXT_INTERFACE_OUTPUT;
93 u32 sw_if_index0, sw_if_index1;
95 ethernet_header_t *en0, *en1;
97 vlib_buffer_t * b0, * b1;
99 /* Prefetch next iteration. */
101 vlib_buffer_t * p2, * p3;
103 p2 = vlib_get_buffer (vm, from[2]);
104 p3 = vlib_get_buffer (vm, from[3]);
106 vlib_prefetch_buffer_header (p2, LOAD);
107 vlib_prefetch_buffer_header (p3, LOAD);
109 CLIB_PREFETCH (p2->data, CLIB_CACHE_LINE_BYTES, STORE);
110 CLIB_PREFETCH (p3->data, CLIB_CACHE_LINE_BYTES, STORE);
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];
121 b0 = vlib_get_buffer (vm, bi0);
122 b1 = vlib_get_buffer (vm, bi1);
124 ASSERT (b0->current_data == 0);
125 ASSERT (b1->current_data == 0);
127 en0 = vlib_buffer_get_current (b0);
128 en1 = vlib_buffer_get_current (b1);
130 /* This is not the fastest way to swap src + dst mac addresses */
131 #define _(a) tmp0[a] = en0->src_address[a];
132 foreach_mac_address_offset;
134 #define _(a) en0->src_address[a] = en0->dst_address[a];
135 foreach_mac_address_offset;
137 #define _(a) en0->dst_address[a] = tmp0[a];
138 foreach_mac_address_offset;
141 #define _(a) tmp1[a] = en1->src_address[a];
142 foreach_mac_address_offset;
144 #define _(a) en1->src_address[a] = en1->dst_address[a];
145 foreach_mac_address_offset;
147 #define _(a) en1->dst_address[a] = tmp1[a];
148 foreach_mac_address_offset;
153 sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_RX];
154 sw_if_index1 = vnet_buffer(b1)->sw_if_index[VLIB_RX];
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;
162 if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE)))
164 if (b0->flags & VLIB_BUFFER_IS_TRACED)
167 vlib_add_trace (vm, node, b0, sizeof (*t));
168 t->sw_if_index = sw_if_index0;
169 t->next_index = next0;
171 if (b1->flags & VLIB_BUFFER_IS_TRACED)
174 vlib_add_trace (vm, node, b1, sizeof (*t));
175 t->sw_if_index = sw_if_index1;
176 t->next_index = next1;
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);
186 while (n_left_from > 0 && n_left_to_next > 0)
190 u32 next0 = SAMPLE_NEXT_INTERFACE_OUTPUT;
193 ethernet_header_t *en0;
195 /* speculatively enqueue b0 to the current next frame */
203 b0 = vlib_get_buffer (vm, bi0);
205 * Direct from the driver, we should be at offset 0
206 * aka at &b0->data[0]
208 ASSERT (b0->current_data == 0);
210 en0 = vlib_buffer_get_current (b0);
212 /* This is not the fastest way to swap src + dst mac addresses */
213 #define _(a) tmp0[a] = en0->src_address[a];
214 foreach_mac_address_offset;
216 #define _(a) en0->src_address[a] = en0->dst_address[a];
217 foreach_mac_address_offset;
219 #define _(a) en0->dst_address[a] = tmp0[a];
220 foreach_mac_address_offset;
223 sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_RX];
225 /* Send pkt back out the RX interface */
226 vnet_buffer(b0)->sw_if_index[VLIB_TX] = sw_if_index0;
228 if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE)
229 && (b0->flags & VLIB_BUFFER_IS_TRACED))) {
231 vlib_add_trace (vm, node, b0, sizeof (*t));
232 t->sw_if_index = sw_if_index0;
233 t->next_index = next0;
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,
244 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
247 vlib_node_increment_counter (vm, sample_node.index,
248 SAMPLE_ERROR_SWAPPED, pkts_swapped);
249 return frame->n_vectors;
252 VLIB_REGISTER_NODE (sample_node) = {
253 .function = sample_node_fn,
255 .vector_size = sizeof (u32),
256 .format_trace = format_sample_trace,
257 .type = VLIB_NODE_TYPE_INTERNAL,
259 .n_errors = ARRAY_LEN(sample_error_strings),
260 .error_strings = sample_error_strings,
262 .n_next_nodes = SAMPLE_N_NEXT,
264 /* edit / add dispositions here */
266 [SAMPLE_NEXT_INTERFACE_OUTPUT] = "interface-output",