vcl: support for eventfd mq signaling
[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_disconnect_session_reply_t_handler (vl_api_disconnect_session_reply_t *
151                                            mp)
152 {
153   if (mp->retval)
154     clib_warning ("VCL<%d>: vpp handle 0x%llx: disconnect session failed: %U",
155                   getpid (), mp->handle, format_api_error,
156                   ntohl (mp->retval));
157 }
158
159 static void
160 vl_api_map_another_segment_t_handler (vl_api_map_another_segment_t * mp)
161 {
162   ssvm_segment_type_t seg_type = SSVM_SEGMENT_SHM;
163   int fd = -1;
164
165   vcm->mounting_segment = 1;
166
167   if (mp->fd_flags)
168     {
169       vl_socket_client_recv_fd_msg (&fd, 1, 5);
170       seg_type = SSVM_SEGMENT_MEMFD;
171     }
172
173   if (PREDICT_FALSE (ssvm_segment_attach ((char *) mp->segment_name,
174                                           seg_type, fd)))
175     {
176       clib_warning ("VCL<%d>: svm_fifo_segment_attach ('%s') failed",
177                     getpid (), mp->segment_name);
178       return;
179     }
180
181   VDBG (1, "VCL<%d>: mapped new segment '%s' size %d", getpid (),
182         mp->segment_name, mp->segment_size);
183   vcm->mounting_segment = 0;
184 }
185
186 static void
187 vl_api_unmap_segment_t_handler (vl_api_unmap_segment_t * mp)
188 {
189
190 /*
191  * XXX Need segment_name to session_id hash,
192  * XXX - have sessionID by handle hash currently
193  */
194
195   VDBG (1, "Unmapped segment '%s'", mp->segment_name);
196 }
197
198 static void
199   vl_api_app_cut_through_registration_add_t_handler
200   (vl_api_app_cut_through_registration_add_t * mp)
201 {
202   vcl_cut_through_registration_t *ctr;
203   u32 mqc_index = ~0;
204   int *fds = 0;
205
206   if (mp->n_fds)
207     {
208       ASSERT (mp->n_fds == 2);
209       vec_validate (fds, mp->n_fds);
210       vl_socket_client_recv_fd_msg (fds, mp->n_fds, 5);
211     }
212
213   ctr = vcl_ct_registration_lock_and_alloc ();
214   ctr->mq = uword_to_pointer (mp->evt_q_address, svm_msg_q_t *);
215   ctr->peer_mq = uword_to_pointer (mp->peer_evt_q_address, svm_msg_q_t *);
216   VDBG (0, "Adding ct registration %u", vcl_ct_registration_index (ctr));
217
218   if (mp->fd_flags & SESSION_FD_F_MQ_EVENTFD)
219     {
220       svm_msg_q_set_consumer_eventfd (ctr->mq, fds[0]);
221       svm_msg_q_set_producer_eventfd (ctr->peer_mq, fds[1]);
222       mqc_index = vcl_mq_epoll_add_evfd (ctr->mq);
223       ctr->epoll_evt_conn_index = mqc_index;
224       vec_free (fds);
225     }
226   vcl_ct_registration_lookup_add (mp->evt_q_address,
227                                   vcl_ct_registration_index (ctr));
228   vcl_ct_registration_unlock ();
229 }
230
231 static void
232 vl_api_disconnect_session_t_handler (vl_api_disconnect_session_t * mp)
233 {
234   uword *p;
235
236   p = hash_get (vcm->session_index_by_vpp_handles, mp->handle);
237   if (p)
238     {
239       int rv;
240       vcl_session_t *session = 0;
241       u32 session_index = p[0];
242
243       VCL_SESSION_LOCK_AND_GET (session_index, &session);
244       session->session_state = STATE_CLOSE_ON_EMPTY;
245
246       VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: setting state to 0x%x "
247             "(%s)", getpid (), mp->handle, session_index,
248             session->session_state,
249             vppcom_session_state_str (session->session_state));
250       VCL_SESSION_UNLOCK ();
251       return;
252
253     done:
254       VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: session lookup failed!",
255             getpid (), mp->handle, session_index);
256     }
257   else
258     clib_warning ("VCL<%d>: vpp handle 0x%llx: session lookup by "
259                   "handle failed!", getpid (), mp->handle);
260 }
261
262 static void
263 vl_api_reset_session_t_handler (vl_api_reset_session_t * mp)
264 {
265   vcl_session_t *session = 0;
266   vl_api_reset_session_reply_t *rmp;
267   uword *p;
268   int rv = 0;
269
270   p = hash_get (vcm->session_index_by_vpp_handles, mp->handle);
271   if (p)
272     {
273       int rval;
274       VCL_SESSION_LOCK ();
275       rval = vppcom_session_at_index (p[0], &session);
276       if (PREDICT_FALSE (rval))
277         {
278           rv = VNET_API_ERROR_INVALID_VALUE_2;
279           clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
280                         "session lookup failed! returning %d %U",
281                         getpid (), mp->handle, p[0],
282                         rv, format_api_error, rv);
283         }
284       else
285         {
286           /* TBD: should this disconnect immediately and
287            * flush the fifos?
288            */
289           session->session_state = STATE_CLOSE_ON_EMPTY;
290
291           VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: state set to %d "
292                 "(%s)!", getpid (), mp->handle, p[0], session->session_state,
293                 vppcom_session_state_str (session->session_state));
294         }
295       VCL_SESSION_UNLOCK ();
296     }
297   else
298     {
299       rv = VNET_API_ERROR_INVALID_VALUE;
300       clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx: session lookup "
301                     "failed! returning %d %U",
302                     getpid (), mp->handle, rv, format_api_error, rv);
303     }
304
305   rmp = vl_msg_api_alloc (sizeof (*rmp));
306   memset (rmp, 0, sizeof (*rmp));
307   rmp->_vl_msg_id = ntohs (VL_API_RESET_SESSION_REPLY);
308   rmp->retval = htonl (rv);
309   rmp->handle = mp->handle;
310   vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & rmp);
311 }
312
313 static void
314 vl_api_connect_session_reply_t_handler (vl_api_connect_session_reply_t * mp)
315 {
316   vcl_session_t *session = 0;
317   u32 session_index;
318   svm_fifo_t *rx_fifo, *tx_fifo;
319   int rv = VPPCOM_OK;
320
321   session_index = mp->context;
322   VCL_SESSION_LOCK_AND_GET (session_index, &session);
323 done:
324   if (mp->retval)
325     {
326       clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
327                     "connect failed! %U",
328                     getpid (), mp->handle, session_index,
329                     format_api_error, ntohl (mp->retval));
330       if (session)
331         {
332           session->session_state = STATE_FAILED;
333           session->vpp_handle = mp->handle;
334         }
335       else
336         {
337           clib_warning ("[%s] ERROR: vpp handle 0x%llx, sid %u: "
338                         "Invalid session index (%u)!",
339                         getpid (), mp->handle, session_index);
340         }
341       goto done_unlock;
342     }
343
344   if (rv)
345     goto done_unlock;
346
347   /*
348    * Setup session
349    */
350   if (vcm->session_io_thread.io_sessions_lockp)
351     {
352       // Add this connection to the active io sessions list
353       VCL_IO_SESSIONS_LOCK ();
354       u32 *active_session_index;
355       pool_get (vcm->session_io_thread.active_session_indexes,
356                 active_session_index);
357       *active_session_index = session_index;
358       VCL_IO_SESSIONS_UNLOCK ();
359     }
360   session->vpp_evt_q = uword_to_pointer (mp->vpp_event_queue_address,
361                                          svm_msg_q_t *);
362
363   rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *);
364   rx_fifo->client_session_index = session_index;
365   tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *);
366   tx_fifo->client_session_index = session_index;
367
368   session->rx_fifo = rx_fifo;
369   session->tx_fifo = tx_fifo;
370   session->vpp_handle = mp->handle;
371   session->transport.is_ip4 = mp->is_ip4;
372   clib_memcpy (&session->transport.lcl_ip, mp->lcl_ip,
373                sizeof (session->transport.rmt_ip));
374   session->transport.lcl_port = mp->lcl_port;
375   session->session_state = STATE_CONNECT;
376
377   /* Add it to lookup table */
378   hash_set (vcm->session_index_by_vpp_handles, mp->handle, session_index);
379
380   VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: connect succeeded! "
381         "session_rx_fifo %p, refcnt %d, session_tx_fifo %p, refcnt %d",
382         getpid (), mp->handle, session_index, session->rx_fifo,
383         session->rx_fifo->refcnt, session->tx_fifo, session->tx_fifo->refcnt);
384 done_unlock:
385   VCL_SESSION_UNLOCK ();
386 }
387
388 static void
389 vl_api_bind_sock_reply_t_handler (vl_api_bind_sock_reply_t * mp)
390 {
391   vcl_session_t *session = 0;
392   u32 session_index = mp->context;
393   int rv;
394
395   VCL_SESSION_LOCK_AND_GET (session_index, &session);
396 done:
397   if (mp->retval)
398     {
399       clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, "
400                     "sid %u: bind failed: %U",
401                     getpid (), mp->handle, session_index,
402                     format_api_error, ntohl (mp->retval));
403       rv = vppcom_session_at_index (session_index, &session);
404       if (rv == VPPCOM_OK)
405         {
406           session->session_state = STATE_FAILED;
407           session->vpp_handle = mp->handle;
408         }
409       else
410         {
411           clib_warning ("[%s] ERROR: vpp handle 0x%llx, sid %u: "
412                         "Invalid session index (%u)!",
413                         getpid (), mp->handle, session_index);
414         }
415       goto done_unlock;
416     }
417
418   session->vpp_handle = mp->handle;
419   session->transport.is_ip4 = mp->lcl_is_ip4;
420   clib_memcpy (&session->transport.lcl_ip, mp->lcl_ip,
421                sizeof (ip46_address_t));
422   session->transport.lcl_port = mp->lcl_port;
423   vppcom_session_table_add_listener (mp->handle, session_index);
424   session->session_state = STATE_LISTEN;
425
426   if (session->is_dgram)
427     {
428       svm_fifo_t *rx_fifo, *tx_fifo;
429       session->vpp_evt_q = uword_to_pointer (mp->vpp_evt_q, svm_msg_q_t *);
430       rx_fifo = uword_to_pointer (mp->rx_fifo, svm_fifo_t *);
431       rx_fifo->client_session_index = session_index;
432       tx_fifo = uword_to_pointer (mp->tx_fifo, svm_fifo_t *);
433       tx_fifo->client_session_index = session_index;
434       session->rx_fifo = rx_fifo;
435       session->tx_fifo = tx_fifo;
436     }
437
438   VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: bind succeeded!",
439         getpid (), mp->handle, mp->context);
440 done_unlock:
441   VCL_SESSION_UNLOCK ();
442 }
443
444 static void
445 vl_api_unbind_sock_reply_t_handler (vl_api_unbind_sock_reply_t * mp)
446 {
447   if (mp->retval)
448     clib_warning ("VCL<%d>: ERROR: sid %u: unbind failed: %U",
449                   getpid (), mp->context, format_api_error,
450                   ntohl (mp->retval));
451
452   else
453     VDBG (1, "VCL<%d>: sid %u: unbind succeeded!", getpid (), mp->context);
454 }
455
456 static void
457 vl_api_accept_session_t_handler (vl_api_accept_session_t * mp)
458 {
459   svm_fifo_t *rx_fifo, *tx_fifo;
460   vcl_session_t *session, *listen_session;
461   u32 session_index;
462   vce_event_connect_request_t *ecr;
463   vce_event_t *ev;
464   int rv;
465   u32 ev_idx;
466   uword elts = 0;
467
468   VCL_SESSION_LOCK ();
469
470   VCL_ACCEPT_FIFO_LOCK ();
471   elts = clib_fifo_free_elts (vcm->client_session_index_fifo);
472   VCL_ACCEPT_FIFO_UNLOCK ();
473
474   if (!elts)
475     {
476       clib_warning ("VCL<%d>: client session queue is full!", getpid ());
477       vppcom_send_accept_session_reply (mp->handle, mp->context,
478                                         VNET_API_ERROR_QUEUE_FULL);
479       VCL_SESSION_UNLOCK ();
480       return;
481     }
482
483   listen_session = vppcom_session_table_lookup_listener (mp->listener_handle);
484   if (!listen_session)
485     {
486       clib_warning ("VCL<%d>: ERROR: couldn't find listen session: "
487                     "unknown vpp listener handle %llx",
488                     getpid (), mp->listener_handle);
489       vppcom_send_accept_session_reply (mp->handle, mp->context,
490                                         VNET_API_ERROR_INVALID_ARGUMENT);
491       VCL_SESSION_UNLOCK ();
492       return;
493     }
494
495   /* TODO check listener depth and update */
496   /* TODO on "child" fd close, update listener depth */
497
498   /* Allocate local session and set it up */
499   pool_get (vcm->sessions, session);
500   memset (session, 0, sizeof (*session));
501   session_index = (u32) (session - vcm->sessions);
502
503   rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *);
504   rx_fifo->client_session_index = session_index;
505   tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *);
506   tx_fifo->client_session_index = session_index;
507
508   session->vpp_handle = mp->handle;
509   session->client_context = mp->context;
510   session->rx_fifo = rx_fifo;
511   session->tx_fifo = tx_fifo;
512   session->vpp_evt_q = uword_to_pointer (mp->vpp_event_queue_address,
513                                          svm_msg_q_t *);
514   session->session_state = STATE_ACCEPT;
515   session->transport.rmt_port = mp->port;
516   session->transport.is_ip4 = mp->is_ip4;
517   clib_memcpy (&session->transport.rmt_ip, mp->ip, sizeof (ip46_address_t));
518
519   /* Add it to lookup table */
520   hash_set (vcm->session_index_by_vpp_handles, mp->handle, session_index);
521   session->transport.lcl_port = listen_session->transport.lcl_port;
522   session->transport.lcl_ip = listen_session->transport.lcl_ip;
523
524   /* Create an event for handlers */
525
526   VCL_EVENTS_LOCK ();
527
528   pool_get (vcm->event_thread.vce_events, ev);
529   ev_idx = (u32) (ev - vcm->event_thread.vce_events);
530   ecr = vce_get_event_data (ev, sizeof (*ecr));
531   ev->evk.eid = VCL_EVENT_CONNECT_REQ_ACCEPTED;
532   listen_session = vppcom_session_table_lookup_listener (mp->listener_handle);
533   ev->evk.session_index = (u32) (listen_session - vcm->sessions);
534   ecr->accepted_session_index = session_index;
535
536   VCL_EVENTS_UNLOCK ();
537
538   rv = vce_generate_event (&vcm->event_thread, ev_idx);
539   ASSERT (rv == 0);
540
541   VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: client accept request from %s"
542         " address %U port %d queue %p!", getpid (), mp->handle, session_index,
543         mp->is_ip4 ? "IPv4" : "IPv6", format_ip46_address, &mp->ip,
544         mp->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
545         clib_net_to_host_u16 (mp->port), session->vpp_evt_q);
546
547   vcl_evt (VCL_EVT_ACCEPT, session, listen_session, session_index);
548   VCL_SESSION_UNLOCK ();
549 }
550
551 #define foreach_sock_msg                                                \
552 _(SESSION_ENABLE_DISABLE_REPLY, session_enable_disable_reply)           \
553 _(BIND_SOCK_REPLY, bind_sock_reply)                                     \
554 _(UNBIND_SOCK_REPLY, unbind_sock_reply)                                 \
555 _(ACCEPT_SESSION, accept_session)                                       \
556 _(CONNECT_SESSION_REPLY, connect_session_reply)                         \
557 _(DISCONNECT_SESSION, disconnect_session)                               \
558 _(DISCONNECT_SESSION_REPLY, disconnect_session_reply)                   \
559 _(RESET_SESSION, reset_session)                                         \
560 _(APPLICATION_ATTACH_REPLY, application_attach_reply)                   \
561 _(APPLICATION_DETACH_REPLY, application_detach_reply)                   \
562 _(MAP_ANOTHER_SEGMENT, map_another_segment)                             \
563 _(UNMAP_SEGMENT, unmap_segment)                                         \
564 _(APP_CUT_THROUGH_REGISTRATION_ADD, app_cut_through_registration_add)   \
565
566 void
567 vppcom_api_hookup (void)
568 {
569 #define _(N, n)                                                  \
570     vl_msg_api_set_handlers(VL_API_##N, #n,                     \
571                            vl_api_##n##_t_handler,              \
572                            vl_noop_handler,                     \
573                            vl_api_##n##_t_endian,               \
574                            vl_api_##n##_t_print,                \
575                            sizeof(vl_api_##n##_t), 1);
576   foreach_sock_msg;
577 #undef _
578 }
579
580 /*
581  * VPP-API message functions
582  */
583 void
584 vppcom_send_session_enable_disable (u8 is_enable)
585 {
586   vl_api_session_enable_disable_t *bmp;
587   bmp = vl_msg_api_alloc (sizeof (*bmp));
588   memset (bmp, 0, sizeof (*bmp));
589
590   bmp->_vl_msg_id = ntohs (VL_API_SESSION_ENABLE_DISABLE);
591   bmp->client_index = vcm->my_client_index;
592   bmp->context = htonl (0xfeedface);
593   bmp->is_enable = is_enable;
594   vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & bmp);
595 }
596
597 void
598 vppcom_app_send_attach (void)
599 {
600   vl_api_application_attach_t *bmp;
601   u8 nsid_len = vec_len (vcm->cfg.namespace_id);
602   u8 app_is_proxy = (vcm->cfg.app_proxy_transport_tcp ||
603                      vcm->cfg.app_proxy_transport_udp);
604
605   bmp = vl_msg_api_alloc (sizeof (*bmp));
606   memset (bmp, 0, sizeof (*bmp));
607
608   bmp->_vl_msg_id = ntohs (VL_API_APPLICATION_ATTACH);
609   bmp->client_index = vcm->my_client_index;
610   bmp->context = htonl (0xfeedface);
611   bmp->options[APP_OPTIONS_FLAGS] =
612     APP_OPTIONS_FLAGS_ACCEPT_REDIRECT | APP_OPTIONS_FLAGS_ADD_SEGMENT |
613     (vcm->cfg.app_scope_local ? APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE : 0) |
614     (vcm->cfg.app_scope_global ? APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE : 0) |
615     (app_is_proxy ? APP_OPTIONS_FLAGS_IS_PROXY : 0) |
616     APP_OPTIONS_FLAGS_USE_MQ_FOR_CTRL_MSGS |
617     (vcm->cfg.use_mq_eventfd ? APP_OPTIONS_FLAGS_EVT_MQ_USE_EVENTFD : 0);
618   bmp->options[APP_OPTIONS_PROXY_TRANSPORT] =
619     (u64) ((vcm->cfg.app_proxy_transport_tcp ? 1 << TRANSPORT_PROTO_TCP : 0) |
620            (vcm->cfg.app_proxy_transport_udp ? 1 << TRANSPORT_PROTO_UDP : 0));
621   bmp->options[APP_OPTIONS_SEGMENT_SIZE] = vcm->cfg.segment_size;
622   bmp->options[APP_OPTIONS_ADD_SEGMENT_SIZE] = vcm->cfg.add_segment_size;
623   bmp->options[APP_OPTIONS_RX_FIFO_SIZE] = vcm->cfg.rx_fifo_size;
624   bmp->options[APP_OPTIONS_TX_FIFO_SIZE] = vcm->cfg.tx_fifo_size;
625   bmp->options[APP_OPTIONS_PREALLOC_FIFO_PAIRS] =
626     vcm->cfg.preallocated_fifo_pairs;
627   bmp->options[APP_OPTIONS_EVT_QUEUE_SIZE] = vcm->cfg.event_queue_size;
628   if (nsid_len)
629     {
630       bmp->namespace_id_len = nsid_len;
631       clib_memcpy (bmp->namespace_id, vcm->cfg.namespace_id, nsid_len);
632       bmp->options[APP_OPTIONS_NAMESPACE_SECRET] = vcm->cfg.namespace_secret;
633     }
634   vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & bmp);
635 }
636
637 void
638 vppcom_app_send_detach (void)
639 {
640   vl_api_application_detach_t *bmp;
641   bmp = vl_msg_api_alloc (sizeof (*bmp));
642   memset (bmp, 0, sizeof (*bmp));
643
644   bmp->_vl_msg_id = ntohs (VL_API_APPLICATION_DETACH);
645   bmp->client_index = vcm->my_client_index;
646   bmp->context = htonl (0xfeedface);
647   vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & bmp);
648 }
649
650 void
651 vppcom_send_connect_sock (vcl_session_t * session, u32 session_index)
652 {
653   vl_api_connect_sock_t *cmp;
654
655   /* Assumes caller as acquired the spinlock: vcm->sessions_lockp */
656   cmp = vl_msg_api_alloc (sizeof (*cmp));
657   memset (cmp, 0, sizeof (*cmp));
658   cmp->_vl_msg_id = ntohs (VL_API_CONNECT_SOCK);
659   cmp->client_index = vcm->my_client_index;
660   cmp->context = session_index;
661
662   cmp->is_ip4 = session->transport.is_ip4;
663   clib_memcpy (cmp->ip, &session->transport.rmt_ip, sizeof (cmp->ip));
664   cmp->port = session->transport.rmt_port;
665   cmp->proto = session->session_type;
666   clib_memcpy (cmp->options, session->options, sizeof (cmp->options));
667   vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & cmp);
668 }
669
670 void
671 vppcom_send_disconnect_session_reply (u64 vpp_handle, u32 session_index,
672                                       int rv)
673 {
674   vl_api_disconnect_session_reply_t *rmp;
675
676   VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: sending disconnect msg",
677         getpid (), vpp_handle, session_index);
678
679   rmp = vl_msg_api_alloc (sizeof (*rmp));
680   memset (rmp, 0, sizeof (*rmp));
681
682   rmp->_vl_msg_id = ntohs (VL_API_DISCONNECT_SESSION_REPLY);
683   rmp->retval = htonl (rv);
684   rmp->handle = vpp_handle;
685   vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & rmp);
686 }
687
688 void
689 vppcom_send_disconnect_session (u64 vpp_handle, u32 session_index)
690 {
691   vl_api_disconnect_session_t *dmp;
692
693   VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: sending disconnect msg",
694         getpid (), vpp_handle, session_index);
695
696   dmp = vl_msg_api_alloc (sizeof (*dmp));
697   memset (dmp, 0, sizeof (*dmp));
698   dmp->_vl_msg_id = ntohs (VL_API_DISCONNECT_SESSION);
699   dmp->client_index = vcm->my_client_index;
700   dmp->handle = vpp_handle;
701   vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & dmp);
702 }
703
704 /* VPP combines bind and listen as one operation. VCL manages the separation
705  * of bind and listen locally via vppcom_session_bind() and
706  * vppcom_session_listen() */
707 void
708 vppcom_send_bind_sock (vcl_session_t * session, u32 session_index)
709 {
710   vl_api_bind_sock_t *bmp;
711
712   /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
713   bmp = vl_msg_api_alloc (sizeof (*bmp));
714   memset (bmp, 0, sizeof (*bmp));
715
716   bmp->_vl_msg_id = ntohs (VL_API_BIND_SOCK);
717   bmp->client_index = vcm->my_client_index;
718   bmp->context = session_index;
719   bmp->is_ip4 = session->transport.is_ip4;
720   clib_memcpy (bmp->ip, &session->transport.lcl_ip, sizeof (bmp->ip));
721   bmp->port = session->transport.lcl_port;
722   bmp->proto = session->session_type;
723   clib_memcpy (bmp->options, session->options, sizeof (bmp->options));
724   vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & bmp);
725 }
726
727 void
728 vppcom_send_unbind_sock (u64 vpp_handle)
729 {
730   vl_api_unbind_sock_t *ump;
731
732   ump = vl_msg_api_alloc (sizeof (*ump));
733   memset (ump, 0, sizeof (*ump));
734
735   ump->_vl_msg_id = ntohs (VL_API_UNBIND_SOCK);
736   ump->client_index = vcm->my_client_index;
737   ump->handle = vpp_handle;
738   vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & ump);
739 }
740
741 void
742 vppcom_send_accept_session_reply (u64 handle, u32 context, int retval)
743 {
744   vl_api_accept_session_reply_t *rmp;
745
746   rmp = vl_msg_api_alloc (sizeof (*rmp));
747   memset (rmp, 0, sizeof (*rmp));
748   rmp->_vl_msg_id = ntohs (VL_API_ACCEPT_SESSION_REPLY);
749   rmp->retval = htonl (retval);
750   rmp->context = context;
751   rmp->handle = handle;
752   vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & rmp);
753 }
754
755 u32
756 vcl_max_nsid_len (void)
757 {
758   vl_api_application_attach_t *mp;
759   return (sizeof (mp->namespace_id) - 1);
760 }
761
762 void
763 vppcom_init_error_string_table (void)
764 {
765   vcm->error_string_by_error_number = hash_create (0, sizeof (uword));
766
767 #define _(n, v, s) hash_set (vcm->error_string_by_error_number, -v, s);
768   foreach_vnet_api_error;
769 #undef _
770
771   hash_set (vcm->error_string_by_error_number, 99, "Misc");
772 }
773
774 int
775 vppcom_connect_to_vpp (char *app_name)
776 {
777   api_main_t *am = &api_main;
778   vppcom_cfg_t *vcl_cfg = &vcm->cfg;
779
780   if (vcl_cfg->vpp_api_socket_name)
781     {
782       if (vl_socket_client_connect ((char *) vcl_cfg->vpp_api_socket_name,
783                                     app_name, 0 /* default rx/tx buffer */ ))
784         {
785           clib_warning ("VCL<%d>: app (%s) socket connect failed!",
786                         getpid (), app_name);
787           return VPPCOM_ECONNREFUSED;
788         }
789
790       if (vl_socket_client_init_shm (0))
791         {
792           clib_warning ("VCL<%d>: app (%s) init shm failed!",
793                         getpid (), app_name);
794           return VPPCOM_ECONNREFUSED;
795         }
796     }
797   else
798     {
799       if (!vcl_cfg->vpp_api_filename)
800         vcl_cfg->vpp_api_filename = format (0, "/vpe-api%c", 0);
801
802       VDBG (0, "VCL<%d>: app (%s) connecting to VPP api (%s)...", getpid (),
803             app_name, vcl_cfg->vpp_api_filename);
804
805       if (vl_client_connect_to_vlib ((char *) vcl_cfg->vpp_api_filename,
806                                      app_name, vcm->cfg.vpp_api_q_length) < 0)
807         {
808           clib_warning ("VCL<%d>: app (%s) connect failed!", getpid (),
809                         app_name);
810           return VPPCOM_ECONNREFUSED;
811         }
812
813     }
814
815   vcm->vl_input_queue = am->shmem_hdr->vl_input_queue;
816   vcm->my_client_index = (u32) am->my_client_index;
817   vcm->app_state = STATE_APP_CONN_VPP;
818
819   VDBG (0, "VCL<%d>: app (%s) is connected to VPP!", getpid (), app_name);
820
821   vcl_evt (VCL_EVT_INIT, vcm);
822   return VPPCOM_OK;
823 }
824
825 /*
826  * fd.io coding-style-patch-verification: ON
827  *
828  * Local Variables:
829  * eval: (c-set-style "gnu")
830  * End:
831  */