cnat: allow max_u16 translation backends
[vpp.git] / src / plugins / cnat / cnat_translation.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_TRANSLATION_H__
17 #define __CNAT_TRANSLATION_H__
18
19 #include <cnat/cnat_types.h>
20 #include <vnet/ip/ip_types.h>
21 #include <vppinfra/bihash_8_8.h>
22
23 /**
24  * Counters for each translation
25  */
26 extern vlib_combined_counter_main_t cnat_translation_counters;
27
28 /**
29  * Data used to track an EP in the FIB
30  */
31 typedef struct cnat_ep_trk_t_
32 {
33   /**
34    * The EP being tracked
35    */
36   cnat_endpoint_t ct_ep[VLIB_N_DIR];
37
38   /**
39    * The FIB entry for the EP
40    */
41   fib_node_index_t ct_fei;
42
43   /**
44    * The sibling on the entry's child list
45    */
46   u32 ct_sibling;
47
48   /**
49    * The forwarding contributed by the entry
50    */
51   dpo_id_t ct_dpo;
52
53   /**
54    * Allows to disable if not resolved yet
55    */
56   u8 is_active;
57 } cnat_ep_trk_t;
58
59 typedef enum cnat_translation_flag_t_
60 {
61   /* Do allocate a source port */
62   CNAT_TRANSLATION_FLAG_ALLOCATE_PORT = (1 << 0),
63   /* Has this translation been satcked ?
64    * this allow not being called twice when
65    * with more then FIB_PATH_LIST_POPULAR backends  */
66   CNAT_TRANSLATION_STACKED = (1 << 1),
67 } cnat_translation_flag_t;
68
69 typedef enum
70 {
71   CNAT_RESOLV_ADDR_ANY,
72   CNAT_RESOLV_ADDR_BACKEND,
73   CNAT_RESOLV_ADDR_SNAT,
74   CNAT_RESOLV_ADDR_TRANSLATION,
75   CNAT_ADDR_N_RESOLUTIONS,
76 } cnat_addr_resol_type_t;
77
78 /**
79  * Entry used to account for a translation's backend
80  * waiting for address resolution
81  */
82 typedef struct addr_resolution_t_
83 {
84   /**
85    * The interface index to resolve
86    */
87   u32 sw_if_index;
88   /**
89    * ip4 or ip6 resolution
90    */
91   ip_address_family_t af;
92   /**
93    * The cnat_addr_resolution_t
94    */
95   cnat_addr_resol_type_t type;
96   /**
97    * Translation index
98    */
99   index_t cti;
100   /**
101    * Callback data
102    */
103   u64 opaque;
104 } addr_resolution_t;
105
106 /**
107  * A Translation represents the translation of a VEP to one of a set
108  * of real server addresses
109  */
110 typedef struct cnat_translation_t_
111 {
112   /**
113    * Linkage into the FIB graph
114    */
115   fib_node_t ct_node;
116
117   /**
118    * The LB used to forward to the backends
119    */
120   dpo_id_t ct_lb;
121
122   /**
123    * The Virtual end point
124    */
125   cnat_endpoint_t ct_vip;
126
127   /**
128    * The vector of tracked back-ends
129    */
130   cnat_ep_trk_t *ct_paths;
131
132   /**
133    * The ip protocol for the translation
134    */
135   ip_protocol_t ct_proto;
136
137   /**
138    * The client object this translation belongs on
139    * INDEX_INVALID if vip is unresolved
140    */
141   index_t ct_cci;
142
143   /**
144    * Own index (if copied for trace)
145    */
146   index_t index;
147
148   /**
149    * Translation flags
150    */
151   u8 flags;
152 } cnat_translation_t;
153
154 extern cnat_translation_t *cnat_translation_pool;
155
156 extern u8 *format_cnat_translation (u8 * s, va_list * args);
157
158 /**
159  * create or update a translation
160  *
161  * @param vip The Virtual Endpoint
162  * @param ip_proto The ip protocol to translate
163  * @param backends the backends to choose from
164  *
165  * @return the ID of the translation. used to delete and gather stats
166  */
167 extern u32 cnat_translation_update (cnat_endpoint_t * vip,
168                                     ip_protocol_t ip_proto,
169                                     cnat_endpoint_tuple_t *
170                                     backends, u8 flags);
171
172 /**
173  * Delete a translation
174  *
175  * @param id the ID as returned from the create
176  */
177 extern int cnat_translation_delete (u32 id);
178
179 /**
180  * Callback function invoked during a walk of all translations
181  */
182 typedef walk_rc_t (*cnat_translation_walk_cb_t) (index_t index, void *ctx);
183
184 /**
185  * Walk/visit each of the translations
186  */
187 extern void cnat_translation_walk (cnat_translation_walk_cb_t cb, void *ctx);
188
189 /**
190  * Purge all the trahslations
191  */
192 extern int cnat_translation_purge (void);
193
194 /**
195  * Add an address resolution request
196  */
197 extern void cnat_translation_watch_addr (index_t cti, u64 opaque,
198                                          cnat_endpoint_t * ep,
199                                          cnat_addr_resol_type_t type);
200
201 /**
202  * Cleanup matching addr resolution requests
203  */
204 extern void cnat_translation_unwatch_addr (u32 cti,
205                                            cnat_addr_resol_type_t type);
206
207 /*
208  * Data plane functions
209  */
210 extern clib_bihash_8_8_t cnat_translation_db;
211
212 static_always_inline cnat_translation_t *
213 cnat_translation_get (index_t cti)
214 {
215   return (pool_elt_at_index (cnat_translation_pool, cti));
216 }
217
218 static_always_inline cnat_translation_t *
219 cnat_find_translation (index_t cti, u16 port, ip_protocol_t proto)
220 {
221   clib_bihash_kv_8_8_t bkey, bvalue;
222   u64 key;
223   int rv;
224
225   key = (proto << 24) | port;
226   key = key << 32 | (u32) cti;
227
228   bkey.key = key;
229   rv = clib_bihash_search_inline_2_8_8 (&cnat_translation_db, &bkey, &bvalue);
230   if (!rv)
231     return (pool_elt_at_index (cnat_translation_pool, bvalue.value));
232
233   return (NULL);
234 }
235
236 /*
237  * fd.io coding-style-patch-verification: ON
238  *
239  * Local Variables:
240  * eval: (c-set-style "gnu")
241  * End:
242  */
243
244 #endif