FD.io VPP  v19.08-27-gf4dcae4
Vector Packet Processing
ip6_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/ip6_fib.h>
17 #include <vnet/dpo/load_balance.h>
18 
19 typedef struct {
23 
24 /* packet trace format function */
25 static u8 * format_ip6_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, "IP6_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_ip6_cop_whitelist_error \
37 _(DROPPED, "ip6 cop whitelist packets dropped")
38 
39 typedef enum {
40 #define _(sym,str) IP6_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;
59  ip6_main_t * im6 = &ip6_main;
61  u32 thread_index = vm->thread_index;
62 
63  from = vlib_frame_vector_args (frame);
64  n_left_from = frame->n_vectors;
65  next_index = node->cached_next_index;
66 
67  while (n_left_from > 0)
68  {
69  u32 n_left_to_next;
70 
71  vlib_get_next_frame (vm, node, next_index,
72  to_next, n_left_to_next);
73 
74  while (n_left_from >= 4 && n_left_to_next >= 2)
75  {
76  u32 bi0, bi1;
77  vlib_buffer_t * b0, * b1;
78  u32 next0, next1;
79  u32 sw_if_index0, sw_if_index1;
80  ip6_header_t * ip0, * ip1;
81  cop_config_main_t * ccm0, * ccm1;
82  cop_config_data_t * c0, * c1;
83  u32 lb_index0, lb_index1;
84  const load_balance_t * lb0, *lb1;
85  const dpo_id_t *dpo0, *dpo1;
86 
87  /* Prefetch next iteration. */
88  {
89  vlib_buffer_t * p2, * p3;
90 
91  p2 = vlib_get_buffer (vm, from[2]);
92  p3 = vlib_get_buffer (vm, from[3]);
93 
94  vlib_prefetch_buffer_header (p2, LOAD);
95  vlib_prefetch_buffer_header (p3, LOAD);
96 
99  }
100 
101  /* speculatively enqueue b0 and b1 to the current next frame */
102  to_next[0] = bi0 = from[0];
103  to_next[1] = bi1 = from[1];
104  from += 2;
105  to_next += 2;
106  n_left_from -= 2;
107  n_left_to_next -= 2;
108 
109  b0 = vlib_get_buffer (vm, bi0);
110  sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_RX];
111 
112  ip0 = vlib_buffer_get_current (b0);
113 
114  ccm0 = cm->cop_config_mains + VNET_COP_IP6;
115 
117  (&ccm0->config_main,
118  &vnet_buffer (b0)->cop.current_config_index,
119  &next0,
120  sizeof (c0[0]));
121 
122  lb_index0 = ip6_fib_table_fwding_lookup (im6, c0->fib_index,
123  &ip0->src_address);
124  lb0 = load_balance_get (lb_index0);
125  dpo0 = load_balance_get_bucket_i(lb0, 0);
126 
127  if (PREDICT_FALSE(dpo0->dpoi_type != DPO_RECEIVE))
128  {
129  b0->error = node->errors[IP6_COP_WHITELIST_ERROR_DROPPED];
130  next0 = RX_COP_DROP;
131  }
132 
133  b1 = vlib_get_buffer (vm, bi1);
134  sw_if_index1 = vnet_buffer(b1)->sw_if_index[VLIB_RX];
135 
136  ip1 = vlib_buffer_get_current (b1);
137 
138  ccm1 = cm->cop_config_mains + VNET_COP_IP6;
139 
141  (&ccm1->config_main,
142  &vnet_buffer (b1)->cop.current_config_index,
143  &next1,
144  sizeof (c1[0]));
145 
146  lb_index1 = ip6_fib_table_fwding_lookup (im6, c1->fib_index,
147  &ip1->src_address);
148 
149  lb1 = load_balance_get (lb_index1);
150  dpo1 = load_balance_get_bucket_i(lb1, 0);
151 
153  (vcm, thread_index, lb_index0, 1,
155  + sizeof(ethernet_header_t));
156 
158  (vcm, thread_index, lb_index1, 1,
160  + sizeof(ethernet_header_t));
161 
162  if (PREDICT_FALSE(dpo1->dpoi_type != DPO_RECEIVE))
163  {
164  b1->error = node->errors[IP6_COP_WHITELIST_ERROR_DROPPED];
165  next1 = RX_COP_DROP;
166  }
167 
168  if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE)
169  && (b0->flags & VLIB_BUFFER_IS_TRACED)))
170  {
172  vlib_add_trace (vm, node, b0, sizeof (*t));
173  t->sw_if_index = sw_if_index0;
174  t->next_index = next0;
175  }
176 
177  if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE)
178  && (b1->flags & VLIB_BUFFER_IS_TRACED)))
179  {
181  vlib_add_trace (vm, node, b1, sizeof (*t));
182  t->sw_if_index = sw_if_index1;
183  t->next_index = next1;
184  }
185 
186  /* verify speculative enqueues, maybe switch current next frame */
187  vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
188  to_next, n_left_to_next,
189  bi0, bi1, next0, next1);
190  }
191 
192  while (n_left_from > 0 && n_left_to_next > 0)
193  {
194  u32 bi0;
195  vlib_buffer_t * b0;
196  u32 next0;
197  u32 sw_if_index0;
198  ip6_header_t * ip0;
199  cop_config_main_t *ccm0;
200  cop_config_data_t *c0;
201  u32 lb_index0;
202  const load_balance_t * lb0;
203  const dpo_id_t *dpo0;
204 
205  /* speculatively enqueue b0 to the current next frame */
206  bi0 = from[0];
207  to_next[0] = bi0;
208  from += 1;
209  to_next += 1;
210  n_left_from -= 1;
211  n_left_to_next -= 1;
212 
213  b0 = vlib_get_buffer (vm, bi0);
214  sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_RX];
215 
216  ip0 = vlib_buffer_get_current (b0);
217 
218  ccm0 = cm->cop_config_mains + VNET_COP_IP6;
219 
221  (&ccm0->config_main,
222  &vnet_buffer (b0)->cop.current_config_index,
223  &next0,
224  sizeof (c0[0]));
225 
226  lb_index0 = ip6_fib_table_fwding_lookup (im6, c0->fib_index,
227  &ip0->src_address);
228 
229  lb0 = load_balance_get (lb_index0);
230  dpo0 = load_balance_get_bucket_i(lb0, 0);
231 
233  (vcm, thread_index, lb_index0, 1,
235  + sizeof(ethernet_header_t));
236 
237  if (PREDICT_FALSE(dpo0->dpoi_type != DPO_RECEIVE))
238  {
239  b0->error = node->errors[IP6_COP_WHITELIST_ERROR_DROPPED];
240  next0 = RX_COP_DROP;
241  }
242 
243  if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE)
244  && (b0->flags & VLIB_BUFFER_IS_TRACED)))
245  {
247  vlib_add_trace (vm, node, b0, sizeof (*t));
248  t->sw_if_index = sw_if_index0;
249  t->next_index = next0;
250  }
251 
252  /* verify speculative enqueue, maybe switch current next frame */
253  vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
254  to_next, n_left_to_next,
255  bi0, next0);
256  }
257 
258  vlib_put_next_frame (vm, node, next_index, n_left_to_next);
259  }
260  return frame->n_vectors;
261 }
262 
264  .name = "ip6-cop-whitelist",
265  .vector_size = sizeof (u32),
266  .format_trace = format_ip6_cop_whitelist_trace,
268 
270  .error_strings = ip6_cop_whitelist_error_strings,
271 
272  .n_next_nodes = COP_RX_N_FEATURES,
273 
274  /* edit / add dispositions here */
275  .next_nodes = {
276  [IP4_RX_COP_WHITELIST] = "ip4-cop-whitelist",
277  [IP6_RX_COP_WHITELIST] = "ip6-cop-whitelist",
278  [DEFAULT_RX_COP_WHITELIST] = "default-cop-whitelist",
279  [IP4_RX_COP_INPUT] = "ip4-input",
280  [IP6_RX_COP_INPUT] = "ip6-input",
281  [DEFAULT_RX_COP_INPUT] = "ethernet-input",
282  [RX_COP_DROP] = "error-drop",
283  },
284 };
285 
286 static clib_error_t *
288 {
289  return 0;
290 }
291 
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
cop_config_main_t cop_config_mains[VNET_N_COPS]
Definition: cop.h:66
u32 thread_index
Definition: main.h:197
u8 data[0]
Packet data.
Definition: buffer.h:181
vlib_node_registration_t ip6_cop_whitelist_node
(constructor) VLIB_REGISTER_NODE (ip6_cop_whitelist_node)
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:424
#define VLIB_NODE_FN(node)
Definition: node.h:201
static u32 ip6_fib_table_fwding_lookup(ip6_main_t *im, u32 fib_index, const ip6_address_t *dst)
Definition: ip6_fib.h:67
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
ip6_address_t src_address
Definition: ip6_packet.h:383
unsigned char u8
Definition: types.h:56
ip6_cop_whitelist_error_t
Definition: ip6_whitelist.c:39
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:173
#define vlib_prefetch_buffer_header(b, type)
Prefetch buffer metadata.
Definition: buffer.h:203
unsigned int u32
Definition: types.h:88
static clib_error_t * ip6_whitelist_init(vlib_main_t *vm)
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:41
static char * ip6_cop_whitelist_error_strings[]
Definition: ip6_whitelist.c:46
dpo_type_t dpoi_type
the type
Definition: dpo.h:174
static const dpo_id_t * load_balance_get_bucket_i(const load_balance_t *lb, u32 bucket)
Definition: load_balance.h:229
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
#define foreach_ip6_cop_whitelist_error
Definition: ip6_whitelist.c:36
load_balance_main_t load_balance_main
The one instance of load-balance main.
Definition: load_balance.c:56
#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:338
#define VLIB_REGISTER_NODE(x,...)
Definition: node.h:169
#define CLIB_PREFETCH(addr, size, type)
Definition: cache.h:80
vlib_main_t * vm
Definition: buffer.c:312
#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:458
cop_main_t cop_main
Definition: cop.c:17
vlib_combined_counter_main_t lbm_via_counters
Definition: load_balance.h:47
ip6_main_t ip6_main
Definition: ip6_forward.c:2671
static load_balance_t * load_balance_get(index_t lbi)
Definition: load_balance.h:220
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
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:244
A collection of combined counters.
Definition: counter.h:188
#define vnet_buffer(b)
Definition: buffer.h:361
static u8 * format_ip6_cop_whitelist_trace(u8 *s, va_list *args)
Definition: ip6_whitelist.c:25
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