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