FD.io VPP  v16.09
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 
17 typedef struct {
21 
22 /* packet trace format function */
23 static u8 * format_ip4_cop_whitelist_trace (u8 * s, va_list * args)
24 {
25  CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
26  CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
28 
29  s = format (s, "IP4_COP_WHITELIST: sw_if_index %d, next index %d",
30  t->sw_if_index, t->next_index);
31  return s;
32 }
33 
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 
52 static uword
54  vlib_node_runtime_t * node,
55  vlib_frame_t * frame)
56 {
57  u32 n_left_from, * from, * to_next;
58  cop_feature_type_t next_index;
59  cop_main_t *cm = &cop_main;
60  ip4_main_t * im4 = &ip4_main;
61  ip_lookup_main_t * lm4 = &im4->lookup_main;
63  u32 cpu_index = vm->cpu_index;
64 
65  from = vlib_frame_vector_args (frame);
66  n_left_from = frame->n_vectors;
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  cop_config_main_t * ccm0, * ccm1;
84  cop_config_data_t * c0, * c1;
85  ip4_fib_mtrie_t * mtrie0, * mtrie1;
86  ip4_fib_mtrie_leaf_t leaf0, leaf1;
87  u32 adj_index0, adj_index1;
88  ip_adjacency_t * adj0, * adj1;
89 
90  /* Prefetch next iteration. */
91  {
92  vlib_buffer_t * p2, * p3;
93 
94  p2 = vlib_get_buffer (vm, from[2]);
95  p3 = vlib_get_buffer (vm, from[3]);
96 
97  vlib_prefetch_buffer_header (p2, LOAD);
98  vlib_prefetch_buffer_header (p3, LOAD);
99 
102  }
103 
104  /* speculatively enqueue b0 and b1 to the current next frame */
105  to_next[0] = bi0 = from[0];
106  to_next[1] = bi1 = from[1];
107  from += 2;
108  to_next += 2;
109  n_left_from -= 2;
110  n_left_to_next -= 2;
111 
112  b0 = vlib_get_buffer (vm, bi0);
113  sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_RX];
114 
115  ip0 = vlib_buffer_get_current (b0);
116 
117  ccm0 = cm->cop_config_mains + VNET_COP_IP4;
118 
120  (&ccm0->config_main,
121  &vnet_buffer (b0)->cop.current_config_index,
122  &next0,
123  sizeof (c0[0]));
124 
125  mtrie0 = &vec_elt_at_index (im4->fibs, c0->fib_index)->mtrie;
126 
127  leaf0 = IP4_FIB_MTRIE_LEAF_ROOT;
128 
129  leaf0 = ip4_fib_mtrie_lookup_step (mtrie0, leaf0,
130  &ip0->src_address, 0);
131 
132  leaf0 = ip4_fib_mtrie_lookup_step (mtrie0, leaf0,
133  &ip0->src_address, 1);
134 
135  leaf0 = ip4_fib_mtrie_lookup_step (mtrie0, leaf0,
136  &ip0->src_address, 2);
137 
138  leaf0 = ip4_fib_mtrie_lookup_step (mtrie0, leaf0,
139  &ip0->src_address, 3);
140 
141  adj_index0 = ip4_fib_mtrie_leaf_get_adj_index (leaf0);
142 
143  ASSERT (adj_index0
145  &ip0->src_address,
146  1 /* no_default_route */));
147  adj0 = ip_get_adjacency (lm4, adj_index0);
149  {
150  b0->error = node->errors[IP4_COP_WHITELIST_ERROR_DROPPED];
151  next0 = RX_COP_DROP;
152  }
153 
154  b1 = vlib_get_buffer (vm, bi1);
155  sw_if_index1 = vnet_buffer(b1)->sw_if_index[VLIB_RX];
156 
157  ip1 = vlib_buffer_get_current (b1);
158 
159  ccm1 = cm->cop_config_mains + VNET_COP_IP4;
160 
162  (&ccm1->config_main,
163  &vnet_buffer (b1)->cop.current_config_index,
164  &next1,
165  sizeof (c1[0]));
166 
167  mtrie1 = &vec_elt_at_index (im4->fibs, c1->fib_index)->mtrie;
168 
169  leaf1 = IP4_FIB_MTRIE_LEAF_ROOT;
170 
171  leaf1 = ip4_fib_mtrie_lookup_step (mtrie1, leaf1,
172  &ip1->src_address, 0);
173 
174  leaf1 = ip4_fib_mtrie_lookup_step (mtrie1, leaf1,
175  &ip1->src_address, 1);
176 
177  leaf1 = ip4_fib_mtrie_lookup_step (mtrie1, leaf1,
178  &ip1->src_address, 2);
179 
180  leaf1 = ip4_fib_mtrie_lookup_step (mtrie1, leaf1,
181  &ip1->src_address, 3);
182 
183  adj_index1 = ip4_fib_mtrie_leaf_get_adj_index (leaf1);
184 
185  ASSERT (adj_index1
187  &ip1->src_address,
188  1 /* no_default_route */));
189  adj1 = ip_get_adjacency (lm4, adj_index1);
190 
192  (vcm, cpu_index, adj_index0, 1,
194  + sizeof(ethernet_header_t));
195 
197  (vcm, cpu_index, adj_index1, 1,
199  + sizeof(ethernet_header_t));
200 
202  {
203  b0->error = node->errors[IP4_COP_WHITELIST_ERROR_DROPPED];
204  next0 = RX_COP_DROP;
205  }
206 
208  {
209  b1->error = node->errors[IP4_COP_WHITELIST_ERROR_DROPPED];
210  next1 = RX_COP_DROP;
211  }
212 
214  && (b0->flags & VLIB_BUFFER_IS_TRACED)))
215  {
217  vlib_add_trace (vm, node, b0, sizeof (*t));
218  t->sw_if_index = sw_if_index0;
219  t->next_index = next0;
220  }
221 
223  && (b1->flags & VLIB_BUFFER_IS_TRACED)))
224  {
226  vlib_add_trace (vm, node, b1, sizeof (*t));
227  t->sw_if_index = sw_if_index1;
228  t->next_index = next1;
229  }
230 
231  /* verify speculative enqueues, maybe switch current next frame */
232  vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
233  to_next, n_left_to_next,
234  bi0, bi1, next0, next1);
235  }
236 
237  while (n_left_from > 0 && n_left_to_next > 0)
238  {
239  u32 bi0;
240  vlib_buffer_t * b0;
241  u32 next0;
242  u32 sw_if_index0;
243  ip4_header_t * ip0;
244  cop_config_main_t *ccm0;
245  cop_config_data_t *c0;
246  ip4_fib_mtrie_t * mtrie0;
247  ip4_fib_mtrie_leaf_t leaf0;
248  u32 adj_index0;
249  ip_adjacency_t * adj0;
250 
251  /* speculatively enqueue b0 to the current next frame */
252  bi0 = from[0];
253  to_next[0] = bi0;
254  from += 1;
255  to_next += 1;
256  n_left_from -= 1;
257  n_left_to_next -= 1;
258 
259  b0 = vlib_get_buffer (vm, bi0);
260  sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_RX];
261 
262  ip0 = vlib_buffer_get_current (b0);
263 
264  ccm0 = cm->cop_config_mains + VNET_COP_IP4;
265 
267  (&ccm0->config_main,
268  &vnet_buffer (b0)->cop.current_config_index,
269  &next0,
270  sizeof (c0[0]));
271 
272  mtrie0 = &vec_elt_at_index (im4->fibs, c0->fib_index)->mtrie;
273 
274  leaf0 = IP4_FIB_MTRIE_LEAF_ROOT;
275 
276  leaf0 = ip4_fib_mtrie_lookup_step (mtrie0, leaf0,
277  &ip0->src_address, 0);
278 
279  leaf0 = ip4_fib_mtrie_lookup_step (mtrie0, leaf0,
280  &ip0->src_address, 1);
281 
282  leaf0 = ip4_fib_mtrie_lookup_step (mtrie0, leaf0,
283  &ip0->src_address, 2);
284 
285  leaf0 = ip4_fib_mtrie_lookup_step (mtrie0, leaf0,
286  &ip0->src_address, 3);
287 
288  adj_index0 = ip4_fib_mtrie_leaf_get_adj_index (leaf0);
289 
290  ASSERT (adj_index0
292  &ip0->src_address,
293  1 /* no_default_route */));
294  adj0 = ip_get_adjacency (lm4, adj_index0);
295 
297  (vcm, cpu_index, adj_index0, 1,
299  + sizeof(ethernet_header_t));
300 
302  {
303  b0->error = node->errors[IP4_COP_WHITELIST_ERROR_DROPPED];
304  next0 = RX_COP_DROP;
305  }
306 
308  && (b0->flags & VLIB_BUFFER_IS_TRACED)))
309  {
311  vlib_add_trace (vm, node, b0, sizeof (*t));
312  t->sw_if_index = sw_if_index0;
313  t->next_index = next0;
314  }
315 
316  /* verify speculative enqueue, maybe switch current next frame */
317  vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
318  to_next, n_left_to_next,
319  bi0, next0);
320  }
321 
322  vlib_put_next_frame (vm, node, next_index, n_left_to_next);
323  }
324  return frame->n_vectors;
325 }
326 
328  .function = ip4_cop_whitelist_node_fn,
329  .name = "ip4-cop-whitelist",
330  .vector_size = sizeof (u32),
331  .format_trace = format_ip4_cop_whitelist_trace,
333 
335  .error_strings = ip4_cop_whitelist_error_strings,
336 
337  .n_next_nodes = COP_RX_N_FEATURES,
338 
339  /* edit / add dispositions here */
340  .next_nodes = {
341  [IP4_RX_COP_WHITELIST] = "ip4-cop-whitelist",
342  [IP6_RX_COP_WHITELIST] = "ip6-cop-whitelist",
343  [DEFAULT_RX_COP_WHITELIST] = "default-cop-whitelist",
344  [IP4_RX_COP_INPUT] = "ip4-input",
345  [IP6_RX_COP_INPUT] = "ip6-input",
346  [DEFAULT_RX_COP_INPUT] = "ethernet-input",
347  [RX_COP_DROP] = "error-drop",
348  },
349 };
350 
352 
353 static clib_error_t *
355 {
356  return 0;
357 }
358 
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:457
#define CLIB_UNUSED(x)
Definition: clib.h:79
cop_config_main_t cop_config_mains[VNET_N_COPS]
Definition: cop.h:66
ip4_address_t src_address
Definition: ip4_packet.h:138
bad routing header type(not 4)") sr_error (NO_MORE_SEGMENTS
ip_lookup_next_t lookup_next_index
Definition: lookup.h:180
IP unicast adjacency.
Definition: lookup.h:164
#define foreach_ip4_cop_whitelist_error
Definition: ip4_whitelist.c:36
struct _vlib_node_registration vlib_node_registration_t
ip_lookup_main_t lookup_main
Definition: ip4.h:115
vlib_error_t * errors
Definition: node.h:418
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:112
static u8 * format_ip4_cop_whitelist_trace(u8 *s, va_list *args)
Definition: ip4_whitelist.c:23
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:111
static void * vlib_buffer_get_current(vlib_buffer_t *b)
Get pointer to current data to process.
Definition: buffer.h:187
u32 ip4_fib_mtrie_leaf_t
Definition: ip4_mtrie.h:52
#define vec_elt_at_index(v, i)
Get vector value at index i checking that i is in bounds.
u32 ip4_fib_lookup_with_table(ip4_main_t *im, u32 fib_index, ip4_address_t *dst, u32 disable_default_route)
Definition: ip4_forward.c:58
u32 cpu_index
Definition: main.h:159
This packet is for one of our own IP addresses.
Definition: lookup.h:68
static u32 ip4_fib_mtrie_leaf_get_adj_index(ip4_fib_mtrie_leaf_t n)
Definition: ip4_mtrie.h:66
static char * ip4_cop_whitelist_error_strings[]
Definition: ip4_whitelist.c:46
cop_feature_type_t
Definition: cop.h:39
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
#define PREDICT_FALSE(x)
Definition: clib.h:97
#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
static ip4_fib_mtrie_leaf_t ip4_fib_mtrie_lookup_step(ip4_fib_mtrie_t *m, ip4_fib_mtrie_leaf_t current_leaf, ip4_address_t *dst_address, u32 dst_address_byte_index)
Definition: ip4_mtrie.h:145
#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:130
#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:348
vlib_error_t error
Error code for buffers to be enqueued to error handler.
Definition: buffer.h:118
vlib_node_registration_t ip4_cop_whitelist_node
(constructor) VLIB_REGISTER_NODE (ip4_cop_whitelist_node)
Definition: ip4_whitelist.c:34
u16 n_vectors
Definition: node.h:344
#define CLIB_PREFETCH(addr, size, type)
Definition: cache.h:82
#define ARRAY_LEN(x)
Definition: clib.h:59
cop_main_t cop_main
Definition: cop.c:17
vlib_combined_counter_main_t adjacency_counters
Adjacency packet/byte counters indexed by adjacency index.
Definition: lookup.h:404
u16 cached_next_index
Definition: node.h:462
static void vlib_increment_combined_counter(vlib_combined_counter_main_t *cm, u32 cpu_index, u32 index, u32 packet_increment, u32 byte_increment)
Increment a combined counter.
Definition: counter.h:241
#define ASSERT(truth)
#define IP4_FIB_MTRIE_LEAF_ROOT
Definition: ip4_mtrie.h:55
unsigned int u32
Definition: types.h:88
ip4_fib_t * fibs
Vector of FIBs.
Definition: ip4.h:118
#define vnet_buffer(b)
Definition: buffer.h:335
u8 * format(u8 *s, char *fmt,...)
Definition: format.c:418
IPv4 main type.
Definition: ip4.h:114
#define VLIB_NODE_FLAG_TRACE
Definition: node.h:259
#define VLIB_BUFFER_IS_TRACED
Definition: buffer.h:93
u64 uword
Definition: types.h:112
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:55
ip4_cop_whitelist_error_t
Definition: ip4_whitelist.c:39
unsigned char u8
Definition: types.h:56
static void * vlib_frame_vector_args(vlib_frame_t *f)
Get pointer to frame vector data.
Definition: node_funcs.h:251
static clib_error_t * ip4_whitelist_init(vlib_main_t *vm)
A collection of combined counters.
Definition: counter.h:212
#define vlib_prefetch_buffer_header(b, type)
Prefetch buffer metadata.
Definition: buffer.h:163
#define VLIB_NODE_FUNCTION_MULTIARCH(node, fn)
Definition: node.h:158
#define VLIB_REGISTER_NODE(x,...)
Definition: node.h:143
ip4_main_t ip4_main
Global ip4 main structure.
Definition: ip4_forward.c:1578
u8 data[0]
Packet data.
Definition: buffer.h:151
Definition: cop.h:65
#define CLIB_CACHE_LINE_BYTES
Definition: cache.h:67
u32 flags
buffer flags: VLIB_BUFFER_IS_TRACED: trace this buffer.
Definition: buffer.h:85
static vlib_buffer_t * vlib_get_buffer(vlib_main_t *vm, u32 buffer_index)
Translate buffer index into buffer pointer.
Definition: buffer_funcs.h:69
static uword ip4_cop_whitelist_node_fn(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
Definition: ip4_whitelist.c:53
u32 fib_index
Definition: cop.h:62
Definition: defs.h:46
vnet_config_main_t config_main
Definition: cop.h:57
static ip_adjacency_t * ip_get_adjacency(ip_lookup_main_t *lm, u32 adj_index)
Definition: lookup.h:480