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);
78 vl_socket_api_send (vl_api_registration_t * rp, u8 * elem)
83 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));
87 vl_api_registration_t *sock_rp;
88 clib_file_main_t *fm = &file_main;
92 cf = vl_api_registration_file (rp);
93 ASSERT (rp->registration_type > REGISTRATION_TYPE_SHMEM);
95 if (msg_id >= vec_len (am->api_trace_cfg))
97 clib_warning ("id out of range: %d", msg_id);
98 vl_msg_api_free ((void *) elem);
102 sock_rp = pool_elt_at_index (sm->registration_pool,
103 rp->vl_api_registration_pool_index);
106 /* Add the msgbuf_t to the output vector */
107 vec_add (sock_rp->output_vector, (u8 *) mb, sizeof (*mb));
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);
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))
119 cf->flags |= UNIX_FILE_DATA_AVAILABLE_TO_WRITE;
120 fm->file_update (cf, UNIX_FILE_UPDATE_MODIFY);
124 output_length = sizeof (*mb) + ntohl (mb->data_len);
125 clib_warning ("wrote %u bytes to fd %d", output_length,
126 cf->file_descriptor);
129 vl_msg_api_free ((void *) elem);
133 vl_socket_free_registration_index (u32 pool_index)
136 vl_api_registration_t *rp;
137 if (pool_is_free_index (socket_main.registration_pool, pool_index))
139 clib_warning ("main pool index %d already free", pool_index);
142 rp = pool_elt_at_index (socket_main.registration_pool, pool_index);
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);
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);
157 vl_socket_process_api_msg (clib_file_t * uf, vl_api_registration_t * rp,
160 msgbuf_t *mbp = (msgbuf_t *) input_v;
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;
171 vl_socket_read_ready (clib_file_t * uf)
173 clib_file_main_t *fm = &file_main;
174 vlib_main_t *vm = vlib_get_main ();
175 vl_api_registration_t *rp;
178 u8 *data_for_process;
180 u32 save_input_buffer_length = vec_len (socket_main.input_buffer);
181 vl_socket_args_for_process_t *a;
185 rp = pool_elt_at_index (socket_main.registration_pool, uf->private_data);
187 n = read (uf->file_descriptor, socket_main.input_buffer,
188 vec_len (socket_main.input_buffer));
190 if (n <= 0 && errno != EAGAIN)
192 clib_file_del (fm, uf);
194 if (!pool_is_free (socket_main.registration_pool, rp))
196 u32 index = rp - socket_main.registration_pool;
197 vl_socket_free_registration_index (index);
201 clib_warning ("client index %d already free?",
202 rp->vl_api_registration_pool_index);
207 _vec_len (socket_main.input_buffer) = n;
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.
217 if (vec_len (rp->unprocessed_input))
219 vec_append (rp->unprocessed_input, socket_main.input_buffer);
220 msg_buffer = rp->unprocessed_input;
224 msg_buffer = socket_main.input_buffer;
230 /* Any chance that we have a complete message? */
231 if (vec_len (msg_buffer) <= sizeof (msgbuf_t))
234 mbp = (msgbuf_t *) msg_buffer;
235 msg_len = ntohl (mbp->data_len);
239 /* We don't have the entire message yet. */
241 || (msg_len + sizeof (msgbuf_t)) > vec_len (msg_buffer))
244 /* if we were using the input buffer save the fragment */
245 if (msg_buffer == socket_main.input_buffer)
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);
253 _vec_len (socket_main.input_buffer) = save_input_buffer_length;
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);
262 a->data = data_for_process;
264 vlib_process_signal_event (vm, vl_api_clnt_node.index,
266 a - socket_main.process_args);
267 if (n > (msg_len + sizeof (*mbp)))
268 vec_delete (msg_buffer, msg_len + sizeof (*mbp), 0);
270 _vec_len (msg_buffer) = 0;
271 n -= msg_len + sizeof (msgbuf_t);
277 _vec_len (socket_main.input_buffer) = save_input_buffer_length;
283 vl_socket_write_ready (clib_file_t * uf)
285 clib_file_main_t *fm = &file_main;
286 vl_api_registration_t *rp;
289 rp = pool_elt_at_index (socket_main.registration_pool, uf->private_data);
291 /* Flush output vector. */
292 n = write (uf->file_descriptor, rp->output_vector,
293 vec_len (rp->output_vector));
297 clib_warning ("write error, close the file...\n");
299 clib_file_del (fm, uf);
300 vl_socket_free_registration_index (rp - socket_main.registration_pool);
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))
309 uf->flags &= ~UNIX_FILE_DATA_AVAILABLE_TO_WRITE;
310 fm->file_update (uf, UNIX_FILE_UPDATE_MODIFY);
318 vl_socket_error_ready (clib_file_t * uf)
320 vl_api_registration_t *rp;
321 clib_file_main_t *fm = &file_main;
323 rp = pool_elt_at_index (socket_main.registration_pool, uf->private_data);
324 clib_file_del (fm, uf);
325 vl_socket_free_registration_index (rp - socket_main.registration_pool);
331 socksvr_file_add (clib_file_main_t * fm, int fd)
333 vl_api_registration_t *rp;
334 clib_file_t template = { 0 };
336 pool_get (socket_main.registration_pool, rp);
337 memset (rp, 0, sizeof (*rp));
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;
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);
350 static clib_error_t *
351 socksvr_accept_ready (clib_file_t * uf)
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;
359 error = clib_socket_accept (sock, &client);
363 socksvr_file_add (fm, client.fd);
367 static clib_error_t *
368 socksvr_bogus_write (clib_file_t * uf)
370 clib_warning ("why am I here?");
375 * vl_api_sockclnt_create_t_handler
378 vl_api_sockclnt_create_t_handler (vl_api_sockclnt_create_t * mp)
380 vl_api_registration_t *regp;
381 vl_api_sockclnt_create_reply_t *rp;
384 regp = socket_main.current_rp;
386 ASSERT (regp->registration_type == REGISTRATION_TYPE_SOCKET_SERVER);
388 regp->name = format (0, "%s%c", mp->name, 0);
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);
397 vl_api_send_msg (regp, (u8 *) rp);
401 * vl_api_sockclnt_delete_t_handler
404 vl_api_sockclnt_delete_t_handler (vl_api_sockclnt_delete_t * mp)
406 vl_api_registration_t *regp;
407 vl_api_sockclnt_delete_reply_t *rp;
409 if (!pool_is_free_index (socket_main.registration_pool, mp->index))
411 regp = pool_elt_at_index (socket_main.registration_pool, mp->index);
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);
418 vl_api_send_msg (regp, (u8 *) rp);
420 vl_api_registration_del_file (regp);
421 vl_socket_free_registration_index (mp->index);
425 clib_warning ("unknown client ID %d", mp->index);
430 vl_sock_api_send_fd_msg (int socket_fd, int fds[], int n_fds)
432 struct msghdr mh = { 0 };
434 char ctl[CMSG_SPACE (sizeof (int) * n_fds)];
435 struct cmsghdr *cmsg;
439 iov[0].iov_base = msg;
440 iov[0].iov_len = strlen (msg);
444 memset (&ctl, 0, sizeof (ctl));
445 mh.msg_control = ctl;
446 mh.msg_controllen = sizeof (ctl);
447 cmsg = CMSG_FIRSTHDR (&mh);
448 cmsg->cmsg_len = CMSG_LEN (sizeof (int) * n_fds);
449 cmsg->cmsg_level = SOL_SOCKET;
450 cmsg->cmsg_type = SCM_RIGHTS;
451 clib_memcpy (CMSG_DATA (cmsg), fds, sizeof (int) * n_fds);
453 rv = sendmsg (socket_fd, &mh, 0);
455 return clib_error_return_unix (0, "sendmsg");
459 vl_api_shm_elem_config_t *
460 vl_api_make_shm_config (vl_api_sock_init_shm_t * mp)
462 vl_api_shm_elem_config_t *config = 0, *c;
468 vec_validate (config, 6);
469 config[0].type = VL_API_VLIB_RING;
470 config[0].size = 256;
471 config[0].count = 32;
473 config[1].type = VL_API_VLIB_RING;
474 config[1].size = 1024;
475 config[1].count = 16;
477 config[2].type = VL_API_VLIB_RING;
478 config[2].size = 4096;
481 config[3].type = VL_API_CLIENT_RING;
482 config[3].size = 256;
483 config[3].count = 32;
485 config[4].type = VL_API_CLIENT_RING;
486 config[4].size = 1024;
487 config[4].count = 16;
489 config[5].type = VL_API_CLIENT_RING;
490 config[5].size = 4096;
493 config[6].type = VL_API_QUEUE;
494 config[6].count = 128;
495 config[6].size = sizeof (uword);
499 vec_validate (config, mp->nitems - 1);
500 for (i = 0; i < mp->nitems; i++)
502 cfg = mp->configs[i];
503 /* Pretty much a hack but it avoids defining our own api type
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;
515 * Bootstrap shm api using the socket api
518 vl_api_sock_init_shm_t_handler (vl_api_sock_init_shm_t * mp)
520 vl_api_sock_init_shm_reply_t *rmp;
521 ssvm_private_t _memfd_private, *memfd = &_memfd_private;
522 svm_map_region_args_t _args, *a = &_args;
523 vl_api_registration_t *regp;
524 api_main_t *am = &api_main;
525 svm_region_t *vlib_rp;
527 vl_api_shm_elem_config_t *config = 0;
528 vl_shmem_hdr_t *shmem_hdr;
531 regp = vl_api_client_index_to_registration (mp->client_index);
534 clib_warning ("API client disconnected");
537 if (regp->registration_type != REGISTRATION_TYPE_SOCKET_SERVER)
539 rv = -31; /* VNET_API_ERROR_INVALID_REGISTRATION */
544 * Set up a memfd segment of the requested size wherein the
545 * shmem data structures will be initialized
547 memset (memfd, 0, sizeof (*memfd));
548 memfd->ssvm_size = mp->requested_size;
549 memfd->requested_va = 0ULL;
550 memfd->i_am_master = 1;
551 memfd->name = format (0, "%s%c", regp->name, 0);
553 if ((rv = ssvm_master_init_memfd (memfd)))
556 /* Remember to close this fd when the socket connection goes away */
557 vec_add1 (regp->additional_fds_to_close, memfd->fd);
560 * Create a plausible svm_region in the memfd backed segment
562 memset (a, 0, sizeof (*a));
563 a->baseva = memfd->sh->ssvm_va + MMAP_PAGESIZE;
564 a->size = memfd->ssvm_size - MMAP_PAGESIZE;
565 /* $$$$ 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);
571 * Part deux, initialize the svm_region_t shared-memory header
572 * api allocation rings, and so on.
574 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 */ );
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);
583 vec_add1 (am->vlib_private_rps, vlib_rp);
584 memfd->sh->ready = 1;
587 /* Recompute the set of input queues to poll in memclnt_process */
588 vec_reset_length (vl_api_queue_cursizes);
592 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);
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
602 vl_socket_api_send (regp, (u8 *) rmp);
607 /* Send the magic "here's your sign (aka fd)" socket message */
608 cf = vl_api_registration_file (regp);
609 vl_sock_api_send_fd_msg (cf->file_descriptor, &memfd->fd, 1);
612 #define foreach_vlib_api_msg \
613 _(SOCKCLNT_CREATE, sockclnt_create) \
614 _(SOCKCLNT_DELETE, sockclnt_delete) \
615 _(SOCK_INIT_SHM, sock_init_shm)
618 vl_sock_api_init (vlib_main_t * vm)
620 clib_file_main_t *fm = &file_main;
621 clib_file_t template = { 0 };
622 vl_api_registration_t *rp;
623 socket_main_t *sm = &socket_main;
624 clib_socket_t *sock = &sm->socksvr_listen_socket;
627 /* If not explicitly configured, do not bind/enable, etc. */
628 if (sm->socket_name == 0)
632 vl_msg_api_set_handlers(VL_API_##N, #n, \
633 vl_api_##n##_t_handler, \
635 vl_api_##n##_t_endian, \
636 vl_api_##n##_t_print, \
637 sizeof(vl_api_##n##_t), 1);
638 foreach_vlib_api_msg;
641 vec_resize (sm->input_buffer, 4096);
643 sock->config = (char *) sm->socket_name;
645 /* mkdir of file socket, only under /run */
646 if (strncmp (sock->config, "/run", 4) == 0)
648 u8 *tmp = format (0, "%s", sock->config);
649 int i = vec_len (tmp);
650 while (i && tmp[--i] != '/')
656 vlib_unix_recursive_mkdir ((char *) tmp);
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);
666 pool_get (sm->registration_pool, rp);
667 memset (rp, 0, sizeof (*rp));
669 rp->registration_type = REGISTRATION_TYPE_SOCKET_LISTEN;
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;
676 rp->clib_file_index = clib_file_add (fm, &template);
680 static clib_error_t *
681 socket_exit (vlib_main_t * vm)
683 socket_main_t *sm = &socket_main;
684 vl_api_registration_t *rp;
686 /* Defensive driving in case something wipes out early */
687 if (sm->registration_pool)
691 pool_foreach (rp, sm->registration_pool, ({
692 vl_api_registration_del_file (rp);
693 index = rp->vl_api_registration_pool_index;
694 vl_socket_free_registration_index (index);
702 VLIB_MAIN_LOOP_EXIT_FUNCTION (socket_exit);
704 static clib_error_t *
705 socksvr_config (vlib_main_t * vm, unformat_input_t * input)
707 socket_main_t *sm = &socket_main;
709 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
711 if (unformat (input, "socket-name %s", &sm->socket_name))
713 else if (unformat (input, "default"))
715 sm->socket_name = format (0, "%s%c", API_SOCKET_FILE, 0);
719 return clib_error_return (0, "unknown input '%U'",
720 format_unformat_error, input);
726 VLIB_CONFIG_FUNCTION (socksvr_config, "socksvr");
729 vlibsocket_init (vlib_main_t * vm)
734 VLIB_INIT_FUNCTION (vlibsocket_init);
737 * fd.io coding-style-patch-verification: ON
740 * eval: (c-set-style "gnu")