Commit | Line | Data |
---|---|---|
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 | ||
18 | #define _GNU_SOURCE | |
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> | |
28 | #include <common/sessiond-comm/relayd.h> | |
50adc264 | 29 | #include <common/index/ctf-index.h> |
00e2e675 DG |
30 | |
31 | #include "relayd.h" | |
32 | ||
33 | /* | |
34 | * Send command. Fill up the header and append the data. | |
35 | */ | |
6151a90f | 36 | static int send_command(struct lttcomm_relayd_sock *rsock, |
7c9534d6 | 37 | enum lttcomm_relayd_command cmd, void *data, size_t size, |
00e2e675 DG |
38 | int flags) |
39 | { | |
40 | int ret; | |
41 | struct lttcomm_relayd_hdr header; | |
42 | char *buf; | |
43 | uint64_t buf_size = sizeof(header); | |
44 | ||
f96e4545 MD |
45 | if (rsock->sock.fd < 0) { |
46 | return -ECONNRESET; | |
47 | } | |
48 | ||
00e2e675 DG |
49 | if (data) { |
50 | buf_size += size; | |
51 | } | |
52 | ||
53 | buf = zmalloc(buf_size); | |
54 | if (buf == NULL) { | |
55 | PERROR("zmalloc relayd send command buf"); | |
56 | ret = -1; | |
57 | goto alloc_error; | |
58 | } | |
59 | ||
53efb85a | 60 | memset(&header, 0, sizeof(header)); |
00e2e675 DG |
61 | header.cmd = htobe32(cmd); |
62 | header.data_size = htobe64(size); | |
63 | ||
64 | /* Zeroed for now since not used. */ | |
65 | header.cmd_version = 0; | |
66 | header.circuit_id = 0; | |
67 | ||
68 | /* Prepare buffer to send. */ | |
69 | memcpy(buf, &header, sizeof(header)); | |
70 | if (data) { | |
71 | memcpy(buf + sizeof(header), data, size); | |
72 | } | |
73 | ||
6151a90f | 74 | ret = rsock->sock.ops->sendmsg(&rsock->sock, buf, buf_size, flags); |
00e2e675 | 75 | if (ret < 0) { |
8994307f | 76 | ret = -errno; |
00e2e675 DG |
77 | goto error; |
78 | } | |
79 | ||
633d0084 | 80 | DBG3("Relayd sending command %d of size %" PRIu64, cmd, buf_size); |
00e2e675 DG |
81 | |
82 | error: | |
83 | free(buf); | |
84 | alloc_error: | |
85 | return ret; | |
86 | } | |
87 | ||
88 | /* | |
89 | * Receive reply data on socket. This MUST be call after send_command or else | |
90 | * could result in unexpected behavior(s). | |
91 | */ | |
6151a90f | 92 | static int recv_reply(struct lttcomm_relayd_sock *rsock, void *data, size_t size) |
00e2e675 DG |
93 | { |
94 | int ret; | |
95 | ||
f96e4545 MD |
96 | if (rsock->sock.fd < 0) { |
97 | return -ECONNRESET; | |
98 | } | |
99 | ||
8fd623e0 | 100 | DBG3("Relayd waiting for reply of size %zu", size); |
00e2e675 | 101 | |
6151a90f | 102 | ret = rsock->sock.ops->recvmsg(&rsock->sock, data, size, 0); |
20275fe8 DG |
103 | if (ret <= 0 || ret != size) { |
104 | if (ret == 0) { | |
105 | /* Orderly shutdown. */ | |
6151a90f | 106 | DBG("Socket %d has performed an orderly shutdown", rsock->sock.fd); |
20275fe8 | 107 | } else { |
8fd623e0 | 108 | DBG("Receiving reply failed on sock %d for size %zu with ret %d", |
6151a90f | 109 | rsock->sock.fd, size, ret); |
20275fe8 DG |
110 | } |
111 | /* Always return -1 here and the caller can use errno. */ | |
112 | ret = -1; | |
00e2e675 DG |
113 | goto error; |
114 | } | |
115 | ||
116 | error: | |
117 | return ret; | |
118 | } | |
119 | ||
d3e2ba59 JD |
120 | /* |
121 | * Starting at 2.4, RELAYD_CREATE_SESSION takes additional parameters to | |
122 | * support the live reading capability. | |
123 | */ | |
124 | static int relayd_create_session_2_4(struct lttcomm_relayd_sock *rsock, | |
125 | uint64_t *session_id, char *session_name, char *hostname, | |
7d2f7452 | 126 | int session_live_timer, unsigned int snapshot) |
d3e2ba59 JD |
127 | { |
128 | int ret; | |
129 | struct lttcomm_relayd_create_session_2_4 msg; | |
130 | ||
131 | strncpy(msg.session_name, session_name, sizeof(msg.session_name)); | |
132 | strncpy(msg.hostname, hostname, sizeof(msg.hostname)); | |
133 | msg.live_timer = htobe32(session_live_timer); | |
7d2f7452 | 134 | msg.snapshot = htobe32(snapshot); |
d3e2ba59 JD |
135 | |
136 | /* Send command */ | |
137 | ret = send_command(rsock, RELAYD_CREATE_SESSION, &msg, sizeof(msg), 0); | |
138 | if (ret < 0) { | |
139 | goto error; | |
140 | } | |
141 | ||
142 | error: | |
143 | return ret; | |
144 | } | |
145 | ||
146 | /* | |
147 | * RELAYD_CREATE_SESSION from 2.1 to 2.3. | |
148 | */ | |
149 | static int relayd_create_session_2_1(struct lttcomm_relayd_sock *rsock, | |
150 | uint64_t *session_id) | |
151 | { | |
152 | int ret; | |
153 | ||
154 | /* Send command */ | |
155 | ret = send_command(rsock, RELAYD_CREATE_SESSION, NULL, 0, 0); | |
156 | if (ret < 0) { | |
157 | goto error; | |
158 | } | |
159 | ||
160 | error: | |
161 | return ret; | |
162 | } | |
163 | ||
c5b6f4f0 DG |
164 | /* |
165 | * Send a RELAYD_CREATE_SESSION command to the relayd with the given socket and | |
166 | * set session_id of the relayd if we have a successful reply from the relayd. | |
167 | * | |
20275fe8 DG |
168 | * On success, return 0 else a negative value which is either an errno error or |
169 | * a lttng error code from the relayd. | |
c5b6f4f0 | 170 | */ |
d3e2ba59 | 171 | int relayd_create_session(struct lttcomm_relayd_sock *rsock, uint64_t *session_id, |
7d2f7452 DG |
172 | char *session_name, char *hostname, int session_live_timer, |
173 | unsigned int snapshot) | |
c5b6f4f0 DG |
174 | { |
175 | int ret; | |
176 | struct lttcomm_relayd_status_session reply; | |
177 | ||
6151a90f | 178 | assert(rsock); |
c5b6f4f0 DG |
179 | assert(session_id); |
180 | ||
181 | DBG("Relayd create session"); | |
182 | ||
d3e2ba59 JD |
183 | switch(rsock->minor) { |
184 | case 1: | |
185 | case 2: | |
186 | case 3: | |
187 | ret = relayd_create_session_2_1(rsock, session_id); | |
31d74dc3 | 188 | break; |
d3e2ba59 JD |
189 | case 4: |
190 | default: | |
7d2f7452 DG |
191 | ret = relayd_create_session_2_4(rsock, session_id, session_name, |
192 | hostname, session_live_timer, snapshot); | |
31d74dc3 | 193 | break; |
d3e2ba59 JD |
194 | } |
195 | ||
c5b6f4f0 DG |
196 | if (ret < 0) { |
197 | goto error; | |
198 | } | |
199 | ||
20275fe8 | 200 | /* Receive response */ |
6151a90f | 201 | ret = recv_reply(rsock, (void *) &reply, sizeof(reply)); |
c5b6f4f0 DG |
202 | if (ret < 0) { |
203 | goto error; | |
204 | } | |
205 | ||
206 | reply.session_id = be64toh(reply.session_id); | |
207 | reply.ret_code = be32toh(reply.ret_code); | |
208 | ||
209 | /* Return session id or negative ret code. */ | |
210 | if (reply.ret_code != LTTNG_OK) { | |
bb63afd9 DG |
211 | ret = -1; |
212 | ERR("Relayd create session replied error %d", reply.ret_code); | |
c5b6f4f0 DG |
213 | goto error; |
214 | } else { | |
215 | ret = 0; | |
216 | *session_id = reply.session_id; | |
217 | } | |
218 | ||
219 | DBG("Relayd session created with id %" PRIu64, reply.session_id); | |
220 | ||
221 | error: | |
222 | return ret; | |
223 | } | |
224 | ||
00e2e675 DG |
225 | /* |
226 | * Add stream on the relayd and assign stream handle to the stream_id argument. | |
227 | * | |
228 | * On success return 0 else return ret_code negative value. | |
229 | */ | |
6151a90f | 230 | int relayd_add_stream(struct lttcomm_relayd_sock *rsock, const char *channel_name, |
0f907de1 JD |
231 | const char *pathname, uint64_t *stream_id, |
232 | uint64_t tracefile_size, uint64_t tracefile_count) | |
00e2e675 DG |
233 | { |
234 | int ret; | |
235 | struct lttcomm_relayd_add_stream msg; | |
0f907de1 | 236 | struct lttcomm_relayd_add_stream_2_2 msg_2_2; |
00e2e675 DG |
237 | struct lttcomm_relayd_status_stream reply; |
238 | ||
239 | /* Code flow error. Safety net. */ | |
6151a90f | 240 | assert(rsock); |
00e2e675 DG |
241 | assert(channel_name); |
242 | assert(pathname); | |
243 | ||
244 | DBG("Relayd adding stream for channel name %s", channel_name); | |
245 | ||
0f907de1 JD |
246 | /* Compat with relayd 2.1 */ |
247 | if (rsock->minor == 1) { | |
53efb85a | 248 | memset(&msg, 0, sizeof(msg)); |
0f907de1 JD |
249 | strncpy(msg.channel_name, channel_name, sizeof(msg.channel_name)); |
250 | strncpy(msg.pathname, pathname, sizeof(msg.pathname)); | |
00e2e675 | 251 | |
0f907de1 JD |
252 | /* Send command */ |
253 | ret = send_command(rsock, RELAYD_ADD_STREAM, (void *) &msg, sizeof(msg), 0); | |
254 | if (ret < 0) { | |
255 | goto error; | |
256 | } | |
257 | } else { | |
53efb85a | 258 | memset(&msg_2_2, 0, sizeof(msg_2_2)); |
0f907de1 JD |
259 | /* Compat with relayd 2.2+ */ |
260 | strncpy(msg_2_2.channel_name, channel_name, sizeof(msg_2_2.channel_name)); | |
261 | strncpy(msg_2_2.pathname, pathname, sizeof(msg_2_2.pathname)); | |
262 | msg_2_2.tracefile_size = htobe64(tracefile_size); | |
263 | msg_2_2.tracefile_count = htobe64(tracefile_count); | |
264 | ||
265 | /* Send command */ | |
266 | ret = send_command(rsock, RELAYD_ADD_STREAM, (void *) &msg_2_2, sizeof(msg_2_2), 0); | |
267 | if (ret < 0) { | |
268 | goto error; | |
269 | } | |
00e2e675 DG |
270 | } |
271 | ||
633d0084 | 272 | /* Waiting for reply */ |
6151a90f | 273 | ret = recv_reply(rsock, (void *) &reply, sizeof(reply)); |
00e2e675 DG |
274 | if (ret < 0) { |
275 | goto error; | |
276 | } | |
277 | ||
278 | /* Back to host bytes order. */ | |
279 | reply.handle = be64toh(reply.handle); | |
280 | reply.ret_code = be32toh(reply.ret_code); | |
281 | ||
282 | /* Return session id or negative ret code. */ | |
f73fabfd | 283 | if (reply.ret_code != LTTNG_OK) { |
bb63afd9 DG |
284 | ret = -1; |
285 | ERR("Relayd add stream replied error %d", reply.ret_code); | |
00e2e675 DG |
286 | } else { |
287 | /* Success */ | |
288 | ret = 0; | |
289 | *stream_id = reply.handle; | |
290 | } | |
291 | ||
77c7c900 MD |
292 | DBG("Relayd stream added successfully with handle %" PRIu64, |
293 | reply.handle); | |
00e2e675 DG |
294 | |
295 | error: | |
296 | return ret; | |
297 | } | |
298 | ||
a4baae1b JD |
299 | /* |
300 | * Inform the relay that all the streams for the current channel has been sent. | |
301 | * | |
302 | * On success return 0 else return ret_code negative value. | |
303 | */ | |
304 | int relayd_streams_sent(struct lttcomm_relayd_sock *rsock) | |
305 | { | |
306 | int ret; | |
307 | struct lttcomm_relayd_generic_reply reply; | |
308 | ||
309 | /* Code flow error. Safety net. */ | |
310 | assert(rsock); | |
311 | ||
312 | DBG("Relayd sending streams sent."); | |
313 | ||
314 | /* This feature was introduced in 2.4, ignore it for earlier versions. */ | |
315 | if (rsock->minor < 4) { | |
316 | ret = 0; | |
317 | goto end; | |
318 | } | |
319 | ||
320 | /* Send command */ | |
321 | ret = send_command(rsock, RELAYD_STREAMS_SENT, NULL, 0, 0); | |
322 | if (ret < 0) { | |
323 | goto error; | |
324 | } | |
325 | ||
326 | /* Waiting for reply */ | |
327 | ret = recv_reply(rsock, (void *) &reply, sizeof(reply)); | |
328 | if (ret < 0) { | |
329 | goto error; | |
330 | } | |
331 | ||
332 | /* Back to host bytes order. */ | |
333 | reply.ret_code = be32toh(reply.ret_code); | |
334 | ||
335 | /* Return session id or negative ret code. */ | |
336 | if (reply.ret_code != LTTNG_OK) { | |
337 | ret = -1; | |
338 | ERR("Relayd streams sent replied error %d", reply.ret_code); | |
339 | goto error; | |
340 | } else { | |
341 | /* Success */ | |
342 | ret = 0; | |
343 | } | |
344 | ||
345 | DBG("Relayd streams sent success"); | |
346 | ||
347 | error: | |
348 | end: | |
349 | return ret; | |
350 | } | |
351 | ||
00e2e675 DG |
352 | /* |
353 | * Check version numbers on the relayd. | |
d4519fa3 JD |
354 | * If major versions are compatible, we assign minor_to_use to the |
355 | * minor version of the procotol we are going to use for this session. | |
00e2e675 DG |
356 | * |
357 | * Return 0 if compatible else negative value. | |
358 | */ | |
6151a90f | 359 | int relayd_version_check(struct lttcomm_relayd_sock *rsock) |
00e2e675 DG |
360 | { |
361 | int ret; | |
092b6259 | 362 | struct lttcomm_relayd_version msg; |
00e2e675 DG |
363 | |
364 | /* Code flow error. Safety net. */ | |
6151a90f | 365 | assert(rsock); |
00e2e675 | 366 | |
6151a90f JD |
367 | DBG("Relayd version check for major.minor %u.%u", rsock->major, |
368 | rsock->minor); | |
00e2e675 | 369 | |
53efb85a | 370 | memset(&msg, 0, sizeof(msg)); |
092b6259 | 371 | /* Prepare network byte order before transmission. */ |
6151a90f JD |
372 | msg.major = htobe32(rsock->major); |
373 | msg.minor = htobe32(rsock->minor); | |
092b6259 | 374 | |
00e2e675 | 375 | /* Send command */ |
6151a90f | 376 | ret = send_command(rsock, RELAYD_VERSION, (void *) &msg, sizeof(msg), 0); |
00e2e675 DG |
377 | if (ret < 0) { |
378 | goto error; | |
379 | } | |
380 | ||
20275fe8 | 381 | /* Receive response */ |
6151a90f | 382 | ret = recv_reply(rsock, (void *) &msg, sizeof(msg)); |
00e2e675 DG |
383 | if (ret < 0) { |
384 | goto error; | |
385 | } | |
386 | ||
387 | /* Set back to host bytes order */ | |
092b6259 DG |
388 | msg.major = be32toh(msg.major); |
389 | msg.minor = be32toh(msg.minor); | |
390 | ||
391 | /* | |
392 | * Only validate the major version. If the other side is higher, | |
393 | * communication is not possible. Only major version equal can talk to each | |
394 | * other. If the minor version differs, the lowest version is used by both | |
395 | * sides. | |
092b6259 | 396 | */ |
6151a90f | 397 | if (msg.major != rsock->major) { |
d4519fa3 JD |
398 | /* Not compatible */ |
399 | ret = -1; | |
400 | DBG2("Relayd version is NOT compatible. Relayd version %u != %u (us)", | |
6151a90f | 401 | msg.major, rsock->major); |
092b6259 | 402 | goto error; |
00e2e675 DG |
403 | } |
404 | ||
092b6259 | 405 | /* |
6151a90f JD |
406 | * If the relayd's minor version is higher, it will adapt to our version so |
407 | * we can continue to use the latest relayd communication data structure. | |
408 | * If the received minor version is higher, the relayd should adapt to us. | |
092b6259 | 409 | */ |
6151a90f JD |
410 | if (rsock->minor > msg.minor) { |
411 | rsock->minor = msg.minor; | |
d4519fa3 | 412 | } |
092b6259 | 413 | |
d4519fa3 JD |
414 | /* Version number compatible */ |
415 | DBG2("Relayd version is compatible, using protocol version %u.%u", | |
6151a90f | 416 | rsock->major, rsock->minor); |
d4519fa3 | 417 | ret = 0; |
00e2e675 DG |
418 | |
419 | error: | |
420 | return ret; | |
421 | } | |
422 | ||
00e2e675 DG |
423 | /* |
424 | * Add stream on the relayd and assign stream handle to the stream_id argument. | |
425 | * | |
426 | * On success return 0 else return ret_code negative value. | |
427 | */ | |
6151a90f | 428 | int relayd_send_metadata(struct lttcomm_relayd_sock *rsock, size_t len) |
00e2e675 DG |
429 | { |
430 | int ret; | |
431 | ||
432 | /* Code flow error. Safety net. */ | |
6151a90f | 433 | assert(rsock); |
00e2e675 | 434 | |
77c7c900 | 435 | DBG("Relayd sending metadata of size %zu", len); |
00e2e675 DG |
436 | |
437 | /* Send command */ | |
6151a90f | 438 | ret = send_command(rsock, RELAYD_SEND_METADATA, NULL, len, 0); |
00e2e675 DG |
439 | if (ret < 0) { |
440 | goto error; | |
441 | } | |
442 | ||
443 | DBG2("Relayd metadata added successfully"); | |
444 | ||
445 | /* | |
446 | * After that call, the metadata data MUST be sent to the relayd so the | |
447 | * receive size on the other end matches the len of the metadata packet | |
633d0084 | 448 | * header. This is why we don't wait for a reply here. |
00e2e675 DG |
449 | */ |
450 | ||
451 | error: | |
452 | return ret; | |
453 | } | |
454 | ||
455 | /* | |
6151a90f | 456 | * Connect to relay daemon with an allocated lttcomm_relayd_sock. |
00e2e675 | 457 | */ |
6151a90f | 458 | int relayd_connect(struct lttcomm_relayd_sock *rsock) |
00e2e675 DG |
459 | { |
460 | /* Code flow error. Safety net. */ | |
6151a90f | 461 | assert(rsock); |
00e2e675 | 462 | |
f96e4545 MD |
463 | if (!rsock->sock.ops) { |
464 | /* | |
465 | * Attempting a connect on a non-initialized socket. | |
466 | */ | |
467 | return -ECONNRESET; | |
468 | } | |
469 | ||
00e2e675 DG |
470 | DBG3("Relayd connect ..."); |
471 | ||
6151a90f | 472 | return rsock->sock.ops->connect(&rsock->sock); |
00e2e675 DG |
473 | } |
474 | ||
475 | /* | |
6151a90f | 476 | * Close relayd socket with an allocated lttcomm_relayd_sock. |
ffe60014 DG |
477 | * |
478 | * If no socket operations are found, simply return 0 meaning that everything | |
479 | * is fine. Without operations, the socket can not possibly be opened or used. | |
480 | * This is possible if the socket was allocated but not created. However, the | |
481 | * caller could simply use it to store a valid file descriptor for instance | |
482 | * passed over a Unix socket and call this to cleanup but still without a valid | |
483 | * ops pointer. | |
484 | * | |
485 | * Return the close returned value. On error, a negative value is usually | |
486 | * returned back from close(2). | |
00e2e675 | 487 | */ |
6151a90f | 488 | int relayd_close(struct lttcomm_relayd_sock *rsock) |
00e2e675 | 489 | { |
ffe60014 DG |
490 | int ret; |
491 | ||
00e2e675 | 492 | /* Code flow error. Safety net. */ |
6151a90f | 493 | assert(rsock); |
00e2e675 | 494 | |
ffe60014 | 495 | /* An invalid fd is fine, return success. */ |
6151a90f | 496 | if (rsock->sock.fd < 0) { |
ffe60014 DG |
497 | ret = 0; |
498 | goto end; | |
499 | } | |
500 | ||
6151a90f | 501 | DBG3("Relayd closing socket %d", rsock->sock.fd); |
00e2e675 | 502 | |
6151a90f JD |
503 | if (rsock->sock.ops) { |
504 | ret = rsock->sock.ops->close(&rsock->sock); | |
ffe60014 DG |
505 | } else { |
506 | /* Default call if no specific ops found. */ | |
6151a90f | 507 | ret = close(rsock->sock.fd); |
ffe60014 DG |
508 | if (ret < 0) { |
509 | PERROR("relayd_close default close"); | |
510 | } | |
511 | } | |
f96e4545 | 512 | rsock->sock.fd = -1; |
ffe60014 DG |
513 | |
514 | end: | |
515 | return ret; | |
00e2e675 DG |
516 | } |
517 | ||
518 | /* | |
519 | * Send data header structure to the relayd. | |
520 | */ | |
6151a90f | 521 | int relayd_send_data_hdr(struct lttcomm_relayd_sock *rsock, |
00e2e675 DG |
522 | struct lttcomm_relayd_data_hdr *hdr, size_t size) |
523 | { | |
524 | int ret; | |
525 | ||
526 | /* Code flow error. Safety net. */ | |
6151a90f | 527 | assert(rsock); |
00e2e675 DG |
528 | assert(hdr); |
529 | ||
f96e4545 MD |
530 | if (rsock->sock.fd < 0) { |
531 | return -ECONNRESET; | |
532 | } | |
533 | ||
8fd623e0 | 534 | DBG3("Relayd sending data header of size %zu", size); |
00e2e675 DG |
535 | |
536 | /* Again, safety net */ | |
537 | if (size == 0) { | |
538 | size = sizeof(struct lttcomm_relayd_data_hdr); | |
539 | } | |
540 | ||
541 | /* Only send data header. */ | |
6151a90f | 542 | ret = rsock->sock.ops->sendmsg(&rsock->sock, hdr, size, 0); |
00e2e675 | 543 | if (ret < 0) { |
8994307f | 544 | ret = -errno; |
00e2e675 DG |
545 | goto error; |
546 | } | |
547 | ||
548 | /* | |
549 | * The data MUST be sent right after that command for the receive on the | |
550 | * other end to match the size in the header. | |
551 | */ | |
552 | ||
553 | error: | |
554 | return ret; | |
555 | } | |
173af62f DG |
556 | |
557 | /* | |
558 | * Send close stream command to the relayd. | |
559 | */ | |
6151a90f | 560 | int relayd_send_close_stream(struct lttcomm_relayd_sock *rsock, uint64_t stream_id, |
173af62f DG |
561 | uint64_t last_net_seq_num) |
562 | { | |
563 | int ret; | |
564 | struct lttcomm_relayd_close_stream msg; | |
565 | struct lttcomm_relayd_generic_reply reply; | |
566 | ||
567 | /* Code flow error. Safety net. */ | |
6151a90f | 568 | assert(rsock); |
173af62f | 569 | |
77c7c900 | 570 | DBG("Relayd closing stream id %" PRIu64, stream_id); |
173af62f | 571 | |
53efb85a | 572 | memset(&msg, 0, sizeof(msg)); |
173af62f DG |
573 | msg.stream_id = htobe64(stream_id); |
574 | msg.last_net_seq_num = htobe64(last_net_seq_num); | |
575 | ||
576 | /* Send command */ | |
6151a90f | 577 | ret = send_command(rsock, RELAYD_CLOSE_STREAM, (void *) &msg, sizeof(msg), 0); |
173af62f DG |
578 | if (ret < 0) { |
579 | goto error; | |
580 | } | |
581 | ||
20275fe8 | 582 | /* Receive response */ |
6151a90f | 583 | ret = recv_reply(rsock, (void *) &reply, sizeof(reply)); |
173af62f DG |
584 | if (ret < 0) { |
585 | goto error; | |
586 | } | |
587 | ||
588 | reply.ret_code = be32toh(reply.ret_code); | |
589 | ||
590 | /* Return session id or negative ret code. */ | |
f73fabfd | 591 | if (reply.ret_code != LTTNG_OK) { |
bb63afd9 DG |
592 | ret = -1; |
593 | ERR("Relayd close stream replied error %d", reply.ret_code); | |
173af62f DG |
594 | } else { |
595 | /* Success */ | |
596 | ret = 0; | |
597 | } | |
598 | ||
77c7c900 | 599 | DBG("Relayd close stream id %" PRIu64 " successfully", stream_id); |
173af62f DG |
600 | |
601 | error: | |
602 | return ret; | |
603 | } | |
c8f59ee5 DG |
604 | |
605 | /* | |
606 | * Check for data availability for a given stream id. | |
607 | * | |
6d805429 | 608 | * Return 0 if NOT pending, 1 if so and a negative value on error. |
c8f59ee5 | 609 | */ |
6151a90f | 610 | int relayd_data_pending(struct lttcomm_relayd_sock *rsock, uint64_t stream_id, |
c8f59ee5 DG |
611 | uint64_t last_net_seq_num) |
612 | { | |
613 | int ret; | |
6d805429 | 614 | struct lttcomm_relayd_data_pending msg; |
c8f59ee5 DG |
615 | struct lttcomm_relayd_generic_reply reply; |
616 | ||
617 | /* Code flow error. Safety net. */ | |
6151a90f | 618 | assert(rsock); |
c8f59ee5 | 619 | |
6d805429 | 620 | DBG("Relayd data pending for stream id %" PRIu64, stream_id); |
c8f59ee5 | 621 | |
53efb85a | 622 | memset(&msg, 0, sizeof(msg)); |
c8f59ee5 DG |
623 | msg.stream_id = htobe64(stream_id); |
624 | msg.last_net_seq_num = htobe64(last_net_seq_num); | |
625 | ||
626 | /* Send command */ | |
6151a90f | 627 | ret = send_command(rsock, RELAYD_DATA_PENDING, (void *) &msg, |
c8f59ee5 DG |
628 | sizeof(msg), 0); |
629 | if (ret < 0) { | |
630 | goto error; | |
631 | } | |
632 | ||
20275fe8 | 633 | /* Receive response */ |
6151a90f | 634 | ret = recv_reply(rsock, (void *) &reply, sizeof(reply)); |
c8f59ee5 DG |
635 | if (ret < 0) { |
636 | goto error; | |
637 | } | |
638 | ||
639 | reply.ret_code = be32toh(reply.ret_code); | |
640 | ||
641 | /* Return session id or negative ret code. */ | |
642 | if (reply.ret_code >= LTTNG_OK) { | |
bb63afd9 | 643 | ERR("Relayd data pending replied error %d", reply.ret_code); |
c8f59ee5 DG |
644 | } |
645 | ||
646 | /* At this point, the ret code is either 1 or 0 */ | |
647 | ret = reply.ret_code; | |
648 | ||
6d805429 | 649 | DBG("Relayd data is %s pending for stream id %" PRIu64, |
9dd26bb9 | 650 | ret == 1 ? "" : "NOT", stream_id); |
c8f59ee5 DG |
651 | |
652 | error: | |
653 | return ret; | |
654 | } | |
655 | ||
656 | /* | |
657 | * Check on the relayd side for a quiescent state on the control socket. | |
658 | */ | |
6151a90f | 659 | int relayd_quiescent_control(struct lttcomm_relayd_sock *rsock, |
ad7051c0 | 660 | uint64_t metadata_stream_id) |
c8f59ee5 DG |
661 | { |
662 | int ret; | |
ad7051c0 | 663 | struct lttcomm_relayd_quiescent_control msg; |
c8f59ee5 DG |
664 | struct lttcomm_relayd_generic_reply reply; |
665 | ||
666 | /* Code flow error. Safety net. */ | |
6151a90f | 667 | assert(rsock); |
c8f59ee5 DG |
668 | |
669 | DBG("Relayd checking quiescent control state"); | |
670 | ||
53efb85a | 671 | memset(&msg, 0, sizeof(msg)); |
ad7051c0 DG |
672 | msg.stream_id = htobe64(metadata_stream_id); |
673 | ||
c8f59ee5 | 674 | /* Send command */ |
6151a90f | 675 | ret = send_command(rsock, RELAYD_QUIESCENT_CONTROL, &msg, sizeof(msg), 0); |
c8f59ee5 DG |
676 | if (ret < 0) { |
677 | goto error; | |
678 | } | |
679 | ||
20275fe8 | 680 | /* Receive response */ |
6151a90f | 681 | ret = recv_reply(rsock, (void *) &reply, sizeof(reply)); |
c8f59ee5 DG |
682 | if (ret < 0) { |
683 | goto error; | |
684 | } | |
685 | ||
686 | reply.ret_code = be32toh(reply.ret_code); | |
687 | ||
688 | /* Return session id or negative ret code. */ | |
689 | if (reply.ret_code != LTTNG_OK) { | |
bb63afd9 DG |
690 | ret = -1; |
691 | ERR("Relayd quiescent control replied error %d", reply.ret_code); | |
c8f59ee5 DG |
692 | goto error; |
693 | } | |
694 | ||
695 | /* Control socket is quiescent */ | |
6d805429 | 696 | return 0; |
c8f59ee5 DG |
697 | |
698 | error: | |
699 | return ret; | |
700 | } | |
f7079f67 DG |
701 | |
702 | /* | |
703 | * Begin a data pending command for a specific session id. | |
704 | */ | |
6151a90f | 705 | int relayd_begin_data_pending(struct lttcomm_relayd_sock *rsock, uint64_t id) |
f7079f67 DG |
706 | { |
707 | int ret; | |
708 | struct lttcomm_relayd_begin_data_pending msg; | |
709 | struct lttcomm_relayd_generic_reply reply; | |
710 | ||
711 | /* Code flow error. Safety net. */ | |
6151a90f | 712 | assert(rsock); |
f7079f67 DG |
713 | |
714 | DBG("Relayd begin data pending"); | |
715 | ||
53efb85a | 716 | memset(&msg, 0, sizeof(msg)); |
f7079f67 DG |
717 | msg.session_id = htobe64(id); |
718 | ||
719 | /* Send command */ | |
6151a90f | 720 | ret = send_command(rsock, RELAYD_BEGIN_DATA_PENDING, &msg, sizeof(msg), 0); |
f7079f67 DG |
721 | if (ret < 0) { |
722 | goto error; | |
723 | } | |
724 | ||
20275fe8 | 725 | /* Receive response */ |
6151a90f | 726 | ret = recv_reply(rsock, (void *) &reply, sizeof(reply)); |
f7079f67 DG |
727 | if (ret < 0) { |
728 | goto error; | |
729 | } | |
730 | ||
731 | reply.ret_code = be32toh(reply.ret_code); | |
732 | ||
733 | /* Return session id or negative ret code. */ | |
734 | if (reply.ret_code != LTTNG_OK) { | |
bb63afd9 DG |
735 | ret = -1; |
736 | ERR("Relayd begin data pending replied error %d", reply.ret_code); | |
f7079f67 DG |
737 | goto error; |
738 | } | |
739 | ||
740 | return 0; | |
741 | ||
742 | error: | |
743 | return ret; | |
744 | } | |
745 | ||
746 | /* | |
747 | * End a data pending command for a specific session id. | |
748 | * | |
749 | * Return 0 on success and set is_data_inflight to 0 if no data is being | |
750 | * streamed or 1 if it is the case. | |
751 | */ | |
6151a90f | 752 | int relayd_end_data_pending(struct lttcomm_relayd_sock *rsock, uint64_t id, |
f7079f67 DG |
753 | unsigned int *is_data_inflight) |
754 | { | |
af6c30b5 | 755 | int ret, recv_ret; |
f7079f67 DG |
756 | struct lttcomm_relayd_end_data_pending msg; |
757 | struct lttcomm_relayd_generic_reply reply; | |
758 | ||
759 | /* Code flow error. Safety net. */ | |
6151a90f | 760 | assert(rsock); |
f7079f67 DG |
761 | |
762 | DBG("Relayd end data pending"); | |
763 | ||
53efb85a | 764 | memset(&msg, 0, sizeof(msg)); |
f7079f67 DG |
765 | msg.session_id = htobe64(id); |
766 | ||
767 | /* Send command */ | |
6151a90f | 768 | ret = send_command(rsock, RELAYD_END_DATA_PENDING, &msg, sizeof(msg), 0); |
f7079f67 DG |
769 | if (ret < 0) { |
770 | goto error; | |
771 | } | |
772 | ||
20275fe8 | 773 | /* Receive response */ |
6151a90f | 774 | ret = recv_reply(rsock, (void *) &reply, sizeof(reply)); |
f7079f67 DG |
775 | if (ret < 0) { |
776 | goto error; | |
777 | } | |
778 | ||
af6c30b5 DG |
779 | recv_ret = be32toh(reply.ret_code); |
780 | if (recv_ret < 0) { | |
781 | ret = recv_ret; | |
f7079f67 DG |
782 | goto error; |
783 | } | |
784 | ||
af6c30b5 | 785 | *is_data_inflight = recv_ret; |
f7079f67 | 786 | |
af6c30b5 | 787 | DBG("Relayd end data pending is data inflight: %d", recv_ret); |
f7079f67 DG |
788 | |
789 | return 0; | |
790 | ||
791 | error: | |
792 | return ret; | |
793 | } | |
1c20f0e2 JD |
794 | |
795 | /* | |
796 | * Send index to the relayd. | |
797 | */ | |
798 | int relayd_send_index(struct lttcomm_relayd_sock *rsock, | |
50adc264 | 799 | struct ctf_packet_index *index, uint64_t relay_stream_id, |
1c20f0e2 JD |
800 | uint64_t net_seq_num) |
801 | { | |
802 | int ret; | |
803 | struct lttcomm_relayd_index msg; | |
804 | struct lttcomm_relayd_generic_reply reply; | |
805 | ||
806 | /* Code flow error. Safety net. */ | |
807 | assert(rsock); | |
808 | ||
809 | if (rsock->minor < 4) { | |
810 | DBG("Not sending indexes before protocol 2.4"); | |
811 | ret = 0; | |
812 | goto error; | |
813 | } | |
814 | ||
815 | DBG("Relayd sending index for stream ID %" PRIu64, relay_stream_id); | |
816 | ||
53efb85a | 817 | memset(&msg, 0, sizeof(msg)); |
1c20f0e2 JD |
818 | msg.relay_stream_id = htobe64(relay_stream_id); |
819 | msg.net_seq_num = htobe64(net_seq_num); | |
820 | ||
821 | /* The index is already in big endian. */ | |
822 | msg.packet_size = index->packet_size; | |
823 | msg.content_size = index->content_size; | |
824 | msg.timestamp_begin = index->timestamp_begin; | |
825 | msg.timestamp_end = index->timestamp_end; | |
826 | msg.events_discarded = index->events_discarded; | |
827 | msg.stream_id = index->stream_id; | |
828 | ||
829 | /* Send command */ | |
830 | ret = send_command(rsock, RELAYD_SEND_INDEX, &msg, sizeof(msg), 0); | |
831 | if (ret < 0) { | |
832 | goto error; | |
833 | } | |
834 | ||
835 | /* Receive response */ | |
836 | ret = recv_reply(rsock, (void *) &reply, sizeof(reply)); | |
837 | if (ret < 0) { | |
838 | goto error; | |
839 | } | |
840 | ||
841 | reply.ret_code = be32toh(reply.ret_code); | |
842 | ||
843 | /* Return session id or negative ret code. */ | |
844 | if (reply.ret_code != LTTNG_OK) { | |
845 | ret = -1; | |
846 | ERR("Relayd send index replied error %d", reply.ret_code); | |
847 | } else { | |
848 | /* Success */ | |
849 | ret = 0; | |
850 | } | |
851 | ||
852 | error: | |
853 | return ret; | |
854 | } |