vcl: use per worker mem bapi context
[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       if (mp->fd_flags & SESSION_FD_F_MEMFD_SEGMENT)
104         {
105           segment_name = vl_api_from_api_to_new_c_string (&mp->segment_name);
106           rv =
107             vcl_segment_attach (segment_handle, segment_name,
108                                 SSVM_SEGMENT_MEMFD, fds[n_fds++]);
109           vec_free (segment_name);
110           if (rv != 0)
111             goto failed;
112         }
113
114       vcl_segment_attach_mq (segment_handle, mp->app_mq, 0,
115                              &wrk->app_event_queue);
116
117       if (mp->fd_flags & SESSION_FD_F_MQ_EVENTFD)
118         {
119           svm_msg_q_set_eventfd (wrk->app_event_queue, fds[n_fds]);
120           vcl_mq_epoll_add_evfd (wrk, wrk->app_event_queue);
121           n_fds++;
122         }
123
124       vcl_segment_discover_mqs (vcl_vpp_worker_segment_handle (0), fds + n_fds,
125                                 mp->n_fds - n_fds);
126       vcl_segment_attach_mq (vcl_vpp_worker_segment_handle (0),
127                              mp->vpp_ctrl_mq, mp->vpp_ctrl_mq_thread,
128                              &wrk->ctrl_mq);
129       vcm->ctrl_mq = wrk->ctrl_mq;
130
131       vec_free (fds);
132     }
133   else
134     {
135       segment_name = vl_api_from_api_to_new_c_string (&mp->segment_name);
136       rv =
137         vcl_segment_attach (segment_handle, segment_name, SSVM_SEGMENT_SHM,
138                             -1);
139       vec_free (segment_name);
140       if (rv != 0)
141         goto failed;
142     }
143
144   vcm->app_index = clib_net_to_host_u32 (mp->app_index);
145   wrk->bapi_app_state = STATE_APP_ATTACHED;
146   return;
147
148 failed:
149   wrk->bapi_app_state = STATE_APP_FAILED;
150   for (i = clib_max (n_fds - 1, 0); i < vec_len (fds); i++)
151     close (fds[i]);
152   vec_free (fds);
153 }
154
155 static void
156 vl_api_app_worker_add_del_reply_t_handler (vl_api_app_worker_add_del_reply_t *
157                                            mp)
158 {
159   int n_fds = 0, *fds = 0, i, rv;
160   u64 segment_handle;
161   vcl_worker_t *wrk;
162   u32 wrk_index;
163   char *segment_name = 0;
164
165   if (!mp->is_add)
166     return;
167
168   wrk_index = mp->context;
169   wrk = vcl_worker_get_if_valid (wrk_index);
170   if (!wrk)
171     return;
172
173   if (mp->retval)
174     {
175       clib_warning ("VCL<%d>: add/del worker failed: %U", getpid (),
176                     format_api_error, ntohl (mp->retval));
177       goto failed;
178     }
179
180   vcl_set_worker_index (wrk_index);
181   wrk->vpp_wrk_index = clib_net_to_host_u32 (mp->wrk_index);
182   wrk->ctrl_mq = vcm->ctrl_mq;
183
184   segment_handle = clib_net_to_host_u64 (mp->segment_handle);
185   if (segment_handle == VCL_INVALID_SEGMENT_HANDLE)
186     {
187       clib_warning ("invalid segment handle");
188       goto failed;
189     }
190
191   if (mp->n_fds)
192     {
193       vec_validate (fds, mp->n_fds);
194       if (vl_socket_client_recv_fd_msg2 (&wrk->bapi_sock_ctx, fds, mp->n_fds,
195                                          5))
196         goto failed;
197
198       if (mp->fd_flags & SESSION_FD_F_VPP_MQ_SEGMENT)
199         if (vcl_segment_attach (vcl_vpp_worker_segment_handle (wrk_index),
200                                 "vpp-worker-seg", SSVM_SEGMENT_MEMFD,
201                                 fds[n_fds++]))
202           goto failed;
203
204       if (mp->fd_flags & SESSION_FD_F_MEMFD_SEGMENT)
205         {
206           segment_name = vl_api_from_api_to_new_c_string (&mp->segment_name);
207           rv =
208             vcl_segment_attach (segment_handle, segment_name,
209                                 SSVM_SEGMENT_MEMFD, fds[n_fds++]);
210           vec_free (segment_name);
211           if (rv != 0)
212             goto failed;
213         }
214
215       vcl_segment_attach_mq (segment_handle, mp->app_event_queue_address, 0,
216                              &wrk->app_event_queue);
217
218       if (mp->fd_flags & SESSION_FD_F_MQ_EVENTFD)
219         {
220           svm_msg_q_set_eventfd (wrk->app_event_queue, fds[n_fds]);
221           vcl_mq_epoll_add_evfd (wrk, wrk->app_event_queue);
222           n_fds++;
223         }
224
225       vec_free (fds);
226     }
227   else
228     {
229       segment_name = vl_api_from_api_to_new_c_string (&mp->segment_name);
230       rv =
231         vcl_segment_attach (segment_handle, segment_name, SSVM_SEGMENT_SHM,
232                             -1);
233       vec_free (segment_name);
234       if (rv != 0)
235         goto failed;
236     }
237   wrk->bapi_app_state = STATE_APP_READY;
238   VDBG (0, "worker %u vpp-worker %u added", wrk_index, wrk->vpp_wrk_index);
239   return;
240
241 failed:
242   wrk->bapi_app_state = STATE_APP_FAILED;
243   for (i = clib_max (n_fds - 1, 0); i < vec_len (fds); i++)
244     close (fds[i]);
245   vec_free (fds);
246 }
247
248 static void
249 vl_api_app_add_cert_key_pair_reply_t_handler (
250   vl_api_app_add_cert_key_pair_reply_t *mp)
251 {
252   vcl_worker_t *wrk = vcl_worker_get_current ();
253
254   if (mp->retval)
255     {
256       VDBG (0, "Adding cert and key failed: %U", format_api_error,
257             ntohl (mp->retval));
258       return;
259     }
260   wrk->bapi_return = clib_net_to_host_u32 (mp->index);
261   wrk->bapi_app_state = STATE_APP_READY;
262 }
263
264 static void
265 vl_api_app_del_cert_key_pair_reply_t_handler (
266   vl_api_app_del_cert_key_pair_reply_t *mp)
267 {
268   if (mp->retval)
269     {
270       VDBG (0, "Deleting cert and key failed: %U", format_api_error,
271             ntohl (mp->retval));
272       return;
273     }
274 }
275
276 #define foreach_sock_msg                                                      \
277   _ (SESSION_ENABLE_DISABLE_REPLY, session_enable_disable_reply)              \
278   _ (APP_ATTACH_REPLY, app_attach_reply)                                      \
279   _ (APP_ADD_CERT_KEY_PAIR_REPLY, app_add_cert_key_pair_reply)                \
280   _ (APP_DEL_CERT_KEY_PAIR_REPLY, app_del_cert_key_pair_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 static void
412 vcl_bapi_send_app_add_cert_key_pair (vppcom_cert_key_pair_t *ckpair)
413 {
414   vcl_worker_t *wrk = vcl_worker_get_current ();
415   u32 cert_len = test_srv_crt_rsa_len;
416   u32 key_len = test_srv_key_rsa_len;
417   vl_api_app_add_cert_key_pair_t *bmp;
418
419   bmp = vl_msg_api_alloc (sizeof (*bmp) + cert_len + key_len);
420   clib_memset (bmp, 0, sizeof (*bmp) + cert_len + key_len);
421
422   bmp->_vl_msg_id = ntohs (VL_API_APP_ADD_CERT_KEY_PAIR);
423   bmp->client_index = wrk->api_client_handle;
424   bmp->context = wrk->wrk_index;
425   bmp->cert_len = clib_host_to_net_u16 (cert_len);
426   bmp->certkey_len = clib_host_to_net_u16 (key_len + cert_len);
427   clib_memcpy_fast (bmp->certkey, test_srv_crt_rsa, cert_len);
428   clib_memcpy_fast (bmp->certkey + cert_len, test_srv_key_rsa, key_len);
429
430   vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) &bmp);
431 }
432
433 static void
434 vcl_bapi_send_app_del_cert_key_pair (u32 ckpair_index)
435 {
436   vcl_worker_t *wrk = vcl_worker_get_current ();
437   vl_api_app_del_cert_key_pair_t *bmp;
438   bmp = vl_msg_api_alloc (sizeof (*bmp));
439   clib_memset (bmp, 0, sizeof (*bmp));
440
441   bmp->_vl_msg_id = ntohs (VL_API_APP_DEL_CERT_KEY_PAIR);
442   bmp->client_index = wrk->api_client_handle;
443   bmp->context = wrk->wrk_index;
444   bmp->index = clib_host_to_net_u32 (ckpair_index);
445   vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) &bmp);
446 }
447
448 u32
449 vcl_bapi_max_nsid_len (void)
450 {
451   vl_api_app_attach_t *mp;
452   return (sizeof (mp->namespace_id) - 1);
453 }
454
455 static void
456 vcl_bapi_init_error_string_table (void)
457 {
458   vcm->error_string_by_error_number = hash_create (0, sizeof (uword));
459
460 #define _(n, v, s) hash_set (vcm->error_string_by_error_number, -v, s);
461   foreach_vnet_api_error;
462 #undef _
463
464   hash_set (vcm->error_string_by_error_number, 99, "Misc");
465 }
466
467 static void
468 vcl_bapi_cleanup (void)
469 {
470   socket_client_main_t *scm = &socket_client_main;
471   api_main_t *am = vlibapi_get_main ();
472
473   am->my_client_index = ~0;
474   am->my_registration = 0;
475   am->vl_input_queue = 0;
476   am->msg_index_by_name_and_crc = 0;
477   scm->socket_fd = 0;
478
479   vl_client_api_unmap ();
480 }
481
482 static int
483 vcl_bapi_connect_to_vpp (void)
484 {
485   vcl_worker_t *wrk = vcl_worker_get_current ();
486   vppcom_cfg_t *vcl_cfg = &vcm->cfg;
487   int rv = VPPCOM_OK;
488   api_main_t *am;
489   u8 *wrk_name;
490
491   wrk_name = format (0, "%v-wrk-%u%c", vcm->app_name, wrk->wrk_index, 0);
492
493   /* Make sure api is cleaned up in case this is a connect from a
494    * forked worker */
495   vcl_bapi_cleanup ();
496
497   vlibapi_set_main (&wrk->bapi_api_ctx);
498   vlibapi_set_memory_client_main (&wrk->bapi_mem_ctx);
499   vcl_bapi_hookup ();
500
501   if (!vcl_cfg->vpp_bapi_socket_name)
502     {
503       rv = VPPCOM_EINVAL;
504       goto error;
505     }
506
507   if (vl_socket_client_connect2 (&wrk->bapi_sock_ctx,
508                                  (char *) vcl_cfg->vpp_bapi_socket_name,
509                                  (char *) wrk_name,
510                                  0 /* default rx/tx buffer */ ))
511     {
512       VERR ("app (%s) socket connect failed!", wrk_name);
513       rv = VPPCOM_ECONNREFUSED;
514       goto error;
515     }
516
517   if (vl_socket_client_init_shm2 (&wrk->bapi_sock_ctx, 0,
518                                   1 /* want_pthread */ ))
519     {
520       VERR ("app (%s) init shm failed!", wrk_name);
521       rv = VPPCOM_ECONNREFUSED;
522       goto error;
523     }
524
525   am = vlibapi_get_main ();
526   wrk->vl_input_queue = am->shmem_hdr->vl_input_queue;
527   wrk->api_client_handle = (u32) am->my_client_index;
528
529   VDBG (0, "app (%s) is connected to VPP!", wrk_name);
530   vcl_evt (VCL_EVT_INIT, vcm);
531
532 error:
533   vec_free (wrk_name);
534   return rv;
535 }
536
537 void
538 vcl_bapi_disconnect_from_vpp (void)
539 {
540   vcl_worker_t *wrk = vcl_worker_get_current ();
541   vppcom_cfg_t *vcl_cfg = &vcm->cfg;
542
543   if (vcl_cfg->vpp_bapi_socket_name)
544     vl_socket_client_disconnect2 (&wrk->bapi_sock_ctx);
545   else
546     vl_client_disconnect_from_vlib ();
547 }
548
549 static const char *
550 vcl_bapi_app_state_str (vcl_bapi_app_state_t state)
551 {
552   char *st;
553
554   switch (state)
555     {
556     case STATE_APP_START:
557       st = "STATE_APP_START";
558       break;
559
560     case STATE_APP_CONN_VPP:
561       st = "STATE_APP_CONN_VPP";
562       break;
563
564     case STATE_APP_ENABLED:
565       st = "STATE_APP_ENABLED";
566       break;
567
568     case STATE_APP_ATTACHED:
569       st = "STATE_APP_ATTACHED";
570       break;
571
572     default:
573       st = "UNKNOWN_APP_STATE";
574       break;
575     }
576
577   return st;
578 }
579
580 static int
581 vcl_bapi_wait_for_wrk_state_change (vcl_bapi_app_state_t app_state)
582 {
583   vcl_worker_t *wrk = vcl_worker_get_current ();
584   f64 timeout = clib_time_now (&wrk->clib_time) + vcm->cfg.app_timeout;
585
586   while (clib_time_now (&wrk->clib_time) < timeout)
587     {
588       if (wrk->bapi_app_state == app_state)
589         return VPPCOM_OK;
590       if (wrk->bapi_app_state == STATE_APP_FAILED)
591         return VPPCOM_ECONNABORTED;
592     }
593   VDBG (0, "timeout waiting for state %s (%d)",
594         vcl_bapi_app_state_str (app_state), app_state);
595   vcl_evt (VCL_EVT_SESSION_TIMEOUT, vcm, bapi_app_state);
596
597   return VPPCOM_ETIMEDOUT;
598 }
599
600 static int
601 vcl_bapi_session_enable (void)
602 {
603   vcl_worker_t *wrk = vcl_worker_get_current ();
604   int rv;
605
606   if (wrk->bapi_app_state != STATE_APP_ENABLED)
607     {
608       vcl_bapi_send_session_enable_disable (1 /* is_enabled == TRUE */ );
609       rv = vcl_bapi_wait_for_wrk_state_change (STATE_APP_ENABLED);
610       if (PREDICT_FALSE (rv))
611         {
612           VDBG (0, "application session enable timed out! returning %d (%s)",
613                 rv, vppcom_retval_str (rv));
614           return rv;
615         }
616     }
617   return VPPCOM_OK;
618 }
619
620 static int
621 vcl_bapi_init (void)
622 {
623   vcl_worker_t *wrk = vcl_worker_get_current ();
624   int rv;
625
626   wrk->bapi_app_state = STATE_APP_START;
627   vcl_bapi_init_error_string_table ();
628   rv = vcl_bapi_connect_to_vpp ();
629   if (rv)
630     {
631       VERR ("couldn't connect to VPP!");
632       return rv;
633     }
634   VDBG (0, "sending session enable");
635   rv = vcl_bapi_session_enable ();
636   if (rv)
637     {
638       VERR ("vppcom_app_session_enable() failed!");
639       return rv;
640     }
641
642   return 0;
643 }
644
645 int
646 vcl_bapi_attach (void)
647 {
648   int rv;
649
650   /* API hookup and connect to VPP */
651   if ((rv = vcl_bapi_init ()))
652     return rv;
653
654   vcl_bapi_send_attach ();
655   rv = vcl_bapi_wait_for_wrk_state_change (STATE_APP_ATTACHED);
656   if (PREDICT_FALSE (rv))
657     {
658       VDBG (0, "application attach timed out! returning %d (%s)", rv,
659             vppcom_retval_str (rv));
660       return rv;
661     }
662
663   return 0;
664 }
665
666 int
667 vcl_bapi_app_worker_add (void)
668 {
669   vcl_worker_t *wrk = vcl_worker_get_current ();
670
671   if (vcl_bapi_connect_to_vpp ())
672     return -1;
673
674   wrk->bapi_app_state = STATE_APP_ADDING_WORKER;
675   vcl_bapi_send_app_worker_add_del (1 /* is_add */ );
676   if (vcl_bapi_wait_for_wrk_state_change (STATE_APP_READY))
677     return -1;
678   return 0;
679 }
680
681 void
682 vcl_bapi_app_worker_del (vcl_worker_t * wrk)
683 {
684   /* Notify vpp that the worker is going away */
685   if (wrk->wrk_index == vcl_get_worker_index ())
686     vcl_bapi_send_app_worker_add_del (0 /* is_add */ );
687   else
688     vcl_bapi_send_child_worker_del (wrk);
689
690   /* Disconnect the binary api */
691   if (vec_len (vcm->workers) == 1)
692     vcl_bapi_disconnect_from_vpp ();
693   else
694     vl_client_send_disconnect (1 /* vpp should cleanup */ );
695 }
696
697 int
698 vcl_bapi_recv_fds (vcl_worker_t * wrk, int *fds, int n_fds)
699 {
700   clib_error_t *err;
701
702   if ((err = vl_socket_client_recv_fd_msg2 (&wrk->bapi_sock_ctx, fds, n_fds,
703                                             5)))
704     {
705       clib_error_report (err);
706       return -1;
707     }
708
709   return 0;
710 }
711
712 int
713 vcl_bapi_add_cert_key_pair (vppcom_cert_key_pair_t *ckpair)
714 {
715   vcl_worker_t *wrk = vcl_worker_get_current ();
716
717   if (ckpair->key_len == 0 || ckpair->key_len == ~0)
718     return VPPCOM_EINVAL;
719
720   vcl_bapi_send_app_add_cert_key_pair (ckpair);
721   wrk->bapi_app_state = STATE_APP_ADDING_TLS_DATA;
722   vcl_bapi_wait_for_wrk_state_change (STATE_APP_READY);
723   if (wrk->bapi_app_state == STATE_APP_READY)
724     return wrk->bapi_return;
725   return VPPCOM_EFAULT;
726 }
727
728 int
729 vcl_bapi_del_cert_key_pair (u32 ckpair_index)
730 {
731   /* Don't wait for reply */
732   vcl_bapi_send_app_del_cert_key_pair (ckpair_index);
733   return 0;
734 }
735
736 int
737 vcl_bapi_worker_set (void)
738 {
739   vcl_worker_t *wrk = vcl_worker_get_current ();
740   int i;
741
742   /* Find the first worker with the same pid */
743   for (i = 0; i < vec_len (vcm->workers); i++)
744     {
745       if (i == wrk->wrk_index)
746         continue;
747       if (vcm->workers[i].current_pid == wrk->current_pid)
748         {
749           wrk->vl_input_queue = vcm->workers[i].vl_input_queue;
750           wrk->api_client_handle = vcm->workers[i].api_client_handle;
751           return 0;
752         }
753     }
754   return -1;
755 }
756
757 /*
758  * fd.io coding-style-patch-verification: ON
759  *
760  * Local Variables:
761  * eval: (c-set-style "gnu")
762  * End:
763  */