2 * node.c: p2p ethernet vpp node
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:
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include <vlib/vlib.h>
19 #include <vnet/vnet.h>
20 #include <vppinfra/error.h>
22 #include <vnet/ethernet/p2p_ethernet.h>
24 #include <vppinfra/error.h>
25 #include <vppinfra/elog.h>
27 vlib_node_registration_t p2p_ethernet_input_node;
29 /* packet trace format function */
31 format_p2p_ethernet_trace (u8 * s, va_list * args)
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 *);
37 vnet_main_t *vnm = &vnet_main;
38 s = format (s, "P2P ethernet: %U -> %U",
39 format_vnet_sw_if_index_name, vnm, t->sw_if_index,
40 format_vnet_sw_if_index_name, vnm, t->p2pe_sw_if_index);
45 #define foreach_p2p_ethernet_error \
46 _(HITS, "P2P ethernet incoming packets processed")
50 #define _(sym,str) P2PE_ERROR_##sym,
51 foreach_p2p_ethernet_error
54 } p2p_ethernet_error_t;
56 static char *p2p_ethernet_error_strings[] = {
57 #define _(sym,string) string,
58 foreach_p2p_ethernet_error
63 p2p_ethernet_input_node_fn (vlib_main_t * vm,
64 vlib_node_runtime_t * node, vlib_frame_t * frame)
66 u32 thread_index = vlib_get_thread_index ();
67 u32 n_trace = vlib_get_trace_count (vm, node);
68 u32 n_left_from, *from, *to_next;
70 u32 n_p2p_ethernet_packets = 0;
71 vlib_combined_counter_main_t *cm =
72 vnet_get_main ()->interface_main.combined_sw_if_counters;
74 from = vlib_frame_vector_args (frame);
75 n_left_from = frame->n_vectors;
76 next_index = node->cached_next_index;
78 while (n_left_from > 0)
82 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
84 while (n_left_from >= 4 && n_left_to_next >= 2)
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;
102 b0 = vlib_get_buffer (vm, bi0);
103 b1 = vlib_get_buffer (vm, bi1);
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];
110 vnet_feature_next (sw_if_index0, &next0, b0);
111 vnet_feature_next (sw_if_index1, &next1, b1);
113 rx0 = p2p_ethernet_lookup (sw_if_index0, en0->src_address);
114 rx1 = p2p_ethernet_lookup (sw_if_index1, en1->src_address);
118 /* Send pkt to p2p_ethernet RX interface */
119 vnet_buffer (b0)->sw_if_index[VLIB_RX] = rx0;
120 n_p2p_ethernet_packets += 1;
122 if (PREDICT_FALSE (n_trace > 0))
124 p2p_ethernet_trace_t *t0;
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;
133 vlib_increment_combined_counter (cm, thread_index, rx0, 1,
134 vlib_buffer_length_in_chain
139 /* Send pkt to p2p_ethernet RX interface */
140 vnet_buffer (b1)->sw_if_index[VLIB_RX] = rx1;
141 n_p2p_ethernet_packets += 1;
143 if (PREDICT_FALSE (n_trace > 0))
145 p2p_ethernet_trace_t *t1;
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;
154 vlib_increment_combined_counter (cm, thread_index, rx1, 1,
155 vlib_buffer_length_in_chain
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,
163 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
164 to_next, n_left_to_next,
168 while (n_left_from > 0 && n_left_to_next > 0)
174 ethernet_header_t *en0;
184 b0 = vlib_get_buffer (vm, bi0);
186 en0 = vlib_buffer_get_current (b0);
187 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
189 vnet_feature_next (sw_if_index0, &next0, b0);
191 rx0 = p2p_ethernet_lookup (sw_if_index0, en0->src_address);
194 /* Send pkt to p2p_ethernet RX interface */
195 vnet_buffer (b0)->sw_if_index[VLIB_RX] = rx0;
196 n_p2p_ethernet_packets += 1;
198 if (PREDICT_FALSE (n_trace > 0))
200 p2p_ethernet_trace_t *t0;
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;
209 vlib_increment_combined_counter (cm, thread_index, rx0, 1,
210 vlib_buffer_length_in_chain
215 if (PREDICT_FALSE (n_trace > 0))
217 node->flags |= VLIB_NODE_FLAG_TRACE;
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,
226 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
229 vlib_node_increment_counter (vm, p2p_ethernet_input_node.index,
230 P2PE_ERROR_HITS, n_p2p_ethernet_packets);
231 return frame->n_vectors;
235 VLIB_REGISTER_NODE (p2p_ethernet_input_node) = {
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,
242 .n_errors = ARRAY_LEN(p2p_ethernet_error_strings),
243 .error_strings = p2p_ethernet_error_strings,
247 /* edit / add dispositions here */
254 VLIB_NODE_FUNCTION_MULTIARCH (p2p_ethernet_input_node,
255 p2p_ethernet_input_node_fn)
257 * fd.io coding-style-patch-verification: ON
260 * eval: (c-set-style "gnu")