blob: f6274e026372a7556a72500476264b9214fd984b [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
Florin Corasd85de682018-11-29 17:02:29 -080065vcl_segment_attach (u64 segment_handle, char *name, ssvm_segment_type_t type,
66 int fd)
Florin Coras99368312018-08-02 10:45:44 -070067{
68 svm_fifo_segment_create_args_t _a, *a = &_a;
69 int rv;
70
71 memset (a, 0, sizeof (*a));
72 a->segment_name = (char *) name;
73 a->segment_type = type;
74
75 if (type == SSVM_SEGMENT_MEMFD)
76 a->memfd_fd = fd;
77
Florin Corasadc74d72018-12-02 13:36:00 -080078 if ((rv = svm_fifo_segment_attach (&vcm->segment_main, a)))
Florin Coras99368312018-08-02 10:45:44 -070079 {
80 clib_warning ("svm_fifo_segment_attach ('%s') failed", name);
81 return rv;
82 }
Florin Corasd85de682018-11-29 17:02:29 -080083 vcl_segment_table_add (segment_handle, a->new_segment_indices[0]);
Florin Coras99368312018-08-02 10:45:44 -070084 vec_reset_length (a->new_segment_indices);
85 return 0;
86}
87
Florin Coras697faea2018-06-27 17:10:49 -070088static void
Florin Corasd85de682018-11-29 17:02:29 -080089vcl_segment_detach (u64 segment_handle)
90{
Florin Corasadc74d72018-12-02 13:36:00 -080091 svm_fifo_segment_main_t *sm = &vcm->segment_main;
Florin Corasd85de682018-11-29 17:02:29 -080092 svm_fifo_segment_private_t *segment;
93 u32 segment_index;
94
95 segment_index = vcl_segment_table_lookup (segment_handle);
96 if (segment_index == (u32) ~ 0)
97 return;
Florin Corasadc74d72018-12-02 13:36:00 -080098 segment = svm_fifo_segment_get_segment (sm, segment_index);
99 svm_fifo_segment_delete (sm, segment);
Florin Corasd85de682018-11-29 17:02:29 -0800100 vcl_segment_table_del (segment_handle);
Florin Coras4850e3e2018-12-12 14:34:38 -0800101 VDBG (0, "detached segment %u handle %u", segment_index, segment_handle);
Florin Corasd85de682018-11-29 17:02:29 -0800102}
103
104static u64
105vcl_vpp_worker_segment_handle (u32 wrk_index)
106{
107 return (VCL_INVALID_SEGMENT_HANDLE - wrk_index - 1);
108}
109
110static void
Florin Coras697faea2018-06-27 17:10:49 -0700111vl_api_application_attach_reply_t_handler (vl_api_application_attach_reply_t *
112 mp)
113{
Florin Coras134a9962018-08-28 11:32:04 -0700114 vcl_worker_t *wrk = vcl_worker_get (0);
Florin Corasd85de682018-11-29 17:02:29 -0800115 u64 segment_handle;
Florin Coras99368312018-08-02 10:45:44 -0700116 u32 n_fds = 0;
117 int *fds = 0;
Florin Coras697faea2018-06-27 17:10:49 -0700118
Florin Coras697faea2018-06-27 17:10:49 -0700119 if (mp->retval)
120 {
121 clib_warning ("VCL<%d>: attach failed: %U", getpid (),
122 format_api_error, ntohl (mp->retval));
123 return;
124 }
125
Florin Coras134a9962018-08-28 11:32:04 -0700126 wrk->app_event_queue = uword_to_pointer (mp->app_event_queue_address,
Florin Coras99368312018-08-02 10:45:44 -0700127 svm_msg_q_t *);
Florin Corasd85de682018-11-29 17:02:29 -0800128 segment_handle = clib_net_to_host_u64 (mp->segment_handle);
129 if (segment_handle == VCL_INVALID_SEGMENT_HANDLE)
130 {
131 clib_warning ("invalid segment handle");
132 return;
133 }
134
Florin Coras99368312018-08-02 10:45:44 -0700135 if (mp->n_fds)
Florin Coras697faea2018-06-27 17:10:49 -0700136 {
Florin Coras99368312018-08-02 10:45:44 -0700137 vec_validate (fds, mp->n_fds);
138 vl_socket_client_recv_fd_msg (fds, mp->n_fds, 5);
139
140 if (mp->fd_flags & SESSION_FD_F_VPP_MQ_SEGMENT)
Florin Corasd85de682018-11-29 17:02:29 -0800141 if (vcl_segment_attach (vcl_vpp_worker_segment_handle (0),
142 "vpp-mq-seg", SSVM_SEGMENT_MEMFD,
143 fds[n_fds++]))
Florin Coras99368312018-08-02 10:45:44 -0700144 return;
145
146 if (mp->fd_flags & SESSION_FD_F_MEMFD_SEGMENT)
Florin Corasd85de682018-11-29 17:02:29 -0800147 if (vcl_segment_attach (segment_handle, (char *) mp->segment_name,
148 SSVM_SEGMENT_MEMFD, fds[n_fds++]))
Florin Coras99368312018-08-02 10:45:44 -0700149 return;
150
151 if (mp->fd_flags & SESSION_FD_F_MQ_EVENTFD)
152 {
Florin Coras134a9962018-08-28 11:32:04 -0700153 svm_msg_q_set_consumer_eventfd (wrk->app_event_queue, fds[n_fds]);
154 vcl_mq_epoll_add_evfd (wrk, wrk->app_event_queue);
Florin Coras99368312018-08-02 10:45:44 -0700155 n_fds++;
156 }
157
158 vec_free (fds);
Florin Coras697faea2018-06-27 17:10:49 -0700159 }
Florin Coras99368312018-08-02 10:45:44 -0700160 else
Florin Coras697faea2018-06-27 17:10:49 -0700161 {
Florin Corasd85de682018-11-29 17:02:29 -0800162 if (vcl_segment_attach (segment_handle, (char *) mp->segment_name,
163 SSVM_SEGMENT_SHM, -1))
Florin Coras99368312018-08-02 10:45:44 -0700164 return;
Florin Coras697faea2018-06-27 17:10:49 -0700165 }
166
Florin Corasc1f5a432018-11-20 11:31:26 -0800167 vcm->app_index = clib_net_to_host_u32 (mp->app_index);
Florin Coras697faea2018-06-27 17:10:49 -0700168 vcm->app_state = STATE_APP_ATTACHED;
169}
170
171static void
Florin Coras134a9962018-08-28 11:32:04 -0700172vl_api_app_worker_add_del_reply_t_handler (vl_api_app_worker_add_del_reply_t *
173 mp)
174{
Florin Corasab2f6db2018-08-31 14:31:41 -0700175 int n_fds = 0, *fds = 0;
Florin Corasd85de682018-11-29 17:02:29 -0800176 u64 segment_handle;
Florin Coras134a9962018-08-28 11:32:04 -0700177 vcl_worker_t *wrk;
Florin Corasab2f6db2018-08-31 14:31:41 -0700178 u32 wrk_index;
Florin Coras134a9962018-08-28 11:32:04 -0700179
180 if (mp->retval)
181 {
182 clib_warning ("VCL<%d>: add/del worker failed: %U", getpid (),
183 format_api_error, ntohl (mp->retval));
184 goto failed;
185 }
Florin Coras053a0e42018-11-13 15:52:38 -0800186
Florin Coras3348a4c2018-09-07 17:09:35 -0700187 if (!mp->is_add)
188 return;
189
Florin Corasdc2e2512018-12-03 17:47:26 -0800190 wrk_index = mp->context;
Florin Coras01f3f892018-12-02 12:45:53 -0800191 wrk = vcl_worker_get_if_valid (wrk_index);
192 if (!wrk)
193 return;
194
Florin Corasdc2e2512018-12-03 17:47:26 -0800195 wrk->vpp_wrk_index = clib_net_to_host_u32 (mp->wrk_index);
Florin Coras134a9962018-08-28 11:32:04 -0700196 wrk->app_event_queue = uword_to_pointer (mp->app_event_queue_address,
197 svm_msg_q_t *);
198
Florin Corasd85de682018-11-29 17:02:29 -0800199 segment_handle = clib_net_to_host_u64 (mp->segment_handle);
200 if (segment_handle == VCL_INVALID_SEGMENT_HANDLE)
201 {
202 clib_warning ("invalid segment handle");
Florin Coras01f3f892018-12-02 12:45:53 -0800203 goto failed;
Florin Corasd85de682018-11-29 17:02:29 -0800204 }
205
Florin Coras134a9962018-08-28 11:32:04 -0700206 if (mp->n_fds)
207 {
208 vec_validate (fds, mp->n_fds);
209 vl_socket_client_recv_fd_msg (fds, mp->n_fds, 5);
210
211 if (mp->fd_flags & SESSION_FD_F_VPP_MQ_SEGMENT)
Florin Coras01f3f892018-12-02 12:45:53 -0800212 if (vcl_segment_attach (vcl_vpp_worker_segment_handle (wrk_index),
213 "vpp-worker-seg", SSVM_SEGMENT_MEMFD,
214 fds[n_fds++]))
Florin Coras134a9962018-08-28 11:32:04 -0700215 goto failed;
216
217 if (mp->fd_flags & SESSION_FD_F_MEMFD_SEGMENT)
Florin Corasd85de682018-11-29 17:02:29 -0800218 if (vcl_segment_attach (segment_handle, (char *) mp->segment_name,
219 SSVM_SEGMENT_MEMFD, fds[n_fds++]))
Florin Coras134a9962018-08-28 11:32:04 -0700220 goto failed;
221
222 if (mp->fd_flags & SESSION_FD_F_MQ_EVENTFD)
223 {
224 svm_msg_q_set_consumer_eventfd (wrk->app_event_queue, fds[n_fds]);
225 vcl_mq_epoll_add_evfd (wrk, wrk->app_event_queue);
226 n_fds++;
227 }
228
229 vec_free (fds);
230 }
231 else
232 {
Florin Corasd85de682018-11-29 17:02:29 -0800233 if (vcl_segment_attach (segment_handle, (char *) mp->segment_name,
234 SSVM_SEGMENT_SHM, -1))
Florin Coras134a9962018-08-28 11:32:04 -0700235 goto failed;
236 }
237 vcm->app_state = STATE_APP_READY;
Florin Coras053a0e42018-11-13 15:52:38 -0800238 VDBG (0, "worker %u vpp-worker %u added", wrk_index, wrk->vpp_wrk_index);
Florin Coras134a9962018-08-28 11:32:04 -0700239 return;
240
241failed:
242 vcm->app_state = STATE_APP_FAILED;
243}
244
245static void
Florin Coras697faea2018-06-27 17:10:49 -0700246vl_api_application_detach_reply_t_handler (vl_api_application_detach_reply_t *
247 mp)
248{
249 if (mp->retval)
250 clib_warning ("VCL<%d>: detach failed: %U", getpid (), format_api_error,
251 ntohl (mp->retval));
252
253 vcm->app_state = STATE_APP_ENABLED;
254}
255
256static void
Florin Coras697faea2018-06-27 17:10:49 -0700257vl_api_map_another_segment_t_handler (vl_api_map_another_segment_t * mp)
258{
Florin Coras99368312018-08-02 10:45:44 -0700259 ssvm_segment_type_t seg_type = SSVM_SEGMENT_SHM;
Florin Corasd85de682018-11-29 17:02:29 -0800260 u64 segment_handle;
Florin Coras99368312018-08-02 10:45:44 -0700261 int fd = -1;
Florin Coras697faea2018-06-27 17:10:49 -0700262
Florin Coras99368312018-08-02 10:45:44 -0700263 if (mp->fd_flags)
264 {
265 vl_socket_client_recv_fd_msg (&fd, 1, 5);
266 seg_type = SSVM_SEGMENT_MEMFD;
267 }
268
Florin Corasd85de682018-11-29 17:02:29 -0800269 segment_handle = clib_net_to_host_u64 (mp->segment_handle);
270 if (segment_handle == VCL_INVALID_SEGMENT_HANDLE)
271 {
272 clib_warning ("invalid segment handle");
273 return;
274 }
275
276 if (vcl_segment_attach (segment_handle, (char *) mp->segment_name,
277 seg_type, fd))
Florin Coras697faea2018-06-27 17:10:49 -0700278 {
279 clib_warning ("VCL<%d>: svm_fifo_segment_attach ('%s') failed",
280 getpid (), mp->segment_name);
281 return;
282 }
283
284 VDBG (1, "VCL<%d>: mapped new segment '%s' size %d", getpid (),
285 mp->segment_name, mp->segment_size);
286}
287
288static void
289vl_api_unmap_segment_t_handler (vl_api_unmap_segment_t * mp)
290{
Florin Corasd85de682018-11-29 17:02:29 -0800291 u64 segment_handle = clib_net_to_host_u64 (mp->segment_handle);
292 vcl_segment_detach (segment_handle);
293 VDBG (1, "Unmapped segment: %d", segment_handle);
Florin Coras697faea2018-06-27 17:10:49 -0700294}
295
296static void
Florin Coras99368312018-08-02 10:45:44 -0700297 vl_api_app_cut_through_registration_add_t_handler
298 (vl_api_app_cut_through_registration_add_t * mp)
299{
300 vcl_cut_through_registration_t *ctr;
301 u32 mqc_index = ~0;
Florin Coras134a9962018-08-28 11:32:04 -0700302 vcl_worker_t *wrk;
Florin Coras99368312018-08-02 10:45:44 -0700303 int *fds = 0;
304
305 if (mp->n_fds)
306 {
307 ASSERT (mp->n_fds == 2);
308 vec_validate (fds, mp->n_fds);
309 vl_socket_client_recv_fd_msg (fds, mp->n_fds, 5);
310 }
311
Florin Coras134a9962018-08-28 11:32:04 -0700312 wrk = vcl_worker_get (mp->wrk_index);
313 ctr = vcl_ct_registration_lock_and_alloc (wrk);
Florin Coras99368312018-08-02 10:45:44 -0700314 ctr->mq = uword_to_pointer (mp->evt_q_address, svm_msg_q_t *);
315 ctr->peer_mq = uword_to_pointer (mp->peer_evt_q_address, svm_msg_q_t *);
Florin Coras134a9962018-08-28 11:32:04 -0700316 VDBG (0, "Adding ct registration %u", vcl_ct_registration_index (wrk, ctr));
Florin Coras99368312018-08-02 10:45:44 -0700317
Florin Coras15531972018-08-12 23:50:53 -0700318 if (mp->n_fds && (mp->fd_flags & SESSION_FD_F_MQ_EVENTFD))
Florin Coras99368312018-08-02 10:45:44 -0700319 {
320 svm_msg_q_set_consumer_eventfd (ctr->mq, fds[0]);
321 svm_msg_q_set_producer_eventfd (ctr->peer_mq, fds[1]);
Florin Coras134a9962018-08-28 11:32:04 -0700322 mqc_index = vcl_mq_epoll_add_evfd (wrk, ctr->mq);
Florin Coras99368312018-08-02 10:45:44 -0700323 ctr->epoll_evt_conn_index = mqc_index;
324 vec_free (fds);
325 }
Florin Coras134a9962018-08-28 11:32:04 -0700326 vcl_ct_registration_lookup_add (wrk, mp->evt_q_address,
327 vcl_ct_registration_index (wrk, ctr));
328 vcl_ct_registration_unlock (wrk);
Florin Coras99368312018-08-02 10:45:44 -0700329}
330
331static void
Florin Coras697faea2018-06-27 17:10:49 -0700332vl_api_bind_sock_reply_t_handler (vl_api_bind_sock_reply_t * mp)
333{
Florin Coras60116992018-08-27 09:52:18 -0700334 /* Expecting a similar message on mq. So ignore this */
Florin Corasdc2e2512018-12-03 17:47:26 -0800335 VDBG (0, "bapi msg vpp handle 0x%llx, sid %u: bind retval: %u!",
Florin Coras60116992018-08-27 09:52:18 -0700336 getpid (), mp->handle, mp->context, mp->retval);
Florin Coras697faea2018-06-27 17:10:49 -0700337}
338
339static void
340vl_api_unbind_sock_reply_t_handler (vl_api_unbind_sock_reply_t * mp)
341{
342 if (mp->retval)
Florin Corasdfae9f92019-02-20 19:48:31 -0800343 VDBG (0, "ERROR: sid %u: unbind failed: %U", mp->context,
344 format_api_error, ntohl (mp->retval));
Florin Coras697faea2018-06-27 17:10:49 -0700345
Florin Corasdfae9f92019-02-20 19:48:31 -0800346 VDBG (1, "sid %u: unbind succeeded!", mp->context);
347
Florin Coras697faea2018-06-27 17:10:49 -0700348}
349
Florin Corasab2f6db2018-08-31 14:31:41 -0700350static void
351vl_api_disconnect_session_reply_t_handler (vl_api_disconnect_session_reply_t *
352 mp)
353{
354 if (mp->retval)
355 clib_warning ("VCL<%d>: ERROR: sid %u: disconnect failed: %U",
356 getpid (), mp->context, format_api_error,
357 ntohl (mp->retval));
358}
359
360static void
Florin Coras6d4bb422018-09-04 22:07:27 -0700361vl_api_connect_session_reply_t_handler (vl_api_connect_sock_reply_t * mp)
Florin Corasab2f6db2018-08-31 14:31:41 -0700362{
363 if (mp->retval)
364 clib_warning ("VCL<%d>: ERROR: sid %u: connect failed: %U",
365 getpid (), mp->context, format_api_error,
366 ntohl (mp->retval));
367}
368
Ping Yu34a3a082018-11-30 19:16:17 -0500369static void
370 vl_api_application_tls_cert_add_reply_t_handler
371 (vl_api_application_tls_cert_add_reply_t * mp)
372{
373 if (mp->retval)
374 {
375 clib_warning ("VCL<%d>: add cert failed: %U", getpid (),
376 format_api_error, ntohl (mp->retval));
377 return;
378 }
379}
380
381static void
382 vl_api_application_tls_key_add_reply_t_handler
383 (vl_api_application_tls_key_add_reply_t * mp)
384{
385 if (mp->retval)
386 {
387 clib_warning ("VCL<%d>: add key failed: %U", getpid (),
388 format_api_error, ntohl (mp->retval));
389 return;
390 }
391
392}
393
Florin Coras99368312018-08-02 10:45:44 -0700394#define foreach_sock_msg \
395_(SESSION_ENABLE_DISABLE_REPLY, session_enable_disable_reply) \
396_(BIND_SOCK_REPLY, bind_sock_reply) \
397_(UNBIND_SOCK_REPLY, unbind_sock_reply) \
Florin Coras6d4bb422018-09-04 22:07:27 -0700398_(CONNECT_SESSION_REPLY, connect_session_reply) \
Florin Corasab2f6db2018-08-31 14:31:41 -0700399_(DISCONNECT_SESSION_REPLY, disconnect_session_reply) \
Florin Coras99368312018-08-02 10:45:44 -0700400_(APPLICATION_ATTACH_REPLY, application_attach_reply) \
401_(APPLICATION_DETACH_REPLY, application_detach_reply) \
Ping Yu34a3a082018-11-30 19:16:17 -0500402_(APPLICATION_TLS_CERT_ADD_REPLY, application_tls_cert_add_reply) \
403_(APPLICATION_TLS_KEY_ADD_REPLY, application_tls_key_add_reply) \
Florin Coras99368312018-08-02 10:45:44 -0700404_(MAP_ANOTHER_SEGMENT, map_another_segment) \
405_(UNMAP_SEGMENT, unmap_segment) \
406_(APP_CUT_THROUGH_REGISTRATION_ADD, app_cut_through_registration_add) \
Florin Coras134a9962018-08-28 11:32:04 -0700407_(APP_WORKER_ADD_DEL_REPLY, app_worker_add_del_reply) \
Florin Coras697faea2018-06-27 17:10:49 -0700408
409void
410vppcom_api_hookup (void)
411{
Florin Coras60116992018-08-27 09:52:18 -0700412#define _(N, n) \
413 vl_msg_api_set_handlers(VL_API_##N, #n, \
Florin Coras697faea2018-06-27 17:10:49 -0700414 vl_api_##n##_t_handler, \
415 vl_noop_handler, \
416 vl_api_##n##_t_endian, \
417 vl_api_##n##_t_print, \
418 sizeof(vl_api_##n##_t), 1);
419 foreach_sock_msg;
420#undef _
421}
422
423/*
424 * VPP-API message functions
425 */
426void
427vppcom_send_session_enable_disable (u8 is_enable)
428{
Florin Coras47c40e22018-11-26 17:01:36 -0800429 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras697faea2018-06-27 17:10:49 -0700430 vl_api_session_enable_disable_t *bmp;
431 bmp = vl_msg_api_alloc (sizeof (*bmp));
432 memset (bmp, 0, sizeof (*bmp));
433
434 bmp->_vl_msg_id = ntohs (VL_API_SESSION_ENABLE_DISABLE);
Florin Coras47c40e22018-11-26 17:01:36 -0800435 bmp->client_index = wrk->my_client_index;
Florin Coras697faea2018-06-27 17:10:49 -0700436 bmp->context = htonl (0xfeedface);
437 bmp->is_enable = is_enable;
Florin Coras47c40e22018-11-26 17:01:36 -0800438 vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & bmp);
Florin Coras697faea2018-06-27 17:10:49 -0700439}
440
441void
442vppcom_app_send_attach (void)
443{
Florin Coras47c40e22018-11-26 17:01:36 -0800444 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras697faea2018-06-27 17:10:49 -0700445 vl_api_application_attach_t *bmp;
446 u8 nsid_len = vec_len (vcm->cfg.namespace_id);
447 u8 app_is_proxy = (vcm->cfg.app_proxy_transport_tcp ||
448 vcm->cfg.app_proxy_transport_udp);
449
450 bmp = vl_msg_api_alloc (sizeof (*bmp));
451 memset (bmp, 0, sizeof (*bmp));
452
453 bmp->_vl_msg_id = ntohs (VL_API_APPLICATION_ATTACH);
Florin Coras47c40e22018-11-26 17:01:36 -0800454 bmp->client_index = wrk->my_client_index;
Florin Coras697faea2018-06-27 17:10:49 -0700455 bmp->context = htonl (0xfeedface);
456 bmp->options[APP_OPTIONS_FLAGS] =
457 APP_OPTIONS_FLAGS_ACCEPT_REDIRECT | APP_OPTIONS_FLAGS_ADD_SEGMENT |
458 (vcm->cfg.app_scope_local ? APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE : 0) |
459 (vcm->cfg.app_scope_global ? APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE : 0) |
Florin Coras54693d22018-07-17 10:46:29 -0700460 (app_is_proxy ? APP_OPTIONS_FLAGS_IS_PROXY : 0) |
Florin Coras99368312018-08-02 10:45:44 -0700461 APP_OPTIONS_FLAGS_USE_MQ_FOR_CTRL_MSGS |
462 (vcm->cfg.use_mq_eventfd ? APP_OPTIONS_FLAGS_EVT_MQ_USE_EVENTFD : 0);
Florin Coras697faea2018-06-27 17:10:49 -0700463 bmp->options[APP_OPTIONS_PROXY_TRANSPORT] =
464 (u64) ((vcm->cfg.app_proxy_transport_tcp ? 1 << TRANSPORT_PROTO_TCP : 0) |
465 (vcm->cfg.app_proxy_transport_udp ? 1 << TRANSPORT_PROTO_UDP : 0));
466 bmp->options[APP_OPTIONS_SEGMENT_SIZE] = vcm->cfg.segment_size;
467 bmp->options[APP_OPTIONS_ADD_SEGMENT_SIZE] = vcm->cfg.add_segment_size;
468 bmp->options[APP_OPTIONS_RX_FIFO_SIZE] = vcm->cfg.rx_fifo_size;
469 bmp->options[APP_OPTIONS_TX_FIFO_SIZE] = vcm->cfg.tx_fifo_size;
470 bmp->options[APP_OPTIONS_PREALLOC_FIFO_PAIRS] =
471 vcm->cfg.preallocated_fifo_pairs;
472 bmp->options[APP_OPTIONS_EVT_QUEUE_SIZE] = vcm->cfg.event_queue_size;
Ping Yu34a3a082018-11-30 19:16:17 -0500473 bmp->options[APP_OPTIONS_TLS_ENGINE] = TLS_ENGINE_OPENSSL;
Florin Coras697faea2018-06-27 17:10:49 -0700474 if (nsid_len)
475 {
476 bmp->namespace_id_len = nsid_len;
Dave Barach178cf492018-11-13 16:34:13 -0500477 clib_memcpy_fast (bmp->namespace_id, vcm->cfg.namespace_id, nsid_len);
Florin Coras697faea2018-06-27 17:10:49 -0700478 bmp->options[APP_OPTIONS_NAMESPACE_SECRET] = vcm->cfg.namespace_secret;
479 }
Florin Coras47c40e22018-11-26 17:01:36 -0800480 vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & bmp);
Florin Coras697faea2018-06-27 17:10:49 -0700481}
482
483void
484vppcom_app_send_detach (void)
485{
Florin Coras47c40e22018-11-26 17:01:36 -0800486 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras697faea2018-06-27 17:10:49 -0700487 vl_api_application_detach_t *bmp;
488 bmp = vl_msg_api_alloc (sizeof (*bmp));
489 memset (bmp, 0, sizeof (*bmp));
490
491 bmp->_vl_msg_id = ntohs (VL_API_APPLICATION_DETACH);
Florin Coras47c40e22018-11-26 17:01:36 -0800492 bmp->client_index = wrk->my_client_index;
Florin Coras697faea2018-06-27 17:10:49 -0700493 bmp->context = htonl (0xfeedface);
Florin Coras47c40e22018-11-26 17:01:36 -0800494 vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & bmp);
Florin Coras697faea2018-06-27 17:10:49 -0700495}
496
497void
Florin Coras134a9962018-08-28 11:32:04 -0700498vcl_send_app_worker_add_del (u8 is_add)
499{
500 vcl_worker_t *wrk = vcl_worker_get_current ();
501 vl_api_app_worker_add_del_t *mp;
Florin Coras134a9962018-08-28 11:32:04 -0700502
503 mp = vl_msg_api_alloc (sizeof (*mp));
504 memset (mp, 0, sizeof (*mp));
505
506 mp->_vl_msg_id = ntohs (VL_API_APP_WORKER_ADD_DEL);
Florin Coras47c40e22018-11-26 17:01:36 -0800507 mp->client_index = wrk->my_client_index;
Florin Corasc1f5a432018-11-20 11:31:26 -0800508 mp->app_index = clib_host_to_net_u32 (vcm->app_index);
Florin Coras01f3f892018-12-02 12:45:53 -0800509 mp->context = wrk->wrk_index;
Florin Coras134a9962018-08-28 11:32:04 -0700510 mp->is_add = is_add;
511 if (!is_add)
Florin Coras01f3f892018-12-02 12:45:53 -0800512 mp->wrk_index = clib_host_to_net_u32 (wrk->vpp_wrk_index);
513
514 vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & mp);
515}
516
517void
518vcl_send_child_worker_del (vcl_worker_t * child_wrk)
519{
520 vcl_worker_t *wrk = vcl_worker_get_current ();
521 vl_api_app_worker_add_del_t *mp;
522
523 mp = vl_msg_api_alloc (sizeof (*mp));
524 memset (mp, 0, sizeof (*mp));
525
526 mp->_vl_msg_id = ntohs (VL_API_APP_WORKER_ADD_DEL);
527 mp->client_index = wrk->my_client_index;
528 mp->app_index = clib_host_to_net_u32 (vcm->app_index);
529 mp->context = wrk->wrk_index;
530 mp->is_add = 0;
531 mp->wrk_index = clib_host_to_net_u32 (child_wrk->vpp_wrk_index);
Florin Coras134a9962018-08-28 11:32:04 -0700532
Florin Coras47c40e22018-11-26 17:01:36 -0800533 vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & mp);
Florin Coras134a9962018-08-28 11:32:04 -0700534}
535
536void
537vppcom_send_connect_sock (vcl_session_t * session)
Florin Coras697faea2018-06-27 17:10:49 -0700538{
Florin Coras053a0e42018-11-13 15:52:38 -0800539 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras697faea2018-06-27 17:10:49 -0700540 vl_api_connect_sock_t *cmp;
541
Florin Coras697faea2018-06-27 17:10:49 -0700542 cmp = vl_msg_api_alloc (sizeof (*cmp));
543 memset (cmp, 0, sizeof (*cmp));
544 cmp->_vl_msg_id = ntohs (VL_API_CONNECT_SOCK);
Florin Coras47c40e22018-11-26 17:01:36 -0800545 cmp->client_index = wrk->my_client_index;
Florin Coras134a9962018-08-28 11:32:04 -0700546 cmp->context = session->session_index;
Florin Coras053a0e42018-11-13 15:52:38 -0800547 cmp->wrk_index = wrk->vpp_wrk_index;
Florin Coras697faea2018-06-27 17:10:49 -0700548 cmp->is_ip4 = session->transport.is_ip4;
Dave Barach178cf492018-11-13 16:34:13 -0500549 clib_memcpy_fast (cmp->ip, &session->transport.rmt_ip, sizeof (cmp->ip));
Florin Coras697faea2018-06-27 17:10:49 -0700550 cmp->port = session->transport.rmt_port;
551 cmp->proto = session->session_type;
Dave Barach178cf492018-11-13 16:34:13 -0500552 clib_memcpy_fast (cmp->options, session->options, sizeof (cmp->options));
Florin Coras47c40e22018-11-26 17:01:36 -0800553 vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & cmp);
Florin Coras697faea2018-06-27 17:10:49 -0700554}
555
556void
Florin Corasab2f6db2018-08-31 14:31:41 -0700557vppcom_send_disconnect_session (u64 vpp_handle)
Florin Coras697faea2018-06-27 17:10:49 -0700558{
Florin Coras47c40e22018-11-26 17:01:36 -0800559 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras697faea2018-06-27 17:10:49 -0700560 vl_api_disconnect_session_t *dmp;
561
Florin Coras697faea2018-06-27 17:10:49 -0700562 dmp = vl_msg_api_alloc (sizeof (*dmp));
563 memset (dmp, 0, sizeof (*dmp));
564 dmp->_vl_msg_id = ntohs (VL_API_DISCONNECT_SESSION);
Florin Coras47c40e22018-11-26 17:01:36 -0800565 dmp->client_index = wrk->my_client_index;
Florin Coras697faea2018-06-27 17:10:49 -0700566 dmp->handle = vpp_handle;
Florin Coras47c40e22018-11-26 17:01:36 -0800567 vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & dmp);
Florin Coras697faea2018-06-27 17:10:49 -0700568}
569
570/* VPP combines bind and listen as one operation. VCL manages the separation
571 * of bind and listen locally via vppcom_session_bind() and
572 * vppcom_session_listen() */
573void
Florin Coras134a9962018-08-28 11:32:04 -0700574vppcom_send_bind_sock (vcl_session_t * session)
Florin Coras697faea2018-06-27 17:10:49 -0700575{
Florin Coras053a0e42018-11-13 15:52:38 -0800576 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras697faea2018-06-27 17:10:49 -0700577 vl_api_bind_sock_t *bmp;
578
579 /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
580 bmp = vl_msg_api_alloc (sizeof (*bmp));
581 memset (bmp, 0, sizeof (*bmp));
582
583 bmp->_vl_msg_id = ntohs (VL_API_BIND_SOCK);
Florin Coras47c40e22018-11-26 17:01:36 -0800584 bmp->client_index = wrk->my_client_index;
Florin Coras134a9962018-08-28 11:32:04 -0700585 bmp->context = session->session_index;
Florin Coras053a0e42018-11-13 15:52:38 -0800586 bmp->wrk_index = wrk->vpp_wrk_index;
Florin Coras697faea2018-06-27 17:10:49 -0700587 bmp->is_ip4 = session->transport.is_ip4;
Dave Barach178cf492018-11-13 16:34:13 -0500588 clib_memcpy_fast (bmp->ip, &session->transport.lcl_ip, sizeof (bmp->ip));
Florin Coras697faea2018-06-27 17:10:49 -0700589 bmp->port = session->transport.lcl_port;
590 bmp->proto = session->session_type;
Dave Barach178cf492018-11-13 16:34:13 -0500591 clib_memcpy_fast (bmp->options, session->options, sizeof (bmp->options));
Florin Coras47c40e22018-11-26 17:01:36 -0800592 vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & bmp);
Florin Coras697faea2018-06-27 17:10:49 -0700593}
594
595void
Florin Coras2d675d72019-01-28 15:54:27 -0800596vppcom_send_unbind_sock (vcl_worker_t * wrk, u64 vpp_handle)
Florin Coras697faea2018-06-27 17:10:49 -0700597{
598 vl_api_unbind_sock_t *ump;
599
600 ump = vl_msg_api_alloc (sizeof (*ump));
601 memset (ump, 0, sizeof (*ump));
602
603 ump->_vl_msg_id = ntohs (VL_API_UNBIND_SOCK);
Florin Coras47c40e22018-11-26 17:01:36 -0800604 ump->client_index = wrk->my_client_index;
Florin Coras053a0e42018-11-13 15:52:38 -0800605 ump->wrk_index = wrk->vpp_wrk_index;
Florin Coras697faea2018-06-27 17:10:49 -0700606 ump->handle = vpp_handle;
Florin Corasdfae9f92019-02-20 19:48:31 -0800607 ump->context = wrk->wrk_index;
Florin Coras47c40e22018-11-26 17:01:36 -0800608 vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & ump);
Florin Coras697faea2018-06-27 17:10:49 -0700609}
610
611void
Ping Yu34a3a082018-11-30 19:16:17 -0500612vppcom_send_application_tls_cert_add (vcl_session_t * session, char *cert,
613 u32 cert_len)
614{
615 vcl_worker_t *wrk = vcl_worker_get_current ();
616 vl_api_application_tls_cert_add_t *cert_mp;
617
618 cert_mp = vl_msg_api_alloc (sizeof (*cert_mp) + cert_len);
619 clib_memset (cert_mp, 0, sizeof (*cert_mp));
620 cert_mp->_vl_msg_id = ntohs (VL_API_APPLICATION_TLS_CERT_ADD);
621 cert_mp->client_index = wrk->my_client_index;
622 cert_mp->context = session->session_index;
623 cert_mp->cert_len = clib_host_to_net_u16 (cert_len);
624 clib_memcpy_fast (cert_mp->cert, cert, cert_len);
625 vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & cert_mp);
626
627}
628
629void
630vppcom_send_application_tls_key_add (vcl_session_t * session, char *key,
631 u32 key_len)
632{
633 vcl_worker_t *wrk = vcl_worker_get_current ();
634 vl_api_application_tls_key_add_t *key_mp;
635
636 key_mp = vl_msg_api_alloc (sizeof (*key_mp) + key_len);
637 clib_memset (key_mp, 0, sizeof (*key_mp));
638 key_mp->_vl_msg_id = ntohs (VL_API_APPLICATION_TLS_KEY_ADD);
639 key_mp->client_index = wrk->my_client_index;
640 key_mp->context = session->session_index;
641 key_mp->key_len = clib_host_to_net_u16 (key_len);
642 clib_memcpy_fast (key_mp->key, key, key_len);
643 vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & key_mp);
644
645}
646
Florin Coras697faea2018-06-27 17:10:49 -0700647u32
648vcl_max_nsid_len (void)
649{
650 vl_api_application_attach_t *mp;
651 return (sizeof (mp->namespace_id) - 1);
652}
653
654void
655vppcom_init_error_string_table (void)
656{
657 vcm->error_string_by_error_number = hash_create (0, sizeof (uword));
658
659#define _(n, v, s) hash_set (vcm->error_string_by_error_number, -v, s);
660 foreach_vnet_api_error;
661#undef _
662
663 hash_set (vcm->error_string_by_error_number, 99, "Misc");
664}
665
666int
667vppcom_connect_to_vpp (char *app_name)
668{
Florin Coras47c40e22018-11-26 17:01:36 -0800669 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras697faea2018-06-27 17:10:49 -0700670 api_main_t *am = &api_main;
671 vppcom_cfg_t *vcl_cfg = &vcm->cfg;
Florin Coras697faea2018-06-27 17:10:49 -0700672
Florin Coras99368312018-08-02 10:45:44 -0700673 if (vcl_cfg->vpp_api_socket_name)
Florin Coras697faea2018-06-27 17:10:49 -0700674 {
Florin Coras99368312018-08-02 10:45:44 -0700675 if (vl_socket_client_connect ((char *) vcl_cfg->vpp_api_socket_name,
676 app_name, 0 /* default rx/tx buffer */ ))
677 {
Florin Coras053a0e42018-11-13 15:52:38 -0800678 VERR ("app (%s) socket connect failed!", app_name);
Florin Coras99368312018-08-02 10:45:44 -0700679 return VPPCOM_ECONNREFUSED;
680 }
681
Tomasz Kulasek97dcf5b2019-01-31 18:26:32 +0100682 if (vl_socket_client_init_shm (0, 1 /* want_pthread */ ))
Florin Coras99368312018-08-02 10:45:44 -0700683 {
Florin Coras053a0e42018-11-13 15:52:38 -0800684 VERR ("app (%s) init shm failed!", app_name);
Florin Coras99368312018-08-02 10:45:44 -0700685 return VPPCOM_ECONNREFUSED;
686 }
Florin Coras697faea2018-06-27 17:10:49 -0700687 }
688 else
689 {
Florin Coras99368312018-08-02 10:45:44 -0700690 if (!vcl_cfg->vpp_api_filename)
691 vcl_cfg->vpp_api_filename = format (0, "/vpe-api%c", 0);
Florin Coras697faea2018-06-27 17:10:49 -0700692
Florin Coras053a0e42018-11-13 15:52:38 -0800693 VDBG (0, "app (%s) connecting to VPP api (%s)...",
Florin Coras99368312018-08-02 10:45:44 -0700694 app_name, vcl_cfg->vpp_api_filename);
695
696 if (vl_client_connect_to_vlib ((char *) vcl_cfg->vpp_api_filename,
697 app_name, vcm->cfg.vpp_api_q_length) < 0)
698 {
Florin Coras053a0e42018-11-13 15:52:38 -0800699 VERR ("app (%s) connect failed!", app_name);
Florin Coras99368312018-08-02 10:45:44 -0700700 return VPPCOM_ECONNREFUSED;
701 }
702
Florin Coras697faea2018-06-27 17:10:49 -0700703 }
704
Florin Coras47c40e22018-11-26 17:01:36 -0800705 wrk->vl_input_queue = am->shmem_hdr->vl_input_queue;
706 wrk->my_client_index = (u32) am->my_client_index;
707 wrk->wrk_state = STATE_APP_CONN_VPP;
Florin Coras99368312018-08-02 10:45:44 -0700708
Florin Coras053a0e42018-11-13 15:52:38 -0800709 VDBG (0, "app (%s) is connected to VPP!", app_name);
Florin Coras697faea2018-06-27 17:10:49 -0700710 vcl_evt (VCL_EVT_INIT, vcm);
Florin Coras99368312018-08-02 10:45:44 -0700711 return VPPCOM_OK;
Florin Coras697faea2018-06-27 17:10:49 -0700712}
713
714/*
715 * fd.io coding-style-patch-verification: ON
716 *
717 * Local Variables:
718 * eval: (c-set-style "gnu")
719 * End:
720 */