FD.io VPP  v20.09-64-g4f7b92f0a
Vector Packet Processing
node.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016,2020 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 #include <plugins/adl/adl.h>
17 #include <vnet/pg/pg.h>
18 
19 typedef struct {
23 
24 /* packet trace format function */
25 static u8 * format_adl_input_trace (u8 * s, va_list * args)
26 {
27  CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
28  CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
29  adl_input_trace_t * t = va_arg (*args, adl_input_trace_t *);
30 
31  s = format (s, "ADL_INPUT: sw_if_index %d, next index %d",
32  t->sw_if_index, t->next_index);
33  return s;
34 }
35 
36 #define foreach_adl_input_error \
37 _(PROCESSED, "Allow/Deny packets processed")
38 
39 typedef enum {
40 #define _(sym,str) ADL_INPUT_ERROR_##sym,
42 #undef _
45 
46 static char * adl_input_error_strings[] = {
47 #define _(sym,string) string,
49 #undef _
50 };
51 
55 {
56  u32 n_left_from, * from, * to_next;
57  adl_feature_type_t next_index;
58  adl_main_t *am = &adl_main;
59 
61  n_left_from = frame->n_vectors;
62  next_index = node->cached_next_index;
63 
64  while (n_left_from > 0)
65  {
66  u32 n_left_to_next;
67 
68  vlib_get_next_frame (vm, node, next_index,
69  to_next, n_left_to_next);
70 
71  while (n_left_from >= 4 && n_left_to_next >= 2)
72  {
73  u32 bi0, bi1;
74  vlib_buffer_t * b0, * b1;
75  u32 next0, next1;
76  u32 sw_if_index0, sw_if_index1;
77  ethernet_header_t * en0, * en1;
78  adl_config_main_t * ccm0, * ccm1;
79  u32 advance0, advance1;
80  int proto0, proto1;
81 
82  /* Prefetch next iteration. */
83  {
84  vlib_buffer_t * p2, * p3;
85 
86  p2 = vlib_get_buffer (vm, from[2]);
87  p3 = vlib_get_buffer (vm, from[3]);
88 
89  vlib_prefetch_buffer_header (p2, LOAD);
90  vlib_prefetch_buffer_header (p3, LOAD);
91 
94  }
95 
96  /* speculatively enqueue b0 and b1 to the current next frame */
97  to_next[0] = bi0 = from[0];
98  to_next[1] = bi1 = from[1];
99  from += 2;
100  to_next += 2;
101  n_left_from -= 2;
102  n_left_to_next -= 2;
103 
104  b0 = vlib_get_buffer (vm, bi0);
105  b1 = vlib_get_buffer (vm, bi1);
106 
107  en0 = vlib_buffer_get_current (b0);
108  en1 = vlib_buffer_get_current (b1);
109 
110  sw_if_index0 = adl_buffer(b0)->sw_if_index[VLIB_RX];
111  sw_if_index1 = adl_buffer(b1)->sw_if_index[VLIB_RX];
112 
113  proto0 = VNET_ADL_DEFAULT;
114  proto1 = VNET_ADL_DEFAULT;
115  advance0 = 0;
116  advance1 = 0;
117 
118  if (en0->type == clib_host_to_net_u16(ETHERNET_TYPE_IP4))
119  {
120  proto0 = VNET_ADL_IP4;
121  advance0 = sizeof(ethernet_header_t);
122  }
123  else if (en0->type == clib_host_to_net_u16(ETHERNET_TYPE_IP6))
124  {
125  proto0 = VNET_ADL_IP6;
126  advance0 = sizeof(ethernet_header_t);
127  }
128 
129  if (en1->type == clib_host_to_net_u16(ETHERNET_TYPE_IP4))
130  {
131  proto1 = VNET_ADL_IP4;
132  advance1 = sizeof(ethernet_header_t);
133  }
134  else if (en1->type == clib_host_to_net_u16(ETHERNET_TYPE_IP6))
135  {
136  proto1 = VNET_ADL_IP6;
137  advance1 = sizeof(ethernet_header_t);
138  }
139 
140  ccm0 = am->adl_config_mains + proto0;
141  ccm1 = am->adl_config_mains + proto1;
142  adl_buffer(b0)->adl.current_config_index =
143  ccm0->config_index_by_sw_if_index [sw_if_index0];
144 
145  adl_buffer(b1)->adl.current_config_index =
146  ccm1->config_index_by_sw_if_index [sw_if_index1];
147 
148  vlib_buffer_advance (b0, advance0);
149  vlib_buffer_advance (b1, advance1);
150 
152  &adl_buffer(b0)->adl.current_config_index,
153  &next0, 0 /* bytes of config data */);
154 
156  &adl_buffer(b1)->adl.current_config_index,
157  &next1, 0 /* bytes of config data */);
158 
160  && (b0->flags & VLIB_BUFFER_IS_TRACED)))
161  {
162  adl_input_trace_t *t =
163  vlib_add_trace (vm, node, b0, sizeof (*t));
164  t->sw_if_index = sw_if_index0;
165  t->next_index = next0;
166  }
167 
169  && (b1->flags & VLIB_BUFFER_IS_TRACED)))
170  {
171  adl_input_trace_t *t =
172  vlib_add_trace (vm, node, b1, sizeof (*t));
173  t->sw_if_index = sw_if_index1;
174  t->next_index = next1;
175  }
176  /* verify speculative enqueues, maybe switch current next frame */
177  vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
178  to_next, n_left_to_next,
179  bi0, bi1, next0, next1);
180  }
181 
182  while (n_left_from > 0 && n_left_to_next > 0)
183  {
184  u32 bi0;
185  vlib_buffer_t * b0;
186  u32 next0;
187  u32 sw_if_index0;
188  ethernet_header_t *en0;
189  adl_config_main_t *ccm0;
190  u32 advance0;
191  int proto0;
192 
193  /* speculatively enqueue b0 to the current next frame */
194  bi0 = from[0];
195  to_next[0] = bi0;
196  from += 1;
197  to_next += 1;
198  n_left_from -= 1;
199  n_left_to_next -= 1;
200 
201  b0 = vlib_get_buffer (vm, bi0);
202 
203  /*
204  * Direct from the driver, we should be at offset 0
205  * aka at &b0->data[0]
206  */
207  ASSERT (b0->current_data == 0);
208 
209  en0 = vlib_buffer_get_current (b0);
210 
211  sw_if_index0 = adl_buffer(b0)->sw_if_index[VLIB_RX];
212 
213  proto0 = VNET_ADL_DEFAULT;
214  advance0 = 0;
215 
216  if (en0->type == clib_host_to_net_u16(ETHERNET_TYPE_IP4))
217  {
218  proto0 = VNET_ADL_IP4;
219  advance0 = sizeof(ethernet_header_t);
220  }
221  else if (en0->type == clib_host_to_net_u16(ETHERNET_TYPE_IP6))
222  {
223  proto0 = VNET_ADL_IP6;
224  advance0 = sizeof(ethernet_header_t);
225  }
226 
227  ccm0 = am->adl_config_mains + proto0;
228  adl_buffer(b0)->adl.current_config_index =
229  ccm0->config_index_by_sw_if_index [sw_if_index0];
230 
231  vlib_buffer_advance (b0, advance0);
232 
234  &adl_buffer(b0)->adl.current_config_index,
235  &next0, 0 /* bytes of config data */);
236 
238  && (b0->flags & VLIB_BUFFER_IS_TRACED)))
239  {
240  adl_input_trace_t *t =
241  vlib_add_trace (vm, node, b0, sizeof (*t));
242  t->sw_if_index = sw_if_index0;
243  t->next_index = next0;
244  }
245 
246  /* verify speculative enqueue, maybe switch current next frame */
247  vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
248  to_next, n_left_to_next,
249  bi0, next0);
250  }
251 
252  vlib_put_next_frame (vm, node, next_index, n_left_to_next);
253  }
255  ADL_INPUT_ERROR_PROCESSED, frame->n_vectors);
256  return frame->n_vectors;
257 }
258 
260  .name = "adl-input",
261  .vector_size = sizeof (u32),
262  .format_trace = format_adl_input_trace,
263  .unformat_buffer = unformat_ethernet_header,
265 
266  .n_errors = ARRAY_LEN(adl_input_error_strings),
267  .error_strings = adl_input_error_strings,
268 
269  .n_next_nodes = ADL_RX_N_FEATURES,
270 
271  /* edit / add dispositions here */
272  .next_nodes = {
273  [IP4_RX_ADL_ALLOWLIST] = "ip4-adl-allowlist",
274  [IP6_RX_ADL_ALLOWLIST] = "ip6-adl-allowlist",
275  [DEFAULT_RX_ADL_ALLOWLIST] = "default-adl-allowlist",
276  [IP4_RX_ADL_INPUT] = "ip4-input",
277  [IP6_RX_ADL_INPUT] = "ip6-input",
278  [DEFAULT_RX_ADL_INPUT] = "ethernet-input",
279  [RX_ADL_DROP] = "error-drop",
280  },
281 };
282 
283 #define foreach_adl_stub \
284 _(default-adl-allowlist, default_adl_allowlist)
285 
286 #define _(n,f) \
287  \
288 static uword \
289 f##_node_fn (vlib_main_t * vm, \
290  vlib_node_runtime_t * node, \
291  vlib_frame_t * frame) \
292 { \
293  clib_warning ("BUG: stub function called"); \
294  return 0; \
295 } \
296  \
297 VLIB_REGISTER_NODE (f##_input_node) = { \
298  .function = f##_node_fn, \
299  .name = #n, \
300  .vector_size = sizeof (u32), \
301  .type = VLIB_NODE_TYPE_INTERNAL, \
302  \
303  .n_errors = 0, \
304  .error_strings = 0, \
305  \
306  .n_next_nodes = 0, \
307 };
308 
u32 flags
buffer flags: VLIB_BUFFER_FREE_LIST_INDEX_MASK: bits used to store free list index, VLIB_BUFFER_IS_TRACED: trace this buffer.
Definition: buffer.h:124
#define CLIB_UNUSED(x)
Definition: clib.h:87
i16 current_data
signed offset in data[], pre_data[] that we are currently processing.
Definition: buffer.h:110
#define foreach_adl_input_error
Definition: node.c:36
#define foreach_adl_stub
Definition: node.c:283
vlib_main_t * vm
Definition: in2out_ed.c:1582
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:424
#define VLIB_NODE_FN(node)
Definition: node.h:202
adl_config_main_t adl_config_mains[VNET_N_ADLS]
Definition: adl.h:66
unsigned char u8
Definition: types.h:56
Definition: adl.h:65
adl_main_t adl_main
Definition: adl.c:20
#define vlib_prefetch_buffer_header(b, type)
Prefetch buffer metadata.
Definition: buffer.h:203
unsigned int u32
Definition: types.h:88
vl_api_fib_path_type_t type
Definition: fib_types.api:123
static void * vlib_buffer_get_current(vlib_buffer_t *b)
Get pointer to current data to process.
Definition: buffer.h:229
static void * vnet_get_config_data(vnet_config_main_t *cm, u32 *config_index, u32 *next_index, u32 n_data_bytes)
Definition: config.h:123
#define PREDICT_FALSE(x)
Definition: clib.h:120
#define adl_buffer(b)
Definition: adl.h:112
#define vlib_validate_buffer_enqueue_x2(vm, node, next_index, to_next, n_left_to_next, bi0, bi1, next0, next1)
Finish enqueueing two buffers forward in the graph.
Definition: buffer_node.h:70
#define vlib_validate_buffer_enqueue_x1(vm, node, next_index, to_next, n_left_to_next, bi0, next0)
Finish enqueueing one buffer forward in the graph.
Definition: buffer_node.h:224
#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:391
vnet_config_main_t config_main
Definition: adl.h:57
static void vlib_node_increment_counter(vlib_main_t *vm, u32 node_index, u32 counter_index, u64 increment)
Definition: node_funcs.h:1231
#define VLIB_REGISTER_NODE(x,...)
Definition: node.h:169
#define CLIB_PREFETCH(addr, size, type)
Definition: cache.h:80
vlib_node_registration_t adl_input_node
(constructor) VLIB_REGISTER_NODE (adl_input_node)
Definition: node.c:259
u32 next_index
Definition: node.c:20
u8 data[]
Packet data.
Definition: buffer.h:181
#define ARRAY_LEN(x)
Definition: clib.h:67
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:483
vlib_main_t vlib_node_runtime_t * node
Definition: in2out_ed.c:1582
#define ASSERT(truth)
static void vlib_buffer_advance(vlib_buffer_t *b, word l)
Advance current data pointer by the supplied (signed!) amount.
Definition: buffer.h:248
static char * adl_input_error_strings[]
Definition: node.c:46
u32 * config_index_by_sw_if_index
Definition: adl.h:58
u32 sw_if_index
Definition: node.c:21
adl_feature_type_t
Definition: adl.h:39
vlib_main_t vlib_node_runtime_t vlib_frame_t * frame
Definition: in2out_ed.c:1583
VLIB buffer representation.
Definition: buffer.h:102
static void * vlib_frame_vector_args(vlib_frame_t *f)
Get pointer to frame vector data.
Definition: node_funcs.h:297
static u8 * format_adl_input_trace(u8 *s, va_list *args)
Definition: node.c:25
void * vlib_add_trace(vlib_main_t *vm, vlib_node_runtime_t *r, vlib_buffer_t *b, u32 n_data_bytes)
Definition: trace.c:577
adl_input_error_t
Definition: node.c:39
#define VLIB_NODE_FLAG_TRACE
Definition: node.h:301
#define CLIB_CACHE_LINE_BYTES
Definition: cache.h:59
static vlib_buffer_t * vlib_get_buffer(vlib_main_t *vm, u32 buffer_index)
Translate buffer index into buffer pointer.
Definition: buffer_funcs.h:85
Definition: defs.h:46
uword unformat_ethernet_header(unformat_input_t *input, va_list *args)
Definition: format.c:290