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