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"); \
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"); \
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 format_nat44_ei_classify_trace (u8 *s, va_list *args)
191 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
192 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
193 nat44_ei_classify_trace_t *t = va_arg (*args, nat44_ei_classify_trace_t *);
197 s = format (s, "nat44-ei-classify: fragment cached");
200 next = t->next_in2out ? "nat44-ei-in2out" : "nat44-ei-out2in";
201 s = format (s, "nat44-ei-classify: next %s", next);
207 static void nat44_ei_db_init (u32 translations, u32 translation_buckets,
210 static void nat44_ei_ip4_add_del_interface_address_cb (
211 ip4_main_t *im, uword opaque, u32 sw_if_index, ip4_address_t *address,
212 u32 address_length, u32 if_address_index, u32 is_delete);
214 static void nat44_ei_ip4_add_del_addr_only_sm_cb (
215 ip4_main_t *im, uword opaque, u32 sw_if_index, ip4_address_t *address,
216 u32 address_length, u32 if_address_index, u32 is_delete);
218 static void nat44_ei_update_outside_fib (ip4_main_t *im, uword opaque,
219 u32 sw_if_index, u32 new_fib_index,
223 nat44_ei_set_node_indexes (nat44_ei_main_t *nm, vlib_main_t *vm)
226 node = vlib_get_node_by_name (vm, (u8 *) "nat44-ei-out2in");
227 nm->out2in_node_index = node->index;
228 node = vlib_get_node_by_name (vm, (u8 *) "nat44-ei-in2out");
229 nm->in2out_node_index = node->index;
230 node = vlib_get_node_by_name (vm, (u8 *) "nat44-ei-in2out-output");
231 nm->in2out_output_node_index = node->index;
235 nat44_ei_set_workers (uword *bitmap)
237 nat44_ei_main_t *nm = &nat44_ei_main;
240 if (nm->num_workers < 2)
241 return VNET_API_ERROR_FEATURE_DISABLED;
243 if (clib_bitmap_last_set (bitmap) >= nm->num_workers)
244 return VNET_API_ERROR_INVALID_WORKER;
246 vec_free (nm->workers);
247 clib_bitmap_foreach (i, bitmap)
249 vec_add1 (nm->workers, i);
250 nm->per_thread_data[nm->first_worker_index + i].snat_thread_index = j;
251 nm->per_thread_data[nm->first_worker_index + i].thread_index = i;
255 nm->port_per_thread = (0xffff - 1024) / _vec_len (nm->workers);
260 #define nat_validate_simple_counter(c, i) \
263 vlib_validate_simple_counter (&c, i); \
264 vlib_zero_simple_counter (&c, i); \
268 #define nat_init_simple_counter(c, n, sn) \
272 c.stat_segment_name = sn; \
273 nat_validate_simple_counter (c, 0); \
277 static_always_inline void
278 nat_validate_interface_counters (nat44_ei_main_t *nm, u32 sw_if_index)
281 nat_validate_simple_counter (nm->counters.fastpath.in2out.x, sw_if_index); \
282 nat_validate_simple_counter (nm->counters.fastpath.out2in.x, sw_if_index); \
283 nat_validate_simple_counter (nm->counters.slowpath.in2out.x, sw_if_index); \
284 nat_validate_simple_counter (nm->counters.slowpath.out2in.x, sw_if_index);
287 nat_validate_simple_counter (nm->counters.hairpinning, sw_if_index);
291 nat44_ei_add_del_addr_to_fib_foreach_out_if (ip4_address_t *addr, u8 is_add)
293 nat44_ei_main_t *nm = &nat44_ei_main;
294 nat44_ei_interface_t *i;
296 pool_foreach (i, nm->interfaces)
298 if (nat44_ei_interface_is_outside (i) && !nm->out2in_dpo)
300 nat44_ei_add_del_addr_to_fib (addr, 32, i->sw_if_index, is_add);
303 pool_foreach (i, nm->output_feature_interfaces)
305 if (nat44_ei_interface_is_outside (i) && !nm->out2in_dpo)
307 nat44_ei_add_del_addr_to_fib (addr, 32, i->sw_if_index, is_add);
312 static_always_inline void
313 nat44_ei_add_del_addr_to_fib_foreach_addr (u32 sw_if_index, u8 is_add)
315 nat44_ei_main_t *nm = &nat44_ei_main;
316 nat44_ei_address_t *ap;
318 vec_foreach (ap, nm->addresses)
320 nat44_ei_add_del_addr_to_fib (&ap->addr, 32, sw_if_index, is_add);
324 static_always_inline void
325 nat44_ei_add_del_addr_to_fib_foreach_addr_only_sm (u32 sw_if_index, u8 is_add)
327 nat44_ei_main_t *nm = &nat44_ei_main;
328 nat44_ei_static_mapping_t *m;
330 pool_foreach (m, nm->static_mappings)
332 if (is_sm_addr_only (m->flags) &&
333 !(m->local_addr.as_u32 == m->external_addr.as_u32))
335 nat44_ei_add_del_addr_to_fib (&m->external_addr, 32, sw_if_index,
342 nat44_ei_is_address_used_in_static_mapping (ip4_address_t addr)
344 nat44_ei_main_t *nm = &nat44_ei_main;
345 nat44_ei_static_mapping_t *m;
346 pool_foreach (m, nm->static_mappings)
348 if (is_sm_addr_only (m->flags) || is_sm_identity_nat (m->flags))
352 if (m->external_addr.as_u32 == addr.as_u32)
361 nat44_ei_init (vlib_main_t *vm)
363 nat44_ei_main_t *nm = &nat44_ei_main;
364 vlib_thread_main_t *tm = vlib_get_thread_main ();
365 vlib_thread_registration_t *tr;
366 ip4_add_del_interface_address_callback_t cbi = { 0 };
367 ip4_table_bind_callback_t cbt = { 0 };
368 u32 i, num_threads = 0;
369 uword *p, *bitmap = 0;
371 clib_memset (nm, 0, sizeof (*nm));
374 nm->vnet_main = vnet_get_main ();
376 nm->ip4_main = &ip4_main;
377 nm->api_main = vlibapi_get_main ();
378 nm->ip4_lookup_main = &ip4_main.lookup_main;
381 nm->fq_out2in_index = ~0;
382 nm->fq_in2out_index = ~0;
383 nm->fq_in2out_output_index = ~0;
385 nm->log_level = NAT_LOG_ERROR;
387 nat44_ei_set_node_indexes (nm, vm);
388 nm->log_class = vlib_log_register_class ("nat44-ei", 0);
390 nat_init_simple_counter (nm->total_users, "total-users",
391 "/nat44-ei/total-users");
392 nat_init_simple_counter (nm->total_sessions, "total-sessions",
393 "/nat44-ei/total-sessions");
394 nat_init_simple_counter (nm->user_limit_reached, "user-limit-reached",
395 "/nat44-ei/user-limit-reached");
398 nat_init_simple_counter (nm->counters.fastpath.in2out.x, #x, \
399 "/nat44-ei/in2out/fastpath/" #x); \
400 nat_init_simple_counter (nm->counters.fastpath.out2in.x, #x, \
401 "/nat44-ei/out2in/fastpath/" #x); \
402 nat_init_simple_counter (nm->counters.slowpath.in2out.x, #x, \
403 "/nat44-ei/in2out/slowpath/" #x); \
404 nat_init_simple_counter (nm->counters.slowpath.out2in.x, #x, \
405 "/nat44-ei/out2in/slowpath/" #x);
408 nat_init_simple_counter (nm->counters.hairpinning, "hairpinning",
409 "/nat44-ei/hairpinning");
411 p = hash_get_mem (tm->thread_registrations_by_name, "workers");
414 tr = (vlib_thread_registration_t *) p[0];
417 nm->num_workers = tr->count;
418 nm->first_worker_index = tr->first_index;
421 num_threads = tm->n_vlib_mains - 1;
422 nm->port_per_thread = 0xffff - 1024;
423 vec_validate (nm->per_thread_data, num_threads);
425 /* Use all available workers by default */
426 if (nm->num_workers > 1)
428 for (i = 0; i < nm->num_workers; i++)
429 bitmap = clib_bitmap_set (bitmap, i, 1);
430 nat44_ei_set_workers (bitmap);
431 clib_bitmap_free (bitmap);
435 nm->per_thread_data[0].snat_thread_index = 0;
438 /* callbacks to call when interface address changes. */
439 cbi.function = nat44_ei_ip4_add_del_interface_address_cb;
440 vec_add1 (nm->ip4_main->add_del_interface_address_callbacks, cbi);
441 cbi.function = nat44_ei_ip4_add_del_addr_only_sm_cb;
442 vec_add1 (nm->ip4_main->add_del_interface_address_callbacks, cbi);
444 /* callbacks to call when interface to table biding changes */
445 cbt.function = nat44_ei_update_outside_fib;
446 vec_add1 (nm->ip4_main->table_bind_callbacks, cbt);
448 nm->fib_src_low = fib_source_allocate (
449 "nat44-ei-low", FIB_SOURCE_PRIORITY_LOW, FIB_SOURCE_BH_SIMPLE);
450 nm->fib_src_hi = fib_source_allocate ("nat44-ei-hi", FIB_SOURCE_PRIORITY_HI,
451 FIB_SOURCE_BH_SIMPLE);
453 // used only by out2in-dpo feature
454 nat_dpo_module_init ();
455 nat_ha_init (vm, nm->num_workers, num_threads);
457 nm->hairpinning_fq_index =
458 vlib_frame_queue_main_init (nat44_ei_hairpinning_node.index, 0);
459 nm->in2out_hairpinning_finish_ip4_lookup_node_fq_index =
460 vlib_frame_queue_main_init (
461 nat44_ei_in2out_hairpinning_finish_ip4_lookup_node.index, 0);
462 nm->in2out_hairpinning_finish_interface_output_node_fq_index =
463 vlib_frame_queue_main_init (
464 nat44_ei_in2out_hairpinning_finish_interface_output_node.index, 0);
465 return nat44_ei_api_hookup (vm);
468 VLIB_INIT_FUNCTION (nat44_ei_init);
471 nat44_ei_plugin_enable (nat44_ei_config_t c)
473 nat44_ei_main_t *nm = &nat44_ei_main;
481 c.sessions = 10 * 1024;
483 if (!c.user_sessions)
484 c.user_sessions = c.sessions;
488 if (!nm->frame_queue_nelts)
489 nm->frame_queue_nelts = NAT_FQ_NELTS_DEFAULT;
491 nm->translations = c.sessions;
492 nm->translation_buckets = nat_calc_bihash_buckets (c.sessions);
493 nm->user_buckets = nat_calc_bihash_buckets (c.users);
495 nm->pat = (!c.static_mapping_only ||
496 (c.static_mapping_only && c.connection_tracking));
498 nm->static_mapping_only = c.static_mapping_only;
499 nm->static_mapping_connection_tracking = c.connection_tracking;
500 nm->out2in_dpo = c.out2in_dpo;
501 nm->forwarding_enabled = 0;
502 nm->mss_clamping = 0;
504 nm->max_users_per_thread = c.users;
505 nm->max_translations_per_thread = c.sessions;
506 nm->max_translations_per_user = c.user_sessions;
508 nm->inside_vrf_id = c.inside_vrf;
509 nm->inside_fib_index = fib_table_find_or_create_and_lock (
510 FIB_PROTOCOL_IP4, c.inside_vrf, nm->fib_src_hi);
512 nm->outside_vrf_id = c.outside_vrf;
513 nm->outside_fib_index = fib_table_find_or_create_and_lock (
514 FIB_PROTOCOL_IP4, c.outside_vrf, nm->fib_src_hi);
516 nat_reset_timeouts (&nm->timeouts);
517 nat44_ei_db_init (nm->translations, nm->translation_buckets,
519 nat44_ei_set_alloc_default ();
521 vlib_zero_simple_counter (&nm->total_users, 0);
522 vlib_zero_simple_counter (&nm->total_sessions, 0);
523 vlib_zero_simple_counter (&nm->user_limit_reached, 0);
525 if (nm->num_workers > 1)
527 if (nm->fq_in2out_index == ~0)
529 nm->fq_in2out_index = vlib_frame_queue_main_init (
530 nm->in2out_node_index, nm->frame_queue_nelts);
532 if (nm->fq_out2in_index == ~0)
534 nm->fq_out2in_index = vlib_frame_queue_main_init (
535 nm->out2in_node_index, nm->frame_queue_nelts);
537 if (nm->fq_in2out_output_index == ~0)
539 nm->fq_in2out_output_index = vlib_frame_queue_main_init (
540 nm->in2out_output_node_index, nm->frame_queue_nelts);
550 static_always_inline nat44_ei_outside_fib_t *
551 nat44_ei_get_outside_fib (nat44_ei_outside_fib_t *outside_fibs, u32 fib_index)
553 nat44_ei_outside_fib_t *f;
554 vec_foreach (f, outside_fibs)
556 if (f->fib_index == fib_index)
564 static_always_inline nat44_ei_interface_t *
565 nat44_ei_get_interface (nat44_ei_interface_t *interfaces, u32 sw_if_index)
567 nat44_ei_interface_t *i;
568 pool_foreach (i, interfaces)
570 if (i->sw_if_index == sw_if_index)
578 static_always_inline int
579 nat44_ei_hairpinning_enable (u32 sw_if_index, u8 is_enable)
581 return vnet_feature_enable_disable ("ip4-local", "nat44-ei-hairpinning",
582 sw_if_index, is_enable, 0, 0);
586 nat44_ei_add_interface (u32 sw_if_index, u8 is_inside)
588 const char *feature_name, *del_feature_name;
589 nat44_ei_main_t *nm = &nat44_ei_main;
591 nat44_ei_outside_fib_t *outside_fib;
592 nat44_ei_interface_t *i;
598 if (nm->out2in_dpo && !is_inside)
600 nat44_ei_log_err ("error unsupported");
601 return VNET_API_ERROR_UNSUPPORTED;
604 if (nat44_ei_get_interface (nm->output_feature_interfaces, sw_if_index))
606 nat44_ei_log_err ("error interface already configured");
607 return VNET_API_ERROR_VALUE_EXIST;
610 i = nat44_ei_get_interface (nm->interfaces, sw_if_index);
613 if ((nat44_ei_interface_is_inside (i) && is_inside) ||
614 (nat44_ei_interface_is_outside (i) && !is_inside))
618 if (nm->num_workers > 1)
620 del_feature_name = !is_inside ? "nat44-ei-in2out-worker-handoff" :
621 "nat44-ei-out2in-worker-handoff";
622 feature_name = "nat44-ei-handoff-classify";
627 !is_inside ? "nat44-ei-in2out" : "nat44-ei-out2in";
629 feature_name = "nat44-ei-classify";
632 rv = ip4_sv_reass_enable_disable_with_refcnt (sw_if_index, 1);
637 rv = vnet_feature_enable_disable ("ip4-unicast", del_feature_name,
638 sw_if_index, 0, 0, 0);
643 rv = vnet_feature_enable_disable ("ip4-unicast", feature_name,
644 sw_if_index, 1, 0, 0);
651 rv = nat44_ei_hairpinning_enable (sw_if_index, 0);
660 if (nm->num_workers > 1)
662 feature_name = is_inside ? "nat44-ei-in2out-worker-handoff" :
663 "nat44-ei-out2in-worker-handoff";
667 feature_name = is_inside ? "nat44-ei-in2out" : "nat44-ei-out2in";
669 nat_validate_interface_counters (nm, sw_if_index);
671 rv = ip4_sv_reass_enable_disable_with_refcnt (sw_if_index, 1);
676 rv = vnet_feature_enable_disable ("ip4-unicast", feature_name,
677 sw_if_index, 1, 0, 0);
682 if (is_inside && !nm->out2in_dpo)
684 rv = nat44_ei_hairpinning_enable (sw_if_index, 1);
691 pool_get (nm->interfaces, i);
692 i->sw_if_index = sw_if_index;
697 fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4, sw_if_index);
701 i->flags |= NAT44_EI_INTERFACE_FLAG_IS_OUTSIDE;
703 outside_fib = nat44_ei_get_outside_fib (nm->outside_fibs, fib_index);
706 outside_fib->refcount++;
710 vec_add2 (nm->outside_fibs, outside_fib, 1);
711 outside_fib->fib_index = fib_index;
712 outside_fib->refcount = 1;
715 nat44_ei_add_del_addr_to_fib_foreach_addr (sw_if_index, 1);
716 nat44_ei_add_del_addr_to_fib_foreach_addr_only_sm (sw_if_index, 1);
720 i->flags |= NAT44_EI_INTERFACE_FLAG_IS_INSIDE;
727 nat44_ei_del_interface (u32 sw_if_index, u8 is_inside)
729 const char *feature_name, *del_feature_name;
730 nat44_ei_main_t *nm = &nat44_ei_main;
732 nat44_ei_outside_fib_t *outside_fib;
733 nat44_ei_interface_t *i;
739 if (nm->out2in_dpo && !is_inside)
741 nat44_ei_log_err ("error unsupported");
742 return VNET_API_ERROR_UNSUPPORTED;
745 i = nat44_ei_get_interface (nm->interfaces, sw_if_index);
748 nat44_ei_log_err ("error interface couldn't be found");
749 return VNET_API_ERROR_NO_SUCH_ENTRY;
752 if (nat44_ei_interface_is_inside (i) && nat44_ei_interface_is_outside (i))
754 if (nm->num_workers > 1)
756 del_feature_name = "nat44-ei-handoff-classify";
757 feature_name = !is_inside ? "nat44-ei-in2out-worker-handoff" :
758 "nat44-ei-out2in-worker-handoff";
762 del_feature_name = "nat44-ei-classify";
763 feature_name = !is_inside ? "nat44-ei-in2out" : "nat44-ei-out2in";
766 rv = ip4_sv_reass_enable_disable_with_refcnt (sw_if_index, 0);
771 rv = vnet_feature_enable_disable ("ip4-unicast", del_feature_name,
772 sw_if_index, 0, 0, 0);
777 rv = vnet_feature_enable_disable ("ip4-unicast", feature_name,
778 sw_if_index, 1, 0, 0);
785 i->flags &= ~NAT44_EI_INTERFACE_FLAG_IS_INSIDE;
789 rv = nat44_ei_hairpinning_enable (sw_if_index, 1);
794 i->flags &= ~NAT44_EI_INTERFACE_FLAG_IS_OUTSIDE;
799 if (nm->num_workers > 1)
801 feature_name = is_inside ? "nat44-ei-in2out-worker-handoff" :
802 "nat44-ei-out2in-worker-handoff";
806 feature_name = is_inside ? "nat44-ei-in2out" : "nat44-ei-out2in";
809 rv = ip4_sv_reass_enable_disable_with_refcnt (sw_if_index, 0);
814 rv = vnet_feature_enable_disable ("ip4-unicast", feature_name,
815 sw_if_index, 0, 0, 0);
822 rv = nat44_ei_hairpinning_enable (sw_if_index, 0);
830 pool_put (nm->interfaces, i);
836 fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4, sw_if_index);
837 outside_fib = nat44_ei_get_outside_fib (nm->outside_fibs, fib_index);
840 outside_fib->refcount--;
841 if (!outside_fib->refcount)
843 vec_del1 (nm->outside_fibs, outside_fib - nm->outside_fibs);
847 nat44_ei_add_del_addr_to_fib_foreach_addr (sw_if_index, 0);
848 nat44_ei_add_del_addr_to_fib_foreach_addr_only_sm (sw_if_index, 0);
855 nat44_ei_add_output_interface (u32 sw_if_index)
857 nat44_ei_main_t *nm = &nat44_ei_main;
859 nat44_ei_outside_fib_t *outside_fib;
860 nat44_ei_interface_t *i;
866 if (nat44_ei_get_interface (nm->interfaces, sw_if_index))
868 nat44_ei_log_err ("error interface already configured");
869 return VNET_API_ERROR_VALUE_EXIST;
872 if (nat44_ei_get_interface (nm->output_feature_interfaces, sw_if_index))
874 nat44_ei_log_err ("error interface already configured");
875 return VNET_API_ERROR_VALUE_EXIST;
878 if (nm->num_workers > 1)
880 rv = ip4_sv_reass_enable_disable_with_refcnt (sw_if_index, 1);
885 rv = ip4_sv_reass_output_enable_disable_with_refcnt (sw_if_index, 1);
890 rv = vnet_feature_enable_disable (
891 "ip4-unicast", "nat44-ei-out2in-worker-handoff", sw_if_index, 1, 0, 0);
896 rv = vnet_feature_enable_disable (
897 "ip4-output", "nat44-ei-in2out-output-worker-handoff", sw_if_index, 1,
906 rv = ip4_sv_reass_enable_disable_with_refcnt (sw_if_index, 1);
911 rv = ip4_sv_reass_output_enable_disable_with_refcnt (sw_if_index, 1);
916 rv = vnet_feature_enable_disable ("ip4-unicast", "nat44-ei-out2in",
917 sw_if_index, 1, 0, 0);
922 rv = vnet_feature_enable_disable ("ip4-output", "nat44-ei-in2out-output",
923 sw_if_index, 1, 0, 0);
930 nat_validate_interface_counters (nm, sw_if_index);
932 pool_get (nm->output_feature_interfaces, i);
933 i->sw_if_index = sw_if_index;
935 i->flags |= NAT44_EI_INTERFACE_FLAG_IS_INSIDE;
936 i->flags |= NAT44_EI_INTERFACE_FLAG_IS_OUTSIDE;
939 fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4, sw_if_index);
940 outside_fib = nat44_ei_get_outside_fib (nm->outside_fibs, fib_index);
943 outside_fib->refcount++;
947 vec_add2 (nm->outside_fibs, outside_fib, 1);
948 outside_fib->fib_index = fib_index;
949 outside_fib->refcount = 1;
952 nat44_ei_add_del_addr_to_fib_foreach_addr (sw_if_index, 1);
953 nat44_ei_add_del_addr_to_fib_foreach_addr_only_sm (sw_if_index, 1);
959 nat44_ei_del_output_interface (u32 sw_if_index)
961 nat44_ei_main_t *nm = &nat44_ei_main;
963 nat44_ei_outside_fib_t *outside_fib;
964 nat44_ei_interface_t *i;
970 i = nat44_ei_get_interface (nm->output_feature_interfaces, sw_if_index);
973 nat44_ei_log_err ("error interface couldn't be found");
974 return VNET_API_ERROR_NO_SUCH_ENTRY;
977 if (nm->num_workers > 1)
979 rv = ip4_sv_reass_enable_disable_with_refcnt (sw_if_index, 0);
984 rv = ip4_sv_reass_output_enable_disable_with_refcnt (sw_if_index, 0);
989 rv = vnet_feature_enable_disable (
990 "ip4-unicast", "nat44-ei-out2in-worker-handoff", sw_if_index, 0, 0, 0);
995 rv = vnet_feature_enable_disable (
996 "ip4-output", "nat44-ei-in2out-output-worker-handoff", sw_if_index, 0,
1005 rv = ip4_sv_reass_enable_disable_with_refcnt (sw_if_index, 0);
1010 rv = ip4_sv_reass_output_enable_disable_with_refcnt (sw_if_index, 0);
1015 rv = vnet_feature_enable_disable ("ip4-unicast", "nat44-ei-out2in",
1016 sw_if_index, 0, 0, 0);
1021 rv = vnet_feature_enable_disable ("ip4-output", "nat44-ei-in2out-output",
1022 sw_if_index, 0, 0, 0);
1029 pool_put (nm->output_feature_interfaces, i);
1032 fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4, sw_if_index);
1033 outside_fib = nat44_ei_get_outside_fib (nm->outside_fibs, fib_index);
1036 outside_fib->refcount--;
1037 if (!outside_fib->refcount)
1039 vec_del1 (nm->outside_fibs, outside_fib - nm->outside_fibs);
1043 nat44_ei_add_del_addr_to_fib_foreach_addr (sw_if_index, 1);
1044 nat44_ei_add_del_addr_to_fib_foreach_addr_only_sm (sw_if_index, 1);
1050 nat44_ei_add_del_output_interface (u32 sw_if_index, int is_del)
1054 return nat44_ei_del_output_interface (sw_if_index);
1058 return nat44_ei_add_output_interface (sw_if_index);
1063 nat44_ei_del_addresses ()
1065 nat44_ei_main_t *nm = &nat44_ei_main;
1066 nat44_ei_address_t *a, *vec;
1069 vec = vec_dup (nm->addresses);
1070 vec_foreach (a, vec)
1072 error = nat44_ei_del_address (a->addr, 0);
1076 nat44_ei_log_err ("error occurred while removing adderess");
1080 vec_free (nm->addresses);
1083 vec_free (nm->auto_add_sw_if_indices);
1084 nm->auto_add_sw_if_indices = 0;
1089 nat44_ei_del_interfaces ()
1091 nat44_ei_main_t *nm = &nat44_ei_main;
1092 nat44_ei_interface_t *i, *pool;
1095 pool = pool_dup (nm->interfaces);
1096 pool_foreach (i, pool)
1098 if (nat44_ei_interface_is_inside (i))
1100 error = nat44_ei_del_interface (i->sw_if_index, 1);
1102 if (nat44_ei_interface_is_outside (i))
1104 error = nat44_ei_del_interface (i->sw_if_index, 0);
1109 nat44_ei_log_err ("error occurred while removing interface");
1113 pool_free (nm->interfaces);
1119 nat44_ei_del_output_interfaces ()
1121 nat44_ei_main_t *nm = &nat44_ei_main;
1122 nat44_ei_interface_t *i, *pool;
1125 pool = pool_dup (nm->output_feature_interfaces);
1126 pool_foreach (i, pool)
1128 error = nat44_ei_del_output_interface (i->sw_if_index);
1131 nat44_ei_log_err ("error occurred while removing output interface");
1135 pool_free (nm->output_feature_interfaces);
1136 nm->output_feature_interfaces = 0;
1141 nat44_ei_del_static_mappings ()
1143 nat44_ei_main_t *nm = &nat44_ei_main;
1144 nat44_ei_static_mapping_t *m, *pool;
1147 pool = pool_dup (nm->static_mappings);
1148 pool_foreach (m, pool)
1150 error = nat44_ei_del_static_mapping_internal (
1151 m->local_addr, m->external_addr, m->local_port, m->external_port,
1152 m->proto, m->vrf_id, ~0, m->flags);
1155 nat44_ei_log_err ("error occurred while removing mapping");
1159 pool_free (nm->static_mappings);
1160 nm->static_mappings = 0;
1162 vec_free (nm->to_resolve);
1165 clib_bihash_free_8_8 (&nm->static_mapping_by_local);
1166 clib_bihash_free_8_8 (&nm->static_mapping_by_external);
1172 nat44_ei_plugin_disable ()
1174 nat44_ei_main_t *nm = &nat44_ei_main;
1175 nat44_ei_main_per_thread_data_t *tnm;
1180 rc = nat44_ei_del_static_mappings ();
1184 rc = nat44_ei_del_addresses ();
1188 rc = nat44_ei_del_interfaces ();
1192 rc = nat44_ei_del_output_interfaces ();
1198 clib_bihash_free_8_8 (&nm->in2out);
1199 clib_bihash_free_8_8 (&nm->out2in);
1201 vec_foreach (tnm, nm->per_thread_data)
1203 nat44_ei_worker_db_free (tnm);
1207 clib_memset (&nm->rconfig, 0, sizeof (nm->rconfig));
1209 nm->forwarding_enabled = 0;
1216 nat44_ei_set_outside_address_and_port (nat44_ei_address_t *addresses,
1217 u32 thread_index, ip4_address_t addr,
1218 u16 port, nat_protocol_t protocol)
1220 nat44_ei_main_t *nm = &nat44_ei_main;
1221 nat44_ei_address_t *a = 0;
1223 u16 port_host_byte_order = clib_net_to_host_u16 (port);
1225 for (address_index = 0; address_index < vec_len (addresses); address_index++)
1227 if (addresses[address_index].addr.as_u32 != addr.as_u32)
1230 a = addresses + address_index;
1233 #define _(N, j, n, s) \
1234 case NAT_PROTOCOL_##N: \
1235 if (a->busy_##n##_port_refcounts[port_host_byte_order]) \
1236 return VNET_API_ERROR_INSTANCE_IN_USE; \
1237 ++a->busy_##n##_port_refcounts[port_host_byte_order]; \
1238 a->busy_##n##_ports_per_thread[thread_index]++; \
1239 a->busy_##n##_ports++; \
1241 foreach_nat_protocol
1243 default : nat_elog_info (nm, "unknown protocol");
1248 return VNET_API_ERROR_NO_SUCH_ENTRY;
1252 nat44_ei_add_del_address_dpo (ip4_address_t addr, u8 is_add)
1254 nat44_ei_main_t *nm = &nat44_ei_main;
1255 dpo_id_t dpo_v4 = DPO_INVALID;
1256 fib_prefix_t pfx = {
1257 .fp_proto = FIB_PROTOCOL_IP4,
1259 .fp_addr.ip4.as_u32 = addr.as_u32,
1264 nat_dpo_create (DPO_PROTO_IP4, 0, &dpo_v4);
1265 fib_table_entry_special_dpo_add (0, &pfx, nm->fib_src_hi,
1266 FIB_ENTRY_FLAG_EXCLUSIVE, &dpo_v4);
1267 dpo_reset (&dpo_v4);
1271 fib_table_entry_special_remove (0, &pfx, nm->fib_src_hi);
1276 nat44_ei_free_outside_address_and_port (nat44_ei_address_t *addresses,
1277 u32 thread_index, ip4_address_t *addr,
1278 u16 port, nat_protocol_t protocol)
1280 nat44_ei_main_t *nm = &nat44_ei_main;
1281 nat44_ei_address_t *a;
1283 u16 port_host_byte_order = clib_net_to_host_u16 (port);
1285 for (address_index = 0; address_index < vec_len (addresses); address_index++)
1287 if (addresses[address_index].addr.as_u32 == addr->as_u32)
1291 ASSERT (address_index < vec_len (addresses));
1293 a = addresses + address_index;
1297 #define _(N, i, n, s) \
1298 case NAT_PROTOCOL_##N: \
1299 ASSERT (a->busy_##n##_port_refcounts[port_host_byte_order] >= 1); \
1300 --a->busy_##n##_port_refcounts[port_host_byte_order]; \
1301 a->busy_##n##_ports--; \
1302 a->busy_##n##_ports_per_thread[thread_index]--; \
1304 foreach_nat_protocol
1306 default : nat_elog_info (nm, "unknown protocol");
1312 nat44_ei_free_session_data_v2 (nat44_ei_main_t *nm, nat44_ei_session_t *s,
1313 u32 thread_index, u8 is_ha)
1315 clib_bihash_kv_8_8_t kv;
1317 /* session lookup tables */
1318 init_nat_i2o_k (&kv, s);
1319 if (clib_bihash_add_del_8_8 (&nm->in2out, &kv, 0))
1320 nat_elog_warn (nm, "in2out key del failed");
1321 init_nat_o2i_k (&kv, s);
1322 if (clib_bihash_add_del_8_8 (&nm->out2in, &kv, 0))
1323 nat_elog_warn (nm, "out2in key del failed");
1326 nat_syslog_nat44_apmdel (s->user_index, s->in2out.fib_index,
1327 &s->in2out.addr, s->in2out.port, &s->out2in.addr,
1328 s->out2in.port, s->nat_proto);
1330 if (nat44_ei_is_unk_proto_session (s))
1336 nat_ipfix_logging_nat44_ses_delete (
1337 thread_index, s->in2out.addr.as_u32, s->out2in.addr.as_u32,
1338 nat_proto_to_ip_proto (s->nat_proto), s->in2out.port, s->out2in.port,
1339 s->in2out.fib_index);
1341 nat_ha_sdel (&s->out2in.addr, s->out2in.port, &s->ext_host_addr,
1342 s->ext_host_port, s->nat_proto, s->out2in.fib_index,
1346 if (nat44_ei_is_session_static (s))
1349 nat44_ei_free_outside_address_and_port (nm->addresses, thread_index,
1350 &s->out2in.addr, s->out2in.port,
1355 nat44_ei_user_get_or_create (nat44_ei_main_t *nm, ip4_address_t *addr,
1356 u32 fib_index, u32 thread_index)
1358 nat44_ei_user_t *u = 0;
1359 nat44_ei_user_key_t user_key;
1360 clib_bihash_kv_8_8_t kv, value;
1361 nat44_ei_main_per_thread_data_t *tnm = &nm->per_thread_data[thread_index];
1362 dlist_elt_t *per_user_list_head_elt;
1364 user_key.addr.as_u32 = addr->as_u32;
1365 user_key.fib_index = fib_index;
1366 kv.key = user_key.as_u64;
1368 /* Ever heard of the "user" = src ip4 address before? */
1369 if (clib_bihash_search_8_8 (&tnm->user_hash, &kv, &value))
1371 if (pool_elts (tnm->users) >= nm->max_users_per_thread)
1373 vlib_increment_simple_counter (&nm->user_limit_reached, thread_index,
1375 nat_elog_warn (nm, "maximum user limit reached");
1378 /* no, make a new one */
1379 pool_get (tnm->users, u);
1380 clib_memset (u, 0, sizeof (*u));
1382 u->addr.as_u32 = addr->as_u32;
1383 u->fib_index = fib_index;
1385 pool_get (tnm->list_pool, per_user_list_head_elt);
1387 u->sessions_per_user_list_head_index =
1388 per_user_list_head_elt - tnm->list_pool;
1390 clib_dlist_init (tnm->list_pool, u->sessions_per_user_list_head_index);
1392 kv.value = u - tnm->users;
1395 if (clib_bihash_add_del_8_8 (&tnm->user_hash, &kv, 1))
1397 nat_elog_warn (nm, "user_hash key add failed");
1398 nat44_ei_delete_user_with_no_session (nm, u, thread_index);
1402 vlib_set_simple_counter (&nm->total_users, thread_index, 0,
1403 pool_elts (tnm->users));
1407 u = pool_elt_at_index (tnm->users, value.value);
1413 nat44_ei_session_t *
1414 nat44_ei_session_alloc_or_recycle (nat44_ei_main_t *nm, nat44_ei_user_t *u,
1415 u32 thread_index, f64 now)
1417 nat44_ei_session_t *s;
1418 nat44_ei_main_per_thread_data_t *tnm = &nm->per_thread_data[thread_index];
1419 u32 oldest_per_user_translation_list_index, session_index;
1420 dlist_elt_t *oldest_per_user_translation_list_elt;
1421 dlist_elt_t *per_user_translation_list_elt;
1423 /* Over quota? Recycle the least recently used translation */
1424 if ((u->nsessions + u->nstaticsessions) >= nm->max_translations_per_user)
1426 oldest_per_user_translation_list_index = clib_dlist_remove_head (
1427 tnm->list_pool, u->sessions_per_user_list_head_index);
1429 ASSERT (oldest_per_user_translation_list_index != ~0);
1431 /* Add it back to the end of the LRU list */
1432 clib_dlist_addtail (tnm->list_pool, u->sessions_per_user_list_head_index,
1433 oldest_per_user_translation_list_index);
1434 /* Get the list element */
1435 oldest_per_user_translation_list_elt = pool_elt_at_index (
1436 tnm->list_pool, oldest_per_user_translation_list_index);
1438 /* Get the session index from the list element */
1439 session_index = oldest_per_user_translation_list_elt->value;
1441 /* Get the session */
1442 s = pool_elt_at_index (tnm->sessions, session_index);
1444 nat44_ei_free_session_data_v2 (nm, s, thread_index, 0);
1445 if (nat44_ei_is_session_static (s))
1446 u->nstaticsessions--;
1453 s->ext_host_addr.as_u32 = 0;
1454 s->ext_host_port = 0;
1455 s->ext_host_nat_addr.as_u32 = 0;
1456 s->ext_host_nat_port = 0;
1460 pool_get (tnm->sessions, s);
1461 clib_memset (s, 0, sizeof (*s));
1463 /* Create list elts */
1464 pool_get (tnm->list_pool, per_user_translation_list_elt);
1465 clib_dlist_init (tnm->list_pool,
1466 per_user_translation_list_elt - tnm->list_pool);
1468 per_user_translation_list_elt->value = s - tnm->sessions;
1469 s->per_user_index = per_user_translation_list_elt - tnm->list_pool;
1470 s->per_user_list_head_index = u->sessions_per_user_list_head_index;
1472 clib_dlist_addtail (tnm->list_pool, s->per_user_list_head_index,
1473 per_user_translation_list_elt - tnm->list_pool);
1475 s->user_index = u - tnm->users;
1476 vlib_set_simple_counter (&nm->total_sessions, thread_index, 0,
1477 pool_elts (tnm->sessions));
1480 s->ha_last_refreshed = now;
1486 nat44_ei_free_session_data (nat44_ei_main_t *nm, nat44_ei_session_t *s,
1487 u32 thread_index, u8 is_ha)
1489 clib_bihash_kv_8_8_t kv;
1491 init_nat_i2o_k (&kv, s);
1492 if (clib_bihash_add_del_8_8 (&nm->in2out, &kv, 0))
1493 nat_elog_warn (nm, "in2out key del failed");
1495 init_nat_o2i_k (&kv, s);
1496 if (clib_bihash_add_del_8_8 (&nm->out2in, &kv, 0))
1497 nat_elog_warn (nm, "out2in key del failed");
1501 nat_syslog_nat44_apmdel (s->user_index, s->in2out.fib_index,
1502 &s->in2out.addr, s->in2out.port,
1503 &s->out2in.addr, s->out2in.port, s->nat_proto);
1505 nat_ipfix_logging_nat44_ses_delete (
1506 thread_index, s->in2out.addr.as_u32, s->out2in.addr.as_u32,
1507 nat_proto_to_ip_proto (s->nat_proto), s->in2out.port, s->out2in.port,
1508 s->in2out.fib_index);
1510 nat_ha_sdel (&s->out2in.addr, s->out2in.port, &s->ext_host_addr,
1511 s->ext_host_port, s->nat_proto, s->out2in.fib_index,
1515 if (nat44_ei_is_session_static (s))
1518 nat44_ei_free_outside_address_and_port (nm->addresses, thread_index,
1519 &s->out2in.addr, s->out2in.port,
1523 static_always_inline void
1524 nat44_ei_user_del_sessions (nat44_ei_user_t *u, u32 thread_index)
1527 nat44_ei_session_t *s;
1529 nat44_ei_main_t *nm = &nat44_ei_main;
1530 nat44_ei_main_per_thread_data_t *tnm = &nm->per_thread_data[thread_index];
1534 pool_elt_at_index (tnm->list_pool, u->sessions_per_user_list_head_index);
1535 // get first element
1536 elt = pool_elt_at_index (tnm->list_pool, elt->next);
1538 while (elt->value != ~0)
1540 s = pool_elt_at_index (tnm->sessions, elt->value);
1541 elt = pool_elt_at_index (tnm->list_pool, elt->next);
1543 nat44_ei_free_session_data (nm, s, thread_index, 0);
1544 nat44_ei_delete_session (nm, s, thread_index);
1549 nat44_ei_user_del (ip4_address_t *addr, u32 fib_index)
1553 nat44_ei_main_t *nm = &nat44_ei_main;
1554 nat44_ei_main_per_thread_data_t *tnm;
1556 nat44_ei_user_key_t user_key;
1557 clib_bihash_kv_8_8_t kv, value;
1559 user_key.addr.as_u32 = addr->as_u32;
1560 user_key.fib_index = fib_index;
1561 kv.key = user_key.as_u64;
1563 if (nm->num_workers > 1)
1565 vec_foreach (tnm, nm->per_thread_data)
1567 if (!clib_bihash_search_8_8 (&tnm->user_hash, &kv, &value))
1569 nat44_ei_user_del_sessions (
1570 pool_elt_at_index (tnm->users, value.value),
1579 tnm = vec_elt_at_index (nm->per_thread_data, nm->num_workers);
1580 if (!clib_bihash_search_8_8 (&tnm->user_hash, &kv, &value))
1582 nat44_ei_user_del_sessions (
1583 pool_elt_at_index (tnm->users, value.value), tnm->thread_index);
1591 nat44_ei_static_mapping_del_sessions (nat44_ei_main_t *nm,
1592 nat44_ei_main_per_thread_data_t *tnm,
1593 nat44_ei_user_key_t u_key, int addr_only,
1594 ip4_address_t e_addr, u16 e_port)
1596 clib_bihash_kv_8_8_t kv, value;
1597 kv.key = u_key.as_u64;
1599 dlist_elt_t *head, *elt;
1601 nat44_ei_session_t *s;
1602 u32 elt_index, head_index, ses_index;
1604 if (!clib_bihash_search_8_8 (&tnm->user_hash, &kv, &value))
1606 user_index = value.value;
1607 u = pool_elt_at_index (tnm->users, user_index);
1608 if (u->nstaticsessions)
1610 head_index = u->sessions_per_user_list_head_index;
1611 head = pool_elt_at_index (tnm->list_pool, head_index);
1612 elt_index = head->next;
1613 elt = pool_elt_at_index (tnm->list_pool, elt_index);
1614 ses_index = elt->value;
1615 while (ses_index != ~0)
1617 s = pool_elt_at_index (tnm->sessions, ses_index);
1618 elt = pool_elt_at_index (tnm->list_pool, elt->next);
1619 ses_index = elt->value;
1623 if ((s->out2in.addr.as_u32 != e_addr.as_u32) ||
1624 (s->out2in.port != e_port))
1628 if (!nat44_ei_is_session_static (s))
1631 nat44_ei_free_session_data_v2 (nm, s, tnm - nm->per_thread_data,
1633 nat44_ei_delete_session (nm, s, tnm - nm->per_thread_data);
1643 nat44_ei_get_in2out_worker_index (ip4_header_t *ip0, u32 rx_fib_index0,
1646 nat44_ei_main_t *nm = &nat44_ei_main;
1647 u32 next_worker_index = 0;
1650 next_worker_index = nm->first_worker_index;
1651 hash = ip0->src_address.as_u32 + (ip0->src_address.as_u32 >> 8) +
1652 (ip0->src_address.as_u32 >> 16) + (ip0->src_address.as_u32 >> 24);
1654 if (PREDICT_TRUE (is_pow2 (_vec_len (nm->workers))))
1655 next_worker_index += nm->workers[hash & (_vec_len (nm->workers) - 1)];
1657 next_worker_index += nm->workers[hash % _vec_len (nm->workers)];
1659 return next_worker_index;
1663 nat44_ei_get_out2in_worker_index (vlib_buffer_t *b, ip4_header_t *ip0,
1664 u32 rx_fib_index0, u8 is_output)
1666 nat44_ei_main_t *nm = &nat44_ei_main;
1669 clib_bihash_kv_8_8_t kv, value;
1670 nat44_ei_static_mapping_t *m;
1672 u32 next_worker_index = 0;
1674 /* first try static mappings without port */
1675 if (PREDICT_FALSE (pool_elts (nm->static_mappings)))
1677 init_nat_k (&kv, ip0->dst_address, 0, rx_fib_index0, 0);
1678 if (!clib_bihash_search_8_8 (&nm->static_mapping_by_external, &kv,
1681 m = pool_elt_at_index (nm->static_mappings, value.value);
1682 return m->workers[0];
1686 proto = ip_proto_to_nat_proto (ip0->protocol);
1687 udp = ip4_next_header (ip0);
1688 port = vnet_buffer (b)->ip.reass.l4_dst_port;
1690 /* unknown protocol */
1691 if (PREDICT_FALSE (proto == NAT_PROTOCOL_OTHER))
1693 /* use current thread */
1694 return vlib_get_thread_index ();
1697 if (PREDICT_FALSE (ip0->protocol == IP_PROTOCOL_ICMP))
1699 icmp46_header_t *icmp = (icmp46_header_t *) udp;
1700 icmp_echo_header_t *echo = (icmp_echo_header_t *) (icmp + 1);
1701 if (!icmp_type_is_error_message (
1702 vnet_buffer (b)->ip.reass.icmp_type_or_tcp_flags))
1703 port = vnet_buffer (b)->ip.reass.l4_src_port;
1706 /* if error message, then it's not fragmented and we can access it */
1707 ip4_header_t *inner_ip = (ip4_header_t *) (echo + 1);
1708 proto = ip_proto_to_nat_proto (inner_ip->protocol);
1709 void *l4_header = ip4_next_header (inner_ip);
1712 case NAT_PROTOCOL_ICMP:
1713 icmp = (icmp46_header_t *) l4_header;
1714 echo = (icmp_echo_header_t *) (icmp + 1);
1715 port = echo->identifier;
1717 case NAT_PROTOCOL_UDP:
1718 case NAT_PROTOCOL_TCP:
1719 port = ((tcp_udp_header_t *) l4_header)->src_port;
1722 return vlib_get_thread_index ();
1727 /* try static mappings with port */
1728 if (PREDICT_FALSE (pool_elts (nm->static_mappings)))
1730 init_nat_k (&kv, ip0->dst_address, port, rx_fib_index0, proto);
1731 if (!clib_bihash_search_8_8 (&nm->static_mapping_by_external, &kv,
1734 m = pool_elt_at_index (nm->static_mappings, value.value);
1735 return m->workers[0];
1739 /* worker by outside port */
1740 next_worker_index = nm->first_worker_index;
1741 next_worker_index +=
1742 nm->workers[(clib_net_to_host_u16 (port) - 1024) / nm->port_per_thread];
1743 return next_worker_index;
1747 nat44_ei_alloc_default_cb (nat44_ei_address_t *addresses, u32 fib_index,
1748 u32 thread_index, nat_protocol_t proto,
1749 ip4_address_t s_addr, ip4_address_t *addr,
1750 u16 *port, u16 port_per_thread,
1751 u32 snat_thread_index)
1753 nat44_ei_main_t *nm = &nat44_ei_main;
1754 nat44_ei_address_t *a, *ga = 0;
1758 if (vec_len (addresses) > 0)
1761 int s_addr_offset = s_addr.as_u32 % vec_len (addresses);
1763 for (i = s_addr_offset; i < vec_len (addresses); ++i)
1768 #define _(N, j, n, s) \
1769 case NAT_PROTOCOL_##N: \
1770 if (a->busy_##n##_ports_per_thread[thread_index] < port_per_thread) \
1772 if (a->fib_index == fib_index) \
1776 portnum = (port_per_thread * snat_thread_index) + \
1777 nat_random_port (&nm->random_seed, 0, \
1778 port_per_thread - 1) + \
1780 if (a->busy_##n##_port_refcounts[portnum]) \
1782 --a->busy_##n##_port_refcounts[portnum]; \
1783 a->busy_##n##_ports_per_thread[thread_index]++; \
1784 a->busy_##n##_ports++; \
1786 *port = clib_host_to_net_u16 (portnum); \
1790 else if (a->fib_index == ~0) \
1796 foreach_nat_protocol;
1798 nat_elog_info (nm, "unknown protocol");
1803 for (i = 0; i < s_addr_offset; ++i)
1808 foreach_nat_protocol;
1810 nat_elog_info (nm, "unknown protocol");
1817 // fake fib index to reuse macro
1821 foreach_nat_protocol;
1822 default : nat_elog_info (nm, "unknown protocol");
1830 /* Totally out of translations to use... */
1831 nat_ipfix_logging_addresses_exhausted (thread_index, 0);
1836 nat44_ei_alloc_range_cb (nat44_ei_address_t *addresses, u32 fib_index,
1837 u32 thread_index, nat_protocol_t proto,
1838 ip4_address_t s_addr, ip4_address_t *addr, u16 *port,
1839 u16 port_per_thread, u32 snat_thread_index)
1841 nat44_ei_main_t *nm = &nat44_ei_main;
1842 nat44_ei_address_t *a = addresses;
1845 ports = nm->end_port - nm->start_port + 1;
1847 if (!vec_len (addresses))
1852 #define _(N, i, n, s) \
1853 case NAT_PROTOCOL_##N: \
1854 if (a->busy_##n##_ports < ports) \
1858 portnum = nat_random_port (&nm->random_seed, nm->start_port, \
1860 if (a->busy_##n##_port_refcounts[portnum]) \
1862 ++a->busy_##n##_port_refcounts[portnum]; \
1863 a->busy_##n##_ports++; \
1865 *port = clib_host_to_net_u16 (portnum); \
1870 foreach_nat_protocol
1872 default : nat_elog_info (nm, "unknown protocol");
1877 /* Totally out of translations to use... */
1878 nat_ipfix_logging_addresses_exhausted (thread_index, 0);
1883 nat44_ei_alloc_mape_cb (nat44_ei_address_t *addresses, u32 fib_index,
1884 u32 thread_index, nat_protocol_t proto,
1885 ip4_address_t s_addr, ip4_address_t *addr, u16 *port,
1886 u16 port_per_thread, u32 snat_thread_index)
1888 nat44_ei_main_t *nm = &nat44_ei_main;
1889 nat44_ei_address_t *a = addresses;
1890 u16 m, ports, portnum, A, j;
1891 m = 16 - (nm->psid_offset + nm->psid_length);
1892 ports = (1 << (16 - nm->psid_length)) - (1 << m);
1894 if (!vec_len (addresses))
1899 #define _(N, i, n, s) \
1900 case NAT_PROTOCOL_##N: \
1901 if (a->busy_##n##_ports < ports) \
1905 A = nat_random_port (&nm->random_seed, 1, \
1906 pow2_mask (nm->psid_offset)); \
1907 j = nat_random_port (&nm->random_seed, 0, pow2_mask (m)); \
1908 portnum = A | (nm->psid << nm->psid_offset) | (j << (16 - m)); \
1909 if (a->busy_##n##_port_refcounts[portnum]) \
1911 ++a->busy_##n##_port_refcounts[portnum]; \
1912 a->busy_##n##_ports++; \
1914 *port = clib_host_to_net_u16 (portnum); \
1919 foreach_nat_protocol
1921 default : nat_elog_info (nm, "unknown protocol");
1926 /* Totally out of translations to use... */
1927 nat_ipfix_logging_addresses_exhausted (thread_index, 0);
1932 nat44_ei_set_alloc_default ()
1934 nat44_ei_main_t *nm = &nat44_ei_main;
1936 nm->addr_and_port_alloc_alg = NAT44_EI_ADDR_AND_PORT_ALLOC_ALG_DEFAULT;
1937 nm->alloc_addr_and_port = nat44_ei_alloc_default_cb;
1941 nat44_ei_set_alloc_range (u16 start_port, u16 end_port)
1943 nat44_ei_main_t *nm = &nat44_ei_main;
1945 nm->addr_and_port_alloc_alg = NAT44_EI_ADDR_AND_PORT_ALLOC_ALG_RANGE;
1946 nm->alloc_addr_and_port = nat44_ei_alloc_range_cb;
1947 nm->start_port = start_port;
1948 nm->end_port = end_port;
1952 nat44_ei_set_alloc_mape (u16 psid, u16 psid_offset, u16 psid_length)
1954 nat44_ei_main_t *nm = &nat44_ei_main;
1956 nm->addr_and_port_alloc_alg = NAT44_EI_ADDR_AND_PORT_ALLOC_ALG_MAPE;
1957 nm->alloc_addr_and_port = nat44_ei_alloc_mape_cb;
1959 nm->psid_offset = psid_offset;
1960 nm->psid_length = psid_length;
1964 nat44_ei_delete_session (nat44_ei_main_t *nm, nat44_ei_session_t *ses,
1967 nat44_ei_main_per_thread_data_t *tnm =
1968 vec_elt_at_index (nm->per_thread_data, thread_index);
1969 clib_bihash_kv_8_8_t kv, value;
1971 const nat44_ei_user_key_t u_key = { .addr = ses->in2out.addr,
1972 .fib_index = ses->in2out.fib_index };
1973 const u8 u_static = nat44_ei_is_session_static (ses);
1975 clib_dlist_remove (tnm->list_pool, ses->per_user_index);
1976 pool_put_index (tnm->list_pool, ses->per_user_index);
1978 pool_put (tnm->sessions, ses);
1979 vlib_set_simple_counter (&nm->total_sessions, thread_index, 0,
1980 pool_elts (tnm->sessions));
1982 kv.key = u_key.as_u64;
1983 if (!clib_bihash_search_8_8 (&tnm->user_hash, &kv, &value))
1985 u = pool_elt_at_index (tnm->users, value.value);
1987 u->nstaticsessions--;
1991 nat44_ei_delete_user_with_no_session (nm, u, thread_index);
1996 nat44_ei_del_session (nat44_ei_main_t *nm, ip4_address_t *addr, u16 port,
1997 nat_protocol_t proto, u32 vrf_id, int is_in)
1999 nat44_ei_main_per_thread_data_t *tnm;
2000 clib_bihash_kv_8_8_t kv, value;
2002 nat44_ei_session_t *s;
2003 clib_bihash_8_8_t *t;
2005 fail_if_disabled ();
2007 fib_index = fib_table_find (FIB_PROTOCOL_IP4, vrf_id);
2008 init_nat_k (&kv, *addr, port, fib_index, proto);
2009 t = is_in ? &nm->in2out : &nm->out2in;
2010 if (!clib_bihash_search_8_8 (t, &kv, &value))
2012 // this is called from API/CLI, so the world is stopped here
2013 // it's safe to manipulate arbitrary per-thread data
2014 u32 thread_index = nat_value_get_thread_index (&value);
2015 tnm = vec_elt_at_index (nm->per_thread_data, thread_index);
2016 u32 session_index = nat_value_get_session_index (&value);
2017 if (pool_is_free_index (tnm->sessions, session_index))
2018 return VNET_API_ERROR_UNSPECIFIED;
2020 s = pool_elt_at_index (tnm->sessions, session_index);
2021 nat44_ei_free_session_data_v2 (nm, s, tnm - nm->per_thread_data, 0);
2022 nat44_ei_delete_session (nm, s, tnm - nm->per_thread_data);
2026 return VNET_API_ERROR_NO_SUCH_ENTRY;
2030 nat44_ei_get_thread_idx_by_port (u16 e_port)
2032 nat44_ei_main_t *nm = &nat44_ei_main;
2033 u32 thread_idx = nm->num_workers;
2034 if (nm->num_workers > 1)
2036 thread_idx = nm->first_worker_index +
2037 nm->workers[(e_port - 1024) / nm->port_per_thread];
2043 nat44_ei_add_del_addr_to_fib (ip4_address_t *addr, u8 p_len, u32 sw_if_index,
2046 nat44_ei_main_t *nm = &nat44_ei_main;
2047 fib_prefix_t prefix = {
2049 .fp_proto = FIB_PROTOCOL_IP4,
2051 .ip4.as_u32 = addr->as_u32,
2054 u32 fib_index = ip4_fib_table_get_index_for_sw_if_index (sw_if_index);
2058 fib_table_entry_update_one_path (fib_index, &prefix, nm->fib_src_low,
2059 (FIB_ENTRY_FLAG_CONNECTED |
2060 FIB_ENTRY_FLAG_LOCAL |
2061 FIB_ENTRY_FLAG_EXCLUSIVE),
2062 DPO_PROTO_IP4, NULL, sw_if_index, ~0, 1,
2063 NULL, FIB_ROUTE_PATH_FLAG_NONE);
2067 fib_table_entry_delete (fib_index, &prefix, nm->fib_src_low);
2072 nat44_ei_reserve_port (ip4_address_t addr, u16 port, nat_protocol_t proto)
2074 u32 ti = nat44_ei_get_thread_idx_by_port (port);
2075 nat44_ei_main_t *nm = &nat44_ei_main;
2076 nat44_ei_address_t *a = 0;
2079 for (i = 0; i < vec_len (nm->addresses); i++)
2081 a = nm->addresses + i;
2083 if (a->addr.as_u32 != addr.as_u32)
2088 #define _(N, j, n, s) \
2089 case NAT_PROTOCOL_##N: \
2090 if (a->busy_##n##_port_refcounts[port]) \
2092 ++a->busy_##n##_port_refcounts[port]; \
2095 a->busy_##n##_ports++; \
2096 a->busy_##n##_ports_per_thread[ti]++; \
2099 foreach_nat_protocol
2101 default : nat_elog_info (nm, "unknown protocol");
2113 nat44_ei_free_port (ip4_address_t addr, u16 port, nat_protocol_t proto)
2115 u32 ti = nat44_ei_get_thread_idx_by_port (port);
2116 nat44_ei_main_t *nm = &nat44_ei_main;
2117 nat44_ei_address_t *a = 0;
2120 for (i = 0; i < vec_len (nm->addresses); i++)
2122 a = nm->addresses + i;
2124 if (a->addr.as_u32 != addr.as_u32)
2129 #define _(N, j, n, s) \
2130 case NAT_PROTOCOL_##N: \
2131 --a->busy_##n##_port_refcounts[port]; \
2134 a->busy_##n##_ports--; \
2135 a->busy_##n##_ports_per_thread[ti]--; \
2138 foreach_nat_protocol
2140 default : nat_elog_info (nm, "unknown protocol");
2152 nat44_ei_add_resolve_record (ip4_address_t l_addr, u16 l_port, u16 e_port,
2153 nat_protocol_t proto, u32 vrf_id, u32 sw_if_index,
2154 u32 flags, ip4_address_t pool_addr, u8 *tag)
2156 nat44_ei_static_map_resolve_t *rp;
2157 nat44_ei_main_t *nm = &nat44_ei_main;
2159 vec_add2 (nm->to_resolve, rp, 1);
2160 rp->l_addr.as_u32 = l_addr.as_u32;
2161 rp->l_port = l_port;
2162 rp->e_port = e_port;
2163 rp->sw_if_index = sw_if_index;
2164 rp->vrf_id = vrf_id;
2167 rp->pool_addr = pool_addr;
2168 rp->tag = vec_dup (tag);
2172 nat44_ei_get_resolve_record (ip4_address_t l_addr, u16 l_port, u16 e_port,
2173 nat_protocol_t proto, u32 vrf_id, u32 sw_if_index,
2174 u32 flags, int *out)
2176 nat44_ei_static_map_resolve_t *rp;
2177 nat44_ei_main_t *nm = &nat44_ei_main;
2180 for (i = 0; i < vec_len (nm->to_resolve); i++)
2182 rp = nm->to_resolve + i;
2184 if (rp->sw_if_index == sw_if_index && rp->vrf_id == vrf_id)
2186 if (is_sm_identity_nat (rp->flags) && is_sm_identity_nat (flags))
2188 if (!(is_sm_addr_only (rp->flags) && is_sm_addr_only (flags)))
2190 if (rp->e_port != e_port || rp->proto != proto)
2196 else if (rp->l_addr.as_u32 == l_addr.as_u32)
2198 if (!(is_sm_addr_only (rp->flags) && is_sm_addr_only (flags)))
2200 if (rp->l_port != l_port || rp->e_port != e_port ||
2222 nat44_ei_del_resolve_record (ip4_address_t l_addr, u16 l_port, u16 e_port,
2223 nat_protocol_t proto, u32 vrf_id, u32 sw_if_index,
2226 nat44_ei_main_t *nm = &nat44_ei_main;
2228 if (!nat44_ei_get_resolve_record (l_addr, l_port, e_port, proto, vrf_id,
2229 sw_if_index, flags, &i))
2231 vec_del1 (nm->to_resolve, i);
2238 delete_matching_dynamic_sessions (const nat44_ei_static_mapping_t *m,
2241 nat44_ei_main_t *nm = &nat44_ei_main;
2242 clib_bihash_kv_8_8_t kv, value;
2243 nat44_ei_session_t *s;
2244 nat44_ei_user_key_t u_key;
2246 nat44_ei_main_per_thread_data_t *tnm;
2247 dlist_elt_t *head, *elt;
2248 u32 elt_index, head_index;
2252 if (nm->static_mapping_only)
2255 tnm = vec_elt_at_index (nm->per_thread_data, worker_index);
2257 u_key.addr = m->local_addr;
2258 u_key.fib_index = m->fib_index;
2259 kv.key = u_key.as_u64;
2260 if (!clib_bihash_search_8_8 (&tnm->user_hash, &kv, &value))
2262 user_index = value.value;
2263 u = pool_elt_at_index (tnm->users, user_index);
2266 head_index = u->sessions_per_user_list_head_index;
2267 head = pool_elt_at_index (tnm->list_pool, head_index);
2268 elt_index = head->next;
2269 elt = pool_elt_at_index (tnm->list_pool, elt_index);
2270 ses_index = elt->value;
2271 while (ses_index != ~0)
2273 s = pool_elt_at_index (tnm->sessions, ses_index);
2274 elt = pool_elt_at_index (tnm->list_pool, elt->next);
2275 ses_index = elt->value;
2277 if (nat44_ei_is_session_static (s))
2280 if (!is_sm_addr_only (m->flags) &&
2281 s->in2out.port != m->local_port)
2284 nat44_ei_free_session_data_v2 (nm, s, tnm - nm->per_thread_data,
2286 nat44_ei_delete_session (nm, s, tnm - nm->per_thread_data);
2288 if (!is_sm_addr_only (m->flags))
2296 nat44_ei_add_static_mapping (ip4_address_t l_addr, ip4_address_t e_addr,
2297 u16 l_port, u16 e_port, nat_protocol_t proto,
2298 u32 vrf_id, u32 sw_if_index, u32 flags,
2299 ip4_address_t pool_addr, u8 *tag)
2302 nat44_ei_main_t *nm = &nat44_ei_main;
2304 if (is_sm_switch_address (flags))
2306 if (!nat44_ei_get_resolve_record (l_addr, l_port, e_port, proto, vrf_id,
2307 sw_if_index, flags, 0))
2309 return VNET_API_ERROR_VALUE_EXIST;
2312 nat44_ei_add_resolve_record (l_addr, l_port, e_port, proto, vrf_id,
2313 sw_if_index, flags, pool_addr, tag);
2315 ip4_address_t *first_int_addr =
2316 ip4_interface_first_address (nm->ip4_main, sw_if_index, 0);
2317 if (!first_int_addr)
2319 // dhcp resolution required
2323 e_addr.as_u32 = first_int_addr->as_u32;
2326 return nat44_ei_add_static_mapping_internal (l_addr, e_addr, l_port, e_port,
2327 proto, vrf_id, sw_if_index,
2328 flags, pool_addr, tag);
2332 nat44_ei_del_static_mapping (ip4_address_t l_addr, ip4_address_t e_addr,
2333 u16 l_port, u16 e_port, nat_protocol_t proto,
2334 u32 vrf_id, u32 sw_if_index, u32 flags)
2336 nat44_ei_main_t *nm = &nat44_ei_main;
2338 if (is_sm_switch_address (flags))
2341 if (nat44_ei_del_resolve_record (l_addr, l_port, e_port, proto, vrf_id,
2342 sw_if_index, flags))
2344 return VNET_API_ERROR_NO_SUCH_ENTRY;
2347 ip4_address_t *first_int_addr =
2348 ip4_interface_first_address (nm->ip4_main, sw_if_index, 0);
2349 if (!first_int_addr)
2351 // dhcp resolution required
2355 e_addr.as_u32 = first_int_addr->as_u32;
2358 return nat44_ei_del_static_mapping_internal (
2359 l_addr, e_addr, l_port, e_port, proto, vrf_id, sw_if_index, flags);
2363 nat44_ei_add_static_mapping_internal (ip4_address_t l_addr,
2364 ip4_address_t e_addr, u16 l_port,
2365 u16 e_port, nat_protocol_t proto,
2366 u32 vrf_id, u32 sw_if_index, u32 flags,
2367 ip4_address_t pool_addr, u8 *tag)
2369 nat44_ei_main_t *nm = &nat44_ei_main;
2370 clib_bihash_kv_8_8_t kv, value;
2371 nat44_ei_lb_addr_port_t *local;
2372 nat44_ei_static_mapping_t *m;
2376 fail_if_disabled ();
2378 if (is_sm_addr_only (flags))
2380 e_port = l_port = proto = 0;
2383 if (is_sm_identity_nat (flags))
2386 l_addr.as_u32 = e_addr.as_u32;
2390 init_nat_k (&kv, e_addr, e_port, 0, proto);
2392 if (!clib_bihash_search_8_8 (&nm->static_mapping_by_external, &kv, &value))
2394 m = pool_elt_at_index (nm->static_mappings, value.value);
2395 if (!is_sm_identity_nat (m->flags))
2397 return VNET_API_ERROR_VALUE_EXIST;
2401 // adding local identity nat record for different vrf table
2402 pool_foreach (local, m->locals)
2404 if (local->vrf_id == vrf_id)
2406 return VNET_API_ERROR_VALUE_EXIST;
2410 pool_get (m->locals, local);
2412 local->vrf_id = vrf_id;
2413 local->fib_index = fib_table_find_or_create_and_lock (
2414 FIB_PROTOCOL_IP4, vrf_id, nm->fib_src_low);
2416 init_nat_kv (&kv, m->local_addr, m->local_port, local->fib_index,
2417 m->proto, 0, m - nm->static_mappings);
2418 clib_bihash_add_del_8_8 (&nm->static_mapping_by_local, &kv, 1);
2425 fib_index = fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4, vrf_id,
2430 // fallback to default vrf
2431 vrf_id = nm->inside_vrf_id;
2432 fib_index = nm->inside_fib_index;
2433 fib_table_lock (fib_index, FIB_PROTOCOL_IP4, nm->fib_src_low);
2436 if (!is_sm_identity_nat (flags))
2438 init_nat_k (&kv, l_addr, l_port, fib_index, proto);
2439 if (!clib_bihash_search_8_8 (&nm->static_mapping_by_local, &kv, &value))
2441 return VNET_API_ERROR_VALUE_EXIST;
2445 if (!(is_sm_addr_only (flags) || nm->static_mapping_only))
2447 if (nat44_ei_reserve_port (e_addr, e_port, proto))
2449 // remove resolve record
2450 if ((is_sm_switch_address (flags)) && !is_sm_identity_nat (flags))
2452 nat44_ei_del_resolve_record (l_addr, l_port, e_port, proto,
2453 vrf_id, sw_if_index, flags);
2455 return VNET_API_ERROR_NO_SUCH_ENTRY;
2459 pool_get (nm->static_mappings, m);
2460 clib_memset (m, 0, sizeof (*m));
2463 m->local_addr = l_addr;
2464 m->external_addr = e_addr;
2466 m->tag = vec_dup (tag);
2468 if (!is_sm_addr_only (flags))
2470 m->local_port = l_port;
2471 m->external_port = e_port;
2475 if (is_sm_identity_nat (flags))
2477 pool_get (m->locals, local);
2479 local->vrf_id = vrf_id;
2480 local->fib_index = fib_index;
2485 m->fib_index = fib_index;
2488 init_nat_kv (&kv, m->local_addr, m->local_port, fib_index, m->proto, 0,
2489 m - nm->static_mappings);
2490 clib_bihash_add_del_8_8 (&nm->static_mapping_by_local, &kv, 1);
2492 init_nat_kv (&kv, m->external_addr, m->external_port, 0, m->proto, 0,
2493 m - nm->static_mappings);
2494 clib_bihash_add_del_8_8 (&nm->static_mapping_by_external, &kv, 1);
2496 if (nm->num_workers > 1)
2498 // store worker index for this record
2500 .src_address = m->local_addr,
2502 worker_index = nat44_ei_get_in2out_worker_index (&ip, m->fib_index, 0);
2503 vec_add1 (m->workers, worker_index);
2507 worker_index = nm->num_workers;
2509 delete_matching_dynamic_sessions (m, worker_index);
2511 if (is_sm_addr_only (flags))
2513 nat44_ei_add_del_addr_to_fib_foreach_out_if (&e_addr, 1);
2520 nat44_ei_del_static_mapping_internal (ip4_address_t l_addr,
2521 ip4_address_t e_addr, u16 l_port,
2522 u16 e_port, nat_protocol_t proto,
2523 u32 vrf_id, u32 sw_if_index, u32 flags)
2525 nat44_ei_main_per_thread_data_t *tnm;
2526 nat44_ei_main_t *nm = &nat44_ei_main;
2527 clib_bihash_kv_8_8_t kv, value;
2528 nat44_ei_lb_addr_port_t *local;
2529 nat44_ei_static_mapping_t *m;
2531 nat44_ei_user_key_t u_key;
2533 fail_if_disabled ();
2535 if (is_sm_addr_only (flags))
2537 e_port = l_port = proto = 0;
2540 if (is_sm_identity_nat (flags))
2543 l_addr.as_u32 = e_addr.as_u32;
2547 init_nat_k (&kv, e_addr, e_port, 0, proto);
2549 if (clib_bihash_search_8_8 (&nm->static_mapping_by_external, &kv, &value))
2551 if (is_sm_switch_address (flags))
2555 return VNET_API_ERROR_NO_SUCH_ENTRY;
2558 m = pool_elt_at_index (nm->static_mappings, value.value);
2560 if (is_sm_identity_nat (flags))
2566 vrf_id = nm->inside_vrf_id;
2569 pool_foreach (local, m->locals)
2571 if (local->vrf_id == vrf_id)
2573 local = pool_elt_at_index (m->locals, local - m->locals);
2574 fib_index = local->fib_index;
2575 pool_put (m->locals, local);
2581 return VNET_API_ERROR_NO_SUCH_ENTRY;
2586 fib_index = m->fib_index;
2589 if (!(is_sm_addr_only (flags) || nm->static_mapping_only))
2591 if (nat44_ei_free_port (e_addr, e_port, proto))
2593 return VNET_API_ERROR_INVALID_VALUE;
2597 init_nat_k (&kv, l_addr, l_port, fib_index, proto);
2598 clib_bihash_add_del_8_8 (&nm->static_mapping_by_local, &kv, 0);
2600 if (!nm->static_mapping_only || nm->static_mapping_connection_tracking)
2602 // delete sessions for static mapping
2603 if (nm->num_workers > 1)
2604 tnm = vec_elt_at_index (nm->per_thread_data, m->workers[0]);
2606 tnm = vec_elt_at_index (nm->per_thread_data, nm->num_workers);
2608 u_key.addr = m->local_addr;
2609 u_key.fib_index = fib_index;
2610 kv.key = u_key.as_u64;
2611 nat44_ei_static_mapping_del_sessions (
2612 nm, tnm, u_key, is_sm_addr_only (flags), e_addr, e_port);
2615 fib_table_unlock (fib_index, FIB_PROTOCOL_IP4, nm->fib_src_low);
2617 if (!pool_elts (m->locals))
2619 // this is last record remove all required stuff
2621 init_nat_k (&kv, e_addr, e_port, 0, proto);
2622 clib_bihash_add_del_8_8 (&nm->static_mapping_by_external, &kv, 0);
2625 vec_free (m->workers);
2626 pool_put (nm->static_mappings, m);
2628 if (is_sm_addr_only (flags) && !is_sm_identity_nat (flags))
2630 nat44_ei_add_del_addr_to_fib_foreach_out_if (&e_addr, 0);
2638 nat44_ei_static_mapping_match (ip4_address_t match_addr, u16 match_port,
2639 u32 match_fib_index,
2640 nat_protocol_t match_protocol,
2641 ip4_address_t *mapping_addr, u16 *mapping_port,
2642 u32 *mapping_fib_index, u8 by_external,
2643 u8 *is_addr_only, u8 *is_identity_nat)
2645 nat44_ei_main_t *nm = &nat44_ei_main;
2646 clib_bihash_kv_8_8_t kv, value;
2647 nat44_ei_static_mapping_t *m;
2652 init_nat_k (&kv, match_addr, match_port, 0, match_protocol);
2653 if (clib_bihash_search_8_8 (&nm->static_mapping_by_external, &kv,
2656 /* Try address only mapping */
2657 init_nat_k (&kv, match_addr, 0, 0, 0);
2658 if (clib_bihash_search_8_8 (&nm->static_mapping_by_external, &kv,
2662 m = pool_elt_at_index (nm->static_mappings, value.value);
2664 *mapping_fib_index = m->fib_index;
2665 *mapping_addr = m->local_addr;
2666 port = m->local_port;
2670 init_nat_k (&kv, match_addr, match_port, match_fib_index,
2672 if (clib_bihash_search_8_8 (&nm->static_mapping_by_local, &kv, &value))
2674 /* Try address only mapping */
2675 init_nat_k (&kv, match_addr, 0, match_fib_index, 0);
2676 if (clib_bihash_search_8_8 (&nm->static_mapping_by_local, &kv,
2680 m = pool_elt_at_index (nm->static_mappings, value.value);
2682 *mapping_fib_index = nm->outside_fib_index;
2683 *mapping_addr = m->external_addr;
2684 port = m->external_port;
2687 /* Address only mapping doesn't change port */
2688 if (is_sm_addr_only (m->flags))
2689 *mapping_port = match_port;
2691 *mapping_port = port;
2693 if (PREDICT_FALSE (is_addr_only != 0))
2694 *is_addr_only = is_sm_addr_only (m->flags);
2696 if (PREDICT_FALSE (is_identity_nat != 0))
2697 *is_identity_nat = is_sm_identity_nat (m->flags);
2703 nat44_ei_worker_db_free (nat44_ei_main_per_thread_data_t *tnm)
2705 pool_free (tnm->list_pool);
2706 pool_free (tnm->lru_pool);
2707 pool_free (tnm->sessions);
2708 pool_free (tnm->users);
2710 clib_bihash_free_8_8 (&tnm->user_hash);
2714 format_nat44_ei_key (u8 *s, va_list *args)
2716 u64 key = va_arg (*args, u64);
2720 nat_protocol_t protocol;
2723 split_nat_key (key, &addr, &port, &fib_index, &protocol);
2725 s = format (s, "%U proto %U port %d fib %d", format_ip4_address, &addr,
2726 format_nat_protocol, protocol, clib_net_to_host_u16 (port),
2732 format_nat44_ei_user_kvp (u8 *s, va_list *args)
2734 clib_bihash_kv_8_8_t *v = va_arg (*args, clib_bihash_kv_8_8_t *);
2735 nat44_ei_user_key_t k;
2739 s = format (s, "%U fib %d user-index %llu", format_ip4_address, &k.addr,
2740 k.fib_index, v->value);
2746 format_nat44_ei_session_kvp (u8 *s, va_list *args)
2748 clib_bihash_kv_8_8_t *v = va_arg (*args, clib_bihash_kv_8_8_t *);
2750 s = format (s, "%U thread-index %llu session-index %llu",
2751 format_nat44_ei_key, v->key, nat_value_get_thread_index (v),
2752 nat_value_get_session_index (v));
2758 format_nat44_ei_static_mapping_kvp (u8 *s, va_list *args)
2760 clib_bihash_kv_8_8_t *v = va_arg (*args, clib_bihash_kv_8_8_t *);
2762 s = format (s, "%U static-mapping-index %llu", format_nat44_ei_key, v->key,
2769 nat44_ei_worker_db_init (nat44_ei_main_per_thread_data_t *tnm,
2770 u32 translations, u32 translation_buckets,
2775 pool_alloc (tnm->list_pool, translations);
2776 pool_alloc (tnm->lru_pool, translations);
2777 pool_alloc (tnm->sessions, translations);
2779 clib_bihash_init_8_8 (&tnm->user_hash, "users", user_buckets, 0);
2781 clib_bihash_set_kvp_format_fn_8_8 (&tnm->user_hash,
2782 format_nat44_ei_user_kvp);
2784 pool_get (tnm->lru_pool, head);
2785 tnm->tcp_trans_lru_head_index = head - tnm->lru_pool;
2786 clib_dlist_init (tnm->lru_pool, tnm->tcp_trans_lru_head_index);
2788 pool_get (tnm->lru_pool, head);
2789 tnm->tcp_estab_lru_head_index = head - tnm->lru_pool;
2790 clib_dlist_init (tnm->lru_pool, tnm->tcp_estab_lru_head_index);
2792 pool_get (tnm->lru_pool, head);
2793 tnm->udp_lru_head_index = head - tnm->lru_pool;
2794 clib_dlist_init (tnm->lru_pool, tnm->udp_lru_head_index);
2796 pool_get (tnm->lru_pool, head);
2797 tnm->icmp_lru_head_index = head - tnm->lru_pool;
2798 clib_dlist_init (tnm->lru_pool, tnm->icmp_lru_head_index);
2800 pool_get (tnm->lru_pool, head);
2801 tnm->unk_proto_lru_head_index = head - tnm->lru_pool;
2802 clib_dlist_init (tnm->lru_pool, tnm->unk_proto_lru_head_index);
2806 nat44_ei_db_init (u32 translations, u32 translation_buckets, u32 user_buckets)
2808 nat44_ei_main_t *nm = &nat44_ei_main;
2809 nat44_ei_main_per_thread_data_t *tnm;
2811 u32 static_mapping_buckets = 1024;
2812 u32 static_mapping_memory_size = 64 << 20;
2814 clib_bihash_init_8_8 (&nm->static_mapping_by_local,
2815 "static_mapping_by_local", static_mapping_buckets,
2816 static_mapping_memory_size);
2817 clib_bihash_init_8_8 (&nm->static_mapping_by_external,
2818 "static_mapping_by_external", static_mapping_buckets,
2819 static_mapping_memory_size);
2820 clib_bihash_set_kvp_format_fn_8_8 (&nm->static_mapping_by_local,
2821 format_nat44_ei_static_mapping_kvp);
2822 clib_bihash_set_kvp_format_fn_8_8 (&nm->static_mapping_by_external,
2823 format_nat44_ei_static_mapping_kvp);
2827 clib_bihash_init_8_8 (&nm->in2out, "in2out", translation_buckets, 0);
2828 clib_bihash_init_8_8 (&nm->out2in, "out2in", translation_buckets, 0);
2829 clib_bihash_set_kvp_format_fn_8_8 (&nm->in2out,
2830 format_nat44_ei_session_kvp);
2831 clib_bihash_set_kvp_format_fn_8_8 (&nm->out2in,
2832 format_nat44_ei_session_kvp);
2833 vec_foreach (tnm, nm->per_thread_data)
2835 nat44_ei_worker_db_init (tnm, translations, translation_buckets,
2842 nat44_ei_sessions_clear ()
2844 nat44_ei_main_t *nm = &nat44_ei_main;
2845 nat44_ei_main_per_thread_data_t *tnm;
2849 clib_bihash_free_8_8 (&nm->in2out);
2850 clib_bihash_free_8_8 (&nm->out2in);
2851 clib_bihash_init_8_8 (&nm->in2out, "in2out", nm->translation_buckets, 0);
2852 clib_bihash_init_8_8 (&nm->out2in, "out2in", nm->translation_buckets, 0);
2853 clib_bihash_set_kvp_format_fn_8_8 (&nm->in2out,
2854 format_nat44_ei_session_kvp);
2855 clib_bihash_set_kvp_format_fn_8_8 (&nm->out2in,
2856 format_nat44_ei_session_kvp);
2857 vec_foreach (tnm, nm->per_thread_data)
2859 nat44_ei_worker_db_free (tnm);
2860 nat44_ei_worker_db_init (tnm, nm->translations,
2861 nm->translation_buckets, nm->user_buckets);
2865 vlib_zero_simple_counter (&nm->total_users, 0);
2866 vlib_zero_simple_counter (&nm->total_sessions, 0);
2867 vlib_zero_simple_counter (&nm->user_limit_reached, 0);
2871 nat44_ei_update_outside_fib (ip4_main_t *im, uword opaque, u32 sw_if_index,
2872 u32 new_fib_index, u32 old_fib_index)
2874 nat44_ei_main_t *nm = &nat44_ei_main;
2875 nat44_ei_outside_fib_t *outside_fib;
2876 nat44_ei_interface_t *i;
2880 if (!nm->enabled || (new_fib_index == old_fib_index) ||
2881 (!vec_len (nm->outside_fibs)))
2886 pool_foreach (i, nm->interfaces)
2888 if (i->sw_if_index == sw_if_index)
2890 if (!(nat44_ei_interface_is_outside (i)))
2896 pool_foreach (i, nm->output_feature_interfaces)
2898 if (i->sw_if_index == sw_if_index)
2900 if (!(nat44_ei_interface_is_outside (i)))
2909 vec_foreach (outside_fib, nm->outside_fibs)
2911 if (outside_fib->fib_index == old_fib_index)
2913 outside_fib->refcount--;
2914 if (!outside_fib->refcount)
2915 vec_del1 (nm->outside_fibs, outside_fib - nm->outside_fibs);
2920 vec_foreach (outside_fib, nm->outside_fibs)
2922 if (outside_fib->fib_index == new_fib_index)
2924 outside_fib->refcount++;
2932 vec_add2 (nm->outside_fibs, outside_fib, 1);
2933 outside_fib->refcount = 1;
2934 outside_fib->fib_index = new_fib_index;
2939 nat44_ei_add_address (ip4_address_t *addr, u32 vrf_id)
2941 nat44_ei_main_t *nm = &nat44_ei_main;
2942 vlib_thread_main_t *tm = vlib_get_thread_main ();
2943 nat44_ei_address_t *ap;
2945 fail_if_disabled ();
2947 /* Check if address already exists */
2948 vec_foreach (ap, nm->addresses)
2950 if (ap->addr.as_u32 == addr->as_u32)
2952 nat44_ei_log_err ("address exist");
2953 return VNET_API_ERROR_VALUE_EXIST;
2957 vec_add2 (nm->addresses, ap, 1);
2964 ap->fib_index = fib_table_find_or_create_and_lock (
2965 FIB_PROTOCOL_IP4, vrf_id, nm->fib_src_low);
2968 #define _(N, i, n, s) \
2969 clib_memset (ap->busy_##n##_port_refcounts, 0, \
2970 sizeof (ap->busy_##n##_port_refcounts)); \
2971 ap->busy_##n##_ports = 0; \
2972 ap->busy_##n##_ports_per_thread = 0; \
2973 vec_validate_init_empty (ap->busy_##n##_ports_per_thread, \
2974 tm->n_vlib_mains - 1, 0);
2975 foreach_nat_protocol
2978 nat44_ei_add_del_addr_to_fib_foreach_out_if (addr, 1);
2984 nat44_ei_del_address (ip4_address_t addr, u8 delete_sm)
2986 nat44_ei_main_t *nm = &nat44_ei_main;
2987 nat44_ei_address_t *a = 0;
2988 nat44_ei_session_t *ses;
2989 u32 *ses_to_be_removed = 0, *ses_index;
2990 nat44_ei_main_per_thread_data_t *tnm;
2991 nat44_ei_static_mapping_t *m;
2994 fail_if_disabled ();
2996 /* Find SNAT address */
2997 for (j = 0; j < vec_len (nm->addresses); j++)
2999 if (nm->addresses[j].addr.as_u32 == addr.as_u32)
3001 a = nm->addresses + j;
3007 nat44_ei_log_err ("no such address");
3008 return VNET_API_ERROR_NO_SUCH_ENTRY;
3013 pool_foreach (m, nm->static_mappings)
3015 if (m->external_addr.as_u32 == addr.as_u32)
3016 nat44_ei_del_static_mapping_internal (
3017 m->local_addr, m->external_addr, m->local_port, m->external_port,
3018 m->proto, m->vrf_id, ~0, m->flags);
3023 /* Check if address is used in some static mapping */
3024 if (nat44_ei_is_address_used_in_static_mapping (addr))
3026 nat44_ei_log_err ("address used in static mapping");
3027 return VNET_API_ERROR_UNSPECIFIED;
3031 /* Delete sessions using address */
3032 if (a->busy_tcp_ports || a->busy_udp_ports || a->busy_icmp_ports)
3034 vec_foreach (tnm, nm->per_thread_data)
3036 pool_foreach (ses, tnm->sessions)
3038 if (ses->out2in.addr.as_u32 == addr.as_u32)
3040 nat44_ei_free_session_data (nm, ses,
3041 tnm - nm->per_thread_data, 0);
3042 vec_add1 (ses_to_be_removed, ses - tnm->sessions);
3045 vec_foreach (ses_index, ses_to_be_removed)
3047 ses = pool_elt_at_index (tnm->sessions, ses_index[0]);
3048 nat44_ei_delete_session (nm, ses, tnm - nm->per_thread_data);
3050 vec_free (ses_to_be_removed);
3054 nat44_ei_add_del_addr_to_fib_foreach_out_if (&addr, 0);
3056 if (a->fib_index != ~0)
3058 fib_table_unlock (a->fib_index, FIB_PROTOCOL_IP4, nm->fib_src_low);
3061 #define _(N, i, n, s) vec_free (a->busy_##n##_ports_per_thread);
3062 foreach_nat_protocol
3065 vec_del1 (nm->addresses, j);
3070 nat44_ei_add_interface_address (u32 sw_if_index)
3072 nat44_ei_main_t *nm = &nat44_ei_main;
3073 ip4_main_t *ip4_main = nm->ip4_main;
3074 ip4_address_t *first_int_addr;
3075 u32 *auto_add_sw_if_indices = nm->auto_add_sw_if_indices;
3078 for (i = 0; i < vec_len (auto_add_sw_if_indices); i++)
3080 if (auto_add_sw_if_indices[i] == sw_if_index)
3082 return VNET_API_ERROR_VALUE_EXIST;
3086 /* add to the auto-address list */
3087 vec_add1 (nm->auto_add_sw_if_indices, sw_if_index);
3089 // if the address is already bound - or static - add it now
3090 first_int_addr = ip4_interface_first_address (ip4_main, sw_if_index, 0);
3093 (void) nat44_ei_add_address (first_int_addr, ~0);
3100 nat44_ei_del_interface_address (u32 sw_if_index)
3102 nat44_ei_main_t *nm = &nat44_ei_main;
3103 ip4_main_t *ip4_main = nm->ip4_main;
3104 ip4_address_t *first_int_addr;
3105 nat44_ei_static_map_resolve_t *rp;
3106 u32 *indices_to_delete = 0;
3108 u32 *auto_add_sw_if_indices = nm->auto_add_sw_if_indices;
3110 fail_if_disabled ();
3112 first_int_addr = ip4_interface_first_address (ip4_main, sw_if_index, 0);
3114 for (i = 0; i < vec_len (auto_add_sw_if_indices); i++)
3116 if (auto_add_sw_if_indices[i] == sw_if_index)
3119 ip4_interface_first_address (ip4_main, sw_if_index, 0);
3122 (void) nat44_ei_del_address (first_int_addr[0], 1);
3126 for (j = 0; j < vec_len (nm->to_resolve); j++)
3128 rp = nm->to_resolve + j;
3129 if (rp->sw_if_index == sw_if_index)
3131 vec_add1 (indices_to_delete, j);
3134 if (vec_len (indices_to_delete))
3136 for (j = vec_len (indices_to_delete) - 1; j >= 0; j--)
3138 vec_del1 (nm->to_resolve, j);
3140 vec_free (indices_to_delete);
3144 vec_del1 (nm->auto_add_sw_if_indices, i);
3148 return VNET_API_ERROR_NO_SUCH_ENTRY;
3151 static_always_inline int
3152 is_sw_if_index_reg_for_auto_resolve (u32 *sw_if_indices, u32 sw_if_index)
3155 vec_foreach (i, sw_if_indices)
3157 if (*i == sw_if_index)
3166 nat44_ei_ip4_add_del_interface_address_cb (ip4_main_t *im, uword opaque,
3168 ip4_address_t *address,
3170 u32 if_address_index, u32 is_delete)
3172 nat44_ei_main_t *nm = &nat44_ei_main;
3173 nat44_ei_static_map_resolve_t *rp;
3174 nat44_ei_address_t *addresses = nm->addresses;
3182 if (!is_sw_if_index_reg_for_auto_resolve (nm->auto_add_sw_if_indices,
3190 /* Don't trip over lease renewal, static config */
3191 for (i = 0; i < vec_len (addresses); i++)
3193 if (addresses[i].addr.as_u32 == address->as_u32)
3199 (void) nat44_ei_add_address (address, ~0);
3201 /* Scan static map resolution vector */
3202 for (i = 0; i < vec_len (nm->to_resolve); i++)
3204 rp = nm->to_resolve + i;
3205 if (is_sm_addr_only (rp->flags))
3209 /* On this interface? */
3210 if (rp->sw_if_index == sw_if_index)
3212 rv = nat44_ei_add_static_mapping_internal (
3213 rp->l_addr, address[0], rp->l_port, rp->e_port, rp->proto,
3214 rp->vrf_id, ~0, rp->flags, rp->pool_addr, rp->tag);
3217 nat_elog_notice_X1 (
3218 nm, "add_static_mapping_internal returned %d", "i4", rv);
3225 // remove all static mapping records
3226 (void) nat44_ei_del_address (address[0], 1);
3231 nat44_ei_set_frame_queue_nelts (u32 frame_queue_nelts)
3234 nat44_ei_main_t *nm = &nat44_ei_main;
3235 nm->frame_queue_nelts = frame_queue_nelts;
3240 nat44_ei_ip4_add_del_addr_only_sm_cb (ip4_main_t *im, uword opaque,
3241 u32 sw_if_index, ip4_address_t *address,
3242 u32 address_length, u32 if_address_index,
3245 nat44_ei_main_t *nm = &nat44_ei_main;
3246 nat44_ei_static_map_resolve_t *rp;
3247 nat44_ei_static_mapping_t *m;
3248 clib_bihash_kv_8_8_t kv, value;
3249 int i, rv = 0, match = 0;
3256 for (i = 0; i < vec_len (nm->to_resolve); i++)
3258 rp = nm->to_resolve + i;
3260 if (is_sm_addr_only (rp->flags) && rp->sw_if_index == sw_if_index)
3272 init_nat_k (&kv, *address, is_sm_addr_only (rp->flags) ? 0 : rp->e_port,
3273 nm->outside_fib_index,
3274 is_sm_addr_only (rp->flags) ? 0 : rp->proto);
3275 if (clib_bihash_search_8_8 (&nm->static_mapping_by_external, &kv, &value))
3278 m = pool_elt_at_index (nm->static_mappings, value.value);
3284 rv = nat44_ei_del_static_mapping_internal (
3285 rp->l_addr, address[0], rp->l_port, rp->e_port, rp->proto, rp->vrf_id,
3289 nat_elog_notice_X1 (nm, "nat44_ei_del_static_mapping returned %d",
3297 rv = nat44_ei_add_static_mapping_internal (
3298 rp->l_addr, address[0], rp->l_port, rp->e_port, rp->proto, rp->vrf_id,
3299 ~0, rp->flags, rp->pool_addr, rp->tag);
3303 nat_elog_notice_X1 (nm, "nat44_ei_add_static_mapping returned %d",
3309 static_always_inline uword
3310 nat44_ei_classify_inline_fn (vlib_main_t *vm, vlib_node_runtime_t *node,
3311 vlib_frame_t *frame)
3313 u32 n_left_from, *from, *to_next;
3314 nat44_ei_classify_next_t next_index;
3315 nat44_ei_main_t *nm = &nat44_ei_main;
3316 nat44_ei_static_mapping_t *m;
3317 u32 next_in2out = 0, next_out2in = 0;
3319 from = vlib_frame_vector_args (frame);
3320 n_left_from = frame->n_vectors;
3321 next_index = node->cached_next_index;
3323 while (n_left_from > 0)
3327 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
3329 while (n_left_from > 0 && n_left_to_next > 0)
3333 u32 next0 = NAT44_EI_CLASSIFY_NEXT_IN2OUT;
3335 nat44_ei_address_t *ap;
3336 clib_bihash_kv_8_8_t kv0, value0;
3338 /* speculatively enqueue b0 to the current next frame */
3344 n_left_to_next -= 1;
3346 b0 = vlib_get_buffer (vm, bi0);
3347 ip0 = vlib_buffer_get_current (b0);
3349 vec_foreach (ap, nm->addresses)
3351 if (ip0->dst_address.as_u32 == ap->addr.as_u32)
3353 next0 = NAT44_EI_CLASSIFY_NEXT_OUT2IN;
3358 if (PREDICT_FALSE (pool_elts (nm->static_mappings)))
3360 init_nat_k (&kv0, ip0->dst_address, 0, 0, 0);
3361 /* try to classify the fragment based on IP header alone */
3362 if (!clib_bihash_search_8_8 (&nm->static_mapping_by_external,
3365 m = pool_elt_at_index (nm->static_mappings, value0.value);
3366 if (m->local_addr.as_u32 != m->external_addr.as_u32)
3367 next0 = NAT44_EI_CLASSIFY_NEXT_OUT2IN;
3370 init_nat_k (&kv0, ip0->dst_address,
3371 vnet_buffer (b0)->ip.reass.l4_dst_port, 0,
3372 ip_proto_to_nat_proto (ip0->protocol));
3373 if (!clib_bihash_search_8_8 (&nm->static_mapping_by_external,
3376 m = pool_elt_at_index (nm->static_mappings, value0.value);
3377 if (m->local_addr.as_u32 != m->external_addr.as_u32)
3378 next0 = NAT44_EI_CLASSIFY_NEXT_OUT2IN;
3383 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE) &&
3384 (b0->flags & VLIB_BUFFER_IS_TRACED)))
3386 nat44_ei_classify_trace_t *t =
3387 vlib_add_trace (vm, node, b0, sizeof (*t));
3389 t->next_in2out = next0 == NAT44_EI_CLASSIFY_NEXT_IN2OUT ? 1 : 0;
3392 next_in2out += next0 == NAT44_EI_CLASSIFY_NEXT_IN2OUT;
3393 next_out2in += next0 == NAT44_EI_CLASSIFY_NEXT_OUT2IN;
3395 /* verify speculative enqueue, maybe switch current next frame */
3396 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
3397 n_left_to_next, bi0, next0);
3400 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
3403 vlib_node_increment_counter (
3404 vm, node->node_index, NAT44_EI_CLASSIFY_ERROR_NEXT_IN2OUT, next_in2out);
3405 vlib_node_increment_counter (
3406 vm, node->node_index, NAT44_EI_CLASSIFY_ERROR_NEXT_OUT2IN, next_out2in);
3407 return frame->n_vectors;
3410 VLIB_NODE_FN (nat44_ei_classify_node)
3411 (vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
3413 return nat44_ei_classify_inline_fn (vm, node, frame);
3416 VLIB_REGISTER_NODE (nat44_ei_classify_node) = {
3417 .name = "nat44-ei-classify",
3418 .vector_size = sizeof (u32),
3419 .format_trace = format_nat44_ei_classify_trace,
3420 .type = VLIB_NODE_TYPE_INTERNAL,
3421 .n_errors = ARRAY_LEN(nat44_ei_classify_error_strings),
3422 .error_strings = nat44_ei_classify_error_strings,
3423 .n_next_nodes = NAT44_EI_CLASSIFY_N_NEXT,
3425 [NAT44_EI_CLASSIFY_NEXT_IN2OUT] = "nat44-ei-in2out",
3426 [NAT44_EI_CLASSIFY_NEXT_OUT2IN] = "nat44-ei-out2in",
3427 [NAT44_EI_CLASSIFY_NEXT_DROP] = "error-drop",
3431 VLIB_NODE_FN (nat44_ei_handoff_classify_node)
3432 (vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
3434 return nat44_ei_classify_inline_fn (vm, node, frame);
3437 VLIB_REGISTER_NODE (nat44_ei_handoff_classify_node) = {
3438 .name = "nat44-ei-handoff-classify",
3439 .vector_size = sizeof (u32),
3440 .format_trace = format_nat44_ei_classify_trace,
3441 .type = VLIB_NODE_TYPE_INTERNAL,
3442 .n_errors = ARRAY_LEN(nat44_ei_classify_error_strings),
3443 .error_strings = nat44_ei_classify_error_strings,
3444 .n_next_nodes = NAT44_EI_CLASSIFY_N_NEXT,
3446 [NAT44_EI_CLASSIFY_NEXT_IN2OUT] = "nat44-ei-in2out-worker-handoff",
3447 [NAT44_EI_CLASSIFY_NEXT_OUT2IN] = "nat44-ei-out2in-worker-handoff",
3448 [NAT44_EI_CLASSIFY_NEXT_DROP] = "error-drop",
3453 * fd.io coding-style-patch-verification: ON
3456 * eval: (c-set-style "gnu")