classify: vpp packet tracer support
[vpp.git] / src / vlib / trace.c
index dcdb837..7ee1b63 100644 (file)
@@ -40,6 +40,8 @@
 #include <vlib/vlib.h>
 #include <vlib/threads.h>
 
+u8 *vnet_trace_dummy;
+
 /* Helper function for nodes which only trace buffer data. */
 void
 vlib_trace_frame_buffers_only (vlib_main_t * vm,
@@ -73,14 +75,14 @@ vlib_trace_frame_buffers_only (vlib_main_t * vm,
       if (b0->flags & VLIB_BUFFER_IS_TRACED)
        {
          t0 = vlib_add_trace (vm, node, b0, n_buffer_data_bytes_in_trace);
-         clib_memcpy (t0, b0->data + b0->current_data,
-                      n_buffer_data_bytes_in_trace);
+         clib_memcpy_fast (t0, b0->data + b0->current_data,
+                           n_buffer_data_bytes_in_trace);
        }
       if (b1->flags & VLIB_BUFFER_IS_TRACED)
        {
          t1 = vlib_add_trace (vm, node, b1, n_buffer_data_bytes_in_trace);
-         clib_memcpy (t1, b1->data + b1->current_data,
-                      n_buffer_data_bytes_in_trace);
+         clib_memcpy_fast (t1, b1->data + b1->current_data,
+                           n_buffer_data_bytes_in_trace);
        }
       from += 2;
       n_left -= 2;
@@ -99,8 +101,8 @@ vlib_trace_frame_buffers_only (vlib_main_t * vm,
       if (b0->flags & VLIB_BUFFER_IS_TRACED)
        {
          t0 = vlib_add_trace (vm, node, b0, n_buffer_data_bytes_in_trace);
-         clib_memcpy (t0, b0->data + b0->current_data,
-                      n_buffer_data_bytes_in_trace);
+         clib_memcpy_fast (t0, b0->data + b0->current_data,
+                           n_buffer_data_bytes_in_trace);
        }
       from += 1;
       n_left -= 1;
@@ -117,23 +119,19 @@ clear_trace_buffer (void)
   /* *INDENT-OFF* */
   foreach_vlib_main (
   ({
-    void *mainheap;
-
     tm = &this_vlib_main->trace_main;
-    mainheap = clib_mem_set_heap (this_vlib_main->heap_base);
 
-    tm->trace_active_hint = 0;
+    tm->trace_enable = 0;
 
     for (i = 0; i < vec_len (tm->trace_buffer_pool); i++)
       if (! pool_is_free_index (tm->trace_buffer_pool, i))
         vec_free (tm->trace_buffer_pool[i]);
     pool_free (tm->trace_buffer_pool);
-    clib_mem_set_heap (mainheap);
   }));
   /* *INDENT-ON* */
 }
 
-static u8 *
+u8 *
 format_vlib_trace (u8 * s, va_list * va)
 {
   vlib_main_t *vm = va_arg (*va, vlib_main_t *);
@@ -298,15 +296,11 @@ cli_show_trace_buffer (vlib_main_t * vm,
   /* *INDENT-OFF* */
   foreach_vlib_main (
   ({
-    void *mainheap;
-
     fmt = "------------------- Start of thread %d %s -------------------\n";
     s = format (s, fmt, index, vlib_worker_threads[index].name);
 
     tm = &this_vlib_main->trace_main;
 
-    mainheap = clib_mem_set_heap (this_vlib_main->heap_base);
-
     trace_apply_filter(this_vlib_main);
 
     traces = 0;
@@ -317,7 +311,6 @@ cli_show_trace_buffer (vlib_main_t * vm,
 
     if (vec_len (traces) == 0)
       {
-        clib_mem_set_heap (mainheap);
         s = format (s, "No packets in trace buffer\n");
         goto done;
       }
@@ -334,17 +327,12 @@ cli_show_trace_buffer (vlib_main_t * vm,
             goto done;
           }
 
-        clib_mem_set_heap (mainheap);
-
         s = format (s, "Packet %d\n%U\n\n", i + 1,
                          format_vlib_trace, vm, traces[i]);
-
-        mainheap = clib_mem_set_heap (this_vlib_main->heap_base);
       }
 
   done:
     vec_free (traces);
-    clib_mem_set_heap (mainheap);
 
     index++;
   }));
@@ -363,19 +351,32 @@ VLIB_CLI_COMMAND (show_trace_cli,static) = {
 };
 /* *INDENT-ON* */
 
+int vlib_enable_disable_pkt_trace_filter (int enable) __attribute__ ((weak));
+int
+vlib_enable_disable_pkt_trace_filter (int enable)
+{
+  return 0;
+}
+
 static clib_error_t *
 cli_add_trace_buffer (vlib_main_t * vm,
                      unformat_input_t * input, vlib_cli_command_t * cmd)
 {
   unformat_input_t _line_input, *line_input = &_line_input;
   vlib_trace_main_t *tm;
+  vlib_node_t *node;
   vlib_trace_node_t *tn;
   u32 node_index, add;
   u8 verbose = 0;
+  int filter = 0;
+  clib_error_t *error = 0;
 
   if (!unformat_user (input, unformat_line_input, line_input))
     return 0;
 
+  if (vnet_trace_dummy == 0)
+    vec_validate_aligned (vnet_trace_dummy, 2048, CLIB_CACHE_LINE_BYTES);
+
   while (unformat_check_input (line_input) != (uword) UNFORMAT_END_OF_INPUT)
     {
       if (unformat (line_input, "%U %d",
@@ -383,27 +384,52 @@ cli_add_trace_buffer (vlib_main_t * vm,
        ;
       else if (unformat (line_input, "verbose"))
        verbose = 1;
+      else if (unformat (line_input, "filter"))
+       filter = 1;
       else
-       return clib_error_create ("expected NODE COUNT, got `%U'",
-                                 format_unformat_error, line_input);
+       {
+         error = clib_error_create ("expected NODE COUNT, got `%U'",
+                                    format_unformat_error, line_input);
+         goto done;
+       }
+    }
+
+  node = vlib_get_node (vm, node_index);
+
+  if ((node->flags & VLIB_NODE_FLAG_TRACE_SUPPORTED) == 0)
+    {
+      error = clib_error_create ("node '%U' doesn't support per-node "
+                                "tracing. There may be another way to "
+                                "initiate trace on this node.",
+                                format_vlib_node_name, vm, node_index);
+      goto done;
+    }
+
+  if (filter)
+    {
+      if (vlib_enable_disable_pkt_trace_filter (1 /* enable */ ))
+       {
+         error = clib_error_create ("No packet trace filter configured...");
+         goto done;
+       }
     }
 
   /* *INDENT-OFF* */
   foreach_vlib_main ((
     {
-      void *oldheap;
       tm = &this_vlib_main->trace_main;
-      tm->trace_active_hint = 1;
       tm->verbose = verbose;
-      oldheap =
-       clib_mem_set_heap (this_vlib_main->heap_base);
       vec_validate (tm->nodes, node_index);
       tn = tm->nodes + node_index;
-      tn->limit += add; clib_mem_set_heap (oldheap);
+      tn->limit += add;
+      tm->trace_enable = 1;
     }));
   /* *INDENT-ON* */
 
-  return 0;
+done:
+  unformat_free (line_input);
+
+  return error;
 }
 
 /* *INDENT-OFF* */
@@ -414,7 +440,6 @@ VLIB_CLI_COMMAND (add_trace_cli,static) = {
 };
 /* *INDENT-ON* */
 
-
 /*
  * Configure a filter for packet traces.
  *
@@ -460,7 +485,6 @@ cli_filter_trace (vlib_main_t * vm,
   u32 filter_node_index;
   u32 filter_flag;
   u32 filter_count;
-  void *mainheap;
 
   if (unformat (input, "include %U %d",
                unformat_vlib_node, vm, &filter_node_index, &filter_count))
@@ -495,11 +519,10 @@ cli_filter_trace (vlib_main_t * vm,
 
     /*
      * Clear the trace limits to stop any in-progress tracing
-     * Prevents runaway trace allocations when the filter changes (or is removed)
+     * Prevents runaway trace allocations when the filter changes
+     * (or is removed)
      */
-    mainheap = clib_mem_set_heap (this_vlib_main->heap_base);
     vec_free (tm->nodes);
-    clib_mem_set_heap (mainheap);
   }));
   /* *INDENT-ON* */
 
@@ -518,6 +541,7 @@ static clib_error_t *
 cli_clear_trace_buffer (vlib_main_t * vm,
                        unformat_input_t * input, vlib_cli_command_t * cmd)
 {
+  vlib_enable_disable_pkt_trace_filter (0 /* enable */ );
   clear_trace_buffer ();
   return 0;
 }
@@ -536,6 +560,18 @@ vlib_trace_cli_reference (void)
 {
 }
 
+int
+vnet_is_packet_traced (vlib_buffer_t * b,
+                      u32 classify_table_index, int func)
+__attribute__ ((weak));
+
+int
+vnet_is_packet_traced (vlib_buffer_t * b, u32 classify_table_index, int func)
+{
+  clib_warning ("BUG: STUB called");
+  return 1;
+}
+
 /*
  * fd.io coding-style-patch-verification: ON
  *