CUSTOM: relayd protocol: ignore reply on relayd_send_index and relayd_send_close_stream
[lttng-tools.git] / src / common / relayd / relayd.c
CommitLineData
00e2e675
DG
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
6c1c0768 18#define _LGPL_SOURCE
00e2e675
DG
19#include <assert.h>
20#include <stdio.h>
21#include <stdlib.h>
22#include <string.h>
23#include <sys/stat.h>
77c7c900 24#include <inttypes.h>
00e2e675
DG
25
26#include <common/common.h>
27#include <common/defaults.h>
f263b7fd 28#include <common/compat/endian.h>
00e2e675 29#include <common/sessiond-comm/relayd.h>
50adc264 30#include <common/index/ctf-index.h>
00e2e675
DG
31
32#include "relayd.h"
33
34/*
35 * Send command. Fill up the header and append the data.
36 */
6151a90f 37static int send_command(struct lttcomm_relayd_sock *rsock,
7c9534d6 38 enum lttcomm_relayd_command cmd, void *data, size_t size,
00e2e675
DG
39 int flags)
40{
41 int ret;
42 struct lttcomm_relayd_hdr header;
43 char *buf;
44 uint64_t buf_size = sizeof(header);
45
f96e4545
MD
46 if (rsock->sock.fd < 0) {
47 return -ECONNRESET;
48 }
49
00e2e675
DG
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
53efb85a 61 memset(&header, 0, sizeof(header));
00e2e675
DG
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
68d5b00b 75 DBG3("Relayd sending command %d of size %" PRIu64, (int) cmd, buf_size);
6151a90f 76 ret = rsock->sock.ops->sendmsg(&rsock->sock, buf, buf_size, flags);
00e2e675 77 if (ret < 0) {
68d5b00b
JG
78 PERROR("Failed to send command %d of size %" PRIu64,
79 (int) cmd, buf_size);
8994307f 80 ret = -errno;
00e2e675
DG
81 goto error;
82 }
00e2e675
DG
83error:
84 free(buf);
85alloc_error:
86 return ret;
87}
88
9c15ec61
JR
89static 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
132error:
133 return ret;
134}
135
00e2e675
DG
136/*
137 * Receive reply data on socket. This MUST be call after send_command or else
138 * could result in unexpected behavior(s).
139 */
6151a90f 140static int recv_reply(struct lttcomm_relayd_sock *rsock, void *data, size_t size)
00e2e675
DG
141{
142 int ret;
143
f96e4545
MD
144 if (rsock->sock.fd < 0) {
145 return -ECONNRESET;
146 }
147
9c15ec61
JR
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
8fd623e0 168 DBG3("Relayd waiting for reply of size %zu", size);
00e2e675 169
6151a90f 170 ret = rsock->sock.ops->recvmsg(&rsock->sock, data, size, 0);
20275fe8
DG
171 if (ret <= 0 || ret != size) {
172 if (ret == 0) {
173 /* Orderly shutdown. */
6151a90f 174 DBG("Socket %d has performed an orderly shutdown", rsock->sock.fd);
20275fe8 175 } else {
8fd623e0 176 DBG("Receiving reply failed on sock %d for size %zu with ret %d",
6151a90f 177 rsock->sock.fd, size, ret);
20275fe8
DG
178 }
179 /* Always return -1 here and the caller can use errno. */
180 ret = -1;
00e2e675
DG
181 goto error;
182 }
183
184error:
185 return ret;
186}
187
d3e2ba59
JD
188/*
189 * Starting at 2.4, RELAYD_CREATE_SESSION takes additional parameters to
190 * support the live reading capability.
191 */
192static int relayd_create_session_2_4(struct lttcomm_relayd_sock *rsock,
193 uint64_t *session_id, char *session_name, char *hostname,
7d2f7452 194 int session_live_timer, unsigned int snapshot)
d3e2ba59
JD
195{
196 int ret;
197 struct lttcomm_relayd_create_session_2_4 msg;
198
3a13ffd5
MD
199 if (lttng_strncpy(msg.session_name, session_name,
200 sizeof(msg.session_name))) {
246777db
MD
201 ret = -1;
202 goto error;
203 }
3a13ffd5 204 if (lttng_strncpy(msg.hostname, hostname, sizeof(msg.hostname))) {
246777db
MD
205 ret = -1;
206 goto error;
207 }
d3e2ba59 208 msg.live_timer = htobe32(session_live_timer);
7d2f7452 209 msg.snapshot = htobe32(snapshot);
d3e2ba59
JD
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
217error:
218 return ret;
219}
220
221/*
222 * RELAYD_CREATE_SESSION from 2.1 to 2.3.
223 */
224static 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
235error:
236 return ret;
237}
238
c5b6f4f0
DG
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 *
20275fe8
DG
243 * On success, return 0 else a negative value which is either an errno error or
244 * a lttng error code from the relayd.
c5b6f4f0 245 */
d3e2ba59 246int relayd_create_session(struct lttcomm_relayd_sock *rsock, uint64_t *session_id,
7d2f7452
DG
247 char *session_name, char *hostname, int session_live_timer,
248 unsigned int snapshot)
c5b6f4f0
DG
249{
250 int ret;
251 struct lttcomm_relayd_status_session reply;
252
6151a90f 253 assert(rsock);
c5b6f4f0
DG
254 assert(session_id);
255
256 DBG("Relayd create session");
257
d3e2ba59
JD
258 switch(rsock->minor) {
259 case 1:
260 case 2:
261 case 3:
262 ret = relayd_create_session_2_1(rsock, session_id);
31d74dc3 263 break;
d3e2ba59
JD
264 case 4:
265 default:
7d2f7452
DG
266 ret = relayd_create_session_2_4(rsock, session_id, session_name,
267 hostname, session_live_timer, snapshot);
31d74dc3 268 break;
d3e2ba59
JD
269 }
270
c5b6f4f0
DG
271 if (ret < 0) {
272 goto error;
273 }
274
20275fe8 275 /* Receive response */
6151a90f 276 ret = recv_reply(rsock, (void *) &reply, sizeof(reply));
c5b6f4f0
DG
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) {
bb63afd9
DG
286 ret = -1;
287 ERR("Relayd create session replied error %d", reply.ret_code);
c5b6f4f0
DG
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
296error:
297 return ret;
298}
299
00e2e675
DG
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 */
6151a90f 305int relayd_add_stream(struct lttcomm_relayd_sock *rsock, const char *channel_name,
0f907de1
JD
306 const char *pathname, uint64_t *stream_id,
307 uint64_t tracefile_size, uint64_t tracefile_count)
00e2e675
DG
308{
309 int ret;
310 struct lttcomm_relayd_add_stream msg;
0f907de1 311 struct lttcomm_relayd_add_stream_2_2 msg_2_2;
00e2e675
DG
312 struct lttcomm_relayd_status_stream reply;
313
314 /* Code flow error. Safety net. */
6151a90f 315 assert(rsock);
00e2e675
DG
316 assert(channel_name);
317 assert(pathname);
318
319 DBG("Relayd adding stream for channel name %s", channel_name);
320
0f907de1
JD
321 /* Compat with relayd 2.1 */
322 if (rsock->minor == 1) {
53efb85a 323 memset(&msg, 0, sizeof(msg));
a7c918ad
MD
324 if (lttng_strncpy(msg.channel_name, channel_name,
325 sizeof(msg.channel_name))) {
246777db
MD
326 ret = -1;
327 goto error;
328 }
a7c918ad
MD
329 if (lttng_strncpy(msg.pathname, pathname,
330 sizeof(msg.pathname))) {
246777db
MD
331 ret = -1;
332 goto error;
333 }
00e2e675 334
0f907de1
JD
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 {
53efb85a 341 memset(&msg_2_2, 0, sizeof(msg_2_2));
0f907de1 342 /* Compat with relayd 2.2+ */
a7c918ad
MD
343 if (lttng_strncpy(msg_2_2.channel_name, channel_name,
344 sizeof(msg_2_2.channel_name))) {
246777db
MD
345 ret = -1;
346 goto error;
347 }
a7c918ad
MD
348 if (lttng_strncpy(msg_2_2.pathname, pathname,
349 sizeof(msg_2_2.pathname))) {
246777db
MD
350 ret = -1;
351 goto error;
352 }
0f907de1
JD
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 }
00e2e675
DG
361 }
362
633d0084 363 /* Waiting for reply */
6151a90f 364 ret = recv_reply(rsock, (void *) &reply, sizeof(reply));
00e2e675
DG
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. */
f73fabfd 374 if (reply.ret_code != LTTNG_OK) {
bb63afd9
DG
375 ret = -1;
376 ERR("Relayd add stream replied error %d", reply.ret_code);
00e2e675
DG
377 } else {
378 /* Success */
379 ret = 0;
380 *stream_id = reply.handle;
381 }
382
77c7c900
MD
383 DBG("Relayd stream added successfully with handle %" PRIu64,
384 reply.handle);
00e2e675
DG
385
386error:
387 return ret;
388}
389
0934dd7e
JR
390/*
391 * Add stream on the relayd. Send part.
392 *
393 * On success return 0 else return ret_code negative value.
394 */
395int 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
454error:
455 return ret;
456}
457
458int 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
489error:
490 return ret;
491}
492
a4baae1b
JD
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 */
498int 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
541error:
542end:
543 return ret;
544}
545
00e2e675
DG
546/*
547 * Check version numbers on the relayd.
d4519fa3
JD
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.
00e2e675 550 *
3071c8c7
JD
551 * Return 0 if the two daemons are compatible, LTTNG_ERR_RELAYD_VERSION_FAIL
552 * otherwise, or a negative value on network errors.
00e2e675 553 */
6151a90f 554int relayd_version_check(struct lttcomm_relayd_sock *rsock)
00e2e675
DG
555{
556 int ret;
092b6259 557 struct lttcomm_relayd_version msg;
00e2e675
DG
558
559 /* Code flow error. Safety net. */
6151a90f 560 assert(rsock);
00e2e675 561
6151a90f
JD
562 DBG("Relayd version check for major.minor %u.%u", rsock->major,
563 rsock->minor);
00e2e675 564
53efb85a 565 memset(&msg, 0, sizeof(msg));
092b6259 566 /* Prepare network byte order before transmission. */
6151a90f
JD
567 msg.major = htobe32(rsock->major);
568 msg.minor = htobe32(rsock->minor);
092b6259 569
00e2e675 570 /* Send command */
6151a90f 571 ret = send_command(rsock, RELAYD_VERSION, (void *) &msg, sizeof(msg), 0);
00e2e675
DG
572 if (ret < 0) {
573 goto error;
574 }
575
20275fe8 576 /* Receive response */
6151a90f 577 ret = recv_reply(rsock, (void *) &msg, sizeof(msg));
00e2e675
DG
578 if (ret < 0) {
579 goto error;
580 }
581
582 /* Set back to host bytes order */
092b6259
DG
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.
092b6259 591 */
6151a90f 592 if (msg.major != rsock->major) {
d4519fa3 593 /* Not compatible */
3071c8c7 594 ret = LTTNG_ERR_RELAYD_VERSION_FAIL;
d4519fa3 595 DBG2("Relayd version is NOT compatible. Relayd version %u != %u (us)",
6151a90f 596 msg.major, rsock->major);
092b6259 597 goto error;
00e2e675
DG
598 }
599
092b6259 600 /*
6151a90f
JD
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.
092b6259 604 */
6151a90f
JD
605 if (rsock->minor > msg.minor) {
606 rsock->minor = msg.minor;
d4519fa3 607 }
092b6259 608
d4519fa3
JD
609 /* Version number compatible */
610 DBG2("Relayd version is compatible, using protocol version %u.%u",
6151a90f 611 rsock->major, rsock->minor);
d4519fa3 612 ret = 0;
00e2e675
DG
613
614error:
615 return ret;
616}
617
00e2e675
DG
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 */
6151a90f 623int relayd_send_metadata(struct lttcomm_relayd_sock *rsock, size_t len)
00e2e675
DG
624{
625 int ret;
626
627 /* Code flow error. Safety net. */
6151a90f 628 assert(rsock);
00e2e675 629
77c7c900 630 DBG("Relayd sending metadata of size %zu", len);
00e2e675
DG
631
632 /* Send command */
6151a90f 633 ret = send_command(rsock, RELAYD_SEND_METADATA, NULL, len, 0);
00e2e675
DG
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
633d0084 643 * header. This is why we don't wait for a reply here.
00e2e675
DG
644 */
645
646error:
647 return ret;
648}
649
650/*
6151a90f 651 * Connect to relay daemon with an allocated lttcomm_relayd_sock.
00e2e675 652 */
6151a90f 653int relayd_connect(struct lttcomm_relayd_sock *rsock)
00e2e675
DG
654{
655 /* Code flow error. Safety net. */
6151a90f 656 assert(rsock);
00e2e675 657
f96e4545
MD
658 if (!rsock->sock.ops) {
659 /*
660 * Attempting a connect on a non-initialized socket.
661 */
662 return -ECONNRESET;
663 }
664
00e2e675
DG
665 DBG3("Relayd connect ...");
666
6151a90f 667 return rsock->sock.ops->connect(&rsock->sock);
00e2e675
DG
668}
669
670/*
6151a90f 671 * Close relayd socket with an allocated lttcomm_relayd_sock.
ffe60014
DG
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).
00e2e675 682 */
6151a90f 683int relayd_close(struct lttcomm_relayd_sock *rsock)
00e2e675 684{
ffe60014
DG
685 int ret;
686
00e2e675 687 /* Code flow error. Safety net. */
6151a90f 688 assert(rsock);
00e2e675 689
ffe60014 690 /* An invalid fd is fine, return success. */
6151a90f 691 if (rsock->sock.fd < 0) {
ffe60014
DG
692 ret = 0;
693 goto end;
694 }
695
9c15ec61
JR
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
6151a90f 717 DBG3("Relayd closing socket %d", rsock->sock.fd);
00e2e675 718
6151a90f
JD
719 if (rsock->sock.ops) {
720 ret = rsock->sock.ops->close(&rsock->sock);
ffe60014
DG
721 } else {
722 /* Default call if no specific ops found. */
6151a90f 723 ret = close(rsock->sock.fd);
ffe60014
DG
724 if (ret < 0) {
725 PERROR("relayd_close default close");
726 }
727 }
f96e4545 728 rsock->sock.fd = -1;
ffe60014
DG
729
730end:
731 return ret;
00e2e675
DG
732}
733
734/*
735 * Send data header structure to the relayd.
736 */
6151a90f 737int relayd_send_data_hdr(struct lttcomm_relayd_sock *rsock,
00e2e675
DG
738 struct lttcomm_relayd_data_hdr *hdr, size_t size)
739{
740 int ret;
741
742 /* Code flow error. Safety net. */
6151a90f 743 assert(rsock);
00e2e675
DG
744 assert(hdr);
745
f96e4545
MD
746 if (rsock->sock.fd < 0) {
747 return -ECONNRESET;
748 }
749
8fd623e0 750 DBG3("Relayd sending data header of size %zu", size);
00e2e675
DG
751
752 /* Again, safety net */
753 if (size == 0) {
754 size = sizeof(struct lttcomm_relayd_data_hdr);
755 }
756
757 /* Only send data header. */
6151a90f 758 ret = rsock->sock.ops->sendmsg(&rsock->sock, hdr, size, 0);
00e2e675 759 if (ret < 0) {
8994307f 760 ret = -errno;
00e2e675
DG
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
769error:
770 return ret;
771}
173af62f
DG
772
773/*
774 * Send close stream command to the relayd.
775 */
6151a90f 776int relayd_send_close_stream(struct lttcomm_relayd_sock *rsock, uint64_t stream_id,
173af62f
DG
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. */
6151a90f 784 assert(rsock);
173af62f 785
77c7c900 786 DBG("Relayd closing stream id %" PRIu64, stream_id);
173af62f 787
53efb85a 788 memset(&msg, 0, sizeof(msg));
173af62f
DG
789 msg.stream_id = htobe64(stream_id);
790 msg.last_net_seq_num = htobe64(last_net_seq_num);
791
792 /* Send command */
6151a90f 793 ret = send_command(rsock, RELAYD_CLOSE_STREAM, (void *) &msg, sizeof(msg), 0);
173af62f
DG
794 if (ret < 0) {
795 goto error;
796 }
797
9c15ec61
JR
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));
173af62f
DG
804 if (ret < 0) {
805 goto error;
806 }
807
77c7c900 808 DBG("Relayd close stream id %" PRIu64 " successfully", stream_id);
173af62f
DG
809
810error:
811 return ret;
812}
c8f59ee5
DG
813
814/*
815 * Check for data availability for a given stream id.
816 *
6d805429 817 * Return 0 if NOT pending, 1 if so and a negative value on error.
c8f59ee5 818 */
6151a90f 819int relayd_data_pending(struct lttcomm_relayd_sock *rsock, uint64_t stream_id,
c8f59ee5
DG
820 uint64_t last_net_seq_num)
821{
822 int ret;
6d805429 823 struct lttcomm_relayd_data_pending msg;
c8f59ee5
DG
824 struct lttcomm_relayd_generic_reply reply;
825
826 /* Code flow error. Safety net. */
6151a90f 827 assert(rsock);
c8f59ee5 828
6d805429 829 DBG("Relayd data pending for stream id %" PRIu64, stream_id);
c8f59ee5 830
53efb85a 831 memset(&msg, 0, sizeof(msg));
c8f59ee5
DG
832 msg.stream_id = htobe64(stream_id);
833 msg.last_net_seq_num = htobe64(last_net_seq_num);
834
835 /* Send command */
6151a90f 836 ret = send_command(rsock, RELAYD_DATA_PENDING, (void *) &msg,
c8f59ee5
DG
837 sizeof(msg), 0);
838 if (ret < 0) {
839 goto error;
840 }
841
20275fe8 842 /* Receive response */
6151a90f 843 ret = recv_reply(rsock, (void *) &reply, sizeof(reply));
c8f59ee5
DG
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) {
bb63afd9 852 ERR("Relayd data pending replied error %d", reply.ret_code);
c8f59ee5
DG
853 }
854
855 /* At this point, the ret code is either 1 or 0 */
856 ret = reply.ret_code;
857
6d805429 858 DBG("Relayd data is %s pending for stream id %" PRIu64,
9dd26bb9 859 ret == 1 ? "" : "NOT", stream_id);
c8f59ee5
DG
860
861error:
862 return ret;
863}
864
865/*
866 * Check on the relayd side for a quiescent state on the control socket.
867 */
6151a90f 868int relayd_quiescent_control(struct lttcomm_relayd_sock *rsock,
ad7051c0 869 uint64_t metadata_stream_id)
c8f59ee5
DG
870{
871 int ret;
ad7051c0 872 struct lttcomm_relayd_quiescent_control msg;
c8f59ee5
DG
873 struct lttcomm_relayd_generic_reply reply;
874
875 /* Code flow error. Safety net. */
6151a90f 876 assert(rsock);
c8f59ee5
DG
877
878 DBG("Relayd checking quiescent control state");
879
53efb85a 880 memset(&msg, 0, sizeof(msg));
ad7051c0
DG
881 msg.stream_id = htobe64(metadata_stream_id);
882
c8f59ee5 883 /* Send command */
6151a90f 884 ret = send_command(rsock, RELAYD_QUIESCENT_CONTROL, &msg, sizeof(msg), 0);
c8f59ee5
DG
885 if (ret < 0) {
886 goto error;
887 }
888
20275fe8 889 /* Receive response */
6151a90f 890 ret = recv_reply(rsock, (void *) &reply, sizeof(reply));
c8f59ee5
DG
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) {
bb63afd9
DG
899 ret = -1;
900 ERR("Relayd quiescent control replied error %d", reply.ret_code);
c8f59ee5
DG
901 goto error;
902 }
903
904 /* Control socket is quiescent */
6d805429 905 return 0;
c8f59ee5
DG
906
907error:
908 return ret;
909}
f7079f67
DG
910
911/*
912 * Begin a data pending command for a specific session id.
913 */
6151a90f 914int relayd_begin_data_pending(struct lttcomm_relayd_sock *rsock, uint64_t id)
f7079f67
DG
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. */
6151a90f 921 assert(rsock);
f7079f67
DG
922
923 DBG("Relayd begin data pending");
924
53efb85a 925 memset(&msg, 0, sizeof(msg));
f7079f67
DG
926 msg.session_id = htobe64(id);
927
928 /* Send command */
6151a90f 929 ret = send_command(rsock, RELAYD_BEGIN_DATA_PENDING, &msg, sizeof(msg), 0);
f7079f67
DG
930 if (ret < 0) {
931 goto error;
932 }
933
20275fe8 934 /* Receive response */
6151a90f 935 ret = recv_reply(rsock, (void *) &reply, sizeof(reply));
f7079f67
DG
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) {
bb63afd9
DG
944 ret = -1;
945 ERR("Relayd begin data pending replied error %d", reply.ret_code);
f7079f67
DG
946 goto error;
947 }
948
949 return 0;
950
951error:
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 */
6151a90f 961int relayd_end_data_pending(struct lttcomm_relayd_sock *rsock, uint64_t id,
f7079f67
DG
962 unsigned int *is_data_inflight)
963{
af6c30b5 964 int ret, recv_ret;
f7079f67
DG
965 struct lttcomm_relayd_end_data_pending msg;
966 struct lttcomm_relayd_generic_reply reply;
967
968 /* Code flow error. Safety net. */
6151a90f 969 assert(rsock);
f7079f67
DG
970
971 DBG("Relayd end data pending");
972
53efb85a 973 memset(&msg, 0, sizeof(msg));
f7079f67
DG
974 msg.session_id = htobe64(id);
975
976 /* Send command */
6151a90f 977 ret = send_command(rsock, RELAYD_END_DATA_PENDING, &msg, sizeof(msg), 0);
f7079f67
DG
978 if (ret < 0) {
979 goto error;
980 }
981
20275fe8 982 /* Receive response */
6151a90f 983 ret = recv_reply(rsock, (void *) &reply, sizeof(reply));
f7079f67
DG
984 if (ret < 0) {
985 goto error;
986 }
987
af6c30b5
DG
988 recv_ret = be32toh(reply.ret_code);
989 if (recv_ret < 0) {
990 ret = recv_ret;
f7079f67
DG
991 goto error;
992 }
993
af6c30b5 994 *is_data_inflight = recv_ret;
f7079f67 995
af6c30b5 996 DBG("Relayd end data pending is data inflight: %d", recv_ret);
f7079f67
DG
997
998 return 0;
999
1000error:
1001 return ret;
1002}
1c20f0e2
JD
1003
1004/*
1005 * Send index to the relayd.
1006 */
1007int relayd_send_index(struct lttcomm_relayd_sock *rsock,
50adc264 1008 struct ctf_packet_index *index, uint64_t relay_stream_id,
1c20f0e2
JD
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
53efb85a 1026 memset(&msg, 0, sizeof(msg));
1c20f0e2
JD
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
234cd636
JD
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
1c20f0e2 1043 /* Send command */
e0547b83
MD
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);
1c20f0e2
JD
1049 if (ret < 0) {
1050 goto error;
1051 }
1052
9c15ec61
JR
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));
1c20f0e2
JD
1061 if (ret < 0) {
1062 goto error;
1063 }
1064
1c20f0e2
JD
1065error:
1066 return ret;
1067}
93ec662e
JD
1068
1069/*
1070 * Ask the relay to reset the metadata trace file (regeneration).
1071 */
1072int 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
1120error:
1121 return ret;
1122}
This page took 0.110696 seconds and 5 git commands to generate.