2 * snat.c - simple nat plugin
4 * Copyright (c) 2016 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,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
18 #include <vnet/vnet.h>
19 #include <vnet/ip/ip.h>
20 #include <vnet/ip/ip4.h>
21 #include <vnet/plugin/plugin.h>
23 #include <nat/nat_dpo.h>
24 #include <nat/lib/ipfix_logging.h>
25 #include <nat/nat_inlines.h>
26 #include <nat/nat44/inlines.h>
27 #include <nat/nat_affinity.h>
28 #include <nat/nat_syslog.h>
29 #include <nat/nat_ha.h>
30 #include <vnet/fib/fib_table.h>
31 #include <vnet/fib/ip4_fib.h>
32 #include <vnet/ip/reass/ip4_sv_reass.h>
33 #include <vppinfra/bihash_16_8.h>
34 #include <nat/nat44/ed_inlines.h>
36 #include <vpp/app/version.h>
38 snat_main_t snat_main;
40 fib_source_t nat_fib_src_hi;
41 fib_source_t nat_fib_src_low;
44 /* Hook up input features */
45 VNET_FEATURE_INIT (nat_pre_in2out, static) = {
46 .arc_name = "ip4-unicast",
47 .node_name = "nat-pre-in2out",
48 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa",
49 "ip4-sv-reassembly-feature"),
51 VNET_FEATURE_INIT (nat_pre_out2in, static) = {
52 .arc_name = "ip4-unicast",
53 .node_name = "nat-pre-out2in",
54 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa",
55 "ip4-dhcp-client-detect",
56 "ip4-sv-reassembly-feature"),
58 VNET_FEATURE_INIT (snat_in2out_worker_handoff, static) = {
59 .arc_name = "ip4-unicast",
60 .node_name = "nat44-in2out-worker-handoff",
61 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa"),
63 VNET_FEATURE_INIT (snat_out2in_worker_handoff, static) = {
64 .arc_name = "ip4-unicast",
65 .node_name = "nat44-out2in-worker-handoff",
66 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa",
67 "ip4-dhcp-client-detect"),
69 VNET_FEATURE_INIT (ip4_snat_in2out, static) = {
70 .arc_name = "ip4-unicast",
71 .node_name = "nat44-in2out",
72 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa","ip4-sv-reassembly-feature"),
74 VNET_FEATURE_INIT (ip4_snat_out2in, static) = {
75 .arc_name = "ip4-unicast",
76 .node_name = "nat44-out2in",
77 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa","ip4-sv-reassembly-feature",
78 "ip4-dhcp-client-detect"),
80 VNET_FEATURE_INIT (ip4_nat_classify, static) = {
81 .arc_name = "ip4-unicast",
82 .node_name = "nat44-classify",
83 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa","ip4-sv-reassembly-feature"),
85 VNET_FEATURE_INIT (ip4_nat44_ed_in2out, static) = {
86 .arc_name = "ip4-unicast",
87 .node_name = "nat44-ed-in2out",
88 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa","ip4-sv-reassembly-feature"),
90 VNET_FEATURE_INIT (ip4_nat44_ed_out2in, static) = {
91 .arc_name = "ip4-unicast",
92 .node_name = "nat44-ed-out2in",
93 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa","ip4-sv-reassembly-feature",
94 "ip4-dhcp-client-detect"),
96 VNET_FEATURE_INIT (ip4_nat44_ed_classify, static) = {
97 .arc_name = "ip4-unicast",
98 .node_name = "nat44-ed-classify",
99 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa","ip4-sv-reassembly-feature"),
101 VNET_FEATURE_INIT (ip4_nat_handoff_classify, static) = {
102 .arc_name = "ip4-unicast",
103 .node_name = "nat44-handoff-classify",
104 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa","ip4-sv-reassembly-feature"),
106 VNET_FEATURE_INIT (ip4_snat_in2out_fast, static) = {
107 .arc_name = "ip4-unicast",
108 .node_name = "nat44-in2out-fast",
109 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa","ip4-sv-reassembly-feature"),
111 VNET_FEATURE_INIT (ip4_snat_out2in_fast, static) = {
112 .arc_name = "ip4-unicast",
113 .node_name = "nat44-out2in-fast",
114 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa","ip4-sv-reassembly-feature",
115 "ip4-dhcp-client-detect"),
117 VNET_FEATURE_INIT (ip4_snat_hairpin_dst, static) = {
118 .arc_name = "ip4-unicast",
119 .node_name = "nat44-hairpin-dst",
120 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa","ip4-sv-reassembly-feature"),
122 VNET_FEATURE_INIT (ip4_nat44_ed_hairpin_dst, static) = {
123 .arc_name = "ip4-unicast",
124 .node_name = "nat44-ed-hairpin-dst",
125 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa","ip4-sv-reassembly-feature"),
128 /* Hook up output features */
129 VNET_FEATURE_INIT (ip4_snat_in2out_output, static) = {
130 .arc_name = "ip4-output",
131 .node_name = "nat44-in2out-output",
132 .runs_after = VNET_FEATURES ("acl-plugin-out-ip4-fa","ip4-sv-reassembly-output-feature"),
134 VNET_FEATURE_INIT (ip4_snat_in2out_output_worker_handoff, static) = {
135 .arc_name = "ip4-output",
136 .node_name = "nat44-in2out-output-worker-handoff",
137 .runs_after = VNET_FEATURES ("acl-plugin-out-ip4-fa","ip4-sv-reassembly-output-feature"),
139 VNET_FEATURE_INIT (ip4_snat_hairpin_src, static) = {
140 .arc_name = "ip4-output",
141 .node_name = "nat44-hairpin-src",
142 .runs_after = VNET_FEATURES ("acl-plugin-out-ip4-fa","ip4-sv-reassembly-output-feature"),
144 VNET_FEATURE_INIT (nat_pre_in2out_output, static) = {
145 .arc_name = "ip4-output",
146 .node_name = "nat-pre-in2out-output",
147 .runs_after = VNET_FEATURES ("ip4-sv-reassembly-output-feature"),
148 .runs_before = VNET_FEATURES ("acl-plugin-out-ip4-fa"),
150 VNET_FEATURE_INIT (ip4_nat44_ed_in2out_output, static) = {
151 .arc_name = "ip4-output",
152 .node_name = "nat44-ed-in2out-output",
153 .runs_after = VNET_FEATURES ("ip4-sv-reassembly-output-feature"),
154 .runs_before = VNET_FEATURES ("acl-plugin-out-ip4-fa"),
156 VNET_FEATURE_INIT (ip4_nat44_ed_hairpin_src, static) = {
157 .arc_name = "ip4-output",
158 .node_name = "nat44-ed-hairpin-src",
159 .runs_after = VNET_FEATURES ("ip4-sv-reassembly-output-feature"),
160 .runs_before = VNET_FEATURES ("acl-plugin-out-ip4-fa"),
163 /* Hook up ip4-local features */
164 VNET_FEATURE_INIT (ip4_nat_hairpinning, static) =
166 .arc_name = "ip4-local",
167 .node_name = "nat44-hairpinning",
168 .runs_before = VNET_FEATURES("ip4-local-end-of-arc"),
170 VNET_FEATURE_INIT (ip4_nat44_ed_hairpinning, static) =
172 .arc_name = "ip4-local",
173 .node_name = "nat44-ed-hairpinning",
174 .runs_before = VNET_FEATURES("ip4-local-end-of-arc"),
178 VLIB_PLUGIN_REGISTER () = {
179 .version = VPP_BUILD_VER,
180 .description = "Network Address Translation (NAT)",
185 nat_free_session_data (snat_main_t * sm, snat_session_t * s, u32 thread_index,
188 clib_bihash_kv_8_8_t kv;
191 ip4_address_t *l_addr, *r_addr;
193 clib_bihash_kv_16_8_t ed_kv;
194 snat_main_per_thread_data_t *tsm =
195 vec_elt_at_index (sm->per_thread_data, thread_index);
197 if (is_ed_session (s))
199 per_vrf_sessions_unregister_session (s, thread_index);
202 if (is_fwd_bypass_session (s))
204 if (snat_is_unk_proto_session (s))
206 init_ed_k (&ed_kv, s->in2out.addr, 0, s->ext_host_addr, 0, 0,
211 l_port = s->in2out.port;
212 r_port = s->ext_host_port;
213 l_addr = &s->in2out.addr;
214 r_addr = &s->ext_host_addr;
215 proto = nat_proto_to_ip_proto (s->nat_proto);
216 fib_index = s->in2out.fib_index;
217 init_ed_k (&ed_kv, *l_addr, l_port, *r_addr, r_port, fib_index,
220 if (clib_bihash_add_del_16_8 (&tsm->in2out_ed, &ed_kv, 0))
221 nat_elog_warn ("in2out_ed key del failed");
225 /* session lookup tables */
226 if (is_ed_session (s))
228 if (is_affinity_sessions (s))
229 nat_affinity_unlock (s->ext_host_addr, s->out2in.addr,
230 s->nat_proto, s->out2in.port);
231 l_addr = &s->out2in.addr;
232 r_addr = &s->ext_host_addr;
233 fib_index = s->out2in.fib_index;
234 if (snat_is_unk_proto_session (s))
236 proto = s->in2out.port;
242 proto = nat_proto_to_ip_proto (s->nat_proto);
243 l_port = s->out2in.port;
244 r_port = s->ext_host_port;
246 init_ed_k (&ed_kv, *l_addr, l_port, *r_addr, r_port, fib_index, proto);
247 if (clib_bihash_add_del_16_8 (&sm->out2in_ed, &ed_kv, 0))
248 nat_elog_warn ("out2in_ed key del failed");
249 l_addr = &s->in2out.addr;
250 fib_index = s->in2out.fib_index;
251 if (!snat_is_unk_proto_session (s))
252 l_port = s->in2out.port;
253 if (is_twice_nat_session (s))
255 r_addr = &s->ext_host_nat_addr;
256 r_port = s->ext_host_nat_port;
258 init_ed_k (&ed_kv, *l_addr, l_port, *r_addr, r_port, fib_index, proto);
259 if (clib_bihash_add_del_16_8 (&tsm->in2out_ed, &ed_kv, 0))
260 nat_elog_warn ("in2out_ed key del failed");
263 nat_syslog_nat44_sdel (s->user_index, s->in2out.fib_index,
264 &s->in2out.addr, s->in2out.port,
265 &s->ext_host_nat_addr, s->ext_host_nat_port,
266 &s->out2in.addr, s->out2in.port,
267 &s->ext_host_addr, s->ext_host_port,
268 s->nat_proto, is_twice_nat_session (s));
272 init_nat_i2o_k (&kv, s);
273 if (clib_bihash_add_del_8_8 (&tsm->in2out, &kv, 0))
274 nat_elog_warn ("in2out key del failed");
275 init_nat_o2i_k (&kv, s);
276 if (clib_bihash_add_del_8_8 (&tsm->out2in, &kv, 0))
277 nat_elog_warn ("out2in key del failed");
280 nat_syslog_nat44_apmdel (s->user_index, s->in2out.fib_index,
281 &s->in2out.addr, s->in2out.port,
282 &s->out2in.addr, s->out2in.port,
286 if (snat_is_unk_proto_session (s))
292 nat_ipfix_logging_nat44_ses_delete (thread_index,
293 s->in2out.addr.as_u32,
294 s->out2in.addr.as_u32,
298 s->in2out.fib_index);
300 nat_ha_sdel (&s->out2in.addr, s->out2in.port, &s->ext_host_addr,
301 s->ext_host_port, s->nat_proto, s->out2in.fib_index,
305 /* Twice NAT address and port for external host */
306 if (is_twice_nat_session (s))
308 snat_free_outside_address_and_port (sm->twice_nat_addresses,
310 &s->ext_host_nat_addr,
311 s->ext_host_nat_port, s->nat_proto);
314 if (snat_is_session_static (s))
317 snat_free_outside_address_and_port (sm->addresses, thread_index,
318 &s->out2in.addr, s->out2in.port,
323 nat44_free_session_data (snat_main_t * sm, snat_session_t * s,
324 u32 thread_index, u8 is_ha)
328 ip4_address_t *l_addr, *r_addr;
330 clib_bihash_kv_16_8_t ed_kv;
331 snat_main_per_thread_data_t *tsm =
332 vec_elt_at_index (sm->per_thread_data, thread_index);
334 if (is_fwd_bypass_session (s))
336 if (snat_is_unk_proto_session (s))
338 proto = s->in2out.port;
344 proto = nat_proto_to_ip_proto (s->nat_proto);
345 l_port = s->in2out.port;
346 r_port = s->ext_host_port;
349 l_addr = &s->in2out.addr;
350 r_addr = &s->ext_host_addr;
352 init_ed_k (&ed_kv, *l_addr, l_port, *r_addr, r_port, fib_index, proto);
355 (clib_bihash_add_del_16_8 (&tsm->in2out_ed, &ed_kv, 0)))
356 nat_elog_warn ("in2out_ed key del failed");
360 /* session lookup tables */
361 if (is_affinity_sessions (s))
362 nat_affinity_unlock (s->ext_host_addr, s->out2in.addr,
363 s->nat_proto, s->out2in.port);
364 l_addr = &s->out2in.addr;
365 r_addr = &s->ext_host_addr;
366 fib_index = s->out2in.fib_index;
367 if (snat_is_unk_proto_session (s))
369 proto = s->in2out.port;
375 proto = nat_proto_to_ip_proto (s->nat_proto);
376 l_port = s->out2in.port;
377 r_port = s->ext_host_port;
379 init_ed_k (&ed_kv, *l_addr, l_port, *r_addr, r_port, fib_index, proto);
381 if (PREDICT_FALSE (clib_bihash_add_del_16_8 (&sm->out2in_ed, &ed_kv, 0)))
382 nat_elog_warn ("out2in_ed key del failed");
384 l_addr = &s->in2out.addr;
385 fib_index = s->in2out.fib_index;
387 if (!snat_is_unk_proto_session (s))
388 l_port = s->in2out.port;
390 if (is_twice_nat_session (s))
392 r_addr = &s->ext_host_nat_addr;
393 r_port = s->ext_host_nat_port;
395 init_ed_k (&ed_kv, *l_addr, l_port, *r_addr, r_port, fib_index, proto);
397 if (PREDICT_FALSE (clib_bihash_add_del_16_8 (&tsm->in2out_ed, &ed_kv, 0)))
398 nat_elog_warn ("in2out_ed key del failed");
402 nat_syslog_nat44_sdel (s->user_index, s->in2out.fib_index,
403 &s->in2out.addr, s->in2out.port,
404 &s->ext_host_nat_addr, s->ext_host_nat_port,
405 &s->out2in.addr, s->out2in.port,
406 &s->ext_host_addr, s->ext_host_port,
407 s->nat_proto, is_twice_nat_session (s));
410 if (snat_is_unk_proto_session (s))
415 nat_ipfix_logging_nat44_ses_delete (thread_index,
416 s->in2out.addr.as_u32,
417 s->out2in.addr.as_u32,
421 s->in2out.fib_index);
422 nat_ha_sdel (&s->out2in.addr, s->out2in.port, &s->ext_host_addr,
423 s->ext_host_port, s->nat_proto, s->out2in.fib_index,
427 /* Twice NAT address and port for external host */
428 if (is_twice_nat_session (s))
430 snat_free_outside_address_and_port (sm->twice_nat_addresses,
432 &s->ext_host_nat_addr,
433 s->ext_host_nat_port, s->nat_proto);
436 if (snat_is_session_static (s))
439 snat_free_outside_address_and_port (sm->addresses, thread_index,
440 &s->out2in.addr, s->out2in.port,
446 nat_user_get_or_create (snat_main_t * sm, ip4_address_t * addr, u32 fib_index,
450 snat_user_key_t user_key;
451 clib_bihash_kv_8_8_t kv, value;
452 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
453 dlist_elt_t *per_user_list_head_elt;
455 user_key.addr.as_u32 = addr->as_u32;
456 user_key.fib_index = fib_index;
457 kv.key = user_key.as_u64;
459 /* Ever heard of the "user" = src ip4 address before? */
460 if (clib_bihash_search_8_8 (&tsm->user_hash, &kv, &value))
462 if (pool_elts (tsm->users) >= sm->max_users_per_thread)
464 vlib_increment_simple_counter (&sm->user_limit_reached,
466 nat_elog_warn ("maximum user limit reached");
469 /* no, make a new one */
470 pool_get (tsm->users, u);
471 clib_memset (u, 0, sizeof (*u));
473 u->addr.as_u32 = addr->as_u32;
474 u->fib_index = fib_index;
476 pool_get (tsm->list_pool, per_user_list_head_elt);
478 u->sessions_per_user_list_head_index = per_user_list_head_elt -
481 clib_dlist_init (tsm->list_pool, u->sessions_per_user_list_head_index);
483 kv.value = u - tsm->users;
486 if (clib_bihash_add_del_8_8 (&tsm->user_hash, &kv, 1))
488 nat_elog_warn ("user_hash key add failed");
489 nat44_delete_user_with_no_session (sm, u, thread_index);
493 vlib_set_simple_counter (&sm->total_users, thread_index, 0,
494 pool_elts (tsm->users));
498 u = pool_elt_at_index (tsm->users, value.value);
505 nat_session_alloc_or_recycle (snat_main_t * sm, snat_user_t * u,
506 u32 thread_index, f64 now)
509 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
510 u32 oldest_per_user_translation_list_index, session_index;
511 dlist_elt_t *oldest_per_user_translation_list_elt;
512 dlist_elt_t *per_user_translation_list_elt;
514 /* Over quota? Recycle the least recently used translation */
515 if ((u->nsessions + u->nstaticsessions) >= sm->max_translations_per_user)
517 oldest_per_user_translation_list_index =
518 clib_dlist_remove_head (tsm->list_pool,
519 u->sessions_per_user_list_head_index);
521 ASSERT (oldest_per_user_translation_list_index != ~0);
523 /* Add it back to the end of the LRU list */
524 clib_dlist_addtail (tsm->list_pool,
525 u->sessions_per_user_list_head_index,
526 oldest_per_user_translation_list_index);
527 /* Get the list element */
528 oldest_per_user_translation_list_elt =
529 pool_elt_at_index (tsm->list_pool,
530 oldest_per_user_translation_list_index);
532 /* Get the session index from the list element */
533 session_index = oldest_per_user_translation_list_elt->value;
535 /* Get the session */
536 s = pool_elt_at_index (tsm->sessions, session_index);
537 nat_free_session_data (sm, s, thread_index, 0);
538 if (snat_is_session_static (s))
539 u->nstaticsessions--;
546 s->ext_host_addr.as_u32 = 0;
547 s->ext_host_port = 0;
548 s->ext_host_nat_addr.as_u32 = 0;
549 s->ext_host_nat_port = 0;
553 pool_get (tsm->sessions, s);
554 clib_memset (s, 0, sizeof (*s));
556 /* Create list elts */
557 pool_get (tsm->list_pool, per_user_translation_list_elt);
558 clib_dlist_init (tsm->list_pool,
559 per_user_translation_list_elt - tsm->list_pool);
561 per_user_translation_list_elt->value = s - tsm->sessions;
562 s->per_user_index = per_user_translation_list_elt - tsm->list_pool;
563 s->per_user_list_head_index = u->sessions_per_user_list_head_index;
565 clib_dlist_addtail (tsm->list_pool,
566 s->per_user_list_head_index,
567 per_user_translation_list_elt - tsm->list_pool);
569 s->user_index = u - tsm->users;
570 vlib_set_simple_counter (&sm->total_sessions, thread_index, 0,
571 pool_elts (tsm->sessions));
574 s->ha_last_refreshed = now;
580 snat_add_del_addr_to_fib (ip4_address_t * addr, u8 p_len, u32 sw_if_index,
583 fib_prefix_t prefix = {
585 .fp_proto = FIB_PROTOCOL_IP4,
587 .ip4.as_u32 = addr->as_u32,
590 u32 fib_index = ip4_fib_table_get_index_for_sw_if_index (sw_if_index);
593 fib_table_entry_update_one_path (fib_index,
596 (FIB_ENTRY_FLAG_CONNECTED |
597 FIB_ENTRY_FLAG_LOCAL |
598 FIB_ENTRY_FLAG_EXCLUSIVE),
602 ~0, 1, NULL, FIB_ROUTE_PATH_FLAG_NONE);
604 fib_table_entry_delete (fib_index, &prefix, nat_fib_src_low);
608 snat_add_address (snat_main_t * sm, ip4_address_t * addr, u32 vrf_id,
613 vlib_thread_main_t *tm = vlib_get_thread_main ();
615 if (twice_nat && !sm->endpoint_dependent)
616 return VNET_API_ERROR_FEATURE_DISABLED;
618 /* Check if address already exists */
620 vec_foreach (ap, twice_nat ? sm->twice_nat_addresses : sm->addresses)
622 if (ap->addr.as_u32 == addr->as_u32)
623 return VNET_API_ERROR_VALUE_EXIST;
628 vec_add2 (sm->twice_nat_addresses, ap, 1);
630 vec_add2 (sm->addresses, ap, 1);
635 fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4, vrf_id,
639 #define _(N, i, n, s) \
640 clib_memset(ap->busy_##n##_port_refcounts, 0, sizeof(ap->busy_##n##_port_refcounts));\
641 ap->busy_##n##_ports = 0; \
642 ap->busy_##n##_ports_per_thread = 0;\
643 vec_validate_init_empty (ap->busy_##n##_ports_per_thread, tm->n_vlib_mains - 1, 0);
649 /* Add external address to FIB */
651 pool_foreach (i, sm->interfaces,
653 if (nat_interface_is_inside(i) || sm->out2in_dpo)
656 snat_add_del_addr_to_fib(addr, 32, i->sw_if_index, 1);
659 pool_foreach (i, sm->output_feature_interfaces,
661 if (nat_interface_is_inside(i) || sm->out2in_dpo)
664 snat_add_del_addr_to_fib(addr, 32, i->sw_if_index, 1);
673 is_snat_address_used_in_static_mapping (snat_main_t * sm, ip4_address_t addr)
675 snat_static_mapping_t *m;
677 pool_foreach (m, sm->static_mappings,
679 if (is_addr_only_static_mapping (m) ||
680 is_out2in_only_static_mapping (m) ||
681 is_identity_static_mapping (m))
683 if (m->external_addr.as_u32 == addr.as_u32)
692 snat_add_static_mapping_when_resolved (snat_main_t * sm,
693 ip4_address_t l_addr,
698 nat_protocol_t proto,
699 int addr_only, int is_add, u8 * tag,
700 int twice_nat, int out2in_only,
702 ip4_address_t pool_addr, int exact)
704 snat_static_map_resolve_t *rp;
706 vec_add2 (sm->to_resolve, rp, 1);
707 rp->l_addr.as_u32 = l_addr.as_u32;
709 rp->sw_if_index = sw_if_index;
713 rp->addr_only = addr_only;
715 rp->twice_nat = twice_nat;
716 rp->out2in_only = out2in_only;
717 rp->identity_nat = identity_nat;
718 rp->tag = vec_dup (tag);
719 rp->pool_addr = pool_addr;
724 get_thread_idx_by_port (u16 e_port)
726 snat_main_t *sm = &snat_main;
727 u32 thread_idx = sm->num_workers;
728 if (sm->num_workers > 1)
731 sm->first_worker_index +
732 sm->workers[(e_port - 1024) / sm->port_per_thread];
738 snat_static_mapping_del_sessions (snat_main_t * sm,
739 snat_main_per_thread_data_t * tsm,
740 snat_user_key_t u_key, int addr_only,
741 ip4_address_t e_addr, u16 e_port)
743 clib_bihash_kv_8_8_t kv, value;
744 kv.key = u_key.as_u64;
746 dlist_elt_t *head, *elt;
749 u32 elt_index, head_index, ses_index;
750 if (!clib_bihash_search_8_8 (&tsm->user_hash, &kv, &value))
752 user_index = value.value;
753 u = pool_elt_at_index (tsm->users, user_index);
754 if (u->nstaticsessions)
756 head_index = u->sessions_per_user_list_head_index;
757 head = pool_elt_at_index (tsm->list_pool, head_index);
758 elt_index = head->next;
759 elt = pool_elt_at_index (tsm->list_pool, elt_index);
760 ses_index = elt->value;
761 while (ses_index != ~0)
763 s = pool_elt_at_index (tsm->sessions, ses_index);
764 elt = pool_elt_at_index (tsm->list_pool, elt->next);
765 ses_index = elt->value;
769 if ((s->out2in.addr.as_u32 != e_addr.as_u32) ||
770 (s->out2in.port != e_port))
774 if (is_lb_session (s))
777 if (!snat_is_session_static (s))
780 nat_free_session_data (sm, s, tsm - sm->per_thread_data, 0);
781 nat44_delete_session (sm, s, tsm - sm->per_thread_data);
791 snat_ed_static_mapping_del_sessions (snat_main_t * sm,
792 snat_main_per_thread_data_t * tsm,
793 ip4_address_t l_addr,
796 u32 fib_index, int addr_only,
797 ip4_address_t e_addr, u16 e_port)
800 u32 *indexes_to_free = NULL;
802 pool_foreach (s, tsm->sessions, {
803 if (s->in2out.fib_index != fib_index ||
804 s->in2out.addr.as_u32 != l_addr.as_u32)
810 if ((s->out2in.addr.as_u32 != e_addr.as_u32) ||
811 s->out2in.port != e_port ||
812 s->in2out.port != l_port ||
813 s->nat_proto != protocol)
817 if (is_lb_session (s))
819 if (!snat_is_session_static (s))
821 nat_free_session_data (sm, s, tsm - sm->per_thread_data, 0);
822 vec_add1 (indexes_to_free, s - tsm->sessions);
828 vec_foreach (ses_index, indexes_to_free)
830 s = pool_elt_at_index (tsm->sessions, *ses_index);
831 nat_ed_session_delete (sm, s, tsm - sm->per_thread_data, 1);
833 vec_free (indexes_to_free);
837 snat_add_static_mapping (ip4_address_t l_addr, ip4_address_t e_addr,
838 u16 l_port, u16 e_port, u32 vrf_id, int addr_only,
839 u32 sw_if_index, nat_protocol_t proto, int is_add,
840 twice_nat_type_t twice_nat, u8 out2in_only, u8 * tag,
841 u8 identity_nat, ip4_address_t pool_addr, int exact)
843 snat_main_t *sm = &snat_main;
844 snat_static_mapping_t *m;
845 clib_bihash_kv_8_8_t kv, value;
846 snat_address_t *a = 0;
848 snat_interface_t *interface;
850 snat_main_per_thread_data_t *tsm;
851 snat_user_key_t u_key;
853 dlist_elt_t *head, *elt;
854 u32 elt_index, head_index;
858 snat_static_map_resolve_t *rp, *rp_match = 0;
859 nat44_lb_addr_port_t *local;
862 if (!sm->endpoint_dependent)
864 if (twice_nat || out2in_only)
865 return VNET_API_ERROR_FEATURE_DISABLED;
868 /* If the external address is a specific interface address */
869 if (sw_if_index != ~0)
871 ip4_address_t *first_int_addr;
873 for (i = 0; i < vec_len (sm->to_resolve); i++)
875 rp = sm->to_resolve + i;
876 if (rp->sw_if_index != sw_if_index ||
877 rp->l_addr.as_u32 != l_addr.as_u32 ||
878 rp->vrf_id != vrf_id || rp->addr_only != addr_only)
883 if ((rp->l_port != l_port && rp->e_port != e_port)
884 || rp->proto != proto)
892 /* Might be already set... */
893 first_int_addr = ip4_interface_first_address
894 (sm->ip4_main, sw_if_index, 0 /* just want the address */ );
899 return VNET_API_ERROR_VALUE_EXIST;
901 snat_add_static_mapping_when_resolved
902 (sm, l_addr, l_port, sw_if_index, e_port, vrf_id, proto,
903 addr_only, is_add, tag, twice_nat, out2in_only,
904 identity_nat, pool_addr, exact);
906 /* DHCP resolution required? */
907 if (first_int_addr == 0)
913 e_addr.as_u32 = first_int_addr->as_u32;
914 /* Identity mapping? */
915 if (l_addr.as_u32 == 0)
916 l_addr.as_u32 = e_addr.as_u32;
922 return VNET_API_ERROR_NO_SUCH_ENTRY;
924 vec_del1 (sm->to_resolve, i);
928 e_addr.as_u32 = first_int_addr->as_u32;
929 /* Identity mapping? */
930 if (l_addr.as_u32 == 0)
931 l_addr.as_u32 = e_addr.as_u32;
938 init_nat_k (&kv, e_addr, addr_only ? 0 : e_port, 0, addr_only ? 0 : proto);
939 if (clib_bihash_search_8_8 (&sm->static_mapping_by_external, &kv, &value))
942 m = pool_elt_at_index (sm->static_mappings, value.value);
948 if (is_identity_static_mapping (m))
951 pool_foreach (local, m->locals,
953 if (local->vrf_id == vrf_id)
954 return VNET_API_ERROR_VALUE_EXIST;
957 pool_get (m->locals, local);
958 local->vrf_id = vrf_id;
960 fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4, vrf_id,
962 init_nat_kv (&kv, m->local_addr, m->local_port,
963 local->fib_index, m->proto,
964 m - sm->static_mappings);
965 clib_bihash_add_del_8_8 (&sm->static_mapping_by_local, &kv, 1);
969 return VNET_API_ERROR_VALUE_EXIST;
972 if (twice_nat && addr_only)
973 return VNET_API_ERROR_UNSUPPORTED;
975 /* Convert VRF id to FIB index */
978 fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4, vrf_id,
980 /* If not specified use inside VRF id from SNAT plugin startup config */
983 fib_index = sm->inside_fib_index;
984 vrf_id = sm->inside_vrf_id;
985 fib_table_lock (fib_index, FIB_PROTOCOL_IP4, nat_fib_src_low);
988 if (!(out2in_only || identity_nat))
990 init_nat_k (&kv, l_addr, addr_only ? 0 : l_port, fib_index,
991 addr_only ? 0 : proto);
992 if (!clib_bihash_search_8_8
993 (&sm->static_mapping_by_local, &kv, &value))
994 return VNET_API_ERROR_VALUE_EXIST;
997 /* Find external address in allocated addresses and reserve port for
998 address and port pair mapping when dynamic translations enabled */
999 if (!(addr_only || sm->static_mapping_only || out2in_only))
1001 for (i = 0; i < vec_len (sm->addresses); i++)
1003 if (sm->addresses[i].addr.as_u32 == e_addr.as_u32)
1005 a = sm->addresses + i;
1006 /* External port must be unused */
1009 #define _(N, j, n, s) \
1010 case NAT_PROTOCOL_##N: \
1011 if (a->busy_##n##_port_refcounts[e_port]) \
1012 return VNET_API_ERROR_INVALID_VALUE; \
1013 ++a->busy_##n##_port_refcounts[e_port]; \
1014 if (e_port > 1024) \
1016 a->busy_##n##_ports++; \
1017 a->busy_##n##_ports_per_thread[get_thread_idx_by_port(e_port)]++; \
1020 foreach_nat_protocol
1023 nat_elog_info ("unknown protocol");
1024 return VNET_API_ERROR_INVALID_VALUE_2;
1029 /* External address must be allocated */
1030 if (!a && (l_addr.as_u32 != e_addr.as_u32))
1032 if (sw_if_index != ~0)
1034 for (i = 0; i < vec_len (sm->to_resolve); i++)
1036 rp = sm->to_resolve + i;
1039 if (rp->sw_if_index != sw_if_index &&
1040 rp->l_addr.as_u32 != l_addr.as_u32 &&
1041 rp->vrf_id != vrf_id && rp->l_port != l_port &&
1042 rp->e_port != e_port && rp->proto != proto)
1045 vec_del1 (sm->to_resolve, i);
1049 return VNET_API_ERROR_NO_SUCH_ENTRY;
1053 pool_get (sm->static_mappings, m);
1054 clib_memset (m, 0, sizeof (*m));
1055 m->tag = vec_dup (tag);
1056 m->local_addr = l_addr;
1057 m->external_addr = e_addr;
1058 m->twice_nat = twice_nat;
1060 if (twice_nat == TWICE_NAT && exact)
1062 m->flags |= NAT_STATIC_MAPPING_FLAG_EXACT_ADDRESS;
1063 m->pool_addr = pool_addr;
1067 m->flags |= NAT_STATIC_MAPPING_FLAG_OUT2IN_ONLY;
1069 m->flags |= NAT_STATIC_MAPPING_FLAG_ADDR_ONLY;
1072 m->flags |= NAT_STATIC_MAPPING_FLAG_IDENTITY_NAT;
1073 pool_get (m->locals, local);
1074 local->vrf_id = vrf_id;
1075 local->fib_index = fib_index;
1080 m->fib_index = fib_index;
1084 m->local_port = l_port;
1085 m->external_port = e_port;
1089 if (sm->num_workers > 1)
1092 .src_address = m->local_addr,
1094 vec_add1 (m->workers, sm->worker_in2out_cb (&ip, m->fib_index, 0));
1095 tsm = vec_elt_at_index (sm->per_thread_data, m->workers[0]);
1098 tsm = vec_elt_at_index (sm->per_thread_data, sm->num_workers);
1100 init_nat_kv (&kv, m->local_addr, m->local_port, fib_index, m->proto,
1101 m - sm->static_mappings);
1103 clib_bihash_add_del_8_8 (&sm->static_mapping_by_local, &kv, 1);
1105 init_nat_kv (&kv, m->external_addr, m->external_port, 0, m->proto,
1106 m - sm->static_mappings);
1107 clib_bihash_add_del_8_8 (&sm->static_mapping_by_external, &kv, 1);
1109 /* Delete dynamic sessions matching local address (+ local port) */
1110 if (!(sm->static_mapping_only))
1112 u_key.addr = m->local_addr;
1113 u_key.fib_index = m->fib_index;
1114 kv.key = u_key.as_u64;
1115 if (!clib_bihash_search_8_8 (&tsm->user_hash, &kv, &value))
1117 user_index = value.value;
1118 u = pool_elt_at_index (tsm->users, user_index);
1121 head_index = u->sessions_per_user_list_head_index;
1122 head = pool_elt_at_index (tsm->list_pool, head_index);
1123 elt_index = head->next;
1124 elt = pool_elt_at_index (tsm->list_pool, elt_index);
1125 ses_index = elt->value;
1126 while (ses_index != ~0)
1128 s = pool_elt_at_index (tsm->sessions, ses_index);
1129 elt = pool_elt_at_index (tsm->list_pool, elt->next);
1130 ses_index = elt->value;
1132 if (snat_is_session_static (s))
1135 if (!addr_only && s->in2out.port != m->local_port)
1138 nat_free_session_data (sm, s,
1139 tsm - sm->per_thread_data, 0);
1140 nat44_delete_session (sm, s, tsm - sm->per_thread_data);
1142 if (!addr_only && !sm->endpoint_dependent)
1153 if (sw_if_index != ~0)
1156 return VNET_API_ERROR_NO_SUCH_ENTRY;
1162 vrf_id = sm->inside_vrf_id;
1165 pool_foreach (local, m->locals,
1167 if (local->vrf_id == vrf_id)
1168 find = local - m->locals;
1172 return VNET_API_ERROR_NO_SUCH_ENTRY;
1174 local = pool_elt_at_index (m->locals, find);
1175 fib_index = local->fib_index;
1176 pool_put (m->locals, local);
1179 fib_index = m->fib_index;
1181 /* Free external address port */
1182 if (!(addr_only || sm->static_mapping_only || out2in_only))
1184 for (i = 0; i < vec_len (sm->addresses); i++)
1186 if (sm->addresses[i].addr.as_u32 == e_addr.as_u32)
1188 a = sm->addresses + i;
1191 #define _(N, j, n, s) \
1192 case NAT_PROTOCOL_##N: \
1193 --a->busy_##n##_port_refcounts[e_port]; \
1194 if (e_port > 1024) \
1196 a->busy_##n##_ports--; \
1197 a->busy_##n##_ports_per_thread[get_thread_idx_by_port(e_port)]--; \
1200 foreach_nat_protocol
1203 nat_elog_info ("unknown protocol");
1204 return VNET_API_ERROR_INVALID_VALUE_2;
1211 if (sm->num_workers > 1)
1212 tsm = vec_elt_at_index (sm->per_thread_data, m->workers[0]);
1214 tsm = vec_elt_at_index (sm->per_thread_data, sm->num_workers);
1216 init_nat_k (&kv, m->local_addr, m->local_port, fib_index, m->proto);
1218 clib_bihash_add_del_8_8 (&sm->static_mapping_by_local, &kv, 0);
1220 /* Delete session(s) for static mapping if exist */
1221 if (!(sm->static_mapping_only) ||
1222 (sm->static_mapping_only && sm->static_mapping_connection_tracking))
1224 if (sm->endpoint_dependent)
1226 snat_ed_static_mapping_del_sessions (sm, tsm, m->local_addr,
1227 m->local_port, m->proto,
1228 fib_index, addr_only,
1233 u_key.addr = m->local_addr;
1234 u_key.fib_index = fib_index;
1235 kv.key = u_key.as_u64;
1236 snat_static_mapping_del_sessions (sm, tsm, u_key, addr_only,
1241 fib_table_unlock (fib_index, FIB_PROTOCOL_IP4, nat_fib_src_low);
1242 if (pool_elts (m->locals))
1245 init_nat_k (&kv, m->external_addr, m->external_port, 0, m->proto);
1246 clib_bihash_add_del_8_8 (&sm->static_mapping_by_external, &kv, 0);
1249 vec_free (m->workers);
1250 /* Delete static mapping from pool */
1251 pool_put (sm->static_mappings, m);
1254 if (!addr_only || (l_addr.as_u32 == e_addr.as_u32))
1257 /* Add/delete external address to FIB */
1259 pool_foreach (interface, sm->interfaces,
1261 if (nat_interface_is_inside(interface) || sm->out2in_dpo)
1264 snat_add_del_addr_to_fib(&e_addr, 32, interface->sw_if_index, is_add);
1267 pool_foreach (interface, sm->output_feature_interfaces,
1269 if (nat_interface_is_inside(interface) || sm->out2in_dpo)
1272 snat_add_del_addr_to_fib(&e_addr, 32, interface->sw_if_index, is_add);
1281 nat44_add_del_lb_static_mapping (ip4_address_t e_addr, u16 e_port,
1282 nat_protocol_t proto,
1283 nat44_lb_addr_port_t * locals, u8 is_add,
1284 twice_nat_type_t twice_nat, u8 out2in_only,
1285 u8 * tag, u32 affinity)
1287 snat_main_t *sm = &snat_main;
1288 snat_static_mapping_t *m;
1289 clib_bihash_kv_8_8_t kv, value;
1290 snat_address_t *a = 0;
1292 nat44_lb_addr_port_t *local;
1293 snat_main_per_thread_data_t *tsm;
1297 if (!sm->endpoint_dependent)
1298 return VNET_API_ERROR_FEATURE_DISABLED;
1300 init_nat_k (&kv, e_addr, e_port, 0, proto);
1301 if (clib_bihash_search_8_8 (&sm->static_mapping_by_external, &kv, &value))
1304 m = pool_elt_at_index (sm->static_mappings, value.value);
1309 return VNET_API_ERROR_VALUE_EXIST;
1311 if (vec_len (locals) < 2)
1312 return VNET_API_ERROR_INVALID_VALUE;
1314 /* Find external address in allocated addresses and reserve port for
1315 address and port pair mapping when dynamic translations enabled */
1316 if (!(sm->static_mapping_only || out2in_only))
1318 for (i = 0; i < vec_len (sm->addresses); i++)
1320 if (sm->addresses[i].addr.as_u32 == e_addr.as_u32)
1322 a = sm->addresses + i;
1323 /* External port must be unused */
1326 #define _(N, j, n, s) \
1327 case NAT_PROTOCOL_##N: \
1328 if (a->busy_##n##_port_refcounts[e_port]) \
1329 return VNET_API_ERROR_INVALID_VALUE; \
1330 ++a->busy_##n##_port_refcounts[e_port]; \
1331 if (e_port > 1024) \
1333 a->busy_##n##_ports++; \
1334 a->busy_##n##_ports_per_thread[get_thread_idx_by_port(e_port)]++; \
1337 foreach_nat_protocol
1340 nat_elog_info ("unknown protocol");
1341 return VNET_API_ERROR_INVALID_VALUE_2;
1346 /* External address must be allocated */
1348 return VNET_API_ERROR_NO_SUCH_ENTRY;
1351 pool_get (sm->static_mappings, m);
1352 clib_memset (m, 0, sizeof (*m));
1353 m->tag = vec_dup (tag);
1354 m->external_addr = e_addr;
1355 m->external_port = e_port;
1357 m->twice_nat = twice_nat;
1358 m->flags |= NAT_STATIC_MAPPING_FLAG_LB;
1360 m->flags |= NAT_STATIC_MAPPING_FLAG_OUT2IN_ONLY;
1361 m->affinity = affinity;
1364 m->affinity_per_service_list_head_index =
1365 nat_affinity_get_per_service_list_head_index ();
1367 m->affinity_per_service_list_head_index = ~0;
1369 init_nat_kv (&kv, m->external_addr, m->external_port, 0, m->proto,
1370 m - sm->static_mappings);
1371 if (clib_bihash_add_del_8_8 (&sm->static_mapping_by_external, &kv, 1))
1373 nat_elog_err ("static_mapping_by_external key add failed");
1374 return VNET_API_ERROR_UNSPECIFIED;
1377 for (i = 0; i < vec_len (locals); i++)
1379 locals[i].fib_index =
1380 fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4,
1385 init_nat_kv (&kv, locals[i].addr, locals[i].port,
1386 locals[i].fib_index, m->proto,
1387 m - sm->static_mappings);
1388 clib_bihash_add_del_8_8 (&sm->static_mapping_by_local, &kv, 1);
1390 locals[i].prefix = (i == 0) ? locals[i].probability :
1391 (locals[i - 1].prefix + locals[i].probability);
1392 pool_get (m->locals, local);
1394 if (sm->num_workers > 1)
1397 .src_address = locals[i].addr,
1400 clib_bitmap_set (bitmap,
1401 sm->worker_in2out_cb (&ip, m->fib_index, 0),
1406 /* Assign workers */
1407 if (sm->num_workers > 1)
1410 clib_bitmap_foreach (i, bitmap,
1412 vec_add1(m->workers, i);
1420 return VNET_API_ERROR_NO_SUCH_ENTRY;
1422 if (!is_lb_static_mapping (m))
1423 return VNET_API_ERROR_INVALID_VALUE;
1425 /* Free external address port */
1426 if (!(sm->static_mapping_only || out2in_only))
1428 for (i = 0; i < vec_len (sm->addresses); i++)
1430 if (sm->addresses[i].addr.as_u32 == e_addr.as_u32)
1432 a = sm->addresses + i;
1435 #define _(N, j, n, s) \
1436 case NAT_PROTOCOL_##N: \
1437 --a->busy_##n##_port_refcounts[e_port]; \
1438 if (e_port > 1024) \
1440 a->busy_##n##_ports--; \
1441 a->busy_##n##_ports_per_thread[get_thread_idx_by_port(e_port)]--; \
1444 foreach_nat_protocol
1447 nat_elog_info ("unknown protocol");
1448 return VNET_API_ERROR_INVALID_VALUE_2;
1455 init_nat_k (&kv, m->external_addr, m->external_port, 0, m->proto);
1456 if (clib_bihash_add_del_8_8 (&sm->static_mapping_by_external, &kv, 0))
1458 nat_elog_err ("static_mapping_by_external key del failed");
1459 return VNET_API_ERROR_UNSPECIFIED;
1463 pool_foreach (local, m->locals,
1465 fib_table_unlock (local->fib_index, FIB_PROTOCOL_IP4,
1469 init_nat_k(& kv, local->addr, local->port, local->fib_index, m->proto);
1470 if (clib_bihash_add_del_8_8(&sm->static_mapping_by_local, &kv, 0))
1472 nat_elog_err ("static_mapping_by_local key del failed");
1473 return VNET_API_ERROR_UNSPECIFIED;
1477 if (sm->num_workers > 1)
1480 .src_address = local->addr,
1482 tsm = vec_elt_at_index (sm->per_thread_data,
1483 sm->worker_in2out_cb (&ip, m->fib_index, 0));
1486 tsm = vec_elt_at_index (sm->per_thread_data, sm->num_workers);
1488 /* Delete sessions */
1489 pool_foreach (s, tsm->sessions, {
1490 if (!(is_lb_session (s)))
1493 if ((s->in2out.addr.as_u32 != local->addr.as_u32) ||
1494 s->in2out.port != local->port)
1497 nat_free_session_data (sm, s, tsm - sm->per_thread_data, 0);
1498 nat_ed_session_delete (sm, s, tsm - sm->per_thread_data, 1);
1503 nat_affinity_flush_service (m->affinity_per_service_list_head_index);
1504 pool_free (m->locals);
1506 vec_free (m->workers);
1508 pool_put (sm->static_mappings, m);
1515 nat44_lb_static_mapping_add_del_local (ip4_address_t e_addr, u16 e_port,
1516 ip4_address_t l_addr, u16 l_port,
1517 nat_protocol_t proto, u32 vrf_id,
1518 u8 probability, u8 is_add)
1520 snat_main_t *sm = &snat_main;
1521 snat_static_mapping_t *m = 0;
1522 clib_bihash_kv_8_8_t kv, value;
1523 nat44_lb_addr_port_t *local, *prev_local, *match_local = 0;
1524 snat_main_per_thread_data_t *tsm;
1530 if (!sm->endpoint_dependent)
1531 return VNET_API_ERROR_FEATURE_DISABLED;
1533 init_nat_k (&kv, e_addr, e_port, 0, proto);
1534 if (!clib_bihash_search_8_8 (&sm->static_mapping_by_external, &kv, &value))
1535 m = pool_elt_at_index (sm->static_mappings, value.value);
1538 return VNET_API_ERROR_NO_SUCH_ENTRY;
1540 if (!is_lb_static_mapping (m))
1541 return VNET_API_ERROR_INVALID_VALUE;
1544 pool_foreach (local, m->locals,
1546 if ((local->addr.as_u32 == l_addr.as_u32) && (local->port == l_port) &&
1547 (local->vrf_id == vrf_id))
1549 match_local = local;
1558 return VNET_API_ERROR_VALUE_EXIST;
1560 pool_get (m->locals, local);
1561 clib_memset (local, 0, sizeof (*local));
1562 local->addr.as_u32 = l_addr.as_u32;
1563 local->port = l_port;
1564 local->probability = probability;
1565 local->vrf_id = vrf_id;
1567 fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4, vrf_id,
1570 if (!is_out2in_only_static_mapping (m))
1572 init_nat_kv (&kv, l_addr, l_port, local->fib_index, proto,
1573 m - sm->static_mappings);
1574 if (clib_bihash_add_del_8_8 (&sm->static_mapping_by_local, &kv, 1))
1575 nat_elog_err ("static_mapping_by_local key add failed");
1581 return VNET_API_ERROR_NO_SUCH_ENTRY;
1583 if (pool_elts (m->locals) < 3)
1584 return VNET_API_ERROR_UNSPECIFIED;
1586 fib_table_unlock (match_local->fib_index, FIB_PROTOCOL_IP4,
1589 if (!is_out2in_only_static_mapping (m))
1591 init_nat_k (&kv, l_addr, l_port, match_local->fib_index, proto);
1592 if (clib_bihash_add_del_8_8 (&sm->static_mapping_by_local, &kv, 0))
1593 nat_elog_err ("static_mapping_by_local key del failed");
1596 if (sm->num_workers > 1)
1599 .src_address = local->addr,
1601 tsm = vec_elt_at_index (sm->per_thread_data,
1602 sm->worker_in2out_cb (&ip, m->fib_index,
1606 tsm = vec_elt_at_index (sm->per_thread_data, sm->num_workers);
1608 /* Delete sessions */
1610 pool_foreach (s, tsm->sessions, {
1611 if (!(is_lb_session (s)))
1614 if ((s->in2out.addr.as_u32 != match_local->addr.as_u32) ||
1615 s->in2out.port != match_local->port)
1618 nat_free_session_data (sm, s, tsm - sm->per_thread_data, 0);
1619 nat_ed_session_delete (sm, s, tsm - sm->per_thread_data, 1);
1623 pool_put (m->locals, match_local);
1626 vec_free (m->workers);
1629 pool_foreach (local, m->locals,
1631 vec_add1 (locals, local - m->locals);
1632 if (sm->num_workers > 1)
1635 ip.src_address.as_u32 = local->addr.as_u32,
1636 bitmap = clib_bitmap_set (bitmap,
1637 sm->worker_in2out_cb (&ip, local->fib_index, 0),
1643 ASSERT (vec_len (locals) > 1);
1645 local = pool_elt_at_index (m->locals, locals[0]);
1646 local->prefix = local->probability;
1647 for (i = 1; i < vec_len (locals); i++)
1649 local = pool_elt_at_index (m->locals, locals[i]);
1650 prev_local = pool_elt_at_index (m->locals, locals[i - 1]);
1651 local->prefix = local->probability + prev_local->prefix;
1654 /* Assign workers */
1655 if (sm->num_workers > 1)
1658 clib_bitmap_foreach (i, bitmap, ({ vec_add1(m->workers, i); }));
1666 snat_del_address (snat_main_t * sm, ip4_address_t addr, u8 delete_sm,
1669 snat_address_t *a = 0;
1670 snat_session_t *ses;
1671 u32 *ses_to_be_removed = 0, *ses_index;
1672 snat_main_per_thread_data_t *tsm;
1673 snat_static_mapping_t *m;
1674 snat_interface_t *interface;
1676 snat_address_t *addresses =
1677 twice_nat ? sm->twice_nat_addresses : sm->addresses;
1679 /* Find SNAT address */
1680 for (i = 0; i < vec_len (addresses); i++)
1682 if (addresses[i].addr.as_u32 == addr.as_u32)
1689 return VNET_API_ERROR_NO_SUCH_ENTRY;
1693 ip4_address_t pool_addr = { 0 };
1695 pool_foreach (m, sm->static_mappings,
1697 if (m->external_addr.as_u32 == addr.as_u32)
1698 (void) snat_add_static_mapping (m->local_addr, m->external_addr,
1699 m->local_port, m->external_port,
1701 is_addr_only_static_mapping(m), ~0,
1702 m->proto, 0 /* is_add */,
1704 is_out2in_only_static_mapping(m),
1706 is_identity_static_mapping(m),
1713 /* Check if address is used in some static mapping */
1714 if (is_snat_address_used_in_static_mapping (sm, addr))
1716 nat_elog_notice ("address used in static mapping");
1717 return VNET_API_ERROR_UNSPECIFIED;
1721 if (a->fib_index != ~0)
1722 fib_table_unlock (a->fib_index, FIB_PROTOCOL_IP4, nat_fib_src_low);
1724 /* Delete sessions using address */
1725 if (a->busy_tcp_ports || a->busy_udp_ports || a->busy_icmp_ports)
1728 vec_foreach (tsm, sm->per_thread_data)
1730 pool_foreach (ses, tsm->sessions, ({
1731 if (ses->out2in.addr.as_u32 == addr.as_u32)
1733 nat_free_session_data (sm, ses, tsm - sm->per_thread_data, 0);
1734 vec_add1 (ses_to_be_removed, ses - tsm->sessions);
1738 if (sm->endpoint_dependent){
1739 vec_foreach (ses_index, ses_to_be_removed)
1741 ses = pool_elt_at_index (tsm->sessions, ses_index[0]);
1742 nat_ed_session_delete (sm, ses, tsm - sm->per_thread_data, 1);
1745 vec_foreach (ses_index, ses_to_be_removed)
1747 ses = pool_elt_at_index (tsm->sessions, ses_index[0]);
1748 nat44_delete_session (sm, ses, tsm - sm->per_thread_data);
1752 vec_free (ses_to_be_removed);
1757 #define _(N, i, n, s) \
1758 vec_free (a->busy_##n##_ports_per_thread);
1759 foreach_nat_protocol
1763 vec_del1 (sm->twice_nat_addresses, i);
1767 vec_del1 (sm->addresses, i);
1769 /* Delete external address from FIB */
1771 pool_foreach (interface, sm->interfaces,
1773 if (nat_interface_is_inside(interface) || sm->out2in_dpo)
1776 snat_add_del_addr_to_fib(&addr, 32, interface->sw_if_index, 0);
1779 pool_foreach (interface, sm->output_feature_interfaces,
1781 if (nat_interface_is_inside(interface) || sm->out2in_dpo)
1784 snat_add_del_addr_to_fib(&addr, 32, interface->sw_if_index, 0);
1793 nat_validate_counters (snat_main_t * sm, u32 sw_if_index)
1796 vlib_validate_simple_counter (&sm->counters.fastpath.in2out.x, \
1798 vlib_zero_simple_counter (&sm->counters.fastpath.in2out.x, sw_if_index); \
1799 vlib_validate_simple_counter (&sm->counters.fastpath.out2in.x, \
1801 vlib_zero_simple_counter (&sm->counters.fastpath.out2in.x, sw_if_index); \
1802 vlib_validate_simple_counter (&sm->counters.slowpath.in2out.x, \
1804 vlib_zero_simple_counter (&sm->counters.slowpath.in2out.x, sw_if_index); \
1805 vlib_validate_simple_counter (&sm->counters.slowpath.out2in.x, \
1807 vlib_zero_simple_counter (&sm->counters.slowpath.out2in.x, sw_if_index); \
1808 vlib_validate_simple_counter (&sm->counters.fastpath.in2out_ed.x, \
1810 vlib_zero_simple_counter (&sm->counters.fastpath.in2out_ed.x, sw_if_index); \
1811 vlib_validate_simple_counter (&sm->counters.fastpath.out2in_ed.x, \
1813 vlib_zero_simple_counter (&sm->counters.fastpath.out2in_ed.x, sw_if_index); \
1814 vlib_validate_simple_counter (&sm->counters.slowpath.in2out_ed.x, \
1816 vlib_zero_simple_counter (&sm->counters.slowpath.in2out_ed.x, sw_if_index); \
1817 vlib_validate_simple_counter (&sm->counters.slowpath.out2in_ed.x, \
1819 vlib_zero_simple_counter (&sm->counters.slowpath.out2in_ed.x, sw_if_index);
1820 foreach_nat_counter;
1822 vlib_validate_simple_counter (&sm->counters.hairpinning, sw_if_index);
1823 vlib_zero_simple_counter (&sm->counters.hairpinning, sw_if_index);
1827 expire_per_vrf_sessions (u32 fib_index)
1829 per_vrf_sessions_t *per_vrf_sessions;
1830 snat_main_per_thread_data_t *tsm;
1831 snat_main_t *sm = &snat_main;
1834 vec_foreach (tsm, sm->per_thread_data)
1836 vec_foreach (per_vrf_sessions, tsm->per_vrf_sessions_vec)
1838 if ((per_vrf_sessions->rx_fib_index == fib_index) ||
1839 (per_vrf_sessions->tx_fib_index == fib_index))
1841 per_vrf_sessions->expired = 1;
1849 update_per_vrf_sessions_vec (u32 fib_index, int is_del)
1851 snat_main_t *sm = &snat_main;
1854 // we don't care if it is outside/inside fib
1855 // we just care about their ref_count
1856 // if it reaches 0 sessions should expire
1857 // because the fib isn't valid for NAT anymore
1859 vec_foreach (fib, sm->fibs)
1861 if (fib->fib_index == fib_index)
1866 if (!fib->ref_count)
1868 vec_del1 (sm->fibs, fib - sm->fibs);
1869 expire_per_vrf_sessions (fib_index);
1879 vec_add2 (sm->fibs, fib, 1);
1881 fib->fib_index = fib_index;
1886 snat_interface_add_del (u32 sw_if_index, u8 is_inside, int is_del)
1888 snat_main_t *sm = &snat_main;
1889 snat_interface_t *i;
1890 const char *feature_name, *del_feature_name;
1892 snat_static_mapping_t *m;
1893 nat_outside_fib_t *outside_fib;
1894 u32 fib_index = fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4,
1897 if (sm->out2in_dpo && !is_inside)
1898 return VNET_API_ERROR_UNSUPPORTED;
1901 pool_foreach (i, sm->output_feature_interfaces,
1903 if (i->sw_if_index == sw_if_index)
1904 return VNET_API_ERROR_VALUE_EXIST;
1908 if (sm->static_mapping_only && !(sm->static_mapping_connection_tracking))
1909 feature_name = is_inside ? "nat44-in2out-fast" : "nat44-out2in-fast";
1912 if (sm->num_workers > 1)
1914 is_inside ? "nat44-in2out-worker-handoff" :
1915 "nat44-out2in-worker-handoff";
1916 else if (sm->endpoint_dependent)
1918 feature_name = is_inside ? "nat-pre-in2out" : "nat-pre-out2in";
1921 feature_name = is_inside ? "nat44-in2out" : "nat44-out2in";
1924 if (sm->fq_in2out_index == ~0 && sm->num_workers > 1)
1925 sm->fq_in2out_index =
1926 vlib_frame_queue_main_init (sm->in2out_node_index, NAT_FQ_NELTS);
1928 if (sm->fq_out2in_index == ~0 && sm->num_workers > 1)
1929 sm->fq_out2in_index =
1930 vlib_frame_queue_main_init (sm->out2in_node_index, NAT_FQ_NELTS);
1932 if (sm->endpoint_dependent)
1933 update_per_vrf_sessions_vec (fib_index, is_del);
1938 vec_foreach (outside_fib, sm->outside_fibs)
1940 if (outside_fib->fib_index == fib_index)
1944 outside_fib->refcount--;
1945 if (!outside_fib->refcount)
1946 vec_del1 (sm->outside_fibs, outside_fib - sm->outside_fibs);
1949 outside_fib->refcount++;
1956 vec_add2 (sm->outside_fibs, outside_fib, 1);
1957 outside_fib->refcount = 1;
1958 outside_fib->fib_index = fib_index;
1964 pool_foreach (i, sm->interfaces,
1966 if (i->sw_if_index == sw_if_index)
1970 if (nat_interface_is_inside(i) && nat_interface_is_outside(i))
1973 i->flags &= ~NAT_INTERFACE_FLAG_IS_INSIDE;
1975 i->flags &= ~NAT_INTERFACE_FLAG_IS_OUTSIDE;
1977 if (sm->num_workers > 1)
1979 del_feature_name = "nat44-handoff-classify";
1980 feature_name = !is_inside ? "nat44-in2out-worker-handoff" :
1981 "nat44-out2in-worker-handoff";
1983 else if (sm->endpoint_dependent)
1985 del_feature_name = "nat44-ed-classify";
1986 feature_name = !is_inside ? "nat-pre-in2out" :
1991 del_feature_name = "nat44-classify";
1992 feature_name = !is_inside ? "nat44-in2out" : "nat44-out2in";
1995 int rv = ip4_sv_reass_enable_disable_with_refcnt (sw_if_index, 0);
1998 vnet_feature_enable_disable ("ip4-unicast", del_feature_name,
1999 sw_if_index, 0, 0, 0);
2000 vnet_feature_enable_disable ("ip4-unicast", feature_name,
2001 sw_if_index, 1, 0, 0);
2004 if (sm->endpoint_dependent)
2005 vnet_feature_enable_disable ("ip4-local",
2006 "nat44-ed-hairpinning",
2007 sw_if_index, 1, 0, 0);
2009 vnet_feature_enable_disable ("ip4-local",
2010 "nat44-hairpinning",
2011 sw_if_index, 1, 0, 0);
2016 int rv = ip4_sv_reass_enable_disable_with_refcnt (sw_if_index, 0);
2019 vnet_feature_enable_disable ("ip4-unicast", feature_name,
2020 sw_if_index, 0, 0, 0);
2021 pool_put (sm->interfaces, i);
2024 if (sm->endpoint_dependent)
2025 vnet_feature_enable_disable ("ip4-local",
2026 "nat44-ed-hairpinning",
2027 sw_if_index, 0, 0, 0);
2029 vnet_feature_enable_disable ("ip4-local",
2030 "nat44-hairpinning",
2031 sw_if_index, 0, 0, 0);
2037 if ((nat_interface_is_inside(i) && is_inside) ||
2038 (nat_interface_is_outside(i) && !is_inside))
2041 if (sm->num_workers > 1)
2043 del_feature_name = !is_inside ? "nat44-in2out-worker-handoff" :
2044 "nat44-out2in-worker-handoff";
2045 feature_name = "nat44-handoff-classify";
2047 else if (sm->endpoint_dependent)
2049 del_feature_name = !is_inside ? "nat-pre-in2out" :
2052 feature_name = "nat44-ed-classify";
2056 del_feature_name = !is_inside ? "nat44-in2out" : "nat44-out2in";
2057 feature_name = "nat44-classify";
2060 int rv = ip4_sv_reass_enable_disable_with_refcnt (sw_if_index, 1);
2063 vnet_feature_enable_disable ("ip4-unicast", del_feature_name,
2064 sw_if_index, 0, 0, 0);
2065 vnet_feature_enable_disable ("ip4-unicast", feature_name,
2066 sw_if_index, 1, 0, 0);
2069 if (sm->endpoint_dependent)
2070 vnet_feature_enable_disable ("ip4-local", "nat44-ed-hairpinning",
2071 sw_if_index, 0, 0, 0);
2073 vnet_feature_enable_disable ("ip4-local", "nat44-hairpinning",
2074 sw_if_index, 0, 0, 0);
2085 return VNET_API_ERROR_NO_SUCH_ENTRY;
2087 pool_get (sm->interfaces, i);
2088 i->sw_if_index = sw_if_index;
2090 nat_validate_counters (sm, sw_if_index);
2092 vnet_feature_enable_disable ("ip4-unicast", feature_name, sw_if_index, 1, 0,
2095 int rv = ip4_sv_reass_enable_disable_with_refcnt (sw_if_index, 1);
2099 if (is_inside && !sm->out2in_dpo)
2101 if (sm->endpoint_dependent)
2102 vnet_feature_enable_disable ("ip4-local", "nat44-ed-hairpinning",
2103 sw_if_index, 1, 0, 0);
2105 vnet_feature_enable_disable ("ip4-local", "nat44-hairpinning",
2106 sw_if_index, 1, 0, 0);
2112 i->flags |= NAT_INTERFACE_FLAG_IS_INSIDE;
2116 i->flags |= NAT_INTERFACE_FLAG_IS_OUTSIDE;
2118 /* Add/delete external addresses to FIB */
2121 vec_foreach (ap, sm->addresses)
2122 snat_add_del_addr_to_fib(&ap->addr, 32, sw_if_index, !is_del);
2124 pool_foreach (m, sm->static_mappings,
2126 if (!(is_addr_only_static_mapping(m)) || (m->local_addr.as_u32 == m->external_addr.as_u32))
2129 snat_add_del_addr_to_fib(&m->external_addr, 32, sw_if_index, !is_del);
2137 snat_interface_add_del_output_feature (u32 sw_if_index,
2138 u8 is_inside, int is_del)
2140 snat_main_t *sm = &snat_main;
2141 snat_interface_t *i;
2143 snat_static_mapping_t *m;
2144 nat_outside_fib_t *outside_fib;
2145 u32 fib_index = fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4,
2148 if (sm->static_mapping_only && !(sm->static_mapping_connection_tracking))
2149 return VNET_API_ERROR_UNSUPPORTED;
2152 pool_foreach (i, sm->interfaces,
2154 if (i->sw_if_index == sw_if_index)
2155 return VNET_API_ERROR_VALUE_EXIST;
2159 if (sm->endpoint_dependent)
2160 update_per_vrf_sessions_vec (fib_index, is_del);
2165 vec_foreach (outside_fib, sm->outside_fibs)
2167 if (outside_fib->fib_index == fib_index)
2171 outside_fib->refcount--;
2172 if (!outside_fib->refcount)
2173 vec_del1 (sm->outside_fibs, outside_fib - sm->outside_fibs);
2176 outside_fib->refcount++;
2183 vec_add2 (sm->outside_fibs, outside_fib, 1);
2184 outside_fib->refcount = 1;
2185 outside_fib->fib_index = fib_index;
2192 if (sm->endpoint_dependent)
2195 ip4_sv_reass_enable_disable_with_refcnt (sw_if_index, !is_del);
2199 ip4_sv_reass_output_enable_disable_with_refcnt (sw_if_index,
2203 vnet_feature_enable_disable ("ip4-unicast", "nat44-ed-hairpin-dst",
2204 sw_if_index, !is_del, 0, 0);
2205 vnet_feature_enable_disable ("ip4-output", "nat44-ed-hairpin-src",
2206 sw_if_index, !is_del, 0, 0);
2211 ip4_sv_reass_enable_disable_with_refcnt (sw_if_index, !is_del);
2215 ip4_sv_reass_output_enable_disable_with_refcnt (sw_if_index,
2219 vnet_feature_enable_disable ("ip4-unicast", "nat44-hairpin-dst",
2220 sw_if_index, !is_del, 0, 0);
2221 vnet_feature_enable_disable ("ip4-output", "nat44-hairpin-src",
2222 sw_if_index, !is_del, 0, 0);
2227 if (sm->num_workers > 1)
2229 int rv = ip4_sv_reass_enable_disable_with_refcnt (sw_if_index, !is_del);
2233 ip4_sv_reass_output_enable_disable_with_refcnt (sw_if_index, !is_del);
2236 vnet_feature_enable_disable ("ip4-unicast",
2237 "nat44-out2in-worker-handoff",
2238 sw_if_index, !is_del, 0, 0);
2239 vnet_feature_enable_disable ("ip4-output",
2240 "nat44-in2out-output-worker-handoff",
2241 sw_if_index, !is_del, 0, 0);
2245 if (sm->endpoint_dependent)
2248 ip4_sv_reass_enable_disable_with_refcnt (sw_if_index, !is_del);
2252 ip4_sv_reass_output_enable_disable_with_refcnt (sw_if_index,
2256 vnet_feature_enable_disable ("ip4-unicast", "nat-pre-out2in",
2257 sw_if_index, !is_del, 0, 0);
2258 vnet_feature_enable_disable ("ip4-output", "nat-pre-in2out-output",
2259 sw_if_index, !is_del, 0, 0);
2264 ip4_sv_reass_enable_disable_with_refcnt (sw_if_index, !is_del);
2268 ip4_sv_reass_output_enable_disable_with_refcnt (sw_if_index,
2272 vnet_feature_enable_disable ("ip4-unicast", "nat44-out2in",
2273 sw_if_index, !is_del, 0, 0);
2274 vnet_feature_enable_disable ("ip4-output", "nat44-in2out-output",
2275 sw_if_index, !is_del, 0, 0);
2280 if (sm->fq_in2out_output_index == ~0 && sm->num_workers > 1)
2281 sm->fq_in2out_output_index =
2282 vlib_frame_queue_main_init (sm->in2out_output_node_index, 0);
2284 if (sm->fq_out2in_index == ~0 && sm->num_workers > 1)
2285 sm->fq_out2in_index =
2286 vlib_frame_queue_main_init (sm->out2in_node_index, 0);
2289 pool_foreach (i, sm->output_feature_interfaces,
2291 if (i->sw_if_index == sw_if_index)
2294 pool_put (sm->output_feature_interfaces, i);
2296 return VNET_API_ERROR_VALUE_EXIST;
2304 return VNET_API_ERROR_NO_SUCH_ENTRY;
2306 pool_get (sm->output_feature_interfaces, i);
2307 i->sw_if_index = sw_if_index;
2309 nat_validate_counters (sm, sw_if_index);
2311 i->flags |= NAT_INTERFACE_FLAG_IS_INSIDE;
2313 i->flags |= NAT_INTERFACE_FLAG_IS_OUTSIDE;
2315 /* Add/delete external addresses to FIB */
2321 vec_foreach (ap, sm->addresses)
2322 snat_add_del_addr_to_fib(&ap->addr, 32, sw_if_index, !is_del);
2324 pool_foreach (m, sm->static_mappings,
2326 if (!((is_addr_only_static_mapping(m))) || (m->local_addr.as_u32 == m->external_addr.as_u32))
2329 snat_add_del_addr_to_fib(&m->external_addr, 32, sw_if_index, !is_del);
2337 snat_set_workers (uword * bitmap)
2339 snat_main_t *sm = &snat_main;
2342 if (sm->num_workers < 2)
2343 return VNET_API_ERROR_FEATURE_DISABLED;
2345 if (clib_bitmap_last_set (bitmap) >= sm->num_workers)
2346 return VNET_API_ERROR_INVALID_WORKER;
2348 vec_free (sm->workers);
2350 clib_bitmap_foreach (i, bitmap,
2352 vec_add1(sm->workers, i);
2353 sm->per_thread_data[sm->first_worker_index + i].snat_thread_index = j;
2354 sm->per_thread_data[sm->first_worker_index + i].thread_index = i;
2359 sm->port_per_thread = (0xffff - 1024) / _vec_len (sm->workers);
2365 snat_update_outside_fib (u32 sw_if_index, u32 new_fib_index,
2368 snat_main_t *sm = &snat_main;
2369 nat_outside_fib_t *outside_fib;
2370 snat_interface_t *i;
2374 if (new_fib_index == old_fib_index)
2377 if (!vec_len (sm->outside_fibs))
2381 pool_foreach (i, sm->interfaces,
2383 if (i->sw_if_index == sw_if_index)
2385 if (!(nat_interface_is_outside (i)))
2391 pool_foreach (i, sm->output_feature_interfaces,
2393 if (i->sw_if_index == sw_if_index)
2395 if (!(nat_interface_is_outside (i)))
2405 vec_foreach (outside_fib, sm->outside_fibs)
2407 if (outside_fib->fib_index == old_fib_index)
2409 outside_fib->refcount--;
2410 if (!outside_fib->refcount)
2411 vec_del1 (sm->outside_fibs, outside_fib - sm->outside_fibs);
2416 vec_foreach (outside_fib, sm->outside_fibs)
2418 if (outside_fib->fib_index == new_fib_index)
2420 outside_fib->refcount++;
2428 vec_add2 (sm->outside_fibs, outside_fib, 1);
2429 outside_fib->refcount = 1;
2430 outside_fib->fib_index = new_fib_index;
2435 snat_ip4_table_bind (ip4_main_t * im,
2437 u32 sw_if_index, u32 new_fib_index, u32 old_fib_index)
2439 snat_update_outside_fib (sw_if_index, new_fib_index, old_fib_index);
2443 snat_ip4_add_del_interface_address_cb (ip4_main_t * im,
2446 ip4_address_t * address,
2448 u32 if_address_index, u32 is_delete);
2451 nat_ip4_add_del_addr_only_sm_cb (ip4_main_t * im,
2454 ip4_address_t * address,
2456 u32 if_address_index, u32 is_delete);
2459 nat_alloc_addr_and_port_default (snat_address_t * addresses, u32 fib_index,
2460 u32 thread_index, nat_protocol_t proto,
2461 ip4_address_t * addr, u16 * port,
2462 u16 port_per_thread, u32 snat_thread_index);
2465 test_key_calc_split ()
2467 ip4_address_t l_addr;
2468 l_addr.as_u8[0] = 1;
2469 l_addr.as_u8[1] = 1;
2470 l_addr.as_u8[2] = 1;
2471 l_addr.as_u8[3] = 1;
2472 ip4_address_t r_addr;
2473 r_addr.as_u8[0] = 2;
2474 r_addr.as_u8[1] = 2;
2475 r_addr.as_u8[2] = 2;
2476 r_addr.as_u8[3] = 2;
2480 u32 fib_index = 9000001;
2481 u32 thread_index = 3000000001;
2482 u32 session_index = 3000000221;
2483 clib_bihash_kv_16_8_t kv;
2484 init_ed_kv (&kv, l_addr, l_port, r_addr, r_port, fib_index, proto,
2485 thread_index, session_index);
2486 ip4_address_t l_addr2;
2487 ip4_address_t r_addr2;
2488 clib_memset (&l_addr2, 0, sizeof (l_addr2));
2489 clib_memset (&r_addr2, 0, sizeof (r_addr2));
2494 split_ed_kv (&kv, &l_addr2, &r_addr2, &proto2, &fib_index2, &l_port2,
2496 ASSERT (l_addr.as_u32 == l_addr2.as_u32);
2497 ASSERT (r_addr.as_u32 == r_addr2.as_u32);
2498 ASSERT (l_port == l_port2);
2499 ASSERT (r_port == r_port2);
2500 ASSERT (proto == proto2);
2501 ASSERT (fib_index == fib_index2);
2502 ASSERT (thread_index == ed_value_get_thread_index (&kv));
2503 ASSERT (session_index == ed_value_get_session_index (&kv));
2507 nat_protocol_t proto3 = ~0;
2508 u64 key = calc_nat_key (l_addr, l_port, fib_index, proto);
2509 split_nat_key (key, &l_addr2, &l_port2, &fib_index2, &proto3);
2510 ASSERT (l_addr.as_u32 == l_addr2.as_u32);
2511 ASSERT (l_port == l_port2);
2512 ASSERT (proto == proto3);
2513 ASSERT (fib_index == fib_index2);
2516 static clib_error_t *
2517 nat_ip_table_add_del (vnet_main_t * vnm, u32 table_id, u32 is_add)
2519 snat_main_t *sm = &snat_main;
2522 if (sm->endpoint_dependent)
2524 // TODO: consider removing all NAT interfaces
2528 fib_index = ip4_fib_index_from_table_id (table_id);
2529 if (fib_index != ~0)
2530 expire_per_vrf_sessions (fib_index);
2536 VNET_IP_TABLE_ADD_DEL_FUNCTION (nat_ip_table_add_del);
2539 static clib_error_t *
2540 snat_init (vlib_main_t * vm)
2542 snat_main_t *sm = &snat_main;
2543 clib_error_t *error = 0;
2544 ip4_main_t *im = &ip4_main;
2545 ip_lookup_main_t *lm = &im->lookup_main;
2547 vlib_thread_registration_t *tr;
2548 vlib_thread_main_t *tm = vlib_get_thread_main ();
2551 ip4_add_del_interface_address_callback_t cb4;
2554 sm->vnet_main = vnet_get_main ();
2556 sm->ip4_lookup_main = lm;
2557 sm->api_main = vlibapi_get_main ();
2558 sm->first_worker_index = 0;
2559 sm->num_workers = 0;
2561 sm->port_per_thread = 0xffff - 1024;
2562 sm->fq_in2out_index = ~0;
2563 sm->fq_in2out_output_index = ~0;
2564 sm->fq_out2in_index = ~0;
2566 sm->alloc_addr_and_port = nat_alloc_addr_and_port_default;
2567 sm->addr_and_port_alloc_alg = NAT_ADDR_AND_PORT_ALLOC_ALG_DEFAULT;
2568 sm->forwarding_enabled = 0;
2569 sm->log_class = vlib_log_register_class ("nat", 0);
2570 sm->log_level = SNAT_LOG_ERROR;
2571 sm->mss_clamping = 0;
2573 node = vlib_get_node_by_name (vm, (u8 *) "error-drop");
2574 sm->error_node_index = node->index;
2576 node = vlib_get_node_by_name (vm, (u8 *) "nat-pre-in2out");
2577 sm->pre_in2out_node_index = node->index;
2578 node = vlib_get_node_by_name (vm, (u8 *) "nat-pre-out2in");
2579 sm->pre_out2in_node_index = node->index;
2581 node = vlib_get_node_by_name (vm, (u8 *) "nat-pre-in2out");
2582 sm->pre_in2out_node_index = node->index;
2584 node = vlib_get_node_by_name (vm, (u8 *) "nat-pre-out2in");
2585 sm->pre_out2in_node_index = node->index;
2587 node = vlib_get_node_by_name (vm, (u8 *) "nat44-in2out");
2588 sm->in2out_node_index = node->index;
2589 node = vlib_get_node_by_name (vm, (u8 *) "nat44-in2out-output");
2590 sm->in2out_output_node_index = node->index;
2591 node = vlib_get_node_by_name (vm, (u8 *) "nat44-in2out-fast");
2592 sm->in2out_fast_node_index = node->index;
2593 node = vlib_get_node_by_name (vm, (u8 *) "nat44-in2out-slowpath");
2594 sm->in2out_slowpath_node_index = node->index;
2595 node = vlib_get_node_by_name (vm, (u8 *) "nat44-in2out-output-slowpath");
2596 sm->in2out_slowpath_output_node_index = node->index;
2598 node = vlib_get_node_by_name (vm, (u8 *) "nat44-ed-in2out");
2599 sm->ed_in2out_node_index = node->index;
2600 node = vlib_get_node_by_name (vm, (u8 *) "nat44-ed-in2out-slowpath");
2601 sm->ed_in2out_slowpath_node_index = node->index;
2603 node = vlib_get_node_by_name (vm, (u8 *) "nat44-out2in");
2604 sm->out2in_node_index = node->index;
2605 node = vlib_get_node_by_name (vm, (u8 *) "nat44-out2in-fast");
2606 sm->out2in_fast_node_index = node->index;
2608 node = vlib_get_node_by_name (vm, (u8 *) "nat44-ed-out2in");
2609 sm->ed_out2in_node_index = node->index;
2610 node = vlib_get_node_by_name (vm, (u8 *) "nat44-ed-out2in-slowpath");
2611 sm->ed_out2in_slowpath_node_index = node->index;
2613 node = vlib_get_node_by_name (vm, (u8 *) "nat44-hairpinning");
2614 sm->hairpinning_node_index = node->index;
2615 node = vlib_get_node_by_name (vm, (u8 *) "nat44-hairpin-dst");
2616 sm->hairpin_dst_node_index = node->index;
2617 node = vlib_get_node_by_name (vm, (u8 *) "nat44-hairpin-src");
2618 sm->hairpin_src_node_index = node->index;
2619 node = vlib_get_node_by_name (vm, (u8 *) "nat44-ed-hairpinning");
2620 sm->ed_hairpinning_node_index = node->index;
2621 node = vlib_get_node_by_name (vm, (u8 *) "nat44-ed-hairpin-dst");
2622 sm->ed_hairpin_dst_node_index = node->index;
2623 node = vlib_get_node_by_name (vm, (u8 *) "nat44-ed-hairpin-src");
2624 sm->ed_hairpin_src_node_index = node->index;
2626 p = hash_get_mem (tm->thread_registrations_by_name, "workers");
2629 tr = (vlib_thread_registration_t *) p[0];
2632 sm->num_workers = tr->count;
2633 sm->first_worker_index = tr->first_index;
2637 vec_validate (sm->per_thread_data, tm->n_vlib_mains - 1);
2639 /* Use all available workers by default */
2640 if (sm->num_workers > 1)
2642 for (i = 0; i < sm->num_workers; i++)
2643 bitmap = clib_bitmap_set (bitmap, i, 1);
2644 snat_set_workers (bitmap);
2645 clib_bitmap_free (bitmap);
2649 sm->per_thread_data[0].snat_thread_index = 0;
2652 error = snat_api_init (vm, sm);
2656 /* Set up the interface address add/del callback */
2657 cb4.function = snat_ip4_add_del_interface_address_cb;
2658 cb4.function_opaque = 0;
2660 vec_add1 (im->add_del_interface_address_callbacks, cb4);
2662 cb4.function = nat_ip4_add_del_addr_only_sm_cb;
2663 cb4.function_opaque = 0;
2665 vec_add1 (im->add_del_interface_address_callbacks, cb4);
2667 nat_dpo_module_init ();
2670 sm->total_users.name = "total-users";
2671 sm->total_users.stat_segment_name = "/nat44/total-users";
2672 vlib_validate_simple_counter (&sm->total_users, 0);
2673 vlib_zero_simple_counter (&sm->total_users, 0);
2674 sm->total_sessions.name = "total-sessions";
2675 sm->total_sessions.stat_segment_name = "/nat44/total-sessions";
2676 vlib_validate_simple_counter (&sm->total_sessions, 0);
2677 vlib_zero_simple_counter (&sm->total_sessions, 0);
2678 sm->user_limit_reached.name = "user-limit-reached";
2679 sm->user_limit_reached.stat_segment_name = "/nat44/user-limit-reached";
2680 vlib_validate_simple_counter (&sm->user_limit_reached, 0);
2681 vlib_zero_simple_counter (&sm->user_limit_reached, 0);
2684 sm->counters.fastpath.in2out.x.name = #x; \
2685 sm->counters.fastpath.in2out.x.stat_segment_name = \
2686 "/nat44/in2out/fastpath/" #x; \
2687 sm->counters.slowpath.in2out.x.name = #x; \
2688 sm->counters.slowpath.in2out.x.stat_segment_name = \
2689 "/nat44/in2out/slowpath/" #x; \
2690 sm->counters.fastpath.out2in.x.name = #x; \
2691 sm->counters.fastpath.out2in.x.stat_segment_name = \
2692 "/nat44/out2in/fastpath/" #x; \
2693 sm->counters.slowpath.out2in.x.name = #x; \
2694 sm->counters.slowpath.out2in.x.stat_segment_name = \
2695 "/nat44/out2in/slowpath/" #x; \
2696 sm->counters.fastpath.in2out_ed.x.name = #x; \
2697 sm->counters.fastpath.in2out_ed.x.stat_segment_name = \
2698 "/nat44/ed/in2out/fastpath/" #x; \
2699 sm->counters.slowpath.in2out_ed.x.name = #x; \
2700 sm->counters.slowpath.in2out_ed.x.stat_segment_name = \
2701 "/nat44/ed/in2out/slowpath/" #x; \
2702 sm->counters.fastpath.out2in_ed.x.name = #x; \
2703 sm->counters.fastpath.out2in_ed.x.stat_segment_name = \
2704 "/nat44/ed/out2in/fastpath/" #x; \
2705 sm->counters.slowpath.out2in_ed.x.name = #x; \
2706 sm->counters.slowpath.out2in_ed.x.stat_segment_name = \
2707 "/nat44/ed/out2in/slowpath/" #x;
2708 foreach_nat_counter;
2710 sm->counters.hairpinning.name = "hairpinning";
2711 sm->counters.hairpinning.stat_segment_name = "/nat44/hairpinning";
2713 /* Init IPFIX logging */
2714 nat_ipfix_logging_init (vm);
2716 ip4_table_bind_callback_t cbt4 = {
2717 .function = snat_ip4_table_bind,
2719 vec_add1 (ip4_main.table_bind_callbacks, cbt4);
2721 nat_fib_src_hi = fib_source_allocate ("nat-hi",
2722 FIB_SOURCE_PRIORITY_HI,
2723 FIB_SOURCE_BH_SIMPLE);
2724 nat_fib_src_low = fib_source_allocate ("nat-low",
2725 FIB_SOURCE_PRIORITY_LOW,
2726 FIB_SOURCE_BH_SIMPLE);
2728 test_key_calc_split ();
2732 VLIB_INIT_FUNCTION (snat_init);
2735 snat_free_outside_address_and_port (snat_address_t * addresses,
2737 ip4_address_t * addr,
2738 u16 port, nat_protocol_t protocol)
2742 u16 port_host_byte_order = clib_net_to_host_u16 (port);
2744 for (address_index = 0; address_index < vec_len (addresses);
2747 if (addresses[address_index].addr.as_u32 == addr->as_u32)
2751 ASSERT (address_index < vec_len (addresses));
2753 a = addresses + address_index;
2757 #define _(N, i, n, s) \
2758 case NAT_PROTOCOL_##N: \
2759 ASSERT (a->busy_##n##_port_refcounts[port_host_byte_order] >= 1); \
2760 --a->busy_##n##_port_refcounts[port_host_byte_order]; \
2761 a->busy_##n##_ports--; \
2762 a->busy_##n##_ports_per_thread[thread_index]--; \
2764 foreach_nat_protocol
2767 nat_elog_info ("unknown protocol");
2773 nat_set_outside_address_and_port (snat_address_t * addresses,
2774 u32 thread_index, ip4_address_t addr,
2775 u16 port, nat_protocol_t protocol)
2777 snat_address_t *a = 0;
2779 u16 port_host_byte_order = clib_net_to_host_u16 (port);
2781 for (address_index = 0; address_index < vec_len (addresses);
2784 if (addresses[address_index].addr.as_u32 != addr.as_u32)
2787 a = addresses + address_index;
2790 #define _(N, j, n, s) \
2791 case NAT_PROTOCOL_##N: \
2792 if (a->busy_##n##_port_refcounts[port_host_byte_order]) \
2793 return VNET_API_ERROR_INSTANCE_IN_USE; \
2794 ++a->busy_##n##_port_refcounts[port_host_byte_order]; \
2795 a->busy_##n##_ports_per_thread[thread_index]++; \
2796 a->busy_##n##_ports++; \
2798 foreach_nat_protocol
2801 nat_elog_info ("unknown protocol");
2806 return VNET_API_ERROR_NO_SUCH_ENTRY;
2810 snat_static_mapping_match (snat_main_t * sm,
2811 ip4_address_t match_addr,
2813 u32 match_fib_index,
2814 nat_protocol_t match_protocol,
2815 ip4_address_t * mapping_addr,
2817 u32 * mapping_fib_index,
2820 twice_nat_type_t * twice_nat,
2821 lb_nat_type_t * lb, ip4_address_t * ext_host_addr,
2822 u8 * is_identity_nat, snat_static_mapping_t ** out)
2824 clib_bihash_kv_8_8_t kv, value;
2825 clib_bihash_8_8_t *mapping_hash;
2826 snat_static_mapping_t *m;
2827 u32 rand, lo = 0, hi, mid, *tmp = 0, i;
2828 nat44_lb_addr_port_t *local;
2833 mapping_hash = &sm->static_mapping_by_local;
2834 init_nat_k (&kv, match_addr, match_port, match_fib_index,
2836 if (clib_bihash_search_8_8 (mapping_hash, &kv, &value))
2838 /* Try address only mapping */
2839 init_nat_k (&kv, match_addr, 0, match_fib_index, 0);
2840 if (clib_bihash_search_8_8 (mapping_hash, &kv, &value))
2846 mapping_hash = &sm->static_mapping_by_external;
2847 init_nat_k (&kv, match_addr, match_port, 0, match_protocol);
2848 if (clib_bihash_search_8_8 (mapping_hash, &kv, &value))
2850 /* Try address only mapping */
2851 init_nat_k (&kv, match_addr, 0, 0, 0);
2852 if (clib_bihash_search_8_8 (mapping_hash, &kv, &value))
2857 m = pool_elt_at_index (sm->static_mappings, value.value);
2861 if (is_lb_static_mapping (m))
2863 if (PREDICT_FALSE (lb != 0))
2864 *lb = m->affinity ? AFFINITY_LB_NAT : LB_NAT;
2865 if (m->affinity && !nat_affinity_find_and_lock (ext_host_addr[0],
2871 local = pool_elt_at_index (m->locals, backend_index);
2872 *mapping_addr = local->addr;
2873 *mapping_port = local->port;
2874 *mapping_fib_index = local->fib_index;
2877 // pick locals matching this worker
2878 if (PREDICT_FALSE (sm->num_workers > 1))
2880 u32 thread_index = vlib_get_thread_index ();
2882 pool_foreach_index (i, m->locals,
2884 local = pool_elt_at_index (m->locals, i);
2887 .src_address = local->addr,
2890 if (sm->worker_in2out_cb (&ip, m->fib_index, 0) ==
2897 ASSERT (vec_len (tmp) != 0);
2902 pool_foreach_index (i, m->locals,
2908 hi = vec_len (tmp) - 1;
2909 local = pool_elt_at_index (m->locals, tmp[hi]);
2910 rand = 1 + (random_u32 (&sm->random_seed) % local->prefix);
2913 mid = ((hi - lo) >> 1) + lo;
2914 local = pool_elt_at_index (m->locals, tmp[mid]);
2915 (rand > local->prefix) ? (lo = mid + 1) : (hi = mid);
2917 local = pool_elt_at_index (m->locals, tmp[lo]);
2918 if (!(local->prefix >= rand))
2920 *mapping_addr = local->addr;
2921 *mapping_port = local->port;
2922 *mapping_fib_index = local->fib_index;
2925 if (nat_affinity_create_and_lock (ext_host_addr[0], match_addr,
2926 match_protocol, match_port,
2927 tmp[lo], m->affinity,
2928 m->affinity_per_service_list_head_index))
2929 nat_elog_info ("create affinity record failed");
2935 if (PREDICT_FALSE (lb != 0))
2937 *mapping_fib_index = m->fib_index;
2938 *mapping_addr = m->local_addr;
2939 /* Address only mapping doesn't change port */
2940 *mapping_port = is_addr_only_static_mapping (m) ? match_port
2946 *mapping_addr = m->external_addr;
2947 /* Address only mapping doesn't change port */
2948 *mapping_port = is_addr_only_static_mapping (m) ? match_port
2950 *mapping_fib_index = sm->outside_fib_index;
2954 if (PREDICT_FALSE (is_addr_only != 0))
2955 *is_addr_only = is_addr_only_static_mapping (m);
2957 if (PREDICT_FALSE (twice_nat != 0))
2958 *twice_nat = m->twice_nat;
2960 if (PREDICT_FALSE (is_identity_nat != 0))
2961 *is_identity_nat = is_identity_static_mapping (m);
2970 snat_alloc_outside_address_and_port (snat_address_t * addresses,
2973 nat_protocol_t proto,
2974 ip4_address_t * addr,
2976 u16 port_per_thread,
2977 u32 snat_thread_index)
2979 snat_main_t *sm = &snat_main;
2981 return sm->alloc_addr_and_port (addresses, fib_index, thread_index, proto,
2982 addr, port, port_per_thread,
2987 nat_alloc_addr_and_port_default (snat_address_t * addresses,
2990 nat_protocol_t proto,
2991 ip4_address_t * addr,
2993 u16 port_per_thread, u32 snat_thread_index)
2996 snat_address_t *a, *ga = 0;
2999 for (i = 0; i < vec_len (addresses); i++)
3004 #define _(N, j, n, s) \
3005 case NAT_PROTOCOL_##N: \
3006 if (a->busy_##n##_ports_per_thread[thread_index] < port_per_thread) \
3008 if (a->fib_index == fib_index) \
3012 portnum = (port_per_thread * \
3013 snat_thread_index) + \
3014 snat_random_port(0, port_per_thread - 1) + 1024; \
3015 if (a->busy_##n##_port_refcounts[portnum]) \
3017 --a->busy_##n##_port_refcounts[portnum]; \
3018 a->busy_##n##_ports_per_thread[thread_index]++; \
3019 a->busy_##n##_ports++; \
3021 *port = clib_host_to_net_u16(portnum); \
3025 else if (a->fib_index == ~0) \
3031 foreach_nat_protocol
3034 nat_elog_info ("unknown protocol");
3045 #define _(N, j, n, s) \
3046 case NAT_PROTOCOL_##N: \
3049 portnum = (port_per_thread * \
3050 snat_thread_index) + \
3051 snat_random_port(0, port_per_thread - 1) + 1024; \
3052 if (a->busy_##n##_port_refcounts[portnum]) \
3054 ++a->busy_##n##_port_refcounts[portnum]; \
3055 a->busy_##n##_ports_per_thread[thread_index]++; \
3056 a->busy_##n##_ports++; \
3058 *port = clib_host_to_net_u16(portnum); \
3062 foreach_nat_protocol
3065 nat_elog_info ("unknown protocol");
3070 /* Totally out of translations to use... */
3071 nat_ipfix_logging_addresses_exhausted (thread_index, 0);
3076 nat_alloc_addr_and_port_mape (snat_address_t * addresses, u32 fib_index,
3077 u32 thread_index, nat_protocol_t proto,
3078 ip4_address_t * addr, u16 * port,
3079 u16 port_per_thread, u32 snat_thread_index)
3081 snat_main_t *sm = &snat_main;
3082 snat_address_t *a = addresses;
3083 u16 m, ports, portnum, A, j;
3084 m = 16 - (sm->psid_offset + sm->psid_length);
3085 ports = (1 << (16 - sm->psid_length)) - (1 << m);
3087 if (!vec_len (addresses))
3092 #define _(N, i, n, s) \
3093 case NAT_PROTOCOL_##N: \
3094 if (a->busy_##n##_ports < ports) \
3098 A = snat_random_port(1, pow2_mask(sm->psid_offset)); \
3099 j = snat_random_port(0, pow2_mask(m)); \
3100 portnum = A | (sm->psid << sm->psid_offset) | (j << (16 - m)); \
3101 if (a->busy_##n##_port_refcounts[portnum]) \
3103 ++a->busy_##n##_port_refcounts[portnum]; \
3104 a->busy_##n##_ports++; \
3106 *port = clib_host_to_net_u16 (portnum); \
3111 foreach_nat_protocol
3114 nat_elog_info ("unknown protocol");
3119 /* Totally out of translations to use... */
3120 nat_ipfix_logging_addresses_exhausted (thread_index, 0);
3125 nat_alloc_addr_and_port_range (snat_address_t * addresses, u32 fib_index,
3126 u32 thread_index, nat_protocol_t proto,
3127 ip4_address_t * addr, u16 * port,
3128 u16 port_per_thread, u32 snat_thread_index)
3130 snat_main_t *sm = &snat_main;
3131 snat_address_t *a = addresses;
3134 ports = sm->end_port - sm->start_port + 1;
3136 if (!vec_len (addresses))
3141 #define _(N, i, n, s) \
3142 case NAT_PROTOCOL_##N: \
3143 if (a->busy_##n##_ports < ports) \
3147 portnum = snat_random_port(sm->start_port, sm->end_port); \
3148 if (a->busy_##n##_port_refcounts[portnum]) \
3150 ++a->busy_##n##_port_refcounts[portnum]; \
3151 a->busy_##n##_ports++; \
3153 *port = clib_host_to_net_u16 (portnum); \
3158 foreach_nat_protocol
3161 nat_elog_info ("unknown protocol");
3166 /* Totally out of translations to use... */
3167 nat_ipfix_logging_addresses_exhausted (thread_index, 0);
3172 nat44_add_del_address_dpo (ip4_address_t addr, u8 is_add)
3174 dpo_id_t dpo_v4 = DPO_INVALID;
3175 fib_prefix_t pfx = {
3176 .fp_proto = FIB_PROTOCOL_IP4,
3178 .fp_addr.ip4.as_u32 = addr.as_u32,
3183 nat_dpo_create (DPO_PROTO_IP4, 0, &dpo_v4);
3184 fib_table_entry_special_dpo_add (0, &pfx, nat_fib_src_hi,
3185 FIB_ENTRY_FLAG_EXCLUSIVE, &dpo_v4);
3186 dpo_reset (&dpo_v4);
3190 fib_table_entry_special_remove (0, &pfx, nat_fib_src_hi);
3195 format_session_kvp (u8 * s, va_list * args)
3197 clib_bihash_kv_8_8_t *v = va_arg (*args, clib_bihash_kv_8_8_t *);
3199 s = format (s, "%U session-index %llu", format_snat_key, v->key, v->value);
3205 format_static_mapping_kvp (u8 * s, va_list * args)
3207 clib_bihash_kv_8_8_t *v = va_arg (*args, clib_bihash_kv_8_8_t *);
3209 s = format (s, "%U static-mapping-index %llu",
3210 format_snat_key, v->key, v->value);
3216 format_user_kvp (u8 * s, va_list * args)
3218 clib_bihash_kv_8_8_t *v = va_arg (*args, clib_bihash_kv_8_8_t *);
3223 s = format (s, "%U fib %d user-index %llu", format_ip4_address, &k.addr,
3224 k.fib_index, v->value);
3230 format_ed_session_kvp (u8 * s, va_list * args)
3232 clib_bihash_kv_16_8_t *v = va_arg (*args, clib_bihash_kv_16_8_t *);
3236 ip4_address_t l_addr, r_addr;
3239 split_ed_kv (v, &l_addr, &r_addr, &proto, &fib_index, &l_port, &r_port);
3242 "local %U:%d remote %U:%d proto %U fib %d thread-index %u session-index %u",
3243 format_ip4_address, &l_addr, clib_net_to_host_u16 (l_port),
3244 format_ip4_address, &r_addr, clib_net_to_host_u16 (r_port),
3245 format_ip_protocol, proto, fib_index,
3246 ed_value_get_session_index (v), ed_value_get_thread_index (v));
3252 snat_get_worker_in2out_cb (ip4_header_t * ip0, u32 rx_fib_index0,
3255 snat_main_t *sm = &snat_main;
3256 u32 next_worker_index = 0;
3259 next_worker_index = sm->first_worker_index;
3260 hash = ip0->src_address.as_u32 + (ip0->src_address.as_u32 >> 8) +
3261 (ip0->src_address.as_u32 >> 16) + (ip0->src_address.as_u32 >> 24);
3263 if (PREDICT_TRUE (is_pow2 (_vec_len (sm->workers))))
3264 next_worker_index += sm->workers[hash & (_vec_len (sm->workers) - 1)];
3266 next_worker_index += sm->workers[hash % _vec_len (sm->workers)];
3268 return next_worker_index;
3272 snat_get_worker_out2in_cb (vlib_buffer_t * b, ip4_header_t * ip0,
3273 u32 rx_fib_index0, u8 is_output)
3275 snat_main_t *sm = &snat_main;
3278 clib_bihash_kv_8_8_t kv, value;
3279 snat_static_mapping_t *m;
3281 u32 next_worker_index = 0;
3283 /* first try static mappings without port */
3284 if (PREDICT_FALSE (pool_elts (sm->static_mappings)))
3286 init_nat_k (&kv, ip0->dst_address, 0, rx_fib_index0, 0);
3287 if (!clib_bihash_search_8_8
3288 (&sm->static_mapping_by_external, &kv, &value))
3290 m = pool_elt_at_index (sm->static_mappings, value.value);
3291 return m->workers[0];
3295 proto = ip_proto_to_nat_proto (ip0->protocol);
3296 udp = ip4_next_header (ip0);
3297 port = udp->dst_port;
3299 /* unknown protocol */
3300 if (PREDICT_FALSE (proto == NAT_PROTOCOL_OTHER))
3302 /* use current thread */
3303 return vlib_get_thread_index ();
3306 if (PREDICT_FALSE (ip0->protocol == IP_PROTOCOL_ICMP))
3308 icmp46_header_t *icmp = (icmp46_header_t *) udp;
3309 icmp_echo_header_t *echo = (icmp_echo_header_t *) (icmp + 1);
3310 if (!icmp_type_is_error_message
3311 (vnet_buffer (b)->ip.reass.icmp_type_or_tcp_flags))
3312 port = vnet_buffer (b)->ip.reass.l4_src_port;
3315 /* if error message, then it's not fragmented and we can access it */
3316 ip4_header_t *inner_ip = (ip4_header_t *) (echo + 1);
3317 proto = ip_proto_to_nat_proto (inner_ip->protocol);
3318 void *l4_header = ip4_next_header (inner_ip);
3321 case NAT_PROTOCOL_ICMP:
3322 icmp = (icmp46_header_t *) l4_header;
3323 echo = (icmp_echo_header_t *) (icmp + 1);
3324 port = echo->identifier;
3326 case NAT_PROTOCOL_UDP:
3327 case NAT_PROTOCOL_TCP:
3328 port = ((tcp_udp_header_t *) l4_header)->src_port;
3331 return vlib_get_thread_index ();
3336 /* try static mappings with port */
3337 if (PREDICT_FALSE (pool_elts (sm->static_mappings)))
3339 init_nat_k (&kv, ip0->dst_address, port, rx_fib_index0, proto);
3340 if (!clib_bihash_search_8_8
3341 (&sm->static_mapping_by_external, &kv, &value))
3343 m = pool_elt_at_index (sm->static_mappings, value.value);
3344 return m->workers[0];
3348 /* worker by outside port */
3349 next_worker_index = sm->first_worker_index;
3350 next_worker_index +=
3351 sm->workers[(clib_net_to_host_u16 (port) - 1024) / sm->port_per_thread];
3352 return next_worker_index;
3356 nat44_ed_get_worker_in2out_cb (ip4_header_t * ip, u32 rx_fib_index,
3359 snat_main_t *sm = &snat_main;
3360 u32 next_worker_index = sm->first_worker_index;
3363 clib_bihash_kv_16_8_t kv16, value16;
3364 snat_main_per_thread_data_t *tsm;
3367 if (PREDICT_FALSE (is_output))
3369 u32 fib_index = sm->outside_fib_index;
3370 nat_outside_fib_t *outside_fib;
3371 fib_node_index_t fei = FIB_NODE_INDEX_INVALID;
3372 fib_prefix_t pfx = {
3373 .fp_proto = FIB_PROTOCOL_IP4,
3376 .ip4.as_u32 = ip->dst_address.as_u32,
3381 udp = ip4_next_header (ip);
3383 switch (vec_len (sm->outside_fibs))
3386 fib_index = sm->outside_fib_index;
3389 fib_index = sm->outside_fibs[0].fib_index;
3393 vec_foreach (outside_fib, sm->outside_fibs)
3395 fei = fib_table_lookup (outside_fib->fib_index, &pfx);
3396 if (FIB_NODE_INDEX_INVALID != fei)
3398 if (fib_entry_get_resolving_interface (fei) != ~0)
3400 fib_index = outside_fib->fib_index;
3409 init_ed_k (&kv16, ip->src_address, udp->src_port, ip->dst_address,
3410 udp->dst_port, fib_index, ip->protocol);
3412 if (PREDICT_TRUE (!clib_bihash_search_16_8 (&sm->out2in_ed,
3416 vec_elt_at_index (sm->per_thread_data,
3417 ed_value_get_thread_index (&value16));
3418 next_worker_index += tsm->thread_index;
3420 nat_elog_debug_handoff ("HANDOFF IN2OUT-OUTPUT-FEATURE (session)",
3421 next_worker_index, fib_index,
3422 clib_net_to_host_u32 (ip->
3423 src_address.as_u32),
3424 clib_net_to_host_u32 (ip->
3425 dst_address.as_u32));
3427 return next_worker_index;
3431 hash = ip->src_address.as_u32 + (ip->src_address.as_u32 >> 8) +
3432 (ip->src_address.as_u32 >> 16) + (ip->src_address.as_u32 >> 24);
3434 if (PREDICT_TRUE (is_pow2 (_vec_len (sm->workers))))
3435 next_worker_index += sm->workers[hash & (_vec_len (sm->workers) - 1)];
3437 next_worker_index += sm->workers[hash % _vec_len (sm->workers)];
3439 if (PREDICT_TRUE (!is_output))
3441 nat_elog_debug_handoff ("HANDOFF IN2OUT",
3442 next_worker_index, rx_fib_index,
3443 clib_net_to_host_u32 (ip->src_address.as_u32),
3444 clib_net_to_host_u32 (ip->dst_address.as_u32));
3448 nat_elog_debug_handoff ("HANDOFF IN2OUT-OUTPUT-FEATURE",
3449 next_worker_index, rx_fib_index,
3450 clib_net_to_host_u32 (ip->src_address.as_u32),
3451 clib_net_to_host_u32 (ip->dst_address.as_u32));
3454 return next_worker_index;
3458 nat44_ed_get_worker_out2in_cb (vlib_buffer_t * b, ip4_header_t * ip,
3459 u32 rx_fib_index, u8 is_output)
3461 snat_main_t *sm = &snat_main;
3462 clib_bihash_kv_8_8_t kv, value;
3463 clib_bihash_kv_16_8_t kv16, value16;
3464 snat_main_per_thread_data_t *tsm;
3466 u32 proto, next_worker_index = 0;
3469 snat_static_mapping_t *m;
3472 proto = ip_proto_to_nat_proto (ip->protocol);
3474 if (PREDICT_TRUE (proto == NAT_PROTOCOL_UDP || proto == NAT_PROTOCOL_TCP))
3476 udp = ip4_next_header (ip);
3478 init_ed_k (&kv16, ip->dst_address, udp->dst_port, ip->src_address,
3479 udp->src_port, rx_fib_index, ip->protocol);
3481 if (PREDICT_TRUE (!clib_bihash_search_16_8 (&sm->out2in_ed,
3485 vec_elt_at_index (sm->per_thread_data,
3486 ed_value_get_thread_index (&value16));
3487 vnet_buffer2 (b)->nat.ed_out2in_nat_session_index =
3488 ed_value_get_session_index (&value16);
3489 next_worker_index = sm->first_worker_index + tsm->thread_index;
3490 nat_elog_debug_handoff ("HANDOFF OUT2IN (session)",
3491 next_worker_index, rx_fib_index,
3492 clib_net_to_host_u32 (ip->
3493 src_address.as_u32),
3494 clib_net_to_host_u32 (ip->
3495 dst_address.as_u32));
3496 return next_worker_index;
3499 else if (proto == NAT_PROTOCOL_ICMP)
3501 if (!get_icmp_o2i_ed_key (b, ip, rx_fib_index, ~0, ~0, 0, 0, 0, &kv16))
3503 if (PREDICT_TRUE (!clib_bihash_search_16_8 (&sm->out2in_ed,
3507 vec_elt_at_index (sm->per_thread_data,
3508 ed_value_get_thread_index (&value16));
3509 next_worker_index = sm->first_worker_index + tsm->thread_index;
3510 nat_elog_debug_handoff ("HANDOFF OUT2IN (session)",
3511 next_worker_index, rx_fib_index,
3512 clib_net_to_host_u32 (ip->
3513 src_address.as_u32),
3514 clib_net_to_host_u32 (ip->
3515 dst_address.as_u32));
3516 return next_worker_index;
3521 /* first try static mappings without port */
3522 if (PREDICT_FALSE (pool_elts (sm->static_mappings)))
3524 init_nat_k (&kv, ip->dst_address, 0, 0, 0);
3525 if (!clib_bihash_search_8_8
3526 (&sm->static_mapping_by_external, &kv, &value))
3528 m = pool_elt_at_index (sm->static_mappings, value.value);
3529 next_worker_index = m->workers[0];
3534 /* unknown protocol */
3535 if (PREDICT_FALSE (proto == NAT_PROTOCOL_OTHER))
3537 /* use current thread */
3538 next_worker_index = vlib_get_thread_index ();
3542 udp = ip4_next_header (ip);
3543 port = udp->dst_port;
3545 if (PREDICT_FALSE (ip->protocol == IP_PROTOCOL_ICMP))
3547 icmp46_header_t *icmp = (icmp46_header_t *) udp;
3548 icmp_echo_header_t *echo = (icmp_echo_header_t *) (icmp + 1);
3549 if (!icmp_type_is_error_message
3550 (vnet_buffer (b)->ip.reass.icmp_type_or_tcp_flags))
3551 port = vnet_buffer (b)->ip.reass.l4_src_port;
3554 /* if error message, then it's not fragmented and we can access it */
3555 ip4_header_t *inner_ip = (ip4_header_t *) (echo + 1);
3556 proto = ip_proto_to_nat_proto (inner_ip->protocol);
3557 void *l4_header = ip4_next_header (inner_ip);
3560 case NAT_PROTOCOL_ICMP:
3561 icmp = (icmp46_header_t *) l4_header;
3562 echo = (icmp_echo_header_t *) (icmp + 1);
3563 port = echo->identifier;
3565 case NAT_PROTOCOL_UDP:
3566 case NAT_PROTOCOL_TCP:
3567 port = ((tcp_udp_header_t *) l4_header)->src_port;
3570 next_worker_index = vlib_get_thread_index ();
3576 /* try static mappings with port */
3577 if (PREDICT_FALSE (pool_elts (sm->static_mappings)))
3579 init_nat_k (&kv, ip->dst_address, proto, 0, port);
3580 if (!clib_bihash_search_8_8
3581 (&sm->static_mapping_by_external, &kv, &value))
3583 m = pool_elt_at_index (sm->static_mappings, value.value);
3584 if (!is_lb_static_mapping (m))
3586 next_worker_index = m->workers[0];
3590 hash = ip->src_address.as_u32 + (ip->src_address.as_u32 >> 8) +
3591 (ip->src_address.as_u32 >> 16) + (ip->src_address.as_u32 >> 24);
3593 if (PREDICT_TRUE (is_pow2 (_vec_len (m->workers))))
3595 m->workers[hash & (_vec_len (m->workers) - 1)];
3597 next_worker_index = m->workers[hash % _vec_len (m->workers)];
3602 /* worker by outside port */
3603 next_worker_index = sm->first_worker_index;
3604 next_worker_index +=
3605 sm->workers[(clib_net_to_host_u16 (port) - 1024) / sm->port_per_thread];
3608 nat_elog_debug_handoff ("HANDOFF OUT2IN", next_worker_index, rx_fib_index,
3609 clib_net_to_host_u32 (ip->src_address.as_u32),
3610 clib_net_to_host_u32 (ip->dst_address.as_u32));
3611 return next_worker_index;
3615 nat_ha_sadd_cb (ip4_address_t * in_addr, u16 in_port,
3616 ip4_address_t * out_addr, u16 out_port,
3617 ip4_address_t * eh_addr, u16 eh_port,
3618 ip4_address_t * ehn_addr, u16 ehn_port, u8 proto,
3619 u32 fib_index, u16 flags, u32 thread_index)
3621 snat_main_t *sm = &snat_main;
3622 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
3625 clib_bihash_kv_8_8_t kv;
3626 vlib_main_t *vm = vlib_get_main ();
3627 f64 now = vlib_time_now (vm);
3628 nat_outside_fib_t *outside_fib;
3629 fib_node_index_t fei = FIB_NODE_INDEX_INVALID;
3630 fib_prefix_t pfx = {
3631 .fp_proto = FIB_PROTOCOL_IP4,
3634 .ip4.as_u32 = eh_addr->as_u32,
3638 if (!(flags & SNAT_SESSION_FLAG_STATIC_MAPPING))
3640 if (nat_set_outside_address_and_port
3641 (sm->addresses, thread_index, *out_addr, out_port, proto))
3645 u = nat_user_get_or_create (sm, in_addr, fib_index, thread_index);
3649 s = nat_session_alloc_or_recycle (sm, u, thread_index, now);
3653 if (sm->endpoint_dependent)
3655 nat_ed_lru_insert (tsm, s, now, nat_proto_to_ip_proto (proto));
3658 s->out2in.addr.as_u32 = out_addr->as_u32;
3659 s->out2in.port = out_port;
3660 s->nat_proto = proto;
3661 s->last_heard = now;
3663 s->ext_host_addr.as_u32 = eh_addr->as_u32;
3664 s->ext_host_port = eh_port;
3665 user_session_increment (sm, u, snat_is_session_static (s));
3666 switch (vec_len (sm->outside_fibs))
3669 s->out2in.fib_index = sm->outside_fib_index;
3672 s->out2in.fib_index = sm->outside_fibs[0].fib_index;
3676 vec_foreach (outside_fib, sm->outside_fibs)
3678 fei = fib_table_lookup (outside_fib->fib_index, &pfx);
3679 if (FIB_NODE_INDEX_INVALID != fei)
3681 if (fib_entry_get_resolving_interface (fei) != ~0)
3683 s->out2in.fib_index = outside_fib->fib_index;
3691 init_nat_o2i_kv (&kv, s, s - tsm->sessions);
3692 if (clib_bihash_add_del_8_8 (&tsm->out2in, &kv, 1))
3693 nat_elog_warn ("out2in key add failed");
3695 s->in2out.addr.as_u32 = in_addr->as_u32;
3696 s->in2out.port = in_port;
3697 s->in2out.fib_index = fib_index;
3698 init_nat_i2o_kv (&kv, s, s - tsm->sessions);
3699 if (clib_bihash_add_del_8_8 (&tsm->in2out, &kv, 1))
3700 nat_elog_warn ("in2out key add failed");
3704 nat_ha_sdel_cb (ip4_address_t * out_addr, u16 out_port,
3705 ip4_address_t * eh_addr, u16 eh_port, u8 proto, u32 fib_index,
3708 snat_main_t *sm = &snat_main;
3709 clib_bihash_kv_8_8_t kv, value;
3712 snat_main_per_thread_data_t *tsm;
3714 if (sm->num_workers > 1)
3716 sm->first_worker_index +
3717 (sm->workers[(clib_net_to_host_u16 (out_port) -
3718 1024) / sm->port_per_thread]);
3720 thread_index = sm->num_workers;
3721 tsm = vec_elt_at_index (sm->per_thread_data, thread_index);
3723 init_nat_k (&kv, *out_addr, out_port, fib_index, proto);
3724 if (clib_bihash_search_8_8 (&tsm->out2in, &kv, &value))
3727 s = pool_elt_at_index (tsm->sessions, value.value);
3728 nat_free_session_data (sm, s, thread_index, 1);
3729 nat44_delete_session (sm, s, thread_index);
3733 nat_ha_sref_cb (ip4_address_t * out_addr, u16 out_port,
3734 ip4_address_t * eh_addr, u16 eh_port, u8 proto, u32 fib_index,
3735 u32 total_pkts, u64 total_bytes, u32 thread_index)
3737 snat_main_t *sm = &snat_main;
3738 clib_bihash_kv_8_8_t kv, value;
3740 snat_main_per_thread_data_t *tsm;
3742 tsm = vec_elt_at_index (sm->per_thread_data, thread_index);
3744 init_nat_k (&kv, *out_addr, out_port, fib_index, proto);
3745 if (clib_bihash_search_8_8 (&tsm->out2in, &kv, &value))
3748 s = pool_elt_at_index (tsm->sessions, value.value);
3749 s->total_pkts = total_pkts;
3750 s->total_bytes = total_bytes;
3754 nat_ha_sadd_ed_cb (ip4_address_t * in_addr, u16 in_port,
3755 ip4_address_t * out_addr, u16 out_port,
3756 ip4_address_t * eh_addr, u16 eh_port,
3757 ip4_address_t * ehn_addr, u16 ehn_port, u8 proto,
3758 u32 fib_index, u16 flags, u32 thread_index)
3760 snat_main_t *sm = &snat_main;
3761 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
3763 clib_bihash_kv_16_8_t kv;
3764 vlib_main_t *vm = vlib_get_main ();
3765 f64 now = vlib_time_now (vm);
3766 nat_outside_fib_t *outside_fib;
3767 fib_node_index_t fei = FIB_NODE_INDEX_INVALID;
3768 fib_prefix_t pfx = {
3769 .fp_proto = FIB_PROTOCOL_IP4,
3772 .ip4.as_u32 = eh_addr->as_u32,
3777 if (!(flags & SNAT_SESSION_FLAG_STATIC_MAPPING))
3779 if (nat_set_outside_address_and_port
3780 (sm->addresses, thread_index, *out_addr, out_port, proto))
3784 if (flags & SNAT_SESSION_FLAG_TWICE_NAT)
3786 if (nat_set_outside_address_and_port
3787 (sm->addresses, thread_index, *ehn_addr, ehn_port, proto))
3791 s = nat_ed_session_alloc (sm, thread_index, now, proto);
3795 s->last_heard = now;
3797 s->ext_host_nat_addr.as_u32 = s->ext_host_addr.as_u32 = eh_addr->as_u32;
3798 s->ext_host_nat_port = s->ext_host_port = eh_port;
3799 if (is_twice_nat_session (s))
3801 s->ext_host_nat_addr.as_u32 = ehn_addr->as_u32;
3802 s->ext_host_nat_port = ehn_port;
3804 switch (vec_len (sm->outside_fibs))
3807 s->out2in.fib_index = sm->outside_fib_index;
3810 s->out2in.fib_index = sm->outside_fibs[0].fib_index;
3814 vec_foreach (outside_fib, sm->outside_fibs)
3816 fei = fib_table_lookup (outside_fib->fib_index, &pfx);
3817 if (FIB_NODE_INDEX_INVALID != fei)
3819 if (fib_entry_get_resolving_interface (fei) != ~0)
3821 s->out2in.fib_index = outside_fib->fib_index;
3829 s->nat_proto = proto;
3830 s->out2in.addr.as_u32 = out_addr->as_u32;
3831 s->out2in.port = out_port;
3833 s->in2out.addr.as_u32 = in_addr->as_u32;
3834 s->in2out.port = in_port;
3835 s->in2out.fib_index = fib_index;
3837 init_ed_kv (&kv, *in_addr, in_port, s->ext_host_nat_addr,
3838 s->ext_host_nat_port, fib_index, nat_proto_to_ip_proto (proto),
3839 thread_index, s - tsm->sessions);
3840 if (clib_bihash_add_del_16_8 (&tsm->in2out_ed, &kv, 1))
3841 nat_elog_warn ("in2out key add failed");
3843 init_ed_kv (&kv, *out_addr, out_port, *eh_addr, eh_port,
3844 s->out2in.fib_index, nat_proto_to_ip_proto (proto),
3845 thread_index, s - tsm->sessions);
3846 if (clib_bihash_add_del_16_8 (&sm->out2in_ed, &kv, 1))
3847 nat_elog_warn ("out2in key add failed");
3851 nat_ha_sdel_ed_cb (ip4_address_t * out_addr, u16 out_port,
3852 ip4_address_t * eh_addr, u16 eh_port, u8 proto,
3853 u32 fib_index, u32 ti)
3855 snat_main_t *sm = &snat_main;
3856 clib_bihash_kv_16_8_t kv, value;
3859 snat_main_per_thread_data_t *tsm;
3861 if (sm->num_workers > 1)
3863 sm->first_worker_index +
3864 (sm->workers[(clib_net_to_host_u16 (out_port) -
3865 1024) / sm->port_per_thread]);
3867 thread_index = sm->num_workers;
3868 tsm = vec_elt_at_index (sm->per_thread_data, thread_index);
3870 init_ed_k (&kv, *out_addr, out_port, *eh_addr, eh_port, fib_index, proto);
3871 if (clib_bihash_search_16_8 (&sm->out2in_ed, &kv, &value))
3874 s = pool_elt_at_index (tsm->sessions, ed_value_get_session_index (&value));
3875 nat_free_session_data (sm, s, thread_index, 1);
3876 nat44_delete_session (sm, s, thread_index);
3880 nat_ha_sref_ed_cb (ip4_address_t * out_addr, u16 out_port,
3881 ip4_address_t * eh_addr, u16 eh_port, u8 proto,
3882 u32 fib_index, u32 total_pkts, u64 total_bytes,
3885 snat_main_t *sm = &snat_main;
3886 clib_bihash_kv_16_8_t kv, value;
3888 snat_main_per_thread_data_t *tsm;
3890 tsm = vec_elt_at_index (sm->per_thread_data, thread_index);
3892 init_ed_k (&kv, *out_addr, out_port, *eh_addr, eh_port, fib_index, proto);
3893 if (clib_bihash_search_16_8 (&sm->out2in_ed, &kv, &value))
3896 s = pool_elt_at_index (tsm->sessions, ed_value_get_session_index (&value));
3897 s->total_pkts = total_pkts;
3898 s->total_bytes = total_bytes;
3902 nat_calc_bihash_buckets (u32 n_elts)
3904 return 1 << (max_log2 (n_elts >> 1) + 1);
3908 nat_calc_bihash_memory (u32 n_buckets, uword kv_size)
3910 return n_buckets * (8 + kv_size * 4);
3914 nat44_get_max_session_limit ()
3916 snat_main_t *sm = &snat_main;
3917 u32 max_limit = 0, len = 0;
3919 for (; len < vec_len (sm->max_translations_per_fib); len++)
3921 if (max_limit < sm->max_translations_per_fib[len])
3922 max_limit = sm->max_translations_per_fib[len];
3928 nat44_set_session_limit (u32 session_limit, u32 vrf_id)
3930 snat_main_t *sm = &snat_main;
3931 u32 fib_index = fib_table_find (FIB_PROTOCOL_IP4, vrf_id);
3932 u32 len = vec_len (sm->max_translations_per_fib);
3934 if (len <= fib_index)
3936 vec_validate (sm->max_translations_per_fib, fib_index + 1);
3938 for (; len < vec_len (sm->max_translations_per_fib); len++)
3939 sm->max_translations_per_fib[len] = sm->max_translations_per_thread;
3942 sm->max_translations_per_fib[fib_index] = session_limit;
3947 nat44_update_session_limit (u32 session_limit, u32 vrf_id)
3949 snat_main_t *sm = &snat_main;
3951 if (nat44_set_session_limit (session_limit, vrf_id))
3953 sm->max_translations_per_thread = nat44_get_max_session_limit ();
3955 sm->translation_buckets =
3956 nat_calc_bihash_buckets (sm->max_translations_per_thread);
3958 if (!sm->translation_memory_size_set)
3960 sm->translation_memory_size =
3961 nat_calc_bihash_memory (sm->translation_buckets,
3962 sizeof (clib_bihash_16_8_t));
3965 nat44_sessions_clear ();
3970 nat44_db_init (snat_main_per_thread_data_t * tsm)
3972 snat_main_t *sm = &snat_main;
3974 pool_alloc (tsm->sessions, sm->max_translations_per_thread);
3975 pool_alloc (tsm->lru_pool, sm->max_translations_per_thread);
3979 pool_get (tsm->lru_pool, head);
3980 tsm->tcp_trans_lru_head_index = head - tsm->lru_pool;
3981 clib_dlist_init (tsm->lru_pool, tsm->tcp_trans_lru_head_index);
3983 pool_get (tsm->lru_pool, head);
3984 tsm->tcp_estab_lru_head_index = head - tsm->lru_pool;
3985 clib_dlist_init (tsm->lru_pool, tsm->tcp_estab_lru_head_index);
3987 pool_get (tsm->lru_pool, head);
3988 tsm->udp_lru_head_index = head - tsm->lru_pool;
3989 clib_dlist_init (tsm->lru_pool, tsm->udp_lru_head_index);
3991 pool_get (tsm->lru_pool, head);
3992 tsm->icmp_lru_head_index = head - tsm->lru_pool;
3993 clib_dlist_init (tsm->lru_pool, tsm->icmp_lru_head_index);
3995 pool_get (tsm->lru_pool, head);
3996 tsm->unk_proto_lru_head_index = head - tsm->lru_pool;
3997 clib_dlist_init (tsm->lru_pool, tsm->unk_proto_lru_head_index);
3999 if (sm->endpoint_dependent)
4001 clib_bihash_init_16_8 (&tsm->in2out_ed, "in2out-ed",
4002 sm->translation_buckets,
4003 sm->translation_memory_size);
4004 clib_bihash_set_kvp_format_fn_16_8 (&tsm->in2out_ed,
4005 format_ed_session_kvp);
4010 clib_bihash_init_8_8 (&tsm->in2out, "in2out",
4011 sm->translation_buckets,
4012 sm->translation_memory_size);
4013 clib_bihash_set_kvp_format_fn_8_8 (&tsm->in2out, format_session_kvp);
4014 clib_bihash_init_8_8 (&tsm->out2in, "out2in",
4015 sm->translation_buckets,
4016 sm->translation_memory_size);
4017 clib_bihash_set_kvp_format_fn_8_8 (&tsm->out2in, format_session_kvp);
4020 // TODO: resolve static mappings (put only to !ED)
4021 pool_alloc (tsm->list_pool, sm->max_translations_per_thread);
4022 clib_bihash_init_8_8 (&tsm->user_hash, "users", sm->user_buckets,
4023 sm->user_memory_size);
4024 clib_bihash_set_kvp_format_fn_8_8 (&tsm->user_hash, format_user_kvp);
4028 nat44_db_free (snat_main_per_thread_data_t * tsm)
4030 snat_main_t *sm = &snat_main;
4032 pool_free (tsm->sessions);
4033 pool_free (tsm->lru_pool);
4035 if (sm->endpoint_dependent)
4037 clib_bihash_free_16_8 (&tsm->in2out_ed);
4038 vec_free (tsm->per_vrf_sessions_vec);
4042 clib_bihash_free_8_8 (&tsm->in2out);
4043 clib_bihash_free_8_8 (&tsm->out2in);
4046 // TODO: resolve static mappings (put only to !ED)
4047 pool_free (tsm->users);
4048 pool_free (tsm->list_pool);
4049 clib_bihash_free_8_8 (&tsm->user_hash);
4053 nat44_sessions_clear ()
4055 snat_main_t *sm = &snat_main;
4056 snat_main_per_thread_data_t *tsm;
4058 if (sm->endpoint_dependent)
4060 clib_bihash_free_16_8 (&sm->out2in_ed);
4061 clib_bihash_init_16_8 (&sm->out2in_ed, "out2in-ed",
4062 clib_max (1, sm->num_workers) *
4063 sm->translation_buckets,
4064 clib_max (1, sm->num_workers) *
4065 sm->translation_memory_size);
4066 clib_bihash_set_kvp_format_fn_16_8 (&sm->out2in_ed,
4067 format_ed_session_kvp);
4071 vec_foreach (tsm, sm->per_thread_data)
4075 nat44_db_free (tsm);
4076 nat44_db_init (tsm);
4078 ti = tsm->snat_thread_index;
4079 vlib_set_simple_counter (&sm->total_users, ti, 0, 0);
4080 vlib_set_simple_counter (&sm->total_sessions, ti, 0, 0);
4085 static clib_error_t *
4086 snat_config (vlib_main_t * vm, unformat_input_t * input)
4088 snat_main_t *sm = &snat_main;
4089 snat_main_per_thread_data_t *tsm;
4091 u32 static_mapping_buckets = 1024;
4092 uword static_mapping_memory_size = 64 << 20;
4094 u32 max_users_per_thread = 0;
4095 u32 user_memory_size = 0;
4096 u32 max_translations_per_thread = 0;
4097 u32 translation_memory_size = 0;
4099 u32 max_translations_per_user = ~0;
4101 u32 outside_vrf_id = 0;
4102 u32 outside_ip6_vrf_id = 0;
4103 u32 inside_vrf_id = 0;
4104 u8 static_mapping_only = 0;
4105 u8 static_mapping_connection_tracking = 0;
4107 u32 udp_timeout = SNAT_UDP_TIMEOUT;
4108 u32 icmp_timeout = SNAT_ICMP_TIMEOUT;
4109 u32 tcp_transitory_timeout = SNAT_TCP_TRANSITORY_TIMEOUT;
4110 u32 tcp_established_timeout = SNAT_TCP_ESTABLISHED_TIMEOUT;
4113 sm->endpoint_dependent = 0;
4115 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
4118 (input, "max translations per thread %d",
4119 &max_translations_per_thread))
4121 else if (unformat (input, "udp timeout %d", &udp_timeout))
4123 else if (unformat (input, "icmp timeout %d", &icmp_timeout))
4125 else if (unformat (input, "tcp transitory timeout %d",
4126 &tcp_transitory_timeout));
4127 else if (unformat (input, "tcp established timeout %d",
4128 &tcp_established_timeout));
4129 else if (unformat (input, "translation hash memory %d",
4130 &translation_memory_size));
4133 (input, "max users per thread %d", &max_users_per_thread))
4135 else if (unformat (input, "user hash memory %d", &user_memory_size))
4137 else if (unformat (input, "max translations per user %d",
4138 &max_translations_per_user))
4140 else if (unformat (input, "outside VRF id %d", &outside_vrf_id))
4142 else if (unformat (input, "outside ip6 VRF id %d", &outside_ip6_vrf_id))
4144 else if (unformat (input, "inside VRF id %d", &inside_vrf_id))
4146 else if (unformat (input, "static mapping only"))
4148 static_mapping_only = 1;
4149 if (unformat (input, "connection tracking"))
4150 static_mapping_connection_tracking = 1;
4152 else if (unformat (input, "out2in dpo"))
4154 else if (unformat (input, "endpoint-dependent"))
4155 sm->endpoint_dependent = 1;
4157 return clib_error_return (0, "unknown input '%U'",
4158 format_unformat_error, input);
4161 if (static_mapping_only && (sm->endpoint_dependent))
4162 return clib_error_return (0,
4163 "static mapping only mode available only for simple nat");
4165 if (sm->out2in_dpo && (sm->endpoint_dependent))
4166 return clib_error_return (0,
4167 "out2in dpo mode available only for simple nat");
4168 if (sm->endpoint_dependent && max_users_per_thread > 0)
4170 return clib_error_return (0,
4171 "setting 'max users' in endpoint-dependent mode is not supported");
4174 if (sm->endpoint_dependent && max_translations_per_user != ~0)
4176 return clib_error_return (0,
4177 "setting 'max translations per user' in endpoint-dependent mode is not supported");
4180 /* optionally configurable timeouts for testing purposes */
4181 sm->udp_timeout = udp_timeout;
4182 sm->tcp_transitory_timeout = tcp_transitory_timeout;
4183 sm->tcp_established_timeout = tcp_established_timeout;
4184 sm->icmp_timeout = icmp_timeout;
4186 if (0 == max_users_per_thread)
4188 max_users_per_thread = 1024;
4190 sm->max_users_per_thread = max_users_per_thread;
4191 sm->user_buckets = nat_calc_bihash_buckets (sm->max_users_per_thread);
4193 if (0 == max_translations_per_thread)
4195 // default value based on legacy setting of load factor 10 * default
4196 // translation buckets 1024
4197 max_translations_per_thread = 10 * 1024;
4199 sm->translation_memory_size_set = translation_memory_size != 0;
4201 sm->max_translations_per_thread = max_translations_per_thread;
4202 sm->translation_buckets =
4203 nat_calc_bihash_buckets (sm->max_translations_per_thread);
4204 if (0 == translation_memory_size)
4206 translation_memory_size =
4207 nat_calc_bihash_memory (sm->translation_buckets,
4208 sizeof (clib_bihash_16_8_t));
4210 sm->translation_memory_size = translation_memory_size;
4211 if (0 == user_memory_size)
4214 nat_calc_bihash_memory (sm->max_users_per_thread,
4215 sizeof (clib_bihash_8_8_t));
4217 sm->user_memory_size = user_memory_size;
4218 vec_add1 (sm->max_translations_per_fib, sm->max_translations_per_thread);
4220 sm->max_translations_per_user = max_translations_per_user == ~0 ?
4221 sm->max_translations_per_thread : max_translations_per_user;
4223 sm->outside_vrf_id = outside_vrf_id;
4224 sm->outside_fib_index = fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4,
4227 sm->inside_vrf_id = inside_vrf_id;
4228 sm->inside_fib_index = fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4,
4231 sm->static_mapping_only = static_mapping_only;
4232 sm->static_mapping_connection_tracking = static_mapping_connection_tracking;
4234 if (sm->endpoint_dependent)
4236 sm->worker_in2out_cb = nat44_ed_get_worker_in2out_cb;
4237 sm->worker_out2in_cb = nat44_ed_get_worker_out2in_cb;
4239 sm->in2out_node_index = nat44_ed_in2out_node.index;
4240 sm->in2out_output_node_index = nat44_ed_in2out_output_node.index;
4241 sm->out2in_node_index = nat44_ed_out2in_node.index;
4243 sm->icmp_match_in2out_cb = icmp_match_in2out_ed;
4244 sm->icmp_match_out2in_cb = icmp_match_out2in_ed;
4245 nat_affinity_init (vm);
4246 nat_ha_init (vm, nat_ha_sadd_ed_cb, nat_ha_sdel_ed_cb,
4248 clib_bihash_init_16_8 (&sm->out2in_ed, "out2in-ed",
4249 clib_max (1, sm->num_workers) *
4250 sm->translation_buckets,
4251 clib_max (1, sm->num_workers) *
4252 sm->translation_memory_size);
4253 clib_bihash_set_kvp_format_fn_16_8 (&sm->out2in_ed,
4254 format_ed_session_kvp);
4258 sm->worker_in2out_cb = snat_get_worker_in2out_cb;
4259 sm->worker_out2in_cb = snat_get_worker_out2in_cb;
4261 sm->in2out_node_index = snat_in2out_node.index;
4262 sm->in2out_output_node_index = snat_in2out_output_node.index;
4263 sm->out2in_node_index = snat_out2in_node.index;
4265 sm->icmp_match_in2out_cb = icmp_match_in2out_slow;
4266 sm->icmp_match_out2in_cb = icmp_match_out2in_slow;
4267 nat_ha_init (vm, nat_ha_sadd_cb, nat_ha_sdel_cb, nat_ha_sref_cb);
4269 if (!static_mapping_only ||
4270 (static_mapping_only && static_mapping_connection_tracking))
4273 vec_foreach (tsm, sm->per_thread_data)
4275 nat44_db_init (tsm);
4281 sm->icmp_match_in2out_cb = icmp_match_in2out_fast;
4282 sm->icmp_match_out2in_cb = icmp_match_out2in_fast;
4284 clib_bihash_init_8_8 (&sm->static_mapping_by_local,
4285 "static_mapping_by_local", static_mapping_buckets,
4286 static_mapping_memory_size);
4287 clib_bihash_set_kvp_format_fn_8_8 (&sm->static_mapping_by_local,
4288 format_static_mapping_kvp);
4290 clib_bihash_init_8_8 (&sm->static_mapping_by_external,
4291 "static_mapping_by_external",
4292 static_mapping_buckets, static_mapping_memory_size);
4293 clib_bihash_set_kvp_format_fn_8_8 (&sm->static_mapping_by_external,
4294 format_static_mapping_kvp);
4299 VLIB_CONFIG_FUNCTION (snat_config, "nat");
4302 nat_ip4_add_del_addr_only_sm_cb (ip4_main_t * im,
4305 ip4_address_t * address,
4307 u32 if_address_index, u32 is_delete)
4309 snat_main_t *sm = &snat_main;
4310 snat_static_map_resolve_t *rp;
4311 snat_static_mapping_t *m;
4312 clib_bihash_kv_8_8_t kv, value;
4314 ip4_address_t l_addr;
4316 for (i = 0; i < vec_len (sm->to_resolve); i++)
4318 rp = sm->to_resolve + i;
4319 if (rp->addr_only == 0)
4321 if (rp->sw_if_index == sw_if_index)
4328 init_nat_k (&kv, *address, rp->addr_only ? 0 : rp->e_port,
4329 sm->outside_fib_index, rp->addr_only ? 0 : rp->proto);
4330 if (clib_bihash_search_8_8 (&sm->static_mapping_by_external, &kv, &value))
4333 m = pool_elt_at_index (sm->static_mappings, value.value);
4337 /* Don't trip over lease renewal, static config */
4347 /* Indetity mapping? */
4348 if (rp->l_addr.as_u32 == 0)
4349 l_addr.as_u32 = address[0].as_u32;
4351 l_addr.as_u32 = rp->l_addr.as_u32;
4352 /* Add the static mapping */
4353 rv = snat_add_static_mapping (l_addr,
4358 rp->addr_only, ~0 /* sw_if_index */ ,
4359 rp->proto, !is_delete, rp->twice_nat,
4360 rp->out2in_only, rp->tag, rp->identity_nat,
4361 rp->pool_addr, rp->exact);
4363 nat_elog_notice_X1 ("snat_add_static_mapping returned %d", "i4", rv);
4367 snat_ip4_add_del_interface_address_cb (ip4_main_t * im,
4370 ip4_address_t * address,
4372 u32 if_address_index, u32 is_delete)
4374 snat_main_t *sm = &snat_main;
4375 snat_static_map_resolve_t *rp;
4376 ip4_address_t l_addr;
4380 snat_address_t *addresses = sm->addresses;
4382 for (i = 0; i < vec_len (sm->auto_add_sw_if_indices); i++)
4384 if (sw_if_index == sm->auto_add_sw_if_indices[i])
4388 for (i = 0; i < vec_len (sm->auto_add_sw_if_indices_twice_nat); i++)
4391 addresses = sm->twice_nat_addresses;
4392 if (sw_if_index == sm->auto_add_sw_if_indices_twice_nat[i])
4401 /* Don't trip over lease renewal, static config */
4402 for (j = 0; j < vec_len (addresses); j++)
4403 if (addresses[j].addr.as_u32 == address->as_u32)
4406 (void) snat_add_address (sm, address, ~0, twice_nat);
4407 /* Scan static map resolution vector */
4408 for (j = 0; j < vec_len (sm->to_resolve); j++)
4410 rp = sm->to_resolve + j;
4413 /* On this interface? */
4414 if (rp->sw_if_index == sw_if_index)
4416 /* Indetity mapping? */
4417 if (rp->l_addr.as_u32 == 0)
4418 l_addr.as_u32 = address[0].as_u32;
4420 l_addr.as_u32 = rp->l_addr.as_u32;
4421 /* Add the static mapping */
4422 rv = snat_add_static_mapping (l_addr,
4428 ~0 /* sw_if_index */ ,
4430 rp->is_add, rp->twice_nat,
4431 rp->out2in_only, rp->tag,
4433 rp->pool_addr, rp->exact);
4435 nat_elog_notice_X1 ("snat_add_static_mapping returned %d",
4443 (void) snat_del_address (sm, address[0], 1, twice_nat);
4450 snat_add_interface_address (snat_main_t * sm, u32 sw_if_index, int is_del,
4453 ip4_main_t *ip4_main = sm->ip4_main;
4454 ip4_address_t *first_int_addr;
4455 snat_static_map_resolve_t *rp;
4456 u32 *indices_to_delete = 0;
4458 u32 *auto_add_sw_if_indices =
4460 auto_add_sw_if_indices_twice_nat : sm->auto_add_sw_if_indices;
4462 first_int_addr = ip4_interface_first_address (ip4_main, sw_if_index, 0 /* just want the address */
4465 for (i = 0; i < vec_len (auto_add_sw_if_indices); i++)
4467 if (auto_add_sw_if_indices[i] == sw_if_index)
4471 /* if have address remove it */
4473 (void) snat_del_address (sm, first_int_addr[0], 1, twice_nat);
4476 for (j = 0; j < vec_len (sm->to_resolve); j++)
4478 rp = sm->to_resolve + j;
4479 if (rp->sw_if_index == sw_if_index)
4480 vec_add1 (indices_to_delete, j);
4482 if (vec_len (indices_to_delete))
4484 for (j = vec_len (indices_to_delete) - 1; j >= 0; j--)
4485 vec_del1 (sm->to_resolve, j);
4486 vec_free (indices_to_delete);
4490 vec_del1 (sm->auto_add_sw_if_indices_twice_nat, i);
4492 vec_del1 (sm->auto_add_sw_if_indices, i);
4495 return VNET_API_ERROR_VALUE_EXIST;
4502 return VNET_API_ERROR_NO_SUCH_ENTRY;
4504 /* add to the auto-address list */
4506 vec_add1 (sm->auto_add_sw_if_indices_twice_nat, sw_if_index);
4508 vec_add1 (sm->auto_add_sw_if_indices, sw_if_index);
4510 /* If the address is already bound - or static - add it now */
4512 (void) snat_add_address (sm, first_int_addr, ~0, twice_nat);
4518 nat44_del_session (snat_main_t * sm, ip4_address_t * addr, u16 port,
4519 nat_protocol_t proto, u32 vrf_id, int is_in)
4521 snat_main_per_thread_data_t *tsm;
4522 clib_bihash_kv_8_8_t kv, value;
4524 u32 fib_index = fib_table_find (FIB_PROTOCOL_IP4, vrf_id);
4526 clib_bihash_8_8_t *t;
4528 if (sm->endpoint_dependent)
4529 return VNET_API_ERROR_UNSUPPORTED;
4531 ip.dst_address.as_u32 = ip.src_address.as_u32 = addr->as_u32;
4532 if (sm->num_workers > 1)
4534 vec_elt_at_index (sm->per_thread_data,
4535 sm->worker_in2out_cb (&ip, fib_index, 0));
4537 tsm = vec_elt_at_index (sm->per_thread_data, sm->num_workers);
4539 init_nat_k (&kv, *addr, port, fib_index, proto);
4540 t = is_in ? &tsm->in2out : &tsm->out2in;
4541 if (!clib_bihash_search_8_8 (t, &kv, &value))
4543 if (pool_is_free_index (tsm->sessions, value.value))
4544 return VNET_API_ERROR_UNSPECIFIED;
4546 s = pool_elt_at_index (tsm->sessions, value.value);
4547 nat_free_session_data (sm, s, tsm - sm->per_thread_data, 0);
4548 nat44_delete_session (sm, s, tsm - sm->per_thread_data);
4552 return VNET_API_ERROR_NO_SUCH_ENTRY;
4556 nat44_del_ed_session (snat_main_t * sm, ip4_address_t * addr, u16 port,
4557 ip4_address_t * eh_addr, u16 eh_port, u8 proto,
4558 u32 vrf_id, int is_in)
4561 clib_bihash_16_8_t *t;
4562 clib_bihash_kv_16_8_t kv, value;
4563 u32 fib_index = fib_table_find (FIB_PROTOCOL_IP4, vrf_id);
4565 snat_main_per_thread_data_t *tsm;
4567 if (!sm->endpoint_dependent)
4568 return VNET_API_ERROR_FEATURE_DISABLED;
4570 ip.dst_address.as_u32 = ip.src_address.as_u32 = addr->as_u32;
4571 if (sm->num_workers > 1)
4573 vec_elt_at_index (sm->per_thread_data,
4574 sm->worker_in2out_cb (&ip, fib_index, 0));
4576 tsm = vec_elt_at_index (sm->per_thread_data, sm->num_workers);
4578 t = is_in ? &tsm->in2out_ed : &sm->out2in_ed;
4579 init_ed_k (&kv, *addr, port, *eh_addr, eh_port, fib_index, proto);
4580 if (clib_bihash_search_16_8 (t, &kv, &value))
4582 return VNET_API_ERROR_NO_SUCH_ENTRY;
4585 if (pool_is_free_index (tsm->sessions, value.value))
4586 return VNET_API_ERROR_UNSPECIFIED;
4587 s = pool_elt_at_index (tsm->sessions, value.value);
4588 nat_free_session_data (sm, s, tsm - sm->per_thread_data, 0);
4589 nat_ed_session_delete (sm, s, tsm - sm->per_thread_data, 1);
4594 nat_set_alloc_addr_and_port_mape (u16 psid, u16 psid_offset, u16 psid_length)
4596 snat_main_t *sm = &snat_main;
4598 sm->addr_and_port_alloc_alg = NAT_ADDR_AND_PORT_ALLOC_ALG_MAPE;
4599 sm->alloc_addr_and_port = nat_alloc_addr_and_port_mape;
4601 sm->psid_offset = psid_offset;
4602 sm->psid_length = psid_length;
4606 nat_set_alloc_addr_and_port_range (u16 start_port, u16 end_port)
4608 snat_main_t *sm = &snat_main;
4610 sm->addr_and_port_alloc_alg = NAT_ADDR_AND_PORT_ALLOC_ALG_RANGE;
4611 sm->alloc_addr_and_port = nat_alloc_addr_and_port_range;
4612 sm->start_port = start_port;
4613 sm->end_port = end_port;
4617 nat_set_alloc_addr_and_port_default (void)
4619 snat_main_t *sm = &snat_main;
4621 sm->addr_and_port_alloc_alg = NAT_ADDR_AND_PORT_ALLOC_ALG_DEFAULT;
4622 sm->alloc_addr_and_port = nat_alloc_addr_and_port_default;
4625 VLIB_NODE_FN (nat_default_node) (vlib_main_t * vm,
4626 vlib_node_runtime_t * node,
4627 vlib_frame_t * frame)
4633 VLIB_REGISTER_NODE (nat_default_node) = {
4634 .name = "nat-default",
4635 .vector_size = sizeof (u32),
4637 .type = VLIB_NODE_TYPE_INTERNAL,
4639 .n_next_nodes = NAT_N_NEXT,
4641 [NAT_NEXT_DROP] = "error-drop",
4642 [NAT_NEXT_ICMP_ERROR] = "ip4-icmp-error",
4643 [NAT_NEXT_IN2OUT_ED_FAST_PATH] = "nat44-ed-in2out",
4644 [NAT_NEXT_IN2OUT_ED_SLOW_PATH] = "nat44-ed-in2out-slowpath",
4645 [NAT_NEXT_IN2OUT_ED_OUTPUT_FAST_PATH] = "nat44-ed-in2out-output",
4646 [NAT_NEXT_IN2OUT_ED_OUTPUT_SLOW_PATH] = "nat44-ed-in2out-output-slowpath",
4647 [NAT_NEXT_OUT2IN_ED_FAST_PATH] = "nat44-ed-out2in",
4648 [NAT_NEXT_OUT2IN_ED_SLOW_PATH] = "nat44-ed-out2in-slowpath",
4649 [NAT_NEXT_IN2OUT_CLASSIFY] = "nat44-in2out-worker-handoff",
4650 [NAT_NEXT_OUT2IN_CLASSIFY] = "nat44-out2in-worker-handoff",
4656 * fd.io coding-style-patch-verification: ON
4659 * eval: (c-set-style "gnu")