blob: 194c136796a30992bd5f9cb2046fb9657c867648 [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 Coras458089b2019-08-21 16:20:44 -0700111vl_api_app_attach_reply_t_handler (vl_api_app_attach_reply_t * mp)
Florin Coras697faea2018-06-27 17:10:49 -0700112{
Florin Coras134a9962018-08-28 11:32:04 -0700113 vcl_worker_t *wrk = vcl_worker_get (0);
Florin Coras458089b2019-08-21 16:20:44 -0700114 svm_msg_q_t *ctrl_mq;
Florin Corasd85de682018-11-29 17:02:29 -0800115 u64 segment_handle;
Florin Coras617574d2019-07-01 07:32:31 -0700116 int *fds = 0, i;
Florin Coras99368312018-08-02 10:45:44 -0700117 u32 n_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 Coras458089b2019-08-21 16:20:44 -0700125 wrk->app_event_queue = uword_to_pointer (mp->app_mq, svm_msg_q_t *);
126 ctrl_mq = uword_to_pointer (mp->vpp_ctrl_mq, svm_msg_q_t *);
127 vec_validate (wrk->vpp_event_queues, mp->vpp_ctrl_mq_thread);
128 wrk->vpp_event_queues[mp->vpp_ctrl_mq_thread] = ctrl_mq;
Florin Coras22ba3302019-08-29 22:45:04 -0700129 vcm->ctrl_mq = wrk->ctrl_mq = ctrl_mq;
Florin Corasd85de682018-11-29 17:02:29 -0800130 segment_handle = clib_net_to_host_u64 (mp->segment_handle);
131 if (segment_handle == VCL_INVALID_SEGMENT_HANDLE)
132 {
Florin Corasa5efca32019-03-20 08:33:10 -0700133 VERR ("invalid segment handle");
134 goto failed;
Florin Corasd85de682018-11-29 17:02:29 -0800135 }
136
Florin Coras99368312018-08-02 10:45:44 -0700137 if (mp->n_fds)
Florin Coras697faea2018-06-27 17:10:49 -0700138 {
Florin Coras99368312018-08-02 10:45:44 -0700139 vec_validate (fds, mp->n_fds);
Florin Corasd4c70922019-11-28 14:21:21 -0800140 if (vl_socket_client_recv_fd_msg2 (&wrk->bapi_sock_ctx, fds, mp->n_fds,
141 5))
Florin Coras617574d2019-07-01 07:32:31 -0700142 goto failed;
Florin Coras99368312018-08-02 10:45:44 -0700143
144 if (mp->fd_flags & SESSION_FD_F_VPP_MQ_SEGMENT)
Florin Corasd85de682018-11-29 17:02:29 -0800145 if (vcl_segment_attach (vcl_vpp_worker_segment_handle (0),
146 "vpp-mq-seg", SSVM_SEGMENT_MEMFD,
147 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_MEMFD_SEGMENT)
Florin Corasd85de682018-11-29 17:02:29 -0800151 if (vcl_segment_attach (segment_handle, (char *) mp->segment_name,
152 SSVM_SEGMENT_MEMFD, fds[n_fds++]))
Florin Corasa5efca32019-03-20 08:33:10 -0700153 goto failed;
Florin Coras99368312018-08-02 10:45:44 -0700154
155 if (mp->fd_flags & SESSION_FD_F_MQ_EVENTFD)
156 {
Florin Coras134a9962018-08-28 11:32:04 -0700157 svm_msg_q_set_consumer_eventfd (wrk->app_event_queue, fds[n_fds]);
158 vcl_mq_epoll_add_evfd (wrk, wrk->app_event_queue);
Florin Coras99368312018-08-02 10:45:44 -0700159 n_fds++;
160 }
161
162 vec_free (fds);
Florin Coras697faea2018-06-27 17:10:49 -0700163 }
Florin Coras99368312018-08-02 10:45:44 -0700164 else
Florin Coras697faea2018-06-27 17:10:49 -0700165 {
Florin Corasd85de682018-11-29 17:02:29 -0800166 if (vcl_segment_attach (segment_handle, (char *) mp->segment_name,
167 SSVM_SEGMENT_SHM, -1))
Florin Corasa5efca32019-03-20 08:33:10 -0700168 goto failed;
Florin Coras697faea2018-06-27 17:10:49 -0700169 }
170
Florin Corasc1f5a432018-11-20 11:31:26 -0800171 vcm->app_index = clib_net_to_host_u32 (mp->app_index);
Florin Coras697faea2018-06-27 17:10:49 -0700172 vcm->app_state = STATE_APP_ATTACHED;
Florin Corasa5efca32019-03-20 08:33:10 -0700173 return;
174
175failed:
176 vcm->app_state = STATE_APP_FAILED;
Florin Coras617574d2019-07-01 07:32:31 -0700177 for (i = clib_max (n_fds - 1, 0); i < vec_len (fds); i++)
178 close (fds[i]);
179 vec_free (fds);
Florin Coras697faea2018-06-27 17:10:49 -0700180}
181
182static void
Florin Coras134a9962018-08-28 11:32:04 -0700183vl_api_app_worker_add_del_reply_t_handler (vl_api_app_worker_add_del_reply_t *
184 mp)
185{
Florin Coras617574d2019-07-01 07:32:31 -0700186 int n_fds = 0, *fds = 0, i;
Florin Corasd85de682018-11-29 17:02:29 -0800187 u64 segment_handle;
Florin Coras134a9962018-08-28 11:32:04 -0700188 vcl_worker_t *wrk;
Florin Corasab2f6db2018-08-31 14:31:41 -0700189 u32 wrk_index;
Florin Coras134a9962018-08-28 11:32:04 -0700190
191 if (mp->retval)
192 {
193 clib_warning ("VCL<%d>: add/del worker failed: %U", getpid (),
194 format_api_error, ntohl (mp->retval));
195 goto failed;
196 }
Florin Coras053a0e42018-11-13 15:52:38 -0800197
Florin Coras3348a4c2018-09-07 17:09:35 -0700198 if (!mp->is_add)
199 return;
200
Florin Corasdc2e2512018-12-03 17:47:26 -0800201 wrk_index = mp->context;
Florin Coras01f3f892018-12-02 12:45:53 -0800202 wrk = vcl_worker_get_if_valid (wrk_index);
203 if (!wrk)
204 return;
205
Florin Corasdc2e2512018-12-03 17:47:26 -0800206 wrk->vpp_wrk_index = clib_net_to_host_u32 (mp->wrk_index);
Florin Coras134a9962018-08-28 11:32:04 -0700207 wrk->app_event_queue = uword_to_pointer (mp->app_event_queue_address,
208 svm_msg_q_t *);
Florin Coras22ba3302019-08-29 22:45:04 -0700209 wrk->ctrl_mq = vcm->ctrl_mq;
Florin Coras134a9962018-08-28 11:32:04 -0700210
Florin Corasd85de682018-11-29 17:02:29 -0800211 segment_handle = clib_net_to_host_u64 (mp->segment_handle);
212 if (segment_handle == VCL_INVALID_SEGMENT_HANDLE)
213 {
214 clib_warning ("invalid segment handle");
Florin Coras01f3f892018-12-02 12:45:53 -0800215 goto failed;
Florin Corasd85de682018-11-29 17:02:29 -0800216 }
217
Florin Coras134a9962018-08-28 11:32:04 -0700218 if (mp->n_fds)
219 {
220 vec_validate (fds, mp->n_fds);
Florin Corasd4c70922019-11-28 14:21:21 -0800221 if (vl_socket_client_recv_fd_msg2 (&wrk->bapi_sock_ctx, fds, mp->n_fds,
222 5))
Florin Coras617574d2019-07-01 07:32:31 -0700223 goto failed;
Florin Coras134a9962018-08-28 11:32:04 -0700224
225 if (mp->fd_flags & SESSION_FD_F_VPP_MQ_SEGMENT)
Florin Coras01f3f892018-12-02 12:45:53 -0800226 if (vcl_segment_attach (vcl_vpp_worker_segment_handle (wrk_index),
227 "vpp-worker-seg", SSVM_SEGMENT_MEMFD,
228 fds[n_fds++]))
Florin Coras134a9962018-08-28 11:32:04 -0700229 goto failed;
230
231 if (mp->fd_flags & SESSION_FD_F_MEMFD_SEGMENT)
Florin Corasd85de682018-11-29 17:02:29 -0800232 if (vcl_segment_attach (segment_handle, (char *) mp->segment_name,
233 SSVM_SEGMENT_MEMFD, fds[n_fds++]))
Florin Coras134a9962018-08-28 11:32:04 -0700234 goto failed;
235
236 if (mp->fd_flags & SESSION_FD_F_MQ_EVENTFD)
237 {
238 svm_msg_q_set_consumer_eventfd (wrk->app_event_queue, fds[n_fds]);
239 vcl_mq_epoll_add_evfd (wrk, wrk->app_event_queue);
240 n_fds++;
241 }
242
243 vec_free (fds);
244 }
245 else
246 {
Florin Corasd85de682018-11-29 17:02:29 -0800247 if (vcl_segment_attach (segment_handle, (char *) mp->segment_name,
248 SSVM_SEGMENT_SHM, -1))
Florin Coras134a9962018-08-28 11:32:04 -0700249 goto failed;
250 }
251 vcm->app_state = STATE_APP_READY;
Florin Coras053a0e42018-11-13 15:52:38 -0800252 VDBG (0, "worker %u vpp-worker %u added", wrk_index, wrk->vpp_wrk_index);
Florin Coras134a9962018-08-28 11:32:04 -0700253 return;
254
255failed:
256 vcm->app_state = STATE_APP_FAILED;
Florin Coras617574d2019-07-01 07:32:31 -0700257 for (i = clib_max (n_fds - 1, 0); i < vec_len (fds); i++)
258 close (fds[i]);
259 vec_free (fds);
Florin Coras134a9962018-08-28 11:32:04 -0700260}
261
262static void
Florin Coras697faea2018-06-27 17:10:49 -0700263vl_api_map_another_segment_t_handler (vl_api_map_another_segment_t * mp)
264{
Florin Coras99368312018-08-02 10:45:44 -0700265 ssvm_segment_type_t seg_type = SSVM_SEGMENT_SHM;
Florin Corasd85de682018-11-29 17:02:29 -0800266 u64 segment_handle;
Florin Coras99368312018-08-02 10:45:44 -0700267 int fd = -1;
Florin Coras697faea2018-06-27 17:10:49 -0700268
Florin Coras99368312018-08-02 10:45:44 -0700269 if (mp->fd_flags)
270 {
271 vl_socket_client_recv_fd_msg (&fd, 1, 5);
272 seg_type = SSVM_SEGMENT_MEMFD;
273 }
274
Florin Corasd85de682018-11-29 17:02:29 -0800275 segment_handle = clib_net_to_host_u64 (mp->segment_handle);
276 if (segment_handle == VCL_INVALID_SEGMENT_HANDLE)
277 {
278 clib_warning ("invalid segment handle");
279 return;
280 }
281
282 if (vcl_segment_attach (segment_handle, (char *) mp->segment_name,
283 seg_type, fd))
Florin Coras697faea2018-06-27 17:10:49 -0700284 {
285 clib_warning ("VCL<%d>: svm_fifo_segment_attach ('%s') failed",
286 getpid (), mp->segment_name);
287 return;
288 }
289
290 VDBG (1, "VCL<%d>: mapped new segment '%s' size %d", getpid (),
291 mp->segment_name, mp->segment_size);
292}
293
294static void
295vl_api_unmap_segment_t_handler (vl_api_unmap_segment_t * mp)
296{
Florin Corasd85de682018-11-29 17:02:29 -0800297 u64 segment_handle = clib_net_to_host_u64 (mp->segment_handle);
298 vcl_segment_detach (segment_handle);
299 VDBG (1, "Unmapped segment: %d", segment_handle);
Florin Coras697faea2018-06-27 17:10:49 -0700300}
301
302static void
Ping Yu34a3a082018-11-30 19:16:17 -0500303 vl_api_application_tls_cert_add_reply_t_handler
304 (vl_api_application_tls_cert_add_reply_t * mp)
305{
306 if (mp->retval)
Florin Coras458089b2019-08-21 16:20:44 -0700307 VDBG (0, "add cert failed: %U", format_api_error, ntohl (mp->retval));
308 vcm->app_state = STATE_APP_READY;
Ping Yu34a3a082018-11-30 19:16:17 -0500309}
310
311static void
312 vl_api_application_tls_key_add_reply_t_handler
313 (vl_api_application_tls_key_add_reply_t * mp)
314{
315 if (mp->retval)
Florin Coras458089b2019-08-21 16:20:44 -0700316 VDBG (0, "add key failed: %U", format_api_error, ntohl (mp->retval));
317 vcm->app_state = STATE_APP_READY;
Ping Yu34a3a082018-11-30 19:16:17 -0500318}
319
Florin Coras99368312018-08-02 10:45:44 -0700320#define foreach_sock_msg \
321_(SESSION_ENABLE_DISABLE_REPLY, session_enable_disable_reply) \
Florin Coras458089b2019-08-21 16:20:44 -0700322_(APP_ATTACH_REPLY, app_attach_reply) \
Ping Yu34a3a082018-11-30 19:16:17 -0500323_(APPLICATION_TLS_CERT_ADD_REPLY, application_tls_cert_add_reply) \
324_(APPLICATION_TLS_KEY_ADD_REPLY, application_tls_key_add_reply) \
Florin Coras99368312018-08-02 10:45:44 -0700325_(MAP_ANOTHER_SEGMENT, map_another_segment) \
326_(UNMAP_SEGMENT, unmap_segment) \
Florin Coras134a9962018-08-28 11:32:04 -0700327_(APP_WORKER_ADD_DEL_REPLY, app_worker_add_del_reply) \
Florin Coras697faea2018-06-27 17:10:49 -0700328
329void
330vppcom_api_hookup (void)
331{
Florin Coras60116992018-08-27 09:52:18 -0700332#define _(N, n) \
333 vl_msg_api_set_handlers(VL_API_##N, #n, \
Florin Coras697faea2018-06-27 17:10:49 -0700334 vl_api_##n##_t_handler, \
335 vl_noop_handler, \
336 vl_api_##n##_t_endian, \
337 vl_api_##n##_t_print, \
338 sizeof(vl_api_##n##_t), 1);
339 foreach_sock_msg;
340#undef _
341}
342
343/*
344 * VPP-API message functions
345 */
346void
347vppcom_send_session_enable_disable (u8 is_enable)
348{
Florin Coras47c40e22018-11-26 17:01:36 -0800349 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras697faea2018-06-27 17:10:49 -0700350 vl_api_session_enable_disable_t *bmp;
351 bmp = vl_msg_api_alloc (sizeof (*bmp));
352 memset (bmp, 0, sizeof (*bmp));
353
354 bmp->_vl_msg_id = ntohs (VL_API_SESSION_ENABLE_DISABLE);
Florin Coras47c40e22018-11-26 17:01:36 -0800355 bmp->client_index = wrk->my_client_index;
Florin Coras697faea2018-06-27 17:10:49 -0700356 bmp->context = htonl (0xfeedface);
357 bmp->is_enable = is_enable;
Florin Coras47c40e22018-11-26 17:01:36 -0800358 vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & bmp);
Florin Coras697faea2018-06-27 17:10:49 -0700359}
360
361void
362vppcom_app_send_attach (void)
363{
Florin Coras47c40e22018-11-26 17:01:36 -0800364 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Corasd747c3c2019-10-20 19:55:56 -0700365 u8 tls_engine = CRYPTO_ENGINE_OPENSSL;
Florin Coras458089b2019-08-21 16:20:44 -0700366 vl_api_app_attach_t *bmp;
Florin Coras697faea2018-06-27 17:10:49 -0700367 u8 nsid_len = vec_len (vcm->cfg.namespace_id);
368 u8 app_is_proxy = (vcm->cfg.app_proxy_transport_tcp ||
369 vcm->cfg.app_proxy_transport_udp);
370
Florin Corasd747c3c2019-10-20 19:55:56 -0700371 tls_engine = vcm->cfg.tls_engine ? vcm->cfg.tls_engine : tls_engine;
372
Florin Coras697faea2018-06-27 17:10:49 -0700373 bmp = vl_msg_api_alloc (sizeof (*bmp));
374 memset (bmp, 0, sizeof (*bmp));
375
Florin Coras458089b2019-08-21 16:20:44 -0700376 bmp->_vl_msg_id = ntohs (VL_API_APP_ATTACH);
Florin Coras47c40e22018-11-26 17:01:36 -0800377 bmp->client_index = wrk->my_client_index;
Florin Coras697faea2018-06-27 17:10:49 -0700378 bmp->context = htonl (0xfeedface);
379 bmp->options[APP_OPTIONS_FLAGS] =
380 APP_OPTIONS_FLAGS_ACCEPT_REDIRECT | APP_OPTIONS_FLAGS_ADD_SEGMENT |
381 (vcm->cfg.app_scope_local ? APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE : 0) |
382 (vcm->cfg.app_scope_global ? APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE : 0) |
Florin Coras54693d22018-07-17 10:46:29 -0700383 (app_is_proxy ? APP_OPTIONS_FLAGS_IS_PROXY : 0) |
Florin Coras99368312018-08-02 10:45:44 -0700384 (vcm->cfg.use_mq_eventfd ? APP_OPTIONS_FLAGS_EVT_MQ_USE_EVENTFD : 0);
Florin Coras697faea2018-06-27 17:10:49 -0700385 bmp->options[APP_OPTIONS_PROXY_TRANSPORT] =
386 (u64) ((vcm->cfg.app_proxy_transport_tcp ? 1 << TRANSPORT_PROTO_TCP : 0) |
387 (vcm->cfg.app_proxy_transport_udp ? 1 << TRANSPORT_PROTO_UDP : 0));
388 bmp->options[APP_OPTIONS_SEGMENT_SIZE] = vcm->cfg.segment_size;
389 bmp->options[APP_OPTIONS_ADD_SEGMENT_SIZE] = vcm->cfg.add_segment_size;
390 bmp->options[APP_OPTIONS_RX_FIFO_SIZE] = vcm->cfg.rx_fifo_size;
391 bmp->options[APP_OPTIONS_TX_FIFO_SIZE] = vcm->cfg.tx_fifo_size;
392 bmp->options[APP_OPTIONS_PREALLOC_FIFO_PAIRS] =
393 vcm->cfg.preallocated_fifo_pairs;
394 bmp->options[APP_OPTIONS_EVT_QUEUE_SIZE] = vcm->cfg.event_queue_size;
Florin Corasd747c3c2019-10-20 19:55:56 -0700395 bmp->options[APP_OPTIONS_TLS_ENGINE] = tls_engine;
Florin Coras697faea2018-06-27 17:10:49 -0700396 if (nsid_len)
397 {
398 bmp->namespace_id_len = nsid_len;
Dave Barach178cf492018-11-13 16:34:13 -0500399 clib_memcpy_fast (bmp->namespace_id, vcm->cfg.namespace_id, nsid_len);
Florin Coras697faea2018-06-27 17:10:49 -0700400 bmp->options[APP_OPTIONS_NAMESPACE_SECRET] = vcm->cfg.namespace_secret;
401 }
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_detach (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_detach_t *bmp;
410 bmp = vl_msg_api_alloc (sizeof (*bmp));
411 memset (bmp, 0, sizeof (*bmp));
412
413 bmp->_vl_msg_id = ntohs (VL_API_APPLICATION_DETACH);
Florin Coras47c40e22018-11-26 17:01:36 -0800414 bmp->client_index = wrk->my_client_index;
Florin Coras697faea2018-06-27 17:10:49 -0700415 bmp->context = htonl (0xfeedface);
Florin Coras47c40e22018-11-26 17:01:36 -0800416 vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & bmp);
Florin Coras697faea2018-06-27 17:10:49 -0700417}
418
419void
Florin Coras134a9962018-08-28 11:32:04 -0700420vcl_send_app_worker_add_del (u8 is_add)
421{
422 vcl_worker_t *wrk = vcl_worker_get_current ();
423 vl_api_app_worker_add_del_t *mp;
Florin Coras134a9962018-08-28 11:32:04 -0700424
425 mp = vl_msg_api_alloc (sizeof (*mp));
426 memset (mp, 0, sizeof (*mp));
427
428 mp->_vl_msg_id = ntohs (VL_API_APP_WORKER_ADD_DEL);
Florin Coras47c40e22018-11-26 17:01:36 -0800429 mp->client_index = wrk->my_client_index;
Florin Corasc1f5a432018-11-20 11:31:26 -0800430 mp->app_index = clib_host_to_net_u32 (vcm->app_index);
Florin Coras01f3f892018-12-02 12:45:53 -0800431 mp->context = wrk->wrk_index;
Florin Coras134a9962018-08-28 11:32:04 -0700432 mp->is_add = is_add;
433 if (!is_add)
Florin Coras01f3f892018-12-02 12:45:53 -0800434 mp->wrk_index = clib_host_to_net_u32 (wrk->vpp_wrk_index);
435
436 vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & mp);
437}
438
439void
440vcl_send_child_worker_del (vcl_worker_t * child_wrk)
441{
442 vcl_worker_t *wrk = vcl_worker_get_current ();
443 vl_api_app_worker_add_del_t *mp;
444
445 mp = vl_msg_api_alloc (sizeof (*mp));
446 memset (mp, 0, sizeof (*mp));
447
448 mp->_vl_msg_id = ntohs (VL_API_APP_WORKER_ADD_DEL);
449 mp->client_index = wrk->my_client_index;
450 mp->app_index = clib_host_to_net_u32 (vcm->app_index);
451 mp->context = wrk->wrk_index;
452 mp->is_add = 0;
453 mp->wrk_index = clib_host_to_net_u32 (child_wrk->vpp_wrk_index);
Florin Coras134a9962018-08-28 11:32:04 -0700454
Florin Coras47c40e22018-11-26 17:01:36 -0800455 vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & mp);
Florin Coras134a9962018-08-28 11:32:04 -0700456}
457
458void
Ping Yu34a3a082018-11-30 19:16:17 -0500459vppcom_send_application_tls_cert_add (vcl_session_t * session, char *cert,
460 u32 cert_len)
461{
462 vcl_worker_t *wrk = vcl_worker_get_current ();
463 vl_api_application_tls_cert_add_t *cert_mp;
464
465 cert_mp = vl_msg_api_alloc (sizeof (*cert_mp) + cert_len);
466 clib_memset (cert_mp, 0, sizeof (*cert_mp));
467 cert_mp->_vl_msg_id = ntohs (VL_API_APPLICATION_TLS_CERT_ADD);
468 cert_mp->client_index = wrk->my_client_index;
469 cert_mp->context = session->session_index;
470 cert_mp->cert_len = clib_host_to_net_u16 (cert_len);
471 clib_memcpy_fast (cert_mp->cert, cert, cert_len);
472 vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & cert_mp);
Ping Yu34a3a082018-11-30 19:16:17 -0500473}
474
475void
476vppcom_send_application_tls_key_add (vcl_session_t * session, char *key,
477 u32 key_len)
478{
479 vcl_worker_t *wrk = vcl_worker_get_current ();
480 vl_api_application_tls_key_add_t *key_mp;
481
482 key_mp = vl_msg_api_alloc (sizeof (*key_mp) + key_len);
483 clib_memset (key_mp, 0, sizeof (*key_mp));
484 key_mp->_vl_msg_id = ntohs (VL_API_APPLICATION_TLS_KEY_ADD);
485 key_mp->client_index = wrk->my_client_index;
486 key_mp->context = session->session_index;
487 key_mp->key_len = clib_host_to_net_u16 (key_len);
488 clib_memcpy_fast (key_mp->key, key, key_len);
489 vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & key_mp);
Ping Yu34a3a082018-11-30 19:16:17 -0500490}
491
Florin Coras697faea2018-06-27 17:10:49 -0700492u32
493vcl_max_nsid_len (void)
494{
495 vl_api_application_attach_t *mp;
496 return (sizeof (mp->namespace_id) - 1);
497}
498
499void
500vppcom_init_error_string_table (void)
501{
502 vcm->error_string_by_error_number = hash_create (0, sizeof (uword));
503
504#define _(n, v, s) hash_set (vcm->error_string_by_error_number, -v, s);
505 foreach_vnet_api_error;
506#undef _
507
508 hash_set (vcm->error_string_by_error_number, 99, "Misc");
509}
510
511int
512vppcom_connect_to_vpp (char *app_name)
513{
Florin Coras47c40e22018-11-26 17:01:36 -0800514 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras697faea2018-06-27 17:10:49 -0700515 vppcom_cfg_t *vcl_cfg = &vcm->cfg;
Florin Corasd4c70922019-11-28 14:21:21 -0800516 api_main_t *am;
517
518 vlibapi_set_main (&wrk->bapi_api_ctx);
519 vlibapi_set_memory_client_main (&wrk->bapi_shm_ctx);
520 vppcom_api_hookup ();
Florin Coras697faea2018-06-27 17:10:49 -0700521
Florin Coras99368312018-08-02 10:45:44 -0700522 if (vcl_cfg->vpp_api_socket_name)
Florin Coras697faea2018-06-27 17:10:49 -0700523 {
Florin Corasd4c70922019-11-28 14:21:21 -0800524 if (vl_socket_client_connect2 (&wrk->bapi_sock_ctx,
525 (char *) vcl_cfg->vpp_api_socket_name,
526 app_name, 0 /* default rx/tx buffer */ ))
Florin Coras99368312018-08-02 10:45:44 -0700527 {
Florin Coras053a0e42018-11-13 15:52:38 -0800528 VERR ("app (%s) socket connect failed!", app_name);
Florin Coras99368312018-08-02 10:45:44 -0700529 return VPPCOM_ECONNREFUSED;
530 }
531
Florin Corasd4c70922019-11-28 14:21:21 -0800532 if (vl_socket_client_init_shm2 (&wrk->bapi_sock_ctx, 0,
533 1 /* want_pthread */ ))
Florin Coras99368312018-08-02 10:45:44 -0700534 {
Florin Coras053a0e42018-11-13 15:52:38 -0800535 VERR ("app (%s) init shm failed!", app_name);
Florin Coras99368312018-08-02 10:45:44 -0700536 return VPPCOM_ECONNREFUSED;
537 }
Florin Coras697faea2018-06-27 17:10:49 -0700538 }
539 else
540 {
Florin Coras99368312018-08-02 10:45:44 -0700541 if (!vcl_cfg->vpp_api_filename)
542 vcl_cfg->vpp_api_filename = format (0, "/vpe-api%c", 0);
Florin Coras697faea2018-06-27 17:10:49 -0700543
Florin Corasd4c70922019-11-28 14:21:21 -0800544 vl_set_memory_root_path ((char *) vcl_cfg->vpp_api_chroot);
545
Florin Coras053a0e42018-11-13 15:52:38 -0800546 VDBG (0, "app (%s) connecting to VPP api (%s)...",
Florin Coras99368312018-08-02 10:45:44 -0700547 app_name, vcl_cfg->vpp_api_filename);
548
549 if (vl_client_connect_to_vlib ((char *) vcl_cfg->vpp_api_filename,
550 app_name, vcm->cfg.vpp_api_q_length) < 0)
551 {
Florin Coras053a0e42018-11-13 15:52:38 -0800552 VERR ("app (%s) connect failed!", app_name);
Florin Coras99368312018-08-02 10:45:44 -0700553 return VPPCOM_ECONNREFUSED;
554 }
Florin Coras697faea2018-06-27 17:10:49 -0700555 }
556
Florin Corasd4c70922019-11-28 14:21:21 -0800557 am = vlibapi_get_main ();
Florin Coras47c40e22018-11-26 17:01:36 -0800558 wrk->vl_input_queue = am->shmem_hdr->vl_input_queue;
559 wrk->my_client_index = (u32) am->my_client_index;
560 wrk->wrk_state = STATE_APP_CONN_VPP;
Florin Coras99368312018-08-02 10:45:44 -0700561
Florin Coras053a0e42018-11-13 15:52:38 -0800562 VDBG (0, "app (%s) is connected to VPP!", app_name);
Florin Coras697faea2018-06-27 17:10:49 -0700563 vcl_evt (VCL_EVT_INIT, vcm);
Florin Coras99368312018-08-02 10:45:44 -0700564 return VPPCOM_OK;
Florin Coras697faea2018-06-27 17:10:49 -0700565}
566
Florin Coras470b2a62019-08-03 18:53:48 -0700567void
568vppcom_disconnect_from_vpp (void)
569{
570 vppcom_cfg_t *vcl_cfg = &vcm->cfg;
571
572 if (vcl_cfg->vpp_api_socket_name)
573 vl_socket_client_disconnect ();
574 else
575 vl_client_disconnect_from_vlib ();
576}
577
Florin Coras697faea2018-06-27 17:10:49 -0700578/*
579 * fd.io coding-style-patch-verification: ON
580 *
581 * Local Variables:
582 * eval: (c-set-style "gnu")
583 * End:
584 */