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