aboutsummaryrefslogtreecommitdiffstats
path: root/src/vnet/fib/fib_test.c
diff options
context:
space:
mode:
authorNeale Ranns <nranns@cisco.com>2017-10-21 10:53:20 -0700
committerDamjan Marion <dmarion.lists@gmail.com>2017-11-09 15:16:52 +0000
commitd792d9c01e60656cbfe1b0f1fd6a9b125f5dab0c (patch)
treedb88d99dd8102389fb92e8ed44bc7d6a55dc3080 /src/vnet/fib/fib_test.c
parenta2ff7b8cfc829ffbb6d5de7534efb51f7cba9cf3 (diff)
BIER
- see draft-ietf-bier-mpls-encapsulation-10 - midpoint, head and tail functions - supported payload protocols; IPv4 and IPv6 only. Change-Id: I59d7363bb6fdfdce8e4016a68a9c8f5a5e5791cb Signed-off-by: Neale Ranns <nranns@cisco.com>
Diffstat (limited to 'src/vnet/fib/fib_test.c')
-rw-r--r--src/vnet/fib/fib_test.c67
1 files changed, 52 insertions, 15 deletions
diff --git a/src/vnet/fib/fib_test.c b/src/vnet/fib/fib_test.c
index 66a38236216..2658eb27811 100644
--- a/src/vnet/fib/fib_test.c
+++ b/src/vnet/fib/fib_test.c
@@ -13,6 +13,7 @@
* limitations under the License.
*/
+#include <vnet/fib/fib_test.h>
#include <vnet/fib/ip6_fib.h>
#include <vnet/fib/ip4_fib.h>
#include <vnet/fib/mpls_fib.h>
@@ -544,15 +545,31 @@ fib_test_validate_lb_v (const load_balance_t *lb,
dpo->dpoi_index,
exp->lb.lb);
break;
- case FT_LB_SPECIAL:
- FIB_TEST_I((DPO_DROP == dpo->dpoi_type),
- "bucket %d stacks on %U",
- bucket,
- format_dpo_type, dpo->dpoi_type);
- FIB_TEST_LB((exp->special.adj == dpo->dpoi_index),
- "bucket %d stacks on drop %d",
+ case FT_LB_BIER_TABLE:
+ FIB_TEST_LB((DPO_BIER_TABLE == dpo->dpoi_type),
+ "bucket %d stacks on %U",
+ bucket,
+ format_dpo_type, dpo->dpoi_type);
+ FIB_TEST_LB((exp->bier.table == dpo->dpoi_index),
+ "bucket %d stacks on lb %d",
+ bucket,
+ exp->bier.table);
+ break;
+ case FT_LB_BIER_FMASK:
+ FIB_TEST_LB((DPO_BIER_FMASK == dpo->dpoi_type),
+ "bucket %d stacks on %U",
+ bucket,
+ format_dpo_type, dpo->dpoi_type);
+ FIB_TEST_LB((exp->bier.fmask == dpo->dpoi_index),
+ "bucket %d stacks on lb %d",
bucket,
- exp->special.adj);
+ exp->bier.fmask);
+ break;
+ case FT_LB_DROP:
+ FIB_TEST_LB((DPO_DROP == dpo->dpoi_type),
+ "bucket %d stacks on %U",
+ bucket,
+ format_dpo_type, dpo->dpoi_type);
break;
}
}
@@ -560,6 +577,29 @@ fib_test_validate_lb_v (const load_balance_t *lb,
}
int
+fib_test_validate_lb (const dpo_id_t *dpo,
+ u16 n_buckets,
+ ...)
+{
+ const load_balance_t *lb;
+ va_list ap;
+ int res;
+
+ va_start(ap, n_buckets);
+
+ FIB_TEST_LB((DPO_LOAD_BALANCE == dpo->dpoi_type),
+ "Entry links to %U",
+ format_dpo_type, dpo->dpoi_type);
+ lb = load_balance_get(dpo->dpoi_index);
+
+ res = fib_test_validate_lb_v(lb, n_buckets, &ap);
+
+ va_end(ap);
+
+ return (res);
+}
+
+int
fib_test_validate_entry (fib_node_index_t fei,
fib_forward_chain_type_t fct,
int n_buckets,
@@ -6678,13 +6718,10 @@ fib_test_label (void)
* remove the other path with a valid label
*/
fib_test_lb_bucket_t bucket_drop = {
- .type = FT_LB_SPECIAL,
- .special = {
- .adj = DPO_PROTO_IP4,
- },
+ .type = FT_LB_DROP,
};
fib_test_lb_bucket_t mpls_bucket_drop = {
- .type = FT_LB_SPECIAL,
+ .type = FT_LB_DROP,
.special = {
.adj = DPO_PROTO_MPLS,
},
@@ -8497,13 +8534,13 @@ lfib_test (void)
* A recursive via a label that does not exist
*/
fib_test_lb_bucket_t bucket_drop = {
- .type = FT_LB_SPECIAL,
+ .type = FT_LB_DROP,
.special = {
.adj = DPO_PROTO_IP4,
},
};
fib_test_lb_bucket_t mpls_bucket_drop = {
- .type = FT_LB_SPECIAL,
+ .type = FT_LB_DROP,
.special = {
.adj = DPO_PROTO_MPLS,
},
' href='#n332'>332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 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
/*
 * l2_vtr.c : layer 2 vlan tag rewrite configuration
 *
 * Copyright (c) 2013 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.
 */

#include <vlib/vlib.h>
#include <vnet/vnet.h>
#include <vnet/ethernet/ethernet.h>
#include <vnet/ethernet/packet.h>
#include <vnet/l2/l2_input.h>
#include <vnet/l2/l2_output.h>
#include <vnet/l2/feat_bitmap.h>
#include <vnet/l2/l2_vtr.h>
#include <vnet/l2/l2_input_vtr.h>
#include <vnet/l2/l2_output.h>

#include <vppinfra/error.h>
#include <vlib/cli.h>

/**
 * @file
 * @brief Ethernet VLAN Tag Rewrite.
 *
 * VLAN tag rewrite provides the ability to change the VLAN tags on a packet.
 * Existing tags can be popped, new tags can be pushed, and existing tags can
 * be swapped with new tags. The rewrite feature is attached to a subinterface
 * as input and output operations. The input operation is explicitly configured.
 * The output operation is the symmetric opposite and is automatically derived
 * from the input operation.
 */

/** Just a placeholder; ensures file is not eliminated by linker. */
clib_error_t *
l2_vtr_init (vlib_main_t * vm)
{
  return 0;
}

VLIB_INIT_FUNCTION (l2_vtr_init);

u32
l2pbb_configure (vlib_main_t * vlib_main,
		 vnet_main_t * vnet_main, u32 sw_if_index, u32 vtr_op,
		 u8 * b_dmac, u8 * b_smac,
		 u16 b_vlanid, u32 i_sid, u16 vlan_outer_tag)
{
  u32 error = 0;
  u32 enable = 0;

  l2_output_config_t *config = 0;
  vnet_hw_interface_t *hi;
  hi = vnet_get_sup_hw_interface (vnet_main, sw_if_index);

  if (!hi)
    {
      error = VNET_API_ERROR_INVALID_INTERFACE;
      goto done;
    }

  // Config for this interface should be already initialized
  ptr_config_t *in_config;
  ptr_config_t *out_config;
  config = vec_elt_at_index (l2output_main.configs, sw_if_index);
  in_config = &(config->input_pbb_vtr);
  out_config = &(config->output_pbb_vtr);

  in_config->pop_bytes = 0;
  in_config->push_bytes = 0;
  out_config->pop_bytes = 0;
  out_config->push_bytes = 0;
  enable = (vtr_op != L2_VTR_DISABLED);

  if (!enable)
    goto done;

  if (vtr_op == L2_VTR_POP_2)
    {
      in_config->pop_bytes = sizeof (ethernet_pbb_header_packed_t);
    }
  else if (vtr_op == L2_VTR_PUSH_2)
    {
      clib_memcpy_fast (in_config->macs_tags.b_dst_address, b_dmac,
			sizeof (in_config->macs_tags.b_dst_address));
      clib_memcpy_fast (in_config->macs_tags.b_src_address, b_smac,
			sizeof (in_config->macs_tags.b_src_address));
      in_config->macs_tags.b_type =
	clib_net_to_host_u16 (ETHERNET_TYPE_DOT1AD);
      in_config->macs_tags.priority_dei_id =
	clib_net_to_host_u16 (b_vlanid & 0xFFF);
      in_config->macs_tags.i_type =
	clib_net_to_host_u16 (ETHERNET_TYPE_DOT1AH);
      in_config->macs_tags.priority_dei_uca_res_sid =
	clib_net_to_host_u32 (i_sid & 0xFFFFF);
      in_config->push_bytes = sizeof (ethernet_pbb_header_packed_t);
    }
  else if (vtr_op == L2_VTR_TRANSLATE_2_2)
    {
      /* TODO after PoC */
    }

  /*
   *  Construct the output tag-rewrite config
   *
   *  The push/pop values are always reversed
   */
  out_config->raw_data = in_config->raw_data;
  out_config->pop_bytes = in_config->push_bytes;
  out_config->push_bytes = in_config->pop_bytes;

done:
  l2input_intf_bitmap_enable (sw_if_index, L2INPUT_FEAT_VTR, enable);
  if (config)
    config->out_vtr_flag = (u8) enable;

  /* output vtr enable is checked explicitly in l2_output */
  return error;
}

/**
 * Configure vtag tag rewrite on the given interface.
 * Return 1 if there is an error, 0 if ok
 */
u32
l2vtr_configure (vlib_main_t * vlib_main, vnet_main_t * vnet_main, u32 sw_if_index, u32 vtr_op, u32 push_dot1q,	/* ethertype of first pushed tag is dot1q/dot1ad */
		 u32 vtr_tag1,	/* first pushed tag */
		 u32 vtr_tag2)	/* second pushed tag */
{
  vnet_hw_interface_t *hi;
  vnet_sw_interface_t *si;
  u32 hw_no_tags;
  u32 error = 0;
  l2_output_config_t *config;
  vtr_config_t *in_config;
  vtr_config_t *out_config;
  u32 enable;
  u32 push_inner_et;
  u32 push_outer_et;
  u32 cfg_tags;

  hi = vnet_get_sup_hw_interface (vnet_main, sw_if_index);
  if (!hi || (hi->hw_class_index != ethernet_hw_interface_class.index))
    {
      error = VNET_API_ERROR_INVALID_INTERFACE;	/* non-ethernet interface */
      goto done;
    }

  /* Init the config for this interface */
  vec_validate (l2output_main.configs, sw_if_index);
  config = vec_elt_at_index (l2output_main.configs, sw_if_index);
  in_config = &(config->input_vtr);
  out_config = &(config->output_vtr);
  in_config->raw_tags = 0;
  out_config->raw_tags = 0;

  /* Get the configured tags for the interface */
  si = vnet_get_sw_interface (vnet_main, sw_if_index);
  hw_no_tags = (si->type == VNET_SW_INTERFACE_TYPE_HARDWARE);

  /* Construct the input tag-rewrite config */

  push_outer_et =
    clib_net_to_host_u16 (push_dot1q ? ETHERNET_TYPE_VLAN :
			  ETHERNET_TYPE_DOT1AD);
  push_inner_et = clib_net_to_host_u16 (ETHERNET_TYPE_VLAN);
  vtr_tag1 = clib_net_to_host_u16 (vtr_tag1);
  vtr_tag2 = clib_net_to_host_u16 (vtr_tag2);

  /* Determine number of vlan tags with explicitly configured values */
  cfg_tags = 0;
  if (hw_no_tags || si->sub.eth.flags.no_tags)
    {
      cfg_tags = 0;
    }
  else if (si->sub.eth.flags.one_tag)
    {
      cfg_tags = 1;
      if (si->sub.eth.flags.outer_vlan_id_any)
	{
	  cfg_tags = 0;
	}
    }
  else if (si->sub.eth.flags.two_tags)
    {
      cfg_tags = 2;
      if (si->sub.eth.flags.inner_vlan_id_any)
	{
	  cfg_tags = 1;
	}
      if (si->sub.eth.flags.outer_vlan_id_any)
	{
	  cfg_tags = 0;
	}
    }

  switch (vtr_op)
    {
    case L2_VTR_DISABLED:
      in_config->push_and_pop_bytes = 0;
      break;

    case L2_VTR_POP_1:
      if (cfg_tags < 1)
	{
	  /* Need one or two tags */
	  error = VNET_API_ERROR_INVALID_VLAN_TAG_COUNT;
	  goto done;
	}
      in_config->pop_bytes = 4;
      in_config->push_bytes = 0;
      break;

    case L2_VTR_POP_2:
      if (cfg_tags < 2)
	{
	  error = VNET_API_ERROR_INVALID_VLAN_TAG_COUNT;	/* Need two tags */
	  goto done;
	}
      in_config->pop_bytes = 8;
      in_config->push_bytes = 0;
      break;

    case L2_VTR_PUSH_1:
      in_config->pop_bytes = 0;
      in_config->push_bytes = 4;
      in_config->tags[1].priority_cfi_and_id = vtr_tag1;
      in_config->tags[1].type = push_outer_et;
      break;

    case L2_VTR_PUSH_2:
      in_config->pop_bytes = 0;
      in_config->push_bytes = 8;
      in_config->tags[0].priority_cfi_and_id = vtr_tag1;
      in_config->tags[0].type = push_outer_et;
      in_config->tags[1].priority_cfi_and_id = vtr_tag2;
      in_config->tags[1].type = push_inner_et;
      break;

    case L2_VTR_TRANSLATE_1_1:
      if (cfg_tags < 1)
	{
	  error = VNET_API_ERROR_INVALID_VLAN_TAG_COUNT;	/* Need one or two tags */
	  goto done;
	}
      in_config->pop_bytes = 4;
      in_config->push_bytes = 4;
      in_config->tags[1].priority_cfi_and_id = vtr_tag1;
      in_config->tags[1].type = push_outer_et;
      break;

    case L2_VTR_TRANSLATE_1_2:
      if (cfg_tags < 1)
	{
	  error = VNET_API_ERROR_INVALID_VLAN_TAG_COUNT;	/* Need one or two tags */
	  goto done;
	}
      in_config->pop_bytes = 4;
      in_config->push_bytes = 8;
      in_config->tags[0].priority_cfi_and_id = vtr_tag1;
      in_config->tags[0].type = push_outer_et;
      in_config->tags[1].priority_cfi_and_id = vtr_tag2;
      in_config->tags[1].type = push_inner_et;
      break;

    case L2_VTR_TRANSLATE_2_1:
      if (cfg_tags < 2)
	{
	  error = VNET_API_ERROR_INVALID_VLAN_TAG_COUNT;	/* Need two tags */
	  goto done;
	}
      in_config->pop_bytes = 8;
      in_config->push_bytes = 4;
      in_config->tags[1].priority_cfi_and_id = vtr_tag1;
      in_config->tags[1].type = push_outer_et;
      break;

    case L2_VTR_TRANSLATE_2_2:
      if (cfg_tags < 2)
	{
	  error = VNET_API_ERROR_INVALID_VLAN_TAG_COUNT;	/* Need two tags */
	  goto done;
	}
      in_config->pop_bytes = 8;
      in_config->push_bytes = 8;
      in_config->tags[0].priority_cfi_and_id = vtr_tag1;
      in_config->tags[0].type = push_outer_et;
      in_config->tags[1].priority_cfi_and_id = vtr_tag2;
      in_config->tags[1].type = push_inner_et;
      break;
    }

  /*
   *  Construct the output tag-rewrite config
   *
   *  The push/pop values are always reversed
   */
  out_config->push_bytes = in_config->pop_bytes;
  out_config->pop_bytes = in_config->push_bytes;

  /* Any pushed tags are derived from the subinterface config */
  push_outer_et =
    clib_net_to_host_u16 (si->sub.eth.flags.dot1ad ? ETHERNET_TYPE_DOT1AD :
			  ETHERNET_TYPE_VLAN);
  push_inner_et = clib_net_to_host_u16 (ETHERNET_TYPE_VLAN);
  vtr_tag1 = clib_net_to_host_u16 (si->sub.eth.outer_vlan_id);
  vtr_tag2 = clib_net_to_host_u16 (si->sub.eth.inner_vlan_id);

  if (out_config->push_bytes == 4)
    {
      out_config->tags[1].priority_cfi_and_id = vtr_tag1;
      out_config->tags[1].type = push_outer_et;
    }
  else if (out_config->push_bytes == 8)
    {
      out_config->tags[0].priority_cfi_and_id = vtr_tag1;
      out_config->tags[0].type = push_outer_et;
      out_config->tags[1].priority_cfi_and_id = vtr_tag2;
      out_config->tags[1].type = push_inner_et;
    }

  /* set the interface enable flags */
  enable = (vtr_op != L2_VTR_DISABLED);
  config->out_vtr_flag = (u8) enable;
  l2input_intf_bitmap_enable (sw_if_index, L2INPUT_FEAT_VTR, enable);
  /* output vtr enable is checked explicitly in l2_output */

done:
  return error;
}

/**
 * Get vtag tag rewrite on the given interface.
 * Return 1 if there is an error, 0 if ok
 */
u32
l2vtr_get (vlib_main_t * vlib_main, vnet_main_t * vnet_main, u32 sw_if_index, u32 * vtr_op, u32 * push_dot1q,	/* ethertype of first pushed tag is dot1q/dot1ad */
	   u32 * vtr_tag1,	/* first pushed tag */
	   u32 * vtr_tag2)	/* second pushed tag */
{
  vnet_hw_interface_t *hi;
  u32 error = 0;
  vtr_config_t *in_config;

  if (!vtr_op || !push_dot1q || !vtr_tag1 || !vtr_tag2)
    {
      clib_warning ("invalid arguments");
      error = VNET_API_ERROR_INVALID_ARGUMENT;
      goto done;
    }

  *vtr_op = L2_VTR_DISABLED;
  *vtr_tag1 = 0;
  *vtr_tag2 = 0;
  *push_dot1q = 0;

  hi = vnet_get_sup_hw_interface (vnet_main, sw_if_index);
  if (!hi || (hi->hw_class_index != ethernet_hw_interface_class.index))
    {
      /* non-ethernet interface */
      goto done;
    }

  if (sw_if_index >= vec_len (l2output_main.configs))
    {
      /* no specific config (return disabled) */
      goto done;
    }

  /* Get the config for this interface */
  in_config =
    &(vec_elt_at_index (l2output_main.configs, sw_if_index)->input_vtr);

  /* DISABLED */
  if (in_config->push_and_pop_bytes == 0)
    {
      goto done;
    }

  /* find out vtr_op */
  switch (in_config->pop_bytes)
    {
    case 0:
      switch (in_config->push_bytes)
	{
	case 0:
	  /* DISABLED */
	  goto done;
	case 4:
	  *vtr_op = L2_VTR_PUSH_1;
	  *vtr_tag1 =
	    clib_host_to_net_u16 (in_config->tags[1].priority_cfi_and_id);
	  *push_dot1q =
	    (ETHERNET_TYPE_VLAN ==
	     clib_host_to_net_u16 (in_config->tags[1].type));
	  break;
	case 8:
	  *vtr_op = L2_VTR_PUSH_2;
	  *vtr_tag1 =
	    clib_host_to_net_u16 (in_config->tags[0].priority_cfi_and_id);
	  *vtr_tag2 =
	    clib_host_to_net_u16 (in_config->tags[1].priority_cfi_and_id);
	  *push_dot1q =
	    (ETHERNET_TYPE_VLAN ==
	     clib_host_to_net_u16 (in_config->tags[0].type));
	  break;
	default:
	  clib_warning ("invalid push_bytes count: %d",
			in_config->push_bytes);
	  error = VNET_API_ERROR_UNEXPECTED_INTF_STATE;
	  goto done;
	}
      break;

    case 4:
      switch (in_config->push_bytes)
	{
	case 0:
	  *vtr_op = L2_VTR_POP_1;
	  break;
	case 4:
	  *vtr_op = L2_VTR_TRANSLATE_1_1;
	  *vtr_tag1 =
	    clib_host_to_net_u16 (in_config->tags[1].priority_cfi_and_id);
	  *push_dot1q =
	    (ETHERNET_TYPE_VLAN ==
	     clib_host_to_net_u16 (in_config->tags[1].type));
	  break;
	case 8:
	  *vtr_op = L2_VTR_TRANSLATE_1_2;
	  *vtr_tag1 =
	    clib_host_to_net_u16 (in_config->tags[0].priority_cfi_and_id);
	  *vtr_tag2 =
	    clib_host_to_net_u16 (in_config->tags[1].priority_cfi_and_id);
	  *push_dot1q =
	    (ETHERNET_TYPE_VLAN ==
	     clib_host_to_net_u16 (in_config->tags[0].type));
	  break;
	default:
	  clib_warning ("invalid push_bytes count: %d",
			in_config->push_bytes);
	  error = VNET_API_ERROR_UNEXPECTED_INTF_STATE;
	  goto done;
	}
      break;

    case 8:
      switch (in_config->push_bytes)
	{
	case 0:
	  *vtr_op = L2_VTR_POP_2;
	  break;
	case 4:
	  *vtr_op = L2_VTR_TRANSLATE_2_1;
	  *vtr_tag1 =
	    clib_host_to_net_u16 (in_config->tags[1].priority_cfi_and_id);
	  *push_dot1q =
	    (ETHERNET_TYPE_VLAN ==
	     clib_host_to_net_u16 (in_config->tags[1].type));
	  break;
	case 8:
	  *vtr_op = L2_VTR_TRANSLATE_2_2;
	  *vtr_tag1 =
	    clib_host_to_net_u16 (in_config->tags[0].priority_cfi_and_id);
	  *vtr_tag2 =
	    clib_host_to_net_u16 (in_config->tags[1].priority_cfi_and_id);
	  *push_dot1q =
	    (ETHERNET_TYPE_VLAN ==
	     clib_host_to_net_u16 (in_config->tags[0].type));
	  break;
	default:
	  clib_warning ("invalid push_bytes count: %d",
			in_config->push_bytes);
	  error = VNET_API_ERROR_UNEXPECTED_INTF_STATE;
	  goto done;
	}
      break;

    default:
      clib_warning ("invalid pop_bytes count: %d", in_config->pop_bytes);
      error = VNET_API_ERROR_UNEXPECTED_INTF_STATE;
      goto done;
    }

done:
  return error;
}

/**
 * Set subinterface vtr enable/disable.
 * The CLI format is:
 *    set interface l2 tag-rewrite <interface> [disable | pop 1 | pop 2 | push {dot1q|dot1ad} <tag> [<tag>]]
 *
 *  "push" can also be replaced by "translate-{1|2}-{1|2}"
 */
static clib_error_t *
int_l2_vtr (vlib_main_t * vm,
	    unformat_input_t * input, vlib_cli_command_t * cmd)
{
  vnet_main_t *vnm = vnet_get_main ();
  clib_error_t *error = 0;
  u32 sw_if_index;
  u32 vtr_op;
  u32 push_dot1q = 0;
  u32 tag1 = 0, tag2 = 0;

  if (!unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
    {
      error = clib_error_return (0, "unknown interface `%U'",
				 format_unformat_error, input);
      goto done;
    }

  vtr_op = L2_VTR_DISABLED;

  if (unformat (input, "disable"))
    {
      vtr_op = L2_VTR_DISABLED;
    }
  else if (unformat (input, "pop 1"))
    {
      vtr_op = L2_VTR_POP_1;
    }
  else if (unformat (input, "pop 2"))
    {
      vtr_op = L2_VTR_POP_2;

    }
  else if (unformat (input, "push dot1q %d %d", &tag1, &tag2))
    {
      vtr_op = L2_VTR_PUSH_2;
      push_dot1q = 1;
    }
  else if (unformat (input, "push dot1ad %d %d", &tag1, &tag2))
    {
      vtr_op = L2_VTR_PUSH_2;

    }
  else if (unformat (input, "push dot1q %d", &tag1))
    {
      vtr_op = L2_VTR_PUSH_1;
      push_dot1q = 1;
    }
  else if (unformat (input, "push dot1ad %d", &tag1))
    {
      vtr_op = L2_VTR_PUSH_1;

    }
  else if (unformat (input, "translate 1-1 dot1q %d", &tag1))
    {
      vtr_op = L2_VTR_TRANSLATE_1_1;
      push_dot1q = 1;
    }
  else if (unformat (input, "translate 1-1 dot1ad %d", &tag1))
    {
      vtr_op = L2_VTR_TRANSLATE_1_1;

    }
  else if (unformat (input, "translate 2-1 dot1q %d", &tag1))
    {
      vtr_op = L2_VTR_TRANSLATE_2_1;
      push_dot1q = 1;
    }
  else if (unformat (input, "translate 2-1 dot1ad %d", &tag1))
    {
      vtr_op = L2_VTR_TRANSLATE_2_1;

    }
  else if (unformat (input, "translate 2-2 dot1q %d %d", &tag1, &tag2))
    {
      vtr_op = L2_VTR_TRANSLATE_2_2;
      push_dot1q = 1;
    }
  else if (unformat (input, "translate 2-2 dot1ad %d %d", &tag1, &tag2))
    {
      vtr_op = L2_VTR_TRANSLATE_2_2;

    }
  else if (unformat (input, "translate 1-2 dot1q %d %d", &tag1, &tag2))
    {
      vtr_op = L2_VTR_TRANSLATE_1_2;
      push_dot1q = 1;
    }
  else if (unformat (input, "translate 1-2 dot1ad %d %d", &tag1, &tag2))
    {
      vtr_op = L2_VTR_TRANSLATE_1_2;

    }
  else
    {
      error =
	clib_error_return (0,
			   "expecting [disable | pop 1 | pop 2 | push {dot1q|dot1ah} <tag> [<tag>]\n"
			   " | translate {1|2}-{1|2} {dot1q|dot1ah} <tag> [<tag>]] but got `%U'",
			   format_unformat_error, input);
      goto done;
    }

  if (l2vtr_configure (vm, vnm, sw_if_index, vtr_op, push_dot1q, tag1, tag2))
    {
      error =
	clib_error_return (0,
			   "vlan tag rewrite is not compatible with interface");
      goto done;
    }

done:
  return error;
}

/*?
 * VLAN tag rewrite provides the ability to change the VLAN tags on a packet.
 * Existing tags can be popped, new tags can be pushed, and existing tags can
 * be swapped with new tags. The rewrite feature is attached to a subinterface
 * as input and output operations. The input operation is explicitly configured.
 * The output operation is the symmetric opposite and is automatically derived
 * from the input operation.
 *
 * <b>POP:</b> For pop operations, the subinterface encapsulation (the vlan
 * tags specified when it was created) must have at least the number of popped
 * tags. e.g. the \"pop 2\" operation would be rejected on a single-vlan interface.
 * The output tag-rewrite operation for pops is to push the specified number of
 * vlan tags onto the packet. The pushed tag values are the ones in the
 * subinterface encapsulation.
 *
 * <b>PUSH:</b> For push operations, the ethertype is also specified. The
 * output tag-rewrite operation for pushes is to pop the same number of tags
 * off the packet. If the packet doesn't have enough tags it is dropped.
 *
 *
 * @cliexpar
 * @parblock
 * By default a subinterface has no tag-rewrite. To return a subinterface to
 * this state use:
 * @cliexcmd{set interface l2 tag-rewrite GigabitEthernet0/8/0.200 disable}
 *
 * To pop vlan tags off packets received from a subinterface, use:
 * @cliexcmd{set interface l2 tag-rewrite GigabitEthernet0/8/0.200 pop 1}
 * @cliexcmd{set interface l2 tag-rewrite GigabitEthernet0/8/0.200 pop 2}
 *
 * To push one or two vlan tags onto packets received from an interface, use:
 * @cliexcmd{set interface l2 tag-rewrite GigabitEthernet0/8/0.200 push dot1q 100}
 * @cliexcmd{set interface l2 tag-rewrite GigabitEthernet0/8/0.200 push dot1ad 100 150}
 *
 * Tags can also be translated, which is basically a combination of a pop and push.
 * @cliexcmd{set interface l2 tag-rewrite GigabitEthernet0/8/0.200 translate 1-1 dot1ad 100}
 * @cliexcmd{set interface l2 tag-rewrite GigabitEthernet0/8/0.200 translate 2-2 dot1ad 100 150}
 * @cliexcmd{set interface l2 tag-rewrite GigabitEthernet0/8/0.200 translate 1-2 dot1q 100}
 * @cliexcmd{set interface l2 tag-rewrite GigabitEthernet0/8/0.200 translate 2-1 dot1q 100 150}
 *
 * To display the VLAN Tag settings, show the associate bridge-domain:
 * @cliexstart{show bridge-domain 200 detail}
 *  ID   Index   Learning   U-Forwrd   UU-Flood   Flooding   ARP-Term     BVI-Intf
 * 200     1        on         on         on         on         off          N/A
 *
 *          Interface           Index  SHG  BVI        VLAN-Tag-Rewrite
 *  GigabitEthernet0/8/0.200      5     0    -       trans-1-1 dot1ad 100
 *  GigabitEthernet0/9/0.200      4     0    -               none
 *  GigabitEthernet0/a/0.200      6     0    -               none
 * @cliexend
 * @endparblock
?*/
/* *INDENT-OFF* */
VLIB_CLI_COMMAND (int_l2_vtr_cli, static) = {
  .path = "set interface l2 tag-rewrite",
  .short_help = "set interface l2 tag-rewrite <interface> [disable | pop {1|2} | push {dot1q|dot1ad} <tag> <tag>]",
  .function = int_l2_vtr,
};
/* *INDENT-ON* */

/**
 * Get pbb tag rewrite on the given interface.
 * Return 1 if there is an error, 0 if ok
 */
u32
l2pbb_get (vlib_main_t * vlib_main, vnet_main_t * vnet_main, u32 sw_if_index,
	   u32 * vtr_op, u16 * outer_tag, ethernet_header_t * eth_hdr,
	   u16 * b_vlanid, u32 * i_sid)
{
  u32 error = 1;
  ptr_config_t *in_config;

  if (!vtr_op || !outer_tag || !b_vlanid || !i_sid)
    {
      clib_warning ("invalid arguments");
      error = VNET_API_ERROR_INVALID_ARGUMENT;
      goto done;
    }

  *vtr_op = L2_VTR_DISABLED;
  *outer_tag = 0;
  *b_vlanid = 0;
  *i_sid = 0;

  if (sw_if_index >= vec_len (l2output_main.configs))
    {
      /* no specific config (return disabled) */
      goto done;
    }

  /* Get the config for this interface */
  in_config =
    &(vec_elt_at_index (l2output_main.configs, sw_if_index)->input_pbb_vtr);

  if (in_config->push_and_pop_bytes == 0)
    {
      /* DISABLED */
      goto done;
    }
  else
    {
      if (in_config->pop_bytes && in_config->push_bytes)
	*vtr_op = L2_VTR_TRANSLATE_2_1;
      else if (in_config->pop_bytes)
	*vtr_op = L2_VTR_POP_2;
      else if (in_config->push_bytes)
	*vtr_op = L2_VTR_PUSH_2;

      clib_memcpy_fast (&eth_hdr->dst_address,
			in_config->macs_tags.b_dst_address,
			sizeof (eth_hdr->dst_address));
      clib_memcpy_fast (&eth_hdr->src_address,
			in_config->macs_tags.b_src_address,
			sizeof (eth_hdr->src_address));

      *b_vlanid =
	clib_host_to_net_u16 (in_config->macs_tags.priority_dei_id) & 0xFFF;
      *i_sid =
	clib_host_to_net_u32 (in_config->macs_tags.
			      priority_dei_uca_res_sid) & 0xFFFFF;
      error = 0;
    }
done:
  return error;
}

/**
 * Set subinterface pbb vtr enable/disable.
 * The CLI format is:
 *    set interface l2 pbb-tag-rewrite <interface> [disable | pop | push | translate_pbb_stag <outer_tag> dmac <address> smac <address> s_id <nn> [b_vlanid <nn>]]
 */
static clib_error_t *
int_l2_pbb_vtr (vlib_main_t * vm,
		unformat_input_t * input, vlib_cli_command_t * cmd)
{
  vnet_main_t *vnm = vnet_get_main ();
  clib_error_t *error = 0;
  u32 sw_if_index, tmp;
  u32 vtr_op = L2_VTR_DISABLED;
  u32 outer_tag = 0;
  u8 dmac[6];
  u8 smac[6];
  u8 dmac_set = 0, smac_set = 0;
  u16 b_vlanid = 0;
  u32 s_id = ~0;

  while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
    {
      if (unformat_user
	  (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
	;
      else if (unformat (input, "disable"))
	vtr_op = L2_VTR_DISABLED;
      else if (vtr_op == L2_VTR_DISABLED && unformat (input, "pop"))
	vtr_op = L2_VTR_POP_2;
      else if (vtr_op == L2_VTR_DISABLED && unformat (input, "push"))
	vtr_op = L2_VTR_PUSH_2;
      else if (vtr_op == L2_VTR_DISABLED
	       && unformat (input, "translate_pbb_stag %d", &outer_tag))
	vtr_op = L2_VTR_TRANSLATE_2_1;
      else if (unformat (input, "dmac %U", unformat_ethernet_address, dmac))
	dmac_set = 1;
      else if (unformat (input, "smac %U", unformat_ethernet_address, smac))
	smac_set = 1;
      else if (unformat (input, "b_vlanid %d", &tmp))
	b_vlanid = tmp;
      else if (unformat (input, "s_id %d", &s_id))
	;
      else
	{
	  error = clib_error_return (0,
				     "expecting [disable | pop | push | translate_pbb_stag <outer_tag>\n"
				     "dmac <address> smac <address> s_id <nn> [b_vlanid <nn>]]");
	  goto done;
	}
    }

  if ((vtr_op == L2_VTR_PUSH_2 || vtr_op == L2_VTR_TRANSLATE_2_1)
      && (!dmac_set || !smac_set || s_id == ~0))
    {
      error = clib_error_return (0,
				 "expecting dmac <address> smac <address> s_id <nn> [b_vlanid <nn>]");
      goto done;
    }

  if (l2pbb_configure
      (vm, vnm, sw_if_index, vtr_op, dmac, smac, b_vlanid, s_id, outer_tag))
    {
      error =
	clib_error_return (0,
			   "pbb tag rewrite is not compatible with interface");
      goto done;
    }

done:
  return error;
}

/* *INDENT-OFF* */
VLIB_CLI_COMMAND (int_l2_pbb_vtr_cli, static) = {
  .path = "set interface l2 pbb-tag-rewrite",
  .short_help = "set interface l2 pbb-tag-rewrite <interface> [disable | pop | push | translate_pbb_stag <outer_tag> dmac <address> smac <address> s_id <nn> [b_vlanid <nn>]]",
  .function = int_l2_pbb_vtr,
};
/* *INDENT-ON* */

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