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/nat_det.h>
23 #include <nat/nat64.h>
24 #include <nat/nat_inlines.h>
25 #include <nat/nat_affinity.h>
26 #include <vnet/fib/fib_table.h>
28 #define UNSUPPORTED_IN_DET_MODE_STR \
29 "This command is unsupported in deterministic mode"
30 #define SUPPORTED_ONLY_IN_DET_MODE_STR \
31 "This command is supported only in deterministic mode"
34 set_workers_command_fn (vlib_main_t * vm,
35 unformat_input_t * input, vlib_cli_command_t * cmd)
37 unformat_input_t _line_input, *line_input = &_line_input;
38 snat_main_t *sm = &snat_main;
41 clib_error_t *error = 0;
43 if (sm->deterministic)
44 return clib_error_return (0, UNSUPPORTED_IN_DET_MODE_STR);
46 /* Get a line of input. */
47 if (!unformat_user (input, unformat_line_input, line_input))
50 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
52 if (unformat (line_input, "%U", unformat_bitmap_list, &bitmap))
56 error = clib_error_return (0, "unknown input '%U'",
57 format_unformat_error, line_input);
64 error = clib_error_return (0, "List of workers must be specified.");
68 rv = snat_set_workers (bitmap);
70 clib_bitmap_free (bitmap);
74 case VNET_API_ERROR_INVALID_WORKER:
75 error = clib_error_return (0, "Invalid worker(s).");
77 case VNET_API_ERROR_FEATURE_DISABLED:
78 error = clib_error_return (0,
79 "Supported only if 2 or more workes available.");
86 unformat_free (line_input);
92 nat_show_workers_commnad_fn (vlib_main_t * vm, unformat_input_t * input,
93 vlib_cli_command_t * cmd)
95 snat_main_t *sm = &snat_main;
98 if (sm->deterministic)
99 return clib_error_return (0, UNSUPPORTED_IN_DET_MODE_STR);
101 if (sm->num_workers > 1)
103 vlib_cli_output (vm, "%d workers", vec_len (sm->workers));
105 vec_foreach (worker, sm->workers)
107 vlib_worker_thread_t *w =
108 vlib_worker_threads + *worker + sm->first_worker_index;
109 vlib_cli_output (vm, " %s", w->name);
117 static clib_error_t *
118 snat_ipfix_logging_enable_disable_command_fn (vlib_main_t * vm,
119 unformat_input_t * input,
120 vlib_cli_command_t * cmd)
122 unformat_input_t _line_input, *line_input = &_line_input;
127 clib_error_t *error = 0;
129 /* Get a line of input. */
130 if (!unformat_user (input, unformat_line_input, line_input))
133 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
135 if (unformat (line_input, "domain %d", &domain_id))
137 else if (unformat (line_input, "src-port %d", &src_port))
139 else if (unformat (line_input, "disable"))
143 error = clib_error_return (0, "unknown input '%U'",
144 format_unformat_error, line_input);
149 rv = snat_ipfix_logging_enable_disable (enable, domain_id, (u16) src_port);
153 error = clib_error_return (0, "ipfix logging enable failed");
158 unformat_free (line_input);
163 static clib_error_t *
164 nat44_show_hash_commnad_fn (vlib_main_t * vm, unformat_input_t * input,
165 vlib_cli_command_t * cmd)
167 snat_main_t *sm = &snat_main;
168 snat_main_per_thread_data_t *tsm;
169 nat_affinity_main_t *nam = &nat_affinity_main;
173 if (unformat (input, "detail"))
175 else if (unformat (input, "verbose"))
178 vlib_cli_output (vm, "%U", format_bihash_8_8, &sm->static_mapping_by_local,
180 vlib_cli_output (vm, "%U",
181 format_bihash_8_8, &sm->static_mapping_by_external,
183 vec_foreach_index (i, sm->per_thread_data)
185 tsm = vec_elt_at_index (sm->per_thread_data, i);
186 vlib_cli_output (vm, "-------- thread %d %s --------\n",
187 i, vlib_worker_threads[i].name);
188 if (sm->endpoint_dependent)
190 vlib_cli_output (vm, "%U", format_bihash_16_8, &tsm->in2out_ed,
192 vlib_cli_output (vm, "%U", format_bihash_16_8, &tsm->out2in_ed,
197 vlib_cli_output (vm, "%U", format_bihash_8_8, &tsm->in2out, verbose);
198 vlib_cli_output (vm, "%U", format_bihash_8_8, &tsm->out2in, verbose);
200 vlib_cli_output (vm, "%U", format_bihash_8_8, &tsm->user_hash, verbose);
203 if (sm->endpoint_dependent)
204 vlib_cli_output (vm, "%U", format_bihash_16_8, &nam->affinity_hash,
209 static clib_error_t *
210 nat44_set_alloc_addr_and_port_alg_command_fn (vlib_main_t * vm,
211 unformat_input_t * input,
212 vlib_cli_command_t * cmd)
214 unformat_input_t _line_input, *line_input = &_line_input;
215 snat_main_t *sm = &snat_main;
216 clib_error_t *error = 0;
217 u32 psid, psid_offset, psid_length, port_start, port_end;
219 if (sm->deterministic)
220 return clib_error_return (0, UNSUPPORTED_IN_DET_MODE_STR);
222 /* Get a line of input. */
223 if (!unformat_user (input, unformat_line_input, line_input))
226 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
228 if (unformat (line_input, "default"))
229 nat_set_alloc_addr_and_port_default ();
232 (line_input, "map-e psid %d psid-offset %d psid-len %d", &psid,
233 &psid_offset, &psid_length))
234 nat_set_alloc_addr_and_port_mape ((u16) psid, (u16) psid_offset,
238 (line_input, "port-range %d - %d", &port_start, &port_end))
240 if (port_end <= port_start)
243 clib_error_return (0,
244 "The end-port must be greater than start-port");
247 nat_set_alloc_addr_and_port_range ((u16) port_start,
252 error = clib_error_return (0, "unknown input '%U'",
253 format_unformat_error, line_input);
259 unformat_free (line_input);
264 static clib_error_t *
265 nat44_show_alloc_addr_and_port_alg_command_fn (vlib_main_t * vm,
266 unformat_input_t * input,
267 vlib_cli_command_t * cmd)
269 snat_main_t *sm = &snat_main;
271 if (sm->deterministic)
272 return clib_error_return (0, UNSUPPORTED_IN_DET_MODE_STR);
274 vlib_cli_output (vm, "NAT address and port: %U",
275 format_nat_addr_and_port_alloc_alg,
276 sm->addr_and_port_alloc_alg);
277 switch (sm->addr_and_port_alloc_alg)
279 case NAT_ADDR_AND_PORT_ALLOC_ALG_MAPE:
280 vlib_cli_output (vm, " psid %d psid-offset %d psid-len %d", sm->psid,
281 sm->psid_offset, sm->psid_length);
283 case NAT_ADDR_AND_PORT_ALLOC_ALG_RANGE:
284 vlib_cli_output (vm, " start-port %d end-port %d", sm->start_port,
294 static clib_error_t *
295 add_address_command_fn (vlib_main_t * vm,
296 unformat_input_t * input, vlib_cli_command_t * cmd)
298 unformat_input_t _line_input, *line_input = &_line_input;
299 snat_main_t *sm = &snat_main;
300 ip4_address_t start_addr, end_addr, this_addr;
301 u32 start_host_order, end_host_order;
306 clib_error_t *error = 0;
309 if (sm->deterministic)
310 return clib_error_return (0, UNSUPPORTED_IN_DET_MODE_STR);
312 /* Get a line of input. */
313 if (!unformat_user (input, unformat_line_input, line_input))
316 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
318 if (unformat (line_input, "%U - %U",
319 unformat_ip4_address, &start_addr,
320 unformat_ip4_address, &end_addr))
322 else if (unformat (line_input, "tenant-vrf %u", &vrf_id))
324 else if (unformat (line_input, "%U", unformat_ip4_address, &start_addr))
325 end_addr = start_addr;
326 else if (unformat (line_input, "twice-nat"))
328 else if (unformat (line_input, "del"))
332 error = clib_error_return (0, "unknown input '%U'",
333 format_unformat_error, line_input);
338 if (sm->static_mapping_only)
340 error = clib_error_return (0, "static mapping only mode");
344 start_host_order = clib_host_to_net_u32 (start_addr.as_u32);
345 end_host_order = clib_host_to_net_u32 (end_addr.as_u32);
347 if (end_host_order < start_host_order)
349 error = clib_error_return (0, "end address less than start address");
353 count = (end_host_order - start_host_order) + 1;
356 nat_log_info ("%U - %U, %d addresses...",
357 format_ip4_address, &start_addr,
358 format_ip4_address, &end_addr, count);
360 this_addr = start_addr;
362 for (i = 0; i < count; i++)
365 rv = snat_add_address (sm, &this_addr, vrf_id, twice_nat);
367 rv = snat_del_address (sm, this_addr, 0, twice_nat);
371 case VNET_API_ERROR_VALUE_EXIST:
372 error = clib_error_return (0, "NAT address already in use.");
374 case VNET_API_ERROR_NO_SUCH_ENTRY:
375 error = clib_error_return (0, "NAT address not exist.");
377 case VNET_API_ERROR_UNSPECIFIED:
379 clib_error_return (0, "NAT address used in static mapping.");
381 case VNET_API_ERROR_FEATURE_DISABLED:
383 clib_error_return (0,
384 "twice NAT available only for endpoint-dependent mode.");
391 nat44_add_del_address_dpo (this_addr, is_add);
393 increment_v4_address (&this_addr);
397 unformat_free (line_input);
402 static clib_error_t *
403 nat44_show_addresses_command_fn (vlib_main_t * vm, unformat_input_t * input,
404 vlib_cli_command_t * cmd)
406 snat_main_t *sm = &snat_main;
409 if (sm->deterministic)
410 return clib_error_return (0, UNSUPPORTED_IN_DET_MODE_STR);
412 vlib_cli_output (vm, "NAT44 pool addresses:");
414 vec_foreach (ap, sm->addresses)
416 vlib_cli_output (vm, "%U", format_ip4_address, &ap->addr);
417 if (ap->fib_index != ~0)
418 vlib_cli_output (vm, " tenant VRF: %u",
419 fib_table_get(ap->fib_index, FIB_PROTOCOL_IP4)->ft_table_id);
421 vlib_cli_output (vm, " tenant VRF independent");
422 #define _(N, i, n, s) \
423 vlib_cli_output (vm, " %d busy %s ports", ap->busy_##n##_ports, s);
424 foreach_snat_protocol
427 vlib_cli_output (vm, "NAT44 twice-nat pool addresses:");
428 vec_foreach (ap, sm->twice_nat_addresses)
430 vlib_cli_output (vm, "%U", format_ip4_address, &ap->addr);
431 if (ap->fib_index != ~0)
432 vlib_cli_output (vm, " tenant VRF: %u",
433 fib_table_get(ap->fib_index, FIB_PROTOCOL_IP4)->ft_table_id);
435 vlib_cli_output (vm, " tenant VRF independent");
436 #define _(N, i, n, s) \
437 vlib_cli_output (vm, " %d busy %s ports", ap->busy_##n##_ports, s);
438 foreach_snat_protocol
445 static clib_error_t *
446 snat_feature_command_fn (vlib_main_t * vm,
447 unformat_input_t * input, vlib_cli_command_t * cmd)
449 unformat_input_t _line_input, *line_input = &_line_input;
450 vnet_main_t *vnm = vnet_get_main ();
451 clib_error_t *error = 0;
453 u32 *inside_sw_if_indices = 0;
454 u32 *outside_sw_if_indices = 0;
455 u8 is_output_feature = 0;
461 /* Get a line of input. */
462 if (!unformat_user (input, unformat_line_input, line_input))
465 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
467 if (unformat (line_input, "in %U", unformat_vnet_sw_interface,
469 vec_add1 (inside_sw_if_indices, sw_if_index);
470 else if (unformat (line_input, "out %U", unformat_vnet_sw_interface,
472 vec_add1 (outside_sw_if_indices, sw_if_index);
473 else if (unformat (line_input, "output-feature"))
474 is_output_feature = 1;
475 else if (unformat (line_input, "del"))
479 error = clib_error_return (0, "unknown input '%U'",
480 format_unformat_error, line_input);
485 if (vec_len (inside_sw_if_indices))
487 for (i = 0; i < vec_len (inside_sw_if_indices); i++)
489 sw_if_index = inside_sw_if_indices[i];
490 if (is_output_feature)
492 if (snat_interface_add_del_output_feature
493 (sw_if_index, 1, is_del))
495 error = clib_error_return (0, "%s %U failed",
496 is_del ? "del" : "add",
497 format_vnet_sw_if_index_name,
504 if (snat_interface_add_del (sw_if_index, 1, is_del))
506 error = clib_error_return (0, "%s %U failed",
507 is_del ? "del" : "add",
508 format_vnet_sw_if_index_name,
516 if (vec_len (outside_sw_if_indices))
518 for (i = 0; i < vec_len (outside_sw_if_indices); i++)
520 sw_if_index = outside_sw_if_indices[i];
521 if (is_output_feature)
523 if (snat_interface_add_del_output_feature
524 (sw_if_index, 0, is_del))
526 error = clib_error_return (0, "%s %U failed",
527 is_del ? "del" : "add",
528 format_vnet_sw_if_index_name,
535 if (snat_interface_add_del (sw_if_index, 0, is_del))
537 error = clib_error_return (0, "%s %U failed",
538 is_del ? "del" : "add",
539 format_vnet_sw_if_index_name,
548 unformat_free (line_input);
549 vec_free (inside_sw_if_indices);
550 vec_free (outside_sw_if_indices);
555 static clib_error_t *
556 nat44_show_interfaces_command_fn (vlib_main_t * vm, unformat_input_t * input,
557 vlib_cli_command_t * cmd)
559 snat_main_t *sm = &snat_main;
561 vnet_main_t *vnm = vnet_get_main ();
563 vlib_cli_output (vm, "NAT44 interfaces:");
565 pool_foreach (i, sm->interfaces,
567 vlib_cli_output (vm, " %U %s", format_vnet_sw_if_index_name, vnm,
569 (nat_interface_is_inside(i) &&
570 nat_interface_is_outside(i)) ? "in out" :
571 (nat_interface_is_inside(i) ? "in" : "out"));
574 pool_foreach (i, sm->output_feature_interfaces,
576 vlib_cli_output (vm, " %U output-feature %s",
577 format_vnet_sw_if_index_name, vnm,
579 (nat_interface_is_inside(i) &&
580 nat_interface_is_outside(i)) ? "in out" :
581 (nat_interface_is_inside(i) ? "in" : "out"));
588 static clib_error_t *
589 add_static_mapping_command_fn (vlib_main_t * vm,
590 unformat_input_t * input,
591 vlib_cli_command_t * cmd)
593 unformat_input_t _line_input, *line_input = &_line_input;
594 snat_main_t *sm = &snat_main;
595 clib_error_t *error = 0;
596 ip4_address_t l_addr, e_addr;
597 u32 l_port = 0, e_port = 0, vrf_id = ~0;
600 u32 sw_if_index = ~0;
601 vnet_main_t *vnm = vnet_get_main ();
603 snat_protocol_t proto = ~0;
605 twice_nat_type_t twice_nat = TWICE_NAT_DISABLED;
608 if (sm->deterministic)
609 return clib_error_return (0, UNSUPPORTED_IN_DET_MODE_STR);
611 /* Get a line of input. */
612 if (!unformat_user (input, unformat_line_input, line_input))
615 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
617 if (unformat (line_input, "local %U %u", unformat_ip4_address, &l_addr,
621 if (unformat (line_input, "local %U", unformat_ip4_address, &l_addr))
623 else if (unformat (line_input, "external %U %u", unformat_ip4_address,
626 else if (unformat (line_input, "external %U", unformat_ip4_address,
629 else if (unformat (line_input, "external %U %u",
630 unformat_vnet_sw_interface, vnm, &sw_if_index,
634 else if (unformat (line_input, "external %U",
635 unformat_vnet_sw_interface, vnm, &sw_if_index))
637 else if (unformat (line_input, "vrf %u", &vrf_id))
639 else if (unformat (line_input, "%U", unformat_snat_protocol, &proto))
641 else if (unformat (line_input, "twice-nat"))
642 twice_nat = TWICE_NAT;
643 else if (unformat (line_input, "self-twice-nat"))
644 twice_nat = TWICE_NAT_SELF;
645 else if (unformat (line_input, "out2in-only"))
647 else if (unformat (line_input, "del"))
651 error = clib_error_return (0, "unknown input: '%U'",
652 format_unformat_error, line_input);
657 if (twice_nat && addr_only)
659 error = clib_error_return (0, "twice NAT only for 1:1 NAPT");
663 if (!addr_only && !proto_set)
665 error = clib_error_return (0, "missing protocol");
669 rv = snat_add_static_mapping (l_addr, e_addr, (u16) l_port, (u16) e_port,
670 vrf_id, addr_only, sw_if_index, proto, is_add,
671 twice_nat, out2in_only, 0);
675 case VNET_API_ERROR_INVALID_VALUE:
676 error = clib_error_return (0, "External port already in use.");
678 case VNET_API_ERROR_NO_SUCH_ENTRY:
680 error = clib_error_return (0, "External addres must be allocated.");
682 error = clib_error_return (0, "Mapping not exist.");
684 case VNET_API_ERROR_NO_SUCH_FIB:
685 error = clib_error_return (0, "No such VRF id.");
687 case VNET_API_ERROR_VALUE_EXIST:
688 error = clib_error_return (0, "Mapping already exist.");
690 case VNET_API_ERROR_FEATURE_DISABLED:
692 clib_error_return (0,
693 "twice-nat/out2in-only available only for endpoint-dependent mode.");
700 unformat_free (line_input);
705 static clib_error_t *
706 add_identity_mapping_command_fn (vlib_main_t * vm,
707 unformat_input_t * input,
708 vlib_cli_command_t * cmd)
710 unformat_input_t _line_input, *line_input = &_line_input;
711 snat_main_t *sm = &snat_main;
712 clib_error_t *error = 0;
714 u32 port = 0, vrf_id = ~0;
717 u32 sw_if_index = ~0;
718 vnet_main_t *vnm = vnet_get_main ();
720 snat_protocol_t proto;
722 if (sm->deterministic)
723 return clib_error_return (0, UNSUPPORTED_IN_DET_MODE_STR);
727 /* Get a line of input. */
728 if (!unformat_user (input, unformat_line_input, line_input))
731 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
733 if (unformat (line_input, "%U", unformat_ip4_address, &addr))
735 else if (unformat (line_input, "external %U",
736 unformat_vnet_sw_interface, vnm, &sw_if_index))
738 else if (unformat (line_input, "vrf %u", &vrf_id))
740 else if (unformat (line_input, "%U %u", unformat_snat_protocol, &proto,
743 else if (unformat (line_input, "del"))
747 error = clib_error_return (0, "unknown input: '%U'",
748 format_unformat_error, line_input);
753 rv = snat_add_static_mapping (addr, addr, (u16) port, (u16) port,
754 vrf_id, addr_only, sw_if_index, proto, is_add,
759 case VNET_API_ERROR_INVALID_VALUE:
760 error = clib_error_return (0, "External port already in use.");
762 case VNET_API_ERROR_NO_SUCH_ENTRY:
764 error = clib_error_return (0, "External addres must be allocated.");
766 error = clib_error_return (0, "Mapping not exist.");
768 case VNET_API_ERROR_NO_SUCH_FIB:
769 error = clib_error_return (0, "No such VRF id.");
771 case VNET_API_ERROR_VALUE_EXIST:
772 error = clib_error_return (0, "Mapping already exist.");
779 unformat_free (line_input);
784 static clib_error_t *
785 add_lb_static_mapping_command_fn (vlib_main_t * vm,
786 unformat_input_t * input,
787 vlib_cli_command_t * cmd)
789 unformat_input_t _line_input, *line_input = &_line_input;
790 snat_main_t *sm = &snat_main;
791 clib_error_t *error = 0;
792 ip4_address_t l_addr, e_addr;
793 u32 l_port = 0, e_port = 0, vrf_id = 0, probability = 0, affinity = 0;
796 snat_protocol_t proto;
798 nat44_lb_addr_port_t *locals = 0, local;
799 twice_nat_type_t twice_nat = TWICE_NAT_DISABLED;
802 if (sm->deterministic)
803 return clib_error_return (0, UNSUPPORTED_IN_DET_MODE_STR);
805 /* Get a line of input. */
806 if (!unformat_user (input, unformat_line_input, line_input))
809 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
811 if (unformat (line_input, "local %U:%u probability %u",
812 unformat_ip4_address, &l_addr, &l_port, &probability))
814 memset (&local, 0, sizeof (local));
816 local.port = (u16) l_port;
817 local.probability = (u8) probability;
818 vec_add1 (locals, local);
820 else if (unformat (line_input, "local %U:%u vrf %u probability %u",
821 unformat_ip4_address, &l_addr, &l_port, &vrf_id,
824 memset (&local, 0, sizeof (local));
826 local.port = (u16) l_port;
827 local.probability = (u8) probability;
828 local.vrf_id = vrf_id;
829 vec_add1 (locals, local);
831 else if (unformat (line_input, "external %U:%u", unformat_ip4_address,
834 else if (unformat (line_input, "protocol %U", unformat_snat_protocol,
837 else if (unformat (line_input, "twice-nat"))
838 twice_nat = TWICE_NAT;
839 else if (unformat (line_input, "self-twice-nat"))
840 twice_nat = TWICE_NAT_SELF;
841 else if (unformat (line_input, "out2in-only"))
843 else if (unformat (line_input, "del"))
845 else if (unformat (line_input, "affinity %u", &affinity))
849 error = clib_error_return (0, "unknown input: '%U'",
850 format_unformat_error, line_input);
855 if (vec_len (locals) < 2)
857 error = clib_error_return (0, "at least two local must be set");
863 error = clib_error_return (0, "missing protocol");
867 rv = nat44_add_del_lb_static_mapping (e_addr, (u16) e_port, proto, locals,
868 is_add, twice_nat, out2in_only, 0,
873 case VNET_API_ERROR_INVALID_VALUE:
874 error = clib_error_return (0, "External port already in use.");
876 case VNET_API_ERROR_NO_SUCH_ENTRY:
878 error = clib_error_return (0, "External addres must be allocated.");
880 error = clib_error_return (0, "Mapping not exist.");
882 case VNET_API_ERROR_VALUE_EXIST:
883 error = clib_error_return (0, "Mapping already exist.");
885 case VNET_API_ERROR_FEATURE_DISABLED:
887 clib_error_return (0, "Available only for endpoint-dependent mode.");
894 unformat_free (line_input);
900 static clib_error_t *
901 nat44_show_static_mappings_command_fn (vlib_main_t * vm,
902 unformat_input_t * input,
903 vlib_cli_command_t * cmd)
905 snat_main_t *sm = &snat_main;
906 snat_static_mapping_t *m;
907 snat_static_map_resolve_t *rp;
909 if (sm->deterministic)
910 return clib_error_return (0, UNSUPPORTED_IN_DET_MODE_STR);
912 vlib_cli_output (vm, "NAT44 static mappings:");
914 pool_foreach (m, sm->static_mappings,
916 vlib_cli_output (vm, " %U", format_snat_static_mapping, m);
918 vec_foreach (rp, sm->to_resolve)
919 vlib_cli_output (vm, " %U", format_snat_static_map_to_resolve, rp);
925 static clib_error_t *
926 snat_add_interface_address_command_fn (vlib_main_t * vm,
927 unformat_input_t * input,
928 vlib_cli_command_t * cmd)
930 snat_main_t *sm = &snat_main;
931 unformat_input_t _line_input, *line_input = &_line_input;
935 clib_error_t *error = 0;
938 if (sm->deterministic)
939 return clib_error_return (0, UNSUPPORTED_IN_DET_MODE_STR);
941 /* Get a line of input. */
942 if (!unformat_user (input, unformat_line_input, line_input))
945 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
947 if (unformat (line_input, "%U", unformat_vnet_sw_interface,
948 sm->vnet_main, &sw_if_index))
950 else if (unformat (line_input, "twice-nat"))
952 else if (unformat (line_input, "del"))
956 error = clib_error_return (0, "unknown input '%U'",
957 format_unformat_error, line_input);
962 rv = snat_add_interface_address (sm, sw_if_index, is_del, twice_nat);
970 error = clib_error_return (0, "snat_add_interface_address returned %d",
976 unformat_free (line_input);
981 static clib_error_t *
982 nat44_show_interface_address_command_fn (vlib_main_t * vm,
983 unformat_input_t * input,
984 vlib_cli_command_t * cmd)
986 snat_main_t *sm = &snat_main;
987 vnet_main_t *vnm = vnet_get_main ();
990 if (sm->deterministic)
991 return clib_error_return (0, UNSUPPORTED_IN_DET_MODE_STR);
994 vlib_cli_output (vm, "NAT44 pool address interfaces:");
995 vec_foreach (sw_if_index, sm->auto_add_sw_if_indices)
997 vlib_cli_output (vm, " %U", format_vnet_sw_if_index_name, vnm,
1000 vlib_cli_output (vm, "NAT44 twice-nat pool address interfaces:");
1001 vec_foreach (sw_if_index, sm->auto_add_sw_if_indices_twice_nat)
1003 vlib_cli_output (vm, " %U", format_vnet_sw_if_index_name, vnm,
1011 static clib_error_t *
1012 nat44_show_sessions_command_fn (vlib_main_t * vm, unformat_input_t * input,
1013 vlib_cli_command_t * cmd)
1016 snat_main_t *sm = &snat_main;
1017 snat_main_per_thread_data_t *tsm;
1021 if (sm->deterministic)
1022 return clib_error_return (0, UNSUPPORTED_IN_DET_MODE_STR);
1024 if (unformat (input, "detail"))
1027 vlib_cli_output (vm, "NAT44 sessions:");
1030 vec_foreach_index (i, sm->per_thread_data)
1032 tsm = vec_elt_at_index (sm->per_thread_data, i);
1034 vlib_cli_output (vm, "-------- thread %d %s: %d sessions --------\n",
1035 i, vlib_worker_threads[i].name,
1036 pool_elts (tsm->sessions));
1037 pool_foreach (u, tsm->users,
1039 vlib_cli_output (vm, " %U", format_snat_user, tsm, u, verbose);
1047 static clib_error_t *
1048 nat44_del_session_command_fn (vlib_main_t * vm,
1049 unformat_input_t * input,
1050 vlib_cli_command_t * cmd)
1052 snat_main_t *sm = &snat_main;
1053 unformat_input_t _line_input, *line_input = &_line_input;
1054 int is_in = 0, is_ed = 0;
1055 clib_error_t *error = 0;
1056 ip4_address_t addr, eh_addr;
1057 u32 port = 0, eh_port = 0, vrf_id = sm->outside_vrf_id;
1058 snat_protocol_t proto;
1061 if (sm->deterministic)
1062 return clib_error_return (0, UNSUPPORTED_IN_DET_MODE_STR);
1064 /* Get a line of input. */
1065 if (!unformat_user (input, unformat_line_input, line_input))
1068 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1071 (line_input, "%U:%u %U", unformat_ip4_address, &addr, &port,
1072 unformat_snat_protocol, &proto))
1074 else if (unformat (line_input, "in"))
1077 vrf_id = sm->inside_vrf_id;
1079 else if (unformat (line_input, "out"))
1082 vrf_id = sm->outside_vrf_id;
1084 else if (unformat (line_input, "vrf %u", &vrf_id))
1088 (line_input, "external-host %U:%u", unformat_ip4_address,
1089 &eh_addr, &eh_port))
1093 error = clib_error_return (0, "unknown input '%U'",
1094 format_unformat_error, line_input);
1101 nat44_del_ed_session (sm, &addr, port, &eh_addr, eh_port,
1102 snat_proto_to_ip_proto (proto), vrf_id, is_in);
1104 rv = nat44_del_session (sm, &addr, port, proto, vrf_id, is_in);
1112 error = clib_error_return (0, "nat44_del_session returned %d", rv);
1117 unformat_free (line_input);
1122 static clib_error_t *
1123 snat_forwarding_set_command_fn (vlib_main_t * vm,
1124 unformat_input_t * input,
1125 vlib_cli_command_t * cmd)
1127 snat_main_t *sm = &snat_main;
1128 unformat_input_t _line_input, *line_input = &_line_input;
1129 u8 forwarding_enable;
1130 u8 forwarding_enable_set = 0;
1131 clib_error_t *error = 0;
1133 if (sm->deterministic)
1134 return clib_error_return (0, UNSUPPORTED_IN_DET_MODE_STR);
1136 /* Get a line of input. */
1137 if (!unformat_user (input, unformat_line_input, line_input))
1138 return clib_error_return (0, "'enable' or 'disable' expected");
1140 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1142 if (!forwarding_enable_set && unformat (line_input, "enable"))
1144 forwarding_enable = 1;
1145 forwarding_enable_set = 1;
1147 else if (!forwarding_enable_set && unformat (line_input, "disable"))
1149 forwarding_enable = 0;
1150 forwarding_enable_set = 1;
1154 error = clib_error_return (0, "unknown input '%U'",
1155 format_unformat_error, line_input);
1160 if (!forwarding_enable_set)
1162 error = clib_error_return (0, "'enable' or 'disable' expected");
1166 sm->forwarding_enabled = forwarding_enable;
1169 unformat_free (line_input);
1174 static clib_error_t *
1175 snat_det_map_command_fn (vlib_main_t * vm,
1176 unformat_input_t * input, vlib_cli_command_t * cmd)
1178 snat_main_t *sm = &snat_main;
1179 unformat_input_t _line_input, *line_input = &_line_input;
1180 ip4_address_t in_addr, out_addr;
1181 u32 in_plen, out_plen;
1183 clib_error_t *error = 0;
1185 if (!sm->deterministic)
1186 return clib_error_return (0, SUPPORTED_ONLY_IN_DET_MODE_STR);
1188 /* Get a line of input. */
1189 if (!unformat_user (input, unformat_line_input, line_input))
1192 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1195 (line_input, "in %U/%u", unformat_ip4_address, &in_addr, &in_plen))
1199 (line_input, "out %U/%u", unformat_ip4_address, &out_addr,
1202 else if (unformat (line_input, "del"))
1206 error = clib_error_return (0, "unknown input '%U'",
1207 format_unformat_error, line_input);
1212 rv = snat_det_add_map (sm, &in_addr, (u8) in_plen, &out_addr, (u8) out_plen,
1217 error = clib_error_return (0, "snat_det_add_map return %d", rv);
1222 unformat_free (line_input);
1227 static clib_error_t *
1228 nat44_det_show_mappings_command_fn (vlib_main_t * vm,
1229 unformat_input_t * input,
1230 vlib_cli_command_t * cmd)
1232 snat_main_t *sm = &snat_main;
1235 if (!sm->deterministic)
1236 return clib_error_return (0, SUPPORTED_ONLY_IN_DET_MODE_STR);
1238 vlib_cli_output (vm, "NAT44 deterministic mappings:");
1240 pool_foreach (dm, sm->det_maps,
1242 vlib_cli_output (vm, " in %U/%d out %U/%d\n",
1243 format_ip4_address, &dm->in_addr, dm->in_plen,
1244 format_ip4_address, &dm->out_addr, dm->out_plen);
1245 vlib_cli_output (vm, " outside address sharing ratio: %d\n",
1247 vlib_cli_output (vm, " number of ports per inside host: %d\n",
1248 dm->ports_per_host);
1249 vlib_cli_output (vm, " sessions number: %d\n", dm->ses_num);
1256 static clib_error_t *
1257 snat_det_forward_command_fn (vlib_main_t * vm,
1258 unformat_input_t * input,
1259 vlib_cli_command_t * cmd)
1261 snat_main_t *sm = &snat_main;
1262 unformat_input_t _line_input, *line_input = &_line_input;
1263 ip4_address_t in_addr, out_addr;
1266 clib_error_t *error = 0;
1268 if (!sm->deterministic)
1269 return clib_error_return (0, SUPPORTED_ONLY_IN_DET_MODE_STR);
1271 /* Get a line of input. */
1272 if (!unformat_user (input, unformat_line_input, line_input))
1275 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1277 if (unformat (line_input, "%U", unformat_ip4_address, &in_addr))
1281 error = clib_error_return (0, "unknown input '%U'",
1282 format_unformat_error, line_input);
1287 dm = snat_det_map_by_user (sm, &in_addr);
1289 vlib_cli_output (vm, "no match");
1292 snat_det_forward (dm, &in_addr, &out_addr, &lo_port);
1293 vlib_cli_output (vm, "%U:<%d-%d>", format_ip4_address, &out_addr,
1294 lo_port, lo_port + dm->ports_per_host - 1);
1298 unformat_free (line_input);
1303 static clib_error_t *
1304 snat_det_reverse_command_fn (vlib_main_t * vm,
1305 unformat_input_t * input,
1306 vlib_cli_command_t * cmd)
1308 snat_main_t *sm = &snat_main;
1309 unformat_input_t _line_input, *line_input = &_line_input;
1310 ip4_address_t in_addr, out_addr;
1313 clib_error_t *error = 0;
1315 if (!sm->deterministic)
1316 return clib_error_return (0, SUPPORTED_ONLY_IN_DET_MODE_STR);
1318 /* Get a line of input. */
1319 if (!unformat_user (input, unformat_line_input, line_input))
1322 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1325 (line_input, "%U:%d", unformat_ip4_address, &out_addr, &out_port))
1329 error = clib_error_return (0, "unknown input '%U'",
1330 format_unformat_error, line_input);
1335 if (out_port < 1024 || out_port > 65535)
1337 error = clib_error_return (0, "wrong port, must be <1024-65535>");
1341 dm = snat_det_map_by_out (sm, &out_addr);
1343 vlib_cli_output (vm, "no match");
1346 snat_det_reverse (dm, &out_addr, (u16) out_port, &in_addr);
1347 vlib_cli_output (vm, "%U", format_ip4_address, &in_addr);
1351 unformat_free (line_input);
1356 static clib_error_t *
1357 set_timeout_command_fn (vlib_main_t * vm,
1358 unformat_input_t * input, vlib_cli_command_t * cmd)
1360 snat_main_t *sm = &snat_main;
1361 unformat_input_t _line_input, *line_input = &_line_input;
1362 clib_error_t *error = 0;
1364 /* Get a line of input. */
1365 if (!unformat_user (input, unformat_line_input, line_input))
1368 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1370 if (unformat (line_input, "udp %u", &sm->udp_timeout))
1372 if (nat64_set_udp_timeout (sm->udp_timeout))
1374 error = clib_error_return (0, "Invalid UDP timeout value");
1378 else if (unformat (line_input, "tcp-established %u",
1379 &sm->tcp_established_timeout))
1381 if (nat64_set_tcp_timeouts
1382 (sm->tcp_transitory_timeout, sm->tcp_established_timeout))
1385 clib_error_return (0,
1386 "Invalid TCP established timeouts value");
1390 else if (unformat (line_input, "tcp-transitory %u",
1391 &sm->tcp_transitory_timeout))
1393 if (nat64_set_tcp_timeouts
1394 (sm->tcp_transitory_timeout, sm->tcp_established_timeout))
1397 clib_error_return (0,
1398 "Invalid TCP transitory timeouts value");
1402 else if (unformat (line_input, "icmp %u", &sm->icmp_timeout))
1404 if (nat64_set_icmp_timeout (sm->icmp_timeout))
1406 error = clib_error_return (0, "Invalid ICMP timeout value");
1410 else if (unformat (line_input, "reset"))
1412 sm->udp_timeout = SNAT_UDP_TIMEOUT;
1413 sm->tcp_established_timeout = SNAT_TCP_ESTABLISHED_TIMEOUT;
1414 sm->tcp_transitory_timeout = SNAT_TCP_TRANSITORY_TIMEOUT;
1415 sm->icmp_timeout = SNAT_ICMP_TIMEOUT;
1416 nat64_set_udp_timeout (0);
1417 nat64_set_icmp_timeout (0);
1418 nat64_set_tcp_timeouts (0, 0);
1422 error = clib_error_return (0, "unknown input '%U'",
1423 format_unformat_error, line_input);
1429 unformat_free (line_input);
1434 static clib_error_t *
1435 nat_show_timeouts_command_fn (vlib_main_t * vm,
1436 unformat_input_t * input,
1437 vlib_cli_command_t * cmd)
1439 snat_main_t *sm = &snat_main;
1441 vlib_cli_output (vm, "udp timeout: %dsec", sm->udp_timeout);
1442 vlib_cli_output (vm, "tcp-established timeout: %dsec",
1443 sm->tcp_established_timeout);
1444 vlib_cli_output (vm, "tcp-transitory timeout: %dsec",
1445 sm->tcp_transitory_timeout);
1446 vlib_cli_output (vm, "icmp timeout: %dsec", sm->icmp_timeout);
1451 static clib_error_t *
1452 nat44_det_show_sessions_command_fn (vlib_main_t * vm,
1453 unformat_input_t * input,
1454 vlib_cli_command_t * cmd)
1456 snat_main_t *sm = &snat_main;
1458 snat_det_session_t *ses;
1461 if (!sm->deterministic)
1462 return clib_error_return (0, SUPPORTED_ONLY_IN_DET_MODE_STR);
1464 vlib_cli_output (vm, "NAT44 deterministic sessions:");
1466 pool_foreach (dm, sm->det_maps,
1468 vec_foreach_index (i, dm->sessions)
1470 ses = vec_elt_at_index (dm->sessions, i);
1472 vlib_cli_output (vm, " %U", format_det_map_ses, dm, ses, &i);
1479 static clib_error_t *
1480 snat_det_close_session_out_fn (vlib_main_t * vm,
1481 unformat_input_t * input,
1482 vlib_cli_command_t * cmd)
1484 snat_main_t *sm = &snat_main;
1485 unformat_input_t _line_input, *line_input = &_line_input;
1486 ip4_address_t out_addr, ext_addr, in_addr;
1487 u32 out_port, ext_port;
1489 snat_det_session_t *ses;
1490 snat_det_out_key_t key;
1491 clib_error_t *error = 0;
1493 if (!sm->deterministic)
1494 return clib_error_return (0, SUPPORTED_ONLY_IN_DET_MODE_STR);
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:%d %U:%d",
1503 unformat_ip4_address, &out_addr, &out_port,
1504 unformat_ip4_address, &ext_addr, &ext_port))
1508 error = clib_error_return (0, "unknown input '%U'",
1509 format_unformat_error, line_input);
1514 unformat_free (line_input);
1516 dm = snat_det_map_by_out (sm, &out_addr);
1518 vlib_cli_output (vm, "no match");
1521 snat_det_reverse (dm, &ext_addr, (u16) out_port, &in_addr);
1522 key.ext_host_addr = out_addr;
1523 key.ext_host_port = ntohs ((u16) ext_port);
1524 key.out_port = ntohs ((u16) out_port);
1525 ses = snat_det_get_ses_by_out (dm, &out_addr, key.as_u64);
1527 vlib_cli_output (vm, "no match");
1529 snat_det_ses_close (dm, ses);
1533 unformat_free (line_input);
1538 static clib_error_t *
1539 snat_det_close_session_in_fn (vlib_main_t * vm,
1540 unformat_input_t * input,
1541 vlib_cli_command_t * cmd)
1543 snat_main_t *sm = &snat_main;
1544 unformat_input_t _line_input, *line_input = &_line_input;
1545 ip4_address_t in_addr, ext_addr;
1546 u32 in_port, ext_port;
1548 snat_det_session_t *ses;
1549 snat_det_out_key_t key;
1550 clib_error_t *error = 0;
1552 if (!sm->deterministic)
1553 return clib_error_return (0, SUPPORTED_ONLY_IN_DET_MODE_STR);
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:%d %U:%d",
1562 unformat_ip4_address, &in_addr, &in_port,
1563 unformat_ip4_address, &ext_addr, &ext_port))
1567 error = clib_error_return (0, "unknown input '%U'",
1568 format_unformat_error, line_input);
1573 unformat_free (line_input);
1575 dm = snat_det_map_by_user (sm, &in_addr);
1577 vlib_cli_output (vm, "no match");
1580 key.ext_host_addr = ext_addr;
1581 key.ext_host_port = ntohs ((u16) ext_port);
1583 snat_det_find_ses_by_in (dm, &in_addr, ntohs ((u16) in_port), key);
1585 vlib_cli_output (vm, "no match");
1587 snat_det_ses_close (dm, ses);
1591 unformat_free (line_input);
1599 * @cliexstart{set snat workers}
1600 * Set NAT workers if 2 or more workers available, use:
1601 * vpp# set snat workers 0-2,5
1604 VLIB_CLI_COMMAND (set_workers_command, static) = {
1605 .path = "set nat workers",
1606 .function = set_workers_command_fn,
1607 .short_help = "set nat workers <workers-list>",
1612 * @cliexstart{show nat workers}
1614 * vpp# show nat workers:
1620 VLIB_CLI_COMMAND (nat_show_workers_command, static) = {
1621 .path = "show nat workers",
1622 .short_help = "show nat workers",
1623 .function = nat_show_workers_commnad_fn,
1628 * @cliexstart{set nat timeout}
1629 * Set values of timeouts for NAT sessions (in seconds), use:
1630 * vpp# set nat timeout udp 120 tcp-established 7500 tcp-transitory 250 icmp 90
1631 * To reset default values use:
1632 * vpp# set nat44 deterministic timeout reset
1635 VLIB_CLI_COMMAND (set_timeout_command, static) = {
1636 .path = "set nat timeout",
1637 .function = set_timeout_command_fn,
1639 "set nat timeout [udp <sec> | tcp-established <sec> "
1640 "tcp-transitory <sec> | icmp <sec> | reset]",
1645 * @cliexstart{show nat timeouts}
1646 * Show values of timeouts for NAT sessions.
1647 * vpp# show nat timeouts
1648 * udp timeout: 300sec
1649 * tcp-established timeout: 7440sec
1650 * tcp-transitory timeout: 240sec
1651 * icmp timeout: 60sec
1654 VLIB_CLI_COMMAND (nat_show_timeouts_command, static) = {
1655 .path = "show nat timeouts",
1656 .short_help = "show nat timeouts",
1657 .function = nat_show_timeouts_command_fn,
1662 * @cliexstart{snat ipfix logging}
1663 * To enable NAT IPFIX logging use:
1664 * vpp# nat ipfix logging
1665 * To set IPFIX exporter use:
1666 * vpp# set ipfix exporter collector 10.10.10.3 src 10.10.10.1
1669 VLIB_CLI_COMMAND (snat_ipfix_logging_enable_disable_command, static) = {
1670 .path = "nat ipfix logging",
1671 .function = snat_ipfix_logging_enable_disable_command_fn,
1672 .short_help = "nat ipfix logging [domain <domain-id>] [src-port <port>] [disable]",
1677 * @cliexstart{nat addr-port-assignment-alg}
1678 * Set address and port assignment algorithm
1679 * For the MAP-E CE limit port choice based on PSID use:
1680 * vpp# nat addr-port-assignment-alg map-e psid 10 psid-offset 6 psid-len 6
1681 * For port range use:
1682 * vpp# nat addr-port-assignment-alg port-range <start-port> - <end-port>
1683 * To set standard (default) address and port assignment algorithm use:
1684 * vpp# nat addr-port-assignment-alg default
1687 VLIB_CLI_COMMAND (nat44_set_alloc_addr_and_port_alg_command, static) = {
1688 .path = "nat addr-port-assignment-alg",
1689 .short_help = "nat addr-port-assignment-alg <alg-name> [<alg-params>]",
1690 .function = nat44_set_alloc_addr_and_port_alg_command_fn,
1695 * @cliexstart{show nat addr-port-assignment-alg}
1696 * Show address and port assignment algorithm
1699 VLIB_CLI_COMMAND (nat44_show_alloc_addr_and_port_alg_command, static) = {
1700 .path = "show nat addr-port-assignment-alg",
1701 .short_help = "show nat addr-port-assignment-alg",
1702 .function = nat44_show_alloc_addr_and_port_alg_command_fn,
1707 * @cliexstart{show nat44 hash tables}
1708 * Show NAT44 hash tables
1711 VLIB_CLI_COMMAND (nat44_show_hash, static) = {
1712 .path = "show nat44 hash tables",
1713 .short_help = "show nat44 hash tables [detail|verbose]",
1714 .function = nat44_show_hash_commnad_fn,
1719 * @cliexstart{nat44 add address}
1720 * Add/delete NAT44 pool address.
1721 * To add NAT44 pool address use:
1722 * vpp# nat44 add address 172.16.1.3
1723 * vpp# nat44 add address 172.16.2.2 - 172.16.2.24
1724 * To add NAT44 pool address for specific tenant (identified by VRF id) use:
1725 * vpp# nat44 add address 172.16.1.3 tenant-vrf 10
1728 VLIB_CLI_COMMAND (add_address_command, static) = {
1729 .path = "nat44 add address",
1730 .short_help = "nat44 add address <ip4-range-start> [- <ip4-range-end>] "
1731 "[tenant-vrf <vrf-id>] [twice-nat] [del]",
1732 .function = add_address_command_fn,
1737 * @cliexstart{show nat44 addresses}
1738 * Show NAT44 pool addresses.
1739 * vpp# show nat44 addresses
1740 * NAT44 pool addresses:
1742 * tenant VRF independent
1751 * NAT44 twice-nat pool addresses:
1753 * tenant VRF independent
1759 VLIB_CLI_COMMAND (nat44_show_addresses_command, static) = {
1760 .path = "show nat44 addresses",
1761 .short_help = "show nat44 addresses",
1762 .function = nat44_show_addresses_command_fn,
1767 * @cliexstart{set interface nat44}
1768 * Enable/disable NAT44 feature on the interface.
1769 * To enable NAT44 feature with local network interface use:
1770 * vpp# set interface nat44 in GigabitEthernet0/8/0
1771 * To enable NAT44 feature with external network interface use:
1772 * vpp# set interface nat44 out GigabitEthernet0/a/0
1775 VLIB_CLI_COMMAND (set_interface_snat_command, static) = {
1776 .path = "set interface nat44",
1777 .function = snat_feature_command_fn,
1778 .short_help = "set interface nat44 in <intfc> out <intfc> [output-feature] "
1784 * @cliexstart{show nat44 interfaces}
1785 * Show interfaces with NAT44 feature.
1786 * vpp# show nat44 interfaces
1788 * GigabitEthernet0/8/0 in
1789 * GigabitEthernet0/a/0 out
1792 VLIB_CLI_COMMAND (nat44_show_interfaces_command, static) = {
1793 .path = "show nat44 interfaces",
1794 .short_help = "show nat44 interfaces",
1795 .function = nat44_show_interfaces_command_fn,
1800 * @cliexstart{nat44 add static mapping}
1801 * Static mapping allows hosts on the external network to initiate connection
1802 * to to the local network host.
1803 * To create static mapping between local host address 10.0.0.3 port 6303 and
1804 * external address 4.4.4.4 port 3606 for TCP protocol use:
1805 * vpp# nat44 add static mapping tcp local 10.0.0.3 6303 external 4.4.4.4 3606
1806 * If not runnig "static mapping only" NAT plugin mode use before:
1807 * vpp# nat44 add address 4.4.4.4
1808 * To create static mapping between local and external address use:
1809 * vpp# nat44 add static mapping local 10.0.0.3 external 4.4.4.4
1812 VLIB_CLI_COMMAND (add_static_mapping_command, static) = {
1813 .path = "nat44 add static mapping",
1814 .function = add_static_mapping_command_fn,
1816 "nat44 add static mapping tcp|udp|icmp local <addr> [<port>] "
1817 "external <addr> [<port>] [vrf <table-id>] [twice-nat|self-twice-nat] "
1818 "[out2in-only] [del]",
1823 * @cliexstart{nat44 add identity mapping}
1824 * Identity mapping translate an IP address to itself.
1825 * To create identity mapping for address 10.0.0.3 port 6303 for TCP protocol
1827 * vpp# nat44 add identity mapping 10.0.0.3 tcp 6303
1828 * To create identity mapping for address 10.0.0.3 use:
1829 * vpp# nat44 add identity mapping 10.0.0.3
1830 * To create identity mapping for DHCP addressed interface use:
1831 * vpp# nat44 add identity mapping GigabitEthernet0/a/0 tcp 3606
1834 VLIB_CLI_COMMAND (add_identity_mapping_command, static) = {
1835 .path = "nat44 add identity mapping",
1836 .function = add_identity_mapping_command_fn,
1837 .short_help = "nat44 add identity mapping <interface>|<ip4-addr> "
1838 "[<protocol> <port>] [vrf <table-id>] [del]",
1843 * @cliexstart{nat44 add load-balancing static mapping}
1844 * Service load balancing using NAT44
1845 * To add static mapping with load balancing for service with external IP
1846 * address 1.2.3.4 and TCP port 80 and mapped to 2 local servers
1847 * 10.100.10.10:8080 and 10.100.10.20:8080 with probability 80% resp. 20% use:
1848 * 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
1851 VLIB_CLI_COMMAND (add_lb_static_mapping_command, static) = {
1852 .path = "nat44 add load-balancing static mapping",
1853 .function = add_lb_static_mapping_command_fn,
1855 "nat44 add load-balancing static mapping protocol tcp|udp "
1856 "external <addr>:<port> local <addr>:<port> [vrf <table-id>] "
1857 "probability <n> [twice-nat|self-twice-nat] [out2in-only] "
1858 "[affinity <timeout-seconds>] [del]",
1863 * @cliexstart{show nat44 static mappings}
1864 * Show NAT44 static mappings.
1865 * vpp# show nat44 static mappings
1866 * NAT44 static mappings:
1867 * local 10.0.0.3 external 4.4.4.4 vrf 0
1868 * tcp local 192.168.0.4:6303 external 4.4.4.3:3606 vrf 0
1869 * tcp vrf 0 external 1.2.3.4:80 out2in-only
1870 * local 10.100.10.10:8080 probability 80
1871 * local 10.100.10.20:8080 probability 20
1872 * tcp local 10.100.3.8:8080 external 169.10.10.1:80 vrf 0 twice-nat
1873 * tcp local 10.0.0.10:3603 external GigabitEthernet0/a/0:6306 vrf 10
1876 VLIB_CLI_COMMAND (nat44_show_static_mappings_command, static) = {
1877 .path = "show nat44 static mappings",
1878 .short_help = "show nat44 static mappings",
1879 .function = nat44_show_static_mappings_command_fn,
1884 * @cliexstart{nat44 add interface address}
1885 * Use NAT44 pool address from specific interfce
1886 * To add NAT44 pool address from specific interface use:
1887 * vpp# nat44 add interface address GigabitEthernet0/8/0
1890 VLIB_CLI_COMMAND (snat_add_interface_address_command, static) = {
1891 .path = "nat44 add interface address",
1892 .short_help = "nat44 add interface address <interface> [twice-nat] [del]",
1893 .function = snat_add_interface_address_command_fn,
1898 * @cliexstart{show nat44 interface address}
1899 * Show NAT44 pool address interfaces
1900 * vpp# show nat44 interface address
1901 * NAT44 pool address interfaces:
1902 * GigabitEthernet0/a/0
1903 * NAT44 twice-nat pool address interfaces:
1904 * GigabitEthernet0/8/0
1907 VLIB_CLI_COMMAND (nat44_show_interface_address_command, static) = {
1908 .path = "show nat44 interface address",
1909 .short_help = "show nat44 interface address",
1910 .function = nat44_show_interface_address_command_fn,
1915 * @cliexstart{show nat44 sessions}
1916 * Show NAT44 sessions.
1919 VLIB_CLI_COMMAND (nat44_show_sessions_command, static) = {
1920 .path = "show nat44 sessions",
1921 .short_help = "show nat44 sessions [detail]",
1922 .function = nat44_show_sessions_command_fn,
1927 * @cliexstart{nat44 del session}
1928 * To administratively delete NAT44 session by inside address and port use:
1929 * vpp# nat44 del session in 10.0.0.3:6303 tcp
1930 * To administratively delete NAT44 session by outside address and port use:
1931 * vpp# nat44 del session out 1.0.0.3:6033 udp
1934 VLIB_CLI_COMMAND (nat44_del_session_command, static) = {
1935 .path = "nat44 del session",
1936 .short_help = "nat44 del session in|out <addr>:<port> tcp|udp|icmp [vrf <id>] [external-host <addr>:<port>]",
1937 .function = nat44_del_session_command_fn,
1942 * @cliexstart{nat44 forwarding}
1943 * Enable or disable forwarding
1944 * Forward packets which don't match existing translation
1945 * or static mapping instead of dropping them.
1946 * To enable forwarding, use:
1947 * vpp# nat44 forwarding enable
1948 * To disable forwarding, use:
1949 * vpp# nat44 forwarding disable
1952 VLIB_CLI_COMMAND (snat_forwarding_set_command, static) = {
1953 .path = "nat44 forwarding",
1954 .short_help = "nat44 forwarding enable|disable",
1955 .function = snat_forwarding_set_command_fn,
1960 * @cliexstart{nat44 deterministic add}
1961 * Create bijective mapping of inside address to outside address and port range
1962 * pairs, with the purpose of enabling deterministic NAT to reduce logging in
1964 * To create deterministic mapping between inside network 10.0.0.0/18 and
1965 * outside network 1.1.1.0/30 use:
1966 * # vpp# nat44 deterministic add in 10.0.0.0/18 out 1.1.1.0/30
1969 VLIB_CLI_COMMAND (snat_det_map_command, static) = {
1970 .path = "nat44 deterministic add",
1971 .short_help = "nat44 deterministic add in <addr>/<plen> out <addr>/<plen> [del]",
1972 .function = snat_det_map_command_fn,
1977 * @cliexpstart{show nat44 deterministic mappings}
1978 * Show NAT44 deterministic mappings
1979 * vpp# show nat44 deterministic mappings
1980 * NAT44 deterministic mappings:
1981 * in 10.0.0.0/24 out 1.1.1.1/32
1982 * outside address sharing ratio: 256
1983 * number of ports per inside host: 252
1984 * sessions number: 0
1987 VLIB_CLI_COMMAND (nat44_det_show_mappings_command, static) = {
1988 .path = "show nat44 deterministic mappings",
1989 .short_help = "show nat44 deterministic mappings",
1990 .function = nat44_det_show_mappings_command_fn,
1995 * @cliexstart{nat44 deterministic forward}
1996 * Return outside address and port range from inside address for deterministic
1998 * To obtain outside address and port of inside host use:
1999 * vpp# nat44 deterministic forward 10.0.0.2
2000 * 1.1.1.0:<1054-1068>
2003 VLIB_CLI_COMMAND (snat_det_forward_command, static) = {
2004 .path = "nat44 deterministic forward",
2005 .short_help = "nat44 deterministic forward <addr>",
2006 .function = snat_det_forward_command_fn,
2011 * @cliexstart{nat44 deterministic reverse}
2012 * Return inside address from outside address and port for deterministic NAT.
2013 * To obtain inside host address from outside address and port use:
2014 * #vpp nat44 deterministic reverse 1.1.1.1:1276
2018 VLIB_CLI_COMMAND (snat_det_reverse_command, static) = {
2019 .path = "nat44 deterministic reverse",
2020 .short_help = "nat44 deterministic reverse <addr>:<port>",
2021 .function = snat_det_reverse_command_fn,
2026 * @cliexstart{show nat44 deterministic sessions}
2027 * Show NAT44 deterministic sessions.
2028 * vpp# show nat44 deterministic sessions
2029 * NAT44 deterministic sessions:
2030 * in 10.0.0.3:3005 out 1.1.1.2:1146 external host 172.16.1.2:3006 state: udp-active expire: 306
2031 * in 10.0.0.3:3000 out 1.1.1.2:1141 external host 172.16.1.2:3001 state: udp-active expire: 306
2032 * in 10.0.0.4:3005 out 1.1.1.2:1177 external host 172.16.1.2:3006 state: udp-active expire: 306
2035 VLIB_CLI_COMMAND (nat44_det_show_sessions_command, static) = {
2036 .path = "show nat44 deterministic sessions",
2037 .short_help = "show nat44 deterministic sessions",
2038 .function = nat44_det_show_sessions_command_fn,
2043 * @cliexstart{nat44 deterministic close session out}
2044 * Close session using outside ip address and port
2045 * and external ip address and port, use:
2046 * vpp# nat44 deterministic close session out 1.1.1.1:1276 2.2.2.2:2387
2049 VLIB_CLI_COMMAND (snat_det_close_sesion_out_command, static) = {
2050 .path = "nat44 deterministic close session out",
2051 .short_help = "nat44 deterministic close session out "
2052 "<out_addr>:<out_port> <ext_addr>:<ext_port>",
2053 .function = snat_det_close_session_out_fn,
2058 * @cliexstart{nat44 deterministic close session in}
2059 * Close session using inside ip address and port
2060 * and external ip address and port, use:
2061 * vpp# nat44 deterministic close session in 3.3.3.3:3487 2.2.2.2:2387
2064 VLIB_CLI_COMMAND (snat_det_close_session_in_command, static) = {
2065 .path = "nat44 deterministic close session in",
2066 .short_help = "nat44 deterministic close session in "
2067 "<in_addr>:<in_port> <ext_addr>:<ext_port>",
2068 .function = snat_det_close_session_in_fn,
2074 * fd.io coding-style-patch-verification: ON
2077 * eval: (c-set-style "gnu")