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