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