API: Add support for type aliases
[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 {
51 #define _(s,n) HDLC_INPUT_NEXT_##s,
52   foreach_hdlc_input_next
53 #undef _
54     HDLC_INPUT_N_NEXT,
55 } hdlc_input_next_t;
56
57 typedef struct
58 {
59   u8 packet_data[32];
60 } hdlc_input_trace_t;
61
62 static u8 *
63 format_hdlc_input_trace (u8 * s, va_list * va)
64 {
65   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*va, vlib_main_t *);
66   CLIB_UNUSED (vlib_node_t * node) = va_arg (*va, vlib_node_t *);
67   hdlc_input_trace_t *t = va_arg (*va, hdlc_input_trace_t *);
68
69   s = format (s, "%U", format_hdlc_header, t->packet_data);
70
71   return s;
72 }
73
74 typedef struct
75 {
76   /* Sparse vector mapping hdlc protocol in network byte order
77      to next index. */
78   u16 *next_by_protocol;
79
80   u32 *sparse_index_by_next_index;
81 } hdlc_input_runtime_t;
82
83 static uword
84 hdlc_input (vlib_main_t * vm,
85             vlib_node_runtime_t * node, vlib_frame_t * from_frame)
86 {
87   hdlc_input_runtime_t *rt = (void *) node->runtime_data;
88   u32 n_left_from, next_index, i_next, *from, *to_next;
89
90   from = vlib_frame_vector_args (from_frame);
91   n_left_from = from_frame->n_vectors;
92
93   if (node->flags & VLIB_NODE_FLAG_TRACE)
94     vlib_trace_frame_buffers_only (vm, node,
95                                    from,
96                                    n_left_from,
97                                    sizeof (from[0]),
98                                    sizeof (hdlc_input_trace_t));
99
100   next_index = node->cached_next_index;
101   i_next = vec_elt (rt->sparse_index_by_next_index, next_index);
102
103   while (n_left_from > 0)
104     {
105       u32 n_left_to_next;
106
107       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
108
109       while (n_left_from >= 4 && n_left_to_next >= 2)
110         {
111           u32 bi0, bi1;
112           vlib_buffer_t *b0, *b1;
113           hdlc_header_t *h0, *h1;
114           u32 i0, i1, len0, len1, protocol0, protocol1, enqueue_code;
115
116           /* Prefetch next iteration. */
117           {
118             vlib_buffer_t *b2, *b3;
119
120             b2 = vlib_get_buffer (vm, from[2]);
121             b3 = vlib_get_buffer (vm, from[3]);
122
123             vlib_prefetch_buffer_header (b2, LOAD);
124             vlib_prefetch_buffer_header (b3, LOAD);
125
126             CLIB_PREFETCH (b2->data, sizeof (h0[0]), LOAD);
127             CLIB_PREFETCH (b3->data, sizeof (h1[0]), LOAD);
128           }
129
130           bi0 = from[0];
131           bi1 = from[1];
132           to_next[0] = bi0;
133           to_next[1] = bi1;
134           from += 2;
135           to_next += 2;
136           n_left_to_next -= 2;
137           n_left_from -= 2;
138
139           b0 = vlib_get_buffer (vm, bi0);
140           b1 = vlib_get_buffer (vm, bi1);
141
142           h0 = (void *) (b0->data + b0->current_data);
143           h1 = (void *) (b1->data + b1->current_data);
144
145           protocol0 = h0->protocol;
146           protocol1 = h1->protocol;
147
148           /* Add padding bytes for OSI protocols. */
149           len0 = sizeof (h0[0]);
150           len1 = sizeof (h1[0]);
151
152           len0 += protocol0 == clib_host_to_net_u16 (HDLC_PROTOCOL_osi);
153           len1 += protocol1 == clib_host_to_net_u16 (HDLC_PROTOCOL_osi);
154
155           b0->current_data += len0;
156           b1->current_data += len1;
157
158           b0->current_length -= len0;
159           b1->current_length -= len1;
160
161           /* Index sparse array with network byte order. */
162           sparse_vec_index2 (rt->next_by_protocol, protocol0, protocol1, &i0,
163                              &i1);
164
165           b0->error =
166             node->errors[i0 ==
167                          SPARSE_VEC_INVALID_INDEX ?
168                          HDLC_ERROR_UNKNOWN_PROTOCOL : HDLC_ERROR_NONE];
169           b1->error =
170             node->errors[i1 ==
171                          SPARSE_VEC_INVALID_INDEX ?
172                          HDLC_ERROR_UNKNOWN_PROTOCOL : HDLC_ERROR_NONE];
173
174           enqueue_code = (i0 != i_next) + 2 * (i1 != i_next);
175
176           if (PREDICT_FALSE (enqueue_code != 0))
177             {
178               switch (enqueue_code)
179                 {
180                 case 1:
181                   /* A B A */
182                   to_next[-2] = bi1;
183                   to_next -= 1;
184                   n_left_to_next += 1;
185                   vlib_set_next_frame_buffer (vm, node,
186                                               vec_elt (rt->next_by_protocol,
187                                                        i0), bi0);
188                   break;
189
190                 case 2:
191                   /* A A B */
192                   to_next -= 1;
193                   n_left_to_next += 1;
194                   vlib_set_next_frame_buffer (vm, node,
195                                               vec_elt (rt->next_by_protocol,
196                                                        i1), bi1);
197                   break;
198
199                 case 3:
200                   /* A B B or A B C */
201                   to_next -= 2;
202                   n_left_to_next += 2;
203                   vlib_set_next_frame_buffer (vm, node,
204                                               vec_elt (rt->next_by_protocol,
205                                                        i0), bi0);
206                   vlib_set_next_frame_buffer (vm, node,
207                                               vec_elt (rt->next_by_protocol,
208                                                        i1), bi1);
209                   if (i0 == i1)
210                     {
211                       vlib_put_next_frame (vm, node, next_index,
212                                            n_left_to_next);
213                       i_next = i1;
214                       next_index = vec_elt (rt->next_by_protocol, i_next);
215                       vlib_get_next_frame (vm, node, next_index, to_next,
216                                            n_left_to_next);
217                     }
218                 }
219             }
220         }
221
222       while (n_left_from > 0 && n_left_to_next > 0)
223         {
224           u32 bi0;
225           vlib_buffer_t *b0;
226           hdlc_header_t *h0;
227           u32 i0, len0, protocol0;
228
229           bi0 = from[0];
230           to_next[0] = bi0;
231           from += 1;
232           to_next += 1;
233           n_left_from -= 1;
234           n_left_to_next -= 1;
235
236           b0 = vlib_get_buffer (vm, bi0);
237
238           h0 = (void *) (b0->data + b0->current_data);
239
240           protocol0 = h0->protocol;
241
242           /* Add padding bytes for OSI protocols. */
243           len0 = sizeof (h0[0]);
244           len0 += protocol0 == clib_host_to_net_u16 (HDLC_PROTOCOL_osi);
245
246           b0->current_data += len0;
247           b0->current_length -= len0;
248
249           i0 = sparse_vec_index (rt->next_by_protocol, protocol0);
250
251           b0->error =
252             node->errors[i0 ==
253                          SPARSE_VEC_INVALID_INDEX ?
254                          HDLC_ERROR_UNKNOWN_PROTOCOL : HDLC_ERROR_NONE];
255
256           /* Sent packet to wrong next? */
257           if (PREDICT_FALSE (i0 != i_next))
258             {
259               /* Return old frame; remove incorrectly enqueued packet. */
260               vlib_put_next_frame (vm, node, next_index, n_left_to_next + 1);
261
262               /* Send to correct next. */
263               i_next = i0;
264               next_index = vec_elt (rt->next_by_protocol, i_next);
265               vlib_get_next_frame (vm, node, next_index,
266                                    to_next, n_left_to_next);
267
268               to_next[0] = bi0;
269               to_next += 1;
270               n_left_to_next -= 1;
271             }
272         }
273
274       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
275     }
276
277   return from_frame->n_vectors;
278 }
279
280 static char *hdlc_error_strings[] = {
281 #define hdlc_error(n,s) s,
282 #include "error.def"
283 #undef hdlc_error
284 };
285
286 /* *INDENT-OFF* */
287 VLIB_REGISTER_NODE (hdlc_input_node) = {
288   .function = hdlc_input,
289   .name = "hdlc-input",
290   /* Takes a vector of packets. */
291   .vector_size = sizeof (u32),
292
293   .runtime_data_bytes = sizeof (hdlc_input_runtime_t),
294
295   .n_errors = HDLC_N_ERROR,
296   .error_strings = hdlc_error_strings,
297
298   .n_next_nodes = HDLC_INPUT_N_NEXT,
299   .next_nodes = {
300 #define _(s,n) [HDLC_INPUT_NEXT_##s] = n,
301     foreach_hdlc_input_next
302 #undef _
303   },
304
305   .format_buffer = format_hdlc_header_with_length,
306   .format_trace = format_hdlc_input_trace,
307   .unformat_buffer = unformat_hdlc_header,
308 };
309 /* *INDENT-ON* */
310
311 static clib_error_t *
312 hdlc_input_runtime_init (vlib_main_t * vm)
313 {
314   hdlc_input_runtime_t *rt;
315   rt = vlib_node_get_runtime_data (vm, hdlc_input_node.index);
316
317   rt->next_by_protocol = sparse_vec_new
318     ( /* elt bytes */ sizeof (rt->next_by_protocol[0]),
319      /* bits in index */ BITS (((hdlc_header_t *) 0)->protocol));
320
321   vec_validate (rt->sparse_index_by_next_index, HDLC_INPUT_NEXT_DROP);
322   vec_validate (rt->sparse_index_by_next_index, HDLC_INPUT_NEXT_PUNT);
323   rt->sparse_index_by_next_index[HDLC_INPUT_NEXT_DROP]
324     = SPARSE_VEC_INVALID_INDEX;
325   rt->sparse_index_by_next_index[HDLC_INPUT_NEXT_PUNT]
326     = SPARSE_VEC_INVALID_INDEX;
327
328   return 0;
329 }
330
331 static clib_error_t *
332 hdlc_input_init (vlib_main_t * vm)
333 {
334
335   {
336     clib_error_t *error = vlib_call_init_function (vm, hdlc_init);
337     if (error)
338       clib_error_report (error);
339   }
340
341   hdlc_setup_node (vm, hdlc_input_node.index);
342   hdlc_input_runtime_init (vm);
343
344   return 0;
345 }
346
347 VLIB_INIT_FUNCTION (hdlc_input_init);
348 VLIB_WORKER_INIT_FUNCTION (hdlc_input_runtime_init);
349
350 void
351 hdlc_register_input_protocol (vlib_main_t * vm,
352                               hdlc_protocol_t protocol, u32 node_index)
353 {
354   hdlc_main_t *em = &hdlc_main;
355   hdlc_protocol_info_t *pi;
356   hdlc_input_runtime_t *rt;
357   u16 *n;
358   u32 i;
359
360   {
361     clib_error_t *error = vlib_call_init_function (vm, hdlc_input_init);
362     if (error)
363       clib_error_report (error);
364   }
365
366   pi = hdlc_get_protocol_info (em, protocol);
367   pi->node_index = node_index;
368   pi->next_index = vlib_node_add_next (vm, hdlc_input_node.index, node_index);
369
370   /* Setup hdlc protocol -> next index sparse vector mapping. */
371   rt = vlib_node_get_runtime_data (vm, hdlc_input_node.index);
372   n =
373     sparse_vec_validate (rt->next_by_protocol,
374                          clib_host_to_net_u16 (protocol));
375   n[0] = pi->next_index;
376
377   /* Rebuild next index -> sparse index inverse mapping when sparse vector
378      is updated. */
379   vec_validate (rt->sparse_index_by_next_index, pi->next_index);
380   for (i = 1; i < vec_len (rt->next_by_protocol); i++)
381     rt->sparse_index_by_next_index[rt->next_by_protocol[i]] = i;
382 }
383
384 /*
385  * fd.io coding-style-patch-verification: ON
386  *
387  * Local Variables:
388  * eval: (c-set-style "gnu")
389  * End:
390  */