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_init (vlib_main_t *vm)
310 nat44_ei_main_t *nm = &nat44_ei_main;
311 vlib_thread_main_t *tm = vlib_get_thread_main ();
312 vlib_thread_registration_t *tr;
313 ip4_add_del_interface_address_callback_t cbi = { 0 };
314 ip4_table_bind_callback_t cbt = { 0 };
315 u32 i, num_threads = 0;
316 uword *p, *bitmap = 0;
318 clib_memset (nm, 0, sizeof (*nm));
321 nm->vnet_main = vnet_get_main ();
323 nm->ip4_main = &ip4_main;
324 nm->api_main = vlibapi_get_main ();
325 nm->ip4_lookup_main = &ip4_main.lookup_main;
328 nm->fq_out2in_index = ~0;
329 nm->fq_in2out_index = ~0;
330 nm->fq_in2out_output_index = ~0;
332 nm->log_level = NAT_LOG_ERROR;
334 nat44_ei_set_node_indexes (nm, vm);
335 nm->log_class = vlib_log_register_class ("nat44-ei", 0);
337 nat_init_simple_counter (nm->total_users, "total-users",
338 "/nat44-ei/total-users");
339 nat_init_simple_counter (nm->total_sessions, "total-sessions",
340 "/nat44-ei/total-sessions");
341 nat_init_simple_counter (nm->user_limit_reached, "user-limit-reached",
342 "/nat44-ei/user-limit-reached");
345 nat_init_simple_counter (nm->counters.fastpath.in2out.x, #x, \
346 "/nat44-ei/in2out/fastpath/" #x); \
347 nat_init_simple_counter (nm->counters.fastpath.out2in.x, #x, \
348 "/nat44-ei/out2in/fastpath/" #x); \
349 nat_init_simple_counter (nm->counters.slowpath.in2out.x, #x, \
350 "/nat44-ei/in2out/slowpath/" #x); \
351 nat_init_simple_counter (nm->counters.slowpath.out2in.x, #x, \
352 "/nat44-ei/out2in/slowpath/" #x);
355 nat_init_simple_counter (nm->counters.hairpinning, "hairpinning",
356 "/nat44-ei/hairpinning");
358 p = hash_get_mem (tm->thread_registrations_by_name, "workers");
361 tr = (vlib_thread_registration_t *) p[0];
364 nm->num_workers = tr->count;
365 nm->first_worker_index = tr->first_index;
368 num_threads = tm->n_vlib_mains - 1;
369 nm->port_per_thread = 0xffff - 1024;
370 vec_validate (nm->per_thread_data, num_threads);
372 /* Use all available workers by default */
373 if (nm->num_workers > 1)
376 for (i = 0; i < nm->num_workers; i++)
377 bitmap = clib_bitmap_set (bitmap, i, 1);
378 nat44_ei_set_workers (bitmap);
379 clib_bitmap_free (bitmap);
382 nm->per_thread_data[0].snat_thread_index = 0;
384 /* callbacks to call when interface address changes. */
385 cbi.function = nat44_ei_ip4_add_del_interface_address_cb;
386 vec_add1 (nm->ip4_main->add_del_interface_address_callbacks, cbi);
387 cbi.function = nat44_ei_ip4_add_del_addr_only_sm_cb;
388 vec_add1 (nm->ip4_main->add_del_interface_address_callbacks, cbi);
390 /* callbacks to call when interface to table biding changes */
391 cbt.function = nat44_ei_update_outside_fib;
392 vec_add1 (nm->ip4_main->table_bind_callbacks, cbt);
394 nm->fib_src_low = fib_source_allocate (
395 "nat44-ei-low", FIB_SOURCE_PRIORITY_LOW, FIB_SOURCE_BH_SIMPLE);
396 nm->fib_src_hi = fib_source_allocate ("nat44-ei-hi", FIB_SOURCE_PRIORITY_HI,
397 FIB_SOURCE_BH_SIMPLE);
399 // used only by out2in-dpo feature
400 nat_dpo_module_init ();
401 nat_ha_init (vm, nm->num_workers, num_threads);
403 nm->hairpinning_fq_index =
404 vlib_frame_queue_main_init (nat44_ei_hairpinning_node.index, 0);
405 nm->hairpin_dst_fq_index =
406 vlib_frame_queue_main_init (nat44_ei_hairpin_dst_node.index, 0);
407 nm->in2out_hairpinning_finish_ip4_lookup_node_fq_index =
408 vlib_frame_queue_main_init (
409 nat44_ei_in2out_hairpinning_finish_ip4_lookup_node.index, 0);
410 nm->in2out_hairpinning_finish_interface_output_node_fq_index =
411 vlib_frame_queue_main_init (
412 nat44_ei_in2out_hairpinning_finish_interface_output_node.index, 0);
413 return nat44_ei_api_hookup (vm);
416 VLIB_INIT_FUNCTION (nat44_ei_init);
419 nat44_ei_plugin_enable (nat44_ei_config_t c)
421 nat44_ei_main_t *nm = &nat44_ei_main;
429 c.sessions = 10 * 1024;
433 if (!nm->frame_queue_nelts)
434 nm->frame_queue_nelts = NAT_FQ_NELTS_DEFAULT;
436 nm->translations = c.sessions;
437 nm->translation_buckets = nat_calc_bihash_buckets (c.sessions);
438 nm->user_buckets = nat_calc_bihash_buckets (c.users);
440 nm->pat = (!c.static_mapping_only ||
441 (c.static_mapping_only && c.connection_tracking));
443 nm->static_mapping_only = c.static_mapping_only;
444 nm->static_mapping_connection_tracking = c.connection_tracking;
445 nm->out2in_dpo = c.out2in_dpo;
446 nm->forwarding_enabled = 0;
447 nm->mss_clamping = 0;
449 nm->max_users_per_thread = c.users;
450 nm->max_translations_per_thread = c.sessions;
451 nm->max_translations_per_user =
452 c.user_sessions ? c.user_sessions : nm->max_translations_per_thread;
454 nm->inside_vrf_id = c.inside_vrf;
455 nm->inside_fib_index = fib_table_find_or_create_and_lock (
456 FIB_PROTOCOL_IP4, c.inside_vrf, nm->fib_src_hi);
458 nm->outside_vrf_id = c.outside_vrf;
459 nm->outside_fib_index = fib_table_find_or_create_and_lock (
460 FIB_PROTOCOL_IP4, c.outside_vrf, nm->fib_src_hi);
462 nat_reset_timeouts (&nm->timeouts);
463 nat44_ei_db_init (nm->translations, nm->translation_buckets,
465 nat44_ei_set_alloc_default ();
467 // TODO: zero simple counter for all counters missing
469 vlib_zero_simple_counter (&nm->total_users, 0);
470 vlib_zero_simple_counter (&nm->total_sessions, 0);
471 vlib_zero_simple_counter (&nm->user_limit_reached, 0);
480 nat44_ei_addresses_free (nat44_ei_address_t **addresses)
482 nat44_ei_address_t *ap;
483 vec_foreach (ap, *addresses)
485 #define _(N, i, n, s) vec_free (ap->busy_##n##_ports_per_thread);
489 vec_free (*addresses);
494 nat44_ei_interface_add_del (u32 sw_if_index, u8 is_inside, int is_del)
496 const char *feature_name, *del_feature_name;
497 nat44_ei_main_t *nm = &nat44_ei_main;
498 nat44_ei_interface_t *i;
499 nat44_ei_address_t *ap;
500 nat44_ei_static_mapping_t *m;
501 nat44_ei_outside_fib_t *outside_fib;
503 fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4, sw_if_index);
507 if (nm->out2in_dpo && !is_inside)
509 nat44_ei_log_err ("error unsupported");
510 return VNET_API_ERROR_UNSUPPORTED;
513 pool_foreach (i, nm->output_feature_interfaces)
515 if (i->sw_if_index == sw_if_index)
517 nat44_ei_log_err ("error interface already configured");
518 return VNET_API_ERROR_VALUE_EXIST;
522 if (nm->static_mapping_only && !(nm->static_mapping_connection_tracking))
523 feature_name = is_inside ? "nat44-ei-in2out-fast" : "nat44-ei-out2in-fast";
526 if (nm->num_workers > 1)
527 feature_name = is_inside ? "nat44-ei-in2out-worker-handoff" :
528 "nat44-ei-out2in-worker-handoff";
530 feature_name = is_inside ? "nat44-ei-in2out" : "nat44-ei-out2in";
533 if (nm->fq_in2out_index == ~0 && nm->num_workers > 1)
534 nm->fq_in2out_index = vlib_frame_queue_main_init (nm->in2out_node_index,
535 nm->frame_queue_nelts);
537 if (nm->fq_out2in_index == ~0 && nm->num_workers > 1)
538 nm->fq_out2in_index = vlib_frame_queue_main_init (nm->out2in_node_index,
539 nm->frame_queue_nelts);
543 vec_foreach (outside_fib, nm->outside_fibs)
545 if (outside_fib->fib_index == fib_index)
549 outside_fib->refcount--;
550 if (!outside_fib->refcount)
551 vec_del1 (nm->outside_fibs,
552 outside_fib - nm->outside_fibs);
555 outside_fib->refcount++;
561 vec_add2 (nm->outside_fibs, outside_fib, 1);
562 outside_fib->refcount = 1;
563 outside_fib->fib_index = fib_index;
568 pool_foreach (i, nm->interfaces)
570 if (i->sw_if_index == sw_if_index)
574 if (nat44_ei_interface_is_inside (i) &&
575 nat44_ei_interface_is_outside (i))
578 i->flags &= ~NAT44_EI_INTERFACE_FLAG_IS_INSIDE;
580 i->flags &= ~NAT44_EI_INTERFACE_FLAG_IS_OUTSIDE;
582 if (nm->num_workers > 1)
584 del_feature_name = "nat44-ei-handoff-classify";
586 "del_feature_name = nat44-ei-handoff-classify");
587 feature_name = !is_inside ?
588 "nat44-ei-in2out-worker-handoff" :
589 "nat44-ei-out2in-worker-handoff";
593 del_feature_name = "nat44-ei-classify";
594 clib_warning ("del_feature_name = nat44-ei-classify");
596 !is_inside ? "nat44-ei-in2out" : "nat44-ei-out2in";
600 ip4_sv_reass_enable_disable_with_refcnt (sw_if_index, 0);
603 rv = vnet_feature_enable_disable (
604 "ip4-unicast", del_feature_name, sw_if_index, 0, 0, 0);
607 rv = vnet_feature_enable_disable (
608 "ip4-unicast", feature_name, sw_if_index, 1, 0, 0);
613 rv = vnet_feature_enable_disable ("ip4-local",
614 "nat44-ei-hairpinning",
615 sw_if_index, 1, 0, 0);
623 ip4_sv_reass_enable_disable_with_refcnt (sw_if_index, 0);
626 rv = vnet_feature_enable_disable (
627 "ip4-unicast", feature_name, sw_if_index, 0, 0, 0);
630 pool_put (nm->interfaces, i);
633 rv = vnet_feature_enable_disable ("ip4-local",
634 "nat44-ei-hairpinning",
635 sw_if_index, 0, 0, 0);
643 if ((nat44_ei_interface_is_inside (i) && is_inside) ||
644 (nat44_ei_interface_is_outside (i) && !is_inside))
647 if (nm->num_workers > 1)
649 del_feature_name = !is_inside ?
650 "nat44-ei-in2out-worker-handoff" :
651 "nat44-ei-out2in-worker-handoff";
652 feature_name = "nat44-ei-handoff-classify";
653 clib_warning ("feature_name = nat44-ei-handoff-classify");
658 !is_inside ? "nat44-ei-in2out" : "nat44-ei-out2in";
659 feature_name = "nat44-ei-classify";
660 clib_warning ("feature_name = nat44-ei-classify");
664 ip4_sv_reass_enable_disable_with_refcnt (sw_if_index, 1);
667 rv = vnet_feature_enable_disable (
668 "ip4-unicast", del_feature_name, sw_if_index, 0, 0, 0);
671 rv = vnet_feature_enable_disable ("ip4-unicast", feature_name,
672 sw_if_index, 1, 0, 0);
677 rv = vnet_feature_enable_disable (
678 "ip4-local", "nat44-ei-hairpinning", sw_if_index, 0, 0, 0);
691 nat44_ei_log_err ("error interface couldn't be found");
692 return VNET_API_ERROR_NO_SUCH_ENTRY;
695 pool_get (nm->interfaces, i);
696 i->sw_if_index = sw_if_index;
698 nat_validate_interface_counters (nm, sw_if_index);
700 int rv = vnet_feature_enable_disable ("ip4-unicast", feature_name,
701 sw_if_index, 1, 0, 0);
705 rv = ip4_sv_reass_enable_disable_with_refcnt (sw_if_index, 1);
709 if (is_inside && !nm->out2in_dpo)
711 rv = vnet_feature_enable_disable ("ip4-local", "nat44-ei-hairpinning",
712 sw_if_index, 1, 0, 0);
720 i->flags |= NAT44_EI_INTERFACE_FLAG_IS_INSIDE;
724 i->flags |= NAT44_EI_INTERFACE_FLAG_IS_OUTSIDE;
726 /* Add/delete external addresses to FIB */
728 vec_foreach (ap, nm->addresses)
729 nat44_ei_add_del_addr_to_fib (&ap->addr, 32, sw_if_index, !is_del);
731 pool_foreach (m, nm->static_mappings)
733 if (!(nat44_ei_is_addr_only_static_mapping (m)) ||
734 (m->local_addr.as_u32 == m->external_addr.as_u32))
737 nat44_ei_add_del_addr_to_fib (&m->external_addr, 32, sw_if_index,
745 nat44_ei_interface_add_del_output_feature (u32 sw_if_index, u8 is_inside,
748 nat44_ei_main_t *nm = &nat44_ei_main;
749 nat44_ei_interface_t *i;
750 nat44_ei_address_t *ap;
751 nat44_ei_static_mapping_t *m;
752 nat44_ei_outside_fib_t *outside_fib;
754 fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4, sw_if_index);
758 if (nm->static_mapping_only && !(nm->static_mapping_connection_tracking))
760 nat44_ei_log_err ("error unsupported");
761 return VNET_API_ERROR_UNSUPPORTED;
764 pool_foreach (i, nm->interfaces)
766 if (i->sw_if_index == sw_if_index)
768 nat44_ei_log_err ("error interface already configured");
769 return VNET_API_ERROR_VALUE_EXIST;
775 vec_foreach (outside_fib, nm->outside_fibs)
777 if (outside_fib->fib_index == fib_index)
781 outside_fib->refcount--;
782 if (!outside_fib->refcount)
783 vec_del1 (nm->outside_fibs,
784 outside_fib - nm->outside_fibs);
787 outside_fib->refcount++;
793 vec_add2 (nm->outside_fibs, outside_fib, 1);
794 outside_fib->refcount = 1;
795 outside_fib->fib_index = fib_index;
802 int rv = ip4_sv_reass_enable_disable_with_refcnt (sw_if_index, !is_del);
806 ip4_sv_reass_output_enable_disable_with_refcnt (sw_if_index, !is_del);
809 rv = vnet_feature_enable_disable ("ip4-unicast", "nat44-ei-hairpin-dst",
810 sw_if_index, !is_del, 0, 0);
813 rv = vnet_feature_enable_disable ("ip4-output", "nat44-ei-hairpin-src",
814 sw_if_index, !is_del, 0, 0);
820 if (nm->num_workers > 1)
822 int rv = ip4_sv_reass_enable_disable_with_refcnt (sw_if_index, !is_del);
826 ip4_sv_reass_output_enable_disable_with_refcnt (sw_if_index, !is_del);
829 rv = vnet_feature_enable_disable ("ip4-unicast",
830 "nat44-ei-out2in-worker-handoff",
831 sw_if_index, !is_del, 0, 0);
834 rv = vnet_feature_enable_disable (
835 "ip4-output", "nat44-ei-in2out-output-worker-handoff", sw_if_index,
842 int rv = ip4_sv_reass_enable_disable_with_refcnt (sw_if_index, !is_del);
846 ip4_sv_reass_output_enable_disable_with_refcnt (sw_if_index, !is_del);
849 rv = vnet_feature_enable_disable ("ip4-unicast", "nat44-ei-out2in",
850 sw_if_index, !is_del, 0, 0);
853 rv = vnet_feature_enable_disable ("ip4-output", "nat44-ei-in2out-output",
854 sw_if_index, !is_del, 0, 0);
860 if (nm->fq_in2out_output_index == ~0 && nm->num_workers > 1)
861 nm->fq_in2out_output_index =
862 vlib_frame_queue_main_init (nm->in2out_output_node_index, 0);
864 if (nm->fq_out2in_index == ~0 && nm->num_workers > 1)
865 nm->fq_out2in_index =
866 vlib_frame_queue_main_init (nm->out2in_node_index, 0);
868 pool_foreach (i, nm->output_feature_interfaces)
870 if (i->sw_if_index == sw_if_index)
873 pool_put (nm->output_feature_interfaces, i);
875 return VNET_API_ERROR_VALUE_EXIST;
883 nat44_ei_log_err ("error interface couldn't be found");
884 return VNET_API_ERROR_NO_SUCH_ENTRY;
887 pool_get (nm->output_feature_interfaces, i);
888 i->sw_if_index = sw_if_index;
890 nat_validate_interface_counters (nm, sw_if_index);
892 i->flags |= NAT44_EI_INTERFACE_FLAG_IS_INSIDE;
894 i->flags |= NAT44_EI_INTERFACE_FLAG_IS_OUTSIDE;
896 /* Add/delete external addresses to FIB */
901 vec_foreach (ap, nm->addresses)
902 nat44_ei_add_del_addr_to_fib (&ap->addr, 32, sw_if_index, !is_del);
904 pool_foreach (m, nm->static_mappings)
906 if (!((nat44_ei_is_addr_only_static_mapping (m))) ||
907 (m->local_addr.as_u32 == m->external_addr.as_u32))
910 nat44_ei_add_del_addr_to_fib (&m->external_addr, 32, sw_if_index,
918 nat44_ei_plugin_disable ()
920 nat44_ei_main_t *nm = &nat44_ei_main;
921 nat44_ei_interface_t *i, *vec;
924 // first unregister all nodes from interfaces
925 vec = vec_dup (nm->interfaces);
928 if (nat44_ei_interface_is_inside (i))
929 error = nat44_ei_interface_add_del (i->sw_if_index, 1, 1);
930 if (nat44_ei_interface_is_outside (i))
931 error = nat44_ei_interface_add_del (i->sw_if_index, 0, 1);
935 nat44_ei_log_err ("error occurred while removing interface %u",
942 vec = vec_dup (nm->output_feature_interfaces);
945 if (nat44_ei_interface_is_inside (i))
947 nat44_ei_interface_add_del_output_feature (i->sw_if_index, 1, 1);
948 if (nat44_ei_interface_is_outside (i))
950 nat44_ei_interface_add_del_output_feature (i->sw_if_index, 0, 1);
954 nat44_ei_log_err ("error occurred while removing interface %u",
959 nm->output_feature_interfaces = 0;
964 nat44_ei_addresses_free (&nm->addresses);
966 vec_free (nm->to_resolve);
967 vec_free (nm->auto_add_sw_if_indices);
970 nm->auto_add_sw_if_indices = 0;
972 nm->forwarding_enabled = 0;
975 clib_memset (&nm->rconfig, 0, sizeof (nm->rconfig));
981 nat44_ei_set_outside_address_and_port (nat44_ei_address_t *addresses,
982 u32 thread_index, ip4_address_t addr,
983 u16 port, nat_protocol_t protocol)
985 nat44_ei_main_t *nm = &nat44_ei_main;
986 nat44_ei_address_t *a = 0;
988 u16 port_host_byte_order = clib_net_to_host_u16 (port);
990 for (address_index = 0; address_index < vec_len (addresses); address_index++)
992 if (addresses[address_index].addr.as_u32 != addr.as_u32)
995 a = addresses + address_index;
998 #define _(N, j, n, s) \
999 case NAT_PROTOCOL_##N: \
1000 if (a->busy_##n##_port_refcounts[port_host_byte_order]) \
1001 return VNET_API_ERROR_INSTANCE_IN_USE; \
1002 ++a->busy_##n##_port_refcounts[port_host_byte_order]; \
1003 a->busy_##n##_ports_per_thread[thread_index]++; \
1004 a->busy_##n##_ports++; \
1006 foreach_nat_protocol
1008 default : nat_elog_info (nm, "unknown protocol");
1013 return VNET_API_ERROR_NO_SUCH_ENTRY;
1017 nat44_ei_add_del_address_dpo (ip4_address_t addr, u8 is_add)
1019 nat44_ei_main_t *nm = &nat44_ei_main;
1020 dpo_id_t dpo_v4 = DPO_INVALID;
1021 fib_prefix_t pfx = {
1022 .fp_proto = FIB_PROTOCOL_IP4,
1024 .fp_addr.ip4.as_u32 = addr.as_u32,
1029 nat_dpo_create (DPO_PROTO_IP4, 0, &dpo_v4);
1030 fib_table_entry_special_dpo_add (0, &pfx, nm->fib_src_hi,
1031 FIB_ENTRY_FLAG_EXCLUSIVE, &dpo_v4);
1032 dpo_reset (&dpo_v4);
1036 fib_table_entry_special_remove (0, &pfx, nm->fib_src_hi);
1041 nat44_ei_free_outside_address_and_port (nat44_ei_address_t *addresses,
1042 u32 thread_index, ip4_address_t *addr,
1043 u16 port, nat_protocol_t protocol)
1045 nat44_ei_main_t *nm = &nat44_ei_main;
1046 nat44_ei_address_t *a;
1048 u16 port_host_byte_order = clib_net_to_host_u16 (port);
1050 for (address_index = 0; address_index < vec_len (addresses); address_index++)
1052 if (addresses[address_index].addr.as_u32 == addr->as_u32)
1056 ASSERT (address_index < vec_len (addresses));
1058 a = addresses + address_index;
1062 #define _(N, i, n, s) \
1063 case NAT_PROTOCOL_##N: \
1064 ASSERT (a->busy_##n##_port_refcounts[port_host_byte_order] >= 1); \
1065 --a->busy_##n##_port_refcounts[port_host_byte_order]; \
1066 a->busy_##n##_ports--; \
1067 a->busy_##n##_ports_per_thread[thread_index]--; \
1069 foreach_nat_protocol
1071 default : nat_elog_info (nm, "unknown protocol");
1077 nat44_ei_free_session_data_v2 (nat44_ei_main_t *nm, nat44_ei_session_t *s,
1078 u32 thread_index, u8 is_ha)
1080 clib_bihash_kv_8_8_t kv;
1082 /* session lookup tables */
1083 init_nat_i2o_k (&kv, s);
1084 if (clib_bihash_add_del_8_8 (&nm->in2out, &kv, 0))
1085 nat_elog_warn (nm, "in2out key del failed");
1086 init_nat_o2i_k (&kv, s);
1087 if (clib_bihash_add_del_8_8 (&nm->out2in, &kv, 0))
1088 nat_elog_warn (nm, "out2in key del failed");
1091 nat_syslog_nat44_apmdel (s->user_index, s->in2out.fib_index,
1092 &s->in2out.addr, s->in2out.port, &s->out2in.addr,
1093 s->out2in.port, s->nat_proto);
1095 if (nat44_ei_is_unk_proto_session (s))
1101 nat_ipfix_logging_nat44_ses_delete (
1102 thread_index, s->in2out.addr.as_u32, s->out2in.addr.as_u32,
1103 s->nat_proto, s->in2out.port, s->out2in.port, s->in2out.fib_index);
1105 nat_ha_sdel (&s->out2in.addr, s->out2in.port, &s->ext_host_addr,
1106 s->ext_host_port, s->nat_proto, s->out2in.fib_index,
1110 if (nat44_ei_is_session_static (s))
1113 nat44_ei_free_outside_address_and_port (nm->addresses, thread_index,
1114 &s->out2in.addr, s->out2in.port,
1119 nat44_ei_user_get_or_create (nat44_ei_main_t *nm, ip4_address_t *addr,
1120 u32 fib_index, u32 thread_index)
1122 nat44_ei_user_t *u = 0;
1123 nat44_ei_user_key_t user_key;
1124 clib_bihash_kv_8_8_t kv, value;
1125 nat44_ei_main_per_thread_data_t *tnm = &nm->per_thread_data[thread_index];
1126 dlist_elt_t *per_user_list_head_elt;
1128 user_key.addr.as_u32 = addr->as_u32;
1129 user_key.fib_index = fib_index;
1130 kv.key = user_key.as_u64;
1132 /* Ever heard of the "user" = src ip4 address before? */
1133 if (clib_bihash_search_8_8 (&tnm->user_hash, &kv, &value))
1135 if (pool_elts (tnm->users) >= nm->max_users_per_thread)
1137 vlib_increment_simple_counter (&nm->user_limit_reached, thread_index,
1139 nat_elog_warn (nm, "maximum user limit reached");
1142 /* no, make a new one */
1143 pool_get (tnm->users, u);
1144 clib_memset (u, 0, sizeof (*u));
1146 u->addr.as_u32 = addr->as_u32;
1147 u->fib_index = fib_index;
1149 pool_get (tnm->list_pool, per_user_list_head_elt);
1151 u->sessions_per_user_list_head_index =
1152 per_user_list_head_elt - tnm->list_pool;
1154 clib_dlist_init (tnm->list_pool, u->sessions_per_user_list_head_index);
1156 kv.value = u - tnm->users;
1159 if (clib_bihash_add_del_8_8 (&tnm->user_hash, &kv, 1))
1161 nat_elog_warn (nm, "user_hash key add failed");
1162 nat44_ei_delete_user_with_no_session (nm, u, thread_index);
1166 vlib_set_simple_counter (&nm->total_users, thread_index, 0,
1167 pool_elts (tnm->users));
1171 u = pool_elt_at_index (tnm->users, value.value);
1177 nat44_ei_session_t *
1178 nat44_ei_session_alloc_or_recycle (nat44_ei_main_t *nm, nat44_ei_user_t *u,
1179 u32 thread_index, f64 now)
1181 nat44_ei_session_t *s;
1182 nat44_ei_main_per_thread_data_t *tnm = &nm->per_thread_data[thread_index];
1183 u32 oldest_per_user_translation_list_index, session_index;
1184 dlist_elt_t *oldest_per_user_translation_list_elt;
1185 dlist_elt_t *per_user_translation_list_elt;
1187 /* Over quota? Recycle the least recently used translation */
1188 if ((u->nsessions + u->nstaticsessions) >= nm->max_translations_per_user)
1190 oldest_per_user_translation_list_index = clib_dlist_remove_head (
1191 tnm->list_pool, u->sessions_per_user_list_head_index);
1193 ASSERT (oldest_per_user_translation_list_index != ~0);
1195 /* Add it back to the end of the LRU list */
1196 clib_dlist_addtail (tnm->list_pool, u->sessions_per_user_list_head_index,
1197 oldest_per_user_translation_list_index);
1198 /* Get the list element */
1199 oldest_per_user_translation_list_elt = pool_elt_at_index (
1200 tnm->list_pool, oldest_per_user_translation_list_index);
1202 /* Get the session index from the list element */
1203 session_index = oldest_per_user_translation_list_elt->value;
1205 /* Get the session */
1206 s = pool_elt_at_index (tnm->sessions, session_index);
1208 nat44_ei_free_session_data_v2 (nm, s, thread_index, 0);
1209 if (nat44_ei_is_session_static (s))
1210 u->nstaticsessions--;
1217 s->ext_host_addr.as_u32 = 0;
1218 s->ext_host_port = 0;
1219 s->ext_host_nat_addr.as_u32 = 0;
1220 s->ext_host_nat_port = 0;
1224 pool_get (tnm->sessions, s);
1225 clib_memset (s, 0, sizeof (*s));
1227 /* Create list elts */
1228 pool_get (tnm->list_pool, per_user_translation_list_elt);
1229 clib_dlist_init (tnm->list_pool,
1230 per_user_translation_list_elt - tnm->list_pool);
1232 per_user_translation_list_elt->value = s - tnm->sessions;
1233 s->per_user_index = per_user_translation_list_elt - tnm->list_pool;
1234 s->per_user_list_head_index = u->sessions_per_user_list_head_index;
1236 clib_dlist_addtail (tnm->list_pool, s->per_user_list_head_index,
1237 per_user_translation_list_elt - tnm->list_pool);
1239 s->user_index = u - tnm->users;
1240 vlib_set_simple_counter (&nm->total_sessions, thread_index, 0,
1241 pool_elts (tnm->sessions));
1244 s->ha_last_refreshed = now;
1250 nat44_ei_free_session_data (nat44_ei_main_t *nm, nat44_ei_session_t *s,
1251 u32 thread_index, u8 is_ha)
1253 clib_bihash_kv_8_8_t kv;
1255 init_nat_i2o_k (&kv, s);
1256 if (clib_bihash_add_del_8_8 (&nm->in2out, &kv, 0))
1257 nat_elog_warn (nm, "in2out key del failed");
1259 init_nat_o2i_k (&kv, s);
1260 if (clib_bihash_add_del_8_8 (&nm->out2in, &kv, 0))
1261 nat_elog_warn (nm, "out2in key del failed");
1265 nat_syslog_nat44_apmdel (s->user_index, s->in2out.fib_index,
1266 &s->in2out.addr, s->in2out.port,
1267 &s->out2in.addr, s->out2in.port, s->nat_proto);
1269 nat_ipfix_logging_nat44_ses_delete (
1270 thread_index, s->in2out.addr.as_u32, s->out2in.addr.as_u32,
1271 s->nat_proto, s->in2out.port, s->out2in.port, s->in2out.fib_index);
1273 nat_ha_sdel (&s->out2in.addr, s->out2in.port, &s->ext_host_addr,
1274 s->ext_host_port, s->nat_proto, s->out2in.fib_index,
1278 if (nat44_ei_is_session_static (s))
1281 nat44_ei_free_outside_address_and_port (nm->addresses, thread_index,
1282 &s->out2in.addr, s->out2in.port,
1286 static_always_inline void
1287 nat44_ei_user_del_sessions (nat44_ei_user_t *u, u32 thread_index)
1290 nat44_ei_session_t *s;
1292 nat44_ei_main_t *nm = &nat44_ei_main;
1293 nat44_ei_main_per_thread_data_t *tnm = &nm->per_thread_data[thread_index];
1297 pool_elt_at_index (tnm->list_pool, u->sessions_per_user_list_head_index);
1298 // get first element
1299 elt = pool_elt_at_index (tnm->list_pool, elt->next);
1301 while (elt->value != ~0)
1303 s = pool_elt_at_index (tnm->sessions, elt->value);
1304 elt = pool_elt_at_index (tnm->list_pool, elt->next);
1306 nat44_ei_free_session_data (nm, s, thread_index, 0);
1307 nat44_ei_delete_session (nm, s, thread_index);
1312 nat44_ei_user_del (ip4_address_t *addr, u32 fib_index)
1316 nat44_ei_main_t *nm = &nat44_ei_main;
1317 nat44_ei_main_per_thread_data_t *tnm;
1319 nat44_ei_user_key_t user_key;
1320 clib_bihash_kv_8_8_t kv, value;
1322 user_key.addr.as_u32 = addr->as_u32;
1323 user_key.fib_index = fib_index;
1324 kv.key = user_key.as_u64;
1326 if (nm->num_workers > 1)
1328 vec_foreach (tnm, nm->per_thread_data)
1330 if (!clib_bihash_search_8_8 (&tnm->user_hash, &kv, &value))
1332 nat44_ei_user_del_sessions (
1333 pool_elt_at_index (tnm->users, value.value),
1342 tnm = vec_elt_at_index (nm->per_thread_data, nm->num_workers);
1343 if (!clib_bihash_search_8_8 (&tnm->user_hash, &kv, &value))
1345 nat44_ei_user_del_sessions (
1346 pool_elt_at_index (tnm->users, value.value), tnm->thread_index);
1354 nat44_ei_static_mapping_del_sessions (nat44_ei_main_t *nm,
1355 nat44_ei_main_per_thread_data_t *tnm,
1356 nat44_ei_user_key_t u_key, int addr_only,
1357 ip4_address_t e_addr, u16 e_port)
1359 clib_bihash_kv_8_8_t kv, value;
1360 kv.key = u_key.as_u64;
1362 dlist_elt_t *head, *elt;
1364 nat44_ei_session_t *s;
1365 u32 elt_index, head_index, ses_index;
1367 if (!clib_bihash_search_8_8 (&tnm->user_hash, &kv, &value))
1369 user_index = value.value;
1370 u = pool_elt_at_index (tnm->users, user_index);
1371 if (u->nstaticsessions)
1373 head_index = u->sessions_per_user_list_head_index;
1374 head = pool_elt_at_index (tnm->list_pool, head_index);
1375 elt_index = head->next;
1376 elt = pool_elt_at_index (tnm->list_pool, elt_index);
1377 ses_index = elt->value;
1378 while (ses_index != ~0)
1380 s = pool_elt_at_index (tnm->sessions, ses_index);
1381 elt = pool_elt_at_index (tnm->list_pool, elt->next);
1382 ses_index = elt->value;
1386 if ((s->out2in.addr.as_u32 != e_addr.as_u32) ||
1387 (s->out2in.port != e_port))
1391 if (!nat44_ei_is_session_static (s))
1394 nat44_ei_free_session_data_v2 (nm, s, tnm - nm->per_thread_data,
1396 nat44_ei_delete_session (nm, s, tnm - nm->per_thread_data);
1406 nat44_ei_get_in2out_worker_index (ip4_header_t *ip0, u32 rx_fib_index0,
1409 nat44_ei_main_t *nm = &nat44_ei_main;
1410 u32 next_worker_index = 0;
1413 next_worker_index = nm->first_worker_index;
1414 hash = ip0->src_address.as_u32 + (ip0->src_address.as_u32 >> 8) +
1415 (ip0->src_address.as_u32 >> 16) + (ip0->src_address.as_u32 >> 24);
1417 if (PREDICT_TRUE (is_pow2 (_vec_len (nm->workers))))
1418 next_worker_index += nm->workers[hash & (_vec_len (nm->workers) - 1)];
1420 next_worker_index += nm->workers[hash % _vec_len (nm->workers)];
1422 return next_worker_index;
1426 nat44_ei_get_out2in_worker_index (vlib_buffer_t *b, ip4_header_t *ip0,
1427 u32 rx_fib_index0, u8 is_output)
1429 nat44_ei_main_t *nm = &nat44_ei_main;
1432 clib_bihash_kv_8_8_t kv, value;
1433 nat44_ei_static_mapping_t *m;
1435 u32 next_worker_index = 0;
1437 /* first try static mappings without port */
1438 if (PREDICT_FALSE (pool_elts (nm->static_mappings)))
1440 init_nat_k (&kv, ip0->dst_address, 0, rx_fib_index0, 0);
1441 if (!clib_bihash_search_8_8 (&nm->static_mapping_by_external, &kv,
1444 m = pool_elt_at_index (nm->static_mappings, value.value);
1445 return m->workers[0];
1449 proto = ip_proto_to_nat_proto (ip0->protocol);
1450 udp = ip4_next_header (ip0);
1451 port = vnet_buffer (b)->ip.reass.l4_dst_port;
1453 /* unknown protocol */
1454 if (PREDICT_FALSE (proto == NAT_PROTOCOL_OTHER))
1456 /* use current thread */
1457 return vlib_get_thread_index ();
1460 if (PREDICT_FALSE (ip0->protocol == IP_PROTOCOL_ICMP))
1462 icmp46_header_t *icmp = (icmp46_header_t *) udp;
1463 icmp_echo_header_t *echo = (icmp_echo_header_t *) (icmp + 1);
1464 if (!icmp_type_is_error_message (
1465 vnet_buffer (b)->ip.reass.icmp_type_or_tcp_flags))
1466 port = vnet_buffer (b)->ip.reass.l4_src_port;
1469 /* if error message, then it's not fragmented and we can access it */
1470 ip4_header_t *inner_ip = (ip4_header_t *) (echo + 1);
1471 proto = ip_proto_to_nat_proto (inner_ip->protocol);
1472 void *l4_header = ip4_next_header (inner_ip);
1475 case NAT_PROTOCOL_ICMP:
1476 icmp = (icmp46_header_t *) l4_header;
1477 echo = (icmp_echo_header_t *) (icmp + 1);
1478 port = echo->identifier;
1480 case NAT_PROTOCOL_UDP:
1481 case NAT_PROTOCOL_TCP:
1482 port = ((tcp_udp_header_t *) l4_header)->src_port;
1485 return vlib_get_thread_index ();
1490 /* try static mappings with port */
1491 if (PREDICT_FALSE (pool_elts (nm->static_mappings)))
1493 init_nat_k (&kv, ip0->dst_address, port, rx_fib_index0, proto);
1494 if (!clib_bihash_search_8_8 (&nm->static_mapping_by_external, &kv,
1497 m = pool_elt_at_index (nm->static_mappings, value.value);
1498 return m->workers[0];
1502 /* worker by outside port */
1503 next_worker_index = nm->first_worker_index;
1504 next_worker_index +=
1505 nm->workers[(clib_net_to_host_u16 (port) - 1024) / nm->port_per_thread];
1506 return next_worker_index;
1510 nat44_ei_alloc_default_cb (nat44_ei_address_t *addresses, u32 fib_index,
1511 u32 thread_index, nat_protocol_t proto,
1512 ip4_address_t s_addr, ip4_address_t *addr,
1513 u16 *port, u16 port_per_thread,
1514 u32 snat_thread_index)
1516 nat44_ei_main_t *nm = &nat44_ei_main;
1517 nat44_ei_address_t *a, *ga = 0;
1521 if (vec_len (addresses) > 0)
1524 int s_addr_offset = s_addr.as_u32 % vec_len (addresses);
1526 for (i = s_addr_offset; i < vec_len (addresses); ++i)
1531 #define _(N, j, n, s) \
1532 case NAT_PROTOCOL_##N: \
1533 if (a->busy_##n##_ports_per_thread[thread_index] < port_per_thread) \
1535 if (a->fib_index == fib_index) \
1539 portnum = (port_per_thread * snat_thread_index) + \
1540 nat_random_port (&nm->random_seed, 0, \
1541 port_per_thread - 1) + \
1543 if (a->busy_##n##_port_refcounts[portnum]) \
1545 --a->busy_##n##_port_refcounts[portnum]; \
1546 a->busy_##n##_ports_per_thread[thread_index]++; \
1547 a->busy_##n##_ports++; \
1549 *port = clib_host_to_net_u16 (portnum); \
1553 else if (a->fib_index == ~0) \
1559 foreach_nat_protocol;
1561 nat_elog_info (nm, "unknown protocol");
1566 for (i = 0; i < s_addr_offset; ++i)
1571 foreach_nat_protocol;
1573 nat_elog_info (nm, "unknown protocol");
1580 // fake fib index to reuse macro
1584 foreach_nat_protocol;
1585 default : nat_elog_info (nm, "unknown protocol");
1593 /* Totally out of translations to use... */
1594 nat_ipfix_logging_addresses_exhausted (thread_index, 0);
1599 nat44_ei_alloc_range_cb (nat44_ei_address_t *addresses, u32 fib_index,
1600 u32 thread_index, nat_protocol_t proto,
1601 ip4_address_t s_addr, ip4_address_t *addr, u16 *port,
1602 u16 port_per_thread, u32 snat_thread_index)
1604 nat44_ei_main_t *nm = &nat44_ei_main;
1605 nat44_ei_address_t *a = addresses;
1608 ports = nm->end_port - nm->start_port + 1;
1610 if (!vec_len (addresses))
1615 #define _(N, i, n, s) \
1616 case NAT_PROTOCOL_##N: \
1617 if (a->busy_##n##_ports < ports) \
1621 portnum = nat_random_port (&nm->random_seed, nm->start_port, \
1623 if (a->busy_##n##_port_refcounts[portnum]) \
1625 ++a->busy_##n##_port_refcounts[portnum]; \
1626 a->busy_##n##_ports++; \
1628 *port = clib_host_to_net_u16 (portnum); \
1633 foreach_nat_protocol
1635 default : nat_elog_info (nm, "unknown protocol");
1640 /* Totally out of translations to use... */
1641 nat_ipfix_logging_addresses_exhausted (thread_index, 0);
1646 nat44_ei_alloc_mape_cb (nat44_ei_address_t *addresses, u32 fib_index,
1647 u32 thread_index, nat_protocol_t proto,
1648 ip4_address_t s_addr, ip4_address_t *addr, u16 *port,
1649 u16 port_per_thread, u32 snat_thread_index)
1651 nat44_ei_main_t *nm = &nat44_ei_main;
1652 nat44_ei_address_t *a = addresses;
1653 u16 m, ports, portnum, A, j;
1654 m = 16 - (nm->psid_offset + nm->psid_length);
1655 ports = (1 << (16 - nm->psid_length)) - (1 << m);
1657 if (!vec_len (addresses))
1662 #define _(N, i, n, s) \
1663 case NAT_PROTOCOL_##N: \
1664 if (a->busy_##n##_ports < ports) \
1668 A = nat_random_port (&nm->random_seed, 1, \
1669 pow2_mask (nm->psid_offset)); \
1670 j = nat_random_port (&nm->random_seed, 0, pow2_mask (m)); \
1671 portnum = A | (nm->psid << nm->psid_offset) | (j << (16 - m)); \
1672 if (a->busy_##n##_port_refcounts[portnum]) \
1674 ++a->busy_##n##_port_refcounts[portnum]; \
1675 a->busy_##n##_ports++; \
1677 *port = clib_host_to_net_u16 (portnum); \
1682 foreach_nat_protocol
1684 default : nat_elog_info (nm, "unknown protocol");
1689 /* Totally out of translations to use... */
1690 nat_ipfix_logging_addresses_exhausted (thread_index, 0);
1695 nat44_ei_set_alloc_default ()
1697 nat44_ei_main_t *nm = &nat44_ei_main;
1699 nm->addr_and_port_alloc_alg = NAT44_EI_ADDR_AND_PORT_ALLOC_ALG_DEFAULT;
1700 nm->alloc_addr_and_port = nat44_ei_alloc_default_cb;
1704 nat44_ei_set_alloc_range (u16 start_port, u16 end_port)
1706 nat44_ei_main_t *nm = &nat44_ei_main;
1708 nm->addr_and_port_alloc_alg = NAT44_EI_ADDR_AND_PORT_ALLOC_ALG_RANGE;
1709 nm->alloc_addr_and_port = nat44_ei_alloc_range_cb;
1710 nm->start_port = start_port;
1711 nm->end_port = end_port;
1715 nat44_ei_set_alloc_mape (u16 psid, u16 psid_offset, u16 psid_length)
1717 nat44_ei_main_t *nm = &nat44_ei_main;
1719 nm->addr_and_port_alloc_alg = NAT44_EI_ADDR_AND_PORT_ALLOC_ALG_MAPE;
1720 nm->alloc_addr_and_port = nat44_ei_alloc_mape_cb;
1722 nm->psid_offset = psid_offset;
1723 nm->psid_length = psid_length;
1727 nat44_ei_add_static_mapping_when_resolved (ip4_address_t l_addr, u16 l_port,
1728 u16 e_port, nat_protocol_t proto,
1729 u32 sw_if_index, u32 vrf_id,
1730 int addr_only, int identity_nat,
1733 nat44_ei_main_t *nm = &nat44_ei_main;
1734 nat44_ei_static_map_resolve_t *rp;
1736 vec_add2 (nm->to_resolve, rp, 1);
1737 clib_memset (rp, 0, sizeof (*rp));
1739 rp->l_addr.as_u32 = l_addr.as_u32;
1740 rp->l_port = l_port;
1741 rp->e_port = e_port;
1742 rp->sw_if_index = sw_if_index;
1743 rp->vrf_id = vrf_id;
1745 rp->addr_only = addr_only;
1746 rp->identity_nat = identity_nat;
1747 rp->tag = vec_dup (tag);
1751 nat44_ei_delete_session (nat44_ei_main_t *nm, nat44_ei_session_t *ses,
1754 nat44_ei_main_per_thread_data_t *tnm =
1755 vec_elt_at_index (nm->per_thread_data, thread_index);
1756 clib_bihash_kv_8_8_t kv, value;
1758 const nat44_ei_user_key_t u_key = { .addr = ses->in2out.addr,
1759 .fib_index = ses->in2out.fib_index };
1760 const u8 u_static = nat44_ei_is_session_static (ses);
1762 clib_dlist_remove (tnm->list_pool, ses->per_user_index);
1763 pool_put_index (tnm->list_pool, ses->per_user_index);
1765 pool_put (tnm->sessions, ses);
1766 vlib_set_simple_counter (&nm->total_sessions, thread_index, 0,
1767 pool_elts (tnm->sessions));
1769 kv.key = u_key.as_u64;
1770 if (!clib_bihash_search_8_8 (&tnm->user_hash, &kv, &value))
1772 u = pool_elt_at_index (tnm->users, value.value);
1774 u->nstaticsessions--;
1778 nat44_ei_delete_user_with_no_session (nm, u, thread_index);
1783 nat44_ei_del_session (nat44_ei_main_t *nm, ip4_address_t *addr, u16 port,
1784 nat_protocol_t proto, u32 vrf_id, int is_in)
1786 nat44_ei_main_per_thread_data_t *tnm;
1787 clib_bihash_kv_8_8_t kv, value;
1789 u32 fib_index = fib_table_find (FIB_PROTOCOL_IP4, vrf_id);
1790 nat44_ei_session_t *s;
1791 clib_bihash_8_8_t *t;
1793 ip.dst_address.as_u32 = ip.src_address.as_u32 = addr->as_u32;
1794 if (nm->num_workers > 1)
1796 vec_elt_at_index (nm->per_thread_data,
1797 nat44_ei_get_in2out_worker_index (&ip, fib_index, 0));
1799 tnm = vec_elt_at_index (nm->per_thread_data, nm->num_workers);
1801 init_nat_k (&kv, *addr, port, fib_index, proto);
1802 t = is_in ? &nm->in2out : &nm->out2in;
1803 if (!clib_bihash_search_8_8 (t, &kv, &value))
1805 if (pool_is_free_index (tnm->sessions, value.value))
1806 return VNET_API_ERROR_UNSPECIFIED;
1808 s = pool_elt_at_index (tnm->sessions, value.value);
1809 nat44_ei_free_session_data_v2 (nm, s, tnm - nm->per_thread_data, 0);
1810 nat44_ei_delete_session (nm, s, tnm - nm->per_thread_data);
1814 return VNET_API_ERROR_NO_SUCH_ENTRY;
1818 nat44_ei_get_thread_idx_by_port (u16 e_port)
1820 nat44_ei_main_t *nm = &nat44_ei_main;
1821 u32 thread_idx = nm->num_workers;
1822 if (nm->num_workers > 1)
1824 thread_idx = nm->first_worker_index +
1825 nm->workers[(e_port - 1024) / nm->port_per_thread];
1831 nat44_ei_add_del_addr_to_fib (ip4_address_t *addr, u8 p_len, u32 sw_if_index,
1834 nat44_ei_main_t *nm = &nat44_ei_main;
1835 fib_prefix_t prefix = {
1837 .fp_proto = FIB_PROTOCOL_IP4,
1839 .ip4.as_u32 = addr->as_u32,
1842 u32 fib_index = ip4_fib_table_get_index_for_sw_if_index (sw_if_index);
1845 fib_table_entry_update_one_path (
1846 fib_index, &prefix, nm->fib_src_low,
1847 (FIB_ENTRY_FLAG_CONNECTED | FIB_ENTRY_FLAG_LOCAL |
1848 FIB_ENTRY_FLAG_EXCLUSIVE),
1849 DPO_PROTO_IP4, NULL, sw_if_index, ~0, 1, NULL, FIB_ROUTE_PATH_FLAG_NONE);
1851 fib_table_entry_delete (fib_index, &prefix, nm->fib_src_low);
1855 nat44_ei_add_del_static_mapping (ip4_address_t l_addr, ip4_address_t e_addr,
1856 u16 l_port, u16 e_port, nat_protocol_t proto,
1857 u32 sw_if_index, u32 vrf_id, u8 addr_only,
1858 u8 identity_nat, u8 *tag, u8 is_add)
1860 nat44_ei_main_t *nm = &nat44_ei_main;
1861 nat44_ei_static_mapping_t *m = 0;
1862 clib_bihash_kv_8_8_t kv, value;
1863 nat44_ei_address_t *a = 0;
1865 nat44_ei_interface_t *interface;
1866 nat44_ei_main_per_thread_data_t *tnm;
1867 nat44_ei_user_key_t u_key;
1869 dlist_elt_t *head, *elt;
1870 u32 elt_index, head_index;
1873 nat44_ei_session_t *s;
1874 nat44_ei_static_map_resolve_t *rp, *rp_match = 0;
1875 nat44_ei_lb_addr_port_t *local;
1879 if (sw_if_index != ~0)
1881 ip4_address_t *first_int_addr;
1883 for (i = 0; i < vec_len (nm->to_resolve); i++)
1885 rp = nm->to_resolve + i;
1886 if (rp->sw_if_index != sw_if_index ||
1887 rp->l_addr.as_u32 != l_addr.as_u32 || rp->vrf_id != vrf_id ||
1888 rp->addr_only != addr_only)
1893 if ((rp->l_port != l_port && rp->e_port != e_port) ||
1902 /* Might be already set... */
1903 first_int_addr = ip4_interface_first_address (
1904 nm->ip4_main, sw_if_index, 0 /* just want the address */);
1909 return VNET_API_ERROR_VALUE_EXIST;
1911 nat44_ei_add_static_mapping_when_resolved (
1912 l_addr, l_port, e_port, proto, sw_if_index, vrf_id, addr_only,
1915 /* DHCP resolution required? */
1916 if (!first_int_addr)
1919 e_addr.as_u32 = first_int_addr->as_u32;
1920 /* Identity mapping? */
1921 if (l_addr.as_u32 == 0)
1922 l_addr.as_u32 = e_addr.as_u32;
1927 return VNET_API_ERROR_NO_SUCH_ENTRY;
1929 vec_del1 (nm->to_resolve, i);
1931 if (!first_int_addr)
1934 e_addr.as_u32 = first_int_addr->as_u32;
1935 /* Identity mapping? */
1936 if (l_addr.as_u32 == 0)
1937 l_addr.as_u32 = e_addr.as_u32;
1941 init_nat_k (&kv, e_addr, addr_only ? 0 : e_port, 0, addr_only ? 0 : proto);
1942 if (!clib_bihash_search_8_8 (&nm->static_mapping_by_external, &kv, &value))
1943 m = pool_elt_at_index (nm->static_mappings, value.value);
1949 // identity mapping for second vrf
1950 if (nat44_ei_is_identity_static_mapping (m))
1952 pool_foreach (local, m->locals)
1954 if (local->vrf_id == vrf_id)
1955 return VNET_API_ERROR_VALUE_EXIST;
1957 pool_get (m->locals, local);
1958 local->vrf_id = vrf_id;
1959 local->fib_index = fib_table_find_or_create_and_lock (
1960 FIB_PROTOCOL_IP4, vrf_id, nm->fib_src_low);
1961 init_nat_kv (&kv, m->local_addr, m->local_port, local->fib_index,
1962 m->proto, 0, m - nm->static_mappings);
1963 clib_bihash_add_del_8_8 (&nm->static_mapping_by_local, &kv, 1);
1966 return VNET_API_ERROR_VALUE_EXIST;
1969 /* Convert VRF id to FIB index */
1972 fib_index = fib_table_find_or_create_and_lock (
1973 FIB_PROTOCOL_IP4, vrf_id, nm->fib_src_low);
1975 /* If not specified use inside VRF id from NAT44 plugin config */
1978 fib_index = nm->inside_fib_index;
1979 vrf_id = nm->inside_vrf_id;
1980 fib_table_lock (fib_index, FIB_PROTOCOL_IP4, nm->fib_src_low);
1985 init_nat_k (&kv, l_addr, addr_only ? 0 : l_port, fib_index,
1986 addr_only ? 0 : proto);
1987 if (!clib_bihash_search_8_8 (&nm->static_mapping_by_local, &kv,
1989 return VNET_API_ERROR_VALUE_EXIST;
1992 /* Find external address in allocated addresses and reserve port for
1993 address and port pair mapping when dynamic translations enabled */
1994 if (!(addr_only || nm->static_mapping_only))
1996 for (i = 0; i < vec_len (nm->addresses); i++)
1998 if (nm->addresses[i].addr.as_u32 == e_addr.as_u32)
2000 a = nm->addresses + i;
2001 /* External port must be unused */
2004 #define _(N, j, n, s) \
2005 case NAT_PROTOCOL_##N: \
2006 if (a->busy_##n##_port_refcounts[e_port]) \
2007 return VNET_API_ERROR_INVALID_VALUE; \
2008 ++a->busy_##n##_port_refcounts[e_port]; \
2009 if (e_port > 1024) \
2011 a->busy_##n##_ports++; \
2012 a->busy_##n##_ports_per_thread[nat44_ei_get_thread_idx_by_port ( \
2016 foreach_nat_protocol
2018 default : nat_elog_info (nm, "unknown protocol");
2019 return VNET_API_ERROR_INVALID_VALUE_2;
2024 /* External address must be allocated */
2025 if (!a && (l_addr.as_u32 != e_addr.as_u32))
2027 if (sw_if_index != ~0)
2029 for (i = 0; i < vec_len (nm->to_resolve); i++)
2031 rp = nm->to_resolve + i;
2034 if (rp->sw_if_index != sw_if_index &&
2035 rp->l_addr.as_u32 != l_addr.as_u32 &&
2036 rp->vrf_id != vrf_id && rp->l_port != l_port &&
2037 rp->e_port != e_port && rp->proto != proto)
2040 vec_del1 (nm->to_resolve, i);
2044 return VNET_API_ERROR_NO_SUCH_ENTRY;
2048 pool_get (nm->static_mappings, m);
2049 clib_memset (m, 0, sizeof (*m));
2050 m->tag = vec_dup (tag);
2051 m->local_addr = l_addr;
2052 m->external_addr = e_addr;
2055 m->flags |= NAT44_EI_STATIC_MAPPING_FLAG_ADDR_ONLY;
2058 m->local_port = l_port;
2059 m->external_port = e_port;
2065 m->flags |= NAT44_EI_STATIC_MAPPING_FLAG_IDENTITY_NAT;
2066 pool_get (m->locals, local);
2067 local->vrf_id = vrf_id;
2068 local->fib_index = fib_index;
2073 m->fib_index = fib_index;
2076 if (nm->num_workers > 1)
2079 .src_address = m->local_addr,
2081 vec_add1 (m->workers,
2082 nat44_ei_get_in2out_worker_index (&ip, m->fib_index, 0));
2083 tnm = vec_elt_at_index (nm->per_thread_data, m->workers[0]);
2086 tnm = vec_elt_at_index (nm->per_thread_data, nm->num_workers);
2088 init_nat_kv (&kv, m->local_addr, m->local_port, fib_index, m->proto, 0,
2089 m - nm->static_mappings);
2090 clib_bihash_add_del_8_8 (&nm->static_mapping_by_local, &kv, 1);
2092 init_nat_kv (&kv, m->external_addr, m->external_port, 0, m->proto, 0,
2093 m - nm->static_mappings);
2094 clib_bihash_add_del_8_8 (&nm->static_mapping_by_external, &kv, 1);
2096 /* Delete dynamic sessions matching local address (+ local port) */
2097 // TODO: based on type of NAT EI/ED
2098 if (!(nm->static_mapping_only))
2100 u_key.addr = m->local_addr;
2101 u_key.fib_index = m->fib_index;
2102 kv.key = u_key.as_u64;
2103 if (!clib_bihash_search_8_8 (&tnm->user_hash, &kv, &value))
2105 user_index = value.value;
2106 u = pool_elt_at_index (tnm->users, user_index);
2109 head_index = u->sessions_per_user_list_head_index;
2110 head = pool_elt_at_index (tnm->list_pool, head_index);
2111 elt_index = head->next;
2112 elt = pool_elt_at_index (tnm->list_pool, elt_index);
2113 ses_index = elt->value;
2114 while (ses_index != ~0)
2116 s = pool_elt_at_index (tnm->sessions, ses_index);
2117 elt = pool_elt_at_index (tnm->list_pool, elt->next);
2118 ses_index = elt->value;
2120 if (nat44_ei_is_session_static (s))
2123 if (!addr_only && s->in2out.port != m->local_port)
2126 nat44_ei_free_session_data_v2 (
2127 nm, s, tnm - nm->per_thread_data, 0);
2128 nat44_ei_delete_session (nm, s,
2129 tnm - nm->per_thread_data);
2142 if (sw_if_index != ~0)
2145 return VNET_API_ERROR_NO_SUCH_ENTRY;
2151 vrf_id = nm->inside_vrf_id;
2153 pool_foreach (local, m->locals)
2155 if (local->vrf_id == vrf_id)
2156 find = local - m->locals;
2159 return VNET_API_ERROR_NO_SUCH_ENTRY;
2161 local = pool_elt_at_index (m->locals, find);
2162 fib_index = local->fib_index;
2163 pool_put (m->locals, local);
2166 fib_index = m->fib_index;
2168 /* Free external address port */
2169 if (!(addr_only || nm->static_mapping_only))
2171 for (i = 0; i < vec_len (nm->addresses); i++)
2173 if (nm->addresses[i].addr.as_u32 == e_addr.as_u32)
2175 a = nm->addresses + i;
2178 #define _(N, j, n, s) \
2179 case NAT_PROTOCOL_##N: \
2180 --a->busy_##n##_port_refcounts[e_port]; \
2181 if (e_port > 1024) \
2183 a->busy_##n##_ports--; \
2184 a->busy_##n##_ports_per_thread[nat44_ei_get_thread_idx_by_port ( \
2188 foreach_nat_protocol
2190 default : return VNET_API_ERROR_INVALID_VALUE_2;
2197 if (nm->num_workers > 1)
2198 tnm = vec_elt_at_index (nm->per_thread_data, m->workers[0]);
2200 tnm = vec_elt_at_index (nm->per_thread_data, nm->num_workers);
2202 init_nat_k (&kv, m->local_addr, m->local_port, fib_index, m->proto);
2203 clib_bihash_add_del_8_8 (&nm->static_mapping_by_local, &kv, 0);
2205 /* Delete session(s) for static mapping if exist */
2206 if (!(nm->static_mapping_only) ||
2207 (nm->static_mapping_only && nm->static_mapping_connection_tracking))
2209 u_key.addr = m->local_addr;
2210 u_key.fib_index = fib_index;
2211 kv.key = u_key.as_u64;
2212 nat44_ei_static_mapping_del_sessions (nm, tnm, u_key, addr_only,
2216 fib_table_unlock (fib_index, FIB_PROTOCOL_IP4, nm->fib_src_low);
2217 if (pool_elts (m->locals))
2220 init_nat_k (&kv, m->external_addr, m->external_port, 0, m->proto);
2221 clib_bihash_add_del_8_8 (&nm->static_mapping_by_external, &kv, 0);
2224 vec_free (m->workers);
2225 /* Delete static mapping from pool */
2226 pool_put (nm->static_mappings, m);
2229 if (!addr_only || (l_addr.as_u32 == e_addr.as_u32))
2232 /* Add/delete external address to FIB */
2233 pool_foreach (interface, nm->interfaces)
2235 if (nat44_ei_interface_is_inside (interface) || nm->out2in_dpo)
2238 nat44_ei_add_del_addr_to_fib (&e_addr, 32, interface->sw_if_index,
2242 pool_foreach (interface, nm->output_feature_interfaces)
2244 if (nat44_ei_interface_is_inside (interface) || nm->out2in_dpo)
2247 nat44_ei_add_del_addr_to_fib (&e_addr, 32, interface->sw_if_index,
2255 nat44_ei_static_mapping_match (ip4_address_t match_addr, u16 match_port,
2256 u32 match_fib_index,
2257 nat_protocol_t match_protocol,
2258 ip4_address_t *mapping_addr, u16 *mapping_port,
2259 u32 *mapping_fib_index, u8 by_external,
2260 u8 *is_addr_only, u8 *is_identity_nat)
2262 nat44_ei_main_t *nm = &nat44_ei_main;
2263 clib_bihash_kv_8_8_t kv, value;
2264 nat44_ei_static_mapping_t *m;
2269 init_nat_k (&kv, match_addr, match_port, 0, match_protocol);
2270 if (clib_bihash_search_8_8 (&nm->static_mapping_by_external, &kv,
2273 /* Try address only mapping */
2274 init_nat_k (&kv, match_addr, 0, 0, 0);
2275 if (clib_bihash_search_8_8 (&nm->static_mapping_by_external, &kv,
2279 m = pool_elt_at_index (nm->static_mappings, value.value);
2281 *mapping_fib_index = m->fib_index;
2282 *mapping_addr = m->local_addr;
2283 port = m->local_port;
2287 init_nat_k (&kv, match_addr, match_port, match_fib_index,
2289 if (clib_bihash_search_8_8 (&nm->static_mapping_by_local, &kv, &value))
2291 /* Try address only mapping */
2292 init_nat_k (&kv, match_addr, 0, match_fib_index, 0);
2293 if (clib_bihash_search_8_8 (&nm->static_mapping_by_local, &kv,
2297 m = pool_elt_at_index (nm->static_mappings, value.value);
2299 *mapping_fib_index = nm->outside_fib_index;
2300 *mapping_addr = m->external_addr;
2301 port = m->external_port;
2304 /* Address only mapping doesn't change port */
2305 if (nat44_ei_is_addr_only_static_mapping (m))
2306 *mapping_port = match_port;
2308 *mapping_port = port;
2310 if (PREDICT_FALSE (is_addr_only != 0))
2311 *is_addr_only = nat44_ei_is_addr_only_static_mapping (m);
2313 if (PREDICT_FALSE (is_identity_nat != 0))
2314 *is_identity_nat = nat44_ei_is_identity_static_mapping (m);
2320 nat44_ei_worker_db_free (nat44_ei_main_per_thread_data_t *tnm)
2322 pool_free (tnm->list_pool);
2323 pool_free (tnm->lru_pool);
2324 pool_free (tnm->sessions);
2325 pool_free (tnm->users);
2327 clib_bihash_free_8_8 (&tnm->user_hash);
2331 format_nat44_ei_key (u8 *s, va_list *args)
2333 u64 key = va_arg (*args, u64);
2337 nat_protocol_t protocol;
2340 split_nat_key (key, &addr, &port, &fib_index, &protocol);
2342 s = format (s, "%U proto %U port %d fib %d", format_ip4_address, &addr,
2343 format_nat_protocol, protocol, clib_net_to_host_u16 (port),
2349 format_nat44_ei_user_kvp (u8 *s, va_list *args)
2351 clib_bihash_kv_8_8_t *v = va_arg (*args, clib_bihash_kv_8_8_t *);
2352 nat44_ei_user_key_t k;
2356 s = format (s, "%U fib %d user-index %llu", format_ip4_address, &k.addr,
2357 k.fib_index, v->value);
2363 format_nat44_ei_session_kvp (u8 *s, va_list *args)
2365 clib_bihash_kv_8_8_t *v = va_arg (*args, clib_bihash_kv_8_8_t *);
2367 s = format (s, "%U thread-index %llu session-index %llu",
2368 format_nat44_ei_key, v->key, nat_value_get_thread_index (v),
2369 nat_value_get_session_index (v));
2375 format_nat44_ei_static_mapping_kvp (u8 *s, va_list *args)
2377 clib_bihash_kv_8_8_t *v = va_arg (*args, clib_bihash_kv_8_8_t *);
2379 s = format (s, "%U static-mapping-index %llu", format_nat44_ei_key, v->key,
2386 nat44_ei_worker_db_init (nat44_ei_main_per_thread_data_t *tnm,
2387 u32 translations, u32 translation_buckets,
2392 pool_alloc (tnm->list_pool, translations);
2393 pool_alloc (tnm->lru_pool, translations);
2394 pool_alloc (tnm->sessions, translations);
2396 clib_bihash_init_8_8 (&tnm->user_hash, "users", user_buckets, 0);
2398 clib_bihash_set_kvp_format_fn_8_8 (&tnm->user_hash,
2399 format_nat44_ei_user_kvp);
2401 pool_get (tnm->lru_pool, head);
2402 tnm->tcp_trans_lru_head_index = head - tnm->lru_pool;
2403 clib_dlist_init (tnm->lru_pool, tnm->tcp_trans_lru_head_index);
2405 pool_get (tnm->lru_pool, head);
2406 tnm->tcp_estab_lru_head_index = head - tnm->lru_pool;
2407 clib_dlist_init (tnm->lru_pool, tnm->tcp_estab_lru_head_index);
2409 pool_get (tnm->lru_pool, head);
2410 tnm->udp_lru_head_index = head - tnm->lru_pool;
2411 clib_dlist_init (tnm->lru_pool, tnm->udp_lru_head_index);
2413 pool_get (tnm->lru_pool, head);
2414 tnm->icmp_lru_head_index = head - tnm->lru_pool;
2415 clib_dlist_init (tnm->lru_pool, tnm->icmp_lru_head_index);
2417 pool_get (tnm->lru_pool, head);
2418 tnm->unk_proto_lru_head_index = head - tnm->lru_pool;
2419 clib_dlist_init (tnm->lru_pool, tnm->unk_proto_lru_head_index);
2425 nat44_ei_main_t *nm = &nat44_ei_main;
2426 nat44_ei_main_per_thread_data_t *tnm;
2428 pool_free (nm->static_mappings);
2429 clib_bihash_free_8_8 (&nm->static_mapping_by_local);
2430 clib_bihash_free_8_8 (&nm->static_mapping_by_external);
2434 clib_bihash_free_8_8 (&nm->in2out);
2435 clib_bihash_free_8_8 (&nm->out2in);
2436 vec_foreach (tnm, nm->per_thread_data)
2438 nat44_ei_worker_db_free (tnm);
2444 nat44_ei_db_init (u32 translations, u32 translation_buckets, u32 user_buckets)
2446 nat44_ei_main_t *nm = &nat44_ei_main;
2447 nat44_ei_main_per_thread_data_t *tnm;
2449 u32 static_mapping_buckets = 1024;
2450 u32 static_mapping_memory_size = 64 << 20;
2452 clib_bihash_init_8_8 (&nm->static_mapping_by_local,
2453 "static_mapping_by_local", static_mapping_buckets,
2454 static_mapping_memory_size);
2455 clib_bihash_init_8_8 (&nm->static_mapping_by_external,
2456 "static_mapping_by_external", static_mapping_buckets,
2457 static_mapping_memory_size);
2458 clib_bihash_set_kvp_format_fn_8_8 (&nm->static_mapping_by_local,
2459 format_nat44_ei_static_mapping_kvp);
2460 clib_bihash_set_kvp_format_fn_8_8 (&nm->static_mapping_by_external,
2461 format_nat44_ei_static_mapping_kvp);
2465 clib_bihash_init_8_8 (&nm->in2out, "in2out", translation_buckets, 0);
2466 clib_bihash_init_8_8 (&nm->out2in, "out2in", translation_buckets, 0);
2467 clib_bihash_set_kvp_format_fn_8_8 (&nm->in2out,
2468 format_nat44_ei_session_kvp);
2469 clib_bihash_set_kvp_format_fn_8_8 (&nm->out2in,
2470 format_nat44_ei_session_kvp);
2471 vec_foreach (tnm, nm->per_thread_data)
2473 nat44_ei_worker_db_init (tnm, translations, translation_buckets,
2480 nat44_ei_sessions_clear ()
2482 nat44_ei_main_t *nm = &nat44_ei_main;
2483 nat44_ei_main_per_thread_data_t *tnm;
2487 clib_bihash_free_8_8 (&nm->in2out);
2488 clib_bihash_free_8_8 (&nm->out2in);
2489 clib_bihash_init_8_8 (&nm->in2out, "in2out", nm->translation_buckets, 0);
2490 clib_bihash_init_8_8 (&nm->out2in, "out2in", nm->translation_buckets, 0);
2491 clib_bihash_set_kvp_format_fn_8_8 (&nm->in2out,
2492 format_nat44_ei_session_kvp);
2493 clib_bihash_set_kvp_format_fn_8_8 (&nm->out2in,
2494 format_nat44_ei_session_kvp);
2495 vec_foreach (tnm, nm->per_thread_data)
2497 nat44_ei_worker_db_free (tnm);
2498 nat44_ei_worker_db_init (tnm, nm->translations,
2499 nm->translation_buckets, nm->user_buckets);
2503 vlib_zero_simple_counter (&nm->total_users, 0);
2504 vlib_zero_simple_counter (&nm->total_sessions, 0);
2505 vlib_zero_simple_counter (&nm->user_limit_reached, 0);
2509 nat44_ei_update_outside_fib (ip4_main_t *im, uword opaque, u32 sw_if_index,
2510 u32 new_fib_index, u32 old_fib_index)
2512 nat44_ei_main_t *nm = &nat44_ei_main;
2513 nat44_ei_outside_fib_t *outside_fib;
2514 nat44_ei_interface_t *i;
2518 if (!nm->enabled || (new_fib_index == old_fib_index) ||
2519 (!vec_len (nm->outside_fibs)))
2524 pool_foreach (i, nm->interfaces)
2526 if (i->sw_if_index == sw_if_index)
2528 if (!(nat44_ei_interface_is_outside (i)))
2534 pool_foreach (i, nm->output_feature_interfaces)
2536 if (i->sw_if_index == sw_if_index)
2538 if (!(nat44_ei_interface_is_outside (i)))
2547 vec_foreach (outside_fib, nm->outside_fibs)
2549 if (outside_fib->fib_index == old_fib_index)
2551 outside_fib->refcount--;
2552 if (!outside_fib->refcount)
2553 vec_del1 (nm->outside_fibs, outside_fib - nm->outside_fibs);
2558 vec_foreach (outside_fib, nm->outside_fibs)
2560 if (outside_fib->fib_index == new_fib_index)
2562 outside_fib->refcount++;
2570 vec_add2 (nm->outside_fibs, outside_fib, 1);
2571 outside_fib->refcount = 1;
2572 outside_fib->fib_index = new_fib_index;
2577 nat44_ei_add_address (nat44_ei_main_t *nm, ip4_address_t *addr, u32 vrf_id)
2579 nat44_ei_address_t *ap;
2580 nat44_ei_interface_t *i;
2581 vlib_thread_main_t *tm = vlib_get_thread_main ();
2583 /* Check if address already exists */
2584 vec_foreach (ap, nm->addresses)
2586 if (ap->addr.as_u32 == addr->as_u32)
2588 nat44_ei_log_err ("address exist");
2589 return VNET_API_ERROR_VALUE_EXIST;
2593 vec_add2 (nm->addresses, ap, 1);
2597 ap->fib_index = fib_table_find_or_create_and_lock (
2598 FIB_PROTOCOL_IP4, vrf_id, nm->fib_src_low);
2602 #define _(N, i, n, s) \
2603 clib_memset (ap->busy_##n##_port_refcounts, 0, \
2604 sizeof (ap->busy_##n##_port_refcounts)); \
2605 ap->busy_##n##_ports = 0; \
2606 ap->busy_##n##_ports_per_thread = 0; \
2607 vec_validate_init_empty (ap->busy_##n##_ports_per_thread, \
2608 tm->n_vlib_mains - 1, 0);
2609 foreach_nat_protocol
2612 /* Add external address to FIB */
2613 pool_foreach (i, nm->interfaces)
2615 if (nat44_ei_interface_is_inside (i) || nm->out2in_dpo)
2618 nat44_ei_add_del_addr_to_fib (addr, 32, i->sw_if_index, 1);
2621 pool_foreach (i, nm->output_feature_interfaces)
2623 if (nat44_ei_interface_is_inside (i) || nm->out2in_dpo)
2626 nat44_ei_add_del_addr_to_fib (addr, 32, i->sw_if_index, 1);
2634 nat44_ei_add_interface_address (nat44_ei_main_t *nm, u32 sw_if_index,
2637 ip4_main_t *ip4_main = nm->ip4_main;
2638 ip4_address_t *first_int_addr;
2639 nat44_ei_static_map_resolve_t *rp;
2640 u32 *indices_to_delete = 0;
2642 u32 *auto_add_sw_if_indices = nm->auto_add_sw_if_indices;
2644 first_int_addr = ip4_interface_first_address (ip4_main, sw_if_index,
2645 0 /* just want the address */);
2647 for (i = 0; i < vec_len (auto_add_sw_if_indices); i++)
2649 if (auto_add_sw_if_indices[i] == sw_if_index)
2653 /* if have address remove it */
2655 (void) nat44_ei_del_address (nm, first_int_addr[0], 1);
2658 for (j = 0; j < vec_len (nm->to_resolve); j++)
2660 rp = nm->to_resolve + j;
2661 if (rp->sw_if_index == sw_if_index)
2662 vec_add1 (indices_to_delete, j);
2664 if (vec_len (indices_to_delete))
2666 for (j = vec_len (indices_to_delete) - 1; j >= 0; j--)
2667 vec_del1 (nm->to_resolve, j);
2668 vec_free (indices_to_delete);
2671 vec_del1 (nm->auto_add_sw_if_indices, i);
2674 return VNET_API_ERROR_VALUE_EXIST;
2681 return VNET_API_ERROR_NO_SUCH_ENTRY;
2683 /* add to the auto-address list */
2684 vec_add1 (nm->auto_add_sw_if_indices, sw_if_index);
2686 /* If the address is already bound - or static - add it now */
2688 (void) nat44_ei_add_address (nm, first_int_addr, ~0);
2694 nat44_ei_is_address_used_in_static_mapping (ip4_address_t addr)
2696 nat44_ei_main_t *nm = &nat44_ei_main;
2697 nat44_ei_static_mapping_t *m;
2698 pool_foreach (m, nm->static_mappings)
2700 if (nat44_ei_is_addr_only_static_mapping (m) ||
2701 nat44_ei_is_identity_static_mapping (m))
2703 if (m->external_addr.as_u32 == addr.as_u32)
2710 nat44_ei_del_address (nat44_ei_main_t *nm, ip4_address_t addr, u8 delete_sm)
2712 nat44_ei_address_t *a = 0;
2713 nat44_ei_session_t *ses;
2714 u32 *ses_to_be_removed = 0, *ses_index;
2715 nat44_ei_main_per_thread_data_t *tnm;
2716 nat44_ei_interface_t *interface;
2717 nat44_ei_static_mapping_t *m;
2720 /* Find SNAT address */
2721 for (i = 0; i < vec_len (nm->addresses); i++)
2723 if (nm->addresses[i].addr.as_u32 == addr.as_u32)
2725 a = nm->addresses + i;
2731 nat44_ei_log_err ("no such address");
2732 return VNET_API_ERROR_NO_SUCH_ENTRY;
2737 pool_foreach (m, nm->static_mappings)
2739 if (m->external_addr.as_u32 == addr.as_u32)
2740 (void) nat44_ei_add_del_static_mapping (
2741 m->local_addr, m->external_addr, m->local_port, m->external_port,
2742 m->proto, ~0 /* sw_if_index */, m->vrf_id,
2743 nat44_ei_is_addr_only_static_mapping (m),
2744 nat44_ei_is_identity_static_mapping (m), m->tag, 0);
2749 /* Check if address is used in some static mapping */
2750 if (nat44_ei_is_address_used_in_static_mapping (addr))
2752 nat44_ei_log_err ("address used in static mapping");
2753 return VNET_API_ERROR_UNSPECIFIED;
2757 if (a->fib_index != ~0)
2758 fib_table_unlock (a->fib_index, FIB_PROTOCOL_IP4, nm->fib_src_low);
2760 /* Delete sessions using address */
2761 if (a->busy_tcp_ports || a->busy_udp_ports || a->busy_icmp_ports)
2763 vec_foreach (tnm, nm->per_thread_data)
2765 pool_foreach (ses, tnm->sessions)
2767 if (ses->out2in.addr.as_u32 == addr.as_u32)
2769 nat44_ei_free_session_data (nm, ses,
2770 tnm - nm->per_thread_data, 0);
2771 vec_add1 (ses_to_be_removed, ses - tnm->sessions);
2774 vec_foreach (ses_index, ses_to_be_removed)
2776 ses = pool_elt_at_index (tnm->sessions, ses_index[0]);
2777 nat44_ei_delete_session (nm, ses, tnm - nm->per_thread_data);
2779 vec_free (ses_to_be_removed);
2783 #define _(N, i, n, s) vec_free (a->busy_##n##_ports_per_thread);
2784 foreach_nat_protocol
2786 vec_del1 (nm->addresses, i);
2788 /* Delete external address from FIB */
2789 pool_foreach (interface, nm->interfaces)
2791 if (nat44_ei_interface_is_inside (interface) || nm->out2in_dpo)
2793 nat44_ei_add_del_addr_to_fib (&addr, 32, interface->sw_if_index, 0);
2797 pool_foreach (interface, nm->output_feature_interfaces)
2799 if (nat44_ei_interface_is_inside (interface) || nm->out2in_dpo)
2801 nat44_ei_add_del_addr_to_fib (&addr, 32, interface->sw_if_index, 0);
2809 nat44_ei_ip4_add_del_interface_address_cb (ip4_main_t *im, uword opaque,
2811 ip4_address_t *address,
2813 u32 if_address_index, u32 is_delete)
2815 nat44_ei_main_t *nm = &nat44_ei_main;
2816 nat44_ei_static_map_resolve_t *rp;
2817 ip4_address_t l_addr;
2820 nat44_ei_address_t *addresses = nm->addresses;
2825 for (i = 0; i < vec_len (nm->auto_add_sw_if_indices); i++)
2827 if (sw_if_index == nm->auto_add_sw_if_indices[i])
2836 /* Don't trip over lease renewal, static config */
2837 for (j = 0; j < vec_len (addresses); j++)
2838 if (addresses[j].addr.as_u32 == address->as_u32)
2841 (void) nat44_ei_add_address (nm, address, ~0);
2842 /* Scan static map resolution vector */
2843 for (j = 0; j < vec_len (nm->to_resolve); j++)
2845 rp = nm->to_resolve + j;
2848 /* On this interface? */
2849 if (rp->sw_if_index == sw_if_index)
2851 /* Indetity mapping? */
2852 if (rp->l_addr.as_u32 == 0)
2853 l_addr.as_u32 = address[0].as_u32;
2855 l_addr.as_u32 = rp->l_addr.as_u32;
2856 /* Add the static mapping */
2857 rv = nat44_ei_add_del_static_mapping (
2858 l_addr, address[0], rp->l_port, rp->e_port, rp->proto,
2859 ~0 /* sw_if_index */, rp->vrf_id, rp->addr_only,
2860 rp->identity_nat, rp->tag, 1);
2862 nat_elog_notice_X1 (
2863 nm, "nat44_ei_add_del_static_mapping returned %d", "i4", rv);
2870 (void) nat44_ei_del_address (nm, address[0], 1);
2876 nat44_ei_set_frame_queue_nelts (u32 frame_queue_nelts)
2879 nat44_ei_main_t *nm = &nat44_ei_main;
2880 nm->frame_queue_nelts = frame_queue_nelts;
2885 nat44_ei_ip4_add_del_addr_only_sm_cb (ip4_main_t *im, uword opaque,
2886 u32 sw_if_index, ip4_address_t *address,
2887 u32 address_length, u32 if_address_index,
2890 nat44_ei_main_t *nm = &nat44_ei_main;
2891 nat44_ei_static_map_resolve_t *rp;
2892 nat44_ei_static_mapping_t *m;
2893 clib_bihash_kv_8_8_t kv, value;
2895 ip4_address_t l_addr;
2900 for (i = 0; i < vec_len (nm->to_resolve); i++)
2902 rp = nm->to_resolve + i;
2903 if (rp->addr_only == 0)
2905 if (rp->sw_if_index == sw_if_index)
2912 init_nat_k (&kv, *address, rp->addr_only ? 0 : rp->e_port,
2913 nm->outside_fib_index, rp->addr_only ? 0 : rp->proto);
2914 if (clib_bihash_search_8_8 (&nm->static_mapping_by_external, &kv, &value))
2917 m = pool_elt_at_index (nm->static_mappings, value.value);
2921 /* Don't trip over lease renewal, static config */
2931 /* Indetity mapping? */
2932 if (rp->l_addr.as_u32 == 0)
2933 l_addr.as_u32 = address[0].as_u32;
2935 l_addr.as_u32 = rp->l_addr.as_u32;
2936 /* Add the static mapping */
2938 rv = nat44_ei_add_del_static_mapping (
2939 l_addr, address[0], rp->l_port, rp->e_port, rp->proto,
2940 ~0 /* sw_if_index */, rp->vrf_id, rp->addr_only, rp->identity_nat, rp->tag,
2943 nat_elog_notice_X1 (nm, "nat44_ei_add_del_static_mapping returned %d",
2947 static_always_inline uword
2948 nat44_ei_classify_inline_fn (vlib_main_t *vm, vlib_node_runtime_t *node,
2949 vlib_frame_t *frame)
2951 u32 n_left_from, *from, *to_next;
2952 nat44_ei_classify_next_t next_index;
2953 nat44_ei_main_t *nm = &nat44_ei_main;
2954 nat44_ei_static_mapping_t *m;
2955 u32 next_in2out = 0, next_out2in = 0;
2957 from = vlib_frame_vector_args (frame);
2958 n_left_from = frame->n_vectors;
2959 next_index = node->cached_next_index;
2961 while (n_left_from > 0)
2965 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
2967 while (n_left_from > 0 && n_left_to_next > 0)
2971 u32 next0 = NAT44_EI_CLASSIFY_NEXT_IN2OUT;
2973 nat44_ei_address_t *ap;
2974 clib_bihash_kv_8_8_t kv0, value0;
2976 /* speculatively enqueue b0 to the current next frame */
2982 n_left_to_next -= 1;
2984 b0 = vlib_get_buffer (vm, bi0);
2985 ip0 = vlib_buffer_get_current (b0);
2987 vec_foreach (ap, nm->addresses)
2989 if (ip0->dst_address.as_u32 == ap->addr.as_u32)
2991 next0 = NAT44_EI_CLASSIFY_NEXT_OUT2IN;
2996 if (PREDICT_FALSE (pool_elts (nm->static_mappings)))
2998 init_nat_k (&kv0, ip0->dst_address, 0, 0, 0);
2999 /* try to classify the fragment based on IP header alone */
3000 if (!clib_bihash_search_8_8 (&nm->static_mapping_by_external,
3003 m = pool_elt_at_index (nm->static_mappings, value0.value);
3004 if (m->local_addr.as_u32 != m->external_addr.as_u32)
3005 next0 = NAT44_EI_CLASSIFY_NEXT_OUT2IN;
3008 init_nat_k (&kv0, ip0->dst_address,
3009 vnet_buffer (b0)->ip.reass.l4_dst_port, 0,
3010 ip_proto_to_nat_proto (ip0->protocol));
3011 if (!clib_bihash_search_8_8 (&nm->static_mapping_by_external,
3014 m = pool_elt_at_index (nm->static_mappings, value0.value);
3015 if (m->local_addr.as_u32 != m->external_addr.as_u32)
3016 next0 = NAT44_EI_CLASSIFY_NEXT_OUT2IN;
3021 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE) &&
3022 (b0->flags & VLIB_BUFFER_IS_TRACED)))
3024 nat44_ei_classify_trace_t *t =
3025 vlib_add_trace (vm, node, b0, sizeof (*t));
3027 t->next_in2out = next0 == NAT44_EI_CLASSIFY_NEXT_IN2OUT ? 1 : 0;
3030 next_in2out += next0 == NAT44_EI_CLASSIFY_NEXT_IN2OUT;
3031 next_out2in += next0 == NAT44_EI_CLASSIFY_NEXT_OUT2IN;
3033 /* verify speculative enqueue, maybe switch current next frame */
3034 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
3035 n_left_to_next, bi0, next0);
3038 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
3041 vlib_node_increment_counter (
3042 vm, node->node_index, NAT44_EI_CLASSIFY_ERROR_NEXT_IN2OUT, next_in2out);
3043 vlib_node_increment_counter (
3044 vm, node->node_index, NAT44_EI_CLASSIFY_ERROR_NEXT_OUT2IN, next_out2in);
3045 return frame->n_vectors;
3048 VLIB_NODE_FN (nat44_ei_classify_node)
3049 (vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
3051 return nat44_ei_classify_inline_fn (vm, node, frame);
3054 VLIB_REGISTER_NODE (nat44_ei_classify_node) = {
3055 .name = "nat44-ei-classify",
3056 .vector_size = sizeof (u32),
3057 .format_trace = format_nat44_ei_classify_trace,
3058 .type = VLIB_NODE_TYPE_INTERNAL,
3059 .n_errors = ARRAY_LEN(nat44_ei_classify_error_strings),
3060 .error_strings = nat44_ei_classify_error_strings,
3061 .n_next_nodes = NAT44_EI_CLASSIFY_N_NEXT,
3063 [NAT44_EI_CLASSIFY_NEXT_IN2OUT] = "nat44-ei-in2out",
3064 [NAT44_EI_CLASSIFY_NEXT_OUT2IN] = "nat44-ei-out2in",
3065 [NAT44_EI_CLASSIFY_NEXT_DROP] = "error-drop",
3069 VLIB_NODE_FN (nat44_ei_handoff_classify_node)
3070 (vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
3072 return nat44_ei_classify_inline_fn (vm, node, frame);
3075 VLIB_REGISTER_NODE (nat44_ei_handoff_classify_node) = {
3076 .name = "nat44-ei-handoff-classify",
3077 .vector_size = sizeof (u32),
3078 .format_trace = format_nat44_ei_classify_trace,
3079 .type = VLIB_NODE_TYPE_INTERNAL,
3080 .n_errors = ARRAY_LEN(nat44_ei_classify_error_strings),
3081 .error_strings = nat44_ei_classify_error_strings,
3082 .n_next_nodes = NAT44_EI_CLASSIFY_N_NEXT,
3084 [NAT44_EI_CLASSIFY_NEXT_IN2OUT] = "nat44-ei-in2out-worker-handoff",
3085 [NAT44_EI_CLASSIFY_NEXT_OUT2IN] = "nat44-ei-out2in-worker-handoff",
3086 [NAT44_EI_CLASSIFY_NEXT_DROP] = "error-drop",
3091 * fd.io coding-style-patch-verification: ON
3094 * eval: (c-set-style "gnu")