acl-plugin: bihash-based ACL lookup
[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 "bihash_40_8.h"
29 #include "fa_node.h"
30 #include "hash_lookup_types.h"
31
32 #define  ACL_PLUGIN_VERSION_MAJOR 1
33 #define  ACL_PLUGIN_VERSION_MINOR 3
34
35 #define UDP_SESSION_IDLE_TIMEOUT_SEC 600
36 #define TCP_SESSION_IDLE_TIMEOUT_SEC (3600*24)
37 #define TCP_SESSION_TRANSIENT_TIMEOUT_SEC 120
38
39 extern vlib_node_registration_t acl_in_node;
40 extern vlib_node_registration_t acl_out_node;
41
42 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);
43 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);
44
45 enum acl_timeout_e {
46   ACL_TIMEOUT_UDP_IDLE = 0,
47   ACL_TIMEOUT_TCP_IDLE,
48   ACL_TIMEOUT_TCP_TRANSIENT,
49   ACL_N_TIMEOUTS
50 };
51
52
53 enum address_e { IP4, IP6 };
54 typedef struct
55 {
56   enum address_e type;
57   union {
58     ip6_address_t ip6;
59     ip4_address_t ip4;
60   } addr;
61 } address_t;
62
63 /*
64  * ACL rules
65  */
66 typedef struct
67 {
68   u8 is_permit;
69   u8 is_ipv6;
70   ip46_address_t src;
71   u8 src_prefixlen;
72   ip46_address_t dst;
73   u8 dst_prefixlen;
74   u8 proto;
75   u16 src_port_or_type_first;
76   u16 src_port_or_type_last;
77   u16 dst_port_or_code_first;
78   u16 dst_port_or_code_last;
79   u8 tcp_flags_value;
80   u8 tcp_flags_mask;
81 } acl_rule_t;
82
83 typedef struct
84 {
85   u8 is_permit;
86   u8 is_ipv6;
87   u8 src_mac[6];
88   u8 src_mac_mask[6];
89   ip46_address_t src_ip_addr;
90   u8 src_prefixlen;
91 } macip_acl_rule_t;
92
93 /*
94  * ACL
95  */
96 typedef struct
97 {
98   u8 tag[64];
99   u32 count;
100   acl_rule_t *rules;
101 } acl_list_t;
102
103 typedef struct
104 {
105   u8 tag[64];
106   u32 count;
107   macip_acl_rule_t *rules;
108   /* References to the classifier tables that will enforce the rules */
109   u32 ip4_table_index;
110   u32 ip6_table_index;
111   u32 l2_table_index;
112 } macip_acl_list_t;
113
114 /*
115  * An element describing a particular configuration fo the mask,
116  * and how many times it has been used.
117  */
118 typedef struct
119 {
120   fa_5tuple_t mask;
121   u32 refcount;
122 } ace_mask_type_entry_t;
123
124 typedef struct {
125   /* API message ID base */
126   u16 msg_id_base;
127
128   acl_list_t *acls;     /* Pool of ACLs */
129   hash_acl_info_t *hash_acl_infos; /* corresponding hash matching housekeeping info */
130   clib_bihash_48_8_t acl_lookup_hash; /* ACL lookup hash table. */
131   int acl_lookup_hash_initialized;
132   applied_hash_ace_entry_t **input_hash_entry_vec_by_sw_if_index;
133   applied_hash_ace_entry_t **output_hash_entry_vec_by_sw_if_index;
134   applied_hash_acl_info_t *input_applied_hash_acl_info_by_sw_if_index;
135   applied_hash_acl_info_t *output_applied_hash_acl_info_by_sw_if_index;
136
137   macip_acl_list_t *macip_acls; /* Pool of MAC-IP ACLs */
138
139   /* ACLs associated with interfaces */
140   u32 **input_acl_vec_by_sw_if_index;
141   u32 **output_acl_vec_by_sw_if_index;
142
143   /* interfaces on which given ACLs are applied */
144   u32 **input_sw_if_index_vec_by_acl;
145   u32 **output_sw_if_index_vec_by_acl;
146
147   /* Do we use hash-based ACL matching or linear */
148   int use_hash_acl_matching;
149
150   /* a pool of all mask types present in all ACEs */
151   ace_mask_type_entry_t *ace_mask_type_pool;
152
153   /*
154    * Classify tables used to grab the packets for the ACL check,
155    * and serving as the 5-tuple session tables at the same time
156    */
157   u32 *acl_ip4_input_classify_table_by_sw_if_index;
158   u32 *acl_ip6_input_classify_table_by_sw_if_index;
159   u32 *acl_ip4_output_classify_table_by_sw_if_index;
160   u32 *acl_ip6_output_classify_table_by_sw_if_index;
161
162   /* MACIP (input) ACLs associated with the interfaces */
163   u32 *macip_acl_by_sw_if_index;
164
165   /* bitmaps when set the processing is enabled on the interface */
166   uword *fa_in_acl_on_sw_if_index;
167   uword *fa_out_acl_on_sw_if_index;
168   /* bihash holding all of the sessions */
169   int fa_sessions_hash_is_initialized;
170   clib_bihash_40_8_t fa_sessions_hash;
171   /* The process node which orcherstrates the cleanup */
172   u32 fa_cleaner_node_index;
173   /* FA session timeouts, in seconds */
174   u32 session_timeout_sec[ACL_N_TIMEOUTS];
175   /* session add/delete counters */
176   u64 *fa_session_adds_by_sw_if_index;
177   u64 *fa_session_dels_by_sw_if_index;
178   /* total session adds/dels */
179   u64 fa_session_total_adds;
180   u64 fa_session_total_dels;
181
182   /* L2 datapath glue */
183
184   /* next indices within L2 classifiers for ip4/ip6 fa L2 nodes */
185   u32 l2_input_classify_next_acl_ip4;
186   u32 l2_input_classify_next_acl_ip6;
187   u32 l2_output_classify_next_acl_ip4;
188   u32 l2_output_classify_next_acl_ip6;
189   /* next node indices for L2 dispatch */
190   u32 fa_acl_in_ip4_l2_node_feat_next_node_index[32];
191   u32 fa_acl_in_ip6_l2_node_feat_next_node_index[32];
192   u32 fa_acl_out_ip4_l2_node_feat_next_node_index[32];
193   u32 fa_acl_out_ip6_l2_node_feat_next_node_index[32];
194
195   /* EH values that we can skip over */
196   uword *fa_ipv6_known_eh_bitmap;
197
198   /* whether to match L4 ACEs with ports on the non-initial fragment */
199   int l4_match_nonfirst_fragment;
200
201   /* conn table per-interface conn table parameters */
202   u32 fa_conn_table_hash_num_buckets;
203   uword fa_conn_table_hash_memory_size;
204   u64 fa_conn_table_max_entries;
205
206   /*
207    * If the cleaner has to delete more than this number
208    * of connections, it halves the sleep time.
209    */
210
211 #define ACL_FA_DEFAULT_MAX_DELETED_SESSIONS_PER_INTERVAL 100
212   u64 fa_max_deleted_sessions_per_interval;
213
214   /*
215    * If the cleaner deletes less than these connections,
216    * it increases the wait time by the "increment"
217    */
218
219 #define ACL_FA_DEFAULT_MIN_DELETED_SESSIONS_PER_INTERVAL 1
220   u64 fa_min_deleted_sessions_per_interval;
221
222 #define ACL_FA_DEFAULT_CLEANER_WAIT_TIME_INCREMENT 0.1
223   f64 fa_cleaner_wait_time_increment;
224
225   u64 fa_current_cleaner_timer_wait_interval;
226
227   /* per-worker data related t conn management */
228   acl_fa_per_worker_data_t *per_worker_data;
229
230   /* Configured session timeout */
231   u64 session_timeout[ACL_N_TIMEOUTS];
232
233
234   /* Counters for the cleaner thread */
235
236 #define foreach_fa_cleaner_counter                                         \
237   _(fa_cleaner_cnt_delete_by_sw_index, "delete_by_sw_index events")        \
238   _(fa_cleaner_cnt_delete_by_sw_index_ok, "delete_by_sw_index handled ok") \
239   _(fa_cleaner_cnt_unknown_event, "unknown events received")               \
240   _(fa_cleaner_cnt_timer_restarted, "session idle timers restarted")       \
241   _(fa_cleaner_cnt_wait_with_timeout, "event wait with timeout called")    \
242   _(fa_cleaner_cnt_wait_without_timeout, "event wait w/o timeout called")  \
243   _(fa_cleaner_cnt_event_cycles, "total event cycles")                     \
244 /* end of counters */
245 #define _(id, desc) u32 id;
246   foreach_fa_cleaner_counter
247 #undef _
248
249   /* convenience */
250   vlib_main_t * vlib_main;
251   vnet_main_t * vnet_main;
252 } acl_main_t;
253
254 #define foreach_acl_eh                                          \
255    _(HOPBYHOP , 0  , "IPv6ExtHdrHopByHop")                      \
256    _(ROUTING  , 43 , "IPv6ExtHdrRouting")                       \
257    _(DESTOPT  , 60 , "IPv6ExtHdrDestOpt")                       \
258    _(FRAGMENT , 44 , "IPv6ExtHdrFragment")                      \
259    _(MOBILITY , 135, "Mobility Header")                         \
260    _(HIP      , 139, "Experimental use Host Identity Protocol") \
261    _(SHIM6    , 140, "Shim6 Protocol")                          \
262    _(EXP1     , 253, "Use for experimentation and testing")     \
263    _(EXP2     , 254, "Use for experimentation and testing")
264
265 /*
266
267  "No Next Header" is not a header.
268  Also, Fragment header needs special processing.
269
270    _(NONEXT   , 59 , "NoNextHdr")                               \
271
272
273 ESP is hiding its internal format, so no point in trying to go past it.
274
275    _(ESP      , 50 , "EncapsulatingSecurityPayload")            \
276
277
278 AH has a special treatment of its length, it is in 32-bit words, not 64-bit words like the rest.
279
280    _(AUTH     , 51 , "Authentication Header")                   \
281
282
283 */
284
285
286  typedef enum {
287  #define _(N, v, s) ACL_EH_##N = v,
288          foreach_acl_eh
289  #undef _
290  } acl_eh_t;
291
292
293
294 extern acl_main_t acl_main;
295
296
297 #endif