bd456c82fef5fc18f8943c6f8664c95d60984267
[vpp.git] / src / plugins / cnat / cnat_translation.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_source.h>
17 #include <vnet/fib/fib_table.h>
18 #include <vnet/fib/fib_entry_track.h>
19 #include <vnet/dpo/load_balance.h>
20 #include <vnet/dpo/drop_dpo.h>
21
22 #include <cnat/cnat_translation.h>
23 #include <cnat/cnat_session.h>
24 #include <cnat/cnat_client.h>
25
26 cnat_translation_t *cnat_translation_pool;
27 clib_bihash_8_8_t cnat_translation_db;
28 addr_resolution_t *tr_resolutions;
29
30 typedef void (*cnat_if_addr_add_cb_t) (addr_resolution_t * ar,
31                                        ip_address_t * address, u8 is_del);
32 cnat_if_addr_add_cb_t *cnat_if_addr_add_cbs;
33
34 static fib_node_type_t cnat_translation_fib_node_type;
35
36 vlib_combined_counter_main_t cnat_translation_counters = {
37   .name = "cnat-translation",
38   .stat_segment_name = "/net/cnat-translation",
39 };
40
41 void
42 cnat_translation_watch_addr (index_t cti, u64 opaque, cnat_endpoint_t * ep,
43                              cnat_addr_resol_type_t type)
44 {
45   addr_resolution_t *ar;
46
47   if (INDEX_INVALID == ep->ce_sw_if_index)
48     return;
49
50   pool_get (tr_resolutions, ar);
51   ar->af = ep->ce_ip.version;
52   ar->sw_if_index = ep->ce_sw_if_index;
53   ar->type = type;
54   ar->opaque = opaque;
55   ar->cti = cti;
56 }
57
58 static void
59 cnat_resolve_ep_tuple (cnat_endpoint_tuple_t * path)
60 {
61   cnat_resolve_ep (&path->src_ep);
62   cnat_resolve_ep (&path->dst_ep);
63 }
64
65 void
66 cnat_translation_unwatch_addr (u32 cti, cnat_addr_resol_type_t type)
67 {
68   /* Delete tr resolution entries matching translation index */
69   addr_resolution_t *ar;
70   index_t *indexes = 0, *ari;
71   pool_foreach (ar, tr_resolutions)
72     {
73       if ((cti == INDEX_INVALID || ar->cti == cti) &&
74           (ar->type == type || CNAT_RESOLV_ADDR_ANY == type))
75         vec_add1 (indexes, ar - tr_resolutions);
76     }
77   vec_foreach (ari, indexes) pool_put_index (tr_resolutions, *ari);
78
79   vec_free (indexes);
80 }
81
82 static void
83 cnat_tracker_release (cnat_ep_trk_t * trk)
84 {
85   /* We only track fully resolved endpoints */
86   if (!(trk->ct_flags & CNAT_TRK_ACTIVE))
87     return;
88   fib_entry_untrack (trk->ct_fei, trk->ct_sibling);
89 }
90
91 static void
92 cnat_tracker_track (index_t cti, cnat_ep_trk_t * trk)
93 {
94   fib_prefix_t pfx;
95   /* We only track fully resolved endpoints */
96   if (trk->ct_ep[VLIB_TX].ce_flags & CNAT_EP_FLAG_RESOLVED &&
97       trk->ct_ep[VLIB_RX].ce_flags & CNAT_EP_FLAG_RESOLVED)
98     trk->ct_flags |= CNAT_TRK_ACTIVE;
99   else
100     {
101       trk->ct_flags &= ~CNAT_TRK_ACTIVE;
102       return;
103     }
104
105   ip_address_to_fib_prefix (&trk->ct_ep[VLIB_TX].ce_ip, &pfx);
106   trk->ct_fei = fib_entry_track (CNAT_FIB_TABLE,
107                                  &pfx,
108                                  cnat_translation_fib_node_type,
109                                  cti, &trk->ct_sibling);
110
111   fib_entry_contribute_forwarding (trk->ct_fei,
112                                    fib_forw_chain_type_from_fib_proto
113                                    (pfx.fp_proto), &trk->ct_dpo);
114 }
115
116 u8 *
117 format_cnat_lb_type (u8 *s, va_list *args)
118 {
119   cnat_lb_type_t lb_type = va_arg (*args, int);
120   if (CNAT_LB_DEFAULT == lb_type)
121     s = format (s, "default");
122   else if (CNAT_LB_MAGLEV == lb_type)
123     s = format (s, "maglev");
124   else
125     s = format (s, "unknown");
126   return (s);
127 }
128
129 uword
130 unformat_cnat_lb_type (unformat_input_t *input, va_list *args)
131 {
132   cnat_lb_type_t *a = va_arg (*args, cnat_lb_type_t *);
133   if (unformat (input, "default"))
134     *a = CNAT_LB_DEFAULT;
135   else if (unformat (input, "maglev"))
136     *a = CNAT_LB_MAGLEV;
137   else
138     return 0;
139   return 1;
140 }
141
142 /**
143  * Add a translation to the bihash
144  *
145  * @param cci the ID of the parent client (invalid if vip not resolved)
146  * @param vip the translation endpoint
147  * @param proto the translation proto
148  * @param cti the translation index to be used as value
149  */
150 static void
151 cnat_add_translation_to_db (index_t cci, cnat_endpoint_t * vip,
152                             ip_protocol_t proto, index_t cti)
153 {
154   clib_bihash_kv_8_8_t bkey;
155   u64 key;
156   if (INDEX_INVALID == cci)
157     {
158       key = proto << 8 | 0x80 | vip->ce_ip.version;
159       key = key << 16 | vip->ce_port;
160       key = key << 32 | (u32) vip->ce_sw_if_index;
161     }
162   else
163     {
164       key = proto << 8;
165       key = key << 16 | vip->ce_port;
166       key = key << 32 | (u32) cci;
167     }
168
169   bkey.key = key;
170   bkey.value = cti;
171
172   clib_bihash_add_del_8_8 (&cnat_translation_db, &bkey, 1);
173 }
174
175 /**
176  * Remove a translation from the bihash
177  *
178  * @param cci the ID of the parent client
179  * @param vip the translation endpoint
180  * @param proto the translation proto
181  */
182 static void
183 cnat_remove_translation_from_db (index_t cci, cnat_endpoint_t * vip,
184                                  ip_protocol_t proto)
185 {
186   clib_bihash_kv_8_8_t bkey;
187   u64 key;
188   if (INDEX_INVALID == cci)
189     {
190       key = proto << 8 | 0x80 | vip->ce_ip.version;
191       key = key << 16 | vip->ce_port;
192       key = key << 32 | (u32) vip->ce_sw_if_index;
193     }
194   else
195     {
196       key = proto << 8;
197       key = key << 16 | vip->ce_port;
198       key = key << 32 | (u32) cci;
199     }
200
201   bkey.key = key;
202
203   clib_bihash_add_del_8_8 (&cnat_translation_db, &bkey, 0);
204 }
205
206 typedef struct
207 {
208   cnat_ep_trk_t *trk;
209   u32 index;
210   u32 offset;
211   u32 skip;
212 } cnat_maglev_entry_t;
213
214 static int
215 cnat_maglev_entry_compare (void *_a, void *_b)
216 {
217   cnat_ep_trk_t *a = ((cnat_maglev_entry_t *) _a)->trk;
218   cnat_ep_trk_t *b = ((cnat_maglev_entry_t *) _b)->trk;
219   int rv = 0;
220   if ((rv =
221          ip_address_cmp (&a->ct_ep[VLIB_TX].ce_ip, &b->ct_ep[VLIB_TX].ce_ip)))
222     return rv;
223   if ((rv = a->ct_ep[VLIB_TX].ce_port - a->ct_ep[VLIB_TX].ce_port))
224     return rv;
225   if ((rv =
226          ip_address_cmp (&a->ct_ep[VLIB_RX].ce_ip, &b->ct_ep[VLIB_RX].ce_ip)))
227     return rv;
228   if ((rv = a->ct_ep[VLIB_RX].ce_port - a->ct_ep[VLIB_RX].ce_port))
229     return rv;
230   return 0;
231 }
232
233 static void
234 cnat_translation_init_maglev (cnat_translation_t *ct)
235 {
236   cnat_maglev_entry_t *backends = NULL, *bk;
237   cnat_main_t *cm = &cnat_main;
238   u32 done = 0;
239   cnat_ep_trk_t *trk;
240   int ep_idx = 0;
241
242   vec_foreach (trk, ct->ct_active_paths)
243     {
244       cnat_maglev_entry_t bk;
245       u32 h1, h2;
246
247       if (AF_IP4 == ip_addr_version (&trk->ct_ep[VLIB_TX].ce_ip))
248         {
249           u32 a, b, c;
250           a = ip_addr_v4 (&trk->ct_ep[VLIB_TX].ce_ip).data_u32;
251           b = (u64) trk->ct_ep[VLIB_TX].ce_port << 16 |
252               (u64) trk->ct_ep[VLIB_RX].ce_port;
253           c = ip_addr_v4 (&trk->ct_ep[VLIB_RX].ce_ip).data_u32;
254           hash_v3_mix32 (a, b, c);
255           hash_v3_finalize32 (a, b, c);
256           h1 = c;
257           h2 = b;
258         }
259       else
260         {
261           u64 a, b, c;
262           a = ip_addr_v6 (&trk->ct_ep[VLIB_TX].ce_ip).as_u64[0] ^
263               ip_addr_v6 (&trk->ct_ep[VLIB_TX].ce_ip).as_u64[1];
264           b = (u64) trk->ct_ep[VLIB_TX].ce_port << 16 |
265               (u64) trk->ct_ep[VLIB_RX].ce_port;
266           c = ip_addr_v6 (&trk->ct_ep[VLIB_RX].ce_ip).as_u64[0] ^
267               ip_addr_v6 (&trk->ct_ep[VLIB_RX].ce_ip).as_u64[1];
268           hash_mix64 (a, b, c);
269           h1 = c;
270           h2 = b;
271         }
272
273       bk.offset = h1 % cm->maglev_len;
274       bk.skip = h2 % (cm->maglev_len - 1) + 1;
275       bk.index = ep_idx++;
276       bk.trk = trk;
277       vec_add1 (backends, bk);
278     }
279
280   if (0 == ep_idx)
281     return;
282
283   vec_sort_with_function (backends, cnat_maglev_entry_compare);
284
285   /* Don't free if previous vector exists, just zero */
286   vec_validate (ct->lb_maglev, cm->maglev_len);
287   vec_set (ct->lb_maglev, -1);
288
289   while (1)
290     {
291       vec_foreach (bk, backends)
292         {
293           u32 next = 0;
294           u32 c = (bk->offset + next * bk->skip) % cm->maglev_len;
295           while (ct->lb_maglev[c] != (u32) -1)
296             {
297               next++;
298               c = (bk->offset + next * bk->skip) % cm->maglev_len;
299             }
300           ct->lb_maglev[c] = bk->index;
301           done++;
302           if (done == cm->maglev_len)
303             goto finished;
304         }
305     }
306
307 finished:
308   vec_free (backends);
309 }
310
311 static void
312 cnat_translation_stack (cnat_translation_t * ct)
313 {
314   fib_protocol_t fproto;
315   cnat_ep_trk_t *trk;
316   dpo_proto_t dproto;
317   u32 ep_idx = 0;
318   index_t lbi;
319
320   fproto = ip_address_family_to_fib_proto (ct->ct_vip.ce_ip.version);
321   dproto = fib_proto_to_dpo (fproto);
322
323   vec_reset_length (ct->ct_active_paths);
324
325   vec_foreach (trk, ct->ct_paths)
326     if (trk->ct_flags & CNAT_TRK_ACTIVE)
327       vec_add1 (ct->ct_active_paths, *trk);
328
329   lbi = load_balance_create (vec_len (ct->ct_active_paths),
330                              fib_proto_to_dpo (fproto), IP_FLOW_HASH_DEFAULT);
331
332   ep_idx = 0;
333   vec_foreach (trk, ct->ct_active_paths)
334     load_balance_set_bucket (lbi, ep_idx++, &trk->ct_dpo);
335
336   if (ep_idx > 0 && CNAT_LB_MAGLEV == ct->lb_type)
337     cnat_translation_init_maglev (ct);
338
339   dpo_set (&ct->ct_lb, DPO_LOAD_BALANCE, dproto, lbi);
340   dpo_stack (cnat_client_dpo, dproto, &ct->ct_lb, &ct->ct_lb);
341   ct->flags |= CNAT_TRANSLATION_STACKED;
342 }
343
344 int
345 cnat_translation_delete (u32 id)
346 {
347   cnat_translation_t *ct;
348   cnat_ep_trk_t *trk;
349
350   if (pool_is_free_index (cnat_translation_pool, id))
351     return (VNET_API_ERROR_NO_SUCH_ENTRY);
352
353   ct = pool_elt_at_index (cnat_translation_pool, id);
354
355   dpo_reset (&ct->ct_lb);
356
357   vec_foreach (trk, ct->ct_active_paths)
358     cnat_tracker_release (trk);
359
360   cnat_remove_translation_from_db (ct->ct_cci, &ct->ct_vip, ct->ct_proto);
361   cnat_client_translation_deleted (ct->ct_cci);
362   cnat_translation_unwatch_addr (id, CNAT_RESOLV_ADDR_ANY);
363   pool_put (cnat_translation_pool, ct);
364
365   return (0);
366 }
367
368 u32
369 cnat_translation_update (cnat_endpoint_t *vip, ip_protocol_t proto,
370                          cnat_endpoint_tuple_t *paths, u8 flags,
371                          cnat_lb_type_t lb_type)
372 {
373   cnat_endpoint_tuple_t *path;
374   const cnat_client_t *cc;
375   cnat_translation_t *ct;
376   cnat_ep_trk_t *trk;
377   index_t cci;
378
379   cnat_lazy_init ();
380   if (cnat_resolve_ep (vip))
381     {
382       /* vip only contains a sw_if_index for now */
383       ct = cnat_find_translation (vip->ce_sw_if_index, vip->ce_port, proto);
384       cci = INDEX_INVALID;
385     }
386   else
387     {
388       /* do we know of this ep's vip */
389       cci = cnat_client_add (&vip->ce_ip, flags);
390       cc = cnat_client_get (cci);
391
392       ct = cnat_find_translation (cc->parent_cci, vip->ce_port, proto);
393     }
394
395   if (NULL == ct)
396     {
397       pool_get_zero (cnat_translation_pool, ct);
398
399       clib_memcpy (&ct->ct_vip, vip, sizeof (*vip));
400       ct->ct_proto = proto;
401       ct->ct_cci = cci;
402       ct->index = ct - cnat_translation_pool;
403       ct->lb_type = lb_type;
404
405       cnat_add_translation_to_db (cci, vip, proto, ct->index);
406       cnat_client_translation_added (cci);
407
408       vlib_validate_combined_counter (&cnat_translation_counters, ct->index);
409       vlib_zero_combined_counter (&cnat_translation_counters, ct->index);
410     }
411   ct->flags = flags;
412
413   cnat_translation_unwatch_addr (ct->index, CNAT_RESOLV_ADDR_ANY);
414   cnat_translation_watch_addr (ct->index, 0, vip,
415                                CNAT_RESOLV_ADDR_TRANSLATION);
416
417   vec_foreach (trk, ct->ct_paths)
418   {
419     cnat_tracker_release (trk);
420   }
421
422   vec_reset_length (ct->ct_paths);
423   ct->flags &= ~CNAT_TRANSLATION_STACKED;
424
425   u64 path_idx = 0;
426   vec_foreach (path, paths)
427   {
428     cnat_resolve_ep_tuple (path);
429     cnat_translation_watch_addr (ct->index,
430                                  path_idx << 32 | VLIB_RX, &path->src_ep,
431                                  CNAT_RESOLV_ADDR_BACKEND);
432     cnat_translation_watch_addr (ct->index,
433                                  path_idx << 32 | VLIB_TX, &path->dst_ep,
434                                  CNAT_RESOLV_ADDR_BACKEND);
435     path_idx++;
436
437     vec_add2 (ct->ct_paths, trk, 1);
438
439     clib_memcpy (&trk->ct_ep[VLIB_TX], &path->dst_ep,
440                  sizeof (trk->ct_ep[VLIB_TX]));
441     clib_memcpy (&trk->ct_ep[VLIB_RX], &path->src_ep,
442                  sizeof (trk->ct_ep[VLIB_RX]));
443     trk->ct_flags = path->ep_flags;
444
445     cnat_tracker_track (ct->index, trk);
446   }
447
448   cnat_translation_stack (ct);
449
450   return (ct->index);
451 }
452
453 void
454 cnat_translation_walk (cnat_translation_walk_cb_t cb, void *ctx)
455 {
456   u32 api;
457
458   pool_foreach_index (api, cnat_translation_pool)
459    {
460     if (!cb(api, ctx))
461       break;
462   }
463 }
464
465 static u8 *
466 format_cnat_ep_trk (u8 * s, va_list * args)
467 {
468   cnat_ep_trk_t *ck = va_arg (*args, cnat_ep_trk_t *);
469   u32 indent = va_arg (*args, u32);
470
471   s = format (s, "%U->%U", format_cnat_endpoint, &ck->ct_ep[VLIB_RX],
472               format_cnat_endpoint, &ck->ct_ep[VLIB_TX]);
473   s = format (s, "\n%Ufib-entry:%d", format_white_space, indent, ck->ct_fei);
474   s = format (s, "\n%U%U",
475               format_white_space, indent, format_dpo_id, &ck->ct_dpo, 6);
476
477   return (s);
478 }
479
480 u8 *
481 format_cnat_translation (u8 * s, va_list * args)
482 {
483   cnat_translation_t *ct = va_arg (*args, cnat_translation_t *);
484   cnat_main_t *cm = &cnat_main;
485   cnat_ep_trk_t *ck;
486
487   s = format (s, "[%d] ", ct->index);
488   s = format (s, "%U %U ", format_cnat_endpoint, &ct->ct_vip,
489               format_ip_protocol, ct->ct_proto);
490   s = format (s, "lb:%U ", format_cnat_lb_type, ct->lb_type);
491
492   vec_foreach (ck, ct->ct_paths)
493     s = format (s, "\n%U", format_cnat_ep_trk, ck, 2);
494
495   /* If printing a trace, the LB object might be deleted */
496   if (!pool_is_free_index (load_balance_pool, ct->ct_lb.dpoi_index))
497     {
498       s = format (s, "\n via:");
499       s = format (s, "\n%U%U",
500                   format_white_space, 2, format_dpo_id, &ct->ct_lb, 2);
501     }
502
503   u32 bid = 0;
504   if (CNAT_LB_MAGLEV == ct->lb_type)
505     {
506       s = format (s, "\nmaglev backends map");
507       uword *bitmap = NULL;
508       clib_bitmap_alloc (bitmap, cm->maglev_len);
509       vec_foreach (ck, ct->ct_paths)
510         {
511           clib_bitmap_zero (bitmap);
512           for (u32 i = 0; i < vec_len (ct->lb_maglev); i++)
513             if (ct->lb_maglev[i] == bid)
514               clib_bitmap_set (bitmap, i, 1);
515           s = format (s, "\n  backend#%d: %U", bid, format_bitmap_hex, bitmap);
516
517           bid++;
518         }
519       clib_bitmap_free (bitmap);
520     }
521
522   return (s);
523 }
524
525 static clib_error_t *
526 cnat_translation_show (vlib_main_t * vm,
527                        unformat_input_t * input, vlib_cli_command_t * cmd)
528 {
529   index_t cti;
530   cnat_translation_t *ct;
531
532   cti = INDEX_INVALID;
533
534   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
535     {
536       if (unformat (input, "%d", &cti))
537         ;
538       else
539         return (clib_error_return (0, "unknown input '%U'",
540                                    format_unformat_error, input));
541     }
542
543   if (INDEX_INVALID == cti)
544     {
545       pool_foreach_index (cti, cnat_translation_pool)
546        {
547         ct = pool_elt_at_index (cnat_translation_pool, cti);
548         vlib_cli_output(vm, "%U", format_cnat_translation, ct);
549       }
550     }
551   else
552     {
553       vlib_cli_output (vm, "Invalid policy ID:%d", cti);
554     }
555
556   return (NULL);
557 }
558
559 int
560 cnat_translation_purge (void)
561 {
562   /* purge all the translations */
563   index_t tri, *trp, *trs = NULL;
564
565   pool_foreach_index (tri, cnat_translation_pool)
566    {
567     vec_add1(trs, tri);
568   }
569
570   vec_foreach (trp, trs) cnat_translation_delete (*trp);
571
572   ASSERT (0 == pool_elts (cnat_translation_pool));
573
574   vec_free (trs);
575
576   return (0);
577 }
578
579 VLIB_CLI_COMMAND (cnat_translation_show_cmd_node, static) = {
580   .path = "show cnat translation",
581   .function = cnat_translation_show,
582   .short_help = "show cnat translation <VIP>",
583   .is_mp_safe = 1,
584 };
585
586 static fib_node_t *
587 cnat_translation_get_node (fib_node_index_t index)
588 {
589   cnat_translation_t *ct = cnat_translation_get (index);
590   return (&(ct->ct_node));
591 }
592
593 static cnat_translation_t *
594 cnat_translation_get_from_node (fib_node_t * node)
595 {
596   return ((cnat_translation_t *) (((char *) node) -
597                                   STRUCT_OFFSET_OF (cnat_translation_t,
598                                                     ct_node)));
599 }
600
601 static void
602 cnat_translation_last_lock_gone (fib_node_t * node)
603 {
604  /**/}
605
606 /*
607  * A back walk has reached this ABF policy
608  */
609 static fib_node_back_walk_rc_t
610 cnat_translation_back_walk_notify (fib_node_t * node,
611                                    fib_node_back_walk_ctx_t * ctx)
612 {
613   /*
614    * re-stack the fmask on the n-eos of the via
615    */
616   cnat_translation_t *ct = cnat_translation_get_from_node (node);
617
618   /* If we have more than FIB_PATH_LIST_POPULAR paths
619    * we might get called during path tracking
620    * (cnat_tracker_track) */
621   if (!(ct->flags & CNAT_TRANSLATION_STACKED))
622     return (FIB_NODE_BACK_WALK_CONTINUE);
623
624   cnat_translation_stack (ct);
625
626   return (FIB_NODE_BACK_WALK_CONTINUE);
627 }
628
629 /*
630  * The translation's graph node virtual function table
631  */
632 static const fib_node_vft_t cnat_translation_vft = {
633   .fnv_get = cnat_translation_get_node,
634   .fnv_last_lock = cnat_translation_last_lock_gone,
635   .fnv_back_walk = cnat_translation_back_walk_notify,
636 };
637
638 static clib_error_t *
639 cnat_translation_cli_add_del (vlib_main_t * vm,
640                               unformat_input_t * input,
641                               vlib_cli_command_t * cmd)
642 {
643   u32 del_index = INDEX_INVALID;
644   ip_protocol_t proto = IP_PROTOCOL_TCP;
645   cnat_endpoint_t vip;
646   u8 flags = CNAT_FLAG_EXCLUSIVE;
647   cnat_endpoint_tuple_t tmp, *paths = NULL, *path;
648   unformat_input_t _line_input, *line_input = &_line_input;
649   clib_error_t *e = 0;
650   cnat_lb_type_t lb_type;
651
652   /* Get a line of input. */
653   if (!unformat_user (input, unformat_line_input, line_input))
654     return 0;
655
656   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
657     {
658       if (unformat (line_input, "add"))
659         del_index = INDEX_INVALID;
660       else if (unformat (line_input, "del %d", &del_index))
661         ;
662       else
663         if (unformat (line_input, "proto %U", unformat_ip_protocol, &proto))
664         ;
665       else if (unformat (line_input, "vip %U", unformat_cnat_ep, &vip))
666         flags = CNAT_FLAG_EXCLUSIVE;
667       else if (unformat (line_input, "real %U", unformat_cnat_ep, &vip))
668         flags = 0;
669       else if (unformat (line_input, "to %U", unformat_cnat_ep_tuple, &tmp))
670         {
671           vec_add2 (paths, path, 1);
672           clib_memcpy (path, &tmp, sizeof (cnat_endpoint_tuple_t));
673         }
674       else if (unformat (line_input, "%U", unformat_cnat_lb_type, &lb_type))
675         ;
676       else
677         {
678           e = clib_error_return (0, "unknown input '%U'",
679                                  format_unformat_error, line_input);
680           goto done;
681         }
682     }
683
684   if (INDEX_INVALID == del_index)
685     cnat_translation_update (&vip, proto, paths, flags, lb_type);
686   else
687     cnat_translation_delete (del_index);
688
689 done:
690   vec_free (paths);
691   unformat_free (line_input);
692   return (e);
693 }
694
695 VLIB_CLI_COMMAND (cnat_translation_cli_add_del_command, static) =
696 {
697   .path = "cnat translation",
698   .short_help = "cnat translation [add|del] proto [TCP|UDP] [vip|real] [ip|sw_if_index [v6]] [port] [to [ip|sw_if_index [v6]] [port]->[ip|sw_if_index [v6]] [port]]",
699   .function = cnat_translation_cli_add_del,
700 };
701
702 static void
703 cnat_if_addr_add_del_translation_cb (addr_resolution_t * ar,
704                                      ip_address_t * address, u8 is_del)
705 {
706   cnat_translation_t *ct;
707   ct = cnat_translation_get (ar->cti);
708   if (!is_del && ct->ct_vip.ce_flags & CNAT_EP_FLAG_RESOLVED)
709     return;
710
711   cnat_remove_translation_from_db (ct->ct_cci, &ct->ct_vip, ct->ct_proto);
712
713   if (is_del)
714     {
715       ct->ct_vip.ce_flags &= ~CNAT_EP_FLAG_RESOLVED;
716       ct->ct_cci = INDEX_INVALID;
717       cnat_client_translation_deleted (ct->ct_cci);
718       /* Are there remaining addresses ? */
719       if (0 == cnat_resolve_addr (ar->sw_if_index, ar->af, address))
720         is_del = 0;
721     }
722
723   if (!is_del)
724     {
725       ct->ct_cci = cnat_client_add (address, ct->flags);
726       cnat_client_translation_added (ct->ct_cci);
727       ip_address_copy (&ct->ct_vip.ce_ip, address);
728       ct->ct_vip.ce_flags |= CNAT_EP_FLAG_RESOLVED;
729     }
730
731   cnat_add_translation_to_db (ct->ct_cci, &ct->ct_vip, ct->ct_proto,
732                               ct->index);
733 }
734
735 static void
736 cnat_if_addr_add_del_backend_cb (addr_resolution_t * ar,
737                                  ip_address_t * address, u8 is_del)
738 {
739   cnat_translation_t *ct;
740   cnat_ep_trk_t *trk;
741   cnat_endpoint_t *ep;
742
743   u8 direction = ar->opaque & 0xf;
744   u32 path_idx = ar->opaque >> 32;
745
746   ct = cnat_translation_get (ar->cti);
747
748   trk = &ct->ct_paths[path_idx];
749   ep = &trk->ct_ep[direction];
750
751   if (!is_del && ep->ce_flags & CNAT_EP_FLAG_RESOLVED)
752     return;
753
754   ASSERT (ep->ce_sw_if_index == ar->sw_if_index);
755
756   if (is_del)
757     {
758       ep->ce_flags &= ~CNAT_EP_FLAG_RESOLVED;
759       /* Are there remaining addresses ? */
760       if (0 == cnat_resolve_addr (ar->sw_if_index, ar->af, address))
761         is_del = 0;
762     }
763
764   if (!is_del)
765     {
766       ip_address_copy (&ep->ce_ip, address);
767       ep->ce_flags |= CNAT_EP_FLAG_RESOLVED;
768     }
769
770   ct->flags &= ~CNAT_TRANSLATION_STACKED;
771   cnat_tracker_track (ar->cti, trk);
772
773   cnat_translation_stack (ct);
774   ct->flags |= CNAT_TRANSLATION_STACKED;
775 }
776
777 static void
778 cnat_if_addr_add_del_snat_cb (addr_resolution_t * ar, ip_address_t * address,
779                               u8 is_del)
780 {
781   cnat_endpoint_t *ep;
782   ep = AF_IP4 == ar->af ? &cnat_main.snat_ip4 : &cnat_main.snat_ip6;
783
784   if (!is_del && ep->ce_flags & CNAT_EP_FLAG_RESOLVED)
785     return;
786
787   if (is_del)
788     {
789       ep->ce_flags &= ~CNAT_EP_FLAG_RESOLVED;
790       /* Are there remaining addresses ? */
791       if (0 == cnat_resolve_addr (ar->sw_if_index, ar->af, address))
792         is_del = 0;
793     }
794
795   if (!is_del)
796     {
797       ip_address_copy (&ep->ce_ip, address);
798       ep->ce_flags |= CNAT_EP_FLAG_RESOLVED;
799     }
800
801 }
802
803 static void
804 cnat_if_addr_add_del_callback (u32 sw_if_index, ip_address_t * address,
805                                u8 is_del)
806 {
807   addr_resolution_t *ar;
808   pool_foreach (ar, tr_resolutions)
809     {
810       if (ar->sw_if_index != sw_if_index)
811         continue;
812       if (ar->af != ip_addr_version (address))
813         continue;
814       cnat_if_addr_add_cbs[ar->type](ar, address, is_del);
815     }
816 }
817
818 static void
819 cnat_ip6_if_addr_add_del_callback (struct ip6_main_t *im,
820                                    uword opaque, u32 sw_if_index,
821                                    ip6_address_t * address,
822                                    u32 address_length, u32 if_address_index,
823                                    u32 is_del)
824 {
825   ip_address_t addr;
826   ip_address_set (&addr, address, AF_IP6);
827   cnat_if_addr_add_del_callback (sw_if_index, &addr, is_del);
828 }
829
830 static void
831 cnat_ip4_if_addr_add_del_callback (struct ip4_main_t *im,
832                                    uword opaque, u32 sw_if_index,
833                                    ip4_address_t * address,
834                                    u32 address_length, u32 if_address_index,
835                                    u32 is_del)
836 {
837   ip_address_t addr;
838   ip_address_set (&addr, address, AF_IP4);
839   cnat_if_addr_add_del_callback (sw_if_index, &addr, is_del);
840 }
841
842 static clib_error_t *
843 cnat_translation_init (vlib_main_t * vm)
844 {
845   ip4_main_t *i4m = &ip4_main;
846   ip6_main_t *i6m = &ip6_main;
847   cnat_main_t *cm = &cnat_main;
848   cnat_translation_fib_node_type =
849     fib_node_register_new_type (&cnat_translation_vft);
850
851   clib_bihash_init_8_8 (&cnat_translation_db, "CNat translation DB",
852                         cm->translation_hash_buckets,
853                         cm->translation_hash_memory);
854
855   ip4_add_del_interface_address_callback_t cb4 = { 0 };
856   cb4.function = cnat_ip4_if_addr_add_del_callback;
857   vec_add1 (i4m->add_del_interface_address_callbacks, cb4);
858
859   ip6_add_del_interface_address_callback_t cb6 = { 0 };
860   cb6.function = cnat_ip6_if_addr_add_del_callback;
861   vec_add1 (i6m->add_del_interface_address_callbacks, cb6);
862
863   vec_validate (cnat_if_addr_add_cbs, CNAT_ADDR_N_RESOLUTIONS);
864   cnat_if_addr_add_cbs[CNAT_RESOLV_ADDR_BACKEND] =
865     cnat_if_addr_add_del_backend_cb;
866   cnat_if_addr_add_cbs[CNAT_RESOLV_ADDR_SNAT] = cnat_if_addr_add_del_snat_cb;
867   cnat_if_addr_add_cbs[CNAT_RESOLV_ADDR_TRANSLATION] =
868     cnat_if_addr_add_del_translation_cb;
869   return (NULL);
870 }
871
872 VLIB_INIT_FUNCTION (cnat_translation_init);
873
874 /*
875  * fd.io coding-style-patch-verification: ON
876  *
877  * Local Variables:
878  * eval: (c-set-style "gnu")
879  * End:
880  */