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 *------------------------------------------------------------------
22 #include <sys/socket.h>
25 #include <vlibmemory/socket_client.h>
26 #include <vlibmemory/memory_client.h>
28 #include <vlibmemory/vl_memory_msg_enum.h>
30 #define vl_typedefs /* define message structures */
31 #include <vlibmemory/vl_memory_api_h.h>
34 #define vl_endianfun /* define message structures */
35 #include <vlibmemory/vl_memory_api_h.h>
38 /* instantiate all the print functions we know about */
39 #define vl_print(handle, ...) clib_warning (__VA_ARGS__)
41 #include <vlibmemory/vl_memory_api_h.h>
44 socket_client_main_t socket_client_main;
47 u32 vl (void *p) __attribute__ ((weak));
56 vl_socket_client_read (int wait)
58 socket_client_main_t *scm = &socket_client_main;
59 int n, current_rx_index;
63 if (scm->socket_fd == 0)
67 timeout = clib_time_now (&scm->clib_time) + wait;
71 current_rx_index = vec_len (scm->socket_rx_buffer);
72 while (vec_len (scm->socket_rx_buffer) <
73 sizeof (*mbp) + 2 /* msg id */ )
75 vec_validate (scm->socket_rx_buffer, current_rx_index
76 + scm->socket_buffer_size - 1);
77 _vec_len (scm->socket_rx_buffer) = current_rx_index;
78 n = read (scm->socket_fd, scm->socket_rx_buffer + current_rx_index,
79 scm->socket_buffer_size);
82 clib_unix_warning ("socket_read");
85 _vec_len (scm->socket_rx_buffer) += n;
90 clib_warning ("read %d bytes", n);
94 mbp = (msgbuf_t *) (scm->socket_rx_buffer);
96 if (vec_len (scm->socket_rx_buffer) >= ntohl (mbp->data_len)
99 vl_msg_api_socket_handler ((void *) (mbp->data));
101 if (vec_len (scm->socket_rx_buffer) == ntohl (mbp->data_len)
103 _vec_len (scm->socket_rx_buffer) = 0;
105 vec_delete (scm->socket_rx_buffer, ntohl (mbp->data_len)
109 /* Quit if we're out of data, and not expecting a ping reply */
110 if (vec_len (scm->socket_rx_buffer) == 0
111 && scm->control_pings_outstanding == 0)
115 if (wait && clib_time_now (&scm->clib_time) >= timeout)
122 vl_socket_client_write (void)
124 socket_client_main_t *scm = &socket_client_main;
129 .gc_mark_timestamp = 0,
130 .data_len = htonl (scm->socket_tx_nbytes),
133 n = write (scm->socket_fd, &msgbuf, sizeof (msgbuf));
134 if (n < sizeof (msgbuf))
136 clib_unix_warning ("socket write (msgbuf)");
140 n = write (scm->socket_fd, scm->socket_tx_buffer, scm->socket_tx_nbytes);
141 if (n < scm->socket_tx_nbytes)
143 clib_unix_warning ("socket write (msg)");
151 vl_socket_client_msg_alloc (int nbytes)
153 socket_client_main.socket_tx_nbytes = nbytes;
154 return ((void *) socket_client_main.socket_tx_buffer);
158 vl_socket_client_disconnect (void)
160 socket_client_main_t *scm = &socket_client_main;
162 if (vl_mem_client_is_connected ())
164 vl_client_disconnect_from_vlib_no_unmap ();
165 ssvm_delete_memfd (&scm->memfd_segment);
167 if (scm->socket_fd && (close (scm->socket_fd) < 0))
168 clib_unix_warning ("close");
173 vl_socket_client_enable_disable (int enable)
175 socket_client_main_t *scm = &socket_client_main;
176 scm->socket_enable = enable;
180 vl_sock_api_recv_fd_msg (int socket_fd, int *my_fd, u32 wait)
182 socket_client_main_t *scm = &socket_client_main;
184 char ctl[CMSG_SPACE (sizeof (int)) + CMSG_SPACE (sizeof (struct ucred))];
185 struct msghdr mh = { 0 };
188 struct ucred *cr = 0;
189 struct cmsghdr *cmsg;
190 pid_t pid __attribute__ ((unused));
191 uid_t uid __attribute__ ((unused));
192 gid_t gid __attribute__ ((unused));
195 iov[0].iov_base = msgbuf;
199 mh.msg_control = ctl;
200 mh.msg_controllen = sizeof (ctl);
202 memset (ctl, 0, sizeof (ctl));
206 timeout = clib_time_now (&scm->clib_time) + wait;
207 while (size != 5 && clib_time_now (&scm->clib_time) < timeout)
208 size = recvmsg (socket_fd, &mh, MSG_DONTWAIT);
211 size = recvmsg (socket_fd, &mh, 0);
215 return (size == 0) ? clib_error_return (0, "disconnected") :
216 clib_error_return_unix (0, "recvmsg: malformed message (fd %d)",
220 cmsg = CMSG_FIRSTHDR (&mh);
223 if (cmsg->cmsg_level == SOL_SOCKET)
225 if (cmsg->cmsg_type == SCM_CREDENTIALS)
227 cr = (struct ucred *) CMSG_DATA (cmsg);
232 else if (cmsg->cmsg_type == SCM_RIGHTS)
234 clib_memcpy (my_fd, CMSG_DATA (cmsg), sizeof (int));
237 cmsg = CMSG_NXTHDR (&mh, cmsg);
242 static void vl_api_sock_init_shm_reply_t_handler
243 (vl_api_sock_init_shm_reply_t * mp)
245 socket_client_main_t *scm = &socket_client_main;
246 ssvm_private_t *memfd = &scm->memfd_segment;
247 i32 retval = ntohl (mp->retval);
248 api_main_t *am = &api_main;
255 clib_warning ("failed to init shmem");
260 * Check the socket for the magic fd
262 error = vl_sock_api_recv_fd_msg (scm->socket_fd, &my_fd, 5);
265 clib_error_report (error);
270 memset (memfd, 0, sizeof (*memfd));
273 /* Note: this closes memfd.fd */
274 retval = ssvm_slave_init_memfd (memfd);
276 clib_warning ("WARNING: segment map returned %d", retval);
279 * Pivot to the memory client segment that vpp just created
281 am->vlib_rp = (void *) (memfd->requested_va + MMAP_PAGESIZE);
282 am->shmem_hdr = (void *) am->vlib_rp->user_ctx;
284 new_name = format (0, "%v[shm]%c", scm->name, 0);
285 vl_client_install_client_message_handlers ();
286 vl_client_connect_to_vlib_no_map ("pvt", (char *) new_name,
287 32 /* input_queue_length */ );
288 vl_socket_client_enable_disable (0);
293 vl_api_sockclnt_create_reply_t_handler (vl_api_sockclnt_create_reply_t * mp)
295 socket_client_main_t *scm = &socket_client_main;
297 scm->socket_enable = 1;
300 #define foreach_sock_client_api_msg \
301 _(SOCKCLNT_CREATE_REPLY, sockclnt_create_reply) \
302 _(SOCK_INIT_SHM_REPLY, sock_init_shm_reply) \
305 noop_handler (void *notused)
310 vl_sock_client_install_message_handlers (void)
314 vl_msg_api_set_handlers(VL_API_##N, #n, \
315 vl_api_##n##_t_handler, \
317 vl_api_##n##_t_endian, \
318 vl_api_##n##_t_print, \
319 sizeof(vl_api_##n##_t), 1);
320 foreach_sock_client_api_msg;
325 vl_socket_client_connect (char *socket_path, char *client_name,
326 u32 socket_buffer_size)
328 socket_client_main_t *scm = &socket_client_main;
329 vl_api_sockclnt_create_t *mp;
333 /* Already connected? */
338 if (socket_path == 0 || client_name == 0)
341 sock = &scm->client_socket;
342 sock->config = socket_path;
343 sock->flags = CLIB_SOCKET_F_IS_CLIENT | CLIB_SOCKET_F_SEQPACKET;
345 if ((error = clib_socket_init (sock)))
347 clib_error_report (error);
351 vl_sock_client_install_message_handlers ();
353 scm->socket_fd = sock->fd;
354 scm->socket_buffer_size = socket_buffer_size ? socket_buffer_size :
355 SOCKET_CLIENT_DEFAULT_BUFFER_SIZE;
356 vec_validate (scm->socket_tx_buffer, scm->socket_buffer_size - 1);
357 vec_validate (scm->socket_rx_buffer, scm->socket_buffer_size - 1);
358 _vec_len (scm->socket_rx_buffer) = 0;
359 _vec_len (scm->socket_tx_buffer) = 0;
360 scm->name = format (0, "%s", client_name);
362 mp = vl_socket_client_msg_alloc (sizeof (*mp));
363 mp->_vl_msg_id = htons (VL_API_SOCKCLNT_CREATE);
364 strncpy ((char *) mp->name, client_name, sizeof (mp->name) - 1);
365 mp->name[sizeof (mp->name) - 1] = 0;
366 mp->context = 0xfeedface;
368 if (vl_socket_client_write () <= 0)
371 if (vl_socket_client_read (1))
374 clib_time_init (&scm->clib_time);
379 vl_socket_client_init_shm (vl_api_shm_elem_config_t * config)
381 vl_api_sock_init_shm_t *mp;
385 mp = vl_socket_client_msg_alloc (sizeof (*mp) +
386 vec_len (config) * sizeof (u64));
387 memset (mp, 0, sizeof (*mp));
388 mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_SOCK_INIT_SHM);
389 mp->client_index = ~0;
390 mp->requested_size = 64 << 20;
394 for (i = 0; i < vec_len (config); i++)
396 cfg = (u64 *) & config[i];
397 mp->configs[i] = *cfg;
399 mp->nitems = vec_len (config);
401 rv = vl_socket_client_write ();
405 if (vl_socket_client_read (1))
412 vl_socket_client_recv_fd_msg (int *fd_to_recv, u32 wait)
414 socket_client_main_t *scm = &socket_client_main;
416 return clib_error_return (0, "no socket");
417 return vl_sock_api_recv_fd_msg (scm->client_socket.fd, fd_to_recv, wait);
421 * fd.io coding-style-patch-verification: ON
424 * eval: (c-set-style "gnu")