Commit | Line | Data |
---|---|---|
00e2e675 | 1 | /* |
ab5be9fa | 2 | * Copyright (C) 2012 David Goulet <dgoulet@efficios.com> |
00e2e675 | 3 | * |
ab5be9fa | 4 | * SPDX-License-Identifier: GPL-2.0-only |
00e2e675 | 5 | * |
00e2e675 DG |
6 | */ |
7 | ||
6c1c0768 | 8 | #define _LGPL_SOURCE |
00e2e675 DG |
9 | #include <assert.h> |
10 | #include <stdio.h> | |
11 | #include <stdlib.h> | |
12 | #include <string.h> | |
13 | #include <sys/stat.h> | |
77c7c900 | 14 | #include <inttypes.h> |
00e2e675 DG |
15 | |
16 | #include <common/common.h> | |
17 | #include <common/defaults.h> | |
f263b7fd | 18 | #include <common/compat/endian.h> |
27283937 | 19 | #include <common/compat/string.h> |
00e2e675 | 20 | #include <common/sessiond-comm/relayd.h> |
50adc264 | 21 | #include <common/index/ctf-index.h> |
d2956687 | 22 | #include <common/trace-chunk.h> |
c35f9726 | 23 | #include <common/string-utils/format.h> |
00e2e675 DG |
24 | |
25 | #include "relayd.h" | |
26 | ||
070b6a86 MD |
27 | static |
28 | bool relayd_supports_chunks(const struct lttcomm_relayd_sock *sock) | |
29 | { | |
30 | if (sock->major > 2) { | |
31 | return true; | |
32 | } else if (sock->major == 2 && sock->minor >= 11) { | |
33 | return true; | |
34 | } | |
35 | return false; | |
36 | } | |
37 | ||
8614e600 MD |
38 | static |
39 | bool relayd_supports_get_configuration(const struct lttcomm_relayd_sock *sock) | |
40 | { | |
41 | if (sock->major > 2) { | |
42 | return true; | |
43 | } else if (sock->major == 2 && sock->minor >= 12) { | |
44 | return true; | |
45 | } | |
46 | return false; | |
47 | } | |
48 | ||
00e2e675 DG |
49 | /* |
50 | * Send command. Fill up the header and append the data. | |
51 | */ | |
6151a90f | 52 | static int send_command(struct lttcomm_relayd_sock *rsock, |
76b9afaa | 53 | enum lttcomm_relayd_command cmd, const void *data, size_t size, |
00e2e675 DG |
54 | int flags) |
55 | { | |
56 | int ret; | |
57 | struct lttcomm_relayd_hdr header; | |
58 | char *buf; | |
59 | uint64_t buf_size = sizeof(header); | |
60 | ||
f96e4545 MD |
61 | if (rsock->sock.fd < 0) { |
62 | return -ECONNRESET; | |
63 | } | |
64 | ||
00e2e675 DG |
65 | if (data) { |
66 | buf_size += size; | |
67 | } | |
68 | ||
69 | buf = zmalloc(buf_size); | |
70 | if (buf == NULL) { | |
71 | PERROR("zmalloc relayd send command buf"); | |
72 | ret = -1; | |
73 | goto alloc_error; | |
74 | } | |
75 | ||
53efb85a | 76 | memset(&header, 0, sizeof(header)); |
00e2e675 DG |
77 | header.cmd = htobe32(cmd); |
78 | header.data_size = htobe64(size); | |
79 | ||
80 | /* Zeroed for now since not used. */ | |
81 | header.cmd_version = 0; | |
82 | header.circuit_id = 0; | |
83 | ||
84 | /* Prepare buffer to send. */ | |
85 | memcpy(buf, &header, sizeof(header)); | |
86 | if (data) { | |
87 | memcpy(buf + sizeof(header), data, size); | |
88 | } | |
89 | ||
06586bbe | 90 | DBG3("Relayd sending command %d of size %" PRIu64, (int) cmd, buf_size); |
6151a90f | 91 | ret = rsock->sock.ops->sendmsg(&rsock->sock, buf, buf_size, flags); |
00e2e675 | 92 | if (ret < 0) { |
06586bbe JG |
93 | PERROR("Failed to send command %d of size %" PRIu64, |
94 | (int) cmd, buf_size); | |
8994307f | 95 | ret = -errno; |
00e2e675 DG |
96 | goto error; |
97 | } | |
00e2e675 DG |
98 | error: |
99 | free(buf); | |
100 | alloc_error: | |
101 | return ret; | |
102 | } | |
103 | ||
104 | /* | |
105 | * Receive reply data on socket. This MUST be call after send_command or else | |
106 | * could result in unexpected behavior(s). | |
107 | */ | |
6151a90f | 108 | static int recv_reply(struct lttcomm_relayd_sock *rsock, void *data, size_t size) |
00e2e675 DG |
109 | { |
110 | int ret; | |
111 | ||
f96e4545 MD |
112 | if (rsock->sock.fd < 0) { |
113 | return -ECONNRESET; | |
114 | } | |
115 | ||
8fd623e0 | 116 | DBG3("Relayd waiting for reply of size %zu", size); |
00e2e675 | 117 | |
6151a90f | 118 | ret = rsock->sock.ops->recvmsg(&rsock->sock, data, size, 0); |
20275fe8 DG |
119 | if (ret <= 0 || ret != size) { |
120 | if (ret == 0) { | |
121 | /* Orderly shutdown. */ | |
6151a90f | 122 | DBG("Socket %d has performed an orderly shutdown", rsock->sock.fd); |
20275fe8 | 123 | } else { |
8fd623e0 | 124 | DBG("Receiving reply failed on sock %d for size %zu with ret %d", |
6151a90f | 125 | rsock->sock.fd, size, ret); |
20275fe8 DG |
126 | } |
127 | /* Always return -1 here and the caller can use errno. */ | |
128 | ret = -1; | |
00e2e675 DG |
129 | goto error; |
130 | } | |
131 | ||
132 | error: | |
133 | return ret; | |
134 | } | |
135 | ||
d3e2ba59 | 136 | /* |
6fa5fe7c MD |
137 | * Starting from 2.11, RELAYD_CREATE_SESSION payload (session_name, |
138 | * hostname, and base_path) have no length restriction on the sender side. | |
f86f6389 JR |
139 | * Length for both payloads is stored in the msg struct. A new dynamic size |
140 | * payload size is introduced. | |
141 | */ | |
142 | static int relayd_create_session_2_11(struct lttcomm_relayd_sock *rsock, | |
fb9a95c4 | 143 | const char *session_name, const char *hostname, |
6fa5fe7c MD |
144 | const char *base_path, int session_live_timer, |
145 | unsigned int snapshot, uint64_t sessiond_session_id, | |
146 | const lttng_uuid sessiond_uuid, const uint64_t *current_chunk_id, | |
ecd1a12f MD |
147 | time_t creation_time, bool session_name_contains_creation_time, |
148 | struct lttcomm_relayd_create_session_reply_2_11 *reply, | |
149 | char *output_path) | |
f86f6389 JR |
150 | { |
151 | int ret; | |
152 | struct lttcomm_relayd_create_session_2_11 *msg = NULL; | |
153 | size_t session_name_len; | |
154 | size_t hostname_len; | |
6fa5fe7c | 155 | size_t base_path_len; |
f86f6389 | 156 | size_t msg_length; |
6fa5fe7c | 157 | char *dst; |
f86f6389 | 158 | |
46ef2188 MD |
159 | if (!base_path) { |
160 | base_path = ""; | |
161 | } | |
162 | /* The three names are sent with a '\0' delimiter between them. */ | |
f86f6389 JR |
163 | session_name_len = strlen(session_name) + 1; |
164 | hostname_len = strlen(hostname) + 1; | |
17e736a5 | 165 | base_path_len = strlen(base_path) + 1; |
f86f6389 | 166 | |
6fa5fe7c | 167 | msg_length = sizeof(*msg) + session_name_len + hostname_len + base_path_len; |
f86f6389 JR |
168 | msg = zmalloc(msg_length); |
169 | if (!msg) { | |
170 | PERROR("zmalloc create_session_2_11 command message"); | |
171 | ret = -1; | |
172 | goto error; | |
173 | } | |
174 | ||
175 | assert(session_name_len <= UINT32_MAX); | |
176 | msg->session_name_len = htobe32(session_name_len); | |
177 | ||
178 | assert(hostname_len <= UINT32_MAX); | |
179 | msg->hostname_len = htobe32(hostname_len); | |
180 | ||
6fa5fe7c MD |
181 | assert(base_path_len <= UINT32_MAX); |
182 | msg->base_path_len = htobe32(base_path_len); | |
183 | ||
184 | dst = msg->names; | |
185 | if (lttng_strncpy(dst, session_name, session_name_len)) { | |
186 | ret = -1; | |
187 | goto error; | |
188 | } | |
189 | dst += session_name_len; | |
190 | if (lttng_strncpy(dst, hostname, hostname_len)) { | |
f86f6389 JR |
191 | ret = -1; |
192 | goto error; | |
193 | } | |
6fa5fe7c | 194 | dst += hostname_len; |
36f9f13b | 195 | if (lttng_strncpy(dst, base_path, base_path_len)) { |
f86f6389 JR |
196 | ret = -1; |
197 | goto error; | |
198 | } | |
199 | ||
200 | msg->live_timer = htobe32(session_live_timer); | |
201 | msg->snapshot = !!snapshot; | |
202 | ||
658f12fa JG |
203 | lttng_uuid_copy(msg->sessiond_uuid, sessiond_uuid); |
204 | msg->session_id = htobe64(sessiond_session_id); | |
46ef2188 | 205 | msg->session_name_contains_creation_time = session_name_contains_creation_time; |
f39bd140 JG |
206 | if (current_chunk_id) { |
207 | LTTNG_OPTIONAL_SET(&msg->current_chunk_id, | |
208 | htobe64(*current_chunk_id)); | |
209 | } | |
210 | ||
db1da059 JG |
211 | msg->creation_time = htobe64((uint64_t) creation_time); |
212 | ||
f86f6389 JR |
213 | /* Send command */ |
214 | ret = send_command(rsock, RELAYD_CREATE_SESSION, msg, msg_length, 0); | |
215 | if (ret < 0) { | |
216 | goto error; | |
217 | } | |
ecd1a12f MD |
218 | /* Receive response */ |
219 | ret = recv_reply(rsock, reply, sizeof(*reply)); | |
220 | if (ret < 0) { | |
221 | goto error; | |
222 | } | |
223 | reply->generic.session_id = be64toh(reply->generic.session_id); | |
224 | reply->generic.ret_code = be32toh(reply->generic.ret_code); | |
225 | reply->output_path_length = be32toh(reply->output_path_length); | |
226 | if (reply->output_path_length >= LTTNG_PATH_MAX) { | |
227 | ERR("Invalid session output path length in reply (%" PRIu32 " bytes) exceeds maximal allowed length (%d bytes)", | |
228 | reply->output_path_length, LTTNG_PATH_MAX); | |
229 | ret = -1; | |
230 | goto error; | |
231 | } | |
232 | ret = recv_reply(rsock, output_path, reply->output_path_length); | |
233 | if (ret < 0) { | |
234 | goto error; | |
235 | } | |
f86f6389 JR |
236 | error: |
237 | free(msg); | |
238 | return ret; | |
239 | } | |
240 | /* | |
241 | * From 2.4 to 2.10, RELAYD_CREATE_SESSION takes additional parameters to | |
d3e2ba59 JD |
242 | * support the live reading capability. |
243 | */ | |
244 | static int relayd_create_session_2_4(struct lttcomm_relayd_sock *rsock, | |
fb9a95c4 | 245 | const char *session_name, const char *hostname, |
ecd1a12f MD |
246 | int session_live_timer, unsigned int snapshot, |
247 | struct lttcomm_relayd_status_session *reply) | |
d3e2ba59 JD |
248 | { |
249 | int ret; | |
250 | struct lttcomm_relayd_create_session_2_4 msg; | |
251 | ||
3a13ffd5 MD |
252 | if (lttng_strncpy(msg.session_name, session_name, |
253 | sizeof(msg.session_name))) { | |
246777db MD |
254 | ret = -1; |
255 | goto error; | |
256 | } | |
3a13ffd5 | 257 | if (lttng_strncpy(msg.hostname, hostname, sizeof(msg.hostname))) { |
246777db MD |
258 | ret = -1; |
259 | goto error; | |
260 | } | |
d3e2ba59 | 261 | msg.live_timer = htobe32(session_live_timer); |
7d2f7452 | 262 | msg.snapshot = htobe32(snapshot); |
d3e2ba59 JD |
263 | |
264 | /* Send command */ | |
265 | ret = send_command(rsock, RELAYD_CREATE_SESSION, &msg, sizeof(msg), 0); | |
266 | if (ret < 0) { | |
267 | goto error; | |
268 | } | |
269 | ||
ecd1a12f MD |
270 | /* Receive response */ |
271 | ret = recv_reply(rsock, reply, sizeof(*reply)); | |
272 | if (ret < 0) { | |
273 | goto error; | |
274 | } | |
275 | reply->session_id = be64toh(reply->session_id); | |
276 | reply->ret_code = be32toh(reply->ret_code); | |
d3e2ba59 JD |
277 | error: |
278 | return ret; | |
279 | } | |
280 | ||
281 | /* | |
282 | * RELAYD_CREATE_SESSION from 2.1 to 2.3. | |
283 | */ | |
ecd1a12f MD |
284 | static int relayd_create_session_2_1(struct lttcomm_relayd_sock *rsock, |
285 | struct lttcomm_relayd_status_session *reply) | |
d3e2ba59 JD |
286 | { |
287 | int ret; | |
288 | ||
289 | /* Send command */ | |
290 | ret = send_command(rsock, RELAYD_CREATE_SESSION, NULL, 0, 0); | |
291 | if (ret < 0) { | |
292 | goto error; | |
293 | } | |
294 | ||
ecd1a12f MD |
295 | /* Receive response */ |
296 | ret = recv_reply(rsock, reply, sizeof(*reply)); | |
297 | if (ret < 0) { | |
298 | goto error; | |
299 | } | |
300 | reply->session_id = be64toh(reply->session_id); | |
301 | reply->ret_code = be32toh(reply->ret_code); | |
d3e2ba59 JD |
302 | error: |
303 | return ret; | |
304 | } | |
305 | ||
c5b6f4f0 DG |
306 | /* |
307 | * Send a RELAYD_CREATE_SESSION command to the relayd with the given socket and | |
308 | * set session_id of the relayd if we have a successful reply from the relayd. | |
309 | * | |
20275fe8 DG |
310 | * On success, return 0 else a negative value which is either an errno error or |
311 | * a lttng error code from the relayd. | |
c5b6f4f0 | 312 | */ |
fb9a95c4 JG |
313 | int relayd_create_session(struct lttcomm_relayd_sock *rsock, |
314 | uint64_t *relayd_session_id, | |
315 | const char *session_name, const char *hostname, | |
6fa5fe7c | 316 | const char *base_path, int session_live_timer, |
658f12fa | 317 | unsigned int snapshot, uint64_t sessiond_session_id, |
f39bd140 | 318 | const lttng_uuid sessiond_uuid, |
db1da059 | 319 | const uint64_t *current_chunk_id, |
ecd1a12f MD |
320 | time_t creation_time, bool session_name_contains_creation_time, |
321 | char *output_path) | |
c5b6f4f0 DG |
322 | { |
323 | int ret; | |
ecd1a12f | 324 | struct lttcomm_relayd_create_session_reply_2_11 reply = {}; |
c5b6f4f0 | 325 | |
6151a90f | 326 | assert(rsock); |
658f12fa | 327 | assert(relayd_session_id); |
c5b6f4f0 DG |
328 | |
329 | DBG("Relayd create session"); | |
330 | ||
f86f6389 JR |
331 | if (rsock->minor < 4) { |
332 | /* From 2.1 to 2.3 */ | |
ecd1a12f | 333 | ret = relayd_create_session_2_1(rsock, &reply.generic); |
f86f6389 JR |
334 | } else if (rsock->minor >= 4 && rsock->minor < 11) { |
335 | /* From 2.4 to 2.10 */ | |
336 | ret = relayd_create_session_2_4(rsock, session_name, | |
ecd1a12f MD |
337 | hostname, session_live_timer, snapshot, |
338 | &reply.generic); | |
f86f6389 JR |
339 | } else { |
340 | /* From 2.11 to ... */ | |
341 | ret = relayd_create_session_2_11(rsock, session_name, | |
6fa5fe7c | 342 | hostname, base_path, session_live_timer, snapshot, |
f39bd140 | 343 | sessiond_session_id, sessiond_uuid, |
46ef2188 | 344 | current_chunk_id, creation_time, |
ecd1a12f MD |
345 | session_name_contains_creation_time, |
346 | &reply, output_path); | |
c5b6f4f0 DG |
347 | } |
348 | ||
c5b6f4f0 DG |
349 | if (ret < 0) { |
350 | goto error; | |
351 | } | |
352 | ||
c5b6f4f0 | 353 | /* Return session id or negative ret code. */ |
ecd1a12f | 354 | if (reply.generic.ret_code != LTTNG_OK) { |
bb63afd9 | 355 | ret = -1; |
ecd1a12f MD |
356 | ERR("Relayd create session replied error %d", |
357 | reply.generic.ret_code); | |
c5b6f4f0 DG |
358 | goto error; |
359 | } else { | |
360 | ret = 0; | |
ecd1a12f | 361 | *relayd_session_id = reply.generic.session_id; |
c5b6f4f0 DG |
362 | } |
363 | ||
ecd1a12f | 364 | DBG("Relayd session created with id %" PRIu64, reply.generic.session_id); |
c5b6f4f0 DG |
365 | |
366 | error: | |
367 | return ret; | |
368 | } | |
369 | ||
2f21a469 JR |
370 | static int relayd_add_stream_2_1(struct lttcomm_relayd_sock *rsock, |
371 | const char *channel_name, const char *pathname) | |
372 | { | |
373 | int ret; | |
374 | struct lttcomm_relayd_add_stream msg; | |
375 | ||
376 | memset(&msg, 0, sizeof(msg)); | |
377 | if (lttng_strncpy(msg.channel_name, channel_name, | |
378 | sizeof(msg.channel_name))) { | |
379 | ret = -1; | |
380 | goto error; | |
381 | } | |
382 | ||
383 | if (lttng_strncpy(msg.pathname, pathname, | |
384 | sizeof(msg.pathname))) { | |
385 | ret = -1; | |
386 | goto error; | |
387 | } | |
388 | ||
389 | /* Send command */ | |
390 | ret = send_command(rsock, RELAYD_ADD_STREAM, (void *) &msg, sizeof(msg), 0); | |
391 | if (ret < 0) { | |
392 | ret = -1; | |
393 | goto error; | |
394 | } | |
395 | ret = 0; | |
396 | error: | |
397 | return ret; | |
398 | } | |
399 | ||
400 | static int relayd_add_stream_2_2(struct lttcomm_relayd_sock *rsock, | |
401 | const char *channel_name, const char *pathname, | |
402 | uint64_t tracefile_size, uint64_t tracefile_count) | |
403 | { | |
404 | int ret; | |
405 | struct lttcomm_relayd_add_stream_2_2 msg; | |
406 | ||
407 | memset(&msg, 0, sizeof(msg)); | |
408 | /* Compat with relayd 2.2 to 2.10 */ | |
409 | if (lttng_strncpy(msg.channel_name, channel_name, | |
410 | sizeof(msg.channel_name))) { | |
411 | ret = -1; | |
412 | goto error; | |
413 | } | |
414 | if (lttng_strncpy(msg.pathname, pathname, | |
415 | sizeof(msg.pathname))) { | |
416 | ret = -1; | |
417 | goto error; | |
418 | } | |
419 | msg.tracefile_size = htobe64(tracefile_size); | |
420 | msg.tracefile_count = htobe64(tracefile_count); | |
421 | ||
422 | /* Send command */ | |
423 | ret = send_command(rsock, RELAYD_ADD_STREAM, (void *) &msg, sizeof(msg), 0); | |
424 | if (ret < 0) { | |
425 | goto error; | |
426 | } | |
427 | ret = 0; | |
428 | error: | |
429 | return ret; | |
430 | } | |
431 | ||
432 | static int relayd_add_stream_2_11(struct lttcomm_relayd_sock *rsock, | |
433 | const char *channel_name, const char *pathname, | |
b00e554e JG |
434 | uint64_t tracefile_size, uint64_t tracefile_count, |
435 | uint64_t trace_archive_id) | |
2f21a469 JR |
436 | { |
437 | int ret; | |
438 | struct lttcomm_relayd_add_stream_2_11 *msg = NULL; | |
439 | size_t channel_name_len; | |
440 | size_t pathname_len; | |
441 | size_t msg_length; | |
442 | ||
443 | /* The two names are sent with a '\0' delimiter between them. */ | |
444 | channel_name_len = strlen(channel_name) + 1; | |
445 | pathname_len = strlen(pathname) + 1; | |
446 | ||
447 | msg_length = sizeof(*msg) + channel_name_len + pathname_len; | |
448 | msg = zmalloc(msg_length); | |
449 | if (!msg) { | |
450 | PERROR("zmalloc add_stream_2_11 command message"); | |
451 | ret = -1; | |
452 | goto error; | |
453 | } | |
454 | ||
455 | assert(channel_name_len <= UINT32_MAX); | |
456 | msg->channel_name_len = htobe32(channel_name_len); | |
457 | ||
458 | assert(pathname_len <= UINT32_MAX); | |
459 | msg->pathname_len = htobe32(pathname_len); | |
460 | ||
461 | if (lttng_strncpy(msg->names, channel_name, channel_name_len)) { | |
462 | ret = -1; | |
463 | goto error; | |
464 | } | |
465 | if (lttng_strncpy(msg->names + channel_name_len, pathname, pathname_len)) { | |
466 | ret = -1; | |
467 | goto error; | |
468 | } | |
469 | ||
470 | msg->tracefile_size = htobe64(tracefile_size); | |
471 | msg->tracefile_count = htobe64(tracefile_count); | |
348a81dc | 472 | msg->trace_chunk_id = htobe64(trace_archive_id); |
2f21a469 JR |
473 | |
474 | /* Send command */ | |
475 | ret = send_command(rsock, RELAYD_ADD_STREAM, (void *) msg, msg_length, 0); | |
476 | if (ret < 0) { | |
477 | goto error; | |
478 | } | |
479 | ret = 0; | |
480 | error: | |
481 | free(msg); | |
482 | return ret; | |
483 | } | |
484 | ||
00e2e675 DG |
485 | /* |
486 | * Add stream on the relayd and assign stream handle to the stream_id argument. | |
487 | * | |
070b6a86 MD |
488 | * Chunks are not supported by relayd prior to 2.11, but are used to |
489 | * internally between session daemon and consumer daemon to keep track | |
490 | * of the channel and stream output path. | |
491 | * | |
00e2e675 DG |
492 | * On success return 0 else return ret_code negative value. |
493 | */ | |
6151a90f | 494 | int relayd_add_stream(struct lttcomm_relayd_sock *rsock, const char *channel_name, |
5da88b0f | 495 | const char *domain_name, const char *_pathname, uint64_t *stream_id, |
0b50e4b3 | 496 | uint64_t tracefile_size, uint64_t tracefile_count, |
d2956687 | 497 | struct lttng_trace_chunk *trace_chunk) |
00e2e675 DG |
498 | { |
499 | int ret; | |
00e2e675 | 500 | struct lttcomm_relayd_status_stream reply; |
5da88b0f | 501 | char pathname[RELAYD_COMM_LTTNG_PATH_MAX]; |
b53d4e59 | 502 | const char *separator; |
00e2e675 DG |
503 | |
504 | /* Code flow error. Safety net. */ | |
6151a90f | 505 | assert(rsock); |
00e2e675 | 506 | assert(channel_name); |
5da88b0f MD |
507 | assert(domain_name); |
508 | assert(_pathname); | |
070b6a86 | 509 | assert(trace_chunk); |
00e2e675 DG |
510 | |
511 | DBG("Relayd adding stream for channel name %s", channel_name); | |
512 | ||
5da88b0f MD |
513 | if (_pathname[0] == '\0') { |
514 | separator = ""; | |
515 | } else { | |
516 | separator = "/"; | |
517 | } | |
518 | ret = snprintf(pathname, RELAYD_COMM_LTTNG_PATH_MAX, "%s%s%s", | |
519 | domain_name, separator, _pathname); | |
520 | if (ret <= 0 || ret >= RELAYD_COMM_LTTNG_PATH_MAX) { | |
521 | ERR("stream path too long."); | |
522 | ret = -1; | |
523 | goto error; | |
524 | } | |
525 | ||
0f907de1 JD |
526 | /* Compat with relayd 2.1 */ |
527 | if (rsock->minor == 1) { | |
2f21a469 JR |
528 | /* For 2.1 */ |
529 | ret = relayd_add_stream_2_1(rsock, channel_name, pathname); | |
530 | ||
531 | } else if (rsock->minor > 1 && rsock->minor < 11) { | |
532 | /* From 2.2 to 2.10 */ | |
533 | ret = relayd_add_stream_2_2(rsock, channel_name, pathname, | |
534 | tracefile_size, tracefile_count); | |
0f907de1 | 535 | } else { |
d2956687 JG |
536 | enum lttng_trace_chunk_status chunk_status; |
537 | uint64_t chunk_id; | |
538 | ||
d2956687 JG |
539 | chunk_status = lttng_trace_chunk_get_id(trace_chunk, |
540 | &chunk_id); | |
541 | assert(chunk_status == LTTNG_TRACE_CHUNK_STATUS_OK); | |
542 | ||
2f21a469 JR |
543 | /* From 2.11 to ...*/ |
544 | ret = relayd_add_stream_2_11(rsock, channel_name, pathname, | |
b00e554e | 545 | tracefile_size, tracefile_count, |
d2956687 | 546 | chunk_id); |
2f21a469 | 547 | } |
0f907de1 | 548 | |
2f21a469 JR |
549 | if (ret) { |
550 | ret = -1; | |
551 | goto error; | |
00e2e675 DG |
552 | } |
553 | ||
633d0084 | 554 | /* Waiting for reply */ |
6151a90f | 555 | ret = recv_reply(rsock, (void *) &reply, sizeof(reply)); |
00e2e675 DG |
556 | if (ret < 0) { |
557 | goto error; | |
558 | } | |
559 | ||
560 | /* Back to host bytes order. */ | |
561 | reply.handle = be64toh(reply.handle); | |
562 | reply.ret_code = be32toh(reply.ret_code); | |
563 | ||
564 | /* Return session id or negative ret code. */ | |
f73fabfd | 565 | if (reply.ret_code != LTTNG_OK) { |
bb63afd9 DG |
566 | ret = -1; |
567 | ERR("Relayd add stream replied error %d", reply.ret_code); | |
00e2e675 DG |
568 | } else { |
569 | /* Success */ | |
570 | ret = 0; | |
571 | *stream_id = reply.handle; | |
572 | } | |
573 | ||
77c7c900 | 574 | DBG("Relayd stream added successfully with handle %" PRIu64, |
2f21a469 | 575 | reply.handle); |
00e2e675 DG |
576 | |
577 | error: | |
578 | return ret; | |
579 | } | |
580 | ||
a4baae1b JD |
581 | /* |
582 | * Inform the relay that all the streams for the current channel has been sent. | |
583 | * | |
584 | * On success return 0 else return ret_code negative value. | |
585 | */ | |
586 | int relayd_streams_sent(struct lttcomm_relayd_sock *rsock) | |
587 | { | |
588 | int ret; | |
589 | struct lttcomm_relayd_generic_reply reply; | |
590 | ||
591 | /* Code flow error. Safety net. */ | |
592 | assert(rsock); | |
593 | ||
594 | DBG("Relayd sending streams sent."); | |
595 | ||
596 | /* This feature was introduced in 2.4, ignore it for earlier versions. */ | |
597 | if (rsock->minor < 4) { | |
598 | ret = 0; | |
599 | goto end; | |
600 | } | |
601 | ||
602 | /* Send command */ | |
603 | ret = send_command(rsock, RELAYD_STREAMS_SENT, NULL, 0, 0); | |
604 | if (ret < 0) { | |
605 | goto error; | |
606 | } | |
607 | ||
608 | /* Waiting for reply */ | |
609 | ret = recv_reply(rsock, (void *) &reply, sizeof(reply)); | |
610 | if (ret < 0) { | |
611 | goto error; | |
612 | } | |
613 | ||
614 | /* Back to host bytes order. */ | |
615 | reply.ret_code = be32toh(reply.ret_code); | |
616 | ||
617 | /* Return session id or negative ret code. */ | |
618 | if (reply.ret_code != LTTNG_OK) { | |
619 | ret = -1; | |
620 | ERR("Relayd streams sent replied error %d", reply.ret_code); | |
621 | goto error; | |
622 | } else { | |
623 | /* Success */ | |
624 | ret = 0; | |
625 | } | |
626 | ||
627 | DBG("Relayd streams sent success"); | |
628 | ||
629 | error: | |
630 | end: | |
631 | return ret; | |
632 | } | |
633 | ||
00e2e675 DG |
634 | /* |
635 | * Check version numbers on the relayd. | |
d4519fa3 JD |
636 | * If major versions are compatible, we assign minor_to_use to the |
637 | * minor version of the procotol we are going to use for this session. | |
00e2e675 | 638 | * |
67d5aa28 JD |
639 | * Return 0 if the two daemons are compatible, LTTNG_ERR_RELAYD_VERSION_FAIL |
640 | * otherwise, or a negative value on network errors. | |
00e2e675 | 641 | */ |
6151a90f | 642 | int relayd_version_check(struct lttcomm_relayd_sock *rsock) |
00e2e675 DG |
643 | { |
644 | int ret; | |
092b6259 | 645 | struct lttcomm_relayd_version msg; |
00e2e675 DG |
646 | |
647 | /* Code flow error. Safety net. */ | |
6151a90f | 648 | assert(rsock); |
00e2e675 | 649 | |
6151a90f JD |
650 | DBG("Relayd version check for major.minor %u.%u", rsock->major, |
651 | rsock->minor); | |
00e2e675 | 652 | |
53efb85a | 653 | memset(&msg, 0, sizeof(msg)); |
092b6259 | 654 | /* Prepare network byte order before transmission. */ |
6151a90f JD |
655 | msg.major = htobe32(rsock->major); |
656 | msg.minor = htobe32(rsock->minor); | |
092b6259 | 657 | |
00e2e675 | 658 | /* Send command */ |
6151a90f | 659 | ret = send_command(rsock, RELAYD_VERSION, (void *) &msg, sizeof(msg), 0); |
00e2e675 DG |
660 | if (ret < 0) { |
661 | goto error; | |
662 | } | |
663 | ||
20275fe8 | 664 | /* Receive response */ |
6151a90f | 665 | ret = recv_reply(rsock, (void *) &msg, sizeof(msg)); |
00e2e675 DG |
666 | if (ret < 0) { |
667 | goto error; | |
668 | } | |
669 | ||
670 | /* Set back to host bytes order */ | |
092b6259 DG |
671 | msg.major = be32toh(msg.major); |
672 | msg.minor = be32toh(msg.minor); | |
673 | ||
674 | /* | |
675 | * Only validate the major version. If the other side is higher, | |
676 | * communication is not possible. Only major version equal can talk to each | |
677 | * other. If the minor version differs, the lowest version is used by both | |
678 | * sides. | |
092b6259 | 679 | */ |
6151a90f | 680 | if (msg.major != rsock->major) { |
d4519fa3 | 681 | /* Not compatible */ |
67d5aa28 | 682 | ret = LTTNG_ERR_RELAYD_VERSION_FAIL; |
d4519fa3 | 683 | DBG2("Relayd version is NOT compatible. Relayd version %u != %u (us)", |
6151a90f | 684 | msg.major, rsock->major); |
092b6259 | 685 | goto error; |
00e2e675 DG |
686 | } |
687 | ||
092b6259 | 688 | /* |
6151a90f JD |
689 | * If the relayd's minor version is higher, it will adapt to our version so |
690 | * we can continue to use the latest relayd communication data structure. | |
691 | * If the received minor version is higher, the relayd should adapt to us. | |
092b6259 | 692 | */ |
6151a90f JD |
693 | if (rsock->minor > msg.minor) { |
694 | rsock->minor = msg.minor; | |
d4519fa3 | 695 | } |
092b6259 | 696 | |
d4519fa3 JD |
697 | /* Version number compatible */ |
698 | DBG2("Relayd version is compatible, using protocol version %u.%u", | |
6151a90f | 699 | rsock->major, rsock->minor); |
d4519fa3 | 700 | ret = 0; |
00e2e675 DG |
701 | |
702 | error: | |
703 | return ret; | |
704 | } | |
705 | ||
00e2e675 DG |
706 | /* |
707 | * Add stream on the relayd and assign stream handle to the stream_id argument. | |
708 | * | |
709 | * On success return 0 else return ret_code negative value. | |
710 | */ | |
6151a90f | 711 | int relayd_send_metadata(struct lttcomm_relayd_sock *rsock, size_t len) |
00e2e675 DG |
712 | { |
713 | int ret; | |
714 | ||
715 | /* Code flow error. Safety net. */ | |
6151a90f | 716 | assert(rsock); |
00e2e675 | 717 | |
77c7c900 | 718 | DBG("Relayd sending metadata of size %zu", len); |
00e2e675 DG |
719 | |
720 | /* Send command */ | |
6151a90f | 721 | ret = send_command(rsock, RELAYD_SEND_METADATA, NULL, len, 0); |
00e2e675 DG |
722 | if (ret < 0) { |
723 | goto error; | |
724 | } | |
725 | ||
726 | DBG2("Relayd metadata added successfully"); | |
727 | ||
728 | /* | |
729 | * After that call, the metadata data MUST be sent to the relayd so the | |
730 | * receive size on the other end matches the len of the metadata packet | |
633d0084 | 731 | * header. This is why we don't wait for a reply here. |
00e2e675 DG |
732 | */ |
733 | ||
734 | error: | |
735 | return ret; | |
736 | } | |
737 | ||
738 | /* | |
6151a90f | 739 | * Connect to relay daemon with an allocated lttcomm_relayd_sock. |
00e2e675 | 740 | */ |
6151a90f | 741 | int relayd_connect(struct lttcomm_relayd_sock *rsock) |
00e2e675 DG |
742 | { |
743 | /* Code flow error. Safety net. */ | |
6151a90f | 744 | assert(rsock); |
00e2e675 | 745 | |
f96e4545 MD |
746 | if (!rsock->sock.ops) { |
747 | /* | |
748 | * Attempting a connect on a non-initialized socket. | |
749 | */ | |
750 | return -ECONNRESET; | |
751 | } | |
752 | ||
00e2e675 DG |
753 | DBG3("Relayd connect ..."); |
754 | ||
6151a90f | 755 | return rsock->sock.ops->connect(&rsock->sock); |
00e2e675 DG |
756 | } |
757 | ||
758 | /* | |
6151a90f | 759 | * Close relayd socket with an allocated lttcomm_relayd_sock. |
ffe60014 DG |
760 | * |
761 | * If no socket operations are found, simply return 0 meaning that everything | |
762 | * is fine. Without operations, the socket can not possibly be opened or used. | |
763 | * This is possible if the socket was allocated but not created. However, the | |
764 | * caller could simply use it to store a valid file descriptor for instance | |
765 | * passed over a Unix socket and call this to cleanup but still without a valid | |
766 | * ops pointer. | |
767 | * | |
768 | * Return the close returned value. On error, a negative value is usually | |
769 | * returned back from close(2). | |
00e2e675 | 770 | */ |
6151a90f | 771 | int relayd_close(struct lttcomm_relayd_sock *rsock) |
00e2e675 | 772 | { |
ffe60014 DG |
773 | int ret; |
774 | ||
00e2e675 | 775 | /* Code flow error. Safety net. */ |
6151a90f | 776 | assert(rsock); |
00e2e675 | 777 | |
ffe60014 | 778 | /* An invalid fd is fine, return success. */ |
6151a90f | 779 | if (rsock->sock.fd < 0) { |
ffe60014 DG |
780 | ret = 0; |
781 | goto end; | |
782 | } | |
783 | ||
6151a90f | 784 | DBG3("Relayd closing socket %d", rsock->sock.fd); |
00e2e675 | 785 | |
6151a90f JD |
786 | if (rsock->sock.ops) { |
787 | ret = rsock->sock.ops->close(&rsock->sock); | |
ffe60014 DG |
788 | } else { |
789 | /* Default call if no specific ops found. */ | |
6151a90f | 790 | ret = close(rsock->sock.fd); |
ffe60014 DG |
791 | if (ret < 0) { |
792 | PERROR("relayd_close default close"); | |
793 | } | |
794 | } | |
f96e4545 | 795 | rsock->sock.fd = -1; |
ffe60014 DG |
796 | |
797 | end: | |
798 | return ret; | |
00e2e675 DG |
799 | } |
800 | ||
801 | /* | |
802 | * Send data header structure to the relayd. | |
803 | */ | |
6151a90f | 804 | int relayd_send_data_hdr(struct lttcomm_relayd_sock *rsock, |
00e2e675 DG |
805 | struct lttcomm_relayd_data_hdr *hdr, size_t size) |
806 | { | |
807 | int ret; | |
808 | ||
809 | /* Code flow error. Safety net. */ | |
6151a90f | 810 | assert(rsock); |
00e2e675 DG |
811 | assert(hdr); |
812 | ||
f96e4545 MD |
813 | if (rsock->sock.fd < 0) { |
814 | return -ECONNRESET; | |
815 | } | |
816 | ||
8fd623e0 | 817 | DBG3("Relayd sending data header of size %zu", size); |
00e2e675 DG |
818 | |
819 | /* Again, safety net */ | |
820 | if (size == 0) { | |
821 | size = sizeof(struct lttcomm_relayd_data_hdr); | |
822 | } | |
823 | ||
824 | /* Only send data header. */ | |
6151a90f | 825 | ret = rsock->sock.ops->sendmsg(&rsock->sock, hdr, size, 0); |
00e2e675 | 826 | if (ret < 0) { |
8994307f | 827 | ret = -errno; |
00e2e675 DG |
828 | goto error; |
829 | } | |
830 | ||
831 | /* | |
832 | * The data MUST be sent right after that command for the receive on the | |
833 | * other end to match the size in the header. | |
834 | */ | |
835 | ||
836 | error: | |
837 | return ret; | |
838 | } | |
173af62f DG |
839 | |
840 | /* | |
841 | * Send close stream command to the relayd. | |
842 | */ | |
6151a90f | 843 | int relayd_send_close_stream(struct lttcomm_relayd_sock *rsock, uint64_t stream_id, |
173af62f DG |
844 | uint64_t last_net_seq_num) |
845 | { | |
846 | int ret; | |
847 | struct lttcomm_relayd_close_stream msg; | |
848 | struct lttcomm_relayd_generic_reply reply; | |
849 | ||
850 | /* Code flow error. Safety net. */ | |
6151a90f | 851 | assert(rsock); |
173af62f | 852 | |
77c7c900 | 853 | DBG("Relayd closing stream id %" PRIu64, stream_id); |
173af62f | 854 | |
53efb85a | 855 | memset(&msg, 0, sizeof(msg)); |
173af62f DG |
856 | msg.stream_id = htobe64(stream_id); |
857 | msg.last_net_seq_num = htobe64(last_net_seq_num); | |
858 | ||
859 | /* Send command */ | |
6151a90f | 860 | ret = send_command(rsock, RELAYD_CLOSE_STREAM, (void *) &msg, sizeof(msg), 0); |
173af62f DG |
861 | if (ret < 0) { |
862 | goto error; | |
863 | } | |
864 | ||
20275fe8 | 865 | /* Receive response */ |
6151a90f | 866 | ret = recv_reply(rsock, (void *) &reply, sizeof(reply)); |
173af62f DG |
867 | if (ret < 0) { |
868 | goto error; | |
869 | } | |
870 | ||
871 | reply.ret_code = be32toh(reply.ret_code); | |
872 | ||
873 | /* Return session id or negative ret code. */ | |
f73fabfd | 874 | if (reply.ret_code != LTTNG_OK) { |
bb63afd9 DG |
875 | ret = -1; |
876 | ERR("Relayd close stream replied error %d", reply.ret_code); | |
173af62f DG |
877 | } else { |
878 | /* Success */ | |
879 | ret = 0; | |
880 | } | |
881 | ||
77c7c900 | 882 | DBG("Relayd close stream id %" PRIu64 " successfully", stream_id); |
173af62f DG |
883 | |
884 | error: | |
885 | return ret; | |
886 | } | |
c8f59ee5 DG |
887 | |
888 | /* | |
889 | * Check for data availability for a given stream id. | |
890 | * | |
6d805429 | 891 | * Return 0 if NOT pending, 1 if so and a negative value on error. |
c8f59ee5 | 892 | */ |
6151a90f | 893 | int relayd_data_pending(struct lttcomm_relayd_sock *rsock, uint64_t stream_id, |
c8f59ee5 DG |
894 | uint64_t last_net_seq_num) |
895 | { | |
896 | int ret; | |
6d805429 | 897 | struct lttcomm_relayd_data_pending msg; |
c8f59ee5 DG |
898 | struct lttcomm_relayd_generic_reply reply; |
899 | ||
900 | /* Code flow error. Safety net. */ | |
6151a90f | 901 | assert(rsock); |
c8f59ee5 | 902 | |
6d805429 | 903 | DBG("Relayd data pending for stream id %" PRIu64, stream_id); |
c8f59ee5 | 904 | |
53efb85a | 905 | memset(&msg, 0, sizeof(msg)); |
c8f59ee5 DG |
906 | msg.stream_id = htobe64(stream_id); |
907 | msg.last_net_seq_num = htobe64(last_net_seq_num); | |
908 | ||
909 | /* Send command */ | |
6151a90f | 910 | ret = send_command(rsock, RELAYD_DATA_PENDING, (void *) &msg, |
c8f59ee5 DG |
911 | sizeof(msg), 0); |
912 | if (ret < 0) { | |
913 | goto error; | |
914 | } | |
915 | ||
20275fe8 | 916 | /* Receive response */ |
6151a90f | 917 | ret = recv_reply(rsock, (void *) &reply, sizeof(reply)); |
c8f59ee5 DG |
918 | if (ret < 0) { |
919 | goto error; | |
920 | } | |
921 | ||
922 | reply.ret_code = be32toh(reply.ret_code); | |
923 | ||
924 | /* Return session id or negative ret code. */ | |
925 | if (reply.ret_code >= LTTNG_OK) { | |
bb63afd9 | 926 | ERR("Relayd data pending replied error %d", reply.ret_code); |
c8f59ee5 DG |
927 | } |
928 | ||
929 | /* At this point, the ret code is either 1 or 0 */ | |
930 | ret = reply.ret_code; | |
931 | ||
6d805429 | 932 | DBG("Relayd data is %s pending for stream id %" PRIu64, |
9dd26bb9 | 933 | ret == 1 ? "" : "NOT", stream_id); |
c8f59ee5 DG |
934 | |
935 | error: | |
936 | return ret; | |
937 | } | |
938 | ||
939 | /* | |
940 | * Check on the relayd side for a quiescent state on the control socket. | |
941 | */ | |
6151a90f | 942 | int relayd_quiescent_control(struct lttcomm_relayd_sock *rsock, |
ad7051c0 | 943 | uint64_t metadata_stream_id) |
c8f59ee5 DG |
944 | { |
945 | int ret; | |
ad7051c0 | 946 | struct lttcomm_relayd_quiescent_control msg; |
c8f59ee5 DG |
947 | struct lttcomm_relayd_generic_reply reply; |
948 | ||
949 | /* Code flow error. Safety net. */ | |
6151a90f | 950 | assert(rsock); |
c8f59ee5 DG |
951 | |
952 | DBG("Relayd checking quiescent control state"); | |
953 | ||
53efb85a | 954 | memset(&msg, 0, sizeof(msg)); |
ad7051c0 DG |
955 | msg.stream_id = htobe64(metadata_stream_id); |
956 | ||
c8f59ee5 | 957 | /* Send command */ |
6151a90f | 958 | ret = send_command(rsock, RELAYD_QUIESCENT_CONTROL, &msg, sizeof(msg), 0); |
c8f59ee5 DG |
959 | if (ret < 0) { |
960 | goto error; | |
961 | } | |
962 | ||
20275fe8 | 963 | /* Receive response */ |
6151a90f | 964 | ret = recv_reply(rsock, (void *) &reply, sizeof(reply)); |
c8f59ee5 DG |
965 | if (ret < 0) { |
966 | goto error; | |
967 | } | |
968 | ||
969 | reply.ret_code = be32toh(reply.ret_code); | |
970 | ||
971 | /* Return session id or negative ret code. */ | |
972 | if (reply.ret_code != LTTNG_OK) { | |
bb63afd9 DG |
973 | ret = -1; |
974 | ERR("Relayd quiescent control replied error %d", reply.ret_code); | |
c8f59ee5 DG |
975 | goto error; |
976 | } | |
977 | ||
978 | /* Control socket is quiescent */ | |
6d805429 | 979 | return 0; |
c8f59ee5 DG |
980 | |
981 | error: | |
982 | return ret; | |
983 | } | |
f7079f67 DG |
984 | |
985 | /* | |
986 | * Begin a data pending command for a specific session id. | |
987 | */ | |
6151a90f | 988 | int relayd_begin_data_pending(struct lttcomm_relayd_sock *rsock, uint64_t id) |
f7079f67 DG |
989 | { |
990 | int ret; | |
991 | struct lttcomm_relayd_begin_data_pending msg; | |
992 | struct lttcomm_relayd_generic_reply reply; | |
993 | ||
994 | /* Code flow error. Safety net. */ | |
6151a90f | 995 | assert(rsock); |
f7079f67 DG |
996 | |
997 | DBG("Relayd begin data pending"); | |
998 | ||
53efb85a | 999 | memset(&msg, 0, sizeof(msg)); |
f7079f67 DG |
1000 | msg.session_id = htobe64(id); |
1001 | ||
1002 | /* Send command */ | |
6151a90f | 1003 | ret = send_command(rsock, RELAYD_BEGIN_DATA_PENDING, &msg, sizeof(msg), 0); |
f7079f67 DG |
1004 | if (ret < 0) { |
1005 | goto error; | |
1006 | } | |
1007 | ||
20275fe8 | 1008 | /* Receive response */ |
6151a90f | 1009 | ret = recv_reply(rsock, (void *) &reply, sizeof(reply)); |
f7079f67 DG |
1010 | if (ret < 0) { |
1011 | goto error; | |
1012 | } | |
1013 | ||
1014 | reply.ret_code = be32toh(reply.ret_code); | |
1015 | ||
1016 | /* Return session id or negative ret code. */ | |
1017 | if (reply.ret_code != LTTNG_OK) { | |
bb63afd9 DG |
1018 | ret = -1; |
1019 | ERR("Relayd begin data pending replied error %d", reply.ret_code); | |
f7079f67 DG |
1020 | goto error; |
1021 | } | |
1022 | ||
1023 | return 0; | |
1024 | ||
1025 | error: | |
1026 | return ret; | |
1027 | } | |
1028 | ||
1029 | /* | |
1030 | * End a data pending command for a specific session id. | |
1031 | * | |
1032 | * Return 0 on success and set is_data_inflight to 0 if no data is being | |
1033 | * streamed or 1 if it is the case. | |
1034 | */ | |
6151a90f | 1035 | int relayd_end_data_pending(struct lttcomm_relayd_sock *rsock, uint64_t id, |
f7079f67 DG |
1036 | unsigned int *is_data_inflight) |
1037 | { | |
af6c30b5 | 1038 | int ret, recv_ret; |
f7079f67 DG |
1039 | struct lttcomm_relayd_end_data_pending msg; |
1040 | struct lttcomm_relayd_generic_reply reply; | |
1041 | ||
1042 | /* Code flow error. Safety net. */ | |
6151a90f | 1043 | assert(rsock); |
f7079f67 DG |
1044 | |
1045 | DBG("Relayd end data pending"); | |
1046 | ||
53efb85a | 1047 | memset(&msg, 0, sizeof(msg)); |
f7079f67 DG |
1048 | msg.session_id = htobe64(id); |
1049 | ||
1050 | /* Send command */ | |
6151a90f | 1051 | ret = send_command(rsock, RELAYD_END_DATA_PENDING, &msg, sizeof(msg), 0); |
f7079f67 DG |
1052 | if (ret < 0) { |
1053 | goto error; | |
1054 | } | |
1055 | ||
20275fe8 | 1056 | /* Receive response */ |
6151a90f | 1057 | ret = recv_reply(rsock, (void *) &reply, sizeof(reply)); |
f7079f67 DG |
1058 | if (ret < 0) { |
1059 | goto error; | |
1060 | } | |
1061 | ||
af6c30b5 DG |
1062 | recv_ret = be32toh(reply.ret_code); |
1063 | if (recv_ret < 0) { | |
1064 | ret = recv_ret; | |
f7079f67 DG |
1065 | goto error; |
1066 | } | |
1067 | ||
af6c30b5 | 1068 | *is_data_inflight = recv_ret; |
f7079f67 | 1069 | |
af6c30b5 | 1070 | DBG("Relayd end data pending is data inflight: %d", recv_ret); |
f7079f67 DG |
1071 | |
1072 | return 0; | |
1073 | ||
1074 | error: | |
1075 | return ret; | |
1076 | } | |
1c20f0e2 JD |
1077 | |
1078 | /* | |
1079 | * Send index to the relayd. | |
1080 | */ | |
1081 | int relayd_send_index(struct lttcomm_relayd_sock *rsock, | |
50adc264 | 1082 | struct ctf_packet_index *index, uint64_t relay_stream_id, |
1c20f0e2 JD |
1083 | uint64_t net_seq_num) |
1084 | { | |
1085 | int ret; | |
1086 | struct lttcomm_relayd_index msg; | |
1087 | struct lttcomm_relayd_generic_reply reply; | |
1088 | ||
1089 | /* Code flow error. Safety net. */ | |
1090 | assert(rsock); | |
1091 | ||
1092 | if (rsock->minor < 4) { | |
1093 | DBG("Not sending indexes before protocol 2.4"); | |
1094 | ret = 0; | |
1095 | goto error; | |
1096 | } | |
1097 | ||
1098 | DBG("Relayd sending index for stream ID %" PRIu64, relay_stream_id); | |
1099 | ||
53efb85a | 1100 | memset(&msg, 0, sizeof(msg)); |
1c20f0e2 JD |
1101 | msg.relay_stream_id = htobe64(relay_stream_id); |
1102 | msg.net_seq_num = htobe64(net_seq_num); | |
1103 | ||
1104 | /* The index is already in big endian. */ | |
1105 | msg.packet_size = index->packet_size; | |
1106 | msg.content_size = index->content_size; | |
1107 | msg.timestamp_begin = index->timestamp_begin; | |
1108 | msg.timestamp_end = index->timestamp_end; | |
1109 | msg.events_discarded = index->events_discarded; | |
1110 | msg.stream_id = index->stream_id; | |
1111 | ||
234cd636 JD |
1112 | if (rsock->minor >= 8) { |
1113 | msg.stream_instance_id = index->stream_instance_id; | |
1114 | msg.packet_seq_num = index->packet_seq_num; | |
1115 | } | |
1116 | ||
1c20f0e2 | 1117 | /* Send command */ |
f8f3885c MD |
1118 | ret = send_command(rsock, RELAYD_SEND_INDEX, &msg, |
1119 | lttcomm_relayd_index_len(lttng_to_index_major(rsock->major, | |
1120 | rsock->minor), | |
1121 | lttng_to_index_minor(rsock->major, rsock->minor)), | |
1122 | 0); | |
1c20f0e2 JD |
1123 | if (ret < 0) { |
1124 | goto error; | |
1125 | } | |
1126 | ||
1127 | /* Receive response */ | |
1128 | ret = recv_reply(rsock, (void *) &reply, sizeof(reply)); | |
1129 | if (ret < 0) { | |
1130 | goto error; | |
1131 | } | |
1132 | ||
1133 | reply.ret_code = be32toh(reply.ret_code); | |
1134 | ||
1135 | /* Return session id or negative ret code. */ | |
1136 | if (reply.ret_code != LTTNG_OK) { | |
1137 | ret = -1; | |
1138 | ERR("Relayd send index replied error %d", reply.ret_code); | |
1139 | } else { | |
1140 | /* Success */ | |
1141 | ret = 0; | |
1142 | } | |
1143 | ||
1144 | error: | |
1145 | return ret; | |
1146 | } | |
93ec662e JD |
1147 | |
1148 | /* | |
1149 | * Ask the relay to reset the metadata trace file (regeneration). | |
1150 | */ | |
1151 | int relayd_reset_metadata(struct lttcomm_relayd_sock *rsock, | |
1152 | uint64_t stream_id, uint64_t version) | |
1153 | { | |
1154 | int ret; | |
1155 | struct lttcomm_relayd_reset_metadata msg; | |
1156 | struct lttcomm_relayd_generic_reply reply; | |
1157 | ||
1158 | /* Code flow error. Safety net. */ | |
1159 | assert(rsock); | |
1160 | ||
1161 | /* Should have been prevented by the sessiond. */ | |
1162 | if (rsock->minor < 8) { | |
1163 | ERR("Metadata regeneration unsupported before 2.8"); | |
1164 | ret = -1; | |
1165 | goto error; | |
1166 | } | |
1167 | ||
1168 | DBG("Relayd reset metadata stream id %" PRIu64, stream_id); | |
1169 | ||
1170 | memset(&msg, 0, sizeof(msg)); | |
1171 | msg.stream_id = htobe64(stream_id); | |
1172 | msg.version = htobe64(version); | |
1173 | ||
1174 | /* Send command */ | |
1175 | ret = send_command(rsock, RELAYD_RESET_METADATA, (void *) &msg, sizeof(msg), 0); | |
1176 | if (ret < 0) { | |
1177 | goto error; | |
1178 | } | |
1179 | ||
1180 | /* Receive response */ | |
1181 | ret = recv_reply(rsock, (void *) &reply, sizeof(reply)); | |
1182 | if (ret < 0) { | |
1183 | goto error; | |
1184 | } | |
1185 | ||
1186 | reply.ret_code = be32toh(reply.ret_code); | |
1187 | ||
1188 | /* Return session id or negative ret code. */ | |
1189 | if (reply.ret_code != LTTNG_OK) { | |
1190 | ret = -1; | |
1191 | ERR("Relayd reset metadata replied error %d", reply.ret_code); | |
1192 | } else { | |
1193 | /* Success */ | |
1194 | ret = 0; | |
1195 | } | |
1196 | ||
1197 | DBG("Relayd reset metadata stream id %" PRIu64 " successfully", stream_id); | |
1198 | ||
1199 | error: | |
1200 | return ret; | |
1201 | } | |
a1ae2ea5 | 1202 | |
c35f9726 | 1203 | int relayd_rotate_streams(struct lttcomm_relayd_sock *sock, |
ebb29c10 | 1204 | unsigned int stream_count, const uint64_t *new_chunk_id, |
c35f9726 | 1205 | const struct relayd_stream_rotation_position *positions) |
d73bf3d7 JD |
1206 | { |
1207 | int ret; | |
c35f9726 JG |
1208 | unsigned int i; |
1209 | struct lttng_dynamic_buffer payload; | |
1210 | struct lttcomm_relayd_generic_reply reply = {}; | |
1211 | const struct lttcomm_relayd_rotate_streams msg = { | |
1212 | .stream_count = htobe32((uint32_t) stream_count), | |
1213 | .new_chunk_id = (typeof(msg.new_chunk_id)) { | |
1214 | .is_set = !!new_chunk_id, | |
1215 | .value = htobe64(new_chunk_id ? *new_chunk_id : 0), | |
1216 | }, | |
1217 | }; | |
1218 | char new_chunk_id_buf[MAX_INT_DEC_LEN(*new_chunk_id)] = {}; | |
1219 | const char *new_chunk_id_str; | |
d73bf3d7 | 1220 | |
070b6a86 MD |
1221 | if (!relayd_supports_chunks(sock)) { |
1222 | DBG("Refusing to rotate remote streams: relayd does not support chunks"); | |
1223 | return 0; | |
1224 | } | |
1225 | ||
c35f9726 | 1226 | lttng_dynamic_buffer_init(&payload); |
d73bf3d7 | 1227 | |
c35f9726 JG |
1228 | /* Code flow error. Safety net. */ |
1229 | assert(sock); | |
d73bf3d7 | 1230 | |
c35f9726 JG |
1231 | if (new_chunk_id) { |
1232 | ret = snprintf(new_chunk_id_buf, sizeof(new_chunk_id_buf), | |
1233 | "%" PRIu64, *new_chunk_id); | |
1234 | if (ret == -1 || ret >= sizeof(new_chunk_id_buf)) { | |
1235 | new_chunk_id_str = "formatting error"; | |
1236 | } else { | |
1237 | new_chunk_id_str = new_chunk_id_buf; | |
1238 | } | |
1239 | } else { | |
1240 | new_chunk_id_str = "none"; | |
d73bf3d7 JD |
1241 | } |
1242 | ||
c35f9726 JG |
1243 | DBG("Preparing \"rotate streams\" command payload: new_chunk_id = %s, stream_count = %u", |
1244 | new_chunk_id_str, stream_count); | |
d73bf3d7 | 1245 | |
c35f9726 JG |
1246 | ret = lttng_dynamic_buffer_append(&payload, &msg, sizeof(msg)); |
1247 | if (ret) { | |
1248 | ERR("Failed to allocate \"rotate streams\" command payload"); | |
d73bf3d7 JD |
1249 | goto error; |
1250 | } | |
1251 | ||
c35f9726 JG |
1252 | for (i = 0; i < stream_count; i++) { |
1253 | const struct relayd_stream_rotation_position *position = | |
1254 | &positions[i]; | |
1255 | const struct lttcomm_relayd_stream_rotation_position comm_position = { | |
1256 | .stream_id = htobe64(position->stream_id), | |
1257 | .rotate_at_seq_num = htobe64( | |
1258 | position->rotate_at_seq_num), | |
1259 | }; | |
1260 | ||
1261 | DBG("Rotate stream %" PRIu64 "at sequence number %" PRIu64, | |
1262 | position->stream_id, | |
1263 | position->rotate_at_seq_num); | |
1264 | ret = lttng_dynamic_buffer_append(&payload, &comm_position, | |
1265 | sizeof(comm_position)); | |
1266 | if (ret) { | |
1267 | ERR("Failed to allocate \"rotate streams\" command payload"); | |
1268 | goto error; | |
1269 | } | |
1270 | } | |
d73bf3d7 JD |
1271 | |
1272 | /* Send command. */ | |
c35f9726 JG |
1273 | ret = send_command(sock, RELAYD_ROTATE_STREAMS, payload.data, |
1274 | payload.size, 0); | |
d73bf3d7 | 1275 | if (ret < 0) { |
c35f9726 | 1276 | ERR("Failed to send \"rotate stream\" command"); |
d73bf3d7 JD |
1277 | goto error; |
1278 | } | |
1279 | ||
1280 | /* Receive response. */ | |
c35f9726 | 1281 | ret = recv_reply(sock, &reply, sizeof(reply)); |
d73bf3d7 | 1282 | if (ret < 0) { |
c35f9726 | 1283 | ERR("Failed to receive \"rotate streams\" command reply"); |
d73bf3d7 JD |
1284 | goto error; |
1285 | } | |
1286 | ||
1287 | reply.ret_code = be32toh(reply.ret_code); | |
d73bf3d7 JD |
1288 | if (reply.ret_code != LTTNG_OK) { |
1289 | ret = -1; | |
c35f9726 | 1290 | ERR("Relayd rotate streams replied error %d", reply.ret_code); |
d73bf3d7 JD |
1291 | } else { |
1292 | /* Success. */ | |
1293 | ret = 0; | |
c35f9726 | 1294 | DBG("Relayd rotated streams successfully"); |
d73bf3d7 JD |
1295 | } |
1296 | ||
1297 | error: | |
c35f9726 | 1298 | lttng_dynamic_buffer_reset(&payload); |
d73bf3d7 JD |
1299 | return ret; |
1300 | } | |
e5add6d0 JG |
1301 | |
1302 | int relayd_create_trace_chunk(struct lttcomm_relayd_sock *sock, | |
1303 | struct lttng_trace_chunk *chunk) | |
1304 | { | |
1305 | int ret = 0; | |
1306 | enum lttng_trace_chunk_status status; | |
1307 | struct lttcomm_relayd_create_trace_chunk msg = {}; | |
1308 | struct lttcomm_relayd_generic_reply reply = {}; | |
1309 | struct lttng_dynamic_buffer payload; | |
1310 | uint64_t chunk_id; | |
1311 | time_t creation_timestamp; | |
1312 | const char *chunk_name; | |
1313 | size_t chunk_name_length; | |
913a542b | 1314 | bool overridden_name; |
e5add6d0 JG |
1315 | |
1316 | lttng_dynamic_buffer_init(&payload); | |
1317 | ||
070b6a86 MD |
1318 | if (!relayd_supports_chunks(sock)) { |
1319 | DBG("Refusing to create remote trace chunk: relayd does not support chunks"); | |
1320 | goto end; | |
1321 | } | |
1322 | ||
e5add6d0 JG |
1323 | status = lttng_trace_chunk_get_id(chunk, &chunk_id); |
1324 | if (status != LTTNG_TRACE_CHUNK_STATUS_OK) { | |
1325 | ret = -1; | |
1326 | goto end; | |
1327 | } | |
1328 | ||
1329 | status = lttng_trace_chunk_get_creation_timestamp( | |
1330 | chunk, &creation_timestamp); | |
1331 | if (status != LTTNG_TRACE_CHUNK_STATUS_OK) { | |
1332 | ret = -1; | |
1333 | goto end; | |
1334 | } | |
1335 | ||
1336 | status = lttng_trace_chunk_get_name( | |
913a542b | 1337 | chunk, &chunk_name, &overridden_name); |
e5add6d0 JG |
1338 | if (status != LTTNG_TRACE_CHUNK_STATUS_OK && |
1339 | status != LTTNG_TRACE_CHUNK_STATUS_NONE) { | |
1340 | ret = -1; | |
1341 | goto end; | |
1342 | } | |
1343 | ||
913a542b | 1344 | chunk_name_length = overridden_name ? (strlen(chunk_name) + 1) : 0; |
e5add6d0 JG |
1345 | msg = (typeof(msg)){ |
1346 | .chunk_id = htobe64(chunk_id), | |
1347 | .creation_timestamp = htobe64((uint64_t) creation_timestamp), | |
1348 | .override_name_length = htobe32((uint32_t) chunk_name_length), | |
1349 | }; | |
1350 | ||
1351 | ret = lttng_dynamic_buffer_append(&payload, &msg, sizeof(msg)); | |
1352 | if (ret) { | |
1353 | goto end; | |
1354 | } | |
1355 | if (chunk_name_length) { | |
1356 | ret = lttng_dynamic_buffer_append( | |
1357 | &payload, chunk_name, chunk_name_length); | |
1358 | if (ret) { | |
1359 | goto end; | |
1360 | } | |
1361 | } | |
1362 | ||
bbc4768c JG |
1363 | ret = send_command(sock, RELAYD_CREATE_TRACE_CHUNK, payload.data, |
1364 | payload.size, 0); | |
e5add6d0 JG |
1365 | if (ret < 0) { |
1366 | ERR("Failed to send trace chunk creation command to relay daemon"); | |
1367 | goto end; | |
1368 | } | |
1369 | ||
1370 | ret = recv_reply(sock, &reply, sizeof(reply)); | |
1371 | if (ret < 0) { | |
1372 | ERR("Failed to receive relay daemon trace chunk creation command reply"); | |
1373 | goto end; | |
1374 | } | |
1375 | ||
1376 | reply.ret_code = be32toh(reply.ret_code); | |
1377 | if (reply.ret_code != LTTNG_OK) { | |
1378 | ret = -1; | |
1379 | ERR("Relayd trace chunk create replied error %d", | |
1380 | reply.ret_code); | |
1381 | } else { | |
1382 | ret = 0; | |
1383 | DBG("Relayd successfully created trace chunk: chunk_id = %" PRIu64, | |
1384 | chunk_id); | |
1385 | } | |
1386 | ||
1387 | end: | |
1388 | lttng_dynamic_buffer_reset(&payload); | |
1389 | return ret; | |
1390 | } | |
bbc4768c JG |
1391 | |
1392 | int relayd_close_trace_chunk(struct lttcomm_relayd_sock *sock, | |
ecd1a12f MD |
1393 | struct lttng_trace_chunk *chunk, |
1394 | char *path) | |
bbc4768c JG |
1395 | { |
1396 | int ret = 0; | |
1397 | enum lttng_trace_chunk_status status; | |
1398 | struct lttcomm_relayd_close_trace_chunk msg = {}; | |
ecd1a12f | 1399 | struct lttcomm_relayd_close_trace_chunk_reply reply = {}; |
bbc4768c JG |
1400 | uint64_t chunk_id; |
1401 | time_t close_timestamp; | |
1402 | LTTNG_OPTIONAL(enum lttng_trace_chunk_command_type) close_command = {}; | |
1403 | ||
070b6a86 MD |
1404 | if (!relayd_supports_chunks(sock)) { |
1405 | DBG("Refusing to close remote trace chunk: relayd does not support chunks"); | |
1406 | goto end; | |
1407 | } | |
1408 | ||
bbc4768c JG |
1409 | status = lttng_trace_chunk_get_id(chunk, &chunk_id); |
1410 | if (status != LTTNG_TRACE_CHUNK_STATUS_OK) { | |
1411 | ERR("Failed to get trace chunk id"); | |
1412 | ret = -1; | |
1413 | goto end; | |
1414 | } | |
1415 | ||
1416 | status = lttng_trace_chunk_get_close_timestamp(chunk, &close_timestamp); | |
1417 | if (status != LTTNG_TRACE_CHUNK_STATUS_OK) { | |
1418 | ERR("Failed to get trace chunk close timestamp"); | |
1419 | ret = -1; | |
1420 | goto end; | |
1421 | } | |
1422 | ||
1423 | status = lttng_trace_chunk_get_close_command(chunk, | |
1424 | &close_command.value); | |
1425 | switch (status) { | |
1426 | case LTTNG_TRACE_CHUNK_STATUS_OK: | |
1427 | close_command.is_set = 1; | |
1428 | break; | |
1429 | case LTTNG_TRACE_CHUNK_STATUS_NONE: | |
1430 | break; | |
1431 | default: | |
1432 | ERR("Failed to get trace chunk close command"); | |
1433 | ret = -1; | |
1434 | goto end; | |
1435 | } | |
1436 | ||
1437 | msg = (typeof(msg)){ | |
1438 | .chunk_id = htobe64(chunk_id), | |
1439 | .close_timestamp = htobe64((uint64_t) close_timestamp), | |
1440 | .close_command = { | |
1441 | .value = htobe32((uint32_t) close_command.value), | |
1442 | .is_set = close_command.is_set, | |
1443 | }, | |
1444 | }; | |
1445 | ||
1446 | ret = send_command(sock, RELAYD_CLOSE_TRACE_CHUNK, &msg, sizeof(msg), | |
1447 | 0); | |
1448 | if (ret < 0) { | |
1449 | ERR("Failed to send trace chunk close command to relay daemon"); | |
1450 | goto end; | |
1451 | } | |
1452 | ||
1453 | ret = recv_reply(sock, &reply, sizeof(reply)); | |
1454 | if (ret < 0) { | |
1455 | ERR("Failed to receive relay daemon trace chunk close command reply"); | |
1456 | goto end; | |
1457 | } | |
1458 | ||
ecd1a12f MD |
1459 | reply.path_length = be32toh(reply.path_length); |
1460 | if (reply.path_length >= LTTNG_PATH_MAX) { | |
1461 | ERR("Chunk path too long"); | |
1462 | ret = -1; | |
1463 | goto end; | |
1464 | } | |
1465 | ||
1466 | ret = recv_reply(sock, path, reply.path_length); | |
1467 | if (ret < 0) { | |
1468 | ERR("Failed to receive relay daemon trace chunk close command reply"); | |
1469 | goto end; | |
1470 | } | |
1471 | if (path[reply.path_length - 1] != '\0') { | |
1472 | ERR("Invalid trace chunk path returned by relay daemon (not null-terminated)"); | |
1473 | ret = -1; | |
1474 | goto end; | |
1475 | } | |
1476 | ||
1477 | reply.generic.ret_code = be32toh(reply.generic.ret_code); | |
1478 | if (reply.generic.ret_code != LTTNG_OK) { | |
bbc4768c JG |
1479 | ret = -1; |
1480 | ERR("Relayd trace chunk close replied error %d", | |
ecd1a12f | 1481 | reply.generic.ret_code); |
bbc4768c JG |
1482 | } else { |
1483 | ret = 0; | |
1484 | DBG("Relayd successfully closed trace chunk: chunk_id = %" PRIu64, | |
1485 | chunk_id); | |
1486 | } | |
1487 | end: | |
1488 | return ret; | |
1489 | } | |
c35f9726 JG |
1490 | |
1491 | int relayd_trace_chunk_exists(struct lttcomm_relayd_sock *sock, | |
1492 | uint64_t chunk_id, bool *chunk_exists) | |
1493 | { | |
1494 | int ret = 0; | |
1495 | struct lttcomm_relayd_trace_chunk_exists msg = {}; | |
1496 | struct lttcomm_relayd_trace_chunk_exists_reply reply = {}; | |
1497 | ||
070b6a86 MD |
1498 | if (!relayd_supports_chunks(sock)) { |
1499 | DBG("Refusing to check for trace chunk existence: relayd does not support chunks"); | |
caa15afd JR |
1500 | /* The chunk will never exist */ |
1501 | *chunk_exists = false; | |
070b6a86 MD |
1502 | goto end; |
1503 | } | |
1504 | ||
c35f9726 JG |
1505 | msg = (typeof(msg)){ |
1506 | .chunk_id = htobe64(chunk_id), | |
1507 | }; | |
1508 | ||
1509 | ret = send_command(sock, RELAYD_TRACE_CHUNK_EXISTS, &msg, sizeof(msg), | |
1510 | 0); | |
1511 | if (ret < 0) { | |
1512 | ERR("Failed to send trace chunk exists command to relay daemon"); | |
1513 | goto end; | |
1514 | } | |
1515 | ||
1516 | ret = recv_reply(sock, &reply, sizeof(reply)); | |
1517 | if (ret < 0) { | |
1518 | ERR("Failed to receive relay daemon trace chunk close command reply"); | |
1519 | goto end; | |
1520 | } | |
1521 | ||
1522 | reply.generic.ret_code = be32toh(reply.generic.ret_code); | |
1523 | if (reply.generic.ret_code != LTTNG_OK) { | |
1524 | ret = -1; | |
1525 | ERR("Relayd trace chunk close replied error %d", | |
1526 | reply.generic.ret_code); | |
1527 | } else { | |
1528 | ret = 0; | |
1529 | DBG("Relayd successfully checked trace chunk existence: chunk_id = %" PRIu64 | |
1530 | ", exists = %s", chunk_id, | |
1531 | reply.trace_chunk_exists ? "true" : "false"); | |
1532 | *chunk_exists = !!reply.trace_chunk_exists; | |
1533 | } | |
1534 | end: | |
1535 | return ret; | |
1536 | } | |
8614e600 MD |
1537 | |
1538 | int relayd_get_configuration(struct lttcomm_relayd_sock *sock, | |
1539 | uint64_t query_flags, | |
1540 | uint64_t *result_flags) | |
1541 | { | |
1542 | int ret = 0; | |
1543 | struct lttcomm_relayd_get_configuration msg = (typeof(msg)) { | |
1544 | .query_flags = htobe64(query_flags), | |
1545 | }; | |
1546 | struct lttcomm_relayd_get_configuration_reply reply = {}; | |
1547 | ||
1548 | if (!relayd_supports_get_configuration(sock)) { | |
1549 | DBG("Refusing to get relayd configuration (unsupported by relayd)"); | |
1550 | if (query_flags) { | |
1551 | ret = -1; | |
1552 | goto end; | |
1553 | } | |
1554 | *result_flags = 0; | |
1555 | goto end; | |
1556 | } | |
1557 | ||
1558 | ret = send_command(sock, RELAYD_GET_CONFIGURATION, &msg, sizeof(msg), | |
1559 | 0); | |
1560 | if (ret < 0) { | |
1561 | ERR("Failed to send get configuration command to relay daemon"); | |
1562 | goto end; | |
1563 | } | |
1564 | ||
1565 | ret = recv_reply(sock, &reply, sizeof(reply)); | |
1566 | if (ret < 0) { | |
1567 | ERR("Failed to receive relay daemon get configuration command reply"); | |
1568 | goto end; | |
1569 | } | |
1570 | ||
1571 | reply.generic.ret_code = be32toh(reply.generic.ret_code); | |
1572 | if (reply.generic.ret_code != LTTNG_OK) { | |
1573 | ret = -1; | |
1574 | ERR("Relayd get configuration replied error %d", | |
1575 | reply.generic.ret_code); | |
1576 | } else { | |
1577 | reply.relayd_configuration_flags = | |
1578 | be64toh(reply.relayd_configuration_flags); | |
1579 | ret = 0; | |
1580 | DBG("Relayd successfully got configuration: query_flags = %" PRIu64 | |
1581 | ", results_flags = %" PRIu64, query_flags, | |
1582 | reply.relayd_configuration_flags); | |
1583 | *result_flags = reply.relayd_configuration_flags; | |
1584 | } | |
1585 | end: | |
1586 | return ret; | |
1587 | } |