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