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  * 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
46 {
52 
53 typedef struct
54 {
55  u8 packet_data[32];
57 
58 static u8 *
59 format_snap_input_trace (u8 * s, va_list * va)
60 {
61  CLIB_UNUSED (vlib_main_t * vm) = va_arg (*va, vlib_main_t *);
62  CLIB_UNUSED (vlib_node_t * node) = va_arg (*va, vlib_node_t *);
63  snap_input_trace_t *t = va_arg (*va, snap_input_trace_t *);
64 
65  s = format (s, "%U", format_snap_header, t->packet_data);
66 
67  return s;
68 }
69 
70 static uword
73 {
74  snap_main_t *sm = &snap_main;
75  u32 n_left_from, next_index, *from, *to_next;
76 
78  n_left_from = from_frame->n_vectors;
79 
80  if (node->flags & VLIB_NODE_FLAG_TRACE)
82  from,
84  sizeof (from[0]),
85  sizeof (snap_input_trace_t));
86 
87  next_index = node->cached_next_index;
88 
89  while (n_left_from > 0)
90  {
91  u32 n_left_to_next;
92 
93  vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
94 
95  while (n_left_from >= 4 && n_left_to_next >= 2)
96  {
97  u32 bi0, bi1;
98  vlib_buffer_t *b0, *b1;
99  snap_header_t *h0, *h1;
100  snap_protocol_info_t *pi0, *pi1;
101  u8 next0, next1, is_ethernet0, is_ethernet1, len0, len1,
102  enqueue_code;
103  u32 oui0, oui1;
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 = vlib_buffer_get_current (b0);
132  h1 = vlib_buffer_get_current (b1);
133 
134  oui0 = snap_header_get_oui (h0);
135  oui1 = snap_header_get_oui (h1);
136 
137  is_ethernet0 = oui0 == IEEE_OUI_ethernet;
138  is_ethernet1 = oui1 == IEEE_OUI_ethernet;
139 
140  len0 = sizeof (h0[0]) - (is_ethernet0 ? sizeof (h0->protocol) : 0);
141  len1 = sizeof (h1[0]) - (is_ethernet1 ? sizeof (h1->protocol) : 0);
142 
143  vlib_buffer_advance (b0, len0);
144  vlib_buffer_advance (b1, len1);
145 
146  pi0 = snap_get_protocol_info (sm, h0);
147  pi1 = snap_get_protocol_info (sm, h1);
148 
149  next0 = pi0 ? pi0->next_index : SNAP_INPUT_NEXT_DROP;
150  next1 = pi1 ? pi1->next_index : SNAP_INPUT_NEXT_DROP;
151 
152  next0 = is_ethernet0 ? SNAP_INPUT_NEXT_ETHERNET_TYPE : next0;
153  next1 = is_ethernet1 ? SNAP_INPUT_NEXT_ETHERNET_TYPE : next1;
154 
155  /* In case of error. */
156  b0->error = node->errors[SNAP_ERROR_UNKNOWN_PROTOCOL];
157  b1->error = node->errors[SNAP_ERROR_UNKNOWN_PROTOCOL];
158 
159  enqueue_code = (next0 != next_index) + 2 * (next1 != next_index);
160 
161  if (PREDICT_FALSE (enqueue_code != 0))
162  {
163  switch (enqueue_code)
164  {
165  case 1:
166  /* A B A */
167  to_next[-2] = bi1;
168  to_next -= 1;
169  n_left_to_next += 1;
170  vlib_set_next_frame_buffer (vm, node, next0, bi0);
171  break;
172 
173  case 2:
174  /* A A B */
175  to_next -= 1;
176  n_left_to_next += 1;
177  vlib_set_next_frame_buffer (vm, node, next1, bi1);
178  break;
179 
180  case 3:
181  /* A B B or A B C */
182  to_next -= 2;
183  n_left_to_next += 2;
184  vlib_set_next_frame_buffer (vm, node, next0, bi0);
185  vlib_set_next_frame_buffer (vm, node, next1, bi1);
186  if (next0 == next1)
187  {
189  n_left_to_next);
190  next_index = next1;
192  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;
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 = vlib_buffer_get_current (b0);
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  vlib_buffer_advance (b0, len0);
225 
226  pi0 = snap_get_protocol_info (sm, h0);
227 
228  next0 = pi0 ? pi0->next_index : SNAP_INPUT_NEXT_DROP;
229 
230  next0 = is_ethernet0 ? SNAP_INPUT_NEXT_ETHERNET_TYPE : next0;
231 
232  /* In case of error. */
233  b0->error = node->errors[SNAP_ERROR_UNKNOWN_PROTOCOL];
234 
235  /* Sent packet to wrong next? */
236  if (PREDICT_FALSE (next0 != next_index))
237  {
238  /* Return old frame; remove incorrectly enqueued packet. */
239  vlib_put_next_frame (vm, node, next_index, n_left_to_next + 1);
240 
241  /* Send to correct next. */
242  next_index = next0;
244  to_next, n_left_to_next);
245 
246  to_next[0] = bi0;
247  to_next += 1;
248  n_left_to_next -= 1;
249  }
250  }
251 
252  vlib_put_next_frame (vm, node, next_index, n_left_to_next);
253  }
254 
255  return from_frame->n_vectors;
256 }
257 
258 static char *snap_error_strings[] = {
259 #define _(f,s) s,
261 #undef _
262 };
263 
264 /* *INDENT-OFF* */
266  .function = snap_input,
267  .name = "snap-input",
268  /* Takes a vector of packets. */
269  .vector_size = sizeof (u32),
270 
271  .n_errors = SNAP_N_ERROR,
272  .error_strings = snap_error_strings,
273 
274  .n_next_nodes = SNAP_INPUT_N_NEXT,
275  .next_nodes = {
276  [SNAP_INPUT_NEXT_DROP] = "error-drop",
277  [SNAP_INPUT_NEXT_PUNT] = "error-punt",
278  [SNAP_INPUT_NEXT_ETHERNET_TYPE] = "ethernet-input-type",
279  },
280 
281  .format_buffer = format_snap_header_with_length,
282  .format_trace = format_snap_input_trace,
283  .unformat_buffer = unformat_snap_header,
284 };
285 /* *INDENT-ON* */
286 
287 static void
289 {
292 
296 }
297 
298 static clib_error_t *
300 {
301  {
303  if (error)
305  }
306 
308 
309  llc_register_input_protocol (vm, LLC_PROTOCOL_snap, snap_input_node.index);
310 
311  return 0;
312 }
313 
315 
316 void
318  char *name,
319  u32 ieee_oui, u16 protocol, u32 node_index)
320 {
321  snap_main_t *sm = &snap_main;
325 
326  {
328  if (error)
330  }
331 
332  h.protocol = clib_host_to_net_u16 (protocol);
333  h.oui[0] = (ieee_oui >> 16) & 0xff;
334  h.oui[1] = (ieee_oui >> 8) & 0xff;
335  h.oui[2] = (ieee_oui >> 0) & 0xff;
336  pi = snap_get_protocol_info (sm, &h);
337  if (pi)
338  return;
339 
340  vec_add2 (sm->protocols, pi, 1);
341 
342  pi->name = format (0, "%s", name);
343  pi->node_index = node_index;
345 
346  key.oui = ieee_oui;
347  key.protocol = clib_host_to_net_u16 (protocol);
348 
349  mhash_set (&sm->protocol_hash, &key, pi - sm->protocols, /* old_value */ 0);
351 }
352 
353 /*
354  * fd.io coding-style-patch-verification: ON
355  *
356  * Local Variables:
357  * eval: (c-set-style "gnu")
358  * End:
359  */
snap_main_t::protocol_hash
mhash_t protocol_hash
Definition: snap.h:142
vlib.h
snap_header_get_oui
static u32 snap_header_get_oui(snap_header_t *h)
Definition: snap.h:149
snap_header_t
Definition: snap.h:76
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
SNAP_INPUT_NEXT_DROP
@ SNAP_INPUT_NEXT_DROP
Definition: node.c:47
name
string name[64]
Definition: fib.api:25
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
u16
unsigned short u16
Definition: types.h:57
hash_set_mem
#define hash_set_mem(h, key, value)
Definition: hash.h:275
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
snap_input_next_t
snap_input_next_t
Definition: node.c:45
unformat_pg_snap_header
unformat_function_t unformat_pg_snap_header
Definition: snap.h:184
snap_init
static clib_error_t * snap_init(vlib_main_t *vm)
Definition: snap.c:179
from_frame
vlib_main_t vlib_node_runtime_t vlib_frame_t * from_frame
Definition: esp_encrypt.c:1328
snap_oui_and_protocol_t
Definition: snap.h:91
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
h
h
Definition: flowhash_template.h:372
error
Definition: cJSON.c:88
key
typedef key
Definition: ipsec_types.api:91
pg_node_t
Definition: pg.h:332
snap.h
snap_protocol_info_t::name
u8 * name
Definition: snap.h:100
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
vlib_buffer_t::error
vlib_error_t error
Error code for buffers to be enqueued to error handler.
Definition: buffer.h:145
vec_add2
#define vec_add2(V, P, N)
Add N elements to end of vector V, return pointer to new elements in P.
Definition: vec.h:644
pg_node_t::unformat_edit
unformat_function_t * unformat_edit
Definition: pg.h:335
CLIB_UNUSED
#define CLIB_UNUSED(x)
Definition: clib.h:90
format_snap_header
format_function_t format_snap_header
Definition: snap.h:176
snap_register_input_protocol
void snap_register_input_protocol(vlib_main_t *vm, char *name, u32 ieee_oui, u16 protocol, u32 node_index)
Definition: node.c:317
VLIB_NODE_FLAG_TRACE
#define VLIB_NODE_FLAG_TRACE
Definition: node.h:291
snap_error_strings
static char * snap_error_strings[]
Definition: node.c:258
PREDICT_FALSE
#define PREDICT_FALSE(x)
Definition: clib.h:124
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
unformat_snap_header
unformat_function_t unformat_snap_header
Definition: snap.h:183
snap_main_t::protocol_info_by_name
uword * protocol_info_by_name
Definition: snap.h:145
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
SNAP_INPUT_N_NEXT
@ SNAP_INPUT_N_NEXT
Definition: node.c:50
snap_input_trace_t
Definition: node.c:53
vlib_node_registration_t
struct _vlib_node_registration vlib_node_registration_t
snap_input
static uword snap_input(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *from_frame)
Definition: node.c:71
snap_setup_node
static void snap_setup_node(vlib_main_t *vm, u32 node_index)
Definition: node.c:288
snap_input_node
vlib_node_registration_t snap_input_node
(constructor) VLIB_REGISTER_NODE (snap_input_node)
Definition: node.c:265
format_snap_input_trace
static u8 * format_snap_input_trace(u8 *s, va_list *va)
Definition: node.c:59
SNAP_INPUT_NEXT_PUNT
@ SNAP_INPUT_NEXT_PUNT
Definition: node.c:48
format
description fragment has unexpected format
Definition: map.api:433
snap_main_t
Definition: snap.h:134
vlib_put_next_frame
vlib_put_next_frame(vm, node, next_index, 0)
SNAP_N_ERROR
@ SNAP_N_ERROR
Definition: snap.h:131
snap_protocol_info_t
Definition: snap.h:97
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
snap_protocol_info_t::node_index
u32 node_index
Definition: snap.h:105
protocol
vl_api_ip_proto_t protocol
Definition: lb_types.api:72
snap_input_init
static clib_error_t * snap_input_init(vlib_main_t *vm)
Definition: node.c:299
vlib_main_t
Definition: main.h:102
foreach_snap_error
#define foreach_snap_error
Definition: snap.h:122
vlib_node_t
Definition: node.h:247
u8
unsigned char u8
Definition: types.h:56
clib_error_t
Definition: clib_error.h:21
llc.h
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
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
snap_input_trace_t::packet_data
u8 packet_data[32]
Definition: node.c:55
format_snap_header_with_length
format_function_t format_snap_header_with_length
Definition: snap.h:177
SNAP_INPUT_NEXT_ETHERNET_TYPE
@ SNAP_INPUT_NEXT_ETHERNET_TYPE
Definition: node.c:49
snap_get_protocol_info
static snap_protocol_info_t * snap_get_protocol_info(snap_main_t *sm, snap_header_t *h)
Definition: snap.h:155
snap_protocol_info_t::next_index
u32 next_index
Definition: snap.h:108
llc_register_input_protocol
void llc_register_input_protocol(vlib_main_t *vm, llc_protocol_t protocol, u32 node_index)
Definition: node.c:308
vlib_node_runtime_t
Definition: node.h:454
from
from
Definition: nat44_ei_hairpinning.c:415
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
n_left_from
n_left_from
Definition: nat44_ei_hairpinning.c:416
snap_main_t::protocols
snap_protocol_info_t * protocols
Definition: snap.h:139
mhash_set
static uword mhash_set(mhash_t *h, void *key, uword new_value, uword *old_value)
Definition: mhash.h:117
vlib_buffer_t
VLIB buffer representation.
Definition: buffer.h:111
VLIB_REGISTER_NODE
#define VLIB_REGISTER_NODE(x,...)
Definition: node.h:169
snap_main
snap_main_t snap_main
Definition: snap.c:45