dpdk: disable DPDK iAVF interrupt support
[vpp.git] / src / plugins / nat / nat44-ei / nat44_ei.c
1 /*
2  * nat44_ei.c - nat44 endpoint dependent plugin
3  *
4  * Copyright (c) 2020 Cisco and/or its affiliates.
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at:
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14  * License for the specific language governing permissions and limitations
15  * under the License.
16  */
17
18 #include <vnet/plugin/plugin.h>
19 #include <vpp/app/version.h>
20
21 #include <vnet/vnet.h>
22 #include <vnet/ip/ip.h>
23 #include <vnet/ip/ip4.h>
24 #include <vnet/ip/ip_table.h>
25 #include <vnet/ip/reass/ip4_sv_reass.h>
26 #include <vnet/fib/fib_table.h>
27 #include <vnet/fib/ip4_fib.h>
28 #include <vnet/plugin/plugin.h>
29
30 // nat lib
31 #include <nat/lib/log.h>
32 #include <nat/lib/nat_syslog.h>
33 #include <nat/lib/nat_inlines.h>
34 #include <nat/lib/ipfix_logging.h>
35
36 #include <nat/nat44-ei/nat44_ei_dpo.h>
37 #include <nat/nat44-ei/nat44_ei_inlines.h>
38 #include <nat/nat44-ei/nat44_ei.h>
39
40 nat44_ei_main_t nat44_ei_main;
41
42 extern vlib_node_registration_t nat44_ei_hairpinning_node;
43 extern vlib_node_registration_t nat44_ei_hairpin_dst_node;
44 extern vlib_node_registration_t
45   nat44_ei_in2out_hairpinning_finish_ip4_lookup_node;
46 extern vlib_node_registration_t
47   nat44_ei_in2out_hairpinning_finish_interface_output_node;
48
49 #define skip_if_disabled()                                                    \
50   do                                                                          \
51     {                                                                         \
52       nat44_ei_main_t *nm = &nat44_ei_main;                                   \
53       if (PREDICT_FALSE (!nm->enabled))                                       \
54         return;                                                               \
55     }                                                                         \
56   while (0)
57
58 #define fail_if_enabled()                                                     \
59   do                                                                          \
60     {                                                                         \
61       nat44_ei_main_t *nm = &nat44_ei_main;                                   \
62       if (PREDICT_FALSE (nm->enabled))                                        \
63         {                                                                     \
64           nat44_ei_log_err ("plugin enabled");                                \
65           return 1;                                                           \
66         }                                                                     \
67     }                                                                         \
68   while (0)
69
70 #define fail_if_disabled()                                                    \
71   do                                                                          \
72     {                                                                         \
73       nat44_ei_main_t *nm = &nat44_ei_main;                                   \
74       if (PREDICT_FALSE (!nm->enabled))                                       \
75         {                                                                     \
76           nat44_ei_log_err ("plugin disabled");                               \
77           return 1;                                                           \
78         }                                                                     \
79     }                                                                         \
80   while (0)
81
82 /* Hook up input features */
83 VNET_FEATURE_INIT (ip4_nat_classify, static) = {
84   .arc_name = "ip4-unicast",
85   .node_name = "nat44-ei-classify",
86   .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa",
87                                "ip4-sv-reassembly-feature"),
88 };
89 VNET_FEATURE_INIT (ip4_nat_handoff_classify, static) = {
90   .arc_name = "ip4-unicast",
91   .node_name = "nat44-ei-handoff-classify",
92   .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa",
93                                "ip4-sv-reassembly-feature"),
94 };
95 VNET_FEATURE_INIT (ip4_nat44_ei_in2out, static) = {
96   .arc_name = "ip4-unicast",
97   .node_name = "nat44-ei-in2out",
98   .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa",
99                                "ip4-sv-reassembly-feature"),
100 };
101 VNET_FEATURE_INIT (ip4_nat44_ei_out2in, static) = {
102   .arc_name = "ip4-unicast",
103   .node_name = "nat44-ei-out2in",
104   .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa",
105                                "ip4-sv-reassembly-feature",
106                                "ip4-dhcp-client-detect"),
107 };
108 VNET_FEATURE_INIT (ip4_nat44_ei_in2out_output, static) = {
109   .arc_name = "ip4-output",
110   .node_name = "nat44-ei-in2out-output",
111   .runs_after = VNET_FEATURES ("acl-plugin-out-ip4-fa",
112                                "ip4-sv-reassembly-output-feature"),
113 };
114 VNET_FEATURE_INIT (ip4_nat44_ei_in2out_fast, static) = {
115   .arc_name = "ip4-unicast",
116   .node_name = "nat44-ei-in2out-fast",
117   .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa",
118                                "ip4-sv-reassembly-feature"),
119 };
120 VNET_FEATURE_INIT (ip4_nat44_ei_out2in_fast, static) = {
121   .arc_name = "ip4-unicast",
122   .node_name = "nat44-ei-out2in-fast",
123   .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa",
124                                "ip4-sv-reassembly-feature",
125                                "ip4-dhcp-client-detect"),
126 };
127 VNET_FEATURE_INIT (ip4_nat44_ei_hairpin_dst, static) = {
128   .arc_name = "ip4-unicast",
129   .node_name = "nat44-ei-hairpin-dst",
130   .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa",
131                                "ip4-sv-reassembly-feature"),
132 };
133 VNET_FEATURE_INIT (ip4_nat44_ei_hairpin_src, static) = {
134   .arc_name = "ip4-output",
135   .node_name = "nat44-ei-hairpin-src",
136   .runs_after = VNET_FEATURES ("acl-plugin-out-ip4-fa",
137                                "ip4-sv-reassembly-output-feature"),
138 };
139 VNET_FEATURE_INIT (ip4_nat44_ei_hairpinning, static) = {
140   .arc_name = "ip4-local",
141   .node_name = "nat44-ei-hairpinning",
142   .runs_before = VNET_FEATURES ("ip4-local-end-of-arc"),
143 };
144 VNET_FEATURE_INIT (ip4_nat44_ei_in2out_worker_handoff, static) = {
145   .arc_name = "ip4-unicast",
146   .node_name = "nat44-ei-in2out-worker-handoff",
147   .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa"),
148 };
149 VNET_FEATURE_INIT (ip4_nat44_ei_out2in_worker_handoff, static) = {
150   .arc_name = "ip4-unicast",
151   .node_name = "nat44-ei-out2in-worker-handoff",
152   .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa",
153                                "ip4-dhcp-client-detect"),
154 };
155 VNET_FEATURE_INIT (ip4_nat44_ei_in2out_output_worker_handoff, static) = {
156   .arc_name = "ip4-output",
157   .node_name = "nat44-ei-in2out-output-worker-handoff",
158   .runs_after = VNET_FEATURES ("acl-plugin-out-ip4-fa",
159                                "ip4-sv-reassembly-output-feature"),
160 };
161
162 VLIB_PLUGIN_REGISTER () = {
163   .version = VPP_BUILD_VER,
164   .description = "IPv4 Endpoint-Independent NAT (NAT44 EI)",
165 };
166
167 #define foreach_nat44_ei_classify_error                                       \
168   _ (NEXT_IN2OUT, "next in2out")                                              \
169   _ (NEXT_OUT2IN, "next out2in")                                              \
170   _ (FRAG_CACHED, "fragment cached")
171
172 typedef enum
173 {
174 #define _(sym, str) NAT44_EI_CLASSIFY_ERROR_##sym,
175   foreach_nat44_ei_classify_error
176 #undef _
177     NAT44_EI_CLASSIFY_N_ERROR,
178 } nat44_ei_classify_error_t;
179
180 static char *nat44_ei_classify_error_strings[] = {
181 #define _(sym, string) string,
182   foreach_nat44_ei_classify_error
183 #undef _
184 };
185
186 typedef enum
187 {
188   NAT44_EI_CLASSIFY_NEXT_IN2OUT,
189   NAT44_EI_CLASSIFY_NEXT_OUT2IN,
190   NAT44_EI_CLASSIFY_NEXT_DROP,
191   NAT44_EI_CLASSIFY_N_NEXT,
192 } nat44_ei_classify_next_t;
193
194 typedef struct
195 {
196   u8 next_in2out;
197   u8 cached;
198 } nat44_ei_classify_trace_t;
199
200 void nat44_ei_add_del_addr_to_fib (ip4_address_t *addr, u8 p_len,
201                                    u32 sw_if_index, int is_add);
202
203 static u8 *
204 format_nat44_ei_classify_trace (u8 *s, va_list *args)
205 {
206   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
207   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
208   nat44_ei_classify_trace_t *t = va_arg (*args, nat44_ei_classify_trace_t *);
209   char *next;
210
211   if (t->cached)
212     s = format (s, "nat44-ei-classify: fragment cached");
213   else
214     {
215       next = t->next_in2out ? "nat44-ei-in2out" : "nat44-ei-out2in";
216       s = format (s, "nat44-ei-classify: next %s", next);
217     }
218
219   return s;
220 }
221
222 static void nat44_ei_db_free ();
223
224 static void nat44_ei_db_init (u32 translations, u32 translation_buckets,
225                               u32 user_buckets);
226
227 static void nat44_ei_ip4_add_del_interface_address_cb (
228   ip4_main_t *im, uword opaque, u32 sw_if_index, ip4_address_t *address,
229   u32 address_length, u32 if_address_index, u32 is_delete);
230
231 static void nat44_ei_ip4_add_del_addr_only_sm_cb (
232   ip4_main_t *im, uword opaque, u32 sw_if_index, ip4_address_t *address,
233   u32 address_length, u32 if_address_index, u32 is_delete);
234
235 static void nat44_ei_update_outside_fib (ip4_main_t *im, uword opaque,
236                                          u32 sw_if_index, u32 new_fib_index,
237                                          u32 old_fib_index);
238
239 void
240 nat44_ei_set_node_indexes (nat44_ei_main_t *nm, vlib_main_t *vm)
241 {
242   vlib_node_t *node;
243   node = vlib_get_node_by_name (vm, (u8 *) "nat44-ei-out2in");
244   nm->out2in_node_index = node->index;
245   node = vlib_get_node_by_name (vm, (u8 *) "nat44-ei-in2out");
246   nm->in2out_node_index = node->index;
247   node = vlib_get_node_by_name (vm, (u8 *) "nat44-ei-in2out-output");
248   nm->in2out_output_node_index = node->index;
249 }
250
251 int
252 nat44_ei_set_workers (uword *bitmap)
253 {
254   nat44_ei_main_t *nm = &nat44_ei_main;
255   int i, j = 0;
256
257   if (nm->num_workers < 2)
258     return VNET_API_ERROR_FEATURE_DISABLED;
259
260   if (clib_bitmap_last_set (bitmap) >= nm->num_workers)
261     return VNET_API_ERROR_INVALID_WORKER;
262
263   vec_free (nm->workers);
264   clib_bitmap_foreach (i, bitmap)
265     {
266       vec_add1 (nm->workers, i);
267       nm->per_thread_data[nm->first_worker_index + i].snat_thread_index = j;
268       nm->per_thread_data[nm->first_worker_index + i].thread_index = i;
269       j++;
270     }
271
272   nm->port_per_thread = (0xffff - 1024) / _vec_len (nm->workers);
273
274   return 0;
275 }
276
277 #define nat_validate_simple_counter(c, i)                                     \
278   do                                                                          \
279     {                                                                         \
280       vlib_validate_simple_counter (&c, i);                                   \
281       vlib_zero_simple_counter (&c, i);                                       \
282     }                                                                         \
283   while (0);
284
285 #define nat_init_simple_counter(c, n, sn)                                     \
286   do                                                                          \
287     {                                                                         \
288       c.name = n;                                                             \
289       c.stat_segment_name = sn;                                               \
290       nat_validate_simple_counter (c, 0);                                     \
291     }                                                                         \
292   while (0);
293
294 static_always_inline void
295 nat_validate_interface_counters (nat44_ei_main_t *nm, u32 sw_if_index)
296 {
297 #define _(x)                                                                  \
298   nat_validate_simple_counter (nm->counters.fastpath.in2out.x, sw_if_index);  \
299   nat_validate_simple_counter (nm->counters.fastpath.out2in.x, sw_if_index);  \
300   nat_validate_simple_counter (nm->counters.slowpath.in2out.x, sw_if_index);  \
301   nat_validate_simple_counter (nm->counters.slowpath.out2in.x, sw_if_index);
302   foreach_nat_counter;
303 #undef _
304   nat_validate_simple_counter (nm->counters.hairpinning, sw_if_index);
305 }
306
307 clib_error_t *
308 nat44_ei_init (vlib_main_t *vm)
309 {
310   nat44_ei_main_t *nm = &nat44_ei_main;
311   vlib_thread_main_t *tm = vlib_get_thread_main ();
312   vlib_thread_registration_t *tr;
313   ip4_add_del_interface_address_callback_t cbi = { 0 };
314   ip4_table_bind_callback_t cbt = { 0 };
315   u32 i, num_threads = 0;
316   uword *p, *bitmap = 0;
317
318   clib_memset (nm, 0, sizeof (*nm));
319
320   // required
321   nm->vnet_main = vnet_get_main ();
322   // convenience
323   nm->ip4_main = &ip4_main;
324   nm->api_main = vlibapi_get_main ();
325   nm->ip4_lookup_main = &ip4_main.lookup_main;
326
327   // handoff stuff
328   nm->fq_out2in_index = ~0;
329   nm->fq_in2out_index = ~0;
330   nm->fq_in2out_output_index = ~0;
331
332   nm->log_level = NAT_LOG_ERROR;
333
334   nat44_ei_set_node_indexes (nm, vm);
335   nm->log_class = vlib_log_register_class ("nat44-ei", 0);
336
337   nat_init_simple_counter (nm->total_users, "total-users",
338                            "/nat44-ei/total-users");
339   nat_init_simple_counter (nm->total_sessions, "total-sessions",
340                            "/nat44-ei/total-sessions");
341   nat_init_simple_counter (nm->user_limit_reached, "user-limit-reached",
342                            "/nat44-ei/user-limit-reached");
343
344 #define _(x)                                                                  \
345   nat_init_simple_counter (nm->counters.fastpath.in2out.x, #x,                \
346                            "/nat44-ei/in2out/fastpath/" #x);                  \
347   nat_init_simple_counter (nm->counters.fastpath.out2in.x, #x,                \
348                            "/nat44-ei/out2in/fastpath/" #x);                  \
349   nat_init_simple_counter (nm->counters.slowpath.in2out.x, #x,                \
350                            "/nat44-ei/in2out/slowpath/" #x);                  \
351   nat_init_simple_counter (nm->counters.slowpath.out2in.x, #x,                \
352                            "/nat44-ei/out2in/slowpath/" #x);
353   foreach_nat_counter;
354 #undef _
355   nat_init_simple_counter (nm->counters.hairpinning, "hairpinning",
356                            "/nat44-ei/hairpinning");
357
358   p = hash_get_mem (tm->thread_registrations_by_name, "workers");
359   if (p)
360     {
361       tr = (vlib_thread_registration_t *) p[0];
362       if (tr)
363         {
364           nm->num_workers = tr->count;
365           nm->first_worker_index = tr->first_index;
366         }
367     }
368   num_threads = tm->n_vlib_mains - 1;
369   nm->port_per_thread = 0xffff - 1024;
370   vec_validate (nm->per_thread_data, num_threads);
371
372   /* Use all available workers by default */
373   if (nm->num_workers > 1)
374     {
375
376       for (i = 0; i < nm->num_workers; i++)
377         bitmap = clib_bitmap_set (bitmap, i, 1);
378       nat44_ei_set_workers (bitmap);
379       clib_bitmap_free (bitmap);
380     }
381   else
382     nm->per_thread_data[0].snat_thread_index = 0;
383
384   /* callbacks to call when interface address changes. */
385   cbi.function = nat44_ei_ip4_add_del_interface_address_cb;
386   vec_add1 (nm->ip4_main->add_del_interface_address_callbacks, cbi);
387   cbi.function = nat44_ei_ip4_add_del_addr_only_sm_cb;
388   vec_add1 (nm->ip4_main->add_del_interface_address_callbacks, cbi);
389
390   /* callbacks to call when interface to table biding changes */
391   cbt.function = nat44_ei_update_outside_fib;
392   vec_add1 (nm->ip4_main->table_bind_callbacks, cbt);
393
394   nm->fib_src_low = fib_source_allocate (
395     "nat44-ei-low", FIB_SOURCE_PRIORITY_LOW, FIB_SOURCE_BH_SIMPLE);
396   nm->fib_src_hi = fib_source_allocate ("nat44-ei-hi", FIB_SOURCE_PRIORITY_HI,
397                                         FIB_SOURCE_BH_SIMPLE);
398
399   // used only by out2in-dpo feature
400   nat_dpo_module_init ();
401   nat_ha_init (vm, nm->num_workers, num_threads);
402
403   nm->hairpinning_fq_index =
404     vlib_frame_queue_main_init (nat44_ei_hairpinning_node.index, 0);
405   nm->hairpin_dst_fq_index =
406     vlib_frame_queue_main_init (nat44_ei_hairpin_dst_node.index, 0);
407   nm->in2out_hairpinning_finish_ip4_lookup_node_fq_index =
408     vlib_frame_queue_main_init (
409       nat44_ei_in2out_hairpinning_finish_ip4_lookup_node.index, 0);
410   nm->in2out_hairpinning_finish_interface_output_node_fq_index =
411     vlib_frame_queue_main_init (
412       nat44_ei_in2out_hairpinning_finish_interface_output_node.index, 0);
413   return nat44_ei_api_hookup (vm);
414 }
415
416 VLIB_INIT_FUNCTION (nat44_ei_init);
417
418 int
419 nat44_ei_plugin_enable (nat44_ei_config_t c)
420 {
421   nat44_ei_main_t *nm = &nat44_ei_main;
422
423   fail_if_enabled ();
424
425   if (!c.users)
426     c.users = 1024;
427
428   if (!c.sessions)
429     c.sessions = 10 * 1024;
430
431   nm->rconfig = c;
432
433   if (!nm->frame_queue_nelts)
434     nm->frame_queue_nelts = NAT_FQ_NELTS_DEFAULT;
435
436   nm->translations = c.sessions;
437   nm->translation_buckets = nat_calc_bihash_buckets (c.sessions);
438   nm->user_buckets = nat_calc_bihash_buckets (c.users);
439
440   nm->pat = (!c.static_mapping_only ||
441              (c.static_mapping_only && c.connection_tracking));
442
443   nm->static_mapping_only = c.static_mapping_only;
444   nm->static_mapping_connection_tracking = c.connection_tracking;
445   nm->out2in_dpo = c.out2in_dpo;
446   nm->forwarding_enabled = 0;
447   nm->mss_clamping = 0;
448
449   nm->max_users_per_thread = c.users;
450   nm->max_translations_per_thread = c.sessions;
451   nm->max_translations_per_user =
452     c.user_sessions ? c.user_sessions : nm->max_translations_per_thread;
453
454   nm->inside_vrf_id = c.inside_vrf;
455   nm->inside_fib_index = fib_table_find_or_create_and_lock (
456     FIB_PROTOCOL_IP4, c.inside_vrf, nm->fib_src_hi);
457
458   nm->outside_vrf_id = c.outside_vrf;
459   nm->outside_fib_index = fib_table_find_or_create_and_lock (
460     FIB_PROTOCOL_IP4, c.outside_vrf, nm->fib_src_hi);
461
462   nat_reset_timeouts (&nm->timeouts);
463   nat44_ei_db_init (nm->translations, nm->translation_buckets,
464                     nm->user_buckets);
465   nat44_ei_set_alloc_default ();
466
467   // TODO: zero simple counter for all counters missing
468
469   vlib_zero_simple_counter (&nm->total_users, 0);
470   vlib_zero_simple_counter (&nm->total_sessions, 0);
471   vlib_zero_simple_counter (&nm->user_limit_reached, 0);
472
473   nat_ha_enable ();
474   nm->enabled = 1;
475
476   return 0;
477 }
478
479 void
480 nat44_ei_addresses_free (nat44_ei_address_t **addresses)
481 {
482   nat44_ei_address_t *ap;
483   vec_foreach (ap, *addresses)
484     {
485 #define _(N, i, n, s) vec_free (ap->busy_##n##_ports_per_thread);
486       foreach_nat_protocol
487 #undef _
488     }
489   vec_free (*addresses);
490   *addresses = 0;
491 }
492
493 int
494 nat44_ei_interface_add_del (u32 sw_if_index, u8 is_inside, int is_del)
495 {
496   const char *feature_name, *del_feature_name;
497   nat44_ei_main_t *nm = &nat44_ei_main;
498   nat44_ei_interface_t *i;
499   nat44_ei_address_t *ap;
500   nat44_ei_static_mapping_t *m;
501   nat44_ei_outside_fib_t *outside_fib;
502   u32 fib_index =
503     fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4, sw_if_index);
504
505   fail_if_disabled ();
506
507   if (nm->out2in_dpo && !is_inside)
508     {
509       nat44_ei_log_err ("error unsupported");
510       return VNET_API_ERROR_UNSUPPORTED;
511     }
512
513   pool_foreach (i, nm->output_feature_interfaces)
514     {
515       if (i->sw_if_index == sw_if_index)
516         {
517           nat44_ei_log_err ("error interface already configured");
518           return VNET_API_ERROR_VALUE_EXIST;
519         }
520     }
521
522   if (nm->static_mapping_only && !(nm->static_mapping_connection_tracking))
523     feature_name = is_inside ? "nat44-ei-in2out-fast" : "nat44-ei-out2in-fast";
524   else
525     {
526       if (nm->num_workers > 1)
527         feature_name = is_inside ? "nat44-ei-in2out-worker-handoff" :
528                                    "nat44-ei-out2in-worker-handoff";
529       else
530         feature_name = is_inside ? "nat44-ei-in2out" : "nat44-ei-out2in";
531     }
532
533   if (nm->fq_in2out_index == ~0 && nm->num_workers > 1)
534     nm->fq_in2out_index = vlib_frame_queue_main_init (nm->in2out_node_index,
535                                                       nm->frame_queue_nelts);
536
537   if (nm->fq_out2in_index == ~0 && nm->num_workers > 1)
538     nm->fq_out2in_index = vlib_frame_queue_main_init (nm->out2in_node_index,
539                                                       nm->frame_queue_nelts);
540
541   if (!is_inside)
542     {
543       vec_foreach (outside_fib, nm->outside_fibs)
544         {
545           if (outside_fib->fib_index == fib_index)
546             {
547               if (is_del)
548                 {
549                   outside_fib->refcount--;
550                   if (!outside_fib->refcount)
551                     vec_del1 (nm->outside_fibs,
552                               outside_fib - nm->outside_fibs);
553                 }
554               else
555                 outside_fib->refcount++;
556               goto feature_set;
557             }
558         }
559       if (!is_del)
560         {
561           vec_add2 (nm->outside_fibs, outside_fib, 1);
562           outside_fib->refcount = 1;
563           outside_fib->fib_index = fib_index;
564         }
565     }
566
567 feature_set:
568   pool_foreach (i, nm->interfaces)
569     {
570       if (i->sw_if_index == sw_if_index)
571         {
572           if (is_del)
573             {
574               if (nat44_ei_interface_is_inside (i) &&
575                   nat44_ei_interface_is_outside (i))
576                 {
577                   if (is_inside)
578                     i->flags &= ~NAT44_EI_INTERFACE_FLAG_IS_INSIDE;
579                   else
580                     i->flags &= ~NAT44_EI_INTERFACE_FLAG_IS_OUTSIDE;
581
582                   if (nm->num_workers > 1)
583                     {
584                       del_feature_name = "nat44-ei-handoff-classify";
585                       clib_warning (
586                         "del_feature_name = nat44-ei-handoff-classify");
587                       feature_name = !is_inside ?
588                                        "nat44-ei-in2out-worker-handoff" :
589                                        "nat44-ei-out2in-worker-handoff";
590                     }
591                   else
592                     {
593                       del_feature_name = "nat44-ei-classify";
594                       clib_warning ("del_feature_name = nat44-ei-classify");
595                       feature_name =
596                         !is_inside ? "nat44-ei-in2out" : "nat44-ei-out2in";
597                     }
598
599                   int rv =
600                     ip4_sv_reass_enable_disable_with_refcnt (sw_if_index, 0);
601                   if (rv)
602                     return rv;
603                   rv = vnet_feature_enable_disable (
604                     "ip4-unicast", del_feature_name, sw_if_index, 0, 0, 0);
605                   if (rv)
606                     return rv;
607                   rv = vnet_feature_enable_disable (
608                     "ip4-unicast", feature_name, sw_if_index, 1, 0, 0);
609                   if (rv)
610                     return rv;
611                   if (!is_inside)
612                     {
613                       rv = vnet_feature_enable_disable ("ip4-local",
614                                                         "nat44-ei-hairpinning",
615                                                         sw_if_index, 1, 0, 0);
616                       if (rv)
617                         return rv;
618                     }
619                 }
620               else
621                 {
622                   int rv =
623                     ip4_sv_reass_enable_disable_with_refcnt (sw_if_index, 0);
624                   if (rv)
625                     return rv;
626                   rv = vnet_feature_enable_disable (
627                     "ip4-unicast", feature_name, sw_if_index, 0, 0, 0);
628                   if (rv)
629                     return rv;
630                   pool_put (nm->interfaces, i);
631                   if (is_inside)
632                     {
633                       rv = vnet_feature_enable_disable ("ip4-local",
634                                                         "nat44-ei-hairpinning",
635                                                         sw_if_index, 0, 0, 0);
636                       if (rv)
637                         return rv;
638                     }
639                 }
640             }
641           else
642             {
643               if ((nat44_ei_interface_is_inside (i) && is_inside) ||
644                   (nat44_ei_interface_is_outside (i) && !is_inside))
645                 return 0;
646
647               if (nm->num_workers > 1)
648                 {
649                   del_feature_name = !is_inside ?
650                                        "nat44-ei-in2out-worker-handoff" :
651                                        "nat44-ei-out2in-worker-handoff";
652                   feature_name = "nat44-ei-handoff-classify";
653                   clib_warning ("feature_name = nat44-ei-handoff-classify");
654                 }
655               else
656                 {
657                   del_feature_name =
658                     !is_inside ? "nat44-ei-in2out" : "nat44-ei-out2in";
659                   feature_name = "nat44-ei-classify";
660                   clib_warning ("feature_name = nat44-ei-classify");
661                 }
662
663               int rv =
664                 ip4_sv_reass_enable_disable_with_refcnt (sw_if_index, 1);
665               if (rv)
666                 return rv;
667               rv = vnet_feature_enable_disable (
668                 "ip4-unicast", del_feature_name, sw_if_index, 0, 0, 0);
669               if (rv)
670                 return rv;
671               rv = vnet_feature_enable_disable ("ip4-unicast", feature_name,
672                                                 sw_if_index, 1, 0, 0);
673               if (rv)
674                 return rv;
675               if (!is_inside)
676                 {
677                   rv = vnet_feature_enable_disable (
678                     "ip4-local", "nat44-ei-hairpinning", sw_if_index, 0, 0, 0);
679                   if (rv)
680                     return rv;
681                 }
682               goto set_flags;
683             }
684
685           goto fib;
686         }
687     }
688
689   if (is_del)
690     {
691       nat44_ei_log_err ("error interface couldn't be found");
692       return VNET_API_ERROR_NO_SUCH_ENTRY;
693     }
694
695   pool_get (nm->interfaces, i);
696   i->sw_if_index = sw_if_index;
697   i->flags = 0;
698   nat_validate_interface_counters (nm, sw_if_index);
699
700   int rv = vnet_feature_enable_disable ("ip4-unicast", feature_name,
701                                         sw_if_index, 1, 0, 0);
702   if (rv)
703     return rv;
704
705   rv = ip4_sv_reass_enable_disable_with_refcnt (sw_if_index, 1);
706   if (rv)
707     return rv;
708
709   if (is_inside && !nm->out2in_dpo)
710     {
711       rv = vnet_feature_enable_disable ("ip4-local", "nat44-ei-hairpinning",
712                                         sw_if_index, 1, 0, 0);
713       if (rv)
714         return rv;
715     }
716
717 set_flags:
718   if (is_inside)
719     {
720       i->flags |= NAT44_EI_INTERFACE_FLAG_IS_INSIDE;
721       return 0;
722     }
723   else
724     i->flags |= NAT44_EI_INTERFACE_FLAG_IS_OUTSIDE;
725
726   /* Add/delete external addresses to FIB */
727 fib:
728   vec_foreach (ap, nm->addresses)
729     nat44_ei_add_del_addr_to_fib (&ap->addr, 32, sw_if_index, !is_del);
730
731   pool_foreach (m, nm->static_mappings)
732     {
733       if (!(nat44_ei_is_addr_only_static_mapping (m)) ||
734           (m->local_addr.as_u32 == m->external_addr.as_u32))
735         continue;
736
737       nat44_ei_add_del_addr_to_fib (&m->external_addr, 32, sw_if_index,
738                                     !is_del);
739     }
740
741   return 0;
742 }
743
744 int
745 nat44_ei_interface_add_del_output_feature (u32 sw_if_index, u8 is_inside,
746                                            int is_del)
747 {
748   nat44_ei_main_t *nm = &nat44_ei_main;
749   nat44_ei_interface_t *i;
750   nat44_ei_address_t *ap;
751   nat44_ei_static_mapping_t *m;
752   nat44_ei_outside_fib_t *outside_fib;
753   u32 fib_index =
754     fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4, sw_if_index);
755
756   fail_if_disabled ();
757
758   if (nm->static_mapping_only && !(nm->static_mapping_connection_tracking))
759     {
760       nat44_ei_log_err ("error unsupported");
761       return VNET_API_ERROR_UNSUPPORTED;
762     }
763
764   pool_foreach (i, nm->interfaces)
765     {
766       if (i->sw_if_index == sw_if_index)
767         {
768           nat44_ei_log_err ("error interface already configured");
769           return VNET_API_ERROR_VALUE_EXIST;
770         }
771     }
772
773   if (!is_inside)
774     {
775       vec_foreach (outside_fib, nm->outside_fibs)
776         {
777           if (outside_fib->fib_index == fib_index)
778             {
779               if (is_del)
780                 {
781                   outside_fib->refcount--;
782                   if (!outside_fib->refcount)
783                     vec_del1 (nm->outside_fibs,
784                               outside_fib - nm->outside_fibs);
785                 }
786               else
787                 outside_fib->refcount++;
788               goto feature_set;
789             }
790         }
791       if (!is_del)
792         {
793           vec_add2 (nm->outside_fibs, outside_fib, 1);
794           outside_fib->refcount = 1;
795           outside_fib->fib_index = fib_index;
796         }
797     }
798
799 feature_set:
800   if (is_inside)
801     {
802       int rv = ip4_sv_reass_enable_disable_with_refcnt (sw_if_index, !is_del);
803       if (rv)
804         return rv;
805       rv =
806         ip4_sv_reass_output_enable_disable_with_refcnt (sw_if_index, !is_del);
807       if (rv)
808         return rv;
809       rv = vnet_feature_enable_disable ("ip4-unicast", "nat44-ei-hairpin-dst",
810                                         sw_if_index, !is_del, 0, 0);
811       if (rv)
812         return rv;
813       rv = vnet_feature_enable_disable ("ip4-output", "nat44-ei-hairpin-src",
814                                         sw_if_index, !is_del, 0, 0);
815       if (rv)
816         return rv;
817       goto fq;
818     }
819
820   if (nm->num_workers > 1)
821     {
822       int rv = ip4_sv_reass_enable_disable_with_refcnt (sw_if_index, !is_del);
823       if (rv)
824         return rv;
825       rv =
826         ip4_sv_reass_output_enable_disable_with_refcnt (sw_if_index, !is_del);
827       if (rv)
828         return rv;
829       rv = vnet_feature_enable_disable ("ip4-unicast",
830                                         "nat44-ei-out2in-worker-handoff",
831                                         sw_if_index, !is_del, 0, 0);
832       if (rv)
833         return rv;
834       rv = vnet_feature_enable_disable (
835         "ip4-output", "nat44-ei-in2out-output-worker-handoff", sw_if_index,
836         !is_del, 0, 0);
837       if (rv)
838         return rv;
839     }
840   else
841     {
842       int rv = ip4_sv_reass_enable_disable_with_refcnt (sw_if_index, !is_del);
843       if (rv)
844         return rv;
845       rv =
846         ip4_sv_reass_output_enable_disable_with_refcnt (sw_if_index, !is_del);
847       if (rv)
848         return rv;
849       rv = vnet_feature_enable_disable ("ip4-unicast", "nat44-ei-out2in",
850                                         sw_if_index, !is_del, 0, 0);
851       if (rv)
852         return rv;
853       rv = vnet_feature_enable_disable ("ip4-output", "nat44-ei-in2out-output",
854                                         sw_if_index, !is_del, 0, 0);
855       if (rv)
856         return rv;
857     }
858
859 fq:
860   if (nm->fq_in2out_output_index == ~0 && nm->num_workers > 1)
861     nm->fq_in2out_output_index =
862       vlib_frame_queue_main_init (nm->in2out_output_node_index, 0);
863
864   if (nm->fq_out2in_index == ~0 && nm->num_workers > 1)
865     nm->fq_out2in_index =
866       vlib_frame_queue_main_init (nm->out2in_node_index, 0);
867
868   pool_foreach (i, nm->output_feature_interfaces)
869     {
870       if (i->sw_if_index == sw_if_index)
871         {
872           if (is_del)
873             pool_put (nm->output_feature_interfaces, i);
874           else
875             return VNET_API_ERROR_VALUE_EXIST;
876
877           goto fib;
878         }
879     }
880
881   if (is_del)
882     {
883       nat44_ei_log_err ("error interface couldn't be found");
884       return VNET_API_ERROR_NO_SUCH_ENTRY;
885     }
886
887   pool_get (nm->output_feature_interfaces, i);
888   i->sw_if_index = sw_if_index;
889   i->flags = 0;
890   nat_validate_interface_counters (nm, sw_if_index);
891   if (is_inside)
892     i->flags |= NAT44_EI_INTERFACE_FLAG_IS_INSIDE;
893   else
894     i->flags |= NAT44_EI_INTERFACE_FLAG_IS_OUTSIDE;
895
896   /* Add/delete external addresses to FIB */
897 fib:
898   if (is_inside)
899     return 0;
900
901   vec_foreach (ap, nm->addresses)
902     nat44_ei_add_del_addr_to_fib (&ap->addr, 32, sw_if_index, !is_del);
903
904   pool_foreach (m, nm->static_mappings)
905     {
906       if (!((nat44_ei_is_addr_only_static_mapping (m))) ||
907           (m->local_addr.as_u32 == m->external_addr.as_u32))
908         continue;
909
910       nat44_ei_add_del_addr_to_fib (&m->external_addr, 32, sw_if_index,
911                                     !is_del);
912     }
913
914   return 0;
915 }
916
917 int
918 nat44_ei_plugin_disable ()
919 {
920   nat44_ei_main_t *nm = &nat44_ei_main;
921   nat44_ei_interface_t *i, *vec;
922   int error = 0;
923
924   // first unregister all nodes from interfaces
925   vec = vec_dup (nm->interfaces);
926   vec_foreach (i, vec)
927     {
928       if (nat44_ei_interface_is_inside (i))
929         error = nat44_ei_interface_add_del (i->sw_if_index, 1, 1);
930       if (nat44_ei_interface_is_outside (i))
931         error = nat44_ei_interface_add_del (i->sw_if_index, 0, 1);
932
933       if (error)
934         {
935           nat44_ei_log_err ("error occurred while removing interface %u",
936                             i->sw_if_index);
937         }
938     }
939   vec_free (vec);
940   nm->interfaces = 0;
941
942   vec = vec_dup (nm->output_feature_interfaces);
943   vec_foreach (i, vec)
944     {
945       if (nat44_ei_interface_is_inside (i))
946         error =
947           nat44_ei_interface_add_del_output_feature (i->sw_if_index, 1, 1);
948       if (nat44_ei_interface_is_outside (i))
949         error =
950           nat44_ei_interface_add_del_output_feature (i->sw_if_index, 0, 1);
951
952       if (error)
953         {
954           nat44_ei_log_err ("error occurred while removing interface %u",
955                             i->sw_if_index);
956         }
957     }
958   vec_free (vec);
959   nm->output_feature_interfaces = 0;
960
961   nat_ha_disable ();
962   nat44_ei_db_free ();
963
964   nat44_ei_addresses_free (&nm->addresses);
965
966   vec_free (nm->to_resolve);
967   vec_free (nm->auto_add_sw_if_indices);
968
969   nm->to_resolve = 0;
970   nm->auto_add_sw_if_indices = 0;
971
972   nm->forwarding_enabled = 0;
973
974   nm->enabled = 0;
975   clib_memset (&nm->rconfig, 0, sizeof (nm->rconfig));
976
977   return error;
978 }
979
980 int
981 nat44_ei_set_outside_address_and_port (nat44_ei_address_t *addresses,
982                                        u32 thread_index, ip4_address_t addr,
983                                        u16 port, nat_protocol_t protocol)
984 {
985   nat44_ei_main_t *nm = &nat44_ei_main;
986   nat44_ei_address_t *a = 0;
987   u32 address_index;
988   u16 port_host_byte_order = clib_net_to_host_u16 (port);
989
990   for (address_index = 0; address_index < vec_len (addresses); address_index++)
991     {
992       if (addresses[address_index].addr.as_u32 != addr.as_u32)
993         continue;
994
995       a = addresses + address_index;
996       switch (protocol)
997         {
998 #define _(N, j, n, s)                                                         \
999   case NAT_PROTOCOL_##N:                                                      \
1000     if (a->busy_##n##_port_refcounts[port_host_byte_order])                   \
1001       return VNET_API_ERROR_INSTANCE_IN_USE;                                  \
1002     ++a->busy_##n##_port_refcounts[port_host_byte_order];                     \
1003     a->busy_##n##_ports_per_thread[thread_index]++;                           \
1004     a->busy_##n##_ports++;                                                    \
1005     return 0;
1006           foreach_nat_protocol
1007 #undef _
1008             default : nat_elog_info (nm, "unknown protocol");
1009           return 1;
1010         }
1011     }
1012
1013   return VNET_API_ERROR_NO_SUCH_ENTRY;
1014 }
1015
1016 void
1017 nat44_ei_add_del_address_dpo (ip4_address_t addr, u8 is_add)
1018 {
1019   nat44_ei_main_t *nm = &nat44_ei_main;
1020   dpo_id_t dpo_v4 = DPO_INVALID;
1021   fib_prefix_t pfx = {
1022     .fp_proto = FIB_PROTOCOL_IP4,
1023     .fp_len = 32,
1024     .fp_addr.ip4.as_u32 = addr.as_u32,
1025   };
1026
1027   if (is_add)
1028     {
1029       nat_dpo_create (DPO_PROTO_IP4, 0, &dpo_v4);
1030       fib_table_entry_special_dpo_add (0, &pfx, nm->fib_src_hi,
1031                                        FIB_ENTRY_FLAG_EXCLUSIVE, &dpo_v4);
1032       dpo_reset (&dpo_v4);
1033     }
1034   else
1035     {
1036       fib_table_entry_special_remove (0, &pfx, nm->fib_src_hi);
1037     }
1038 }
1039
1040 void
1041 nat44_ei_free_outside_address_and_port (nat44_ei_address_t *addresses,
1042                                         u32 thread_index, ip4_address_t *addr,
1043                                         u16 port, nat_protocol_t protocol)
1044 {
1045   nat44_ei_main_t *nm = &nat44_ei_main;
1046   nat44_ei_address_t *a;
1047   u32 address_index;
1048   u16 port_host_byte_order = clib_net_to_host_u16 (port);
1049
1050   for (address_index = 0; address_index < vec_len (addresses); address_index++)
1051     {
1052       if (addresses[address_index].addr.as_u32 == addr->as_u32)
1053         break;
1054     }
1055
1056   ASSERT (address_index < vec_len (addresses));
1057
1058   a = addresses + address_index;
1059
1060   switch (protocol)
1061     {
1062 #define _(N, i, n, s)                                                         \
1063   case NAT_PROTOCOL_##N:                                                      \
1064     ASSERT (a->busy_##n##_port_refcounts[port_host_byte_order] >= 1);         \
1065     --a->busy_##n##_port_refcounts[port_host_byte_order];                     \
1066     a->busy_##n##_ports--;                                                    \
1067     a->busy_##n##_ports_per_thread[thread_index]--;                           \
1068     break;
1069       foreach_nat_protocol
1070 #undef _
1071         default : nat_elog_info (nm, "unknown protocol");
1072       return;
1073     }
1074 }
1075
1076 void
1077 nat44_ei_free_session_data_v2 (nat44_ei_main_t *nm, nat44_ei_session_t *s,
1078                                u32 thread_index, u8 is_ha)
1079 {
1080   clib_bihash_kv_8_8_t kv;
1081
1082   /* session lookup tables */
1083   init_nat_i2o_k (&kv, s);
1084   if (clib_bihash_add_del_8_8 (&nm->in2out, &kv, 0))
1085     nat_elog_warn (nm, "in2out key del failed");
1086   init_nat_o2i_k (&kv, s);
1087   if (clib_bihash_add_del_8_8 (&nm->out2in, &kv, 0))
1088     nat_elog_warn (nm, "out2in key del failed");
1089
1090   if (!is_ha)
1091     nat_syslog_nat44_apmdel (s->user_index, s->in2out.fib_index,
1092                              &s->in2out.addr, s->in2out.port, &s->out2in.addr,
1093                              s->out2in.port, s->nat_proto);
1094
1095   if (nat44_ei_is_unk_proto_session (s))
1096     return;
1097
1098   if (!is_ha)
1099     {
1100       /* log NAT event */
1101       nat_ipfix_logging_nat44_ses_delete (
1102         thread_index, s->in2out.addr.as_u32, s->out2in.addr.as_u32,
1103         s->nat_proto, s->in2out.port, s->out2in.port, s->in2out.fib_index);
1104
1105       nat_ha_sdel (&s->out2in.addr, s->out2in.port, &s->ext_host_addr,
1106                    s->ext_host_port, s->nat_proto, s->out2in.fib_index,
1107                    thread_index);
1108     }
1109
1110   if (nat44_ei_is_session_static (s))
1111     return;
1112
1113   nat44_ei_free_outside_address_and_port (nm->addresses, thread_index,
1114                                           &s->out2in.addr, s->out2in.port,
1115                                           s->nat_proto);
1116 }
1117
1118 nat44_ei_user_t *
1119 nat44_ei_user_get_or_create (nat44_ei_main_t *nm, ip4_address_t *addr,
1120                              u32 fib_index, u32 thread_index)
1121 {
1122   nat44_ei_user_t *u = 0;
1123   nat44_ei_user_key_t user_key;
1124   clib_bihash_kv_8_8_t kv, value;
1125   nat44_ei_main_per_thread_data_t *tnm = &nm->per_thread_data[thread_index];
1126   dlist_elt_t *per_user_list_head_elt;
1127
1128   user_key.addr.as_u32 = addr->as_u32;
1129   user_key.fib_index = fib_index;
1130   kv.key = user_key.as_u64;
1131
1132   /* Ever heard of the "user" = src ip4 address before? */
1133   if (clib_bihash_search_8_8 (&tnm->user_hash, &kv, &value))
1134     {
1135       if (pool_elts (tnm->users) >= nm->max_users_per_thread)
1136         {
1137           vlib_increment_simple_counter (&nm->user_limit_reached, thread_index,
1138                                          0, 1);
1139           nat_elog_warn (nm, "maximum user limit reached");
1140           return NULL;
1141         }
1142       /* no, make a new one */
1143       pool_get (tnm->users, u);
1144       clib_memset (u, 0, sizeof (*u));
1145
1146       u->addr.as_u32 = addr->as_u32;
1147       u->fib_index = fib_index;
1148
1149       pool_get (tnm->list_pool, per_user_list_head_elt);
1150
1151       u->sessions_per_user_list_head_index =
1152         per_user_list_head_elt - tnm->list_pool;
1153
1154       clib_dlist_init (tnm->list_pool, u->sessions_per_user_list_head_index);
1155
1156       kv.value = u - tnm->users;
1157
1158       /* add user */
1159       if (clib_bihash_add_del_8_8 (&tnm->user_hash, &kv, 1))
1160         {
1161           nat_elog_warn (nm, "user_hash key add failed");
1162           nat44_ei_delete_user_with_no_session (nm, u, thread_index);
1163           return NULL;
1164         }
1165
1166       vlib_set_simple_counter (&nm->total_users, thread_index, 0,
1167                                pool_elts (tnm->users));
1168     }
1169   else
1170     {
1171       u = pool_elt_at_index (tnm->users, value.value);
1172     }
1173
1174   return u;
1175 }
1176
1177 nat44_ei_session_t *
1178 nat44_ei_session_alloc_or_recycle (nat44_ei_main_t *nm, nat44_ei_user_t *u,
1179                                    u32 thread_index, f64 now)
1180 {
1181   nat44_ei_session_t *s;
1182   nat44_ei_main_per_thread_data_t *tnm = &nm->per_thread_data[thread_index];
1183   u32 oldest_per_user_translation_list_index, session_index;
1184   dlist_elt_t *oldest_per_user_translation_list_elt;
1185   dlist_elt_t *per_user_translation_list_elt;
1186
1187   /* Over quota? Recycle the least recently used translation */
1188   if ((u->nsessions + u->nstaticsessions) >= nm->max_translations_per_user)
1189     {
1190       oldest_per_user_translation_list_index = clib_dlist_remove_head (
1191         tnm->list_pool, u->sessions_per_user_list_head_index);
1192
1193       ASSERT (oldest_per_user_translation_list_index != ~0);
1194
1195       /* Add it back to the end of the LRU list */
1196       clib_dlist_addtail (tnm->list_pool, u->sessions_per_user_list_head_index,
1197                           oldest_per_user_translation_list_index);
1198       /* Get the list element */
1199       oldest_per_user_translation_list_elt = pool_elt_at_index (
1200         tnm->list_pool, oldest_per_user_translation_list_index);
1201
1202       /* Get the session index from the list element */
1203       session_index = oldest_per_user_translation_list_elt->value;
1204
1205       /* Get the session */
1206       s = pool_elt_at_index (tnm->sessions, session_index);
1207
1208       nat44_ei_free_session_data_v2 (nm, s, thread_index, 0);
1209       if (nat44_ei_is_session_static (s))
1210         u->nstaticsessions--;
1211       else
1212         u->nsessions--;
1213       s->flags = 0;
1214       s->total_bytes = 0;
1215       s->total_pkts = 0;
1216       s->state = 0;
1217       s->ext_host_addr.as_u32 = 0;
1218       s->ext_host_port = 0;
1219       s->ext_host_nat_addr.as_u32 = 0;
1220       s->ext_host_nat_port = 0;
1221     }
1222   else
1223     {
1224       pool_get (tnm->sessions, s);
1225       clib_memset (s, 0, sizeof (*s));
1226
1227       /* Create list elts */
1228       pool_get (tnm->list_pool, per_user_translation_list_elt);
1229       clib_dlist_init (tnm->list_pool,
1230                        per_user_translation_list_elt - tnm->list_pool);
1231
1232       per_user_translation_list_elt->value = s - tnm->sessions;
1233       s->per_user_index = per_user_translation_list_elt - tnm->list_pool;
1234       s->per_user_list_head_index = u->sessions_per_user_list_head_index;
1235
1236       clib_dlist_addtail (tnm->list_pool, s->per_user_list_head_index,
1237                           per_user_translation_list_elt - tnm->list_pool);
1238
1239       s->user_index = u - tnm->users;
1240       vlib_set_simple_counter (&nm->total_sessions, thread_index, 0,
1241                                pool_elts (tnm->sessions));
1242     }
1243
1244   s->ha_last_refreshed = now;
1245
1246   return s;
1247 }
1248
1249 void
1250 nat44_ei_free_session_data (nat44_ei_main_t *nm, nat44_ei_session_t *s,
1251                             u32 thread_index, u8 is_ha)
1252 {
1253   clib_bihash_kv_8_8_t kv;
1254
1255   init_nat_i2o_k (&kv, s);
1256   if (clib_bihash_add_del_8_8 (&nm->in2out, &kv, 0))
1257     nat_elog_warn (nm, "in2out key del failed");
1258
1259   init_nat_o2i_k (&kv, s);
1260   if (clib_bihash_add_del_8_8 (&nm->out2in, &kv, 0))
1261     nat_elog_warn (nm, "out2in key del failed");
1262
1263   if (!is_ha)
1264     {
1265       nat_syslog_nat44_apmdel (s->user_index, s->in2out.fib_index,
1266                                &s->in2out.addr, s->in2out.port,
1267                                &s->out2in.addr, s->out2in.port, s->nat_proto);
1268
1269       nat_ipfix_logging_nat44_ses_delete (
1270         thread_index, s->in2out.addr.as_u32, s->out2in.addr.as_u32,
1271         s->nat_proto, s->in2out.port, s->out2in.port, s->in2out.fib_index);
1272
1273       nat_ha_sdel (&s->out2in.addr, s->out2in.port, &s->ext_host_addr,
1274                    s->ext_host_port, s->nat_proto, s->out2in.fib_index,
1275                    thread_index);
1276     }
1277
1278   if (nat44_ei_is_session_static (s))
1279     return;
1280
1281   nat44_ei_free_outside_address_and_port (nm->addresses, thread_index,
1282                                           &s->out2in.addr, s->out2in.port,
1283                                           s->nat_proto);
1284 }
1285
1286 static_always_inline void
1287 nat44_ei_user_del_sessions (nat44_ei_user_t *u, u32 thread_index)
1288 {
1289   dlist_elt_t *elt;
1290   nat44_ei_session_t *s;
1291
1292   nat44_ei_main_t *nm = &nat44_ei_main;
1293   nat44_ei_main_per_thread_data_t *tnm = &nm->per_thread_data[thread_index];
1294
1295   // get head
1296   elt =
1297     pool_elt_at_index (tnm->list_pool, u->sessions_per_user_list_head_index);
1298   // get first element
1299   elt = pool_elt_at_index (tnm->list_pool, elt->next);
1300
1301   while (elt->value != ~0)
1302     {
1303       s = pool_elt_at_index (tnm->sessions, elt->value);
1304       elt = pool_elt_at_index (tnm->list_pool, elt->next);
1305
1306       nat44_ei_free_session_data (nm, s, thread_index, 0);
1307       nat44_ei_delete_session (nm, s, thread_index);
1308     }
1309 }
1310
1311 int
1312 nat44_ei_user_del (ip4_address_t *addr, u32 fib_index)
1313 {
1314   int rv = 1;
1315
1316   nat44_ei_main_t *nm = &nat44_ei_main;
1317   nat44_ei_main_per_thread_data_t *tnm;
1318
1319   nat44_ei_user_key_t user_key;
1320   clib_bihash_kv_8_8_t kv, value;
1321
1322   user_key.addr.as_u32 = addr->as_u32;
1323   user_key.fib_index = fib_index;
1324   kv.key = user_key.as_u64;
1325
1326   if (nm->num_workers > 1)
1327     {
1328       vec_foreach (tnm, nm->per_thread_data)
1329         {
1330           if (!clib_bihash_search_8_8 (&tnm->user_hash, &kv, &value))
1331             {
1332               nat44_ei_user_del_sessions (
1333                 pool_elt_at_index (tnm->users, value.value),
1334                 tnm->thread_index);
1335               rv = 0;
1336               break;
1337             }
1338         }
1339     }
1340   else
1341     {
1342       tnm = vec_elt_at_index (nm->per_thread_data, nm->num_workers);
1343       if (!clib_bihash_search_8_8 (&tnm->user_hash, &kv, &value))
1344         {
1345           nat44_ei_user_del_sessions (
1346             pool_elt_at_index (tnm->users, value.value), tnm->thread_index);
1347           rv = 0;
1348         }
1349     }
1350   return rv;
1351 }
1352
1353 void
1354 nat44_ei_static_mapping_del_sessions (nat44_ei_main_t *nm,
1355                                       nat44_ei_main_per_thread_data_t *tnm,
1356                                       nat44_ei_user_key_t u_key, int addr_only,
1357                                       ip4_address_t e_addr, u16 e_port)
1358 {
1359   clib_bihash_kv_8_8_t kv, value;
1360   kv.key = u_key.as_u64;
1361   u64 user_index;
1362   dlist_elt_t *head, *elt;
1363   nat44_ei_user_t *u;
1364   nat44_ei_session_t *s;
1365   u32 elt_index, head_index, ses_index;
1366
1367   if (!clib_bihash_search_8_8 (&tnm->user_hash, &kv, &value))
1368     {
1369       user_index = value.value;
1370       u = pool_elt_at_index (tnm->users, user_index);
1371       if (u->nstaticsessions)
1372         {
1373           head_index = u->sessions_per_user_list_head_index;
1374           head = pool_elt_at_index (tnm->list_pool, head_index);
1375           elt_index = head->next;
1376           elt = pool_elt_at_index (tnm->list_pool, elt_index);
1377           ses_index = elt->value;
1378           while (ses_index != ~0)
1379             {
1380               s = pool_elt_at_index (tnm->sessions, ses_index);
1381               elt = pool_elt_at_index (tnm->list_pool, elt->next);
1382               ses_index = elt->value;
1383
1384               if (!addr_only)
1385                 {
1386                   if ((s->out2in.addr.as_u32 != e_addr.as_u32) ||
1387                       (s->out2in.port != e_port))
1388                     continue;
1389                 }
1390
1391               if (!nat44_ei_is_session_static (s))
1392                 continue;
1393
1394               nat44_ei_free_session_data_v2 (nm, s, tnm - nm->per_thread_data,
1395                                              0);
1396               nat44_ei_delete_session (nm, s, tnm - nm->per_thread_data);
1397
1398               if (!addr_only)
1399                 break;
1400             }
1401         }
1402     }
1403 }
1404
1405 u32
1406 nat44_ei_get_in2out_worker_index (ip4_header_t *ip0, u32 rx_fib_index0,
1407                                   u8 is_output)
1408 {
1409   nat44_ei_main_t *nm = &nat44_ei_main;
1410   u32 next_worker_index = 0;
1411   u32 hash;
1412
1413   next_worker_index = nm->first_worker_index;
1414   hash = ip0->src_address.as_u32 + (ip0->src_address.as_u32 >> 8) +
1415          (ip0->src_address.as_u32 >> 16) + (ip0->src_address.as_u32 >> 24);
1416
1417   if (PREDICT_TRUE (is_pow2 (_vec_len (nm->workers))))
1418     next_worker_index += nm->workers[hash & (_vec_len (nm->workers) - 1)];
1419   else
1420     next_worker_index += nm->workers[hash % _vec_len (nm->workers)];
1421
1422   return next_worker_index;
1423 }
1424
1425 u32
1426 nat44_ei_get_out2in_worker_index (vlib_buffer_t *b, ip4_header_t *ip0,
1427                                   u32 rx_fib_index0, u8 is_output)
1428 {
1429   nat44_ei_main_t *nm = &nat44_ei_main;
1430   udp_header_t *udp;
1431   u16 port;
1432   clib_bihash_kv_8_8_t kv, value;
1433   nat44_ei_static_mapping_t *m;
1434   u32 proto;
1435   u32 next_worker_index = 0;
1436
1437   /* first try static mappings without port */
1438   if (PREDICT_FALSE (pool_elts (nm->static_mappings)))
1439     {
1440       init_nat_k (&kv, ip0->dst_address, 0, rx_fib_index0, 0);
1441       if (!clib_bihash_search_8_8 (&nm->static_mapping_by_external, &kv,
1442                                    &value))
1443         {
1444           m = pool_elt_at_index (nm->static_mappings, value.value);
1445           return m->workers[0];
1446         }
1447     }
1448
1449   proto = ip_proto_to_nat_proto (ip0->protocol);
1450   udp = ip4_next_header (ip0);
1451   port = vnet_buffer (b)->ip.reass.l4_dst_port;
1452
1453   /* unknown protocol */
1454   if (PREDICT_FALSE (proto == NAT_PROTOCOL_OTHER))
1455     {
1456       /* use current thread */
1457       return vlib_get_thread_index ();
1458     }
1459
1460   if (PREDICT_FALSE (ip0->protocol == IP_PROTOCOL_ICMP))
1461     {
1462       icmp46_header_t *icmp = (icmp46_header_t *) udp;
1463       icmp_echo_header_t *echo = (icmp_echo_header_t *) (icmp + 1);
1464       if (!icmp_type_is_error_message (
1465             vnet_buffer (b)->ip.reass.icmp_type_or_tcp_flags))
1466         port = vnet_buffer (b)->ip.reass.l4_src_port;
1467       else
1468         {
1469           /* if error message, then it's not fragmented and we can access it */
1470           ip4_header_t *inner_ip = (ip4_header_t *) (echo + 1);
1471           proto = ip_proto_to_nat_proto (inner_ip->protocol);
1472           void *l4_header = ip4_next_header (inner_ip);
1473           switch (proto)
1474             {
1475             case NAT_PROTOCOL_ICMP:
1476               icmp = (icmp46_header_t *) l4_header;
1477               echo = (icmp_echo_header_t *) (icmp + 1);
1478               port = echo->identifier;
1479               break;
1480             case NAT_PROTOCOL_UDP:
1481             case NAT_PROTOCOL_TCP:
1482               port = ((tcp_udp_header_t *) l4_header)->src_port;
1483               break;
1484             default:
1485               return vlib_get_thread_index ();
1486             }
1487         }
1488     }
1489
1490   /* try static mappings with port */
1491   if (PREDICT_FALSE (pool_elts (nm->static_mappings)))
1492     {
1493       init_nat_k (&kv, ip0->dst_address, port, rx_fib_index0, proto);
1494       if (!clib_bihash_search_8_8 (&nm->static_mapping_by_external, &kv,
1495                                    &value))
1496         {
1497           m = pool_elt_at_index (nm->static_mappings, value.value);
1498           return m->workers[0];
1499         }
1500     }
1501
1502   /* worker by outside port */
1503   next_worker_index = nm->first_worker_index;
1504   next_worker_index +=
1505     nm->workers[(clib_net_to_host_u16 (port) - 1024) / nm->port_per_thread];
1506   return next_worker_index;
1507 }
1508
1509 static int
1510 nat44_ei_alloc_default_cb (nat44_ei_address_t *addresses, u32 fib_index,
1511                            u32 thread_index, nat_protocol_t proto,
1512                            ip4_address_t s_addr, ip4_address_t *addr,
1513                            u16 *port, u16 port_per_thread,
1514                            u32 snat_thread_index)
1515 {
1516   nat44_ei_main_t *nm = &nat44_ei_main;
1517   nat44_ei_address_t *a, *ga = 0;
1518   u32 portnum;
1519   int i;
1520
1521   if (vec_len (addresses) > 0)
1522     {
1523
1524       int s_addr_offset = s_addr.as_u32 % vec_len (addresses);
1525
1526       for (i = s_addr_offset; i < vec_len (addresses); ++i)
1527         {
1528           a = addresses + i;
1529           switch (proto)
1530             {
1531 #define _(N, j, n, s)                                                         \
1532   case NAT_PROTOCOL_##N:                                                      \
1533     if (a->busy_##n##_ports_per_thread[thread_index] < port_per_thread)       \
1534       {                                                                       \
1535         if (a->fib_index == fib_index)                                        \
1536           {                                                                   \
1537             while (1)                                                         \
1538               {                                                               \
1539                 portnum = (port_per_thread * snat_thread_index) +             \
1540                           nat_random_port (&nm->random_seed, 0,               \
1541                                            port_per_thread - 1) +             \
1542                           1024;                                               \
1543                 if (a->busy_##n##_port_refcounts[portnum])                    \
1544                   continue;                                                   \
1545                 --a->busy_##n##_port_refcounts[portnum];                      \
1546                 a->busy_##n##_ports_per_thread[thread_index]++;               \
1547                 a->busy_##n##_ports++;                                        \
1548                 *addr = a->addr;                                              \
1549                 *port = clib_host_to_net_u16 (portnum);                       \
1550                 return 0;                                                     \
1551               }                                                               \
1552           }                                                                   \
1553         else if (a->fib_index == ~0)                                          \
1554           {                                                                   \
1555             ga = a;                                                           \
1556           }                                                                   \
1557       }                                                                       \
1558     break;
1559               foreach_nat_protocol;
1560             default:
1561               nat_elog_info (nm, "unknown protocol");
1562               return 1;
1563             }
1564         }
1565
1566       for (i = 0; i < s_addr_offset; ++i)
1567         {
1568           a = addresses + i;
1569           switch (proto)
1570             {
1571               foreach_nat_protocol;
1572             default:
1573               nat_elog_info (nm, "unknown protocol");
1574               return 1;
1575             }
1576         }
1577   if (ga)
1578     {
1579       a = ga;
1580       // fake fib index to reuse macro
1581       fib_index = ~0;
1582       switch (proto)
1583         {
1584           foreach_nat_protocol;
1585             default : nat_elog_info (nm, "unknown protocol");
1586           return 1;
1587         }
1588     }
1589     }
1590
1591 #undef _
1592
1593   /* Totally out of translations to use... */
1594   nat_ipfix_logging_addresses_exhausted (thread_index, 0);
1595   return 1;
1596 }
1597
1598 static int
1599 nat44_ei_alloc_range_cb (nat44_ei_address_t *addresses, u32 fib_index,
1600                          u32 thread_index, nat_protocol_t proto,
1601                          ip4_address_t s_addr, ip4_address_t *addr, u16 *port,
1602                          u16 port_per_thread, u32 snat_thread_index)
1603 {
1604   nat44_ei_main_t *nm = &nat44_ei_main;
1605   nat44_ei_address_t *a = addresses;
1606   u16 portnum, ports;
1607
1608   ports = nm->end_port - nm->start_port + 1;
1609
1610   if (!vec_len (addresses))
1611     goto exhausted;
1612
1613   switch (proto)
1614     {
1615 #define _(N, i, n, s)                                                         \
1616   case NAT_PROTOCOL_##N:                                                      \
1617     if (a->busy_##n##_ports < ports)                                          \
1618       {                                                                       \
1619         while (1)                                                             \
1620           {                                                                   \
1621             portnum = nat_random_port (&nm->random_seed, nm->start_port,      \
1622                                        nm->end_port);                         \
1623             if (a->busy_##n##_port_refcounts[portnum])                        \
1624               continue;                                                       \
1625             ++a->busy_##n##_port_refcounts[portnum];                          \
1626             a->busy_##n##_ports++;                                            \
1627             *addr = a->addr;                                                  \
1628             *port = clib_host_to_net_u16 (portnum);                           \
1629             return 0;                                                         \
1630           }                                                                   \
1631       }                                                                       \
1632     break;
1633       foreach_nat_protocol
1634 #undef _
1635         default : nat_elog_info (nm, "unknown protocol");
1636       return 1;
1637     }
1638
1639 exhausted:
1640   /* Totally out of translations to use... */
1641   nat_ipfix_logging_addresses_exhausted (thread_index, 0);
1642   return 1;
1643 }
1644
1645 static int
1646 nat44_ei_alloc_mape_cb (nat44_ei_address_t *addresses, u32 fib_index,
1647                         u32 thread_index, nat_protocol_t proto,
1648                         ip4_address_t s_addr, ip4_address_t *addr, u16 *port,
1649                         u16 port_per_thread, u32 snat_thread_index)
1650 {
1651   nat44_ei_main_t *nm = &nat44_ei_main;
1652   nat44_ei_address_t *a = addresses;
1653   u16 m, ports, portnum, A, j;
1654   m = 16 - (nm->psid_offset + nm->psid_length);
1655   ports = (1 << (16 - nm->psid_length)) - (1 << m);
1656
1657   if (!vec_len (addresses))
1658     goto exhausted;
1659
1660   switch (proto)
1661     {
1662 #define _(N, i, n, s)                                                         \
1663   case NAT_PROTOCOL_##N:                                                      \
1664     if (a->busy_##n##_ports < ports)                                          \
1665       {                                                                       \
1666         while (1)                                                             \
1667           {                                                                   \
1668             A = nat_random_port (&nm->random_seed, 1,                         \
1669                                  pow2_mask (nm->psid_offset));                \
1670             j = nat_random_port (&nm->random_seed, 0, pow2_mask (m));         \
1671             portnum = A | (nm->psid << nm->psid_offset) | (j << (16 - m));    \
1672             if (a->busy_##n##_port_refcounts[portnum])                        \
1673               continue;                                                       \
1674             ++a->busy_##n##_port_refcounts[portnum];                          \
1675             a->busy_##n##_ports++;                                            \
1676             *addr = a->addr;                                                  \
1677             *port = clib_host_to_net_u16 (portnum);                           \
1678             return 0;                                                         \
1679           }                                                                   \
1680       }                                                                       \
1681     break;
1682       foreach_nat_protocol
1683 #undef _
1684         default : nat_elog_info (nm, "unknown protocol");
1685       return 1;
1686     }
1687
1688 exhausted:
1689   /* Totally out of translations to use... */
1690   nat_ipfix_logging_addresses_exhausted (thread_index, 0);
1691   return 1;
1692 }
1693
1694 void
1695 nat44_ei_set_alloc_default ()
1696 {
1697   nat44_ei_main_t *nm = &nat44_ei_main;
1698
1699   nm->addr_and_port_alloc_alg = NAT44_EI_ADDR_AND_PORT_ALLOC_ALG_DEFAULT;
1700   nm->alloc_addr_and_port = nat44_ei_alloc_default_cb;
1701 }
1702
1703 void
1704 nat44_ei_set_alloc_range (u16 start_port, u16 end_port)
1705 {
1706   nat44_ei_main_t *nm = &nat44_ei_main;
1707
1708   nm->addr_and_port_alloc_alg = NAT44_EI_ADDR_AND_PORT_ALLOC_ALG_RANGE;
1709   nm->alloc_addr_and_port = nat44_ei_alloc_range_cb;
1710   nm->start_port = start_port;
1711   nm->end_port = end_port;
1712 }
1713
1714 void
1715 nat44_ei_set_alloc_mape (u16 psid, u16 psid_offset, u16 psid_length)
1716 {
1717   nat44_ei_main_t *nm = &nat44_ei_main;
1718
1719   nm->addr_and_port_alloc_alg = NAT44_EI_ADDR_AND_PORT_ALLOC_ALG_MAPE;
1720   nm->alloc_addr_and_port = nat44_ei_alloc_mape_cb;
1721   nm->psid = psid;
1722   nm->psid_offset = psid_offset;
1723   nm->psid_length = psid_length;
1724 }
1725
1726 static void
1727 nat44_ei_add_static_mapping_when_resolved (ip4_address_t l_addr, u16 l_port,
1728                                            u16 e_port, nat_protocol_t proto,
1729                                            u32 sw_if_index, u32 vrf_id,
1730                                            int addr_only, int identity_nat,
1731                                            u8 *tag)
1732 {
1733   nat44_ei_main_t *nm = &nat44_ei_main;
1734   nat44_ei_static_map_resolve_t *rp;
1735
1736   vec_add2 (nm->to_resolve, rp, 1);
1737   clib_memset (rp, 0, sizeof (*rp));
1738
1739   rp->l_addr.as_u32 = l_addr.as_u32;
1740   rp->l_port = l_port;
1741   rp->e_port = e_port;
1742   rp->sw_if_index = sw_if_index;
1743   rp->vrf_id = vrf_id;
1744   rp->proto = proto;
1745   rp->addr_only = addr_only;
1746   rp->identity_nat = identity_nat;
1747   rp->tag = vec_dup (tag);
1748 }
1749
1750 void
1751 nat44_ei_delete_session (nat44_ei_main_t *nm, nat44_ei_session_t *ses,
1752                          u32 thread_index)
1753 {
1754   nat44_ei_main_per_thread_data_t *tnm =
1755     vec_elt_at_index (nm->per_thread_data, thread_index);
1756   clib_bihash_kv_8_8_t kv, value;
1757   nat44_ei_user_t *u;
1758   const nat44_ei_user_key_t u_key = { .addr = ses->in2out.addr,
1759                                       .fib_index = ses->in2out.fib_index };
1760   const u8 u_static = nat44_ei_is_session_static (ses);
1761
1762   clib_dlist_remove (tnm->list_pool, ses->per_user_index);
1763   pool_put_index (tnm->list_pool, ses->per_user_index);
1764
1765   pool_put (tnm->sessions, ses);
1766   vlib_set_simple_counter (&nm->total_sessions, thread_index, 0,
1767                            pool_elts (tnm->sessions));
1768
1769   kv.key = u_key.as_u64;
1770   if (!clib_bihash_search_8_8 (&tnm->user_hash, &kv, &value))
1771     {
1772       u = pool_elt_at_index (tnm->users, value.value);
1773       if (u_static)
1774         u->nstaticsessions--;
1775       else
1776         u->nsessions--;
1777
1778       nat44_ei_delete_user_with_no_session (nm, u, thread_index);
1779     }
1780 }
1781
1782 int
1783 nat44_ei_del_session (nat44_ei_main_t *nm, ip4_address_t *addr, u16 port,
1784                       nat_protocol_t proto, u32 vrf_id, int is_in)
1785 {
1786   nat44_ei_main_per_thread_data_t *tnm;
1787   clib_bihash_kv_8_8_t kv, value;
1788   u32 fib_index = fib_table_find (FIB_PROTOCOL_IP4, vrf_id);
1789   nat44_ei_session_t *s;
1790   clib_bihash_8_8_t *t;
1791
1792   init_nat_k (&kv, *addr, port, fib_index, proto);
1793   t = is_in ? &nm->in2out : &nm->out2in;
1794   if (!clib_bihash_search_8_8 (t, &kv, &value))
1795     {
1796       // this is called from API/CLI, so the world is stopped here
1797       // it's safe to manipulate arbitrary per-thread data
1798       u32 thread_index = nat_value_get_thread_index (&value);
1799       tnm = vec_elt_at_index (nm->per_thread_data, thread_index);
1800       u32 session_index = nat_value_get_session_index (&value);
1801       if (pool_is_free_index (tnm->sessions, session_index))
1802         return VNET_API_ERROR_UNSPECIFIED;
1803
1804       s = pool_elt_at_index (tnm->sessions, session_index);
1805       nat44_ei_free_session_data_v2 (nm, s, tnm - nm->per_thread_data, 0);
1806       nat44_ei_delete_session (nm, s, tnm - nm->per_thread_data);
1807       return 0;
1808     }
1809
1810   return VNET_API_ERROR_NO_SUCH_ENTRY;
1811 }
1812
1813 u32
1814 nat44_ei_get_thread_idx_by_port (u16 e_port)
1815 {
1816   nat44_ei_main_t *nm = &nat44_ei_main;
1817   u32 thread_idx = nm->num_workers;
1818   if (nm->num_workers > 1)
1819     {
1820       thread_idx = nm->first_worker_index +
1821                    nm->workers[(e_port - 1024) / nm->port_per_thread];
1822     }
1823   return thread_idx;
1824 }
1825
1826 void
1827 nat44_ei_add_del_addr_to_fib (ip4_address_t *addr, u8 p_len, u32 sw_if_index,
1828                               int is_add)
1829 {
1830   nat44_ei_main_t *nm = &nat44_ei_main;
1831   fib_prefix_t prefix = {
1832     .fp_len = p_len,
1833     .fp_proto = FIB_PROTOCOL_IP4,
1834     .fp_addr = {
1835                 .ip4.as_u32 = addr->as_u32,
1836                 },
1837   };
1838   u32 fib_index = ip4_fib_table_get_index_for_sw_if_index (sw_if_index);
1839
1840   if (is_add)
1841     fib_table_entry_update_one_path (
1842       fib_index, &prefix, nm->fib_src_low,
1843       (FIB_ENTRY_FLAG_CONNECTED | FIB_ENTRY_FLAG_LOCAL |
1844        FIB_ENTRY_FLAG_EXCLUSIVE),
1845       DPO_PROTO_IP4, NULL, sw_if_index, ~0, 1, NULL, FIB_ROUTE_PATH_FLAG_NONE);
1846   else
1847     fib_table_entry_delete (fib_index, &prefix, nm->fib_src_low);
1848 }
1849
1850 int
1851 nat44_ei_add_del_static_mapping (ip4_address_t l_addr, ip4_address_t e_addr,
1852                                  u16 l_port, u16 e_port, nat_protocol_t proto,
1853                                  u32 sw_if_index, u32 vrf_id, u8 addr_only,
1854                                  u8 identity_nat, u8 *tag, u8 is_add)
1855 {
1856   nat44_ei_main_t *nm = &nat44_ei_main;
1857   nat44_ei_static_mapping_t *m = 0;
1858   clib_bihash_kv_8_8_t kv, value;
1859   nat44_ei_address_t *a = 0;
1860   u32 fib_index = ~0;
1861   nat44_ei_interface_t *interface;
1862   nat44_ei_main_per_thread_data_t *tnm;
1863   nat44_ei_user_key_t u_key;
1864   nat44_ei_user_t *u;
1865   dlist_elt_t *head, *elt;
1866   u32 elt_index, head_index;
1867   u32 ses_index;
1868   u64 user_index;
1869   nat44_ei_session_t *s;
1870   nat44_ei_static_map_resolve_t *rp, *rp_match = 0;
1871   nat44_ei_lb_addr_port_t *local;
1872   u32 find = ~0;
1873   int i;
1874
1875   if (sw_if_index != ~0)
1876     {
1877       ip4_address_t *first_int_addr;
1878
1879       for (i = 0; i < vec_len (nm->to_resolve); i++)
1880         {
1881           rp = nm->to_resolve + i;
1882           if (rp->sw_if_index != sw_if_index ||
1883               rp->l_addr.as_u32 != l_addr.as_u32 || rp->vrf_id != vrf_id ||
1884               rp->addr_only != addr_only)
1885             continue;
1886
1887           if (!addr_only)
1888             {
1889               if ((rp->l_port != l_port && rp->e_port != e_port) ||
1890                   rp->proto != proto)
1891                 continue;
1892             }
1893
1894           rp_match = rp;
1895           break;
1896         }
1897
1898       /* Might be already set... */
1899       first_int_addr = ip4_interface_first_address (
1900         nm->ip4_main, sw_if_index, 0 /* just want the address */);
1901
1902       if (is_add)
1903         {
1904           if (rp_match)
1905             return VNET_API_ERROR_VALUE_EXIST;
1906
1907           nat44_ei_add_static_mapping_when_resolved (
1908             l_addr, l_port, e_port, proto, sw_if_index, vrf_id, addr_only,
1909             identity_nat, tag);
1910
1911           /* DHCP resolution required? */
1912           if (!first_int_addr)
1913             return 0;
1914
1915           e_addr.as_u32 = first_int_addr->as_u32;
1916           /* Identity mapping? */
1917           if (l_addr.as_u32 == 0)
1918             l_addr.as_u32 = e_addr.as_u32;
1919         }
1920       else
1921         {
1922           if (!rp_match)
1923             return VNET_API_ERROR_NO_SUCH_ENTRY;
1924
1925           vec_del1 (nm->to_resolve, i);
1926
1927           if (!first_int_addr)
1928             return 0;
1929
1930           e_addr.as_u32 = first_int_addr->as_u32;
1931           /* Identity mapping? */
1932           if (l_addr.as_u32 == 0)
1933             l_addr.as_u32 = e_addr.as_u32;
1934         }
1935     }
1936
1937   init_nat_k (&kv, e_addr, addr_only ? 0 : e_port, 0, addr_only ? 0 : proto);
1938   if (!clib_bihash_search_8_8 (&nm->static_mapping_by_external, &kv, &value))
1939     m = pool_elt_at_index (nm->static_mappings, value.value);
1940
1941   if (is_add)
1942     {
1943       if (m)
1944         {
1945           // identity mapping for second vrf
1946           if (nat44_ei_is_identity_static_mapping (m))
1947             {
1948               pool_foreach (local, m->locals)
1949                 {
1950                   if (local->vrf_id == vrf_id)
1951                     return VNET_API_ERROR_VALUE_EXIST;
1952                 }
1953               pool_get (m->locals, local);
1954               local->vrf_id = vrf_id;
1955               local->fib_index = fib_table_find_or_create_and_lock (
1956                 FIB_PROTOCOL_IP4, vrf_id, nm->fib_src_low);
1957               init_nat_kv (&kv, m->local_addr, m->local_port, local->fib_index,
1958                            m->proto, 0, m - nm->static_mappings);
1959               clib_bihash_add_del_8_8 (&nm->static_mapping_by_local, &kv, 1);
1960               return 0;
1961             }
1962           return VNET_API_ERROR_VALUE_EXIST;
1963         }
1964
1965       /* Convert VRF id to FIB index */
1966       if (vrf_id != ~0)
1967         {
1968           fib_index = fib_table_find_or_create_and_lock (
1969             FIB_PROTOCOL_IP4, vrf_id, nm->fib_src_low);
1970         }
1971       /* If not specified use inside VRF id from NAT44 plugin config */
1972       else
1973         {
1974           fib_index = nm->inside_fib_index;
1975           vrf_id = nm->inside_vrf_id;
1976           fib_table_lock (fib_index, FIB_PROTOCOL_IP4, nm->fib_src_low);
1977         }
1978
1979       if (!identity_nat)
1980         {
1981           init_nat_k (&kv, l_addr, addr_only ? 0 : l_port, fib_index,
1982                       addr_only ? 0 : proto);
1983           if (!clib_bihash_search_8_8 (&nm->static_mapping_by_local, &kv,
1984                                        &value))
1985             return VNET_API_ERROR_VALUE_EXIST;
1986         }
1987
1988       /* Find external address in allocated addresses and reserve port for
1989          address and port pair mapping when dynamic translations enabled */
1990       if (!(addr_only || nm->static_mapping_only))
1991         {
1992           for (i = 0; i < vec_len (nm->addresses); i++)
1993             {
1994               if (nm->addresses[i].addr.as_u32 == e_addr.as_u32)
1995                 {
1996                   a = nm->addresses + i;
1997                   /* External port must be unused */
1998                   switch (proto)
1999                     {
2000 #define _(N, j, n, s)                                                         \
2001   case NAT_PROTOCOL_##N:                                                      \
2002     if (a->busy_##n##_port_refcounts[e_port])                                 \
2003       return VNET_API_ERROR_INVALID_VALUE;                                    \
2004     ++a->busy_##n##_port_refcounts[e_port];                                   \
2005     if (e_port > 1024)                                                        \
2006       {                                                                       \
2007         a->busy_##n##_ports++;                                                \
2008         a->busy_##n##_ports_per_thread[nat44_ei_get_thread_idx_by_port (      \
2009           e_port)]++;                                                         \
2010       }                                                                       \
2011     break;
2012                       foreach_nat_protocol
2013 #undef _
2014                         default : nat_elog_info (nm, "unknown protocol");
2015                       return VNET_API_ERROR_INVALID_VALUE_2;
2016                     }
2017                   break;
2018                 }
2019             }
2020           /* External address must be allocated */
2021           if (!a && (l_addr.as_u32 != e_addr.as_u32))
2022             {
2023               if (sw_if_index != ~0)
2024                 {
2025                   for (i = 0; i < vec_len (nm->to_resolve); i++)
2026                     {
2027                       rp = nm->to_resolve + i;
2028                       if (rp->addr_only)
2029                         continue;
2030                       if (rp->sw_if_index != sw_if_index &&
2031                           rp->l_addr.as_u32 != l_addr.as_u32 &&
2032                           rp->vrf_id != vrf_id && rp->l_port != l_port &&
2033                           rp->e_port != e_port && rp->proto != proto)
2034                         continue;
2035
2036                       vec_del1 (nm->to_resolve, i);
2037                       break;
2038                     }
2039                 }
2040               return VNET_API_ERROR_NO_SUCH_ENTRY;
2041             }
2042         }
2043
2044       pool_get (nm->static_mappings, m);
2045       clib_memset (m, 0, sizeof (*m));
2046       m->tag = vec_dup (tag);
2047       m->local_addr = l_addr;
2048       m->external_addr = e_addr;
2049
2050       if (addr_only)
2051         m->flags |= NAT44_EI_STATIC_MAPPING_FLAG_ADDR_ONLY;
2052       else
2053         {
2054           m->local_port = l_port;
2055           m->external_port = e_port;
2056           m->proto = proto;
2057         }
2058
2059       if (identity_nat)
2060         {
2061           m->flags |= NAT44_EI_STATIC_MAPPING_FLAG_IDENTITY_NAT;
2062           pool_get (m->locals, local);
2063           local->vrf_id = vrf_id;
2064           local->fib_index = fib_index;
2065         }
2066       else
2067         {
2068           m->vrf_id = vrf_id;
2069           m->fib_index = fib_index;
2070         }
2071
2072       if (nm->num_workers > 1)
2073         {
2074           ip4_header_t ip = {
2075             .src_address = m->local_addr,
2076           };
2077           vec_add1 (m->workers,
2078                     nat44_ei_get_in2out_worker_index (&ip, m->fib_index, 0));
2079           tnm = vec_elt_at_index (nm->per_thread_data, m->workers[0]);
2080         }
2081       else
2082         tnm = vec_elt_at_index (nm->per_thread_data, nm->num_workers);
2083
2084       init_nat_kv (&kv, m->local_addr, m->local_port, fib_index, m->proto, 0,
2085                    m - nm->static_mappings);
2086       clib_bihash_add_del_8_8 (&nm->static_mapping_by_local, &kv, 1);
2087
2088       init_nat_kv (&kv, m->external_addr, m->external_port, 0, m->proto, 0,
2089                    m - nm->static_mappings);
2090       clib_bihash_add_del_8_8 (&nm->static_mapping_by_external, &kv, 1);
2091
2092       /* Delete dynamic sessions matching local address (+ local port) */
2093       // TODO: based on type of NAT EI/ED
2094       if (!(nm->static_mapping_only))
2095         {
2096           u_key.addr = m->local_addr;
2097           u_key.fib_index = m->fib_index;
2098           kv.key = u_key.as_u64;
2099           if (!clib_bihash_search_8_8 (&tnm->user_hash, &kv, &value))
2100             {
2101               user_index = value.value;
2102               u = pool_elt_at_index (tnm->users, user_index);
2103               if (u->nsessions)
2104                 {
2105                   head_index = u->sessions_per_user_list_head_index;
2106                   head = pool_elt_at_index (tnm->list_pool, head_index);
2107                   elt_index = head->next;
2108                   elt = pool_elt_at_index (tnm->list_pool, elt_index);
2109                   ses_index = elt->value;
2110                   while (ses_index != ~0)
2111                     {
2112                       s = pool_elt_at_index (tnm->sessions, ses_index);
2113                       elt = pool_elt_at_index (tnm->list_pool, elt->next);
2114                       ses_index = elt->value;
2115
2116                       if (nat44_ei_is_session_static (s))
2117                         continue;
2118
2119                       if (!addr_only && s->in2out.port != m->local_port)
2120                         continue;
2121
2122                       nat44_ei_free_session_data_v2 (
2123                         nm, s, tnm - nm->per_thread_data, 0);
2124                       nat44_ei_delete_session (nm, s,
2125                                                tnm - nm->per_thread_data);
2126
2127                       if (!addr_only)
2128                         break;
2129                     }
2130                 }
2131             }
2132         }
2133     }
2134   else
2135     {
2136       if (!m)
2137         {
2138           if (sw_if_index != ~0)
2139             return 0;
2140           else
2141             return VNET_API_ERROR_NO_SUCH_ENTRY;
2142         }
2143
2144       if (identity_nat)
2145         {
2146           if (vrf_id == ~0)
2147             vrf_id = nm->inside_vrf_id;
2148
2149           pool_foreach (local, m->locals)
2150             {
2151               if (local->vrf_id == vrf_id)
2152                 find = local - m->locals;
2153             }
2154           if (find == ~0)
2155             return VNET_API_ERROR_NO_SUCH_ENTRY;
2156
2157           local = pool_elt_at_index (m->locals, find);
2158           fib_index = local->fib_index;
2159           pool_put (m->locals, local);
2160         }
2161       else
2162         fib_index = m->fib_index;
2163
2164       /* Free external address port */
2165       if (!(addr_only || nm->static_mapping_only))
2166         {
2167           for (i = 0; i < vec_len (nm->addresses); i++)
2168             {
2169               if (nm->addresses[i].addr.as_u32 == e_addr.as_u32)
2170                 {
2171                   a = nm->addresses + i;
2172                   switch (proto)
2173                     {
2174 #define _(N, j, n, s)                                                         \
2175   case NAT_PROTOCOL_##N:                                                      \
2176     --a->busy_##n##_port_refcounts[e_port];                                   \
2177     if (e_port > 1024)                                                        \
2178       {                                                                       \
2179         a->busy_##n##_ports--;                                                \
2180         a->busy_##n##_ports_per_thread[nat44_ei_get_thread_idx_by_port (      \
2181           e_port)]--;                                                         \
2182       }                                                                       \
2183     break;
2184                       foreach_nat_protocol
2185 #undef _
2186                         default : return VNET_API_ERROR_INVALID_VALUE_2;
2187                     }
2188                   break;
2189                 }
2190             }
2191         }
2192
2193       if (nm->num_workers > 1)
2194         tnm = vec_elt_at_index (nm->per_thread_data, m->workers[0]);
2195       else
2196         tnm = vec_elt_at_index (nm->per_thread_data, nm->num_workers);
2197
2198       init_nat_k (&kv, m->local_addr, m->local_port, fib_index, m->proto);
2199       clib_bihash_add_del_8_8 (&nm->static_mapping_by_local, &kv, 0);
2200
2201       /* Delete session(s) for static mapping if exist */
2202       if (!(nm->static_mapping_only) ||
2203           (nm->static_mapping_only && nm->static_mapping_connection_tracking))
2204         {
2205           u_key.addr = m->local_addr;
2206           u_key.fib_index = fib_index;
2207           kv.key = u_key.as_u64;
2208           nat44_ei_static_mapping_del_sessions (nm, tnm, u_key, addr_only,
2209                                                 e_addr, e_port);
2210         }
2211
2212       fib_table_unlock (fib_index, FIB_PROTOCOL_IP4, nm->fib_src_low);
2213       if (pool_elts (m->locals))
2214         return 0;
2215
2216       init_nat_k (&kv, m->external_addr, m->external_port, 0, m->proto);
2217       clib_bihash_add_del_8_8 (&nm->static_mapping_by_external, &kv, 0);
2218
2219       vec_free (m->tag);
2220       vec_free (m->workers);
2221       /* Delete static mapping from pool */
2222       pool_put (nm->static_mappings, m);
2223     }
2224
2225   if (!addr_only || (l_addr.as_u32 == e_addr.as_u32))
2226     return 0;
2227
2228   /* Add/delete external address to FIB */
2229   pool_foreach (interface, nm->interfaces)
2230     {
2231       if (nat44_ei_interface_is_inside (interface) || nm->out2in_dpo)
2232         continue;
2233
2234       nat44_ei_add_del_addr_to_fib (&e_addr, 32, interface->sw_if_index,
2235                                     is_add);
2236       break;
2237     }
2238   pool_foreach (interface, nm->output_feature_interfaces)
2239     {
2240       if (nat44_ei_interface_is_inside (interface) || nm->out2in_dpo)
2241         continue;
2242
2243       nat44_ei_add_del_addr_to_fib (&e_addr, 32, interface->sw_if_index,
2244                                     is_add);
2245       break;
2246     }
2247   return 0;
2248 }
2249
2250 int
2251 nat44_ei_static_mapping_match (ip4_address_t match_addr, u16 match_port,
2252                                u32 match_fib_index,
2253                                nat_protocol_t match_protocol,
2254                                ip4_address_t *mapping_addr, u16 *mapping_port,
2255                                u32 *mapping_fib_index, u8 by_external,
2256                                u8 *is_addr_only, u8 *is_identity_nat)
2257 {
2258   nat44_ei_main_t *nm = &nat44_ei_main;
2259   clib_bihash_kv_8_8_t kv, value;
2260   nat44_ei_static_mapping_t *m;
2261   u16 port;
2262
2263   if (by_external)
2264     {
2265       init_nat_k (&kv, match_addr, match_port, 0, match_protocol);
2266       if (clib_bihash_search_8_8 (&nm->static_mapping_by_external, &kv,
2267                                   &value))
2268         {
2269           /* Try address only mapping */
2270           init_nat_k (&kv, match_addr, 0, 0, 0);
2271           if (clib_bihash_search_8_8 (&nm->static_mapping_by_external, &kv,
2272                                       &value))
2273             return 1;
2274         }
2275       m = pool_elt_at_index (nm->static_mappings, value.value);
2276
2277       *mapping_fib_index = m->fib_index;
2278       *mapping_addr = m->local_addr;
2279       port = m->local_port;
2280     }
2281   else
2282     {
2283       init_nat_k (&kv, match_addr, match_port, match_fib_index,
2284                   match_protocol);
2285       if (clib_bihash_search_8_8 (&nm->static_mapping_by_local, &kv, &value))
2286         {
2287           /* Try address only mapping */
2288           init_nat_k (&kv, match_addr, 0, match_fib_index, 0);
2289           if (clib_bihash_search_8_8 (&nm->static_mapping_by_local, &kv,
2290                                       &value))
2291             return 1;
2292         }
2293       m = pool_elt_at_index (nm->static_mappings, value.value);
2294
2295       *mapping_fib_index = nm->outside_fib_index;
2296       *mapping_addr = m->external_addr;
2297       port = m->external_port;
2298     }
2299
2300   /* Address only mapping doesn't change port */
2301   if (nat44_ei_is_addr_only_static_mapping (m))
2302     *mapping_port = match_port;
2303   else
2304     *mapping_port = port;
2305
2306   if (PREDICT_FALSE (is_addr_only != 0))
2307     *is_addr_only = nat44_ei_is_addr_only_static_mapping (m);
2308
2309   if (PREDICT_FALSE (is_identity_nat != 0))
2310     *is_identity_nat = nat44_ei_is_identity_static_mapping (m);
2311
2312   return 0;
2313 }
2314
2315 static void
2316 nat44_ei_worker_db_free (nat44_ei_main_per_thread_data_t *tnm)
2317 {
2318   pool_free (tnm->list_pool);
2319   pool_free (tnm->lru_pool);
2320   pool_free (tnm->sessions);
2321   pool_free (tnm->users);
2322
2323   clib_bihash_free_8_8 (&tnm->user_hash);
2324 }
2325
2326 u8 *
2327 format_nat44_ei_key (u8 *s, va_list *args)
2328 {
2329   u64 key = va_arg (*args, u64);
2330
2331   ip4_address_t addr;
2332   u16 port;
2333   nat_protocol_t protocol;
2334   u32 fib_index;
2335
2336   split_nat_key (key, &addr, &port, &fib_index, &protocol);
2337
2338   s = format (s, "%U proto %U port %d fib %d", format_ip4_address, &addr,
2339               format_nat_protocol, protocol, clib_net_to_host_u16 (port),
2340               fib_index);
2341   return s;
2342 }
2343
2344 u8 *
2345 format_nat44_ei_user_kvp (u8 *s, va_list *args)
2346 {
2347   clib_bihash_kv_8_8_t *v = va_arg (*args, clib_bihash_kv_8_8_t *);
2348   nat44_ei_user_key_t k;
2349
2350   k.as_u64 = v->key;
2351
2352   s = format (s, "%U fib %d user-index %llu", format_ip4_address, &k.addr,
2353               k.fib_index, v->value);
2354
2355   return s;
2356 }
2357
2358 u8 *
2359 format_nat44_ei_session_kvp (u8 *s, va_list *args)
2360 {
2361   clib_bihash_kv_8_8_t *v = va_arg (*args, clib_bihash_kv_8_8_t *);
2362
2363   s = format (s, "%U thread-index %llu session-index %llu",
2364               format_nat44_ei_key, v->key, nat_value_get_thread_index (v),
2365               nat_value_get_session_index (v));
2366
2367   return s;
2368 }
2369
2370 u8 *
2371 format_nat44_ei_static_mapping_kvp (u8 *s, va_list *args)
2372 {
2373   clib_bihash_kv_8_8_t *v = va_arg (*args, clib_bihash_kv_8_8_t *);
2374
2375   s = format (s, "%U static-mapping-index %llu", format_nat44_ei_key, v->key,
2376               v->value);
2377
2378   return s;
2379 }
2380
2381 static void
2382 nat44_ei_worker_db_init (nat44_ei_main_per_thread_data_t *tnm,
2383                          u32 translations, u32 translation_buckets,
2384                          u32 user_buckets)
2385 {
2386   dlist_elt_t *head;
2387
2388   pool_alloc (tnm->list_pool, translations);
2389   pool_alloc (tnm->lru_pool, translations);
2390   pool_alloc (tnm->sessions, translations);
2391
2392   clib_bihash_init_8_8 (&tnm->user_hash, "users", user_buckets, 0);
2393
2394   clib_bihash_set_kvp_format_fn_8_8 (&tnm->user_hash,
2395                                      format_nat44_ei_user_kvp);
2396
2397   pool_get (tnm->lru_pool, head);
2398   tnm->tcp_trans_lru_head_index = head - tnm->lru_pool;
2399   clib_dlist_init (tnm->lru_pool, tnm->tcp_trans_lru_head_index);
2400
2401   pool_get (tnm->lru_pool, head);
2402   tnm->tcp_estab_lru_head_index = head - tnm->lru_pool;
2403   clib_dlist_init (tnm->lru_pool, tnm->tcp_estab_lru_head_index);
2404
2405   pool_get (tnm->lru_pool, head);
2406   tnm->udp_lru_head_index = head - tnm->lru_pool;
2407   clib_dlist_init (tnm->lru_pool, tnm->udp_lru_head_index);
2408
2409   pool_get (tnm->lru_pool, head);
2410   tnm->icmp_lru_head_index = head - tnm->lru_pool;
2411   clib_dlist_init (tnm->lru_pool, tnm->icmp_lru_head_index);
2412
2413   pool_get (tnm->lru_pool, head);
2414   tnm->unk_proto_lru_head_index = head - tnm->lru_pool;
2415   clib_dlist_init (tnm->lru_pool, tnm->unk_proto_lru_head_index);
2416 }
2417
2418 static void
2419 nat44_ei_db_free ()
2420 {
2421   nat44_ei_main_t *nm = &nat44_ei_main;
2422   nat44_ei_main_per_thread_data_t *tnm;
2423
2424   pool_free (nm->static_mappings);
2425   clib_bihash_free_8_8 (&nm->static_mapping_by_local);
2426   clib_bihash_free_8_8 (&nm->static_mapping_by_external);
2427
2428   if (nm->pat)
2429     {
2430       clib_bihash_free_8_8 (&nm->in2out);
2431       clib_bihash_free_8_8 (&nm->out2in);
2432       vec_foreach (tnm, nm->per_thread_data)
2433         {
2434           nat44_ei_worker_db_free (tnm);
2435         }
2436     }
2437 }
2438
2439 static void
2440 nat44_ei_db_init (u32 translations, u32 translation_buckets, u32 user_buckets)
2441 {
2442   nat44_ei_main_t *nm = &nat44_ei_main;
2443   nat44_ei_main_per_thread_data_t *tnm;
2444
2445   u32 static_mapping_buckets = 1024;
2446   u32 static_mapping_memory_size = 64 << 20;
2447
2448   clib_bihash_init_8_8 (&nm->static_mapping_by_local,
2449                         "static_mapping_by_local", static_mapping_buckets,
2450                         static_mapping_memory_size);
2451   clib_bihash_init_8_8 (&nm->static_mapping_by_external,
2452                         "static_mapping_by_external", static_mapping_buckets,
2453                         static_mapping_memory_size);
2454   clib_bihash_set_kvp_format_fn_8_8 (&nm->static_mapping_by_local,
2455                                      format_nat44_ei_static_mapping_kvp);
2456   clib_bihash_set_kvp_format_fn_8_8 (&nm->static_mapping_by_external,
2457                                      format_nat44_ei_static_mapping_kvp);
2458
2459   if (nm->pat)
2460     {
2461       clib_bihash_init_8_8 (&nm->in2out, "in2out", translation_buckets, 0);
2462       clib_bihash_init_8_8 (&nm->out2in, "out2in", translation_buckets, 0);
2463       clib_bihash_set_kvp_format_fn_8_8 (&nm->in2out,
2464                                          format_nat44_ei_session_kvp);
2465       clib_bihash_set_kvp_format_fn_8_8 (&nm->out2in,
2466                                          format_nat44_ei_session_kvp);
2467       vec_foreach (tnm, nm->per_thread_data)
2468         {
2469           nat44_ei_worker_db_init (tnm, translations, translation_buckets,
2470                                    user_buckets);
2471         }
2472     }
2473 }
2474
2475 void
2476 nat44_ei_sessions_clear ()
2477 {
2478   nat44_ei_main_t *nm = &nat44_ei_main;
2479   nat44_ei_main_per_thread_data_t *tnm;
2480
2481   if (nm->pat)
2482     {
2483       clib_bihash_free_8_8 (&nm->in2out);
2484       clib_bihash_free_8_8 (&nm->out2in);
2485       clib_bihash_init_8_8 (&nm->in2out, "in2out", nm->translation_buckets, 0);
2486       clib_bihash_init_8_8 (&nm->out2in, "out2in", nm->translation_buckets, 0);
2487       clib_bihash_set_kvp_format_fn_8_8 (&nm->in2out,
2488                                          format_nat44_ei_session_kvp);
2489       clib_bihash_set_kvp_format_fn_8_8 (&nm->out2in,
2490                                          format_nat44_ei_session_kvp);
2491       vec_foreach (tnm, nm->per_thread_data)
2492         {
2493           nat44_ei_worker_db_free (tnm);
2494           nat44_ei_worker_db_init (tnm, nm->translations,
2495                                    nm->translation_buckets, nm->user_buckets);
2496         }
2497     }
2498
2499   vlib_zero_simple_counter (&nm->total_users, 0);
2500   vlib_zero_simple_counter (&nm->total_sessions, 0);
2501   vlib_zero_simple_counter (&nm->user_limit_reached, 0);
2502 }
2503
2504 static void
2505 nat44_ei_update_outside_fib (ip4_main_t *im, uword opaque, u32 sw_if_index,
2506                              u32 new_fib_index, u32 old_fib_index)
2507 {
2508   nat44_ei_main_t *nm = &nat44_ei_main;
2509   nat44_ei_outside_fib_t *outside_fib;
2510   nat44_ei_interface_t *i;
2511   u8 is_add = 1;
2512   u8 match = 0;
2513
2514   if (!nm->enabled || (new_fib_index == old_fib_index) ||
2515       (!vec_len (nm->outside_fibs)))
2516     {
2517       return;
2518     }
2519
2520   pool_foreach (i, nm->interfaces)
2521     {
2522       if (i->sw_if_index == sw_if_index)
2523         {
2524           if (!(nat44_ei_interface_is_outside (i)))
2525             return;
2526           match = 1;
2527         }
2528     }
2529
2530   pool_foreach (i, nm->output_feature_interfaces)
2531     {
2532       if (i->sw_if_index == sw_if_index)
2533         {
2534           if (!(nat44_ei_interface_is_outside (i)))
2535             return;
2536           match = 1;
2537         }
2538     }
2539
2540   if (!match)
2541     return;
2542
2543   vec_foreach (outside_fib, nm->outside_fibs)
2544     {
2545       if (outside_fib->fib_index == old_fib_index)
2546         {
2547           outside_fib->refcount--;
2548           if (!outside_fib->refcount)
2549             vec_del1 (nm->outside_fibs, outside_fib - nm->outside_fibs);
2550           break;
2551         }
2552     }
2553
2554   vec_foreach (outside_fib, nm->outside_fibs)
2555     {
2556       if (outside_fib->fib_index == new_fib_index)
2557         {
2558           outside_fib->refcount++;
2559           is_add = 0;
2560           break;
2561         }
2562     }
2563
2564   if (is_add)
2565     {
2566       vec_add2 (nm->outside_fibs, outside_fib, 1);
2567       outside_fib->refcount = 1;
2568       outside_fib->fib_index = new_fib_index;
2569     }
2570 }
2571
2572 int
2573 nat44_ei_add_address (nat44_ei_main_t *nm, ip4_address_t *addr, u32 vrf_id)
2574 {
2575   nat44_ei_address_t *ap;
2576   nat44_ei_interface_t *i;
2577   vlib_thread_main_t *tm = vlib_get_thread_main ();
2578
2579   /* Check if address already exists */
2580   vec_foreach (ap, nm->addresses)
2581     {
2582       if (ap->addr.as_u32 == addr->as_u32)
2583         {
2584           nat44_ei_log_err ("address exist");
2585           return VNET_API_ERROR_VALUE_EXIST;
2586         }
2587     }
2588
2589   vec_add2 (nm->addresses, ap, 1);
2590
2591   ap->addr = *addr;
2592   if (vrf_id != ~0)
2593     ap->fib_index = fib_table_find_or_create_and_lock (
2594       FIB_PROTOCOL_IP4, vrf_id, nm->fib_src_low);
2595   else
2596     ap->fib_index = ~0;
2597
2598 #define _(N, i, n, s)                                                         \
2599   clib_memset (ap->busy_##n##_port_refcounts, 0,                              \
2600                sizeof (ap->busy_##n##_port_refcounts));                       \
2601   ap->busy_##n##_ports = 0;                                                   \
2602   ap->busy_##n##_ports_per_thread = 0;                                        \
2603   vec_validate_init_empty (ap->busy_##n##_ports_per_thread,                   \
2604                            tm->n_vlib_mains - 1, 0);
2605   foreach_nat_protocol
2606 #undef _
2607
2608     /* Add external address to FIB */
2609     pool_foreach (i, nm->interfaces)
2610   {
2611     if (nat44_ei_interface_is_inside (i) || nm->out2in_dpo)
2612       continue;
2613
2614     nat44_ei_add_del_addr_to_fib (addr, 32, i->sw_if_index, 1);
2615     break;
2616   }
2617   pool_foreach (i, nm->output_feature_interfaces)
2618     {
2619       if (nat44_ei_interface_is_inside (i) || nm->out2in_dpo)
2620         continue;
2621
2622       nat44_ei_add_del_addr_to_fib (addr, 32, i->sw_if_index, 1);
2623       break;
2624     }
2625
2626   return 0;
2627 }
2628
2629 int
2630 nat44_ei_add_interface_address (nat44_ei_main_t *nm, u32 sw_if_index,
2631                                 int is_del)
2632 {
2633   ip4_main_t *ip4_main = nm->ip4_main;
2634   ip4_address_t *first_int_addr;
2635   nat44_ei_static_map_resolve_t *rp;
2636   u32 *indices_to_delete = 0;
2637   int i, j;
2638   u32 *auto_add_sw_if_indices = nm->auto_add_sw_if_indices;
2639
2640   first_int_addr = ip4_interface_first_address (ip4_main, sw_if_index,
2641                                                 0 /* just want the address */);
2642
2643   for (i = 0; i < vec_len (auto_add_sw_if_indices); i++)
2644     {
2645       if (auto_add_sw_if_indices[i] == sw_if_index)
2646         {
2647           if (is_del)
2648             {
2649               /* if have address remove it */
2650               if (first_int_addr)
2651                 (void) nat44_ei_del_address (nm, first_int_addr[0], 1);
2652               else
2653                 {
2654                   for (j = 0; j < vec_len (nm->to_resolve); j++)
2655                     {
2656                       rp = nm->to_resolve + j;
2657                       if (rp->sw_if_index == sw_if_index)
2658                         vec_add1 (indices_to_delete, j);
2659                     }
2660                   if (vec_len (indices_to_delete))
2661                     {
2662                       for (j = vec_len (indices_to_delete) - 1; j >= 0; j--)
2663                         vec_del1 (nm->to_resolve, j);
2664                       vec_free (indices_to_delete);
2665                     }
2666                 }
2667               vec_del1 (nm->auto_add_sw_if_indices, i);
2668             }
2669           else
2670             return VNET_API_ERROR_VALUE_EXIST;
2671
2672           return 0;
2673         }
2674     }
2675
2676   if (is_del)
2677     return VNET_API_ERROR_NO_SUCH_ENTRY;
2678
2679   /* add to the auto-address list */
2680   vec_add1 (nm->auto_add_sw_if_indices, sw_if_index);
2681
2682   /* If the address is already bound - or static - add it now */
2683   if (first_int_addr)
2684     (void) nat44_ei_add_address (nm, first_int_addr, ~0);
2685
2686   return 0;
2687 }
2688
2689 static int
2690 nat44_ei_is_address_used_in_static_mapping (ip4_address_t addr)
2691 {
2692   nat44_ei_main_t *nm = &nat44_ei_main;
2693   nat44_ei_static_mapping_t *m;
2694   pool_foreach (m, nm->static_mappings)
2695     {
2696       if (nat44_ei_is_addr_only_static_mapping (m) ||
2697           nat44_ei_is_identity_static_mapping (m))
2698         continue;
2699       if (m->external_addr.as_u32 == addr.as_u32)
2700         return 1;
2701     }
2702   return 0;
2703 }
2704
2705 int
2706 nat44_ei_del_address (nat44_ei_main_t *nm, ip4_address_t addr, u8 delete_sm)
2707 {
2708   nat44_ei_address_t *a = 0;
2709   nat44_ei_session_t *ses;
2710   u32 *ses_to_be_removed = 0, *ses_index;
2711   nat44_ei_main_per_thread_data_t *tnm;
2712   nat44_ei_interface_t *interface;
2713   nat44_ei_static_mapping_t *m;
2714   int i;
2715
2716   /* Find SNAT address */
2717   for (i = 0; i < vec_len (nm->addresses); i++)
2718     {
2719       if (nm->addresses[i].addr.as_u32 == addr.as_u32)
2720         {
2721           a = nm->addresses + i;
2722           break;
2723         }
2724     }
2725   if (!a)
2726     {
2727       nat44_ei_log_err ("no such address");
2728       return VNET_API_ERROR_NO_SUCH_ENTRY;
2729     }
2730
2731   if (delete_sm)
2732     {
2733       pool_foreach (m, nm->static_mappings)
2734         {
2735           if (m->external_addr.as_u32 == addr.as_u32)
2736             (void) nat44_ei_add_del_static_mapping (
2737               m->local_addr, m->external_addr, m->local_port, m->external_port,
2738               m->proto, ~0 /* sw_if_index */, m->vrf_id,
2739               nat44_ei_is_addr_only_static_mapping (m),
2740               nat44_ei_is_identity_static_mapping (m), m->tag, 0);
2741         }
2742     }
2743   else
2744     {
2745       /* Check if address is used in some static mapping */
2746       if (nat44_ei_is_address_used_in_static_mapping (addr))
2747         {
2748           nat44_ei_log_err ("address used in static mapping");
2749           return VNET_API_ERROR_UNSPECIFIED;
2750         }
2751     }
2752
2753   if (a->fib_index != ~0)
2754     fib_table_unlock (a->fib_index, FIB_PROTOCOL_IP4, nm->fib_src_low);
2755
2756   /* Delete sessions using address */
2757   if (a->busy_tcp_ports || a->busy_udp_ports || a->busy_icmp_ports)
2758     {
2759       vec_foreach (tnm, nm->per_thread_data)
2760         {
2761           pool_foreach (ses, tnm->sessions)
2762             {
2763               if (ses->out2in.addr.as_u32 == addr.as_u32)
2764                 {
2765                   nat44_ei_free_session_data (nm, ses,
2766                                               tnm - nm->per_thread_data, 0);
2767                   vec_add1 (ses_to_be_removed, ses - tnm->sessions);
2768                 }
2769             }
2770           vec_foreach (ses_index, ses_to_be_removed)
2771             {
2772               ses = pool_elt_at_index (tnm->sessions, ses_index[0]);
2773               nat44_ei_delete_session (nm, ses, tnm - nm->per_thread_data);
2774             }
2775           vec_free (ses_to_be_removed);
2776         }
2777     }
2778
2779 #define _(N, i, n, s) vec_free (a->busy_##n##_ports_per_thread);
2780   foreach_nat_protocol
2781 #undef _
2782     vec_del1 (nm->addresses, i);
2783
2784   /* Delete external address from FIB */
2785   pool_foreach (interface, nm->interfaces)
2786     {
2787       if (nat44_ei_interface_is_inside (interface) || nm->out2in_dpo)
2788         continue;
2789       nat44_ei_add_del_addr_to_fib (&addr, 32, interface->sw_if_index, 0);
2790       break;
2791     }
2792
2793   pool_foreach (interface, nm->output_feature_interfaces)
2794     {
2795       if (nat44_ei_interface_is_inside (interface) || nm->out2in_dpo)
2796         continue;
2797       nat44_ei_add_del_addr_to_fib (&addr, 32, interface->sw_if_index, 0);
2798       break;
2799     }
2800
2801   return 0;
2802 }
2803
2804 static void
2805 nat44_ei_ip4_add_del_interface_address_cb (ip4_main_t *im, uword opaque,
2806                                            u32 sw_if_index,
2807                                            ip4_address_t *address,
2808                                            u32 address_length,
2809                                            u32 if_address_index, u32 is_delete)
2810 {
2811   nat44_ei_main_t *nm = &nat44_ei_main;
2812   nat44_ei_static_map_resolve_t *rp;
2813   ip4_address_t l_addr;
2814   int i, j;
2815   int rv;
2816   nat44_ei_address_t *addresses = nm->addresses;
2817
2818   if (!nm->enabled)
2819     return;
2820
2821   for (i = 0; i < vec_len (nm->auto_add_sw_if_indices); i++)
2822     {
2823       if (sw_if_index == nm->auto_add_sw_if_indices[i])
2824         goto match;
2825     }
2826
2827   return;
2828
2829 match:
2830   if (!is_delete)
2831     {
2832       /* Don't trip over lease renewal, static config */
2833       for (j = 0; j < vec_len (addresses); j++)
2834         if (addresses[j].addr.as_u32 == address->as_u32)
2835           return;
2836
2837       (void) nat44_ei_add_address (nm, address, ~0);
2838       /* Scan static map resolution vector */
2839       for (j = 0; j < vec_len (nm->to_resolve); j++)
2840         {
2841           rp = nm->to_resolve + j;
2842           if (rp->addr_only)
2843             continue;
2844           /* On this interface? */
2845           if (rp->sw_if_index == sw_if_index)
2846             {
2847               /* Indetity mapping? */
2848               if (rp->l_addr.as_u32 == 0)
2849                 l_addr.as_u32 = address[0].as_u32;
2850               else
2851                 l_addr.as_u32 = rp->l_addr.as_u32;
2852               /* Add the static mapping */
2853               rv = nat44_ei_add_del_static_mapping (
2854                 l_addr, address[0], rp->l_port, rp->e_port, rp->proto,
2855                 ~0 /* sw_if_index */, rp->vrf_id, rp->addr_only,
2856                 rp->identity_nat, rp->tag, 1);
2857               if (rv)
2858                 nat_elog_notice_X1 (
2859                   nm, "nat44_ei_add_del_static_mapping returned %d", "i4", rv);
2860             }
2861         }
2862       return;
2863     }
2864   else
2865     {
2866       (void) nat44_ei_del_address (nm, address[0], 1);
2867       return;
2868     }
2869 }
2870
2871 int
2872 nat44_ei_set_frame_queue_nelts (u32 frame_queue_nelts)
2873 {
2874   fail_if_enabled ();
2875   nat44_ei_main_t *nm = &nat44_ei_main;
2876   nm->frame_queue_nelts = frame_queue_nelts;
2877   return 0;
2878 }
2879
2880 static void
2881 nat44_ei_ip4_add_del_addr_only_sm_cb (ip4_main_t *im, uword opaque,
2882                                       u32 sw_if_index, ip4_address_t *address,
2883                                       u32 address_length, u32 if_address_index,
2884                                       u32 is_delete)
2885 {
2886   nat44_ei_main_t *nm = &nat44_ei_main;
2887   nat44_ei_static_map_resolve_t *rp;
2888   nat44_ei_static_mapping_t *m;
2889   clib_bihash_kv_8_8_t kv, value;
2890   int i, rv;
2891   ip4_address_t l_addr;
2892
2893   if (!nm->enabled)
2894     return;
2895
2896   for (i = 0; i < vec_len (nm->to_resolve); i++)
2897     {
2898       rp = nm->to_resolve + i;
2899       if (rp->addr_only == 0)
2900         continue;
2901       if (rp->sw_if_index == sw_if_index)
2902         goto match;
2903     }
2904
2905   return;
2906
2907 match:
2908   init_nat_k (&kv, *address, rp->addr_only ? 0 : rp->e_port,
2909               nm->outside_fib_index, rp->addr_only ? 0 : rp->proto);
2910   if (clib_bihash_search_8_8 (&nm->static_mapping_by_external, &kv, &value))
2911     m = 0;
2912   else
2913     m = pool_elt_at_index (nm->static_mappings, value.value);
2914
2915   if (!is_delete)
2916     {
2917       /* Don't trip over lease renewal, static config */
2918       if (m)
2919         return;
2920     }
2921   else
2922     {
2923       if (!m)
2924         return;
2925     }
2926
2927   /* Indetity mapping? */
2928   if (rp->l_addr.as_u32 == 0)
2929     l_addr.as_u32 = address[0].as_u32;
2930   else
2931     l_addr.as_u32 = rp->l_addr.as_u32;
2932   /* Add the static mapping */
2933
2934   rv = nat44_ei_add_del_static_mapping (
2935     l_addr, address[0], rp->l_port, rp->e_port, rp->proto,
2936     ~0 /* sw_if_index */, rp->vrf_id, rp->addr_only, rp->identity_nat, rp->tag,
2937     !is_delete);
2938   if (rv)
2939     nat_elog_notice_X1 (nm, "nat44_ei_add_del_static_mapping returned %d",
2940                         "i4", rv);
2941 }
2942
2943 static_always_inline uword
2944 nat44_ei_classify_inline_fn (vlib_main_t *vm, vlib_node_runtime_t *node,
2945                              vlib_frame_t *frame)
2946 {
2947   u32 n_left_from, *from, *to_next;
2948   nat44_ei_classify_next_t next_index;
2949   nat44_ei_main_t *nm = &nat44_ei_main;
2950   nat44_ei_static_mapping_t *m;
2951   u32 next_in2out = 0, next_out2in = 0;
2952
2953   from = vlib_frame_vector_args (frame);
2954   n_left_from = frame->n_vectors;
2955   next_index = node->cached_next_index;
2956
2957   while (n_left_from > 0)
2958     {
2959       u32 n_left_to_next;
2960
2961       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
2962
2963       while (n_left_from > 0 && n_left_to_next > 0)
2964         {
2965           u32 bi0;
2966           vlib_buffer_t *b0;
2967           u32 next0 = NAT44_EI_CLASSIFY_NEXT_IN2OUT;
2968           ip4_header_t *ip0;
2969           nat44_ei_address_t *ap;
2970           clib_bihash_kv_8_8_t kv0, value0;
2971
2972           /* speculatively enqueue b0 to the current next frame */
2973           bi0 = from[0];
2974           to_next[0] = bi0;
2975           from += 1;
2976           to_next += 1;
2977           n_left_from -= 1;
2978           n_left_to_next -= 1;
2979
2980           b0 = vlib_get_buffer (vm, bi0);
2981           ip0 = vlib_buffer_get_current (b0);
2982
2983           vec_foreach (ap, nm->addresses)
2984             {
2985               if (ip0->dst_address.as_u32 == ap->addr.as_u32)
2986                 {
2987                   next0 = NAT44_EI_CLASSIFY_NEXT_OUT2IN;
2988                   goto enqueue0;
2989                 }
2990             }
2991
2992           if (PREDICT_FALSE (pool_elts (nm->static_mappings)))
2993             {
2994               init_nat_k (&kv0, ip0->dst_address, 0, 0, 0);
2995               /* try to classify the fragment based on IP header alone */
2996               if (!clib_bihash_search_8_8 (&nm->static_mapping_by_external,
2997                                            &kv0, &value0))
2998                 {
2999                   m = pool_elt_at_index (nm->static_mappings, value0.value);
3000                   if (m->local_addr.as_u32 != m->external_addr.as_u32)
3001                     next0 = NAT44_EI_CLASSIFY_NEXT_OUT2IN;
3002                   goto enqueue0;
3003                 }
3004               init_nat_k (&kv0, ip0->dst_address,
3005                           vnet_buffer (b0)->ip.reass.l4_dst_port, 0,
3006                           ip_proto_to_nat_proto (ip0->protocol));
3007               if (!clib_bihash_search_8_8 (&nm->static_mapping_by_external,
3008                                            &kv0, &value0))
3009                 {
3010                   m = pool_elt_at_index (nm->static_mappings, value0.value);
3011                   if (m->local_addr.as_u32 != m->external_addr.as_u32)
3012                     next0 = NAT44_EI_CLASSIFY_NEXT_OUT2IN;
3013                 }
3014             }
3015
3016         enqueue0:
3017           if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE) &&
3018                              (b0->flags & VLIB_BUFFER_IS_TRACED)))
3019             {
3020               nat44_ei_classify_trace_t *t =
3021                 vlib_add_trace (vm, node, b0, sizeof (*t));
3022               t->cached = 0;
3023               t->next_in2out = next0 == NAT44_EI_CLASSIFY_NEXT_IN2OUT ? 1 : 0;
3024             }
3025
3026           next_in2out += next0 == NAT44_EI_CLASSIFY_NEXT_IN2OUT;
3027           next_out2in += next0 == NAT44_EI_CLASSIFY_NEXT_OUT2IN;
3028
3029           /* verify speculative enqueue, maybe switch current next frame */
3030           vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
3031                                            n_left_to_next, bi0, next0);
3032         }
3033
3034       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
3035     }
3036
3037   vlib_node_increment_counter (
3038     vm, node->node_index, NAT44_EI_CLASSIFY_ERROR_NEXT_IN2OUT, next_in2out);
3039   vlib_node_increment_counter (
3040     vm, node->node_index, NAT44_EI_CLASSIFY_ERROR_NEXT_OUT2IN, next_out2in);
3041   return frame->n_vectors;
3042 }
3043
3044 VLIB_NODE_FN (nat44_ei_classify_node)
3045 (vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
3046 {
3047   return nat44_ei_classify_inline_fn (vm, node, frame);
3048 }
3049
3050 VLIB_REGISTER_NODE (nat44_ei_classify_node) = {
3051   .name = "nat44-ei-classify",
3052   .vector_size = sizeof (u32),
3053   .format_trace = format_nat44_ei_classify_trace,
3054   .type = VLIB_NODE_TYPE_INTERNAL,
3055   .n_errors = ARRAY_LEN(nat44_ei_classify_error_strings),
3056   .error_strings = nat44_ei_classify_error_strings,
3057   .n_next_nodes = NAT44_EI_CLASSIFY_N_NEXT,
3058   .next_nodes = {
3059     [NAT44_EI_CLASSIFY_NEXT_IN2OUT] = "nat44-ei-in2out",
3060     [NAT44_EI_CLASSIFY_NEXT_OUT2IN] = "nat44-ei-out2in",
3061     [NAT44_EI_CLASSIFY_NEXT_DROP] = "error-drop",
3062   },
3063 };
3064
3065 VLIB_NODE_FN (nat44_ei_handoff_classify_node)
3066 (vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
3067 {
3068   return nat44_ei_classify_inline_fn (vm, node, frame);
3069 }
3070
3071 VLIB_REGISTER_NODE (nat44_ei_handoff_classify_node) = {
3072   .name = "nat44-ei-handoff-classify",
3073   .vector_size = sizeof (u32),
3074   .format_trace = format_nat44_ei_classify_trace,
3075   .type = VLIB_NODE_TYPE_INTERNAL,
3076   .n_errors = ARRAY_LEN(nat44_ei_classify_error_strings),
3077   .error_strings = nat44_ei_classify_error_strings,
3078   .n_next_nodes = NAT44_EI_CLASSIFY_N_NEXT,
3079   .next_nodes = {
3080     [NAT44_EI_CLASSIFY_NEXT_IN2OUT] = "nat44-ei-in2out-worker-handoff",
3081     [NAT44_EI_CLASSIFY_NEXT_OUT2IN] = "nat44-ei-out2in-worker-handoff",
3082     [NAT44_EI_CLASSIFY_NEXT_DROP] = "error-drop",
3083   },
3084 };
3085
3086 /*
3087  * fd.io coding-style-patch-verification: ON
3088  *
3089  * Local Variables:
3090  * eval: (c-set-style "gnu")
3091  * End:
3092  */