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;
51 vl_sock_api_dump_clients (vlib_main_t * vm, api_main_t * am)
53 vl_api_registration_t *reg;
54 socket_main_t *sm = &socket_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 = vl_api_registration_file (reg);
71 vlib_cli_output (vm, "%20s %8d", reg->name, f->file_descriptor);
77 vl_api_registration_t *
78 vl_socket_api_client_index_to_registration (u32 index)
80 socket_main_t *sm = &socket_main;
81 if (pool_is_free_index (sm->registration_pool, ntohl (index)))
84 clib_warning ("Invalid index %d\n", ntohl (index));
88 return pool_elt_at_index (sm->registration_pool, ntohl (index));
92 vl_socket_api_send (vl_api_registration_t * rp, u8 * elem)
97 socket_main_t *sm = &socket_main;
98 u16 msg_id = ntohs (*(u16 *) elem);
99 api_main_t *am = &api_main;
100 msgbuf_t *mb = (msgbuf_t *) (elem - offsetof (msgbuf_t, data));
101 vl_api_registration_t *sock_rp;
102 clib_file_main_t *fm = &file_main;
106 cf = vl_api_registration_file (rp);
107 ASSERT (rp->registration_type > REGISTRATION_TYPE_SHMEM);
109 if (msg_id >= vec_len (am->api_trace_cfg))
111 clib_warning ("id out of range: %d", msg_id);
112 vl_msg_api_free ((void *) elem);
116 sock_rp = pool_elt_at_index (sm->registration_pool,
117 rp->vl_api_registration_pool_index);
120 /* Add the msgbuf_t to the output vector */
121 vec_add (sock_rp->output_vector, (u8 *) mb, sizeof (*mb));
123 /* Try to send the message and save any error like
124 * we do in the input epoll loop */
125 vec_add (sock_rp->output_vector, elem, ntohl (mb->data_len));
126 error = clib_file_write (cf);
127 unix_save_error (&unix_main, error);
129 /* If we didn't finish sending everything, wait for tx space */
130 if (vec_len (sock_rp->output_vector) > 0
131 && !(cf->flags & UNIX_FILE_DATA_AVAILABLE_TO_WRITE))
133 cf->flags |= UNIX_FILE_DATA_AVAILABLE_TO_WRITE;
134 fm->file_update (cf, UNIX_FILE_UPDATE_MODIFY);
138 output_length = sizeof (*mb) + ntohl (mb->data_len);
139 clib_warning ("wrote %u bytes to fd %d", output_length,
140 cf->file_descriptor);
143 vl_msg_api_free ((void *) elem);
147 vl_socket_free_registration_index (u32 pool_index)
150 vl_api_registration_t *rp;
151 if (pool_is_free_index (socket_main.registration_pool, pool_index))
153 clib_warning ("main pool index %d already free", pool_index);
156 rp = pool_elt_at_index (socket_main.registration_pool, pool_index);
158 ASSERT (rp->registration_type != REGISTRATION_TYPE_FREE);
159 for (i = 0; i < vec_len (rp->additional_fds_to_close); i++)
160 if (close (rp->additional_fds_to_close[i]) < 0)
161 clib_unix_warning ("close");
162 vec_free (rp->additional_fds_to_close);
164 vec_free (rp->unprocessed_input);
165 vec_free (rp->output_vector);
166 rp->registration_type = REGISTRATION_TYPE_FREE;
167 pool_put (socket_main.registration_pool, rp);
171 vl_socket_process_api_msg (clib_file_t * uf, vl_api_registration_t * rp,
174 msgbuf_t *mbp = (msgbuf_t *) input_v;
176 u8 *the_msg = (u8 *) (mbp->data);
177 socket_main.current_uf = uf;
178 socket_main.current_rp = rp;
179 vl_msg_api_socket_handler (the_msg);
180 socket_main.current_uf = 0;
181 socket_main.current_rp = 0;
185 vl_socket_read_ready (clib_file_t * uf)
187 clib_file_main_t *fm = &file_main;
188 vlib_main_t *vm = vlib_get_main ();
189 vl_api_registration_t *rp;
192 u8 *data_for_process;
194 u32 save_input_buffer_length = vec_len (socket_main.input_buffer);
195 vl_socket_args_for_process_t *a;
199 rp = pool_elt_at_index (socket_main.registration_pool, uf->private_data);
201 n = read (uf->file_descriptor, socket_main.input_buffer,
202 vec_len (socket_main.input_buffer));
204 if (n <= 0 && errno != EAGAIN)
206 clib_file_del (fm, uf);
208 if (!pool_is_free (socket_main.registration_pool, rp))
210 u32 index = rp - socket_main.registration_pool;
211 vl_socket_free_registration_index (index);
215 clib_warning ("client index %d already free?",
216 rp->vl_api_registration_pool_index);
221 _vec_len (socket_main.input_buffer) = n;
224 * Look for bugs here. This code is tricky because
225 * data read from a stream socket does not honor message
226 * boundaries. In the case of a long message (>4K bytes)
227 * we have to do (at least) 2 reads, etc.
231 if (vec_len (rp->unprocessed_input))
233 vec_append (rp->unprocessed_input, socket_main.input_buffer);
234 msg_buffer = rp->unprocessed_input;
238 msg_buffer = socket_main.input_buffer;
244 /* Any chance that we have a complete message? */
245 if (vec_len (msg_buffer) <= sizeof (msgbuf_t))
248 mbp = (msgbuf_t *) msg_buffer;
249 msg_len = ntohl (mbp->data_len);
253 /* We don't have the entire message yet. */
255 || (msg_len + sizeof (msgbuf_t)) > vec_len (msg_buffer))
258 /* if we were using the input buffer save the fragment */
259 if (msg_buffer == socket_main.input_buffer)
261 ASSERT (vec_len (rp->unprocessed_input) == 0);
262 vec_validate (rp->unprocessed_input, vec_len (msg_buffer) - 1);
263 clib_memcpy (rp->unprocessed_input, msg_buffer,
264 vec_len (msg_buffer));
265 _vec_len (rp->unprocessed_input) = vec_len (msg_buffer);
267 _vec_len (socket_main.input_buffer) = save_input_buffer_length;
271 data_for_process = (u8 *) vec_dup (msg_buffer);
272 _vec_len (data_for_process) = (msg_len + sizeof (msgbuf_t));
273 pool_get (socket_main.process_args, a);
276 a->data = data_for_process;
278 vlib_process_signal_event (vm, vl_api_clnt_node.index,
280 a - socket_main.process_args);
281 if (n > (msg_len + sizeof (*mbp)))
282 vec_delete (msg_buffer, msg_len + sizeof (*mbp), 0);
284 _vec_len (msg_buffer) = 0;
285 n -= msg_len + sizeof (msgbuf_t);
291 _vec_len (socket_main.input_buffer) = save_input_buffer_length;
297 vl_socket_write_ready (clib_file_t * uf)
299 clib_file_main_t *fm = &file_main;
300 vl_api_registration_t *rp;
303 rp = pool_elt_at_index (socket_main.registration_pool, uf->private_data);
305 /* Flush output vector. */
306 size_t total_bytes = vec_len (rp->output_vector);
307 size_t bytes_to_send, remaining_bytes = total_bytes;
308 void *p = rp->output_vector;
309 while (remaining_bytes > 0)
311 bytes_to_send = remaining_bytes > 4096 ? 4096 : remaining_bytes;
312 n = write (uf->file_descriptor, p, bytes_to_send);
320 clib_warning ("write error, close the file...\n");
322 clib_file_del (fm, uf);
323 vl_socket_free_registration_index (rp -
324 socket_main.registration_pool);
327 remaining_bytes -= bytes_to_send;
331 vec_delete (rp->output_vector, total_bytes - remaining_bytes, 0);
332 if (vec_len (rp->output_vector) <= 0
333 && (uf->flags & UNIX_FILE_DATA_AVAILABLE_TO_WRITE))
335 uf->flags &= ~UNIX_FILE_DATA_AVAILABLE_TO_WRITE;
336 fm->file_update (uf, UNIX_FILE_UPDATE_MODIFY);
343 vl_socket_error_ready (clib_file_t * uf)
345 vl_api_registration_t *rp;
346 clib_file_main_t *fm = &file_main;
348 rp = pool_elt_at_index (socket_main.registration_pool, uf->private_data);
349 clib_file_del (fm, uf);
350 vl_socket_free_registration_index (rp - socket_main.registration_pool);
356 socksvr_file_add (clib_file_main_t * fm, int fd)
358 vl_api_registration_t *rp;
359 clib_file_t template = { 0 };
361 pool_get (socket_main.registration_pool, rp);
362 memset (rp, 0, sizeof (*rp));
364 template.read_function = vl_socket_read_ready;
365 template.write_function = vl_socket_write_ready;
366 template.error_function = vl_socket_error_ready;
367 template.file_descriptor = fd;
368 template.private_data = rp - socket_main.registration_pool;
370 rp->registration_type = REGISTRATION_TYPE_SOCKET_SERVER;
371 rp->vl_api_registration_pool_index = rp - socket_main.registration_pool;
372 rp->clib_file_index = clib_file_add (fm, &template);
375 static clib_error_t *
376 socksvr_accept_ready (clib_file_t * uf)
378 clib_file_main_t *fm = &file_main;
379 socket_main_t *sm = &socket_main;
380 clib_socket_t *sock = &sm->socksvr_listen_socket;
381 clib_socket_t client;
384 error = clib_socket_accept (sock, &client);
388 socksvr_file_add (fm, client.fd);
392 static clib_error_t *
393 socksvr_bogus_write (clib_file_t * uf)
395 clib_warning ("why am I here?");
400 * vl_api_sockclnt_create_t_handler
403 vl_api_sockclnt_create_t_handler (vl_api_sockclnt_create_t * mp)
405 vl_api_registration_t *regp;
406 vl_api_sockclnt_create_reply_t *rp;
407 api_main_t *am = &api_main;
410 u32 nmsg = hash_elts (am->msg_index_by_name_and_crc);
413 regp = socket_main.current_rp;
415 ASSERT (regp->registration_type == REGISTRATION_TYPE_SOCKET_SERVER);
417 regp->name = format (0, "%s%c", mp->name, 0);
419 u32 size = sizeof (*rp) + (nmsg * sizeof (vl_api_message_table_entry_t));
420 rp = vl_msg_api_alloc (size);
421 rp->_vl_msg_id = htons (VL_API_SOCKCLNT_CREATE_REPLY);
422 rp->index = htonl (regp->vl_api_registration_pool_index);
423 rp->context = mp->context;
424 rp->response = htonl (rv);
425 rp->count = htons (nmsg);
428 hash_foreach_pair (hp, am->msg_index_by_name_and_crc,
430 rp->message_table[i].index = htons(hp->value[0]);
431 strncpy((char *)rp->message_table[i].name, (char *)hp->key, 64-1);
435 vl_api_send_msg (regp, (u8 *) rp);
439 * vl_api_sockclnt_delete_t_handler
442 vl_api_sockclnt_delete_t_handler (vl_api_sockclnt_delete_t * mp)
444 vl_api_registration_t *regp;
445 vl_api_sockclnt_delete_reply_t *rp;
447 regp = vl_api_client_index_to_registration (mp->client_index);
451 u32 reg_index = ntohl (mp->index);
452 rp = vl_msg_api_alloc (sizeof (*rp));
453 rp->_vl_msg_id = htons (VL_API_SOCKCLNT_DELETE_REPLY);
454 rp->context = mp->context;
456 if (!pool_is_free_index (socket_main.registration_pool, reg_index))
458 rp->response = htonl (1);
459 vl_api_send_msg (regp, (u8 *) rp);
461 vl_api_registration_del_file (regp);
462 vl_socket_free_registration_index (reg_index);
466 clib_warning ("unknown client ID %d", reg_index);
467 rp->response = htonl (-1);
468 vl_api_send_msg (regp, (u8 *) rp);
473 vl_sock_api_send_fd_msg (int socket_fd, int fds[], int n_fds)
475 struct msghdr mh = { 0 };
477 char ctl[CMSG_SPACE (sizeof (int) * n_fds)];
478 struct cmsghdr *cmsg;
482 iov[0].iov_base = msg;
483 iov[0].iov_len = strlen (msg);
487 memset (&ctl, 0, sizeof (ctl));
488 mh.msg_control = ctl;
489 mh.msg_controllen = sizeof (ctl);
490 cmsg = CMSG_FIRSTHDR (&mh);
491 cmsg->cmsg_len = CMSG_LEN (sizeof (int) * n_fds);
492 cmsg->cmsg_level = SOL_SOCKET;
493 cmsg->cmsg_type = SCM_RIGHTS;
494 clib_memcpy (CMSG_DATA (cmsg), fds, sizeof (int) * n_fds);
496 rv = sendmsg (socket_fd, &mh, 0);
498 return clib_error_return_unix (0, "sendmsg");
502 vl_api_shm_elem_config_t *
503 vl_api_make_shm_config (vl_api_sock_init_shm_t * mp)
505 vl_api_shm_elem_config_t *config = 0, *c;
511 vec_validate (config, 6);
512 config[0].type = VL_API_VLIB_RING;
513 config[0].size = 256;
514 config[0].count = 32;
516 config[1].type = VL_API_VLIB_RING;
517 config[1].size = 1024;
518 config[1].count = 16;
520 config[2].type = VL_API_VLIB_RING;
521 config[2].size = 4096;
524 config[3].type = VL_API_CLIENT_RING;
525 config[3].size = 256;
526 config[3].count = 32;
528 config[4].type = VL_API_CLIENT_RING;
529 config[4].size = 1024;
530 config[4].count = 16;
532 config[5].type = VL_API_CLIENT_RING;
533 config[5].size = 4096;
536 config[6].type = VL_API_QUEUE;
537 config[6].count = 128;
538 config[6].size = sizeof (uword);
542 vec_validate (config, mp->nitems - 1);
543 for (i = 0; i < mp->nitems; i++)
545 cfg = mp->configs[i];
546 /* Pretty much a hack but it avoids defining our own api type
548 c = (vl_api_shm_elem_config_t *) & cfg;
549 config[i].type = c->type;
550 config[i].count = c->count;
551 config[i].size = c->size;
558 * Bootstrap shm api using the socket api
561 vl_api_sock_init_shm_t_handler (vl_api_sock_init_shm_t * mp)
563 vl_api_sock_init_shm_reply_t *rmp;
564 ssvm_private_t _memfd_private, *memfd = &_memfd_private;
565 svm_map_region_args_t _args, *a = &_args;
566 vl_api_registration_t *regp;
567 api_main_t *am = &api_main;
568 svm_region_t *vlib_rp;
570 vl_api_shm_elem_config_t *config = 0;
571 vl_shmem_hdr_t *shmem_hdr;
574 regp = vl_api_client_index_to_registration (mp->client_index);
577 clib_warning ("API client disconnected");
580 if (regp->registration_type != REGISTRATION_TYPE_SOCKET_SERVER)
582 rv = -31; /* VNET_API_ERROR_INVALID_REGISTRATION */
587 * Set up a memfd segment of the requested size wherein the
588 * shmem data structures will be initialized
590 memset (memfd, 0, sizeof (*memfd));
591 memfd->ssvm_size = mp->requested_size;
592 memfd->requested_va = 0ULL;
593 memfd->i_am_master = 1;
594 memfd->name = format (0, "%s%c", regp->name, 0);
596 if ((rv = ssvm_master_init_memfd (memfd)))
599 /* Remember to close this fd when the socket connection goes away */
600 vec_add1 (regp->additional_fds_to_close, memfd->fd);
603 * Create a plausible svm_region in the memfd backed segment
605 memset (a, 0, sizeof (*a));
606 a->baseva = memfd->sh->ssvm_va + MMAP_PAGESIZE;
607 a->size = memfd->ssvm_size - MMAP_PAGESIZE;
608 /* $$$$ might want a different config parameter */
609 a->pvt_heap_size = am->api_pvt_heap_size;
610 a->flags = SVM_FLAGS_MHEAP;
611 svm_region_init_mapped_region (a, (svm_region_t *) a->baseva);
614 * Part deux, initialize the svm_region_t shared-memory header
615 * api allocation rings, and so on.
617 config = vl_api_make_shm_config (mp);
618 vlib_rp = (svm_region_t *) a->baseva;
619 vl_init_shmem (vlib_rp, config, 1 /* is_vlib (dont-care) */ ,
620 1 /* is_private */ );
622 /* Remember who created this. Needs to be post vl_init_shmem */
623 shmem_hdr = (vl_shmem_hdr_t *) vlib_rp->user_ctx;
624 shmem_hdr->clib_file_index = vl_api_registration_file_index (regp);
626 vec_add1 (am->vlib_private_rps, vlib_rp);
627 memfd->sh->ready = 1;
630 /* Recompute the set of input queues to poll in memclnt_process */
631 vec_reset_length (vl_api_queue_cursizes);
635 rmp = vl_msg_api_alloc (sizeof (*rmp));
636 rmp->_vl_msg_id = htons (VL_API_SOCK_INIT_SHM_REPLY);
637 rmp->context = mp->context;
638 rmp->retval = htonl (rv);
641 * Note: The reply message needs to make it out the back door
642 * before we send the magic fd message. That's taken care of by
645 vl_socket_api_send (regp, (u8 *) rmp);
650 /* Send the magic "here's your sign (aka fd)" socket message */
651 cf = vl_api_registration_file (regp);
652 vl_sock_api_send_fd_msg (cf->file_descriptor, &memfd->fd, 1);
655 #define foreach_vlib_api_msg \
656 _(SOCKCLNT_CREATE, sockclnt_create) \
657 _(SOCKCLNT_DELETE, sockclnt_delete) \
658 _(SOCK_INIT_SHM, sock_init_shm)
661 vl_sock_api_init (vlib_main_t * vm)
663 clib_file_main_t *fm = &file_main;
664 clib_file_t template = { 0 };
665 vl_api_registration_t *rp;
666 socket_main_t *sm = &socket_main;
667 clib_socket_t *sock = &sm->socksvr_listen_socket;
670 /* If not explicitly configured, do not bind/enable, etc. */
671 if (sm->socket_name == 0)
675 vl_msg_api_set_handlers(VL_API_##N, #n, \
676 vl_api_##n##_t_handler, \
678 vl_api_##n##_t_endian, \
679 vl_api_##n##_t_print, \
680 sizeof(vl_api_##n##_t), 1);
681 foreach_vlib_api_msg;
684 vec_resize (sm->input_buffer, 4096);
686 sock->config = (char *) sm->socket_name;
688 /* mkdir of file socket, only under /run */
689 if (strncmp (sock->config, "/run", 4) == 0)
691 u8 *tmp = format (0, "%s", sock->config);
692 int i = vec_len (tmp);
693 while (i && tmp[--i] != '/')
699 vlib_unix_recursive_mkdir ((char *) tmp);
703 sock->flags = CLIB_SOCKET_F_IS_SERVER | CLIB_SOCKET_F_SEQPACKET |
704 CLIB_SOCKET_F_ALLOW_GROUP_WRITE;
705 error = clib_socket_init (sock);
709 pool_get (sm->registration_pool, rp);
710 memset (rp, 0, sizeof (*rp));
712 rp->registration_type = REGISTRATION_TYPE_SOCKET_LISTEN;
714 template.read_function = socksvr_accept_ready;
715 template.write_function = socksvr_bogus_write;
716 template.file_descriptor = sock->fd;
717 template.private_data = rp - sm->registration_pool;
719 rp->clib_file_index = clib_file_add (fm, &template);
723 static clib_error_t *
724 socket_exit (vlib_main_t * vm)
726 socket_main_t *sm = &socket_main;
727 vl_api_registration_t *rp;
729 /* Defensive driving in case something wipes out early */
730 if (sm->registration_pool)
734 pool_foreach (rp, sm->registration_pool, ({
735 vl_api_registration_del_file (rp);
736 index = rp->vl_api_registration_pool_index;
737 vl_socket_free_registration_index (index);
745 VLIB_MAIN_LOOP_EXIT_FUNCTION (socket_exit);
747 static clib_error_t *
748 socksvr_config (vlib_main_t * vm, unformat_input_t * input)
750 socket_main_t *sm = &socket_main;
752 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
754 if (unformat (input, "socket-name %s", &sm->socket_name))
756 else if (unformat (input, "default"))
758 sm->socket_name = format (0, "%s%c", API_SOCKET_FILE, 0);
762 return clib_error_return (0, "unknown input '%U'",
763 format_unformat_error, input);
769 VLIB_CONFIG_FUNCTION (socksvr_config, "socksvr");
772 vlibsocket_init (vlib_main_t * vm)
777 VLIB_INIT_FUNCTION (vlibsocket_init);
780 * fd.io coding-style-patch-verification: ON
783 * eval: (c-set-style "gnu")