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)
635 vlib_cli_output (vm, " tenant VRF: %u",
636 fib_table_get(ap->fib_index, FIB_PROTOCOL_IP4)->ft_table_id);
638 vlib_cli_output (vm, " tenant VRF independent");
640 vlib_cli_output (vm, "NAT44 twice-nat pool addresses:");
641 vec_foreach (ap, sm->twice_nat_addresses)
643 vlib_cli_output (vm, "%U", format_ip4_address, &ap->addr);
644 if (ap->fib_index != ~0)
645 vlib_cli_output (vm, " tenant VRF: %u",
646 fib_table_get(ap->fib_index, FIB_PROTOCOL_IP4)->ft_table_id);
648 vlib_cli_output (vm, " tenant VRF independent");
653 static clib_error_t *
654 snat_feature_command_fn (vlib_main_t * vm,
655 unformat_input_t * input, vlib_cli_command_t * cmd)
657 unformat_input_t _line_input, *line_input = &_line_input;
658 vnet_main_t *vnm = vnet_get_main ();
659 clib_error_t *error = 0;
661 u32 *inside_sw_if_indices = 0;
662 u32 *outside_sw_if_indices = 0;
663 u8 is_output_feature = 0;
664 int i, rv, is_del = 0;
668 /* Get a line of input. */
669 if (!unformat_user (input, unformat_line_input, line_input))
670 return clib_error_return (0, NAT44_ED_EXPECTED_ARGUMENT);
672 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
674 if (unformat (line_input, "in %U", unformat_vnet_sw_interface,
676 vec_add1 (inside_sw_if_indices, sw_if_index);
677 else if (unformat (line_input, "out %U", unformat_vnet_sw_interface,
679 vec_add1 (outside_sw_if_indices, sw_if_index);
680 else if (unformat (line_input, "output-feature"))
681 is_output_feature = 1;
682 else if (unformat (line_input, "del"))
686 error = clib_error_return (0, "unknown input '%U'",
687 format_unformat_error, line_input);
692 if (vec_len (inside_sw_if_indices))
694 for (i = 0; i < vec_len (inside_sw_if_indices); i++)
696 sw_if_index = inside_sw_if_indices[i];
697 if (is_output_feature)
701 rv = nat44_ed_del_output_interface (sw_if_index);
705 rv = nat44_ed_add_output_interface (sw_if_index);
709 error = clib_error_return (0, "%s %U failed",
710 is_del ? "del" : "add",
711 format_vnet_sw_if_index_name,
720 rv = nat44_ed_del_interface (sw_if_index, 1);
724 rv = nat44_ed_add_interface (sw_if_index, 1);
728 error = clib_error_return (0, "%s %U failed",
729 is_del ? "del" : "add",
730 format_vnet_sw_if_index_name,
738 if (vec_len (outside_sw_if_indices))
740 for (i = 0; i < vec_len (outside_sw_if_indices); i++)
742 sw_if_index = outside_sw_if_indices[i];
743 if (is_output_feature)
747 rv = nat44_ed_del_output_interface (sw_if_index);
751 rv = nat44_ed_add_output_interface (sw_if_index);
755 error = clib_error_return (0, "%s %U failed",
756 is_del ? "del" : "add",
757 format_vnet_sw_if_index_name,
766 rv = nat44_ed_del_interface (sw_if_index, 0);
770 rv = nat44_ed_add_interface (sw_if_index, 0);
774 error = clib_error_return (0, "%s %U failed",
775 is_del ? "del" : "add",
776 format_vnet_sw_if_index_name,
785 unformat_free (line_input);
786 vec_free (inside_sw_if_indices);
787 vec_free (outside_sw_if_indices);
792 static clib_error_t *
793 nat44_show_interfaces_command_fn (vlib_main_t * vm, unformat_input_t * input,
794 vlib_cli_command_t * cmd)
796 snat_main_t *sm = &snat_main;
798 vnet_main_t *vnm = vnet_get_main ();
800 vlib_cli_output (vm, "NAT44 interfaces:");
801 pool_foreach (i, sm->interfaces)
803 vlib_cli_output (vm, " %U %s", format_vnet_sw_if_index_name, vnm,
805 (nat44_ed_is_interface_inside (i) &&
806 nat44_ed_is_interface_outside (i)) ?
808 (nat44_ed_is_interface_inside (i) ? "in" : "out"));
811 pool_foreach (i, sm->output_feature_interfaces)
813 vlib_cli_output (vm, " %U output-feature %s",
814 format_vnet_sw_if_index_name, vnm, i->sw_if_index,
815 (nat44_ed_is_interface_inside (i) &&
816 nat44_ed_is_interface_outside (i)) ?
818 (nat44_ed_is_interface_inside (i) ? "in" : "out"));
824 static clib_error_t *
825 add_static_mapping_command_fn (vlib_main_t * vm,
826 unformat_input_t * input,
827 vlib_cli_command_t * cmd)
829 unformat_input_t _line_input, *line_input = &_line_input;
830 vnet_main_t *vnm = vnet_get_main ();
831 clib_error_t *error = 0;
832 ip4_address_t l_addr, e_addr, pool_addr;
833 u32 l_port = 0, e_port = 0, vrf_id = ~0;
834 u8 l_port_set = 0, e_port_set = 0;
837 u32 sw_if_index = ~0;
838 ip_protocol_t proto = 0;
840 if (!unformat_user (input, unformat_line_input, line_input))
841 return clib_error_return (0, NAT44_ED_EXPECTED_ARGUMENT);
843 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
845 if (unformat (line_input, "local %U %u", unformat_ip4_address, &l_addr,
851 if (unformat (line_input, "local %U", unformat_ip4_address, &l_addr))
853 else if (unformat (line_input, "external %U %u", unformat_ip4_address,
858 else if (unformat (line_input, "external %U", unformat_ip4_address,
861 else if (unformat (line_input, "external %U %u",
862 unformat_vnet_sw_interface, vnm, &sw_if_index,
865 flags |= NAT_SM_FLAG_SWITCH_ADDRESS;
868 else if (unformat (line_input, "external %U",
869 unformat_vnet_sw_interface, vnm, &sw_if_index))
871 flags |= NAT_SM_FLAG_SWITCH_ADDRESS;
873 else if (unformat (line_input, "exact %U", unformat_ip4_address,
876 flags |= NAT_SM_FLAG_EXACT_ADDRESS;
878 else if (unformat (line_input, "vrf %u", &vrf_id))
880 else if (unformat (line_input, "%U", unformat_ip_protocol, &proto))
882 else if (unformat (line_input, "self-twice-nat"))
884 flags |= NAT_SM_FLAG_SELF_TWICE_NAT;
886 else if (unformat (line_input, "twice-nat"))
888 flags |= NAT_SM_FLAG_TWICE_NAT;
890 else if (unformat (line_input, "out2in-only"))
892 flags |= NAT_SM_FLAG_OUT2IN_ONLY;
894 else if (unformat (line_input, "del"))
900 error = clib_error_return (0, "unknown input: '%U'",
901 format_unformat_error, line_input);
906 if (l_port_set != e_port_set)
908 error = clib_error_return (0, "Either both ports are set or none.");
914 flags |= NAT_SM_FLAG_ADDR_ONLY;
918 l_port = clib_host_to_net_u16 (l_port);
919 e_port = clib_host_to_net_u16 (e_port);
922 // TODO: specific pool_addr for both pool & twice nat pool ?
927 nat44_ed_add_static_mapping (l_addr, e_addr, l_port, e_port, proto,
928 vrf_id, sw_if_index, flags, pool_addr, 0);
932 rv = nat44_ed_del_static_mapping (l_addr, e_addr, l_port, e_port, proto,
933 vrf_id, sw_if_index, flags);
940 case VNET_API_ERROR_INVALID_VALUE:
941 error = clib_error_return (0, "External port already in use.");
943 case VNET_API_ERROR_NO_SUCH_ENTRY:
945 error = clib_error_return (0, "External address must be allocated.");
947 error = clib_error_return (0, "Mapping not exist.");
949 case VNET_API_ERROR_NO_SUCH_FIB:
950 error = clib_error_return (0, "No such VRF id.");
952 case VNET_API_ERROR_VALUE_EXIST:
953 error = clib_error_return (0, "Mapping already exist.");
960 unformat_free (line_input);
965 // TODO: either delete this bullshit or update it
966 static clib_error_t *
967 add_identity_mapping_command_fn (vlib_main_t * vm,
968 unformat_input_t * input,
969 vlib_cli_command_t * cmd)
971 unformat_input_t _line_input, *line_input = &_line_input;
972 vnet_main_t *vnm = vnet_get_main ();
973 clib_error_t *error = 0;
975 int rv, is_add = 1, port_set = 0;
976 u32 sw_if_index, port, flags, vrf_id = ~0;
977 ip_protocol_t proto = 0;
980 flags = NAT_SM_FLAG_IDENTITY_NAT;
982 /* Get a line of input. */
983 if (!unformat_user (input, unformat_line_input, line_input))
984 return clib_error_return (0, NAT44_ED_EXPECTED_ARGUMENT);
986 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
988 if (unformat (line_input, "%U", unformat_ip4_address, &addr))
990 else if (unformat (line_input, "external %U",
991 unformat_vnet_sw_interface, vnm, &sw_if_index))
993 flags |= NAT_SM_FLAG_SWITCH_ADDRESS;
995 else if (unformat (line_input, "vrf %u", &vrf_id))
997 else if (unformat (line_input, "%U %u", unformat_ip_protocol, &proto,
1002 else if (unformat (line_input, "del"))
1008 error = clib_error_return (0, "unknown input: '%U'",
1009 format_unformat_error, line_input);
1016 flags |= NAT_SM_FLAG_ADDR_ONLY;
1020 port = clib_host_to_net_u16 (port);
1026 rv = nat44_ed_add_static_mapping (addr, addr, port, port, proto, vrf_id,
1027 sw_if_index, flags, addr, 0);
1031 rv = nat44_ed_del_static_mapping (addr, addr, port, port, proto, vrf_id,
1032 sw_if_index, flags);
1035 // TODO: fix returns
1039 case VNET_API_ERROR_INVALID_VALUE:
1040 error = clib_error_return (0, "External port already in use.");
1042 case VNET_API_ERROR_NO_SUCH_ENTRY:
1044 error = clib_error_return (0, "External address must be allocated.");
1046 error = clib_error_return (0, "Mapping not exist.");
1048 case VNET_API_ERROR_NO_SUCH_FIB:
1049 error = clib_error_return (0, "No such VRF id.");
1051 case VNET_API_ERROR_VALUE_EXIST:
1052 error = clib_error_return (0, "Mapping already exist.");
1059 unformat_free (line_input);
1064 static clib_error_t *
1065 add_lb_static_mapping_command_fn (vlib_main_t * vm,
1066 unformat_input_t * input,
1067 vlib_cli_command_t * cmd)
1069 unformat_input_t _line_input, *line_input = &_line_input;
1070 clib_error_t *error = 0;
1071 ip4_address_t l_addr, e_addr;
1072 u32 l_port = 0, e_port = 0, vrf_id = 0, probability = 0, affinity = 0;
1074 ip_protocol_t proto;
1075 nat44_lb_addr_port_t *locals = 0, local;
1079 /* Get a line of input. */
1080 if (!unformat_user (input, unformat_line_input, line_input))
1081 return clib_error_return (0, NAT44_ED_EXPECTED_ARGUMENT);
1083 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1085 if (unformat (line_input, "local %U:%u probability %u",
1086 unformat_ip4_address, &l_addr, &l_port, &probability))
1088 clib_memset (&local, 0, sizeof (local));
1089 local.addr = l_addr;
1090 local.port = (u16) l_port;
1091 local.probability = (u8) probability;
1092 vec_add1 (locals, local);
1094 else if (unformat (line_input, "local %U:%u vrf %u probability %u",
1095 unformat_ip4_address, &l_addr, &l_port, &vrf_id,
1098 clib_memset (&local, 0, sizeof (local));
1099 local.addr = l_addr;
1100 local.port = (u16) l_port;
1101 local.probability = (u8) probability;
1102 local.vrf_id = vrf_id;
1103 vec_add1 (locals, local);
1105 else if (unformat (line_input, "external %U:%u", unformat_ip4_address,
1108 else if (unformat (line_input, "protocol %U", unformat_ip_protocol,
1113 else if (unformat (line_input, "twice-nat"))
1115 flags |= NAT_SM_FLAG_TWICE_NAT;
1117 else if (unformat (line_input, "self-twice-nat"))
1119 flags |= NAT_SM_FLAG_SELF_TWICE_NAT;
1121 else if (unformat (line_input, "out2in-only"))
1123 flags |= NAT_SM_FLAG_OUT2IN_ONLY;
1125 else if (unformat (line_input, "del"))
1129 else if (unformat (line_input, "affinity %u", &affinity))
1133 error = clib_error_return (0, "unknown input: '%U'",
1134 format_unformat_error, line_input);
1139 if (vec_len (locals) < 2)
1141 error = clib_error_return (0, "at least two local must be set");
1147 error = clib_error_return (0, "missing protocol");
1153 rv = nat44_ed_add_lb_static_mapping (e_addr, (u16) e_port, proto, locals,
1154 flags, 0, affinity);
1158 rv = nat44_ed_del_lb_static_mapping (e_addr, (u16) e_port, proto, flags);
1163 case VNET_API_ERROR_INVALID_VALUE:
1164 error = clib_error_return (0, "External port already in use.");
1166 case VNET_API_ERROR_NO_SUCH_ENTRY:
1168 error = clib_error_return (0, "External address must be allocated.");
1170 error = clib_error_return (0, "Mapping not exist.");
1172 case VNET_API_ERROR_VALUE_EXIST:
1173 error = clib_error_return (0, "Mapping already exist.");
1180 unformat_free (line_input);
1186 static clib_error_t *
1187 add_lb_backend_command_fn (vlib_main_t * vm,
1188 unformat_input_t * input, vlib_cli_command_t * cmd)
1190 unformat_input_t _line_input, *line_input = &_line_input;
1191 clib_error_t *error = 0;
1192 ip4_address_t l_addr, e_addr;
1193 u32 l_port = 0, e_port = 0, vrf_id = 0, probability = 0;
1196 ip_protocol_t proto;
1199 /* Get a line of input. */
1200 if (!unformat_user (input, unformat_line_input, line_input))
1201 return clib_error_return (0, NAT44_ED_EXPECTED_ARGUMENT);
1203 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1205 if (unformat (line_input, "local %U:%u probability %u",
1206 unformat_ip4_address, &l_addr, &l_port, &probability))
1208 else if (unformat (line_input, "local %U:%u vrf %u probability %u",
1209 unformat_ip4_address, &l_addr, &l_port, &vrf_id,
1212 else if (unformat (line_input, "external %U:%u", unformat_ip4_address,
1215 else if (unformat (line_input, "protocol %U", unformat_ip_protocol,
1218 else if (unformat (line_input, "del"))
1222 error = clib_error_return (0, "unknown input: '%U'",
1223 format_unformat_error, line_input);
1228 if (!l_port || !e_port)
1230 error = clib_error_return (0, "local or external must be set");
1236 error = clib_error_return (0, "missing protocol");
1240 rv = nat44_ed_add_del_lb_static_mapping_local (
1241 e_addr, (u16) e_port, l_addr, l_port, proto, vrf_id, probability, is_add);
1245 case VNET_API_ERROR_INVALID_VALUE:
1246 error = clib_error_return (0, "External is not load-balancing static "
1249 case VNET_API_ERROR_NO_SUCH_ENTRY:
1250 error = clib_error_return (0, "Mapping or back-end not exist.");
1252 case VNET_API_ERROR_VALUE_EXIST:
1253 error = clib_error_return (0, "Back-end already exist.");
1255 case VNET_API_ERROR_UNSPECIFIED:
1256 error = clib_error_return (0, "At least two back-ends must remain");
1263 unformat_free (line_input);
1268 static clib_error_t *
1269 nat44_show_static_mappings_command_fn (vlib_main_t * vm,
1270 unformat_input_t * input,
1271 vlib_cli_command_t * cmd)
1273 snat_main_t *sm = &snat_main;
1274 snat_static_mapping_t *m;
1275 snat_static_map_resolve_t *rp;
1277 vlib_cli_output (vm, "NAT44 static mappings:");
1278 pool_foreach (m, sm->static_mappings)
1280 vlib_cli_output (vm, " %U", format_snat_static_mapping, m);
1282 vec_foreach (rp, sm->to_resolve)
1283 vlib_cli_output (vm, " %U", format_snat_static_map_to_resolve, rp);
1288 static clib_error_t *
1289 snat_add_interface_address_command_fn (vlib_main_t * vm,
1290 unformat_input_t * input,
1291 vlib_cli_command_t * cmd)
1293 unformat_input_t _line_input, *line_input = &_line_input;
1294 snat_main_t *sm = &snat_main;
1295 clib_error_t *error = 0;
1300 if (!unformat_user (input, unformat_line_input, line_input))
1301 return clib_error_return (0, NAT44_ED_EXPECTED_ARGUMENT);
1303 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1305 if (unformat (line_input, "%U", unformat_vnet_sw_interface,
1306 sm->vnet_main, &sw_if_index))
1308 else if (unformat (line_input, "twice-nat"))
1312 else if (unformat (line_input, "del"))
1318 error = clib_error_return (0, "unknown input '%U'",
1319 format_unformat_error, line_input);
1326 rv = nat44_ed_add_interface_address (sw_if_index, twice_nat);
1329 error = clib_error_return (0, "add address returned %d", rv);
1334 rv = nat44_ed_del_interface_address (sw_if_index, twice_nat);
1337 error = clib_error_return (0, "del address returned %d", rv);
1342 unformat_free (line_input);
1347 static clib_error_t *
1348 nat44_show_interface_address_command_fn (vlib_main_t * vm,
1349 unformat_input_t * input,
1350 vlib_cli_command_t * cmd)
1352 snat_main_t *sm = &snat_main;
1353 vnet_main_t *vnm = vnet_get_main ();
1356 vlib_cli_output (vm, "NAT44 pool address interfaces:");
1357 vec_foreach (sw_if_index, sm->auto_add_sw_if_indices)
1359 vlib_cli_output (vm, " %U", format_vnet_sw_if_index_name, vnm,
1362 vlib_cli_output (vm, "NAT44 twice-nat pool address interfaces:");
1363 vec_foreach (sw_if_index, sm->auto_add_sw_if_indices_twice_nat)
1365 vlib_cli_output (vm, " %U", format_vnet_sw_if_index_name, vnm,
1372 static clib_error_t *
1373 nat44_show_sessions_command_fn (vlib_main_t * vm, unformat_input_t * input,
1374 vlib_cli_command_t * cmd)
1376 unformat_input_t _line_input, *line_input = &_line_input;
1377 clib_error_t *error = 0;
1378 snat_main_per_thread_data_t *tsm;
1379 snat_main_t *sm = &snat_main;
1380 ip4_address_t i2o_sa, i2o_da, o2i_sa, o2i_da;
1381 u8 filter_i2o_sa = 0, filter_i2o_da = 0;
1382 u8 filter_o2i_sa = 0, filter_o2i_da = 0;
1383 u16 i2o_sp, i2o_dp, o2i_sp, o2i_dp;
1384 u8 filter_i2o_sp = 0, filter_i2o_dp = 0;
1385 u8 filter_o2i_sp = 0, filter_o2i_dp = 0;
1386 ip_protocol_t proto;
1387 u8 filter_proto = 0;
1388 u8 had_input = 1, filtering = 0;
1389 int i = 0, showed_sessions;
1391 if (!unformat_user (input, unformat_line_input, line_input))
1397 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1399 if (unformat (line_input, "filter i2o saddr %U", unformat_ip4_address,
1401 filter_i2o_sa = filtering = 1;
1402 else if (unformat (line_input, "filter i2o daddr %U",
1403 unformat_ip4_address, &i2o_da))
1404 filter_i2o_da = filtering = 1;
1405 else if (unformat (line_input, "filter o2i saddr %U",
1406 unformat_ip4_address, &o2i_sa))
1407 filter_o2i_sa = filtering = 1;
1408 else if (unformat (line_input, "filter o2i daddr %U",
1409 unformat_ip4_address, &o2i_da))
1410 filter_o2i_da = filtering = 1;
1411 else if (unformat (line_input, "filter i2o sport %u", &i2o_sp))
1412 filter_i2o_sp = filtering = 1;
1413 else if (unformat (line_input, "filter i2o dport %u", &i2o_dp))
1414 filter_i2o_dp = filtering = 1;
1415 else if (unformat (line_input, "filter o2i sport %u", &o2i_sp))
1416 filter_o2i_sp = filtering = 1;
1417 else if (unformat (line_input, "filter o2i dport %u", &o2i_dp))
1418 filter_o2i_dp = filtering = 1;
1419 else if (unformat (line_input, "filter i2o proto %U",
1420 unformat_ip_protocol, &proto))
1421 filter_proto = filtering = 1;
1422 else if (unformat (line_input, "filter o2i proto %U",
1423 unformat_ip_protocol, &proto))
1424 filter_proto = filtering = 1;
1427 error = clib_error_return (0, "unknown input '%U'",
1428 format_unformat_error, line_input);
1434 vlib_cli_output (vm, "NAT44 ED sessions:");
1436 vec_foreach_index (i, sm->per_thread_data)
1438 tsm = vec_elt_at_index (sm->per_thread_data, i);
1440 vlib_cli_output (vm, "-------- thread %d %s: %d sessions --------\n",
1441 i, vlib_worker_threads[i].name,
1442 pool_elts (tsm->sessions));
1444 showed_sessions = 0;
1446 pool_foreach (s, tsm->sessions)
1450 if (filter_i2o_sa && i2o_sa.as_u32 != s->i2o.match.saddr.as_u32)
1452 if (filter_i2o_da && i2o_da.as_u32 != s->i2o.match.daddr.as_u32)
1454 if (filter_o2i_sa && o2i_sa.as_u32 != s->o2i.match.saddr.as_u32)
1456 if (filter_o2i_da && o2i_da.as_u32 != s->o2i.match.daddr.as_u32)
1458 if (filter_i2o_sp &&
1459 i2o_sp != clib_net_to_host_u16 (s->i2o.match.sport))
1461 if (filter_i2o_dp &&
1462 i2o_dp != clib_net_to_host_u16 (s->i2o.match.dport))
1464 if (filter_o2i_sp &&
1465 o2i_sp != clib_net_to_host_u16 (s->o2i.match.sport))
1467 if (filter_o2i_dp &&
1468 o2i_dp != clib_net_to_host_u16 (s->o2i.match.dport))
1470 if (filter_proto && proto != s->proto)
1474 vlib_cli_output (vm, " %U\n", format_snat_session, tsm, s);
1478 vlib_cli_output (vm,
1479 "Showed: %d, Filtered: %d of total %d "
1480 "sessions of thread %d\n\n",
1482 pool_elts (tsm->sessions) - showed_sessions,
1483 pool_elts (tsm->sessions), i);
1489 unformat_free (line_input);
1493 static clib_error_t *
1494 nat44_set_session_limit_command_fn (vlib_main_t * vm,
1495 unformat_input_t * input,
1496 vlib_cli_command_t * cmd)
1498 unformat_input_t _line_input, *line_input = &_line_input;
1499 clib_error_t *error = 0;
1501 u32 session_limit = 0, vrf_id = 0;
1503 if (!unformat_user (input, unformat_line_input, line_input))
1504 return clib_error_return (0, NAT44_ED_EXPECTED_ARGUMENT);
1506 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1508 if (unformat (line_input, "%u", &session_limit))
1510 else if (unformat (line_input, "vrf %u", &vrf_id))
1514 error = clib_error_return (0, "unknown input '%U'",
1515 format_unformat_error, line_input);
1521 error = clib_error_return (0, "missing value of session limit");
1522 else if (nat44_update_session_limit (session_limit, vrf_id))
1523 error = clib_error_return (0, "nat44_set_session_limit failed");
1526 unformat_free (line_input);
1531 static clib_error_t *
1532 nat44_del_session_command_fn (vlib_main_t * vm,
1533 unformat_input_t * input,
1534 vlib_cli_command_t * cmd)
1536 snat_main_t *sm = &snat_main;
1537 unformat_input_t _line_input, *line_input = &_line_input;
1538 u32 port = 0, eh_port = 0, vrf_id = sm->outside_vrf_id;
1539 clib_error_t *error = 0;
1540 ip4_address_t addr, eh_addr;
1541 ip_protocol_t proto;
1545 if (!unformat_user (input, unformat_line_input, line_input))
1546 return clib_error_return (0, NAT44_ED_EXPECTED_ARGUMENT);
1548 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1550 if (unformat (line_input, "%U:%u %U", unformat_ip4_address, &addr, &port,
1551 unformat_ip_protocol, &proto))
1553 else if (unformat (line_input, "in"))
1556 vrf_id = sm->inside_vrf_id;
1558 else if (unformat (line_input, "out"))
1561 vrf_id = sm->outside_vrf_id;
1563 else if (unformat (line_input, "vrf %u", &vrf_id))
1565 else if (unformat (line_input, "external-host %U:%u",
1566 unformat_ip4_address, &eh_addr, &eh_port))
1570 error = clib_error_return (0, "unknown input '%U'",
1571 format_unformat_error, line_input);
1576 rv = nat44_ed_del_session (sm, &addr, clib_host_to_net_u16 (port), &eh_addr,
1577 clib_host_to_net_u16 (eh_port), proto, vrf_id,
1586 error = clib_error_return (0, "nat44_del_session returned %d", rv);
1591 unformat_free (line_input);
1596 static clib_error_t *
1597 snat_forwarding_set_command_fn (vlib_main_t * vm,
1598 unformat_input_t * input,
1599 vlib_cli_command_t * cmd)
1601 snat_main_t *sm = &snat_main;
1602 unformat_input_t _line_input, *line_input = &_line_input;
1603 clib_error_t *error = 0;
1605 u8 enable_set = 0, enable = 0;
1607 if (!unformat_user (input, unformat_line_input, line_input))
1608 return clib_error_return (0, NAT44_ED_EXPECTED_ARGUMENT);
1610 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1615 if (unformat (line_input, "disable"))
1617 else if (unformat (line_input, "enable"))
1622 error = clib_error_return (0, "unknown input '%U'",
1623 format_unformat_error, line_input);
1629 error = clib_error_return (0, "expected enable | disable");
1631 sm->forwarding_enabled = enable;
1634 unformat_free (line_input);
1638 static clib_error_t *
1639 set_timeout_command_fn (vlib_main_t * vm,
1640 unformat_input_t * input, vlib_cli_command_t * cmd)
1642 snat_main_t *sm = &snat_main;
1643 unformat_input_t _line_input, *line_input = &_line_input;
1644 clib_error_t *error = 0;
1646 if (!unformat_user (input, unformat_line_input, line_input))
1647 return clib_error_return (0, NAT44_ED_EXPECTED_ARGUMENT);
1649 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1651 if (unformat (line_input, "udp %u", &sm->timeouts.udp));
1652 else if (unformat (line_input, "tcp-established %u",
1653 &sm->timeouts.tcp.established));
1654 else if (unformat (line_input, "tcp-transitory %u",
1655 &sm->timeouts.tcp.transitory));
1656 else if (unformat (line_input, "icmp %u", &sm->timeouts.icmp));
1657 else if (unformat (line_input, "reset"))
1658 nat_reset_timeouts (&sm->timeouts);
1661 error = clib_error_return (0, "unknown input '%U'",
1662 format_unformat_error, line_input);
1667 unformat_free (line_input);
1671 static clib_error_t *
1672 nat_show_timeouts_command_fn (vlib_main_t * vm,
1673 unformat_input_t * input,
1674 vlib_cli_command_t * cmd)
1676 snat_main_t *sm = &snat_main;
1678 vlib_cli_output (vm, "udp timeout: %dsec", sm->timeouts.udp);
1679 vlib_cli_output (vm, "tcp-established timeout: %dsec",
1680 sm->timeouts.tcp.established);
1681 vlib_cli_output (vm, "tcp-transitory timeout: %dsec",
1682 sm->timeouts.tcp.transitory);
1683 vlib_cli_output (vm, "icmp timeout: %dsec", sm->timeouts.icmp);
1688 static clib_error_t *
1689 set_frame_queue_nelts_command_fn (vlib_main_t *vm, unformat_input_t *input,
1690 vlib_cli_command_t *cmd)
1692 unformat_input_t _line_input, *line_input = &_line_input;
1693 clib_error_t *error = 0;
1694 u32 frame_queue_nelts = 0;
1696 if (!unformat_user (input, unformat_line_input, line_input))
1697 return clib_error_return (0, NAT44_ED_EXPECTED_ARGUMENT);
1699 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1701 if (unformat (line_input, "%u", &frame_queue_nelts))
1705 error = clib_error_return (0, "unknown input '%U'",
1706 format_unformat_error, line_input);
1710 if (!frame_queue_nelts)
1712 error = clib_error_return (0, "frame_queue_nelts cannot be zero");
1715 if (nat44_ed_set_frame_queue_nelts (frame_queue_nelts) != 0)
1717 error = clib_error_return (0, "snat_set_frame_queue_nelts failed");
1721 unformat_free (line_input);
1727 * @cliexstart{nat44}
1728 * Enable nat44 plugin
1729 * To enable nat44-ed, use:
1731 * To disable nat44-ed, use:
1732 * vpp# nat44 disable
1733 * To enable nat44-ed static mapping with connection tracking, use:
1734 * vpp# nat44-ed enable static-mapping connection-tracking
1735 * To set inside-vrf outside-vrf, use:
1736 * vpp# nat44 enable inside-vrf <id> outside-vrf <id>
1739 VLIB_CLI_COMMAND (nat44_ed_enable_disable_command, static) = {
1741 .short_help = "nat44 <enable [sessions <max-number>] [static-mapping-only "
1742 "connection-tracking] [inside-vrf <vrf-id>] "
1743 "[outside-vrf <vrf-id>]>|disable",
1744 .function = nat44_ed_enable_disable_command_fn,
1749 * @cliexstart{set snat workers}
1750 * Set NAT workers if 2 or more workers available, use:
1751 * vpp# set snat workers 0-2,5
1754 VLIB_CLI_COMMAND (set_workers_command, static) = {
1755 .path = "set nat workers",
1756 .function = set_workers_command_fn,
1757 .short_help = "set nat workers <workers-list>",
1762 * @cliexstart{show nat workers}
1764 * vpp# show nat workers:
1770 VLIB_CLI_COMMAND (nat_show_workers_command, static) = {
1771 .path = "show nat workers",
1772 .short_help = "show nat workers",
1773 .function = nat_show_workers_command_fn,
1778 * @cliexstart{set nat timeout}
1779 * Set values of timeouts for NAT sessions (in seconds), use:
1780 * vpp# set nat timeout udp 120 tcp-established 7500 tcp-transitory 250 icmp 90
1781 * To reset default values use:
1782 * vpp# set nat timeout reset
1785 VLIB_CLI_COMMAND (set_timeout_command, static) = {
1786 .path = "set nat timeout",
1787 .function = set_timeout_command_fn,
1789 "set nat timeout [udp <sec> | tcp-established <sec> "
1790 "tcp-transitory <sec> | icmp <sec> | reset]",
1795 * @cliexstart{show nat timeouts}
1796 * Show values of timeouts for NAT sessions.
1797 * vpp# show nat timeouts
1798 * udp timeout: 300sec
1799 * tcp-established timeout: 7440sec
1800 * tcp-transitory timeout: 240sec
1801 * icmp timeout: 60sec
1804 VLIB_CLI_COMMAND (nat_show_timeouts_command, static) = {
1805 .path = "show nat timeouts",
1806 .short_help = "show nat timeouts",
1807 .function = nat_show_timeouts_command_fn,
1812 * @cliexstart{set nat frame-queue-nelts}
1813 * Set number of worker handoff frame queue elements.
1816 VLIB_CLI_COMMAND (set_frame_queue_nelts_command, static) = {
1817 .path = "set nat frame-queue-nelts",
1818 .function = set_frame_queue_nelts_command_fn,
1819 .short_help = "set nat frame-queue-nelts <number>",
1824 * @cliexstart{nat set logging level}
1825 * To set NAT logging level use:
1826 * Set nat logging level
1829 VLIB_CLI_COMMAND (snat_set_log_level_command, static) = {
1830 .path = "nat set logging level",
1831 .function = snat_set_log_level_command_fn,
1832 .short_help = "nat set logging level <level>",
1837 * @cliexstart{snat ipfix logging}
1838 * To enable NAT IPFIX logging use:
1839 * vpp# nat ipfix logging
1840 * To set IPFIX exporter use:
1841 * vpp# set ipfix exporter collector 10.10.10.3 src 10.10.10.1
1844 VLIB_CLI_COMMAND (snat_ipfix_logging_enable_disable_command, static) = {
1845 .path = "nat ipfix logging",
1846 .function = snat_ipfix_logging_enable_disable_command_fn,
1847 .short_help = "nat ipfix logging disable|<enable [domain <domain-id>] "
1848 "[src-port <port>]>",
1853 * @cliexstart{nat mss-clamping}
1854 * Set TCP MSS rewriting configuration
1855 * To enable TCP MSS rewriting use:
1856 * vpp# nat mss-clamping 1452
1857 * To disbale TCP MSS rewriting use:
1858 * vpp# nat mss-clamping disable
1861 VLIB_CLI_COMMAND (nat_set_mss_clamping_command, static) = {
1862 .path = "nat mss-clamping",
1863 .short_help = "nat mss-clamping <mss-value>|disable",
1864 .function = nat_set_mss_clamping_command_fn,
1869 * @cliexstart{show nat mss-clamping}
1870 * Show TCP MSS rewriting configuration
1873 VLIB_CLI_COMMAND (nat_show_mss_clamping_command, static) = {
1874 .path = "show nat mss-clamping",
1875 .short_help = "show nat mss-clamping",
1876 .function = nat_show_mss_clamping_command_fn,
1881 * @cliexstart{show nat44 hash tables}
1882 * Show NAT44 hash tables
1885 VLIB_CLI_COMMAND (nat44_show_hash, static) = {
1886 .path = "show nat44 hash tables",
1887 .short_help = "show nat44 hash tables [detail|verbose]",
1888 .function = nat44_show_hash_command_fn,
1893 * @cliexstart{nat44 add address}
1894 * Add/delete NAT44 pool address.
1895 * To add NAT44 pool address use:
1896 * vpp# nat44 add address 172.16.1.3
1897 * vpp# nat44 add address 172.16.2.2 - 172.16.2.24
1898 * To add NAT44 pool address for specific tenant (identified by VRF id) use:
1899 * vpp# nat44 add address 172.16.1.3 tenant-vrf 10
1902 VLIB_CLI_COMMAND (add_address_command, static) = {
1903 .path = "nat44 add address",
1904 .short_help = "nat44 add address <ip4-range-start> [- <ip4-range-end>] "
1905 "[tenant-vrf <vrf-id>] [twice-nat] [del]",
1906 .function = add_address_command_fn,
1911 * @cliexstart{show nat44 summary}
1912 * Show NAT44 summary
1913 * vpp# show nat44 summary
1916 VLIB_CLI_COMMAND (nat44_show_summary_command, static) = {
1917 .path = "show nat44 summary",
1918 .short_help = "show nat44 summary",
1919 .function = nat44_show_summary_command_fn,
1924 * @cliexstart{show nat44 addresses}
1925 * Show NAT44 pool addresses.
1926 * vpp# show nat44 addresses
1927 * NAT44 pool addresses:
1929 * tenant VRF independent
1938 * NAT44 twice-nat pool addresses:
1940 * tenant VRF independent
1946 VLIB_CLI_COMMAND (nat44_show_addresses_command, static) = {
1947 .path = "show nat44 addresses",
1948 .short_help = "show nat44 addresses",
1949 .function = nat44_show_addresses_command_fn,
1954 * @cliexstart{set interface nat44}
1955 * Enable/disable NAT44 feature on the interface.
1956 * To enable NAT44 feature with local network interface use:
1957 * vpp# set interface nat44 in GigabitEthernet0/8/0
1958 * To enable NAT44 feature with external network interface use:
1959 * vpp# set interface nat44 out GigabitEthernet0/a/0
1962 VLIB_CLI_COMMAND (set_interface_snat_command, static) = {
1963 .path = "set interface nat44",
1964 .function = snat_feature_command_fn,
1965 .short_help = "set interface nat44 in <intfc> out <intfc> [output-feature] "
1971 * @cliexstart{show nat44 interfaces}
1972 * Show interfaces with NAT44 feature.
1973 * vpp# show nat44 interfaces
1975 * GigabitEthernet0/8/0 in
1976 * GigabitEthernet0/a/0 out
1979 VLIB_CLI_COMMAND (nat44_show_interfaces_command, static) = {
1980 .path = "show nat44 interfaces",
1981 .short_help = "show nat44 interfaces",
1982 .function = nat44_show_interfaces_command_fn,
1987 * @cliexstart{nat44 add static mapping}
1988 * Static mapping allows hosts on the external network to initiate connection
1989 * to to the local network host.
1990 * To create static mapping between local host address 10.0.0.3 port 6303 and
1991 * external address 4.4.4.4 port 3606 for TCP protocol use:
1992 * vpp# nat44 add static mapping tcp local 10.0.0.3 6303 external 4.4.4.4 3606
1993 * If not runnig "static mapping only" NAT plugin mode use before:
1994 * vpp# nat44 add address 4.4.4.4
1995 * To create address only static mapping between local and external address use:
1996 * vpp# nat44 add static mapping local 10.0.0.3 external 4.4.4.4
1997 * To create ICMP static mapping between local and external with ICMP echo
1998 * identifier 10 use:
1999 * vpp# nat44 add static mapping icmp local 10.0.0.3 10 external 4.4.4.4 10
2000 * To force use of specific pool address, vrf independent
2001 * vpp# nat44 add static mapping local 10.0.0.2 1234 external 10.0.2.2 1234 twice-nat exact 10.0.1.2
2004 VLIB_CLI_COMMAND (add_static_mapping_command, static) = {
2005 .path = "nat44 add static mapping",
2006 .function = add_static_mapping_command_fn,
2008 "nat44 add static mapping tcp|udp|icmp local <addr> [<port|icmp-echo-id>] "
2009 "external <addr> [<port|icmp-echo-id>] [vrf <table-id>] [twice-nat|self-twice-nat] "
2010 "[out2in-only] [exact <pool-addr>] [del]",
2015 * @cliexstart{nat44 add identity mapping}
2016 * Identity mapping translate an IP address to itself.
2017 * To create identity mapping for address 10.0.0.3 port 6303 for TCP protocol
2019 * vpp# nat44 add identity mapping 10.0.0.3 tcp 6303
2020 * To create identity mapping for address 10.0.0.3 use:
2021 * vpp# nat44 add identity mapping 10.0.0.3
2022 * To create identity mapping for DHCP addressed interface use:
2023 * vpp# nat44 add identity mapping external GigabitEthernet0/a/0 tcp 3606
2026 VLIB_CLI_COMMAND (add_identity_mapping_command, static) = {
2027 .path = "nat44 add identity mapping",
2028 .function = add_identity_mapping_command_fn,
2029 .short_help = "nat44 add identity mapping <ip4-addr>|external <interface> "
2030 "[<protocol> <port>] [vrf <table-id>] [del]",
2035 * @cliexstart{nat44 add load-balancing static mapping}
2036 * Service load balancing using NAT44
2037 * To add static mapping with load balancing for service with external IP
2038 * address 1.2.3.4 and TCP port 80 and mapped to 2 local servers
2039 * 10.100.10.10:8080 and 10.100.10.20:8080 with probability 80% resp. 20% use:
2040 * 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
2043 VLIB_CLI_COMMAND (add_lb_static_mapping_command, static) = {
2044 .path = "nat44 add load-balancing static mapping",
2045 .function = add_lb_static_mapping_command_fn,
2047 "nat44 add load-balancing static mapping protocol tcp|udp "
2048 "external <addr>:<port> local <addr>:<port> [vrf <table-id>] "
2049 "probability <n> [twice-nat|self-twice-nat] [out2in-only] "
2050 "[affinity <timeout-seconds>] [del]",
2055 * @cliexstart{nat44 add load-balancing static mapping}
2056 * Modify service load balancing using NAT44
2057 * To add new back-end server 10.100.10.30:8080 for service load balancing
2058 * static mapping with external IP address 1.2.3.4 and TCP port 80 use:
2059 * vpp# nat44 add load-balancing back-end protocol tcp external 1.2.3.4:80 local 10.100.10.30:8080 probability 25
2062 VLIB_CLI_COMMAND (add_lb_backend_command, static) = {
2063 .path = "nat44 add load-balancing back-end",
2064 .function = add_lb_backend_command_fn,
2066 "nat44 add load-balancing back-end protocol tcp|udp "
2067 "external <addr>:<port> local <addr>:<port> [vrf <table-id>] "
2068 "probability <n> [del]",
2073 * @cliexstart{show nat44 static mappings}
2074 * Show NAT44 static mappings.
2075 * vpp# show nat44 static mappings
2076 * NAT44 static mappings:
2077 * local 10.0.0.3 external 4.4.4.4 vrf 0
2078 * tcp local 192.168.0.4:6303 external 4.4.4.3:3606 vrf 0
2079 * tcp vrf 0 external 1.2.3.4:80 out2in-only
2080 * local 10.100.10.10:8080 probability 80
2081 * local 10.100.10.20:8080 probability 20
2082 * tcp local 10.100.3.8:8080 external 169.10.10.1:80 vrf 0 twice-nat
2083 * tcp local 10.0.0.10:3603 external GigabitEthernet0/a/0:6306 vrf 10
2086 VLIB_CLI_COMMAND (nat44_show_static_mappings_command, static) = {
2087 .path = "show nat44 static mappings",
2088 .short_help = "show nat44 static mappings",
2089 .function = nat44_show_static_mappings_command_fn,
2094 * @cliexstart{nat44 add interface address}
2095 * Use NAT44 pool address from specific interfce
2096 * To add NAT44 pool address from specific interface use:
2097 * vpp# nat44 add interface address GigabitEthernet0/8/0
2100 VLIB_CLI_COMMAND (snat_add_interface_address_command, static) = {
2101 .path = "nat44 add interface address",
2102 .short_help = "nat44 add interface address <interface> [twice-nat] [del]",
2103 .function = snat_add_interface_address_command_fn,
2108 * @cliexstart{show nat44 interface address}
2109 * Show NAT44 pool address interfaces
2110 * vpp# show nat44 interface address
2111 * NAT44 pool address interfaces:
2112 * GigabitEthernet0/a/0
2113 * NAT44 twice-nat pool address interfaces:
2114 * GigabitEthernet0/8/0
2117 VLIB_CLI_COMMAND (nat44_show_interface_address_command, static) = {
2118 .path = "show nat44 interface address",
2119 .short_help = "show nat44 interface address",
2120 .function = nat44_show_interface_address_command_fn,
2125 * @cliexstart{show nat44 sessions}
2126 * Show NAT44 sessions.
2129 VLIB_CLI_COMMAND (nat44_show_sessions_command, static) = {
2130 .path = "show nat44 sessions",
2131 .short_help = "show nat44 sessions [filter {i2o | o2i} {saddr <ip4-addr> "
2132 "| sport <n> | daddr <ip4-addr> | dport <n> | proto <proto>} "
2133 "[filter .. [..]]]",
2134 .function = nat44_show_sessions_command_fn,
2139 * @cliexstart{set nat44 session limit}
2140 * Set NAT44 session limit.
2143 VLIB_CLI_COMMAND (nat44_set_session_limit_command, static) = {
2144 .path = "set nat44 session limit",
2145 .short_help = "set nat44 session limit <limit> [vrf <table-id>]",
2146 .function = nat44_set_session_limit_command_fn,
2151 * @cliexstart{nat44 del session}
2152 * To administratively delete NAT44 session by inside address and port use:
2153 * vpp# nat44 del session in 10.0.0.3:6303 tcp
2154 * To administratively delete NAT44 session by outside address and port use:
2155 * vpp# nat44 del session out 1.0.0.3:6033 udp
2158 VLIB_CLI_COMMAND (nat44_del_session_command, static) = {
2159 .path = "nat44 del session",
2160 .short_help = "nat44 del session in|out <addr>:<port> tcp|udp|icmp [vrf <id>] [external-host <addr>:<port>]",
2161 .function = nat44_del_session_command_fn,
2166 * @cliexstart{nat44 forwarding}
2167 * Enable or disable forwarding
2168 * Forward packets which don't match existing translation
2169 * or static mapping instead of dropping them.
2170 * To enable forwarding, use:
2171 * vpp# nat44 forwarding enable
2172 * To disable forwarding, use:
2173 * vpp# nat44 forwarding disable
2176 VLIB_CLI_COMMAND (snat_forwarding_set_command, static) = {
2177 .path = "nat44 forwarding",
2178 .short_help = "nat44 forwarding enable|disable",
2179 .function = snat_forwarding_set_command_fn,
2183 * fd.io coding-style-patch-verification: ON
2186 * eval: (c-set-style "gnu")