Break out the broom for some cleanup work
[vpp.git] / src / vlib / main.c
index 552e693..b6006e8 100644 (file)
@@ -91,10 +91,11 @@ vlib_frame_find_magic (vlib_frame_t * f, vlib_node_t * node)
   return p;
 }
 
-static vlib_frame_size_t *
+static inline vlib_frame_size_t *
 get_frame_size_info (vlib_node_main_t * nm,
                     u32 n_scalar_bytes, u32 n_vector_bytes)
 {
+#ifdef VLIB_SUPPORTS_ARBITRARY_SCALAR_SIZES
   uword key = (n_scalar_bytes << 16) | n_vector_bytes;
   uword *p, i;
 
@@ -109,6 +110,11 @@ get_frame_size_info (vlib_node_main_t * nm,
     }
 
   return vec_elt_at_index (nm->frame_sizes, i);
+#else
+  ASSERT (vlib_frame_bytes (n_scalar_bytes, n_vector_bytes)
+         == (vlib_frame_bytes (0, 4)));
+  return vec_elt_at_index (nm->frame_sizes, 0);
+#endif
 }
 
 static u32
@@ -674,13 +680,16 @@ vlib_node_runtime_update_stats (vlib_main_t * vm,
   return r;
 }
 
-static inline void
-vlib_node_runtime_perf_counter (vlib_main_t * vm, u64 * pmc0, u64 * pmc1)
+always_inline void
+vlib_node_runtime_perf_counter (vlib_main_t * vm, u64 * pmc0, u64 * pmc1,
+                               vlib_node_runtime_t * node,
+                               vlib_frame_t * frame, int before_or_after)
 {
   *pmc0 = 0;
   *pmc1 = 0;
-  if (PREDICT_FALSE (vm->vlib_node_runtime_perf_counter_cb != 0))
-    (*vm->vlib_node_runtime_perf_counter_cb) (vm, pmc0, pmc1);
+  if (PREDICT_FALSE (vec_len (vm->vlib_node_runtime_perf_counter_cbs) != 0))
+    clib_call_callbacks (vm->vlib_node_runtime_perf_counter_cbs, vm, pmc0,
+                        pmc1, node, frame, before_or_after);
 }
 
 always_inline void
@@ -1175,7 +1184,8 @@ dispatch_node (vlib_main_t * vm,
                             last_time_stamp, frame ? frame->n_vectors : 0,
                             /* is_after */ 0);
 
-  vlib_node_runtime_perf_counter (vm, &pmc_before[0], &pmc_before[1]);
+  vlib_node_runtime_perf_counter (vm, &pmc_before[0], &pmc_before[1],
+                                 node, frame, 0 /* before */ );
 
   /*
    * Turn this on if you run into
@@ -1209,7 +1219,8 @@ dispatch_node (vlib_main_t * vm,
    * To validate accounting: pmc_delta = t - pmc_before;
    * perf ticks should equal clocks/pkt...
    */
-  vlib_node_runtime_perf_counter (vm, &pmc_after[0], &pmc_after[1]);
+  vlib_node_runtime_perf_counter (vm, &pmc_after[0], &pmc_after[1], node,
+                                 frame, 1 /* after */ );
 
   pmc_delta[0] = pmc_after[0] - pmc_before[0];
   pmc_delta[1] = pmc_after[1] - pmc_before[1];
@@ -1673,6 +1684,7 @@ vlib_main_or_worker_loop (vlib_main_t * vm, int is_main)
   u64 cpu_time_now;
   vlib_frame_queue_main_t *fqm;
   u32 *last_node_runtime_indices = 0;
+  u32 frame_queue_check_counter = 0;
 
   /* Initialize pending node vector. */
   if (is_main)
@@ -1728,11 +1740,28 @@ vlib_main_or_worker_loop (vlib_main_t * vm, int is_main)
       if (!is_main)
        {
          vlib_worker_thread_barrier_check ();
-         vec_foreach (fqm, tm->frame_queue_mains)
-           vlib_frame_queue_dequeue (vm, fqm);
-         if (PREDICT_FALSE (vm->worker_thread_main_loop_callback != 0))
-           ((void (*)(vlib_main_t *)) vm->worker_thread_main_loop_callback)
-             (vm);
+         if (PREDICT_FALSE (vm->check_frame_queues +
+                            frame_queue_check_counter))
+           {
+             u32 processed = 0;
+
+             if (vm->check_frame_queues)
+               {
+                 frame_queue_check_counter = 100;
+                 vm->check_frame_queues = 0;
+               }
+
+             vec_foreach (fqm, tm->frame_queue_mains)
+               processed += vlib_frame_queue_dequeue (vm, fqm);
+
+             /* No handoff queue work found? */
+             if (processed)
+               frame_queue_check_counter = 100;
+             else
+               frame_queue_check_counter--;
+           }
+         if (PREDICT_FALSE (vec_len (vm->worker_thread_main_loop_callbacks)))
+           clib_call_callbacks (vm->worker_thread_main_loop_callbacks, vm);
        }
 
       /* Process pre-input nodes. */
@@ -2058,6 +2087,17 @@ vlib_main (vlib_main_t * volatile vm, unformat_input_t * input)
   vec_validate (vm->processing_rpc_requests, 0);
   _vec_len (vm->processing_rpc_requests) = 0;
 
+  if ((error = vlib_call_all_config_functions (vm, input, 0 /* is_early */ )))
+    goto done;
+
+  /* Call all main loop enter functions. */
+  {
+    clib_error_t *sub_error;
+    sub_error = vlib_call_all_main_loop_enter_functions (vm);
+    if (sub_error)
+      clib_error_report (sub_error);
+  }
+
   switch (clib_setjmp (&vm->main_loop_exit, VLIB_MAIN_LOOP_EXIT_NONE))
     {
     case VLIB_MAIN_LOOP_EXIT_NONE:
@@ -2072,17 +2112,6 @@ vlib_main (vlib_main_t * volatile vm, unformat_input_t * input)
       goto done;
     }
 
-  if ((error = vlib_call_all_config_functions (vm, input, 0 /* is_early */ )))
-    goto done;
-
-  /* Call all main loop enter functions. */
-  {
-    clib_error_t *sub_error;
-    sub_error = vlib_call_all_main_loop_enter_functions (vm);
-    if (sub_error)
-      clib_error_report (sub_error);
-  }
-
   vlib_main_loop (vm);
 
 done:
@@ -2105,15 +2134,12 @@ pcap_dispatch_trace_command_internal (vlib_main_t * vm,
                                      unformat_input_t * input,
                                      vlib_cli_command_t * cmd, int rx_tx)
 {
-#define PCAP_DEF_PKT_TO_CAPTURE (100)
-
   unformat_input_t _line_input, *line_input = &_line_input;
   pcap_main_t *pm = &vm->dispatch_pcap_main;
-  u8 *filename;
-  u8 *chroot_filename = 0;
-  u32 max = 0;
+  u8 *filename = 0;
+  u32 max = 1000;
   int enabled = 0;
-  int errorFlag = 0;
+  int is_error = 0;
   clib_error_t *error = 0;
   u32 node_index, add;
   vlib_trace_main_t *tm;
@@ -2134,7 +2160,7 @@ pcap_dispatch_trace_command_internal (vlib_main_t * vm,
          else
            {
              vlib_cli_output (vm, "pcap dispatch capture already on...");
-             errorFlag = 1;
+             is_error = 1;
              break;
            }
        }
@@ -2158,7 +2184,7 @@ pcap_dispatch_trace_command_internal (vlib_main_t * vm,
          else
            {
              vlib_cli_output (vm, "pcap tx capture already off...");
-             errorFlag = 1;
+             is_error = 1;
              break;
            }
        }
@@ -2169,7 +2195,7 @@ pcap_dispatch_trace_command_internal (vlib_main_t * vm,
              vlib_cli_output
                (vm,
                 "can't change max value while pcap tx capture active...");
-             errorFlag = 1;
+             is_error = 1;
              break;
            }
          pm->n_packets_to_capture = max;
@@ -2182,7 +2208,7 @@ pcap_dispatch_trace_command_internal (vlib_main_t * vm,
            {
              vlib_cli_output
                (vm, "can't change file while pcap tx capture active...");
-             errorFlag = 1;
+             is_error = 1;
              break;
            }
        }
@@ -2227,32 +2253,27 @@ pcap_dispatch_trace_command_internal (vlib_main_t * vm,
        {
          error = clib_error_return (0, "unknown input `%U'",
                                     format_unformat_error, line_input);
-         errorFlag = 1;
+         is_error = 1;
          break;
        }
     }
   unformat_free (line_input);
 
-
-  if (errorFlag == 0)
+  if (is_error == 0)
     {
-      /* Since no error, save configured values. */
-      if (chroot_filename)
-       {
-         if (pm->file_name)
-           vec_free (pm->file_name);
-         vec_add1 (chroot_filename, 0);
-         pm->file_name = (char *) chroot_filename;
-       }
+      /* Clean up from previous run */
+      vec_free (pm->file_name);
+      vec_free (pm->pcap_data);
 
-      if (max)
-       pm->n_packets_to_capture = max;
+      memset (pm, 0, sizeof (*pm));
+      pm->n_packets_to_capture = max;
 
       if (enabled)
        {
-         if (pm->file_name == 0)
-           pm->file_name = (char *) format (0, "/tmp/dispatch.pcap%c", 0);
+         if (filename == 0)
+           filename = format (0, "/tmp/dispatch.pcap%c", 0);
 
+         pm->file_name = (char *) filename;
          pm->n_packets_captured = 0;
          pm->packet_type = PCAP_PACKET_TYPE_vpp;
          if (pm->lock == 0)
@@ -2261,8 +2282,6 @@ pcap_dispatch_trace_command_internal (vlib_main_t * vm,
          vlib_cli_output (vm, "pcap dispatch capture on...");
        }
     }
-  else if (chroot_filename)
-    vec_free (chroot_filename);
 
   return error;
 }