FD.io VPP  v19.01.3-6-g70449b9b9
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  * llc_node.c: llc 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/llc/llc.h>
43 
44 #define foreach_llc_input_next \
45  _ (PUNT, "error-punt") \
46  _ (DROP, "error-drop")
47 
48 typedef enum
49 {
50 #define _(s,n) LLC_INPUT_NEXT_##s,
52 #undef _
55 
56 typedef struct
57 {
58  u8 packet_data[32];
60 
61 static u8 *
62 format_llc_input_trace (u8 * s, va_list * va)
63 {
64  CLIB_UNUSED (vlib_main_t * vm) = va_arg (*va, vlib_main_t *);
65  CLIB_UNUSED (vlib_node_t * node) = va_arg (*va, vlib_node_t *);
66  llc_input_trace_t *t = va_arg (*va, llc_input_trace_t *);
67 
68  s = format (s, "%U", format_llc_header, t->packet_data);
69 
70  return s;
71 }
72 
73 static uword
75  vlib_node_runtime_t * node, vlib_frame_t * from_frame)
76 {
77  llc_main_t *lm = &llc_main;
78  u32 n_left_from, next_index, *from, *to_next;
79 
80  from = vlib_frame_vector_args (from_frame);
81  n_left_from = from_frame->n_vectors;
82 
83  if (node->flags & VLIB_NODE_FLAG_TRACE)
85  from,
86  n_left_from,
87  sizeof (from[0]),
88  sizeof (llc_input_trace_t));
89 
90  next_index = node->cached_next_index;
91 
92  while (n_left_from > 0)
93  {
94  u32 n_left_to_next;
95 
96  vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
97 
98  while (n_left_from >= 4 && n_left_to_next >= 2)
99  {
100  u32 bi0, bi1;
101  vlib_buffer_t *b0, *b1;
102  llc_header_t *h0, *h1;
103  u8 next0, next1, len0, len1, enqueue_code;
104 
105  /* Prefetch next iteration. */
106  {
107  vlib_buffer_t *b2, *b3;
108 
109  b2 = vlib_get_buffer (vm, from[2]);
110  b3 = vlib_get_buffer (vm, from[3]);
111 
112  vlib_prefetch_buffer_header (b2, LOAD);
113  vlib_prefetch_buffer_header (b3, LOAD);
114 
115  CLIB_PREFETCH (b2->data, sizeof (h0[0]), LOAD);
116  CLIB_PREFETCH (b3->data, sizeof (h1[0]), LOAD);
117  }
118 
119  bi0 = from[0];
120  bi1 = from[1];
121  to_next[0] = bi0;
122  to_next[1] = bi1;
123  from += 2;
124  to_next += 2;
125  n_left_to_next -= 2;
126  n_left_from -= 2;
127 
128  b0 = vlib_get_buffer (vm, bi0);
129  b1 = vlib_get_buffer (vm, bi1);
130 
131  h0 = (void *) (b0->data + b0->current_data);
132  h1 = (void *) (b1->data + b1->current_data);
133 
134  len0 = llc_header_length (h0);
135  len1 = llc_header_length (h1);
136 
137  b0->current_data += len0;
138  b1->current_data += len1;
139 
140  b0->current_length -= len0;
141  b1->current_length -= len1;
142 
143  next0 = lm->input_next_by_protocol[h0->dst_sap];
144  next1 = lm->input_next_by_protocol[h1->dst_sap];
145 
146  b0->error =
147  node->errors[next0 ==
148  LLC_INPUT_NEXT_DROP ? LLC_ERROR_UNKNOWN_PROTOCOL :
149  LLC_ERROR_NONE];
150  b1->error =
151  node->errors[next1 ==
152  LLC_INPUT_NEXT_DROP ? LLC_ERROR_UNKNOWN_PROTOCOL :
153  LLC_ERROR_NONE];
154 
155  enqueue_code = (next0 != next_index) + 2 * (next1 != next_index);
156 
157  if (PREDICT_FALSE (enqueue_code != 0))
158  {
159  switch (enqueue_code)
160  {
161  case 1:
162  /* A B A */
163  to_next[-2] = bi1;
164  to_next -= 1;
165  n_left_to_next += 1;
166  vlib_set_next_frame_buffer (vm, node, next0, bi0);
167  break;
168 
169  case 2:
170  /* A A B */
171  to_next -= 1;
172  n_left_to_next += 1;
173  vlib_set_next_frame_buffer (vm, node, next1, bi1);
174  break;
175 
176  case 3:
177  /* A B B or A B C */
178  to_next -= 2;
179  n_left_to_next += 2;
180  vlib_set_next_frame_buffer (vm, node, next0, bi0);
181  vlib_set_next_frame_buffer (vm, node, next1, bi1);
182  if (next0 == next1)
183  {
184  vlib_put_next_frame (vm, node, next_index,
185  n_left_to_next);
186  next_index = next1;
187  vlib_get_next_frame (vm, node, next_index, to_next,
188  n_left_to_next);
189  }
190  }
191  }
192  }
193 
194  while (n_left_from > 0 && n_left_to_next > 0)
195  {
196  u32 bi0;
197  vlib_buffer_t *b0;
198  llc_header_t *h0;
199  u8 next0, len0;
200 
201  bi0 = from[0];
202  to_next[0] = bi0;
203  from += 1;
204  to_next += 1;
205  n_left_from -= 1;
206  n_left_to_next -= 1;
207 
208  b0 = vlib_get_buffer (vm, bi0);
209 
210  h0 = (void *) (b0->data + b0->current_data);
211 
212  len0 = llc_header_length (h0);
213 
214  b0->current_data += len0;
215 
216  b0->current_length -= len0;
217 
218  next0 = lm->input_next_by_protocol[h0->dst_sap];
219 
220  b0->error =
221  node->errors[next0 ==
222  LLC_INPUT_NEXT_DROP ? LLC_ERROR_UNKNOWN_PROTOCOL :
223  LLC_ERROR_NONE];
224 
225  /* Sent packet to wrong next? */
226  if (PREDICT_FALSE (next0 != next_index))
227  {
228  /* Return old frame; remove incorrectly enqueued packet. */
229  vlib_put_next_frame (vm, node, next_index, n_left_to_next + 1);
230 
231  /* Send to correct next. */
232  next_index = next0;
233  vlib_get_next_frame (vm, node, next_index,
234  to_next, n_left_to_next);
235 
236  to_next[0] = bi0;
237  to_next += 1;
238  n_left_to_next -= 1;
239  }
240  }
241 
242  vlib_put_next_frame (vm, node, next_index, n_left_to_next);
243  }
244 
245  return from_frame->n_vectors;
246 }
247 
248 static char *llc_error_strings[] = {
249 #define _(f,s) s,
251 #undef _
252 };
253 
254 /* *INDENT-OFF* */
256  .function = llc_input,
257  .name = "llc-input",
258  /* Takes a vector of packets. */
259  .vector_size = sizeof (u32),
260 
261  .n_errors = LLC_N_ERROR,
262  .error_strings = llc_error_strings,
263 
264  .n_next_nodes = LLC_INPUT_N_NEXT,
265  .next_nodes = {
266 #define _(s,n) [LLC_INPUT_NEXT_##s] = n,
268 #undef _
269  },
270 
271  .format_buffer = format_llc_header_with_length,
272  .format_trace = format_llc_input_trace,
273  .unformat_buffer = unformat_llc_header,
274 };
275 /* *INDENT-ON* */
276 
277 static clib_error_t *
279 {
280  llc_main_t *lm = &llc_main;
281 
282  {
284  if (error)
285  clib_error_report (error);
286  }
287 
288  llc_setup_node (vm, llc_input_node.index);
289 
290  {
291  int i;
292  for (i = 0; i < ARRAY_LEN (lm->input_next_by_protocol); i++)
293  lm->input_next_by_protocol[i] = LLC_INPUT_NEXT_DROP;
294  }
295 
296  return 0;
297 }
298 
300 
301 void
303  llc_protocol_t protocol, u32 node_index)
304 {
305  llc_main_t *lm = &llc_main;
307 
308  {
310  if (error)
311  clib_error_report (error);
312  /* Otherwise, osi_input_init will wipe out e.g. the snap init */
314  if (error)
315  clib_error_report (error);
316  }
317 
318  pi = llc_get_protocol_info (lm, protocol);
319  pi->node_index = node_index;
320  pi->next_index = vlib_node_add_next (vm, llc_input_node.index, node_index);
321 
322  lm->input_next_by_protocol[protocol] = pi->next_index;
323 }
324 
325 /*
326  * fd.io coding-style-patch-verification: ON
327  *
328  * Local Variables:
329  * eval: (c-set-style "gnu")
330  * End:
331  */
#define CLIB_UNUSED(x)
Definition: clib.h:82
static u8 llc_header_length(llc_header_t *h)
Definition: llc.h:101
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:371
int i
void llc_register_input_protocol(vlib_main_t *vm, llc_protocol_t protocol, u32 node_index)
Definition: node.c:302
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:419
vlib_error_t * errors
Vector of errors for this node.
Definition: node.h:493
static uword vlib_node_add_next(vlib_main_t *vm, uword node, uword next_node)
Definition: node_funcs.h:1092
unsigned char u8
Definition: types.h:56
static u8 * format_llc_input_trace(u8 *s, va_list *va)
Definition: node.c:62
llc_input_next_t
Definition: node.c:48
#define foreach_llc_input_next
Definition: node.c:44
i16 current_data
signed offset in data[], pre_data[] that we are currently processing.
Definition: buffer.h:110
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:163
#define vlib_prefetch_buffer_header(b, type)
Prefetch buffer metadata.
Definition: buffer.h:188
llc_protocol_t
Definition: llc.h:71
unsigned int u32
Definition: types.h:88
#define vlib_call_init_function(vm, x)
Definition: init.h:260
static void llc_setup_node(vlib_main_t *vm, u32 node_index)
Definition: llc.h:173
vlib_node_registration_t llc_input_node
(constructor) VLIB_REGISTER_NODE (llc_input_node)
Definition: node.c:255
u16 current_length
Nbytes between current data and the end of this buffer.
Definition: buffer.h:114
static clib_error_t * osi_input_init(vlib_main_t *vm)
Definition: node.c:266
unformat_function_t unformat_llc_header
Definition: llc.h:169
#define PREDICT_FALSE(x)
Definition: clib.h:111
format_function_t format_llc_header
Definition: llc.h:162
#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:338
static uword llc_input(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *from_frame)
Definition: node.c:74
vlib_error_t error
Error code for buffers to be enqueued to error handler.
Definition: buffer.h:139
static llc_protocol_info_t * llc_get_protocol_info(llc_main_t *m, llc_protocol_t protocol)
Definition: llc.h:148
#define VLIB_REGISTER_NODE(x,...)
Definition: node.h:169
u16 n_vectors
Definition: node.h:420
u8 packet_data[32]
Definition: node.c:58
#define CLIB_PREFETCH(addr, size, type)
Definition: cache.h:79
vlib_main_t * vm
Definition: buffer.c:301
#define foreach_llc_error
Definition: llc.h:121
#define ARRAY_LEN(x)
Definition: clib.h:62
void vlib_put_next_frame(vlib_main_t *vm, vlib_node_runtime_t *r, u32 next_index, u32 n_vectors_left)
Release pointer to next frame vector data.
Definition: main.c:452
format_function_t format_llc_header_with_length
Definition: llc.h:163
u16 cached_next_index
Next frame index that vector arguments were last enqueued to last time this node ran.
Definition: node.h:537
static char * llc_error_strings[]
Definition: node.c:248
#define clib_error_report(e)
Definition: error.h:113
static clib_error_t * llc_init(vlib_main_t *vm)
Definition: llc.c:211
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:47
u8 input_next_by_protocol[256]
Definition: llc.h:144
llc_main_t llc_main
Definition: llc.c:44
u64 uword
Definition: types.h:112
static void * vlib_frame_vector_args(vlib_frame_t *f)
Get pointer to frame vector data.
Definition: node_funcs.h:244
u8 data[0]
Packet data.
Definition: buffer.h:176
u16 flags
Copy of main node flags.
Definition: node.h:531
#define VLIB_NODE_FLAG_TRACE
Definition: node.h:326
static vlib_buffer_t * vlib_get_buffer(vlib_main_t *vm, u32 buffer_index)
Translate buffer index into buffer pointer.
Definition: buffer_funcs.h:62
u8 dst_sap
Definition: llc.h:82
static clib_error_t * llc_input_init(vlib_main_t *vm)
Definition: node.c:278