blob: d9e8e7e046cb044abc2199bb4cca038de79f4711 [file] [log] [blame]
Florin Coras697faea2018-06-27 17:10:49 -07001/*
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
Florin Coras54693d22018-07-17 10:46:29 -070036u8 *
Florin Coras697faea2018-06-27 17:10:49 -070037format_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
51static 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
Florin Coras99368312018-08-02 10:45:44 -070064static int
65ssvm_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
Florin Coras697faea2018-06-27 17:10:49 -070086static void
87vl_api_application_attach_reply_t_handler (vl_api_application_attach_reply_t *
88 mp)
89{
Florin Coras99368312018-08-02 10:45:44 -070090 u32 n_fds = 0;
91 int *fds = 0;
Florin Coras697faea2018-06-27 17:10:49 -070092
Florin Coras697faea2018-06-27 17:10:49 -070093 if (mp->retval)
94 {
95 clib_warning ("VCL<%d>: attach failed: %U", getpid (),
96 format_api_error, ntohl (mp->retval));
97 return;
98 }
99
Florin Coras99368312018-08-02 10:45:44 -0700100 vcm->app_event_queue = uword_to_pointer (mp->app_event_queue_address,
101 svm_msg_q_t *);
102 if (mp->n_fds)
Florin Coras697faea2018-06-27 17:10:49 -0700103 {
Florin Coras99368312018-08-02 10:45:44 -0700104 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);
Florin Coras697faea2018-06-27 17:10:49 -0700127 }
Florin Coras99368312018-08-02 10:45:44 -0700128 else
Florin Coras697faea2018-06-27 17:10:49 -0700129 {
Florin Coras99368312018-08-02 10:45:44 -0700130 if (ssvm_segment_attach ((char *) mp->segment_name, SSVM_SEGMENT_SHM,
131 -1))
132 return;
Florin Coras697faea2018-06-27 17:10:49 -0700133 }
134
Florin Coras697faea2018-06-27 17:10:49 -0700135 vcm->app_state = STATE_APP_ATTACHED;
136}
137
138static void
139vl_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
149static void
Florin Coras697faea2018-06-27 17:10:49 -0700150vl_api_map_another_segment_t_handler (vl_api_map_another_segment_t * mp)
151{
Florin Coras99368312018-08-02 10:45:44 -0700152 ssvm_segment_type_t seg_type = SSVM_SEGMENT_SHM;
153 int fd = -1;
Florin Coras697faea2018-06-27 17:10:49 -0700154
Florin Coras460dce62018-07-27 05:45:06 -0700155 vcm->mounting_segment = 1;
Florin Coras99368312018-08-02 10:45:44 -0700156
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)))
Florin Coras697faea2018-06-27 17:10:49 -0700165 {
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);
Florin Coras460dce62018-07-27 05:45:06 -0700173 vcm->mounting_segment = 0;
Florin Coras697faea2018-06-27 17:10:49 -0700174}
175
176static void
177vl_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
188static void
Florin Coras99368312018-08-02 10:45:44 -0700189 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
Florin Coras15531972018-08-12 23:50:53 -0700208 if (mp->n_fds && (mp->fd_flags & SESSION_FD_F_MQ_EVENTFD))
Florin Coras99368312018-08-02 10:45:44 -0700209 {
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
221static void
Florin Coras697faea2018-06-27 17:10:49 -0700222vl_api_bind_sock_reply_t_handler (vl_api_bind_sock_reply_t * mp)
223{
Florin Coras60116992018-08-27 09:52:18 -0700224 /* 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);
Florin Coras697faea2018-06-27 17:10:49 -0700227}
228
229static void
230vl_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
Florin Coras99368312018-08-02 10:45:44 -0700241#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) \
Florin Coras99368312018-08-02 10:45:44 -0700245_(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) \
Florin Coras697faea2018-06-27 17:10:49 -0700250
251void
252vppcom_api_hookup (void)
253{
Florin Coras60116992018-08-27 09:52:18 -0700254#define _(N, n) \
255 vl_msg_api_set_handlers(VL_API_##N, #n, \
Florin Coras697faea2018-06-27 17:10:49 -0700256 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 */
268void
269vppcom_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
282void
283vppcom_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) |
Florin Coras54693d22018-07-17 10:46:29 -0700300 (app_is_proxy ? APP_OPTIONS_FLAGS_IS_PROXY : 0) |
Florin Coras99368312018-08-02 10:45:44 -0700301 APP_OPTIONS_FLAGS_USE_MQ_FOR_CTRL_MSGS |
302 (vcm->cfg.use_mq_eventfd ? APP_OPTIONS_FLAGS_EVT_MQ_USE_EVENTFD : 0);
Florin Coras697faea2018-06-27 17:10:49 -0700303 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
322void
323vppcom_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
335void
336vppcom_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
355void
Florin Coras697faea2018-06-27 17:10:49 -0700356vppcom_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() */
374void
375vppcom_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
394void
395vppcom_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
408void
409vppcom_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
422u32
423vcl_max_nsid_len (void)
424{
425 vl_api_application_attach_t *mp;
426 return (sizeof (mp->namespace_id) - 1);
427}
428
429void
430vppcom_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
441int
442vppcom_connect_to_vpp (char *app_name)
443{
444 api_main_t *am = &api_main;
445 vppcom_cfg_t *vcl_cfg = &vcm->cfg;
Florin Coras697faea2018-06-27 17:10:49 -0700446
Florin Coras99368312018-08-02 10:45:44 -0700447 if (vcl_cfg->vpp_api_socket_name)
Florin Coras697faea2018-06-27 17:10:49 -0700448 {
Florin Coras99368312018-08-02 10:45:44 -0700449 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 }
Florin Coras697faea2018-06-27 17:10:49 -0700463 }
464 else
465 {
Florin Coras99368312018-08-02 10:45:44 -0700466 if (!vcl_cfg->vpp_api_filename)
467 vcl_cfg->vpp_api_filename = format (0, "/vpe-api%c", 0);
Florin Coras697faea2018-06-27 17:10:49 -0700468
Florin Coras99368312018-08-02 10:45:44 -0700469 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
Florin Coras697faea2018-06-27 17:10:49 -0700480 }
481
Florin Coras99368312018-08-02 10:45:44 -0700482 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
Florin Coras697faea2018-06-27 17:10:49 -0700488 vcl_evt (VCL_EVT_INIT, vcm);
Florin Coras99368312018-08-02 10:45:44 -0700489 return VPPCOM_OK;
Florin Coras697faea2018-06-27 17:10:49 -0700490}
491
492/*
493 * fd.io coding-style-patch-verification: ON
494 *
495 * Local Variables:
496 * eval: (c-set-style "gnu")
497 * End:
498 */