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