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;
29 * Data used to track an EP in the FIB
31 typedef struct cnat_ep_trk_t_
34 * The EP being tracked
36 cnat_endpoint_t ct_ep[VLIB_N_DIR];
39 * The FIB entry for the EP
41 fib_node_index_t ct_fei;
44 * The sibling on the entry's child list
49 * The forwarding contributed by the entry
54 typedef enum cnat_translation_flag_t_
56 CNAT_TRANSLATION_FLAG_ALLOCATE_PORT = (1 << 0),
57 } cnat_translation_flag_t;
60 * A Translation represents the translation of a VEP to one of a set
61 * of real server addresses
63 typedef struct cnat_translation_t_
66 * Linkage into the FIB graph
71 * The LB used to forward to the backends
76 * The Virtual end point
78 cnat_endpoint_t ct_vip;
81 * The vector of tracked back-ends
83 cnat_ep_trk_t *ct_paths;
86 * The ip protocol for the translation
88 ip_protocol_t ct_proto;
91 * The client object this translation belongs on
96 * Own index (if copied for trace)
104 } cnat_translation_t;
106 extern cnat_translation_t *cnat_translation_pool;
108 extern u8 *format_cnat_translation (u8 * s, va_list * args);
111 * create or update a translation
113 * @param vip The Virtual Endpoint
114 * @param ip_proto The ip protocol to translate
115 * @param backends the backends to choose from
117 * @return the ID of the translation. used to delete and gather stats
119 extern u32 cnat_translation_update (const cnat_endpoint_t * vip,
120 ip_protocol_t ip_proto,
121 const cnat_endpoint_tuple_t *
125 * Add a translation to the bihash
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
132 extern void cnat_add_translation_to_db (index_t cci, u16 port,
133 ip_protocol_t proto, index_t cti);
136 * Remove a translation from the bihash
138 * @param cci the ID of the parent client
139 * @param port the translation port
140 * @param proto the translation proto
142 extern void cnat_remove_translation_from_db (index_t cci, u16 port,
143 ip_protocol_t proto);
146 * Delete a translation
148 * @param id the ID as returned from the create
150 extern int cnat_translation_delete (u32 id);
153 * Callback function invoked during a walk of all translations
155 typedef walk_rc_t (*cnat_translation_walk_cb_t) (index_t index, void *ctx);
158 * Walk/visit each of the translations
160 extern void cnat_translation_walk (cnat_translation_walk_cb_t cb, void *ctx);
163 * Purge all the trahslations
165 extern int cnat_translation_purge (void);
168 * Data plane functions
170 extern clib_bihash_8_8_t cnat_translation_db;
172 static_always_inline cnat_translation_t *
173 cnat_translation_get (index_t cti)
175 return (pool_elt_at_index (cnat_translation_pool, cti));
178 static_always_inline cnat_translation_t *
179 cnat_find_translation (index_t cti, u16 port, ip_protocol_t proto)
181 clib_bihash_kv_8_8_t bkey, bvalue;
185 key = (proto << 16) | port;
186 key = key << 32 | (u32) cti;
189 rv = clib_bihash_search_inline_2_8_8 (&cnat_translation_db, &bkey, &bvalue);
191 return (pool_elt_at_index (cnat_translation_pool, bvalue.value));
197 * fd.io coding-style-patch-verification: ON
200 * eval: (c-set-style "gnu")