Line data Source code
1 : /*
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>
21 : #define __USE_GNU
22 : #define _GNU_SOURCE
23 : #include <sys/socket.h>
24 :
25 : #include <svm/ssvm.h>
26 : #include <vlibmemory/socket_client.h>
27 : #include <vlibmemory/memory_client.h>
28 :
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 :
39 : #define vl_calcsizefun
40 : #include <vlibmemory/vl_memory_api_h.h>
41 : #undef vl_calcsizefun
42 :
43 : /* instantiate all the print functions we know about */
44 : #define vl_printfun
45 : #include <vlibmemory/vl_memory_api_h.h>
46 : #undef vl_printfun
47 :
48 : socket_client_main_t socket_client_main;
49 : __thread socket_client_main_t *socket_client_ctx = &socket_client_main;
50 :
51 : /* Debug aid */
52 : u32 vl (void *p) __attribute__ ((weak));
53 :
54 : u32
55 0 : vl (void *p)
56 : {
57 0 : return vec_len (p);
58 : }
59 :
60 : static socket_client_main_t *
61 58 : vl_socket_client_ctx_push (socket_client_main_t * ctx)
62 : {
63 58 : socket_client_main_t *old = socket_client_ctx;
64 58 : socket_client_ctx = ctx;
65 58 : return old;
66 : }
67 :
68 : static void
69 58 : vl_socket_client_ctx_pop (socket_client_main_t * old_ctx)
70 : {
71 58 : socket_client_ctx = old_ctx;
72 58 : }
73 :
74 : static int
75 58 : vl_socket_client_read_internal (socket_client_main_t * scm, int wait)
76 : {
77 58 : u32 data_len = 0, msg_size;
78 : int n, current_rx_index;
79 58 : msgbuf_t *mbp = 0;
80 : f64 timeout;
81 :
82 58 : if (scm->socket_fd == 0)
83 0 : return -1;
84 :
85 58 : if (wait)
86 58 : timeout = clib_time_now (&scm->clib_time) + wait;
87 :
88 : while (1)
89 : {
90 58 : current_rx_index = vec_len (scm->socket_rx_buffer);
91 282652 : while (current_rx_index < sizeof (*mbp))
92 : {
93 282594 : vec_validate (scm->socket_rx_buffer, current_rx_index
94 : + scm->socket_buffer_size - 1);
95 565188 : n = read (scm->socket_fd, scm->socket_rx_buffer + current_rx_index,
96 282594 : scm->socket_buffer_size);
97 282594 : if (n < 0)
98 : {
99 282536 : if (errno == EAGAIN)
100 282536 : continue;
101 :
102 0 : clib_unix_warning ("socket_read");
103 0 : vec_set_len (scm->socket_rx_buffer, current_rx_index);
104 0 : return -1;
105 : }
106 58 : current_rx_index += n;
107 : }
108 58 : vec_set_len (scm->socket_rx_buffer, current_rx_index);
109 :
110 : #if CLIB_DEBUG > 1
111 : if (n > 0)
112 : clib_warning ("read %d bytes", n);
113 : #endif
114 :
115 58 : mbp = (msgbuf_t *) (scm->socket_rx_buffer);
116 58 : data_len = ntohl (mbp->data_len);
117 58 : current_rx_index = vec_len (scm->socket_rx_buffer);
118 58 : vec_validate (scm->socket_rx_buffer, current_rx_index + data_len);
119 58 : mbp = (msgbuf_t *) (scm->socket_rx_buffer);
120 58 : msg_size = data_len + sizeof (*mbp);
121 :
122 87 : while (current_rx_index < msg_size)
123 : {
124 58 : n = read (scm->socket_fd, scm->socket_rx_buffer + current_rx_index,
125 29 : msg_size - current_rx_index);
126 29 : if (n < 0)
127 : {
128 0 : if (errno == EAGAIN)
129 0 : continue;
130 :
131 0 : clib_unix_warning ("socket_read");
132 0 : vec_set_len (scm->socket_rx_buffer, current_rx_index);
133 0 : return -1;
134 : }
135 29 : current_rx_index += n;
136 : }
137 58 : vec_set_len (scm->socket_rx_buffer, current_rx_index);
138 :
139 58 : if (vec_len (scm->socket_rx_buffer) >= data_len + sizeof (*mbp))
140 : {
141 58 : vl_msg_api_socket_handler ((void *) (mbp->data), data_len);
142 :
143 58 : if (vec_len (scm->socket_rx_buffer) == data_len + sizeof (*mbp))
144 58 : vec_set_len (scm->socket_rx_buffer, 0);
145 : else
146 0 : vec_delete (scm->socket_rx_buffer, data_len + sizeof (*mbp), 0);
147 58 : mbp = 0;
148 :
149 : /* Quit if we're out of data, and not expecting a ping reply */
150 58 : if (vec_len (scm->socket_rx_buffer) == 0
151 58 : && scm->control_pings_outstanding == 0)
152 58 : break;
153 : }
154 0 : if (wait && clib_time_now (&scm->clib_time) >= timeout)
155 0 : return -1;
156 : }
157 58 : return 0;
158 : }
159 :
160 : int
161 0 : vl_socket_client_read (int wait)
162 : {
163 0 : return vl_socket_client_read_internal (socket_client_ctx, wait);
164 : }
165 :
166 : int
167 0 : vl_socket_client_read2 (socket_client_main_t * scm, int wait)
168 : {
169 : socket_client_main_t *old_ctx;
170 : int rv;
171 :
172 0 : old_ctx = vl_socket_client_ctx_push (scm);
173 0 : rv = vl_socket_client_read_internal (scm, wait);
174 0 : vl_socket_client_ctx_pop (old_ctx);
175 0 : return rv;
176 : }
177 :
178 : static int
179 58 : vl_socket_client_write_internal (socket_client_main_t * scm)
180 : {
181 : int n;
182 58 : int len = vec_len (scm->socket_tx_buffer);
183 58 : msgbuf_t msgbuf = {
184 : .q = 0,
185 : .gc_mark_timestamp = 0,
186 58 : .data_len = htonl (len),
187 : };
188 :
189 58 : n = write (scm->socket_fd, &msgbuf, sizeof (msgbuf));
190 58 : if (n < sizeof (msgbuf))
191 : {
192 0 : clib_unix_warning ("socket write (msgbuf)");
193 0 : return -1;
194 : }
195 :
196 58 : n = write (scm->socket_fd, scm->socket_tx_buffer, len);
197 :
198 58 : vec_set_len (scm->socket_tx_buffer, 0);
199 :
200 58 : if (n < len)
201 : {
202 0 : clib_unix_warning ("socket write (msg)");
203 0 : return -1;
204 : }
205 :
206 58 : return n;
207 : }
208 :
209 : int
210 0 : vl_socket_client_write (void)
211 : {
212 0 : return vl_socket_client_write_internal (socket_client_ctx);
213 : }
214 :
215 : int
216 0 : vl_socket_client_write2 (socket_client_main_t * scm)
217 : {
218 : socket_client_main_t *old_ctx;
219 : int rv;
220 :
221 0 : old_ctx = vl_socket_client_ctx_push (scm);
222 0 : rv = vl_socket_client_write_internal (scm);
223 0 : vl_socket_client_ctx_pop (old_ctx);
224 0 : return rv;
225 : }
226 :
227 : void *
228 58 : vl_socket_client_msg_alloc2 (socket_client_main_t * scm, int nbytes)
229 : {
230 58 : vec_set_len (scm->socket_tx_buffer, nbytes);
231 58 : return ((void *) scm->socket_tx_buffer);
232 : }
233 :
234 : void *
235 0 : vl_socket_client_msg_alloc (int nbytes)
236 : {
237 0 : return vl_socket_client_msg_alloc2 (socket_client_ctx, nbytes);
238 : }
239 :
240 : void
241 29 : vl_socket_client_disconnect2 (socket_client_main_t * scm)
242 : {
243 29 : if (vl_mem_client_is_connected ())
244 : {
245 29 : vl_client_disconnect_from_vlib_no_unmap ();
246 29 : ssvm_delete_memfd (&scm->memfd_segment);
247 : }
248 29 : if (scm->socket_fd && (close (scm->socket_fd) < 0))
249 0 : clib_unix_warning ("close");
250 29 : scm->socket_fd = 0;
251 29 : }
252 :
253 : void
254 0 : vl_socket_client_disconnect (void)
255 : {
256 0 : vl_socket_client_disconnect2 (socket_client_ctx);
257 0 : }
258 :
259 : void
260 29 : vl_socket_client_enable_disable2 (socket_client_main_t * scm, int enable)
261 : {
262 29 : scm->socket_enable = enable;
263 29 : }
264 :
265 : void
266 29 : vl_socket_client_enable_disable (int enable)
267 : {
268 29 : vl_socket_client_enable_disable2 (socket_client_ctx, enable);
269 29 : }
270 :
271 : static clib_error_t *
272 88 : vl_sock_api_recv_fd_msg_internal (socket_client_main_t * scm, int fds[],
273 : int n_fds, u32 wait)
274 88 : {
275 : char msgbuf[16];
276 88 : char ctl[CMSG_SPACE (sizeof (int) * n_fds)
277 88 : + CMSG_SPACE (sizeof (struct ucred))];
278 88 : struct msghdr mh = { 0 };
279 : struct iovec iov[1];
280 88 : ssize_t size = 0;
281 88 : 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));
286 : int socket_fd;
287 : f64 timeout;
288 :
289 88 : socket_fd = scm->client_socket.fd;
290 :
291 88 : iov[0].iov_base = msgbuf;
292 88 : iov[0].iov_len = 5;
293 88 : mh.msg_iov = iov;
294 88 : mh.msg_iovlen = 1;
295 88 : mh.msg_control = ctl;
296 88 : mh.msg_controllen = sizeof (ctl);
297 :
298 88 : clib_memset (ctl, 0, sizeof (ctl));
299 :
300 88 : if (wait != ~0)
301 : {
302 88 : timeout = clib_time_now (&scm->clib_time) + wait;
303 42835 : while (size != 5 && clib_time_now (&scm->clib_time) < timeout)
304 42747 : size = recvmsg (socket_fd, &mh, MSG_DONTWAIT);
305 : }
306 : else
307 0 : size = recvmsg (socket_fd, &mh, 0);
308 :
309 88 : if (size != 5)
310 : {
311 0 : return (size == 0) ? clib_error_return (0, "disconnected") :
312 0 : clib_error_return_unix (0, "recvmsg: malformed message (fd %d)",
313 : socket_fd);
314 : }
315 :
316 88 : cmsg = CMSG_FIRSTHDR (&mh);
317 176 : while (cmsg)
318 : {
319 88 : if (cmsg->cmsg_level == SOL_SOCKET)
320 : {
321 88 : if (cmsg->cmsg_type == SCM_CREDENTIALS)
322 : {
323 0 : cr = (struct ucred *) CMSG_DATA (cmsg);
324 0 : uid = cr->uid;
325 0 : gid = cr->gid;
326 0 : pid = cr->pid;
327 : }
328 88 : else if (cmsg->cmsg_type == SCM_RIGHTS)
329 : {
330 88 : clib_memcpy_fast (fds, CMSG_DATA (cmsg), sizeof (int) * n_fds);
331 : }
332 : }
333 88 : cmsg = CMSG_NXTHDR (&mh, cmsg);
334 : }
335 88 : return 0;
336 : }
337 :
338 : clib_error_t *
339 29 : vl_sock_api_recv_fd_msg (int socket_fd, int fds[], int n_fds, u32 wait)
340 : {
341 29 : return vl_sock_api_recv_fd_msg_internal (socket_client_ctx, fds, n_fds,
342 : wait);
343 : }
344 :
345 : clib_error_t *
346 0 : vl_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 0 : old_ctx = vl_socket_client_ctx_push (scm);
353 0 : error = vl_sock_api_recv_fd_msg_internal (scm, fds, n_fds, wait);
354 0 : vl_socket_client_ctx_pop (old_ctx);
355 0 : return error;
356 : }
357 :
358 29 : static void vl_api_sock_init_shm_reply_t_handler
359 : (vl_api_sock_init_shm_reply_t * mp)
360 : {
361 29 : socket_client_main_t *scm = socket_client_ctx;
362 29 : ssvm_private_t *memfd = &scm->memfd_segment;
363 29 : i32 retval = ntohl (mp->retval);
364 29 : api_main_t *am = vlibapi_get_main ();
365 : clib_error_t *error;
366 29 : int my_fd = -1;
367 : u8 *new_name;
368 :
369 29 : if (retval)
370 : {
371 0 : clib_warning ("failed to init shmem");
372 0 : return;
373 : }
374 :
375 : /*
376 : * Check the socket for the magic fd
377 : */
378 29 : error = vl_sock_api_recv_fd_msg (scm->socket_fd, &my_fd, 1, 5);
379 29 : if (error)
380 : {
381 0 : clib_error_report (error);
382 0 : retval = -99;
383 0 : return;
384 : }
385 :
386 29 : clib_memset (memfd, 0, sizeof (*memfd));
387 29 : memfd->fd = my_fd;
388 :
389 : /* Note: this closes memfd.fd */
390 29 : retval = ssvm_client_init_memfd (memfd);
391 29 : if (retval)
392 0 : clib_warning ("WARNING: segment map returned %d", retval);
393 :
394 : /*
395 : * Pivot to the memory client segment that vpp just created
396 : */
397 29 : am->vlib_rp = (void *) (memfd->requested_va + MMAP_PAGESIZE);
398 29 : am->shmem_hdr = (void *) am->vlib_rp->user_ctx;
399 :
400 29 : new_name = format (0, "%v[shm]%c", scm->name, 0);
401 29 : vl_client_install_client_message_handlers ();
402 29 : if (scm->want_shm_pthread)
403 : {
404 29 : vl_client_connect_to_vlib_no_map ("pvt", (char *) new_name,
405 : 32 /* input_queue_length */ );
406 : }
407 : else
408 : {
409 0 : vl_client_connect_to_vlib_no_rx_pthread_no_map ("pvt",
410 : (char *) new_name, 32
411 : /* input_queue_length */
412 : );
413 : }
414 29 : vl_socket_client_enable_disable (0);
415 29 : vec_free (new_name);
416 : }
417 :
418 : static void
419 29 : vl_api_sockclnt_create_reply_t_handler (vl_api_sockclnt_create_reply_t * mp)
420 : {
421 29 : socket_client_main_t *scm = socket_client_ctx;
422 29 : if (!mp->response)
423 : {
424 29 : scm->socket_enable = 1;
425 29 : scm->client_index = clib_net_to_host_u32 (mp->index);
426 : }
427 29 : }
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 :
433 : void
434 29 : vl_sock_client_install_message_handlers (void)
435 : {
436 :
437 : #define _(N, n) \
438 : 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 : });
450 29 : foreach_sock_client_api_msg;
451 : #undef _
452 29 : }
453 :
454 : int
455 29 : vl_socket_client_connect_internal (socket_client_main_t * scm,
456 : char *socket_path, char *client_name,
457 : u32 socket_buffer_size)
458 : {
459 : vl_api_sockclnt_create_t *mp;
460 : clib_socket_t *sock;
461 : clib_error_t *error;
462 :
463 : /* Already connected? */
464 29 : if (scm->socket_fd)
465 0 : return (-2);
466 :
467 : /* bogus call? */
468 29 : if (socket_path == 0 || client_name == 0)
469 0 : return (-3);
470 :
471 29 : sock = &scm->client_socket;
472 29 : sock->config = socket_path;
473 29 : sock->flags = CLIB_SOCKET_F_IS_CLIENT;
474 :
475 29 : if ((error = clib_socket_init (sock)))
476 : {
477 0 : clib_error_report (error);
478 0 : return (-1);
479 : }
480 :
481 29 : vl_sock_client_install_message_handlers ();
482 :
483 29 : scm->socket_fd = sock->fd;
484 29 : scm->socket_buffer_size = socket_buffer_size ? socket_buffer_size :
485 : SOCKET_CLIENT_DEFAULT_BUFFER_SIZE;
486 29 : vec_validate (scm->socket_tx_buffer, scm->socket_buffer_size - 1);
487 29 : vec_validate (scm->socket_rx_buffer, scm->socket_buffer_size - 1);
488 29 : vec_set_len (scm->socket_rx_buffer, 0);
489 29 : vec_set_len (scm->socket_tx_buffer, 0);
490 29 : scm->name = format (0, "%s", client_name);
491 :
492 29 : mp = vl_socket_client_msg_alloc2 (scm, sizeof (*mp));
493 29 : mp->_vl_msg_id = htons (VL_API_SOCKCLNT_CREATE);
494 29 : strncpy ((char *) mp->name, client_name, sizeof (mp->name) - 1);
495 29 : mp->name[sizeof (mp->name) - 1] = 0;
496 29 : mp->context = 0xfeedface;
497 :
498 29 : clib_time_init (&scm->clib_time);
499 :
500 29 : if (vl_socket_client_write_internal (scm) <= 0)
501 0 : return (-1);
502 :
503 29 : if (vl_socket_client_read_internal (scm, 5))
504 0 : return (-1);
505 :
506 29 : return (0);
507 : }
508 :
509 : int
510 0 : vl_socket_client_connect (char *socket_path, char *client_name,
511 : u32 socket_buffer_size)
512 : {
513 0 : return vl_socket_client_connect_internal (socket_client_ctx, socket_path,
514 : client_name, socket_buffer_size);
515 : }
516 :
517 : int
518 29 : vl_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 29 : old_ctx = vl_socket_client_ctx_push (scm);
525 29 : rv = vl_socket_client_connect_internal (socket_client_ctx, socket_path,
526 : client_name, socket_buffer_size);
527 29 : vl_socket_client_ctx_pop (old_ctx);
528 29 : return rv;
529 : }
530 :
531 : int
532 29 : vl_socket_client_init_shm_internal (socket_client_main_t * scm,
533 : vl_api_shm_elem_config_t * config,
534 : int want_pthread)
535 : {
536 : vl_api_sock_init_shm_t *mp;
537 : int rv, i;
538 : u64 *cfg;
539 :
540 29 : scm->want_shm_pthread = want_pthread;
541 :
542 29 : mp = vl_socket_client_msg_alloc2 (scm, sizeof (*mp) +
543 0 : vec_len (config) * sizeof (u64));
544 29 : clib_memset (mp, 0, sizeof (*mp));
545 29 : mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_SOCK_INIT_SHM);
546 29 : mp->client_index = clib_host_to_net_u32 (scm->client_index);
547 29 : mp->requested_size = 64 << 20;
548 :
549 29 : if (config)
550 : {
551 0 : for (i = 0; i < vec_len (config); i++)
552 : {
553 0 : cfg = (u64 *) & config[i];
554 0 : mp->configs[i] = *cfg;
555 : }
556 0 : mp->nitems = vec_len (config);
557 : }
558 29 : rv = vl_socket_client_write_internal (scm);
559 29 : if (rv <= 0)
560 0 : return rv;
561 :
562 29 : if (vl_socket_client_read_internal (scm, 1))
563 0 : return -1;
564 :
565 29 : return 0;
566 : }
567 :
568 : int
569 0 : vl_socket_client_init_shm (vl_api_shm_elem_config_t * config,
570 : int want_pthread)
571 : {
572 0 : return vl_socket_client_init_shm_internal (socket_client_ctx, config,
573 : want_pthread);
574 : }
575 :
576 : int
577 29 : vl_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 29 : old_ctx = vl_socket_client_ctx_push (scm);
585 29 : rv = vl_socket_client_init_shm_internal (socket_client_ctx, config,
586 : want_pthread);
587 29 : vl_socket_client_ctx_pop (old_ctx);
588 29 : return rv;
589 : }
590 :
591 : clib_error_t *
592 59 : vl_socket_client_recv_fd_msg2 (socket_client_main_t * scm, int fds[],
593 : int n_fds, u32 wait)
594 : {
595 59 : if (!scm->socket_fd)
596 0 : return clib_error_return (0, "no socket");
597 59 : return vl_sock_api_recv_fd_msg_internal (scm, fds, n_fds, wait);
598 : }
599 :
600 : clib_error_t *
601 0 : vl_socket_client_recv_fd_msg (int fds[], int n_fds, u32 wait)
602 : {
603 0 : return vl_socket_client_recv_fd_msg2 (socket_client_ctx, fds, n_fds, wait);
604 : }
605 :
606 : /*
607 : * fd.io coding-style-patch-verification: ON
608 : *
609 : * Local Variables:
610 : * eval: (c-set-style "gnu")
611 : * End:
612 : */
|