ip: optimize ip4_header_checksum, take 2
[vpp.git] / src / plugins / adl / node.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
16 #include <plugins/adl/adl.h>
17 #include <vnet/pg/pg.h>
18
19 typedef struct {
20   u32 next_index;
21   u32 sw_if_index;
22 } adl_input_trace_t;
23
24 /* packet trace format function */
25 static u8 * format_adl_input_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   adl_input_trace_t * t = va_arg (*args, adl_input_trace_t *);
30
31   s = format (s, "ADL_INPUT: sw_if_index %d, next index %d",
32               t->sw_if_index, t->next_index);
33   return s;
34 }
35
36 #define foreach_adl_input_error \
37 _(PROCESSED, "Allow/Deny packets processed")
38
39 typedef enum {
40 #define _(sym,str) ADL_INPUT_ERROR_##sym,
41   foreach_adl_input_error
42 #undef _
43   ADL_INPUT_N_ERROR,
44 } adl_input_error_t;
45
46 static char * adl_input_error_strings[] = {
47 #define _(sym,string) string,
48   foreach_adl_input_error
49 #undef _
50 };
51
52 VLIB_NODE_FN (adl_input_node) (vlib_main_t * vm,
53                   vlib_node_runtime_t * node,
54                   vlib_frame_t * frame)
55 {
56   u32 n_left_from, * from, * to_next;
57   adl_feature_type_t next_index;
58   adl_main_t *am = &adl_main;
59
60   from = vlib_frame_vector_args (frame);
61   n_left_from = frame->n_vectors;
62   next_index = node->cached_next_index;
63
64   while (n_left_from > 0)
65     {
66       u32 n_left_to_next;
67
68       vlib_get_next_frame (vm, node, next_index,
69                            to_next, n_left_to_next);
70
71       while (n_left_from >= 4 && n_left_to_next >= 2)
72         {
73           u32 bi0, bi1;
74           vlib_buffer_t * b0, * b1;
75           u32 next0, next1;
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;
80           int proto0, proto1;
81
82           /* Prefetch next iteration. */
83           {
84             vlib_buffer_t * p2, * p3;
85
86             p2 = vlib_get_buffer (vm, from[2]);
87             p3 = vlib_get_buffer (vm, from[3]);
88
89             vlib_prefetch_buffer_header (p2, LOAD);
90             vlib_prefetch_buffer_header (p3, LOAD);
91
92             CLIB_PREFETCH (p2->data, CLIB_CACHE_LINE_BYTES, STORE);
93             CLIB_PREFETCH (p3->data, CLIB_CACHE_LINE_BYTES, STORE);
94           }
95
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];
99           from += 2;
100           to_next += 2;
101           n_left_from -= 2;
102           n_left_to_next -= 2;
103
104           b0 = vlib_get_buffer (vm, bi0);
105           b1 = vlib_get_buffer (vm, bi1);
106
107           en0 = vlib_buffer_get_current (b0);
108           en1 = vlib_buffer_get_current (b1);
109
110           sw_if_index0 = adl_buffer(b0)->sw_if_index[VLIB_RX];
111           sw_if_index1 = adl_buffer(b1)->sw_if_index[VLIB_RX];
112
113           proto0 = VNET_ADL_DEFAULT;
114           proto1 = VNET_ADL_DEFAULT;
115           advance0 = 0;
116           advance1 = 0;
117
118           if (en0->type == clib_host_to_net_u16(ETHERNET_TYPE_IP4))
119             {
120               proto0 = VNET_ADL_IP4;
121               advance0 = sizeof(ethernet_header_t);
122             }
123           else if (en0->type == clib_host_to_net_u16(ETHERNET_TYPE_IP6))
124             {
125               proto0 = VNET_ADL_IP6;
126               advance0 = sizeof(ethernet_header_t);
127             }
128
129           if (en1->type == clib_host_to_net_u16(ETHERNET_TYPE_IP4))
130             {
131               proto1 = VNET_ADL_IP4;
132               advance1 = sizeof(ethernet_header_t);
133             }
134           else if (en1->type == clib_host_to_net_u16(ETHERNET_TYPE_IP6))
135             {
136               proto1 = VNET_ADL_IP6;
137               advance1 = sizeof(ethernet_header_t);
138             }
139
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];
144
145           adl_buffer(b1)->adl.current_config_index =
146             ccm1->config_index_by_sw_if_index [sw_if_index1];
147
148           vlib_buffer_advance (b0, advance0);
149           vlib_buffer_advance (b1, advance1);
150
151           vnet_get_config_data (&ccm0->config_main,
152                                 &adl_buffer(b0)->adl.current_config_index,
153                                 &next0, 0 /* bytes of config data */);
154
155           vnet_get_config_data (&ccm1->config_main,
156                                 &adl_buffer(b1)->adl.current_config_index,
157                                 &next1, 0 /* bytes of config data */);
158
159           if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE)
160                             && (b0->flags & VLIB_BUFFER_IS_TRACED)))
161             {
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;
166             }
167
168           if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE)
169                             && (b1->flags & VLIB_BUFFER_IS_TRACED)))
170             {
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;
175             }
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);
180         }
181
182       while (n_left_from > 0 && n_left_to_next > 0)
183         {
184           u32 bi0;
185           vlib_buffer_t * b0;
186           u32 next0;
187           u32 sw_if_index0;
188           ethernet_header_t *en0;
189           adl_config_main_t *ccm0;
190           u32 advance0;
191           int proto0;
192
193           /* speculatively enqueue b0 to the current next frame */
194           bi0 = from[0];
195           to_next[0] = bi0;
196           from += 1;
197           to_next += 1;
198           n_left_from -= 1;
199           n_left_to_next -= 1;
200
201           b0 = vlib_get_buffer (vm, bi0);
202
203           /*
204            * Direct from the driver, we should be at offset 0
205            * aka at &b0->data[0]
206            */
207           ASSERT (b0->current_data == 0);
208
209           en0 = vlib_buffer_get_current (b0);
210
211           sw_if_index0 = adl_buffer(b0)->sw_if_index[VLIB_RX];
212
213           proto0 = VNET_ADL_DEFAULT;
214           advance0 = 0;
215
216           if (en0->type == clib_host_to_net_u16(ETHERNET_TYPE_IP4))
217             {
218               proto0 = VNET_ADL_IP4;
219               advance0 = sizeof(ethernet_header_t);
220             }
221           else if (en0->type == clib_host_to_net_u16(ETHERNET_TYPE_IP6))
222             {
223               proto0 = VNET_ADL_IP6;
224               advance0 = sizeof(ethernet_header_t);
225             }
226
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];
230
231           vlib_buffer_advance (b0, advance0);
232
233           vnet_get_config_data (&ccm0->config_main,
234                                 &adl_buffer(b0)->adl.current_config_index,
235                                 &next0, 0 /* bytes of config data */);
236
237           if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE)
238                             && (b0->flags & VLIB_BUFFER_IS_TRACED)))
239             {
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;
244             }
245
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,
249                                            bi0, next0);
250         }
251
252       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
253     }
254   vlib_node_increment_counter (vm, adl_input_node.index,
255                                ADL_INPUT_ERROR_PROCESSED, frame->n_vectors);
256   return frame->n_vectors;
257 }
258
259 VLIB_REGISTER_NODE (adl_input_node) = {
260   .name = "adl-input",
261   .vector_size = sizeof (u32),
262   .format_trace = format_adl_input_trace,
263   .unformat_buffer = unformat_ethernet_header,
264   .type = VLIB_NODE_TYPE_INTERNAL,
265
266   .n_errors = ARRAY_LEN(adl_input_error_strings),
267   .error_strings = adl_input_error_strings,
268
269   .n_next_nodes = ADL_RX_N_FEATURES,
270
271   /* edit / add dispositions here */
272   .next_nodes = {
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",
280   },
281 };
282
283 #define foreach_adl_stub                        \
284 _(default-adl-allowlist, default_adl_allowlist)
285
286 #define _(n,f)                                  \
287                                                 \
288 static uword                                    \
289 f##_node_fn (vlib_main_t * vm,                  \
290              vlib_node_runtime_t * node,        \
291              vlib_frame_t * frame)              \
292 {                                               \
293   clib_warning ("BUG: stub function called");   \
294   return 0;                                     \
295 }                                               \
296                                                 \
297 VLIB_REGISTER_NODE (f##_input_node) = {         \
298   .function = f##_node_fn,                      \
299   .name = #n,                                   \
300   .vector_size = sizeof (u32),                  \
301   .type = VLIB_NODE_TYPE_INTERNAL,              \
302                                                 \
303   .n_errors = 0,                                \
304   .error_strings = 0,                           \
305                                                 \
306   .n_next_nodes = 0,                            \
307 };
308
309 foreach_adl_stub;