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/pg/pg.h>
17 #include <vnet/ethernet/ethernet.h>
18 #include <vppinfra/error.h>
19 #include <vnet/devices/pci/ige.h>
20 #include <vnet/devices/pci/ixge.h>
21 #include <vnet/devices/pci/ixgev.h>
25 u32 cached_next_index;
26 u32 cached_sw_if_index;
28 /* Hash table to map sw_if_index to next node index */
29 uword *next_node_index_by_sw_if_index;
32 vlib_main_t *vlib_main;
33 vnet_main_t *vnet_main;
44 /* packet trace format function */
46 format_swap_trace (u8 * s, va_list * args)
48 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
49 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
50 swap_trace_t *t = va_arg (*args, swap_trace_t *);
52 s = format (s, "SWAP: dst now %U src now %U sw_if_index %d next_index %d",
53 format_ethernet_address, t->dst,
54 format_ethernet_address, t->src, t->sw_if_index, t->next_index);
58 #define foreach_hw_driver_next \
63 mac_swap_main_t mac_swap_main;
65 static vlib_node_registration_t mac_swap_node;
67 #define foreach_mac_swap_error \
68 _(SWAPS, "mac addresses swapped")
72 #define _(sym,str) MAC_SWAP_ERROR_##sym,
73 foreach_mac_swap_error
78 static char *mac_swap_error_strings[] = {
79 #define _(sym,string) string,
80 foreach_mac_swap_error
85 * To drop a pkt and increment one of the previous counters:
87 * set b0->error = error_node->errors[RANDOM_ERROR_SAMPLE];
88 * set next0 to a disposition index bound to "error-drop".
90 * To manually increment the specific counter MAC_SWAP_ERROR_SAMPLE:
92 * vlib_node_t *n = vlib_get_node (vm, mac_swap.index);
93 * u32 node_counter_base_index = n->error_heap_index;
94 * vlib_error_main_t * em = &vm->error_main;
95 * em->counters[node_counter_base_index + MAC_SWAP_ERROR_SAMPLE] += 1;
106 mac_swap_node_fn (vlib_main_t * vm,
107 vlib_node_runtime_t * node, vlib_frame_t * frame)
109 u32 n_left_from, *from, *to_next;
110 mac_swap_next_t next_index;
111 mac_swap_main_t *msm = &mac_swap_main;
112 vlib_node_t *n = vlib_get_node (vm, mac_swap_node.index);
113 u32 node_counter_base_index = n->error_heap_index;
114 vlib_error_main_t *em = &vm->error_main;
116 from = vlib_frame_vector_args (frame);
117 n_left_from = frame->n_vectors;
118 next_index = node->cached_next_index;
120 while (n_left_from > 0)
124 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
126 while (n_left_from >= 4 && n_left_to_next >= 2)
129 vlib_buffer_t *b0, *b1;
131 u32 sw_if_index0, sw_if_index1;
135 ethernet_header_t *h0, *h1;
138 /* Prefetch next iteration. */
140 vlib_buffer_t *p2, *p3;
142 p2 = vlib_get_buffer (vm, from[2]);
143 p3 = vlib_get_buffer (vm, from[3]);
145 vlib_prefetch_buffer_header (p2, LOAD);
146 vlib_prefetch_buffer_header (p3, LOAD);
148 CLIB_PREFETCH (p2->data, CLIB_CACHE_LINE_BYTES, STORE);
149 CLIB_PREFETCH (p3->data, CLIB_CACHE_LINE_BYTES, STORE);
152 to_next[0] = bi0 = from[0];
153 to_next[1] = bi1 = from[1];
159 b0 = vlib_get_buffer (vm, bi0);
160 b1 = vlib_get_buffer (vm, bi1);
162 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
163 next0 = msm->cached_next_index;
164 sw_if_index1 = vnet_buffer (b1)->sw_if_index[VLIB_RX];
165 next1 = msm->cached_next_index;
167 if (PREDICT_FALSE (msm->cached_sw_if_index != sw_if_index0))
170 hash_get (msm->next_node_index_by_sw_if_index, sw_if_index0);
173 vnet_hw_interface_t *hw0;
175 hw0 = vnet_get_sup_hw_interface (msm->vnet_main,
178 next0 = vlib_node_add_next (msm->vlib_main,
180 hw0->output_node_index);
181 hash_set (msm->next_node_index_by_sw_if_index,
182 sw_if_index0, next0);
186 msm->cached_sw_if_index = sw_if_index0;
187 msm->cached_next_index = next0;
190 if (PREDICT_FALSE (msm->cached_sw_if_index != sw_if_index1))
193 hash_get (msm->next_node_index_by_sw_if_index, sw_if_index1);
196 vnet_hw_interface_t *hw1;
198 hw1 = vnet_get_sup_hw_interface (msm->vnet_main,
201 next1 = vlib_node_add_next (msm->vlib_main,
203 hw1->output_node_index);
204 hash_set (msm->next_node_index_by_sw_if_index,
205 sw_if_index1, next1);
209 msm->cached_sw_if_index = sw_if_index1;
210 msm->cached_next_index = next1;
213 em->counters[node_counter_base_index + MAC_SWAP_ERROR_SWAPS] += 2;
215 /* reset buffer so we always point at the MAC hdr */
216 vlib_buffer_reset (b0);
217 vlib_buffer_reset (b1);
218 h0 = vlib_buffer_get_current (b0);
219 h1 = vlib_buffer_get_current (b1);
221 /* Swap 2 x src and dst mac addresses using 8-byte load/stores */
222 tmp0a = clib_net_to_host_u64 (((u64 *) (h0->dst_address))[0]);
223 tmp1a = clib_net_to_host_u64 (((u64 *) (h1->dst_address))[0]);
224 tmp0b = clib_net_to_host_u64 (((u64 *) (h0->src_address))[0]);
225 tmp1b = clib_net_to_host_u64 (((u64 *) (h1->src_address))[0]);
226 ((u64 *) (h0->dst_address))[0] = clib_host_to_net_u64 (tmp0b);
227 ((u64 *) (h1->dst_address))[0] = clib_host_to_net_u64 (tmp1b);
228 /* Move the ethertype from "b" to "a" */
231 tmp0a |= tmp0b & 0xFFFF;
232 ((u64 *) (h0->src_address))[0] = clib_host_to_net_u64 (tmp0a);
233 tmp1a |= tmp1b & 0xFFFF;
234 ((u64 *) (h1->src_address))[0] = clib_host_to_net_u64 (tmp1a);
236 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)))
238 if (b0->flags & VLIB_BUFFER_IS_TRACED)
241 vlib_add_trace (vm, node, b0, sizeof (*t));
242 clib_memcpy (t->src, h0->src_address, 6);
243 clib_memcpy (t->dst, h0->dst_address, 6);
244 t->sw_if_index = sw_if_index0;
245 t->next_index = next0;
247 if (b1->flags & VLIB_BUFFER_IS_TRACED)
250 vlib_add_trace (vm, node, b1, sizeof (*t));
251 clib_memcpy (t->src, h1->src_address, 6);
252 clib_memcpy (t->dst, h1->dst_address, 6);
253 t->sw_if_index = sw_if_index1;
254 t->next_index = next1;
258 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
259 to_next, n_left_to_next,
260 bi0, bi1, next0, next1);
263 while (n_left_from > 0 && n_left_to_next > 0)
271 ethernet_header_t *h0;
280 b0 = vlib_get_buffer (vm, bi0);
282 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
283 next0 = msm->cached_next_index;
285 if (PREDICT_FALSE (msm->cached_sw_if_index != sw_if_index0))
288 hash_get (msm->next_node_index_by_sw_if_index, sw_if_index0);
291 vnet_hw_interface_t *hw0;
293 hw0 = vnet_get_sup_hw_interface (msm->vnet_main,
296 next0 = vlib_node_add_next (msm->vlib_main,
298 hw0->output_node_index);
299 hash_set (msm->next_node_index_by_sw_if_index,
300 sw_if_index0, next0);
304 msm->cached_sw_if_index = sw_if_index0;
305 msm->cached_next_index = next0;
308 em->counters[node_counter_base_index + MAC_SWAP_ERROR_SWAPS] += 1;
310 /* reset buffer so we always point at the MAC hdr */
311 vlib_buffer_reset (b0);
312 h0 = vlib_buffer_get_current (b0);
314 /* Exchange src and dst, preserve the ethertype */
315 tmp0a = clib_net_to_host_u64 (((u64 *) (h0->dst_address))[0]);
316 tmp0b = clib_net_to_host_u64 (((u64 *) (h0->src_address))[0]);
317 ((u64 *) (h0->dst_address))[0] = clib_host_to_net_u64 (tmp0b);
319 tmp0a |= tmp0b & 0xFFFF;
320 ((u64 *) (h0->src_address))[0] = clib_host_to_net_u64 (tmp0a);
323 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
324 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
326 swap_trace_t *t = vlib_add_trace (vm, node, b0, sizeof (*t));
327 clib_memcpy (t->src, h0->src_address, 6);
328 clib_memcpy (t->dst, h0->dst_address, 6);
329 t->sw_if_index = sw_if_index0;
330 t->next_index = next0;
333 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
334 to_next, n_left_to_next,
338 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
341 return frame->n_vectors;
345 VLIB_REGISTER_NODE (mac_swap_node,static) = {
346 .function = mac_swap_node_fn,
348 .vector_size = sizeof (u32),
349 .format_trace = format_swap_trace,
350 .type = VLIB_NODE_TYPE_INTERNAL,
352 .n_errors = ARRAY_LEN(mac_swap_error_strings),
353 .error_strings = mac_swap_error_strings,
355 .n_next_nodes = MAC_SWAP_N_NEXT,
357 /* edit / add dispositions here */
359 [MAC_SWAP_NEXT_DROP] = "error-drop",
365 mac_swap_init (vlib_main_t * vm)
367 mac_swap_main_t *msm = &mac_swap_main;
369 msm->next_node_index_by_sw_if_index = hash_create (0, sizeof (uword));
370 msm->cached_next_index = (u32) ~ 0;
371 msm->cached_sw_if_index = (u32) ~ 0;
373 msm->vnet_main = vnet_get_main ();
375 /* Driver RX nodes send pkts here... */
376 #define _(a) ixge_set_next_node (IXGE_RX_NEXT_##a##_INPUT, "mac-swap");
377 foreach_hw_driver_next
379 #define _(a) ixgev_set_next_node (IXGEV_RX_NEXT_##a##_INPUT, "mac-swap");
380 foreach_hw_driver_next
382 #define _(a) ige_set_next_node (IGE_RX_NEXT_##a##_INPUT, "mac-swap");
383 foreach_hw_driver_next
388 VLIB_INIT_FUNCTION (mac_swap_init);
392 * fd.io coding-style-patch-verification: ON
395 * eval: (c-set-style "gnu")