hs-test: more debug output in http3 test
[vpp.git] / src / plugins / cnat / cnat_session.h
1 /*
2  * Copyright (c) 2020 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
16 #ifndef __CNAT_SESSION_H__
17 #define __CNAT_SESSION_H__
18
19 #include <vnet/udp/udp_packet.h>
20
21 #include <cnat/cnat_types.h>
22 #include <cnat/cnat_client.h>
23 #include <cnat/cnat_bihash.h>
24
25 /**
26  * A session represents the memory of a translation.
27  * In the tx direction (from behind to in front of the NAT), the
28  * session is preserved so subsequent packets follow the same path
29  * even if the translation has been updated. In the tx direction
30  * the session represents the swap from the VIP to the server address
31  * In the RX direction the swap is from the server address/port to VIP.
32  *
33  * A session exists only as key and value in the bihash, there is no
34  * pool for this object. If there were a pool, one would need to be
35  * concerned about what worker is using it.
36  */
37 typedef struct cnat_session_t_
38 {
39   /**
40    * this key sits in the same memory location a 'key' in the bihash kvp
41    */
42   struct
43   {
44     /**
45      * IP 4/6 address in the rx/tx direction
46      */
47     ip46_address_t cs_ip[VLIB_N_DIR];
48
49     /**
50      * ports in rx/tx
51      */
52     u16 cs_port[VLIB_N_DIR];
53
54     /**
55      * The IP protocol TCP or UDP only supported
56      */
57     ip_protocol_t cs_proto;
58
59     /**
60      * The address family describing the IP addresses
61      */
62     u8 cs_af;
63
64     /**
65      * input / output / fib session
66      */
67     u8 cs_loc;
68
69     u8 __cs_pad;
70   } key;
71   /**
72    * this value sits in the same memory location a 'value' in the bihash kvp
73    */
74   struct
75   {
76     /**
77      * The IP address to translate to.
78      */
79     ip46_address_t cs_ip[VLIB_N_DIR];
80
81     /**
82      * the port to translate to.
83      */
84     u16 cs_port[VLIB_N_DIR];
85
86     /**
87      * The load balance object to use to forward
88      */
89     index_t cs_lbi;
90
91     /**
92      * Persist translation->ct_lb.dpoi_next_node
93      */
94     u32 dpoi_next_node;
95
96     /**
97      * Timestamp index this session was last used
98      */
99     u32 cs_ts_index;
100
101     /**
102      * session flags
103      */
104     u32 flags;
105
106     u32 __pad;
107   } value;
108 } cnat_session_t;
109
110 typedef enum cnat_session_flag_t_
111 {
112   /**
113    * Indicates a return path session that was source NATed
114    * on the way in.
115    */
116   CNAT_SESSION_FLAG_HAS_SNAT = (1 << 0),
117   /**
118    * This session source port was allocated, free it on cleanup
119    */
120   CNAT_SESSION_FLAG_ALLOC_PORT = (1 << 1),
121   /**
122    * This session doesn't have a client, do not attempt to free it
123    */
124   CNAT_SESSION_FLAG_NO_CLIENT = (1 << 2),
125
126   /* Do not actually translate the packet but still forward it
127    * Used for Maglev, with an encap */
128   CNAT_SESSION_FLAG_NO_NAT = (1 << 3),
129
130   /* Debug flag marking return sessions */
131   CNAT_SESSION_IS_RETURN = (1 << 4),
132
133   /** On conflicts when adding the return session, try to sNAT the
134    * forward session, and dNAT the return session with a random port */
135   CNAT_SESSION_RETRY_SNAT = (1 << 5),
136
137 } cnat_session_flag_t;
138
139 typedef enum cnat_session_location_t_
140 {
141   CNAT_LOCATION_INPUT = 0,
142   CNAT_LOCATION_OUTPUT = 1,
143   CNAT_LOCATION_FIB = 0xff,
144 } cnat_session_location_t;
145
146 extern u8 *format_cnat_session (u8 * s, va_list * args);
147
148 /**
149  * Ensure the session object correctly overlays the bihash key/value pair
150  */
151 STATIC_ASSERT (STRUCT_OFFSET_OF (cnat_session_t, key) ==
152                  STRUCT_OFFSET_OF (cnat_bihash_kv_t, key),
153                "key overlaps");
154 STATIC_ASSERT (STRUCT_OFFSET_OF (cnat_session_t, value) ==
155                  STRUCT_OFFSET_OF (cnat_bihash_kv_t, value),
156                "value overlaps");
157 STATIC_ASSERT (sizeof (cnat_session_t) == sizeof (cnat_bihash_kv_t),
158                "session kvp");
159
160 /**
161  * The DB of sessions
162  */
163 extern cnat_bihash_t cnat_session_db;
164
165 /**
166  * Callback function invoked during a walk of all translations
167  */
168 typedef walk_rc_t (*cnat_session_walk_cb_t) (const cnat_session_t *
169                                              session, void *ctx);
170
171 /**
172  * Walk/visit each of the cnat session
173  */
174 extern void cnat_session_walk (cnat_session_walk_cb_t cb, void *ctx);
175
176 /**
177  * Scan the session DB for expired sessions
178  */
179 extern u64 cnat_session_scan (vlib_main_t * vm, f64 start_time, int i);
180
181 /**
182  * Purge all the sessions
183  */
184 extern int cnat_session_purge (void);
185
186 /**
187  * Free a session & update refcounts
188  */
189 extern void cnat_session_free (cnat_session_t * session);
190
191 /**
192  * Port cleanup callback
193  */
194 extern void (*cnat_free_port_cb) (u16 port, ip_protocol_t iproto);
195
196 /*
197  * fd.io coding-style-patch-verification: ON
198  *
199  * Local Variables:
200  * eval: (c-set-style "gnu")
201  * End:
202  */
203
204 #endif