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