acl: Remove unsued type
[vpp.git] / src / plugins / acl / acl.h
1 /*
2  * Copyright (c) 2016 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 #ifndef included_acl_h
16 #define included_acl_h
17
18 #include <vnet/vnet.h>
19 #include <vnet/ip/ip.h>
20 #include <vnet/l2/l2_output.h>
21
22
23 #include <vppinfra/hash.h>
24 #include <vppinfra/error.h>
25 #include <vppinfra/bitmap.h>
26 #include <vppinfra/elog.h>
27 #include <vppinfra/bihash_48_8.h>
28 #include <vppinfra/bihash_40_8.h>
29 #include <vppinfra/bihash_16_8.h>
30 #include <vlib/counter.h>
31
32 #include "types.h"
33 #include "fa_node.h"
34 #include "hash_lookup_types.h"
35 #include "lookup_context.h"
36
37 #define  ACL_PLUGIN_VERSION_MAJOR 1
38 #define  ACL_PLUGIN_VERSION_MINOR 4
39
40 #define UDP_SESSION_IDLE_TIMEOUT_SEC 600
41 #define TCP_SESSION_IDLE_TIMEOUT_SEC (3600*24)
42 #define TCP_SESSION_TRANSIENT_TIMEOUT_SEC 120
43
44 #define SESSION_PURGATORY_TIMEOUT_USEC 10
45
46 #define ACL_PLUGIN_HASH_LOOKUP_HEAP_SIZE (2 << 25)
47 #define ACL_PLUGIN_HASH_LOOKUP_HASH_BUCKETS 65536
48 #define ACL_PLUGIN_HASH_LOOKUP_HASH_MEMORY (2 << 25)
49
50 extern vlib_node_registration_t acl_in_node;
51 extern vlib_node_registration_t acl_out_node;
52
53 void input_acl_packet_match(u32 sw_if_index, vlib_buffer_t * b0, u32 *nextp, u32 *acl_match_p, u32 *rule_match_p, u32 *trace_bitmap);
54 void output_acl_packet_match(u32 sw_if_index, vlib_buffer_t * b0, u32 *nextp, u32 *acl_match_p, u32 *rule_match_p, u32 *trace_bitmap);
55
56 enum acl_timeout_e {
57   ACL_TIMEOUT_UNUSED = 0,
58   ACL_TIMEOUT_UDP_IDLE,
59   ACL_TIMEOUT_TCP_IDLE,
60   ACL_TIMEOUT_TCP_TRANSIENT,
61   ACL_N_USER_TIMEOUTS,
62   ACL_TIMEOUT_PURGATORY = ACL_N_USER_TIMEOUTS, /* a special-case queue for deletion-in-progress sessions */
63   ACL_N_TIMEOUTS
64 };
65
66 typedef struct
67 {
68   u8 is_permit;
69   u8 is_ipv6;
70   u8 src_mac[6];
71   u8 src_mac_mask[6];
72   ip46_address_t src_ip_addr;
73   u8 src_prefixlen;
74 } macip_acl_rule_t;
75
76 /*
77  * ACL
78  */
79 typedef struct
80 {
81   /** Required for pool_get_aligned */
82   CLIB_CACHE_LINE_ALIGN_MARK(cacheline0);
83   u8 tag[64];
84   acl_rule_t *rules;
85 } acl_list_t;
86
87 typedef struct
88 {
89   /** Required for pool_get_aligned */
90   CLIB_CACHE_LINE_ALIGN_MARK(cacheline0);
91   u8 tag[64];
92   u32 count;
93   macip_acl_rule_t *rules;
94   /* References to the classifier tables that will enforce the rules */
95   u32 ip4_table_index;
96   u32 ip6_table_index;
97   u32 l2_table_index;
98   /* outacl classifier tables */
99   u32 out_ip4_table_index;
100   u32 out_ip6_table_index;
101   u32 out_l2_table_index;
102 } macip_acl_list_t;
103
104 /*
105  * An element describing a particular configuration fo the mask,
106  * and how many times it has been used.
107  */
108 typedef struct
109 {
110   /** Required for pool_get_aligned */
111   CLIB_CACHE_LINE_ALIGN_MARK(cacheline0);
112   fa_5tuple_t mask;
113   u32 refcount;
114   u8 from_tm;
115 } ace_mask_type_entry_t;
116
117 typedef struct {
118   /* mheap to hold all the ACL module related allocations, other than hash */
119   void *acl_mheap;
120   uword acl_mheap_size;
121
122   /* API message ID base */
123   u16 msg_id_base;
124
125   /* The pool of users of ACL lookup contexts */
126   acl_lookup_context_user_t *acl_users;
127   /* The pool of ACL lookup contexts */
128   acl_lookup_context_t *acl_lookup_contexts;
129
130   acl_list_t *acls;     /* Pool of ACLs */
131   hash_acl_info_t *hash_acl_infos; /* corresponding hash matching housekeeping info */
132   clib_bihash_48_8_t acl_lookup_hash; /* ACL lookup hash table. */
133   u32 hash_lookup_hash_buckets;
134   uword hash_lookup_hash_memory;
135
136   /* mheap to hold all the miscellaneous allocations related to hash-based lookups */
137   void *hash_lookup_mheap;
138   uword hash_lookup_mheap_size;
139   int acl_lookup_hash_initialized;
140 /*
141   applied_hash_ace_entry_t **input_hash_entry_vec_by_sw_if_index;
142   applied_hash_ace_entry_t **output_hash_entry_vec_by_sw_if_index;
143   applied_hash_acl_info_t *input_applied_hash_acl_info_by_sw_if_index;
144   applied_hash_acl_info_t *output_applied_hash_acl_info_by_sw_if_index;
145 */
146   applied_hash_ace_entry_t **hash_entry_vec_by_lc_index;
147   applied_hash_acl_info_t *applied_hash_acl_info_by_lc_index;
148
149   /* Corresponding lookup context indices for in/out lookups per sw_if_index */
150   u32 *input_lc_index_by_sw_if_index;
151   u32 *output_lc_index_by_sw_if_index;
152   /* context user id for interface ACLs */
153   u32 interface_acl_user_id;
154
155   macip_acl_list_t *macip_acls; /* Pool of MAC-IP ACLs */
156
157   /* ACLs associated with interfaces */
158   u32 **input_acl_vec_by_sw_if_index;
159   u32 **output_acl_vec_by_sw_if_index;
160
161   /* interfaces on which given ACLs are applied */
162   u32 **input_sw_if_index_vec_by_acl;
163   u32 **output_sw_if_index_vec_by_acl;
164
165   /* bitmaps 1=sw_if_index has in/out ACL processing enabled */
166   uword *in_acl_on_sw_if_index;
167   uword *out_acl_on_sw_if_index;
168
169   /* lookup contexts where a given ACL is used */
170   u32 **lc_index_vec_by_acl;
171
172   /* input and output policy epochs by interface */
173   u32 *input_policy_epoch_by_sw_if_index;
174   u32 *output_policy_epoch_by_sw_if_index;
175
176   /* whether we need to take the epoch of the session into account */
177   int reclassify_sessions;
178
179
180
181   /* Total count of interface+direction pairs enabled */
182   u32 fa_total_enabled_count;
183
184   /* Do we use hash-based ACL matching or linear */
185   int use_hash_acl_matching;
186
187   /* Do we use the TupleMerge for hash ACLs or not */
188   int use_tuple_merge;
189
190   /* Max collision vector length before splitting the tuple */
191 #define TM_SPLIT_THRESHOLD 39
192   int tuple_merge_split_threshold;
193
194   /* a pool of all mask types present in all ACEs */
195   ace_mask_type_entry_t *ace_mask_type_pool;
196
197   /* vec of vectors of all info of all mask types present in ACEs contained in each lc_index */
198   hash_applied_mask_info_t **hash_applied_mask_info_vec_by_lc_index;
199
200   /*
201    * Classify tables used to grab the packets for the ACL check,
202    * and serving as the 5-tuple session tables at the same time
203    */
204   u32 *acl_ip4_input_classify_table_by_sw_if_index;
205   u32 *acl_ip6_input_classify_table_by_sw_if_index;
206   u32 *acl_ip4_output_classify_table_by_sw_if_index;
207   u32 *acl_ip6_output_classify_table_by_sw_if_index;
208
209   u32 *acl_dot1q_input_classify_table_by_sw_if_index;
210   u32 *acl_dot1ad_input_classify_table_by_sw_if_index;
211   u32 *acl_dot1q_output_classify_table_by_sw_if_index;
212   u32 *acl_dot1ad_output_classify_table_by_sw_if_index;
213
214   u32 *acl_etype_input_classify_table_by_sw_if_index;
215   u32 *acl_etype_output_classify_table_by_sw_if_index;
216
217   u16 **input_etype_whitelist_by_sw_if_index;
218   u16 **output_etype_whitelist_by_sw_if_index;
219
220   /* MACIP (input) ACLs associated with the interfaces */
221   u32 *macip_acl_by_sw_if_index;
222
223   /* Vector of interfaces on which given MACIP ACLs are applied */
224   u32 **sw_if_index_vec_by_macip_acl;
225
226   /* bitmaps when set the processing is enabled on the interface */
227   uword *fa_in_acl_on_sw_if_index;
228   uword *fa_out_acl_on_sw_if_index;
229   /* bihash holding all of the sessions */
230   int fa_sessions_hash_is_initialized;
231   clib_bihash_40_8_t fa_ip6_sessions_hash;
232   clib_bihash_16_8_t fa_ip4_sessions_hash;
233   /* The process node which orchestrates the cleanup */
234   u32 fa_cleaner_node_index;
235   /* FA session timeouts, in seconds */
236   u32 session_timeout_sec[ACL_N_TIMEOUTS];
237   /* total session adds/dels */
238   u64 fa_session_total_adds;
239   u64 fa_session_total_dels;
240   /* how many sessions went into purgatory */
241   u64 fa_session_total_deactivations;
242
243   /* EH values that we can skip over */
244   uword *fa_ipv6_known_eh_bitmap;
245
246   /* whether to match L4 ACEs with ports on the non-initial fragment */
247   int l4_match_nonfirst_fragment;
248
249   /* conn table per-interface conn table parameters */
250   u32 fa_conn_table_hash_num_buckets;
251   uword fa_conn_table_hash_memory_size;
252   u64 fa_conn_table_max_entries;
253
254   int trace_sessions;
255   int trace_acl;
256
257   /*
258    * If the cleaner has to delete more than this number
259    * of connections, it halves the sleep time.
260    */
261
262 #define ACL_FA_DEFAULT_MAX_DELETED_SESSIONS_PER_INTERVAL 100
263   u64 fa_max_deleted_sessions_per_interval;
264
265   /*
266    * If the cleaner deletes less than these connections,
267    * it increases the wait time by the "increment"
268    */
269
270 #define ACL_FA_DEFAULT_MIN_DELETED_SESSIONS_PER_INTERVAL 1
271   u64 fa_min_deleted_sessions_per_interval;
272
273 #define ACL_FA_DEFAULT_CLEANER_WAIT_TIME_INCREMENT 0.1
274   f64 fa_cleaner_wait_time_increment;
275
276   u64 fa_current_cleaner_timer_wait_interval;
277
278   int fa_interrupt_generation;
279
280   /* per-worker data related t conn management */
281   acl_fa_per_worker_data_t *per_worker_data;
282
283   /* Configured session timeout */
284   u64 session_timeout[ACL_N_TIMEOUTS];
285
286
287   /* Counters for the cleaner thread */
288
289 #define foreach_fa_cleaner_counter                                         \
290   _(fa_cleaner_cnt_delete_by_sw_index, "delete_by_sw_index events")        \
291   _(fa_cleaner_cnt_delete_by_sw_index_ok, "delete_by_sw_index handled ok") \
292   _(fa_cleaner_cnt_unknown_event, "unknown events received")               \
293   _(fa_cleaner_cnt_timer_restarted, "session idle timers restarted")       \
294   _(fa_cleaner_cnt_wait_with_timeout, "event wait with timeout called")    \
295   _(fa_cleaner_cnt_wait_without_timeout, "event wait w/o timeout called")  \
296   _(fa_cleaner_cnt_event_cycles, "total event cycles")                     \
297 /* end of counters */
298 #define _(id, desc) u32 id;
299   foreach_fa_cleaner_counter
300 #undef _
301
302   /* convenience */
303   vlib_main_t * vlib_main;
304   vnet_main_t * vnet_main;
305   /* logging */
306   vlib_log_class_t log_default;
307   /* acl counters exposed via stats segment */
308   volatile u32 *acl_counter_lock;
309   vlib_combined_counter_main_t *combined_acl_counters;
310   /* enable/disable ACL counters for interface processing */
311   u32 interface_acl_counters_enabled;
312 } acl_main_t;
313
314 #define acl_log_err(...) \
315   vlib_log(VLIB_LOG_LEVEL_ERR, acl_main.log_default, __VA_ARGS__)
316 #define acl_log_warn(...) \
317   vlib_log(VLIB_LOG_LEVEL_WARNING, acl_main.log_default, __VA_ARGS__)
318 #define acl_log_notice(...) \
319   vlib_log(VLIB_LOG_LEVEL_NOTICE, acl_main.log_default, __VA_ARGS__)
320 #define acl_log_info(...) \
321   vlib_log(VLIB_LOG_LEVEL_INFO, acl_main.log_default, __VA_ARGS__)
322
323
324 static inline void
325 acl_plugin_counter_lock (acl_main_t * am)
326 {
327   if (am->acl_counter_lock)
328     while (clib_atomic_test_and_set (am->acl_counter_lock))
329       /* zzzz */ ;
330 }
331
332 static inline void
333 acl_plugin_counter_unlock (acl_main_t * am)
334 {
335   if (am->acl_counter_lock)
336     clib_atomic_release (am->acl_counter_lock);
337 }
338
339
340 #define foreach_acl_eh                                          \
341    _(HOPBYHOP , 0  , "IPv6ExtHdrHopByHop")                      \
342    _(ROUTING  , 43 , "IPv6ExtHdrRouting")                       \
343    _(DESTOPT  , 60 , "IPv6ExtHdrDestOpt")                       \
344    _(FRAGMENT , 44 , "IPv6ExtHdrFragment")                      \
345    _(MOBILITY , 135, "Mobility Header")                         \
346    _(HIP      , 139, "Experimental use Host Identity Protocol") \
347    _(SHIM6    , 140, "Shim6 Protocol")                          \
348    _(EXP1     , 253, "Use for experimentation and testing")     \
349    _(EXP2     , 254, "Use for experimentation and testing")
350
351 /*
352
353  "No Next Header" is not a header.
354  Also, Fragment header needs special processing.
355
356    _(NONEXT   , 59 , "NoNextHdr")                               \
357
358
359 ESP is hiding its internal format, so no point in trying to go past it.
360
361    _(ESP      , 50 , "EncapsulatingSecurityPayload")            \
362
363
364 AH has a special treatment of its length, it is in 32-bit words, not 64-bit words like the rest.
365
366    _(AUTH     , 51 , "Authentication Header")                   \
367
368
369 */
370
371
372  typedef enum {
373  #define _(N, v, s) ACL_EH_##N = v,
374          foreach_acl_eh
375  #undef _
376  } acl_eh_t;
377
378
379
380 extern acl_main_t acl_main;
381
382 void *acl_plugin_set_heap();
383
384 typedef enum {
385   ACL_FA_REQ_SESS_RESCHEDULE = 0,
386   ACL_FA_N_REQ,
387 } acl_fa_sess_req_t;
388
389 void aclp_post_session_change_request(acl_main_t *am, u32 target_thread, u32 target_session, acl_fa_sess_req_t request_type);
390 void aclp_swap_wip_and_pending_session_change_requests(acl_main_t *am, u32 target_thread);
391
392 #endif