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 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;
49 #define skip_if_disabled() \
52 nat44_ei_main_t *nm = &nat44_ei_main; \
53 if (PREDICT_FALSE (!nm->enabled)) \
58 #define fail_if_enabled() \
61 nat44_ei_main_t *nm = &nat44_ei_main; \
62 if (PREDICT_FALSE (nm->enabled)) \
64 nat44_ei_log_err ("plugin enabled"); \
70 #define fail_if_disabled() \
73 nat44_ei_main_t *nm = &nat44_ei_main; \
74 if (PREDICT_FALSE (!nm->enabled)) \
76 nat44_ei_log_err ("plugin disabled"); \
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"),
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"),
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"),
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"),
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"),
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"),
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"),
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"),
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"),
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"),
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"),
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"),
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"),
162 VLIB_PLUGIN_REGISTER () = {
163 .version = VPP_BUILD_VER,
164 .description = "IPv4 Endpoint-Independent NAT (NAT44 EI)",
167 #define foreach_nat44_ei_classify_error \
168 _ (NEXT_IN2OUT, "next in2out") \
169 _ (NEXT_OUT2IN, "next out2in") \
170 _ (FRAG_CACHED, "fragment cached")
174 #define _(sym, str) NAT44_EI_CLASSIFY_ERROR_##sym,
175 foreach_nat44_ei_classify_error
177 NAT44_EI_CLASSIFY_N_ERROR,
178 } nat44_ei_classify_error_t;
180 static char *nat44_ei_classify_error_strings[] = {
181 #define _(sym, string) string,
182 foreach_nat44_ei_classify_error
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;
198 } nat44_ei_classify_trace_t;
200 void nat44_ei_add_del_addr_to_fib (ip4_address_t *addr, u8 p_len,
201 u32 sw_if_index, int is_add);
204 format_nat44_ei_classify_trace (u8 *s, va_list *args)
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 *);
212 s = format (s, "nat44-ei-classify: fragment cached");
215 next = t->next_in2out ? "nat44-ei-in2out" : "nat44-ei-out2in";
216 s = format (s, "nat44-ei-classify: next %s", next);
222 static void nat44_ei_db_free ();
224 static void nat44_ei_db_init (u32 translations, u32 translation_buckets,
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);
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);
235 static void nat44_ei_update_outside_fib (ip4_main_t *im, uword opaque,
236 u32 sw_if_index, u32 new_fib_index,
240 nat44_ei_set_node_indexes (nat44_ei_main_t *nm, vlib_main_t *vm)
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;
252 nat44_ei_set_workers (uword *bitmap)
254 nat44_ei_main_t *nm = &nat44_ei_main;
257 if (nm->num_workers < 2)
258 return VNET_API_ERROR_FEATURE_DISABLED;
260 if (clib_bitmap_last_set (bitmap) >= nm->num_workers)
261 return VNET_API_ERROR_INVALID_WORKER;
263 vec_free (nm->workers);
264 clib_bitmap_foreach (i, bitmap)
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;
272 nm->port_per_thread = (0xffff - 1024) / _vec_len (nm->workers);
277 #define nat_validate_simple_counter(c, i) \
280 vlib_validate_simple_counter (&c, i); \
281 vlib_zero_simple_counter (&c, i); \
285 #define nat_init_simple_counter(c, n, sn) \
289 c.stat_segment_name = sn; \
290 nat_validate_simple_counter (c, 0); \
294 static_always_inline void
295 nat_validate_interface_counters (nat44_ei_main_t *nm, u32 sw_if_index)
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);
304 nat_validate_simple_counter (nm->counters.hairpinning, sw_if_index);
308 nat44_ei_add_del_addr_to_fib_foreach_out_if (ip4_address_t *addr, u8 is_add)
310 nat44_ei_main_t *nm = &nat44_ei_main;
311 nat44_ei_interface_t *i;
313 pool_foreach (i, nm->interfaces)
315 if (nat44_ei_interface_is_outside (i) && !nm->out2in_dpo)
317 nat44_ei_add_del_addr_to_fib (addr, 32, i->sw_if_index, is_add);
320 pool_foreach (i, nm->output_feature_interfaces)
322 if (nat44_ei_interface_is_outside (i) && !nm->out2in_dpo)
324 nat44_ei_add_del_addr_to_fib (addr, 32, i->sw_if_index, is_add);
329 static_always_inline void
330 nat44_ei_add_del_addr_to_fib_foreach_addr (u32 sw_if_index, u8 is_add)
332 nat44_ei_main_t *nm = &nat44_ei_main;
333 nat44_ei_address_t *ap;
335 vec_foreach (ap, nm->addresses)
337 nat44_ei_add_del_addr_to_fib (&ap->addr, 32, sw_if_index, is_add);
341 static_always_inline void
342 nat44_ei_add_del_addr_to_fib_foreach_addr_only_sm (u32 sw_if_index, u8 is_add)
344 nat44_ei_main_t *nm = &nat44_ei_main;
345 nat44_ei_static_mapping_t *m;
347 pool_foreach (m, nm->static_mappings)
349 if (is_sm_addr_only (m->flags) &&
350 !(m->local_addr.as_u32 == m->external_addr.as_u32))
352 nat44_ei_add_del_addr_to_fib (&m->external_addr, 32, sw_if_index,
359 nat44_ei_is_address_used_in_static_mapping (ip4_address_t addr)
361 nat44_ei_main_t *nm = &nat44_ei_main;
362 nat44_ei_static_mapping_t *m;
363 pool_foreach (m, nm->static_mappings)
365 if (is_sm_addr_only (m->flags) || is_sm_identity_nat (m->flags))
369 if (m->external_addr.as_u32 == addr.as_u32)
378 nat44_ei_init (vlib_main_t *vm)
380 nat44_ei_main_t *nm = &nat44_ei_main;
381 vlib_thread_main_t *tm = vlib_get_thread_main ();
382 vlib_thread_registration_t *tr;
383 ip4_add_del_interface_address_callback_t cbi = { 0 };
384 ip4_table_bind_callback_t cbt = { 0 };
385 u32 i, num_threads = 0;
386 uword *p, *bitmap = 0;
388 clib_memset (nm, 0, sizeof (*nm));
391 nm->vnet_main = vnet_get_main ();
393 nm->ip4_main = &ip4_main;
394 nm->api_main = vlibapi_get_main ();
395 nm->ip4_lookup_main = &ip4_main.lookup_main;
398 nm->fq_out2in_index = ~0;
399 nm->fq_in2out_index = ~0;
400 nm->fq_in2out_output_index = ~0;
402 nm->log_level = NAT_LOG_ERROR;
404 nat44_ei_set_node_indexes (nm, vm);
405 nm->log_class = vlib_log_register_class ("nat44-ei", 0);
407 nat_init_simple_counter (nm->total_users, "total-users",
408 "/nat44-ei/total-users");
409 nat_init_simple_counter (nm->total_sessions, "total-sessions",
410 "/nat44-ei/total-sessions");
411 nat_init_simple_counter (nm->user_limit_reached, "user-limit-reached",
412 "/nat44-ei/user-limit-reached");
415 nat_init_simple_counter (nm->counters.fastpath.in2out.x, #x, \
416 "/nat44-ei/in2out/fastpath/" #x); \
417 nat_init_simple_counter (nm->counters.fastpath.out2in.x, #x, \
418 "/nat44-ei/out2in/fastpath/" #x); \
419 nat_init_simple_counter (nm->counters.slowpath.in2out.x, #x, \
420 "/nat44-ei/in2out/slowpath/" #x); \
421 nat_init_simple_counter (nm->counters.slowpath.out2in.x, #x, \
422 "/nat44-ei/out2in/slowpath/" #x);
425 nat_init_simple_counter (nm->counters.hairpinning, "hairpinning",
426 "/nat44-ei/hairpinning");
428 p = hash_get_mem (tm->thread_registrations_by_name, "workers");
431 tr = (vlib_thread_registration_t *) p[0];
434 nm->num_workers = tr->count;
435 nm->first_worker_index = tr->first_index;
438 num_threads = tm->n_vlib_mains - 1;
439 nm->port_per_thread = 0xffff - 1024;
440 vec_validate (nm->per_thread_data, num_threads);
442 /* Use all available workers by default */
443 if (nm->num_workers > 1)
445 for (i = 0; i < nm->num_workers; i++)
446 bitmap = clib_bitmap_set (bitmap, i, 1);
447 nat44_ei_set_workers (bitmap);
448 clib_bitmap_free (bitmap);
452 nm->per_thread_data[0].snat_thread_index = 0;
455 /* callbacks to call when interface address changes. */
456 cbi.function = nat44_ei_ip4_add_del_interface_address_cb;
457 vec_add1 (nm->ip4_main->add_del_interface_address_callbacks, cbi);
458 cbi.function = nat44_ei_ip4_add_del_addr_only_sm_cb;
459 vec_add1 (nm->ip4_main->add_del_interface_address_callbacks, cbi);
461 /* callbacks to call when interface to table biding changes */
462 cbt.function = nat44_ei_update_outside_fib;
463 vec_add1 (nm->ip4_main->table_bind_callbacks, cbt);
465 nm->fib_src_low = fib_source_allocate (
466 "nat44-ei-low", FIB_SOURCE_PRIORITY_LOW, FIB_SOURCE_BH_SIMPLE);
467 nm->fib_src_hi = fib_source_allocate ("nat44-ei-hi", FIB_SOURCE_PRIORITY_HI,
468 FIB_SOURCE_BH_SIMPLE);
470 // used only by out2in-dpo feature
471 nat_dpo_module_init ();
472 nat_ha_init (vm, nm->num_workers, num_threads);
474 nm->hairpinning_fq_index =
475 vlib_frame_queue_main_init (nat44_ei_hairpinning_node.index, 0);
476 nm->hairpin_dst_fq_index =
477 vlib_frame_queue_main_init (nat44_ei_hairpin_dst_node.index, 0);
478 nm->in2out_hairpinning_finish_ip4_lookup_node_fq_index =
479 vlib_frame_queue_main_init (
480 nat44_ei_in2out_hairpinning_finish_ip4_lookup_node.index, 0);
481 nm->in2out_hairpinning_finish_interface_output_node_fq_index =
482 vlib_frame_queue_main_init (
483 nat44_ei_in2out_hairpinning_finish_interface_output_node.index, 0);
484 return nat44_ei_api_hookup (vm);
487 VLIB_INIT_FUNCTION (nat44_ei_init);
490 nat44_ei_plugin_enable (nat44_ei_config_t c)
492 nat44_ei_main_t *nm = &nat44_ei_main;
500 c.sessions = 10 * 1024;
502 if (!c.user_sessions)
503 c.user_sessions = c.sessions;
507 if (!nm->frame_queue_nelts)
508 nm->frame_queue_nelts = NAT_FQ_NELTS_DEFAULT;
510 nm->translations = c.sessions;
511 nm->translation_buckets = nat_calc_bihash_buckets (c.sessions);
512 nm->user_buckets = nat_calc_bihash_buckets (c.users);
514 nm->pat = (!c.static_mapping_only ||
515 (c.static_mapping_only && c.connection_tracking));
517 nm->static_mapping_only = c.static_mapping_only;
518 nm->static_mapping_connection_tracking = c.connection_tracking;
519 nm->out2in_dpo = c.out2in_dpo;
520 nm->forwarding_enabled = 0;
521 nm->mss_clamping = 0;
523 nm->max_users_per_thread = c.users;
524 nm->max_translations_per_thread = c.sessions;
525 nm->max_translations_per_user = c.user_sessions;
527 nm->inside_vrf_id = c.inside_vrf;
528 nm->inside_fib_index = fib_table_find_or_create_and_lock (
529 FIB_PROTOCOL_IP4, c.inside_vrf, nm->fib_src_hi);
531 nm->outside_vrf_id = c.outside_vrf;
532 nm->outside_fib_index = fib_table_find_or_create_and_lock (
533 FIB_PROTOCOL_IP4, c.outside_vrf, nm->fib_src_hi);
535 nat_reset_timeouts (&nm->timeouts);
536 nat44_ei_db_init (nm->translations, nm->translation_buckets,
538 nat44_ei_set_alloc_default ();
540 // TODO: zero simple counter for all counters missing
542 vlib_zero_simple_counter (&nm->total_users, 0);
543 vlib_zero_simple_counter (&nm->total_sessions, 0);
544 vlib_zero_simple_counter (&nm->user_limit_reached, 0);
546 if (nm->num_workers > 1)
548 if (nm->fq_in2out_index == ~0)
550 nm->fq_in2out_index = vlib_frame_queue_main_init (
551 nm->in2out_node_index, nm->frame_queue_nelts);
553 if (nm->fq_out2in_index == ~0)
555 nm->fq_out2in_index = vlib_frame_queue_main_init (
556 nm->out2in_node_index, nm->frame_queue_nelts);
558 if (nm->fq_in2out_output_index == ~0)
560 nm->fq_in2out_output_index = vlib_frame_queue_main_init (
561 nm->in2out_output_node_index, nm->frame_queue_nelts);
572 nat44_ei_addresses_free (nat44_ei_address_t **addresses)
574 nat44_ei_address_t *ap;
575 vec_foreach (ap, *addresses)
577 #define _(N, i, n, s) vec_free (ap->busy_##n##_ports_per_thread);
581 vec_free (*addresses);
585 static_always_inline nat44_ei_outside_fib_t *
586 nat44_ei_get_outside_fib (nat44_ei_outside_fib_t *outside_fibs, u32 fib_index)
588 nat44_ei_outside_fib_t *f;
589 vec_foreach (f, outside_fibs)
591 if (f->fib_index == fib_index)
599 static_always_inline nat44_ei_interface_t *
600 nat44_ei_get_interface (nat44_ei_interface_t *interfaces, u32 sw_if_index)
602 nat44_ei_interface_t *i;
603 pool_foreach (i, interfaces)
605 if (i->sw_if_index == sw_if_index)
614 nat44_ei_add_interface (u32 sw_if_index, u8 is_inside)
616 const char *feature_name, *del_feature_name;
617 nat44_ei_main_t *nm = &nat44_ei_main;
619 nat44_ei_outside_fib_t *outside_fib;
620 nat44_ei_interface_t *i;
626 if (nm->out2in_dpo && !is_inside)
628 nat44_ei_log_err ("error unsupported");
629 return VNET_API_ERROR_UNSUPPORTED;
632 if (nat44_ei_get_interface (nm->output_feature_interfaces, sw_if_index))
634 nat44_ei_log_err ("error interface already configured");
635 return VNET_API_ERROR_VALUE_EXIST;
638 i = nat44_ei_get_interface (nm->interfaces, sw_if_index);
641 if ((nat44_ei_interface_is_inside (i) && is_inside) ||
642 (nat44_ei_interface_is_outside (i) && !is_inside))
646 if (nm->num_workers > 1)
648 del_feature_name = !is_inside ? "nat44-ei-in2out-worker-handoff" :
649 "nat44-ei-out2in-worker-handoff";
650 feature_name = "nat44-ei-handoff-classify";
655 !is_inside ? "nat44-ei-in2out" : "nat44-ei-out2in";
657 feature_name = "nat44-ei-classify";
660 rv = ip4_sv_reass_enable_disable_with_refcnt (sw_if_index, 1);
665 rv = vnet_feature_enable_disable ("ip4-unicast", del_feature_name,
666 sw_if_index, 0, 0, 0);
671 rv = vnet_feature_enable_disable ("ip4-unicast", feature_name,
672 sw_if_index, 1, 0, 0);
679 rv = vnet_feature_enable_disable (
680 "ip4-local", "nat44-ei-hairpinning", sw_if_index, 0, 0, 0);
689 if (nm->num_workers > 1)
691 feature_name = is_inside ? "nat44-ei-in2out-worker-handoff" :
692 "nat44-ei-out2in-worker-handoff";
696 feature_name = is_inside ? "nat44-ei-in2out" : "nat44-ei-out2in";
698 nat_validate_interface_counters (nm, sw_if_index);
700 rv = ip4_sv_reass_enable_disable_with_refcnt (sw_if_index, 1);
705 rv = vnet_feature_enable_disable ("ip4-unicast", feature_name,
706 sw_if_index, 1, 0, 0);
711 if (is_inside && !nm->out2in_dpo)
713 rv = vnet_feature_enable_disable (
714 "ip4-local", "nat44-ei-hairpinning", sw_if_index, 1, 0, 0);
721 pool_get (nm->interfaces, i);
722 i->sw_if_index = sw_if_index;
727 fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4, sw_if_index);
731 i->flags |= NAT44_EI_INTERFACE_FLAG_IS_OUTSIDE;
733 outside_fib = nat44_ei_get_outside_fib (nm->outside_fibs, fib_index);
736 outside_fib->refcount++;
740 vec_add2 (nm->outside_fibs, outside_fib, 1);
741 outside_fib->fib_index = fib_index;
742 outside_fib->refcount = 1;
745 nat44_ei_add_del_addr_to_fib_foreach_addr (sw_if_index, 1);
746 nat44_ei_add_del_addr_to_fib_foreach_addr_only_sm (sw_if_index, 1);
750 i->flags |= NAT44_EI_INTERFACE_FLAG_IS_INSIDE;
757 nat44_ei_del_interface (u32 sw_if_index, u8 is_inside)
759 const char *feature_name, *del_feature_name;
760 nat44_ei_main_t *nm = &nat44_ei_main;
762 nat44_ei_outside_fib_t *outside_fib;
763 nat44_ei_interface_t *i;
769 if (nm->out2in_dpo && !is_inside)
771 nat44_ei_log_err ("error unsupported");
772 return VNET_API_ERROR_UNSUPPORTED;
775 i = nat44_ei_get_interface (nm->interfaces, sw_if_index);
778 nat44_ei_log_err ("error interface couldn't be found");
779 return VNET_API_ERROR_NO_SUCH_ENTRY;
782 if (nat44_ei_interface_is_inside (i) && nat44_ei_interface_is_outside (i))
784 if (nm->num_workers > 1)
786 del_feature_name = "nat44-ei-handoff-classify";
787 feature_name = !is_inside ? "nat44-ei-in2out-worker-handoff" :
788 "nat44-ei-out2in-worker-handoff";
792 del_feature_name = "nat44-ei-classify";
793 feature_name = !is_inside ? "nat44-ei-in2out" : "nat44-ei-out2in";
796 rv = ip4_sv_reass_enable_disable_with_refcnt (sw_if_index, 0);
801 rv = vnet_feature_enable_disable ("ip4-unicast", del_feature_name,
802 sw_if_index, 0, 0, 0);
807 rv = vnet_feature_enable_disable ("ip4-unicast", feature_name,
808 sw_if_index, 1, 0, 0);
815 i->flags &= ~NAT44_EI_INTERFACE_FLAG_IS_INSIDE;
819 rv = vnet_feature_enable_disable (
820 "ip4-local", "nat44-ei-hairpinning", sw_if_index, 1, 0, 0);
825 i->flags &= ~NAT44_EI_INTERFACE_FLAG_IS_OUTSIDE;
830 if (nm->num_workers > 1)
832 feature_name = is_inside ? "nat44-ei-in2out-worker-handoff" :
833 "nat44-ei-out2in-worker-handoff";
837 feature_name = is_inside ? "nat44-ei-in2out" : "nat44-ei-out2in";
840 rv = ip4_sv_reass_enable_disable_with_refcnt (sw_if_index, 0);
845 rv = vnet_feature_enable_disable ("ip4-unicast", feature_name,
846 sw_if_index, 0, 0, 0);
853 rv = vnet_feature_enable_disable (
854 "ip4-local", "nat44-ei-hairpinning", sw_if_index, 0, 0, 0);
862 pool_put (nm->interfaces, i);
868 fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4, sw_if_index);
869 outside_fib = nat44_ei_get_outside_fib (nm->outside_fibs, fib_index);
872 outside_fib->refcount--;
873 if (!outside_fib->refcount)
875 vec_del1 (nm->outside_fibs, outside_fib - nm->outside_fibs);
879 nat44_ei_add_del_addr_to_fib_foreach_addr (sw_if_index, 0);
880 nat44_ei_add_del_addr_to_fib_foreach_addr_only_sm (sw_if_index, 0);
887 nat44_ei_add_output_interface (u32 sw_if_index)
889 nat44_ei_main_t *nm = &nat44_ei_main;
891 nat44_ei_outside_fib_t *outside_fib;
892 nat44_ei_interface_t *i;
898 if (nat44_ei_get_interface (nm->interfaces, sw_if_index))
900 nat44_ei_log_err ("error interface already configured");
901 return VNET_API_ERROR_VALUE_EXIST;
904 if (nat44_ei_get_interface (nm->output_feature_interfaces, sw_if_index))
906 nat44_ei_log_err ("error interface already configured");
907 return VNET_API_ERROR_VALUE_EXIST;
910 if (nm->num_workers > 1)
912 rv = ip4_sv_reass_enable_disable_with_refcnt (sw_if_index, 1);
917 rv = ip4_sv_reass_output_enable_disable_with_refcnt (sw_if_index, 1);
922 rv = vnet_feature_enable_disable (
923 "ip4-unicast", "nat44-ei-out2in-worker-handoff", sw_if_index, 1, 0, 0);
928 rv = vnet_feature_enable_disable (
929 "ip4-output", "nat44-ei-in2out-output-worker-handoff", sw_if_index, 1,
938 rv = ip4_sv_reass_enable_disable_with_refcnt (sw_if_index, 1);
943 rv = ip4_sv_reass_output_enable_disable_with_refcnt (sw_if_index, 1);
948 rv = vnet_feature_enable_disable ("ip4-unicast", "nat44-ei-out2in",
949 sw_if_index, 1, 0, 0);
954 rv = vnet_feature_enable_disable ("ip4-output", "nat44-ei-in2out-output",
955 sw_if_index, 1, 0, 0);
962 nat_validate_interface_counters (nm, sw_if_index);
964 pool_get (nm->output_feature_interfaces, i);
965 i->sw_if_index = sw_if_index;
967 i->flags |= NAT44_EI_INTERFACE_FLAG_IS_INSIDE;
968 i->flags |= NAT44_EI_INTERFACE_FLAG_IS_OUTSIDE;
971 fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4, sw_if_index);
972 outside_fib = nat44_ei_get_outside_fib (nm->outside_fibs, fib_index);
975 outside_fib->refcount++;
979 vec_add2 (nm->outside_fibs, outside_fib, 1);
980 outside_fib->fib_index = fib_index;
981 outside_fib->refcount = 1;
984 nat44_ei_add_del_addr_to_fib_foreach_addr (sw_if_index, 1);
985 nat44_ei_add_del_addr_to_fib_foreach_addr_only_sm (sw_if_index, 1);
991 nat44_ei_del_output_interface (u32 sw_if_index)
993 nat44_ei_main_t *nm = &nat44_ei_main;
995 nat44_ei_outside_fib_t *outside_fib;
996 nat44_ei_interface_t *i;
1000 fail_if_disabled ();
1002 i = nat44_ei_get_interface (nm->output_feature_interfaces, sw_if_index);
1005 nat44_ei_log_err ("error interface couldn't be found");
1006 return VNET_API_ERROR_NO_SUCH_ENTRY;
1009 if (nm->num_workers > 1)
1011 rv = ip4_sv_reass_enable_disable_with_refcnt (sw_if_index, 0);
1016 rv = ip4_sv_reass_output_enable_disable_with_refcnt (sw_if_index, 0);
1021 rv = vnet_feature_enable_disable (
1022 "ip4-unicast", "nat44-ei-out2in-worker-handoff", sw_if_index, 0, 0, 0);
1027 rv = vnet_feature_enable_disable (
1028 "ip4-output", "nat44-ei-in2out-output-worker-handoff", sw_if_index, 0,
1037 rv = ip4_sv_reass_enable_disable_with_refcnt (sw_if_index, 0);
1042 rv = ip4_sv_reass_output_enable_disable_with_refcnt (sw_if_index, 0);
1047 rv = vnet_feature_enable_disable ("ip4-unicast", "nat44-ei-out2in",
1048 sw_if_index, 0, 0, 0);
1053 rv = vnet_feature_enable_disable ("ip4-output", "nat44-ei-in2out-output",
1054 sw_if_index, 0, 0, 0);
1061 pool_put (nm->output_feature_interfaces, i);
1064 fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4, sw_if_index);
1065 outside_fib = nat44_ei_get_outside_fib (nm->outside_fibs, fib_index);
1068 outside_fib->refcount--;
1069 if (!outside_fib->refcount)
1071 vec_del1 (nm->outside_fibs, outside_fib - nm->outside_fibs);
1075 nat44_ei_add_del_addr_to_fib_foreach_addr (sw_if_index, 1);
1076 nat44_ei_add_del_addr_to_fib_foreach_addr_only_sm (sw_if_index, 1);
1082 nat44_ei_plugin_disable ()
1084 nat44_ei_main_t *nm = &nat44_ei_main;
1085 nat44_ei_interface_t *i, *pool;
1088 // first unregister all nodes from interfaces
1089 pool = pool_dup (nm->interfaces);
1090 pool_foreach (i, pool)
1092 if (nat44_ei_interface_is_inside (i))
1094 error = nat44_ei_del_interface (i->sw_if_index, 1);
1096 if (nat44_ei_interface_is_outside (i))
1098 error = nat44_ei_del_interface (i->sw_if_index, 0);
1103 nat44_ei_log_err ("error occurred while removing interface %u",
1108 pool_free (nm->interfaces);
1110 pool = pool_dup (nm->output_feature_interfaces);
1111 pool_foreach (i, pool)
1113 error = nat44_ei_del_output_interface (i->sw_if_index);
1116 nat44_ei_log_err ("error occurred while removing interface %u",
1121 pool_free (nm->output_feature_interfaces);
1124 nat44_ei_db_free ();
1126 nat44_ei_addresses_free (&nm->addresses);
1128 vec_free (nm->to_resolve);
1129 vec_free (nm->auto_add_sw_if_indices);
1132 nm->auto_add_sw_if_indices = 0;
1134 nm->forwarding_enabled = 0;
1137 clib_memset (&nm->rconfig, 0, sizeof (nm->rconfig));
1143 nat44_ei_set_outside_address_and_port (nat44_ei_address_t *addresses,
1144 u32 thread_index, ip4_address_t addr,
1145 u16 port, nat_protocol_t protocol)
1147 nat44_ei_main_t *nm = &nat44_ei_main;
1148 nat44_ei_address_t *a = 0;
1150 u16 port_host_byte_order = clib_net_to_host_u16 (port);
1152 for (address_index = 0; address_index < vec_len (addresses); address_index++)
1154 if (addresses[address_index].addr.as_u32 != addr.as_u32)
1157 a = addresses + address_index;
1160 #define _(N, j, n, s) \
1161 case NAT_PROTOCOL_##N: \
1162 if (a->busy_##n##_port_refcounts[port_host_byte_order]) \
1163 return VNET_API_ERROR_INSTANCE_IN_USE; \
1164 ++a->busy_##n##_port_refcounts[port_host_byte_order]; \
1165 a->busy_##n##_ports_per_thread[thread_index]++; \
1166 a->busy_##n##_ports++; \
1168 foreach_nat_protocol
1170 default : nat_elog_info (nm, "unknown protocol");
1175 return VNET_API_ERROR_NO_SUCH_ENTRY;
1179 nat44_ei_add_del_address_dpo (ip4_address_t addr, u8 is_add)
1181 nat44_ei_main_t *nm = &nat44_ei_main;
1182 dpo_id_t dpo_v4 = DPO_INVALID;
1183 fib_prefix_t pfx = {
1184 .fp_proto = FIB_PROTOCOL_IP4,
1186 .fp_addr.ip4.as_u32 = addr.as_u32,
1191 nat_dpo_create (DPO_PROTO_IP4, 0, &dpo_v4);
1192 fib_table_entry_special_dpo_add (0, &pfx, nm->fib_src_hi,
1193 FIB_ENTRY_FLAG_EXCLUSIVE, &dpo_v4);
1194 dpo_reset (&dpo_v4);
1198 fib_table_entry_special_remove (0, &pfx, nm->fib_src_hi);
1203 nat44_ei_free_outside_address_and_port (nat44_ei_address_t *addresses,
1204 u32 thread_index, ip4_address_t *addr,
1205 u16 port, nat_protocol_t protocol)
1207 nat44_ei_main_t *nm = &nat44_ei_main;
1208 nat44_ei_address_t *a;
1210 u16 port_host_byte_order = clib_net_to_host_u16 (port);
1212 for (address_index = 0; address_index < vec_len (addresses); address_index++)
1214 if (addresses[address_index].addr.as_u32 == addr->as_u32)
1218 ASSERT (address_index < vec_len (addresses));
1220 a = addresses + address_index;
1224 #define _(N, i, n, s) \
1225 case NAT_PROTOCOL_##N: \
1226 ASSERT (a->busy_##n##_port_refcounts[port_host_byte_order] >= 1); \
1227 --a->busy_##n##_port_refcounts[port_host_byte_order]; \
1228 a->busy_##n##_ports--; \
1229 a->busy_##n##_ports_per_thread[thread_index]--; \
1231 foreach_nat_protocol
1233 default : nat_elog_info (nm, "unknown protocol");
1239 nat44_ei_free_session_data_v2 (nat44_ei_main_t *nm, nat44_ei_session_t *s,
1240 u32 thread_index, u8 is_ha)
1242 clib_bihash_kv_8_8_t kv;
1244 /* session lookup tables */
1245 init_nat_i2o_k (&kv, s);
1246 if (clib_bihash_add_del_8_8 (&nm->in2out, &kv, 0))
1247 nat_elog_warn (nm, "in2out key del failed");
1248 init_nat_o2i_k (&kv, s);
1249 if (clib_bihash_add_del_8_8 (&nm->out2in, &kv, 0))
1250 nat_elog_warn (nm, "out2in key del failed");
1253 nat_syslog_nat44_apmdel (s->user_index, s->in2out.fib_index,
1254 &s->in2out.addr, s->in2out.port, &s->out2in.addr,
1255 s->out2in.port, s->nat_proto);
1257 if (nat44_ei_is_unk_proto_session (s))
1263 nat_ipfix_logging_nat44_ses_delete (
1264 thread_index, s->in2out.addr.as_u32, s->out2in.addr.as_u32,
1265 nat_proto_to_ip_proto (s->nat_proto), s->in2out.port, s->out2in.port,
1266 s->in2out.fib_index);
1268 nat_ha_sdel (&s->out2in.addr, s->out2in.port, &s->ext_host_addr,
1269 s->ext_host_port, s->nat_proto, s->out2in.fib_index,
1273 if (nat44_ei_is_session_static (s))
1276 nat44_ei_free_outside_address_and_port (nm->addresses, thread_index,
1277 &s->out2in.addr, s->out2in.port,
1282 nat44_ei_user_get_or_create (nat44_ei_main_t *nm, ip4_address_t *addr,
1283 u32 fib_index, u32 thread_index)
1285 nat44_ei_user_t *u = 0;
1286 nat44_ei_user_key_t user_key;
1287 clib_bihash_kv_8_8_t kv, value;
1288 nat44_ei_main_per_thread_data_t *tnm = &nm->per_thread_data[thread_index];
1289 dlist_elt_t *per_user_list_head_elt;
1291 user_key.addr.as_u32 = addr->as_u32;
1292 user_key.fib_index = fib_index;
1293 kv.key = user_key.as_u64;
1295 /* Ever heard of the "user" = src ip4 address before? */
1296 if (clib_bihash_search_8_8 (&tnm->user_hash, &kv, &value))
1298 if (pool_elts (tnm->users) >= nm->max_users_per_thread)
1300 vlib_increment_simple_counter (&nm->user_limit_reached, thread_index,
1302 nat_elog_warn (nm, "maximum user limit reached");
1305 /* no, make a new one */
1306 pool_get (tnm->users, u);
1307 clib_memset (u, 0, sizeof (*u));
1309 u->addr.as_u32 = addr->as_u32;
1310 u->fib_index = fib_index;
1312 pool_get (tnm->list_pool, per_user_list_head_elt);
1314 u->sessions_per_user_list_head_index =
1315 per_user_list_head_elt - tnm->list_pool;
1317 clib_dlist_init (tnm->list_pool, u->sessions_per_user_list_head_index);
1319 kv.value = u - tnm->users;
1322 if (clib_bihash_add_del_8_8 (&tnm->user_hash, &kv, 1))
1324 nat_elog_warn (nm, "user_hash key add failed");
1325 nat44_ei_delete_user_with_no_session (nm, u, thread_index);
1329 vlib_set_simple_counter (&nm->total_users, thread_index, 0,
1330 pool_elts (tnm->users));
1334 u = pool_elt_at_index (tnm->users, value.value);
1340 nat44_ei_session_t *
1341 nat44_ei_session_alloc_or_recycle (nat44_ei_main_t *nm, nat44_ei_user_t *u,
1342 u32 thread_index, f64 now)
1344 nat44_ei_session_t *s;
1345 nat44_ei_main_per_thread_data_t *tnm = &nm->per_thread_data[thread_index];
1346 u32 oldest_per_user_translation_list_index, session_index;
1347 dlist_elt_t *oldest_per_user_translation_list_elt;
1348 dlist_elt_t *per_user_translation_list_elt;
1350 /* Over quota? Recycle the least recently used translation */
1351 if ((u->nsessions + u->nstaticsessions) >= nm->max_translations_per_user)
1353 oldest_per_user_translation_list_index = clib_dlist_remove_head (
1354 tnm->list_pool, u->sessions_per_user_list_head_index);
1356 ASSERT (oldest_per_user_translation_list_index != ~0);
1358 /* Add it back to the end of the LRU list */
1359 clib_dlist_addtail (tnm->list_pool, u->sessions_per_user_list_head_index,
1360 oldest_per_user_translation_list_index);
1361 /* Get the list element */
1362 oldest_per_user_translation_list_elt = pool_elt_at_index (
1363 tnm->list_pool, oldest_per_user_translation_list_index);
1365 /* Get the session index from the list element */
1366 session_index = oldest_per_user_translation_list_elt->value;
1368 /* Get the session */
1369 s = pool_elt_at_index (tnm->sessions, session_index);
1371 nat44_ei_free_session_data_v2 (nm, s, thread_index, 0);
1372 if (nat44_ei_is_session_static (s))
1373 u->nstaticsessions--;
1380 s->ext_host_addr.as_u32 = 0;
1381 s->ext_host_port = 0;
1382 s->ext_host_nat_addr.as_u32 = 0;
1383 s->ext_host_nat_port = 0;
1387 pool_get (tnm->sessions, s);
1388 clib_memset (s, 0, sizeof (*s));
1390 /* Create list elts */
1391 pool_get (tnm->list_pool, per_user_translation_list_elt);
1392 clib_dlist_init (tnm->list_pool,
1393 per_user_translation_list_elt - tnm->list_pool);
1395 per_user_translation_list_elt->value = s - tnm->sessions;
1396 s->per_user_index = per_user_translation_list_elt - tnm->list_pool;
1397 s->per_user_list_head_index = u->sessions_per_user_list_head_index;
1399 clib_dlist_addtail (tnm->list_pool, s->per_user_list_head_index,
1400 per_user_translation_list_elt - tnm->list_pool);
1402 s->user_index = u - tnm->users;
1403 vlib_set_simple_counter (&nm->total_sessions, thread_index, 0,
1404 pool_elts (tnm->sessions));
1407 s->ha_last_refreshed = now;
1413 nat44_ei_free_session_data (nat44_ei_main_t *nm, nat44_ei_session_t *s,
1414 u32 thread_index, u8 is_ha)
1416 clib_bihash_kv_8_8_t kv;
1418 init_nat_i2o_k (&kv, s);
1419 if (clib_bihash_add_del_8_8 (&nm->in2out, &kv, 0))
1420 nat_elog_warn (nm, "in2out key del failed");
1422 init_nat_o2i_k (&kv, s);
1423 if (clib_bihash_add_del_8_8 (&nm->out2in, &kv, 0))
1424 nat_elog_warn (nm, "out2in key del failed");
1428 nat_syslog_nat44_apmdel (s->user_index, s->in2out.fib_index,
1429 &s->in2out.addr, s->in2out.port,
1430 &s->out2in.addr, s->out2in.port, s->nat_proto);
1432 nat_ipfix_logging_nat44_ses_delete (
1433 thread_index, s->in2out.addr.as_u32, s->out2in.addr.as_u32,
1434 nat_proto_to_ip_proto (s->nat_proto), s->in2out.port, s->out2in.port,
1435 s->in2out.fib_index);
1437 nat_ha_sdel (&s->out2in.addr, s->out2in.port, &s->ext_host_addr,
1438 s->ext_host_port, s->nat_proto, s->out2in.fib_index,
1442 if (nat44_ei_is_session_static (s))
1445 nat44_ei_free_outside_address_and_port (nm->addresses, thread_index,
1446 &s->out2in.addr, s->out2in.port,
1450 static_always_inline void
1451 nat44_ei_user_del_sessions (nat44_ei_user_t *u, u32 thread_index)
1454 nat44_ei_session_t *s;
1456 nat44_ei_main_t *nm = &nat44_ei_main;
1457 nat44_ei_main_per_thread_data_t *tnm = &nm->per_thread_data[thread_index];
1461 pool_elt_at_index (tnm->list_pool, u->sessions_per_user_list_head_index);
1462 // get first element
1463 elt = pool_elt_at_index (tnm->list_pool, elt->next);
1465 while (elt->value != ~0)
1467 s = pool_elt_at_index (tnm->sessions, elt->value);
1468 elt = pool_elt_at_index (tnm->list_pool, elt->next);
1470 nat44_ei_free_session_data (nm, s, thread_index, 0);
1471 nat44_ei_delete_session (nm, s, thread_index);
1476 nat44_ei_user_del (ip4_address_t *addr, u32 fib_index)
1480 nat44_ei_main_t *nm = &nat44_ei_main;
1481 nat44_ei_main_per_thread_data_t *tnm;
1483 nat44_ei_user_key_t user_key;
1484 clib_bihash_kv_8_8_t kv, value;
1486 user_key.addr.as_u32 = addr->as_u32;
1487 user_key.fib_index = fib_index;
1488 kv.key = user_key.as_u64;
1490 if (nm->num_workers > 1)
1492 vec_foreach (tnm, nm->per_thread_data)
1494 if (!clib_bihash_search_8_8 (&tnm->user_hash, &kv, &value))
1496 nat44_ei_user_del_sessions (
1497 pool_elt_at_index (tnm->users, value.value),
1506 tnm = vec_elt_at_index (nm->per_thread_data, nm->num_workers);
1507 if (!clib_bihash_search_8_8 (&tnm->user_hash, &kv, &value))
1509 nat44_ei_user_del_sessions (
1510 pool_elt_at_index (tnm->users, value.value), tnm->thread_index);
1518 nat44_ei_static_mapping_del_sessions (nat44_ei_main_t *nm,
1519 nat44_ei_main_per_thread_data_t *tnm,
1520 nat44_ei_user_key_t u_key, int addr_only,
1521 ip4_address_t e_addr, u16 e_port)
1523 clib_bihash_kv_8_8_t kv, value;
1524 kv.key = u_key.as_u64;
1526 dlist_elt_t *head, *elt;
1528 nat44_ei_session_t *s;
1529 u32 elt_index, head_index, ses_index;
1531 if (!clib_bihash_search_8_8 (&tnm->user_hash, &kv, &value))
1533 user_index = value.value;
1534 u = pool_elt_at_index (tnm->users, user_index);
1535 if (u->nstaticsessions)
1537 head_index = u->sessions_per_user_list_head_index;
1538 head = pool_elt_at_index (tnm->list_pool, head_index);
1539 elt_index = head->next;
1540 elt = pool_elt_at_index (tnm->list_pool, elt_index);
1541 ses_index = elt->value;
1542 while (ses_index != ~0)
1544 s = pool_elt_at_index (tnm->sessions, ses_index);
1545 elt = pool_elt_at_index (tnm->list_pool, elt->next);
1546 ses_index = elt->value;
1550 if ((s->out2in.addr.as_u32 != e_addr.as_u32) ||
1551 (s->out2in.port != e_port))
1555 if (!nat44_ei_is_session_static (s))
1558 nat44_ei_free_session_data_v2 (nm, s, tnm - nm->per_thread_data,
1560 nat44_ei_delete_session (nm, s, tnm - nm->per_thread_data);
1570 nat44_ei_get_in2out_worker_index (ip4_header_t *ip0, u32 rx_fib_index0,
1573 nat44_ei_main_t *nm = &nat44_ei_main;
1574 u32 next_worker_index = 0;
1577 next_worker_index = nm->first_worker_index;
1578 hash = ip0->src_address.as_u32 + (ip0->src_address.as_u32 >> 8) +
1579 (ip0->src_address.as_u32 >> 16) + (ip0->src_address.as_u32 >> 24);
1581 if (PREDICT_TRUE (is_pow2 (_vec_len (nm->workers))))
1582 next_worker_index += nm->workers[hash & (_vec_len (nm->workers) - 1)];
1584 next_worker_index += nm->workers[hash % _vec_len (nm->workers)];
1586 return next_worker_index;
1590 nat44_ei_get_out2in_worker_index (vlib_buffer_t *b, ip4_header_t *ip0,
1591 u32 rx_fib_index0, u8 is_output)
1593 nat44_ei_main_t *nm = &nat44_ei_main;
1596 clib_bihash_kv_8_8_t kv, value;
1597 nat44_ei_static_mapping_t *m;
1599 u32 next_worker_index = 0;
1601 /* first try static mappings without port */
1602 if (PREDICT_FALSE (pool_elts (nm->static_mappings)))
1604 init_nat_k (&kv, ip0->dst_address, 0, rx_fib_index0, 0);
1605 if (!clib_bihash_search_8_8 (&nm->static_mapping_by_external, &kv,
1608 m = pool_elt_at_index (nm->static_mappings, value.value);
1609 return m->workers[0];
1613 proto = ip_proto_to_nat_proto (ip0->protocol);
1614 udp = ip4_next_header (ip0);
1615 port = vnet_buffer (b)->ip.reass.l4_dst_port;
1617 /* unknown protocol */
1618 if (PREDICT_FALSE (proto == NAT_PROTOCOL_OTHER))
1620 /* use current thread */
1621 return vlib_get_thread_index ();
1624 if (PREDICT_FALSE (ip0->protocol == IP_PROTOCOL_ICMP))
1626 icmp46_header_t *icmp = (icmp46_header_t *) udp;
1627 icmp_echo_header_t *echo = (icmp_echo_header_t *) (icmp + 1);
1628 if (!icmp_type_is_error_message (
1629 vnet_buffer (b)->ip.reass.icmp_type_or_tcp_flags))
1630 port = vnet_buffer (b)->ip.reass.l4_src_port;
1633 /* if error message, then it's not fragmented and we can access it */
1634 ip4_header_t *inner_ip = (ip4_header_t *) (echo + 1);
1635 proto = ip_proto_to_nat_proto (inner_ip->protocol);
1636 void *l4_header = ip4_next_header (inner_ip);
1639 case NAT_PROTOCOL_ICMP:
1640 icmp = (icmp46_header_t *) l4_header;
1641 echo = (icmp_echo_header_t *) (icmp + 1);
1642 port = echo->identifier;
1644 case NAT_PROTOCOL_UDP:
1645 case NAT_PROTOCOL_TCP:
1646 port = ((tcp_udp_header_t *) l4_header)->src_port;
1649 return vlib_get_thread_index ();
1654 /* try static mappings with port */
1655 if (PREDICT_FALSE (pool_elts (nm->static_mappings)))
1657 init_nat_k (&kv, ip0->dst_address, port, rx_fib_index0, proto);
1658 if (!clib_bihash_search_8_8 (&nm->static_mapping_by_external, &kv,
1661 m = pool_elt_at_index (nm->static_mappings, value.value);
1662 return m->workers[0];
1666 /* worker by outside port */
1667 next_worker_index = nm->first_worker_index;
1668 next_worker_index +=
1669 nm->workers[(clib_net_to_host_u16 (port) - 1024) / nm->port_per_thread];
1670 return next_worker_index;
1674 nat44_ei_alloc_default_cb (nat44_ei_address_t *addresses, u32 fib_index,
1675 u32 thread_index, nat_protocol_t proto,
1676 ip4_address_t s_addr, ip4_address_t *addr,
1677 u16 *port, u16 port_per_thread,
1678 u32 snat_thread_index)
1680 nat44_ei_main_t *nm = &nat44_ei_main;
1681 nat44_ei_address_t *a, *ga = 0;
1685 if (vec_len (addresses) > 0)
1688 int s_addr_offset = s_addr.as_u32 % vec_len (addresses);
1690 for (i = s_addr_offset; i < vec_len (addresses); ++i)
1695 #define _(N, j, n, s) \
1696 case NAT_PROTOCOL_##N: \
1697 if (a->busy_##n##_ports_per_thread[thread_index] < port_per_thread) \
1699 if (a->fib_index == fib_index) \
1703 portnum = (port_per_thread * snat_thread_index) + \
1704 nat_random_port (&nm->random_seed, 0, \
1705 port_per_thread - 1) + \
1707 if (a->busy_##n##_port_refcounts[portnum]) \
1709 --a->busy_##n##_port_refcounts[portnum]; \
1710 a->busy_##n##_ports_per_thread[thread_index]++; \
1711 a->busy_##n##_ports++; \
1713 *port = clib_host_to_net_u16 (portnum); \
1717 else if (a->fib_index == ~0) \
1723 foreach_nat_protocol;
1725 nat_elog_info (nm, "unknown protocol");
1730 for (i = 0; i < s_addr_offset; ++i)
1735 foreach_nat_protocol;
1737 nat_elog_info (nm, "unknown protocol");
1744 // fake fib index to reuse macro
1748 foreach_nat_protocol;
1749 default : nat_elog_info (nm, "unknown protocol");
1757 /* Totally out of translations to use... */
1758 nat_ipfix_logging_addresses_exhausted (thread_index, 0);
1763 nat44_ei_alloc_range_cb (nat44_ei_address_t *addresses, u32 fib_index,
1764 u32 thread_index, nat_protocol_t proto,
1765 ip4_address_t s_addr, ip4_address_t *addr, u16 *port,
1766 u16 port_per_thread, u32 snat_thread_index)
1768 nat44_ei_main_t *nm = &nat44_ei_main;
1769 nat44_ei_address_t *a = addresses;
1772 ports = nm->end_port - nm->start_port + 1;
1774 if (!vec_len (addresses))
1779 #define _(N, i, n, s) \
1780 case NAT_PROTOCOL_##N: \
1781 if (a->busy_##n##_ports < ports) \
1785 portnum = nat_random_port (&nm->random_seed, nm->start_port, \
1787 if (a->busy_##n##_port_refcounts[portnum]) \
1789 ++a->busy_##n##_port_refcounts[portnum]; \
1790 a->busy_##n##_ports++; \
1792 *port = clib_host_to_net_u16 (portnum); \
1797 foreach_nat_protocol
1799 default : nat_elog_info (nm, "unknown protocol");
1804 /* Totally out of translations to use... */
1805 nat_ipfix_logging_addresses_exhausted (thread_index, 0);
1810 nat44_ei_alloc_mape_cb (nat44_ei_address_t *addresses, u32 fib_index,
1811 u32 thread_index, nat_protocol_t proto,
1812 ip4_address_t s_addr, ip4_address_t *addr, u16 *port,
1813 u16 port_per_thread, u32 snat_thread_index)
1815 nat44_ei_main_t *nm = &nat44_ei_main;
1816 nat44_ei_address_t *a = addresses;
1817 u16 m, ports, portnum, A, j;
1818 m = 16 - (nm->psid_offset + nm->psid_length);
1819 ports = (1 << (16 - nm->psid_length)) - (1 << m);
1821 if (!vec_len (addresses))
1826 #define _(N, i, n, s) \
1827 case NAT_PROTOCOL_##N: \
1828 if (a->busy_##n##_ports < ports) \
1832 A = nat_random_port (&nm->random_seed, 1, \
1833 pow2_mask (nm->psid_offset)); \
1834 j = nat_random_port (&nm->random_seed, 0, pow2_mask (m)); \
1835 portnum = A | (nm->psid << nm->psid_offset) | (j << (16 - m)); \
1836 if (a->busy_##n##_port_refcounts[portnum]) \
1838 ++a->busy_##n##_port_refcounts[portnum]; \
1839 a->busy_##n##_ports++; \
1841 *port = clib_host_to_net_u16 (portnum); \
1846 foreach_nat_protocol
1848 default : nat_elog_info (nm, "unknown protocol");
1853 /* Totally out of translations to use... */
1854 nat_ipfix_logging_addresses_exhausted (thread_index, 0);
1859 nat44_ei_set_alloc_default ()
1861 nat44_ei_main_t *nm = &nat44_ei_main;
1863 nm->addr_and_port_alloc_alg = NAT44_EI_ADDR_AND_PORT_ALLOC_ALG_DEFAULT;
1864 nm->alloc_addr_and_port = nat44_ei_alloc_default_cb;
1868 nat44_ei_set_alloc_range (u16 start_port, u16 end_port)
1870 nat44_ei_main_t *nm = &nat44_ei_main;
1872 nm->addr_and_port_alloc_alg = NAT44_EI_ADDR_AND_PORT_ALLOC_ALG_RANGE;
1873 nm->alloc_addr_and_port = nat44_ei_alloc_range_cb;
1874 nm->start_port = start_port;
1875 nm->end_port = end_port;
1879 nat44_ei_set_alloc_mape (u16 psid, u16 psid_offset, u16 psid_length)
1881 nat44_ei_main_t *nm = &nat44_ei_main;
1883 nm->addr_and_port_alloc_alg = NAT44_EI_ADDR_AND_PORT_ALLOC_ALG_MAPE;
1884 nm->alloc_addr_and_port = nat44_ei_alloc_mape_cb;
1886 nm->psid_offset = psid_offset;
1887 nm->psid_length = psid_length;
1891 nat44_ei_delete_session (nat44_ei_main_t *nm, nat44_ei_session_t *ses,
1894 nat44_ei_main_per_thread_data_t *tnm =
1895 vec_elt_at_index (nm->per_thread_data, thread_index);
1896 clib_bihash_kv_8_8_t kv, value;
1898 const nat44_ei_user_key_t u_key = { .addr = ses->in2out.addr,
1899 .fib_index = ses->in2out.fib_index };
1900 const u8 u_static = nat44_ei_is_session_static (ses);
1902 clib_dlist_remove (tnm->list_pool, ses->per_user_index);
1903 pool_put_index (tnm->list_pool, ses->per_user_index);
1905 pool_put (tnm->sessions, ses);
1906 vlib_set_simple_counter (&nm->total_sessions, thread_index, 0,
1907 pool_elts (tnm->sessions));
1909 kv.key = u_key.as_u64;
1910 if (!clib_bihash_search_8_8 (&tnm->user_hash, &kv, &value))
1912 u = pool_elt_at_index (tnm->users, value.value);
1914 u->nstaticsessions--;
1918 nat44_ei_delete_user_with_no_session (nm, u, thread_index);
1923 nat44_ei_del_session (nat44_ei_main_t *nm, ip4_address_t *addr, u16 port,
1924 nat_protocol_t proto, u32 vrf_id, int is_in)
1926 nat44_ei_main_per_thread_data_t *tnm;
1927 clib_bihash_kv_8_8_t kv, value;
1929 nat44_ei_session_t *s;
1930 clib_bihash_8_8_t *t;
1932 fail_if_disabled ();
1934 fib_index = fib_table_find (FIB_PROTOCOL_IP4, vrf_id);
1935 init_nat_k (&kv, *addr, port, fib_index, proto);
1936 t = is_in ? &nm->in2out : &nm->out2in;
1937 if (!clib_bihash_search_8_8 (t, &kv, &value))
1939 // this is called from API/CLI, so the world is stopped here
1940 // it's safe to manipulate arbitrary per-thread data
1941 u32 thread_index = nat_value_get_thread_index (&value);
1942 tnm = vec_elt_at_index (nm->per_thread_data, thread_index);
1943 u32 session_index = nat_value_get_session_index (&value);
1944 if (pool_is_free_index (tnm->sessions, session_index))
1945 return VNET_API_ERROR_UNSPECIFIED;
1947 s = pool_elt_at_index (tnm->sessions, session_index);
1948 nat44_ei_free_session_data_v2 (nm, s, tnm - nm->per_thread_data, 0);
1949 nat44_ei_delete_session (nm, s, tnm - nm->per_thread_data);
1953 return VNET_API_ERROR_NO_SUCH_ENTRY;
1957 nat44_ei_get_thread_idx_by_port (u16 e_port)
1959 nat44_ei_main_t *nm = &nat44_ei_main;
1960 u32 thread_idx = nm->num_workers;
1961 if (nm->num_workers > 1)
1963 thread_idx = nm->first_worker_index +
1964 nm->workers[(e_port - 1024) / nm->port_per_thread];
1970 nat44_ei_add_del_addr_to_fib (ip4_address_t *addr, u8 p_len, u32 sw_if_index,
1973 nat44_ei_main_t *nm = &nat44_ei_main;
1974 fib_prefix_t prefix = {
1976 .fp_proto = FIB_PROTOCOL_IP4,
1978 .ip4.as_u32 = addr->as_u32,
1981 u32 fib_index = ip4_fib_table_get_index_for_sw_if_index (sw_if_index);
1985 fib_table_entry_update_one_path (fib_index, &prefix, nm->fib_src_low,
1986 (FIB_ENTRY_FLAG_CONNECTED |
1987 FIB_ENTRY_FLAG_LOCAL |
1988 FIB_ENTRY_FLAG_EXCLUSIVE),
1989 DPO_PROTO_IP4, NULL, sw_if_index, ~0, 1,
1990 NULL, FIB_ROUTE_PATH_FLAG_NONE);
1994 fib_table_entry_delete (fib_index, &prefix, nm->fib_src_low);
1999 nat44_ei_reserve_port (ip4_address_t addr, u16 port, nat_protocol_t proto)
2001 u32 ti = nat44_ei_get_thread_idx_by_port (port);
2002 nat44_ei_main_t *nm = &nat44_ei_main;
2003 nat44_ei_address_t *a = 0;
2006 for (i = 0; i < vec_len (nm->addresses); i++)
2008 a = nm->addresses + i;
2010 if (a->addr.as_u32 != addr.as_u32)
2015 #define _(N, j, n, s) \
2016 case NAT_PROTOCOL_##N: \
2017 if (a->busy_##n##_port_refcounts[port]) \
2019 ++a->busy_##n##_port_refcounts[port]; \
2022 a->busy_##n##_ports++; \
2023 a->busy_##n##_ports_per_thread[ti]++; \
2026 foreach_nat_protocol
2028 default : nat_elog_info (nm, "unknown protocol");
2040 nat44_ei_free_port (ip4_address_t addr, u16 port, nat_protocol_t proto)
2042 u32 ti = nat44_ei_get_thread_idx_by_port (port);
2043 nat44_ei_main_t *nm = &nat44_ei_main;
2044 nat44_ei_address_t *a = 0;
2047 for (i = 0; i < vec_len (nm->addresses); i++)
2049 a = nm->addresses + i;
2051 if (a->addr.as_u32 != addr.as_u32)
2056 #define _(N, j, n, s) \
2057 case NAT_PROTOCOL_##N: \
2058 --a->busy_##n##_port_refcounts[port]; \
2061 a->busy_##n##_ports--; \
2062 a->busy_##n##_ports_per_thread[ti]--; \
2065 foreach_nat_protocol
2067 default : nat_elog_info (nm, "unknown protocol");
2079 nat44_ei_add_resolve_record (ip4_address_t l_addr, u16 l_port, u16 e_port,
2080 nat_protocol_t proto, u32 vrf_id, u32 sw_if_index,
2081 u32 flags, ip4_address_t pool_addr, u8 *tag)
2083 nat44_ei_static_map_resolve_t *rp;
2084 nat44_ei_main_t *nm = &nat44_ei_main;
2086 vec_add2 (nm->to_resolve, rp, 1);
2087 rp->l_addr.as_u32 = l_addr.as_u32;
2088 rp->l_port = l_port;
2089 rp->e_port = e_port;
2090 rp->sw_if_index = sw_if_index;
2091 rp->vrf_id = vrf_id;
2094 rp->pool_addr = pool_addr;
2095 rp->tag = vec_dup (tag);
2099 nat44_ei_get_resolve_record (ip4_address_t l_addr, u16 l_port, u16 e_port,
2100 nat_protocol_t proto, u32 vrf_id, u32 sw_if_index,
2101 u32 flags, int *out)
2103 nat44_ei_static_map_resolve_t *rp;
2104 nat44_ei_main_t *nm = &nat44_ei_main;
2107 for (i = 0; i < vec_len (nm->to_resolve); i++)
2109 rp = nm->to_resolve + i;
2111 if (rp->sw_if_index == sw_if_index && rp->vrf_id == vrf_id)
2113 if (is_sm_identity_nat (rp->flags) && is_sm_identity_nat (flags))
2115 if (!(is_sm_addr_only (rp->flags) && is_sm_addr_only (flags)))
2117 if (rp->e_port != e_port || rp->proto != proto)
2123 else if (rp->l_addr.as_u32 == l_addr.as_u32)
2125 if (!(is_sm_addr_only (rp->flags) && is_sm_addr_only (flags)))
2127 if (rp->l_port != l_port || rp->e_port != e_port ||
2149 nat44_ei_del_resolve_record (ip4_address_t l_addr, u16 l_port, u16 e_port,
2150 nat_protocol_t proto, u32 vrf_id, u32 sw_if_index,
2153 nat44_ei_main_t *nm = &nat44_ei_main;
2155 if (!nat44_ei_get_resolve_record (l_addr, l_port, e_port, proto, vrf_id,
2156 sw_if_index, flags, &i))
2158 vec_del1 (nm->to_resolve, i);
2165 delete_matching_dynamic_sessions (const nat44_ei_static_mapping_t *m,
2168 nat44_ei_main_t *nm = &nat44_ei_main;
2169 clib_bihash_kv_8_8_t kv, value;
2170 nat44_ei_session_t *s;
2171 nat44_ei_user_key_t u_key;
2173 nat44_ei_main_per_thread_data_t *tnm;
2174 dlist_elt_t *head, *elt;
2175 u32 elt_index, head_index;
2179 if (nm->static_mapping_only)
2182 tnm = vec_elt_at_index (nm->per_thread_data, worker_index);
2184 u_key.addr = m->local_addr;
2185 u_key.fib_index = m->fib_index;
2186 kv.key = u_key.as_u64;
2187 if (!clib_bihash_search_8_8 (&tnm->user_hash, &kv, &value))
2189 user_index = value.value;
2190 u = pool_elt_at_index (tnm->users, user_index);
2193 head_index = u->sessions_per_user_list_head_index;
2194 head = pool_elt_at_index (tnm->list_pool, head_index);
2195 elt_index = head->next;
2196 elt = pool_elt_at_index (tnm->list_pool, elt_index);
2197 ses_index = elt->value;
2198 while (ses_index != ~0)
2200 s = pool_elt_at_index (tnm->sessions, ses_index);
2201 elt = pool_elt_at_index (tnm->list_pool, elt->next);
2202 ses_index = elt->value;
2204 if (nat44_ei_is_session_static (s))
2207 if (!is_sm_addr_only (m->flags) &&
2208 s->in2out.port != m->local_port)
2211 nat44_ei_free_session_data_v2 (nm, s, tnm - nm->per_thread_data,
2213 nat44_ei_delete_session (nm, s, tnm - nm->per_thread_data);
2215 if (!is_sm_addr_only (m->flags))
2223 nat44_ei_add_static_mapping (ip4_address_t l_addr, ip4_address_t e_addr,
2224 u16 l_port, u16 e_port, nat_protocol_t proto,
2225 u32 vrf_id, u32 sw_if_index, u32 flags,
2226 ip4_address_t pool_addr, u8 *tag)
2228 nat44_ei_main_t *nm = &nat44_ei_main;
2229 clib_bihash_kv_8_8_t kv, value;
2230 nat44_ei_lb_addr_port_t *local;
2231 nat44_ei_static_mapping_t *m;
2235 fail_if_disabled ();
2237 if (is_sm_addr_only (flags))
2239 e_port = l_port = proto = 0;
2242 if (sw_if_index != ~0)
2244 // this mapping is interface bound
2245 ip4_address_t *first_int_addr;
2247 // check if this record isn't registered for resolve
2248 if (!nat44_ei_get_resolve_record (l_addr, l_port, e_port, proto, vrf_id,
2249 sw_if_index, flags, 0))
2251 return VNET_API_ERROR_VALUE_EXIST;
2253 // register record for resolve
2254 nat44_ei_add_resolve_record (l_addr, l_port, e_port, proto, vrf_id,
2255 sw_if_index, flags, pool_addr, tag);
2258 ip4_interface_first_address (nm->ip4_main, sw_if_index, 0);
2259 if (!first_int_addr)
2261 // dhcp resolution required
2265 e_addr.as_u32 = first_int_addr->as_u32;
2268 if (is_sm_identity_nat (flags))
2271 l_addr.as_u32 = e_addr.as_u32;
2275 init_nat_k (&kv, e_addr, e_port, 0, proto);
2277 if (!clib_bihash_search_8_8 (&nm->static_mapping_by_external, &kv, &value))
2279 m = pool_elt_at_index (nm->static_mappings, value.value);
2280 if (!is_sm_identity_nat (m->flags))
2282 return VNET_API_ERROR_VALUE_EXIST;
2286 // adding local identity nat record for different vrf table
2287 pool_foreach (local, m->locals)
2289 if (local->vrf_id == vrf_id)
2291 return VNET_API_ERROR_VALUE_EXIST;
2295 pool_get (m->locals, local);
2297 local->vrf_id = vrf_id;
2298 local->fib_index = fib_table_find_or_create_and_lock (
2299 FIB_PROTOCOL_IP4, vrf_id, nm->fib_src_low);
2301 init_nat_kv (&kv, m->local_addr, m->local_port, local->fib_index,
2302 m->proto, 0, m - nm->static_mappings);
2303 clib_bihash_add_del_8_8 (&nm->static_mapping_by_local, &kv, 1);
2310 fib_index = fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4, vrf_id,
2315 // fallback to default vrf
2316 vrf_id = nm->inside_vrf_id;
2317 fib_index = nm->inside_fib_index;
2318 fib_table_lock (fib_index, FIB_PROTOCOL_IP4, nm->fib_src_low);
2321 if (!is_sm_identity_nat (flags))
2323 init_nat_k (&kv, l_addr, l_port, fib_index, proto);
2324 if (!clib_bihash_search_8_8 (&nm->static_mapping_by_local, &kv, &value))
2326 return VNET_API_ERROR_VALUE_EXIST;
2330 if (!(is_sm_addr_only (flags) || nm->static_mapping_only))
2332 if (nat44_ei_reserve_port (e_addr, e_port, proto))
2334 // remove resolve record
2335 if ((sw_if_index != ~0) && !is_sm_identity_nat (flags))
2337 nat44_ei_del_resolve_record (l_addr, l_port, e_port, proto,
2338 vrf_id, sw_if_index, flags);
2340 return VNET_API_ERROR_NO_SUCH_ENTRY;
2344 pool_get (nm->static_mappings, m);
2345 clib_memset (m, 0, sizeof (*m));
2348 m->local_addr = l_addr;
2349 m->external_addr = e_addr;
2351 m->tag = vec_dup (tag);
2353 if (!is_sm_addr_only (flags))
2355 m->local_port = l_port;
2356 m->external_port = e_port;
2360 if (is_sm_identity_nat (flags))
2362 pool_get (m->locals, local);
2364 local->vrf_id = vrf_id;
2365 local->fib_index = fib_index;
2370 m->fib_index = fib_index;
2373 init_nat_kv (&kv, m->local_addr, m->local_port, fib_index, m->proto, 0,
2374 m - nm->static_mappings);
2375 clib_bihash_add_del_8_8 (&nm->static_mapping_by_local, &kv, 1);
2377 init_nat_kv (&kv, m->external_addr, m->external_port, 0, m->proto, 0,
2378 m - nm->static_mappings);
2379 clib_bihash_add_del_8_8 (&nm->static_mapping_by_external, &kv, 1);
2381 if (nm->num_workers > 1)
2383 // store worker index for this record
2385 .src_address = m->local_addr,
2387 worker_index = nat44_ei_get_in2out_worker_index (&ip, m->fib_index, 0);
2388 vec_add1 (m->workers, worker_index);
2392 worker_index = nm->num_workers;
2394 delete_matching_dynamic_sessions (m, worker_index);
2396 if (is_sm_addr_only (flags))
2398 nat44_ei_add_del_addr_to_fib_foreach_out_if (&e_addr, 1);
2405 nat44_ei_del_static_mapping (ip4_address_t l_addr, ip4_address_t e_addr,
2406 u16 l_port, u16 e_port, nat_protocol_t proto,
2407 u32 vrf_id, u32 sw_if_index, u32 flags)
2409 nat44_ei_main_per_thread_data_t *tnm;
2410 nat44_ei_main_t *nm = &nat44_ei_main;
2411 clib_bihash_kv_8_8_t kv, value;
2412 nat44_ei_lb_addr_port_t *local;
2413 nat44_ei_static_mapping_t *m;
2415 nat44_ei_user_key_t u_key;
2417 fail_if_disabled ();
2419 if (is_sm_addr_only (flags))
2421 e_port = l_port = proto = 0;
2424 if (sw_if_index != ~0)
2426 // this mapping is interface bound
2427 ip4_address_t *first_int_addr;
2429 // delete record registered for resolve
2430 if (nat44_ei_del_resolve_record (l_addr, l_port, e_port, proto, vrf_id,
2431 sw_if_index, flags))
2433 return VNET_API_ERROR_NO_SUCH_ENTRY;
2437 ip4_interface_first_address (nm->ip4_main, sw_if_index, 0);
2438 if (!first_int_addr)
2440 // dhcp resolution required
2444 e_addr.as_u32 = first_int_addr->as_u32;
2447 if (is_sm_identity_nat (flags))
2450 l_addr.as_u32 = e_addr.as_u32;
2454 init_nat_k (&kv, e_addr, e_port, 0, proto);
2456 if (clib_bihash_search_8_8 (&nm->static_mapping_by_external, &kv, &value))
2458 if (sw_if_index != ~0)
2462 return VNET_API_ERROR_NO_SUCH_ENTRY;
2465 m = pool_elt_at_index (nm->static_mappings, value.value);
2467 if (is_sm_identity_nat (flags))
2473 vrf_id = nm->inside_vrf_id;
2476 pool_foreach (local, m->locals)
2478 if (local->vrf_id == vrf_id)
2480 local = pool_elt_at_index (m->locals, local - m->locals);
2481 fib_index = local->fib_index;
2482 pool_put (m->locals, local);
2488 return VNET_API_ERROR_NO_SUCH_ENTRY;
2493 fib_index = m->fib_index;
2496 if (!(is_sm_addr_only (flags) || nm->static_mapping_only))
2498 if (nat44_ei_free_port (e_addr, e_port, proto))
2500 return VNET_API_ERROR_INVALID_VALUE;
2504 init_nat_k (&kv, l_addr, l_port, fib_index, proto);
2505 clib_bihash_add_del_8_8 (&nm->static_mapping_by_local, &kv, 0);
2507 if (!nm->static_mapping_only || nm->static_mapping_connection_tracking)
2509 // delete sessions for static mapping
2510 if (nm->num_workers > 1)
2511 tnm = vec_elt_at_index (nm->per_thread_data, m->workers[0]);
2513 tnm = vec_elt_at_index (nm->per_thread_data, nm->num_workers);
2515 u_key.addr = m->local_addr;
2516 u_key.fib_index = fib_index;
2517 kv.key = u_key.as_u64;
2518 nat44_ei_static_mapping_del_sessions (
2519 nm, tnm, u_key, is_sm_addr_only (flags), e_addr, e_port);
2522 fib_table_unlock (fib_index, FIB_PROTOCOL_IP4, nm->fib_src_low);
2524 if (!pool_elts (m->locals))
2526 // this is last record remove all required stuff
2528 init_nat_k (&kv, e_addr, e_port, 0, proto);
2529 clib_bihash_add_del_8_8 (&nm->static_mapping_by_external, &kv, 0);
2532 vec_free (m->workers);
2533 pool_put (nm->static_mappings, m);
2535 if (is_sm_addr_only (flags) && !is_sm_identity_nat (flags))
2537 nat44_ei_add_del_addr_to_fib_foreach_out_if (&e_addr, 0);
2545 nat44_ei_static_mapping_match (ip4_address_t match_addr, u16 match_port,
2546 u32 match_fib_index,
2547 nat_protocol_t match_protocol,
2548 ip4_address_t *mapping_addr, u16 *mapping_port,
2549 u32 *mapping_fib_index, u8 by_external,
2550 u8 *is_addr_only, u8 *is_identity_nat)
2552 nat44_ei_main_t *nm = &nat44_ei_main;
2553 clib_bihash_kv_8_8_t kv, value;
2554 nat44_ei_static_mapping_t *m;
2559 init_nat_k (&kv, match_addr, match_port, 0, match_protocol);
2560 if (clib_bihash_search_8_8 (&nm->static_mapping_by_external, &kv,
2563 /* Try address only mapping */
2564 init_nat_k (&kv, match_addr, 0, 0, 0);
2565 if (clib_bihash_search_8_8 (&nm->static_mapping_by_external, &kv,
2569 m = pool_elt_at_index (nm->static_mappings, value.value);
2571 *mapping_fib_index = m->fib_index;
2572 *mapping_addr = m->local_addr;
2573 port = m->local_port;
2577 init_nat_k (&kv, match_addr, match_port, match_fib_index,
2579 if (clib_bihash_search_8_8 (&nm->static_mapping_by_local, &kv, &value))
2581 /* Try address only mapping */
2582 init_nat_k (&kv, match_addr, 0, match_fib_index, 0);
2583 if (clib_bihash_search_8_8 (&nm->static_mapping_by_local, &kv,
2587 m = pool_elt_at_index (nm->static_mappings, value.value);
2589 *mapping_fib_index = nm->outside_fib_index;
2590 *mapping_addr = m->external_addr;
2591 port = m->external_port;
2594 /* Address only mapping doesn't change port */
2595 if (is_sm_addr_only (m->flags))
2596 *mapping_port = match_port;
2598 *mapping_port = port;
2600 if (PREDICT_FALSE (is_addr_only != 0))
2601 *is_addr_only = is_sm_addr_only (m->flags);
2603 if (PREDICT_FALSE (is_identity_nat != 0))
2604 *is_identity_nat = is_sm_identity_nat (m->flags);
2610 nat44_ei_worker_db_free (nat44_ei_main_per_thread_data_t *tnm)
2612 pool_free (tnm->list_pool);
2613 pool_free (tnm->lru_pool);
2614 pool_free (tnm->sessions);
2615 pool_free (tnm->users);
2617 clib_bihash_free_8_8 (&tnm->user_hash);
2621 format_nat44_ei_key (u8 *s, va_list *args)
2623 u64 key = va_arg (*args, u64);
2627 nat_protocol_t protocol;
2630 split_nat_key (key, &addr, &port, &fib_index, &protocol);
2632 s = format (s, "%U proto %U port %d fib %d", format_ip4_address, &addr,
2633 format_nat_protocol, protocol, clib_net_to_host_u16 (port),
2639 format_nat44_ei_user_kvp (u8 *s, va_list *args)
2641 clib_bihash_kv_8_8_t *v = va_arg (*args, clib_bihash_kv_8_8_t *);
2642 nat44_ei_user_key_t k;
2646 s = format (s, "%U fib %d user-index %llu", format_ip4_address, &k.addr,
2647 k.fib_index, v->value);
2653 format_nat44_ei_session_kvp (u8 *s, va_list *args)
2655 clib_bihash_kv_8_8_t *v = va_arg (*args, clib_bihash_kv_8_8_t *);
2657 s = format (s, "%U thread-index %llu session-index %llu",
2658 format_nat44_ei_key, v->key, nat_value_get_thread_index (v),
2659 nat_value_get_session_index (v));
2665 format_nat44_ei_static_mapping_kvp (u8 *s, va_list *args)
2667 clib_bihash_kv_8_8_t *v = va_arg (*args, clib_bihash_kv_8_8_t *);
2669 s = format (s, "%U static-mapping-index %llu", format_nat44_ei_key, v->key,
2676 nat44_ei_worker_db_init (nat44_ei_main_per_thread_data_t *tnm,
2677 u32 translations, u32 translation_buckets,
2682 pool_alloc (tnm->list_pool, translations);
2683 pool_alloc (tnm->lru_pool, translations);
2684 pool_alloc (tnm->sessions, translations);
2686 clib_bihash_init_8_8 (&tnm->user_hash, "users", user_buckets, 0);
2688 clib_bihash_set_kvp_format_fn_8_8 (&tnm->user_hash,
2689 format_nat44_ei_user_kvp);
2691 pool_get (tnm->lru_pool, head);
2692 tnm->tcp_trans_lru_head_index = head - tnm->lru_pool;
2693 clib_dlist_init (tnm->lru_pool, tnm->tcp_trans_lru_head_index);
2695 pool_get (tnm->lru_pool, head);
2696 tnm->tcp_estab_lru_head_index = head - tnm->lru_pool;
2697 clib_dlist_init (tnm->lru_pool, tnm->tcp_estab_lru_head_index);
2699 pool_get (tnm->lru_pool, head);
2700 tnm->udp_lru_head_index = head - tnm->lru_pool;
2701 clib_dlist_init (tnm->lru_pool, tnm->udp_lru_head_index);
2703 pool_get (tnm->lru_pool, head);
2704 tnm->icmp_lru_head_index = head - tnm->lru_pool;
2705 clib_dlist_init (tnm->lru_pool, tnm->icmp_lru_head_index);
2707 pool_get (tnm->lru_pool, head);
2708 tnm->unk_proto_lru_head_index = head - tnm->lru_pool;
2709 clib_dlist_init (tnm->lru_pool, tnm->unk_proto_lru_head_index);
2715 nat44_ei_main_t *nm = &nat44_ei_main;
2716 nat44_ei_main_per_thread_data_t *tnm;
2718 pool_free (nm->static_mappings);
2719 clib_bihash_free_8_8 (&nm->static_mapping_by_local);
2720 clib_bihash_free_8_8 (&nm->static_mapping_by_external);
2724 clib_bihash_free_8_8 (&nm->in2out);
2725 clib_bihash_free_8_8 (&nm->out2in);
2726 vec_foreach (tnm, nm->per_thread_data)
2728 nat44_ei_worker_db_free (tnm);
2734 nat44_ei_db_init (u32 translations, u32 translation_buckets, u32 user_buckets)
2736 nat44_ei_main_t *nm = &nat44_ei_main;
2737 nat44_ei_main_per_thread_data_t *tnm;
2739 u32 static_mapping_buckets = 1024;
2740 u32 static_mapping_memory_size = 64 << 20;
2742 clib_bihash_init_8_8 (&nm->static_mapping_by_local,
2743 "static_mapping_by_local", static_mapping_buckets,
2744 static_mapping_memory_size);
2745 clib_bihash_init_8_8 (&nm->static_mapping_by_external,
2746 "static_mapping_by_external", static_mapping_buckets,
2747 static_mapping_memory_size);
2748 clib_bihash_set_kvp_format_fn_8_8 (&nm->static_mapping_by_local,
2749 format_nat44_ei_static_mapping_kvp);
2750 clib_bihash_set_kvp_format_fn_8_8 (&nm->static_mapping_by_external,
2751 format_nat44_ei_static_mapping_kvp);
2755 clib_bihash_init_8_8 (&nm->in2out, "in2out", translation_buckets, 0);
2756 clib_bihash_init_8_8 (&nm->out2in, "out2in", translation_buckets, 0);
2757 clib_bihash_set_kvp_format_fn_8_8 (&nm->in2out,
2758 format_nat44_ei_session_kvp);
2759 clib_bihash_set_kvp_format_fn_8_8 (&nm->out2in,
2760 format_nat44_ei_session_kvp);
2761 vec_foreach (tnm, nm->per_thread_data)
2763 nat44_ei_worker_db_init (tnm, translations, translation_buckets,
2770 nat44_ei_sessions_clear ()
2772 nat44_ei_main_t *nm = &nat44_ei_main;
2773 nat44_ei_main_per_thread_data_t *tnm;
2777 clib_bihash_free_8_8 (&nm->in2out);
2778 clib_bihash_free_8_8 (&nm->out2in);
2779 clib_bihash_init_8_8 (&nm->in2out, "in2out", nm->translation_buckets, 0);
2780 clib_bihash_init_8_8 (&nm->out2in, "out2in", nm->translation_buckets, 0);
2781 clib_bihash_set_kvp_format_fn_8_8 (&nm->in2out,
2782 format_nat44_ei_session_kvp);
2783 clib_bihash_set_kvp_format_fn_8_8 (&nm->out2in,
2784 format_nat44_ei_session_kvp);
2785 vec_foreach (tnm, nm->per_thread_data)
2787 nat44_ei_worker_db_free (tnm);
2788 nat44_ei_worker_db_init (tnm, nm->translations,
2789 nm->translation_buckets, nm->user_buckets);
2793 vlib_zero_simple_counter (&nm->total_users, 0);
2794 vlib_zero_simple_counter (&nm->total_sessions, 0);
2795 vlib_zero_simple_counter (&nm->user_limit_reached, 0);
2799 nat44_ei_update_outside_fib (ip4_main_t *im, uword opaque, u32 sw_if_index,
2800 u32 new_fib_index, u32 old_fib_index)
2802 nat44_ei_main_t *nm = &nat44_ei_main;
2803 nat44_ei_outside_fib_t *outside_fib;
2804 nat44_ei_interface_t *i;
2808 if (!nm->enabled || (new_fib_index == old_fib_index) ||
2809 (!vec_len (nm->outside_fibs)))
2814 pool_foreach (i, nm->interfaces)
2816 if (i->sw_if_index == sw_if_index)
2818 if (!(nat44_ei_interface_is_outside (i)))
2824 pool_foreach (i, nm->output_feature_interfaces)
2826 if (i->sw_if_index == sw_if_index)
2828 if (!(nat44_ei_interface_is_outside (i)))
2837 vec_foreach (outside_fib, nm->outside_fibs)
2839 if (outside_fib->fib_index == old_fib_index)
2841 outside_fib->refcount--;
2842 if (!outside_fib->refcount)
2843 vec_del1 (nm->outside_fibs, outside_fib - nm->outside_fibs);
2848 vec_foreach (outside_fib, nm->outside_fibs)
2850 if (outside_fib->fib_index == new_fib_index)
2852 outside_fib->refcount++;
2860 vec_add2 (nm->outside_fibs, outside_fib, 1);
2861 outside_fib->refcount = 1;
2862 outside_fib->fib_index = new_fib_index;
2867 nat44_ei_add_address (ip4_address_t *addr, u32 vrf_id)
2869 nat44_ei_main_t *nm = &nat44_ei_main;
2870 vlib_thread_main_t *tm = vlib_get_thread_main ();
2871 nat44_ei_address_t *ap;
2873 fail_if_disabled ();
2875 /* Check if address already exists */
2876 vec_foreach (ap, nm->addresses)
2878 if (ap->addr.as_u32 == addr->as_u32)
2880 nat44_ei_log_err ("address exist");
2881 return VNET_API_ERROR_VALUE_EXIST;
2885 vec_add2 (nm->addresses, ap, 1);
2892 ap->fib_index = fib_table_find_or_create_and_lock (
2893 FIB_PROTOCOL_IP4, vrf_id, nm->fib_src_low);
2896 #define _(N, i, n, s) \
2897 clib_memset (ap->busy_##n##_port_refcounts, 0, \
2898 sizeof (ap->busy_##n##_port_refcounts)); \
2899 ap->busy_##n##_ports = 0; \
2900 ap->busy_##n##_ports_per_thread = 0; \
2901 vec_validate_init_empty (ap->busy_##n##_ports_per_thread, \
2902 tm->n_vlib_mains - 1, 0);
2903 foreach_nat_protocol
2906 nat44_ei_add_del_addr_to_fib_foreach_out_if (addr, 1);
2912 nat44_ei_del_address (ip4_address_t addr, u8 delete_sm)
2914 nat44_ei_main_t *nm = &nat44_ei_main;
2915 nat44_ei_address_t *a = 0;
2916 nat44_ei_session_t *ses;
2917 u32 *ses_to_be_removed = 0, *ses_index;
2918 nat44_ei_main_per_thread_data_t *tnm;
2919 nat44_ei_static_mapping_t *m;
2922 fail_if_disabled ();
2924 /* Find SNAT address */
2925 for (j = 0; j < vec_len (nm->addresses); j++)
2927 if (nm->addresses[j].addr.as_u32 == addr.as_u32)
2929 a = nm->addresses + j;
2935 nat44_ei_log_err ("no such address");
2936 return VNET_API_ERROR_NO_SUCH_ENTRY;
2941 pool_foreach (m, nm->static_mappings)
2943 if (m->external_addr.as_u32 == addr.as_u32)
2944 nat44_ei_del_static_mapping (m->local_addr, m->external_addr,
2945 m->local_port, m->external_port,
2946 m->proto, m->vrf_id, ~0, m->flags);
2951 /* Check if address is used in some static mapping */
2952 if (nat44_ei_is_address_used_in_static_mapping (addr))
2954 nat44_ei_log_err ("address used in static mapping");
2955 return VNET_API_ERROR_UNSPECIFIED;
2959 if (a->fib_index != ~0)
2961 fib_table_unlock (a->fib_index, FIB_PROTOCOL_IP4, nm->fib_src_low);
2964 /* Delete sessions using address */
2965 if (a->busy_tcp_ports || a->busy_udp_ports || a->busy_icmp_ports)
2967 vec_foreach (tnm, nm->per_thread_data)
2969 pool_foreach (ses, tnm->sessions)
2971 if (ses->out2in.addr.as_u32 == addr.as_u32)
2973 nat44_ei_free_session_data (nm, ses,
2974 tnm - nm->per_thread_data, 0);
2975 vec_add1 (ses_to_be_removed, ses - tnm->sessions);
2978 vec_foreach (ses_index, ses_to_be_removed)
2980 ses = pool_elt_at_index (tnm->sessions, ses_index[0]);
2981 nat44_ei_delete_session (nm, ses, tnm - nm->per_thread_data);
2983 vec_free (ses_to_be_removed);
2987 #define _(N, i, n, s) vec_free (a->busy_##n##_ports_per_thread);
2988 foreach_nat_protocol
2991 vec_del1 (nm->addresses, j);
2993 nat44_ei_add_del_addr_to_fib_foreach_out_if (&addr, 0);
2999 nat44_ei_add_interface_address (u32 sw_if_index)
3001 nat44_ei_main_t *nm = &nat44_ei_main;
3002 ip4_main_t *ip4_main = nm->ip4_main;
3003 ip4_address_t *first_int_addr;
3004 u32 *auto_add_sw_if_indices = nm->auto_add_sw_if_indices;
3007 for (i = 0; i < vec_len (auto_add_sw_if_indices); i++)
3009 if (auto_add_sw_if_indices[i] == sw_if_index)
3011 return VNET_API_ERROR_VALUE_EXIST;
3015 /* add to the auto-address list */
3016 vec_add1 (nm->auto_add_sw_if_indices, sw_if_index);
3018 // if the address is already bound - or static - add it now
3019 first_int_addr = ip4_interface_first_address (ip4_main, sw_if_index, 0);
3022 (void) nat44_ei_add_address (first_int_addr, ~0);
3029 nat44_ei_del_interface_address (u32 sw_if_index)
3031 nat44_ei_main_t *nm = &nat44_ei_main;
3032 ip4_main_t *ip4_main = nm->ip4_main;
3033 ip4_address_t *first_int_addr;
3034 nat44_ei_static_map_resolve_t *rp;
3035 u32 *indices_to_delete = 0;
3037 u32 *auto_add_sw_if_indices = nm->auto_add_sw_if_indices;
3039 fail_if_disabled ();
3041 first_int_addr = ip4_interface_first_address (ip4_main, sw_if_index, 0);
3043 for (i = 0; i < vec_len (auto_add_sw_if_indices); i++)
3045 if (auto_add_sw_if_indices[i] == sw_if_index)
3048 ip4_interface_first_address (ip4_main, sw_if_index, 0);
3051 (void) nat44_ei_del_address (first_int_addr[0], 1);
3055 for (j = 0; j < vec_len (nm->to_resolve); j++)
3057 rp = nm->to_resolve + j;
3058 if (rp->sw_if_index == sw_if_index)
3060 vec_add1 (indices_to_delete, j);
3063 if (vec_len (indices_to_delete))
3065 for (j = vec_len (indices_to_delete) - 1; j >= 0; j--)
3067 vec_del1 (nm->to_resolve, j);
3069 vec_free (indices_to_delete);
3073 vec_del1 (nm->auto_add_sw_if_indices, i);
3077 return VNET_API_ERROR_NO_SUCH_ENTRY;
3080 static_always_inline int
3081 is_sw_if_index_reg_for_auto_resolve (u32 *sw_if_indices, u32 sw_if_index)
3084 vec_foreach (i, sw_if_indices)
3086 if (*i == sw_if_index)
3095 nat44_ei_ip4_add_del_interface_address_cb (ip4_main_t *im, uword opaque,
3097 ip4_address_t *address,
3099 u32 if_address_index, u32 is_delete)
3101 nat44_ei_main_t *nm = &nat44_ei_main;
3102 nat44_ei_static_map_resolve_t *rp;
3103 nat44_ei_address_t *addresses = nm->addresses;
3111 if (!is_sw_if_index_reg_for_auto_resolve (nm->auto_add_sw_if_indices,
3119 /* Don't trip over lease renewal, static config */
3120 for (i = 0; i < vec_len (addresses); i++)
3122 if (addresses[i].addr.as_u32 == address->as_u32)
3128 (void) nat44_ei_add_address (address, ~0);
3130 /* Scan static map resolution vector */
3131 for (i = 0; i < vec_len (nm->to_resolve); i++)
3133 rp = nm->to_resolve + i;
3134 if (is_sm_addr_only (rp->flags))
3138 /* On this interface? */
3139 if (rp->sw_if_index == sw_if_index)
3141 rv = nat44_ei_add_static_mapping (
3142 rp->l_addr, address[0], rp->l_port, rp->e_port, rp->proto,
3143 rp->vrf_id, ~0, rp->flags, rp->pool_addr, rp->tag);
3146 nat_elog_notice_X1 (nm, "add_static_mapping returned %d",
3154 // remove all static mapping records
3155 (void) nat44_ei_del_address (address[0], 1);
3160 nat44_ei_set_frame_queue_nelts (u32 frame_queue_nelts)
3163 nat44_ei_main_t *nm = &nat44_ei_main;
3164 nm->frame_queue_nelts = frame_queue_nelts;
3169 nat44_ei_ip4_add_del_addr_only_sm_cb (ip4_main_t *im, uword opaque,
3170 u32 sw_if_index, ip4_address_t *address,
3171 u32 address_length, u32 if_address_index,
3174 nat44_ei_main_t *nm = &nat44_ei_main;
3175 nat44_ei_static_map_resolve_t *rp;
3176 nat44_ei_static_mapping_t *m;
3177 clib_bihash_kv_8_8_t kv, value;
3178 int i, rv = 0, match = 0;
3185 for (i = 0; i < vec_len (nm->to_resolve); i++)
3187 rp = nm->to_resolve + i;
3189 if (is_sm_addr_only (rp->flags) && rp->sw_if_index == sw_if_index)
3201 init_nat_k (&kv, *address, is_sm_addr_only (rp->flags) ? 0 : rp->e_port,
3202 nm->outside_fib_index,
3203 is_sm_addr_only (rp->flags) ? 0 : rp->proto);
3204 if (clib_bihash_search_8_8 (&nm->static_mapping_by_external, &kv, &value))
3207 m = pool_elt_at_index (nm->static_mappings, value.value);
3213 rv = nat44_ei_del_static_mapping (rp->l_addr, address[0], rp->l_port,
3214 rp->e_port, rp->proto, rp->vrf_id, ~0,
3218 nat_elog_notice_X1 (nm, "nat44_ei_del_static_mapping returned %d",
3226 rv = nat44_ei_add_static_mapping (rp->l_addr, address[0], rp->l_port,
3227 rp->e_port, rp->proto, rp->vrf_id, ~0,
3228 rp->flags, rp->pool_addr, rp->tag);
3231 nat_elog_notice_X1 (nm, "nat44_ei_add_static_mapping returned %d",
3237 static_always_inline uword
3238 nat44_ei_classify_inline_fn (vlib_main_t *vm, vlib_node_runtime_t *node,
3239 vlib_frame_t *frame)
3241 u32 n_left_from, *from, *to_next;
3242 nat44_ei_classify_next_t next_index;
3243 nat44_ei_main_t *nm = &nat44_ei_main;
3244 nat44_ei_static_mapping_t *m;
3245 u32 next_in2out = 0, next_out2in = 0;
3247 from = vlib_frame_vector_args (frame);
3248 n_left_from = frame->n_vectors;
3249 next_index = node->cached_next_index;
3251 while (n_left_from > 0)
3255 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
3257 while (n_left_from > 0 && n_left_to_next > 0)
3261 u32 next0 = NAT44_EI_CLASSIFY_NEXT_IN2OUT;
3263 nat44_ei_address_t *ap;
3264 clib_bihash_kv_8_8_t kv0, value0;
3266 /* speculatively enqueue b0 to the current next frame */
3272 n_left_to_next -= 1;
3274 b0 = vlib_get_buffer (vm, bi0);
3275 ip0 = vlib_buffer_get_current (b0);
3277 vec_foreach (ap, nm->addresses)
3279 if (ip0->dst_address.as_u32 == ap->addr.as_u32)
3281 next0 = NAT44_EI_CLASSIFY_NEXT_OUT2IN;
3286 if (PREDICT_FALSE (pool_elts (nm->static_mappings)))
3288 init_nat_k (&kv0, ip0->dst_address, 0, 0, 0);
3289 /* try to classify the fragment based on IP header alone */
3290 if (!clib_bihash_search_8_8 (&nm->static_mapping_by_external,
3293 m = pool_elt_at_index (nm->static_mappings, value0.value);
3294 if (m->local_addr.as_u32 != m->external_addr.as_u32)
3295 next0 = NAT44_EI_CLASSIFY_NEXT_OUT2IN;
3298 init_nat_k (&kv0, ip0->dst_address,
3299 vnet_buffer (b0)->ip.reass.l4_dst_port, 0,
3300 ip_proto_to_nat_proto (ip0->protocol));
3301 if (!clib_bihash_search_8_8 (&nm->static_mapping_by_external,
3304 m = pool_elt_at_index (nm->static_mappings, value0.value);
3305 if (m->local_addr.as_u32 != m->external_addr.as_u32)
3306 next0 = NAT44_EI_CLASSIFY_NEXT_OUT2IN;
3311 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE) &&
3312 (b0->flags & VLIB_BUFFER_IS_TRACED)))
3314 nat44_ei_classify_trace_t *t =
3315 vlib_add_trace (vm, node, b0, sizeof (*t));
3317 t->next_in2out = next0 == NAT44_EI_CLASSIFY_NEXT_IN2OUT ? 1 : 0;
3320 next_in2out += next0 == NAT44_EI_CLASSIFY_NEXT_IN2OUT;
3321 next_out2in += next0 == NAT44_EI_CLASSIFY_NEXT_OUT2IN;
3323 /* verify speculative enqueue, maybe switch current next frame */
3324 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
3325 n_left_to_next, bi0, next0);
3328 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
3331 vlib_node_increment_counter (
3332 vm, node->node_index, NAT44_EI_CLASSIFY_ERROR_NEXT_IN2OUT, next_in2out);
3333 vlib_node_increment_counter (
3334 vm, node->node_index, NAT44_EI_CLASSIFY_ERROR_NEXT_OUT2IN, next_out2in);
3335 return frame->n_vectors;
3338 VLIB_NODE_FN (nat44_ei_classify_node)
3339 (vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
3341 return nat44_ei_classify_inline_fn (vm, node, frame);
3344 VLIB_REGISTER_NODE (nat44_ei_classify_node) = {
3345 .name = "nat44-ei-classify",
3346 .vector_size = sizeof (u32),
3347 .format_trace = format_nat44_ei_classify_trace,
3348 .type = VLIB_NODE_TYPE_INTERNAL,
3349 .n_errors = ARRAY_LEN(nat44_ei_classify_error_strings),
3350 .error_strings = nat44_ei_classify_error_strings,
3351 .n_next_nodes = NAT44_EI_CLASSIFY_N_NEXT,
3353 [NAT44_EI_CLASSIFY_NEXT_IN2OUT] = "nat44-ei-in2out",
3354 [NAT44_EI_CLASSIFY_NEXT_OUT2IN] = "nat44-ei-out2in",
3355 [NAT44_EI_CLASSIFY_NEXT_DROP] = "error-drop",
3359 VLIB_NODE_FN (nat44_ei_handoff_classify_node)
3360 (vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
3362 return nat44_ei_classify_inline_fn (vm, node, frame);
3365 VLIB_REGISTER_NODE (nat44_ei_handoff_classify_node) = {
3366 .name = "nat44-ei-handoff-classify",
3367 .vector_size = sizeof (u32),
3368 .format_trace = format_nat44_ei_classify_trace,
3369 .type = VLIB_NODE_TYPE_INTERNAL,
3370 .n_errors = ARRAY_LEN(nat44_ei_classify_error_strings),
3371 .error_strings = nat44_ei_classify_error_strings,
3372 .n_next_nodes = NAT44_EI_CLASSIFY_N_NEXT,
3374 [NAT44_EI_CLASSIFY_NEXT_IN2OUT] = "nat44-ei-in2out-worker-handoff",
3375 [NAT44_EI_CLASSIFY_NEXT_OUT2IN] = "nat44-ei-out2in-worker-handoff",
3376 [NAT44_EI_CLASSIFY_NEXT_DROP] = "error-drop",
3381 * fd.io coding-style-patch-verification: ON
3384 * eval: (c-set-style "gnu")