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