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> | |
1c20f0e2 | 29 | #include <common/index/lttng-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); | |
187 | case 4: | |
188 | default: | |
7d2f7452 DG |
189 | ret = relayd_create_session_2_4(rsock, session_id, session_name, |
190 | hostname, session_live_timer, snapshot); | |
d3e2ba59 JD |
191 | } |
192 | ||
c5b6f4f0 DG |
193 | if (ret < 0) { |
194 | goto error; | |
195 | } | |
196 | ||
20275fe8 | 197 | /* Receive response */ |
6151a90f | 198 | ret = recv_reply(rsock, (void *) &reply, sizeof(reply)); |
c5b6f4f0 DG |
199 | if (ret < 0) { |
200 | goto error; | |
201 | } | |
202 | ||
203 | reply.session_id = be64toh(reply.session_id); | |
204 | reply.ret_code = be32toh(reply.ret_code); | |
205 | ||
206 | /* Return session id or negative ret code. */ | |
207 | if (reply.ret_code != LTTNG_OK) { | |
bb63afd9 DG |
208 | ret = -1; |
209 | ERR("Relayd create session replied error %d", reply.ret_code); | |
c5b6f4f0 DG |
210 | goto error; |
211 | } else { | |
212 | ret = 0; | |
213 | *session_id = reply.session_id; | |
214 | } | |
215 | ||
216 | DBG("Relayd session created with id %" PRIu64, reply.session_id); | |
217 | ||
218 | error: | |
219 | return ret; | |
220 | } | |
221 | ||
00e2e675 DG |
222 | /* |
223 | * Add stream on the relayd and assign stream handle to the stream_id argument. | |
224 | * | |
225 | * On success return 0 else return ret_code negative value. | |
226 | */ | |
6151a90f | 227 | int relayd_add_stream(struct lttcomm_relayd_sock *rsock, const char *channel_name, |
0f907de1 JD |
228 | const char *pathname, uint64_t *stream_id, |
229 | uint64_t tracefile_size, uint64_t tracefile_count) | |
00e2e675 DG |
230 | { |
231 | int ret; | |
232 | struct lttcomm_relayd_add_stream msg; | |
0f907de1 | 233 | struct lttcomm_relayd_add_stream_2_2 msg_2_2; |
00e2e675 DG |
234 | struct lttcomm_relayd_status_stream reply; |
235 | ||
236 | /* Code flow error. Safety net. */ | |
6151a90f | 237 | assert(rsock); |
00e2e675 DG |
238 | assert(channel_name); |
239 | assert(pathname); | |
240 | ||
241 | DBG("Relayd adding stream for channel name %s", channel_name); | |
242 | ||
0f907de1 JD |
243 | /* Compat with relayd 2.1 */ |
244 | if (rsock->minor == 1) { | |
245 | strncpy(msg.channel_name, channel_name, sizeof(msg.channel_name)); | |
246 | strncpy(msg.pathname, pathname, sizeof(msg.pathname)); | |
00e2e675 | 247 | |
0f907de1 JD |
248 | /* Send command */ |
249 | ret = send_command(rsock, RELAYD_ADD_STREAM, (void *) &msg, sizeof(msg), 0); | |
250 | if (ret < 0) { | |
251 | goto error; | |
252 | } | |
253 | } else { | |
254 | /* Compat with relayd 2.2+ */ | |
255 | strncpy(msg_2_2.channel_name, channel_name, sizeof(msg_2_2.channel_name)); | |
256 | strncpy(msg_2_2.pathname, pathname, sizeof(msg_2_2.pathname)); | |
257 | msg_2_2.tracefile_size = htobe64(tracefile_size); | |
258 | msg_2_2.tracefile_count = htobe64(tracefile_count); | |
259 | ||
260 | /* Send command */ | |
261 | ret = send_command(rsock, RELAYD_ADD_STREAM, (void *) &msg_2_2, sizeof(msg_2_2), 0); | |
262 | if (ret < 0) { | |
263 | goto error; | |
264 | } | |
00e2e675 DG |
265 | } |
266 | ||
633d0084 | 267 | /* Waiting for reply */ |
6151a90f | 268 | ret = recv_reply(rsock, (void *) &reply, sizeof(reply)); |
00e2e675 DG |
269 | if (ret < 0) { |
270 | goto error; | |
271 | } | |
272 | ||
273 | /* Back to host bytes order. */ | |
274 | reply.handle = be64toh(reply.handle); | |
275 | reply.ret_code = be32toh(reply.ret_code); | |
276 | ||
277 | /* Return session id or negative ret code. */ | |
f73fabfd | 278 | if (reply.ret_code != LTTNG_OK) { |
bb63afd9 DG |
279 | ret = -1; |
280 | ERR("Relayd add stream replied error %d", reply.ret_code); | |
00e2e675 DG |
281 | } else { |
282 | /* Success */ | |
283 | ret = 0; | |
284 | *stream_id = reply.handle; | |
285 | } | |
286 | ||
77c7c900 MD |
287 | DBG("Relayd stream added successfully with handle %" PRIu64, |
288 | reply.handle); | |
00e2e675 DG |
289 | |
290 | error: | |
291 | return ret; | |
292 | } | |
293 | ||
294 | /* | |
295 | * Check version numbers on the relayd. | |
d4519fa3 JD |
296 | * If major versions are compatible, we assign minor_to_use to the |
297 | * minor version of the procotol we are going to use for this session. | |
00e2e675 DG |
298 | * |
299 | * Return 0 if compatible else negative value. | |
300 | */ | |
6151a90f | 301 | int relayd_version_check(struct lttcomm_relayd_sock *rsock) |
00e2e675 DG |
302 | { |
303 | int ret; | |
092b6259 | 304 | struct lttcomm_relayd_version msg; |
00e2e675 DG |
305 | |
306 | /* Code flow error. Safety net. */ | |
6151a90f | 307 | assert(rsock); |
00e2e675 | 308 | |
6151a90f JD |
309 | DBG("Relayd version check for major.minor %u.%u", rsock->major, |
310 | rsock->minor); | |
00e2e675 | 311 | |
092b6259 | 312 | /* Prepare network byte order before transmission. */ |
6151a90f JD |
313 | msg.major = htobe32(rsock->major); |
314 | msg.minor = htobe32(rsock->minor); | |
092b6259 | 315 | |
00e2e675 | 316 | /* Send command */ |
6151a90f | 317 | ret = send_command(rsock, RELAYD_VERSION, (void *) &msg, sizeof(msg), 0); |
00e2e675 DG |
318 | if (ret < 0) { |
319 | goto error; | |
320 | } | |
321 | ||
20275fe8 | 322 | /* Receive response */ |
6151a90f | 323 | ret = recv_reply(rsock, (void *) &msg, sizeof(msg)); |
00e2e675 DG |
324 | if (ret < 0) { |
325 | goto error; | |
326 | } | |
327 | ||
328 | /* Set back to host bytes order */ | |
092b6259 DG |
329 | msg.major = be32toh(msg.major); |
330 | msg.minor = be32toh(msg.minor); | |
331 | ||
332 | /* | |
333 | * Only validate the major version. If the other side is higher, | |
334 | * communication is not possible. Only major version equal can talk to each | |
335 | * other. If the minor version differs, the lowest version is used by both | |
336 | * sides. | |
092b6259 | 337 | */ |
6151a90f | 338 | if (msg.major != rsock->major) { |
d4519fa3 JD |
339 | /* Not compatible */ |
340 | ret = -1; | |
341 | DBG2("Relayd version is NOT compatible. Relayd version %u != %u (us)", | |
6151a90f | 342 | msg.major, rsock->major); |
092b6259 | 343 | goto error; |
00e2e675 DG |
344 | } |
345 | ||
092b6259 | 346 | /* |
6151a90f JD |
347 | * If the relayd's minor version is higher, it will adapt to our version so |
348 | * we can continue to use the latest relayd communication data structure. | |
349 | * If the received minor version is higher, the relayd should adapt to us. | |
092b6259 | 350 | */ |
6151a90f JD |
351 | if (rsock->minor > msg.minor) { |
352 | rsock->minor = msg.minor; | |
d4519fa3 | 353 | } |
092b6259 | 354 | |
d4519fa3 JD |
355 | /* Version number compatible */ |
356 | DBG2("Relayd version is compatible, using protocol version %u.%u", | |
6151a90f | 357 | rsock->major, rsock->minor); |
d4519fa3 | 358 | ret = 0; |
00e2e675 DG |
359 | |
360 | error: | |
361 | return ret; | |
362 | } | |
363 | ||
00e2e675 DG |
364 | /* |
365 | * Add stream on the relayd and assign stream handle to the stream_id argument. | |
366 | * | |
367 | * On success return 0 else return ret_code negative value. | |
368 | */ | |
6151a90f | 369 | int relayd_send_metadata(struct lttcomm_relayd_sock *rsock, size_t len) |
00e2e675 DG |
370 | { |
371 | int ret; | |
372 | ||
373 | /* Code flow error. Safety net. */ | |
6151a90f | 374 | assert(rsock); |
00e2e675 | 375 | |
77c7c900 | 376 | DBG("Relayd sending metadata of size %zu", len); |
00e2e675 DG |
377 | |
378 | /* Send command */ | |
6151a90f | 379 | ret = send_command(rsock, RELAYD_SEND_METADATA, NULL, len, 0); |
00e2e675 DG |
380 | if (ret < 0) { |
381 | goto error; | |
382 | } | |
383 | ||
384 | DBG2("Relayd metadata added successfully"); | |
385 | ||
386 | /* | |
387 | * After that call, the metadata data MUST be sent to the relayd so the | |
388 | * receive size on the other end matches the len of the metadata packet | |
633d0084 | 389 | * header. This is why we don't wait for a reply here. |
00e2e675 DG |
390 | */ |
391 | ||
392 | error: | |
393 | return ret; | |
394 | } | |
395 | ||
396 | /* | |
6151a90f | 397 | * Connect to relay daemon with an allocated lttcomm_relayd_sock. |
00e2e675 | 398 | */ |
6151a90f | 399 | int relayd_connect(struct lttcomm_relayd_sock *rsock) |
00e2e675 DG |
400 | { |
401 | /* Code flow error. Safety net. */ | |
6151a90f | 402 | assert(rsock); |
00e2e675 | 403 | |
f96e4545 MD |
404 | if (!rsock->sock.ops) { |
405 | /* | |
406 | * Attempting a connect on a non-initialized socket. | |
407 | */ | |
408 | return -ECONNRESET; | |
409 | } | |
410 | ||
00e2e675 DG |
411 | DBG3("Relayd connect ..."); |
412 | ||
6151a90f | 413 | return rsock->sock.ops->connect(&rsock->sock); |
00e2e675 DG |
414 | } |
415 | ||
416 | /* | |
6151a90f | 417 | * Close relayd socket with an allocated lttcomm_relayd_sock. |
ffe60014 DG |
418 | * |
419 | * If no socket operations are found, simply return 0 meaning that everything | |
420 | * is fine. Without operations, the socket can not possibly be opened or used. | |
421 | * This is possible if the socket was allocated but not created. However, the | |
422 | * caller could simply use it to store a valid file descriptor for instance | |
423 | * passed over a Unix socket and call this to cleanup but still without a valid | |
424 | * ops pointer. | |
425 | * | |
426 | * Return the close returned value. On error, a negative value is usually | |
427 | * returned back from close(2). | |
00e2e675 | 428 | */ |
6151a90f | 429 | int relayd_close(struct lttcomm_relayd_sock *rsock) |
00e2e675 | 430 | { |
ffe60014 DG |
431 | int ret; |
432 | ||
00e2e675 | 433 | /* Code flow error. Safety net. */ |
6151a90f | 434 | assert(rsock); |
00e2e675 | 435 | |
ffe60014 | 436 | /* An invalid fd is fine, return success. */ |
6151a90f | 437 | if (rsock->sock.fd < 0) { |
ffe60014 DG |
438 | ret = 0; |
439 | goto end; | |
440 | } | |
441 | ||
6151a90f | 442 | DBG3("Relayd closing socket %d", rsock->sock.fd); |
00e2e675 | 443 | |
6151a90f JD |
444 | if (rsock->sock.ops) { |
445 | ret = rsock->sock.ops->close(&rsock->sock); | |
ffe60014 DG |
446 | } else { |
447 | /* Default call if no specific ops found. */ | |
6151a90f | 448 | ret = close(rsock->sock.fd); |
ffe60014 DG |
449 | if (ret < 0) { |
450 | PERROR("relayd_close default close"); | |
451 | } | |
452 | } | |
f96e4545 | 453 | rsock->sock.fd = -1; |
ffe60014 DG |
454 | |
455 | end: | |
456 | return ret; | |
00e2e675 DG |
457 | } |
458 | ||
459 | /* | |
460 | * Send data header structure to the relayd. | |
461 | */ | |
6151a90f | 462 | int relayd_send_data_hdr(struct lttcomm_relayd_sock *rsock, |
00e2e675 DG |
463 | struct lttcomm_relayd_data_hdr *hdr, size_t size) |
464 | { | |
465 | int ret; | |
466 | ||
467 | /* Code flow error. Safety net. */ | |
6151a90f | 468 | assert(rsock); |
00e2e675 DG |
469 | assert(hdr); |
470 | ||
f96e4545 MD |
471 | if (rsock->sock.fd < 0) { |
472 | return -ECONNRESET; | |
473 | } | |
474 | ||
8fd623e0 | 475 | DBG3("Relayd sending data header of size %zu", size); |
00e2e675 DG |
476 | |
477 | /* Again, safety net */ | |
478 | if (size == 0) { | |
479 | size = sizeof(struct lttcomm_relayd_data_hdr); | |
480 | } | |
481 | ||
482 | /* Only send data header. */ | |
6151a90f | 483 | ret = rsock->sock.ops->sendmsg(&rsock->sock, hdr, size, 0); |
00e2e675 | 484 | if (ret < 0) { |
8994307f | 485 | ret = -errno; |
00e2e675 DG |
486 | goto error; |
487 | } | |
488 | ||
489 | /* | |
490 | * The data MUST be sent right after that command for the receive on the | |
491 | * other end to match the size in the header. | |
492 | */ | |
493 | ||
494 | error: | |
495 | return ret; | |
496 | } | |
173af62f DG |
497 | |
498 | /* | |
499 | * Send close stream command to the relayd. | |
500 | */ | |
6151a90f | 501 | int relayd_send_close_stream(struct lttcomm_relayd_sock *rsock, uint64_t stream_id, |
173af62f DG |
502 | uint64_t last_net_seq_num) |
503 | { | |
504 | int ret; | |
505 | struct lttcomm_relayd_close_stream msg; | |
506 | struct lttcomm_relayd_generic_reply reply; | |
507 | ||
508 | /* Code flow error. Safety net. */ | |
6151a90f | 509 | assert(rsock); |
173af62f | 510 | |
77c7c900 | 511 | DBG("Relayd closing stream id %" PRIu64, stream_id); |
173af62f DG |
512 | |
513 | msg.stream_id = htobe64(stream_id); | |
514 | msg.last_net_seq_num = htobe64(last_net_seq_num); | |
515 | ||
516 | /* Send command */ | |
6151a90f | 517 | ret = send_command(rsock, RELAYD_CLOSE_STREAM, (void *) &msg, sizeof(msg), 0); |
173af62f DG |
518 | if (ret < 0) { |
519 | goto error; | |
520 | } | |
521 | ||
20275fe8 | 522 | /* Receive response */ |
6151a90f | 523 | ret = recv_reply(rsock, (void *) &reply, sizeof(reply)); |
173af62f DG |
524 | if (ret < 0) { |
525 | goto error; | |
526 | } | |
527 | ||
528 | reply.ret_code = be32toh(reply.ret_code); | |
529 | ||
530 | /* Return session id or negative ret code. */ | |
f73fabfd | 531 | if (reply.ret_code != LTTNG_OK) { |
bb63afd9 DG |
532 | ret = -1; |
533 | ERR("Relayd close stream replied error %d", reply.ret_code); | |
173af62f DG |
534 | } else { |
535 | /* Success */ | |
536 | ret = 0; | |
537 | } | |
538 | ||
77c7c900 | 539 | DBG("Relayd close stream id %" PRIu64 " successfully", stream_id); |
173af62f DG |
540 | |
541 | error: | |
542 | return ret; | |
543 | } | |
c8f59ee5 DG |
544 | |
545 | /* | |
546 | * Check for data availability for a given stream id. | |
547 | * | |
6d805429 | 548 | * Return 0 if NOT pending, 1 if so and a negative value on error. |
c8f59ee5 | 549 | */ |
6151a90f | 550 | int relayd_data_pending(struct lttcomm_relayd_sock *rsock, uint64_t stream_id, |
c8f59ee5 DG |
551 | uint64_t last_net_seq_num) |
552 | { | |
553 | int ret; | |
6d805429 | 554 | struct lttcomm_relayd_data_pending msg; |
c8f59ee5 DG |
555 | struct lttcomm_relayd_generic_reply reply; |
556 | ||
557 | /* Code flow error. Safety net. */ | |
6151a90f | 558 | assert(rsock); |
c8f59ee5 | 559 | |
6d805429 | 560 | DBG("Relayd data pending for stream id %" PRIu64, stream_id); |
c8f59ee5 DG |
561 | |
562 | msg.stream_id = htobe64(stream_id); | |
563 | msg.last_net_seq_num = htobe64(last_net_seq_num); | |
564 | ||
565 | /* Send command */ | |
6151a90f | 566 | ret = send_command(rsock, RELAYD_DATA_PENDING, (void *) &msg, |
c8f59ee5 DG |
567 | sizeof(msg), 0); |
568 | if (ret < 0) { | |
569 | goto error; | |
570 | } | |
571 | ||
20275fe8 | 572 | /* Receive response */ |
6151a90f | 573 | ret = recv_reply(rsock, (void *) &reply, sizeof(reply)); |
c8f59ee5 DG |
574 | if (ret < 0) { |
575 | goto error; | |
576 | } | |
577 | ||
578 | reply.ret_code = be32toh(reply.ret_code); | |
579 | ||
580 | /* Return session id or negative ret code. */ | |
581 | if (reply.ret_code >= LTTNG_OK) { | |
bb63afd9 | 582 | ERR("Relayd data pending replied error %d", reply.ret_code); |
c8f59ee5 DG |
583 | } |
584 | ||
585 | /* At this point, the ret code is either 1 or 0 */ | |
586 | ret = reply.ret_code; | |
587 | ||
6d805429 | 588 | DBG("Relayd data is %s pending for stream id %" PRIu64, |
9dd26bb9 | 589 | ret == 1 ? "" : "NOT", stream_id); |
c8f59ee5 DG |
590 | |
591 | error: | |
592 | return ret; | |
593 | } | |
594 | ||
595 | /* | |
596 | * Check on the relayd side for a quiescent state on the control socket. | |
597 | */ | |
6151a90f | 598 | int relayd_quiescent_control(struct lttcomm_relayd_sock *rsock, |
ad7051c0 | 599 | uint64_t metadata_stream_id) |
c8f59ee5 DG |
600 | { |
601 | int ret; | |
ad7051c0 | 602 | struct lttcomm_relayd_quiescent_control msg; |
c8f59ee5 DG |
603 | struct lttcomm_relayd_generic_reply reply; |
604 | ||
605 | /* Code flow error. Safety net. */ | |
6151a90f | 606 | assert(rsock); |
c8f59ee5 DG |
607 | |
608 | DBG("Relayd checking quiescent control state"); | |
609 | ||
ad7051c0 DG |
610 | msg.stream_id = htobe64(metadata_stream_id); |
611 | ||
c8f59ee5 | 612 | /* Send command */ |
6151a90f | 613 | ret = send_command(rsock, RELAYD_QUIESCENT_CONTROL, &msg, sizeof(msg), 0); |
c8f59ee5 DG |
614 | if (ret < 0) { |
615 | goto error; | |
616 | } | |
617 | ||
20275fe8 | 618 | /* Receive response */ |
6151a90f | 619 | ret = recv_reply(rsock, (void *) &reply, sizeof(reply)); |
c8f59ee5 DG |
620 | if (ret < 0) { |
621 | goto error; | |
622 | } | |
623 | ||
624 | reply.ret_code = be32toh(reply.ret_code); | |
625 | ||
626 | /* Return session id or negative ret code. */ | |
627 | if (reply.ret_code != LTTNG_OK) { | |
bb63afd9 DG |
628 | ret = -1; |
629 | ERR("Relayd quiescent control replied error %d", reply.ret_code); | |
c8f59ee5 DG |
630 | goto error; |
631 | } | |
632 | ||
633 | /* Control socket is quiescent */ | |
6d805429 | 634 | return 0; |
c8f59ee5 DG |
635 | |
636 | error: | |
637 | return ret; | |
638 | } | |
f7079f67 DG |
639 | |
640 | /* | |
641 | * Begin a data pending command for a specific session id. | |
642 | */ | |
6151a90f | 643 | int relayd_begin_data_pending(struct lttcomm_relayd_sock *rsock, uint64_t id) |
f7079f67 DG |
644 | { |
645 | int ret; | |
646 | struct lttcomm_relayd_begin_data_pending msg; | |
647 | struct lttcomm_relayd_generic_reply reply; | |
648 | ||
649 | /* Code flow error. Safety net. */ | |
6151a90f | 650 | assert(rsock); |
f7079f67 DG |
651 | |
652 | DBG("Relayd begin data pending"); | |
653 | ||
654 | msg.session_id = htobe64(id); | |
655 | ||
656 | /* Send command */ | |
6151a90f | 657 | ret = send_command(rsock, RELAYD_BEGIN_DATA_PENDING, &msg, sizeof(msg), 0); |
f7079f67 DG |
658 | if (ret < 0) { |
659 | goto error; | |
660 | } | |
661 | ||
20275fe8 | 662 | /* Receive response */ |
6151a90f | 663 | ret = recv_reply(rsock, (void *) &reply, sizeof(reply)); |
f7079f67 DG |
664 | if (ret < 0) { |
665 | goto error; | |
666 | } | |
667 | ||
668 | reply.ret_code = be32toh(reply.ret_code); | |
669 | ||
670 | /* Return session id or negative ret code. */ | |
671 | if (reply.ret_code != LTTNG_OK) { | |
bb63afd9 DG |
672 | ret = -1; |
673 | ERR("Relayd begin data pending replied error %d", reply.ret_code); | |
f7079f67 DG |
674 | goto error; |
675 | } | |
676 | ||
677 | return 0; | |
678 | ||
679 | error: | |
680 | return ret; | |
681 | } | |
682 | ||
683 | /* | |
684 | * End a data pending command for a specific session id. | |
685 | * | |
686 | * Return 0 on success and set is_data_inflight to 0 if no data is being | |
687 | * streamed or 1 if it is the case. | |
688 | */ | |
6151a90f | 689 | int relayd_end_data_pending(struct lttcomm_relayd_sock *rsock, uint64_t id, |
f7079f67 DG |
690 | unsigned int *is_data_inflight) |
691 | { | |
692 | int ret; | |
693 | struct lttcomm_relayd_end_data_pending msg; | |
694 | struct lttcomm_relayd_generic_reply reply; | |
695 | ||
696 | /* Code flow error. Safety net. */ | |
6151a90f | 697 | assert(rsock); |
f7079f67 DG |
698 | |
699 | DBG("Relayd end data pending"); | |
700 | ||
701 | msg.session_id = htobe64(id); | |
702 | ||
703 | /* Send command */ | |
6151a90f | 704 | ret = send_command(rsock, RELAYD_END_DATA_PENDING, &msg, sizeof(msg), 0); |
f7079f67 DG |
705 | if (ret < 0) { |
706 | goto error; | |
707 | } | |
708 | ||
20275fe8 | 709 | /* Receive response */ |
6151a90f | 710 | ret = recv_reply(rsock, (void *) &reply, sizeof(reply)); |
f7079f67 DG |
711 | if (ret < 0) { |
712 | goto error; | |
713 | } | |
714 | ||
715 | reply.ret_code = be32toh(reply.ret_code); | |
716 | if (reply.ret_code < 0) { | |
717 | ret = reply.ret_code; | |
718 | goto error; | |
719 | } | |
720 | ||
721 | *is_data_inflight = reply.ret_code; | |
722 | ||
723 | DBG("Relayd end data pending is data inflight: %d", reply.ret_code); | |
724 | ||
725 | return 0; | |
726 | ||
727 | error: | |
728 | return ret; | |
729 | } | |
1c20f0e2 JD |
730 | |
731 | /* | |
732 | * Send index to the relayd. | |
733 | */ | |
734 | int relayd_send_index(struct lttcomm_relayd_sock *rsock, | |
735 | struct lttng_packet_index *index, uint64_t relay_stream_id, | |
736 | uint64_t net_seq_num) | |
737 | { | |
738 | int ret; | |
739 | struct lttcomm_relayd_index msg; | |
740 | struct lttcomm_relayd_generic_reply reply; | |
741 | ||
742 | /* Code flow error. Safety net. */ | |
743 | assert(rsock); | |
744 | ||
745 | if (rsock->minor < 4) { | |
746 | DBG("Not sending indexes before protocol 2.4"); | |
747 | ret = 0; | |
748 | goto error; | |
749 | } | |
750 | ||
751 | DBG("Relayd sending index for stream ID %" PRIu64, relay_stream_id); | |
752 | ||
753 | msg.relay_stream_id = htobe64(relay_stream_id); | |
754 | msg.net_seq_num = htobe64(net_seq_num); | |
755 | ||
756 | /* The index is already in big endian. */ | |
757 | msg.packet_size = index->packet_size; | |
758 | msg.content_size = index->content_size; | |
759 | msg.timestamp_begin = index->timestamp_begin; | |
760 | msg.timestamp_end = index->timestamp_end; | |
761 | msg.events_discarded = index->events_discarded; | |
762 | msg.stream_id = index->stream_id; | |
763 | ||
764 | /* Send command */ | |
765 | ret = send_command(rsock, RELAYD_SEND_INDEX, &msg, sizeof(msg), 0); | |
766 | if (ret < 0) { | |
767 | goto error; | |
768 | } | |
769 | ||
770 | /* Receive response */ | |
771 | ret = recv_reply(rsock, (void *) &reply, sizeof(reply)); | |
772 | if (ret < 0) { | |
773 | goto error; | |
774 | } | |
775 | ||
776 | reply.ret_code = be32toh(reply.ret_code); | |
777 | ||
778 | /* Return session id or negative ret code. */ | |
779 | if (reply.ret_code != LTTNG_OK) { | |
780 | ret = -1; | |
781 | ERR("Relayd send index replied error %d", reply.ret_code); | |
782 | } else { | |
783 | /* Success */ | |
784 | ret = 0; | |
785 | } | |
786 | ||
787 | error: | |
788 | return ret; | |
789 | } |