FD.io VPP  v21.01.1
Vector Packet Processing
ip4_allowlist.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 #include <plugins/adl/adl.h>
16 #include <vnet/fib/ip4_fib.h>
17 #include <vnet/dpo/load_balance.h>
18 
19 typedef struct {
23 
24 /* packet trace format function */
25 static u8 * format_ip4_adl_allowlist_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 *);
30 
31  s = format (s, "IP4_ADL_ALLOWLIST: sw_if_index %d, next index %d",
32  t->sw_if_index, t->next_index);
33  return s;
34 }
35 
36 #define foreach_ip4_adl_allowlist_error \
37 _(ALLOWED, "ip4 allowlist allowed") \
38 _(DROPPED, "ip4 allowlist dropped")
39 
40 typedef enum {
41 #define _(sym,str) IP4_ADL_ALLOWLIST_ERROR_##sym,
43 #undef _
46 
48 #define _(sym,string) string,
50 #undef _
51 };
52 
56 {
57  u32 n_left_from, * from, * to_next;
58  adl_feature_type_t next_index;
61  u32 thread_index = vm->thread_index;
62  u32 allowed_packets;
63 
65  n_left_from = frame->n_vectors;
66  allowed_packets = n_left_from;
67  next_index = node->cached_next_index;
68 
69  while (n_left_from > 0)
70  {
71  u32 n_left_to_next;
72 
73  vlib_get_next_frame (vm, node, next_index,
74  to_next, n_left_to_next);
75 
76  while (n_left_from >= 4 && n_left_to_next >= 2)
77  {
78  u32 bi0, bi1;
79  vlib_buffer_t * b0, * b1;
80  u32 next0, next1;
81  u32 sw_if_index0, sw_if_index1;
82  ip4_header_t * ip0, * ip1;
83  adl_config_main_t * ccm0, * ccm1;
84  adl_config_data_t * c0, * c1;
85  ip4_fib_mtrie_t * mtrie0, * mtrie1;
86  ip4_fib_mtrie_leaf_t leaf0, leaf1;
87  u32 lb_index0, lb_index1;
88  const load_balance_t * lb0, *lb1;
89  const dpo_id_t *dpo0, *dpo1;
90 
91  /* Prefetch next iteration. */
92  {
93  vlib_buffer_t * p2, * p3;
94 
95  p2 = vlib_get_buffer (vm, from[2]);
96  p3 = vlib_get_buffer (vm, from[3]);
97 
98  vlib_prefetch_buffer_header (p2, LOAD);
99  vlib_prefetch_buffer_header (p3, LOAD);
100 
103  }
104 
105  /* speculatively enqueue b0 and b1 to the current next frame */
106  to_next[0] = bi0 = from[0];
107  to_next[1] = bi1 = from[1];
108  from += 2;
109  to_next += 2;
110  n_left_from -= 2;
111  n_left_to_next -= 2;
112 
113  b0 = vlib_get_buffer (vm, bi0);
114  sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_RX];
115 
116  ip0 = vlib_buffer_get_current (b0);
117 
118  ccm0 = cm->adl_config_mains + VNET_ADL_IP4;
119 
121  (&ccm0->config_main,
122  &adl_buffer (b0)->adl.current_config_index,
123  &next0,
124  sizeof (c0[0]));
125 
126  mtrie0 = &ip4_fib_get (c0->fib_index)->mtrie;
127 
128  leaf0 = ip4_fib_mtrie_lookup_step_one (mtrie0, &ip0->src_address);
129 
130  leaf0 = ip4_fib_mtrie_lookup_step (mtrie0, leaf0,
131  &ip0->src_address, 2);
132 
133  leaf0 = ip4_fib_mtrie_lookup_step (mtrie0, leaf0,
134  &ip0->src_address, 3);
135 
136  lb_index0 = ip4_fib_mtrie_leaf_get_adj_index (leaf0);
137 
138  ASSERT (lb_index0
140  &ip0->src_address));
141  lb0 = load_balance_get (lb_index0);
142  dpo0 = load_balance_get_bucket_i(lb0, 0);
143 
144  if (PREDICT_FALSE(dpo0->dpoi_type != DPO_RECEIVE))
145  {
146  b0->error = node->errors[IP4_ADL_ALLOWLIST_ERROR_DROPPED];
147  allowed_packets--;
148  next0 = RX_ADL_DROP;
149  }
150 
151  b1 = vlib_get_buffer (vm, bi1);
152  sw_if_index1 = vnet_buffer(b1)->sw_if_index[VLIB_RX];
153 
154  ip1 = vlib_buffer_get_current (b1);
155 
156  ccm1 = cm->adl_config_mains + VNET_ADL_IP4;
157 
159  (&ccm1->config_main,
160  &adl_buffer (b1)->adl.current_config_index,
161  &next1,
162  sizeof (c1[0]));
163  mtrie1 = &ip4_fib_get (c1->fib_index)->mtrie;
164 
165  leaf1 = ip4_fib_mtrie_lookup_step_one (mtrie1, &ip1->src_address);
166 
167  leaf1 = ip4_fib_mtrie_lookup_step (mtrie1, leaf1,
168  &ip1->src_address, 2);
169 
170  leaf1 = ip4_fib_mtrie_lookup_step (mtrie1, leaf1,
171  &ip1->src_address, 3);
172 
173  lb_index1 = ip4_fib_mtrie_leaf_get_adj_index (leaf1);
174  ASSERT (lb_index1
176  &ip1->src_address));
177  lb1 = load_balance_get (lb_index1);
178  dpo1 = load_balance_get_bucket_i(lb1, 0);
179 
181  (vcm, thread_index, lb_index0, 1,
183  + sizeof(ethernet_header_t));
184 
186  (vcm, thread_index, lb_index1, 1,
188  + sizeof(ethernet_header_t));
189 
190 
191  if (PREDICT_FALSE(dpo1->dpoi_type != DPO_RECEIVE))
192  {
193  b1->error = node->errors[IP4_ADL_ALLOWLIST_ERROR_DROPPED];
194  allowed_packets--;
195  next1 = RX_ADL_DROP;
196  }
197 
199  && (b0->flags & VLIB_BUFFER_IS_TRACED)))
200  {
202  vlib_add_trace (vm, node, b0, sizeof (*t));
203  t->sw_if_index = sw_if_index0;
204  t->next_index = next0;
205  }
206 
208  && (b1->flags & VLIB_BUFFER_IS_TRACED)))
209  {
211  vlib_add_trace (vm, node, b1, sizeof (*t));
212  t->sw_if_index = sw_if_index1;
213  t->next_index = next1;
214  }
215 
216  /* verify speculative enqueues, maybe switch current next frame */
217  vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
218  to_next, n_left_to_next,
219  bi0, bi1, next0, next1);
220  }
221 
222  while (n_left_from > 0 && n_left_to_next > 0)
223  {
224  u32 bi0;
225  vlib_buffer_t * b0;
226  u32 next0;
227  u32 sw_if_index0;
228  ip4_header_t * ip0;
229  adl_config_main_t *ccm0;
230  adl_config_data_t *c0;
231  ip4_fib_mtrie_t * mtrie0;
232  ip4_fib_mtrie_leaf_t leaf0;
233  u32 lb_index0;
234  const load_balance_t * lb0;
235  const dpo_id_t *dpo0;
236 
237  /* speculatively enqueue b0 to the current next frame */
238  bi0 = from[0];
239  to_next[0] = bi0;
240  from += 1;
241  to_next += 1;
242  n_left_from -= 1;
243  n_left_to_next -= 1;
244 
245  b0 = vlib_get_buffer (vm, bi0);
246  sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_RX];
247 
248  ip0 = vlib_buffer_get_current (b0);
249 
250  ccm0 = cm->adl_config_mains + VNET_ADL_IP4;
251 
253  (&ccm0->config_main,
254  &adl_buffer (b0)->adl.current_config_index,
255  &next0,
256  sizeof (c0[0]));
257 
258  mtrie0 = &ip4_fib_get (c0->fib_index)->mtrie;
259 
260  leaf0 = ip4_fib_mtrie_lookup_step_one (mtrie0, &ip0->src_address);
261 
262  leaf0 = ip4_fib_mtrie_lookup_step (mtrie0, leaf0,
263  &ip0->src_address, 2);
264 
265  leaf0 = ip4_fib_mtrie_lookup_step (mtrie0, leaf0,
266  &ip0->src_address, 3);
267 
268  lb_index0 = ip4_fib_mtrie_leaf_get_adj_index (leaf0);
269 
270  ASSERT (lb_index0
272  &ip0->src_address));
273 
274  lb0 = load_balance_get (lb_index0);
275  dpo0 = load_balance_get_bucket_i(lb0, 0);
276 
278  (vcm, thread_index, lb_index0, 1,
280  + sizeof(ethernet_header_t));
281 
282  if (PREDICT_FALSE(dpo0->dpoi_type != DPO_RECEIVE))
283  {
284  b0->error = node->errors[IP4_ADL_ALLOWLIST_ERROR_DROPPED];
285  allowed_packets--;
286  next0 = RX_ADL_DROP;
287  }
288 
290  && (b0->flags & VLIB_BUFFER_IS_TRACED)))
291  {
293  vlib_add_trace (vm, node, b0, sizeof (*t));
294  t->sw_if_index = sw_if_index0;
295  t->next_index = next0;
296  }
297 
298  /* verify speculative enqueue, maybe switch current next frame */
299  vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
300  to_next, n_left_to_next,
301  bi0, next0);
302  }
303 
304  vlib_put_next_frame (vm, node, next_index, n_left_to_next);
305  }
306 
307  vlib_node_increment_counter (vm, node->node_index,
308  IP4_ADL_ALLOWLIST_ERROR_ALLOWED,
309  allowed_packets);
310 
311  return frame->n_vectors;
312 }
313 
315  .name = "ip4-adl-allowlist",
316  .vector_size = sizeof (u32),
317  .format_trace = format_ip4_adl_allowlist_trace,
319 
321  .error_strings = ip4_adl_allowlist_error_strings,
322 
323  .n_next_nodes = ADL_RX_N_FEATURES,
324 
325  /* edit / add dispositions here */
326  .next_nodes = {
327  [IP4_RX_ADL_ALLOWLIST] = "ip4-adl-allowlist",
328  [IP6_RX_ADL_ALLOWLIST] = "ip6-adl-allowlist",
329  [DEFAULT_RX_ADL_ALLOWLIST] = "default-adl-allowlist",
330  [IP4_RX_ADL_INPUT] = "ip4-input",
331  [IP6_RX_ADL_INPUT] = "ip6-input",
332  [DEFAULT_RX_ADL_INPUT] = "ethernet-input",
333  [RX_ADL_DROP] = "error-drop",
334  },
335 };
336 
337 static clib_error_t *
339 {
340  return 0;
341 }
342 
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 foreach_ip4_adl_allowlist_error
Definition: ip4_allowlist.c:36
#define CLIB_UNUSED(x)
Definition: clib.h:87
static void vlib_increment_combined_counter(vlib_combined_counter_main_t *cm, u32 thread_index, u32 index, u64 n_packets, u64 n_bytes)
Increment a combined counter.
Definition: counter.h:239
The mutiway-TRIE.
Definition: ip4_mtrie.h:129
ip4_address_t src_address
Definition: ip4_packet.h:125
static ip4_fib_mtrie_leaf_t ip4_fib_mtrie_lookup_step(const ip4_fib_mtrie_t *m, ip4_fib_mtrie_leaf_t current_leaf, const ip4_address_t *dst_address, u32 dst_address_byte_index)
Lookup step.
Definition: ip4_mtrie.h:202
u32 thread_index
Definition: main.h:250
vlib_main_t * vm
Definition: in2out_ed.c:1580
#define VLIB_NODE_FN(node)
Definition: node.h:203
adl_config_main_t adl_config_mains[VNET_N_ADLS]
Definition: adl.h:65
static uword vlib_buffer_length_in_chain(vlib_main_t *vm, vlib_buffer_t *b)
Get length in bytes of the buffer chain.
Definition: buffer_funcs.h:402
unsigned char u8
Definition: types.h:56
Definition: adl.h:64
adl_main_t adl_main
Definition: adl.c:20
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:173
description fragment has unexpected format
Definition: map.api:433
#define vlib_prefetch_buffer_header(b, type)
Prefetch buffer metadata.
Definition: buffer.h:207
u32 ip4_fib_mtrie_leaf_t
Definition: ip4_mtrie.h:52
unsigned int u32
Definition: types.h:88
static char * ip4_adl_allowlist_error_strings[]
Definition: ip4_allowlist.c:47
static u32 ip4_fib_mtrie_leaf_get_adj_index(ip4_fib_mtrie_leaf_t n)
From the stored slot value extract the LB index value.
Definition: ip4_mtrie.h:192
vl_api_fib_path_type_t type
Definition: fib_types.api:123
vlib_error_t error
Error code for buffers to be enqueued to error handler.
Definition: buffer.h:136
The identity of a DPO is a combination of its type and its instance number/index of objects of that t...
Definition: dpo.h:170
vnet_crypto_main_t * cm
Definition: quic_crypto.c:53
dpo_type_t dpoi_type
the type
Definition: dpo.h:176
static const dpo_id_t * load_balance_get_bucket_i(const load_balance_t *lb, u32 bucket)
Definition: load_balance.h:229
static void * vlib_buffer_get_current(vlib_buffer_t *b)
Get pointer to current data to process.
Definition: buffer.h:233
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
The FIB DPO provieds;.
Definition: load_balance.h:106
#define PREDICT_FALSE(x)
Definition: clib.h:121
#define adl_buffer(b)
Definition: adl.h:111
load_balance_main_t load_balance_main
The one instance of load-balance main.
Definition: load_balance.c:57
#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
ip4_adl_allowlist_error_t
Definition: ip4_allowlist.c:40
#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
ip4_fib_mtrie_t mtrie
Mtrie for fast lookups.
Definition: ip4_fib.h:48
vnet_config_main_t config_main
Definition: adl.h:56
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:170
#define CLIB_PREFETCH(addr, size, type)
Definition: cache.h:80
static clib_error_t * ip4_allowlist_init(vlib_main_t *vm)
static ip4_fib_t * ip4_fib_get(u32 index)
Get the FIB at the given index.
Definition: ip4_fib.h:113
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_combined_counter_main_t lbm_via_counters
Definition: load_balance.h:47
u32 fib_index
Definition: adl.h:61
vlib_main_t vlib_node_runtime_t * node
Definition: in2out_ed.c:1580
#define ASSERT(truth)
static ip4_fib_mtrie_leaf_t ip4_fib_mtrie_lookup_step_one(const ip4_fib_mtrie_t *m, const ip4_address_t *dst_address)
Lookup step number 1.
Definition: ip4_mtrie.h:224
static load_balance_t * load_balance_get(index_t lbi)
Definition: load_balance.h:220
static u8 * format_ip4_adl_allowlist_trace(u8 *s, va_list *args)
Definition: ip4_allowlist.c:25
vlib_node_registration_t ip4_adl_allowlist_node
(constructor) VLIB_REGISTER_NODE (ip4_adl_allowlist_node)
adl_feature_type_t
Definition: adl.h:38
vlib_main_t vlib_node_runtime_t vlib_frame_t * frame
Definition: in2out_ed.c:1581
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
A collection of combined counters.
Definition: counter.h:207
#define vnet_buffer(b)
Definition: buffer.h:417
index_t ip4_fib_table_lookup_lb(ip4_fib_t *fib, const ip4_address_t *addr)
Definition: ip4_fib.c:270
void * vlib_add_trace(vlib_main_t *vm, vlib_node_runtime_t *r, vlib_buffer_t *b, u32 n_data_bytes)
Definition: trace.c:634
#define VLIB_NODE_FLAG_TRACE
Definition: node.h:302
#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