blob: 9258f7927a0e9d920a57272c81dce2416c7d79f8 [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;
26 cs->flags = CLIB_SOCKET_F_IS_CLIENT | CLIB_SOCKET_F_SEQPACKET;
27
28 wrk->vcl_needs_real_epoll = 1;
29
30 if ((err = clib_socket_init (cs)))
31 {
32 clib_error_report (err);
33 rv = -1;
34 goto done;
35 }
36
37done:
38
39 wrk->vcl_needs_real_epoll = 0;
40
41 return rv;
42}
43
44static int
45vcl_api_attach_reply_handler (app_sapi_attach_reply_msg_t * mp, int *fds)
46{
47 vcl_worker_t *wrk = vcl_worker_get_current ();
48 int i, rv, n_fds_used = 0;
49 svm_msg_q_t *ctrl_mq;
50 u64 segment_handle;
51 u8 *segment_name;
52
53 if (mp->retval)
54 {
55 VERR ("attach failed: %U", format_session_error, mp->retval);
56 goto failed;
57 }
58
Florin Corascc7c88e2020-09-15 15:56:51 -070059 wrk->api_client_handle = mp->api_client_handle;
Florin Coras935ce752020-09-08 22:43:47 -070060 wrk->app_event_queue = uword_to_pointer (mp->app_mq, svm_msg_q_t *);
61 ctrl_mq = uword_to_pointer (mp->vpp_ctrl_mq, svm_msg_q_t *);
62 vec_validate (wrk->vpp_event_queues, mp->vpp_ctrl_mq_thread);
63 wrk->vpp_event_queues[mp->vpp_ctrl_mq_thread] = ctrl_mq;
64 vcm->ctrl_mq = wrk->ctrl_mq = ctrl_mq;
65 segment_handle = mp->segment_handle;
66 if (segment_handle == VCL_INVALID_SEGMENT_HANDLE)
67 {
68 VERR ("invalid segment handle");
69 goto failed;
70 }
71
72 if (!mp->n_fds)
73 goto failed;
74
75 if (mp->fd_flags & SESSION_FD_F_VPP_MQ_SEGMENT)
76 if (vcl_segment_attach (vcl_vpp_worker_segment_handle (0), "vpp-mq-seg",
77 SSVM_SEGMENT_MEMFD, fds[n_fds_used++]))
78 goto failed;
79
80 if (mp->fd_flags & SESSION_FD_F_MEMFD_SEGMENT)
81 {
82 segment_name = format (0, "memfd-%ld%c", segment_handle, 0);
83 rv = vcl_segment_attach (segment_handle, (char *) segment_name,
84 SSVM_SEGMENT_MEMFD, fds[n_fds_used++]);
85 vec_free (segment_name);
86 if (rv != 0)
87 goto failed;
88 }
89
90 if (mp->fd_flags & SESSION_FD_F_MQ_EVENTFD)
91 {
92 svm_msg_q_set_consumer_eventfd (wrk->app_event_queue,
93 fds[n_fds_used++]);
94 vcl_mq_epoll_add_evfd (wrk, wrk->app_event_queue);
95 }
96
97 vcm->app_index = mp->app_index;
98
99 return 0;
100
101failed:
102
103 for (i = clib_max (n_fds_used - 1, 0); i < mp->n_fds; i++)
104 close (fds[i]);
105
106 return -1;
107}
108
109static int
110vcl_api_send_attach (clib_socket_t * cs)
111{
112 app_sapi_msg_t msg = { 0 };
113 app_sapi_attach_msg_t *mp = &msg.attach;
114 u8 app_is_proxy, tls_engine;
115 clib_error_t *err;
116
117 app_is_proxy = (vcm->cfg.app_proxy_transport_tcp ||
118 vcm->cfg.app_proxy_transport_udp);
119 tls_engine = CRYPTO_ENGINE_OPENSSL;
120
121 clib_memcpy (&mp->name, vcm->app_name, vec_len (vcm->app_name));
122 mp->options[APP_OPTIONS_FLAGS] =
123 APP_OPTIONS_FLAGS_ACCEPT_REDIRECT | APP_OPTIONS_FLAGS_ADD_SEGMENT |
124 (vcm->cfg.app_scope_local ? APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE : 0) |
125 (vcm->cfg.app_scope_global ? APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE : 0) |
126 (app_is_proxy ? APP_OPTIONS_FLAGS_IS_PROXY : 0) |
127 (vcm->cfg.use_mq_eventfd ? APP_OPTIONS_FLAGS_EVT_MQ_USE_EVENTFD : 0);
128 mp->options[APP_OPTIONS_PROXY_TRANSPORT] =
129 (u64) ((vcm->cfg.app_proxy_transport_tcp ? 1 << TRANSPORT_PROTO_TCP : 0) |
130 (vcm->cfg.app_proxy_transport_udp ? 1 << TRANSPORT_PROTO_UDP : 0));
131 mp->options[APP_OPTIONS_SEGMENT_SIZE] = vcm->cfg.segment_size;
132 mp->options[APP_OPTIONS_ADD_SEGMENT_SIZE] = vcm->cfg.add_segment_size;
133 mp->options[APP_OPTIONS_RX_FIFO_SIZE] = vcm->cfg.rx_fifo_size;
134 mp->options[APP_OPTIONS_TX_FIFO_SIZE] = vcm->cfg.tx_fifo_size;
135 mp->options[APP_OPTIONS_PREALLOC_FIFO_PAIRS] =
136 vcm->cfg.preallocated_fifo_pairs;
137 mp->options[APP_OPTIONS_EVT_QUEUE_SIZE] = vcm->cfg.event_queue_size;
138 mp->options[APP_OPTIONS_TLS_ENGINE] = tls_engine;
139
140 msg.type = APP_SAPI_MSG_TYPE_ATTACH;
141 err = clib_socket_sendmsg (cs, &msg, sizeof (msg), 0, 0);
142 if (err)
143 {
144 clib_error_report (err);
145 return -1;
146 }
147
148 return 0;
149}
150
151int
152vcl_sapi_attach (void)
153{
154 vcl_worker_t *wrk = vcl_worker_get_current ();
155 app_sapi_msg_t _rmp, *rmp = &_rmp;
156 clib_error_t *err;
157 clib_socket_t *cs;
158 int fds[SESSION_N_FD_TYPE];
159
160 /*
161 * Init client socket and send attach
162 */
163 if (vcl_api_connect_app_socket (wrk))
164 return -1;
165
166 cs = &wrk->app_api_sock;
167 if (vcl_api_send_attach (cs))
168 return -1;
169
170 /*
171 * Wait for attach reply
172 */
173 err = clib_socket_recvmsg (cs, rmp, sizeof (*rmp), fds, ARRAY_LEN (fds));
174 if (err)
175 {
176 clib_error_report (err);
177 return -1;
178 }
179
180 if (rmp->type != APP_SAPI_MSG_TYPE_ATTACH_REPLY)
181 return -1;
182
183 return vcl_api_attach_reply_handler (&rmp->attach_reply, fds);
184}
185
186static int
187vcl_api_add_del_worker_reply_handler (app_sapi_worker_add_del_reply_msg_t *
188 mp, int *fds)
189{
190 int n_fds = 0, i, rv;
191 u64 segment_handle;
192 vcl_worker_t *wrk;
193
194 if (mp->retval)
195 {
196 VDBG (0, "add/del worker failed: %U", format_session_error, mp->retval);
197 goto failed;
198 }
199
200 if (!mp->is_add)
201 goto failed;
202
203 wrk = vcl_worker_get_current ();
Florin Corascc7c88e2020-09-15 15:56:51 -0700204 wrk->api_client_handle = mp->api_client_handle;
Florin Coras935ce752020-09-08 22:43:47 -0700205 wrk->vpp_wrk_index = mp->wrk_index;
206 wrk->app_event_queue = uword_to_pointer (mp->app_event_queue_address,
207 svm_msg_q_t *);
208 wrk->ctrl_mq = vcm->ctrl_mq;
209
210 segment_handle = mp->segment_handle;
211 if (segment_handle == VCL_INVALID_SEGMENT_HANDLE)
212 {
213 clib_warning ("invalid segment handle");
214 goto failed;
215 }
216
217 if (!mp->n_fds)
218 goto failed;
219
220 if (mp->fd_flags & SESSION_FD_F_VPP_MQ_SEGMENT)
221 if (vcl_segment_attach (vcl_vpp_worker_segment_handle (wrk->wrk_index),
222 "vpp-worker-seg", SSVM_SEGMENT_MEMFD,
223 fds[n_fds++]))
224 goto failed;
225
226 if (mp->fd_flags & SESSION_FD_F_MEMFD_SEGMENT)
227 {
228 u8 *segment_name = format (0, "memfd-%ld%c", segment_handle, 0);
229 rv = vcl_segment_attach (segment_handle, (char *) segment_name,
230 SSVM_SEGMENT_MEMFD, fds[n_fds++]);
231 vec_free (segment_name);
232 if (rv != 0)
233 goto failed;
234 }
235
236 if (mp->fd_flags & SESSION_FD_F_MQ_EVENTFD)
237 {
238 svm_msg_q_set_consumer_eventfd (wrk->app_event_queue, fds[n_fds]);
239 vcl_mq_epoll_add_evfd (wrk, wrk->app_event_queue);
240 n_fds++;
241 }
242
243 VDBG (0, "worker %u vpp-worker %u added", wrk->wrk_index,
244 wrk->vpp_wrk_index);
245
246 return 0;
247
248failed:
249 for (i = clib_max (n_fds - 1, 0); i < mp->n_fds; i++)
250 close (fds[i]);
251
252 return -1;
253}
254
255int
256vcl_sapi_app_worker_add (void)
257{
258 vcl_worker_t *wrk = vcl_worker_get_current ();
259 app_sapi_worker_add_del_msg_t *mp;
260 app_sapi_msg_t _rmp, *rmp = &_rmp;
261 app_sapi_msg_t msg = { 0 };
262 int fds[SESSION_N_FD_TYPE];
263 clib_error_t *err;
264 clib_socket_t *cs;
265
266 /* Connect to socket api */
267 if (vcl_api_connect_app_socket (wrk))
268 return -1;
269
270 /*
271 * Send add worker
272 */
273 cs = &wrk->app_api_sock;
274
275 msg.type = APP_SAPI_MSG_TYPE_ADD_DEL_WORKER;
276 mp = &msg.worker_add_del;
277 mp->app_index = vcm->app_index;
278 mp->is_add = 1;
279
280 err = clib_socket_sendmsg (cs, &msg, sizeof (msg), 0, 0);
281 if (err)
282 {
283 clib_error_report (err);
284 return -1;
285 }
286
287 /*
288 * Wait for reply and process it
289 */
290 err = clib_socket_recvmsg (cs, rmp, sizeof (*rmp), fds, ARRAY_LEN (fds));
291 if (err)
292 {
293 clib_error_report (err);
294 return -1;
295 }
296
297 if (rmp->type != APP_SAPI_MSG_TYPE_ADD_DEL_WORKER_REPLY)
298 {
299 clib_warning ("unexpected reply type %u", rmp->type);
300 return -1;
301 }
302
303 return vcl_api_add_del_worker_reply_handler (&rmp->worker_add_del_reply,
304 fds);
305}
306
307void
308vcl_sapi_app_worker_del (vcl_worker_t * wrk)
309{
310 app_sapi_worker_add_del_msg_t *mp;
311 app_sapi_msg_t msg = { 0 };
312 clib_error_t *err;
313 clib_socket_t *cs;
314
315 cs = &wrk->app_api_sock;
316
317 msg.type = APP_SAPI_MSG_TYPE_ADD_DEL_WORKER;
318 mp = &msg.worker_add_del;
319 mp->app_index = vcm->app_index;
320 mp->wrk_index = wrk->vpp_wrk_index;
321 mp->is_add = 0;
322
323 err = clib_socket_sendmsg (cs, &msg, sizeof (msg), 0, 0);
324 if (err)
325 clib_error_report (err);
326 clib_socket_close (cs);
327}
328
329void
330vcl_sapi_detach (vcl_worker_t * wrk)
331{
332 clib_socket_t *cs = &wrk->app_api_sock;
333 clib_socket_close (cs);
334}
335
336int
337vcl_sapi_recv_fds (vcl_worker_t * wrk, int *fds, int n_fds)
338{
339 app_sapi_msg_t _msg, *msg = &_msg;
340 clib_socket_t *cs;
341 clib_error_t *err;
342
343 cs = &wrk->app_api_sock;
344
345 err = clib_socket_recvmsg (cs, msg, sizeof (*msg), fds, n_fds);
346 if (err)
347 {
348 clib_error_report (err);
349 return -1;
350 }
351 if (msg->type != APP_SAPI_MSG_TYPE_SEND_FDS)
352 return -1;
353
354 return 0;
355}
356
357/*
358 * fd.io coding-style-patch-verification: ON
359 *
360 * Local Variables:
361 * eval: (c-set-style "gnu")
362 * End:
363 */