blob: b7d4f2d6e54ade85e509c6ac5f07d6df8c38f2c3 [file] [log] [blame]
Dave Barach59b25652017-09-10 15:04:27 -04001/*
2 *------------------------------------------------------------------
3 * socket_client.c - API message handling over sockets, client code.
4 *
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:
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
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 *------------------------------------------------------------------
18 */
19
20#include <stdio.h>
Florin Coras90a63982017-12-19 04:50:01 -080021#define __USE_GNU
22#include <sys/socket.h>
Dave Barach59b25652017-09-10 15:04:27 -040023
Florin Coras4d9b9d82018-01-14 12:25:50 -080024#include <svm/ssvm.h>
Florin Corase86a8ed2018-01-05 03:20:25 -080025#include <vlibmemory/socket_client.h>
26#include <vlibmemory/memory_client.h>
Dave Barach59b25652017-09-10 15:04:27 -040027
28#include <vlibmemory/vl_memory_msg_enum.h>
29
30#define vl_typedefs /* define message structures */
31#include <vlibmemory/vl_memory_api_h.h>
32#undef vl_typedefs
33
34#define vl_endianfun /* define message structures */
35#include <vlibmemory/vl_memory_api_h.h>
36#undef vl_endianfun
37
38/* instantiate all the print functions we know about */
39#define vl_print(handle, ...) clib_warning (__VA_ARGS__)
40#define vl_printfun
41#include <vlibmemory/vl_memory_api_h.h>
42#undef vl_printfun
43
44socket_client_main_t socket_client_main;
Florin Coras59cea1a2019-11-25 13:40:42 -080045__thread socket_client_main_t *socket_client_ctx = &socket_client_main;
Dave Barach59b25652017-09-10 15:04:27 -040046
47/* Debug aid */
48u32 vl (void *p) __attribute__ ((weak));
Florin Coras90a63982017-12-19 04:50:01 -080049
Dave Barach59b25652017-09-10 15:04:27 -040050u32
51vl (void *p)
52{
53 return vec_len (p);
54}
55
Florin Coras59cea1a2019-11-25 13:40:42 -080056static socket_client_main_t *
57vl_socket_client_ctx_push (socket_client_main_t * ctx)
Dave Barach59b25652017-09-10 15:04:27 -040058{
Florin Coras59cea1a2019-11-25 13:40:42 -080059 socket_client_main_t *old = socket_client_ctx;
60 socket_client_ctx = ctx;
61 return old;
62}
63
64static void
65vl_socket_client_ctx_pop (socket_client_main_t * old_ctx)
66{
67 socket_client_ctx = old_ctx;
68}
69
70static int
71vl_socket_client_read_internal (socket_client_main_t * scm, int wait)
72{
Florin Coras2881dec2018-10-02 18:29:25 -070073 u32 data_len = 0, msg_size;
Dave Barach59b25652017-09-10 15:04:27 -040074 int n, current_rx_index;
Florin Coras90a63982017-12-19 04:50:01 -080075 msgbuf_t *mbp = 0;
76 f64 timeout;
Dave Barach59b25652017-09-10 15:04:27 -040077
Florin Coras90a63982017-12-19 04:50:01 -080078 if (scm->socket_fd == 0)
79 return -1;
Dave Barach59b25652017-09-10 15:04:27 -040080
Florin Coras90a63982017-12-19 04:50:01 -080081 if (wait)
82 timeout = clib_time_now (&scm->clib_time) + wait;
Dave Barach59b25652017-09-10 15:04:27 -040083
84 while (1)
85 {
Florin Coras2881dec2018-10-02 18:29:25 -070086 while (vec_len (scm->socket_rx_buffer) < sizeof (*mbp))
Dave Barach59b25652017-09-10 15:04:27 -040087 {
Florin Coras2881dec2018-10-02 18:29:25 -070088 current_rx_index = vec_len (scm->socket_rx_buffer);
Dave Barach59b25652017-09-10 15:04:27 -040089 vec_validate (scm->socket_rx_buffer, current_rx_index
90 + scm->socket_buffer_size - 1);
91 _vec_len (scm->socket_rx_buffer) = current_rx_index;
92 n = read (scm->socket_fd, scm->socket_rx_buffer + current_rx_index,
93 scm->socket_buffer_size);
94 if (n < 0)
95 {
Florin Coras66a10032018-12-21 16:23:09 -080096 if (errno == EAGAIN)
97 continue;
98
Dave Barach59b25652017-09-10 15:04:27 -040099 clib_unix_warning ("socket_read");
Florin Coras90a63982017-12-19 04:50:01 -0800100 return -1;
Dave Barach59b25652017-09-10 15:04:27 -0400101 }
102 _vec_len (scm->socket_rx_buffer) += n;
103 }
104
105#if CLIB_DEBUG > 1
106 if (n > 0)
107 clib_warning ("read %d bytes", n);
108#endif
109
Florin Coras2881dec2018-10-02 18:29:25 -0700110 mbp = (msgbuf_t *) (scm->socket_rx_buffer);
111 data_len = ntohl (mbp->data_len);
112 current_rx_index = vec_len (scm->socket_rx_buffer);
113 vec_validate (scm->socket_rx_buffer, current_rx_index + data_len);
114 _vec_len (scm->socket_rx_buffer) = current_rx_index;
115 mbp = (msgbuf_t *) (scm->socket_rx_buffer);
116 msg_size = data_len + sizeof (*mbp);
Dave Barach59b25652017-09-10 15:04:27 -0400117
Florin Coras2881dec2018-10-02 18:29:25 -0700118 while (vec_len (scm->socket_rx_buffer) < msg_size)
119 {
120 n = read (scm->socket_fd,
121 scm->socket_rx_buffer + vec_len (scm->socket_rx_buffer),
122 msg_size - vec_len (scm->socket_rx_buffer));
Florin Coras66a10032018-12-21 16:23:09 -0800123 if (n < 0)
Florin Coras2881dec2018-10-02 18:29:25 -0700124 {
Florin Coras66a10032018-12-21 16:23:09 -0800125 if (errno == EAGAIN)
126 continue;
127
Florin Coras2881dec2018-10-02 18:29:25 -0700128 clib_unix_warning ("socket_read");
129 return -1;
130 }
131 _vec_len (scm->socket_rx_buffer) += n;
132 }
133
134 if (vec_len (scm->socket_rx_buffer) >= data_len + sizeof (*mbp))
Dave Barach59b25652017-09-10 15:04:27 -0400135 {
136 vl_msg_api_socket_handler ((void *) (mbp->data));
137
Florin Coras2881dec2018-10-02 18:29:25 -0700138 if (vec_len (scm->socket_rx_buffer) == data_len + sizeof (*mbp))
Dave Barach59b25652017-09-10 15:04:27 -0400139 _vec_len (scm->socket_rx_buffer) = 0;
140 else
Florin Coras2881dec2018-10-02 18:29:25 -0700141 vec_delete (scm->socket_rx_buffer, data_len + sizeof (*mbp), 0);
Dave Barach59b25652017-09-10 15:04:27 -0400142 mbp = 0;
143
144 /* Quit if we're out of data, and not expecting a ping reply */
145 if (vec_len (scm->socket_rx_buffer) == 0
146 && scm->control_pings_outstanding == 0)
147 break;
148 }
Florin Coras90a63982017-12-19 04:50:01 -0800149 if (wait && clib_time_now (&scm->clib_time) >= timeout)
150 return -1;
Dave Barach59b25652017-09-10 15:04:27 -0400151 }
Florin Coras90a63982017-12-19 04:50:01 -0800152 return 0;
Dave Barach59b25652017-09-10 15:04:27 -0400153}
154
155int
Florin Coras59cea1a2019-11-25 13:40:42 -0800156vl_socket_client_read (int wait)
Dave Barach59b25652017-09-10 15:04:27 -0400157{
Florin Coras59cea1a2019-11-25 13:40:42 -0800158 return vl_socket_client_read_internal (socket_client_ctx, wait);
159}
160
161int
162vl_socket_client_read2 (socket_client_main_t * scm, int wait)
163{
164 socket_client_main_t *old_ctx;
165 int rv;
166
167 old_ctx = vl_socket_client_ctx_push (scm);
168 rv = vl_socket_client_read_internal (scm, wait);
169 vl_socket_client_ctx_pop (old_ctx);
170 return rv;
171}
172
173static int
174vl_socket_client_write_internal (socket_client_main_t * scm)
175{
Florin Coras90a63982017-12-19 04:50:01 -0800176 int n;
177
178 msgbuf_t msgbuf = {
179 .q = 0,
180 .gc_mark_timestamp = 0,
181 .data_len = htonl (scm->socket_tx_nbytes),
182 };
183
184 n = write (scm->socket_fd, &msgbuf, sizeof (msgbuf));
185 if (n < sizeof (msgbuf))
186 {
187 clib_unix_warning ("socket write (msgbuf)");
188 return -1;
189 }
190
191 n = write (scm->socket_fd, scm->socket_tx_buffer, scm->socket_tx_nbytes);
192 if (n < scm->socket_tx_nbytes)
193 {
194 clib_unix_warning ("socket write (msg)");
195 return -1;
196 }
197
198 return n;
199}
200
Florin Coras59cea1a2019-11-25 13:40:42 -0800201int
202vl_socket_client_write (void)
203{
204 return vl_socket_client_write_internal (socket_client_ctx);
205}
206
207int
208vl_socket_client_write2 (socket_client_main_t * scm)
209{
210 socket_client_main_t *old_ctx;
211 int rv;
212
213 old_ctx = vl_socket_client_ctx_push (scm);
214 rv = vl_socket_client_write_internal (scm);
215 vl_socket_client_ctx_pop (old_ctx);
216 return rv;
217}
218
219void *
220vl_socket_client_msg_alloc2 (socket_client_main_t * scm, int nbytes)
221{
222 scm->socket_tx_nbytes = nbytes;
223 return ((void *) scm->socket_tx_buffer);
224}
225
Florin Coras90a63982017-12-19 04:50:01 -0800226void *
227vl_socket_client_msg_alloc (int nbytes)
228{
Florin Coras59cea1a2019-11-25 13:40:42 -0800229 return vl_socket_client_msg_alloc2 (socket_client_ctx, nbytes);
Florin Coras90a63982017-12-19 04:50:01 -0800230}
231
232void
Florin Coras59cea1a2019-11-25 13:40:42 -0800233vl_socket_client_disconnect2 (socket_client_main_t * scm)
Florin Coras90a63982017-12-19 04:50:01 -0800234{
Florin Corasb384b542018-01-15 01:08:33 -0800235 if (vl_mem_client_is_connected ())
236 {
237 vl_client_disconnect_from_vlib_no_unmap ();
238 ssvm_delete_memfd (&scm->memfd_segment);
239 }
Florin Coras90a63982017-12-19 04:50:01 -0800240 if (scm->socket_fd && (close (scm->socket_fd) < 0))
241 clib_unix_warning ("close");
242 scm->socket_fd = 0;
243}
244
245void
Florin Coras59cea1a2019-11-25 13:40:42 -0800246vl_socket_client_disconnect (void)
247{
248 vl_socket_client_disconnect2 (socket_client_ctx);
249}
250
251void
252vl_socket_client_enable_disable2 (socket_client_main_t * scm, int enable)
253{
254 scm->socket_enable = enable;
255}
256
257void
Florin Coras90a63982017-12-19 04:50:01 -0800258vl_socket_client_enable_disable (int enable)
259{
Florin Coras59cea1a2019-11-25 13:40:42 -0800260 vl_socket_client_enable_disable2 (socket_client_ctx, enable);
Florin Coras90a63982017-12-19 04:50:01 -0800261}
262
Florin Corasd4c70922019-11-28 14:21:21 -0800263static clib_error_t *
264vl_sock_api_recv_fd_msg_internal (socket_client_main_t * scm, int fds[],
265 int n_fds, u32 wait)
Florin Coras90a63982017-12-19 04:50:01 -0800266{
267 char msgbuf[16];
Florin Coras466f2892018-08-03 02:50:43 -0700268 char ctl[CMSG_SPACE (sizeof (int) * n_fds)
269 + CMSG_SPACE (sizeof (struct ucred))];
Florin Coras90a63982017-12-19 04:50:01 -0800270 struct msghdr mh = { 0 };
271 struct iovec iov[1];
Florin Corasb384b542018-01-15 01:08:33 -0800272 ssize_t size = 0;
Florin Coras90a63982017-12-19 04:50:01 -0800273 struct ucred *cr = 0;
274 struct cmsghdr *cmsg;
275 pid_t pid __attribute__ ((unused));
276 uid_t uid __attribute__ ((unused));
277 gid_t gid __attribute__ ((unused));
Florin Corasd4c70922019-11-28 14:21:21 -0800278 int socket_fd;
Florin Corasb384b542018-01-15 01:08:33 -0800279 f64 timeout;
Florin Coras90a63982017-12-19 04:50:01 -0800280
Florin Corasd4c70922019-11-28 14:21:21 -0800281 socket_fd = scm->client_socket.fd;
282
Florin Coras90a63982017-12-19 04:50:01 -0800283 iov[0].iov_base = msgbuf;
284 iov[0].iov_len = 5;
285 mh.msg_iov = iov;
286 mh.msg_iovlen = 1;
287 mh.msg_control = ctl;
288 mh.msg_controllen = sizeof (ctl);
289
Dave Barachb7b92992018-10-17 10:38:51 -0400290 clib_memset (ctl, 0, sizeof (ctl));
Florin Coras90a63982017-12-19 04:50:01 -0800291
Florin Corasb384b542018-01-15 01:08:33 -0800292 if (wait != ~0)
293 {
294 timeout = clib_time_now (&scm->clib_time) + wait;
295 while (size != 5 && clib_time_now (&scm->clib_time) < timeout)
296 size = recvmsg (socket_fd, &mh, MSG_DONTWAIT);
297 }
298 else
299 size = recvmsg (socket_fd, &mh, 0);
300
Florin Coras90a63982017-12-19 04:50:01 -0800301 if (size != 5)
302 {
303 return (size == 0) ? clib_error_return (0, "disconnected") :
304 clib_error_return_unix (0, "recvmsg: malformed message (fd %d)",
305 socket_fd);
306 }
307
308 cmsg = CMSG_FIRSTHDR (&mh);
309 while (cmsg)
310 {
311 if (cmsg->cmsg_level == SOL_SOCKET)
312 {
313 if (cmsg->cmsg_type == SCM_CREDENTIALS)
314 {
315 cr = (struct ucred *) CMSG_DATA (cmsg);
316 uid = cr->uid;
317 gid = cr->gid;
318 pid = cr->pid;
319 }
320 else if (cmsg->cmsg_type == SCM_RIGHTS)
321 {
Dave Barach178cf492018-11-13 16:34:13 -0500322 clib_memcpy_fast (fds, CMSG_DATA (cmsg), sizeof (int) * n_fds);
Florin Coras90a63982017-12-19 04:50:01 -0800323 }
324 }
325 cmsg = CMSG_NXTHDR (&mh, cmsg);
326 }
327 return 0;
328}
329
Florin Corasd4c70922019-11-28 14:21:21 -0800330clib_error_t *
331vl_sock_api_recv_fd_msg (int socket_fd, int fds[], int n_fds, u32 wait)
332{
333 return vl_sock_api_recv_fd_msg_internal (socket_client_ctx, fds, n_fds,
334 wait);
335}
336
337clib_error_t *
338vl_sock_api_recv_fd_msg2 (socket_client_main_t * scm, int socket_fd,
339 int fds[], int n_fds, u32 wait)
340{
341 socket_client_main_t *old_ctx;
342 clib_error_t *error;
343
344 old_ctx = vl_socket_client_ctx_push (scm);
345 error = vl_sock_api_recv_fd_msg_internal (scm, fds, n_fds, wait);
346 vl_socket_client_ctx_pop (old_ctx);
347 return error;
348}
349
Florin Coras90a63982017-12-19 04:50:01 -0800350static void vl_api_sock_init_shm_reply_t_handler
351 (vl_api_sock_init_shm_reply_t * mp)
352{
Florin Coras59cea1a2019-11-25 13:40:42 -0800353 socket_client_main_t *scm = socket_client_ctx;
Florin Corasb384b542018-01-15 01:08:33 -0800354 ssvm_private_t *memfd = &scm->memfd_segment;
Florin Coras90a63982017-12-19 04:50:01 -0800355 i32 retval = ntohl (mp->retval);
Dave Barach39d69112019-11-27 11:42:13 -0500356 api_main_t *am = vlibapi_get_main ();
Florin Corasb384b542018-01-15 01:08:33 -0800357 clib_error_t *error;
358 int my_fd = -1;
Florin Coras90a63982017-12-19 04:50:01 -0800359 u8 *new_name;
360
361 if (retval)
362 {
363 clib_warning ("failed to init shmem");
364 return;
365 }
366
367 /*
368 * Check the socket for the magic fd
369 */
Florin Coras466f2892018-08-03 02:50:43 -0700370 error = vl_sock_api_recv_fd_msg (scm->socket_fd, &my_fd, 1, 5);
Florin Coras90a63982017-12-19 04:50:01 -0800371 if (error)
372 {
Florin Corasb384b542018-01-15 01:08:33 -0800373 clib_error_report (error);
Florin Coras90a63982017-12-19 04:50:01 -0800374 retval = -99;
375 return;
376 }
377
Dave Barachb7b92992018-10-17 10:38:51 -0400378 clib_memset (memfd, 0, sizeof (*memfd));
Florin Corasb384b542018-01-15 01:08:33 -0800379 memfd->fd = my_fd;
Florin Coras90a63982017-12-19 04:50:01 -0800380
381 /* Note: this closes memfd.fd */
Florin Corasb384b542018-01-15 01:08:33 -0800382 retval = ssvm_slave_init_memfd (memfd);
Florin Coras90a63982017-12-19 04:50:01 -0800383 if (retval)
384 clib_warning ("WARNING: segment map returned %d", retval);
385
386 /*
387 * Pivot to the memory client segment that vpp just created
388 */
Florin Corasb384b542018-01-15 01:08:33 -0800389 am->vlib_rp = (void *) (memfd->requested_va + MMAP_PAGESIZE);
Florin Coras90a63982017-12-19 04:50:01 -0800390 am->shmem_hdr = (void *) am->vlib_rp->user_ctx;
391
392 new_name = format (0, "%v[shm]%c", scm->name, 0);
393 vl_client_install_client_message_handlers ();
Tomasz Kulasek97dcf5b2019-01-31 18:26:32 +0100394 if (scm->want_shm_pthread)
395 {
396 vl_client_connect_to_vlib_no_map ("pvt", (char *) new_name,
397 32 /* input_queue_length */ );
398 }
399 else
400 {
401 vl_client_connect_to_vlib_no_rx_pthread_no_map ("pvt",
402 (char *) new_name, 32
403 /* input_queue_length */
404 );
405 }
Florin Coras90a63982017-12-19 04:50:01 -0800406 vl_socket_client_enable_disable (0);
407 vec_free (new_name);
408}
409
410static void
411vl_api_sockclnt_create_reply_t_handler (vl_api_sockclnt_create_reply_t * mp)
412{
Florin Coras59cea1a2019-11-25 13:40:42 -0800413 socket_client_main_t *scm = socket_client_ctx;
Florin Coras90a63982017-12-19 04:50:01 -0800414 if (!mp->response)
Florin Coras2881dec2018-10-02 18:29:25 -0700415 {
416 scm->socket_enable = 1;
417 scm->client_index = clib_net_to_host_u32 (mp->index);
418 }
Florin Coras90a63982017-12-19 04:50:01 -0800419}
420
421#define foreach_sock_client_api_msg \
422_(SOCKCLNT_CREATE_REPLY, sockclnt_create_reply) \
423_(SOCK_INIT_SHM_REPLY, sock_init_shm_reply) \
424
425static void
426noop_handler (void *notused)
427{
428}
429
430void
431vl_sock_client_install_message_handlers (void)
432{
433
434#define _(N,n) \
435 vl_msg_api_set_handlers(VL_API_##N, #n, \
436 vl_api_##n##_t_handler, \
437 noop_handler, \
438 vl_api_##n##_t_endian, \
439 vl_api_##n##_t_print, \
440 sizeof(vl_api_##n##_t), 1);
441 foreach_sock_client_api_msg;
442#undef _
443}
444
445int
Florin Coras59cea1a2019-11-25 13:40:42 -0800446vl_socket_client_connect_internal (socket_client_main_t * scm,
447 char *socket_path, char *client_name,
448 u32 socket_buffer_size)
Florin Coras90a63982017-12-19 04:50:01 -0800449{
Dave Barach59b25652017-09-10 15:04:27 -0400450 vl_api_sockclnt_create_t *mp;
Florin Coras90a63982017-12-19 04:50:01 -0800451 clib_socket_t *sock;
Dave Barach59b25652017-09-10 15:04:27 -0400452 clib_error_t *error;
453
454 /* Already connected? */
455 if (scm->socket_fd)
456 return (-2);
457
458 /* bogus call? */
459 if (socket_path == 0 || client_name == 0)
460 return (-3);
461
Florin Coras90a63982017-12-19 04:50:01 -0800462 sock = &scm->client_socket;
Dave Barach59b25652017-09-10 15:04:27 -0400463 sock->config = socket_path;
Ole Troan4ff09ae2019-04-15 11:27:22 +0200464 sock->flags = CLIB_SOCKET_F_IS_CLIENT | CLIB_SOCKET_F_NON_BLOCKING_CONNECT;
Dave Barach59b25652017-09-10 15:04:27 -0400465
Florin Coras90a63982017-12-19 04:50:01 -0800466 if ((error = clib_socket_init (sock)))
Dave Barach59b25652017-09-10 15:04:27 -0400467 {
468 clib_error_report (error);
469 return (-1);
470 }
471
Florin Coras90a63982017-12-19 04:50:01 -0800472 vl_sock_client_install_message_handlers ();
473
Dave Barach59b25652017-09-10 15:04:27 -0400474 scm->socket_fd = sock->fd;
Dave Barach59b25652017-09-10 15:04:27 -0400475 scm->socket_buffer_size = socket_buffer_size ? socket_buffer_size :
476 SOCKET_CLIENT_DEFAULT_BUFFER_SIZE;
477 vec_validate (scm->socket_tx_buffer, scm->socket_buffer_size - 1);
478 vec_validate (scm->socket_rx_buffer, scm->socket_buffer_size - 1);
479 _vec_len (scm->socket_rx_buffer) = 0;
Florin Coras90a63982017-12-19 04:50:01 -0800480 _vec_len (scm->socket_tx_buffer) = 0;
481 scm->name = format (0, "%s", client_name);
Dave Barach59b25652017-09-10 15:04:27 -0400482
Florin Coras59cea1a2019-11-25 13:40:42 -0800483 mp = vl_socket_client_msg_alloc2 (scm, sizeof (*mp));
Florin Coras90a63982017-12-19 04:50:01 -0800484 mp->_vl_msg_id = htons (VL_API_SOCKCLNT_CREATE);
Ole Troan7adaa222019-08-27 15:05:27 +0200485 strncpy ((char *) mp->name, client_name, sizeof (mp->name) - 1);
486 mp->name[sizeof (mp->name) - 1] = 0;
Florin Coras90a63982017-12-19 04:50:01 -0800487 mp->context = 0xfeedface;
488
Florin Coras2881dec2018-10-02 18:29:25 -0700489 clib_time_init (&scm->clib_time);
490
Florin Coras59cea1a2019-11-25 13:40:42 -0800491 if (vl_socket_client_write_internal (scm) <= 0)
Florin Coras90a63982017-12-19 04:50:01 -0800492 return (-1);
493
Florin Coras59cea1a2019-11-25 13:40:42 -0800494 if (vl_socket_client_read_internal (scm, 5))
Florin Coras90a63982017-12-19 04:50:01 -0800495 return (-1);
496
Dave Barach59b25652017-09-10 15:04:27 -0400497 return (0);
498}
499
Florin Coras90a63982017-12-19 04:50:01 -0800500int
Florin Coras59cea1a2019-11-25 13:40:42 -0800501vl_socket_client_connect (char *socket_path, char *client_name,
502 u32 socket_buffer_size)
Dave Barach59b25652017-09-10 15:04:27 -0400503{
Florin Coras59cea1a2019-11-25 13:40:42 -0800504 return vl_socket_client_connect_internal (socket_client_ctx, socket_path,
505 client_name, socket_buffer_size);
506}
507
508int
509vl_socket_client_connect2 (socket_client_main_t * scm, char *socket_path,
510 char *client_name, u32 socket_buffer_size)
511{
512 socket_client_main_t *old_ctx;
513 int rv;
514
515 old_ctx = vl_socket_client_ctx_push (scm);
516 rv = vl_socket_client_connect_internal (socket_client_ctx, socket_path,
517 client_name, socket_buffer_size);
518 vl_socket_client_ctx_pop (old_ctx);
519 return rv;
520}
521
522int
523vl_socket_client_init_shm_internal (socket_client_main_t * scm,
524 vl_api_shm_elem_config_t * config,
525 int want_pthread)
526{
Florin Coras90a63982017-12-19 04:50:01 -0800527 vl_api_sock_init_shm_t *mp;
528 int rv, i;
529 u64 *cfg;
Dave Barach59b25652017-09-10 15:04:27 -0400530
Tomasz Kulasek97dcf5b2019-01-31 18:26:32 +0100531 scm->want_shm_pthread = want_pthread;
532
Florin Coras59cea1a2019-11-25 13:40:42 -0800533 mp = vl_socket_client_msg_alloc2 (scm, sizeof (*mp) +
534 vec_len (config) * sizeof (u64));
Dave Barachb7b92992018-10-17 10:38:51 -0400535 clib_memset (mp, 0, sizeof (*mp));
Florin Coras90a63982017-12-19 04:50:01 -0800536 mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_SOCK_INIT_SHM);
Florin Coras2881dec2018-10-02 18:29:25 -0700537 mp->client_index = clib_host_to_net_u32 (scm->client_index);
Florin Coras90a63982017-12-19 04:50:01 -0800538 mp->requested_size = 64 << 20;
539
540 if (config)
541 {
542 for (i = 0; i < vec_len (config); i++)
543 {
544 cfg = (u64 *) & config[i];
545 mp->configs[i] = *cfg;
546 }
547 mp->nitems = vec_len (config);
548 }
Florin Coras59cea1a2019-11-25 13:40:42 -0800549 rv = vl_socket_client_write_internal (scm);
Florin Coras90a63982017-12-19 04:50:01 -0800550 if (rv <= 0)
551 return rv;
552
Florin Coras59cea1a2019-11-25 13:40:42 -0800553 if (vl_socket_client_read_internal (scm, 1))
Florin Coras90a63982017-12-19 04:50:01 -0800554 return -1;
555
556 return 0;
Dave Barach59b25652017-09-10 15:04:27 -0400557}
558
Florin Coras59cea1a2019-11-25 13:40:42 -0800559int
560vl_socket_client_init_shm (vl_api_shm_elem_config_t * config,
561 int want_pthread)
Florin Corasb384b542018-01-15 01:08:33 -0800562{
Florin Coras59cea1a2019-11-25 13:40:42 -0800563 return vl_socket_client_init_shm_internal (socket_client_ctx, config,
564 want_pthread);
565}
566
567int
568vl_socket_client_init_shm2 (socket_client_main_t * scm,
569 vl_api_shm_elem_config_t * config,
570 int want_pthread)
571{
572 socket_client_main_t *old_ctx;
573 int rv;
574
575 old_ctx = vl_socket_client_ctx_push (scm);
576 rv = vl_socket_client_init_shm_internal (socket_client_ctx, config,
577 want_pthread);
578 vl_socket_client_ctx_pop (old_ctx);
579 return rv;
580}
581
582clib_error_t *
583vl_socket_client_recv_fd_msg2 (socket_client_main_t * scm, int fds[],
584 int n_fds, u32 wait)
585{
Florin Corasb384b542018-01-15 01:08:33 -0800586 if (!scm->socket_fd)
587 return clib_error_return (0, "no socket");
Florin Corasd4c70922019-11-28 14:21:21 -0800588 return vl_sock_api_recv_fd_msg_internal (scm, fds, n_fds, wait);
Florin Corasb384b542018-01-15 01:08:33 -0800589}
590
Florin Coras59cea1a2019-11-25 13:40:42 -0800591clib_error_t *
592vl_socket_client_recv_fd_msg (int fds[], int n_fds, u32 wait)
593{
594 return vl_socket_client_recv_fd_msg2 (socket_client_ctx, fds, n_fds, wait);
595}
596
Dave Barach59b25652017-09-10 15:04:27 -0400597/*
598 * fd.io coding-style-patch-verification: ON
599 *
600 * Local Variables:
601 * eval: (c-set-style "gnu")
602 * End:
603 */