FD.io VPP  v21.10.1-2-g0a485f517
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;
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 
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  u32 lb_index0, lb_index1;
86  const load_balance_t *lb0, *lb1;
87  const dpo_id_t *dpo0, *dpo1;
88 
89  /* Prefetch next iteration. */
90  {
91  vlib_buffer_t *p2, *p3;
92 
93  p2 = vlib_get_buffer (vm, from[2]);
94  p3 = vlib_get_buffer (vm, from[3]);
95 
96  vlib_prefetch_buffer_header (p2, LOAD);
97  vlib_prefetch_buffer_header (p3, LOAD);
98 
101  }
102 
103  /* speculatively enqueue b0 and b1 to the current next frame */
104  to_next[0] = bi0 = from[0];
105  to_next[1] = bi1 = from[1];
106  from += 2;
107  to_next += 2;
108  n_left_from -= 2;
109  n_left_to_next -= 2;
110 
111  b0 = vlib_get_buffer (vm, bi0);
112  sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
113 
114  ip0 = vlib_buffer_get_current (b0);
115 
116  ccm0 = cm->adl_config_mains + VNET_ADL_IP4;
117 
118  c0 = vnet_get_config_data (
119  &ccm0->config_main, &adl_buffer (b0)->adl.current_config_index,
120  &next0, sizeof (c0[0]));
121 
122  lb_index0 =
124 
125  ASSERT (lb_index0
127  &ip0->src_address));
128  lb0 = load_balance_get (lb_index0);
129  dpo0 = load_balance_get_bucket_i(lb0, 0);
130 
131  if (PREDICT_FALSE(dpo0->dpoi_type != DPO_RECEIVE))
132  {
133  b0->error = node->errors[IP4_ADL_ALLOWLIST_ERROR_DROPPED];
134  allowed_packets--;
135  next0 = RX_ADL_DROP;
136  }
137 
138  b1 = vlib_get_buffer (vm, bi1);
139  sw_if_index1 = vnet_buffer(b1)->sw_if_index[VLIB_RX];
140 
141  ip1 = vlib_buffer_get_current (b1);
142 
143  ccm1 = cm->adl_config_mains + VNET_ADL_IP4;
144 
146  (&ccm1->config_main,
147  &adl_buffer (b1)->adl.current_config_index,
148  &next1,
149  sizeof (c1[0]));
150 
151  lb_index1 =
153 
154  ASSERT (lb_index1
156  &ip1->src_address));
157  lb1 = load_balance_get (lb_index1);
158  dpo1 = load_balance_get_bucket_i(lb1, 0);
159 
161  (vcm, thread_index, lb_index0, 1,
163  + sizeof(ethernet_header_t));
164 
166  (vcm, thread_index, lb_index1, 1,
168  + sizeof(ethernet_header_t));
169 
170 
171  if (PREDICT_FALSE(dpo1->dpoi_type != DPO_RECEIVE))
172  {
173  b1->error = node->errors[IP4_ADL_ALLOWLIST_ERROR_DROPPED];
174  allowed_packets--;
175  next1 = RX_ADL_DROP;
176  }
177 
179  && (b0->flags & VLIB_BUFFER_IS_TRACED)))
180  {
182  vlib_add_trace (vm, node, b0, sizeof (*t));
183  t->sw_if_index = sw_if_index0;
184  t->next_index = next0;
185  }
186 
188  && (b1->flags & VLIB_BUFFER_IS_TRACED)))
189  {
191  vlib_add_trace (vm, node, b1, sizeof (*t));
192  t->sw_if_index = sw_if_index1;
193  t->next_index = next1;
194  }
195 
196  /* verify speculative enqueues, maybe switch current next frame */
198  to_next, n_left_to_next,
199  bi0, bi1, next0, next1);
200  }
201 
202  while (n_left_from > 0 && n_left_to_next > 0)
203  {
204  u32 bi0;
205  vlib_buffer_t * b0;
206  u32 next0;
207  u32 sw_if_index0;
208  ip4_header_t * ip0;
209  adl_config_main_t *ccm0;
210  adl_config_data_t *c0;
211  u32 lb_index0;
212  const load_balance_t *lb0;
213  const dpo_id_t *dpo0;
214 
215  /* speculatively enqueue b0 to the current next frame */
216  bi0 = from[0];
217  to_next[0] = bi0;
218  from += 1;
219  to_next += 1;
220  n_left_from -= 1;
221  n_left_to_next -= 1;
222 
223  b0 = vlib_get_buffer (vm, bi0);
224  sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_RX];
225 
226  ip0 = vlib_buffer_get_current (b0);
227 
228  ccm0 = cm->adl_config_mains + VNET_ADL_IP4;
229 
231  (&ccm0->config_main,
232  &adl_buffer (b0)->adl.current_config_index,
233  &next0,
234  sizeof (c0[0]));
235 
236  lb_index0 =
238 
239  ASSERT (lb_index0 ==
241  &ip0->src_address));
242 
243  lb0 = load_balance_get (lb_index0);
244  dpo0 = load_balance_get_bucket_i(lb0, 0);
245 
247  (vcm, thread_index, lb_index0, 1,
249  + sizeof(ethernet_header_t));
250 
251  if (PREDICT_FALSE(dpo0->dpoi_type != DPO_RECEIVE))
252  {
253  b0->error = node->errors[IP4_ADL_ALLOWLIST_ERROR_DROPPED];
254  allowed_packets--;
255  next0 = RX_ADL_DROP;
256  }
257 
259  && (b0->flags & VLIB_BUFFER_IS_TRACED)))
260  {
262  vlib_add_trace (vm, node, b0, sizeof (*t));
263  t->sw_if_index = sw_if_index0;
264  t->next_index = next0;
265  }
266 
267  /* verify speculative enqueue, maybe switch current next frame */
269  to_next, n_left_to_next,
270  bi0, next0);
271  }
272 
273  vlib_put_next_frame (vm, node, next_index, n_left_to_next);
274  }
275 
276  vlib_node_increment_counter (vm, node->node_index,
277  IP4_ADL_ALLOWLIST_ERROR_ALLOWED,
278  allowed_packets);
279 
280  return frame->n_vectors;
281 }
282 
284  .name = "ip4-adl-allowlist",
285  .vector_size = sizeof (u32),
286  .format_trace = format_ip4_adl_allowlist_trace,
288 
290  .error_strings = ip4_adl_allowlist_error_strings,
291 
292  .n_next_nodes = ADL_RX_N_FEATURES,
293 
294  /* edit / add dispositions here */
295  .next_nodes = {
296  [IP4_RX_ADL_ALLOWLIST] = "ip4-adl-allowlist",
297  [IP6_RX_ADL_ALLOWLIST] = "ip6-adl-allowlist",
298  [DEFAULT_RX_ADL_ALLOWLIST] = "default-adl-allowlist",
299  [IP4_RX_ADL_INPUT] = "ip4-input",
300  [IP6_RX_ADL_INPUT] = "ip6-input",
301  [DEFAULT_RX_ADL_INPUT] = "ethernet-input",
302  [RX_ADL_DROP] = "error-drop",
303  },
304 };
305 
306 static clib_error_t *
308 {
309  return 0;
310 }
311 
load_balance.h
VNET_ADL_IP4
@ VNET_ADL_IP4
Definition: adl.h:32
thread_index
u32 thread_index
Definition: nat44_ei_hairpinning.c:495
vlib_prefetch_buffer_header
#define vlib_prefetch_buffer_header(b, type)
Prefetch buffer metadata.
Definition: buffer.h:231
frame
vlib_main_t vlib_node_runtime_t vlib_frame_t * frame
Definition: nat44_ei.c:3048
DEFAULT_RX_ADL_ALLOWLIST
@ DEFAULT_RX_ADL_ALLOWLIST
Definition: adl.h:42
next_index
nat44_ei_hairpin_src_next_t next_index
Definition: nat44_ei_hairpinning.c:412
ip4_allowlist_init
static clib_error_t * ip4_allowlist_init(vlib_main_t *vm)
Definition: ip4_allowlist.c:307
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
dpo_id_t_::dpoi_type
dpo_type_t dpoi_type
the type
Definition: dpo.h:178
ADL_RX_N_FEATURES
@ ADL_RX_N_FEATURES
Definition: adl.h:52
VLIB_NODE_TYPE_INTERNAL
@ VLIB_NODE_TYPE_INTERNAL
Definition: node.h:72
node
vlib_main_t vlib_node_runtime_t * node
Definition: nat44_ei.c:3047
RX_ADL_DROP
@ RX_ADL_DROP
Definition: adl.h:50
DEFAULT_RX_ADL_INPUT
@ DEFAULT_RX_ADL_INPUT
Definition: adl.h:47
ip4_adl_allowlist_error_t
ip4_adl_allowlist_error_t
Definition: ip4_allowlist.c:40
adl_feature_type_t
adl_feature_type_t
Definition: adl.h:38
load_balance_main_t_::lbm_via_counters
vlib_combined_counter_main_t lbm_via_counters
Definition: load_balance.h:47
vm
vlib_main_t * vm
X-connect all packets from the HOST to the PHY.
Definition: nat44_ei.c:3047
VLIB_RX
@ VLIB_RX
Definition: defs.h:46
ip4_fib_table_lookup_lb
#define ip4_fib_table_lookup_lb
Definition: ip4_fib.h:74
vlib_frame_t
Definition: node.h:372
vlib_buffer_length_in_chain
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:433
ip4_header_t
Definition: ip4_packet.h:87
DPO_RECEIVE
@ DPO_RECEIVE
Definition: dpo.h:112
ip4_adl_allowlist_error_strings
static char * ip4_adl_allowlist_error_strings[]
Definition: ip4_allowlist.c:47
adl_main_t
Definition: adl.h:64
IP4_RX_ADL_ALLOWLIST
@ IP4_RX_ADL_ALLOWLIST
Definition: adl.h:40
load_balance_main
load_balance_main_t load_balance_main
The one instance of load-balance main.
Definition: load_balance.c:59
ip4_adl_allowlist_trace_t::sw_if_index
u32 sw_if_index
Definition: ip4_allowlist.c:21
vlib_buffer_t::error
vlib_error_t error
Error code for buffers to be enqueued to error handler.
Definition: buffer.h:145
VLIB_NODE_FN
#define VLIB_NODE_FN(node)
Definition: node.h:202
CLIB_UNUSED
#define CLIB_UNUSED(x)
Definition: clib.h:90
vnet_buffer
#define vnet_buffer(b)
Definition: buffer.h:441
ip4_adl_allowlist_trace_t
Definition: ip4_allowlist.c:19
VLIB_NODE_FLAG_TRACE
#define VLIB_NODE_FLAG_TRACE
Definition: node.h:291
PREDICT_FALSE
#define PREDICT_FALSE(x)
Definition: clib.h:124
ARRAY_LEN
#define ARRAY_LEN(x)
Definition: clib.h:70
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
ethernet_header_t
Definition: packet.h:52
vlib_main_t::thread_index
u32 thread_index
Definition: main.h:215
vlib_node_increment_counter
static void vlib_node_increment_counter(vlib_main_t *vm, u32 node_index, u32 counter_index, u64 increment)
Definition: node_funcs.h:1244
load_balance_get_bucket_i
static const dpo_id_t * load_balance_get_bucket_i(const load_balance_t *lb, u32 bucket)
Definition: load_balance.h:229
adl_config_main_t
Definition: adl.h:55
ip4_fib_get
static ip4_fib_t * ip4_fib_get(u32 index)
Get the FIB at the given index.
Definition: ip4_fib.h:88
cm
vnet_feature_config_main_t * cm
Definition: nat44_ei_hairpinning.c:594
ip4_adl_allowlist_node
vlib_node_registration_t ip4_adl_allowlist_node
(constructor) VLIB_REGISTER_NODE (ip4_adl_allowlist_node)
Definition: ip4_allowlist.c:283
vlib_node_registration_t
struct _vlib_node_registration vlib_node_registration_t
adl_config_data_t::fib_index
u32 fib_index
Definition: adl.h:61
vlib_validate_buffer_enqueue_x1
#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
ip4_header_t::src_address
ip4_address_t src_address
Definition: ip4_packet.h:125
adl_config_data_t
Definition: adl.h:60
format
description fragment has unexpected format
Definition: map.api:433
ASSERT
#define ASSERT(truth)
Definition: error_bootstrap.h:69
vlib_combined_counter_main_t
A collection of combined counters.
Definition: counter.h:203
format_ip4_adl_allowlist_trace
static u8 * format_ip4_adl_allowlist_trace(u8 *s, va_list *args)
Definition: ip4_allowlist.c:25
vlib_put_next_frame
vlib_put_next_frame(vm, node, next_index, 0)
u32
unsigned int u32
Definition: types.h:88
IP6_RX_ADL_ALLOWLIST
@ IP6_RX_ADL_ALLOWLIST
Definition: adl.h:41
VLIB_INIT_FUNCTION
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:172
IP4_ADL_ALLOWLIST_N_ERROR
@ IP4_ADL_ALLOWLIST_N_ERROR
Definition: ip4_allowlist.c:44
load_balance_get
static load_balance_t * load_balance_get(index_t lbi)
Definition: load_balance.h:220
ip4_adl_allowlist_trace_t::next_index
u32 next_index
Definition: ip4_allowlist.c:20
ip4_fib.h
vlib_main_t
Definition: main.h:102
vlib_node_t
Definition: node.h:247
vlib_add_trace
void * vlib_add_trace(vlib_main_t *vm, vlib_node_runtime_t *r, vlib_buffer_t *b, u32 n_data_bytes)
Definition: trace.c:628
foreach_ip4_adl_allowlist_error
#define foreach_ip4_adl_allowlist_error
Definition: ip4_allowlist.c:36
u8
unsigned char u8
Definition: types.h:56
clib_error_t
Definition: clib_error.h:21
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
vnet_get_config_data
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
vlib_buffer_t::data
u8 data[]
Packet data.
Definition: buffer.h:204
IP4_RX_ADL_INPUT
@ IP4_RX_ADL_INPUT
Definition: adl.h:45
dpo_id_t_
The identity of a DPO is a combination of its type and its instance number/index of objects of that t...
Definition: dpo.h:172
vlib_validate_buffer_enqueue_x2
#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_fib_forwarding_lookup
static index_t ip4_fib_forwarding_lookup(u32 fib_index, const ip4_address_t *addr)
Definition: ip4_fib.h:223
load_balance_t_
The FIB DPO provieds;.
Definition: load_balance.h:106
clib_prefetch_store
static_always_inline void clib_prefetch_store(void *p)
Definition: cache.h:98
vlib_node_runtime_t
Definition: node.h:454
adl_main
adl_main_t adl_main
Definition: adl.c:20
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
adl_config_main_t::config_main
vnet_config_main_t config_main
Definition: adl.h:56
n_left_from
n_left_from
Definition: nat44_ei_hairpinning.c:416
type
vl_api_fib_path_type_t type
Definition: fib_types.api:123
vlib_increment_combined_counter
vlib_increment_combined_counter(ccm, ti, sw_if_index, n_buffers, n_bytes)
adl_buffer
#define adl_buffer(b)
Definition: adl.h:111
IP6_RX_ADL_INPUT
@ IP6_RX_ADL_INPUT
Definition: adl.h:46
adl.h
vlib_buffer_t::flags
u32 flags
buffer flags: VLIB_BUFFER_FREE_LIST_INDEX_MASK: bits used to store free list index,...
Definition: buffer.h:133
vlib_buffer_t
VLIB buffer representation.
Definition: buffer.h:111
VLIB_REGISTER_NODE
#define VLIB_REGISTER_NODE(x,...)
Definition: node.h:169