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);
903 sm->per_thread_data[i].snat_thread_index = j;
907 sm->port_per_thread = (0xffff - 1024) / _vec_len (sm->workers);
908 sm->num_snat_thread = _vec_len (sm->workers);
915 snat_ip4_add_del_interface_address_cb (ip4_main_t * im,
918 ip4_address_t * address,
920 u32 if_address_index,
923 static clib_error_t * snat_init (vlib_main_t * vm)
925 snat_main_t * sm = &snat_main;
926 clib_error_t * error = 0;
927 ip4_main_t * im = &ip4_main;
928 ip_lookup_main_t * lm = &im->lookup_main;
930 vlib_thread_registration_t *tr;
931 vlib_thread_main_t *tm = vlib_get_thread_main ();
934 ip4_add_del_interface_address_callback_t cb4;
937 sm->vnet_main = vnet_get_main();
939 sm->ip4_lookup_main = lm;
940 sm->api_main = &api_main;
941 sm->first_worker_index = 0;
944 sm->num_snat_thread = 1;
946 sm->port_per_thread = 0xffff - 1024;
947 sm->fq_in2out_index = ~0;
948 sm->fq_out2in_index = ~0;
949 sm->udp_timeout = SNAT_UDP_TIMEOUT;
950 sm->tcp_established_timeout = SNAT_TCP_ESTABLISHED_TIMEOUT;
951 sm->tcp_transitory_timeout = SNAT_TCP_TRANSITORY_TIMEOUT;
952 sm->icmp_timeout = SNAT_ICMP_TIMEOUT;
954 p = hash_get_mem (tm->thread_registrations_by_name, "workers");
957 tr = (vlib_thread_registration_t *) p[0];
960 sm->num_workers = tr->count;
961 sm->first_worker_index = tr->first_index;
965 vec_validate (sm->per_thread_data, tm->n_vlib_mains - 1);
967 /* Use all available workers by default */
968 if (sm->num_workers > 1)
970 for (i=0; i < sm->num_workers; i++)
971 bitmap = clib_bitmap_set (bitmap, i, 1);
972 snat_set_workers(bitmap);
973 clib_bitmap_free (bitmap);
977 sm->per_thread_data[0].snat_thread_index = 0;
980 error = snat_api_init(vm, sm);
984 /* Set up the interface address add/del callback */
985 cb4.function = snat_ip4_add_del_interface_address_cb;
986 cb4.function_opaque = 0;
988 vec_add1 (im->add_del_interface_address_callbacks, cb4);
990 /* Init IPFIX logging */
991 snat_ipfix_logging_init(vm);
993 error = nat64_init(vm);
998 VLIB_INIT_FUNCTION (snat_init);
1000 void snat_free_outside_address_and_port (snat_main_t * sm,
1001 snat_session_key_t * k,
1005 u16 port_host_byte_order = clib_net_to_host_u16 (k->port);
1007 ASSERT (address_index < vec_len (sm->addresses));
1009 a = sm->addresses + address_index;
1011 switch (k->protocol)
1013 #define _(N, i, n, s) \
1014 case SNAT_PROTOCOL_##N: \
1015 ASSERT (clib_bitmap_get_no_check (a->busy_##n##_port_bitmap, \
1016 port_host_byte_order) == 1); \
1017 clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, \
1018 port_host_byte_order, 0); \
1019 a->busy_##n##_ports--; \
1021 foreach_snat_protocol
1024 clib_warning("unknown_protocol");
1030 * @brief Match SNAT static mapping.
1032 * @param sm SNAT main.
1033 * @param match Address and port to match.
1034 * @param mapping External or local address and port of the matched mapping.
1035 * @param by_external If 0 match by local address otherwise match by external
1037 * @param is_addr_only If matched mapping is address only
1039 * @returns 0 if match found otherwise 1.
1041 int snat_static_mapping_match (snat_main_t * sm,
1042 snat_session_key_t match,
1043 snat_session_key_t * mapping,
1047 clib_bihash_kv_8_8_t kv, value;
1048 snat_static_mapping_t *m;
1049 snat_session_key_t m_key;
1050 clib_bihash_8_8_t *mapping_hash = &sm->static_mapping_by_local;
1053 mapping_hash = &sm->static_mapping_by_external;
1055 m_key.addr = match.addr;
1056 m_key.port = clib_net_to_host_u16 (match.port);
1057 m_key.protocol = match.protocol;
1058 m_key.fib_index = match.fib_index;
1060 kv.key = m_key.as_u64;
1062 if (clib_bihash_search_8_8 (mapping_hash, &kv, &value))
1064 /* Try address only mapping */
1067 kv.key = m_key.as_u64;
1068 if (clib_bihash_search_8_8 (mapping_hash, &kv, &value))
1072 m = pool_elt_at_index (sm->static_mappings, value.value);
1076 mapping->addr = m->local_addr;
1077 /* Address only mapping doesn't change port */
1078 mapping->port = m->addr_only ? match.port
1079 : clib_host_to_net_u16 (m->local_port);
1080 mapping->fib_index = m->fib_index;
1084 mapping->addr = m->external_addr;
1085 /* Address only mapping doesn't change port */
1086 mapping->port = m->addr_only ? match.port
1087 : clib_host_to_net_u16 (m->external_port);
1088 mapping->fib_index = sm->outside_fib_index;
1091 if (PREDICT_FALSE(is_addr_only != 0))
1092 *is_addr_only = m->addr_only;
1097 static_always_inline u16
1098 snat_random_port (snat_main_t * sm, u16 min, u16 max)
1100 return min + random_u32 (&sm->random_seed) /
1101 (random_u32_max() / (max - min + 1) + 1);
1104 int snat_alloc_outside_address_and_port (snat_main_t * sm,
1107 snat_session_key_t * k,
1108 u32 * address_indexp)
1114 for (i = 0; i < vec_len (sm->addresses); i++)
1116 a = sm->addresses + i;
1117 if (sm->vrf_mode && a->fib_index != ~0 && a->fib_index != fib_index)
1119 switch (k->protocol)
1121 #define _(N, j, n, s) \
1122 case SNAT_PROTOCOL_##N: \
1123 if (a->busy_##n##_ports < (sm->port_per_thread * sm->num_snat_thread)) \
1127 portnum = (sm->port_per_thread * \
1128 sm->per_thread_data[thread_index].snat_thread_index) + \
1129 snat_random_port(sm, 0, sm->port_per_thread) + 1024; \
1130 if (clib_bitmap_get_no_check (a->busy_##n##_port_bitmap, portnum)) \
1132 clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, portnum, 1); \
1133 a->busy_##n##_ports++; \
1134 k->addr = a->addr; \
1135 k->port = clib_host_to_net_u16(portnum); \
1136 *address_indexp = i; \
1141 foreach_snat_protocol
1144 clib_warning("unknown protocol");
1149 /* Totally out of translations to use... */
1150 snat_ipfix_logging_addresses_exhausted(0);
1155 static clib_error_t *
1156 add_address_command_fn (vlib_main_t * vm,
1157 unformat_input_t * input,
1158 vlib_cli_command_t * cmd)
1160 unformat_input_t _line_input, *line_input = &_line_input;
1161 snat_main_t * sm = &snat_main;
1162 ip4_address_t start_addr, end_addr, this_addr;
1163 u32 start_host_order, end_host_order;
1168 clib_error_t *error = 0;
1170 /* Get a line of input. */
1171 if (!unformat_user (input, unformat_line_input, line_input))
1174 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1176 if (unformat (line_input, "%U - %U",
1177 unformat_ip4_address, &start_addr,
1178 unformat_ip4_address, &end_addr))
1180 else if (unformat (line_input, "tenant-vrf %u", &vrf_id))
1182 else if (unformat (line_input, "%U", unformat_ip4_address, &start_addr))
1183 end_addr = start_addr;
1184 else if (unformat (line_input, "del"))
1188 error = clib_error_return (0, "unknown input '%U'",
1189 format_unformat_error, line_input);
1194 if (sm->static_mapping_only)
1196 error = clib_error_return (0, "static mapping only mode");
1200 start_host_order = clib_host_to_net_u32 (start_addr.as_u32);
1201 end_host_order = clib_host_to_net_u32 (end_addr.as_u32);
1203 if (end_host_order < start_host_order)
1205 error = clib_error_return (0, "end address less than start address");
1209 count = (end_host_order - start_host_order) + 1;
1212 clib_warning ("%U - %U, %d addresses...",
1213 format_ip4_address, &start_addr,
1214 format_ip4_address, &end_addr,
1217 this_addr = start_addr;
1219 for (i = 0; i < count; i++)
1222 snat_add_address (sm, &this_addr, vrf_id);
1224 rv = snat_del_address (sm, this_addr, 0);
1228 case VNET_API_ERROR_NO_SUCH_ENTRY:
1229 error = clib_error_return (0, "S-NAT address not exist.");
1231 case VNET_API_ERROR_UNSPECIFIED:
1232 error = clib_error_return (0, "S-NAT address used in static mapping.");
1238 increment_v4_address (&this_addr);
1242 unformat_free (line_input);
1247 VLIB_CLI_COMMAND (add_address_command, static) = {
1248 .path = "snat add address",
1249 .short_help = "snat add addresses <ip4-range-start> [- <ip4-range-end>] "
1250 "[tenant-vrf <vrf-id>] [del]",
1251 .function = add_address_command_fn,
1254 static clib_error_t *
1255 snat_feature_command_fn (vlib_main_t * vm,
1256 unformat_input_t * input,
1257 vlib_cli_command_t * cmd)
1259 unformat_input_t _line_input, *line_input = &_line_input;
1260 vnet_main_t * vnm = vnet_get_main();
1261 clib_error_t * error = 0;
1263 u32 * inside_sw_if_indices = 0;
1264 u32 * outside_sw_if_indices = 0;
1265 u8 is_output_feature = 0;
1271 /* Get a line of input. */
1272 if (!unformat_user (input, unformat_line_input, line_input))
1275 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1277 if (unformat (line_input, "in %U", unformat_vnet_sw_interface,
1279 vec_add1 (inside_sw_if_indices, sw_if_index);
1280 else if (unformat (line_input, "out %U", unformat_vnet_sw_interface,
1282 vec_add1 (outside_sw_if_indices, sw_if_index);
1283 else if (unformat (line_input, "output-feature"))
1284 is_output_feature = 1;
1285 else if (unformat (line_input, "del"))
1289 error = clib_error_return (0, "unknown input '%U'",
1290 format_unformat_error, line_input);
1295 if (vec_len (inside_sw_if_indices))
1297 for (i = 0; i < vec_len(inside_sw_if_indices); i++)
1299 sw_if_index = inside_sw_if_indices[i];
1300 if (is_output_feature)
1302 if (snat_interface_add_del_output_feature (sw_if_index, 1, is_del))
1304 error = clib_error_return (0, "%s %U failed",
1305 is_del ? "del" : "add",
1306 format_vnet_sw_interface_name, vnm,
1307 vnet_get_sw_interface (vnm,
1314 if (snat_interface_add_del (sw_if_index, 1, 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,
1327 if (vec_len (outside_sw_if_indices))
1329 for (i = 0; i < vec_len(outside_sw_if_indices); i++)
1331 sw_if_index = outside_sw_if_indices[i];
1332 if (is_output_feature)
1334 if (snat_interface_add_del_output_feature (sw_if_index, 0, is_del))
1336 error = clib_error_return (0, "%s %U failed",
1337 is_del ? "del" : "add",
1338 format_vnet_sw_interface_name, vnm,
1339 vnet_get_sw_interface (vnm,
1346 if (snat_interface_add_del (sw_if_index, 0, is_del))
1348 error = clib_error_return (0, "%s %U failed",
1349 is_del ? "del" : "add",
1350 format_vnet_sw_interface_name, vnm,
1351 vnet_get_sw_interface (vnm,
1360 unformat_free (line_input);
1361 vec_free (inside_sw_if_indices);
1362 vec_free (outside_sw_if_indices);
1367 VLIB_CLI_COMMAND (set_interface_snat_command, static) = {
1368 .path = "set interface snat",
1369 .function = snat_feature_command_fn,
1370 .short_help = "set interface snat in <intfc> out <intfc> [output-feature] "
1375 unformat_snat_protocol (unformat_input_t * input, va_list * args)
1377 u32 *r = va_arg (*args, u32 *);
1380 #define _(N, i, n, s) else if (unformat (input, s)) *r = SNAT_PROTOCOL_##N;
1381 foreach_snat_protocol
1389 format_snat_protocol (u8 * s, va_list * args)
1391 u32 i = va_arg (*args, u32);
1396 #define _(N, j, n, str) case SNAT_PROTOCOL_##N: t = (u8 *) str; break;
1397 foreach_snat_protocol
1400 s = format (s, "unknown");
1403 s = format (s, "%s", t);
1407 static clib_error_t *
1408 add_static_mapping_command_fn (vlib_main_t * vm,
1409 unformat_input_t * input,
1410 vlib_cli_command_t * cmd)
1412 unformat_input_t _line_input, *line_input = &_line_input;
1413 clib_error_t * error = 0;
1414 ip4_address_t l_addr, e_addr;
1415 u32 l_port = 0, e_port = 0, vrf_id = ~0;
1418 u32 sw_if_index = ~0;
1419 vnet_main_t * vnm = vnet_get_main();
1421 snat_protocol_t proto;
1424 /* Get a line of input. */
1425 if (!unformat_user (input, unformat_line_input, line_input))
1428 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1430 if (unformat (line_input, "local %U %u", unformat_ip4_address, &l_addr,
1433 else if (unformat (line_input, "local %U", unformat_ip4_address, &l_addr))
1435 else if (unformat (line_input, "external %U %u", unformat_ip4_address,
1438 else if (unformat (line_input, "external %U", unformat_ip4_address,
1441 else if (unformat (line_input, "external %U %u",
1442 unformat_vnet_sw_interface, vnm, &sw_if_index,
1446 else if (unformat (line_input, "external %U",
1447 unformat_vnet_sw_interface, vnm, &sw_if_index))
1449 else if (unformat (line_input, "vrf %u", &vrf_id))
1451 else if (unformat (line_input, "%U", unformat_snat_protocol, &proto))
1453 else if (unformat (line_input, "del"))
1457 error = clib_error_return (0, "unknown input: '%U'",
1458 format_unformat_error, line_input);
1463 if (!addr_only && !proto_set)
1465 error = clib_error_return (0, "missing protocol");
1469 rv = snat_add_static_mapping(l_addr, e_addr, (u16) l_port, (u16) e_port,
1470 vrf_id, addr_only, sw_if_index, proto, is_add);
1474 case VNET_API_ERROR_INVALID_VALUE:
1475 error = clib_error_return (0, "External port already in use.");
1477 case VNET_API_ERROR_NO_SUCH_ENTRY:
1479 error = clib_error_return (0, "External addres must be allocated.");
1481 error = clib_error_return (0, "Mapping not exist.");
1483 case VNET_API_ERROR_NO_SUCH_FIB:
1484 error = clib_error_return (0, "No such VRF id.");
1486 case VNET_API_ERROR_VALUE_EXIST:
1487 error = clib_error_return (0, "Mapping already exist.");
1494 unformat_free (line_input);
1501 * @cliexstart{snat add static mapping}
1502 * Static mapping allows hosts on the external network to initiate connection
1503 * to to the local network host.
1504 * To create static mapping between local host address 10.0.0.3 port 6303 and
1505 * external address 4.4.4.4 port 3606 for TCP protocol use:
1506 * vpp# snat add static mapping local tcp 10.0.0.3 6303 external 4.4.4.4 3606
1507 * If not runnig "static mapping only" S-NAT plugin mode use before:
1508 * vpp# snat add address 4.4.4.4
1509 * To create static mapping between local and external address use:
1510 * vpp# snat add static mapping local 10.0.0.3 external 4.4.4.4
1513 VLIB_CLI_COMMAND (add_static_mapping_command, static) = {
1514 .path = "snat add static mapping",
1515 .function = add_static_mapping_command_fn,
1517 "snat add static mapping local tcp|udp|icmp <addr> [<port>] external <addr> [<port>] [vrf <table-id>] [del]",
1520 static clib_error_t *
1521 set_workers_command_fn (vlib_main_t * vm,
1522 unformat_input_t * input,
1523 vlib_cli_command_t * cmd)
1525 unformat_input_t _line_input, *line_input = &_line_input;
1528 clib_error_t *error = 0;
1530 /* Get a line of input. */
1531 if (!unformat_user (input, unformat_line_input, line_input))
1534 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1536 if (unformat (line_input, "%U", unformat_bitmap_list, &bitmap))
1540 error = clib_error_return (0, "unknown input '%U'",
1541 format_unformat_error, line_input);
1548 error = clib_error_return (0, "List of workers must be specified.");
1552 rv = snat_set_workers(bitmap);
1554 clib_bitmap_free (bitmap);
1558 case VNET_API_ERROR_INVALID_WORKER:
1559 error = clib_error_return (0, "Invalid worker(s).");
1561 case VNET_API_ERROR_FEATURE_DISABLED:
1562 error = clib_error_return (0,
1563 "Supported only if 2 or more workes available.");
1570 unformat_free (line_input);
1577 * @cliexstart{set snat workers}
1578 * Set SNAT workers if 2 or more workers available, use:
1579 * vpp# set snat workers 0-2,5
1582 VLIB_CLI_COMMAND (set_workers_command, static) = {
1583 .path = "set snat workers",
1584 .function = set_workers_command_fn,
1586 "set snat workers <workers-list>",
1589 static clib_error_t *
1590 snat_ipfix_logging_enable_disable_command_fn (vlib_main_t * vm,
1591 unformat_input_t * input,
1592 vlib_cli_command_t * cmd)
1594 unformat_input_t _line_input, *line_input = &_line_input;
1599 clib_error_t *error = 0;
1601 /* Get a line of input. */
1602 if (!unformat_user (input, unformat_line_input, line_input))
1605 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1607 if (unformat (line_input, "domain %d", &domain_id))
1609 else if (unformat (line_input, "src-port %d", &src_port))
1611 else if (unformat (line_input, "disable"))
1615 error = clib_error_return (0, "unknown input '%U'",
1616 format_unformat_error, line_input);
1621 rv = snat_ipfix_logging_enable_disable (enable, domain_id, (u16) src_port);
1625 error = clib_error_return (0, "ipfix logging enable failed");
1630 unformat_free (line_input);
1637 * @cliexstart{snat ipfix logging}
1638 * To enable SNAT IPFIX logging use:
1639 * vpp# snat ipfix logging
1640 * To set IPFIX exporter use:
1641 * vpp# set ipfix exporter collector 10.10.10.3 src 10.10.10.1
1644 VLIB_CLI_COMMAND (snat_ipfix_logging_enable_disable_command, static) = {
1645 .path = "snat ipfix logging",
1646 .function = snat_ipfix_logging_enable_disable_command_fn,
1647 .short_help = "snat ipfix logging [domain <domain-id>] [src-port <port>] [disable]",
1651 snat_get_worker_in2out_cb (ip4_header_t * ip0, u32 rx_fib_index0)
1653 snat_main_t *sm = &snat_main;
1654 snat_user_key_t key0;
1655 clib_bihash_kv_8_8_t kv0, value0;
1656 u32 next_worker_index = 0;
1658 key0.addr = ip0->src_address;
1659 key0.fib_index = rx_fib_index0;
1661 kv0.key = key0.as_u64;
1663 /* Ever heard of of the "user" before? */
1664 if (clib_bihash_search_8_8 (&sm->worker_by_in, &kv0, &value0))
1666 /* No, assign next available worker (RR) */
1667 next_worker_index = sm->first_worker_index;
1668 if (vec_len (sm->workers))
1670 next_worker_index +=
1671 sm->workers[sm->next_worker++ % _vec_len (sm->workers)];
1674 /* add non-traslated packets worker lookup */
1675 kv0.value = next_worker_index;
1676 clib_bihash_add_del_8_8 (&sm->worker_by_in, &kv0, 1);
1679 next_worker_index = value0.value;
1681 return next_worker_index;
1685 snat_get_worker_out2in_cb (ip4_header_t * ip0, u32 rx_fib_index0)
1687 snat_main_t *sm = &snat_main;
1688 snat_worker_key_t key0;
1689 clib_bihash_kv_8_8_t kv0, value0;
1690 udp_header_t * udp0;
1691 u32 next_worker_index = 0;
1693 udp0 = ip4_next_header (ip0);
1695 key0.addr = ip0->dst_address;
1696 key0.port = udp0->dst_port;
1697 key0.fib_index = rx_fib_index0;
1699 if (PREDICT_FALSE(ip0->protocol == IP_PROTOCOL_ICMP))
1701 icmp46_header_t * icmp0 = (icmp46_header_t *) udp0;
1702 icmp_echo_header_t *echo0 = (icmp_echo_header_t *)(icmp0+1);
1703 key0.port = echo0->identifier;
1706 kv0.key = key0.as_u64;
1708 /* Ever heard of of the "user" before? */
1709 if (clib_bihash_search_8_8 (&sm->worker_by_out, &kv0, &value0))
1712 kv0.key = key0.as_u64;
1714 if (clib_bihash_search_8_8 (&sm->worker_by_out, &kv0, &value0))
1716 /* No, assign next available worker (RR) */
1717 next_worker_index = sm->first_worker_index;
1718 if (vec_len (sm->workers))
1720 next_worker_index +=
1721 sm->workers[sm->next_worker++ % _vec_len (sm->workers)];
1726 /* Static mapping without port */
1727 next_worker_index = value0.value;
1730 /* Add to translated packets worker lookup */
1731 kv0.value = next_worker_index;
1732 clib_bihash_add_del_8_8 (&sm->worker_by_out, &kv0, 1);
1735 next_worker_index = value0.value;
1737 return next_worker_index;
1740 static clib_error_t *
1741 snat_config (vlib_main_t * vm, unformat_input_t * input)
1743 snat_main_t * sm = &snat_main;
1744 u32 translation_buckets = 1024;
1745 u32 translation_memory_size = 128<<20;
1746 u32 user_buckets = 128;
1747 u32 user_memory_size = 64<<20;
1748 u32 max_translations_per_user = 100;
1749 u32 outside_vrf_id = 0;
1750 u32 inside_vrf_id = 0;
1751 u32 static_mapping_buckets = 1024;
1752 u32 static_mapping_memory_size = 64<<20;
1753 u8 static_mapping_only = 0;
1754 u8 static_mapping_connection_tracking = 0;
1756 sm->deterministic = 0;
1758 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1760 if (unformat (input, "translation hash buckets %d", &translation_buckets))
1762 else if (unformat (input, "translation hash memory %d",
1763 &translation_memory_size));
1764 else if (unformat (input, "user hash buckets %d", &user_buckets))
1766 else if (unformat (input, "user hash memory %d",
1769 else if (unformat (input, "max translations per user %d",
1770 &max_translations_per_user))
1772 else if (unformat (input, "outside VRF id %d",
1775 else if (unformat (input, "inside VRF id %d",
1778 else if (unformat (input, "static mapping only"))
1780 static_mapping_only = 1;
1781 if (unformat (input, "connection tracking"))
1782 static_mapping_connection_tracking = 1;
1784 else if (unformat (input, "deterministic"))
1785 sm->deterministic = 1;
1787 return clib_error_return (0, "unknown input '%U'",
1788 format_unformat_error, input);
1791 /* for show commands, etc. */
1792 sm->translation_buckets = translation_buckets;
1793 sm->translation_memory_size = translation_memory_size;
1794 sm->user_buckets = user_buckets;
1795 sm->user_memory_size = user_memory_size;
1796 sm->max_translations_per_user = max_translations_per_user;
1797 sm->outside_vrf_id = outside_vrf_id;
1798 sm->outside_fib_index = fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4,
1800 sm->inside_vrf_id = inside_vrf_id;
1801 sm->inside_fib_index = fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4,
1803 sm->static_mapping_only = static_mapping_only;
1804 sm->static_mapping_connection_tracking = static_mapping_connection_tracking;
1806 if (sm->deterministic)
1808 sm->in2out_node_index = snat_det_in2out_node.index;
1809 sm->in2out_output_node_index = ~0;
1810 sm->out2in_node_index = snat_det_out2in_node.index;
1811 sm->icmp_match_in2out_cb = icmp_match_in2out_det;
1812 sm->icmp_match_out2in_cb = icmp_match_out2in_det;
1816 sm->worker_in2out_cb = snat_get_worker_in2out_cb;
1817 sm->worker_out2in_cb = snat_get_worker_out2in_cb;
1818 sm->in2out_node_index = snat_in2out_node.index;
1819 sm->in2out_output_node_index = snat_in2out_output_node.index;
1820 sm->out2in_node_index = snat_out2in_node.index;
1821 if (!static_mapping_only ||
1822 (static_mapping_only && static_mapping_connection_tracking))
1824 sm->icmp_match_in2out_cb = icmp_match_in2out_slow;
1825 sm->icmp_match_out2in_cb = icmp_match_out2in_slow;
1827 clib_bihash_init_8_8 (&sm->worker_by_in, "worker-by-in", user_buckets,
1830 clib_bihash_init_8_8 (&sm->worker_by_out, "worker-by-out", user_buckets,
1833 clib_bihash_init_8_8 (&sm->in2out, "in2out", translation_buckets,
1834 translation_memory_size);
1836 clib_bihash_init_8_8 (&sm->out2in, "out2in", translation_buckets,
1837 translation_memory_size);
1839 clib_bihash_init_8_8 (&sm->user_hash, "users", user_buckets,
1842 clib_bihash_init_16_8 (&sm->in2out_unk_proto, "in2out-unk-proto",
1843 translation_buckets, translation_memory_size);
1845 clib_bihash_init_16_8 (&sm->out2in_unk_proto, "out2in-unk-proto",
1846 translation_buckets, translation_memory_size);
1850 sm->icmp_match_in2out_cb = icmp_match_in2out_fast;
1851 sm->icmp_match_out2in_cb = icmp_match_out2in_fast;
1853 clib_bihash_init_8_8 (&sm->static_mapping_by_local,
1854 "static_mapping_by_local", static_mapping_buckets,
1855 static_mapping_memory_size);
1857 clib_bihash_init_8_8 (&sm->static_mapping_by_external,
1858 "static_mapping_by_external", static_mapping_buckets,
1859 static_mapping_memory_size);
1865 VLIB_CONFIG_FUNCTION (snat_config, "snat");
1867 u8 * format_snat_session_state (u8 * s, va_list * args)
1869 u32 i = va_arg (*args, u32);
1874 #define _(v, N, str) case SNAT_SESSION_##N: t = (u8 *) str; break;
1875 foreach_snat_session_state
1878 t = format (t, "unknown");
1880 s = format (s, "%s", t);
1884 u8 * format_snat_key (u8 * s, va_list * args)
1886 snat_session_key_t * key = va_arg (*args, snat_session_key_t *);
1887 char * protocol_string = "unknown";
1888 static char *protocol_strings[] = {
1894 if (key->protocol < ARRAY_LEN(protocol_strings))
1895 protocol_string = protocol_strings[key->protocol];
1897 s = format (s, "%U proto %s port %d fib %d",
1898 format_ip4_address, &key->addr, protocol_string,
1899 clib_net_to_host_u16 (key->port), key->fib_index);
1903 u8 * format_snat_session (u8 * s, va_list * args)
1905 snat_main_t * sm __attribute__((unused)) = va_arg (*args, snat_main_t *);
1906 snat_session_t * sess = va_arg (*args, snat_session_t *);
1908 if (snat_is_unk_proto_session (sess))
1910 s = format (s, " i2o %U proto %u fib %u\n",
1911 format_ip4_address, &sess->in2out.addr, sess->in2out.port,
1912 sess->in2out.fib_index);
1913 s = format (s, " o2i %U proto %u fib %u\n",
1914 format_ip4_address, &sess->out2in.addr, sess->out2in.port,
1915 sess->out2in.fib_index);
1919 s = format (s, " i2o %U\n", format_snat_key, &sess->in2out);
1920 s = format (s, " o2i %U\n", format_snat_key, &sess->out2in);
1922 s = format (s, " last heard %.2f\n", sess->last_heard);
1923 s = format (s, " total pkts %d, total bytes %lld\n",
1924 sess->total_pkts, sess->total_bytes);
1925 if (snat_is_session_static (sess))
1926 s = format (s, " static translation\n");
1928 s = format (s, " dynamic translation\n");
1933 u8 * format_snat_user (u8 * s, va_list * args)
1935 snat_main_per_thread_data_t * sm = va_arg (*args, snat_main_per_thread_data_t *);
1936 snat_user_t * u = va_arg (*args, snat_user_t *);
1937 int verbose = va_arg (*args, int);
1938 dlist_elt_t * head, * elt;
1939 u32 elt_index, head_index;
1941 snat_session_t * sess;
1943 s = format (s, "%U: %d dynamic translations, %d static translations\n",
1944 format_ip4_address, &u->addr, u->nsessions, u->nstaticsessions);
1949 if (u->nsessions || u->nstaticsessions)
1951 head_index = u->sessions_per_user_list_head_index;
1952 head = pool_elt_at_index (sm->list_pool, head_index);
1954 elt_index = head->next;
1955 elt = pool_elt_at_index (sm->list_pool, elt_index);
1956 session_index = elt->value;
1958 while (session_index != ~0)
1960 sess = pool_elt_at_index (sm->sessions, session_index);
1962 s = format (s, " %U\n", format_snat_session, sm, sess);
1964 elt_index = elt->next;
1965 elt = pool_elt_at_index (sm->list_pool, elt_index);
1966 session_index = elt->value;
1973 u8 * format_snat_static_mapping (u8 * s, va_list * args)
1975 snat_static_mapping_t *m = va_arg (*args, snat_static_mapping_t *);
1978 s = format (s, "local %U external %U vrf %d",
1979 format_ip4_address, &m->local_addr,
1980 format_ip4_address, &m->external_addr,
1983 s = format (s, "%U local %U:%d external %U:%d vrf %d",
1984 format_snat_protocol, m->proto,
1985 format_ip4_address, &m->local_addr, m->local_port,
1986 format_ip4_address, &m->external_addr, m->external_port,
1992 u8 * format_snat_static_map_to_resolve (u8 * s, va_list * args)
1994 snat_static_map_resolve_t *m = va_arg (*args, snat_static_map_resolve_t *);
1995 vnet_main_t *vnm = vnet_get_main();
1998 s = format (s, "local %U external %U vrf %d",
1999 format_ip4_address, &m->l_addr,
2000 format_vnet_sw_interface_name, vnm,
2001 vnet_get_sw_interface (vnm, m->sw_if_index),
2004 s = format (s, "%U local %U:%d external %U:%d vrf %d",
2005 format_snat_protocol, m->proto,
2006 format_ip4_address, &m->l_addr, m->l_port,
2007 format_vnet_sw_interface_name, vnm,
2008 vnet_get_sw_interface (vnm, m->sw_if_index), m->e_port,
2014 u8 * format_det_map_ses (u8 * s, va_list * args)
2016 snat_det_map_t * det_map = va_arg (*args, snat_det_map_t *);
2017 ip4_address_t in_addr, out_addr;
2018 u32 in_offset, out_offset;
2019 snat_det_session_t * ses = va_arg (*args, snat_det_session_t *);
2020 u32 * i = va_arg (*args, u32 *);
2022 u32 user_index = *i / SNAT_DET_SES_PER_USER;
2023 in_addr.as_u32 = clib_host_to_net_u32 (
2024 clib_net_to_host_u32(det_map->in_addr.as_u32) + user_index);
2025 in_offset = clib_net_to_host_u32(in_addr.as_u32) -
2026 clib_net_to_host_u32(det_map->in_addr.as_u32);
2027 out_offset = in_offset / det_map->sharing_ratio;
2028 out_addr.as_u32 = clib_host_to_net_u32(
2029 clib_net_to_host_u32(det_map->out_addr.as_u32) + out_offset);
2030 s = format (s, "in %U:%d out %U:%d external host %U:%d state: %U expire: %d\n",
2031 format_ip4_address, &in_addr,
2032 clib_net_to_host_u16 (ses->in_port),
2033 format_ip4_address, &out_addr,
2034 clib_net_to_host_u16 (ses->out.out_port),
2035 format_ip4_address, &ses->out.ext_host_addr,
2036 clib_net_to_host_u16 (ses->out.ext_host_port),
2037 format_snat_session_state, ses->state,
2043 static clib_error_t *
2044 show_snat_command_fn (vlib_main_t * vm,
2045 unformat_input_t * input,
2046 vlib_cli_command_t * cmd)
2049 snat_main_t * sm = &snat_main;
2051 snat_static_mapping_t *m;
2052 snat_interface_t *i;
2053 snat_address_t * ap;
2054 vnet_main_t *vnm = vnet_get_main();
2055 snat_main_per_thread_data_t *tsm;
2056 u32 users_num = 0, sessions_num = 0, *worker, *sw_if_index;
2058 snat_static_map_resolve_t *rp;
2059 snat_det_map_t * dm;
2060 snat_det_session_t * ses;
2062 if (unformat (input, "detail"))
2064 else if (unformat (input, "verbose"))
2067 if (sm->static_mapping_only)
2069 if (sm->static_mapping_connection_tracking)
2070 vlib_cli_output (vm, "SNAT mode: static mapping only connection "
2073 vlib_cli_output (vm, "SNAT mode: static mapping only");
2075 else if (sm->deterministic)
2077 vlib_cli_output (vm, "SNAT mode: deterministic mapping");
2081 vlib_cli_output (vm, "SNAT mode: dynamic translations enabled");
2086 pool_foreach (i, sm->interfaces,
2088 vlib_cli_output (vm, "%U %s", format_vnet_sw_interface_name, vnm,
2089 vnet_get_sw_interface (vnm, i->sw_if_index),
2090 i->is_inside ? "in" : "out");
2093 pool_foreach (i, sm->output_feature_interfaces,
2095 vlib_cli_output (vm, "%U output-feature %s",
2096 format_vnet_sw_interface_name, vnm,
2097 vnet_get_sw_interface (vnm, i->sw_if_index),
2098 i->is_inside ? "in" : "out");
2101 if (vec_len (sm->auto_add_sw_if_indices))
2103 vlib_cli_output (vm, "SNAT pool addresses interfaces:");
2104 vec_foreach (sw_if_index, sm->auto_add_sw_if_indices)
2106 vlib_cli_output (vm, "%U", format_vnet_sw_interface_name, vnm,
2107 vnet_get_sw_interface (vnm, *sw_if_index));
2111 vec_foreach (ap, sm->addresses)
2113 vlib_cli_output (vm, "%U", format_ip4_address, &ap->addr);
2114 if (ap->fib_index != ~0)
2115 vlib_cli_output (vm, " tenant VRF: %u",
2116 ip4_fib_get(ap->fib_index)->table_id);
2118 vlib_cli_output (vm, " tenant VRF independent");
2119 #define _(N, i, n, s) \
2120 vlib_cli_output (vm, " %d busy %s ports", ap->busy_##n##_ports, s);
2121 foreach_snat_protocol
2126 if (sm->num_workers > 1)
2128 vlib_cli_output (vm, "%d workers", vec_len (sm->workers));
2131 vec_foreach (worker, sm->workers)
2133 vlib_worker_thread_t *w =
2134 vlib_worker_threads + *worker + sm->first_worker_index;
2135 vlib_cli_output (vm, " %s", w->name);
2140 if (sm->deterministic)
2142 vlib_cli_output (vm, "udp timeout: %dsec", sm->udp_timeout);
2143 vlib_cli_output (vm, "tcp-established timeout: %dsec",
2144 sm->tcp_established_timeout);
2145 vlib_cli_output (vm, "tcp-transitory timeout: %dsec",
2146 sm->tcp_transitory_timeout);
2147 vlib_cli_output (vm, "icmp timeout: %dsec", sm->icmp_timeout);
2148 vlib_cli_output (vm, "%d deterministic mappings",
2149 pool_elts (sm->det_maps));
2152 pool_foreach (dm, sm->det_maps,
2154 vlib_cli_output (vm, "in %U/%d out %U/%d\n",
2155 format_ip4_address, &dm->in_addr, dm->in_plen,
2156 format_ip4_address, &dm->out_addr, dm->out_plen);
2157 vlib_cli_output (vm, " outside address sharing ratio: %d\n",
2159 vlib_cli_output (vm, " number of ports per inside host: %d\n",
2160 dm->ports_per_host);
2161 vlib_cli_output (vm, " sessions number: %d\n", dm->ses_num);
2164 vec_foreach_index (j, dm->sessions)
2166 ses = vec_elt_at_index (dm->sessions, j);
2168 vlib_cli_output (vm, " %U", format_det_map_ses, dm, ses,
2177 if (sm->static_mapping_only && !(sm->static_mapping_connection_tracking))
2179 vlib_cli_output (vm, "%d static mappings",
2180 pool_elts (sm->static_mappings));
2184 pool_foreach (m, sm->static_mappings,
2186 vlib_cli_output (vm, "%U", format_snat_static_mapping, m);
2192 vec_foreach (tsm, sm->per_thread_data)
2194 users_num += pool_elts (tsm->users);
2195 sessions_num += pool_elts (tsm->sessions);
2198 vlib_cli_output (vm, "%d users, %d outside addresses, %d active sessions,"
2199 " %d static mappings",
2201 vec_len (sm->addresses),
2203 pool_elts (sm->static_mappings));
2207 vlib_cli_output (vm, "%U", format_bihash_8_8, &sm->in2out,
2209 vlib_cli_output (vm, "%U", format_bihash_8_8, &sm->out2in,
2211 vlib_cli_output (vm, "%U", format_bihash_8_8, &sm->worker_by_in,
2213 vlib_cli_output (vm, "%U", format_bihash_8_8, &sm->worker_by_out,
2215 vec_foreach_index (j, sm->per_thread_data)
2217 tsm = vec_elt_at_index (sm->per_thread_data, j);
2219 if (pool_elts (tsm->users) == 0)
2222 vlib_worker_thread_t *w = vlib_worker_threads + j;
2223 vlib_cli_output (vm, "Thread %d (%s at lcore %u):", j, w->name,
2225 vlib_cli_output (vm, " %d list pool elements",
2226 pool_elts (tsm->list_pool));
2228 pool_foreach (u, tsm->users,
2230 vlib_cli_output (vm, " %U", format_snat_user, tsm, u,
2235 if (pool_elts (sm->static_mappings))
2237 vlib_cli_output (vm, "static mappings:");
2238 pool_foreach (m, sm->static_mappings,
2240 vlib_cli_output (vm, "%U", format_snat_static_mapping, m);
2242 for (j = 0; j < vec_len (sm->to_resolve); j++)
2244 rp = sm->to_resolve + j;
2245 vlib_cli_output (vm, "%U",
2246 format_snat_static_map_to_resolve, rp);
2255 VLIB_CLI_COMMAND (show_snat_command, static) = {
2256 .path = "show snat",
2257 .short_help = "show snat",
2258 .function = show_snat_command_fn,
2263 snat_ip4_add_del_interface_address_cb (ip4_main_t * im,
2266 ip4_address_t * address,
2268 u32 if_address_index,
2271 snat_main_t *sm = &snat_main;
2272 snat_static_map_resolve_t *rp;
2273 u32 *indices_to_delete = 0;
2277 for (i = 0; i < vec_len(sm->auto_add_sw_if_indices); i++)
2279 if (sw_if_index == sm->auto_add_sw_if_indices[i])
2283 /* Don't trip over lease renewal, static config */
2284 for (j = 0; j < vec_len(sm->addresses); j++)
2285 if (sm->addresses[j].addr.as_u32 == address->as_u32)
2288 snat_add_address (sm, address, ~0);
2289 /* Scan static map resolution vector */
2290 for (j = 0; j < vec_len (sm->to_resolve); j++)
2292 rp = sm->to_resolve + j;
2293 /* On this interface? */
2294 if (rp->sw_if_index == sw_if_index)
2296 /* Add the static mapping */
2297 rv = snat_add_static_mapping (rp->l_addr,
2303 ~0 /* sw_if_index */,
2307 clib_warning ("snat_add_static_mapping returned %d",
2309 vec_add1 (indices_to_delete, j);
2312 /* If we resolved any of the outstanding static mappings */
2313 if (vec_len(indices_to_delete))
2316 for (j = vec_len(indices_to_delete)-1; j >= 0; j--)
2317 vec_delete(sm->to_resolve, 1, j);
2318 vec_free(indices_to_delete);
2324 (void) snat_del_address(sm, address[0], 1);
2332 int snat_add_interface_address (snat_main_t *sm, u32 sw_if_index, int is_del)
2334 ip4_main_t * ip4_main = sm->ip4_main;
2335 ip4_address_t * first_int_addr;
2336 snat_static_map_resolve_t *rp;
2337 u32 *indices_to_delete = 0;
2340 first_int_addr = ip4_interface_first_address (ip4_main, sw_if_index,
2341 0 /* just want the address*/);
2343 for (i = 0; i < vec_len(sm->auto_add_sw_if_indices); i++)
2345 if (sm->auto_add_sw_if_indices[i] == sw_if_index)
2349 /* if have address remove it */
2351 (void) snat_del_address (sm, first_int_addr[0], 1);
2354 for (j = 0; j < vec_len (sm->to_resolve); j++)
2356 rp = sm->to_resolve + j;
2357 if (rp->sw_if_index == sw_if_index)
2358 vec_add1 (indices_to_delete, j);
2360 if (vec_len(indices_to_delete))
2362 for (j = vec_len(indices_to_delete)-1; j >= 0; j--)
2363 vec_del1(sm->to_resolve, j);
2364 vec_free(indices_to_delete);
2367 vec_del1(sm->auto_add_sw_if_indices, i);
2370 return VNET_API_ERROR_VALUE_EXIST;
2377 return VNET_API_ERROR_NO_SUCH_ENTRY;
2379 /* add to the auto-address list */
2380 vec_add1(sm->auto_add_sw_if_indices, sw_if_index);
2382 /* If the address is already bound - or static - add it now */
2384 snat_add_address (sm, first_int_addr, ~0);
2389 static clib_error_t *
2390 snat_add_interface_address_command_fn (vlib_main_t * vm,
2391 unformat_input_t * input,
2392 vlib_cli_command_t * cmd)
2394 snat_main_t *sm = &snat_main;
2395 unformat_input_t _line_input, *line_input = &_line_input;
2399 clib_error_t *error = 0;
2401 /* Get a line of input. */
2402 if (!unformat_user (input, unformat_line_input, line_input))
2405 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2407 if (unformat (line_input, "%U", unformat_vnet_sw_interface,
2408 sm->vnet_main, &sw_if_index))
2410 else if (unformat (line_input, "del"))
2414 error = clib_error_return (0, "unknown input '%U'",
2415 format_unformat_error, line_input);
2420 rv = snat_add_interface_address (sm, sw_if_index, is_del);
2428 error = clib_error_return (0, "snat_add_interface_address returned %d",
2434 unformat_free (line_input);
2439 VLIB_CLI_COMMAND (snat_add_interface_address_command, static) = {
2440 .path = "snat add interface address",
2441 .short_help = "snat add interface address <interface> [del]",
2442 .function = snat_add_interface_address_command_fn,
2445 static clib_error_t *
2446 snat_det_map_command_fn (vlib_main_t * vm,
2447 unformat_input_t * input,
2448 vlib_cli_command_t * cmd)
2450 snat_main_t *sm = &snat_main;
2451 unformat_input_t _line_input, *line_input = &_line_input;
2452 ip4_address_t in_addr, out_addr;
2453 u32 in_plen, out_plen;
2455 clib_error_t *error = 0;
2457 /* Get a line of input. */
2458 if (!unformat_user (input, unformat_line_input, line_input))
2461 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2463 if (unformat (line_input, "in %U/%u", unformat_ip4_address, &in_addr, &in_plen))
2465 else if (unformat (line_input, "out %U/%u", unformat_ip4_address, &out_addr, &out_plen))
2467 else if (unformat (line_input, "del"))
2471 error = clib_error_return (0, "unknown input '%U'",
2472 format_unformat_error, line_input);
2477 unformat_free (line_input);
2479 rv = snat_det_add_map(sm, &in_addr, (u8) in_plen, &out_addr, (u8)out_plen,
2484 error = clib_error_return (0, "snat_det_add_map return %d", rv);
2489 unformat_free (line_input);
2496 * @cliexstart{snat deterministic add}
2497 * Create bijective mapping of inside address to outside address and port range
2498 * pairs, with the purpose of enabling deterministic NAT to reduce logging in
2500 * To create deterministic mapping between inside network 10.0.0.0/18 and
2501 * outside network 1.1.1.0/30 use:
2502 * # vpp# snat deterministic add in 10.0.0.0/18 out 1.1.1.0/30
2505 VLIB_CLI_COMMAND (snat_det_map_command, static) = {
2506 .path = "snat deterministic add",
2507 .short_help = "snat deterministic add in <addr>/<plen> out <addr>/<plen> [del]",
2508 .function = snat_det_map_command_fn,
2511 static clib_error_t *
2512 snat_det_forward_command_fn (vlib_main_t * vm,
2513 unformat_input_t * input,
2514 vlib_cli_command_t * cmd)
2516 snat_main_t *sm = &snat_main;
2517 unformat_input_t _line_input, *line_input = &_line_input;
2518 ip4_address_t in_addr, out_addr;
2520 snat_det_map_t * dm;
2521 clib_error_t *error = 0;
2523 /* Get a line of input. */
2524 if (!unformat_user (input, unformat_line_input, line_input))
2527 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2529 if (unformat (line_input, "%U", unformat_ip4_address, &in_addr))
2533 error = clib_error_return (0, "unknown input '%U'",
2534 format_unformat_error, line_input);
2539 unformat_free (line_input);
2541 dm = snat_det_map_by_user(sm, &in_addr);
2543 vlib_cli_output (vm, "no match");
2546 snat_det_forward (dm, &in_addr, &out_addr, &lo_port);
2547 vlib_cli_output (vm, "%U:<%d-%d>", format_ip4_address, &out_addr,
2548 lo_port, lo_port + dm->ports_per_host - 1);
2552 unformat_free (line_input);
2559 * @cliexstart{snat deterministic forward}
2560 * Return outside address and port range from inside address for deterministic
2562 * To obtain outside address and port of inside host use:
2563 * vpp# snat deterministic forward 10.0.0.2
2564 * 1.1.1.0:<1054-1068>
2567 VLIB_CLI_COMMAND (snat_det_forward_command, static) = {
2568 .path = "snat deterministic forward",
2569 .short_help = "snat deterministic forward <addr>",
2570 .function = snat_det_forward_command_fn,
2573 static clib_error_t *
2574 snat_det_reverse_command_fn (vlib_main_t * vm,
2575 unformat_input_t * input,
2576 vlib_cli_command_t * cmd)
2578 snat_main_t *sm = &snat_main;
2579 unformat_input_t _line_input, *line_input = &_line_input;
2580 ip4_address_t in_addr, out_addr;
2582 snat_det_map_t * dm;
2583 clib_error_t *error = 0;
2585 /* Get a line of input. */
2586 if (!unformat_user (input, unformat_line_input, line_input))
2589 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2591 if (unformat (line_input, "%U:%d", unformat_ip4_address, &out_addr, &out_port))
2595 error = clib_error_return (0, "unknown input '%U'",
2596 format_unformat_error, line_input);
2600 unformat_free (line_input);
2602 if (out_port < 1024 || out_port > 65535)
2604 error = clib_error_return (0, "wrong port, must be <1024-65535>");
2608 dm = snat_det_map_by_out(sm, &out_addr);
2610 vlib_cli_output (vm, "no match");
2613 snat_det_reverse (dm, &out_addr, (u16) out_port, &in_addr);
2614 vlib_cli_output (vm, "%U", format_ip4_address, &in_addr);
2618 unformat_free (line_input);
2625 * @cliexstart{snat deterministic reverse}
2626 * Return inside address from outside address and port for deterministic NAT.
2627 * To obtain inside host address from outside address and port use:
2628 * #vpp snat deterministic reverse 1.1.1.1:1276
2632 VLIB_CLI_COMMAND (snat_det_reverse_command, static) = {
2633 .path = "snat deterministic reverse",
2634 .short_help = "snat deterministic reverse <addr>:<port>",
2635 .function = snat_det_reverse_command_fn,
2638 static clib_error_t *
2639 set_timeout_command_fn (vlib_main_t * vm,
2640 unformat_input_t * input,
2641 vlib_cli_command_t * cmd)
2643 snat_main_t *sm = &snat_main;
2644 unformat_input_t _line_input, *line_input = &_line_input;
2645 clib_error_t *error = 0;
2647 /* Get a line of input. */
2648 if (!unformat_user (input, unformat_line_input, line_input))
2651 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2653 if (unformat (line_input, "udp %u", &sm->udp_timeout))
2655 else if (unformat (line_input, "tcp-established %u",
2656 &sm->tcp_established_timeout))
2658 else if (unformat (line_input, "tcp-transitory %u",
2659 &sm->tcp_transitory_timeout))
2661 else if (unformat (line_input, "icmp %u", &sm->icmp_timeout))
2663 else if (unformat (line_input, "reset"))
2665 sm->udp_timeout = SNAT_UDP_TIMEOUT;
2666 sm->tcp_established_timeout = SNAT_TCP_ESTABLISHED_TIMEOUT;
2667 sm->tcp_transitory_timeout = SNAT_TCP_TRANSITORY_TIMEOUT;
2668 sm->icmp_timeout = SNAT_ICMP_TIMEOUT;
2672 error = clib_error_return (0, "unknown input '%U'",
2673 format_unformat_error, line_input);
2678 unformat_free (line_input);
2681 unformat_free (line_input);
2688 * @cliexstart{set snat deterministic timeout}
2689 * Set values of timeouts for deterministic NAT (in seconds), use:
2690 * vpp# set snat deterministic timeout udp 120 tcp-established 7500
2691 * tcp-transitory 250 icmp 90
2692 * To reset default values use:
2693 * vpp# set snat deterministic timeout reset
2696 VLIB_CLI_COMMAND (set_timeout_command, static) = {
2697 .path = "set snat deterministic timeout",
2698 .function = set_timeout_command_fn,
2700 "set snat deterministic timeout [udp <sec> | tcp-established <sec> "
2701 "tcp-transitory <sec> | icmp <sec> | reset]",
2704 static clib_error_t *
2705 snat_det_close_session_out_fn (vlib_main_t *vm,
2706 unformat_input_t * input,
2707 vlib_cli_command_t * cmd)
2709 snat_main_t *sm = &snat_main;
2710 unformat_input_t _line_input, *line_input = &_line_input;
2711 ip4_address_t out_addr, ext_addr, in_addr;
2712 u16 out_port, ext_port;
2713 snat_det_map_t * dm;
2714 snat_det_session_t * ses;
2715 snat_det_out_key_t key;
2716 clib_error_t *error = 0;
2718 /* Get a line of input. */
2719 if (!unformat_user (input, unformat_line_input, line_input))
2722 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2724 if (unformat (line_input, "%U:%d %U:%d",
2725 unformat_ip4_address, &out_addr, &out_port,
2726 unformat_ip4_address, &ext_addr, &ext_port))
2730 error = clib_error_return (0, "unknown input '%U'",
2731 format_unformat_error, line_input);
2736 unformat_free (line_input);
2738 dm = snat_det_map_by_out(sm, &out_addr);
2740 vlib_cli_output (vm, "no match");
2743 snat_det_reverse(dm, &ext_addr, out_port, &in_addr);
2744 key.ext_host_addr = out_addr;
2745 key.ext_host_port = ntohs(ext_port);
2746 key.out_port = ntohs(out_port);
2747 ses = snat_det_get_ses_by_out(dm, &out_addr, key.as_u64);
2749 vlib_cli_output (vm, "no match");
2751 snat_det_ses_close(dm, ses);
2755 unformat_free (line_input);
2762 * @cliexstart{snat deterministic close session out}
2763 * Close session using outside ip address and port
2764 * and external ip address and port, use:
2765 * vpp# snat deterministic close session out 1.1.1.1:1276 2.2.2.2:2387
2768 VLIB_CLI_COMMAND (snat_det_close_sesion_out_command, static) = {
2769 .path = "snat deterministic close session out",
2770 .short_help = "snat deterministic close session out "
2771 "<out_addr>:<out_port> <ext_addr>:<ext_port>",
2772 .function = snat_det_close_session_out_fn,
2775 static clib_error_t *
2776 snat_det_close_session_in_fn (vlib_main_t *vm,
2777 unformat_input_t * input,
2778 vlib_cli_command_t * cmd)
2780 snat_main_t *sm = &snat_main;
2781 unformat_input_t _line_input, *line_input = &_line_input;
2782 ip4_address_t in_addr, ext_addr;
2783 u16 in_port, ext_port;
2784 snat_det_map_t * dm;
2785 snat_det_session_t * ses;
2786 snat_det_out_key_t key;
2787 clib_error_t *error = 0;
2789 /* Get a line of input. */
2790 if (!unformat_user (input, unformat_line_input, line_input))
2793 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2795 if (unformat (line_input, "%U:%d %U:%d",
2796 unformat_ip4_address, &in_addr, &in_port,
2797 unformat_ip4_address, &ext_addr, &ext_port))
2801 error = clib_error_return (0, "unknown input '%U'",
2802 format_unformat_error, line_input);
2807 unformat_free (line_input);
2809 dm = snat_det_map_by_user (sm, &in_addr);
2811 vlib_cli_output (vm, "no match");
2814 key.ext_host_addr = ext_addr;
2815 key.ext_host_port = ntohs (ext_port);
2816 ses = snat_det_find_ses_by_in (dm, &in_addr, ntohs(in_port), key);
2818 vlib_cli_output (vm, "no match");
2820 snat_det_ses_close(dm, ses);
2824 unformat_free(line_input);
2831 * @cliexstart{snat deterministic close_session_in}
2832 * Close session using inside ip address and port
2833 * and external ip address and port, use:
2834 * vpp# snat deterministic close session in 3.3.3.3:3487 2.2.2.2:2387
2837 VLIB_CLI_COMMAND (snat_det_close_session_in_command, static) = {
2838 .path = "snat deterministic close session in",
2839 .short_help = "snat deterministic close session in "
2840 "<in_addr>:<in_port> <ext_addr>:<ext_port>",
2841 .function = snat_det_close_session_in_fn,