blob: b28398101b3ba9c0e908d4e8656e0f793228644c [file] [log] [blame]
Florin Coras697faea2018-06-27 17:10:49 -07001/*
Florin Coras5e062572019-03-14 19:07:51 -07002 * Copyright (c) 2018-2019 Cisco and/or its affiliates.
Florin Coras697faea2018-06-27 17:10:49 -07003 * 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{
Florin Coras88001c62019-04-24 14:44:46 -070068 fifo_segment_create_args_t _a, *a = &_a;
Florin Coras99368312018-08-02 10:45:44 -070069 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 Coras88001c62019-04-24 14:44:46 -070078 if ((rv = 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 Coras88001c62019-04-24 14:44:46 -070091 fifo_segment_main_t *sm = &vcm->segment_main;
92 fifo_segment_t *segment;
Florin Corasd85de682018-11-29 17:02:29 -080093 u32 segment_index;
94
95 segment_index = vcl_segment_table_lookup (segment_handle);
96 if (segment_index == (u32) ~ 0)
97 return;
Florin Coras88001c62019-04-24 14:44:46 -070098 segment = fifo_segment_get_segment (sm, segment_index);
99 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 {
Florin Corasa5efca32019-03-20 08:33:10 -0700121 VERR ("attach failed: %U", format_api_error, ntohl (mp->retval));
122 goto failed;
Florin Coras697faea2018-06-27 17:10:49 -0700123 }
124
Florin Coras134a9962018-08-28 11:32:04 -0700125 wrk->app_event_queue = uword_to_pointer (mp->app_event_queue_address,
Florin Coras99368312018-08-02 10:45:44 -0700126 svm_msg_q_t *);
Florin Corasd85de682018-11-29 17:02:29 -0800127 segment_handle = clib_net_to_host_u64 (mp->segment_handle);
128 if (segment_handle == VCL_INVALID_SEGMENT_HANDLE)
129 {
Florin Corasa5efca32019-03-20 08:33:10 -0700130 VERR ("invalid segment handle");
131 goto failed;
Florin Corasd85de682018-11-29 17:02:29 -0800132 }
133
Florin Coras99368312018-08-02 10:45:44 -0700134 if (mp->n_fds)
Florin Coras697faea2018-06-27 17:10:49 -0700135 {
Florin Coras99368312018-08-02 10:45:44 -0700136 vec_validate (fds, mp->n_fds);
137 vl_socket_client_recv_fd_msg (fds, mp->n_fds, 5);
138
139 if (mp->fd_flags & SESSION_FD_F_VPP_MQ_SEGMENT)
Florin Corasd85de682018-11-29 17:02:29 -0800140 if (vcl_segment_attach (vcl_vpp_worker_segment_handle (0),
141 "vpp-mq-seg", SSVM_SEGMENT_MEMFD,
142 fds[n_fds++]))
Florin Corasa5efca32019-03-20 08:33:10 -0700143 goto failed;
Florin Coras99368312018-08-02 10:45:44 -0700144
145 if (mp->fd_flags & SESSION_FD_F_MEMFD_SEGMENT)
Florin Corasd85de682018-11-29 17:02:29 -0800146 if (vcl_segment_attach (segment_handle, (char *) mp->segment_name,
147 SSVM_SEGMENT_MEMFD, fds[n_fds++]))
Florin Corasa5efca32019-03-20 08:33:10 -0700148 goto failed;
Florin Coras99368312018-08-02 10:45:44 -0700149
150 if (mp->fd_flags & SESSION_FD_F_MQ_EVENTFD)
151 {
Florin Coras134a9962018-08-28 11:32:04 -0700152 svm_msg_q_set_consumer_eventfd (wrk->app_event_queue, fds[n_fds]);
153 vcl_mq_epoll_add_evfd (wrk, wrk->app_event_queue);
Florin Coras99368312018-08-02 10:45:44 -0700154 n_fds++;
155 }
156
157 vec_free (fds);
Florin Coras697faea2018-06-27 17:10:49 -0700158 }
Florin Coras99368312018-08-02 10:45:44 -0700159 else
Florin Coras697faea2018-06-27 17:10:49 -0700160 {
Florin Corasd85de682018-11-29 17:02:29 -0800161 if (vcl_segment_attach (segment_handle, (char *) mp->segment_name,
162 SSVM_SEGMENT_SHM, -1))
Florin Corasa5efca32019-03-20 08:33:10 -0700163 goto failed;
Florin Coras697faea2018-06-27 17:10:49 -0700164 }
165
Florin Corasc1f5a432018-11-20 11:31:26 -0800166 vcm->app_index = clib_net_to_host_u32 (mp->app_index);
Florin Coras697faea2018-06-27 17:10:49 -0700167 vcm->app_state = STATE_APP_ATTACHED;
Florin Corasa5efca32019-03-20 08:33:10 -0700168 return;
169
170failed:
171 vcm->app_state = STATE_APP_FAILED;
Florin Coras697faea2018-06-27 17:10:49 -0700172}
173
174static void
Florin Coras134a9962018-08-28 11:32:04 -0700175vl_api_app_worker_add_del_reply_t_handler (vl_api_app_worker_add_del_reply_t *
176 mp)
177{
Florin Corasab2f6db2018-08-31 14:31:41 -0700178 int n_fds = 0, *fds = 0;
Florin Corasd85de682018-11-29 17:02:29 -0800179 u64 segment_handle;
Florin Coras134a9962018-08-28 11:32:04 -0700180 vcl_worker_t *wrk;
Florin Corasab2f6db2018-08-31 14:31:41 -0700181 u32 wrk_index;
Florin Coras134a9962018-08-28 11:32:04 -0700182
183 if (mp->retval)
184 {
185 clib_warning ("VCL<%d>: add/del worker failed: %U", getpid (),
186 format_api_error, ntohl (mp->retval));
187 goto failed;
188 }
Florin Coras053a0e42018-11-13 15:52:38 -0800189
Florin Coras3348a4c2018-09-07 17:09:35 -0700190 if (!mp->is_add)
191 return;
192
Florin Corasdc2e2512018-12-03 17:47:26 -0800193 wrk_index = mp->context;
Florin Coras01f3f892018-12-02 12:45:53 -0800194 wrk = vcl_worker_get_if_valid (wrk_index);
195 if (!wrk)
196 return;
197
Florin Corasdc2e2512018-12-03 17:47:26 -0800198 wrk->vpp_wrk_index = clib_net_to_host_u32 (mp->wrk_index);
Florin Coras134a9962018-08-28 11:32:04 -0700199 wrk->app_event_queue = uword_to_pointer (mp->app_event_queue_address,
200 svm_msg_q_t *);
201
Florin Corasd85de682018-11-29 17:02:29 -0800202 segment_handle = clib_net_to_host_u64 (mp->segment_handle);
203 if (segment_handle == VCL_INVALID_SEGMENT_HANDLE)
204 {
205 clib_warning ("invalid segment handle");
Florin Coras01f3f892018-12-02 12:45:53 -0800206 goto failed;
Florin Corasd85de682018-11-29 17:02:29 -0800207 }
208
Florin Coras134a9962018-08-28 11:32:04 -0700209 if (mp->n_fds)
210 {
211 vec_validate (fds, mp->n_fds);
212 vl_socket_client_recv_fd_msg (fds, mp->n_fds, 5);
213
214 if (mp->fd_flags & SESSION_FD_F_VPP_MQ_SEGMENT)
Florin Coras01f3f892018-12-02 12:45:53 -0800215 if (vcl_segment_attach (vcl_vpp_worker_segment_handle (wrk_index),
216 "vpp-worker-seg", SSVM_SEGMENT_MEMFD,
217 fds[n_fds++]))
Florin Coras134a9962018-08-28 11:32:04 -0700218 goto failed;
219
220 if (mp->fd_flags & SESSION_FD_F_MEMFD_SEGMENT)
Florin Corasd85de682018-11-29 17:02:29 -0800221 if (vcl_segment_attach (segment_handle, (char *) mp->segment_name,
222 SSVM_SEGMENT_MEMFD, fds[n_fds++]))
Florin Coras134a9962018-08-28 11:32:04 -0700223 goto failed;
224
225 if (mp->fd_flags & SESSION_FD_F_MQ_EVENTFD)
226 {
227 svm_msg_q_set_consumer_eventfd (wrk->app_event_queue, fds[n_fds]);
228 vcl_mq_epoll_add_evfd (wrk, wrk->app_event_queue);
229 n_fds++;
230 }
231
232 vec_free (fds);
233 }
234 else
235 {
Florin Corasd85de682018-11-29 17:02:29 -0800236 if (vcl_segment_attach (segment_handle, (char *) mp->segment_name,
237 SSVM_SEGMENT_SHM, -1))
Florin Coras134a9962018-08-28 11:32:04 -0700238 goto failed;
239 }
240 vcm->app_state = STATE_APP_READY;
Florin Coras053a0e42018-11-13 15:52:38 -0800241 VDBG (0, "worker %u vpp-worker %u added", wrk_index, wrk->vpp_wrk_index);
Florin Coras134a9962018-08-28 11:32:04 -0700242 return;
243
244failed:
245 vcm->app_state = STATE_APP_FAILED;
246}
247
248static void
Florin Coras697faea2018-06-27 17:10:49 -0700249vl_api_application_detach_reply_t_handler (vl_api_application_detach_reply_t *
250 mp)
251{
252 if (mp->retval)
253 clib_warning ("VCL<%d>: detach failed: %U", getpid (), format_api_error,
254 ntohl (mp->retval));
255
256 vcm->app_state = STATE_APP_ENABLED;
257}
258
259static void
Florin Coras697faea2018-06-27 17:10:49 -0700260vl_api_map_another_segment_t_handler (vl_api_map_another_segment_t * mp)
261{
Florin Coras99368312018-08-02 10:45:44 -0700262 ssvm_segment_type_t seg_type = SSVM_SEGMENT_SHM;
Florin Corasd85de682018-11-29 17:02:29 -0800263 u64 segment_handle;
Florin Coras99368312018-08-02 10:45:44 -0700264 int fd = -1;
Florin Coras697faea2018-06-27 17:10:49 -0700265
Florin Coras99368312018-08-02 10:45:44 -0700266 if (mp->fd_flags)
267 {
268 vl_socket_client_recv_fd_msg (&fd, 1, 5);
269 seg_type = SSVM_SEGMENT_MEMFD;
270 }
271
Florin Corasd85de682018-11-29 17:02:29 -0800272 segment_handle = clib_net_to_host_u64 (mp->segment_handle);
273 if (segment_handle == VCL_INVALID_SEGMENT_HANDLE)
274 {
275 clib_warning ("invalid segment handle");
276 return;
277 }
278
279 if (vcl_segment_attach (segment_handle, (char *) mp->segment_name,
280 seg_type, fd))
Florin Coras697faea2018-06-27 17:10:49 -0700281 {
282 clib_warning ("VCL<%d>: svm_fifo_segment_attach ('%s') failed",
283 getpid (), mp->segment_name);
284 return;
285 }
286
287 VDBG (1, "VCL<%d>: mapped new segment '%s' size %d", getpid (),
288 mp->segment_name, mp->segment_size);
289}
290
291static void
292vl_api_unmap_segment_t_handler (vl_api_unmap_segment_t * mp)
293{
Florin Corasd85de682018-11-29 17:02:29 -0800294 u64 segment_handle = clib_net_to_host_u64 (mp->segment_handle);
295 vcl_segment_detach (segment_handle);
296 VDBG (1, "Unmapped segment: %d", segment_handle);
Florin Coras697faea2018-06-27 17:10:49 -0700297}
298
299static void
Florin Coras697faea2018-06-27 17:10:49 -0700300vl_api_bind_sock_reply_t_handler (vl_api_bind_sock_reply_t * mp)
301{
Florin Coras60116992018-08-27 09:52:18 -0700302 /* Expecting a similar message on mq. So ignore this */
Florin Coras64424012019-03-02 10:47:47 -0800303 VDBG (0, "bapi bind retval: %u!", mp->retval);
Florin Coras697faea2018-06-27 17:10:49 -0700304}
305
306static void
307vl_api_unbind_sock_reply_t_handler (vl_api_unbind_sock_reply_t * mp)
308{
309 if (mp->retval)
Florin Corasdfae9f92019-02-20 19:48:31 -0800310 VDBG (0, "ERROR: sid %u: unbind failed: %U", mp->context,
311 format_api_error, ntohl (mp->retval));
Florin Coras697faea2018-06-27 17:10:49 -0700312
Florin Corasdfae9f92019-02-20 19:48:31 -0800313 VDBG (1, "sid %u: unbind succeeded!", mp->context);
314
Florin Coras697faea2018-06-27 17:10:49 -0700315}
316
Florin Corasab2f6db2018-08-31 14:31:41 -0700317static void
318vl_api_disconnect_session_reply_t_handler (vl_api_disconnect_session_reply_t *
319 mp)
320{
321 if (mp->retval)
Florin Coras64424012019-03-02 10:47:47 -0800322 VDBG (0, "ERROR: sid %u: disconnect failed: %U", mp->context,
323 format_api_error, ntohl (mp->retval));
Florin Corasab2f6db2018-08-31 14:31:41 -0700324}
325
326static void
Florin Coras64424012019-03-02 10:47:47 -0800327vl_api_connect_sock_reply_t_handler (vl_api_connect_sock_reply_t * mp)
Florin Corasab2f6db2018-08-31 14:31:41 -0700328{
329 if (mp->retval)
Florin Coras64424012019-03-02 10:47:47 -0800330 VDBG (0, "ERROR: connect failed: %U", format_api_error,
331 ntohl (mp->retval));
Florin Corasab2f6db2018-08-31 14:31:41 -0700332}
333
Ping Yu34a3a082018-11-30 19:16:17 -0500334static void
335 vl_api_application_tls_cert_add_reply_t_handler
336 (vl_api_application_tls_cert_add_reply_t * mp)
337{
338 if (mp->retval)
339 {
340 clib_warning ("VCL<%d>: add cert failed: %U", getpid (),
341 format_api_error, ntohl (mp->retval));
342 return;
343 }
344}
345
346static void
347 vl_api_application_tls_key_add_reply_t_handler
348 (vl_api_application_tls_key_add_reply_t * mp)
349{
350 if (mp->retval)
351 {
352 clib_warning ("VCL<%d>: add key failed: %U", getpid (),
353 format_api_error, ntohl (mp->retval));
354 return;
355 }
356
357}
358
Florin Coras99368312018-08-02 10:45:44 -0700359#define foreach_sock_msg \
360_(SESSION_ENABLE_DISABLE_REPLY, session_enable_disable_reply) \
361_(BIND_SOCK_REPLY, bind_sock_reply) \
362_(UNBIND_SOCK_REPLY, unbind_sock_reply) \
Florin Coras64424012019-03-02 10:47:47 -0800363_(CONNECT_SOCK_REPLY, connect_sock_reply) \
Florin Corasab2f6db2018-08-31 14:31:41 -0700364_(DISCONNECT_SESSION_REPLY, disconnect_session_reply) \
Florin Coras99368312018-08-02 10:45:44 -0700365_(APPLICATION_ATTACH_REPLY, application_attach_reply) \
366_(APPLICATION_DETACH_REPLY, application_detach_reply) \
Ping Yu34a3a082018-11-30 19:16:17 -0500367_(APPLICATION_TLS_CERT_ADD_REPLY, application_tls_cert_add_reply) \
368_(APPLICATION_TLS_KEY_ADD_REPLY, application_tls_key_add_reply) \
Florin Coras99368312018-08-02 10:45:44 -0700369_(MAP_ANOTHER_SEGMENT, map_another_segment) \
370_(UNMAP_SEGMENT, unmap_segment) \
Florin Coras134a9962018-08-28 11:32:04 -0700371_(APP_WORKER_ADD_DEL_REPLY, app_worker_add_del_reply) \
Florin Coras697faea2018-06-27 17:10:49 -0700372
373void
374vppcom_api_hookup (void)
375{
Florin Coras60116992018-08-27 09:52:18 -0700376#define _(N, n) \
377 vl_msg_api_set_handlers(VL_API_##N, #n, \
Florin Coras697faea2018-06-27 17:10:49 -0700378 vl_api_##n##_t_handler, \
379 vl_noop_handler, \
380 vl_api_##n##_t_endian, \
381 vl_api_##n##_t_print, \
382 sizeof(vl_api_##n##_t), 1);
383 foreach_sock_msg;
384#undef _
385}
386
387/*
388 * VPP-API message functions
389 */
390void
391vppcom_send_session_enable_disable (u8 is_enable)
392{
Florin Coras47c40e22018-11-26 17:01:36 -0800393 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras697faea2018-06-27 17:10:49 -0700394 vl_api_session_enable_disable_t *bmp;
395 bmp = vl_msg_api_alloc (sizeof (*bmp));
396 memset (bmp, 0, sizeof (*bmp));
397
398 bmp->_vl_msg_id = ntohs (VL_API_SESSION_ENABLE_DISABLE);
Florin Coras47c40e22018-11-26 17:01:36 -0800399 bmp->client_index = wrk->my_client_index;
Florin Coras697faea2018-06-27 17:10:49 -0700400 bmp->context = htonl (0xfeedface);
401 bmp->is_enable = is_enable;
Florin Coras47c40e22018-11-26 17:01:36 -0800402 vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & bmp);
Florin Coras697faea2018-06-27 17:10:49 -0700403}
404
405void
406vppcom_app_send_attach (void)
407{
Florin Coras47c40e22018-11-26 17:01:36 -0800408 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras697faea2018-06-27 17:10:49 -0700409 vl_api_application_attach_t *bmp;
410 u8 nsid_len = vec_len (vcm->cfg.namespace_id);
411 u8 app_is_proxy = (vcm->cfg.app_proxy_transport_tcp ||
412 vcm->cfg.app_proxy_transport_udp);
413
414 bmp = vl_msg_api_alloc (sizeof (*bmp));
415 memset (bmp, 0, sizeof (*bmp));
416
417 bmp->_vl_msg_id = ntohs (VL_API_APPLICATION_ATTACH);
Florin Coras47c40e22018-11-26 17:01:36 -0800418 bmp->client_index = wrk->my_client_index;
Florin Coras697faea2018-06-27 17:10:49 -0700419 bmp->context = htonl (0xfeedface);
420 bmp->options[APP_OPTIONS_FLAGS] =
421 APP_OPTIONS_FLAGS_ACCEPT_REDIRECT | APP_OPTIONS_FLAGS_ADD_SEGMENT |
422 (vcm->cfg.app_scope_local ? APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE : 0) |
423 (vcm->cfg.app_scope_global ? APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE : 0) |
Florin Coras54693d22018-07-17 10:46:29 -0700424 (app_is_proxy ? APP_OPTIONS_FLAGS_IS_PROXY : 0) |
Florin Coras99368312018-08-02 10:45:44 -0700425 (vcm->cfg.use_mq_eventfd ? APP_OPTIONS_FLAGS_EVT_MQ_USE_EVENTFD : 0);
Florin Coras697faea2018-06-27 17:10:49 -0700426 bmp->options[APP_OPTIONS_PROXY_TRANSPORT] =
427 (u64) ((vcm->cfg.app_proxy_transport_tcp ? 1 << TRANSPORT_PROTO_TCP : 0) |
428 (vcm->cfg.app_proxy_transport_udp ? 1 << TRANSPORT_PROTO_UDP : 0));
429 bmp->options[APP_OPTIONS_SEGMENT_SIZE] = vcm->cfg.segment_size;
430 bmp->options[APP_OPTIONS_ADD_SEGMENT_SIZE] = vcm->cfg.add_segment_size;
431 bmp->options[APP_OPTIONS_RX_FIFO_SIZE] = vcm->cfg.rx_fifo_size;
432 bmp->options[APP_OPTIONS_TX_FIFO_SIZE] = vcm->cfg.tx_fifo_size;
433 bmp->options[APP_OPTIONS_PREALLOC_FIFO_PAIRS] =
434 vcm->cfg.preallocated_fifo_pairs;
435 bmp->options[APP_OPTIONS_EVT_QUEUE_SIZE] = vcm->cfg.event_queue_size;
Ping Yu34a3a082018-11-30 19:16:17 -0500436 bmp->options[APP_OPTIONS_TLS_ENGINE] = TLS_ENGINE_OPENSSL;
Florin Coras697faea2018-06-27 17:10:49 -0700437 if (nsid_len)
438 {
439 bmp->namespace_id_len = nsid_len;
Dave Barach178cf492018-11-13 16:34:13 -0500440 clib_memcpy_fast (bmp->namespace_id, vcm->cfg.namespace_id, nsid_len);
Florin Coras697faea2018-06-27 17:10:49 -0700441 bmp->options[APP_OPTIONS_NAMESPACE_SECRET] = vcm->cfg.namespace_secret;
442 }
Florin Coras47c40e22018-11-26 17:01:36 -0800443 vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & bmp);
Florin Coras697faea2018-06-27 17:10:49 -0700444}
445
446void
447vppcom_app_send_detach (void)
448{
Florin Coras47c40e22018-11-26 17:01:36 -0800449 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras697faea2018-06-27 17:10:49 -0700450 vl_api_application_detach_t *bmp;
451 bmp = vl_msg_api_alloc (sizeof (*bmp));
452 memset (bmp, 0, sizeof (*bmp));
453
454 bmp->_vl_msg_id = ntohs (VL_API_APPLICATION_DETACH);
Florin Coras47c40e22018-11-26 17:01:36 -0800455 bmp->client_index = wrk->my_client_index;
Florin Coras697faea2018-06-27 17:10:49 -0700456 bmp->context = htonl (0xfeedface);
Florin Coras47c40e22018-11-26 17:01:36 -0800457 vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & bmp);
Florin Coras697faea2018-06-27 17:10:49 -0700458}
459
460void
Florin Coras134a9962018-08-28 11:32:04 -0700461vcl_send_app_worker_add_del (u8 is_add)
462{
463 vcl_worker_t *wrk = vcl_worker_get_current ();
464 vl_api_app_worker_add_del_t *mp;
Florin Coras134a9962018-08-28 11:32:04 -0700465
466 mp = vl_msg_api_alloc (sizeof (*mp));
467 memset (mp, 0, sizeof (*mp));
468
469 mp->_vl_msg_id = ntohs (VL_API_APP_WORKER_ADD_DEL);
Florin Coras47c40e22018-11-26 17:01:36 -0800470 mp->client_index = wrk->my_client_index;
Florin Corasc1f5a432018-11-20 11:31:26 -0800471 mp->app_index = clib_host_to_net_u32 (vcm->app_index);
Florin Coras01f3f892018-12-02 12:45:53 -0800472 mp->context = wrk->wrk_index;
Florin Coras134a9962018-08-28 11:32:04 -0700473 mp->is_add = is_add;
474 if (!is_add)
Florin Coras01f3f892018-12-02 12:45:53 -0800475 mp->wrk_index = clib_host_to_net_u32 (wrk->vpp_wrk_index);
476
477 vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & mp);
478}
479
480void
481vcl_send_child_worker_del (vcl_worker_t * child_wrk)
482{
483 vcl_worker_t *wrk = vcl_worker_get_current ();
484 vl_api_app_worker_add_del_t *mp;
485
486 mp = vl_msg_api_alloc (sizeof (*mp));
487 memset (mp, 0, sizeof (*mp));
488
489 mp->_vl_msg_id = ntohs (VL_API_APP_WORKER_ADD_DEL);
490 mp->client_index = wrk->my_client_index;
491 mp->app_index = clib_host_to_net_u32 (vcm->app_index);
492 mp->context = wrk->wrk_index;
493 mp->is_add = 0;
494 mp->wrk_index = clib_host_to_net_u32 (child_wrk->vpp_wrk_index);
Florin Coras134a9962018-08-28 11:32:04 -0700495
Florin Coras47c40e22018-11-26 17:01:36 -0800496 vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & mp);
Florin Coras134a9962018-08-28 11:32:04 -0700497}
498
499void
500vppcom_send_connect_sock (vcl_session_t * session)
Florin Coras697faea2018-06-27 17:10:49 -0700501{
Florin Coras053a0e42018-11-13 15:52:38 -0800502 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras697faea2018-06-27 17:10:49 -0700503 vl_api_connect_sock_t *cmp;
504
Florin Coras697faea2018-06-27 17:10:49 -0700505 cmp = vl_msg_api_alloc (sizeof (*cmp));
506 memset (cmp, 0, sizeof (*cmp));
507 cmp->_vl_msg_id = ntohs (VL_API_CONNECT_SOCK);
Florin Coras47c40e22018-11-26 17:01:36 -0800508 cmp->client_index = wrk->my_client_index;
Florin Coras134a9962018-08-28 11:32:04 -0700509 cmp->context = session->session_index;
Florin Coras053a0e42018-11-13 15:52:38 -0800510 cmp->wrk_index = wrk->vpp_wrk_index;
Florin Coras697faea2018-06-27 17:10:49 -0700511 cmp->is_ip4 = session->transport.is_ip4;
Dave Barach178cf492018-11-13 16:34:13 -0500512 clib_memcpy_fast (cmp->ip, &session->transport.rmt_ip, sizeof (cmp->ip));
Florin Coras697faea2018-06-27 17:10:49 -0700513 cmp->port = session->transport.rmt_port;
514 cmp->proto = session->session_type;
Florin Coras47c40e22018-11-26 17:01:36 -0800515 vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & cmp);
Florin Coras697faea2018-06-27 17:10:49 -0700516}
517
518void
Florin Corasab2f6db2018-08-31 14:31:41 -0700519vppcom_send_disconnect_session (u64 vpp_handle)
Florin Coras697faea2018-06-27 17:10:49 -0700520{
Florin Coras47c40e22018-11-26 17:01:36 -0800521 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras697faea2018-06-27 17:10:49 -0700522 vl_api_disconnect_session_t *dmp;
523
Florin Coras697faea2018-06-27 17:10:49 -0700524 dmp = vl_msg_api_alloc (sizeof (*dmp));
525 memset (dmp, 0, sizeof (*dmp));
526 dmp->_vl_msg_id = ntohs (VL_API_DISCONNECT_SESSION);
Florin Coras47c40e22018-11-26 17:01:36 -0800527 dmp->client_index = wrk->my_client_index;
Florin Coras697faea2018-06-27 17:10:49 -0700528 dmp->handle = vpp_handle;
Florin Coras47c40e22018-11-26 17:01:36 -0800529 vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & dmp);
Florin Coras697faea2018-06-27 17:10:49 -0700530}
531
532/* VPP combines bind and listen as one operation. VCL manages the separation
533 * of bind and listen locally via vppcom_session_bind() and
534 * vppcom_session_listen() */
535void
Florin Coras134a9962018-08-28 11:32:04 -0700536vppcom_send_bind_sock (vcl_session_t * session)
Florin Coras697faea2018-06-27 17:10:49 -0700537{
Florin Coras053a0e42018-11-13 15:52:38 -0800538 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras697faea2018-06-27 17:10:49 -0700539 vl_api_bind_sock_t *bmp;
540
541 /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
542 bmp = vl_msg_api_alloc (sizeof (*bmp));
543 memset (bmp, 0, sizeof (*bmp));
544
545 bmp->_vl_msg_id = ntohs (VL_API_BIND_SOCK);
Florin Coras47c40e22018-11-26 17:01:36 -0800546 bmp->client_index = wrk->my_client_index;
Florin Coras134a9962018-08-28 11:32:04 -0700547 bmp->context = session->session_index;
Florin Coras053a0e42018-11-13 15:52:38 -0800548 bmp->wrk_index = wrk->vpp_wrk_index;
Florin Coras697faea2018-06-27 17:10:49 -0700549 bmp->is_ip4 = session->transport.is_ip4;
Dave Barach178cf492018-11-13 16:34:13 -0500550 clib_memcpy_fast (bmp->ip, &session->transport.lcl_ip, sizeof (bmp->ip));
Florin Coras697faea2018-06-27 17:10:49 -0700551 bmp->port = session->transport.lcl_port;
552 bmp->proto = session->session_type;
Florin Coras47c40e22018-11-26 17:01:36 -0800553 vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & bmp);
Florin Coras697faea2018-06-27 17:10:49 -0700554}
555
556void
Florin Coras2d675d72019-01-28 15:54:27 -0800557vppcom_send_unbind_sock (vcl_worker_t * wrk, u64 vpp_handle)
Florin Coras697faea2018-06-27 17:10:49 -0700558{
559 vl_api_unbind_sock_t *ump;
560
561 ump = vl_msg_api_alloc (sizeof (*ump));
562 memset (ump, 0, sizeof (*ump));
563
564 ump->_vl_msg_id = ntohs (VL_API_UNBIND_SOCK);
Florin Coras47c40e22018-11-26 17:01:36 -0800565 ump->client_index = wrk->my_client_index;
Florin Coras053a0e42018-11-13 15:52:38 -0800566 ump->wrk_index = wrk->vpp_wrk_index;
Florin Coras697faea2018-06-27 17:10:49 -0700567 ump->handle = vpp_handle;
Florin Corasdfae9f92019-02-20 19:48:31 -0800568 ump->context = wrk->wrk_index;
Florin Coras47c40e22018-11-26 17:01:36 -0800569 vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & ump);
Florin Coras697faea2018-06-27 17:10:49 -0700570}
571
572void
Ping Yu34a3a082018-11-30 19:16:17 -0500573vppcom_send_application_tls_cert_add (vcl_session_t * session, char *cert,
574 u32 cert_len)
575{
576 vcl_worker_t *wrk = vcl_worker_get_current ();
577 vl_api_application_tls_cert_add_t *cert_mp;
578
579 cert_mp = vl_msg_api_alloc (sizeof (*cert_mp) + cert_len);
580 clib_memset (cert_mp, 0, sizeof (*cert_mp));
581 cert_mp->_vl_msg_id = ntohs (VL_API_APPLICATION_TLS_CERT_ADD);
582 cert_mp->client_index = wrk->my_client_index;
583 cert_mp->context = session->session_index;
584 cert_mp->cert_len = clib_host_to_net_u16 (cert_len);
585 clib_memcpy_fast (cert_mp->cert, cert, cert_len);
586 vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & cert_mp);
587
588}
589
590void
591vppcom_send_application_tls_key_add (vcl_session_t * session, char *key,
592 u32 key_len)
593{
594 vcl_worker_t *wrk = vcl_worker_get_current ();
595 vl_api_application_tls_key_add_t *key_mp;
596
597 key_mp = vl_msg_api_alloc (sizeof (*key_mp) + key_len);
598 clib_memset (key_mp, 0, sizeof (*key_mp));
599 key_mp->_vl_msg_id = ntohs (VL_API_APPLICATION_TLS_KEY_ADD);
600 key_mp->client_index = wrk->my_client_index;
601 key_mp->context = session->session_index;
602 key_mp->key_len = clib_host_to_net_u16 (key_len);
603 clib_memcpy_fast (key_mp->key, key, key_len);
604 vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & key_mp);
605
606}
607
Florin Coras697faea2018-06-27 17:10:49 -0700608u32
609vcl_max_nsid_len (void)
610{
611 vl_api_application_attach_t *mp;
612 return (sizeof (mp->namespace_id) - 1);
613}
614
615void
616vppcom_init_error_string_table (void)
617{
618 vcm->error_string_by_error_number = hash_create (0, sizeof (uword));
619
620#define _(n, v, s) hash_set (vcm->error_string_by_error_number, -v, s);
621 foreach_vnet_api_error;
622#undef _
623
624 hash_set (vcm->error_string_by_error_number, 99, "Misc");
625}
626
627int
628vppcom_connect_to_vpp (char *app_name)
629{
Florin Coras47c40e22018-11-26 17:01:36 -0800630 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras697faea2018-06-27 17:10:49 -0700631 api_main_t *am = &api_main;
632 vppcom_cfg_t *vcl_cfg = &vcm->cfg;
Florin Coras697faea2018-06-27 17:10:49 -0700633
Florin Coras99368312018-08-02 10:45:44 -0700634 if (vcl_cfg->vpp_api_socket_name)
Florin Coras697faea2018-06-27 17:10:49 -0700635 {
Florin Coras99368312018-08-02 10:45:44 -0700636 if (vl_socket_client_connect ((char *) vcl_cfg->vpp_api_socket_name,
637 app_name, 0 /* default rx/tx buffer */ ))
638 {
Florin Coras053a0e42018-11-13 15:52:38 -0800639 VERR ("app (%s) socket connect failed!", app_name);
Florin Coras99368312018-08-02 10:45:44 -0700640 return VPPCOM_ECONNREFUSED;
641 }
642
Tomasz Kulasek97dcf5b2019-01-31 18:26:32 +0100643 if (vl_socket_client_init_shm (0, 1 /* want_pthread */ ))
Florin Coras99368312018-08-02 10:45:44 -0700644 {
Florin Coras053a0e42018-11-13 15:52:38 -0800645 VERR ("app (%s) init shm failed!", app_name);
Florin Coras99368312018-08-02 10:45:44 -0700646 return VPPCOM_ECONNREFUSED;
647 }
Florin Coras697faea2018-06-27 17:10:49 -0700648 }
649 else
650 {
Florin Coras99368312018-08-02 10:45:44 -0700651 if (!vcl_cfg->vpp_api_filename)
652 vcl_cfg->vpp_api_filename = format (0, "/vpe-api%c", 0);
Florin Coras697faea2018-06-27 17:10:49 -0700653
Florin Coras053a0e42018-11-13 15:52:38 -0800654 VDBG (0, "app (%s) connecting to VPP api (%s)...",
Florin Coras99368312018-08-02 10:45:44 -0700655 app_name, vcl_cfg->vpp_api_filename);
656
657 if (vl_client_connect_to_vlib ((char *) vcl_cfg->vpp_api_filename,
658 app_name, vcm->cfg.vpp_api_q_length) < 0)
659 {
Florin Coras053a0e42018-11-13 15:52:38 -0800660 VERR ("app (%s) connect failed!", app_name);
Florin Coras99368312018-08-02 10:45:44 -0700661 return VPPCOM_ECONNREFUSED;
662 }
663
Florin Coras697faea2018-06-27 17:10:49 -0700664 }
665
Florin Coras47c40e22018-11-26 17:01:36 -0800666 wrk->vl_input_queue = am->shmem_hdr->vl_input_queue;
667 wrk->my_client_index = (u32) am->my_client_index;
668 wrk->wrk_state = STATE_APP_CONN_VPP;
Florin Coras99368312018-08-02 10:45:44 -0700669
Florin Coras053a0e42018-11-13 15:52:38 -0800670 VDBG (0, "app (%s) is connected to VPP!", app_name);
Florin Coras697faea2018-06-27 17:10:49 -0700671 vcl_evt (VCL_EVT_INIT, vcm);
Florin Coras99368312018-08-02 10:45:44 -0700672 return VPPCOM_OK;
Florin Coras697faea2018-06-27 17:10:49 -0700673}
674
675/*
676 * fd.io coding-style-patch-verification: ON
677 *
678 * Local Variables:
679 * eval: (c-set-style "gnu")
680 * End:
681 */