cnat: Destination based NAT
[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 } cnat_ep_trk_t;
53
54 typedef enum cnat_translation_flag_t_
55 {
56   CNAT_TRANSLATION_FLAG_ALLOCATE_PORT = (1 << 0),
57 } cnat_translation_flag_t;
58
59 /**
60  * A Translation represents the translation of a VEP to one of a set
61  * of real server addresses
62  */
63 typedef struct cnat_translation_t_
64 {
65   /**
66    * Linkage into the FIB graph
67    */
68   fib_node_t ct_node;
69
70   /**
71    * The LB used to forward to the backends
72    */
73   dpo_id_t ct_lb;
74
75   /**
76    * The Virtual end point
77    */
78   cnat_endpoint_t ct_vip;
79
80   /**
81    * The vector of tracked back-ends
82    */
83   cnat_ep_trk_t *ct_paths;
84
85   /**
86    * The ip protocol for the translation
87    */
88   ip_protocol_t ct_proto;
89
90   /**
91    * The client object this translation belongs on
92    */
93   index_t ct_cci;
94
95   /**
96    * Own index (if copied for trace)
97    */
98   index_t index;
99
100   /**
101    * Translation flags
102    */
103   u8 flags;
104 } cnat_translation_t;
105
106 extern cnat_translation_t *cnat_translation_pool;
107
108 extern u8 *format_cnat_translation (u8 * s, va_list * args);
109
110 /**
111  * create or update a translation
112  *
113  * @param vip The Virtual Endpoint
114  * @param ip_proto The ip protocol to translate
115  * @param backends the backends to choose from
116  *
117  * @return the ID of the translation. used to delete and gather stats
118  */
119 extern u32 cnat_translation_update (const cnat_endpoint_t * vip,
120                                     ip_protocol_t ip_proto,
121                                     const cnat_endpoint_tuple_t *
122                                     backends, u8 flags);
123
124 /**
125  * Add a translation to the bihash
126  *
127  * @param cci the ID of the parent client
128  * @param port the translation port
129  * @param proto the translation proto
130  * @param cti the translation index to be used as value
131  */
132 extern void cnat_add_translation_to_db (index_t cci, u16 port,
133                                         ip_protocol_t proto, index_t cti);
134
135 /**
136  * Remove a translation from the bihash
137  *
138  * @param cci the ID of the parent client
139  * @param port the translation port
140  * @param proto the translation proto
141  */
142 extern void cnat_remove_translation_from_db (index_t cci, u16 port,
143                                              ip_protocol_t proto);
144
145 /**
146  * Delete a translation
147  *
148  * @param id the ID as returned from the create
149  */
150 extern int cnat_translation_delete (u32 id);
151
152 /**
153  * Callback function invoked during a walk of all translations
154  */
155 typedef walk_rc_t (*cnat_translation_walk_cb_t) (index_t index, void *ctx);
156
157 /**
158  * Walk/visit each of the translations
159  */
160 extern void cnat_translation_walk (cnat_translation_walk_cb_t cb, void *ctx);
161
162 /**
163  * Purge all the trahslations
164  */
165 extern int cnat_translation_purge (void);
166
167 /*
168  * Data plane functions
169  */
170 extern clib_bihash_8_8_t cnat_translation_db;
171
172 static_always_inline cnat_translation_t *
173 cnat_translation_get (index_t cti)
174 {
175   return (pool_elt_at_index (cnat_translation_pool, cti));
176 }
177
178 static_always_inline cnat_translation_t *
179 cnat_find_translation (index_t cti, u16 port, ip_protocol_t proto)
180 {
181   clib_bihash_kv_8_8_t bkey, bvalue;
182   u64 key;
183   int rv;
184
185   key = (proto << 16) | port;
186   key = key << 32 | (u32) cti;
187
188   bkey.key = key;
189   rv = clib_bihash_search_inline_2_8_8 (&cnat_translation_db, &bkey, &bvalue);
190   if (!rv)
191     return (pool_elt_at_index (cnat_translation_pool, bvalue.value));
192
193   return (NULL);
194 }
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