2 *------------------------------------------------------------------
3 * socket_client.c - API message handling over sockets, client code.
5 * Copyright (c) 2017 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 *------------------------------------------------------------------
23 #include <sys/socket.h>
27 #include <sys/types.h>
28 #include <sys/param.h>
29 #include <sys/ucred.h>
31 #endif /* __FreeBSD__ */
34 #include <vlibmemory/socket_client.h>
35 #include <vlibmemory/memory_client.h>
37 #include <vlibmemory/vl_memory_msg_enum.h>
39 #define vl_typedefs /* define message structures */
40 #include <vlibmemory/vl_memory_api_h.h>
43 #define vl_endianfun /* define message structures */
44 #include <vlibmemory/vl_memory_api_h.h>
47 #define vl_calcsizefun
48 #include <vlibmemory/vl_memory_api_h.h>
51 /* instantiate all the print functions we know about */
53 #include <vlibmemory/vl_memory_api_h.h>
56 socket_client_main_t socket_client_main;
57 __thread socket_client_main_t *socket_client_ctx = &socket_client_main;
60 u32 vl (void *p) __attribute__ ((weak));
68 static socket_client_main_t *
69 vl_socket_client_ctx_push (socket_client_main_t * ctx)
71 socket_client_main_t *old = socket_client_ctx;
72 socket_client_ctx = ctx;
77 vl_socket_client_ctx_pop (socket_client_main_t * old_ctx)
79 socket_client_ctx = old_ctx;
83 vl_socket_client_read_internal (socket_client_main_t * scm, int wait)
85 u32 data_len = 0, msg_size;
86 int n, current_rx_index;
90 if (scm->socket_fd == 0)
94 timeout = clib_time_now (&scm->clib_time) + wait;
98 current_rx_index = vec_len (scm->socket_rx_buffer);
99 while (current_rx_index < sizeof (*mbp))
101 vec_validate (scm->socket_rx_buffer, current_rx_index
102 + scm->socket_buffer_size - 1);
103 n = read (scm->socket_fd, scm->socket_rx_buffer + current_rx_index,
104 scm->socket_buffer_size);
110 clib_unix_warning ("socket_read");
111 vec_set_len (scm->socket_rx_buffer, current_rx_index);
114 current_rx_index += n;
116 vec_set_len (scm->socket_rx_buffer, current_rx_index);
120 clib_warning ("read %d bytes", n);
123 mbp = (msgbuf_t *) (scm->socket_rx_buffer);
124 data_len = ntohl (mbp->data_len);
125 current_rx_index = vec_len (scm->socket_rx_buffer);
126 vec_validate (scm->socket_rx_buffer, current_rx_index + data_len);
127 mbp = (msgbuf_t *) (scm->socket_rx_buffer);
128 msg_size = data_len + sizeof (*mbp);
130 while (current_rx_index < msg_size)
132 n = read (scm->socket_fd, scm->socket_rx_buffer + current_rx_index,
133 msg_size - current_rx_index);
139 clib_unix_warning ("socket_read");
140 vec_set_len (scm->socket_rx_buffer, current_rx_index);
143 current_rx_index += n;
145 vec_set_len (scm->socket_rx_buffer, current_rx_index);
147 if (vec_len (scm->socket_rx_buffer) >= data_len + sizeof (*mbp))
149 vl_msg_api_socket_handler ((void *) (mbp->data), data_len);
151 if (vec_len (scm->socket_rx_buffer) == data_len + sizeof (*mbp))
152 vec_set_len (scm->socket_rx_buffer, 0);
154 vec_delete (scm->socket_rx_buffer, data_len + sizeof (*mbp), 0);
157 /* Quit if we're out of data, and not expecting a ping reply */
158 if (vec_len (scm->socket_rx_buffer) == 0
159 && scm->control_pings_outstanding == 0)
162 if (wait && clib_time_now (&scm->clib_time) >= timeout)
169 vl_socket_client_read (int wait)
171 return vl_socket_client_read_internal (socket_client_ctx, wait);
175 vl_socket_client_read2 (socket_client_main_t * scm, int wait)
177 socket_client_main_t *old_ctx;
180 old_ctx = vl_socket_client_ctx_push (scm);
181 rv = vl_socket_client_read_internal (scm, wait);
182 vl_socket_client_ctx_pop (old_ctx);
187 vl_socket_client_write_internal (socket_client_main_t * scm)
190 int len = vec_len (scm->socket_tx_buffer);
193 .gc_mark_timestamp = 0,
194 .data_len = htonl (len),
197 n = write (scm->socket_fd, &msgbuf, sizeof (msgbuf));
198 if (n < sizeof (msgbuf))
200 clib_unix_warning ("socket write (msgbuf)");
204 n = write (scm->socket_fd, scm->socket_tx_buffer, len);
206 vec_set_len (scm->socket_tx_buffer, 0);
210 clib_unix_warning ("socket write (msg)");
218 vl_socket_client_write (void)
220 return vl_socket_client_write_internal (socket_client_ctx);
224 vl_socket_client_write2 (socket_client_main_t * scm)
226 socket_client_main_t *old_ctx;
229 old_ctx = vl_socket_client_ctx_push (scm);
230 rv = vl_socket_client_write_internal (scm);
231 vl_socket_client_ctx_pop (old_ctx);
236 vl_socket_client_msg_alloc2 (socket_client_main_t * scm, int nbytes)
238 vec_set_len (scm->socket_tx_buffer, nbytes);
239 return ((void *) scm->socket_tx_buffer);
243 vl_socket_client_msg_alloc (int nbytes)
245 return vl_socket_client_msg_alloc2 (socket_client_ctx, nbytes);
249 vl_socket_client_disconnect2 (socket_client_main_t * scm)
251 if (vl_mem_client_is_connected ())
253 vl_client_disconnect_from_vlib_no_unmap ();
254 ssvm_delete_memfd (&scm->memfd_segment);
256 if (scm->socket_fd && (close (scm->socket_fd) < 0))
257 clib_unix_warning ("close");
262 vl_socket_client_disconnect (void)
264 vl_socket_client_disconnect2 (socket_client_ctx);
268 vl_socket_client_enable_disable2 (socket_client_main_t * scm, int enable)
270 scm->socket_enable = enable;
274 vl_socket_client_enable_disable (int enable)
276 vl_socket_client_enable_disable2 (socket_client_ctx, enable);
279 static clib_error_t *
280 vl_sock_api_recv_fd_msg_internal (socket_client_main_t * scm, int fds[],
284 char ctl[CMSG_SPACE (sizeof (int) * n_fds)
285 + CMSG_SPACE (sizeof (struct ucred))];
286 struct msghdr mh = { 0 };
290 struct ucred *cr = 0;
292 struct cmsgcred *cr = 0;
293 #endif /* __linux__ */
294 struct cmsghdr *cmsg;
295 pid_t pid __attribute__ ((unused));
296 uid_t uid __attribute__ ((unused));
297 gid_t gid __attribute__ ((unused));
301 socket_fd = scm->client_socket.fd;
303 iov[0].iov_base = msgbuf;
307 mh.msg_control = ctl;
308 mh.msg_controllen = sizeof (ctl);
310 clib_memset (ctl, 0, sizeof (ctl));
314 timeout = clib_time_now (&scm->clib_time) + wait;
315 while (size != 5 && clib_time_now (&scm->clib_time) < timeout)
316 size = recvmsg (socket_fd, &mh, MSG_DONTWAIT);
319 size = recvmsg (socket_fd, &mh, 0);
323 return (size == 0) ? clib_error_return (0, "disconnected") :
324 clib_error_return_unix (0, "recvmsg: malformed message (fd %d)",
328 cmsg = CMSG_FIRSTHDR (&mh);
331 if (cmsg->cmsg_level == SOL_SOCKET)
334 if (cmsg->cmsg_type == SCM_CREDENTIALS)
336 cr = (struct ucred *) CMSG_DATA (cmsg);
342 if (cmsg->cmsg_type == SCM_CREDS)
344 cr = (struct cmsgcred *) CMSG_DATA (cmsg);
345 uid = cr->cmcred_uid;
346 gid = cr->cmcred_gid;
347 pid = cr->cmcred_pid;
349 #endif /* __linux__ */
350 else if (cmsg->cmsg_type == SCM_RIGHTS)
352 clib_memcpy_fast (fds, CMSG_DATA (cmsg), sizeof (int) * n_fds);
355 cmsg = CMSG_NXTHDR (&mh, cmsg);
361 vl_sock_api_recv_fd_msg (int socket_fd, int fds[], int n_fds, u32 wait)
363 return vl_sock_api_recv_fd_msg_internal (socket_client_ctx, fds, n_fds,
368 vl_sock_api_recv_fd_msg2 (socket_client_main_t * scm, int socket_fd,
369 int fds[], int n_fds, u32 wait)
371 socket_client_main_t *old_ctx;
374 old_ctx = vl_socket_client_ctx_push (scm);
375 error = vl_sock_api_recv_fd_msg_internal (scm, fds, n_fds, wait);
376 vl_socket_client_ctx_pop (old_ctx);
380 static void vl_api_sock_init_shm_reply_t_handler
381 (vl_api_sock_init_shm_reply_t * mp)
383 socket_client_main_t *scm = socket_client_ctx;
384 ssvm_private_t *memfd = &scm->memfd_segment;
385 i32 retval = ntohl (mp->retval);
386 api_main_t *am = vlibapi_get_main ();
393 clib_warning ("failed to init shmem");
398 * Check the socket for the magic fd
400 error = vl_sock_api_recv_fd_msg (scm->socket_fd, &my_fd, 1, 5);
403 clib_error_report (error);
408 clib_memset (memfd, 0, sizeof (*memfd));
411 /* Note: this closes memfd.fd */
412 retval = ssvm_client_init_memfd (memfd);
414 clib_warning ("WARNING: segment map returned %d", retval);
417 * Pivot to the memory client segment that vpp just created
419 am->vlib_rp = (void *) (memfd->requested_va + MMAP_PAGESIZE);
420 am->shmem_hdr = (void *) am->vlib_rp->user_ctx;
422 new_name = format (0, "%v[shm]%c", scm->name, 0);
423 vl_client_install_client_message_handlers ();
424 if (scm->want_shm_pthread)
426 vl_client_connect_to_vlib_no_map ("pvt", (char *) new_name,
427 32 /* input_queue_length */ );
431 vl_client_connect_to_vlib_no_rx_pthread_no_map ("pvt",
432 (char *) new_name, 32
433 /* input_queue_length */
436 vl_socket_client_enable_disable (0);
441 vl_api_sockclnt_create_reply_t_handler (vl_api_sockclnt_create_reply_t * mp)
443 socket_client_main_t *scm = socket_client_ctx;
446 scm->socket_enable = 1;
447 scm->client_index = clib_net_to_host_u32 (mp->index);
451 #define foreach_sock_client_api_msg \
452 _(SOCKCLNT_CREATE_REPLY, sockclnt_create_reply) \
453 _(SOCK_INIT_SHM_REPLY, sock_init_shm_reply) \
456 vl_sock_client_install_message_handlers (void)
460 vl_msg_api_config (&(vl_msg_api_msg_config_t){ \
463 .handler = vl_api_##n##_t_handler, \
464 .endian = vl_api_##n##_t_endian, \
465 .format_fn = vl_api_##n##_t_format, \
466 .size = sizeof (vl_api_##n##_t), \
468 .tojson = vl_api_##n##_t_tojson, \
469 .fromjson = vl_api_##n##_t_fromjson, \
470 .calc_size = vl_api_##n##_t_calc_size, \
472 foreach_sock_client_api_msg;
477 vl_socket_client_connect_internal (socket_client_main_t * scm,
478 char *socket_path, char *client_name,
479 u32 socket_buffer_size)
481 vl_api_sockclnt_create_t *mp;
485 /* Already connected? */
490 if (socket_path == 0 || client_name == 0)
493 sock = &scm->client_socket;
494 sock->config = socket_path;
495 sock->flags = CLIB_SOCKET_F_IS_CLIENT;
497 if ((error = clib_socket_init (sock)))
499 clib_error_report (error);
503 vl_sock_client_install_message_handlers ();
505 scm->socket_fd = sock->fd;
506 scm->socket_buffer_size = socket_buffer_size ? socket_buffer_size :
507 SOCKET_CLIENT_DEFAULT_BUFFER_SIZE;
508 vec_validate (scm->socket_tx_buffer, scm->socket_buffer_size - 1);
509 vec_validate (scm->socket_rx_buffer, scm->socket_buffer_size - 1);
510 vec_set_len (scm->socket_rx_buffer, 0);
511 vec_set_len (scm->socket_tx_buffer, 0);
512 scm->name = format (0, "%s", client_name);
514 mp = vl_socket_client_msg_alloc2 (scm, sizeof (*mp));
515 mp->_vl_msg_id = htons (VL_API_SOCKCLNT_CREATE);
516 strncpy ((char *) mp->name, client_name, sizeof (mp->name) - 1);
517 mp->name[sizeof (mp->name) - 1] = 0;
518 mp->context = 0xfeedface;
520 clib_time_init (&scm->clib_time);
522 if (vl_socket_client_write_internal (scm) <= 0)
525 if (vl_socket_client_read_internal (scm, 5))
532 vl_socket_client_connect (char *socket_path, char *client_name,
533 u32 socket_buffer_size)
535 return vl_socket_client_connect_internal (socket_client_ctx, socket_path,
536 client_name, socket_buffer_size);
540 vl_socket_client_connect2 (socket_client_main_t * scm, char *socket_path,
541 char *client_name, u32 socket_buffer_size)
543 socket_client_main_t *old_ctx;
546 old_ctx = vl_socket_client_ctx_push (scm);
547 rv = vl_socket_client_connect_internal (socket_client_ctx, socket_path,
548 client_name, socket_buffer_size);
549 vl_socket_client_ctx_pop (old_ctx);
554 vl_socket_client_init_shm_internal (socket_client_main_t * scm,
555 vl_api_shm_elem_config_t * config,
558 vl_api_sock_init_shm_t *mp;
562 scm->want_shm_pthread = want_pthread;
564 mp = vl_socket_client_msg_alloc2 (scm, sizeof (*mp) +
565 vec_len (config) * sizeof (u64));
566 clib_memset (mp, 0, sizeof (*mp));
567 mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_SOCK_INIT_SHM);
568 mp->client_index = clib_host_to_net_u32 (scm->client_index);
569 mp->requested_size = 64 << 20;
573 for (i = 0; i < vec_len (config); i++)
575 cfg = (u64 *) & config[i];
576 mp->configs[i] = *cfg;
578 mp->nitems = vec_len (config);
580 rv = vl_socket_client_write_internal (scm);
584 if (vl_socket_client_read_internal (scm, 1))
591 vl_socket_client_init_shm (vl_api_shm_elem_config_t * config,
594 return vl_socket_client_init_shm_internal (socket_client_ctx, config,
599 vl_socket_client_init_shm2 (socket_client_main_t * scm,
600 vl_api_shm_elem_config_t * config,
603 socket_client_main_t *old_ctx;
606 old_ctx = vl_socket_client_ctx_push (scm);
607 rv = vl_socket_client_init_shm_internal (socket_client_ctx, config,
609 vl_socket_client_ctx_pop (old_ctx);
614 vl_socket_client_recv_fd_msg2 (socket_client_main_t * scm, int fds[],
618 return clib_error_return (0, "no socket");
619 return vl_sock_api_recv_fd_msg_internal (scm, fds, n_fds, wait);
623 vl_socket_client_recv_fd_msg (int fds[], int n_fds, u32 wait)
625 return vl_socket_client_recv_fd_msg2 (socket_client_ctx, fds, n_fds, wait);
629 * fd.io coding-style-patch-verification: ON
632 * eval: (c-set-style "gnu")