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/nat_ipfix_logging.h>
22 #include <nat/nat64.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>
27 #include <nat/nat_ha.h>
29 #define UNSUPPORTED_IN_ED_MODE_STR \
30 "This command is unsupported in endpoint dependent mode"
31 #define SUPPORTED_ONLY_IN_ED_MODE_STR \
32 "This command is supported only in endpoint dependent mode"
35 set_workers_command_fn (vlib_main_t * vm,
36 unformat_input_t * input, vlib_cli_command_t * cmd)
38 unformat_input_t _line_input, *line_input = &_line_input;
41 clib_error_t *error = 0;
43 /* Get a line of input. */
44 if (!unformat_user (input, unformat_line_input, line_input))
47 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
49 if (unformat (line_input, "%U", unformat_bitmap_list, &bitmap))
53 error = clib_error_return (0, "unknown input '%U'",
54 format_unformat_error, line_input);
61 error = clib_error_return (0, "List of workers must be specified.");
65 rv = snat_set_workers (bitmap);
67 clib_bitmap_free (bitmap);
71 case VNET_API_ERROR_INVALID_WORKER:
72 error = clib_error_return (0, "Invalid worker(s).");
74 case VNET_API_ERROR_FEATURE_DISABLED:
75 error = clib_error_return (0,
76 "Supported only if 2 or more workes available.");
83 unformat_free (line_input);
89 nat_show_workers_commnad_fn (vlib_main_t * vm, unformat_input_t * input,
90 vlib_cli_command_t * cmd)
92 snat_main_t *sm = &snat_main;
95 if (sm->num_workers > 1)
97 vlib_cli_output (vm, "%d workers", vec_len (sm->workers));
99 vec_foreach (worker, sm->workers)
101 vlib_worker_thread_t *w =
102 vlib_worker_threads + *worker + sm->first_worker_index;
103 vlib_cli_output (vm, " %s", w->name);
111 static clib_error_t *
112 snat_set_log_level_command_fn (vlib_main_t * vm,
113 unformat_input_t * input,
114 vlib_cli_command_t * cmd)
116 unformat_input_t _line_input, *line_input = &_line_input;
117 snat_main_t *sm = &snat_main;
118 u8 log_level = SNAT_LOG_NONE;
119 clib_error_t *error = 0;
121 /* Get a line of input. */
122 if (!unformat_user (input, unformat_line_input, line_input))
125 if (!unformat (line_input, "%d", &log_level))
127 error = clib_error_return (0, "unknown input '%U'",
128 format_unformat_error, line_input);
131 if (log_level > SNAT_LOG_DEBUG)
133 error = clib_error_return (0, "unknown logging level '%d'", log_level);
136 sm->log_level = log_level;
139 unformat_free (line_input);
144 static clib_error_t *
145 snat_ipfix_logging_enable_disable_command_fn (vlib_main_t * vm,
146 unformat_input_t * input,
147 vlib_cli_command_t * cmd)
149 unformat_input_t _line_input, *line_input = &_line_input;
154 clib_error_t *error = 0;
156 /* Get a line of input. */
157 if (!unformat_user (input, unformat_line_input, line_input))
159 rv = snat_ipfix_logging_enable_disable (enable, domain_id,
162 return clib_error_return (0, "ipfix logging enable failed");
166 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
168 if (unformat (line_input, "domain %d", &domain_id))
170 else if (unformat (line_input, "src-port %d", &src_port))
172 else if (unformat (line_input, "disable"))
176 error = clib_error_return (0, "unknown input '%U'",
177 format_unformat_error, line_input);
182 rv = snat_ipfix_logging_enable_disable (enable, domain_id, (u16) src_port);
186 error = clib_error_return (0, "ipfix logging enable failed");
191 unformat_free (line_input);
196 static clib_error_t *
197 nat44_show_hash_command_fn (vlib_main_t * vm, unformat_input_t * input,
198 vlib_cli_command_t * cmd)
200 snat_main_t *sm = &snat_main;
201 snat_main_per_thread_data_t *tsm;
202 nat_affinity_main_t *nam = &nat_affinity_main;
206 if (unformat (input, "detail"))
208 else if (unformat (input, "verbose"))
211 vlib_cli_output (vm, "%U", format_bihash_8_8, &sm->static_mapping_by_local,
213 vlib_cli_output (vm, "%U",
214 format_bihash_8_8, &sm->static_mapping_by_external,
216 vlib_cli_output (vm, "%U", format_bihash_16_8, &sm->out2in_ed, verbose);
217 vec_foreach_index (i, sm->per_thread_data)
219 tsm = vec_elt_at_index (sm->per_thread_data, i);
220 vlib_cli_output (vm, "-------- thread %d %s --------\n",
221 i, vlib_worker_threads[i].name);
222 if (sm->endpoint_dependent)
224 vlib_cli_output (vm, "%U", format_bihash_16_8, &tsm->in2out_ed,
229 vlib_cli_output (vm, "%U", format_bihash_8_8, &tsm->in2out, verbose);
230 vlib_cli_output (vm, "%U", format_bihash_8_8, &tsm->out2in, verbose);
232 vlib_cli_output (vm, "%U", format_bihash_8_8, &tsm->user_hash, verbose);
235 if (sm->endpoint_dependent)
237 vlib_cli_output (vm, "%U", format_bihash_16_8, &nam->affinity_hash,
241 vlib_cli_output (vm, "-------- hash table parameters --------\n");
242 vlib_cli_output (vm, "translation buckets: %u", sm->translation_buckets);
243 vlib_cli_output (vm, "translation memory size: %U",
244 format_memory_size, sm->translation_memory_size);
245 if (!sm->endpoint_dependent)
247 vlib_cli_output (vm, "user buckets: %u", sm->user_buckets);
248 vlib_cli_output (vm, "user memory size: %U",
249 format_memory_size, sm->user_memory_size);
254 static clib_error_t *
255 nat44_set_alloc_addr_and_port_alg_command_fn (vlib_main_t * vm,
256 unformat_input_t * input,
257 vlib_cli_command_t * cmd)
259 unformat_input_t _line_input, *line_input = &_line_input;
260 clib_error_t *error = 0;
261 u32 psid, psid_offset, psid_length, port_start, port_end;
263 /* Get a line of input. */
264 if (!unformat_user (input, unformat_line_input, line_input))
267 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
269 if (unformat (line_input, "default"))
270 nat_set_alloc_addr_and_port_default ();
273 (line_input, "map-e psid %d psid-offset %d psid-len %d", &psid,
274 &psid_offset, &psid_length))
275 nat_set_alloc_addr_and_port_mape ((u16) psid, (u16) psid_offset,
279 (line_input, "port-range %d - %d", &port_start, &port_end))
281 if (port_end <= port_start)
284 clib_error_return (0,
285 "The end-port must be greater than start-port");
288 nat_set_alloc_addr_and_port_range ((u16) port_start,
293 error = clib_error_return (0, "unknown input '%U'",
294 format_unformat_error, line_input);
300 unformat_free (line_input);
305 static clib_error_t *
306 nat44_show_alloc_addr_and_port_alg_command_fn (vlib_main_t * vm,
307 unformat_input_t * input,
308 vlib_cli_command_t * cmd)
310 snat_main_t *sm = &snat_main;
312 vlib_cli_output (vm, "NAT address and port: %U",
313 format_nat_addr_and_port_alloc_alg,
314 sm->addr_and_port_alloc_alg);
315 switch (sm->addr_and_port_alloc_alg)
317 case NAT_ADDR_AND_PORT_ALLOC_ALG_MAPE:
318 vlib_cli_output (vm, " psid %d psid-offset %d psid-len %d", sm->psid,
319 sm->psid_offset, sm->psid_length);
321 case NAT_ADDR_AND_PORT_ALLOC_ALG_RANGE:
322 vlib_cli_output (vm, " start-port %d end-port %d", sm->start_port,
332 static clib_error_t *
333 nat_set_mss_clamping_command_fn (vlib_main_t * vm, unformat_input_t * input,
334 vlib_cli_command_t * cmd)
336 unformat_input_t _line_input, *line_input = &_line_input;
337 snat_main_t *sm = &snat_main;
338 clib_error_t *error = 0;
341 /* Get a line of input. */
342 if (!unformat_user (input, unformat_line_input, line_input))
345 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
347 if (unformat (line_input, "disable"))
348 sm->mss_clamping = 0;
349 else if (unformat (line_input, "%d", &mss))
350 sm->mss_clamping = (u16) mss;
353 error = clib_error_return (0, "unknown input '%U'",
354 format_unformat_error, line_input);
360 unformat_free (line_input);
365 static clib_error_t *
366 nat_show_mss_clamping_command_fn (vlib_main_t * vm, unformat_input_t * input,
367 vlib_cli_command_t * cmd)
369 snat_main_t *sm = &snat_main;
371 if (sm->mss_clamping)
372 vlib_cli_output (vm, "mss-clamping %d", sm->mss_clamping);
374 vlib_cli_output (vm, "mss-clamping disabled");
379 static clib_error_t *
380 nat_ha_failover_command_fn (vlib_main_t * vm, unformat_input_t * input,
381 vlib_cli_command_t * cmd)
383 unformat_input_t _line_input, *line_input = &_line_input;
385 u32 port, session_refresh_interval = 10;
387 clib_error_t *error = 0;
389 /* Get a line of input. */
390 if (!unformat_user (input, unformat_line_input, line_input))
393 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
395 if (unformat (line_input, "%U:%u", unformat_ip4_address, &addr, &port))
399 (line_input, "refresh-interval %u", &session_refresh_interval))
403 error = clib_error_return (0, "unknown input '%U'",
404 format_unformat_error, line_input);
409 rv = nat_ha_set_failover (&addr, (u16) port, session_refresh_interval);
411 error = clib_error_return (0, "set HA failover failed");
414 unformat_free (line_input);
419 static clib_error_t *
420 nat_ha_listener_command_fn (vlib_main_t * vm, unformat_input_t * input,
421 vlib_cli_command_t * cmd)
423 unformat_input_t _line_input, *line_input = &_line_input;
425 u32 port, path_mtu = 512;
427 clib_error_t *error = 0;
429 /* Get a line of input. */
430 if (!unformat_user (input, unformat_line_input, line_input))
433 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
435 if (unformat (line_input, "%U:%u", unformat_ip4_address, &addr, &port))
437 else if (unformat (line_input, "path-mtu %u", &path_mtu))
441 error = clib_error_return (0, "unknown input '%U'",
442 format_unformat_error, line_input);
447 rv = nat_ha_set_listener (&addr, (u16) port, path_mtu);
449 error = clib_error_return (0, "set HA listener failed");
452 unformat_free (line_input);
457 static clib_error_t *
458 nat_show_ha_command_fn (vlib_main_t * vm, unformat_input_t * input,
459 vlib_cli_command_t * cmd)
463 u32 path_mtu, session_refresh_interval, resync_ack_missed;
466 nat_ha_get_listener (&addr, &port, &path_mtu);
469 vlib_cli_output (vm, "NAT HA disabled\n");
473 vlib_cli_output (vm, "LISTENER:\n");
474 vlib_cli_output (vm, " %U:%u path-mtu %u\n",
475 format_ip4_address, &addr, port, path_mtu);
477 nat_ha_get_failover (&addr, &port, &session_refresh_interval);
478 vlib_cli_output (vm, "FAILOVER:\n");
480 vlib_cli_output (vm, " %U:%u refresh-interval %usec\n",
481 format_ip4_address, &addr, port,
482 session_refresh_interval);
484 vlib_cli_output (vm, " NA\n");
486 nat_ha_get_resync_status (&in_resync, &resync_ack_missed);
487 vlib_cli_output (vm, "RESYNC:\n");
489 vlib_cli_output (vm, " in progress\n");
491 vlib_cli_output (vm, " completed (%d ACK missed)\n", resync_ack_missed);
496 static clib_error_t *
497 nat_ha_flush_command_fn (vlib_main_t * vm, unformat_input_t * input,
498 vlib_cli_command_t * cmd)
504 static clib_error_t *
505 nat_ha_resync_command_fn (vlib_main_t * vm, unformat_input_t * input,
506 vlib_cli_command_t * cmd)
508 clib_error_t *error = 0;
510 if (nat_ha_resync (0, 0, 0))
511 error = clib_error_return (0, "NAT HA resync already running");
516 static clib_error_t *
517 add_address_command_fn (vlib_main_t * vm,
518 unformat_input_t * input, vlib_cli_command_t * cmd)
520 unformat_input_t _line_input, *line_input = &_line_input;
521 snat_main_t *sm = &snat_main;
522 ip4_address_t start_addr, end_addr, this_addr;
523 u32 start_host_order, end_host_order;
528 clib_error_t *error = 0;
531 /* Get a line of input. */
532 if (!unformat_user (input, unformat_line_input, line_input))
535 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
537 if (unformat (line_input, "%U - %U",
538 unformat_ip4_address, &start_addr,
539 unformat_ip4_address, &end_addr))
541 else if (unformat (line_input, "tenant-vrf %u", &vrf_id))
543 else if (unformat (line_input, "%U", unformat_ip4_address, &start_addr))
544 end_addr = start_addr;
545 else if (unformat (line_input, "twice-nat"))
547 else if (unformat (line_input, "del"))
551 error = clib_error_return (0, "unknown input '%U'",
552 format_unformat_error, line_input);
557 if (sm->static_mapping_only)
559 error = clib_error_return (0, "static mapping only mode");
563 start_host_order = clib_host_to_net_u32 (start_addr.as_u32);
564 end_host_order = clib_host_to_net_u32 (end_addr.as_u32);
566 if (end_host_order < start_host_order)
568 error = clib_error_return (0, "end address less than start address");
572 count = (end_host_order - start_host_order) + 1;
575 nat_log_info ("%U - %U, %d addresses...",
576 format_ip4_address, &start_addr,
577 format_ip4_address, &end_addr, count);
579 this_addr = start_addr;
581 for (i = 0; i < count; i++)
584 rv = snat_add_address (sm, &this_addr, vrf_id, twice_nat);
586 rv = snat_del_address (sm, this_addr, 0, twice_nat);
590 case VNET_API_ERROR_VALUE_EXIST:
591 error = clib_error_return (0, "NAT address already in use.");
593 case VNET_API_ERROR_NO_SUCH_ENTRY:
594 error = clib_error_return (0, "NAT address not exist.");
596 case VNET_API_ERROR_UNSPECIFIED:
598 clib_error_return (0, "NAT address used in static mapping.");
600 case VNET_API_ERROR_FEATURE_DISABLED:
602 clib_error_return (0,
603 "twice NAT available only for endpoint-dependent mode.");
610 nat44_add_del_address_dpo (this_addr, is_add);
612 increment_v4_address (&this_addr);
616 unformat_free (line_input);
622 nat44_show_lru_summary (vlib_main_t * vm, snat_main_per_thread_data_t * tsm,
623 u64 now, u64 sess_timeout_time)
625 snat_main_t *sm = &snat_main;
626 dlist_elt_t *oldest_elt;
632 clib_dlist_remove_head (tsm->lru_pool, tsm->n##_lru_head_index); \
633 if (~0 != oldest_index) \
635 oldest_elt = pool_elt_at_index (tsm->lru_pool, oldest_index); \
636 s = pool_elt_at_index (tsm->sessions, oldest_elt->value); \
637 sess_timeout_time = \
638 s->last_heard + (f64)nat44_session_get_timeout (sm, s); \
639 vlib_cli_output (vm, d " LRU min session timeout %llu (now %llu)", \
640 sess_timeout_time, now); \
641 clib_dlist_addhead (tsm->lru_pool, tsm->n##_lru_head_index, \
644 _(tcp_estab, "established tcp");
645 _(tcp_trans, "transitory tcp");
647 _(unk_proto, "unknown protocol");
652 static clib_error_t *
653 nat44_show_summary_command_fn (vlib_main_t * vm, unformat_input_t * input,
654 vlib_cli_command_t * cmd)
656 snat_main_per_thread_data_t *tsm;
657 snat_main_t *sm = &snat_main;
660 if (!sm->endpoint_dependent)
661 return clib_error_return (0, SUPPORTED_ONLY_IN_ED_MODE_STR);
665 u64 now = vlib_time_now (vm);
666 u64 sess_timeout_time;
668 u32 udp_sessions = 0;
669 u32 tcp_sessions = 0;
670 u32 icmp_sessions = 0;
674 u32 transitory_wait_closed = 0;
675 u32 transitory_closed = 0;
680 for (fib = 0; fib < vec_len (sm->max_translations_per_fib); fib++)
681 vlib_cli_output (vm, "max translations per thread: %u fib %u",
682 sm->max_translations_per_fib[fib], fib);
684 if (sm->num_workers > 1)
687 vec_foreach (tsm, sm->per_thread_data)
689 pool_foreach (s, tsm->sessions,
691 sess_timeout_time = s->last_heard +
692 (f64) nat44_session_get_timeout (sm, s);
693 if (now >= sess_timeout_time)
696 switch (s->nat_proto)
698 case NAT_PROTOCOL_ICMP:
701 case NAT_PROTOCOL_TCP:
705 if (s->tcp_closed_timestamp)
707 if (now >= s->tcp_closed_timestamp)
713 ++transitory_wait_closed;
721 case NAT_PROTOCOL_UDP:
727 nat44_show_lru_summary (vm, tsm, now, sess_timeout_time);
728 count += pool_elts (tsm->sessions);
734 tsm = vec_elt_at_index (sm->per_thread_data, sm->num_workers);
736 pool_foreach (s, tsm->sessions,
738 sess_timeout_time = s->last_heard +
739 (f64) nat44_session_get_timeout (sm, s);
740 if (now >= sess_timeout_time)
743 switch (s->nat_proto)
745 case NAT_PROTOCOL_ICMP:
748 case NAT_PROTOCOL_TCP:
752 if (s->tcp_closed_timestamp)
754 if (now >= s->tcp_closed_timestamp)
760 ++transitory_wait_closed;
768 case NAT_PROTOCOL_UDP:
775 nat44_show_lru_summary (vm, tsm, now, sess_timeout_time);
776 count = pool_elts (tsm->sessions);
779 vlib_cli_output (vm, "total timed out sessions: %u", timed_out);
780 vlib_cli_output (vm, "total sessions: %u", count);
781 vlib_cli_output (vm, "total tcp sessions: %u", tcp_sessions);
782 vlib_cli_output (vm, "total tcp established sessions: %u", established);
783 vlib_cli_output (vm, "total tcp transitory sessions: %u", transitory);
784 vlib_cli_output (vm, "total tcp transitory (WAIT-CLOSED) sessions: %u",
785 transitory_wait_closed);
786 vlib_cli_output (vm, "total tcp transitory (CLOSED) sessions: %u",
788 vlib_cli_output (vm, "total udp sessions: %u", udp_sessions);
789 vlib_cli_output (vm, "total icmp sessions: %u", icmp_sessions);
793 static clib_error_t *
794 nat44_show_addresses_command_fn (vlib_main_t * vm, unformat_input_t * input,
795 vlib_cli_command_t * cmd)
797 snat_main_t *sm = &snat_main;
800 vlib_cli_output (vm, "NAT44 pool addresses:");
802 vec_foreach (ap, sm->addresses)
804 vlib_cli_output (vm, "%U", format_ip4_address, &ap->addr);
805 if (ap->fib_index != ~0)
806 vlib_cli_output (vm, " tenant VRF: %u",
807 fib_table_get(ap->fib_index, FIB_PROTOCOL_IP4)->ft_table_id);
809 vlib_cli_output (vm, " tenant VRF independent");
810 #define _(N, i, n, s) \
811 vlib_cli_output (vm, " %d busy %s ports", ap->busy_##n##_ports, s);
815 vlib_cli_output (vm, "NAT44 twice-nat pool addresses:");
816 vec_foreach (ap, sm->twice_nat_addresses)
818 vlib_cli_output (vm, "%U", format_ip4_address, &ap->addr);
819 if (ap->fib_index != ~0)
820 vlib_cli_output (vm, " tenant VRF: %u",
821 fib_table_get(ap->fib_index, FIB_PROTOCOL_IP4)->ft_table_id);
823 vlib_cli_output (vm, " tenant VRF independent");
824 #define _(N, i, n, s) \
825 vlib_cli_output (vm, " %d busy %s ports", ap->busy_##n##_ports, s);
833 static clib_error_t *
834 snat_feature_command_fn (vlib_main_t * vm,
835 unformat_input_t * input, 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;
841 u32 *inside_sw_if_indices = 0;
842 u32 *outside_sw_if_indices = 0;
843 u8 is_output_feature = 0;
849 /* Get a line of input. */
850 if (!unformat_user (input, unformat_line_input, line_input))
853 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
855 if (unformat (line_input, "in %U", unformat_vnet_sw_interface,
857 vec_add1 (inside_sw_if_indices, sw_if_index);
858 else if (unformat (line_input, "out %U", unformat_vnet_sw_interface,
860 vec_add1 (outside_sw_if_indices, sw_if_index);
861 else if (unformat (line_input, "output-feature"))
862 is_output_feature = 1;
863 else if (unformat (line_input, "del"))
867 error = clib_error_return (0, "unknown input '%U'",
868 format_unformat_error, line_input);
873 if (vec_len (inside_sw_if_indices))
875 for (i = 0; i < vec_len (inside_sw_if_indices); i++)
877 sw_if_index = inside_sw_if_indices[i];
878 if (is_output_feature)
880 if (snat_interface_add_del_output_feature
881 (sw_if_index, 1, is_del))
883 error = clib_error_return (0, "%s %U failed",
884 is_del ? "del" : "add",
885 format_vnet_sw_if_index_name,
892 if (snat_interface_add_del (sw_if_index, 1, is_del))
894 error = clib_error_return (0, "%s %U failed",
895 is_del ? "del" : "add",
896 format_vnet_sw_if_index_name,
904 if (vec_len (outside_sw_if_indices))
906 for (i = 0; i < vec_len (outside_sw_if_indices); i++)
908 sw_if_index = outside_sw_if_indices[i];
909 if (is_output_feature)
911 if (snat_interface_add_del_output_feature
912 (sw_if_index, 0, is_del))
914 error = clib_error_return (0, "%s %U failed",
915 is_del ? "del" : "add",
916 format_vnet_sw_if_index_name,
923 if (snat_interface_add_del (sw_if_index, 0, is_del))
925 error = clib_error_return (0, "%s %U failed",
926 is_del ? "del" : "add",
927 format_vnet_sw_if_index_name,
936 unformat_free (line_input);
937 vec_free (inside_sw_if_indices);
938 vec_free (outside_sw_if_indices);
943 static clib_error_t *
944 nat44_show_interfaces_command_fn (vlib_main_t * vm, unformat_input_t * input,
945 vlib_cli_command_t * cmd)
947 snat_main_t *sm = &snat_main;
949 vnet_main_t *vnm = vnet_get_main ();
951 vlib_cli_output (vm, "NAT44 interfaces:");
953 pool_foreach (i, sm->interfaces,
955 vlib_cli_output (vm, " %U %s", format_vnet_sw_if_index_name, vnm,
957 (nat_interface_is_inside(i) &&
958 nat_interface_is_outside(i)) ? "in out" :
959 (nat_interface_is_inside(i) ? "in" : "out"));
962 pool_foreach (i, sm->output_feature_interfaces,
964 vlib_cli_output (vm, " %U output-feature %s",
965 format_vnet_sw_if_index_name, vnm,
967 (nat_interface_is_inside(i) &&
968 nat_interface_is_outside(i)) ? "in out" :
969 (nat_interface_is_inside(i) ? "in" : "out"));
976 static clib_error_t *
977 add_static_mapping_command_fn (vlib_main_t * vm,
978 unformat_input_t * input,
979 vlib_cli_command_t * cmd)
981 unformat_input_t _line_input, *line_input = &_line_input;
982 clib_error_t *error = 0;
983 ip4_address_t l_addr, e_addr, exact_addr;
984 u32 l_port = 0, e_port = 0, vrf_id = ~0;
985 int is_add = 1, addr_only = 1, rv, exact = 0;
986 u32 sw_if_index = ~0;
987 vnet_main_t *vnm = vnet_get_main ();
988 nat_protocol_t proto = NAT_PROTOCOL_OTHER;
990 twice_nat_type_t twice_nat = TWICE_NAT_DISABLED;
993 /* Get a line of input. */
994 if (!unformat_user (input, unformat_line_input, line_input))
997 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
999 if (unformat (line_input, "local %U %u", unformat_ip4_address, &l_addr,
1003 if (unformat (line_input, "local %U", unformat_ip4_address, &l_addr))
1005 else if (unformat (line_input, "external %U %u", unformat_ip4_address,
1008 else if (unformat (line_input, "external %U", unformat_ip4_address,
1011 else if (unformat (line_input, "external %U %u",
1012 unformat_vnet_sw_interface, vnm, &sw_if_index,
1015 else if (unformat (line_input, "external %U",
1016 unformat_vnet_sw_interface, vnm, &sw_if_index))
1018 else if (unformat (line_input, "exact %U", unformat_ip4_address,
1021 else if (unformat (line_input, "vrf %u", &vrf_id))
1023 else if (unformat (line_input, "%U", unformat_nat_protocol, &proto))
1025 else if (unformat (line_input, "twice-nat"))
1026 twice_nat = TWICE_NAT;
1027 else if (unformat (line_input, "self-twice-nat"))
1028 twice_nat = TWICE_NAT_SELF;
1029 else if (unformat (line_input, "out2in-only"))
1031 else if (unformat (line_input, "del"))
1035 error = clib_error_return (0, "unknown input: '%U'",
1036 format_unformat_error, line_input);
1041 if (twice_nat && addr_only)
1043 error = clib_error_return (0, "twice NAT only for 1:1 NAPT");
1052 clib_error_return (0,
1053 "address only mapping doesn't support protocol");
1057 else if (!proto_set)
1059 error = clib_error_return (0, "protocol is required");
1063 rv = snat_add_static_mapping (l_addr, e_addr, clib_host_to_net_u16 (l_port),
1064 clib_host_to_net_u16 (e_port),
1065 vrf_id, addr_only, sw_if_index, proto, is_add,
1066 twice_nat, out2in_only, 0, 0, exact_addr,
1071 case VNET_API_ERROR_INVALID_VALUE:
1072 error = clib_error_return (0, "External port already in use.");
1074 case VNET_API_ERROR_NO_SUCH_ENTRY:
1076 error = clib_error_return (0, "External address must be allocated.");
1078 error = clib_error_return (0, "Mapping not exist.");
1080 case VNET_API_ERROR_NO_SUCH_FIB:
1081 error = clib_error_return (0, "No such VRF id.");
1083 case VNET_API_ERROR_VALUE_EXIST:
1084 error = clib_error_return (0, "Mapping already exist.");
1086 case VNET_API_ERROR_FEATURE_DISABLED:
1088 clib_error_return (0,
1089 "twice-nat/out2in-only available only for endpoint-dependent mode.");
1096 unformat_free (line_input);
1101 static clib_error_t *
1102 add_identity_mapping_command_fn (vlib_main_t * vm,
1103 unformat_input_t * input,
1104 vlib_cli_command_t * cmd)
1106 unformat_input_t _line_input, *line_input = &_line_input;
1107 clib_error_t *error = 0;
1108 ip4_address_t addr, pool_addr = { 0 };
1109 u32 port = 0, vrf_id = ~0;
1112 u32 sw_if_index = ~0;
1113 vnet_main_t *vnm = vnet_get_main ();
1115 nat_protocol_t proto;
1119 /* Get a line of input. */
1120 if (!unformat_user (input, unformat_line_input, line_input))
1123 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1125 if (unformat (line_input, "%U", unformat_ip4_address, &addr))
1127 else if (unformat (line_input, "external %U",
1128 unformat_vnet_sw_interface, vnm, &sw_if_index))
1130 else if (unformat (line_input, "vrf %u", &vrf_id))
1132 else if (unformat (line_input, "%U %u", unformat_nat_protocol, &proto,
1135 else if (unformat (line_input, "del"))
1139 error = clib_error_return (0, "unknown input: '%U'",
1140 format_unformat_error, line_input);
1146 snat_add_static_mapping (addr, addr, clib_host_to_net_u16 (port),
1147 clib_host_to_net_u16 (port), vrf_id, addr_only,
1148 sw_if_index, proto, is_add, 0, 0, 0, 1,
1153 case VNET_API_ERROR_INVALID_VALUE:
1154 error = clib_error_return (0, "External port already in use.");
1156 case VNET_API_ERROR_NO_SUCH_ENTRY:
1158 error = clib_error_return (0, "External address must be allocated.");
1160 error = clib_error_return (0, "Mapping not exist.");
1162 case VNET_API_ERROR_NO_SUCH_FIB:
1163 error = clib_error_return (0, "No such VRF id.");
1165 case VNET_API_ERROR_VALUE_EXIST:
1166 error = clib_error_return (0, "Mapping already exist.");
1173 unformat_free (line_input);
1178 static clib_error_t *
1179 add_lb_static_mapping_command_fn (vlib_main_t * vm,
1180 unformat_input_t * input,
1181 vlib_cli_command_t * cmd)
1183 unformat_input_t _line_input, *line_input = &_line_input;
1184 clib_error_t *error = 0;
1185 ip4_address_t l_addr, e_addr;
1186 u32 l_port = 0, e_port = 0, vrf_id = 0, probability = 0, affinity = 0;
1189 nat_protocol_t proto;
1191 nat44_lb_addr_port_t *locals = 0, local;
1192 twice_nat_type_t twice_nat = TWICE_NAT_DISABLED;
1195 /* Get a line of input. */
1196 if (!unformat_user (input, unformat_line_input, line_input))
1199 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1201 if (unformat (line_input, "local %U:%u probability %u",
1202 unformat_ip4_address, &l_addr, &l_port, &probability))
1204 clib_memset (&local, 0, sizeof (local));
1205 local.addr = l_addr;
1206 local.port = (u16) l_port;
1207 local.probability = (u8) probability;
1208 vec_add1 (locals, local);
1210 else if (unformat (line_input, "local %U:%u vrf %u probability %u",
1211 unformat_ip4_address, &l_addr, &l_port, &vrf_id,
1214 clib_memset (&local, 0, sizeof (local));
1215 local.addr = l_addr;
1216 local.port = (u16) l_port;
1217 local.probability = (u8) probability;
1218 local.vrf_id = vrf_id;
1219 vec_add1 (locals, local);
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, "twice-nat"))
1228 twice_nat = TWICE_NAT;
1229 else if (unformat (line_input, "self-twice-nat"))
1230 twice_nat = TWICE_NAT_SELF;
1231 else if (unformat (line_input, "out2in-only"))
1233 else if (unformat (line_input, "del"))
1235 else if (unformat (line_input, "affinity %u", &affinity))
1239 error = clib_error_return (0, "unknown input: '%U'",
1240 format_unformat_error, line_input);
1245 if (vec_len (locals) < 2)
1247 error = clib_error_return (0, "at least two local must be set");
1253 error = clib_error_return (0, "missing protocol");
1257 rv = nat44_add_del_lb_static_mapping (e_addr, (u16) e_port, proto, locals,
1258 is_add, twice_nat, out2in_only, 0,
1263 case VNET_API_ERROR_INVALID_VALUE:
1264 error = clib_error_return (0, "External port already in use.");
1266 case VNET_API_ERROR_NO_SUCH_ENTRY:
1268 error = clib_error_return (0, "External address must be allocated.");
1270 error = clib_error_return (0, "Mapping not exist.");
1272 case VNET_API_ERROR_VALUE_EXIST:
1273 error = clib_error_return (0, "Mapping already exist.");
1275 case VNET_API_ERROR_FEATURE_DISABLED:
1277 clib_error_return (0, "Available only for endpoint-dependent mode.");
1284 unformat_free (line_input);
1290 static clib_error_t *
1291 add_lb_backend_command_fn (vlib_main_t * vm,
1292 unformat_input_t * input, vlib_cli_command_t * cmd)
1294 unformat_input_t _line_input, *line_input = &_line_input;
1295 clib_error_t *error = 0;
1296 ip4_address_t l_addr, e_addr;
1297 u32 l_port = 0, e_port = 0, vrf_id = 0, probability = 0;
1300 nat_protocol_t proto;
1303 /* Get a line of input. */
1304 if (!unformat_user (input, unformat_line_input, line_input))
1307 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1309 if (unformat (line_input, "local %U:%u probability %u",
1310 unformat_ip4_address, &l_addr, &l_port, &probability))
1312 else if (unformat (line_input, "local %U:%u vrf %u probability %u",
1313 unformat_ip4_address, &l_addr, &l_port, &vrf_id,
1316 else if (unformat (line_input, "external %U:%u", unformat_ip4_address,
1319 else if (unformat (line_input, "protocol %U", unformat_nat_protocol,
1322 else if (unformat (line_input, "del"))
1326 error = clib_error_return (0, "unknown input: '%U'",
1327 format_unformat_error, line_input);
1332 if (!l_port || !e_port)
1334 error = clib_error_return (0, "local or external must be set");
1340 error = clib_error_return (0, "missing protocol");
1345 nat44_lb_static_mapping_add_del_local (e_addr, (u16) e_port, l_addr,
1346 l_port, proto, vrf_id, probability,
1351 case VNET_API_ERROR_INVALID_VALUE:
1352 error = clib_error_return (0, "External is not load-balancing static "
1355 case VNET_API_ERROR_NO_SUCH_ENTRY:
1356 error = clib_error_return (0, "Mapping or back-end not exist.");
1358 case VNET_API_ERROR_VALUE_EXIST:
1359 error = clib_error_return (0, "Back-end already exist.");
1361 case VNET_API_ERROR_FEATURE_DISABLED:
1363 clib_error_return (0, "Available only for endpoint-dependent mode.");
1365 case VNET_API_ERROR_UNSPECIFIED:
1366 error = clib_error_return (0, "At least two back-ends must remain");
1373 unformat_free (line_input);
1378 static clib_error_t *
1379 nat44_show_static_mappings_command_fn (vlib_main_t * vm,
1380 unformat_input_t * input,
1381 vlib_cli_command_t * cmd)
1383 snat_main_t *sm = &snat_main;
1384 snat_static_mapping_t *m;
1385 snat_static_map_resolve_t *rp;
1387 vlib_cli_output (vm, "NAT44 static mappings:");
1389 pool_foreach (m, sm->static_mappings,
1391 vlib_cli_output (vm, " %U", format_snat_static_mapping, m);
1393 vec_foreach (rp, sm->to_resolve)
1394 vlib_cli_output (vm, " %U", format_snat_static_map_to_resolve, rp);
1400 static clib_error_t *
1401 snat_add_interface_address_command_fn (vlib_main_t * vm,
1402 unformat_input_t * input,
1403 vlib_cli_command_t * cmd)
1405 snat_main_t *sm = &snat_main;
1406 unformat_input_t _line_input, *line_input = &_line_input;
1410 clib_error_t *error = 0;
1413 /* Get a line of input. */
1414 if (!unformat_user (input, unformat_line_input, line_input))
1417 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1419 if (unformat (line_input, "%U", unformat_vnet_sw_interface,
1420 sm->vnet_main, &sw_if_index))
1422 else if (unformat (line_input, "twice-nat"))
1424 else if (unformat (line_input, "del"))
1428 error = clib_error_return (0, "unknown input '%U'",
1429 format_unformat_error, line_input);
1434 rv = snat_add_interface_address (sm, sw_if_index, is_del, twice_nat);
1442 error = clib_error_return (0, "snat_add_interface_address returned %d",
1448 unformat_free (line_input);
1453 static clib_error_t *
1454 nat44_show_interface_address_command_fn (vlib_main_t * vm,
1455 unformat_input_t * input,
1456 vlib_cli_command_t * cmd)
1458 snat_main_t *sm = &snat_main;
1459 vnet_main_t *vnm = vnet_get_main ();
1463 vlib_cli_output (vm, "NAT44 pool address interfaces:");
1464 vec_foreach (sw_if_index, sm->auto_add_sw_if_indices)
1466 vlib_cli_output (vm, " %U", format_vnet_sw_if_index_name, vnm,
1469 vlib_cli_output (vm, "NAT44 twice-nat pool address interfaces:");
1470 vec_foreach (sw_if_index, sm->auto_add_sw_if_indices_twice_nat)
1472 vlib_cli_output (vm, " %U", format_vnet_sw_if_index_name, vnm,
1480 static clib_error_t *
1481 nat44_show_sessions_command_fn (vlib_main_t * vm, unformat_input_t * input,
1482 vlib_cli_command_t * cmd)
1484 unformat_input_t _line_input, *line_input = &_line_input;
1485 clib_error_t *error = 0;
1487 snat_main_per_thread_data_t *tsm;
1488 snat_main_t *sm = &snat_main;
1493 if (!unformat_user (input, unformat_line_input, line_input))
1496 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1498 if (unformat (line_input, "detail"))
1502 error = clib_error_return (0, "unknown input '%U'",
1503 format_unformat_error, line_input);
1507 unformat_free (line_input);
1510 if (!sm->endpoint_dependent)
1511 vlib_cli_output (vm, "NAT44 sessions:");
1513 vlib_cli_output (vm, "NAT44 ED sessions:");
1516 vec_foreach_index (i, sm->per_thread_data)
1518 tsm = vec_elt_at_index (sm->per_thread_data, i);
1520 vlib_cli_output (vm, "-------- thread %d %s: %d sessions --------\n",
1521 i, vlib_worker_threads[i].name,
1522 pool_elts (tsm->sessions));
1524 if (!sm->endpoint_dependent)
1527 pool_foreach (u, tsm->users,
1529 vlib_cli_output (vm, " %U", format_snat_user, tsm, u, detail);
1535 pool_foreach (s, tsm->sessions,
1537 vlib_cli_output (vm, " %U\n", format_snat_session, tsm, s);
1545 static clib_error_t *
1546 nat44_set_session_limit_command_fn (vlib_main_t * vm,
1547 unformat_input_t * input,
1548 vlib_cli_command_t * cmd)
1550 unformat_input_t _line_input, *line_input = &_line_input;
1551 clib_error_t *error = 0;
1553 u32 session_limit = 0, vrf_id = 0;
1555 /* Get a line of input. */
1556 if (!unformat_user (input, unformat_line_input, line_input))
1559 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1561 if (unformat (line_input, "%u", &session_limit))
1563 else if (unformat (line_input, "vrf %u", &vrf_id))
1567 error = clib_error_return (0, "unknown input '%U'",
1568 format_unformat_error, line_input);
1574 error = clib_error_return (0, "missing value of session limit");
1575 else if (nat44_update_session_limit (session_limit, vrf_id))
1576 error = clib_error_return (0, "nat44_set_session_limit failed");
1579 unformat_free (line_input);
1584 static clib_error_t *
1585 nat44_del_user_command_fn (vlib_main_t * vm,
1586 unformat_input_t * input, vlib_cli_command_t * cmd)
1588 snat_main_t *sm = &snat_main;
1589 unformat_input_t _line_input, *line_input = &_line_input;
1590 clib_error_t *error = 0;
1595 if (sm->endpoint_dependent)
1596 return clib_error_return (0, UNSUPPORTED_IN_ED_MODE_STR);
1598 /* Get a line of input. */
1599 if (!unformat_user (input, unformat_line_input, line_input))
1602 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1604 if (unformat (line_input, "%U", unformat_ip4_address, &addr))
1606 else if (unformat (line_input, "fib %u", &fib_index))
1610 error = clib_error_return (0, "unknown input '%U'",
1611 format_unformat_error, line_input);
1616 rv = nat44_user_del (&addr, fib_index);
1620 error = clib_error_return (0, "nat44_user_del returned %d", rv);
1624 unformat_free (line_input);
1629 static clib_error_t *
1630 nat44_clear_sessions_command_fn (vlib_main_t * vm,
1631 unformat_input_t * input,
1632 vlib_cli_command_t * cmd)
1634 clib_error_t *error = 0;
1635 nat44_sessions_clear ();
1639 static clib_error_t *
1640 nat44_del_session_command_fn (vlib_main_t * vm,
1641 unformat_input_t * input,
1642 vlib_cli_command_t * cmd)
1644 snat_main_t *sm = &snat_main;
1645 unformat_input_t _line_input, *line_input = &_line_input;
1646 int is_in = 0, is_ed = 0;
1647 clib_error_t *error = 0;
1648 ip4_address_t addr, eh_addr;
1649 u32 port = 0, eh_port = 0, vrf_id = sm->outside_vrf_id;
1650 nat_protocol_t proto;
1653 /* Get a line of input. */
1654 if (!unformat_user (input, unformat_line_input, line_input))
1657 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1660 (line_input, "%U:%u %U", unformat_ip4_address, &addr, &port,
1661 unformat_nat_protocol, &proto))
1663 else if (unformat (line_input, "in"))
1666 vrf_id = sm->inside_vrf_id;
1668 else if (unformat (line_input, "out"))
1671 vrf_id = sm->outside_vrf_id;
1673 else if (unformat (line_input, "vrf %u", &vrf_id))
1677 (line_input, "external-host %U:%u", unformat_ip4_address,
1678 &eh_addr, &eh_port))
1682 error = clib_error_return (0, "unknown input '%U'",
1683 format_unformat_error, line_input);
1690 nat44_del_ed_session (sm, &addr, clib_host_to_net_u16 (port), &eh_addr,
1691 clib_host_to_net_u16 (eh_port),
1692 nat_proto_to_ip_proto (proto), vrf_id, is_in);
1695 nat44_del_session (sm, &addr, clib_host_to_net_u16 (port), proto,
1704 error = clib_error_return (0, "nat44_del_session returned %d", rv);
1709 unformat_free (line_input);
1714 static clib_error_t *
1715 snat_forwarding_set_command_fn (vlib_main_t * vm,
1716 unformat_input_t * input,
1717 vlib_cli_command_t * cmd)
1719 snat_main_t *sm = &snat_main;
1720 unformat_input_t _line_input, *line_input = &_line_input;
1721 u8 forwarding_enable;
1722 u8 forwarding_enable_set = 0;
1723 clib_error_t *error = 0;
1725 /* Get a line of input. */
1726 if (!unformat_user (input, unformat_line_input, line_input))
1727 return clib_error_return (0, "'enable' or 'disable' expected");
1729 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1731 if (!forwarding_enable_set && unformat (line_input, "enable"))
1733 forwarding_enable = 1;
1734 forwarding_enable_set = 1;
1736 else if (!forwarding_enable_set && unformat (line_input, "disable"))
1738 forwarding_enable = 0;
1739 forwarding_enable_set = 1;
1743 error = clib_error_return (0, "unknown input '%U'",
1744 format_unformat_error, line_input);
1749 if (!forwarding_enable_set)
1751 error = clib_error_return (0, "'enable' or 'disable' expected");
1755 sm->forwarding_enabled = forwarding_enable;
1758 unformat_free (line_input);
1763 static clib_error_t *
1764 set_timeout_command_fn (vlib_main_t * vm,
1765 unformat_input_t * input, vlib_cli_command_t * cmd)
1767 snat_main_t *sm = &snat_main;
1768 unformat_input_t _line_input, *line_input = &_line_input;
1769 clib_error_t *error = 0;
1771 /* Get a line of input. */
1772 if (!unformat_user (input, unformat_line_input, line_input))
1775 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1777 if (unformat (line_input, "udp %u", &sm->udp_timeout))
1779 if (nat64_set_udp_timeout (sm->udp_timeout))
1781 error = clib_error_return (0, "Invalid UDP timeout value");
1785 else if (unformat (line_input, "tcp-established %u",
1786 &sm->tcp_established_timeout))
1788 if (nat64_set_tcp_timeouts
1789 (sm->tcp_transitory_timeout, sm->tcp_established_timeout))
1792 clib_error_return (0,
1793 "Invalid TCP established timeouts value");
1797 else if (unformat (line_input, "tcp-transitory %u",
1798 &sm->tcp_transitory_timeout))
1800 if (nat64_set_tcp_timeouts
1801 (sm->tcp_transitory_timeout, sm->tcp_established_timeout))
1804 clib_error_return (0,
1805 "Invalid TCP transitory timeouts value");
1809 else if (unformat (line_input, "icmp %u", &sm->icmp_timeout))
1811 if (nat64_set_icmp_timeout (sm->icmp_timeout))
1813 error = clib_error_return (0, "Invalid ICMP timeout value");
1817 else if (unformat (line_input, "reset"))
1819 sm->udp_timeout = SNAT_UDP_TIMEOUT;
1820 sm->tcp_established_timeout = SNAT_TCP_ESTABLISHED_TIMEOUT;
1821 sm->tcp_transitory_timeout = SNAT_TCP_TRANSITORY_TIMEOUT;
1822 sm->icmp_timeout = SNAT_ICMP_TIMEOUT;
1823 nat64_set_udp_timeout (0);
1824 nat64_set_icmp_timeout (0);
1825 nat64_set_tcp_timeouts (0, 0);
1829 error = clib_error_return (0, "unknown input '%U'",
1830 format_unformat_error, line_input);
1835 unformat_free (line_input);
1839 static clib_error_t *
1840 nat_show_timeouts_command_fn (vlib_main_t * vm,
1841 unformat_input_t * input,
1842 vlib_cli_command_t * cmd)
1844 snat_main_t *sm = &snat_main;
1846 vlib_cli_output (vm, "udp timeout: %dsec", sm->udp_timeout);
1847 vlib_cli_output (vm, "tcp-established timeout: %dsec",
1848 sm->tcp_established_timeout);
1849 vlib_cli_output (vm, "tcp-transitory timeout: %dsec",
1850 sm->tcp_transitory_timeout);
1851 vlib_cli_output (vm, "icmp timeout: %dsec", sm->icmp_timeout);
1856 static clib_error_t *
1857 nat44_debug_fib_expire_command_fn (vlib_main_t * vm,
1858 unformat_input_t * input,
1859 vlib_cli_command_t * cmd)
1861 unformat_input_t _line_input, *line_input = &_line_input;
1862 clib_error_t *error = 0;
1865 /* Get a line of input. */
1866 if (!unformat_user (input, unformat_line_input, line_input))
1869 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1871 if (unformat (line_input, "%u", &fib))
1875 error = clib_error_return (0, "unknown input '%U'",
1876 format_unformat_error, line_input);
1880 expire_per_vrf_sessions (fib);
1882 unformat_free (line_input);
1886 static clib_error_t *
1887 nat44_debug_fib_registration_command_fn (vlib_main_t * vm,
1888 unformat_input_t * input,
1889 vlib_cli_command_t * cmd)
1891 snat_main_t *sm = &snat_main;
1892 snat_main_per_thread_data_t *tsm;
1893 per_vrf_sessions_t *per_vrf_sessions;
1895 vlib_cli_output (vm, "VRF registration debug:");
1896 vec_foreach (tsm, sm->per_thread_data)
1898 vlib_cli_output (vm, "thread %u:", tsm->thread_index);
1899 vec_foreach (per_vrf_sessions, tsm->per_vrf_sessions_vec)
1901 vlib_cli_output (vm, "rx fib %u tx fib %u ses count %u %s",
1902 per_vrf_sessions->rx_fib_index,
1903 per_vrf_sessions->tx_fib_index,
1904 per_vrf_sessions->ses_count,
1905 per_vrf_sessions->expired ? "expired" : "");
1915 VLIB_CLI_COMMAND (nat44_debug_fib_expire_command, static) = {
1916 .path = "debug nat44 fib expire",
1917 .short_help = "debug nat44 fib expire <fib-index>",
1918 .function = nat44_debug_fib_expire_command_fn,
1923 VLIB_CLI_COMMAND (nat44_debug_fib_registration_command, static) = {
1924 .path = "debug nat44 fib registration",
1925 .short_help = "debug nat44 fib registration",
1926 .function = nat44_debug_fib_registration_command_fn,
1931 * @cliexstart{set snat workers}
1932 * Set NAT workers if 2 or more workers available, use:
1933 * vpp# set snat workers 0-2,5
1936 VLIB_CLI_COMMAND (set_workers_command, static) = {
1937 .path = "set nat workers",
1938 .function = set_workers_command_fn,
1939 .short_help = "set nat workers <workers-list>",
1944 * @cliexstart{show nat workers}
1946 * vpp# show nat workers:
1952 VLIB_CLI_COMMAND (nat_show_workers_command, static) = {
1953 .path = "show nat workers",
1954 .short_help = "show nat workers",
1955 .function = nat_show_workers_commnad_fn,
1960 * @cliexstart{set nat timeout}
1961 * Set values of timeouts for NAT sessions (in seconds), use:
1962 * vpp# set nat timeout udp 120 tcp-established 7500 tcp-transitory 250 icmp 90
1963 * To reset default values use:
1964 * vpp# set nat timeout reset
1967 VLIB_CLI_COMMAND (set_timeout_command, static) = {
1968 .path = "set nat timeout",
1969 .function = set_timeout_command_fn,
1971 "set nat timeout [udp <sec> | tcp-established <sec> "
1972 "tcp-transitory <sec> | icmp <sec> | reset]",
1977 * @cliexstart{show nat timeouts}
1978 * Show values of timeouts for NAT sessions.
1979 * vpp# show nat timeouts
1980 * udp timeout: 300sec
1981 * tcp-established timeout: 7440sec
1982 * tcp-transitory timeout: 240sec
1983 * icmp timeout: 60sec
1986 VLIB_CLI_COMMAND (nat_show_timeouts_command, static) = {
1987 .path = "show nat timeouts",
1988 .short_help = "show nat timeouts",
1989 .function = nat_show_timeouts_command_fn,
1994 * @cliexstart{nat set logging level}
1995 * To set NAT logging level use:
1996 * Set nat logging level
1999 VLIB_CLI_COMMAND (snat_set_log_level_command, static) = {
2000 .path = "nat set logging level",
2001 .function = snat_set_log_level_command_fn,
2002 .short_help = "nat set logging level <level>",
2007 * @cliexstart{snat ipfix logging}
2008 * To enable NAT IPFIX logging use:
2009 * vpp# nat ipfix logging
2010 * To set IPFIX exporter use:
2011 * vpp# set ipfix exporter collector 10.10.10.3 src 10.10.10.1
2014 VLIB_CLI_COMMAND (snat_ipfix_logging_enable_disable_command, static) = {
2015 .path = "nat ipfix logging",
2016 .function = snat_ipfix_logging_enable_disable_command_fn,
2017 .short_help = "nat ipfix logging [domain <domain-id>] [src-port <port>] [disable]",
2022 * @cliexstart{nat addr-port-assignment-alg}
2023 * Set address and port assignment algorithm
2024 * For the MAP-E CE limit port choice based on PSID use:
2025 * vpp# nat addr-port-assignment-alg map-e psid 10 psid-offset 6 psid-len 6
2026 * For port range use:
2027 * vpp# nat addr-port-assignment-alg port-range <start-port> - <end-port>
2028 * To set standard (default) address and port assignment algorithm use:
2029 * vpp# nat addr-port-assignment-alg default
2032 VLIB_CLI_COMMAND (nat44_set_alloc_addr_and_port_alg_command, static) = {
2033 .path = "nat addr-port-assignment-alg",
2034 .short_help = "nat addr-port-assignment-alg <alg-name> [<alg-params>]",
2035 .function = nat44_set_alloc_addr_and_port_alg_command_fn,
2040 * @cliexstart{show nat addr-port-assignment-alg}
2041 * Show address and port assignment algorithm
2044 VLIB_CLI_COMMAND (nat44_show_alloc_addr_and_port_alg_command, static) = {
2045 .path = "show nat addr-port-assignment-alg",
2046 .short_help = "show nat addr-port-assignment-alg",
2047 .function = nat44_show_alloc_addr_and_port_alg_command_fn,
2052 * @cliexstart{nat mss-clamping}
2053 * Set TCP MSS rewriting configuration
2054 * To enable TCP MSS rewriting use:
2055 * vpp# nat mss-clamping 1452
2056 * To disbale TCP MSS rewriting use:
2057 * vpp# nat mss-clamping disable
2060 VLIB_CLI_COMMAND (nat_set_mss_clamping_command, static) = {
2061 .path = "nat mss-clamping",
2062 .short_help = "nat mss-clamping <mss-value>|disable",
2063 .function = nat_set_mss_clamping_command_fn,
2068 * @cliexstart{show nat mss-clamping}
2069 * Show TCP MSS rewriting configuration
2072 VLIB_CLI_COMMAND (nat_show_mss_clamping_command, static) = {
2073 .path = "show nat mss-clamping",
2074 .short_help = "show nat mss-clamping",
2075 .function = nat_show_mss_clamping_command_fn,
2080 * @cliexstart{nat ha failover}
2081 * Set HA failover (remote settings)
2084 VLIB_CLI_COMMAND (nat_ha_failover_command, static) = {
2085 .path = "nat ha failover",
2086 .short_help = "nat ha failover <ip4-address>:<port> [refresh-interval <sec>]",
2087 .function = nat_ha_failover_command_fn,
2092 * @cliexstart{nat ha listener}
2093 * Set HA listener (local settings)
2096 VLIB_CLI_COMMAND (nat_ha_listener_command, static) = {
2097 .path = "nat ha listener",
2098 .short_help = "nat ha listener <ip4-address>:<port> [path-mtu <path-mtu>]",
2099 .function = nat_ha_listener_command_fn,
2104 * @cliexstart{show nat ha}
2105 * Show HA configuration/status
2108 VLIB_CLI_COMMAND (nat_show_ha_command, static) = {
2109 .path = "show nat ha",
2110 .short_help = "show nat ha",
2111 .function = nat_show_ha_command_fn,
2116 * @cliexstart{nat ha flush}
2117 * Flush the current HA data (for testing)
2120 VLIB_CLI_COMMAND (nat_ha_flush_command, static) = {
2121 .path = "nat ha flush",
2122 .short_help = "nat ha flush",
2123 .function = nat_ha_flush_command_fn,
2128 * @cliexstart{nat ha resync}
2129 * Resync HA (resend existing sessions to new failover)
2132 VLIB_CLI_COMMAND (nat_ha_resync_command, static) = {
2133 .path = "nat ha resync",
2134 .short_help = "nat ha resync",
2135 .function = nat_ha_resync_command_fn,
2140 * @cliexstart{show nat44 hash tables}
2141 * Show NAT44 hash tables
2144 VLIB_CLI_COMMAND (nat44_show_hash, static) = {
2145 .path = "show nat44 hash tables",
2146 .short_help = "show nat44 hash tables [detail|verbose]",
2147 .function = nat44_show_hash_command_fn,
2152 * @cliexstart{nat44 add address}
2153 * Add/delete NAT44 pool address.
2154 * To add NAT44 pool address use:
2155 * vpp# nat44 add address 172.16.1.3
2156 * vpp# nat44 add address 172.16.2.2 - 172.16.2.24
2157 * To add NAT44 pool address for specific tenant (identified by VRF id) use:
2158 * vpp# nat44 add address 172.16.1.3 tenant-vrf 10
2161 VLIB_CLI_COMMAND (add_address_command, static) = {
2162 .path = "nat44 add address",
2163 .short_help = "nat44 add address <ip4-range-start> [- <ip4-range-end>] "
2164 "[tenant-vrf <vrf-id>] [twice-nat] [del]",
2165 .function = add_address_command_fn,
2170 * @cliexstart{show nat44 summary}
2171 * Show NAT44 summary
2172 * vpp# show nat44 summary
2175 VLIB_CLI_COMMAND (nat44_show_summary_command, static) = {
2176 .path = "show nat44 summary",
2177 .short_help = "show nat44 summary",
2178 .function = nat44_show_summary_command_fn,
2183 * @cliexstart{show nat44 addresses}
2184 * Show NAT44 pool addresses.
2185 * vpp# show nat44 addresses
2186 * NAT44 pool addresses:
2188 * tenant VRF independent
2197 * NAT44 twice-nat pool addresses:
2199 * tenant VRF independent
2205 VLIB_CLI_COMMAND (nat44_show_addresses_command, static) = {
2206 .path = "show nat44 addresses",
2207 .short_help = "show nat44 addresses",
2208 .function = nat44_show_addresses_command_fn,
2213 * @cliexstart{set interface nat44}
2214 * Enable/disable NAT44 feature on the interface.
2215 * To enable NAT44 feature with local network interface use:
2216 * vpp# set interface nat44 in GigabitEthernet0/8/0
2217 * To enable NAT44 feature with external network interface use:
2218 * vpp# set interface nat44 out GigabitEthernet0/a/0
2221 VLIB_CLI_COMMAND (set_interface_snat_command, static) = {
2222 .path = "set interface nat44",
2223 .function = snat_feature_command_fn,
2224 .short_help = "set interface nat44 in <intfc> out <intfc> [output-feature] "
2230 * @cliexstart{show nat44 interfaces}
2231 * Show interfaces with NAT44 feature.
2232 * vpp# show nat44 interfaces
2234 * GigabitEthernet0/8/0 in
2235 * GigabitEthernet0/a/0 out
2238 VLIB_CLI_COMMAND (nat44_show_interfaces_command, static) = {
2239 .path = "show nat44 interfaces",
2240 .short_help = "show nat44 interfaces",
2241 .function = nat44_show_interfaces_command_fn,
2246 * @cliexstart{nat44 add static mapping}
2247 * Static mapping allows hosts on the external network to initiate connection
2248 * to to the local network host.
2249 * To create static mapping between local host address 10.0.0.3 port 6303 and
2250 * external address 4.4.4.4 port 3606 for TCP protocol use:
2251 * vpp# nat44 add static mapping tcp local 10.0.0.3 6303 external 4.4.4.4 3606
2252 * If not runnig "static mapping only" NAT plugin mode use before:
2253 * vpp# nat44 add address 4.4.4.4
2254 * To create address only static mapping between local and external address use:
2255 * vpp# nat44 add static mapping local 10.0.0.3 external 4.4.4.4
2256 * To create ICMP static mapping between local and external with ICMP echo
2257 * identifier 10 use:
2258 * vpp# nat44 add static mapping icmp local 10.0.0.3 10 external 4.4.4.4 10
2259 * To force use of specific pool address, vrf independent
2260 * vpp# nat44 add static mapping local 10.0.0.2 1234 external 10.0.2.2 1234 twice-nat exact 10.0.1.2
2263 VLIB_CLI_COMMAND (add_static_mapping_command, static) = {
2264 .path = "nat44 add static mapping",
2265 .function = add_static_mapping_command_fn,
2267 "nat44 add static mapping tcp|udp|icmp local <addr> [<port|icmp-echo-id>] "
2268 "external <addr> [<port|icmp-echo-id>] [vrf <table-id>] [twice-nat|self-twice-nat] "
2269 "[out2in-only] [exact <pool-addr>] [del]",
2274 * @cliexstart{nat44 add identity mapping}
2275 * Identity mapping translate an IP address to itself.
2276 * To create identity mapping for address 10.0.0.3 port 6303 for TCP protocol
2278 * vpp# nat44 add identity mapping 10.0.0.3 tcp 6303
2279 * To create identity mapping for address 10.0.0.3 use:
2280 * vpp# nat44 add identity mapping 10.0.0.3
2281 * To create identity mapping for DHCP addressed interface use:
2282 * vpp# nat44 add identity mapping external GigabitEthernet0/a/0 tcp 3606
2285 VLIB_CLI_COMMAND (add_identity_mapping_command, static) = {
2286 .path = "nat44 add identity mapping",
2287 .function = add_identity_mapping_command_fn,
2288 .short_help = "nat44 add identity mapping <ip4-addr>|external <interface> "
2289 "[<protocol> <port>] [vrf <table-id>] [del]",
2294 * @cliexstart{nat44 add load-balancing static mapping}
2295 * Service load balancing using NAT44
2296 * To add static mapping with load balancing for service with external IP
2297 * address 1.2.3.4 and TCP port 80 and mapped to 2 local servers
2298 * 10.100.10.10:8080 and 10.100.10.20:8080 with probability 80% resp. 20% use:
2299 * 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
2302 VLIB_CLI_COMMAND (add_lb_static_mapping_command, static) = {
2303 .path = "nat44 add load-balancing static mapping",
2304 .function = add_lb_static_mapping_command_fn,
2306 "nat44 add load-balancing static mapping protocol tcp|udp "
2307 "external <addr>:<port> local <addr>:<port> [vrf <table-id>] "
2308 "probability <n> [twice-nat|self-twice-nat] [out2in-only] "
2309 "[affinity <timeout-seconds>] [del]",
2314 * @cliexstart{nat44 add load-balancing static mapping}
2315 * Modify service load balancing using NAT44
2316 * To add new back-end server 10.100.10.30:8080 for service load balancing
2317 * static mapping with external IP address 1.2.3.4 and TCP port 80 use:
2318 * vpp# nat44 add load-balancing back-end protocol tcp external 1.2.3.4:80 local 10.100.10.30:8080 probability 25
2321 VLIB_CLI_COMMAND (add_lb_backend_command, static) = {
2322 .path = "nat44 add load-balancing back-end",
2323 .function = add_lb_backend_command_fn,
2325 "nat44 add load-balancing back-end protocol tcp|udp "
2326 "external <addr>:<port> local <addr>:<port> [vrf <table-id>] "
2327 "probability <n> [del]",
2332 * @cliexstart{show nat44 static mappings}
2333 * Show NAT44 static mappings.
2334 * vpp# show nat44 static mappings
2335 * NAT44 static mappings:
2336 * local 10.0.0.3 external 4.4.4.4 vrf 0
2337 * tcp local 192.168.0.4:6303 external 4.4.4.3:3606 vrf 0
2338 * tcp vrf 0 external 1.2.3.4:80 out2in-only
2339 * local 10.100.10.10:8080 probability 80
2340 * local 10.100.10.20:8080 probability 20
2341 * tcp local 10.100.3.8:8080 external 169.10.10.1:80 vrf 0 twice-nat
2342 * tcp local 10.0.0.10:3603 external GigabitEthernet0/a/0:6306 vrf 10
2345 VLIB_CLI_COMMAND (nat44_show_static_mappings_command, static) = {
2346 .path = "show nat44 static mappings",
2347 .short_help = "show nat44 static mappings",
2348 .function = nat44_show_static_mappings_command_fn,
2353 * @cliexstart{nat44 add interface address}
2354 * Use NAT44 pool address from specific interfce
2355 * To add NAT44 pool address from specific interface use:
2356 * vpp# nat44 add interface address GigabitEthernet0/8/0
2359 VLIB_CLI_COMMAND (snat_add_interface_address_command, static) = {
2360 .path = "nat44 add interface address",
2361 .short_help = "nat44 add interface address <interface> [twice-nat] [del]",
2362 .function = snat_add_interface_address_command_fn,
2367 * @cliexstart{show nat44 interface address}
2368 * Show NAT44 pool address interfaces
2369 * vpp# show nat44 interface address
2370 * NAT44 pool address interfaces:
2371 * GigabitEthernet0/a/0
2372 * NAT44 twice-nat pool address interfaces:
2373 * GigabitEthernet0/8/0
2376 VLIB_CLI_COMMAND (nat44_show_interface_address_command, static) = {
2377 .path = "show nat44 interface address",
2378 .short_help = "show nat44 interface address",
2379 .function = nat44_show_interface_address_command_fn,
2384 * @cliexstart{show nat44 sessions}
2385 * Show NAT44 sessions.
2388 VLIB_CLI_COMMAND (nat44_show_sessions_command, static) = {
2389 .path = "show nat44 sessions",
2390 .short_help = "show nat44 sessions [detail|metrics]",
2391 .function = nat44_show_sessions_command_fn,
2396 * @cliexstart{set nat44 session limit}
2397 * Set NAT44 session limit.
2400 VLIB_CLI_COMMAND (nat44_set_session_limit_command, static) = {
2401 .path = "set nat44 session limit",
2402 .short_help = "set nat44 session limit <limit> [vrf <table-id>]",
2403 .function = nat44_set_session_limit_command_fn,
2408 * @cliexstart{nat44 del user}
2409 * To delete all NAT44 user sessions:
2410 * vpp# nat44 del user 10.0.0.3
2413 VLIB_CLI_COMMAND (nat44_del_user_command, static) = {
2414 .path = "nat44 del user",
2415 .short_help = "nat44 del user <addr> [fib <index>]",
2416 .function = nat44_del_user_command_fn,
2421 * @cliexstart{clear nat44 sessions}
2422 * To clear all NAT44 sessions
2423 * vpp# clear nat44 sessions
2426 VLIB_CLI_COMMAND (nat44_clear_sessions_command, static) = {
2427 .path = "clear nat44 sessions",
2428 .short_help = "clear nat44 sessions",
2429 .function = nat44_clear_sessions_command_fn,
2434 * @cliexstart{nat44 del session}
2435 * To administratively delete NAT44 session by inside address and port use:
2436 * vpp# nat44 del session in 10.0.0.3:6303 tcp
2437 * To administratively delete NAT44 session by outside address and port use:
2438 * vpp# nat44 del session out 1.0.0.3:6033 udp
2441 VLIB_CLI_COMMAND (nat44_del_session_command, static) = {
2442 .path = "nat44 del session",
2443 .short_help = "nat44 del session in|out <addr>:<port> tcp|udp|icmp [vrf <id>] [external-host <addr>:<port>]",
2444 .function = nat44_del_session_command_fn,
2449 * @cliexstart{nat44 forwarding}
2450 * Enable or disable forwarding
2451 * Forward packets which don't match existing translation
2452 * or static mapping instead of dropping them.
2453 * To enable forwarding, use:
2454 * vpp# nat44 forwarding enable
2455 * To disable forwarding, use:
2456 * vpp# nat44 forwarding disable
2459 VLIB_CLI_COMMAND (snat_forwarding_set_command, static) = {
2460 .path = "nat44 forwarding",
2461 .short_help = "nat44 forwarding enable|disable",
2462 .function = snat_forwarding_set_command_fn,
2468 * fd.io coding-style-patch-verification: ON
2471 * eval: (c-set-style "gnu")