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.
21 #include <nat/lib/ipfix_logging.h>
22 #include <nat/lib/nat_inlines.h>
23 #include <nat/nat_inlines.h>
24 #include <nat/nat44/inlines.h>
25 #include <nat/nat_affinity.h>
26 #include <vnet/fib/fib_table.h>
28 #include <nat/nat44-ei/nat44_ei_ha.h>
29 #include <nat/nat44-ei/nat44_ei.h>
31 #define UNSUPPORTED_IN_ED_MODE_STR \
32 "This command is unsupported in endpoint dependent mode"
33 #define SUPPORTED_ONLY_IN_ED_MODE_STR \
34 "This command is supported only in endpoint dependent mode"
37 nat44_enable_command_fn (vlib_main_t * vm,
38 unformat_input_t * input, vlib_cli_command_t * cmd)
40 snat_main_t *sm = &snat_main;
41 unformat_input_t _line_input, *line_input = &_line_input;
42 clib_error_t *error = 0;
44 nat44_config_t c = { 0 };
48 return clib_error_return (0, "nat44 already enabled");
50 /* Get a line of input. */
51 if (!unformat_user (input, unformat_line_input, line_input))
53 if (nat44_plugin_enable (c) != 0)
54 return clib_error_return (0, "nat44 enable failed");
58 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
60 if (!mode_set && unformat (line_input, "static-mapping-only"))
63 c.static_mapping_only = 1;
64 if (unformat (line_input, "connection-tracking"))
66 c.connection_tracking = 1;
69 else if (!mode_set && unformat (line_input, "out2in-dpo"))
74 else if (!mode_set && unformat (line_input, "endpoint-dependent"))
77 c.endpoint_dependent = 1;
79 else if (unformat (line_input, "inside-vrf %u", &c.inside_vrf));
80 else if (unformat (line_input, "outside-vrf %u", &c.outside_vrf));
81 else if (unformat (line_input, "users %u", &c.users));
82 else if (unformat (line_input, "sessions %u", &c.sessions));
83 else if (unformat (line_input, "user-sessions %u", &c.user_sessions));
86 error = clib_error_return (0, "unknown input '%U'",
87 format_unformat_error, line_input);
94 error = clib_error_return (0, "number of sessions is required");
98 if (nat44_plugin_enable (c) != 0)
99 error = clib_error_return (0, "nat44 enable failed");
101 unformat_free (line_input);
105 static clib_error_t *
106 nat44_disable_command_fn (vlib_main_t * vm,
107 unformat_input_t * input, vlib_cli_command_t * cmd)
109 snat_main_t *sm = &snat_main;
110 clib_error_t *error = 0;
113 return clib_error_return (0, "nat44 already disabled");
115 if (nat44_plugin_disable () != 0)
116 error = clib_error_return (0, "nat44 disable failed");
121 static clib_error_t *
122 set_workers_command_fn (vlib_main_t * vm,
123 unformat_input_t * input, vlib_cli_command_t * cmd)
125 unformat_input_t _line_input, *line_input = &_line_input;
128 clib_error_t *error = 0;
130 /* Get a line of input. */
131 if (!unformat_user (input, unformat_line_input, line_input))
134 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
136 if (unformat (line_input, "%U", unformat_bitmap_list, &bitmap))
140 error = clib_error_return (0, "unknown input '%U'",
141 format_unformat_error, line_input);
148 error = clib_error_return (0, "List of workers must be specified.");
152 rv = snat_set_workers (bitmap);
154 clib_bitmap_free (bitmap);
158 case VNET_API_ERROR_INVALID_WORKER:
159 error = clib_error_return (0, "Invalid worker(s).");
161 case VNET_API_ERROR_FEATURE_DISABLED:
162 error = clib_error_return (0,
163 "Supported only if 2 or more workes available.");
170 unformat_free (line_input);
175 static clib_error_t *
176 nat_show_workers_commnad_fn (vlib_main_t * vm, unformat_input_t * input,
177 vlib_cli_command_t * cmd)
179 snat_main_t *sm = &snat_main;
182 if (sm->num_workers > 1)
184 vlib_cli_output (vm, "%d workers", vec_len (sm->workers));
186 vec_foreach (worker, sm->workers)
188 vlib_worker_thread_t *w =
189 vlib_worker_threads + *worker + sm->first_worker_index;
190 vlib_cli_output (vm, " %s", w->name);
198 static clib_error_t *
199 snat_set_log_level_command_fn (vlib_main_t * vm,
200 unformat_input_t * input,
201 vlib_cli_command_t * cmd)
203 unformat_input_t _line_input, *line_input = &_line_input;
204 snat_main_t *sm = &snat_main;
205 u8 log_level = SNAT_LOG_NONE;
206 clib_error_t *error = 0;
208 /* Get a line of input. */
209 if (!unformat_user (input, unformat_line_input, line_input))
212 if (!unformat (line_input, "%d", &log_level))
214 error = clib_error_return (0, "unknown input '%U'",
215 format_unformat_error, line_input);
218 if (log_level > SNAT_LOG_DEBUG)
220 error = clib_error_return (0, "unknown logging level '%d'", log_level);
223 sm->log_level = log_level;
226 unformat_free (line_input);
231 static clib_error_t *
232 snat_ipfix_logging_enable_disable_command_fn (vlib_main_t * vm,
233 unformat_input_t * input,
234 vlib_cli_command_t * cmd)
236 unformat_input_t _line_input, *line_input = &_line_input;
241 clib_error_t *error = 0;
243 /* Get a line of input. */
244 if (!unformat_user (input, unformat_line_input, line_input))
246 rv = nat_ipfix_logging_enable_disable (enable, domain_id,
249 return clib_error_return (0, "ipfix logging enable failed");
253 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
255 if (unformat (line_input, "domain %d", &domain_id))
257 else if (unformat (line_input, "src-port %d", &src_port))
259 else if (unformat (line_input, "disable"))
263 error = clib_error_return (0, "unknown input '%U'",
264 format_unformat_error, line_input);
269 rv = nat_ipfix_logging_enable_disable (enable, domain_id, (u16) src_port);
273 error = clib_error_return (0, "ipfix logging enable failed");
278 unformat_free (line_input);
283 static clib_error_t *
284 nat44_show_hash_command_fn (vlib_main_t * vm, unformat_input_t * input,
285 vlib_cli_command_t * cmd)
287 snat_main_t *sm = &snat_main;
288 snat_main_per_thread_data_t *tsm;
289 nat_affinity_main_t *nam = &nat_affinity_main;
293 if (unformat (input, "detail"))
295 else if (unformat (input, "verbose"))
298 vlib_cli_output (vm, "%U", format_bihash_8_8, &sm->static_mapping_by_local,
300 vlib_cli_output (vm, "%U",
301 format_bihash_8_8, &sm->static_mapping_by_external,
303 vlib_cli_output (vm, "%U", format_bihash_16_8, &sm->out2in_ed, verbose);
304 vec_foreach_index (i, sm->per_thread_data)
306 tsm = vec_elt_at_index (sm->per_thread_data, i);
307 vlib_cli_output (vm, "-------- thread %d %s --------\n",
308 i, vlib_worker_threads[i].name);
309 if (sm->endpoint_dependent)
311 vlib_cli_output (vm, "%U", format_bihash_16_8, &tsm->in2out_ed,
316 vlib_cli_output (vm, "%U", format_bihash_8_8, &tsm->in2out, verbose);
317 vlib_cli_output (vm, "%U", format_bihash_8_8, &tsm->out2in, verbose);
319 vlib_cli_output (vm, "%U", format_bihash_8_8, &tsm->user_hash, verbose);
322 if (sm->endpoint_dependent)
324 vlib_cli_output (vm, "%U", format_bihash_16_8, &nam->affinity_hash,
328 vlib_cli_output (vm, "-------- hash table parameters --------\n");
329 vlib_cli_output (vm, "translation buckets: %u", sm->translation_buckets);
330 if (!sm->endpoint_dependent)
332 vlib_cli_output (vm, "user buckets: %u", sm->user_buckets);
337 static clib_error_t *
338 nat44_set_alloc_addr_and_port_alg_command_fn (vlib_main_t * vm,
339 unformat_input_t * input,
340 vlib_cli_command_t * cmd)
342 unformat_input_t _line_input, *line_input = &_line_input;
343 clib_error_t *error = 0;
344 u32 psid, psid_offset, psid_length, port_start, port_end;
345 snat_main_t *sm = &snat_main;
347 if (sm->endpoint_dependent)
348 return clib_error_return (0, UNSUPPORTED_IN_ED_MODE_STR);
350 /* Get a line of input. */
351 if (!unformat_user (input, unformat_line_input, line_input))
354 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
356 if (unformat (line_input, "default"))
357 nat44_ei_set_alloc_default ();
360 (line_input, "map-e psid %d psid-offset %d psid-len %d", &psid,
361 &psid_offset, &psid_length))
362 nat44_ei_set_alloc_mape ((u16) psid, (u16) psid_offset,
366 (line_input, "port-range %d - %d", &port_start, &port_end))
368 if (port_end <= port_start)
371 clib_error_return (0,
372 "The end-port must be greater than start-port");
375 nat44_ei_set_alloc_range ((u16) port_start, (u16) port_end);
379 error = clib_error_return (0, "unknown input '%U'",
380 format_unformat_error, line_input);
386 unformat_free (line_input);
391 static clib_error_t *
392 nat44_show_alloc_addr_and_port_alg_command_fn (vlib_main_t * vm,
393 unformat_input_t * input,
394 vlib_cli_command_t * cmd)
396 snat_main_t *sm = &snat_main;
398 vlib_cli_output (vm, "NAT address and port: %U",
399 format_nat_addr_and_port_alloc_alg,
400 sm->addr_and_port_alloc_alg);
401 switch (sm->addr_and_port_alloc_alg)
403 case NAT_ADDR_AND_PORT_ALLOC_ALG_MAPE:
404 vlib_cli_output (vm, " psid %d psid-offset %d psid-len %d", sm->psid,
405 sm->psid_offset, sm->psid_length);
407 case NAT_ADDR_AND_PORT_ALLOC_ALG_RANGE:
408 vlib_cli_output (vm, " start-port %d end-port %d", sm->start_port,
418 static clib_error_t *
419 nat_set_mss_clamping_command_fn (vlib_main_t * vm, unformat_input_t * input,
420 vlib_cli_command_t * cmd)
422 unformat_input_t _line_input, *line_input = &_line_input;
423 snat_main_t *sm = &snat_main;
424 clib_error_t *error = 0;
427 /* Get a line of input. */
428 if (!unformat_user (input, unformat_line_input, line_input))
431 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
433 if (unformat (line_input, "disable"))
434 sm->mss_clamping = 0;
435 else if (unformat (line_input, "%d", &mss))
436 sm->mss_clamping = (u16) mss;
439 error = clib_error_return (0, "unknown input '%U'",
440 format_unformat_error, line_input);
446 unformat_free (line_input);
451 static clib_error_t *
452 nat_show_mss_clamping_command_fn (vlib_main_t * vm, unformat_input_t * input,
453 vlib_cli_command_t * cmd)
455 snat_main_t *sm = &snat_main;
457 if (sm->mss_clamping)
458 vlib_cli_output (vm, "mss-clamping %d", sm->mss_clamping);
460 vlib_cli_output (vm, "mss-clamping disabled");
465 static clib_error_t *
466 nat_ha_failover_command_fn (vlib_main_t * vm, unformat_input_t * input,
467 vlib_cli_command_t * cmd)
469 unformat_input_t _line_input, *line_input = &_line_input;
471 u32 port, session_refresh_interval = 10;
473 clib_error_t *error = 0;
475 /* Get a line of input. */
476 if (!unformat_user (input, unformat_line_input, line_input))
479 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
481 if (unformat (line_input, "%U:%u", unformat_ip4_address, &addr, &port))
485 (line_input, "refresh-interval %u", &session_refresh_interval))
489 error = clib_error_return (0, "unknown input '%U'",
490 format_unformat_error, line_input);
495 rv = nat_ha_set_failover (&addr, (u16) port, session_refresh_interval);
497 error = clib_error_return (0, "set HA failover failed");
500 unformat_free (line_input);
505 static clib_error_t *
506 nat_ha_listener_command_fn (vlib_main_t * vm, unformat_input_t * input,
507 vlib_cli_command_t * cmd)
509 unformat_input_t _line_input, *line_input = &_line_input;
511 u32 port, path_mtu = 512;
513 clib_error_t *error = 0;
515 /* Get a line of input. */
516 if (!unformat_user (input, unformat_line_input, line_input))
519 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
521 if (unformat (line_input, "%U:%u", unformat_ip4_address, &addr, &port))
523 else if (unformat (line_input, "path-mtu %u", &path_mtu))
527 error = clib_error_return (0, "unknown input '%U'",
528 format_unformat_error, line_input);
533 rv = nat_ha_set_listener (&addr, (u16) port, path_mtu);
535 error = clib_error_return (0, "set HA listener failed");
538 unformat_free (line_input);
543 static clib_error_t *
544 nat_show_ha_command_fn (vlib_main_t * vm, unformat_input_t * input,
545 vlib_cli_command_t * cmd)
549 u32 path_mtu, session_refresh_interval, resync_ack_missed;
552 nat_ha_get_listener (&addr, &port, &path_mtu);
555 vlib_cli_output (vm, "NAT HA disabled\n");
559 vlib_cli_output (vm, "LISTENER:\n");
560 vlib_cli_output (vm, " %U:%u path-mtu %u\n",
561 format_ip4_address, &addr, port, path_mtu);
563 nat_ha_get_failover (&addr, &port, &session_refresh_interval);
564 vlib_cli_output (vm, "FAILOVER:\n");
566 vlib_cli_output (vm, " %U:%u refresh-interval %usec\n",
567 format_ip4_address, &addr, port,
568 session_refresh_interval);
570 vlib_cli_output (vm, " NA\n");
572 nat_ha_get_resync_status (&in_resync, &resync_ack_missed);
573 vlib_cli_output (vm, "RESYNC:\n");
575 vlib_cli_output (vm, " in progress\n");
577 vlib_cli_output (vm, " completed (%d ACK missed)\n", resync_ack_missed);
582 static clib_error_t *
583 nat_ha_flush_command_fn (vlib_main_t * vm, unformat_input_t * input,
584 vlib_cli_command_t * cmd)
590 static clib_error_t *
591 nat_ha_resync_command_fn (vlib_main_t * vm, unformat_input_t * input,
592 vlib_cli_command_t * cmd)
594 clib_error_t *error = 0;
596 if (nat_ha_resync (0, 0, 0))
597 error = clib_error_return (0, "NAT HA resync already running");
602 static clib_error_t *
603 add_address_command_fn (vlib_main_t * vm,
604 unformat_input_t * input, vlib_cli_command_t * cmd)
606 unformat_input_t _line_input, *line_input = &_line_input;
607 snat_main_t *sm = &snat_main;
608 ip4_address_t start_addr, end_addr, this_addr;
609 u32 start_host_order, end_host_order;
614 clib_error_t *error = 0;
617 /* Get a line of input. */
618 if (!unformat_user (input, unformat_line_input, line_input))
621 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
623 if (unformat (line_input, "%U - %U",
624 unformat_ip4_address, &start_addr,
625 unformat_ip4_address, &end_addr))
627 else if (unformat (line_input, "tenant-vrf %u", &vrf_id))
629 else if (unformat (line_input, "%U", unformat_ip4_address, &start_addr))
630 end_addr = start_addr;
631 else if (unformat (line_input, "twice-nat"))
633 else if (unformat (line_input, "del"))
637 error = clib_error_return (0, "unknown input '%U'",
638 format_unformat_error, line_input);
643 if (sm->static_mapping_only)
645 error = clib_error_return (0, "static mapping only mode");
649 start_host_order = clib_host_to_net_u32 (start_addr.as_u32);
650 end_host_order = clib_host_to_net_u32 (end_addr.as_u32);
652 if (end_host_order < start_host_order)
654 error = clib_error_return (0, "end address less than start address");
658 count = (end_host_order - start_host_order) + 1;
661 nat_log_info ("%U - %U, %d addresses...",
662 format_ip4_address, &start_addr,
663 format_ip4_address, &end_addr, count);
665 this_addr = start_addr;
667 for (i = 0; i < count; i++)
670 rv = snat_add_address (sm, &this_addr, vrf_id, twice_nat);
672 rv = snat_del_address (sm, this_addr, 0, twice_nat);
676 case VNET_API_ERROR_VALUE_EXIST:
677 error = clib_error_return (0, "NAT address already in use.");
679 case VNET_API_ERROR_NO_SUCH_ENTRY:
680 error = clib_error_return (0, "NAT address not exist.");
682 case VNET_API_ERROR_UNSPECIFIED:
684 clib_error_return (0, "NAT address used in static mapping.");
686 case VNET_API_ERROR_FEATURE_DISABLED:
688 clib_error_return (0,
689 "twice NAT available only for endpoint-dependent mode.");
696 nat44_add_del_address_dpo (this_addr, is_add);
698 increment_v4_address (&this_addr);
702 unformat_free (line_input);
708 nat44_show_lru_summary (vlib_main_t * vm, snat_main_per_thread_data_t * tsm,
709 u64 now, u64 sess_timeout_time)
711 snat_main_t *sm = &snat_main;
712 dlist_elt_t *oldest_elt;
718 clib_dlist_remove_head (tsm->lru_pool, tsm->n##_lru_head_index); \
719 if (~0 != oldest_index) \
721 oldest_elt = pool_elt_at_index (tsm->lru_pool, oldest_index); \
722 s = pool_elt_at_index (tsm->sessions, oldest_elt->value); \
723 sess_timeout_time = \
724 s->last_heard + (f64)nat44_session_get_timeout (sm, s); \
725 vlib_cli_output (vm, d " LRU min session timeout %llu (now %llu)", \
726 sess_timeout_time, now); \
727 clib_dlist_addhead (tsm->lru_pool, tsm->n##_lru_head_index, \
730 _(tcp_estab, "established tcp");
731 _(tcp_trans, "transitory tcp");
733 _(unk_proto, "unknown protocol");
738 static clib_error_t *
739 nat44_show_summary_command_fn (vlib_main_t * vm, unformat_input_t * input,
740 vlib_cli_command_t * cmd)
742 snat_main_per_thread_data_t *tsm;
743 snat_main_t *sm = &snat_main;
746 if (!sm->endpoint_dependent)
747 return clib_error_return (0, SUPPORTED_ONLY_IN_ED_MODE_STR);
751 u64 now = vlib_time_now (vm);
752 u64 sess_timeout_time = 0;
754 u32 udp_sessions = 0;
755 u32 tcp_sessions = 0;
756 u32 icmp_sessions = 0;
760 u32 transitory_wait_closed = 0;
761 u32 transitory_closed = 0;
766 for (fib = 0; fib < vec_len (sm->max_translations_per_fib); fib++)
767 vlib_cli_output (vm, "max translations per thread: %u fib %u",
768 sm->max_translations_per_fib[fib], fib);
770 if (sm->num_workers > 1)
773 vec_foreach (tsm, sm->per_thread_data)
775 pool_foreach (s, tsm->sessions)
777 sess_timeout_time = s->last_heard +
778 (f64) nat44_session_get_timeout (sm, s);
779 if (now >= sess_timeout_time)
782 switch (s->nat_proto)
784 case NAT_PROTOCOL_ICMP:
787 case NAT_PROTOCOL_TCP:
791 if (s->tcp_closed_timestamp)
793 if (now >= s->tcp_closed_timestamp)
799 ++transitory_wait_closed;
807 case NAT_PROTOCOL_UDP:
813 nat44_show_lru_summary (vm, tsm, now, sess_timeout_time);
814 count += pool_elts (tsm->sessions);
820 tsm = vec_elt_at_index (sm->per_thread_data, sm->num_workers);
822 pool_foreach (s, tsm->sessions)
824 sess_timeout_time = s->last_heard +
825 (f64) nat44_session_get_timeout (sm, s);
826 if (now >= sess_timeout_time)
829 switch (s->nat_proto)
831 case NAT_PROTOCOL_ICMP:
834 case NAT_PROTOCOL_TCP:
838 if (s->tcp_closed_timestamp)
840 if (now >= s->tcp_closed_timestamp)
846 ++transitory_wait_closed;
854 case NAT_PROTOCOL_UDP:
861 nat44_show_lru_summary (vm, tsm, now, sess_timeout_time);
862 count = pool_elts (tsm->sessions);
865 vlib_cli_output (vm, "total timed out sessions: %u", timed_out);
866 vlib_cli_output (vm, "total sessions: %u", count);
867 vlib_cli_output (vm, "total tcp sessions: %u", tcp_sessions);
868 vlib_cli_output (vm, "total tcp established sessions: %u", established);
869 vlib_cli_output (vm, "total tcp transitory sessions: %u", transitory);
870 vlib_cli_output (vm, "total tcp transitory (WAIT-CLOSED) sessions: %u",
871 transitory_wait_closed);
872 vlib_cli_output (vm, "total tcp transitory (CLOSED) sessions: %u",
874 vlib_cli_output (vm, "total udp sessions: %u", udp_sessions);
875 vlib_cli_output (vm, "total icmp sessions: %u", icmp_sessions);
879 static clib_error_t *
880 nat44_show_addresses_command_fn (vlib_main_t * vm, unformat_input_t * input,
881 vlib_cli_command_t * cmd)
883 snat_main_t *sm = &snat_main;
886 vlib_cli_output (vm, "NAT44 pool addresses:");
888 vec_foreach (ap, sm->addresses)
890 vlib_cli_output (vm, "%U", format_ip4_address, &ap->addr);
891 if (ap->fib_index != ~0)
892 vlib_cli_output (vm, " tenant VRF: %u",
893 fib_table_get(ap->fib_index, FIB_PROTOCOL_IP4)->ft_table_id);
895 vlib_cli_output (vm, " tenant VRF independent");
896 #define _(N, i, n, s) \
897 vlib_cli_output (vm, " %d busy %s ports", ap->busy_##n##_ports, s);
901 vlib_cli_output (vm, "NAT44 twice-nat pool addresses:");
902 vec_foreach (ap, sm->twice_nat_addresses)
904 vlib_cli_output (vm, "%U", format_ip4_address, &ap->addr);
905 if (ap->fib_index != ~0)
906 vlib_cli_output (vm, " tenant VRF: %u",
907 fib_table_get(ap->fib_index, FIB_PROTOCOL_IP4)->ft_table_id);
909 vlib_cli_output (vm, " tenant VRF independent");
910 #define _(N, i, n, s) \
911 vlib_cli_output (vm, " %d busy %s ports", ap->busy_##n##_ports, s);
919 static clib_error_t *
920 snat_feature_command_fn (vlib_main_t * vm,
921 unformat_input_t * input, vlib_cli_command_t * cmd)
923 unformat_input_t _line_input, *line_input = &_line_input;
924 vnet_main_t *vnm = vnet_get_main ();
925 clib_error_t *error = 0;
927 u32 *inside_sw_if_indices = 0;
928 u32 *outside_sw_if_indices = 0;
929 u8 is_output_feature = 0;
935 /* Get a line of input. */
936 if (!unformat_user (input, unformat_line_input, line_input))
939 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
941 if (unformat (line_input, "in %U", unformat_vnet_sw_interface,
943 vec_add1 (inside_sw_if_indices, sw_if_index);
944 else if (unformat (line_input, "out %U", unformat_vnet_sw_interface,
946 vec_add1 (outside_sw_if_indices, sw_if_index);
947 else if (unformat (line_input, "output-feature"))
948 is_output_feature = 1;
949 else if (unformat (line_input, "del"))
953 error = clib_error_return (0, "unknown input '%U'",
954 format_unformat_error, line_input);
959 if (vec_len (inside_sw_if_indices))
961 for (i = 0; i < vec_len (inside_sw_if_indices); i++)
963 sw_if_index = inside_sw_if_indices[i];
964 if (is_output_feature)
966 if (snat_interface_add_del_output_feature
967 (sw_if_index, 1, is_del))
969 error = clib_error_return (0, "%s %U failed",
970 is_del ? "del" : "add",
971 format_vnet_sw_if_index_name,
978 if (snat_interface_add_del (sw_if_index, 1, is_del))
980 error = clib_error_return (0, "%s %U failed",
981 is_del ? "del" : "add",
982 format_vnet_sw_if_index_name,
990 if (vec_len (outside_sw_if_indices))
992 for (i = 0; i < vec_len (outside_sw_if_indices); i++)
994 sw_if_index = outside_sw_if_indices[i];
995 if (is_output_feature)
997 if (snat_interface_add_del_output_feature
998 (sw_if_index, 0, is_del))
1000 error = clib_error_return (0, "%s %U failed",
1001 is_del ? "del" : "add",
1002 format_vnet_sw_if_index_name,
1009 if (snat_interface_add_del (sw_if_index, 0, is_del))
1011 error = clib_error_return (0, "%s %U failed",
1012 is_del ? "del" : "add",
1013 format_vnet_sw_if_index_name,
1022 unformat_free (line_input);
1023 vec_free (inside_sw_if_indices);
1024 vec_free (outside_sw_if_indices);
1029 static clib_error_t *
1030 nat44_show_interfaces_command_fn (vlib_main_t * vm, unformat_input_t * input,
1031 vlib_cli_command_t * cmd)
1033 snat_main_t *sm = &snat_main;
1034 snat_interface_t *i;
1035 vnet_main_t *vnm = vnet_get_main ();
1037 vlib_cli_output (vm, "NAT44 interfaces:");
1039 pool_foreach (i, sm->interfaces)
1041 vlib_cli_output (vm, " %U %s", format_vnet_sw_if_index_name, vnm,
1043 (nat_interface_is_inside(i) &&
1044 nat_interface_is_outside(i)) ? "in out" :
1045 (nat_interface_is_inside(i) ? "in" : "out"));
1048 pool_foreach (i, sm->output_feature_interfaces)
1050 vlib_cli_output (vm, " %U output-feature %s",
1051 format_vnet_sw_if_index_name, vnm,
1053 (nat_interface_is_inside(i) &&
1054 nat_interface_is_outside(i)) ? "in out" :
1055 (nat_interface_is_inside(i) ? "in" : "out"));
1062 static clib_error_t *
1063 add_static_mapping_command_fn (vlib_main_t * vm,
1064 unformat_input_t * input,
1065 vlib_cli_command_t * cmd)
1067 unformat_input_t _line_input, *line_input = &_line_input;
1068 clib_error_t *error = 0;
1069 ip4_address_t l_addr, e_addr, exact_addr;
1070 u32 l_port = 0, e_port = 0, vrf_id = ~0;
1071 int is_add = 1, addr_only = 1, rv, exact = 0;
1072 u32 sw_if_index = ~0;
1073 vnet_main_t *vnm = vnet_get_main ();
1074 nat_protocol_t proto = NAT_PROTOCOL_OTHER;
1076 twice_nat_type_t twice_nat = TWICE_NAT_DISABLED;
1079 /* Get a line of input. */
1080 if (!unformat_user (input, unformat_line_input, line_input))
1083 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1085 if (unformat (line_input, "local %U %u", unformat_ip4_address, &l_addr,
1089 if (unformat (line_input, "local %U", unformat_ip4_address, &l_addr))
1091 else if (unformat (line_input, "external %U %u", unformat_ip4_address,
1094 else if (unformat (line_input, "external %U", unformat_ip4_address,
1097 else if (unformat (line_input, "external %U %u",
1098 unformat_vnet_sw_interface, vnm, &sw_if_index,
1101 else if (unformat (line_input, "external %U",
1102 unformat_vnet_sw_interface, vnm, &sw_if_index))
1104 else if (unformat (line_input, "exact %U", unformat_ip4_address,
1107 else if (unformat (line_input, "vrf %u", &vrf_id))
1109 else if (unformat (line_input, "%U", unformat_nat_protocol, &proto))
1111 else if (unformat (line_input, "twice-nat"))
1112 twice_nat = TWICE_NAT;
1113 else if (unformat (line_input, "self-twice-nat"))
1114 twice_nat = TWICE_NAT_SELF;
1115 else if (unformat (line_input, "out2in-only"))
1117 else if (unformat (line_input, "del"))
1121 error = clib_error_return (0, "unknown input: '%U'",
1122 format_unformat_error, line_input);
1127 if (twice_nat && addr_only)
1129 error = clib_error_return (0, "twice NAT only for 1:1 NAPT");
1138 clib_error_return (0,
1139 "address only mapping doesn't support protocol");
1143 else if (!proto_set)
1145 error = clib_error_return (0, "protocol is required");
1149 rv = snat_add_static_mapping (
1150 l_addr, e_addr, clib_host_to_net_u16 (l_port),
1151 clib_host_to_net_u16 (e_port), vrf_id, addr_only, sw_if_index, proto,
1152 is_add, twice_nat, out2in_only, 0, 0, exact_addr, exact);
1156 case VNET_API_ERROR_INVALID_VALUE:
1157 error = clib_error_return (0, "External port already in use.");
1159 case VNET_API_ERROR_NO_SUCH_ENTRY:
1161 error = clib_error_return (0, "External address must be allocated.");
1163 error = clib_error_return (0, "Mapping not exist.");
1165 case VNET_API_ERROR_NO_SUCH_FIB:
1166 error = clib_error_return (0, "No such VRF id.");
1168 case VNET_API_ERROR_VALUE_EXIST:
1169 error = clib_error_return (0, "Mapping already exist.");
1171 case VNET_API_ERROR_FEATURE_DISABLED:
1173 clib_error_return (0,
1174 "twice-nat/out2in-only available only for endpoint-dependent mode.");
1181 unformat_free (line_input);
1186 static clib_error_t *
1187 add_identity_mapping_command_fn (vlib_main_t * vm,
1188 unformat_input_t * input,
1189 vlib_cli_command_t * cmd)
1191 unformat_input_t _line_input, *line_input = &_line_input;
1192 clib_error_t *error = 0;
1193 ip4_address_t addr, pool_addr = { 0 };
1194 u32 port = 0, vrf_id = ~0;
1197 u32 sw_if_index = ~0;
1198 vnet_main_t *vnm = vnet_get_main ();
1200 nat_protocol_t proto;
1204 /* Get a line of input. */
1205 if (!unformat_user (input, unformat_line_input, line_input))
1208 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1210 if (unformat (line_input, "%U", unformat_ip4_address, &addr))
1212 else if (unformat (line_input, "external %U",
1213 unformat_vnet_sw_interface, vnm, &sw_if_index))
1215 else if (unformat (line_input, "vrf %u", &vrf_id))
1217 else if (unformat (line_input, "%U %u", unformat_nat_protocol, &proto,
1220 else if (unformat (line_input, "del"))
1224 error = clib_error_return (0, "unknown input: '%U'",
1225 format_unformat_error, line_input);
1230 rv = snat_add_static_mapping (
1231 addr, addr, clib_host_to_net_u16 (port), clib_host_to_net_u16 (port),
1232 vrf_id, addr_only, sw_if_index, proto, is_add, 0, 0, 0, 1, pool_addr, 0);
1236 case VNET_API_ERROR_INVALID_VALUE:
1237 error = clib_error_return (0, "External port already in use.");
1239 case VNET_API_ERROR_NO_SUCH_ENTRY:
1241 error = clib_error_return (0, "External address must be allocated.");
1243 error = clib_error_return (0, "Mapping not exist.");
1245 case VNET_API_ERROR_NO_SUCH_FIB:
1246 error = clib_error_return (0, "No such VRF id.");
1248 case VNET_API_ERROR_VALUE_EXIST:
1249 error = clib_error_return (0, "Mapping already exist.");
1256 unformat_free (line_input);
1261 static clib_error_t *
1262 add_lb_static_mapping_command_fn (vlib_main_t * vm,
1263 unformat_input_t * input,
1264 vlib_cli_command_t * cmd)
1266 unformat_input_t _line_input, *line_input = &_line_input;
1267 clib_error_t *error = 0;
1268 ip4_address_t l_addr, e_addr;
1269 u32 l_port = 0, e_port = 0, vrf_id = 0, probability = 0, affinity = 0;
1272 nat_protocol_t proto;
1274 nat44_lb_addr_port_t *locals = 0, local;
1275 twice_nat_type_t twice_nat = TWICE_NAT_DISABLED;
1278 /* Get a line of input. */
1279 if (!unformat_user (input, unformat_line_input, line_input))
1282 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1284 if (unformat (line_input, "local %U:%u probability %u",
1285 unformat_ip4_address, &l_addr, &l_port, &probability))
1287 clib_memset (&local, 0, sizeof (local));
1288 local.addr = l_addr;
1289 local.port = (u16) l_port;
1290 local.probability = (u8) probability;
1291 vec_add1 (locals, local);
1293 else if (unformat (line_input, "local %U:%u vrf %u probability %u",
1294 unformat_ip4_address, &l_addr, &l_port, &vrf_id,
1297 clib_memset (&local, 0, sizeof (local));
1298 local.addr = l_addr;
1299 local.port = (u16) l_port;
1300 local.probability = (u8) probability;
1301 local.vrf_id = vrf_id;
1302 vec_add1 (locals, local);
1304 else if (unformat (line_input, "external %U:%u", unformat_ip4_address,
1307 else if (unformat (line_input, "protocol %U", unformat_nat_protocol,
1310 else if (unformat (line_input, "twice-nat"))
1311 twice_nat = TWICE_NAT;
1312 else if (unformat (line_input, "self-twice-nat"))
1313 twice_nat = TWICE_NAT_SELF;
1314 else if (unformat (line_input, "out2in-only"))
1316 else if (unformat (line_input, "del"))
1318 else if (unformat (line_input, "affinity %u", &affinity))
1322 error = clib_error_return (0, "unknown input: '%U'",
1323 format_unformat_error, line_input);
1328 if (vec_len (locals) < 2)
1330 error = clib_error_return (0, "at least two local must be set");
1336 error = clib_error_return (0, "missing protocol");
1340 rv = nat44_add_del_lb_static_mapping (e_addr, (u16) e_port, proto, locals,
1341 is_add, twice_nat, out2in_only, 0,
1346 case VNET_API_ERROR_INVALID_VALUE:
1347 error = clib_error_return (0, "External port already in use.");
1349 case VNET_API_ERROR_NO_SUCH_ENTRY:
1351 error = clib_error_return (0, "External address must be allocated.");
1353 error = clib_error_return (0, "Mapping not exist.");
1355 case VNET_API_ERROR_VALUE_EXIST:
1356 error = clib_error_return (0, "Mapping already exist.");
1358 case VNET_API_ERROR_FEATURE_DISABLED:
1360 clib_error_return (0, "Available only for endpoint-dependent mode.");
1367 unformat_free (line_input);
1373 static clib_error_t *
1374 add_lb_backend_command_fn (vlib_main_t * vm,
1375 unformat_input_t * input, vlib_cli_command_t * cmd)
1377 unformat_input_t _line_input, *line_input = &_line_input;
1378 clib_error_t *error = 0;
1379 ip4_address_t l_addr, e_addr;
1380 u32 l_port = 0, e_port = 0, vrf_id = 0, probability = 0;
1383 nat_protocol_t proto;
1386 /* Get a line of input. */
1387 if (!unformat_user (input, unformat_line_input, line_input))
1390 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1392 if (unformat (line_input, "local %U:%u probability %u",
1393 unformat_ip4_address, &l_addr, &l_port, &probability))
1395 else if (unformat (line_input, "local %U:%u vrf %u probability %u",
1396 unformat_ip4_address, &l_addr, &l_port, &vrf_id,
1399 else if (unformat (line_input, "external %U:%u", unformat_ip4_address,
1402 else if (unformat (line_input, "protocol %U", unformat_nat_protocol,
1405 else if (unformat (line_input, "del"))
1409 error = clib_error_return (0, "unknown input: '%U'",
1410 format_unformat_error, line_input);
1415 if (!l_port || !e_port)
1417 error = clib_error_return (0, "local or external must be set");
1423 error = clib_error_return (0, "missing protocol");
1428 nat44_lb_static_mapping_add_del_local (e_addr, (u16) e_port, l_addr,
1429 l_port, proto, vrf_id, probability,
1434 case VNET_API_ERROR_INVALID_VALUE:
1435 error = clib_error_return (0, "External is not load-balancing static "
1438 case VNET_API_ERROR_NO_SUCH_ENTRY:
1439 error = clib_error_return (0, "Mapping or back-end not exist.");
1441 case VNET_API_ERROR_VALUE_EXIST:
1442 error = clib_error_return (0, "Back-end already exist.");
1444 case VNET_API_ERROR_FEATURE_DISABLED:
1446 clib_error_return (0, "Available only for endpoint-dependent mode.");
1448 case VNET_API_ERROR_UNSPECIFIED:
1449 error = clib_error_return (0, "At least two back-ends must remain");
1456 unformat_free (line_input);
1461 static clib_error_t *
1462 nat44_show_static_mappings_command_fn (vlib_main_t * vm,
1463 unformat_input_t * input,
1464 vlib_cli_command_t * cmd)
1466 snat_main_t *sm = &snat_main;
1467 snat_static_mapping_t *m;
1468 snat_static_map_resolve_t *rp;
1470 vlib_cli_output (vm, "NAT44 static mappings:");
1472 pool_foreach (m, sm->static_mappings)
1474 vlib_cli_output (vm, " %U", format_snat_static_mapping, m);
1476 vec_foreach (rp, sm->to_resolve)
1477 vlib_cli_output (vm, " %U", format_snat_static_map_to_resolve, rp);
1483 static clib_error_t *
1484 snat_add_interface_address_command_fn (vlib_main_t * vm,
1485 unformat_input_t * input,
1486 vlib_cli_command_t * cmd)
1488 snat_main_t *sm = &snat_main;
1489 unformat_input_t _line_input, *line_input = &_line_input;
1493 clib_error_t *error = 0;
1496 /* Get a line of input. */
1497 if (!unformat_user (input, unformat_line_input, line_input))
1500 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1502 if (unformat (line_input, "%U", unformat_vnet_sw_interface,
1503 sm->vnet_main, &sw_if_index))
1505 else if (unformat (line_input, "twice-nat"))
1507 else if (unformat (line_input, "del"))
1511 error = clib_error_return (0, "unknown input '%U'",
1512 format_unformat_error, line_input);
1517 rv = snat_add_interface_address (sm, sw_if_index, is_del, twice_nat);
1525 error = clib_error_return (0, "snat_add_interface_address returned %d",
1531 unformat_free (line_input);
1536 static clib_error_t *
1537 nat44_show_interface_address_command_fn (vlib_main_t * vm,
1538 unformat_input_t * input,
1539 vlib_cli_command_t * cmd)
1541 snat_main_t *sm = &snat_main;
1542 vnet_main_t *vnm = vnet_get_main ();
1546 vlib_cli_output (vm, "NAT44 pool address interfaces:");
1547 vec_foreach (sw_if_index, sm->auto_add_sw_if_indices)
1549 vlib_cli_output (vm, " %U", format_vnet_sw_if_index_name, vnm,
1552 vlib_cli_output (vm, "NAT44 twice-nat pool address interfaces:");
1553 vec_foreach (sw_if_index, sm->auto_add_sw_if_indices_twice_nat)
1555 vlib_cli_output (vm, " %U", format_vnet_sw_if_index_name, vnm,
1563 static clib_error_t *
1564 nat44_show_sessions_command_fn (vlib_main_t * vm, unformat_input_t * input,
1565 vlib_cli_command_t * cmd)
1567 unformat_input_t _line_input, *line_input = &_line_input;
1568 clib_error_t *error = 0;
1570 snat_main_per_thread_data_t *tsm;
1571 snat_main_t *sm = &snat_main;
1576 if (!unformat_user (input, unformat_line_input, line_input))
1579 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1581 if (unformat (line_input, "detail"))
1585 error = clib_error_return (0, "unknown input '%U'",
1586 format_unformat_error, line_input);
1590 unformat_free (line_input);
1593 if (!sm->endpoint_dependent)
1594 vlib_cli_output (vm, "NAT44 sessions:");
1596 vlib_cli_output (vm, "NAT44 ED sessions:");
1599 vec_foreach_index (i, sm->per_thread_data)
1601 tsm = vec_elt_at_index (sm->per_thread_data, i);
1603 vlib_cli_output (vm, "-------- thread %d %s: %d sessions --------\n",
1604 i, vlib_worker_threads[i].name,
1605 pool_elts (tsm->sessions));
1607 if (!sm->endpoint_dependent)
1610 pool_foreach (u, tsm->users)
1612 vlib_cli_output (vm, " %U", format_snat_user, tsm, u, detail);
1618 pool_foreach (s, tsm->sessions)
1620 vlib_cli_output (vm, " %U\n", format_snat_session, tsm, s);
1628 static clib_error_t *
1629 nat44_set_session_limit_command_fn (vlib_main_t * vm,
1630 unformat_input_t * input,
1631 vlib_cli_command_t * cmd)
1633 unformat_input_t _line_input, *line_input = &_line_input;
1634 clib_error_t *error = 0;
1636 u32 session_limit = 0, vrf_id = 0;
1638 /* Get a line of input. */
1639 if (!unformat_user (input, unformat_line_input, line_input))
1642 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1644 if (unformat (line_input, "%u", &session_limit))
1646 else if (unformat (line_input, "vrf %u", &vrf_id))
1650 error = clib_error_return (0, "unknown input '%U'",
1651 format_unformat_error, line_input);
1657 error = clib_error_return (0, "missing value of session limit");
1658 else if (nat44_update_session_limit (session_limit, vrf_id))
1659 error = clib_error_return (0, "nat44_set_session_limit failed");
1662 unformat_free (line_input);
1667 static clib_error_t *
1668 nat44_del_user_command_fn (vlib_main_t * vm,
1669 unformat_input_t * input, vlib_cli_command_t * cmd)
1671 snat_main_t *sm = &snat_main;
1672 unformat_input_t _line_input, *line_input = &_line_input;
1673 clib_error_t *error = 0;
1678 if (sm->endpoint_dependent)
1679 return clib_error_return (0, UNSUPPORTED_IN_ED_MODE_STR);
1681 /* Get a line of input. */
1682 if (!unformat_user (input, unformat_line_input, line_input))
1685 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1687 if (unformat (line_input, "%U", unformat_ip4_address, &addr))
1689 else if (unformat (line_input, "fib %u", &fib_index))
1693 error = clib_error_return (0, "unknown input '%U'",
1694 format_unformat_error, line_input);
1699 rv = nat44_ei_user_del (&addr, fib_index);
1703 error = clib_error_return (0, "nat44_ei_user_del returned %d", rv);
1707 unformat_free (line_input);
1712 static clib_error_t *
1713 nat44_clear_sessions_command_fn (vlib_main_t * vm,
1714 unformat_input_t * input,
1715 vlib_cli_command_t * cmd)
1717 clib_error_t *error = 0;
1718 nat44_sessions_clear ();
1722 static clib_error_t *
1723 nat44_del_session_command_fn (vlib_main_t * vm,
1724 unformat_input_t * input,
1725 vlib_cli_command_t * cmd)
1727 snat_main_t *sm = &snat_main;
1728 unformat_input_t _line_input, *line_input = &_line_input;
1729 int is_in = 0, is_ed = 0;
1730 clib_error_t *error = 0;
1731 ip4_address_t addr, eh_addr;
1732 u32 port = 0, eh_port = 0, vrf_id = sm->outside_vrf_id;
1733 nat_protocol_t proto;
1736 /* Get a line of input. */
1737 if (!unformat_user (input, unformat_line_input, line_input))
1740 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1743 (line_input, "%U:%u %U", unformat_ip4_address, &addr, &port,
1744 unformat_nat_protocol, &proto))
1746 else if (unformat (line_input, "in"))
1749 vrf_id = sm->inside_vrf_id;
1751 else if (unformat (line_input, "out"))
1754 vrf_id = sm->outside_vrf_id;
1756 else if (unformat (line_input, "vrf %u", &vrf_id))
1760 (line_input, "external-host %U:%u", unformat_ip4_address,
1761 &eh_addr, &eh_port))
1765 error = clib_error_return (0, "unknown input '%U'",
1766 format_unformat_error, line_input);
1773 nat44_del_ed_session (sm, &addr, clib_host_to_net_u16 (port), &eh_addr,
1774 clib_host_to_net_u16 (eh_port),
1775 nat_proto_to_ip_proto (proto), vrf_id, is_in);
1777 rv = nat44_ei_del_session (sm, &addr, clib_host_to_net_u16 (port), proto,
1786 error = clib_error_return (0, "nat44_del_session returned %d", rv);
1791 unformat_free (line_input);
1796 static clib_error_t *
1797 snat_forwarding_set_command_fn (vlib_main_t * vm,
1798 unformat_input_t * input,
1799 vlib_cli_command_t * cmd)
1801 snat_main_t *sm = &snat_main;
1802 unformat_input_t _line_input, *line_input = &_line_input;
1803 u8 forwarding_enable;
1804 u8 forwarding_enable_set = 0;
1805 clib_error_t *error = 0;
1807 /* Get a line of input. */
1808 if (!unformat_user (input, unformat_line_input, line_input))
1809 return clib_error_return (0, "'enable' or 'disable' expected");
1811 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1813 if (!forwarding_enable_set && unformat (line_input, "enable"))
1815 forwarding_enable = 1;
1816 forwarding_enable_set = 1;
1818 else if (!forwarding_enable_set && unformat (line_input, "disable"))
1820 forwarding_enable = 0;
1821 forwarding_enable_set = 1;
1825 error = clib_error_return (0, "unknown input '%U'",
1826 format_unformat_error, line_input);
1831 if (!forwarding_enable_set)
1833 error = clib_error_return (0, "'enable' or 'disable' expected");
1837 sm->forwarding_enabled = forwarding_enable;
1840 unformat_free (line_input);
1845 static clib_error_t *
1846 set_timeout_command_fn (vlib_main_t * vm,
1847 unformat_input_t * input, vlib_cli_command_t * cmd)
1849 snat_main_t *sm = &snat_main;
1850 unformat_input_t _line_input, *line_input = &_line_input;
1851 clib_error_t *error = 0;
1853 /* Get a line of input. */
1854 if (!unformat_user (input, unformat_line_input, line_input))
1857 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1859 if (unformat (line_input, "udp %u", &sm->timeouts.udp));
1860 else if (unformat (line_input, "tcp-established %u",
1861 &sm->timeouts.tcp.established));
1862 else if (unformat (line_input, "tcp-transitory %u",
1863 &sm->timeouts.tcp.transitory));
1864 else if (unformat (line_input, "icmp %u", &sm->timeouts.icmp));
1865 else if (unformat (line_input, "reset"))
1866 nat_reset_timeouts (&sm->timeouts);
1869 error = clib_error_return (0, "unknown input '%U'",
1870 format_unformat_error, line_input);
1875 unformat_free (line_input);
1879 static clib_error_t *
1880 nat_show_timeouts_command_fn (vlib_main_t * vm,
1881 unformat_input_t * input,
1882 vlib_cli_command_t * cmd)
1884 snat_main_t *sm = &snat_main;
1886 vlib_cli_output (vm, "udp timeout: %dsec", sm->timeouts.udp);
1887 vlib_cli_output (vm, "tcp-established timeout: %dsec",
1888 sm->timeouts.tcp.established);
1889 vlib_cli_output (vm, "tcp-transitory timeout: %dsec",
1890 sm->timeouts.tcp.transitory);
1891 vlib_cli_output (vm, "icmp timeout: %dsec", sm->timeouts.icmp);
1896 static clib_error_t *
1897 nat44_debug_fib_expire_command_fn (vlib_main_t * vm,
1898 unformat_input_t * input,
1899 vlib_cli_command_t * cmd)
1901 unformat_input_t _line_input, *line_input = &_line_input;
1902 clib_error_t *error = 0;
1905 /* Get a line of input. */
1906 if (!unformat_user (input, unformat_line_input, line_input))
1909 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1911 if (unformat (line_input, "%u", &fib))
1915 error = clib_error_return (0, "unknown input '%U'",
1916 format_unformat_error, line_input);
1920 expire_per_vrf_sessions (fib);
1922 unformat_free (line_input);
1926 static clib_error_t *
1927 nat44_debug_fib_registration_command_fn (vlib_main_t * vm,
1928 unformat_input_t * input,
1929 vlib_cli_command_t * cmd)
1931 snat_main_t *sm = &snat_main;
1932 snat_main_per_thread_data_t *tsm;
1933 per_vrf_sessions_t *per_vrf_sessions;
1935 vlib_cli_output (vm, "VRF registration debug:");
1936 vec_foreach (tsm, sm->per_thread_data)
1938 vlib_cli_output (vm, "thread %u:", tsm->thread_index);
1939 vec_foreach (per_vrf_sessions, tsm->per_vrf_sessions_vec)
1941 vlib_cli_output (vm, "rx fib %u tx fib %u ses count %u %s",
1942 per_vrf_sessions->rx_fib_index,
1943 per_vrf_sessions->tx_fib_index,
1944 per_vrf_sessions->ses_count,
1945 per_vrf_sessions->expired ? "expired" : "");
1955 VLIB_CLI_COMMAND (nat44_debug_fib_expire_command, static) = {
1956 .path = "debug nat44 fib expire",
1957 .short_help = "debug nat44 fib expire <fib-index>",
1958 .function = nat44_debug_fib_expire_command_fn,
1963 VLIB_CLI_COMMAND (nat44_debug_fib_registration_command, static) = {
1964 .path = "debug nat44 fib registration",
1965 .short_help = "debug nat44 fib registration",
1966 .function = nat44_debug_fib_registration_command_fn,
1971 * @cliexstart{nat44 enable}
1972 * Enable nat44 plugin
1973 * To enable nat44, use:
1974 * vpp# nat44 enable sessions <n>
1975 * To enable nat44 static mapping only, use:
1976 * vpp# nat44 enable sessions <n> static-mapping
1977 * To enable nat44 static mapping with connection tracking, use:
1978 * vpp# nat44 enable sessions <n> static-mapping connection-tracking
1979 * To enable nat44 out2in dpo, use:
1980 * vpp# nat44 enable sessions <n> out2in-dpo
1981 * To enable nat44 endpoint-dependent, use:
1982 * vpp# nat44 enable sessions <n> endpoint-dependent
1983 * To set inside-vrf outside-vrf, use:
1984 * vpp# nat44 enable sessions <n> inside-vrf <id> outside-vrf <id>
1987 VLIB_CLI_COMMAND (nat44_enable_command, static) = {
1988 .path = "nat44 enable",
1989 .short_help = "nat44 enable sessions <max-number> [users <max-number>] [static-mappig-only [connection-tracking]|out2in-dpo|endpoint-dependent] [inside-vrf <vrf-id>] [outside-vrf <vrf-id>] [user-sessions <max-number>]",
1990 .function = nat44_enable_command_fn,
1995 * @cliexstart{nat44 disable}
1996 * Disable nat44 plugin
1997 * To disable nat44, use:
1998 * vpp# nat44 disable
2001 VLIB_CLI_COMMAND (nat44_disable_command, static) = {
2002 .path = "nat44 disable",
2003 .short_help = "nat44 disable",
2004 .function = nat44_disable_command_fn,
2009 * @cliexstart{set snat workers}
2010 * Set NAT workers if 2 or more workers available, use:
2011 * vpp# set snat workers 0-2,5
2014 VLIB_CLI_COMMAND (set_workers_command, static) = {
2015 .path = "set nat workers",
2016 .function = set_workers_command_fn,
2017 .short_help = "set nat workers <workers-list>",
2022 * @cliexstart{show nat workers}
2024 * vpp# show nat workers:
2030 VLIB_CLI_COMMAND (nat_show_workers_command, static) = {
2031 .path = "show nat workers",
2032 .short_help = "show nat workers",
2033 .function = nat_show_workers_commnad_fn,
2038 * @cliexstart{set nat timeout}
2039 * Set values of timeouts for NAT sessions (in seconds), use:
2040 * vpp# set nat timeout udp 120 tcp-established 7500 tcp-transitory 250 icmp 90
2041 * To reset default values use:
2042 * vpp# set nat timeout reset
2045 VLIB_CLI_COMMAND (set_timeout_command, static) = {
2046 .path = "set nat timeout",
2047 .function = set_timeout_command_fn,
2049 "set nat timeout [udp <sec> | tcp-established <sec> "
2050 "tcp-transitory <sec> | icmp <sec> | reset]",
2055 * @cliexstart{show nat timeouts}
2056 * Show values of timeouts for NAT sessions.
2057 * vpp# show nat timeouts
2058 * udp timeout: 300sec
2059 * tcp-established timeout: 7440sec
2060 * tcp-transitory timeout: 240sec
2061 * icmp timeout: 60sec
2064 VLIB_CLI_COMMAND (nat_show_timeouts_command, static) = {
2065 .path = "show nat timeouts",
2066 .short_help = "show nat timeouts",
2067 .function = nat_show_timeouts_command_fn,
2072 * @cliexstart{nat set logging level}
2073 * To set NAT logging level use:
2074 * Set nat logging level
2077 VLIB_CLI_COMMAND (snat_set_log_level_command, static) = {
2078 .path = "nat set logging level",
2079 .function = snat_set_log_level_command_fn,
2080 .short_help = "nat set logging level <level>",
2085 * @cliexstart{snat ipfix logging}
2086 * To enable NAT IPFIX logging use:
2087 * vpp# nat ipfix logging
2088 * To set IPFIX exporter use:
2089 * vpp# set ipfix exporter collector 10.10.10.3 src 10.10.10.1
2092 VLIB_CLI_COMMAND (snat_ipfix_logging_enable_disable_command, static) = {
2093 .path = "nat ipfix logging",
2094 .function = snat_ipfix_logging_enable_disable_command_fn,
2095 .short_help = "nat ipfix logging [domain <domain-id>] [src-port <port>] [disable]",
2100 * @cliexstart{nat addr-port-assignment-alg}
2101 * Set address and port assignment algorithm
2102 * For the MAP-E CE limit port choice based on PSID use:
2103 * vpp# nat addr-port-assignment-alg map-e psid 10 psid-offset 6 psid-len 6
2104 * For port range use:
2105 * vpp# nat addr-port-assignment-alg port-range <start-port> - <end-port>
2106 * To set standard (default) address and port assignment algorithm use:
2107 * vpp# nat addr-port-assignment-alg default
2110 VLIB_CLI_COMMAND (nat44_set_alloc_addr_and_port_alg_command, static) = {
2111 .path = "nat addr-port-assignment-alg",
2112 .short_help = "nat addr-port-assignment-alg <alg-name> [<alg-params>]",
2113 .function = nat44_set_alloc_addr_and_port_alg_command_fn,
2118 * @cliexstart{show nat addr-port-assignment-alg}
2119 * Show address and port assignment algorithm
2122 VLIB_CLI_COMMAND (nat44_show_alloc_addr_and_port_alg_command, static) = {
2123 .path = "show nat addr-port-assignment-alg",
2124 .short_help = "show nat addr-port-assignment-alg",
2125 .function = nat44_show_alloc_addr_and_port_alg_command_fn,
2130 * @cliexstart{nat mss-clamping}
2131 * Set TCP MSS rewriting configuration
2132 * To enable TCP MSS rewriting use:
2133 * vpp# nat mss-clamping 1452
2134 * To disbale TCP MSS rewriting use:
2135 * vpp# nat mss-clamping disable
2138 VLIB_CLI_COMMAND (nat_set_mss_clamping_command, static) = {
2139 .path = "nat mss-clamping",
2140 .short_help = "nat mss-clamping <mss-value>|disable",
2141 .function = nat_set_mss_clamping_command_fn,
2146 * @cliexstart{show nat mss-clamping}
2147 * Show TCP MSS rewriting configuration
2150 VLIB_CLI_COMMAND (nat_show_mss_clamping_command, static) = {
2151 .path = "show nat mss-clamping",
2152 .short_help = "show nat mss-clamping",
2153 .function = nat_show_mss_clamping_command_fn,
2158 * @cliexstart{nat ha failover}
2159 * Set HA failover (remote settings)
2162 VLIB_CLI_COMMAND (nat_ha_failover_command, static) = {
2163 .path = "nat ha failover",
2164 .short_help = "nat ha failover <ip4-address>:<port> [refresh-interval <sec>]",
2165 .function = nat_ha_failover_command_fn,
2170 * @cliexstart{nat ha listener}
2171 * Set HA listener (local settings)
2174 VLIB_CLI_COMMAND (nat_ha_listener_command, static) = {
2175 .path = "nat ha listener",
2176 .short_help = "nat ha listener <ip4-address>:<port> [path-mtu <path-mtu>]",
2177 .function = nat_ha_listener_command_fn,
2182 * @cliexstart{show nat ha}
2183 * Show HA configuration/status
2186 VLIB_CLI_COMMAND (nat_show_ha_command, static) = {
2187 .path = "show nat ha",
2188 .short_help = "show nat ha",
2189 .function = nat_show_ha_command_fn,
2194 * @cliexstart{nat ha flush}
2195 * Flush the current HA data (for testing)
2198 VLIB_CLI_COMMAND (nat_ha_flush_command, static) = {
2199 .path = "nat ha flush",
2200 .short_help = "nat ha flush",
2201 .function = nat_ha_flush_command_fn,
2206 * @cliexstart{nat ha resync}
2207 * Resync HA (resend existing sessions to new failover)
2210 VLIB_CLI_COMMAND (nat_ha_resync_command, static) = {
2211 .path = "nat ha resync",
2212 .short_help = "nat ha resync",
2213 .function = nat_ha_resync_command_fn,
2218 * @cliexstart{show nat44 hash tables}
2219 * Show NAT44 hash tables
2222 VLIB_CLI_COMMAND (nat44_show_hash, static) = {
2223 .path = "show nat44 hash tables",
2224 .short_help = "show nat44 hash tables [detail|verbose]",
2225 .function = nat44_show_hash_command_fn,
2230 * @cliexstart{nat44 add address}
2231 * Add/delete NAT44 pool address.
2232 * To add NAT44 pool address use:
2233 * vpp# nat44 add address 172.16.1.3
2234 * vpp# nat44 add address 172.16.2.2 - 172.16.2.24
2235 * To add NAT44 pool address for specific tenant (identified by VRF id) use:
2236 * vpp# nat44 add address 172.16.1.3 tenant-vrf 10
2239 VLIB_CLI_COMMAND (add_address_command, static) = {
2240 .path = "nat44 add address",
2241 .short_help = "nat44 add address <ip4-range-start> [- <ip4-range-end>] "
2242 "[tenant-vrf <vrf-id>] [twice-nat] [del]",
2243 .function = add_address_command_fn,
2248 * @cliexstart{show nat44 summary}
2249 * Show NAT44 summary
2250 * vpp# show nat44 summary
2253 VLIB_CLI_COMMAND (nat44_show_summary_command, static) = {
2254 .path = "show nat44 summary",
2255 .short_help = "show nat44 summary",
2256 .function = nat44_show_summary_command_fn,
2261 * @cliexstart{show nat44 addresses}
2262 * Show NAT44 pool addresses.
2263 * vpp# show nat44 addresses
2264 * NAT44 pool addresses:
2266 * tenant VRF independent
2275 * NAT44 twice-nat pool addresses:
2277 * tenant VRF independent
2283 VLIB_CLI_COMMAND (nat44_show_addresses_command, static) = {
2284 .path = "show nat44 addresses",
2285 .short_help = "show nat44 addresses",
2286 .function = nat44_show_addresses_command_fn,
2291 * @cliexstart{set interface nat44}
2292 * Enable/disable NAT44 feature on the interface.
2293 * To enable NAT44 feature with local network interface use:
2294 * vpp# set interface nat44 in GigabitEthernet0/8/0
2295 * To enable NAT44 feature with external network interface use:
2296 * vpp# set interface nat44 out GigabitEthernet0/a/0
2299 VLIB_CLI_COMMAND (set_interface_snat_command, static) = {
2300 .path = "set interface nat44",
2301 .function = snat_feature_command_fn,
2302 .short_help = "set interface nat44 in <intfc> out <intfc> [output-feature] "
2308 * @cliexstart{show nat44 interfaces}
2309 * Show interfaces with NAT44 feature.
2310 * vpp# show nat44 interfaces
2312 * GigabitEthernet0/8/0 in
2313 * GigabitEthernet0/a/0 out
2316 VLIB_CLI_COMMAND (nat44_show_interfaces_command, static) = {
2317 .path = "show nat44 interfaces",
2318 .short_help = "show nat44 interfaces",
2319 .function = nat44_show_interfaces_command_fn,
2324 * @cliexstart{nat44 add static mapping}
2325 * Static mapping allows hosts on the external network to initiate connection
2326 * to to the local network host.
2327 * To create static mapping between local host address 10.0.0.3 port 6303 and
2328 * external address 4.4.4.4 port 3606 for TCP protocol use:
2329 * vpp# nat44 add static mapping tcp local 10.0.0.3 6303 external 4.4.4.4 3606
2330 * If not runnig "static mapping only" NAT plugin mode use before:
2331 * vpp# nat44 add address 4.4.4.4
2332 * To create address only static mapping between local and external address use:
2333 * vpp# nat44 add static mapping local 10.0.0.3 external 4.4.4.4
2334 * To create ICMP static mapping between local and external with ICMP echo
2335 * identifier 10 use:
2336 * vpp# nat44 add static mapping icmp local 10.0.0.3 10 external 4.4.4.4 10
2337 * To force use of specific pool address, vrf independent
2338 * vpp# nat44 add static mapping local 10.0.0.2 1234 external 10.0.2.2 1234 twice-nat exact 10.0.1.2
2341 VLIB_CLI_COMMAND (add_static_mapping_command, static) = {
2342 .path = "nat44 add static mapping",
2343 .function = add_static_mapping_command_fn,
2345 "nat44 add static mapping tcp|udp|icmp local <addr> [<port|icmp-echo-id>] "
2346 "external <addr> [<port|icmp-echo-id>] [vrf <table-id>] [twice-nat|self-twice-nat] "
2347 "[out2in-only] [exact <pool-addr>] [del]",
2352 * @cliexstart{nat44 add identity mapping}
2353 * Identity mapping translate an IP address to itself.
2354 * To create identity mapping for address 10.0.0.3 port 6303 for TCP protocol
2356 * vpp# nat44 add identity mapping 10.0.0.3 tcp 6303
2357 * To create identity mapping for address 10.0.0.3 use:
2358 * vpp# nat44 add identity mapping 10.0.0.3
2359 * To create identity mapping for DHCP addressed interface use:
2360 * vpp# nat44 add identity mapping external GigabitEthernet0/a/0 tcp 3606
2363 VLIB_CLI_COMMAND (add_identity_mapping_command, static) = {
2364 .path = "nat44 add identity mapping",
2365 .function = add_identity_mapping_command_fn,
2366 .short_help = "nat44 add identity mapping <ip4-addr>|external <interface> "
2367 "[<protocol> <port>] [vrf <table-id>] [del]",
2372 * @cliexstart{nat44 add load-balancing static mapping}
2373 * Service load balancing using NAT44
2374 * To add static mapping with load balancing for service with external IP
2375 * address 1.2.3.4 and TCP port 80 and mapped to 2 local servers
2376 * 10.100.10.10:8080 and 10.100.10.20:8080 with probability 80% resp. 20% use:
2377 * 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
2380 VLIB_CLI_COMMAND (add_lb_static_mapping_command, static) = {
2381 .path = "nat44 add load-balancing static mapping",
2382 .function = add_lb_static_mapping_command_fn,
2384 "nat44 add load-balancing static mapping protocol tcp|udp "
2385 "external <addr>:<port> local <addr>:<port> [vrf <table-id>] "
2386 "probability <n> [twice-nat|self-twice-nat] [out2in-only] "
2387 "[affinity <timeout-seconds>] [del]",
2392 * @cliexstart{nat44 add load-balancing static mapping}
2393 * Modify service load balancing using NAT44
2394 * To add new back-end server 10.100.10.30:8080 for service load balancing
2395 * static mapping with external IP address 1.2.3.4 and TCP port 80 use:
2396 * vpp# nat44 add load-balancing back-end protocol tcp external 1.2.3.4:80 local 10.100.10.30:8080 probability 25
2399 VLIB_CLI_COMMAND (add_lb_backend_command, static) = {
2400 .path = "nat44 add load-balancing back-end",
2401 .function = add_lb_backend_command_fn,
2403 "nat44 add load-balancing back-end protocol tcp|udp "
2404 "external <addr>:<port> local <addr>:<port> [vrf <table-id>] "
2405 "probability <n> [del]",
2410 * @cliexstart{show nat44 static mappings}
2411 * Show NAT44 static mappings.
2412 * vpp# show nat44 static mappings
2413 * NAT44 static mappings:
2414 * local 10.0.0.3 external 4.4.4.4 vrf 0
2415 * tcp local 192.168.0.4:6303 external 4.4.4.3:3606 vrf 0
2416 * tcp vrf 0 external 1.2.3.4:80 out2in-only
2417 * local 10.100.10.10:8080 probability 80
2418 * local 10.100.10.20:8080 probability 20
2419 * tcp local 10.100.3.8:8080 external 169.10.10.1:80 vrf 0 twice-nat
2420 * tcp local 10.0.0.10:3603 external GigabitEthernet0/a/0:6306 vrf 10
2423 VLIB_CLI_COMMAND (nat44_show_static_mappings_command, static) = {
2424 .path = "show nat44 static mappings",
2425 .short_help = "show nat44 static mappings",
2426 .function = nat44_show_static_mappings_command_fn,
2431 * @cliexstart{nat44 add interface address}
2432 * Use NAT44 pool address from specific interfce
2433 * To add NAT44 pool address from specific interface use:
2434 * vpp# nat44 add interface address GigabitEthernet0/8/0
2437 VLIB_CLI_COMMAND (snat_add_interface_address_command, static) = {
2438 .path = "nat44 add interface address",
2439 .short_help = "nat44 add interface address <interface> [twice-nat] [del]",
2440 .function = snat_add_interface_address_command_fn,
2445 * @cliexstart{show nat44 interface address}
2446 * Show NAT44 pool address interfaces
2447 * vpp# show nat44 interface address
2448 * NAT44 pool address interfaces:
2449 * GigabitEthernet0/a/0
2450 * NAT44 twice-nat pool address interfaces:
2451 * GigabitEthernet0/8/0
2454 VLIB_CLI_COMMAND (nat44_show_interface_address_command, static) = {
2455 .path = "show nat44 interface address",
2456 .short_help = "show nat44 interface address",
2457 .function = nat44_show_interface_address_command_fn,
2462 * @cliexstart{show nat44 sessions}
2463 * Show NAT44 sessions.
2466 VLIB_CLI_COMMAND (nat44_show_sessions_command, static) = {
2467 .path = "show nat44 sessions",
2468 .short_help = "show nat44 sessions [detail|metrics]",
2469 .function = nat44_show_sessions_command_fn,
2474 * @cliexstart{set nat44 session limit}
2475 * Set NAT44 session limit.
2478 VLIB_CLI_COMMAND (nat44_set_session_limit_command, static) = {
2479 .path = "set nat44 session limit",
2480 .short_help = "set nat44 session limit <limit> [vrf <table-id>]",
2481 .function = nat44_set_session_limit_command_fn,
2486 * @cliexstart{nat44 del user}
2487 * To delete all NAT44 user sessions:
2488 * vpp# nat44 del user 10.0.0.3
2491 VLIB_CLI_COMMAND (nat44_del_user_command, static) = {
2492 .path = "nat44 del user",
2493 .short_help = "nat44 del user <addr> [fib <index>]",
2494 .function = nat44_del_user_command_fn,
2499 * @cliexstart{clear nat44 sessions}
2500 * To clear all NAT44 sessions
2501 * vpp# clear nat44 sessions
2504 VLIB_CLI_COMMAND (nat44_clear_sessions_command, static) = {
2505 .path = "clear nat44 sessions",
2506 .short_help = "clear nat44 sessions",
2507 .function = nat44_clear_sessions_command_fn,
2512 * @cliexstart{nat44 del session}
2513 * To administratively delete NAT44 session by inside address and port use:
2514 * vpp# nat44 del session in 10.0.0.3:6303 tcp
2515 * To administratively delete NAT44 session by outside address and port use:
2516 * vpp# nat44 del session out 1.0.0.3:6033 udp
2519 VLIB_CLI_COMMAND (nat44_del_session_command, static) = {
2520 .path = "nat44 del session",
2521 .short_help = "nat44 del session in|out <addr>:<port> tcp|udp|icmp [vrf <id>] [external-host <addr>:<port>]",
2522 .function = nat44_del_session_command_fn,
2527 * @cliexstart{nat44 forwarding}
2528 * Enable or disable forwarding
2529 * Forward packets which don't match existing translation
2530 * or static mapping instead of dropping them.
2531 * To enable forwarding, use:
2532 * vpp# nat44 forwarding enable
2533 * To disable forwarding, use:
2534 * vpp# nat44 forwarding disable
2537 VLIB_CLI_COMMAND (snat_forwarding_set_command, static) = {
2538 .path = "nat44 forwarding",
2539 .short_help = "nat44 forwarding enable|disable",
2540 .function = snat_forwarding_set_command_fn,
2546 * fd.io coding-style-patch-verification: ON
2549 * eval: (c-set-style "gnu")