blob: 3d933deecb69338f27ef1d7c6ce78ff821a0ce47 [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;
45
46/* Debug aid */
47u32 vl (void *p) __attribute__ ((weak));
Florin Coras90a63982017-12-19 04:50:01 -080048
Dave Barach59b25652017-09-10 15:04:27 -040049u32
50vl (void *p)
51{
52 return vec_len (p);
53}
54
Florin Coras90a63982017-12-19 04:50:01 -080055int
56vl_socket_client_read (int wait)
Dave Barach59b25652017-09-10 15:04:27 -040057{
Florin Coras90a63982017-12-19 04:50:01 -080058 socket_client_main_t *scm = &socket_client_main;
Dave Barach59b25652017-09-10 15:04:27 -040059 int n, current_rx_index;
Florin Coras90a63982017-12-19 04:50:01 -080060 msgbuf_t *mbp = 0;
61 f64 timeout;
Dave Barach59b25652017-09-10 15:04:27 -040062
Florin Coras90a63982017-12-19 04:50:01 -080063 if (scm->socket_fd == 0)
64 return -1;
Dave Barach59b25652017-09-10 15:04:27 -040065
Florin Coras90a63982017-12-19 04:50:01 -080066 if (wait)
67 timeout = clib_time_now (&scm->clib_time) + wait;
Dave Barach59b25652017-09-10 15:04:27 -040068
69 while (1)
70 {
71 current_rx_index = vec_len (scm->socket_rx_buffer);
72 while (vec_len (scm->socket_rx_buffer) <
73 sizeof (*mbp) + 2 /* msg id */ )
74 {
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);
80 if (n < 0)
81 {
82 clib_unix_warning ("socket_read");
Florin Coras90a63982017-12-19 04:50:01 -080083 return -1;
Dave Barach59b25652017-09-10 15:04:27 -040084 }
85 _vec_len (scm->socket_rx_buffer) += n;
86 }
87
88#if CLIB_DEBUG > 1
89 if (n > 0)
90 clib_warning ("read %d bytes", n);
91#endif
92
93 if (mbp == 0)
94 mbp = (msgbuf_t *) (scm->socket_rx_buffer);
95
96 if (vec_len (scm->socket_rx_buffer) >= ntohl (mbp->data_len)
97 + sizeof (*mbp))
98 {
99 vl_msg_api_socket_handler ((void *) (mbp->data));
100
101 if (vec_len (scm->socket_rx_buffer) == ntohl (mbp->data_len)
102 + sizeof (*mbp))
103 _vec_len (scm->socket_rx_buffer) = 0;
104 else
105 vec_delete (scm->socket_rx_buffer, ntohl (mbp->data_len)
106 + sizeof (*mbp), 0);
107 mbp = 0;
108
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)
112 break;
113 }
Florin Coras90a63982017-12-19 04:50:01 -0800114
115 if (wait && clib_time_now (&scm->clib_time) >= timeout)
116 return -1;
Dave Barach59b25652017-09-10 15:04:27 -0400117 }
Florin Coras90a63982017-12-19 04:50:01 -0800118 return 0;
Dave Barach59b25652017-09-10 15:04:27 -0400119}
120
121int
Florin Coras90a63982017-12-19 04:50:01 -0800122vl_socket_client_write (void)
Dave Barach59b25652017-09-10 15:04:27 -0400123{
Florin Coras90a63982017-12-19 04:50:01 -0800124 socket_client_main_t *scm = &socket_client_main;
125 int n;
126
127 msgbuf_t msgbuf = {
128 .q = 0,
129 .gc_mark_timestamp = 0,
130 .data_len = htonl (scm->socket_tx_nbytes),
131 };
132
133 n = write (scm->socket_fd, &msgbuf, sizeof (msgbuf));
134 if (n < sizeof (msgbuf))
135 {
136 clib_unix_warning ("socket write (msgbuf)");
137 return -1;
138 }
139
140 n = write (scm->socket_fd, scm->socket_tx_buffer, scm->socket_tx_nbytes);
141 if (n < scm->socket_tx_nbytes)
142 {
143 clib_unix_warning ("socket write (msg)");
144 return -1;
145 }
146
147 return n;
148}
149
150void *
151vl_socket_client_msg_alloc (int nbytes)
152{
153 socket_client_main.socket_tx_nbytes = nbytes;
154 return ((void *) socket_client_main.socket_tx_buffer);
155}
156
157void
158vl_socket_client_disconnect (void)
159{
160 socket_client_main_t *scm = &socket_client_main;
Florin Corasb384b542018-01-15 01:08:33 -0800161
162 if (vl_mem_client_is_connected ())
163 {
164 vl_client_disconnect_from_vlib_no_unmap ();
165 ssvm_delete_memfd (&scm->memfd_segment);
166 }
Florin Coras90a63982017-12-19 04:50:01 -0800167 if (scm->socket_fd && (close (scm->socket_fd) < 0))
168 clib_unix_warning ("close");
169 scm->socket_fd = 0;
170}
171
172void
173vl_socket_client_enable_disable (int enable)
174{
175 socket_client_main_t *scm = &socket_client_main;
176 scm->socket_enable = enable;
177}
178
Florin Corasb384b542018-01-15 01:08:33 -0800179clib_error_t *
Florin Coras466f2892018-08-03 02:50:43 -0700180vl_sock_api_recv_fd_msg (int socket_fd, int fds[], int n_fds, u32 wait)
Florin Coras90a63982017-12-19 04:50:01 -0800181{
Florin Corasb384b542018-01-15 01:08:33 -0800182 socket_client_main_t *scm = &socket_client_main;
Florin Coras90a63982017-12-19 04:50:01 -0800183 char msgbuf[16];
Florin Coras466f2892018-08-03 02:50:43 -0700184 char ctl[CMSG_SPACE (sizeof (int) * n_fds)
185 + CMSG_SPACE (sizeof (struct ucred))];
Florin Coras90a63982017-12-19 04:50:01 -0800186 struct msghdr mh = { 0 };
187 struct iovec iov[1];
Florin Corasb384b542018-01-15 01:08:33 -0800188 ssize_t size = 0;
Florin Coras90a63982017-12-19 04:50:01 -0800189 struct ucred *cr = 0;
190 struct cmsghdr *cmsg;
191 pid_t pid __attribute__ ((unused));
192 uid_t uid __attribute__ ((unused));
193 gid_t gid __attribute__ ((unused));
Florin Corasb384b542018-01-15 01:08:33 -0800194 f64 timeout;
Florin Coras90a63982017-12-19 04:50:01 -0800195
196 iov[0].iov_base = msgbuf;
197 iov[0].iov_len = 5;
198 mh.msg_iov = iov;
199 mh.msg_iovlen = 1;
200 mh.msg_control = ctl;
201 mh.msg_controllen = sizeof (ctl);
202
203 memset (ctl, 0, sizeof (ctl));
204
Florin Corasb384b542018-01-15 01:08:33 -0800205 if (wait != ~0)
206 {
207 timeout = clib_time_now (&scm->clib_time) + wait;
208 while (size != 5 && clib_time_now (&scm->clib_time) < timeout)
209 size = recvmsg (socket_fd, &mh, MSG_DONTWAIT);
210 }
211 else
212 size = recvmsg (socket_fd, &mh, 0);
213
Florin Coras90a63982017-12-19 04:50:01 -0800214 if (size != 5)
215 {
216 return (size == 0) ? clib_error_return (0, "disconnected") :
217 clib_error_return_unix (0, "recvmsg: malformed message (fd %d)",
218 socket_fd);
219 }
220
221 cmsg = CMSG_FIRSTHDR (&mh);
222 while (cmsg)
223 {
224 if (cmsg->cmsg_level == SOL_SOCKET)
225 {
226 if (cmsg->cmsg_type == SCM_CREDENTIALS)
227 {
228 cr = (struct ucred *) CMSG_DATA (cmsg);
229 uid = cr->uid;
230 gid = cr->gid;
231 pid = cr->pid;
232 }
233 else if (cmsg->cmsg_type == SCM_RIGHTS)
234 {
Florin Coras466f2892018-08-03 02:50:43 -0700235 clib_memcpy (fds, CMSG_DATA (cmsg), sizeof (int) * n_fds);
Florin Coras90a63982017-12-19 04:50:01 -0800236 }
237 }
238 cmsg = CMSG_NXTHDR (&mh, cmsg);
239 }
240 return 0;
241}
242
243static void vl_api_sock_init_shm_reply_t_handler
244 (vl_api_sock_init_shm_reply_t * mp)
245{
246 socket_client_main_t *scm = &socket_client_main;
Florin Corasb384b542018-01-15 01:08:33 -0800247 ssvm_private_t *memfd = &scm->memfd_segment;
Florin Coras90a63982017-12-19 04:50:01 -0800248 i32 retval = ntohl (mp->retval);
Florin Coras90a63982017-12-19 04:50:01 -0800249 api_main_t *am = &api_main;
Florin Corasb384b542018-01-15 01:08:33 -0800250 clib_error_t *error;
251 int my_fd = -1;
Florin Coras90a63982017-12-19 04:50:01 -0800252 u8 *new_name;
253
254 if (retval)
255 {
256 clib_warning ("failed to init shmem");
257 return;
258 }
259
260 /*
261 * Check the socket for the magic fd
262 */
Florin Coras466f2892018-08-03 02:50:43 -0700263 error = vl_sock_api_recv_fd_msg (scm->socket_fd, &my_fd, 1, 5);
Florin Coras90a63982017-12-19 04:50:01 -0800264 if (error)
265 {
Florin Corasb384b542018-01-15 01:08:33 -0800266 clib_error_report (error);
Florin Coras90a63982017-12-19 04:50:01 -0800267 retval = -99;
268 return;
269 }
270
Florin Corasb384b542018-01-15 01:08:33 -0800271 memset (memfd, 0, sizeof (*memfd));
272 memfd->fd = my_fd;
Florin Coras90a63982017-12-19 04:50:01 -0800273
274 /* Note: this closes memfd.fd */
Florin Corasb384b542018-01-15 01:08:33 -0800275 retval = ssvm_slave_init_memfd (memfd);
Florin Coras90a63982017-12-19 04:50:01 -0800276 if (retval)
277 clib_warning ("WARNING: segment map returned %d", retval);
278
279 /*
280 * Pivot to the memory client segment that vpp just created
281 */
Florin Corasb384b542018-01-15 01:08:33 -0800282 am->vlib_rp = (void *) (memfd->requested_va + MMAP_PAGESIZE);
Florin Coras90a63982017-12-19 04:50:01 -0800283 am->shmem_hdr = (void *) am->vlib_rp->user_ctx;
284
285 new_name = format (0, "%v[shm]%c", scm->name, 0);
286 vl_client_install_client_message_handlers ();
287 vl_client_connect_to_vlib_no_map ("pvt", (char *) new_name,
288 32 /* input_queue_length */ );
289 vl_socket_client_enable_disable (0);
290 vec_free (new_name);
291}
292
293static void
294vl_api_sockclnt_create_reply_t_handler (vl_api_sockclnt_create_reply_t * mp)
295{
296 socket_client_main_t *scm = &socket_client_main;
297 if (!mp->response)
298 scm->socket_enable = 1;
299}
300
301#define foreach_sock_client_api_msg \
302_(SOCKCLNT_CREATE_REPLY, sockclnt_create_reply) \
303_(SOCK_INIT_SHM_REPLY, sock_init_shm_reply) \
304
305static void
306noop_handler (void *notused)
307{
308}
309
310void
311vl_sock_client_install_message_handlers (void)
312{
313
314#define _(N,n) \
315 vl_msg_api_set_handlers(VL_API_##N, #n, \
316 vl_api_##n##_t_handler, \
317 noop_handler, \
318 vl_api_##n##_t_endian, \
319 vl_api_##n##_t_print, \
320 sizeof(vl_api_##n##_t), 1);
321 foreach_sock_client_api_msg;
322#undef _
323}
324
325int
326vl_socket_client_connect (char *socket_path, char *client_name,
327 u32 socket_buffer_size)
328{
329 socket_client_main_t *scm = &socket_client_main;
Dave Barach59b25652017-09-10 15:04:27 -0400330 vl_api_sockclnt_create_t *mp;
Florin Coras90a63982017-12-19 04:50:01 -0800331 clib_socket_t *sock;
Dave Barach59b25652017-09-10 15:04:27 -0400332 clib_error_t *error;
333
334 /* Already connected? */
335 if (scm->socket_fd)
336 return (-2);
337
338 /* bogus call? */
339 if (socket_path == 0 || client_name == 0)
340 return (-3);
341
Florin Coras90a63982017-12-19 04:50:01 -0800342 sock = &scm->client_socket;
Dave Barach59b25652017-09-10 15:04:27 -0400343 sock->config = socket_path;
344 sock->flags = CLIB_SOCKET_F_IS_CLIENT | CLIB_SOCKET_F_SEQPACKET;
345
Florin Coras90a63982017-12-19 04:50:01 -0800346 if ((error = clib_socket_init (sock)))
Dave Barach59b25652017-09-10 15:04:27 -0400347 {
348 clib_error_report (error);
349 return (-1);
350 }
351
Florin Coras90a63982017-12-19 04:50:01 -0800352 vl_sock_client_install_message_handlers ();
353
Dave Barach59b25652017-09-10 15:04:27 -0400354 scm->socket_fd = sock->fd;
Dave Barach59b25652017-09-10 15:04:27 -0400355 scm->socket_buffer_size = socket_buffer_size ? socket_buffer_size :
356 SOCKET_CLIENT_DEFAULT_BUFFER_SIZE;
357 vec_validate (scm->socket_tx_buffer, scm->socket_buffer_size - 1);
358 vec_validate (scm->socket_rx_buffer, scm->socket_buffer_size - 1);
359 _vec_len (scm->socket_rx_buffer) = 0;
Florin Coras90a63982017-12-19 04:50:01 -0800360 _vec_len (scm->socket_tx_buffer) = 0;
361 scm->name = format (0, "%s", client_name);
Dave Barach59b25652017-09-10 15:04:27 -0400362
Florin Coras90a63982017-12-19 04:50:01 -0800363 mp = vl_socket_client_msg_alloc (sizeof (*mp));
364 mp->_vl_msg_id = htons (VL_API_SOCKCLNT_CREATE);
365 strncpy ((char *) mp->name, client_name, sizeof (mp->name) - 1);
366 mp->name[sizeof (mp->name) - 1] = 0;
367 mp->context = 0xfeedface;
368
369 if (vl_socket_client_write () <= 0)
370 return (-1);
371
372 if (vl_socket_client_read (1))
373 return (-1);
374
375 clib_time_init (&scm->clib_time);
Dave Barach59b25652017-09-10 15:04:27 -0400376 return (0);
377}
378
Florin Coras90a63982017-12-19 04:50:01 -0800379int
380vl_socket_client_init_shm (vl_api_shm_elem_config_t * config)
Dave Barach59b25652017-09-10 15:04:27 -0400381{
Florin Coras90a63982017-12-19 04:50:01 -0800382 vl_api_sock_init_shm_t *mp;
383 int rv, i;
384 u64 *cfg;
Dave Barach59b25652017-09-10 15:04:27 -0400385
Florin Coras90a63982017-12-19 04:50:01 -0800386 mp = vl_socket_client_msg_alloc (sizeof (*mp) +
387 vec_len (config) * sizeof (u64));
388 memset (mp, 0, sizeof (*mp));
389 mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_SOCK_INIT_SHM);
390 mp->client_index = ~0;
391 mp->requested_size = 64 << 20;
392
393 if (config)
394 {
395 for (i = 0; i < vec_len (config); i++)
396 {
397 cfg = (u64 *) & config[i];
398 mp->configs[i] = *cfg;
399 }
400 mp->nitems = vec_len (config);
401 }
402 rv = vl_socket_client_write ();
403 if (rv <= 0)
404 return rv;
405
406 if (vl_socket_client_read (1))
407 return -1;
408
409 return 0;
Dave Barach59b25652017-09-10 15:04:27 -0400410}
411
Florin Corasb384b542018-01-15 01:08:33 -0800412clib_error_t *
Florin Coras466f2892018-08-03 02:50:43 -0700413vl_socket_client_recv_fd_msg (int fds[], int n_fds, u32 wait)
Florin Corasb384b542018-01-15 01:08:33 -0800414{
415 socket_client_main_t *scm = &socket_client_main;
416 if (!scm->socket_fd)
417 return clib_error_return (0, "no socket");
Florin Coras466f2892018-08-03 02:50:43 -0700418 return vl_sock_api_recv_fd_msg (scm->client_socket.fd, fds, n_fds, wait);
Florin Corasb384b542018-01-15 01:08:33 -0800419}
420
Dave Barach59b25652017-09-10 15:04:27 -0400421/*
422 * fd.io coding-style-patch-verification: ON
423 *
424 * Local Variables:
425 * eval: (c-set-style "gnu")
426 * End:
427 */