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