cnat: Fix throttle hash & cleanup
[vpp.git] / src / plugins / cnat / cnat_client.c
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 #include <vnet/fib/fib_table.h>
17 #include <vnet/dpo/drop_dpo.h>
18
19 #include <cnat/cnat_client.h>
20 #include <cnat/cnat_translation.h>
21
22 cnat_client_t *cnat_client_pool;
23
24 cnat_client_db_t cnat_client_db;
25
26 dpo_type_t cnat_client_dpo;
27
28 static_always_inline u8
29 cnat_client_is_clone (cnat_client_t * cc)
30 {
31   return (FIB_NODE_INDEX_INVALID == cc->cc_fei);
32 }
33
34 static void
35 cnat_client_db_remove (cnat_client_t * cc)
36 {
37   if (ip_addr_version (&cc->cc_ip) == AF_IP4)
38     hash_unset (cnat_client_db.crd_cip4, ip_addr_v4 (&cc->cc_ip).as_u32);
39   else
40     hash_unset_mem_free (&cnat_client_db.crd_cip6, &ip_addr_v6 (&cc->cc_ip));
41 }
42
43 static void
44 cnat_client_destroy (cnat_client_t * cc)
45 {
46   ASSERT (!cnat_client_is_clone (cc));
47
48   ASSERT (fib_entry_is_sourced (cc->cc_fei, cnat_fib_source));
49   fib_table_entry_delete_index (cc->cc_fei, cnat_fib_source);
50
51   cnat_client_db_remove (cc);
52   dpo_reset (&cc->cc_parent);
53   pool_put (cnat_client_pool, cc);
54 }
55
56 void
57 cnat_client_free_by_ip (ip46_address_t * ip, u8 af)
58 {
59   cnat_client_t *cc;
60   cc = (AF_IP4 == af ?
61         cnat_client_ip4_find (&ip->ip4) : cnat_client_ip6_find (&ip->ip6));
62   ASSERT (NULL != cc);
63
64   if (0 == cnat_client_uncnt_session (cc) && 0 == cc->tr_refcnt)
65     cnat_client_destroy (cc);
66 }
67
68 void
69 cnat_client_throttle_pool_process ()
70 {
71   /* This processes ips stored in the throttle pool
72      to update session refcounts
73      and should be called before cnat_client_free_by_ip */
74   cnat_client_t *cc;
75   ip_address_t *addr, *del_vec = NULL;
76   u32 refcnt;
77
78   vec_reset_length (del_vec);
79   clib_spinlock_lock (&cnat_client_db.throttle_lock);
80   hash_foreach_mem (addr, refcnt, cnat_client_db.throttle_mem, {
81     cc = (AF_IP4 == addr->version ? cnat_client_ip4_find (&ip_addr_v4 (addr)) :
82                                     cnat_client_ip6_find (&ip_addr_v6 (addr)));
83     /* Client might not already be created */
84     if (NULL != cc)
85       {
86         cnat_client_t *ccp = cnat_client_get (cc->parent_cci);
87         clib_atomic_add_fetch (&ccp->session_refcnt, refcnt);
88         vec_add1 (del_vec, *addr);
89       }
90   });
91   vec_foreach (addr, del_vec)
92     hash_unset_mem_free (&cnat_client_db.throttle_mem, addr);
93   clib_spinlock_unlock (&cnat_client_db.throttle_lock);
94 }
95
96 void
97 cnat_client_translation_added (index_t cci)
98 {
99   cnat_client_t *cc;
100   if (INDEX_INVALID == cci)
101     return;
102
103   cc = cnat_client_get (cci);
104   cc->tr_refcnt++;
105 }
106
107 void
108 cnat_client_translation_deleted (index_t cci)
109 {
110   cnat_client_t *cc;
111   if (INDEX_INVALID == cci)
112     return;
113
114   cc = cnat_client_get (cci);
115   cc->tr_refcnt--;
116
117   if (0 == cc->tr_refcnt && 0 == cc->session_refcnt)
118     cnat_client_destroy (cc);
119 }
120
121 static void
122 cnat_client_db_add (cnat_client_t * cc)
123 {
124   index_t cci;
125
126   cci = cc - cnat_client_pool;
127
128   if (ip_addr_version (&cc->cc_ip) == AF_IP4)
129     hash_set (cnat_client_db.crd_cip4, ip_addr_v4 (&cc->cc_ip).as_u32, cci);
130   else
131     hash_set_mem_alloc (&cnat_client_db.crd_cip6,
132                         &ip_addr_v6 (&cc->cc_ip), cci);
133 }
134
135
136 index_t
137 cnat_client_add (const ip_address_t * ip, u8 flags)
138 {
139   cnat_client_t *cc;
140   dpo_id_t tmp = DPO_INVALID;
141   fib_node_index_t fei;
142   dpo_proto_t dproto;
143   fib_prefix_t pfx;
144   index_t cci;
145   u32 fib_flags;
146
147   /* check again if we need this client */
148   cc = (AF_IP4 == ip->version ?
149         cnat_client_ip4_find (&ip->ip.ip4) :
150         cnat_client_ip6_find (&ip->ip.ip6));
151
152   if (NULL != cc)
153     return (cc - cnat_client_pool);
154
155
156   pool_get_aligned (cnat_client_pool, cc, CLIB_CACHE_LINE_BYTES);
157   cc->cc_locks = 1;
158   cci = cc - cnat_client_pool;
159   cc->parent_cci = cci;
160   cc->flags = flags;
161   cc->tr_refcnt = 0;
162   cc->session_refcnt = 0;
163
164   ip_address_copy (&cc->cc_ip, ip);
165   cnat_client_db_add (cc);
166
167   ip_address_to_fib_prefix (&cc->cc_ip, &pfx);
168
169   dproto = fib_proto_to_dpo (pfx.fp_proto);
170   dpo_set (&tmp, cnat_client_dpo, dproto, cci);
171   dpo_stack (cnat_client_dpo, dproto, &cc->cc_parent, drop_dpo_get (dproto));
172
173   fib_flags = FIB_ENTRY_FLAG_LOOSE_URPF_EXEMPT;
174   fib_flags |= (flags & CNAT_FLAG_EXCLUSIVE) ?
175     FIB_ENTRY_FLAG_EXCLUSIVE : FIB_ENTRY_FLAG_INTERPOSE;
176
177   fei = fib_table_entry_special_dpo_add (CNAT_FIB_TABLE,
178                                          &pfx, cnat_fib_source, fib_flags,
179                                          &tmp);
180
181   cc = pool_elt_at_index (cnat_client_pool, cci);
182   cc->cc_fei = fei;
183
184   return (cci);
185 }
186
187 void
188 cnat_client_learn (const ip_address_t *addr)
189 {
190   /* RPC call to add a client from the dataplane */
191   index_t cci;
192   cnat_client_t *cc;
193   cci = cnat_client_add (addr, 0 /* flags */);
194   cc = pool_elt_at_index (cnat_client_pool, cci);
195   cnat_client_cnt_session (cc);
196   /* Process throttled calls if any */
197   cnat_client_throttle_pool_process ();
198 }
199
200 /**
201  * Interpose a policy DPO
202  */
203 static void
204 cnat_client_dpo_interpose (const dpo_id_t * original,
205                            const dpo_id_t * parent, dpo_id_t * clone)
206 {
207   cnat_client_t *cc, *cc_clone;
208
209   pool_get_zero (cnat_client_pool, cc_clone);
210   cc = cnat_client_get (original->dpoi_index);
211
212   cc_clone->cc_fei = FIB_NODE_INDEX_INVALID;
213   cc_clone->parent_cci = cc->parent_cci;
214   cc_clone->flags = cc->flags;
215   ip_address_copy (&cc_clone->cc_ip, &cc->cc_ip);
216
217   /* stack the clone on the FIB provided parent */
218   dpo_stack (cnat_client_dpo, original->dpoi_proto, &cc_clone->cc_parent,
219              parent);
220
221   /* return the clone */
222   dpo_set (clone,
223            cnat_client_dpo,
224            original->dpoi_proto, cc_clone - cnat_client_pool);
225 }
226
227 int
228 cnat_client_purge (void)
229 {
230   int rv = 0, rrv = 0;
231   if ((rv = hash_elts (cnat_client_db.crd_cip6)))
232     clib_warning ("len(crd_cip6) isnt 0 but %d", rv);
233   rrv |= rv;
234   if ((rv = hash_elts (cnat_client_db.crd_cip4)))
235     clib_warning ("len(crd_cip4) isnt 0 but %d", rv);
236   rrv |= rv;
237   if ((rv = pool_elts (cnat_client_pool)))
238     clib_warning ("len(cnat_client_pool) isnt 0 but %d", rv);
239   rrv |= rv;
240   if ((rv = hash_elts (cnat_client_db.throttle_mem)))
241     clib_warning ("len(throttle_mem) isnt 0 but %d", rv);
242   rrv |= rv;
243   return (rrv);
244 }
245
246 u8 *
247 format_cnat_client (u8 * s, va_list * args)
248 {
249   index_t cci = va_arg (*args, index_t);
250   u32 indent = va_arg (*args, u32);
251
252   cnat_client_t *cc = pool_elt_at_index (cnat_client_pool, cci);
253
254   s = format (s, "[%d] cnat-client:[%U] tr:%d sess:%d", cci,
255               format_ip_address, &cc->cc_ip,
256               cc->tr_refcnt, cc->session_refcnt);
257
258   if (cc->flags & CNAT_FLAG_EXCLUSIVE)
259     s = format (s, " exclusive");
260
261   if (cnat_client_is_clone (cc))
262     s = format (s, "\n%Uclone of [%d]\n%U%U",
263                 format_white_space, indent + 2, cc->parent_cci,
264                 format_white_space, indent + 2,
265                 format_dpo_id, &cc->cc_parent, indent + 4);
266
267   return (s);
268 }
269
270
271 static clib_error_t *
272 cnat_client_show (vlib_main_t * vm,
273                   unformat_input_t * input, vlib_cli_command_t * cmd)
274 {
275   index_t cci;
276
277   cci = INDEX_INVALID;
278
279   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
280     {
281       if (unformat (input, "%d", &cci))
282         ;
283       else
284         return (clib_error_return (0, "unknown input '%U'",
285                                    format_unformat_error, input));
286     }
287
288   if (INDEX_INVALID == cci)
289     {
290       pool_foreach_index (cci, cnat_client_pool)
291         vlib_cli_output(vm, "%U", format_cnat_client, cci, 0);
292
293       vlib_cli_output (vm, "%d clients", pool_elts (cnat_client_pool));
294       vlib_cli_output (vm, "%d timestamps", pool_elts (cnat_timestamps));
295     }
296   else
297     {
298       vlib_cli_output (vm, "Invalid policy ID:%d", cci);
299     }
300
301   return (NULL);
302 }
303
304 /* *INDENT-OFF* */
305 VLIB_CLI_COMMAND (cnat_client_show_cmd_node, static) = {
306   .path = "show cnat client",
307   .function = cnat_client_show,
308   .short_help = "show cnat client",
309   .is_mp_safe = 1,
310 };
311 /* *INDENT-ON* */
312
313 const static char *const cnat_client_dpo_ip4_nodes[] = {
314   "ip4-cnat-tx",
315   NULL,
316 };
317
318 const static char *const cnat_client_dpo_ip6_nodes[] = {
319   "ip6-cnat-tx",
320   NULL,
321 };
322
323 const static char *const *const cnat_client_dpo_nodes[DPO_PROTO_NUM] = {
324   [DPO_PROTO_IP4] = cnat_client_dpo_ip4_nodes,
325   [DPO_PROTO_IP6] = cnat_client_dpo_ip6_nodes,
326 };
327
328 static void
329 cnat_client_dpo_lock (dpo_id_t * dpo)
330 {
331   cnat_client_t *cc;
332
333   cc = cnat_client_get (dpo->dpoi_index);
334
335   cc->cc_locks++;
336 }
337
338 static void
339 cnat_client_dpo_unlock (dpo_id_t * dpo)
340 {
341   cnat_client_t *cc;
342
343   cc = cnat_client_get (dpo->dpoi_index);
344
345   cc->cc_locks--;
346
347   if (0 == cc->cc_locks)
348     {
349       ASSERT (cnat_client_is_clone (cc));
350       dpo_reset (&cc->cc_parent);
351       pool_put (cnat_client_pool, cc);
352     }
353 }
354
355 u8 *
356 format_cnat_client_dpo (u8 * s, va_list * ap)
357 {
358   index_t cci = va_arg (*ap, index_t);
359   u32 indent = va_arg (*ap, u32);
360
361   s = format (s, "%U", format_cnat_client, cci, indent);
362
363   return (s);
364 }
365
366 const static dpo_vft_t cnat_client_dpo_vft = {
367   .dv_lock = cnat_client_dpo_lock,
368   .dv_unlock = cnat_client_dpo_unlock,
369   .dv_format = format_cnat_client_dpo,
370   .dv_mk_interpose = cnat_client_dpo_interpose,
371 };
372
373 static clib_error_t *
374 cnat_client_init (vlib_main_t * vm)
375 {
376   cnat_client_dpo = dpo_register_new_type (&cnat_client_dpo_vft,
377                                            cnat_client_dpo_nodes);
378
379   cnat_client_db.crd_cip6 = hash_create_mem (0,
380                                              sizeof (ip6_address_t),
381                                              sizeof (uword));
382
383   clib_spinlock_init (&cnat_client_db.throttle_lock);
384   cnat_client_db.throttle_mem =
385     hash_create_mem (0, sizeof (ip_address_t), sizeof (uword));
386
387   return (NULL);
388 }
389
390 VLIB_INIT_FUNCTION (cnat_client_init);
391
392 /*
393  * fd.io coding-style-patch-verification: ON
394  *
395  * Local Variables:
396  * eval: (c-set-style "gnu")
397  * End:
398  */