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