Pass the consumerd stream's trace archive id to the relayd
[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,
76b9afaa 38 enum lttcomm_relayd_command cmd, const 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
06586bbe 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) {
06586bbe
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
89/*
90 * Receive reply data on socket. This MUST be call after send_command or else
91 * could result in unexpected behavior(s).
92 */
6151a90f 93static int recv_reply(struct lttcomm_relayd_sock *rsock, void *data, size_t size)
00e2e675
DG
94{
95 int ret;
96
f96e4545
MD
97 if (rsock->sock.fd < 0) {
98 return -ECONNRESET;
99 }
100
8fd623e0 101 DBG3("Relayd waiting for reply of size %zu", size);
00e2e675 102
6151a90f 103 ret = rsock->sock.ops->recvmsg(&rsock->sock, data, size, 0);
20275fe8
DG
104 if (ret <= 0 || ret != size) {
105 if (ret == 0) {
106 /* Orderly shutdown. */
6151a90f 107 DBG("Socket %d has performed an orderly shutdown", rsock->sock.fd);
20275fe8 108 } else {
8fd623e0 109 DBG("Receiving reply failed on sock %d for size %zu with ret %d",
6151a90f 110 rsock->sock.fd, size, ret);
20275fe8
DG
111 }
112 /* Always return -1 here and the caller can use errno. */
113 ret = -1;
00e2e675
DG
114 goto error;
115 }
116
117error:
118 return ret;
119}
120
d3e2ba59 121/*
f86f6389
JR
122 * Starting from 2.11, RELAYD_CREATE_SESSION payload (session_name & hostname)
123 * have no length restriction on the sender side.
124 * Length for both payloads is stored in the msg struct. A new dynamic size
125 * payload size is introduced.
126 */
127static int relayd_create_session_2_11(struct lttcomm_relayd_sock *rsock,
128 char *session_name, char *hostname,
129 int session_live_timer, unsigned int snapshot)
130{
131 int ret;
132 struct lttcomm_relayd_create_session_2_11 *msg = NULL;
133 size_t session_name_len;
134 size_t hostname_len;
135 size_t msg_length;
136
137 /* The two names are sent with a '\0' delimiter between them. */
138 session_name_len = strlen(session_name) + 1;
139 hostname_len = strlen(hostname) + 1;
140
141 msg_length = sizeof(*msg) + session_name_len + hostname_len;
142 msg = zmalloc(msg_length);
143 if (!msg) {
144 PERROR("zmalloc create_session_2_11 command message");
145 ret = -1;
146 goto error;
147 }
148
149 assert(session_name_len <= UINT32_MAX);
150 msg->session_name_len = htobe32(session_name_len);
151
152 assert(hostname_len <= UINT32_MAX);
153 msg->hostname_len = htobe32(hostname_len);
154
155 if (lttng_strncpy(msg->names, session_name, session_name_len)) {
156 ret = -1;
157 goto error;
158 }
159 if (lttng_strncpy(msg->names + session_name_len, hostname, hostname_len)) {
160 ret = -1;
161 goto error;
162 }
163
164 msg->live_timer = htobe32(session_live_timer);
165 msg->snapshot = !!snapshot;
166
167 /* Send command */
168 ret = send_command(rsock, RELAYD_CREATE_SESSION, msg, msg_length, 0);
169 if (ret < 0) {
170 goto error;
171 }
172error:
173 free(msg);
174 return ret;
175}
176/*
177 * From 2.4 to 2.10, RELAYD_CREATE_SESSION takes additional parameters to
d3e2ba59
JD
178 * support the live reading capability.
179 */
180static int relayd_create_session_2_4(struct lttcomm_relayd_sock *rsock,
42e9a27b
JR
181 char *session_name, char *hostname, int session_live_timer,
182 unsigned int snapshot)
d3e2ba59
JD
183{
184 int ret;
185 struct lttcomm_relayd_create_session_2_4 msg;
186
3a13ffd5
MD
187 if (lttng_strncpy(msg.session_name, session_name,
188 sizeof(msg.session_name))) {
246777db
MD
189 ret = -1;
190 goto error;
191 }
3a13ffd5 192 if (lttng_strncpy(msg.hostname, hostname, sizeof(msg.hostname))) {
246777db
MD
193 ret = -1;
194 goto error;
195 }
d3e2ba59 196 msg.live_timer = htobe32(session_live_timer);
7d2f7452 197 msg.snapshot = htobe32(snapshot);
d3e2ba59
JD
198
199 /* Send command */
200 ret = send_command(rsock, RELAYD_CREATE_SESSION, &msg, sizeof(msg), 0);
201 if (ret < 0) {
202 goto error;
203 }
204
205error:
206 return ret;
207}
208
209/*
210 * RELAYD_CREATE_SESSION from 2.1 to 2.3.
211 */
42e9a27b 212static int relayd_create_session_2_1(struct lttcomm_relayd_sock *rsock)
d3e2ba59
JD
213{
214 int ret;
215
216 /* Send command */
217 ret = send_command(rsock, RELAYD_CREATE_SESSION, NULL, 0, 0);
218 if (ret < 0) {
219 goto error;
220 }
221
222error:
223 return ret;
224}
225
c5b6f4f0
DG
226/*
227 * Send a RELAYD_CREATE_SESSION command to the relayd with the given socket and
228 * set session_id of the relayd if we have a successful reply from the relayd.
229 *
20275fe8
DG
230 * On success, return 0 else a negative value which is either an errno error or
231 * a lttng error code from the relayd.
c5b6f4f0 232 */
d3e2ba59 233int relayd_create_session(struct lttcomm_relayd_sock *rsock, uint64_t *session_id,
7d2f7452
DG
234 char *session_name, char *hostname, int session_live_timer,
235 unsigned int snapshot)
c5b6f4f0
DG
236{
237 int ret;
238 struct lttcomm_relayd_status_session reply;
239
6151a90f 240 assert(rsock);
c5b6f4f0
DG
241 assert(session_id);
242
243 DBG("Relayd create session");
244
f86f6389
JR
245 if (rsock->minor < 4) {
246 /* From 2.1 to 2.3 */
247 ret = relayd_create_session_2_1(rsock);
248 } else if (rsock->minor >= 4 && rsock->minor < 11) {
249 /* From 2.4 to 2.10 */
250 ret = relayd_create_session_2_4(rsock, session_name,
251 hostname, session_live_timer, snapshot);
252 } else {
253 /* From 2.11 to ... */
254 ret = relayd_create_session_2_11(rsock, session_name,
255 hostname, session_live_timer, snapshot);
d3e2ba59
JD
256 }
257
c5b6f4f0
DG
258 if (ret < 0) {
259 goto error;
260 }
261
20275fe8 262 /* Receive response */
6151a90f 263 ret = recv_reply(rsock, (void *) &reply, sizeof(reply));
c5b6f4f0
DG
264 if (ret < 0) {
265 goto error;
266 }
267
268 reply.session_id = be64toh(reply.session_id);
269 reply.ret_code = be32toh(reply.ret_code);
270
271 /* Return session id or negative ret code. */
272 if (reply.ret_code != LTTNG_OK) {
bb63afd9
DG
273 ret = -1;
274 ERR("Relayd create session replied error %d", reply.ret_code);
c5b6f4f0
DG
275 goto error;
276 } else {
277 ret = 0;
278 *session_id = reply.session_id;
279 }
280
281 DBG("Relayd session created with id %" PRIu64, reply.session_id);
282
283error:
284 return ret;
285}
286
2f21a469
JR
287static int relayd_add_stream_2_1(struct lttcomm_relayd_sock *rsock,
288 const char *channel_name, const char *pathname)
289{
290 int ret;
291 struct lttcomm_relayd_add_stream msg;
292
293 memset(&msg, 0, sizeof(msg));
294 if (lttng_strncpy(msg.channel_name, channel_name,
295 sizeof(msg.channel_name))) {
296 ret = -1;
297 goto error;
298 }
299
300 if (lttng_strncpy(msg.pathname, pathname,
301 sizeof(msg.pathname))) {
302 ret = -1;
303 goto error;
304 }
305
306 /* Send command */
307 ret = send_command(rsock, RELAYD_ADD_STREAM, (void *) &msg, sizeof(msg), 0);
308 if (ret < 0) {
309 ret = -1;
310 goto error;
311 }
312 ret = 0;
313error:
314 return ret;
315}
316
317static int relayd_add_stream_2_2(struct lttcomm_relayd_sock *rsock,
318 const char *channel_name, const char *pathname,
319 uint64_t tracefile_size, uint64_t tracefile_count)
320{
321 int ret;
322 struct lttcomm_relayd_add_stream_2_2 msg;
323
324 memset(&msg, 0, sizeof(msg));
325 /* Compat with relayd 2.2 to 2.10 */
326 if (lttng_strncpy(msg.channel_name, channel_name,
327 sizeof(msg.channel_name))) {
328 ret = -1;
329 goto error;
330 }
331 if (lttng_strncpy(msg.pathname, pathname,
332 sizeof(msg.pathname))) {
333 ret = -1;
334 goto error;
335 }
336 msg.tracefile_size = htobe64(tracefile_size);
337 msg.tracefile_count = htobe64(tracefile_count);
338
339 /* Send command */
340 ret = send_command(rsock, RELAYD_ADD_STREAM, (void *) &msg, sizeof(msg), 0);
341 if (ret < 0) {
342 goto error;
343 }
344 ret = 0;
345error:
346 return ret;
347}
348
349static int relayd_add_stream_2_11(struct lttcomm_relayd_sock *rsock,
350 const char *channel_name, const char *pathname,
351 uint64_t tracefile_size, uint64_t tracefile_count)
352{
353 int ret;
354 struct lttcomm_relayd_add_stream_2_11 *msg = NULL;
355 size_t channel_name_len;
356 size_t pathname_len;
357 size_t msg_length;
358
359 /* The two names are sent with a '\0' delimiter between them. */
360 channel_name_len = strlen(channel_name) + 1;
361 pathname_len = strlen(pathname) + 1;
362
363 msg_length = sizeof(*msg) + channel_name_len + pathname_len;
364 msg = zmalloc(msg_length);
365 if (!msg) {
366 PERROR("zmalloc add_stream_2_11 command message");
367 ret = -1;
368 goto error;
369 }
370
371 assert(channel_name_len <= UINT32_MAX);
372 msg->channel_name_len = htobe32(channel_name_len);
373
374 assert(pathname_len <= UINT32_MAX);
375 msg->pathname_len = htobe32(pathname_len);
376
377 if (lttng_strncpy(msg->names, channel_name, channel_name_len)) {
378 ret = -1;
379 goto error;
380 }
381 if (lttng_strncpy(msg->names + channel_name_len, pathname, pathname_len)) {
382 ret = -1;
383 goto error;
384 }
385
386 msg->tracefile_size = htobe64(tracefile_size);
387 msg->tracefile_count = htobe64(tracefile_count);
388
389 /* Send command */
390 ret = send_command(rsock, RELAYD_ADD_STREAM, (void *) msg, msg_length, 0);
391 if (ret < 0) {
392 goto error;
393 }
394 ret = 0;
395error:
396 free(msg);
397 return ret;
398}
399
00e2e675
DG
400/*
401 * Add stream on the relayd and assign stream handle to the stream_id argument.
402 *
403 * On success return 0 else return ret_code negative value.
404 */
6151a90f 405int relayd_add_stream(struct lttcomm_relayd_sock *rsock, const char *channel_name,
0f907de1 406 const char *pathname, uint64_t *stream_id,
0b50e4b3
JG
407 uint64_t tracefile_size, uint64_t tracefile_count,
408 uint64_t trace_archive_id)
00e2e675
DG
409{
410 int ret;
00e2e675
DG
411 struct lttcomm_relayd_status_stream reply;
412
413 /* Code flow error. Safety net. */
6151a90f 414 assert(rsock);
00e2e675
DG
415 assert(channel_name);
416 assert(pathname);
417
418 DBG("Relayd adding stream for channel name %s", channel_name);
419
0f907de1
JD
420 /* Compat with relayd 2.1 */
421 if (rsock->minor == 1) {
2f21a469
JR
422 /* For 2.1 */
423 ret = relayd_add_stream_2_1(rsock, channel_name, pathname);
424
425 } else if (rsock->minor > 1 && rsock->minor < 11) {
426 /* From 2.2 to 2.10 */
427 ret = relayd_add_stream_2_2(rsock, channel_name, pathname,
428 tracefile_size, tracefile_count);
0f907de1 429 } else {
2f21a469
JR
430 /* From 2.11 to ...*/
431 ret = relayd_add_stream_2_11(rsock, channel_name, pathname,
432 tracefile_size, tracefile_count);
433 }
0f907de1 434
2f21a469
JR
435 if (ret) {
436 ret = -1;
437 goto error;
00e2e675
DG
438 }
439
633d0084 440 /* Waiting for reply */
6151a90f 441 ret = recv_reply(rsock, (void *) &reply, sizeof(reply));
00e2e675
DG
442 if (ret < 0) {
443 goto error;
444 }
445
446 /* Back to host bytes order. */
447 reply.handle = be64toh(reply.handle);
448 reply.ret_code = be32toh(reply.ret_code);
449
450 /* Return session id or negative ret code. */
f73fabfd 451 if (reply.ret_code != LTTNG_OK) {
bb63afd9
DG
452 ret = -1;
453 ERR("Relayd add stream replied error %d", reply.ret_code);
00e2e675
DG
454 } else {
455 /* Success */
456 ret = 0;
457 *stream_id = reply.handle;
458 }
459
77c7c900 460 DBG("Relayd stream added successfully with handle %" PRIu64,
2f21a469 461 reply.handle);
00e2e675
DG
462
463error:
464 return ret;
465}
466
a4baae1b
JD
467/*
468 * Inform the relay that all the streams for the current channel has been sent.
469 *
470 * On success return 0 else return ret_code negative value.
471 */
472int relayd_streams_sent(struct lttcomm_relayd_sock *rsock)
473{
474 int ret;
475 struct lttcomm_relayd_generic_reply reply;
476
477 /* Code flow error. Safety net. */
478 assert(rsock);
479
480 DBG("Relayd sending streams sent.");
481
482 /* This feature was introduced in 2.4, ignore it for earlier versions. */
483 if (rsock->minor < 4) {
484 ret = 0;
485 goto end;
486 }
487
488 /* Send command */
489 ret = send_command(rsock, RELAYD_STREAMS_SENT, NULL, 0, 0);
490 if (ret < 0) {
491 goto error;
492 }
493
494 /* Waiting for reply */
495 ret = recv_reply(rsock, (void *) &reply, sizeof(reply));
496 if (ret < 0) {
497 goto error;
498 }
499
500 /* Back to host bytes order. */
501 reply.ret_code = be32toh(reply.ret_code);
502
503 /* Return session id or negative ret code. */
504 if (reply.ret_code != LTTNG_OK) {
505 ret = -1;
506 ERR("Relayd streams sent replied error %d", reply.ret_code);
507 goto error;
508 } else {
509 /* Success */
510 ret = 0;
511 }
512
513 DBG("Relayd streams sent success");
514
515error:
516end:
517 return ret;
518}
519
00e2e675
DG
520/*
521 * Check version numbers on the relayd.
d4519fa3
JD
522 * If major versions are compatible, we assign minor_to_use to the
523 * minor version of the procotol we are going to use for this session.
00e2e675 524 *
67d5aa28
JD
525 * Return 0 if the two daemons are compatible, LTTNG_ERR_RELAYD_VERSION_FAIL
526 * otherwise, or a negative value on network errors.
00e2e675 527 */
6151a90f 528int relayd_version_check(struct lttcomm_relayd_sock *rsock)
00e2e675
DG
529{
530 int ret;
092b6259 531 struct lttcomm_relayd_version msg;
00e2e675
DG
532
533 /* Code flow error. Safety net. */
6151a90f 534 assert(rsock);
00e2e675 535
6151a90f
JD
536 DBG("Relayd version check for major.minor %u.%u", rsock->major,
537 rsock->minor);
00e2e675 538
53efb85a 539 memset(&msg, 0, sizeof(msg));
092b6259 540 /* Prepare network byte order before transmission. */
6151a90f
JD
541 msg.major = htobe32(rsock->major);
542 msg.minor = htobe32(rsock->minor);
092b6259 543
00e2e675 544 /* Send command */
6151a90f 545 ret = send_command(rsock, RELAYD_VERSION, (void *) &msg, sizeof(msg), 0);
00e2e675
DG
546 if (ret < 0) {
547 goto error;
548 }
549
20275fe8 550 /* Receive response */
6151a90f 551 ret = recv_reply(rsock, (void *) &msg, sizeof(msg));
00e2e675
DG
552 if (ret < 0) {
553 goto error;
554 }
555
556 /* Set back to host bytes order */
092b6259
DG
557 msg.major = be32toh(msg.major);
558 msg.minor = be32toh(msg.minor);
559
560 /*
561 * Only validate the major version. If the other side is higher,
562 * communication is not possible. Only major version equal can talk to each
563 * other. If the minor version differs, the lowest version is used by both
564 * sides.
092b6259 565 */
6151a90f 566 if (msg.major != rsock->major) {
d4519fa3 567 /* Not compatible */
67d5aa28 568 ret = LTTNG_ERR_RELAYD_VERSION_FAIL;
d4519fa3 569 DBG2("Relayd version is NOT compatible. Relayd version %u != %u (us)",
6151a90f 570 msg.major, rsock->major);
092b6259 571 goto error;
00e2e675
DG
572 }
573
092b6259 574 /*
6151a90f
JD
575 * If the relayd's minor version is higher, it will adapt to our version so
576 * we can continue to use the latest relayd communication data structure.
577 * If the received minor version is higher, the relayd should adapt to us.
092b6259 578 */
6151a90f
JD
579 if (rsock->minor > msg.minor) {
580 rsock->minor = msg.minor;
d4519fa3 581 }
092b6259 582
d4519fa3
JD
583 /* Version number compatible */
584 DBG2("Relayd version is compatible, using protocol version %u.%u",
6151a90f 585 rsock->major, rsock->minor);
d4519fa3 586 ret = 0;
00e2e675
DG
587
588error:
589 return ret;
590}
591
00e2e675
DG
592/*
593 * Add stream on the relayd and assign stream handle to the stream_id argument.
594 *
595 * On success return 0 else return ret_code negative value.
596 */
6151a90f 597int relayd_send_metadata(struct lttcomm_relayd_sock *rsock, size_t len)
00e2e675
DG
598{
599 int ret;
600
601 /* Code flow error. Safety net. */
6151a90f 602 assert(rsock);
00e2e675 603
77c7c900 604 DBG("Relayd sending metadata of size %zu", len);
00e2e675
DG
605
606 /* Send command */
6151a90f 607 ret = send_command(rsock, RELAYD_SEND_METADATA, NULL, len, 0);
00e2e675
DG
608 if (ret < 0) {
609 goto error;
610 }
611
612 DBG2("Relayd metadata added successfully");
613
614 /*
615 * After that call, the metadata data MUST be sent to the relayd so the
616 * receive size on the other end matches the len of the metadata packet
633d0084 617 * header. This is why we don't wait for a reply here.
00e2e675
DG
618 */
619
620error:
621 return ret;
622}
623
624/*
6151a90f 625 * Connect to relay daemon with an allocated lttcomm_relayd_sock.
00e2e675 626 */
6151a90f 627int relayd_connect(struct lttcomm_relayd_sock *rsock)
00e2e675
DG
628{
629 /* Code flow error. Safety net. */
6151a90f 630 assert(rsock);
00e2e675 631
f96e4545
MD
632 if (!rsock->sock.ops) {
633 /*
634 * Attempting a connect on a non-initialized socket.
635 */
636 return -ECONNRESET;
637 }
638
00e2e675
DG
639 DBG3("Relayd connect ...");
640
6151a90f 641 return rsock->sock.ops->connect(&rsock->sock);
00e2e675
DG
642}
643
644/*
6151a90f 645 * Close relayd socket with an allocated lttcomm_relayd_sock.
ffe60014
DG
646 *
647 * If no socket operations are found, simply return 0 meaning that everything
648 * is fine. Without operations, the socket can not possibly be opened or used.
649 * This is possible if the socket was allocated but not created. However, the
650 * caller could simply use it to store a valid file descriptor for instance
651 * passed over a Unix socket and call this to cleanup but still without a valid
652 * ops pointer.
653 *
654 * Return the close returned value. On error, a negative value is usually
655 * returned back from close(2).
00e2e675 656 */
6151a90f 657int relayd_close(struct lttcomm_relayd_sock *rsock)
00e2e675 658{
ffe60014
DG
659 int ret;
660
00e2e675 661 /* Code flow error. Safety net. */
6151a90f 662 assert(rsock);
00e2e675 663
ffe60014 664 /* An invalid fd is fine, return success. */
6151a90f 665 if (rsock->sock.fd < 0) {
ffe60014
DG
666 ret = 0;
667 goto end;
668 }
669
6151a90f 670 DBG3("Relayd closing socket %d", rsock->sock.fd);
00e2e675 671
6151a90f
JD
672 if (rsock->sock.ops) {
673 ret = rsock->sock.ops->close(&rsock->sock);
ffe60014
DG
674 } else {
675 /* Default call if no specific ops found. */
6151a90f 676 ret = close(rsock->sock.fd);
ffe60014
DG
677 if (ret < 0) {
678 PERROR("relayd_close default close");
679 }
680 }
f96e4545 681 rsock->sock.fd = -1;
ffe60014
DG
682
683end:
684 return ret;
00e2e675
DG
685}
686
687/*
688 * Send data header structure to the relayd.
689 */
6151a90f 690int relayd_send_data_hdr(struct lttcomm_relayd_sock *rsock,
00e2e675
DG
691 struct lttcomm_relayd_data_hdr *hdr, size_t size)
692{
693 int ret;
694
695 /* Code flow error. Safety net. */
6151a90f 696 assert(rsock);
00e2e675
DG
697 assert(hdr);
698
f96e4545
MD
699 if (rsock->sock.fd < 0) {
700 return -ECONNRESET;
701 }
702
8fd623e0 703 DBG3("Relayd sending data header of size %zu", size);
00e2e675
DG
704
705 /* Again, safety net */
706 if (size == 0) {
707 size = sizeof(struct lttcomm_relayd_data_hdr);
708 }
709
710 /* Only send data header. */
6151a90f 711 ret = rsock->sock.ops->sendmsg(&rsock->sock, hdr, size, 0);
00e2e675 712 if (ret < 0) {
8994307f 713 ret = -errno;
00e2e675
DG
714 goto error;
715 }
716
717 /*
718 * The data MUST be sent right after that command for the receive on the
719 * other end to match the size in the header.
720 */
721
722error:
723 return ret;
724}
173af62f
DG
725
726/*
727 * Send close stream command to the relayd.
728 */
6151a90f 729int relayd_send_close_stream(struct lttcomm_relayd_sock *rsock, uint64_t stream_id,
173af62f
DG
730 uint64_t last_net_seq_num)
731{
732 int ret;
733 struct lttcomm_relayd_close_stream msg;
734 struct lttcomm_relayd_generic_reply reply;
735
736 /* Code flow error. Safety net. */
6151a90f 737 assert(rsock);
173af62f 738
77c7c900 739 DBG("Relayd closing stream id %" PRIu64, stream_id);
173af62f 740
53efb85a 741 memset(&msg, 0, sizeof(msg));
173af62f
DG
742 msg.stream_id = htobe64(stream_id);
743 msg.last_net_seq_num = htobe64(last_net_seq_num);
744
745 /* Send command */
6151a90f 746 ret = send_command(rsock, RELAYD_CLOSE_STREAM, (void *) &msg, sizeof(msg), 0);
173af62f
DG
747 if (ret < 0) {
748 goto error;
749 }
750
20275fe8 751 /* Receive response */
6151a90f 752 ret = recv_reply(rsock, (void *) &reply, sizeof(reply));
173af62f
DG
753 if (ret < 0) {
754 goto error;
755 }
756
757 reply.ret_code = be32toh(reply.ret_code);
758
759 /* Return session id or negative ret code. */
f73fabfd 760 if (reply.ret_code != LTTNG_OK) {
bb63afd9
DG
761 ret = -1;
762 ERR("Relayd close stream replied error %d", reply.ret_code);
173af62f
DG
763 } else {
764 /* Success */
765 ret = 0;
766 }
767
77c7c900 768 DBG("Relayd close stream id %" PRIu64 " successfully", stream_id);
173af62f
DG
769
770error:
771 return ret;
772}
c8f59ee5
DG
773
774/*
775 * Check for data availability for a given stream id.
776 *
6d805429 777 * Return 0 if NOT pending, 1 if so and a negative value on error.
c8f59ee5 778 */
6151a90f 779int relayd_data_pending(struct lttcomm_relayd_sock *rsock, uint64_t stream_id,
c8f59ee5
DG
780 uint64_t last_net_seq_num)
781{
782 int ret;
6d805429 783 struct lttcomm_relayd_data_pending msg;
c8f59ee5
DG
784 struct lttcomm_relayd_generic_reply reply;
785
786 /* Code flow error. Safety net. */
6151a90f 787 assert(rsock);
c8f59ee5 788
6d805429 789 DBG("Relayd data pending for stream id %" PRIu64, stream_id);
c8f59ee5 790
53efb85a 791 memset(&msg, 0, sizeof(msg));
c8f59ee5
DG
792 msg.stream_id = htobe64(stream_id);
793 msg.last_net_seq_num = htobe64(last_net_seq_num);
794
795 /* Send command */
6151a90f 796 ret = send_command(rsock, RELAYD_DATA_PENDING, (void *) &msg,
c8f59ee5
DG
797 sizeof(msg), 0);
798 if (ret < 0) {
799 goto error;
800 }
801
20275fe8 802 /* Receive response */
6151a90f 803 ret = recv_reply(rsock, (void *) &reply, sizeof(reply));
c8f59ee5
DG
804 if (ret < 0) {
805 goto error;
806 }
807
808 reply.ret_code = be32toh(reply.ret_code);
809
810 /* Return session id or negative ret code. */
811 if (reply.ret_code >= LTTNG_OK) {
bb63afd9 812 ERR("Relayd data pending replied error %d", reply.ret_code);
c8f59ee5
DG
813 }
814
815 /* At this point, the ret code is either 1 or 0 */
816 ret = reply.ret_code;
817
6d805429 818 DBG("Relayd data is %s pending for stream id %" PRIu64,
9dd26bb9 819 ret == 1 ? "" : "NOT", stream_id);
c8f59ee5
DG
820
821error:
822 return ret;
823}
824
825/*
826 * Check on the relayd side for a quiescent state on the control socket.
827 */
6151a90f 828int relayd_quiescent_control(struct lttcomm_relayd_sock *rsock,
ad7051c0 829 uint64_t metadata_stream_id)
c8f59ee5
DG
830{
831 int ret;
ad7051c0 832 struct lttcomm_relayd_quiescent_control msg;
c8f59ee5
DG
833 struct lttcomm_relayd_generic_reply reply;
834
835 /* Code flow error. Safety net. */
6151a90f 836 assert(rsock);
c8f59ee5
DG
837
838 DBG("Relayd checking quiescent control state");
839
53efb85a 840 memset(&msg, 0, sizeof(msg));
ad7051c0
DG
841 msg.stream_id = htobe64(metadata_stream_id);
842
c8f59ee5 843 /* Send command */
6151a90f 844 ret = send_command(rsock, RELAYD_QUIESCENT_CONTROL, &msg, sizeof(msg), 0);
c8f59ee5
DG
845 if (ret < 0) {
846 goto error;
847 }
848
20275fe8 849 /* Receive response */
6151a90f 850 ret = recv_reply(rsock, (void *) &reply, sizeof(reply));
c8f59ee5
DG
851 if (ret < 0) {
852 goto error;
853 }
854
855 reply.ret_code = be32toh(reply.ret_code);
856
857 /* Return session id or negative ret code. */
858 if (reply.ret_code != LTTNG_OK) {
bb63afd9
DG
859 ret = -1;
860 ERR("Relayd quiescent control replied error %d", reply.ret_code);
c8f59ee5
DG
861 goto error;
862 }
863
864 /* Control socket is quiescent */
6d805429 865 return 0;
c8f59ee5
DG
866
867error:
868 return ret;
869}
f7079f67
DG
870
871/*
872 * Begin a data pending command for a specific session id.
873 */
6151a90f 874int relayd_begin_data_pending(struct lttcomm_relayd_sock *rsock, uint64_t id)
f7079f67
DG
875{
876 int ret;
877 struct lttcomm_relayd_begin_data_pending msg;
878 struct lttcomm_relayd_generic_reply reply;
879
880 /* Code flow error. Safety net. */
6151a90f 881 assert(rsock);
f7079f67
DG
882
883 DBG("Relayd begin data pending");
884
53efb85a 885 memset(&msg, 0, sizeof(msg));
f7079f67
DG
886 msg.session_id = htobe64(id);
887
888 /* Send command */
6151a90f 889 ret = send_command(rsock, RELAYD_BEGIN_DATA_PENDING, &msg, sizeof(msg), 0);
f7079f67
DG
890 if (ret < 0) {
891 goto error;
892 }
893
20275fe8 894 /* Receive response */
6151a90f 895 ret = recv_reply(rsock, (void *) &reply, sizeof(reply));
f7079f67
DG
896 if (ret < 0) {
897 goto error;
898 }
899
900 reply.ret_code = be32toh(reply.ret_code);
901
902 /* Return session id or negative ret code. */
903 if (reply.ret_code != LTTNG_OK) {
bb63afd9
DG
904 ret = -1;
905 ERR("Relayd begin data pending replied error %d", reply.ret_code);
f7079f67
DG
906 goto error;
907 }
908
909 return 0;
910
911error:
912 return ret;
913}
914
915/*
916 * End a data pending command for a specific session id.
917 *
918 * Return 0 on success and set is_data_inflight to 0 if no data is being
919 * streamed or 1 if it is the case.
920 */
6151a90f 921int relayd_end_data_pending(struct lttcomm_relayd_sock *rsock, uint64_t id,
f7079f67
DG
922 unsigned int *is_data_inflight)
923{
af6c30b5 924 int ret, recv_ret;
f7079f67
DG
925 struct lttcomm_relayd_end_data_pending msg;
926 struct lttcomm_relayd_generic_reply reply;
927
928 /* Code flow error. Safety net. */
6151a90f 929 assert(rsock);
f7079f67
DG
930
931 DBG("Relayd end data pending");
932
53efb85a 933 memset(&msg, 0, sizeof(msg));
f7079f67
DG
934 msg.session_id = htobe64(id);
935
936 /* Send command */
6151a90f 937 ret = send_command(rsock, RELAYD_END_DATA_PENDING, &msg, sizeof(msg), 0);
f7079f67
DG
938 if (ret < 0) {
939 goto error;
940 }
941
20275fe8 942 /* Receive response */
6151a90f 943 ret = recv_reply(rsock, (void *) &reply, sizeof(reply));
f7079f67
DG
944 if (ret < 0) {
945 goto error;
946 }
947
af6c30b5
DG
948 recv_ret = be32toh(reply.ret_code);
949 if (recv_ret < 0) {
950 ret = recv_ret;
f7079f67
DG
951 goto error;
952 }
953
af6c30b5 954 *is_data_inflight = recv_ret;
f7079f67 955
af6c30b5 956 DBG("Relayd end data pending is data inflight: %d", recv_ret);
f7079f67
DG
957
958 return 0;
959
960error:
961 return ret;
962}
1c20f0e2
JD
963
964/*
965 * Send index to the relayd.
966 */
967int relayd_send_index(struct lttcomm_relayd_sock *rsock,
50adc264 968 struct ctf_packet_index *index, uint64_t relay_stream_id,
1c20f0e2
JD
969 uint64_t net_seq_num)
970{
971 int ret;
972 struct lttcomm_relayd_index msg;
973 struct lttcomm_relayd_generic_reply reply;
974
975 /* Code flow error. Safety net. */
976 assert(rsock);
977
978 if (rsock->minor < 4) {
979 DBG("Not sending indexes before protocol 2.4");
980 ret = 0;
981 goto error;
982 }
983
984 DBG("Relayd sending index for stream ID %" PRIu64, relay_stream_id);
985
53efb85a 986 memset(&msg, 0, sizeof(msg));
1c20f0e2
JD
987 msg.relay_stream_id = htobe64(relay_stream_id);
988 msg.net_seq_num = htobe64(net_seq_num);
989
990 /* The index is already in big endian. */
991 msg.packet_size = index->packet_size;
992 msg.content_size = index->content_size;
993 msg.timestamp_begin = index->timestamp_begin;
994 msg.timestamp_end = index->timestamp_end;
995 msg.events_discarded = index->events_discarded;
996 msg.stream_id = index->stream_id;
997
234cd636
JD
998 if (rsock->minor >= 8) {
999 msg.stream_instance_id = index->stream_instance_id;
1000 msg.packet_seq_num = index->packet_seq_num;
1001 }
1002
1c20f0e2 1003 /* Send command */
f8f3885c
MD
1004 ret = send_command(rsock, RELAYD_SEND_INDEX, &msg,
1005 lttcomm_relayd_index_len(lttng_to_index_major(rsock->major,
1006 rsock->minor),
1007 lttng_to_index_minor(rsock->major, rsock->minor)),
1008 0);
1c20f0e2
JD
1009 if (ret < 0) {
1010 goto error;
1011 }
1012
1013 /* Receive response */
1014 ret = recv_reply(rsock, (void *) &reply, sizeof(reply));
1015 if (ret < 0) {
1016 goto error;
1017 }
1018
1019 reply.ret_code = be32toh(reply.ret_code);
1020
1021 /* Return session id or negative ret code. */
1022 if (reply.ret_code != LTTNG_OK) {
1023 ret = -1;
1024 ERR("Relayd send index replied error %d", reply.ret_code);
1025 } else {
1026 /* Success */
1027 ret = 0;
1028 }
1029
1030error:
1031 return ret;
1032}
93ec662e
JD
1033
1034/*
1035 * Ask the relay to reset the metadata trace file (regeneration).
1036 */
1037int relayd_reset_metadata(struct lttcomm_relayd_sock *rsock,
1038 uint64_t stream_id, uint64_t version)
1039{
1040 int ret;
1041 struct lttcomm_relayd_reset_metadata msg;
1042 struct lttcomm_relayd_generic_reply reply;
1043
1044 /* Code flow error. Safety net. */
1045 assert(rsock);
1046
1047 /* Should have been prevented by the sessiond. */
1048 if (rsock->minor < 8) {
1049 ERR("Metadata regeneration unsupported before 2.8");
1050 ret = -1;
1051 goto error;
1052 }
1053
1054 DBG("Relayd reset metadata stream id %" PRIu64, stream_id);
1055
1056 memset(&msg, 0, sizeof(msg));
1057 msg.stream_id = htobe64(stream_id);
1058 msg.version = htobe64(version);
1059
1060 /* Send command */
1061 ret = send_command(rsock, RELAYD_RESET_METADATA, (void *) &msg, sizeof(msg), 0);
1062 if (ret < 0) {
1063 goto error;
1064 }
1065
1066 /* Receive response */
1067 ret = recv_reply(rsock, (void *) &reply, sizeof(reply));
1068 if (ret < 0) {
1069 goto error;
1070 }
1071
1072 reply.ret_code = be32toh(reply.ret_code);
1073
1074 /* Return session id or negative ret code. */
1075 if (reply.ret_code != LTTNG_OK) {
1076 ret = -1;
1077 ERR("Relayd reset metadata replied error %d", reply.ret_code);
1078 } else {
1079 /* Success */
1080 ret = 0;
1081 }
1082
1083 DBG("Relayd reset metadata stream id %" PRIu64 " successfully", stream_id);
1084
1085error:
1086 return ret;
1087}
a1ae2ea5 1088
d73bf3d7
JD
1089int relayd_rotate_stream(struct lttcomm_relayd_sock *rsock, uint64_t stream_id,
1090 const char *new_pathname, uint64_t new_chunk_id,
1091 uint64_t seq_num)
1092{
1093 int ret;
1094 struct lttcomm_relayd_rotate_stream *msg = NULL;
1095 struct lttcomm_relayd_generic_reply reply;
1096 size_t len;
1097 int msg_len;
1098
1099 /* Code flow error. Safety net. */
1100 assert(rsock);
1101
1102 DBG("Sending rotate stream id %" PRIu64 " command to relayd", stream_id);
1103
1104 /* Account for the trailing NULL. */
1105 len = strnlen(new_pathname, LTTNG_PATH_MAX) + 1;
1106 if (len > LTTNG_PATH_MAX) {
1107 ERR("Path used in relayd rotate stream command exceeds the maximal allowed length");
1108 ret = -1;
1109 goto error;
1110 }
1111
1112 msg_len = offsetof(struct lttcomm_relayd_rotate_stream, new_pathname) + len;
1113 msg = zmalloc(msg_len);
1114 if (!msg) {
1115 PERROR("Failed to allocate relayd rotate stream command of %d bytes",
1116 msg_len);
1117 ret = -1;
1118 goto error;
1119 }
1120
1121 if (lttng_strncpy(msg->new_pathname, new_pathname, len)) {
1122 ret = -1;
1123 ERR("Failed to copy relayd rotate stream command's new path name");
1124 goto error;
1125 }
1126
1127 msg->pathname_length = htobe32(len);
1128 msg->stream_id = htobe64(stream_id);
1129 msg->new_chunk_id = htobe64(new_chunk_id);
1130 /*
1131 * The seq_num is invalid for metadata streams, but it is ignored on
1132 * the relay.
1133 */
1134 msg->rotate_at_seq_num = htobe64(seq_num);
1135
1136 /* Send command. */
1137 ret = send_command(rsock, RELAYD_ROTATE_STREAM, (void *) msg, msg_len, 0);
1138 if (ret < 0) {
1139 ERR("Send rotate command");
1140 goto error;
1141 }
1142
1143 /* Receive response. */
1144 ret = recv_reply(rsock, (void *) &reply, sizeof(reply));
1145 if (ret < 0) {
1146 ERR("Receive rotate reply");
1147 goto error;
1148 }
1149
1150 reply.ret_code = be32toh(reply.ret_code);
1151
1152 /* Return session id or negative ret code. */
1153 if (reply.ret_code != LTTNG_OK) {
1154 ret = -1;
1155 ERR("Relayd rotate stream replied error %d", reply.ret_code);
1156 } else {
1157 /* Success. */
1158 ret = 0;
1159 DBG("Relayd rotated stream id %" PRIu64 " successfully", stream_id);
1160 }
1161
1162error:
1163 free(msg);
1164 return ret;
1165}
1166
00fb02ac
JD
1167int relayd_rotate_rename(struct lttcomm_relayd_sock *rsock,
1168 const char *old_path, const char *new_path)
1169{
1170 int ret;
1171 struct lttcomm_relayd_rotate_rename *msg = NULL;
1172 struct lttcomm_relayd_generic_reply reply;
1173 size_t old_path_length, new_path_length;
1174 size_t msg_length;
1175
1176 /* Code flow error. Safety net. */
1177 assert(rsock);
1178
1179 DBG("Relayd rename chunk %s to %s", old_path, new_path);
1180
1181 /* The two paths are sent with a '\0' delimiter between them. */
1182 old_path_length = strlen(old_path) + 1;
1183 new_path_length = strlen(new_path) + 1;
1184
1185 msg_length = sizeof(*msg) + old_path_length + new_path_length;
1186 msg = zmalloc(msg_length);
1187 if (!msg) {
1188 PERROR("zmalloc rotate-rename command message");
1189 ret = -1;
1190 goto error;
1191 }
1192
1193 assert(old_path_length <= UINT32_MAX);
1194 msg->old_path_length = htobe32(old_path_length);
1195
1196 assert(new_path_length <= UINT32_MAX);
1197 msg->new_path_length = htobe32(new_path_length);
1198
1199 strcpy(msg->paths, old_path);
1200 strcpy(msg->paths + old_path_length, new_path);
1201
1202 /* Send command */
1203 ret = send_command(rsock, RELAYD_ROTATE_RENAME, (const void *) msg,
1204 msg_length, 0);
1205 if (ret < 0) {
1206 goto error;
1207 }
1208
1209 /* Receive response */
1210 ret = recv_reply(rsock, (void *) &reply, sizeof(reply));
1211 if (ret < 0) {
1212 goto error;
1213 }
1214
1215 reply.ret_code = be32toh(reply.ret_code);
1216
1217 /* Return session id or negative ret code. */
1218 if (reply.ret_code != LTTNG_OK) {
1219 ret = -1;
1220 ERR("Relayd rotate rename replied error %d", reply.ret_code);
1221 } else {
1222 /* Success */
1223 ret = 0;
1224 }
1225
1226 DBG("Relayd rotate rename completed successfully");
1227
1228error:
1229 free(msg);
1230 return ret;
1231}
1232
d88744a4
JD
1233int relayd_rotate_pending(struct lttcomm_relayd_sock *rsock, uint64_t chunk_id)
1234{
1235 int ret;
1236 struct lttcomm_relayd_rotate_pending msg;
1237 struct lttcomm_relayd_rotate_pending_reply reply;
1238
1239 /* Code flow error. Safety net. */
1240 assert(rsock);
1241
1242 DBG("Querying relayd for rotate pending with chunk_id %" PRIu64,
1243 chunk_id);
1244
1245 memset(&msg, 0, sizeof(msg));
1246 msg.chunk_id = htobe64(chunk_id);
1247
1248 /* Send command */
1249 ret = send_command(rsock, RELAYD_ROTATE_PENDING, (void *) &msg,
1250 sizeof(msg), 0);
1251 if (ret < 0) {
1252 goto error;
1253 }
1254
1255 /* Receive response */
1256 ret = recv_reply(rsock, (void *) &reply, sizeof(reply));
1257 if (ret < 0) {
1258 goto error;
1259 }
1260
1261 reply.generic.ret_code = be32toh(reply.generic.ret_code);
1262
1263 /* Return session id or negative ret code. */
1264 if (reply.generic.ret_code != LTTNG_OK) {
1265 ret = -reply.generic.ret_code;
1266 ERR("Relayd rotate pending replied with error %d", ret);
1267 goto error;
1268 } else {
1269 /* No error, just rotate pending state */
1270 if (reply.is_pending == 0 || reply.is_pending == 1) {
1271 ret = reply.is_pending;
1272 DBG("Relayd rotate pending command completed successfully with result \"%s\"",
1273 ret ? "rotation pending" : "rotation NOT pending");
1274 } else {
1275 ret = -LTTNG_ERR_UNK;
1276 }
1277 }
1278
1279error:
1280 return ret;
1281}
1282
a1ae2ea5
JD
1283int relayd_mkdir(struct lttcomm_relayd_sock *rsock, const char *path)
1284{
1285 int ret;
1286 struct lttcomm_relayd_mkdir *msg;
1287 struct lttcomm_relayd_generic_reply reply;
1288 size_t len;
1289
1290 /* Code flow error. Safety net. */
1291 assert(rsock);
1292
1293 DBG("Relayd mkdir path %s", path);
1294
1295 len = strlen(path) + 1;
1296 msg = zmalloc(sizeof(msg->length) + len);
1297 if (!msg) {
1298 PERROR("Alloc mkdir msg");
1299 ret = -1;
1300 goto error;
1301 }
1302 msg->length = htobe32((uint32_t) len);
1303
1304 if (lttng_strncpy(msg->path, path, len)) {
1305 ret = -1;
1306 goto error;
1307 }
1308
1309 /* Send command */
1310 ret = send_command(rsock, RELAYD_MKDIR, (void *) msg,
1311 sizeof(msg->length) + len, 0);
1312 if (ret < 0) {
1313 goto error;
1314 }
1315
1316 /* Receive response */
1317 ret = recv_reply(rsock, (void *) &reply, sizeof(reply));
1318 if (ret < 0) {
1319 goto error;
1320 }
1321
1322 reply.ret_code = be32toh(reply.ret_code);
1323
1324 /* Return session id or negative ret code. */
1325 if (reply.ret_code != LTTNG_OK) {
1326 ret = -1;
1327 ERR("Relayd mkdir replied error %d", reply.ret_code);
1328 } else {
1329 /* Success */
1330 ret = 0;
1331 }
1332
1333 DBG("Relayd mkdir completed successfully");
1334
1335error:
1336 free(msg);
1337 return ret;
a1ae2ea5 1338}
This page took 0.10017 seconds and 4 git commands to generate.