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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 #ifndef __CNAT_TRANSLATION_H__
17 #define __CNAT_TRANSLATION_H__
19 #include <cnat/cnat_types.h>
20 #include <vnet/ip/ip_types.h>
21 #include <vppinfra/bihash_8_8.h>
24 * Counters for each translation
26 extern vlib_combined_counter_main_t cnat_translation_counters;
30 * Data used to track an EP in the FIB
32 typedef struct cnat_ep_trk_t_
35 * The EP being tracked
37 cnat_endpoint_t ct_ep[VLIB_N_DIR];
40 * The FIB entry for the EP
42 fib_node_index_t ct_fei;
45 * The sibling on the entry's child list
50 * The forwarding contributed by the entry
55 * Allows to disable if not resolved yet
57 u8 ct_flags; /* cnat_trk_flag_t */
60 typedef enum cnat_translation_flag_t_
62 /* Do allocate a source port */
63 CNAT_TRANSLATION_FLAG_ALLOCATE_PORT = (1 << 0),
64 /* Has this translation been satcked ?
65 * this allow not being called twice when
66 * with more then FIB_PATH_LIST_POPULAR backends */
67 CNAT_TRANSLATION_STACKED = (1 << 1),
68 } cnat_translation_flag_t;
73 CNAT_RESOLV_ADDR_BACKEND,
74 CNAT_RESOLV_ADDR_SNAT,
75 CNAT_RESOLV_ADDR_TRANSLATION,
76 CNAT_ADDR_N_RESOLUTIONS,
77 } cnat_addr_resol_type_t;
79 typedef enum __attribute__ ((__packed__))
86 * Entry used to account for a translation's backend
87 * waiting for address resolution
89 typedef struct addr_resolution_t_
92 * The interface index to resolve
96 * ip4 or ip6 resolution
98 ip_address_family_t af;
100 * The cnat_addr_resolution_t
102 cnat_addr_resol_type_t type;
114 * A Translation represents the translation of a VEP to one of a set
115 * of real server addresses
117 typedef struct cnat_translation_t_
120 * Linkage into the FIB graph
125 * The LB used to forward to the backends
130 * The Virtual end point
132 cnat_endpoint_t ct_vip;
135 * The vector of tracked back-ends
137 cnat_ep_trk_t *ct_paths;
140 * The vector of active tracked back-ends
142 cnat_ep_trk_t *ct_active_paths;
145 * The ip protocol for the translation
147 ip_protocol_t ct_proto;
150 * The client object this translation belongs on
151 * INDEX_INVALID if vip is unresolved
156 * Own index (if copied for trace)
166 * Type of load balancing
168 cnat_lb_type_t lb_type;
174 } cnat_translation_t;
176 extern cnat_translation_t *cnat_translation_pool;
178 extern u8 *format_cnat_translation (u8 * s, va_list * args);
181 * create or update a translation
183 * @param vip The Virtual Endpoint
184 * @param ip_proto The ip protocol to translate
185 * @param backends the backends to choose from
187 * @return the ID of the translation. used to delete and gather stats
189 extern u32 cnat_translation_update (cnat_endpoint_t *vip,
190 ip_protocol_t ip_proto,
191 cnat_endpoint_tuple_t *backends, u8 flags,
192 cnat_lb_type_t lb_type);
195 * Delete a translation
197 * @param id the ID as returned from the create
199 extern int cnat_translation_delete (u32 id);
202 * Callback function invoked during a walk of all translations
204 typedef walk_rc_t (*cnat_translation_walk_cb_t) (index_t index, void *ctx);
207 * Walk/visit each of the translations
209 extern void cnat_translation_walk (cnat_translation_walk_cb_t cb, void *ctx);
212 * Purge all the trahslations
214 extern int cnat_translation_purge (void);
217 * Add an address resolution request
219 extern void cnat_translation_watch_addr (index_t cti, u64 opaque,
220 cnat_endpoint_t * ep,
221 cnat_addr_resol_type_t type);
224 * Cleanup matching addr resolution requests
226 extern void cnat_translation_unwatch_addr (u32 cti,
227 cnat_addr_resol_type_t type);
230 * Register a call back for endpoint->address resolution
232 typedef void (*cnat_if_addr_add_cb_t) (addr_resolution_t *ar,
233 ip_address_t *address, u8 is_del);
235 extern void cnat_translation_register_addr_add_cb (cnat_addr_resol_type_t typ,
236 cnat_if_addr_add_cb_t fn);
239 * Data plane functions
241 extern clib_bihash_8_8_t cnat_translation_db;
243 static_always_inline cnat_translation_t *
244 cnat_translation_get (index_t cti)
246 return (pool_elt_at_index (cnat_translation_pool, cti));
249 static_always_inline cnat_translation_t *
250 cnat_find_translation (index_t cti, u16 port, ip_protocol_t proto)
252 clib_bihash_kv_8_8_t bkey, bvalue;
256 key = ((u64) proto << 24) | port;
257 key = key << 32 | (u32) cti;
260 rv = clib_bihash_search_inline_2_8_8 (&cnat_translation_db, &bkey, &bvalue);
262 return (pool_elt_at_index (cnat_translation_pool, bvalue.value));
268 * fd.io coding-style-patch-verification: ON
271 * eval: (c-set-style "gnu")