blob: 7d241624d01c3d78ae9b8e24ff90ca3889812516 [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 Coras00e01d32019-10-21 16:07:46 -070036static u8 *
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{
Florin Coras3b6c84c2021-01-05 20:45:44 -080055 vcl_worker_t *wrk = vcl_worker_get (0);
56
Florin Coras697faea2018-06-27 17:10:49 -070057 if (mp->retval)
58 {
59 clib_warning ("VCL<%d>: session_enable_disable failed: %U", getpid (),
60 format_api_error, ntohl (mp->retval));
61 }
62 else
Florin Coras3b6c84c2021-01-05 20:45:44 -080063 wrk->bapi_app_state = STATE_APP_ENABLED;
Florin Coras697faea2018-06-27 17:10:49 -070064}
65
Florin Corasd85de682018-11-29 17:02:29 -080066static void
Florin Coras458089b2019-08-21 16:20:44 -070067vl_api_app_attach_reply_t_handler (vl_api_app_attach_reply_t * mp)
Florin Coras697faea2018-06-27 17:10:49 -070068{
Florin Coras134a9962018-08-28 11:32:04 -070069 vcl_worker_t *wrk = vcl_worker_get (0);
Florin Corasd85de682018-11-29 17:02:29 -080070 u64 segment_handle;
Jakub Grajciarb4e5e502020-01-31 09:35:29 +010071 int *fds = 0, i, rv;
Florin Coras99368312018-08-02 10:45:44 -070072 u32 n_fds = 0;
Jakub Grajciarb4e5e502020-01-31 09:35:29 +010073 char *segment_name = 0;
Florin Coras697faea2018-06-27 17:10:49 -070074
Florin Coras697faea2018-06-27 17:10:49 -070075 if (mp->retval)
76 {
Florin Corasa5efca32019-03-20 08:33:10 -070077 VERR ("attach failed: %U", format_api_error, ntohl (mp->retval));
78 goto failed;
Florin Coras697faea2018-06-27 17:10:49 -070079 }
80
Florin Coras3b6c84c2021-01-05 20:45:44 -080081 vcl_set_worker_index (0);
82
Florin Corasd85de682018-11-29 17:02:29 -080083 segment_handle = clib_net_to_host_u64 (mp->segment_handle);
84 if (segment_handle == VCL_INVALID_SEGMENT_HANDLE)
85 {
Florin Corasa5efca32019-03-20 08:33:10 -070086 VERR ("invalid segment handle");
87 goto failed;
Florin Corasd85de682018-11-29 17:02:29 -080088 }
89
Florin Coras99368312018-08-02 10:45:44 -070090 if (mp->n_fds)
Florin Coras697faea2018-06-27 17:10:49 -070091 {
Florin Coras99368312018-08-02 10:45:44 -070092 vec_validate (fds, mp->n_fds);
Florin Corasd4c70922019-11-28 14:21:21 -080093 if (vl_socket_client_recv_fd_msg2 (&wrk->bapi_sock_ctx, fds, mp->n_fds,
94 5))
Florin Coras617574d2019-07-01 07:32:31 -070095 goto failed;
Florin Coras99368312018-08-02 10:45:44 -070096
97 if (mp->fd_flags & SESSION_FD_F_VPP_MQ_SEGMENT)
Florin Corasd85de682018-11-29 17:02:29 -080098 if (vcl_segment_attach (vcl_vpp_worker_segment_handle (0),
99 "vpp-mq-seg", SSVM_SEGMENT_MEMFD,
100 fds[n_fds++]))
Florin Corasa5efca32019-03-20 08:33:10 -0700101 goto failed;
Florin Coras99368312018-08-02 10:45:44 -0700102
Florin Corasb4624182020-12-11 13:58:12 -0800103 vcl_segment_attach_mq (vcl_vpp_worker_segment_handle (0),
104 mp->vpp_ctrl_mq, mp->vpp_ctrl_mq_thread,
105 &wrk->ctrl_mq);
106 vcm->ctrl_mq = wrk->ctrl_mq;
107
Florin Coras99368312018-08-02 10:45:44 -0700108 if (mp->fd_flags & SESSION_FD_F_MEMFD_SEGMENT)
Jakub Grajciarb4e5e502020-01-31 09:35:29 +0100109 {
110 segment_name = vl_api_from_api_to_new_c_string (&mp->segment_name);
111 rv =
112 vcl_segment_attach (segment_handle, segment_name,
113 SSVM_SEGMENT_MEMFD, fds[n_fds++]);
114 vec_free (segment_name);
115 if (rv != 0)
116 goto failed;
117 }
118
Florin Corasb4624182020-12-11 13:58:12 -0800119 vcl_segment_attach_mq (segment_handle, mp->app_mq, 0,
120 &wrk->app_event_queue);
Florin Coras99368312018-08-02 10:45:44 -0700121
122 if (mp->fd_flags & SESSION_FD_F_MQ_EVENTFD)
123 {
Florin Coras134a9962018-08-28 11:32:04 -0700124 svm_msg_q_set_consumer_eventfd (wrk->app_event_queue, fds[n_fds]);
125 vcl_mq_epoll_add_evfd (wrk, wrk->app_event_queue);
Florin Coras99368312018-08-02 10:45:44 -0700126 n_fds++;
127 }
128
129 vec_free (fds);
Florin Coras697faea2018-06-27 17:10:49 -0700130 }
Florin Coras99368312018-08-02 10:45:44 -0700131 else
Florin Coras697faea2018-06-27 17:10:49 -0700132 {
Jakub Grajciarb4e5e502020-01-31 09:35:29 +0100133 segment_name = vl_api_from_api_to_new_c_string (&mp->segment_name);
134 rv =
135 vcl_segment_attach (segment_handle, segment_name, SSVM_SEGMENT_SHM,
136 -1);
137 vec_free (segment_name);
138 if (rv != 0)
Florin Corasa5efca32019-03-20 08:33:10 -0700139 goto failed;
Florin Coras697faea2018-06-27 17:10:49 -0700140 }
141
Florin Corasc1f5a432018-11-20 11:31:26 -0800142 vcm->app_index = clib_net_to_host_u32 (mp->app_index);
Florin Coras3b6c84c2021-01-05 20:45:44 -0800143 wrk->bapi_app_state = STATE_APP_ATTACHED;
Florin Corasa5efca32019-03-20 08:33:10 -0700144 return;
145
146failed:
Florin Coras3b6c84c2021-01-05 20:45:44 -0800147 wrk->bapi_app_state = STATE_APP_FAILED;
Florin Coras617574d2019-07-01 07:32:31 -0700148 for (i = clib_max (n_fds - 1, 0); i < vec_len (fds); i++)
149 close (fds[i]);
150 vec_free (fds);
Florin Coras697faea2018-06-27 17:10:49 -0700151}
152
153static void
Florin Coras134a9962018-08-28 11:32:04 -0700154vl_api_app_worker_add_del_reply_t_handler (vl_api_app_worker_add_del_reply_t *
155 mp)
156{
Jakub Grajciarb4e5e502020-01-31 09:35:29 +0100157 int n_fds = 0, *fds = 0, i, rv;
Florin Corasd85de682018-11-29 17:02:29 -0800158 u64 segment_handle;
Florin Coras134a9962018-08-28 11:32:04 -0700159 vcl_worker_t *wrk;
Florin Corasab2f6db2018-08-31 14:31:41 -0700160 u32 wrk_index;
Jakub Grajciarb4e5e502020-01-31 09:35:29 +0100161 char *segment_name = 0;
Florin Coras134a9962018-08-28 11:32:04 -0700162
Florin Coras3348a4c2018-09-07 17:09:35 -0700163 if (!mp->is_add)
164 return;
165
Florin Corasdc2e2512018-12-03 17:47:26 -0800166 wrk_index = mp->context;
Florin Coras01f3f892018-12-02 12:45:53 -0800167 wrk = vcl_worker_get_if_valid (wrk_index);
168 if (!wrk)
169 return;
170
Florin Coras3b6c84c2021-01-05 20:45:44 -0800171 if (mp->retval)
172 {
173 clib_warning ("VCL<%d>: add/del worker failed: %U", getpid (),
174 format_api_error, ntohl (mp->retval));
175 goto failed;
176 }
177
178 vcl_set_worker_index (wrk_index);
Florin Corasdc2e2512018-12-03 17:47:26 -0800179 wrk->vpp_wrk_index = clib_net_to_host_u32 (mp->wrk_index);
Florin Coras22ba3302019-08-29 22:45:04 -0700180 wrk->ctrl_mq = vcm->ctrl_mq;
Florin Coras134a9962018-08-28 11:32:04 -0700181
Florin Corasd85de682018-11-29 17:02:29 -0800182 segment_handle = clib_net_to_host_u64 (mp->segment_handle);
183 if (segment_handle == VCL_INVALID_SEGMENT_HANDLE)
184 {
185 clib_warning ("invalid segment handle");
Florin Coras01f3f892018-12-02 12:45:53 -0800186 goto failed;
Florin Corasd85de682018-11-29 17:02:29 -0800187 }
188
Florin Coras134a9962018-08-28 11:32:04 -0700189 if (mp->n_fds)
190 {
191 vec_validate (fds, mp->n_fds);
Florin Corasd4c70922019-11-28 14:21:21 -0800192 if (vl_socket_client_recv_fd_msg2 (&wrk->bapi_sock_ctx, fds, mp->n_fds,
193 5))
Florin Coras617574d2019-07-01 07:32:31 -0700194 goto failed;
Florin Coras134a9962018-08-28 11:32:04 -0700195
196 if (mp->fd_flags & SESSION_FD_F_VPP_MQ_SEGMENT)
Florin Coras01f3f892018-12-02 12:45:53 -0800197 if (vcl_segment_attach (vcl_vpp_worker_segment_handle (wrk_index),
198 "vpp-worker-seg", SSVM_SEGMENT_MEMFD,
199 fds[n_fds++]))
Florin Coras134a9962018-08-28 11:32:04 -0700200 goto failed;
201
202 if (mp->fd_flags & SESSION_FD_F_MEMFD_SEGMENT)
Jakub Grajciarb4e5e502020-01-31 09:35:29 +0100203 {
204 segment_name = vl_api_from_api_to_new_c_string (&mp->segment_name);
205 rv =
206 vcl_segment_attach (segment_handle, segment_name,
207 SSVM_SEGMENT_MEMFD, fds[n_fds++]);
208 vec_free (segment_name);
209 if (rv != 0)
210 goto failed;
211 }
Florin Coras134a9962018-08-28 11:32:04 -0700212
Florin Corasb4624182020-12-11 13:58:12 -0800213 vcl_segment_attach_mq (segment_handle, mp->app_event_queue_address, 0,
214 &wrk->app_event_queue);
215
Florin Coras134a9962018-08-28 11:32:04 -0700216 if (mp->fd_flags & SESSION_FD_F_MQ_EVENTFD)
217 {
218 svm_msg_q_set_consumer_eventfd (wrk->app_event_queue, fds[n_fds]);
219 vcl_mq_epoll_add_evfd (wrk, wrk->app_event_queue);
220 n_fds++;
221 }
222
223 vec_free (fds);
224 }
225 else
226 {
Jakub Grajciarb4e5e502020-01-31 09:35:29 +0100227 segment_name = vl_api_from_api_to_new_c_string (&mp->segment_name);
228 rv =
229 vcl_segment_attach (segment_handle, segment_name, SSVM_SEGMENT_SHM,
230 -1);
231 vec_free (segment_name);
232 if (rv != 0)
Florin Coras134a9962018-08-28 11:32:04 -0700233 goto failed;
234 }
Florin Coras3b6c84c2021-01-05 20:45:44 -0800235 wrk->bapi_app_state = STATE_APP_READY;
Florin Coras053a0e42018-11-13 15:52:38 -0800236 VDBG (0, "worker %u vpp-worker %u added", wrk_index, wrk->vpp_wrk_index);
Florin Coras134a9962018-08-28 11:32:04 -0700237 return;
238
239failed:
Florin Coras3b6c84c2021-01-05 20:45:44 -0800240 wrk->bapi_app_state = STATE_APP_FAILED;
Florin Coras617574d2019-07-01 07:32:31 -0700241 for (i = clib_max (n_fds - 1, 0); i < vec_len (fds); i++)
242 close (fds[i]);
243 vec_free (fds);
Florin Coras134a9962018-08-28 11:32:04 -0700244}
245
246static void
Florin Corasa5a9efd2021-01-05 17:03:29 -0800247vl_api_app_add_cert_key_pair_reply_t_handler (
248 vl_api_app_add_cert_key_pair_reply_t *mp)
Ping Yu34a3a082018-11-30 19:16:17 -0500249{
Florin Coras3b6c84c2021-01-05 20:45:44 -0800250 vcl_worker_t *wrk = vcl_worker_get_current ();
251
Ping Yu34a3a082018-11-30 19:16:17 -0500252 if (mp->retval)
Florin Coras3b6c84c2021-01-05 20:45:44 -0800253 {
Florin Corasa5a9efd2021-01-05 17:03:29 -0800254 VDBG (0, "Adding cert and key failed: %U", format_api_error,
255 ntohl (mp->retval));
Florin Coras3b6c84c2021-01-05 20:45:44 -0800256 return;
257 }
Florin Corasa5a9efd2021-01-05 17:03:29 -0800258 wrk->bapi_return = clib_net_to_host_u32 (mp->index);
Florin Coras3b6c84c2021-01-05 20:45:44 -0800259 wrk->bapi_app_state = STATE_APP_READY;
Ping Yu34a3a082018-11-30 19:16:17 -0500260}
261
262static void
Florin Corasa5a9efd2021-01-05 17:03:29 -0800263vl_api_app_del_cert_key_pair_reply_t_handler (
264 vl_api_app_del_cert_key_pair_reply_t *mp)
Ping Yu34a3a082018-11-30 19:16:17 -0500265{
266 if (mp->retval)
Florin Coras3b6c84c2021-01-05 20:45:44 -0800267 {
Florin Corasa5a9efd2021-01-05 17:03:29 -0800268 VDBG (0, "Deleting cert and key failed: %U", format_api_error,
269 ntohl (mp->retval));
Florin Coras3b6c84c2021-01-05 20:45:44 -0800270 return;
271 }
Ping Yu34a3a082018-11-30 19:16:17 -0500272}
273
Florin Corasa5a9efd2021-01-05 17:03:29 -0800274#define foreach_sock_msg \
275 _ (SESSION_ENABLE_DISABLE_REPLY, session_enable_disable_reply) \
276 _ (APP_ATTACH_REPLY, app_attach_reply) \
277 _ (APP_ADD_CERT_KEY_PAIR_REPLY, app_add_cert_key_pair_reply) \
278 _ (APP_DEL_CERT_KEY_PAIR_REPLY, app_del_cert_key_pair_reply) \
279 _ (APP_WORKER_ADD_DEL_REPLY, app_worker_add_del_reply)
Florin Coras697faea2018-06-27 17:10:49 -0700280
Florin Corasb88de902020-09-08 16:47:57 -0700281static void
282vcl_bapi_hookup (void)
Florin Coras697faea2018-06-27 17:10:49 -0700283{
Florin Coras60116992018-08-27 09:52:18 -0700284#define _(N, n) \
285 vl_msg_api_set_handlers(VL_API_##N, #n, \
Florin Coras697faea2018-06-27 17:10:49 -0700286 vl_api_##n##_t_handler, \
287 vl_noop_handler, \
288 vl_api_##n##_t_endian, \
289 vl_api_##n##_t_print, \
290 sizeof(vl_api_##n##_t), 1);
291 foreach_sock_msg;
292#undef _
293}
294
295/*
296 * VPP-API message functions
297 */
Florin Corasb88de902020-09-08 16:47:57 -0700298static void
299vcl_bapi_send_session_enable_disable (u8 is_enable)
Florin Coras697faea2018-06-27 17:10:49 -0700300{
Florin Coras47c40e22018-11-26 17:01:36 -0800301 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras697faea2018-06-27 17:10:49 -0700302 vl_api_session_enable_disable_t *bmp;
303 bmp = vl_msg_api_alloc (sizeof (*bmp));
304 memset (bmp, 0, sizeof (*bmp));
305
306 bmp->_vl_msg_id = ntohs (VL_API_SESSION_ENABLE_DISABLE);
Florin Corascc7c88e2020-09-15 15:56:51 -0700307 bmp->client_index = wrk->api_client_handle;
Florin Coras697faea2018-06-27 17:10:49 -0700308 bmp->context = htonl (0xfeedface);
309 bmp->is_enable = is_enable;
Florin Coras47c40e22018-11-26 17:01:36 -0800310 vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & bmp);
Florin Coras697faea2018-06-27 17:10:49 -0700311}
312
313void
Florin Corasb88de902020-09-08 16:47:57 -0700314vcl_bapi_send_attach (void)
Florin Coras697faea2018-06-27 17:10:49 -0700315{
Florin Coras47c40e22018-11-26 17:01:36 -0800316 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Corasd747c3c2019-10-20 19:55:56 -0700317 u8 tls_engine = CRYPTO_ENGINE_OPENSSL;
Florin Coras458089b2019-08-21 16:20:44 -0700318 vl_api_app_attach_t *bmp;
Florin Coras697faea2018-06-27 17:10:49 -0700319 u8 nsid_len = vec_len (vcm->cfg.namespace_id);
320 u8 app_is_proxy = (vcm->cfg.app_proxy_transport_tcp ||
321 vcm->cfg.app_proxy_transport_udp);
322
Florin Corasd747c3c2019-10-20 19:55:56 -0700323 tls_engine = vcm->cfg.tls_engine ? vcm->cfg.tls_engine : tls_engine;
324
Florin Coras697faea2018-06-27 17:10:49 -0700325 bmp = vl_msg_api_alloc (sizeof (*bmp));
326 memset (bmp, 0, sizeof (*bmp));
327
Florin Coras458089b2019-08-21 16:20:44 -0700328 bmp->_vl_msg_id = ntohs (VL_API_APP_ATTACH);
Florin Corascc7c88e2020-09-15 15:56:51 -0700329 bmp->client_index = wrk->api_client_handle;
Florin Coras697faea2018-06-27 17:10:49 -0700330 bmp->context = htonl (0xfeedface);
331 bmp->options[APP_OPTIONS_FLAGS] =
332 APP_OPTIONS_FLAGS_ACCEPT_REDIRECT | APP_OPTIONS_FLAGS_ADD_SEGMENT |
333 (vcm->cfg.app_scope_local ? APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE : 0) |
334 (vcm->cfg.app_scope_global ? APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE : 0) |
Florin Coras54693d22018-07-17 10:46:29 -0700335 (app_is_proxy ? APP_OPTIONS_FLAGS_IS_PROXY : 0) |
Florin Coras99368312018-08-02 10:45:44 -0700336 (vcm->cfg.use_mq_eventfd ? APP_OPTIONS_FLAGS_EVT_MQ_USE_EVENTFD : 0);
Florin Coras697faea2018-06-27 17:10:49 -0700337 bmp->options[APP_OPTIONS_PROXY_TRANSPORT] =
338 (u64) ((vcm->cfg.app_proxy_transport_tcp ? 1 << TRANSPORT_PROTO_TCP : 0) |
339 (vcm->cfg.app_proxy_transport_udp ? 1 << TRANSPORT_PROTO_UDP : 0));
340 bmp->options[APP_OPTIONS_SEGMENT_SIZE] = vcm->cfg.segment_size;
341 bmp->options[APP_OPTIONS_ADD_SEGMENT_SIZE] = vcm->cfg.add_segment_size;
342 bmp->options[APP_OPTIONS_RX_FIFO_SIZE] = vcm->cfg.rx_fifo_size;
343 bmp->options[APP_OPTIONS_TX_FIFO_SIZE] = vcm->cfg.tx_fifo_size;
344 bmp->options[APP_OPTIONS_PREALLOC_FIFO_PAIRS] =
345 vcm->cfg.preallocated_fifo_pairs;
346 bmp->options[APP_OPTIONS_EVT_QUEUE_SIZE] = vcm->cfg.event_queue_size;
Florin Corasd747c3c2019-10-20 19:55:56 -0700347 bmp->options[APP_OPTIONS_TLS_ENGINE] = tls_engine;
Florin Coras697faea2018-06-27 17:10:49 -0700348 if (nsid_len)
349 {
Jakub Grajciarb4e5e502020-01-31 09:35:29 +0100350 vl_api_vec_to_api_string (vcm->cfg.namespace_id, &bmp->namespace_id);
Florin Coras697faea2018-06-27 17:10:49 -0700351 bmp->options[APP_OPTIONS_NAMESPACE_SECRET] = vcm->cfg.namespace_secret;
352 }
Florin Coras47c40e22018-11-26 17:01:36 -0800353 vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & bmp);
Florin Coras697faea2018-06-27 17:10:49 -0700354}
355
356void
Florin Corasb88de902020-09-08 16:47:57 -0700357vcl_bapi_send_detach (void)
Florin Coras697faea2018-06-27 17:10:49 -0700358{
Florin Coras47c40e22018-11-26 17:01:36 -0800359 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras697faea2018-06-27 17:10:49 -0700360 vl_api_application_detach_t *bmp;
361 bmp = vl_msg_api_alloc (sizeof (*bmp));
362 memset (bmp, 0, sizeof (*bmp));
363
364 bmp->_vl_msg_id = ntohs (VL_API_APPLICATION_DETACH);
Florin Corascc7c88e2020-09-15 15:56:51 -0700365 bmp->client_index = wrk->api_client_handle;
Florin Coras697faea2018-06-27 17:10:49 -0700366 bmp->context = htonl (0xfeedface);
Florin Coras47c40e22018-11-26 17:01:36 -0800367 vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & bmp);
Florin Coras697faea2018-06-27 17:10:49 -0700368}
369
Florin Corasb88de902020-09-08 16:47:57 -0700370static void
371vcl_bapi_send_app_worker_add_del (u8 is_add)
Florin Coras134a9962018-08-28 11:32:04 -0700372{
373 vcl_worker_t *wrk = vcl_worker_get_current ();
374 vl_api_app_worker_add_del_t *mp;
Florin Coras134a9962018-08-28 11:32:04 -0700375
376 mp = vl_msg_api_alloc (sizeof (*mp));
377 memset (mp, 0, sizeof (*mp));
378
379 mp->_vl_msg_id = ntohs (VL_API_APP_WORKER_ADD_DEL);
Florin Corascc7c88e2020-09-15 15:56:51 -0700380 mp->client_index = wrk->api_client_handle;
Florin Corasc1f5a432018-11-20 11:31:26 -0800381 mp->app_index = clib_host_to_net_u32 (vcm->app_index);
Florin Coras01f3f892018-12-02 12:45:53 -0800382 mp->context = wrk->wrk_index;
Florin Coras134a9962018-08-28 11:32:04 -0700383 mp->is_add = is_add;
384 if (!is_add)
Florin Coras01f3f892018-12-02 12:45:53 -0800385 mp->wrk_index = clib_host_to_net_u32 (wrk->vpp_wrk_index);
386
387 vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & mp);
388}
389
Florin Corasb88de902020-09-08 16:47:57 -0700390static void
391vcl_bapi_send_child_worker_del (vcl_worker_t * child_wrk)
Florin Coras01f3f892018-12-02 12:45:53 -0800392{
393 vcl_worker_t *wrk = vcl_worker_get_current ();
394 vl_api_app_worker_add_del_t *mp;
395
396 mp = vl_msg_api_alloc (sizeof (*mp));
397 memset (mp, 0, sizeof (*mp));
398
399 mp->_vl_msg_id = ntohs (VL_API_APP_WORKER_ADD_DEL);
Florin Corascc7c88e2020-09-15 15:56:51 -0700400 mp->client_index = wrk->api_client_handle;
Florin Coras01f3f892018-12-02 12:45:53 -0800401 mp->app_index = clib_host_to_net_u32 (vcm->app_index);
402 mp->context = wrk->wrk_index;
403 mp->is_add = 0;
404 mp->wrk_index = clib_host_to_net_u32 (child_wrk->vpp_wrk_index);
Florin Coras134a9962018-08-28 11:32:04 -0700405
Florin Coras47c40e22018-11-26 17:01:36 -0800406 vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & mp);
Florin Coras134a9962018-08-28 11:32:04 -0700407}
408
Florin Corasa5a9efd2021-01-05 17:03:29 -0800409static void
410vcl_bapi_send_app_add_cert_key_pair (vppcom_cert_key_pair_t *ckpair)
Ping Yu34a3a082018-11-30 19:16:17 -0500411{
412 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Corasa5a9efd2021-01-05 17:03:29 -0800413 u32 cert_len = test_srv_crt_rsa_len;
414 u32 key_len = test_srv_key_rsa_len;
415 vl_api_app_add_cert_key_pair_t *bmp;
Ping Yu34a3a082018-11-30 19:16:17 -0500416
Florin Corasa5a9efd2021-01-05 17:03:29 -0800417 bmp = vl_msg_api_alloc (sizeof (*bmp) + cert_len + key_len);
418 clib_memset (bmp, 0, sizeof (*bmp) + cert_len + key_len);
419
420 bmp->_vl_msg_id = ntohs (VL_API_APP_ADD_CERT_KEY_PAIR);
421 bmp->client_index = wrk->api_client_handle;
422 bmp->context = wrk->wrk_index;
423 bmp->cert_len = clib_host_to_net_u16 (cert_len);
424 bmp->certkey_len = clib_host_to_net_u16 (key_len + cert_len);
425 clib_memcpy_fast (bmp->certkey, test_srv_crt_rsa, cert_len);
426 clib_memcpy_fast (bmp->certkey + cert_len, test_srv_key_rsa, key_len);
427
428 vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) &bmp);
Ping Yu34a3a082018-11-30 19:16:17 -0500429}
430
Florin Corasa5a9efd2021-01-05 17:03:29 -0800431static void
432vcl_bapi_send_app_del_cert_key_pair (u32 ckpair_index)
Ping Yu34a3a082018-11-30 19:16:17 -0500433{
434 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Corasa5a9efd2021-01-05 17:03:29 -0800435 vl_api_app_del_cert_key_pair_t *bmp;
436 bmp = vl_msg_api_alloc (sizeof (*bmp));
437 clib_memset (bmp, 0, sizeof (*bmp));
Ping Yu34a3a082018-11-30 19:16:17 -0500438
Florin Corasa5a9efd2021-01-05 17:03:29 -0800439 bmp->_vl_msg_id = ntohs (VL_API_APP_DEL_CERT_KEY_PAIR);
440 bmp->client_index = wrk->api_client_handle;
441 bmp->context = wrk->wrk_index;
442 bmp->index = clib_host_to_net_u32 (ckpair_index);
443 vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) &bmp);
Ping Yu34a3a082018-11-30 19:16:17 -0500444}
445
Florin Coras697faea2018-06-27 17:10:49 -0700446u32
Florin Corasb88de902020-09-08 16:47:57 -0700447vcl_bapi_max_nsid_len (void)
Florin Coras697faea2018-06-27 17:10:49 -0700448{
Florin Coras888d9f02020-04-02 23:00:13 +0000449 vl_api_app_attach_t *mp;
Florin Coras697faea2018-06-27 17:10:49 -0700450 return (sizeof (mp->namespace_id) - 1);
451}
452
Florin Corasb88de902020-09-08 16:47:57 -0700453static void
454vcl_bapi_init_error_string_table (void)
Florin Coras697faea2018-06-27 17:10:49 -0700455{
456 vcm->error_string_by_error_number = hash_create (0, sizeof (uword));
457
458#define _(n, v, s) hash_set (vcm->error_string_by_error_number, -v, s);
459 foreach_vnet_api_error;
460#undef _
461
462 hash_set (vcm->error_string_by_error_number, 99, "Misc");
463}
464
Florin Corasb88de902020-09-08 16:47:57 -0700465static void
466vcl_bapi_cleanup (void)
467{
468 socket_client_main_t *scm = &socket_client_main;
469 api_main_t *am = vlibapi_get_main ();
470
471 am->my_client_index = ~0;
472 am->my_registration = 0;
473 am->vl_input_queue = 0;
474 am->msg_index_by_name_and_crc = 0;
475 scm->socket_fd = 0;
476
477 vl_client_api_unmap ();
478}
479
480static int
481vcl_bapi_connect_to_vpp (void)
Florin Coras697faea2018-06-27 17:10:49 -0700482{
Florin Coras47c40e22018-11-26 17:01:36 -0800483 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras697faea2018-06-27 17:10:49 -0700484 vppcom_cfg_t *vcl_cfg = &vcm->cfg;
Florin Corasb88de902020-09-08 16:47:57 -0700485 int rv = VPPCOM_OK;
Florin Corasd4c70922019-11-28 14:21:21 -0800486 api_main_t *am;
Florin Corasb88de902020-09-08 16:47:57 -0700487 u8 *wrk_name;
488
jiangxiaomingb438c762020-10-13 14:23:29 +0800489 wrk_name = format (0, "%v-wrk-%u%c", vcm->app_name, wrk->wrk_index, 0);
Florin Corasb88de902020-09-08 16:47:57 -0700490
491 /* Make sure api is cleaned up in case this is a connect from a
492 * forked worker */
493 vcl_bapi_cleanup ();
Florin Corasd4c70922019-11-28 14:21:21 -0800494
495 vlibapi_set_main (&wrk->bapi_api_ctx);
Florin Corasb88de902020-09-08 16:47:57 -0700496 vcl_bapi_hookup ();
Florin Coras697faea2018-06-27 17:10:49 -0700497
Florin Coras165f3ae2020-11-08 18:04:33 -0800498 if (!vcl_cfg->vpp_bapi_socket_name)
Florin Coras697faea2018-06-27 17:10:49 -0700499 {
Florin Coras165f3ae2020-11-08 18:04:33 -0800500 rv = VPPCOM_EINVAL;
501 goto error;
Florin Coras697faea2018-06-27 17:10:49 -0700502 }
Florin Coras165f3ae2020-11-08 18:04:33 -0800503
504 if (vl_socket_client_connect2 (&wrk->bapi_sock_ctx,
505 (char *) vcl_cfg->vpp_bapi_socket_name,
506 (char *) wrk_name,
507 0 /* default rx/tx buffer */ ))
Florin Coras697faea2018-06-27 17:10:49 -0700508 {
Florin Coras165f3ae2020-11-08 18:04:33 -0800509 VERR ("app (%s) socket connect failed!", wrk_name);
510 rv = VPPCOM_ECONNREFUSED;
511 goto error;
512 }
Florin Coras697faea2018-06-27 17:10:49 -0700513
Florin Coras165f3ae2020-11-08 18:04:33 -0800514 if (vl_socket_client_init_shm2 (&wrk->bapi_sock_ctx, 0,
515 1 /* want_pthread */ ))
516 {
517 VERR ("app (%s) init shm failed!", wrk_name);
518 rv = VPPCOM_ECONNREFUSED;
519 goto error;
Florin Coras697faea2018-06-27 17:10:49 -0700520 }
521
Florin Corasd4c70922019-11-28 14:21:21 -0800522 am = vlibapi_get_main ();
Florin Coras47c40e22018-11-26 17:01:36 -0800523 wrk->vl_input_queue = am->shmem_hdr->vl_input_queue;
Florin Corascc7c88e2020-09-15 15:56:51 -0700524 wrk->api_client_handle = (u32) am->my_client_index;
Florin Coras99368312018-08-02 10:45:44 -0700525
Florin Corasb88de902020-09-08 16:47:57 -0700526 VDBG (0, "app (%s) is connected to VPP!", wrk_name);
Florin Coras697faea2018-06-27 17:10:49 -0700527 vcl_evt (VCL_EVT_INIT, vcm);
Florin Corasb88de902020-09-08 16:47:57 -0700528
529error:
530 vec_free (wrk_name);
531 return rv;
Florin Coras697faea2018-06-27 17:10:49 -0700532}
533
Florin Coras470b2a62019-08-03 18:53:48 -0700534void
Florin Corasb88de902020-09-08 16:47:57 -0700535vcl_bapi_disconnect_from_vpp (void)
Florin Coras470b2a62019-08-03 18:53:48 -0700536{
Florin Coras64cf4592019-12-12 12:01:24 -0800537 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras470b2a62019-08-03 18:53:48 -0700538 vppcom_cfg_t *vcl_cfg = &vcm->cfg;
539
Florin Corasb88de902020-09-08 16:47:57 -0700540 if (vcl_cfg->vpp_bapi_socket_name)
Florin Coras64cf4592019-12-12 12:01:24 -0800541 vl_socket_client_disconnect2 (&wrk->bapi_sock_ctx);
Florin Coras470b2a62019-08-03 18:53:48 -0700542 else
543 vl_client_disconnect_from_vlib ();
544}
545
Florin Corasb88de902020-09-08 16:47:57 -0700546static const char *
547vcl_bapi_app_state_str (vcl_bapi_app_state_t state)
548{
549 char *st;
550
551 switch (state)
552 {
553 case STATE_APP_START:
554 st = "STATE_APP_START";
555 break;
556
557 case STATE_APP_CONN_VPP:
558 st = "STATE_APP_CONN_VPP";
559 break;
560
561 case STATE_APP_ENABLED:
562 st = "STATE_APP_ENABLED";
563 break;
564
565 case STATE_APP_ATTACHED:
566 st = "STATE_APP_ATTACHED";
567 break;
568
569 default:
570 st = "UNKNOWN_APP_STATE";
571 break;
572 }
573
574 return st;
575}
576
577static int
Florin Coras3b6c84c2021-01-05 20:45:44 -0800578vcl_bapi_wait_for_wrk_state_change (vcl_bapi_app_state_t app_state)
Florin Corasb88de902020-09-08 16:47:57 -0700579{
580 vcl_worker_t *wrk = vcl_worker_get_current ();
581 f64 timeout = clib_time_now (&wrk->clib_time) + vcm->cfg.app_timeout;
582
583 while (clib_time_now (&wrk->clib_time) < timeout)
584 {
Florin Coras3b6c84c2021-01-05 20:45:44 -0800585 if (wrk->bapi_app_state == app_state)
Florin Corasb88de902020-09-08 16:47:57 -0700586 return VPPCOM_OK;
Florin Coras3b6c84c2021-01-05 20:45:44 -0800587 if (wrk->bapi_app_state == STATE_APP_FAILED)
Florin Corasb88de902020-09-08 16:47:57 -0700588 return VPPCOM_ECONNABORTED;
589 }
590 VDBG (0, "timeout waiting for state %s (%d)",
591 vcl_bapi_app_state_str (app_state), app_state);
592 vcl_evt (VCL_EVT_SESSION_TIMEOUT, vcm, bapi_app_state);
593
594 return VPPCOM_ETIMEDOUT;
595}
596
597static int
598vcl_bapi_session_enable (void)
599{
Florin Coras3b6c84c2021-01-05 20:45:44 -0800600 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Corasb88de902020-09-08 16:47:57 -0700601 int rv;
602
Florin Coras3b6c84c2021-01-05 20:45:44 -0800603 if (wrk->bapi_app_state != STATE_APP_ENABLED)
Florin Corasb88de902020-09-08 16:47:57 -0700604 {
605 vcl_bapi_send_session_enable_disable (1 /* is_enabled == TRUE */ );
Florin Coras3b6c84c2021-01-05 20:45:44 -0800606 rv = vcl_bapi_wait_for_wrk_state_change (STATE_APP_ENABLED);
Florin Corasb88de902020-09-08 16:47:57 -0700607 if (PREDICT_FALSE (rv))
608 {
609 VDBG (0, "application session enable timed out! returning %d (%s)",
610 rv, vppcom_retval_str (rv));
611 return rv;
612 }
613 }
614 return VPPCOM_OK;
615}
616
617static int
618vcl_bapi_init (void)
619{
Florin Coras3b6c84c2021-01-05 20:45:44 -0800620 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Corasb88de902020-09-08 16:47:57 -0700621 int rv;
622
Florin Coras3b6c84c2021-01-05 20:45:44 -0800623 wrk->bapi_app_state = STATE_APP_START;
Florin Corasb88de902020-09-08 16:47:57 -0700624 vcl_bapi_init_error_string_table ();
625 rv = vcl_bapi_connect_to_vpp ();
626 if (rv)
627 {
628 VERR ("couldn't connect to VPP!");
629 return rv;
630 }
631 VDBG (0, "sending session enable");
632 rv = vcl_bapi_session_enable ();
633 if (rv)
634 {
635 VERR ("vppcom_app_session_enable() failed!");
636 return rv;
637 }
638
639 return 0;
640}
641
642int
643vcl_bapi_attach (void)
644{
645 int rv;
646
647 /* API hookup and connect to VPP */
648 if ((rv = vcl_bapi_init ()))
649 return rv;
650
651 vcl_bapi_send_attach ();
Florin Coras3b6c84c2021-01-05 20:45:44 -0800652 rv = vcl_bapi_wait_for_wrk_state_change (STATE_APP_ATTACHED);
Florin Corasb88de902020-09-08 16:47:57 -0700653 if (PREDICT_FALSE (rv))
654 {
655 VDBG (0, "application attach timed out! returning %d (%s)", rv,
656 vppcom_retval_str (rv));
657 return rv;
658 }
659
660 return 0;
661}
662
663int
664vcl_bapi_app_worker_add (void)
665{
Florin Coras3b6c84c2021-01-05 20:45:44 -0800666 vcl_worker_t *wrk = vcl_worker_get_current ();
667
Florin Corasb88de902020-09-08 16:47:57 -0700668 if (vcl_bapi_connect_to_vpp ())
669 return -1;
670
Florin Coras3b6c84c2021-01-05 20:45:44 -0800671 wrk->bapi_app_state = STATE_APP_ADDING_WORKER;
Florin Corasb88de902020-09-08 16:47:57 -0700672 vcl_bapi_send_app_worker_add_del (1 /* is_add */ );
Florin Coras3b6c84c2021-01-05 20:45:44 -0800673 if (vcl_bapi_wait_for_wrk_state_change (STATE_APP_READY))
Florin Corasb88de902020-09-08 16:47:57 -0700674 return -1;
675 return 0;
676}
677
678void
679vcl_bapi_app_worker_del (vcl_worker_t * wrk)
680{
681 /* Notify vpp that the worker is going away */
682 if (wrk->wrk_index == vcl_get_worker_index ())
683 vcl_bapi_send_app_worker_add_del (0 /* is_add */ );
684 else
685 vcl_bapi_send_child_worker_del (wrk);
686
687 /* Disconnect the binary api */
688 if (vec_len (vcm->workers) == 1)
689 vcl_bapi_disconnect_from_vpp ();
690 else
691 vl_client_send_disconnect (1 /* vpp should cleanup */ );
692}
693
694int
Florin Coras935ce752020-09-08 22:43:47 -0700695vcl_bapi_recv_fds (vcl_worker_t * wrk, int *fds, int n_fds)
696{
697 clib_error_t *err;
698
699 if ((err = vl_socket_client_recv_fd_msg2 (&wrk->bapi_sock_ctx, fds, n_fds,
700 5)))
701 {
702 clib_error_report (err);
703 return -1;
704 }
705
706 return 0;
707}
708
709int
Florin Corasa5a9efd2021-01-05 17:03:29 -0800710vcl_bapi_add_cert_key_pair (vppcom_cert_key_pair_t *ckpair)
Florin Corasb88de902020-09-08 16:47:57 -0700711{
Florin Corasb88de902020-09-08 16:47:57 -0700712 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Corasb88de902020-09-08 16:47:57 -0700713
Florin Corasa5a9efd2021-01-05 17:03:29 -0800714 if (ckpair->key_len == 0 || ckpair->key_len == ~0)
715 return VPPCOM_EINVAL;
Florin Corasb88de902020-09-08 16:47:57 -0700716
Florin Corasa5a9efd2021-01-05 17:03:29 -0800717 vcl_bapi_send_app_add_cert_key_pair (ckpair);
Florin Coras3b6c84c2021-01-05 20:45:44 -0800718 wrk->bapi_app_state = STATE_APP_ADDING_TLS_DATA;
719 vcl_bapi_wait_for_wrk_state_change (STATE_APP_READY);
Florin Corasa5a9efd2021-01-05 17:03:29 -0800720 if (wrk->bapi_app_state == STATE_APP_READY)
721 return wrk->bapi_return;
722 return VPPCOM_EFAULT;
Florin Corasb88de902020-09-08 16:47:57 -0700723}
724
725int
Florin Corasa5a9efd2021-01-05 17:03:29 -0800726vcl_bapi_del_cert_key_pair (u32 ckpair_index)
Florin Corasb88de902020-09-08 16:47:57 -0700727{
Florin Corasa5a9efd2021-01-05 17:03:29 -0800728 /* Don't wait for reply */
729 vcl_bapi_send_app_del_cert_key_pair (ckpair_index);
730 return 0;
Florin Corasb88de902020-09-08 16:47:57 -0700731}
732
733int
734vcl_bapi_worker_set (void)
735{
736 vcl_worker_t *wrk = vcl_worker_get_current ();
737 int i;
738
739 /* Find the first worker with the same pid */
740 for (i = 0; i < vec_len (vcm->workers); i++)
741 {
742 if (i == wrk->wrk_index)
743 continue;
744 if (vcm->workers[i].current_pid == wrk->current_pid)
745 {
746 wrk->vl_input_queue = vcm->workers[i].vl_input_queue;
Florin Corascc7c88e2020-09-15 15:56:51 -0700747 wrk->api_client_handle = vcm->workers[i].api_client_handle;
Florin Corasb88de902020-09-08 16:47:57 -0700748 return 0;
749 }
750 }
751 return -1;
752}
753
Florin Coras697faea2018-06-27 17:10:49 -0700754/*
755 * fd.io coding-style-patch-verification: ON
756 *
757 * Local Variables:
758 * eval: (c-set-style "gnu")
759 * End:
760 */