summaryrefslogtreecommitdiffstats
path: root/src/vnet
diff options
context:
space:
mode:
authorNeale Ranns <neale.ranns@cisco.com>2017-01-05 01:01:47 -0800
committerJohn Lo <loj@cisco.com>2017-01-05 15:10:05 +0000
commitc0790cfef0bd1c56f4c75dc4f959584148386258 (patch)
tree08abc77cb599b15f1ea9d80bdf44bf14855a1360 /src/vnet
parentce1aae4e88d8ed2c73e8213115cf9f3a4e37370f (diff)
FIB memory leaks (VPP-578)
1) vec_free the fe_srcs of a fib_entry_t when the fib_entry_t is itself reed 2) in the load-balance fixup if a drop path is required add this to a new vector of next-hops 'fixed_nhs'. This vector is managed by the load-balance function. The caller continues to manage its own set. The function is now const implying that the caller is safe to assume the next-hops do not change. Change-Id: I0f29203ee16b9a270f40edf237488fa99ba65320 Signed-off-by: Neale Ranns <nranns@cisco.com> Signed-off-by: Neale Ranns <neale.ranns@cisco.com>
Diffstat (limited to 'src/vnet')
-rw-r--r--src/vnet/dpo/load_balance.c33
-rw-r--r--src/vnet/dpo/load_balance.h2
-rw-r--r--src/vnet/fib/fib_entry.c1
-rw-r--r--src/vnet/fib/fib_entry_src.c8
-rw-r--r--src/vnet/fib/fib_path_list.c26
-rw-r--r--src/vnet/fib/fib_path_list.h2
6 files changed, 48 insertions, 24 deletions
diff --git a/src/vnet/dpo/load_balance.c b/src/vnet/dpo/load_balance.c
index a244776ffb8..e70a7a306e1 100644
--- a/src/vnet/dpo/load_balance.c
+++ b/src/vnet/dpo/load_balance.c
@@ -279,8 +279,8 @@ load_balance_get_bucket (index_t lbi,
}
static int
-next_hop_sort_by_weight (load_balance_path_t * n1,
- load_balance_path_t * n2)
+next_hop_sort_by_weight (const load_balance_path_t * n1,
+ const load_balance_path_t * n2)
{
return ((int) n1->path_weight - (int) n2->path_weight);
}
@@ -289,7 +289,7 @@ next_hop_sort_by_weight (load_balance_path_t * n1,
with weights corresponding to the number of adjacencies for each next hop.
Returns number of adjacencies in block. */
u32
-ip_multipath_normalize_next_hops (load_balance_path_t * raw_next_hops,
+ip_multipath_normalize_next_hops (const load_balance_path_t * raw_next_hops,
load_balance_path_t ** normalized_next_hops,
u32 *sum_weight_in,
f64 multipath_next_hop_error_tolerance)
@@ -409,23 +409,25 @@ done:
}
static load_balance_path_t *
-load_balance_multipath_next_hop_fixup (load_balance_path_t *nhs,
+load_balance_multipath_next_hop_fixup (const load_balance_path_t *nhs,
dpo_proto_t drop_proto)
{
if (0 == vec_len(nhs))
{
- load_balance_path_t *nh;
+ load_balance_path_t *new_nhs = NULL, *nh;
/*
* we need something for the load-balance. so use the drop
*/
- vec_add2(nhs, nh, 1);
+ vec_add2(new_nhs, nh, 1);
nh->path_weight = 1;
dpo_copy(&nh->path_dpo, drop_dpo_get(drop_proto));
+
+ return (new_nhs);
}
- return (nhs);
+ return (NULL);
}
/*
@@ -467,11 +469,11 @@ load_balance_set_n_buckets (load_balance_t *lb,
void
load_balance_multipath_update (const dpo_id_t *dpo,
- load_balance_path_t * raw_next_hops,
+ const load_balance_path_t * raw_nhs,
load_balance_flags_t flags)
{
- u32 sum_of_weights,n_buckets, ii;
- load_balance_path_t * nh, * nhs;
+ load_balance_path_t *nh, *nhs, *fixed_nhs;
+ u32 sum_of_weights, n_buckets, ii;
index_t lbmi, old_lbmi;
load_balance_t *lb;
dpo_id_t *tmp_dpo;
@@ -480,16 +482,16 @@ load_balance_multipath_update (const dpo_id_t *dpo,
ASSERT(DPO_LOAD_BALANCE == dpo->dpoi_type);
lb = load_balance_get(dpo->dpoi_index);
- raw_next_hops =
- load_balance_multipath_next_hop_fixup(raw_next_hops,
- lb->lb_proto);
+ fixed_nhs = load_balance_multipath_next_hop_fixup(raw_nhs, lb->lb_proto);
n_buckets =
- ip_multipath_normalize_next_hops(raw_next_hops,
+ ip_multipath_normalize_next_hops((NULL == fixed_nhs ?
+ raw_nhs :
+ fixed_nhs),
&nhs,
&sum_of_weights,
multipath_next_hop_error_tolerance);
- ASSERT (n_buckets >= vec_len (raw_next_hops));
+ ASSERT (n_buckets >= vec_len (raw_nhs));
/*
* Save the old load-balance map used, and get a new one if required.
@@ -694,6 +696,7 @@ load_balance_multipath_update (const dpo_id_t *dpo,
dpo_reset(&nh->path_dpo);
}
vec_free(nhs);
+ vec_free(fixed_nhs);
load_balance_map_unlock(old_lbmi);
}
diff --git a/src/vnet/dpo/load_balance.h b/src/vnet/dpo/load_balance.h
index dc6485e688a..1799653628d 100644
--- a/src/vnet/dpo/load_balance.h
+++ b/src/vnet/dpo/load_balance.h
@@ -159,7 +159,7 @@ extern index_t load_balance_create(u32 num_buckets,
flow_hash_config_t fhc);
extern void load_balance_multipath_update(
const dpo_id_t *dpo,
- load_balance_path_t * raw_next_hops,
+ const load_balance_path_t * raw_next_hops,
load_balance_flags_t flags);
extern void load_balance_set_bucket(index_t lbi,
diff --git a/src/vnet/fib/fib_entry.c b/src/vnet/fib/fib_entry.c
index 24b506379ac..3aa3632c596 100644
--- a/src/vnet/fib/fib_entry.c
+++ b/src/vnet/fib/fib_entry.c
@@ -230,6 +230,7 @@ fib_entry_last_lock_gone (fib_node_t *node)
ASSERT(0 == vec_len(fib_entry->fe_delegates));
vec_free(fib_entry->fe_delegates);
+ vec_free(fib_entry->fe_srcs);
pool_put(fib_entry_pool, fib_entry);
}
diff --git a/src/vnet/fib/fib_entry_src.c b/src/vnet/fib/fib_entry_src.c
index 060fac941d2..1fb040608b4 100644
--- a/src/vnet/fib/fib_entry_src.c
+++ b/src/vnet/fib/fib_entry_src.c
@@ -382,6 +382,14 @@ fib_entry_src_mk_lb (fib_entry_t *fib_entry,
.fct = fct,
};
+ /*
+ * As an optimisation we allocate the vector of next-hops to be sized
+ * equal to the maximum nuber of paths we will need, which is also the
+ * most likely number we will need, since in most cases the paths are 'up'.
+ */
+ vec_validate(ctx.next_hops, fib_path_list_get_n_paths(esrc->fes_pl));
+ vec_reset_length(ctx.next_hops);
+
lb_proto = fib_proto_to_dpo(fib_entry->fe_prefix.fp_proto);
fib_path_list_walk(esrc->fes_pl,
diff --git a/src/vnet/fib/fib_path_list.c b/src/vnet/fib/fib_path_list.c
index 5b35e9b87e7..db9d1af9e3f 100644
--- a/src/vnet/fib/fib_path_list.c
+++ b/src/vnet/fib/fib_path_list.c
@@ -365,10 +365,10 @@ fib_path_list_mk_lb (fib_path_list_t *path_list,
fib_forward_chain_type_t fct,
dpo_id_t *dpo)
{
- load_balance_path_t *hash_key;
+ load_balance_path_t *nhs;
fib_node_index_t *path_index;
- hash_key = NULL;
+ nhs = NULL;
if (!dpo_id_is_valid(dpo))
{
@@ -388,21 +388,20 @@ fib_path_list_mk_lb (fib_path_list_t *path_list,
*/
vec_foreach (path_index, path_list->fpl_paths)
{
- hash_key = fib_path_append_nh_for_multipath_hash(
- *path_index,
- fct,
- hash_key);
+ nhs = fib_path_append_nh_for_multipath_hash(*path_index,
+ fct,
+ nhs);
}
/*
* Path-list load-balances, which if used, would be shared and hence
* never need a load-balance map.
*/
- load_balance_multipath_update(dpo, hash_key, LOAD_BALANCE_FLAG_NONE);
+ load_balance_multipath_update(dpo, nhs, LOAD_BALANCE_FLAG_NONE);
FIB_PATH_LIST_DBG(path_list, "mk lb: %d", dpo->dpoi_index);
- vec_free(hash_key);
+ vec_free(nhs);
}
/**
@@ -592,6 +591,17 @@ fib_path_list_resolve (fib_path_list_t *path_list)
}
u32
+fib_path_list_get_n_paths (fib_node_index_t path_list_index)
+{
+ fib_path_list_t *path_list;
+
+ path_list = fib_path_list_get(path_list_index);
+
+ return (vec_len(path_list->fpl_paths));
+}
+
+
+u32
fib_path_list_get_resolving_interface (fib_node_index_t path_list_index)
{
fib_node_index_t *path_index;
diff --git a/src/vnet/fib/fib_path_list.h b/src/vnet/fib/fib_path_list.h
index 8bc1b20b6bf..f4f94a1b04a 100644
--- a/src/vnet/fib/fib_path_list.h
+++ b/src/vnet/fib/fib_path_list.h
@@ -104,6 +104,8 @@ extern fib_node_index_t fib_path_list_copy_and_path_remove(
fib_node_index_t pl_index,
fib_path_list_flags_t flags,
const fib_route_path_t *path);
+extern u32 fib_path_list_get_n_paths(fib_node_index_t pl_index);
+
extern void fib_path_list_contribute_forwarding(fib_node_index_t path_list_index,
fib_forward_chain_type_t type,
dpo_id_t *dpo);
616' href='#n616'>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 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066
/*
 * l2_fib.c : layer 2 forwarding table (aka mac table)
 *
 * 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/pg/pg.h>
#include <vnet/ethernet/ethernet.h>
#include <vlib/cli.h>

#include <vppinfra/error.h>
#include <vppinfra/hash.h>
#include <vnet/l2/l2_input.h>
#include <vnet/l2/l2_fib.h>
#include <vnet/l2/l2_learn.h>
#include <vnet/l2/l2_bd.h>

#include <vppinfra/bihash_template.c>

/**
 * @file
 * @brief Ethernet MAC Address FIB Table Management.
 *
 * The MAC Address forwarding table for bridge-domains is called the l2fib.
 * Entries are added automatically as part of mac learning, but MAC Addresses
 * entries can also be added manually.
 *
 */

typedef struct
{

  /* hash table */
  BVT (clib_bihash) mac_table;

  /* convenience variables */
  vlib_main_t *vlib_main;
  vnet_main_t *vnet_main;
} l2fib_main_t;

l2fib_main_t l2fib_main;

/** Format sw_if_index. If the value is ~0, use the text "N/A" */
u8 *
format_vnet_sw_if_index_name_with_NA (u8 * s, va_list * args)
{
  vnet_main_t *vnm = va_arg (*args, vnet_main_t *);
  u32 sw_if_index = va_arg (*args, u32);
  if (sw_if_index == ~0)
    return format (s, "N/A");
  else
    return format (s, "%U",
		   format_vnet_sw_interface_name, vnm,
		   vnet_get_sw_interface (vnm, sw_if_index));
}

void
l2fib_table_dump (u32 bd_index, l2fib_entry_key_t ** l2fe_key,
		  l2fib_entry_result_t ** l2fe_res)
{
  l2fib_main_t *msm = &l2fib_main;
  BVT (clib_bihash) * h = &msm->mac_table;
  clib_bihash_bucket_t *b;
  BVT (clib_bihash_value) * v;
  l2fib_entry_key_t key;
  l2fib_entry_result_t result;
  int i, j, k;

  for (i = 0; i < h->nbuckets; i++)
    {
      b = &h->buckets[i];
      if (b->offset == 0)
	continue;
      v = BV (clib_bihash_get_value) (h, b->offset);
      for (j = 0; j < (1 << b->log2_pages); j++)
	{
	  for (k = 0; k < BIHASH_KVP_PER_PAGE; k++)
	    {
	      if (v->kvp[k].key == ~0ULL && v->kvp[k].value == ~0ULL)
		continue;

	      key.raw = v->kvp[k].key;
	      result.raw = v->kvp[k].value;

	      if ((bd_index == ~0) || (bd_index == key.fields.bd_index))
		{
		  vec_add1 (*l2fe_key, key);
		  vec_add1 (*l2fe_res, result);
		}
	    }
	  v++;
	}
    }
}

/** Display the contents of the l2fib. */
static clib_error_t *
show_l2fib (vlib_main_t * vm,
	    unformat_input_t * input, vlib_cli_command_t * cmd)
{
  bd_main_t *bdm = &bd_main;
  l2fib_main_t *msm = &l2fib_main;
  l2_bridge_domain_t *bd_config;
  BVT (clib_bihash) * h = &msm->mac_table;
  clib_bihash_bucket_t *b;
  BVT (clib_bihash_value) * v;
  l2fib_entry_key_t key;
  l2fib_entry_result_t result;
  u32 first_entry = 1;
  u64 total_entries = 0;
  int i, j, k;
  u8 verbose = 0;
  u8 raw = 0;
  u32 bd_id, bd_index = ~0;
  u8 now = (u8) (vlib_time_now (vm) / 60);
  u8 *s = 0;

  if (unformat (input, "raw"))
    raw = 1;
  else if (unformat (input, "verbose"))
    verbose = 1;
  else if (unformat (input, "bd_index %d", &bd_index))
    verbose = 1;
  else if (unformat (input, "bd_id %d", &bd_id))
    {
      uword *p = hash_get (bdm->bd_index_by_bd_id, bd_id);
      if (p)
	{
	  verbose = 1;
	  bd_index = p[0];
	}
      else
	{
	  vlib_cli_output (vm, "no such bridge domain id");
	  return 0;
	}
    }

  for (i = 0; i < h->nbuckets; i++)
    {
      b = &h->buckets[i];
      if (b->offset == 0)
	continue;
      v = BV (clib_bihash_get_value) (h, b->offset);
      for (j = 0; j < (1 << b->log2_pages); j++)
	{
	  for (k = 0; k < BIHASH_KVP_PER_PAGE; k++)
	    {
	      if (v->kvp[k].key == ~0ULL && v->kvp[k].value == ~0ULL)
		continue;

	      if (verbose && first_entry)
		{
		  first_entry = 0;
		  vlib_cli_output (vm,
				   "%=19s%=7s%=7s%=8s%=9s%=7s%=7s%=5s%=30s",
				   "Mac-Address", "BD-Idx", "If-Idx",
				   "BSN-ISN", "Age(min)", "static", "filter",
				   "bvi", "Interface-Name");
		}

	      key.raw = v->kvp[k].key;
	      result.raw = v->kvp[k].value;

	      if (verbose
		  & ((bd_index >> 31) || (bd_index == key.fields.bd_index)))
		{
		  bd_config = vec_elt_at_index (l2input_main.bd_configs,
						key.fields.bd_index);

		  if (bd_config->mac_age && !result.fields.static_mac)
		    {
		      i16 delta = now - result.fields.timestamp;
		      delta += delta < 0 ? 256 : 0;
		      s = format (s, "%d", delta);
		    }
		  else
		    s = format (s, "-");

		  vlib_cli_output (vm,
				   "%=19U%=7d%=7d %3d/%-3d%=9v%=7s%=7s%=5s%=30U",
				   format_ethernet_address, key.fields.mac,
				   key.fields.bd_index,
				   result.fields.sw_if_index == ~0
				   ? -1 : result.fields.sw_if_index,
				   result.fields.sn.bd, result.fields.sn.swif,
				   s, result.fields.static_mac ? "*" : "-",
				   result.fields.filter ? "*" : "-",
				   result.fields.bvi ? "*" : "-",
				   format_vnet_sw_if_index_name_with_NA,
				   msm->vnet_main, result.fields.sw_if_index);
		  vec_reset_length (s);
		}
	      total_entries++;
	    }
	  v++;
	}
    }

  if (total_entries == 0)
    vlib_cli_output (vm, "no l2fib entries");
  else
    vlib_cli_output (vm,
		     "%lld l2fib entries with %d learned (or non-static) entries",
		     total_entries, l2learn_main.global_learn_count);

  if (raw)
    vlib_cli_output (vm, "Raw Hash Table:\n%U\n",
		     BV (format_bihash), h, 1 /* verbose */ );

  vec_free (s);
  return 0;
}

/*?
 * This command dispays the MAC Address entries of the L2 FIB table.
 * Output can be filtered to just get the number of MAC Addresses or display
 * each MAC Address for all bridge domains or just a single bridge domain.
 *
 * @cliexpar
 * Example of how to display the number of MAC Address entries in the L2
 * FIB table:
 * @cliexstart{show l2fib}
 * 3 l2fib entries
 * @cliexend
 * Example of how to display all the MAC Address entries in the L2
 * FIB table:
 * @cliexstart{show l2fib verbose}
 *     Mac Address     BD Idx           Interface           Index  static  filter  bvi  refresh  timestamp
 *  52:54:00:53:18:33    1      GigabitEthernet0/8/0.200      3       0       0     0      0         0
 *  52:54:00:53:18:55    1      GigabitEthernet0/8/0.200      3       1       0     0      0         0
 *  52:54:00:53:18:77    1                 N/A                -1      1       1     0      0         0
 * 3 l2fib entries
 * @cliexend
?*/
/* *INDENT-OFF* */
VLIB_CLI_COMMAND (show_l2fib_cli, static) = {
  .path = "show l2fib",
  .short_help = "show l2fib [verbose | bd_id <nn> | bd_index <nn> | raw]",
  .function = show_l2fib,
};
/* *INDENT-ON* */


/* Remove all entries from the l2fib */
void
l2fib_clear_table (void)
{
  l2fib_main_t *mp = &l2fib_main;

  /* Remove all entries */
  BV (clib_bihash_free) (&mp->mac_table);
  BV (clib_bihash_init) (&mp->mac_table, "l2fib mac table",
			 L2FIB_NUM_BUCKETS, L2FIB_MEMORY_SIZE);
  l2learn_main.global_learn_count = 0;
}

/** Clear all entries in L2FIB.
 * @TODO: Later we may want a way to remove only the non-static entries
 */
static clib_error_t *
clear_l2fib (vlib_main_t * vm,
	     unformat_input_t * input, vlib_cli_command_t * cmd)
{
  l2fib_clear_table ();
  return 0;
}

/*?
 * This command clears all the MAC Address entries from the L2 FIB table.
 *
 * @cliexpar
 * Example of how to clear the L2 FIB Table:
 * @cliexcmd{clear l2fib}
 * Example to show the L2 FIB Table has been cleared:
 * @cliexstart{show l2fib verbose}
 * no l2fib entries
 * @cliexend
?*/
/* *INDENT-OFF* */
VLIB_CLI_COMMAND (clear_l2fib_cli, static) = {
  .path = "clear l2fib",
  .short_help = "clear l2fib",
  .function = clear_l2fib,
};
/* *INDENT-ON* */

static inline l2fib_seq_num_t
l2fib_cur_seq_num (u32 bd_index, u32 sw_if_index)
{
  l2_input_config_t *int_config = l2input_intf_config (sw_if_index);
  l2_bridge_domain_t *bd_config = l2input_bd_config (bd_index);
  /* *INDENT-OFF* */
  return (l2fib_seq_num_t) {
    .swif = int_config->seq_num,
    .bd = bd_config->seq_num,
  };
  /* *INDENT-ON* */
}

/**
 * Add an entry to the l2fib.
 * If the entry already exists then overwrite it
 */
void
l2fib_add_entry (u64 mac, u32 bd_index,
		 u32 sw_if_index, u32 static_mac, u32 filter_mac, u32 bvi_mac)
{
  l2fib_entry_key_t key;
  l2fib_entry_result_t result;
  __attribute__ ((unused)) u32 bucket_contents;
  l2fib_main_t *mp = &l2fib_main;
  BVT (clib_bihash_kv) kv;

  /* set up key */
  key.raw = l2fib_make_key ((u8 *) & mac, bd_index);

  /* set up result */
  result.raw = 0;		/* clear all fields */
  result.fields.sw_if_index = sw_if_index;
  result.fields.static_mac = static_mac;
  result.fields.filter = filter_mac;
  result.fields.bvi = bvi_mac;
  if (!static_mac)
    result.fields.sn = l2fib_cur_seq_num (bd_index, sw_if_index);

  kv.key = key.raw;
  kv.value = result.raw;

  BV (clib_bihash_add_del) (&mp->mac_table, &kv, 1 /* is_add */ );

  /* increment counter if dynamically learned mac */
  if (result.fields.static_mac == 0)
    {
      l2learn_main.global_learn_count++;
    }
}

/**
 * Add an entry to the L2FIB.
 * The CLI format is:
 *    l2fib add <mac> <bd> <intf> [static] [bvi]
 *    l2fib add <mac> <bd> filter
 * Note that filter and bvi entries are always static
 */
static clib_error_t *
l2fib_add (vlib_main_t * vm,
	   unformat_input_t * input, vlib_cli_command_t * cmd)
{
  bd_main_t *bdm = &bd_main;
  vnet_main_t *vnm = vnet_get_main ();
  clib_error_t *error = 0;
  u64 mac;
  u32 bd_id;
  u32 bd_index;
  u32 sw_if_index = ~0;
  u32 filter_mac = 0;
  u32 static_mac = 0;
  u32 bvi_mac = 0;
  uword *p;

  if (!unformat_user (input, unformat_ethernet_address, &mac))
    {
      error = clib_error_return (0, "expected mac address `%U'",
				 format_unformat_error, input);
      goto done;
    }

  if (!unformat (input, "%d", &bd_id))
    {
      error = clib_error_return (0, "expected bridge domain ID `%U'",
				 format_unformat_error, input);
      goto done;
    }

  p = hash_get (bdm->bd_index_by_bd_id, bd_id);
  if (!p)
    {
      error = clib_error_return (0, "bridge domain ID %d invalid", bd_id);
      goto done;
    }
  bd_index = p[0];

  if (unformat (input, "filter"))
    {
      filter_mac = 1;
      static_mac = 1;

    }
  else
    {

      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;
	}
      if (unformat (input, "static"))
	{
	  static_mac = 1;
	}
      else if (unformat (input, "bvi"))
	{
	  bvi_mac = 1;
	  static_mac = 1;
	}
    }

  l2fib_add_entry (mac, bd_index, sw_if_index, static_mac, filter_mac,
		   bvi_mac);

done:
  return error;
}

/*?
 * This command adds a MAC Address entry to the L2 FIB table
 * of an existing bridge-domain. The MAC Address can be static
 * or dynamic. This command also allows a filter to be added,
 * such that packets with given MAC Addresses (source mac or
 * destination mac match) are dropped.
 *
 * @cliexpar
 * Example of how to add a dynamic MAC Address entry to the L2 FIB table
 * of a bridge-domain (where 200 is the bridge-domain-id):
 * @cliexcmd{l2fib add 52:54:00:53:18:33 200 GigabitEthernet0/8/0.200}
 * Example of how to add a static MAC Address entry to the L2 FIB table
 * of a bridge-domain (where 200 is the bridge-domain-id):
 * @cliexcmd{l2fib add 52:54:00:53:18:55 200 GigabitEthernet0/8/0.200 static}
 * Example of how to add a filter such that a packet with the given MAC
 * Address will be dropped in a given bridge-domain (where 200 is the
 * bridge-domain-id):
 * @cliexcmd{l2fib add 52:54:00:53:18:77 200 filter}
 * Example of show command of the provisioned MAC Addresses and filters:
 * @cliexstart{show l2fib verbose}
 *     Mac Address     BD Idx           Interface           Index  static  filter  bvi  refresh  timestamp
 *  52:54:00:53:18:33    1      GigabitEthernet0/8/0.200      3       0       0     0      0         0
 *  52:54:00:53:18:55    1      GigabitEthernet0/8/0.200      3       1       0     0      0         0
 *  52:54:00:53:18:77    1                 N/A                -1      1       1     0      0         0
 * 3 l2fib entries
 * @cliexend
?*/
/* *INDENT-OFF* */
VLIB_CLI_COMMAND (l2fib_add_cli, static) = {
  .path = "l2fib add",
  .short_help = "l2fib add <mac> <bridge-domain-id> filter | <intf> [static | bvi]",
  .function = l2fib_add,
};
/* *INDENT-ON* */


static clib_error_t *
l2fib_test_command_fn (vlib_main_t * vm,
		       unformat_input_t * input, vlib_cli_command_t * cmd)
{
  clib_error_t *error = 0;
  u64 mac, save_mac;
  u32 bd_index = 0;
  u32 sw_if_index = 8;
  u32 filter_mac = 0;
  u32 bvi_mac = 0;
  u32 is_add = 0;
  u32 is_del = 0;
  u32 is_check = 0;
  u32 count = 1;
  int mac_set = 0;
  int i;

  while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
    {
      if (unformat (input, "mac %U", unformat_ethernet_address, &mac))
	mac_set = 1;
      else if (unformat (input, "add"))
	is_add = 1;
      else if (unformat (input, "del"))
	is_del = 1;
      else if (unformat (input, "check"))
	is_check = 1;
      else if (unformat (input, "count %d", &count))
	;
      else
	break;
    }

  if (mac_set == 0)
    return clib_error_return (0, "mac not set");

  if (is_add == 0 && is_del == 0 && is_check == 0)
    return clib_error_return (0,
			      "noop: pick at least one of (add,del,check)");

  save_mac = mac;

  if (is_add)
    {
      for (i = 0; i < count; i++)
	{
	  u64 tmp;
	  l2fib_add_entry (mac, bd_index, sw_if_index, mac,
			   filter_mac, bvi_mac);
	  tmp = clib_net_to_host_u64 (mac);
	  tmp >>= 16;
	  tmp++;
	  tmp <<= 16;
	  mac = clib_host_to_net_u64 (tmp);
	}
    }

  if (is_check)
    {
      BVT (clib_bihash_kv) kv;
      l2fib_main_t *mp = &l2fib_main;

      mac = save_mac;

      for (i = 0; i < count; i++)
	{
	  u64 tmp;
	  kv.key = l2fib_make_key ((u8 *) & mac, bd_index);
	  if (BV (clib_bihash_search) (&mp->mac_table, &kv, &kv))
	    {
	      clib_warning ("key %U AWOL", format_ethernet_address, &mac);
	      break;
	    }
	  tmp = clib_net_to_host_u64 (mac);
	  tmp >>= 16;
	  tmp++;
	  tmp <<= 16;
	  mac = clib_host_to_net_u64 (tmp);
	}
    }

  if (is_del)
    {
      for (i = 0; i < count; i++)
	{
	  u64 tmp;

	  l2fib_del_entry (mac, bd_index);

	  tmp = clib_net_to_host_u64 (mac);
	  tmp >>= 16;
	  tmp++;
	  tmp <<= 16;
	  mac = clib_host_to_net_u64 (tmp);
	}
    }

  return error;
}

/*?
 * The set of '<em>test l2fib</em>' commands allow the L2 FIB table of the default
 * bridge domain (bridge-domain-id of 0) to be modified.
 *
 * @cliexpar
 * @parblock
 * Example of how to add a set of 4 sequential MAC Address entries to L2
 * FIB table of the default bridge-domain:
 * @cliexcmd{test l2fib add mac 52:54:00:53:00:00 count 4}
 *
 * Show the set of 4 sequential MAC Address entries that were added:
 * @cliexstart{show l2fib verbose}
 *     Mac Address     BD Idx           Interface           Index  static  filter  bvi  refresh  timestamp
 * 52:54:00:53:00:00    0       GigabitEthernet0/8/0.300     8       0       0     0      0         0
 * 52:54:00:53:00:01    0       GigabitEthernet0/8/0.300     8       0       0     0      0         0
 * 52:54:00:53:00:03    0       GigabitEthernet0/8/0.300     8       0       0     0      0         0
 * 52:54:00:53:00:02    0       GigabitEthernet0/8/0.300     8       0       0     0      0         0
 * 4 l2fib entries
 * @cliexend
 *
 * Example of how to check that the set of 4 sequential MAC Address
 * entries were added to L2 FIB table of the default
 * bridge-domain. Used a count of 5 to produce an error:
 *
 * @cliexcmd{test l2fib check mac 52:54:00:53:00:00 count 5}
 * The output of the check command is in the log files. Log file
 * location may vary based on your OS and Version:
 *
 * <b><em># tail -f /var/log/messages | grep l2fib_test_command_fn</em></b>
 *
 * Sep  7 17:15:24 localhost vnet[4952]: l2fib_test_command_fn:446: key 52:54:00:53:00:04 AWOL
 *
 * Example of how to delete a set of 4 sequential MAC Address entries
 * from L2 FIB table of the default bridge-domain:
 * @cliexcmd{test l2fib del mac 52:54:00:53:00:00 count 4}
 * @endparblock
?*/
/* *INDENT-OFF* */
VLIB_CLI_COMMAND (l2fib_test_command, static) = {
  .path = "test l2fib",
  .short_help = "test l2fib [add|del|check] mac <base-addr> count <nn>",
  .function = l2fib_test_command_fn,
};
/* *INDENT-ON* */


/**
 * Delete an entry from the l2fib.
 * Return 0 if the entry was deleted, or 1 if it was not found
 */
static u32
l2fib_del_entry_by_key (u64 raw_key)
{

  l2fib_entry_result_t result;
  l2fib_main_t *mp = &l2fib_main;
  BVT (clib_bihash_kv) kv;

  /* set up key */
  kv.key = raw_key;

  if (BV (clib_bihash_search) (&mp->mac_table, &kv, &kv))
    return 1;

  result.raw = kv.value;

  /* decrement counter if dynamically learned mac */
  if (result.fields.static_mac == 0)
    {
      if (l2learn_main.global_learn_count > 0)
	{
	  l2learn_main.global_learn_count--;
	}
    }

  /* Remove entry from hash table */
  BV (clib_bihash_add_del) (&mp->mac_table, &kv, 0 /* is_add */ );
  return 0;
}

/**
 * Delete an entry from the l2fib.
 * Return 0 if the entry was deleted, or 1 if it was not found
 */
u32
l2fib_del_entry (u64 mac, u32 bd_index)
{
  return l2fib_del_entry_by_key (l2fib_make_key ((u8 *) & mac, bd_index));
}

/**
 * Delete an entry from the L2FIB.
 * The CLI format is:
 *    l2fib del <mac> <bd-id>
 */
static clib_error_t *
l2fib_del (vlib_main_t * vm,
	   unformat_input_t * input, vlib_cli_command_t * cmd)
{
  bd_main_t *bdm = &bd_main;
  clib_error_t *error = 0;
  u64 mac;
  u32 bd_id;
  u32 bd_index;
  uword *p;

  if (!unformat_user (input, unformat_ethernet_address, &mac))
    {
      error = clib_error_return (0, "expected mac address `%U'",
				 format_unformat_error, input);
      goto done;
    }

  if (!unformat (input, "%d", &bd_id))
    {
      error = clib_error_return (0, "expected bridge domain ID `%U'",
				 format_unformat_error, input);
      goto done;
    }

  p = hash_get (bdm->bd_index_by_bd_id, bd_id);
  if (!p)
    {
      error = clib_error_return (0, "bridge domain ID %d invalid", bd_id);
      goto done;
    }
  bd_index = p[0];

  /* Delete the entry */
  if (l2fib_del_entry (mac, bd_index))
    {
      error = clib_error_return (0, "mac entry not found");
      goto done;
    }

done:
  return error;
}

/*?
 * This command deletes an existing MAC Address entry from the L2 FIB
 * table of an existing bridge-domain.
 *
 * @cliexpar
 * Example of how to delete a MAC Address entry from the L2 FIB table of a bridge-domain (where 200 is the bridge-domain-id):
 * @cliexcmd{l2fib del 52:54:00:53:18:33 200}
?*/
/* *INDENT-OFF* */
VLIB_CLI_COMMAND (l2fib_del_cli, static) = {
  .path = "l2fib del",
  .short_help = "l2fib del <mac> <bridge-domain-id>",
  .function = l2fib_del,
};
/* *INDENT-ON* */

/**
    Kick off ager to scan MACs to age/delete MAC entries
*/
void
l2fib_start_ager_scan (vlib_main_t * vm)
{
  l2_bridge_domain_t *bd_config;
  int enable = 0;

  /* check if there is at least one bd with mac aging enabled */
  vec_foreach (bd_config, l2input_main.bd_configs)
    if (bd_config->bd_id != ~0 && bd_config->mac_age != 0)
    enable = 1;

  vlib_process_signal_event (vm, l2fib_mac_age_scanner_process_node.index,
			     enable ? L2_MAC_AGE_PROCESS_EVENT_START :
			     L2_MAC_AGE_PROCESS_EVENT_ONE_PASS, 0);
}

/**
    Flush all non static MACs from an interface
*/
void
l2fib_flush_int_mac (vlib_main_t * vm, u32 sw_if_index)
{
  l2_input_config_t *int_config = l2input_intf_config (sw_if_index);
  int_config->seq_num += 1;
  l2fib_start_ager_scan (vm);
}

/**
    Flush all non static MACs in a bridge domain
*/
void
l2fib_flush_bd_mac (vlib_main_t * vm, u32 bd_index)
{
  l2_bridge_domain_t *bd_config = l2input_bd_config (bd_index);
  bd_config->seq_num += 1;
  l2fib_start_ager_scan (vm);
}

/**
    Flush all non static MACs - flushes all valid BDs
*/
void
l2fib_flush_all_mac (vlib_main_t * vm)
{
  l2_bridge_domain_t *bd_config;
  vec_foreach (bd_config, l2input_main.bd_configs)
    if (bd_is_valid (bd_config))
    bd_config->seq_num += 1;

  l2fib_start_ager_scan (vm);
}


/**
    Flush MACs, except static ones, associated with an interface
    The CLI format is:
    l2fib flush-mac interface <if-name>
*/
static clib_error_t *
l2fib_flush_mac_int (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;

  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;
    }

  l2fib_flush_int_mac (vm, sw_if_index);

done:
  return error;
}

/**
    Flush all MACs, except static ones
    The CLI format is:
    l2fib flush-mac all
*/
static clib_error_t *
l2fib_flush_mac_all (vlib_main_t * vm,
		     unformat_input_t * input, vlib_cli_command_t * cmd)
{
  l2fib_flush_all_mac (vm);
  return 0;
}

/*?
 * This command kick off ager to delete all existing MAC Address entries,
 * except static ones, associated with an interface from the L2 FIB table.
 *
 * @cliexpar
 * Example of how to flush MAC Address entries learned on an interface from the L2 FIB table:
 * @cliexcmd{l2fib flush-mac interface GigabitEthernet2/1/0}
?*/
/* *INDENT-OFF* */
VLIB_CLI_COMMAND (l2fib_flush_mac_all_cli, static) = {
  .path = "l2fib flush-mac all",
  .short_help = "l2fib flush-mac all",
  .function = l2fib_flush_mac_all,
};
/* *INDENT-ON* */

/*?
 * This command kick off ager to delete all existing MAC Address entries,
 * except static ones, associated with an interface from the L2 FIB table.
 *
 * @cliexpar
 * Example of how to flush MAC Address entries learned on an interface from the L2 FIB table:
 * @cliexcmd{l2fib flush-mac interface GigabitEthernet2/1/0}
?*/
/* *INDENT-OFF* */
VLIB_CLI_COMMAND (l2fib_flush_mac_int_cli, static) = {
  .path = "l2fib flush-mac interface",
  .short_help = "l2fib flush-mac interface <if-name>",
  .function = l2fib_flush_mac_int,
};
/* *INDENT-ON* */

/**
    Flush bridge-domain MACs except static ones.
    The CLI format is:
    l2fib flush-mac bridge-domain <bd-id>
*/
static clib_error_t *
l2fib_flush_mac_bd (vlib_main_t * vm,
		    unformat_input_t * input, vlib_cli_command_t * cmd)
{
  bd_main_t *bdm = &bd_main;
  clib_error_t *error = 0;
  u32 bd_index, bd_id;
  uword *p;

  if (!unformat (input, "%d", &bd_id))
    {
      error = clib_error_return (0, "expecting bridge-domain id but got `%U'",
				 format_unformat_error, input);
      goto done;
    }

  p = hash_get (bdm->bd_index_by_bd_id, bd_id);
  if (p)
    bd_index = *p;
  else
    return clib_error_return (0, "No such bridge domain %d", bd_id);

  l2fib_flush_bd_mac (vm, bd_index);

done:
  return error;
}

/*?
 * This command kick off ager to delete all existing MAC Address entries,
 * except static ones, in a bridge domain from the L2 FIB table.
 *
 * @cliexpar
 * Example of how to flush MAC Address entries learned in a bridge domain from the L2 FIB table:
 * @cliexcmd{l2fib flush-mac bridge-domain 1000}
?*/
/* *INDENT-OFF* */
VLIB_CLI_COMMAND (l2fib_flush_mac_bd_cli, static) = {
  .path = "l2fib flush-mac bridge-domain",
  .short_help = "l2fib flush-mac bridge-domain <bd-id>",
  .function = l2fib_flush_mac_bd,
};
/* *INDENT-ON* */

clib_error_t *
l2fib_sw_interface_up_down (vnet_main_t * vnm, u32 sw_if_index, u32 flags)
{
  l2_input_config_t *config = l2input_intf_config (sw_if_index);
  if ((flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) == 0 && config->bridge)
    l2fib_flush_int_mac (vnm->vlib_main, sw_if_index);
  return 0;
}

VNET_SW_INTERFACE_ADMIN_UP_DOWN_FUNCTION (l2fib_sw_interface_up_down);

BVT (clib_bihash) * get_mac_table (void)
{
  l2fib_main_t *mp = &l2fib_main;
  return &mp->mac_table;
}

static uword
l2fib_mac_age_scanner_process (vlib_main_t * vm, vlib_node_runtime_t * rt,
			       vlib_frame_t * f)
{
  uword event_type, *event_data = 0;
  l2fib_main_t *msm = &l2fib_main;
  bool enabled = 0;
  f64 start_time, last_run_duration = 0, t;

  while (1)
    {
      if (enabled)
	vlib_process_wait_for_event_or_clock (vm, 60 - last_run_duration);
      else
	vlib_process_wait_for_event (vm);

      event_type = vlib_process_get_events (vm, &event_data);
      vec_reset_length (event_data);

      switch (event_type)
	{
	case ~0:
	  break;
	case L2_MAC_AGE_PROCESS_EVENT_START:
	  enabled = 1;
	  break;
	case L2_MAC_AGE_PROCESS_EVENT_STOP:
	  enabled = 0;
	  continue;
	case L2_MAC_AGE_PROCESS_EVENT_ONE_PASS:
	  enabled = 0;
	  break;
	default:
	  ASSERT (0);
	}
      last_run_duration = start_time = vlib_time_now (vm);

      BVT (clib_bihash) * h = &msm->mac_table;
      int i, j, k;
      for (i = 0; i < h->nbuckets; i++)
	{
	  /* Allow no more than 10us without a pause */
	  t = vlib_time_now (vm);
	  if (t > start_time + 10e-6)
	    {
	      vlib_process_suspend (vm, 100e-6);	/* suspend for 100 us */
	      start_time = vlib_time_now (vm);
	    }

	  if (i < (h->nbuckets - 3))
	    {
	      clib_bihash_bucket_t *b = &h->buckets[i + 3];
	      CLIB_PREFETCH (b, CLIB_CACHE_LINE_BYTES, LOAD);
	      b = &h->buckets[i + 1];
	      if (b->offset)
		{
		  BVT (clib_bihash_value) * v =
		    BV (clib_bihash_get_value) (h, b->offset);
		  CLIB_PREFETCH (v, CLIB_CACHE_LINE_BYTES, LOAD);
		}
	    }

	  clib_bihash_bucket_t *b = &h->buckets[i];
	  if (b->offset == 0)
	    continue;
	  BVT (clib_bihash_value) * v =
	    BV (clib_bihash_get_value) (h, b->offset);
	  for (j = 0; j < (1 << b->log2_pages); j++)
	    {
	      for (k = 0; k < BIHASH_KVP_PER_PAGE; k++)
		{
		  if (v->kvp[k].key == ~0ULL && v->kvp[k].value == ~0ULL)
		    continue;

		  l2fib_entry_key_t key = {.raw = v->kvp[k].key };
		  l2fib_entry_result_t result = {.raw = v->kvp[k].value };

		  if (result.fields.static_mac)
		    continue;

		  u32 bd_index = key.fields.bd_index;
		  u32 sw_if_index = result.fields.sw_if_index;
		  u16 sn = l2fib_cur_seq_num (bd_index, sw_if_index).as_u16;
		  if (result.fields.sn.as_u16 != sn)
		    {
		      l2fib_del_entry_by_key (key.raw);
		      continue;
		    }
		  l2_bridge_domain_t *bd_config =
		    vec_elt_at_index (l2input_main.bd_configs, bd_index);

		  if (bd_config->mac_age == 0)
		    continue;

		  i16 delta =
		    (u8) (start_time / 60) - result.fields.timestamp;
		  delta += delta < 0 ? 256 : 0;

		  if (delta > bd_config->mac_age)
		    l2fib_del_entry_by_key (key.raw);
		}
	      v++;
	    }
	}
      last_run_duration = vlib_time_now (vm) - last_run_duration;
    }
  return 0;
}

/* *INDENT-OFF* */
VLIB_REGISTER_NODE (l2fib_mac_age_scanner_process_node) = {
    .function = l2fib_mac_age_scanner_process,
    .type = VLIB_NODE_TYPE_PROCESS,
    .name = "l2fib-mac-age-scanner-process",
};
/* *INDENT-ON* */

clib_error_t *
l2fib_init (vlib_main_t * vm)
{
  l2fib_main_t *mp = &l2fib_main;
  l2fib_entry_key_t test_key;
  u8 test_mac[6];

  mp->vlib_main = vm;
  mp->vnet_main = vnet_get_main ();

  /* Create the hash table  */
  BV (clib_bihash_init) (&mp->mac_table, "l2fib mac table",
			 L2FIB_NUM_BUCKETS, L2FIB_MEMORY_SIZE);

  /* verify the key constructor is good, since it is endian-sensitive */
  memset (test_mac, 0, sizeof (test_mac));
  test_mac[0] = 0x11;
  test_key.raw = 0;
  test_key.raw = l2fib_make_key ((u8 *) & test_mac, 0x1234);
  ASSERT (test_key.fields.mac[0] == 0x11);
  ASSERT (test_key.fields.bd_index == 0x1234);

  return 0;
}

VLIB_INIT_FUNCTION (l2fib_init);

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