vcl: remove unused binary api handlers
[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 ssvm_segment_attach (char *name, ssvm_segment_type_t type, int fd)
66 {
67   svm_fifo_segment_create_args_t _a, *a = &_a;
68   int rv;
69
70   memset (a, 0, sizeof (*a));
71   a->segment_name = (char *) name;
72   a->segment_type = type;
73
74   if (type == SSVM_SEGMENT_MEMFD)
75     a->memfd_fd = fd;
76
77   if ((rv = svm_fifo_segment_attach (a)))
78     {
79       clib_warning ("svm_fifo_segment_attach ('%s') failed", name);
80       return rv;
81     }
82   vec_reset_length (a->new_segment_indices);
83   return 0;
84 }
85
86 static void
87 vl_api_application_attach_reply_t_handler (vl_api_application_attach_reply_t *
88                                            mp)
89 {
90   u32 n_fds = 0;
91   int *fds = 0;
92
93   if (mp->retval)
94     {
95       clib_warning ("VCL<%d>: attach failed: %U", getpid (),
96                     format_api_error, ntohl (mp->retval));
97       return;
98     }
99
100   vcm->app_event_queue = uword_to_pointer (mp->app_event_queue_address,
101                                            svm_msg_q_t *);
102   if (mp->n_fds)
103     {
104       vec_validate (fds, mp->n_fds);
105       vl_socket_client_recv_fd_msg (fds, mp->n_fds, 5);
106
107       if (mp->fd_flags & SESSION_FD_F_VPP_MQ_SEGMENT)
108         if (ssvm_segment_attach ("vpp-mq-seg", SSVM_SEGMENT_MEMFD,
109                                  fds[n_fds++]))
110           return;
111
112       if (mp->fd_flags & SESSION_FD_F_MEMFD_SEGMENT)
113         if (ssvm_segment_attach ((char *) mp->segment_name,
114                                  SSVM_SEGMENT_MEMFD, fds[n_fds++]))
115           return;
116
117       if (mp->fd_flags & SESSION_FD_F_MQ_EVENTFD)
118         {
119           svm_msg_q_set_consumer_eventfd (vcm->app_event_queue, fds[n_fds]);
120           if (vcm->mqs_epfd < 0)
121             clib_unix_warning ("epoll_create() returned");
122           vcl_mq_epoll_add_evfd (vcm->app_event_queue);
123           n_fds++;
124         }
125
126       vec_free (fds);
127     }
128   else
129     {
130       if (ssvm_segment_attach ((char *) mp->segment_name, SSVM_SEGMENT_SHM,
131                                -1))
132         return;
133     }
134
135   vcm->app_state = STATE_APP_ATTACHED;
136 }
137
138 static void
139 vl_api_application_detach_reply_t_handler (vl_api_application_detach_reply_t *
140                                            mp)
141 {
142   if (mp->retval)
143     clib_warning ("VCL<%d>: detach failed: %U", getpid (), format_api_error,
144                   ntohl (mp->retval));
145
146   vcm->app_state = STATE_APP_ENABLED;
147 }
148
149 static void
150 vl_api_map_another_segment_t_handler (vl_api_map_another_segment_t * mp)
151 {
152   ssvm_segment_type_t seg_type = SSVM_SEGMENT_SHM;
153   int fd = -1;
154
155   vcm->mounting_segment = 1;
156
157   if (mp->fd_flags)
158     {
159       vl_socket_client_recv_fd_msg (&fd, 1, 5);
160       seg_type = SSVM_SEGMENT_MEMFD;
161     }
162
163   if (PREDICT_FALSE (ssvm_segment_attach ((char *) mp->segment_name,
164                                           seg_type, fd)))
165     {
166       clib_warning ("VCL<%d>: svm_fifo_segment_attach ('%s') failed",
167                     getpid (), mp->segment_name);
168       return;
169     }
170
171   VDBG (1, "VCL<%d>: mapped new segment '%s' size %d", getpid (),
172         mp->segment_name, mp->segment_size);
173   vcm->mounting_segment = 0;
174 }
175
176 static void
177 vl_api_unmap_segment_t_handler (vl_api_unmap_segment_t * mp)
178 {
179
180 /*
181  * XXX Need segment_name to session_id hash,
182  * XXX - have sessionID by handle hash currently
183  */
184
185   VDBG (1, "Unmapped segment '%s'", mp->segment_name);
186 }
187
188 static void
189   vl_api_app_cut_through_registration_add_t_handler
190   (vl_api_app_cut_through_registration_add_t * mp)
191 {
192   vcl_cut_through_registration_t *ctr;
193   u32 mqc_index = ~0;
194   int *fds = 0;
195
196   if (mp->n_fds)
197     {
198       ASSERT (mp->n_fds == 2);
199       vec_validate (fds, mp->n_fds);
200       vl_socket_client_recv_fd_msg (fds, mp->n_fds, 5);
201     }
202
203   ctr = vcl_ct_registration_lock_and_alloc ();
204   ctr->mq = uword_to_pointer (mp->evt_q_address, svm_msg_q_t *);
205   ctr->peer_mq = uword_to_pointer (mp->peer_evt_q_address, svm_msg_q_t *);
206   VDBG (0, "Adding ct registration %u", vcl_ct_registration_index (ctr));
207
208   if (mp->n_fds && (mp->fd_flags & SESSION_FD_F_MQ_EVENTFD))
209     {
210       svm_msg_q_set_consumer_eventfd (ctr->mq, fds[0]);
211       svm_msg_q_set_producer_eventfd (ctr->peer_mq, fds[1]);
212       mqc_index = vcl_mq_epoll_add_evfd (ctr->mq);
213       ctr->epoll_evt_conn_index = mqc_index;
214       vec_free (fds);
215     }
216   vcl_ct_registration_lookup_add (mp->evt_q_address,
217                                   vcl_ct_registration_index (ctr));
218   vcl_ct_registration_unlock ();
219 }
220
221 static void
222 vl_api_bind_sock_reply_t_handler (vl_api_bind_sock_reply_t * mp)
223 {
224   vcl_session_t *session = 0;
225   u32 session_index = mp->context;
226   int rv;
227
228   VCL_SESSION_LOCK_AND_GET (session_index, &session);
229 done:
230   if (mp->retval)
231     {
232       clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, "
233                     "sid %u: bind failed: %U",
234                     getpid (), mp->handle, session_index,
235                     format_api_error, ntohl (mp->retval));
236       rv = vppcom_session_at_index (session_index, &session);
237       if (rv == VPPCOM_OK)
238         {
239           session->session_state = STATE_FAILED;
240           session->vpp_handle = mp->handle;
241         }
242       else
243         {
244           clib_warning ("[%s] ERROR: vpp handle 0x%llx, sid %u: "
245                         "Invalid session index (%u)!",
246                         getpid (), mp->handle, session_index);
247         }
248       goto done_unlock;
249     }
250
251   session->vpp_handle = mp->handle;
252   session->transport.is_ip4 = mp->lcl_is_ip4;
253   clib_memcpy (&session->transport.lcl_ip, mp->lcl_ip,
254                sizeof (ip46_address_t));
255   session->transport.lcl_port = mp->lcl_port;
256   vppcom_session_table_add_listener (mp->handle, session_index);
257   session->session_state = STATE_LISTEN;
258
259   if (session->is_dgram)
260     {
261       svm_fifo_t *rx_fifo, *tx_fifo;
262       session->vpp_evt_q = uword_to_pointer (mp->vpp_evt_q, svm_msg_q_t *);
263       rx_fifo = uword_to_pointer (mp->rx_fifo, svm_fifo_t *);
264       rx_fifo->client_session_index = session_index;
265       tx_fifo = uword_to_pointer (mp->tx_fifo, svm_fifo_t *);
266       tx_fifo->client_session_index = session_index;
267       session->rx_fifo = rx_fifo;
268       session->tx_fifo = tx_fifo;
269     }
270
271   VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: bind succeeded!",
272         getpid (), mp->handle, mp->context);
273 done_unlock:
274   VCL_SESSION_UNLOCK ();
275 }
276
277 static void
278 vl_api_unbind_sock_reply_t_handler (vl_api_unbind_sock_reply_t * mp)
279 {
280   if (mp->retval)
281     clib_warning ("VCL<%d>: ERROR: sid %u: unbind failed: %U",
282                   getpid (), mp->context, format_api_error,
283                   ntohl (mp->retval));
284
285   else
286     VDBG (1, "VCL<%d>: sid %u: unbind succeeded!", getpid (), mp->context);
287 }
288
289 #define foreach_sock_msg                                                \
290 _(SESSION_ENABLE_DISABLE_REPLY, session_enable_disable_reply)           \
291 _(BIND_SOCK_REPLY, bind_sock_reply)                                     \
292 _(UNBIND_SOCK_REPLY, unbind_sock_reply)                                 \
293 _(APPLICATION_ATTACH_REPLY, application_attach_reply)                   \
294 _(APPLICATION_DETACH_REPLY, application_detach_reply)                   \
295 _(MAP_ANOTHER_SEGMENT, map_another_segment)                             \
296 _(UNMAP_SEGMENT, unmap_segment)                                         \
297 _(APP_CUT_THROUGH_REGISTRATION_ADD, app_cut_through_registration_add)   \
298
299 void
300 vppcom_api_hookup (void)
301 {
302 #define _(N, n)                                                  \
303     vl_msg_api_set_handlers(VL_API_##N, #n,                     \
304                            vl_api_##n##_t_handler,              \
305                            vl_noop_handler,                     \
306                            vl_api_##n##_t_endian,               \
307                            vl_api_##n##_t_print,                \
308                            sizeof(vl_api_##n##_t), 1);
309   foreach_sock_msg;
310 #undef _
311 }
312
313 /*
314  * VPP-API message functions
315  */
316 void
317 vppcom_send_session_enable_disable (u8 is_enable)
318 {
319   vl_api_session_enable_disable_t *bmp;
320   bmp = vl_msg_api_alloc (sizeof (*bmp));
321   memset (bmp, 0, sizeof (*bmp));
322
323   bmp->_vl_msg_id = ntohs (VL_API_SESSION_ENABLE_DISABLE);
324   bmp->client_index = vcm->my_client_index;
325   bmp->context = htonl (0xfeedface);
326   bmp->is_enable = is_enable;
327   vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & bmp);
328 }
329
330 void
331 vppcom_app_send_attach (void)
332 {
333   vl_api_application_attach_t *bmp;
334   u8 nsid_len = vec_len (vcm->cfg.namespace_id);
335   u8 app_is_proxy = (vcm->cfg.app_proxy_transport_tcp ||
336                      vcm->cfg.app_proxy_transport_udp);
337
338   bmp = vl_msg_api_alloc (sizeof (*bmp));
339   memset (bmp, 0, sizeof (*bmp));
340
341   bmp->_vl_msg_id = ntohs (VL_API_APPLICATION_ATTACH);
342   bmp->client_index = vcm->my_client_index;
343   bmp->context = htonl (0xfeedface);
344   bmp->options[APP_OPTIONS_FLAGS] =
345     APP_OPTIONS_FLAGS_ACCEPT_REDIRECT | APP_OPTIONS_FLAGS_ADD_SEGMENT |
346     (vcm->cfg.app_scope_local ? APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE : 0) |
347     (vcm->cfg.app_scope_global ? APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE : 0) |
348     (app_is_proxy ? APP_OPTIONS_FLAGS_IS_PROXY : 0) |
349     APP_OPTIONS_FLAGS_USE_MQ_FOR_CTRL_MSGS |
350     (vcm->cfg.use_mq_eventfd ? APP_OPTIONS_FLAGS_EVT_MQ_USE_EVENTFD : 0);
351   bmp->options[APP_OPTIONS_PROXY_TRANSPORT] =
352     (u64) ((vcm->cfg.app_proxy_transport_tcp ? 1 << TRANSPORT_PROTO_TCP : 0) |
353            (vcm->cfg.app_proxy_transport_udp ? 1 << TRANSPORT_PROTO_UDP : 0));
354   bmp->options[APP_OPTIONS_SEGMENT_SIZE] = vcm->cfg.segment_size;
355   bmp->options[APP_OPTIONS_ADD_SEGMENT_SIZE] = vcm->cfg.add_segment_size;
356   bmp->options[APP_OPTIONS_RX_FIFO_SIZE] = vcm->cfg.rx_fifo_size;
357   bmp->options[APP_OPTIONS_TX_FIFO_SIZE] = vcm->cfg.tx_fifo_size;
358   bmp->options[APP_OPTIONS_PREALLOC_FIFO_PAIRS] =
359     vcm->cfg.preallocated_fifo_pairs;
360   bmp->options[APP_OPTIONS_EVT_QUEUE_SIZE] = vcm->cfg.event_queue_size;
361   if (nsid_len)
362     {
363       bmp->namespace_id_len = nsid_len;
364       clib_memcpy (bmp->namespace_id, vcm->cfg.namespace_id, nsid_len);
365       bmp->options[APP_OPTIONS_NAMESPACE_SECRET] = vcm->cfg.namespace_secret;
366     }
367   vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & bmp);
368 }
369
370 void
371 vppcom_app_send_detach (void)
372 {
373   vl_api_application_detach_t *bmp;
374   bmp = vl_msg_api_alloc (sizeof (*bmp));
375   memset (bmp, 0, sizeof (*bmp));
376
377   bmp->_vl_msg_id = ntohs (VL_API_APPLICATION_DETACH);
378   bmp->client_index = vcm->my_client_index;
379   bmp->context = htonl (0xfeedface);
380   vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & bmp);
381 }
382
383 void
384 vppcom_send_connect_sock (vcl_session_t * session, u32 session_index)
385 {
386   vl_api_connect_sock_t *cmp;
387
388   /* Assumes caller as acquired the spinlock: vcm->sessions_lockp */
389   cmp = vl_msg_api_alloc (sizeof (*cmp));
390   memset (cmp, 0, sizeof (*cmp));
391   cmp->_vl_msg_id = ntohs (VL_API_CONNECT_SOCK);
392   cmp->client_index = vcm->my_client_index;
393   cmp->context = session_index;
394
395   cmp->is_ip4 = session->transport.is_ip4;
396   clib_memcpy (cmp->ip, &session->transport.rmt_ip, sizeof (cmp->ip));
397   cmp->port = session->transport.rmt_port;
398   cmp->proto = session->session_type;
399   clib_memcpy (cmp->options, session->options, sizeof (cmp->options));
400   vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & cmp);
401 }
402
403 void
404 vppcom_send_disconnect_session (u64 vpp_handle, u32 session_index)
405 {
406   vl_api_disconnect_session_t *dmp;
407
408   VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: sending disconnect msg",
409         getpid (), vpp_handle, session_index);
410
411   dmp = vl_msg_api_alloc (sizeof (*dmp));
412   memset (dmp, 0, sizeof (*dmp));
413   dmp->_vl_msg_id = ntohs (VL_API_DISCONNECT_SESSION);
414   dmp->client_index = vcm->my_client_index;
415   dmp->handle = vpp_handle;
416   vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & dmp);
417 }
418
419 /* VPP combines bind and listen as one operation. VCL manages the separation
420  * of bind and listen locally via vppcom_session_bind() and
421  * vppcom_session_listen() */
422 void
423 vppcom_send_bind_sock (vcl_session_t * session, u32 session_index)
424 {
425   vl_api_bind_sock_t *bmp;
426
427   /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
428   bmp = vl_msg_api_alloc (sizeof (*bmp));
429   memset (bmp, 0, sizeof (*bmp));
430
431   bmp->_vl_msg_id = ntohs (VL_API_BIND_SOCK);
432   bmp->client_index = vcm->my_client_index;
433   bmp->context = session_index;
434   bmp->is_ip4 = session->transport.is_ip4;
435   clib_memcpy (bmp->ip, &session->transport.lcl_ip, sizeof (bmp->ip));
436   bmp->port = session->transport.lcl_port;
437   bmp->proto = session->session_type;
438   clib_memcpy (bmp->options, session->options, sizeof (bmp->options));
439   vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & bmp);
440 }
441
442 void
443 vppcom_send_unbind_sock (u64 vpp_handle)
444 {
445   vl_api_unbind_sock_t *ump;
446
447   ump = vl_msg_api_alloc (sizeof (*ump));
448   memset (ump, 0, sizeof (*ump));
449
450   ump->_vl_msg_id = ntohs (VL_API_UNBIND_SOCK);
451   ump->client_index = vcm->my_client_index;
452   ump->handle = vpp_handle;
453   vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & ump);
454 }
455
456 void
457 vppcom_send_accept_session_reply (u64 handle, u32 context, int retval)
458 {
459   vl_api_accept_session_reply_t *rmp;
460
461   rmp = vl_msg_api_alloc (sizeof (*rmp));
462   memset (rmp, 0, sizeof (*rmp));
463   rmp->_vl_msg_id = ntohs (VL_API_ACCEPT_SESSION_REPLY);
464   rmp->retval = htonl (retval);
465   rmp->context = context;
466   rmp->handle = handle;
467   vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & rmp);
468 }
469
470 u32
471 vcl_max_nsid_len (void)
472 {
473   vl_api_application_attach_t *mp;
474   return (sizeof (mp->namespace_id) - 1);
475 }
476
477 void
478 vppcom_init_error_string_table (void)
479 {
480   vcm->error_string_by_error_number = hash_create (0, sizeof (uword));
481
482 #define _(n, v, s) hash_set (vcm->error_string_by_error_number, -v, s);
483   foreach_vnet_api_error;
484 #undef _
485
486   hash_set (vcm->error_string_by_error_number, 99, "Misc");
487 }
488
489 int
490 vppcom_connect_to_vpp (char *app_name)
491 {
492   api_main_t *am = &api_main;
493   vppcom_cfg_t *vcl_cfg = &vcm->cfg;
494
495   if (vcl_cfg->vpp_api_socket_name)
496     {
497       if (vl_socket_client_connect ((char *) vcl_cfg->vpp_api_socket_name,
498                                     app_name, 0 /* default rx/tx buffer */ ))
499         {
500           clib_warning ("VCL<%d>: app (%s) socket connect failed!",
501                         getpid (), app_name);
502           return VPPCOM_ECONNREFUSED;
503         }
504
505       if (vl_socket_client_init_shm (0))
506         {
507           clib_warning ("VCL<%d>: app (%s) init shm failed!",
508                         getpid (), app_name);
509           return VPPCOM_ECONNREFUSED;
510         }
511     }
512   else
513     {
514       if (!vcl_cfg->vpp_api_filename)
515         vcl_cfg->vpp_api_filename = format (0, "/vpe-api%c", 0);
516
517       VDBG (0, "VCL<%d>: app (%s) connecting to VPP api (%s)...", getpid (),
518             app_name, vcl_cfg->vpp_api_filename);
519
520       if (vl_client_connect_to_vlib ((char *) vcl_cfg->vpp_api_filename,
521                                      app_name, vcm->cfg.vpp_api_q_length) < 0)
522         {
523           clib_warning ("VCL<%d>: app (%s) connect failed!", getpid (),
524                         app_name);
525           return VPPCOM_ECONNREFUSED;
526         }
527
528     }
529
530   vcm->vl_input_queue = am->shmem_hdr->vl_input_queue;
531   vcm->my_client_index = (u32) am->my_client_index;
532   vcm->app_state = STATE_APP_CONN_VPP;
533
534   VDBG (0, "VCL<%d>: app (%s) is connected to VPP!", getpid (), app_name);
535
536   vcl_evt (VCL_EVT_INIT, vcm);
537   return VPPCOM_OK;
538 }
539
540 /*
541  * fd.io coding-style-patch-verification: ON
542  *
543  * Local Variables:
544  * eval: (c-set-style "gnu")
545  * End:
546  */