aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/nat/nat64_cli.c
blob: f3645bbbe0bee3bb275603d0d28a500bbd75ae94 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
@media only all and (prefers-color-scheme: dark) {
.highlight .hll { background-color: #49483e }
.highlight .c { color: #75715e } /* Comment */
.highlight .err { color: #960050; background-color: #1e0010 } /* Error */
.highlight .k { color: #66d9ef } /* Keyword */
.highlight .l { color: #ae81ff } /* Literal */
.highlight .n { color: #f8f8f2 } /* Name */
.highlight .o { color: #f92672 } /* Operator */
.highlight .p { color: #f8f8f2 } /* Punctuation */
.highlight .ch { color: #75715e } /* Comment.Hashbang */
.highlight .cm { color: #75715e } /* Comment.Multiline */
.highlight .cp { color: #75715e } /* Comment.Preproc */
.highlight .cpf { color: #75715e } /* Comment.PreprocFile */
.highlight .c1 { color: #75715e } /* Comment.Single */
.highlight .cs { color: #75715e } /* Comment.Special */
.highlight .gd { color: #f92672 } /* Generic.Deleted */
.highlight .ge { font-style: italic } /* Generic.Emph */
.highlight .gi { color: #a6e22e } /* Generic.Inserted */
.highlight .gs { font-weight: bold } /* Generic.Strong */
.highlight .gu { color: #75715e } /* Generic.Subheading */
.highlight .kc { color: #66d9ef } /* Keyword.Constant */
.highlight .kd { color: #66d9ef } /* Keyword.Declaration */
.highlight .kn { color: #f92672 } /* Keyword.Namespace */
.highlight .kp { color: #66d9ef } /* Keyword.Pseudo */
.highlight .kr { color: #66d9ef } /* Keyword.Reserved */
.highlight .kt { color: #66d9ef } /* Keyword.Type */
.highlight .ld { color: #e6db74 } /* Literal.Date */
.highlight .m { color: #ae81ff } /* Literal.Number */
.highlight .s { color: #e6db74 } /* Literal.String */
.highlight .na { color: #a6e22e } /* Name.Attribute */
.highlight .nb { color: #f8f8f2 } /* Name.Builtin */
.highlight .nc { color: #a6e22e } /* Name.Class */
.highlight .no { color: #66d9ef } /* Name.Constant */
.highlight .nd { color: #a6e22e } /* Name.Decorator */
.highlight .ni { color: #f8f8f2 } /* Name.Entity */
.highlight .ne { color: #a6e22e } /* Name.Exception */
.highlight .nf { color: #a6e22e } /* Name.Function */
.highlight .nl { color: #f8f8f2 } /* Name.Label */
.highlight .nn { color: #f8f8f2 } /* Name.Namespace */
.highlight .nx { color: #a6e22e } /* Name.Other */
.highlight .py { color: #f8f8f2 } /* Name.Property */
.highlight .nt { color: #f92672 } /* Name.Tag */
.highlight .nv { color: #f8f8f2 } /* Name.Variable */
.highlight .ow { color: #f92672 } /* Operator.Word */
.highlight .w { color: #f8f8f2 } /* Text.Whitespace */
.highlight .mb { color: #ae81ff } /* Literal.Number.Bin */
.highlight .mf { color: #ae81ff } /* Literal.Number.Float */
.highlight .mh { color: #ae81ff } /* Literal.Number.Hex */
.highlight .mi { color: #ae81ff } /* Literal.Number.Integer */
.highlight .mo { color: #ae81ff } /* Literal.Number.Oct */
.highlight .sa { color: #e6db74 } /* Literal.String.Affix */
.highlight .sb { color: #e6db74 } /* Literal.String.Backtick */
.highlight .sc { color: #e6db74 } /* Literal.String.Char */
.highlight .dl { color: #e6db74 } /* Literal.String.Delimiter */
.highlight .sd { color: #e6db74 } /* Literal.String.Doc */
.highlight .s2 { color: #e6db74 } /* Literal.String.Double */
.highlight .se { color: #ae81ff } /* Literal.String.Escape */
.highlight .sh { color: #e6db74 } /* Literal.String.Heredoc */
.highlight .si { color: #e6db74 } /* Literal.String.Interpol */
.highlight .sx { color: #e6db74 } /* Literal.String.Other */
.highlight .sr { color: #e6db74 } /* Literal.String.Regex */
.highlight .s1 { color: #e6db74 } /* Literal.String.Single */
.highlight .ss { color: #e6db74 } /* Literal.String.Symbol */
.highlight .bp { color: #f8f8f2 } /* Name.Builtin.Pseudo */
.highlight .fm { color: #a6e22e } /* Name.Function.Magic */
.highlight .vc { color: #f8f8f2 } /* Name.Variable.Class */
.highlight .vg { color: #f8f8f2 } /* Name.Variable.Global */
.highlight .vi { color: #f8f8f2 } /* Name.Variable.Instance */
.highlight .vm { color: #f8f8f2 } /* Name.Variable.Magic */
.highlight .il { color: #ae81ff } /* Literal.Number.Integer.Long */
}
@media (prefers-color-scheme: light) {
.highlight .hll { background-color: #ffffcc }
.highlight .c { color: #888888 } /* Comment */
.highlight .err { color: #a61717; background-color: #e3d2d2 } /* Error */
.highlight .k { color: #008800; font-weight: bold } /* Keyword */
.highlight .ch { color: #888888 } /* Comment.Hashbang */
.highlight .cm { color: #888888 } /* Comment.Multiline */
.highlight .cp { color: #cc0000; font-weight: bold } /* Comment.Preproc */
.highlight .cpf { color: #888888 } /* Comment.PreprocFile */
.highlight .c1 { color: #888888 } /* Comment.Single */
.highlight .cs { color: #cc0000; font-weight: bold; background-color: #fff0f0 } /* Comment.Special */
.highlight .gd { color: #000000; background-color: #ffdddd } /* Generic.Deleted */
.highlight .ge { font-style: italic } /* Generic.Emph */
.highlight .gr { color: #aa0000 } /* Generic.Error */
.highlight .gh { color: #333333 } /* Generic.Heading */
.highlight .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */
.highlight .go { color: #888888 } /* Generic.Output */
.highlight .gp { color: #555555 } /* Generic.Prompt */
.highlight .gs { font-weight: bold } /* Generic.Strong */
.highlight .gu { color: #666666 } /* Generic.Subheading */
.highlight .gt { color: #aa0000 } /* Generic.Traceback */
.highlight .kc { color: #008800; font-weight: bold } /* Keyword.Constant */
.highlight .kd { color: #008800; font-weight: bold } /* Keyword.Declaration */
.highlight .kn { color: #008800; font-weight: bold } /* Keyword.Namespace */
.highlight .kp { color: #008800 } /* Keyword.Pseudo */
.highlight .kr { color: #008800; font-weight: bold } /* Keyword.Reserved */
.highlight .kt { color: #888888; font-weight: bold } /* Keyword.Type */
.highlight .m { color: #0000DD; font-weight: bold } /* Literal.Number */
.highlight .s { color: #dd2200; background-color: #fff0f0 } /* Literal.String */
.highlight .na { color: #336699 } /* Name.Attribute */
.highlight .nb { color: #003388 } /* Name.Builtin */
.highlight .nc { color: #bb0066; font-weight: bold } /* Name.Class */
.highlight .no { color: #003366; font-weight: bold } /* Name.Constant */
.highlight .nd { color: #555555 } /* Name.Decorator */
.highlight .ne { color: #bb0066; font-weight: bold } /* Name.Exception */
.highlight .nf { color: #0066bb; font-weight: bold } /* Name.Function */
.highlight .nl { color: #336699; font-style: italic } /* Name.Label */
.highlight .nn { color: #bb0066; font-weight: bold } /* Name.Namespace */
.highlight .py { color: #336699; font-weight: bold } /* Name.Property */
.highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */
.highlight .nv { color: #336699 } /* Name.Variable */
.highlight .ow { color: #008800 } /* Operator.Word */
.highlight .w { color: #bbbbbb } /* Text.Whitespace */
.highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */
.highlight .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */
.highlight .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */
.highlight .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */
.highlight .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */
.highlight .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */
.highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */
.highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */
.highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */
.highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */
.highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */
.highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */
.highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */
.highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */
.highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */
.highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */
.highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */
.highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */
.highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */
.highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */
.highlight .vc { color: #336699 } /* Name.Variable.Class */
.highlight .vg { color: #dd7700 } /* Name.Variable.Global */
.highlight .vi { color: #3333bb } /* Name.Variable.Instance */
.highlight .vm { color: #336699 } /* Name.Variable.Magic */
.highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */
}
# Copyright (c) 2016 Comcast Cable Communications Management, LLC.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at:
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import re

from . import generate

# Register our regexp
generate.siphon_patterns.append((
    re.compile("(?P<m>VLIB_CONFIG_FUNCTION)\s*"
        '[(](?P<fn>[a-zA-Z0-9_]+)\s*,\s*"(?P<name>[^"]*)"[)]'),
    "syscfg"
))
65 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984
/*
 * Copyright (c) 2017 Cisco and/or its affiliates.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
/**
 * @file
 * @brief NAT64 CLI
 */

#include <nat/nat64.h>
#include <nat/nat.h>
#include <vnet/fib/fib_table.h>

static clib_error_t *
nat64_add_del_pool_addr_command_fn (vlib_main_t * vm,
				    unformat_input_t * input,
				    vlib_cli_command_t * cmd)
{
  nat64_main_t *nm = &nat64_main;
  unformat_input_t _line_input, *line_input = &_line_input;
  ip4_address_t start_addr, end_addr, this_addr;
  u32 start_host_order, end_host_order;
  int i, count, rv;
  u32 vrf_id = ~0;
  u8 is_add = 1;
  clib_error_t *error = 0;

  if (nm->is_disabled)
    return clib_error_return (0,
			      "NAT64 disabled, multi thread not supported");

  /* Get a line of input. */
  if (!unformat_user (input, unformat_line_input, line_input))
    return 0;

  while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
    {
      if (unformat (line_input, "%U - %U",
		    unformat_ip4_address, &start_addr,
		    unformat_ip4_address, &end_addr))
	;
      else if (unformat (line_input, "tenant-vrf %u", &vrf_id))
	;
      else if (unformat (line_input, "%U", unformat_ip4_address, &start_addr))
	end_addr = start_addr;
      else if (unformat (line_input, "del"))
	is_add = 0;
      else
	{
	  error = clib_error_return (0, "unknown input '%U'",
				     format_unformat_error, line_input);
	  goto done;
	}
    }

  start_host_order = clib_host_to_net_u32 (start_addr.as_u32);
  end_host_order = clib_host_to_net_u32 (end_addr.as_u32);

  if (end_host_order < start_host_order)
    {
      error = clib_error_return (0, "end address less than start address");
      goto done;
    }

  count = (end_host_order - start_host_order) + 1;
  this_addr = start_addr;

  for (i = 0; i < count; i++)
    {
      rv = nat64_add_del_pool_addr (&this_addr, vrf_id, is_add);

      switch (rv)
	{
	case VNET_API_ERROR_NO_SUCH_ENTRY:
	  error =
	    clib_error_return (0, "NAT64 pool address %U not exist.",
			       format_ip4_address, &this_addr);
	  goto done;
	case VNET_API_ERROR_VALUE_EXIST:
	  error =
	    clib_error_return (0, "NAT64 pool address %U exist.",
			       format_ip4_address, &this_addr);
	  goto done;
	default:
	  break;

	}
      increment_v4_address (&this_addr);
    }

done:
  unformat_free (line_input);

  return error;
}

static int
nat64_cli_pool_walk (snat_address_t * ap, void *ctx)
{
  vlib_main_t *vm = ctx;

  if (ap->fib_index != ~0)
    {
      fib_table_t *fib;
      fib = fib_table_get (ap->fib_index, FIB_PROTOCOL_IP6);
      if (!fib)
	return -1;
      vlib_cli_output (vm, " %U tenant VRF: %u", format_ip4_address,
		       &ap->addr, fib->ft_table_id);
    }
  else
    vlib_cli_output (vm, " %U", format_ip4_address, &ap->addr);

  return 0;
}

static clib_error_t *
nat64_show_pool_command_fn (vlib_main_t * vm,
			    unformat_input_t * input,
			    vlib_cli_command_t * cmd)
{
  nat64_main_t *nm = &nat64_main;

  if (nm->is_disabled)
    return clib_error_return (0,
			      "NAT64 disabled, multi thread not supported");

  vlib_cli_output (vm, "NAT64 pool:");
  nat64_pool_addr_walk (nat64_cli_pool_walk, vm);

  return 0;
}

static clib_error_t *
nat64_interface_feature_command_fn (vlib_main_t * vm,
				    unformat_input_t *
				    input, vlib_cli_command_t * cmd)
{
  nat64_main_t *nm = &nat64_main;
  unformat_input_t _line_input, *line_input = &_line_input;
  vnet_main_t *vnm = vnet_get_main ();
  clib_error_t *error = 0;
  u32 sw_if_index;
  u32 *inside_sw_if_indices = 0;
  u32 *outside_sw_if_indices = 0;
  u8 is_add = 1;
  int i, rv;

  if (nm->is_disabled)
    return clib_error_return (0,
			      "NAT64 disabled, multi thread not supported");

  /* Get a line of input. */
  if (!unformat_user (input, unformat_line_input, line_input))
    return 0;

  while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
    {
      if (unformat (line_input, "in %U", unformat_vnet_sw_interface,
		    vnm, &sw_if_index))
	vec_add1 (inside_sw_if_indices, sw_if_index);
      else if (unformat (line_input, "out %U", unformat_vnet_sw_interface,
			 vnm, &sw_if_index))
	vec_add1 (outside_sw_if_indices, sw_if_index);
      else if (unformat (line_input, "del"))
	is_add = 0;
      else
	{
	  error = clib_error_return (0, "unknown input '%U'",
				     format_unformat_error, line_input);
	  goto done;
	}
    }

  if (vec_len (inside_sw_if_indices))
    {
      for (i = 0; i < vec_len (inside_sw_if_indices); i++)
	{
	  sw_if_index = inside_sw_if_indices[i];
	  rv = nat64_add_del_interface (sw_if_index, 1, is_add);
	  switch (rv)
	    {
	    case VNET_API_ERROR_NO_SUCH_ENTRY:
	      error =
		clib_error_return (0, "%U NAT64 feature not enabled.",
				   format_vnet_sw_interface_name, vnm,
				   vnet_get_sw_interface (vnm, sw_if_index));
	      goto done;
	    case VNET_API_ERROR_VALUE_EXIST:
	      error =
		clib_error_return (0, "%U NAT64 feature already enabled.",
				   format_vnet_sw_interface_name, vnm,
				   vnet_get_sw_interface (vnm, sw_if_index));
	      goto done;
	    case VNET_API_ERROR_INVALID_VALUE:
	    case VNET_API_ERROR_INVALID_VALUE_2:
	      error =
		clib_error_return (0,
				   "%U NAT64 feature enable/disable failed.",
				   format_vnet_sw_interface_name, vnm,
				   vnet_get_sw_interface (vnm, sw_if_index));
	      goto done;
	    default:
	      break;

	    }
	}
    }

  if (vec_len (outside_sw_if_indices))
    {
      for (i = 0; i < vec_len (outside_sw_if_indices); i++)
	{
	  sw_if_index = outside_sw_if_indices[i];
	  rv = nat64_add_del_interface (sw_if_index, 0, is_add);
	  switch (rv)
	    {
	    case VNET_API_ERROR_NO_SUCH_ENTRY:
	      error =
		clib_error_return (0, "%U NAT64 feature not enabled.",
				   format_vnet_sw_interface_name, vnm,
				   vnet_get_sw_interface (vnm, sw_if_index));
	      goto done;
	    case VNET_API_ERROR_VALUE_EXIST:
	      error =
		clib_error_return (0, "%U NAT64 feature already enabled.",
				   format_vnet_sw_interface_name, vnm,
				   vnet_get_sw_interface (vnm, sw_if_index));
	      goto done;
	    case VNET_API_ERROR_INVALID_VALUE:
	    case VNET_API_ERROR_INVALID_VALUE_2:
	      error =
		clib_error_return (0,
				   "%U NAT64 feature enable/disable failed.",
				   format_vnet_sw_interface_name, vnm,
				   vnet_get_sw_interface (vnm, sw_if_index));
	      goto done;
	    default:
	      break;

	    }
	}
    }

done:
  unformat_free (line_input);
  vec_free (inside_sw_if_indices);
  vec_free (outside_sw_if_indices);

  return error;
}

static int
nat64_cli_interface_walk (snat_interface_t * i, void *ctx)
{
  vlib_main_t *vm = ctx;
  vnet_main_t *vnm = vnet_get_main ();

  vlib_cli_output (vm, " %U %s", format_vnet_sw_interface_name, vnm,
		   vnet_get_sw_interface (vnm, i->sw_if_index),
		   i->is_inside ? "in" : "out");
  return 0;
}

static clib_error_t *
nat64_show_interfaces_command_fn (vlib_main_t * vm,
				  unformat_input_t *
				  input, vlib_cli_command_t * cmd)
{
  nat64_main_t *nm = &nat64_main;

  if (nm->is_disabled)
    return clib_error_return (0,
			      "NAT64 disabled, multi thread not supported");

  vlib_cli_output (vm, "NAT64 interfaces:");
  nat64_interfaces_walk (nat64_cli_interface_walk, vm);

  return 0;
}

static clib_error_t *
nat64_add_del_static_bib_command_fn (vlib_main_t *
				     vm,
				     unformat_input_t
				     * input, vlib_cli_command_t * cmd)
{
  nat64_main_t *nm = &nat64_main;
  unformat_input_t _line_input, *line_input = &_line_input;
  clib_error_t *error = 0;
  u8 is_add = 1;
  ip6_address_t in_addr;
  ip4_address_t out_addr;
  u32 in_port = 0;
  u32 out_port = 0;
  u32 vrf_id = 0, protocol;
  snat_protocol_t proto = 0;
  u8 p = 0;
  int rv;

  if (nm->is_disabled)
    return clib_error_return (0,
			      "NAT64 disabled, multi thread not supported");

  if (!unformat_user (input, unformat_line_input, line_input))
    return 0;

  while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
    {
      if (unformat (line_input, "%U %u", unformat_ip6_address,
		    &in_addr, &in_port))
	;
      else if (unformat (line_input, "%U %u", unformat_ip4_address,
			 &out_addr, &out_port))
	;
      else if (unformat (line_input, "vrf %u", &vrf_id))
	;
      else if (unformat (line_input, "%U", unformat_snat_protocol, &proto))
	;
      else
	if (unformat
	    (line_input, "%U %U %u", unformat_ip6_address, &in_addr,
	     unformat_ip4_address, &out_addr, &protocol))
	p = (u8) protocol;
      else if (unformat (line_input, "del"))
	is_add = 0;
      else
	{
	  error = clib_error_return (0, "unknown input: '%U'",
				     format_unformat_error, line_input);
	  goto done;
	}
    }

  if (!p)
    {
      if (!in_port)
	{
	  error =
	    clib_error_return (0, "inside port and address  must be set");
	  goto done;
	}

      if (!out_port)
	{
	  error =
	    clib_error_return (0, "outside port and address  must be set");
	  goto done;
	}

      p = snat_proto_to_ip_proto (proto);
    }

  rv =
    nat64_add_del_static_bib_entry (&in_addr, &out_addr, (u16) in_port,
				    (u16) out_port, p, vrf_id, is_add);

  switch (rv)
    {
    case VNET_API_ERROR_NO_SUCH_ENTRY:
      error = clib_error_return (0, "NAT64 BIB entry not exist.");
      goto done;
    case VNET_API_ERROR_VALUE_EXIST:
      error = clib_error_return (0, "NAT64 BIB entry exist.");
      goto done;
    case VNET_API_ERROR_UNSPECIFIED:
      error = clib_error_return (0, "Crerate NAT64 BIB entry failed.");
      goto done;
    case VNET_API_ERROR_INVALID_VALUE:
      error =
	clib_error_return (0, "Outside addres %U and port %u already in use.",
			   format_ip4_address, &out_addr, out_port);
      goto done;
    default:
      break;
    }

done:
  unformat_free (line_input);

  return error;
}

static int
nat64_cli_bib_walk (nat64_db_bib_entry_t * bibe, void *ctx)
{
  vlib_main_t *vm = ctx;
  fib_table_t *fib;

  fib = fib_table_get (bibe->fib_index, FIB_PROTOCOL_IP6);
  if (!fib)
    return -1;

  switch (bibe->proto)
    {
    case IP_PROTOCOL_ICMP:
    case IP_PROTOCOL_TCP:
    case IP_PROTOCOL_UDP:
      vlib_cli_output (vm, " %U %u %U %u protocol %U vrf %u %s %u sessions",
		       format_ip6_address, &bibe->in_addr,
		       clib_net_to_host_u16 (bibe->in_port),
		       format_ip4_address, &bibe->out_addr,
		       clib_net_to_host_u16 (bibe->out_port),
		       format_snat_protocol,
		       ip_proto_to_snat_proto (bibe->proto), fib->ft_table_id,
		       bibe->is_static ? "static" : "dynamic", bibe->ses_num);
      break;
    default:
      vlib_cli_output (vm, " %U %U protocol %u vrf %u %s %u sessions",
		       format_ip6_address, &bibe->in_addr,
		       format_ip4_address, &bibe->out_addr,
		       bibe->proto, fib->ft_table_id,
		       bibe->is_static ? "static" : "dynamic", bibe->ses_num);
    }
  return 0;
}

static clib_error_t *
nat64_show_bib_command_fn (vlib_main_t * vm,
			   unformat_input_t * input, vlib_cli_command_t * cmd)
{
  nat64_main_t *nm = &nat64_main;
  unformat_input_t _line_input, *line_input = &_line_input;
  clib_error_t *error = 0;
  u32 proto = ~0;
  u8 p = 255;

  if (nm->is_disabled)
    return clib_error_return (0,
			      "NAT64 disabled, multi thread not supported");

  if (!unformat_user (input, unformat_line_input, line_input))
    return 0;

  if (unformat (line_input, "%U", unformat_snat_protocol, &proto))
    p = snat_proto_to_ip_proto (proto);
  else if (unformat (line_input, "unknown"))
    p = 0;
  else if (unformat (line_input, "all"))
    ;
  else
    {
      error = clib_error_return (0, "unknown input: '%U'",
				 format_unformat_error, line_input);
      goto done;
    }

  if (p == 255)
    vlib_cli_output (vm, "NAT64 BIB entries:");
  else
    vlib_cli_output (vm, "NAT64 %U BIB entries:", format_snat_protocol,
		     proto);
  nat64_db_bib_walk (&nm->db, p, nat64_cli_bib_walk, vm);

done:
  unformat_free (line_input);

  return error;
}

static clib_error_t *
nat64_set_timeouts_command_fn (vlib_main_t * vm, unformat_input_t * input,
			       vlib_cli_command_t * cmd)
{
  nat64_main_t *nm = &nat64_main;
  unformat_input_t _line_input, *line_input = &_line_input;
  clib_error_t *error = 0;
  u32 timeout, tcp_trans, tcp_est, tcp_incoming_syn;

  tcp_trans = nat64_get_tcp_trans_timeout ();
  tcp_est = nat64_get_tcp_est_timeout ();
  tcp_incoming_syn = nat64_get_tcp_incoming_syn_timeout ();

  if (nm->is_disabled)
    return clib_error_return (0,
			      "NAT64 disabled, multi thread not supported");

  if (!unformat_user (input, unformat_line_input, line_input))
    return 0;

  while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
    {
      if (unformat (line_input, "udp %u", &timeout))
	{
	  if (nat64_set_udp_timeout (timeout))
	    {
	      error = clib_error_return (0, "Invalid UDP timeout value");
	      goto done;
	    }
	}
      else if (unformat (line_input, "icmp %u", &timeout))
	{
	  if (nat64_set_icmp_timeout (timeout))
	    {
	      error = clib_error_return (0, "Invalid ICMP timeout value");
	      goto done;
	    }
	}
      else if (unformat (line_input, "tcp-trans %u", &tcp_trans))
	{
	  if (nat64_set_tcp_timeouts (tcp_trans, tcp_est, tcp_incoming_syn))
	    {
	      error =
		clib_error_return (0,
				   "Invalid TCP transitory timeouts value");
	      goto done;
	    }
	}
      else if (unformat (line_input, "tcp-est %u", &tcp_est))
	{
	  if (nat64_set_tcp_timeouts (tcp_trans, tcp_est, tcp_incoming_syn))
	    {
	      error =
		clib_error_return (0,
				   "Invalid TCP established timeouts value");
	      goto done;
	    }
	}
      else
	if (unformat (line_input, "tcp-incoming-syn %u", &tcp_incoming_syn))
	{
	  if (nat64_set_tcp_timeouts (tcp_trans, tcp_est, tcp_incoming_syn))
	    {
	      error =
		clib_error_return (0,
				   "Invalid TCP incoming SYN timeouts value");
	      goto done;
	    }
	}
      else if (unformat (line_input, "reset"))
	{
	  nat64_set_udp_timeout (0);
	  nat64_set_icmp_timeout (0);
	  nat64_set_tcp_timeouts (0, 0, 0);
	}
      else
	{
	  error = clib_error_return (0, "unknown input '%U'",
				     format_unformat_error, line_input);
	  goto done;
	}
    }

done:
  unformat_free (line_input);

  return error;
}

static clib_error_t *
nat64_show_timeouts_command_fn (vlib_main_t * vm, unformat_input_t * input,
				vlib_cli_command_t * cmd)
{
  nat64_main_t *nm = &nat64_main;

  if (nm->is_disabled)
    return clib_error_return (0,
			      "NAT64 disabled, multi thread not supported");

  vlib_cli_output (vm, "NAT64 session timeouts:");
  vlib_cli_output (vm, " UDP %usec", nat64_get_udp_timeout ());
  vlib_cli_output (vm, " ICMP %usec", nat64_get_icmp_timeout ());
  vlib_cli_output (vm, " TCP transitory %usec",
		   nat64_get_tcp_trans_timeout ());
  vlib_cli_output (vm, " TCP established %usec",
		   nat64_get_tcp_est_timeout ());
  vlib_cli_output (vm, " TCP incoming SYN %usec",
		   nat64_get_tcp_incoming_syn_timeout ());

  return 0;
}

static int
nat64_cli_st_walk (nat64_db_st_entry_t * ste, void *ctx)
{
  vlib_main_t *vm = ctx;
  nat64_main_t *nm = &nat64_main;
  nat64_db_bib_entry_t *bibe;
  fib_table_t *fib;

  bibe = nat64_db_bib_entry_by_index (&nm->db, ste->proto, ste->bibe_index);
  if (!bibe)
    return -1;

  fib = fib_table_get (bibe->fib_index, FIB_PROTOCOL_IP6);
  if (!fib)
    return -1;

  u32 vrf_id = fib->ft_table_id;

  if (ste->proto == IP_PROTOCOL_ICMP)
    vlib_cli_output (vm, " %U %U %u %U %U %u protocol %U vrf %u",
		     format_ip6_address, &bibe->in_addr,
		     format_ip6_address, &ste->in_r_addr,
		     clib_net_to_host_u16 (bibe->in_port),
		     format_ip4_address, &bibe->out_addr,
		     format_ip4_address, &ste->out_r_addr,
		     clib_net_to_host_u16 (bibe->out_port),
		     format_snat_protocol,
		     ip_proto_to_snat_proto (bibe->proto), vrf_id);
  else if (ste->proto == IP_PROTOCOL_TCP || ste->proto == IP_PROTOCOL_UDP)
    vlib_cli_output (vm, " %U %u %U %u %U %u %U %u protcol %U vrf %u",
		     format_ip6_address, &bibe->in_addr,
		     clib_net_to_host_u16 (bibe->in_port),
		     format_ip6_address, &ste->in_r_addr,
		     clib_net_to_host_u16 (ste->r_port),
		     format_ip4_address, &bibe->out_addr,
		     clib_net_to_host_u16 (bibe->out_port),
		     format_ip4_address, &ste->out_r_addr,
		     clib_net_to_host_u16 (ste->r_port),
		     format_snat_protocol,
		     ip_proto_to_snat_proto (bibe->proto), vrf_id);
  else
    vlib_cli_output (vm, " %U %U %U %U protocol %u vrf %u",
		     format_ip6_address, &bibe->in_addr,
		     format_ip6_address, &ste->in_r_addr,
		     format_ip4_address, &bibe->out_addr,
		     format_ip4_address, &ste->out_r_addr,
		     bibe->proto, vrf_id);

  return 0;
}

static clib_error_t *
nat64_show_st_command_fn (vlib_main_t * vm,
			  unformat_input_t * input, vlib_cli_command_t * cmd)
{
  nat64_main_t *nm = &nat64_main;
  unformat_input_t _line_input, *line_input = &_line_input;
  clib_error_t *error = 0;
  u32 proto = ~0;
  u8 p = 255;

  if (nm->is_disabled)
    return clib_error_return (0,
			      "NAT64 disabled, multi thread not supported");

  if (!unformat_user (input, unformat_line_input, line_input))
    return 0;

  if (unformat (line_input, "%U", unformat_snat_protocol, &proto))
    p = snat_proto_to_ip_proto (proto);
  else if (unformat (line_input, "unknown"))
    p = 0;
  else if (unformat (line_input, "all"))
    ;
  else
    {
      error = clib_error_return (0, "unknown input: '%U'",
				 format_unformat_error, line_input);
      goto done;
    }

  if (p == 255)
    vlib_cli_output (vm, "NAT64 sessions:");
  else
    vlib_cli_output (vm, "NAT64 %U sessions:", format_snat_protocol, proto);
  nat64_db_st_walk (&nm->db, p, nat64_cli_st_walk, vm);

done:
  unformat_free (line_input);

  return error;
}

static clib_error_t *
nat64_add_del_prefix_command_fn (vlib_main_t * vm, unformat_input_t * input,
				 vlib_cli_command_t * cmd)
{
  nat64_main_t *nm = &nat64_main;
  clib_error_t *error = 0;
  unformat_input_t _line_input, *line_input = &_line_input;
  u8 is_add = 1;
  u32 vrf_id = 0;
  ip6_address_t prefix;
  u32 plen = 0;
  int rv;

  if (nm->is_disabled)
    return clib_error_return (0,
			      "NAT64 disabled, multi thread not supported");

  if (!unformat_user (input, unformat_line_input, line_input))
    return 0;

  while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
    {
      if (unformat
	  (line_input, "%U/%u", unformat_ip6_address, &prefix, &plen))
	;
      else if (unformat (line_input, "tenant-vrf %u", &vrf_id))
	;
      else if (unformat (line_input, "del"))
	is_add = 0;
      else
	{
	  error = clib_error_return (0, "unknown input: '%U'",
				     format_unformat_error, line_input);
	  goto done;
	}
    }

  if (!plen)
    {
      error = clib_error_return (0, "NAT64 prefix must be set.");
      goto done;
    }

  rv = nat64_add_del_prefix (&prefix, (u8) plen, vrf_id, is_add);

  switch (rv)
    {
    case VNET_API_ERROR_NO_SUCH_ENTRY:
      error = clib_error_return (0, "NAT64 prefix not exist.");
      goto done;
    case VNET_API_ERROR_INVALID_VALUE:
      error = clib_error_return (0, "Invalid prefix length.");
      goto done;
    default:
      break;
    }

done:
  unformat_free (line_input);

  return error;
}

static int
nat64_cli_prefix_walk (nat64_prefix_t * p, void *ctx)
{
  vlib_main_t *vm = ctx;

  vlib_cli_output (vm, " %U/%u tenant-vrf %u",
		   format_ip6_address, &p->prefix, p->plen, p->vrf_id);

  return 0;
}

static clib_error_t *
nat64_show_prefix_command_fn (vlib_main_t * vm, unformat_input_t * input,
			      vlib_cli_command_t * cmd)
{
  nat64_main_t *nm = &nat64_main;

  if (nm->is_disabled)
    return clib_error_return (0,
			      "NAT64 disabled, multi thread not supported");

  vlib_cli_output (vm, "NAT64 prefix:");
  nat64_prefix_walk (nat64_cli_prefix_walk, vm);

  return 0;
}

/* *INDENT-OFF* */

/*?
 * @cliexpar
 * @cliexstart{nat64 add pool address}
 * Add/delete NAT64 pool address.
 * To add single NAT64 pool address use:
 *  vpp# nat64 add pool address 10.1.1.10
 * To add NAT64 pool address range use:
 *  vpp# nat64 add pool address 10.1.1.2 - 10.1.1.5
 * To add NAT64 pool address for specific tenant use:
 *  vpp# nat64 add pool address 10.1.1.100 tenant-vrf 100
 * @cliexend
?*/
VLIB_CLI_COMMAND (nat64_add_pool_address_command, static) = {
  .path = "nat64 add pool address",
  .short_help = "nat64 add pool address <ip4-range-start> [- <ip4-range-end>] "
                "[tenant-vrf <vrf-id>] [del]",
  .function = nat64_add_del_pool_addr_command_fn,
};

/*?
 * @cliexpar
 * @cliexstart{show nat64 pool}
 * Show NAT64 pool.
 *  vpp# show nat64 pool
 *  NAT64 pool:
 *   10.1.1.3 tenant VRF: 0
 *   10.1.1.10 tenant VRF: 10
 * @cliexend
?*/
VLIB_CLI_COMMAND (show_nat64_pool_command, static) = {
  .path = "show nat64 pool",
  .short_help = "show nat64 pool",
  .function = nat64_show_pool_command_fn,
};

/*?
 * @cliexpar
 * @cliexstart{set interface nat64}
 * Enable/disable NAT64 feature on the interface.
 * To enable NAT64 feature with local (IPv6) network interface
 * GigabitEthernet0/8/0 and external (IPv4) network interface
 * GigabitEthernet0/a/0 use:
 *  vpp# set interface nat64 in GigabitEthernet0/8/0 out GigabitEthernet0/a/0
 * @cliexend
?*/
VLIB_CLI_COMMAND (set_interface_nat64_command, static) = {
  .path = "set interface nat64",
  .short_help = "set interface nat64 in|out <intfc> [del]",
  .function = nat64_interface_feature_command_fn,
};

/*?
 * @cliexpar
 * @cliexstart{show nat64 interfaces}
 * Show interfaces with NAT64 feature.
 * To show interfaces with NAT64 feature use:
 *  vpp# show nat64 interfaces
 *  NAT64 interfaces:
 *   GigabitEthernet0/8/0 in
 *   GigabitEthernet0/a/0 out
 * @cliexend
?*/
VLIB_CLI_COMMAND (show_nat64_interfaces_command, static) = {
  .path = "show nat64 interfaces",
  .short_help = "show nat64 interfaces",
  .function = nat64_show_interfaces_command_fn,
};

/*?
 * @cliexpar
 * @cliexstart{nat64 add static bib}
 * Add/delete NAT64 static BIB entry.
 * To create NAT64 satatic BIB entry use:
 *  vpp# nat64 add static bib 2001:db8:c000:221:: 1234 10.1.1.3 5678 tcp
 *  vpp# nat64 add static bib 2001:db8:c000:221:: 1234 10.1.1.3 5678 udp vrf 10
 * @cliexend
?*/
VLIB_CLI_COMMAND (nat64_add_del_static_bib_command, static) = {
  .path = "nat64 add static bib",
  .short_help = "nat64 add static bib <ip6-addr> <port> <ip4-addr> <port> "
                "tcp|udp|icmp [vfr <table-id>] [del]",
  .function = nat64_add_del_static_bib_command_fn,
};

/*?
 * @cliexpar
 * @cliexstart{show nat64 bib}
 * Show NAT64 BIB entries.
 * To show NAT64 TCP BIB entries use:
 *  vpp# show nat64 bib tcp
 *  NAT64 tcp BIB:
 *   fd01:1::2 6303 10.0.0.3 62303 tcp vrf 0 dynamic 1 sessions
 *   2001:db8:c000:221:: 1234 10.1.1.3 5678 tcp vrf 0 static 2 sessions
 * To show NAT64 UDP BIB entries use:
 *  vpp# show nat64 bib udp
 *  NAT64 udp BIB:
 *   fd01:1::2 6304 10.0.0.3 10546 udp vrf 0 dynamic 10 sessions
 *   2001:db8:c000:221:: 1234 10.1.1.3 5678 udp vrf 10 static 0 sessions
 * To show NAT64 ICMP BIB entries use:
 *  vpp# show nat64 bib icmp
 *  NAT64 icmp BIB:
 *   fd01:1::2 6305 10.0.0.3 63209 icmp vrf 10 dynamic 1 sessions
 * @cliexend
?*/
VLIB_CLI_COMMAND (show_nat64_bib_command, static) = {
  .path = "show nat64 bib",
  .short_help = "show nat64 bib all|tcp|udp|icmp|unknown",
  .function = nat64_show_bib_command_fn,
};

/*?
 * @cliexpar
 * @cliexstart{set nat64 timeouts}
 * Set NAT64 session timeouts (in seconds).
 * To set NAT64 session timeoutes use use:
 *  vpp# set nat64 timeouts udp 200 icmp 30 tcp-trans 250 tcp-est 7450
 * To reset NAT64 session timeoutes to default values use:
 *  vpp# set nat64 timeouts reset
 * @cliexend
?*/
VLIB_CLI_COMMAND (set_nat64_timeouts_command, static) = {
  .path = "set nat64 timeouts",
  .short_help = "set nat64 timeouts udp <sec> icmp <sec> tcp-trans <sec> "
                "tcp-est <sec> tcp-incoming-syn <sec> | reset",
  .function = nat64_set_timeouts_command_fn,
};

/*?
 * @cliexpar
 * @cliexstart{show nat64 timeoutss}
 * Show NAT64 session timeouts:
 *  vpp# show nat64 timeouts
 *  NAT64 session timeouts:
 *   UDP 300sec
 *   ICMP 60sec
 *   TCP transitory 240sec
 *   TCP established 7440sec
 *   TCP incoming SYN 6sec
 * @cliexend
?*/
VLIB_CLI_COMMAND (show_nat64_timeouts_command, static) = {
  .path = "show nat64 timeouts",
  .short_help = "show nat64 timeouts",
  .function = nat64_show_timeouts_command_fn,
};

/*?
 * @cliexpar
 * @cliexstart{show nat64 session table}
 * Show NAT64 session table.
 * To show NAT64 TCP session table use:
 *  vpp# show nat64 session table tcp
 *  NAT64 tcp session table:
 *   fd01:1::2 6303 64:ff9b::ac10:202 20 10.0.0.3 62303 172.16.2.2 20 tcp vrf 0
 *   fd01:3::2 6303 64:ff9b::ac10:202 20 10.0.10.3 21300 172.16.2.2 20 tcp vrf 10
 * To show NAT64 UDP session table use:
 * #vpp show nat64 session table udp
 * NAT64 udp session table:
 *  fd01:1::2 6304 64:ff9b::ac10:202 20 10.0.0.3 10546 172.16.2.2 20 udp vrf 0
 *  fd01:3::2 6304 64:ff9b::ac10:202 20 10.0.10.3 58627 172.16.2.2 20 udp vrf 10
 *  fd01:1::2 1235 64:ff9b::a00:3 4023 10.0.0.3 24488 10.0.0.3 4023 udp vrf 0
 *  fd01:1::3 23 64:ff9b::a00:3 24488 10.0.0.3 4023 10.0.0.3 24488 udp vrf 0
 * To show NAT64 ICMP session table use:
 * #vpp show nat64 session table icmp
 * NAT64 icmp session table:
 *  fd01:1::2 64:ff9b::ac10:202 6305 10.0.0.3 172.16.2.2 63209 icmp vrf 0
 * @cliexend
?*/
VLIB_CLI_COMMAND (show_nat64_st_command, static) = {
  .path = "show nat64 session table",
  .short_help = "show nat64 session table all|tcp|udp|icmp|unknown",
  .function = nat64_show_st_command_fn,
};

/*?
 * @cliexpar
 * @cliexstart{nat64 add prefix}
 * Set NAT64 prefix for generating IPv6 representations of IPv4 addresses.
 * To set NAT64 global prefix use:
 *  vpp# nat64 add prefix 2001:db8::/32
 * To set NAT64 prefix for specific tenant use:
 *  vpp# nat64 add prefix 2001:db8:122:300::/56 tenant-vrf 10
 * @cliexend
?*/
VLIB_CLI_COMMAND (nat64_add_del_prefix_command, static) = {
  .path = "nat64 add prefix",
  .short_help = "nat64 add prefix <ip6-prefix>/<plen> [tenant-vrf <vrf-id>] "
                "[del]",
  .function = nat64_add_del_prefix_command_fn,
};

/*?
 * @cliexpar
 * @cliexstart{show nat64 prefix}
 * Show NAT64 prefix.
 * To show NAT64 prefix use:
 *  vpp# show nat64 prefix
 *  NAT64 prefix:
 *   2001:db8::/32 tenant-vrf 0
 *   2001:db8:122:300::/56 tenant-vrf 10
 * @cliexend
?*/
VLIB_CLI_COMMAND (show_nat64_prefix_command, static) = {
  .path = "show nat64 prefix",
  .short_help = "show nat64 prefix",
  .function = nat64_show_prefix_command_fn,
};

/* *INDENT-ON* */

/*
 * fd.io coding-style-patch-verification: ON
 *
 * Local Variables:
 * eval: (c-set-style "gnu")
 * End:
 */