blob: afe02d205368300044a06d31b182fc5345ae2d8b [file] [log] [blame]
Dave Barach59b25652017-09-10 15:04:27 -04001/*
2 *------------------------------------------------------------------
3 * socksvr_vlib.c
4 *
5 * Copyright (c) 2009 Cisco and/or its affiliates.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at:
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *------------------------------------------------------------------
18 */
19
20#include <sys/types.h>
21#include <sys/socket.h>
22#include <netinet/in.h>
23#include <sys/ioctl.h>
Dave Barach59b25652017-09-10 15:04:27 -040024#include <fcntl.h>
25#include <sys/stat.h>
26
Florin Corase86a8ed2018-01-05 03:20:25 -080027#include <vppinfra/byte_order.h>
Florin Coras4d9b9d82018-01-14 12:25:50 -080028#include <svm/ssvm.h>
Dave Barach59b25652017-09-10 15:04:27 -040029#include <vlibmemory/api.h>
30
31#include <vlibmemory/vl_memory_msg_enum.h>
32
33#define vl_typedefs /* define message structures */
34#include <vlibmemory/vl_memory_api_h.h>
35#undef vl_typedefs
36
37/* instantiate all the print functions we know about */
38#define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
39#define vl_printfun
40#include <vlibmemory/vl_memory_api_h.h>
41#undef vl_printfun
42
43/* instantiate all the endian swap functions we know about */
44#define vl_endianfun
45#include <vlibmemory/vl_memory_api_h.h>
46#undef vl_endianfun
47
Florin Corase86a8ed2018-01-05 03:20:25 -080048socket_main_t socket_main;
49
Dave Barach59b25652017-09-10 15:04:27 -040050void
Florin Corase86a8ed2018-01-05 03:20:25 -080051vl_sock_api_dump_clients (vlib_main_t * vm, api_main_t * am)
Dave Barach59b25652017-09-10 15:04:27 -040052{
53 vl_api_registration_t *reg;
54 socket_main_t *sm = &socket_main;
Dave Barach59b25652017-09-10 15:04:27 -040055 clib_file_t *f;
56
57 /*
58 * Must have at least one active client, not counting the
59 * REGISTRATION_TYPE_SOCKET_LISTEN bind/accept socket
60 */
61 if (pool_elts (sm->registration_pool) < 2)
62 return;
63
64 vlib_cli_output (vm, "Socket clients");
Florin Coras90a63982017-12-19 04:50:01 -080065 vlib_cli_output (vm, "%20s %8s", "Name", "Fildesc");
Dave Barach59b25652017-09-10 15:04:27 -040066 /* *INDENT-OFF* */
67 pool_foreach (reg, sm->registration_pool,
68 ({
69 if (reg->registration_type == REGISTRATION_TYPE_SOCKET_SERVER) {
Florin Corasb384b542018-01-15 01:08:33 -080070 f = vl_api_registration_file (reg);
71 vlib_cli_output (vm, "%20s %8d", reg->name, f->file_descriptor);
Dave Barach59b25652017-09-10 15:04:27 -040072 }
73 }));
74/* *INDENT-ON* */
75}
76
77void
78vl_socket_api_send (vl_api_registration_t * rp, u8 * elem)
79{
Dave Barach59b25652017-09-10 15:04:27 -040080#if CLIB_DEBUG > 1
81 u32 output_length;
82#endif
Florin Coras90a63982017-12-19 04:50:01 -080083 socket_main_t *sm = &socket_main;
84 u16 msg_id = ntohs (*(u16 *) elem);
85 api_main_t *am = &api_main;
86 msgbuf_t *mb = (msgbuf_t *) (elem - offsetof (msgbuf_t, data));
Florin Coras90a63982017-12-19 04:50:01 -080087 vl_api_registration_t *sock_rp;
Florin Coras8023ad42018-08-02 12:16:03 -070088 clib_file_main_t *fm = &file_main;
89 clib_error_t *error;
Florin Corasb384b542018-01-15 01:08:33 -080090 clib_file_t *cf;
Dave Barach59b25652017-09-10 15:04:27 -040091
Florin Corasb384b542018-01-15 01:08:33 -080092 cf = vl_api_registration_file (rp);
Dave Barach59b25652017-09-10 15:04:27 -040093 ASSERT (rp->registration_type > REGISTRATION_TYPE_SHMEM);
94
95 if (msg_id >= vec_len (am->api_trace_cfg))
96 {
97 clib_warning ("id out of range: %d", msg_id);
98 vl_msg_api_free ((void *) elem);
99 return;
100 }
101
Florin Coras90a63982017-12-19 04:50:01 -0800102 sock_rp = pool_elt_at_index (sm->registration_pool,
103 rp->vl_api_registration_pool_index);
104 ASSERT (sock_rp);
105
Dave Barach59b25652017-09-10 15:04:27 -0400106 /* Add the msgbuf_t to the output vector */
Florin Coras8023ad42018-08-02 12:16:03 -0700107 vec_add (sock_rp->output_vector, (u8 *) mb, sizeof (*mb));
108
109 /* Try to send the message and save any error like
110 * we do in the input epoll loop */
111 vec_add (sock_rp->output_vector, elem, ntohl (mb->data_len));
112 error = clib_file_write (cf);
113 unix_save_error (&unix_main, error);
114
115 /* If we didn't finish sending everything, wait for tx space */
116 if (vec_len (sock_rp->output_vector) > 0
117 && !(cf->flags & UNIX_FILE_DATA_AVAILABLE_TO_WRITE))
118 {
119 cf->flags |= UNIX_FILE_DATA_AVAILABLE_TO_WRITE;
120 fm->file_update (cf, UNIX_FILE_UPDATE_MODIFY);
121 }
Dave Barach59b25652017-09-10 15:04:27 -0400122
123#if CLIB_DEBUG > 1
124 output_length = sizeof (*mb) + ntohl (mb->data_len);
125 clib_warning ("wrote %u bytes to fd %d", output_length,
126 cf->file_descriptor);
127#endif
128
129 vl_msg_api_free ((void *) elem);
130}
131
132void
Florin Corase86a8ed2018-01-05 03:20:25 -0800133vl_socket_free_registration_index (u32 pool_index)
Dave Barach59b25652017-09-10 15:04:27 -0400134{
135 int i;
136 vl_api_registration_t *rp;
137 if (pool_is_free_index (socket_main.registration_pool, pool_index))
138 {
139 clib_warning ("main pool index %d already free", pool_index);
140 return;
141 }
142 rp = pool_elt_at_index (socket_main.registration_pool, pool_index);
143
144 ASSERT (rp->registration_type != REGISTRATION_TYPE_FREE);
145 for (i = 0; i < vec_len (rp->additional_fds_to_close); i++)
146 if (close (rp->additional_fds_to_close[i]) < 0)
147 clib_unix_warning ("close");
148 vec_free (rp->additional_fds_to_close);
149 vec_free (rp->name);
150 vec_free (rp->unprocessed_input);
151 vec_free (rp->output_vector);
152 rp->registration_type = REGISTRATION_TYPE_FREE;
153 pool_put (socket_main.registration_pool, rp);
154}
155
156void
Florin Corase86a8ed2018-01-05 03:20:25 -0800157vl_socket_process_api_msg (clib_file_t * uf, vl_api_registration_t * rp,
Dave Barach59b25652017-09-10 15:04:27 -0400158 i8 * input_v)
159{
160 msgbuf_t *mbp = (msgbuf_t *) input_v;
161
162 u8 *the_msg = (u8 *) (mbp->data);
163 socket_main.current_uf = uf;
164 socket_main.current_rp = rp;
165 vl_msg_api_socket_handler (the_msg);
166 socket_main.current_uf = 0;
167 socket_main.current_rp = 0;
168}
169
170clib_error_t *
171vl_socket_read_ready (clib_file_t * uf)
172{
173 clib_file_main_t *fm = &file_main;
174 vlib_main_t *vm = vlib_get_main ();
175 vl_api_registration_t *rp;
176 int n;
177 i8 *msg_buffer = 0;
178 u8 *data_for_process;
179 u32 msg_len;
180 u32 save_input_buffer_length = vec_len (socket_main.input_buffer);
181 vl_socket_args_for_process_t *a;
182 msgbuf_t *mbp;
183 int mbp_set = 0;
184
185 rp = pool_elt_at_index (socket_main.registration_pool, uf->private_data);
186
187 n = read (uf->file_descriptor, socket_main.input_buffer,
188 vec_len (socket_main.input_buffer));
189
190 if (n <= 0 && errno != EAGAIN)
191 {
192 clib_file_del (fm, uf);
193
194 if (!pool_is_free (socket_main.registration_pool, rp))
195 {
196 u32 index = rp - socket_main.registration_pool;
Florin Corase86a8ed2018-01-05 03:20:25 -0800197 vl_socket_free_registration_index (index);
Dave Barach59b25652017-09-10 15:04:27 -0400198 }
199 else
200 {
201 clib_warning ("client index %d already free?",
202 rp->vl_api_registration_pool_index);
203 }
204 return 0;
205 }
206
207 _vec_len (socket_main.input_buffer) = n;
208
209 /*
210 * Look for bugs here. This code is tricky because
211 * data read from a stream socket does not honor message
212 * boundaries. In the case of a long message (>4K bytes)
213 * we have to do (at least) 2 reads, etc.
214 */
215 do
216 {
217 if (vec_len (rp->unprocessed_input))
218 {
219 vec_append (rp->unprocessed_input, socket_main.input_buffer);
220 msg_buffer = rp->unprocessed_input;
221 }
222 else
223 {
224 msg_buffer = socket_main.input_buffer;
225 mbp_set = 0;
226 }
227
228 if (mbp_set == 0)
229 {
230 /* Any chance that we have a complete message? */
231 if (vec_len (msg_buffer) <= sizeof (msgbuf_t))
232 goto save_and_split;
233
234 mbp = (msgbuf_t *) msg_buffer;
235 msg_len = ntohl (mbp->data_len);
236 mbp_set = 1;
237 }
238
239 /* We don't have the entire message yet. */
240 if (mbp_set == 0
241 || (msg_len + sizeof (msgbuf_t)) > vec_len (msg_buffer))
242 {
243 save_and_split:
244 /* if we were using the input buffer save the fragment */
245 if (msg_buffer == socket_main.input_buffer)
246 {
247 ASSERT (vec_len (rp->unprocessed_input) == 0);
248 vec_validate (rp->unprocessed_input, vec_len (msg_buffer) - 1);
249 clib_memcpy (rp->unprocessed_input, msg_buffer,
250 vec_len (msg_buffer));
251 _vec_len (rp->unprocessed_input) = vec_len (msg_buffer);
252 }
253 _vec_len (socket_main.input_buffer) = save_input_buffer_length;
254 return 0;
255 }
256
257 data_for_process = (u8 *) vec_dup (msg_buffer);
258 _vec_len (data_for_process) = (msg_len + sizeof (msgbuf_t));
259 pool_get (socket_main.process_args, a);
260 a->clib_file = uf;
261 a->regp = rp;
262 a->data = data_for_process;
263
Florin Corase86a8ed2018-01-05 03:20:25 -0800264 vlib_process_signal_event (vm, vl_api_clnt_node.index,
Dave Barach59b25652017-09-10 15:04:27 -0400265 SOCKET_READ_EVENT,
266 a - socket_main.process_args);
267 if (n > (msg_len + sizeof (*mbp)))
268 vec_delete (msg_buffer, msg_len + sizeof (*mbp), 0);
269 else
270 _vec_len (msg_buffer) = 0;
271 n -= msg_len + sizeof (msgbuf_t);
272 msg_len = 0;
273 mbp_set = 0;
274 }
275 while (n > 0);
276
277 _vec_len (socket_main.input_buffer) = save_input_buffer_length;
278
279 return 0;
280}
281
Dave Barach59b25652017-09-10 15:04:27 -0400282clib_error_t *
283vl_socket_write_ready (clib_file_t * uf)
284{
285 clib_file_main_t *fm = &file_main;
286 vl_api_registration_t *rp;
287 int n;
288
289 rp = pool_elt_at_index (socket_main.registration_pool, uf->private_data);
290
291 /* Flush output vector. */
Florin Coras8023ad42018-08-02 12:16:03 -0700292 n = write (uf->file_descriptor, rp->output_vector,
293 vec_len (rp->output_vector));
Dave Barach59b25652017-09-10 15:04:27 -0400294 if (n < 0)
295 {
296#if DEBUG > 2
297 clib_warning ("write error, close the file...\n");
298#endif
299 clib_file_del (fm, uf);
Florin Corase86a8ed2018-01-05 03:20:25 -0800300 vl_socket_free_registration_index (rp - socket_main.registration_pool);
Dave Barach59b25652017-09-10 15:04:27 -0400301 return 0;
302 }
Dave Barach59b25652017-09-10 15:04:27 -0400303 else if (n > 0)
Florin Coras8023ad42018-08-02 12:16:03 -0700304 {
305 vec_delete (rp->output_vector, n, 0);
306 if (vec_len (rp->output_vector) <= 0
307 && (uf->flags & UNIX_FILE_DATA_AVAILABLE_TO_WRITE))
308 {
309 uf->flags &= ~UNIX_FILE_DATA_AVAILABLE_TO_WRITE;
310 fm->file_update (uf, UNIX_FILE_UPDATE_MODIFY);
311 }
312 }
Dave Barach59b25652017-09-10 15:04:27 -0400313
314 return 0;
315}
316
317clib_error_t *
318vl_socket_error_ready (clib_file_t * uf)
319{
320 vl_api_registration_t *rp;
321 clib_file_main_t *fm = &file_main;
322
323 rp = pool_elt_at_index (socket_main.registration_pool, uf->private_data);
324 clib_file_del (fm, uf);
Florin Corase86a8ed2018-01-05 03:20:25 -0800325 vl_socket_free_registration_index (rp - socket_main.registration_pool);
Dave Barach59b25652017-09-10 15:04:27 -0400326
327 return 0;
328}
329
330void
331socksvr_file_add (clib_file_main_t * fm, int fd)
332{
333 vl_api_registration_t *rp;
334 clib_file_t template = { 0 };
335
336 pool_get (socket_main.registration_pool, rp);
337 memset (rp, 0, sizeof (*rp));
338
339 template.read_function = vl_socket_read_ready;
340 template.write_function = vl_socket_write_ready;
341 template.error_function = vl_socket_error_ready;
342 template.file_descriptor = fd;
343 template.private_data = rp - socket_main.registration_pool;
344
345 rp->registration_type = REGISTRATION_TYPE_SOCKET_SERVER;
346 rp->vl_api_registration_pool_index = rp - socket_main.registration_pool;
347 rp->clib_file_index = clib_file_add (fm, &template);
348}
349
350static clib_error_t *
351socksvr_accept_ready (clib_file_t * uf)
352{
353 clib_file_main_t *fm = &file_main;
354 socket_main_t *sm = &socket_main;
355 clib_socket_t *sock = &sm->socksvr_listen_socket;
356 clib_socket_t client;
357 clib_error_t *error;
358
359 error = clib_socket_accept (sock, &client);
Dave Barach59b25652017-09-10 15:04:27 -0400360 if (error)
361 return error;
362
363 socksvr_file_add (fm, client.fd);
364 return 0;
365}
366
367static clib_error_t *
368socksvr_bogus_write (clib_file_t * uf)
369{
370 clib_warning ("why am I here?");
371 return 0;
372}
373
374/*
375 * vl_api_sockclnt_create_t_handler
376 */
377void
378vl_api_sockclnt_create_t_handler (vl_api_sockclnt_create_t * mp)
379{
380 vl_api_registration_t *regp;
381 vl_api_sockclnt_create_reply_t *rp;
Florin Coras90a63982017-12-19 04:50:01 -0800382 int rv = 0;
Dave Barach59b25652017-09-10 15:04:27 -0400383
384 regp = socket_main.current_rp;
385
386 ASSERT (regp->registration_type == REGISTRATION_TYPE_SOCKET_SERVER);
387
388 regp->name = format (0, "%s%c", mp->name, 0);
389
390 rp = vl_msg_api_alloc (sizeof (*rp));
391 rp->_vl_msg_id = htons (VL_API_SOCKCLNT_CREATE_REPLY);
392 rp->handle = (uword) regp;
393 rp->index = (uword) regp->vl_api_registration_pool_index;
394 rp->context = mp->context;
395 rp->response = htonl (rv);
396
Florin Corase86a8ed2018-01-05 03:20:25 -0800397 vl_api_send_msg (regp, (u8 *) rp);
Dave Barach59b25652017-09-10 15:04:27 -0400398}
399
400/*
401 * vl_api_sockclnt_delete_t_handler
402 */
403void
404vl_api_sockclnt_delete_t_handler (vl_api_sockclnt_delete_t * mp)
405{
406 vl_api_registration_t *regp;
407 vl_api_sockclnt_delete_reply_t *rp;
408
409 if (!pool_is_free_index (socket_main.registration_pool, mp->index))
410 {
411 regp = pool_elt_at_index (socket_main.registration_pool, mp->index);
412
413 rp = vl_msg_api_alloc (sizeof (*rp));
414 rp->_vl_msg_id = htons (VL_API_SOCKCLNT_DELETE_REPLY);
415 rp->handle = mp->handle;
416 rp->response = htonl (1);
417
Florin Corase86a8ed2018-01-05 03:20:25 -0800418 vl_api_send_msg (regp, (u8 *) rp);
Dave Barach59b25652017-09-10 15:04:27 -0400419
Florin Corasb384b542018-01-15 01:08:33 -0800420 vl_api_registration_del_file (regp);
Florin Corase86a8ed2018-01-05 03:20:25 -0800421 vl_socket_free_registration_index (mp->index);
Dave Barach59b25652017-09-10 15:04:27 -0400422 }
423 else
424 {
425 clib_warning ("unknown client ID %d", mp->index);
426 }
427}
428
Florin Corasb384b542018-01-15 01:08:33 -0800429clib_error_t *
Florin Coras466f2892018-08-03 02:50:43 -0700430vl_sock_api_send_fd_msg (int socket_fd, int fds[], int n_fds)
Dave Barach59b25652017-09-10 15:04:27 -0400431{
432 struct msghdr mh = { 0 };
433 struct iovec iov[1];
Florin Coras99368312018-08-02 10:45:44 -0700434 char ctl[CMSG_SPACE (sizeof (int) * n_fds)];
Florin Coras466f2892018-08-03 02:50:43 -0700435 struct cmsghdr *cmsg;
436 char *msg = "fdmsg";
Dave Barach59b25652017-09-10 15:04:27 -0400437 int rv;
438
439 iov[0].iov_base = msg;
440 iov[0].iov_len = strlen (msg);
441 mh.msg_iov = iov;
442 mh.msg_iovlen = 1;
443
Dave Barach59b25652017-09-10 15:04:27 -0400444 memset (&ctl, 0, sizeof (ctl));
445 mh.msg_control = ctl;
446 mh.msg_controllen = sizeof (ctl);
447 cmsg = CMSG_FIRSTHDR (&mh);
Florin Coras466f2892018-08-03 02:50:43 -0700448 cmsg->cmsg_len = CMSG_LEN (sizeof (int) * n_fds);
Dave Barach59b25652017-09-10 15:04:27 -0400449 cmsg->cmsg_level = SOL_SOCKET;
450 cmsg->cmsg_type = SCM_RIGHTS;
Florin Coras99368312018-08-02 10:45:44 -0700451 clib_memcpy (CMSG_DATA (cmsg), fds, sizeof (int) * n_fds);
Dave Barach59b25652017-09-10 15:04:27 -0400452
453 rv = sendmsg (socket_fd, &mh, 0);
454 if (rv < 0)
455 return clib_error_return_unix (0, "sendmsg");
456 return 0;
457}
458
Florin Coras90a63982017-12-19 04:50:01 -0800459vl_api_shm_elem_config_t *
460vl_api_make_shm_config (vl_api_sock_init_shm_t * mp)
461{
462 vl_api_shm_elem_config_t *config = 0, *c;
463 u64 cfg;
464 int i;
465
466 if (!mp->nitems)
467 {
Dave Barach78958722018-05-10 16:44:27 -0400468 vec_validate (config, 6);
Florin Coras90a63982017-12-19 04:50:01 -0800469 config[0].type = VL_API_VLIB_RING;
Florin Coras90a63982017-12-19 04:50:01 -0800470 config[0].size = 256;
Dave Barach78958722018-05-10 16:44:27 -0400471 config[0].count = 32;
472
473 config[1].type = VL_API_VLIB_RING;
Florin Coras90a63982017-12-19 04:50:01 -0800474 config[1].size = 1024;
Dave Barach78958722018-05-10 16:44:27 -0400475 config[1].count = 16;
476
477 config[2].type = VL_API_VLIB_RING;
Florin Coras90a63982017-12-19 04:50:01 -0800478 config[2].size = 4096;
Dave Barach78958722018-05-10 16:44:27 -0400479 config[2].count = 2;
480
481 config[3].type = VL_API_CLIENT_RING;
482 config[3].size = 256;
483 config[3].count = 32;
484
485 config[4].type = VL_API_CLIENT_RING;
486 config[4].size = 1024;
487 config[4].count = 16;
488
489 config[5].type = VL_API_CLIENT_RING;
490 config[5].size = 4096;
491 config[5].count = 2;
492
493 config[6].type = VL_API_QUEUE;
494 config[6].count = 128;
495 config[6].size = sizeof (uword);
Florin Coras90a63982017-12-19 04:50:01 -0800496 }
497 else
498 {
499 vec_validate (config, mp->nitems - 1);
500 for (i = 0; i < mp->nitems; i++)
501 {
502 cfg = mp->configs[i];
503 /* Pretty much a hack but it avoids defining our own api type
504 * in memclnt.api */
505 c = (vl_api_shm_elem_config_t *) & cfg;
506 config[i].type = c->type;
507 config[i].count = c->count;
508 config[i].size = c->size;
509 }
510 }
511 return config;
512}
513
Dave Barach59b25652017-09-10 15:04:27 -0400514/*
Florin Coras90a63982017-12-19 04:50:01 -0800515 * Bootstrap shm api using the socket api
Dave Barach59b25652017-09-10 15:04:27 -0400516 */
517void
Florin Coras90a63982017-12-19 04:50:01 -0800518vl_api_sock_init_shm_t_handler (vl_api_sock_init_shm_t * mp)
Dave Barach59b25652017-09-10 15:04:27 -0400519{
Florin Coras90a63982017-12-19 04:50:01 -0800520 vl_api_sock_init_shm_reply_t *rmp;
Florin Coras4d9b9d82018-01-14 12:25:50 -0800521 ssvm_private_t _memfd_private, *memfd = &_memfd_private;
Dave Barach59b25652017-09-10 15:04:27 -0400522 svm_map_region_args_t _args, *a = &_args;
Florin Coras90a63982017-12-19 04:50:01 -0800523 vl_api_registration_t *regp;
524 api_main_t *am = &api_main;
Dave Barach59b25652017-09-10 15:04:27 -0400525 svm_region_t *vlib_rp;
Florin Coras90a63982017-12-19 04:50:01 -0800526 clib_file_t *cf;
527 vl_api_shm_elem_config_t *config = 0;
Florin Corasb384b542018-01-15 01:08:33 -0800528 vl_shmem_hdr_t *shmem_hdr;
Dave Barach59b25652017-09-10 15:04:27 -0400529 int rv;
530
531 regp = vl_api_client_index_to_registration (mp->client_index);
Dave Barach59b25652017-09-10 15:04:27 -0400532 if (regp == 0)
533 {
534 clib_warning ("API client disconnected");
535 return;
536 }
Dave Barach59b25652017-09-10 15:04:27 -0400537 if (regp->registration_type != REGISTRATION_TYPE_SOCKET_SERVER)
538 {
539 rv = -31; /* VNET_API_ERROR_INVALID_REGISTRATION */
540 goto reply;
541 }
542
Florin Coras90a63982017-12-19 04:50:01 -0800543 /*
544 * Set up a memfd segment of the requested size wherein the
545 * shmem data structures will be initialized
546 */
Dave Barach59b25652017-09-10 15:04:27 -0400547 memset (memfd, 0, sizeof (*memfd));
Florin Coras4d9b9d82018-01-14 12:25:50 -0800548 memfd->ssvm_size = mp->requested_size;
Dave Barach59b25652017-09-10 15:04:27 -0400549 memfd->requested_va = 0ULL;
550 memfd->i_am_master = 1;
551 memfd->name = format (0, "%s%c", regp->name, 0);
552
Florin Corasb384b542018-01-15 01:08:33 -0800553 if ((rv = ssvm_master_init_memfd (memfd)))
Dave Barach59b25652017-09-10 15:04:27 -0400554 goto reply;
555
556 /* Remember to close this fd when the socket connection goes away */
557 vec_add1 (regp->additional_fds_to_close, memfd->fd);
558
Florin Coras90a63982017-12-19 04:50:01 -0800559 /*
560 * Create a plausible svm_region in the memfd backed segment
561 */
Dave Barach59b25652017-09-10 15:04:27 -0400562 memset (a, 0, sizeof (*a));
Florin Coras4d9b9d82018-01-14 12:25:50 -0800563 a->baseva = memfd->sh->ssvm_va + MMAP_PAGESIZE;
564 a->size = memfd->ssvm_size - MMAP_PAGESIZE;
Dave Barach59b25652017-09-10 15:04:27 -0400565 /* $$$$ might want a different config parameter */
566 a->pvt_heap_size = am->api_pvt_heap_size;
567 a->flags = SVM_FLAGS_MHEAP;
568 svm_region_init_mapped_region (a, (svm_region_t *) a->baseva);
569
Dave Barach59b25652017-09-10 15:04:27 -0400570 /*
571 * Part deux, initialize the svm_region_t shared-memory header
572 * api allocation rings, and so on.
573 */
Florin Coras90a63982017-12-19 04:50:01 -0800574 config = vl_api_make_shm_config (mp);
575 vlib_rp = (svm_region_t *) a->baseva;
576 vl_init_shmem (vlib_rp, config, 1 /* is_vlib (dont-care) */ ,
577 1 /* is_private */ );
Florin Corasb384b542018-01-15 01:08:33 -0800578
579 /* Remember who created this. Needs to be post vl_init_shmem */
580 shmem_hdr = (vl_shmem_hdr_t *) vlib_rp->user_ctx;
581 shmem_hdr->clib_file_index = vl_api_registration_file_index (regp);
582
Dave Barach59b25652017-09-10 15:04:27 -0400583 vec_add1 (am->vlib_private_rps, vlib_rp);
Dave Barach59b25652017-09-10 15:04:27 -0400584 memfd->sh->ready = 1;
Florin Coras90a63982017-12-19 04:50:01 -0800585 vec_free (config);
Dave Barach59b25652017-09-10 15:04:27 -0400586
587 /* Recompute the set of input queues to poll in memclnt_process */
588 vec_reset_length (vl_api_queue_cursizes);
589
590reply:
591
Florin Coras90a63982017-12-19 04:50:01 -0800592 rmp = vl_msg_api_alloc (sizeof (*rmp));
593 rmp->_vl_msg_id = htons (VL_API_SOCK_INIT_SHM_REPLY);
594 rmp->context = mp->context;
595 rmp->retval = htonl (rv);
596
Florin Coras8023ad42018-08-02 12:16:03 -0700597 /*
598 * Note: The reply message needs to make it out the back door
599 * before we send the magic fd message. That's taken care of by
600 * the send function.
601 */
602 vl_socket_api_send (regp, (u8 *) rmp);
Florin Coras90a63982017-12-19 04:50:01 -0800603
604 if (rv != 0)
605 return;
606
Florin Coras90a63982017-12-19 04:50:01 -0800607 /* Send the magic "here's your sign (aka fd)" socket message */
Florin Coras8023ad42018-08-02 12:16:03 -0700608 cf = vl_api_registration_file (regp);
Florin Coras466f2892018-08-03 02:50:43 -0700609 vl_sock_api_send_fd_msg (cf->file_descriptor, &memfd->fd, 1);
Florin Coras90a63982017-12-19 04:50:01 -0800610}
611
Florin Coras90a63982017-12-19 04:50:01 -0800612#define foreach_vlib_api_msg \
613_(SOCKCLNT_CREATE, sockclnt_create) \
614_(SOCKCLNT_DELETE, sockclnt_delete) \
Dave Barach78958722018-05-10 16:44:27 -0400615_(SOCK_INIT_SHM, sock_init_shm)
Dave Barach59b25652017-09-10 15:04:27 -0400616
617clib_error_t *
Florin Corase86a8ed2018-01-05 03:20:25 -0800618vl_sock_api_init (vlib_main_t * vm)
Dave Barach59b25652017-09-10 15:04:27 -0400619{
620 clib_file_main_t *fm = &file_main;
621 clib_file_t template = { 0 };
622 vl_api_registration_t *rp;
Dave Barach59b25652017-09-10 15:04:27 -0400623 socket_main_t *sm = &socket_main;
624 clib_socket_t *sock = &sm->socksvr_listen_socket;
625 clib_error_t *error;
626
627 /* If not explicitly configured, do not bind/enable, etc. */
628 if (sm->socket_name == 0)
629 return 0;
630
Florin Coras90a63982017-12-19 04:50:01 -0800631#define _(N,n) \
632 vl_msg_api_set_handlers(VL_API_##N, #n, \
633 vl_api_##n##_t_handler, \
634 vl_noop_handler, \
635 vl_api_##n##_t_endian, \
636 vl_api_##n##_t_print, \
637 sizeof(vl_api_##n##_t), 1);
Dave Barach59b25652017-09-10 15:04:27 -0400638 foreach_vlib_api_msg;
639#undef _
640
641 vec_resize (sm->input_buffer, 4096);
642
643 sock->config = (char *) sm->socket_name;
644
645 /* mkdir of file socket, only under /run */
646 if (strncmp (sock->config, "/run", 4) == 0)
647 {
648 u8 *tmp = format (0, "%s", sock->config);
649 int i = vec_len (tmp);
650 while (i && tmp[--i] != '/')
651 ;
652
653 tmp[i] = 0;
654
655 if (i)
656 vlib_unix_recursive_mkdir ((char *) tmp);
657 vec_free (tmp);
658 }
659
660 sock->flags = CLIB_SOCKET_F_IS_SERVER | CLIB_SOCKET_F_SEQPACKET |
661 CLIB_SOCKET_F_ALLOW_GROUP_WRITE;
662 error = clib_socket_init (sock);
663 if (error)
664 return error;
665
666 pool_get (sm->registration_pool, rp);
667 memset (rp, 0, sizeof (*rp));
668
669 rp->registration_type = REGISTRATION_TYPE_SOCKET_LISTEN;
670
671 template.read_function = socksvr_accept_ready;
672 template.write_function = socksvr_bogus_write;
673 template.file_descriptor = sock->fd;
674 template.private_data = rp - sm->registration_pool;
675
676 rp->clib_file_index = clib_file_add (fm, &template);
677 return 0;
678}
679
680static clib_error_t *
681socket_exit (vlib_main_t * vm)
682{
Dave Barach59b25652017-09-10 15:04:27 -0400683 socket_main_t *sm = &socket_main;
684 vl_api_registration_t *rp;
685
686 /* Defensive driving in case something wipes out early */
687 if (sm->registration_pool)
688 {
689 u32 index;
690 /* *INDENT-OFF* */
691 pool_foreach (rp, sm->registration_pool, ({
Florin Corasb384b542018-01-15 01:08:33 -0800692 vl_api_registration_del_file (rp);
693 index = rp->vl_api_registration_pool_index;
694 vl_socket_free_registration_index (index);
Dave Barach59b25652017-09-10 15:04:27 -0400695 }));
696/* *INDENT-ON* */
697 }
698
699 return 0;
700}
701
702VLIB_MAIN_LOOP_EXIT_FUNCTION (socket_exit);
703
704static clib_error_t *
705socksvr_config (vlib_main_t * vm, unformat_input_t * input)
706{
707 socket_main_t *sm = &socket_main;
708
709 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
710 {
711 if (unformat (input, "socket-name %s", &sm->socket_name))
712 ;
713 else if (unformat (input, "default"))
714 {
715 sm->socket_name = format (0, "%s%c", API_SOCKET_FILE, 0);
716 }
717 else
718 {
719 return clib_error_return (0, "unknown input '%U'",
720 format_unformat_error, input);
721 }
722 }
723 return 0;
724}
725
726VLIB_CONFIG_FUNCTION (socksvr_config, "socksvr");
727
728clib_error_t *
729vlibsocket_init (vlib_main_t * vm)
730{
731 return 0;
732}
733
734VLIB_INIT_FUNCTION (vlibsocket_init);
735
736/*
737 * fd.io coding-style-patch-verification: ON
738 *
739 * Local Variables:
740 * eval: (c-set-style "gnu")
741 * End:
742 */