cnat: Ip ICMP error support
[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.h>
20
21 #include <cnat/cnat_types.h>
22 #include <cnat/cnat_client.h>
23 #include <cnat/bihash_40_48.h>
24
25
26 /**
27  * A session represents the memory of a translation.
28  * In the tx direction (from behind to in front of the NAT), the
29  * session is preserved so subsequent packets follow the same path
30  * even if the translation has been updated. In the tx direction
31  * the session represents the swap from the VIP to the server address
32  * In the RX direction the swap is from the server address/port to VIP.
33  *
34  * A session exists only as key and value in the bihash, there is no
35  * pool for this object. If there were a pool, one would need to be
36  * concerned about what worker is using it.
37  */
38 typedef struct cnat_session_t_
39 {
40   /**
41    * this key sits in the same memory location a 'key' in the bihash kvp
42    */
43   struct
44   {
45     /**
46      * IP 4/6 address in the rx/tx direction
47      */
48     ip46_address_t cs_ip[VLIB_N_DIR];
49
50     /**
51      * ports in rx/tx
52      */
53     u16 cs_port[VLIB_N_DIR];
54
55     /**
56      * The IP protocol TCP or UDP only supported
57      */
58     ip_protocol_t cs_proto;
59
60     /**
61      * The address family describing the IP addresses
62      */
63     u8 cs_af;
64
65     /**
66      * spare space
67      */
68     u8 __cs_pad[2];
69   } key;
70   /**
71    * this value sits in the same memory location a 'value' in the bihash kvp
72    */
73   struct
74   {
75     /**
76      * The IP address to translate to.
77      */
78     ip46_address_t cs_ip[VLIB_N_DIR];
79
80     /**
81      * the port to translate to.
82      */
83     u16 cs_port[VLIB_N_DIR];
84
85     /**
86      * The load balance object to use to forward
87      */
88     index_t cs_lbi;
89
90     /**
91      * Timestamp index this session was last used
92      */
93     u32 cs_ts_index;
94     /**
95      * Indicates a return path session that was source NATed
96      * on the way in.
97      */
98     u32 flags;
99   } value;
100 } cnat_session_t;
101
102 typedef enum cnat_session_flag_t_
103 {
104   CNAT_SESSION_FLAG_HAS_SNAT = (1 << 0),
105   CNAT_SESSION_FLAG_ALLOC_PORT = (1 << 1),
106   CNAT_SESSION_FLAG_NO_CLIENT = (1 << 2),
107 } cnat_session_flag_t;
108
109 extern u8 *format_cnat_session (u8 * s, va_list * args);
110
111 /**
112  * Ensure the session object correctly overlays the bihash key/value pair
113  */
114 STATIC_ASSERT (STRUCT_OFFSET_OF (cnat_session_t, key) ==
115                STRUCT_OFFSET_OF (clib_bihash_kv_40_48_t, key),
116                "key overlaps");
117 STATIC_ASSERT (STRUCT_OFFSET_OF (cnat_session_t, value) ==
118                STRUCT_OFFSET_OF (clib_bihash_kv_40_48_t, value),
119                "value overlaps");
120 STATIC_ASSERT (sizeof (cnat_session_t) == sizeof (clib_bihash_kv_40_48_t),
121                "session kvp");
122
123 /**
124  * The DB of sessions
125  */
126 extern clib_bihash_40_48_t cnat_session_db;
127
128 /**
129  * Callback function invoked during a walk of all translations
130  */
131 typedef walk_rc_t (*cnat_session_walk_cb_t) (const cnat_session_t *
132                                              session, void *ctx);
133
134 /**
135  * Walk/visit each of the cnat session
136  */
137 extern void cnat_session_walk (cnat_session_walk_cb_t cb, void *ctx);
138
139 /**
140  * Scan the session DB for expired sessions
141  */
142 extern u64 cnat_session_scan (vlib_main_t * vm, f64 start_time, int i);
143
144 /**
145  * Purge all the sessions
146  */
147 extern int cnat_session_purge (void);
148
149 /**
150  * Free a session & update refcounts
151  */
152 extern void cnat_session_free (cnat_session_t * session);
153
154 /*
155  * fd.io coding-style-patch-verification: ON
156  *
157  * Local Variables:
158  * eval: (c-set-style "gnu")
159  * End:
160  */
161
162 #endif