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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
15 #include <vnet/cop/cop.h>
20 } ip4_cop_whitelist_trace_t;
22 /* packet trace format function */
23 static u8 * format_ip4_cop_whitelist_trace (u8 * s, va_list * args)
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 *);
27 ip4_cop_whitelist_trace_t * t = va_arg (*args, ip4_cop_whitelist_trace_t *);
29 s = format (s, "IP4_COP_WHITELIST: sw_if_index %d, next index %d",
30 t->sw_if_index, t->next_index);
34 vlib_node_registration_t ip4_cop_whitelist_node;
36 #define foreach_ip4_cop_whitelist_error \
37 _(DROPPED, "ip4 cop whitelist packets dropped")
40 #define _(sym,str) IP4_COP_WHITELIST_ERROR_##sym,
41 foreach_ip4_cop_whitelist_error
43 IP4_COP_WHITELIST_N_ERROR,
44 } ip4_cop_whitelist_error_t;
46 static char * ip4_cop_whitelist_error_strings[] = {
47 #define _(sym,string) string,
48 foreach_ip4_cop_whitelist_error
53 ip4_cop_whitelist_node_fn (vlib_main_t * vm,
54 vlib_node_runtime_t * node,
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;
62 vlib_combined_counter_main_t * vcm = &im4->lookup_main.adjacency_counters;
63 u32 cpu_index = vm->cpu_index;
65 from = vlib_frame_vector_args (frame);
66 n_left_from = frame->n_vectors;
67 next_index = node->cached_next_index;
69 while (n_left_from > 0)
73 vlib_get_next_frame (vm, node, next_index,
74 to_next, n_left_to_next);
76 while (n_left_from >= 4 && n_left_to_next >= 2)
79 vlib_buffer_t * b0, * b1;
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;
90 /* Prefetch next iteration. */
92 vlib_buffer_t * p2, * p3;
94 p2 = vlib_get_buffer (vm, from[2]);
95 p3 = vlib_get_buffer (vm, from[3]);
97 vlib_prefetch_buffer_header (p2, LOAD);
98 vlib_prefetch_buffer_header (p3, LOAD);
100 CLIB_PREFETCH (p2->data, CLIB_CACHE_LINE_BYTES, STORE);
101 CLIB_PREFETCH (p3->data, CLIB_CACHE_LINE_BYTES, STORE);
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];
112 b0 = vlib_get_buffer (vm, bi0);
113 sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_RX];
115 ip0 = vlib_buffer_get_current (b0);
117 ccm0 = cm->cop_config_mains + VNET_COP_IP4;
119 c0 = vnet_get_config_data
121 &vnet_buffer (b0)->cop.current_config_index,
125 mtrie0 = &vec_elt_at_index (im4->fibs, c0->fib_index)->mtrie;
127 leaf0 = IP4_FIB_MTRIE_LEAF_ROOT;
129 leaf0 = ip4_fib_mtrie_lookup_step (mtrie0, leaf0,
130 &ip0->src_address, 0);
132 leaf0 = ip4_fib_mtrie_lookup_step (mtrie0, leaf0,
133 &ip0->src_address, 1);
135 leaf0 = ip4_fib_mtrie_lookup_step (mtrie0, leaf0,
136 &ip0->src_address, 2);
138 leaf0 = ip4_fib_mtrie_lookup_step (mtrie0, leaf0,
139 &ip0->src_address, 3);
141 adj_index0 = ip4_fib_mtrie_leaf_get_adj_index (leaf0);
144 == ip4_fib_lookup_with_table (im4, c0->fib_index,
146 1 /* no_default_route */));
147 adj0 = ip_get_adjacency (lm4, adj_index0);
148 if (PREDICT_FALSE(adj0->lookup_next_index != IP_LOOKUP_NEXT_LOCAL))
150 b0->error = node->errors[IP4_COP_WHITELIST_ERROR_DROPPED];
154 b1 = vlib_get_buffer (vm, bi1);
155 sw_if_index1 = vnet_buffer(b1)->sw_if_index[VLIB_RX];
157 ip1 = vlib_buffer_get_current (b1);
159 ccm1 = cm->cop_config_mains + VNET_COP_IP4;
161 c1 = vnet_get_config_data
163 &vnet_buffer (b1)->cop.current_config_index,
167 mtrie1 = &vec_elt_at_index (im4->fibs, c1->fib_index)->mtrie;
169 leaf1 = IP4_FIB_MTRIE_LEAF_ROOT;
171 leaf1 = ip4_fib_mtrie_lookup_step (mtrie1, leaf1,
172 &ip1->src_address, 0);
174 leaf1 = ip4_fib_mtrie_lookup_step (mtrie1, leaf1,
175 &ip1->src_address, 1);
177 leaf1 = ip4_fib_mtrie_lookup_step (mtrie1, leaf1,
178 &ip1->src_address, 2);
180 leaf1 = ip4_fib_mtrie_lookup_step (mtrie1, leaf1,
181 &ip1->src_address, 3);
183 adj_index1 = ip4_fib_mtrie_leaf_get_adj_index (leaf1);
186 == ip4_fib_lookup_with_table (im4, c1->fib_index,
188 1 /* no_default_route */));
189 adj1 = ip_get_adjacency (lm4, adj_index1);
191 vlib_increment_combined_counter
192 (vcm, cpu_index, adj_index0, 1,
193 vlib_buffer_length_in_chain (vm, b0)
194 + sizeof(ethernet_header_t));
196 vlib_increment_combined_counter
197 (vcm, cpu_index, adj_index1, 1,
198 vlib_buffer_length_in_chain (vm, b1)
199 + sizeof(ethernet_header_t));
201 if (PREDICT_FALSE(adj0->lookup_next_index != IP_LOOKUP_NEXT_LOCAL))
203 b0->error = node->errors[IP4_COP_WHITELIST_ERROR_DROPPED];
207 if (PREDICT_FALSE(adj1->lookup_next_index != IP_LOOKUP_NEXT_LOCAL))
209 b1->error = node->errors[IP4_COP_WHITELIST_ERROR_DROPPED];
213 if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE)
214 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
216 ip4_cop_whitelist_trace_t *t =
217 vlib_add_trace (vm, node, b0, sizeof (*t));
218 t->sw_if_index = sw_if_index0;
219 t->next_index = next0;
222 if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE)
223 && (b1->flags & VLIB_BUFFER_IS_TRACED)))
225 ip4_cop_whitelist_trace_t *t =
226 vlib_add_trace (vm, node, b1, sizeof (*t));
227 t->sw_if_index = sw_if_index1;
228 t->next_index = next1;
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);
237 while (n_left_from > 0 && n_left_to_next > 0)
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;
249 ip_adjacency_t * adj0;
251 /* speculatively enqueue b0 to the current next frame */
259 b0 = vlib_get_buffer (vm, bi0);
260 sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_RX];
262 ip0 = vlib_buffer_get_current (b0);
264 ccm0 = cm->cop_config_mains + VNET_COP_IP4;
266 c0 = vnet_get_config_data
268 &vnet_buffer (b0)->cop.current_config_index,
272 mtrie0 = &vec_elt_at_index (im4->fibs, c0->fib_index)->mtrie;
274 leaf0 = IP4_FIB_MTRIE_LEAF_ROOT;
276 leaf0 = ip4_fib_mtrie_lookup_step (mtrie0, leaf0,
277 &ip0->src_address, 0);
279 leaf0 = ip4_fib_mtrie_lookup_step (mtrie0, leaf0,
280 &ip0->src_address, 1);
282 leaf0 = ip4_fib_mtrie_lookup_step (mtrie0, leaf0,
283 &ip0->src_address, 2);
285 leaf0 = ip4_fib_mtrie_lookup_step (mtrie0, leaf0,
286 &ip0->src_address, 3);
288 adj_index0 = ip4_fib_mtrie_leaf_get_adj_index (leaf0);
291 == ip4_fib_lookup_with_table (im4, c0->fib_index,
293 1 /* no_default_route */));
294 adj0 = ip_get_adjacency (lm4, adj_index0);
296 vlib_increment_combined_counter
297 (vcm, cpu_index, adj_index0, 1,
298 vlib_buffer_length_in_chain (vm, b0)
299 + sizeof(ethernet_header_t));
301 if (PREDICT_FALSE(adj0->lookup_next_index != IP_LOOKUP_NEXT_LOCAL))
303 b0->error = node->errors[IP4_COP_WHITELIST_ERROR_DROPPED];
307 if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE)
308 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
310 ip4_cop_whitelist_trace_t *t =
311 vlib_add_trace (vm, node, b0, sizeof (*t));
312 t->sw_if_index = sw_if_index0;
313 t->next_index = next0;
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,
322 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
324 return frame->n_vectors;
327 VLIB_REGISTER_NODE (ip4_cop_whitelist_node) = {
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,
332 .type = VLIB_NODE_TYPE_INTERNAL,
334 .n_errors = ARRAY_LEN(ip4_cop_whitelist_error_strings),
335 .error_strings = ip4_cop_whitelist_error_strings,
337 .n_next_nodes = COP_RX_N_FEATURES,
339 /* edit / add dispositions here */
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",
351 VLIB_NODE_FUNCTION_MULTIARCH (ip4_cop_whitelist_node, ip4_cop_whitelist_node_fn)
353 static clib_error_t *
354 ip4_whitelist_init (vlib_main_t * vm)
359 VLIB_INIT_FUNCTION (ip4_whitelist_init);