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>
26 #include <vlibmemory/socket_client.h>
27 #include <vlibmemory/memory_client.h>
29 #include <vlibmemory/vl_memory_msg_enum.h>
31 #define vl_typedefs /* define message structures */
32 #include <vlibmemory/vl_memory_api_h.h>
35 #define vl_endianfun /* define message structures */
36 #include <vlibmemory/vl_memory_api_h.h>
39 #define vl_calcsizefun
40 #include <vlibmemory/vl_memory_api_h.h>
43 /* instantiate all the print functions we know about */
45 #include <vlibmemory/vl_memory_api_h.h>
48 socket_client_main_t socket_client_main;
49 __thread socket_client_main_t *socket_client_ctx = &socket_client_main;
52 u32 vl (void *p) __attribute__ ((weak));
60 static socket_client_main_t *
61 vl_socket_client_ctx_push (socket_client_main_t * ctx)
63 socket_client_main_t *old = socket_client_ctx;
64 socket_client_ctx = ctx;
69 vl_socket_client_ctx_pop (socket_client_main_t * old_ctx)
71 socket_client_ctx = old_ctx;
75 vl_socket_client_read_internal (socket_client_main_t * scm, int wait)
77 u32 data_len = 0, msg_size;
78 int n, current_rx_index;
82 if (scm->socket_fd == 0)
86 timeout = clib_time_now (&scm->clib_time) + wait;
90 while (vec_len (scm->socket_rx_buffer) < sizeof (*mbp))
92 current_rx_index = vec_len (scm->socket_rx_buffer);
93 vec_validate (scm->socket_rx_buffer, current_rx_index
94 + scm->socket_buffer_size - 1);
95 vec_set_len (scm->socket_rx_buffer, current_rx_index);
96 n = read (scm->socket_fd, scm->socket_rx_buffer + current_rx_index,
97 scm->socket_buffer_size);
103 clib_unix_warning ("socket_read");
106 vec_inc_len (scm->socket_rx_buffer, n);
111 clib_warning ("read %d bytes", n);
114 mbp = (msgbuf_t *) (scm->socket_rx_buffer);
115 data_len = ntohl (mbp->data_len);
116 current_rx_index = vec_len (scm->socket_rx_buffer);
117 vec_validate (scm->socket_rx_buffer, current_rx_index + data_len);
118 vec_set_len (scm->socket_rx_buffer, current_rx_index);
119 mbp = (msgbuf_t *) (scm->socket_rx_buffer);
120 msg_size = data_len + sizeof (*mbp);
122 while (vec_len (scm->socket_rx_buffer) < msg_size)
124 n = read (scm->socket_fd,
125 scm->socket_rx_buffer + vec_len (scm->socket_rx_buffer),
126 msg_size - vec_len (scm->socket_rx_buffer));
132 clib_unix_warning ("socket_read");
135 vec_inc_len (scm->socket_rx_buffer, n);
138 if (vec_len (scm->socket_rx_buffer) >= data_len + sizeof (*mbp))
140 vl_msg_api_socket_handler ((void *) (mbp->data), data_len);
142 if (vec_len (scm->socket_rx_buffer) == data_len + sizeof (*mbp))
143 vec_set_len (scm->socket_rx_buffer, 0);
145 vec_delete (scm->socket_rx_buffer, data_len + sizeof (*mbp), 0);
148 /* Quit if we're out of data, and not expecting a ping reply */
149 if (vec_len (scm->socket_rx_buffer) == 0
150 && scm->control_pings_outstanding == 0)
153 if (wait && clib_time_now (&scm->clib_time) >= timeout)
160 vl_socket_client_read (int wait)
162 return vl_socket_client_read_internal (socket_client_ctx, wait);
166 vl_socket_client_read2 (socket_client_main_t * scm, int wait)
168 socket_client_main_t *old_ctx;
171 old_ctx = vl_socket_client_ctx_push (scm);
172 rv = vl_socket_client_read_internal (scm, wait);
173 vl_socket_client_ctx_pop (old_ctx);
178 vl_socket_client_write_internal (socket_client_main_t * scm)
181 int len = vec_len (scm->socket_tx_buffer);
184 .gc_mark_timestamp = 0,
185 .data_len = htonl (len),
188 n = write (scm->socket_fd, &msgbuf, sizeof (msgbuf));
189 if (n < sizeof (msgbuf))
191 clib_unix_warning ("socket write (msgbuf)");
195 n = write (scm->socket_fd, scm->socket_tx_buffer, len);
197 vec_set_len (scm->socket_tx_buffer, 0);
201 clib_unix_warning ("socket write (msg)");
209 vl_socket_client_write (void)
211 return vl_socket_client_write_internal (socket_client_ctx);
215 vl_socket_client_write2 (socket_client_main_t * scm)
217 socket_client_main_t *old_ctx;
220 old_ctx = vl_socket_client_ctx_push (scm);
221 rv = vl_socket_client_write_internal (scm);
222 vl_socket_client_ctx_pop (old_ctx);
227 vl_socket_client_msg_alloc2 (socket_client_main_t * scm, int nbytes)
229 vec_set_len (scm->socket_tx_buffer, nbytes);
230 return ((void *) scm->socket_tx_buffer);
234 vl_socket_client_msg_alloc (int nbytes)
236 return vl_socket_client_msg_alloc2 (socket_client_ctx, nbytes);
240 vl_socket_client_disconnect2 (socket_client_main_t * scm)
242 if (vl_mem_client_is_connected ())
244 vl_client_disconnect_from_vlib_no_unmap ();
245 ssvm_delete_memfd (&scm->memfd_segment);
247 if (scm->socket_fd && (close (scm->socket_fd) < 0))
248 clib_unix_warning ("close");
253 vl_socket_client_disconnect (void)
255 vl_socket_client_disconnect2 (socket_client_ctx);
259 vl_socket_client_enable_disable2 (socket_client_main_t * scm, int enable)
261 scm->socket_enable = enable;
265 vl_socket_client_enable_disable (int enable)
267 vl_socket_client_enable_disable2 (socket_client_ctx, enable);
270 static clib_error_t *
271 vl_sock_api_recv_fd_msg_internal (socket_client_main_t * scm, int fds[],
275 char ctl[CMSG_SPACE (sizeof (int) * n_fds)
276 + CMSG_SPACE (sizeof (struct ucred))];
277 struct msghdr mh = { 0 };
280 struct ucred *cr = 0;
281 struct cmsghdr *cmsg;
282 pid_t pid __attribute__ ((unused));
283 uid_t uid __attribute__ ((unused));
284 gid_t gid __attribute__ ((unused));
288 socket_fd = scm->client_socket.fd;
290 iov[0].iov_base = msgbuf;
294 mh.msg_control = ctl;
295 mh.msg_controllen = sizeof (ctl);
297 clib_memset (ctl, 0, sizeof (ctl));
301 timeout = clib_time_now (&scm->clib_time) + wait;
302 while (size != 5 && clib_time_now (&scm->clib_time) < timeout)
303 size = recvmsg (socket_fd, &mh, MSG_DONTWAIT);
306 size = recvmsg (socket_fd, &mh, 0);
310 return (size == 0) ? clib_error_return (0, "disconnected") :
311 clib_error_return_unix (0, "recvmsg: malformed message (fd %d)",
315 cmsg = CMSG_FIRSTHDR (&mh);
318 if (cmsg->cmsg_level == SOL_SOCKET)
320 if (cmsg->cmsg_type == SCM_CREDENTIALS)
322 cr = (struct ucred *) CMSG_DATA (cmsg);
327 else if (cmsg->cmsg_type == SCM_RIGHTS)
329 clib_memcpy_fast (fds, CMSG_DATA (cmsg), sizeof (int) * n_fds);
332 cmsg = CMSG_NXTHDR (&mh, cmsg);
338 vl_sock_api_recv_fd_msg (int socket_fd, int fds[], int n_fds, u32 wait)
340 return vl_sock_api_recv_fd_msg_internal (socket_client_ctx, fds, n_fds,
345 vl_sock_api_recv_fd_msg2 (socket_client_main_t * scm, int socket_fd,
346 int fds[], int n_fds, u32 wait)
348 socket_client_main_t *old_ctx;
351 old_ctx = vl_socket_client_ctx_push (scm);
352 error = vl_sock_api_recv_fd_msg_internal (scm, fds, n_fds, wait);
353 vl_socket_client_ctx_pop (old_ctx);
357 static void vl_api_sock_init_shm_reply_t_handler
358 (vl_api_sock_init_shm_reply_t * mp)
360 socket_client_main_t *scm = socket_client_ctx;
361 ssvm_private_t *memfd = &scm->memfd_segment;
362 i32 retval = ntohl (mp->retval);
363 api_main_t *am = vlibapi_get_main ();
370 clib_warning ("failed to init shmem");
375 * Check the socket for the magic fd
377 error = vl_sock_api_recv_fd_msg (scm->socket_fd, &my_fd, 1, 5);
380 clib_error_report (error);
385 clib_memset (memfd, 0, sizeof (*memfd));
388 /* Note: this closes memfd.fd */
389 retval = ssvm_client_init_memfd (memfd);
391 clib_warning ("WARNING: segment map returned %d", retval);
394 * Pivot to the memory client segment that vpp just created
396 am->vlib_rp = (void *) (memfd->requested_va + MMAP_PAGESIZE);
397 am->shmem_hdr = (void *) am->vlib_rp->user_ctx;
399 new_name = format (0, "%v[shm]%c", scm->name, 0);
400 vl_client_install_client_message_handlers ();
401 if (scm->want_shm_pthread)
403 vl_client_connect_to_vlib_no_map ("pvt", (char *) new_name,
404 32 /* input_queue_length */ );
408 vl_client_connect_to_vlib_no_rx_pthread_no_map ("pvt",
409 (char *) new_name, 32
410 /* input_queue_length */
413 vl_socket_client_enable_disable (0);
418 vl_api_sockclnt_create_reply_t_handler (vl_api_sockclnt_create_reply_t * mp)
420 socket_client_main_t *scm = socket_client_ctx;
423 scm->socket_enable = 1;
424 scm->client_index = clib_net_to_host_u32 (mp->index);
428 #define foreach_sock_client_api_msg \
429 _(SOCKCLNT_CREATE_REPLY, sockclnt_create_reply) \
430 _(SOCK_INIT_SHM_REPLY, sock_init_shm_reply) \
433 vl_sock_client_install_message_handlers (void)
437 vl_msg_api_config (&(vl_msg_api_msg_config_t){ \
440 .handler = vl_api_##n##_t_handler, \
441 .endian = vl_api_##n##_t_endian, \
442 .format_fn = vl_api_##n##_t_format, \
443 .size = sizeof (vl_api_##n##_t), \
445 .tojson = vl_api_##n##_t_tojson, \
446 .fromjson = vl_api_##n##_t_fromjson, \
447 .calc_size = vl_api_##n##_t_calc_size, \
449 foreach_sock_client_api_msg;
454 vl_socket_client_connect_internal (socket_client_main_t * scm,
455 char *socket_path, char *client_name,
456 u32 socket_buffer_size)
458 vl_api_sockclnt_create_t *mp;
462 /* Already connected? */
467 if (socket_path == 0 || client_name == 0)
470 sock = &scm->client_socket;
471 sock->config = socket_path;
472 sock->flags = CLIB_SOCKET_F_IS_CLIENT;
474 if ((error = clib_socket_init (sock)))
476 clib_error_report (error);
480 vl_sock_client_install_message_handlers ();
482 scm->socket_fd = sock->fd;
483 scm->socket_buffer_size = socket_buffer_size ? socket_buffer_size :
484 SOCKET_CLIENT_DEFAULT_BUFFER_SIZE;
485 vec_validate (scm->socket_tx_buffer, scm->socket_buffer_size - 1);
486 vec_validate (scm->socket_rx_buffer, scm->socket_buffer_size - 1);
487 vec_set_len (scm->socket_rx_buffer, 0);
488 vec_set_len (scm->socket_tx_buffer, 0);
489 scm->name = format (0, "%s", client_name);
491 mp = vl_socket_client_msg_alloc2 (scm, sizeof (*mp));
492 mp->_vl_msg_id = htons (VL_API_SOCKCLNT_CREATE);
493 strncpy ((char *) mp->name, client_name, sizeof (mp->name) - 1);
494 mp->name[sizeof (mp->name) - 1] = 0;
495 mp->context = 0xfeedface;
497 clib_time_init (&scm->clib_time);
499 if (vl_socket_client_write_internal (scm) <= 0)
502 if (vl_socket_client_read_internal (scm, 5))
509 vl_socket_client_connect (char *socket_path, char *client_name,
510 u32 socket_buffer_size)
512 return vl_socket_client_connect_internal (socket_client_ctx, socket_path,
513 client_name, socket_buffer_size);
517 vl_socket_client_connect2 (socket_client_main_t * scm, char *socket_path,
518 char *client_name, u32 socket_buffer_size)
520 socket_client_main_t *old_ctx;
523 old_ctx = vl_socket_client_ctx_push (scm);
524 rv = vl_socket_client_connect_internal (socket_client_ctx, socket_path,
525 client_name, socket_buffer_size);
526 vl_socket_client_ctx_pop (old_ctx);
531 vl_socket_client_init_shm_internal (socket_client_main_t * scm,
532 vl_api_shm_elem_config_t * config,
535 vl_api_sock_init_shm_t *mp;
539 scm->want_shm_pthread = want_pthread;
541 mp = vl_socket_client_msg_alloc2 (scm, sizeof (*mp) +
542 vec_len (config) * sizeof (u64));
543 clib_memset (mp, 0, sizeof (*mp));
544 mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_SOCK_INIT_SHM);
545 mp->client_index = clib_host_to_net_u32 (scm->client_index);
546 mp->requested_size = 64 << 20;
550 for (i = 0; i < vec_len (config); i++)
552 cfg = (u64 *) & config[i];
553 mp->configs[i] = *cfg;
555 mp->nitems = vec_len (config);
557 rv = vl_socket_client_write_internal (scm);
561 if (vl_socket_client_read_internal (scm, 1))
568 vl_socket_client_init_shm (vl_api_shm_elem_config_t * config,
571 return vl_socket_client_init_shm_internal (socket_client_ctx, config,
576 vl_socket_client_init_shm2 (socket_client_main_t * scm,
577 vl_api_shm_elem_config_t * config,
580 socket_client_main_t *old_ctx;
583 old_ctx = vl_socket_client_ctx_push (scm);
584 rv = vl_socket_client_init_shm_internal (socket_client_ctx, config,
586 vl_socket_client_ctx_pop (old_ctx);
591 vl_socket_client_recv_fd_msg2 (socket_client_main_t * scm, int fds[],
595 return clib_error_return (0, "no socket");
596 return vl_sock_api_recv_fd_msg_internal (scm, fds, n_fds, wait);
600 vl_socket_client_recv_fd_msg (int fds[], int n_fds, u32 wait)
602 return vl_socket_client_recv_fd_msg2 (socket_client_ctx, fds, n_fds, wait);
606 * fd.io coding-style-patch-verification: ON
609 * eval: (c-set-style "gnu")