ctf: rename bt_msg_iter::msg_iter to self_msg_iter
[babeltrace.git] / src / plugins / ctf / lttng-live / viewer-connection.c
CommitLineData
7cdc2bab 1/*
14f28187 2 * Copyright 2019 - Francis Deslauriers <francis.deslauriers@efficios.com>
7cdc2bab
MD
3 * Copyright 2016 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 * SOFTWARE.
22 */
23
2ece7dd0 24#define BT_COMP_LOG_SELF_COMP (viewer_connection->self_comp)
c01594de 25#define BT_LOG_OUTPUT_LEVEL (viewer_connection->log_level)
350ad6c1 26#define BT_LOG_TAG "PLUGIN/SRC.CTF.LTTNG-LIVE/VIEWER"
d9c39b0a 27#include "logging/comp-logging.h"
020bc26f 28
3c22a242
FD
29#include <fcntl.h>
30#include <stdbool.h>
7cdc2bab 31#include <stdint.h>
3c22a242 32#include <stdio.h>
7cdc2bab 33#include <stdlib.h>
3c22a242 34#include <sys/types.h>
7cdc2bab 35#include <unistd.h>
3c22a242 36
7cdc2bab 37#include <glib.h>
7cdc2bab 38
578e048b
MJ
39#include "compat/socket.h"
40#include "compat/endian.h"
41#include "compat/compiler.h"
42#include "common/common.h"
3fadfbc0 43#include <babeltrace2/babeltrace.h>
7cdc2bab 44
14f28187 45#include "lttng-live.h"
7cdc2bab
MD
46#include "viewer-connection.h"
47#include "lttng-viewer-abi.h"
48#include "data-stream.h"
49#include "metadata.h"
50
14f28187
FD
51static
52ssize_t lttng_live_recv(struct live_viewer_connection *viewer_connection,
4c66436f 53 void *buf, size_t len)
7cdc2bab
MD
54{
55 ssize_t ret;
56 size_t copied = 0, to_copy = len;
14f28187
FD
57 struct lttng_live_msg_iter *lttng_live_msg_iter =
58 viewer_connection->lttng_live_msg_iter;
1cb3cdd7 59 BT_SOCKET sock = viewer_connection->control_sock;
7cdc2bab
MD
60
61 do {
1cb3cdd7 62 ret = bt_socket_recv(sock, buf + copied, to_copy, 0);
7cdc2bab 63 if (ret > 0) {
f6ccaed9 64 BT_ASSERT(ret <= to_copy);
7cdc2bab
MD
65 copied += ret;
66 to_copy -= ret;
67 }
1cb3cdd7 68 if (ret == BT_SOCKET_ERROR && bt_socket_interrupted()) {
14f28187 69 if (!viewer_connection->in_query &&
9b4f9b42 70 lttng_live_graph_is_canceled(lttng_live_msg_iter)) {
4c66436f
MD
71 break;
72 } else {
73 continue;
74 }
75 }
76 } while (ret > 0 && to_copy > 0);
710d900e
FD
77
78 if (ret > 0) {
7cdc2bab 79 ret = copied;
710d900e
FD
80 }
81
1cb3cdd7 82 /* ret = 0 means orderly shutdown, ret == BT_SOCKET_ERROR is error. */
7cdc2bab
MD
83 return ret;
84}
85
14f28187
FD
86static
87ssize_t lttng_live_send(struct live_viewer_connection *viewer_connection,
4c66436f 88 const void *buf, size_t len)
7cdc2bab 89{
14f28187
FD
90 struct lttng_live_msg_iter *lttng_live_msg_iter =
91 viewer_connection->lttng_live_msg_iter;
1cb3cdd7 92 BT_SOCKET sock = viewer_connection->control_sock;
7cdc2bab
MD
93 ssize_t ret;
94
4c66436f 95 for (;;) {
1cb3cdd7
MJ
96 ret = bt_socket_send_nosigpipe(sock, buf, len);
97 if (ret == BT_SOCKET_ERROR && bt_socket_interrupted()) {
14f28187 98 if (!viewer_connection->in_query &&
9b4f9b42 99 lttng_live_graph_is_canceled(lttng_live_msg_iter)) {
4c66436f
MD
100 break;
101 } else {
102 continue;
103 }
104 } else {
105 break;
106 }
107 }
7cdc2bab
MD
108 return ret;
109}
110
14f28187
FD
111static
112int parse_url(struct live_viewer_connection *viewer_connection)
7cdc2bab 113{
94b828f3 114 char error_buf[256] = { 0 };
1419db2b
FD
115 bt_self_component *self_comp = viewer_connection->self_comp;
116 bt_self_component_class *self_comp_class = viewer_connection->self_comp_class;
94b828f3
MD
117 struct bt_common_lttng_live_url_parts lttng_live_url_parts = { 0 };
118 int ret = -1;
7cdc2bab 119 const char *path = viewer_connection->url->str;
7cdc2bab
MD
120
121 if (!path) {
122 goto end;
123 }
7cdc2bab 124
0f1979c3
FD
125 lttng_live_url_parts = bt_common_parse_lttng_live_url(path, error_buf,
126 sizeof(error_buf));
94b828f3 127 if (!lttng_live_url_parts.proto) {
1419db2b
FD
128 BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp,
129 self_comp_class,"Invalid LTTng live URL format: %s",
130 error_buf);
7cdc2bab
MD
131 goto end;
132 }
7cdc2bab 133
0f1979c3 134 viewer_connection->relay_hostname = lttng_live_url_parts.hostname;
94b828f3
MD
135 lttng_live_url_parts.hostname = NULL;
136
137 if (lttng_live_url_parts.port >= 0) {
138 viewer_connection->port = lttng_live_url_parts.port;
139 } else {
7cdc2bab
MD
140 viewer_connection->port = LTTNG_DEFAULT_NETWORK_VIEWER_PORT;
141 }
142
0f1979c3 143 viewer_connection->target_hostname = lttng_live_url_parts.target_hostname;
94b828f3
MD
144 lttng_live_url_parts.target_hostname = NULL;
145
146 if (lttng_live_url_parts.session_name) {
0f1979c3 147 viewer_connection->session_name = lttng_live_url_parts.session_name;
94b828f3 148 lttng_live_url_parts.session_name = NULL;
7cdc2bab
MD
149 }
150
2ece7dd0 151 BT_COMP_LOGI("Connecting to hostname : %s, port : %d, "
0f1979c3
FD
152 "target hostname : %s, session name : %s, proto : %s",
153 viewer_connection->relay_hostname->str,
154 viewer_connection->port,
155 !viewer_connection->target_hostname ?
156 "<none>" : viewer_connection->target_hostname->str,
157 !viewer_connection->session_name ?
158 "<none>" : viewer_connection->session_name->str,
159 lttng_live_url_parts.proto->str);
7cdc2bab
MD
160 ret = 0;
161
162end:
94b828f3 163 bt_common_destroy_lttng_live_url_parts(&lttng_live_url_parts);
7cdc2bab
MD
164 return ret;
165}
166
14f28187
FD
167static
168int lttng_live_handshake(struct live_viewer_connection *viewer_connection)
7cdc2bab
MD
169{
170 struct lttng_viewer_cmd cmd;
171 struct lttng_viewer_connect connect;
1419db2b
FD
172 bt_self_component_class *self_comp_class = viewer_connection->self_comp_class;
173 bt_self_component *self_comp = viewer_connection->self_comp;
bdcbd52e
JR
174 const size_t cmd_buf_len = sizeof(cmd) + sizeof(connect);
175 char cmd_buf[cmd_buf_len];
7cdc2bab
MD
176 int ret;
177 ssize_t ret_len;
178
179 cmd.cmd = htobe32(LTTNG_VIEWER_CONNECT);
180 cmd.data_size = htobe64((uint64_t) sizeof(connect));
181 cmd.cmd_version = htobe32(0);
182
183 connect.viewer_session_id = -1ULL; /* will be set on recv */
184 connect.major = htobe32(LTTNG_LIVE_MAJOR);
185 connect.minor = htobe32(LTTNG_LIVE_MINOR);
186 connect.type = htobe32(LTTNG_VIEWER_CLIENT_COMMAND);
187
bdcbd52e
JR
188 /*
189 * Merge the cmd and connection request to prevent a write-write
190 * sequence on the TCP socket. Otherwise, a delayed ACK will prevent the
191 * second write to be performed quickly in presence of Nagle's algorithm
192 */
193 memcpy(cmd_buf, &cmd, sizeof(cmd));
194 memcpy(cmd_buf + sizeof(cmd), &connect, sizeof(connect));
bdcbd52e 195 ret_len = lttng_live_send(viewer_connection, &cmd_buf, cmd_buf_len);
1cb3cdd7 196 if (ret_len == BT_SOCKET_ERROR) {
1419db2b
FD
197 BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp,
198 self_comp_class, "Error sending version: %s",
199 bt_socket_errormsg());
7cdc2bab
MD
200 goto error;
201 }
f6ccaed9
PP
202
203 BT_ASSERT(ret_len == cmd_buf_len);
7cdc2bab 204
4c66436f 205 ret_len = lttng_live_recv(viewer_connection, &connect, sizeof(connect));
7cdc2bab 206 if (ret_len == 0) {
1419db2b
FD
207 BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp,
208 self_comp_class, "Remote side has closed connection");
7cdc2bab
MD
209 goto error;
210 }
1cb3cdd7 211 if (ret_len == BT_SOCKET_ERROR) {
1419db2b
FD
212 BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp,
213 self_comp_class, "Error receiving version: %s",
214 bt_socket_errormsg());
7cdc2bab
MD
215 goto error;
216 }
f6ccaed9 217 BT_ASSERT(ret_len == sizeof(connect));
7cdc2bab 218
2ece7dd0 219 BT_COMP_LOGI("Received viewer session ID : %" PRIu64,
83c78c35 220 (uint64_t) be64toh(connect.viewer_session_id));
2ece7dd0 221 BT_COMP_LOGI("Relayd version : %u.%u", be32toh(connect.major),
7cdc2bab
MD
222 be32toh(connect.minor));
223
224 if (LTTNG_LIVE_MAJOR != be32toh(connect.major)) {
1419db2b
FD
225 BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp,
226 self_comp_class, "Incompatible lttng-relayd protocol");
7cdc2bab
MD
227 goto error;
228 }
229 /* Use the smallest protocol version implemented. */
230 if (LTTNG_LIVE_MINOR > be32toh(connect.minor)) {
231 viewer_connection->minor = be32toh(connect.minor);
232 } else {
233 viewer_connection->minor = LTTNG_LIVE_MINOR;
234 }
235 viewer_connection->major = LTTNG_LIVE_MAJOR;
236 ret = 0;
237 return ret;
238
239error:
7cdc2bab
MD
240 return -1;
241}
242
14f28187
FD
243static
244int lttng_live_connect_viewer(struct live_viewer_connection *viewer_connection)
7cdc2bab
MD
245{
246 struct hostent *host;
247 struct sockaddr_in server_addr;
1419db2b
FD
248 bt_self_component_class *self_comp_class = viewer_connection->self_comp_class;
249 bt_self_component *self_comp = viewer_connection->self_comp;
7cdc2bab
MD
250 int ret;
251
252 if (parse_url(viewer_connection)) {
1419db2b
FD
253 BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp,
254 self_comp_class, "Failed to parse URL");
7cdc2bab
MD
255 goto error;
256 }
257
94b828f3 258 host = gethostbyname(viewer_connection->relay_hostname->str);
7cdc2bab 259 if (!host) {
1419db2b
FD
260 BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp,
261 self_comp_class, "Cannot lookup hostname: hostname=\"%s\"",
94b828f3 262 viewer_connection->relay_hostname->str);
7cdc2bab
MD
263 goto error;
264 }
265
1cb3cdd7 266 if ((viewer_connection->control_sock = socket(AF_INET, SOCK_STREAM, 0)) == BT_INVALID_SOCKET) {
1419db2b
FD
267 BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp,
268 self_comp_class, "Socket creation failed: %s", bt_socket_errormsg());
7cdc2bab
MD
269 goto error;
270 }
271
272 server_addr.sin_family = AF_INET;
273 server_addr.sin_port = htons(viewer_connection->port);
274 server_addr.sin_addr = *((struct in_addr *) host->h_addr);
275 memset(&(server_addr.sin_zero), 0, 8);
276
277 if (connect(viewer_connection->control_sock, (struct sockaddr *) &server_addr,
1cb3cdd7 278 sizeof(struct sockaddr)) == BT_SOCKET_ERROR) {
1419db2b
FD
279 BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp,
280 self_comp_class, "Connection failed: %s",
281 bt_socket_errormsg());
7cdc2bab
MD
282 goto error;
283 }
284 if (lttng_live_handshake(viewer_connection)) {
1419db2b
FD
285 BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp,
286 self_comp_class, "Viewer handshake failed");
7cdc2bab
MD
287 goto error;
288 }
289
290 ret = 0;
291
292 return ret;
293
294error:
1cb3cdd7
MJ
295 if (viewer_connection->control_sock != BT_INVALID_SOCKET) {
296 if (bt_socket_close(viewer_connection->control_sock) == BT_SOCKET_ERROR) {
1419db2b
FD
297 BT_COMP_OR_COMP_CLASS_LOGE(self_comp, self_comp_class,
298 "Error closing socket: %s", bt_socket_errormsg());
7cdc2bab
MD
299 }
300 }
1cb3cdd7 301 viewer_connection->control_sock = BT_INVALID_SOCKET;
7cdc2bab
MD
302 return -1;
303}
304
14f28187
FD
305static
306void lttng_live_disconnect_viewer(
307 struct live_viewer_connection *viewer_connection)
7cdc2bab 308{
1cb3cdd7 309 if (viewer_connection->control_sock == BT_INVALID_SOCKET) {
7cdc2bab
MD
310 return;
311 }
1cb3cdd7 312 if (bt_socket_close(viewer_connection->control_sock) == BT_SOCKET_ERROR) {
1419db2b
FD
313 BT_COMP_LOGE("Error closing socket: %s",
314 bt_socket_errormsg());
1cb3cdd7 315 viewer_connection->control_sock = BT_INVALID_SOCKET;
7cdc2bab
MD
316 }
317}
318
7cdc2bab 319static
14f28187 320int list_update_session(bt_value *results,
7cdc2bab 321 const struct lttng_viewer_session *session,
c01594de 322 bool *_found, struct live_viewer_connection *viewer_connection)
7cdc2bab 323{
1419db2b
FD
324 bt_self_component_class *self_comp_class = viewer_connection->self_comp_class;
325 bt_self_component *self_comp = viewer_connection->self_comp;
f80e9ec1
FD
326 int ret = 0;
327 uint64_t i, len;
b19ff26f
PP
328 bt_value *map = NULL;
329 bt_value *hostname = NULL;
330 bt_value *session_name = NULL;
331 bt_value *btval = NULL;
7cdc2bab
MD
332 bool found = false;
333
393729a6 334 len = bt_value_array_get_length(results);
7cdc2bab
MD
335 for (i = 0; i < len; i++) {
336 const char *hostname_str = NULL;
337 const char *session_name_str = NULL;
338
f80e9ec1 339 map = bt_value_array_borrow_element_by_index(results, i);
14f28187 340 hostname = bt_value_map_borrow_entry_value(map, "target-hostname");
7cdc2bab 341 if (!hostname) {
1419db2b
FD
342 BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp,
343 self_comp_class,
344 "Error borrowing \"target-hostname\" entry.");
14f28187 345 ret = -1;
7cdc2bab
MD
346 goto end;
347 }
14f28187 348 session_name = bt_value_map_borrow_entry_value(map, "session-name");
7cdc2bab 349 if (!session_name) {
1419db2b
FD
350 BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp,
351 self_comp_class,
352 "Error borrowing \"session-name\" entry.");
14f28187 353 ret = -1;
7cdc2bab
MD
354 goto end;
355 }
601b0d3c
PP
356 hostname_str = bt_value_string_get(hostname);
357 session_name_str = bt_value_string_get(session_name);
7cdc2bab 358
2242b43d
PP
359 if (strcmp(session->hostname, hostname_str) == 0
360 && strcmp(session->session_name, session_name_str) == 0) {
7cdc2bab
MD
361 int64_t val;
362 uint32_t streams = be32toh(session->streams);
363 uint32_t clients = be32toh(session->clients);
364
365 found = true;
366
14f28187 367 btval = bt_value_map_borrow_entry_value(map, "stream-count");
7cdc2bab 368 if (!btval) {
1419db2b
FD
369 BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE(
370 self_comp, self_comp_class,
371 "Error borrowing \"stream-count\" entry.");
14f28187 372 ret = -1;
7cdc2bab
MD
373 goto end;
374 }
a91cb83e 375 val = bt_value_integer_unsigned_get(btval);
7cdc2bab
MD
376 /* sum */
377 val += streams;
a91cb83e 378 bt_value_integer_unsigned_set(btval, val);
7cdc2bab 379
14f28187 380 btval = bt_value_map_borrow_entry_value(map, "client-count");
7cdc2bab 381 if (!btval) {
1419db2b
FD
382 BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE(
383 self_comp, self_comp_class,
384 "Error borrowing \"client-count\" entry.");
14f28187 385 ret = -1;
7cdc2bab
MD
386 goto end;
387 }
a91cb83e 388 val = bt_value_integer_unsigned_get(btval);
7cdc2bab 389 /* max */
91d81473 390 val = bt_max_t(int64_t, clients, val);
a91cb83e 391 bt_value_integer_unsigned_set(btval, val);
7cdc2bab
MD
392 }
393
7cdc2bab
MD
394 if (found) {
395 break;
396 }
397 }
398end:
7cdc2bab
MD
399 *_found = found;
400 return ret;
401}
402
403static
14f28187 404int list_append_session(bt_value *results,
7cdc2bab 405 GString *base_url,
c01594de
PP
406 const struct lttng_viewer_session *session,
407 struct live_viewer_connection *viewer_connection)
7cdc2bab 408{
14f28187 409 int ret = 0;
1419db2b 410 bt_self_component_class *self_comp_class = viewer_connection->self_comp_class;
d24d5663
PP
411 bt_value_map_insert_entry_status insert_status;
412 bt_value_array_append_element_status append_status;
b19ff26f 413 bt_value *map = NULL;
7cdc2bab
MD
414 GString *url = NULL;
415 bool found = false;
416
417 /*
418 * If the session already exists, add the stream count to it,
419 * and do max of client counts.
420 */
c01594de 421 ret = list_update_session(results, session, &found, viewer_connection);
14f28187 422 if (ret || found) {
7cdc2bab
MD
423 goto end;
424 }
425
14f28187 426 map = bt_value_map_create();
7cdc2bab 427 if (!map) {
1419db2b
FD
428 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class,
429 "Error creating map value.");
14f28187 430 ret = -1;
7cdc2bab
MD
431 goto end;
432 }
433
434 if (base_url->len < 1) {
1419db2b
FD
435 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class,
436 "Error: base_url length smaller than 1.");
14f28187 437 ret = -1;
7cdc2bab
MD
438 goto end;
439 }
440 /*
441 * key = "url",
442 * value = <string>,
443 */
444 url = g_string_new(base_url->str);
445 g_string_append(url, "/host/");
446 g_string_append(url, session->hostname);
447 g_string_append_c(url, '/');
448 g_string_append(url, session->session_name);
449
d24d5663
PP
450 insert_status = bt_value_map_insert_string_entry(map, "url", url->str);
451 if (insert_status != BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK) {
1419db2b
FD
452 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class,
453 "Error inserting \"url\" entry.");
14f28187 454 ret = -1;
7cdc2bab
MD
455 goto end;
456 }
457
458 /*
459 * key = "target-hostname",
460 * value = <string>,
461 */
d24d5663 462 insert_status = bt_value_map_insert_string_entry(map, "target-hostname",
7cdc2bab 463 session->hostname);
d24d5663 464 if (insert_status != BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK) {
1419db2b
FD
465 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class,
466 "Error inserting \"target-hostname\" entry.");
14f28187 467 ret = -1;
7cdc2bab
MD
468 goto end;
469 }
470
471 /*
472 * key = "session-name",
473 * value = <string>,
474 */
d24d5663 475 insert_status = bt_value_map_insert_string_entry(map, "session-name",
7cdc2bab 476 session->session_name);
d24d5663 477 if (insert_status != BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK) {
1419db2b
FD
478 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class,
479 "Error inserting \"session-name\" entry.");
14f28187 480 ret = -1;
7cdc2bab
MD
481 goto end;
482 }
483
484 /*
485 * key = "timer-us",
486 * value = <integer>,
487 */
488 {
489 uint32_t live_timer = be32toh(session->live_timer);
490
a91cb83e 491 insert_status = bt_value_map_insert_unsigned_integer_entry(
fdd3a2da 492 map, "timer-us", live_timer);
d24d5663 493 if (insert_status != BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK) {
1419db2b
FD
494 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class,
495 "Error inserting \"timer-us\" entry.");
14f28187 496 ret = -1;
7cdc2bab
MD
497 goto end;
498 }
499 }
500
501 /*
502 * key = "stream-count",
503 * value = <integer>,
504 */
505 {
506 uint32_t streams = be32toh(session->streams);
507
a91cb83e 508 insert_status = bt_value_map_insert_unsigned_integer_entry(map,
fdd3a2da 509 "stream-count", streams);
d24d5663 510 if (insert_status != BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK) {
1419db2b
FD
511 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class,
512 "Error inserting \"stream-count\" entry.");
14f28187 513 ret = -1;
7cdc2bab
MD
514 goto end;
515 }
516 }
517
7cdc2bab
MD
518 /*
519 * key = "client-count",
520 * value = <integer>,
521 */
522 {
523 uint32_t clients = be32toh(session->clients);
524
a91cb83e 525 insert_status = bt_value_map_insert_unsigned_integer_entry(map,
fdd3a2da 526 "client-count", clients);
d24d5663 527 if (insert_status != BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK) {
1419db2b
FD
528 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class,
529 "Error inserting \"client-count\" entry.");
14f28187 530 ret = -1;
7cdc2bab
MD
531 goto end;
532 }
533 }
534
d24d5663
PP
535 append_status = bt_value_array_append_element(results, map);
536 if (append_status != BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK) {
1419db2b
FD
537 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class,
538 "Error appending map to results.");
14f28187
FD
539 ret = -1;
540 }
541
7cdc2bab
MD
542end:
543 if (url) {
14f28187 544 g_string_free(url, true);
7cdc2bab 545 }
c5b9b441 546 BT_VALUE_PUT_REF_AND_RESET(map);
7cdc2bab
MD
547 return ret;
548}
549
550/*
551 * Data structure returned:
552 *
553 * {
554 * <array> = {
555 * [n] = {
556 * <map> = {
557 * {
558 * key = "url",
559 * value = <string>,
560 * },
561 * {
562 * key = "target-hostname",
563 * value = <string>,
564 * },
565 * {
566 * key = "session-name",
567 * value = <string>,
568 * },
569 * {
570 * key = "timer-us",
571 * value = <integer>,
572 * },
573 * {
574 * key = "stream-count",
575 * value = <integer>,
576 * },
577 * {
578 * key = "client-count",
579 * value = <integer>,
580 * },
581 * },
582 * }
583 * }
584 */
585
586BT_HIDDEN
d24d5663 587bt_component_class_query_method_status live_viewer_connection_list_sessions(
14f28187
FD
588 struct live_viewer_connection *viewer_connection,
589 const bt_value **user_result)
7cdc2bab 590{
1419db2b 591 bt_self_component_class *self_comp_class = viewer_connection->self_comp_class;
d24d5663
PP
592 bt_component_class_query_method_status status =
593 BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_OK;
14f28187 594 bt_value *result = NULL;
7cdc2bab
MD
595 struct lttng_viewer_cmd cmd;
596 struct lttng_viewer_list_sessions list;
597 uint32_t i, sessions_count;
598 ssize_t ret_len;
599
14f28187
FD
600 result = bt_value_array_create();
601 if (!result) {
1419db2b
FD
602 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class,
603 "Error creating array");
d24d5663 604 status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_MEMORY_ERROR;
7cdc2bab
MD
605 goto error;
606 }
607
608 cmd.cmd = htobe32(LTTNG_VIEWER_LIST_SESSIONS);
609 cmd.data_size = htobe64((uint64_t) 0);
610 cmd.cmd_version = htobe32(0);
611
4c66436f 612 ret_len = lttng_live_send(viewer_connection, &cmd, sizeof(cmd));
1cb3cdd7 613 if (ret_len == BT_SOCKET_ERROR) {
1419db2b
FD
614 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class,
615 "Error sending cmd: %s", bt_socket_errormsg());
d24d5663 616 status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR;
7cdc2bab
MD
617 goto error;
618 }
f6ccaed9 619 BT_ASSERT(ret_len == sizeof(cmd));
7cdc2bab 620
4c66436f 621 ret_len = lttng_live_recv(viewer_connection, &list, sizeof(list));
7cdc2bab 622 if (ret_len == 0) {
1419db2b
FD
623 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class,
624 "Remote side has closed connection");
d24d5663 625 status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR;
7cdc2bab
MD
626 goto error;
627 }
1cb3cdd7 628 if (ret_len == BT_SOCKET_ERROR) {
1419db2b
FD
629 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class,
630 "Error receiving session list: %s",
631 bt_socket_errormsg());
d24d5663 632 status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR;
7cdc2bab
MD
633 goto error;
634 }
f6ccaed9 635 BT_ASSERT(ret_len == sizeof(list));
7cdc2bab
MD
636
637 sessions_count = be32toh(list.sessions_count);
638 for (i = 0; i < sessions_count; i++) {
639 struct lttng_viewer_session lsession;
640
14f28187
FD
641 ret_len = lttng_live_recv(viewer_connection, &lsession,
642 sizeof(lsession));
7cdc2bab 643 if (ret_len == 0) {
1419db2b
FD
644 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class,
645 "Remote side has closed connection");
d24d5663 646 status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR;
7cdc2bab
MD
647 goto error;
648 }
1cb3cdd7 649 if (ret_len == BT_SOCKET_ERROR) {
1419db2b
FD
650 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class,
651 "Error receiving session: %s",
652 bt_socket_errormsg());
d24d5663 653 status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR;
7cdc2bab
MD
654 goto error;
655 }
f6ccaed9 656 BT_ASSERT(ret_len == sizeof(lsession));
7cdc2bab
MD
657 lsession.hostname[LTTNG_VIEWER_HOST_NAME_MAX - 1] = '\0';
658 lsession.session_name[LTTNG_VIEWER_NAME_MAX - 1] = '\0';
14f28187 659 if (list_append_session(result, viewer_connection->url,
c01594de 660 &lsession, viewer_connection)) {
1419db2b
FD
661 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class,
662 "Error appending session");
d24d5663 663 status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR;
7cdc2bab
MD
664 goto error;
665 }
666 }
14f28187
FD
667
668 *user_result = result;
7cdc2bab
MD
669 goto end;
670error:
14f28187 671 BT_VALUE_PUT_REF_AND_RESET(result);
7cdc2bab 672end:
14f28187 673 return status;
7cdc2bab
MD
674}
675
676static
14f28187 677int lttng_live_query_session_ids(struct lttng_live_msg_iter *lttng_live_msg_iter)
7cdc2bab
MD
678{
679 struct lttng_viewer_cmd cmd;
680 struct lttng_viewer_list_sessions list;
681 struct lttng_viewer_session lsession;
682 uint32_t i, sessions_count;
683 ssize_t ret_len;
684 uint64_t session_id;
14f28187 685 struct live_viewer_connection *viewer_connection =
1419db2b
FD
686 lttng_live_msg_iter->viewer_connection;
687 bt_self_component *self_comp = viewer_connection->self_comp;
7cdc2bab
MD
688
689 cmd.cmd = htobe32(LTTNG_VIEWER_LIST_SESSIONS);
690 cmd.data_size = htobe64((uint64_t) 0);
691 cmd.cmd_version = htobe32(0);
692
4c66436f 693 ret_len = lttng_live_send(viewer_connection, &cmd, sizeof(cmd));
1cb3cdd7 694 if (ret_len == BT_SOCKET_ERROR) {
1419db2b
FD
695 BT_COMP_LOGE_APPEND_CAUSE(self_comp, "Error sending cmd: %s",
696 bt_socket_errormsg());
7cdc2bab
MD
697 goto error;
698 }
f6ccaed9 699 BT_ASSERT(ret_len == sizeof(cmd));
7cdc2bab 700
4c66436f 701 ret_len = lttng_live_recv(viewer_connection, &list, sizeof(list));
7cdc2bab 702 if (ret_len == 0) {
1419db2b
FD
703 BT_COMP_LOGE_APPEND_CAUSE(self_comp,
704 "Remote side has closed connection");
7cdc2bab
MD
705 goto error;
706 }
1cb3cdd7 707 if (ret_len == BT_SOCKET_ERROR) {
1419db2b
FD
708 BT_COMP_LOGE_APPEND_CAUSE(self_comp,
709 "Error receiving session list: %s",
710 bt_socket_errormsg());
7cdc2bab
MD
711 goto error;
712 }
f6ccaed9 713 BT_ASSERT(ret_len == sizeof(list));
7cdc2bab
MD
714
715 sessions_count = be32toh(list.sessions_count);
716 for (i = 0; i < sessions_count; i++) {
4c66436f 717 ret_len = lttng_live_recv(viewer_connection,
7cdc2bab
MD
718 &lsession, sizeof(lsession));
719 if (ret_len == 0) {
1419db2b
FD
720 BT_COMP_LOGE_APPEND_CAUSE(self_comp,
721 "Remote side has closed connection");
7cdc2bab
MD
722 goto error;
723 }
1cb3cdd7 724 if (ret_len == BT_SOCKET_ERROR) {
1419db2b
FD
725 BT_COMP_LOGE_APPEND_CAUSE(self_comp,
726 "Error receiving session: %s",
727 bt_socket_errormsg());
7cdc2bab
MD
728 goto error;
729 }
f6ccaed9 730 BT_ASSERT(ret_len == sizeof(lsession));
7cdc2bab
MD
731 lsession.hostname[LTTNG_VIEWER_HOST_NAME_MAX - 1] = '\0';
732 lsession.session_name[LTTNG_VIEWER_NAME_MAX - 1] = '\0';
733 session_id = be64toh(lsession.id);
734
2ece7dd0 735 BT_COMP_LOGI("Adding session %" PRIu64 " hostname: %s session_name: %s",
06994c71
MD
736 session_id, lsession.hostname, lsession.session_name);
737
7cdc2bab 738 if ((strncmp(lsession.session_name,
94b828f3 739 viewer_connection->session_name->str,
1cb3cdd7 740 LTTNG_VIEWER_NAME_MAX) == 0) && (strncmp(lsession.hostname,
94b828f3 741 viewer_connection->target_hostname->str,
1cb3cdd7 742 LTTNG_VIEWER_HOST_NAME_MAX) == 0)) {
14f28187 743 if (lttng_live_add_session(lttng_live_msg_iter, session_id,
06994c71
MD
744 lsession.hostname,
745 lsession.session_name)) {
1419db2b
FD
746 BT_COMP_LOGE_APPEND_CAUSE(self_comp,
747 "Failed to add live session");
7cdc2bab
MD
748 goto error;
749 }
750 }
751 }
752
753 return 0;
754
755error:
7cdc2bab
MD
756 return -1;
757}
758
759BT_HIDDEN
14f28187
FD
760int lttng_live_create_viewer_session(
761 struct lttng_live_msg_iter *lttng_live_msg_iter)
7cdc2bab
MD
762{
763 struct lttng_viewer_cmd cmd;
764 struct lttng_viewer_create_session_response resp;
765 ssize_t ret_len;
14f28187 766 struct live_viewer_connection *viewer_connection =
1419db2b
FD
767 lttng_live_msg_iter->viewer_connection;
768 bt_self_component *self_comp = viewer_connection->self_comp;
7cdc2bab
MD
769
770 cmd.cmd = htobe32(LTTNG_VIEWER_CREATE_SESSION);
771 cmd.data_size = htobe64((uint64_t) 0);
772 cmd.cmd_version = htobe32(0);
773
4c66436f 774 ret_len = lttng_live_send(viewer_connection, &cmd, sizeof(cmd));
1cb3cdd7 775 if (ret_len == BT_SOCKET_ERROR) {
1419db2b
FD
776 BT_COMP_LOGE_APPEND_CAUSE(self_comp, "Error sending cmd: %s",
777 bt_socket_errormsg());
7cdc2bab
MD
778 goto error;
779 }
f6ccaed9 780 BT_ASSERT(ret_len == sizeof(cmd));
7cdc2bab 781
4c66436f 782 ret_len = lttng_live_recv(viewer_connection, &resp, sizeof(resp));
7cdc2bab 783 if (ret_len == 0) {
1419db2b
FD
784 BT_COMP_LOGE_APPEND_CAUSE(self_comp,
785 "Remote side has closed connection");
7cdc2bab
MD
786 goto error;
787 }
1cb3cdd7 788 if (ret_len == BT_SOCKET_ERROR) {
1419db2b
FD
789 BT_COMP_LOGE_APPEND_CAUSE(self_comp,
790 "Error receiving create session reply: %s",
791 bt_socket_errormsg());
7cdc2bab
MD
792 goto error;
793 }
f6ccaed9 794 BT_ASSERT(ret_len == sizeof(resp));
7cdc2bab
MD
795
796 if (be32toh(resp.status) != LTTNG_VIEWER_CREATE_SESSION_OK) {
1419db2b
FD
797 BT_COMP_LOGE_APPEND_CAUSE(self_comp,
798 "Error creating viewer session");
7cdc2bab
MD
799 goto error;
800 }
14f28187 801 if (lttng_live_query_session_ids(lttng_live_msg_iter)) {
1419db2b
FD
802 BT_COMP_LOGE_APPEND_CAUSE(self_comp,
803 "Failed to query live viewer session ids");
7cdc2bab
MD
804 goto error;
805 }
806
807 return 0;
808
809error:
810 return -1;
811}
812
813static
814int receive_streams(struct lttng_live_session *session,
815 uint32_t stream_count)
816{
817 ssize_t ret_len;
818 uint32_t i;
14f28187 819 struct lttng_live_msg_iter *lttng_live_msg_iter =
0f1979c3 820 session->lttng_live_msg_iter;
14f28187 821 struct live_viewer_connection *viewer_connection =
0f1979c3 822 lttng_live_msg_iter->viewer_connection;
1419db2b 823 bt_self_component *self_comp = viewer_connection->self_comp;
7cdc2bab 824
2ece7dd0 825 BT_COMP_LOGI("Getting %" PRIu32 " new streams:", stream_count);
7cdc2bab
MD
826 for (i = 0; i < stream_count; i++) {
827 struct lttng_viewer_stream stream;
828 struct lttng_live_stream_iterator *live_stream;
829 uint64_t stream_id;
830 uint64_t ctf_trace_id;
831
4c66436f 832 ret_len = lttng_live_recv(viewer_connection, &stream, sizeof(stream));
7cdc2bab 833 if (ret_len == 0) {
1419db2b
FD
834 BT_COMP_LOGE_APPEND_CAUSE(self_comp,
835 "Remote side has closed connection");
7cdc2bab
MD
836 goto error;
837 }
1cb3cdd7 838 if (ret_len == BT_SOCKET_ERROR) {
1419db2b
FD
839 BT_COMP_LOGE_APPEND_CAUSE(self_comp,
840 "Error receiving stream");
7cdc2bab
MD
841 goto error;
842 }
f6ccaed9 843 BT_ASSERT(ret_len == sizeof(stream));
7cdc2bab
MD
844 stream.path_name[LTTNG_VIEWER_PATH_MAX - 1] = '\0';
845 stream.channel_name[LTTNG_VIEWER_NAME_MAX - 1] = '\0';
846 stream_id = be64toh(stream.id);
847 ctf_trace_id = be64toh(stream.ctf_trace_id);
848
849 if (stream.metadata_flag) {
2ece7dd0 850 BT_COMP_LOGI(" metadata stream %" PRIu64 " : %s/%s",
0f1979c3 851 stream_id, stream.path_name, stream.channel_name);
7cdc2bab 852 if (lttng_live_metadata_create_stream(session,
06994c71
MD
853 ctf_trace_id, stream_id,
854 stream.path_name)) {
1419db2b
FD
855 BT_COMP_LOGE_APPEND_CAUSE(self_comp,
856 "Error creating metadata stream");
7cdc2bab
MD
857
858 goto error;
859 }
d6e69534 860 session->lazy_stream_msg_init = true;
7cdc2bab 861 } else {
2ece7dd0 862 BT_COMP_LOGI(" stream %" PRIu64 " : %s/%s",
0f1979c3 863 stream_id, stream.path_name, stream.channel_name);
7cdc2bab
MD
864 live_stream = lttng_live_stream_iterator_create(session,
865 ctf_trace_id, stream_id);
866 if (!live_stream) {
1419db2b
FD
867 BT_COMP_LOGE_APPEND_CAUSE(self_comp,
868 "Error creating stream");
7cdc2bab
MD
869 goto error;
870 }
871 }
872 }
873 return 0;
874
875error:
876 return -1;
877}
878
879BT_HIDDEN
eee8e741
FD
880enum lttng_live_attach_session_status lttng_live_attach_session(
881 struct lttng_live_session *session)
7cdc2bab
MD
882{
883 struct lttng_viewer_cmd cmd;
eee8e741 884 enum lttng_live_attach_session_status attach_status;
7cdc2bab
MD
885 struct lttng_viewer_attach_session_request rq;
886 struct lttng_viewer_attach_session_response rp;
0f1979c3
FD
887 struct lttng_live_msg_iter *lttng_live_msg_iter =
888 session->lttng_live_msg_iter;
14f28187 889 struct live_viewer_connection *viewer_connection =
0f1979c3 890 lttng_live_msg_iter->viewer_connection;
1419db2b 891 bt_self_component *self_comp = viewer_connection->self_comp;
7cdc2bab
MD
892 uint64_t session_id = session->id;
893 uint32_t streams_count;
bdcbd52e
JR
894 const size_t cmd_buf_len = sizeof(cmd) + sizeof(rq);
895 char cmd_buf[cmd_buf_len];
0f1979c3 896 ssize_t ret_len;
7cdc2bab 897
7cdc2bab
MD
898 cmd.cmd = htobe32(LTTNG_VIEWER_ATTACH_SESSION);
899 cmd.data_size = htobe64((uint64_t) sizeof(rq));
900 cmd.cmd_version = htobe32(0);
901
902 memset(&rq, 0, sizeof(rq));
903 rq.session_id = htobe64(session_id);
904 // TODO: add cmd line parameter to select seek beginning
905 // rq.seek = htobe32(LTTNG_VIEWER_SEEK_BEGINNING);
906 rq.seek = htobe32(LTTNG_VIEWER_SEEK_LAST);
907
bdcbd52e
JR
908 /*
909 * Merge the cmd and connection request to prevent a write-write
910 * sequence on the TCP socket. Otherwise, a delayed ACK will prevent the
911 * second write to be performed quickly in presence of Nagle's algorithm.
912 */
913 memcpy(cmd_buf, &cmd, sizeof(cmd));
914 memcpy(cmd_buf + sizeof(cmd), &rq, sizeof(rq));
bdcbd52e 915 ret_len = lttng_live_send(viewer_connection, &cmd_buf, cmd_buf_len);
1cb3cdd7 916 if (ret_len == BT_SOCKET_ERROR) {
1419db2b
FD
917 BT_COMP_LOGE_APPEND_CAUSE(self_comp,
918 "Error sending attach request: %s",
919 bt_socket_errormsg());
7cdc2bab
MD
920 goto error;
921 }
7cdc2bab 922
f6ccaed9 923 BT_ASSERT(ret_len == cmd_buf_len);
4c66436f 924 ret_len = lttng_live_recv(viewer_connection, &rp, sizeof(rp));
7cdc2bab 925 if (ret_len == 0) {
1419db2b
FD
926 BT_COMP_LOGE_APPEND_CAUSE(self_comp,
927 "Remote side has closed connection");
7cdc2bab
MD
928 goto error;
929 }
1cb3cdd7 930 if (ret_len == BT_SOCKET_ERROR) {
1419db2b
FD
931 BT_COMP_LOGE_APPEND_CAUSE(self_comp,
932 "Error receiving attach response: %s",
933 bt_socket_errormsg());
7cdc2bab
MD
934 goto error;
935 }
f6ccaed9 936 BT_ASSERT(ret_len == sizeof(rp));
7cdc2bab
MD
937
938 streams_count = be32toh(rp.streams_count);
939 switch(be32toh(rp.status)) {
940 case LTTNG_VIEWER_ATTACH_OK:
941 break;
942 case LTTNG_VIEWER_ATTACH_UNK:
1419db2b
FD
943 BT_COMP_LOGE_APPEND_CAUSE(self_comp,
944 "Session id %" PRIu64 " is unknown", session_id);
7cdc2bab
MD
945 goto error;
946 case LTTNG_VIEWER_ATTACH_ALREADY:
1419db2b
FD
947 BT_COMP_LOGE_APPEND_CAUSE(self_comp,
948 "There is already a viewer attached to this session");
7cdc2bab
MD
949 goto error;
950 case LTTNG_VIEWER_ATTACH_NOT_LIVE:
1419db2b 951 BT_COMP_LOGE_APPEND_CAUSE(self_comp, "Not a live session");
7cdc2bab
MD
952 goto error;
953 case LTTNG_VIEWER_ATTACH_SEEK_ERR:
1419db2b 954 BT_COMP_LOGE_APPEND_CAUSE(self_comp, "Wrong seek parameter");
7cdc2bab
MD
955 goto error;
956 default:
1419db2b
FD
957 BT_COMP_LOGE_APPEND_CAUSE(self_comp,
958 "Unknown attach return code %u", be32toh(rp.status));
7cdc2bab
MD
959 goto error;
960 }
961
962 /* We receive the initial list of streams. */
963 if (receive_streams(session, streams_count)) {
1419db2b 964 BT_COMP_LOGE_APPEND_CAUSE(self_comp, "Error receiving streams");
7cdc2bab
MD
965 goto error;
966 }
967
968 session->attached = true;
969 session->new_streams_needed = false;
970
eee8e741
FD
971 attach_status = LTTNG_LIVE_ATTACH_SESSION_STATUS_OK;
972 goto end;
7cdc2bab
MD
973
974error:
eee8e741
FD
975 attach_status = LTTNG_LIVE_ATTACH_SESSION_STATUS_ERROR;
976
977end:
978 return attach_status;
7cdc2bab
MD
979}
980
981BT_HIDDEN
982int lttng_live_detach_session(struct lttng_live_session *session)
983{
984 struct lttng_viewer_cmd cmd;
985 struct lttng_viewer_detach_session_request rq;
986 struct lttng_viewer_detach_session_response rp;
987 ssize_t ret_len;
0f1979c3
FD
988 struct lttng_live_msg_iter *lttng_live_msg_iter =
989 session->lttng_live_msg_iter;
14f28187 990 struct live_viewer_connection *viewer_connection =
0f1979c3 991 lttng_live_msg_iter->viewer_connection;
7cdc2bab 992 uint64_t session_id = session->id;
bdcbd52e
JR
993 const size_t cmd_buf_len = sizeof(cmd) + sizeof(rq);
994 char cmd_buf[cmd_buf_len];
7cdc2bab
MD
995
996 if (!session->attached) {
997 return 0;
998 }
999
1000 cmd.cmd = htobe32(LTTNG_VIEWER_DETACH_SESSION);
1001 cmd.data_size = htobe64((uint64_t) sizeof(rq));
1002 cmd.cmd_version = htobe32(0);
1003
1004 memset(&rq, 0, sizeof(rq));
1005 rq.session_id = htobe64(session_id);
1006
bdcbd52e
JR
1007 /*
1008 * Merge the cmd and connection request to prevent a write-write
1009 * sequence on the TCP socket. Otherwise, a delayed ACK will prevent the
1010 * second write to be performed quickly in presence of Nagle's algorithm.
1011 */
1012 memcpy(cmd_buf, &cmd, sizeof(cmd));
1013 memcpy(cmd_buf + sizeof(cmd), &rq, sizeof(rq));
bdcbd52e 1014 ret_len = lttng_live_send(viewer_connection, &cmd_buf, cmd_buf_len);
1cb3cdd7 1015 if (ret_len == BT_SOCKET_ERROR) {
1419db2b
FD
1016 BT_COMP_LOGE("Error sending detach request: %s",
1017 bt_socket_errormsg());
7cdc2bab
MD
1018 goto error;
1019 }
7cdc2bab 1020
f6ccaed9 1021 BT_ASSERT(ret_len == cmd_buf_len);
4c66436f 1022 ret_len = lttng_live_recv(viewer_connection, &rp, sizeof(rp));
7cdc2bab 1023 if (ret_len == 0) {
1419db2b 1024 BT_COMP_LOGE("Remote side has closed connection");
7cdc2bab
MD
1025 goto error;
1026 }
1cb3cdd7 1027 if (ret_len == BT_SOCKET_ERROR) {
1419db2b
FD
1028 BT_COMP_LOGE("Error receiving detach response: %s",
1029 bt_socket_errormsg());
7cdc2bab
MD
1030 goto error;
1031 }
f6ccaed9 1032 BT_ASSERT(ret_len == sizeof(rp));
7cdc2bab
MD
1033
1034 switch(be32toh(rp.status)) {
1035 case LTTNG_VIEWER_DETACH_SESSION_OK:
1036 break;
1037 case LTTNG_VIEWER_DETACH_SESSION_UNK:
2ece7dd0 1038 BT_COMP_LOGW("Session id %" PRIu64 " is unknown", session_id);
7cdc2bab
MD
1039 goto error;
1040 case LTTNG_VIEWER_DETACH_SESSION_ERR:
2ece7dd0 1041 BT_COMP_LOGW("Error detaching session id %" PRIu64 "", session_id);
7cdc2bab
MD
1042 goto error;
1043 default:
2ece7dd0 1044 BT_COMP_LOGE("Unknown detach return code %u", be32toh(rp.status));
7cdc2bab
MD
1045 goto error;
1046 }
1047
1048 session->attached = false;
1049
1050 return 0;
1051
1052error:
1053 return -1;
1054}
1055
1056BT_HIDDEN
c28512ab
FD
1057enum lttng_live_get_one_metadata_status lttng_live_get_one_metadata_packet(
1058 struct lttng_live_trace *trace, FILE *fp, size_t *reply_len)
7cdc2bab
MD
1059{
1060 uint64_t len = 0;
c28512ab 1061 enum lttng_live_get_one_metadata_status metadata_status;
7cdc2bab
MD
1062 struct lttng_viewer_cmd cmd;
1063 struct lttng_viewer_get_metadata rq;
1064 struct lttng_viewer_metadata_packet rp;
1065 char *data = NULL;
1066 ssize_t ret_len;
1067 struct lttng_live_session *session = trace->session;
0f1979c3
FD
1068 struct lttng_live_msg_iter *lttng_live_msg_iter =
1069 session->lttng_live_msg_iter;
7cdc2bab 1070 struct lttng_live_metadata *metadata = trace->metadata;
14f28187 1071 struct live_viewer_connection *viewer_connection =
0f1979c3 1072 lttng_live_msg_iter->viewer_connection;
1419db2b 1073 bt_self_component *self_comp = viewer_connection->self_comp;
bdcbd52e
JR
1074 const size_t cmd_buf_len = sizeof(cmd) + sizeof(rq);
1075 char cmd_buf[cmd_buf_len];
7cdc2bab
MD
1076
1077 rq.stream_id = htobe64(metadata->stream_id);
1078 cmd.cmd = htobe32(LTTNG_VIEWER_GET_METADATA);
1079 cmd.data_size = htobe64((uint64_t) sizeof(rq));
1080 cmd.cmd_version = htobe32(0);
1081
bdcbd52e
JR
1082 /*
1083 * Merge the cmd and connection request to prevent a write-write
1084 * sequence on the TCP socket. Otherwise, a delayed ACK will prevent the
1085 * second write to be performed quickly in presence of Nagle's algorithm.
1086 */
1087 memcpy(cmd_buf, &cmd, sizeof(cmd));
1088 memcpy(cmd_buf + sizeof(cmd), &rq, sizeof(rq));
bdcbd52e 1089 ret_len = lttng_live_send(viewer_connection, &cmd_buf, cmd_buf_len);
1cb3cdd7 1090 if (ret_len == BT_SOCKET_ERROR) {
1419db2b
FD
1091 BT_COMP_LOGE_APPEND_CAUSE(self_comp,
1092 "Error sending get_metadata request: %s",
1093 bt_socket_errormsg());
7cdc2bab
MD
1094 goto error;
1095 }
7cdc2bab 1096
f6ccaed9 1097 BT_ASSERT(ret_len == cmd_buf_len);
4c66436f 1098 ret_len = lttng_live_recv(viewer_connection, &rp, sizeof(rp));
7cdc2bab 1099 if (ret_len == 0) {
1419db2b
FD
1100 BT_COMP_LOGE_APPEND_CAUSE(self_comp,
1101 "Remote side has closed connection");
7cdc2bab
MD
1102 goto error;
1103 }
1cb3cdd7 1104 if (ret_len == BT_SOCKET_ERROR) {
1419db2b
FD
1105 BT_COMP_LOGE_APPEND_CAUSE(self_comp,
1106 "Error receiving get_metadata response: %s",
1107 bt_socket_errormsg());
7cdc2bab
MD
1108 goto error;
1109 }
f6ccaed9 1110 BT_ASSERT(ret_len == sizeof(rp));
7cdc2bab
MD
1111
1112 switch (be32toh(rp.status)) {
1113 case LTTNG_VIEWER_METADATA_OK:
1419db2b 1114 BT_COMP_LOGD("Received get_metadata response: ok");
7cdc2bab
MD
1115 break;
1116 case LTTNG_VIEWER_NO_NEW_METADATA:
c28512ab
FD
1117 BT_COMP_LOGD("Received get_metadata response: no new");
1118 metadata_status = LTTNG_LIVE_GET_ONE_METADATA_STATUS_END;
7cdc2bab
MD
1119 goto end;
1120 case LTTNG_VIEWER_METADATA_ERR:
c28512ab
FD
1121 /*
1122 * The Relayd cannot find this stream id. Maybe its
1123 * gone already. This can happen in short lived UST app
1124 * in a per-pid session.
1125 */
1126 BT_COMP_LOGD("Received get_metadata response: error");
1127 metadata_status = LTTNG_LIVE_GET_ONE_METADATA_STATUS_CLOSED;
1128 goto end;
7cdc2bab 1129 default:
1419db2b
FD
1130 BT_COMP_LOGE_APPEND_CAUSE(self_comp,
1131 "Received get_metadata response: unknown");
7cdc2bab
MD
1132 goto error;
1133 }
1134
1135 len = be64toh(rp.len);
2ece7dd0 1136 BT_COMP_LOGD("Writing %" PRIu64" bytes to metadata", len);
7cdc2bab 1137 if (len <= 0) {
1419db2b
FD
1138 BT_COMP_LOGE_APPEND_CAUSE(self_comp,
1139 "Erroneous response length");
7cdc2bab
MD
1140 goto error;
1141 }
1142
91d81473 1143 data = calloc(1, len);
7cdc2bab 1144 if (!data) {
1419db2b
FD
1145 BT_COMP_LOGE_APPEND_CAUSE_ERRNO(self_comp,
1146 "Failed to allocate data buffer", ".");
7cdc2bab
MD
1147 goto error;
1148 }
4c66436f 1149 ret_len = lttng_live_recv(viewer_connection, data, len);
7cdc2bab 1150 if (ret_len == 0) {
2ece7dd0 1151 BT_COMP_LOGI("Remote side has closed connection");
7cdc2bab
MD
1152 goto error_free_data;
1153 }
1cb3cdd7 1154 if (ret_len == BT_SOCKET_ERROR) {
1419db2b
FD
1155 BT_COMP_LOGE_APPEND_CAUSE(self_comp,
1156 "Error receiving trace packet: %s", bt_socket_errormsg());
7cdc2bab
MD
1157 goto error_free_data;
1158 }
f6ccaed9 1159 BT_ASSERT(ret_len == len);
7cdc2bab
MD
1160
1161 do {
1162 ret_len = fwrite(data, 1, len, fp);
1163 } while (ret_len < 0 && errno == EINTR);
1164 if (ret_len < 0) {
1419db2b
FD
1165 BT_COMP_LOGE_APPEND_CAUSE(self_comp,
1166 "Writing in the metadata file stream");
7cdc2bab
MD
1167 goto error_free_data;
1168 }
f6ccaed9 1169 BT_ASSERT(ret_len == len);
7cdc2bab 1170 free(data);
c28512ab
FD
1171 *reply_len = len;
1172 metadata_status = LTTNG_LIVE_GET_ONE_METADATA_STATUS_OK;
1173
1174 goto end;
7cdc2bab
MD
1175
1176error_free_data:
1177 free(data);
1178error:
c28512ab
FD
1179 metadata_status = LTTNG_LIVE_GET_ONE_METADATA_STATUS_ERROR;
1180
1181end:
1182 return metadata_status;
7cdc2bab
MD
1183}
1184
1185/*
1186 * Assign the fields from a lttng_viewer_index to a packet_index.
1187 */
1188static
1189void lttng_index_to_packet_index(struct lttng_viewer_index *lindex,
1190 struct packet_index *pindex)
1191{
f6ccaed9
PP
1192 BT_ASSERT(lindex);
1193 BT_ASSERT(pindex);
7cdc2bab
MD
1194
1195 pindex->offset = be64toh(lindex->offset);
1196 pindex->packet_size = be64toh(lindex->packet_size);
1197 pindex->content_size = be64toh(lindex->content_size);
1198 pindex->ts_cycles.timestamp_begin = be64toh(lindex->timestamp_begin);
1199 pindex->ts_cycles.timestamp_end = be64toh(lindex->timestamp_end);
1200 pindex->events_discarded = be64toh(lindex->events_discarded);
1201}
1202
1203BT_HIDDEN
14f28187
FD
1204enum lttng_live_iterator_status lttng_live_get_next_index(
1205 struct lttng_live_msg_iter *lttng_live_msg_iter,
7cdc2bab
MD
1206 struct lttng_live_stream_iterator *stream,
1207 struct packet_index *index)
1208{
1209 struct lttng_viewer_cmd cmd;
1210 struct lttng_viewer_get_next_index rq;
7cdc2bab 1211 struct lttng_viewer_index rp;
0f1979c3 1212 enum lttng_live_iterator_status retstatus = LTTNG_LIVE_ITERATOR_STATUS_OK;
14f28187 1213 struct live_viewer_connection *viewer_connection =
1419db2b
FD
1214 lttng_live_msg_iter->viewer_connection;
1215 bt_self_component *self_comp = viewer_connection->self_comp;
7cdc2bab 1216 struct lttng_live_trace *trace = stream->trace;
bdcbd52e
JR
1217 const size_t cmd_buf_len = sizeof(cmd) + sizeof(rq);
1218 char cmd_buf[cmd_buf_len];
0f1979c3
FD
1219 uint32_t flags, status;
1220 ssize_t ret_len;
7cdc2bab
MD
1221
1222 cmd.cmd = htobe32(LTTNG_VIEWER_GET_NEXT_INDEX);
1223 cmd.data_size = htobe64((uint64_t) sizeof(rq));
1224 cmd.cmd_version = htobe32(0);
1225
bdcbd52e 1226
7cdc2bab
MD
1227 memset(&rq, 0, sizeof(rq));
1228 rq.stream_id = htobe64(stream->viewer_stream_id);
1229
bdcbd52e
JR
1230 /*
1231 * Merge the cmd and connection request to prevent a write-write
1232 * sequence on the TCP socket. Otherwise, a delayed ACK will prevent the
1233 * second write to be performed quickly in presence of Nagle's algorithm.
1234 */
1235 memcpy(cmd_buf, &cmd, sizeof(cmd));
1236 memcpy(cmd_buf + sizeof(cmd), &rq, sizeof(rq));
bdcbd52e 1237 ret_len = lttng_live_send(viewer_connection, &cmd_buf, cmd_buf_len);
1cb3cdd7 1238 if (ret_len == BT_SOCKET_ERROR) {
1419db2b
FD
1239 BT_COMP_LOGE_APPEND_CAUSE(self_comp,
1240 "Error sending get_next_index request: %s",
1241 bt_socket_errormsg());
7cdc2bab
MD
1242 goto error;
1243 }
7cdc2bab 1244
f6ccaed9 1245 BT_ASSERT(ret_len == cmd_buf_len);
4c66436f 1246 ret_len = lttng_live_recv(viewer_connection, &rp, sizeof(rp));
7cdc2bab 1247 if (ret_len == 0) {
1419db2b
FD
1248 BT_COMP_LOGE_APPEND_CAUSE(self_comp,
1249 "Remote side has closed connection");
7cdc2bab
MD
1250 goto error;
1251 }
1cb3cdd7 1252 if (ret_len == BT_SOCKET_ERROR) {
1419db2b
FD
1253 BT_COMP_LOGE_APPEND_CAUSE(self_comp,
1254 "Error receiving get_next_index response: %s",
1255 bt_socket_errormsg());
7cdc2bab
MD
1256 goto error;
1257 }
f6ccaed9 1258 BT_ASSERT(ret_len == sizeof(rp));
7cdc2bab
MD
1259
1260 flags = be32toh(rp.flags);
1261 status = be32toh(rp.status);
1262
1263 switch (status) {
1264 case LTTNG_VIEWER_INDEX_INACTIVE:
1265 {
1266 uint64_t ctf_stream_class_id;
1267
1419db2b 1268 BT_COMP_LOGD("Received get_next_index response: inactive");
7cdc2bab
MD
1269 memset(index, 0, sizeof(struct packet_index));
1270 index->ts_cycles.timestamp_end = be64toh(rp.timestamp_end);
14f28187 1271 stream->current_inactivity_ts = index->ts_cycles.timestamp_end;
7cdc2bab
MD
1272 ctf_stream_class_id = be64toh(rp.stream_id);
1273 if (stream->ctf_stream_class_id != -1ULL) {
f6ccaed9 1274 BT_ASSERT(stream->ctf_stream_class_id ==
7cdc2bab
MD
1275 ctf_stream_class_id);
1276 } else {
1277 stream->ctf_stream_class_id = ctf_stream_class_id;
1278 }
1279 stream->state = LTTNG_LIVE_STREAM_QUIESCENT;
1280 break;
1281 }
1282 case LTTNG_VIEWER_INDEX_OK:
1283 {
1284 uint64_t ctf_stream_class_id;
1285
1419db2b 1286 BT_COMP_LOGD("Received get_next_index response: OK");
7cdc2bab
MD
1287 lttng_index_to_packet_index(&rp, index);
1288 ctf_stream_class_id = be64toh(rp.stream_id);
1289 if (stream->ctf_stream_class_id != -1ULL) {
f6ccaed9 1290 BT_ASSERT(stream->ctf_stream_class_id ==
7cdc2bab
MD
1291 ctf_stream_class_id);
1292 } else {
1293 stream->ctf_stream_class_id = ctf_stream_class_id;
1294 }
1295
1296 stream->state = LTTNG_LIVE_STREAM_ACTIVE_DATA;
7cdc2bab
MD
1297
1298 if (flags & LTTNG_VIEWER_FLAG_NEW_METADATA) {
1419db2b 1299 BT_COMP_LOGD("Received get_next_index response: new metadata needed");
7cdc2bab
MD
1300 trace->new_metadata_needed = true;
1301 }
1302 if (flags & LTTNG_VIEWER_FLAG_NEW_STREAM) {
1419db2b 1303 BT_COMP_LOGD("Received get_next_index response: new streams needed");
14f28187 1304 lttng_live_need_new_streams(lttng_live_msg_iter);
7cdc2bab
MD
1305 }
1306 break;
1307 }
1308 case LTTNG_VIEWER_INDEX_RETRY:
1419db2b 1309 BT_COMP_LOGD("Received get_next_index response: retry");
7cdc2bab 1310 memset(index, 0, sizeof(struct packet_index));
14f28187 1311 retstatus = LTTNG_LIVE_ITERATOR_STATUS_AGAIN;
7cdc2bab
MD
1312 stream->state = LTTNG_LIVE_STREAM_ACTIVE_NO_DATA;
1313 goto end;
1314 case LTTNG_VIEWER_INDEX_HUP:
1419db2b 1315 BT_COMP_LOGD("Received get_next_index response: stream hung up");
7cdc2bab
MD
1316 memset(index, 0, sizeof(struct packet_index));
1317 index->offset = EOF;
14f28187 1318 retstatus = LTTNG_LIVE_ITERATOR_STATUS_END;
7cdc2bab 1319 stream->state = LTTNG_LIVE_STREAM_EOF;
4a39caef 1320 stream->has_stream_hung_up = true;
7cdc2bab
MD
1321 break;
1322 case LTTNG_VIEWER_INDEX_ERR:
1419db2b 1323 BT_COMP_LOGD("Received get_next_index response: error");
7cdc2bab
MD
1324 memset(index, 0, sizeof(struct packet_index));
1325 stream->state = LTTNG_LIVE_STREAM_ACTIVE_NO_DATA;
1326 goto error;
1327 default:
1419db2b 1328 BT_COMP_LOGD("Received get_next_index response: unknown value");
7cdc2bab
MD
1329 memset(index, 0, sizeof(struct packet_index));
1330 stream->state = LTTNG_LIVE_STREAM_ACTIVE_NO_DATA;
1331 goto error;
1332 }
1333end:
1334 return retstatus;
1335
1336error:
9b4f9b42 1337 if (lttng_live_graph_is_canceled(lttng_live_msg_iter)) {
14f28187 1338 retstatus = LTTNG_LIVE_ITERATOR_STATUS_AGAIN;
4c66436f 1339 } else {
14f28187 1340 retstatus = LTTNG_LIVE_ITERATOR_STATUS_ERROR;
4c66436f 1341 }
7cdc2bab
MD
1342 return retstatus;
1343}
1344
1345BT_HIDDEN
14f28187
FD
1346enum bt_msg_iter_medium_status lttng_live_get_stream_bytes(
1347 struct lttng_live_msg_iter *lttng_live_msg_iter,
1348 struct lttng_live_stream_iterator *stream, uint8_t *buf,
1349 uint64_t offset, uint64_t req_len, uint64_t *recv_len)
7cdc2bab 1350{
d6e69534 1351 enum bt_msg_iter_medium_status retstatus = BT_MSG_ITER_MEDIUM_STATUS_OK;
0f1979c3 1352 struct lttng_viewer_trace_packet rp;
7cdc2bab
MD
1353 struct lttng_viewer_cmd cmd;
1354 struct lttng_viewer_get_packet rq;
14f28187 1355 struct live_viewer_connection *viewer_connection =
1419db2b
FD
1356 lttng_live_msg_iter->viewer_connection;
1357 bt_self_component *self_comp = viewer_connection->self_comp;
7cdc2bab 1358 struct lttng_live_trace *trace = stream->trace;
bdcbd52e
JR
1359 const size_t cmd_buf_len = sizeof(cmd) + sizeof(rq);
1360 char cmd_buf[cmd_buf_len];
0f1979c3
FD
1361 uint32_t flags, status;
1362 ssize_t ret_len;
7cdc2bab 1363
2ece7dd0 1364 BT_COMP_LOGD("lttng_live_get_stream_bytes: offset=%" PRIu64 ", req_len=%" PRIu64,
7cdc2bab
MD
1365 offset, req_len);
1366 cmd.cmd = htobe32(LTTNG_VIEWER_GET_PACKET);
1367 cmd.data_size = htobe64((uint64_t) sizeof(rq));
1368 cmd.cmd_version = htobe32(0);
1369
1370 memset(&rq, 0, sizeof(rq));
1371 rq.stream_id = htobe64(stream->viewer_stream_id);
1372 rq.offset = htobe64(offset);
1373 rq.len = htobe32(req_len);
1374
bdcbd52e
JR
1375 /*
1376 * Merge the cmd and connection request to prevent a write-write
1377 * sequence on the TCP socket. Otherwise, a delayed ACK will prevent the
1378 * second write to be performed quickly in presence of Nagle's algorithm.
1379 */
1380 memcpy(cmd_buf, &cmd, sizeof(cmd));
1381 memcpy(cmd_buf + sizeof(cmd), &rq, sizeof(rq));
bdcbd52e 1382 ret_len = lttng_live_send(viewer_connection, &cmd_buf, cmd_buf_len);
1cb3cdd7 1383 if (ret_len == BT_SOCKET_ERROR) {
1419db2b
FD
1384 BT_COMP_LOGE_APPEND_CAUSE(self_comp,
1385 "Error sending get_data_packet request: %s",
1386 bt_socket_errormsg());
7cdc2bab
MD
1387 goto error;
1388 }
7cdc2bab 1389
f6ccaed9 1390 BT_ASSERT(ret_len == cmd_buf_len);
4c66436f 1391 ret_len = lttng_live_recv(viewer_connection, &rp, sizeof(rp));
7cdc2bab 1392 if (ret_len == 0) {
1419db2b
FD
1393 BT_COMP_LOGE_APPEND_CAUSE(self_comp,
1394 "Remote side has closed connection");
7cdc2bab
MD
1395 goto error;
1396 }
1cb3cdd7 1397 if (ret_len == BT_SOCKET_ERROR) {
1419db2b
FD
1398 BT_COMP_LOGE_APPEND_CAUSE(self_comp,
1399 "Socket error receiving get_data response: %s",
1400 bt_socket_errormsg());
7cdc2bab
MD
1401 goto error;
1402 }
1403 if (ret_len != sizeof(rp)) {
1419db2b
FD
1404 BT_COMP_LOGE_APPEND_CAUSE(self_comp, "get_data_packet: "
1405 "expected %zu, received %zd", sizeof(rp), ret_len);
7cdc2bab
MD
1406 goto error;
1407 }
1408
1409 flags = be32toh(rp.flags);
1410 status = be32toh(rp.status);
1411
1412 switch (status) {
1413 case LTTNG_VIEWER_GET_PACKET_OK:
1414 req_len = be32toh(rp.len);
1419db2b
FD
1415 BT_COMP_LOGD("Received get_data_packet response: Ok, "
1416 "packet size : %" PRIu64 "", req_len);
7cdc2bab
MD
1417 break;
1418 case LTTNG_VIEWER_GET_PACKET_RETRY:
1419 /* Unimplemented by relay daemon */
1419db2b 1420 BT_COMP_LOGD("Received get_data_packet response: retry");
d6e69534 1421 retstatus = BT_MSG_ITER_MEDIUM_STATUS_AGAIN;
7cdc2bab
MD
1422 goto end;
1423 case LTTNG_VIEWER_GET_PACKET_ERR:
1424 if (flags & LTTNG_VIEWER_FLAG_NEW_METADATA) {
2ece7dd0 1425 BT_COMP_LOGD("get_data_packet: new metadata needed, try again later");
7cdc2bab
MD
1426 trace->new_metadata_needed = true;
1427 }
1428 if (flags & LTTNG_VIEWER_FLAG_NEW_STREAM) {
2ece7dd0 1429 BT_COMP_LOGD("get_data_packet: new streams needed, try again later");
14f28187 1430 lttng_live_need_new_streams(lttng_live_msg_iter);
7cdc2bab
MD
1431 }
1432 if (flags & (LTTNG_VIEWER_FLAG_NEW_METADATA
1433 | LTTNG_VIEWER_FLAG_NEW_STREAM)) {
d6e69534 1434 retstatus = BT_MSG_ITER_MEDIUM_STATUS_AGAIN;
7cdc2bab
MD
1435 goto end;
1436 }
1419db2b
FD
1437 BT_COMP_LOGE_APPEND_CAUSE(self_comp,
1438 "Received get_data_packet response: error");
7cdc2bab
MD
1439 goto error;
1440 case LTTNG_VIEWER_GET_PACKET_EOF:
d6e69534 1441 retstatus = BT_MSG_ITER_MEDIUM_STATUS_EOF;
7cdc2bab
MD
1442 goto end;
1443 default:
1419db2b
FD
1444 BT_COMP_LOGE_APPEND_CAUSE(self_comp,
1445 "Received get_data_packet response: unknown");
7cdc2bab
MD
1446 goto error;
1447 }
1448
1449 if (req_len == 0) {
1450 goto error;
1451 }
1452
4c66436f 1453 ret_len = lttng_live_recv(viewer_connection, buf, req_len);
7cdc2bab 1454 if (ret_len == 0) {
1419db2b
FD
1455 BT_COMP_LOGE_APPEND_CAUSE(self_comp,
1456 "Remote side has closed connection");
7cdc2bab
MD
1457 goto error;
1458 }
1cb3cdd7 1459 if (ret_len == BT_SOCKET_ERROR) {
1419db2b
FD
1460 BT_COMP_LOGE_APPEND_CAUSE(self_comp,
1461 "Error receiving trace packet: %s",
1462 bt_socket_errormsg());
7cdc2bab
MD
1463 goto error;
1464 }
f6ccaed9 1465 BT_ASSERT(ret_len == req_len);
7cdc2bab
MD
1466 *recv_len = ret_len;
1467end:
1468 return retstatus;
1469
1470error:
9b4f9b42 1471 if (lttng_live_graph_is_canceled(lttng_live_msg_iter)) {
d6e69534 1472 retstatus = BT_MSG_ITER_MEDIUM_STATUS_AGAIN;
4c66436f 1473 } else {
d6e69534 1474 retstatus = BT_MSG_ITER_MEDIUM_STATUS_ERROR;
4c66436f 1475 }
7cdc2bab
MD
1476 return retstatus;
1477}
1478
1479/*
1480 * Request new streams for a session.
1481 */
1482BT_HIDDEN
14f28187 1483enum lttng_live_iterator_status lttng_live_get_new_streams(
7cdc2bab
MD
1484 struct lttng_live_session *session)
1485{
14f28187
FD
1486 enum lttng_live_iterator_status status =
1487 LTTNG_LIVE_ITERATOR_STATUS_OK;
7cdc2bab
MD
1488 struct lttng_viewer_cmd cmd;
1489 struct lttng_viewer_new_streams_request rq;
1490 struct lttng_viewer_new_streams_response rp;
14f28187
FD
1491 struct lttng_live_msg_iter *lttng_live_msg_iter =
1492 session->lttng_live_msg_iter;
1493 struct live_viewer_connection *viewer_connection =
1494 lttng_live_msg_iter->viewer_connection;
1419db2b 1495 bt_self_component *self_comp = viewer_connection->self_comp;
7cdc2bab 1496 uint32_t streams_count;
bdcbd52e
JR
1497 const size_t cmd_buf_len = sizeof(cmd) + sizeof(rq);
1498 char cmd_buf[cmd_buf_len];
0f1979c3 1499 ssize_t ret_len;
7cdc2bab
MD
1500
1501 if (!session->new_streams_needed) {
14f28187 1502 return LTTNG_LIVE_ITERATOR_STATUS_OK;
7cdc2bab
MD
1503 }
1504
1505 cmd.cmd = htobe32(LTTNG_VIEWER_GET_NEW_STREAMS);
1506 cmd.data_size = htobe64((uint64_t) sizeof(rq));
1507 cmd.cmd_version = htobe32(0);
1508
1509 memset(&rq, 0, sizeof(rq));
1510 rq.session_id = htobe64(session->id);
1511
bdcbd52e
JR
1512 /*
1513 * Merge the cmd and connection request to prevent a write-write
1514 * sequence on the TCP socket. Otherwise, a delayed ACK will prevent the
1515 * second write to be performed quickly in presence of Nagle's algorithm.
1516 */
1517 memcpy(cmd_buf, &cmd, sizeof(cmd));
1518 memcpy(cmd_buf + sizeof(cmd), &rq, sizeof(rq));
bdcbd52e 1519 ret_len = lttng_live_send(viewer_connection, &cmd_buf, cmd_buf_len);
1cb3cdd7 1520 if (ret_len == BT_SOCKET_ERROR) {
1419db2b
FD
1521 BT_COMP_LOGE_APPEND_CAUSE(self_comp,
1522 "Error sending get_new_streams request: %s",
1523 bt_socket_errormsg());
7cdc2bab
MD
1524 goto error;
1525 }
7cdc2bab 1526
f6ccaed9 1527 BT_ASSERT(ret_len == cmd_buf_len);
4c66436f 1528 ret_len = lttng_live_recv(viewer_connection, &rp, sizeof(rp));
7cdc2bab 1529 if (ret_len == 0) {
1419db2b
FD
1530 BT_COMP_LOGE_APPEND_CAUSE(self_comp,
1531 "Remote side has closed connection");
7cdc2bab
MD
1532 goto error;
1533 }
1cb3cdd7 1534 if (ret_len == BT_SOCKET_ERROR) {
1419db2b
FD
1535 BT_COMP_LOGE_APPEND_CAUSE(self_comp,
1536 "Error receiving get_new_streams response");
7cdc2bab
MD
1537 goto error;
1538 }
f6ccaed9 1539 BT_ASSERT(ret_len == sizeof(rp));
7cdc2bab
MD
1540
1541 streams_count = be32toh(rp.streams_count);
1542
1543 switch(be32toh(rp.status)) {
1544 case LTTNG_VIEWER_NEW_STREAMS_OK:
1545 session->new_streams_needed = false;
1546 break;
1547 case LTTNG_VIEWER_NEW_STREAMS_NO_NEW:
1548 session->new_streams_needed = false;
1549 goto end;
1550 case LTTNG_VIEWER_NEW_STREAMS_HUP:
1551 session->new_streams_needed = false;
1552 session->closed = true;
14f28187 1553 status = LTTNG_LIVE_ITERATOR_STATUS_END;
7cdc2bab
MD
1554 goto end;
1555 case LTTNG_VIEWER_NEW_STREAMS_ERR:
1419db2b 1556 BT_COMP_LOGD("Received get_new_streams response: error");
7cdc2bab
MD
1557 goto error;
1558 default:
1419db2b
FD
1559 BT_COMP_LOGE_APPEND_CAUSE(self_comp,
1560 "Received get_new_streams response: Unknown:"
1561 "return code %u", be32toh(rp.status));
7cdc2bab
MD
1562 goto error;
1563 }
1564
1565 if (receive_streams(session, streams_count)) {
1419db2b 1566 BT_COMP_LOGE_APPEND_CAUSE(self_comp, "Error receiving streams");
7cdc2bab
MD
1567 goto error;
1568 }
1569end:
1570 return status;
1571
1572error:
9b4f9b42 1573 if (lttng_live_graph_is_canceled(lttng_live_msg_iter)) {
14f28187 1574 status = LTTNG_LIVE_ITERATOR_STATUS_AGAIN;
4c66436f 1575 } else {
14f28187 1576 status = LTTNG_LIVE_ITERATOR_STATUS_ERROR;
1419db2b 1577 BT_COMP_LOGE("Error receiving streams.");
4c66436f 1578 }
7cdc2bab
MD
1579 return status;
1580}
1581
1582BT_HIDDEN
14f28187
FD
1583struct live_viewer_connection *live_viewer_connection_create(
1584 const char *url, bool in_query,
550004b4 1585 struct lttng_live_msg_iter *lttng_live_msg_iter,
1419db2b
FD
1586 bt_self_component *self_comp,
1587 bt_self_component_class *self_comp_class,
550004b4 1588 bt_logging_level log_level)
7cdc2bab 1589{
14f28187 1590 struct live_viewer_connection *viewer_connection;
7cdc2bab 1591
14f28187 1592 viewer_connection = g_new0(struct live_viewer_connection, 1);
7cdc2bab 1593
550004b4 1594 if (bt_socket_init(log_level) != 0) {
1419db2b
FD
1595 BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp,
1596 self_comp_class, "Failed to init socket");
1cb3cdd7
MJ
1597 goto error;
1598 }
1599
550004b4
PP
1600 viewer_connection->log_level = log_level;
1601
1419db2b
FD
1602 viewer_connection->self_comp = self_comp;
1603 viewer_connection->self_comp_class = self_comp_class;
550004b4 1604
1cb3cdd7 1605 viewer_connection->control_sock = BT_INVALID_SOCKET;
7cdc2bab 1606 viewer_connection->port = -1;
14f28187
FD
1607 viewer_connection->in_query = in_query;
1608 viewer_connection->lttng_live_msg_iter = lttng_live_msg_iter;
7cdc2bab
MD
1609 viewer_connection->url = g_string_new(url);
1610 if (!viewer_connection->url) {
1419db2b
FD
1611 BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp,
1612 self_comp_class, "Failed to allocate URL buffer");
7cdc2bab
MD
1613 goto error;
1614 }
1615
2ece7dd0 1616 BT_COMP_LOGI("Establishing connection to url \"%s\"...", url);
7cdc2bab 1617 if (lttng_live_connect_viewer(viewer_connection)) {
1419db2b
FD
1618 BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp,
1619 self_comp_class, "Failed to establish connection: "
1620 "url=\"%s\"", url);
1621 goto error;
7cdc2bab 1622 }
2ece7dd0 1623 BT_COMP_LOGI("Connection to url \"%s\" is established", url);
7cdc2bab
MD
1624 return viewer_connection;
1625
7cdc2bab 1626error:
2f767475
PP
1627 if (viewer_connection) {
1628 live_viewer_connection_destroy(viewer_connection);
1629 }
1630
7cdc2bab
MD
1631 return NULL;
1632}
1633
1634BT_HIDDEN
14f28187
FD
1635void live_viewer_connection_destroy(
1636 struct live_viewer_connection *viewer_connection)
7cdc2bab 1637{
2ece7dd0 1638 BT_COMP_LOGI("Closing connection to url \"%s\"", viewer_connection->url->str);
b9e6ec43
FD
1639
1640 if (!viewer_connection) {
1641 goto end;
1642 }
1643
7cdc2bab 1644 lttng_live_disconnect_viewer(viewer_connection);
b9e6ec43 1645
2f767475
PP
1646 if (viewer_connection->url) {
1647 g_string_free(viewer_connection->url, true);
1648 }
b9e6ec43 1649
94b828f3 1650 if (viewer_connection->relay_hostname) {
14f28187 1651 g_string_free(viewer_connection->relay_hostname, true);
94b828f3 1652 }
b9e6ec43 1653
94b828f3 1654 if (viewer_connection->target_hostname) {
14f28187 1655 g_string_free(viewer_connection->target_hostname, true);
94b828f3 1656 }
b9e6ec43 1657
94b828f3 1658 if (viewer_connection->session_name) {
14f28187 1659 g_string_free(viewer_connection->session_name, true);
94b828f3 1660 }
b9e6ec43 1661
7cdc2bab 1662 g_free(viewer_connection);
1cb3cdd7
MJ
1663
1664 bt_socket_fini();
b9e6ec43
FD
1665
1666end:
1667 return;
7cdc2bab 1668}
This page took 0.150081 seconds and 4 git commands to generate.