FD.io VPP  v16.06
Vector Packet Processing
mac_swap.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/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>
22 
23 typedef struct {
26 
27  /* Hash table to map sw_if_index to next node index */
29 
30  /* convenience */
34 
35 typedef struct {
36  u8 src[6];
37  u8 dst[6];
40 } swap_trace_t;
41 
42 /* packet trace format function */
43 static u8 * format_swap_trace (u8 * s, va_list * args)
44 {
45  CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
46  CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
47  swap_trace_t * t = va_arg (*args, swap_trace_t *);
48 
49  s = format (s, "SWAP: dst now %U src now %U sw_if_index %d next_index %d",
52  t->sw_if_index,
53  t->next_index);
54  return s;
55 }
56 
57 #define foreach_hw_driver_next \
58  _(IP4) \
59  _(IP6) \
60  _(ETHERNET)
61 
63 
65 
66 #define foreach_mac_swap_error \
67 _(SWAPS, "mac addresses swapped")
68 
69 typedef enum {
70 #define _(sym,str) MAC_SWAP_ERROR_##sym,
72 #undef _
75 
76 static char * mac_swap_error_strings[] = {
77 #define _(sym,string) string,
79 #undef _
80 };
81 
82 /*
83  * To drop a pkt and increment one of the previous counters:
84  *
85  * set b0->error = error_node->errors[RANDOM_ERROR_SAMPLE];
86  * set next0 to a disposition index bound to "error-drop".
87  *
88  * To manually increment the specific counter MAC_SWAP_ERROR_SAMPLE:
89  *
90  * vlib_node_t *n = vlib_get_node (vm, mac_swap.index);
91  * u32 node_counter_base_index = n->error_heap_index;
92  * vlib_error_main_t * em = &vm->error_main;
93  * em->counters[node_counter_base_index + MAC_SWAP_ERROR_SAMPLE] += 1;
94  *
95  */
96 
97 typedef enum {
101 
102 static uword
104  vlib_node_runtime_t * node,
105  vlib_frame_t * frame)
106 {
107  u32 n_left_from, * from, * to_next;
108  mac_swap_next_t next_index;
110  vlib_node_t *n = vlib_get_node (vm, mac_swap_node.index);
111  u32 node_counter_base_index = n->error_heap_index;
112  vlib_error_main_t * em = &vm->error_main;
113 
114  from = vlib_frame_vector_args (frame);
115  n_left_from = frame->n_vectors;
116  next_index = node->cached_next_index;
117 
118  while (n_left_from > 0)
119  {
120  u32 n_left_to_next;
121 
122  vlib_get_next_frame (vm, node, next_index,
123  to_next, n_left_to_next);
124 
125  while (n_left_from >= 4 && n_left_to_next >= 2)
126  {
127  u32 bi0, bi1;
128  vlib_buffer_t * b0, * b1;
129  u32 next0, next1;
130  u32 sw_if_index0, sw_if_index1;
131  uword * p0, * p1;
132  u64 tmp0a, tmp0b;
133  u64 tmp1a, tmp1b;
134  ethernet_header_t * h0, *h1;
135 
136 
137  /* Prefetch next iteration. */
138  {
139  vlib_buffer_t * p2, * p3;
140 
141  p2 = vlib_get_buffer (vm, from[2]);
142  p3 = vlib_get_buffer (vm, from[3]);
143 
144  vlib_prefetch_buffer_header (p2, LOAD);
145  vlib_prefetch_buffer_header (p3, LOAD);
146 
149  }
150 
151  to_next[0] = bi0 = from[0];
152  to_next[1] = bi1 = from[1];
153  from += 2;
154  to_next += 2;
155  n_left_from -= 2;
156  n_left_to_next -= 2;
157 
158  b0 = vlib_get_buffer (vm, bi0);
159  b1 = vlib_get_buffer (vm, bi1);
160 
161  sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_RX];
162  next0 = msm->cached_next_index;
163  sw_if_index1 = vnet_buffer(b1)->sw_if_index[VLIB_RX];
164  next1 = msm->cached_next_index;
165 
166  if (PREDICT_FALSE (msm->cached_sw_if_index != sw_if_index0))
167  {
168  p0 = hash_get (msm->next_node_index_by_sw_if_index, sw_if_index0);
169  if (p0 == 0)
170  {
171  vnet_hw_interface_t *hw0;
172 
174  sw_if_index0);
175 
176  next0 = vlib_node_add_next (msm->vlib_main,
177  mac_swap_node.index,
178  hw0->output_node_index);
180  sw_if_index0, next0);
181  }
182  else
183  next0 = p0[0];
184  msm->cached_sw_if_index = sw_if_index0;
185  msm->cached_next_index = next0;
186  next1 = next0;
187  }
188  if (PREDICT_FALSE (msm->cached_sw_if_index != sw_if_index1))
189  {
190  p1 = hash_get (msm->next_node_index_by_sw_if_index, sw_if_index1);
191  if (p1 == 0)
192  {
193  vnet_hw_interface_t *hw1;
194 
196  sw_if_index1);
197 
198  next1 = vlib_node_add_next (msm->vlib_main,
199  mac_swap_node.index,
200  hw1->output_node_index);
202  sw_if_index1, next1);
203  }
204  else
205  next1 = p1[0];
206  msm->cached_sw_if_index = sw_if_index1;
207  msm->cached_next_index = next1;
208  }
209 
210  em->counters[node_counter_base_index + MAC_SWAP_ERROR_SWAPS] += 2;
211 
212  /* reset buffer so we always point at the MAC hdr */
213  vlib_buffer_reset (b0);
214  vlib_buffer_reset (b1);
215  h0 = vlib_buffer_get_current (b0);
216  h1 = vlib_buffer_get_current (b1);
217 
218  /* Swap 2 x src and dst mac addresses using 8-byte load/stores */
219  tmp0a = clib_net_to_host_u64(((u64 *)(h0->dst_address))[0]);
220  tmp1a = clib_net_to_host_u64(((u64 *)(h1->dst_address))[0]);
221  tmp0b = clib_net_to_host_u64(((u64 *)(h0->src_address))[0]);
222  tmp1b = clib_net_to_host_u64(((u64 *)(h1->src_address))[0]);
223  ((u64 *)(h0->dst_address))[0] = clib_host_to_net_u64(tmp0b);
224  ((u64 *)(h1->dst_address))[0] = clib_host_to_net_u64(tmp1b);
225  /* Move the ethertype from "b" to "a" */
226  tmp0a &= ~(0xFFFF);
227  tmp1a &= ~(0xFFFF);
228  tmp0a |= tmp0b & 0xFFFF;
229  ((u64 *)(h0->src_address))[0] = clib_host_to_net_u64(tmp0a);
230  tmp1a |= tmp1b & 0xFFFF;
231  ((u64 *)(h1->src_address))[0] = clib_host_to_net_u64(tmp1a);
232 
234  {
235  if (b0->flags & VLIB_BUFFER_IS_TRACED)
236  {
237  swap_trace_t *t = vlib_add_trace (vm, node, b0, sizeof (*t));
238  clib_memcpy (t->src, h0->src_address, 6);
239  clib_memcpy (t->dst, h0->dst_address, 6);
240  t->sw_if_index = sw_if_index0;
241  t->next_index = next0;
242  }
243  if (b1->flags & VLIB_BUFFER_IS_TRACED)
244  {
245  swap_trace_t *t = vlib_add_trace (vm, node, b1, sizeof (*t));
246  clib_memcpy (t->src, h1->src_address, 6);
247  clib_memcpy (t->dst, h1->dst_address, 6);
248  t->sw_if_index = sw_if_index1;
249  t->next_index = next1;
250  }
251  }
252 
253  vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
254  to_next, n_left_to_next,
255  bi0, bi1, next0, next1);
256  }
257 
258  while (n_left_from > 0 && n_left_to_next > 0)
259  {
260  u32 bi0;
261  vlib_buffer_t * b0;
262  u32 next0;
263  u32 sw_if_index0;
264  uword * p0;
265  u64 tmp0a, tmp0b;
266  ethernet_header_t * h0;
267 
268  bi0 = from[0];
269  to_next[0] = bi0;
270  from += 1;
271  to_next += 1;
272  n_left_from -= 1;
273  n_left_to_next -= 1;
274 
275  b0 = vlib_get_buffer (vm, bi0);
276 
277  sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_RX];
278  next0 = msm->cached_next_index;
279 
280  if (PREDICT_FALSE (msm->cached_sw_if_index != sw_if_index0))
281  {
282  p0 = hash_get (msm->next_node_index_by_sw_if_index, sw_if_index0);
283  if (p0 == 0)
284  {
285  vnet_hw_interface_t *hw0;
286 
288  sw_if_index0);
289 
290  next0 = vlib_node_add_next (msm->vlib_main,
291  mac_swap_node.index,
292  hw0->output_node_index);
294  sw_if_index0, next0);
295  }
296  else
297  next0 = p0[0];
298  msm->cached_sw_if_index = sw_if_index0;
299  msm->cached_next_index = next0;
300  }
301 
302  em->counters[node_counter_base_index + MAC_SWAP_ERROR_SWAPS] += 1;
303 
304  /* reset buffer so we always point at the MAC hdr */
305  vlib_buffer_reset (b0);
306  h0 = vlib_buffer_get_current (b0);
307 
308  /* Exchange src and dst, preserve the ethertype */
309  tmp0a = clib_net_to_host_u64(((u64 *)(h0->dst_address))[0]);
310  tmp0b = clib_net_to_host_u64(((u64 *)(h0->src_address))[0]);
311  ((u64 *)(h0->dst_address))[0] = clib_host_to_net_u64(tmp0b);
312  tmp0a &= ~(0xFFFF);
313  tmp0a |= tmp0b & 0xFFFF;
314  ((u64 *)(h0->src_address))[0] = clib_host_to_net_u64(tmp0a);
315 
316  /* ship it */
318  && (b0->flags & VLIB_BUFFER_IS_TRACED))) {
319  swap_trace_t *t = vlib_add_trace (vm, node, b0, sizeof (*t));
320  clib_memcpy (t->src, h0->src_address, 6);
321  clib_memcpy (t->dst, h0->dst_address, 6);
322  t->sw_if_index = sw_if_index0;
323  t->next_index = next0;
324  }
325 
326  vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
327  to_next, n_left_to_next,
328  bi0, next0);
329  }
330 
331  vlib_put_next_frame (vm, node, next_index, n_left_to_next);
332  }
333 
334  return frame->n_vectors;
335 }
336 
338  .function = mac_swap_node_fn,
339  .name = "mac-swap",
340  .vector_size = sizeof (u32),
341  .format_trace = format_swap_trace,
343 
344  .n_errors = ARRAY_LEN(mac_swap_error_strings),
345  .error_strings = mac_swap_error_strings,
346 
347  .n_next_nodes = MAC_SWAP_N_NEXT,
348 
349  /* edit / add dispositions here */
350  .next_nodes = {
351  [MAC_SWAP_NEXT_DROP] = "error-drop",
352  },
353 };
354 
356 {
358 
359  msm->next_node_index_by_sw_if_index = hash_create (0, sizeof (uword));
360  msm->cached_next_index = (u32)~0;
361  msm->cached_sw_if_index = (u32)~0;
362  msm->vlib_main = vm;
363  msm->vnet_main = vnet_get_main();
364 
365  /* Driver RX nodes send pkts here... */
366 #define _(a) ixge_set_next_node (IXGE_RX_NEXT_##a##_INPUT, "mac-swap");
368 #undef _
369 #define _(a) ixgev_set_next_node (IXGEV_RX_NEXT_##a##_INPUT, "mac-swap");
371 #undef _
372 #define _(a) ige_set_next_node (IGE_RX_NEXT_##a##_INPUT, "mac-swap");
374 #undef _
375 
376  return 0;
377 }
378 
380 
void vlib_put_next_frame(vlib_main_t *vm, vlib_node_runtime_t *r, u32 next_index, u32 n_vectors_left)
Definition: main.c:459
u32 error_heap_index
Definition: node.h:244
#define hash_set(h, key, value)
Definition: hash.h:237
#define CLIB_UNUSED(x)
Definition: clib.h:79
always_inline vlib_node_t * vlib_get_node(vlib_main_t *vm, u32 i)
Definition: node_funcs.h:46
bad routing header type(not 4)") sr_error (NO_MORE_SEGMENTS
u8 src_address[6]
Definition: packet.h:52
struct _vlib_node_registration vlib_node_registration_t
u8 src[6]
Definition: mac_swap.c:36
always_inline void * vlib_buffer_get_current(vlib_buffer_t *b)
Get pointer to current data to process.
Definition: buffer.h:184
u8 dst[6]
Definition: mac_swap.c:37
u32 cached_sw_if_index
Definition: mac_swap.c:25
#define foreach_hw_driver_next
Definition: mac_swap.c:57
vnet_main_t * vnet_get_main(void)
Definition: misc.c:45
u8 * format_ethernet_address(u8 *s, va_list *args)
Definition: format.c:43
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:109
u8 dst_address[6]
Definition: packet.h:51
#define foreach_mac_swap_error
Definition: mac_swap.c:66
unsigned long u64
Definition: types.h:89
static u8 * format_swap_trace(u8 *s, va_list *args)
Definition: mac_swap.c:43
mac_swap_main_t mac_swap_main
Definition: mac_swap.c:62
always_inline void * vlib_frame_vector_args(vlib_frame_t *f)
Definition: node_funcs.h:202
#define hash_get(h, key)
Definition: hash.h:231
clib_error_t * mac_swap_init(vlib_main_t *vm)
Definition: mac_swap.c:355
vlib_error_main_t error_main
Definition: main.h:124
#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)
Definition: buffer_node.h:43
#define vlib_validate_buffer_enqueue_x1(vm, node, next_index, to_next, n_left_to_next, bi0, next0)
Definition: buffer_node.h:83
always_inline void vlib_buffer_reset(vlib_buffer_t *b)
Reset current header & length to state they were in when packet was received.
Definition: buffer.h:211
#define vlib_get_next_frame(vm, node, next_index, vectors, n_vectors_left)
Definition: node_funcs.h:265
u32 cached_next_index
Definition: mac_swap.c:24
u64 * counters
Definition: error.h:73
vlib_main_t * vlib_main
Definition: mac_swap.c:31
u16 n_vectors
Definition: node.h:307
#define CLIB_PREFETCH(addr, size, type)
Definition: cache.h:82
vnet_main_t * vnet_main
Definition: mac_swap.c:32
#define clib_memcpy(a, b, c)
Definition: string.h:63
always_inline vnet_hw_interface_t * vnet_get_sup_hw_interface(vnet_main_t *vnm, u32 sw_if_index)
#define ARRAY_LEN(x)
Definition: clib.h:59
mac_swap_next_t
Definition: mac_swap.c:97
#define hash_create(elts, value_bytes)
Definition: hash.h:615
u16 cached_next_index
Definition: node.h:422
static vlib_node_registration_t mac_swap_node
(constructor) VLIB_REGISTER_NODE (mac_swap_node)
Definition: mac_swap.c:64
unsigned int u32
Definition: types.h:88
#define vnet_buffer(b)
Definition: buffer.h:300
u8 * format(u8 *s, char *fmt,...)
Definition: format.c:405
#define VLIB_NODE_FLAG_TRACE
Definition: node.h:225
always_inline uword vlib_node_add_next(vlib_main_t *vm, uword node, uword next_node)
Definition: node_funcs.h:919
mac_swap_error_t
Definition: mac_swap.c:69
#define VLIB_BUFFER_IS_TRACED
Definition: buffer.h:91
u32 sw_if_index
Definition: mac_swap.c:38
u64 uword
Definition: types.h:112
static char * mac_swap_error_strings[]
Definition: mac_swap.c:76
unsigned char u8
Definition: types.h:56
always_inline 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
#define vlib_prefetch_buffer_header(b, type)
Prefetch buffer metadata.
Definition: buffer.h:162
#define VLIB_REGISTER_NODE(x,...)
Definition: node.h:140
u8 data[0]
Packet data.
Definition: buffer.h:150
static uword mac_swap_node_fn(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
Definition: mac_swap.c:103
#define CLIB_CACHE_LINE_BYTES
Definition: cache.h:67
u32 flags
buffer flags: VLIB_BUFFER_IS_TRACED: trace this buffer.
Definition: buffer.h:84
always_inline vlib_buffer_t * vlib_get_buffer(vlib_main_t *vm, u32 buffer_index)
Translate buffer index into buffer pointer.
Definition: buffer_funcs.h:69
u32 next_index
Definition: mac_swap.c:39
Definition: defs.h:45
uword * next_node_index_by_sw_if_index
Definition: mac_swap.c:28