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>
22 #include <snat/snat.h>
23 #include <snat/snat_ipfix_logging.h>
24 #include <snat/snat_det.h>
25 #include <snat/nat64.h>
26 #include <vnet/fib/fib_table.h>
27 #include <vnet/fib/ip4_fib.h>
29 #include <vpp/app/version.h>
31 snat_main_t snat_main;
34 /* Hook up input features */
35 VNET_FEATURE_INIT (ip4_snat_in2out, static) = {
36 .arc_name = "ip4-unicast",
37 .node_name = "snat-in2out",
38 .runs_before = VNET_FEATURES ("snat-out2in"),
40 VNET_FEATURE_INIT (ip4_snat_out2in, static) = {
41 .arc_name = "ip4-unicast",
42 .node_name = "snat-out2in",
43 .runs_before = VNET_FEATURES ("ip4-lookup"),
45 VNET_FEATURE_INIT (ip4_snat_det_in2out, static) = {
46 .arc_name = "ip4-unicast",
47 .node_name = "snat-det-in2out",
48 .runs_before = VNET_FEATURES ("snat-det-out2in"),
50 VNET_FEATURE_INIT (ip4_snat_det_out2in, static) = {
51 .arc_name = "ip4-unicast",
52 .node_name = "snat-det-out2in",
53 .runs_before = VNET_FEATURES ("ip4-lookup"),
55 VNET_FEATURE_INIT (ip4_snat_in2out_worker_handoff, static) = {
56 .arc_name = "ip4-unicast",
57 .node_name = "snat-in2out-worker-handoff",
58 .runs_before = VNET_FEATURES ("snat-out2in-worker-handoff"),
60 VNET_FEATURE_INIT (ip4_snat_out2in_worker_handoff, static) = {
61 .arc_name = "ip4-unicast",
62 .node_name = "snat-out2in-worker-handoff",
63 .runs_before = VNET_FEATURES ("ip4-lookup"),
65 VNET_FEATURE_INIT (ip4_snat_in2out_fast, static) = {
66 .arc_name = "ip4-unicast",
67 .node_name = "snat-in2out-fast",
68 .runs_before = VNET_FEATURES ("snat-out2in-fast"),
70 VNET_FEATURE_INIT (ip4_snat_out2in_fast, static) = {
71 .arc_name = "ip4-unicast",
72 .node_name = "snat-out2in-fast",
73 .runs_before = VNET_FEATURES ("ip4-lookup"),
75 VNET_FEATURE_INIT (ip4_snat_hairpin_dst, static) = {
76 .arc_name = "ip4-unicast",
77 .node_name = "snat-hairpin-dst",
78 .runs_before = VNET_FEATURES ("ip4-lookup"),
81 /* Hook up output features */
82 VNET_FEATURE_INIT (ip4_snat_in2out_output, static) = {
83 .arc_name = "ip4-output",
84 .node_name = "snat-in2out-output",
85 .runs_before = VNET_FEATURES ("interface-output"),
87 VNET_FEATURE_INIT (ip4_snat_in2out_output_worker_handoff, static) = {
88 .arc_name = "ip4-output",
89 .node_name = "snat-in2out-output-worker-handoff",
90 .runs_before = VNET_FEATURES ("interface-output"),
92 VNET_FEATURE_INIT (ip4_snat_hairpin_src, static) = {
93 .arc_name = "ip4-output",
94 .node_name = "snat-hairpin-src",
95 .runs_before = VNET_FEATURES ("interface-output"),
100 VLIB_PLUGIN_REGISTER () = {
101 .version = VPP_BUILD_VER,
102 .description = "Network Address Translation",
107 * @brief Add/del NAT address to FIB.
109 * Add the external NAT address to the FIB as receive entries. This ensures
110 * that VPP will reply to ARP for this address and we don't need to enable
111 * proxy ARP on the outside interface.
113 * @param addr IPv4 address.
114 * @param plen address prefix length
115 * @param sw_if_index Interface.
116 * @param is_add If 0 delete, otherwise add.
119 snat_add_del_addr_to_fib (ip4_address_t * addr, u8 p_len, u32 sw_if_index,
122 fib_prefix_t prefix = {
124 .fp_proto = FIB_PROTOCOL_IP4,
126 .ip4.as_u32 = addr->as_u32,
129 u32 fib_index = ip4_fib_table_get_index_for_sw_if_index(sw_if_index);
132 fib_table_entry_update_one_path(fib_index,
134 FIB_SOURCE_PLUGIN_HI,
135 (FIB_ENTRY_FLAG_CONNECTED |
136 FIB_ENTRY_FLAG_LOCAL |
137 FIB_ENTRY_FLAG_EXCLUSIVE),
144 FIB_ROUTE_PATH_FLAG_NONE);
146 fib_table_entry_delete(fib_index,
148 FIB_SOURCE_PLUGIN_HI);
151 void snat_add_address (snat_main_t *sm, ip4_address_t *addr, u32 vrf_id)
159 /* Check if address already exists */
160 vec_foreach (ap, sm->addresses)
162 if (ap->addr.as_u32 == addr->as_u32)
166 vec_add2 (sm->addresses, ap, 1);
170 fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4, vrf_id);
173 #define _(N, i, n, s) \
174 clib_bitmap_alloc (ap->busy_##n##_port_bitmap, 65535);
175 foreach_snat_protocol
178 /* Add external address to FIB */
179 pool_foreach (i, sm->interfaces,
184 snat_add_del_addr_to_fib(addr, 32, i->sw_if_index, 1);
187 pool_foreach (i, sm->output_feature_interfaces,
192 snat_add_del_addr_to_fib(addr, 32, i->sw_if_index, 1);
197 static int is_snat_address_used_in_static_mapping (snat_main_t *sm,
200 snat_static_mapping_t *m;
201 pool_foreach (m, sm->static_mappings,
203 if (m->external_addr.as_u32 == addr.as_u32)
210 void increment_v4_address (ip4_address_t * a)
214 v = clib_net_to_host_u32(a->as_u32) + 1;
215 a->as_u32 = clib_host_to_net_u32(v);
219 snat_add_static_mapping_when_resolved (snat_main_t * sm,
220 ip4_address_t l_addr,
225 snat_protocol_t proto,
229 snat_static_map_resolve_t *rp;
231 vec_add2 (sm->to_resolve, rp, 1);
232 rp->l_addr.as_u32 = l_addr.as_u32;
234 rp->sw_if_index = sw_if_index;
238 rp->addr_only = addr_only;
243 * @brief Add static mapping.
245 * Create static mapping between local addr+port and external addr+port.
247 * @param l_addr Local IPv4 address.
248 * @param e_addr External IPv4 address.
249 * @param l_port Local port number.
250 * @param e_port External port number.
251 * @param vrf_id VRF ID.
252 * @param addr_only If 0 address port and pair mapping, otherwise address only.
253 * @param sw_if_index External port instead of specific IP address.
254 * @param is_add If 0 delete static mapping, otherwise add.
258 int snat_add_static_mapping(ip4_address_t l_addr, ip4_address_t e_addr,
259 u16 l_port, u16 e_port, u32 vrf_id, int addr_only,
260 u32 sw_if_index, snat_protocol_t proto, int is_add)
262 snat_main_t * sm = &snat_main;
263 snat_static_mapping_t *m;
264 snat_session_key_t m_key;
265 clib_bihash_kv_8_8_t kv, value;
266 snat_address_t *a = 0;
269 snat_interface_t *interface;
272 /* If the external address is a specific interface address */
273 if (sw_if_index != ~0)
275 ip4_address_t * first_int_addr;
277 /* Might be already set... */
278 first_int_addr = ip4_interface_first_address
279 (sm->ip4_main, sw_if_index, 0 /* just want the address*/);
281 /* DHCP resolution required? */
282 if (first_int_addr == 0)
284 snat_add_static_mapping_when_resolved
285 (sm, l_addr, l_port, sw_if_index, e_port, vrf_id, proto,
290 e_addr.as_u32 = first_int_addr->as_u32;
294 m_key.port = addr_only ? 0 : e_port;
295 m_key.protocol = addr_only ? 0 : proto;
296 m_key.fib_index = sm->outside_fib_index;
297 kv.key = m_key.as_u64;
298 if (clib_bihash_search_8_8 (&sm->static_mapping_by_external, &kv, &value))
301 m = pool_elt_at_index (sm->static_mappings, value.value);
306 return VNET_API_ERROR_VALUE_EXIST;
308 /* Convert VRF id to FIB index */
311 p = hash_get (sm->ip4_main->fib_index_by_table_id, vrf_id);
313 return VNET_API_ERROR_NO_SUCH_FIB;
316 /* If not specified use inside VRF id from SNAT plugin startup config */
319 fib_index = sm->inside_fib_index;
320 vrf_id = sm->inside_vrf_id;
323 /* Find external address in allocated addresses and reserve port for
324 address and port pair mapping when dynamic translations enabled */
325 if (!addr_only && !(sm->static_mapping_only))
327 for (i = 0; i < vec_len (sm->addresses); i++)
329 if (sm->addresses[i].addr.as_u32 == e_addr.as_u32)
331 a = sm->addresses + i;
332 /* External port must be unused */
335 #define _(N, j, n, s) \
336 case SNAT_PROTOCOL_##N: \
337 if (clib_bitmap_get_no_check (a->busy_##n##_port_bitmap, e_port)) \
338 return VNET_API_ERROR_INVALID_VALUE; \
339 clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, e_port, 1); \
341 a->busy_##n##_ports++; \
343 foreach_snat_protocol
346 clib_warning("unknown_protocol");
347 return VNET_API_ERROR_INVALID_VALUE_2;
352 /* External address must be allocated */
354 return VNET_API_ERROR_NO_SUCH_ENTRY;
357 pool_get (sm->static_mappings, m);
358 memset (m, 0, sizeof (*m));
359 m->local_addr = l_addr;
360 m->external_addr = e_addr;
361 m->addr_only = addr_only;
363 m->fib_index = fib_index;
366 m->local_port = l_port;
367 m->external_port = e_port;
371 m_key.addr = m->local_addr;
372 m_key.port = m->local_port;
373 m_key.protocol = m->proto;
374 m_key.fib_index = m->fib_index;
375 kv.key = m_key.as_u64;
376 kv.value = m - sm->static_mappings;
377 clib_bihash_add_del_8_8(&sm->static_mapping_by_local, &kv, 1);
379 m_key.addr = m->external_addr;
380 m_key.port = m->external_port;
381 m_key.fib_index = sm->outside_fib_index;
382 kv.key = m_key.as_u64;
383 kv.value = m - sm->static_mappings;
384 clib_bihash_add_del_8_8(&sm->static_mapping_by_external, &kv, 1);
389 snat_user_key_t w_key0;
390 snat_worker_key_t w_key1;
392 w_key0.addr = m->local_addr;
393 w_key0.fib_index = m->fib_index;
394 kv.key = w_key0.as_u64;
396 if (clib_bihash_search_8_8 (&sm->worker_by_in, &kv, &value))
398 kv.value = sm->first_worker_index +
399 sm->workers[sm->next_worker++ % vec_len (sm->workers)];
401 clib_bihash_add_del_8_8 (&sm->worker_by_in, &kv, 1);
405 kv.value = value.value;
408 w_key1.addr = m->external_addr;
409 w_key1.port = clib_host_to_net_u16 (m->external_port);
410 w_key1.fib_index = sm->outside_fib_index;
411 kv.key = w_key1.as_u64;
412 clib_bihash_add_del_8_8 (&sm->worker_by_out, &kv, 1);
418 return VNET_API_ERROR_NO_SUCH_ENTRY;
420 /* Free external address port */
421 if (!addr_only && !(sm->static_mapping_only))
423 for (i = 0; i < vec_len (sm->addresses); i++)
425 if (sm->addresses[i].addr.as_u32 == e_addr.as_u32)
427 a = sm->addresses + i;
430 #define _(N, j, n, s) \
431 case SNAT_PROTOCOL_##N: \
432 clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, e_port, 0); \
434 a->busy_##n##_ports--; \
436 foreach_snat_protocol
439 clib_warning("unknown_protocol");
440 return VNET_API_ERROR_INVALID_VALUE_2;
447 m_key.addr = m->local_addr;
448 m_key.port = m->local_port;
449 m_key.protocol = m->proto;
450 m_key.fib_index = m->fib_index;
451 kv.key = m_key.as_u64;
452 clib_bihash_add_del_8_8(&sm->static_mapping_by_local, &kv, 0);
454 m_key.addr = m->external_addr;
455 m_key.port = m->external_port;
456 m_key.fib_index = sm->outside_fib_index;
457 kv.key = m_key.as_u64;
458 clib_bihash_add_del_8_8(&sm->static_mapping_by_external, &kv, 0);
460 /* Delete session(s) for static mapping if exist */
461 if (!(sm->static_mapping_only) ||
462 (sm->static_mapping_only && sm->static_mapping_connection_tracking))
464 snat_user_key_t u_key;
466 dlist_elt_t * head, * elt;
467 u32 elt_index, head_index, del_elt_index;
471 snat_main_per_thread_data_t *tsm;
473 u_key.addr = m->local_addr;
474 u_key.fib_index = m->fib_index;
475 kv.key = u_key.as_u64;
476 if (!clib_bihash_search_8_8 (&sm->user_hash, &kv, &value))
478 user_index = value.value;
479 if (!clib_bihash_search_8_8 (&sm->worker_by_in, &kv, &value))
480 tsm = vec_elt_at_index (sm->per_thread_data, value.value);
482 tsm = vec_elt_at_index (sm->per_thread_data, sm->num_workers);
483 u = pool_elt_at_index (tsm->users, user_index);
484 if (u->nstaticsessions)
486 head_index = u->sessions_per_user_list_head_index;
487 head = pool_elt_at_index (tsm->list_pool, head_index);
488 elt_index = head->next;
489 elt = pool_elt_at_index (tsm->list_pool, elt_index);
490 ses_index = elt->value;
491 while (ses_index != ~0)
493 s = pool_elt_at_index (tsm->sessions, ses_index);
494 del_elt_index = elt_index;
495 elt_index = elt->next;
496 elt = pool_elt_at_index (tsm->list_pool, elt_index);
497 ses_index = elt->value;
501 if ((s->out2in.addr.as_u32 != e_addr.as_u32) &&
502 (clib_net_to_host_u16 (s->out2in.port) != e_port))
506 if (snat_is_unk_proto_session (s))
508 clib_bihash_kv_16_8_t up_kv;
509 snat_unk_proto_ses_key_t up_key;
510 up_key.l_addr = s->in2out.addr;
511 up_key.r_addr = s->ext_host_addr;
512 up_key.fib_index = s->in2out.fib_index;
513 up_key.proto = s->in2out.port;
514 up_key.rsvd[0] = up_key.rsvd[1] = up_key.rsvd[2] = 0;
515 up_kv.key[0] = up_key.as_u64[0];
516 up_kv.key[1] = up_key.as_u64[1];
517 if (clib_bihash_add_del_16_8 (&sm->in2out_unk_proto,
519 clib_warning ("in2out key del failed");
521 up_key.l_addr = s->out2in.addr;
522 up_key.fib_index = s->out2in.fib_index;
523 up_kv.key[0] = up_key.as_u64[0];
524 up_kv.key[1] = up_key.as_u64[1];
525 if (clib_bihash_add_del_16_8 (&sm->out2in_unk_proto,
527 clib_warning ("out2in key del failed");
532 snat_ipfix_logging_nat44_ses_delete(s->in2out.addr.as_u32,
533 s->out2in.addr.as_u32,
537 s->in2out.fib_index);
539 value.key = s->in2out.as_u64;
540 if (clib_bihash_add_del_8_8 (&sm->in2out, &value, 0))
541 clib_warning ("in2out key del failed");
542 value.key = s->out2in.as_u64;
543 if (clib_bihash_add_del_8_8 (&sm->out2in, &value, 0))
544 clib_warning ("out2in key del failed");
546 pool_put (tsm->sessions, s);
548 clib_dlist_remove (tsm->list_pool, del_elt_index);
549 pool_put_index (tsm->list_pool, del_elt_index);
550 u->nstaticsessions--;
557 pool_put (tsm->users, u);
558 clib_bihash_add_del_8_8 (&sm->user_hash, &kv, 0);
564 /* Delete static mapping from pool */
565 pool_put (sm->static_mappings, m);
571 /* Add/delete external address to FIB */
572 pool_foreach (interface, sm->interfaces,
574 if (interface->is_inside)
577 snat_add_del_addr_to_fib(&e_addr, 32, interface->sw_if_index, is_add);
580 pool_foreach (interface, sm->output_feature_interfaces,
582 if (interface->is_inside)
585 snat_add_del_addr_to_fib(&e_addr, 32, interface->sw_if_index, is_add);
592 int snat_del_address (snat_main_t *sm, ip4_address_t addr, u8 delete_sm)
594 snat_address_t *a = 0;
596 u32 *ses_to_be_removed = 0, *ses_index;
597 clib_bihash_kv_8_8_t kv, value;
598 snat_user_key_t user_key;
600 snat_main_per_thread_data_t *tsm;
601 snat_static_mapping_t *m;
602 snat_interface_t *interface;
605 /* Find SNAT address */
606 for (i=0; i < vec_len (sm->addresses); i++)
608 if (sm->addresses[i].addr.as_u32 == addr.as_u32)
610 a = sm->addresses + i;
615 return VNET_API_ERROR_NO_SUCH_ENTRY;
619 pool_foreach (m, sm->static_mappings,
621 if (m->external_addr.as_u32 == addr.as_u32)
622 (void) snat_add_static_mapping (m->local_addr, m->external_addr,
623 m->local_port, m->external_port,
624 m->vrf_id, m->addr_only, ~0,
630 /* Check if address is used in some static mapping */
631 if (is_snat_address_used_in_static_mapping(sm, addr))
633 clib_warning ("address used in static mapping");
634 return VNET_API_ERROR_UNSPECIFIED;
638 if (a->fib_index != ~0)
639 fib_table_unlock(a->fib_index, FIB_PROTOCOL_IP4);
641 /* Delete sessions using address */
642 if (a->busy_tcp_ports || a->busy_udp_ports || a->busy_icmp_ports)
644 vec_foreach (tsm, sm->per_thread_data)
646 pool_foreach (ses, tsm->sessions, ({
647 if (ses->out2in.addr.as_u32 == addr.as_u32)
649 if (snat_is_unk_proto_session (ses))
651 clib_bihash_kv_16_8_t up_kv;
652 snat_unk_proto_ses_key_t up_key;
653 up_key.l_addr = ses->in2out.addr;
654 up_key.r_addr = ses->ext_host_addr;
655 up_key.fib_index = ses->in2out.fib_index;
656 up_key.proto = ses->in2out.port;
657 up_key.rsvd[0] = up_key.rsvd[1] = up_key.rsvd[2] = 0;
658 up_kv.key[0] = up_key.as_u64[0];
659 up_kv.key[1] = up_key.as_u64[1];
660 if (clib_bihash_add_del_16_8 (&sm->in2out_unk_proto,
662 clib_warning ("in2out key del failed");
664 up_key.l_addr = ses->out2in.addr;
665 up_key.fib_index = ses->out2in.fib_index;
666 up_kv.key[0] = up_key.as_u64[0];
667 up_kv.key[1] = up_key.as_u64[1];
668 if (clib_bihash_add_del_16_8 (&sm->out2in_unk_proto,
670 clib_warning ("out2in key del failed");
675 snat_ipfix_logging_nat44_ses_delete(ses->in2out.addr.as_u32,
676 ses->out2in.addr.as_u32,
677 ses->in2out.protocol,
680 ses->in2out.fib_index);
681 kv.key = ses->in2out.as_u64;
682 clib_bihash_add_del_8_8 (&sm->in2out, &kv, 0);
683 kv.key = ses->out2in.as_u64;
684 clib_bihash_add_del_8_8 (&sm->out2in, &kv, 0);
686 vec_add1 (ses_to_be_removed, ses - tsm->sessions);
687 clib_dlist_remove (tsm->list_pool, ses->per_user_index);
688 user_key.addr = ses->in2out.addr;
689 user_key.fib_index = ses->in2out.fib_index;
690 kv.key = user_key.as_u64;
691 if (!clib_bihash_search_8_8 (&sm->user_hash, &kv, &value))
693 u = pool_elt_at_index (tsm->users, value.value);
699 vec_foreach (ses_index, ses_to_be_removed)
700 pool_put_index (tsm->sessions, ses_index[0]);
702 vec_free (ses_to_be_removed);
706 vec_del1 (sm->addresses, i);
708 /* Delete external address from FIB */
709 pool_foreach (interface, sm->interfaces,
711 if (interface->is_inside)
714 snat_add_del_addr_to_fib(&addr, 32, interface->sw_if_index, 0);
717 pool_foreach (interface, sm->output_feature_interfaces,
719 if (interface->is_inside)
722 snat_add_del_addr_to_fib(&addr, 32, interface->sw_if_index, 0);
729 int snat_interface_add_del (u32 sw_if_index, u8 is_inside, int is_del)
731 snat_main_t *sm = &snat_main;
733 const char * feature_name;
735 snat_static_mapping_t * m;
738 if (sm->static_mapping_only && !(sm->static_mapping_connection_tracking))
739 feature_name = is_inside ? "snat-in2out-fast" : "snat-out2in-fast";
742 if (sm->num_workers > 1 && !sm->deterministic)
743 feature_name = is_inside ? "snat-in2out-worker-handoff" : "snat-out2in-worker-handoff";
744 else if (sm->deterministic)
745 feature_name = is_inside ? "snat-det-in2out" : "snat-det-out2in";
747 feature_name = is_inside ? "snat-in2out" : "snat-out2in";
750 vnet_feature_enable_disable ("ip4-unicast", feature_name, sw_if_index,
753 if (sm->fq_in2out_index == ~0 && !sm->deterministic && sm->num_workers > 1)
754 sm->fq_in2out_index = vlib_frame_queue_main_init (sm->in2out_node_index, 0);
756 if (sm->fq_out2in_index == ~0 && !sm->deterministic && sm->num_workers > 1)
757 sm->fq_out2in_index = vlib_frame_queue_main_init (sm->out2in_node_index, 0);
759 pool_foreach (i, sm->interfaces,
761 if (i->sw_if_index == sw_if_index)
764 pool_put (sm->interfaces, i);
766 return VNET_API_ERROR_VALUE_EXIST;
773 return VNET_API_ERROR_NO_SUCH_ENTRY;
775 pool_get (sm->interfaces, i);
776 i->sw_if_index = sw_if_index;
777 i->is_inside = is_inside;
779 /* Add/delete external addresses to FIB */
784 vec_foreach (ap, sm->addresses)
785 snat_add_del_addr_to_fib(&ap->addr, 32, sw_if_index, !is_del);
787 pool_foreach (m, sm->static_mappings,
792 snat_add_del_addr_to_fib(&m->external_addr, 32, sw_if_index, !is_del);
795 pool_foreach (dm, sm->det_maps,
797 snat_add_del_addr_to_fib(&dm->out_addr, dm->out_plen, sw_if_index, !is_del);
803 int snat_interface_add_del_output_feature (u32 sw_if_index,
807 snat_main_t *sm = &snat_main;
810 snat_static_mapping_t * m;
812 if (sm->deterministic ||
813 (sm->static_mapping_only && !(sm->static_mapping_connection_tracking)))
814 return VNET_API_ERROR_UNSUPPORTED;
818 vnet_feature_enable_disable ("ip4-unicast", "snat-hairpin-dst",
819 sw_if_index, !is_del, 0, 0);
820 vnet_feature_enable_disable ("ip4-output", "snat-hairpin-src",
821 sw_if_index, !is_del, 0, 0);
825 if (sm->num_workers > 1)
827 vnet_feature_enable_disable ("ip4-unicast", "snat-out2in-worker-handoff",
828 sw_if_index, !is_del, 0, 0);
829 vnet_feature_enable_disable ("ip4-output",
830 "snat-in2out-output-worker-handoff",
831 sw_if_index, !is_del, 0, 0);
835 vnet_feature_enable_disable ("ip4-unicast", "snat-out2in", sw_if_index,
837 vnet_feature_enable_disable ("ip4-output", "snat-in2out-output",
838 sw_if_index, !is_del, 0, 0);
842 if (sm->fq_in2out_output_index == ~0 && sm->num_workers > 1)
843 sm->fq_in2out_output_index =
844 vlib_frame_queue_main_init (sm->in2out_output_node_index, 0);
846 if (sm->fq_out2in_index == ~0 && sm->num_workers > 1)
847 sm->fq_out2in_index = vlib_frame_queue_main_init (sm->out2in_node_index, 0);
849 pool_foreach (i, sm->output_feature_interfaces,
851 if (i->sw_if_index == sw_if_index)
854 pool_put (sm->output_feature_interfaces, i);
856 return VNET_API_ERROR_VALUE_EXIST;
863 return VNET_API_ERROR_NO_SUCH_ENTRY;
865 pool_get (sm->output_feature_interfaces, i);
866 i->sw_if_index = sw_if_index;
867 i->is_inside = is_inside;
869 /* Add/delete external addresses to FIB */
874 vec_foreach (ap, sm->addresses)
875 snat_add_del_addr_to_fib(&ap->addr, 32, sw_if_index, !is_del);
877 pool_foreach (m, sm->static_mappings,
882 snat_add_del_addr_to_fib(&m->external_addr, 32, sw_if_index, !is_del);
888 int snat_set_workers (uword * bitmap)
890 snat_main_t *sm = &snat_main;
893 if (sm->num_workers < 2)
894 return VNET_API_ERROR_FEATURE_DISABLED;
896 if (clib_bitmap_last_set (bitmap) >= sm->num_workers)
897 return VNET_API_ERROR_INVALID_WORKER;
899 vec_free (sm->workers);
900 clib_bitmap_foreach (i, bitmap,
902 vec_add1(sm->workers, i);
910 snat_ip4_add_del_interface_address_cb (ip4_main_t * im,
913 ip4_address_t * address,
915 u32 if_address_index,
918 static clib_error_t * snat_init (vlib_main_t * vm)
920 snat_main_t * sm = &snat_main;
921 clib_error_t * error = 0;
922 ip4_main_t * im = &ip4_main;
923 ip_lookup_main_t * lm = &im->lookup_main;
925 vlib_thread_registration_t *tr;
926 vlib_thread_main_t *tm = vlib_get_thread_main ();
929 ip4_add_del_interface_address_callback_t cb4;
932 sm->vnet_main = vnet_get_main();
934 sm->ip4_lookup_main = lm;
935 sm->api_main = &api_main;
936 sm->first_worker_index = 0;
940 sm->fq_in2out_index = ~0;
941 sm->fq_out2in_index = ~0;
942 sm->udp_timeout = SNAT_UDP_TIMEOUT;
943 sm->tcp_established_timeout = SNAT_TCP_ESTABLISHED_TIMEOUT;
944 sm->tcp_transitory_timeout = SNAT_TCP_TRANSITORY_TIMEOUT;
945 sm->icmp_timeout = SNAT_ICMP_TIMEOUT;
947 p = hash_get_mem (tm->thread_registrations_by_name, "workers");
950 tr = (vlib_thread_registration_t *) p[0];
953 sm->num_workers = tr->count;
954 sm->first_worker_index = tr->first_index;
958 /* Use all available workers by default */
959 if (sm->num_workers > 1)
961 for (i=0; i < sm->num_workers; i++)
962 bitmap = clib_bitmap_set (bitmap, i, 1);
963 snat_set_workers(bitmap);
964 clib_bitmap_free (bitmap);
967 error = snat_api_init(vm, sm);
971 /* Set up the interface address add/del callback */
972 cb4.function = snat_ip4_add_del_interface_address_cb;
973 cb4.function_opaque = 0;
975 vec_add1 (im->add_del_interface_address_callbacks, cb4);
977 /* Init IPFIX logging */
978 snat_ipfix_logging_init(vm);
980 error = nat64_init(vm);
985 VLIB_INIT_FUNCTION (snat_init);
987 void snat_free_outside_address_and_port (snat_main_t * sm,
988 snat_session_key_t * k,
992 u16 port_host_byte_order = clib_net_to_host_u16 (k->port);
994 ASSERT (address_index < vec_len (sm->addresses));
996 a = sm->addresses + address_index;
1000 #define _(N, i, n, s) \
1001 case SNAT_PROTOCOL_##N: \
1002 ASSERT (clib_bitmap_get_no_check (a->busy_##n##_port_bitmap, \
1003 port_host_byte_order) == 1); \
1004 clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, \
1005 port_host_byte_order, 0); \
1006 a->busy_##n##_ports--; \
1008 foreach_snat_protocol
1011 clib_warning("unknown_protocol");
1017 * @brief Match SNAT static mapping.
1019 * @param sm SNAT main.
1020 * @param match Address and port to match.
1021 * @param mapping External or local address and port of the matched mapping.
1022 * @param by_external If 0 match by local address otherwise match by external
1024 * @param is_addr_only If matched mapping is address only
1026 * @returns 0 if match found otherwise 1.
1028 int snat_static_mapping_match (snat_main_t * sm,
1029 snat_session_key_t match,
1030 snat_session_key_t * mapping,
1034 clib_bihash_kv_8_8_t kv, value;
1035 snat_static_mapping_t *m;
1036 snat_session_key_t m_key;
1037 clib_bihash_8_8_t *mapping_hash = &sm->static_mapping_by_local;
1040 mapping_hash = &sm->static_mapping_by_external;
1042 m_key.addr = match.addr;
1043 m_key.port = clib_net_to_host_u16 (match.port);
1044 m_key.protocol = match.protocol;
1045 m_key.fib_index = match.fib_index;
1047 kv.key = m_key.as_u64;
1049 if (clib_bihash_search_8_8 (mapping_hash, &kv, &value))
1051 /* Try address only mapping */
1054 kv.key = m_key.as_u64;
1055 if (clib_bihash_search_8_8 (mapping_hash, &kv, &value))
1059 m = pool_elt_at_index (sm->static_mappings, value.value);
1063 mapping->addr = m->local_addr;
1064 /* Address only mapping doesn't change port */
1065 mapping->port = m->addr_only ? match.port
1066 : clib_host_to_net_u16 (m->local_port);
1067 mapping->fib_index = m->fib_index;
1071 mapping->addr = m->external_addr;
1072 /* Address only mapping doesn't change port */
1073 mapping->port = m->addr_only ? match.port
1074 : clib_host_to_net_u16 (m->external_port);
1075 mapping->fib_index = sm->outside_fib_index;
1078 if (PREDICT_FALSE(is_addr_only != 0))
1079 *is_addr_only = m->addr_only;
1084 int snat_alloc_outside_address_and_port (snat_main_t * sm,
1086 snat_session_key_t * k,
1087 u32 * address_indexp)
1093 for (i = 0; i < vec_len (sm->addresses); i++)
1095 a = sm->addresses + i;
1096 if (sm->vrf_mode && a->fib_index != ~0 && a->fib_index != fib_index)
1098 switch (k->protocol)
1100 #define _(N, j, n, s) \
1101 case SNAT_PROTOCOL_##N: \
1102 if (a->busy_##n##_ports < (65535-1024)) \
1106 portnum = random_u32 (&sm->random_seed); \
1107 portnum &= 0xFFFF; \
1108 if (portnum < 1024) \
1110 if (clib_bitmap_get_no_check (a->busy_##n##_port_bitmap, portnum)) \
1112 clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, portnum, 1); \
1113 a->busy_##n##_ports++; \
1114 k->addr = a->addr; \
1115 k->port = clib_host_to_net_u16(portnum); \
1116 *address_indexp = i; \
1121 foreach_snat_protocol
1124 clib_warning("unknown protocol");
1129 /* Totally out of translations to use... */
1130 snat_ipfix_logging_addresses_exhausted(0);
1135 static clib_error_t *
1136 add_address_command_fn (vlib_main_t * vm,
1137 unformat_input_t * input,
1138 vlib_cli_command_t * cmd)
1140 unformat_input_t _line_input, *line_input = &_line_input;
1141 snat_main_t * sm = &snat_main;
1142 ip4_address_t start_addr, end_addr, this_addr;
1143 u32 start_host_order, end_host_order;
1148 clib_error_t *error = 0;
1150 /* Get a line of input. */
1151 if (!unformat_user (input, unformat_line_input, line_input))
1154 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1156 if (unformat (line_input, "%U - %U",
1157 unformat_ip4_address, &start_addr,
1158 unformat_ip4_address, &end_addr))
1160 else if (unformat (line_input, "tenant-vrf %u", &vrf_id))
1162 else if (unformat (line_input, "%U", unformat_ip4_address, &start_addr))
1163 end_addr = start_addr;
1164 else if (unformat (line_input, "del"))
1168 error = clib_error_return (0, "unknown input '%U'",
1169 format_unformat_error, line_input);
1174 if (sm->static_mapping_only)
1176 error = clib_error_return (0, "static mapping only mode");
1180 start_host_order = clib_host_to_net_u32 (start_addr.as_u32);
1181 end_host_order = clib_host_to_net_u32 (end_addr.as_u32);
1183 if (end_host_order < start_host_order)
1185 error = clib_error_return (0, "end address less than start address");
1189 count = (end_host_order - start_host_order) + 1;
1192 clib_warning ("%U - %U, %d addresses...",
1193 format_ip4_address, &start_addr,
1194 format_ip4_address, &end_addr,
1197 this_addr = start_addr;
1199 for (i = 0; i < count; i++)
1202 snat_add_address (sm, &this_addr, vrf_id);
1204 rv = snat_del_address (sm, this_addr, 0);
1208 case VNET_API_ERROR_NO_SUCH_ENTRY:
1209 error = clib_error_return (0, "S-NAT address not exist.");
1211 case VNET_API_ERROR_UNSPECIFIED:
1212 error = clib_error_return (0, "S-NAT address used in static mapping.");
1218 increment_v4_address (&this_addr);
1222 unformat_free (line_input);
1227 VLIB_CLI_COMMAND (add_address_command, static) = {
1228 .path = "snat add address",
1229 .short_help = "snat add addresses <ip4-range-start> [- <ip4-range-end>] "
1230 "[tenant-vrf <vrf-id>] [del]",
1231 .function = add_address_command_fn,
1234 static clib_error_t *
1235 snat_feature_command_fn (vlib_main_t * vm,
1236 unformat_input_t * input,
1237 vlib_cli_command_t * cmd)
1239 unformat_input_t _line_input, *line_input = &_line_input;
1240 vnet_main_t * vnm = vnet_get_main();
1241 clib_error_t * error = 0;
1243 u32 * inside_sw_if_indices = 0;
1244 u32 * outside_sw_if_indices = 0;
1245 u8 is_output_feature = 0;
1251 /* Get a line of input. */
1252 if (!unformat_user (input, unformat_line_input, line_input))
1255 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1257 if (unformat (line_input, "in %U", unformat_vnet_sw_interface,
1259 vec_add1 (inside_sw_if_indices, sw_if_index);
1260 else if (unformat (line_input, "out %U", unformat_vnet_sw_interface,
1262 vec_add1 (outside_sw_if_indices, sw_if_index);
1263 else if (unformat (line_input, "output-feature"))
1264 is_output_feature = 1;
1265 else if (unformat (line_input, "del"))
1269 error = clib_error_return (0, "unknown input '%U'",
1270 format_unformat_error, line_input);
1275 if (vec_len (inside_sw_if_indices))
1277 for (i = 0; i < vec_len(inside_sw_if_indices); i++)
1279 sw_if_index = inside_sw_if_indices[i];
1280 if (is_output_feature)
1282 if (snat_interface_add_del_output_feature (sw_if_index, 1, is_del))
1284 error = clib_error_return (0, "%s %U failed",
1285 is_del ? "del" : "add",
1286 format_vnet_sw_interface_name, vnm,
1287 vnet_get_sw_interface (vnm,
1294 if (snat_interface_add_del (sw_if_index, 1, is_del))
1296 error = clib_error_return (0, "%s %U failed",
1297 is_del ? "del" : "add",
1298 format_vnet_sw_interface_name, vnm,
1299 vnet_get_sw_interface (vnm,
1307 if (vec_len (outside_sw_if_indices))
1309 for (i = 0; i < vec_len(outside_sw_if_indices); i++)
1311 sw_if_index = outside_sw_if_indices[i];
1312 if (is_output_feature)
1314 if (snat_interface_add_del_output_feature (sw_if_index, 0, is_del))
1316 error = clib_error_return (0, "%s %U failed",
1317 is_del ? "del" : "add",
1318 format_vnet_sw_interface_name, vnm,
1319 vnet_get_sw_interface (vnm,
1326 if (snat_interface_add_del (sw_if_index, 0, is_del))
1328 error = clib_error_return (0, "%s %U failed",
1329 is_del ? "del" : "add",
1330 format_vnet_sw_interface_name, vnm,
1331 vnet_get_sw_interface (vnm,
1340 unformat_free (line_input);
1341 vec_free (inside_sw_if_indices);
1342 vec_free (outside_sw_if_indices);
1347 VLIB_CLI_COMMAND (set_interface_snat_command, static) = {
1348 .path = "set interface snat",
1349 .function = snat_feature_command_fn,
1350 .short_help = "set interface snat in <intfc> out <intfc> [output-feature] "
1355 unformat_snat_protocol (unformat_input_t * input, va_list * args)
1357 u32 *r = va_arg (*args, u32 *);
1360 #define _(N, i, n, s) else if (unformat (input, s)) *r = SNAT_PROTOCOL_##N;
1361 foreach_snat_protocol
1369 format_snat_protocol (u8 * s, va_list * args)
1371 u32 i = va_arg (*args, u32);
1376 #define _(N, j, n, str) case SNAT_PROTOCOL_##N: t = (u8 *) str; break;
1377 foreach_snat_protocol
1380 s = format (s, "unknown");
1382 s = format (s, "%s", t);
1386 static clib_error_t *
1387 add_static_mapping_command_fn (vlib_main_t * vm,
1388 unformat_input_t * input,
1389 vlib_cli_command_t * cmd)
1391 unformat_input_t _line_input, *line_input = &_line_input;
1392 clib_error_t * error = 0;
1393 ip4_address_t l_addr, e_addr;
1394 u32 l_port = 0, e_port = 0, vrf_id = ~0;
1397 u32 sw_if_index = ~0;
1398 vnet_main_t * vnm = vnet_get_main();
1400 snat_protocol_t proto;
1403 /* Get a line of input. */
1404 if (!unformat_user (input, unformat_line_input, line_input))
1407 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1409 if (unformat (line_input, "local %U %u", unformat_ip4_address, &l_addr,
1412 else if (unformat (line_input, "local %U", unformat_ip4_address, &l_addr))
1414 else if (unformat (line_input, "external %U %u", unformat_ip4_address,
1417 else if (unformat (line_input, "external %U", unformat_ip4_address,
1420 else if (unformat (line_input, "external %U %u",
1421 unformat_vnet_sw_interface, vnm, &sw_if_index,
1425 else if (unformat (line_input, "external %U",
1426 unformat_vnet_sw_interface, vnm, &sw_if_index))
1428 else if (unformat (line_input, "vrf %u", &vrf_id))
1430 else if (unformat (line_input, "%U", unformat_snat_protocol, &proto))
1432 else if (unformat (line_input, "del"))
1436 error = clib_error_return (0, "unknown input: '%U'",
1437 format_unformat_error, line_input);
1442 if (!addr_only && !proto_set)
1444 error = clib_error_return (0, "missing protocol");
1448 rv = snat_add_static_mapping(l_addr, e_addr, (u16) l_port, (u16) e_port,
1449 vrf_id, addr_only, sw_if_index, proto, is_add);
1453 case VNET_API_ERROR_INVALID_VALUE:
1454 error = clib_error_return (0, "External port already in use.");
1456 case VNET_API_ERROR_NO_SUCH_ENTRY:
1458 error = clib_error_return (0, "External addres must be allocated.");
1460 error = clib_error_return (0, "Mapping not exist.");
1462 case VNET_API_ERROR_NO_SUCH_FIB:
1463 error = clib_error_return (0, "No such VRF id.");
1465 case VNET_API_ERROR_VALUE_EXIST:
1466 error = clib_error_return (0, "Mapping already exist.");
1473 unformat_free (line_input);
1480 * @cliexstart{snat add static mapping}
1481 * Static mapping allows hosts on the external network to initiate connection
1482 * to to the local network host.
1483 * To create static mapping between local host address 10.0.0.3 port 6303 and
1484 * external address 4.4.4.4 port 3606 for TCP protocol use:
1485 * vpp# snat add static mapping local tcp 10.0.0.3 6303 external 4.4.4.4 3606
1486 * If not runnig "static mapping only" S-NAT plugin mode use before:
1487 * vpp# snat add address 4.4.4.4
1488 * To create static mapping between local and external address use:
1489 * vpp# snat add static mapping local 10.0.0.3 external 4.4.4.4
1492 VLIB_CLI_COMMAND (add_static_mapping_command, static) = {
1493 .path = "snat add static mapping",
1494 .function = add_static_mapping_command_fn,
1496 "snat add static mapping local tcp|udp|icmp <addr> [<port>] external <addr> [<port>] [vrf <table-id>] [del]",
1499 static clib_error_t *
1500 set_workers_command_fn (vlib_main_t * vm,
1501 unformat_input_t * input,
1502 vlib_cli_command_t * cmd)
1504 unformat_input_t _line_input, *line_input = &_line_input;
1507 clib_error_t *error = 0;
1509 /* Get a line of input. */
1510 if (!unformat_user (input, unformat_line_input, line_input))
1513 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1515 if (unformat (line_input, "%U", unformat_bitmap_list, &bitmap))
1519 error = clib_error_return (0, "unknown input '%U'",
1520 format_unformat_error, line_input);
1527 error = clib_error_return (0, "List of workers must be specified.");
1531 rv = snat_set_workers(bitmap);
1533 clib_bitmap_free (bitmap);
1537 case VNET_API_ERROR_INVALID_WORKER:
1538 error = clib_error_return (0, "Invalid worker(s).");
1540 case VNET_API_ERROR_FEATURE_DISABLED:
1541 error = clib_error_return (0,
1542 "Supported only if 2 or more workes available.");
1549 unformat_free (line_input);
1556 * @cliexstart{set snat workers}
1557 * Set SNAT workers if 2 or more workers available, use:
1558 * vpp# set snat workers 0-2,5
1561 VLIB_CLI_COMMAND (set_workers_command, static) = {
1562 .path = "set snat workers",
1563 .function = set_workers_command_fn,
1565 "set snat workers <workers-list>",
1568 static clib_error_t *
1569 snat_ipfix_logging_enable_disable_command_fn (vlib_main_t * vm,
1570 unformat_input_t * input,
1571 vlib_cli_command_t * cmd)
1573 unformat_input_t _line_input, *line_input = &_line_input;
1578 clib_error_t *error = 0;
1580 /* Get a line of input. */
1581 if (!unformat_user (input, unformat_line_input, line_input))
1584 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1586 if (unformat (line_input, "domain %d", &domain_id))
1588 else if (unformat (line_input, "src-port %d", &src_port))
1590 else if (unformat (line_input, "disable"))
1594 error = clib_error_return (0, "unknown input '%U'",
1595 format_unformat_error, line_input);
1600 rv = snat_ipfix_logging_enable_disable (enable, domain_id, (u16) src_port);
1604 error = clib_error_return (0, "ipfix logging enable failed");
1609 unformat_free (line_input);
1616 * @cliexstart{snat ipfix logging}
1617 * To enable SNAT IPFIX logging use:
1618 * vpp# snat ipfix logging
1619 * To set IPFIX exporter use:
1620 * vpp# set ipfix exporter collector 10.10.10.3 src 10.10.10.1
1623 VLIB_CLI_COMMAND (snat_ipfix_logging_enable_disable_command, static) = {
1624 .path = "snat ipfix logging",
1625 .function = snat_ipfix_logging_enable_disable_command_fn,
1626 .short_help = "snat ipfix logging [domain <domain-id>] [src-port <port>] [disable]",
1630 snat_get_worker_in2out_cb (ip4_header_t * ip0, u32 rx_fib_index0)
1632 snat_main_t *sm = &snat_main;
1633 snat_user_key_t key0;
1634 clib_bihash_kv_8_8_t kv0, value0;
1635 u32 next_worker_index = 0;
1637 key0.addr = ip0->src_address;
1638 key0.fib_index = rx_fib_index0;
1640 kv0.key = key0.as_u64;
1642 /* Ever heard of of the "user" before? */
1643 if (clib_bihash_search_8_8 (&sm->worker_by_in, &kv0, &value0))
1645 /* No, assign next available worker (RR) */
1646 next_worker_index = sm->first_worker_index;
1647 if (vec_len (sm->workers))
1649 next_worker_index +=
1650 sm->workers[sm->next_worker++ % _vec_len (sm->workers)];
1653 /* add non-traslated packets worker lookup */
1654 kv0.value = next_worker_index;
1655 clib_bihash_add_del_8_8 (&sm->worker_by_in, &kv0, 1);
1658 next_worker_index = value0.value;
1660 return next_worker_index;
1664 snat_get_worker_out2in_cb (ip4_header_t * ip0, u32 rx_fib_index0)
1666 snat_main_t *sm = &snat_main;
1667 snat_worker_key_t key0;
1668 clib_bihash_kv_8_8_t kv0, value0;
1669 udp_header_t * udp0;
1670 u32 next_worker_index = 0;
1672 udp0 = ip4_next_header (ip0);
1674 key0.addr = ip0->dst_address;
1675 key0.port = udp0->dst_port;
1676 key0.fib_index = rx_fib_index0;
1678 if (PREDICT_FALSE(ip0->protocol == IP_PROTOCOL_ICMP))
1680 icmp46_header_t * icmp0 = (icmp46_header_t *) udp0;
1681 icmp_echo_header_t *echo0 = (icmp_echo_header_t *)(icmp0+1);
1682 key0.port = echo0->identifier;
1685 kv0.key = key0.as_u64;
1687 /* Ever heard of of the "user" before? */
1688 if (clib_bihash_search_8_8 (&sm->worker_by_out, &kv0, &value0))
1691 kv0.key = key0.as_u64;
1693 if (clib_bihash_search_8_8 (&sm->worker_by_out, &kv0, &value0))
1695 /* No, assign next available worker (RR) */
1696 next_worker_index = sm->first_worker_index;
1697 if (vec_len (sm->workers))
1699 next_worker_index +=
1700 sm->workers[sm->next_worker++ % _vec_len (sm->workers)];
1705 /* Static mapping without port */
1706 next_worker_index = value0.value;
1709 /* Add to translated packets worker lookup */
1710 kv0.value = next_worker_index;
1711 clib_bihash_add_del_8_8 (&sm->worker_by_out, &kv0, 1);
1714 next_worker_index = value0.value;
1716 return next_worker_index;
1719 static clib_error_t *
1720 snat_config (vlib_main_t * vm, unformat_input_t * input)
1722 snat_main_t * sm = &snat_main;
1723 u32 translation_buckets = 1024;
1724 u32 translation_memory_size = 128<<20;
1725 u32 user_buckets = 128;
1726 u32 user_memory_size = 64<<20;
1727 u32 max_translations_per_user = 100;
1728 u32 outside_vrf_id = 0;
1729 u32 inside_vrf_id = 0;
1730 u32 static_mapping_buckets = 1024;
1731 u32 static_mapping_memory_size = 64<<20;
1732 u8 static_mapping_only = 0;
1733 u8 static_mapping_connection_tracking = 0;
1734 vlib_thread_main_t *tm = vlib_get_thread_main ();
1736 sm->deterministic = 0;
1738 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1740 if (unformat (input, "translation hash buckets %d", &translation_buckets))
1742 else if (unformat (input, "translation hash memory %d",
1743 &translation_memory_size));
1744 else if (unformat (input, "user hash buckets %d", &user_buckets))
1746 else if (unformat (input, "user hash memory %d",
1749 else if (unformat (input, "max translations per user %d",
1750 &max_translations_per_user))
1752 else if (unformat (input, "outside VRF id %d",
1755 else if (unformat (input, "inside VRF id %d",
1758 else if (unformat (input, "static mapping only"))
1760 static_mapping_only = 1;
1761 if (unformat (input, "connection tracking"))
1762 static_mapping_connection_tracking = 1;
1764 else if (unformat (input, "deterministic"))
1765 sm->deterministic = 1;
1767 return clib_error_return (0, "unknown input '%U'",
1768 format_unformat_error, input);
1771 /* for show commands, etc. */
1772 sm->translation_buckets = translation_buckets;
1773 sm->translation_memory_size = translation_memory_size;
1774 sm->user_buckets = user_buckets;
1775 sm->user_memory_size = user_memory_size;
1776 sm->max_translations_per_user = max_translations_per_user;
1777 sm->outside_vrf_id = outside_vrf_id;
1778 sm->outside_fib_index = fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4,
1780 sm->inside_vrf_id = inside_vrf_id;
1781 sm->inside_fib_index = fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4,
1783 sm->static_mapping_only = static_mapping_only;
1784 sm->static_mapping_connection_tracking = static_mapping_connection_tracking;
1786 if (sm->deterministic)
1788 sm->in2out_node_index = snat_det_in2out_node.index;
1789 sm->in2out_output_node_index = ~0;
1790 sm->out2in_node_index = snat_det_out2in_node.index;
1791 sm->icmp_match_in2out_cb = icmp_match_in2out_det;
1792 sm->icmp_match_out2in_cb = icmp_match_out2in_det;
1796 sm->worker_in2out_cb = snat_get_worker_in2out_cb;
1797 sm->worker_out2in_cb = snat_get_worker_out2in_cb;
1798 sm->in2out_node_index = snat_in2out_node.index;
1799 sm->in2out_output_node_index = snat_in2out_output_node.index;
1800 sm->out2in_node_index = snat_out2in_node.index;
1801 if (!static_mapping_only ||
1802 (static_mapping_only && static_mapping_connection_tracking))
1804 sm->icmp_match_in2out_cb = icmp_match_in2out_slow;
1805 sm->icmp_match_out2in_cb = icmp_match_out2in_slow;
1807 clib_bihash_init_8_8 (&sm->worker_by_in, "worker-by-in", user_buckets,
1810 clib_bihash_init_8_8 (&sm->worker_by_out, "worker-by-out", user_buckets,
1813 vec_validate (sm->per_thread_data, tm->n_vlib_mains - 1);
1815 clib_bihash_init_8_8 (&sm->in2out, "in2out", translation_buckets,
1816 translation_memory_size);
1818 clib_bihash_init_8_8 (&sm->out2in, "out2in", translation_buckets,
1819 translation_memory_size);
1821 clib_bihash_init_8_8 (&sm->user_hash, "users", user_buckets,
1824 clib_bihash_init_16_8 (&sm->in2out_unk_proto, "in2out-unk-proto",
1825 translation_buckets, translation_memory_size);
1827 clib_bihash_init_16_8 (&sm->out2in_unk_proto, "out2in-unk-proto",
1828 translation_buckets, translation_memory_size);
1832 sm->icmp_match_in2out_cb = icmp_match_in2out_fast;
1833 sm->icmp_match_out2in_cb = icmp_match_out2in_fast;
1835 clib_bihash_init_8_8 (&sm->static_mapping_by_local,
1836 "static_mapping_by_local", static_mapping_buckets,
1837 static_mapping_memory_size);
1839 clib_bihash_init_8_8 (&sm->static_mapping_by_external,
1840 "static_mapping_by_external", static_mapping_buckets,
1841 static_mapping_memory_size);
1847 VLIB_CONFIG_FUNCTION (snat_config, "snat");
1849 u8 * format_snat_session_state (u8 * s, va_list * args)
1851 u32 i = va_arg (*args, u32);
1856 #define _(v, N, str) case SNAT_SESSION_##N: t = (u8 *) str; break;
1857 foreach_snat_session_state
1860 t = format (t, "unknown");
1862 s = format (s, "%s", t);
1866 u8 * format_snat_key (u8 * s, va_list * args)
1868 snat_session_key_t * key = va_arg (*args, snat_session_key_t *);
1869 char * protocol_string = "unknown";
1870 static char *protocol_strings[] = {
1876 if (key->protocol < ARRAY_LEN(protocol_strings))
1877 protocol_string = protocol_strings[key->protocol];
1879 s = format (s, "%U proto %s port %d fib %d",
1880 format_ip4_address, &key->addr, protocol_string,
1881 clib_net_to_host_u16 (key->port), key->fib_index);
1885 u8 * format_snat_session (u8 * s, va_list * args)
1887 snat_main_t * sm __attribute__((unused)) = va_arg (*args, snat_main_t *);
1888 snat_session_t * sess = va_arg (*args, snat_session_t *);
1890 if (snat_is_unk_proto_session (sess))
1892 s = format (s, " i2o %U proto %u fib %u\n",
1893 format_ip4_address, &sess->in2out.addr, sess->in2out.port,
1894 sess->in2out.fib_index);
1895 s = format (s, " o2i %U proto %u fib %u\n",
1896 format_ip4_address, &sess->out2in.addr, sess->out2in.port,
1897 sess->out2in.fib_index);
1901 s = format (s, " i2o %U\n", format_snat_key, &sess->in2out);
1902 s = format (s, " o2i %U\n", format_snat_key, &sess->out2in);
1904 s = format (s, " last heard %.2f\n", sess->last_heard);
1905 s = format (s, " total pkts %d, total bytes %lld\n",
1906 sess->total_pkts, sess->total_bytes);
1907 if (snat_is_session_static (sess))
1908 s = format (s, " static translation\n");
1910 s = format (s, " dynamic translation\n");
1915 u8 * format_snat_user (u8 * s, va_list * args)
1917 snat_main_per_thread_data_t * sm = va_arg (*args, snat_main_per_thread_data_t *);
1918 snat_user_t * u = va_arg (*args, snat_user_t *);
1919 int verbose = va_arg (*args, int);
1920 dlist_elt_t * head, * elt;
1921 u32 elt_index, head_index;
1923 snat_session_t * sess;
1925 s = format (s, "%U: %d dynamic translations, %d static translations\n",
1926 format_ip4_address, &u->addr, u->nsessions, u->nstaticsessions);
1931 if (u->nsessions || u->nstaticsessions)
1933 head_index = u->sessions_per_user_list_head_index;
1934 head = pool_elt_at_index (sm->list_pool, head_index);
1936 elt_index = head->next;
1937 elt = pool_elt_at_index (sm->list_pool, elt_index);
1938 session_index = elt->value;
1940 while (session_index != ~0)
1942 sess = pool_elt_at_index (sm->sessions, session_index);
1944 s = format (s, " %U\n", format_snat_session, sm, sess);
1946 elt_index = elt->next;
1947 elt = pool_elt_at_index (sm->list_pool, elt_index);
1948 session_index = elt->value;
1955 u8 * format_snat_static_mapping (u8 * s, va_list * args)
1957 snat_static_mapping_t *m = va_arg (*args, snat_static_mapping_t *);
1960 s = format (s, "local %U external %U vrf %d",
1961 format_ip4_address, &m->local_addr,
1962 format_ip4_address, &m->external_addr,
1965 s = format (s, "%U local %U:%d external %U:%d vrf %d",
1966 format_snat_protocol, m->proto,
1967 format_ip4_address, &m->local_addr, m->local_port,
1968 format_ip4_address, &m->external_addr, m->external_port,
1974 u8 * format_snat_static_map_to_resolve (u8 * s, va_list * args)
1976 snat_static_map_resolve_t *m = va_arg (*args, snat_static_map_resolve_t *);
1977 vnet_main_t *vnm = vnet_get_main();
1980 s = format (s, "local %U external %U vrf %d",
1981 format_ip4_address, &m->l_addr,
1982 format_vnet_sw_interface_name, vnm,
1983 vnet_get_sw_interface (vnm, m->sw_if_index),
1986 s = format (s, "%U local %U:%d external %U:%d vrf %d",
1987 format_snat_protocol, m->proto,
1988 format_ip4_address, &m->l_addr, m->l_port,
1989 format_vnet_sw_interface_name, vnm,
1990 vnet_get_sw_interface (vnm, m->sw_if_index), m->e_port,
1996 u8 * format_det_map_ses (u8 * s, va_list * args)
1998 snat_det_map_t * det_map = va_arg (*args, snat_det_map_t *);
1999 ip4_address_t in_addr, out_addr;
2000 u32 in_offset, out_offset;
2001 snat_det_session_t * ses = va_arg (*args, snat_det_session_t *);
2002 u32 * i = va_arg (*args, u32 *);
2004 u32 user_index = *i / SNAT_DET_SES_PER_USER;
2005 in_addr.as_u32 = clib_host_to_net_u32 (
2006 clib_net_to_host_u32(det_map->in_addr.as_u32) + user_index);
2007 in_offset = clib_net_to_host_u32(in_addr.as_u32) -
2008 clib_net_to_host_u32(det_map->in_addr.as_u32);
2009 out_offset = in_offset / det_map->sharing_ratio;
2010 out_addr.as_u32 = clib_host_to_net_u32(
2011 clib_net_to_host_u32(det_map->out_addr.as_u32) + out_offset);
2012 s = format (s, "in %U:%d out %U:%d external host %U:%d state: %U expire: %d\n",
2013 format_ip4_address, &in_addr,
2014 clib_net_to_host_u16 (ses->in_port),
2015 format_ip4_address, &out_addr,
2016 clib_net_to_host_u16 (ses->out.out_port),
2017 format_ip4_address, &ses->out.ext_host_addr,
2018 clib_net_to_host_u16 (ses->out.ext_host_port),
2019 format_snat_session_state, ses->state,
2025 static clib_error_t *
2026 show_snat_command_fn (vlib_main_t * vm,
2027 unformat_input_t * input,
2028 vlib_cli_command_t * cmd)
2031 snat_main_t * sm = &snat_main;
2033 snat_static_mapping_t *m;
2034 snat_interface_t *i;
2035 snat_address_t * ap;
2036 vnet_main_t *vnm = vnet_get_main();
2037 snat_main_per_thread_data_t *tsm;
2038 u32 users_num = 0, sessions_num = 0, *worker, *sw_if_index;
2040 snat_static_map_resolve_t *rp;
2041 snat_det_map_t * dm;
2042 snat_det_session_t * ses;
2044 if (unformat (input, "detail"))
2046 else if (unformat (input, "verbose"))
2049 if (sm->static_mapping_only)
2051 if (sm->static_mapping_connection_tracking)
2052 vlib_cli_output (vm, "SNAT mode: static mapping only connection "
2055 vlib_cli_output (vm, "SNAT mode: static mapping only");
2057 else if (sm->deterministic)
2059 vlib_cli_output (vm, "SNAT mode: deterministic mapping");
2063 vlib_cli_output (vm, "SNAT mode: dynamic translations enabled");
2068 pool_foreach (i, sm->interfaces,
2070 vlib_cli_output (vm, "%U %s", format_vnet_sw_interface_name, vnm,
2071 vnet_get_sw_interface (vnm, i->sw_if_index),
2072 i->is_inside ? "in" : "out");
2075 pool_foreach (i, sm->output_feature_interfaces,
2077 vlib_cli_output (vm, "%U output-feature %s",
2078 format_vnet_sw_interface_name, vnm,
2079 vnet_get_sw_interface (vnm, i->sw_if_index),
2080 i->is_inside ? "in" : "out");
2083 if (vec_len (sm->auto_add_sw_if_indices))
2085 vlib_cli_output (vm, "SNAT pool addresses interfaces:");
2086 vec_foreach (sw_if_index, sm->auto_add_sw_if_indices)
2088 vlib_cli_output (vm, "%U", format_vnet_sw_interface_name, vnm,
2089 vnet_get_sw_interface (vnm, *sw_if_index));
2093 vec_foreach (ap, sm->addresses)
2095 vlib_cli_output (vm, "%U", format_ip4_address, &ap->addr);
2096 if (ap->fib_index != ~0)
2097 vlib_cli_output (vm, " tenant VRF: %u",
2098 ip4_fib_get(ap->fib_index)->table_id);
2100 vlib_cli_output (vm, " tenant VRF independent");
2101 #define _(N, i, n, s) \
2102 vlib_cli_output (vm, " %d busy %s ports", ap->busy_##n##_ports, s);
2103 foreach_snat_protocol
2108 if (sm->num_workers > 1)
2110 vlib_cli_output (vm, "%d workers", vec_len (sm->workers));
2113 vec_foreach (worker, sm->workers)
2115 vlib_worker_thread_t *w =
2116 vlib_worker_threads + *worker + sm->first_worker_index;
2117 vlib_cli_output (vm, " %s", w->name);
2122 if (sm->deterministic)
2124 vlib_cli_output (vm, "udp timeout: %dsec", sm->udp_timeout);
2125 vlib_cli_output (vm, "tcp-established timeout: %dsec",
2126 sm->tcp_established_timeout);
2127 vlib_cli_output (vm, "tcp-transitory timeout: %dsec",
2128 sm->tcp_transitory_timeout);
2129 vlib_cli_output (vm, "icmp timeout: %dsec", sm->icmp_timeout);
2130 vlib_cli_output (vm, "%d deterministic mappings",
2131 pool_elts (sm->det_maps));
2134 pool_foreach (dm, sm->det_maps,
2136 vlib_cli_output (vm, "in %U/%d out %U/%d\n",
2137 format_ip4_address, &dm->in_addr, dm->in_plen,
2138 format_ip4_address, &dm->out_addr, dm->out_plen);
2139 vlib_cli_output (vm, " outside address sharing ratio: %d\n",
2141 vlib_cli_output (vm, " number of ports per inside host: %d\n",
2142 dm->ports_per_host);
2143 vlib_cli_output (vm, " sessions number: %d\n", dm->ses_num);
2146 vec_foreach_index (j, dm->sessions)
2148 ses = vec_elt_at_index (dm->sessions, j);
2150 vlib_cli_output (vm, " %U", format_det_map_ses, dm, ses,
2159 if (sm->static_mapping_only && !(sm->static_mapping_connection_tracking))
2161 vlib_cli_output (vm, "%d static mappings",
2162 pool_elts (sm->static_mappings));
2166 pool_foreach (m, sm->static_mappings,
2168 vlib_cli_output (vm, "%U", format_snat_static_mapping, m);
2174 vec_foreach (tsm, sm->per_thread_data)
2176 users_num += pool_elts (tsm->users);
2177 sessions_num += pool_elts (tsm->sessions);
2180 vlib_cli_output (vm, "%d users, %d outside addresses, %d active sessions,"
2181 " %d static mappings",
2183 vec_len (sm->addresses),
2185 pool_elts (sm->static_mappings));
2189 vlib_cli_output (vm, "%U", format_bihash_8_8, &sm->in2out,
2191 vlib_cli_output (vm, "%U", format_bihash_8_8, &sm->out2in,
2193 vlib_cli_output (vm, "%U", format_bihash_8_8, &sm->worker_by_in,
2195 vlib_cli_output (vm, "%U", format_bihash_8_8, &sm->worker_by_out,
2197 vec_foreach_index (j, sm->per_thread_data)
2199 tsm = vec_elt_at_index (sm->per_thread_data, j);
2201 if (pool_elts (tsm->users) == 0)
2204 vlib_worker_thread_t *w = vlib_worker_threads + j;
2205 vlib_cli_output (vm, "Thread %d (%s at lcore %u):", j, w->name,
2207 vlib_cli_output (vm, " %d list pool elements",
2208 pool_elts (tsm->list_pool));
2210 pool_foreach (u, tsm->users,
2212 vlib_cli_output (vm, " %U", format_snat_user, tsm, u,
2217 if (pool_elts (sm->static_mappings))
2219 vlib_cli_output (vm, "static mappings:");
2220 pool_foreach (m, sm->static_mappings,
2222 vlib_cli_output (vm, "%U", format_snat_static_mapping, m);
2224 for (j = 0; j < vec_len (sm->to_resolve); j++)
2226 rp = sm->to_resolve + j;
2227 vlib_cli_output (vm, "%U",
2228 format_snat_static_map_to_resolve, rp);
2237 VLIB_CLI_COMMAND (show_snat_command, static) = {
2238 .path = "show snat",
2239 .short_help = "show snat",
2240 .function = show_snat_command_fn,
2245 snat_ip4_add_del_interface_address_cb (ip4_main_t * im,
2248 ip4_address_t * address,
2250 u32 if_address_index,
2253 snat_main_t *sm = &snat_main;
2254 snat_static_map_resolve_t *rp;
2255 u32 *indices_to_delete = 0;
2259 for (i = 0; i < vec_len(sm->auto_add_sw_if_indices); i++)
2261 if (sw_if_index == sm->auto_add_sw_if_indices[i])
2265 /* Don't trip over lease renewal, static config */
2266 for (j = 0; j < vec_len(sm->addresses); j++)
2267 if (sm->addresses[j].addr.as_u32 == address->as_u32)
2270 snat_add_address (sm, address, ~0);
2271 /* Scan static map resolution vector */
2272 for (j = 0; j < vec_len (sm->to_resolve); j++)
2274 rp = sm->to_resolve + j;
2275 /* On this interface? */
2276 if (rp->sw_if_index == sw_if_index)
2278 /* Add the static mapping */
2279 rv = snat_add_static_mapping (rp->l_addr,
2285 ~0 /* sw_if_index */,
2289 clib_warning ("snat_add_static_mapping returned %d",
2291 vec_add1 (indices_to_delete, j);
2294 /* If we resolved any of the outstanding static mappings */
2295 if (vec_len(indices_to_delete))
2298 for (j = vec_len(indices_to_delete)-1; j >= 0; j--)
2299 vec_delete(sm->to_resolve, 1, j);
2300 vec_free(indices_to_delete);
2306 (void) snat_del_address(sm, address[0], 1);
2314 int snat_add_interface_address (snat_main_t *sm, u32 sw_if_index, int is_del)
2316 ip4_main_t * ip4_main = sm->ip4_main;
2317 ip4_address_t * first_int_addr;
2318 snat_static_map_resolve_t *rp;
2319 u32 *indices_to_delete = 0;
2322 first_int_addr = ip4_interface_first_address (ip4_main, sw_if_index,
2323 0 /* just want the address*/);
2325 for (i = 0; i < vec_len(sm->auto_add_sw_if_indices); i++)
2327 if (sm->auto_add_sw_if_indices[i] == sw_if_index)
2331 /* if have address remove it */
2333 (void) snat_del_address (sm, first_int_addr[0], 1);
2336 for (j = 0; j < vec_len (sm->to_resolve); j++)
2338 rp = sm->to_resolve + j;
2339 if (rp->sw_if_index == sw_if_index)
2340 vec_add1 (indices_to_delete, j);
2342 if (vec_len(indices_to_delete))
2344 for (j = vec_len(indices_to_delete)-1; j >= 0; j--)
2345 vec_del1(sm->to_resolve, j);
2346 vec_free(indices_to_delete);
2349 vec_del1(sm->auto_add_sw_if_indices, i);
2352 return VNET_API_ERROR_VALUE_EXIST;
2359 return VNET_API_ERROR_NO_SUCH_ENTRY;
2361 /* add to the auto-address list */
2362 vec_add1(sm->auto_add_sw_if_indices, sw_if_index);
2364 /* If the address is already bound - or static - add it now */
2366 snat_add_address (sm, first_int_addr, ~0);
2371 static clib_error_t *
2372 snat_add_interface_address_command_fn (vlib_main_t * vm,
2373 unformat_input_t * input,
2374 vlib_cli_command_t * cmd)
2376 snat_main_t *sm = &snat_main;
2377 unformat_input_t _line_input, *line_input = &_line_input;
2381 clib_error_t *error = 0;
2383 /* Get a line of input. */
2384 if (!unformat_user (input, unformat_line_input, line_input))
2387 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2389 if (unformat (line_input, "%U", unformat_vnet_sw_interface,
2390 sm->vnet_main, &sw_if_index))
2392 else if (unformat (line_input, "del"))
2396 error = clib_error_return (0, "unknown input '%U'",
2397 format_unformat_error, line_input);
2402 rv = snat_add_interface_address (sm, sw_if_index, is_del);
2410 error = clib_error_return (0, "snat_add_interface_address returned %d",
2416 unformat_free (line_input);
2421 VLIB_CLI_COMMAND (snat_add_interface_address_command, static) = {
2422 .path = "snat add interface address",
2423 .short_help = "snat add interface address <interface> [del]",
2424 .function = snat_add_interface_address_command_fn,
2427 static clib_error_t *
2428 snat_det_map_command_fn (vlib_main_t * vm,
2429 unformat_input_t * input,
2430 vlib_cli_command_t * cmd)
2432 snat_main_t *sm = &snat_main;
2433 unformat_input_t _line_input, *line_input = &_line_input;
2434 ip4_address_t in_addr, out_addr;
2435 u32 in_plen, out_plen;
2437 clib_error_t *error = 0;
2439 /* Get a line of input. */
2440 if (!unformat_user (input, unformat_line_input, line_input))
2443 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2445 if (unformat (line_input, "in %U/%u", unformat_ip4_address, &in_addr, &in_plen))
2447 else if (unformat (line_input, "out %U/%u", unformat_ip4_address, &out_addr, &out_plen))
2449 else if (unformat (line_input, "del"))
2453 error = clib_error_return (0, "unknown input '%U'",
2454 format_unformat_error, line_input);
2459 unformat_free (line_input);
2461 rv = snat_det_add_map(sm, &in_addr, (u8) in_plen, &out_addr, (u8)out_plen,
2466 error = clib_error_return (0, "snat_det_add_map return %d", rv);
2471 unformat_free (line_input);
2478 * @cliexstart{snat deterministic add}
2479 * Create bijective mapping of inside address to outside address and port range
2480 * pairs, with the purpose of enabling deterministic NAT to reduce logging in
2482 * To create deterministic mapping between inside network 10.0.0.0/18 and
2483 * outside network 1.1.1.0/30 use:
2484 * # vpp# snat deterministic add in 10.0.0.0/18 out 1.1.1.0/30
2487 VLIB_CLI_COMMAND (snat_det_map_command, static) = {
2488 .path = "snat deterministic add",
2489 .short_help = "snat deterministic add in <addr>/<plen> out <addr>/<plen> [del]",
2490 .function = snat_det_map_command_fn,
2493 static clib_error_t *
2494 snat_det_forward_command_fn (vlib_main_t * vm,
2495 unformat_input_t * input,
2496 vlib_cli_command_t * cmd)
2498 snat_main_t *sm = &snat_main;
2499 unformat_input_t _line_input, *line_input = &_line_input;
2500 ip4_address_t in_addr, out_addr;
2502 snat_det_map_t * dm;
2503 clib_error_t *error = 0;
2505 /* Get a line of input. */
2506 if (!unformat_user (input, unformat_line_input, line_input))
2509 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2511 if (unformat (line_input, "%U", unformat_ip4_address, &in_addr))
2515 error = clib_error_return (0, "unknown input '%U'",
2516 format_unformat_error, line_input);
2521 unformat_free (line_input);
2523 dm = snat_det_map_by_user(sm, &in_addr);
2525 vlib_cli_output (vm, "no match");
2528 snat_det_forward (dm, &in_addr, &out_addr, &lo_port);
2529 vlib_cli_output (vm, "%U:<%d-%d>", format_ip4_address, &out_addr,
2530 lo_port, lo_port + dm->ports_per_host - 1);
2534 unformat_free (line_input);
2541 * @cliexstart{snat deterministic forward}
2542 * Return outside address and port range from inside address for deterministic
2544 * To obtain outside address and port of inside host use:
2545 * vpp# snat deterministic forward 10.0.0.2
2546 * 1.1.1.0:<1054-1068>
2549 VLIB_CLI_COMMAND (snat_det_forward_command, static) = {
2550 .path = "snat deterministic forward",
2551 .short_help = "snat deterministic forward <addr>",
2552 .function = snat_det_forward_command_fn,
2555 static clib_error_t *
2556 snat_det_reverse_command_fn (vlib_main_t * vm,
2557 unformat_input_t * input,
2558 vlib_cli_command_t * cmd)
2560 snat_main_t *sm = &snat_main;
2561 unformat_input_t _line_input, *line_input = &_line_input;
2562 ip4_address_t in_addr, out_addr;
2564 snat_det_map_t * dm;
2565 clib_error_t *error = 0;
2567 /* Get a line of input. */
2568 if (!unformat_user (input, unformat_line_input, line_input))
2571 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2573 if (unformat (line_input, "%U:%d", unformat_ip4_address, &out_addr, &out_port))
2577 error = clib_error_return (0, "unknown input '%U'",
2578 format_unformat_error, line_input);
2582 unformat_free (line_input);
2584 if (out_port < 1024 || out_port > 65535)
2586 error = clib_error_return (0, "wrong port, must be <1024-65535>");
2590 dm = snat_det_map_by_out(sm, &out_addr);
2592 vlib_cli_output (vm, "no match");
2595 snat_det_reverse (dm, &out_addr, (u16) out_port, &in_addr);
2596 vlib_cli_output (vm, "%U", format_ip4_address, &in_addr);
2600 unformat_free (line_input);
2607 * @cliexstart{snat deterministic reverse}
2608 * Return inside address from outside address and port for deterministic NAT.
2609 * To obtain inside host address from outside address and port use:
2610 * #vpp snat deterministic reverse 1.1.1.1:1276
2614 VLIB_CLI_COMMAND (snat_det_reverse_command, static) = {
2615 .path = "snat deterministic reverse",
2616 .short_help = "snat deterministic reverse <addr>:<port>",
2617 .function = snat_det_reverse_command_fn,
2620 static clib_error_t *
2621 set_timeout_command_fn (vlib_main_t * vm,
2622 unformat_input_t * input,
2623 vlib_cli_command_t * cmd)
2625 snat_main_t *sm = &snat_main;
2626 unformat_input_t _line_input, *line_input = &_line_input;
2627 clib_error_t *error = 0;
2629 /* Get a line of input. */
2630 if (!unformat_user (input, unformat_line_input, line_input))
2633 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2635 if (unformat (line_input, "udp %u", &sm->udp_timeout))
2637 else if (unformat (line_input, "tcp-established %u",
2638 &sm->tcp_established_timeout))
2640 else if (unformat (line_input, "tcp-transitory %u",
2641 &sm->tcp_transitory_timeout))
2643 else if (unformat (line_input, "icmp %u", &sm->icmp_timeout))
2645 else if (unformat (line_input, "reset"))
2647 sm->udp_timeout = SNAT_UDP_TIMEOUT;
2648 sm->tcp_established_timeout = SNAT_TCP_ESTABLISHED_TIMEOUT;
2649 sm->tcp_transitory_timeout = SNAT_TCP_TRANSITORY_TIMEOUT;
2650 sm->icmp_timeout = SNAT_ICMP_TIMEOUT;
2654 error = clib_error_return (0, "unknown input '%U'",
2655 format_unformat_error, line_input);
2660 unformat_free (line_input);
2663 unformat_free (line_input);
2670 * @cliexstart{set snat deterministic timeout}
2671 * Set values of timeouts for deterministic NAT (in seconds), use:
2672 * vpp# set snat deterministic timeout udp 120 tcp-established 7500
2673 * tcp-transitory 250 icmp 90
2674 * To reset default values use:
2675 * vpp# set snat deterministic timeout reset
2678 VLIB_CLI_COMMAND (set_timeout_command, static) = {
2679 .path = "set snat deterministic timeout",
2680 .function = set_timeout_command_fn,
2682 "set snat deterministic timeout [udp <sec> | tcp-established <sec> "
2683 "tcp-transitory <sec> | icmp <sec> | reset]",
2686 static clib_error_t *
2687 snat_det_close_session_out_fn (vlib_main_t *vm,
2688 unformat_input_t * input,
2689 vlib_cli_command_t * cmd)
2691 snat_main_t *sm = &snat_main;
2692 unformat_input_t _line_input, *line_input = &_line_input;
2693 ip4_address_t out_addr, ext_addr, in_addr;
2694 u16 out_port, ext_port;
2695 snat_det_map_t * dm;
2696 snat_det_session_t * ses;
2697 snat_det_out_key_t key;
2698 clib_error_t *error = 0;
2700 /* Get a line of input. */
2701 if (!unformat_user (input, unformat_line_input, line_input))
2704 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2706 if (unformat (line_input, "%U:%d %U:%d",
2707 unformat_ip4_address, &out_addr, &out_port,
2708 unformat_ip4_address, &ext_addr, &ext_port))
2712 error = clib_error_return (0, "unknown input '%U'",
2713 format_unformat_error, line_input);
2718 unformat_free (line_input);
2720 dm = snat_det_map_by_out(sm, &out_addr);
2722 vlib_cli_output (vm, "no match");
2725 snat_det_reverse(dm, &ext_addr, out_port, &in_addr);
2726 key.ext_host_addr = out_addr;
2727 key.ext_host_port = ntohs(ext_port);
2728 key.out_port = ntohs(out_port);
2729 ses = snat_det_get_ses_by_out(dm, &out_addr, key.as_u64);
2731 vlib_cli_output (vm, "no match");
2733 snat_det_ses_close(dm, ses);
2737 unformat_free (line_input);
2744 * @cliexstart{snat deterministic close session out}
2745 * Close session using outside ip address and port
2746 * and external ip address and port, use:
2747 * vpp# snat deterministic close session out 1.1.1.1:1276 2.2.2.2:2387
2750 VLIB_CLI_COMMAND (snat_det_close_sesion_out_command, static) = {
2751 .path = "snat deterministic close session out",
2752 .short_help = "snat deterministic close session out "
2753 "<out_addr>:<out_port> <ext_addr>:<ext_port>",
2754 .function = snat_det_close_session_out_fn,
2757 static clib_error_t *
2758 snat_det_close_session_in_fn (vlib_main_t *vm,
2759 unformat_input_t * input,
2760 vlib_cli_command_t * cmd)
2762 snat_main_t *sm = &snat_main;
2763 unformat_input_t _line_input, *line_input = &_line_input;
2764 ip4_address_t in_addr, ext_addr;
2765 u16 in_port, ext_port;
2766 snat_det_map_t * dm;
2767 snat_det_session_t * ses;
2768 snat_det_out_key_t key;
2769 clib_error_t *error = 0;
2771 /* Get a line of input. */
2772 if (!unformat_user (input, unformat_line_input, line_input))
2775 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2777 if (unformat (line_input, "%U:%d %U:%d",
2778 unformat_ip4_address, &in_addr, &in_port,
2779 unformat_ip4_address, &ext_addr, &ext_port))
2783 error = clib_error_return (0, "unknown input '%U'",
2784 format_unformat_error, line_input);
2789 unformat_free (line_input);
2791 dm = snat_det_map_by_user (sm, &in_addr);
2793 vlib_cli_output (vm, "no match");
2796 key.ext_host_addr = ext_addr;
2797 key.ext_host_port = ntohs (ext_port);
2798 ses = snat_det_find_ses_by_in (dm, &in_addr, ntohs(in_port), key);
2800 vlib_cli_output (vm, "no match");
2802 snat_det_ses_close(dm, ses);
2806 unformat_free(line_input);
2813 * @cliexstart{snat deterministic close_session_in}
2814 * Close session using inside ip address and port
2815 * and external ip address and port, use:
2816 * vpp# snat deterministic close session in 3.3.3.3:3487 2.2.2.2:2387
2819 VLIB_CLI_COMMAND (snat_det_close_session_in_command, static) = {
2820 .path = "snat deterministic close session in",
2821 .short_help = "snat deterministic close session in "
2822 "<in_addr>:<in_port> <ext_addr>:<ext_port>",
2823 .function = snat_det_close_session_in_fn,