2 * Copyright (c) 2016,2020 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.
16 #include <plugins/adl/adl.h>
23 /* packet trace format function */
24 static u8 * format_adl_input_trace (u8 * s, va_list * args)
26 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
27 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
28 adl_input_trace_t * t = va_arg (*args, adl_input_trace_t *);
30 s = format (s, "ADL_INPUT: sw_if_index %d, next index %d",
31 t->sw_if_index, t->next_index);
35 #define foreach_adl_input_error \
36 _(PROCESSED, "Allow/Deny packets processed")
39 #define _(sym,str) ADL_INPUT_ERROR_##sym,
40 foreach_adl_input_error
45 static char * adl_input_error_strings[] = {
46 #define _(sym,string) string,
47 foreach_adl_input_error
51 VLIB_NODE_FN (adl_input_node) (vlib_main_t * vm,
52 vlib_node_runtime_t * node,
55 u32 n_left_from, * from, * to_next;
56 adl_feature_type_t next_index;
57 adl_main_t *am = &adl_main;
59 from = vlib_frame_vector_args (frame);
60 n_left_from = frame->n_vectors;
61 next_index = node->cached_next_index;
63 while (n_left_from > 0)
67 vlib_get_next_frame (vm, node, next_index,
68 to_next, n_left_to_next);
70 while (n_left_from >= 4 && n_left_to_next >= 2)
73 vlib_buffer_t * b0, * b1;
75 u32 sw_if_index0, sw_if_index1;
76 ethernet_header_t * en0, * en1;
77 adl_config_main_t * ccm0, * ccm1;
78 u32 advance0, advance1;
81 /* Prefetch next iteration. */
83 vlib_buffer_t * p2, * p3;
85 p2 = vlib_get_buffer (vm, from[2]);
86 p3 = vlib_get_buffer (vm, from[3]);
88 vlib_prefetch_buffer_header (p2, LOAD);
89 vlib_prefetch_buffer_header (p3, LOAD);
91 clib_prefetch_store (p2->data);
92 clib_prefetch_store (p3->data);
95 /* speculatively enqueue b0 and b1 to the current next frame */
96 to_next[0] = bi0 = from[0];
97 to_next[1] = bi1 = from[1];
103 b0 = vlib_get_buffer (vm, bi0);
104 b1 = vlib_get_buffer (vm, bi1);
106 en0 = vlib_buffer_get_current (b0);
107 en1 = vlib_buffer_get_current (b1);
109 sw_if_index0 = adl_buffer(b0)->sw_if_index[VLIB_RX];
110 sw_if_index1 = adl_buffer(b1)->sw_if_index[VLIB_RX];
112 proto0 = VNET_ADL_DEFAULT;
113 proto1 = VNET_ADL_DEFAULT;
117 if (en0->type == clib_host_to_net_u16(ETHERNET_TYPE_IP4))
119 proto0 = VNET_ADL_IP4;
120 advance0 = sizeof(ethernet_header_t);
122 else if (en0->type == clib_host_to_net_u16(ETHERNET_TYPE_IP6))
124 proto0 = VNET_ADL_IP6;
125 advance0 = sizeof(ethernet_header_t);
128 if (en1->type == clib_host_to_net_u16(ETHERNET_TYPE_IP4))
130 proto1 = VNET_ADL_IP4;
131 advance1 = sizeof(ethernet_header_t);
133 else if (en1->type == clib_host_to_net_u16(ETHERNET_TYPE_IP6))
135 proto1 = VNET_ADL_IP6;
136 advance1 = sizeof(ethernet_header_t);
139 ccm0 = am->adl_config_mains + proto0;
140 ccm1 = am->adl_config_mains + proto1;
141 adl_buffer(b0)->adl.current_config_index =
142 ccm0->config_index_by_sw_if_index [sw_if_index0];
144 adl_buffer(b1)->adl.current_config_index =
145 ccm1->config_index_by_sw_if_index [sw_if_index1];
147 vlib_buffer_advance (b0, advance0);
148 vlib_buffer_advance (b1, advance1);
150 vnet_get_config_data (&ccm0->config_main,
151 &adl_buffer(b0)->adl.current_config_index,
152 &next0, 0 /* bytes of config data */);
154 vnet_get_config_data (&ccm1->config_main,
155 &adl_buffer(b1)->adl.current_config_index,
156 &next1, 0 /* bytes of config data */);
158 if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE)
159 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
161 adl_input_trace_t *t =
162 vlib_add_trace (vm, node, b0, sizeof (*t));
163 t->sw_if_index = sw_if_index0;
164 t->next_index = next0;
167 if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE)
168 && (b1->flags & VLIB_BUFFER_IS_TRACED)))
170 adl_input_trace_t *t =
171 vlib_add_trace (vm, node, b1, sizeof (*t));
172 t->sw_if_index = sw_if_index1;
173 t->next_index = next1;
175 /* verify speculative enqueues, maybe switch current next frame */
176 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
177 to_next, n_left_to_next,
178 bi0, bi1, next0, next1);
181 while (n_left_from > 0 && n_left_to_next > 0)
187 ethernet_header_t *en0;
188 adl_config_main_t *ccm0;
192 /* speculatively enqueue b0 to the current next frame */
200 b0 = vlib_get_buffer (vm, bi0);
203 * Direct from the driver, we should be at offset 0
204 * aka at &b0->data[0]
206 ASSERT (b0->current_data == 0);
208 en0 = vlib_buffer_get_current (b0);
210 sw_if_index0 = adl_buffer(b0)->sw_if_index[VLIB_RX];
212 proto0 = VNET_ADL_DEFAULT;
215 if (en0->type == clib_host_to_net_u16(ETHERNET_TYPE_IP4))
217 proto0 = VNET_ADL_IP4;
218 advance0 = sizeof(ethernet_header_t);
220 else if (en0->type == clib_host_to_net_u16(ETHERNET_TYPE_IP6))
222 proto0 = VNET_ADL_IP6;
223 advance0 = sizeof(ethernet_header_t);
226 ccm0 = am->adl_config_mains + proto0;
227 adl_buffer(b0)->adl.current_config_index =
228 ccm0->config_index_by_sw_if_index [sw_if_index0];
230 vlib_buffer_advance (b0, advance0);
232 vnet_get_config_data (&ccm0->config_main,
233 &adl_buffer(b0)->adl.current_config_index,
234 &next0, 0 /* bytes of config data */);
236 if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE)
237 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
239 adl_input_trace_t *t =
240 vlib_add_trace (vm, node, b0, sizeof (*t));
241 t->sw_if_index = sw_if_index0;
242 t->next_index = next0;
245 /* verify speculative enqueue, maybe switch current next frame */
246 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
247 to_next, n_left_to_next,
251 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
253 vlib_node_increment_counter (vm, adl_input_node.index,
254 ADL_INPUT_ERROR_PROCESSED, frame->n_vectors);
255 return frame->n_vectors;
258 VLIB_REGISTER_NODE (adl_input_node) = {
260 .vector_size = sizeof (u32),
261 .format_trace = format_adl_input_trace,
262 .unformat_buffer = unformat_ethernet_header,
263 .type = VLIB_NODE_TYPE_INTERNAL,
265 .n_errors = ARRAY_LEN(adl_input_error_strings),
266 .error_strings = adl_input_error_strings,
268 .n_next_nodes = ADL_RX_N_FEATURES,
270 /* edit / add dispositions here */
272 [IP4_RX_ADL_ALLOWLIST] = "ip4-adl-allowlist",
273 [IP6_RX_ADL_ALLOWLIST] = "ip6-adl-allowlist",
274 [DEFAULT_RX_ADL_ALLOWLIST] = "default-adl-allowlist",
275 [IP4_RX_ADL_INPUT] = "ip4-input",
276 [IP6_RX_ADL_INPUT] = "ip6-input",
277 [DEFAULT_RX_ADL_INPUT] = "ethernet-input",
278 [RX_ADL_DROP] = "error-drop",
282 #define foreach_adl_stub \
283 _(default-adl-allowlist, default_adl_allowlist)
288 f##_node_fn (vlib_main_t * vm, \
289 vlib_node_runtime_t * node, \
290 vlib_frame_t * frame) \
292 clib_warning ("BUG: stub function called"); \
296 VLIB_REGISTER_NODE (f##_input_node) = { \
297 .function = f##_node_fn, \
299 .vector_size = sizeof (u32), \
300 .type = VLIB_NODE_TYPE_INTERNAL, \
303 .error_strings = 0, \