2 *------------------------------------------------------------------
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:
10 * http://www.apache.org/licenses/LICENSE-2.0
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 *------------------------------------------------------------------
20 #include <sys/types.h>
21 #include <sys/socket.h>
22 #include <netinet/in.h>
23 #include <sys/ioctl.h>
24 #include <vppinfra/byte_order.h>
25 #include <svm/memfd.h>
30 #include <vlibmemory/api.h>
32 #include <vlibmemory/vl_memory_msg_enum.h>
34 #define vl_typedefs /* define message structures */
35 #include <vlibmemory/vl_memory_api_h.h>
38 /* instantiate all the print functions we know about */
39 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
41 #include <vlibmemory/vl_memory_api_h.h>
44 /* instantiate all the endian swap functions we know about */
46 #include <vlibmemory/vl_memory_api_h.h>
50 dump_socket_clients (vlib_main_t * vm, api_main_t * am)
52 vl_api_registration_t *reg;
53 socket_main_t *sm = &socket_main;
54 clib_file_main_t *fm = &file_main;
58 * Must have at least one active client, not counting the
59 * REGISTRATION_TYPE_SOCKET_LISTEN bind/accept socket
61 if (pool_elts (sm->registration_pool) < 2)
64 vlib_cli_output (vm, "Socket clients");
65 vlib_cli_output (vm, "%20s %8s", "Name", "Fildesc");
67 pool_foreach (reg, sm->registration_pool,
69 if (reg->registration_type == REGISTRATION_TYPE_SOCKET_SERVER) {
70 f = pool_elt_at_index (fm->file_pool, reg->clib_file_index);
71 vlib_cli_output (vm, "%20s %8d",
72 reg->name, f->file_descriptor);
79 vl_socket_api_send (vl_api_registration_t * rp, u8 * elem)
84 socket_main_t *sm = &socket_main;
85 u16 msg_id = ntohs (*(u16 *) elem);
86 api_main_t *am = &api_main;
87 msgbuf_t *mb = (msgbuf_t *) (elem - offsetof (msgbuf_t, data));
88 clib_file_t *cf = clib_file_get (&file_main, rp->clib_file_index);
89 vl_api_registration_t *sock_rp;
91 ASSERT (rp->registration_type > REGISTRATION_TYPE_SHMEM);
93 if (msg_id >= vec_len (am->api_trace_cfg))
95 clib_warning ("id out of range: %d", msg_id);
96 vl_msg_api_free ((void *) elem);
100 sock_rp = pool_elt_at_index (sm->registration_pool,
101 rp->vl_api_registration_pool_index);
104 /* Add the msgbuf_t to the output vector */
105 vl_socket_add_pending_output_no_flush (cf, sock_rp, (u8 *) mb,
107 /* Send the message */
108 vl_socket_add_pending_output (cf, sock_rp, elem, ntohl (mb->data_len));
111 output_length = sizeof (*mb) + ntohl (mb->data_len);
112 clib_warning ("wrote %u bytes to fd %d", output_length,
113 cf->file_descriptor);
116 vl_msg_api_free ((void *) elem);
120 vl_free_socket_registration_index (u32 pool_index)
123 vl_api_registration_t *rp;
124 if (pool_is_free_index (socket_main.registration_pool, pool_index))
126 clib_warning ("main pool index %d already free", pool_index);
129 rp = pool_elt_at_index (socket_main.registration_pool, pool_index);
131 ASSERT (rp->registration_type != REGISTRATION_TYPE_FREE);
132 for (i = 0; i < vec_len (rp->additional_fds_to_close); i++)
133 if (close (rp->additional_fds_to_close[i]) < 0)
134 clib_unix_warning ("close");
135 vec_free (rp->additional_fds_to_close);
137 vec_free (rp->unprocessed_input);
138 vec_free (rp->output_vector);
139 rp->registration_type = REGISTRATION_TYPE_FREE;
140 pool_put (socket_main.registration_pool, rp);
144 vl_api_socket_process_msg (clib_file_t * uf, vl_api_registration_t * rp,
147 msgbuf_t *mbp = (msgbuf_t *) input_v;
149 u8 *the_msg = (u8 *) (mbp->data);
150 socket_main.current_uf = uf;
151 socket_main.current_rp = rp;
152 vl_msg_api_socket_handler (the_msg);
153 socket_main.current_uf = 0;
154 socket_main.current_rp = 0;
158 vl_socket_read_ready (clib_file_t * uf)
160 clib_file_main_t *fm = &file_main;
161 vlib_main_t *vm = vlib_get_main ();
162 vl_api_registration_t *rp;
165 u8 *data_for_process;
167 u32 save_input_buffer_length = vec_len (socket_main.input_buffer);
168 vl_socket_args_for_process_t *a;
172 rp = pool_elt_at_index (socket_main.registration_pool, uf->private_data);
174 n = read (uf->file_descriptor, socket_main.input_buffer,
175 vec_len (socket_main.input_buffer));
177 if (n <= 0 && errno != EAGAIN)
179 clib_file_del (fm, uf);
181 if (!pool_is_free (socket_main.registration_pool, rp))
183 u32 index = rp - socket_main.registration_pool;
184 vl_free_socket_registration_index (index);
188 clib_warning ("client index %d already free?",
189 rp->vl_api_registration_pool_index);
194 _vec_len (socket_main.input_buffer) = n;
197 * Look for bugs here. This code is tricky because
198 * data read from a stream socket does not honor message
199 * boundaries. In the case of a long message (>4K bytes)
200 * we have to do (at least) 2 reads, etc.
204 if (vec_len (rp->unprocessed_input))
206 vec_append (rp->unprocessed_input, socket_main.input_buffer);
207 msg_buffer = rp->unprocessed_input;
211 msg_buffer = socket_main.input_buffer;
217 /* Any chance that we have a complete message? */
218 if (vec_len (msg_buffer) <= sizeof (msgbuf_t))
221 mbp = (msgbuf_t *) msg_buffer;
222 msg_len = ntohl (mbp->data_len);
226 /* We don't have the entire message yet. */
228 || (msg_len + sizeof (msgbuf_t)) > vec_len (msg_buffer))
231 /* if we were using the input buffer save the fragment */
232 if (msg_buffer == socket_main.input_buffer)
234 ASSERT (vec_len (rp->unprocessed_input) == 0);
235 vec_validate (rp->unprocessed_input, vec_len (msg_buffer) - 1);
236 clib_memcpy (rp->unprocessed_input, msg_buffer,
237 vec_len (msg_buffer));
238 _vec_len (rp->unprocessed_input) = vec_len (msg_buffer);
240 _vec_len (socket_main.input_buffer) = save_input_buffer_length;
244 data_for_process = (u8 *) vec_dup (msg_buffer);
245 _vec_len (data_for_process) = (msg_len + sizeof (msgbuf_t));
246 pool_get (socket_main.process_args, a);
249 a->data = data_for_process;
251 vlib_process_signal_event (vm, memclnt_node.index,
253 a - socket_main.process_args);
254 if (n > (msg_len + sizeof (*mbp)))
255 vec_delete (msg_buffer, msg_len + sizeof (*mbp), 0);
257 _vec_len (msg_buffer) = 0;
258 n -= msg_len + sizeof (msgbuf_t);
264 _vec_len (socket_main.input_buffer) = save_input_buffer_length;
270 vl_socket_add_pending_output (clib_file_t * uf,
271 vl_api_registration_t * rp,
272 u8 * buffer, uword buffer_bytes)
274 clib_file_main_t *fm = &file_main;
276 vec_add (rp->output_vector, buffer, buffer_bytes);
277 if (vec_len (rp->output_vector) > 0)
279 int skip_update = 0 != (uf->flags & UNIX_FILE_DATA_AVAILABLE_TO_WRITE);
280 uf->flags |= UNIX_FILE_DATA_AVAILABLE_TO_WRITE;
282 fm->file_update (uf, UNIX_FILE_UPDATE_MODIFY);
287 vl_socket_add_pending_output_no_flush (clib_file_t * uf,
288 vl_api_registration_t * rp,
289 u8 * buffer, uword buffer_bytes)
291 vec_add (rp->output_vector, buffer, buffer_bytes);
295 socket_del_pending_output (clib_file_t * uf,
296 vl_api_registration_t * rp, uword n_bytes)
298 clib_file_main_t *fm = &file_main;
300 vec_delete (rp->output_vector, n_bytes, 0);
301 if (vec_len (rp->output_vector) <= 0)
303 int skip_update = 0 == (uf->flags & UNIX_FILE_DATA_AVAILABLE_TO_WRITE);
304 uf->flags &= ~UNIX_FILE_DATA_AVAILABLE_TO_WRITE;
306 fm->file_update (uf, UNIX_FILE_UPDATE_MODIFY);
311 vl_socket_write_ready (clib_file_t * uf)
313 clib_file_main_t *fm = &file_main;
314 vl_api_registration_t *rp;
317 rp = pool_elt_at_index (socket_main.registration_pool, uf->private_data);
319 /* Flush output vector. */
320 n = write (uf->file_descriptor,
321 rp->output_vector, vec_len (rp->output_vector));
325 clib_warning ("write error, close the file...\n");
327 clib_file_del (fm, uf);
329 vl_free_socket_registration_index (rp - socket_main.registration_pool);
334 socket_del_pending_output (uf, rp, n);
340 vl_socket_error_ready (clib_file_t * uf)
342 vl_api_registration_t *rp;
343 clib_file_main_t *fm = &file_main;
345 rp = pool_elt_at_index (socket_main.registration_pool, uf->private_data);
346 clib_file_del (fm, uf);
347 vl_free_socket_registration_index (rp - socket_main.registration_pool);
353 socksvr_file_add (clib_file_main_t * fm, int fd)
355 vl_api_registration_t *rp;
356 clib_file_t template = { 0 };
358 pool_get (socket_main.registration_pool, rp);
359 memset (rp, 0, sizeof (*rp));
361 template.read_function = vl_socket_read_ready;
362 template.write_function = vl_socket_write_ready;
363 template.error_function = vl_socket_error_ready;
364 template.file_descriptor = fd;
365 template.private_data = rp - socket_main.registration_pool;
367 rp->registration_type = REGISTRATION_TYPE_SOCKET_SERVER;
368 rp->vl_api_registration_pool_index = rp - socket_main.registration_pool;
369 rp->clib_file_index = clib_file_add (fm, &template);
372 static clib_error_t *
373 socksvr_accept_ready (clib_file_t * uf)
375 clib_file_main_t *fm = &file_main;
376 socket_main_t *sm = &socket_main;
377 clib_socket_t *sock = &sm->socksvr_listen_socket;
378 clib_socket_t client;
381 error = clib_socket_accept (sock, &client);
386 socksvr_file_add (fm, client.fd);
390 static clib_error_t *
391 socksvr_bogus_write (clib_file_t * uf)
393 clib_warning ("why am I here?");
398 * vl_api_sockclnt_create_t_handler
401 vl_api_sockclnt_create_t_handler (vl_api_sockclnt_create_t * mp)
403 vl_api_registration_t *regp;
404 vl_api_sockclnt_create_reply_t *rp;
407 regp = socket_main.current_rp;
409 ASSERT (regp->registration_type == REGISTRATION_TYPE_SOCKET_SERVER);
411 regp->name = format (0, "%s%c", mp->name, 0);
413 rp = vl_msg_api_alloc (sizeof (*rp));
414 rp->_vl_msg_id = htons (VL_API_SOCKCLNT_CREATE_REPLY);
415 rp->handle = (uword) regp;
416 rp->index = (uword) regp->vl_api_registration_pool_index;
417 rp->context = mp->context;
418 rp->response = htonl (rv);
420 vl_msg_api_send (regp, (u8 *) rp);
424 * vl_api_sockclnt_delete_t_handler
427 vl_api_sockclnt_delete_t_handler (vl_api_sockclnt_delete_t * mp)
429 vl_api_registration_t *regp;
430 vl_api_sockclnt_delete_reply_t *rp;
432 if (!pool_is_free_index (socket_main.registration_pool, mp->index))
434 regp = pool_elt_at_index (socket_main.registration_pool, mp->index);
436 rp = vl_msg_api_alloc (sizeof (*rp));
437 rp->_vl_msg_id = htons (VL_API_SOCKCLNT_DELETE_REPLY);
438 rp->handle = mp->handle;
439 rp->response = htonl (1);
441 vl_msg_api_send (regp, (u8 *) rp);
443 clib_file_del (&file_main, file_main.file_pool + regp->clib_file_index);
445 vl_free_socket_registration_index (mp->index);
449 clib_warning ("unknown client ID %d", mp->index);
453 static clib_error_t *
454 send_fd_msg (int socket_fd, int fd_to_share)
456 struct msghdr mh = { 0 };
458 char ctl[CMSG_SPACE (sizeof (int))];
462 iov[0].iov_base = msg;
463 iov[0].iov_len = strlen (msg);
467 struct cmsghdr *cmsg;
468 memset (&ctl, 0, sizeof (ctl));
469 mh.msg_control = ctl;
470 mh.msg_controllen = sizeof (ctl);
471 cmsg = CMSG_FIRSTHDR (&mh);
472 cmsg->cmsg_len = CMSG_LEN (sizeof (int));
473 cmsg->cmsg_level = SOL_SOCKET;
474 cmsg->cmsg_type = SCM_RIGHTS;
475 memcpy (CMSG_DATA (cmsg), &fd_to_share, sizeof (int));
477 rv = sendmsg (socket_fd, &mh, 0);
479 return clib_error_return_unix (0, "sendmsg");
483 vl_api_shm_elem_config_t *
484 vl_api_make_shm_config (vl_api_sock_init_shm_t * mp)
486 vl_api_shm_elem_config_t *config = 0, *c;
492 vec_validate (config, 3);
493 config[0].type = VL_API_VLIB_RING;
494 config[0].count = 128;
495 config[0].size = 256;
496 config[1].type = VL_API_CLIENT_RING;
497 config[1].count = 128;
498 config[1].size = 1024;
499 config[2].type = VL_API_CLIENT_RING;
501 config[2].size = 4096;
502 config[3].type = VL_API_QUEUE;
503 config[3].count = 128;
504 config[3].size = sizeof (uword);
508 vec_validate (config, mp->nitems - 1);
509 for (i = 0; i < mp->nitems; i++)
511 cfg = mp->configs[i];
512 /* Pretty much a hack but it avoids defining our own api type
514 c = (vl_api_shm_elem_config_t *) & cfg;
515 config[i].type = c->type;
516 config[i].count = c->count;
517 config[i].size = c->size;
524 * Bootstrap shm api using the socket api
527 vl_api_sock_init_shm_t_handler (vl_api_sock_init_shm_t * mp)
529 vl_api_sock_init_shm_reply_t *rmp;
530 memfd_private_t _memfd_private, *memfd = &_memfd_private;
531 svm_map_region_args_t _args, *a = &_args;
532 vl_api_registration_t *regp;
533 api_main_t *am = &api_main;
534 svm_region_t *vlib_rp;
536 vl_api_shm_elem_config_t *config = 0;
539 regp = vl_api_client_index_to_registration (mp->client_index);
542 clib_warning ("API client disconnected");
545 if (regp->registration_type != REGISTRATION_TYPE_SOCKET_SERVER)
547 rv = -31; /* VNET_API_ERROR_INVALID_REGISTRATION */
552 * Set up a memfd segment of the requested size wherein the
553 * shmem data structures will be initialized
555 memset (memfd, 0, sizeof (*memfd));
556 memfd->memfd_size = mp->requested_size;
557 memfd->requested_va = 0ULL;
558 memfd->i_am_master = 1;
559 memfd->name = format (0, "%s%c", regp->name, 0);
561 if ((rv = memfd_master_init (memfd, mp->client_index)))
564 /* Remember to close this fd when the socket connection goes away */
565 vec_add1 (regp->additional_fds_to_close, memfd->fd);
568 * Create a plausible svm_region in the memfd backed segment
570 memset (a, 0, sizeof (*a));
571 a->baseva = memfd->sh->memfd_va + MMAP_PAGESIZE;
572 a->size = memfd->memfd_size - MMAP_PAGESIZE;
573 /* $$$$ might want a different config parameter */
574 a->pvt_heap_size = am->api_pvt_heap_size;
575 a->flags = SVM_FLAGS_MHEAP;
576 svm_region_init_mapped_region (a, (svm_region_t *) a->baseva);
579 * Part deux, initialize the svm_region_t shared-memory header
580 * api allocation rings, and so on.
582 config = vl_api_make_shm_config (mp);
583 vlib_rp = (svm_region_t *) a->baseva;
584 vl_init_shmem (vlib_rp, config, 1 /* is_vlib (dont-care) */ ,
585 1 /* is_private */ );
586 vec_add1 (am->vlib_private_rps, vlib_rp);
587 memfd->sh->ready = 1;
590 /* Recompute the set of input queues to poll in memclnt_process */
591 vec_reset_length (vl_api_queue_cursizes);
595 rmp = vl_msg_api_alloc (sizeof (*rmp));
596 rmp->_vl_msg_id = htons (VL_API_SOCK_INIT_SHM_REPLY);
597 rmp->context = mp->context;
598 rmp->retval = htonl (rv);
600 vl_msg_api_send (regp, (u8 *) rmp);
606 * We need the reply message to make it out the back door
607 * before we send the magic fd message so force a flush
609 cf = clib_file_get (&file_main, regp->clib_file_index);
610 cf->write_function (cf);
612 /* Send the magic "here's your sign (aka fd)" socket message */
613 send_fd_msg (cf->file_descriptor, memfd->fd);
617 * Create a memory-fd segment.
620 vl_api_memfd_segment_create_t_handler (vl_api_memfd_segment_create_t * mp)
622 vl_api_memfd_segment_create_reply_t *rmp;
624 memfd_private_t _memfd_private, *memfd = &_memfd_private;
625 vl_api_registration_t *regp;
628 regp = vl_api_client_index_to_registration (mp->client_index);
631 clib_warning ("API client disconnected");
635 memset (memfd, 0, sizeof (*memfd));
636 memfd->memfd_size = mp->requested_size;
637 memfd->requested_va = 0ULL;
638 memfd->i_am_master = 1;
639 memfd->name = format (0, "%s%c", regp->name, 0);
641 /* Set up a memfd segment of the requested size */
642 if ((rv = memfd_master_init (memfd, mp->client_index)))
645 /* Remember to close this fd when the socket connection goes away */
646 vec_add1 (regp->additional_fds_to_close, memfd->fd);
650 rmp = vl_msg_api_alloc (sizeof (*rmp));
651 rmp->_vl_msg_id = htons (VL_API_MEMFD_SEGMENT_CREATE_REPLY);
652 rmp->context = mp->context;
653 rmp->retval = htonl (rv);
655 vl_msg_api_send (regp, (u8 *) rmp);
661 * We need the reply message to make it out the back door
662 * before we send the magic fd message.
664 cf = file_main.file_pool + regp->clib_file_index;
665 cf->write_function (cf);
667 /* send the magic "here's your sign (aka fd)" socket message */
668 send_fd_msg (cf->file_descriptor, memfd->fd);
671 #define foreach_vlib_api_msg \
672 _(SOCKCLNT_CREATE, sockclnt_create) \
673 _(SOCKCLNT_DELETE, sockclnt_delete) \
674 _(SOCK_INIT_SHM, sock_init_shm) \
675 _(MEMFD_SEGMENT_CREATE, memfd_segment_create)
678 socksvr_api_init (vlib_main_t * vm)
680 clib_file_main_t *fm = &file_main;
681 clib_file_t template = { 0 };
682 vl_api_registration_t *rp;
683 socket_main_t *sm = &socket_main;
684 clib_socket_t *sock = &sm->socksvr_listen_socket;
687 /* If not explicitly configured, do not bind/enable, etc. */
688 if (sm->socket_name == 0)
692 vl_msg_api_set_handlers(VL_API_##N, #n, \
693 vl_api_##n##_t_handler, \
695 vl_api_##n##_t_endian, \
696 vl_api_##n##_t_print, \
697 sizeof(vl_api_##n##_t), 1);
698 foreach_vlib_api_msg;
701 vec_resize (sm->input_buffer, 4096);
703 sock->config = (char *) sm->socket_name;
705 /* mkdir of file socket, only under /run */
706 if (strncmp (sock->config, "/run", 4) == 0)
708 u8 *tmp = format (0, "%s", sock->config);
709 int i = vec_len (tmp);
710 while (i && tmp[--i] != '/')
716 vlib_unix_recursive_mkdir ((char *) tmp);
720 sock->flags = CLIB_SOCKET_F_IS_SERVER | CLIB_SOCKET_F_SEQPACKET |
721 CLIB_SOCKET_F_ALLOW_GROUP_WRITE;
722 error = clib_socket_init (sock);
726 pool_get (sm->registration_pool, rp);
727 memset (rp, 0, sizeof (*rp));
729 rp->registration_type = REGISTRATION_TYPE_SOCKET_LISTEN;
731 template.read_function = socksvr_accept_ready;
732 template.write_function = socksvr_bogus_write;
733 template.file_descriptor = sock->fd;
734 template.private_data = rp - sm->registration_pool;
736 rp->clib_file_index = clib_file_add (fm, &template);
740 static clib_error_t *
741 socket_exit (vlib_main_t * vm)
743 clib_file_main_t *fm = &file_main;
744 socket_main_t *sm = &socket_main;
745 vl_api_registration_t *rp;
747 /* Defensive driving in case something wipes out early */
748 if (sm->registration_pool)
752 pool_foreach (rp, sm->registration_pool, ({
753 clib_file_del (fm, fm->file_pool + rp->clib_file_index);
754 index = rp->vl_api_registration_pool_index;
755 vl_free_socket_registration_index (index);
763 VLIB_MAIN_LOOP_EXIT_FUNCTION (socket_exit);
765 static clib_error_t *
766 socksvr_config (vlib_main_t * vm, unformat_input_t * input)
768 socket_main_t *sm = &socket_main;
770 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
772 if (unformat (input, "socket-name %s", &sm->socket_name))
774 else if (unformat (input, "default"))
776 sm->socket_name = format (0, "%s%c", API_SOCKET_FILE, 0);
780 return clib_error_return (0, "unknown input '%U'",
781 format_unformat_error, input);
787 VLIB_CONFIG_FUNCTION (socksvr_config, "socksvr");
790 vlibsocket_init (vlib_main_t * vm)
795 VLIB_INIT_FUNCTION (vlibsocket_init);
798 * fd.io coding-style-patch-verification: ON
801 * eval: (c-set-style "gnu")