blob: e245c4dc86579e47c8bd42b4d15943ffd3f5dfc0 [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{
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
Florin Corasb88de902020-09-08 16:47:57 -070061 vcm->bapi_app_state = STATE_APP_ENABLED;
Florin Coras697faea2018-06-27 17:10:49 -070062}
63
Florin Corasd85de682018-11-29 17:02:29 -080064static void
Florin Coras458089b2019-08-21 16:20:44 -070065vl_api_app_attach_reply_t_handler (vl_api_app_attach_reply_t * mp)
Florin Coras697faea2018-06-27 17:10:49 -070066{
Florin Coras134a9962018-08-28 11:32:04 -070067 vcl_worker_t *wrk = vcl_worker_get (0);
Florin Coras458089b2019-08-21 16:20:44 -070068 svm_msg_q_t *ctrl_mq;
Florin Corasd85de682018-11-29 17:02:29 -080069 u64 segment_handle;
Jakub Grajciarb4e5e502020-01-31 09:35:29 +010070 int *fds = 0, i, rv;
Florin Coras99368312018-08-02 10:45:44 -070071 u32 n_fds = 0;
Jakub Grajciarb4e5e502020-01-31 09:35:29 +010072 char *segment_name = 0;
Florin Coras697faea2018-06-27 17:10:49 -070073
Florin Coras697faea2018-06-27 17:10:49 -070074 if (mp->retval)
75 {
Florin Corasa5efca32019-03-20 08:33:10 -070076 VERR ("attach failed: %U", format_api_error, ntohl (mp->retval));
77 goto failed;
Florin Coras697faea2018-06-27 17:10:49 -070078 }
79
Florin Coras458089b2019-08-21 16:20:44 -070080 wrk->app_event_queue = uword_to_pointer (mp->app_mq, svm_msg_q_t *);
81 ctrl_mq = uword_to_pointer (mp->vpp_ctrl_mq, svm_msg_q_t *);
Florin Coras22ba3302019-08-29 22:45:04 -070082 vcm->ctrl_mq = wrk->ctrl_mq = ctrl_mq;
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
103 if (mp->fd_flags & SESSION_FD_F_MEMFD_SEGMENT)
Jakub Grajciarb4e5e502020-01-31 09:35:29 +0100104 {
105 segment_name = vl_api_from_api_to_new_c_string (&mp->segment_name);
106 rv =
107 vcl_segment_attach (segment_handle, segment_name,
108 SSVM_SEGMENT_MEMFD, fds[n_fds++]);
109 vec_free (segment_name);
110 if (rv != 0)
111 goto failed;
112 }
113
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 {
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 Corasb88de902020-09-08 16:47:57 -0700136 vcm->bapi_app_state = STATE_APP_ATTACHED;
Florin Corasa5efca32019-03-20 08:33:10 -0700137 return;
138
139failed:
Florin Corasb88de902020-09-08 16:47:57 -0700140 vcm->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
156 if (mp->retval)
157 {
158 clib_warning ("VCL<%d>: add/del worker failed: %U", getpid (),
159 format_api_error, ntohl (mp->retval));
160 goto failed;
161 }
Florin Coras053a0e42018-11-13 15:52:38 -0800162
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 Corasdc2e2512018-12-03 17:47:26 -0800171 wrk->vpp_wrk_index = clib_net_to_host_u32 (mp->wrk_index);
Florin Coras134a9962018-08-28 11:32:04 -0700172 wrk->app_event_queue = uword_to_pointer (mp->app_event_queue_address,
173 svm_msg_q_t *);
Florin Coras22ba3302019-08-29 22:45:04 -0700174 wrk->ctrl_mq = vcm->ctrl_mq;
Florin Coras134a9962018-08-28 11:32:04 -0700175
Florin Corasd85de682018-11-29 17:02:29 -0800176 segment_handle = clib_net_to_host_u64 (mp->segment_handle);
177 if (segment_handle == VCL_INVALID_SEGMENT_HANDLE)
178 {
179 clib_warning ("invalid segment handle");
Florin Coras01f3f892018-12-02 12:45:53 -0800180 goto failed;
Florin Corasd85de682018-11-29 17:02:29 -0800181 }
182
Florin Coras134a9962018-08-28 11:32:04 -0700183 if (mp->n_fds)
184 {
185 vec_validate (fds, mp->n_fds);
Florin Corasd4c70922019-11-28 14:21:21 -0800186 if (vl_socket_client_recv_fd_msg2 (&wrk->bapi_sock_ctx, fds, mp->n_fds,
187 5))
Florin Coras617574d2019-07-01 07:32:31 -0700188 goto failed;
Florin Coras134a9962018-08-28 11:32:04 -0700189
190 if (mp->fd_flags & SESSION_FD_F_VPP_MQ_SEGMENT)
Florin Coras01f3f892018-12-02 12:45:53 -0800191 if (vcl_segment_attach (vcl_vpp_worker_segment_handle (wrk_index),
192 "vpp-worker-seg", SSVM_SEGMENT_MEMFD,
193 fds[n_fds++]))
Florin Coras134a9962018-08-28 11:32:04 -0700194 goto failed;
195
196 if (mp->fd_flags & SESSION_FD_F_MEMFD_SEGMENT)
Jakub Grajciarb4e5e502020-01-31 09:35:29 +0100197 {
198 segment_name = vl_api_from_api_to_new_c_string (&mp->segment_name);
199 rv =
200 vcl_segment_attach (segment_handle, segment_name,
201 SSVM_SEGMENT_MEMFD, fds[n_fds++]);
202 vec_free (segment_name);
203 if (rv != 0)
204 goto failed;
205 }
Florin Coras134a9962018-08-28 11:32:04 -0700206
207 if (mp->fd_flags & SESSION_FD_F_MQ_EVENTFD)
208 {
209 svm_msg_q_set_consumer_eventfd (wrk->app_event_queue, fds[n_fds]);
210 vcl_mq_epoll_add_evfd (wrk, wrk->app_event_queue);
211 n_fds++;
212 }
213
214 vec_free (fds);
215 }
216 else
217 {
Jakub Grajciarb4e5e502020-01-31 09:35:29 +0100218 segment_name = vl_api_from_api_to_new_c_string (&mp->segment_name);
219 rv =
220 vcl_segment_attach (segment_handle, segment_name, SSVM_SEGMENT_SHM,
221 -1);
222 vec_free (segment_name);
223 if (rv != 0)
Florin Coras134a9962018-08-28 11:32:04 -0700224 goto failed;
225 }
Florin Corasb88de902020-09-08 16:47:57 -0700226 vcm->bapi_app_state = STATE_APP_READY;
Florin Coras053a0e42018-11-13 15:52:38 -0800227 VDBG (0, "worker %u vpp-worker %u added", wrk_index, wrk->vpp_wrk_index);
Florin Coras134a9962018-08-28 11:32:04 -0700228 return;
229
230failed:
Florin Corasb88de902020-09-08 16:47:57 -0700231 vcm->bapi_app_state = STATE_APP_FAILED;
Florin Coras617574d2019-07-01 07:32:31 -0700232 for (i = clib_max (n_fds - 1, 0); i < vec_len (fds); i++)
233 close (fds[i]);
234 vec_free (fds);
Florin Coras134a9962018-08-28 11:32:04 -0700235}
236
237static void
Ping Yu34a3a082018-11-30 19:16:17 -0500238 vl_api_application_tls_cert_add_reply_t_handler
239 (vl_api_application_tls_cert_add_reply_t * mp)
240{
241 if (mp->retval)
Florin Coras458089b2019-08-21 16:20:44 -0700242 VDBG (0, "add cert failed: %U", format_api_error, ntohl (mp->retval));
Florin Corasb88de902020-09-08 16:47:57 -0700243 vcm->bapi_app_state = STATE_APP_READY;
Ping Yu34a3a082018-11-30 19:16:17 -0500244}
245
246static void
247 vl_api_application_tls_key_add_reply_t_handler
248 (vl_api_application_tls_key_add_reply_t * mp)
249{
250 if (mp->retval)
Florin Coras458089b2019-08-21 16:20:44 -0700251 VDBG (0, "add key failed: %U", format_api_error, ntohl (mp->retval));
Florin Corasb88de902020-09-08 16:47:57 -0700252 vcm->bapi_app_state = STATE_APP_READY;
Ping Yu34a3a082018-11-30 19:16:17 -0500253}
254
Florin Coras99368312018-08-02 10:45:44 -0700255#define foreach_sock_msg \
256_(SESSION_ENABLE_DISABLE_REPLY, session_enable_disable_reply) \
Florin Coras458089b2019-08-21 16:20:44 -0700257_(APP_ATTACH_REPLY, app_attach_reply) \
Ping Yu34a3a082018-11-30 19:16:17 -0500258_(APPLICATION_TLS_CERT_ADD_REPLY, application_tls_cert_add_reply) \
259_(APPLICATION_TLS_KEY_ADD_REPLY, application_tls_key_add_reply) \
Florin Coras134a9962018-08-28 11:32:04 -0700260_(APP_WORKER_ADD_DEL_REPLY, app_worker_add_del_reply) \
Florin Coras697faea2018-06-27 17:10:49 -0700261
Florin Corasb88de902020-09-08 16:47:57 -0700262static void
263vcl_bapi_hookup (void)
Florin Coras697faea2018-06-27 17:10:49 -0700264{
Florin Coras60116992018-08-27 09:52:18 -0700265#define _(N, n) \
266 vl_msg_api_set_handlers(VL_API_##N, #n, \
Florin Coras697faea2018-06-27 17:10:49 -0700267 vl_api_##n##_t_handler, \
268 vl_noop_handler, \
269 vl_api_##n##_t_endian, \
270 vl_api_##n##_t_print, \
271 sizeof(vl_api_##n##_t), 1);
272 foreach_sock_msg;
273#undef _
274}
275
276/*
277 * VPP-API message functions
278 */
Florin Corasb88de902020-09-08 16:47:57 -0700279static void
280vcl_bapi_send_session_enable_disable (u8 is_enable)
Florin Coras697faea2018-06-27 17:10:49 -0700281{
Florin Coras47c40e22018-11-26 17:01:36 -0800282 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras697faea2018-06-27 17:10:49 -0700283 vl_api_session_enable_disable_t *bmp;
284 bmp = vl_msg_api_alloc (sizeof (*bmp));
285 memset (bmp, 0, sizeof (*bmp));
286
287 bmp->_vl_msg_id = ntohs (VL_API_SESSION_ENABLE_DISABLE);
Florin Corascc7c88e2020-09-15 15:56:51 -0700288 bmp->client_index = wrk->api_client_handle;
Florin Coras697faea2018-06-27 17:10:49 -0700289 bmp->context = htonl (0xfeedface);
290 bmp->is_enable = is_enable;
Florin Coras47c40e22018-11-26 17:01:36 -0800291 vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & bmp);
Florin Coras697faea2018-06-27 17:10:49 -0700292}
293
294void
Florin Corasb88de902020-09-08 16:47:57 -0700295vcl_bapi_send_attach (void)
Florin Coras697faea2018-06-27 17:10:49 -0700296{
Florin Coras47c40e22018-11-26 17:01:36 -0800297 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Corasd747c3c2019-10-20 19:55:56 -0700298 u8 tls_engine = CRYPTO_ENGINE_OPENSSL;
Florin Coras458089b2019-08-21 16:20:44 -0700299 vl_api_app_attach_t *bmp;
Florin Coras697faea2018-06-27 17:10:49 -0700300 u8 nsid_len = vec_len (vcm->cfg.namespace_id);
301 u8 app_is_proxy = (vcm->cfg.app_proxy_transport_tcp ||
302 vcm->cfg.app_proxy_transport_udp);
303
Florin Corasd747c3c2019-10-20 19:55:56 -0700304 tls_engine = vcm->cfg.tls_engine ? vcm->cfg.tls_engine : tls_engine;
305
Florin Coras697faea2018-06-27 17:10:49 -0700306 bmp = vl_msg_api_alloc (sizeof (*bmp));
307 memset (bmp, 0, sizeof (*bmp));
308
Florin Coras458089b2019-08-21 16:20:44 -0700309 bmp->_vl_msg_id = ntohs (VL_API_APP_ATTACH);
Florin Corascc7c88e2020-09-15 15:56:51 -0700310 bmp->client_index = wrk->api_client_handle;
Florin Coras697faea2018-06-27 17:10:49 -0700311 bmp->context = htonl (0xfeedface);
312 bmp->options[APP_OPTIONS_FLAGS] =
313 APP_OPTIONS_FLAGS_ACCEPT_REDIRECT | APP_OPTIONS_FLAGS_ADD_SEGMENT |
314 (vcm->cfg.app_scope_local ? APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE : 0) |
315 (vcm->cfg.app_scope_global ? APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE : 0) |
Florin Coras54693d22018-07-17 10:46:29 -0700316 (app_is_proxy ? APP_OPTIONS_FLAGS_IS_PROXY : 0) |
Florin Coras99368312018-08-02 10:45:44 -0700317 (vcm->cfg.use_mq_eventfd ? APP_OPTIONS_FLAGS_EVT_MQ_USE_EVENTFD : 0);
Florin Coras697faea2018-06-27 17:10:49 -0700318 bmp->options[APP_OPTIONS_PROXY_TRANSPORT] =
319 (u64) ((vcm->cfg.app_proxy_transport_tcp ? 1 << TRANSPORT_PROTO_TCP : 0) |
320 (vcm->cfg.app_proxy_transport_udp ? 1 << TRANSPORT_PROTO_UDP : 0));
321 bmp->options[APP_OPTIONS_SEGMENT_SIZE] = vcm->cfg.segment_size;
322 bmp->options[APP_OPTIONS_ADD_SEGMENT_SIZE] = vcm->cfg.add_segment_size;
323 bmp->options[APP_OPTIONS_RX_FIFO_SIZE] = vcm->cfg.rx_fifo_size;
324 bmp->options[APP_OPTIONS_TX_FIFO_SIZE] = vcm->cfg.tx_fifo_size;
325 bmp->options[APP_OPTIONS_PREALLOC_FIFO_PAIRS] =
326 vcm->cfg.preallocated_fifo_pairs;
327 bmp->options[APP_OPTIONS_EVT_QUEUE_SIZE] = vcm->cfg.event_queue_size;
Florin Corasd747c3c2019-10-20 19:55:56 -0700328 bmp->options[APP_OPTIONS_TLS_ENGINE] = tls_engine;
Florin Coras697faea2018-06-27 17:10:49 -0700329 if (nsid_len)
330 {
Jakub Grajciarb4e5e502020-01-31 09:35:29 +0100331 vl_api_vec_to_api_string (vcm->cfg.namespace_id, &bmp->namespace_id);
Florin Coras697faea2018-06-27 17:10:49 -0700332 bmp->options[APP_OPTIONS_NAMESPACE_SECRET] = vcm->cfg.namespace_secret;
333 }
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 Corasb88de902020-09-08 16:47:57 -0700338vcl_bapi_send_detach (void)
Florin Coras697faea2018-06-27 17:10:49 -0700339{
Florin Coras47c40e22018-11-26 17:01:36 -0800340 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras697faea2018-06-27 17:10:49 -0700341 vl_api_application_detach_t *bmp;
342 bmp = vl_msg_api_alloc (sizeof (*bmp));
343 memset (bmp, 0, sizeof (*bmp));
344
345 bmp->_vl_msg_id = ntohs (VL_API_APPLICATION_DETACH);
Florin Corascc7c88e2020-09-15 15:56:51 -0700346 bmp->client_index = wrk->api_client_handle;
Florin Coras697faea2018-06-27 17:10:49 -0700347 bmp->context = htonl (0xfeedface);
Florin Coras47c40e22018-11-26 17:01:36 -0800348 vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & bmp);
Florin Coras697faea2018-06-27 17:10:49 -0700349}
350
Florin Corasb88de902020-09-08 16:47:57 -0700351static void
352vcl_bapi_send_app_worker_add_del (u8 is_add)
Florin Coras134a9962018-08-28 11:32:04 -0700353{
354 vcl_worker_t *wrk = vcl_worker_get_current ();
355 vl_api_app_worker_add_del_t *mp;
Florin Coras134a9962018-08-28 11:32:04 -0700356
357 mp = vl_msg_api_alloc (sizeof (*mp));
358 memset (mp, 0, sizeof (*mp));
359
360 mp->_vl_msg_id = ntohs (VL_API_APP_WORKER_ADD_DEL);
Florin Corascc7c88e2020-09-15 15:56:51 -0700361 mp->client_index = wrk->api_client_handle;
Florin Corasc1f5a432018-11-20 11:31:26 -0800362 mp->app_index = clib_host_to_net_u32 (vcm->app_index);
Florin Coras01f3f892018-12-02 12:45:53 -0800363 mp->context = wrk->wrk_index;
Florin Coras134a9962018-08-28 11:32:04 -0700364 mp->is_add = is_add;
365 if (!is_add)
Florin Coras01f3f892018-12-02 12:45:53 -0800366 mp->wrk_index = clib_host_to_net_u32 (wrk->vpp_wrk_index);
367
368 vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & mp);
369}
370
Florin Corasb88de902020-09-08 16:47:57 -0700371static void
372vcl_bapi_send_child_worker_del (vcl_worker_t * child_wrk)
Florin Coras01f3f892018-12-02 12:45:53 -0800373{
374 vcl_worker_t *wrk = vcl_worker_get_current ();
375 vl_api_app_worker_add_del_t *mp;
376
377 mp = vl_msg_api_alloc (sizeof (*mp));
378 memset (mp, 0, sizeof (*mp));
379
380 mp->_vl_msg_id = ntohs (VL_API_APP_WORKER_ADD_DEL);
Florin Corascc7c88e2020-09-15 15:56:51 -0700381 mp->client_index = wrk->api_client_handle;
Florin Coras01f3f892018-12-02 12:45:53 -0800382 mp->app_index = clib_host_to_net_u32 (vcm->app_index);
383 mp->context = wrk->wrk_index;
384 mp->is_add = 0;
385 mp->wrk_index = clib_host_to_net_u32 (child_wrk->vpp_wrk_index);
Florin Coras134a9962018-08-28 11:32:04 -0700386
Florin Coras47c40e22018-11-26 17:01:36 -0800387 vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & mp);
Florin Coras134a9962018-08-28 11:32:04 -0700388}
389
390void
Florin Corasb88de902020-09-08 16:47:57 -0700391vcl_bapi_send_application_tls_cert_add (vcl_session_t * session, char *cert,
392 u32 cert_len)
Ping Yu34a3a082018-11-30 19:16:17 -0500393{
394 vcl_worker_t *wrk = vcl_worker_get_current ();
395 vl_api_application_tls_cert_add_t *cert_mp;
396
397 cert_mp = vl_msg_api_alloc (sizeof (*cert_mp) + cert_len);
398 clib_memset (cert_mp, 0, sizeof (*cert_mp));
399 cert_mp->_vl_msg_id = ntohs (VL_API_APPLICATION_TLS_CERT_ADD);
Florin Corascc7c88e2020-09-15 15:56:51 -0700400 cert_mp->client_index = wrk->api_client_handle;
Ping Yu34a3a082018-11-30 19:16:17 -0500401 cert_mp->context = session->session_index;
402 cert_mp->cert_len = clib_host_to_net_u16 (cert_len);
403 clib_memcpy_fast (cert_mp->cert, cert, cert_len);
404 vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & cert_mp);
Ping Yu34a3a082018-11-30 19:16:17 -0500405}
406
407void
Florin Corasb88de902020-09-08 16:47:57 -0700408vcl_bapi_send_application_tls_key_add (vcl_session_t * session, char *key,
409 u32 key_len)
Ping Yu34a3a082018-11-30 19:16:17 -0500410{
411 vcl_worker_t *wrk = vcl_worker_get_current ();
412 vl_api_application_tls_key_add_t *key_mp;
413
414 key_mp = vl_msg_api_alloc (sizeof (*key_mp) + key_len);
415 clib_memset (key_mp, 0, sizeof (*key_mp));
416 key_mp->_vl_msg_id = ntohs (VL_API_APPLICATION_TLS_KEY_ADD);
Florin Corascc7c88e2020-09-15 15:56:51 -0700417 key_mp->client_index = wrk->api_client_handle;
Ping Yu34a3a082018-11-30 19:16:17 -0500418 key_mp->context = session->session_index;
419 key_mp->key_len = clib_host_to_net_u16 (key_len);
420 clib_memcpy_fast (key_mp->key, key, key_len);
421 vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & key_mp);
Ping Yu34a3a082018-11-30 19:16:17 -0500422}
423
Florin Coras697faea2018-06-27 17:10:49 -0700424u32
Florin Corasb88de902020-09-08 16:47:57 -0700425vcl_bapi_max_nsid_len (void)
Florin Coras697faea2018-06-27 17:10:49 -0700426{
Florin Coras888d9f02020-04-02 23:00:13 +0000427 vl_api_app_attach_t *mp;
Florin Coras697faea2018-06-27 17:10:49 -0700428 return (sizeof (mp->namespace_id) - 1);
429}
430
Florin Corasb88de902020-09-08 16:47:57 -0700431static void
432vcl_bapi_init_error_string_table (void)
Florin Coras697faea2018-06-27 17:10:49 -0700433{
434 vcm->error_string_by_error_number = hash_create (0, sizeof (uword));
435
436#define _(n, v, s) hash_set (vcm->error_string_by_error_number, -v, s);
437 foreach_vnet_api_error;
438#undef _
439
440 hash_set (vcm->error_string_by_error_number, 99, "Misc");
441}
442
Florin Corasb88de902020-09-08 16:47:57 -0700443static void
444vcl_bapi_cleanup (void)
445{
446 socket_client_main_t *scm = &socket_client_main;
447 api_main_t *am = vlibapi_get_main ();
448
449 am->my_client_index = ~0;
450 am->my_registration = 0;
451 am->vl_input_queue = 0;
452 am->msg_index_by_name_and_crc = 0;
453 scm->socket_fd = 0;
454
455 vl_client_api_unmap ();
456}
457
458static int
459vcl_bapi_connect_to_vpp (void)
Florin Coras697faea2018-06-27 17:10:49 -0700460{
Florin Coras47c40e22018-11-26 17:01:36 -0800461 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras697faea2018-06-27 17:10:49 -0700462 vppcom_cfg_t *vcl_cfg = &vcm->cfg;
Florin Corasb88de902020-09-08 16:47:57 -0700463 int rv = VPPCOM_OK;
Florin Corasd4c70922019-11-28 14:21:21 -0800464 api_main_t *am;
Florin Corasb88de902020-09-08 16:47:57 -0700465 u8 *wrk_name;
466
jiangxiaomingb438c762020-10-13 14:23:29 +0800467 wrk_name = format (0, "%v-wrk-%u%c", vcm->app_name, wrk->wrk_index, 0);
Florin Corasb88de902020-09-08 16:47:57 -0700468
469 /* Make sure api is cleaned up in case this is a connect from a
470 * forked worker */
471 vcl_bapi_cleanup ();
Florin Corasd4c70922019-11-28 14:21:21 -0800472
473 vlibapi_set_main (&wrk->bapi_api_ctx);
Florin Corasb88de902020-09-08 16:47:57 -0700474 vcl_bapi_hookup ();
Florin Coras697faea2018-06-27 17:10:49 -0700475
Florin Coras165f3ae2020-11-08 18:04:33 -0800476 if (!vcl_cfg->vpp_bapi_socket_name)
Florin Coras697faea2018-06-27 17:10:49 -0700477 {
Florin Coras165f3ae2020-11-08 18:04:33 -0800478 rv = VPPCOM_EINVAL;
479 goto error;
Florin Coras697faea2018-06-27 17:10:49 -0700480 }
Florin Coras165f3ae2020-11-08 18:04:33 -0800481
482 if (vl_socket_client_connect2 (&wrk->bapi_sock_ctx,
483 (char *) vcl_cfg->vpp_bapi_socket_name,
484 (char *) wrk_name,
485 0 /* default rx/tx buffer */ ))
Florin Coras697faea2018-06-27 17:10:49 -0700486 {
Florin Coras165f3ae2020-11-08 18:04:33 -0800487 VERR ("app (%s) socket connect failed!", wrk_name);
488 rv = VPPCOM_ECONNREFUSED;
489 goto error;
490 }
Florin Coras697faea2018-06-27 17:10:49 -0700491
Florin Coras165f3ae2020-11-08 18:04:33 -0800492 if (vl_socket_client_init_shm2 (&wrk->bapi_sock_ctx, 0,
493 1 /* want_pthread */ ))
494 {
495 VERR ("app (%s) init shm failed!", wrk_name);
496 rv = VPPCOM_ECONNREFUSED;
497 goto error;
Florin Coras697faea2018-06-27 17:10:49 -0700498 }
499
Florin Corasd4c70922019-11-28 14:21:21 -0800500 am = vlibapi_get_main ();
Florin Coras47c40e22018-11-26 17:01:36 -0800501 wrk->vl_input_queue = am->shmem_hdr->vl_input_queue;
Florin Corascc7c88e2020-09-15 15:56:51 -0700502 wrk->api_client_handle = (u32) am->my_client_index;
Florin Coras99368312018-08-02 10:45:44 -0700503
Florin Corasb88de902020-09-08 16:47:57 -0700504 VDBG (0, "app (%s) is connected to VPP!", wrk_name);
Florin Coras697faea2018-06-27 17:10:49 -0700505 vcl_evt (VCL_EVT_INIT, vcm);
Florin Corasb88de902020-09-08 16:47:57 -0700506
507error:
508 vec_free (wrk_name);
509 return rv;
Florin Coras697faea2018-06-27 17:10:49 -0700510}
511
Florin Coras470b2a62019-08-03 18:53:48 -0700512void
Florin Corasb88de902020-09-08 16:47:57 -0700513vcl_bapi_disconnect_from_vpp (void)
Florin Coras470b2a62019-08-03 18:53:48 -0700514{
Florin Coras64cf4592019-12-12 12:01:24 -0800515 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras470b2a62019-08-03 18:53:48 -0700516 vppcom_cfg_t *vcl_cfg = &vcm->cfg;
517
Florin Corasb88de902020-09-08 16:47:57 -0700518 if (vcl_cfg->vpp_bapi_socket_name)
Florin Coras64cf4592019-12-12 12:01:24 -0800519 vl_socket_client_disconnect2 (&wrk->bapi_sock_ctx);
Florin Coras470b2a62019-08-03 18:53:48 -0700520 else
521 vl_client_disconnect_from_vlib ();
522}
523
Florin Corasb88de902020-09-08 16:47:57 -0700524static const char *
525vcl_bapi_app_state_str (vcl_bapi_app_state_t state)
526{
527 char *st;
528
529 switch (state)
530 {
531 case STATE_APP_START:
532 st = "STATE_APP_START";
533 break;
534
535 case STATE_APP_CONN_VPP:
536 st = "STATE_APP_CONN_VPP";
537 break;
538
539 case STATE_APP_ENABLED:
540 st = "STATE_APP_ENABLED";
541 break;
542
543 case STATE_APP_ATTACHED:
544 st = "STATE_APP_ATTACHED";
545 break;
546
547 default:
548 st = "UNKNOWN_APP_STATE";
549 break;
550 }
551
552 return st;
553}
554
555static int
556vcl_bapi_wait_for_app_state_change (vcl_bapi_app_state_t app_state)
557{
558 vcl_worker_t *wrk = vcl_worker_get_current ();
559 f64 timeout = clib_time_now (&wrk->clib_time) + vcm->cfg.app_timeout;
560
561 while (clib_time_now (&wrk->clib_time) < timeout)
562 {
563 if (vcm->bapi_app_state == app_state)
564 return VPPCOM_OK;
565 if (vcm->bapi_app_state == STATE_APP_FAILED)
566 return VPPCOM_ECONNABORTED;
567 }
568 VDBG (0, "timeout waiting for state %s (%d)",
569 vcl_bapi_app_state_str (app_state), app_state);
570 vcl_evt (VCL_EVT_SESSION_TIMEOUT, vcm, bapi_app_state);
571
572 return VPPCOM_ETIMEDOUT;
573}
574
575static int
576vcl_bapi_session_enable (void)
577{
578 int rv;
579
580 if (vcm->bapi_app_state != STATE_APP_ENABLED)
581 {
582 vcl_bapi_send_session_enable_disable (1 /* is_enabled == TRUE */ );
583 rv = vcl_bapi_wait_for_app_state_change (STATE_APP_ENABLED);
584 if (PREDICT_FALSE (rv))
585 {
586 VDBG (0, "application session enable timed out! returning %d (%s)",
587 rv, vppcom_retval_str (rv));
588 return rv;
589 }
590 }
591 return VPPCOM_OK;
592}
593
594static int
595vcl_bapi_init (void)
596{
597 int rv;
598
599 vcm->bapi_app_state = STATE_APP_START;
600 vcl_bapi_init_error_string_table ();
601 rv = vcl_bapi_connect_to_vpp ();
602 if (rv)
603 {
604 VERR ("couldn't connect to VPP!");
605 return rv;
606 }
607 VDBG (0, "sending session enable");
608 rv = vcl_bapi_session_enable ();
609 if (rv)
610 {
611 VERR ("vppcom_app_session_enable() failed!");
612 return rv;
613 }
614
615 return 0;
616}
617
618int
619vcl_bapi_attach (void)
620{
621 int rv;
622
623 /* API hookup and connect to VPP */
624 if ((rv = vcl_bapi_init ()))
625 return rv;
626
627 vcl_bapi_send_attach ();
628 rv = vcl_bapi_wait_for_app_state_change (STATE_APP_ATTACHED);
629 if (PREDICT_FALSE (rv))
630 {
631 VDBG (0, "application attach timed out! returning %d (%s)", rv,
632 vppcom_retval_str (rv));
633 return rv;
634 }
635
636 return 0;
637}
638
639int
640vcl_bapi_app_worker_add (void)
641{
642 if (vcl_bapi_connect_to_vpp ())
643 return -1;
644
645 vcm->bapi_app_state = STATE_APP_ADDING_WORKER;
646 vcl_bapi_send_app_worker_add_del (1 /* is_add */ );
647 if (vcl_bapi_wait_for_app_state_change (STATE_APP_READY))
648 return -1;
649 return 0;
650}
651
652void
653vcl_bapi_app_worker_del (vcl_worker_t * wrk)
654{
655 /* Notify vpp that the worker is going away */
656 if (wrk->wrk_index == vcl_get_worker_index ())
657 vcl_bapi_send_app_worker_add_del (0 /* is_add */ );
658 else
659 vcl_bapi_send_child_worker_del (wrk);
660
661 /* Disconnect the binary api */
662 if (vec_len (vcm->workers) == 1)
663 vcl_bapi_disconnect_from_vpp ();
664 else
665 vl_client_send_disconnect (1 /* vpp should cleanup */ );
666}
667
668int
Florin Coras935ce752020-09-08 22:43:47 -0700669vcl_bapi_recv_fds (vcl_worker_t * wrk, int *fds, int n_fds)
670{
671 clib_error_t *err;
672
673 if ((err = vl_socket_client_recv_fd_msg2 (&wrk->bapi_sock_ctx, fds, n_fds,
674 5)))
675 {
676 clib_error_report (err);
677 return -1;
678 }
679
680 return 0;
681}
682
683int
Florin Corasb88de902020-09-08 16:47:57 -0700684vppcom_session_tls_add_cert (uint32_t session_handle, char *cert,
685 uint32_t cert_len)
686{
687
688 vcl_worker_t *wrk = vcl_worker_get_current ();
689 vcl_session_t *session = 0;
690
691 session = vcl_session_get_w_handle (wrk, session_handle);
692 if (!session)
693 return VPPCOM_EBADFD;
694
695 if (cert_len == 0 || cert_len == ~0)
696 return VPPCOM_EBADFD;
697
698 /*
699 * Send listen request to vpp and wait for reply
700 */
701 vcl_bapi_send_application_tls_cert_add (session, cert, cert_len);
702 vcm->bapi_app_state = STATE_APP_ADDING_TLS_DATA;
703 vcl_bapi_wait_for_app_state_change (STATE_APP_READY);
704 return VPPCOM_OK;
705}
706
707int
708vppcom_session_tls_add_key (uint32_t session_handle, char *key,
709 uint32_t key_len)
710{
711
712 vcl_worker_t *wrk = vcl_worker_get_current ();
713 vcl_session_t *session = 0;
714
715 session = vcl_session_get_w_handle (wrk, session_handle);
716 if (!session)
717 return VPPCOM_EBADFD;
718
719 if (key_len == 0 || key_len == ~0)
720 return VPPCOM_EBADFD;
721
722 vcl_bapi_send_application_tls_key_add (session, key, key_len);
723 vcm->bapi_app_state = STATE_APP_ADDING_TLS_DATA;
724 vcl_bapi_wait_for_app_state_change (STATE_APP_READY);
725 return VPPCOM_OK;
726}
727
728int
729vcl_bapi_worker_set (void)
730{
731 vcl_worker_t *wrk = vcl_worker_get_current ();
732 int i;
733
734 /* Find the first worker with the same pid */
735 for (i = 0; i < vec_len (vcm->workers); i++)
736 {
737 if (i == wrk->wrk_index)
738 continue;
739 if (vcm->workers[i].current_pid == wrk->current_pid)
740 {
741 wrk->vl_input_queue = vcm->workers[i].vl_input_queue;
Florin Corascc7c88e2020-09-15 15:56:51 -0700742 wrk->api_client_handle = vcm->workers[i].api_client_handle;
Florin Corasb88de902020-09-08 16:47:57 -0700743 return 0;
744 }
745 }
746 return -1;
747}
748
Florin Coras697faea2018-06-27 17:10:49 -0700749/*
750 * fd.io coding-style-patch-verification: ON
751 *
752 * Local Variables:
753 * eval: (c-set-style "gnu")
754 * End:
755 */