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_commnad_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_8_8, &sm->static_mapping_by_local,
288 vlib_cli_output (vm, "%U",
289 format_bihash_8_8, &sm->static_mapping_by_external,
291 vlib_cli_output (vm, "%U", format_bihash_16_8, &sm->flow_hash, verbose);
292 vec_foreach_index (i, sm->per_thread_data)
294 vlib_cli_output (vm, "-------- thread %d %s --------\n",
295 i, vlib_worker_threads[i].name);
296 vlib_cli_output (vm, "%U", format_bihash_16_8, &sm->flow_hash, verbose);
299 vlib_cli_output (vm, "%U", format_bihash_16_8, &nam->affinity_hash,
302 vlib_cli_output (vm, "-------- hash table parameters --------\n");
303 vlib_cli_output (vm, "translation buckets: %u", sm->translation_buckets);
307 static clib_error_t *
308 nat_set_mss_clamping_command_fn (vlib_main_t * vm, unformat_input_t * input,
309 vlib_cli_command_t * cmd)
311 unformat_input_t _line_input, *line_input = &_line_input;
312 snat_main_t *sm = &snat_main;
313 clib_error_t *error = 0;
316 /* Get a line of input. */
317 if (!unformat_user (input, unformat_line_input, line_input))
318 return clib_error_return (0, NAT44_ED_EXPECTED_ARGUMENT);
320 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
322 if (unformat (line_input, "disable"))
323 sm->mss_clamping = 0;
324 else if (unformat (line_input, "%d", &mss))
325 sm->mss_clamping = (u16) mss;
328 error = clib_error_return (0, "unknown input '%U'",
329 format_unformat_error, line_input);
335 unformat_free (line_input);
340 static clib_error_t *
341 nat_show_mss_clamping_command_fn (vlib_main_t * vm, unformat_input_t * input,
342 vlib_cli_command_t * cmd)
344 snat_main_t *sm = &snat_main;
346 if (sm->mss_clamping)
347 vlib_cli_output (vm, "mss-clamping %d", sm->mss_clamping);
349 vlib_cli_output (vm, "mss-clamping disabled");
354 static clib_error_t *
355 add_address_command_fn (vlib_main_t * vm,
356 unformat_input_t * input, vlib_cli_command_t * cmd)
358 unformat_input_t _line_input, *line_input = &_line_input;
359 snat_main_t *sm = &snat_main;
360 ip4_address_t start_addr, end_addr, this_addr;
361 u32 start_host_order, end_host_order;
366 clib_error_t *error = 0;
369 /* Get a line of input. */
370 if (!unformat_user (input, unformat_line_input, line_input))
371 return clib_error_return (0, NAT44_ED_EXPECTED_ARGUMENT);
373 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
375 if (unformat (line_input, "%U - %U",
376 unformat_ip4_address, &start_addr,
377 unformat_ip4_address, &end_addr))
379 else if (unformat (line_input, "tenant-vrf %u", &vrf_id))
381 else if (unformat (line_input, "%U", unformat_ip4_address, &start_addr))
382 end_addr = start_addr;
383 else if (unformat (line_input, "twice-nat"))
385 else if (unformat (line_input, "del"))
389 error = clib_error_return (0, "unknown input '%U'",
390 format_unformat_error, line_input);
395 if (sm->static_mapping_only)
397 error = clib_error_return (0, "static mapping only mode");
401 start_host_order = clib_host_to_net_u32 (start_addr.as_u32);
402 end_host_order = clib_host_to_net_u32 (end_addr.as_u32);
404 if (end_host_order < start_host_order)
406 error = clib_error_return (0, "end address less than start address");
410 count = (end_host_order - start_host_order) + 1;
413 nat_log_info ("%U - %U, %d addresses...",
414 format_ip4_address, &start_addr,
415 format_ip4_address, &end_addr, count);
417 this_addr = start_addr;
419 for (i = 0; i < count; i++)
423 rv = nat44_ed_add_address (&this_addr, vrf_id, twice_nat);
427 rv = nat44_ed_del_address (this_addr, 0, twice_nat);
432 case VNET_API_ERROR_VALUE_EXIST:
433 error = clib_error_return (0, "NAT address already in use.");
435 case VNET_API_ERROR_NO_SUCH_ENTRY:
436 error = clib_error_return (0, "NAT address not exist.");
438 case VNET_API_ERROR_UNSPECIFIED:
440 clib_error_return (0, "NAT address used in static mapping.");
446 increment_v4_address (&this_addr);
450 unformat_free (line_input);
456 nat44_show_lru_summary (vlib_main_t * vm, snat_main_per_thread_data_t * tsm,
457 u64 now, u64 sess_timeout_time)
459 snat_main_t *sm = &snat_main;
460 dlist_elt_t *oldest_elt;
468 clib_dlist_remove_head (tsm->lru_pool, tsm->n##_lru_head_index); \
469 if (~0 != oldest_index) \
471 oldest_elt = pool_elt_at_index (tsm->lru_pool, oldest_index); \
472 s = pool_elt_at_index (tsm->sessions, oldest_elt->value); \
473 sess_timeout_time = \
474 s->last_heard + (f64) nat44_session_get_timeout (sm, s); \
475 vlib_cli_output (vm, d " LRU min session timeout %llu (now %llu)", \
476 sess_timeout_time, now); \
477 clib_dlist_addhead (tsm->lru_pool, tsm->n##_lru_head_index, \
480 _ (tcp_estab, "established tcp");
481 _ (tcp_trans, "transitory tcp");
483 _ (unk_proto, "unknown protocol");
489 static clib_error_t *
490 nat44_show_summary_command_fn (vlib_main_t * vm, unformat_input_t * input,
491 vlib_cli_command_t * cmd)
493 snat_main_per_thread_data_t *tsm;
494 snat_main_t *sm = &snat_main;
499 u64 now = vlib_time_now (vm);
500 u64 sess_timeout_time = 0;
502 u32 udp_sessions = 0;
503 u32 tcp_sessions = 0;
504 u32 icmp_sessions = 0;
508 u32 transitory_wait_closed = 0;
509 u32 transitory_closed = 0;
514 for (fib = 0; fib < vec_len (sm->max_translations_per_fib); fib++)
515 vlib_cli_output (vm, "max translations per thread: %u fib %u",
516 sm->max_translations_per_fib[fib], fib);
518 if (sm->num_workers > 1)
520 vec_foreach (tsm, sm->per_thread_data)
522 pool_foreach (s, tsm->sessions)
524 sess_timeout_time = s->last_heard +
525 (f64) nat44_session_get_timeout (sm, s);
526 if (now >= sess_timeout_time)
529 switch (s->nat_proto)
531 case NAT_PROTOCOL_ICMP:
534 case NAT_PROTOCOL_TCP:
538 if (s->tcp_closed_timestamp)
540 if (now >= s->tcp_closed_timestamp)
546 ++transitory_wait_closed;
554 case NAT_PROTOCOL_UDP:
560 nat44_show_lru_summary (vm, tsm, now, sess_timeout_time);
561 count += pool_elts (tsm->sessions);
566 tsm = vec_elt_at_index (sm->per_thread_data, sm->num_workers);
567 pool_foreach (s, tsm->sessions)
569 sess_timeout_time = s->last_heard +
570 (f64) nat44_session_get_timeout (sm, s);
571 if (now >= sess_timeout_time)
574 switch (s->nat_proto)
576 case NAT_PROTOCOL_ICMP:
579 case NAT_PROTOCOL_TCP:
583 if (s->tcp_closed_timestamp)
585 if (now >= s->tcp_closed_timestamp)
591 ++transitory_wait_closed;
599 case NAT_PROTOCOL_UDP:
605 nat44_show_lru_summary (vm, tsm, now, sess_timeout_time);
606 count = pool_elts (tsm->sessions);
609 vlib_cli_output (vm, "total timed out sessions: %u", timed_out);
610 vlib_cli_output (vm, "total sessions: %u", count);
611 vlib_cli_output (vm, "total tcp sessions: %u", tcp_sessions);
612 vlib_cli_output (vm, "total tcp established sessions: %u", established);
613 vlib_cli_output (vm, "total tcp transitory sessions: %u", transitory);
614 vlib_cli_output (vm, "total tcp transitory (WAIT-CLOSED) sessions: %u",
615 transitory_wait_closed);
616 vlib_cli_output (vm, "total tcp transitory (CLOSED) sessions: %u",
618 vlib_cli_output (vm, "total udp sessions: %u", udp_sessions);
619 vlib_cli_output (vm, "total icmp sessions: %u", icmp_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");
639 #define _(N, i, n, s) \
640 vlib_cli_output (vm, " %d busy %s ports", ap->busy_##n##_ports, s);
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");
653 #define _(N, i, n, s) \
654 vlib_cli_output (vm, " %d busy %s ports", ap->busy_##n##_ports, s);
661 static clib_error_t *
662 snat_feature_command_fn (vlib_main_t * vm,
663 unformat_input_t * input, vlib_cli_command_t * cmd)
665 unformat_input_t _line_input, *line_input = &_line_input;
666 vnet_main_t *vnm = vnet_get_main ();
667 clib_error_t *error = 0;
669 u32 *inside_sw_if_indices = 0;
670 u32 *outside_sw_if_indices = 0;
671 u8 is_output_feature = 0;
672 int i, rv, is_del = 0;
676 /* Get a line of input. */
677 if (!unformat_user (input, unformat_line_input, line_input))
678 return clib_error_return (0, NAT44_ED_EXPECTED_ARGUMENT);
680 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
682 if (unformat (line_input, "in %U", unformat_vnet_sw_interface,
684 vec_add1 (inside_sw_if_indices, sw_if_index);
685 else if (unformat (line_input, "out %U", unformat_vnet_sw_interface,
687 vec_add1 (outside_sw_if_indices, sw_if_index);
688 else if (unformat (line_input, "output-feature"))
689 is_output_feature = 1;
690 else if (unformat (line_input, "del"))
694 error = clib_error_return (0, "unknown input '%U'",
695 format_unformat_error, line_input);
700 if (vec_len (inside_sw_if_indices))
702 for (i = 0; i < vec_len (inside_sw_if_indices); i++)
704 sw_if_index = inside_sw_if_indices[i];
705 if (is_output_feature)
709 rv = nat44_ed_del_output_interface (sw_if_index);
713 rv = nat44_ed_add_output_interface (sw_if_index);
717 error = clib_error_return (0, "%s %U failed",
718 is_del ? "del" : "add",
719 format_vnet_sw_if_index_name,
728 rv = nat44_ed_del_interface (sw_if_index, 1);
732 rv = nat44_ed_add_interface (sw_if_index, 1);
736 error = clib_error_return (0, "%s %U failed",
737 is_del ? "del" : "add",
738 format_vnet_sw_if_index_name,
746 if (vec_len (outside_sw_if_indices))
748 for (i = 0; i < vec_len (outside_sw_if_indices); i++)
750 sw_if_index = outside_sw_if_indices[i];
751 if (is_output_feature)
755 rv = nat44_ed_del_output_interface (sw_if_index);
759 rv = nat44_ed_add_output_interface (sw_if_index);
763 error = clib_error_return (0, "%s %U failed",
764 is_del ? "del" : "add",
765 format_vnet_sw_if_index_name,
774 rv = nat44_ed_del_interface (sw_if_index, 0);
778 rv = nat44_ed_add_interface (sw_if_index, 0);
782 error = clib_error_return (0, "%s %U failed",
783 is_del ? "del" : "add",
784 format_vnet_sw_if_index_name,
793 unformat_free (line_input);
794 vec_free (inside_sw_if_indices);
795 vec_free (outside_sw_if_indices);
800 static clib_error_t *
801 nat44_show_interfaces_command_fn (vlib_main_t * vm, unformat_input_t * input,
802 vlib_cli_command_t * cmd)
804 snat_main_t *sm = &snat_main;
806 vnet_main_t *vnm = vnet_get_main ();
808 vlib_cli_output (vm, "NAT44 interfaces:");
809 pool_foreach (i, sm->interfaces)
811 vlib_cli_output (vm, " %U %s", format_vnet_sw_if_index_name, vnm,
813 (nat44_ed_is_interface_inside (i) &&
814 nat44_ed_is_interface_outside (i)) ?
816 (nat44_ed_is_interface_inside (i) ? "in" : "out"));
819 pool_foreach (i, sm->output_feature_interfaces)
821 vlib_cli_output (vm, " %U output-feature %s",
822 format_vnet_sw_if_index_name, vnm, i->sw_if_index,
823 (nat44_ed_is_interface_inside (i) &&
824 nat44_ed_is_interface_outside (i)) ?
826 (nat44_ed_is_interface_inside (i) ? "in" : "out"));
832 static clib_error_t *
833 add_static_mapping_command_fn (vlib_main_t * vm,
834 unformat_input_t * input,
835 vlib_cli_command_t * cmd)
837 unformat_input_t _line_input, *line_input = &_line_input;
838 vnet_main_t *vnm = vnet_get_main ();
839 clib_error_t *error = 0;
842 nat_protocol_t proto = NAT_PROTOCOL_OTHER;
843 ip4_address_t l_addr, e_addr, pool_addr;
844 u32 l_port = 0, e_port = 0, vrf_id = ~0;
845 u8 l_port_set = 0, e_port_set = 0;
846 u32 sw_if_index, flags = 0;
849 if (!unformat_user (input, unformat_line_input, line_input))
850 return clib_error_return (0, NAT44_ED_EXPECTED_ARGUMENT);
852 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
854 if (unformat (line_input, "local %U %u", unformat_ip4_address, &l_addr,
860 if (unformat (line_input, "local %U", unformat_ip4_address, &l_addr))
862 else if (unformat (line_input, "external %U %u", unformat_ip4_address,
867 else if (unformat (line_input, "external %U", unformat_ip4_address,
870 else if (unformat (line_input, "external %U %u",
871 unformat_vnet_sw_interface, vnm, &sw_if_index,
874 flags |= NAT_SM_FLAG_SWITCH_ADDRESS;
877 else if (unformat (line_input, "external %U",
878 unformat_vnet_sw_interface, vnm, &sw_if_index))
880 flags |= NAT_SM_FLAG_SWITCH_ADDRESS;
882 else if (unformat (line_input, "exact %U", unformat_ip4_address,
885 flags |= NAT_SM_FLAG_EXACT_ADDRESS;
887 else if (unformat (line_input, "vrf %u", &vrf_id))
889 else if (unformat (line_input, "%U", unformat_nat_protocol, &proto))
891 else if (unformat (line_input, "self-twice-nat"))
893 flags |= NAT_SM_FLAG_SELF_TWICE_NAT;
895 else if (unformat (line_input, "twice-nat"))
897 flags |= NAT_SM_FLAG_TWICE_NAT;
899 else if (unformat (line_input, "out2in-only"))
901 flags |= NAT_SM_FLAG_OUT2IN_ONLY;
903 else if (unformat (line_input, "del"))
909 error = clib_error_return (0, "unknown input: '%U'",
910 format_unformat_error, line_input);
915 if (l_port_set != e_port_set)
917 error = clib_error_return (0, "Either both ports are set or none.");
923 flags |= NAT_SM_FLAG_ADDR_ONLY;
927 l_port = clib_host_to_net_u16 (l_port);
928 e_port = clib_host_to_net_u16 (e_port);
931 // TODO: specific pool_addr for both pool & twice nat pool ?
936 nat44_ed_add_static_mapping (l_addr, e_addr, l_port, e_port, proto,
937 vrf_id, sw_if_index, flags, pool_addr, 0);
941 rv = nat44_ed_del_static_mapping (l_addr, e_addr, l_port, e_port, proto,
942 vrf_id, sw_if_index, flags);
949 case VNET_API_ERROR_INVALID_VALUE:
950 error = clib_error_return (0, "External port already in use.");
952 case VNET_API_ERROR_NO_SUCH_ENTRY:
954 error = clib_error_return (0, "External address must be allocated.");
956 error = clib_error_return (0, "Mapping not exist.");
958 case VNET_API_ERROR_NO_SUCH_FIB:
959 error = clib_error_return (0, "No such VRF id.");
961 case VNET_API_ERROR_VALUE_EXIST:
962 error = clib_error_return (0, "Mapping already exist.");
969 unformat_free (line_input);
974 // TODO: either delete this bullshit or update it
975 static clib_error_t *
976 add_identity_mapping_command_fn (vlib_main_t * vm,
977 unformat_input_t * input,
978 vlib_cli_command_t * cmd)
980 unformat_input_t _line_input, *line_input = &_line_input;
981 vnet_main_t *vnm = vnet_get_main ();
982 clib_error_t *error = 0;
984 int rv, is_add = 1, port_set = 0;
985 u32 sw_if_index, port, flags, vrf_id = ~0;
986 nat_protocol_t proto;
989 flags = NAT_SM_FLAG_IDENTITY_NAT;
991 /* Get a line of input. */
992 if (!unformat_user (input, unformat_line_input, line_input))
993 return clib_error_return (0, NAT44_ED_EXPECTED_ARGUMENT);
995 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
997 if (unformat (line_input, "%U", unformat_ip4_address, &addr))
999 else if (unformat (line_input, "external %U",
1000 unformat_vnet_sw_interface, vnm, &sw_if_index))
1002 flags |= NAT_SM_FLAG_SWITCH_ADDRESS;
1004 else if (unformat (line_input, "vrf %u", &vrf_id))
1006 else if (unformat (line_input, "%U %u", unformat_nat_protocol, &proto,
1011 else if (unformat (line_input, "del"))
1017 error = clib_error_return (0, "unknown input: '%U'",
1018 format_unformat_error, line_input);
1025 flags |= NAT_SM_FLAG_ADDR_ONLY;
1029 port = clib_host_to_net_u16 (port);
1035 rv = nat44_ed_add_static_mapping (addr, addr, port, port, proto, vrf_id,
1036 sw_if_index, flags, addr, 0);
1040 rv = nat44_ed_del_static_mapping (addr, addr, port, port, proto, vrf_id,
1041 sw_if_index, flags);
1044 // TODO: fix returns
1048 case VNET_API_ERROR_INVALID_VALUE:
1049 error = clib_error_return (0, "External port already in use.");
1051 case VNET_API_ERROR_NO_SUCH_ENTRY:
1053 error = clib_error_return (0, "External address must be allocated.");
1055 error = clib_error_return (0, "Mapping not exist.");
1057 case VNET_API_ERROR_NO_SUCH_FIB:
1058 error = clib_error_return (0, "No such VRF id.");
1060 case VNET_API_ERROR_VALUE_EXIST:
1061 error = clib_error_return (0, "Mapping already exist.");
1068 unformat_free (line_input);
1073 static clib_error_t *
1074 add_lb_static_mapping_command_fn (vlib_main_t * vm,
1075 unformat_input_t * input,
1076 vlib_cli_command_t * cmd)
1078 unformat_input_t _line_input, *line_input = &_line_input;
1079 clib_error_t *error = 0;
1080 ip4_address_t l_addr, e_addr;
1081 u32 l_port = 0, e_port = 0, vrf_id = 0, probability = 0, affinity = 0;
1083 nat_protocol_t proto;
1084 nat44_lb_addr_port_t *locals = 0, local;
1088 /* Get a line of input. */
1089 if (!unformat_user (input, unformat_line_input, line_input))
1090 return clib_error_return (0, NAT44_ED_EXPECTED_ARGUMENT);
1092 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1094 if (unformat (line_input, "local %U:%u probability %u",
1095 unformat_ip4_address, &l_addr, &l_port, &probability))
1097 clib_memset (&local, 0, sizeof (local));
1098 local.addr = l_addr;
1099 local.port = (u16) l_port;
1100 local.probability = (u8) probability;
1101 vec_add1 (locals, local);
1103 else if (unformat (line_input, "local %U:%u vrf %u probability %u",
1104 unformat_ip4_address, &l_addr, &l_port, &vrf_id,
1107 clib_memset (&local, 0, sizeof (local));
1108 local.addr = l_addr;
1109 local.port = (u16) l_port;
1110 local.probability = (u8) probability;
1111 local.vrf_id = vrf_id;
1112 vec_add1 (locals, local);
1114 else if (unformat (line_input, "external %U:%u", unformat_ip4_address,
1117 else if (unformat (line_input, "protocol %U", unformat_nat_protocol,
1122 else if (unformat (line_input, "twice-nat"))
1124 flags |= NAT_SM_FLAG_TWICE_NAT;
1126 else if (unformat (line_input, "self-twice-nat"))
1128 flags |= NAT_SM_FLAG_SELF_TWICE_NAT;
1130 else if (unformat (line_input, "out2in-only"))
1132 flags |= NAT_SM_FLAG_OUT2IN_ONLY;
1134 else if (unformat (line_input, "del"))
1138 else if (unformat (line_input, "affinity %u", &affinity))
1142 error = clib_error_return (0, "unknown input: '%U'",
1143 format_unformat_error, line_input);
1148 if (vec_len (locals) < 2)
1150 error = clib_error_return (0, "at least two local must be set");
1156 error = clib_error_return (0, "missing protocol");
1162 rv = nat44_ed_add_lb_static_mapping (e_addr, (u16) e_port, proto, locals,
1163 flags, 0, affinity);
1167 rv = nat44_ed_del_lb_static_mapping (e_addr, (u16) e_port, proto, flags);
1172 case VNET_API_ERROR_INVALID_VALUE:
1173 error = clib_error_return (0, "External port already in use.");
1175 case VNET_API_ERROR_NO_SUCH_ENTRY:
1177 error = clib_error_return (0, "External address must be allocated.");
1179 error = clib_error_return (0, "Mapping not exist.");
1181 case VNET_API_ERROR_VALUE_EXIST:
1182 error = clib_error_return (0, "Mapping already exist.");
1189 unformat_free (line_input);
1195 static clib_error_t *
1196 add_lb_backend_command_fn (vlib_main_t * vm,
1197 unformat_input_t * input, vlib_cli_command_t * cmd)
1199 unformat_input_t _line_input, *line_input = &_line_input;
1200 clib_error_t *error = 0;
1201 ip4_address_t l_addr, e_addr;
1202 u32 l_port = 0, e_port = 0, vrf_id = 0, probability = 0;
1205 nat_protocol_t proto;
1208 /* Get a line of input. */
1209 if (!unformat_user (input, unformat_line_input, line_input))
1210 return clib_error_return (0, NAT44_ED_EXPECTED_ARGUMENT);
1212 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1214 if (unformat (line_input, "local %U:%u probability %u",
1215 unformat_ip4_address, &l_addr, &l_port, &probability))
1217 else if (unformat (line_input, "local %U:%u vrf %u probability %u",
1218 unformat_ip4_address, &l_addr, &l_port, &vrf_id,
1221 else if (unformat (line_input, "external %U:%u", unformat_ip4_address,
1224 else if (unformat (line_input, "protocol %U", unformat_nat_protocol,
1227 else if (unformat (line_input, "del"))
1231 error = clib_error_return (0, "unknown input: '%U'",
1232 format_unformat_error, line_input);
1237 if (!l_port || !e_port)
1239 error = clib_error_return (0, "local or external must be set");
1245 error = clib_error_return (0, "missing protocol");
1249 rv = nat44_ed_add_del_lb_static_mapping_local (
1250 e_addr, (u16) e_port, l_addr, l_port, proto, vrf_id, probability, is_add);
1254 case VNET_API_ERROR_INVALID_VALUE:
1255 error = clib_error_return (0, "External is not load-balancing static "
1258 case VNET_API_ERROR_NO_SUCH_ENTRY:
1259 error = clib_error_return (0, "Mapping or back-end not exist.");
1261 case VNET_API_ERROR_VALUE_EXIST:
1262 error = clib_error_return (0, "Back-end already exist.");
1264 case VNET_API_ERROR_UNSPECIFIED:
1265 error = clib_error_return (0, "At least two back-ends must remain");
1272 unformat_free (line_input);
1277 static clib_error_t *
1278 nat44_show_static_mappings_command_fn (vlib_main_t * vm,
1279 unformat_input_t * input,
1280 vlib_cli_command_t * cmd)
1282 snat_main_t *sm = &snat_main;
1283 snat_static_mapping_t *m;
1284 snat_static_map_resolve_t *rp;
1286 vlib_cli_output (vm, "NAT44 static mappings:");
1287 pool_foreach (m, sm->static_mappings)
1289 vlib_cli_output (vm, " %U", format_snat_static_mapping, m);
1291 vec_foreach (rp, sm->to_resolve)
1292 vlib_cli_output (vm, " %U", format_snat_static_map_to_resolve, rp);
1297 static clib_error_t *
1298 snat_add_interface_address_command_fn (vlib_main_t * vm,
1299 unformat_input_t * input,
1300 vlib_cli_command_t * cmd)
1302 unformat_input_t _line_input, *line_input = &_line_input;
1303 snat_main_t *sm = &snat_main;
1304 clib_error_t *error = 0;
1309 if (!unformat_user (input, unformat_line_input, line_input))
1310 return clib_error_return (0, NAT44_ED_EXPECTED_ARGUMENT);
1312 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1314 if (unformat (line_input, "%U", unformat_vnet_sw_interface,
1315 sm->vnet_main, &sw_if_index))
1317 else if (unformat (line_input, "twice-nat"))
1321 else if (unformat (line_input, "del"))
1327 error = clib_error_return (0, "unknown input '%U'",
1328 format_unformat_error, line_input);
1335 rv = nat44_ed_add_interface_address (sw_if_index, twice_nat);
1338 error = clib_error_return (0, "add address returned %d", rv);
1343 rv = nat44_ed_del_interface_address (sw_if_index, twice_nat);
1346 error = clib_error_return (0, "del address returned %d", rv);
1351 unformat_free (line_input);
1356 static clib_error_t *
1357 nat44_show_interface_address_command_fn (vlib_main_t * vm,
1358 unformat_input_t * input,
1359 vlib_cli_command_t * cmd)
1361 snat_main_t *sm = &snat_main;
1362 vnet_main_t *vnm = vnet_get_main ();
1365 vlib_cli_output (vm, "NAT44 pool address interfaces:");
1366 vec_foreach (sw_if_index, sm->auto_add_sw_if_indices)
1368 vlib_cli_output (vm, " %U", format_vnet_sw_if_index_name, vnm,
1371 vlib_cli_output (vm, "NAT44 twice-nat pool address interfaces:");
1372 vec_foreach (sw_if_index, sm->auto_add_sw_if_indices_twice_nat)
1374 vlib_cli_output (vm, " %U", format_vnet_sw_if_index_name, vnm,
1381 static clib_error_t *
1382 nat44_show_sessions_command_fn (vlib_main_t * vm, unformat_input_t * input,
1383 vlib_cli_command_t * cmd)
1385 unformat_input_t _line_input, *line_input = &_line_input;
1386 clib_error_t *error = 0;
1387 snat_main_per_thread_data_t *tsm;
1388 snat_main_t *sm = &snat_main;
1389 ip4_address_t i2o_sa, i2o_da, o2i_sa, o2i_da;
1390 u8 filter_i2o_sa = 0, filter_i2o_da = 0;
1391 u8 filter_o2i_sa = 0, filter_o2i_da = 0;
1392 u16 i2o_sp, i2o_dp, o2i_sp, o2i_dp;
1393 u8 filter_i2o_sp = 0, filter_i2o_dp = 0;
1394 u8 filter_o2i_sp = 0, filter_o2i_dp = 0;
1395 nat_protocol_t proto;
1396 u8 filter_proto = 0;
1397 u8 had_input = 1, filtering = 0;
1398 int i = 0, showed_sessions;
1400 if (!unformat_user (input, unformat_line_input, line_input))
1406 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1408 if (unformat (line_input, "filter i2o saddr %U", unformat_ip4_address,
1410 filter_i2o_sa = filtering = 1;
1411 else if (unformat (line_input, "filter i2o daddr %U",
1412 unformat_ip4_address, &i2o_da))
1413 filter_i2o_da = filtering = 1;
1414 else if (unformat (line_input, "filter o2i saddr %U",
1415 unformat_ip4_address, &o2i_sa))
1416 filter_o2i_sa = filtering = 1;
1417 else if (unformat (line_input, "filter o2i daddr %U",
1418 unformat_ip4_address, &o2i_da))
1419 filter_o2i_da = filtering = 1;
1420 else if (unformat (line_input, "filter i2o sport %u", &i2o_sp))
1421 filter_i2o_sp = filtering = 1;
1422 else if (unformat (line_input, "filter i2o dport %u", &i2o_dp))
1423 filter_i2o_dp = filtering = 1;
1424 else if (unformat (line_input, "filter o2i sport %u", &o2i_sp))
1425 filter_o2i_sp = filtering = 1;
1426 else if (unformat (line_input, "filter o2i dport %u", &o2i_dp))
1427 filter_o2i_dp = filtering = 1;
1428 else if (unformat (line_input, "filter i2o proto %U",
1429 unformat_nat_protocol, &proto))
1430 filter_proto = filtering = 1;
1431 else if (unformat (line_input, "filter o2i proto %U",
1432 unformat_nat_protocol, &proto))
1433 filter_proto = filtering = 1;
1436 error = clib_error_return (0, "unknown input '%U'",
1437 format_unformat_error, line_input);
1443 vlib_cli_output (vm, "NAT44 ED sessions:");
1445 vec_foreach_index (i, sm->per_thread_data)
1447 tsm = vec_elt_at_index (sm->per_thread_data, i);
1449 vlib_cli_output (vm, "-------- thread %d %s: %d sessions --------\n",
1450 i, vlib_worker_threads[i].name,
1451 pool_elts (tsm->sessions));
1453 showed_sessions = 0;
1455 pool_foreach (s, tsm->sessions)
1459 if (filter_i2o_sa && i2o_sa.as_u32 != s->i2o.match.saddr.as_u32)
1461 if (filter_i2o_da && i2o_da.as_u32 != s->i2o.match.daddr.as_u32)
1463 if (filter_o2i_sa && o2i_sa.as_u32 != s->o2i.match.saddr.as_u32)
1465 if (filter_o2i_da && o2i_da.as_u32 != s->o2i.match.daddr.as_u32)
1467 if (filter_i2o_sp &&
1468 i2o_sp != clib_net_to_host_u16 (s->i2o.match.sport))
1470 if (filter_i2o_dp &&
1471 i2o_dp != clib_net_to_host_u16 (s->i2o.match.dport))
1473 if (filter_o2i_sp &&
1474 o2i_sp != clib_net_to_host_u16 (s->o2i.match.sport))
1476 if (filter_o2i_dp &&
1477 o2i_dp != clib_net_to_host_u16 (s->o2i.match.dport))
1479 if (filter_proto && proto != s->nat_proto)
1483 vlib_cli_output (vm, " %U\n", format_snat_session, tsm, s);
1487 vlib_cli_output (vm,
1488 "Showed: %d, Filtered: %d of total %d "
1489 "sessions of thread %d\n\n",
1491 pool_elts (tsm->sessions) - showed_sessions,
1492 pool_elts (tsm->sessions), i);
1498 unformat_free (line_input);
1502 static clib_error_t *
1503 nat44_set_session_limit_command_fn (vlib_main_t * vm,
1504 unformat_input_t * input,
1505 vlib_cli_command_t * cmd)
1507 unformat_input_t _line_input, *line_input = &_line_input;
1508 clib_error_t *error = 0;
1510 u32 session_limit = 0, vrf_id = 0;
1512 if (!unformat_user (input, unformat_line_input, line_input))
1513 return clib_error_return (0, NAT44_ED_EXPECTED_ARGUMENT);
1515 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1517 if (unformat (line_input, "%u", &session_limit))
1519 else if (unformat (line_input, "vrf %u", &vrf_id))
1523 error = clib_error_return (0, "unknown input '%U'",
1524 format_unformat_error, line_input);
1530 error = clib_error_return (0, "missing value of session limit");
1531 else if (nat44_update_session_limit (session_limit, vrf_id))
1532 error = clib_error_return (0, "nat44_set_session_limit failed");
1535 unformat_free (line_input);
1540 static clib_error_t *
1541 nat44_del_session_command_fn (vlib_main_t * vm,
1542 unformat_input_t * input,
1543 vlib_cli_command_t * cmd)
1545 snat_main_t *sm = &snat_main;
1546 unformat_input_t _line_input, *line_input = &_line_input;
1547 u32 port = 0, eh_port = 0, vrf_id = sm->outside_vrf_id;
1548 clib_error_t *error = 0;
1549 ip4_address_t addr, eh_addr;
1550 nat_protocol_t proto;
1554 if (!unformat_user (input, unformat_line_input, line_input))
1555 return clib_error_return (0, NAT44_ED_EXPECTED_ARGUMENT);
1557 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1560 (line_input, "%U:%u %U", unformat_ip4_address, &addr, &port,
1561 unformat_nat_protocol, &proto))
1563 else if (unformat (line_input, "in"))
1566 vrf_id = sm->inside_vrf_id;
1568 else if (unformat (line_input, "out"))
1571 vrf_id = sm->outside_vrf_id;
1573 else if (unformat (line_input, "vrf %u", &vrf_id))
1575 else if (unformat (line_input, "external-host %U:%u",
1576 unformat_ip4_address, &eh_addr, &eh_port))
1580 error = clib_error_return (0, "unknown input '%U'",
1581 format_unformat_error, line_input);
1586 rv = nat44_ed_del_session (sm, &addr, clib_host_to_net_u16 (port), &eh_addr,
1587 clib_host_to_net_u16 (eh_port),
1588 nat_proto_to_ip_proto (proto), vrf_id, is_in);
1596 error = clib_error_return (0, "nat44_del_session returned %d", rv);
1601 unformat_free (line_input);
1606 static clib_error_t *
1607 snat_forwarding_set_command_fn (vlib_main_t * vm,
1608 unformat_input_t * input,
1609 vlib_cli_command_t * cmd)
1611 snat_main_t *sm = &snat_main;
1612 unformat_input_t _line_input, *line_input = &_line_input;
1613 clib_error_t *error = 0;
1615 u8 enable_set = 0, enable = 0;
1617 if (!unformat_user (input, unformat_line_input, line_input))
1618 return clib_error_return (0, NAT44_ED_EXPECTED_ARGUMENT);
1620 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1625 if (unformat (line_input, "disable"))
1627 else if (unformat (line_input, "enable"))
1632 error = clib_error_return (0, "unknown input '%U'",
1633 format_unformat_error, line_input);
1639 error = clib_error_return (0, "expected enable | disable");
1641 sm->forwarding_enabled = enable;
1644 unformat_free (line_input);
1648 static clib_error_t *
1649 set_timeout_command_fn (vlib_main_t * vm,
1650 unformat_input_t * input, vlib_cli_command_t * cmd)
1652 snat_main_t *sm = &snat_main;
1653 unformat_input_t _line_input, *line_input = &_line_input;
1654 clib_error_t *error = 0;
1656 if (!unformat_user (input, unformat_line_input, line_input))
1657 return clib_error_return (0, NAT44_ED_EXPECTED_ARGUMENT);
1659 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1661 if (unformat (line_input, "udp %u", &sm->timeouts.udp));
1662 else if (unformat (line_input, "tcp-established %u",
1663 &sm->timeouts.tcp.established));
1664 else if (unformat (line_input, "tcp-transitory %u",
1665 &sm->timeouts.tcp.transitory));
1666 else if (unformat (line_input, "icmp %u", &sm->timeouts.icmp));
1667 else if (unformat (line_input, "reset"))
1668 nat_reset_timeouts (&sm->timeouts);
1671 error = clib_error_return (0, "unknown input '%U'",
1672 format_unformat_error, line_input);
1677 unformat_free (line_input);
1681 static clib_error_t *
1682 nat_show_timeouts_command_fn (vlib_main_t * vm,
1683 unformat_input_t * input,
1684 vlib_cli_command_t * cmd)
1686 snat_main_t *sm = &snat_main;
1688 vlib_cli_output (vm, "udp timeout: %dsec", sm->timeouts.udp);
1689 vlib_cli_output (vm, "tcp-established timeout: %dsec",
1690 sm->timeouts.tcp.established);
1691 vlib_cli_output (vm, "tcp-transitory timeout: %dsec",
1692 sm->timeouts.tcp.transitory);
1693 vlib_cli_output (vm, "icmp timeout: %dsec", sm->timeouts.icmp);
1698 static clib_error_t *
1699 set_frame_queue_nelts_command_fn (vlib_main_t *vm, unformat_input_t *input,
1700 vlib_cli_command_t *cmd)
1702 unformat_input_t _line_input, *line_input = &_line_input;
1703 clib_error_t *error = 0;
1704 u32 frame_queue_nelts = 0;
1706 if (!unformat_user (input, unformat_line_input, line_input))
1707 return clib_error_return (0, NAT44_ED_EXPECTED_ARGUMENT);
1709 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1711 if (unformat (line_input, "%u", &frame_queue_nelts))
1715 error = clib_error_return (0, "unknown input '%U'",
1716 format_unformat_error, line_input);
1720 if (!frame_queue_nelts)
1722 error = clib_error_return (0, "frame_queue_nelts cannot be zero");
1725 if (nat44_ed_set_frame_queue_nelts (frame_queue_nelts) != 0)
1727 error = clib_error_return (0, "snat_set_frame_queue_nelts failed");
1731 unformat_free (line_input);
1737 * @cliexstart{nat44}
1738 * Enable nat44 plugin
1739 * To enable nat44-ed, use:
1741 * To disable nat44-ed, use:
1742 * vpp# nat44 disable
1743 * To enable nat44-ed static mapping with connection tracking, use:
1744 * vpp# nat44-ed enable static-mapping connection-tracking
1745 * To set inside-vrf outside-vrf, use:
1746 * vpp# nat44 enable inside-vrf <id> outside-vrf <id>
1749 VLIB_CLI_COMMAND (nat44_ed_enable_disable_command, static) = {
1751 .short_help = "nat44 <enable [sessions <max-number>] [static-mapping-only "
1752 "connection-tracking] [inside-vrf <vrf-id>] "
1753 "[outside-vrf <vrf-id>]>|disable",
1754 .function = nat44_ed_enable_disable_command_fn,
1759 * @cliexstart{set snat workers}
1760 * Set NAT workers if 2 or more workers available, use:
1761 * vpp# set snat workers 0-2,5
1764 VLIB_CLI_COMMAND (set_workers_command, static) = {
1765 .path = "set nat workers",
1766 .function = set_workers_command_fn,
1767 .short_help = "set nat workers <workers-list>",
1772 * @cliexstart{show nat workers}
1774 * vpp# show nat workers:
1780 VLIB_CLI_COMMAND (nat_show_workers_command, static) = {
1781 .path = "show nat workers",
1782 .short_help = "show nat workers",
1783 .function = nat_show_workers_commnad_fn,
1788 * @cliexstart{set nat timeout}
1789 * Set values of timeouts for NAT sessions (in seconds), use:
1790 * vpp# set nat timeout udp 120 tcp-established 7500 tcp-transitory 250 icmp 90
1791 * To reset default values use:
1792 * vpp# set nat timeout reset
1795 VLIB_CLI_COMMAND (set_timeout_command, static) = {
1796 .path = "set nat timeout",
1797 .function = set_timeout_command_fn,
1799 "set nat timeout [udp <sec> | tcp-established <sec> "
1800 "tcp-transitory <sec> | icmp <sec> | reset]",
1805 * @cliexstart{show nat timeouts}
1806 * Show values of timeouts for NAT sessions.
1807 * vpp# show nat timeouts
1808 * udp timeout: 300sec
1809 * tcp-established timeout: 7440sec
1810 * tcp-transitory timeout: 240sec
1811 * icmp timeout: 60sec
1814 VLIB_CLI_COMMAND (nat_show_timeouts_command, static) = {
1815 .path = "show nat timeouts",
1816 .short_help = "show nat timeouts",
1817 .function = nat_show_timeouts_command_fn,
1822 * @cliexstart{set nat frame-queue-nelts}
1823 * Set number of worker handoff frame queue elements.
1826 VLIB_CLI_COMMAND (set_frame_queue_nelts_command, static) = {
1827 .path = "set nat frame-queue-nelts",
1828 .function = set_frame_queue_nelts_command_fn,
1829 .short_help = "set nat frame-queue-nelts <number>",
1834 * @cliexstart{nat set logging level}
1835 * To set NAT logging level use:
1836 * Set nat logging level
1839 VLIB_CLI_COMMAND (snat_set_log_level_command, static) = {
1840 .path = "nat set logging level",
1841 .function = snat_set_log_level_command_fn,
1842 .short_help = "nat set logging level <level>",
1847 * @cliexstart{snat ipfix logging}
1848 * To enable NAT IPFIX logging use:
1849 * vpp# nat ipfix logging
1850 * To set IPFIX exporter use:
1851 * vpp# set ipfix exporter collector 10.10.10.3 src 10.10.10.1
1854 VLIB_CLI_COMMAND (snat_ipfix_logging_enable_disable_command, static) = {
1855 .path = "nat ipfix logging",
1856 .function = snat_ipfix_logging_enable_disable_command_fn,
1857 .short_help = "nat ipfix logging disable|<enable [domain <domain-id>] "
1858 "[src-port <port>]>",
1863 * @cliexstart{nat mss-clamping}
1864 * Set TCP MSS rewriting configuration
1865 * To enable TCP MSS rewriting use:
1866 * vpp# nat mss-clamping 1452
1867 * To disbale TCP MSS rewriting use:
1868 * vpp# nat mss-clamping disable
1871 VLIB_CLI_COMMAND (nat_set_mss_clamping_command, static) = {
1872 .path = "nat mss-clamping",
1873 .short_help = "nat mss-clamping <mss-value>|disable",
1874 .function = nat_set_mss_clamping_command_fn,
1879 * @cliexstart{show nat mss-clamping}
1880 * Show TCP MSS rewriting configuration
1883 VLIB_CLI_COMMAND (nat_show_mss_clamping_command, static) = {
1884 .path = "show nat mss-clamping",
1885 .short_help = "show nat mss-clamping",
1886 .function = nat_show_mss_clamping_command_fn,
1891 * @cliexstart{show nat44 hash tables}
1892 * Show NAT44 hash tables
1895 VLIB_CLI_COMMAND (nat44_show_hash, static) = {
1896 .path = "show nat44 hash tables",
1897 .short_help = "show nat44 hash tables [detail|verbose]",
1898 .function = nat44_show_hash_command_fn,
1903 * @cliexstart{nat44 add address}
1904 * Add/delete NAT44 pool address.
1905 * To add NAT44 pool address use:
1906 * vpp# nat44 add address 172.16.1.3
1907 * vpp# nat44 add address 172.16.2.2 - 172.16.2.24
1908 * To add NAT44 pool address for specific tenant (identified by VRF id) use:
1909 * vpp# nat44 add address 172.16.1.3 tenant-vrf 10
1912 VLIB_CLI_COMMAND (add_address_command, static) = {
1913 .path = "nat44 add address",
1914 .short_help = "nat44 add address <ip4-range-start> [- <ip4-range-end>] "
1915 "[tenant-vrf <vrf-id>] [twice-nat] [del]",
1916 .function = add_address_command_fn,
1921 * @cliexstart{show nat44 summary}
1922 * Show NAT44 summary
1923 * vpp# show nat44 summary
1926 VLIB_CLI_COMMAND (nat44_show_summary_command, static) = {
1927 .path = "show nat44 summary",
1928 .short_help = "show nat44 summary",
1929 .function = nat44_show_summary_command_fn,
1934 * @cliexstart{show nat44 addresses}
1935 * Show NAT44 pool addresses.
1936 * vpp# show nat44 addresses
1937 * NAT44 pool addresses:
1939 * tenant VRF independent
1948 * NAT44 twice-nat pool addresses:
1950 * tenant VRF independent
1956 VLIB_CLI_COMMAND (nat44_show_addresses_command, static) = {
1957 .path = "show nat44 addresses",
1958 .short_help = "show nat44 addresses",
1959 .function = nat44_show_addresses_command_fn,
1964 * @cliexstart{set interface nat44}
1965 * Enable/disable NAT44 feature on the interface.
1966 * To enable NAT44 feature with local network interface use:
1967 * vpp# set interface nat44 in GigabitEthernet0/8/0
1968 * To enable NAT44 feature with external network interface use:
1969 * vpp# set interface nat44 out GigabitEthernet0/a/0
1972 VLIB_CLI_COMMAND (set_interface_snat_command, static) = {
1973 .path = "set interface nat44",
1974 .function = snat_feature_command_fn,
1975 .short_help = "set interface nat44 in <intfc> out <intfc> [output-feature] "
1981 * @cliexstart{show nat44 interfaces}
1982 * Show interfaces with NAT44 feature.
1983 * vpp# show nat44 interfaces
1985 * GigabitEthernet0/8/0 in
1986 * GigabitEthernet0/a/0 out
1989 VLIB_CLI_COMMAND (nat44_show_interfaces_command, static) = {
1990 .path = "show nat44 interfaces",
1991 .short_help = "show nat44 interfaces",
1992 .function = nat44_show_interfaces_command_fn,
1997 * @cliexstart{nat44 add static mapping}
1998 * Static mapping allows hosts on the external network to initiate connection
1999 * to to the local network host.
2000 * To create static mapping between local host address 10.0.0.3 port 6303 and
2001 * external address 4.4.4.4 port 3606 for TCP protocol use:
2002 * vpp# nat44 add static mapping tcp local 10.0.0.3 6303 external 4.4.4.4 3606
2003 * If not runnig "static mapping only" NAT plugin mode use before:
2004 * vpp# nat44 add address 4.4.4.4
2005 * To create address only static mapping between local and external address use:
2006 * vpp# nat44 add static mapping local 10.0.0.3 external 4.4.4.4
2007 * To create ICMP static mapping between local and external with ICMP echo
2008 * identifier 10 use:
2009 * vpp# nat44 add static mapping icmp local 10.0.0.3 10 external 4.4.4.4 10
2010 * To force use of specific pool address, vrf independent
2011 * vpp# nat44 add static mapping local 10.0.0.2 1234 external 10.0.2.2 1234 twice-nat exact 10.0.1.2
2014 VLIB_CLI_COMMAND (add_static_mapping_command, static) = {
2015 .path = "nat44 add static mapping",
2016 .function = add_static_mapping_command_fn,
2018 "nat44 add static mapping tcp|udp|icmp local <addr> [<port|icmp-echo-id>] "
2019 "external <addr> [<port|icmp-echo-id>] [vrf <table-id>] [twice-nat|self-twice-nat] "
2020 "[out2in-only] [exact <pool-addr>] [del]",
2025 * @cliexstart{nat44 add identity mapping}
2026 * Identity mapping translate an IP address to itself.
2027 * To create identity mapping for address 10.0.0.3 port 6303 for TCP protocol
2029 * vpp# nat44 add identity mapping 10.0.0.3 tcp 6303
2030 * To create identity mapping for address 10.0.0.3 use:
2031 * vpp# nat44 add identity mapping 10.0.0.3
2032 * To create identity mapping for DHCP addressed interface use:
2033 * vpp# nat44 add identity mapping external GigabitEthernet0/a/0 tcp 3606
2036 VLIB_CLI_COMMAND (add_identity_mapping_command, static) = {
2037 .path = "nat44 add identity mapping",
2038 .function = add_identity_mapping_command_fn,
2039 .short_help = "nat44 add identity mapping <ip4-addr>|external <interface> "
2040 "[<protocol> <port>] [vrf <table-id>] [del]",
2045 * @cliexstart{nat44 add load-balancing static mapping}
2046 * Service load balancing using NAT44
2047 * To add static mapping with load balancing for service with external IP
2048 * address 1.2.3.4 and TCP port 80 and mapped to 2 local servers
2049 * 10.100.10.10:8080 and 10.100.10.20:8080 with probability 80% resp. 20% use:
2050 * 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
2053 VLIB_CLI_COMMAND (add_lb_static_mapping_command, static) = {
2054 .path = "nat44 add load-balancing static mapping",
2055 .function = add_lb_static_mapping_command_fn,
2057 "nat44 add load-balancing static mapping protocol tcp|udp "
2058 "external <addr>:<port> local <addr>:<port> [vrf <table-id>] "
2059 "probability <n> [twice-nat|self-twice-nat] [out2in-only] "
2060 "[affinity <timeout-seconds>] [del]",
2065 * @cliexstart{nat44 add load-balancing static mapping}
2066 * Modify service load balancing using NAT44
2067 * To add new back-end server 10.100.10.30:8080 for service load balancing
2068 * static mapping with external IP address 1.2.3.4 and TCP port 80 use:
2069 * vpp# nat44 add load-balancing back-end protocol tcp external 1.2.3.4:80 local 10.100.10.30:8080 probability 25
2072 VLIB_CLI_COMMAND (add_lb_backend_command, static) = {
2073 .path = "nat44 add load-balancing back-end",
2074 .function = add_lb_backend_command_fn,
2076 "nat44 add load-balancing back-end protocol tcp|udp "
2077 "external <addr>:<port> local <addr>:<port> [vrf <table-id>] "
2078 "probability <n> [del]",
2083 * @cliexstart{show nat44 static mappings}
2084 * Show NAT44 static mappings.
2085 * vpp# show nat44 static mappings
2086 * NAT44 static mappings:
2087 * local 10.0.0.3 external 4.4.4.4 vrf 0
2088 * tcp local 192.168.0.4:6303 external 4.4.4.3:3606 vrf 0
2089 * tcp vrf 0 external 1.2.3.4:80 out2in-only
2090 * local 10.100.10.10:8080 probability 80
2091 * local 10.100.10.20:8080 probability 20
2092 * tcp local 10.100.3.8:8080 external 169.10.10.1:80 vrf 0 twice-nat
2093 * tcp local 10.0.0.10:3603 external GigabitEthernet0/a/0:6306 vrf 10
2096 VLIB_CLI_COMMAND (nat44_show_static_mappings_command, static) = {
2097 .path = "show nat44 static mappings",
2098 .short_help = "show nat44 static mappings",
2099 .function = nat44_show_static_mappings_command_fn,
2104 * @cliexstart{nat44 add interface address}
2105 * Use NAT44 pool address from specific interfce
2106 * To add NAT44 pool address from specific interface use:
2107 * vpp# nat44 add interface address GigabitEthernet0/8/0
2110 VLIB_CLI_COMMAND (snat_add_interface_address_command, static) = {
2111 .path = "nat44 add interface address",
2112 .short_help = "nat44 add interface address <interface> [twice-nat] [del]",
2113 .function = snat_add_interface_address_command_fn,
2118 * @cliexstart{show nat44 interface address}
2119 * Show NAT44 pool address interfaces
2120 * vpp# show nat44 interface address
2121 * NAT44 pool address interfaces:
2122 * GigabitEthernet0/a/0
2123 * NAT44 twice-nat pool address interfaces:
2124 * GigabitEthernet0/8/0
2127 VLIB_CLI_COMMAND (nat44_show_interface_address_command, static) = {
2128 .path = "show nat44 interface address",
2129 .short_help = "show nat44 interface address",
2130 .function = nat44_show_interface_address_command_fn,
2135 * @cliexstart{show nat44 sessions}
2136 * Show NAT44 sessions.
2139 VLIB_CLI_COMMAND (nat44_show_sessions_command, static) = {
2140 .path = "show nat44 sessions",
2141 .short_help = "show nat44 sessions [filter {i2o | o2i} {saddr <ip4-addr> "
2142 "| sport <n> | daddr <ip4-addr> | dport <n> | proto <proto>} "
2143 "[filter .. [..]]]",
2144 .function = nat44_show_sessions_command_fn,
2149 * @cliexstart{set nat44 session limit}
2150 * Set NAT44 session limit.
2153 VLIB_CLI_COMMAND (nat44_set_session_limit_command, static) = {
2154 .path = "set nat44 session limit",
2155 .short_help = "set nat44 session limit <limit> [vrf <table-id>]",
2156 .function = nat44_set_session_limit_command_fn,
2161 * @cliexstart{nat44 del session}
2162 * To administratively delete NAT44 session by inside address and port use:
2163 * vpp# nat44 del session in 10.0.0.3:6303 tcp
2164 * To administratively delete NAT44 session by outside address and port use:
2165 * vpp# nat44 del session out 1.0.0.3:6033 udp
2168 VLIB_CLI_COMMAND (nat44_del_session_command, static) = {
2169 .path = "nat44 del session",
2170 .short_help = "nat44 del session in|out <addr>:<port> tcp|udp|icmp [vrf <id>] [external-host <addr>:<port>]",
2171 .function = nat44_del_session_command_fn,
2176 * @cliexstart{nat44 forwarding}
2177 * Enable or disable forwarding
2178 * Forward packets which don't match existing translation
2179 * or static mapping instead of dropping them.
2180 * To enable forwarding, use:
2181 * vpp# nat44 forwarding enable
2182 * To disable forwarding, use:
2183 * vpp# nat44 forwarding disable
2186 VLIB_CLI_COMMAND (snat_forwarding_set_command, static) = {
2187 .path = "nat44 forwarding",
2188 .short_help = "nat44 forwarding enable|disable",
2189 .function = snat_forwarding_set_command_fn,
2193 * fd.io coding-style-patch-verification: ON
2196 * eval: (c-set-style "gnu")