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  * snap_node.c: snap 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 #include <vnet/snap/snap.h>
44 
45 typedef enum {
51 
52 typedef struct {
53  u8 packet_data[32];
55 
56 static u8 * format_snap_input_trace (u8 * s, va_list * va)
57 {
58  CLIB_UNUSED (vlib_main_t * vm) = va_arg (*va, vlib_main_t *);
59  CLIB_UNUSED (vlib_node_t * node) = va_arg (*va, vlib_node_t *);
60  snap_input_trace_t * t = va_arg (*va, snap_input_trace_t *);
61 
62  s = format (s, "%U", format_snap_header, t->packet_data);
63 
64  return s;
65 }
66 
67 static uword
69  vlib_node_runtime_t * node,
70  vlib_frame_t * from_frame)
71 {
72  snap_main_t * sm = &snap_main;
73  u32 n_left_from, next_index, * from, * to_next;
74 
75  from = vlib_frame_vector_args (from_frame);
76  n_left_from = from_frame->n_vectors;
77 
78  if (node->flags & VLIB_NODE_FLAG_TRACE)
80  from,
81  n_left_from,
82  sizeof (from[0]),
83  sizeof (snap_input_trace_t));
84 
85  next_index = node->cached_next_index;
86 
87  while (n_left_from > 0)
88  {
89  u32 n_left_to_next;
90 
91  vlib_get_next_frame (vm, node, next_index,
92  to_next, n_left_to_next);
93 
94  while (n_left_from >= 4 && n_left_to_next >= 2)
95  {
96  u32 bi0, bi1;
97  vlib_buffer_t * b0, * b1;
98  snap_header_t * h0, * h1;
99  snap_protocol_info_t * pi0, * pi1;
100  u8 next0, next1, is_ethernet0, is_ethernet1, len0, len1, enqueue_code;
101  u32 oui0, oui1;
102 
103  /* Prefetch next iteration. */
104  {
105  vlib_buffer_t * b2, * b3;
106 
107  b2 = vlib_get_buffer (vm, from[2]);
108  b3 = vlib_get_buffer (vm, from[3]);
109 
110  vlib_prefetch_buffer_header (b2, LOAD);
111  vlib_prefetch_buffer_header (b3, LOAD);
112 
113  CLIB_PREFETCH (b2->data, sizeof (h0[0]), LOAD);
114  CLIB_PREFETCH (b3->data, sizeof (h1[0]), LOAD);
115  }
116 
117  bi0 = from[0];
118  bi1 = from[1];
119  to_next[0] = bi0;
120  to_next[1] = bi1;
121  from += 2;
122  to_next += 2;
123  n_left_to_next -= 2;
124  n_left_from -= 2;
125 
126  b0 = vlib_get_buffer (vm, bi0);
127  b1 = vlib_get_buffer (vm, bi1);
128 
129  h0 = (void *) (b0->data + b0->current_data);
130  h1 = (void *) (b1->data + b1->current_data);
131 
132  oui0 = snap_header_get_oui (h0);
133  oui1 = snap_header_get_oui (h1);
134 
135  is_ethernet0 = oui0 == IEEE_OUI_ethernet;
136  is_ethernet1 = oui1 == IEEE_OUI_ethernet;
137 
138  len0 = sizeof (h0[0]) - (is_ethernet0 ? sizeof (h0->protocol) : 0);
139  len1 = sizeof (h1[0]) - (is_ethernet1 ? sizeof (h1->protocol) : 0);
140 
141  b0->current_data += len0;
142  b1->current_data += len1;
143 
144  b0->current_length -= len0;
145  b1->current_length -= len1;
146 
147  pi0 = snap_get_protocol_info (sm, h0);
148  pi1 = snap_get_protocol_info (sm, h1);
149 
150  next0 = pi0 ? pi0->next_index : SNAP_INPUT_NEXT_DROP;
151  next1 = pi1 ? pi1->next_index : SNAP_INPUT_NEXT_DROP;
152 
153  next0 = is_ethernet0 ? SNAP_INPUT_NEXT_ETHERNET_TYPE : next0;
154  next1 = is_ethernet1 ? SNAP_INPUT_NEXT_ETHERNET_TYPE : next1;
155 
156  /* In case of error. */
157  b0->error = node->errors[SNAP_ERROR_UNKNOWN_PROTOCOL];
158  b1->error = node->errors[SNAP_ERROR_UNKNOWN_PROTOCOL];
159 
160  enqueue_code = (next0 != next_index) + 2*(next1 != next_index);
161 
162  if (PREDICT_FALSE (enqueue_code != 0))
163  {
164  switch (enqueue_code)
165  {
166  case 1:
167  /* A B A */
168  to_next[-2] = bi1;
169  to_next -= 1;
170  n_left_to_next += 1;
171  vlib_set_next_frame_buffer (vm, node, next0, bi0);
172  break;
173 
174  case 2:
175  /* A A B */
176  to_next -= 1;
177  n_left_to_next += 1;
178  vlib_set_next_frame_buffer (vm, node, next1, bi1);
179  break;
180 
181  case 3:
182  /* A B B or A B C */
183  to_next -= 2;
184  n_left_to_next += 2;
185  vlib_set_next_frame_buffer (vm, node, next0, bi0);
186  vlib_set_next_frame_buffer (vm, node, next1, bi1);
187  if (next0 == next1)
188  {
189  vlib_put_next_frame (vm, node, next_index,
190  n_left_to_next);
191  next_index = next1;
192  vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
193  }
194  }
195  }
196  }
197 
198  while (n_left_from > 0 && n_left_to_next > 0)
199  {
200  u32 bi0;
201  vlib_buffer_t * b0;
202  snap_header_t * h0;
203  snap_protocol_info_t * pi0;
204  u8 next0, is_ethernet0, len0;
205  u32 oui0;
206 
207  bi0 = from[0];
208  to_next[0] = bi0;
209  from += 1;
210  to_next += 1;
211  n_left_from -= 1;
212  n_left_to_next -= 1;
213 
214  b0 = vlib_get_buffer (vm, bi0);
215 
216  h0 = (void *) (b0->data + b0->current_data);
217 
218  oui0 = snap_header_get_oui (h0);
219 
220  is_ethernet0 = oui0 == IEEE_OUI_ethernet;
221 
222  len0 = sizeof (h0[0]) - (is_ethernet0 ? sizeof (h0->protocol) : 0);
223 
224  b0->current_data += len0;
225 
226  b0->current_length -= len0;
227 
228  pi0 = snap_get_protocol_info (sm, h0);
229 
230  next0 = pi0 ? pi0->next_index : SNAP_INPUT_NEXT_DROP;
231 
232  next0 = is_ethernet0 ? SNAP_INPUT_NEXT_ETHERNET_TYPE : next0;
233 
234  /* In case of error. */
235  b0->error = node->errors[SNAP_ERROR_UNKNOWN_PROTOCOL];
236 
237  /* Sent packet to wrong next? */
238  if (PREDICT_FALSE (next0 != next_index))
239  {
240  /* Return old frame; remove incorrectly enqueued packet. */
241  vlib_put_next_frame (vm, node, next_index, n_left_to_next + 1);
242 
243  /* Send to correct next. */
244  next_index = next0;
245  vlib_get_next_frame (vm, node, next_index,
246  to_next, n_left_to_next);
247 
248  to_next[0] = bi0;
249  to_next += 1;
250  n_left_to_next -= 1;
251  }
252  }
253 
254  vlib_put_next_frame (vm, node, next_index, n_left_to_next);
255  }
256 
257  return from_frame->n_vectors;
258 }
259 
260 static char * snap_error_strings[] = {
261 #define _(f,s) s,
263 #undef _
264 };
265 
267  .function = snap_input,
268  .name = "snap-input",
269  /* Takes a vector of packets. */
270  .vector_size = sizeof (u32),
271 
272  .n_errors = SNAP_N_ERROR,
273  .error_strings = snap_error_strings,
274 
275  .n_next_nodes = SNAP_INPUT_N_NEXT,
276  .next_nodes = {
277  [SNAP_INPUT_NEXT_DROP] = "error-drop",
278  [SNAP_INPUT_NEXT_PUNT] = "error-punt",
279  [SNAP_INPUT_NEXT_ETHERNET_TYPE] = "ethernet-input-type",
280  },
281 
282  .format_buffer = format_snap_header_with_length,
283  .format_trace = format_snap_input_trace,
284  .unformat_buffer = unformat_snap_header,
285 };
286 
288 {
289  {
291  if (error)
292  clib_error_report (error);
293  }
294 
295  snap_setup_node (vm, snap_input_node.index);
296 
297  llc_register_input_protocol (vm, LLC_PROTOCOL_snap, snap_input_node.index);
298 
299  return 0;
300 }
301 
303 
304 void
306  char * name,
307  u32 ieee_oui,
308  u16 protocol,
309  u32 node_index)
310 {
311  snap_main_t * sm = &snap_main;
313  snap_header_t h;
315 
316  {
318  if (error)
319  clib_error_report (error);
320  }
321 
322  h.protocol = clib_host_to_net_u16 (protocol);
323  h.oui[0] = (ieee_oui >> 16) & 0xff;
324  h.oui[1] = (ieee_oui >> 8) & 0xff;
325  h.oui[2] = (ieee_oui >> 0) & 0xff;
326  pi = snap_get_protocol_info (sm, &h);
327  if (pi)
328  return;
329 
330  vec_add2 (sm->protocols, pi, 1);
331 
332  pi->name = format (0, "%s", name);
333  pi->node_index = node_index;
334  pi->next_index = vlib_node_add_next (vm,
335  snap_input_node.index,
336  node_index);
337 
338  key.oui = ieee_oui;
339  key.protocol = clib_host_to_net_u16 (protocol);
340 
341  mhash_set (&sm->protocol_hash, &key, pi - sm->protocols, /* old_value */ 0);
342  hash_set_mem (sm->protocol_info_by_name, name, pi - sm->protocols);
343 }
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
always_inline snap_protocol_info_t * snap_get_protocol_info(snap_main_t *sm, snap_header_t *h)
Definition: snap.h:147
#define CLIB_UNUSED(x)
Definition: clib.h:79
vlib_node_registration_t snap_input_node
(constructor) VLIB_REGISTER_NODE (snap_input_node)
Definition: node.c:266
static uword snap_input(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *from_frame)
Definition: node.c:68
static char * snap_error_strings[]
Definition: node.c:260
always_inline uword mhash_set(mhash_t *h, void *key, uword new_value, uword *old_value)
Definition: mhash.h:111
#define vec_add2(V, P, N)
Add N elements to end of vector V, return pointer to new elements in P.
Definition: vec.h:519
#define foreach_snap_error
Definition: snap.h:116
#define hash_set_mem(h, key, value)
Definition: hash.h:257
#define clib_error_report(e)
Definition: error.h:126
vlib_error_t * errors
Definition: node.h:378
format_function_t format_snap_header
Definition: snap.h:175
static u8 * format_snap_input_trace(u8 *s, va_list *va)
Definition: node.c:56
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
#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
u8 packet_data[32]
Definition: node.c:53
u16 current_length
Nbytes between current data and the end of this buffer.
Definition: buffer.h:81
format_function_t format_snap_header_with_length
Definition: snap.h:176
#define PREDICT_FALSE(x)
Definition: clib.h:97
#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
mhash_t protocol_hash
Definition: snap.h:134
u16 n_vectors
Definition: node.h:307
#define CLIB_PREFETCH(addr, size, type)
Definition: cache.h:82
uword * protocol_info_by_name
Definition: snap.h:137
u16 cached_next_index
Definition: node.h:422
unsigned int u32
Definition: types.h:88
always_inline u32 snap_header_get_oui(snap_header_t *h)
Definition: snap.h:141
u8 * format(u8 *s, char *fmt,...)
Definition: format.c:405
snap_main_t snap_main
Definition: snap.c:45
unformat_function_t unformat_snap_header
Definition: snap.h:182
#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
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
unsigned short u16
Definition: types.h:57
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
always_inline void snap_setup_node(vlib_main_t *vm, u32 node_index)
Definition: snap.h:186
snap_protocol_info_t * protocols
Definition: snap.h:131
static clib_error_t * snap_input_init(vlib_main_t *vm)
Definition: node.c:287
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
static clib_error_t * snap_init(vlib_main_t *vm)
Definition: snap.c:173
snap_input_next_t
Definition: node.c:45
void snap_register_input_protocol(vlib_main_t *vm, char *name, u32 ieee_oui, u16 protocol, u32 node_index)
Definition: node.c:305