FD.io VPP  v19.01.3-6-g70449b9b9
Vector Packet Processing
p2p_ethernet_input.c
Go to the documentation of this file.
1 /*
2  * node.c: p2p ethernet vpp node
3  *
4  * Copyright (c) 2016 Cisco and/or its affiliates.
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at:
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 #include <vlib/vlib.h>
19 #include <vnet/vnet.h>
20 #include <vppinfra/error.h>
21 
23 
24 #include <vppinfra/error.h>
25 #include <vppinfra/elog.h>
26 
28 
29 /* packet trace format function */
30 u8 *
31 format_p2p_ethernet_trace (u8 * s, va_list * args)
32 {
33  CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
34  CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
35  p2p_ethernet_trace_t *t = va_arg (*args, p2p_ethernet_trace_t *);
36 
37  vnet_main_t *vnm = &vnet_main;
38  s = format (s, "P2P ethernet: %U -> %U",
41 
42  return s;
43 }
44 
45 #define foreach_p2p_ethernet_error \
46 _(HITS, "P2P ethernet incoming packets processed")
47 
48 typedef enum
49 {
50 #define _(sym,str) P2PE_ERROR_##sym,
52 #undef _
55 
56 static char *p2p_ethernet_error_strings[] = {
57 #define _(sym,string) string,
59 #undef _
60 };
61 
62 static uword
64  vlib_node_runtime_t * node, vlib_frame_t * frame)
65 {
66  u32 thread_index = vm->thread_index;
67  u32 n_trace = vlib_get_trace_count (vm, node);
68  u32 n_left_from, *from, *to_next;
69  u32 next_index;
70  u32 n_p2p_ethernet_packets = 0;
73 
74  from = vlib_frame_vector_args (frame);
75  n_left_from = frame->n_vectors;
76  next_index = node->cached_next_index;
77 
78  while (n_left_from > 0)
79  {
80  u32 n_left_to_next;
81 
82  vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
83 
84  while (n_left_from >= 4 && n_left_to_next >= 2)
85  {
86  u32 bi0, bi1;
87  vlib_buffer_t *b0, *b1;
88  u32 next0 = 0, next1 = 0;
89  u32 sw_if_index0, sw_if_index1;
90  ethernet_header_t *en0, *en1;
91  u32 rx0, rx1;
92 
93  bi0 = from[0];
94  bi1 = from[1];
95  to_next[0] = bi0;
96  to_next[1] = bi1;
97  from += 2;
98  to_next += 2;
99  n_left_to_next -= 2;
100  n_left_from -= 2;
101 
102  b0 = vlib_get_buffer (vm, bi0);
103  b1 = vlib_get_buffer (vm, bi1);
104 
105  en0 = vlib_buffer_get_current (b0);
106  sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
107  en1 = vlib_buffer_get_current (b1);
108  sw_if_index1 = vnet_buffer (b1)->sw_if_index[VLIB_RX];
109 
110  vnet_feature_next (&next0, b0);
111  vnet_feature_next (&next1, b1);
112 
113  rx0 = p2p_ethernet_lookup (sw_if_index0, en0->src_address);
114  rx1 = p2p_ethernet_lookup (sw_if_index1, en1->src_address);
115 
116  if (rx0 != ~0)
117  {
118  /* Send pkt to p2p_ethernet RX interface */
119  vnet_buffer (b0)->sw_if_index[VLIB_RX] = rx0;
120  n_p2p_ethernet_packets += 1;
121 
122  if (PREDICT_FALSE (n_trace > 0))
123  {
125  vlib_trace_buffer (vm, node, next_index, b0,
126  1 /* follow_chain */ );
127  vlib_set_trace_count (vm, node, --n_trace);
128  t0 = vlib_add_trace (vm, node, b0, sizeof (*t0));
129  t0->sw_if_index = sw_if_index0;
130  t0->p2pe_sw_if_index = rx0;
131  }
132 
133  vlib_increment_combined_counter (cm, thread_index, rx0, 1,
135  (vm, b0));
136  }
137  if (rx1 != ~0)
138  {
139  /* Send pkt to p2p_ethernet RX interface */
140  vnet_buffer (b1)->sw_if_index[VLIB_RX] = rx1;
141  n_p2p_ethernet_packets += 1;
142 
143  if (PREDICT_FALSE (n_trace > 0))
144  {
146  vlib_trace_buffer (vm, node, next_index, b1,
147  1 /* follow_chain */ );
148  vlib_set_trace_count (vm, node, --n_trace);
149  t1 = vlib_add_trace (vm, node, b1, sizeof (*t1));
150  t1->sw_if_index = sw_if_index1;
151  t1->p2pe_sw_if_index = rx1;
152  }
153 
154  vlib_increment_combined_counter (cm, thread_index, rx1, 1,
156  (vm, b1));
157  }
158 
159  /* verify speculative enqueue, maybe switch current next frame */
160  vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
161  to_next, n_left_to_next,
162  bi0, next0);
163  vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
164  to_next, n_left_to_next,
165  bi1, next1);
166  }
167 
168  while (n_left_from > 0 && n_left_to_next > 0)
169  {
170  u32 bi0;
171  vlib_buffer_t *b0;
172  u32 next0 = 0;
173  u32 sw_if_index0;
174  ethernet_header_t *en0;
175  u32 rx0;
176 
177  bi0 = from[0];
178  to_next[0] = bi0;
179  from += 1;
180  to_next += 1;
181  n_left_from -= 1;
182  n_left_to_next -= 1;
183 
184  b0 = vlib_get_buffer (vm, bi0);
185 
186  en0 = vlib_buffer_get_current (b0);
187  sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
188 
189  vnet_feature_next (&next0, b0);
190 
191  rx0 = p2p_ethernet_lookup (sw_if_index0, en0->src_address);
192  if (rx0 != ~0)
193  {
194  /* Send pkt to p2p_ethernet RX interface */
195  vnet_buffer (b0)->sw_if_index[VLIB_RX] = rx0;
196  n_p2p_ethernet_packets += 1;
197 
198  if (PREDICT_FALSE (n_trace > 0))
199  {
201  vlib_trace_buffer (vm, node, next_index, b0,
202  1 /* follow_chain */ );
203  vlib_set_trace_count (vm, node, --n_trace);
204  t0 = vlib_add_trace (vm, node, b0, sizeof (*t0));
205  t0->sw_if_index = sw_if_index0;
206  t0->p2pe_sw_if_index = rx0;
207  }
208 
209  vlib_increment_combined_counter (cm, thread_index, rx0, 1,
211  (vm, b0));
212  }
213  else
214  {
215  if (PREDICT_FALSE (n_trace > 0))
216  {
217  node->flags |= VLIB_NODE_FLAG_TRACE;
218  }
219  }
220 
221  /* verify speculative enqueue, maybe switch current next frame */
222  vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
223  to_next, n_left_to_next,
224  bi0, next0);
225  }
226  vlib_put_next_frame (vm, node, next_index, n_left_to_next);
227  }
228 
230  P2PE_ERROR_HITS, n_p2p_ethernet_packets);
231  return frame->n_vectors;
232 }
233 
234 /* *INDENT-OFF* */
236  .function = p2p_ethernet_input_node_fn,
237  .name = "p2p-ethernet-input",
238  .vector_size = sizeof (u32),
239  .format_trace = format_p2p_ethernet_trace,
240  .type = VLIB_NODE_TYPE_INTERNAL,
241 
243  .error_strings = p2p_ethernet_error_strings,
244 
245  .n_next_nodes = 1,
246 
247  /* edit / add dispositions here */
248  .next_nodes = {
249  [0] = "error-drop",
250  },
251 };
252 /* *INDENT-ON* */
253 
256 /*
257  * fd.io coding-style-patch-verification: ON
258  *
259  * Local Variables:
260  * eval: (c-set-style "gnu")
261  * End:
262  */
#define foreach_p2p_ethernet_error
u8 * format_p2p_ethernet_trace(u8 *s, va_list *args)
#define CLIB_UNUSED(x)
Definition: clib.h:82
static u32 vlib_get_trace_count(vlib_main_t *vm, vlib_node_runtime_t *rt)
Definition: trace_funcs.h:156
static void vlib_increment_combined_counter(vlib_combined_counter_main_t *cm, u32 thread_index, u32 index, u64 n_packets, u64 n_bytes)
Increment a combined counter.
Definition: counter.h:220
vnet_main_t * vnet_get_main(void)
Definition: misc.c:47
vnet_interface_main_t interface_main
Definition: vnet.h:56
u8 src_address[6]
Definition: packet.h:56
u32 thread_index
Definition: main.h:179
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:419
static uword vlib_buffer_length_in_chain(vlib_main_t *vm, vlib_buffer_t *b)
Get length in bytes of the buffer chain.
Definition: buffer_funcs.h:267
format_function_t format_vnet_sw_if_index_name
unsigned char u8
Definition: types.h:56
static void vlib_trace_buffer(vlib_main_t *vm, vlib_node_runtime_t *r, u32 next_index, vlib_buffer_t *b, int follow_chain)
Definition: trace_funcs.h:114
static char * p2p_ethernet_error_strings[]
vlib_combined_counter_main_t * combined_sw_if_counters
Definition: interface.h:839
unsigned int u32
Definition: types.h:88
#define VLIB_NODE_FUNCTION_MULTIARCH(node, fn)
Definition: node.h:223
p2p_ethernet_error_t
static void * vlib_buffer_get_current(vlib_buffer_t *b)
Get pointer to current data to process.
Definition: buffer.h:214
#define PREDICT_FALSE(x)
Definition: clib.h:111
vnet_main_t vnet_main
Definition: misc.c:43
#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:218
#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:338
static void vlib_node_increment_counter(vlib_main_t *vm, u32 node_index, u32 counter_index, u64 increment)
Definition: node_funcs.h:1150
The fine-grained event logger allows lightweight, thread-safe event logging at minimum cost...
u32 p2p_ethernet_lookup(u32 parent_if_index, u8 *client_mac)
Definition: p2p_ethernet.c:35
#define VLIB_REGISTER_NODE(x,...)
Definition: node.h:169
u16 n_vectors
Definition: node.h:420
vlib_node_registration_t p2p_ethernet_input_node
(constructor) VLIB_REGISTER_NODE (p2p_ethernet_input_node)
vlib_main_t * vm
Definition: buffer.c:301
static_always_inline void vnet_feature_next(u32 *next0, vlib_buffer_t *b0)
Definition: feature.h:295
#define ARRAY_LEN(x)
Definition: clib.h:62
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:452
u16 cached_next_index
Next frame index that vector arguments were last enqueued to last time this node ran.
Definition: node.h:537
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:57
struct _vlib_node_registration vlib_node_registration_t
static uword p2p_ethernet_input_node_fn(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
u64 uword
Definition: types.h:112
static void * vlib_frame_vector_args(vlib_frame_t *f)
Get pointer to frame vector data.
Definition: node_funcs.h:244
A collection of combined counters.
Definition: counter.h:188
#define vnet_buffer(b)
Definition: buffer.h:368
u16 flags
Copy of main node flags.
Definition: node.h:531
static void vlib_set_trace_count(vlib_main_t *vm, vlib_node_runtime_t *rt, u32 count)
Definition: trace_funcs.h:172
#define VLIB_NODE_FLAG_TRACE
Definition: node.h:326
static vlib_buffer_t * vlib_get_buffer(vlib_main_t *vm, u32 buffer_index)
Translate buffer index into buffer pointer.
Definition: buffer_funcs.h:62
Definition: defs.h:46