api: fix free socket process args
[vpp.git] / src / vlibmemory / memory_api.c
index 544e59d..42d1ee0 100644 (file)
@@ -210,6 +210,7 @@ vl_api_memclnt_create_t_handler (vl_api_memclnt_create_t * mp)
   regp->clib_file_index = am->shmem_hdr->clib_file_index;
 
   q = regp->vl_input_queue = (svm_queue_t *) (uword) mp->input_queue;
+  VL_MSG_API_SVM_QUEUE_UNPOISON (q);
 
   regp->name = format (0, "%s", mp->name);
   vec_add1 (regp->name, 0);
@@ -296,25 +297,32 @@ vl_api_memclnt_delete_t_handler (vl_api_memclnt_delete_t * mp)
       svm = am->vlib_rp;
       int private_registration = 0;
 
-      /*
-       * Note: the API message handling path will set am->vlib_rp
-       * as appropriate for pairwise / private memory segments
-       */
-      rp = vl_msg_api_alloc (sizeof (*rp));
-      rp->_vl_msg_id = ntohs (VL_API_MEMCLNT_DELETE_REPLY);
-      rp->handle = mp->handle;
-      rp->response = 1;
-
-      vl_msg_api_send_shmem (regp->vl_input_queue, (u8 *) & rp);
-
-      if (client_index != regp->vl_api_registration_pool_index)
+      /* Send reply unless client asked us to do the cleanup */
+      if (!mp->do_cleanup)
        {
-         clib_warning ("mismatch client_index %d pool_index %d",
-                       client_index, regp->vl_api_registration_pool_index);
-         vl_msg_api_free (rp);
-         return;
+         /*
+          * Note: the API message handling path will set am->vlib_rp
+          * as appropriate for pairwise / private memory segments
+          */
+         rp = vl_msg_api_alloc (sizeof (*rp));
+         rp->_vl_msg_id = ntohs (VL_API_MEMCLNT_DELETE_REPLY);
+         rp->handle = mp->handle;
+         rp->response = 1;
+
+         vl_msg_api_send_shmem (regp->vl_input_queue, (u8 *) & rp);
+         if (client_index != regp->vl_api_registration_pool_index)
+           {
+             clib_warning ("mismatch client_index %d pool_index %d",
+                           client_index,
+                           regp->vl_api_registration_pool_index);
+             vl_msg_api_free (rp);
+             return;
+           }
        }
 
+      /* No dangling references, please */
+      *regpp = 0;
+
       /* For horizontal scaling, add a hash table... */
       for (i = 0; i < vec_len (am->vlib_private_rps); i++)
        {
@@ -322,8 +330,8 @@ vl_api_memclnt_delete_t_handler (vl_api_memclnt_delete_t * mp)
          if (am->vlib_private_rps[i] == svm)
            {
              /* Note: account for the memfd header page */
-             u64 virtual_base = svm->virtual_base - MMAP_PAGESIZE;
-             u64 virtual_size = svm->virtual_size + MMAP_PAGESIZE;
+             uword virtual_base = svm->virtual_base - MMAP_PAGESIZE;
+             uword virtual_size = svm->virtual_size + MMAP_PAGESIZE;
 
              /*
               * Kill the registration pool element before we make
@@ -343,15 +351,14 @@ vl_api_memclnt_delete_t_handler (vl_api_memclnt_delete_t * mp)
            }
        }
 
-      /* No dangling references, please */
-      *regpp = 0;
-
       if (private_registration == 0)
        {
          pool_put_index (am->vl_clients,
                          regp->vl_api_registration_pool_index);
          pthread_mutex_lock (&svm->mutex);
          oldheap = svm_push_data_heap (svm);
+         if (mp->do_cleanup)
+           svm_queue_free (regp->vl_input_queue);
          vec_free (regp->name);
          /* Poison the old registration */
          clib_memset (regp, 0xF1, sizeof (*regp));
@@ -414,11 +421,16 @@ vl_api_memclnt_keepalive_t_handler (vl_api_memclnt_keepalive_t * mp)
   vl_msg_api_send_shmem (shmem_hdr->vl_input_queue, (u8 *) & rmp);
 }
 
+/*
+ * To avoid filling the API trace buffer with boring messages,
+ * don't trace memclnt_keepalive[_reply] msgs
+ */
+
 #define foreach_vlib_api_msg                            \
-_(MEMCLNT_CREATE, memclnt_create)                       \
-_(MEMCLNT_DELETE, memclnt_delete)                       \
-_(MEMCLNT_KEEPALIVE, memclnt_keepalive)                 \
-_(MEMCLNT_KEEPALIVE_REPLY, memclnt_keepalive_reply)    \
+_(MEMCLNT_CREATE, memclnt_create, 1)                    \
+_(MEMCLNT_DELETE, memclnt_delete, 1)                    \
+_(MEMCLNT_KEEPALIVE, memclnt_keepalive, 0)              \
+_(MEMCLNT_KEEPALIVE_REPLY, memclnt_keepalive_reply, 0)
 
 /*
  * memory_api_init
@@ -438,7 +450,7 @@ vl_mem_api_init (const char *region_name)
   if ((rv = vl_map_shmem (region_name, 1 /* is_vlib */ )) < 0)
     return rv;
 
-#define _(N,n) do {                                             \
+#define _(N,n,t) do {                                            \
     c->id = VL_API_##N;                                         \
     c->name = #n;                                               \
     c->handler = vl_api_##n##_t_handler;                        \
@@ -446,7 +458,7 @@ vl_mem_api_init (const char *region_name)
     c->endian = vl_api_##n##_t_endian;                          \
     c->print = vl_api_##n##_t_print;                            \
     c->size = sizeof(vl_api_##n##_t);                           \
-    c->traced = 1; /* trace, so these msgs print */             \
+    c->traced = t; /* trace, so these msgs print */             \
     c->replay = 0; /* don't replay client create/delete msgs */ \
     c->message_bounce = 0; /* don't bounce this message */     \
     vl_msg_api_config(c);} while (0);
@@ -460,6 +472,7 @@ vl_mem_api_init (const char *region_name)
    */
   am->message_bounce[VL_API_MEMCLNT_DELETE] = 1;
   am->is_mp_safe[VL_API_MEMCLNT_KEEPALIVE_REPLY] = 1;
+  am->is_mp_safe[VL_API_MEMCLNT_KEEPALIVE] = 1;
 
   vlib_set_queue_signal_callback (vm, memclnt_queue_callback);
 
@@ -644,8 +657,8 @@ vl_mem_api_dead_client_scan (api_main_t * am, vl_shmem_hdr_t * shm, f64 now)
                  int i;
                  svm_region_t *dead_rp = (*regpp)->vlib_rp;
                  /* Note: account for the memfd header page */
-                 u64 virtual_base = dead_rp->virtual_base - MMAP_PAGESIZE;
-                 u64 virtual_size = dead_rp->virtual_size + MMAP_PAGESIZE;
+                 uword virtual_base = dead_rp->virtual_base - MMAP_PAGESIZE;
+                 uword virtual_size = dead_rp->virtual_size + MMAP_PAGESIZE;
 
                  /* For horizontal scaling, add a hash table... */
                  for (i = 0; i < vec_len (am->vlib_private_rps); i++)
@@ -654,14 +667,18 @@ vl_mem_api_dead_client_scan (api_main_t * am, vl_shmem_hdr_t * shm, f64 now)
                        vec_delete (am->vlib_private_rps, 1, i);
                        goto found;
                      }
+                 svm_pop_heap (oldheap);
                  clib_warning ("private rp %llx AWOL", dead_rp);
+                 oldheap = svm_push_data_heap (svm);
 
                found:
                  /* Kill it, accounting for the memfd header page */
+                 svm_pop_heap (oldheap);
                  if (munmap ((void *) virtual_base, virtual_size) < 0)
                    clib_unix_warning ("munmap");
                  /* Reset the queue-length-address cache */
                  vec_reset_length (vl_api_queue_cursizes);
+                 oldheap = svm_push_data_heap (svm);
                }
              else
                {
@@ -697,6 +714,7 @@ void_mem_api_handle_msg_i (api_main_t * am, vlib_main_t * vm,
   uword mp;
   if (!svm_queue_sub2 (q, (u8 *) & mp))
     {
+      VL_MSG_API_UNPOISON ((void *) mp);
       vl_msg_api_handler_with_vm_node (am, (void *) mp, vm, node);
       return 0;
     }
@@ -729,11 +747,28 @@ vl_mem_api_handle_rpc (vlib_main_t * vm, vlib_node_runtime_t * node)
   vm->pending_rpc_requests = tmp;
   clib_spinlock_unlock_if_init (&vm->pending_rpc_lock);
 
-  for (i = 0; i < vec_len (vm->processing_rpc_requests); i++)
+  /*
+   * RPCs are used to reflect function calls to thread 0
+   * when the underlying code is not thread-safe.
+   *
+   * Grabbing the thread barrier across a set of RPCs
+   * greatly increases efficiency, and avoids
+   * running afoul of the barrier sync holddown timer.
+   * The barrier sync code supports recursive locking.
+   *
+   * We really need to rewrite RPC-based code...
+   */
+  if (PREDICT_TRUE (vec_len (vm->processing_rpc_requests)))
     {
-      mp = vm->processing_rpc_requests[i];
-      vl_msg_api_handler_with_vm_node (am, (void *) mp, vm, node);
+      vl_msg_api_barrier_sync ();
+      for (i = 0; i < vec_len (vm->processing_rpc_requests); i++)
+       {
+         mp = vm->processing_rpc_requests[i];
+         vl_msg_api_handler_with_vm_node (am, (void *) mp, vm, node);
+       }
+      vl_msg_api_barrier_release ();
     }
+
   return 0;
 }
 
@@ -931,8 +966,10 @@ vlibmemory_init (vlib_main_t * vm)
 {
   api_main_t *am = &api_main;
   svm_map_region_args_t _a, *a = &_a;
-  clib_error_t *error;
   u8 *remove_path1, *remove_path2;
+  void vlibsocket_reference (void);
+
+  vlibsocket_reference ();
 
   /*
    * By popular request / to avoid support fires, remove any old api segment
@@ -970,9 +1007,7 @@ vlibmemory_init (vlib_main_t * vm)
 
   svm_region_init_args (a);
 
-  error = vlib_call_init_function (vm, vlibsocket_init);
-
-  return error;
+  return 0;
 }
 
 void