bond: show trace causes a crash if the interface is deleted

For the debug image, if the interface is removed and the trace was
collected prior to the interface delete, show trace may cause a crash.
This is because vnet_get_sw_interface_name and vnet_get_sup_hw_interface
are not safe if the interface is deleted.

The fix is to use format_vnet_sw_if_index_name if all we need is to
get the interface name in the trace to display. It would show "DELETED"
which is better than a crash.

Change-Id: I912402d3e71592ece9f49d36c8a6b7af97f3b69e
Signed-off-by: Steven <sluong@cisco.com>
diff --git a/src/vnet/adj/adj_nbr.c b/src/vnet/adj/adj_nbr.c
index 97940da..dcf3cb1 100644
--- a/src/vnet/adj/adj_nbr.c
+++ b/src/vnet/adj/adj_nbr.c
@@ -981,10 +981,8 @@
                 format_ip46_address, &adj->sub_type.nbr.next_hop,
 		adj_proto_to_46(adj->ia_nh_proto));
     s = format (s, " %U",
-                format_vnet_sw_interface_name,
-                vnm,
-                vnet_get_sw_interface(vnm,
-                                      adj->rewrite_header.sw_if_index));
+                format_vnet_sw_if_index_name,
+                vnm, adj->rewrite_header.sw_if_index);
 
     return (s);
 }
diff --git a/src/vnet/bonding/cli.c b/src/vnet/bonding/cli.c
index 901032e..1768912 100644
--- a/src/vnet/bonding/cli.c
+++ b/src/vnet/bonding/cli.c
@@ -176,6 +176,8 @@
   vnet_hw_interface_t *hw;
   u32 *sif_sw_if_index;
   u32 thread_index;
+  u32 **s_list = 0;
+  u32 i;
 
   hw = vnet_get_sup_hw_interface (vnm, sw_if_index);
   if (hw == NULL || bond_dev_class.index != hw->dev_class_index)
@@ -185,11 +187,20 @@
 
   vec_foreach (sif_sw_if_index, bif->slaves)
   {
-    sif = bond_get_slave_by_sw_if_index (*sif_sw_if_index);
-    if (sif)
-      bond_delete_neighbor (vm, bif, sif);
+    vec_add1 (s_list, sif_sw_if_index);
   }
 
+  for (i = 0; i < vec_len (s_list); i++)
+    {
+      sif_sw_if_index = s_list[i];
+      sif = bond_get_slave_by_sw_if_index (*sif_sw_if_index);
+      if (sif)
+	bond_delete_neighbor (vm, bif, sif);
+    }
+
+  if (s_list)
+    vec_free (s_list);
+
   /* bring down the interface */
   vnet_hw_interface_set_flags (vnm, bif->hw_if_index, 0);
   vnet_sw_interface_set_flags (vnm, bif->sw_if_index, 0);
diff --git a/src/vnet/bonding/node.c b/src/vnet/bonding/node.c
index 4deec82..b831d40 100644
--- a/src/vnet/bonding/node.c
+++ b/src/vnet/bonding/node.c
@@ -50,15 +50,14 @@
   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
   bond_packet_trace_t *t = va_arg (*args, bond_packet_trace_t *);
-  vnet_hw_interface_t *hw, *hw1;
-  vnet_main_t *vnm = vnet_get_main ();
 
-  hw = vnet_get_sup_hw_interface (vnm, t->sw_if_index);
-  hw1 = vnet_get_sup_hw_interface (vnm, t->bond_sw_if_index);
-  s = format (s, "src %U, dst %U, %s -> %s",
+  s = format (s, "src %U, dst %U, %U -> %U",
 	      format_ethernet_address, t->ethernet.src_address,
 	      format_ethernet_address, t->ethernet.dst_address,
-	      hw->name, hw1->name);
+	      format_vnet_sw_if_index_name, vnet_get_main (),
+	      t->sw_if_index,
+	      format_vnet_sw_if_index_name, vnet_get_main (),
+	      t->bond_sw_if_index);
 
   return s;
 }
@@ -382,6 +381,17 @@
 	  sif = bond_get_slave_by_sw_if_index (sw_if_index);
 	  bond_sw_if_index_rewrite (vm, node, sif, eth, b0);
 
+	  if (PREDICT_FALSE (n_trace > 0))
+	    {
+	      vlib_trace_buffer (vm, node, next0, b0, 0 /* follow_chain */ );
+	      vlib_set_trace_count (vm, node, --n_trace);
+	      t0 = vlib_add_trace (vm, node, b0, sizeof (*t0));
+	      t0->ethernet = *eth;
+	      t0->sw_if_index = sw_if_index;
+	      t0->bond_sw_if_index = vnet_buffer (b0)->sw_if_index[VLIB_RX];
+
+	    }
+
 	  VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b0);
 
 	  /* verify speculative enqueue, maybe switch current next frame */