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>
30 #define NAT44_ED_EXPECTED_ARGUMENT "expected required argument(s)"
33 nat44_ed_enable_disable_command_fn (vlib_main_t *vm, unformat_input_t *input,
34 vlib_cli_command_t *cmd)
36 snat_main_t *sm = &snat_main;
37 unformat_input_t _line_input, *line_input = &_line_input;
38 clib_error_t *error = 0;
40 nat44_config_t c = { 0 };
41 u8 enable_set = 0, enable = 0, mode_set = 0;
43 if (!unformat_user (input, unformat_line_input, line_input))
44 return clib_error_return (0, NAT44_ED_EXPECTED_ARGUMENT);
46 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
48 if (!mode_set && unformat (line_input, "static-mapping-only"))
51 c.static_mapping_only = 1;
52 if (unformat (line_input, "connection-tracking"))
54 c.connection_tracking = 1;
57 else if (unformat (line_input, "inside-vrf %u", &c.inside_vrf));
58 else if (unformat (line_input, "outside-vrf %u", &c.outside_vrf));
59 else if (unformat (line_input, "sessions %u", &c.sessions));
63 if (unformat (line_input, "disable"))
65 else if (unformat (line_input, "enable"))
70 error = clib_error_return (0, "unknown input '%U'",
71 format_unformat_error, line_input);
78 error = clib_error_return (0, "expected enable | disable");
86 error = clib_error_return (0, "already enabled");
90 if (nat44_plugin_enable (c) != 0)
91 error = clib_error_return (0, "enable failed");
97 error = clib_error_return (0, "already disabled");
101 if (nat44_plugin_disable () != 0)
102 error = clib_error_return (0, "disable failed");
106 unformat_free (line_input);
110 static clib_error_t *
111 set_workers_command_fn (vlib_main_t * vm,
112 unformat_input_t * input, vlib_cli_command_t * cmd)
114 unformat_input_t _line_input, *line_input = &_line_input;
117 clib_error_t *error = 0;
119 /* Get a line of input. */
120 if (!unformat_user (input, unformat_line_input, line_input))
121 return clib_error_return (0, NAT44_ED_EXPECTED_ARGUMENT);
123 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
125 if (unformat (line_input, "%U", unformat_bitmap_list, &bitmap))
129 error = clib_error_return (0, "unknown input '%U'",
130 format_unformat_error, line_input);
137 error = clib_error_return (0, "List of workers must be specified.");
141 rv = snat_set_workers (bitmap);
143 clib_bitmap_free (bitmap);
147 case VNET_API_ERROR_INVALID_WORKER:
148 error = clib_error_return (0, "Invalid worker(s).");
150 case VNET_API_ERROR_FEATURE_DISABLED:
151 error = clib_error_return (0,
152 "Supported only if 2 or more workes available.");
159 unformat_free (line_input);
164 static clib_error_t *
165 nat_show_workers_command_fn (vlib_main_t *vm, unformat_input_t *input,
166 vlib_cli_command_t *cmd)
168 snat_main_t *sm = &snat_main;
171 if (sm->num_workers > 1)
173 vlib_cli_output (vm, "%d workers", vec_len (sm->workers));
174 vec_foreach (worker, sm->workers)
176 vlib_worker_thread_t *w =
177 vlib_worker_threads + *worker + sm->first_worker_index;
178 vlib_cli_output (vm, " %s", w->name);
185 static clib_error_t *
186 snat_set_log_level_command_fn (vlib_main_t * vm,
187 unformat_input_t * input,
188 vlib_cli_command_t * cmd)
190 unformat_input_t _line_input, *line_input = &_line_input;
191 snat_main_t *sm = &snat_main;
192 u8 log_level = NAT_LOG_NONE;
193 clib_error_t *error = 0;
195 /* Get a line of input. */
196 if (!unformat_user (input, unformat_line_input, line_input))
197 return clib_error_return (0, NAT44_ED_EXPECTED_ARGUMENT);
199 if (!unformat (line_input, "%d", &log_level))
201 error = clib_error_return (0, "unknown input '%U'",
202 format_unformat_error, line_input);
205 if (log_level > NAT_LOG_DEBUG)
207 error = clib_error_return (0, "unknown logging level '%d'", log_level);
210 sm->log_level = log_level;
213 unformat_free (line_input);
218 static clib_error_t *
219 snat_ipfix_logging_enable_disable_command_fn (vlib_main_t * vm,
220 unformat_input_t * input,
221 vlib_cli_command_t * cmd)
223 unformat_input_t _line_input, *line_input = &_line_input;
224 clib_error_t *error = 0;
226 u32 domain_id = 0, src_port = 0;
227 u8 enable_set = 0, enable = 0;
229 if (!unformat_user (input, unformat_line_input, line_input))
230 return clib_error_return (0, NAT44_ED_EXPECTED_ARGUMENT);
232 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
234 if (unformat (line_input, "domain %d", &domain_id))
236 else if (unformat (line_input, "src-port %d", &src_port))
238 else if (!enable_set)
241 if (unformat (line_input, "disable"))
243 else if (unformat (line_input, "enable"))
248 error = clib_error_return (0, "unknown input '%U'",
249 format_unformat_error, line_input);
256 error = clib_error_return (0, "expected enable | disable");
260 if (nat_ipfix_logging_enable_disable (enable, domain_id, (u16) src_port))
262 error = clib_error_return (0, "ipfix logging enable failed");
267 unformat_free (line_input);
272 static clib_error_t *
273 nat44_show_hash_command_fn (vlib_main_t * vm, unformat_input_t * input,
274 vlib_cli_command_t * cmd)
276 snat_main_t *sm = &snat_main;
277 nat_affinity_main_t *nam = &nat_affinity_main;
281 if (unformat (input, "detail"))
283 else if (unformat (input, "verbose"))
286 vlib_cli_output (vm, "%U", format_bihash_16_8, &sm->flow_hash, verbose);
287 vec_foreach_index (i, sm->per_thread_data)
289 vlib_cli_output (vm, "-------- thread %d %s --------\n",
290 i, vlib_worker_threads[i].name);
291 vlib_cli_output (vm, "%U", format_bihash_16_8, &sm->flow_hash, verbose);
294 vlib_cli_output (vm, "%U", format_bihash_16_8, &nam->affinity_hash, verbose);
296 vlib_cli_output (vm, "-------- hash table parameters --------\n");
297 vlib_cli_output (vm, "translation buckets: %u", sm->translation_buckets);
301 static clib_error_t *
302 nat_set_mss_clamping_command_fn (vlib_main_t * vm, unformat_input_t * input,
303 vlib_cli_command_t * cmd)
305 unformat_input_t _line_input, *line_input = &_line_input;
306 snat_main_t *sm = &snat_main;
307 clib_error_t *error = 0;
310 /* Get a line of input. */
311 if (!unformat_user (input, unformat_line_input, line_input))
312 return clib_error_return (0, NAT44_ED_EXPECTED_ARGUMENT);
314 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
316 if (unformat (line_input, "disable"))
317 sm->mss_clamping = 0;
318 else if (unformat (line_input, "%d", &mss))
319 sm->mss_clamping = (u16) mss;
322 error = clib_error_return (0, "unknown input '%U'",
323 format_unformat_error, line_input);
329 unformat_free (line_input);
334 static clib_error_t *
335 nat_show_mss_clamping_command_fn (vlib_main_t * vm, unformat_input_t * input,
336 vlib_cli_command_t * cmd)
338 snat_main_t *sm = &snat_main;
340 if (sm->mss_clamping)
341 vlib_cli_output (vm, "mss-clamping %d", sm->mss_clamping);
343 vlib_cli_output (vm, "mss-clamping disabled");
348 static clib_error_t *
349 add_address_command_fn (vlib_main_t * vm,
350 unformat_input_t * input, vlib_cli_command_t * cmd)
352 unformat_input_t _line_input, *line_input = &_line_input;
353 snat_main_t *sm = &snat_main;
354 ip4_address_t start_addr, end_addr, this_addr;
355 u32 start_host_order, end_host_order;
360 clib_error_t *error = 0;
363 /* Get a line of input. */
364 if (!unformat_user (input, unformat_line_input, line_input))
365 return clib_error_return (0, NAT44_ED_EXPECTED_ARGUMENT);
367 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
369 if (unformat (line_input, "%U - %U",
370 unformat_ip4_address, &start_addr,
371 unformat_ip4_address, &end_addr))
373 else if (unformat (line_input, "tenant-vrf %u", &vrf_id))
375 else if (unformat (line_input, "%U", unformat_ip4_address, &start_addr))
376 end_addr = start_addr;
377 else if (unformat (line_input, "twice-nat"))
379 else if (unformat (line_input, "del"))
383 error = clib_error_return (0, "unknown input '%U'",
384 format_unformat_error, line_input);
389 if (sm->static_mapping_only)
391 error = clib_error_return (0, "static mapping only mode");
395 start_host_order = clib_host_to_net_u32 (start_addr.as_u32);
396 end_host_order = clib_host_to_net_u32 (end_addr.as_u32);
398 if (end_host_order < start_host_order)
400 error = clib_error_return (0, "end address less than start address");
404 count = (end_host_order - start_host_order) + 1;
407 nat_log_info ("%U - %U, %d addresses...",
408 format_ip4_address, &start_addr,
409 format_ip4_address, &end_addr, count);
411 this_addr = start_addr;
413 for (i = 0; i < count; i++)
417 rv = nat44_ed_add_address (&this_addr, vrf_id, twice_nat);
421 rv = nat44_ed_del_address (this_addr, 0, twice_nat);
426 case VNET_API_ERROR_VALUE_EXIST:
427 error = clib_error_return (0, "NAT address already in use.");
429 case VNET_API_ERROR_NO_SUCH_ENTRY:
430 error = clib_error_return (0, "NAT address not exist.");
432 case VNET_API_ERROR_UNSPECIFIED:
434 clib_error_return (0, "NAT address used in static mapping.");
440 increment_v4_address (&this_addr);
444 unformat_free (line_input);
450 nat44_show_lru_summary (vlib_main_t * vm, snat_main_per_thread_data_t * tsm,
451 u64 now, u64 sess_timeout_time)
453 snat_main_t *sm = &snat_main;
454 dlist_elt_t *oldest_elt;
462 clib_dlist_remove_head (tsm->lru_pool, tsm->n##_lru_head_index); \
463 if (~0 != oldest_index) \
465 oldest_elt = pool_elt_at_index (tsm->lru_pool, oldest_index); \
466 s = pool_elt_at_index (tsm->sessions, oldest_elt->value); \
467 sess_timeout_time = \
468 s->last_heard + (f64) nat44_session_get_timeout (sm, s); \
469 vlib_cli_output (vm, d " LRU min session timeout %llu (now %llu)", \
470 sess_timeout_time, now); \
471 clib_dlist_addhead (tsm->lru_pool, tsm->n##_lru_head_index, \
474 _ (tcp_estab, "established tcp");
475 _ (tcp_trans, "transitory tcp");
477 _ (unk_proto, "unknown protocol");
483 static clib_error_t *
484 nat44_show_summary_command_fn (vlib_main_t * vm, unformat_input_t * input,
485 vlib_cli_command_t * cmd)
487 snat_main_per_thread_data_t *tsm;
488 snat_main_t *sm = &snat_main;
493 u64 now = vlib_time_now (vm);
494 u64 sess_timeout_time = 0;
496 u32 udp_sessions = 0;
497 u32 tcp_sessions = 0;
498 u32 icmp_sessions = 0;
499 u32 other_sessions = 0;
503 u32 transitory_wait_closed = 0;
504 u32 transitory_closed = 0;
509 for (fib = 0; fib < vec_len (sm->max_translations_per_fib); fib++)
510 vlib_cli_output (vm, "max translations per thread: %u fib %u",
511 sm->max_translations_per_fib[fib], fib);
513 if (sm->num_workers > 1)
515 vec_foreach (tsm, sm->per_thread_data)
517 pool_foreach (s, tsm->sessions)
519 sess_timeout_time = s->last_heard +
520 (f64) nat44_session_get_timeout (sm, s);
521 if (now >= sess_timeout_time)
526 case IP_PROTOCOL_ICMP:
529 case IP_PROTOCOL_TCP:
533 if (s->tcp_closed_timestamp)
535 if (now >= s->tcp_closed_timestamp)
541 ++transitory_wait_closed;
549 case IP_PROTOCOL_UDP:
557 nat44_show_lru_summary (vm, tsm, now, sess_timeout_time);
558 count += pool_elts (tsm->sessions);
563 tsm = vec_elt_at_index (sm->per_thread_data, sm->num_workers);
564 pool_foreach (s, tsm->sessions)
566 sess_timeout_time = s->last_heard +
567 (f64) nat44_session_get_timeout (sm, s);
568 if (now >= sess_timeout_time)
573 case IP_PROTOCOL_ICMP:
576 case IP_PROTOCOL_TCP:
580 if (s->tcp_closed_timestamp)
582 if (now >= s->tcp_closed_timestamp)
588 ++transitory_wait_closed;
596 case IP_PROTOCOL_UDP:
604 nat44_show_lru_summary (vm, tsm, now, sess_timeout_time);
605 count = pool_elts (tsm->sessions);
608 vlib_cli_output (vm, "total timed out sessions: %u", timed_out);
609 vlib_cli_output (vm, "total sessions: %u", count);
610 vlib_cli_output (vm, "total tcp sessions: %u", tcp_sessions);
611 vlib_cli_output (vm, "total tcp established sessions: %u", established);
612 vlib_cli_output (vm, "total tcp transitory sessions: %u", transitory);
613 vlib_cli_output (vm, "total tcp transitory (WAIT-CLOSED) sessions: %u",
614 transitory_wait_closed);
615 vlib_cli_output (vm, "total tcp transitory (CLOSED) sessions: %u",
617 vlib_cli_output (vm, "total udp sessions: %u", udp_sessions);
618 vlib_cli_output (vm, "total icmp sessions: %u", icmp_sessions);
619 vlib_cli_output (vm, "total other sessions: %u", other_sessions);
623 static clib_error_t *
624 nat44_show_addresses_command_fn (vlib_main_t * vm, unformat_input_t * input,
625 vlib_cli_command_t * cmd)
627 snat_main_t *sm = &snat_main;
630 vlib_cli_output (vm, "NAT44 pool addresses:");
631 vec_foreach (ap, sm->addresses)
633 vlib_cli_output (vm, "%U", format_ip4_address, &ap->addr);
634 if (ap->fib_index != ~0)
636 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");
641 if (ap->addr_len != ~0)
642 vlib_cli_output (vm, " synced with interface address");
644 vlib_cli_output (vm, "NAT44 twice-nat pool addresses:");
645 vec_foreach (ap, sm->twice_nat_addresses)
647 vlib_cli_output (vm, "%U", format_ip4_address, &ap->addr);
648 if (ap->fib_index != ~0)
649 vlib_cli_output (vm, " tenant VRF: %u",
650 fib_table_get(ap->fib_index, FIB_PROTOCOL_IP4)->ft_table_id);
652 vlib_cli_output (vm, " tenant VRF independent");
654 if (ap->addr_len != ~0)
655 vlib_cli_output (vm, " synced with interface address");
660 static clib_error_t *
661 snat_feature_command_fn (vlib_main_t * vm,
662 unformat_input_t * input, vlib_cli_command_t * cmd)
664 unformat_input_t _line_input, *line_input = &_line_input;
665 vnet_main_t *vnm = vnet_get_main ();
666 clib_error_t *error = 0;
668 u32 *inside_sw_if_indices = 0;
669 u32 *outside_sw_if_indices = 0;
670 u8 is_output_feature = 0;
671 int i, rv, is_del = 0;
675 /* Get a line of input. */
676 if (!unformat_user (input, unformat_line_input, line_input))
677 return clib_error_return (0, NAT44_ED_EXPECTED_ARGUMENT);
679 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
681 if (unformat (line_input, "in %U", unformat_vnet_sw_interface,
683 vec_add1 (inside_sw_if_indices, sw_if_index);
684 else if (unformat (line_input, "out %U", unformat_vnet_sw_interface,
686 vec_add1 (outside_sw_if_indices, sw_if_index);
687 else if (unformat (line_input, "output-feature"))
688 is_output_feature = 1;
689 else if (unformat (line_input, "del"))
693 error = clib_error_return (0, "unknown input '%U'",
694 format_unformat_error, line_input);
699 if (vec_len (inside_sw_if_indices))
701 for (i = 0; i < vec_len (inside_sw_if_indices); i++)
703 sw_if_index = inside_sw_if_indices[i];
704 if (is_output_feature)
708 rv = nat44_ed_del_output_interface (sw_if_index);
712 rv = nat44_ed_add_output_interface (sw_if_index);
716 error = clib_error_return (0, "%s %U failed",
717 is_del ? "del" : "add",
718 format_vnet_sw_if_index_name,
727 rv = nat44_ed_del_interface (sw_if_index, 1);
731 rv = nat44_ed_add_interface (sw_if_index, 1);
735 error = clib_error_return (0, "%s %U failed",
736 is_del ? "del" : "add",
737 format_vnet_sw_if_index_name,
745 if (vec_len (outside_sw_if_indices))
747 for (i = 0; i < vec_len (outside_sw_if_indices); i++)
749 sw_if_index = outside_sw_if_indices[i];
750 if (is_output_feature)
754 rv = nat44_ed_del_output_interface (sw_if_index);
758 rv = nat44_ed_add_output_interface (sw_if_index);
762 error = clib_error_return (0, "%s %U failed",
763 is_del ? "del" : "add",
764 format_vnet_sw_if_index_name,
773 rv = nat44_ed_del_interface (sw_if_index, 0);
777 rv = nat44_ed_add_interface (sw_if_index, 0);
781 error = clib_error_return (0, "%s %U failed",
782 is_del ? "del" : "add",
783 format_vnet_sw_if_index_name,
792 unformat_free (line_input);
793 vec_free (inside_sw_if_indices);
794 vec_free (outside_sw_if_indices);
799 static clib_error_t *
800 nat44_show_interfaces_command_fn (vlib_main_t * vm, unformat_input_t * input,
801 vlib_cli_command_t * cmd)
803 snat_main_t *sm = &snat_main;
805 vnet_main_t *vnm = vnet_get_main ();
807 vlib_cli_output (vm, "NAT44 interfaces:");
808 pool_foreach (i, sm->interfaces)
810 vlib_cli_output (vm, " %U %s", format_vnet_sw_if_index_name, vnm,
812 (nat44_ed_is_interface_inside (i) &&
813 nat44_ed_is_interface_outside (i)) ?
815 (nat44_ed_is_interface_inside (i) ? "in" : "out"));
818 pool_foreach (i, sm->output_feature_interfaces)
820 vlib_cli_output (vm, " %U output-feature %s",
821 format_vnet_sw_if_index_name, vnm, i->sw_if_index,
822 (nat44_ed_is_interface_inside (i) &&
823 nat44_ed_is_interface_outside (i)) ?
825 (nat44_ed_is_interface_inside (i) ? "in" : "out"));
831 static clib_error_t *
832 add_static_mapping_command_fn (vlib_main_t * vm,
833 unformat_input_t * input,
834 vlib_cli_command_t * cmd)
836 unformat_input_t _line_input, *line_input = &_line_input;
837 vnet_main_t *vnm = vnet_get_main ();
838 clib_error_t *error = 0;
839 ip4_address_t l_addr, e_addr, pool_addr;
840 u32 l_port = 0, e_port = 0, vrf_id = ~0;
841 u8 l_port_set = 0, e_port_set = 0;
844 u32 sw_if_index = ~0;
845 ip_protocol_t proto = 0;
847 if (!unformat_user (input, unformat_line_input, line_input))
848 return clib_error_return (0, NAT44_ED_EXPECTED_ARGUMENT);
850 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
852 if (unformat (line_input, "local %U %u", unformat_ip4_address, &l_addr,
858 if (unformat (line_input, "local %U", unformat_ip4_address, &l_addr))
860 else if (unformat (line_input, "external %U %u", unformat_ip4_address,
865 else if (unformat (line_input, "external %U", unformat_ip4_address,
868 else if (unformat (line_input, "external %U %u",
869 unformat_vnet_sw_interface, vnm, &sw_if_index,
872 flags |= NAT_SM_FLAG_SWITCH_ADDRESS;
875 else if (unformat (line_input, "external %U",
876 unformat_vnet_sw_interface, vnm, &sw_if_index))
878 flags |= NAT_SM_FLAG_SWITCH_ADDRESS;
880 else if (unformat (line_input, "exact %U", unformat_ip4_address,
883 flags |= NAT_SM_FLAG_EXACT_ADDRESS;
885 else if (unformat (line_input, "vrf %u", &vrf_id))
887 else if (unformat (line_input, "%U", unformat_ip_protocol, &proto))
889 else if (unformat (line_input, "self-twice-nat"))
891 flags |= NAT_SM_FLAG_SELF_TWICE_NAT;
893 else if (unformat (line_input, "twice-nat"))
895 flags |= NAT_SM_FLAG_TWICE_NAT;
897 else if (unformat (line_input, "out2in-only"))
899 flags |= NAT_SM_FLAG_OUT2IN_ONLY;
901 else if (unformat (line_input, "del"))
907 error = clib_error_return (0, "unknown input: '%U'",
908 format_unformat_error, line_input);
913 if (l_port_set != e_port_set)
915 error = clib_error_return (0, "Either both ports are set or none.");
921 flags |= NAT_SM_FLAG_ADDR_ONLY;
925 l_port = clib_host_to_net_u16 (l_port);
926 e_port = clib_host_to_net_u16 (e_port);
929 // TODO: specific pool_addr for both pool & twice nat pool ?
934 nat44_ed_add_static_mapping (l_addr, e_addr, l_port, e_port, proto,
935 vrf_id, sw_if_index, flags, pool_addr, 0);
939 rv = nat44_ed_del_static_mapping (l_addr, e_addr, l_port, e_port, proto,
940 vrf_id, sw_if_index, flags);
947 case VNET_API_ERROR_INVALID_VALUE:
948 error = clib_error_return (0, "External port already in use.");
950 case VNET_API_ERROR_NO_SUCH_ENTRY:
952 error = clib_error_return (0, "External address must be allocated.");
954 error = clib_error_return (0, "Mapping not exist.");
956 case VNET_API_ERROR_NO_SUCH_FIB:
957 error = clib_error_return (0, "No such VRF id.");
959 case VNET_API_ERROR_VALUE_EXIST:
960 error = clib_error_return (0, "Mapping already exist.");
967 unformat_free (line_input);
972 // TODO: either delete this bullshit or update it
973 static clib_error_t *
974 add_identity_mapping_command_fn (vlib_main_t * vm,
975 unformat_input_t * input,
976 vlib_cli_command_t * cmd)
978 unformat_input_t _line_input, *line_input = &_line_input;
979 vnet_main_t *vnm = vnet_get_main ();
980 clib_error_t *error = 0;
982 int rv, is_add = 1, port_set = 0;
983 u32 sw_if_index, port, flags, vrf_id = ~0;
984 ip_protocol_t proto = 0;
987 flags = NAT_SM_FLAG_IDENTITY_NAT;
989 /* Get a line of input. */
990 if (!unformat_user (input, unformat_line_input, line_input))
991 return clib_error_return (0, NAT44_ED_EXPECTED_ARGUMENT);
993 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
995 if (unformat (line_input, "%U", unformat_ip4_address, &addr))
997 else if (unformat (line_input, "external %U",
998 unformat_vnet_sw_interface, vnm, &sw_if_index))
1000 flags |= NAT_SM_FLAG_SWITCH_ADDRESS;
1002 else if (unformat (line_input, "vrf %u", &vrf_id))
1004 else if (unformat (line_input, "%U %u", unformat_ip_protocol, &proto,
1009 else if (unformat (line_input, "del"))
1015 error = clib_error_return (0, "unknown input: '%U'",
1016 format_unformat_error, line_input);
1023 flags |= NAT_SM_FLAG_ADDR_ONLY;
1027 port = clib_host_to_net_u16 (port);
1033 rv = nat44_ed_add_static_mapping (addr, addr, port, port, proto, vrf_id,
1034 sw_if_index, flags, addr, 0);
1038 rv = nat44_ed_del_static_mapping (addr, addr, port, port, proto, vrf_id,
1039 sw_if_index, flags);
1042 // TODO: fix returns
1046 case VNET_API_ERROR_INVALID_VALUE:
1047 error = clib_error_return (0, "External port already in use.");
1049 case VNET_API_ERROR_NO_SUCH_ENTRY:
1051 error = clib_error_return (0, "External address must be allocated.");
1053 error = clib_error_return (0, "Mapping not exist.");
1055 case VNET_API_ERROR_NO_SUCH_FIB:
1056 error = clib_error_return (0, "No such VRF id.");
1058 case VNET_API_ERROR_VALUE_EXIST:
1059 error = clib_error_return (0, "Mapping already exist.");
1066 unformat_free (line_input);
1071 static clib_error_t *
1072 add_lb_static_mapping_command_fn (vlib_main_t * vm,
1073 unformat_input_t * input,
1074 vlib_cli_command_t * cmd)
1076 unformat_input_t _line_input, *line_input = &_line_input;
1077 clib_error_t *error = 0;
1078 ip4_address_t l_addr, e_addr;
1079 u32 l_port = 0, e_port = 0, vrf_id = 0, probability = 0, affinity = 0;
1081 ip_protocol_t proto;
1082 nat44_lb_addr_port_t *locals = 0, local;
1086 /* Get a line of input. */
1087 if (!unformat_user (input, unformat_line_input, line_input))
1088 return clib_error_return (0, NAT44_ED_EXPECTED_ARGUMENT);
1090 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1092 if (unformat (line_input, "local %U:%u probability %u",
1093 unformat_ip4_address, &l_addr, &l_port, &probability))
1095 clib_memset (&local, 0, sizeof (local));
1096 local.addr = l_addr;
1097 local.port = (u16) l_port;
1098 local.probability = (u8) probability;
1099 vec_add1 (locals, local);
1101 else if (unformat (line_input, "local %U:%u vrf %u probability %u",
1102 unformat_ip4_address, &l_addr, &l_port, &vrf_id,
1105 clib_memset (&local, 0, sizeof (local));
1106 local.addr = l_addr;
1107 local.port = (u16) l_port;
1108 local.probability = (u8) probability;
1109 local.vrf_id = vrf_id;
1110 vec_add1 (locals, local);
1112 else if (unformat (line_input, "external %U:%u", unformat_ip4_address,
1115 else if (unformat (line_input, "protocol %U", unformat_ip_protocol,
1120 else if (unformat (line_input, "twice-nat"))
1122 flags |= NAT_SM_FLAG_TWICE_NAT;
1124 else if (unformat (line_input, "self-twice-nat"))
1126 flags |= NAT_SM_FLAG_SELF_TWICE_NAT;
1128 else if (unformat (line_input, "out2in-only"))
1130 flags |= NAT_SM_FLAG_OUT2IN_ONLY;
1132 else if (unformat (line_input, "del"))
1136 else if (unformat (line_input, "affinity %u", &affinity))
1140 error = clib_error_return (0, "unknown input: '%U'",
1141 format_unformat_error, line_input);
1146 if (vec_len (locals) < 2)
1148 error = clib_error_return (0, "at least two local must be set");
1154 error = clib_error_return (0, "missing protocol");
1160 rv = nat44_ed_add_lb_static_mapping (e_addr, (u16) e_port, proto, locals,
1161 flags, 0, affinity);
1165 rv = nat44_ed_del_lb_static_mapping (e_addr, (u16) e_port, proto, flags);
1170 case VNET_API_ERROR_INVALID_VALUE:
1171 error = clib_error_return (0, "External port already in use.");
1173 case VNET_API_ERROR_NO_SUCH_ENTRY:
1175 error = clib_error_return (0, "External address must be allocated.");
1177 error = clib_error_return (0, "Mapping not exist.");
1179 case VNET_API_ERROR_VALUE_EXIST:
1180 error = clib_error_return (0, "Mapping already exist.");
1187 unformat_free (line_input);
1193 static clib_error_t *
1194 add_lb_backend_command_fn (vlib_main_t * vm,
1195 unformat_input_t * input, vlib_cli_command_t * cmd)
1197 unformat_input_t _line_input, *line_input = &_line_input;
1198 clib_error_t *error = 0;
1199 ip4_address_t l_addr, e_addr;
1200 u32 l_port = 0, e_port = 0, vrf_id = 0, probability = 0;
1203 ip_protocol_t proto;
1206 /* Get a line of input. */
1207 if (!unformat_user (input, unformat_line_input, line_input))
1208 return clib_error_return (0, NAT44_ED_EXPECTED_ARGUMENT);
1210 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1212 if (unformat (line_input, "local %U:%u probability %u",
1213 unformat_ip4_address, &l_addr, &l_port, &probability))
1215 else if (unformat (line_input, "local %U:%u vrf %u probability %u",
1216 unformat_ip4_address, &l_addr, &l_port, &vrf_id,
1219 else if (unformat (line_input, "external %U:%u", unformat_ip4_address,
1222 else if (unformat (line_input, "protocol %U", unformat_ip_protocol,
1225 else if (unformat (line_input, "del"))
1229 error = clib_error_return (0, "unknown input: '%U'",
1230 format_unformat_error, line_input);
1235 if (!l_port || !e_port)
1237 error = clib_error_return (0, "local or external must be set");
1243 error = clib_error_return (0, "missing protocol");
1247 rv = nat44_ed_add_del_lb_static_mapping_local (
1248 e_addr, (u16) e_port, l_addr, l_port, proto, vrf_id, probability, is_add);
1252 case VNET_API_ERROR_INVALID_VALUE:
1253 error = clib_error_return (0, "External is not load-balancing static "
1256 case VNET_API_ERROR_NO_SUCH_ENTRY:
1257 error = clib_error_return (0, "Mapping or back-end not exist.");
1259 case VNET_API_ERROR_VALUE_EXIST:
1260 error = clib_error_return (0, "Back-end already exist.");
1262 case VNET_API_ERROR_UNSPECIFIED:
1263 error = clib_error_return (0, "At least two back-ends must remain");
1270 unformat_free (line_input);
1275 static clib_error_t *
1276 nat44_show_static_mappings_command_fn (vlib_main_t * vm,
1277 unformat_input_t * input,
1278 vlib_cli_command_t * cmd)
1280 snat_main_t *sm = &snat_main;
1281 snat_static_mapping_t *m;
1282 snat_static_map_resolve_t *rp;
1284 vlib_cli_output (vm, "NAT44 static mappings:");
1285 pool_foreach (m, sm->static_mappings)
1287 vlib_cli_output (vm, " %U", format_snat_static_mapping, m);
1289 vec_foreach (rp, sm->to_resolve)
1290 vlib_cli_output (vm, " %U", format_snat_static_map_to_resolve, rp);
1295 static clib_error_t *
1296 snat_add_interface_address_command_fn (vlib_main_t * vm,
1297 unformat_input_t * input,
1298 vlib_cli_command_t * cmd)
1300 unformat_input_t _line_input, *line_input = &_line_input;
1301 snat_main_t *sm = &snat_main;
1302 clib_error_t *error = 0;
1307 if (!unformat_user (input, unformat_line_input, line_input))
1308 return clib_error_return (0, NAT44_ED_EXPECTED_ARGUMENT);
1310 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1312 if (unformat (line_input, "%U", unformat_vnet_sw_interface,
1313 sm->vnet_main, &sw_if_index))
1315 else if (unformat (line_input, "twice-nat"))
1319 else if (unformat (line_input, "del"))
1325 error = clib_error_return (0, "unknown input '%U'",
1326 format_unformat_error, line_input);
1333 rv = nat44_ed_add_interface_address (sw_if_index, twice_nat);
1336 error = clib_error_return (0, "add address returned %d", rv);
1341 rv = nat44_ed_del_interface_address (sw_if_index, twice_nat);
1344 error = clib_error_return (0, "del address returned %d", rv);
1349 unformat_free (line_input);
1354 static clib_error_t *
1355 nat44_show_interface_address_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 vnet_main_t *vnm = vnet_get_main ();
1363 vlib_cli_output (vm, "NAT44 pool address interfaces:");
1364 vec_foreach (sw_if_index, sm->auto_add_sw_if_indices)
1366 vlib_cli_output (vm, " %U", format_vnet_sw_if_index_name, vnm,
1369 vlib_cli_output (vm, "NAT44 twice-nat pool address interfaces:");
1370 vec_foreach (sw_if_index, sm->auto_add_sw_if_indices_twice_nat)
1372 vlib_cli_output (vm, " %U", format_vnet_sw_if_index_name, vnm,
1379 static clib_error_t *
1380 nat44_show_sessions_command_fn (vlib_main_t * vm, unformat_input_t * input,
1381 vlib_cli_command_t * cmd)
1383 unformat_input_t _line_input, *line_input = &_line_input;
1384 clib_error_t *error = 0;
1385 snat_main_per_thread_data_t *tsm;
1386 snat_main_t *sm = &snat_main;
1387 ip4_address_t i2o_sa, i2o_da, o2i_sa, o2i_da;
1388 u8 filter_i2o_sa = 0, filter_i2o_da = 0;
1389 u8 filter_o2i_sa = 0, filter_o2i_da = 0;
1390 u16 i2o_sp, i2o_dp, o2i_sp, o2i_dp;
1391 u8 filter_i2o_sp = 0, filter_i2o_dp = 0;
1392 u8 filter_o2i_sp = 0, filter_o2i_dp = 0;
1393 ip_protocol_t proto;
1394 u8 filter_proto = 0;
1395 u8 had_input = 1, filtering = 0;
1396 int i = 0, showed_sessions;
1398 if (!unformat_user (input, unformat_line_input, line_input))
1404 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1406 if (unformat (line_input, "filter i2o saddr %U", unformat_ip4_address,
1408 filter_i2o_sa = filtering = 1;
1409 else if (unformat (line_input, "filter i2o daddr %U",
1410 unformat_ip4_address, &i2o_da))
1411 filter_i2o_da = filtering = 1;
1412 else if (unformat (line_input, "filter o2i saddr %U",
1413 unformat_ip4_address, &o2i_sa))
1414 filter_o2i_sa = filtering = 1;
1415 else if (unformat (line_input, "filter o2i daddr %U",
1416 unformat_ip4_address, &o2i_da))
1417 filter_o2i_da = filtering = 1;
1418 else if (unformat (line_input, "filter i2o sport %u", &i2o_sp))
1419 filter_i2o_sp = filtering = 1;
1420 else if (unformat (line_input, "filter i2o dport %u", &i2o_dp))
1421 filter_i2o_dp = filtering = 1;
1422 else if (unformat (line_input, "filter o2i sport %u", &o2i_sp))
1423 filter_o2i_sp = filtering = 1;
1424 else if (unformat (line_input, "filter o2i dport %u", &o2i_dp))
1425 filter_o2i_dp = filtering = 1;
1426 else if (unformat (line_input, "filter i2o proto %U",
1427 unformat_ip_protocol, &proto))
1428 filter_proto = filtering = 1;
1429 else if (unformat (line_input, "filter o2i proto %U",
1430 unformat_ip_protocol, &proto))
1431 filter_proto = filtering = 1;
1434 error = clib_error_return (0, "unknown input '%U'",
1435 format_unformat_error, line_input);
1441 vlib_cli_output (vm, "NAT44 ED sessions:");
1443 vec_foreach_index (i, sm->per_thread_data)
1445 tsm = vec_elt_at_index (sm->per_thread_data, i);
1447 vlib_cli_output (vm, "-------- thread %d %s: %d sessions --------\n",
1448 i, vlib_worker_threads[i].name,
1449 pool_elts (tsm->sessions));
1451 showed_sessions = 0;
1453 pool_foreach (s, tsm->sessions)
1457 if (filter_i2o_sa && i2o_sa.as_u32 != s->i2o.match.saddr.as_u32)
1459 if (filter_i2o_da && i2o_da.as_u32 != s->i2o.match.daddr.as_u32)
1461 if (filter_o2i_sa && o2i_sa.as_u32 != s->o2i.match.saddr.as_u32)
1463 if (filter_o2i_da && o2i_da.as_u32 != s->o2i.match.daddr.as_u32)
1465 if (filter_i2o_sp &&
1466 i2o_sp != clib_net_to_host_u16 (s->i2o.match.sport))
1468 if (filter_i2o_dp &&
1469 i2o_dp != clib_net_to_host_u16 (s->i2o.match.dport))
1471 if (filter_o2i_sp &&
1472 o2i_sp != clib_net_to_host_u16 (s->o2i.match.sport))
1474 if (filter_o2i_dp &&
1475 o2i_dp != clib_net_to_host_u16 (s->o2i.match.dport))
1477 if (filter_proto && proto != s->proto)
1481 vlib_cli_output (vm, " %U\n", format_snat_session, tsm, s);
1485 vlib_cli_output (vm,
1486 "Showed: %d, Filtered: %d of total %d "
1487 "sessions of thread %d\n\n",
1489 pool_elts (tsm->sessions) - showed_sessions,
1490 pool_elts (tsm->sessions), i);
1496 unformat_free (line_input);
1500 static clib_error_t *
1501 nat44_set_session_limit_command_fn (vlib_main_t * vm,
1502 unformat_input_t * input,
1503 vlib_cli_command_t * cmd)
1505 unformat_input_t _line_input, *line_input = &_line_input;
1506 clib_error_t *error = 0;
1508 u32 session_limit = 0, vrf_id = 0;
1510 if (!unformat_user (input, unformat_line_input, line_input))
1511 return clib_error_return (0, NAT44_ED_EXPECTED_ARGUMENT);
1513 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1515 if (unformat (line_input, "%u", &session_limit))
1517 else if (unformat (line_input, "vrf %u", &vrf_id))
1521 error = clib_error_return (0, "unknown input '%U'",
1522 format_unformat_error, line_input);
1528 error = clib_error_return (0, "missing value of session limit");
1529 else if (nat44_update_session_limit (session_limit, vrf_id))
1530 error = clib_error_return (0, "nat44_set_session_limit failed");
1533 unformat_free (line_input);
1538 static clib_error_t *
1539 nat44_del_session_command_fn (vlib_main_t * vm,
1540 unformat_input_t * input,
1541 vlib_cli_command_t * cmd)
1543 snat_main_t *sm = &snat_main;
1544 unformat_input_t _line_input, *line_input = &_line_input;
1545 u32 port = 0, eh_port = 0, vrf_id = sm->outside_vrf_id;
1546 clib_error_t *error = 0;
1547 ip4_address_t addr, eh_addr;
1548 ip_protocol_t proto;
1552 if (!unformat_user (input, unformat_line_input, line_input))
1553 return clib_error_return (0, NAT44_ED_EXPECTED_ARGUMENT);
1555 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1557 if (unformat (line_input, "%U:%u %U", unformat_ip4_address, &addr, &port,
1558 unformat_ip_protocol, &proto))
1560 else if (unformat (line_input, "in"))
1563 vrf_id = sm->inside_vrf_id;
1565 else if (unformat (line_input, "out"))
1568 vrf_id = sm->outside_vrf_id;
1570 else if (unformat (line_input, "vrf %u", &vrf_id))
1572 else if (unformat (line_input, "external-host %U:%u",
1573 unformat_ip4_address, &eh_addr, &eh_port))
1577 error = clib_error_return (0, "unknown input '%U'",
1578 format_unformat_error, line_input);
1583 rv = nat44_ed_del_session (sm, &addr, clib_host_to_net_u16 (port), &eh_addr,
1584 clib_host_to_net_u16 (eh_port), proto, vrf_id,
1593 error = clib_error_return (0, "nat44_del_session returned %d", rv);
1598 unformat_free (line_input);
1603 static clib_error_t *
1604 snat_forwarding_set_command_fn (vlib_main_t * vm,
1605 unformat_input_t * input,
1606 vlib_cli_command_t * cmd)
1608 snat_main_t *sm = &snat_main;
1609 unformat_input_t _line_input, *line_input = &_line_input;
1610 clib_error_t *error = 0;
1612 u8 enable_set = 0, enable = 0;
1614 if (!unformat_user (input, unformat_line_input, line_input))
1615 return clib_error_return (0, NAT44_ED_EXPECTED_ARGUMENT);
1617 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1622 if (unformat (line_input, "disable"))
1624 else if (unformat (line_input, "enable"))
1629 error = clib_error_return (0, "unknown input '%U'",
1630 format_unformat_error, line_input);
1636 error = clib_error_return (0, "expected enable | disable");
1638 sm->forwarding_enabled = enable;
1641 unformat_free (line_input);
1645 static clib_error_t *
1646 set_timeout_command_fn (vlib_main_t * vm,
1647 unformat_input_t * input, vlib_cli_command_t * cmd)
1649 snat_main_t *sm = &snat_main;
1650 unformat_input_t _line_input, *line_input = &_line_input;
1651 clib_error_t *error = 0;
1653 if (!unformat_user (input, unformat_line_input, line_input))
1654 return clib_error_return (0, NAT44_ED_EXPECTED_ARGUMENT);
1656 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1658 if (unformat (line_input, "udp %u", &sm->timeouts.udp));
1659 else if (unformat (line_input, "tcp-established %u",
1660 &sm->timeouts.tcp.established));
1661 else if (unformat (line_input, "tcp-transitory %u",
1662 &sm->timeouts.tcp.transitory));
1663 else if (unformat (line_input, "icmp %u", &sm->timeouts.icmp));
1664 else if (unformat (line_input, "reset"))
1665 nat_reset_timeouts (&sm->timeouts);
1668 error = clib_error_return (0, "unknown input '%U'",
1669 format_unformat_error, line_input);
1674 unformat_free (line_input);
1678 static clib_error_t *
1679 nat_show_timeouts_command_fn (vlib_main_t * vm,
1680 unformat_input_t * input,
1681 vlib_cli_command_t * cmd)
1683 snat_main_t *sm = &snat_main;
1685 vlib_cli_output (vm, "udp timeout: %dsec", sm->timeouts.udp);
1686 vlib_cli_output (vm, "tcp-established timeout: %dsec",
1687 sm->timeouts.tcp.established);
1688 vlib_cli_output (vm, "tcp-transitory timeout: %dsec",
1689 sm->timeouts.tcp.transitory);
1690 vlib_cli_output (vm, "icmp timeout: %dsec", sm->timeouts.icmp);
1695 static clib_error_t *
1696 set_frame_queue_nelts_command_fn (vlib_main_t *vm, unformat_input_t *input,
1697 vlib_cli_command_t *cmd)
1699 unformat_input_t _line_input, *line_input = &_line_input;
1700 clib_error_t *error = 0;
1701 u32 frame_queue_nelts = 0;
1703 if (!unformat_user (input, unformat_line_input, line_input))
1704 return clib_error_return (0, NAT44_ED_EXPECTED_ARGUMENT);
1706 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1708 if (unformat (line_input, "%u", &frame_queue_nelts))
1712 error = clib_error_return (0, "unknown input '%U'",
1713 format_unformat_error, line_input);
1717 if (!frame_queue_nelts)
1719 error = clib_error_return (0, "frame_queue_nelts cannot be zero");
1722 if (nat44_ed_set_frame_queue_nelts (frame_queue_nelts) != 0)
1724 error = clib_error_return (0, "snat_set_frame_queue_nelts failed");
1728 unformat_free (line_input);
1734 * @cliexstart{nat44}
1735 * Enable nat44 plugin
1736 * To enable nat44-ed, use:
1738 * To disable nat44-ed, use:
1739 * vpp# nat44 disable
1740 * To enable nat44-ed static mapping with connection tracking, use:
1741 * vpp# nat44-ed enable static-mapping connection-tracking
1742 * To set inside-vrf outside-vrf, use:
1743 * vpp# nat44 enable inside-vrf <id> outside-vrf <id>
1746 VLIB_CLI_COMMAND (nat44_ed_enable_disable_command, static) = {
1748 .short_help = "nat44 <enable [sessions <max-number>] [static-mapping-only "
1749 "connection-tracking] [inside-vrf <vrf-id>] "
1750 "[outside-vrf <vrf-id>]>|disable",
1751 .function = nat44_ed_enable_disable_command_fn,
1756 * @cliexstart{set snat workers}
1757 * Set NAT workers if 2 or more workers available, use:
1758 * vpp# set snat workers 0-2,5
1761 VLIB_CLI_COMMAND (set_workers_command, static) = {
1762 .path = "set nat workers",
1763 .function = set_workers_command_fn,
1764 .short_help = "set nat workers <workers-list>",
1769 * @cliexstart{show nat workers}
1771 * vpp# show nat workers:
1777 VLIB_CLI_COMMAND (nat_show_workers_command, static) = {
1778 .path = "show nat workers",
1779 .short_help = "show nat workers",
1780 .function = nat_show_workers_command_fn,
1785 * @cliexstart{set nat timeout}
1786 * Set values of timeouts for NAT sessions (in seconds), use:
1787 * vpp# set nat timeout udp 120 tcp-established 7500 tcp-transitory 250 icmp 90
1788 * To reset default values use:
1789 * vpp# set nat timeout reset
1792 VLIB_CLI_COMMAND (set_timeout_command, static) = {
1793 .path = "set nat timeout",
1794 .function = set_timeout_command_fn,
1796 "set nat timeout [udp <sec> | tcp-established <sec> "
1797 "tcp-transitory <sec> | icmp <sec> | reset]",
1802 * @cliexstart{show nat timeouts}
1803 * Show values of timeouts for NAT sessions.
1804 * vpp# show nat timeouts
1805 * udp timeout: 300sec
1806 * tcp-established timeout: 7440sec
1807 * tcp-transitory timeout: 240sec
1808 * icmp timeout: 60sec
1811 VLIB_CLI_COMMAND (nat_show_timeouts_command, static) = {
1812 .path = "show nat timeouts",
1813 .short_help = "show nat timeouts",
1814 .function = nat_show_timeouts_command_fn,
1819 * @cliexstart{set nat frame-queue-nelts}
1820 * Set number of worker handoff frame queue elements.
1823 VLIB_CLI_COMMAND (set_frame_queue_nelts_command, static) = {
1824 .path = "set nat frame-queue-nelts",
1825 .function = set_frame_queue_nelts_command_fn,
1826 .short_help = "set nat frame-queue-nelts <number>",
1831 * @cliexstart{nat set logging level}
1832 * To set NAT logging level use:
1833 * Set nat logging level
1836 VLIB_CLI_COMMAND (snat_set_log_level_command, static) = {
1837 .path = "nat set logging level",
1838 .function = snat_set_log_level_command_fn,
1839 .short_help = "nat set logging level <level>",
1844 * @cliexstart{snat ipfix logging}
1845 * To enable NAT IPFIX logging use:
1846 * vpp# nat ipfix logging
1847 * To set IPFIX exporter use:
1848 * vpp# set ipfix exporter collector 10.10.10.3 src 10.10.10.1
1851 VLIB_CLI_COMMAND (snat_ipfix_logging_enable_disable_command, static) = {
1852 .path = "nat ipfix logging",
1853 .function = snat_ipfix_logging_enable_disable_command_fn,
1854 .short_help = "nat ipfix logging disable|<enable [domain <domain-id>] "
1855 "[src-port <port>]>",
1860 * @cliexstart{nat mss-clamping}
1861 * Set TCP MSS rewriting configuration
1862 * To enable TCP MSS rewriting use:
1863 * vpp# nat mss-clamping 1452
1864 * To disbale TCP MSS rewriting use:
1865 * vpp# nat mss-clamping disable
1868 VLIB_CLI_COMMAND (nat_set_mss_clamping_command, static) = {
1869 .path = "nat mss-clamping",
1870 .short_help = "nat mss-clamping <mss-value>|disable",
1871 .function = nat_set_mss_clamping_command_fn,
1876 * @cliexstart{show nat mss-clamping}
1877 * Show TCP MSS rewriting configuration
1880 VLIB_CLI_COMMAND (nat_show_mss_clamping_command, static) = {
1881 .path = "show nat mss-clamping",
1882 .short_help = "show nat mss-clamping",
1883 .function = nat_show_mss_clamping_command_fn,
1888 * @cliexstart{show nat44 hash tables}
1889 * Show NAT44 hash tables
1892 VLIB_CLI_COMMAND (nat44_show_hash, static) = {
1893 .path = "show nat44 hash tables",
1894 .short_help = "show nat44 hash tables [detail|verbose]",
1895 .function = nat44_show_hash_command_fn,
1900 * @cliexstart{nat44 add address}
1901 * Add/delete NAT44 pool address.
1902 * To add NAT44 pool address use:
1903 * vpp# nat44 add address 172.16.1.3
1904 * vpp# nat44 add address 172.16.2.2 - 172.16.2.24
1905 * To add NAT44 pool address for specific tenant (identified by VRF id) use:
1906 * vpp# nat44 add address 172.16.1.3 tenant-vrf 10
1909 VLIB_CLI_COMMAND (add_address_command, static) = {
1910 .path = "nat44 add address",
1911 .short_help = "nat44 add address <ip4-range-start> [- <ip4-range-end>] "
1912 "[tenant-vrf <vrf-id>] [twice-nat] [del]",
1913 .function = add_address_command_fn,
1918 * @cliexstart{show nat44 summary}
1919 * Show NAT44 summary
1920 * vpp# show nat44 summary
1923 VLIB_CLI_COMMAND (nat44_show_summary_command, static) = {
1924 .path = "show nat44 summary",
1925 .short_help = "show nat44 summary",
1926 .function = nat44_show_summary_command_fn,
1931 * @cliexstart{show nat44 addresses}
1932 * Show NAT44 pool addresses.
1933 * vpp# show nat44 addresses
1934 * NAT44 pool addresses:
1936 * tenant VRF independent
1945 * NAT44 twice-nat pool addresses:
1947 * tenant VRF independent
1953 VLIB_CLI_COMMAND (nat44_show_addresses_command, static) = {
1954 .path = "show nat44 addresses",
1955 .short_help = "show nat44 addresses",
1956 .function = nat44_show_addresses_command_fn,
1961 * @cliexstart{set interface nat44}
1962 * Enable/disable NAT44 feature on the interface.
1963 * To enable NAT44 feature with local network interface use:
1964 * vpp# set interface nat44 in GigabitEthernet0/8/0
1965 * To enable NAT44 feature with external network interface use:
1966 * vpp# set interface nat44 out GigabitEthernet0/a/0
1969 VLIB_CLI_COMMAND (set_interface_snat_command, static) = {
1970 .path = "set interface nat44",
1971 .function = snat_feature_command_fn,
1972 .short_help = "set interface nat44 in <intfc> out <intfc> [output-feature] "
1978 * @cliexstart{show nat44 interfaces}
1979 * Show interfaces with NAT44 feature.
1980 * vpp# show nat44 interfaces
1982 * GigabitEthernet0/8/0 in
1983 * GigabitEthernet0/a/0 out
1986 VLIB_CLI_COMMAND (nat44_show_interfaces_command, static) = {
1987 .path = "show nat44 interfaces",
1988 .short_help = "show nat44 interfaces",
1989 .function = nat44_show_interfaces_command_fn,
1994 * @cliexstart{nat44 add static mapping}
1995 * Static mapping allows hosts on the external network to initiate connection
1996 * to to the local network host.
1997 * To create static mapping between local host address 10.0.0.3 port 6303 and
1998 * external address 4.4.4.4 port 3606 for TCP protocol use:
1999 * vpp# nat44 add static mapping tcp local 10.0.0.3 6303 external 4.4.4.4 3606
2000 * If not runnig "static mapping only" NAT plugin mode use before:
2001 * vpp# nat44 add address 4.4.4.4
2002 * To create address only static mapping between local and external address use:
2003 * vpp# nat44 add static mapping local 10.0.0.3 external 4.4.4.4
2004 * To create ICMP static mapping between local and external with ICMP echo
2005 * identifier 10 use:
2006 * vpp# nat44 add static mapping icmp local 10.0.0.3 10 external 4.4.4.4 10
2007 * To force use of specific pool address, vrf independent
2008 * vpp# nat44 add static mapping local 10.0.0.2 1234 external 10.0.2.2 1234 twice-nat exact 10.0.1.2
2011 VLIB_CLI_COMMAND (add_static_mapping_command, static) = {
2012 .path = "nat44 add static mapping",
2013 .function = add_static_mapping_command_fn,
2015 "nat44 add static mapping tcp|udp|icmp local <addr> [<port|icmp-echo-id>] "
2016 "external <addr> [<port|icmp-echo-id>] [vrf <table-id>] [twice-nat|self-twice-nat] "
2017 "[out2in-only] [exact <pool-addr>] [del]",
2022 * @cliexstart{nat44 add identity mapping}
2023 * Identity mapping translate an IP address to itself.
2024 * To create identity mapping for address 10.0.0.3 port 6303 for TCP protocol
2026 * vpp# nat44 add identity mapping 10.0.0.3 tcp 6303
2027 * To create identity mapping for address 10.0.0.3 use:
2028 * vpp# nat44 add identity mapping 10.0.0.3
2029 * To create identity mapping for DHCP addressed interface use:
2030 * vpp# nat44 add identity mapping external GigabitEthernet0/a/0 tcp 3606
2033 VLIB_CLI_COMMAND (add_identity_mapping_command, static) = {
2034 .path = "nat44 add identity mapping",
2035 .function = add_identity_mapping_command_fn,
2036 .short_help = "nat44 add identity mapping <ip4-addr>|external <interface> "
2037 "[<protocol> <port>] [vrf <table-id>] [del]",
2042 * @cliexstart{nat44 add load-balancing static mapping}
2043 * Service load balancing using NAT44
2044 * To add static mapping with load balancing for service with external IP
2045 * address 1.2.3.4 and TCP port 80 and mapped to 2 local servers
2046 * 10.100.10.10:8080 and 10.100.10.20:8080 with probability 80% resp. 20% use:
2047 * 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
2050 VLIB_CLI_COMMAND (add_lb_static_mapping_command, static) = {
2051 .path = "nat44 add load-balancing static mapping",
2052 .function = add_lb_static_mapping_command_fn,
2054 "nat44 add load-balancing static mapping protocol tcp|udp "
2055 "external <addr>:<port> local <addr>:<port> [vrf <table-id>] "
2056 "probability <n> [twice-nat|self-twice-nat] [out2in-only] "
2057 "[affinity <timeout-seconds>] [del]",
2062 * @cliexstart{nat44 add load-balancing static mapping}
2063 * Modify service load balancing using NAT44
2064 * To add new back-end server 10.100.10.30:8080 for service load balancing
2065 * static mapping with external IP address 1.2.3.4 and TCP port 80 use:
2066 * vpp# nat44 add load-balancing back-end protocol tcp external 1.2.3.4:80 local 10.100.10.30:8080 probability 25
2069 VLIB_CLI_COMMAND (add_lb_backend_command, static) = {
2070 .path = "nat44 add load-balancing back-end",
2071 .function = add_lb_backend_command_fn,
2073 "nat44 add load-balancing back-end protocol tcp|udp "
2074 "external <addr>:<port> local <addr>:<port> [vrf <table-id>] "
2075 "probability <n> [del]",
2080 * @cliexstart{show nat44 static mappings}
2081 * Show NAT44 static mappings.
2082 * vpp# show nat44 static mappings
2083 * NAT44 static mappings:
2084 * local 10.0.0.3 external 4.4.4.4 vrf 0
2085 * tcp local 192.168.0.4:6303 external 4.4.4.3:3606 vrf 0
2086 * tcp vrf 0 external 1.2.3.4:80 out2in-only
2087 * local 10.100.10.10:8080 probability 80
2088 * local 10.100.10.20:8080 probability 20
2089 * tcp local 10.100.3.8:8080 external 169.10.10.1:80 vrf 0 twice-nat
2090 * tcp local 10.0.0.10:3603 external GigabitEthernet0/a/0:6306 vrf 10
2093 VLIB_CLI_COMMAND (nat44_show_static_mappings_command, static) = {
2094 .path = "show nat44 static mappings",
2095 .short_help = "show nat44 static mappings",
2096 .function = nat44_show_static_mappings_command_fn,
2101 * @cliexstart{nat44 add interface address}
2102 * Use NAT44 pool address from specific interfce
2103 * To add NAT44 pool address from specific interface use:
2104 * vpp# nat44 add interface address GigabitEthernet0/8/0
2107 VLIB_CLI_COMMAND (snat_add_interface_address_command, static) = {
2108 .path = "nat44 add interface address",
2109 .short_help = "nat44 add interface address <interface> [twice-nat] [del]",
2110 .function = snat_add_interface_address_command_fn,
2115 * @cliexstart{show nat44 interface address}
2116 * Show NAT44 pool address interfaces
2117 * vpp# show nat44 interface address
2118 * NAT44 pool address interfaces:
2119 * GigabitEthernet0/a/0
2120 * NAT44 twice-nat pool address interfaces:
2121 * GigabitEthernet0/8/0
2124 VLIB_CLI_COMMAND (nat44_show_interface_address_command, static) = {
2125 .path = "show nat44 interface address",
2126 .short_help = "show nat44 interface address",
2127 .function = nat44_show_interface_address_command_fn,
2132 * @cliexstart{show nat44 sessions}
2133 * Show NAT44 sessions.
2136 VLIB_CLI_COMMAND (nat44_show_sessions_command, static) = {
2137 .path = "show nat44 sessions",
2138 .short_help = "show nat44 sessions [filter {i2o | o2i} {saddr <ip4-addr> "
2139 "| sport <n> | daddr <ip4-addr> | dport <n> | proto <proto>} "
2140 "[filter .. [..]]]",
2141 .function = nat44_show_sessions_command_fn,
2146 * @cliexstart{set nat44 session limit}
2147 * Set NAT44 session limit.
2150 VLIB_CLI_COMMAND (nat44_set_session_limit_command, static) = {
2151 .path = "set nat44 session limit",
2152 .short_help = "set nat44 session limit <limit> [vrf <table-id>]",
2153 .function = nat44_set_session_limit_command_fn,
2158 * @cliexstart{nat44 del session}
2159 * To administratively delete NAT44 session by inside address and port use:
2160 * vpp# nat44 del session in 10.0.0.3:6303 tcp
2161 * To administratively delete NAT44 session by outside address and port use:
2162 * vpp# nat44 del session out 1.0.0.3:6033 udp
2165 VLIB_CLI_COMMAND (nat44_del_session_command, static) = {
2166 .path = "nat44 del session",
2167 .short_help = "nat44 del session in|out <addr>:<port> tcp|udp|icmp [vrf <id>] [external-host <addr>:<port>]",
2168 .function = nat44_del_session_command_fn,
2173 * @cliexstart{nat44 forwarding}
2174 * Enable or disable forwarding
2175 * Forward packets which don't match existing translation
2176 * or static mapping instead of dropping them.
2177 * To enable forwarding, use:
2178 * vpp# nat44 forwarding enable
2179 * To disable forwarding, use:
2180 * vpp# nat44 forwarding disable
2183 VLIB_CLI_COMMAND (snat_forwarding_set_command, static) = {
2184 .path = "nat44 forwarding",
2185 .short_help = "nat44 forwarding enable|disable",
2186 .function = snat_forwarding_set_command_fn,
2190 * fd.io coding-style-patch-verification: ON
2193 * eval: (c-set-style "gnu")