blob: efa5cc170efa71f1dc00b302996bfdc27a655712 [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 Corasd85de682018-11-29 17:02:29 -080064static u64
65vcl_vpp_worker_segment_handle (u32 wrk_index)
66{
67 return (VCL_INVALID_SEGMENT_HANDLE - wrk_index - 1);
68}
69
70static void
Florin Coras458089b2019-08-21 16:20:44 -070071vl_api_app_attach_reply_t_handler (vl_api_app_attach_reply_t * mp)
Florin Coras697faea2018-06-27 17:10:49 -070072{
Florin Coras134a9962018-08-28 11:32:04 -070073 vcl_worker_t *wrk = vcl_worker_get (0);
Florin Coras458089b2019-08-21 16:20:44 -070074 svm_msg_q_t *ctrl_mq;
Florin Corasd85de682018-11-29 17:02:29 -080075 u64 segment_handle;
Florin Coras617574d2019-07-01 07:32:31 -070076 int *fds = 0, i;
Florin Coras99368312018-08-02 10:45:44 -070077 u32 n_fds = 0;
Florin Coras697faea2018-06-27 17:10:49 -070078
Florin Coras697faea2018-06-27 17:10:49 -070079 if (mp->retval)
80 {
Florin Corasa5efca32019-03-20 08:33:10 -070081 VERR ("attach failed: %U", format_api_error, ntohl (mp->retval));
82 goto failed;
Florin Coras697faea2018-06-27 17:10:49 -070083 }
84
Florin Coras458089b2019-08-21 16:20:44 -070085 wrk->app_event_queue = uword_to_pointer (mp->app_mq, svm_msg_q_t *);
86 ctrl_mq = uword_to_pointer (mp->vpp_ctrl_mq, svm_msg_q_t *);
87 vec_validate (wrk->vpp_event_queues, mp->vpp_ctrl_mq_thread);
88 wrk->vpp_event_queues[mp->vpp_ctrl_mq_thread] = ctrl_mq;
Florin Coras22ba3302019-08-29 22:45:04 -070089 vcm->ctrl_mq = wrk->ctrl_mq = ctrl_mq;
Florin Corasd85de682018-11-29 17:02:29 -080090 segment_handle = clib_net_to_host_u64 (mp->segment_handle);
91 if (segment_handle == VCL_INVALID_SEGMENT_HANDLE)
92 {
Florin Corasa5efca32019-03-20 08:33:10 -070093 VERR ("invalid segment handle");
94 goto failed;
Florin Corasd85de682018-11-29 17:02:29 -080095 }
96
Florin Coras99368312018-08-02 10:45:44 -070097 if (mp->n_fds)
Florin Coras697faea2018-06-27 17:10:49 -070098 {
Florin Coras99368312018-08-02 10:45:44 -070099 vec_validate (fds, mp->n_fds);
Florin Corasd4c70922019-11-28 14:21:21 -0800100 if (vl_socket_client_recv_fd_msg2 (&wrk->bapi_sock_ctx, fds, mp->n_fds,
101 5))
Florin Coras617574d2019-07-01 07:32:31 -0700102 goto failed;
Florin Coras99368312018-08-02 10:45:44 -0700103
104 if (mp->fd_flags & SESSION_FD_F_VPP_MQ_SEGMENT)
Florin Corasd85de682018-11-29 17:02:29 -0800105 if (vcl_segment_attach (vcl_vpp_worker_segment_handle (0),
106 "vpp-mq-seg", SSVM_SEGMENT_MEMFD,
107 fds[n_fds++]))
Florin Corasa5efca32019-03-20 08:33:10 -0700108 goto failed;
Florin Coras99368312018-08-02 10:45:44 -0700109
110 if (mp->fd_flags & SESSION_FD_F_MEMFD_SEGMENT)
Florin Corasd85de682018-11-29 17:02:29 -0800111 if (vcl_segment_attach (segment_handle, (char *) mp->segment_name,
112 SSVM_SEGMENT_MEMFD, fds[n_fds++]))
Florin Corasa5efca32019-03-20 08:33:10 -0700113 goto failed;
Florin Coras99368312018-08-02 10:45:44 -0700114
115 if (mp->fd_flags & SESSION_FD_F_MQ_EVENTFD)
116 {
Florin Coras134a9962018-08-28 11:32:04 -0700117 svm_msg_q_set_consumer_eventfd (wrk->app_event_queue, fds[n_fds]);
118 vcl_mq_epoll_add_evfd (wrk, wrk->app_event_queue);
Florin Coras99368312018-08-02 10:45:44 -0700119 n_fds++;
120 }
121
122 vec_free (fds);
Florin Coras697faea2018-06-27 17:10:49 -0700123 }
Florin Coras99368312018-08-02 10:45:44 -0700124 else
Florin Coras697faea2018-06-27 17:10:49 -0700125 {
Florin Corasd85de682018-11-29 17:02:29 -0800126 if (vcl_segment_attach (segment_handle, (char *) mp->segment_name,
127 SSVM_SEGMENT_SHM, -1))
Florin Corasa5efca32019-03-20 08:33:10 -0700128 goto failed;
Florin Coras697faea2018-06-27 17:10:49 -0700129 }
130
Florin Corasc1f5a432018-11-20 11:31:26 -0800131 vcm->app_index = clib_net_to_host_u32 (mp->app_index);
Florin Coras697faea2018-06-27 17:10:49 -0700132 vcm->app_state = STATE_APP_ATTACHED;
Florin Corasa5efca32019-03-20 08:33:10 -0700133 return;
134
135failed:
136 vcm->app_state = STATE_APP_FAILED;
Florin Coras617574d2019-07-01 07:32:31 -0700137 for (i = clib_max (n_fds - 1, 0); i < vec_len (fds); i++)
138 close (fds[i]);
139 vec_free (fds);
Florin Coras697faea2018-06-27 17:10:49 -0700140}
141
142static void
Florin Coras134a9962018-08-28 11:32:04 -0700143vl_api_app_worker_add_del_reply_t_handler (vl_api_app_worker_add_del_reply_t *
144 mp)
145{
Florin Coras617574d2019-07-01 07:32:31 -0700146 int n_fds = 0, *fds = 0, i;
Florin Corasd85de682018-11-29 17:02:29 -0800147 u64 segment_handle;
Florin Coras134a9962018-08-28 11:32:04 -0700148 vcl_worker_t *wrk;
Florin Corasab2f6db2018-08-31 14:31:41 -0700149 u32 wrk_index;
Florin Coras134a9962018-08-28 11:32:04 -0700150
151 if (mp->retval)
152 {
153 clib_warning ("VCL<%d>: add/del worker failed: %U", getpid (),
154 format_api_error, ntohl (mp->retval));
155 goto failed;
156 }
Florin Coras053a0e42018-11-13 15:52:38 -0800157
Florin Coras3348a4c2018-09-07 17:09:35 -0700158 if (!mp->is_add)
159 return;
160
Florin Corasdc2e2512018-12-03 17:47:26 -0800161 wrk_index = mp->context;
Florin Coras01f3f892018-12-02 12:45:53 -0800162 wrk = vcl_worker_get_if_valid (wrk_index);
163 if (!wrk)
164 return;
165
Florin Corasdc2e2512018-12-03 17:47:26 -0800166 wrk->vpp_wrk_index = clib_net_to_host_u32 (mp->wrk_index);
Florin Coras134a9962018-08-28 11:32:04 -0700167 wrk->app_event_queue = uword_to_pointer (mp->app_event_queue_address,
168 svm_msg_q_t *);
Florin Coras22ba3302019-08-29 22:45:04 -0700169 wrk->ctrl_mq = vcm->ctrl_mq;
Florin Coras134a9962018-08-28 11:32:04 -0700170
Florin Corasd85de682018-11-29 17:02:29 -0800171 segment_handle = clib_net_to_host_u64 (mp->segment_handle);
172 if (segment_handle == VCL_INVALID_SEGMENT_HANDLE)
173 {
174 clib_warning ("invalid segment handle");
Florin Coras01f3f892018-12-02 12:45:53 -0800175 goto failed;
Florin Corasd85de682018-11-29 17:02:29 -0800176 }
177
Florin Coras134a9962018-08-28 11:32:04 -0700178 if (mp->n_fds)
179 {
180 vec_validate (fds, mp->n_fds);
Florin Corasd4c70922019-11-28 14:21:21 -0800181 if (vl_socket_client_recv_fd_msg2 (&wrk->bapi_sock_ctx, fds, mp->n_fds,
182 5))
Florin Coras617574d2019-07-01 07:32:31 -0700183 goto failed;
Florin Coras134a9962018-08-28 11:32:04 -0700184
185 if (mp->fd_flags & SESSION_FD_F_VPP_MQ_SEGMENT)
Florin Coras01f3f892018-12-02 12:45:53 -0800186 if (vcl_segment_attach (vcl_vpp_worker_segment_handle (wrk_index),
187 "vpp-worker-seg", SSVM_SEGMENT_MEMFD,
188 fds[n_fds++]))
Florin Coras134a9962018-08-28 11:32:04 -0700189 goto failed;
190
191 if (mp->fd_flags & SESSION_FD_F_MEMFD_SEGMENT)
Florin Corasd85de682018-11-29 17:02:29 -0800192 if (vcl_segment_attach (segment_handle, (char *) mp->segment_name,
193 SSVM_SEGMENT_MEMFD, fds[n_fds++]))
Florin Coras134a9962018-08-28 11:32:04 -0700194 goto failed;
195
196 if (mp->fd_flags & SESSION_FD_F_MQ_EVENTFD)
197 {
198 svm_msg_q_set_consumer_eventfd (wrk->app_event_queue, fds[n_fds]);
199 vcl_mq_epoll_add_evfd (wrk, wrk->app_event_queue);
200 n_fds++;
201 }
202
203 vec_free (fds);
204 }
205 else
206 {
Florin Corasd85de682018-11-29 17:02:29 -0800207 if (vcl_segment_attach (segment_handle, (char *) mp->segment_name,
208 SSVM_SEGMENT_SHM, -1))
Florin Coras134a9962018-08-28 11:32:04 -0700209 goto failed;
210 }
211 vcm->app_state = STATE_APP_READY;
Florin Coras053a0e42018-11-13 15:52:38 -0800212 VDBG (0, "worker %u vpp-worker %u added", wrk_index, wrk->vpp_wrk_index);
Florin Coras134a9962018-08-28 11:32:04 -0700213 return;
214
215failed:
216 vcm->app_state = STATE_APP_FAILED;
Florin Coras617574d2019-07-01 07:32:31 -0700217 for (i = clib_max (n_fds - 1, 0); i < vec_len (fds); i++)
218 close (fds[i]);
219 vec_free (fds);
Florin Coras134a9962018-08-28 11:32:04 -0700220}
221
222static void
Ping Yu34a3a082018-11-30 19:16:17 -0500223 vl_api_application_tls_cert_add_reply_t_handler
224 (vl_api_application_tls_cert_add_reply_t * mp)
225{
226 if (mp->retval)
Florin Coras458089b2019-08-21 16:20:44 -0700227 VDBG (0, "add cert failed: %U", format_api_error, ntohl (mp->retval));
228 vcm->app_state = STATE_APP_READY;
Ping Yu34a3a082018-11-30 19:16:17 -0500229}
230
231static void
232 vl_api_application_tls_key_add_reply_t_handler
233 (vl_api_application_tls_key_add_reply_t * mp)
234{
235 if (mp->retval)
Florin Coras458089b2019-08-21 16:20:44 -0700236 VDBG (0, "add key failed: %U", format_api_error, ntohl (mp->retval));
237 vcm->app_state = STATE_APP_READY;
Ping Yu34a3a082018-11-30 19:16:17 -0500238}
239
Florin Coras99368312018-08-02 10:45:44 -0700240#define foreach_sock_msg \
241_(SESSION_ENABLE_DISABLE_REPLY, session_enable_disable_reply) \
Florin Coras458089b2019-08-21 16:20:44 -0700242_(APP_ATTACH_REPLY, app_attach_reply) \
Ping Yu34a3a082018-11-30 19:16:17 -0500243_(APPLICATION_TLS_CERT_ADD_REPLY, application_tls_cert_add_reply) \
244_(APPLICATION_TLS_KEY_ADD_REPLY, application_tls_key_add_reply) \
Florin Coras134a9962018-08-28 11:32:04 -0700245_(APP_WORKER_ADD_DEL_REPLY, app_worker_add_del_reply) \
Florin Coras697faea2018-06-27 17:10:49 -0700246
247void
248vppcom_api_hookup (void)
249{
Florin Coras60116992018-08-27 09:52:18 -0700250#define _(N, n) \
251 vl_msg_api_set_handlers(VL_API_##N, #n, \
Florin Coras697faea2018-06-27 17:10:49 -0700252 vl_api_##n##_t_handler, \
253 vl_noop_handler, \
254 vl_api_##n##_t_endian, \
255 vl_api_##n##_t_print, \
256 sizeof(vl_api_##n##_t), 1);
257 foreach_sock_msg;
258#undef _
259}
260
261/*
262 * VPP-API message functions
263 */
264void
265vppcom_send_session_enable_disable (u8 is_enable)
266{
Florin Coras47c40e22018-11-26 17:01:36 -0800267 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras697faea2018-06-27 17:10:49 -0700268 vl_api_session_enable_disable_t *bmp;
269 bmp = vl_msg_api_alloc (sizeof (*bmp));
270 memset (bmp, 0, sizeof (*bmp));
271
272 bmp->_vl_msg_id = ntohs (VL_API_SESSION_ENABLE_DISABLE);
Florin Coras47c40e22018-11-26 17:01:36 -0800273 bmp->client_index = wrk->my_client_index;
Florin Coras697faea2018-06-27 17:10:49 -0700274 bmp->context = htonl (0xfeedface);
275 bmp->is_enable = is_enable;
Florin Coras47c40e22018-11-26 17:01:36 -0800276 vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & bmp);
Florin Coras697faea2018-06-27 17:10:49 -0700277}
278
279void
280vppcom_app_send_attach (void)
281{
Florin Coras47c40e22018-11-26 17:01:36 -0800282 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Corasd747c3c2019-10-20 19:55:56 -0700283 u8 tls_engine = CRYPTO_ENGINE_OPENSSL;
Florin Coras458089b2019-08-21 16:20:44 -0700284 vl_api_app_attach_t *bmp;
Florin Coras697faea2018-06-27 17:10:49 -0700285 u8 nsid_len = vec_len (vcm->cfg.namespace_id);
286 u8 app_is_proxy = (vcm->cfg.app_proxy_transport_tcp ||
287 vcm->cfg.app_proxy_transport_udp);
288
Florin Corasd747c3c2019-10-20 19:55:56 -0700289 tls_engine = vcm->cfg.tls_engine ? vcm->cfg.tls_engine : tls_engine;
290
Florin Coras697faea2018-06-27 17:10:49 -0700291 bmp = vl_msg_api_alloc (sizeof (*bmp));
292 memset (bmp, 0, sizeof (*bmp));
293
Florin Coras458089b2019-08-21 16:20:44 -0700294 bmp->_vl_msg_id = ntohs (VL_API_APP_ATTACH);
Florin Coras47c40e22018-11-26 17:01:36 -0800295 bmp->client_index = wrk->my_client_index;
Florin Coras697faea2018-06-27 17:10:49 -0700296 bmp->context = htonl (0xfeedface);
297 bmp->options[APP_OPTIONS_FLAGS] =
298 APP_OPTIONS_FLAGS_ACCEPT_REDIRECT | APP_OPTIONS_FLAGS_ADD_SEGMENT |
299 (vcm->cfg.app_scope_local ? APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE : 0) |
300 (vcm->cfg.app_scope_global ? APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE : 0) |
Florin Coras54693d22018-07-17 10:46:29 -0700301 (app_is_proxy ? APP_OPTIONS_FLAGS_IS_PROXY : 0) |
Florin Coras99368312018-08-02 10:45:44 -0700302 (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;
Florin Corasd747c3c2019-10-20 19:55:56 -0700313 bmp->options[APP_OPTIONS_TLS_ENGINE] = tls_engine;
Florin Coras697faea2018-06-27 17:10:49 -0700314 if (nsid_len)
315 {
316 bmp->namespace_id_len = nsid_len;
Dave Barach178cf492018-11-13 16:34:13 -0500317 clib_memcpy_fast (bmp->namespace_id, vcm->cfg.namespace_id, nsid_len);
Florin Coras697faea2018-06-27 17:10:49 -0700318 bmp->options[APP_OPTIONS_NAMESPACE_SECRET] = vcm->cfg.namespace_secret;
319 }
Florin Coras47c40e22018-11-26 17:01:36 -0800320 vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & bmp);
Florin Coras697faea2018-06-27 17:10:49 -0700321}
322
323void
324vppcom_app_send_detach (void)
325{
Florin Coras47c40e22018-11-26 17:01:36 -0800326 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras697faea2018-06-27 17:10:49 -0700327 vl_api_application_detach_t *bmp;
328 bmp = vl_msg_api_alloc (sizeof (*bmp));
329 memset (bmp, 0, sizeof (*bmp));
330
331 bmp->_vl_msg_id = ntohs (VL_API_APPLICATION_DETACH);
Florin Coras47c40e22018-11-26 17:01:36 -0800332 bmp->client_index = wrk->my_client_index;
Florin Coras697faea2018-06-27 17:10:49 -0700333 bmp->context = htonl (0xfeedface);
Florin Coras47c40e22018-11-26 17:01:36 -0800334 vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & bmp);
Florin Coras697faea2018-06-27 17:10:49 -0700335}
336
337void
Florin Coras134a9962018-08-28 11:32:04 -0700338vcl_send_app_worker_add_del (u8 is_add)
339{
340 vcl_worker_t *wrk = vcl_worker_get_current ();
341 vl_api_app_worker_add_del_t *mp;
Florin Coras134a9962018-08-28 11:32:04 -0700342
343 mp = vl_msg_api_alloc (sizeof (*mp));
344 memset (mp, 0, sizeof (*mp));
345
346 mp->_vl_msg_id = ntohs (VL_API_APP_WORKER_ADD_DEL);
Florin Coras47c40e22018-11-26 17:01:36 -0800347 mp->client_index = wrk->my_client_index;
Florin Corasc1f5a432018-11-20 11:31:26 -0800348 mp->app_index = clib_host_to_net_u32 (vcm->app_index);
Florin Coras01f3f892018-12-02 12:45:53 -0800349 mp->context = wrk->wrk_index;
Florin Coras134a9962018-08-28 11:32:04 -0700350 mp->is_add = is_add;
351 if (!is_add)
Florin Coras01f3f892018-12-02 12:45:53 -0800352 mp->wrk_index = clib_host_to_net_u32 (wrk->vpp_wrk_index);
353
354 vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & mp);
355}
356
357void
358vcl_send_child_worker_del (vcl_worker_t * child_wrk)
359{
360 vcl_worker_t *wrk = vcl_worker_get_current ();
361 vl_api_app_worker_add_del_t *mp;
362
363 mp = vl_msg_api_alloc (sizeof (*mp));
364 memset (mp, 0, sizeof (*mp));
365
366 mp->_vl_msg_id = ntohs (VL_API_APP_WORKER_ADD_DEL);
367 mp->client_index = wrk->my_client_index;
368 mp->app_index = clib_host_to_net_u32 (vcm->app_index);
369 mp->context = wrk->wrk_index;
370 mp->is_add = 0;
371 mp->wrk_index = clib_host_to_net_u32 (child_wrk->vpp_wrk_index);
Florin Coras134a9962018-08-28 11:32:04 -0700372
Florin Coras47c40e22018-11-26 17:01:36 -0800373 vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & mp);
Florin Coras134a9962018-08-28 11:32:04 -0700374}
375
376void
Ping Yu34a3a082018-11-30 19:16:17 -0500377vppcom_send_application_tls_cert_add (vcl_session_t * session, char *cert,
378 u32 cert_len)
379{
380 vcl_worker_t *wrk = vcl_worker_get_current ();
381 vl_api_application_tls_cert_add_t *cert_mp;
382
383 cert_mp = vl_msg_api_alloc (sizeof (*cert_mp) + cert_len);
384 clib_memset (cert_mp, 0, sizeof (*cert_mp));
385 cert_mp->_vl_msg_id = ntohs (VL_API_APPLICATION_TLS_CERT_ADD);
386 cert_mp->client_index = wrk->my_client_index;
387 cert_mp->context = session->session_index;
388 cert_mp->cert_len = clib_host_to_net_u16 (cert_len);
389 clib_memcpy_fast (cert_mp->cert, cert, cert_len);
390 vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & cert_mp);
Ping Yu34a3a082018-11-30 19:16:17 -0500391}
392
393void
394vppcom_send_application_tls_key_add (vcl_session_t * session, char *key,
395 u32 key_len)
396{
397 vcl_worker_t *wrk = vcl_worker_get_current ();
398 vl_api_application_tls_key_add_t *key_mp;
399
400 key_mp = vl_msg_api_alloc (sizeof (*key_mp) + key_len);
401 clib_memset (key_mp, 0, sizeof (*key_mp));
402 key_mp->_vl_msg_id = ntohs (VL_API_APPLICATION_TLS_KEY_ADD);
403 key_mp->client_index = wrk->my_client_index;
404 key_mp->context = session->session_index;
405 key_mp->key_len = clib_host_to_net_u16 (key_len);
406 clib_memcpy_fast (key_mp->key, key, key_len);
407 vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & key_mp);
Ping Yu34a3a082018-11-30 19:16:17 -0500408}
409
Florin Coras697faea2018-06-27 17:10:49 -0700410u32
411vcl_max_nsid_len (void)
412{
413 vl_api_application_attach_t *mp;
414 return (sizeof (mp->namespace_id) - 1);
415}
416
417void
418vppcom_init_error_string_table (void)
419{
420 vcm->error_string_by_error_number = hash_create (0, sizeof (uword));
421
422#define _(n, v, s) hash_set (vcm->error_string_by_error_number, -v, s);
423 foreach_vnet_api_error;
424#undef _
425
426 hash_set (vcm->error_string_by_error_number, 99, "Misc");
427}
428
429int
430vppcom_connect_to_vpp (char *app_name)
431{
Florin Coras47c40e22018-11-26 17:01:36 -0800432 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras697faea2018-06-27 17:10:49 -0700433 vppcom_cfg_t *vcl_cfg = &vcm->cfg;
Florin Corasd4c70922019-11-28 14:21:21 -0800434 api_main_t *am;
435
436 vlibapi_set_main (&wrk->bapi_api_ctx);
437 vlibapi_set_memory_client_main (&wrk->bapi_shm_ctx);
438 vppcom_api_hookup ();
Florin Coras697faea2018-06-27 17:10:49 -0700439
Florin Coras99368312018-08-02 10:45:44 -0700440 if (vcl_cfg->vpp_api_socket_name)
Florin Coras697faea2018-06-27 17:10:49 -0700441 {
Florin Corasd4c70922019-11-28 14:21:21 -0800442 if (vl_socket_client_connect2 (&wrk->bapi_sock_ctx,
443 (char *) vcl_cfg->vpp_api_socket_name,
444 app_name, 0 /* default rx/tx buffer */ ))
Florin Coras99368312018-08-02 10:45:44 -0700445 {
Florin Coras053a0e42018-11-13 15:52:38 -0800446 VERR ("app (%s) socket connect failed!", app_name);
Florin Coras99368312018-08-02 10:45:44 -0700447 return VPPCOM_ECONNREFUSED;
448 }
449
Florin Corasd4c70922019-11-28 14:21:21 -0800450 if (vl_socket_client_init_shm2 (&wrk->bapi_sock_ctx, 0,
451 1 /* want_pthread */ ))
Florin Coras99368312018-08-02 10:45:44 -0700452 {
Florin Coras053a0e42018-11-13 15:52:38 -0800453 VERR ("app (%s) init shm failed!", app_name);
Florin Coras99368312018-08-02 10:45:44 -0700454 return VPPCOM_ECONNREFUSED;
455 }
Florin Coras697faea2018-06-27 17:10:49 -0700456 }
457 else
458 {
Florin Coras99368312018-08-02 10:45:44 -0700459 if (!vcl_cfg->vpp_api_filename)
460 vcl_cfg->vpp_api_filename = format (0, "/vpe-api%c", 0);
Florin Coras697faea2018-06-27 17:10:49 -0700461
Florin Corasd4c70922019-11-28 14:21:21 -0800462 vl_set_memory_root_path ((char *) vcl_cfg->vpp_api_chroot);
463
Florin Coras053a0e42018-11-13 15:52:38 -0800464 VDBG (0, "app (%s) connecting to VPP api (%s)...",
Florin Coras99368312018-08-02 10:45:44 -0700465 app_name, vcl_cfg->vpp_api_filename);
466
467 if (vl_client_connect_to_vlib ((char *) vcl_cfg->vpp_api_filename,
468 app_name, vcm->cfg.vpp_api_q_length) < 0)
469 {
Florin Coras053a0e42018-11-13 15:52:38 -0800470 VERR ("app (%s) connect failed!", app_name);
Florin Coras99368312018-08-02 10:45:44 -0700471 return VPPCOM_ECONNREFUSED;
472 }
Florin Coras697faea2018-06-27 17:10:49 -0700473 }
474
Florin Corasd4c70922019-11-28 14:21:21 -0800475 am = vlibapi_get_main ();
Florin Coras47c40e22018-11-26 17:01:36 -0800476 wrk->vl_input_queue = am->shmem_hdr->vl_input_queue;
477 wrk->my_client_index = (u32) am->my_client_index;
478 wrk->wrk_state = STATE_APP_CONN_VPP;
Florin Coras99368312018-08-02 10:45:44 -0700479
Florin Coras053a0e42018-11-13 15:52:38 -0800480 VDBG (0, "app (%s) is connected to VPP!", app_name);
Florin Coras697faea2018-06-27 17:10:49 -0700481 vcl_evt (VCL_EVT_INIT, vcm);
Florin Coras99368312018-08-02 10:45:44 -0700482 return VPPCOM_OK;
Florin Coras697faea2018-06-27 17:10:49 -0700483}
484
Florin Coras470b2a62019-08-03 18:53:48 -0700485void
486vppcom_disconnect_from_vpp (void)
487{
Florin Coras64cf4592019-12-12 12:01:24 -0800488 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras470b2a62019-08-03 18:53:48 -0700489 vppcom_cfg_t *vcl_cfg = &vcm->cfg;
490
491 if (vcl_cfg->vpp_api_socket_name)
Florin Coras64cf4592019-12-12 12:01:24 -0800492 vl_socket_client_disconnect2 (&wrk->bapi_sock_ctx);
Florin Coras470b2a62019-08-03 18:53:48 -0700493 else
494 vl_client_disconnect_from_vlib ();
495}
496
Florin Coras697faea2018-06-27 17:10:49 -0700497/*
498 * fd.io coding-style-patch-verification: ON
499 *
500 * Local Variables:
501 * eval: (c-set-style "gnu")
502 * End:
503 */