dpdk: Add support for Mellanox ConnectX-4 devices
[vpp.git] / src / vnet / hdlc / node.c
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 /*
16  * hdlc_node.c: hdlc packet processing
17  *
18  * Copyright (c) 2010 Eliot Dresselhaus
19  *
20  * Permission is hereby granted, free of charge, to any person obtaining
21  * a copy of this software and associated documentation files (the
22  * "Software"), to deal in the Software without restriction, including
23  * without limitation the rights to use, copy, modify, merge, publish,
24  * distribute, sublicense, and/or sell copies of the Software, and to
25  * permit persons to whom the Software is furnished to do so, subject to
26  * the following conditions:
27  *
28  * The above copyright notice and this permission notice shall be
29  * included in all copies or substantial portions of the Software.
30  *
31  *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
32  *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
33  *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
34  *  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
35  *  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
36  *  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
37  *  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
38  */
39
40 #include <vlib/vlib.h>
41 #include <vnet/pg/pg.h>
42 #include <vnet/hdlc/hdlc.h>
43 #include <vppinfra/sparse_vec.h>
44
45 #define foreach_hdlc_input_next                 \
46   _ (PUNT, "error-punt")                        \
47   _ (DROP, "error-drop")
48
49 typedef enum {
50 #define _(s,n) HDLC_INPUT_NEXT_##s,
51   foreach_hdlc_input_next
52 #undef _
53   HDLC_INPUT_N_NEXT,
54 } hdlc_input_next_t;
55
56 typedef struct {
57   u8 packet_data[32];
58 } hdlc_input_trace_t;
59
60 static u8 * format_hdlc_input_trace (u8 * s, va_list * va)
61 {
62   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*va, vlib_main_t *);
63   CLIB_UNUSED (vlib_node_t * node) = va_arg (*va, vlib_node_t *);
64   hdlc_input_trace_t * t = va_arg (*va, hdlc_input_trace_t *);
65
66   s = format (s, "%U", format_hdlc_header, t->packet_data);
67
68   return s;
69 }
70
71 typedef struct {
72   /* Sparse vector mapping hdlc protocol in network byte order
73      to next index. */
74   u16 * next_by_protocol;
75
76   u32 * sparse_index_by_next_index;
77 } hdlc_input_runtime_t;
78
79 static uword
80 hdlc_input (vlib_main_t * vm,
81            vlib_node_runtime_t * node,
82            vlib_frame_t * from_frame)
83 {
84   hdlc_input_runtime_t * rt = (void *) node->runtime_data;
85   u32 n_left_from, next_index, i_next, * from, * to_next;
86
87   from = vlib_frame_vector_args (from_frame);
88   n_left_from = from_frame->n_vectors;
89
90   if (node->flags & VLIB_NODE_FLAG_TRACE)
91     vlib_trace_frame_buffers_only (vm, node,
92                                    from,
93                                    n_left_from,
94                                    sizeof (from[0]),
95                                    sizeof (hdlc_input_trace_t));
96
97   next_index = node->cached_next_index;
98   i_next = vec_elt (rt->sparse_index_by_next_index, next_index);
99
100   while (n_left_from > 0)
101     {
102       u32 n_left_to_next;
103
104       vlib_get_next_frame (vm, node, next_index,
105                            to_next, n_left_to_next);
106
107       while (n_left_from >= 4 && n_left_to_next >= 2)
108         {
109           u32 bi0, bi1;
110           vlib_buffer_t * b0, * b1;
111           hdlc_header_t * h0, * h1;
112           u32 i0, i1, len0, len1, protocol0, protocol1, enqueue_code;
113
114           /* Prefetch next iteration. */
115           {
116             vlib_buffer_t * b2, * b3;
117
118             b2 = vlib_get_buffer (vm, from[2]);
119             b3 = vlib_get_buffer (vm, from[3]);
120
121             vlib_prefetch_buffer_header (b2, LOAD);
122             vlib_prefetch_buffer_header (b3, LOAD);
123
124             CLIB_PREFETCH (b2->data, sizeof (h0[0]), LOAD);
125             CLIB_PREFETCH (b3->data, sizeof (h1[0]), LOAD);
126           }
127
128           bi0 = from[0];
129           bi1 = from[1];
130           to_next[0] = bi0;
131           to_next[1] = bi1;
132           from += 2;
133           to_next += 2;
134           n_left_to_next -= 2;
135           n_left_from -= 2;
136
137           b0 = vlib_get_buffer (vm, bi0);
138           b1 = vlib_get_buffer (vm, bi1);
139
140           h0 = (void *) (b0->data + b0->current_data);
141           h1 = (void *) (b1->data + b1->current_data);
142
143           protocol0 = h0->protocol;
144           protocol1 = h1->protocol;
145
146           /* Add padding bytes for OSI protocols. */
147           len0 = sizeof (h0[0]);
148           len1 = sizeof (h1[0]);
149           
150           len0 += protocol0 == clib_host_to_net_u16 (HDLC_PROTOCOL_osi);
151           len1 += protocol1 == clib_host_to_net_u16 (HDLC_PROTOCOL_osi);
152
153           b0->current_data += len0;
154           b1->current_data += len1;
155
156           b0->current_length -= len0;
157           b1->current_length -= len1;
158
159           /* Index sparse array with network byte order. */
160           sparse_vec_index2 (rt->next_by_protocol, protocol0, protocol1, &i0, &i1);
161
162           b0->error = node->errors[i0 == SPARSE_VEC_INVALID_INDEX ? HDLC_ERROR_UNKNOWN_PROTOCOL : HDLC_ERROR_NONE];
163           b1->error = node->errors[i1 == SPARSE_VEC_INVALID_INDEX ? HDLC_ERROR_UNKNOWN_PROTOCOL : HDLC_ERROR_NONE];
164
165           enqueue_code = (i0 != i_next) + 2*(i1 != i_next);
166
167           if (PREDICT_FALSE (enqueue_code != 0))
168             {
169               switch (enqueue_code)
170                 {
171                 case 1:
172                   /* A B A */
173                   to_next[-2] = bi1;
174                   to_next -= 1;
175                   n_left_to_next += 1;
176                   vlib_set_next_frame_buffer (vm, node, vec_elt (rt->next_by_protocol, i0), bi0);
177                   break;
178
179                 case 2:
180                   /* A A B */
181                   to_next -= 1;
182                   n_left_to_next += 1;
183                   vlib_set_next_frame_buffer (vm, node, vec_elt (rt->next_by_protocol, i1), bi1);
184                   break;
185
186                 case 3:
187                   /* A B B or A B C */
188                   to_next -= 2;
189                   n_left_to_next += 2;
190                   vlib_set_next_frame_buffer (vm, node, vec_elt (rt->next_by_protocol, i0), bi0);
191                   vlib_set_next_frame_buffer (vm, node, vec_elt (rt->next_by_protocol, i1), bi1);
192                   if (i0 == i1)
193                     {
194                       vlib_put_next_frame (vm, node, next_index,
195                                            n_left_to_next);
196                       i_next = i1;
197                       next_index = vec_elt (rt->next_by_protocol, i_next);
198                       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
199                     }
200                 }
201             }
202         }
203     
204       while (n_left_from > 0 && n_left_to_next > 0)
205         {
206           u32 bi0;
207           vlib_buffer_t * b0;
208           hdlc_header_t * h0;
209           u32 i0, len0, protocol0;
210
211           bi0 = from[0];
212           to_next[0] = bi0;
213           from += 1;
214           to_next += 1;
215           n_left_from -= 1;
216           n_left_to_next -= 1;
217
218           b0 = vlib_get_buffer (vm, bi0);
219
220           h0 = (void *) (b0->data + b0->current_data);
221
222           protocol0 = h0->protocol;
223
224           /* Add padding bytes for OSI protocols. */
225           len0 = sizeof (h0[0]);
226           len0 += protocol0 == clib_host_to_net_u16 (HDLC_PROTOCOL_osi);
227
228           b0->current_data += len0;
229           b0->current_length -= len0;
230
231           i0 = sparse_vec_index (rt->next_by_protocol, protocol0);
232
233           b0->error = node->errors[i0 == SPARSE_VEC_INVALID_INDEX ? HDLC_ERROR_UNKNOWN_PROTOCOL : HDLC_ERROR_NONE];
234           
235           /* Sent packet to wrong next? */
236           if (PREDICT_FALSE (i0 != i_next))
237             {
238               /* Return old frame; remove incorrectly enqueued packet. */
239               vlib_put_next_frame (vm, node, next_index, n_left_to_next + 1);
240
241               /* Send to correct next. */
242               i_next = i0;
243               next_index = vec_elt (rt->next_by_protocol, i_next);
244               vlib_get_next_frame (vm, node, next_index,
245                                    to_next, n_left_to_next);
246
247               to_next[0] = bi0;
248               to_next += 1;
249               n_left_to_next -= 1;
250             }
251         }
252
253       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
254     }
255
256   return from_frame->n_vectors;
257 }
258
259 static char * hdlc_error_strings[] = {
260 #define hdlc_error(n,s) s,
261 #include "error.def"
262 #undef hdlc_error
263 };
264
265 VLIB_REGISTER_NODE (hdlc_input_node) = {
266   .function = hdlc_input,
267   .name = "hdlc-input",
268   /* Takes a vector of packets. */
269   .vector_size = sizeof (u32),
270
271   .runtime_data_bytes = sizeof (hdlc_input_runtime_t),
272
273   .n_errors = HDLC_N_ERROR,
274   .error_strings = hdlc_error_strings,
275
276   .n_next_nodes = HDLC_INPUT_N_NEXT,
277   .next_nodes = {
278 #define _(s,n) [HDLC_INPUT_NEXT_##s] = n,
279     foreach_hdlc_input_next
280 #undef _
281   },
282
283   .format_buffer = format_hdlc_header_with_length,
284   .format_trace = format_hdlc_input_trace,
285   .unformat_buffer = unformat_hdlc_header,
286 };
287
288 static clib_error_t * hdlc_input_init (vlib_main_t * vm)
289 {
290   hdlc_input_runtime_t * rt;
291
292   {
293     clib_error_t * error = vlib_call_init_function (vm, hdlc_init);
294     if (error)
295       clib_error_report (error);
296   }
297
298   hdlc_setup_node (vm, hdlc_input_node.index);
299
300   rt = vlib_node_get_runtime_data (vm, hdlc_input_node.index);
301
302   rt->next_by_protocol = sparse_vec_new
303     (/* elt bytes */ sizeof (rt->next_by_protocol[0]),
304      /* bits in index */ BITS (((hdlc_header_t *) 0)->protocol));
305
306   vec_validate (rt->sparse_index_by_next_index, HDLC_INPUT_NEXT_DROP);
307   vec_validate (rt->sparse_index_by_next_index, HDLC_INPUT_NEXT_PUNT);
308   rt->sparse_index_by_next_index[HDLC_INPUT_NEXT_DROP]
309     = SPARSE_VEC_INVALID_INDEX;
310   rt->sparse_index_by_next_index[HDLC_INPUT_NEXT_PUNT]
311     = SPARSE_VEC_INVALID_INDEX;
312
313   return 0;
314 }
315
316 VLIB_INIT_FUNCTION (hdlc_input_init);
317
318 void
319 hdlc_register_input_protocol (vlib_main_t * vm,
320                              hdlc_protocol_t protocol,
321                              u32 node_index)
322 {
323   hdlc_main_t * em = &hdlc_main;
324   hdlc_protocol_info_t * pi;
325   hdlc_input_runtime_t * rt;
326   u16 * n;
327   u32 i;
328
329   {
330     clib_error_t * error = vlib_call_init_function (vm, hdlc_input_init);
331     if (error)
332       clib_error_report (error);
333   }
334
335   pi = hdlc_get_protocol_info (em, protocol);
336   pi->node_index = node_index;
337   pi->next_index = vlib_node_add_next (vm, 
338                                        hdlc_input_node.index,
339                                        node_index);
340
341   /* Setup hdlc protocol -> next index sparse vector mapping. */
342   rt = vlib_node_get_runtime_data (vm, hdlc_input_node.index);
343   n = sparse_vec_validate (rt->next_by_protocol, clib_host_to_net_u16 (protocol));
344   n[0] = pi->next_index;
345
346   /* Rebuild next index -> sparse index inverse mapping when sparse vector
347      is updated. */
348   vec_validate (rt->sparse_index_by_next_index, pi->next_index);
349   for (i = 1; i < vec_len (rt->next_by_protocol); i++)
350     rt->sparse_index_by_next_index[rt->next_by_protocol[i]] = i;
351 }