blob: 48269a3b8d3ff505f0c277ac43b890151a861d71 [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
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
Swarup Nayakcc42db82017-11-22 16:41:57 +053049typedef enum
50{
Ed Warnickecb9cada2015-12-08 15:45:58 -070051#define _(s,n) HDLC_INPUT_NEXT_##s,
52 foreach_hdlc_input_next
53#undef _
Swarup Nayakcc42db82017-11-22 16:41:57 +053054 HDLC_INPUT_N_NEXT,
Ed Warnickecb9cada2015-12-08 15:45:58 -070055} hdlc_input_next_t;
56
Swarup Nayakcc42db82017-11-22 16:41:57 +053057typedef struct
58{
Ed Warnickecb9cada2015-12-08 15:45:58 -070059 u8 packet_data[32];
60} hdlc_input_trace_t;
61
Swarup Nayakcc42db82017-11-22 16:41:57 +053062static u8 *
63format_hdlc_input_trace (u8 * s, va_list * va)
Ed Warnickecb9cada2015-12-08 15:45:58 -070064{
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 *);
Swarup Nayakcc42db82017-11-22 16:41:57 +053067 hdlc_input_trace_t *t = va_arg (*va, hdlc_input_trace_t *);
Ed Warnickecb9cada2015-12-08 15:45:58 -070068
69 s = format (s, "%U", format_hdlc_header, t->packet_data);
70
71 return s;
72}
73
Swarup Nayakcc42db82017-11-22 16:41:57 +053074typedef struct
75{
Ed Warnickecb9cada2015-12-08 15:45:58 -070076 /* Sparse vector mapping hdlc protocol in network byte order
77 to next index. */
Swarup Nayakcc42db82017-11-22 16:41:57 +053078 u16 *next_by_protocol;
Ed Warnickecb9cada2015-12-08 15:45:58 -070079
Swarup Nayakcc42db82017-11-22 16:41:57 +053080 u32 *sparse_index_by_next_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -070081} hdlc_input_runtime_t;
82
83static uword
84hdlc_input (vlib_main_t * vm,
Swarup Nayakcc42db82017-11-22 16:41:57 +053085 vlib_node_runtime_t * node, vlib_frame_t * from_frame)
Ed Warnickecb9cada2015-12-08 15:45:58 -070086{
Swarup Nayakcc42db82017-11-22 16:41:57 +053087 hdlc_input_runtime_t *rt = (void *) node->runtime_data;
88 u32 n_left_from, next_index, i_next, *from, *to_next;
Ed Warnickecb9cada2015-12-08 15:45:58 -070089
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
Swarup Nayakcc42db82017-11-22 16:41:57 +0530107 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700108
109 while (n_left_from >= 4 && n_left_to_next >= 2)
110 {
111 u32 bi0, bi1;
Swarup Nayakcc42db82017-11-22 16:41:57 +0530112 vlib_buffer_t *b0, *b1;
113 hdlc_header_t *h0, *h1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700114 u32 i0, i1, len0, len1, protocol0, protocol1, enqueue_code;
115
116 /* Prefetch next iteration. */
117 {
Swarup Nayakcc42db82017-11-22 16:41:57 +0530118 vlib_buffer_t *b2, *b3;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700119
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
Zhiyong Yang21dc4ec2019-05-28 01:26:05 -0400142 h0 = vlib_buffer_get_current (b0);
143 h1 = vlib_buffer_get_current (b1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700144
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]);
Swarup Nayakcc42db82017-11-22 16:41:57 +0530151
Ed Warnickecb9cada2015-12-08 15:45:58 -0700152 len0 += protocol0 == clib_host_to_net_u16 (HDLC_PROTOCOL_osi);
153 len1 += protocol1 == clib_host_to_net_u16 (HDLC_PROTOCOL_osi);
154
Zhiyong Yang21dc4ec2019-05-28 01:26:05 -0400155 vlib_buffer_advance (b0, len0);
156 vlib_buffer_advance (b1, len1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700157
158 /* Index sparse array with network byte order. */
Swarup Nayakcc42db82017-11-22 16:41:57 +0530159 sparse_vec_index2 (rt->next_by_protocol, protocol0, protocol1, &i0,
160 &i1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700161
Swarup Nayakcc42db82017-11-22 16:41:57 +0530162 b0->error =
163 node->errors[i0 ==
164 SPARSE_VEC_INVALID_INDEX ?
165 HDLC_ERROR_UNKNOWN_PROTOCOL : HDLC_ERROR_NONE];
166 b1->error =
167 node->errors[i1 ==
168 SPARSE_VEC_INVALID_INDEX ?
169 HDLC_ERROR_UNKNOWN_PROTOCOL : HDLC_ERROR_NONE];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700170
Swarup Nayakcc42db82017-11-22 16:41:57 +0530171 enqueue_code = (i0 != i_next) + 2 * (i1 != i_next);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700172
173 if (PREDICT_FALSE (enqueue_code != 0))
174 {
175 switch (enqueue_code)
176 {
177 case 1:
178 /* A B A */
179 to_next[-2] = bi1;
180 to_next -= 1;
181 n_left_to_next += 1;
Swarup Nayakcc42db82017-11-22 16:41:57 +0530182 vlib_set_next_frame_buffer (vm, node,
183 vec_elt (rt->next_by_protocol,
184 i0), bi0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700185 break;
186
187 case 2:
188 /* A A B */
189 to_next -= 1;
190 n_left_to_next += 1;
Swarup Nayakcc42db82017-11-22 16:41:57 +0530191 vlib_set_next_frame_buffer (vm, node,
192 vec_elt (rt->next_by_protocol,
193 i1), bi1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700194 break;
195
196 case 3:
197 /* A B B or A B C */
198 to_next -= 2;
199 n_left_to_next += 2;
Swarup Nayakcc42db82017-11-22 16:41:57 +0530200 vlib_set_next_frame_buffer (vm, node,
201 vec_elt (rt->next_by_protocol,
202 i0), bi0);
203 vlib_set_next_frame_buffer (vm, node,
204 vec_elt (rt->next_by_protocol,
205 i1), bi1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700206 if (i0 == i1)
207 {
208 vlib_put_next_frame (vm, node, next_index,
209 n_left_to_next);
210 i_next = i1;
211 next_index = vec_elt (rt->next_by_protocol, i_next);
Swarup Nayakcc42db82017-11-22 16:41:57 +0530212 vlib_get_next_frame (vm, node, next_index, to_next,
213 n_left_to_next);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700214 }
215 }
216 }
217 }
Swarup Nayakcc42db82017-11-22 16:41:57 +0530218
Ed Warnickecb9cada2015-12-08 15:45:58 -0700219 while (n_left_from > 0 && n_left_to_next > 0)
220 {
221 u32 bi0;
Swarup Nayakcc42db82017-11-22 16:41:57 +0530222 vlib_buffer_t *b0;
223 hdlc_header_t *h0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700224 u32 i0, len0, protocol0;
225
226 bi0 = from[0];
227 to_next[0] = bi0;
228 from += 1;
229 to_next += 1;
230 n_left_from -= 1;
231 n_left_to_next -= 1;
232
233 b0 = vlib_get_buffer (vm, bi0);
234
Zhiyong Yang21dc4ec2019-05-28 01:26:05 -0400235 h0 = vlib_buffer_get_current (b0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700236
237 protocol0 = h0->protocol;
238
239 /* Add padding bytes for OSI protocols. */
240 len0 = sizeof (h0[0]);
241 len0 += protocol0 == clib_host_to_net_u16 (HDLC_PROTOCOL_osi);
242
Zhiyong Yang21dc4ec2019-05-28 01:26:05 -0400243 vlib_buffer_advance (b0, len0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700244
245 i0 = sparse_vec_index (rt->next_by_protocol, protocol0);
246
Swarup Nayakcc42db82017-11-22 16:41:57 +0530247 b0->error =
248 node->errors[i0 ==
249 SPARSE_VEC_INVALID_INDEX ?
250 HDLC_ERROR_UNKNOWN_PROTOCOL : HDLC_ERROR_NONE];
251
Ed Warnickecb9cada2015-12-08 15:45:58 -0700252 /* Sent packet to wrong next? */
253 if (PREDICT_FALSE (i0 != i_next))
254 {
255 /* Return old frame; remove incorrectly enqueued packet. */
256 vlib_put_next_frame (vm, node, next_index, n_left_to_next + 1);
257
258 /* Send to correct next. */
259 i_next = i0;
260 next_index = vec_elt (rt->next_by_protocol, i_next);
261 vlib_get_next_frame (vm, node, next_index,
262 to_next, n_left_to_next);
263
264 to_next[0] = bi0;
265 to_next += 1;
266 n_left_to_next -= 1;
267 }
268 }
269
270 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
271 }
272
273 return from_frame->n_vectors;
274}
275
Swarup Nayakcc42db82017-11-22 16:41:57 +0530276static char *hdlc_error_strings[] = {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700277#define hdlc_error(n,s) s,
278#include "error.def"
279#undef hdlc_error
280};
281
282VLIB_REGISTER_NODE (hdlc_input_node) = {
283 .function = hdlc_input,
284 .name = "hdlc-input",
285 /* Takes a vector of packets. */
286 .vector_size = sizeof (u32),
287
288 .runtime_data_bytes = sizeof (hdlc_input_runtime_t),
289
290 .n_errors = HDLC_N_ERROR,
291 .error_strings = hdlc_error_strings,
292
293 .n_next_nodes = HDLC_INPUT_N_NEXT,
294 .next_nodes = {
295#define _(s,n) [HDLC_INPUT_NEXT_##s] = n,
296 foreach_hdlc_input_next
297#undef _
298 },
299
300 .format_buffer = format_hdlc_header_with_length,
301 .format_trace = format_hdlc_input_trace,
302 .unformat_buffer = unformat_hdlc_header,
303};
304
Swarup Nayakcc42db82017-11-22 16:41:57 +0530305static clib_error_t *
306hdlc_input_runtime_init (vlib_main_t * vm)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700307{
Swarup Nayakcc42db82017-11-22 16:41:57 +0530308 hdlc_input_runtime_t *rt;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700309 rt = vlib_node_get_runtime_data (vm, hdlc_input_node.index);
310
311 rt->next_by_protocol = sparse_vec_new
Swarup Nayakcc42db82017-11-22 16:41:57 +0530312 ( /* elt bytes */ sizeof (rt->next_by_protocol[0]),
Ed Warnickecb9cada2015-12-08 15:45:58 -0700313 /* bits in index */ BITS (((hdlc_header_t *) 0)->protocol));
314
315 vec_validate (rt->sparse_index_by_next_index, HDLC_INPUT_NEXT_DROP);
316 vec_validate (rt->sparse_index_by_next_index, HDLC_INPUT_NEXT_PUNT);
317 rt->sparse_index_by_next_index[HDLC_INPUT_NEXT_DROP]
318 = SPARSE_VEC_INVALID_INDEX;
319 rt->sparse_index_by_next_index[HDLC_INPUT_NEXT_PUNT]
320 = SPARSE_VEC_INVALID_INDEX;
321
322 return 0;
323}
324
Neale Ranns68d48d92021-06-03 14:59:47 +0000325static void
326hdlc_setup_node (vlib_main_t *vm, u32 node_index)
327{
328 vlib_node_t *n = vlib_get_node (vm, node_index);
329 pg_node_t *pn = pg_get_node (node_index);
330
331 n->format_buffer = format_hdlc_header_with_length;
332 n->unformat_buffer = unformat_hdlc_header;
333 pn->unformat_edit = unformat_pg_hdlc_header;
334}
335
Swarup Nayakcc42db82017-11-22 16:41:57 +0530336static clib_error_t *
337hdlc_input_init (vlib_main_t * vm)
Damjan Marione9f929b2017-03-16 11:32:09 +0100338{
339
340 {
Swarup Nayakcc42db82017-11-22 16:41:57 +0530341 clib_error_t *error = vlib_call_init_function (vm, hdlc_init);
Damjan Marione9f929b2017-03-16 11:32:09 +0100342 if (error)
343 clib_error_report (error);
344 }
345
346 hdlc_setup_node (vm, hdlc_input_node.index);
347 hdlc_input_runtime_init (vm);
348
349 return 0;
350}
351
Ed Warnickecb9cada2015-12-08 15:45:58 -0700352VLIB_INIT_FUNCTION (hdlc_input_init);
Damjan Marione9f929b2017-03-16 11:32:09 +0100353VLIB_WORKER_INIT_FUNCTION (hdlc_input_runtime_init);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700354
355void
356hdlc_register_input_protocol (vlib_main_t * vm,
Swarup Nayakcc42db82017-11-22 16:41:57 +0530357 hdlc_protocol_t protocol, u32 node_index)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700358{
Swarup Nayakcc42db82017-11-22 16:41:57 +0530359 hdlc_main_t *em = &hdlc_main;
360 hdlc_protocol_info_t *pi;
361 hdlc_input_runtime_t *rt;
362 u16 *n;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700363 u32 i;
364
365 {
Swarup Nayakcc42db82017-11-22 16:41:57 +0530366 clib_error_t *error = vlib_call_init_function (vm, hdlc_input_init);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700367 if (error)
368 clib_error_report (error);
369 }
370
371 pi = hdlc_get_protocol_info (em, protocol);
372 pi->node_index = node_index;
Swarup Nayakcc42db82017-11-22 16:41:57 +0530373 pi->next_index = vlib_node_add_next (vm, hdlc_input_node.index, node_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700374
375 /* Setup hdlc protocol -> next index sparse vector mapping. */
376 rt = vlib_node_get_runtime_data (vm, hdlc_input_node.index);
Swarup Nayakcc42db82017-11-22 16:41:57 +0530377 n =
378 sparse_vec_validate (rt->next_by_protocol,
379 clib_host_to_net_u16 (protocol));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700380 n[0] = pi->next_index;
381
382 /* Rebuild next index -> sparse index inverse mapping when sparse vector
383 is updated. */
384 vec_validate (rt->sparse_index_by_next_index, pi->next_index);
385 for (i = 1; i < vec_len (rt->next_by_protocol); i++)
386 rt->sparse_index_by_next_index[rt->next_by_protocol[i]] = i;
387}
Swarup Nayakcc42db82017-11-22 16:41:57 +0530388
389/*
390 * fd.io coding-style-patch-verification: ON
391 *
392 * Local Variables:
393 * eval: (c-set-style "gnu")
394 * End:
395 */