misc: replace CLIB_PREFETCH with clib_prefetch_{load,store}
[vpp.git] / src / plugins / adl / ip4_allowlist.c
1 /*
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:
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 <plugins/adl/adl.h>
16 #include <vnet/fib/ip4_fib.h>
17 #include <vnet/dpo/load_balance.h>
18
19 typedef struct {
20   u32 next_index;
21   u32 sw_if_index;
22 } ip4_adl_allowlist_trace_t;
23
24 /* packet trace format function */
25 static u8 * format_ip4_adl_allowlist_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 *);
29   ip4_adl_allowlist_trace_t * t = va_arg (*args, ip4_adl_allowlist_trace_t *);
30
31   s = format (s, "IP4_ADL_ALLOWLIST: sw_if_index %d, next index %d",
32               t->sw_if_index, t->next_index);
33   return s;
34 }
35
36 #define foreach_ip4_adl_allowlist_error         \
37 _(ALLOWED, "ip4 allowlist allowed")             \
38 _(DROPPED, "ip4 allowlist dropped")
39
40 typedef enum {
41 #define _(sym,str) IP4_ADL_ALLOWLIST_ERROR_##sym,
42   foreach_ip4_adl_allowlist_error
43 #undef _
44   IP4_ADL_ALLOWLIST_N_ERROR,
45 } ip4_adl_allowlist_error_t;
46
47 static char * ip4_adl_allowlist_error_strings[] = {
48 #define _(sym,string) string,
49   foreach_ip4_adl_allowlist_error
50 #undef _
51 };
52
53 VLIB_NODE_FN (ip4_adl_allowlist_node) (vlib_main_t * vm,
54                   vlib_node_runtime_t * node,
55                   vlib_frame_t * frame)
56 {
57   u32 n_left_from, * from, * to_next;
58   adl_feature_type_t next_index;
59   adl_main_t *cm = &adl_main;
60   vlib_combined_counter_main_t * vcm = &load_balance_main.lbm_via_counters;
61   u32 thread_index = vm->thread_index;
62   u32 allowed_packets;
63
64   from = vlib_frame_vector_args (frame);
65   n_left_from = frame->n_vectors;
66   allowed_packets = n_left_from;
67   next_index = node->cached_next_index;
68
69   while (n_left_from > 0)
70     {
71       u32 n_left_to_next;
72
73       vlib_get_next_frame (vm, node, next_index,
74                            to_next, n_left_to_next);
75
76       while (n_left_from >= 4 && n_left_to_next >= 2)
77         {
78           u32 bi0, bi1;
79           vlib_buffer_t * b0, * b1;
80           u32 next0, next1;
81           u32 sw_if_index0, sw_if_index1;
82           ip4_header_t * ip0, * ip1;
83           adl_config_main_t * ccm0, * ccm1;
84           adl_config_data_t * c0, * c1;
85           ip4_fib_mtrie_t * mtrie0, * mtrie1;
86           ip4_fib_mtrie_leaf_t leaf0, leaf1;
87           u32 lb_index0, lb_index1;
88           const load_balance_t * lb0, *lb1;
89           const dpo_id_t *dpo0, *dpo1;
90
91           /* Prefetch next iteration. */
92           {
93             vlib_buffer_t * p2, * p3;
94
95             p2 = vlib_get_buffer (vm, from[2]);
96             p3 = vlib_get_buffer (vm, from[3]);
97
98             vlib_prefetch_buffer_header (p2, LOAD);
99             vlib_prefetch_buffer_header (p3, LOAD);
100
101             clib_prefetch_store (p2->data);
102             clib_prefetch_store (p3->data);
103           }
104
105           /* speculatively enqueue b0 and b1 to the current next frame */
106           to_next[0] = bi0 = from[0];
107           to_next[1] = bi1 = from[1];
108           from += 2;
109           to_next += 2;
110           n_left_from -= 2;
111           n_left_to_next -= 2;
112
113           b0 = vlib_get_buffer (vm, bi0);
114           sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
115
116           ip0 = vlib_buffer_get_current (b0);
117
118           ccm0 = cm->adl_config_mains + VNET_ADL_IP4;
119
120           c0 = vnet_get_config_data (
121             &ccm0->config_main, &adl_buffer (b0)->adl.current_config_index,
122             &next0, sizeof (c0[0]));
123
124           mtrie0 = &ip4_fib_get (c0->fib_index)->mtrie;
125
126           leaf0 = ip4_fib_mtrie_lookup_step_one (mtrie0, &ip0->src_address);
127
128           leaf0 = ip4_fib_mtrie_lookup_step (mtrie0, leaf0,
129                                              &ip0->src_address, 2);
130
131           leaf0 = ip4_fib_mtrie_lookup_step (mtrie0, leaf0,
132                                              &ip0->src_address, 3);
133
134           lb_index0 = ip4_fib_mtrie_leaf_get_adj_index (leaf0);
135
136           ASSERT (lb_index0
137                   == ip4_fib_table_lookup_lb (ip4_fib_get(c0->fib_index),
138                                                &ip0->src_address));
139           lb0 = load_balance_get (lb_index0);
140           dpo0 = load_balance_get_bucket_i(lb0, 0);
141
142           if (PREDICT_FALSE(dpo0->dpoi_type != DPO_RECEIVE))
143             {
144               b0->error = node->errors[IP4_ADL_ALLOWLIST_ERROR_DROPPED];
145               allowed_packets--;
146               next0 = RX_ADL_DROP;
147             }
148
149           b1 = vlib_get_buffer (vm, bi1);
150           sw_if_index1 = vnet_buffer(b1)->sw_if_index[VLIB_RX];
151
152           ip1 = vlib_buffer_get_current (b1);
153
154           ccm1 = cm->adl_config_mains + VNET_ADL_IP4;
155
156           c1 = vnet_get_config_data
157               (&ccm1->config_main,
158                &adl_buffer (b1)->adl.current_config_index,
159                &next1,
160                sizeof (c1[0]));
161           mtrie1 = &ip4_fib_get (c1->fib_index)->mtrie;
162
163           leaf1 = ip4_fib_mtrie_lookup_step_one (mtrie1, &ip1->src_address);
164
165           leaf1 = ip4_fib_mtrie_lookup_step (mtrie1, leaf1,
166                                              &ip1->src_address, 2);
167
168           leaf1 = ip4_fib_mtrie_lookup_step (mtrie1, leaf1,
169                                              &ip1->src_address, 3);
170
171           lb_index1 = ip4_fib_mtrie_leaf_get_adj_index (leaf1);
172           ASSERT (lb_index1
173                   == ip4_fib_table_lookup_lb (ip4_fib_get(c1->fib_index),
174                                                &ip1->src_address));
175           lb1 = load_balance_get (lb_index1);
176           dpo1 = load_balance_get_bucket_i(lb1, 0);
177
178           vlib_increment_combined_counter
179               (vcm, thread_index, lb_index0, 1,
180                vlib_buffer_length_in_chain (vm, b0)
181                + sizeof(ethernet_header_t));
182
183           vlib_increment_combined_counter
184               (vcm, thread_index, lb_index1, 1,
185                vlib_buffer_length_in_chain (vm, b1)
186                + sizeof(ethernet_header_t));
187
188
189           if (PREDICT_FALSE(dpo1->dpoi_type != DPO_RECEIVE))
190             {
191               b1->error = node->errors[IP4_ADL_ALLOWLIST_ERROR_DROPPED];
192               allowed_packets--;
193               next1 = RX_ADL_DROP;
194             }
195
196           if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE)
197                             && (b0->flags & VLIB_BUFFER_IS_TRACED)))
198             {
199               ip4_adl_allowlist_trace_t *t =
200                  vlib_add_trace (vm, node, b0, sizeof (*t));
201               t->sw_if_index = sw_if_index0;
202               t->next_index = next0;
203             }
204
205           if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE)
206                             && (b1->flags & VLIB_BUFFER_IS_TRACED)))
207             {
208               ip4_adl_allowlist_trace_t *t =
209                  vlib_add_trace (vm, node, b1, sizeof (*t));
210               t->sw_if_index = sw_if_index1;
211               t->next_index = next1;
212             }
213
214           /* verify speculative enqueues, maybe switch current next frame */
215           vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
216                                            to_next, n_left_to_next,
217                                            bi0, bi1, next0, next1);
218         }
219
220       while (n_left_from > 0 && n_left_to_next > 0)
221         {
222           u32 bi0;
223           vlib_buffer_t * b0;
224           u32 next0;
225           u32 sw_if_index0;
226           ip4_header_t * ip0;
227           adl_config_main_t *ccm0;
228           adl_config_data_t *c0;
229           ip4_fib_mtrie_t * mtrie0;
230           ip4_fib_mtrie_leaf_t leaf0;
231           u32 lb_index0;
232           const load_balance_t * lb0;
233           const dpo_id_t *dpo0;
234
235           /* speculatively enqueue b0 to the current next frame */
236           bi0 = from[0];
237           to_next[0] = bi0;
238           from += 1;
239           to_next += 1;
240           n_left_from -= 1;
241           n_left_to_next -= 1;
242
243           b0 = vlib_get_buffer (vm, bi0);
244           sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_RX];
245
246           ip0 = vlib_buffer_get_current (b0);
247
248           ccm0 = cm->adl_config_mains + VNET_ADL_IP4;
249
250           c0 = vnet_get_config_data
251               (&ccm0->config_main,
252                &adl_buffer (b0)->adl.current_config_index,
253                &next0,
254                sizeof (c0[0]));
255
256           mtrie0 = &ip4_fib_get (c0->fib_index)->mtrie;
257
258           leaf0 = ip4_fib_mtrie_lookup_step_one (mtrie0, &ip0->src_address);
259
260           leaf0 = ip4_fib_mtrie_lookup_step (mtrie0, leaf0,
261                                              &ip0->src_address, 2);
262
263           leaf0 = ip4_fib_mtrie_lookup_step (mtrie0, leaf0,
264                                              &ip0->src_address, 3);
265
266           lb_index0 = ip4_fib_mtrie_leaf_get_adj_index (leaf0);
267
268           ASSERT (lb_index0
269                   == ip4_fib_table_lookup_lb (ip4_fib_get(c0->fib_index),
270                                               &ip0->src_address));
271
272           lb0 = load_balance_get (lb_index0);
273           dpo0 = load_balance_get_bucket_i(lb0, 0);
274
275           vlib_increment_combined_counter
276               (vcm, thread_index, lb_index0, 1,
277                vlib_buffer_length_in_chain (vm, b0)
278                + sizeof(ethernet_header_t));
279
280           if (PREDICT_FALSE(dpo0->dpoi_type != DPO_RECEIVE))
281             {
282               b0->error = node->errors[IP4_ADL_ALLOWLIST_ERROR_DROPPED];
283               allowed_packets--;
284               next0 = RX_ADL_DROP;
285             }
286
287           if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE)
288                             && (b0->flags & VLIB_BUFFER_IS_TRACED)))
289             {
290               ip4_adl_allowlist_trace_t *t =
291                  vlib_add_trace (vm, node, b0, sizeof (*t));
292               t->sw_if_index = sw_if_index0;
293               t->next_index = next0;
294             }
295
296           /* verify speculative enqueue, maybe switch current next frame */
297           vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
298                                            to_next, n_left_to_next,
299                                            bi0, next0);
300         }
301
302       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
303     }
304
305   vlib_node_increment_counter (vm, node->node_index,
306                                IP4_ADL_ALLOWLIST_ERROR_ALLOWED,
307                                allowed_packets);
308
309   return frame->n_vectors;
310 }
311
312 VLIB_REGISTER_NODE (ip4_adl_allowlist_node) = {
313   .name = "ip4-adl-allowlist",
314   .vector_size = sizeof (u32),
315   .format_trace = format_ip4_adl_allowlist_trace,
316   .type = VLIB_NODE_TYPE_INTERNAL,
317
318   .n_errors = ARRAY_LEN(ip4_adl_allowlist_error_strings),
319   .error_strings = ip4_adl_allowlist_error_strings,
320
321   .n_next_nodes = ADL_RX_N_FEATURES,
322
323   /* edit / add dispositions here */
324   .next_nodes = {
325     [IP4_RX_ADL_ALLOWLIST] = "ip4-adl-allowlist",
326     [IP6_RX_ADL_ALLOWLIST] = "ip6-adl-allowlist",
327     [DEFAULT_RX_ADL_ALLOWLIST] = "default-adl-allowlist",
328     [IP4_RX_ADL_INPUT] = "ip4-input",
329     [IP6_RX_ADL_INPUT] = "ip6-input",
330     [DEFAULT_RX_ADL_INPUT] = "ethernet-input",
331     [RX_ADL_DROP] = "error-drop",
332   },
333 };
334
335 static clib_error_t *
336 ip4_allowlist_init (vlib_main_t * vm)
337 {
338   return 0;
339 }
340
341 VLIB_INIT_FUNCTION (ip4_allowlist_init);