e858c8ed9cc8cb7d2ba9735407cda1c9f45dd047
[vpp.git] / src / vnet / ip / ip_in_out_acl.c
1 /*
2  * Copyright (c) 2015 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/ip/ip.h>
16 #include <vnet/classify/vnet_classify.h>
17 #include <vnet/classify/in_out_acl.h>
18
19 typedef struct
20 {
21   u32 sw_if_index;
22   u32 next_index;
23   u32 table_index;
24   u32 offset;
25 }
26 ip_in_out_acl_trace_t;
27
28 /* packet trace format function */
29 static u8 *
30 format_ip_in_out_acl_trace (u8 * s, u32 is_output, va_list * args)
31 {
32   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
33   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
34   ip_in_out_acl_trace_t *t = va_arg (*args, ip_in_out_acl_trace_t *);
35
36   s = format (s, "%s: sw_if_index %d, next_index %d, table %d, offset %d",
37               is_output ? "OUTACL" : "INACL",
38               t->sw_if_index, t->next_index, t->table_index, t->offset);
39   return s;
40 }
41
42 static u8 *
43 format_ip_inacl_trace (u8 * s, va_list * args)
44 {
45   return format_ip_in_out_acl_trace (s, 0 /* is_output */ , args);
46 }
47
48 static u8 *
49 format_ip_outacl_trace (u8 * s, va_list * args)
50 {
51   return format_ip_in_out_acl_trace (s, 1 /* is_output */ , args);
52 }
53
54 extern vlib_node_registration_t ip4_inacl_node;
55 extern vlib_node_registration_t ip4_outacl_node;
56 extern vlib_node_registration_t ip6_inacl_node;
57 extern vlib_node_registration_t ip6_outacl_node;
58
59 #define foreach_ip_inacl_error                  \
60 _(MISS, "input ACL misses")                     \
61 _(HIT, "input ACL hits")                        \
62 _(CHAIN_HIT, "input ACL hits after chain walk")
63
64 #define foreach_ip_outacl_error                  \
65 _(MISS, "output ACL misses")                     \
66 _(HIT, "output ACL hits")                        \
67 _(CHAIN_HIT, "output ACL hits after chain walk")
68
69 typedef enum
70 {
71 #define _(sym,str) IP_INACL_ERROR_##sym,
72   foreach_ip_inacl_error
73 #undef _
74     IP_INACL_N_ERROR,
75 }
76 ip_inacl_error_t;
77
78 static char *ip_inacl_error_strings[] = {
79 #define _(sym,string) string,
80   foreach_ip_inacl_error
81 #undef _
82 };
83
84 typedef enum
85 {
86 #define _(sym,str) IP_OUTACL_ERROR_##sym,
87   foreach_ip_outacl_error
88 #undef _
89     IP_OUTACL_N_ERROR,
90 }
91 ip_outacl_error_t;
92
93 static char *ip_outacl_error_strings[] = {
94 #define _(sym,string) string,
95   foreach_ip_outacl_error
96 #undef _
97 };
98
99 static_always_inline void
100 ip_in_out_acl_inline_trace (vlib_main_t *vm, vlib_node_runtime_t *node,
101                             vlib_frame_t *frame, vlib_buffer_t **b, u16 *next,
102                             u32 n_left, u32 *hits__, u32 *misses__,
103                             u32 *chain_hits__, const vlib_error_t error_none,
104                             const vlib_error_t error_deny,
105                             const vlib_error_t error_miss,
106                             vnet_classify_table_t *tables,
107                             const u32 *table_index_by_sw_if_index,
108                             vnet_config_main_t *cm, const vlib_rx_or_tx_t way,
109                             const int is_output, const int do_trace)
110 {
111   f64 now = vlib_time_now (vm);
112   u32 hits = 0;
113   u32 misses = 0;
114   u32 chain_hits = 0;
115   u32 n_next_nodes = node->n_next_nodes;
116   u8 *h[4];
117   u32 sw_if_index[4];
118   u32 table_index[4];
119   vnet_classify_table_t *t[4] = { 0, 0 };
120   u64 hash[4];
121
122   /* calculate hashes for b[0] & b[1] */
123   if (n_left >= 2)
124     {
125       /* ~0 is used as a wildcard to say 'always use sw_if_index 0'
126        * aka local0. It is used when we do not care about the sw_if_index, as
127        * when punting */
128       sw_if_index[2] = ~0 == way ? 0 : vnet_buffer (b[0])->sw_if_index[way];
129       sw_if_index[3] = ~0 == way ? 0 : vnet_buffer (b[1])->sw_if_index[way];
130
131       table_index[2] = table_index_by_sw_if_index[sw_if_index[2]];
132       table_index[3] = table_index_by_sw_if_index[sw_if_index[3]];
133
134       t[2] = pool_elt_at_index (tables, table_index[2]);
135       t[3] = pool_elt_at_index (tables, table_index[3]);
136
137       if (t[2]->current_data_flag == CLASSIFY_FLAG_USE_CURR_DATA)
138         h[2] =
139           (void *) vlib_buffer_get_current (b[0]) + t[2]->current_data_offset;
140       else
141         h[2] = b[0]->data;
142
143       if (t[3]->current_data_flag == CLASSIFY_FLAG_USE_CURR_DATA)
144         h[3] =
145           (void *) vlib_buffer_get_current (b[1]) + t[3]->current_data_offset;
146       else
147         h[3] = b[1]->data;
148
149       if (is_output)
150         {
151           /* Save the rewrite length, since we are using the l2_classify struct */
152           vnet_buffer (b[0])->l2_classify.pad.l2_len =
153             vnet_buffer (b[0])->ip.save_rewrite_length;
154           /* advance the match pointer so the matching happens on IP header */
155           h[2] += vnet_buffer (b[0])->l2_classify.pad.l2_len;
156
157           /* Save the rewrite length, since we are using the l2_classify struct */
158           vnet_buffer (b[1])->l2_classify.pad.l2_len =
159             vnet_buffer (b[1])->ip.save_rewrite_length;
160           /* advance the match pointer so the matching happens on IP header */
161           h[3] += vnet_buffer (b[1])->l2_classify.pad.l2_len;
162         }
163
164       hash[2] = vnet_classify_hash_packet_inline (t[2], (u8 *) h[2]);
165       hash[3] = vnet_classify_hash_packet_inline (t[3], (u8 *) h[3]);
166
167       vnet_buffer (b[0])->l2_classify.hash = hash[2];
168       vnet_buffer (b[1])->l2_classify.hash = hash[3];
169
170       vnet_buffer (b[0])->l2_classify.table_index = table_index[2];
171       vnet_buffer (b[1])->l2_classify.table_index = table_index[3];
172
173       vnet_buffer (b[0])->l2_classify.opaque_index = ~0;
174       vnet_buffer (b[1])->l2_classify.opaque_index = ~0;
175
176       vnet_classify_prefetch_bucket (t[2],
177                                      vnet_buffer (b[0])->l2_classify.hash);
178       vnet_classify_prefetch_bucket (t[3],
179                                      vnet_buffer (b[1])->l2_classify.hash);
180     }
181
182   while (n_left >= 2)
183     {
184       vnet_classify_entry_t *e[2] = { 0, 0 };
185       u32 _next[2] = { ACL_NEXT_INDEX_DENY, ACL_NEXT_INDEX_DENY };
186
187       h[0] = h[2];
188       h[1] = h[3];
189       t[0] = t[2];
190       t[1] = t[3];
191
192       sw_if_index[0] = sw_if_index[2];
193       sw_if_index[1] = sw_if_index[3];
194
195       table_index[0] = table_index[2];
196       table_index[1] = table_index[3];
197
198       hash[0] = hash[2];
199       hash[1] = hash[3];
200
201       /* prefetch next iteration */
202       if (n_left >= 6)
203         {
204           vlib_prefetch_buffer_header (b[4], LOAD);
205           vlib_prefetch_buffer_header (b[5], LOAD);
206
207           clib_prefetch_load (b[4]->data);
208           clib_prefetch_load (b[5]->data);
209         }
210
211       /* calculate hashes for b[2] & b[3] */
212       if (n_left >= 4)
213         {
214           sw_if_index[2] =
215             ~0 == way ? 0 : vnet_buffer (b[2])->sw_if_index[way];
216           sw_if_index[3] =
217             ~0 == way ? 0 : vnet_buffer (b[3])->sw_if_index[way];
218
219           table_index[2] = table_index_by_sw_if_index[sw_if_index[2]];
220           table_index[3] = table_index_by_sw_if_index[sw_if_index[3]];
221
222           t[2] = pool_elt_at_index (tables, table_index[2]);
223           t[3] = pool_elt_at_index (tables, table_index[3]);
224
225           if (t[2]->current_data_flag == CLASSIFY_FLAG_USE_CURR_DATA)
226             h[2] =
227               (void *) vlib_buffer_get_current (b[2]) +
228               t[2]->current_data_offset;
229           else
230             h[2] = b[2]->data;
231
232           if (t[3]->current_data_flag == CLASSIFY_FLAG_USE_CURR_DATA)
233             h[3] =
234               (void *) vlib_buffer_get_current (b[3]) +
235               t[3]->current_data_offset;
236           else
237             h[3] = b[3]->data;
238
239           if (is_output)
240             {
241               /* Save the rewrite length, since we are using the l2_classify struct */
242               vnet_buffer (b[2])->l2_classify.pad.l2_len =
243                 vnet_buffer (b[2])->ip.save_rewrite_length;
244               /* advance the match pointer so the matching happens on IP header */
245               h[2] += vnet_buffer (b[2])->l2_classify.pad.l2_len;
246
247               /* Save the rewrite length, since we are using the l2_classify struct */
248               vnet_buffer (b[3])->l2_classify.pad.l2_len =
249                 vnet_buffer (b[3])->ip.save_rewrite_length;
250               /* advance the match pointer so the matching happens on IP header */
251               h[3] += vnet_buffer (b[3])->l2_classify.pad.l2_len;
252             }
253
254           hash[2] = vnet_classify_hash_packet_inline (t[2], (u8 *) h[2]);
255           hash[3] = vnet_classify_hash_packet_inline (t[3], (u8 *) h[3]);
256
257           vnet_buffer (b[2])->l2_classify.hash = hash[2];
258           vnet_buffer (b[3])->l2_classify.hash = hash[3];
259
260           vnet_buffer (b[2])->l2_classify.table_index = table_index[2];
261           vnet_buffer (b[3])->l2_classify.table_index = table_index[3];
262
263           vnet_buffer (b[2])->l2_classify.opaque_index = ~0;
264           vnet_buffer (b[3])->l2_classify.opaque_index = ~0;
265
266           vnet_classify_prefetch_bucket (t[2],
267                                          vnet_buffer (b[2])->
268                                          l2_classify.hash);
269           vnet_classify_prefetch_bucket (t[3],
270                                          vnet_buffer (b[3])->
271                                          l2_classify.hash);
272         }
273
274       /* find entry for b[0] & b[1] */
275       vnet_get_config_data (cm, &b[0]->current_config_index, &_next[0],
276                             /* # bytes of config data */ 0);
277       vnet_get_config_data (cm, &b[1]->current_config_index, &_next[1],
278                             /* # bytes of config data */ 0);
279
280       if (PREDICT_TRUE (table_index[0] != ~0))
281         {
282           e[0] =
283             vnet_classify_find_entry_inline (t[0], (u8 *) h[0], hash[0], now);
284           if (e[0])
285             {
286               vnet_buffer (b[0])->l2_classify.opaque_index
287                 = e[0]->opaque_index;
288               vlib_buffer_advance (b[0], e[0]->advance);
289
290               _next[0] = (e[0]->next_index < n_next_nodes) ?
291                 e[0]->next_index : _next[0];
292
293               hits++;
294
295               b[0]->error =
296                 (_next[0] == ACL_NEXT_INDEX_DENY) ? error_deny : error_none;
297
298               if (!is_output)
299                 {
300                   if (e[0]->action == CLASSIFY_ACTION_SET_IP4_FIB_INDEX ||
301                       e[0]->action == CLASSIFY_ACTION_SET_IP6_FIB_INDEX)
302                     vnet_buffer (b[0])->sw_if_index[VLIB_TX] = e[0]->metadata;
303                   else if (e[0]->action == CLASSIFY_ACTION_SET_METADATA)
304                     vnet_buffer (b[0])->ip.adj_index[VLIB_TX] =
305                       e[0]->metadata;
306                 }
307             }
308           else
309             {
310               while (1)
311                 {
312                   if (PREDICT_TRUE (t[0]->next_table_index != ~0))
313                     t[0] = pool_elt_at_index (tables, t[0]->next_table_index);
314                   else
315                     {
316                       _next[0] = (t[0]->miss_next_index < n_next_nodes) ?
317                         t[0]->miss_next_index : _next[0];
318
319                       misses++;
320
321                       b[0]->error = (_next[0] == ACL_NEXT_INDEX_DENY) ?
322                                       error_miss :
323                                       error_none;
324                       break;
325                     }
326
327                   if (t[0]->current_data_flag == CLASSIFY_FLAG_USE_CURR_DATA)
328                     h[0] =
329                       (void *) vlib_buffer_get_current (b[0]) +
330                       t[0]->current_data_offset;
331                   else
332                     h[0] = b[0]->data;
333
334                   /* advance the match pointer so the matching happens on IP header */
335                   if (is_output)
336                     h[0] += vnet_buffer (b[0])->l2_classify.pad.l2_len;
337
338                   hash[0] =
339                     vnet_classify_hash_packet_inline (t[0], (u8 *) h[0]);
340                   e[0] =
341                     vnet_classify_find_entry_inline (t[0], (u8 *) h[0],
342                                                      hash[0], now);
343                   if (e[0])
344                     {
345                       vnet_buffer (b[0])->l2_classify.opaque_index
346                         = e[0]->opaque_index;
347                       vlib_buffer_advance (b[0], e[0]->advance);
348                       _next[0] = (e[0]->next_index < n_next_nodes) ?
349                         e[0]->next_index : _next[0];
350                       hits++;
351                       chain_hits++;
352
353                       b[0]->error = (_next[0] == ACL_NEXT_INDEX_DENY) ?
354                                       error_deny :
355                                       error_none;
356
357                       if (!is_output)
358                         {
359                           if (e[0]->action ==
360                               CLASSIFY_ACTION_SET_IP4_FIB_INDEX
361                               || e[0]->action ==
362                               CLASSIFY_ACTION_SET_IP6_FIB_INDEX)
363                             vnet_buffer (b[0])->sw_if_index[VLIB_TX] =
364                               e[0]->metadata;
365                           else if (e[0]->action ==
366                                    CLASSIFY_ACTION_SET_METADATA)
367                             vnet_buffer (b[0])->ip.adj_index[VLIB_TX] =
368                               e[0]->metadata;
369                         }
370                       break;
371                     }
372                 }
373             }
374         }
375
376       if (PREDICT_TRUE (table_index[1] != ~0))
377         {
378           e[1] =
379             vnet_classify_find_entry_inline (t[1], (u8 *) h[1], hash[1], now);
380           if (e[1])
381             {
382               vnet_buffer (b[1])->l2_classify.opaque_index
383                 = e[1]->opaque_index;
384               vlib_buffer_advance (b[1], e[1]->advance);
385
386               _next[1] = (e[1]->next_index < n_next_nodes) ?
387                 e[1]->next_index : _next[1];
388
389               hits++;
390
391               b[1]->error =
392                 (_next[1] == ACL_NEXT_INDEX_DENY) ? error_deny : error_none;
393
394               if (!is_output)
395                 {
396                   if (e[1]->action == CLASSIFY_ACTION_SET_IP4_FIB_INDEX ||
397                       e[1]->action == CLASSIFY_ACTION_SET_IP6_FIB_INDEX)
398                     vnet_buffer (b[1])->sw_if_index[VLIB_TX] = e[1]->metadata;
399                   else if (e[1]->action == CLASSIFY_ACTION_SET_METADATA)
400                     vnet_buffer (b[1])->ip.adj_index[VLIB_TX] =
401                       e[1]->metadata;
402                 }
403             }
404           else
405             {
406               while (1)
407                 {
408                   if (PREDICT_TRUE (t[1]->next_table_index != ~0))
409                     t[1] = pool_elt_at_index (tables, t[1]->next_table_index);
410                   else
411                     {
412                       _next[1] = (t[1]->miss_next_index < n_next_nodes) ?
413                         t[1]->miss_next_index : _next[1];
414
415                       misses++;
416
417                       b[1]->error = (_next[1] == ACL_NEXT_INDEX_DENY) ?
418                                       error_miss :
419                                       error_none;
420                       break;
421                     }
422
423                   if (t[1]->current_data_flag == CLASSIFY_FLAG_USE_CURR_DATA)
424                     h[1] =
425                       (void *) vlib_buffer_get_current (b[1]) +
426                       t[1]->current_data_offset;
427                   else
428                     h[1] = b[1]->data;
429
430                   /* advance the match pointer so the matching happens on IP header */
431                   if (is_output)
432                     h[1] += vnet_buffer (b[1])->l2_classify.pad.l2_len;
433
434                   hash[1] =
435                     vnet_classify_hash_packet_inline (t[1], (u8 *) h[1]);
436                   e[1] =
437                     vnet_classify_find_entry_inline (t[1], (u8 *) h[1],
438                                                      hash[1], now);
439                   if (e[1])
440                     {
441                       vnet_buffer (b[1])->l2_classify.opaque_index
442                         = e[1]->opaque_index;
443                       vlib_buffer_advance (b[1], e[1]->advance);
444                       _next[1] = (e[1]->next_index < n_next_nodes) ?
445                         e[1]->next_index : _next[1];
446                       hits++;
447                       chain_hits++;
448
449                       b[1]->error = (_next[1] == ACL_NEXT_INDEX_DENY) ?
450                                       error_deny :
451                                       error_none;
452
453                       if (!is_output)
454                         {
455                           if (e[1]->action ==
456                               CLASSIFY_ACTION_SET_IP4_FIB_INDEX
457                               || e[1]->action ==
458                               CLASSIFY_ACTION_SET_IP6_FIB_INDEX)
459                             vnet_buffer (b[1])->sw_if_index[VLIB_TX] =
460                               e[1]->metadata;
461                           else if (e[1]->action ==
462                                    CLASSIFY_ACTION_SET_METADATA)
463                             vnet_buffer (b[1])->ip.adj_index[VLIB_TX] =
464                               e[1]->metadata;
465                         }
466                       break;
467                     }
468                 }
469             }
470         }
471
472       if (do_trace && b[0]->flags & VLIB_BUFFER_IS_TRACED)
473         {
474           ip_in_out_acl_trace_t *_t =
475             vlib_add_trace (vm, node, b[0], sizeof (*_t));
476           _t->sw_if_index =
477             ~0 == way ? 0 : vnet_buffer (b[0])->sw_if_index[way];
478           _t->next_index = _next[0];
479           _t->table_index = t[0] ? t[0] - tables : ~0;
480           _t->offset = (e[0]
481                         && t[0]) ? vnet_classify_get_offset (t[0], e[0]) : ~0;
482         }
483
484       if (do_trace && b[1]->flags & VLIB_BUFFER_IS_TRACED)
485         {
486           ip_in_out_acl_trace_t *_t =
487             vlib_add_trace (vm, node, b[1], sizeof (*_t));
488           _t->sw_if_index =
489             ~0 == way ? 0 : vnet_buffer (b[1])->sw_if_index[way];
490           _t->next_index = _next[1];
491           _t->table_index = t[1] ? t[1] - tables : ~0;
492           _t->offset = (e[1]
493                         && t[1]) ? vnet_classify_get_offset (t[1], e[1]) : ~0;
494         }
495
496       if ((_next[0] == ACL_NEXT_INDEX_DENY) && is_output)
497         {
498           /* on output, for the drop node to work properly, go back to ip header */
499           vlib_buffer_advance (b[0], vnet_buffer (b[0])->l2.l2_len);
500         }
501
502       if ((_next[1] == ACL_NEXT_INDEX_DENY) && is_output)
503         {
504           /* on output, for the drop node to work properly, go back to ip header */
505           vlib_buffer_advance (b[1], vnet_buffer (b[1])->l2.l2_len);
506         }
507
508       next[0] = _next[0];
509       next[1] = _next[1];
510
511       /* _next */
512       next += 2;
513       b += 2;
514       n_left -= 2;
515     }
516
517   while (n_left > 0)
518     {
519       u8 *h0;
520       u32 sw_if_index0;
521       u32 table_index0;
522       vnet_classify_table_t *t0 = 0;
523       vnet_classify_entry_t *e0 = 0;
524       u32 next0 = ACL_NEXT_INDEX_DENY;
525       u64 hash0;
526
527       sw_if_index0 = ~0 == way ? 0 : vnet_buffer (b[0])->sw_if_index[way];
528       table_index0 = table_index_by_sw_if_index[sw_if_index0];
529
530       t0 = pool_elt_at_index (tables, table_index0);
531
532       if (t0->current_data_flag == CLASSIFY_FLAG_USE_CURR_DATA)
533         h0 =
534           (void *) vlib_buffer_get_current (b[0]) + t0->current_data_offset;
535       else
536         h0 = b[0]->data;
537
538       if (is_output)
539         {
540           /* Save the rewrite length, since we are using the l2_classify struct */
541           vnet_buffer (b[0])->l2_classify.pad.l2_len =
542             vnet_buffer (b[0])->ip.save_rewrite_length;
543           /* advance the match pointer so the matching happens on IP header */
544           h0 += vnet_buffer (b[0])->l2_classify.pad.l2_len;
545         }
546
547       vnet_buffer (b[0])->l2_classify.hash =
548         vnet_classify_hash_packet (t0, (u8 *) h0);
549
550       vnet_buffer (b[0])->l2_classify.table_index = table_index0;
551       vnet_buffer (b[0])->l2_classify.opaque_index = ~0;
552
553       vnet_get_config_data (cm, &b[0]->current_config_index, &next0,
554                             /* # bytes of config data */ 0);
555
556       if (PREDICT_TRUE (table_index0 != ~0))
557         {
558           hash0 = vnet_buffer (b[0])->l2_classify.hash;
559           t0 = pool_elt_at_index (tables, table_index0);
560
561           if (t0->current_data_flag == CLASSIFY_FLAG_USE_CURR_DATA)
562             h0 =
563               (void *) vlib_buffer_get_current (b[0]) +
564               t0->current_data_offset;
565           else
566             h0 = b[0]->data;
567
568           /* advance the match pointer so the matching happens on IP header */
569           if (is_output)
570             h0 += vnet_buffer (b[0])->l2_classify.pad.l2_len;
571
572           e0 = vnet_classify_find_entry_inline (t0, (u8 *) h0, hash0, now);
573           if (e0)
574             {
575               vnet_buffer (b[0])->l2_classify.opaque_index = e0->opaque_index;
576               vlib_buffer_advance (b[0], e0->advance);
577
578               next0 = (e0->next_index < n_next_nodes) ?
579                 e0->next_index : next0;
580
581               hits++;
582
583               b[0]->error =
584                 (next0 == ACL_NEXT_INDEX_DENY) ? error_deny : error_none;
585
586               if (!is_output)
587                 {
588                   if (e0->action == CLASSIFY_ACTION_SET_IP4_FIB_INDEX ||
589                       e0->action == CLASSIFY_ACTION_SET_IP6_FIB_INDEX)
590                     vnet_buffer (b[0])->sw_if_index[VLIB_TX] = e0->metadata;
591                   else if (e0->action == CLASSIFY_ACTION_SET_METADATA)
592                     vnet_buffer (b[0])->ip.adj_index[VLIB_TX] = e0->metadata;
593                 }
594             }
595           else
596             {
597               while (1)
598                 {
599                   if (PREDICT_TRUE (t0->next_table_index != ~0))
600                     t0 = pool_elt_at_index (tables, t0->next_table_index);
601                   else
602                     {
603                       next0 = (t0->miss_next_index < n_next_nodes) ?
604                         t0->miss_next_index : next0;
605
606                       misses++;
607
608                       b[0]->error = (next0 == ACL_NEXT_INDEX_DENY) ?
609                                       error_miss :
610                                       error_none;
611                       break;
612                     }
613
614                   if (t0->current_data_flag == CLASSIFY_FLAG_USE_CURR_DATA)
615                     h0 =
616                       (void *) vlib_buffer_get_current (b[0]) +
617                       t0->current_data_offset;
618                   else
619                     h0 = b[0]->data;
620
621                   /* advance the match pointer so the matching happens on IP header */
622                   if (is_output)
623                     h0 += vnet_buffer (b[0])->l2_classify.pad.l2_len;
624
625                   hash0 = vnet_classify_hash_packet_inline (t0, (u8 *) h0);
626                   e0 = vnet_classify_find_entry_inline
627                     (t0, (u8 *) h0, hash0, now);
628                   if (e0)
629                     {
630                       vnet_buffer (b[0])->l2_classify.opaque_index
631                         = e0->opaque_index;
632                       vlib_buffer_advance (b[0], e0->advance);
633                       next0 = (e0->next_index < n_next_nodes) ?
634                         e0->next_index : next0;
635                       hits++;
636
637                       b[0]->error = (next0 == ACL_NEXT_INDEX_DENY) ?
638                                       error_deny :
639                                       error_none;
640
641                       if (!is_output)
642                         {
643                           if (e0->action ==
644                               CLASSIFY_ACTION_SET_IP4_FIB_INDEX
645                               || e0->action ==
646                               CLASSIFY_ACTION_SET_IP6_FIB_INDEX)
647                             vnet_buffer (b[0])->sw_if_index[VLIB_TX] =
648                               e0->metadata;
649                           else if (e0->action == CLASSIFY_ACTION_SET_METADATA)
650                             vnet_buffer (b[0])->ip.adj_index[VLIB_TX] =
651                               e0->metadata;
652                         }
653                       break;
654                     }
655                 }
656             }
657         }
658
659       if (do_trace && b[0]->flags & VLIB_BUFFER_IS_TRACED)
660         {
661           ip_in_out_acl_trace_t *t =
662             vlib_add_trace (vm, node, b[0], sizeof (*t));
663           t->sw_if_index =
664             ~0 == way ? 0 : vnet_buffer (b[0])->sw_if_index[way];
665           t->next_index = next0;
666           t->table_index = t0 ? t0 - tables : ~0;
667           t->offset = (e0 && t0) ? vnet_classify_get_offset (t0, e0) : ~0;
668         }
669
670       if ((next0 == ACL_NEXT_INDEX_DENY) && is_output)
671         {
672           /* on output, for the drop node to work properly, go back to ip header */
673           vlib_buffer_advance (b[0], vnet_buffer (b[0])->l2.l2_len);
674         }
675
676       next[0] = next0;
677
678       /* next */
679       next++;
680       b++;
681       n_left--;
682     }
683
684   *hits__ = hits;
685   *misses__ = misses;
686   *chain_hits__ = chain_hits;
687 }
688
689 static_always_inline uword
690 ip_in_out_acl_inline (vlib_main_t *vm, vlib_node_runtime_t *node,
691                       vlib_frame_t *frame, const in_out_acl_table_id_t tid,
692                       const vlib_node_registration_t *parent_error_node,
693                       const u32 error_none_index, const u32 error_deny_index,
694                       const u32 error_miss_index, const vlib_rx_or_tx_t way,
695                       const int is_output)
696 {
697   const in_out_acl_main_t *am = &in_out_acl_main;
698   vnet_classify_table_t *tables = am->vnet_classify_main->tables;
699   u32 *from = vlib_frame_vector_args (frame);
700   const u32 *table_index_by_sw_if_index =
701     am->classify_table_index_by_sw_if_index[is_output][tid];
702   vnet_config_main_t *cm = am->vnet_config_main[is_output][tid];
703   const vlib_node_runtime_t *error_node =
704     vlib_node_get_runtime (vm, parent_error_node->index);
705   const vlib_error_t error_none = error_node->errors[error_none_index];
706   const vlib_error_t error_deny = error_node->errors[error_deny_index];
707   const vlib_error_t error_miss = error_node->errors[error_miss_index];
708   vlib_buffer_t *bufs[VLIB_FRAME_SIZE];
709   u16 nexts[VLIB_FRAME_SIZE];
710   u32 hits, misses, chain_hits;
711
712   vlib_get_buffers (vm, from, bufs, frame->n_vectors);
713
714 #define ip_in_out_acl_inline_trace__(do_trace)                                \
715   ip_in_out_acl_inline_trace (                                                \
716     vm, node, frame, bufs, nexts, frame->n_vectors, &hits, &misses,           \
717     &chain_hits, error_deny, error_miss, error_none, tables,                  \
718     table_index_by_sw_if_index, cm, way, is_output, do_trace)
719
720   if (PREDICT_FALSE (node->flags & VLIB_NODE_FLAG_TRACE))
721     ip_in_out_acl_inline_trace__ (1 /* do_trace */);
722   else
723     ip_in_out_acl_inline_trace__ (0 /* do_trace */);
724
725   vlib_node_increment_counter (
726     vm, node->node_index,
727     is_output ? IP_OUTACL_ERROR_MISS : IP_INACL_ERROR_MISS, misses);
728   vlib_node_increment_counter (
729     vm, node->node_index, is_output ? IP_OUTACL_ERROR_HIT : IP_INACL_ERROR_HIT,
730     hits);
731   vlib_node_increment_counter (vm, node->node_index,
732                                is_output ? IP_OUTACL_ERROR_CHAIN_HIT :
733                                            IP_INACL_ERROR_CHAIN_HIT,
734                                chain_hits);
735
736   vlib_buffer_enqueue_to_next (vm, node, from, nexts, frame->n_vectors);
737   return frame->n_vectors;
738 }
739
740 VLIB_NODE_FN (ip4_inacl_node)
741 (vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
742 {
743   return ip_in_out_acl_inline (
744     vm, node, frame, IN_OUT_ACL_TABLE_IP4, &ip4_input_node, IP4_ERROR_NONE,
745     IP4_ERROR_INACL_SESSION_DENY, IP4_ERROR_INACL_TABLE_MISS, VLIB_RX,
746     0 /* is_output */);
747 }
748
749 VLIB_NODE_FN (ip4_punt_acl_node)
750 (vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
751 {
752   return ip_in_out_acl_inline (
753     vm, node, frame, IN_OUT_ACL_TABLE_IP4_PUNT, &ip4_input_node,
754     IP4_ERROR_NONE, IP4_ERROR_INACL_SESSION_DENY, IP4_ERROR_INACL_TABLE_MISS,
755     ~0 /* way */, 0 /* is_output */);
756 }
757
758 VLIB_NODE_FN (ip4_outacl_node)
759 (vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
760 {
761   return ip_in_out_acl_inline (
762     vm, node, frame, IN_OUT_ACL_TABLE_IP4, &ip4_input_node, IP4_ERROR_NONE,
763     IP4_ERROR_INACL_SESSION_DENY, IP4_ERROR_INACL_TABLE_MISS, VLIB_TX,
764     1 /* is_output */);
765 }
766
767 /* *INDENT-OFF* */
768 VLIB_REGISTER_NODE (ip4_inacl_node) = {
769   .name = "ip4-inacl",
770   .vector_size = sizeof (u32),
771   .format_trace = format_ip_inacl_trace,
772   .n_errors = ARRAY_LEN(ip_inacl_error_strings),
773   .error_strings = ip_inacl_error_strings,
774
775   .n_next_nodes = ACL_NEXT_INDEX_N_NEXT,
776   .next_nodes = {
777     [ACL_NEXT_INDEX_DENY] = "ip4-drop",
778   },
779 };
780
781 VLIB_REGISTER_NODE (ip4_punt_acl_node) = {
782   .name = "ip4-punt-acl",
783   .vector_size = sizeof (u32),
784   .format_trace = format_ip_inacl_trace,
785   .n_errors = ARRAY_LEN(ip_inacl_error_strings),
786   .error_strings = ip_inacl_error_strings,
787
788   .n_next_nodes = ACL_NEXT_INDEX_N_NEXT,
789   .next_nodes = {
790     [ACL_NEXT_INDEX_DENY] = "ip4-drop",
791   },
792 };
793
794 VLIB_REGISTER_NODE (ip4_outacl_node) = {
795   .name = "ip4-outacl",
796   .vector_size = sizeof (u32),
797   .format_trace = format_ip_outacl_trace,
798   .n_errors = ARRAY_LEN(ip_outacl_error_strings),
799   .error_strings = ip_outacl_error_strings,
800
801   .n_next_nodes = ACL_NEXT_INDEX_N_NEXT,
802   .next_nodes = {
803     [ACL_NEXT_INDEX_DENY] = "ip4-drop",
804   },
805 };
806 /* *INDENT-ON* */
807
808 VNET_FEATURE_INIT (ip4_punt_acl_feature) = {
809   .arc_name = "ip4-punt",
810   .node_name = "ip4-punt-acl",
811   .runs_after = VNET_FEATURES ("ip4-punt-policer"),
812 };
813
814 VLIB_NODE_FN (ip6_inacl_node) (vlib_main_t * vm, vlib_node_runtime_t * node,
815                                vlib_frame_t * frame)
816 {
817   return ip_in_out_acl_inline (
818     vm, node, frame, IN_OUT_ACL_TABLE_IP6, &ip6_input_node, IP6_ERROR_NONE,
819     IP6_ERROR_INACL_SESSION_DENY, IP6_ERROR_INACL_TABLE_MISS, VLIB_RX,
820     0 /* is_output */);
821 }
822
823 VLIB_NODE_FN (ip6_punt_acl_node)
824 (vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
825 {
826   return ip_in_out_acl_inline (
827     vm, node, frame, IN_OUT_ACL_TABLE_IP6_PUNT, &ip6_input_node,
828     IP6_ERROR_NONE, IP6_ERROR_INACL_SESSION_DENY, IP6_ERROR_INACL_TABLE_MISS,
829     ~0 /* way */, 0 /* is_output */);
830 }
831
832 VLIB_NODE_FN (ip6_outacl_node) (vlib_main_t * vm, vlib_node_runtime_t * node,
833                                 vlib_frame_t * frame)
834 {
835   return ip_in_out_acl_inline (
836     vm, node, frame, IN_OUT_ACL_TABLE_IP6, &ip6_input_node, IP6_ERROR_NONE,
837     IP6_ERROR_INACL_SESSION_DENY, IP6_ERROR_INACL_TABLE_MISS, VLIB_TX,
838     1 /* is_output */);
839 }
840
841 /* *INDENT-OFF* */
842 VLIB_REGISTER_NODE (ip6_inacl_node) = {
843   .name = "ip6-inacl",
844   .vector_size = sizeof (u32),
845   .format_trace = format_ip_inacl_trace,
846   .n_errors = ARRAY_LEN(ip_inacl_error_strings),
847   .error_strings = ip_inacl_error_strings,
848
849   .n_next_nodes = ACL_NEXT_INDEX_N_NEXT,
850   .next_nodes = {
851     [ACL_NEXT_INDEX_DENY] = "ip6-drop",
852   },
853 };
854
855 VLIB_REGISTER_NODE (ip6_punt_acl_node) = {
856   .name = "ip6-punt-acl",
857   .vector_size = sizeof (u32),
858   .format_trace = format_ip_inacl_trace,
859   .n_errors = ARRAY_LEN(ip_inacl_error_strings),
860   .error_strings = ip_inacl_error_strings,
861
862   .n_next_nodes = ACL_NEXT_INDEX_N_NEXT,
863   .next_nodes = {
864     [ACL_NEXT_INDEX_DENY] = "ip6-drop",
865   },
866 };
867
868 VLIB_REGISTER_NODE (ip6_outacl_node) = {
869   .name = "ip6-outacl",
870   .vector_size = sizeof (u32),
871   .format_trace = format_ip_outacl_trace,
872   .n_errors = ARRAY_LEN(ip_outacl_error_strings),
873   .error_strings = ip_outacl_error_strings,
874
875   .n_next_nodes = ACL_NEXT_INDEX_N_NEXT,
876   .next_nodes = {
877     [ACL_NEXT_INDEX_DENY] = "ip6-drop",
878   },
879 };
880 /* *INDENT-ON* */
881
882 VNET_FEATURE_INIT (ip6_punt_acl_feature) = {
883   .arc_name = "ip6-punt",
884   .node_name = "ip6-punt-acl",
885   .runs_after = VNET_FEATURES ("ip6-punt-policer"),
886 };
887
888 #ifndef CLIB_MARCH_VARIANT
889 static clib_error_t *
890 ip_in_out_acl_init (vlib_main_t * vm)
891 {
892   return 0;
893 }
894
895 VLIB_INIT_FUNCTION (ip_in_out_acl_init);
896 #endif /* CLIB_MARCH_VARIANT */
897
898
899 /*
900  * fd.io coding-style-patch-verification: ON
901  *
902  * Local Variables:
903  * eval: (c-set-style "gnu")
904  * End:
905  */