Add transport_opts to connect_sock bapi
[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 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   if (mp->retval)
56     {
57       clib_warning ("VCL<%d>: session_enable_disable failed: %U", getpid (),
58                     format_api_error, ntohl (mp->retval));
59     }
60   else
61     vcm->app_state = STATE_APP_ENABLED;
62 }
63
64 static int
65 vcl_segment_attach (u64 segment_handle, char *name, ssvm_segment_type_t type,
66                     int fd)
67 {
68   fifo_segment_create_args_t _a, *a = &_a;
69   int rv;
70
71   memset (a, 0, sizeof (*a));
72   a->segment_name = (char *) name;
73   a->segment_type = type;
74
75   if (type == SSVM_SEGMENT_MEMFD)
76     a->memfd_fd = fd;
77
78   if ((rv = fifo_segment_attach (&vcm->segment_main, a)))
79     {
80       clib_warning ("svm_fifo_segment_attach ('%s') failed", name);
81       return rv;
82     }
83   vcl_segment_table_add (segment_handle, a->new_segment_indices[0]);
84   vec_reset_length (a->new_segment_indices);
85   return 0;
86 }
87
88 static void
89 vcl_segment_detach (u64 segment_handle)
90 {
91   fifo_segment_main_t *sm = &vcm->segment_main;
92   fifo_segment_t *segment;
93   u32 segment_index;
94
95   segment_index = vcl_segment_table_lookup (segment_handle);
96   if (segment_index == (u32) ~ 0)
97     return;
98   segment = fifo_segment_get_segment (sm, segment_index);
99   fifo_segment_delete (sm, segment);
100   vcl_segment_table_del (segment_handle);
101   VDBG (0, "detached segment %u handle %u", segment_index, segment_handle);
102 }
103
104 static u64
105 vcl_vpp_worker_segment_handle (u32 wrk_index)
106 {
107   return (VCL_INVALID_SEGMENT_HANDLE - wrk_index - 1);
108 }
109
110 static void
111 vl_api_application_attach_reply_t_handler (vl_api_application_attach_reply_t *
112                                            mp)
113 {
114   vcl_worker_t *wrk = vcl_worker_get (0);
115   u64 segment_handle;
116   u32 n_fds = 0;
117   int *fds = 0;
118
119   if (mp->retval)
120     {
121       VERR ("attach failed: %U", format_api_error, ntohl (mp->retval));
122       goto failed;
123     }
124
125   wrk->app_event_queue = uword_to_pointer (mp->app_event_queue_address,
126                                            svm_msg_q_t *);
127   segment_handle = clib_net_to_host_u64 (mp->segment_handle);
128   if (segment_handle == VCL_INVALID_SEGMENT_HANDLE)
129     {
130       VERR ("invalid segment handle");
131       goto failed;
132     }
133
134   if (mp->n_fds)
135     {
136       vec_validate (fds, mp->n_fds);
137       vl_socket_client_recv_fd_msg (fds, mp->n_fds, 5);
138
139       if (mp->fd_flags & SESSION_FD_F_VPP_MQ_SEGMENT)
140         if (vcl_segment_attach (vcl_vpp_worker_segment_handle (0),
141                                 "vpp-mq-seg", SSVM_SEGMENT_MEMFD,
142                                 fds[n_fds++]))
143           goto failed;
144
145       if (mp->fd_flags & SESSION_FD_F_MEMFD_SEGMENT)
146         if (vcl_segment_attach (segment_handle, (char *) mp->segment_name,
147                                 SSVM_SEGMENT_MEMFD, fds[n_fds++]))
148           goto failed;
149
150       if (mp->fd_flags & SESSION_FD_F_MQ_EVENTFD)
151         {
152           svm_msg_q_set_consumer_eventfd (wrk->app_event_queue, fds[n_fds]);
153           vcl_mq_epoll_add_evfd (wrk, wrk->app_event_queue);
154           n_fds++;
155         }
156
157       vec_free (fds);
158     }
159   else
160     {
161       if (vcl_segment_attach (segment_handle, (char *) mp->segment_name,
162                               SSVM_SEGMENT_SHM, -1))
163         goto failed;
164     }
165
166   vcm->app_index = clib_net_to_host_u32 (mp->app_index);
167   vcm->app_state = STATE_APP_ATTACHED;
168   return;
169
170 failed:
171   vcm->app_state = STATE_APP_FAILED;
172 }
173
174 static void
175 vl_api_app_worker_add_del_reply_t_handler (vl_api_app_worker_add_del_reply_t *
176                                            mp)
177 {
178   int n_fds = 0, *fds = 0;
179   u64 segment_handle;
180   vcl_worker_t *wrk;
181   u32 wrk_index;
182
183   if (mp->retval)
184     {
185       clib_warning ("VCL<%d>: add/del worker failed: %U", getpid (),
186                     format_api_error, ntohl (mp->retval));
187       goto failed;
188     }
189
190   if (!mp->is_add)
191     return;
192
193   wrk_index = mp->context;
194   wrk = vcl_worker_get_if_valid (wrk_index);
195   if (!wrk)
196     return;
197
198   wrk->vpp_wrk_index = clib_net_to_host_u32 (mp->wrk_index);
199   wrk->app_event_queue = uword_to_pointer (mp->app_event_queue_address,
200                                            svm_msg_q_t *);
201
202   segment_handle = clib_net_to_host_u64 (mp->segment_handle);
203   if (segment_handle == VCL_INVALID_SEGMENT_HANDLE)
204     {
205       clib_warning ("invalid segment handle");
206       goto failed;
207     }
208
209   if (mp->n_fds)
210     {
211       vec_validate (fds, mp->n_fds);
212       vl_socket_client_recv_fd_msg (fds, mp->n_fds, 5);
213
214       if (mp->fd_flags & SESSION_FD_F_VPP_MQ_SEGMENT)
215         if (vcl_segment_attach (vcl_vpp_worker_segment_handle (wrk_index),
216                                 "vpp-worker-seg", SSVM_SEGMENT_MEMFD,
217                                 fds[n_fds++]))
218           goto failed;
219
220       if (mp->fd_flags & SESSION_FD_F_MEMFD_SEGMENT)
221         if (vcl_segment_attach (segment_handle, (char *) mp->segment_name,
222                                 SSVM_SEGMENT_MEMFD, fds[n_fds++]))
223           goto failed;
224
225       if (mp->fd_flags & SESSION_FD_F_MQ_EVENTFD)
226         {
227           svm_msg_q_set_consumer_eventfd (wrk->app_event_queue, fds[n_fds]);
228           vcl_mq_epoll_add_evfd (wrk, wrk->app_event_queue);
229           n_fds++;
230         }
231
232       vec_free (fds);
233     }
234   else
235     {
236       if (vcl_segment_attach (segment_handle, (char *) mp->segment_name,
237                               SSVM_SEGMENT_SHM, -1))
238         goto failed;
239     }
240   vcm->app_state = STATE_APP_READY;
241   VDBG (0, "worker %u vpp-worker %u added", wrk_index, wrk->vpp_wrk_index);
242   return;
243
244 failed:
245   vcm->app_state = STATE_APP_FAILED;
246 }
247
248 static void
249 vl_api_application_detach_reply_t_handler (vl_api_application_detach_reply_t *
250                                            mp)
251 {
252   if (mp->retval)
253     clib_warning ("VCL<%d>: detach failed: %U", getpid (), format_api_error,
254                   ntohl (mp->retval));
255
256   vcm->app_state = STATE_APP_ENABLED;
257 }
258
259 static void
260 vl_api_map_another_segment_t_handler (vl_api_map_another_segment_t * mp)
261 {
262   ssvm_segment_type_t seg_type = SSVM_SEGMENT_SHM;
263   u64 segment_handle;
264   int fd = -1;
265
266   if (mp->fd_flags)
267     {
268       vl_socket_client_recv_fd_msg (&fd, 1, 5);
269       seg_type = SSVM_SEGMENT_MEMFD;
270     }
271
272   segment_handle = clib_net_to_host_u64 (mp->segment_handle);
273   if (segment_handle == VCL_INVALID_SEGMENT_HANDLE)
274     {
275       clib_warning ("invalid segment handle");
276       return;
277     }
278
279   if (vcl_segment_attach (segment_handle, (char *) mp->segment_name,
280                           seg_type, fd))
281     {
282       clib_warning ("VCL<%d>: svm_fifo_segment_attach ('%s') failed",
283                     getpid (), mp->segment_name);
284       return;
285     }
286
287   VDBG (1, "VCL<%d>: mapped new segment '%s' size %d", getpid (),
288         mp->segment_name, mp->segment_size);
289 }
290
291 static void
292 vl_api_unmap_segment_t_handler (vl_api_unmap_segment_t * mp)
293 {
294   u64 segment_handle = clib_net_to_host_u64 (mp->segment_handle);
295   vcl_segment_detach (segment_handle);
296   VDBG (1, "Unmapped segment: %d", segment_handle);
297 }
298
299 static void
300 vl_api_bind_sock_reply_t_handler (vl_api_bind_sock_reply_t * mp)
301 {
302   /* Expecting a similar message on mq. So ignore this */
303   VDBG (0, "bapi bind retval: %u!", mp->retval);
304 }
305
306 static void
307 vl_api_unbind_sock_reply_t_handler (vl_api_unbind_sock_reply_t * mp)
308 {
309   if (mp->retval)
310     VDBG (0, "ERROR: sid %u: unbind failed: %U", mp->context,
311           format_api_error, ntohl (mp->retval));
312
313   VDBG (1, "sid %u: unbind succeeded!", mp->context);
314
315 }
316
317 static void
318 vl_api_disconnect_session_reply_t_handler (vl_api_disconnect_session_reply_t *
319                                            mp)
320 {
321   if (mp->retval)
322     VDBG (0, "ERROR: sid %u: disconnect failed: %U", mp->context,
323           format_api_error, ntohl (mp->retval));
324 }
325
326 static void
327 vl_api_connect_sock_reply_t_handler (vl_api_connect_sock_reply_t * mp)
328 {
329   if (mp->retval)
330     VDBG (0, "ERROR: connect failed: %U", format_api_error,
331           ntohl (mp->retval));
332 }
333
334 static void
335   vl_api_application_tls_cert_add_reply_t_handler
336   (vl_api_application_tls_cert_add_reply_t * mp)
337 {
338   if (mp->retval)
339     {
340       clib_warning ("VCL<%d>: add cert failed: %U", getpid (),
341                     format_api_error, ntohl (mp->retval));
342       return;
343     }
344 }
345
346 static void
347   vl_api_application_tls_key_add_reply_t_handler
348   (vl_api_application_tls_key_add_reply_t * mp)
349 {
350   if (mp->retval)
351     {
352       clib_warning ("VCL<%d>: add key failed: %U", getpid (),
353                     format_api_error, ntohl (mp->retval));
354       return;
355     }
356
357 }
358
359 #define foreach_sock_msg                                                \
360 _(SESSION_ENABLE_DISABLE_REPLY, session_enable_disable_reply)           \
361 _(BIND_SOCK_REPLY, bind_sock_reply)                                     \
362 _(UNBIND_SOCK_REPLY, unbind_sock_reply)                                 \
363 _(CONNECT_SOCK_REPLY, connect_sock_reply)                               \
364 _(DISCONNECT_SESSION_REPLY, disconnect_session_reply)                   \
365 _(APPLICATION_ATTACH_REPLY, application_attach_reply)                   \
366 _(APPLICATION_DETACH_REPLY, application_detach_reply)                   \
367 _(APPLICATION_TLS_CERT_ADD_REPLY, application_tls_cert_add_reply)       \
368 _(APPLICATION_TLS_KEY_ADD_REPLY, application_tls_key_add_reply)         \
369 _(MAP_ANOTHER_SEGMENT, map_another_segment)                             \
370 _(UNMAP_SEGMENT, unmap_segment)                                         \
371 _(APP_WORKER_ADD_DEL_REPLY, app_worker_add_del_reply)                   \
372
373 void
374 vppcom_api_hookup (void)
375 {
376 #define _(N, n)                                                 \
377     vl_msg_api_set_handlers(VL_API_##N, #n,                     \
378                            vl_api_##n##_t_handler,              \
379                            vl_noop_handler,                     \
380                            vl_api_##n##_t_endian,               \
381                            vl_api_##n##_t_print,                \
382                            sizeof(vl_api_##n##_t), 1);
383   foreach_sock_msg;
384 #undef _
385 }
386
387 /*
388  * VPP-API message functions
389  */
390 void
391 vppcom_send_session_enable_disable (u8 is_enable)
392 {
393   vcl_worker_t *wrk = vcl_worker_get_current ();
394   vl_api_session_enable_disable_t *bmp;
395   bmp = vl_msg_api_alloc (sizeof (*bmp));
396   memset (bmp, 0, sizeof (*bmp));
397
398   bmp->_vl_msg_id = ntohs (VL_API_SESSION_ENABLE_DISABLE);
399   bmp->client_index = wrk->my_client_index;
400   bmp->context = htonl (0xfeedface);
401   bmp->is_enable = is_enable;
402   vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & bmp);
403 }
404
405 void
406 vppcom_app_send_attach (void)
407 {
408   vcl_worker_t *wrk = vcl_worker_get_current ();
409   vl_api_application_attach_t *bmp;
410   u8 nsid_len = vec_len (vcm->cfg.namespace_id);
411   u8 app_is_proxy = (vcm->cfg.app_proxy_transport_tcp ||
412                      vcm->cfg.app_proxy_transport_udp);
413
414   bmp = vl_msg_api_alloc (sizeof (*bmp));
415   memset (bmp, 0, sizeof (*bmp));
416
417   bmp->_vl_msg_id = ntohs (VL_API_APPLICATION_ATTACH);
418   bmp->client_index = wrk->my_client_index;
419   bmp->context = htonl (0xfeedface);
420   bmp->options[APP_OPTIONS_FLAGS] =
421     APP_OPTIONS_FLAGS_ACCEPT_REDIRECT | APP_OPTIONS_FLAGS_ADD_SEGMENT |
422     (vcm->cfg.app_scope_local ? APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE : 0) |
423     (vcm->cfg.app_scope_global ? APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE : 0) |
424     (app_is_proxy ? APP_OPTIONS_FLAGS_IS_PROXY : 0) |
425     (vcm->cfg.use_mq_eventfd ? APP_OPTIONS_FLAGS_EVT_MQ_USE_EVENTFD : 0);
426   bmp->options[APP_OPTIONS_PROXY_TRANSPORT] =
427     (u64) ((vcm->cfg.app_proxy_transport_tcp ? 1 << TRANSPORT_PROTO_TCP : 0) |
428            (vcm->cfg.app_proxy_transport_udp ? 1 << TRANSPORT_PROTO_UDP : 0));
429   bmp->options[APP_OPTIONS_SEGMENT_SIZE] = vcm->cfg.segment_size;
430   bmp->options[APP_OPTIONS_ADD_SEGMENT_SIZE] = vcm->cfg.add_segment_size;
431   bmp->options[APP_OPTIONS_RX_FIFO_SIZE] = vcm->cfg.rx_fifo_size;
432   bmp->options[APP_OPTIONS_TX_FIFO_SIZE] = vcm->cfg.tx_fifo_size;
433   bmp->options[APP_OPTIONS_PREALLOC_FIFO_PAIRS] =
434     vcm->cfg.preallocated_fifo_pairs;
435   bmp->options[APP_OPTIONS_EVT_QUEUE_SIZE] = vcm->cfg.event_queue_size;
436   bmp->options[APP_OPTIONS_TLS_ENGINE] = TLS_ENGINE_OPENSSL;
437   if (nsid_len)
438     {
439       bmp->namespace_id_len = nsid_len;
440       clib_memcpy_fast (bmp->namespace_id, vcm->cfg.namespace_id, nsid_len);
441       bmp->options[APP_OPTIONS_NAMESPACE_SECRET] = vcm->cfg.namespace_secret;
442     }
443   vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & bmp);
444 }
445
446 void
447 vppcom_app_send_detach (void)
448 {
449   vcl_worker_t *wrk = vcl_worker_get_current ();
450   vl_api_application_detach_t *bmp;
451   bmp = vl_msg_api_alloc (sizeof (*bmp));
452   memset (bmp, 0, sizeof (*bmp));
453
454   bmp->_vl_msg_id = ntohs (VL_API_APPLICATION_DETACH);
455   bmp->client_index = wrk->my_client_index;
456   bmp->context = htonl (0xfeedface);
457   vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & bmp);
458 }
459
460 void
461 vcl_send_app_worker_add_del (u8 is_add)
462 {
463   vcl_worker_t *wrk = vcl_worker_get_current ();
464   vl_api_app_worker_add_del_t *mp;
465
466   mp = vl_msg_api_alloc (sizeof (*mp));
467   memset (mp, 0, sizeof (*mp));
468
469   mp->_vl_msg_id = ntohs (VL_API_APP_WORKER_ADD_DEL);
470   mp->client_index = wrk->my_client_index;
471   mp->app_index = clib_host_to_net_u32 (vcm->app_index);
472   mp->context = wrk->wrk_index;
473   mp->is_add = is_add;
474   if (!is_add)
475     mp->wrk_index = clib_host_to_net_u32 (wrk->vpp_wrk_index);
476
477   vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & mp);
478 }
479
480 void
481 vcl_send_child_worker_del (vcl_worker_t * child_wrk)
482 {
483   vcl_worker_t *wrk = vcl_worker_get_current ();
484   vl_api_app_worker_add_del_t *mp;
485
486   mp = vl_msg_api_alloc (sizeof (*mp));
487   memset (mp, 0, sizeof (*mp));
488
489   mp->_vl_msg_id = ntohs (VL_API_APP_WORKER_ADD_DEL);
490   mp->client_index = wrk->my_client_index;
491   mp->app_index = clib_host_to_net_u32 (vcm->app_index);
492   mp->context = wrk->wrk_index;
493   mp->is_add = 0;
494   mp->wrk_index = clib_host_to_net_u32 (child_wrk->vpp_wrk_index);
495
496   vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & mp);
497 }
498
499 void
500 vppcom_send_connect_sock (vcl_session_t * session)
501 {
502   vcl_worker_t *wrk = vcl_worker_get_current ();
503   vl_api_connect_sock_t *cmp;
504
505   cmp = vl_msg_api_alloc (sizeof (*cmp));
506   memset (cmp, 0, sizeof (*cmp));
507   cmp->_vl_msg_id = ntohs (VL_API_CONNECT_SOCK);
508   cmp->client_index = wrk->my_client_index;
509   cmp->context = session->session_index;
510   cmp->wrk_index = wrk->vpp_wrk_index;
511   cmp->is_ip4 = session->transport.is_ip4;
512   cmp->transport_opts = session->transport_opts;
513   clib_memcpy_fast (cmp->ip, &session->transport.rmt_ip, sizeof (cmp->ip));
514   cmp->port = session->transport.rmt_port;
515   cmp->proto = session->session_type;
516   vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & cmp);
517 }
518
519 void
520 vppcom_send_disconnect_session (u64 vpp_handle)
521 {
522   vcl_worker_t *wrk = vcl_worker_get_current ();
523   vl_api_disconnect_session_t *dmp;
524
525   dmp = vl_msg_api_alloc (sizeof (*dmp));
526   memset (dmp, 0, sizeof (*dmp));
527   dmp->_vl_msg_id = ntohs (VL_API_DISCONNECT_SESSION);
528   dmp->client_index = wrk->my_client_index;
529   dmp->handle = vpp_handle;
530   vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & dmp);
531 }
532
533 /* VPP combines bind and listen as one operation. VCL manages the separation
534  * of bind and listen locally via vppcom_session_bind() and
535  * vppcom_session_listen() */
536 void
537 vppcom_send_bind_sock (vcl_session_t * session)
538 {
539   vcl_worker_t *wrk = vcl_worker_get_current ();
540   vl_api_bind_sock_t *bmp;
541
542   /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
543   bmp = vl_msg_api_alloc (sizeof (*bmp));
544   memset (bmp, 0, sizeof (*bmp));
545
546   bmp->_vl_msg_id = ntohs (VL_API_BIND_SOCK);
547   bmp->client_index = wrk->my_client_index;
548   bmp->context = session->session_index;
549   bmp->wrk_index = wrk->vpp_wrk_index;
550   bmp->is_ip4 = session->transport.is_ip4;
551   clib_memcpy_fast (bmp->ip, &session->transport.lcl_ip, sizeof (bmp->ip));
552   bmp->port = session->transport.lcl_port;
553   bmp->proto = session->session_type;
554   vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & bmp);
555 }
556
557 void
558 vppcom_send_unbind_sock (vcl_worker_t * wrk, u64 vpp_handle)
559 {
560   vl_api_unbind_sock_t *ump;
561
562   ump = vl_msg_api_alloc (sizeof (*ump));
563   memset (ump, 0, sizeof (*ump));
564
565   ump->_vl_msg_id = ntohs (VL_API_UNBIND_SOCK);
566   ump->client_index = wrk->my_client_index;
567   ump->wrk_index = wrk->vpp_wrk_index;
568   ump->handle = vpp_handle;
569   ump->context = wrk->wrk_index;
570   vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & ump);
571 }
572
573 void
574 vppcom_send_application_tls_cert_add (vcl_session_t * session, char *cert,
575                                       u32 cert_len)
576 {
577   vcl_worker_t *wrk = vcl_worker_get_current ();
578   vl_api_application_tls_cert_add_t *cert_mp;
579
580   cert_mp = vl_msg_api_alloc (sizeof (*cert_mp) + cert_len);
581   clib_memset (cert_mp, 0, sizeof (*cert_mp));
582   cert_mp->_vl_msg_id = ntohs (VL_API_APPLICATION_TLS_CERT_ADD);
583   cert_mp->client_index = wrk->my_client_index;
584   cert_mp->context = session->session_index;
585   cert_mp->cert_len = clib_host_to_net_u16 (cert_len);
586   clib_memcpy_fast (cert_mp->cert, cert, cert_len);
587   vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & cert_mp);
588
589 }
590
591 void
592 vppcom_send_application_tls_key_add (vcl_session_t * session, char *key,
593                                      u32 key_len)
594 {
595   vcl_worker_t *wrk = vcl_worker_get_current ();
596   vl_api_application_tls_key_add_t *key_mp;
597
598   key_mp = vl_msg_api_alloc (sizeof (*key_mp) + key_len);
599   clib_memset (key_mp, 0, sizeof (*key_mp));
600   key_mp->_vl_msg_id = ntohs (VL_API_APPLICATION_TLS_KEY_ADD);
601   key_mp->client_index = wrk->my_client_index;
602   key_mp->context = session->session_index;
603   key_mp->key_len = clib_host_to_net_u16 (key_len);
604   clib_memcpy_fast (key_mp->key, key, key_len);
605   vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & key_mp);
606
607 }
608
609 u32
610 vcl_max_nsid_len (void)
611 {
612   vl_api_application_attach_t *mp;
613   return (sizeof (mp->namespace_id) - 1);
614 }
615
616 void
617 vppcom_init_error_string_table (void)
618 {
619   vcm->error_string_by_error_number = hash_create (0, sizeof (uword));
620
621 #define _(n, v, s) hash_set (vcm->error_string_by_error_number, -v, s);
622   foreach_vnet_api_error;
623 #undef _
624
625   hash_set (vcm->error_string_by_error_number, 99, "Misc");
626 }
627
628 int
629 vppcom_connect_to_vpp (char *app_name)
630 {
631   vcl_worker_t *wrk = vcl_worker_get_current ();
632   api_main_t *am = &api_main;
633   vppcom_cfg_t *vcl_cfg = &vcm->cfg;
634
635   if (vcl_cfg->vpp_api_socket_name)
636     {
637       if (vl_socket_client_connect ((char *) vcl_cfg->vpp_api_socket_name,
638                                     app_name, 0 /* default rx/tx buffer */ ))
639         {
640           VERR ("app (%s) socket connect failed!", app_name);
641           return VPPCOM_ECONNREFUSED;
642         }
643
644       if (vl_socket_client_init_shm (0, 1 /* want_pthread */ ))
645         {
646           VERR ("app (%s) init shm failed!", app_name);
647           return VPPCOM_ECONNREFUSED;
648         }
649     }
650   else
651     {
652       if (!vcl_cfg->vpp_api_filename)
653         vcl_cfg->vpp_api_filename = format (0, "/vpe-api%c", 0);
654
655       VDBG (0, "app (%s) connecting to VPP api (%s)...",
656             app_name, vcl_cfg->vpp_api_filename);
657
658       if (vl_client_connect_to_vlib ((char *) vcl_cfg->vpp_api_filename,
659                                      app_name, vcm->cfg.vpp_api_q_length) < 0)
660         {
661           VERR ("app (%s) connect failed!", app_name);
662           return VPPCOM_ECONNREFUSED;
663         }
664
665     }
666
667   wrk->vl_input_queue = am->shmem_hdr->vl_input_queue;
668   wrk->my_client_index = (u32) am->my_client_index;
669   wrk->wrk_state = STATE_APP_CONN_VPP;
670
671   VDBG (0, "app (%s) is connected to VPP!", app_name);
672   vcl_evt (VCL_EVT_INIT, vcm);
673   return VPPCOM_OK;
674 }
675
676 /*
677  * fd.io coding-style-patch-verification: ON
678  *
679  * Local Variables:
680  * eval: (c-set-style "gnu")
681  * End:
682  */