Initial commit of vpp code.
[vpp.git] / vnet / vnet / l2 / l2_input_acl.c
1 /*
2  * l2_input_acl.c : layer 2 input acl processing
3  *
4  * Copyright (c) 2013 Cisco and/or its affiliates.
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at:
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 #include <vlib/vlib.h>
19 #include <vnet/vnet.h>
20 #include <vnet/pg/pg.h>
21 #include <vnet/ethernet/ethernet.h>
22 #include <vnet/ethernet/packet.h>
23 #include <vnet/ip/ip_packet.h>
24 #include <vnet/ip/ip4_packet.h>
25 #include <vnet/ip/ip6_packet.h>
26 #include <vlib/cli.h>
27 #include <vnet/l2/l2_input.h>
28 #include <vnet/l2/feat_bitmap.h>
29
30 #include <vppinfra/error.h>
31 #include <vppinfra/hash.h>
32 #include <vppinfra/cache.h>
33
34 #include <vnet/classify/vnet_classify.h>
35 #include <vnet/classify/input_acl.h>
36
37 typedef struct {
38
39   // Next nodes for each feature
40   u32 feat_next_node_index[32];
41
42   /* convenience variables */
43   vlib_main_t * vlib_main;
44   vnet_main_t * vnet_main;
45 } l2_inacl_main_t;
46
47 typedef struct {
48   u32 sw_if_index;
49   u32 next_index;
50   u32 table_index;
51   u32 offset;
52 } l2_inacl_trace_t;
53
54 /* packet trace format function */
55 static u8 * format_l2_inacl_trace (u8 * s, va_list * args)
56 {
57   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
58   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
59   l2_inacl_trace_t * t = va_arg (*args, l2_inacl_trace_t *);
60   
61   s = format (s, "INACL: sw_if_index %d, next_index %d, table %d, offset %d",
62               t->sw_if_index, t->next_index, t->table_index, t->offset);
63   return s;
64 }
65
66 l2_inacl_main_t l2_inacl_main;
67
68 static vlib_node_registration_t l2_inacl_node;
69
70 #define foreach_l2_inacl_error                  \
71 _(NONE, "valid input ACL packets")              \
72 _(MISS, "input ACL misses")                     \
73 _(HIT, "input ACL hits")                        \
74 _(CHAIN_HIT, "input ACL hits after chain walk") \
75 _(TABLE_MISS, "input ACL table-miss drops")     \
76 _(SESSION_DENY, "input ACL session deny drops")
77
78
79 typedef enum {
80 #define _(sym,str) L2_INACL_ERROR_##sym,
81   foreach_l2_inacl_error
82 #undef _
83   L2_INACL_N_ERROR,
84 } l2_inacl_error_t;
85
86 static char * l2_inacl_error_strings[] = {
87 #define _(sym,string) string,
88   foreach_l2_inacl_error
89 #undef _
90 };
91
92 static uword
93 l2_inacl_node_fn (vlib_main_t * vm,
94                   vlib_node_runtime_t * node,
95                   vlib_frame_t * frame)
96 {
97   u32 n_left_from, * from, * to_next;
98   acl_next_index_t next_index;
99   l2_inacl_main_t * msm = &l2_inacl_main;
100   input_acl_main_t * am = &input_acl_main;
101   vnet_classify_main_t * vcm = am->vnet_classify_main;
102   input_acl_table_id_t tid = INPUT_ACL_TABLE_L2;
103   f64 now = vlib_time_now (vm);
104   u32 hits = 0;
105   u32 misses = 0;
106   u32 chain_hits = 0;
107
108   from = vlib_frame_vector_args (frame);
109   n_left_from = frame->n_vectors; /* number of packets to process */
110   next_index = node->cached_next_index;
111
112   /* First pass: compute hashes */
113   while (n_left_from > 2)
114     {
115       vlib_buffer_t * b0, * b1;
116       u32 bi0, bi1;
117       u8 * h0, * h1;
118       u32 sw_if_index0, sw_if_index1;
119       u32 table_index0, table_index1;
120       vnet_classify_table_t * t0, * t1;
121
122       /* prefetch next iteration */
123       {
124         vlib_buffer_t * p1, * p2;
125
126         p1 = vlib_get_buffer (vm, from[1]);
127         p2 = vlib_get_buffer (vm, from[2]);
128
129         vlib_prefetch_buffer_header (p1, STORE);
130         CLIB_PREFETCH (p1->data, CLIB_CACHE_LINE_BYTES, STORE);
131         vlib_prefetch_buffer_header (p2, STORE);
132         CLIB_PREFETCH (p2->data, CLIB_CACHE_LINE_BYTES, STORE);
133       }
134
135       bi0 = from[0];
136       b0 = vlib_get_buffer (vm, bi0);
137       h0 = b0->data;
138
139       bi1 = from[1];
140       b1 = vlib_get_buffer (vm, bi1);
141       h1 = b1->data;
142
143       sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
144       table_index0 = am->classify_table_index_by_sw_if_index[tid][sw_if_index0];
145
146       sw_if_index1 = vnet_buffer (b1)->sw_if_index[VLIB_RX];
147       table_index1 = am->classify_table_index_by_sw_if_index[tid][sw_if_index1];
148
149       t0 = pool_elt_at_index (vcm->tables, table_index0);
150
151       t1 = pool_elt_at_index (vcm->tables, table_index1);
152
153       vnet_buffer(b0)->l2_classify.hash =
154         vnet_classify_hash_packet (t0, (u8 *) h0);
155
156       vnet_classify_prefetch_bucket (t0, vnet_buffer(b0)->l2_classify.hash);
157
158       vnet_buffer(b1)->l2_classify.hash =
159         vnet_classify_hash_packet (t1, (u8 *) h1);
160
161       vnet_classify_prefetch_bucket (t1, vnet_buffer(b1)->l2_classify.hash);
162
163       vnet_buffer(b0)->l2_classify.table_index = table_index0;
164
165       vnet_buffer(b1)->l2_classify.table_index = table_index1;
166
167       from += 2;
168       n_left_from -= 2;
169     }
170
171   while (n_left_from > 0)
172     {
173       vlib_buffer_t * b0;
174       u32 bi0;
175       u8 * h0;
176       u32 sw_if_index0;
177       u32 table_index0;
178       vnet_classify_table_t * t0;
179
180       bi0 = from[0];
181       b0 = vlib_get_buffer (vm, bi0);
182       h0 = b0->data;
183
184       sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
185       table_index0 = am->classify_table_index_by_sw_if_index[tid][sw_if_index0];
186
187       t0 = pool_elt_at_index (vcm->tables, table_index0);
188       vnet_buffer(b0)->l2_classify.hash =
189         vnet_classify_hash_packet (t0, (u8 *) h0);
190
191       vnet_buffer(b0)->l2_classify.table_index = table_index0;
192       vnet_classify_prefetch_bucket (t0, vnet_buffer(b0)->l2_classify.hash);
193
194       from++;
195       n_left_from--;
196     }
197
198   next_index = node->cached_next_index;
199   from = vlib_frame_vector_args (frame);
200   n_left_from = frame->n_vectors;
201
202   while (n_left_from > 0)
203     {
204       u32 n_left_to_next;
205
206       vlib_get_next_frame (vm, node, next_index,
207                            to_next, n_left_to_next);
208
209       /* Not enough load/store slots to dual loop... */
210       while (n_left_from > 0 && n_left_to_next > 0)
211         {
212           u32 bi0;
213           vlib_buffer_t * b0;
214           u32 next0 = ACL_NEXT_INDEX_DENY;
215           u32 table_index0;
216           vnet_classify_table_t * t0;
217           vnet_classify_entry_t * e0;
218           u64 hash0;
219           u8 * h0;
220           u8 error0;
221
222           /* Stride 3 seems to work best */
223           if (PREDICT_TRUE (n_left_from > 3))
224             {
225               vlib_buffer_t * p1 = vlib_get_buffer(vm, from[3]);
226               vnet_classify_table_t * tp1;
227               u32 table_index1;
228               u64 phash1;
229
230               table_index1 = vnet_buffer(p1)->l2_classify.table_index;
231
232               if (PREDICT_TRUE (table_index1 != ~0))
233                 {
234                   tp1 = pool_elt_at_index (vcm->tables, table_index1);
235                   phash1 = vnet_buffer(p1)->l2_classify.hash;
236                   vnet_classify_prefetch_entry (tp1, phash1);
237                 }
238             }
239
240           /* speculatively enqueue b0 to the current next frame */
241           bi0 = from[0];
242           to_next[0] = bi0;
243           from += 1;
244           to_next += 1;
245           n_left_from -= 1;
246           n_left_to_next -= 1;
247
248           b0 = vlib_get_buffer (vm, bi0);
249           h0 = b0->data;
250           table_index0 = vnet_buffer(b0)->l2_classify.table_index;
251           e0 = 0;
252           t0 = 0;
253
254           /* Feature bitmap update */
255           vnet_buffer(b0)->l2.feature_bitmap &= ~L2INPUT_FEAT_ACL;
256
257           /* Determine the next node */
258           next0 = feat_bitmap_get_next_node_index(msm->feat_next_node_index,
259                     vnet_buffer(b0)->l2.feature_bitmap);
260
261           if (PREDICT_TRUE(table_index0 != ~0))
262             {
263               hash0 = vnet_buffer(b0)->l2_classify.hash;
264               t0 = pool_elt_at_index (vcm->tables, table_index0);
265
266               e0 = vnet_classify_find_entry (t0, (u8 *) h0, hash0,
267                                              now);
268               if (e0)
269                 {
270                   vlib_buffer_advance (b0, e0->advance);
271
272                   next0 = (e0->next_index < ACL_NEXT_INDEX_N_NEXT)?
273                            e0->next_index:next0;
274
275                   hits++;
276
277                   error0 = (next0 == ACL_NEXT_INDEX_DENY)?
278                     L2_INACL_ERROR_SESSION_DENY:L2_INACL_ERROR_NONE;
279                   b0->error = node->errors[error0];
280                 }
281               else
282                 {
283                   while (1)
284                     {
285                       if (PREDICT_TRUE(t0->next_table_index != ~0))
286                         t0 = pool_elt_at_index (vcm->tables,
287                                                 t0->next_table_index);
288                       else
289                         {
290                           next0 = (t0->miss_next_index < ACL_NEXT_INDEX_N_NEXT)?
291                                    t0->miss_next_index:next0;
292
293                           misses++;
294
295                           error0 = (next0 == ACL_NEXT_INDEX_DENY)?
296                             L2_INACL_ERROR_TABLE_MISS:L2_INACL_ERROR_NONE;
297                           b0->error = node->errors[error0];
298                           break;
299                         }
300
301                       hash0 = vnet_classify_hash_packet (t0, (u8 *) h0);
302                       e0 = vnet_classify_find_entry
303                         (t0, (u8 *) h0, hash0, now);
304                       if (e0)
305                         {
306                           vlib_buffer_advance (b0, e0->advance);
307                           next0 = (e0->next_index < ACL_NEXT_INDEX_N_NEXT)?
308                                    e0->next_index:next0;
309                           hits++;
310                           chain_hits++;
311
312                           error0 = (next0 == ACL_NEXT_INDEX_DENY)?
313                             L2_INACL_ERROR_SESSION_DENY:L2_INACL_ERROR_NONE;
314                           b0->error = node->errors[error0];
315                           break;
316                         }
317                     }
318                 }
319             }
320
321           if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE)
322                             && (b0->flags & VLIB_BUFFER_IS_TRACED)))
323             {
324               l2_inacl_trace_t *t =
325                  vlib_add_trace (vm, node, b0, sizeof (*t));
326               t->sw_if_index = vnet_buffer(b0)->sw_if_index[VLIB_RX];
327               t->next_index = next0;
328               t->table_index = t0 ? t0 - vcm->tables : ~0;
329               t->offset = e0 ? vnet_classify_get_offset (t0, e0): ~0;
330             }
331
332           /* verify speculative enqueue, maybe switch current next frame */
333           vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
334                                            to_next, n_left_to_next,
335                                            bi0, next0);
336         }
337
338       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
339     }
340
341   vlib_node_increment_counter (vm, node->node_index,
342                                L2_INACL_ERROR_MISS,
343                                misses);
344   vlib_node_increment_counter (vm, node->node_index,
345                                L2_INACL_ERROR_HIT,
346                                hits);
347   vlib_node_increment_counter (vm, node->node_index,
348                                L2_INACL_ERROR_CHAIN_HIT,
349                                chain_hits);
350   return frame->n_vectors;
351 }
352
353 VLIB_REGISTER_NODE (l2_inacl_node,static) = {
354   .function = l2_inacl_node_fn,
355   .name = "l2-input-acl",
356   .vector_size = sizeof (u32),
357   .format_trace = format_l2_inacl_trace,
358   .type = VLIB_NODE_TYPE_INTERNAL,
359   
360   .n_errors = ARRAY_LEN(l2_inacl_error_strings),
361   .error_strings = l2_inacl_error_strings,
362
363   .n_next_nodes = ACL_NEXT_INDEX_N_NEXT,
364
365   /* edit / add dispositions here */
366   .next_nodes = {
367        [ACL_NEXT_INDEX_DENY]  = "error-drop",
368   },
369 };
370
371 clib_error_t *l2_inacl_init (vlib_main_t *vm)
372 {
373   l2_inacl_main_t * mp = &l2_inacl_main;
374  
375   mp->vlib_main = vm;
376   mp->vnet_main = vnet_get_main();
377
378   // Initialize the feature next-node indexes
379   feat_bitmap_init_next_nodes(vm,
380                               l2_inacl_node.index,
381                               L2INPUT_N_FEAT,
382                               l2input_get_feat_names(),
383                               mp->feat_next_node_index);
384
385   return 0;
386 }
387
388 VLIB_INIT_FUNCTION (l2_inacl_init);
389
390
391 // set subinterface inacl enable/disable
392 // The CLI format is:
393 //    set interface acl input <interface> [disable]
394 static clib_error_t *
395 int_l2_inacl (vlib_main_t * vm,
396               unformat_input_t * input,
397               vlib_cli_command_t * cmd)
398 {
399   vnet_main_t * vnm = vnet_get_main();
400   clib_error_t * error = 0;
401   u32 sw_if_index;
402   u32 enable;
403
404   if (! unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
405     {
406       error = clib_error_return (0, "unknown interface `%U'",
407                                  format_unformat_error, input);
408       goto done;
409     }
410
411   enable = 1;
412   if (unformat (input, "disable")) {
413     enable = 0;
414   }
415
416   // set the interface flag
417   l2input_intf_bitmap_enable(sw_if_index, L2INPUT_FEAT_ACL, enable);
418
419  done:
420   return error;
421 }
422
423 VLIB_CLI_COMMAND (int_l2_inacl_cli, static) = {
424   .path = "set interface acl input",
425   .short_help = "set interface acl input <interface> [disable]",
426   .function = int_l2_inacl,
427 };