cnat: undo fib_entry_contribute_forwarding
[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 #include <vnet/dpo/dpo.h>
22
23 #include <cnat/cnat_translation.h>
24 #include <cnat/cnat_maglev.h>
25 #include <cnat/cnat_session.h>
26 #include <cnat/cnat_client.h>
27
28 cnat_translation_t *cnat_translation_pool;
29 clib_bihash_8_8_t cnat_translation_db;
30 addr_resolution_t *tr_resolutions;
31 cnat_if_addr_add_cb_t *cnat_if_addr_add_cbs;
32
33 static fib_node_type_t cnat_translation_fib_node_type;
34
35 vlib_combined_counter_main_t cnat_translation_counters = {
36   .name = "cnat-translation",
37   .stat_segment_name = "/net/cnat-translation",
38 };
39
40 void
41 cnat_translation_watch_addr (index_t cti, u64 opaque, cnat_endpoint_t * ep,
42                              cnat_addr_resol_type_t type)
43 {
44   addr_resolution_t *ar;
45
46   if (INDEX_INVALID == ep->ce_sw_if_index)
47     return;
48
49   pool_get (tr_resolutions, ar);
50   ar->af = ep->ce_ip.version;
51   ar->sw_if_index = ep->ce_sw_if_index;
52   ar->type = type;
53   ar->opaque = opaque;
54   ar->cti = cti;
55 }
56
57 static void
58 cnat_resolve_ep_tuple (cnat_endpoint_tuple_t * path)
59 {
60   cnat_resolve_ep (&path->src_ep);
61   cnat_resolve_ep (&path->dst_ep);
62 }
63
64 void
65 cnat_translation_unwatch_addr (u32 cti, cnat_addr_resol_type_t type)
66 {
67   /* Delete tr resolution entries matching translation index */
68   addr_resolution_t *ar;
69   index_t *indexes = 0, *ari;
70   pool_foreach (ar, tr_resolutions)
71     {
72       if ((cti == INDEX_INVALID || ar->cti == cti) &&
73           (ar->type == type || CNAT_RESOLV_ADDR_ANY == type))
74         vec_add1 (indexes, ar - tr_resolutions);
75     }
76   vec_foreach (ari, indexes) pool_put_index (tr_resolutions, *ari);
77
78   vec_free (indexes);
79 }
80
81 static void
82 cnat_tracker_release (cnat_ep_trk_t * trk)
83 {
84   /* We only track fully resolved endpoints */
85   if (!(trk->ct_flags & CNAT_TRK_ACTIVE))
86     return;
87   dpo_reset (&trk->ct_dpo); // undo fib_entry_contribute_forwarding
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
207
208 static void
209 cnat_translation_stack (cnat_translation_t * ct)
210 {
211   fib_protocol_t fproto;
212   cnat_ep_trk_t *trk;
213   dpo_proto_t dproto;
214   u32 ep_idx = 0;
215   index_t lbi;
216
217   fproto = ip_address_family_to_fib_proto (ct->ct_vip.ce_ip.version);
218   dproto = fib_proto_to_dpo (fproto);
219
220   vec_reset_length (ct->ct_active_paths);
221
222   vec_foreach (trk, ct->ct_paths)
223     if (trk->ct_flags & CNAT_TRK_ACTIVE)
224       vec_add1 (ct->ct_active_paths, *trk);
225
226   flow_hash_config_t fhc = IP_FLOW_HASH_DEFAULT;
227   if (ct->fhc != 0)
228     fhc = ct->fhc;
229   lbi = load_balance_create (vec_len (ct->ct_active_paths),
230                              fib_proto_to_dpo (fproto), fhc);
231
232   ep_idx = 0;
233   vec_foreach (trk, ct->ct_active_paths)
234     load_balance_set_bucket (lbi, ep_idx++, &trk->ct_dpo);
235
236   if (ep_idx > 0 && CNAT_LB_MAGLEV == ct->lb_type)
237     cnat_translation_init_maglev (ct);
238
239   dpo_set (&ct->ct_lb, DPO_LOAD_BALANCE, dproto, lbi);
240   dpo_stack (cnat_client_dpo, dproto, &ct->ct_lb, &ct->ct_lb);
241   ct->flags |= CNAT_TR_FLAG_STACKED;
242 }
243
244 int
245 cnat_translation_delete (u32 id)
246 {
247   cnat_translation_t *ct;
248   cnat_ep_trk_t *trk;
249
250   if (pool_is_free_index (cnat_translation_pool, id))
251     return (VNET_API_ERROR_NO_SUCH_ENTRY);
252
253   ct = pool_elt_at_index (cnat_translation_pool, id);
254
255   dpo_reset (&ct->ct_lb);
256
257   vec_foreach (trk, ct->ct_active_paths)
258     cnat_tracker_release (trk);
259
260   cnat_remove_translation_from_db (ct->ct_cci, &ct->ct_vip, ct->ct_proto);
261   cnat_client_translation_deleted (ct->ct_cci);
262   cnat_translation_unwatch_addr (id, CNAT_RESOLV_ADDR_ANY);
263   pool_put (cnat_translation_pool, ct);
264
265   return (0);
266 }
267
268 u32
269 cnat_translation_update (cnat_endpoint_t *vip, ip_protocol_t proto,
270                          cnat_endpoint_tuple_t *paths, u8 flags,
271                          cnat_lb_type_t lb_type, flow_hash_config_t fhc)
272 {
273   const dpo_id_t tmp = DPO_INVALID;
274   cnat_endpoint_tuple_t *path;
275   const cnat_client_t *cc;
276   cnat_translation_t *ct;
277   cnat_ep_trk_t *trk;
278   index_t cci;
279
280   cnat_lazy_init ();
281   if (cnat_resolve_ep (vip))
282     {
283       /* vip only contains a sw_if_index for now */
284       ct = cnat_find_translation (vip->ce_sw_if_index, vip->ce_port, proto);
285       cci = INDEX_INVALID;
286     }
287   else
288     {
289       /* do we know of this ep's vip */
290       cci = cnat_client_add (&vip->ce_ip, flags);
291       cc = cnat_client_get (cci);
292
293       ct = cnat_find_translation (cc->parent_cci, vip->ce_port, proto);
294     }
295
296   if (NULL == ct)
297     {
298       pool_get_zero (cnat_translation_pool, ct);
299
300       clib_memcpy (&ct->ct_vip, vip, sizeof (*vip));
301       ct->ct_proto = proto;
302       ct->ct_cci = cci;
303       ct->index = ct - cnat_translation_pool;
304       ct->lb_type = lb_type;
305       ct->fhc = fhc;
306
307       cnat_add_translation_to_db (cci, vip, proto, ct->index);
308       cnat_client_translation_added (cci);
309
310       vlib_validate_combined_counter (&cnat_translation_counters, ct->index);
311       vlib_zero_combined_counter (&cnat_translation_counters, ct->index);
312     }
313   ct->flags = flags;
314
315   cnat_translation_unwatch_addr (ct->index, CNAT_RESOLV_ADDR_ANY);
316   cnat_translation_watch_addr (ct->index, 0, vip,
317                                CNAT_RESOLV_ADDR_TRANSLATION);
318
319   vec_foreach (trk, ct->ct_paths)
320   {
321     cnat_tracker_release (trk);
322   }
323
324   vec_reset_length (ct->ct_paths);
325   ct->flags &= ~CNAT_TR_FLAG_STACKED;
326
327   u64 path_idx = 0;
328   vec_foreach (path, paths)
329   {
330     cnat_resolve_ep_tuple (path);
331     cnat_translation_watch_addr (ct->index,
332                                  path_idx << 32 | VLIB_RX, &path->src_ep,
333                                  CNAT_RESOLV_ADDR_BACKEND);
334     cnat_translation_watch_addr (ct->index,
335                                  path_idx << 32 | VLIB_TX, &path->dst_ep,
336                                  CNAT_RESOLV_ADDR_BACKEND);
337     path_idx++;
338
339     vec_add2 (ct->ct_paths, trk, 1);
340
341     clib_memcpy (&trk->ct_ep[VLIB_TX], &path->dst_ep,
342                  sizeof (trk->ct_ep[VLIB_TX]));
343     clib_memcpy (&trk->ct_ep[VLIB_RX], &path->src_ep,
344                  sizeof (trk->ct_ep[VLIB_RX]));
345     trk->ct_flags = path->ep_flags;
346     trk->ct_dpo = tmp;
347
348     cnat_tracker_track (ct->index, trk);
349   }
350
351   cnat_translation_stack (ct);
352
353   return (ct->index);
354 }
355
356 void
357 cnat_translation_walk (cnat_translation_walk_cb_t cb, void *ctx)
358 {
359   u32 api;
360
361   pool_foreach_index (api, cnat_translation_pool)
362    {
363     if (!cb(api, ctx))
364       break;
365   }
366 }
367
368 static u8 *
369 format_cnat_ep_trk (u8 * s, va_list * args)
370 {
371   cnat_ep_trk_t *ck = va_arg (*args, cnat_ep_trk_t *);
372   u32 indent = va_arg (*args, u32);
373
374   s = format (s, "%U->%U", format_cnat_endpoint, &ck->ct_ep[VLIB_RX],
375               format_cnat_endpoint, &ck->ct_ep[VLIB_TX]);
376   s = format (s, "\n%Ufib-entry:%d", format_white_space, indent, ck->ct_fei);
377   s = format (s, "\n%U%U",
378               format_white_space, indent, format_dpo_id, &ck->ct_dpo, 6);
379
380   return (s);
381 }
382
383 u8 *
384 format_cnat_translation (u8 * s, va_list * args)
385 {
386   cnat_translation_t *ct = va_arg (*args, cnat_translation_t *);
387   cnat_main_t *cm = &cnat_main;
388   cnat_ep_trk_t *ck;
389
390   s = format (s, "[%d] ", ct->index);
391   s = format (s, "%U %U ", format_cnat_endpoint, &ct->ct_vip,
392               format_ip_protocol, ct->ct_proto);
393   s = format (s, "lb:%U ", format_cnat_lb_type, ct->lb_type);
394
395   if ((ct->fhc == 0) || (ct->fhc == IP_FLOW_HASH_DEFAULT))
396     s = format (s, "fhc:0x%x(default)", IP_FLOW_HASH_DEFAULT);
397   else
398     s = format (s, "fhc:0x%x", ct->fhc);
399
400   vec_foreach (ck, ct->ct_paths)
401     s = format (s, "\n%U", format_cnat_ep_trk, ck, 2);
402
403   /* If printing a trace, the LB object might be deleted */
404   if (!pool_is_free_index (load_balance_pool, ct->ct_lb.dpoi_index))
405     {
406       s = format (s, "\n via:");
407       s = format (s, "\n%U%U",
408                   format_white_space, 2, format_dpo_id, &ct->ct_lb, 2);
409     }
410
411   u32 bid = 0;
412   if (CNAT_LB_MAGLEV == ct->lb_type)
413     {
414       s = format (s, "\nmaglev backends map");
415       uword *bitmap = NULL;
416       clib_bitmap_alloc (bitmap, cm->maglev_len);
417       vec_foreach (ck, ct->ct_paths)
418         {
419           clib_bitmap_zero (bitmap);
420           for (u32 i = 0; i < vec_len (ct->lb_maglev); i++)
421             if (ct->lb_maglev[i] == bid)
422               clib_bitmap_set (bitmap, i, 1);
423           s = format (s, "\n  backend#%d: %U", bid, format_bitmap_hex, bitmap);
424
425           bid++;
426         }
427       clib_bitmap_free (bitmap);
428     }
429
430   return (s);
431 }
432
433 static clib_error_t *
434 cnat_translation_show (vlib_main_t * vm,
435                        unformat_input_t * input, vlib_cli_command_t * cmd)
436 {
437   index_t cti;
438   cnat_translation_t *ct;
439
440   cti = INDEX_INVALID;
441
442   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
443     {
444       if (unformat (input, "%d", &cti))
445         ;
446       else
447         return (clib_error_return (0, "unknown input '%U'",
448                                    format_unformat_error, input));
449     }
450
451   if (INDEX_INVALID == cti)
452     {
453       pool_foreach_index (cti, cnat_translation_pool)
454        {
455         ct = pool_elt_at_index (cnat_translation_pool, cti);
456         vlib_cli_output(vm, "%U", format_cnat_translation, ct);
457       }
458     }
459   else
460     {
461       vlib_cli_output (vm, "Invalid policy ID:%d", cti);
462     }
463
464   return (NULL);
465 }
466
467 int
468 cnat_translation_purge (void)
469 {
470   /* purge all the translations */
471   index_t tri, *trp, *trs = NULL;
472
473   pool_foreach_index (tri, cnat_translation_pool)
474    {
475     vec_add1(trs, tri);
476   }
477
478   vec_foreach (trp, trs) cnat_translation_delete (*trp);
479
480   ASSERT (0 == pool_elts (cnat_translation_pool));
481
482   vec_free (trs);
483
484   return (0);
485 }
486
487 VLIB_CLI_COMMAND (cnat_translation_show_cmd_node, static) = {
488   .path = "show cnat translation",
489   .function = cnat_translation_show,
490   .short_help = "show cnat translation <VIP>",
491   .is_mp_safe = 1,
492 };
493
494 static fib_node_t *
495 cnat_translation_get_node (fib_node_index_t index)
496 {
497   cnat_translation_t *ct = cnat_translation_get (index);
498   return (&(ct->ct_node));
499 }
500
501 static cnat_translation_t *
502 cnat_translation_get_from_node (fib_node_t * node)
503 {
504   return ((cnat_translation_t *) (((char *) node) -
505                                   STRUCT_OFFSET_OF (cnat_translation_t,
506                                                     ct_node)));
507 }
508
509 static void
510 cnat_translation_last_lock_gone (fib_node_t * node)
511 {
512  /**/}
513
514 /*
515  * A back walk has reached this ABF policy
516  */
517 static fib_node_back_walk_rc_t
518 cnat_translation_back_walk_notify (fib_node_t * node,
519                                    fib_node_back_walk_ctx_t * ctx)
520 {
521   /*
522    * re-stack the fmask on the n-eos of the via
523    */
524   cnat_translation_t *ct = cnat_translation_get_from_node (node);
525
526   /* If we have more than FIB_PATH_LIST_POPULAR paths
527    * we might get called during path tracking
528    * (cnat_tracker_track) */
529   if (!(ct->flags & CNAT_TR_FLAG_STACKED))
530     return (FIB_NODE_BACK_WALK_CONTINUE);
531
532   cnat_translation_stack (ct);
533
534   return (FIB_NODE_BACK_WALK_CONTINUE);
535 }
536
537 /*
538  * The translation's graph node virtual function table
539  */
540 static const fib_node_vft_t cnat_translation_vft = {
541   .fnv_get = cnat_translation_get_node,
542   .fnv_last_lock = cnat_translation_last_lock_gone,
543   .fnv_back_walk = cnat_translation_back_walk_notify,
544 };
545
546 static clib_error_t *
547 cnat_translation_cli_add_del (vlib_main_t * vm,
548                               unformat_input_t * input,
549                               vlib_cli_command_t * cmd)
550 {
551   u32 del_index = INDEX_INVALID;
552   ip_protocol_t proto = IP_PROTOCOL_TCP;
553   cnat_endpoint_t vip;
554   u8 flags = CNAT_FLAG_EXCLUSIVE;
555   cnat_endpoint_tuple_t tmp, *paths = NULL, *path;
556   unformat_input_t _line_input, *line_input = &_line_input;
557   clib_error_t *e = 0;
558   cnat_lb_type_t lb_type;
559
560   /* Get a line of input. */
561   if (!unformat_user (input, unformat_line_input, line_input))
562     return 0;
563
564   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
565     {
566       if (unformat (line_input, "add"))
567         del_index = INDEX_INVALID;
568       else if (unformat (line_input, "del %d", &del_index))
569         ;
570       else
571         if (unformat (line_input, "proto %U", unformat_ip_protocol, &proto))
572         ;
573       else if (unformat (line_input, "vip %U", unformat_cnat_ep, &vip))
574         flags = CNAT_FLAG_EXCLUSIVE;
575       else if (unformat (line_input, "real %U", unformat_cnat_ep, &vip))
576         flags = 0;
577       else if (unformat (line_input, "to %U", unformat_cnat_ep_tuple, &tmp))
578         {
579           vec_add2 (paths, path, 1);
580           clib_memcpy (path, &tmp, sizeof (cnat_endpoint_tuple_t));
581         }
582       else if (unformat (line_input, "%U", unformat_cnat_lb_type, &lb_type))
583         ;
584       else
585         {
586           e = clib_error_return (0, "unknown input '%U'",
587                                  format_unformat_error, line_input);
588           goto done;
589         }
590     }
591
592   flow_hash_config_t fhc = 0;
593   if (INDEX_INVALID == del_index)
594     cnat_translation_update (&vip, proto, paths, flags, lb_type, fhc);
595   else
596     cnat_translation_delete (del_index);
597
598 done:
599   vec_free (paths);
600   unformat_free (line_input);
601   return (e);
602 }
603
604 VLIB_CLI_COMMAND (cnat_translation_cli_add_del_command, static) =
605 {
606   .path = "cnat translation",
607   .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]]",
608   .function = cnat_translation_cli_add_del,
609 };
610
611 static void
612 cnat_if_addr_add_del_translation_cb (addr_resolution_t * ar,
613                                      ip_address_t * address, u8 is_del)
614 {
615   cnat_translation_t *ct;
616   ct = cnat_translation_get (ar->cti);
617   if (!is_del && ct->ct_vip.ce_flags & CNAT_EP_FLAG_RESOLVED)
618     return;
619
620   cnat_remove_translation_from_db (ct->ct_cci, &ct->ct_vip, ct->ct_proto);
621
622   if (is_del)
623     {
624       ct->ct_vip.ce_flags &= ~CNAT_EP_FLAG_RESOLVED;
625       ct->ct_cci = INDEX_INVALID;
626       cnat_client_translation_deleted (ct->ct_cci);
627       /* Are there remaining addresses ? */
628       if (0 == cnat_resolve_addr (ar->sw_if_index, ar->af, address))
629         is_del = 0;
630     }
631
632   if (!is_del)
633     {
634       ct->ct_cci = cnat_client_add (address, ct->flags);
635       cnat_client_translation_added (ct->ct_cci);
636       ip_address_copy (&ct->ct_vip.ce_ip, address);
637       ct->ct_vip.ce_flags |= CNAT_EP_FLAG_RESOLVED;
638     }
639
640   cnat_add_translation_to_db (ct->ct_cci, &ct->ct_vip, ct->ct_proto,
641                               ct->index);
642 }
643
644 static void
645 cnat_if_addr_add_del_backend_cb (addr_resolution_t * ar,
646                                  ip_address_t * address, u8 is_del)
647 {
648   cnat_translation_t *ct;
649   cnat_ep_trk_t *trk;
650   cnat_endpoint_t *ep;
651
652   u8 direction = ar->opaque & 0xf;
653   u32 path_idx = ar->opaque >> 32;
654
655   ct = cnat_translation_get (ar->cti);
656
657   trk = &ct->ct_paths[path_idx];
658   ep = &trk->ct_ep[direction];
659
660   if (!is_del && ep->ce_flags & CNAT_EP_FLAG_RESOLVED)
661     return;
662
663   ASSERT (ep->ce_sw_if_index == ar->sw_if_index);
664
665   if (is_del)
666     {
667       ep->ce_flags &= ~CNAT_EP_FLAG_RESOLVED;
668       /* Are there remaining addresses ? */
669       if (0 == cnat_resolve_addr (ar->sw_if_index, ar->af, address))
670         is_del = 0;
671     }
672
673   if (!is_del)
674     {
675       ip_address_copy (&ep->ce_ip, address);
676       ep->ce_flags |= CNAT_EP_FLAG_RESOLVED;
677     }
678
679   ct->flags &= ~CNAT_TR_FLAG_STACKED;
680   cnat_tracker_track (ar->cti, trk);
681
682   cnat_translation_stack (ct);
683   ct->flags |= CNAT_TR_FLAG_STACKED;
684 }
685
686 static void
687 cnat_if_addr_add_del_callback (u32 sw_if_index, ip_address_t * address,
688                                u8 is_del)
689 {
690   addr_resolution_t *ar;
691   pool_foreach (ar, tr_resolutions)
692     {
693       if (ar->sw_if_index != sw_if_index)
694         continue;
695       if (ar->af != ip_addr_version (address))
696         continue;
697       cnat_if_addr_add_cbs[ar->type](ar, address, is_del);
698     }
699 }
700
701 static void
702 cnat_ip6_if_addr_add_del_callback (struct ip6_main_t *im,
703                                    uword opaque, u32 sw_if_index,
704                                    ip6_address_t * address,
705                                    u32 address_length, u32 if_address_index,
706                                    u32 is_del)
707 {
708   ip_address_t addr;
709   ip_address_set (&addr, address, AF_IP6);
710   cnat_if_addr_add_del_callback (sw_if_index, &addr, is_del);
711 }
712
713 static void
714 cnat_ip4_if_addr_add_del_callback (struct ip4_main_t *im,
715                                    uword opaque, u32 sw_if_index,
716                                    ip4_address_t * address,
717                                    u32 address_length, u32 if_address_index,
718                                    u32 is_del)
719 {
720   ip_address_t addr;
721   ip_address_set (&addr, address, AF_IP4);
722   cnat_if_addr_add_del_callback (sw_if_index, &addr, is_del);
723 }
724
725 void
726 cnat_translation_register_addr_add_cb (cnat_addr_resol_type_t typ,
727                                        cnat_if_addr_add_cb_t fn)
728 {
729   vec_validate (cnat_if_addr_add_cbs, CNAT_ADDR_N_RESOLUTIONS);
730   cnat_if_addr_add_cbs[typ] = fn;
731 }
732
733 static clib_error_t *
734 cnat_translation_init (vlib_main_t * vm)
735 {
736   ip4_main_t *i4m = &ip4_main;
737   ip6_main_t *i6m = &ip6_main;
738   cnat_main_t *cm = &cnat_main;
739   cnat_translation_fib_node_type =
740     fib_node_register_new_type ("cnat-translation", &cnat_translation_vft);
741
742   clib_bihash_init_8_8 (&cnat_translation_db, "CNat translation DB",
743                         cm->translation_hash_buckets,
744                         cm->translation_hash_memory);
745
746   ip4_add_del_interface_address_callback_t cb4 = { 0 };
747   cb4.function = cnat_ip4_if_addr_add_del_callback;
748   vec_add1 (i4m->add_del_interface_address_callbacks, cb4);
749
750   ip6_add_del_interface_address_callback_t cb6 = { 0 };
751   cb6.function = cnat_ip6_if_addr_add_del_callback;
752   vec_add1 (i6m->add_del_interface_address_callbacks, cb6);
753
754   cnat_translation_register_addr_add_cb (CNAT_RESOLV_ADDR_BACKEND,
755                                          cnat_if_addr_add_del_backend_cb);
756   cnat_translation_register_addr_add_cb (CNAT_RESOLV_ADDR_TRANSLATION,
757                                          cnat_if_addr_add_del_translation_cb);
758
759   return (NULL);
760 }
761
762 VLIB_INIT_FUNCTION (cnat_translation_init);
763
764 /*
765  * fd.io coding-style-patch-verification: ON
766  *
767  * Local Variables:
768  * eval: (c-set-style "gnu")
769  * End:
770  */