blob: 5f4263b3b13e5fb591e479b1746aa70bf820eb80 [file] [log] [blame]
Florin Coras935ce752020-09-08 22:43:47 -07001/*
2 * Copyright (c) 2020 Cisco and/or its affiliates.
3 * 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
18static int
19vcl_api_connect_app_socket (vcl_worker_t * wrk)
20{
21 clib_socket_t *cs = &wrk->app_api_sock;
22 clib_error_t *err;
23 int rv = 0;
24
25 cs->config = (char *) vcm->cfg.vpp_app_socket_api;
Florin Coras57e0af92021-05-26 10:21:10 -070026 cs->flags =
27 CLIB_SOCKET_F_IS_CLIENT | CLIB_SOCKET_F_SEQPACKET | CLIB_SOCKET_F_BLOCKING;
Florin Coras935ce752020-09-08 22:43:47 -070028
29 wrk->vcl_needs_real_epoll = 1;
30
31 if ((err = clib_socket_init (cs)))
32 {
Filip Tehlar8ccc6b32022-02-02 17:38:20 +000033 /* don't report the error to avoid flood of error messages during
34 * reconnect */
35 clib_error_free (err);
Florin Coras935ce752020-09-08 22:43:47 -070036 rv = -1;
37 goto done;
38 }
39
40done:
41
42 wrk->vcl_needs_real_epoll = 0;
43
44 return rv;
45}
46
47static int
48vcl_api_attach_reply_handler (app_sapi_attach_reply_msg_t * mp, int *fds)
49{
50 vcl_worker_t *wrk = vcl_worker_get_current ();
51 int i, rv, n_fds_used = 0;
Florin Coras935ce752020-09-08 22:43:47 -070052 u64 segment_handle;
53 u8 *segment_name;
54
55 if (mp->retval)
56 {
57 VERR ("attach failed: %U", format_session_error, mp->retval);
58 goto failed;
59 }
60
Florin Corascc7c88e2020-09-15 15:56:51 -070061 wrk->api_client_handle = mp->api_client_handle;
Florin Coras935ce752020-09-08 22:43:47 -070062 segment_handle = mp->segment_handle;
63 if (segment_handle == VCL_INVALID_SEGMENT_HANDLE)
64 {
65 VERR ("invalid segment handle");
66 goto failed;
67 }
68
69 if (!mp->n_fds)
70 goto failed;
71
72 if (mp->fd_flags & SESSION_FD_F_VPP_MQ_SEGMENT)
73 if (vcl_segment_attach (vcl_vpp_worker_segment_handle (0), "vpp-mq-seg",
74 SSVM_SEGMENT_MEMFD, fds[n_fds_used++]))
75 goto failed;
76
77 if (mp->fd_flags & SESSION_FD_F_MEMFD_SEGMENT)
78 {
79 segment_name = format (0, "memfd-%ld%c", segment_handle, 0);
80 rv = vcl_segment_attach (segment_handle, (char *) segment_name,
81 SSVM_SEGMENT_MEMFD, fds[n_fds_used++]);
82 vec_free (segment_name);
83 if (rv != 0)
84 goto failed;
85 }
86
Florin Corasb4624182020-12-11 13:58:12 -080087 vcl_segment_attach_mq (segment_handle, mp->app_mq, 0, &wrk->app_event_queue);
88
Florin Coras935ce752020-09-08 22:43:47 -070089 if (mp->fd_flags & SESSION_FD_F_MQ_EVENTFD)
90 {
Florin Coras86f12322021-01-22 15:05:14 -080091 svm_msg_q_set_eventfd (wrk->app_event_queue, fds[n_fds_used++]);
Florin Coras935ce752020-09-08 22:43:47 -070092 vcl_mq_epoll_add_evfd (wrk, wrk->app_event_queue);
93 }
94
Florin Coras80b74252021-01-27 18:08:25 -080095 vcl_segment_discover_mqs (vcl_vpp_worker_segment_handle (0),
96 fds + n_fds_used, mp->n_fds - n_fds_used);
97 vcl_segment_attach_mq (vcl_vpp_worker_segment_handle (0), mp->vpp_ctrl_mq,
98 mp->vpp_ctrl_mq_thread, &wrk->ctrl_mq);
99 vcm->ctrl_mq = wrk->ctrl_mq;
Florin Coras935ce752020-09-08 22:43:47 -0700100 vcm->app_index = mp->app_index;
101
102 return 0;
103
104failed:
105
106 for (i = clib_max (n_fds_used - 1, 0); i < mp->n_fds; i++)
107 close (fds[i]);
108
109 return -1;
110}
111
112static int
113vcl_api_send_attach (clib_socket_t * cs)
114{
115 app_sapi_msg_t msg = { 0 };
116 app_sapi_attach_msg_t *mp = &msg.attach;
117 u8 app_is_proxy, tls_engine;
118 clib_error_t *err;
119
120 app_is_proxy = (vcm->cfg.app_proxy_transport_tcp ||
121 vcm->cfg.app_proxy_transport_udp);
122 tls_engine = CRYPTO_ENGINE_OPENSSL;
123
124 clib_memcpy (&mp->name, vcm->app_name, vec_len (vcm->app_name));
125 mp->options[APP_OPTIONS_FLAGS] =
126 APP_OPTIONS_FLAGS_ACCEPT_REDIRECT | APP_OPTIONS_FLAGS_ADD_SEGMENT |
127 (vcm->cfg.app_scope_local ? APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE : 0) |
128 (vcm->cfg.app_scope_global ? APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE : 0) |
129 (app_is_proxy ? APP_OPTIONS_FLAGS_IS_PROXY : 0) |
130 (vcm->cfg.use_mq_eventfd ? APP_OPTIONS_FLAGS_EVT_MQ_USE_EVENTFD : 0);
131 mp->options[APP_OPTIONS_PROXY_TRANSPORT] =
132 (u64) ((vcm->cfg.app_proxy_transport_tcp ? 1 << TRANSPORT_PROTO_TCP : 0) |
133 (vcm->cfg.app_proxy_transport_udp ? 1 << TRANSPORT_PROTO_UDP : 0));
134 mp->options[APP_OPTIONS_SEGMENT_SIZE] = vcm->cfg.segment_size;
135 mp->options[APP_OPTIONS_ADD_SEGMENT_SIZE] = vcm->cfg.add_segment_size;
136 mp->options[APP_OPTIONS_RX_FIFO_SIZE] = vcm->cfg.rx_fifo_size;
137 mp->options[APP_OPTIONS_TX_FIFO_SIZE] = vcm->cfg.tx_fifo_size;
138 mp->options[APP_OPTIONS_PREALLOC_FIFO_PAIRS] =
139 vcm->cfg.preallocated_fifo_pairs;
140 mp->options[APP_OPTIONS_EVT_QUEUE_SIZE] = vcm->cfg.event_queue_size;
141 mp->options[APP_OPTIONS_TLS_ENGINE] = tls_engine;
142
143 msg.type = APP_SAPI_MSG_TYPE_ATTACH;
144 err = clib_socket_sendmsg (cs, &msg, sizeof (msg), 0, 0);
145 if (err)
146 {
147 clib_error_report (err);
148 return -1;
149 }
150
151 return 0;
152}
153
154int
155vcl_sapi_attach (void)
156{
157 vcl_worker_t *wrk = vcl_worker_get_current ();
158 app_sapi_msg_t _rmp, *rmp = &_rmp;
159 clib_error_t *err;
160 clib_socket_t *cs;
Florin Coras41d5f542021-01-15 13:49:33 -0800161 int fds[32];
Florin Coras935ce752020-09-08 22:43:47 -0700162
163 /*
164 * Init client socket and send attach
165 */
166 if (vcl_api_connect_app_socket (wrk))
167 return -1;
168
169 cs = &wrk->app_api_sock;
170 if (vcl_api_send_attach (cs))
171 return -1;
172
173 /*
174 * Wait for attach reply
175 */
176 err = clib_socket_recvmsg (cs, rmp, sizeof (*rmp), fds, ARRAY_LEN (fds));
177 if (err)
178 {
179 clib_error_report (err);
180 return -1;
181 }
182
183 if (rmp->type != APP_SAPI_MSG_TYPE_ATTACH_REPLY)
184 return -1;
185
186 return vcl_api_attach_reply_handler (&rmp->attach_reply, fds);
187}
188
189static int
190vcl_api_add_del_worker_reply_handler (app_sapi_worker_add_del_reply_msg_t *
191 mp, int *fds)
192{
193 int n_fds = 0, i, rv;
194 u64 segment_handle;
195 vcl_worker_t *wrk;
196
197 if (mp->retval)
198 {
199 VDBG (0, "add/del worker failed: %U", format_session_error, mp->retval);
200 goto failed;
201 }
202
203 if (!mp->is_add)
204 goto failed;
205
206 wrk = vcl_worker_get_current ();
Florin Corascc7c88e2020-09-15 15:56:51 -0700207 wrk->api_client_handle = mp->api_client_handle;
Florin Coras935ce752020-09-08 22:43:47 -0700208 wrk->vpp_wrk_index = mp->wrk_index;
Florin Coras935ce752020-09-08 22:43:47 -0700209 wrk->ctrl_mq = vcm->ctrl_mq;
210
211 segment_handle = mp->segment_handle;
212 if (segment_handle == VCL_INVALID_SEGMENT_HANDLE)
213 {
214 clib_warning ("invalid segment handle");
215 goto failed;
216 }
217
218 if (!mp->n_fds)
219 goto failed;
220
221 if (mp->fd_flags & SESSION_FD_F_VPP_MQ_SEGMENT)
222 if (vcl_segment_attach (vcl_vpp_worker_segment_handle (wrk->wrk_index),
223 "vpp-worker-seg", SSVM_SEGMENT_MEMFD,
224 fds[n_fds++]))
225 goto failed;
226
227 if (mp->fd_flags & SESSION_FD_F_MEMFD_SEGMENT)
228 {
229 u8 *segment_name = format (0, "memfd-%ld%c", segment_handle, 0);
230 rv = vcl_segment_attach (segment_handle, (char *) segment_name,
231 SSVM_SEGMENT_MEMFD, fds[n_fds++]);
232 vec_free (segment_name);
233 if (rv != 0)
234 goto failed;
235 }
236
Florin Corasb4624182020-12-11 13:58:12 -0800237 vcl_segment_attach_mq (segment_handle, mp->app_event_queue_address, 0,
238 &wrk->app_event_queue);
239
Florin Coras935ce752020-09-08 22:43:47 -0700240 if (mp->fd_flags & SESSION_FD_F_MQ_EVENTFD)
241 {
Florin Coras86f12322021-01-22 15:05:14 -0800242 svm_msg_q_set_eventfd (wrk->app_event_queue, fds[n_fds]);
Florin Coras935ce752020-09-08 22:43:47 -0700243 vcl_mq_epoll_add_evfd (wrk, wrk->app_event_queue);
244 n_fds++;
245 }
246
247 VDBG (0, "worker %u vpp-worker %u added", wrk->wrk_index,
248 wrk->vpp_wrk_index);
249
250 return 0;
251
252failed:
253 for (i = clib_max (n_fds - 1, 0); i < mp->n_fds; i++)
254 close (fds[i]);
255
256 return -1;
257}
258
259int
260vcl_sapi_app_worker_add (void)
261{
262 vcl_worker_t *wrk = vcl_worker_get_current ();
263 app_sapi_worker_add_del_msg_t *mp;
264 app_sapi_msg_t _rmp, *rmp = &_rmp;
265 app_sapi_msg_t msg = { 0 };
266 int fds[SESSION_N_FD_TYPE];
267 clib_error_t *err;
268 clib_socket_t *cs;
269
270 /* Connect to socket api */
271 if (vcl_api_connect_app_socket (wrk))
272 return -1;
273
274 /*
275 * Send add worker
276 */
277 cs = &wrk->app_api_sock;
278
279 msg.type = APP_SAPI_MSG_TYPE_ADD_DEL_WORKER;
280 mp = &msg.worker_add_del;
281 mp->app_index = vcm->app_index;
282 mp->is_add = 1;
283
284 err = clib_socket_sendmsg (cs, &msg, sizeof (msg), 0, 0);
285 if (err)
286 {
287 clib_error_report (err);
288 return -1;
289 }
290
291 /*
292 * Wait for reply and process it
293 */
294 err = clib_socket_recvmsg (cs, rmp, sizeof (*rmp), fds, ARRAY_LEN (fds));
295 if (err)
296 {
297 clib_error_report (err);
298 return -1;
299 }
300
301 if (rmp->type != APP_SAPI_MSG_TYPE_ADD_DEL_WORKER_REPLY)
302 {
303 clib_warning ("unexpected reply type %u", rmp->type);
304 return -1;
305 }
306
307 return vcl_api_add_del_worker_reply_handler (&rmp->worker_add_del_reply,
308 fds);
309}
310
311void
312vcl_sapi_app_worker_del (vcl_worker_t * wrk)
313{
314 app_sapi_worker_add_del_msg_t *mp;
315 app_sapi_msg_t msg = { 0 };
316 clib_error_t *err;
317 clib_socket_t *cs;
318
319 cs = &wrk->app_api_sock;
320
321 msg.type = APP_SAPI_MSG_TYPE_ADD_DEL_WORKER;
322 mp = &msg.worker_add_del;
323 mp->app_index = vcm->app_index;
324 mp->wrk_index = wrk->vpp_wrk_index;
325 mp->is_add = 0;
326
327 err = clib_socket_sendmsg (cs, &msg, sizeof (msg), 0, 0);
328 if (err)
329 clib_error_report (err);
330 clib_socket_close (cs);
331}
332
333void
334vcl_sapi_detach (vcl_worker_t * wrk)
335{
336 clib_socket_t *cs = &wrk->app_api_sock;
337 clib_socket_close (cs);
338}
339
340int
341vcl_sapi_recv_fds (vcl_worker_t * wrk, int *fds, int n_fds)
342{
343 app_sapi_msg_t _msg, *msg = &_msg;
344 clib_socket_t *cs;
345 clib_error_t *err;
346
347 cs = &wrk->app_api_sock;
348
349 err = clib_socket_recvmsg (cs, msg, sizeof (*msg), fds, n_fds);
350 if (err)
351 {
352 clib_error_report (err);
353 return -1;
354 }
355 if (msg->type != APP_SAPI_MSG_TYPE_SEND_FDS)
356 return -1;
357
358 return 0;
359}
360
Florin Corase191d762021-08-11 14:55:49 -0700361int
362vcl_sapi_add_cert_key_pair (vppcom_cert_key_pair_t *ckpair)
363{
364 u32 cert_len = ckpair->cert_len, key_len = ckpair->key_len, certkey_len;
365 vcl_worker_t *wrk = vcl_worker_get_current ();
366 app_sapi_msg_t _msg = { 0 }, *msg = &_msg;
367 app_sapi_cert_key_add_del_msg_t *mp;
368 app_sapi_msg_t _rmp, *rmp = &_rmp;
369 clib_error_t *err;
370 clib_socket_t *cs;
371 u8 *certkey = 0;
372 int rv = -1;
373
374 msg->type = APP_SAPI_MSG_TYPE_ADD_DEL_CERT_KEY;
375 mp = &msg->cert_key_add_del;
376 mp->context = wrk->wrk_index;
377 mp->cert_len = cert_len;
378 mp->certkey_len = cert_len + key_len;
379 mp->is_add = 1;
380
381 certkey_len = cert_len + key_len;
382 vec_validate (certkey, certkey_len - 1);
383 clib_memcpy_fast (certkey, ckpair->cert, cert_len);
384 clib_memcpy_fast (certkey + cert_len, ckpair->key, key_len);
385
386 cs = &wrk->app_api_sock;
387 err = clib_socket_sendmsg (cs, msg, sizeof (*msg), 0, 0);
388 if (err)
389 {
390 clib_error_report (err);
391 goto done;
392 }
393
394 err = clib_socket_sendmsg (cs, certkey, certkey_len, 0, 0);
395 if (err)
396 {
397 clib_error_report (err);
398 goto done;
399 }
400
401 /*
402 * Wait for reply and process it
403 */
404 err = clib_socket_recvmsg (cs, rmp, sizeof (*rmp), 0, 0);
405 if (err)
406 {
407 clib_error_report (err);
408 goto done;
409 }
410
411 if (rmp->type != APP_SAPI_MSG_TYPE_ADD_DEL_CERT_KEY_REPLY)
412 {
413 clib_warning ("unexpected reply type %u", rmp->type);
414 goto done;
415 }
416
417 if (!rmp->cert_key_add_del_reply.retval)
418 rv = rmp->cert_key_add_del_reply.index;
419
420done:
421
422 return rv;
423}
424
425int
426vcl_sapi_del_cert_key_pair (u32 ckpair_index)
427{
428 vcl_worker_t *wrk = vcl_worker_get_current ();
429 app_sapi_msg_t _msg = { 0 }, *msg = &_msg;
430 app_sapi_cert_key_add_del_msg_t *mp;
431 app_sapi_msg_t _rmp, *rmp = &_rmp;
432 clib_error_t *err;
433 clib_socket_t *cs;
434
435 msg->type = APP_SAPI_MSG_TYPE_ADD_DEL_CERT_KEY;
436 mp = &msg->cert_key_add_del;
437 mp->context = wrk->wrk_index;
438 mp->index = ckpair_index;
439
440 cs = &wrk->app_api_sock;
Filip Tehlareeb31ec2022-03-17 17:25:47 +0000441 err = clib_socket_sendmsg (cs, msg, sizeof (*msg), 0, 0);
Florin Corase191d762021-08-11 14:55:49 -0700442 if (err)
443 {
444 clib_error_report (err);
445 return -1;
446 }
447
448 /*
449 * Wait for reply and process it
450 */
451 err = clib_socket_recvmsg (cs, rmp, sizeof (*rmp), 0, 0);
452 if (err)
453 {
454 clib_error_report (err);
455 return -1;
456 }
457
458 if (rmp->type != APP_SAPI_MSG_TYPE_ADD_DEL_CERT_KEY_REPLY)
459 {
460 clib_warning ("unexpected reply type %u", rmp->type);
461 return -1;
462 }
463
464 if (rmp->cert_key_add_del_reply.retval)
465 return -1;
466
467 return 0;
468}
469
Florin Coras935ce752020-09-08 22:43:47 -0700470/*
471 * fd.io coding-style-patch-verification: ON
472 *
473 * Local Variables:
474 * eval: (c-set-style "gnu")
475 * End:
476 */