vlib: introduce vlib_get_main_by_index(), vlib_get_n_threads()
[vpp.git] / src / plugins / nat / nat64 / nat64.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 <vppinfra/crc32.h>
17 #include <vnet/fib/ip4_fib.h>
18
19 #include <vnet/ip/reass/ip4_sv_reass.h>
20 #include <vnet/ip/reass/ip6_sv_reass.h>
21 #include <vnet/plugin/plugin.h>
22 #include <vpp/app/version.h>
23
24 #include <nat/lib/ipfix_logging.h>
25 #include <nat/nat64/nat64.h>
26
27 nat64_main_t nat64_main;
28
29 /* *INDENT-OFF* */
30 /* Hook up input features */
31 VNET_FEATURE_INIT (nat64_in2out, static) = {
32   .arc_name = "ip6-unicast",
33   .node_name = "nat64-in2out",
34   .runs_before = VNET_FEATURES ("ip6-lookup"),
35   .runs_after = VNET_FEATURES ("ip6-sv-reassembly-feature"),
36 };
37 VNET_FEATURE_INIT (nat64_out2in, static) = {
38   .arc_name = "ip4-unicast",
39   .node_name = "nat64-out2in",
40   .runs_before = VNET_FEATURES ("ip4-lookup"),
41   .runs_after = VNET_FEATURES ("ip4-sv-reassembly-feature"),
42 };
43 VNET_FEATURE_INIT (nat64_in2out_handoff, static) = {
44   .arc_name = "ip6-unicast",
45   .node_name = "nat64-in2out-handoff",
46   .runs_before = VNET_FEATURES ("ip6-lookup"),
47   .runs_after = VNET_FEATURES ("ip6-sv-reassembly-feature"),
48 };
49 VNET_FEATURE_INIT (nat64_out2in_handoff, static) = {
50   .arc_name = "ip4-unicast",
51   .node_name = "nat64-out2in-handoff",
52   .runs_before = VNET_FEATURES ("ip4-lookup"),
53   .runs_after = VNET_FEATURES ("ip4-sv-reassembly-feature"),
54 };
55 VLIB_PLUGIN_REGISTER () = {
56     .version = VPP_BUILD_VER,
57     .description = "NAT64",
58 };
59 static u8 well_known_prefix[] = {
60   0x00, 0x64, 0xff, 0x9b,
61   0x00, 0x00, 0x00, 0x00,
62   0x00, 0x00, 0x00, 0x00,
63   0x00, 0x00, 0x00, 0x00
64 };
65 /* *INDENT-ON* */
66
67 #define nat_elog_str(_str)                      \
68 do                                              \
69   {                                             \
70     ELOG_TYPE_DECLARE (e) =                     \
71       {                                         \
72         .format = "nat-msg " _str,              \
73         .format_args = "",                      \
74       };                                        \
75     ELOG_DATA (&vlib_global_main.elog_main, e); \
76   } while (0);
77
78 static void
79 nat64_ip4_add_del_interface_address_cb (ip4_main_t * im, uword opaque,
80                                         u32 sw_if_index,
81                                         ip4_address_t * address,
82                                         u32 address_length,
83                                         u32 if_address_index, u32 is_delete)
84 {
85   nat64_main_t *nm = &nat64_main;
86   int i, j;
87
88   if (plugin_enabled () == 0)
89     return;
90
91   for (i = 0; i < vec_len (nm->auto_add_sw_if_indices); i++)
92     {
93       if (sw_if_index == nm->auto_add_sw_if_indices[i])
94         {
95           if (!is_delete)
96             {
97               /* Don't trip over lease renewal, static config */
98               for (j = 0; j < vec_len (nm->addr_pool); j++)
99                 if (nm->addr_pool[j].addr.as_u32 == address->as_u32)
100                   return;
101
102               (void) nat64_add_del_pool_addr (vlib_get_thread_index (),
103                                               address, ~0, 1);
104               return;
105             }
106           else
107             {
108               (void) nat64_add_del_pool_addr (vlib_get_thread_index (),
109                                               address, ~0, 0);
110               return;
111             }
112         }
113     }
114 }
115
116 u32
117 nat64_get_worker_in2out (ip6_address_t * addr)
118 {
119   nat64_main_t *nm = &nat64_main;
120   u32 next_worker_index = nm->first_worker_index;
121   u32 hash;
122
123 #ifdef clib_crc32c_uses_intrinsics
124   hash = clib_crc32c ((u8 *) addr->as_u32, 16);
125 #else
126   u64 tmp = addr->as_u64[0] ^ addr->as_u64[1];
127   hash = clib_xxhash (tmp);
128 #endif
129
130   if (PREDICT_TRUE (is_pow2 (_vec_len (nm->workers))))
131     next_worker_index += nm->workers[hash & (_vec_len (nm->workers) - 1)];
132   else
133     next_worker_index += nm->workers[hash % _vec_len (nm->workers)];
134
135   return next_worker_index;
136 }
137
138 u32
139 nat64_get_worker_out2in (vlib_buffer_t * b, ip4_header_t * ip)
140 {
141   nat64_main_t *nm = &nat64_main;
142   udp_header_t *udp;
143   u16 port;
144   u32 proto;
145
146   proto = ip_proto_to_nat_proto (ip->protocol);
147   udp = ip4_next_header (ip);
148   port = udp->dst_port;
149
150   /* unknown protocol */
151   if (PREDICT_FALSE (proto == NAT_PROTOCOL_OTHER))
152     {
153       nat64_db_t *db;
154       ip46_address_t daddr;
155       nat64_db_bib_entry_t *bibe;
156
157       clib_memset (&daddr, 0, sizeof (daddr));
158       daddr.ip4.as_u32 = ip->dst_address.as_u32;
159
160       /* *INDENT-OFF* */
161       vec_foreach (db, nm->db)
162         {
163           bibe = nat64_db_bib_entry_find (db, &daddr, 0, ip->protocol, 0, 0);
164           if (bibe)
165             return (u32) (db - nm->db);
166         }
167       /* *INDENT-ON* */
168       return vlib_get_thread_index ();
169     }
170
171   /* ICMP */
172   if (PREDICT_FALSE (ip->protocol == IP_PROTOCOL_ICMP))
173     {
174       icmp46_header_t *icmp = (icmp46_header_t *) udp;
175       icmp_echo_header_t *echo = (icmp_echo_header_t *) (icmp + 1);
176       if (!icmp_type_is_error_message
177           (vnet_buffer (b)->ip.reass.icmp_type_or_tcp_flags))
178         port = vnet_buffer (b)->ip.reass.l4_src_port;
179       else
180         {
181           /* if error message, then it's not fragmented and we can access it */
182           ip4_header_t *inner_ip = (ip4_header_t *) (echo + 1);
183           proto = ip_proto_to_nat_proto (inner_ip->protocol);
184           void *l4_header = ip4_next_header (inner_ip);
185           switch (proto)
186             {
187             case NAT_PROTOCOL_ICMP:
188               icmp = (icmp46_header_t *) l4_header;
189               echo = (icmp_echo_header_t *) (icmp + 1);
190               port = echo->identifier;
191               break;
192             case NAT_PROTOCOL_UDP:
193             case NAT_PROTOCOL_TCP:
194               port = ((tcp_udp_header_t *) l4_header)->src_port;
195               break;
196             default:
197               return vlib_get_thread_index ();
198             }
199         }
200     }
201
202   /* worker by outside port  (TCP/UDP) */
203   port = clib_net_to_host_u16 (port);
204   if (port > 1024)
205     return nm->first_worker_index + ((port - 1024) / nm->port_per_thread);
206
207   return vlib_get_thread_index ();
208 }
209
210 clib_error_t *
211 nat64_init (vlib_main_t * vm)
212 {
213   nat64_main_t *nm = &nat64_main;
214   vlib_thread_main_t *tm = vlib_get_thread_main ();
215   ip4_add_del_interface_address_callback_t cb4;
216   vlib_node_t *node;
217
218   clib_memset (nm, 0, sizeof (*nm));
219
220   nm->ip4_main = &ip4_main;
221   nm->log_class = vlib_log_register_class ("nat64", 0);
222
223   nm->port_per_thread = 0xffff - 1024;
224
225   nm->fq_in2out_index = ~0;
226   nm->fq_out2in_index = ~0;
227
228   node = vlib_get_node_by_name (vm, (u8 *) "error-drop");
229   nm->error_node_index = node->index;
230   node = vlib_get_node_by_name (vm, (u8 *) "nat64-in2out");
231   nm->in2out_node_index = node->index;
232   node = vlib_get_node_by_name (vm, (u8 *) "nat64-in2out-slowpath");
233   nm->in2out_slowpath_node_index = node->index;
234   node = vlib_get_node_by_name (vm, (u8 *) "nat64-out2in");
235   nm->out2in_node_index = node->index;
236
237   node = vlib_get_node_by_name (vm, (u8 *) "nat64-expire-worker-walk");
238   nm->expire_worker_walk_node_index = node->index;
239
240   nm->fib_src_hi = fib_source_allocate ("nat64-hi",
241                                         FIB_SOURCE_PRIORITY_HI,
242                                         FIB_SOURCE_BH_SIMPLE);
243   nm->fib_src_low = fib_source_allocate ("nat64-low",
244                                          FIB_SOURCE_PRIORITY_LOW,
245                                          FIB_SOURCE_BH_SIMPLE);
246
247   // set protocol timeouts to defaults
248   nat64_reset_timeouts ();
249
250   /* Set up the interface address add/del callback */
251   cb4.function = nat64_ip4_add_del_interface_address_cb;
252   cb4.function_opaque = 0;
253   vec_add1 (nm->ip4_main->add_del_interface_address_callbacks, cb4);
254
255   /* Init counters */
256   nm->total_bibs.name = "total-bibs";
257   nm->total_bibs.stat_segment_name = "/nat64/total-bibs";
258   vlib_validate_simple_counter (&nm->total_bibs, 0);
259   vlib_zero_simple_counter (&nm->total_bibs, 0);
260   nm->total_sessions.name = "total-sessions";
261   nm->total_sessions.stat_segment_name = "/nat64/total-sessions";
262   vlib_validate_simple_counter (&nm->total_sessions, 0);
263   vlib_zero_simple_counter (&nm->total_sessions, 0);
264
265   uword *p = hash_get_mem (tm->thread_registrations_by_name, "workers");
266   if (p)
267     {
268       vlib_thread_registration_t *tr;
269       tr = (vlib_thread_registration_t *) p[0];
270       if (tr)
271         {
272           nm->num_workers = tr->count;
273           nm->first_worker_index = tr->first_index;
274         }
275     }
276
277   if (nm->num_workers > 1)
278     {
279       int i;
280       uword *bitmap = 0;
281
282       for (i = 0; i < nm->num_workers; i++)
283         bitmap = clib_bitmap_set (bitmap, i, 1);
284
285       /* *INDENT-OFF* */
286       clib_bitmap_foreach (i, bitmap)
287          {
288           vec_add1(nm->workers, i);
289         }
290       /* *INDENT-ON* */
291
292       clib_bitmap_free (bitmap);
293
294       nm->port_per_thread = (0xffff - 1024) / _vec_len (nm->workers);
295     }
296
297   /* Init IPFIX logging */
298   nat_ipfix_logging_init (vm);
299
300 #define _(x)                                                     \
301   nm->counters.in2out.x.name = #x;                               \
302   nm->counters.in2out.x.stat_segment_name = "/nat64/in2out/" #x; \
303   nm->counters.out2in.x.name = #x;                               \
304   nm->counters.out2in.x.stat_segment_name = "/nat64/out2in/" #x;
305   foreach_nat_counter;
306 #undef _
307   return nat64_api_hookup (vm);
308 }
309
310 VLIB_INIT_FUNCTION (nat64_init);
311
312 static void nat64_free_out_addr_and_port (struct nat64_db_s *db,
313                                           ip4_address_t * addr, u16 port,
314                                           u8 protocol);
315
316 int
317 nat64_init_hash (nat64_config_t c)
318 {
319   vlib_thread_main_t *tm = vlib_get_thread_main ();
320   nat64_main_t *nm = &nat64_main;
321   nat64_db_t *db;
322   int rv = 0;
323
324   vec_validate (nm->db, tm->n_vlib_mains - 1);
325
326   /* *INDENT-OFF* */
327   vec_foreach (db, nm->db)
328     {
329       if (nat64_db_init (db, c, nat64_free_out_addr_and_port))
330         {
331           nat64_log_err ("NAT64 DB init failed");
332           rv = 1;
333         }
334     }
335   /* *INDENT-ON* */
336
337   return rv;
338 }
339
340 int
341 nat64_free_hash ()
342 {
343   nat64_main_t *nm = &nat64_main;
344   nat64_db_t *db;
345   int rv = 0;
346
347   /* *INDENT-OFF* */
348   vec_foreach (db, nm->db)
349     {
350       if (nat64_db_free (db))
351         {
352           nat64_log_err ("NAT64 DB free failed");
353           rv = 1;
354         }
355     }
356   /* *INDENT-ON* */
357
358   vec_free (nm->db);
359
360   return rv;
361 }
362
363 int
364 nat64_add_del_pool_addr (u32 thread_index,
365                          ip4_address_t * addr, u32 vrf_id, u8 is_add)
366 {
367   nat64_main_t *nm = &nat64_main;
368   nat64_address_t *a = 0;
369   nat64_interface_t *interface;
370   int i;
371   nat64_db_t *db;
372   vlib_thread_main_t *tm = vlib_get_thread_main ();
373
374   /* Check if address already exists */
375   for (i = 0; i < vec_len (nm->addr_pool); i++)
376     {
377       if (nm->addr_pool[i].addr.as_u32 == addr->as_u32)
378         {
379           a = nm->addr_pool + i;
380           break;
381         }
382     }
383
384   if (is_add)
385     {
386       if (a)
387         return VNET_API_ERROR_VALUE_EXIST;
388
389       vec_add2 (nm->addr_pool, a, 1);
390       a->addr = *addr;
391       a->fib_index = ~0;
392       if (vrf_id != ~0)
393         a->fib_index =
394           fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP6, vrf_id,
395                                              nm->fib_src_hi);
396 #define _(N, id, n, s) \
397       clib_memset (a->busy_##n##_port_refcounts, 0, sizeof(a->busy_##n##_port_refcounts)); \
398       a->busy_##n##_ports = 0; \
399       vec_validate_init_empty (a->busy_##n##_ports_per_thread, tm->n_vlib_mains - 1, 0);
400       foreach_nat_protocol
401 #undef _
402     }
403   else
404     {
405       if (!a)
406         return VNET_API_ERROR_NO_SUCH_ENTRY;
407
408       if (a->fib_index != ~0)
409         fib_table_unlock (a->fib_index, FIB_PROTOCOL_IP6, nm->fib_src_hi);
410       /* Delete sessions using address */
411       /* *INDENT-OFF* */
412       vec_foreach (db, nm->db)
413         {
414           nat64_db_free_out_addr (thread_index, db, &a->addr);
415           vlib_set_simple_counter (&nm->total_bibs, db - nm->db, 0,
416                                    db->bib.bib_entries_num);
417           vlib_set_simple_counter (&nm->total_sessions, db - nm->db, 0,
418                                    db->st.st_entries_num);
419         }
420       /* *INDENT-ON* */
421       vec_del1 (nm->addr_pool, i);
422     }
423
424   /* Add/del external address to FIB */
425   /* *INDENT-OFF* */
426   pool_foreach (interface, nm->interfaces)
427    {
428     if (nat64_interface_is_inside(interface))
429       continue;
430
431     nat64_add_del_addr_to_fib (addr, 32, interface->sw_if_index, is_add);
432     break;
433   }
434   /* *INDENT-ON* */
435
436   return 0;
437 }
438
439 void
440 nat64_pool_addr_walk (nat64_pool_addr_walk_fn_t fn, void *ctx)
441 {
442   nat64_main_t *nm = &nat64_main;
443   nat64_address_t *a = 0;
444
445   /* *INDENT-OFF* */
446   vec_foreach (a, nm->addr_pool)
447     {
448       if (fn (a, ctx))
449         break;
450     };
451   /* *INDENT-ON* */
452 }
453
454 int
455 nat64_add_interface_address (u32 sw_if_index, int is_add)
456 {
457   nat64_main_t *nm = &nat64_main;
458   ip4_main_t *ip4_main = nm->ip4_main;
459   ip4_address_t *first_int_addr;
460   int i;
461
462   first_int_addr = ip4_interface_first_address (ip4_main, sw_if_index, 0);
463
464   for (i = 0; i < vec_len (nm->auto_add_sw_if_indices); i++)
465     {
466       if (nm->auto_add_sw_if_indices[i] == sw_if_index)
467         {
468           if (is_add)
469             return VNET_API_ERROR_VALUE_EXIST;
470           else
471             {
472               /* if have address remove it */
473               if (first_int_addr)
474                 (void) nat64_add_del_pool_addr (vlib_get_thread_index (),
475                                                 first_int_addr, ~0, 0);
476               vec_del1 (nm->auto_add_sw_if_indices, i);
477               return 0;
478             }
479         }
480     }
481
482   if (!is_add)
483     return VNET_API_ERROR_NO_SUCH_ENTRY;
484
485   /* add to the auto-address list */
486   vec_add1 (nm->auto_add_sw_if_indices, sw_if_index);
487
488   /* If the address is already bound - or static - add it now */
489   if (first_int_addr)
490     (void) nat64_add_del_pool_addr (vlib_get_thread_index (),
491                                     first_int_addr, ~0, 1);
492
493   return 0;
494 }
495
496 static void
497 nat64_validate_counters (nat64_main_t * nm, u32 sw_if_index)
498 {
499 #define _(x)                                                          \
500   vlib_validate_simple_counter (&nm->counters.in2out.x, sw_if_index); \
501   vlib_zero_simple_counter (&nm->counters.in2out.x, sw_if_index);     \
502   vlib_validate_simple_counter (&nm->counters.out2in.x, sw_if_index); \
503   vlib_zero_simple_counter (&nm->counters.out2in.x, sw_if_index);
504   foreach_nat_counter;
505 #undef _
506 }
507
508 void
509 nat64_add_del_addr_to_fib (ip4_address_t * addr, u8 p_len, u32 sw_if_index,
510                            int is_add)
511 {
512   nat64_main_t *nm = &nat64_main;
513   fib_prefix_t prefix = {
514     .fp_len = p_len,
515     .fp_proto = FIB_PROTOCOL_IP4,
516     .fp_addr = {
517                 .ip4.as_u32 = addr->as_u32,
518                 },
519   };
520   u32 fib_index = ip4_fib_table_get_index_for_sw_if_index (sw_if_index);
521
522   if (is_add)
523     fib_table_entry_update_one_path (fib_index,
524                                      &prefix,
525                                      nm->fib_src_low,
526                                      (FIB_ENTRY_FLAG_CONNECTED |
527                                       FIB_ENTRY_FLAG_LOCAL |
528                                       FIB_ENTRY_FLAG_EXCLUSIVE),
529                                      DPO_PROTO_IP4,
530                                      NULL,
531                                      sw_if_index,
532                                      ~0, 1, NULL, FIB_ROUTE_PATH_FLAG_NONE);
533   else
534     fib_table_entry_delete (fib_index, &prefix, nm->fib_src_low);
535 }
536
537 int
538 nat64_interface_add_del (u32 sw_if_index, u8 is_inside, u8 is_add)
539 {
540   vlib_main_t *vm = vlib_get_main ();
541   nat64_main_t *nm = &nat64_main;
542   nat64_interface_t *interface = 0, *i;
543   nat64_address_t *ap;
544   const char *feature_name, *arc_name;
545
546   // TODO: is enabled ? we can't signal if it is not
547
548   /* Check if interface already exists */
549   /* *INDENT-OFF* */
550   pool_foreach (i, nm->interfaces)
551    {
552     if (i->sw_if_index == sw_if_index)
553       {
554         interface = i;
555         break;
556       }
557   }
558   /* *INDENT-ON* */
559
560   if (is_add)
561     {
562       if (interface)
563         goto set_flags;
564
565       pool_get (nm->interfaces, interface);
566       interface->sw_if_index = sw_if_index;
567       interface->flags = 0;
568       nat64_validate_counters (nm, sw_if_index);
569     set_flags:
570       if (is_inside)
571         interface->flags |= NAT64_INTERFACE_FLAG_IS_INSIDE;
572       else
573         interface->flags |= NAT64_INTERFACE_FLAG_IS_OUTSIDE;
574
575       nm->total_enabled_count++;
576       vlib_process_signal_event (vm,
577                                  nm->expire_walk_node_index,
578                                  NAT64_CLEANER_RESCHEDULE, 0);
579
580     }
581   else
582     {
583       if (!interface)
584         return VNET_API_ERROR_NO_SUCH_ENTRY;
585
586       if ((nat64_interface_is_inside (interface)
587            && nat64_interface_is_outside (interface)))
588         interface->flags &=
589           is_inside ? ~NAT64_INTERFACE_FLAG_IS_INSIDE :
590           ~NAT64_INTERFACE_FLAG_IS_OUTSIDE;
591       else
592         pool_put (nm->interfaces, interface);
593
594       nm->total_enabled_count--;
595     }
596
597   if (!is_inside)
598     {
599       /* *INDENT-OFF* */
600       vec_foreach (ap, nm->addr_pool)
601         nat64_add_del_addr_to_fib (&ap->addr, 32, sw_if_index, is_add);
602       /* *INDENT-ON* */
603     }
604
605   if (nm->num_workers > 1)
606     {
607       feature_name =
608         is_inside ? "nat64-in2out-handoff" : "nat64-out2in-handoff";
609       if (nm->fq_in2out_index == ~0)
610         nm->fq_in2out_index =
611           vlib_frame_queue_main_init (nat64_in2out_node.index, 0);
612       if (nm->fq_out2in_index == ~0)
613         nm->fq_out2in_index =
614           vlib_frame_queue_main_init (nat64_out2in_node.index, 0);
615     }
616   else
617     feature_name = is_inside ? "nat64-in2out" : "nat64-out2in";
618
619   arc_name = is_inside ? "ip6-unicast" : "ip4-unicast";
620
621   if (is_inside)
622     {
623       int rv = ip6_sv_reass_enable_disable_with_refcnt (sw_if_index, is_add);
624       if (rv)
625         return rv;
626     }
627   else
628     {
629       int rv = ip4_sv_reass_enable_disable_with_refcnt (sw_if_index, is_add);
630       if (rv)
631         return rv;
632     }
633
634   return vnet_feature_enable_disable (arc_name, feature_name, sw_if_index,
635                                       is_add, 0, 0);
636 }
637
638 void
639 nat64_interfaces_walk (nat64_interface_walk_fn_t fn, void *ctx)
640 {
641   nat64_main_t *nm = &nat64_main;
642   nat64_interface_t *i = 0;
643
644   /* *INDENT-OFF* */
645   pool_foreach (i, nm->interfaces)
646    {
647     if (fn (i, ctx))
648       break;
649   }
650   /* *INDENT-ON* */
651 }
652
653 // TODO: plugin independent
654 static_always_inline u16
655 nat64_random_port (u16 min, u16 max)
656 {
657   nat64_main_t *nm = &nat64_main;
658   u32 rwide;
659   u16 r;
660
661   rwide = random_u32 (&nm->random_seed);
662   r = rwide & 0xFFFF;
663   if (r >= min && r <= max)
664     return r;
665
666   return min + (rwide % (max - min + 1));
667 }
668
669 static_always_inline int
670 nat64_alloc_addr_and_port_default (nat64_address_t * addresses,
671                                    u32 fib_index,
672                                    u32 thread_index,
673                                    nat_protocol_t proto,
674                                    ip4_address_t * addr,
675                                    u16 * port,
676                                    u16 port_per_thread, u32 nat_thread_index)
677 {
678   int i;
679   nat64_address_t *a, *ga = 0;
680   u32 portnum;
681
682   for (i = 0; i < vec_len (addresses); i++)
683     {
684       a = addresses + i;
685       switch (proto)
686         {
687 #define _(N, j, n, s) \
688         case NAT_PROTOCOL_##N: \
689           if (a->busy_##n##_ports_per_thread[thread_index] < port_per_thread) \
690             { \
691               if (a->fib_index == fib_index) \
692                 { \
693                   while (1) \
694                     { \
695                       portnum = (port_per_thread * \
696                         nat_thread_index) + \
697                         nat64_random_port(0, port_per_thread - 1) + 1024; \
698                       if (a->busy_##n##_port_refcounts[portnum]) \
699                         continue; \
700                       --a->busy_##n##_port_refcounts[portnum]; \
701                       a->busy_##n##_ports_per_thread[thread_index]++; \
702                       a->busy_##n##_ports++; \
703                       *addr = a->addr; \
704                       *port = clib_host_to_net_u16(portnum); \
705                       return 0; \
706                     } \
707                 } \
708               else if (a->fib_index == ~0) \
709                 { \
710                   ga = a; \
711                 } \
712             } \
713           break;
714           foreach_nat_protocol
715 #undef _
716         default:
717           return 1;
718         }
719
720     }
721
722   if (ga)
723     {
724       a = ga;
725       switch (proto)
726         {
727 #define _(N, j, n, s) \
728         case NAT_PROTOCOL_##N: \
729           while (1) \
730             { \
731               portnum = (port_per_thread * \
732                 nat_thread_index) + \
733                 nat64_random_port(0, port_per_thread - 1) + 1024; \
734               if (a->busy_##n##_port_refcounts[portnum]) \
735                 continue; \
736               ++a->busy_##n##_port_refcounts[portnum]; \
737               a->busy_##n##_ports_per_thread[thread_index]++; \
738               a->busy_##n##_ports++; \
739               *addr = a->addr; \
740               *port = clib_host_to_net_u16(portnum); \
741               return 0; \
742             }
743           break;
744           foreach_nat_protocol
745 #undef _
746         default:
747           return 1;
748         }
749     }
750
751   /* Totally out of translations to use... */
752   nat_ipfix_logging_addresses_exhausted (thread_index, 0);
753   return 1;
754 }
755
756 int
757 nat64_alloc_out_addr_and_port (u32 fib_index, nat_protocol_t proto,
758                                ip4_address_t * addr, u16 * port,
759                                u32 thread_index)
760 {
761   nat64_main_t *nm = &nat64_main;
762   u32 worker_index = 0;
763   int rv;
764
765   if (nm->num_workers > 1)
766     worker_index = thread_index - nm->first_worker_index;
767
768   rv = nat64_alloc_addr_and_port_default (nm->addr_pool, fib_index,
769                                           thread_index,
770                                           proto, addr, port,
771                                           nm->port_per_thread, worker_index);
772
773   return rv;
774 }
775
776 static void
777 nat64_free_out_addr_and_port (struct nat64_db_s *db, ip4_address_t * addr,
778                               u16 port, u8 protocol)
779 {
780   nat64_main_t *nm = &nat64_main;
781   u32 thread_index = db - nm->db;
782   nat_protocol_t proto = ip_proto_to_nat_proto (protocol);
783   u16 port_host_byte_order = clib_net_to_host_u16 (port);
784   nat64_address_t *a;
785   int i;
786
787   for (i = 0; i < vec_len (nm->addr_pool); i++)
788     {
789       a = nm->addr_pool + i;
790       if (addr->as_u32 != a->addr.as_u32)
791         continue;
792       switch (proto)
793         {
794 #define _(N, j, n, s) \
795         case NAT_PROTOCOL_##N: \
796           ASSERT (a->busy_##n##_port_refcounts[port_host_byte_order] >= 1); \
797           --a->busy_##n##_port_refcounts[port_host_byte_order]; \
798           a->busy_##n##_ports--; \
799           a->busy_##n##_ports_per_thread[thread_index]--; \
800           break;
801           foreach_nat_protocol
802 #undef _
803         default:
804           nat_elog_str ("unknown protocol");
805           return;
806         }
807       break;
808     }
809 }
810
811 /**
812  * @brief Add/delete static BIB entry in worker thread.
813  */
814 static uword
815 nat64_static_bib_worker_fn (vlib_main_t * vm, vlib_node_runtime_t * rt,
816                             vlib_frame_t * f)
817 {
818   nat64_main_t *nm = &nat64_main;
819   u32 thread_index = vm->thread_index;
820   nat64_db_t *db = &nm->db[thread_index];
821   nat64_static_bib_to_update_t *static_bib;
822   nat64_db_bib_entry_t *bibe;
823   ip46_address_t addr;
824
825   /* *INDENT-OFF* */
826   pool_foreach (static_bib, nm->static_bibs)
827    {
828     if ((static_bib->thread_index != thread_index) || (static_bib->done))
829       continue;
830
831     if (static_bib->is_add)
832       {
833           (void) nat64_db_bib_entry_create (thread_index, db,
834                                             &static_bib->in_addr,
835                                             &static_bib->out_addr,
836                                             static_bib->in_port,
837                                             static_bib->out_port,
838                                             static_bib->fib_index,
839                                             static_bib->proto, 1);
840           vlib_set_simple_counter (&nm->total_bibs, thread_index, 0,
841                                    db->bib.bib_entries_num);
842       }
843     else
844       {
845         addr.as_u64[0] = static_bib->in_addr.as_u64[0];
846         addr.as_u64[1] = static_bib->in_addr.as_u64[1];
847         bibe = nat64_db_bib_entry_find (db, &addr, static_bib->in_port,
848                                         static_bib->proto,
849                                         static_bib->fib_index, 1);
850         if (bibe)
851           {
852             nat64_db_bib_entry_free (thread_index, db, bibe);
853             vlib_set_simple_counter (&nm->total_bibs, thread_index, 0,
854                                      db->bib.bib_entries_num);
855             vlib_set_simple_counter (&nm->total_sessions, thread_index, 0,
856                                      db->st.st_entries_num);
857           }
858       }
859
860       static_bib->done = 1;
861   }
862   /* *INDENT-ON* */
863
864   return 0;
865 }
866
867 static vlib_node_registration_t nat64_static_bib_worker_node;
868
869 /* *INDENT-OFF* */
870 VLIB_REGISTER_NODE (nat64_static_bib_worker_node, static) = {
871     .function = nat64_static_bib_worker_fn,
872     .type = VLIB_NODE_TYPE_INPUT,
873     .state = VLIB_NODE_STATE_INTERRUPT,
874     .name = "nat64-static-bib-worker",
875 };
876 /* *INDENT-ON* */
877
878 int
879 nat64_add_del_static_bib_entry (ip6_address_t * in_addr,
880                                 ip4_address_t * out_addr, u16 in_port,
881                                 u16 out_port, u8 proto, u32 vrf_id, u8 is_add)
882 {
883   nat64_main_t *nm = &nat64_main;
884   nat64_db_bib_entry_t *bibe;
885   u32 fib_index = fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP6, vrf_id,
886                                                      nm->fib_src_hi);
887   nat_protocol_t p = ip_proto_to_nat_proto (proto);
888   ip46_address_t addr;
889   int i;
890   nat64_address_t *a;
891   u32 thread_index = 0;
892   nat64_db_t *db;
893   nat64_static_bib_to_update_t *static_bib;
894   vlib_main_t *worker_vm;
895   u32 *to_be_free = 0, *index;
896
897   if (nm->num_workers > 1)
898     {
899       thread_index = nat64_get_worker_in2out (in_addr);
900       db = &nm->db[thread_index];
901     }
902   else
903     db = &nm->db[nm->num_workers];
904
905   addr.as_u64[0] = in_addr->as_u64[0];
906   addr.as_u64[1] = in_addr->as_u64[1];
907   bibe =
908     nat64_db_bib_entry_find (db, &addr, clib_host_to_net_u16 (in_port),
909                              proto, fib_index, 1);
910
911   if (is_add)
912     {
913       if (bibe)
914         return VNET_API_ERROR_VALUE_EXIST;
915
916       /* outside port must be assigned to same thread as internall address */
917       if ((out_port > 1024) && (nm->num_workers > 1))
918         {
919           if (thread_index != ((out_port - 1024) / nm->port_per_thread))
920             return VNET_API_ERROR_INVALID_VALUE_2;
921         }
922
923       for (i = 0; i < vec_len (nm->addr_pool); i++)
924         {
925           a = nm->addr_pool + i;
926           if (out_addr->as_u32 != a->addr.as_u32)
927             continue;
928           switch (p)
929             {
930 #define _(N, j, n, s) \
931             case NAT_PROTOCOL_##N: \
932               if (a->busy_##n##_port_refcounts[out_port]) \
933                 return VNET_API_ERROR_INVALID_VALUE; \
934               ++a->busy_##n##_port_refcounts[out_port]; \
935               if (out_port > 1024) \
936                 { \
937                   a->busy_##n##_ports++; \
938                   a->busy_##n##_ports_per_thread[thread_index]++; \
939                 } \
940               break;
941               foreach_nat_protocol
942 #undef _
943             default:
944               clib_memset (&addr, 0, sizeof (addr));
945               addr.ip4.as_u32 = out_addr->as_u32;
946               if (nat64_db_bib_entry_find (db, &addr, 0, proto, fib_index, 0))
947                 return VNET_API_ERROR_INVALID_VALUE;
948             }
949           break;
950         }
951       if (!nm->num_workers)
952         {
953           bibe =
954             nat64_db_bib_entry_create (thread_index, db, in_addr, out_addr,
955                                        clib_host_to_net_u16 (in_port),
956                                        clib_host_to_net_u16 (out_port),
957                                        fib_index, proto, 1);
958           if (!bibe)
959             return VNET_API_ERROR_UNSPECIFIED;
960
961           vlib_set_simple_counter (&nm->total_bibs, thread_index, 0,
962                                    db->bib.bib_entries_num);
963         }
964     }
965   else
966     {
967       if (!bibe)
968         return VNET_API_ERROR_NO_SUCH_ENTRY;
969
970       if (!nm->num_workers)
971         {
972           nat64_db_bib_entry_free (thread_index, db, bibe);
973           vlib_set_simple_counter (&nm->total_bibs, thread_index, 0,
974                                    db->bib.bib_entries_num);
975         }
976     }
977
978   if (nm->num_workers)
979     {
980       /* *INDENT-OFF* */
981       pool_foreach (static_bib, nm->static_bibs)
982        {
983         if (static_bib->done)
984           vec_add1 (to_be_free, static_bib - nm->static_bibs);
985       }
986       vec_foreach (index, to_be_free)
987         pool_put_index (nm->static_bibs, index[0]);
988       /* *INDENT-ON* */
989       vec_free (to_be_free);
990       pool_get (nm->static_bibs, static_bib);
991       static_bib->in_addr.as_u64[0] = in_addr->as_u64[0];
992       static_bib->in_addr.as_u64[1] = in_addr->as_u64[1];
993       static_bib->in_port = clib_host_to_net_u16 (in_port);
994       static_bib->out_addr.as_u32 = out_addr->as_u32;
995       static_bib->out_port = clib_host_to_net_u16 (out_port);
996       static_bib->fib_index = fib_index;
997       static_bib->proto = proto;
998       static_bib->is_add = is_add;
999       static_bib->thread_index = thread_index;
1000       static_bib->done = 0;
1001       worker_vm = vlib_get_main_by_index (thread_index);
1002       if (worker_vm)
1003         vlib_node_set_interrupt_pending (worker_vm,
1004                                          nat64_static_bib_worker_node.index);
1005       else
1006         return VNET_API_ERROR_UNSPECIFIED;
1007     }
1008
1009   return 0;
1010 }
1011
1012 int
1013 nat64_set_udp_timeout (u32 timeout)
1014 {
1015   nat64_main_t *nm = &nat64_main;
1016
1017   if (timeout == 0)
1018     nm->udp_timeout = NAT_UDP_TIMEOUT;
1019   else
1020     nm->udp_timeout = timeout;
1021
1022   return 0;
1023 }
1024
1025 u32
1026 nat64_get_udp_timeout (void)
1027 {
1028   nat64_main_t *nm = &nat64_main;
1029
1030   return nm->udp_timeout;
1031 }
1032
1033 int
1034 nat64_set_icmp_timeout (u32 timeout)
1035 {
1036   nat64_main_t *nm = &nat64_main;
1037
1038   if (timeout == 0)
1039     nm->icmp_timeout = NAT_ICMP_TIMEOUT;
1040   else
1041     nm->icmp_timeout = timeout;
1042
1043   return 0;
1044 }
1045
1046 void
1047 nat64_reset_timeouts ()
1048 {
1049   nat64_main_t *nm = &nat64_main;
1050
1051   nm->udp_timeout = NAT_UDP_TIMEOUT;
1052   nm->icmp_timeout = NAT_ICMP_TIMEOUT;
1053   nm->tcp_est_timeout = NAT_TCP_ESTABLISHED_TIMEOUT;
1054   nm->tcp_trans_timeout = NAT_TCP_TRANSITORY_TIMEOUT;
1055 }
1056
1057 u32
1058 nat64_get_icmp_timeout (void)
1059 {
1060   nat64_main_t *nm = &nat64_main;
1061
1062   return nm->icmp_timeout;
1063 }
1064
1065 int
1066 nat64_set_tcp_timeouts (u32 trans, u32 est)
1067 {
1068   nat64_main_t *nm = &nat64_main;
1069
1070   if (trans == 0)
1071     nm->tcp_trans_timeout = NAT_TCP_TRANSITORY_TIMEOUT;
1072   else
1073     nm->tcp_trans_timeout = trans;
1074
1075   if (est == 0)
1076     nm->tcp_est_timeout = NAT_TCP_ESTABLISHED_TIMEOUT;
1077   else
1078     nm->tcp_est_timeout = est;
1079
1080   return 0;
1081 }
1082
1083 u32
1084 nat64_get_tcp_trans_timeout (void)
1085 {
1086   nat64_main_t *nm = &nat64_main;
1087
1088   return nm->tcp_trans_timeout;
1089 }
1090
1091 u32
1092 nat64_get_tcp_est_timeout (void)
1093 {
1094   nat64_main_t *nm = &nat64_main;
1095
1096   return nm->tcp_est_timeout;
1097 }
1098
1099 void
1100 nat64_session_reset_timeout (nat64_db_st_entry_t * ste, vlib_main_t * vm)
1101 {
1102   nat64_main_t *nm = &nat64_main;
1103   u32 now = (u32) vlib_time_now (vm);
1104
1105   switch (ip_proto_to_nat_proto (ste->proto))
1106     {
1107     case NAT_PROTOCOL_ICMP:
1108       ste->expire = now + nm->icmp_timeout;
1109       return;
1110     case NAT_PROTOCOL_TCP:
1111       {
1112         switch (ste->tcp_state)
1113           {
1114           case NAT64_TCP_STATE_V4_INIT:
1115           case NAT64_TCP_STATE_V6_INIT:
1116           case NAT64_TCP_STATE_V4_FIN_RCV:
1117           case NAT64_TCP_STATE_V6_FIN_RCV:
1118           case NAT64_TCP_STATE_V6_FIN_V4_FIN_RCV:
1119           case NAT64_TCP_STATE_TRANS:
1120             ste->expire = now + nm->tcp_trans_timeout;
1121             return;
1122           case NAT64_TCP_STATE_ESTABLISHED:
1123             ste->expire = now + nm->tcp_est_timeout;
1124             return;
1125           default:
1126             return;
1127           }
1128       }
1129     case NAT_PROTOCOL_UDP:
1130       ste->expire = now + nm->udp_timeout;
1131       return;
1132     default:
1133       ste->expire = now + nm->udp_timeout;
1134       return;
1135     }
1136 }
1137
1138 void
1139 nat64_tcp_session_set_state (nat64_db_st_entry_t * ste, tcp_header_t * tcp,
1140                              u8 is_ip6)
1141 {
1142   switch (ste->tcp_state)
1143     {
1144     case NAT64_TCP_STATE_CLOSED:
1145       {
1146         if (tcp->flags & TCP_FLAG_SYN)
1147           {
1148             if (is_ip6)
1149               ste->tcp_state = NAT64_TCP_STATE_V6_INIT;
1150             else
1151               ste->tcp_state = NAT64_TCP_STATE_V4_INIT;
1152           }
1153         return;
1154       }
1155     case NAT64_TCP_STATE_V4_INIT:
1156       {
1157         if (is_ip6 && (tcp->flags & TCP_FLAG_SYN))
1158           ste->tcp_state = NAT64_TCP_STATE_ESTABLISHED;
1159         return;
1160       }
1161     case NAT64_TCP_STATE_V6_INIT:
1162       {
1163         if (!is_ip6 && (tcp->flags & TCP_FLAG_SYN))
1164           ste->tcp_state = NAT64_TCP_STATE_ESTABLISHED;
1165         return;
1166       }
1167     case NAT64_TCP_STATE_ESTABLISHED:
1168       {
1169         if (tcp->flags & TCP_FLAG_FIN)
1170           {
1171             if (is_ip6)
1172               ste->tcp_state = NAT64_TCP_STATE_V6_FIN_RCV;
1173             else
1174               ste->tcp_state = NAT64_TCP_STATE_V4_FIN_RCV;
1175           }
1176         else if (tcp->flags & TCP_FLAG_RST)
1177           {
1178             ste->tcp_state = NAT64_TCP_STATE_TRANS;
1179           }
1180         return;
1181       }
1182     case NAT64_TCP_STATE_V4_FIN_RCV:
1183       {
1184         if (is_ip6 && (tcp->flags & TCP_FLAG_FIN))
1185           ste->tcp_state = NAT64_TCP_STATE_V6_FIN_V4_FIN_RCV;
1186         return;
1187       }
1188     case NAT64_TCP_STATE_V6_FIN_RCV:
1189       {
1190         if (!is_ip6 && (tcp->flags & TCP_FLAG_FIN))
1191           ste->tcp_state = NAT64_TCP_STATE_V6_FIN_V4_FIN_RCV;
1192         return;
1193       }
1194     case NAT64_TCP_STATE_TRANS:
1195       {
1196         if (!(tcp->flags & TCP_FLAG_RST))
1197           ste->tcp_state = NAT64_TCP_STATE_ESTABLISHED;
1198         return;
1199       }
1200     default:
1201       return;
1202     }
1203 }
1204
1205 int
1206 nat64_add_del_prefix (ip6_address_t * prefix, u8 plen, u32 vrf_id, u8 is_add)
1207 {
1208   nat64_main_t *nm = &nat64_main;
1209   nat64_prefix_t *p = 0;
1210   int i;
1211
1212   /* Verify prefix length */
1213   if (plen != 32 && plen != 40 && plen != 48 && plen != 56 && plen != 64
1214       && plen != 96)
1215     return VNET_API_ERROR_INVALID_VALUE;
1216
1217   /* Check if tenant already have prefix */
1218   for (i = 0; i < vec_len (nm->pref64); i++)
1219     {
1220       if (nm->pref64[i].vrf_id == vrf_id)
1221         {
1222           p = nm->pref64 + i;
1223           break;
1224         }
1225     }
1226
1227   if (is_add)
1228     {
1229       if (!p)
1230         {
1231           vec_add2 (nm->pref64, p, 1);
1232           p->fib_index =
1233             fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP6, vrf_id,
1234                                                nm->fib_src_hi);
1235           p->vrf_id = vrf_id;
1236         }
1237
1238       p->prefix.as_u64[0] = prefix->as_u64[0];
1239       p->prefix.as_u64[1] = prefix->as_u64[1];
1240       p->plen = plen;
1241     }
1242   else
1243     {
1244       if (!p)
1245         return VNET_API_ERROR_NO_SUCH_ENTRY;
1246
1247       // TODO: missing fib_table_unlock ?
1248
1249       vec_del1 (nm->pref64, i);
1250     }
1251
1252   return 0;
1253 }
1254
1255 void
1256 nat64_prefix_walk (nat64_prefix_walk_fn_t fn, void *ctx)
1257 {
1258   nat64_main_t *nm = &nat64_main;
1259   nat64_prefix_t *p = 0;
1260
1261   /* *INDENT-OFF* */
1262   vec_foreach (p, nm->pref64)
1263     {
1264       if (fn (p, ctx))
1265         break;
1266     };
1267   /* *INDENT-ON* */
1268 }
1269
1270 void
1271 nat64_compose_ip6 (ip6_address_t * ip6, ip4_address_t * ip4, u32 fib_index)
1272 {
1273   nat64_main_t *nm = &nat64_main;
1274   nat64_prefix_t *p, *gp = 0, *prefix = 0;
1275
1276   /* *INDENT-OFF* */
1277   vec_foreach (p, nm->pref64)
1278     {
1279       if (p->fib_index == fib_index)
1280         {
1281           prefix = p;
1282           break;
1283         }
1284
1285       if (p->fib_index == 0)
1286         gp = p;
1287     };
1288   /* *INDENT-ON* */
1289
1290   if (!prefix)
1291     prefix = gp;
1292
1293   if (prefix)
1294     {
1295       clib_memcpy_fast (ip6, &p->prefix, sizeof (ip6_address_t));
1296       switch (p->plen)
1297         {
1298         case 32:
1299           ip6->as_u32[1] = ip4->as_u32;
1300           break;
1301         case 40:
1302           ip6->as_u8[5] = ip4->as_u8[0];
1303           ip6->as_u8[6] = ip4->as_u8[1];
1304           ip6->as_u8[7] = ip4->as_u8[2];
1305           ip6->as_u8[9] = ip4->as_u8[3];
1306           break;
1307         case 48:
1308           ip6->as_u8[6] = ip4->as_u8[0];
1309           ip6->as_u8[7] = ip4->as_u8[1];
1310           ip6->as_u8[9] = ip4->as_u8[2];
1311           ip6->as_u8[10] = ip4->as_u8[3];
1312           break;
1313         case 56:
1314           ip6->as_u8[7] = ip4->as_u8[0];
1315           ip6->as_u8[9] = ip4->as_u8[1];
1316           ip6->as_u8[10] = ip4->as_u8[2];
1317           ip6->as_u8[11] = ip4->as_u8[3];
1318           break;
1319         case 64:
1320           ip6->as_u8[9] = ip4->as_u8[0];
1321           ip6->as_u8[10] = ip4->as_u8[1];
1322           ip6->as_u8[11] = ip4->as_u8[2];
1323           ip6->as_u8[12] = ip4->as_u8[3];
1324           break;
1325         case 96:
1326           ip6->as_u32[3] = ip4->as_u32;
1327           break;
1328         default:
1329           nat_elog_str ("invalid prefix length");
1330           break;
1331         }
1332     }
1333   else
1334     {
1335       clib_memcpy_fast (ip6, well_known_prefix, sizeof (ip6_address_t));
1336       ip6->as_u32[3] = ip4->as_u32;
1337     }
1338 }
1339
1340 void
1341 nat64_extract_ip4 (ip6_address_t * ip6, ip4_address_t * ip4, u32 fib_index)
1342 {
1343   nat64_main_t *nm = &nat64_main;
1344   nat64_prefix_t *p, *gp = 0;
1345   u8 plen = 0;
1346
1347   /* *INDENT-OFF* */
1348   vec_foreach (p, nm->pref64)
1349     {
1350       if (p->fib_index == fib_index)
1351         {
1352           plen = p->plen;
1353           break;
1354         }
1355
1356       if (p->vrf_id == 0)
1357         gp = p;
1358     };
1359   /* *INDENT-ON* */
1360
1361   if (!plen)
1362     {
1363       if (gp)
1364         plen = gp->plen;
1365       else
1366         plen = 96;
1367     }
1368
1369   switch (plen)
1370     {
1371     case 32:
1372       ip4->as_u32 = ip6->as_u32[1];
1373       break;
1374     case 40:
1375       ip4->as_u8[0] = ip6->as_u8[5];
1376       ip4->as_u8[1] = ip6->as_u8[6];
1377       ip4->as_u8[2] = ip6->as_u8[7];
1378       ip4->as_u8[3] = ip6->as_u8[9];
1379       break;
1380     case 48:
1381       ip4->as_u8[0] = ip6->as_u8[6];
1382       ip4->as_u8[1] = ip6->as_u8[7];
1383       ip4->as_u8[2] = ip6->as_u8[9];
1384       ip4->as_u8[3] = ip6->as_u8[10];
1385       break;
1386     case 56:
1387       ip4->as_u8[0] = ip6->as_u8[7];
1388       ip4->as_u8[1] = ip6->as_u8[9];
1389       ip4->as_u8[2] = ip6->as_u8[10];
1390       ip4->as_u8[3] = ip6->as_u8[11];
1391       break;
1392     case 64:
1393       ip4->as_u8[0] = ip6->as_u8[9];
1394       ip4->as_u8[1] = ip6->as_u8[10];
1395       ip4->as_u8[2] = ip6->as_u8[11];
1396       ip4->as_u8[3] = ip6->as_u8[12];
1397       break;
1398     case 96:
1399       ip4->as_u32 = ip6->as_u32[3];
1400       break;
1401     default:
1402       nat_elog_str ("invalid prefix length");
1403       break;
1404     }
1405 }
1406
1407 /**
1408  * @brief Per worker process checking expire time for NAT64 sessions.
1409  */
1410 static uword
1411 nat64_expire_worker_walk_fn (vlib_main_t * vm, vlib_node_runtime_t * rt,
1412                              vlib_frame_t * f)
1413 {
1414   nat64_main_t *nm = &nat64_main;
1415   u32 thread_index = vm->thread_index;
1416   nat64_db_t *db;
1417   u32 now;
1418
1419   // TODO: barier sync on plugin enabled
1420   if (plugin_enabled () == 0)
1421     return 0;
1422
1423   db = &nm->db[thread_index];
1424   now = (u32) vlib_time_now (vm);
1425
1426   nad64_db_st_free_expired (thread_index, db, now);
1427   vlib_set_simple_counter (&nm->total_bibs, thread_index, 0,
1428                            db->bib.bib_entries_num);
1429   vlib_set_simple_counter (&nm->total_sessions, thread_index, 0,
1430                            db->st.st_entries_num);
1431   return 0;
1432 }
1433
1434 /* *INDENT-OFF* */
1435 VLIB_REGISTER_NODE (nat64_expire_worker_walk_node, static) = {
1436     .function = nat64_expire_worker_walk_fn,
1437     .type = VLIB_NODE_TYPE_INPUT,
1438     .state = VLIB_NODE_STATE_INTERRUPT,
1439     .name = "nat64-expire-worker-walk",
1440 };
1441 /* *INDENT-ON* */
1442
1443 /**
1444  * @brief Centralized process to drive per worker expire walk.
1445  */
1446 static uword
1447 nat64_expire_walk_fn (vlib_main_t * vm, vlib_node_runtime_t * rt,
1448                       vlib_frame_t * f)
1449 {
1450   nat64_main_t *nm = &nat64_main;
1451   vlib_main_t **worker_vms = 0, *worker_vm;
1452   int i;
1453   uword event_type, *event_data = 0;
1454
1455   if (vlib_get_n_threads () == 0)
1456     vec_add1 (worker_vms, vm);
1457   else
1458     {
1459       for (i = 0; i < vlib_get_n_threads (); i++)
1460         {
1461           worker_vm = vlib_get_main_by_index (i);
1462           if (worker_vm)
1463             vec_add1 (worker_vms, worker_vm);
1464         }
1465     }
1466
1467   while (1)
1468     {
1469       if (nm->total_enabled_count)
1470         {
1471           vlib_process_wait_for_event_or_clock (vm, 10.0);
1472           event_type = vlib_process_get_events (vm, &event_data);
1473         }
1474       else
1475         {
1476           vlib_process_wait_for_event (vm);
1477           event_type = vlib_process_get_events (vm, &event_data);
1478         }
1479
1480       switch (event_type)
1481         {
1482         case ~0:
1483           break;
1484         case NAT64_CLEANER_RESCHEDULE:
1485           break;
1486         default:
1487           nat64_log_err ("unknown event %u", event_type);
1488           break;
1489         }
1490
1491       for (i = 0; i < vec_len (worker_vms); i++)
1492         {
1493           worker_vm = worker_vms[i];
1494           vlib_node_set_interrupt_pending (worker_vm,
1495                                            nm->expire_worker_walk_node_index);
1496         }
1497     }
1498
1499   return 0;
1500 }
1501
1502 void
1503 nat64_create_expire_walk_process ()
1504 {
1505   nat64_main_t *nm = &nat64_main;
1506
1507   if (nm->expire_walk_node_index)
1508     return;
1509   nm->expire_walk_node_index = vlib_process_create (vlib_get_main (),
1510                                                     "nat64-expire-walk",
1511                                                     nat64_expire_walk_fn,
1512                                                     16 /* stack_bytes */ );
1513 }
1514
1515 int
1516 nat64_plugin_enable (nat64_config_t c)
1517 {
1518   nat64_main_t *nm = &nat64_main;
1519
1520   if (plugin_enabled () == 1)
1521     {
1522       nat64_log_err ("plugin already enabled!");
1523       return 1;
1524     }
1525
1526   if (!c.bib_buckets)
1527     c.bib_buckets = 1024;
1528
1529   if (!c.bib_memory_size)
1530     c.bib_memory_size = 128 << 20;
1531
1532   if (!c.st_buckets)
1533     c.st_buckets = 2048;
1534
1535   if (!c.st_memory_size)
1536     c.st_memory_size = 256 << 20;
1537
1538   nm->config = c;
1539
1540   if (nat64_init_hash (c))
1541     {
1542       nat64_log_err ("initializing hashes failed!");
1543       return 1;
1544     }
1545
1546   nat64_create_expire_walk_process ();
1547
1548   nm->enabled = 1;
1549   return 0;
1550 }
1551
1552 int
1553 nat64_plugin_disable ()
1554 {
1555   nat64_main_t *nm = &nat64_main;
1556   vnet_main_t *vnm = vnet_get_main ();
1557   int rv = 0;
1558
1559   nat64_address_t *a;
1560   nat64_interface_t *i, *interfaces = 0;
1561
1562   if (plugin_enabled () == 0)
1563     {
1564       nat64_log_err ("plugin already disabled!");
1565       return 1;
1566     }
1567   nm->enabled = 0;
1568
1569   /* *INDENT-OFF* */
1570   pool_foreach (i, nm->interfaces)
1571    {
1572     vec_add1 (interfaces, *i);
1573   }
1574   /* *INDENT-ON* */
1575   vec_foreach (i, interfaces)
1576   {
1577     rv = nat64_interface_add_del (i->sw_if_index, i->flags, 0);
1578     if (rv)
1579       {
1580         nat64_log_err ("%U %s interface del failed",
1581                        format_vnet_sw_if_index_name, vnm, i->sw_if_index,
1582                        i->flags & NAT64_INTERFACE_FLAG_IS_INSIDE ?
1583                        "inside" : "outside");
1584       }
1585   }
1586   vec_free (interfaces);
1587   pool_free (nm->interfaces);
1588
1589   nat64_reset_timeouts ();
1590
1591   if (nat64_free_hash ())
1592     {
1593       rv = 1;
1594       nat64_log_err ("freeing hashes failed!");
1595     }
1596
1597   // TODO: based on nat64_add_del_prefix fib_table_unlock is not called
1598   vec_free (nm->pref64);
1599
1600   if (vec_len (nm->addr_pool))
1601     {
1602       vec_foreach (a, nm->addr_pool)
1603       {
1604         if (a->fib_index != ~0)
1605           fib_table_unlock (a->fib_index, FIB_PROTOCOL_IP6, nm->fib_src_hi);
1606       }
1607       vec_free (nm->addr_pool);
1608     }
1609   return rv;
1610 }
1611
1612 uword
1613 unformat_nat_protocol (unformat_input_t * input, va_list * args)
1614 {
1615   u32 *r = va_arg (*args, u32 *);
1616
1617   if (0);
1618 #define _(N, i, n, s) else if (unformat (input, s)) *r = NAT_PROTOCOL_##N;
1619   foreach_nat_protocol
1620 #undef _
1621     else
1622     return 0;
1623   return 1;
1624 }
1625
1626 u8 *
1627 format_nat_protocol (u8 * s, va_list * args)
1628 {
1629   u32 i = va_arg (*args, u32);
1630   u8 *t = 0;
1631
1632   switch (i)
1633     {
1634 #define _(N, j, n, str) case NAT_PROTOCOL_##N: t = (u8 *) str; break;
1635       foreach_nat_protocol
1636 #undef _
1637     default:
1638       s = format (s, "unknown");
1639       return s;
1640     }
1641   s = format (s, "%s", t);
1642   return s;
1643 }
1644
1645 /*
1646  * fd.io coding-style-patch-verification: ON
1647  *
1648  * Local Variables:
1649  * eval: (c-set-style "gnu")
1650  * End:
1651  */