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>
16 #include <vnet/fib/ip6_fib.h>
17 #include <vnet/dpo/load_balance.h>
22 } ip6_cop_whitelist_trace_t;
24 /* packet trace format function */
25 static u8 * format_ip6_cop_whitelist_trace (u8 * s, va_list * args)
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 *);
29 ip6_cop_whitelist_trace_t * t = va_arg (*args, ip6_cop_whitelist_trace_t *);
31 s = format (s, "IP6_COP_WHITELIST: sw_if_index %d, next index %d",
32 t->sw_if_index, t->next_index);
36 #define foreach_ip6_cop_whitelist_error \
37 _(DROPPED, "ip6 cop whitelist packets dropped")
40 #define _(sym,str) IP6_COP_WHITELIST_ERROR_##sym,
41 foreach_ip6_cop_whitelist_error
43 IP6_COP_WHITELIST_N_ERROR,
44 } ip6_cop_whitelist_error_t;
46 static char * ip6_cop_whitelist_error_strings[] = {
47 #define _(sym,string) string,
48 foreach_ip6_cop_whitelist_error
52 VLIB_NODE_FN (ip6_cop_whitelist_node) (vlib_main_t * vm,
53 vlib_node_runtime_t * node,
56 u32 n_left_from, * from, * to_next;
57 cop_feature_type_t next_index;
58 cop_main_t *cm = &cop_main;
59 ip6_main_t * im6 = &ip6_main;
60 vlib_combined_counter_main_t * vcm = &load_balance_main.lbm_via_counters;
61 u32 thread_index = vm->thread_index;
63 from = vlib_frame_vector_args (frame);
64 n_left_from = frame->n_vectors;
65 next_index = node->cached_next_index;
67 while (n_left_from > 0)
71 vlib_get_next_frame (vm, node, next_index,
72 to_next, n_left_to_next);
74 while (n_left_from >= 4 && n_left_to_next >= 2)
77 vlib_buffer_t * b0, * b1;
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;
87 /* Prefetch next iteration. */
89 vlib_buffer_t * p2, * p3;
91 p2 = vlib_get_buffer (vm, from[2]);
92 p3 = vlib_get_buffer (vm, from[3]);
94 vlib_prefetch_buffer_header (p2, LOAD);
95 vlib_prefetch_buffer_header (p3, LOAD);
97 CLIB_PREFETCH (p2->data, CLIB_CACHE_LINE_BYTES, STORE);
98 CLIB_PREFETCH (p3->data, CLIB_CACHE_LINE_BYTES, STORE);
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];
109 b0 = vlib_get_buffer (vm, bi0);
110 sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_RX];
112 ip0 = vlib_buffer_get_current (b0);
114 ccm0 = cm->cop_config_mains + VNET_COP_IP6;
116 c0 = vnet_get_config_data
118 &vnet_buffer (b0)->cop.current_config_index,
122 lb_index0 = ip6_fib_table_fwding_lookup (im6, c0->fib_index,
124 lb0 = load_balance_get (lb_index0);
125 dpo0 = load_balance_get_bucket_i(lb0, 0);
127 if (PREDICT_FALSE(dpo0->dpoi_type != DPO_RECEIVE))
129 b0->error = node->errors[IP6_COP_WHITELIST_ERROR_DROPPED];
133 b1 = vlib_get_buffer (vm, bi1);
134 sw_if_index1 = vnet_buffer(b1)->sw_if_index[VLIB_RX];
136 ip1 = vlib_buffer_get_current (b1);
138 ccm1 = cm->cop_config_mains + VNET_COP_IP6;
140 c1 = vnet_get_config_data
142 &vnet_buffer (b1)->cop.current_config_index,
146 lb_index1 = ip6_fib_table_fwding_lookup (im6, c1->fib_index,
149 lb1 = load_balance_get (lb_index1);
150 dpo1 = load_balance_get_bucket_i(lb1, 0);
152 vlib_increment_combined_counter
153 (vcm, thread_index, lb_index0, 1,
154 vlib_buffer_length_in_chain (vm, b0)
155 + sizeof(ethernet_header_t));
157 vlib_increment_combined_counter
158 (vcm, thread_index, lb_index1, 1,
159 vlib_buffer_length_in_chain (vm, b1)
160 + sizeof(ethernet_header_t));
162 if (PREDICT_FALSE(dpo1->dpoi_type != DPO_RECEIVE))
164 b1->error = node->errors[IP6_COP_WHITELIST_ERROR_DROPPED];
168 if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE)
169 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
171 ip6_cop_whitelist_trace_t *t =
172 vlib_add_trace (vm, node, b0, sizeof (*t));
173 t->sw_if_index = sw_if_index0;
174 t->next_index = next0;
177 if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE)
178 && (b1->flags & VLIB_BUFFER_IS_TRACED)))
180 ip6_cop_whitelist_trace_t *t =
181 vlib_add_trace (vm, node, b1, sizeof (*t));
182 t->sw_if_index = sw_if_index1;
183 t->next_index = next1;
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);
192 while (n_left_from > 0 && n_left_to_next > 0)
199 cop_config_main_t *ccm0;
200 cop_config_data_t *c0;
202 const load_balance_t * lb0;
203 const dpo_id_t *dpo0;
205 /* speculatively enqueue b0 to the current next frame */
213 b0 = vlib_get_buffer (vm, bi0);
214 sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_RX];
216 ip0 = vlib_buffer_get_current (b0);
218 ccm0 = cm->cop_config_mains + VNET_COP_IP6;
220 c0 = vnet_get_config_data
222 &vnet_buffer (b0)->cop.current_config_index,
226 lb_index0 = ip6_fib_table_fwding_lookup (im6, c0->fib_index,
229 lb0 = load_balance_get (lb_index0);
230 dpo0 = load_balance_get_bucket_i(lb0, 0);
232 vlib_increment_combined_counter
233 (vcm, thread_index, lb_index0, 1,
234 vlib_buffer_length_in_chain (vm, b0)
235 + sizeof(ethernet_header_t));
237 if (PREDICT_FALSE(dpo0->dpoi_type != DPO_RECEIVE))
239 b0->error = node->errors[IP6_COP_WHITELIST_ERROR_DROPPED];
243 if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE)
244 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
246 ip6_cop_whitelist_trace_t *t =
247 vlib_add_trace (vm, node, b0, sizeof (*t));
248 t->sw_if_index = sw_if_index0;
249 t->next_index = next0;
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,
258 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
260 return frame->n_vectors;
263 VLIB_REGISTER_NODE (ip6_cop_whitelist_node) = {
264 .name = "ip6-cop-whitelist",
265 .vector_size = sizeof (u32),
266 .format_trace = format_ip6_cop_whitelist_trace,
267 .type = VLIB_NODE_TYPE_INTERNAL,
269 .n_errors = ARRAY_LEN(ip6_cop_whitelist_error_strings),
270 .error_strings = ip6_cop_whitelist_error_strings,
272 .n_next_nodes = COP_RX_N_FEATURES,
274 /* edit / add dispositions here */
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",
286 static clib_error_t *
287 ip6_whitelist_init (vlib_main_t * vm)
292 VLIB_INIT_FUNCTION (ip6_whitelist_init);