vcl: allow all workers to block on api
[vpp.git] / src / vcl / vcl_bapi.c
1 /*
2  * Copyright (c) 2018-2019 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this
5  * You may obtain a copy of the License at:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15
16 #include <vcl/vcl_private.h>
17 #include <vlibmemory/api.h>
18 #include <vpp/api/vpe_msg_enum.h>
19
20 #define vl_typedefs             /* define message structures */
21 #include <vpp/api/vpe_all_api_h.h>
22 #undef vl_typedefs
23
24 /* declare message handlers for each api */
25
26 #define vl_endianfun            /* define message structures */
27 #include <vpp/api/vpe_all_api_h.h>
28 #undef vl_endianfun
29
30 /* instantiate all the print functions we know about */
31 #define vl_print(handle, ...)
32 #define vl_printfun
33 #include <vpp/api/vpe_all_api_h.h>
34 #undef vl_printfun
35
36 static u8 *
37 format_api_error (u8 * s, va_list * args)
38 {
39   i32 error = va_arg (*args, u32);
40   uword *p;
41
42   p = hash_get (vcm->error_string_by_error_number, -error);
43
44   if (p)
45     s = format (s, "%s (%d)", p[0], error);
46   else
47     s = format (s, "%d", error);
48   return s;
49 }
50
51 static void
52   vl_api_session_enable_disable_reply_t_handler
53   (vl_api_session_enable_disable_reply_t * mp)
54 {
55   vcl_worker_t *wrk = vcl_worker_get (0);
56
57   if (mp->retval)
58     {
59       clib_warning ("VCL<%d>: session_enable_disable failed: %U", getpid (),
60                     format_api_error, ntohl (mp->retval));
61     }
62   else
63     wrk->bapi_app_state = STATE_APP_ENABLED;
64 }
65
66 static void
67 vl_api_app_attach_reply_t_handler (vl_api_app_attach_reply_t * mp)
68 {
69   vcl_worker_t *wrk = vcl_worker_get (0);
70   u64 segment_handle;
71   int *fds = 0, i, rv;
72   u32 n_fds = 0;
73   char *segment_name = 0;
74
75   if (mp->retval)
76     {
77       VERR ("attach failed: %U", format_api_error, ntohl (mp->retval));
78       goto failed;
79     }
80
81   vcl_set_worker_index (0);
82
83   segment_handle = clib_net_to_host_u64 (mp->segment_handle);
84   if (segment_handle == VCL_INVALID_SEGMENT_HANDLE)
85     {
86       VERR ("invalid segment handle");
87       goto failed;
88     }
89
90   if (mp->n_fds)
91     {
92       vec_validate (fds, mp->n_fds);
93       if (vl_socket_client_recv_fd_msg2 (&wrk->bapi_sock_ctx, fds, mp->n_fds,
94                                          5))
95         goto failed;
96
97       if (mp->fd_flags & SESSION_FD_F_VPP_MQ_SEGMENT)
98         if (vcl_segment_attach (vcl_vpp_worker_segment_handle (0),
99                                 "vpp-mq-seg", SSVM_SEGMENT_MEMFD,
100                                 fds[n_fds++]))
101           goto failed;
102
103       vcl_segment_attach_mq (vcl_vpp_worker_segment_handle (0),
104                              mp->vpp_ctrl_mq, mp->vpp_ctrl_mq_thread,
105                              &wrk->ctrl_mq);
106       vcm->ctrl_mq = wrk->ctrl_mq;
107
108       if (mp->fd_flags & SESSION_FD_F_MEMFD_SEGMENT)
109         {
110           segment_name = vl_api_from_api_to_new_c_string (&mp->segment_name);
111           rv =
112             vcl_segment_attach (segment_handle, segment_name,
113                                 SSVM_SEGMENT_MEMFD, fds[n_fds++]);
114           vec_free (segment_name);
115           if (rv != 0)
116             goto failed;
117         }
118
119       vcl_segment_attach_mq (segment_handle, mp->app_mq, 0,
120                              &wrk->app_event_queue);
121
122       if (mp->fd_flags & SESSION_FD_F_MQ_EVENTFD)
123         {
124           svm_msg_q_set_consumer_eventfd (wrk->app_event_queue, fds[n_fds]);
125           vcl_mq_epoll_add_evfd (wrk, wrk->app_event_queue);
126           n_fds++;
127         }
128
129       vec_free (fds);
130     }
131   else
132     {
133       segment_name = vl_api_from_api_to_new_c_string (&mp->segment_name);
134       rv =
135         vcl_segment_attach (segment_handle, segment_name, SSVM_SEGMENT_SHM,
136                             -1);
137       vec_free (segment_name);
138       if (rv != 0)
139         goto failed;
140     }
141
142   vcm->app_index = clib_net_to_host_u32 (mp->app_index);
143   wrk->bapi_app_state = STATE_APP_ATTACHED;
144   return;
145
146 failed:
147   wrk->bapi_app_state = STATE_APP_FAILED;
148   for (i = clib_max (n_fds - 1, 0); i < vec_len (fds); i++)
149     close (fds[i]);
150   vec_free (fds);
151 }
152
153 static void
154 vl_api_app_worker_add_del_reply_t_handler (vl_api_app_worker_add_del_reply_t *
155                                            mp)
156 {
157   int n_fds = 0, *fds = 0, i, rv;
158   u64 segment_handle;
159   vcl_worker_t *wrk;
160   u32 wrk_index;
161   char *segment_name = 0;
162
163   if (!mp->is_add)
164     return;
165
166   wrk_index = mp->context;
167   wrk = vcl_worker_get_if_valid (wrk_index);
168   if (!wrk)
169     return;
170
171   if (mp->retval)
172     {
173       clib_warning ("VCL<%d>: add/del worker failed: %U", getpid (),
174                     format_api_error, ntohl (mp->retval));
175       goto failed;
176     }
177
178   vcl_set_worker_index (wrk_index);
179   wrk->vpp_wrk_index = clib_net_to_host_u32 (mp->wrk_index);
180   wrk->ctrl_mq = vcm->ctrl_mq;
181
182   segment_handle = clib_net_to_host_u64 (mp->segment_handle);
183   if (segment_handle == VCL_INVALID_SEGMENT_HANDLE)
184     {
185       clib_warning ("invalid segment handle");
186       goto failed;
187     }
188
189   if (mp->n_fds)
190     {
191       vec_validate (fds, mp->n_fds);
192       if (vl_socket_client_recv_fd_msg2 (&wrk->bapi_sock_ctx, fds, mp->n_fds,
193                                          5))
194         goto failed;
195
196       if (mp->fd_flags & SESSION_FD_F_VPP_MQ_SEGMENT)
197         if (vcl_segment_attach (vcl_vpp_worker_segment_handle (wrk_index),
198                                 "vpp-worker-seg", SSVM_SEGMENT_MEMFD,
199                                 fds[n_fds++]))
200           goto failed;
201
202       if (mp->fd_flags & SESSION_FD_F_MEMFD_SEGMENT)
203         {
204           segment_name = vl_api_from_api_to_new_c_string (&mp->segment_name);
205           rv =
206             vcl_segment_attach (segment_handle, segment_name,
207                                 SSVM_SEGMENT_MEMFD, fds[n_fds++]);
208           vec_free (segment_name);
209           if (rv != 0)
210             goto failed;
211         }
212
213       vcl_segment_attach_mq (segment_handle, mp->app_event_queue_address, 0,
214                              &wrk->app_event_queue);
215
216       if (mp->fd_flags & SESSION_FD_F_MQ_EVENTFD)
217         {
218           svm_msg_q_set_consumer_eventfd (wrk->app_event_queue, fds[n_fds]);
219           vcl_mq_epoll_add_evfd (wrk, wrk->app_event_queue);
220           n_fds++;
221         }
222
223       vec_free (fds);
224     }
225   else
226     {
227       segment_name = vl_api_from_api_to_new_c_string (&mp->segment_name);
228       rv =
229         vcl_segment_attach (segment_handle, segment_name, SSVM_SEGMENT_SHM,
230                             -1);
231       vec_free (segment_name);
232       if (rv != 0)
233         goto failed;
234     }
235   wrk->bapi_app_state = STATE_APP_READY;
236   VDBG (0, "worker %u vpp-worker %u added", wrk_index, wrk->vpp_wrk_index);
237   return;
238
239 failed:
240   wrk->bapi_app_state = STATE_APP_FAILED;
241   for (i = clib_max (n_fds - 1, 0); i < vec_len (fds); i++)
242     close (fds[i]);
243   vec_free (fds);
244 }
245
246 static void
247   vl_api_application_tls_cert_add_reply_t_handler
248   (vl_api_application_tls_cert_add_reply_t * mp)
249 {
250   vcl_worker_t *wrk = vcl_worker_get_current ();
251
252   if (mp->retval)
253     {
254       VDBG (0, "add cert failed: %U", format_api_error, ntohl (mp->retval));
255       wrk->bapi_app_state = STATE_APP_FAILED;
256       return;
257     }
258   wrk->bapi_app_state = STATE_APP_READY;
259 }
260
261 static void
262   vl_api_application_tls_key_add_reply_t_handler
263   (vl_api_application_tls_key_add_reply_t * mp)
264 {
265   vcl_worker_t *wrk = vcl_worker_get_current ();
266
267   if (mp->retval)
268     {
269       VDBG (0, "add key failed: %U", format_api_error, ntohl (mp->retval));
270       wrk->bapi_app_state = STATE_APP_FAILED;
271       return;
272     }
273   wrk->bapi_app_state = STATE_APP_READY;
274 }
275
276 #define foreach_sock_msg                                                \
277 _(SESSION_ENABLE_DISABLE_REPLY, session_enable_disable_reply)           \
278 _(APP_ATTACH_REPLY, app_attach_reply)                                   \
279 _(APPLICATION_TLS_CERT_ADD_REPLY, application_tls_cert_add_reply)       \
280 _(APPLICATION_TLS_KEY_ADD_REPLY, application_tls_key_add_reply)         \
281 _(APP_WORKER_ADD_DEL_REPLY, app_worker_add_del_reply)                   \
282
283 static void
284 vcl_bapi_hookup (void)
285 {
286 #define _(N, n)                                                 \
287     vl_msg_api_set_handlers(VL_API_##N, #n,                     \
288                            vl_api_##n##_t_handler,              \
289                            vl_noop_handler,                     \
290                            vl_api_##n##_t_endian,               \
291                            vl_api_##n##_t_print,                \
292                            sizeof(vl_api_##n##_t), 1);
293   foreach_sock_msg;
294 #undef _
295 }
296
297 /*
298  * VPP-API message functions
299  */
300 static void
301 vcl_bapi_send_session_enable_disable (u8 is_enable)
302 {
303   vcl_worker_t *wrk = vcl_worker_get_current ();
304   vl_api_session_enable_disable_t *bmp;
305   bmp = vl_msg_api_alloc (sizeof (*bmp));
306   memset (bmp, 0, sizeof (*bmp));
307
308   bmp->_vl_msg_id = ntohs (VL_API_SESSION_ENABLE_DISABLE);
309   bmp->client_index = wrk->api_client_handle;
310   bmp->context = htonl (0xfeedface);
311   bmp->is_enable = is_enable;
312   vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & bmp);
313 }
314
315 void
316 vcl_bapi_send_attach (void)
317 {
318   vcl_worker_t *wrk = vcl_worker_get_current ();
319   u8 tls_engine = CRYPTO_ENGINE_OPENSSL;
320   vl_api_app_attach_t *bmp;
321   u8 nsid_len = vec_len (vcm->cfg.namespace_id);
322   u8 app_is_proxy = (vcm->cfg.app_proxy_transport_tcp ||
323                      vcm->cfg.app_proxy_transport_udp);
324
325   tls_engine = vcm->cfg.tls_engine ? vcm->cfg.tls_engine : tls_engine;
326
327   bmp = vl_msg_api_alloc (sizeof (*bmp));
328   memset (bmp, 0, sizeof (*bmp));
329
330   bmp->_vl_msg_id = ntohs (VL_API_APP_ATTACH);
331   bmp->client_index = wrk->api_client_handle;
332   bmp->context = htonl (0xfeedface);
333   bmp->options[APP_OPTIONS_FLAGS] =
334     APP_OPTIONS_FLAGS_ACCEPT_REDIRECT | APP_OPTIONS_FLAGS_ADD_SEGMENT |
335     (vcm->cfg.app_scope_local ? APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE : 0) |
336     (vcm->cfg.app_scope_global ? APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE : 0) |
337     (app_is_proxy ? APP_OPTIONS_FLAGS_IS_PROXY : 0) |
338     (vcm->cfg.use_mq_eventfd ? APP_OPTIONS_FLAGS_EVT_MQ_USE_EVENTFD : 0);
339   bmp->options[APP_OPTIONS_PROXY_TRANSPORT] =
340     (u64) ((vcm->cfg.app_proxy_transport_tcp ? 1 << TRANSPORT_PROTO_TCP : 0) |
341            (vcm->cfg.app_proxy_transport_udp ? 1 << TRANSPORT_PROTO_UDP : 0));
342   bmp->options[APP_OPTIONS_SEGMENT_SIZE] = vcm->cfg.segment_size;
343   bmp->options[APP_OPTIONS_ADD_SEGMENT_SIZE] = vcm->cfg.add_segment_size;
344   bmp->options[APP_OPTIONS_RX_FIFO_SIZE] = vcm->cfg.rx_fifo_size;
345   bmp->options[APP_OPTIONS_TX_FIFO_SIZE] = vcm->cfg.tx_fifo_size;
346   bmp->options[APP_OPTIONS_PREALLOC_FIFO_PAIRS] =
347     vcm->cfg.preallocated_fifo_pairs;
348   bmp->options[APP_OPTIONS_EVT_QUEUE_SIZE] = vcm->cfg.event_queue_size;
349   bmp->options[APP_OPTIONS_TLS_ENGINE] = tls_engine;
350   if (nsid_len)
351     {
352       vl_api_vec_to_api_string (vcm->cfg.namespace_id, &bmp->namespace_id);
353       bmp->options[APP_OPTIONS_NAMESPACE_SECRET] = vcm->cfg.namespace_secret;
354     }
355   vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & bmp);
356 }
357
358 void
359 vcl_bapi_send_detach (void)
360 {
361   vcl_worker_t *wrk = vcl_worker_get_current ();
362   vl_api_application_detach_t *bmp;
363   bmp = vl_msg_api_alloc (sizeof (*bmp));
364   memset (bmp, 0, sizeof (*bmp));
365
366   bmp->_vl_msg_id = ntohs (VL_API_APPLICATION_DETACH);
367   bmp->client_index = wrk->api_client_handle;
368   bmp->context = htonl (0xfeedface);
369   vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & bmp);
370 }
371
372 static void
373 vcl_bapi_send_app_worker_add_del (u8 is_add)
374 {
375   vcl_worker_t *wrk = vcl_worker_get_current ();
376   vl_api_app_worker_add_del_t *mp;
377
378   mp = vl_msg_api_alloc (sizeof (*mp));
379   memset (mp, 0, sizeof (*mp));
380
381   mp->_vl_msg_id = ntohs (VL_API_APP_WORKER_ADD_DEL);
382   mp->client_index = wrk->api_client_handle;
383   mp->app_index = clib_host_to_net_u32 (vcm->app_index);
384   mp->context = wrk->wrk_index;
385   mp->is_add = is_add;
386   if (!is_add)
387     mp->wrk_index = clib_host_to_net_u32 (wrk->vpp_wrk_index);
388
389   vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & mp);
390 }
391
392 static void
393 vcl_bapi_send_child_worker_del (vcl_worker_t * child_wrk)
394 {
395   vcl_worker_t *wrk = vcl_worker_get_current ();
396   vl_api_app_worker_add_del_t *mp;
397
398   mp = vl_msg_api_alloc (sizeof (*mp));
399   memset (mp, 0, sizeof (*mp));
400
401   mp->_vl_msg_id = ntohs (VL_API_APP_WORKER_ADD_DEL);
402   mp->client_index = wrk->api_client_handle;
403   mp->app_index = clib_host_to_net_u32 (vcm->app_index);
404   mp->context = wrk->wrk_index;
405   mp->is_add = 0;
406   mp->wrk_index = clib_host_to_net_u32 (child_wrk->vpp_wrk_index);
407
408   vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & mp);
409 }
410
411 void
412 vcl_bapi_send_application_tls_cert_add (vcl_session_t * session, char *cert,
413                                         u32 cert_len)
414 {
415   vcl_worker_t *wrk = vcl_worker_get_current ();
416   vl_api_application_tls_cert_add_t *cert_mp;
417
418   cert_mp = vl_msg_api_alloc (sizeof (*cert_mp) + cert_len);
419   clib_memset (cert_mp, 0, sizeof (*cert_mp));
420   cert_mp->_vl_msg_id = ntohs (VL_API_APPLICATION_TLS_CERT_ADD);
421   cert_mp->client_index = wrk->api_client_handle;
422   cert_mp->context = session->session_index;
423   cert_mp->cert_len = clib_host_to_net_u16 (cert_len);
424   clib_memcpy_fast (cert_mp->cert, cert, cert_len);
425   vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & cert_mp);
426 }
427
428 void
429 vcl_bapi_send_application_tls_key_add (vcl_session_t * session, char *key,
430                                        u32 key_len)
431 {
432   vcl_worker_t *wrk = vcl_worker_get_current ();
433   vl_api_application_tls_key_add_t *key_mp;
434
435   key_mp = vl_msg_api_alloc (sizeof (*key_mp) + key_len);
436   clib_memset (key_mp, 0, sizeof (*key_mp));
437   key_mp->_vl_msg_id = ntohs (VL_API_APPLICATION_TLS_KEY_ADD);
438   key_mp->client_index = wrk->api_client_handle;
439   key_mp->context = session->session_index;
440   key_mp->key_len = clib_host_to_net_u16 (key_len);
441   clib_memcpy_fast (key_mp->key, key, key_len);
442   vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & key_mp);
443 }
444
445 u32
446 vcl_bapi_max_nsid_len (void)
447 {
448   vl_api_app_attach_t *mp;
449   return (sizeof (mp->namespace_id) - 1);
450 }
451
452 static void
453 vcl_bapi_init_error_string_table (void)
454 {
455   vcm->error_string_by_error_number = hash_create (0, sizeof (uword));
456
457 #define _(n, v, s) hash_set (vcm->error_string_by_error_number, -v, s);
458   foreach_vnet_api_error;
459 #undef _
460
461   hash_set (vcm->error_string_by_error_number, 99, "Misc");
462 }
463
464 static void
465 vcl_bapi_cleanup (void)
466 {
467   socket_client_main_t *scm = &socket_client_main;
468   api_main_t *am = vlibapi_get_main ();
469
470   am->my_client_index = ~0;
471   am->my_registration = 0;
472   am->vl_input_queue = 0;
473   am->msg_index_by_name_and_crc = 0;
474   scm->socket_fd = 0;
475
476   vl_client_api_unmap ();
477 }
478
479 static int
480 vcl_bapi_connect_to_vpp (void)
481 {
482   vcl_worker_t *wrk = vcl_worker_get_current ();
483   vppcom_cfg_t *vcl_cfg = &vcm->cfg;
484   int rv = VPPCOM_OK;
485   api_main_t *am;
486   u8 *wrk_name;
487
488   wrk_name = format (0, "%v-wrk-%u%c", vcm->app_name, wrk->wrk_index, 0);
489
490   /* Make sure api is cleaned up in case this is a connect from a
491    * forked worker */
492   vcl_bapi_cleanup ();
493
494   vlibapi_set_main (&wrk->bapi_api_ctx);
495   vcl_bapi_hookup ();
496
497   if (!vcl_cfg->vpp_bapi_socket_name)
498     {
499       rv = VPPCOM_EINVAL;
500       goto error;
501     }
502
503   if (vl_socket_client_connect2 (&wrk->bapi_sock_ctx,
504                                  (char *) vcl_cfg->vpp_bapi_socket_name,
505                                  (char *) wrk_name,
506                                  0 /* default rx/tx buffer */ ))
507     {
508       VERR ("app (%s) socket connect failed!", wrk_name);
509       rv = VPPCOM_ECONNREFUSED;
510       goto error;
511     }
512
513   if (vl_socket_client_init_shm2 (&wrk->bapi_sock_ctx, 0,
514                                   1 /* want_pthread */ ))
515     {
516       VERR ("app (%s) init shm failed!", wrk_name);
517       rv = VPPCOM_ECONNREFUSED;
518       goto error;
519     }
520
521   am = vlibapi_get_main ();
522   wrk->vl_input_queue = am->shmem_hdr->vl_input_queue;
523   wrk->api_client_handle = (u32) am->my_client_index;
524
525   VDBG (0, "app (%s) is connected to VPP!", wrk_name);
526   vcl_evt (VCL_EVT_INIT, vcm);
527
528 error:
529   vec_free (wrk_name);
530   return rv;
531 }
532
533 void
534 vcl_bapi_disconnect_from_vpp (void)
535 {
536   vcl_worker_t *wrk = vcl_worker_get_current ();
537   vppcom_cfg_t *vcl_cfg = &vcm->cfg;
538
539   if (vcl_cfg->vpp_bapi_socket_name)
540     vl_socket_client_disconnect2 (&wrk->bapi_sock_ctx);
541   else
542     vl_client_disconnect_from_vlib ();
543 }
544
545 static const char *
546 vcl_bapi_app_state_str (vcl_bapi_app_state_t state)
547 {
548   char *st;
549
550   switch (state)
551     {
552     case STATE_APP_START:
553       st = "STATE_APP_START";
554       break;
555
556     case STATE_APP_CONN_VPP:
557       st = "STATE_APP_CONN_VPP";
558       break;
559
560     case STATE_APP_ENABLED:
561       st = "STATE_APP_ENABLED";
562       break;
563
564     case STATE_APP_ATTACHED:
565       st = "STATE_APP_ATTACHED";
566       break;
567
568     default:
569       st = "UNKNOWN_APP_STATE";
570       break;
571     }
572
573   return st;
574 }
575
576 static int
577 vcl_bapi_wait_for_wrk_state_change (vcl_bapi_app_state_t app_state)
578 {
579   vcl_worker_t *wrk = vcl_worker_get_current ();
580   f64 timeout = clib_time_now (&wrk->clib_time) + vcm->cfg.app_timeout;
581
582   while (clib_time_now (&wrk->clib_time) < timeout)
583     {
584       if (wrk->bapi_app_state == app_state)
585         return VPPCOM_OK;
586       if (wrk->bapi_app_state == STATE_APP_FAILED)
587         return VPPCOM_ECONNABORTED;
588     }
589   VDBG (0, "timeout waiting for state %s (%d)",
590         vcl_bapi_app_state_str (app_state), app_state);
591   vcl_evt (VCL_EVT_SESSION_TIMEOUT, vcm, bapi_app_state);
592
593   return VPPCOM_ETIMEDOUT;
594 }
595
596 static int
597 vcl_bapi_session_enable (void)
598 {
599   vcl_worker_t *wrk = vcl_worker_get_current ();
600   int rv;
601
602   if (wrk->bapi_app_state != STATE_APP_ENABLED)
603     {
604       vcl_bapi_send_session_enable_disable (1 /* is_enabled == TRUE */ );
605       rv = vcl_bapi_wait_for_wrk_state_change (STATE_APP_ENABLED);
606       if (PREDICT_FALSE (rv))
607         {
608           VDBG (0, "application session enable timed out! returning %d (%s)",
609                 rv, vppcom_retval_str (rv));
610           return rv;
611         }
612     }
613   return VPPCOM_OK;
614 }
615
616 static int
617 vcl_bapi_init (void)
618 {
619   vcl_worker_t *wrk = vcl_worker_get_current ();
620   int rv;
621
622   wrk->bapi_app_state = STATE_APP_START;
623   vcl_bapi_init_error_string_table ();
624   rv = vcl_bapi_connect_to_vpp ();
625   if (rv)
626     {
627       VERR ("couldn't connect to VPP!");
628       return rv;
629     }
630   VDBG (0, "sending session enable");
631   rv = vcl_bapi_session_enable ();
632   if (rv)
633     {
634       VERR ("vppcom_app_session_enable() failed!");
635       return rv;
636     }
637
638   return 0;
639 }
640
641 int
642 vcl_bapi_attach (void)
643 {
644   int rv;
645
646   /* API hookup and connect to VPP */
647   if ((rv = vcl_bapi_init ()))
648     return rv;
649
650   vcl_bapi_send_attach ();
651   rv = vcl_bapi_wait_for_wrk_state_change (STATE_APP_ATTACHED);
652   if (PREDICT_FALSE (rv))
653     {
654       VDBG (0, "application attach timed out! returning %d (%s)", rv,
655             vppcom_retval_str (rv));
656       return rv;
657     }
658
659   return 0;
660 }
661
662 int
663 vcl_bapi_app_worker_add (void)
664 {
665   vcl_worker_t *wrk = vcl_worker_get_current ();
666
667   if (vcl_bapi_connect_to_vpp ())
668     return -1;
669
670   wrk->bapi_app_state = STATE_APP_ADDING_WORKER;
671   vcl_bapi_send_app_worker_add_del (1 /* is_add */ );
672   if (vcl_bapi_wait_for_wrk_state_change (STATE_APP_READY))
673     return -1;
674   return 0;
675 }
676
677 void
678 vcl_bapi_app_worker_del (vcl_worker_t * wrk)
679 {
680   /* Notify vpp that the worker is going away */
681   if (wrk->wrk_index == vcl_get_worker_index ())
682     vcl_bapi_send_app_worker_add_del (0 /* is_add */ );
683   else
684     vcl_bapi_send_child_worker_del (wrk);
685
686   /* Disconnect the binary api */
687   if (vec_len (vcm->workers) == 1)
688     vcl_bapi_disconnect_from_vpp ();
689   else
690     vl_client_send_disconnect (1 /* vpp should cleanup */ );
691 }
692
693 int
694 vcl_bapi_recv_fds (vcl_worker_t * wrk, int *fds, int n_fds)
695 {
696   clib_error_t *err;
697
698   if ((err = vl_socket_client_recv_fd_msg2 (&wrk->bapi_sock_ctx, fds, n_fds,
699                                             5)))
700     {
701       clib_error_report (err);
702       return -1;
703     }
704
705   return 0;
706 }
707
708 int
709 vppcom_session_tls_add_cert (uint32_t session_handle, char *cert,
710                              uint32_t cert_len)
711 {
712
713   vcl_worker_t *wrk = vcl_worker_get_current ();
714   vcl_session_t *session = 0;
715
716   session = vcl_session_get_w_handle (wrk, session_handle);
717   if (!session)
718     return VPPCOM_EBADFD;
719
720   if (cert_len == 0 || cert_len == ~0)
721     return VPPCOM_EBADFD;
722
723   /*
724    * Send listen request to vpp and wait for reply
725    */
726   vcl_bapi_send_application_tls_cert_add (session, cert, cert_len);
727   wrk->bapi_app_state = STATE_APP_ADDING_TLS_DATA;
728   vcl_bapi_wait_for_wrk_state_change (STATE_APP_READY);
729   return VPPCOM_OK;
730 }
731
732 int
733 vppcom_session_tls_add_key (uint32_t session_handle, char *key,
734                             uint32_t key_len)
735 {
736
737   vcl_worker_t *wrk = vcl_worker_get_current ();
738   vcl_session_t *session = 0;
739
740   session = vcl_session_get_w_handle (wrk, session_handle);
741   if (!session)
742     return VPPCOM_EBADFD;
743
744   if (key_len == 0 || key_len == ~0)
745     return VPPCOM_EBADFD;
746
747   vcl_bapi_send_application_tls_key_add (session, key, key_len);
748   wrk->bapi_app_state = STATE_APP_ADDING_TLS_DATA;
749   vcl_bapi_wait_for_wrk_state_change (STATE_APP_READY);
750   return VPPCOM_OK;
751 }
752
753 int
754 vcl_bapi_worker_set (void)
755 {
756   vcl_worker_t *wrk = vcl_worker_get_current ();
757   int i;
758
759   /* Find the first worker with the same pid */
760   for (i = 0; i < vec_len (vcm->workers); i++)
761     {
762       if (i == wrk->wrk_index)
763         continue;
764       if (vcm->workers[i].current_pid == wrk->current_pid)
765         {
766           wrk->vl_input_queue = vcm->workers[i].vl_input_queue;
767           wrk->api_client_handle = vcm->workers[i].api_client_handle;
768           return 0;
769         }
770     }
771   return -1;
772 }
773
774 /*
775  * fd.io coding-style-patch-verification: ON
776  *
777  * Local Variables:
778  * eval: (c-set-style "gnu")
779  * End:
780  */