vcl/session: use mq for bind replies
[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   /* Expecting a similar message on mq. So ignore this */
225   VDBG (1, "VCL<%d>: bapi msg vpp handle 0x%llx, sid %u: bind retval: %u!",
226         getpid (), mp->handle, mp->context, mp->retval);
227 }
228
229 static void
230 vl_api_unbind_sock_reply_t_handler (vl_api_unbind_sock_reply_t * mp)
231 {
232   if (mp->retval)
233     clib_warning ("VCL<%d>: ERROR: sid %u: unbind failed: %U",
234                   getpid (), mp->context, format_api_error,
235                   ntohl (mp->retval));
236
237   else
238     VDBG (1, "VCL<%d>: sid %u: unbind succeeded!", getpid (), mp->context);
239 }
240
241 #define foreach_sock_msg                                                \
242 _(SESSION_ENABLE_DISABLE_REPLY, session_enable_disable_reply)           \
243 _(BIND_SOCK_REPLY, bind_sock_reply)                                     \
244 _(UNBIND_SOCK_REPLY, unbind_sock_reply)                                 \
245 _(APPLICATION_ATTACH_REPLY, application_attach_reply)                   \
246 _(APPLICATION_DETACH_REPLY, application_detach_reply)                   \
247 _(MAP_ANOTHER_SEGMENT, map_another_segment)                             \
248 _(UNMAP_SEGMENT, unmap_segment)                                         \
249 _(APP_CUT_THROUGH_REGISTRATION_ADD, app_cut_through_registration_add)   \
250
251 void
252 vppcom_api_hookup (void)
253 {
254 #define _(N, n)                                                 \
255     vl_msg_api_set_handlers(VL_API_##N, #n,                     \
256                            vl_api_##n##_t_handler,              \
257                            vl_noop_handler,                     \
258                            vl_api_##n##_t_endian,               \
259                            vl_api_##n##_t_print,                \
260                            sizeof(vl_api_##n##_t), 1);
261   foreach_sock_msg;
262 #undef _
263 }
264
265 /*
266  * VPP-API message functions
267  */
268 void
269 vppcom_send_session_enable_disable (u8 is_enable)
270 {
271   vl_api_session_enable_disable_t *bmp;
272   bmp = vl_msg_api_alloc (sizeof (*bmp));
273   memset (bmp, 0, sizeof (*bmp));
274
275   bmp->_vl_msg_id = ntohs (VL_API_SESSION_ENABLE_DISABLE);
276   bmp->client_index = vcm->my_client_index;
277   bmp->context = htonl (0xfeedface);
278   bmp->is_enable = is_enable;
279   vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & bmp);
280 }
281
282 void
283 vppcom_app_send_attach (void)
284 {
285   vl_api_application_attach_t *bmp;
286   u8 nsid_len = vec_len (vcm->cfg.namespace_id);
287   u8 app_is_proxy = (vcm->cfg.app_proxy_transport_tcp ||
288                      vcm->cfg.app_proxy_transport_udp);
289
290   bmp = vl_msg_api_alloc (sizeof (*bmp));
291   memset (bmp, 0, sizeof (*bmp));
292
293   bmp->_vl_msg_id = ntohs (VL_API_APPLICATION_ATTACH);
294   bmp->client_index = vcm->my_client_index;
295   bmp->context = htonl (0xfeedface);
296   bmp->options[APP_OPTIONS_FLAGS] =
297     APP_OPTIONS_FLAGS_ACCEPT_REDIRECT | APP_OPTIONS_FLAGS_ADD_SEGMENT |
298     (vcm->cfg.app_scope_local ? APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE : 0) |
299     (vcm->cfg.app_scope_global ? APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE : 0) |
300     (app_is_proxy ? APP_OPTIONS_FLAGS_IS_PROXY : 0) |
301     APP_OPTIONS_FLAGS_USE_MQ_FOR_CTRL_MSGS |
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   if (nsid_len)
314     {
315       bmp->namespace_id_len = nsid_len;
316       clib_memcpy (bmp->namespace_id, vcm->cfg.namespace_id, nsid_len);
317       bmp->options[APP_OPTIONS_NAMESPACE_SECRET] = vcm->cfg.namespace_secret;
318     }
319   vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & bmp);
320 }
321
322 void
323 vppcom_app_send_detach (void)
324 {
325   vl_api_application_detach_t *bmp;
326   bmp = vl_msg_api_alloc (sizeof (*bmp));
327   memset (bmp, 0, sizeof (*bmp));
328
329   bmp->_vl_msg_id = ntohs (VL_API_APPLICATION_DETACH);
330   bmp->client_index = vcm->my_client_index;
331   bmp->context = htonl (0xfeedface);
332   vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & bmp);
333 }
334
335 void
336 vppcom_send_connect_sock (vcl_session_t * session, u32 session_index)
337 {
338   vl_api_connect_sock_t *cmp;
339
340   /* Assumes caller as acquired the spinlock: vcm->sessions_lockp */
341   cmp = vl_msg_api_alloc (sizeof (*cmp));
342   memset (cmp, 0, sizeof (*cmp));
343   cmp->_vl_msg_id = ntohs (VL_API_CONNECT_SOCK);
344   cmp->client_index = vcm->my_client_index;
345   cmp->context = session_index;
346
347   cmp->is_ip4 = session->transport.is_ip4;
348   clib_memcpy (cmp->ip, &session->transport.rmt_ip, sizeof (cmp->ip));
349   cmp->port = session->transport.rmt_port;
350   cmp->proto = session->session_type;
351   clib_memcpy (cmp->options, session->options, sizeof (cmp->options));
352   vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & cmp);
353 }
354
355 void
356 vppcom_send_disconnect_session (u64 vpp_handle, u32 session_index)
357 {
358   vl_api_disconnect_session_t *dmp;
359
360   VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: sending disconnect msg",
361         getpid (), vpp_handle, session_index);
362
363   dmp = vl_msg_api_alloc (sizeof (*dmp));
364   memset (dmp, 0, sizeof (*dmp));
365   dmp->_vl_msg_id = ntohs (VL_API_DISCONNECT_SESSION);
366   dmp->client_index = vcm->my_client_index;
367   dmp->handle = vpp_handle;
368   vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & dmp);
369 }
370
371 /* VPP combines bind and listen as one operation. VCL manages the separation
372  * of bind and listen locally via vppcom_session_bind() and
373  * vppcom_session_listen() */
374 void
375 vppcom_send_bind_sock (vcl_session_t * session, u32 session_index)
376 {
377   vl_api_bind_sock_t *bmp;
378
379   /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
380   bmp = vl_msg_api_alloc (sizeof (*bmp));
381   memset (bmp, 0, sizeof (*bmp));
382
383   bmp->_vl_msg_id = ntohs (VL_API_BIND_SOCK);
384   bmp->client_index = vcm->my_client_index;
385   bmp->context = session_index;
386   bmp->is_ip4 = session->transport.is_ip4;
387   clib_memcpy (bmp->ip, &session->transport.lcl_ip, sizeof (bmp->ip));
388   bmp->port = session->transport.lcl_port;
389   bmp->proto = session->session_type;
390   clib_memcpy (bmp->options, session->options, sizeof (bmp->options));
391   vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & bmp);
392 }
393
394 void
395 vppcom_send_unbind_sock (u64 vpp_handle)
396 {
397   vl_api_unbind_sock_t *ump;
398
399   ump = vl_msg_api_alloc (sizeof (*ump));
400   memset (ump, 0, sizeof (*ump));
401
402   ump->_vl_msg_id = ntohs (VL_API_UNBIND_SOCK);
403   ump->client_index = vcm->my_client_index;
404   ump->handle = vpp_handle;
405   vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & ump);
406 }
407
408 void
409 vppcom_send_accept_session_reply (u64 handle, u32 context, int retval)
410 {
411   vl_api_accept_session_reply_t *rmp;
412
413   rmp = vl_msg_api_alloc (sizeof (*rmp));
414   memset (rmp, 0, sizeof (*rmp));
415   rmp->_vl_msg_id = ntohs (VL_API_ACCEPT_SESSION_REPLY);
416   rmp->retval = htonl (retval);
417   rmp->context = context;
418   rmp->handle = handle;
419   vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & rmp);
420 }
421
422 u32
423 vcl_max_nsid_len (void)
424 {
425   vl_api_application_attach_t *mp;
426   return (sizeof (mp->namespace_id) - 1);
427 }
428
429 void
430 vppcom_init_error_string_table (void)
431 {
432   vcm->error_string_by_error_number = hash_create (0, sizeof (uword));
433
434 #define _(n, v, s) hash_set (vcm->error_string_by_error_number, -v, s);
435   foreach_vnet_api_error;
436 #undef _
437
438   hash_set (vcm->error_string_by_error_number, 99, "Misc");
439 }
440
441 int
442 vppcom_connect_to_vpp (char *app_name)
443 {
444   api_main_t *am = &api_main;
445   vppcom_cfg_t *vcl_cfg = &vcm->cfg;
446
447   if (vcl_cfg->vpp_api_socket_name)
448     {
449       if (vl_socket_client_connect ((char *) vcl_cfg->vpp_api_socket_name,
450                                     app_name, 0 /* default rx/tx buffer */ ))
451         {
452           clib_warning ("VCL<%d>: app (%s) socket connect failed!",
453                         getpid (), app_name);
454           return VPPCOM_ECONNREFUSED;
455         }
456
457       if (vl_socket_client_init_shm (0))
458         {
459           clib_warning ("VCL<%d>: app (%s) init shm failed!",
460                         getpid (), app_name);
461           return VPPCOM_ECONNREFUSED;
462         }
463     }
464   else
465     {
466       if (!vcl_cfg->vpp_api_filename)
467         vcl_cfg->vpp_api_filename = format (0, "/vpe-api%c", 0);
468
469       VDBG (0, "VCL<%d>: app (%s) connecting to VPP api (%s)...", getpid (),
470             app_name, vcl_cfg->vpp_api_filename);
471
472       if (vl_client_connect_to_vlib ((char *) vcl_cfg->vpp_api_filename,
473                                      app_name, vcm->cfg.vpp_api_q_length) < 0)
474         {
475           clib_warning ("VCL<%d>: app (%s) connect failed!", getpid (),
476                         app_name);
477           return VPPCOM_ECONNREFUSED;
478         }
479
480     }
481
482   vcm->vl_input_queue = am->shmem_hdr->vl_input_queue;
483   vcm->my_client_index = (u32) am->my_client_index;
484   vcm->app_state = STATE_APP_CONN_VPP;
485
486   VDBG (0, "VCL<%d>: app (%s) is connected to VPP!", getpid (), app_name);
487
488   vcl_evt (VCL_EVT_INIT, vcm);
489   return VPPCOM_OK;
490 }
491
492 /*
493  * fd.io coding-style-patch-verification: ON
494  *
495  * Local Variables:
496  * eval: (c-set-style "gnu")
497  * End:
498  */