a58b832af146bfdb6ab71e78bc2a8ece6007adf7
[vpp.git] / src / vnet / ethernet / p2p_ethernet_input.c
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
22 #include <vnet/ethernet/p2p_ethernet.h>
23
24 #include <vppinfra/error.h>
25 #include <vppinfra/elog.h>
26
27 vlib_node_registration_t p2p_ethernet_input_node;
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",
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);
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,
51   foreach_p2p_ethernet_error
52 #undef _
53     P2PE_N_ERROR,
54 } p2p_ethernet_error_t;
55
56 static char *p2p_ethernet_error_strings[] = {
57 #define _(sym,string) string,
58   foreach_p2p_ethernet_error
59 #undef _
60 };
61
62 static uword
63 p2p_ethernet_input_node_fn (vlib_main_t * vm,
64                             vlib_node_runtime_t * node, vlib_frame_t * frame)
65 {
66   u32 n_trace = vlib_get_trace_count (vm, node);
67   u32 n_left_from, *from, *to_next;
68   u32 next_index;
69   u32 n_p2p_ethernet_packets = 0;
70
71   from = vlib_frame_vector_args (frame);
72   n_left_from = frame->n_vectors;
73   next_index = node->cached_next_index;
74
75   while (n_left_from > 0)
76     {
77       u32 n_left_to_next;
78
79       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
80
81       while (n_left_from >= 4 && n_left_to_next >= 2)
82         {
83           u32 bi0, bi1;
84           vlib_buffer_t *b0, *b1;
85           u32 next0 = 0, next1 = 0;
86           u32 sw_if_index0, sw_if_index1;
87           ethernet_header_t *en0, *en1;
88           u32 rx0, rx1;
89
90           bi0 = from[0];
91           bi1 = from[1];
92           to_next[0] = bi0;
93           to_next[1] = bi1;
94           from += 2;
95           to_next += 2;
96           n_left_to_next -= 2;
97           n_left_from -= 2;
98
99           b0 = vlib_get_buffer (vm, bi0);
100           b1 = vlib_get_buffer (vm, bi1);
101
102           en0 = vlib_buffer_get_current (b0);
103           sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
104           en1 = vlib_buffer_get_current (b1);
105           sw_if_index1 = vnet_buffer (b1)->sw_if_index[VLIB_RX];
106
107           vnet_feature_next (sw_if_index0, &next0, b0);
108           vnet_feature_next (sw_if_index1, &next1, b1);
109
110           rx0 = p2p_ethernet_lookup (sw_if_index0, en0->src_address);
111           rx1 = p2p_ethernet_lookup (sw_if_index1, en1->src_address);
112
113           if (rx0 != ~0)
114             {
115               /* Send pkt to p2p_ethernet RX interface */
116               vnet_buffer (b0)->sw_if_index[VLIB_RX] = rx0;
117               n_p2p_ethernet_packets += 1;
118
119               if (PREDICT_FALSE (n_trace > 0))
120                 {
121                   p2p_ethernet_trace_t *t0;
122                   vlib_trace_buffer (vm, node, next_index, b0,
123                                      1 /* follow_chain */ );
124                   vlib_set_trace_count (vm, node, --n_trace);
125                   t0 = vlib_add_trace (vm, node, b0, sizeof (*t0));
126                   t0->sw_if_index = sw_if_index0;
127                   t0->p2pe_sw_if_index = rx0;
128                 }
129             }
130           if (rx1 != ~0)
131             {
132               /* Send pkt to p2p_ethernet RX interface */
133               vnet_buffer (b1)->sw_if_index[VLIB_RX] = rx1;
134               n_p2p_ethernet_packets += 1;
135
136               if (PREDICT_FALSE (n_trace > 0))
137                 {
138                   p2p_ethernet_trace_t *t1;
139                   vlib_trace_buffer (vm, node, next_index, b1,
140                                      1 /* follow_chain */ );
141                   vlib_set_trace_count (vm, node, --n_trace);
142                   t1 = vlib_add_trace (vm, node, b1, sizeof (*t1));
143                   t1->sw_if_index = sw_if_index1;
144                   t1->p2pe_sw_if_index = rx1;
145                 }
146             }
147
148           /* verify speculative enqueue, maybe switch current next frame */
149           vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
150                                            to_next, n_left_to_next,
151                                            bi0, next0);
152           vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
153                                            to_next, n_left_to_next,
154                                            bi1, next1);
155         }
156
157       while (n_left_from > 0 && n_left_to_next > 0)
158         {
159           u32 bi0;
160           vlib_buffer_t *b0;
161           u32 next0 = 0;
162           u32 sw_if_index0;
163           ethernet_header_t *en0;
164           u32 rx0;
165
166           bi0 = from[0];
167           to_next[0] = bi0;
168           from += 1;
169           to_next += 1;
170           n_left_from -= 1;
171           n_left_to_next -= 1;
172
173           b0 = vlib_get_buffer (vm, bi0);
174
175           en0 = vlib_buffer_get_current (b0);
176           sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
177
178           vnet_feature_next (sw_if_index0, &next0, b0);
179
180           rx0 = p2p_ethernet_lookup (sw_if_index0, en0->src_address);
181           if (rx0 != ~0)
182             {
183               /* Send pkt to p2p_ethernet RX interface */
184               vnet_buffer (b0)->sw_if_index[VLIB_RX] = rx0;
185               n_p2p_ethernet_packets += 1;
186
187               if (PREDICT_FALSE (n_trace > 0))
188                 {
189                   p2p_ethernet_trace_t *t0;
190                   vlib_trace_buffer (vm, node, next_index, b0,
191                                      1 /* follow_chain */ );
192                   vlib_set_trace_count (vm, node, --n_trace);
193                   t0 = vlib_add_trace (vm, node, b0, sizeof (*t0));
194                   t0->sw_if_index = sw_if_index0;
195                   t0->p2pe_sw_if_index = rx0;
196                 }
197             }
198           else
199             {
200               if (PREDICT_FALSE (n_trace > 0))
201                 {
202                   node->flags |= VLIB_NODE_FLAG_TRACE;
203                 }
204             }
205
206           /* verify speculative enqueue, maybe switch current next frame */
207           vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
208                                            to_next, n_left_to_next,
209                                            bi0, next0);
210         }
211       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
212     }
213
214   vlib_node_increment_counter (vm, p2p_ethernet_input_node.index,
215                                P2PE_ERROR_HITS, n_p2p_ethernet_packets);
216   return frame->n_vectors;
217 }
218
219 /* *INDENT-OFF* */
220 VLIB_REGISTER_NODE (p2p_ethernet_input_node) = {
221   .function = p2p_ethernet_input_node_fn,
222   .name = "p2p-ethernet-input",
223   .vector_size = sizeof (u32),
224   .format_trace = format_p2p_ethernet_trace,
225   .type = VLIB_NODE_TYPE_INTERNAL,
226
227   .n_errors = ARRAY_LEN(p2p_ethernet_error_strings),
228   .error_strings = p2p_ethernet_error_strings,
229
230   .n_next_nodes = 1,
231
232   /* edit / add dispositions here */
233   .next_nodes = {
234     [0] = "error-drop",
235   },
236 };
237 /* *INDENT-ON* */
238
239 VLIB_NODE_FUNCTION_MULTIARCH (p2p_ethernet_input_node,
240                               p2p_ethernet_input_node_fn)
241 /*
242  * fd.io coding-style-patch-verification: ON
243  *
244  * Local Variables:
245  * eval: (c-set-style "gnu")
246  * End:
247  */