FD.io VPP  v21.10.1-2-g0a485f517
Vector Packet Processing
node.c
Go to the documentation of this file.
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,
53 #undef _
56 
57 typedef struct
58 {
59  u8 packet_data[32];
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. */
79 
82 
83 static uword
86 {
87  hdlc_input_runtime_t *rt = (void *) node->runtime_data;
88  u32 n_left_from, next_index, i_next, *from, *to_next;
89 
91  n_left_from = from_frame->n_vectors;
92 
93  if (node->flags & VLIB_NODE_FLAG_TRACE)
95  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 = vlib_buffer_get_current (b0);
143  h1 = vlib_buffer_get_current (b1);
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  vlib_buffer_advance (b0, len0);
156  vlib_buffer_advance (b1, len1);
157 
158  /* Index sparse array with network byte order. */
159  sparse_vec_index2 (rt->next_by_protocol, protocol0, protocol1, &i0,
160  &i1);
161 
162  b0->error =
163  node->errors[i0 ==
165  HDLC_ERROR_UNKNOWN_PROTOCOL : HDLC_ERROR_NONE];
166  b1->error =
167  node->errors[i1 ==
169  HDLC_ERROR_UNKNOWN_PROTOCOL : HDLC_ERROR_NONE];
170 
171  enqueue_code = (i0 != i_next) + 2 * (i1 != i_next);
172 
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;
183  vec_elt (rt->next_by_protocol,
184  i0), bi0);
185  break;
186 
187  case 2:
188  /* A A B */
189  to_next -= 1;
190  n_left_to_next += 1;
192  vec_elt (rt->next_by_protocol,
193  i1), bi1);
194  break;
195 
196  case 3:
197  /* A B B or A B C */
198  to_next -= 2;
199  n_left_to_next += 2;
201  vec_elt (rt->next_by_protocol,
202  i0), bi0);
204  vec_elt (rt->next_by_protocol,
205  i1), bi1);
206  if (i0 == i1)
207  {
209  n_left_to_next);
210  i_next = i1;
211  next_index = vec_elt (rt->next_by_protocol, i_next);
213  n_left_to_next);
214  }
215  }
216  }
217  }
218 
219  while (n_left_from > 0 && n_left_to_next > 0)
220  {
221  u32 bi0;
222  vlib_buffer_t *b0;
223  hdlc_header_t *h0;
224  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 
235  h0 = vlib_buffer_get_current (b0);
236 
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 
243  vlib_buffer_advance (b0, len0);
244 
245  i0 = sparse_vec_index (rt->next_by_protocol, protocol0);
246 
247  b0->error =
248  node->errors[i0 ==
250  HDLC_ERROR_UNKNOWN_PROTOCOL : HDLC_ERROR_NONE];
251 
252  /* 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);
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 
276 static char *hdlc_error_strings[] = {
277 #define hdlc_error(n,s) s,
278 #include "error.def"
279 #undef hdlc_error
280 };
281 
282 /* *INDENT-OFF* */
284  .function = hdlc_input,
285  .name = "hdlc-input",
286  /* Takes a vector of packets. */
287  .vector_size = sizeof (u32),
288 
289  .runtime_data_bytes = sizeof (hdlc_input_runtime_t),
290 
291  .n_errors = HDLC_N_ERROR,
292  .error_strings = hdlc_error_strings,
293 
294  .n_next_nodes = HDLC_INPUT_N_NEXT,
295  .next_nodes = {
296 #define _(s,n) [HDLC_INPUT_NEXT_##s] = n,
298 #undef _
299  },
300 
301  .format_buffer = format_hdlc_header_with_length,
302  .format_trace = format_hdlc_input_trace,
303  .unformat_buffer = unformat_hdlc_header,
304 };
305 /* *INDENT-ON* */
306 
307 static clib_error_t *
309 {
312 
313  rt->next_by_protocol = sparse_vec_new
314  ( /* elt bytes */ sizeof (rt->next_by_protocol[0]),
315  /* bits in index */ BITS (((hdlc_header_t *) 0)->protocol));
316 
317  vec_validate (rt->sparse_index_by_next_index, HDLC_INPUT_NEXT_DROP);
318  vec_validate (rt->sparse_index_by_next_index, HDLC_INPUT_NEXT_PUNT);
319  rt->sparse_index_by_next_index[HDLC_INPUT_NEXT_DROP]
321  rt->sparse_index_by_next_index[HDLC_INPUT_NEXT_PUNT]
323 
324  return 0;
325 }
326 
327 static void
329 {
332 
336 }
337 
338 static clib_error_t *
340 {
341 
342  {
344  if (error)
346  }
347 
350 
351  return 0;
352 }
353 
356 
357 void
360 {
361  hdlc_main_t *em = &hdlc_main;
364  u16 *n;
365  u32 i;
366 
367  {
369  if (error)
371  }
372 
373  pi = hdlc_get_protocol_info (em, protocol);
374  pi->node_index = node_index;
376 
377  /* Setup hdlc protocol -> next index sparse vector mapping. */
379  n =
380  sparse_vec_validate (rt->next_by_protocol,
381  clib_host_to_net_u16 (protocol));
382  n[0] = pi->next_index;
383 
384  /* Rebuild next index -> sparse index inverse mapping when sparse vector
385  is updated. */
386  vec_validate (rt->sparse_index_by_next_index, pi->next_index);
387  for (i = 1; i < vec_len (rt->next_by_protocol); i++)
388  rt->sparse_index_by_next_index[rt->next_by_protocol[i]] = i;
389 }
390 
391 /*
392  * fd.io coding-style-patch-verification: ON
393  *
394  * Local Variables:
395  * eval: (c-set-style "gnu")
396  * End:
397  */
vlib.h
vlib_node_get_runtime_data
static void * vlib_node_get_runtime_data(vlib_main_t *vm, u32 node_index)
Get node runtime private data by node index.
Definition: node_funcs.h:137
vlib_prefetch_buffer_header
#define vlib_prefetch_buffer_header(b, type)
Prefetch buffer metadata.
Definition: buffer.h:231
vlib_node_add_next
static uword vlib_node_add_next(vlib_main_t *vm, uword node, uword next_node)
Definition: node_funcs.h:1177
next_index
nat44_ei_hairpin_src_next_t next_index
Definition: nat44_ei_hairpinning.c:412
pg.h
vlib_get_buffer
static vlib_buffer_t * vlib_get_buffer(vlib_main_t *vm, u32 buffer_index)
Translate buffer index into buffer pointer.
Definition: buffer_funcs.h:111
hdlc_input_trace_t::packet_data
u8 packet_data[32]
Definition: node.c:59
vlib_trace_frame_buffers_only
void vlib_trace_frame_buffers_only(vlib_main_t *vm, vlib_node_runtime_t *node, u32 *buffers, uword n_buffers, uword next_buffer_stride, uword n_buffer_data_bytes_in_trace)
Definition: trace.c:48
node
vlib_main_t vlib_node_runtime_t * node
Definition: nat44_ei.c:3047
format_hdlc_header
format_function_t format_hdlc_header
Definition: hdlc.h:96
hdlc_init
static clib_error_t * hdlc_init(vlib_main_t *vm)
Definition: hdlc.c:228
hdlc.h
u16
unsigned short u16
Definition: types.h:57
hdlc_input_runtime_t
Definition: node.c:74
vlib_call_init_function
#define vlib_call_init_function(vm, x)
Definition: init.h:259
vm
vlib_main_t * vm
X-connect all packets from the HOST to the PHY.
Definition: nat44_ei.c:3047
node_index
node node_index
Definition: interface_output.c:440
hdlc_input_runtime_t::sparse_index_by_next_index
u32 * sparse_index_by_next_index
Definition: node.c:80
from_frame
vlib_main_t vlib_node_runtime_t vlib_frame_t * from_frame
Definition: esp_encrypt.c:1328
clib_error_report
#define clib_error_report(e)
Definition: error.h:113
vlib_node_t::unformat_buffer
unformat_function_t * unformat_buffer
Definition: node.h:349
vlib_frame_t
Definition: node.h:372
SPARSE_VEC_INVALID_INDEX
#define SPARSE_VEC_INVALID_INDEX
Definition: sparse_vec.h:68
hdlc_input_runtime_init
static clib_error_t * hdlc_input_runtime_init(vlib_main_t *vm)
Definition: node.c:308
hdlc_main
hdlc_main_t hdlc_main
Definition: hdlc.c:44
error
Definition: cJSON.c:88
pg_node_t
Definition: pg.h:332
hdlc_register_input_protocol
void hdlc_register_input_protocol(vlib_main_t *vm, hdlc_protocol_t protocol, u32 node_index)
Definition: node.c:358
vec_elt
#define vec_elt(v, i)
Get vector value at index i.
Definition: vec_bootstrap.h:210
CLIB_PREFETCH
#define CLIB_PREFETCH(addr, size, type)
Definition: cache.h:76
vlib_buffer_advance
static void vlib_buffer_advance(vlib_buffer_t *b, word l)
Advance current data pointer by the supplied (signed!) amount.
Definition: buffer.h:276
vlib_node_t::format_buffer
format_function_t * format_buffer
Definition: node.h:348
vec_len
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
Definition: vec_bootstrap.h:142
vlib_buffer_t::error
vlib_error_t error
Error code for buffers to be enqueued to error handler.
Definition: buffer.h:145
format_hdlc_input_trace
static u8 * format_hdlc_input_trace(u8 *s, va_list *va)
Definition: node.c:63
sparse_vec_validate
#define sparse_vec_validate(v, i)
Definition: sparse_vec.h:231
hdlc_header_t
Definition: packet.h:59
pg_node_t::unformat_edit
unformat_function_t * unformat_edit
Definition: pg.h:335
hdlc_input_init
static clib_error_t * hdlc_input_init(vlib_main_t *vm)
Definition: node.c:339
CLIB_UNUSED
#define CLIB_UNUSED(x)
Definition: clib.h:90
VLIB_NODE_FLAG_TRACE
#define VLIB_NODE_FLAG_TRACE
Definition: node.h:291
PREDICT_FALSE
#define PREDICT_FALSE(x)
Definition: clib.h:124
BITS
#define BITS(x)
Definition: clib.h:69
vlib_frame_vector_args
static void * vlib_frame_vector_args(vlib_frame_t *f)
Get pointer to frame vector data.
Definition: node_funcs.h:301
uword
u64 uword
Definition: types.h:112
pg_get_node
static pg_node_t * pg_get_node(uword node_index)
Definition: pg.h:391
VLIB_WORKER_INIT_FUNCTION
VLIB_WORKER_INIT_FUNCTION(hdlc_input_runtime_init)
sparse_vec_index
static uword sparse_vec_index(void *v, uword sparse_index)
Definition: sparse_vec.h:161
sparse_vec.h
error.def
vlib_get_node
static vlib_node_t * vlib_get_node(vlib_main_t *vm, u32 i)
Get vlib node by index.
Definition: node_funcs.h:86
hdlc_input
static uword hdlc_input(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *from_frame)
Definition: node.c:84
hdlc_error_strings
static char * hdlc_error_strings[]
Definition: node.c:276
vec_validate
#define vec_validate(V, I)
Make sure vector is long enough for given index (no header, unspecified alignment)
Definition: vec.h:523
hdlc_get_protocol_info
static hdlc_protocol_info_t * hdlc_get_protocol_info(hdlc_main_t *em, hdlc_protocol_t protocol)
Definition: hdlc.h:82
vlib_node_registration_t
struct _vlib_node_registration vlib_node_registration_t
HDLC_N_ERROR
@ HDLC_N_ERROR
Definition: hdlc.h:53
hdlc_main_t
Definition: hdlc.h:71
hdlc_header_t::protocol
u16 protocol
Definition: packet.h:68
sparse_vec_index2
static void sparse_vec_index2(void *v, u32 si0, u32 si1, u32 *i0_return, u32 *i1_return)
Definition: sparse_vec.h:169
hdlc_protocol_t
hdlc_protocol_t
Definition: packet.h:52
format
description fragment has unexpected format
Definition: map.api:433
vlib_put_next_frame
vlib_put_next_frame(vm, node, next_index, 0)
vlib_set_next_frame_buffer
static void vlib_set_next_frame_buffer(vlib_main_t *vm, vlib_node_runtime_t *node, u32 next_index, u32 buffer_index)
Definition: node_funcs.h:428
u32
unsigned int u32
Definition: types.h:88
VLIB_INIT_FUNCTION
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:172
protocol
vl_api_ip_proto_t protocol
Definition: lb_types.api:72
vlib_main_t
Definition: main.h:102
foreach_hdlc_input_next
#define foreach_hdlc_input_next
Definition: node.c:45
vlib_node_t
Definition: node.h:247
hdlc_protocol_info_t::next_index
u32 next_index
Definition: hdlc.h:68
hdlc_setup_node
static void hdlc_setup_node(vlib_main_t *vm, u32 node_index)
Definition: node.c:328
u8
unsigned char u8
Definition: types.h:56
clib_error_t
Definition: clib_error.h:21
format_hdlc_header_with_length
format_function_t format_hdlc_header_with_length
Definition: hdlc.h:97
vlib_buffer_get_current
static void * vlib_buffer_get_current(vlib_buffer_t *b)
Get pointer to current data to process.
Definition: buffer.h:257
rt
vnet_interface_output_runtime_t * rt
Definition: interface_output.c:419
unformat_hdlc_header
unformat_function_t unformat_hdlc_header
Definition: hdlc.h:105
vlib_init_function_t
clib_error_t *() vlib_init_function_t(struct vlib_main_t *vm)
Definition: init.h:51
vlib_buffer_t::data
u8 data[]
Packet data.
Definition: buffer.h:204
HDLC_INPUT_N_NEXT
@ HDLC_INPUT_N_NEXT
Definition: node.c:54
i
int i
Definition: flowhash_template.h:376
hdlc_input_node
vlib_node_registration_t hdlc_input_node
(constructor) VLIB_REGISTER_NODE (hdlc_input_node)
Definition: node.c:283
vlib_node_runtime_t
Definition: node.h:454
hdlc_protocol_info_t
Definition: hdlc.h:56
from
from
Definition: nat44_ei_hairpinning.c:415
hdlc_protocol_info_t::node_index
u32 node_index
Definition: hdlc.h:65
vlib_get_next_frame
#define vlib_get_next_frame(vm, node, next_index, vectors, n_vectors_left)
Get pointer to next frame vector data by (vlib_node_runtime_t, next_index).
Definition: node_funcs.h:395
hdlc_input_next_t
hdlc_input_next_t
Definition: node.c:49
n_left_from
n_left_from
Definition: nat44_ei_hairpinning.c:416
unformat_pg_hdlc_header
unformat_function_t unformat_pg_hdlc_header
Definition: hdlc.h:106
sparse_vec_new
static void * sparse_vec_new(uword elt_bytes, uword sparse_index_bits)
Definition: sparse_vec.h:71
hdlc_input_runtime_t::next_by_protocol
u16 * next_by_protocol
Definition: node.c:78
vlib_buffer_t
VLIB buffer representation.
Definition: buffer.h:111
hdlc_input_trace_t
Definition: node.c:57
VLIB_REGISTER_NODE
#define VLIB_REGISTER_NODE(x,...)
Definition: node.h:169