vcl: stop tracking vpp event queues and thread index
[vpp.git] / src / vcl / vcl_bapi.c
index d3a27ac..e245c4d 100644 (file)
@@ -61,12 +61,6 @@ static void
     vcm->bapi_app_state = STATE_APP_ENABLED;
 }
 
-static u64
-vcl_vpp_worker_segment_handle (u32 wrk_index)
-{
-  return (VCL_INVALID_SEGMENT_HANDLE - wrk_index - 1);
-}
-
 static void
 vl_api_app_attach_reply_t_handler (vl_api_app_attach_reply_t * mp)
 {
@@ -85,8 +79,6 @@ vl_api_app_attach_reply_t_handler (vl_api_app_attach_reply_t * mp)
 
   wrk->app_event_queue = uword_to_pointer (mp->app_mq, svm_msg_q_t *);
   ctrl_mq = uword_to_pointer (mp->vpp_ctrl_mq, svm_msg_q_t *);
-  vec_validate (wrk->vpp_event_queues, mp->vpp_ctrl_mq_thread);
-  wrk->vpp_event_queues[mp->vpp_ctrl_mq_thread] = ctrl_mq;
   vcm->ctrl_mq = wrk->ctrl_mq = ctrl_mq;
   segment_handle = clib_net_to_host_u64 (mp->segment_handle);
   if (segment_handle == VCL_INVALID_SEGMENT_HANDLE)
@@ -293,7 +285,7 @@ vcl_bapi_send_session_enable_disable (u8 is_enable)
   memset (bmp, 0, sizeof (*bmp));
 
   bmp->_vl_msg_id = ntohs (VL_API_SESSION_ENABLE_DISABLE);
-  bmp->client_index = wrk->bapi_client_index;
+  bmp->client_index = wrk->api_client_handle;
   bmp->context = htonl (0xfeedface);
   bmp->is_enable = is_enable;
   vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & bmp);
@@ -315,7 +307,7 @@ vcl_bapi_send_attach (void)
   memset (bmp, 0, sizeof (*bmp));
 
   bmp->_vl_msg_id = ntohs (VL_API_APP_ATTACH);
-  bmp->client_index = wrk->bapi_client_index;
+  bmp->client_index = wrk->api_client_handle;
   bmp->context = htonl (0xfeedface);
   bmp->options[APP_OPTIONS_FLAGS] =
     APP_OPTIONS_FLAGS_ACCEPT_REDIRECT | APP_OPTIONS_FLAGS_ADD_SEGMENT |
@@ -351,7 +343,7 @@ vcl_bapi_send_detach (void)
   memset (bmp, 0, sizeof (*bmp));
 
   bmp->_vl_msg_id = ntohs (VL_API_APPLICATION_DETACH);
-  bmp->client_index = wrk->bapi_client_index;
+  bmp->client_index = wrk->api_client_handle;
   bmp->context = htonl (0xfeedface);
   vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & bmp);
 }
@@ -366,7 +358,7 @@ vcl_bapi_send_app_worker_add_del (u8 is_add)
   memset (mp, 0, sizeof (*mp));
 
   mp->_vl_msg_id = ntohs (VL_API_APP_WORKER_ADD_DEL);
-  mp->client_index = wrk->bapi_client_index;
+  mp->client_index = wrk->api_client_handle;
   mp->app_index = clib_host_to_net_u32 (vcm->app_index);
   mp->context = wrk->wrk_index;
   mp->is_add = is_add;
@@ -386,7 +378,7 @@ vcl_bapi_send_child_worker_del (vcl_worker_t * child_wrk)
   memset (mp, 0, sizeof (*mp));
 
   mp->_vl_msg_id = ntohs (VL_API_APP_WORKER_ADD_DEL);
-  mp->client_index = wrk->bapi_client_index;
+  mp->client_index = wrk->api_client_handle;
   mp->app_index = clib_host_to_net_u32 (vcm->app_index);
   mp->context = wrk->wrk_index;
   mp->is_add = 0;
@@ -405,7 +397,7 @@ vcl_bapi_send_application_tls_cert_add (vcl_session_t * session, char *cert,
   cert_mp = vl_msg_api_alloc (sizeof (*cert_mp) + cert_len);
   clib_memset (cert_mp, 0, sizeof (*cert_mp));
   cert_mp->_vl_msg_id = ntohs (VL_API_APPLICATION_TLS_CERT_ADD);
-  cert_mp->client_index = wrk->bapi_client_index;
+  cert_mp->client_index = wrk->api_client_handle;
   cert_mp->context = session->session_index;
   cert_mp->cert_len = clib_host_to_net_u16 (cert_len);
   clib_memcpy_fast (cert_mp->cert, cert, cert_len);
@@ -422,7 +414,7 @@ vcl_bapi_send_application_tls_key_add (vcl_session_t * session, char *key,
   key_mp = vl_msg_api_alloc (sizeof (*key_mp) + key_len);
   clib_memset (key_mp, 0, sizeof (*key_mp));
   key_mp->_vl_msg_id = ntohs (VL_API_APPLICATION_TLS_KEY_ADD);
-  key_mp->client_index = wrk->bapi_client_index;
+  key_mp->client_index = wrk->api_client_handle;
   key_mp->context = session->session_index;
   key_mp->key_len = clib_host_to_net_u16 (key_len);
   clib_memcpy_fast (key_mp->key, key, key_len);
@@ -472,59 +464,42 @@ vcl_bapi_connect_to_vpp (void)
   api_main_t *am;
   u8 *wrk_name;
 
-  wrk_name = format (0, "%s-wrk-%u%c", vcm->app_name, wrk->wrk_index, 0);
+  wrk_name = format (0, "%v-wrk-%u%c", vcm->app_name, wrk->wrk_index, 0);
 
   /* Make sure api is cleaned up in case this is a connect from a
    * forked worker */
   vcl_bapi_cleanup ();
 
   vlibapi_set_main (&wrk->bapi_api_ctx);
-  vlibapi_set_memory_client_main (&wrk->bapi_shm_ctx);
   vcl_bapi_hookup ();
 
-  if (vcl_cfg->vpp_bapi_socket_name)
+  if (!vcl_cfg->vpp_bapi_socket_name)
     {
-      if (vl_socket_client_connect2 (&wrk->bapi_sock_ctx,
-                                    (char *) vcl_cfg->vpp_bapi_socket_name,
-                                    (char *) wrk_name,
-                                    0 /* default rx/tx buffer */ ))
-       {
-         VERR ("app (%s) socket connect failed!", wrk_name);
-         rv = VPPCOM_ECONNREFUSED;
-         goto error;
-       }
-
-      if (vl_socket_client_init_shm2 (&wrk->bapi_sock_ctx, 0,
-                                     1 /* want_pthread */ ))
-       {
-         VERR ("app (%s) init shm failed!", wrk_name);
-         rv = VPPCOM_ECONNREFUSED;
-         goto error;
-       }
+      rv = VPPCOM_EINVAL;
+      goto error;
     }
-  else
-    {
-      if (!vcl_cfg->vpp_bapi_filename)
-       vcl_cfg->vpp_bapi_filename = format (0, "/vpe-api%c", 0);
-
-      vl_set_memory_root_path ((char *) vcl_cfg->vpp_bapi_chroot);
 
-      VDBG (0, "app (%s) connecting to VPP api (%s)...",
-           wrk_name, vcl_cfg->vpp_bapi_filename);
+  if (vl_socket_client_connect2 (&wrk->bapi_sock_ctx,
+                                (char *) vcl_cfg->vpp_bapi_socket_name,
+                                (char *) wrk_name,
+                                0 /* default rx/tx buffer */ ))
+    {
+      VERR ("app (%s) socket connect failed!", wrk_name);
+      rv = VPPCOM_ECONNREFUSED;
+      goto error;
+    }
 
-      if (vl_client_connect_to_vlib ((char *) vcl_cfg->vpp_bapi_filename,
-                                    (char *) wrk_name,
-                                    vcm->cfg.vpp_api_q_length) < 0)
-       {
-         VERR ("app (%s) connect failed!", wrk_name);
-         rv = VPPCOM_ECONNREFUSED;
-         goto error;
-       }
+  if (vl_socket_client_init_shm2 (&wrk->bapi_sock_ctx, 0,
+                                 1 /* want_pthread */ ))
+    {
+      VERR ("app (%s) init shm failed!", wrk_name);
+      rv = VPPCOM_ECONNREFUSED;
+      goto error;
     }
 
   am = vlibapi_get_main ();
   wrk->vl_input_queue = am->shmem_hdr->vl_input_queue;
-  wrk->bapi_client_index = (u32) am->my_client_index;
+  wrk->api_client_handle = (u32) am->my_client_index;
 
   VDBG (0, "app (%s) is connected to VPP!", wrk_name);
   vcl_evt (VCL_EVT_INIT, vcm);
@@ -690,6 +665,21 @@ vcl_bapi_app_worker_del (vcl_worker_t * wrk)
     vl_client_send_disconnect (1 /* vpp should cleanup */ );
 }
 
+int
+vcl_bapi_recv_fds (vcl_worker_t * wrk, int *fds, int n_fds)
+{
+  clib_error_t *err;
+
+  if ((err = vl_socket_client_recv_fd_msg2 (&wrk->bapi_sock_ctx, fds, n_fds,
+                                           5)))
+    {
+      clib_error_report (err);
+      return -1;
+    }
+
+  return 0;
+}
+
 int
 vppcom_session_tls_add_cert (uint32_t session_handle, char *cert,
                             uint32_t cert_len)
@@ -749,7 +739,7 @@ vcl_bapi_worker_set (void)
       if (vcm->workers[i].current_pid == wrk->current_pid)
        {
          wrk->vl_input_queue = vcm->workers[i].vl_input_queue;
-         wrk->bapi_client_index = vcm->workers[i].bapi_client_index;
+         wrk->api_client_handle = vcm->workers[i].api_client_handle;
          return 0;
        }
     }