acl-plugin: multicore: session management fixes
[vpp.git] / src / plugins / acl / fa_node.h
1 #ifndef _FA_NODE_H_
2 #define _FA_NODE_H_
3
4 #include <stddef.h>
5 #include <vppinfra/bihash_40_8.h>
6
7 // #define FA_NODE_VERBOSE_DEBUG 3
8
9 #define TCP_FLAG_FIN    0x01
10 #define TCP_FLAG_SYN    0x02
11 #define TCP_FLAG_RST    0x04
12 #define TCP_FLAG_PUSH   0x08
13 #define TCP_FLAG_ACK    0x10
14 #define TCP_FLAG_URG    0x20
15 #define TCP_FLAG_ECE    0x40
16 #define TCP_FLAG_CWR    0x80
17 #define TCP_FLAGS_RSTFINACKSYN (TCP_FLAG_RST + TCP_FLAG_FIN + TCP_FLAG_SYN + TCP_FLAG_ACK)
18 #define TCP_FLAGS_ACKSYN (TCP_FLAG_SYN + TCP_FLAG_ACK)
19
20 #define ACL_FA_CONN_TABLE_DEFAULT_HASH_NUM_BUCKETS (64 * 1024)
21 #define ACL_FA_CONN_TABLE_DEFAULT_HASH_MEMORY_SIZE (1ULL<<30)
22 #define ACL_FA_CONN_TABLE_DEFAULT_MAX_ENTRIES 500000
23
24 typedef union {
25   u64 as_u64;
26   struct {
27     u32 lc_index;
28     u16 mask_type_index_lsb;
29     u8 tcp_flags;
30     u8 tcp_flags_valid:1;
31     u8 l4_valid:1;
32     u8 is_nonfirst_fragment:1;
33     u8 is_ip6:1;
34     u8 flags_reserved:4;
35   };
36 } fa_packet_info_t;
37
38 typedef union {
39   u64 as_u64;
40   struct {
41     u16 port[2];
42     union {
43       struct {
44         u8 proto;
45         u8 is_input: 1;
46         u8 is_slowpath: 1;
47         u8 reserved0: 6;
48         u16 lsb_of_sw_if_index;
49       };
50       u32 non_port_l4_data;
51     };
52   };
53 } fa_session_l4_key_t;
54
55 typedef union {
56   struct {
57     ip46_address_t addr[2];
58     fa_session_l4_key_t l4;
59     /* This field should align with u64 value in bihash_40_8 keyvalue struct */
60     fa_packet_info_t pkt;
61   };
62   clib_bihash_kv_40_8_t kv;
63 } fa_5tuple_t;
64
65 typedef struct {
66   u8 opaque[sizeof(fa_5tuple_t)];
67 } fa_5tuple_opaque_t;
68
69
70 typedef struct {
71   fa_5tuple_t info; /* (5+1)*8 = 48 bytes */
72   u64 last_active_time;   /* +8 bytes = 56 */
73   u32 sw_if_index;        /* +4 bytes = 60 */
74   union {
75     u8 as_u8[2];
76     u16 as_u16;
77   } tcp_flags_seen; ;     /* +2 bytes = 62 */
78   u16 thread_index;          /* +2 bytes = 64 */
79   u64 link_enqueue_time;  /* 8 byte = 8 */
80   u32 link_prev_idx;      /* +4 bytes = 12 */
81   u32 link_next_idx;      /* +4 bytes = 16 */
82   u8 link_list_id;        /* +1 bytes = 17 */
83   u8 deleted;             /* +1 bytes = 18 */
84   u8 reserved1[6];        /* +6 bytes = 24 */
85   u64 reserved2[5];       /* +5*8 bytes = 64 */
86 } fa_session_t;
87
88 #define FA_POLICY_EPOCH_MASK 0x7fff
89 /* input policy epochs have the MSB set */
90 #define FA_POLICY_EPOCH_IS_INPUT 0x8000
91
92
93 /* This structure is used to fill in the u64 value
94    in the per-sw-if-index hash table */
95 typedef struct {
96   union {
97     u64 as_u64;
98     struct {
99       u32 session_index;
100       u16 thread_index;
101       u16 intf_policy_epoch;
102     };
103   };
104 } fa_full_session_id_t;
105
106 /*
107  * A few compile-time constraints on the size and the layout of the union, to ensure
108  * it makes sense both for bihash and for us.
109  */
110
111 #define CT_ASSERT_EQUAL(name, x,y) typedef int assert_ ## name ## _compile_time_assertion_failed[((x) == (y))-1]
112 CT_ASSERT_EQUAL(fa_l3_key_size_is_40, offsetof(fa_5tuple_t, pkt), offsetof(clib_bihash_kv_40_8_t, value));
113 CT_ASSERT_EQUAL(fa_l4_key_t_is_8, sizeof(fa_session_l4_key_t), sizeof(u64));
114 CT_ASSERT_EQUAL(fa_packet_info_t_is_8, sizeof(fa_packet_info_t), sizeof(u64));
115 CT_ASSERT_EQUAL(fa_l3_kv_size_is_48, sizeof(fa_5tuple_t), sizeof(clib_bihash_kv_40_8_t));
116
117 /* Let's try to fit within two cachelines */
118 CT_ASSERT_EQUAL(fa_session_t_size_is_128, sizeof(fa_session_t), 128);
119
120 /* Session ID MUST be the same as u64 */
121 CT_ASSERT_EQUAL(fa_full_session_id_size_is_64, sizeof(fa_full_session_id_t), sizeof(u64));
122 #undef CT_ASSERT_EQUAL
123
124 #define FA_SESSION_BOGUS_INDEX ~0
125
126 typedef struct {
127   /* The pool of sessions managed by this worker */
128   fa_session_t *fa_sessions_pool;
129   /* per-worker ACL_N_TIMEOUTS of conn lists */
130   u32 *fa_conn_list_head;
131   u32 *fa_conn_list_tail;
132   /* expiry time set whenever an element is enqueued */
133   u64 *fa_conn_list_head_expiry_time;
134   /* adds and deletes per-worker-per-interface */
135   u64 *fa_session_dels_by_sw_if_index;
136   u64 *fa_session_adds_by_sw_if_index;
137   /* sessions deleted due to epoch change */
138   u64 *fa_session_epoch_change_by_sw_if_index;
139   /* Vector of expired connections retrieved from lists */
140   u32 *expired;
141   /* the earliest next expiry time */
142   u64 next_expiry_time;
143   /* if not zero, look at all the elements until their enqueue timestamp is after below one */
144   u64 requeue_until_time;
145   /* Current time between the checks */
146   u64 current_time_wait_interval;
147   /* Counter of how many sessions we did delete */
148   u64 cnt_deleted_sessions;
149   /* Counter of already deleted sessions being deleted - should not increment unless a bug */
150   u64 cnt_already_deleted_sessions;
151   /* Number of times we requeued a session to a head of the list */
152   u64 cnt_session_timer_restarted;
153   /* swipe up to this enqueue time, rather than following the timeouts */
154   u64 swipe_end_time;
155   /* bitmap of sw_if_index serviced by this worker */
156   uword *serviced_sw_if_index_bitmap;
157   /* bitmap of sw_if_indices to clear. set by main thread, cleared by worker */
158   uword *pending_clear_sw_if_index_bitmap;
159   /* atomic, indicates that the swipe-deletion of connections is in progress */
160   u32 clear_in_process;
161   /* Interrupt is pending from main thread */
162   int interrupt_is_pending;
163   /*
164    * Interrupt node on the worker thread sets this if it knows there is
165    * more work to do, but it has to finish to avoid hogging the
166    * core for too long.
167    */
168   int interrupt_is_needed;
169   /*
170    * Set to indicate that the interrupt node wants to get less interrupts
171    * because there is not enough work for the current rate.
172    */
173   int interrupt_is_unwanted;
174   /*
175    * Set to copy of a "generation" counter in main thread so we can sync the interrupts.
176    */
177   int interrupt_generation;
178 } acl_fa_per_worker_data_t;
179
180
181 typedef enum {
182   ACL_FA_ERROR_DROP,
183   ACL_FA_N_NEXT,
184 } acl_fa_next_t;
185
186
187 enum
188 {
189   ACL_FA_CLEANER_RESCHEDULE = 1,
190   ACL_FA_CLEANER_DELETE_BY_SW_IF_INDEX,
191 } acl_fa_cleaner_process_event_e;
192
193 void acl_fa_enable_disable(u32 sw_if_index, int is_input, int enable_disable);
194
195 void show_fa_sessions_hash(vlib_main_t * vm, u32 verbose);
196
197 u8 *format_acl_plugin_5tuple (u8 * s, va_list * args);
198
199 /* use like: elog_acl_maybe_trace_X1(am, "foobar: %d", "i4", int32_value); */
200
201 #define elog_acl_maybe_trace_X1(am, acl_elog_trace_format_label, acl_elog_trace_format_args, acl_elog_val1)              \
202 do {                                                                                                                     \
203   if (am->trace_sessions) {                                                                                              \
204     CLIB_UNUSED(struct { u8 available_space[18 - sizeof(acl_elog_val1)]; } *static_check);                               \
205     u16 thread_index = os_get_thread_index ();                                                                           \
206     vlib_worker_thread_t * w = vlib_worker_threads + thread_index;                                                       \
207     ELOG_TYPE_DECLARE (e) =                                                                                              \
208       {                                                                                                                  \
209         .format = "(%02d) " acl_elog_trace_format_label,                                                                 \
210         .format_args = "i2" acl_elog_trace_format_args,                                                                  \
211       };                                                                                                                 \
212     CLIB_PACKED(struct                                                                                                   \
213       {                                                                                                                  \
214         u16 thread;                                                                                                      \
215         typeof(acl_elog_val1) val1;                                                                                      \
216       }) *ed;                                                                                                            \
217     ed = ELOG_TRACK_DATA (&vlib_global_main.elog_main, e, w->elog_track);                                                \
218     ed->thread = thread_index;                                                                                           \
219     ed->val1 = acl_elog_val1;                                                                                            \
220   }                                                                                                                      \
221 } while (0)
222
223
224 /* use like: elog_acl_maybe_trace_X2(am, "foobar: %d some u64: %lu", "i4i8", int32_value, int64_value); */
225
226 #define elog_acl_maybe_trace_X2(am, acl_elog_trace_format_label, acl_elog_trace_format_args,                             \
227                                                                                            acl_elog_val1, acl_elog_val2) \
228 do {                                                                                                                     \
229   if (am->trace_sessions) {                                                                                              \
230     CLIB_UNUSED(struct { u8 available_space[18 - sizeof(acl_elog_val1) - sizeof(acl_elog_val2)]; } *static_check);       \
231     u16 thread_index = os_get_thread_index ();                                                                           \
232     vlib_worker_thread_t * w = vlib_worker_threads + thread_index;                                                       \
233     ELOG_TYPE_DECLARE (e) =                                                                                              \
234       {                                                                                                                  \
235         .format = "(%02d) " acl_elog_trace_format_label,                                                                 \
236         .format_args = "i2" acl_elog_trace_format_args,                                                                  \
237       };                                                                                                                 \
238     CLIB_PACKED(struct                                                                                                   \
239       {                                                                                                                  \
240         u16 thread;                                                                                                      \
241         typeof(acl_elog_val1) val1;                                                                                      \
242         typeof(acl_elog_val2) val2;                                                                                      \
243       }) *ed;                                                                                                            \
244     ed = ELOG_TRACK_DATA (&vlib_global_main.elog_main, e, w->elog_track);                                                \
245     ed->thread = thread_index;                                                                                           \
246     ed->val1 = acl_elog_val1;                                                                                            \
247     ed->val2 = acl_elog_val2;                                                                                            \
248   }                                                                                                                      \
249 } while (0)
250
251
252 /* use like: elog_acl_maybe_trace_X3(am, "foobar: %d some u64 %lu baz: %d", "i4i8i4", int32_value, u64_value, int_value); */
253
254 #define elog_acl_maybe_trace_X3(am, acl_elog_trace_format_label, acl_elog_trace_format_args, acl_elog_val1,              \
255                                                                                            acl_elog_val2, acl_elog_val3) \
256 do {                                                                                                                     \
257   if (am->trace_sessions) {                                                                                              \
258     CLIB_UNUSED(struct { u8 available_space[18 - sizeof(acl_elog_val1) - sizeof(acl_elog_val2)                           \
259                                                - sizeof(acl_elog_val3)]; } *static_check);                               \
260     u16 thread_index = os_get_thread_index ();                                                                           \
261     vlib_worker_thread_t * w = vlib_worker_threads + thread_index;                                                       \
262     ELOG_TYPE_DECLARE (e) =                                                                                              \
263       {                                                                                                                  \
264         .format = "(%02d) " acl_elog_trace_format_label,                                                                 \
265         .format_args = "i2" acl_elog_trace_format_args,                                                                  \
266       };                                                                                                                 \
267     CLIB_PACKED(struct                                                                                                   \
268       {                                                                                                                  \
269         u16 thread;                                                                                                      \
270         typeof(acl_elog_val1) val1;                                                                                      \
271         typeof(acl_elog_val2) val2;                                                                                      \
272         typeof(acl_elog_val3) val3;                                                                                      \
273       }) *ed;                                                                                                            \
274     ed = ELOG_TRACK_DATA (&vlib_global_main.elog_main, e, w->elog_track);                                                \
275     ed->thread = thread_index;                                                                                           \
276     ed->val1 = acl_elog_val1;                                                                                            \
277     ed->val2 = acl_elog_val2;                                                                                            \
278     ed->val3 = acl_elog_val3;                                                                                            \
279   }                                                                                                                      \
280 } while (0)
281
282
283 /* use like: elog_acl_maybe_trace_X4(am, "foobar: %d some int %d baz: %d bar: %d", "i4i4i4i4", int32_value, int32_value2, int_value, int_value); */
284
285 #define elog_acl_maybe_trace_X4(am, acl_elog_trace_format_label, acl_elog_trace_format_args, acl_elog_val1,              \
286                                                                             acl_elog_val2, acl_elog_val3, acl_elog_val4) \
287 do {                                                                                                                     \
288   if (am->trace_sessions) {                                                                                              \
289     CLIB_UNUSED(struct { u8 available_space[18 - sizeof(acl_elog_val1) - sizeof(acl_elog_val2)                           \
290                                                - sizeof(acl_elog_val3) -sizeof(acl_elog_val4)]; } *static_check);        \
291     u16 thread_index = os_get_thread_index ();                                                                           \
292     vlib_worker_thread_t * w = vlib_worker_threads + thread_index;                                                       \
293     ELOG_TYPE_DECLARE (e) =                                                                                              \
294       {                                                                                                                  \
295         .format = "(%02d) " acl_elog_trace_format_label,                                                                 \
296         .format_args = "i2" acl_elog_trace_format_args,                                                                  \
297       };                                                                                                                 \
298     CLIB_PACKED(struct                                                                                                   \
299       {                                                                                                                  \
300         u16 thread;                                                                                                      \
301         typeof(acl_elog_val1) val1;                                                                                      \
302         typeof(acl_elog_val2) val2;                                                                                      \
303         typeof(acl_elog_val3) val3;                                                                                      \
304         typeof(acl_elog_val4) val4;                                                                                      \
305       }) *ed;                                                                                                            \
306     ed = ELOG_TRACK_DATA (&vlib_global_main.elog_main, e, w->elog_track);                                                \
307     ed->thread = thread_index;                                                                                           \
308     ed->val1 = acl_elog_val1;                                                                                            \
309     ed->val2 = acl_elog_val2;                                                                                            \
310     ed->val3 = acl_elog_val3;                                                                                            \
311     ed->val4 = acl_elog_val4;                                                                                            \
312   }                                                                                                                      \
313 } while (0)
314
315
316 #endif