2 * nat44_ei.c - nat44 endpoint dependent plugin
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:
9 * http://www.apache.org/licenses/LICENSE-2.0
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
18 #include <vnet/plugin/plugin.h>
19 #include <vpp/app/version.h>
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>
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>
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>
40 nat44_ei_main_t nat44_ei_main;
42 extern vlib_node_registration_t nat44_ei_hairpinning_node;
43 extern vlib_node_registration_t
44 nat44_ei_in2out_hairpinning_finish_ip4_lookup_node;
45 extern vlib_node_registration_t
46 nat44_ei_in2out_hairpinning_finish_interface_output_node;
48 #define skip_if_disabled() \
51 nat44_ei_main_t *nm = &nat44_ei_main; \
52 if (PREDICT_FALSE (!nm->enabled)) \
57 #define fail_if_enabled() \
60 nat44_ei_main_t *nm = &nat44_ei_main; \
61 if (PREDICT_FALSE (nm->enabled)) \
63 nat44_ei_log_err ("plugin enabled"); \
64 return VNET_API_ERROR_FEATURE_ALREADY_ENABLED; \
69 #define fail_if_disabled() \
72 nat44_ei_main_t *nm = &nat44_ei_main; \
73 if (PREDICT_FALSE (!nm->enabled)) \
75 nat44_ei_log_err ("plugin disabled"); \
76 return VNET_API_ERROR_FEATURE_ALREADY_DISABLED; \
81 /* Hook up input features */
82 VNET_FEATURE_INIT (ip4_nat_classify, static) = {
83 .arc_name = "ip4-unicast",
84 .node_name = "nat44-ei-classify",
85 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa",
86 "ip4-sv-reassembly-feature"),
88 VNET_FEATURE_INIT (ip4_nat_handoff_classify, static) = {
89 .arc_name = "ip4-unicast",
90 .node_name = "nat44-ei-handoff-classify",
91 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa",
92 "ip4-sv-reassembly-feature"),
94 VNET_FEATURE_INIT (ip4_nat44_ei_in2out, static) = {
95 .arc_name = "ip4-unicast",
96 .node_name = "nat44-ei-in2out",
97 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa",
98 "ip4-sv-reassembly-feature"),
100 VNET_FEATURE_INIT (ip4_nat44_ei_out2in, static) = {
101 .arc_name = "ip4-unicast",
102 .node_name = "nat44-ei-out2in",
103 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa",
104 "ip4-sv-reassembly-feature",
105 "ip4-dhcp-client-detect"),
107 VNET_FEATURE_INIT (ip4_nat44_ei_in2out_output, static) = {
108 .arc_name = "ip4-output",
109 .node_name = "nat44-ei-in2out-output",
110 .runs_after = VNET_FEATURES ("acl-plugin-out-ip4-fa",
111 "ip4-sv-reassembly-output-feature"),
113 VNET_FEATURE_INIT (ip4_nat44_ei_hairpinning, static) = {
114 .arc_name = "ip4-local",
115 .node_name = "nat44-ei-hairpinning",
116 .runs_before = VNET_FEATURES ("ip4-local-end-of-arc"),
118 VNET_FEATURE_INIT (ip4_nat44_ei_in2out_worker_handoff, static) = {
119 .arc_name = "ip4-unicast",
120 .node_name = "nat44-ei-in2out-worker-handoff",
121 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa"),
123 VNET_FEATURE_INIT (ip4_nat44_ei_out2in_worker_handoff, static) = {
124 .arc_name = "ip4-unicast",
125 .node_name = "nat44-ei-out2in-worker-handoff",
126 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa",
127 "ip4-dhcp-client-detect"),
129 VNET_FEATURE_INIT (ip4_nat44_ei_in2out_output_worker_handoff, static) = {
130 .arc_name = "ip4-output",
131 .node_name = "nat44-ei-in2out-output-worker-handoff",
132 .runs_after = VNET_FEATURES ("acl-plugin-out-ip4-fa",
133 "ip4-sv-reassembly-output-feature"),
136 VLIB_PLUGIN_REGISTER () = {
137 .version = VPP_BUILD_VER,
138 .description = "IPv4 Endpoint-Independent NAT (NAT44 EI)",
141 #define foreach_nat44_ei_classify_error \
142 _ (NEXT_IN2OUT, "next in2out") \
143 _ (NEXT_OUT2IN, "next out2in") \
144 _ (FRAG_CACHED, "fragment cached")
148 #define _(sym, str) NAT44_EI_CLASSIFY_ERROR_##sym,
149 foreach_nat44_ei_classify_error
151 NAT44_EI_CLASSIFY_N_ERROR,
152 } nat44_ei_classify_error_t;
154 static char *nat44_ei_classify_error_strings[] = {
155 #define _(sym, string) string,
156 foreach_nat44_ei_classify_error
162 NAT44_EI_CLASSIFY_NEXT_IN2OUT,
163 NAT44_EI_CLASSIFY_NEXT_OUT2IN,
164 NAT44_EI_CLASSIFY_NEXT_DROP,
165 NAT44_EI_CLASSIFY_N_NEXT,
166 } nat44_ei_classify_next_t;
172 } nat44_ei_classify_trace_t;
174 void nat44_ei_add_del_addr_to_fib (ip4_address_t *addr, u8 p_len,
175 u32 sw_if_index, int is_add);
177 static void nat44_ei_worker_db_free (nat44_ei_main_per_thread_data_t *tnm);
179 static int nat44_ei_add_static_mapping_internal (
180 ip4_address_t l_addr, ip4_address_t e_addr, u16 l_port, u16 e_port,
181 nat_protocol_t proto, u32 vrf_id, u32 sw_if_index, u32 flags,
182 ip4_address_t pool_addr, u8 *tag);
184 static int nat44_ei_del_static_mapping_internal (
185 ip4_address_t l_addr, ip4_address_t e_addr, u16 l_port, u16 e_port,
186 nat_protocol_t proto, u32 vrf_id, u32 sw_if_index, u32 flags);
189 nat44_ei_port_is_used (nat44_ei_address_t *a, u8 proto, u16 port)
191 return clib_bitmap_get (a->busy_port_bitmap[proto], port);
195 nat44_ei_port_get (nat44_ei_address_t *a, u8 proto, u16 port)
197 ASSERT (!nat44_ei_port_is_used (a, proto, port));
198 a->busy_port_bitmap[proto] =
199 clib_bitmap_set (a->busy_port_bitmap[proto], port, 1);
203 nat44_ei_port_put (nat44_ei_address_t *a, u8 proto, u16 port)
205 ASSERT (nat44_ei_port_is_used (a, proto, port));
206 a->busy_port_bitmap[proto] =
207 clib_bitmap_set (a->busy_port_bitmap[proto], port, 0);
211 format_nat44_ei_classify_trace (u8 *s, va_list *args)
213 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
214 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
215 nat44_ei_classify_trace_t *t = va_arg (*args, nat44_ei_classify_trace_t *);
219 s = format (s, "nat44-ei-classify: fragment cached");
222 next = t->next_in2out ? "nat44-ei-in2out" : "nat44-ei-out2in";
223 s = format (s, "nat44-ei-classify: next %s", next);
229 static void nat44_ei_db_init (u32 translations, u32 translation_buckets,
232 static void nat44_ei_ip4_add_del_interface_address_cb (
233 ip4_main_t *im, uword opaque, u32 sw_if_index, ip4_address_t *address,
234 u32 address_length, u32 if_address_index, u32 is_delete);
236 static void nat44_ei_ip4_add_del_addr_only_sm_cb (
237 ip4_main_t *im, uword opaque, u32 sw_if_index, ip4_address_t *address,
238 u32 address_length, u32 if_address_index, u32 is_delete);
240 static void nat44_ei_update_outside_fib (ip4_main_t *im, uword opaque,
241 u32 sw_if_index, u32 new_fib_index,
245 nat44_ei_set_node_indexes (nat44_ei_main_t *nm, vlib_main_t *vm)
248 node = vlib_get_node_by_name (vm, (u8 *) "nat44-ei-out2in");
249 nm->out2in_node_index = node->index;
250 node = vlib_get_node_by_name (vm, (u8 *) "nat44-ei-in2out");
251 nm->in2out_node_index = node->index;
252 node = vlib_get_node_by_name (vm, (u8 *) "nat44-ei-in2out-output");
253 nm->in2out_output_node_index = node->index;
257 nat44_ei_set_workers (uword *bitmap)
259 nat44_ei_main_t *nm = &nat44_ei_main;
262 if (nm->num_workers < 2)
263 return VNET_API_ERROR_FEATURE_DISABLED;
265 if (clib_bitmap_last_set (bitmap) >= nm->num_workers)
266 return VNET_API_ERROR_INVALID_WORKER;
268 vec_free (nm->workers);
269 clib_bitmap_foreach (i, bitmap)
271 vec_add1 (nm->workers, i);
272 nm->per_thread_data[nm->first_worker_index + i].snat_thread_index = j;
273 nm->per_thread_data[nm->first_worker_index + i].thread_index = i;
277 nm->port_per_thread = (0xffff - 1024) / _vec_len (nm->workers);
282 #define nat_validate_simple_counter(c, i) \
285 vlib_validate_simple_counter (&c, i); \
286 vlib_zero_simple_counter (&c, i); \
290 #define nat_init_simple_counter(c, n, sn) \
294 c.stat_segment_name = sn; \
295 nat_validate_simple_counter (c, 0); \
299 static_always_inline void
300 nat_validate_interface_counters (nat44_ei_main_t *nm, u32 sw_if_index)
303 nat_validate_simple_counter (nm->counters.fastpath.in2out.x, sw_if_index); \
304 nat_validate_simple_counter (nm->counters.fastpath.out2in.x, sw_if_index); \
305 nat_validate_simple_counter (nm->counters.slowpath.in2out.x, sw_if_index); \
306 nat_validate_simple_counter (nm->counters.slowpath.out2in.x, sw_if_index);
309 nat_validate_simple_counter (nm->counters.hairpinning, sw_if_index);
313 nat44_ei_add_del_addr_to_fib_foreach_out_if (ip4_address_t *addr, u8 is_add)
315 nat44_ei_main_t *nm = &nat44_ei_main;
316 nat44_ei_interface_t *i;
318 pool_foreach (i, nm->interfaces)
320 if (nat44_ei_interface_is_outside (i) && !nm->out2in_dpo)
322 nat44_ei_add_del_addr_to_fib (addr, 32, i->sw_if_index, is_add);
325 pool_foreach (i, nm->output_feature_interfaces)
327 if (nat44_ei_interface_is_outside (i) && !nm->out2in_dpo)
329 nat44_ei_add_del_addr_to_fib (addr, 32, i->sw_if_index, is_add);
334 static_always_inline void
335 nat44_ei_add_del_addr_to_fib_foreach_addr (u32 sw_if_index, u8 is_add)
337 nat44_ei_main_t *nm = &nat44_ei_main;
338 nat44_ei_address_t *ap;
340 vec_foreach (ap, nm->addresses)
342 nat44_ei_add_del_addr_to_fib (&ap->addr, 32, sw_if_index, is_add);
346 static_always_inline void
347 nat44_ei_add_del_addr_to_fib_foreach_addr_only_sm (u32 sw_if_index, u8 is_add)
349 nat44_ei_main_t *nm = &nat44_ei_main;
350 nat44_ei_static_mapping_t *m;
352 pool_foreach (m, nm->static_mappings)
354 if (is_sm_addr_only (m->flags) &&
355 !(m->local_addr.as_u32 == m->external_addr.as_u32))
357 nat44_ei_add_del_addr_to_fib (&m->external_addr, 32, sw_if_index,
364 nat44_ei_is_address_used_in_static_mapping (ip4_address_t addr)
366 nat44_ei_main_t *nm = &nat44_ei_main;
367 nat44_ei_static_mapping_t *m;
368 pool_foreach (m, nm->static_mappings)
370 if (is_sm_addr_only (m->flags) || is_sm_identity_nat (m->flags))
374 if (m->external_addr.as_u32 == addr.as_u32)
383 nat44_ei_init (vlib_main_t *vm)
385 nat44_ei_main_t *nm = &nat44_ei_main;
386 vlib_thread_main_t *tm = vlib_get_thread_main ();
387 vlib_thread_registration_t *tr;
388 ip4_add_del_interface_address_callback_t cbi = { 0 };
389 ip4_table_bind_callback_t cbt = { 0 };
390 u32 i, num_threads = 0;
391 uword *p, *bitmap = 0;
393 clib_memset (nm, 0, sizeof (*nm));
396 nm->vnet_main = vnet_get_main ();
398 nm->ip4_main = &ip4_main;
399 nm->api_main = vlibapi_get_main ();
400 nm->ip4_lookup_main = &ip4_main.lookup_main;
403 nm->fq_out2in_index = ~0;
404 nm->fq_in2out_index = ~0;
405 nm->fq_in2out_output_index = ~0;
407 nm->log_level = NAT_LOG_ERROR;
409 nat44_ei_set_node_indexes (nm, vm);
410 nm->log_class = vlib_log_register_class ("nat44-ei", 0);
412 nat_init_simple_counter (nm->total_users, "total-users",
413 "/nat44-ei/total-users");
414 nat_init_simple_counter (nm->total_sessions, "total-sessions",
415 "/nat44-ei/total-sessions");
416 nat_init_simple_counter (nm->user_limit_reached, "user-limit-reached",
417 "/nat44-ei/user-limit-reached");
420 nat_init_simple_counter (nm->counters.fastpath.in2out.x, #x, \
421 "/nat44-ei/in2out/fastpath/" #x); \
422 nat_init_simple_counter (nm->counters.fastpath.out2in.x, #x, \
423 "/nat44-ei/out2in/fastpath/" #x); \
424 nat_init_simple_counter (nm->counters.slowpath.in2out.x, #x, \
425 "/nat44-ei/in2out/slowpath/" #x); \
426 nat_init_simple_counter (nm->counters.slowpath.out2in.x, #x, \
427 "/nat44-ei/out2in/slowpath/" #x);
430 nat_init_simple_counter (nm->counters.hairpinning, "hairpinning",
431 "/nat44-ei/hairpinning");
433 p = hash_get_mem (tm->thread_registrations_by_name, "workers");
436 tr = (vlib_thread_registration_t *) p[0];
439 nm->num_workers = tr->count;
440 nm->first_worker_index = tr->first_index;
443 num_threads = tm->n_vlib_mains - 1;
444 nm->port_per_thread = 0xffff - 1024;
445 vec_validate (nm->per_thread_data, num_threads);
447 /* Use all available workers by default */
448 if (nm->num_workers > 1)
450 for (i = 0; i < nm->num_workers; i++)
451 bitmap = clib_bitmap_set (bitmap, i, 1);
452 nat44_ei_set_workers (bitmap);
453 clib_bitmap_free (bitmap);
457 nm->per_thread_data[0].snat_thread_index = 0;
460 /* callbacks to call when interface address changes. */
461 cbi.function = nat44_ei_ip4_add_del_interface_address_cb;
462 vec_add1 (nm->ip4_main->add_del_interface_address_callbacks, cbi);
463 cbi.function = nat44_ei_ip4_add_del_addr_only_sm_cb;
464 vec_add1 (nm->ip4_main->add_del_interface_address_callbacks, cbi);
466 /* callbacks to call when interface to table biding changes */
467 cbt.function = nat44_ei_update_outside_fib;
468 vec_add1 (nm->ip4_main->table_bind_callbacks, cbt);
470 nm->fib_src_low = fib_source_allocate (
471 "nat44-ei-low", FIB_SOURCE_PRIORITY_LOW, FIB_SOURCE_BH_SIMPLE);
472 nm->fib_src_hi = fib_source_allocate ("nat44-ei-hi", FIB_SOURCE_PRIORITY_HI,
473 FIB_SOURCE_BH_SIMPLE);
475 // used only by out2in-dpo feature
476 nat_dpo_module_init ();
477 nat_ha_init (vm, nm->num_workers, num_threads);
479 nm->hairpinning_fq_index =
480 vlib_frame_queue_main_init (nat44_ei_hairpinning_node.index, 0);
481 nm->in2out_hairpinning_finish_ip4_lookup_node_fq_index =
482 vlib_frame_queue_main_init (
483 nat44_ei_in2out_hairpinning_finish_ip4_lookup_node.index, 0);
484 nm->in2out_hairpinning_finish_interface_output_node_fq_index =
485 vlib_frame_queue_main_init (
486 nat44_ei_in2out_hairpinning_finish_interface_output_node.index, 0);
487 return nat44_ei_api_hookup (vm);
490 VLIB_INIT_FUNCTION (nat44_ei_init);
493 nat44_ei_plugin_enable (nat44_ei_config_t c)
495 nat44_ei_main_t *nm = &nat44_ei_main;
503 c.sessions = 10 * 1024;
505 if (!c.user_sessions)
506 c.user_sessions = c.sessions;
510 if (!nm->frame_queue_nelts)
511 nm->frame_queue_nelts = NAT_FQ_NELTS_DEFAULT;
513 nm->translations = c.sessions;
514 nm->translation_buckets = nat_calc_bihash_buckets (c.sessions);
515 nm->user_buckets = nat_calc_bihash_buckets (c.users);
517 nm->pat = (!c.static_mapping_only ||
518 (c.static_mapping_only && c.connection_tracking));
520 nm->static_mapping_only = c.static_mapping_only;
521 nm->static_mapping_connection_tracking = c.connection_tracking;
522 nm->out2in_dpo = c.out2in_dpo;
523 nm->forwarding_enabled = 0;
524 nm->mss_clamping = 0;
526 nm->max_users_per_thread = c.users;
527 nm->max_translations_per_thread = c.sessions;
528 nm->max_translations_per_user = c.user_sessions;
530 nm->inside_vrf_id = c.inside_vrf;
531 nm->inside_fib_index = fib_table_find_or_create_and_lock (
532 FIB_PROTOCOL_IP4, c.inside_vrf, nm->fib_src_hi);
534 nm->outside_vrf_id = c.outside_vrf;
535 nm->outside_fib_index = fib_table_find_or_create_and_lock (
536 FIB_PROTOCOL_IP4, c.outside_vrf, nm->fib_src_hi);
538 nat_reset_timeouts (&nm->timeouts);
539 nat44_ei_db_init (nm->translations, nm->translation_buckets,
541 nat44_ei_set_alloc_default ();
543 vlib_zero_simple_counter (&nm->total_users, 0);
544 vlib_zero_simple_counter (&nm->total_sessions, 0);
545 vlib_zero_simple_counter (&nm->user_limit_reached, 0);
547 if (nm->num_workers > 1)
549 if (nm->fq_in2out_index == ~0)
551 nm->fq_in2out_index = vlib_frame_queue_main_init (
552 nm->in2out_node_index, nm->frame_queue_nelts);
554 if (nm->fq_out2in_index == ~0)
556 nm->fq_out2in_index = vlib_frame_queue_main_init (
557 nm->out2in_node_index, nm->frame_queue_nelts);
559 if (nm->fq_in2out_output_index == ~0)
561 nm->fq_in2out_output_index = vlib_frame_queue_main_init (
562 nm->in2out_output_node_index, nm->frame_queue_nelts);
572 static_always_inline nat44_ei_outside_fib_t *
573 nat44_ei_get_outside_fib (nat44_ei_outside_fib_t *outside_fibs, u32 fib_index)
575 nat44_ei_outside_fib_t *f;
576 vec_foreach (f, outside_fibs)
578 if (f->fib_index == fib_index)
586 static_always_inline nat44_ei_interface_t *
587 nat44_ei_get_interface (nat44_ei_interface_t *interfaces, u32 sw_if_index)
589 nat44_ei_interface_t *i;
590 pool_foreach (i, interfaces)
592 if (i->sw_if_index == sw_if_index)
600 static_always_inline int
601 nat44_ei_hairpinning_enable (u8 is_enable)
603 nat44_ei_main_t *nm = &nat44_ei_main;
604 u32 sw_if_index = 0; // local0
608 nm->hairpin_reg += 1;
609 if (1 == nm->hairpin_reg)
611 return vnet_feature_enable_disable (
612 "ip4-local", "nat44-ei-hairpinning", sw_if_index, is_enable, 0, 0);
617 if (0 == nm->hairpin_reg)
620 nm->hairpin_reg -= 1;
621 if (0 == nm->hairpin_reg)
623 return vnet_feature_enable_disable (
624 "ip4-local", "nat44-ei-hairpinning", sw_if_index, is_enable, 0, 0);
632 nat44_ei_add_interface (u32 sw_if_index, u8 is_inside)
634 const char *feature_name, *del_feature_name;
635 nat44_ei_main_t *nm = &nat44_ei_main;
637 nat44_ei_outside_fib_t *outside_fib;
638 nat44_ei_interface_t *i;
644 if (nm->out2in_dpo && !is_inside)
646 nat44_ei_log_err ("error unsupported");
647 return VNET_API_ERROR_UNSUPPORTED;
650 if (nat44_ei_get_interface (nm->output_feature_interfaces, sw_if_index))
652 nat44_ei_log_err ("error interface already configured");
653 return VNET_API_ERROR_VALUE_EXIST;
656 i = nat44_ei_get_interface (nm->interfaces, sw_if_index);
659 if ((nat44_ei_interface_is_inside (i) && is_inside) ||
660 (nat44_ei_interface_is_outside (i) && !is_inside))
664 if (nm->num_workers > 1)
666 del_feature_name = !is_inside ? "nat44-ei-in2out-worker-handoff" :
667 "nat44-ei-out2in-worker-handoff";
668 feature_name = "nat44-ei-handoff-classify";
673 !is_inside ? "nat44-ei-in2out" : "nat44-ei-out2in";
675 feature_name = "nat44-ei-classify";
678 rv = ip4_sv_reass_enable_disable_with_refcnt (sw_if_index, 1);
683 rv = vnet_feature_enable_disable ("ip4-unicast", del_feature_name,
684 sw_if_index, 0, 0, 0);
689 rv = vnet_feature_enable_disable ("ip4-unicast", feature_name,
690 sw_if_index, 1, 0, 0);
697 rv = nat44_ei_hairpinning_enable (0);
706 if (nm->num_workers > 1)
708 feature_name = is_inside ? "nat44-ei-in2out-worker-handoff" :
709 "nat44-ei-out2in-worker-handoff";
713 feature_name = is_inside ? "nat44-ei-in2out" : "nat44-ei-out2in";
715 nat_validate_interface_counters (nm, sw_if_index);
717 rv = ip4_sv_reass_enable_disable_with_refcnt (sw_if_index, 1);
722 rv = vnet_feature_enable_disable ("ip4-unicast", feature_name,
723 sw_if_index, 1, 0, 0);
728 if (is_inside && !nm->out2in_dpo)
730 rv = nat44_ei_hairpinning_enable (1);
737 pool_get (nm->interfaces, i);
738 i->sw_if_index = sw_if_index;
743 fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4, sw_if_index);
747 i->flags |= NAT44_EI_INTERFACE_FLAG_IS_OUTSIDE;
749 outside_fib = nat44_ei_get_outside_fib (nm->outside_fibs, fib_index);
752 outside_fib->refcount++;
756 vec_add2 (nm->outside_fibs, outside_fib, 1);
757 outside_fib->fib_index = fib_index;
758 outside_fib->refcount = 1;
761 nat44_ei_add_del_addr_to_fib_foreach_addr (sw_if_index, 1);
762 nat44_ei_add_del_addr_to_fib_foreach_addr_only_sm (sw_if_index, 1);
766 i->flags |= NAT44_EI_INTERFACE_FLAG_IS_INSIDE;
773 nat44_ei_del_interface (u32 sw_if_index, u8 is_inside)
775 const char *feature_name, *del_feature_name;
776 nat44_ei_main_t *nm = &nat44_ei_main;
778 nat44_ei_outside_fib_t *outside_fib;
779 nat44_ei_interface_t *i;
785 if (nm->out2in_dpo && !is_inside)
787 nat44_ei_log_err ("error unsupported");
788 return VNET_API_ERROR_UNSUPPORTED;
791 i = nat44_ei_get_interface (nm->interfaces, sw_if_index);
794 nat44_ei_log_err ("error interface couldn't be found");
795 return VNET_API_ERROR_NO_SUCH_ENTRY;
798 if (nat44_ei_interface_is_inside (i) && nat44_ei_interface_is_outside (i))
800 if (nm->num_workers > 1)
802 del_feature_name = "nat44-ei-handoff-classify";
803 feature_name = !is_inside ? "nat44-ei-in2out-worker-handoff" :
804 "nat44-ei-out2in-worker-handoff";
808 del_feature_name = "nat44-ei-classify";
809 feature_name = !is_inside ? "nat44-ei-in2out" : "nat44-ei-out2in";
812 rv = ip4_sv_reass_enable_disable_with_refcnt (sw_if_index, 0);
817 rv = vnet_feature_enable_disable ("ip4-unicast", del_feature_name,
818 sw_if_index, 0, 0, 0);
823 rv = vnet_feature_enable_disable ("ip4-unicast", feature_name,
824 sw_if_index, 1, 0, 0);
831 i->flags &= ~NAT44_EI_INTERFACE_FLAG_IS_INSIDE;
835 rv = nat44_ei_hairpinning_enable (1);
840 i->flags &= ~NAT44_EI_INTERFACE_FLAG_IS_OUTSIDE;
845 if (nm->num_workers > 1)
847 feature_name = is_inside ? "nat44-ei-in2out-worker-handoff" :
848 "nat44-ei-out2in-worker-handoff";
852 feature_name = is_inside ? "nat44-ei-in2out" : "nat44-ei-out2in";
855 rv = ip4_sv_reass_enable_disable_with_refcnt (sw_if_index, 0);
860 rv = vnet_feature_enable_disable ("ip4-unicast", feature_name,
861 sw_if_index, 0, 0, 0);
868 rv = nat44_ei_hairpinning_enable (0);
876 pool_put (nm->interfaces, i);
882 fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4, sw_if_index);
883 outside_fib = nat44_ei_get_outside_fib (nm->outside_fibs, fib_index);
886 outside_fib->refcount--;
887 if (!outside_fib->refcount)
889 vec_del1 (nm->outside_fibs, outside_fib - nm->outside_fibs);
893 nat44_ei_add_del_addr_to_fib_foreach_addr (sw_if_index, 0);
894 nat44_ei_add_del_addr_to_fib_foreach_addr_only_sm (sw_if_index, 0);
901 nat44_ei_add_output_interface (u32 sw_if_index)
903 nat44_ei_main_t *nm = &nat44_ei_main;
905 nat44_ei_outside_fib_t *outside_fib;
906 nat44_ei_interface_t *i;
912 if (nat44_ei_get_interface (nm->interfaces, sw_if_index))
914 nat44_ei_log_err ("error interface already configured");
915 return VNET_API_ERROR_VALUE_EXIST;
918 if (nat44_ei_get_interface (nm->output_feature_interfaces, sw_if_index))
920 nat44_ei_log_err ("error interface already configured");
921 return VNET_API_ERROR_VALUE_EXIST;
924 if (nm->num_workers > 1)
926 rv = ip4_sv_reass_enable_disable_with_refcnt (sw_if_index, 1);
931 rv = ip4_sv_reass_output_enable_disable_with_refcnt (sw_if_index, 1);
936 rv = vnet_feature_enable_disable (
937 "ip4-unicast", "nat44-ei-out2in-worker-handoff", sw_if_index, 1, 0, 0);
942 rv = vnet_feature_enable_disable (
943 "ip4-output", "nat44-ei-in2out-output-worker-handoff", sw_if_index, 1,
952 rv = ip4_sv_reass_enable_disable_with_refcnt (sw_if_index, 1);
957 rv = ip4_sv_reass_output_enable_disable_with_refcnt (sw_if_index, 1);
962 rv = vnet_feature_enable_disable ("ip4-unicast", "nat44-ei-out2in",
963 sw_if_index, 1, 0, 0);
968 rv = vnet_feature_enable_disable ("ip4-output", "nat44-ei-in2out-output",
969 sw_if_index, 1, 0, 0);
976 nat_validate_interface_counters (nm, sw_if_index);
978 pool_get (nm->output_feature_interfaces, i);
979 i->sw_if_index = sw_if_index;
981 i->flags |= NAT44_EI_INTERFACE_FLAG_IS_INSIDE;
982 i->flags |= NAT44_EI_INTERFACE_FLAG_IS_OUTSIDE;
985 fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4, sw_if_index);
986 outside_fib = nat44_ei_get_outside_fib (nm->outside_fibs, fib_index);
989 outside_fib->refcount++;
993 vec_add2 (nm->outside_fibs, outside_fib, 1);
994 outside_fib->fib_index = fib_index;
995 outside_fib->refcount = 1;
998 nat44_ei_add_del_addr_to_fib_foreach_addr (sw_if_index, 1);
999 nat44_ei_add_del_addr_to_fib_foreach_addr_only_sm (sw_if_index, 1);
1005 nat44_ei_del_output_interface (u32 sw_if_index)
1007 nat44_ei_main_t *nm = &nat44_ei_main;
1009 nat44_ei_outside_fib_t *outside_fib;
1010 nat44_ei_interface_t *i;
1014 fail_if_disabled ();
1016 i = nat44_ei_get_interface (nm->output_feature_interfaces, sw_if_index);
1019 nat44_ei_log_err ("error interface couldn't be found");
1020 return VNET_API_ERROR_NO_SUCH_ENTRY;
1023 if (nm->num_workers > 1)
1025 rv = ip4_sv_reass_enable_disable_with_refcnt (sw_if_index, 0);
1030 rv = ip4_sv_reass_output_enable_disable_with_refcnt (sw_if_index, 0);
1035 rv = vnet_feature_enable_disable (
1036 "ip4-unicast", "nat44-ei-out2in-worker-handoff", sw_if_index, 0, 0, 0);
1041 rv = vnet_feature_enable_disable (
1042 "ip4-output", "nat44-ei-in2out-output-worker-handoff", sw_if_index, 0,
1051 rv = ip4_sv_reass_enable_disable_with_refcnt (sw_if_index, 0);
1056 rv = ip4_sv_reass_output_enable_disable_with_refcnt (sw_if_index, 0);
1061 rv = vnet_feature_enable_disable ("ip4-unicast", "nat44-ei-out2in",
1062 sw_if_index, 0, 0, 0);
1067 rv = vnet_feature_enable_disable ("ip4-output", "nat44-ei-in2out-output",
1068 sw_if_index, 0, 0, 0);
1075 pool_put (nm->output_feature_interfaces, i);
1078 fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4, sw_if_index);
1079 outside_fib = nat44_ei_get_outside_fib (nm->outside_fibs, fib_index);
1082 outside_fib->refcount--;
1083 if (!outside_fib->refcount)
1085 vec_del1 (nm->outside_fibs, outside_fib - nm->outside_fibs);
1089 nat44_ei_add_del_addr_to_fib_foreach_addr (sw_if_index, 0);
1090 nat44_ei_add_del_addr_to_fib_foreach_addr_only_sm (sw_if_index, 0);
1096 nat44_ei_add_del_output_interface (u32 sw_if_index, int is_del)
1100 return nat44_ei_del_output_interface (sw_if_index);
1104 return nat44_ei_add_output_interface (sw_if_index);
1109 nat44_ei_del_addresses ()
1111 nat44_ei_main_t *nm = &nat44_ei_main;
1112 nat44_ei_address_t *a, *vec;
1115 vec = vec_dup (nm->addresses);
1116 vec_foreach (a, vec)
1118 error = nat44_ei_del_address (a->addr, 0);
1122 nat44_ei_log_err ("error occurred while removing adderess");
1126 vec_free (nm->addresses);
1129 vec_free (nm->auto_add_sw_if_indices);
1130 nm->auto_add_sw_if_indices = 0;
1135 nat44_ei_del_interfaces ()
1137 nat44_ei_main_t *nm = &nat44_ei_main;
1138 nat44_ei_interface_t *i, *pool;
1141 pool = pool_dup (nm->interfaces);
1142 pool_foreach (i, pool)
1144 if (nat44_ei_interface_is_inside (i))
1146 error = nat44_ei_del_interface (i->sw_if_index, 1);
1148 if (nat44_ei_interface_is_outside (i))
1150 error = nat44_ei_del_interface (i->sw_if_index, 0);
1155 nat44_ei_log_err ("error occurred while removing interface");
1159 pool_free (nm->interfaces);
1165 nat44_ei_del_output_interfaces ()
1167 nat44_ei_main_t *nm = &nat44_ei_main;
1168 nat44_ei_interface_t *i, *pool;
1171 pool = pool_dup (nm->output_feature_interfaces);
1172 pool_foreach (i, pool)
1174 error = nat44_ei_del_output_interface (i->sw_if_index);
1177 nat44_ei_log_err ("error occurred while removing output interface");
1181 pool_free (nm->output_feature_interfaces);
1182 nm->output_feature_interfaces = 0;
1186 static clib_error_t *
1187 nat44_ei_sw_interface_add_del (vnet_main_t *vnm, u32 sw_if_index, u32 is_add)
1189 nat44_ei_main_t *nm = &nat44_ei_main;
1190 nat44_ei_interface_t *i;
1199 i = nat44_ei_get_interface (nm->interfaces, sw_if_index);
1202 bool is_inside = nat44_ei_interface_is_inside (i);
1203 bool is_outside = nat44_ei_interface_is_outside (i);
1207 error |= nat44_ei_del_interface (sw_if_index, 1);
1211 error |= nat44_ei_del_interface (sw_if_index, 0);
1216 nat44_ei_log_err ("error occurred while removing interface");
1220 i = nat44_ei_get_interface (nm->output_feature_interfaces, sw_if_index);
1223 error = nat44_ei_del_output_interface (sw_if_index);
1226 nat44_ei_log_err ("error occurred while removing output interface");
1233 VNET_SW_INTERFACE_ADD_DEL_FUNCTION (nat44_ei_sw_interface_add_del);
1236 nat44_ei_del_static_mappings ()
1238 nat44_ei_main_t *nm = &nat44_ei_main;
1239 nat44_ei_static_mapping_t *m, *pool;
1242 pool = pool_dup (nm->static_mappings);
1243 pool_foreach (m, pool)
1245 error = nat44_ei_del_static_mapping_internal (
1246 m->local_addr, m->external_addr, m->local_port, m->external_port,
1247 m->proto, m->vrf_id, ~0, m->flags);
1250 nat44_ei_log_err ("error occurred while removing mapping");
1254 pool_free (nm->static_mappings);
1255 nm->static_mappings = 0;
1257 vec_free (nm->to_resolve);
1260 clib_bihash_free_8_8 (&nm->static_mapping_by_local);
1261 clib_bihash_free_8_8 (&nm->static_mapping_by_external);
1267 nat44_ei_plugin_disable ()
1269 nat44_ei_main_t *nm = &nat44_ei_main;
1270 nat44_ei_main_per_thread_data_t *tnm;
1273 fail_if_disabled ();
1277 rc = nat44_ei_del_static_mappings ();
1279 error = VNET_API_ERROR_BUG;
1281 rc = nat44_ei_del_addresses ();
1283 error = VNET_API_ERROR_BUG;
1285 rc = nat44_ei_del_interfaces ();
1287 error = VNET_API_ERROR_BUG;
1289 rc = nat44_ei_del_output_interfaces ();
1291 error = VNET_API_ERROR_BUG;
1295 clib_bihash_free_8_8 (&nm->in2out);
1296 clib_bihash_free_8_8 (&nm->out2in);
1298 vec_foreach (tnm, nm->per_thread_data)
1300 nat44_ei_worker_db_free (tnm);
1304 clib_memset (&nm->rconfig, 0, sizeof (nm->rconfig));
1306 nm->forwarding_enabled = 0;
1313 nat44_ei_set_outside_address_and_port (nat44_ei_address_t *addresses,
1314 u32 thread_index, ip4_address_t addr,
1315 u16 port, nat_protocol_t protocol)
1317 nat44_ei_address_t *a = 0;
1319 u16 port_host_byte_order = clib_net_to_host_u16 (port);
1321 for (address_index = 0; address_index < vec_len (addresses); address_index++)
1323 if (addresses[address_index].addr.as_u32 != addr.as_u32)
1326 a = addresses + address_index;
1327 if (nat44_ei_port_is_used (a, protocol, port_host_byte_order))
1328 return VNET_API_ERROR_INSTANCE_IN_USE;
1330 nat44_ei_port_get (a, protocol, port_host_byte_order);
1331 a->busy_ports_per_thread[protocol][thread_index]++;
1332 a->busy_ports[protocol]++;
1336 return VNET_API_ERROR_NO_SUCH_ENTRY;
1340 nat44_ei_add_del_address_dpo (ip4_address_t addr, u8 is_add)
1342 nat44_ei_main_t *nm = &nat44_ei_main;
1343 dpo_id_t dpo_v4 = DPO_INVALID;
1344 fib_prefix_t pfx = {
1345 .fp_proto = FIB_PROTOCOL_IP4,
1347 .fp_addr.ip4.as_u32 = addr.as_u32,
1352 nat_dpo_create (DPO_PROTO_IP4, 0, &dpo_v4);
1353 fib_table_entry_special_dpo_add (0, &pfx, nm->fib_src_hi,
1354 FIB_ENTRY_FLAG_EXCLUSIVE, &dpo_v4);
1355 dpo_reset (&dpo_v4);
1359 fib_table_entry_special_remove (0, &pfx, nm->fib_src_hi);
1364 nat44_ei_free_outside_address_and_port (nat44_ei_address_t *addresses,
1365 u32 thread_index, ip4_address_t *addr,
1366 u16 port, nat_protocol_t protocol)
1368 nat44_ei_address_t *a;
1370 u16 port_host_byte_order = clib_net_to_host_u16 (port);
1372 for (address_index = 0; address_index < vec_len (addresses); address_index++)
1374 if (addresses[address_index].addr.as_u32 == addr->as_u32)
1378 ASSERT (address_index < vec_len (addresses));
1380 a = addresses + address_index;
1381 nat44_ei_port_put (a, protocol, port_host_byte_order);
1382 a->busy_ports[protocol]--;
1383 a->busy_ports_per_thread[protocol][thread_index]--;
1387 nat44_ei_free_session_data_v2 (nat44_ei_main_t *nm, nat44_ei_session_t *s,
1388 u32 thread_index, u8 is_ha)
1390 clib_bihash_kv_8_8_t kv;
1392 /* session lookup tables */
1393 init_nat_i2o_k (&kv, s);
1394 if (clib_bihash_add_del_8_8 (&nm->in2out, &kv, 0))
1395 nat_elog_warn (nm, "in2out key del failed");
1396 init_nat_o2i_k (&kv, s);
1397 if (clib_bihash_add_del_8_8 (&nm->out2in, &kv, 0))
1398 nat_elog_warn (nm, "out2in key del failed");
1401 nat_syslog_nat44_apmdel (s->user_index, s->in2out.fib_index,
1402 &s->in2out.addr, s->in2out.port, &s->out2in.addr,
1403 s->out2in.port, s->nat_proto);
1405 if (nat44_ei_is_unk_proto_session (s))
1411 nat_ipfix_logging_nat44_ses_delete (
1412 thread_index, s->in2out.addr.as_u32, s->out2in.addr.as_u32,
1413 nat_proto_to_ip_proto (s->nat_proto), s->in2out.port, s->out2in.port,
1414 s->in2out.fib_index);
1416 nat_ha_sdel (&s->out2in.addr, s->out2in.port, &s->ext_host_addr,
1417 s->ext_host_port, s->nat_proto, s->out2in.fib_index,
1421 if (nat44_ei_is_session_static (s))
1424 nat44_ei_free_outside_address_and_port (nm->addresses, thread_index,
1425 &s->out2in.addr, s->out2in.port,
1430 nat44_ei_user_get_or_create (nat44_ei_main_t *nm, ip4_address_t *addr,
1431 u32 fib_index, u32 thread_index)
1433 nat44_ei_user_t *u = 0;
1434 nat44_ei_user_key_t user_key;
1435 clib_bihash_kv_8_8_t kv, value;
1436 nat44_ei_main_per_thread_data_t *tnm = &nm->per_thread_data[thread_index];
1437 dlist_elt_t *per_user_list_head_elt;
1439 user_key.addr.as_u32 = addr->as_u32;
1440 user_key.fib_index = fib_index;
1441 kv.key = user_key.as_u64;
1443 /* Ever heard of the "user" = src ip4 address before? */
1444 if (clib_bihash_search_8_8 (&tnm->user_hash, &kv, &value))
1446 if (pool_elts (tnm->users) >= nm->max_users_per_thread)
1448 vlib_increment_simple_counter (&nm->user_limit_reached, thread_index,
1450 nat_elog_warn (nm, "maximum user limit reached");
1453 /* no, make a new one */
1454 pool_get (tnm->users, u);
1455 clib_memset (u, 0, sizeof (*u));
1457 u->addr.as_u32 = addr->as_u32;
1458 u->fib_index = fib_index;
1460 pool_get (tnm->list_pool, per_user_list_head_elt);
1462 u->sessions_per_user_list_head_index =
1463 per_user_list_head_elt - tnm->list_pool;
1465 clib_dlist_init (tnm->list_pool, u->sessions_per_user_list_head_index);
1467 kv.value = u - tnm->users;
1470 if (clib_bihash_add_del_8_8 (&tnm->user_hash, &kv, 1))
1472 nat_elog_warn (nm, "user_hash key add failed");
1473 nat44_ei_delete_user_with_no_session (nm, u, thread_index);
1477 vlib_set_simple_counter (&nm->total_users, thread_index, 0,
1478 pool_elts (tnm->users));
1482 u = pool_elt_at_index (tnm->users, value.value);
1488 nat44_ei_session_t *
1489 nat44_ei_session_alloc_or_recycle (nat44_ei_main_t *nm, nat44_ei_user_t *u,
1490 u32 thread_index, f64 now)
1492 nat44_ei_session_t *s;
1493 nat44_ei_main_per_thread_data_t *tnm = &nm->per_thread_data[thread_index];
1494 u32 oldest_per_user_translation_list_index, session_index;
1495 dlist_elt_t *oldest_per_user_translation_list_elt;
1496 dlist_elt_t *per_user_translation_list_elt;
1498 /* Over quota? Recycle the least recently used translation */
1499 if ((u->nsessions + u->nstaticsessions) >= nm->max_translations_per_user)
1501 oldest_per_user_translation_list_index = clib_dlist_remove_head (
1502 tnm->list_pool, u->sessions_per_user_list_head_index);
1504 ASSERT (oldest_per_user_translation_list_index != ~0);
1506 /* Add it back to the end of the LRU list */
1507 clib_dlist_addtail (tnm->list_pool, u->sessions_per_user_list_head_index,
1508 oldest_per_user_translation_list_index);
1509 /* Get the list element */
1510 oldest_per_user_translation_list_elt = pool_elt_at_index (
1511 tnm->list_pool, oldest_per_user_translation_list_index);
1513 /* Get the session index from the list element */
1514 session_index = oldest_per_user_translation_list_elt->value;
1516 /* Get the session */
1517 s = pool_elt_at_index (tnm->sessions, session_index);
1519 nat44_ei_free_session_data_v2 (nm, s, thread_index, 0);
1520 if (nat44_ei_is_session_static (s))
1521 u->nstaticsessions--;
1528 s->ext_host_addr.as_u32 = 0;
1529 s->ext_host_port = 0;
1530 s->ext_host_nat_addr.as_u32 = 0;
1531 s->ext_host_nat_port = 0;
1535 pool_get (tnm->sessions, s);
1536 clib_memset (s, 0, sizeof (*s));
1538 /* Create list elts */
1539 pool_get (tnm->list_pool, per_user_translation_list_elt);
1540 clib_dlist_init (tnm->list_pool,
1541 per_user_translation_list_elt - tnm->list_pool);
1543 per_user_translation_list_elt->value = s - tnm->sessions;
1544 s->per_user_index = per_user_translation_list_elt - tnm->list_pool;
1545 s->per_user_list_head_index = u->sessions_per_user_list_head_index;
1547 clib_dlist_addtail (tnm->list_pool, s->per_user_list_head_index,
1548 per_user_translation_list_elt - tnm->list_pool);
1550 s->user_index = u - tnm->users;
1551 vlib_set_simple_counter (&nm->total_sessions, thread_index, 0,
1552 pool_elts (tnm->sessions));
1555 s->ha_last_refreshed = now;
1561 nat44_ei_free_session_data (nat44_ei_main_t *nm, nat44_ei_session_t *s,
1562 u32 thread_index, u8 is_ha)
1564 clib_bihash_kv_8_8_t kv;
1566 init_nat_i2o_k (&kv, s);
1567 if (clib_bihash_add_del_8_8 (&nm->in2out, &kv, 0))
1568 nat_elog_warn (nm, "in2out key del failed");
1570 init_nat_o2i_k (&kv, s);
1571 if (clib_bihash_add_del_8_8 (&nm->out2in, &kv, 0))
1572 nat_elog_warn (nm, "out2in key del failed");
1576 nat_syslog_nat44_apmdel (s->user_index, s->in2out.fib_index,
1577 &s->in2out.addr, s->in2out.port,
1578 &s->out2in.addr, s->out2in.port, s->nat_proto);
1580 nat_ipfix_logging_nat44_ses_delete (
1581 thread_index, s->in2out.addr.as_u32, s->out2in.addr.as_u32,
1582 nat_proto_to_ip_proto (s->nat_proto), s->in2out.port, s->out2in.port,
1583 s->in2out.fib_index);
1585 nat_ha_sdel (&s->out2in.addr, s->out2in.port, &s->ext_host_addr,
1586 s->ext_host_port, s->nat_proto, s->out2in.fib_index,
1590 if (nat44_ei_is_session_static (s))
1593 nat44_ei_free_outside_address_and_port (nm->addresses, thread_index,
1594 &s->out2in.addr, s->out2in.port,
1598 static_always_inline void
1599 nat44_ei_user_del_sessions (nat44_ei_user_t *u, u32 thread_index)
1602 nat44_ei_session_t *s;
1604 nat44_ei_main_t *nm = &nat44_ei_main;
1605 nat44_ei_main_per_thread_data_t *tnm = &nm->per_thread_data[thread_index];
1609 pool_elt_at_index (tnm->list_pool, u->sessions_per_user_list_head_index);
1610 // get first element
1611 elt = pool_elt_at_index (tnm->list_pool, elt->next);
1613 while (elt->value != ~0)
1615 s = pool_elt_at_index (tnm->sessions, elt->value);
1616 elt = pool_elt_at_index (tnm->list_pool, elt->next);
1618 nat44_ei_free_session_data (nm, s, thread_index, 0);
1619 nat44_ei_delete_session (nm, s, thread_index);
1624 nat44_ei_user_del (ip4_address_t *addr, u32 fib_index)
1628 nat44_ei_main_t *nm = &nat44_ei_main;
1629 nat44_ei_main_per_thread_data_t *tnm;
1631 nat44_ei_user_key_t user_key;
1632 clib_bihash_kv_8_8_t kv, value;
1634 user_key.addr.as_u32 = addr->as_u32;
1635 user_key.fib_index = fib_index;
1636 kv.key = user_key.as_u64;
1638 if (nm->num_workers > 1)
1640 vec_foreach (tnm, nm->per_thread_data)
1642 if (!clib_bihash_search_8_8 (&tnm->user_hash, &kv, &value))
1644 nat44_ei_user_del_sessions (
1645 pool_elt_at_index (tnm->users, value.value),
1654 tnm = vec_elt_at_index (nm->per_thread_data, nm->num_workers);
1655 if (!clib_bihash_search_8_8 (&tnm->user_hash, &kv, &value))
1657 nat44_ei_user_del_sessions (
1658 pool_elt_at_index (tnm->users, value.value), tnm->thread_index);
1666 nat44_ei_static_mapping_del_sessions (nat44_ei_main_t *nm,
1667 nat44_ei_main_per_thread_data_t *tnm,
1668 nat44_ei_user_key_t u_key, int addr_only,
1669 ip4_address_t e_addr, u16 e_port)
1671 clib_bihash_kv_8_8_t kv, value;
1672 kv.key = u_key.as_u64;
1674 dlist_elt_t *head, *elt;
1676 nat44_ei_session_t *s;
1677 u32 elt_index, head_index, ses_index;
1679 if (!clib_bihash_search_8_8 (&tnm->user_hash, &kv, &value))
1681 user_index = value.value;
1682 u = pool_elt_at_index (tnm->users, user_index);
1683 if (u->nstaticsessions)
1685 head_index = u->sessions_per_user_list_head_index;
1686 head = pool_elt_at_index (tnm->list_pool, head_index);
1687 elt_index = head->next;
1688 elt = pool_elt_at_index (tnm->list_pool, elt_index);
1689 ses_index = elt->value;
1690 while (ses_index != ~0)
1692 s = pool_elt_at_index (tnm->sessions, ses_index);
1693 elt = pool_elt_at_index (tnm->list_pool, elt->next);
1694 ses_index = elt->value;
1698 if ((s->out2in.addr.as_u32 != e_addr.as_u32) ||
1699 (s->out2in.port != e_port))
1703 if (!nat44_ei_is_session_static (s))
1706 nat44_ei_free_session_data_v2 (nm, s, tnm - nm->per_thread_data,
1708 nat44_ei_delete_session (nm, s, tnm - nm->per_thread_data);
1718 nat44_ei_get_in2out_worker_index (ip4_header_t *ip0, u32 rx_fib_index0,
1721 nat44_ei_main_t *nm = &nat44_ei_main;
1722 u32 next_worker_index = 0;
1725 next_worker_index = nm->first_worker_index;
1726 hash = ip0->src_address.as_u32 + (ip0->src_address.as_u32 >> 8) +
1727 (ip0->src_address.as_u32 >> 16) + (ip0->src_address.as_u32 >> 24);
1729 if (PREDICT_TRUE (is_pow2 (_vec_len (nm->workers))))
1730 next_worker_index += nm->workers[hash & (_vec_len (nm->workers) - 1)];
1732 next_worker_index += nm->workers[hash % _vec_len (nm->workers)];
1734 return next_worker_index;
1738 nat44_ei_get_thread_idx_by_port (u16 e_port)
1740 nat44_ei_main_t *nm = &nat44_ei_main;
1741 u32 thread_idx = nm->num_workers;
1742 if (nm->num_workers > 1)
1744 thread_idx = nm->first_worker_index +
1745 nm->workers[(e_port - 1024) / nm->port_per_thread %
1746 _vec_len (nm->workers)];
1752 nat44_ei_get_out2in_worker_index (vlib_buffer_t *b, ip4_header_t *ip0,
1753 u32 rx_fib_index0, u8 is_output)
1755 nat44_ei_main_t *nm = &nat44_ei_main;
1758 clib_bihash_kv_8_8_t kv, value;
1759 nat44_ei_static_mapping_t *m;
1761 u32 next_worker_index = 0;
1763 /* first try static mappings without port */
1764 if (PREDICT_FALSE (pool_elts (nm->static_mappings)))
1766 init_nat_k (&kv, ip0->dst_address, 0, rx_fib_index0, 0);
1767 if (!clib_bihash_search_8_8 (&nm->static_mapping_by_external, &kv,
1770 m = pool_elt_at_index (nm->static_mappings, value.value);
1771 return m->workers[0];
1775 proto = ip_proto_to_nat_proto (ip0->protocol);
1776 udp = ip4_next_header (ip0);
1777 port = vnet_buffer (b)->ip.reass.l4_dst_port;
1779 /* unknown protocol */
1780 if (PREDICT_FALSE (proto == NAT_PROTOCOL_OTHER))
1782 /* use current thread */
1783 return vlib_get_thread_index ();
1786 if (PREDICT_FALSE (ip0->protocol == IP_PROTOCOL_ICMP))
1788 icmp46_header_t *icmp = (icmp46_header_t *) udp;
1789 icmp_echo_header_t *echo = (icmp_echo_header_t *) (icmp + 1);
1790 if (!icmp_type_is_error_message (
1791 vnet_buffer (b)->ip.reass.icmp_type_or_tcp_flags))
1792 port = vnet_buffer (b)->ip.reass.l4_src_port;
1795 /* if error message, then it's not fragmented and we can access it */
1796 ip4_header_t *inner_ip = (ip4_header_t *) (echo + 1);
1797 proto = ip_proto_to_nat_proto (inner_ip->protocol);
1798 void *l4_header = ip4_next_header (inner_ip);
1801 case NAT_PROTOCOL_ICMP:
1802 icmp = (icmp46_header_t *) l4_header;
1803 echo = (icmp_echo_header_t *) (icmp + 1);
1804 port = echo->identifier;
1806 case NAT_PROTOCOL_UDP:
1807 case NAT_PROTOCOL_TCP:
1808 port = ((tcp_udp_header_t *) l4_header)->src_port;
1811 return vlib_get_thread_index ();
1816 /* try static mappings with port */
1817 if (PREDICT_FALSE (pool_elts (nm->static_mappings)))
1819 init_nat_k (&kv, ip0->dst_address, port, rx_fib_index0, proto);
1820 if (!clib_bihash_search_8_8 (&nm->static_mapping_by_external, &kv,
1823 m = pool_elt_at_index (nm->static_mappings, value.value);
1824 return m->workers[0];
1828 /* worker by outside port */
1830 nat44_ei_get_thread_idx_by_port (clib_net_to_host_u16 (port));
1831 return next_worker_index;
1835 nat44_ei_alloc_default_cb (nat44_ei_address_t *addresses, u32 fib_index,
1836 u32 thread_index, nat_protocol_t proto,
1837 ip4_address_t s_addr, ip4_address_t *addr,
1838 u16 *port, u16 port_per_thread,
1839 u32 snat_thread_index)
1841 nat44_ei_main_t *nm = &nat44_ei_main;
1842 nat44_ei_address_t *a, *ga = 0;
1846 if (vec_len (addresses) > 0)
1848 int s_addr_offset = s_addr.as_u32 % vec_len (addresses);
1850 for (i = s_addr_offset; i < vec_len (addresses); ++i)
1854 if (a->busy_ports_per_thread[proto][thread_index] < port_per_thread)
1856 if (a->fib_index == fib_index)
1860 portnum = (port_per_thread * snat_thread_index) +
1861 nat_random_port (&nm->random_seed, 0,
1862 port_per_thread - 1) +
1864 if (nat44_ei_port_is_used (a, proto, portnum))
1866 nat44_ei_port_get (a, proto, portnum);
1867 a->busy_ports_per_thread[proto][thread_index]++;
1868 a->busy_ports[proto]++;
1870 *port = clib_host_to_net_u16 (portnum);
1874 else if (a->fib_index == ~0)
1881 for (i = 0; i < s_addr_offset; ++i)
1884 if (a->busy_ports_per_thread[proto][thread_index] < port_per_thread)
1886 if (a->fib_index == fib_index)
1890 portnum = (port_per_thread * snat_thread_index) +
1891 nat_random_port (&nm->random_seed, 0,
1892 port_per_thread - 1) +
1894 if (nat44_ei_port_is_used (a, proto, portnum))
1896 nat44_ei_port_get (a, proto, portnum);
1897 a->busy_ports_per_thread[proto][thread_index]++;
1898 a->busy_ports[proto]++;
1900 *port = clib_host_to_net_u16 (portnum);
1904 else if (a->fib_index == ~0)
1914 if (a->busy_ports_per_thread[proto][thread_index] < port_per_thread)
1916 if (a->fib_index == ~0)
1920 portnum = (port_per_thread * snat_thread_index) +
1921 nat_random_port (&nm->random_seed, 0,
1922 port_per_thread - 1) +
1924 if (nat44_ei_port_is_used (a, proto, portnum))
1926 nat44_ei_port_get (a, proto, portnum);
1927 a->busy_ports_per_thread[proto][thread_index]++;
1928 a->busy_ports[proto]++;
1930 *port = clib_host_to_net_u16 (portnum);
1938 /* Totally out of translations to use... */
1939 nat_ipfix_logging_addresses_exhausted (thread_index, 0);
1944 nat44_ei_alloc_range_cb (nat44_ei_address_t *addresses, u32 fib_index,
1945 u32 thread_index, nat_protocol_t proto,
1946 ip4_address_t s_addr, ip4_address_t *addr, u16 *port,
1947 u16 port_per_thread, u32 snat_thread_index)
1949 nat44_ei_main_t *nm = &nat44_ei_main;
1950 nat44_ei_address_t *a = addresses;
1953 ports = nm->end_port - nm->start_port + 1;
1955 if (!vec_len (addresses))
1958 if (a->busy_ports[proto] < ports)
1963 nat_random_port (&nm->random_seed, nm->start_port, nm->end_port);
1964 if (nat44_ei_port_is_used (a, proto, portnum))
1966 nat44_ei_port_get (a, proto, portnum);
1967 a->busy_ports[proto]++;
1969 *port = clib_host_to_net_u16 (portnum);
1975 /* Totally out of translations to use... */
1976 nat_ipfix_logging_addresses_exhausted (thread_index, 0);
1981 nat44_ei_alloc_mape_cb (nat44_ei_address_t *addresses, u32 fib_index,
1982 u32 thread_index, nat_protocol_t proto,
1983 ip4_address_t s_addr, ip4_address_t *addr, u16 *port,
1984 u16 port_per_thread, u32 snat_thread_index)
1986 nat44_ei_main_t *nm = &nat44_ei_main;
1987 nat44_ei_address_t *a = addresses;
1988 u16 m, ports, portnum, A, j;
1989 m = 16 - (nm->psid_offset + nm->psid_length);
1990 ports = (1 << (16 - nm->psid_length)) - (1 << m);
1992 if (!vec_len (addresses))
1995 if (a->busy_ports[proto] < ports)
2000 nat_random_port (&nm->random_seed, 1, pow2_mask (nm->psid_offset));
2001 j = nat_random_port (&nm->random_seed, 0, pow2_mask (m));
2002 portnum = A | (nm->psid << nm->psid_offset) | (j << (16 - m));
2003 if (nat44_ei_port_is_used (a, proto, portnum))
2005 nat44_ei_port_get (a, proto, portnum);
2006 a->busy_ports[proto]++;
2008 *port = clib_host_to_net_u16 (portnum);
2014 /* Totally out of translations to use... */
2015 nat_ipfix_logging_addresses_exhausted (thread_index, 0);
2020 nat44_ei_set_alloc_default ()
2022 nat44_ei_main_t *nm = &nat44_ei_main;
2024 nm->addr_and_port_alloc_alg = NAT44_EI_ADDR_AND_PORT_ALLOC_ALG_DEFAULT;
2025 nm->alloc_addr_and_port = nat44_ei_alloc_default_cb;
2029 nat44_ei_set_alloc_range (u16 start_port, u16 end_port)
2031 nat44_ei_main_t *nm = &nat44_ei_main;
2033 nm->addr_and_port_alloc_alg = NAT44_EI_ADDR_AND_PORT_ALLOC_ALG_RANGE;
2034 nm->alloc_addr_and_port = nat44_ei_alloc_range_cb;
2035 nm->start_port = start_port;
2036 nm->end_port = end_port;
2040 nat44_ei_set_alloc_mape (u16 psid, u16 psid_offset, u16 psid_length)
2042 nat44_ei_main_t *nm = &nat44_ei_main;
2044 nm->addr_and_port_alloc_alg = NAT44_EI_ADDR_AND_PORT_ALLOC_ALG_MAPE;
2045 nm->alloc_addr_and_port = nat44_ei_alloc_mape_cb;
2047 nm->psid_offset = psid_offset;
2048 nm->psid_length = psid_length;
2052 nat44_ei_delete_session (nat44_ei_main_t *nm, nat44_ei_session_t *ses,
2055 nat44_ei_main_per_thread_data_t *tnm =
2056 vec_elt_at_index (nm->per_thread_data, thread_index);
2057 clib_bihash_kv_8_8_t kv, value;
2059 const nat44_ei_user_key_t u_key = { .addr = ses->in2out.addr,
2060 .fib_index = ses->in2out.fib_index };
2061 const u8 u_static = nat44_ei_is_session_static (ses);
2063 clib_dlist_remove (tnm->list_pool, ses->per_user_index);
2064 pool_put_index (tnm->list_pool, ses->per_user_index);
2066 pool_put (tnm->sessions, ses);
2067 vlib_set_simple_counter (&nm->total_sessions, thread_index, 0,
2068 pool_elts (tnm->sessions));
2070 kv.key = u_key.as_u64;
2071 if (!clib_bihash_search_8_8 (&tnm->user_hash, &kv, &value))
2073 u = pool_elt_at_index (tnm->users, value.value);
2075 u->nstaticsessions--;
2079 nat44_ei_delete_user_with_no_session (nm, u, thread_index);
2084 nat44_ei_del_session (nat44_ei_main_t *nm, ip4_address_t *addr, u16 port,
2085 nat_protocol_t proto, u32 vrf_id, int is_in)
2087 nat44_ei_main_per_thread_data_t *tnm;
2088 clib_bihash_kv_8_8_t kv, value;
2090 nat44_ei_session_t *s;
2091 clib_bihash_8_8_t *t;
2093 fail_if_disabled ();
2095 fib_index = fib_table_find (FIB_PROTOCOL_IP4, vrf_id);
2096 init_nat_k (&kv, *addr, port, fib_index, proto);
2097 t = is_in ? &nm->in2out : &nm->out2in;
2098 if (!clib_bihash_search_8_8 (t, &kv, &value))
2100 // this is called from API/CLI, so the world is stopped here
2101 // it's safe to manipulate arbitrary per-thread data
2102 u32 thread_index = nat_value_get_thread_index (&value);
2103 tnm = vec_elt_at_index (nm->per_thread_data, thread_index);
2104 u32 session_index = nat_value_get_session_index (&value);
2105 if (pool_is_free_index (tnm->sessions, session_index))
2106 return VNET_API_ERROR_UNSPECIFIED;
2108 s = pool_elt_at_index (tnm->sessions, session_index);
2109 nat44_ei_free_session_data_v2 (nm, s, tnm - nm->per_thread_data, 0);
2110 nat44_ei_delete_session (nm, s, tnm - nm->per_thread_data);
2114 return VNET_API_ERROR_NO_SUCH_ENTRY;
2118 nat44_ei_add_del_addr_to_fib (ip4_address_t *addr, u8 p_len, u32 sw_if_index,
2121 nat44_ei_main_t *nm = &nat44_ei_main;
2122 fib_prefix_t prefix = {
2124 .fp_proto = FIB_PROTOCOL_IP4,
2126 .ip4.as_u32 = addr->as_u32,
2129 u32 fib_index = ip4_fib_table_get_index_for_sw_if_index (sw_if_index);
2133 fib_table_entry_update_one_path (fib_index, &prefix, nm->fib_src_low,
2134 (FIB_ENTRY_FLAG_CONNECTED |
2135 FIB_ENTRY_FLAG_LOCAL |
2136 FIB_ENTRY_FLAG_EXCLUSIVE),
2137 DPO_PROTO_IP4, NULL, sw_if_index, ~0, 1,
2138 NULL, FIB_ROUTE_PATH_FLAG_NONE);
2142 fib_table_entry_delete (fib_index, &prefix, nm->fib_src_low);
2147 nat44_ei_reserve_port (ip4_address_t addr, u16 port, nat_protocol_t proto)
2149 u32 ti = nat44_ei_get_thread_idx_by_port (port);
2150 nat44_ei_main_t *nm = &nat44_ei_main;
2151 nat44_ei_address_t *a = 0;
2154 for (i = 0; i < vec_len (nm->addresses); i++)
2156 a = nm->addresses + i;
2158 if (a->addr.as_u32 != addr.as_u32)
2161 if (nat44_ei_port_is_used (a, proto, port))
2164 nat44_ei_port_get (a, proto, port);
2167 a->busy_ports[proto]++;
2168 a->busy_ports_per_thread[proto][ti]++;
2177 nat44_ei_free_port (ip4_address_t addr, u16 port, nat_protocol_t proto)
2179 u32 ti = nat44_ei_get_thread_idx_by_port (port);
2180 nat44_ei_main_t *nm = &nat44_ei_main;
2181 nat44_ei_address_t *a = 0;
2184 for (i = 0; i < vec_len (nm->addresses); i++)
2186 a = nm->addresses + i;
2188 if (a->addr.as_u32 != addr.as_u32)
2191 nat44_ei_port_put (a, proto, port);
2194 a->busy_ports[proto]--;
2195 a->busy_ports_per_thread[proto][ti]--;
2204 nat44_ei_add_resolve_record (ip4_address_t l_addr, u16 l_port, u16 e_port,
2205 nat_protocol_t proto, u32 vrf_id, u32 sw_if_index,
2206 u32 flags, ip4_address_t pool_addr, u8 *tag)
2208 nat44_ei_static_map_resolve_t *rp;
2209 nat44_ei_main_t *nm = &nat44_ei_main;
2211 vec_add2 (nm->to_resolve, rp, 1);
2212 rp->l_addr.as_u32 = l_addr.as_u32;
2213 rp->l_port = l_port;
2214 rp->e_port = e_port;
2215 rp->sw_if_index = sw_if_index;
2216 rp->vrf_id = vrf_id;
2219 rp->pool_addr = pool_addr;
2220 rp->tag = vec_dup (tag);
2224 nat44_ei_get_resolve_record (ip4_address_t l_addr, u16 l_port, u16 e_port,
2225 nat_protocol_t proto, u32 vrf_id, u32 sw_if_index,
2226 u32 flags, int *out)
2228 nat44_ei_static_map_resolve_t *rp;
2229 nat44_ei_main_t *nm = &nat44_ei_main;
2232 for (i = 0; i < vec_len (nm->to_resolve); i++)
2234 rp = nm->to_resolve + i;
2236 if (rp->sw_if_index == sw_if_index && rp->vrf_id == vrf_id)
2238 if (is_sm_identity_nat (rp->flags) && is_sm_identity_nat (flags))
2240 if (!(is_sm_addr_only (rp->flags) && is_sm_addr_only (flags)))
2242 if (rp->e_port != e_port || rp->proto != proto)
2248 else if (rp->l_addr.as_u32 == l_addr.as_u32)
2250 if (!(is_sm_addr_only (rp->flags) && is_sm_addr_only (flags)))
2252 if (rp->l_port != l_port || rp->e_port != e_port ||
2274 nat44_ei_del_resolve_record (ip4_address_t l_addr, u16 l_port, u16 e_port,
2275 nat_protocol_t proto, u32 vrf_id, u32 sw_if_index,
2278 nat44_ei_main_t *nm = &nat44_ei_main;
2280 if (!nat44_ei_get_resolve_record (l_addr, l_port, e_port, proto, vrf_id,
2281 sw_if_index, flags, &i))
2283 vec_del1 (nm->to_resolve, i);
2290 delete_matching_dynamic_sessions (const nat44_ei_static_mapping_t *m,
2293 nat44_ei_main_t *nm = &nat44_ei_main;
2294 clib_bihash_kv_8_8_t kv, value;
2295 nat44_ei_session_t *s;
2296 nat44_ei_user_key_t u_key;
2298 nat44_ei_main_per_thread_data_t *tnm;
2299 dlist_elt_t *head, *elt;
2300 u32 elt_index, head_index;
2304 if (nm->static_mapping_only)
2307 tnm = vec_elt_at_index (nm->per_thread_data, worker_index);
2309 u_key.addr = m->local_addr;
2310 u_key.fib_index = m->fib_index;
2311 kv.key = u_key.as_u64;
2312 if (!clib_bihash_search_8_8 (&tnm->user_hash, &kv, &value))
2314 user_index = value.value;
2315 u = pool_elt_at_index (tnm->users, user_index);
2318 head_index = u->sessions_per_user_list_head_index;
2319 head = pool_elt_at_index (tnm->list_pool, head_index);
2320 elt_index = head->next;
2321 elt = pool_elt_at_index (tnm->list_pool, elt_index);
2322 ses_index = elt->value;
2323 while (ses_index != ~0)
2325 s = pool_elt_at_index (tnm->sessions, ses_index);
2326 elt = pool_elt_at_index (tnm->list_pool, elt->next);
2327 ses_index = elt->value;
2329 if (nat44_ei_is_session_static (s))
2332 if (!is_sm_addr_only (m->flags) &&
2333 s->in2out.port != m->local_port)
2336 nat44_ei_free_session_data_v2 (nm, s, tnm - nm->per_thread_data,
2338 nat44_ei_delete_session (nm, s, tnm - nm->per_thread_data);
2340 if (!is_sm_addr_only (m->flags))
2348 nat44_ei_add_static_mapping (ip4_address_t l_addr, ip4_address_t e_addr,
2349 u16 l_port, u16 e_port, nat_protocol_t proto,
2350 u32 vrf_id, u32 sw_if_index, u32 flags,
2351 ip4_address_t pool_addr, u8 *tag)
2354 nat44_ei_main_t *nm = &nat44_ei_main;
2356 if (is_sm_switch_address (flags))
2358 if (!nat44_ei_get_resolve_record (l_addr, l_port, e_port, proto, vrf_id,
2359 sw_if_index, flags, 0))
2361 return VNET_API_ERROR_VALUE_EXIST;
2364 nat44_ei_add_resolve_record (l_addr, l_port, e_port, proto, vrf_id,
2365 sw_if_index, flags, pool_addr, tag);
2367 ip4_address_t *first_int_addr =
2368 ip4_interface_first_address (nm->ip4_main, sw_if_index, 0);
2369 if (!first_int_addr)
2371 // dhcp resolution required
2375 e_addr.as_u32 = first_int_addr->as_u32;
2378 return nat44_ei_add_static_mapping_internal (l_addr, e_addr, l_port, e_port,
2379 proto, vrf_id, sw_if_index,
2380 flags, pool_addr, tag);
2384 nat44_ei_del_static_mapping (ip4_address_t l_addr, ip4_address_t e_addr,
2385 u16 l_port, u16 e_port, nat_protocol_t proto,
2386 u32 vrf_id, u32 sw_if_index, u32 flags)
2388 nat44_ei_main_t *nm = &nat44_ei_main;
2390 if (is_sm_switch_address (flags))
2393 if (nat44_ei_del_resolve_record (l_addr, l_port, e_port, proto, vrf_id,
2394 sw_if_index, flags))
2396 return VNET_API_ERROR_NO_SUCH_ENTRY;
2399 ip4_address_t *first_int_addr =
2400 ip4_interface_first_address (nm->ip4_main, sw_if_index, 0);
2401 if (!first_int_addr)
2403 // dhcp resolution required
2407 e_addr.as_u32 = first_int_addr->as_u32;
2410 return nat44_ei_del_static_mapping_internal (
2411 l_addr, e_addr, l_port, e_port, proto, vrf_id, sw_if_index, flags);
2415 nat44_ei_add_static_mapping_internal (ip4_address_t l_addr,
2416 ip4_address_t e_addr, u16 l_port,
2417 u16 e_port, nat_protocol_t proto,
2418 u32 vrf_id, u32 sw_if_index, u32 flags,
2419 ip4_address_t pool_addr, u8 *tag)
2421 nat44_ei_main_t *nm = &nat44_ei_main;
2422 clib_bihash_kv_8_8_t kv, value;
2423 nat44_ei_lb_addr_port_t *local;
2424 nat44_ei_static_mapping_t *m;
2428 fail_if_disabled ();
2430 if (is_sm_addr_only (flags))
2432 e_port = l_port = proto = 0;
2435 if (is_sm_identity_nat (flags))
2438 l_addr.as_u32 = e_addr.as_u32;
2442 init_nat_k (&kv, e_addr, e_port, 0, proto);
2444 if (!clib_bihash_search_8_8 (&nm->static_mapping_by_external, &kv, &value))
2446 m = pool_elt_at_index (nm->static_mappings, value.value);
2447 if (!is_sm_identity_nat (m->flags))
2449 return VNET_API_ERROR_VALUE_EXIST;
2453 // adding local identity nat record for different vrf table
2454 pool_foreach (local, m->locals)
2456 if (local->vrf_id == vrf_id)
2458 return VNET_API_ERROR_VALUE_EXIST;
2462 pool_get (m->locals, local);
2464 local->vrf_id = vrf_id;
2465 local->fib_index = fib_table_find_or_create_and_lock (
2466 FIB_PROTOCOL_IP4, vrf_id, nm->fib_src_low);
2468 init_nat_kv (&kv, m->local_addr, m->local_port, local->fib_index,
2469 m->proto, 0, m - nm->static_mappings);
2470 clib_bihash_add_del_8_8 (&nm->static_mapping_by_local, &kv, 1);
2477 fib_index = fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4, vrf_id,
2482 // fallback to default vrf
2483 vrf_id = nm->inside_vrf_id;
2484 fib_index = nm->inside_fib_index;
2485 fib_table_lock (fib_index, FIB_PROTOCOL_IP4, nm->fib_src_low);
2488 if (!is_sm_identity_nat (flags))
2490 init_nat_k (&kv, l_addr, l_port, fib_index, proto);
2491 if (!clib_bihash_search_8_8 (&nm->static_mapping_by_local, &kv, &value))
2493 return VNET_API_ERROR_VALUE_EXIST;
2497 if (!(is_sm_addr_only (flags) || nm->static_mapping_only))
2499 if (nat44_ei_reserve_port (e_addr, e_port, proto))
2501 // remove resolve record
2502 if ((is_sm_switch_address (flags)) && !is_sm_identity_nat (flags))
2504 nat44_ei_del_resolve_record (l_addr, l_port, e_port, proto,
2505 vrf_id, sw_if_index, flags);
2507 return VNET_API_ERROR_NO_SUCH_ENTRY;
2511 pool_get (nm->static_mappings, m);
2512 clib_memset (m, 0, sizeof (*m));
2515 m->local_addr = l_addr;
2516 m->external_addr = e_addr;
2518 m->tag = vec_dup (tag);
2520 if (!is_sm_addr_only (flags))
2522 m->local_port = l_port;
2523 m->external_port = e_port;
2527 if (is_sm_identity_nat (flags))
2529 pool_get (m->locals, local);
2531 local->vrf_id = vrf_id;
2532 local->fib_index = fib_index;
2537 m->fib_index = fib_index;
2540 init_nat_kv (&kv, m->local_addr, m->local_port, fib_index, m->proto, 0,
2541 m - nm->static_mappings);
2542 clib_bihash_add_del_8_8 (&nm->static_mapping_by_local, &kv, 1);
2544 init_nat_kv (&kv, m->external_addr, m->external_port, 0, m->proto, 0,
2545 m - nm->static_mappings);
2546 clib_bihash_add_del_8_8 (&nm->static_mapping_by_external, &kv, 1);
2548 if (nm->num_workers > 1)
2550 // store worker index for this record
2552 .src_address = m->local_addr,
2554 worker_index = nat44_ei_get_in2out_worker_index (&ip, m->fib_index, 0);
2555 vec_add1 (m->workers, worker_index);
2559 worker_index = nm->num_workers;
2561 delete_matching_dynamic_sessions (m, worker_index);
2563 if (is_sm_addr_only (flags))
2565 nat44_ei_add_del_addr_to_fib_foreach_out_if (&e_addr, 1);
2572 nat44_ei_del_static_mapping_internal (ip4_address_t l_addr,
2573 ip4_address_t e_addr, u16 l_port,
2574 u16 e_port, nat_protocol_t proto,
2575 u32 vrf_id, u32 sw_if_index, u32 flags)
2577 nat44_ei_main_per_thread_data_t *tnm;
2578 nat44_ei_main_t *nm = &nat44_ei_main;
2579 clib_bihash_kv_8_8_t kv, value;
2580 nat44_ei_lb_addr_port_t *local;
2581 nat44_ei_static_mapping_t *m;
2583 nat44_ei_user_key_t u_key;
2585 fail_if_disabled ();
2587 if (is_sm_addr_only (flags))
2589 e_port = l_port = proto = 0;
2592 if (is_sm_identity_nat (flags))
2595 l_addr.as_u32 = e_addr.as_u32;
2599 init_nat_k (&kv, e_addr, e_port, 0, proto);
2601 if (clib_bihash_search_8_8 (&nm->static_mapping_by_external, &kv, &value))
2603 if (is_sm_switch_address (flags))
2607 return VNET_API_ERROR_NO_SUCH_ENTRY;
2610 m = pool_elt_at_index (nm->static_mappings, value.value);
2612 if (is_sm_identity_nat (flags))
2618 vrf_id = nm->inside_vrf_id;
2621 pool_foreach (local, m->locals)
2623 if (local->vrf_id == vrf_id)
2625 local = pool_elt_at_index (m->locals, local - m->locals);
2626 fib_index = local->fib_index;
2627 pool_put (m->locals, local);
2633 return VNET_API_ERROR_NO_SUCH_ENTRY;
2638 fib_index = m->fib_index;
2641 if (!(is_sm_addr_only (flags) || nm->static_mapping_only))
2643 if (nat44_ei_free_port (e_addr, e_port, proto))
2645 return VNET_API_ERROR_INVALID_VALUE;
2649 init_nat_k (&kv, l_addr, l_port, fib_index, proto);
2650 clib_bihash_add_del_8_8 (&nm->static_mapping_by_local, &kv, 0);
2652 if (!nm->static_mapping_only || nm->static_mapping_connection_tracking)
2654 // delete sessions for static mapping
2655 if (nm->num_workers > 1)
2656 tnm = vec_elt_at_index (nm->per_thread_data, m->workers[0]);
2658 tnm = vec_elt_at_index (nm->per_thread_data, nm->num_workers);
2660 u_key.addr = m->local_addr;
2661 u_key.fib_index = fib_index;
2662 kv.key = u_key.as_u64;
2663 nat44_ei_static_mapping_del_sessions (
2664 nm, tnm, u_key, is_sm_addr_only (flags), e_addr, e_port);
2667 fib_table_unlock (fib_index, FIB_PROTOCOL_IP4, nm->fib_src_low);
2669 if (!pool_elts (m->locals))
2671 // this is last record remove all required stuff
2673 init_nat_k (&kv, e_addr, e_port, 0, proto);
2674 clib_bihash_add_del_8_8 (&nm->static_mapping_by_external, &kv, 0);
2677 vec_free (m->workers);
2678 pool_put (nm->static_mappings, m);
2680 if (is_sm_addr_only (flags) && !is_sm_identity_nat (flags))
2682 nat44_ei_add_del_addr_to_fib_foreach_out_if (&e_addr, 0);
2690 nat44_ei_static_mapping_match (ip4_address_t match_addr, u16 match_port,
2691 u32 match_fib_index,
2692 nat_protocol_t match_protocol,
2693 ip4_address_t *mapping_addr, u16 *mapping_port,
2694 u32 *mapping_fib_index, u8 by_external,
2695 u8 *is_addr_only, u8 *is_identity_nat)
2697 nat44_ei_main_t *nm = &nat44_ei_main;
2698 clib_bihash_kv_8_8_t kv, value;
2699 nat44_ei_static_mapping_t *m;
2704 init_nat_k (&kv, match_addr, match_port, 0, match_protocol);
2705 if (clib_bihash_search_8_8 (&nm->static_mapping_by_external, &kv,
2708 /* Try address only mapping */
2709 init_nat_k (&kv, match_addr, 0, 0, 0);
2710 if (clib_bihash_search_8_8 (&nm->static_mapping_by_external, &kv,
2714 m = pool_elt_at_index (nm->static_mappings, value.value);
2716 *mapping_fib_index = m->fib_index;
2717 *mapping_addr = m->local_addr;
2718 port = m->local_port;
2722 init_nat_k (&kv, match_addr, match_port, match_fib_index,
2724 if (clib_bihash_search_8_8 (&nm->static_mapping_by_local, &kv, &value))
2726 /* Try address only mapping */
2727 init_nat_k (&kv, match_addr, 0, match_fib_index, 0);
2728 if (clib_bihash_search_8_8 (&nm->static_mapping_by_local, &kv,
2732 m = pool_elt_at_index (nm->static_mappings, value.value);
2734 *mapping_fib_index = nm->outside_fib_index;
2735 *mapping_addr = m->external_addr;
2736 port = m->external_port;
2739 /* Address only mapping doesn't change port */
2740 if (is_sm_addr_only (m->flags))
2741 *mapping_port = match_port;
2743 *mapping_port = port;
2745 if (PREDICT_FALSE (is_addr_only != 0))
2746 *is_addr_only = is_sm_addr_only (m->flags);
2748 if (PREDICT_FALSE (is_identity_nat != 0))
2749 *is_identity_nat = is_sm_identity_nat (m->flags);
2755 nat44_ei_worker_db_free (nat44_ei_main_per_thread_data_t *tnm)
2757 pool_free (tnm->list_pool);
2758 pool_free (tnm->lru_pool);
2759 pool_free (tnm->sessions);
2760 pool_free (tnm->users);
2762 clib_bihash_free_8_8 (&tnm->user_hash);
2766 format_nat44_ei_key (u8 *s, va_list *args)
2768 u64 key = va_arg (*args, u64);
2772 nat_protocol_t protocol;
2775 split_nat_key (key, &addr, &port, &fib_index, &protocol);
2777 s = format (s, "%U proto %U port %d fib %d", format_ip4_address, &addr,
2778 format_nat_protocol, protocol, clib_net_to_host_u16 (port),
2784 format_nat44_ei_user_kvp (u8 *s, va_list *args)
2786 clib_bihash_kv_8_8_t *v = va_arg (*args, clib_bihash_kv_8_8_t *);
2787 nat44_ei_user_key_t k;
2791 s = format (s, "%U fib %d user-index %llu", format_ip4_address, &k.addr,
2792 k.fib_index, v->value);
2798 format_nat44_ei_session_kvp (u8 *s, va_list *args)
2800 clib_bihash_kv_8_8_t *v = va_arg (*args, clib_bihash_kv_8_8_t *);
2802 s = format (s, "%U thread-index %llu session-index %llu",
2803 format_nat44_ei_key, v->key, nat_value_get_thread_index (v),
2804 nat_value_get_session_index (v));
2810 format_nat44_ei_static_mapping_kvp (u8 *s, va_list *args)
2812 clib_bihash_kv_8_8_t *v = va_arg (*args, clib_bihash_kv_8_8_t *);
2814 s = format (s, "%U static-mapping-index %llu", format_nat44_ei_key, v->key,
2821 nat44_ei_worker_db_init (nat44_ei_main_per_thread_data_t *tnm,
2822 u32 translations, u32 translation_buckets,
2827 pool_alloc (tnm->list_pool, translations);
2828 pool_alloc (tnm->lru_pool, translations);
2829 pool_alloc (tnm->sessions, translations);
2831 clib_bihash_init_8_8 (&tnm->user_hash, "users", user_buckets, 0);
2833 clib_bihash_set_kvp_format_fn_8_8 (&tnm->user_hash,
2834 format_nat44_ei_user_kvp);
2836 pool_get (tnm->lru_pool, head);
2837 tnm->tcp_trans_lru_head_index = head - tnm->lru_pool;
2838 clib_dlist_init (tnm->lru_pool, tnm->tcp_trans_lru_head_index);
2840 pool_get (tnm->lru_pool, head);
2841 tnm->tcp_estab_lru_head_index = head - tnm->lru_pool;
2842 clib_dlist_init (tnm->lru_pool, tnm->tcp_estab_lru_head_index);
2844 pool_get (tnm->lru_pool, head);
2845 tnm->udp_lru_head_index = head - tnm->lru_pool;
2846 clib_dlist_init (tnm->lru_pool, tnm->udp_lru_head_index);
2848 pool_get (tnm->lru_pool, head);
2849 tnm->icmp_lru_head_index = head - tnm->lru_pool;
2850 clib_dlist_init (tnm->lru_pool, tnm->icmp_lru_head_index);
2852 pool_get (tnm->lru_pool, head);
2853 tnm->unk_proto_lru_head_index = head - tnm->lru_pool;
2854 clib_dlist_init (tnm->lru_pool, tnm->unk_proto_lru_head_index);
2858 nat44_ei_db_init (u32 translations, u32 translation_buckets, u32 user_buckets)
2860 nat44_ei_main_t *nm = &nat44_ei_main;
2861 nat44_ei_main_per_thread_data_t *tnm;
2863 u32 static_mapping_buckets = 1024;
2864 u32 static_mapping_memory_size = 64 << 20;
2866 clib_bihash_init_8_8 (&nm->static_mapping_by_local,
2867 "static_mapping_by_local", static_mapping_buckets,
2868 static_mapping_memory_size);
2869 clib_bihash_init_8_8 (&nm->static_mapping_by_external,
2870 "static_mapping_by_external", static_mapping_buckets,
2871 static_mapping_memory_size);
2872 clib_bihash_set_kvp_format_fn_8_8 (&nm->static_mapping_by_local,
2873 format_nat44_ei_static_mapping_kvp);
2874 clib_bihash_set_kvp_format_fn_8_8 (&nm->static_mapping_by_external,
2875 format_nat44_ei_static_mapping_kvp);
2879 clib_bihash_init_8_8 (&nm->in2out, "in2out", translation_buckets, 0);
2880 clib_bihash_init_8_8 (&nm->out2in, "out2in", translation_buckets, 0);
2881 clib_bihash_set_kvp_format_fn_8_8 (&nm->in2out,
2882 format_nat44_ei_session_kvp);
2883 clib_bihash_set_kvp_format_fn_8_8 (&nm->out2in,
2884 format_nat44_ei_session_kvp);
2885 vec_foreach (tnm, nm->per_thread_data)
2887 nat44_ei_worker_db_init (tnm, translations, translation_buckets,
2894 nat44_ei_sessions_clear ()
2896 nat44_ei_main_t *nm = &nat44_ei_main;
2897 nat44_ei_main_per_thread_data_t *tnm;
2901 clib_bihash_free_8_8 (&nm->in2out);
2902 clib_bihash_free_8_8 (&nm->out2in);
2903 clib_bihash_init_8_8 (&nm->in2out, "in2out", nm->translation_buckets, 0);
2904 clib_bihash_init_8_8 (&nm->out2in, "out2in", nm->translation_buckets, 0);
2905 clib_bihash_set_kvp_format_fn_8_8 (&nm->in2out,
2906 format_nat44_ei_session_kvp);
2907 clib_bihash_set_kvp_format_fn_8_8 (&nm->out2in,
2908 format_nat44_ei_session_kvp);
2909 vec_foreach (tnm, nm->per_thread_data)
2911 nat44_ei_worker_db_free (tnm);
2912 nat44_ei_worker_db_init (tnm, nm->translations,
2913 nm->translation_buckets, nm->user_buckets);
2917 vlib_zero_simple_counter (&nm->total_users, 0);
2918 vlib_zero_simple_counter (&nm->total_sessions, 0);
2919 vlib_zero_simple_counter (&nm->user_limit_reached, 0);
2923 nat44_ei_update_outside_fib (ip4_main_t *im, uword opaque, u32 sw_if_index,
2924 u32 new_fib_index, u32 old_fib_index)
2926 nat44_ei_main_t *nm = &nat44_ei_main;
2927 nat44_ei_outside_fib_t *outside_fib;
2928 nat44_ei_interface_t *i;
2932 if (!nm->enabled || (new_fib_index == old_fib_index) ||
2933 (!vec_len (nm->outside_fibs)))
2938 pool_foreach (i, nm->interfaces)
2940 if (i->sw_if_index == sw_if_index)
2942 if (!(nat44_ei_interface_is_outside (i)))
2948 pool_foreach (i, nm->output_feature_interfaces)
2950 if (i->sw_if_index == sw_if_index)
2952 if (!(nat44_ei_interface_is_outside (i)))
2961 vec_foreach (outside_fib, nm->outside_fibs)
2963 if (outside_fib->fib_index == old_fib_index)
2965 outside_fib->refcount--;
2966 if (!outside_fib->refcount)
2967 vec_del1 (nm->outside_fibs, outside_fib - nm->outside_fibs);
2972 vec_foreach (outside_fib, nm->outside_fibs)
2974 if (outside_fib->fib_index == new_fib_index)
2976 outside_fib->refcount++;
2984 vec_add2 (nm->outside_fibs, outside_fib, 1);
2985 outside_fib->refcount = 1;
2986 outside_fib->fib_index = new_fib_index;
2991 nat44_ei_add_address (ip4_address_t *addr, u32 vrf_id)
2993 nat44_ei_main_t *nm = &nat44_ei_main;
2994 vlib_thread_main_t *tm = vlib_get_thread_main ();
2995 nat44_ei_address_t *ap;
2997 fail_if_disabled ();
2999 /* Check if address already exists */
3000 vec_foreach (ap, nm->addresses)
3002 if (ap->addr.as_u32 == addr->as_u32)
3004 nat44_ei_log_err ("address exist");
3005 return VNET_API_ERROR_VALUE_EXIST;
3009 vec_add2 (nm->addresses, ap, 1);
3016 ap->fib_index = fib_table_find_or_create_and_lock (
3017 FIB_PROTOCOL_IP4, vrf_id, nm->fib_src_low);
3020 nat_protocol_t proto;
3021 for (proto = 0; proto < NAT_N_PROTOCOLS; ++proto)
3023 ap->busy_port_bitmap[proto] = 0;
3024 ap->busy_ports[proto] = 0;
3025 ap->busy_ports_per_thread[proto] = 0;
3026 vec_validate_init_empty (ap->busy_ports_per_thread[proto],
3027 tm->n_vlib_mains - 1, 0);
3030 nat44_ei_add_del_addr_to_fib_foreach_out_if (addr, 1);
3036 nat44_ei_del_address (ip4_address_t addr, u8 delete_sm)
3038 nat44_ei_main_t *nm = &nat44_ei_main;
3039 nat44_ei_address_t *a = 0;
3040 nat44_ei_session_t *ses;
3041 u32 *ses_to_be_removed = 0, *ses_index;
3042 nat44_ei_main_per_thread_data_t *tnm;
3043 nat44_ei_static_mapping_t *m;
3046 fail_if_disabled ();
3048 /* Find SNAT address */
3049 for (j = 0; j < vec_len (nm->addresses); j++)
3051 if (nm->addresses[j].addr.as_u32 == addr.as_u32)
3053 a = nm->addresses + j;
3059 nat44_ei_log_err ("no such address");
3060 return VNET_API_ERROR_NO_SUCH_ENTRY;
3065 pool_foreach (m, nm->static_mappings)
3067 if (m->external_addr.as_u32 == addr.as_u32)
3068 nat44_ei_del_static_mapping_internal (
3069 m->local_addr, m->external_addr, m->local_port, m->external_port,
3070 m->proto, m->vrf_id, ~0, m->flags);
3075 /* Check if address is used in some static mapping */
3076 if (nat44_ei_is_address_used_in_static_mapping (addr))
3078 nat44_ei_log_err ("address used in static mapping");
3079 return VNET_API_ERROR_UNSPECIFIED;
3083 /* Delete sessions using address */
3084 if (a->busy_ports[NAT_PROTOCOL_TCP] || a->busy_ports[NAT_PROTOCOL_UDP] ||
3085 a->busy_ports[NAT_PROTOCOL_ICMP])
3087 vec_foreach (tnm, nm->per_thread_data)
3089 pool_foreach (ses, tnm->sessions)
3091 if (ses->out2in.addr.as_u32 == addr.as_u32)
3093 nat44_ei_free_session_data (nm, ses,
3094 tnm - nm->per_thread_data, 0);
3095 vec_add1 (ses_to_be_removed, ses - tnm->sessions);
3098 vec_foreach (ses_index, ses_to_be_removed)
3100 ses = pool_elt_at_index (tnm->sessions, ses_index[0]);
3101 nat44_ei_delete_session (nm, ses, tnm - nm->per_thread_data);
3103 vec_free (ses_to_be_removed);
3107 nat44_ei_add_del_addr_to_fib_foreach_out_if (&addr, 0);
3109 if (a->fib_index != ~0)
3111 fib_table_unlock (a->fib_index, FIB_PROTOCOL_IP4, nm->fib_src_low);
3114 nat_protocol_t proto;
3115 for (proto = 0; proto < NAT_N_PROTOCOLS; ++proto)
3117 vec_free (a->busy_ports_per_thread[proto]);
3120 vec_del1 (nm->addresses, j);
3125 nat44_ei_add_interface_address (u32 sw_if_index)
3127 nat44_ei_main_t *nm = &nat44_ei_main;
3128 ip4_main_t *ip4_main = nm->ip4_main;
3129 ip4_address_t *first_int_addr;
3130 u32 *auto_add_sw_if_indices = nm->auto_add_sw_if_indices;
3133 for (i = 0; i < vec_len (auto_add_sw_if_indices); i++)
3135 if (auto_add_sw_if_indices[i] == sw_if_index)
3137 return VNET_API_ERROR_VALUE_EXIST;
3141 /* add to the auto-address list */
3142 vec_add1 (nm->auto_add_sw_if_indices, sw_if_index);
3144 // if the address is already bound - or static - add it now
3145 first_int_addr = ip4_interface_first_address (ip4_main, sw_if_index, 0);
3148 (void) nat44_ei_add_address (first_int_addr, ~0);
3155 nat44_ei_del_interface_address (u32 sw_if_index)
3157 nat44_ei_main_t *nm = &nat44_ei_main;
3158 ip4_main_t *ip4_main = nm->ip4_main;
3159 ip4_address_t *first_int_addr;
3160 nat44_ei_static_map_resolve_t *rp;
3161 u32 *indices_to_delete = 0;
3163 u32 *auto_add_sw_if_indices = nm->auto_add_sw_if_indices;
3165 fail_if_disabled ();
3167 first_int_addr = ip4_interface_first_address (ip4_main, sw_if_index, 0);
3169 for (i = 0; i < vec_len (auto_add_sw_if_indices); i++)
3171 if (auto_add_sw_if_indices[i] == sw_if_index)
3174 ip4_interface_first_address (ip4_main, sw_if_index, 0);
3177 (void) nat44_ei_del_address (first_int_addr[0], 1);
3181 for (j = 0; j < vec_len (nm->to_resolve); j++)
3183 rp = nm->to_resolve + j;
3184 if (rp->sw_if_index == sw_if_index)
3186 vec_add1 (indices_to_delete, j);
3189 if (vec_len (indices_to_delete))
3191 for (j = vec_len (indices_to_delete) - 1; j >= 0; j--)
3193 vec_del1 (nm->to_resolve, j);
3195 vec_free (indices_to_delete);
3199 vec_del1 (nm->auto_add_sw_if_indices, i);
3203 return VNET_API_ERROR_NO_SUCH_ENTRY;
3206 static_always_inline int
3207 is_sw_if_index_reg_for_auto_resolve (u32 *sw_if_indices, u32 sw_if_index)
3210 vec_foreach (i, sw_if_indices)
3212 if (*i == sw_if_index)
3221 nat44_ei_ip4_add_del_interface_address_cb (ip4_main_t *im, uword opaque,
3223 ip4_address_t *address,
3225 u32 if_address_index, u32 is_delete)
3227 nat44_ei_main_t *nm = &nat44_ei_main;
3228 nat44_ei_static_map_resolve_t *rp;
3229 nat44_ei_address_t *addresses = nm->addresses;
3237 if (!is_sw_if_index_reg_for_auto_resolve (nm->auto_add_sw_if_indices,
3245 /* Don't trip over lease renewal, static config */
3246 for (i = 0; i < vec_len (addresses); i++)
3248 if (addresses[i].addr.as_u32 == address->as_u32)
3254 (void) nat44_ei_add_address (address, ~0);
3256 /* Scan static map resolution vector */
3257 for (i = 0; i < vec_len (nm->to_resolve); i++)
3259 rp = nm->to_resolve + i;
3260 if (is_sm_addr_only (rp->flags))
3264 /* On this interface? */
3265 if (rp->sw_if_index == sw_if_index)
3267 rv = nat44_ei_add_static_mapping_internal (
3268 rp->l_addr, address[0], rp->l_port, rp->e_port, rp->proto,
3269 rp->vrf_id, ~0, rp->flags, rp->pool_addr, rp->tag);
3272 nat_elog_notice_X1 (
3273 nm, "add_static_mapping_internal returned %d", "i4", rv);
3280 // remove all static mapping records
3281 (void) nat44_ei_del_address (address[0], 1);
3286 nat44_ei_set_frame_queue_nelts (u32 frame_queue_nelts)
3289 nat44_ei_main_t *nm = &nat44_ei_main;
3290 nm->frame_queue_nelts = frame_queue_nelts;
3295 nat44_ei_ip4_add_del_addr_only_sm_cb (ip4_main_t *im, uword opaque,
3296 u32 sw_if_index, ip4_address_t *address,
3297 u32 address_length, u32 if_address_index,
3300 nat44_ei_main_t *nm = &nat44_ei_main;
3301 nat44_ei_static_map_resolve_t *rp;
3302 nat44_ei_static_mapping_t *m;
3303 clib_bihash_kv_8_8_t kv, value;
3304 int i, rv = 0, match = 0;
3311 for (i = 0; i < vec_len (nm->to_resolve); i++)
3313 rp = nm->to_resolve + i;
3315 if (is_sm_addr_only (rp->flags) && rp->sw_if_index == sw_if_index)
3327 init_nat_k (&kv, *address, is_sm_addr_only (rp->flags) ? 0 : rp->e_port,
3328 nm->outside_fib_index,
3329 is_sm_addr_only (rp->flags) ? 0 : rp->proto);
3330 if (clib_bihash_search_8_8 (&nm->static_mapping_by_external, &kv, &value))
3333 m = pool_elt_at_index (nm->static_mappings, value.value);
3339 rv = nat44_ei_del_static_mapping_internal (
3340 rp->l_addr, address[0], rp->l_port, rp->e_port, rp->proto, rp->vrf_id,
3344 nat_elog_notice_X1 (nm, "nat44_ei_del_static_mapping returned %d",
3352 rv = nat44_ei_add_static_mapping_internal (
3353 rp->l_addr, address[0], rp->l_port, rp->e_port, rp->proto, rp->vrf_id,
3354 ~0, rp->flags, rp->pool_addr, rp->tag);
3358 nat_elog_notice_X1 (nm, "nat44_ei_add_static_mapping returned %d",
3364 static_always_inline uword
3365 nat44_ei_classify_inline_fn (vlib_main_t *vm, vlib_node_runtime_t *node,
3366 vlib_frame_t *frame)
3368 u32 n_left_from, *from, *to_next;
3369 nat44_ei_classify_next_t next_index;
3370 nat44_ei_main_t *nm = &nat44_ei_main;
3371 nat44_ei_static_mapping_t *m;
3372 u32 next_in2out = 0, next_out2in = 0;
3374 from = vlib_frame_vector_args (frame);
3375 n_left_from = frame->n_vectors;
3376 next_index = node->cached_next_index;
3378 while (n_left_from > 0)
3382 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
3384 while (n_left_from > 0 && n_left_to_next > 0)
3388 u32 next0 = NAT44_EI_CLASSIFY_NEXT_IN2OUT;
3390 nat44_ei_address_t *ap;
3391 clib_bihash_kv_8_8_t kv0, value0;
3393 /* speculatively enqueue b0 to the current next frame */
3399 n_left_to_next -= 1;
3401 b0 = vlib_get_buffer (vm, bi0);
3402 ip0 = vlib_buffer_get_current (b0);
3404 vec_foreach (ap, nm->addresses)
3406 if (ip0->dst_address.as_u32 == ap->addr.as_u32)
3408 next0 = NAT44_EI_CLASSIFY_NEXT_OUT2IN;
3413 if (PREDICT_FALSE (pool_elts (nm->static_mappings)))
3415 init_nat_k (&kv0, ip0->dst_address, 0, 0, 0);
3416 /* try to classify the fragment based on IP header alone */
3417 if (!clib_bihash_search_8_8 (&nm->static_mapping_by_external,
3420 m = pool_elt_at_index (nm->static_mappings, value0.value);
3421 if (m->local_addr.as_u32 != m->external_addr.as_u32)
3422 next0 = NAT44_EI_CLASSIFY_NEXT_OUT2IN;
3425 init_nat_k (&kv0, ip0->dst_address,
3426 vnet_buffer (b0)->ip.reass.l4_dst_port, 0,
3427 ip_proto_to_nat_proto (ip0->protocol));
3428 if (!clib_bihash_search_8_8 (&nm->static_mapping_by_external,
3431 m = pool_elt_at_index (nm->static_mappings, value0.value);
3432 if (m->local_addr.as_u32 != m->external_addr.as_u32)
3433 next0 = NAT44_EI_CLASSIFY_NEXT_OUT2IN;
3438 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE) &&
3439 (b0->flags & VLIB_BUFFER_IS_TRACED)))
3441 nat44_ei_classify_trace_t *t =
3442 vlib_add_trace (vm, node, b0, sizeof (*t));
3444 t->next_in2out = next0 == NAT44_EI_CLASSIFY_NEXT_IN2OUT ? 1 : 0;
3447 next_in2out += next0 == NAT44_EI_CLASSIFY_NEXT_IN2OUT;
3448 next_out2in += next0 == NAT44_EI_CLASSIFY_NEXT_OUT2IN;
3450 /* verify speculative enqueue, maybe switch current next frame */
3451 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
3452 n_left_to_next, bi0, next0);
3455 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
3458 vlib_node_increment_counter (
3459 vm, node->node_index, NAT44_EI_CLASSIFY_ERROR_NEXT_IN2OUT, next_in2out);
3460 vlib_node_increment_counter (
3461 vm, node->node_index, NAT44_EI_CLASSIFY_ERROR_NEXT_OUT2IN, next_out2in);
3462 return frame->n_vectors;
3465 VLIB_NODE_FN (nat44_ei_classify_node)
3466 (vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
3468 return nat44_ei_classify_inline_fn (vm, node, frame);
3471 VLIB_REGISTER_NODE (nat44_ei_classify_node) = {
3472 .name = "nat44-ei-classify",
3473 .vector_size = sizeof (u32),
3474 .format_trace = format_nat44_ei_classify_trace,
3475 .type = VLIB_NODE_TYPE_INTERNAL,
3476 .n_errors = ARRAY_LEN(nat44_ei_classify_error_strings),
3477 .error_strings = nat44_ei_classify_error_strings,
3478 .n_next_nodes = NAT44_EI_CLASSIFY_N_NEXT,
3480 [NAT44_EI_CLASSIFY_NEXT_IN2OUT] = "nat44-ei-in2out",
3481 [NAT44_EI_CLASSIFY_NEXT_OUT2IN] = "nat44-ei-out2in",
3482 [NAT44_EI_CLASSIFY_NEXT_DROP] = "error-drop",
3486 VLIB_NODE_FN (nat44_ei_handoff_classify_node)
3487 (vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
3489 return nat44_ei_classify_inline_fn (vm, node, frame);
3492 VLIB_REGISTER_NODE (nat44_ei_handoff_classify_node) = {
3493 .name = "nat44-ei-handoff-classify",
3494 .vector_size = sizeof (u32),
3495 .format_trace = format_nat44_ei_classify_trace,
3496 .type = VLIB_NODE_TYPE_INTERNAL,
3497 .n_errors = ARRAY_LEN(nat44_ei_classify_error_strings),
3498 .error_strings = nat44_ei_classify_error_strings,
3499 .n_next_nodes = NAT44_EI_CLASSIFY_N_NEXT,
3501 [NAT44_EI_CLASSIFY_NEXT_IN2OUT] = "nat44-ei-in2out-worker-handoff",
3502 [NAT44_EI_CLASSIFY_NEXT_OUT2IN] = "nat44-ei-out2in-worker-handoff",
3503 [NAT44_EI_CLASSIFY_NEXT_DROP] = "error-drop",
3508 * fd.io coding-style-patch-verification: ON
3511 * eval: (c-set-style "gnu")