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>
27 #include <vppinfra/byte_order.h>
29 #include <vlibmemory/api.h>
31 #include <vlibmemory/vl_memory_msg_enum.h>
33 #define vl_typedefs /* define message structures */
34 #include <vlibmemory/vl_memory_api_h.h>
37 /* instantiate all the print functions we know about */
38 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
40 #include <vlibmemory/vl_memory_api_h.h>
43 /* instantiate all the endian swap functions we know about */
45 #include <vlibmemory/vl_memory_api_h.h>
48 socket_main_t socket_main;
50 #define SOCK_API_REG_HANDLE_BIT (1<<31)
53 sock_api_registration_handle (vl_api_registration_t * regp)
55 ASSERT (regp->vl_api_registration_pool_index < SOCK_API_REG_HANDLE_BIT);
56 return regp->vl_api_registration_pool_index | SOCK_API_REG_HANDLE_BIT;
60 socket_api_registration_handle_to_index (u32 reg_index)
62 return (reg_index & ~SOCK_API_REG_HANDLE_BIT);
66 vl_socket_api_registration_handle_is_valid (u32 reg_handle)
68 return ((reg_handle & SOCK_API_REG_HANDLE_BIT) != 0);
72 vl_sock_api_dump_clients (vlib_main_t * vm, api_main_t * am)
74 vl_api_registration_t *reg;
75 socket_main_t *sm = &socket_main;
79 * Must have at least one active client, not counting the
80 * REGISTRATION_TYPE_SOCKET_LISTEN bind/accept socket
82 if (pool_elts (sm->registration_pool) < 2)
85 vlib_cli_output (vm, "Socket clients");
86 vlib_cli_output (vm, "%20s %8s", "Name", "Fildesc");
88 pool_foreach (reg, sm->registration_pool,
90 if (reg->registration_type == REGISTRATION_TYPE_SOCKET_SERVER) {
91 f = vl_api_registration_file (reg);
92 vlib_cli_output (vm, "%20s %8d", reg->name, f->file_descriptor);
98 vl_api_registration_t *
99 vl_socket_api_client_handle_to_registration (u32 handle)
101 socket_main_t *sm = &socket_main;
102 u32 index = socket_api_registration_handle_to_index (handle);
103 if (pool_is_free_index (sm->registration_pool, index))
106 clib_warning ("Invalid index %d\n", index);
110 return pool_elt_at_index (sm->registration_pool, index);
114 vl_socket_api_send (vl_api_registration_t * rp, u8 * elem)
119 socket_main_t *sm = &socket_main;
120 u16 msg_id = ntohs (*(u16 *) elem);
121 api_main_t *am = &api_main;
122 msgbuf_t *mb = (msgbuf_t *) (elem - offsetof (msgbuf_t, data));
123 vl_api_registration_t *sock_rp;
124 clib_file_main_t *fm = &file_main;
128 cf = vl_api_registration_file (rp);
129 ASSERT (rp->registration_type > REGISTRATION_TYPE_SHMEM);
131 if (msg_id >= vec_len (am->api_trace_cfg))
133 clib_warning ("id out of range: %d", msg_id);
134 vl_msg_api_free ((void *) elem);
138 sock_rp = pool_elt_at_index (sm->registration_pool,
139 rp->vl_api_registration_pool_index);
142 /* Add the msgbuf_t to the output vector */
143 vec_add (sock_rp->output_vector, (u8 *) mb, sizeof (*mb));
145 /* Try to send the message and save any error like
146 * we do in the input epoll loop */
147 vec_add (sock_rp->output_vector, elem, ntohl (mb->data_len));
148 error = clib_file_write (cf);
149 unix_save_error (&unix_main, error);
151 /* If we didn't finish sending everything, wait for tx space */
152 if (vec_len (sock_rp->output_vector) > 0
153 && !(cf->flags & UNIX_FILE_DATA_AVAILABLE_TO_WRITE))
155 cf->flags |= UNIX_FILE_DATA_AVAILABLE_TO_WRITE;
156 fm->file_update (cf, UNIX_FILE_UPDATE_MODIFY);
160 output_length = sizeof (*mb) + ntohl (mb->data_len);
161 clib_warning ("wrote %u bytes to fd %d", output_length,
162 cf->file_descriptor);
165 vl_msg_api_free ((void *) elem);
169 vl_socket_free_registration_index (u32 pool_index)
172 vl_api_registration_t *rp;
173 if (pool_is_free_index (socket_main.registration_pool, pool_index))
175 clib_warning ("main pool index %d already free", pool_index);
178 rp = pool_elt_at_index (socket_main.registration_pool, pool_index);
180 ASSERT (rp->registration_type != REGISTRATION_TYPE_FREE);
181 for (i = 0; i < vec_len (rp->additional_fds_to_close); i++)
182 if (close (rp->additional_fds_to_close[i]) < 0)
183 clib_unix_warning ("close");
184 vec_free (rp->additional_fds_to_close);
186 vec_free (rp->unprocessed_input);
187 vec_free (rp->output_vector);
188 rp->registration_type = REGISTRATION_TYPE_FREE;
189 pool_put (socket_main.registration_pool, rp);
193 vl_socket_process_api_msg (clib_file_t * uf, vl_api_registration_t * rp,
196 msgbuf_t *mbp = (msgbuf_t *) input_v;
198 u8 *the_msg = (u8 *) (mbp->data);
199 socket_main.current_uf = uf;
200 socket_main.current_rp = rp;
201 vl_msg_api_socket_handler (the_msg);
202 socket_main.current_uf = 0;
203 socket_main.current_rp = 0;
207 vl_socket_read_ready (clib_file_t * uf)
209 clib_file_main_t *fm = &file_main;
210 vlib_main_t *vm = vlib_get_main ();
211 vl_api_registration_t *rp;
214 u8 *data_for_process;
216 u32 save_input_buffer_length = vec_len (socket_main.input_buffer);
217 vl_socket_args_for_process_t *a;
221 rp = pool_elt_at_index (socket_main.registration_pool, uf->private_data);
223 n = read (uf->file_descriptor, socket_main.input_buffer,
224 vec_len (socket_main.input_buffer));
226 if (n <= 0 && errno != EAGAIN)
228 clib_file_del (fm, uf);
230 if (!pool_is_free (socket_main.registration_pool, rp))
232 u32 index = rp - socket_main.registration_pool;
233 vl_socket_free_registration_index (index);
237 clib_warning ("client index %d already free?",
238 rp->vl_api_registration_pool_index);
243 _vec_len (socket_main.input_buffer) = n;
246 * Look for bugs here. This code is tricky because
247 * data read from a stream socket does not honor message
248 * boundaries. In the case of a long message (>4K bytes)
249 * we have to do (at least) 2 reads, etc.
253 if (vec_len (rp->unprocessed_input))
255 vec_append (rp->unprocessed_input, socket_main.input_buffer);
256 msg_buffer = rp->unprocessed_input;
260 msg_buffer = socket_main.input_buffer;
266 /* Any chance that we have a complete message? */
267 if (vec_len (msg_buffer) <= sizeof (msgbuf_t))
270 mbp = (msgbuf_t *) msg_buffer;
271 msg_len = ntohl (mbp->data_len);
275 /* We don't have the entire message yet. */
277 || (msg_len + sizeof (msgbuf_t)) > vec_len (msg_buffer))
280 /* if we were using the input buffer save the fragment */
281 if (msg_buffer == socket_main.input_buffer)
283 ASSERT (vec_len (rp->unprocessed_input) == 0);
284 vec_validate (rp->unprocessed_input, vec_len (msg_buffer) - 1);
285 clib_memcpy_fast (rp->unprocessed_input, msg_buffer,
286 vec_len (msg_buffer));
287 _vec_len (rp->unprocessed_input) = vec_len (msg_buffer);
289 _vec_len (socket_main.input_buffer) = save_input_buffer_length;
293 data_for_process = (u8 *) vec_dup (msg_buffer);
294 _vec_len (data_for_process) = (msg_len + sizeof (msgbuf_t));
295 pool_get (socket_main.process_args, a);
298 a->data = data_for_process;
300 vlib_process_signal_event (vm, vl_api_clnt_node.index,
302 a - socket_main.process_args);
303 if (n > (msg_len + sizeof (*mbp)))
304 vec_delete (msg_buffer, msg_len + sizeof (*mbp), 0);
306 _vec_len (msg_buffer) = 0;
307 n -= msg_len + sizeof (msgbuf_t);
313 _vec_len (socket_main.input_buffer) = save_input_buffer_length;
319 vl_socket_write_ready (clib_file_t * uf)
321 clib_file_main_t *fm = &file_main;
322 vl_api_registration_t *rp;
325 rp = pool_elt_at_index (socket_main.registration_pool, uf->private_data);
327 /* Flush output vector. */
328 size_t total_bytes = vec_len (rp->output_vector);
329 size_t bytes_to_send, remaining_bytes = total_bytes;
330 void *p = rp->output_vector;
331 while (remaining_bytes > 0)
333 bytes_to_send = remaining_bytes > 4096 ? 4096 : remaining_bytes;
334 n = write (uf->file_descriptor, p, bytes_to_send);
342 clib_warning ("write error, close the file...\n");
344 clib_file_del (fm, uf);
345 vl_socket_free_registration_index (rp -
346 socket_main.registration_pool);
349 remaining_bytes -= bytes_to_send;
353 vec_delete (rp->output_vector, total_bytes - remaining_bytes, 0);
354 if (vec_len (rp->output_vector) <= 0
355 && (uf->flags & UNIX_FILE_DATA_AVAILABLE_TO_WRITE))
357 uf->flags &= ~UNIX_FILE_DATA_AVAILABLE_TO_WRITE;
358 fm->file_update (uf, UNIX_FILE_UPDATE_MODIFY);
365 vl_socket_error_ready (clib_file_t * uf)
367 vl_api_registration_t *rp;
368 clib_file_main_t *fm = &file_main;
370 rp = pool_elt_at_index (socket_main.registration_pool, uf->private_data);
371 clib_file_del (fm, uf);
372 vl_socket_free_registration_index (rp - socket_main.registration_pool);
378 socksvr_file_add (clib_file_main_t * fm, int fd)
380 vl_api_registration_t *rp;
381 clib_file_t template = { 0 };
383 pool_get (socket_main.registration_pool, rp);
384 clib_memset (rp, 0, sizeof (*rp));
386 template.read_function = vl_socket_read_ready;
387 template.write_function = vl_socket_write_ready;
388 template.error_function = vl_socket_error_ready;
389 template.file_descriptor = fd;
390 template.private_data = rp - socket_main.registration_pool;
392 rp->registration_type = REGISTRATION_TYPE_SOCKET_SERVER;
393 rp->vl_api_registration_pool_index = rp - socket_main.registration_pool;
394 rp->clib_file_index = clib_file_add (fm, &template);
397 static clib_error_t *
398 socksvr_accept_ready (clib_file_t * uf)
400 clib_file_main_t *fm = &file_main;
401 socket_main_t *sm = &socket_main;
402 clib_socket_t *sock = &sm->socksvr_listen_socket;
403 clib_socket_t client;
406 error = clib_socket_accept (sock, &client);
410 socksvr_file_add (fm, client.fd);
414 static clib_error_t *
415 socksvr_bogus_write (clib_file_t * uf)
417 clib_warning ("why am I here?");
422 * vl_api_sockclnt_create_t_handler
425 vl_api_sockclnt_create_t_handler (vl_api_sockclnt_create_t * mp)
427 vl_api_registration_t *regp;
428 vl_api_sockclnt_create_reply_t *rp;
429 api_main_t *am = &api_main;
432 u32 nmsg = hash_elts (am->msg_index_by_name_and_crc);
435 regp = socket_main.current_rp;
437 ASSERT (regp->registration_type == REGISTRATION_TYPE_SOCKET_SERVER);
439 regp->name = format (0, "%s%c", mp->name, 0);
441 u32 size = sizeof (*rp) + (nmsg * sizeof (vl_api_message_table_entry_t));
442 rp = vl_msg_api_alloc_zero (size);
443 rp->_vl_msg_id = htons (VL_API_SOCKCLNT_CREATE_REPLY);
444 rp->index = htonl (sock_api_registration_handle (regp));
445 rp->context = mp->context;
446 rp->response = htonl (rv);
447 rp->count = htons (nmsg);
450 hash_foreach_pair (hp, am->msg_index_by_name_and_crc,
452 rp->message_table[i].index = htons(hp->value[0]);
453 strncpy_s((char *)rp->message_table[i].name, 64 /* bytes of space at dst */,
454 (char *)hp->key, 64-1 /* chars to copy, without zero byte. */);
458 vl_api_send_msg (regp, (u8 *) rp);
462 * vl_api_sockclnt_delete_t_handler
465 vl_api_sockclnt_delete_t_handler (vl_api_sockclnt_delete_t * mp)
467 vl_api_registration_t *regp;
468 vl_api_sockclnt_delete_reply_t *rp;
470 regp = vl_api_client_index_to_registration (mp->client_index);
474 u32 reg_index = socket_api_registration_handle_to_index (ntohl (mp->index));
475 rp = vl_msg_api_alloc (sizeof (*rp));
476 rp->_vl_msg_id = htons (VL_API_SOCKCLNT_DELETE_REPLY);
477 rp->context = mp->context;
479 if (!pool_is_free_index (socket_main.registration_pool, reg_index))
481 rp->response = htonl (1);
482 vl_api_send_msg (regp, (u8 *) rp);
484 vl_api_registration_del_file (regp);
485 vl_socket_free_registration_index (reg_index);
489 clib_warning ("unknown client ID %d", reg_index);
490 rp->response = htonl (-1);
491 vl_api_send_msg (regp, (u8 *) rp);
496 vl_sock_api_send_fd_msg (int socket_fd, int fds[], int n_fds)
498 struct msghdr mh = { 0 };
500 char ctl[CMSG_SPACE (sizeof (int) * n_fds)];
501 struct cmsghdr *cmsg;
505 iov[0].iov_base = msg;
506 iov[0].iov_len = strlen (msg);
510 clib_memset (&ctl, 0, sizeof (ctl));
511 mh.msg_control = ctl;
512 mh.msg_controllen = sizeof (ctl);
513 cmsg = CMSG_FIRSTHDR (&mh);
514 cmsg->cmsg_len = CMSG_LEN (sizeof (int) * n_fds);
515 cmsg->cmsg_level = SOL_SOCKET;
516 cmsg->cmsg_type = SCM_RIGHTS;
517 clib_memcpy_fast (CMSG_DATA (cmsg), fds, sizeof (int) * n_fds);
519 rv = sendmsg (socket_fd, &mh, 0);
521 return clib_error_return_unix (0, "sendmsg");
525 vl_api_shm_elem_config_t *
526 vl_api_make_shm_config (vl_api_sock_init_shm_t * mp)
528 vl_api_shm_elem_config_t *config = 0, *c;
534 vec_validate (config, 6);
535 config[0].type = VL_API_VLIB_RING;
536 config[0].size = 256;
537 config[0].count = 32;
539 config[1].type = VL_API_VLIB_RING;
540 config[1].size = 1024;
541 config[1].count = 16;
543 config[2].type = VL_API_VLIB_RING;
544 config[2].size = 4096;
547 config[3].type = VL_API_CLIENT_RING;
548 config[3].size = 256;
549 config[3].count = 32;
551 config[4].type = VL_API_CLIENT_RING;
552 config[4].size = 1024;
553 config[4].count = 16;
555 config[5].type = VL_API_CLIENT_RING;
556 config[5].size = 4096;
559 config[6].type = VL_API_QUEUE;
560 config[6].count = 128;
561 config[6].size = sizeof (uword);
565 vec_validate (config, mp->nitems - 1);
566 for (i = 0; i < mp->nitems; i++)
568 cfg = mp->configs[i];
569 /* Pretty much a hack but it avoids defining our own api type
571 c = (vl_api_shm_elem_config_t *) & cfg;
572 config[i].type = c->type;
573 config[i].count = c->count;
574 config[i].size = c->size;
581 * Bootstrap shm api using the socket api
584 vl_api_sock_init_shm_t_handler (vl_api_sock_init_shm_t * mp)
586 vl_api_sock_init_shm_reply_t *rmp;
587 ssvm_private_t _memfd_private, *memfd = &_memfd_private;
588 svm_map_region_args_t _args, *a = &_args;
589 vl_api_registration_t *regp;
590 api_main_t *am = &api_main;
591 svm_region_t *vlib_rp;
593 vl_api_shm_elem_config_t *config = 0;
594 vl_shmem_hdr_t *shmem_hdr;
595 int rv, tries = 1000;
597 regp = vl_api_client_index_to_registration (mp->client_index);
600 clib_warning ("API client disconnected");
603 if (regp->registration_type != REGISTRATION_TYPE_SOCKET_SERVER)
605 rv = -31; /* VNET_API_ERROR_INVALID_REGISTRATION */
610 * Set up a memfd segment of the requested size wherein the
611 * shmem data structures will be initialized
613 clib_memset (memfd, 0, sizeof (*memfd));
614 memfd->ssvm_size = mp->requested_size;
615 memfd->requested_va = 0ULL;
616 memfd->i_am_master = 1;
617 memfd->name = format (0, "%s%c", regp->name, 0);
619 if ((rv = ssvm_master_init_memfd (memfd)))
622 /* Remember to close this fd when the socket connection goes away */
623 vec_add1 (regp->additional_fds_to_close, memfd->fd);
626 * Create a plausible svm_region in the memfd backed segment
628 clib_memset (a, 0, sizeof (*a));
629 a->baseva = memfd->sh->ssvm_va + MMAP_PAGESIZE;
630 a->size = memfd->ssvm_size - MMAP_PAGESIZE;
631 /* $$$$ might want a different config parameter */
632 a->pvt_heap_size = am->api_pvt_heap_size;
633 a->flags = SVM_FLAGS_MHEAP;
634 svm_region_init_mapped_region (a, (svm_region_t *) a->baseva);
637 * Part deux, initialize the svm_region_t shared-memory header
638 * api allocation rings, and so on.
640 config = vl_api_make_shm_config (mp);
641 vlib_rp = (svm_region_t *) a->baseva;
642 vl_init_shmem (vlib_rp, config, 1 /* is_vlib (dont-care) */ ,
643 1 /* is_private */ );
645 /* Remember who created this. Needs to be post vl_init_shmem */
646 shmem_hdr = (vl_shmem_hdr_t *) vlib_rp->user_ctx;
647 shmem_hdr->clib_file_index = vl_api_registration_file_index (regp);
649 vec_add1 (am->vlib_private_rps, vlib_rp);
650 memfd->sh->ready = 1;
653 /* Recompute the set of input queues to poll in memclnt_process */
654 vec_reset_length (vl_api_queue_cursizes);
658 rmp = vl_msg_api_alloc (sizeof (*rmp));
659 rmp->_vl_msg_id = htons (VL_API_SOCK_INIT_SHM_REPLY);
660 rmp->context = mp->context;
661 rmp->retval = htonl (rv);
664 * Note: The reply message needs to make it out the back door
665 * before we send the magic fd message. That's taken care of by
668 vl_socket_api_send (regp, (u8 *) rmp);
673 /* Send the magic "here's your sign (aka fd)" socket message */
674 cf = vl_api_registration_file (regp);
676 /* Wait for reply to be consumed before sending the fd */
680 rv = ioctl (cf->file_descriptor, TIOCOUTQ, &bytes);
683 clib_unix_warning ("ioctl returned");
691 vl_sock_api_send_fd_msg (cf->file_descriptor, &memfd->fd, 1);
694 #define foreach_vlib_api_msg \
695 _(SOCKCLNT_CREATE, sockclnt_create, 1) \
696 _(SOCKCLNT_DELETE, sockclnt_delete, 1) \
697 _(SOCK_INIT_SHM, sock_init_shm, 1)
700 vl_sock_api_init (vlib_main_t * vm)
702 clib_file_main_t *fm = &file_main;
703 clib_file_t template = { 0 };
704 vl_api_registration_t *rp;
705 socket_main_t *sm = &socket_main;
706 clib_socket_t *sock = &sm->socksvr_listen_socket;
709 /* If not explicitly configured, do not bind/enable, etc. */
710 if (sm->socket_name == 0)
714 vl_msg_api_set_handlers(VL_API_##N, #n, \
715 vl_api_##n##_t_handler, \
717 vl_api_##n##_t_endian, \
718 vl_api_##n##_t_print, \
719 sizeof(vl_api_##n##_t), t);
720 foreach_vlib_api_msg;
723 vec_resize (sm->input_buffer, 4096);
725 sock->config = (char *) sm->socket_name;
726 sock->flags = CLIB_SOCKET_F_IS_SERVER | CLIB_SOCKET_F_ALLOW_GROUP_WRITE;
727 error = clib_socket_init (sock);
731 pool_get (sm->registration_pool, rp);
732 clib_memset (rp, 0, sizeof (*rp));
734 rp->registration_type = REGISTRATION_TYPE_SOCKET_LISTEN;
736 template.read_function = socksvr_accept_ready;
737 template.write_function = socksvr_bogus_write;
738 template.file_descriptor = sock->fd;
739 template.private_data = rp - sm->registration_pool;
741 rp->clib_file_index = clib_file_add (fm, &template);
745 static clib_error_t *
746 socket_exit (vlib_main_t * vm)
748 socket_main_t *sm = &socket_main;
749 vl_api_registration_t *rp;
751 /* Defensive driving in case something wipes out early */
752 if (sm->registration_pool)
756 pool_foreach (rp, sm->registration_pool, ({
757 vl_api_registration_del_file (rp);
758 index = rp->vl_api_registration_pool_index;
759 vl_socket_free_registration_index (index);
767 VLIB_MAIN_LOOP_EXIT_FUNCTION (socket_exit);
769 static clib_error_t *
770 socksvr_config (vlib_main_t * vm, unformat_input_t * input)
772 socket_main_t *sm = &socket_main;
774 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
776 if (unformat (input, "socket-name %s", &sm->socket_name))
778 /* DEPRECATE: default keyword is ignored */
779 else if (unformat (input, "default"))
783 return clib_error_return (0, "unknown input '%U'",
784 format_unformat_error, input);
788 if (!vec_len (sm->socket_name))
789 sm->socket_name = format (0, "%s/%s", vlib_unix_get_runtime_dir (),
790 API_SOCKET_FILENAME);
791 vec_terminate_c_string (sm->socket_name);
796 VLIB_CONFIG_FUNCTION (socksvr_config, "socksvr");
799 vlibsocket_reference ()
804 * fd.io coding-style-patch-verification: ON
807 * eval: (c-set-style "gnu")