db899917113f5ed102172891845ee3ff7bea065c
[vpp.git] / plugins / acl-plugin / acl / l2sess.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_l2sess_h__
16 #define __included_l2sess_h__
17
18 #include <vnet/vnet.h>
19 #include <vnet/ip/ip.h>
20 #include <vnet/ethernet/ethernet.h>
21
22 #include <vppinfra/hash.h>
23 #include <vppinfra/error.h>
24 #include <vppinfra/elog.h>
25 #include <vppinfra/timing_wheel.h>
26
27 #include <vnet/l2/l2_output.h>
28 #include <vnet/l2/l2_input.h>
29
30 #define _(node_name, node_var, is_out, is_ip6, is_track)
31 #undef _
32 #define foreach_l2sess_node \
33   _("aclp-l2s-input-ip4-add", l2sess_in_ip4_add, 0, 0, 0)  \
34   _("aclp-l2s-input-ip6-add", l2sess_in_ip6_add, 0, 1, 0)  \
35   _("aclp-l2s-output-ip4-add", l2sess_out_ip4_add, 1, 0, 0) \
36   _("aclp-l2s-output-ip6-add", l2sess_out_ip6_add, 1, 1, 0) \
37   _("aclp-l2s-input-ip4-track", l2sess_in_ip4_track, 0, 0, 1) \
38   _("aclp-l2s-input-ip6-track", l2sess_in_ip6_track, 0, 1, 1) \
39   _("aclp-l2s-output-ip4-track",l2sess_out_ip4_track, 1, 0, 1) \
40   _("aclp-l2s-output-ip6-track", l2sess_out_ip6_track, 1, 1, 1)
41
42 #define _(node_name, node_var, is_out, is_ip6, is_track)  \
43   extern vlib_node_registration_t node_var;
44 foreach_l2sess_node
45 #undef _
46
47 #define TCP_FLAG_FIN    0x01
48 #define TCP_FLAG_SYN    0x02
49 #define TCP_FLAG_RST    0x04
50 #define TCP_FLAG_PUSH   0x08
51 #define TCP_FLAG_ACK    0x10
52 #define TCP_FLAG_URG    0x20
53 #define TCP_FLAG_ECE    0x40
54 #define TCP_FLAG_CWR    0x80
55 #define TCP_FLAGS_RSTFINACKSYN (TCP_FLAG_RST + TCP_FLAG_FIN + TCP_FLAG_SYN + TCP_FLAG_ACK)
56 #define TCP_FLAGS_ACKSYN (TCP_FLAG_SYN + TCP_FLAG_ACK)
57
58 typedef struct {
59   ip46_address_t addr;
60   u64 active_time;
61   u64 n_packets;
62   u64 n_bytes;
63   u16 port;
64 } l2s_session_side_t;
65
66 enum {
67   L2S_SESSION_SIDE_IN = 0,
68   L2S_SESSION_SIDE_OUT,
69   L2S_N_SESSION_SIDES
70 };
71
72 typedef struct {
73   u64 create_time;
74   l2s_session_side_t side[L2S_N_SESSION_SIDES];
75   u8 l4_proto;
76   u8 is_ip6;
77   u16 tcp_flags_seen; /* u16 because of two sides */
78 } l2s_session_t;
79
80 #define PROD
81 #ifdef PROD
82 #define UDP_SESSION_IDLE_TIMEOUT_SEC 600
83 #define TCP_SESSION_IDLE_TIMEOUT_SEC (3600*24)
84 #define TCP_SESSION_TRANSIENT_TIMEOUT_SEC 120
85 #else
86 #define UDP_SESSION_IDLE_TIMEOUT_SEC 15
87 #define TCP_SESSION_IDLE_TIMEOUT_SEC 15
88 #define TCP_SESSION_TRANSIENT_TIMEOUT_SEC 5
89 #endif
90
91 typedef struct {
92     /*
93      * the next two fields are present for all nodes, but
94      *  only one of them is used per node - depending
95      * on whether the node is an input or output one.
96      */
97 #define _(node_name, node_var, is_out, is_ip6, is_track) \
98     u32 node_var ## _input_next_node_index[32]; \
99     l2_output_next_nodes_st node_var ## _next_nodes;
100 foreach_l2sess_node
101 #undef _
102     l2_output_next_nodes_st output_next_nodes;
103
104     /* Next indices of the tracker nodes */
105     u32 next_slot_track_node_by_is_ip6_is_out[2][2];
106
107     /* 
108      * Pairing of "forward" and "reverse" tables by table index.
109      * Each relationship has two entries - for one and the other table,
110      * so it is bidirectional.
111      */
112      
113     u32 *fwd_to_rev_by_table_index;
114
115     /*
116      * The vector of per-interface session pools
117      */
118
119     l2s_session_t *sessions;
120
121     /* The session timeouts */
122     u64 tcp_session_transient_timeout;
123     u64 tcp_session_idle_timeout;
124     u64 udp_session_idle_timeout;
125
126     /* Timing wheel to time out the idle sessions */
127     timing_wheel_t timing_wheel;
128     u32 *data_from_advancing_timing_wheel;
129     u64 timer_wheel_next_expiring_time;
130     u64 timer_wheel_tick;
131
132     /* convenience */
133     vlib_main_t * vlib_main;
134     vnet_main_t * vnet_main;
135     ethernet_main_t * ethernet_main;
136
137     /* Counter(s) */
138     u64 counter_attempted_delete_free_session;
139 } l2sess_main_t;
140
141 l2sess_main_t l2sess_main;
142
143 /* Just exposed for acl.c */
144
145 void
146 l2sess_vlib_plugin_register (vlib_main_t * vm, void * hh,
147                       int from_early_init);
148
149
150 #endif /* __included_l2sess_h__ */