CUSTOM: relayd protocol: ignore reply on relayd_send_index and relayd_send_close_stream
[lttng-tools.git] / src / common / relayd / relayd.c
1 /*
2 * Copyright (C) 2012 - David Goulet <dgoulet@efficios.com>
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License, version 2 only, as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
11 * more details.
12 *
13 * You should have received a copy of the GNU General Public License along with
14 * this program; if not, write to the Free Software Foundation, Inc., 51
15 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16 */
17
18 #define _LGPL_SOURCE
19 #include <assert.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <sys/stat.h>
24 #include <inttypes.h>
25
26 #include <common/common.h>
27 #include <common/defaults.h>
28 #include <common/compat/endian.h>
29 #include <common/sessiond-comm/relayd.h>
30 #include <common/index/ctf-index.h>
31
32 #include "relayd.h"
33
34 /*
35 * Send command. Fill up the header and append the data.
36 */
37 static int send_command(struct lttcomm_relayd_sock *rsock,
38 enum lttcomm_relayd_command cmd, void *data, size_t size,
39 int flags)
40 {
41 int ret;
42 struct lttcomm_relayd_hdr header;
43 char *buf;
44 uint64_t buf_size = sizeof(header);
45
46 if (rsock->sock.fd < 0) {
47 return -ECONNRESET;
48 }
49
50 if (data) {
51 buf_size += size;
52 }
53
54 buf = zmalloc(buf_size);
55 if (buf == NULL) {
56 PERROR("zmalloc relayd send command buf");
57 ret = -1;
58 goto alloc_error;
59 }
60
61 memset(&header, 0, sizeof(header));
62 header.cmd = htobe32(cmd);
63 header.data_size = htobe64(size);
64
65 /* Zeroed for now since not used. */
66 header.cmd_version = 0;
67 header.circuit_id = 0;
68
69 /* Prepare buffer to send. */
70 memcpy(buf, &header, sizeof(header));
71 if (data) {
72 memcpy(buf + sizeof(header), data, size);
73 }
74
75 DBG3("Relayd sending command %d of size %" PRIu64, (int) cmd, buf_size);
76 ret = rsock->sock.ops->sendmsg(&rsock->sock, buf, buf_size, flags);
77 if (ret < 0) {
78 PERROR("Failed to send command %d of size %" PRIu64,
79 (int) cmd, buf_size);
80 ret = -errno;
81 goto error;
82 }
83 error:
84 free(buf);
85 alloc_error:
86 return ret;
87 }
88
89 static int recv_reply_ignore(struct lttcomm_relayd_sock *rsock, size_t size)
90 {
91 int ret;
92 size_t cutfoff = 128;
93
94 /*
95 * To prevent ever growing size of recv_reply to ignore, if the number
96 * of bytes we want to ignore is bigger than `cutoff`, consume half of
97 * the cutoff. We might block on it but still, most of bytes to ignore
98 * should already be ready to consume at this point.
99 *
100 * This kind of scenario can easily happen on stopped session with a
101 * live_timer since no actual receive is done on the socket that would
102 * discard the `ignore` portion.
103 *
104 * TCP guarantee in-order transmission both on send and receive so this
105 * is safe to do.
106 */
107 if (rsock->bytes_to_ignore_on_recv >= cutfoff) {
108 size_t to_discard = cutfoff / 2;
109
110 ret = rsock->sock.ops->recvmsg(&rsock->sock, NULL, to_discard, MSG_TRUNC);
111 if (ret <= 0 || ret != to_discard) {
112 if (ret == 0) {
113 /* Orderly shutdown. */
114 DBG("Socket %d has performed an orderly shutdown", rsock->sock.fd);
115 } else {
116 DBG("Receiving reply to discard failed on sock %d for size %zu with ret %d",
117 rsock->sock.fd, to_discard, ret);
118 }
119 ret = -1;
120 goto error;
121 }
122
123 DBG("Force discard of %zu bytes for socket %d", to_discard, rsock->sock.fd);
124 rsock->bytes_to_ignore_on_recv -= to_discard;
125 }
126
127 DBG3("Relayd ignore reply of %zu bytes for socket %d.", size, rsock->sock.fd);
128 /* Do not wait for the current reply to be ignored */
129 rsock->bytes_to_ignore_on_recv += size;
130 ret = 0;
131
132 error:
133 return ret;
134 }
135
136 /*
137 * Receive reply data on socket. This MUST be call after send_command or else
138 * could result in unexpected behavior(s).
139 */
140 static int recv_reply(struct lttcomm_relayd_sock *rsock, void *data, size_t size)
141 {
142 int ret;
143
144 if (rsock->sock.fd < 0) {
145 return -ECONNRESET;
146 }
147
148 /*
149 * We have to consume the bytes that are marked to ignore.
150 */
151 if (rsock->bytes_to_ignore_on_recv != 0) {
152 ret = rsock->sock.ops->recvmsg(&rsock->sock, NULL, rsock->bytes_to_ignore_on_recv, MSG_TRUNC);
153 if (ret <= 0 || ret != rsock->bytes_to_ignore_on_recv) {
154 if (ret == 0) {
155 /* Orderly shutdown. */
156 DBG("Socket %d has performed an orderly shutdown", rsock->sock.fd);
157 } else {
158 DBG("Receiving reply to skip failed on sock %d for size %zu with ret %d",
159 rsock->sock.fd, rsock->bytes_to_ignore_on_recv, ret);
160 }
161 ret = -1;
162 goto error;
163 }
164 DBG("Discarded %zu bytes on sock %d", rsock->bytes_to_ignore_on_recv, rsock->sock.fd);
165 rsock->bytes_to_ignore_on_recv = 0;
166 }
167
168 DBG3("Relayd waiting for reply of size %zu", size);
169
170 ret = rsock->sock.ops->recvmsg(&rsock->sock, data, size, 0);
171 if (ret <= 0 || ret != size) {
172 if (ret == 0) {
173 /* Orderly shutdown. */
174 DBG("Socket %d has performed an orderly shutdown", rsock->sock.fd);
175 } else {
176 DBG("Receiving reply failed on sock %d for size %zu with ret %d",
177 rsock->sock.fd, size, ret);
178 }
179 /* Always return -1 here and the caller can use errno. */
180 ret = -1;
181 goto error;
182 }
183
184 error:
185 return ret;
186 }
187
188 /*
189 * Starting at 2.4, RELAYD_CREATE_SESSION takes additional parameters to
190 * support the live reading capability.
191 */
192 static int relayd_create_session_2_4(struct lttcomm_relayd_sock *rsock,
193 uint64_t *session_id, char *session_name, char *hostname,
194 int session_live_timer, unsigned int snapshot)
195 {
196 int ret;
197 struct lttcomm_relayd_create_session_2_4 msg;
198
199 if (lttng_strncpy(msg.session_name, session_name,
200 sizeof(msg.session_name))) {
201 ret = -1;
202 goto error;
203 }
204 if (lttng_strncpy(msg.hostname, hostname, sizeof(msg.hostname))) {
205 ret = -1;
206 goto error;
207 }
208 msg.live_timer = htobe32(session_live_timer);
209 msg.snapshot = htobe32(snapshot);
210
211 /* Send command */
212 ret = send_command(rsock, RELAYD_CREATE_SESSION, &msg, sizeof(msg), 0);
213 if (ret < 0) {
214 goto error;
215 }
216
217 error:
218 return ret;
219 }
220
221 /*
222 * RELAYD_CREATE_SESSION from 2.1 to 2.3.
223 */
224 static int relayd_create_session_2_1(struct lttcomm_relayd_sock *rsock,
225 uint64_t *session_id)
226 {
227 int ret;
228
229 /* Send command */
230 ret = send_command(rsock, RELAYD_CREATE_SESSION, NULL, 0, 0);
231 if (ret < 0) {
232 goto error;
233 }
234
235 error:
236 return ret;
237 }
238
239 /*
240 * Send a RELAYD_CREATE_SESSION command to the relayd with the given socket and
241 * set session_id of the relayd if we have a successful reply from the relayd.
242 *
243 * On success, return 0 else a negative value which is either an errno error or
244 * a lttng error code from the relayd.
245 */
246 int relayd_create_session(struct lttcomm_relayd_sock *rsock, uint64_t *session_id,
247 char *session_name, char *hostname, int session_live_timer,
248 unsigned int snapshot)
249 {
250 int ret;
251 struct lttcomm_relayd_status_session reply;
252
253 assert(rsock);
254 assert(session_id);
255
256 DBG("Relayd create session");
257
258 switch(rsock->minor) {
259 case 1:
260 case 2:
261 case 3:
262 ret = relayd_create_session_2_1(rsock, session_id);
263 break;
264 case 4:
265 default:
266 ret = relayd_create_session_2_4(rsock, session_id, session_name,
267 hostname, session_live_timer, snapshot);
268 break;
269 }
270
271 if (ret < 0) {
272 goto error;
273 }
274
275 /* Receive response */
276 ret = recv_reply(rsock, (void *) &reply, sizeof(reply));
277 if (ret < 0) {
278 goto error;
279 }
280
281 reply.session_id = be64toh(reply.session_id);
282 reply.ret_code = be32toh(reply.ret_code);
283
284 /* Return session id or negative ret code. */
285 if (reply.ret_code != LTTNG_OK) {
286 ret = -1;
287 ERR("Relayd create session replied error %d", reply.ret_code);
288 goto error;
289 } else {
290 ret = 0;
291 *session_id = reply.session_id;
292 }
293
294 DBG("Relayd session created with id %" PRIu64, reply.session_id);
295
296 error:
297 return ret;
298 }
299
300 /*
301 * Add stream on the relayd and assign stream handle to the stream_id argument.
302 *
303 * On success return 0 else return ret_code negative value.
304 */
305 int relayd_add_stream(struct lttcomm_relayd_sock *rsock, const char *channel_name,
306 const char *pathname, uint64_t *stream_id,
307 uint64_t tracefile_size, uint64_t tracefile_count)
308 {
309 int ret;
310 struct lttcomm_relayd_add_stream msg;
311 struct lttcomm_relayd_add_stream_2_2 msg_2_2;
312 struct lttcomm_relayd_status_stream reply;
313
314 /* Code flow error. Safety net. */
315 assert(rsock);
316 assert(channel_name);
317 assert(pathname);
318
319 DBG("Relayd adding stream for channel name %s", channel_name);
320
321 /* Compat with relayd 2.1 */
322 if (rsock->minor == 1) {
323 memset(&msg, 0, sizeof(msg));
324 if (lttng_strncpy(msg.channel_name, channel_name,
325 sizeof(msg.channel_name))) {
326 ret = -1;
327 goto error;
328 }
329 if (lttng_strncpy(msg.pathname, pathname,
330 sizeof(msg.pathname))) {
331 ret = -1;
332 goto error;
333 }
334
335 /* Send command */
336 ret = send_command(rsock, RELAYD_ADD_STREAM, (void *) &msg, sizeof(msg), 0);
337 if (ret < 0) {
338 goto error;
339 }
340 } else {
341 memset(&msg_2_2, 0, sizeof(msg_2_2));
342 /* Compat with relayd 2.2+ */
343 if (lttng_strncpy(msg_2_2.channel_name, channel_name,
344 sizeof(msg_2_2.channel_name))) {
345 ret = -1;
346 goto error;
347 }
348 if (lttng_strncpy(msg_2_2.pathname, pathname,
349 sizeof(msg_2_2.pathname))) {
350 ret = -1;
351 goto error;
352 }
353 msg_2_2.tracefile_size = htobe64(tracefile_size);
354 msg_2_2.tracefile_count = htobe64(tracefile_count);
355
356 /* Send command */
357 ret = send_command(rsock, RELAYD_ADD_STREAM, (void *) &msg_2_2, sizeof(msg_2_2), 0);
358 if (ret < 0) {
359 goto error;
360 }
361 }
362
363 /* Waiting for reply */
364 ret = recv_reply(rsock, (void *) &reply, sizeof(reply));
365 if (ret < 0) {
366 goto error;
367 }
368
369 /* Back to host bytes order. */
370 reply.handle = be64toh(reply.handle);
371 reply.ret_code = be32toh(reply.ret_code);
372
373 /* Return session id or negative ret code. */
374 if (reply.ret_code != LTTNG_OK) {
375 ret = -1;
376 ERR("Relayd add stream replied error %d", reply.ret_code);
377 } else {
378 /* Success */
379 ret = 0;
380 *stream_id = reply.handle;
381 }
382
383 DBG("Relayd stream added successfully with handle %" PRIu64,
384 reply.handle);
385
386 error:
387 return ret;
388 }
389
390 /*
391 * Add stream on the relayd. Send part.
392 *
393 * On success return 0 else return ret_code negative value.
394 */
395 int relayd_add_stream_send(struct lttcomm_relayd_sock *rsock, const char *channel_name,
396 const char *pathname, uint64_t tracefile_size, uint64_t tracefile_count)
397 {
398 int ret;
399 struct lttcomm_relayd_add_stream msg;
400 struct lttcomm_relayd_add_stream_2_2 msg_2_2;
401
402 /* Code flow error. Safety net. */
403 assert(rsock);
404 assert(channel_name);
405 assert(pathname);
406
407 DBG("Relayd adding stream for channel name %s. Part send", channel_name);
408
409 /* Compat with relayd 2.1 */
410 if (rsock->minor == 1) {
411 memset(&msg, 0, sizeof(msg));
412 if (lttng_strncpy(msg.channel_name, channel_name,
413 sizeof(msg.channel_name))) {
414 ret = -1;
415 goto error;
416 }
417 if (lttng_strncpy(msg.pathname, pathname,
418 sizeof(msg.pathname))) {
419 ret = -1;
420 goto error;
421 }
422
423 /* Send command */
424 ret = send_command(rsock, RELAYD_ADD_STREAM, (void *) &msg, sizeof(msg), 0);
425 if (ret < 0) {
426 goto error;
427 }
428 } else {
429 memset(&msg_2_2, 0, sizeof(msg_2_2));
430 /* Compat with relayd 2.2+ */
431 if (lttng_strncpy(msg_2_2.channel_name, channel_name,
432 sizeof(msg_2_2.channel_name))) {
433 ret = -1;
434 goto error;
435 }
436 if (lttng_strncpy(msg_2_2.pathname, pathname,
437 sizeof(msg_2_2.pathname))) {
438 ret = -1;
439 goto error;
440 }
441 msg_2_2.tracefile_size = htobe64(tracefile_size);
442 msg_2_2.tracefile_count = htobe64(tracefile_count);
443
444 /* Send command */
445 ret = send_command(rsock, RELAYD_ADD_STREAM, (void *) &msg_2_2, sizeof(msg_2_2), 0);
446 if (ret < 0) {
447 goto error;
448 }
449 }
450
451 DBG("Relayd add stream sent for channel name %s.", channel_name);
452 ret = 0;
453
454 error:
455 return ret;
456 }
457
458 int relayd_add_stream_rcv(struct lttcomm_relayd_sock *rsock, uint64_t *_stream_id)
459 {
460 int ret;
461 struct lttcomm_relayd_status_stream reply;
462
463 /* Code flow error. Safety net. */
464 assert(rsock);
465
466 /* Waiting for reply */
467 ret = recv_reply(rsock, (void *) &reply, sizeof(reply));
468 if (ret < 0) {
469 goto error;
470 }
471
472 /* Back to host bytes order. */
473 reply.handle = be64toh(reply.handle);
474 reply.ret_code = be32toh(reply.ret_code);
475
476 /* Return session id or negative ret code. */
477 if (reply.ret_code != LTTNG_OK) {
478 ret = -1;
479 ERR("Relayd add stream replied error %d", reply.ret_code);
480 } else {
481 /* Success */
482 ret = 0;
483 *_stream_id = reply.handle;
484 }
485
486 DBG("Relayd stream added successfully with handle %" PRIu64,
487 reply.handle);
488
489 error:
490 return ret;
491 }
492
493 /*
494 * Inform the relay that all the streams for the current channel has been sent.
495 *
496 * On success return 0 else return ret_code negative value.
497 */
498 int relayd_streams_sent(struct lttcomm_relayd_sock *rsock)
499 {
500 int ret;
501 struct lttcomm_relayd_generic_reply reply;
502
503 /* Code flow error. Safety net. */
504 assert(rsock);
505
506 DBG("Relayd sending streams sent.");
507
508 /* This feature was introduced in 2.4, ignore it for earlier versions. */
509 if (rsock->minor < 4) {
510 ret = 0;
511 goto end;
512 }
513
514 /* Send command */
515 ret = send_command(rsock, RELAYD_STREAMS_SENT, NULL, 0, 0);
516 if (ret < 0) {
517 goto error;
518 }
519
520 /* Waiting for reply */
521 ret = recv_reply(rsock, (void *) &reply, sizeof(reply));
522 if (ret < 0) {
523 goto error;
524 }
525
526 /* Back to host bytes order. */
527 reply.ret_code = be32toh(reply.ret_code);
528
529 /* Return session id or negative ret code. */
530 if (reply.ret_code != LTTNG_OK) {
531 ret = -1;
532 ERR("Relayd streams sent replied error %d", reply.ret_code);
533 goto error;
534 } else {
535 /* Success */
536 ret = 0;
537 }
538
539 DBG("Relayd streams sent success");
540
541 error:
542 end:
543 return ret;
544 }
545
546 /*
547 * Check version numbers on the relayd.
548 * If major versions are compatible, we assign minor_to_use to the
549 * minor version of the procotol we are going to use for this session.
550 *
551 * Return 0 if the two daemons are compatible, LTTNG_ERR_RELAYD_VERSION_FAIL
552 * otherwise, or a negative value on network errors.
553 */
554 int relayd_version_check(struct lttcomm_relayd_sock *rsock)
555 {
556 int ret;
557 struct lttcomm_relayd_version msg;
558
559 /* Code flow error. Safety net. */
560 assert(rsock);
561
562 DBG("Relayd version check for major.minor %u.%u", rsock->major,
563 rsock->minor);
564
565 memset(&msg, 0, sizeof(msg));
566 /* Prepare network byte order before transmission. */
567 msg.major = htobe32(rsock->major);
568 msg.minor = htobe32(rsock->minor);
569
570 /* Send command */
571 ret = send_command(rsock, RELAYD_VERSION, (void *) &msg, sizeof(msg), 0);
572 if (ret < 0) {
573 goto error;
574 }
575
576 /* Receive response */
577 ret = recv_reply(rsock, (void *) &msg, sizeof(msg));
578 if (ret < 0) {
579 goto error;
580 }
581
582 /* Set back to host bytes order */
583 msg.major = be32toh(msg.major);
584 msg.minor = be32toh(msg.minor);
585
586 /*
587 * Only validate the major version. If the other side is higher,
588 * communication is not possible. Only major version equal can talk to each
589 * other. If the minor version differs, the lowest version is used by both
590 * sides.
591 */
592 if (msg.major != rsock->major) {
593 /* Not compatible */
594 ret = LTTNG_ERR_RELAYD_VERSION_FAIL;
595 DBG2("Relayd version is NOT compatible. Relayd version %u != %u (us)",
596 msg.major, rsock->major);
597 goto error;
598 }
599
600 /*
601 * If the relayd's minor version is higher, it will adapt to our version so
602 * we can continue to use the latest relayd communication data structure.
603 * If the received minor version is higher, the relayd should adapt to us.
604 */
605 if (rsock->minor > msg.minor) {
606 rsock->minor = msg.minor;
607 }
608
609 /* Version number compatible */
610 DBG2("Relayd version is compatible, using protocol version %u.%u",
611 rsock->major, rsock->minor);
612 ret = 0;
613
614 error:
615 return ret;
616 }
617
618 /*
619 * Add stream on the relayd and assign stream handle to the stream_id argument.
620 *
621 * On success return 0 else return ret_code negative value.
622 */
623 int relayd_send_metadata(struct lttcomm_relayd_sock *rsock, size_t len)
624 {
625 int ret;
626
627 /* Code flow error. Safety net. */
628 assert(rsock);
629
630 DBG("Relayd sending metadata of size %zu", len);
631
632 /* Send command */
633 ret = send_command(rsock, RELAYD_SEND_METADATA, NULL, len, 0);
634 if (ret < 0) {
635 goto error;
636 }
637
638 DBG2("Relayd metadata added successfully");
639
640 /*
641 * After that call, the metadata data MUST be sent to the relayd so the
642 * receive size on the other end matches the len of the metadata packet
643 * header. This is why we don't wait for a reply here.
644 */
645
646 error:
647 return ret;
648 }
649
650 /*
651 * Connect to relay daemon with an allocated lttcomm_relayd_sock.
652 */
653 int relayd_connect(struct lttcomm_relayd_sock *rsock)
654 {
655 /* Code flow error. Safety net. */
656 assert(rsock);
657
658 if (!rsock->sock.ops) {
659 /*
660 * Attempting a connect on a non-initialized socket.
661 */
662 return -ECONNRESET;
663 }
664
665 DBG3("Relayd connect ...");
666
667 return rsock->sock.ops->connect(&rsock->sock);
668 }
669
670 /*
671 * Close relayd socket with an allocated lttcomm_relayd_sock.
672 *
673 * If no socket operations are found, simply return 0 meaning that everything
674 * is fine. Without operations, the socket can not possibly be opened or used.
675 * This is possible if the socket was allocated but not created. However, the
676 * caller could simply use it to store a valid file descriptor for instance
677 * passed over a Unix socket and call this to cleanup but still without a valid
678 * ops pointer.
679 *
680 * Return the close returned value. On error, a negative value is usually
681 * returned back from close(2).
682 */
683 int relayd_close(struct lttcomm_relayd_sock *rsock)
684 {
685 int ret;
686
687 /* Code flow error. Safety net. */
688 assert(rsock);
689
690 /* An invalid fd is fine, return success. */
691 if (rsock->sock.fd < 0) {
692 ret = 0;
693 goto end;
694 }
695
696 /*
697 * This ensure that we do not close the socket while the lttng-relayd
698 * expects to be able to send a response that we skipped.
699 * While we loose some time to receive everything, this keep the
700 * protocol intact from the point of view of lttng-relayd.
701 */
702 if (rsock->bytes_to_ignore_on_recv != 0) {
703 ret = rsock->sock.ops->recvmsg(&rsock->sock, NULL, rsock->bytes_to_ignore_on_recv, MSG_TRUNC);
704 if (ret <= 0 || ret != rsock->bytes_to_ignore_on_recv) {
705 if (ret == 0) {
706 /* Orderly shutdown. */
707 DBG("Socket %d has performed an orderly shutdown", rsock->sock.fd);
708 } else {
709 DBG("Receiving reply to skip failed on sock %d for size %zu with ret %d",
710 rsock->sock.fd, rsock->bytes_to_ignore_on_recv, ret);
711 }
712 }
713 DBG("Discarded %zu bytes on sock %d", rsock->bytes_to_ignore_on_recv, rsock->sock.fd);
714 rsock->bytes_to_ignore_on_recv = 0;
715 }
716
717 DBG3("Relayd closing socket %d", rsock->sock.fd);
718
719 if (rsock->sock.ops) {
720 ret = rsock->sock.ops->close(&rsock->sock);
721 } else {
722 /* Default call if no specific ops found. */
723 ret = close(rsock->sock.fd);
724 if (ret < 0) {
725 PERROR("relayd_close default close");
726 }
727 }
728 rsock->sock.fd = -1;
729
730 end:
731 return ret;
732 }
733
734 /*
735 * Send data header structure to the relayd.
736 */
737 int relayd_send_data_hdr(struct lttcomm_relayd_sock *rsock,
738 struct lttcomm_relayd_data_hdr *hdr, size_t size)
739 {
740 int ret;
741
742 /* Code flow error. Safety net. */
743 assert(rsock);
744 assert(hdr);
745
746 if (rsock->sock.fd < 0) {
747 return -ECONNRESET;
748 }
749
750 DBG3("Relayd sending data header of size %zu", size);
751
752 /* Again, safety net */
753 if (size == 0) {
754 size = sizeof(struct lttcomm_relayd_data_hdr);
755 }
756
757 /* Only send data header. */
758 ret = rsock->sock.ops->sendmsg(&rsock->sock, hdr, size, 0);
759 if (ret < 0) {
760 ret = -errno;
761 goto error;
762 }
763
764 /*
765 * The data MUST be sent right after that command for the receive on the
766 * other end to match the size in the header.
767 */
768
769 error:
770 return ret;
771 }
772
773 /*
774 * Send close stream command to the relayd.
775 */
776 int relayd_send_close_stream(struct lttcomm_relayd_sock *rsock, uint64_t stream_id,
777 uint64_t last_net_seq_num)
778 {
779 int ret;
780 struct lttcomm_relayd_close_stream msg;
781 struct lttcomm_relayd_generic_reply reply;
782
783 /* Code flow error. Safety net. */
784 assert(rsock);
785
786 DBG("Relayd closing stream id %" PRIu64, stream_id);
787
788 memset(&msg, 0, sizeof(msg));
789 msg.stream_id = htobe64(stream_id);
790 msg.last_net_seq_num = htobe64(last_net_seq_num);
791
792 /* Send command */
793 ret = send_command(rsock, RELAYD_CLOSE_STREAM, (void *) &msg, sizeof(msg), 0);
794 if (ret < 0) {
795 goto error;
796 }
797
798 /*
799 * Discard response since we do not really care for it and that TCP
800 * guarantee in-order delivery. As for error handling, there is not much
801 * to do at this point (closing).
802 **/
803 ret = recv_reply_ignore(rsock, sizeof(reply));
804 if (ret < 0) {
805 goto error;
806 }
807
808 DBG("Relayd close stream id %" PRIu64 " successfully", stream_id);
809
810 error:
811 return ret;
812 }
813
814 /*
815 * Check for data availability for a given stream id.
816 *
817 * Return 0 if NOT pending, 1 if so and a negative value on error.
818 */
819 int relayd_data_pending(struct lttcomm_relayd_sock *rsock, uint64_t stream_id,
820 uint64_t last_net_seq_num)
821 {
822 int ret;
823 struct lttcomm_relayd_data_pending msg;
824 struct lttcomm_relayd_generic_reply reply;
825
826 /* Code flow error. Safety net. */
827 assert(rsock);
828
829 DBG("Relayd data pending for stream id %" PRIu64, stream_id);
830
831 memset(&msg, 0, sizeof(msg));
832 msg.stream_id = htobe64(stream_id);
833 msg.last_net_seq_num = htobe64(last_net_seq_num);
834
835 /* Send command */
836 ret = send_command(rsock, RELAYD_DATA_PENDING, (void *) &msg,
837 sizeof(msg), 0);
838 if (ret < 0) {
839 goto error;
840 }
841
842 /* Receive response */
843 ret = recv_reply(rsock, (void *) &reply, sizeof(reply));
844 if (ret < 0) {
845 goto error;
846 }
847
848 reply.ret_code = be32toh(reply.ret_code);
849
850 /* Return session id or negative ret code. */
851 if (reply.ret_code >= LTTNG_OK) {
852 ERR("Relayd data pending replied error %d", reply.ret_code);
853 }
854
855 /* At this point, the ret code is either 1 or 0 */
856 ret = reply.ret_code;
857
858 DBG("Relayd data is %s pending for stream id %" PRIu64,
859 ret == 1 ? "" : "NOT", stream_id);
860
861 error:
862 return ret;
863 }
864
865 /*
866 * Check on the relayd side for a quiescent state on the control socket.
867 */
868 int relayd_quiescent_control(struct lttcomm_relayd_sock *rsock,
869 uint64_t metadata_stream_id)
870 {
871 int ret;
872 struct lttcomm_relayd_quiescent_control msg;
873 struct lttcomm_relayd_generic_reply reply;
874
875 /* Code flow error. Safety net. */
876 assert(rsock);
877
878 DBG("Relayd checking quiescent control state");
879
880 memset(&msg, 0, sizeof(msg));
881 msg.stream_id = htobe64(metadata_stream_id);
882
883 /* Send command */
884 ret = send_command(rsock, RELAYD_QUIESCENT_CONTROL, &msg, sizeof(msg), 0);
885 if (ret < 0) {
886 goto error;
887 }
888
889 /* Receive response */
890 ret = recv_reply(rsock, (void *) &reply, sizeof(reply));
891 if (ret < 0) {
892 goto error;
893 }
894
895 reply.ret_code = be32toh(reply.ret_code);
896
897 /* Return session id or negative ret code. */
898 if (reply.ret_code != LTTNG_OK) {
899 ret = -1;
900 ERR("Relayd quiescent control replied error %d", reply.ret_code);
901 goto error;
902 }
903
904 /* Control socket is quiescent */
905 return 0;
906
907 error:
908 return ret;
909 }
910
911 /*
912 * Begin a data pending command for a specific session id.
913 */
914 int relayd_begin_data_pending(struct lttcomm_relayd_sock *rsock, uint64_t id)
915 {
916 int ret;
917 struct lttcomm_relayd_begin_data_pending msg;
918 struct lttcomm_relayd_generic_reply reply;
919
920 /* Code flow error. Safety net. */
921 assert(rsock);
922
923 DBG("Relayd begin data pending");
924
925 memset(&msg, 0, sizeof(msg));
926 msg.session_id = htobe64(id);
927
928 /* Send command */
929 ret = send_command(rsock, RELAYD_BEGIN_DATA_PENDING, &msg, sizeof(msg), 0);
930 if (ret < 0) {
931 goto error;
932 }
933
934 /* Receive response */
935 ret = recv_reply(rsock, (void *) &reply, sizeof(reply));
936 if (ret < 0) {
937 goto error;
938 }
939
940 reply.ret_code = be32toh(reply.ret_code);
941
942 /* Return session id or negative ret code. */
943 if (reply.ret_code != LTTNG_OK) {
944 ret = -1;
945 ERR("Relayd begin data pending replied error %d", reply.ret_code);
946 goto error;
947 }
948
949 return 0;
950
951 error:
952 return ret;
953 }
954
955 /*
956 * End a data pending command for a specific session id.
957 *
958 * Return 0 on success and set is_data_inflight to 0 if no data is being
959 * streamed or 1 if it is the case.
960 */
961 int relayd_end_data_pending(struct lttcomm_relayd_sock *rsock, uint64_t id,
962 unsigned int *is_data_inflight)
963 {
964 int ret, recv_ret;
965 struct lttcomm_relayd_end_data_pending msg;
966 struct lttcomm_relayd_generic_reply reply;
967
968 /* Code flow error. Safety net. */
969 assert(rsock);
970
971 DBG("Relayd end data pending");
972
973 memset(&msg, 0, sizeof(msg));
974 msg.session_id = htobe64(id);
975
976 /* Send command */
977 ret = send_command(rsock, RELAYD_END_DATA_PENDING, &msg, sizeof(msg), 0);
978 if (ret < 0) {
979 goto error;
980 }
981
982 /* Receive response */
983 ret = recv_reply(rsock, (void *) &reply, sizeof(reply));
984 if (ret < 0) {
985 goto error;
986 }
987
988 recv_ret = be32toh(reply.ret_code);
989 if (recv_ret < 0) {
990 ret = recv_ret;
991 goto error;
992 }
993
994 *is_data_inflight = recv_ret;
995
996 DBG("Relayd end data pending is data inflight: %d", recv_ret);
997
998 return 0;
999
1000 error:
1001 return ret;
1002 }
1003
1004 /*
1005 * Send index to the relayd.
1006 */
1007 int relayd_send_index(struct lttcomm_relayd_sock *rsock,
1008 struct ctf_packet_index *index, uint64_t relay_stream_id,
1009 uint64_t net_seq_num)
1010 {
1011 int ret;
1012 struct lttcomm_relayd_index msg;
1013 struct lttcomm_relayd_generic_reply reply;
1014
1015 /* Code flow error. Safety net. */
1016 assert(rsock);
1017
1018 if (rsock->minor < 4) {
1019 DBG("Not sending indexes before protocol 2.4");
1020 ret = 0;
1021 goto error;
1022 }
1023
1024 DBG("Relayd sending index for stream ID %" PRIu64, relay_stream_id);
1025
1026 memset(&msg, 0, sizeof(msg));
1027 msg.relay_stream_id = htobe64(relay_stream_id);
1028 msg.net_seq_num = htobe64(net_seq_num);
1029
1030 /* The index is already in big endian. */
1031 msg.packet_size = index->packet_size;
1032 msg.content_size = index->content_size;
1033 msg.timestamp_begin = index->timestamp_begin;
1034 msg.timestamp_end = index->timestamp_end;
1035 msg.events_discarded = index->events_discarded;
1036 msg.stream_id = index->stream_id;
1037
1038 if (rsock->minor >= 8) {
1039 msg.stream_instance_id = index->stream_instance_id;
1040 msg.packet_seq_num = index->packet_seq_num;
1041 }
1042
1043 /* Send command */
1044 ret = send_command(rsock, RELAYD_SEND_INDEX, &msg,
1045 lttcomm_relayd_index_len(lttng_to_index_major(rsock->major,
1046 rsock->minor),
1047 lttng_to_index_minor(rsock->major, rsock->minor)),
1048 0);
1049 if (ret < 0) {
1050 goto error;
1051 }
1052
1053 /*
1054 * Ignore the response. TCP guarantee in-order arrival and the overall
1055 * protocol do not rely on hard ordering between the control and data
1056 * socket for index.
1057 * Indexes are sent either at the end of the buffer consumption or
1058 * during the live timer.
1059 */
1060 ret = recv_reply_ignore(rsock, sizeof(reply));
1061 if (ret < 0) {
1062 goto error;
1063 }
1064
1065 error:
1066 return ret;
1067 }
1068
1069 /*
1070 * Ask the relay to reset the metadata trace file (regeneration).
1071 */
1072 int relayd_reset_metadata(struct lttcomm_relayd_sock *rsock,
1073 uint64_t stream_id, uint64_t version)
1074 {
1075 int ret;
1076 struct lttcomm_relayd_reset_metadata msg;
1077 struct lttcomm_relayd_generic_reply reply;
1078
1079 /* Code flow error. Safety net. */
1080 assert(rsock);
1081
1082 /* Should have been prevented by the sessiond. */
1083 if (rsock->minor < 8) {
1084 ERR("Metadata regeneration unsupported before 2.8");
1085 ret = -1;
1086 goto error;
1087 }
1088
1089 DBG("Relayd reset metadata stream id %" PRIu64, stream_id);
1090
1091 memset(&msg, 0, sizeof(msg));
1092 msg.stream_id = htobe64(stream_id);
1093 msg.version = htobe64(version);
1094
1095 /* Send command */
1096 ret = send_command(rsock, RELAYD_RESET_METADATA, (void *) &msg, sizeof(msg), 0);
1097 if (ret < 0) {
1098 goto error;
1099 }
1100
1101 /* Receive response */
1102 ret = recv_reply(rsock, (void *) &reply, sizeof(reply));
1103 if (ret < 0) {
1104 goto error;
1105 }
1106
1107 reply.ret_code = be32toh(reply.ret_code);
1108
1109 /* Return session id or negative ret code. */
1110 if (reply.ret_code != LTTNG_OK) {
1111 ret = -1;
1112 ERR("Relayd reset metadata replied error %d", reply.ret_code);
1113 } else {
1114 /* Success */
1115 ret = 0;
1116 }
1117
1118 DBG("Relayd reset metadata stream id %" PRIu64 " successfully", stream_id);
1119
1120 error:
1121 return ret;
1122 }
This page took 0.102104 seconds and 5 git commands to generate.