FD.io VPP  v21.10.1-2-g0a485f517
Vector Packet Processing
cnat_client.h
Go to the documentation of this file.
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_CLIENT_H__
17 #define __CNAT_CLIENT_H__
18 
19 #include <cnat/cnat_types.h>
20 
21 /**
22  * A client is a representation of an IP address behind the NAT.
23  * A client thus sends packet to a VIP.
24  * Clients are learned in the Data-plane when they send packets,
25  * but, since they make additions to the FIB they must be programmed
26  * in the main thread. They are aged out when they become idle.
27  *
28  * A client interposes in the FIB graph for the prefix corresponding
29  * to the client (e.g. client's-IP/32). As a result this client object
30  * is cloned as the interpose DPO. The clones are removed when the lock
31  * count drops to zero. The originals are removed when the client ages.
32  * At forwarding time the client preforms the reverse translation and
33  * then ships the packet to where the FIB would send it.
34  */
35 typedef struct cnat_client_t_
36 {
37  CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
38 
39  /**
40  * the client's IP address
41  */
43 
44  /**
45  * How to send packets to this client post translation
46  */
48 
49  /**
50  * the FIB entry this client sources
51  */
53 
54  /**
55  * number of DPO locks
56  */
58 
59  /**
60  * Translations refcount for cleanup
61  */
63 
64  /**
65  * Session refcount for cleanup
66  */
68 
69  /**
70  * Parent cnat_client index if cloned via interpose
71  * or own index if vanilla client.
72  * Used to get translations & update session_refcnt
73  */
75 
76  /**
77  * Client flags
78  */
81 
82 extern u8 *format_cnat_client (u8 * s, va_list * args);
83 extern void cnat_client_free_by_ip (ip46_address_t * addr, u8 af);
84 
87 
88 #define CC_INDEX_INVALID ((u32)(~0))
89 
92 {
94 }
95 
96 /**
97  * A translation that references this VIP was deleted
98  */
100 
101 /**
102  * A translation that references this VIP was added
103  */
104 extern void cnat_client_translation_added (index_t cci);
105 /**
106  * Called in the main thread by RPC from the workers to learn a
107  * new client
108  */
109 extern void cnat_client_learn (const ip_address_t *addr);
110 
111 extern index_t cnat_client_add (const ip_address_t * ip, u8 flags);
112 
113 /**
114  * Check all the clients were purged by translation & session purge
115  */
116 extern int cnat_client_purge (void);
117 
118 /**
119  * CNat Client (dpo) flags
120  */
121 typedef enum
122 {
123  /* IP already present in the FIB, need to interpose dpo */
126 
127 
128 extern void cnat_client_throttle_pool_process ();
129 
130 /**
131  * DB of clients
132  */
133 typedef struct cnat_client_db_t_
134 {
137  /* Pool of addresses that have been throttled
138  and need to be refcounted before calling
139  cnat_client_free_by_ip */
143 
145 
146 /**
147  * Find a client from an IP4 address
148  */
151 {
152  uword *p;
153 
154  p = hash_get (cnat_client_db.crd_cip4, ip->as_u32);
155 
156  if (p)
157  return (pool_elt_at_index (cnat_client_pool, p[0]));
158 
159  return (NULL);
160 }
161 
164 {
165  uword *p;
166 
167  p = hash_get (cnat_client_db.crd_cip4, ip->as_u32);
168 
169  if (p)
170  return p[0];
171 
172  return -1;
173 }
174 
175 /**
176  * Find a client from an IP6 address
177  */
179 cnat_client_ip6_find (const ip6_address_t * ip)
180 {
181  uword *p;
182 
184 
185  if (p)
186  return (pool_elt_at_index (cnat_client_pool, p[0]));
187 
188  return (NULL);
189 }
190 
191 /**
192  * Add a session refcnt to this client
193  */
196 {
198  return clib_atomic_add_fetch (&ccp->session_refcnt, 1);
199 }
200 
201 /**
202  * Del a session refcnt to this client
203  */
206 {
208  return clib_atomic_sub_fetch (&ccp->session_refcnt, 1);
209 }
210 
211 /*
212  * fd.io coding-style-patch-verification: ON
213  *
214  * Local Variables:
215  * eval: (c-set-style "gnu")
216  * End:
217  */
218 
219 #endif
ip_address
Definition: ip_types.h:79
cnat_client_ip6_find
static_always_inline cnat_client_t * cnat_client_ip6_find(const ip6_address_t *ip)
Find a client from an IP6 address.
Definition: cnat_client.h:179
cnat_client_purge
int cnat_client_purge(void)
Check all the clients were purged by translation & session purge.
Definition: cnat_client.c:228
cnat_client_translation_deleted
void cnat_client_translation_deleted(index_t cci)
A translation that references this VIP was deleted.
Definition: cnat_client.c:108
CNAT_FLAG_EXCLUSIVE
@ CNAT_FLAG_EXCLUSIVE
Definition: cnat_client.h:124
pool_elt_at_index
#define pool_elt_at_index(p, i)
Returns pointer to element at given index.
Definition: pool.h:549
cnat_client_ip4_find_index
static_always_inline u32 cnat_client_ip4_find_index(const ip4_address_t *ip)
Definition: cnat_client.h:163
clib_atomic_add_fetch
#define clib_atomic_add_fetch(a, b)
Definition: atomics.h:30
cnat_client_cnt_session
static_always_inline u32 cnat_client_cnt_session(cnat_client_t *cc)
Add a session refcnt to this client.
Definition: cnat_client.h:195
cnat_client_ip4_find
static_always_inline cnat_client_t * cnat_client_ip4_find(const ip4_address_t *ip)
Find a client from an IP4 address.
Definition: cnat_client.h:150
cnat_client_add
index_t cnat_client_add(const ip_address_t *ip, u8 flags)
Definition: cnat_client.c:137
addr
vhost_vring_addr_t addr
Definition: vhost_user.h:130
cnat_client_t_::CLIB_CACHE_LINE_ALIGN_MARK
CLIB_CACHE_LINE_ALIGN_MARK(cacheline0)
cnat_client_t
struct cnat_client_t_ cnat_client_t
A client is a representation of an IP address behind the NAT.
cnat_types.h
cnat_client_t_::cc_ip
ip_address_t cc_ip
the client's IP address
Definition: cnat_client.h:42
cnat_client_t_::cc_parent
dpo_id_t cc_parent
How to send packets to this client post translation.
Definition: cnat_client.h:47
cnat_client_t_::cc_locks
u32 cc_locks
number of DPO locks
Definition: cnat_client.h:57
cnat_entry_flag_t
cnat_entry_flag_t
CNat Client (dpo) flags.
Definition: cnat_client.h:121
cnat_client_throttle_pool_process
void cnat_client_throttle_pool_process()
Definition: cnat_client.c:69
cnat_client_t_::parent_cci
index_t parent_cci
Parent cnat_client index if cloned via interpose or own index if vanilla client.
Definition: cnat_client.h:74
hash_get
#define hash_get(h, key)
Definition: hash.h:249
cnat_client_t_::session_refcnt
u32 session_refcnt
Session refcount for cleanup.
Definition: cnat_client.h:67
index_t
u32 index_t
A Data-Path Object is an object that represents actions that are applied to packets are they are swit...
Definition: dpo.h:43
static_always_inline
#define static_always_inline
Definition: clib.h:112
clib_spinlock_s
Definition: lock.h:51
cnat_client_translation_added
void cnat_client_translation_added(index_t cci)
A translation that references this VIP was added.
Definition: cnat_client.c:97
fib_node_index_t
u32 fib_node_index_t
A typedef of a node index.
Definition: fib_types.h:29
uword
u64 uword
Definition: types.h:112
dpo_type_t
enum dpo_type_t_ dpo_type_t
Common types of data-path objects New types can be dynamically added using dpo_register_new_type()
ip4_address_t
Definition: ip4_packet.h:50
cnat_client_db_t_::throttle_mem
uword * throttle_mem
Definition: cnat_client.h:141
cnat_client_db_t_::crd_cip6
uword * crd_cip6
Definition: cnat_client.h:136
clib_atomic_sub_fetch
#define clib_atomic_sub_fetch(a, b)
Definition: atomics.h:31
hash_get_mem
#define hash_get_mem(h, key)
Definition: hash.h:269
cnat_client_learn
void cnat_client_learn(const ip_address_t *addr)
Called in the main thread by RPC from the workers to learn a new client.
Definition: cnat_client.c:188
cnat_client_pool
cnat_client_t * cnat_client_pool
Definition: cnat_client.c:22
cnat_client_free_by_ip
void cnat_client_free_by_ip(ip46_address_t *addr, u8 af)
Definition: cnat_client.c:57
cnat_client_t_::tr_refcnt
u32 tr_refcnt
Translations refcount for cleanup.
Definition: cnat_client.h:62
cnat_client_uncnt_session
static_always_inline u32 cnat_client_uncnt_session(cnat_client_t *cc)
Del a session refcnt to this client.
Definition: cnat_client.h:205
u32
unsigned int u32
Definition: types.h:88
af
vl_api_address_family_t af
Definition: ip.api:619
format_cnat_client
u8 * format_cnat_client(u8 *s, va_list *args)
Definition: cnat_client.c:247
cnat_client_t_::cc_fei
fib_node_index_t cc_fei
the FIB entry this client sources
Definition: cnat_client.h:52
cnat_client_dpo
dpo_type_t cnat_client_dpo
Definition: cnat_client.c:26
cnat_client_get
static_always_inline cnat_client_t * cnat_client_get(index_t i)
Definition: cnat_client.h:91
u8
unsigned char u8
Definition: types.h:56
ip
vl_api_address_t ip
Definition: l2.api:558
i
int i
Definition: flowhash_template.h:376
cnat_client_t_::flags
u8 flags
Client flags.
Definition: cnat_client.h:79
dpo_id_t_
The identity of a DPO is a combination of its type and its instance number/index of objects of that t...
Definition: dpo.h:172
cnat_client_db_t
struct cnat_client_db_t_ cnat_client_db_t
DB of clients.
cnat_client_db
cnat_client_db_t cnat_client_db
Definition: cnat_client.c:24
cnat_client_db_t_::crd_cip4
uword * crd_cip4
Definition: cnat_client.h:135
cnat_client_t_
A client is a representation of an IP address behind the NAT.
Definition: cnat_client.h:35
cnat_client_db_t_
DB of clients.
Definition: cnat_client.h:133
cnat_client_db_t_::throttle_lock
clib_spinlock_t throttle_lock
Definition: cnat_client.h:140
flags
vl_api_wireguard_peer_flags_t flags
Definition: wireguard.api:105