efa5cc170efa71f1dc00b302996bfdc27a655712
[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 u64
65 vcl_vpp_worker_segment_handle (u32 wrk_index)
66 {
67   return (VCL_INVALID_SEGMENT_HANDLE - wrk_index - 1);
68 }
69
70 static void
71 vl_api_app_attach_reply_t_handler (vl_api_app_attach_reply_t * mp)
72 {
73   vcl_worker_t *wrk = vcl_worker_get (0);
74   svm_msg_q_t *ctrl_mq;
75   u64 segment_handle;
76   int *fds = 0, i;
77   u32 n_fds = 0;
78
79   if (mp->retval)
80     {
81       VERR ("attach failed: %U", format_api_error, ntohl (mp->retval));
82       goto failed;
83     }
84
85   wrk->app_event_queue = uword_to_pointer (mp->app_mq, svm_msg_q_t *);
86   ctrl_mq = uword_to_pointer (mp->vpp_ctrl_mq, svm_msg_q_t *);
87   vec_validate (wrk->vpp_event_queues, mp->vpp_ctrl_mq_thread);
88   wrk->vpp_event_queues[mp->vpp_ctrl_mq_thread] = ctrl_mq;
89   vcm->ctrl_mq = wrk->ctrl_mq = ctrl_mq;
90   segment_handle = clib_net_to_host_u64 (mp->segment_handle);
91   if (segment_handle == VCL_INVALID_SEGMENT_HANDLE)
92     {
93       VERR ("invalid segment handle");
94       goto failed;
95     }
96
97   if (mp->n_fds)
98     {
99       vec_validate (fds, mp->n_fds);
100       if (vl_socket_client_recv_fd_msg2 (&wrk->bapi_sock_ctx, fds, mp->n_fds,
101                                          5))
102         goto failed;
103
104       if (mp->fd_flags & SESSION_FD_F_VPP_MQ_SEGMENT)
105         if (vcl_segment_attach (vcl_vpp_worker_segment_handle (0),
106                                 "vpp-mq-seg", SSVM_SEGMENT_MEMFD,
107                                 fds[n_fds++]))
108           goto failed;
109
110       if (mp->fd_flags & SESSION_FD_F_MEMFD_SEGMENT)
111         if (vcl_segment_attach (segment_handle, (char *) mp->segment_name,
112                                 SSVM_SEGMENT_MEMFD, fds[n_fds++]))
113           goto failed;
114
115       if (mp->fd_flags & SESSION_FD_F_MQ_EVENTFD)
116         {
117           svm_msg_q_set_consumer_eventfd (wrk->app_event_queue, fds[n_fds]);
118           vcl_mq_epoll_add_evfd (wrk, wrk->app_event_queue);
119           n_fds++;
120         }
121
122       vec_free (fds);
123     }
124   else
125     {
126       if (vcl_segment_attach (segment_handle, (char *) mp->segment_name,
127                               SSVM_SEGMENT_SHM, -1))
128         goto failed;
129     }
130
131   vcm->app_index = clib_net_to_host_u32 (mp->app_index);
132   vcm->app_state = STATE_APP_ATTACHED;
133   return;
134
135 failed:
136   vcm->app_state = STATE_APP_FAILED;
137   for (i = clib_max (n_fds - 1, 0); i < vec_len (fds); i++)
138     close (fds[i]);
139   vec_free (fds);
140 }
141
142 static void
143 vl_api_app_worker_add_del_reply_t_handler (vl_api_app_worker_add_del_reply_t *
144                                            mp)
145 {
146   int n_fds = 0, *fds = 0, i;
147   u64 segment_handle;
148   vcl_worker_t *wrk;
149   u32 wrk_index;
150
151   if (mp->retval)
152     {
153       clib_warning ("VCL<%d>: add/del worker failed: %U", getpid (),
154                     format_api_error, ntohl (mp->retval));
155       goto failed;
156     }
157
158   if (!mp->is_add)
159     return;
160
161   wrk_index = mp->context;
162   wrk = vcl_worker_get_if_valid (wrk_index);
163   if (!wrk)
164     return;
165
166   wrk->vpp_wrk_index = clib_net_to_host_u32 (mp->wrk_index);
167   wrk->app_event_queue = uword_to_pointer (mp->app_event_queue_address,
168                                            svm_msg_q_t *);
169   wrk->ctrl_mq = vcm->ctrl_mq;
170
171   segment_handle = clib_net_to_host_u64 (mp->segment_handle);
172   if (segment_handle == VCL_INVALID_SEGMENT_HANDLE)
173     {
174       clib_warning ("invalid segment handle");
175       goto failed;
176     }
177
178   if (mp->n_fds)
179     {
180       vec_validate (fds, mp->n_fds);
181       if (vl_socket_client_recv_fd_msg2 (&wrk->bapi_sock_ctx, fds, mp->n_fds,
182                                          5))
183         goto failed;
184
185       if (mp->fd_flags & SESSION_FD_F_VPP_MQ_SEGMENT)
186         if (vcl_segment_attach (vcl_vpp_worker_segment_handle (wrk_index),
187                                 "vpp-worker-seg", SSVM_SEGMENT_MEMFD,
188                                 fds[n_fds++]))
189           goto failed;
190
191       if (mp->fd_flags & SESSION_FD_F_MEMFD_SEGMENT)
192         if (vcl_segment_attach (segment_handle, (char *) mp->segment_name,
193                                 SSVM_SEGMENT_MEMFD, fds[n_fds++]))
194           goto failed;
195
196       if (mp->fd_flags & SESSION_FD_F_MQ_EVENTFD)
197         {
198           svm_msg_q_set_consumer_eventfd (wrk->app_event_queue, fds[n_fds]);
199           vcl_mq_epoll_add_evfd (wrk, wrk->app_event_queue);
200           n_fds++;
201         }
202
203       vec_free (fds);
204     }
205   else
206     {
207       if (vcl_segment_attach (segment_handle, (char *) mp->segment_name,
208                               SSVM_SEGMENT_SHM, -1))
209         goto failed;
210     }
211   vcm->app_state = STATE_APP_READY;
212   VDBG (0, "worker %u vpp-worker %u added", wrk_index, wrk->vpp_wrk_index);
213   return;
214
215 failed:
216   vcm->app_state = STATE_APP_FAILED;
217   for (i = clib_max (n_fds - 1, 0); i < vec_len (fds); i++)
218     close (fds[i]);
219   vec_free (fds);
220 }
221
222 static void
223   vl_api_application_tls_cert_add_reply_t_handler
224   (vl_api_application_tls_cert_add_reply_t * mp)
225 {
226   if (mp->retval)
227     VDBG (0, "add cert failed: %U", format_api_error, ntohl (mp->retval));
228   vcm->app_state = STATE_APP_READY;
229 }
230
231 static void
232   vl_api_application_tls_key_add_reply_t_handler
233   (vl_api_application_tls_key_add_reply_t * mp)
234 {
235   if (mp->retval)
236     VDBG (0, "add key failed: %U", format_api_error, ntohl (mp->retval));
237   vcm->app_state = STATE_APP_READY;
238 }
239
240 #define foreach_sock_msg                                                \
241 _(SESSION_ENABLE_DISABLE_REPLY, session_enable_disable_reply)           \
242 _(APP_ATTACH_REPLY, app_attach_reply)                                   \
243 _(APPLICATION_TLS_CERT_ADD_REPLY, application_tls_cert_add_reply)       \
244 _(APPLICATION_TLS_KEY_ADD_REPLY, application_tls_key_add_reply)         \
245 _(APP_WORKER_ADD_DEL_REPLY, app_worker_add_del_reply)                   \
246
247 void
248 vppcom_api_hookup (void)
249 {
250 #define _(N, n)                                                 \
251     vl_msg_api_set_handlers(VL_API_##N, #n,                     \
252                            vl_api_##n##_t_handler,              \
253                            vl_noop_handler,                     \
254                            vl_api_##n##_t_endian,               \
255                            vl_api_##n##_t_print,                \
256                            sizeof(vl_api_##n##_t), 1);
257   foreach_sock_msg;
258 #undef _
259 }
260
261 /*
262  * VPP-API message functions
263  */
264 void
265 vppcom_send_session_enable_disable (u8 is_enable)
266 {
267   vcl_worker_t *wrk = vcl_worker_get_current ();
268   vl_api_session_enable_disable_t *bmp;
269   bmp = vl_msg_api_alloc (sizeof (*bmp));
270   memset (bmp, 0, sizeof (*bmp));
271
272   bmp->_vl_msg_id = ntohs (VL_API_SESSION_ENABLE_DISABLE);
273   bmp->client_index = wrk->my_client_index;
274   bmp->context = htonl (0xfeedface);
275   bmp->is_enable = is_enable;
276   vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & bmp);
277 }
278
279 void
280 vppcom_app_send_attach (void)
281 {
282   vcl_worker_t *wrk = vcl_worker_get_current ();
283   u8 tls_engine = CRYPTO_ENGINE_OPENSSL;
284   vl_api_app_attach_t *bmp;
285   u8 nsid_len = vec_len (vcm->cfg.namespace_id);
286   u8 app_is_proxy = (vcm->cfg.app_proxy_transport_tcp ||
287                      vcm->cfg.app_proxy_transport_udp);
288
289   tls_engine = vcm->cfg.tls_engine ? vcm->cfg.tls_engine : tls_engine;
290
291   bmp = vl_msg_api_alloc (sizeof (*bmp));
292   memset (bmp, 0, sizeof (*bmp));
293
294   bmp->_vl_msg_id = ntohs (VL_API_APP_ATTACH);
295   bmp->client_index = wrk->my_client_index;
296   bmp->context = htonl (0xfeedface);
297   bmp->options[APP_OPTIONS_FLAGS] =
298     APP_OPTIONS_FLAGS_ACCEPT_REDIRECT | APP_OPTIONS_FLAGS_ADD_SEGMENT |
299     (vcm->cfg.app_scope_local ? APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE : 0) |
300     (vcm->cfg.app_scope_global ? APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE : 0) |
301     (app_is_proxy ? APP_OPTIONS_FLAGS_IS_PROXY : 0) |
302     (vcm->cfg.use_mq_eventfd ? APP_OPTIONS_FLAGS_EVT_MQ_USE_EVENTFD : 0);
303   bmp->options[APP_OPTIONS_PROXY_TRANSPORT] =
304     (u64) ((vcm->cfg.app_proxy_transport_tcp ? 1 << TRANSPORT_PROTO_TCP : 0) |
305            (vcm->cfg.app_proxy_transport_udp ? 1 << TRANSPORT_PROTO_UDP : 0));
306   bmp->options[APP_OPTIONS_SEGMENT_SIZE] = vcm->cfg.segment_size;
307   bmp->options[APP_OPTIONS_ADD_SEGMENT_SIZE] = vcm->cfg.add_segment_size;
308   bmp->options[APP_OPTIONS_RX_FIFO_SIZE] = vcm->cfg.rx_fifo_size;
309   bmp->options[APP_OPTIONS_TX_FIFO_SIZE] = vcm->cfg.tx_fifo_size;
310   bmp->options[APP_OPTIONS_PREALLOC_FIFO_PAIRS] =
311     vcm->cfg.preallocated_fifo_pairs;
312   bmp->options[APP_OPTIONS_EVT_QUEUE_SIZE] = vcm->cfg.event_queue_size;
313   bmp->options[APP_OPTIONS_TLS_ENGINE] = tls_engine;
314   if (nsid_len)
315     {
316       bmp->namespace_id_len = nsid_len;
317       clib_memcpy_fast (bmp->namespace_id, vcm->cfg.namespace_id, nsid_len);
318       bmp->options[APP_OPTIONS_NAMESPACE_SECRET] = vcm->cfg.namespace_secret;
319     }
320   vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & bmp);
321 }
322
323 void
324 vppcom_app_send_detach (void)
325 {
326   vcl_worker_t *wrk = vcl_worker_get_current ();
327   vl_api_application_detach_t *bmp;
328   bmp = vl_msg_api_alloc (sizeof (*bmp));
329   memset (bmp, 0, sizeof (*bmp));
330
331   bmp->_vl_msg_id = ntohs (VL_API_APPLICATION_DETACH);
332   bmp->client_index = wrk->my_client_index;
333   bmp->context = htonl (0xfeedface);
334   vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & bmp);
335 }
336
337 void
338 vcl_send_app_worker_add_del (u8 is_add)
339 {
340   vcl_worker_t *wrk = vcl_worker_get_current ();
341   vl_api_app_worker_add_del_t *mp;
342
343   mp = vl_msg_api_alloc (sizeof (*mp));
344   memset (mp, 0, sizeof (*mp));
345
346   mp->_vl_msg_id = ntohs (VL_API_APP_WORKER_ADD_DEL);
347   mp->client_index = wrk->my_client_index;
348   mp->app_index = clib_host_to_net_u32 (vcm->app_index);
349   mp->context = wrk->wrk_index;
350   mp->is_add = is_add;
351   if (!is_add)
352     mp->wrk_index = clib_host_to_net_u32 (wrk->vpp_wrk_index);
353
354   vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & mp);
355 }
356
357 void
358 vcl_send_child_worker_del (vcl_worker_t * child_wrk)
359 {
360   vcl_worker_t *wrk = vcl_worker_get_current ();
361   vl_api_app_worker_add_del_t *mp;
362
363   mp = vl_msg_api_alloc (sizeof (*mp));
364   memset (mp, 0, sizeof (*mp));
365
366   mp->_vl_msg_id = ntohs (VL_API_APP_WORKER_ADD_DEL);
367   mp->client_index = wrk->my_client_index;
368   mp->app_index = clib_host_to_net_u32 (vcm->app_index);
369   mp->context = wrk->wrk_index;
370   mp->is_add = 0;
371   mp->wrk_index = clib_host_to_net_u32 (child_wrk->vpp_wrk_index);
372
373   vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & mp);
374 }
375
376 void
377 vppcom_send_application_tls_cert_add (vcl_session_t * session, char *cert,
378                                       u32 cert_len)
379 {
380   vcl_worker_t *wrk = vcl_worker_get_current ();
381   vl_api_application_tls_cert_add_t *cert_mp;
382
383   cert_mp = vl_msg_api_alloc (sizeof (*cert_mp) + cert_len);
384   clib_memset (cert_mp, 0, sizeof (*cert_mp));
385   cert_mp->_vl_msg_id = ntohs (VL_API_APPLICATION_TLS_CERT_ADD);
386   cert_mp->client_index = wrk->my_client_index;
387   cert_mp->context = session->session_index;
388   cert_mp->cert_len = clib_host_to_net_u16 (cert_len);
389   clib_memcpy_fast (cert_mp->cert, cert, cert_len);
390   vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & cert_mp);
391 }
392
393 void
394 vppcom_send_application_tls_key_add (vcl_session_t * session, char *key,
395                                      u32 key_len)
396 {
397   vcl_worker_t *wrk = vcl_worker_get_current ();
398   vl_api_application_tls_key_add_t *key_mp;
399
400   key_mp = vl_msg_api_alloc (sizeof (*key_mp) + key_len);
401   clib_memset (key_mp, 0, sizeof (*key_mp));
402   key_mp->_vl_msg_id = ntohs (VL_API_APPLICATION_TLS_KEY_ADD);
403   key_mp->client_index = wrk->my_client_index;
404   key_mp->context = session->session_index;
405   key_mp->key_len = clib_host_to_net_u16 (key_len);
406   clib_memcpy_fast (key_mp->key, key, key_len);
407   vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & key_mp);
408 }
409
410 u32
411 vcl_max_nsid_len (void)
412 {
413   vl_api_application_attach_t *mp;
414   return (sizeof (mp->namespace_id) - 1);
415 }
416
417 void
418 vppcom_init_error_string_table (void)
419 {
420   vcm->error_string_by_error_number = hash_create (0, sizeof (uword));
421
422 #define _(n, v, s) hash_set (vcm->error_string_by_error_number, -v, s);
423   foreach_vnet_api_error;
424 #undef _
425
426   hash_set (vcm->error_string_by_error_number, 99, "Misc");
427 }
428
429 int
430 vppcom_connect_to_vpp (char *app_name)
431 {
432   vcl_worker_t *wrk = vcl_worker_get_current ();
433   vppcom_cfg_t *vcl_cfg = &vcm->cfg;
434   api_main_t *am;
435
436   vlibapi_set_main (&wrk->bapi_api_ctx);
437   vlibapi_set_memory_client_main (&wrk->bapi_shm_ctx);
438   vppcom_api_hookup ();
439
440   if (vcl_cfg->vpp_api_socket_name)
441     {
442       if (vl_socket_client_connect2 (&wrk->bapi_sock_ctx,
443                                      (char *) vcl_cfg->vpp_api_socket_name,
444                                      app_name, 0 /* default rx/tx buffer */ ))
445         {
446           VERR ("app (%s) socket connect failed!", app_name);
447           return VPPCOM_ECONNREFUSED;
448         }
449
450       if (vl_socket_client_init_shm2 (&wrk->bapi_sock_ctx, 0,
451                                       1 /* want_pthread */ ))
452         {
453           VERR ("app (%s) init shm failed!", app_name);
454           return VPPCOM_ECONNREFUSED;
455         }
456     }
457   else
458     {
459       if (!vcl_cfg->vpp_api_filename)
460         vcl_cfg->vpp_api_filename = format (0, "/vpe-api%c", 0);
461
462       vl_set_memory_root_path ((char *) vcl_cfg->vpp_api_chroot);
463
464       VDBG (0, "app (%s) connecting to VPP api (%s)...",
465             app_name, vcl_cfg->vpp_api_filename);
466
467       if (vl_client_connect_to_vlib ((char *) vcl_cfg->vpp_api_filename,
468                                      app_name, vcm->cfg.vpp_api_q_length) < 0)
469         {
470           VERR ("app (%s) connect failed!", app_name);
471           return VPPCOM_ECONNREFUSED;
472         }
473     }
474
475   am = vlibapi_get_main ();
476   wrk->vl_input_queue = am->shmem_hdr->vl_input_queue;
477   wrk->my_client_index = (u32) am->my_client_index;
478   wrk->wrk_state = STATE_APP_CONN_VPP;
479
480   VDBG (0, "app (%s) is connected to VPP!", app_name);
481   vcl_evt (VCL_EVT_INIT, vcm);
482   return VPPCOM_OK;
483 }
484
485 void
486 vppcom_disconnect_from_vpp (void)
487 {
488   vcl_worker_t *wrk = vcl_worker_get_current ();
489   vppcom_cfg_t *vcl_cfg = &vcm->cfg;
490
491   if (vcl_cfg->vpp_api_socket_name)
492     vl_socket_client_disconnect2 (&wrk->bapi_sock_ctx);
493   else
494     vl_client_disconnect_from_vlib ();
495 }
496
497 /*
498  * fd.io coding-style-patch-verification: ON
499  *
500  * Local Variables:
501  * eval: (c-set-style "gnu")
502  * End:
503  */