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