blob: afe8824812f11f857e890f21106bcba37ec05ab6 [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>
Florin Coras697faea2018-06-27 17:10:49 -070018
Filip Tehlar0046e972021-06-26 22:12:08 +000019#include <vnet/format_fns.h>
20#include <vnet/session/session.api_enum.h>
21#include <vnet/session/session.api_types.h>
Florin Coras697faea2018-06-27 17:10:49 -070022
Filip Tehlar0046e972021-06-26 22:12:08 +000023#define REPLY_MSG_ID_BASE msg_id_base
Florin Coras697faea2018-06-27 17:10:49 -070024
Filip Tehlar0046e972021-06-26 22:12:08 +000025static u16 msg_id_base;
Florin Coras697faea2018-06-27 17:10:49 -070026
Florin Coras00e01d32019-10-21 16:07:46 -070027static u8 *
Florin Coras697faea2018-06-27 17:10:49 -070028format_api_error (u8 * s, va_list * args)
29{
30 i32 error = va_arg (*args, u32);
31 uword *p;
32
33 p = hash_get (vcm->error_string_by_error_number, -error);
34
35 if (p)
36 s = format (s, "%s (%d)", p[0], error);
37 else
38 s = format (s, "%d", error);
39 return s;
40}
41
42static void
43 vl_api_session_enable_disable_reply_t_handler
44 (vl_api_session_enable_disable_reply_t * mp)
45{
Florin Coras3b6c84c2021-01-05 20:45:44 -080046 vcl_worker_t *wrk = vcl_worker_get (0);
47
Florin Coras697faea2018-06-27 17:10:49 -070048 if (mp->retval)
49 {
50 clib_warning ("VCL<%d>: session_enable_disable failed: %U", getpid (),
51 format_api_error, ntohl (mp->retval));
52 }
53 else
Florin Coras3b6c84c2021-01-05 20:45:44 -080054 wrk->bapi_app_state = STATE_APP_ENABLED;
Florin Coras697faea2018-06-27 17:10:49 -070055}
56
Florin Corasd85de682018-11-29 17:02:29 -080057static void
Florin Coras458089b2019-08-21 16:20:44 -070058vl_api_app_attach_reply_t_handler (vl_api_app_attach_reply_t * mp)
Florin Coras697faea2018-06-27 17:10:49 -070059{
Florin Coras134a9962018-08-28 11:32:04 -070060 vcl_worker_t *wrk = vcl_worker_get (0);
Florin Corasd85de682018-11-29 17:02:29 -080061 u64 segment_handle;
Jakub Grajciarb4e5e502020-01-31 09:35:29 +010062 int *fds = 0, i, rv;
Florin Coras99368312018-08-02 10:45:44 -070063 u32 n_fds = 0;
Jakub Grajciarb4e5e502020-01-31 09:35:29 +010064 char *segment_name = 0;
Florin Coras697faea2018-06-27 17:10:49 -070065
Florin Coras697faea2018-06-27 17:10:49 -070066 if (mp->retval)
67 {
Florin Corasa5efca32019-03-20 08:33:10 -070068 VERR ("attach failed: %U", format_api_error, ntohl (mp->retval));
69 goto failed;
Florin Coras697faea2018-06-27 17:10:49 -070070 }
71
Florin Coras3b6c84c2021-01-05 20:45:44 -080072 vcl_set_worker_index (0);
73
Florin Corasd85de682018-11-29 17:02:29 -080074 segment_handle = clib_net_to_host_u64 (mp->segment_handle);
75 if (segment_handle == VCL_INVALID_SEGMENT_HANDLE)
76 {
Florin Corasa5efca32019-03-20 08:33:10 -070077 VERR ("invalid segment handle");
78 goto failed;
Florin Corasd85de682018-11-29 17:02:29 -080079 }
80
Florin Coras99368312018-08-02 10:45:44 -070081 if (mp->n_fds)
Florin Coras697faea2018-06-27 17:10:49 -070082 {
Florin Coras99368312018-08-02 10:45:44 -070083 vec_validate (fds, mp->n_fds);
Florin Corasd4c70922019-11-28 14:21:21 -080084 if (vl_socket_client_recv_fd_msg2 (&wrk->bapi_sock_ctx, fds, mp->n_fds,
85 5))
Florin Coras617574d2019-07-01 07:32:31 -070086 goto failed;
Florin Coras99368312018-08-02 10:45:44 -070087
88 if (mp->fd_flags & SESSION_FD_F_VPP_MQ_SEGMENT)
Florin Corasd85de682018-11-29 17:02:29 -080089 if (vcl_segment_attach (vcl_vpp_worker_segment_handle (0),
90 "vpp-mq-seg", SSVM_SEGMENT_MEMFD,
91 fds[n_fds++]))
Florin Corasa5efca32019-03-20 08:33:10 -070092 goto failed;
Florin Coras99368312018-08-02 10:45:44 -070093
94 if (mp->fd_flags & SESSION_FD_F_MEMFD_SEGMENT)
Jakub Grajciarb4e5e502020-01-31 09:35:29 +010095 {
96 segment_name = vl_api_from_api_to_new_c_string (&mp->segment_name);
97 rv =
98 vcl_segment_attach (segment_handle, segment_name,
99 SSVM_SEGMENT_MEMFD, fds[n_fds++]);
100 vec_free (segment_name);
101 if (rv != 0)
102 goto failed;
103 }
104
Florin Corasb4624182020-12-11 13:58:12 -0800105 vcl_segment_attach_mq (segment_handle, mp->app_mq, 0,
106 &wrk->app_event_queue);
Florin Coras99368312018-08-02 10:45:44 -0700107
108 if (mp->fd_flags & SESSION_FD_F_MQ_EVENTFD)
109 {
Florin Coras86f12322021-01-22 15:05:14 -0800110 svm_msg_q_set_eventfd (wrk->app_event_queue, fds[n_fds]);
Florin Coras134a9962018-08-28 11:32:04 -0700111 vcl_mq_epoll_add_evfd (wrk, wrk->app_event_queue);
Florin Coras99368312018-08-02 10:45:44 -0700112 n_fds++;
113 }
114
Florin Coras80b74252021-01-27 18:08:25 -0800115 vcl_segment_discover_mqs (vcl_vpp_worker_segment_handle (0), fds + n_fds,
116 mp->n_fds - n_fds);
117 vcl_segment_attach_mq (vcl_vpp_worker_segment_handle (0),
118 mp->vpp_ctrl_mq, mp->vpp_ctrl_mq_thread,
119 &wrk->ctrl_mq);
120 vcm->ctrl_mq = wrk->ctrl_mq;
121
Florin Coras99368312018-08-02 10:45:44 -0700122 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 {
Jakub Grajciarb4e5e502020-01-31 09:35:29 +0100126 segment_name = vl_api_from_api_to_new_c_string (&mp->segment_name);
127 rv =
128 vcl_segment_attach (segment_handle, segment_name, SSVM_SEGMENT_SHM,
129 -1);
130 vec_free (segment_name);
131 if (rv != 0)
Florin Corasa5efca32019-03-20 08:33:10 -0700132 goto failed;
Florin Coras697faea2018-06-27 17:10:49 -0700133 }
134
Florin Corasc1f5a432018-11-20 11:31:26 -0800135 vcm->app_index = clib_net_to_host_u32 (mp->app_index);
Florin Coras3b6c84c2021-01-05 20:45:44 -0800136 wrk->bapi_app_state = STATE_APP_ATTACHED;
Florin Corasa5efca32019-03-20 08:33:10 -0700137 return;
138
139failed:
Florin Coras3b6c84c2021-01-05 20:45:44 -0800140 wrk->bapi_app_state = STATE_APP_FAILED;
Florin Coras617574d2019-07-01 07:32:31 -0700141 for (i = clib_max (n_fds - 1, 0); i < vec_len (fds); i++)
142 close (fds[i]);
143 vec_free (fds);
Florin Coras697faea2018-06-27 17:10:49 -0700144}
145
146static void
Florin Coras134a9962018-08-28 11:32:04 -0700147vl_api_app_worker_add_del_reply_t_handler (vl_api_app_worker_add_del_reply_t *
148 mp)
149{
Jakub Grajciarb4e5e502020-01-31 09:35:29 +0100150 int n_fds = 0, *fds = 0, i, rv;
Florin Corasd85de682018-11-29 17:02:29 -0800151 u64 segment_handle;
Florin Coras134a9962018-08-28 11:32:04 -0700152 vcl_worker_t *wrk;
Florin Corasab2f6db2018-08-31 14:31:41 -0700153 u32 wrk_index;
Jakub Grajciarb4e5e502020-01-31 09:35:29 +0100154 char *segment_name = 0;
Florin Coras134a9962018-08-28 11:32:04 -0700155
Florin Coras3348a4c2018-09-07 17:09:35 -0700156 if (!mp->is_add)
157 return;
158
Florin Corasdc2e2512018-12-03 17:47:26 -0800159 wrk_index = mp->context;
Florin Coras01f3f892018-12-02 12:45:53 -0800160 wrk = vcl_worker_get_if_valid (wrk_index);
161 if (!wrk)
162 return;
163
Florin Coras3b6c84c2021-01-05 20:45:44 -0800164 if (mp->retval)
165 {
166 clib_warning ("VCL<%d>: add/del worker failed: %U", getpid (),
167 format_api_error, ntohl (mp->retval));
168 goto failed;
169 }
170
171 vcl_set_worker_index (wrk_index);
Florin Corasdc2e2512018-12-03 17:47:26 -0800172 wrk->vpp_wrk_index = clib_net_to_host_u32 (mp->wrk_index);
Florin Coras22ba3302019-08-29 22:45:04 -0700173 wrk->ctrl_mq = vcm->ctrl_mq;
Florin Coras134a9962018-08-28 11:32:04 -0700174
Florin Corasd85de682018-11-29 17:02:29 -0800175 segment_handle = clib_net_to_host_u64 (mp->segment_handle);
176 if (segment_handle == VCL_INVALID_SEGMENT_HANDLE)
177 {
178 clib_warning ("invalid segment handle");
Florin Coras01f3f892018-12-02 12:45:53 -0800179 goto failed;
Florin Corasd85de682018-11-29 17:02:29 -0800180 }
181
Florin Coras134a9962018-08-28 11:32:04 -0700182 if (mp->n_fds)
183 {
184 vec_validate (fds, mp->n_fds);
Florin Corasd4c70922019-11-28 14:21:21 -0800185 if (vl_socket_client_recv_fd_msg2 (&wrk->bapi_sock_ctx, fds, mp->n_fds,
186 5))
Florin Coras617574d2019-07-01 07:32:31 -0700187 goto failed;
Florin Coras134a9962018-08-28 11:32:04 -0700188
189 if (mp->fd_flags & SESSION_FD_F_VPP_MQ_SEGMENT)
Florin Coras01f3f892018-12-02 12:45:53 -0800190 if (vcl_segment_attach (vcl_vpp_worker_segment_handle (wrk_index),
191 "vpp-worker-seg", SSVM_SEGMENT_MEMFD,
192 fds[n_fds++]))
Florin Coras134a9962018-08-28 11:32:04 -0700193 goto failed;
194
195 if (mp->fd_flags & SESSION_FD_F_MEMFD_SEGMENT)
Jakub Grajciarb4e5e502020-01-31 09:35:29 +0100196 {
197 segment_name = vl_api_from_api_to_new_c_string (&mp->segment_name);
198 rv =
199 vcl_segment_attach (segment_handle, segment_name,
200 SSVM_SEGMENT_MEMFD, fds[n_fds++]);
201 vec_free (segment_name);
202 if (rv != 0)
203 goto failed;
204 }
Florin Coras134a9962018-08-28 11:32:04 -0700205
Florin Corasb4624182020-12-11 13:58:12 -0800206 vcl_segment_attach_mq (segment_handle, mp->app_event_queue_address, 0,
207 &wrk->app_event_queue);
208
Florin Coras134a9962018-08-28 11:32:04 -0700209 if (mp->fd_flags & SESSION_FD_F_MQ_EVENTFD)
210 {
Florin Coras86f12322021-01-22 15:05:14 -0800211 svm_msg_q_set_eventfd (wrk->app_event_queue, fds[n_fds]);
Florin Coras134a9962018-08-28 11:32:04 -0700212 vcl_mq_epoll_add_evfd (wrk, wrk->app_event_queue);
213 n_fds++;
214 }
215
216 vec_free (fds);
217 }
218 else
219 {
Jakub Grajciarb4e5e502020-01-31 09:35:29 +0100220 segment_name = vl_api_from_api_to_new_c_string (&mp->segment_name);
221 rv =
222 vcl_segment_attach (segment_handle, segment_name, SSVM_SEGMENT_SHM,
223 -1);
224 vec_free (segment_name);
225 if (rv != 0)
Florin Coras134a9962018-08-28 11:32:04 -0700226 goto failed;
227 }
Florin Coras3b6c84c2021-01-05 20:45:44 -0800228 wrk->bapi_app_state = STATE_APP_READY;
Florin Coras053a0e42018-11-13 15:52:38 -0800229 VDBG (0, "worker %u vpp-worker %u added", wrk_index, wrk->vpp_wrk_index);
Florin Coras134a9962018-08-28 11:32:04 -0700230 return;
231
232failed:
Florin Coras3b6c84c2021-01-05 20:45:44 -0800233 wrk->bapi_app_state = STATE_APP_FAILED;
Florin Coras617574d2019-07-01 07:32:31 -0700234 for (i = clib_max (n_fds - 1, 0); i < vec_len (fds); i++)
235 close (fds[i]);
236 vec_free (fds);
Florin Coras134a9962018-08-28 11:32:04 -0700237}
238
239static void
Florin Corasa5a9efd2021-01-05 17:03:29 -0800240vl_api_app_add_cert_key_pair_reply_t_handler (
241 vl_api_app_add_cert_key_pair_reply_t *mp)
Ping Yu34a3a082018-11-30 19:16:17 -0500242{
Florin Coras3b6c84c2021-01-05 20:45:44 -0800243 vcl_worker_t *wrk = vcl_worker_get_current ();
244
Ping Yu34a3a082018-11-30 19:16:17 -0500245 if (mp->retval)
Florin Coras3b6c84c2021-01-05 20:45:44 -0800246 {
Florin Corasa5a9efd2021-01-05 17:03:29 -0800247 VDBG (0, "Adding cert and key failed: %U", format_api_error,
248 ntohl (mp->retval));
Florin Coras3b6c84c2021-01-05 20:45:44 -0800249 return;
250 }
Florin Corasa5a9efd2021-01-05 17:03:29 -0800251 wrk->bapi_return = clib_net_to_host_u32 (mp->index);
Florin Coras3b6c84c2021-01-05 20:45:44 -0800252 wrk->bapi_app_state = STATE_APP_READY;
Ping Yu34a3a082018-11-30 19:16:17 -0500253}
254
255static void
Florin Corasa5a9efd2021-01-05 17:03:29 -0800256vl_api_app_del_cert_key_pair_reply_t_handler (
257 vl_api_app_del_cert_key_pair_reply_t *mp)
Ping Yu34a3a082018-11-30 19:16:17 -0500258{
259 if (mp->retval)
Florin Coras3b6c84c2021-01-05 20:45:44 -0800260 {
Florin Corasa5a9efd2021-01-05 17:03:29 -0800261 VDBG (0, "Deleting cert and key failed: %U", format_api_error,
262 ntohl (mp->retval));
Florin Coras3b6c84c2021-01-05 20:45:44 -0800263 return;
264 }
Ping Yu34a3a082018-11-30 19:16:17 -0500265}
266
Florin Corasa5a9efd2021-01-05 17:03:29 -0800267#define foreach_sock_msg \
268 _ (SESSION_ENABLE_DISABLE_REPLY, session_enable_disable_reply) \
269 _ (APP_ATTACH_REPLY, app_attach_reply) \
270 _ (APP_ADD_CERT_KEY_PAIR_REPLY, app_add_cert_key_pair_reply) \
271 _ (APP_DEL_CERT_KEY_PAIR_REPLY, app_del_cert_key_pair_reply) \
272 _ (APP_WORKER_ADD_DEL_REPLY, app_worker_add_del_reply)
Florin Coras697faea2018-06-27 17:10:49 -0700273
Filip Tehlar0046e972021-06-26 22:12:08 +0000274#define vl_endianfun /* define message structures */
275#include <vnet/session/session.api.h>
276#undef vl_endianfun
277
Klement Sekera9b7e8ac2021-11-22 21:26:20 +0100278#define vl_calcsizefun
279#include <vnet/session/session.api.h>
280#undef vl_calcsizefun
281
Filip Tehlar0046e972021-06-26 22:12:08 +0000282/* instantiate all the print functions we know about */
283#define vl_printfun
284#include <vnet/session/session.api.h>
285#undef vl_printfun
286
287#define vl_api_version(n, v) static u32 api_version = v;
288#include <vnet/session/session.api.h>
289#undef vl_api_version
290
Florin Corasb88de902020-09-08 16:47:57 -0700291static void
292vcl_bapi_hookup (void)
Florin Coras697faea2018-06-27 17:10:49 -0700293{
Filip Tehlar0046e972021-06-26 22:12:08 +0000294 u8 *msg_base_lookup_name = format (0, "session_%08x%c", api_version, 0);
295
296 REPLY_MSG_ID_BASE =
297 vl_client_get_first_plugin_msg_id ((char *) msg_base_lookup_name);
298
299 vec_free (msg_base_lookup_name);
300
301 if (REPLY_MSG_ID_BASE == (u16) ~0)
302 return;
303
304#define _(N, n) \
Damjan Mariona2eb5072022-05-20 20:06:01 +0200305 vl_msg_api_config (&(vl_msg_api_msg_config_t){ \
306 .id = REPLY_MSG_ID_BASE + VL_API_##N, \
307 .name = #n, \
308 .handler = vl_api_##n##_t_handler, \
309 .endian = vl_api_##n##_t_endian, \
310 .format_fn = vl_api_##n##_t_format, \
311 .size = sizeof (vl_api_##n##_t), \
312 .traced = 1, \
313 .tojson = vl_api_##n##_t_tojson, \
314 .fromjson = vl_api_##n##_t_fromjson, \
315 .calc_size = vl_api_##n##_t_calc_size, \
316 });
Florin Coras697faea2018-06-27 17:10:49 -0700317 foreach_sock_msg;
318#undef _
319}
320
321/*
322 * VPP-API message functions
323 */
Florin Corasb88de902020-09-08 16:47:57 -0700324static void
325vcl_bapi_send_session_enable_disable (u8 is_enable)
Florin Coras697faea2018-06-27 17:10:49 -0700326{
Florin Coras47c40e22018-11-26 17:01:36 -0800327 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras697faea2018-06-27 17:10:49 -0700328 vl_api_session_enable_disable_t *bmp;
329 bmp = vl_msg_api_alloc (sizeof (*bmp));
330 memset (bmp, 0, sizeof (*bmp));
331
Filip Tehlar0046e972021-06-26 22:12:08 +0000332 bmp->_vl_msg_id = ntohs (REPLY_MSG_ID_BASE + VL_API_SESSION_ENABLE_DISABLE);
Florin Corascc7c88e2020-09-15 15:56:51 -0700333 bmp->client_index = wrk->api_client_handle;
Florin Coras697faea2018-06-27 17:10:49 -0700334 bmp->context = htonl (0xfeedface);
335 bmp->is_enable = is_enable;
Florin Coras47c40e22018-11-26 17:01:36 -0800336 vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & bmp);
Florin Coras697faea2018-06-27 17:10:49 -0700337}
338
339void
Florin Corasb88de902020-09-08 16:47:57 -0700340vcl_bapi_send_attach (void)
Florin Coras697faea2018-06-27 17:10:49 -0700341{
Florin Coras47c40e22018-11-26 17:01:36 -0800342 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Corasd747c3c2019-10-20 19:55:56 -0700343 u8 tls_engine = CRYPTO_ENGINE_OPENSSL;
Florin Coras458089b2019-08-21 16:20:44 -0700344 vl_api_app_attach_t *bmp;
Florin Coras697faea2018-06-27 17:10:49 -0700345 u8 nsid_len = vec_len (vcm->cfg.namespace_id);
346 u8 app_is_proxy = (vcm->cfg.app_proxy_transport_tcp ||
347 vcm->cfg.app_proxy_transport_udp);
348
Florin Corasd747c3c2019-10-20 19:55:56 -0700349 tls_engine = vcm->cfg.tls_engine ? vcm->cfg.tls_engine : tls_engine;
350
Florin Coras697faea2018-06-27 17:10:49 -0700351 bmp = vl_msg_api_alloc (sizeof (*bmp));
352 memset (bmp, 0, sizeof (*bmp));
353
Filip Tehlar0046e972021-06-26 22:12:08 +0000354 bmp->_vl_msg_id = ntohs (REPLY_MSG_ID_BASE + VL_API_APP_ATTACH);
Florin Corascc7c88e2020-09-15 15:56:51 -0700355 bmp->client_index = wrk->api_client_handle;
Florin Coras697faea2018-06-27 17:10:49 -0700356 bmp->context = htonl (0xfeedface);
357 bmp->options[APP_OPTIONS_FLAGS] =
358 APP_OPTIONS_FLAGS_ACCEPT_REDIRECT | APP_OPTIONS_FLAGS_ADD_SEGMENT |
359 (vcm->cfg.app_scope_local ? APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE : 0) |
360 (vcm->cfg.app_scope_global ? APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE : 0) |
Florin Coras54693d22018-07-17 10:46:29 -0700361 (app_is_proxy ? APP_OPTIONS_FLAGS_IS_PROXY : 0) |
Junfeng Wangc795b882022-08-12 16:24:46 +0800362 (vcm->cfg.use_mq_eventfd ? APP_OPTIONS_FLAGS_EVT_MQ_USE_EVENTFD : 0) |
363 (vcm->cfg.huge_page ? APP_OPTIONS_FLAGS_USE_HUGE_PAGE : 0);
Florin Coras697faea2018-06-27 17:10:49 -0700364 bmp->options[APP_OPTIONS_PROXY_TRANSPORT] =
365 (u64) ((vcm->cfg.app_proxy_transport_tcp ? 1 << TRANSPORT_PROTO_TCP : 0) |
366 (vcm->cfg.app_proxy_transport_udp ? 1 << TRANSPORT_PROTO_UDP : 0));
367 bmp->options[APP_OPTIONS_SEGMENT_SIZE] = vcm->cfg.segment_size;
368 bmp->options[APP_OPTIONS_ADD_SEGMENT_SIZE] = vcm->cfg.add_segment_size;
369 bmp->options[APP_OPTIONS_RX_FIFO_SIZE] = vcm->cfg.rx_fifo_size;
370 bmp->options[APP_OPTIONS_TX_FIFO_SIZE] = vcm->cfg.tx_fifo_size;
371 bmp->options[APP_OPTIONS_PREALLOC_FIFO_PAIRS] =
372 vcm->cfg.preallocated_fifo_pairs;
373 bmp->options[APP_OPTIONS_EVT_QUEUE_SIZE] = vcm->cfg.event_queue_size;
Florin Corasd747c3c2019-10-20 19:55:56 -0700374 bmp->options[APP_OPTIONS_TLS_ENGINE] = tls_engine;
Florin Coras697faea2018-06-27 17:10:49 -0700375 if (nsid_len)
376 {
Jakub Grajciarb4e5e502020-01-31 09:35:29 +0100377 vl_api_vec_to_api_string (vcm->cfg.namespace_id, &bmp->namespace_id);
Florin Coras697faea2018-06-27 17:10:49 -0700378 bmp->options[APP_OPTIONS_NAMESPACE_SECRET] = vcm->cfg.namespace_secret;
379 }
Florin Coras47c40e22018-11-26 17:01:36 -0800380 vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & bmp);
Florin Coras697faea2018-06-27 17:10:49 -0700381}
382
383void
Florin Corasb88de902020-09-08 16:47:57 -0700384vcl_bapi_send_detach (void)
Florin Coras697faea2018-06-27 17:10:49 -0700385{
Florin Coras47c40e22018-11-26 17:01:36 -0800386 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras697faea2018-06-27 17:10:49 -0700387 vl_api_application_detach_t *bmp;
388 bmp = vl_msg_api_alloc (sizeof (*bmp));
389 memset (bmp, 0, sizeof (*bmp));
390
Filip Tehlar0046e972021-06-26 22:12:08 +0000391 bmp->_vl_msg_id = ntohs (REPLY_MSG_ID_BASE + VL_API_APPLICATION_DETACH);
Florin Corascc7c88e2020-09-15 15:56:51 -0700392 bmp->client_index = wrk->api_client_handle;
Florin Coras697faea2018-06-27 17:10:49 -0700393 bmp->context = htonl (0xfeedface);
Florin Coras47c40e22018-11-26 17:01:36 -0800394 vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & bmp);
Florin Coras697faea2018-06-27 17:10:49 -0700395}
396
Florin Corasb88de902020-09-08 16:47:57 -0700397static void
398vcl_bapi_send_app_worker_add_del (u8 is_add)
Florin Coras134a9962018-08-28 11:32:04 -0700399{
400 vcl_worker_t *wrk = vcl_worker_get_current ();
401 vl_api_app_worker_add_del_t *mp;
Florin Coras134a9962018-08-28 11:32:04 -0700402
403 mp = vl_msg_api_alloc (sizeof (*mp));
404 memset (mp, 0, sizeof (*mp));
405
Filip Tehlar0046e972021-06-26 22:12:08 +0000406 mp->_vl_msg_id = ntohs (REPLY_MSG_ID_BASE + VL_API_APP_WORKER_ADD_DEL);
Florin Corascc7c88e2020-09-15 15:56:51 -0700407 mp->client_index = wrk->api_client_handle;
Florin Corasc1f5a432018-11-20 11:31:26 -0800408 mp->app_index = clib_host_to_net_u32 (vcm->app_index);
Florin Coras01f3f892018-12-02 12:45:53 -0800409 mp->context = wrk->wrk_index;
Florin Coras134a9962018-08-28 11:32:04 -0700410 mp->is_add = is_add;
411 if (!is_add)
Florin Coras01f3f892018-12-02 12:45:53 -0800412 mp->wrk_index = clib_host_to_net_u32 (wrk->vpp_wrk_index);
413
414 vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & mp);
415}
416
Florin Corasb88de902020-09-08 16:47:57 -0700417static void
418vcl_bapi_send_child_worker_del (vcl_worker_t * child_wrk)
Florin Coras01f3f892018-12-02 12:45:53 -0800419{
420 vcl_worker_t *wrk = vcl_worker_get_current ();
421 vl_api_app_worker_add_del_t *mp;
422
423 mp = vl_msg_api_alloc (sizeof (*mp));
424 memset (mp, 0, sizeof (*mp));
425
Filip Tehlar0046e972021-06-26 22:12:08 +0000426 mp->_vl_msg_id = ntohs (REPLY_MSG_ID_BASE + VL_API_APP_WORKER_ADD_DEL);
Florin Corascc7c88e2020-09-15 15:56:51 -0700427 mp->client_index = wrk->api_client_handle;
Florin Coras01f3f892018-12-02 12:45:53 -0800428 mp->app_index = clib_host_to_net_u32 (vcm->app_index);
429 mp->context = wrk->wrk_index;
430 mp->is_add = 0;
431 mp->wrk_index = clib_host_to_net_u32 (child_wrk->vpp_wrk_index);
Florin Coras134a9962018-08-28 11:32:04 -0700432
Florin Coras47c40e22018-11-26 17:01:36 -0800433 vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & mp);
Florin Coras134a9962018-08-28 11:32:04 -0700434}
435
Florin Corasa5a9efd2021-01-05 17:03:29 -0800436static void
437vcl_bapi_send_app_add_cert_key_pair (vppcom_cert_key_pair_t *ckpair)
Ping Yu34a3a082018-11-30 19:16:17 -0500438{
439 vcl_worker_t *wrk = vcl_worker_get_current ();
Sivaprasad Tummala39d25e52021-07-27 17:38:00 +0530440 u32 cert_len = ckpair->cert_len;
441 u32 key_len = ckpair->key_len;
Florin Corasa5a9efd2021-01-05 17:03:29 -0800442 vl_api_app_add_cert_key_pair_t *bmp;
Ping Yu34a3a082018-11-30 19:16:17 -0500443
Florin Corasa5a9efd2021-01-05 17:03:29 -0800444 bmp = vl_msg_api_alloc (sizeof (*bmp) + cert_len + key_len);
445 clib_memset (bmp, 0, sizeof (*bmp) + cert_len + key_len);
446
Filip Tehlar0046e972021-06-26 22:12:08 +0000447 bmp->_vl_msg_id = ntohs (REPLY_MSG_ID_BASE + VL_API_APP_ADD_CERT_KEY_PAIR);
Florin Corasa5a9efd2021-01-05 17:03:29 -0800448 bmp->client_index = wrk->api_client_handle;
449 bmp->context = wrk->wrk_index;
450 bmp->cert_len = clib_host_to_net_u16 (cert_len);
451 bmp->certkey_len = clib_host_to_net_u16 (key_len + cert_len);
Sivaprasad Tummala39d25e52021-07-27 17:38:00 +0530452 clib_memcpy_fast (bmp->certkey, ckpair->cert, cert_len);
453 clib_memcpy_fast (bmp->certkey + cert_len, ckpair->key, key_len);
Florin Corasa5a9efd2021-01-05 17:03:29 -0800454
455 vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) &bmp);
Ping Yu34a3a082018-11-30 19:16:17 -0500456}
457
Florin Corasa5a9efd2021-01-05 17:03:29 -0800458static void
459vcl_bapi_send_app_del_cert_key_pair (u32 ckpair_index)
Ping Yu34a3a082018-11-30 19:16:17 -0500460{
461 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Corasa5a9efd2021-01-05 17:03:29 -0800462 vl_api_app_del_cert_key_pair_t *bmp;
463 bmp = vl_msg_api_alloc (sizeof (*bmp));
464 clib_memset (bmp, 0, sizeof (*bmp));
Ping Yu34a3a082018-11-30 19:16:17 -0500465
Filip Tehlar0046e972021-06-26 22:12:08 +0000466 bmp->_vl_msg_id = ntohs (REPLY_MSG_ID_BASE + VL_API_APP_DEL_CERT_KEY_PAIR);
Florin Corasa5a9efd2021-01-05 17:03:29 -0800467 bmp->client_index = wrk->api_client_handle;
468 bmp->context = wrk->wrk_index;
469 bmp->index = clib_host_to_net_u32 (ckpair_index);
470 vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) &bmp);
Ping Yu34a3a082018-11-30 19:16:17 -0500471}
472
Florin Coras697faea2018-06-27 17:10:49 -0700473u32
Florin Corasb88de902020-09-08 16:47:57 -0700474vcl_bapi_max_nsid_len (void)
Florin Coras697faea2018-06-27 17:10:49 -0700475{
Florin Coras888d9f02020-04-02 23:00:13 +0000476 vl_api_app_attach_t *mp;
Florin Coras697faea2018-06-27 17:10:49 -0700477 return (sizeof (mp->namespace_id) - 1);
478}
479
Florin Corasb88de902020-09-08 16:47:57 -0700480static void
481vcl_bapi_init_error_string_table (void)
Florin Coras697faea2018-06-27 17:10:49 -0700482{
483 vcm->error_string_by_error_number = hash_create (0, sizeof (uword));
484
485#define _(n, v, s) hash_set (vcm->error_string_by_error_number, -v, s);
486 foreach_vnet_api_error;
487#undef _
488
489 hash_set (vcm->error_string_by_error_number, 99, "Misc");
490}
491
Florin Corasb88de902020-09-08 16:47:57 -0700492static void
493vcl_bapi_cleanup (void)
494{
495 socket_client_main_t *scm = &socket_client_main;
496 api_main_t *am = vlibapi_get_main ();
497
498 am->my_client_index = ~0;
499 am->my_registration = 0;
500 am->vl_input_queue = 0;
501 am->msg_index_by_name_and_crc = 0;
502 scm->socket_fd = 0;
503
504 vl_client_api_unmap ();
505}
506
507static int
508vcl_bapi_connect_to_vpp (void)
Florin Coras697faea2018-06-27 17:10:49 -0700509{
Florin Coras47c40e22018-11-26 17:01:36 -0800510 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras697faea2018-06-27 17:10:49 -0700511 vppcom_cfg_t *vcl_cfg = &vcm->cfg;
Florin Corasb88de902020-09-08 16:47:57 -0700512 int rv = VPPCOM_OK;
Florin Corasd4c70922019-11-28 14:21:21 -0800513 api_main_t *am;
Florin Corasb88de902020-09-08 16:47:57 -0700514 u8 *wrk_name;
515
jiangxiaomingb438c762020-10-13 14:23:29 +0800516 wrk_name = format (0, "%v-wrk-%u%c", vcm->app_name, wrk->wrk_index, 0);
Florin Corasb88de902020-09-08 16:47:57 -0700517
518 /* Make sure api is cleaned up in case this is a connect from a
519 * forked worker */
520 vcl_bapi_cleanup ();
Florin Corasd4c70922019-11-28 14:21:21 -0800521
522 vlibapi_set_main (&wrk->bapi_api_ctx);
Florin Corasfda21c92021-04-07 17:03:01 -0700523 vlibapi_set_memory_client_main (&wrk->bapi_mem_ctx);
Florin Coras697faea2018-06-27 17:10:49 -0700524
Florin Coras165f3ae2020-11-08 18:04:33 -0800525 if (!vcl_cfg->vpp_bapi_socket_name)
Florin Coras697faea2018-06-27 17:10:49 -0700526 {
Florin Coras165f3ae2020-11-08 18:04:33 -0800527 rv = VPPCOM_EINVAL;
528 goto error;
Florin Coras697faea2018-06-27 17:10:49 -0700529 }
Florin Coras165f3ae2020-11-08 18:04:33 -0800530
531 if (vl_socket_client_connect2 (&wrk->bapi_sock_ctx,
532 (char *) vcl_cfg->vpp_bapi_socket_name,
533 (char *) wrk_name,
534 0 /* default rx/tx buffer */ ))
Florin Coras697faea2018-06-27 17:10:49 -0700535 {
Florin Coras165f3ae2020-11-08 18:04:33 -0800536 VERR ("app (%s) socket connect failed!", wrk_name);
537 rv = VPPCOM_ECONNREFUSED;
538 goto error;
539 }
Florin Coras697faea2018-06-27 17:10:49 -0700540
Florin Coras165f3ae2020-11-08 18:04:33 -0800541 if (vl_socket_client_init_shm2 (&wrk->bapi_sock_ctx, 0,
542 1 /* want_pthread */ ))
543 {
544 VERR ("app (%s) init shm failed!", wrk_name);
545 rv = VPPCOM_ECONNREFUSED;
546 goto error;
Florin Coras697faea2018-06-27 17:10:49 -0700547 }
Filip Tehlar0046e972021-06-26 22:12:08 +0000548 vcl_bapi_hookup ();
Florin Coras697faea2018-06-27 17:10:49 -0700549
Florin Corasd4c70922019-11-28 14:21:21 -0800550 am = vlibapi_get_main ();
Florin Coras47c40e22018-11-26 17:01:36 -0800551 wrk->vl_input_queue = am->shmem_hdr->vl_input_queue;
Florin Corascc7c88e2020-09-15 15:56:51 -0700552 wrk->api_client_handle = (u32) am->my_client_index;
Florin Coras99368312018-08-02 10:45:44 -0700553
Florin Corasb88de902020-09-08 16:47:57 -0700554 VDBG (0, "app (%s) is connected to VPP!", wrk_name);
Florin Coras697faea2018-06-27 17:10:49 -0700555 vcl_evt (VCL_EVT_INIT, vcm);
Florin Corasb88de902020-09-08 16:47:57 -0700556
557error:
558 vec_free (wrk_name);
559 return rv;
Florin Coras697faea2018-06-27 17:10:49 -0700560}
561
Florin Coras470b2a62019-08-03 18:53:48 -0700562void
Florin Corasb88de902020-09-08 16:47:57 -0700563vcl_bapi_disconnect_from_vpp (void)
Florin Coras470b2a62019-08-03 18:53:48 -0700564{
Florin Coras64cf4592019-12-12 12:01:24 -0800565 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras470b2a62019-08-03 18:53:48 -0700566 vppcom_cfg_t *vcl_cfg = &vcm->cfg;
567
Florin Corasb88de902020-09-08 16:47:57 -0700568 if (vcl_cfg->vpp_bapi_socket_name)
Florin Coras64cf4592019-12-12 12:01:24 -0800569 vl_socket_client_disconnect2 (&wrk->bapi_sock_ctx);
Florin Coras470b2a62019-08-03 18:53:48 -0700570 else
571 vl_client_disconnect_from_vlib ();
572}
573
Florin Corasb88de902020-09-08 16:47:57 -0700574static const char *
575vcl_bapi_app_state_str (vcl_bapi_app_state_t state)
576{
577 char *st;
578
579 switch (state)
580 {
581 case STATE_APP_START:
582 st = "STATE_APP_START";
583 break;
584
585 case STATE_APP_CONN_VPP:
586 st = "STATE_APP_CONN_VPP";
587 break;
588
589 case STATE_APP_ENABLED:
590 st = "STATE_APP_ENABLED";
591 break;
592
593 case STATE_APP_ATTACHED:
594 st = "STATE_APP_ATTACHED";
595 break;
596
597 default:
598 st = "UNKNOWN_APP_STATE";
599 break;
600 }
601
602 return st;
603}
604
605static int
Florin Coras3b6c84c2021-01-05 20:45:44 -0800606vcl_bapi_wait_for_wrk_state_change (vcl_bapi_app_state_t app_state)
Florin Corasb88de902020-09-08 16:47:57 -0700607{
608 vcl_worker_t *wrk = vcl_worker_get_current ();
609 f64 timeout = clib_time_now (&wrk->clib_time) + vcm->cfg.app_timeout;
610
611 while (clib_time_now (&wrk->clib_time) < timeout)
612 {
Florin Coras3b6c84c2021-01-05 20:45:44 -0800613 if (wrk->bapi_app_state == app_state)
Florin Corasb88de902020-09-08 16:47:57 -0700614 return VPPCOM_OK;
Florin Coras3b6c84c2021-01-05 20:45:44 -0800615 if (wrk->bapi_app_state == STATE_APP_FAILED)
Florin Corasb88de902020-09-08 16:47:57 -0700616 return VPPCOM_ECONNABORTED;
617 }
wanghanlin85207e62021-07-06 15:57:55 +0800618 VDBG (0, "timeout waiting for state %s, current state %d",
619 vcl_bapi_app_state_str (app_state), wrk->bapi_app_state);
Florin Corasb88de902020-09-08 16:47:57 -0700620 vcl_evt (VCL_EVT_SESSION_TIMEOUT, vcm, bapi_app_state);
621
622 return VPPCOM_ETIMEDOUT;
623}
624
625static int
626vcl_bapi_session_enable (void)
627{
Florin Coras3b6c84c2021-01-05 20:45:44 -0800628 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Corasb88de902020-09-08 16:47:57 -0700629 int rv;
630
Florin Coras3b6c84c2021-01-05 20:45:44 -0800631 if (wrk->bapi_app_state != STATE_APP_ENABLED)
Florin Corasb88de902020-09-08 16:47:57 -0700632 {
633 vcl_bapi_send_session_enable_disable (1 /* is_enabled == TRUE */ );
Florin Coras3b6c84c2021-01-05 20:45:44 -0800634 rv = vcl_bapi_wait_for_wrk_state_change (STATE_APP_ENABLED);
Florin Corasb88de902020-09-08 16:47:57 -0700635 if (PREDICT_FALSE (rv))
636 {
637 VDBG (0, "application session enable timed out! returning %d (%s)",
638 rv, vppcom_retval_str (rv));
639 return rv;
640 }
641 }
642 return VPPCOM_OK;
643}
644
645static int
646vcl_bapi_init (void)
647{
Florin Coras3b6c84c2021-01-05 20:45:44 -0800648 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Corasb88de902020-09-08 16:47:57 -0700649 int rv;
650
Florin Coras3b6c84c2021-01-05 20:45:44 -0800651 wrk->bapi_app_state = STATE_APP_START;
Florin Corasb88de902020-09-08 16:47:57 -0700652 vcl_bapi_init_error_string_table ();
653 rv = vcl_bapi_connect_to_vpp ();
654 if (rv)
655 {
656 VERR ("couldn't connect to VPP!");
657 return rv;
658 }
659 VDBG (0, "sending session enable");
660 rv = vcl_bapi_session_enable ();
661 if (rv)
662 {
663 VERR ("vppcom_app_session_enable() failed!");
664 return rv;
665 }
666
667 return 0;
668}
669
670int
671vcl_bapi_attach (void)
672{
673 int rv;
674
675 /* API hookup and connect to VPP */
676 if ((rv = vcl_bapi_init ()))
677 return rv;
678
679 vcl_bapi_send_attach ();
Florin Coras3b6c84c2021-01-05 20:45:44 -0800680 rv = vcl_bapi_wait_for_wrk_state_change (STATE_APP_ATTACHED);
Florin Corasb88de902020-09-08 16:47:57 -0700681 if (PREDICT_FALSE (rv))
682 {
683 VDBG (0, "application attach timed out! returning %d (%s)", rv,
684 vppcom_retval_str (rv));
685 return rv;
686 }
687
688 return 0;
689}
690
691int
692vcl_bapi_app_worker_add (void)
693{
Florin Coras3b6c84c2021-01-05 20:45:44 -0800694 vcl_worker_t *wrk = vcl_worker_get_current ();
695
Florin Corasb88de902020-09-08 16:47:57 -0700696 if (vcl_bapi_connect_to_vpp ())
697 return -1;
698
Florin Coras3b6c84c2021-01-05 20:45:44 -0800699 wrk->bapi_app_state = STATE_APP_ADDING_WORKER;
Florin Corasb88de902020-09-08 16:47:57 -0700700 vcl_bapi_send_app_worker_add_del (1 /* is_add */ );
Florin Coras3b6c84c2021-01-05 20:45:44 -0800701 if (vcl_bapi_wait_for_wrk_state_change (STATE_APP_READY))
Florin Corasb88de902020-09-08 16:47:57 -0700702 return -1;
703 return 0;
704}
705
706void
707vcl_bapi_app_worker_del (vcl_worker_t * wrk)
708{
709 /* Notify vpp that the worker is going away */
710 if (wrk->wrk_index == vcl_get_worker_index ())
711 vcl_bapi_send_app_worker_add_del (0 /* is_add */ );
712 else
713 vcl_bapi_send_child_worker_del (wrk);
714
715 /* Disconnect the binary api */
716 if (vec_len (vcm->workers) == 1)
717 vcl_bapi_disconnect_from_vpp ();
718 else
719 vl_client_send_disconnect (1 /* vpp should cleanup */ );
720}
721
722int
Florin Coras935ce752020-09-08 22:43:47 -0700723vcl_bapi_recv_fds (vcl_worker_t * wrk, int *fds, int n_fds)
724{
725 clib_error_t *err;
726
727 if ((err = vl_socket_client_recv_fd_msg2 (&wrk->bapi_sock_ctx, fds, n_fds,
728 5)))
729 {
730 clib_error_report (err);
731 return -1;
732 }
733
734 return 0;
735}
736
737int
Florin Corasa5a9efd2021-01-05 17:03:29 -0800738vcl_bapi_add_cert_key_pair (vppcom_cert_key_pair_t *ckpair)
Florin Corasb88de902020-09-08 16:47:57 -0700739{
Florin Corasb88de902020-09-08 16:47:57 -0700740 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Corasb88de902020-09-08 16:47:57 -0700741
Florin Corasa5a9efd2021-01-05 17:03:29 -0800742 if (ckpair->key_len == 0 || ckpair->key_len == ~0)
743 return VPPCOM_EINVAL;
Florin Corasb88de902020-09-08 16:47:57 -0700744
Florin Corasa5a9efd2021-01-05 17:03:29 -0800745 vcl_bapi_send_app_add_cert_key_pair (ckpair);
Florin Coras3b6c84c2021-01-05 20:45:44 -0800746 wrk->bapi_app_state = STATE_APP_ADDING_TLS_DATA;
747 vcl_bapi_wait_for_wrk_state_change (STATE_APP_READY);
Florin Corasa5a9efd2021-01-05 17:03:29 -0800748 if (wrk->bapi_app_state == STATE_APP_READY)
749 return wrk->bapi_return;
750 return VPPCOM_EFAULT;
Florin Corasb88de902020-09-08 16:47:57 -0700751}
752
753int
Florin Corasa5a9efd2021-01-05 17:03:29 -0800754vcl_bapi_del_cert_key_pair (u32 ckpair_index)
Florin Corasb88de902020-09-08 16:47:57 -0700755{
Florin Corasa5a9efd2021-01-05 17:03:29 -0800756 /* Don't wait for reply */
757 vcl_bapi_send_app_del_cert_key_pair (ckpair_index);
758 return 0;
Florin Corasb88de902020-09-08 16:47:57 -0700759}
760
761int
762vcl_bapi_worker_set (void)
763{
764 vcl_worker_t *wrk = vcl_worker_get_current ();
765 int i;
766
767 /* Find the first worker with the same pid */
768 for (i = 0; i < vec_len (vcm->workers); i++)
769 {
770 if (i == wrk->wrk_index)
771 continue;
772 if (vcm->workers[i].current_pid == wrk->current_pid)
773 {
774 wrk->vl_input_queue = vcm->workers[i].vl_input_queue;
Florin Corascc7c88e2020-09-15 15:56:51 -0700775 wrk->api_client_handle = vcm->workers[i].api_client_handle;
Florin Corasb88de902020-09-08 16:47:57 -0700776 return 0;
777 }
778 }
779 return -1;
780}
781
Florin Coras697faea2018-06-27 17:10:49 -0700782/*
783 * fd.io coding-style-patch-verification: ON
784 *
785 * Local Variables:
786 * eval: (c-set-style "gnu")
787 * End:
788 */