cnat: fixes & prepare maglev
[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 /**
117  * Add a translation to the bihash
118  *
119  * @param cci the ID of the parent client (invalid if vip not resolved)
120  * @param vip the translation endpoint
121  * @param proto the translation proto
122  * @param cti the translation index to be used as value
123  */
124 static void
125 cnat_add_translation_to_db (index_t cci, cnat_endpoint_t * vip,
126                             ip_protocol_t proto, index_t cti)
127 {
128   clib_bihash_kv_8_8_t bkey;
129   u64 key;
130   if (INDEX_INVALID == cci)
131     {
132       key = proto << 8 | 0x80 | vip->ce_ip.version;
133       key = key << 16 | vip->ce_port;
134       key = key << 32 | (u32) vip->ce_sw_if_index;
135     }
136   else
137     {
138       key = proto << 8;
139       key = key << 16 | vip->ce_port;
140       key = key << 32 | (u32) cci;
141     }
142
143   bkey.key = key;
144   bkey.value = cti;
145
146   clib_bihash_add_del_8_8 (&cnat_translation_db, &bkey, 1);
147 }
148
149 /**
150  * Remove a translation from the bihash
151  *
152  * @param cci the ID of the parent client
153  * @param vip the translation endpoint
154  * @param proto the translation proto
155  */
156 static void
157 cnat_remove_translation_from_db (index_t cci, cnat_endpoint_t * vip,
158                                  ip_protocol_t proto)
159 {
160   clib_bihash_kv_8_8_t bkey;
161   u64 key;
162   if (INDEX_INVALID == cci)
163     {
164       key = proto << 8 | 0x80 | vip->ce_ip.version;
165       key = key << 16 | vip->ce_port;
166       key = key << 32 | (u32) vip->ce_sw_if_index;
167     }
168   else
169     {
170       key = proto << 8;
171       key = key << 16 | vip->ce_port;
172       key = key << 32 | (u32) cci;
173     }
174
175   bkey.key = key;
176
177   clib_bihash_add_del_8_8 (&cnat_translation_db, &bkey, 0);
178 }
179
180 static void
181 cnat_translation_stack (cnat_translation_t * ct)
182 {
183   fib_protocol_t fproto;
184   cnat_ep_trk_t *trk;
185   dpo_proto_t dproto;
186   u32 ep_idx = 0;
187   index_t lbi;
188
189   fproto = ip_address_family_to_fib_proto (ct->ct_vip.ce_ip.version);
190   dproto = fib_proto_to_dpo (fproto);
191
192   vec_reset_length (ct->ct_active_paths);
193
194   vec_foreach (trk, ct->ct_paths)
195     if (trk->ct_flags & CNAT_TRK_ACTIVE)
196       vec_add1 (ct->ct_active_paths, *trk);
197
198   lbi = load_balance_create (vec_len (ct->ct_active_paths),
199                              fib_proto_to_dpo (fproto), IP_FLOW_HASH_DEFAULT);
200
201   ep_idx = 0;
202   vec_foreach (trk, ct->ct_active_paths)
203     load_balance_set_bucket (lbi, ep_idx++, &trk->ct_dpo);
204
205   dpo_set (&ct->ct_lb, DPO_LOAD_BALANCE, dproto, lbi);
206   dpo_stack (cnat_client_dpo, dproto, &ct->ct_lb, &ct->ct_lb);
207   ct->flags |= CNAT_TRANSLATION_STACKED;
208 }
209
210 int
211 cnat_translation_delete (u32 id)
212 {
213   cnat_translation_t *ct;
214   cnat_ep_trk_t *trk;
215
216   if (pool_is_free_index (cnat_translation_pool, id))
217     return (VNET_API_ERROR_NO_SUCH_ENTRY);
218
219   ct = pool_elt_at_index (cnat_translation_pool, id);
220
221   dpo_reset (&ct->ct_lb);
222
223   vec_foreach (trk, ct->ct_active_paths)
224     cnat_tracker_release (trk);
225
226   cnat_remove_translation_from_db (ct->ct_cci, &ct->ct_vip, ct->ct_proto);
227   cnat_client_translation_deleted (ct->ct_cci);
228   cnat_translation_unwatch_addr (id, CNAT_RESOLV_ADDR_ANY);
229   pool_put (cnat_translation_pool, ct);
230
231   return (0);
232 }
233
234 u32
235 cnat_translation_update (cnat_endpoint_t * vip,
236                          ip_protocol_t proto,
237                          cnat_endpoint_tuple_t * paths, u8 flags)
238 {
239   cnat_endpoint_tuple_t *path;
240   const cnat_client_t *cc;
241   cnat_translation_t *ct;
242   cnat_ep_trk_t *trk;
243   index_t cci;
244
245   cnat_lazy_init ();
246   if (cnat_resolve_ep (vip))
247     {
248       /* vip only contains a sw_if_index for now */
249       ct = cnat_find_translation (vip->ce_sw_if_index, vip->ce_port, proto);
250       cci = INDEX_INVALID;
251     }
252   else
253     {
254       /* do we know of this ep's vip */
255       cci = cnat_client_add (&vip->ce_ip, flags);
256       cc = cnat_client_get (cci);
257
258       ct = cnat_find_translation (cc->parent_cci, vip->ce_port, proto);
259     }
260
261   if (NULL == ct)
262     {
263       pool_get_zero (cnat_translation_pool, ct);
264
265       clib_memcpy (&ct->ct_vip, vip, sizeof (*vip));
266       ct->ct_proto = proto;
267       ct->ct_cci = cci;
268       ct->index = ct - cnat_translation_pool;
269
270       cnat_add_translation_to_db (cci, vip, proto, ct->index);
271       cnat_client_translation_added (cci);
272
273       vlib_validate_combined_counter (&cnat_translation_counters, ct->index);
274       vlib_zero_combined_counter (&cnat_translation_counters, ct->index);
275     }
276   ct->flags = flags;
277
278   cnat_translation_unwatch_addr (ct->index, CNAT_RESOLV_ADDR_ANY);
279   cnat_translation_watch_addr (ct->index, 0, vip,
280                                CNAT_RESOLV_ADDR_TRANSLATION);
281
282   vec_foreach (trk, ct->ct_paths)
283   {
284     cnat_tracker_release (trk);
285   }
286
287   vec_reset_length (ct->ct_paths);
288   ct->flags &= ~CNAT_TRANSLATION_STACKED;
289
290   u64 path_idx = 0;
291   vec_foreach (path, paths)
292   {
293     cnat_resolve_ep_tuple (path);
294     cnat_translation_watch_addr (ct->index,
295                                  path_idx << 32 | VLIB_RX, &path->src_ep,
296                                  CNAT_RESOLV_ADDR_BACKEND);
297     cnat_translation_watch_addr (ct->index,
298                                  path_idx << 32 | VLIB_TX, &path->dst_ep,
299                                  CNAT_RESOLV_ADDR_BACKEND);
300     path_idx++;
301
302     vec_add2 (ct->ct_paths, trk, 1);
303
304     clib_memcpy (&trk->ct_ep[VLIB_TX], &path->dst_ep,
305                  sizeof (trk->ct_ep[VLIB_TX]));
306     clib_memcpy (&trk->ct_ep[VLIB_RX], &path->src_ep,
307                  sizeof (trk->ct_ep[VLIB_RX]));
308
309     cnat_tracker_track (ct->index, trk);
310   }
311
312   cnat_translation_stack (ct);
313
314   return (ct->index);
315 }
316
317 void
318 cnat_translation_walk (cnat_translation_walk_cb_t cb, void *ctx)
319 {
320   u32 api;
321
322   pool_foreach_index (api, cnat_translation_pool)
323    {
324     if (!cb(api, ctx))
325       break;
326   }
327 }
328
329 static u8 *
330 format_cnat_ep_trk (u8 * s, va_list * args)
331 {
332   cnat_ep_trk_t *ck = va_arg (*args, cnat_ep_trk_t *);
333   u32 indent = va_arg (*args, u32);
334
335   s = format (s, "%U->%U", format_cnat_endpoint, &ck->ct_ep[VLIB_RX],
336               format_cnat_endpoint, &ck->ct_ep[VLIB_TX]);
337   s = format (s, "\n%Ufib-entry:%d", format_white_space, indent, ck->ct_fei);
338   s = format (s, "\n%U%U",
339               format_white_space, indent, format_dpo_id, &ck->ct_dpo, 6);
340
341   return (s);
342 }
343
344 u8 *
345 format_cnat_translation (u8 * s, va_list * args)
346 {
347   cnat_translation_t *ct = va_arg (*args, cnat_translation_t *);
348   cnat_ep_trk_t *ck;
349
350   s = format (s, "[%d] ", ct->index);
351   s = format (s, "%U %U", format_cnat_endpoint, &ct->ct_vip,
352               format_ip_protocol, ct->ct_proto);
353
354   vec_foreach (ck, ct->ct_paths)
355     s = format (s, "\n%U", format_cnat_ep_trk, ck, 2);
356
357   /* If printing a trace, the LB object might be deleted */
358   if (!pool_is_free_index (load_balance_pool, ct->ct_lb.dpoi_index))
359     {
360       s = format (s, "\n via:");
361       s = format (s, "\n%U%U",
362                   format_white_space, 2, format_dpo_id, &ct->ct_lb, 2);
363     }
364
365   return (s);
366 }
367
368 static clib_error_t *
369 cnat_translation_show (vlib_main_t * vm,
370                        unformat_input_t * input, vlib_cli_command_t * cmd)
371 {
372   index_t cti;
373   cnat_translation_t *ct;
374
375   cti = INDEX_INVALID;
376
377   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
378     {
379       if (unformat (input, "%d", &cti))
380         ;
381       else
382         return (clib_error_return (0, "unknown input '%U'",
383                                    format_unformat_error, input));
384     }
385
386   if (INDEX_INVALID == cti)
387     {
388       pool_foreach_index (cti, cnat_translation_pool)
389        {
390         ct = pool_elt_at_index (cnat_translation_pool, cti);
391         vlib_cli_output(vm, "%U", format_cnat_translation, ct);
392       }
393     }
394   else
395     {
396       vlib_cli_output (vm, "Invalid policy ID:%d", cti);
397     }
398
399   return (NULL);
400 }
401
402 int
403 cnat_translation_purge (void)
404 {
405   /* purge all the translations */
406   index_t tri, *trp, *trs = NULL;
407
408   pool_foreach_index (tri, cnat_translation_pool)
409    {
410     vec_add1(trs, tri);
411   }
412
413   vec_foreach (trp, trs) cnat_translation_delete (*trp);
414
415   ASSERT (0 == pool_elts (cnat_translation_pool));
416
417   vec_free (trs);
418
419   return (0);
420 }
421
422 VLIB_CLI_COMMAND (cnat_translation_show_cmd_node, static) = {
423   .path = "show cnat translation",
424   .function = cnat_translation_show,
425   .short_help = "show cnat translation <VIP>",
426   .is_mp_safe = 1,
427 };
428
429 static fib_node_t *
430 cnat_translation_get_node (fib_node_index_t index)
431 {
432   cnat_translation_t *ct = cnat_translation_get (index);
433   return (&(ct->ct_node));
434 }
435
436 static cnat_translation_t *
437 cnat_translation_get_from_node (fib_node_t * node)
438 {
439   return ((cnat_translation_t *) (((char *) node) -
440                                   STRUCT_OFFSET_OF (cnat_translation_t,
441                                                     ct_node)));
442 }
443
444 static void
445 cnat_translation_last_lock_gone (fib_node_t * node)
446 {
447  /**/}
448
449 /*
450  * A back walk has reached this ABF policy
451  */
452 static fib_node_back_walk_rc_t
453 cnat_translation_back_walk_notify (fib_node_t * node,
454                                    fib_node_back_walk_ctx_t * ctx)
455 {
456   /*
457    * re-stack the fmask on the n-eos of the via
458    */
459   cnat_translation_t *ct = cnat_translation_get_from_node (node);
460
461   /* If we have more than FIB_PATH_LIST_POPULAR paths
462    * we might get called during path tracking
463    * (cnat_tracker_track) */
464   if (!(ct->flags & CNAT_TRANSLATION_STACKED))
465     return (FIB_NODE_BACK_WALK_CONTINUE);
466
467   cnat_translation_stack (ct);
468
469   return (FIB_NODE_BACK_WALK_CONTINUE);
470 }
471
472 /*
473  * The translation's graph node virtual function table
474  */
475 static const fib_node_vft_t cnat_translation_vft = {
476   .fnv_get = cnat_translation_get_node,
477   .fnv_last_lock = cnat_translation_last_lock_gone,
478   .fnv_back_walk = cnat_translation_back_walk_notify,
479 };
480
481 static clib_error_t *
482 cnat_translation_cli_add_del (vlib_main_t * vm,
483                               unformat_input_t * input,
484                               vlib_cli_command_t * cmd)
485 {
486   u32 del_index = INDEX_INVALID;
487   ip_protocol_t proto = IP_PROTOCOL_TCP;
488   cnat_endpoint_t vip;
489   u8 flags = CNAT_FLAG_EXCLUSIVE;
490   cnat_endpoint_tuple_t tmp, *paths = NULL, *path;
491   unformat_input_t _line_input, *line_input = &_line_input;
492   clib_error_t *e = 0;
493
494   /* Get a line of input. */
495   if (!unformat_user (input, unformat_line_input, line_input))
496     return 0;
497
498   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
499     {
500       if (unformat (line_input, "add"))
501         del_index = INDEX_INVALID;
502       else if (unformat (line_input, "del %d", &del_index))
503         ;
504       else
505         if (unformat (line_input, "proto %U", unformat_ip_protocol, &proto))
506         ;
507       else if (unformat (line_input, "vip %U", unformat_cnat_ep, &vip))
508         flags = CNAT_FLAG_EXCLUSIVE;
509       else if (unformat (line_input, "real %U", unformat_cnat_ep, &vip))
510         flags = 0;
511       else if (unformat (line_input, "to %U", unformat_cnat_ep_tuple, &tmp))
512         {
513           vec_add2 (paths, path, 1);
514           clib_memcpy (path, &tmp, sizeof (cnat_endpoint_tuple_t));
515         }
516       else
517         {
518           e = clib_error_return (0, "unknown input '%U'",
519                                  format_unformat_error, line_input);
520           goto done;
521         }
522     }
523
524   if (INDEX_INVALID == del_index)
525     cnat_translation_update (&vip, proto, paths, flags);
526   else
527     cnat_translation_delete (del_index);
528
529 done:
530   vec_free (paths);
531   unformat_free (line_input);
532   return (e);
533 }
534
535 VLIB_CLI_COMMAND (cnat_translation_cli_add_del_command, static) =
536 {
537   .path = "cnat translation",
538   .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]]",
539   .function = cnat_translation_cli_add_del,
540 };
541
542 static void
543 cnat_if_addr_add_del_translation_cb (addr_resolution_t * ar,
544                                      ip_address_t * address, u8 is_del)
545 {
546   cnat_translation_t *ct;
547   ct = cnat_translation_get (ar->cti);
548   if (!is_del && ct->ct_vip.ce_flags & CNAT_EP_FLAG_RESOLVED)
549     return;
550
551   cnat_remove_translation_from_db (ct->ct_cci, &ct->ct_vip, ct->ct_proto);
552
553   if (is_del)
554     {
555       ct->ct_vip.ce_flags &= ~CNAT_EP_FLAG_RESOLVED;
556       ct->ct_cci = INDEX_INVALID;
557       cnat_client_translation_deleted (ct->ct_cci);
558       /* Are there remaining addresses ? */
559       if (0 == cnat_resolve_addr (ar->sw_if_index, ar->af, address))
560         is_del = 0;
561     }
562
563   if (!is_del)
564     {
565       ct->ct_cci = cnat_client_add (address, ct->flags);
566       cnat_client_translation_added (ct->ct_cci);
567       ip_address_copy (&ct->ct_vip.ce_ip, address);
568       ct->ct_vip.ce_flags |= CNAT_EP_FLAG_RESOLVED;
569     }
570
571   cnat_add_translation_to_db (ct->ct_cci, &ct->ct_vip, ct->ct_proto,
572                               ct->index);
573 }
574
575 static void
576 cnat_if_addr_add_del_backend_cb (addr_resolution_t * ar,
577                                  ip_address_t * address, u8 is_del)
578 {
579   cnat_translation_t *ct;
580   cnat_ep_trk_t *trk;
581   cnat_endpoint_t *ep;
582
583   u8 direction = ar->opaque & 0xf;
584   u32 path_idx = ar->opaque >> 32;
585
586   ct = cnat_translation_get (ar->cti);
587
588   trk = &ct->ct_paths[path_idx];
589   ep = &trk->ct_ep[direction];
590
591   if (!is_del && ep->ce_flags & CNAT_EP_FLAG_RESOLVED)
592     return;
593
594   ASSERT (ep->ce_sw_if_index == ar->sw_if_index);
595
596   if (is_del)
597     {
598       ep->ce_flags &= ~CNAT_EP_FLAG_RESOLVED;
599       /* Are there remaining addresses ? */
600       if (0 == cnat_resolve_addr (ar->sw_if_index, ar->af, address))
601         is_del = 0;
602     }
603
604   if (!is_del)
605     {
606       ip_address_copy (&ep->ce_ip, address);
607       ep->ce_flags |= CNAT_EP_FLAG_RESOLVED;
608     }
609
610   ct->flags &= ~CNAT_TRANSLATION_STACKED;
611   cnat_tracker_track (ar->cti, trk);
612
613   cnat_translation_stack (ct);
614   ct->flags |= CNAT_TRANSLATION_STACKED;
615 }
616
617 static void
618 cnat_if_addr_add_del_snat_cb (addr_resolution_t * ar, ip_address_t * address,
619                               u8 is_del)
620 {
621   cnat_endpoint_t *ep;
622   ep = AF_IP4 == ar->af ? &cnat_main.snat_ip4 : &cnat_main.snat_ip6;
623
624   if (!is_del && ep->ce_flags & CNAT_EP_FLAG_RESOLVED)
625     return;
626
627   if (is_del)
628     {
629       ep->ce_flags &= ~CNAT_EP_FLAG_RESOLVED;
630       /* Are there remaining addresses ? */
631       if (0 == cnat_resolve_addr (ar->sw_if_index, ar->af, address))
632         is_del = 0;
633     }
634
635   if (!is_del)
636     {
637       ip_address_copy (&ep->ce_ip, address);
638       ep->ce_flags |= CNAT_EP_FLAG_RESOLVED;
639     }
640
641 }
642
643 static void
644 cnat_if_addr_add_del_callback (u32 sw_if_index, ip_address_t * address,
645                                u8 is_del)
646 {
647   addr_resolution_t *ar;
648   pool_foreach (ar, tr_resolutions)
649     {
650       if (ar->sw_if_index != sw_if_index)
651         continue;
652       if (ar->af != ip_addr_version (address))
653         continue;
654       cnat_if_addr_add_cbs[ar->type](ar, address, is_del);
655     }
656 }
657
658 static void
659 cnat_ip6_if_addr_add_del_callback (struct ip6_main_t *im,
660                                    uword opaque, u32 sw_if_index,
661                                    ip6_address_t * address,
662                                    u32 address_length, u32 if_address_index,
663                                    u32 is_del)
664 {
665   ip_address_t addr;
666   ip_address_set (&addr, address, AF_IP6);
667   cnat_if_addr_add_del_callback (sw_if_index, &addr, is_del);
668 }
669
670 static void
671 cnat_ip4_if_addr_add_del_callback (struct ip4_main_t *im,
672                                    uword opaque, u32 sw_if_index,
673                                    ip4_address_t * address,
674                                    u32 address_length, u32 if_address_index,
675                                    u32 is_del)
676 {
677   ip_address_t addr;
678   ip_address_set (&addr, address, AF_IP4);
679   cnat_if_addr_add_del_callback (sw_if_index, &addr, is_del);
680 }
681
682 static clib_error_t *
683 cnat_translation_init (vlib_main_t * vm)
684 {
685   ip4_main_t *i4m = &ip4_main;
686   ip6_main_t *i6m = &ip6_main;
687   cnat_main_t *cm = &cnat_main;
688   cnat_translation_fib_node_type =
689     fib_node_register_new_type (&cnat_translation_vft);
690
691   clib_bihash_init_8_8 (&cnat_translation_db, "CNat translation DB",
692                         cm->translation_hash_buckets,
693                         cm->translation_hash_memory);
694
695   ip4_add_del_interface_address_callback_t cb4 = { 0 };
696   cb4.function = cnat_ip4_if_addr_add_del_callback;
697   vec_add1 (i4m->add_del_interface_address_callbacks, cb4);
698
699   ip6_add_del_interface_address_callback_t cb6 = { 0 };
700   cb6.function = cnat_ip6_if_addr_add_del_callback;
701   vec_add1 (i6m->add_del_interface_address_callbacks, cb6);
702
703   vec_validate (cnat_if_addr_add_cbs, CNAT_ADDR_N_RESOLUTIONS);
704   cnat_if_addr_add_cbs[CNAT_RESOLV_ADDR_BACKEND] =
705     cnat_if_addr_add_del_backend_cb;
706   cnat_if_addr_add_cbs[CNAT_RESOLV_ADDR_SNAT] = cnat_if_addr_add_del_snat_cb;
707   cnat_if_addr_add_cbs[CNAT_RESOLV_ADDR_TRANSLATION] =
708     cnat_if_addr_add_del_translation_cb;
709   return (NULL);
710 }
711
712 VLIB_INIT_FUNCTION (cnat_translation_init);
713
714 /*
715  * fd.io coding-style-patch-verification: ON
716  *
717  * Local Variables:
718  * eval: (c-set-style "gnu")
719  * End:
720  */