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>
17 #include <vnet/pg/pg.h>
24 /* packet trace format function */
25 static u8 * format_adl_input_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 adl_input_trace_t * t = va_arg (*args, adl_input_trace_t *);
31 s = format (s, "ADL_INPUT: sw_if_index %d, next index %d",
32 t->sw_if_index, t->next_index);
36 #define foreach_adl_input_error \
37 _(PROCESSED, "Allow/Deny packets processed")
40 #define _(sym,str) ADL_INPUT_ERROR_##sym,
41 foreach_adl_input_error
46 static char * adl_input_error_strings[] = {
47 #define _(sym,string) string,
48 foreach_adl_input_error
52 VLIB_NODE_FN (adl_input_node) (vlib_main_t * vm,
53 vlib_node_runtime_t * node,
56 u32 n_left_from, * from, * to_next;
57 adl_feature_type_t next_index;
58 adl_main_t *am = &adl_main;
60 from = vlib_frame_vector_args (frame);
61 n_left_from = frame->n_vectors;
62 next_index = node->cached_next_index;
64 while (n_left_from > 0)
68 vlib_get_next_frame (vm, node, next_index,
69 to_next, n_left_to_next);
71 while (n_left_from >= 4 && n_left_to_next >= 2)
74 vlib_buffer_t * b0, * b1;
76 u32 sw_if_index0, sw_if_index1;
77 ethernet_header_t * en0, * en1;
78 adl_config_main_t * ccm0, * ccm1;
79 u32 advance0, advance1;
82 /* Prefetch next iteration. */
84 vlib_buffer_t * p2, * p3;
86 p2 = vlib_get_buffer (vm, from[2]);
87 p3 = vlib_get_buffer (vm, from[3]);
89 vlib_prefetch_buffer_header (p2, LOAD);
90 vlib_prefetch_buffer_header (p3, LOAD);
92 CLIB_PREFETCH (p2->data, CLIB_CACHE_LINE_BYTES, STORE);
93 CLIB_PREFETCH (p3->data, CLIB_CACHE_LINE_BYTES, STORE);
96 /* speculatively enqueue b0 and b1 to the current next frame */
97 to_next[0] = bi0 = from[0];
98 to_next[1] = bi1 = from[1];
104 b0 = vlib_get_buffer (vm, bi0);
105 b1 = vlib_get_buffer (vm, bi1);
107 en0 = vlib_buffer_get_current (b0);
108 en1 = vlib_buffer_get_current (b1);
110 sw_if_index0 = adl_buffer(b0)->sw_if_index[VLIB_RX];
111 sw_if_index1 = adl_buffer(b1)->sw_if_index[VLIB_RX];
113 proto0 = VNET_ADL_DEFAULT;
114 proto1 = VNET_ADL_DEFAULT;
118 if (en0->type == clib_host_to_net_u16(ETHERNET_TYPE_IP4))
120 proto0 = VNET_ADL_IP4;
121 advance0 = sizeof(ethernet_header_t);
123 else if (en0->type == clib_host_to_net_u16(ETHERNET_TYPE_IP6))
125 proto0 = VNET_ADL_IP6;
126 advance0 = sizeof(ethernet_header_t);
129 if (en1->type == clib_host_to_net_u16(ETHERNET_TYPE_IP4))
131 proto1 = VNET_ADL_IP4;
132 advance1 = sizeof(ethernet_header_t);
134 else if (en1->type == clib_host_to_net_u16(ETHERNET_TYPE_IP6))
136 proto1 = VNET_ADL_IP6;
137 advance1 = sizeof(ethernet_header_t);
140 ccm0 = am->adl_config_mains + proto0;
141 ccm1 = am->adl_config_mains + proto1;
142 adl_buffer(b0)->adl.current_config_index =
143 ccm0->config_index_by_sw_if_index [sw_if_index0];
145 adl_buffer(b1)->adl.current_config_index =
146 ccm1->config_index_by_sw_if_index [sw_if_index1];
148 vlib_buffer_advance (b0, advance0);
149 vlib_buffer_advance (b1, advance1);
151 vnet_get_config_data (&ccm0->config_main,
152 &adl_buffer(b0)->adl.current_config_index,
153 &next0, 0 /* bytes of config data */);
155 vnet_get_config_data (&ccm1->config_main,
156 &adl_buffer(b1)->adl.current_config_index,
157 &next1, 0 /* bytes of config data */);
159 if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE)
160 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
162 adl_input_trace_t *t =
163 vlib_add_trace (vm, node, b0, sizeof (*t));
164 t->sw_if_index = sw_if_index0;
165 t->next_index = next0;
168 if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE)
169 && (b1->flags & VLIB_BUFFER_IS_TRACED)))
171 adl_input_trace_t *t =
172 vlib_add_trace (vm, node, b1, sizeof (*t));
173 t->sw_if_index = sw_if_index1;
174 t->next_index = next1;
176 /* verify speculative enqueues, maybe switch current next frame */
177 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
178 to_next, n_left_to_next,
179 bi0, bi1, next0, next1);
182 while (n_left_from > 0 && n_left_to_next > 0)
188 ethernet_header_t *en0;
189 adl_config_main_t *ccm0;
193 /* speculatively enqueue b0 to the current next frame */
201 b0 = vlib_get_buffer (vm, bi0);
204 * Direct from the driver, we should be at offset 0
205 * aka at &b0->data[0]
207 ASSERT (b0->current_data == 0);
209 en0 = vlib_buffer_get_current (b0);
211 sw_if_index0 = adl_buffer(b0)->sw_if_index[VLIB_RX];
213 proto0 = VNET_ADL_DEFAULT;
216 if (en0->type == clib_host_to_net_u16(ETHERNET_TYPE_IP4))
218 proto0 = VNET_ADL_IP4;
219 advance0 = sizeof(ethernet_header_t);
221 else if (en0->type == clib_host_to_net_u16(ETHERNET_TYPE_IP6))
223 proto0 = VNET_ADL_IP6;
224 advance0 = sizeof(ethernet_header_t);
227 ccm0 = am->adl_config_mains + proto0;
228 adl_buffer(b0)->adl.current_config_index =
229 ccm0->config_index_by_sw_if_index [sw_if_index0];
231 vlib_buffer_advance (b0, advance0);
233 vnet_get_config_data (&ccm0->config_main,
234 &adl_buffer(b0)->adl.current_config_index,
235 &next0, 0 /* bytes of config data */);
237 if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE)
238 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
240 adl_input_trace_t *t =
241 vlib_add_trace (vm, node, b0, sizeof (*t));
242 t->sw_if_index = sw_if_index0;
243 t->next_index = next0;
246 /* verify speculative enqueue, maybe switch current next frame */
247 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
248 to_next, n_left_to_next,
252 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
254 vlib_node_increment_counter (vm, adl_input_node.index,
255 ADL_INPUT_ERROR_PROCESSED, frame->n_vectors);
256 return frame->n_vectors;
259 VLIB_REGISTER_NODE (adl_input_node) = {
261 .vector_size = sizeof (u32),
262 .format_trace = format_adl_input_trace,
263 .unformat_buffer = unformat_ethernet_header,
264 .type = VLIB_NODE_TYPE_INTERNAL,
266 .n_errors = ARRAY_LEN(adl_input_error_strings),
267 .error_strings = adl_input_error_strings,
269 .n_next_nodes = ADL_RX_N_FEATURES,
271 /* edit / add dispositions here */
273 [IP4_RX_ADL_ALLOWLIST] = "ip4-adl-allowlist",
274 [IP6_RX_ADL_ALLOWLIST] = "ip6-adl-allowlist",
275 [DEFAULT_RX_ADL_ALLOWLIST] = "default-adl-allowlist",
276 [IP4_RX_ADL_INPUT] = "ip4-input",
277 [IP6_RX_ADL_INPUT] = "ip6-input",
278 [DEFAULT_RX_ADL_INPUT] = "ethernet-input",
279 [RX_ADL_DROP] = "error-drop",
283 #define foreach_adl_stub \
284 _(default-adl-allowlist, default_adl_allowlist)
289 f##_node_fn (vlib_main_t * vm, \
290 vlib_node_runtime_t * node, \
291 vlib_frame_t * frame) \
293 clib_warning ("BUG: stub function called"); \
297 VLIB_REGISTER_NODE (f##_input_node) = { \
298 .function = f##_node_fn, \
300 .vector_size = sizeof (u32), \
301 .type = VLIB_NODE_TYPE_INTERNAL, \
304 .error_strings = 0, \