blob: 5fa19c6a9c040020a69eb492a82bbcaf2c66c503 [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
Nathan Moosbfa03982021-01-15 14:32:07 -080022#define _GNU_SOURCE
Florin Coras90a63982017-12-19 04:50:01 -080023#include <sys/socket.h>
Dave Barach59b25652017-09-10 15:04:27 -040024
Florin Coras4d9b9d82018-01-14 12:25:50 -080025#include <svm/ssvm.h>
Florin Corase86a8ed2018-01-05 03:20:25 -080026#include <vlibmemory/socket_client.h>
27#include <vlibmemory/memory_client.h>
Dave Barach59b25652017-09-10 15:04:27 -040028
29#include <vlibmemory/vl_memory_msg_enum.h>
30
31#define vl_typedefs /* define message structures */
32#include <vlibmemory/vl_memory_api_h.h>
33#undef vl_typedefs
34
35#define vl_endianfun /* define message structures */
36#include <vlibmemory/vl_memory_api_h.h>
37#undef vl_endianfun
38
Klement Sekera9b7e8ac2021-11-22 21:26:20 +010039#define vl_calcsizefun
40#include <vlibmemory/vl_memory_api_h.h>
41#undef vl_calcsizefun
42
Dave Barach59b25652017-09-10 15:04:27 -040043/* instantiate all the print functions we know about */
Dave Barach59b25652017-09-10 15:04:27 -040044#define vl_printfun
45#include <vlibmemory/vl_memory_api_h.h>
46#undef vl_printfun
47
48socket_client_main_t socket_client_main;
Florin Coras59cea1a2019-11-25 13:40:42 -080049__thread socket_client_main_t *socket_client_ctx = &socket_client_main;
Dave Barach59b25652017-09-10 15:04:27 -040050
51/* Debug aid */
52u32 vl (void *p) __attribute__ ((weak));
Florin Coras90a63982017-12-19 04:50:01 -080053
Dave Barach59b25652017-09-10 15:04:27 -040054u32
55vl (void *p)
56{
57 return vec_len (p);
58}
59
Florin Coras59cea1a2019-11-25 13:40:42 -080060static socket_client_main_t *
61vl_socket_client_ctx_push (socket_client_main_t * ctx)
Dave Barach59b25652017-09-10 15:04:27 -040062{
Florin Coras59cea1a2019-11-25 13:40:42 -080063 socket_client_main_t *old = socket_client_ctx;
64 socket_client_ctx = ctx;
65 return old;
66}
67
68static void
69vl_socket_client_ctx_pop (socket_client_main_t * old_ctx)
70{
71 socket_client_ctx = old_ctx;
72}
73
74static int
75vl_socket_client_read_internal (socket_client_main_t * scm, int wait)
76{
Florin Coras2881dec2018-10-02 18:29:25 -070077 u32 data_len = 0, msg_size;
Dave Barach59b25652017-09-10 15:04:27 -040078 int n, current_rx_index;
Florin Coras90a63982017-12-19 04:50:01 -080079 msgbuf_t *mbp = 0;
80 f64 timeout;
Dave Barach59b25652017-09-10 15:04:27 -040081
Florin Coras90a63982017-12-19 04:50:01 -080082 if (scm->socket_fd == 0)
83 return -1;
Dave Barach59b25652017-09-10 15:04:27 -040084
Florin Coras90a63982017-12-19 04:50:01 -080085 if (wait)
86 timeout = clib_time_now (&scm->clib_time) + wait;
Dave Barach59b25652017-09-10 15:04:27 -040087
88 while (1)
89 {
Benoît Gannea769a502023-01-26 19:28:16 +010090 current_rx_index = vec_len (scm->socket_rx_buffer);
91 while (current_rx_index < sizeof (*mbp))
Dave Barach59b25652017-09-10 15:04:27 -040092 {
93 vec_validate (scm->socket_rx_buffer, current_rx_index
94 + scm->socket_buffer_size - 1);
Dave Barach59b25652017-09-10 15:04:27 -040095 n = read (scm->socket_fd, scm->socket_rx_buffer + current_rx_index,
96 scm->socket_buffer_size);
97 if (n < 0)
98 {
Florin Coras66a10032018-12-21 16:23:09 -080099 if (errno == EAGAIN)
100 continue;
101
Dave Barach59b25652017-09-10 15:04:27 -0400102 clib_unix_warning ("socket_read");
Benoît Gannea769a502023-01-26 19:28:16 +0100103 vec_set_len (scm->socket_rx_buffer, current_rx_index);
Florin Coras90a63982017-12-19 04:50:01 -0800104 return -1;
Dave Barach59b25652017-09-10 15:04:27 -0400105 }
Benoît Gannea769a502023-01-26 19:28:16 +0100106 current_rx_index += n;
Dave Barach59b25652017-09-10 15:04:27 -0400107 }
Benoît Gannea769a502023-01-26 19:28:16 +0100108 vec_set_len (scm->socket_rx_buffer, current_rx_index);
Dave Barach59b25652017-09-10 15:04:27 -0400109
110#if CLIB_DEBUG > 1
111 if (n > 0)
112 clib_warning ("read %d bytes", n);
113#endif
114
Florin Coras2881dec2018-10-02 18:29:25 -0700115 mbp = (msgbuf_t *) (scm->socket_rx_buffer);
116 data_len = ntohl (mbp->data_len);
117 current_rx_index = vec_len (scm->socket_rx_buffer);
118 vec_validate (scm->socket_rx_buffer, current_rx_index + data_len);
Florin Coras2881dec2018-10-02 18:29:25 -0700119 mbp = (msgbuf_t *) (scm->socket_rx_buffer);
120 msg_size = data_len + sizeof (*mbp);
Dave Barach59b25652017-09-10 15:04:27 -0400121
Benoît Gannea769a502023-01-26 19:28:16 +0100122 while (current_rx_index < msg_size)
Florin Coras2881dec2018-10-02 18:29:25 -0700123 {
Benoît Gannea769a502023-01-26 19:28:16 +0100124 n = read (scm->socket_fd, scm->socket_rx_buffer + current_rx_index,
125 msg_size - current_rx_index);
Florin Coras66a10032018-12-21 16:23:09 -0800126 if (n < 0)
Florin Coras2881dec2018-10-02 18:29:25 -0700127 {
Florin Coras66a10032018-12-21 16:23:09 -0800128 if (errno == EAGAIN)
129 continue;
130
Florin Coras2881dec2018-10-02 18:29:25 -0700131 clib_unix_warning ("socket_read");
Benoît Gannea769a502023-01-26 19:28:16 +0100132 vec_set_len (scm->socket_rx_buffer, current_rx_index);
Florin Coras2881dec2018-10-02 18:29:25 -0700133 return -1;
134 }
Benoît Gannea769a502023-01-26 19:28:16 +0100135 current_rx_index += n;
Florin Coras2881dec2018-10-02 18:29:25 -0700136 }
Benoît Gannea769a502023-01-26 19:28:16 +0100137 vec_set_len (scm->socket_rx_buffer, current_rx_index);
Florin Coras2881dec2018-10-02 18:29:25 -0700138
139 if (vec_len (scm->socket_rx_buffer) >= data_len + sizeof (*mbp))
Dave Barach59b25652017-09-10 15:04:27 -0400140 {
Klement Sekera9b7e8ac2021-11-22 21:26:20 +0100141 vl_msg_api_socket_handler ((void *) (mbp->data), data_len);
Dave Barach59b25652017-09-10 15:04:27 -0400142
Florin Coras2881dec2018-10-02 18:29:25 -0700143 if (vec_len (scm->socket_rx_buffer) == data_len + sizeof (*mbp))
Damjan Marion8bea5892022-04-04 22:40:45 +0200144 vec_set_len (scm->socket_rx_buffer, 0);
Dave Barach59b25652017-09-10 15:04:27 -0400145 else
Florin Coras2881dec2018-10-02 18:29:25 -0700146 vec_delete (scm->socket_rx_buffer, data_len + sizeof (*mbp), 0);
Dave Barach59b25652017-09-10 15:04:27 -0400147 mbp = 0;
148
149 /* Quit if we're out of data, and not expecting a ping reply */
150 if (vec_len (scm->socket_rx_buffer) == 0
151 && scm->control_pings_outstanding == 0)
152 break;
153 }
Florin Coras90a63982017-12-19 04:50:01 -0800154 if (wait && clib_time_now (&scm->clib_time) >= timeout)
155 return -1;
Dave Barach59b25652017-09-10 15:04:27 -0400156 }
Florin Coras90a63982017-12-19 04:50:01 -0800157 return 0;
Dave Barach59b25652017-09-10 15:04:27 -0400158}
159
160int
Florin Coras59cea1a2019-11-25 13:40:42 -0800161vl_socket_client_read (int wait)
Dave Barach59b25652017-09-10 15:04:27 -0400162{
Florin Coras59cea1a2019-11-25 13:40:42 -0800163 return vl_socket_client_read_internal (socket_client_ctx, wait);
164}
165
166int
167vl_socket_client_read2 (socket_client_main_t * scm, int wait)
168{
169 socket_client_main_t *old_ctx;
170 int rv;
171
172 old_ctx = vl_socket_client_ctx_push (scm);
173 rv = vl_socket_client_read_internal (scm, wait);
174 vl_socket_client_ctx_pop (old_ctx);
175 return rv;
176}
177
178static int
179vl_socket_client_write_internal (socket_client_main_t * scm)
180{
Florin Coras90a63982017-12-19 04:50:01 -0800181 int n;
Benoît Ganne8a1d0792023-01-26 17:04:58 +0100182 int len = vec_len (scm->socket_tx_buffer);
Florin Coras90a63982017-12-19 04:50:01 -0800183 msgbuf_t msgbuf = {
184 .q = 0,
185 .gc_mark_timestamp = 0,
Benoît Ganne8a1d0792023-01-26 17:04:58 +0100186 .data_len = htonl (len),
Florin Coras90a63982017-12-19 04:50:01 -0800187 };
188
189 n = write (scm->socket_fd, &msgbuf, sizeof (msgbuf));
190 if (n < sizeof (msgbuf))
191 {
192 clib_unix_warning ("socket write (msgbuf)");
193 return -1;
194 }
195
Benoît Ganne8a1d0792023-01-26 17:04:58 +0100196 n = write (scm->socket_fd, scm->socket_tx_buffer, len);
197
198 vec_set_len (scm->socket_tx_buffer, 0);
199
200 if (n < len)
Florin Coras90a63982017-12-19 04:50:01 -0800201 {
202 clib_unix_warning ("socket write (msg)");
203 return -1;
204 }
205
206 return n;
207}
208
Florin Coras59cea1a2019-11-25 13:40:42 -0800209int
210vl_socket_client_write (void)
211{
212 return vl_socket_client_write_internal (socket_client_ctx);
213}
214
215int
216vl_socket_client_write2 (socket_client_main_t * scm)
217{
218 socket_client_main_t *old_ctx;
219 int rv;
220
221 old_ctx = vl_socket_client_ctx_push (scm);
222 rv = vl_socket_client_write_internal (scm);
223 vl_socket_client_ctx_pop (old_ctx);
224 return rv;
225}
226
227void *
228vl_socket_client_msg_alloc2 (socket_client_main_t * scm, int nbytes)
229{
Benoît Ganne8a1d0792023-01-26 17:04:58 +0100230 vec_set_len (scm->socket_tx_buffer, nbytes);
Florin Coras59cea1a2019-11-25 13:40:42 -0800231 return ((void *) scm->socket_tx_buffer);
232}
233
Florin Coras90a63982017-12-19 04:50:01 -0800234void *
235vl_socket_client_msg_alloc (int nbytes)
236{
Florin Coras59cea1a2019-11-25 13:40:42 -0800237 return vl_socket_client_msg_alloc2 (socket_client_ctx, nbytes);
Florin Coras90a63982017-12-19 04:50:01 -0800238}
239
240void
Florin Coras59cea1a2019-11-25 13:40:42 -0800241vl_socket_client_disconnect2 (socket_client_main_t * scm)
Florin Coras90a63982017-12-19 04:50:01 -0800242{
Florin Corasb384b542018-01-15 01:08:33 -0800243 if (vl_mem_client_is_connected ())
244 {
245 vl_client_disconnect_from_vlib_no_unmap ();
246 ssvm_delete_memfd (&scm->memfd_segment);
247 }
Florin Coras90a63982017-12-19 04:50:01 -0800248 if (scm->socket_fd && (close (scm->socket_fd) < 0))
249 clib_unix_warning ("close");
250 scm->socket_fd = 0;
251}
252
253void
Florin Coras59cea1a2019-11-25 13:40:42 -0800254vl_socket_client_disconnect (void)
255{
256 vl_socket_client_disconnect2 (socket_client_ctx);
257}
258
259void
260vl_socket_client_enable_disable2 (socket_client_main_t * scm, int enable)
261{
262 scm->socket_enable = enable;
263}
264
265void
Florin Coras90a63982017-12-19 04:50:01 -0800266vl_socket_client_enable_disable (int enable)
267{
Florin Coras59cea1a2019-11-25 13:40:42 -0800268 vl_socket_client_enable_disable2 (socket_client_ctx, enable);
Florin Coras90a63982017-12-19 04:50:01 -0800269}
270
Florin Corasd4c70922019-11-28 14:21:21 -0800271static clib_error_t *
272vl_sock_api_recv_fd_msg_internal (socket_client_main_t * scm, int fds[],
273 int n_fds, u32 wait)
Florin Coras90a63982017-12-19 04:50:01 -0800274{
275 char msgbuf[16];
Florin Coras466f2892018-08-03 02:50:43 -0700276 char ctl[CMSG_SPACE (sizeof (int) * n_fds)
277 + CMSG_SPACE (sizeof (struct ucred))];
Florin Coras90a63982017-12-19 04:50:01 -0800278 struct msghdr mh = { 0 };
279 struct iovec iov[1];
Florin Corasb384b542018-01-15 01:08:33 -0800280 ssize_t size = 0;
Florin Coras90a63982017-12-19 04:50:01 -0800281 struct ucred *cr = 0;
282 struct cmsghdr *cmsg;
283 pid_t pid __attribute__ ((unused));
284 uid_t uid __attribute__ ((unused));
285 gid_t gid __attribute__ ((unused));
Florin Corasd4c70922019-11-28 14:21:21 -0800286 int socket_fd;
Florin Corasb384b542018-01-15 01:08:33 -0800287 f64 timeout;
Florin Coras90a63982017-12-19 04:50:01 -0800288
Florin Corasd4c70922019-11-28 14:21:21 -0800289 socket_fd = scm->client_socket.fd;
290
Florin Coras90a63982017-12-19 04:50:01 -0800291 iov[0].iov_base = msgbuf;
292 iov[0].iov_len = 5;
293 mh.msg_iov = iov;
294 mh.msg_iovlen = 1;
295 mh.msg_control = ctl;
296 mh.msg_controllen = sizeof (ctl);
297
Dave Barachb7b92992018-10-17 10:38:51 -0400298 clib_memset (ctl, 0, sizeof (ctl));
Florin Coras90a63982017-12-19 04:50:01 -0800299
Florin Corasb384b542018-01-15 01:08:33 -0800300 if (wait != ~0)
301 {
302 timeout = clib_time_now (&scm->clib_time) + wait;
303 while (size != 5 && clib_time_now (&scm->clib_time) < timeout)
304 size = recvmsg (socket_fd, &mh, MSG_DONTWAIT);
305 }
306 else
307 size = recvmsg (socket_fd, &mh, 0);
308
Florin Coras90a63982017-12-19 04:50:01 -0800309 if (size != 5)
310 {
311 return (size == 0) ? clib_error_return (0, "disconnected") :
312 clib_error_return_unix (0, "recvmsg: malformed message (fd %d)",
313 socket_fd);
314 }
315
316 cmsg = CMSG_FIRSTHDR (&mh);
317 while (cmsg)
318 {
319 if (cmsg->cmsg_level == SOL_SOCKET)
320 {
321 if (cmsg->cmsg_type == SCM_CREDENTIALS)
322 {
323 cr = (struct ucred *) CMSG_DATA (cmsg);
324 uid = cr->uid;
325 gid = cr->gid;
326 pid = cr->pid;
327 }
328 else if (cmsg->cmsg_type == SCM_RIGHTS)
329 {
Dave Barach178cf492018-11-13 16:34:13 -0500330 clib_memcpy_fast (fds, CMSG_DATA (cmsg), sizeof (int) * n_fds);
Florin Coras90a63982017-12-19 04:50:01 -0800331 }
332 }
333 cmsg = CMSG_NXTHDR (&mh, cmsg);
334 }
335 return 0;
336}
337
Florin Corasd4c70922019-11-28 14:21:21 -0800338clib_error_t *
339vl_sock_api_recv_fd_msg (int socket_fd, int fds[], int n_fds, u32 wait)
340{
341 return vl_sock_api_recv_fd_msg_internal (socket_client_ctx, fds, n_fds,
342 wait);
343}
344
345clib_error_t *
346vl_sock_api_recv_fd_msg2 (socket_client_main_t * scm, int socket_fd,
347 int fds[], int n_fds, u32 wait)
348{
349 socket_client_main_t *old_ctx;
350 clib_error_t *error;
351
352 old_ctx = vl_socket_client_ctx_push (scm);
353 error = vl_sock_api_recv_fd_msg_internal (scm, fds, n_fds, wait);
354 vl_socket_client_ctx_pop (old_ctx);
355 return error;
356}
357
Florin Coras90a63982017-12-19 04:50:01 -0800358static void vl_api_sock_init_shm_reply_t_handler
359 (vl_api_sock_init_shm_reply_t * mp)
360{
Florin Coras59cea1a2019-11-25 13:40:42 -0800361 socket_client_main_t *scm = socket_client_ctx;
Florin Corasb384b542018-01-15 01:08:33 -0800362 ssvm_private_t *memfd = &scm->memfd_segment;
Florin Coras90a63982017-12-19 04:50:01 -0800363 i32 retval = ntohl (mp->retval);
Dave Barach39d69112019-11-27 11:42:13 -0500364 api_main_t *am = vlibapi_get_main ();
Florin Corasb384b542018-01-15 01:08:33 -0800365 clib_error_t *error;
366 int my_fd = -1;
Florin Coras90a63982017-12-19 04:50:01 -0800367 u8 *new_name;
368
369 if (retval)
370 {
371 clib_warning ("failed to init shmem");
372 return;
373 }
374
375 /*
376 * Check the socket for the magic fd
377 */
Florin Coras466f2892018-08-03 02:50:43 -0700378 error = vl_sock_api_recv_fd_msg (scm->socket_fd, &my_fd, 1, 5);
Florin Coras90a63982017-12-19 04:50:01 -0800379 if (error)
380 {
Florin Corasb384b542018-01-15 01:08:33 -0800381 clib_error_report (error);
Florin Coras90a63982017-12-19 04:50:01 -0800382 retval = -99;
383 return;
384 }
385
Dave Barachb7b92992018-10-17 10:38:51 -0400386 clib_memset (memfd, 0, sizeof (*memfd));
Florin Corasb384b542018-01-15 01:08:33 -0800387 memfd->fd = my_fd;
Florin Coras90a63982017-12-19 04:50:01 -0800388
389 /* Note: this closes memfd.fd */
Florin Coras5220a262020-09-29 18:11:24 -0700390 retval = ssvm_client_init_memfd (memfd);
Florin Coras90a63982017-12-19 04:50:01 -0800391 if (retval)
392 clib_warning ("WARNING: segment map returned %d", retval);
393
394 /*
395 * Pivot to the memory client segment that vpp just created
396 */
Florin Corasb384b542018-01-15 01:08:33 -0800397 am->vlib_rp = (void *) (memfd->requested_va + MMAP_PAGESIZE);
Florin Coras90a63982017-12-19 04:50:01 -0800398 am->shmem_hdr = (void *) am->vlib_rp->user_ctx;
399
400 new_name = format (0, "%v[shm]%c", scm->name, 0);
401 vl_client_install_client_message_handlers ();
Tomasz Kulasek97dcf5b2019-01-31 18:26:32 +0100402 if (scm->want_shm_pthread)
403 {
404 vl_client_connect_to_vlib_no_map ("pvt", (char *) new_name,
405 32 /* input_queue_length */ );
406 }
407 else
408 {
409 vl_client_connect_to_vlib_no_rx_pthread_no_map ("pvt",
410 (char *) new_name, 32
411 /* input_queue_length */
412 );
413 }
Florin Coras90a63982017-12-19 04:50:01 -0800414 vl_socket_client_enable_disable (0);
415 vec_free (new_name);
416}
417
418static void
419vl_api_sockclnt_create_reply_t_handler (vl_api_sockclnt_create_reply_t * mp)
420{
Florin Coras59cea1a2019-11-25 13:40:42 -0800421 socket_client_main_t *scm = socket_client_ctx;
Florin Coras90a63982017-12-19 04:50:01 -0800422 if (!mp->response)
Florin Coras2881dec2018-10-02 18:29:25 -0700423 {
424 scm->socket_enable = 1;
425 scm->client_index = clib_net_to_host_u32 (mp->index);
426 }
Florin Coras90a63982017-12-19 04:50:01 -0800427}
428
429#define foreach_sock_client_api_msg \
430_(SOCKCLNT_CREATE_REPLY, sockclnt_create_reply) \
431_(SOCK_INIT_SHM_REPLY, sock_init_shm_reply) \
432
Florin Coras90a63982017-12-19 04:50:01 -0800433void
434vl_sock_client_install_message_handlers (void)
435{
436
Filip Tehlar36217e32021-07-23 08:51:10 +0000437#define _(N, n) \
Damjan Mariona2eb5072022-05-20 20:06:01 +0200438 vl_msg_api_config (&(vl_msg_api_msg_config_t){ \
439 .id = VL_API_##N, \
440 .name = #n, \
441 .handler = vl_api_##n##_t_handler, \
442 .endian = vl_api_##n##_t_endian, \
443 .format_fn = vl_api_##n##_t_format, \
444 .size = sizeof (vl_api_##n##_t), \
445 .traced = 0, \
446 .tojson = vl_api_##n##_t_tojson, \
447 .fromjson = vl_api_##n##_t_fromjson, \
448 .calc_size = vl_api_##n##_t_calc_size, \
449 });
Florin Coras90a63982017-12-19 04:50:01 -0800450 foreach_sock_client_api_msg;
451#undef _
452}
453
454int
Florin Coras59cea1a2019-11-25 13:40:42 -0800455vl_socket_client_connect_internal (socket_client_main_t * scm,
456 char *socket_path, char *client_name,
457 u32 socket_buffer_size)
Florin Coras90a63982017-12-19 04:50:01 -0800458{
Dave Barach59b25652017-09-10 15:04:27 -0400459 vl_api_sockclnt_create_t *mp;
Florin Coras90a63982017-12-19 04:50:01 -0800460 clib_socket_t *sock;
Dave Barach59b25652017-09-10 15:04:27 -0400461 clib_error_t *error;
462
463 /* Already connected? */
464 if (scm->socket_fd)
465 return (-2);
466
467 /* bogus call? */
468 if (socket_path == 0 || client_name == 0)
469 return (-3);
470
Florin Coras90a63982017-12-19 04:50:01 -0800471 sock = &scm->client_socket;
Dave Barach59b25652017-09-10 15:04:27 -0400472 sock->config = socket_path;
Florin Coras5fe94572021-05-24 08:58:15 -0700473 sock->flags = CLIB_SOCKET_F_IS_CLIENT;
Dave Barach59b25652017-09-10 15:04:27 -0400474
Florin Coras90a63982017-12-19 04:50:01 -0800475 if ((error = clib_socket_init (sock)))
Dave Barach59b25652017-09-10 15:04:27 -0400476 {
477 clib_error_report (error);
478 return (-1);
479 }
480
Florin Coras90a63982017-12-19 04:50:01 -0800481 vl_sock_client_install_message_handlers ();
482
Dave Barach59b25652017-09-10 15:04:27 -0400483 scm->socket_fd = sock->fd;
Dave Barach59b25652017-09-10 15:04:27 -0400484 scm->socket_buffer_size = socket_buffer_size ? socket_buffer_size :
485 SOCKET_CLIENT_DEFAULT_BUFFER_SIZE;
486 vec_validate (scm->socket_tx_buffer, scm->socket_buffer_size - 1);
487 vec_validate (scm->socket_rx_buffer, scm->socket_buffer_size - 1);
Damjan Marion8bea5892022-04-04 22:40:45 +0200488 vec_set_len (scm->socket_rx_buffer, 0);
489 vec_set_len (scm->socket_tx_buffer, 0);
Florin Coras90a63982017-12-19 04:50:01 -0800490 scm->name = format (0, "%s", client_name);
Dave Barach59b25652017-09-10 15:04:27 -0400491
Florin Coras59cea1a2019-11-25 13:40:42 -0800492 mp = vl_socket_client_msg_alloc2 (scm, sizeof (*mp));
Florin Coras90a63982017-12-19 04:50:01 -0800493 mp->_vl_msg_id = htons (VL_API_SOCKCLNT_CREATE);
Ole Troan7adaa222019-08-27 15:05:27 +0200494 strncpy ((char *) mp->name, client_name, sizeof (mp->name) - 1);
495 mp->name[sizeof (mp->name) - 1] = 0;
Florin Coras90a63982017-12-19 04:50:01 -0800496 mp->context = 0xfeedface;
497
Florin Coras2881dec2018-10-02 18:29:25 -0700498 clib_time_init (&scm->clib_time);
499
Florin Coras59cea1a2019-11-25 13:40:42 -0800500 if (vl_socket_client_write_internal (scm) <= 0)
Florin Coras90a63982017-12-19 04:50:01 -0800501 return (-1);
502
Florin Coras59cea1a2019-11-25 13:40:42 -0800503 if (vl_socket_client_read_internal (scm, 5))
Florin Coras90a63982017-12-19 04:50:01 -0800504 return (-1);
505
Dave Barach59b25652017-09-10 15:04:27 -0400506 return (0);
507}
508
Florin Coras90a63982017-12-19 04:50:01 -0800509int
Florin Coras59cea1a2019-11-25 13:40:42 -0800510vl_socket_client_connect (char *socket_path, char *client_name,
511 u32 socket_buffer_size)
Dave Barach59b25652017-09-10 15:04:27 -0400512{
Florin Coras59cea1a2019-11-25 13:40:42 -0800513 return vl_socket_client_connect_internal (socket_client_ctx, socket_path,
514 client_name, socket_buffer_size);
515}
516
517int
518vl_socket_client_connect2 (socket_client_main_t * scm, char *socket_path,
519 char *client_name, u32 socket_buffer_size)
520{
521 socket_client_main_t *old_ctx;
522 int rv;
523
524 old_ctx = vl_socket_client_ctx_push (scm);
525 rv = vl_socket_client_connect_internal (socket_client_ctx, socket_path,
526 client_name, socket_buffer_size);
527 vl_socket_client_ctx_pop (old_ctx);
528 return rv;
529}
530
531int
532vl_socket_client_init_shm_internal (socket_client_main_t * scm,
533 vl_api_shm_elem_config_t * config,
534 int want_pthread)
535{
Florin Coras90a63982017-12-19 04:50:01 -0800536 vl_api_sock_init_shm_t *mp;
537 int rv, i;
538 u64 *cfg;
Dave Barach59b25652017-09-10 15:04:27 -0400539
Tomasz Kulasek97dcf5b2019-01-31 18:26:32 +0100540 scm->want_shm_pthread = want_pthread;
541
Florin Coras59cea1a2019-11-25 13:40:42 -0800542 mp = vl_socket_client_msg_alloc2 (scm, sizeof (*mp) +
543 vec_len (config) * sizeof (u64));
Dave Barachb7b92992018-10-17 10:38:51 -0400544 clib_memset (mp, 0, sizeof (*mp));
Florin Coras90a63982017-12-19 04:50:01 -0800545 mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_SOCK_INIT_SHM);
Florin Coras2881dec2018-10-02 18:29:25 -0700546 mp->client_index = clib_host_to_net_u32 (scm->client_index);
Florin Coras90a63982017-12-19 04:50:01 -0800547 mp->requested_size = 64 << 20;
548
549 if (config)
550 {
551 for (i = 0; i < vec_len (config); i++)
552 {
553 cfg = (u64 *) & config[i];
554 mp->configs[i] = *cfg;
555 }
556 mp->nitems = vec_len (config);
557 }
Florin Coras59cea1a2019-11-25 13:40:42 -0800558 rv = vl_socket_client_write_internal (scm);
Florin Coras90a63982017-12-19 04:50:01 -0800559 if (rv <= 0)
560 return rv;
561
Florin Coras59cea1a2019-11-25 13:40:42 -0800562 if (vl_socket_client_read_internal (scm, 1))
Florin Coras90a63982017-12-19 04:50:01 -0800563 return -1;
564
565 return 0;
Dave Barach59b25652017-09-10 15:04:27 -0400566}
567
Florin Coras59cea1a2019-11-25 13:40:42 -0800568int
569vl_socket_client_init_shm (vl_api_shm_elem_config_t * config,
570 int want_pthread)
Florin Corasb384b542018-01-15 01:08:33 -0800571{
Florin Coras59cea1a2019-11-25 13:40:42 -0800572 return vl_socket_client_init_shm_internal (socket_client_ctx, config,
573 want_pthread);
574}
575
576int
577vl_socket_client_init_shm2 (socket_client_main_t * scm,
578 vl_api_shm_elem_config_t * config,
579 int want_pthread)
580{
581 socket_client_main_t *old_ctx;
582 int rv;
583
584 old_ctx = vl_socket_client_ctx_push (scm);
585 rv = vl_socket_client_init_shm_internal (socket_client_ctx, config,
586 want_pthread);
587 vl_socket_client_ctx_pop (old_ctx);
588 return rv;
589}
590
591clib_error_t *
592vl_socket_client_recv_fd_msg2 (socket_client_main_t * scm, int fds[],
593 int n_fds, u32 wait)
594{
Florin Corasb384b542018-01-15 01:08:33 -0800595 if (!scm->socket_fd)
596 return clib_error_return (0, "no socket");
Florin Corasd4c70922019-11-28 14:21:21 -0800597 return vl_sock_api_recv_fd_msg_internal (scm, fds, n_fds, wait);
Florin Corasb384b542018-01-15 01:08:33 -0800598}
599
Florin Coras59cea1a2019-11-25 13:40:42 -0800600clib_error_t *
601vl_socket_client_recv_fd_msg (int fds[], int n_fds, u32 wait)
602{
603 return vl_socket_client_recv_fd_msg2 (socket_client_ctx, fds, n_fds, wait);
604}
605
Dave Barach59b25652017-09-10 15:04:27 -0400606/*
607 * fd.io coding-style-patch-verification: ON
608 *
609 * Local Variables:
610 * eval: (c-set-style "gnu")
611 * End:
612 */