Use dynamic payload for the add stream realyd command
[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
JD
406 const char *pathname, uint64_t *stream_id,
407 uint64_t tracefile_size, uint64_t tracefile_count)
00e2e675
DG
408{
409 int ret;
00e2e675
DG
410 struct lttcomm_relayd_status_stream reply;
411
412 /* Code flow error. Safety net. */
6151a90f 413 assert(rsock);
00e2e675
DG
414 assert(channel_name);
415 assert(pathname);
416
417 DBG("Relayd adding stream for channel name %s", channel_name);
418
0f907de1
JD
419 /* Compat with relayd 2.1 */
420 if (rsock->minor == 1) {
2f21a469
JR
421 /* For 2.1 */
422 ret = relayd_add_stream_2_1(rsock, channel_name, pathname);
423
424 } else if (rsock->minor > 1 && rsock->minor < 11) {
425 /* From 2.2 to 2.10 */
426 ret = relayd_add_stream_2_2(rsock, channel_name, pathname,
427 tracefile_size, tracefile_count);
0f907de1 428 } else {
2f21a469
JR
429 /* From 2.11 to ...*/
430 ret = relayd_add_stream_2_11(rsock, channel_name, pathname,
431 tracefile_size, tracefile_count);
432 }
0f907de1 433
2f21a469
JR
434 if (ret) {
435 ret = -1;
436 goto error;
00e2e675
DG
437 }
438
633d0084 439 /* Waiting for reply */
6151a90f 440 ret = recv_reply(rsock, (void *) &reply, sizeof(reply));
00e2e675
DG
441 if (ret < 0) {
442 goto error;
443 }
444
445 /* Back to host bytes order. */
446 reply.handle = be64toh(reply.handle);
447 reply.ret_code = be32toh(reply.ret_code);
448
449 /* Return session id or negative ret code. */
f73fabfd 450 if (reply.ret_code != LTTNG_OK) {
bb63afd9
DG
451 ret = -1;
452 ERR("Relayd add stream replied error %d", reply.ret_code);
00e2e675
DG
453 } else {
454 /* Success */
455 ret = 0;
456 *stream_id = reply.handle;
457 }
458
77c7c900 459 DBG("Relayd stream added successfully with handle %" PRIu64,
2f21a469 460 reply.handle);
00e2e675
DG
461
462error:
463 return ret;
464}
465
a4baae1b
JD
466/*
467 * Inform the relay that all the streams for the current channel has been sent.
468 *
469 * On success return 0 else return ret_code negative value.
470 */
471int relayd_streams_sent(struct lttcomm_relayd_sock *rsock)
472{
473 int ret;
474 struct lttcomm_relayd_generic_reply reply;
475
476 /* Code flow error. Safety net. */
477 assert(rsock);
478
479 DBG("Relayd sending streams sent.");
480
481 /* This feature was introduced in 2.4, ignore it for earlier versions. */
482 if (rsock->minor < 4) {
483 ret = 0;
484 goto end;
485 }
486
487 /* Send command */
488 ret = send_command(rsock, RELAYD_STREAMS_SENT, NULL, 0, 0);
489 if (ret < 0) {
490 goto error;
491 }
492
493 /* Waiting for reply */
494 ret = recv_reply(rsock, (void *) &reply, sizeof(reply));
495 if (ret < 0) {
496 goto error;
497 }
498
499 /* Back to host bytes order. */
500 reply.ret_code = be32toh(reply.ret_code);
501
502 /* Return session id or negative ret code. */
503 if (reply.ret_code != LTTNG_OK) {
504 ret = -1;
505 ERR("Relayd streams sent replied error %d", reply.ret_code);
506 goto error;
507 } else {
508 /* Success */
509 ret = 0;
510 }
511
512 DBG("Relayd streams sent success");
513
514error:
515end:
516 return ret;
517}
518
00e2e675
DG
519/*
520 * Check version numbers on the relayd.
d4519fa3
JD
521 * If major versions are compatible, we assign minor_to_use to the
522 * minor version of the procotol we are going to use for this session.
00e2e675 523 *
67d5aa28
JD
524 * Return 0 if the two daemons are compatible, LTTNG_ERR_RELAYD_VERSION_FAIL
525 * otherwise, or a negative value on network errors.
00e2e675 526 */
6151a90f 527int relayd_version_check(struct lttcomm_relayd_sock *rsock)
00e2e675
DG
528{
529 int ret;
092b6259 530 struct lttcomm_relayd_version msg;
00e2e675
DG
531
532 /* Code flow error. Safety net. */
6151a90f 533 assert(rsock);
00e2e675 534
6151a90f
JD
535 DBG("Relayd version check for major.minor %u.%u", rsock->major,
536 rsock->minor);
00e2e675 537
53efb85a 538 memset(&msg, 0, sizeof(msg));
092b6259 539 /* Prepare network byte order before transmission. */
6151a90f
JD
540 msg.major = htobe32(rsock->major);
541 msg.minor = htobe32(rsock->minor);
092b6259 542
00e2e675 543 /* Send command */
6151a90f 544 ret = send_command(rsock, RELAYD_VERSION, (void *) &msg, sizeof(msg), 0);
00e2e675
DG
545 if (ret < 0) {
546 goto error;
547 }
548
20275fe8 549 /* Receive response */
6151a90f 550 ret = recv_reply(rsock, (void *) &msg, sizeof(msg));
00e2e675
DG
551 if (ret < 0) {
552 goto error;
553 }
554
555 /* Set back to host bytes order */
092b6259
DG
556 msg.major = be32toh(msg.major);
557 msg.minor = be32toh(msg.minor);
558
559 /*
560 * Only validate the major version. If the other side is higher,
561 * communication is not possible. Only major version equal can talk to each
562 * other. If the minor version differs, the lowest version is used by both
563 * sides.
092b6259 564 */
6151a90f 565 if (msg.major != rsock->major) {
d4519fa3 566 /* Not compatible */
67d5aa28 567 ret = LTTNG_ERR_RELAYD_VERSION_FAIL;
d4519fa3 568 DBG2("Relayd version is NOT compatible. Relayd version %u != %u (us)",
6151a90f 569 msg.major, rsock->major);
092b6259 570 goto error;
00e2e675
DG
571 }
572
092b6259 573 /*
6151a90f
JD
574 * If the relayd's minor version is higher, it will adapt to our version so
575 * we can continue to use the latest relayd communication data structure.
576 * If the received minor version is higher, the relayd should adapt to us.
092b6259 577 */
6151a90f
JD
578 if (rsock->minor > msg.minor) {
579 rsock->minor = msg.minor;
d4519fa3 580 }
092b6259 581
d4519fa3
JD
582 /* Version number compatible */
583 DBG2("Relayd version is compatible, using protocol version %u.%u",
6151a90f 584 rsock->major, rsock->minor);
d4519fa3 585 ret = 0;
00e2e675
DG
586
587error:
588 return ret;
589}
590
00e2e675
DG
591/*
592 * Add stream on the relayd and assign stream handle to the stream_id argument.
593 *
594 * On success return 0 else return ret_code negative value.
595 */
6151a90f 596int relayd_send_metadata(struct lttcomm_relayd_sock *rsock, size_t len)
00e2e675
DG
597{
598 int ret;
599
600 /* Code flow error. Safety net. */
6151a90f 601 assert(rsock);
00e2e675 602
77c7c900 603 DBG("Relayd sending metadata of size %zu", len);
00e2e675
DG
604
605 /* Send command */
6151a90f 606 ret = send_command(rsock, RELAYD_SEND_METADATA, NULL, len, 0);
00e2e675
DG
607 if (ret < 0) {
608 goto error;
609 }
610
611 DBG2("Relayd metadata added successfully");
612
613 /*
614 * After that call, the metadata data MUST be sent to the relayd so the
615 * receive size on the other end matches the len of the metadata packet
633d0084 616 * header. This is why we don't wait for a reply here.
00e2e675
DG
617 */
618
619error:
620 return ret;
621}
622
623/*
6151a90f 624 * Connect to relay daemon with an allocated lttcomm_relayd_sock.
00e2e675 625 */
6151a90f 626int relayd_connect(struct lttcomm_relayd_sock *rsock)
00e2e675
DG
627{
628 /* Code flow error. Safety net. */
6151a90f 629 assert(rsock);
00e2e675 630
f96e4545
MD
631 if (!rsock->sock.ops) {
632 /*
633 * Attempting a connect on a non-initialized socket.
634 */
635 return -ECONNRESET;
636 }
637
00e2e675
DG
638 DBG3("Relayd connect ...");
639
6151a90f 640 return rsock->sock.ops->connect(&rsock->sock);
00e2e675
DG
641}
642
643/*
6151a90f 644 * Close relayd socket with an allocated lttcomm_relayd_sock.
ffe60014
DG
645 *
646 * If no socket operations are found, simply return 0 meaning that everything
647 * is fine. Without operations, the socket can not possibly be opened or used.
648 * This is possible if the socket was allocated but not created. However, the
649 * caller could simply use it to store a valid file descriptor for instance
650 * passed over a Unix socket and call this to cleanup but still without a valid
651 * ops pointer.
652 *
653 * Return the close returned value. On error, a negative value is usually
654 * returned back from close(2).
00e2e675 655 */
6151a90f 656int relayd_close(struct lttcomm_relayd_sock *rsock)
00e2e675 657{
ffe60014
DG
658 int ret;
659
00e2e675 660 /* Code flow error. Safety net. */
6151a90f 661 assert(rsock);
00e2e675 662
ffe60014 663 /* An invalid fd is fine, return success. */
6151a90f 664 if (rsock->sock.fd < 0) {
ffe60014
DG
665 ret = 0;
666 goto end;
667 }
668
6151a90f 669 DBG3("Relayd closing socket %d", rsock->sock.fd);
00e2e675 670
6151a90f
JD
671 if (rsock->sock.ops) {
672 ret = rsock->sock.ops->close(&rsock->sock);
ffe60014
DG
673 } else {
674 /* Default call if no specific ops found. */
6151a90f 675 ret = close(rsock->sock.fd);
ffe60014
DG
676 if (ret < 0) {
677 PERROR("relayd_close default close");
678 }
679 }
f96e4545 680 rsock->sock.fd = -1;
ffe60014
DG
681
682end:
683 return ret;
00e2e675
DG
684}
685
686/*
687 * Send data header structure to the relayd.
688 */
6151a90f 689int relayd_send_data_hdr(struct lttcomm_relayd_sock *rsock,
00e2e675
DG
690 struct lttcomm_relayd_data_hdr *hdr, size_t size)
691{
692 int ret;
693
694 /* Code flow error. Safety net. */
6151a90f 695 assert(rsock);
00e2e675
DG
696 assert(hdr);
697
f96e4545
MD
698 if (rsock->sock.fd < 0) {
699 return -ECONNRESET;
700 }
701
8fd623e0 702 DBG3("Relayd sending data header of size %zu", size);
00e2e675
DG
703
704 /* Again, safety net */
705 if (size == 0) {
706 size = sizeof(struct lttcomm_relayd_data_hdr);
707 }
708
709 /* Only send data header. */
6151a90f 710 ret = rsock->sock.ops->sendmsg(&rsock->sock, hdr, size, 0);
00e2e675 711 if (ret < 0) {
8994307f 712 ret = -errno;
00e2e675
DG
713 goto error;
714 }
715
716 /*
717 * The data MUST be sent right after that command for the receive on the
718 * other end to match the size in the header.
719 */
720
721error:
722 return ret;
723}
173af62f
DG
724
725/*
726 * Send close stream command to the relayd.
727 */
6151a90f 728int relayd_send_close_stream(struct lttcomm_relayd_sock *rsock, uint64_t stream_id,
173af62f
DG
729 uint64_t last_net_seq_num)
730{
731 int ret;
732 struct lttcomm_relayd_close_stream msg;
733 struct lttcomm_relayd_generic_reply reply;
734
735 /* Code flow error. Safety net. */
6151a90f 736 assert(rsock);
173af62f 737
77c7c900 738 DBG("Relayd closing stream id %" PRIu64, stream_id);
173af62f 739
53efb85a 740 memset(&msg, 0, sizeof(msg));
173af62f
DG
741 msg.stream_id = htobe64(stream_id);
742 msg.last_net_seq_num = htobe64(last_net_seq_num);
743
744 /* Send command */
6151a90f 745 ret = send_command(rsock, RELAYD_CLOSE_STREAM, (void *) &msg, sizeof(msg), 0);
173af62f
DG
746 if (ret < 0) {
747 goto error;
748 }
749
20275fe8 750 /* Receive response */
6151a90f 751 ret = recv_reply(rsock, (void *) &reply, sizeof(reply));
173af62f
DG
752 if (ret < 0) {
753 goto error;
754 }
755
756 reply.ret_code = be32toh(reply.ret_code);
757
758 /* Return session id or negative ret code. */
f73fabfd 759 if (reply.ret_code != LTTNG_OK) {
bb63afd9
DG
760 ret = -1;
761 ERR("Relayd close stream replied error %d", reply.ret_code);
173af62f
DG
762 } else {
763 /* Success */
764 ret = 0;
765 }
766
77c7c900 767 DBG("Relayd close stream id %" PRIu64 " successfully", stream_id);
173af62f
DG
768
769error:
770 return ret;
771}
c8f59ee5
DG
772
773/*
774 * Check for data availability for a given stream id.
775 *
6d805429 776 * Return 0 if NOT pending, 1 if so and a negative value on error.
c8f59ee5 777 */
6151a90f 778int relayd_data_pending(struct lttcomm_relayd_sock *rsock, uint64_t stream_id,
c8f59ee5
DG
779 uint64_t last_net_seq_num)
780{
781 int ret;
6d805429 782 struct lttcomm_relayd_data_pending msg;
c8f59ee5
DG
783 struct lttcomm_relayd_generic_reply reply;
784
785 /* Code flow error. Safety net. */
6151a90f 786 assert(rsock);
c8f59ee5 787
6d805429 788 DBG("Relayd data pending for stream id %" PRIu64, stream_id);
c8f59ee5 789
53efb85a 790 memset(&msg, 0, sizeof(msg));
c8f59ee5
DG
791 msg.stream_id = htobe64(stream_id);
792 msg.last_net_seq_num = htobe64(last_net_seq_num);
793
794 /* Send command */
6151a90f 795 ret = send_command(rsock, RELAYD_DATA_PENDING, (void *) &msg,
c8f59ee5
DG
796 sizeof(msg), 0);
797 if (ret < 0) {
798 goto error;
799 }
800
20275fe8 801 /* Receive response */
6151a90f 802 ret = recv_reply(rsock, (void *) &reply, sizeof(reply));
c8f59ee5
DG
803 if (ret < 0) {
804 goto error;
805 }
806
807 reply.ret_code = be32toh(reply.ret_code);
808
809 /* Return session id or negative ret code. */
810 if (reply.ret_code >= LTTNG_OK) {
bb63afd9 811 ERR("Relayd data pending replied error %d", reply.ret_code);
c8f59ee5
DG
812 }
813
814 /* At this point, the ret code is either 1 or 0 */
815 ret = reply.ret_code;
816
6d805429 817 DBG("Relayd data is %s pending for stream id %" PRIu64,
9dd26bb9 818 ret == 1 ? "" : "NOT", stream_id);
c8f59ee5
DG
819
820error:
821 return ret;
822}
823
824/*
825 * Check on the relayd side for a quiescent state on the control socket.
826 */
6151a90f 827int relayd_quiescent_control(struct lttcomm_relayd_sock *rsock,
ad7051c0 828 uint64_t metadata_stream_id)
c8f59ee5
DG
829{
830 int ret;
ad7051c0 831 struct lttcomm_relayd_quiescent_control msg;
c8f59ee5
DG
832 struct lttcomm_relayd_generic_reply reply;
833
834 /* Code flow error. Safety net. */
6151a90f 835 assert(rsock);
c8f59ee5
DG
836
837 DBG("Relayd checking quiescent control state");
838
53efb85a 839 memset(&msg, 0, sizeof(msg));
ad7051c0
DG
840 msg.stream_id = htobe64(metadata_stream_id);
841
c8f59ee5 842 /* Send command */
6151a90f 843 ret = send_command(rsock, RELAYD_QUIESCENT_CONTROL, &msg, sizeof(msg), 0);
c8f59ee5
DG
844 if (ret < 0) {
845 goto error;
846 }
847
20275fe8 848 /* Receive response */
6151a90f 849 ret = recv_reply(rsock, (void *) &reply, sizeof(reply));
c8f59ee5
DG
850 if (ret < 0) {
851 goto error;
852 }
853
854 reply.ret_code = be32toh(reply.ret_code);
855
856 /* Return session id or negative ret code. */
857 if (reply.ret_code != LTTNG_OK) {
bb63afd9
DG
858 ret = -1;
859 ERR("Relayd quiescent control replied error %d", reply.ret_code);
c8f59ee5
DG
860 goto error;
861 }
862
863 /* Control socket is quiescent */
6d805429 864 return 0;
c8f59ee5
DG
865
866error:
867 return ret;
868}
f7079f67
DG
869
870/*
871 * Begin a data pending command for a specific session id.
872 */
6151a90f 873int relayd_begin_data_pending(struct lttcomm_relayd_sock *rsock, uint64_t id)
f7079f67
DG
874{
875 int ret;
876 struct lttcomm_relayd_begin_data_pending msg;
877 struct lttcomm_relayd_generic_reply reply;
878
879 /* Code flow error. Safety net. */
6151a90f 880 assert(rsock);
f7079f67
DG
881
882 DBG("Relayd begin data pending");
883
53efb85a 884 memset(&msg, 0, sizeof(msg));
f7079f67
DG
885 msg.session_id = htobe64(id);
886
887 /* Send command */
6151a90f 888 ret = send_command(rsock, RELAYD_BEGIN_DATA_PENDING, &msg, sizeof(msg), 0);
f7079f67
DG
889 if (ret < 0) {
890 goto error;
891 }
892
20275fe8 893 /* Receive response */
6151a90f 894 ret = recv_reply(rsock, (void *) &reply, sizeof(reply));
f7079f67
DG
895 if (ret < 0) {
896 goto error;
897 }
898
899 reply.ret_code = be32toh(reply.ret_code);
900
901 /* Return session id or negative ret code. */
902 if (reply.ret_code != LTTNG_OK) {
bb63afd9
DG
903 ret = -1;
904 ERR("Relayd begin data pending replied error %d", reply.ret_code);
f7079f67
DG
905 goto error;
906 }
907
908 return 0;
909
910error:
911 return ret;
912}
913
914/*
915 * End a data pending command for a specific session id.
916 *
917 * Return 0 on success and set is_data_inflight to 0 if no data is being
918 * streamed or 1 if it is the case.
919 */
6151a90f 920int relayd_end_data_pending(struct lttcomm_relayd_sock *rsock, uint64_t id,
f7079f67
DG
921 unsigned int *is_data_inflight)
922{
af6c30b5 923 int ret, recv_ret;
f7079f67
DG
924 struct lttcomm_relayd_end_data_pending msg;
925 struct lttcomm_relayd_generic_reply reply;
926
927 /* Code flow error. Safety net. */
6151a90f 928 assert(rsock);
f7079f67
DG
929
930 DBG("Relayd end data pending");
931
53efb85a 932 memset(&msg, 0, sizeof(msg));
f7079f67
DG
933 msg.session_id = htobe64(id);
934
935 /* Send command */
6151a90f 936 ret = send_command(rsock, RELAYD_END_DATA_PENDING, &msg, sizeof(msg), 0);
f7079f67
DG
937 if (ret < 0) {
938 goto error;
939 }
940
20275fe8 941 /* Receive response */
6151a90f 942 ret = recv_reply(rsock, (void *) &reply, sizeof(reply));
f7079f67
DG
943 if (ret < 0) {
944 goto error;
945 }
946
af6c30b5
DG
947 recv_ret = be32toh(reply.ret_code);
948 if (recv_ret < 0) {
949 ret = recv_ret;
f7079f67
DG
950 goto error;
951 }
952
af6c30b5 953 *is_data_inflight = recv_ret;
f7079f67 954
af6c30b5 955 DBG("Relayd end data pending is data inflight: %d", recv_ret);
f7079f67
DG
956
957 return 0;
958
959error:
960 return ret;
961}
1c20f0e2
JD
962
963/*
964 * Send index to the relayd.
965 */
966int relayd_send_index(struct lttcomm_relayd_sock *rsock,
50adc264 967 struct ctf_packet_index *index, uint64_t relay_stream_id,
1c20f0e2
JD
968 uint64_t net_seq_num)
969{
970 int ret;
971 struct lttcomm_relayd_index msg;
972 struct lttcomm_relayd_generic_reply reply;
973
974 /* Code flow error. Safety net. */
975 assert(rsock);
976
977 if (rsock->minor < 4) {
978 DBG("Not sending indexes before protocol 2.4");
979 ret = 0;
980 goto error;
981 }
982
983 DBG("Relayd sending index for stream ID %" PRIu64, relay_stream_id);
984
53efb85a 985 memset(&msg, 0, sizeof(msg));
1c20f0e2
JD
986 msg.relay_stream_id = htobe64(relay_stream_id);
987 msg.net_seq_num = htobe64(net_seq_num);
988
989 /* The index is already in big endian. */
990 msg.packet_size = index->packet_size;
991 msg.content_size = index->content_size;
992 msg.timestamp_begin = index->timestamp_begin;
993 msg.timestamp_end = index->timestamp_end;
994 msg.events_discarded = index->events_discarded;
995 msg.stream_id = index->stream_id;
996
234cd636
JD
997 if (rsock->minor >= 8) {
998 msg.stream_instance_id = index->stream_instance_id;
999 msg.packet_seq_num = index->packet_seq_num;
1000 }
1001
1c20f0e2 1002 /* Send command */
f8f3885c
MD
1003 ret = send_command(rsock, RELAYD_SEND_INDEX, &msg,
1004 lttcomm_relayd_index_len(lttng_to_index_major(rsock->major,
1005 rsock->minor),
1006 lttng_to_index_minor(rsock->major, rsock->minor)),
1007 0);
1c20f0e2
JD
1008 if (ret < 0) {
1009 goto error;
1010 }
1011
1012 /* Receive response */
1013 ret = recv_reply(rsock, (void *) &reply, sizeof(reply));
1014 if (ret < 0) {
1015 goto error;
1016 }
1017
1018 reply.ret_code = be32toh(reply.ret_code);
1019
1020 /* Return session id or negative ret code. */
1021 if (reply.ret_code != LTTNG_OK) {
1022 ret = -1;
1023 ERR("Relayd send index replied error %d", reply.ret_code);
1024 } else {
1025 /* Success */
1026 ret = 0;
1027 }
1028
1029error:
1030 return ret;
1031}
93ec662e
JD
1032
1033/*
1034 * Ask the relay to reset the metadata trace file (regeneration).
1035 */
1036int relayd_reset_metadata(struct lttcomm_relayd_sock *rsock,
1037 uint64_t stream_id, uint64_t version)
1038{
1039 int ret;
1040 struct lttcomm_relayd_reset_metadata msg;
1041 struct lttcomm_relayd_generic_reply reply;
1042
1043 /* Code flow error. Safety net. */
1044 assert(rsock);
1045
1046 /* Should have been prevented by the sessiond. */
1047 if (rsock->minor < 8) {
1048 ERR("Metadata regeneration unsupported before 2.8");
1049 ret = -1;
1050 goto error;
1051 }
1052
1053 DBG("Relayd reset metadata stream id %" PRIu64, stream_id);
1054
1055 memset(&msg, 0, sizeof(msg));
1056 msg.stream_id = htobe64(stream_id);
1057 msg.version = htobe64(version);
1058
1059 /* Send command */
1060 ret = send_command(rsock, RELAYD_RESET_METADATA, (void *) &msg, sizeof(msg), 0);
1061 if (ret < 0) {
1062 goto error;
1063 }
1064
1065 /* Receive response */
1066 ret = recv_reply(rsock, (void *) &reply, sizeof(reply));
1067 if (ret < 0) {
1068 goto error;
1069 }
1070
1071 reply.ret_code = be32toh(reply.ret_code);
1072
1073 /* Return session id or negative ret code. */
1074 if (reply.ret_code != LTTNG_OK) {
1075 ret = -1;
1076 ERR("Relayd reset metadata replied error %d", reply.ret_code);
1077 } else {
1078 /* Success */
1079 ret = 0;
1080 }
1081
1082 DBG("Relayd reset metadata stream id %" PRIu64 " successfully", stream_id);
1083
1084error:
1085 return ret;
1086}
a1ae2ea5 1087
d73bf3d7
JD
1088int relayd_rotate_stream(struct lttcomm_relayd_sock *rsock, uint64_t stream_id,
1089 const char *new_pathname, uint64_t new_chunk_id,
1090 uint64_t seq_num)
1091{
1092 int ret;
1093 struct lttcomm_relayd_rotate_stream *msg = NULL;
1094 struct lttcomm_relayd_generic_reply reply;
1095 size_t len;
1096 int msg_len;
1097
1098 /* Code flow error. Safety net. */
1099 assert(rsock);
1100
1101 DBG("Sending rotate stream id %" PRIu64 " command to relayd", stream_id);
1102
1103 /* Account for the trailing NULL. */
1104 len = strnlen(new_pathname, LTTNG_PATH_MAX) + 1;
1105 if (len > LTTNG_PATH_MAX) {
1106 ERR("Path used in relayd rotate stream command exceeds the maximal allowed length");
1107 ret = -1;
1108 goto error;
1109 }
1110
1111 msg_len = offsetof(struct lttcomm_relayd_rotate_stream, new_pathname) + len;
1112 msg = zmalloc(msg_len);
1113 if (!msg) {
1114 PERROR("Failed to allocate relayd rotate stream command of %d bytes",
1115 msg_len);
1116 ret = -1;
1117 goto error;
1118 }
1119
1120 if (lttng_strncpy(msg->new_pathname, new_pathname, len)) {
1121 ret = -1;
1122 ERR("Failed to copy relayd rotate stream command's new path name");
1123 goto error;
1124 }
1125
1126 msg->pathname_length = htobe32(len);
1127 msg->stream_id = htobe64(stream_id);
1128 msg->new_chunk_id = htobe64(new_chunk_id);
1129 /*
1130 * The seq_num is invalid for metadata streams, but it is ignored on
1131 * the relay.
1132 */
1133 msg->rotate_at_seq_num = htobe64(seq_num);
1134
1135 /* Send command. */
1136 ret = send_command(rsock, RELAYD_ROTATE_STREAM, (void *) msg, msg_len, 0);
1137 if (ret < 0) {
1138 ERR("Send rotate command");
1139 goto error;
1140 }
1141
1142 /* Receive response. */
1143 ret = recv_reply(rsock, (void *) &reply, sizeof(reply));
1144 if (ret < 0) {
1145 ERR("Receive rotate reply");
1146 goto error;
1147 }
1148
1149 reply.ret_code = be32toh(reply.ret_code);
1150
1151 /* Return session id or negative ret code. */
1152 if (reply.ret_code != LTTNG_OK) {
1153 ret = -1;
1154 ERR("Relayd rotate stream replied error %d", reply.ret_code);
1155 } else {
1156 /* Success. */
1157 ret = 0;
1158 DBG("Relayd rotated stream id %" PRIu64 " successfully", stream_id);
1159 }
1160
1161error:
1162 free(msg);
1163 return ret;
1164}
1165
00fb02ac
JD
1166int relayd_rotate_rename(struct lttcomm_relayd_sock *rsock,
1167 const char *old_path, const char *new_path)
1168{
1169 int ret;
1170 struct lttcomm_relayd_rotate_rename *msg = NULL;
1171 struct lttcomm_relayd_generic_reply reply;
1172 size_t old_path_length, new_path_length;
1173 size_t msg_length;
1174
1175 /* Code flow error. Safety net. */
1176 assert(rsock);
1177
1178 DBG("Relayd rename chunk %s to %s", old_path, new_path);
1179
1180 /* The two paths are sent with a '\0' delimiter between them. */
1181 old_path_length = strlen(old_path) + 1;
1182 new_path_length = strlen(new_path) + 1;
1183
1184 msg_length = sizeof(*msg) + old_path_length + new_path_length;
1185 msg = zmalloc(msg_length);
1186 if (!msg) {
1187 PERROR("zmalloc rotate-rename command message");
1188 ret = -1;
1189 goto error;
1190 }
1191
1192 assert(old_path_length <= UINT32_MAX);
1193 msg->old_path_length = htobe32(old_path_length);
1194
1195 assert(new_path_length <= UINT32_MAX);
1196 msg->new_path_length = htobe32(new_path_length);
1197
1198 strcpy(msg->paths, old_path);
1199 strcpy(msg->paths + old_path_length, new_path);
1200
1201 /* Send command */
1202 ret = send_command(rsock, RELAYD_ROTATE_RENAME, (const void *) msg,
1203 msg_length, 0);
1204 if (ret < 0) {
1205 goto error;
1206 }
1207
1208 /* Receive response */
1209 ret = recv_reply(rsock, (void *) &reply, sizeof(reply));
1210 if (ret < 0) {
1211 goto error;
1212 }
1213
1214 reply.ret_code = be32toh(reply.ret_code);
1215
1216 /* Return session id or negative ret code. */
1217 if (reply.ret_code != LTTNG_OK) {
1218 ret = -1;
1219 ERR("Relayd rotate rename replied error %d", reply.ret_code);
1220 } else {
1221 /* Success */
1222 ret = 0;
1223 }
1224
1225 DBG("Relayd rotate rename completed successfully");
1226
1227error:
1228 free(msg);
1229 return ret;
1230}
1231
d88744a4
JD
1232int relayd_rotate_pending(struct lttcomm_relayd_sock *rsock, uint64_t chunk_id)
1233{
1234 int ret;
1235 struct lttcomm_relayd_rotate_pending msg;
1236 struct lttcomm_relayd_rotate_pending_reply reply;
1237
1238 /* Code flow error. Safety net. */
1239 assert(rsock);
1240
1241 DBG("Querying relayd for rotate pending with chunk_id %" PRIu64,
1242 chunk_id);
1243
1244 memset(&msg, 0, sizeof(msg));
1245 msg.chunk_id = htobe64(chunk_id);
1246
1247 /* Send command */
1248 ret = send_command(rsock, RELAYD_ROTATE_PENDING, (void *) &msg,
1249 sizeof(msg), 0);
1250 if (ret < 0) {
1251 goto error;
1252 }
1253
1254 /* Receive response */
1255 ret = recv_reply(rsock, (void *) &reply, sizeof(reply));
1256 if (ret < 0) {
1257 goto error;
1258 }
1259
1260 reply.generic.ret_code = be32toh(reply.generic.ret_code);
1261
1262 /* Return session id or negative ret code. */
1263 if (reply.generic.ret_code != LTTNG_OK) {
1264 ret = -reply.generic.ret_code;
1265 ERR("Relayd rotate pending replied with error %d", ret);
1266 goto error;
1267 } else {
1268 /* No error, just rotate pending state */
1269 if (reply.is_pending == 0 || reply.is_pending == 1) {
1270 ret = reply.is_pending;
1271 DBG("Relayd rotate pending command completed successfully with result \"%s\"",
1272 ret ? "rotation pending" : "rotation NOT pending");
1273 } else {
1274 ret = -LTTNG_ERR_UNK;
1275 }
1276 }
1277
1278error:
1279 return ret;
1280}
1281
a1ae2ea5
JD
1282int relayd_mkdir(struct lttcomm_relayd_sock *rsock, const char *path)
1283{
1284 int ret;
1285 struct lttcomm_relayd_mkdir *msg;
1286 struct lttcomm_relayd_generic_reply reply;
1287 size_t len;
1288
1289 /* Code flow error. Safety net. */
1290 assert(rsock);
1291
1292 DBG("Relayd mkdir path %s", path);
1293
1294 len = strlen(path) + 1;
1295 msg = zmalloc(sizeof(msg->length) + len);
1296 if (!msg) {
1297 PERROR("Alloc mkdir msg");
1298 ret = -1;
1299 goto error;
1300 }
1301 msg->length = htobe32((uint32_t) len);
1302
1303 if (lttng_strncpy(msg->path, path, len)) {
1304 ret = -1;
1305 goto error;
1306 }
1307
1308 /* Send command */
1309 ret = send_command(rsock, RELAYD_MKDIR, (void *) msg,
1310 sizeof(msg->length) + len, 0);
1311 if (ret < 0) {
1312 goto error;
1313 }
1314
1315 /* Receive response */
1316 ret = recv_reply(rsock, (void *) &reply, sizeof(reply));
1317 if (ret < 0) {
1318 goto error;
1319 }
1320
1321 reply.ret_code = be32toh(reply.ret_code);
1322
1323 /* Return session id or negative ret code. */
1324 if (reply.ret_code != LTTNG_OK) {
1325 ret = -1;
1326 ERR("Relayd mkdir replied error %d", reply.ret_code);
1327 } else {
1328 /* Success */
1329 ret = 0;
1330 }
1331
1332 DBG("Relayd mkdir completed successfully");
1333
1334error:
1335 free(msg);
1336 return ret;
a1ae2ea5 1337}
This page took 0.099462 seconds and 4 git commands to generate.