ip: optimize ip4_header_checksum, take 2
[vpp.git] / src / vnet / cop / ip6_whitelist.c
1 /*
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:
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 <vnet/cop/cop.h>
16 #include <vnet/fib/ip6_fib.h>
17 #include <vnet/dpo/load_balance.h>
18
19 typedef struct {
20   u32 next_index;
21   u32 sw_if_index;
22 } ip6_cop_whitelist_trace_t;
23
24 /* packet trace format function */
25 static u8 * format_ip6_cop_whitelist_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   ip6_cop_whitelist_trace_t * t = va_arg (*args, ip6_cop_whitelist_trace_t *);
30   
31   s = format (s, "IP6_COP_WHITELIST: sw_if_index %d, next index %d",
32               t->sw_if_index, t->next_index);
33   return s;
34 }
35
36 #define foreach_ip6_cop_whitelist_error                         \
37 _(DROPPED, "ip6 cop whitelist packets dropped")
38
39 typedef enum {
40 #define _(sym,str) IP6_COP_WHITELIST_ERROR_##sym,
41   foreach_ip6_cop_whitelist_error
42 #undef _
43   IP6_COP_WHITELIST_N_ERROR,
44 } ip6_cop_whitelist_error_t;
45
46 static char * ip6_cop_whitelist_error_strings[] = {
47 #define _(sym,string) string,
48   foreach_ip6_cop_whitelist_error
49 #undef _
50 };
51
52 VLIB_NODE_FN (ip6_cop_whitelist_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   cop_feature_type_t next_index;
58   cop_main_t *cm = &cop_main;
59   vlib_combined_counter_main_t * vcm = &load_balance_main.lbm_via_counters;
60   u32 thread_index = vm->thread_index;
61
62   from = vlib_frame_vector_args (frame);
63   n_left_from = frame->n_vectors;
64   next_index = node->cached_next_index;
65
66   while (n_left_from > 0)
67     {
68       u32 n_left_to_next;
69
70       vlib_get_next_frame (vm, node, next_index,
71                            to_next, n_left_to_next);
72
73       while (n_left_from >= 4 && n_left_to_next >= 2)
74         {
75           u32 bi0, bi1;
76           vlib_buffer_t * b0, * b1;
77           u32 next0, next1;
78           u32 sw_if_index0, sw_if_index1;
79           ip6_header_t * ip0, * ip1;
80           cop_config_main_t * ccm0, * ccm1;
81           cop_config_data_t * c0, * c1;
82           u32 lb_index0, lb_index1;
83           const load_balance_t * lb0, *lb1;
84           const dpo_id_t *dpo0, *dpo1;
85          
86           /* Prefetch next iteration. */
87           {
88             vlib_buffer_t * p2, * p3;
89             
90             p2 = vlib_get_buffer (vm, from[2]);
91             p3 = vlib_get_buffer (vm, from[3]);
92             
93             vlib_prefetch_buffer_header (p2, LOAD);
94             vlib_prefetch_buffer_header (p3, LOAD);
95
96             CLIB_PREFETCH (p2->data, CLIB_CACHE_LINE_BYTES, STORE);
97             CLIB_PREFETCH (p3->data, CLIB_CACHE_LINE_BYTES, STORE);
98           }
99
100           /* speculatively enqueue b0 and b1 to the current next frame */
101           to_next[0] = bi0 = from[0];
102           to_next[1] = bi1 = from[1];
103           from += 2;
104           to_next += 2;
105           n_left_from -= 2;
106           n_left_to_next -= 2;
107
108           b0 = vlib_get_buffer (vm, bi0);
109           sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_RX];
110
111           ip0 = vlib_buffer_get_current (b0);
112
113           ccm0 = cm->cop_config_mains + VNET_COP_IP6;
114
115           c0 = vnet_get_config_data 
116               (&ccm0->config_main,
117                &vnet_buffer (b0)->cop.current_config_index,
118                &next0,
119                sizeof (c0[0]));
120
121           lb_index0 = ip6_fib_table_fwding_lookup (c0->fib_index, 
122                                                     &ip0->src_address);
123           lb0 = load_balance_get (lb_index0);
124           dpo0 = load_balance_get_bucket_i(lb0, 0);
125
126           if (PREDICT_FALSE(dpo0->dpoi_type != DPO_RECEIVE))
127             {
128               b0->error = node->errors[IP6_COP_WHITELIST_ERROR_DROPPED];
129               next0 = RX_COP_DROP;
130             }
131
132           b1 = vlib_get_buffer (vm, bi1);
133           sw_if_index1 = vnet_buffer(b1)->sw_if_index[VLIB_RX];
134
135           ip1 = vlib_buffer_get_current (b1);
136
137           ccm1 = cm->cop_config_mains + VNET_COP_IP6;
138
139           c1 = vnet_get_config_data 
140               (&ccm1->config_main,
141                &vnet_buffer (b1)->cop.current_config_index,
142                &next1,
143                sizeof (c1[0]));
144
145           lb_index1 = ip6_fib_table_fwding_lookup (c1->fib_index, 
146                                                     &ip1->src_address);
147
148           lb1 = load_balance_get (lb_index1);
149           dpo1 = load_balance_get_bucket_i(lb1, 0);
150
151           vlib_increment_combined_counter 
152               (vcm, thread_index, lb_index0, 1,
153                vlib_buffer_length_in_chain (vm, b0) 
154                + sizeof(ethernet_header_t));
155
156           vlib_increment_combined_counter 
157               (vcm, thread_index, lb_index1, 1,
158                vlib_buffer_length_in_chain (vm, b1)
159                + sizeof(ethernet_header_t));
160
161           if (PREDICT_FALSE(dpo1->dpoi_type != DPO_RECEIVE))
162             {
163               b1->error = node->errors[IP6_COP_WHITELIST_ERROR_DROPPED];
164               next1 = RX_COP_DROP;
165             }
166
167           if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE) 
168                             && (b0->flags & VLIB_BUFFER_IS_TRACED))) 
169             {
170               ip6_cop_whitelist_trace_t *t = 
171                  vlib_add_trace (vm, node, b0, sizeof (*t));
172               t->sw_if_index = sw_if_index0;
173               t->next_index = next0;
174             }
175
176           if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE) 
177                             && (b1->flags & VLIB_BUFFER_IS_TRACED))) 
178             {
179               ip6_cop_whitelist_trace_t *t = 
180                  vlib_add_trace (vm, node, b1, sizeof (*t));
181               t->sw_if_index = sw_if_index1;
182               t->next_index = next1;
183             }
184
185           /* verify speculative enqueues, maybe switch current next frame */
186           vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
187                                            to_next, n_left_to_next,
188                                            bi0, bi1, next0, next1);
189         }
190
191       while (n_left_from > 0 && n_left_to_next > 0)
192         {
193           u32 bi0;
194           vlib_buffer_t * b0;
195           u32 next0;
196           u32 sw_if_index0;
197           ip6_header_t * ip0;
198           cop_config_main_t *ccm0;
199           cop_config_data_t *c0;
200           u32 lb_index0;
201           const load_balance_t * lb0;
202           const dpo_id_t *dpo0;
203
204           /* speculatively enqueue b0 to the current next frame */
205           bi0 = from[0];
206           to_next[0] = bi0;
207           from += 1;
208           to_next += 1;
209           n_left_from -= 1;
210           n_left_to_next -= 1;
211
212           b0 = vlib_get_buffer (vm, bi0);
213           sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_RX];
214
215           ip0 = vlib_buffer_get_current (b0);
216
217           ccm0 = cm->cop_config_mains + VNET_COP_IP6;
218
219           c0 = vnet_get_config_data 
220               (&ccm0->config_main,
221                &vnet_buffer (b0)->cop.current_config_index,
222                &next0,
223                sizeof (c0[0]));
224
225           lb_index0 = ip6_fib_table_fwding_lookup (c0->fib_index, 
226                                                     &ip0->src_address);
227
228           lb0 = load_balance_get (lb_index0);
229           dpo0 = load_balance_get_bucket_i(lb0, 0);
230
231           vlib_increment_combined_counter 
232               (vcm, thread_index, lb_index0, 1,
233                vlib_buffer_length_in_chain (vm, b0) 
234                + sizeof(ethernet_header_t));
235
236           if (PREDICT_FALSE(dpo0->dpoi_type != DPO_RECEIVE))
237             {
238               b0->error = node->errors[IP6_COP_WHITELIST_ERROR_DROPPED];
239               next0 = RX_COP_DROP;
240             }
241
242           if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE) 
243                             && (b0->flags & VLIB_BUFFER_IS_TRACED))) 
244             {
245               ip6_cop_whitelist_trace_t *t = 
246                  vlib_add_trace (vm, node, b0, sizeof (*t));
247               t->sw_if_index = sw_if_index0;
248               t->next_index = next0;
249             }
250             
251           /* verify speculative enqueue, maybe switch current next frame */
252           vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
253                                            to_next, n_left_to_next,
254                                            bi0, next0);
255         }
256
257       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
258     }
259   return frame->n_vectors;
260 }
261
262 VLIB_REGISTER_NODE (ip6_cop_whitelist_node) = {
263   .name = "ip6-cop-whitelist",
264   .vector_size = sizeof (u32),
265   .format_trace = format_ip6_cop_whitelist_trace,
266   .type = VLIB_NODE_TYPE_INTERNAL,
267   
268   .n_errors = ARRAY_LEN(ip6_cop_whitelist_error_strings),
269   .error_strings = ip6_cop_whitelist_error_strings,
270
271   .n_next_nodes = COP_RX_N_FEATURES,
272
273   /* edit / add dispositions here */
274   .next_nodes = {
275     [IP4_RX_COP_WHITELIST] = "ip4-cop-whitelist",
276     [IP6_RX_COP_WHITELIST] = "ip6-cop-whitelist",
277     [DEFAULT_RX_COP_WHITELIST] = "default-cop-whitelist",
278     [IP4_RX_COP_INPUT] = "ip4-input",
279     [IP6_RX_COP_INPUT] = "ip6-input",
280     [DEFAULT_RX_COP_INPUT] = "ethernet-input",
281     [RX_COP_DROP] = "error-drop",
282   },
283 };
284
285 static clib_error_t *
286 ip6_whitelist_init (vlib_main_t * vm)
287 {
288   return 0;
289 }
290
291 VLIB_INIT_FUNCTION (ip6_whitelist_init);