FD.io VPP  v16.06
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  * osi_node.c: osi 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/osi/osi.h>
43 #include <vnet/ppp/ppp.h>
44 #include <vnet/hdlc/hdlc.h>
45 #include <vnet/llc/llc.h>
46 
47 #define foreach_osi_input_next \
48  _ (PUNT, "error-punt") \
49  _ (DROP, "error-drop")
50 
51 typedef enum {
52 #define _(s,n) OSI_INPUT_NEXT_##s,
54 #undef _
57 
58 typedef struct {
59  u8 packet_data[32];
61 
62 static u8 * format_osi_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  osi_input_trace_t * t = va_arg (*va, osi_input_trace_t *);
67 
68  s = format (s, "%U", format_osi_header, t->packet_data);
69 
70  return s;
71 }
72 
73 static uword
75  vlib_node_runtime_t * node,
76  vlib_frame_t * from_frame)
77 {
78  osi_main_t * lm = &osi_main;
79  u32 n_left_from, next_index, * from, * to_next;
80 
81  from = vlib_frame_vector_args (from_frame);
82  n_left_from = from_frame->n_vectors;
83 
84  if (node->flags & VLIB_NODE_FLAG_TRACE)
86  from,
87  n_left_from,
88  sizeof (from[0]),
89  sizeof (osi_input_trace_t));
90 
91  next_index = node->cached_next_index;
92 
93  while (n_left_from > 0)
94  {
95  u32 n_left_to_next;
96 
97  vlib_get_next_frame (vm, node, next_index,
98  to_next, n_left_to_next);
99 
100  while (n_left_from >= 4 && n_left_to_next >= 2)
101  {
102  u32 bi0, bi1;
103  vlib_buffer_t * b0, * b1;
104  osi_header_t * h0, * h1;
105  u8 next0, next1, enqueue_code;
106 
107  /* Prefetch next iteration. */
108  {
109  vlib_buffer_t * b2, * b3;
110 
111  b2 = vlib_get_buffer (vm, from[2]);
112  b3 = vlib_get_buffer (vm, from[3]);
113 
114  vlib_prefetch_buffer_header (b2, LOAD);
115  vlib_prefetch_buffer_header (b3, LOAD);
116 
117  CLIB_PREFETCH (b2->data, sizeof (h0[0]), LOAD);
118  CLIB_PREFETCH (b3->data, sizeof (h1[0]), LOAD);
119  }
120 
121  bi0 = from[0];
122  bi1 = from[1];
123  to_next[0] = bi0;
124  to_next[1] = bi1;
125  from += 2;
126  to_next += 2;
127  n_left_to_next -= 2;
128  n_left_from -= 2;
129 
130  b0 = vlib_get_buffer (vm, bi0);
131  b1 = vlib_get_buffer (vm, bi1);
132 
133  h0 = (void *) (b0->data + b0->current_data);
134  h1 = (void *) (b1->data + b1->current_data);
135 
136  next0 = lm->input_next_by_protocol[h0->protocol];
137  next1 = lm->input_next_by_protocol[h1->protocol];
138 
139  b0->error = node->errors[next0 == OSI_INPUT_NEXT_DROP ? OSI_ERROR_UNKNOWN_PROTOCOL : OSI_ERROR_NONE];
140  b1->error = node->errors[next1 == OSI_INPUT_NEXT_DROP ? OSI_ERROR_UNKNOWN_PROTOCOL : OSI_ERROR_NONE];
141 
142  enqueue_code = (next0 != next_index) + 2*(next1 != next_index);
143 
144  if (PREDICT_FALSE (enqueue_code != 0))
145  {
146  switch (enqueue_code)
147  {
148  case 1:
149  /* A B A */
150  to_next[-2] = bi1;
151  to_next -= 1;
152  n_left_to_next += 1;
153  vlib_set_next_frame_buffer (vm, node, next0, bi0);
154  break;
155 
156  case 2:
157  /* A A B */
158  to_next -= 1;
159  n_left_to_next += 1;
160  vlib_set_next_frame_buffer (vm, node, next1, bi1);
161  break;
162 
163  case 3:
164  /* A B B or A B C */
165  to_next -= 2;
166  n_left_to_next += 2;
167  vlib_set_next_frame_buffer (vm, node, next0, bi0);
168  vlib_set_next_frame_buffer (vm, node, next1, bi1);
169  if (next0 == next1)
170  {
171  vlib_put_next_frame (vm, node, next_index,
172  n_left_to_next);
173  next_index = next1;
174  vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
175  }
176  }
177  }
178  }
179 
180  while (n_left_from > 0 && n_left_to_next > 0)
181  {
182  u32 bi0;
183  vlib_buffer_t * b0;
184  osi_header_t * h0;
185  u8 next0;
186 
187  bi0 = from[0];
188  to_next[0] = bi0;
189  from += 1;
190  to_next += 1;
191  n_left_from -= 1;
192  n_left_to_next -= 1;
193 
194  b0 = vlib_get_buffer (vm, bi0);
195 
196  h0 = (void *) (b0->data + b0->current_data);
197 
198  next0 = lm->input_next_by_protocol[h0->protocol];
199 
200  b0->error = node->errors[next0 == OSI_INPUT_NEXT_DROP ? OSI_ERROR_UNKNOWN_PROTOCOL : OSI_ERROR_NONE];
201 
202  /* Sent packet to wrong next? */
203  if (PREDICT_FALSE (next0 != next_index))
204  {
205  /* Return old frame; remove incorrectly enqueued packet. */
206  vlib_put_next_frame (vm, node, next_index, n_left_to_next + 1);
207 
208  /* Send to correct next. */
209  next_index = next0;
210  vlib_get_next_frame (vm, node, next_index,
211  to_next, n_left_to_next);
212 
213  to_next[0] = bi0;
214  to_next += 1;
215  n_left_to_next -= 1;
216  }
217  }
218 
219  vlib_put_next_frame (vm, node, next_index, n_left_to_next);
220  }
221 
222  return from_frame->n_vectors;
223 }
224 
225 static char * osi_error_strings[] = {
226 #define _(f,s) s,
228 #undef _
229 };
230 
232  .function = osi_input,
233  .name = "osi-input",
234  /* Takes a vector of packets. */
235  .vector_size = sizeof (u32),
236 
237  .n_errors = OSI_N_ERROR,
238  .error_strings = osi_error_strings,
239 
240  .n_next_nodes = OSI_INPUT_N_NEXT,
241  .next_nodes = {
242 #define _(s,n) [OSI_INPUT_NEXT_##s] = n,
244 #undef _
245  },
246 
247  .format_buffer = format_osi_header_with_length,
248  .format_trace = format_osi_input_trace,
249  .unformat_buffer = unformat_osi_header,
250 };
251 
253 {
254  clib_error_t * error = 0;
255  osi_main_t * lm = &osi_main;
256 
257  if ((error = vlib_call_init_function (vm, osi_init)))
258  return error;
259 
260  osi_setup_node (vm, osi_input_node.index);
261 
262  {
263  int i;
264  for (i = 0; i < ARRAY_LEN (lm->input_next_by_protocol); i++)
265  lm->input_next_by_protocol[i] = OSI_INPUT_NEXT_DROP;
266  }
267 
268  ppp_register_input_protocol (vm, PPP_PROTOCOL_osi, osi_input_node.index);
269  hdlc_register_input_protocol (vm, HDLC_PROTOCOL_osi, osi_input_node.index);
270  llc_register_input_protocol (vm, LLC_PROTOCOL_osi_layer1, osi_input_node.index);
271  llc_register_input_protocol (vm, LLC_PROTOCOL_osi_layer2, osi_input_node.index);
272  llc_register_input_protocol (vm, LLC_PROTOCOL_osi_layer3, osi_input_node.index);
273  llc_register_input_protocol (vm, LLC_PROTOCOL_osi_layer4, osi_input_node.index);
274  llc_register_input_protocol (vm, LLC_PROTOCOL_osi_layer5, osi_input_node.index);
275 
276  return 0;
277 }
278 
280 
281 void
283  u32 node_index)
284 {
285  osi_main_t * lm = &osi_main;
286  vlib_main_t * vm = lm->vlib_main;
287  osi_protocol_info_t * pi;
288 
289  {
291  if (error)
292  clib_error_report (error);
293  }
294 
295  pi = osi_get_protocol_info (lm, protocol);
296  pi->node_index = node_index;
297  pi->next_index = vlib_node_add_next (vm,
298  osi_input_node.index,
299  node_index);
300 
301  lm->input_next_by_protocol[protocol] = pi->next_index;
302 }
void vlib_put_next_frame(vlib_main_t *vm, vlib_node_runtime_t *r, u32 next_index, u32 n_vectors_left)
Definition: main.c:459
u32 next_index
Definition: osi.h:91
u32 node_index
Definition: osi.h:88
static uword osi_input(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *from_frame)
Definition: node.c:74
sll srl srl sll sra u16x4 i
Definition: vector_sse2.h:267
#define CLIB_UNUSED(x)
Definition: clib.h:79
static u8 * format_osi_input_trace(u8 *s, va_list *va)
Definition: node.c:62
u8 packet_data[32]
Definition: node.c:59
unformat_function_t unformat_osi_header
Definition: osi.h:143
format_function_t format_osi_header_with_length
Definition: osi.h:137
u8 protocol
Definition: osi.h:75
#define clib_error_report(e)
Definition: error.h:126
vlib_error_t * errors
Definition: node.h:378
always_inline osi_protocol_info_t * osi_get_protocol_info(osi_main_t *m, osi_protocol_t protocol)
Definition: osi.h:118
#define foreach_osi_error
Definition: osi.h:94
i16 current_data
signed offset in data[], pre_data[] that we are currently processing.
Definition: buffer.h:77
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:109
void llc_register_input_protocol(vlib_main_t *vm, llc_protocol_t protocol, u32 node_index)
Definition: node.c:288
static char * osi_error_strings[]
Definition: node.c:225
osi_protocol_t
Definition: osi.h:68
#define vlib_call_init_function(vm, x)
Definition: init.h:159
always_inline void * vlib_frame_vector_args(vlib_frame_t *f)
Definition: node_funcs.h:202
void ppp_register_input_protocol(vlib_main_t *vm, ppp_protocol_t protocol, u32 node_index)
Definition: node.c:305
vlib_node_registration_t osi_input_node
(constructor) VLIB_REGISTER_NODE (osi_input_node)
Definition: node.c:231
vlib_main_t * vlib_main
Definition: osi.h:106
osi_main_t osi_main
Definition: osi.c:44
#define PREDICT_FALSE(x)
Definition: clib.h:97
void osi_register_input_protocol(osi_protocol_t protocol, u32 node_index)
Definition: node.c:282
#define vlib_get_next_frame(vm, node, next_index, vectors, n_vectors_left)
Definition: node_funcs.h:265
vlib_error_t error
Error code for buffers to be enqueued to error handler.
Definition: buffer.h:129
static clib_error_t * osi_init(vlib_main_t *vm)
Definition: osi.c:168
u16 n_vectors
Definition: node.h:307
#define CLIB_PREFETCH(addr, size, type)
Definition: cache.h:82
#define ARRAY_LEN(x)
Definition: clib.h:59
#define foreach_osi_input_next
Definition: node.c:47
u16 cached_next_index
Definition: node.h:422
unsigned int u32
Definition: types.h:88
static clib_error_t * osi_input_init(vlib_main_t *vm)
Definition: node.c:252
u8 * format(u8 *s, char *fmt,...)
Definition: format.c:405
osi_input_next_t
Definition: node.c:51
#define VLIB_NODE_FLAG_TRACE
Definition: node.h:225
always_inline uword vlib_node_add_next(vlib_main_t *vm, uword node, uword next_node)
Definition: node_funcs.h:919
u64 uword
Definition: types.h:112
always_inline void osi_setup_node(vlib_main_t *vm, u32 node_index)
Definition: osi.h:147
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:45
void hdlc_register_input_protocol(vlib_main_t *vm, hdlc_protocol_t protocol, u32 node_index)
Definition: node.c:319
unsigned char u8
Definition: types.h:56
always_inline 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:292
#define vlib_prefetch_buffer_header(b, type)
Prefetch buffer metadata.
Definition: buffer.h:162
#define VLIB_REGISTER_NODE(x,...)
Definition: node.h:140
u8 data[0]
Packet data.
Definition: buffer.h:150
u8 input_next_by_protocol[256]
Definition: osi.h:114
format_function_t format_osi_header
Definition: osi.h:136
always_inline vlib_buffer_t * vlib_get_buffer(vlib_main_t *vm, u32 buffer_index)
Translate buffer index into buffer pointer.
Definition: buffer_funcs.h:69