FD.io VPP  v19.04-6-g6f05f72
Vector Packet Processing
ip4_whitelist.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016 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 <vnet/cop/cop.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_cop_whitelist_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_COP_WHITELIST: sw_if_index %d, next index %d",
32  t->sw_if_index, t->next_index);
33  return s;
34 }
35 
36 #define foreach_ip4_cop_whitelist_error \
37 _(DROPPED, "ip4 cop whitelist packets dropped")
38 
39 typedef enum {
40 #define _(sym,str) IP4_COP_WHITELIST_ERROR_##sym,
42 #undef _
45 
47 #define _(sym,string) string,
49 #undef _
50 };
51 
53  vlib_node_runtime_t * node,
54  vlib_frame_t * frame)
55 {
56  u32 n_left_from, * from, * to_next;
57  cop_feature_type_t next_index;
58  cop_main_t *cm = &cop_main;
60  u32 thread_index = vm->thread_index;
61 
62  from = vlib_frame_vector_args (frame);
63  n_left_from = frame->n_vectors;
64  next_index = node->cached_next_index;
65 
66  while (n_left_from > 0)
67  {
68  u32 n_left_to_next;
69 
70  vlib_get_next_frame (vm, node, next_index,
71  to_next, n_left_to_next);
72 
73  while (n_left_from >= 4 && n_left_to_next >= 2)
74  {
75  u32 bi0, bi1;
76  vlib_buffer_t * b0, * b1;
77  u32 next0, next1;
78  u32 sw_if_index0, sw_if_index1;
79  ip4_header_t * ip0, * ip1;
80  cop_config_main_t * ccm0, * ccm1;
81  cop_config_data_t * c0, * c1;
82  ip4_fib_mtrie_t * mtrie0, * mtrie1;
83  ip4_fib_mtrie_leaf_t leaf0, leaf1;
84  u32 lb_index0, lb_index1;
85  const load_balance_t * lb0, *lb1;
86  const dpo_id_t *dpo0, *dpo1;
87 
88  /* Prefetch next iteration. */
89  {
90  vlib_buffer_t * p2, * p3;
91 
92  p2 = vlib_get_buffer (vm, from[2]);
93  p3 = vlib_get_buffer (vm, from[3]);
94 
95  vlib_prefetch_buffer_header (p2, LOAD);
96  vlib_prefetch_buffer_header (p3, LOAD);
97 
100  }
101 
102  /* speculatively enqueue b0 and b1 to the current next frame */
103  to_next[0] = bi0 = from[0];
104  to_next[1] = bi1 = from[1];
105  from += 2;
106  to_next += 2;
107  n_left_from -= 2;
108  n_left_to_next -= 2;
109 
110  b0 = vlib_get_buffer (vm, bi0);
111  sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_RX];
112 
113  ip0 = vlib_buffer_get_current (b0);
114 
115  ccm0 = cm->cop_config_mains + VNET_COP_IP4;
116 
118  (&ccm0->config_main,
119  &vnet_buffer (b0)->cop.current_config_index,
120  &next0,
121  sizeof (c0[0]));
122 
123  mtrie0 = &ip4_fib_get (c0->fib_index)->mtrie;
124 
125  leaf0 = ip4_fib_mtrie_lookup_step_one (mtrie0, &ip0->src_address);
126 
127  leaf0 = ip4_fib_mtrie_lookup_step (mtrie0, leaf0,
128  &ip0->src_address, 2);
129 
130  leaf0 = ip4_fib_mtrie_lookup_step (mtrie0, leaf0,
131  &ip0->src_address, 3);
132 
133  lb_index0 = ip4_fib_mtrie_leaf_get_adj_index (leaf0);
134 
135  ASSERT (lb_index0
137  &ip0->src_address));
138  lb0 = load_balance_get (lb_index0);
139  dpo0 = load_balance_get_bucket_i(lb0, 0);
140 
141  if (PREDICT_FALSE(dpo0->dpoi_type != DPO_RECEIVE))
142  {
143  b0->error = node->errors[IP4_COP_WHITELIST_ERROR_DROPPED];
144  next0 = RX_COP_DROP;
145  }
146 
147  b1 = vlib_get_buffer (vm, bi1);
148  sw_if_index1 = vnet_buffer(b1)->sw_if_index[VLIB_RX];
149 
150  ip1 = vlib_buffer_get_current (b1);
151 
152  ccm1 = cm->cop_config_mains + VNET_COP_IP4;
153 
155  (&ccm1->config_main,
156  &vnet_buffer (b1)->cop.current_config_index,
157  &next1,
158  sizeof (c1[0]));
159  mtrie1 = &ip4_fib_get (c1->fib_index)->mtrie;
160 
161  leaf1 = ip4_fib_mtrie_lookup_step_one (mtrie1, &ip1->src_address);
162 
163  leaf1 = ip4_fib_mtrie_lookup_step (mtrie1, leaf1,
164  &ip1->src_address, 2);
165 
166  leaf1 = ip4_fib_mtrie_lookup_step (mtrie1, leaf1,
167  &ip1->src_address, 3);
168 
169  lb_index1 = ip4_fib_mtrie_leaf_get_adj_index (leaf1);
170  ASSERT (lb_index1
172  &ip1->src_address));
173  lb1 = load_balance_get (lb_index1);
174  dpo1 = load_balance_get_bucket_i(lb1, 0);
175 
177  (vcm, thread_index, lb_index0, 1,
179  + sizeof(ethernet_header_t));
180 
182  (vcm, thread_index, lb_index1, 1,
184  + sizeof(ethernet_header_t));
185 
186 
187  if (PREDICT_FALSE(dpo1->dpoi_type != DPO_RECEIVE))
188  {
189  b1->error = node->errors[IP4_COP_WHITELIST_ERROR_DROPPED];
190  next1 = RX_COP_DROP;
191  }
192 
193  if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE)
194  && (b0->flags & VLIB_BUFFER_IS_TRACED)))
195  {
197  vlib_add_trace (vm, node, b0, sizeof (*t));
198  t->sw_if_index = sw_if_index0;
199  t->next_index = next0;
200  }
201 
202  if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE)
203  && (b1->flags & VLIB_BUFFER_IS_TRACED)))
204  {
206  vlib_add_trace (vm, node, b1, sizeof (*t));
207  t->sw_if_index = sw_if_index1;
208  t->next_index = next1;
209  }
210 
211  /* verify speculative enqueues, maybe switch current next frame */
212  vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
213  to_next, n_left_to_next,
214  bi0, bi1, next0, next1);
215  }
216 
217  while (n_left_from > 0 && n_left_to_next > 0)
218  {
219  u32 bi0;
220  vlib_buffer_t * b0;
221  u32 next0;
222  u32 sw_if_index0;
223  ip4_header_t * ip0;
224  cop_config_main_t *ccm0;
225  cop_config_data_t *c0;
226  ip4_fib_mtrie_t * mtrie0;
227  ip4_fib_mtrie_leaf_t leaf0;
228  u32 lb_index0;
229  const load_balance_t * lb0;
230  const dpo_id_t *dpo0;
231 
232  /* speculatively enqueue b0 to the current next frame */
233  bi0 = from[0];
234  to_next[0] = bi0;
235  from += 1;
236  to_next += 1;
237  n_left_from -= 1;
238  n_left_to_next -= 1;
239 
240  b0 = vlib_get_buffer (vm, bi0);
241  sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_RX];
242 
243  ip0 = vlib_buffer_get_current (b0);
244 
245  ccm0 = cm->cop_config_mains + VNET_COP_IP4;
246 
248  (&ccm0->config_main,
249  &vnet_buffer (b0)->cop.current_config_index,
250  &next0,
251  sizeof (c0[0]));
252 
253  mtrie0 = &ip4_fib_get (c0->fib_index)->mtrie;
254 
255  leaf0 = ip4_fib_mtrie_lookup_step_one (mtrie0, &ip0->src_address);
256 
257  leaf0 = ip4_fib_mtrie_lookup_step (mtrie0, leaf0,
258  &ip0->src_address, 2);
259 
260  leaf0 = ip4_fib_mtrie_lookup_step (mtrie0, leaf0,
261  &ip0->src_address, 3);
262 
263  lb_index0 = ip4_fib_mtrie_leaf_get_adj_index (leaf0);
264 
265  ASSERT (lb_index0
267  &ip0->src_address));
268 
269  lb0 = load_balance_get (lb_index0);
270  dpo0 = load_balance_get_bucket_i(lb0, 0);
271 
273  (vcm, thread_index, lb_index0, 1,
275  + sizeof(ethernet_header_t));
276 
277  if (PREDICT_FALSE(dpo0->dpoi_type != DPO_RECEIVE))
278  {
279  b0->error = node->errors[IP4_COP_WHITELIST_ERROR_DROPPED];
280  next0 = RX_COP_DROP;
281  }
282 
283  if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE)
284  && (b0->flags & VLIB_BUFFER_IS_TRACED)))
285  {
287  vlib_add_trace (vm, node, b0, sizeof (*t));
288  t->sw_if_index = sw_if_index0;
289  t->next_index = next0;
290  }
291 
292  /* verify speculative enqueue, maybe switch current next frame */
293  vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
294  to_next, n_left_to_next,
295  bi0, next0);
296  }
297 
298  vlib_put_next_frame (vm, node, next_index, n_left_to_next);
299  }
300  return frame->n_vectors;
301 }
302 
304  .name = "ip4-cop-whitelist",
305  .vector_size = sizeof (u32),
306  .format_trace = format_ip4_cop_whitelist_trace,
307  .type = VLIB_NODE_TYPE_INTERNAL,
308 
310  .error_strings = ip4_cop_whitelist_error_strings,
311 
312  .n_next_nodes = COP_RX_N_FEATURES,
313 
314  /* edit / add dispositions here */
315  .next_nodes = {
316  [IP4_RX_COP_WHITELIST] = "ip4-cop-whitelist",
317  [IP6_RX_COP_WHITELIST] = "ip6-cop-whitelist",
318  [DEFAULT_RX_COP_WHITELIST] = "default-cop-whitelist",
319  [IP4_RX_COP_INPUT] = "ip4-input",
320  [IP6_RX_COP_INPUT] = "ip6-input",
321  [DEFAULT_RX_COP_INPUT] = "ethernet-input",
322  [RX_COP_DROP] = "error-drop",
323  },
324 };
325 
326 static clib_error_t *
328 {
329  return 0;
330 }
331 
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:82
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:220
The mutiway-TRIE.
Definition: ip4_mtrie.h:129
cop_config_main_t cop_config_mains[VNET_N_COPS]
Definition: cop.h:66
ip4_address_t src_address
Definition: ip4_packet.h:170
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
#define foreach_ip4_cop_whitelist_error
Definition: ip4_whitelist.c:36
u32 thread_index
Definition: main.h:197
u8 data[0]
Packet data.
Definition: buffer.h:181
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:424
#define VLIB_NODE_FN(node)
Definition: node.h:201
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:366
static u8 * format_ip4_cop_whitelist_trace(u8 *s, va_list *args)
Definition: ip4_whitelist.c:25
unsigned char u8
Definition: types.h:56
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:163
#define vlib_prefetch_buffer_header(b, type)
Prefetch buffer metadata.
Definition: buffer.h:203
u32 ip4_fib_mtrie_leaf_t
Definition: ip4_mtrie.h:52
unsigned int u32
Definition: types.h:88
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
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:168
static char * ip4_cop_whitelist_error_strings[]
Definition: ip4_whitelist.c:46
dpo_type_t dpoi_type
the type
Definition: dpo.h:172
static const dpo_id_t * load_balance_get_bucket_i(const load_balance_t *lb, u32 bucket)
Definition: load_balance.h:228
cop_feature_type_t
Definition: cop.h:39
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:122
The FIB DPO provieds;.
Definition: load_balance.h:106
#define PREDICT_FALSE(x)
Definition: clib.h:111
load_balance_main_t load_balance_main
The one instance of load-balance main.
Definition: load_balance.c:55
#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:218
#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:368
ip4_fib_mtrie_t mtrie
Mtrie for fast lookups.
Definition: ip4_fib.h:48
#define VLIB_REGISTER_NODE(x,...)
Definition: node.h:169
vlib_node_registration_t ip4_cop_whitelist_node
(constructor) VLIB_REGISTER_NODE (ip4_cop_whitelist_node)
#define CLIB_PREFETCH(addr, size, type)
Definition: cache.h:80
vlib_main_t * vm
Definition: buffer.c:312
static ip4_fib_t * ip4_fib_get(u32 index)
Get the FIB at the given index.
Definition: ip4_fib.h:113
#define ARRAY_LEN(x)
Definition: clib.h:62
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:465
cop_main_t cop_main
Definition: cop.c:17
vlib_combined_counter_main_t lbm_via_counters
Definition: load_balance.h:47
#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:219
static void * vlib_add_trace(vlib_main_t *vm, vlib_node_runtime_t *r, vlib_buffer_t *b, u32 n_data_bytes)
Definition: trace_funcs.h:57
ip4_cop_whitelist_error_t
Definition: ip4_whitelist.c:39
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:274
static clib_error_t * ip4_whitelist_init(vlib_main_t *vm)
A collection of combined counters.
Definition: counter.h:188
#define vnet_buffer(b)
Definition: buffer.h:369
index_t ip4_fib_table_lookup_lb(ip4_fib_t *fib, const ip4_address_t *addr)
Definition: ip4_fib.c:267
Definition: cop.h:65
#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
u32 fib_index
Definition: cop.h:62
Definition: defs.h:46
vnet_config_main_t config_main
Definition: cop.h:57