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