2 * Copyright (c) 2018 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
20 #include <vnet/fib/fib_table.h>
22 #include <nat/lib/log.h>
23 #include <nat/lib/nat_inlines.h>
24 #include <nat/lib/ipfix_logging.h>
26 #include <nat/nat44-ed/nat44_ed.h>
27 #include <nat/nat44-ed/nat44_ed_inlines.h>
28 #include <nat/nat44-ed/nat44_ed_affinity.h>
31 nat44_enable_command_fn (vlib_main_t * vm,
32 unformat_input_t * input, vlib_cli_command_t * cmd)
34 snat_main_t *sm = &snat_main;
35 unformat_input_t _line_input, *line_input = &_line_input;
36 clib_error_t *error = 0;
38 nat44_config_t c = { 0 };
42 return clib_error_return (0, "nat44 already enabled");
44 /* Get a line of input. */
45 if (!unformat_user (input, unformat_line_input, line_input))
47 if (nat44_plugin_enable (c) != 0)
48 return clib_error_return (0, "nat44 enable failed");
52 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
54 if (!mode_set && unformat (line_input, "static-mapping-only"))
57 c.static_mapping_only = 1;
58 if (unformat (line_input, "connection-tracking"))
60 c.connection_tracking = 1;
63 else if (unformat (line_input, "inside-vrf %u", &c.inside_vrf));
64 else if (unformat (line_input, "outside-vrf %u", &c.outside_vrf));
65 else if (unformat (line_input, "sessions %u", &c.sessions));
68 error = clib_error_return (0, "unknown input '%U'",
69 format_unformat_error, line_input);
76 error = clib_error_return (0, "number of sessions is required");
80 if (nat44_plugin_enable (c) != 0)
81 error = clib_error_return (0, "nat44 enable failed");
83 unformat_free (line_input);
88 nat44_disable_command_fn (vlib_main_t * vm,
89 unformat_input_t * input, vlib_cli_command_t * cmd)
91 snat_main_t *sm = &snat_main;
92 clib_error_t *error = 0;
95 return clib_error_return (0, "nat44 already disabled");
97 if (nat44_plugin_disable () != 0)
98 error = clib_error_return (0, "nat44 disable failed");
103 static clib_error_t *
104 set_workers_command_fn (vlib_main_t * vm,
105 unformat_input_t * input, vlib_cli_command_t * cmd)
107 unformat_input_t _line_input, *line_input = &_line_input;
110 clib_error_t *error = 0;
112 /* Get a line of input. */
113 if (!unformat_user (input, unformat_line_input, line_input))
116 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
118 if (unformat (line_input, "%U", unformat_bitmap_list, &bitmap))
122 error = clib_error_return (0, "unknown input '%U'",
123 format_unformat_error, line_input);
130 error = clib_error_return (0, "List of workers must be specified.");
134 rv = snat_set_workers (bitmap);
136 clib_bitmap_free (bitmap);
140 case VNET_API_ERROR_INVALID_WORKER:
141 error = clib_error_return (0, "Invalid worker(s).");
143 case VNET_API_ERROR_FEATURE_DISABLED:
144 error = clib_error_return (0,
145 "Supported only if 2 or more workes available.");
152 unformat_free (line_input);
157 static clib_error_t *
158 nat_show_workers_commnad_fn (vlib_main_t * vm, unformat_input_t * input,
159 vlib_cli_command_t * cmd)
161 snat_main_t *sm = &snat_main;
164 if (sm->num_workers > 1)
166 vlib_cli_output (vm, "%d workers", vec_len (sm->workers));
167 vec_foreach (worker, sm->workers)
169 vlib_worker_thread_t *w =
170 vlib_worker_threads + *worker + sm->first_worker_index;
171 vlib_cli_output (vm, " %s", w->name);
178 static clib_error_t *
179 snat_set_log_level_command_fn (vlib_main_t * vm,
180 unformat_input_t * input,
181 vlib_cli_command_t * cmd)
183 unformat_input_t _line_input, *line_input = &_line_input;
184 snat_main_t *sm = &snat_main;
185 u8 log_level = NAT_LOG_NONE;
186 clib_error_t *error = 0;
188 /* Get a line of input. */
189 if (!unformat_user (input, unformat_line_input, line_input))
192 if (!unformat (line_input, "%d", &log_level))
194 error = clib_error_return (0, "unknown input '%U'",
195 format_unformat_error, line_input);
198 if (log_level > NAT_LOG_DEBUG)
200 error = clib_error_return (0, "unknown logging level '%d'", log_level);
203 sm->log_level = log_level;
206 unformat_free (line_input);
211 static clib_error_t *
212 snat_ipfix_logging_enable_disable_command_fn (vlib_main_t * vm,
213 unformat_input_t * input,
214 vlib_cli_command_t * cmd)
216 unformat_input_t _line_input, *line_input = &_line_input;
221 clib_error_t *error = 0;
223 /* Get a line of input. */
224 if (!unformat_user (input, unformat_line_input, line_input))
226 rv = nat_ipfix_logging_enable_disable (enable, domain_id,
229 return clib_error_return (0, "ipfix logging enable failed");
233 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
235 if (unformat (line_input, "domain %d", &domain_id))
237 else if (unformat (line_input, "src-port %d", &src_port))
239 else if (unformat (line_input, "disable"))
243 error = clib_error_return (0, "unknown input '%U'",
244 format_unformat_error, line_input);
249 rv = nat_ipfix_logging_enable_disable (enable, domain_id, (u16) src_port);
253 error = clib_error_return (0, "ipfix logging enable failed");
258 unformat_free (line_input);
263 static clib_error_t *
264 nat44_show_hash_command_fn (vlib_main_t * vm, unformat_input_t * input,
265 vlib_cli_command_t * cmd)
267 snat_main_t *sm = &snat_main;
268 nat_affinity_main_t *nam = &nat_affinity_main;
272 if (unformat (input, "detail"))
274 else if (unformat (input, "verbose"))
277 vlib_cli_output (vm, "%U", format_bihash_8_8, &sm->static_mapping_by_local,
279 vlib_cli_output (vm, "%U",
280 format_bihash_8_8, &sm->static_mapping_by_external,
282 vlib_cli_output (vm, "%U", format_bihash_16_8, &sm->flow_hash, verbose);
283 vec_foreach_index (i, sm->per_thread_data)
285 vlib_cli_output (vm, "-------- thread %d %s --------\n",
286 i, vlib_worker_threads[i].name);
287 vlib_cli_output (vm, "%U", format_bihash_16_8, &sm->flow_hash, verbose);
290 vlib_cli_output (vm, "%U", format_bihash_16_8, &nam->affinity_hash,
293 vlib_cli_output (vm, "-------- hash table parameters --------\n");
294 vlib_cli_output (vm, "translation buckets: %u", sm->translation_buckets);
298 static clib_error_t *
299 nat_set_mss_clamping_command_fn (vlib_main_t * vm, unformat_input_t * input,
300 vlib_cli_command_t * cmd)
302 unformat_input_t _line_input, *line_input = &_line_input;
303 snat_main_t *sm = &snat_main;
304 clib_error_t *error = 0;
307 /* Get a line of input. */
308 if (!unformat_user (input, unformat_line_input, line_input))
311 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
313 if (unformat (line_input, "disable"))
314 sm->mss_clamping = 0;
315 else if (unformat (line_input, "%d", &mss))
316 sm->mss_clamping = (u16) mss;
319 error = clib_error_return (0, "unknown input '%U'",
320 format_unformat_error, line_input);
326 unformat_free (line_input);
331 static clib_error_t *
332 nat_show_mss_clamping_command_fn (vlib_main_t * vm, unformat_input_t * input,
333 vlib_cli_command_t * cmd)
335 snat_main_t *sm = &snat_main;
337 if (sm->mss_clamping)
338 vlib_cli_output (vm, "mss-clamping %d", sm->mss_clamping);
340 vlib_cli_output (vm, "mss-clamping disabled");
345 static clib_error_t *
346 add_address_command_fn (vlib_main_t * vm,
347 unformat_input_t * input, vlib_cli_command_t * cmd)
349 unformat_input_t _line_input, *line_input = &_line_input;
350 snat_main_t *sm = &snat_main;
351 ip4_address_t start_addr, end_addr, this_addr;
352 u32 start_host_order, end_host_order;
357 clib_error_t *error = 0;
360 /* Get a line of input. */
361 if (!unformat_user (input, unformat_line_input, line_input))
364 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
366 if (unformat (line_input, "%U - %U",
367 unformat_ip4_address, &start_addr,
368 unformat_ip4_address, &end_addr))
370 else if (unformat (line_input, "tenant-vrf %u", &vrf_id))
372 else if (unformat (line_input, "%U", unformat_ip4_address, &start_addr))
373 end_addr = start_addr;
374 else if (unformat (line_input, "twice-nat"))
376 else if (unformat (line_input, "del"))
380 error = clib_error_return (0, "unknown input '%U'",
381 format_unformat_error, line_input);
386 if (sm->static_mapping_only)
388 error = clib_error_return (0, "static mapping only mode");
392 start_host_order = clib_host_to_net_u32 (start_addr.as_u32);
393 end_host_order = clib_host_to_net_u32 (end_addr.as_u32);
395 if (end_host_order < start_host_order)
397 error = clib_error_return (0, "end address less than start address");
401 count = (end_host_order - start_host_order) + 1;
404 nat_log_info ("%U - %U, %d addresses...",
405 format_ip4_address, &start_addr,
406 format_ip4_address, &end_addr, count);
408 this_addr = start_addr;
410 for (i = 0; i < count; i++)
413 rv = snat_add_address (sm, &this_addr, vrf_id, twice_nat);
415 rv = snat_del_address (sm, this_addr, 0, twice_nat);
419 case VNET_API_ERROR_VALUE_EXIST:
420 error = clib_error_return (0, "NAT address already in use.");
422 case VNET_API_ERROR_NO_SUCH_ENTRY:
423 error = clib_error_return (0, "NAT address not exist.");
425 case VNET_API_ERROR_UNSPECIFIED:
427 clib_error_return (0, "NAT address used in static mapping.");
433 increment_v4_address (&this_addr);
437 unformat_free (line_input);
443 nat44_show_lru_summary (vlib_main_t * vm, snat_main_per_thread_data_t * tsm,
444 u64 now, u64 sess_timeout_time)
446 snat_main_t *sm = &snat_main;
447 dlist_elt_t *oldest_elt;
455 clib_dlist_remove_head (tsm->lru_pool, tsm->n##_lru_head_index); \
456 if (~0 != oldest_index) \
458 oldest_elt = pool_elt_at_index (tsm->lru_pool, oldest_index); \
459 s = pool_elt_at_index (tsm->sessions, oldest_elt->value); \
460 sess_timeout_time = \
461 s->last_heard + (f64) nat44_session_get_timeout (sm, s); \
462 vlib_cli_output (vm, d " LRU min session timeout %llu (now %llu)", \
463 sess_timeout_time, now); \
464 clib_dlist_addhead (tsm->lru_pool, tsm->n##_lru_head_index, \
467 _ (tcp_estab, "established tcp");
468 _ (tcp_trans, "transitory tcp");
470 _ (unk_proto, "unknown protocol");
476 static clib_error_t *
477 nat44_show_summary_command_fn (vlib_main_t * vm, unformat_input_t * input,
478 vlib_cli_command_t * cmd)
480 snat_main_per_thread_data_t *tsm;
481 snat_main_t *sm = &snat_main;
486 u64 now = vlib_time_now (vm);
487 u64 sess_timeout_time = 0;
489 u32 udp_sessions = 0;
490 u32 tcp_sessions = 0;
491 u32 icmp_sessions = 0;
495 u32 transitory_wait_closed = 0;
496 u32 transitory_closed = 0;
501 for (fib = 0; fib < vec_len (sm->max_translations_per_fib); fib++)
502 vlib_cli_output (vm, "max translations per thread: %u fib %u",
503 sm->max_translations_per_fib[fib], fib);
505 if (sm->num_workers > 1)
507 vec_foreach (tsm, sm->per_thread_data)
509 pool_foreach (s, tsm->sessions)
511 sess_timeout_time = s->last_heard +
512 (f64) nat44_session_get_timeout (sm, s);
513 if (now >= sess_timeout_time)
516 switch (s->nat_proto)
518 case NAT_PROTOCOL_ICMP:
521 case NAT_PROTOCOL_TCP:
525 if (s->tcp_closed_timestamp)
527 if (now >= s->tcp_closed_timestamp)
533 ++transitory_wait_closed;
541 case NAT_PROTOCOL_UDP:
547 nat44_show_lru_summary (vm, tsm, now, sess_timeout_time);
548 count += pool_elts (tsm->sessions);
553 tsm = vec_elt_at_index (sm->per_thread_data, sm->num_workers);
554 pool_foreach (s, tsm->sessions)
556 sess_timeout_time = s->last_heard +
557 (f64) nat44_session_get_timeout (sm, s);
558 if (now >= sess_timeout_time)
561 switch (s->nat_proto)
563 case NAT_PROTOCOL_ICMP:
566 case NAT_PROTOCOL_TCP:
570 if (s->tcp_closed_timestamp)
572 if (now >= s->tcp_closed_timestamp)
578 ++transitory_wait_closed;
586 case NAT_PROTOCOL_UDP:
592 nat44_show_lru_summary (vm, tsm, now, sess_timeout_time);
593 count = pool_elts (tsm->sessions);
596 vlib_cli_output (vm, "total timed out sessions: %u", timed_out);
597 vlib_cli_output (vm, "total sessions: %u", count);
598 vlib_cli_output (vm, "total tcp sessions: %u", tcp_sessions);
599 vlib_cli_output (vm, "total tcp established sessions: %u", established);
600 vlib_cli_output (vm, "total tcp transitory sessions: %u", transitory);
601 vlib_cli_output (vm, "total tcp transitory (WAIT-CLOSED) sessions: %u",
602 transitory_wait_closed);
603 vlib_cli_output (vm, "total tcp transitory (CLOSED) sessions: %u",
605 vlib_cli_output (vm, "total udp sessions: %u", udp_sessions);
606 vlib_cli_output (vm, "total icmp sessions: %u", icmp_sessions);
610 static clib_error_t *
611 nat44_show_addresses_command_fn (vlib_main_t * vm, unformat_input_t * input,
612 vlib_cli_command_t * cmd)
614 snat_main_t *sm = &snat_main;
617 vlib_cli_output (vm, "NAT44 pool addresses:");
618 vec_foreach (ap, sm->addresses)
620 vlib_cli_output (vm, "%U", format_ip4_address, &ap->addr);
621 if (ap->fib_index != ~0)
622 vlib_cli_output (vm, " tenant VRF: %u",
623 fib_table_get(ap->fib_index, FIB_PROTOCOL_IP4)->ft_table_id);
625 vlib_cli_output (vm, " tenant VRF independent");
626 #define _(N, i, n, s) \
627 vlib_cli_output (vm, " %d busy %s ports", ap->busy_##n##_ports, s);
631 vlib_cli_output (vm, "NAT44 twice-nat pool addresses:");
632 vec_foreach (ap, sm->twice_nat_addresses)
634 vlib_cli_output (vm, "%U", format_ip4_address, &ap->addr);
635 if (ap->fib_index != ~0)
636 vlib_cli_output (vm, " tenant VRF: %u",
637 fib_table_get(ap->fib_index, FIB_PROTOCOL_IP4)->ft_table_id);
639 vlib_cli_output (vm, " tenant VRF independent");
640 #define _(N, i, n, s) \
641 vlib_cli_output (vm, " %d busy %s ports", ap->busy_##n##_ports, s);
648 static clib_error_t *
649 snat_feature_command_fn (vlib_main_t * vm,
650 unformat_input_t * input, vlib_cli_command_t * cmd)
652 unformat_input_t _line_input, *line_input = &_line_input;
653 vnet_main_t *vnm = vnet_get_main ();
654 clib_error_t *error = 0;
656 u32 *inside_sw_if_indices = 0;
657 u32 *outside_sw_if_indices = 0;
658 u8 is_output_feature = 0;
664 /* Get a line of input. */
665 if (!unformat_user (input, unformat_line_input, line_input))
668 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
670 if (unformat (line_input, "in %U", unformat_vnet_sw_interface,
672 vec_add1 (inside_sw_if_indices, sw_if_index);
673 else if (unformat (line_input, "out %U", unformat_vnet_sw_interface,
675 vec_add1 (outside_sw_if_indices, sw_if_index);
676 else if (unformat (line_input, "output-feature"))
677 is_output_feature = 1;
678 else if (unformat (line_input, "del"))
682 error = clib_error_return (0, "unknown input '%U'",
683 format_unformat_error, line_input);
688 if (vec_len (inside_sw_if_indices))
690 for (i = 0; i < vec_len (inside_sw_if_indices); i++)
692 sw_if_index = inside_sw_if_indices[i];
693 if (is_output_feature)
695 if (snat_interface_add_del_output_feature
696 (sw_if_index, 1, is_del))
698 error = clib_error_return (0, "%s %U failed",
699 is_del ? "del" : "add",
700 format_vnet_sw_if_index_name,
707 if (snat_interface_add_del (sw_if_index, 1, is_del))
709 error = clib_error_return (0, "%s %U failed",
710 is_del ? "del" : "add",
711 format_vnet_sw_if_index_name,
719 if (vec_len (outside_sw_if_indices))
721 for (i = 0; i < vec_len (outside_sw_if_indices); i++)
723 sw_if_index = outside_sw_if_indices[i];
724 if (is_output_feature)
726 if (snat_interface_add_del_output_feature
727 (sw_if_index, 0, is_del))
729 error = clib_error_return (0, "%s %U failed",
730 is_del ? "del" : "add",
731 format_vnet_sw_if_index_name,
738 if (snat_interface_add_del (sw_if_index, 0, is_del))
740 error = clib_error_return (0, "%s %U failed",
741 is_del ? "del" : "add",
742 format_vnet_sw_if_index_name,
751 unformat_free (line_input);
752 vec_free (inside_sw_if_indices);
753 vec_free (outside_sw_if_indices);
758 static clib_error_t *
759 nat44_show_interfaces_command_fn (vlib_main_t * vm, unformat_input_t * input,
760 vlib_cli_command_t * cmd)
762 snat_main_t *sm = &snat_main;
764 vnet_main_t *vnm = vnet_get_main ();
766 vlib_cli_output (vm, "NAT44 interfaces:");
767 pool_foreach (i, sm->interfaces)
769 vlib_cli_output (vm, " %U %s", format_vnet_sw_if_index_name, vnm,
771 (nat_interface_is_inside(i) &&
772 nat_interface_is_outside(i)) ? "in out" :
773 (nat_interface_is_inside(i) ? "in" : "out"));
776 pool_foreach (i, sm->output_feature_interfaces)
778 vlib_cli_output (vm, " %U output-feature %s",
779 format_vnet_sw_if_index_name, vnm,
781 (nat_interface_is_inside(i) &&
782 nat_interface_is_outside(i)) ? "in out" :
783 (nat_interface_is_inside(i) ? "in" : "out"));
789 static clib_error_t *
790 add_static_mapping_command_fn (vlib_main_t * vm,
791 unformat_input_t * input,
792 vlib_cli_command_t * cmd)
794 unformat_input_t _line_input, *line_input = &_line_input;
795 clib_error_t *error = 0;
796 ip4_address_t l_addr, e_addr, exact_addr;
797 u32 l_port = 0, e_port = 0, vrf_id = ~0;
798 int is_add = 1, addr_only = 1, rv, exact = 0;
799 u32 sw_if_index = ~0;
800 vnet_main_t *vnm = vnet_get_main ();
801 nat_protocol_t proto = NAT_PROTOCOL_OTHER;
803 twice_nat_type_t twice_nat = TWICE_NAT_DISABLED;
806 /* Get a line of input. */
807 if (!unformat_user (input, unformat_line_input, line_input))
810 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
812 if (unformat (line_input, "local %U %u", unformat_ip4_address, &l_addr,
816 if (unformat (line_input, "local %U", unformat_ip4_address, &l_addr))
818 else if (unformat (line_input, "external %U %u", unformat_ip4_address,
821 else if (unformat (line_input, "external %U", unformat_ip4_address,
824 else if (unformat (line_input, "external %U %u",
825 unformat_vnet_sw_interface, vnm, &sw_if_index,
828 else if (unformat (line_input, "external %U",
829 unformat_vnet_sw_interface, vnm, &sw_if_index))
831 else if (unformat (line_input, "exact %U", unformat_ip4_address,
834 else if (unformat (line_input, "vrf %u", &vrf_id))
836 else if (unformat (line_input, "%U", unformat_nat_protocol, &proto))
838 else if (unformat (line_input, "twice-nat"))
839 twice_nat = TWICE_NAT;
840 else if (unformat (line_input, "self-twice-nat"))
841 twice_nat = TWICE_NAT_SELF;
842 else if (unformat (line_input, "out2in-only"))
844 else if (unformat (line_input, "del"))
848 error = clib_error_return (0, "unknown input: '%U'",
849 format_unformat_error, line_input);
854 if (twice_nat && addr_only)
856 error = clib_error_return (0, "twice NAT only for 1:1 NAPT");
865 clib_error_return (0,
866 "address only mapping doesn't support protocol");
872 error = clib_error_return (0, "protocol is required");
876 rv = snat_add_static_mapping (
877 l_addr, e_addr, clib_host_to_net_u16 (l_port),
878 clib_host_to_net_u16 (e_port), vrf_id, addr_only, sw_if_index, proto,
879 is_add, twice_nat, out2in_only, 0, 0, exact_addr, exact);
883 case VNET_API_ERROR_INVALID_VALUE:
884 error = clib_error_return (0, "External port already in use.");
886 case VNET_API_ERROR_NO_SUCH_ENTRY:
888 error = clib_error_return (0, "External address must be allocated.");
890 error = clib_error_return (0, "Mapping not exist.");
892 case VNET_API_ERROR_NO_SUCH_FIB:
893 error = clib_error_return (0, "No such VRF id.");
895 case VNET_API_ERROR_VALUE_EXIST:
896 error = clib_error_return (0, "Mapping already exist.");
903 unformat_free (line_input);
908 static clib_error_t *
909 add_identity_mapping_command_fn (vlib_main_t * vm,
910 unformat_input_t * input,
911 vlib_cli_command_t * cmd)
913 unformat_input_t _line_input, *line_input = &_line_input;
914 clib_error_t *error = 0;
915 ip4_address_t addr, pool_addr = { 0 };
916 u32 port = 0, vrf_id = ~0;
919 u32 sw_if_index = ~0;
920 vnet_main_t *vnm = vnet_get_main ();
922 nat_protocol_t proto;
926 /* Get a line of input. */
927 if (!unformat_user (input, unformat_line_input, line_input))
930 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
932 if (unformat (line_input, "%U", unformat_ip4_address, &addr))
934 else if (unformat (line_input, "external %U",
935 unformat_vnet_sw_interface, vnm, &sw_if_index))
937 else if (unformat (line_input, "vrf %u", &vrf_id))
939 else if (unformat (line_input, "%U %u", unformat_nat_protocol, &proto,
942 else if (unformat (line_input, "del"))
946 error = clib_error_return (0, "unknown input: '%U'",
947 format_unformat_error, line_input);
952 rv = snat_add_static_mapping (
953 addr, addr, clib_host_to_net_u16 (port), clib_host_to_net_u16 (port),
954 vrf_id, addr_only, sw_if_index, proto, is_add, 0, 0, 0, 1, pool_addr, 0);
958 case VNET_API_ERROR_INVALID_VALUE:
959 error = clib_error_return (0, "External port already in use.");
961 case VNET_API_ERROR_NO_SUCH_ENTRY:
963 error = clib_error_return (0, "External address must be allocated.");
965 error = clib_error_return (0, "Mapping not exist.");
967 case VNET_API_ERROR_NO_SUCH_FIB:
968 error = clib_error_return (0, "No such VRF id.");
970 case VNET_API_ERROR_VALUE_EXIST:
971 error = clib_error_return (0, "Mapping already exist.");
978 unformat_free (line_input);
983 static clib_error_t *
984 add_lb_static_mapping_command_fn (vlib_main_t * vm,
985 unformat_input_t * input,
986 vlib_cli_command_t * cmd)
988 unformat_input_t _line_input, *line_input = &_line_input;
989 clib_error_t *error = 0;
990 ip4_address_t l_addr, e_addr;
991 u32 l_port = 0, e_port = 0, vrf_id = 0, probability = 0, affinity = 0;
994 nat_protocol_t proto;
996 nat44_lb_addr_port_t *locals = 0, local;
997 twice_nat_type_t twice_nat = TWICE_NAT_DISABLED;
1000 /* Get a line of input. */
1001 if (!unformat_user (input, unformat_line_input, line_input))
1004 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1006 if (unformat (line_input, "local %U:%u probability %u",
1007 unformat_ip4_address, &l_addr, &l_port, &probability))
1009 clib_memset (&local, 0, sizeof (local));
1010 local.addr = l_addr;
1011 local.port = (u16) l_port;
1012 local.probability = (u8) probability;
1013 vec_add1 (locals, local);
1015 else if (unformat (line_input, "local %U:%u vrf %u probability %u",
1016 unformat_ip4_address, &l_addr, &l_port, &vrf_id,
1019 clib_memset (&local, 0, sizeof (local));
1020 local.addr = l_addr;
1021 local.port = (u16) l_port;
1022 local.probability = (u8) probability;
1023 local.vrf_id = vrf_id;
1024 vec_add1 (locals, local);
1026 else if (unformat (line_input, "external %U:%u", unformat_ip4_address,
1029 else if (unformat (line_input, "protocol %U", unformat_nat_protocol,
1032 else if (unformat (line_input, "twice-nat"))
1033 twice_nat = TWICE_NAT;
1034 else if (unformat (line_input, "self-twice-nat"))
1035 twice_nat = TWICE_NAT_SELF;
1036 else if (unformat (line_input, "out2in-only"))
1038 else if (unformat (line_input, "del"))
1040 else if (unformat (line_input, "affinity %u", &affinity))
1044 error = clib_error_return (0, "unknown input: '%U'",
1045 format_unformat_error, line_input);
1050 if (vec_len (locals) < 2)
1052 error = clib_error_return (0, "at least two local must be set");
1058 error = clib_error_return (0, "missing protocol");
1062 rv = nat44_add_del_lb_static_mapping (e_addr, (u16) e_port, proto, locals,
1063 is_add, twice_nat, out2in_only, 0,
1068 case VNET_API_ERROR_INVALID_VALUE:
1069 error = clib_error_return (0, "External port already in use.");
1071 case VNET_API_ERROR_NO_SUCH_ENTRY:
1073 error = clib_error_return (0, "External address must be allocated.");
1075 error = clib_error_return (0, "Mapping not exist.");
1077 case VNET_API_ERROR_VALUE_EXIST:
1078 error = clib_error_return (0, "Mapping already exist.");
1085 unformat_free (line_input);
1091 static clib_error_t *
1092 add_lb_backend_command_fn (vlib_main_t * vm,
1093 unformat_input_t * input, vlib_cli_command_t * cmd)
1095 unformat_input_t _line_input, *line_input = &_line_input;
1096 clib_error_t *error = 0;
1097 ip4_address_t l_addr, e_addr;
1098 u32 l_port = 0, e_port = 0, vrf_id = 0, probability = 0;
1101 nat_protocol_t proto;
1104 /* Get a line of input. */
1105 if (!unformat_user (input, unformat_line_input, line_input))
1108 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1110 if (unformat (line_input, "local %U:%u probability %u",
1111 unformat_ip4_address, &l_addr, &l_port, &probability))
1113 else if (unformat (line_input, "local %U:%u vrf %u probability %u",
1114 unformat_ip4_address, &l_addr, &l_port, &vrf_id,
1117 else if (unformat (line_input, "external %U:%u", unformat_ip4_address,
1120 else if (unformat (line_input, "protocol %U", unformat_nat_protocol,
1123 else if (unformat (line_input, "del"))
1127 error = clib_error_return (0, "unknown input: '%U'",
1128 format_unformat_error, line_input);
1133 if (!l_port || !e_port)
1135 error = clib_error_return (0, "local or external must be set");
1141 error = clib_error_return (0, "missing protocol");
1146 nat44_lb_static_mapping_add_del_local (e_addr, (u16) e_port, l_addr,
1147 l_port, proto, vrf_id, probability,
1152 case VNET_API_ERROR_INVALID_VALUE:
1153 error = clib_error_return (0, "External is not load-balancing static "
1156 case VNET_API_ERROR_NO_SUCH_ENTRY:
1157 error = clib_error_return (0, "Mapping or back-end not exist.");
1159 case VNET_API_ERROR_VALUE_EXIST:
1160 error = clib_error_return (0, "Back-end already exist.");
1162 case VNET_API_ERROR_UNSPECIFIED:
1163 error = clib_error_return (0, "At least two back-ends must remain");
1170 unformat_free (line_input);
1175 static clib_error_t *
1176 nat44_show_static_mappings_command_fn (vlib_main_t * vm,
1177 unformat_input_t * input,
1178 vlib_cli_command_t * cmd)
1180 snat_main_t *sm = &snat_main;
1181 snat_static_mapping_t *m;
1182 snat_static_map_resolve_t *rp;
1184 vlib_cli_output (vm, "NAT44 static mappings:");
1185 pool_foreach (m, sm->static_mappings)
1187 vlib_cli_output (vm, " %U", format_snat_static_mapping, m);
1189 vec_foreach (rp, sm->to_resolve)
1190 vlib_cli_output (vm, " %U", format_snat_static_map_to_resolve, rp);
1195 static clib_error_t *
1196 snat_add_interface_address_command_fn (vlib_main_t * vm,
1197 unformat_input_t * input,
1198 vlib_cli_command_t * cmd)
1200 snat_main_t *sm = &snat_main;
1201 unformat_input_t _line_input, *line_input = &_line_input;
1205 clib_error_t *error = 0;
1208 /* Get a line of input. */
1209 if (!unformat_user (input, unformat_line_input, line_input))
1212 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1214 if (unformat (line_input, "%U", unformat_vnet_sw_interface,
1215 sm->vnet_main, &sw_if_index))
1217 else if (unformat (line_input, "twice-nat"))
1219 else if (unformat (line_input, "del"))
1223 error = clib_error_return (0, "unknown input '%U'",
1224 format_unformat_error, line_input);
1229 rv = snat_add_interface_address (sm, sw_if_index, is_del, twice_nat);
1237 error = clib_error_return (0, "snat_add_interface_address returned %d",
1243 unformat_free (line_input);
1248 static clib_error_t *
1249 nat44_show_interface_address_command_fn (vlib_main_t * vm,
1250 unformat_input_t * input,
1251 vlib_cli_command_t * cmd)
1253 snat_main_t *sm = &snat_main;
1254 vnet_main_t *vnm = vnet_get_main ();
1257 vlib_cli_output (vm, "NAT44 pool address interfaces:");
1258 vec_foreach (sw_if_index, sm->auto_add_sw_if_indices)
1260 vlib_cli_output (vm, " %U", format_vnet_sw_if_index_name, vnm,
1263 vlib_cli_output (vm, "NAT44 twice-nat pool address interfaces:");
1264 vec_foreach (sw_if_index, sm->auto_add_sw_if_indices_twice_nat)
1266 vlib_cli_output (vm, " %U", format_vnet_sw_if_index_name, vnm,
1273 static clib_error_t *
1274 nat44_show_sessions_command_fn (vlib_main_t * vm, unformat_input_t * input,
1275 vlib_cli_command_t * cmd)
1277 unformat_input_t _line_input, *line_input = &_line_input;
1278 clib_error_t *error = 0;
1279 snat_main_per_thread_data_t *tsm;
1280 snat_main_t *sm = &snat_main;
1284 if (!unformat_user (input, unformat_line_input, line_input))
1287 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1289 error = clib_error_return (0, "unknown input '%U'",
1290 format_unformat_error, line_input);
1293 unformat_free (line_input);
1296 vlib_cli_output (vm, "NAT44 ED sessions:");
1298 vec_foreach_index (i, sm->per_thread_data)
1300 tsm = vec_elt_at_index (sm->per_thread_data, i);
1302 vlib_cli_output (vm, "-------- thread %d %s: %d sessions --------\n",
1303 i, vlib_worker_threads[i].name,
1304 pool_elts (tsm->sessions));
1307 pool_foreach (s, tsm->sessions)
1309 vlib_cli_output (vm, " %U\n", format_snat_session, tsm, s);
1315 static clib_error_t *
1316 nat44_set_session_limit_command_fn (vlib_main_t * vm,
1317 unformat_input_t * input,
1318 vlib_cli_command_t * cmd)
1320 unformat_input_t _line_input, *line_input = &_line_input;
1321 clib_error_t *error = 0;
1323 u32 session_limit = 0, vrf_id = 0;
1325 /* Get a line of input. */
1326 if (!unformat_user (input, unformat_line_input, line_input))
1329 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1331 if (unformat (line_input, "%u", &session_limit))
1333 else if (unformat (line_input, "vrf %u", &vrf_id))
1337 error = clib_error_return (0, "unknown input '%U'",
1338 format_unformat_error, line_input);
1344 error = clib_error_return (0, "missing value of session limit");
1345 else if (nat44_update_session_limit (session_limit, vrf_id))
1346 error = clib_error_return (0, "nat44_set_session_limit failed");
1349 unformat_free (line_input);
1354 static clib_error_t *
1355 nat44_del_session_command_fn (vlib_main_t * vm,
1356 unformat_input_t * input,
1357 vlib_cli_command_t * cmd)
1359 snat_main_t *sm = &snat_main;
1360 unformat_input_t _line_input, *line_input = &_line_input;
1361 u32 port = 0, eh_port = 0, vrf_id = sm->outside_vrf_id;
1362 clib_error_t *error = 0;
1363 ip4_address_t addr, eh_addr;
1364 nat_protocol_t proto;
1368 /* Get a line of input. */
1369 if (!unformat_user (input, unformat_line_input, line_input))
1372 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1375 (line_input, "%U:%u %U", unformat_ip4_address, &addr, &port,
1376 unformat_nat_protocol, &proto))
1378 else if (unformat (line_input, "in"))
1381 vrf_id = sm->inside_vrf_id;
1383 else if (unformat (line_input, "out"))
1386 vrf_id = sm->outside_vrf_id;
1388 else if (unformat (line_input, "vrf %u", &vrf_id))
1390 else if (unformat (line_input, "external-host %U:%u",
1391 unformat_ip4_address, &eh_addr, &eh_port))
1395 error = clib_error_return (0, "unknown input '%U'",
1396 format_unformat_error, line_input);
1402 nat44_del_ed_session (sm, &addr, clib_host_to_net_u16 (port), &eh_addr,
1403 clib_host_to_net_u16 (eh_port),
1404 nat_proto_to_ip_proto (proto), vrf_id, is_in);
1412 error = clib_error_return (0, "nat44_del_session returned %d", rv);
1417 unformat_free (line_input);
1422 static clib_error_t *
1423 snat_forwarding_set_command_fn (vlib_main_t * vm,
1424 unformat_input_t * input,
1425 vlib_cli_command_t * cmd)
1427 snat_main_t *sm = &snat_main;
1428 unformat_input_t _line_input, *line_input = &_line_input;
1429 u8 forwarding_enable;
1430 u8 forwarding_enable_set = 0;
1431 clib_error_t *error = 0;
1433 /* Get a line of input. */
1434 if (!unformat_user (input, unformat_line_input, line_input))
1435 return clib_error_return (0, "'enable' or 'disable' expected");
1437 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1439 if (!forwarding_enable_set && unformat (line_input, "enable"))
1441 forwarding_enable = 1;
1442 forwarding_enable_set = 1;
1444 else if (!forwarding_enable_set && unformat (line_input, "disable"))
1446 forwarding_enable = 0;
1447 forwarding_enable_set = 1;
1451 error = clib_error_return (0, "unknown input '%U'",
1452 format_unformat_error, line_input);
1457 if (!forwarding_enable_set)
1459 error = clib_error_return (0, "'enable' or 'disable' expected");
1463 sm->forwarding_enabled = forwarding_enable;
1466 unformat_free (line_input);
1471 static clib_error_t *
1472 set_timeout_command_fn (vlib_main_t * vm,
1473 unformat_input_t * input, vlib_cli_command_t * cmd)
1475 snat_main_t *sm = &snat_main;
1476 unformat_input_t _line_input, *line_input = &_line_input;
1477 clib_error_t *error = 0;
1479 /* Get a line of input. */
1480 if (!unformat_user (input, unformat_line_input, line_input))
1483 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1485 if (unformat (line_input, "udp %u", &sm->timeouts.udp));
1486 else if (unformat (line_input, "tcp-established %u",
1487 &sm->timeouts.tcp.established));
1488 else if (unformat (line_input, "tcp-transitory %u",
1489 &sm->timeouts.tcp.transitory));
1490 else if (unformat (line_input, "icmp %u", &sm->timeouts.icmp));
1491 else if (unformat (line_input, "reset"))
1492 nat_reset_timeouts (&sm->timeouts);
1495 error = clib_error_return (0, "unknown input '%U'",
1496 format_unformat_error, line_input);
1501 unformat_free (line_input);
1505 static clib_error_t *
1506 nat_show_timeouts_command_fn (vlib_main_t * vm,
1507 unformat_input_t * input,
1508 vlib_cli_command_t * cmd)
1510 snat_main_t *sm = &snat_main;
1512 vlib_cli_output (vm, "udp timeout: %dsec", sm->timeouts.udp);
1513 vlib_cli_output (vm, "tcp-established timeout: %dsec",
1514 sm->timeouts.tcp.established);
1515 vlib_cli_output (vm, "tcp-transitory timeout: %dsec",
1516 sm->timeouts.tcp.transitory);
1517 vlib_cli_output (vm, "icmp timeout: %dsec", sm->timeouts.icmp);
1522 static clib_error_t *
1523 set_frame_queue_nelts_command_fn (vlib_main_t *vm, unformat_input_t *input,
1524 vlib_cli_command_t *cmd)
1526 unformat_input_t _line_input, *line_input = &_line_input;
1527 clib_error_t *error = 0;
1528 u32 frame_queue_nelts = 0;
1529 /* Get a line of input. */
1530 if (!unformat_user (input, unformat_line_input, line_input))
1532 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1534 if (unformat (line_input, "%u", &frame_queue_nelts))
1538 error = clib_error_return (0, "unknown input '%U'",
1539 format_unformat_error, line_input);
1543 if (!frame_queue_nelts)
1545 error = clib_error_return (0, "frame_queue_nelts cannot be zero");
1548 if (snat_set_frame_queue_nelts (frame_queue_nelts) != 0)
1550 error = clib_error_return (0, "snat_set_frame_queue_nelts failed");
1554 unformat_free (line_input);
1560 * @cliexstart{nat44 enable}
1561 * Enable nat44 plugin
1562 * To enable nat44, use:
1563 * vpp# nat44 enable sessions <n>
1564 * To enable nat44 static mapping only, use:
1565 * vpp# nat44 enable sessions <n> static-mapping
1566 * To enable nat44 static mapping with connection tracking, use:
1567 * vpp# nat44 enable sessions <n> static-mapping connection-tracking
1568 * To set inside-vrf outside-vrf, use:
1569 * vpp# nat44 enable sessions <n> inside-vrf <id> outside-vrf <id>
1572 VLIB_CLI_COMMAND (nat44_enable_command, static) = {
1573 .path = "nat44 enable",
1575 "nat44 enable sessions <max-number> [static-mappig-only "
1576 "[connection-tracking]] [inside-vrf <vrf-id>] [outside-vrf <vrf-id>]",
1577 .function = nat44_enable_command_fn,
1582 * @cliexstart{nat44 disable}
1583 * Disable nat44 plugin
1584 * To disable nat44, use:
1585 * vpp# nat44 disable
1588 VLIB_CLI_COMMAND (nat44_disable_command, static) = {
1589 .path = "nat44 disable",
1590 .short_help = "nat44 disable",
1591 .function = nat44_disable_command_fn,
1596 * @cliexstart{set snat workers}
1597 * Set NAT workers if 2 or more workers available, use:
1598 * vpp# set snat workers 0-2,5
1601 VLIB_CLI_COMMAND (set_workers_command, static) = {
1602 .path = "set nat workers",
1603 .function = set_workers_command_fn,
1604 .short_help = "set nat workers <workers-list>",
1609 * @cliexstart{show nat workers}
1611 * vpp# show nat workers:
1617 VLIB_CLI_COMMAND (nat_show_workers_command, static) = {
1618 .path = "show nat workers",
1619 .short_help = "show nat workers",
1620 .function = nat_show_workers_commnad_fn,
1625 * @cliexstart{set nat timeout}
1626 * Set values of timeouts for NAT sessions (in seconds), use:
1627 * vpp# set nat timeout udp 120 tcp-established 7500 tcp-transitory 250 icmp 90
1628 * To reset default values use:
1629 * vpp# set nat timeout reset
1632 VLIB_CLI_COMMAND (set_timeout_command, static) = {
1633 .path = "set nat timeout",
1634 .function = set_timeout_command_fn,
1636 "set nat timeout [udp <sec> | tcp-established <sec> "
1637 "tcp-transitory <sec> | icmp <sec> | reset]",
1642 * @cliexstart{show nat timeouts}
1643 * Show values of timeouts for NAT sessions.
1644 * vpp# show nat timeouts
1645 * udp timeout: 300sec
1646 * tcp-established timeout: 7440sec
1647 * tcp-transitory timeout: 240sec
1648 * icmp timeout: 60sec
1651 VLIB_CLI_COMMAND (nat_show_timeouts_command, static) = {
1652 .path = "show nat timeouts",
1653 .short_help = "show nat timeouts",
1654 .function = nat_show_timeouts_command_fn,
1659 * @cliexstart{set nat frame-queue-nelts}
1660 * Set number of worker handoff frame queue elements.
1663 VLIB_CLI_COMMAND (set_frame_queue_nelts_command, static) = {
1664 .path = "set nat frame-queue-nelts",
1665 .function = set_frame_queue_nelts_command_fn,
1666 .short_help = "set nat frame-queue-nelts <number>",
1671 * @cliexstart{nat set logging level}
1672 * To set NAT logging level use:
1673 * Set nat logging level
1676 VLIB_CLI_COMMAND (snat_set_log_level_command, static) = {
1677 .path = "nat set logging level",
1678 .function = snat_set_log_level_command_fn,
1679 .short_help = "nat set logging level <level>",
1684 * @cliexstart{snat ipfix logging}
1685 * To enable NAT IPFIX logging use:
1686 * vpp# nat ipfix logging
1687 * To set IPFIX exporter use:
1688 * vpp# set ipfix exporter collector 10.10.10.3 src 10.10.10.1
1691 VLIB_CLI_COMMAND (snat_ipfix_logging_enable_disable_command, static) = {
1692 .path = "nat ipfix logging",
1693 .function = snat_ipfix_logging_enable_disable_command_fn,
1694 .short_help = "nat ipfix logging [domain <domain-id>] [src-port <port>] [disable]",
1699 * @cliexstart{nat mss-clamping}
1700 * Set TCP MSS rewriting configuration
1701 * To enable TCP MSS rewriting use:
1702 * vpp# nat mss-clamping 1452
1703 * To disbale TCP MSS rewriting use:
1704 * vpp# nat mss-clamping disable
1707 VLIB_CLI_COMMAND (nat_set_mss_clamping_command, static) = {
1708 .path = "nat mss-clamping",
1709 .short_help = "nat mss-clamping <mss-value>|disable",
1710 .function = nat_set_mss_clamping_command_fn,
1715 * @cliexstart{show nat mss-clamping}
1716 * Show TCP MSS rewriting configuration
1719 VLIB_CLI_COMMAND (nat_show_mss_clamping_command, static) = {
1720 .path = "show nat mss-clamping",
1721 .short_help = "show nat mss-clamping",
1722 .function = nat_show_mss_clamping_command_fn,
1727 * @cliexstart{show nat44 hash tables}
1728 * Show NAT44 hash tables
1731 VLIB_CLI_COMMAND (nat44_show_hash, static) = {
1732 .path = "show nat44 hash tables",
1733 .short_help = "show nat44 hash tables [detail|verbose]",
1734 .function = nat44_show_hash_command_fn,
1739 * @cliexstart{nat44 add address}
1740 * Add/delete NAT44 pool address.
1741 * To add NAT44 pool address use:
1742 * vpp# nat44 add address 172.16.1.3
1743 * vpp# nat44 add address 172.16.2.2 - 172.16.2.24
1744 * To add NAT44 pool address for specific tenant (identified by VRF id) use:
1745 * vpp# nat44 add address 172.16.1.3 tenant-vrf 10
1748 VLIB_CLI_COMMAND (add_address_command, static) = {
1749 .path = "nat44 add address",
1750 .short_help = "nat44 add address <ip4-range-start> [- <ip4-range-end>] "
1751 "[tenant-vrf <vrf-id>] [twice-nat] [del]",
1752 .function = add_address_command_fn,
1757 * @cliexstart{show nat44 summary}
1758 * Show NAT44 summary
1759 * vpp# show nat44 summary
1762 VLIB_CLI_COMMAND (nat44_show_summary_command, static) = {
1763 .path = "show nat44 summary",
1764 .short_help = "show nat44 summary",
1765 .function = nat44_show_summary_command_fn,
1770 * @cliexstart{show nat44 addresses}
1771 * Show NAT44 pool addresses.
1772 * vpp# show nat44 addresses
1773 * NAT44 pool addresses:
1775 * tenant VRF independent
1784 * NAT44 twice-nat pool addresses:
1786 * tenant VRF independent
1792 VLIB_CLI_COMMAND (nat44_show_addresses_command, static) = {
1793 .path = "show nat44 addresses",
1794 .short_help = "show nat44 addresses",
1795 .function = nat44_show_addresses_command_fn,
1800 * @cliexstart{set interface nat44}
1801 * Enable/disable NAT44 feature on the interface.
1802 * To enable NAT44 feature with local network interface use:
1803 * vpp# set interface nat44 in GigabitEthernet0/8/0
1804 * To enable NAT44 feature with external network interface use:
1805 * vpp# set interface nat44 out GigabitEthernet0/a/0
1808 VLIB_CLI_COMMAND (set_interface_snat_command, static) = {
1809 .path = "set interface nat44",
1810 .function = snat_feature_command_fn,
1811 .short_help = "set interface nat44 in <intfc> out <intfc> [output-feature] "
1817 * @cliexstart{show nat44 interfaces}
1818 * Show interfaces with NAT44 feature.
1819 * vpp# show nat44 interfaces
1821 * GigabitEthernet0/8/0 in
1822 * GigabitEthernet0/a/0 out
1825 VLIB_CLI_COMMAND (nat44_show_interfaces_command, static) = {
1826 .path = "show nat44 interfaces",
1827 .short_help = "show nat44 interfaces",
1828 .function = nat44_show_interfaces_command_fn,
1833 * @cliexstart{nat44 add static mapping}
1834 * Static mapping allows hosts on the external network to initiate connection
1835 * to to the local network host.
1836 * To create static mapping between local host address 10.0.0.3 port 6303 and
1837 * external address 4.4.4.4 port 3606 for TCP protocol use:
1838 * vpp# nat44 add static mapping tcp local 10.0.0.3 6303 external 4.4.4.4 3606
1839 * If not runnig "static mapping only" NAT plugin mode use before:
1840 * vpp# nat44 add address 4.4.4.4
1841 * To create address only static mapping between local and external address use:
1842 * vpp# nat44 add static mapping local 10.0.0.3 external 4.4.4.4
1843 * To create ICMP static mapping between local and external with ICMP echo
1844 * identifier 10 use:
1845 * vpp# nat44 add static mapping icmp local 10.0.0.3 10 external 4.4.4.4 10
1846 * To force use of specific pool address, vrf independent
1847 * vpp# nat44 add static mapping local 10.0.0.2 1234 external 10.0.2.2 1234 twice-nat exact 10.0.1.2
1850 VLIB_CLI_COMMAND (add_static_mapping_command, static) = {
1851 .path = "nat44 add static mapping",
1852 .function = add_static_mapping_command_fn,
1854 "nat44 add static mapping tcp|udp|icmp local <addr> [<port|icmp-echo-id>] "
1855 "external <addr> [<port|icmp-echo-id>] [vrf <table-id>] [twice-nat|self-twice-nat] "
1856 "[out2in-only] [exact <pool-addr>] [del]",
1861 * @cliexstart{nat44 add identity mapping}
1862 * Identity mapping translate an IP address to itself.
1863 * To create identity mapping for address 10.0.0.3 port 6303 for TCP protocol
1865 * vpp# nat44 add identity mapping 10.0.0.3 tcp 6303
1866 * To create identity mapping for address 10.0.0.3 use:
1867 * vpp# nat44 add identity mapping 10.0.0.3
1868 * To create identity mapping for DHCP addressed interface use:
1869 * vpp# nat44 add identity mapping external GigabitEthernet0/a/0 tcp 3606
1872 VLIB_CLI_COMMAND (add_identity_mapping_command, static) = {
1873 .path = "nat44 add identity mapping",
1874 .function = add_identity_mapping_command_fn,
1875 .short_help = "nat44 add identity mapping <ip4-addr>|external <interface> "
1876 "[<protocol> <port>] [vrf <table-id>] [del]",
1881 * @cliexstart{nat44 add load-balancing static mapping}
1882 * Service load balancing using NAT44
1883 * To add static mapping with load balancing for service with external IP
1884 * address 1.2.3.4 and TCP port 80 and mapped to 2 local servers
1885 * 10.100.10.10:8080 and 10.100.10.20:8080 with probability 80% resp. 20% use:
1886 * vpp# nat44 add load-balancing static mapping protocol tcp external 1.2.3.4:80 local 10.100.10.10:8080 probability 80 local 10.100.10.20:8080 probability 20
1889 VLIB_CLI_COMMAND (add_lb_static_mapping_command, static) = {
1890 .path = "nat44 add load-balancing static mapping",
1891 .function = add_lb_static_mapping_command_fn,
1893 "nat44 add load-balancing static mapping protocol tcp|udp "
1894 "external <addr>:<port> local <addr>:<port> [vrf <table-id>] "
1895 "probability <n> [twice-nat|self-twice-nat] [out2in-only] "
1896 "[affinity <timeout-seconds>] [del]",
1901 * @cliexstart{nat44 add load-balancing static mapping}
1902 * Modify service load balancing using NAT44
1903 * To add new back-end server 10.100.10.30:8080 for service load balancing
1904 * static mapping with external IP address 1.2.3.4 and TCP port 80 use:
1905 * vpp# nat44 add load-balancing back-end protocol tcp external 1.2.3.4:80 local 10.100.10.30:8080 probability 25
1908 VLIB_CLI_COMMAND (add_lb_backend_command, static) = {
1909 .path = "nat44 add load-balancing back-end",
1910 .function = add_lb_backend_command_fn,
1912 "nat44 add load-balancing back-end protocol tcp|udp "
1913 "external <addr>:<port> local <addr>:<port> [vrf <table-id>] "
1914 "probability <n> [del]",
1919 * @cliexstart{show nat44 static mappings}
1920 * Show NAT44 static mappings.
1921 * vpp# show nat44 static mappings
1922 * NAT44 static mappings:
1923 * local 10.0.0.3 external 4.4.4.4 vrf 0
1924 * tcp local 192.168.0.4:6303 external 4.4.4.3:3606 vrf 0
1925 * tcp vrf 0 external 1.2.3.4:80 out2in-only
1926 * local 10.100.10.10:8080 probability 80
1927 * local 10.100.10.20:8080 probability 20
1928 * tcp local 10.100.3.8:8080 external 169.10.10.1:80 vrf 0 twice-nat
1929 * tcp local 10.0.0.10:3603 external GigabitEthernet0/a/0:6306 vrf 10
1932 VLIB_CLI_COMMAND (nat44_show_static_mappings_command, static) = {
1933 .path = "show nat44 static mappings",
1934 .short_help = "show nat44 static mappings",
1935 .function = nat44_show_static_mappings_command_fn,
1940 * @cliexstart{nat44 add interface address}
1941 * Use NAT44 pool address from specific interfce
1942 * To add NAT44 pool address from specific interface use:
1943 * vpp# nat44 add interface address GigabitEthernet0/8/0
1946 VLIB_CLI_COMMAND (snat_add_interface_address_command, static) = {
1947 .path = "nat44 add interface address",
1948 .short_help = "nat44 add interface address <interface> [twice-nat] [del]",
1949 .function = snat_add_interface_address_command_fn,
1954 * @cliexstart{show nat44 interface address}
1955 * Show NAT44 pool address interfaces
1956 * vpp# show nat44 interface address
1957 * NAT44 pool address interfaces:
1958 * GigabitEthernet0/a/0
1959 * NAT44 twice-nat pool address interfaces:
1960 * GigabitEthernet0/8/0
1963 VLIB_CLI_COMMAND (nat44_show_interface_address_command, static) = {
1964 .path = "show nat44 interface address",
1965 .short_help = "show nat44 interface address",
1966 .function = nat44_show_interface_address_command_fn,
1971 * @cliexstart{show nat44 sessions}
1972 * Show NAT44 sessions.
1975 VLIB_CLI_COMMAND (nat44_show_sessions_command, static) = {
1976 .path = "show nat44 sessions",
1977 .short_help = "show nat44 sessions [detail|metrics]",
1978 .function = nat44_show_sessions_command_fn,
1983 * @cliexstart{set nat44 session limit}
1984 * Set NAT44 session limit.
1987 VLIB_CLI_COMMAND (nat44_set_session_limit_command, static) = {
1988 .path = "set nat44 session limit",
1989 .short_help = "set nat44 session limit <limit> [vrf <table-id>]",
1990 .function = nat44_set_session_limit_command_fn,
1995 * @cliexstart{nat44 del session}
1996 * To administratively delete NAT44 session by inside address and port use:
1997 * vpp# nat44 del session in 10.0.0.3:6303 tcp
1998 * To administratively delete NAT44 session by outside address and port use:
1999 * vpp# nat44 del session out 1.0.0.3:6033 udp
2002 VLIB_CLI_COMMAND (nat44_del_session_command, static) = {
2003 .path = "nat44 del session",
2004 .short_help = "nat44 del session in|out <addr>:<port> tcp|udp|icmp [vrf <id>] [external-host <addr>:<port>]",
2005 .function = nat44_del_session_command_fn,
2010 * @cliexstart{nat44 forwarding}
2011 * Enable or disable forwarding
2012 * Forward packets which don't match existing translation
2013 * or static mapping instead of dropping them.
2014 * To enable forwarding, use:
2015 * vpp# nat44 forwarding enable
2016 * To disable forwarding, use:
2017 * vpp# nat44 forwarding disable
2020 VLIB_CLI_COMMAND (snat_forwarding_set_command, static) = {
2021 .path = "nat44 forwarding",
2022 .short_help = "nat44 forwarding enable|disable",
2023 .function = snat_forwarding_set_command_fn,
2027 * fd.io coding-style-patch-verification: ON
2030 * eval: (c-set-style "gnu")