blob: f160786e262bf90f6ef707e4bb6a0023dcbf9ac2 [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
59 wrk->bapi_client_index = mp->api_client_handle;
60 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 ();
204 wrk->vpp_wrk_index = mp->wrk_index;
205 wrk->app_event_queue = uword_to_pointer (mp->app_event_queue_address,
206 svm_msg_q_t *);
207 wrk->ctrl_mq = vcm->ctrl_mq;
208
209 segment_handle = mp->segment_handle;
210 if (segment_handle == VCL_INVALID_SEGMENT_HANDLE)
211 {
212 clib_warning ("invalid segment handle");
213 goto failed;
214 }
215
216 if (!mp->n_fds)
217 goto failed;
218
219 if (mp->fd_flags & SESSION_FD_F_VPP_MQ_SEGMENT)
220 if (vcl_segment_attach (vcl_vpp_worker_segment_handle (wrk->wrk_index),
221 "vpp-worker-seg", SSVM_SEGMENT_MEMFD,
222 fds[n_fds++]))
223 goto failed;
224
225 if (mp->fd_flags & SESSION_FD_F_MEMFD_SEGMENT)
226 {
227 u8 *segment_name = format (0, "memfd-%ld%c", segment_handle, 0);
228 rv = vcl_segment_attach (segment_handle, (char *) segment_name,
229 SSVM_SEGMENT_MEMFD, fds[n_fds++]);
230 vec_free (segment_name);
231 if (rv != 0)
232 goto failed;
233 }
234
235 if (mp->fd_flags & SESSION_FD_F_MQ_EVENTFD)
236 {
237 svm_msg_q_set_consumer_eventfd (wrk->app_event_queue, fds[n_fds]);
238 vcl_mq_epoll_add_evfd (wrk, wrk->app_event_queue);
239 n_fds++;
240 }
241
242 VDBG (0, "worker %u vpp-worker %u added", wrk->wrk_index,
243 wrk->vpp_wrk_index);
244
245 return 0;
246
247failed:
248 for (i = clib_max (n_fds - 1, 0); i < mp->n_fds; i++)
249 close (fds[i]);
250
251 return -1;
252}
253
254int
255vcl_sapi_app_worker_add (void)
256{
257 vcl_worker_t *wrk = vcl_worker_get_current ();
258 app_sapi_worker_add_del_msg_t *mp;
259 app_sapi_msg_t _rmp, *rmp = &_rmp;
260 app_sapi_msg_t msg = { 0 };
261 int fds[SESSION_N_FD_TYPE];
262 clib_error_t *err;
263 clib_socket_t *cs;
264
265 /* Connect to socket api */
266 if (vcl_api_connect_app_socket (wrk))
267 return -1;
268
269 /*
270 * Send add worker
271 */
272 cs = &wrk->app_api_sock;
273
274 msg.type = APP_SAPI_MSG_TYPE_ADD_DEL_WORKER;
275 mp = &msg.worker_add_del;
276 mp->app_index = vcm->app_index;
277 mp->is_add = 1;
278
279 err = clib_socket_sendmsg (cs, &msg, sizeof (msg), 0, 0);
280 if (err)
281 {
282 clib_error_report (err);
283 return -1;
284 }
285
286 /*
287 * Wait for reply and process it
288 */
289 err = clib_socket_recvmsg (cs, rmp, sizeof (*rmp), fds, ARRAY_LEN (fds));
290 if (err)
291 {
292 clib_error_report (err);
293 return -1;
294 }
295
296 if (rmp->type != APP_SAPI_MSG_TYPE_ADD_DEL_WORKER_REPLY)
297 {
298 clib_warning ("unexpected reply type %u", rmp->type);
299 return -1;
300 }
301
302 return vcl_api_add_del_worker_reply_handler (&rmp->worker_add_del_reply,
303 fds);
304}
305
306void
307vcl_sapi_app_worker_del (vcl_worker_t * wrk)
308{
309 app_sapi_worker_add_del_msg_t *mp;
310 app_sapi_msg_t msg = { 0 };
311 clib_error_t *err;
312 clib_socket_t *cs;
313
314 cs = &wrk->app_api_sock;
315
316 msg.type = APP_SAPI_MSG_TYPE_ADD_DEL_WORKER;
317 mp = &msg.worker_add_del;
318 mp->app_index = vcm->app_index;
319 mp->wrk_index = wrk->vpp_wrk_index;
320 mp->is_add = 0;
321
322 err = clib_socket_sendmsg (cs, &msg, sizeof (msg), 0, 0);
323 if (err)
324 clib_error_report (err);
325 clib_socket_close (cs);
326}
327
328void
329vcl_sapi_detach (vcl_worker_t * wrk)
330{
331 clib_socket_t *cs = &wrk->app_api_sock;
332 clib_socket_close (cs);
333}
334
335int
336vcl_sapi_recv_fds (vcl_worker_t * wrk, int *fds, int n_fds)
337{
338 app_sapi_msg_t _msg, *msg = &_msg;
339 clib_socket_t *cs;
340 clib_error_t *err;
341
342 cs = &wrk->app_api_sock;
343
344 err = clib_socket_recvmsg (cs, msg, sizeof (*msg), fds, n_fds);
345 if (err)
346 {
347 clib_error_report (err);
348 return -1;
349 }
350 if (msg->type != APP_SAPI_MSG_TYPE_SEND_FDS)
351 return -1;
352
353 return 0;
354}
355
356/*
357 * fd.io coding-style-patch-verification: ON
358 *
359 * Local Variables:
360 * eval: (c-set-style "gnu")
361 * End:
362 */