New api in order to get max entries of connection table is added.
[vpp.git] / src / plugins / acl / dataplane_node.c
1 /*
2  * Copyright (c) 2016-2018 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 <stddef.h>
16 #include <netinet/in.h>
17
18 #include <vlib/vlib.h>
19 #include <vnet/vnet.h>
20 #include <vnet/pg/pg.h>
21 #include <vppinfra/error.h>
22
23
24 #include <acl/acl.h>
25 #include <vnet/ip/icmp46_packet.h>
26
27 #include <plugins/acl/fa_node.h>
28 #include <plugins/acl/acl.h>
29 #include <plugins/acl/lookup_context.h>
30 #include <plugins/acl/public_inlines.h>
31 #include <plugins/acl/session_inlines.h>
32
33 #include <vppinfra/bihash_40_8.h>
34 #include <vppinfra/bihash_template.h>
35
36 typedef struct
37 {
38   u32 next_index;
39   u32 sw_if_index;
40   u32 lc_index;
41   u32 match_acl_in_index;
42   u32 match_rule_index;
43   u64 packet_info[6];
44   u32 trace_bitmap;
45   u8 action;
46 } acl_fa_trace_t;
47
48 /* *INDENT-OFF* */
49 #define foreach_acl_fa_error \
50 _(ACL_DROP, "ACL deny packets")  \
51 _(ACL_PERMIT, "ACL permit packets")  \
52 _(ACL_NEW_SESSION, "new sessions added") \
53 _(ACL_EXIST_SESSION, "existing session packets") \
54 _(ACL_CHECK, "checked packets") \
55 _(ACL_RESTART_SESSION_TIMER, "restart session timer") \
56 _(ACL_TOO_MANY_SESSIONS, "too many sessions to add new") \
57 /* end  of errors */
58
59 typedef enum
60 {
61 #define _(sym,str) ACL_FA_ERROR_##sym,
62   foreach_acl_fa_error
63 #undef _
64     ACL_FA_N_ERROR,
65 } acl_fa_error_t;
66
67 /* *INDENT-ON* */
68
69 typedef struct
70 {
71   u32 next_index;
72   u32 sw_if_index;
73   u16 ethertype;
74 } nonip_in_out_trace_t;
75
76 /* packet trace format function */
77 static u8 *
78 format_nonip_in_out_trace (u8 * s, u32 is_output, va_list * args)
79 {
80   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
81   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
82   nonip_in_out_trace_t *t = va_arg (*args, nonip_in_out_trace_t *);
83
84   s = format (s, "%s: sw_if_index %d next_index %x ethertype %x",
85               is_output ? "OUT-ETHER-WHITELIST" : "IN-ETHER-WHITELIST",
86               t->sw_if_index, t->next_index, t->ethertype);
87   return s;
88 }
89
90 static u8 *
91 format_l2_nonip_in_trace (u8 * s, va_list * args)
92 {
93   return format_nonip_in_out_trace (s, 0, args);
94 }
95
96 static u8 *
97 format_l2_nonip_out_trace (u8 * s, va_list * args)
98 {
99   return format_nonip_in_out_trace (s, 1, args);
100 }
101
102 #define foreach_nonip_in_error                    \
103 _(DROP, "dropped inbound non-whitelisted non-ip packets") \
104 _(PERMIT, "permitted inbound whitelisted non-ip packets") \
105
106
107 #define foreach_nonip_out_error                    \
108 _(DROP, "dropped outbound non-whitelisted non-ip packets") \
109 _(PERMIT, "permitted outbound whitelisted non-ip packets") \
110
111
112 /* *INDENT-OFF* */
113
114 typedef enum
115 {
116 #define _(sym,str) FA_IN_NONIP_ERROR_##sym,
117   foreach_nonip_in_error
118 #undef _
119     FA_IN_NONIP_N_ERROR,
120 } l2_in_feat_arc_error_t;
121
122 static char *fa_in_nonip_error_strings[] = {
123 #define _(sym,string) string,
124   foreach_nonip_in_error
125 #undef _
126 };
127
128 typedef enum
129 {
130 #define _(sym,str) FA_OUT_NONIP_ERROR_##sym,
131   foreach_nonip_out_error
132 #undef _
133     FA_OUT_NONIP_N_ERROR,
134 } l2_out_feat_arc_error_t;
135
136 static char *fa_out_nonip_error_strings[] = {
137 #define _(sym,string) string,
138   foreach_nonip_out_error
139 #undef _
140 };
141 /* *INDENT-ON* */
142
143
144 always_inline int
145 is_permitted_ethertype (acl_main_t * am, int sw_if_index0, int is_output,
146                         u16 ethertype)
147 {
148   u16 **v = is_output
149     ? am->output_etype_whitelist_by_sw_if_index
150     : am->input_etype_whitelist_by_sw_if_index;
151   u16 *whitelist = vec_elt (v, sw_if_index0);
152   int i;
153
154   if (vec_len (whitelist) == 0)
155     return 1;
156
157   for (i = 0; i < vec_len (whitelist); i++)
158     if (whitelist[i] == ethertype)
159       return 1;
160   return 0;
161 }
162
163 #define get_u16(addr) ( *((u16 *)(addr)) )
164
165 always_inline uword
166 nonip_in_out_node_fn (vlib_main_t * vm,
167                       vlib_node_runtime_t * node, vlib_frame_t * frame,
168                       int is_output)
169 {
170   acl_main_t *am = &acl_main;
171   u32 n_left, *from;
172   u16 nexts[VLIB_FRAME_SIZE], *next;
173   vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b;
174   vlib_node_runtime_t *error_node;
175
176   from = vlib_frame_vector_args (frame);
177   error_node = vlib_node_get_runtime (vm, node->node_index);
178   vlib_get_buffers (vm, from, bufs, frame->n_vectors);
179   /* set the initial values for the current buffer the next pointers */
180   b = bufs;
181   next = nexts;
182
183   n_left = frame->n_vectors;
184   while (n_left > 0)
185     {
186       u32 next_index = 0;
187       u32 sw_if_index0 =
188         vnet_buffer (b[0])->sw_if_index[is_output ? VLIB_TX : VLIB_RX];
189       u16 ethertype = 0;
190
191       int error0 = 0;
192
193       ethernet_header_t *h0 = vlib_buffer_get_current (b[0]);
194       u8 *l3h0 = (u8 *) h0 + vnet_buffer (b[0])->l2.l2_len;
195       ethertype = clib_net_to_host_u16 (get_u16 (l3h0 - 2));
196
197       if (is_permitted_ethertype (am, sw_if_index0, is_output, ethertype))
198         vnet_feature_next (&next_index, b[0]);
199
200       next[0] = next_index;
201
202       if (0 == next[0])
203         b[0]->error = error_node->errors[error0];
204
205       if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
206                          && (b[0]->flags & VLIB_BUFFER_IS_TRACED)))
207         {
208           nonip_in_out_trace_t *t =
209             vlib_add_trace (vm, node, b[0], sizeof (*t));
210           t->sw_if_index = sw_if_index0;
211           t->ethertype = ethertype;
212           t->next_index = next[0];
213         }
214       next[0] = next[0] < node->n_next_nodes ? next[0] : 0;
215
216       next++;
217       b++;
218       n_left--;
219     }
220   vlib_buffer_enqueue_to_next (vm, node, from, nexts, frame->n_vectors);
221
222   return frame->n_vectors;
223 }
224
225 VLIB_NODE_FN (acl_in_nonip_node) (vlib_main_t * vm,
226                                   vlib_node_runtime_t * node,
227                                   vlib_frame_t * frame)
228 {
229   return nonip_in_out_node_fn (vm, node, frame, 0);
230 }
231
232 VLIB_NODE_FN (acl_out_nonip_node) (vlib_main_t * vm,
233                                    vlib_node_runtime_t * node,
234                                    vlib_frame_t * frame)
235 {
236   return nonip_in_out_node_fn (vm, node, frame, 1);
237 }
238
239
240 /* *INDENT-OFF* */
241
242 VLIB_REGISTER_NODE (acl_in_nonip_node) =
243 {
244   .name = "acl-plugin-in-nonip-l2",
245   .vector_size = sizeof (u32),
246   .format_trace = format_l2_nonip_in_trace,
247   .type = VLIB_NODE_TYPE_INTERNAL,
248   .n_errors = ARRAY_LEN (fa_in_nonip_error_strings),
249   .error_strings = fa_in_nonip_error_strings,
250   .n_next_nodes = ACL_FA_N_NEXT,
251   .next_nodes =
252   {
253     [ACL_FA_ERROR_DROP] = "error-drop",
254   }
255 };
256
257 VNET_FEATURE_INIT (acl_in_l2_nonip_fa_feature, static) =
258 {
259   .arc_name = "l2-input-nonip",
260   .node_name = "acl-plugin-in-nonip-l2",
261   .runs_before = VNET_FEATURES ("l2-input-feat-arc-end"),
262 };
263
264 VLIB_REGISTER_NODE (acl_out_nonip_node) =
265 {
266   .name = "acl-plugin-out-nonip-l2",
267   .vector_size = sizeof (u32),
268   .format_trace = format_l2_nonip_out_trace,
269   .type = VLIB_NODE_TYPE_INTERNAL,
270   .n_errors = ARRAY_LEN (fa_out_nonip_error_strings),
271   .error_strings = fa_out_nonip_error_strings,
272   .n_next_nodes = ACL_FA_N_NEXT,
273   .next_nodes =
274   {
275     [ACL_FA_ERROR_DROP] = "error-drop",
276   }
277 };
278
279 VNET_FEATURE_INIT (acl_out_l2_nonip_fa_feature, static) =
280 {
281   .arc_name = "l2-output-nonip",
282   .node_name = "acl-plugin-out-nonip-l2",
283   .runs_before = VNET_FEATURES ("l2-output-feat-arc-end"),
284 };
285
286 /* *INDENT-ON* */
287
288
289
290 always_inline u16
291 get_current_policy_epoch (acl_main_t * am, int is_input, u32 sw_if_index0)
292 {
293   u32 **p_epoch_vec =
294     is_input ? &am->input_policy_epoch_by_sw_if_index :
295     &am->output_policy_epoch_by_sw_if_index;
296   u16 current_policy_epoch =
297     sw_if_index0 < vec_len (*p_epoch_vec) ? vec_elt (*p_epoch_vec,
298                                                      sw_if_index0)
299     : (is_input * FA_POLICY_EPOCH_IS_INPUT);
300   return current_policy_epoch;
301 }
302
303 always_inline void
304 maybe_trace_buffer (vlib_main_t * vm, vlib_node_runtime_t * node,
305                     vlib_buffer_t * b, u32 sw_if_index0, u32 lc_index0,
306                     u16 next0, int match_acl_in_index, int match_rule_index,
307                     fa_5tuple_t * fa_5tuple, u8 action, u32 trace_bitmap)
308 {
309   if (PREDICT_FALSE (b->flags & VLIB_BUFFER_IS_TRACED))
310     {
311       acl_fa_trace_t *t = vlib_add_trace (vm, node, b, sizeof (*t));
312       t->sw_if_index = sw_if_index0;
313       t->lc_index = lc_index0;
314       t->next_index = next0;
315       t->match_acl_in_index = match_acl_in_index;
316       t->match_rule_index = match_rule_index;
317       t->packet_info[0] = fa_5tuple->kv_40_8.key[0];
318       t->packet_info[1] = fa_5tuple->kv_40_8.key[1];
319       t->packet_info[2] = fa_5tuple->kv_40_8.key[2];
320       t->packet_info[3] = fa_5tuple->kv_40_8.key[3];
321       t->packet_info[4] = fa_5tuple->kv_40_8.key[4];
322       t->packet_info[5] = fa_5tuple->kv_40_8.value;
323       t->action = action;
324       t->trace_bitmap = trace_bitmap;
325     }
326 }
327
328
329 always_inline int
330 stale_session_deleted (acl_main_t * am, int is_input,
331                        acl_fa_per_worker_data_t * pw, u64 now,
332                        u32 sw_if_index0, fa_full_session_id_t f_sess_id)
333 {
334   u16 current_policy_epoch =
335     get_current_policy_epoch (am, is_input, sw_if_index0);
336
337   /* if the MSB of policy epoch matches but not the LSB means it is a stale session */
338   if ((0 ==
339        ((current_policy_epoch ^
340          f_sess_id.intf_policy_epoch) &
341         FA_POLICY_EPOCH_IS_INPUT))
342       && (current_policy_epoch != f_sess_id.intf_policy_epoch))
343     {
344       /* delete session and increment the counter */
345       vec_validate (pw->fa_session_epoch_change_by_sw_if_index, sw_if_index0);
346       vec_elt (pw->fa_session_epoch_change_by_sw_if_index, sw_if_index0)++;
347       if (acl_fa_conn_list_delete_session (am, f_sess_id, now))
348         {
349           /* delete the session only if we were able to unlink it */
350           acl_fa_two_stage_delete_session (am, sw_if_index0, f_sess_id, now);
351         }
352       return 1;
353     }
354   else
355     return 0;
356 }
357
358
359
360
361
362 always_inline void
363 get_sw_if_index_xN (int vector_sz, int is_input, vlib_buffer_t ** b,
364                     u32 * out_sw_if_index)
365 {
366   int ii;
367   for (ii = 0; ii < vector_sz; ii++)
368     if (is_input)
369       out_sw_if_index[ii] = vnet_buffer (b[ii])->sw_if_index[VLIB_RX];
370     else
371       out_sw_if_index[ii] = vnet_buffer (b[ii])->sw_if_index[VLIB_TX];
372 }
373
374 always_inline void
375 fill_5tuple_xN (int vector_sz, acl_main_t * am, int is_ip6, int is_input,
376                 int is_l2_path, vlib_buffer_t ** b, u32 * sw_if_index,
377                 fa_5tuple_t * out_fa_5tuple)
378 {
379   int ii;
380   for (ii = 0; ii < vector_sz; ii++)
381     acl_fill_5tuple (am, sw_if_index[ii], b[ii], is_ip6,
382                      is_input, is_l2_path, &out_fa_5tuple[ii]);
383 }
384
385 always_inline void
386 make_session_hash_xN (int vector_sz, acl_main_t * am, int is_ip6,
387                       u32 * sw_if_index, fa_5tuple_t * fa_5tuple,
388                       u64 * out_hash)
389 {
390   int ii;
391   for (ii = 0; ii < vector_sz; ii++)
392     out_hash[ii] =
393       acl_fa_make_session_hash (am, is_ip6, sw_if_index[ii], &fa_5tuple[ii]);
394 }
395
396 always_inline void
397 prefetch_session_entry (acl_main_t * am, fa_full_session_id_t f_sess_id)
398 {
399   fa_session_t *sess = get_session_ptr_no_check (am, f_sess_id.thread_index,
400                                                  f_sess_id.session_index);
401   CLIB_PREFETCH (sess, 2 * CLIB_CACHE_LINE_BYTES, STORE);
402 }
403
404 always_inline u8
405 process_established_session (vlib_main_t * vm, acl_main_t * am,
406                              u32 counter_node_index, int is_input, u64 now,
407                              fa_full_session_id_t f_sess_id,
408                              u32 * sw_if_index, fa_5tuple_t * fa_5tuple,
409                              u32 pkt_len, int node_trace_on,
410                              u32 * trace_bitmap)
411 {
412   u8 action = 0;
413   fa_session_t *sess = get_session_ptr_no_check (am, f_sess_id.thread_index,
414                                                  f_sess_id.session_index);
415
416   int old_timeout_type = fa_session_get_timeout_type (am, sess);
417   action =
418     acl_fa_track_session (am, is_input, sw_if_index[0], now,
419                           sess, &fa_5tuple[0], pkt_len);
420   int new_timeout_type = fa_session_get_timeout_type (am, sess);
421   /* Tracking might have changed the session timeout type, e.g. from transient to established */
422   if (PREDICT_FALSE (old_timeout_type != new_timeout_type))
423     {
424       acl_fa_restart_timer_for_session (am, now, f_sess_id);
425       vlib_node_increment_counter (vm, counter_node_index,
426                                    ACL_FA_ERROR_ACL_RESTART_SESSION_TIMER, 1);
427       if (node_trace_on)
428         *trace_bitmap |=
429           0x00010000 + ((0xff & old_timeout_type) << 8) +
430           (0xff & new_timeout_type);
431     }
432   /*
433    * I estimate the likelihood to be very low - the VPP needs
434    * to have >64K interfaces to start with and then on
435    * exactly 64K indices apart needs to be exactly the same
436    * 5-tuple... Anyway, since this probability is nonzero -
437    * print an error and drop the unlucky packet.
438    * If this shows up in real world, we would need to bump
439    * the hash key length.
440    */
441   if (PREDICT_FALSE (sess->sw_if_index != sw_if_index[0]))
442     {
443       clib_warning
444         ("BUG: session LSB16(sw_if_index)=%d and 5-tuple=%d collision!",
445          sess->sw_if_index, sw_if_index[0]);
446       action = 0;
447     }
448   return action;
449
450 }
451
452 #define ACL_PLUGIN_VECTOR_SIZE 4
453 #define ACL_PLUGIN_PREFETCH_GAP 3
454
455 always_inline uword
456 acl_fa_inner_node_fn (vlib_main_t * vm,
457                       vlib_node_runtime_t * node, vlib_frame_t * frame,
458                       int is_ip6, int is_input, int is_l2_path,
459                       int with_stateful_datapath, int node_trace_on,
460                       int reclassify_sessions)
461 {
462   u32 n_left, *from;
463   u32 pkts_exist_session = 0;
464   u32 pkts_new_session = 0;
465   u32 pkts_acl_permit = 0;
466   u32 trace_bitmap = 0;
467   acl_main_t *am = &acl_main;
468   vlib_node_runtime_t *error_node;
469   vlib_error_t no_error_existing_session;
470   u64 now = clib_cpu_time_now ();
471   uword thread_index = os_get_thread_index ();
472   acl_fa_per_worker_data_t *pw = &am->per_worker_data[thread_index];
473
474   u16 *next;
475   vlib_buffer_t **b;
476   u32 *sw_if_index;
477   fa_5tuple_t *fa_5tuple;
478   u64 *hash;
479
480
481
482   from = vlib_frame_vector_args (frame);
483   error_node = vlib_node_get_runtime (vm, node->node_index);
484   no_error_existing_session =
485     error_node->errors[ACL_FA_ERROR_ACL_EXIST_SESSION];
486
487   vlib_get_buffers (vm, from, pw->bufs, frame->n_vectors);
488
489   /* set the initial values for the current buffer the next pointers */
490   b = pw->bufs;
491   next = pw->nexts;
492   sw_if_index = pw->sw_if_indices;
493   fa_5tuple = pw->fa_5tuples;
494   hash = pw->hashes;
495
496
497   /*
498    * fill the sw_if_index, 5tuple and session hash,
499    * First in strides of size ACL_PLUGIN_VECTOR_SIZE,
500    * with buffer prefetch being
501    * ACL_PLUGIN_PREFETCH_GAP * ACL_PLUGIN_VECTOR_SIZE entries
502    * in front. Then with a simple single loop.
503    */
504
505   n_left = frame->n_vectors;
506   while (n_left >= (ACL_PLUGIN_PREFETCH_GAP + 1) * ACL_PLUGIN_VECTOR_SIZE)
507     {
508       const int vec_sz = ACL_PLUGIN_VECTOR_SIZE;
509       {
510         int ii;
511         for (ii = ACL_PLUGIN_PREFETCH_GAP * vec_sz;
512              ii < (ACL_PLUGIN_PREFETCH_GAP + 1) * vec_sz; ii++)
513           {
514             CLIB_PREFETCH (b[ii], CLIB_CACHE_LINE_BYTES, LOAD);
515             CLIB_PREFETCH (b[ii]->data, 2 * CLIB_CACHE_LINE_BYTES, LOAD);
516           }
517       }
518
519
520       get_sw_if_index_xN (vec_sz, is_input, b, sw_if_index);
521       fill_5tuple_xN (vec_sz, am, is_ip6, is_input, is_l2_path, &b[0],
522                       &sw_if_index[0], &fa_5tuple[0]);
523       if (with_stateful_datapath)
524         make_session_hash_xN (vec_sz, am, is_ip6, &sw_if_index[0],
525                               &fa_5tuple[0], &hash[0]);
526
527       n_left -= vec_sz;
528
529       fa_5tuple += vec_sz;
530       b += vec_sz;
531       sw_if_index += vec_sz;
532       hash += vec_sz;
533     }
534
535   while (n_left > 0)
536     {
537       const int vec_sz = 1;
538
539       get_sw_if_index_xN (vec_sz, is_input, b, sw_if_index);
540       fill_5tuple_xN (vec_sz, am, is_ip6, is_input, is_l2_path, &b[0],
541                       &sw_if_index[0], &fa_5tuple[0]);
542       if (with_stateful_datapath)
543         make_session_hash_xN (vec_sz, am, is_ip6, &sw_if_index[0],
544                               &fa_5tuple[0], &hash[0]);
545
546       n_left -= vec_sz;
547
548       fa_5tuple += vec_sz;
549       b += vec_sz;
550       sw_if_index += vec_sz;
551       hash += vec_sz;
552     }
553
554   b = pw->bufs;
555   next = pw->nexts;
556   sw_if_index = pw->sw_if_indices;
557   fa_5tuple = pw->fa_5tuples;
558   hash = pw->hashes;
559
560   /*
561    * Now the "hard" work of session lookups and ACL lookups for new sessions.
562    * Due to the complexity, do it for the time being in single loop with
563    * the pipeline of three prefetches:
564    *    1) bucket for the session bihash
565    *    2) data for the session bihash
566    *    3) worker session record
567    */
568
569   fa_full_session_id_t f_sess_id_next = {.as_u64 = ~0ULL };
570
571   /* find the "next" session so we can kickstart the pipeline */
572   if (with_stateful_datapath)
573     acl_fa_find_session_with_hash (am, is_ip6, sw_if_index[0], hash[0],
574                                    &fa_5tuple[0], &f_sess_id_next.as_u64);
575
576   n_left = frame->n_vectors;
577   while (n_left > 0)
578     {
579       u8 action = 0;
580       u32 lc_index0 = ~0;
581       int acl_check_needed = 1;
582       u32 match_acl_in_index = ~0;
583       u32 match_acl_pos = ~0;
584       u32 match_rule_index = ~0;
585
586       next[0] = 0;              /* drop by default */
587
588       /* Try to match an existing session first */
589
590       if (with_stateful_datapath)
591         {
592           fa_full_session_id_t f_sess_id = f_sess_id_next;
593           switch (n_left)
594             {
595             default:
596               acl_fa_prefetch_session_bucket_for_hash (am, is_ip6, hash[5]);
597               /* fallthrough */
598             case 5:
599             case 4:
600               acl_fa_prefetch_session_data_for_hash (am, is_ip6, hash[3]);
601               /* fallthrough */
602             case 3:
603             case 2:
604               acl_fa_find_session_with_hash (am, is_ip6, sw_if_index[1],
605                                              hash[1], &fa_5tuple[1],
606                                              &f_sess_id_next.as_u64);
607               if (f_sess_id_next.as_u64 != ~0ULL)
608                 {
609                   prefetch_session_entry (am, f_sess_id_next);
610                 }
611               /* fallthrough */
612             case 1:
613               if (f_sess_id.as_u64 != ~0ULL)
614                 {
615                   if (node_trace_on)
616                     {
617                       trace_bitmap |= 0x80000000;
618                     }
619                   ASSERT (f_sess_id.thread_index < vec_len (vlib_mains));
620                   b[0]->error = no_error_existing_session;
621                   acl_check_needed = 0;
622                   pkts_exist_session += 1;
623                   action =
624                     process_established_session (vm, am, node->node_index,
625                                                  is_input, now, f_sess_id,
626                                                  &sw_if_index[0],
627                                                  &fa_5tuple[0],
628                                                  b[0]->current_length,
629                                                  node_trace_on,
630                                                  &trace_bitmap);
631
632                   /* expose the session id to the tracer */
633                   if (node_trace_on)
634                     {
635                       match_rule_index = f_sess_id.session_index;
636                     }
637
638                   if (reclassify_sessions)
639                     {
640                       if (PREDICT_FALSE
641                           (stale_session_deleted
642                            (am, is_input, pw, now, sw_if_index[0],
643                             f_sess_id)))
644                         {
645                           acl_check_needed = 1;
646                           if (node_trace_on)
647                             {
648                               trace_bitmap |= 0x40000000;
649                             }
650                           /*
651                            * If we have just deleted the session, and the next
652                            * buffer is the same 5-tuple, that session prediction
653                            * is wrong, correct it.
654                            */
655                           if ((f_sess_id_next.as_u64 != ~0ULL)
656                               && 0 == memcmp (&fa_5tuple[1], &fa_5tuple[0],
657                                               sizeof (fa_5tuple[1])))
658                             f_sess_id_next.as_u64 = ~0ULL;
659                         }
660                     }
661                 }
662             }
663
664           if (acl_check_needed)
665             {
666               if (is_input)
667                 lc_index0 = am->input_lc_index_by_sw_if_index[sw_if_index[0]];
668               else
669                 lc_index0 =
670                   am->output_lc_index_by_sw_if_index[sw_if_index[0]];
671
672               action = 0;       /* deny by default */
673               acl_plugin_match_5tuple_inline (am, lc_index0,
674                                               (fa_5tuple_opaque_t *) &
675                                               fa_5tuple[0], is_ip6, &action,
676                                               &match_acl_pos,
677                                               &match_acl_in_index,
678                                               &match_rule_index,
679                                               &trace_bitmap);
680               b[0]->error = error_node->errors[action];
681
682               if (1 == action)
683                 pkts_acl_permit++;
684
685               if (2 == action)
686                 {
687                   if (!acl_fa_can_add_session (am, is_input, sw_if_index[0]))
688                     acl_fa_try_recycle_session (am, is_input,
689                                                 thread_index,
690                                                 sw_if_index[0], now);
691
692                   if (acl_fa_can_add_session (am, is_input, sw_if_index[0]))
693                     {
694                       u16 current_policy_epoch =
695                         get_current_policy_epoch (am, is_input,
696                                                   sw_if_index[0]);
697                       fa_full_session_id_t f_sess_id =
698                         acl_fa_add_session (am, is_input, is_ip6,
699                                             sw_if_index[0],
700                                             now, &fa_5tuple[0],
701                                             current_policy_epoch);
702
703                       /* perform the accounting for the newly added session */
704                       process_established_session (vm, am,
705                                                    node->node_index,
706                                                    is_input, now,
707                                                    f_sess_id,
708                                                    &sw_if_index[0],
709                                                    &fa_5tuple[0],
710                                                    b[0]->current_length,
711                                                    node_trace_on,
712                                                    &trace_bitmap);
713                       pkts_new_session++;
714                       /*
715                        * If the next 5tuple is the same and we just added the session,
716                        * the f_sess_id_next can not be ~0. Correct it.
717                        */
718                       if ((f_sess_id_next.as_u64 == ~0ULL)
719                           && 0 == memcmp (&fa_5tuple[1], &fa_5tuple[0],
720                                           sizeof (fa_5tuple[1])))
721                         f_sess_id_next = f_sess_id;
722                     }
723                   else
724                     {
725                       action = 0;
726                       b[0]->error =
727                         error_node->errors
728                         [ACL_FA_ERROR_ACL_TOO_MANY_SESSIONS];
729                     }
730                 }
731
732             }
733
734           {
735             u32 next0;
736             /* speculatively get the next0 */
737             vnet_feature_next (&next0, b[0]);
738             /* if the action is not deny - then use that next */
739             next[0] = action ? next0 : 0;
740           }
741
742           if (node_trace_on)    // PREDICT_FALSE (node->flags & VLIB_NODE_FLAG_TRACE))
743             {
744               maybe_trace_buffer (vm, node, b[0], sw_if_index[0], lc_index0,
745                                   next[0], match_acl_in_index,
746                                   match_rule_index, &fa_5tuple[0], action,
747                                   trace_bitmap);
748             }
749
750           next++;
751           b++;
752           fa_5tuple++;
753           sw_if_index++;
754           hash++;
755           n_left -= 1;
756         }
757     }
758
759   vlib_buffer_enqueue_to_next (vm, node, from, pw->nexts, frame->n_vectors);
760
761   vlib_node_increment_counter (vm, node->node_index,
762                                ACL_FA_ERROR_ACL_CHECK, frame->n_vectors);
763   vlib_node_increment_counter (vm, node->node_index,
764                                ACL_FA_ERROR_ACL_EXIST_SESSION,
765                                pkts_exist_session);
766   vlib_node_increment_counter (vm, node->node_index,
767                                ACL_FA_ERROR_ACL_NEW_SESSION,
768                                pkts_new_session);
769   vlib_node_increment_counter (vm, node->node_index,
770                                ACL_FA_ERROR_ACL_PERMIT, pkts_acl_permit);
771   return frame->n_vectors;
772 }
773
774 always_inline uword
775 acl_fa_outer_node_fn (vlib_main_t * vm,
776                       vlib_node_runtime_t * node, vlib_frame_t * frame,
777                       int is_ip6, int is_input, int is_l2_path,
778                       int do_reclassify)
779 {
780   acl_main_t *am = &acl_main;
781
782   if (am->fa_sessions_hash_is_initialized)
783     {
784       if (PREDICT_FALSE (node->flags & VLIB_NODE_FLAG_TRACE))
785         return acl_fa_inner_node_fn (vm, node, frame, is_ip6, is_input,
786                                      is_l2_path, 1 /* stateful */ ,
787                                      1 /* trace */ ,
788                                      do_reclassify);
789       else
790         return acl_fa_inner_node_fn (vm, node, frame, is_ip6, is_input,
791                                      is_l2_path, 1 /* stateful */ , 0,
792                                      do_reclassify);
793     }
794   else
795     {
796       if (PREDICT_FALSE (node->flags & VLIB_NODE_FLAG_TRACE))
797         return acl_fa_inner_node_fn (vm, node, frame, is_ip6, is_input,
798                                      is_l2_path, 0 /* no state */ ,
799                                      1 /* trace */ ,
800                                      do_reclassify);
801       else
802         return acl_fa_inner_node_fn (vm, node, frame, is_ip6, is_input,
803                                      is_l2_path, 0 /* no state */ , 0,
804                                      do_reclassify);
805     }
806 }
807
808 always_inline uword
809 acl_fa_node_fn (vlib_main_t * vm,
810                 vlib_node_runtime_t * node, vlib_frame_t * frame, int is_ip6,
811                 int is_input, int is_l2_path)
812 {
813   /* select the reclassify/no-reclassify version of the datapath */
814   acl_main_t *am = &acl_main;
815
816   if (am->reclassify_sessions)
817     return acl_fa_outer_node_fn (vm, node, frame, is_ip6, is_input,
818                                  is_l2_path, 1);
819   else
820     return acl_fa_outer_node_fn (vm, node, frame, is_ip6, is_input,
821                                  is_l2_path, 0);
822 }
823
824 VLIB_NODE_FN (acl_in_l2_ip6_node) (vlib_main_t * vm,
825                                    vlib_node_runtime_t * node,
826                                    vlib_frame_t * frame)
827 {
828   return acl_fa_node_fn (vm, node, frame, 1, 1, 1);
829 }
830
831 VLIB_NODE_FN (acl_in_l2_ip4_node) (vlib_main_t * vm,
832                                    vlib_node_runtime_t * node,
833                                    vlib_frame_t * frame)
834 {
835   return acl_fa_node_fn (vm, node, frame, 0, 1, 1);
836 }
837
838 VLIB_NODE_FN (acl_out_l2_ip6_node) (vlib_main_t * vm,
839                                     vlib_node_runtime_t * node,
840                                     vlib_frame_t * frame)
841 {
842   return acl_fa_node_fn (vm, node, frame, 1, 0, 1);
843 }
844
845 VLIB_NODE_FN (acl_out_l2_ip4_node) (vlib_main_t * vm,
846                                     vlib_node_runtime_t * node,
847                                     vlib_frame_t * frame)
848 {
849   return acl_fa_node_fn (vm, node, frame, 0, 0, 1);
850 }
851
852 /**** L3 processing path nodes ****/
853
854 VLIB_NODE_FN (acl_in_fa_ip6_node) (vlib_main_t * vm,
855                                    vlib_node_runtime_t * node,
856                                    vlib_frame_t * frame)
857 {
858   return acl_fa_node_fn (vm, node, frame, 1, 1, 0);
859 }
860
861 VLIB_NODE_FN (acl_in_fa_ip4_node) (vlib_main_t * vm,
862                                    vlib_node_runtime_t * node,
863                                    vlib_frame_t * frame)
864 {
865   return acl_fa_node_fn (vm, node, frame, 0, 1, 0);
866 }
867
868 VLIB_NODE_FN (acl_out_fa_ip6_node) (vlib_main_t * vm,
869                                     vlib_node_runtime_t * node,
870                                     vlib_frame_t * frame)
871 {
872   return acl_fa_node_fn (vm, node, frame, 1, 0, 0);
873 }
874
875 VLIB_NODE_FN (acl_out_fa_ip4_node) (vlib_main_t * vm,
876                                     vlib_node_runtime_t * node,
877                                     vlib_frame_t * frame)
878 {
879   return acl_fa_node_fn (vm, node, frame, 0, 0, 0);
880 }
881
882 static u8 *
883 format_fa_5tuple (u8 * s, va_list * args)
884 {
885   fa_5tuple_t *p5t = va_arg (*args, fa_5tuple_t *);
886   void *paddr0;
887   void *paddr1;
888   void *format_address_func;
889   void *ip_af;
890   void *ip_frag_txt =
891     p5t->pkt.is_nonfirst_fragment ? " non-initial fragment" : "";
892
893   if (p5t->pkt.is_ip6)
894     {
895       ip_af = "ip6";
896       format_address_func = format_ip6_address;
897       paddr0 = &p5t->ip6_addr[0];
898       paddr1 = &p5t->ip6_addr[1];
899     }
900   else
901     {
902       ip_af = "ip4";
903       format_address_func = format_ip4_address;
904       paddr0 = &p5t->ip4_addr[0];
905       paddr1 = &p5t->ip4_addr[1];
906     }
907
908   s =
909     format (s, "lc_index %d l3 %s%s ", p5t->pkt.lc_index, ip_af, ip_frag_txt);
910   s =
911     format (s, "%U -> %U ", format_address_func, paddr0, format_address_func,
912             paddr1);
913   s = format (s, "%U ", format_fa_session_l4_key, &p5t->l4);
914   s = format (s, "tcp flags (%s) %02x rsvd %x",
915               p5t->pkt.tcp_flags_valid ? "valid" : "invalid",
916               p5t->pkt.tcp_flags, p5t->pkt.flags_reserved);
917   return s;
918 }
919
920 #ifndef CLIB_MARCH_VARIANT
921 u8 *
922 format_acl_plugin_5tuple (u8 * s, va_list * args)
923 {
924   return format_fa_5tuple (s, args);
925 }
926 #endif
927
928 /* packet trace format function */
929 static u8 *
930 format_acl_plugin_trace (u8 * s, va_list * args)
931 {
932   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
933   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
934   acl_fa_trace_t *t = va_arg (*args, acl_fa_trace_t *);
935
936   s =
937     format (s,
938             "acl-plugin: lc_index: %d, sw_if_index %d, next index %d, action: %d, match: acl %d rule %d trace_bits %08x\n"
939             "  pkt info %016llx %016llx %016llx %016llx %016llx %016llx",
940             t->lc_index, t->sw_if_index, t->next_index, t->action,
941             t->match_acl_in_index, t->match_rule_index, t->trace_bitmap,
942             t->packet_info[0], t->packet_info[1], t->packet_info[2],
943             t->packet_info[3], t->packet_info[4], t->packet_info[5]);
944
945   /* Now also print out the packet_info in a form usable by humans */
946   s = format (s, "\n   %U", format_fa_5tuple, t->packet_info);
947   return s;
948 }
949
950 /* *INDENT-OFF* */
951
952 static char *acl_fa_error_strings[] = {
953 #define _(sym,string) string,
954   foreach_acl_fa_error
955 #undef _
956 };
957
958 VLIB_REGISTER_NODE (acl_in_l2_ip6_node) =
959 {
960   .name = "acl-plugin-in-ip6-l2",
961   .vector_size = sizeof (u32),
962   .format_trace = format_acl_plugin_trace,
963   .type = VLIB_NODE_TYPE_INTERNAL,
964   .n_errors = ARRAY_LEN (acl_fa_error_strings),
965   .error_strings = acl_fa_error_strings,
966   .n_next_nodes = ACL_FA_N_NEXT,
967   .next_nodes =
968   {
969     [ACL_FA_ERROR_DROP] = "error-drop",
970   }
971 };
972
973 VNET_FEATURE_INIT (acl_in_l2_ip6_fa_feature, static) =
974 {
975   .arc_name = "l2-input-ip6",
976   .node_name = "acl-plugin-in-ip6-l2",
977   .runs_before = VNET_FEATURES ("l2-input-feat-arc-end"),
978 };
979
980 VLIB_REGISTER_NODE (acl_in_l2_ip4_node) =
981 {
982   .name = "acl-plugin-in-ip4-l2",
983   .vector_size = sizeof (u32),
984   .format_trace = format_acl_plugin_trace,
985   .type = VLIB_NODE_TYPE_INTERNAL,
986   .n_errors = ARRAY_LEN (acl_fa_error_strings),
987   .error_strings = acl_fa_error_strings,
988   .n_next_nodes = ACL_FA_N_NEXT,
989   .next_nodes =
990   {
991     [ACL_FA_ERROR_DROP] = "error-drop",
992   }
993 };
994
995 VNET_FEATURE_INIT (acl_in_l2_ip4_fa_feature, static) =
996 {
997   .arc_name = "l2-input-ip4",
998   .node_name = "acl-plugin-in-ip4-l2",
999   .runs_before = VNET_FEATURES ("l2-input-feat-arc-end"),
1000 };
1001
1002
1003 VLIB_REGISTER_NODE (acl_out_l2_ip6_node) =
1004 {
1005   .name = "acl-plugin-out-ip6-l2",
1006   .vector_size = sizeof (u32),
1007   .format_trace = format_acl_plugin_trace,
1008   .type = VLIB_NODE_TYPE_INTERNAL,
1009   .n_errors = ARRAY_LEN (acl_fa_error_strings),
1010   .error_strings = acl_fa_error_strings,
1011   .n_next_nodes = ACL_FA_N_NEXT,
1012   .next_nodes =
1013   {
1014     [ACL_FA_ERROR_DROP] = "error-drop",
1015   }
1016 };
1017
1018 VNET_FEATURE_INIT (acl_out_l2_ip6_fa_feature, static) =
1019 {
1020   .arc_name = "l2-output-ip6",
1021   .node_name = "acl-plugin-out-ip6-l2",
1022   .runs_before = VNET_FEATURES ("l2-output-feat-arc-end"),
1023 };
1024
1025
1026 VLIB_REGISTER_NODE (acl_out_l2_ip4_node) =
1027 {
1028   .name = "acl-plugin-out-ip4-l2",
1029   .vector_size = sizeof (u32),
1030   .format_trace = format_acl_plugin_trace,
1031   .type = VLIB_NODE_TYPE_INTERNAL,
1032   .n_errors = ARRAY_LEN (acl_fa_error_strings),
1033   .error_strings = acl_fa_error_strings,
1034   .n_next_nodes = ACL_FA_N_NEXT,
1035   .next_nodes =
1036   {
1037     [ACL_FA_ERROR_DROP] = "error-drop",
1038   }
1039 };
1040
1041 VNET_FEATURE_INIT (acl_out_l2_ip4_fa_feature, static) =
1042 {
1043   .arc_name = "l2-output-ip4",
1044   .node_name = "acl-plugin-out-ip4-l2",
1045   .runs_before = VNET_FEATURES ("l2-output-feat-arc-end"),
1046 };
1047
1048
1049 VLIB_REGISTER_NODE (acl_in_fa_ip6_node) =
1050 {
1051   .name = "acl-plugin-in-ip6-fa",
1052   .vector_size = sizeof (u32),
1053   .format_trace = format_acl_plugin_trace,
1054   .type = VLIB_NODE_TYPE_INTERNAL,
1055   .n_errors = ARRAY_LEN (acl_fa_error_strings),
1056   .error_strings = acl_fa_error_strings,
1057   .n_next_nodes = ACL_FA_N_NEXT,
1058   .next_nodes =
1059   {
1060     [ACL_FA_ERROR_DROP] = "error-drop",
1061   }
1062 };
1063
1064 VNET_FEATURE_INIT (acl_in_ip6_fa_feature, static) =
1065 {
1066   .arc_name = "ip6-unicast",
1067   .node_name = "acl-plugin-in-ip6-fa",
1068   .runs_before = VNET_FEATURES ("ip6-flow-classify"),
1069 };
1070
1071 VLIB_REGISTER_NODE (acl_in_fa_ip4_node) =
1072 {
1073   .name = "acl-plugin-in-ip4-fa",
1074   .vector_size = sizeof (u32),
1075   .format_trace = format_acl_plugin_trace,
1076   .type = VLIB_NODE_TYPE_INTERNAL,
1077   .n_errors = ARRAY_LEN (acl_fa_error_strings),
1078   .error_strings = acl_fa_error_strings,
1079   .n_next_nodes = ACL_FA_N_NEXT,
1080   .next_nodes =
1081   {
1082     [ACL_FA_ERROR_DROP] = "error-drop",
1083   }
1084 };
1085
1086 VNET_FEATURE_INIT (acl_in_ip4_fa_feature, static) =
1087 {
1088   .arc_name = "ip4-unicast",
1089   .node_name = "acl-plugin-in-ip4-fa",
1090   .runs_before = VNET_FEATURES ("ip4-flow-classify"),
1091 };
1092
1093
1094 VLIB_REGISTER_NODE (acl_out_fa_ip6_node) =
1095 {
1096   .name = "acl-plugin-out-ip6-fa",
1097   .vector_size = sizeof (u32),
1098   .format_trace = format_acl_plugin_trace,
1099   .type = VLIB_NODE_TYPE_INTERNAL,
1100   .n_errors = ARRAY_LEN (acl_fa_error_strings),
1101   .error_strings = acl_fa_error_strings,
1102   .n_next_nodes = ACL_FA_N_NEXT,
1103   .next_nodes =
1104   {
1105     [ACL_FA_ERROR_DROP] = "error-drop",
1106   }
1107 };
1108
1109 VNET_FEATURE_INIT (acl_out_ip6_fa_feature, static) =
1110 {
1111   .arc_name = "ip6-output",
1112   .node_name = "acl-plugin-out-ip6-fa",
1113   .runs_before = VNET_FEATURES ("interface-output"),
1114 };
1115
1116 VLIB_REGISTER_NODE (acl_out_fa_ip4_node) =
1117 {
1118   .name = "acl-plugin-out-ip4-fa",
1119   .vector_size = sizeof (u32),
1120   .format_trace = format_acl_plugin_trace,
1121   .type = VLIB_NODE_TYPE_INTERNAL,
1122   .n_errors = ARRAY_LEN (acl_fa_error_strings),
1123   .error_strings = acl_fa_error_strings,
1124   .n_next_nodes = ACL_FA_N_NEXT,
1125     /* edit / add dispositions here */
1126   .next_nodes =
1127   {
1128     [ACL_FA_ERROR_DROP] = "error-drop",
1129   }
1130 };
1131
1132 VNET_FEATURE_INIT (acl_out_ip4_fa_feature, static) =
1133 {
1134   .arc_name = "ip4-output",
1135   .node_name = "acl-plugin-out-ip4-fa",
1136   .runs_before = VNET_FEATURES ("interface-output"),
1137 };
1138
1139 /* *INDENT-ON* */
1140
1141 /*
1142  * fd.io coding-style-patch-verification: ON
1143  *
1144  * Local Variables:
1145  * eval: (c-set-style "gnu")
1146  * End:
1147  */