vcl session: switch to generic cert key apis
[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_app_add_cert_key_pair_reply_t_handler (
248   vl_api_app_add_cert_key_pair_reply_t *mp)
249 {
250   vcl_worker_t *wrk = vcl_worker_get_current ();
251
252   if (mp->retval)
253     {
254       VDBG (0, "Adding cert and key failed: %U", format_api_error,
255             ntohl (mp->retval));
256       return;
257     }
258   wrk->bapi_return = clib_net_to_host_u32 (mp->index);
259   wrk->bapi_app_state = STATE_APP_READY;
260 }
261
262 static void
263 vl_api_app_del_cert_key_pair_reply_t_handler (
264   vl_api_app_del_cert_key_pair_reply_t *mp)
265 {
266   if (mp->retval)
267     {
268       VDBG (0, "Deleting cert and key failed: %U", format_api_error,
269             ntohl (mp->retval));
270       return;
271     }
272 }
273
274 #define foreach_sock_msg                                                      \
275   _ (SESSION_ENABLE_DISABLE_REPLY, session_enable_disable_reply)              \
276   _ (APP_ATTACH_REPLY, app_attach_reply)                                      \
277   _ (APP_ADD_CERT_KEY_PAIR_REPLY, app_add_cert_key_pair_reply)                \
278   _ (APP_DEL_CERT_KEY_PAIR_REPLY, app_del_cert_key_pair_reply)                \
279   _ (APP_WORKER_ADD_DEL_REPLY, app_worker_add_del_reply)
280
281 static void
282 vcl_bapi_hookup (void)
283 {
284 #define _(N, n)                                                 \
285     vl_msg_api_set_handlers(VL_API_##N, #n,                     \
286                            vl_api_##n##_t_handler,              \
287                            vl_noop_handler,                     \
288                            vl_api_##n##_t_endian,               \
289                            vl_api_##n##_t_print,                \
290                            sizeof(vl_api_##n##_t), 1);
291   foreach_sock_msg;
292 #undef _
293 }
294
295 /*
296  * VPP-API message functions
297  */
298 static void
299 vcl_bapi_send_session_enable_disable (u8 is_enable)
300 {
301   vcl_worker_t *wrk = vcl_worker_get_current ();
302   vl_api_session_enable_disable_t *bmp;
303   bmp = vl_msg_api_alloc (sizeof (*bmp));
304   memset (bmp, 0, sizeof (*bmp));
305
306   bmp->_vl_msg_id = ntohs (VL_API_SESSION_ENABLE_DISABLE);
307   bmp->client_index = wrk->api_client_handle;
308   bmp->context = htonl (0xfeedface);
309   bmp->is_enable = is_enable;
310   vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & bmp);
311 }
312
313 void
314 vcl_bapi_send_attach (void)
315 {
316   vcl_worker_t *wrk = vcl_worker_get_current ();
317   u8 tls_engine = CRYPTO_ENGINE_OPENSSL;
318   vl_api_app_attach_t *bmp;
319   u8 nsid_len = vec_len (vcm->cfg.namespace_id);
320   u8 app_is_proxy = (vcm->cfg.app_proxy_transport_tcp ||
321                      vcm->cfg.app_proxy_transport_udp);
322
323   tls_engine = vcm->cfg.tls_engine ? vcm->cfg.tls_engine : tls_engine;
324
325   bmp = vl_msg_api_alloc (sizeof (*bmp));
326   memset (bmp, 0, sizeof (*bmp));
327
328   bmp->_vl_msg_id = ntohs (VL_API_APP_ATTACH);
329   bmp->client_index = wrk->api_client_handle;
330   bmp->context = htonl (0xfeedface);
331   bmp->options[APP_OPTIONS_FLAGS] =
332     APP_OPTIONS_FLAGS_ACCEPT_REDIRECT | APP_OPTIONS_FLAGS_ADD_SEGMENT |
333     (vcm->cfg.app_scope_local ? APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE : 0) |
334     (vcm->cfg.app_scope_global ? APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE : 0) |
335     (app_is_proxy ? APP_OPTIONS_FLAGS_IS_PROXY : 0) |
336     (vcm->cfg.use_mq_eventfd ? APP_OPTIONS_FLAGS_EVT_MQ_USE_EVENTFD : 0);
337   bmp->options[APP_OPTIONS_PROXY_TRANSPORT] =
338     (u64) ((vcm->cfg.app_proxy_transport_tcp ? 1 << TRANSPORT_PROTO_TCP : 0) |
339            (vcm->cfg.app_proxy_transport_udp ? 1 << TRANSPORT_PROTO_UDP : 0));
340   bmp->options[APP_OPTIONS_SEGMENT_SIZE] = vcm->cfg.segment_size;
341   bmp->options[APP_OPTIONS_ADD_SEGMENT_SIZE] = vcm->cfg.add_segment_size;
342   bmp->options[APP_OPTIONS_RX_FIFO_SIZE] = vcm->cfg.rx_fifo_size;
343   bmp->options[APP_OPTIONS_TX_FIFO_SIZE] = vcm->cfg.tx_fifo_size;
344   bmp->options[APP_OPTIONS_PREALLOC_FIFO_PAIRS] =
345     vcm->cfg.preallocated_fifo_pairs;
346   bmp->options[APP_OPTIONS_EVT_QUEUE_SIZE] = vcm->cfg.event_queue_size;
347   bmp->options[APP_OPTIONS_TLS_ENGINE] = tls_engine;
348   if (nsid_len)
349     {
350       vl_api_vec_to_api_string (vcm->cfg.namespace_id, &bmp->namespace_id);
351       bmp->options[APP_OPTIONS_NAMESPACE_SECRET] = vcm->cfg.namespace_secret;
352     }
353   vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & bmp);
354 }
355
356 void
357 vcl_bapi_send_detach (void)
358 {
359   vcl_worker_t *wrk = vcl_worker_get_current ();
360   vl_api_application_detach_t *bmp;
361   bmp = vl_msg_api_alloc (sizeof (*bmp));
362   memset (bmp, 0, sizeof (*bmp));
363
364   bmp->_vl_msg_id = ntohs (VL_API_APPLICATION_DETACH);
365   bmp->client_index = wrk->api_client_handle;
366   bmp->context = htonl (0xfeedface);
367   vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & bmp);
368 }
369
370 static void
371 vcl_bapi_send_app_worker_add_del (u8 is_add)
372 {
373   vcl_worker_t *wrk = vcl_worker_get_current ();
374   vl_api_app_worker_add_del_t *mp;
375
376   mp = vl_msg_api_alloc (sizeof (*mp));
377   memset (mp, 0, sizeof (*mp));
378
379   mp->_vl_msg_id = ntohs (VL_API_APP_WORKER_ADD_DEL);
380   mp->client_index = wrk->api_client_handle;
381   mp->app_index = clib_host_to_net_u32 (vcm->app_index);
382   mp->context = wrk->wrk_index;
383   mp->is_add = is_add;
384   if (!is_add)
385     mp->wrk_index = clib_host_to_net_u32 (wrk->vpp_wrk_index);
386
387   vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & mp);
388 }
389
390 static void
391 vcl_bapi_send_child_worker_del (vcl_worker_t * child_wrk)
392 {
393   vcl_worker_t *wrk = vcl_worker_get_current ();
394   vl_api_app_worker_add_del_t *mp;
395
396   mp = vl_msg_api_alloc (sizeof (*mp));
397   memset (mp, 0, sizeof (*mp));
398
399   mp->_vl_msg_id = ntohs (VL_API_APP_WORKER_ADD_DEL);
400   mp->client_index = wrk->api_client_handle;
401   mp->app_index = clib_host_to_net_u32 (vcm->app_index);
402   mp->context = wrk->wrk_index;
403   mp->is_add = 0;
404   mp->wrk_index = clib_host_to_net_u32 (child_wrk->vpp_wrk_index);
405
406   vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & mp);
407 }
408
409 static void
410 vcl_bapi_send_app_add_cert_key_pair (vppcom_cert_key_pair_t *ckpair)
411 {
412   vcl_worker_t *wrk = vcl_worker_get_current ();
413   u32 cert_len = test_srv_crt_rsa_len;
414   u32 key_len = test_srv_key_rsa_len;
415   vl_api_app_add_cert_key_pair_t *bmp;
416
417   bmp = vl_msg_api_alloc (sizeof (*bmp) + cert_len + key_len);
418   clib_memset (bmp, 0, sizeof (*bmp) + cert_len + key_len);
419
420   bmp->_vl_msg_id = ntohs (VL_API_APP_ADD_CERT_KEY_PAIR);
421   bmp->client_index = wrk->api_client_handle;
422   bmp->context = wrk->wrk_index;
423   bmp->cert_len = clib_host_to_net_u16 (cert_len);
424   bmp->certkey_len = clib_host_to_net_u16 (key_len + cert_len);
425   clib_memcpy_fast (bmp->certkey, test_srv_crt_rsa, cert_len);
426   clib_memcpy_fast (bmp->certkey + cert_len, test_srv_key_rsa, key_len);
427
428   vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) &bmp);
429 }
430
431 static void
432 vcl_bapi_send_app_del_cert_key_pair (u32 ckpair_index)
433 {
434   vcl_worker_t *wrk = vcl_worker_get_current ();
435   vl_api_app_del_cert_key_pair_t *bmp;
436   bmp = vl_msg_api_alloc (sizeof (*bmp));
437   clib_memset (bmp, 0, sizeof (*bmp));
438
439   bmp->_vl_msg_id = ntohs (VL_API_APP_DEL_CERT_KEY_PAIR);
440   bmp->client_index = wrk->api_client_handle;
441   bmp->context = wrk->wrk_index;
442   bmp->index = clib_host_to_net_u32 (ckpair_index);
443   vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) &bmp);
444 }
445
446 u32
447 vcl_bapi_max_nsid_len (void)
448 {
449   vl_api_app_attach_t *mp;
450   return (sizeof (mp->namespace_id) - 1);
451 }
452
453 static void
454 vcl_bapi_init_error_string_table (void)
455 {
456   vcm->error_string_by_error_number = hash_create (0, sizeof (uword));
457
458 #define _(n, v, s) hash_set (vcm->error_string_by_error_number, -v, s);
459   foreach_vnet_api_error;
460 #undef _
461
462   hash_set (vcm->error_string_by_error_number, 99, "Misc");
463 }
464
465 static void
466 vcl_bapi_cleanup (void)
467 {
468   socket_client_main_t *scm = &socket_client_main;
469   api_main_t *am = vlibapi_get_main ();
470
471   am->my_client_index = ~0;
472   am->my_registration = 0;
473   am->vl_input_queue = 0;
474   am->msg_index_by_name_and_crc = 0;
475   scm->socket_fd = 0;
476
477   vl_client_api_unmap ();
478 }
479
480 static int
481 vcl_bapi_connect_to_vpp (void)
482 {
483   vcl_worker_t *wrk = vcl_worker_get_current ();
484   vppcom_cfg_t *vcl_cfg = &vcm->cfg;
485   int rv = VPPCOM_OK;
486   api_main_t *am;
487   u8 *wrk_name;
488
489   wrk_name = format (0, "%v-wrk-%u%c", vcm->app_name, wrk->wrk_index, 0);
490
491   /* Make sure api is cleaned up in case this is a connect from a
492    * forked worker */
493   vcl_bapi_cleanup ();
494
495   vlibapi_set_main (&wrk->bapi_api_ctx);
496   vcl_bapi_hookup ();
497
498   if (!vcl_cfg->vpp_bapi_socket_name)
499     {
500       rv = VPPCOM_EINVAL;
501       goto error;
502     }
503
504   if (vl_socket_client_connect2 (&wrk->bapi_sock_ctx,
505                                  (char *) vcl_cfg->vpp_bapi_socket_name,
506                                  (char *) wrk_name,
507                                  0 /* default rx/tx buffer */ ))
508     {
509       VERR ("app (%s) socket connect failed!", wrk_name);
510       rv = VPPCOM_ECONNREFUSED;
511       goto error;
512     }
513
514   if (vl_socket_client_init_shm2 (&wrk->bapi_sock_ctx, 0,
515                                   1 /* want_pthread */ ))
516     {
517       VERR ("app (%s) init shm failed!", wrk_name);
518       rv = VPPCOM_ECONNREFUSED;
519       goto error;
520     }
521
522   am = vlibapi_get_main ();
523   wrk->vl_input_queue = am->shmem_hdr->vl_input_queue;
524   wrk->api_client_handle = (u32) am->my_client_index;
525
526   VDBG (0, "app (%s) is connected to VPP!", wrk_name);
527   vcl_evt (VCL_EVT_INIT, vcm);
528
529 error:
530   vec_free (wrk_name);
531   return rv;
532 }
533
534 void
535 vcl_bapi_disconnect_from_vpp (void)
536 {
537   vcl_worker_t *wrk = vcl_worker_get_current ();
538   vppcom_cfg_t *vcl_cfg = &vcm->cfg;
539
540   if (vcl_cfg->vpp_bapi_socket_name)
541     vl_socket_client_disconnect2 (&wrk->bapi_sock_ctx);
542   else
543     vl_client_disconnect_from_vlib ();
544 }
545
546 static const char *
547 vcl_bapi_app_state_str (vcl_bapi_app_state_t state)
548 {
549   char *st;
550
551   switch (state)
552     {
553     case STATE_APP_START:
554       st = "STATE_APP_START";
555       break;
556
557     case STATE_APP_CONN_VPP:
558       st = "STATE_APP_CONN_VPP";
559       break;
560
561     case STATE_APP_ENABLED:
562       st = "STATE_APP_ENABLED";
563       break;
564
565     case STATE_APP_ATTACHED:
566       st = "STATE_APP_ATTACHED";
567       break;
568
569     default:
570       st = "UNKNOWN_APP_STATE";
571       break;
572     }
573
574   return st;
575 }
576
577 static int
578 vcl_bapi_wait_for_wrk_state_change (vcl_bapi_app_state_t app_state)
579 {
580   vcl_worker_t *wrk = vcl_worker_get_current ();
581   f64 timeout = clib_time_now (&wrk->clib_time) + vcm->cfg.app_timeout;
582
583   while (clib_time_now (&wrk->clib_time) < timeout)
584     {
585       if (wrk->bapi_app_state == app_state)
586         return VPPCOM_OK;
587       if (wrk->bapi_app_state == STATE_APP_FAILED)
588         return VPPCOM_ECONNABORTED;
589     }
590   VDBG (0, "timeout waiting for state %s (%d)",
591         vcl_bapi_app_state_str (app_state), app_state);
592   vcl_evt (VCL_EVT_SESSION_TIMEOUT, vcm, bapi_app_state);
593
594   return VPPCOM_ETIMEDOUT;
595 }
596
597 static int
598 vcl_bapi_session_enable (void)
599 {
600   vcl_worker_t *wrk = vcl_worker_get_current ();
601   int rv;
602
603   if (wrk->bapi_app_state != STATE_APP_ENABLED)
604     {
605       vcl_bapi_send_session_enable_disable (1 /* is_enabled == TRUE */ );
606       rv = vcl_bapi_wait_for_wrk_state_change (STATE_APP_ENABLED);
607       if (PREDICT_FALSE (rv))
608         {
609           VDBG (0, "application session enable timed out! returning %d (%s)",
610                 rv, vppcom_retval_str (rv));
611           return rv;
612         }
613     }
614   return VPPCOM_OK;
615 }
616
617 static int
618 vcl_bapi_init (void)
619 {
620   vcl_worker_t *wrk = vcl_worker_get_current ();
621   int rv;
622
623   wrk->bapi_app_state = STATE_APP_START;
624   vcl_bapi_init_error_string_table ();
625   rv = vcl_bapi_connect_to_vpp ();
626   if (rv)
627     {
628       VERR ("couldn't connect to VPP!");
629       return rv;
630     }
631   VDBG (0, "sending session enable");
632   rv = vcl_bapi_session_enable ();
633   if (rv)
634     {
635       VERR ("vppcom_app_session_enable() failed!");
636       return rv;
637     }
638
639   return 0;
640 }
641
642 int
643 vcl_bapi_attach (void)
644 {
645   int rv;
646
647   /* API hookup and connect to VPP */
648   if ((rv = vcl_bapi_init ()))
649     return rv;
650
651   vcl_bapi_send_attach ();
652   rv = vcl_bapi_wait_for_wrk_state_change (STATE_APP_ATTACHED);
653   if (PREDICT_FALSE (rv))
654     {
655       VDBG (0, "application attach timed out! returning %d (%s)", rv,
656             vppcom_retval_str (rv));
657       return rv;
658     }
659
660   return 0;
661 }
662
663 int
664 vcl_bapi_app_worker_add (void)
665 {
666   vcl_worker_t *wrk = vcl_worker_get_current ();
667
668   if (vcl_bapi_connect_to_vpp ())
669     return -1;
670
671   wrk->bapi_app_state = STATE_APP_ADDING_WORKER;
672   vcl_bapi_send_app_worker_add_del (1 /* is_add */ );
673   if (vcl_bapi_wait_for_wrk_state_change (STATE_APP_READY))
674     return -1;
675   return 0;
676 }
677
678 void
679 vcl_bapi_app_worker_del (vcl_worker_t * wrk)
680 {
681   /* Notify vpp that the worker is going away */
682   if (wrk->wrk_index == vcl_get_worker_index ())
683     vcl_bapi_send_app_worker_add_del (0 /* is_add */ );
684   else
685     vcl_bapi_send_child_worker_del (wrk);
686
687   /* Disconnect the binary api */
688   if (vec_len (vcm->workers) == 1)
689     vcl_bapi_disconnect_from_vpp ();
690   else
691     vl_client_send_disconnect (1 /* vpp should cleanup */ );
692 }
693
694 int
695 vcl_bapi_recv_fds (vcl_worker_t * wrk, int *fds, int n_fds)
696 {
697   clib_error_t *err;
698
699   if ((err = vl_socket_client_recv_fd_msg2 (&wrk->bapi_sock_ctx, fds, n_fds,
700                                             5)))
701     {
702       clib_error_report (err);
703       return -1;
704     }
705
706   return 0;
707 }
708
709 int
710 vcl_bapi_add_cert_key_pair (vppcom_cert_key_pair_t *ckpair)
711 {
712   vcl_worker_t *wrk = vcl_worker_get_current ();
713
714   if (ckpair->key_len == 0 || ckpair->key_len == ~0)
715     return VPPCOM_EINVAL;
716
717   vcl_bapi_send_app_add_cert_key_pair (ckpair);
718   wrk->bapi_app_state = STATE_APP_ADDING_TLS_DATA;
719   vcl_bapi_wait_for_wrk_state_change (STATE_APP_READY);
720   if (wrk->bapi_app_state == STATE_APP_READY)
721     return wrk->bapi_return;
722   return VPPCOM_EFAULT;
723 }
724
725 int
726 vcl_bapi_del_cert_key_pair (u32 ckpair_index)
727 {
728   /* Don't wait for reply */
729   vcl_bapi_send_app_del_cert_key_pair (ckpair_index);
730   return 0;
731 }
732
733 int
734 vcl_bapi_worker_set (void)
735 {
736   vcl_worker_t *wrk = vcl_worker_get_current ();
737   int i;
738
739   /* Find the first worker with the same pid */
740   for (i = 0; i < vec_len (vcm->workers); i++)
741     {
742       if (i == wrk->wrk_index)
743         continue;
744       if (vcm->workers[i].current_pid == wrk->current_pid)
745         {
746           wrk->vl_input_queue = vcm->workers[i].vl_input_queue;
747           wrk->api_client_handle = vcm->workers[i].api_client_handle;
748           return 0;
749         }
750     }
751   return -1;
752 }
753
754 /*
755  * fd.io coding-style-patch-verification: ON
756  *
757  * Local Variables:
758  * eval: (c-set-style "gnu")
759  * End:
760  */