classify: add pcap/trace classfier mgmt API calls
[vpp.git] / src / vlib / trace_funcs.h
index 257c3a3..357079c 100644 (file)
@@ -40,7 +40,7 @@
 #ifndef included_vlib_trace_funcs_h
 #define included_vlib_trace_funcs_h
 
-extern u8 *vnet_trace_dummy;
+extern u8 *vnet_trace_placeholder;
 
 always_inline void
 vlib_validate_trace (vlib_trace_main_t * tm, vlib_buffer_t * b)
@@ -49,17 +49,21 @@ vlib_validate_trace (vlib_trace_main_t * tm, vlib_buffer_t * b)
                               vlib_buffer_get_trace_index (b)));
 }
 
-void vlib_add_handoff_trace (vlib_main_t * vm, vlib_buffer_t * b);
+int vlib_add_handoff_trace (vlib_main_t * vm, vlib_buffer_t * b);
 
 always_inline void *
-vlib_add_trace (vlib_main_t * vm,
-               vlib_node_runtime_t * r, vlib_buffer_t * b, u32 n_data_bytes)
+vlib_add_trace_inline (vlib_main_t * vm,
+                      vlib_node_runtime_t * r, vlib_buffer_t * b,
+                      u32 n_data_bytes)
 {
   vlib_trace_main_t *tm = &vm->trace_main;
   vlib_trace_header_t *h;
   u32 n_data_words;
 
-  ASSERT (vnet_trace_dummy);
+  ASSERT (vnet_trace_placeholder);
+
+  if (PREDICT_FALSE ((b->flags & VLIB_BUFFER_IS_TRACED) == 0))
+    return vnet_trace_placeholder;
 
   if (PREDICT_FALSE (tm->add_trace_callback != 0))
     {
@@ -70,13 +74,14 @@ vlib_add_trace (vlib_main_t * vm,
     }
   else if (PREDICT_FALSE (tm->trace_enable == 0))
     {
-      ASSERT (vec_len (vnet_trace_dummy) >= n_data_bytes + sizeof (*h));
-      return vnet_trace_dummy;
+      ASSERT (vec_len (vnet_trace_placeholder) >= n_data_bytes + sizeof (*h));
+      return vnet_trace_placeholder;
     }
 
   /* Are we trying to trace a handoff case? */
   if (PREDICT_FALSE (vlib_buffer_get_trace_thread (b) != vm->thread_index))
-    vlib_add_handoff_trace (vm, b);
+    if (PREDICT_FALSE (!vlib_add_handoff_trace (vm, b)))
+      return vnet_trace_placeholder;
 
   vlib_validate_trace (tm, b);
 
@@ -92,6 +97,11 @@ vlib_add_trace (vlib_main_t * vm,
   return h->data;
 }
 
+/* Non-inline (typical use-case) version of the above */
+void *vlib_add_trace (vlib_main_t * vm,
+                     vlib_node_runtime_t * r, vlib_buffer_t * b,
+                     u32 n_data_bytes);
+
 always_inline vlib_trace_header_t *
 vlib_trace_header_next (vlib_trace_header_t * h)
 {
@@ -118,9 +128,17 @@ vlib_trace_next_frame (vlib_main_t * vm,
 }
 
 void trace_apply_filter (vlib_main_t * vm);
+int vnet_is_packet_traced (vlib_buffer_t * b,
+                          u32 classify_table_index, int func);
 
-/* Mark buffer as traced and allocate trace buffer. */
-always_inline void
+
+/*
+ * Mark buffer as traced and allocate trace buffer.
+ * return 1 if the buffer is successfully traced, 0 if not
+ * A buffer might not be traced if tracing is off or if the packet did not
+ * match the filter.
+ */
+always_inline __clib_warn_unused_result int
 vlib_trace_buffer (vlib_main_t * vm,
                   vlib_node_runtime_t * r,
                   u32 next_index, vlib_buffer_t * b, int follow_chain)
@@ -129,7 +147,17 @@ vlib_trace_buffer (vlib_main_t * vm,
   vlib_trace_header_t **h;
 
   if (PREDICT_FALSE (tm->trace_enable == 0))
-    return;
+    return 0;
+
+  /* Classifier filter in use? */
+  if (PREDICT_FALSE (vlib_global_main.trace_filter.trace_filter_enable))
+    {
+      /* See if we're supposed to trace this packet... */
+      if (vnet_is_packet_traced
+         (b, vlib_global_main.trace_filter.classify_table_index,
+          0 /* full classify */ ) != 1)
+       return 0;
+    }
 
   /*
    * Apply filter to existing traces to keep number of allocated traces low.
@@ -156,6 +184,8 @@ vlib_trace_buffer (vlib_main_t * vm,
        (vm->thread_index, h - tm->trace_buffer_pool);
     }
   while (follow_chain && (b = vlib_get_next_buffer (vm, b)));
+
+  return 1;
 }
 
 always_inline void
@@ -172,15 +202,13 @@ vlib_get_trace_count (vlib_main_t * vm, vlib_node_runtime_t * rt)
 {
   vlib_trace_main_t *tm = &vm->trace_main;
   vlib_trace_node_t *tn;
-  int n;
 
   if (rt->node_index >= vec_len (tm->nodes))
     return 0;
   tn = tm->nodes + rt->node_index;
-  n = tn->limit - tn->count;
-  ASSERT (n >= 0);
+  ASSERT (tn->count <= tn->limit);
 
-  return n;
+  return tn->limit - tn->count;
 }
 
 always_inline void