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