.gitignore: add some missing files
[babeltrace.git] / src / plugins / ctf / lttng-live / viewer-connection.cpp
1 /*
2 * SPDX-License-Identifier: MIT
3 *
4 * Copyright 2019 Francis Deslauriers <francis.deslauriers@efficios.com>
5 * Copyright 2016 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
6 */
7
8 #include <glib.h>
9 #include <stdint.h>
10 #include <stdio.h>
11
12 #include <babeltrace2/babeltrace.h>
13
14 #include "common/common.h"
15 #include "compat/endian.h" /* IWYU pragma: keep */
16 #include "cpp-common/bt2s/make-unique.hpp"
17
18 #include "data-stream.hpp"
19 #include "lttng-live.hpp"
20 #include "lttng-viewer-abi.hpp"
21 #include "metadata.hpp"
22 #include "viewer-connection.hpp"
23
24 #define viewer_handle_send_recv_status(_status, _action, _msg_str) \
25 do { \
26 switch (_status) { \
27 case LTTNG_LIVE_VIEWER_STATUS_INTERRUPTED: \
28 break; \
29 case LTTNG_LIVE_VIEWER_STATUS_ERROR: \
30 BT_CPPLOGE_APPEND_CAUSE_SPEC(viewer_connection->logger, \
31 "Error " _action " " _msg_str); \
32 break; \
33 default: \
34 bt_common_abort(); \
35 } \
36 } while (0)
37
38 #define viewer_handle_send_status(_status, _msg_str) \
39 viewer_handle_send_recv_status(_status, "sending", _msg_str)
40
41 #define viewer_handle_recv_status(_status, _msg_str) \
42 viewer_handle_send_recv_status(_status, "receiving", _msg_str)
43
44 #define LTTNG_LIVE_CPPLOGE_APPEND_CAUSE_ERRNO(_msg, _fmt, ...) \
45 do { \
46 BT_CPPLOGE_APPEND_CAUSE_SPEC(viewer_connection->logger, _msg ": {}" _fmt, \
47 bt_socket_errormsg(), ##__VA_ARGS__); \
48 } while (0)
49
50 static inline enum lttng_live_iterator_status
51 viewer_status_to_live_iterator_status(enum lttng_live_viewer_status viewer_status)
52 {
53 switch (viewer_status) {
54 case LTTNG_LIVE_VIEWER_STATUS_OK:
55 return LTTNG_LIVE_ITERATOR_STATUS_OK;
56 case LTTNG_LIVE_VIEWER_STATUS_INTERRUPTED:
57 return LTTNG_LIVE_ITERATOR_STATUS_AGAIN;
58 case LTTNG_LIVE_VIEWER_STATUS_ERROR:
59 return LTTNG_LIVE_ITERATOR_STATUS_ERROR;
60 }
61
62 bt_common_abort();
63 }
64
65 static inline enum lttng_live_get_stream_bytes_status
66 viewer_status_to_lttng_live_get_stream_bytes_status(enum lttng_live_viewer_status viewer_status)
67 {
68 switch (viewer_status) {
69 case LTTNG_LIVE_VIEWER_STATUS_OK:
70 return LTTNG_LIVE_GET_STREAM_BYTES_STATUS_OK;
71 case LTTNG_LIVE_VIEWER_STATUS_INTERRUPTED:
72 return LTTNG_LIVE_GET_STREAM_BYTES_STATUS_AGAIN;
73 case LTTNG_LIVE_VIEWER_STATUS_ERROR:
74 return LTTNG_LIVE_GET_STREAM_BYTES_STATUS_ERROR;
75 }
76
77 bt_common_abort();
78 }
79
80 static inline void viewer_connection_close_socket(struct live_viewer_connection *viewer_connection)
81 {
82 if (viewer_connection->control_sock == BT_INVALID_SOCKET) {
83 return;
84 }
85
86 int ret = bt_socket_close(viewer_connection->control_sock);
87 if (ret == -1) {
88 BT_CPPLOGW_ERRNO_SPEC(viewer_connection->logger,
89 "Error closing viewer connection socket: ", ".");
90 }
91
92 viewer_connection->control_sock = BT_INVALID_SOCKET;
93 }
94
95 /*
96 * This function receives a message from the Relay daemon.
97 * If it received the entire message, it returns _OK,
98 * If it's interrupted, it returns _INTERRUPTED,
99 * otherwise, it returns _ERROR.
100 */
101 static enum lttng_live_viewer_status
102 lttng_live_recv(struct live_viewer_connection *viewer_connection, void *buf, size_t len)
103 {
104 ssize_t received;
105 size_t total_received = 0, to_receive = len;
106 struct lttng_live_msg_iter *lttng_live_msg_iter = viewer_connection->lttng_live_msg_iter;
107 BT_SOCKET sock = viewer_connection->control_sock;
108
109 /*
110 * Receive a message from the Relay.
111 */
112 do {
113 received = bt_socket_recv(sock, (char *) buf + total_received, to_receive, 0);
114 if (received == BT_SOCKET_ERROR) {
115 if (bt_socket_interrupted()) {
116 if (lttng_live_graph_is_canceled(lttng_live_msg_iter)) {
117 /*
118 * This interruption was due to a
119 * SIGINT and the graph is being torn
120 * down.
121 */
122 lttng_live_msg_iter->was_interrupted = true;
123 return LTTNG_LIVE_VIEWER_STATUS_INTERRUPTED;
124 } else {
125 /*
126 * A signal was received, but the graph
127 * is not being torn down. Carry on.
128 */
129 continue;
130 }
131 } else {
132 /*
133 * For any other types of socket error, close
134 * the socket and return an error.
135 */
136 LTTNG_LIVE_CPPLOGE_APPEND_CAUSE_ERRNO("Error receiving from Relay", ".");
137
138 viewer_connection_close_socket(viewer_connection);
139 return LTTNG_LIVE_VIEWER_STATUS_ERROR;
140 }
141 } else if (received == 0) {
142 /*
143 * The recv() call returned 0. This means the
144 * connection was orderly shutdown from the other peer.
145 * If that happens when we are trying to receive
146 * a message from it, it means something when wrong.
147 * Close the socket and return an error.
148 */
149 BT_CPPLOGE_APPEND_CAUSE_SPEC(viewer_connection->logger,
150 "Remote side has closed connection");
151 viewer_connection_close_socket(viewer_connection);
152 return LTTNG_LIVE_VIEWER_STATUS_ERROR;
153 }
154
155 BT_ASSERT(received <= to_receive);
156 total_received += received;
157 to_receive -= received;
158
159 } while (to_receive > 0);
160
161 BT_ASSERT(total_received == len);
162 return LTTNG_LIVE_VIEWER_STATUS_OK;
163 }
164
165 /*
166 * This function sends a message to the Relay daemon.
167 * If it send the message, it returns _OK,
168 * If it's interrupted, it returns _INTERRUPTED,
169 * otherwise, it returns _ERROR.
170 */
171 static enum lttng_live_viewer_status
172 lttng_live_send(struct live_viewer_connection *viewer_connection, const void *buf, size_t len)
173 {
174 struct lttng_live_msg_iter *lttng_live_msg_iter = viewer_connection->lttng_live_msg_iter;
175 BT_SOCKET sock = viewer_connection->control_sock;
176 size_t to_send = len;
177 ssize_t total_sent = 0;
178
179 do {
180 ssize_t sent = bt_socket_send_nosigpipe(sock, (char *) buf + total_sent, to_send);
181 if (sent == BT_SOCKET_ERROR) {
182 if (bt_socket_interrupted()) {
183 if (lttng_live_graph_is_canceled(lttng_live_msg_iter)) {
184 /*
185 * This interruption was a SIGINT and
186 * the graph is being teared down.
187 */
188 lttng_live_msg_iter->was_interrupted = true;
189 return LTTNG_LIVE_VIEWER_STATUS_INTERRUPTED;
190 } else {
191 /*
192 * A signal was received, but the graph
193 * is not being teared down. Carry on.
194 */
195 continue;
196 }
197 } else {
198 /*
199 * For any other types of socket error, close
200 * the socket and return an error.
201 */
202 LTTNG_LIVE_CPPLOGE_APPEND_CAUSE_ERRNO("Error sending to Relay", ".");
203
204 viewer_connection_close_socket(viewer_connection);
205 return LTTNG_LIVE_VIEWER_STATUS_ERROR;
206 }
207 }
208
209 BT_ASSERT(sent <= to_send);
210 total_sent += sent;
211 to_send -= sent;
212
213 } while (to_send > 0);
214
215 BT_ASSERT(total_sent == len);
216 return LTTNG_LIVE_VIEWER_STATUS_OK;
217 }
218
219 static int parse_url(struct live_viewer_connection *viewer_connection)
220 {
221 char error_buf[256] = {0};
222 struct bt_common_lttng_live_url_parts lttng_live_url_parts = {};
223 bt_common_lttng_live_url_parts_deleter partsDeleter {lttng_live_url_parts};
224
225 if (viewer_connection->url.empty()) {
226 return -1;
227 }
228
229 lttng_live_url_parts = bt_common_parse_lttng_live_url(viewer_connection->url.c_str(), error_buf,
230 sizeof(error_buf));
231 if (!lttng_live_url_parts.proto) {
232 BT_CPPLOGE_APPEND_CAUSE_SPEC(viewer_connection->logger, "Invalid LTTng live URL format: {}",
233 error_buf);
234 return -1;
235 }
236 viewer_connection->proto.reset(lttng_live_url_parts.proto);
237 lttng_live_url_parts.proto = NULL;
238
239 viewer_connection->relay_hostname.reset(lttng_live_url_parts.hostname);
240 lttng_live_url_parts.hostname = NULL;
241
242 if (lttng_live_url_parts.port >= 0) {
243 viewer_connection->port = lttng_live_url_parts.port;
244 } else {
245 viewer_connection->port = LTTNG_DEFAULT_NETWORK_VIEWER_PORT;
246 }
247
248 viewer_connection->target_hostname.reset(lttng_live_url_parts.target_hostname);
249 lttng_live_url_parts.target_hostname = NULL;
250
251 if (lttng_live_url_parts.session_name) {
252 viewer_connection->session_name.reset(lttng_live_url_parts.session_name);
253 lttng_live_url_parts.session_name = NULL;
254 }
255
256 return 0;
257 }
258
259 static enum lttng_live_viewer_status
260 lttng_live_handshake(struct live_viewer_connection *viewer_connection)
261 {
262 struct lttng_viewer_cmd cmd;
263 struct lttng_viewer_connect connect;
264 enum lttng_live_viewer_status status;
265 const size_t cmd_buf_len = sizeof(cmd) + sizeof(connect);
266 char cmd_buf[cmd_buf_len];
267
268 BT_CPPLOGD_SPEC(viewer_connection->logger,
269 "Handshaking with the relay daemon: cmd={}, major-version={}, minor-version={}",
270 LTTNG_VIEWER_CONNECT, LTTNG_LIVE_MAJOR, LTTNG_LIVE_MINOR);
271
272 cmd.cmd = htobe32(LTTNG_VIEWER_CONNECT);
273 cmd.data_size = htobe64((uint64_t) sizeof(connect));
274 cmd.cmd_version = htobe32(0);
275
276 connect.viewer_session_id = -1ULL; /* will be set on recv */
277 connect.major = htobe32(LTTNG_LIVE_MAJOR);
278 connect.minor = htobe32(LTTNG_LIVE_MINOR);
279 connect.type = htobe32(LTTNG_VIEWER_CLIENT_COMMAND);
280
281 /*
282 * Merge the cmd and connection request to prevent a write-write
283 * sequence on the TCP socket. Otherwise, a delayed ACK will prevent the
284 * second write to be performed quickly in presence of Nagle's algorithm
285 */
286 memcpy(cmd_buf, &cmd, sizeof(cmd));
287 memcpy(cmd_buf + sizeof(cmd), &connect, sizeof(connect));
288
289 status = lttng_live_send(viewer_connection, &cmd_buf, cmd_buf_len);
290 if (status != LTTNG_LIVE_VIEWER_STATUS_OK) {
291 viewer_handle_send_status(status, "viewer connect command");
292 return status;
293 }
294
295 status = lttng_live_recv(viewer_connection, &connect, sizeof(connect));
296 if (status != LTTNG_LIVE_VIEWER_STATUS_OK) {
297 viewer_handle_recv_status(status, "viewer connect reply");
298 return status;
299 }
300
301 BT_CPPLOGI_SPEC(viewer_connection->logger, "Received viewer session ID : {}",
302 (uint64_t) be64toh(connect.viewer_session_id));
303 BT_CPPLOGI_SPEC(viewer_connection->logger, "Relayd version : {}.{}", be32toh(connect.major),
304 be32toh(connect.minor));
305
306 if (LTTNG_LIVE_MAJOR != be32toh(connect.major)) {
307 BT_CPPLOGE_APPEND_CAUSE_SPEC(viewer_connection->logger,
308 "Incompatible lttng-relayd protocol");
309 return LTTNG_LIVE_VIEWER_STATUS_ERROR;
310 }
311 /* Use the smallest protocol version implemented. */
312 if (LTTNG_LIVE_MINOR > be32toh(connect.minor)) {
313 viewer_connection->minor = be32toh(connect.minor);
314 } else {
315 viewer_connection->minor = LTTNG_LIVE_MINOR;
316 }
317 viewer_connection->major = LTTNG_LIVE_MAJOR;
318
319 return LTTNG_LIVE_VIEWER_STATUS_OK;
320 }
321
322 static enum lttng_live_viewer_status
323 lttng_live_connect_viewer(struct live_viewer_connection *viewer_connection)
324 {
325 struct hostent *host;
326 struct sockaddr_in server_addr;
327 enum lttng_live_viewer_status status;
328
329 if (parse_url(viewer_connection)) {
330 BT_CPPLOGE_APPEND_CAUSE_SPEC(viewer_connection->logger, "Failed to parse URL");
331 return LTTNG_LIVE_VIEWER_STATUS_ERROR;
332 }
333
334 BT_CPPLOGD_SPEC(
335 viewer_connection->logger,
336 "Connecting to hostname : {}, port : {}, target hostname : {}, session name : {}, proto : {}",
337 viewer_connection->relay_hostname->str, viewer_connection->port,
338 !viewer_connection->target_hostname ? "<none>" : viewer_connection->target_hostname->str,
339 !viewer_connection->session_name ? "<none>" : viewer_connection->session_name->str,
340 viewer_connection->proto->str);
341
342 host = gethostbyname(viewer_connection->relay_hostname->str);
343 if (!host) {
344 BT_CPPLOGE_APPEND_CAUSE_SPEC(viewer_connection->logger,
345 "Cannot lookup hostname: hostname=\"{}\"",
346 viewer_connection->relay_hostname->str);
347 return LTTNG_LIVE_VIEWER_STATUS_ERROR;
348 }
349
350 if ((viewer_connection->control_sock = socket(AF_INET, SOCK_STREAM, 0)) == BT_INVALID_SOCKET) {
351 BT_CPPLOGE_APPEND_CAUSE_SPEC(viewer_connection->logger, "Socket creation failed: {}",
352 bt_socket_errormsg());
353 return LTTNG_LIVE_VIEWER_STATUS_ERROR;
354 }
355
356 server_addr.sin_family = AF_INET;
357 server_addr.sin_port = htons(viewer_connection->port);
358 server_addr.sin_addr = *((struct in_addr *) host->h_addr);
359 memset(&(server_addr.sin_zero), 0, 8);
360
361 if (connect(viewer_connection->control_sock, (struct sockaddr *) &server_addr,
362 sizeof(struct sockaddr)) == BT_SOCKET_ERROR) {
363 BT_CPPLOGE_APPEND_CAUSE_SPEC(viewer_connection->logger, "Connection failed: {}",
364 bt_socket_errormsg());
365 viewer_connection_close_socket(viewer_connection);
366 return LTTNG_LIVE_VIEWER_STATUS_ERROR;
367 }
368
369 status = lttng_live_handshake(viewer_connection);
370
371 /*
372 * Only print error and append cause in case of error. not in case of
373 * interruption.
374 */
375 if (status == LTTNG_LIVE_VIEWER_STATUS_ERROR) {
376 BT_CPPLOGE_APPEND_CAUSE_SPEC(viewer_connection->logger, "Viewer handshake failed");
377 viewer_connection_close_socket(viewer_connection);
378 return LTTNG_LIVE_VIEWER_STATUS_ERROR;
379 } else if (status == LTTNG_LIVE_VIEWER_STATUS_INTERRUPTED) {
380 return LTTNG_LIVE_VIEWER_STATUS_INTERRUPTED;
381 }
382
383 return LTTNG_LIVE_VIEWER_STATUS_OK;
384 }
385
386 static int list_update_session(const bt2::ArrayValue results,
387 const struct lttng_viewer_session *session, bool *_found,
388 struct live_viewer_connection *viewer_connection)
389 {
390 bool found = false;
391
392 for (const auto value : results) {
393 const auto map = value.asMap();
394 const auto hostnameVal = map["target-hostname"];
395
396 if (!hostnameVal) {
397 BT_CPPLOGE_APPEND_CAUSE_SPEC(viewer_connection->logger,
398 "Error borrowing \"target-hostname\" entry.");
399 return -1;
400 }
401
402 const auto sessionNameVal = map["session-name"];
403
404 if (!sessionNameVal) {
405 BT_CPPLOGE_APPEND_CAUSE_SPEC(viewer_connection->logger,
406 "Error borrowing \"session-name\" entry.");
407 return -1;
408 }
409
410 const auto hostname_str = hostnameVal->asString().value();
411 const auto session_name_str = sessionNameVal->asString().value();
412
413 if (strcmp(session->hostname, hostname_str) == 0 &&
414 strcmp(session->session_name, session_name_str) == 0) {
415 uint32_t streams = be32toh(session->streams);
416 uint32_t clients = be32toh(session->clients);
417
418 found = true;
419
420 const auto streamCountVal = map["stream-count"];
421
422 if (!streamCountVal) {
423 BT_CPPLOGE_APPEND_CAUSE_SPEC(viewer_connection->logger,
424 "Error borrowing \"stream-count\" entry.");
425 return -1;
426 }
427
428 auto val = streamCountVal->asUnsignedInteger().value();
429
430 /* sum */
431 val += streams;
432 streamCountVal->asUnsignedInteger().value(val);
433
434 const auto clientCountVal = map["client-count"];
435
436 if (!clientCountVal) {
437 BT_CPPLOGE_APPEND_CAUSE_SPEC(viewer_connection->logger,
438 "Error borrowing \"client-count\" entry.");
439 return -1;
440 }
441
442 val = clientCountVal->asUnsignedInteger().value();
443
444 /* max */
445 val = std::max<uint64_t>(clients, val);
446 clientCountVal->asUnsignedInteger().value(val);
447 }
448
449 if (found) {
450 break;
451 }
452 }
453
454 *_found = found;
455 return 0;
456 }
457
458 static int list_append_session(const bt2::ArrayValue results, const std::string& base_url,
459 const struct lttng_viewer_session *session,
460 struct live_viewer_connection *viewer_connection)
461 {
462 int ret = 0;
463 bool found = false;
464
465 /*
466 * If the session already exists, add the stream count to it,
467 * and do max of client counts.
468 */
469 ret = list_update_session(results, session, &found, viewer_connection);
470 if (ret || found) {
471 return ret;
472 }
473
474 const auto map = bt2::MapValue::create();
475
476 if (base_url.empty()) {
477 BT_CPPLOGE_APPEND_CAUSE_SPEC(viewer_connection->logger, "Error: base_url empty.");
478 return -1;
479 }
480
481 /*
482 * key = "url",
483 * value = <string>,
484 */
485 map->insert("url",
486 fmt::format("{}/host/{}/{}", base_url, session->hostname, session->session_name));
487
488 /*
489 * key = "target-hostname",
490 * value = <string>,
491 */
492 map->insert("target-hostname", session->hostname);
493
494 /*
495 * key = "session-name",
496 * value = <string>,
497 */
498 map->insert("session-name", session->session_name);
499
500 /*
501 * key = "timer-us",
502 * value = <integer>,
503 */
504 {
505 uint32_t live_timer = be32toh(session->live_timer);
506
507 map->insert("timer-us", (uint64_t) live_timer);
508 }
509
510 /*
511 * key = "stream-count",
512 * value = <integer>,
513 */
514 {
515 uint32_t streams = be32toh(session->streams);
516
517 map->insert("stream-count", (uint64_t) streams);
518 }
519
520 /*
521 * key = "client-count",
522 * value = <integer>,
523 */
524 {
525 uint32_t clients = be32toh(session->clients);
526
527 map->insert("client-count", (uint64_t) clients);
528 }
529
530 results.append(*map);
531 return 0;
532 }
533
534 /*
535 * Data structure returned:
536 *
537 * {
538 * <array> = {
539 * [n] = {
540 * <map> = {
541 * {
542 * key = "url",
543 * value = <string>,
544 * },
545 * {
546 * key = "target-hostname",
547 * value = <string>,
548 * },
549 * {
550 * key = "session-name",
551 * value = <string>,
552 * },
553 * {
554 * key = "timer-us",
555 * value = <integer>,
556 * },
557 * {
558 * key = "stream-count",
559 * value = <integer>,
560 * },
561 * {
562 * key = "client-count",
563 * value = <integer>,
564 * },
565 * },
566 * }
567 * }
568 */
569
570 bt2::Value::Shared
571 live_viewer_connection_list_sessions(struct live_viewer_connection *viewer_connection)
572 {
573 enum lttng_live_viewer_status viewer_status;
574 struct lttng_viewer_cmd cmd;
575 struct lttng_viewer_list_sessions list;
576 uint32_t i, sessions_count;
577 auto result = bt2::ArrayValue::create();
578
579 BT_CPPLOGD_SPEC(viewer_connection->logger, "Requesting list of sessions: cmd={}",
580 LTTNG_VIEWER_LIST_SESSIONS);
581
582 cmd.cmd = htobe32(LTTNG_VIEWER_LIST_SESSIONS);
583 cmd.data_size = htobe64((uint64_t) 0);
584 cmd.cmd_version = htobe32(0);
585
586 viewer_status = lttng_live_send(viewer_connection, &cmd, sizeof(cmd));
587 if (viewer_status == LTTNG_LIVE_VIEWER_STATUS_ERROR) {
588 BT_CPPLOGE_APPEND_CAUSE_AND_THROW_SPEC(viewer_connection->logger, bt2::Error,
589 "Error sending list sessions command");
590 } else if (viewer_status == LTTNG_LIVE_VIEWER_STATUS_INTERRUPTED) {
591 throw bt2c::TryAgain {};
592 }
593
594 viewer_status = lttng_live_recv(viewer_connection, &list, sizeof(list));
595 if (viewer_status == LTTNG_LIVE_VIEWER_STATUS_ERROR) {
596 BT_CPPLOGE_APPEND_CAUSE_AND_THROW_SPEC(viewer_connection->logger, bt2::Error,
597 "Error receiving session list");
598 } else if (viewer_status == LTTNG_LIVE_VIEWER_STATUS_INTERRUPTED) {
599 throw bt2c::TryAgain {};
600 }
601
602 sessions_count = be32toh(list.sessions_count);
603 for (i = 0; i < sessions_count; i++) {
604 struct lttng_viewer_session lsession;
605
606 viewer_status = lttng_live_recv(viewer_connection, &lsession, sizeof(lsession));
607 if (viewer_status == LTTNG_LIVE_VIEWER_STATUS_ERROR) {
608 BT_CPPLOGE_APPEND_CAUSE_AND_THROW_SPEC(viewer_connection->logger, bt2::Error,
609 "Error receiving session:");
610 } else if (viewer_status == LTTNG_LIVE_VIEWER_STATUS_INTERRUPTED) {
611 throw bt2c::TryAgain {};
612 }
613
614 lsession.hostname[LTTNG_VIEWER_HOST_NAME_MAX - 1] = '\0';
615 lsession.session_name[LTTNG_VIEWER_NAME_MAX - 1] = '\0';
616 if (list_append_session(*result, viewer_connection->url, &lsession, viewer_connection)) {
617 BT_CPPLOGE_APPEND_CAUSE_AND_THROW_SPEC(viewer_connection->logger, bt2::Error,
618 "Error appending session");
619 }
620 }
621
622 return result;
623 }
624
625 static enum lttng_live_viewer_status
626 lttng_live_query_session_ids(struct lttng_live_msg_iter *lttng_live_msg_iter)
627 {
628 struct lttng_viewer_cmd cmd;
629 struct lttng_viewer_list_sessions list;
630 struct lttng_viewer_session lsession;
631 uint32_t i, sessions_count;
632 uint64_t session_id;
633 enum lttng_live_viewer_status status;
634 live_viewer_connection *viewer_connection = lttng_live_msg_iter->viewer_connection.get();
635
636 BT_CPPLOGD_SPEC(viewer_connection->logger,
637 "Asking the relay daemon for the list of sessions: cmd={}",
638 LTTNG_VIEWER_LIST_SESSIONS);
639
640 cmd.cmd = htobe32(LTTNG_VIEWER_LIST_SESSIONS);
641 cmd.data_size = htobe64((uint64_t) 0);
642 cmd.cmd_version = htobe32(0);
643
644 status = lttng_live_send(viewer_connection, &cmd, sizeof(cmd));
645 if (status != LTTNG_LIVE_VIEWER_STATUS_OK) {
646 viewer_handle_send_status(status, "list sessions command");
647 return status;
648 }
649
650 status = lttng_live_recv(viewer_connection, &list, sizeof(list));
651 if (status != LTTNG_LIVE_VIEWER_STATUS_OK) {
652 viewer_handle_recv_status(status, "session list reply");
653 return status;
654 }
655
656 sessions_count = be32toh(list.sessions_count);
657 for (i = 0; i < sessions_count; i++) {
658 status = lttng_live_recv(viewer_connection, &lsession, sizeof(lsession));
659 if (status != LTTNG_LIVE_VIEWER_STATUS_OK) {
660 viewer_handle_recv_status(status, "session reply");
661 return status;
662 }
663 lsession.hostname[LTTNG_VIEWER_HOST_NAME_MAX - 1] = '\0';
664 lsession.session_name[LTTNG_VIEWER_NAME_MAX - 1] = '\0';
665 session_id = be64toh(lsession.id);
666
667 BT_CPPLOGI_SPEC(viewer_connection->logger,
668 "Adding session to internal list: "
669 "session-id={}, hostname=\"{}\", session-name=\"{}\"",
670 session_id, lsession.hostname, lsession.session_name);
671
672 if ((strncmp(lsession.session_name, viewer_connection->session_name->str,
673 LTTNG_VIEWER_NAME_MAX) == 0) &&
674 (strncmp(lsession.hostname, viewer_connection->target_hostname->str,
675 LTTNG_VIEWER_HOST_NAME_MAX) == 0)) {
676 if (lttng_live_add_session(lttng_live_msg_iter, session_id, lsession.hostname,
677 lsession.session_name)) {
678 BT_CPPLOGE_APPEND_CAUSE_SPEC(viewer_connection->logger,
679 "Failed to add live session");
680 return LTTNG_LIVE_VIEWER_STATUS_ERROR;
681 }
682 }
683 }
684
685 return LTTNG_LIVE_VIEWER_STATUS_OK;
686 }
687
688 enum lttng_live_viewer_status
689 lttng_live_create_viewer_session(struct lttng_live_msg_iter *lttng_live_msg_iter)
690 {
691 struct lttng_viewer_cmd cmd;
692 struct lttng_viewer_create_session_response resp;
693 enum lttng_live_viewer_status status;
694 live_viewer_connection *viewer_connection = lttng_live_msg_iter->viewer_connection.get();
695
696 BT_CPPLOGD_SPEC(viewer_connection->logger, "Creating a viewer session: cmd={}",
697 LTTNG_VIEWER_CREATE_SESSION);
698
699 cmd.cmd = htobe32(LTTNG_VIEWER_CREATE_SESSION);
700 cmd.data_size = htobe64((uint64_t) 0);
701 cmd.cmd_version = htobe32(0);
702
703 status = lttng_live_send(viewer_connection, &cmd, sizeof(cmd));
704 if (status != LTTNG_LIVE_VIEWER_STATUS_OK) {
705 viewer_handle_send_status(status, "create session command");
706 return status;
707 }
708
709 status = lttng_live_recv(viewer_connection, &resp, sizeof(resp));
710 if (status != LTTNG_LIVE_VIEWER_STATUS_OK) {
711 viewer_handle_recv_status(status, "create session reply");
712 return status;
713 }
714
715 if (be32toh(resp.status) != LTTNG_VIEWER_CREATE_SESSION_OK) {
716 BT_CPPLOGE_APPEND_CAUSE_SPEC(viewer_connection->logger, "Error creating viewer session");
717 return LTTNG_LIVE_VIEWER_STATUS_ERROR;
718 }
719
720 status = lttng_live_query_session_ids(lttng_live_msg_iter);
721 if (status == LTTNG_LIVE_VIEWER_STATUS_ERROR) {
722 BT_CPPLOGE_APPEND_CAUSE_SPEC(viewer_connection->logger,
723 "Failed to query live viewer session ids");
724 return LTTNG_LIVE_VIEWER_STATUS_ERROR;
725 } else if (status == LTTNG_LIVE_VIEWER_STATUS_INTERRUPTED) {
726 return LTTNG_LIVE_VIEWER_STATUS_INTERRUPTED;
727 }
728
729 return LTTNG_LIVE_VIEWER_STATUS_OK;
730 }
731
732 static enum lttng_live_viewer_status receive_streams(struct lttng_live_session *session,
733 uint32_t stream_count)
734 {
735 uint32_t i;
736 struct lttng_live_msg_iter *lttng_live_msg_iter = session->lttng_live_msg_iter;
737 enum lttng_live_viewer_status status;
738 live_viewer_connection *viewer_connection = lttng_live_msg_iter->viewer_connection.get();
739
740 BT_CPPLOGI_SPEC(viewer_connection->logger, "Getting {} new streams", stream_count);
741 for (i = 0; i < stream_count; i++) {
742 struct lttng_viewer_stream stream;
743 struct lttng_live_stream_iterator *live_stream;
744 uint64_t stream_id;
745 uint64_t ctf_trace_id;
746
747 status = lttng_live_recv(viewer_connection, &stream, sizeof(stream));
748 if (status != LTTNG_LIVE_VIEWER_STATUS_OK) {
749 viewer_handle_recv_status(status, "stream reply");
750 return status;
751 }
752 stream.path_name[LTTNG_VIEWER_PATH_MAX - 1] = '\0';
753 stream.channel_name[LTTNG_VIEWER_NAME_MAX - 1] = '\0';
754 stream_id = be64toh(stream.id);
755 ctf_trace_id = be64toh(stream.ctf_trace_id);
756
757 if (stream.metadata_flag) {
758 BT_CPPLOGI_SPEC(viewer_connection->logger, " metadata stream {} : {}/{}", stream_id,
759 stream.path_name, stream.channel_name);
760 if (lttng_live_metadata_create_stream(session, ctf_trace_id, stream_id)) {
761 BT_CPPLOGE_APPEND_CAUSE_SPEC(viewer_connection->logger,
762 "Error creating metadata stream");
763 return LTTNG_LIVE_VIEWER_STATUS_ERROR;
764 }
765 session->lazy_stream_msg_init = true;
766 } else {
767 BT_CPPLOGI_SPEC(viewer_connection->logger, " stream {} : {}/{}", stream_id,
768 stream.path_name, stream.channel_name);
769 live_stream = lttng_live_stream_iterator_create(session, ctf_trace_id, stream_id);
770 if (!live_stream) {
771 BT_CPPLOGE_APPEND_CAUSE_SPEC(viewer_connection->logger, "Error creating stream");
772 return LTTNG_LIVE_VIEWER_STATUS_ERROR;
773 }
774 }
775 }
776
777 return LTTNG_LIVE_VIEWER_STATUS_OK;
778 }
779
780 enum lttng_live_viewer_status lttng_live_session_attach(struct lttng_live_session *session)
781 {
782 struct lttng_viewer_cmd cmd;
783 enum lttng_live_viewer_status status;
784 struct lttng_viewer_attach_session_request rq;
785 struct lttng_viewer_attach_session_response rp;
786 struct lttng_live_msg_iter *lttng_live_msg_iter = session->lttng_live_msg_iter;
787 live_viewer_connection *viewer_connection = lttng_live_msg_iter->viewer_connection.get();
788 uint64_t session_id = session->id;
789 uint32_t streams_count;
790 const size_t cmd_buf_len = sizeof(cmd) + sizeof(rq);
791 char cmd_buf[cmd_buf_len];
792
793 BT_CPPLOGD_SPEC(viewer_connection->logger,
794 "Attaching to session: cmd={}, session-id={}, seek={}",
795 LTTNG_VIEWER_ATTACH_SESSION, session_id, LTTNG_VIEWER_SEEK_LAST);
796
797 cmd.cmd = htobe32(LTTNG_VIEWER_ATTACH_SESSION);
798 cmd.data_size = htobe64((uint64_t) sizeof(rq));
799 cmd.cmd_version = htobe32(0);
800
801 memset(&rq, 0, sizeof(rq));
802 rq.session_id = htobe64(session_id);
803 // TODO: add cmd line parameter to select seek beginning
804 // rq.seek = htobe32(LTTNG_VIEWER_SEEK_BEGINNING);
805 rq.seek = htobe32(LTTNG_VIEWER_SEEK_LAST);
806
807 /*
808 * Merge the cmd and connection request to prevent a write-write
809 * sequence on the TCP socket. Otherwise, a delayed ACK will prevent the
810 * second write to be performed quickly in presence of Nagle's algorithm.
811 */
812 memcpy(cmd_buf, &cmd, sizeof(cmd));
813 memcpy(cmd_buf + sizeof(cmd), &rq, sizeof(rq));
814 status = lttng_live_send(viewer_connection, &cmd_buf, cmd_buf_len);
815 if (status != LTTNG_LIVE_VIEWER_STATUS_OK) {
816 viewer_handle_send_status(status, "attach session command");
817 return status;
818 }
819
820 status = lttng_live_recv(viewer_connection, &rp, sizeof(rp));
821 if (status != LTTNG_LIVE_VIEWER_STATUS_OK) {
822 viewer_handle_recv_status(status, "attach session reply");
823 return status;
824 }
825
826 streams_count = be32toh(rp.streams_count);
827 switch (be32toh(rp.status)) {
828 case LTTNG_VIEWER_ATTACH_OK:
829 break;
830 case LTTNG_VIEWER_ATTACH_UNK:
831 BT_CPPLOGE_APPEND_CAUSE_SPEC(viewer_connection->logger, "Session id {} is unknown",
832 session_id);
833 return LTTNG_LIVE_VIEWER_STATUS_ERROR;
834 case LTTNG_VIEWER_ATTACH_ALREADY:
835 BT_CPPLOGE_APPEND_CAUSE_SPEC(viewer_connection->logger,
836 "There is already a viewer attached to this session");
837 return LTTNG_LIVE_VIEWER_STATUS_ERROR;
838 case LTTNG_VIEWER_ATTACH_NOT_LIVE:
839 BT_CPPLOGE_APPEND_CAUSE_SPEC(viewer_connection->logger, "Not a live session");
840 return LTTNG_LIVE_VIEWER_STATUS_ERROR;
841 case LTTNG_VIEWER_ATTACH_SEEK_ERR:
842 BT_CPPLOGE_APPEND_CAUSE_SPEC(viewer_connection->logger, "Wrong seek parameter");
843 return LTTNG_LIVE_VIEWER_STATUS_ERROR;
844 default:
845 BT_CPPLOGE_APPEND_CAUSE_SPEC(viewer_connection->logger, "Unknown attach return code {}",
846 be32toh(rp.status));
847 return LTTNG_LIVE_VIEWER_STATUS_ERROR;
848 }
849
850 /* We receive the initial list of streams. */
851 status = receive_streams(session, streams_count);
852 switch (status) {
853 case LTTNG_LIVE_VIEWER_STATUS_OK:
854 break;
855 case LTTNG_LIVE_VIEWER_STATUS_INTERRUPTED:
856 return status;
857 case LTTNG_LIVE_VIEWER_STATUS_ERROR:
858 BT_CPPLOGE_APPEND_CAUSE_SPEC(viewer_connection->logger, "Error receiving streams");
859 return status;
860 default:
861 bt_common_abort();
862 }
863
864 session->attached = true;
865 session->new_streams_needed = false;
866
867 return LTTNG_LIVE_VIEWER_STATUS_OK;
868 }
869
870 enum lttng_live_viewer_status lttng_live_session_detach(struct lttng_live_session *session)
871 {
872 struct lttng_viewer_cmd cmd;
873 enum lttng_live_viewer_status status;
874 struct lttng_viewer_detach_session_request rq;
875 struct lttng_viewer_detach_session_response rp;
876 struct lttng_live_msg_iter *lttng_live_msg_iter = session->lttng_live_msg_iter;
877 live_viewer_connection *viewer_connection = lttng_live_msg_iter->viewer_connection.get();
878 uint64_t session_id = session->id;
879 const size_t cmd_buf_len = sizeof(cmd) + sizeof(rq);
880 char cmd_buf[cmd_buf_len];
881
882 /*
883 * The session might already be detached and the viewer socket might
884 * already been closed. This happens when calling this function when
885 * tearing down the graph after an error.
886 */
887 if (!session->attached || viewer_connection->control_sock == BT_INVALID_SOCKET) {
888 return LTTNG_LIVE_VIEWER_STATUS_OK;
889 }
890
891 BT_CPPLOGD_SPEC(viewer_connection->logger, "Detaching from session: cmd={}, session-id={}",
892 LTTNG_VIEWER_DETACH_SESSION, session_id);
893
894 cmd.cmd = htobe32(LTTNG_VIEWER_DETACH_SESSION);
895 cmd.data_size = htobe64((uint64_t) sizeof(rq));
896 cmd.cmd_version = htobe32(0);
897
898 memset(&rq, 0, sizeof(rq));
899 rq.session_id = htobe64(session_id);
900
901 /*
902 * Merge the cmd and connection request to prevent a write-write
903 * sequence on the TCP socket. Otherwise, a delayed ACK will prevent the
904 * second write to be performed quickly in presence of Nagle's algorithm.
905 */
906 memcpy(cmd_buf, &cmd, sizeof(cmd));
907 memcpy(cmd_buf + sizeof(cmd), &rq, sizeof(rq));
908 status = lttng_live_send(viewer_connection, &cmd_buf, cmd_buf_len);
909 if (status != LTTNG_LIVE_VIEWER_STATUS_OK) {
910 viewer_handle_send_status(status, "detach session command");
911 return status;
912 }
913
914 status = lttng_live_recv(viewer_connection, &rp, sizeof(rp));
915 if (status != LTTNG_LIVE_VIEWER_STATUS_OK) {
916 viewer_handle_recv_status(status, "detach session reply");
917 return status;
918 }
919
920 switch (be32toh(rp.status)) {
921 case LTTNG_VIEWER_DETACH_SESSION_OK:
922 break;
923 case LTTNG_VIEWER_DETACH_SESSION_UNK:
924 BT_CPPLOGW_SPEC(viewer_connection->logger, "Session id {} is unknown", session_id);
925 return LTTNG_LIVE_VIEWER_STATUS_ERROR;
926 case LTTNG_VIEWER_DETACH_SESSION_ERR:
927 BT_CPPLOGW_SPEC(viewer_connection->logger, "Error detaching session id {}", session_id);
928 return LTTNG_LIVE_VIEWER_STATUS_ERROR;
929 default:
930 BT_CPPLOGE_SPEC(viewer_connection->logger, "Unknown detach return code {}",
931 be32toh(rp.status));
932 return LTTNG_LIVE_VIEWER_STATUS_ERROR;
933 }
934
935 session->attached = false;
936
937 return LTTNG_LIVE_VIEWER_STATUS_OK;
938 }
939
940 enum lttng_live_get_one_metadata_status
941 lttng_live_get_one_metadata_packet(struct lttng_live_trace *trace, std::vector<uint8_t>& buf)
942 {
943 uint64_t len = 0;
944 enum lttng_live_viewer_status viewer_status;
945 struct lttng_viewer_cmd cmd;
946 struct lttng_viewer_get_metadata rq;
947 struct lttng_viewer_metadata_packet rp;
948 struct lttng_live_session *session = trace->session;
949 struct lttng_live_msg_iter *lttng_live_msg_iter = session->lttng_live_msg_iter;
950 struct lttng_live_metadata *metadata = trace->metadata.get();
951 live_viewer_connection *viewer_connection = lttng_live_msg_iter->viewer_connection.get();
952 const size_t cmd_buf_len = sizeof(cmd) + sizeof(rq);
953 char cmd_buf[cmd_buf_len];
954
955 BT_CPPLOGD_SPEC(viewer_connection->logger,
956 "Requesting new metadata for trace:"
957 "cmd={}, trace-id={}, metadata-stream-id={}",
958 LTTNG_VIEWER_GET_METADATA, trace->id, metadata->stream_id);
959
960 rq.stream_id = htobe64(metadata->stream_id);
961 cmd.cmd = htobe32(LTTNG_VIEWER_GET_METADATA);
962 cmd.data_size = htobe64((uint64_t) sizeof(rq));
963 cmd.cmd_version = htobe32(0);
964
965 /*
966 * Merge the cmd and connection request to prevent a write-write
967 * sequence on the TCP socket. Otherwise, a delayed ACK will prevent the
968 * second write to be performed quickly in presence of Nagle's algorithm.
969 */
970 memcpy(cmd_buf, &cmd, sizeof(cmd));
971 memcpy(cmd_buf + sizeof(cmd), &rq, sizeof(rq));
972 viewer_status = lttng_live_send(viewer_connection, &cmd_buf, cmd_buf_len);
973 if (viewer_status != LTTNG_LIVE_VIEWER_STATUS_OK) {
974 viewer_handle_send_status(viewer_status, "get metadata command");
975 return (lttng_live_get_one_metadata_status) viewer_status;
976 }
977
978 viewer_status = lttng_live_recv(viewer_connection, &rp, sizeof(rp));
979 if (viewer_status != LTTNG_LIVE_VIEWER_STATUS_OK) {
980 viewer_handle_recv_status(viewer_status, "get metadata reply");
981 return (lttng_live_get_one_metadata_status) viewer_status;
982 }
983
984 switch (be32toh(rp.status)) {
985 case LTTNG_VIEWER_METADATA_OK:
986 BT_CPPLOGD_SPEC(viewer_connection->logger, "Received get_metadata response: ok");
987 break;
988 case LTTNG_VIEWER_NO_NEW_METADATA:
989 BT_CPPLOGD_SPEC(viewer_connection->logger, "Received get_metadata response: no new");
990 return LTTNG_LIVE_GET_ONE_METADATA_STATUS_END;
991 case LTTNG_VIEWER_METADATA_ERR:
992 /*
993 * The Relayd cannot find this stream id. Maybe its
994 * gone already. This can happen in short lived UST app
995 * in a per-pid session.
996 */
997 BT_CPPLOGD_SPEC(viewer_connection->logger, "Received get_metadata response: error");
998 return LTTNG_LIVE_GET_ONE_METADATA_STATUS_CLOSED;
999 default:
1000 BT_CPPLOGE_APPEND_CAUSE_SPEC(viewer_connection->logger,
1001 "Received get_metadata response: unknown");
1002 return LTTNG_LIVE_GET_ONE_METADATA_STATUS_ERROR;
1003 }
1004
1005 len = be64toh(rp.len);
1006 if (len == 0) {
1007 /*
1008 * We received a `LTTNG_VIEWER_METADATA_OK` with a packet
1009 * length of 0. This means we must try again. This scenario
1010 * arises when a clear command is performed on an lttng session.
1011 */
1012 BT_CPPLOGD_SPEC(
1013 viewer_connection->logger,
1014 "Expecting a metadata packet of size 0. Retry to get a packet from the relay.");
1015 return LTTNG_LIVE_GET_ONE_METADATA_STATUS_OK;
1016 }
1017
1018 BT_CPPLOGD_SPEC(viewer_connection->logger, "Writing {} bytes to metadata", len);
1019 if (len <= 0) {
1020 BT_CPPLOGE_APPEND_CAUSE_SPEC(viewer_connection->logger, "Erroneous response length");
1021 return LTTNG_LIVE_GET_ONE_METADATA_STATUS_ERROR;
1022 }
1023
1024 std::vector<char> localBuf(len);
1025
1026 viewer_status = lttng_live_recv(viewer_connection, localBuf.data(), len);
1027 if (viewer_status != LTTNG_LIVE_VIEWER_STATUS_OK) {
1028 viewer_handle_recv_status(viewer_status, "get metadata packet");
1029 return (lttng_live_get_one_metadata_status) viewer_status;
1030 }
1031
1032 buf.insert(buf.end(), localBuf.begin(), localBuf.end());
1033
1034 return LTTNG_LIVE_GET_ONE_METADATA_STATUS_OK;
1035 }
1036
1037 /*
1038 * Assign the fields from a lttng_viewer_index to a packet_index.
1039 */
1040 static void lttng_index_to_packet_index(struct lttng_viewer_index *lindex,
1041 struct packet_index *pindex)
1042 {
1043 BT_ASSERT(lindex);
1044 BT_ASSERT(pindex);
1045
1046 pindex->offset = be64toh(lindex->offset);
1047 pindex->packet_size = be64toh(lindex->packet_size);
1048 pindex->content_size = be64toh(lindex->content_size);
1049 pindex->ts_cycles.timestamp_begin = be64toh(lindex->timestamp_begin);
1050 pindex->ts_cycles.timestamp_end = be64toh(lindex->timestamp_end);
1051 pindex->events_discarded = be64toh(lindex->events_discarded);
1052 }
1053
1054 static void lttng_live_need_new_streams(struct lttng_live_msg_iter *lttng_live_msg_iter)
1055 {
1056 for (const auto& session : lttng_live_msg_iter->sessions) {
1057 BT_CPPLOGD_SPEC(lttng_live_msg_iter->logger,
1058 "Marking session as needing new streams: "
1059 "session-id={}",
1060 session->id);
1061 session->new_streams_needed = true;
1062 }
1063 }
1064
1065 enum lttng_live_iterator_status
1066 lttng_live_get_next_index(struct lttng_live_msg_iter *lttng_live_msg_iter,
1067 struct lttng_live_stream_iterator *stream, struct packet_index *index)
1068 {
1069 struct lttng_viewer_cmd cmd;
1070 struct lttng_viewer_get_next_index rq;
1071 enum lttng_live_viewer_status viewer_status;
1072 struct lttng_viewer_index rp;
1073 live_viewer_connection *viewer_connection = lttng_live_msg_iter->viewer_connection.get();
1074 struct lttng_live_trace *trace = stream->trace;
1075 const size_t cmd_buf_len = sizeof(cmd) + sizeof(rq);
1076 char cmd_buf[cmd_buf_len];
1077 uint32_t flags, rp_status;
1078
1079 BT_CPPLOGD_SPEC(viewer_connection->logger,
1080 "Requesting next index for stream: cmd={}, "
1081 "viewer-stream-id={}",
1082 LTTNG_VIEWER_GET_NEXT_INDEX, stream->viewer_stream_id);
1083 cmd.cmd = htobe32(LTTNG_VIEWER_GET_NEXT_INDEX);
1084 cmd.data_size = htobe64((uint64_t) sizeof(rq));
1085 cmd.cmd_version = htobe32(0);
1086
1087 memset(&rq, 0, sizeof(rq));
1088 rq.stream_id = htobe64(stream->viewer_stream_id);
1089
1090 /*
1091 * Merge the cmd and connection request to prevent a write-write
1092 * sequence on the TCP socket. Otherwise, a delayed ACK will prevent the
1093 * second write to be performed quickly in presence of Nagle's algorithm.
1094 */
1095 memcpy(cmd_buf, &cmd, sizeof(cmd));
1096 memcpy(cmd_buf + sizeof(cmd), &rq, sizeof(rq));
1097
1098 viewer_status = lttng_live_send(viewer_connection, &cmd_buf, cmd_buf_len);
1099 if (viewer_status != LTTNG_LIVE_VIEWER_STATUS_OK) {
1100 viewer_handle_send_status(viewer_status, "get next index command");
1101 return viewer_status_to_live_iterator_status(viewer_status);
1102 }
1103
1104 viewer_status = lttng_live_recv(viewer_connection, &rp, sizeof(rp));
1105 if (viewer_status != LTTNG_LIVE_VIEWER_STATUS_OK) {
1106 viewer_handle_recv_status(viewer_status, "get next index reply");
1107 return viewer_status_to_live_iterator_status(viewer_status);
1108 }
1109
1110 flags = be32toh(rp.flags);
1111 rp_status = be32toh(rp.status);
1112
1113 BT_CPPLOGD_SPEC(
1114 viewer_connection->logger, "Received response from relay daemon: cmd=%s, response={}",
1115 LTTNG_VIEWER_GET_NEXT_INDEX, static_cast<lttng_viewer_next_index_return_code>(rp_status));
1116
1117 if (flags & LTTNG_VIEWER_FLAG_NEW_STREAM) {
1118 BT_CPPLOGD_SPEC(viewer_connection->logger,
1119 "Marking all sessions as possibly needing new streams: "
1120 "response={}, response-flag=NEW_STREAM",
1121 static_cast<lttng_viewer_next_index_return_code>(rp_status));
1122 lttng_live_need_new_streams(lttng_live_msg_iter);
1123 }
1124
1125 switch (rp_status) {
1126 case LTTNG_VIEWER_INDEX_INACTIVE:
1127 {
1128 uint64_t ctf_stream_class_id;
1129
1130 memset(index, 0, sizeof(struct packet_index));
1131 index->ts_cycles.timestamp_end = be64toh(rp.timestamp_end);
1132 stream->current_inactivity_ts = index->ts_cycles.timestamp_end;
1133 ctf_stream_class_id = be64toh(rp.stream_id);
1134 if (stream->ctf_stream_class_id.is_set) {
1135 BT_ASSERT(stream->ctf_stream_class_id.value == ctf_stream_class_id);
1136 } else {
1137 stream->ctf_stream_class_id.value = ctf_stream_class_id;
1138 stream->ctf_stream_class_id.is_set = true;
1139 }
1140 lttng_live_stream_iterator_set_state(stream, LTTNG_LIVE_STREAM_QUIESCENT);
1141 return LTTNG_LIVE_ITERATOR_STATUS_OK;
1142 }
1143 case LTTNG_VIEWER_INDEX_OK:
1144 {
1145 uint64_t ctf_stream_class_id;
1146
1147 lttng_index_to_packet_index(&rp, index);
1148 ctf_stream_class_id = be64toh(rp.stream_id);
1149 if (stream->ctf_stream_class_id.is_set) {
1150 BT_ASSERT(stream->ctf_stream_class_id.value == ctf_stream_class_id);
1151 } else {
1152 stream->ctf_stream_class_id.value = ctf_stream_class_id;
1153 stream->ctf_stream_class_id.is_set = true;
1154 }
1155 lttng_live_stream_iterator_set_state(stream, LTTNG_LIVE_STREAM_ACTIVE_DATA);
1156
1157 if (flags & LTTNG_VIEWER_FLAG_NEW_METADATA) {
1158 BT_CPPLOGD_SPEC(viewer_connection->logger,
1159 "Marking trace as needing new metadata: "
1160 "response={}, response-flag=NEW_METADATA, trace-id={}",
1161 static_cast<lttng_viewer_next_index_return_code>(rp_status), trace->id);
1162 trace->metadata_stream_state = LTTNG_LIVE_METADATA_STREAM_STATE_NEEDED;
1163 }
1164 return LTTNG_LIVE_ITERATOR_STATUS_OK;
1165 break;
1166 }
1167 case LTTNG_VIEWER_INDEX_RETRY:
1168 memset(index, 0, sizeof(struct packet_index));
1169 lttng_live_stream_iterator_set_state(stream, LTTNG_LIVE_STREAM_ACTIVE_NO_DATA);
1170 return LTTNG_LIVE_ITERATOR_STATUS_AGAIN;
1171 case LTTNG_VIEWER_INDEX_HUP:
1172 memset(index, 0, sizeof(struct packet_index));
1173 index->offset = EOF;
1174 lttng_live_stream_iterator_set_state(stream, LTTNG_LIVE_STREAM_EOF);
1175 stream->has_stream_hung_up = true;
1176 return LTTNG_LIVE_ITERATOR_STATUS_END;
1177 case LTTNG_VIEWER_INDEX_ERR:
1178 memset(index, 0, sizeof(struct packet_index));
1179 lttng_live_stream_iterator_set_state(stream, LTTNG_LIVE_STREAM_ACTIVE_NO_DATA);
1180 return LTTNG_LIVE_ITERATOR_STATUS_ERROR;
1181 default:
1182 BT_CPPLOGD_SPEC(viewer_connection->logger,
1183 "Received get_next_index response: unknown value");
1184 memset(index, 0, sizeof(struct packet_index));
1185 lttng_live_stream_iterator_set_state(stream, LTTNG_LIVE_STREAM_ACTIVE_NO_DATA);
1186 return LTTNG_LIVE_ITERATOR_STATUS_ERROR;
1187 }
1188 }
1189
1190 lttng_live_get_stream_bytes_status
1191 lttng_live_get_stream_bytes(struct lttng_live_msg_iter *lttng_live_msg_iter,
1192 struct lttng_live_stream_iterator *stream, uint8_t *buf,
1193 uint64_t offset, uint64_t req_len, uint64_t *recv_len)
1194 {
1195 enum lttng_live_viewer_status viewer_status;
1196 struct lttng_viewer_trace_packet rp;
1197 struct lttng_viewer_cmd cmd;
1198 struct lttng_viewer_get_packet rq;
1199 live_viewer_connection *viewer_connection = lttng_live_msg_iter->viewer_connection.get();
1200 struct lttng_live_trace *trace = stream->trace;
1201 const size_t cmd_buf_len = sizeof(cmd) + sizeof(rq);
1202 char cmd_buf[cmd_buf_len];
1203 uint32_t flags, rp_status;
1204
1205 BT_CPPLOGD_SPEC(viewer_connection->logger,
1206 "Requesting data from stream: cmd={}, "
1207 "offset={}, request-len={}",
1208 LTTNG_VIEWER_GET_PACKET, offset, req_len);
1209
1210 cmd.cmd = htobe32(LTTNG_VIEWER_GET_PACKET);
1211 cmd.data_size = htobe64((uint64_t) sizeof(rq));
1212 cmd.cmd_version = htobe32(0);
1213
1214 memset(&rq, 0, sizeof(rq));
1215 rq.stream_id = htobe64(stream->viewer_stream_id);
1216 rq.offset = htobe64(offset);
1217 rq.len = htobe32(req_len);
1218
1219 /*
1220 * Merge the cmd and connection request to prevent a write-write
1221 * sequence on the TCP socket. Otherwise, a delayed ACK will prevent the
1222 * second write to be performed quickly in presence of Nagle's algorithm.
1223 */
1224 memcpy(cmd_buf, &cmd, sizeof(cmd));
1225 memcpy(cmd_buf + sizeof(cmd), &rq, sizeof(rq));
1226
1227 viewer_status = lttng_live_send(viewer_connection, &cmd_buf, cmd_buf_len);
1228 if (viewer_status != LTTNG_LIVE_VIEWER_STATUS_OK) {
1229 viewer_handle_send_status(viewer_status, "get data packet command");
1230 return viewer_status_to_lttng_live_get_stream_bytes_status(viewer_status);
1231 }
1232
1233 viewer_status = lttng_live_recv(viewer_connection, &rp, sizeof(rp));
1234 if (viewer_status != LTTNG_LIVE_VIEWER_STATUS_OK) {
1235 viewer_handle_recv_status(viewer_status, "get data packet reply");
1236 return viewer_status_to_lttng_live_get_stream_bytes_status(viewer_status);
1237 }
1238
1239 flags = be32toh(rp.flags);
1240 rp_status = be32toh(rp.status);
1241
1242 BT_CPPLOGD_SPEC(
1243 viewer_connection->logger, "Received response from relay daemon: cmd={}, response={}",
1244 LTTNG_VIEWER_GET_PACKET, static_cast<lttng_viewer_get_packet_return_code>(rp_status));
1245 switch (rp_status) {
1246 case LTTNG_VIEWER_GET_PACKET_OK:
1247 req_len = be32toh(rp.len);
1248 BT_CPPLOGD_SPEC(viewer_connection->logger,
1249 "Got packet from relay daemon: response={}, packet-len={}",
1250 static_cast<lttng_viewer_get_packet_return_code>(rp_status), req_len);
1251 break;
1252 case LTTNG_VIEWER_GET_PACKET_RETRY:
1253 /* Unimplemented by relay daemon */
1254 return LTTNG_LIVE_GET_STREAM_BYTES_STATUS_AGAIN;
1255 case LTTNG_VIEWER_GET_PACKET_ERR:
1256 if (flags & LTTNG_VIEWER_FLAG_NEW_METADATA) {
1257 BT_CPPLOGD_SPEC(viewer_connection->logger,
1258 "Marking trace as needing new metadata: "
1259 "response={}, response-flag=NEW_METADATA, trace-id={}",
1260 static_cast<lttng_viewer_get_packet_return_code>(rp_status), trace->id);
1261 trace->metadata_stream_state = LTTNG_LIVE_METADATA_STREAM_STATE_NEEDED;
1262 }
1263 if (flags & LTTNG_VIEWER_FLAG_NEW_STREAM) {
1264 BT_CPPLOGD_SPEC(viewer_connection->logger,
1265 "Marking all sessions as possibly needing new streams: "
1266 "response={}, response-flag=NEW_STREAM",
1267 static_cast<lttng_viewer_get_packet_return_code>(rp_status));
1268 lttng_live_need_new_streams(lttng_live_msg_iter);
1269 }
1270 if (flags & (LTTNG_VIEWER_FLAG_NEW_METADATA | LTTNG_VIEWER_FLAG_NEW_STREAM)) {
1271 BT_CPPLOGD_SPEC(viewer_connection->logger,
1272 "Reply with any one flags set means we should retry: response={}",
1273 static_cast<lttng_viewer_get_packet_return_code>(rp_status));
1274 return LTTNG_LIVE_GET_STREAM_BYTES_STATUS_AGAIN;
1275 }
1276 BT_CPPLOGE_APPEND_CAUSE_SPEC(viewer_connection->logger,
1277 "Received get_data_packet response: error");
1278 return LTTNG_LIVE_GET_STREAM_BYTES_STATUS_ERROR;
1279 case LTTNG_VIEWER_GET_PACKET_EOF:
1280 return LTTNG_LIVE_GET_STREAM_BYTES_STATUS_EOF;
1281 default:
1282 BT_CPPLOGE_APPEND_CAUSE_SPEC(viewer_connection->logger,
1283 "Received get_data_packet response: unknown ({})", rp_status);
1284 return LTTNG_LIVE_GET_STREAM_BYTES_STATUS_ERROR;
1285 }
1286
1287 if (req_len == 0) {
1288 return LTTNG_LIVE_GET_STREAM_BYTES_STATUS_ERROR;
1289 }
1290
1291 viewer_status = lttng_live_recv(viewer_connection, buf, req_len);
1292 if (viewer_status != LTTNG_LIVE_VIEWER_STATUS_OK) {
1293 viewer_handle_recv_status(viewer_status, "get data packet");
1294 return viewer_status_to_lttng_live_get_stream_bytes_status(viewer_status);
1295 }
1296 *recv_len = req_len;
1297
1298 return LTTNG_LIVE_GET_STREAM_BYTES_STATUS_OK;
1299 }
1300
1301 /*
1302 * Request new streams for a session.
1303 */
1304 enum lttng_live_iterator_status
1305 lttng_live_session_get_new_streams(struct lttng_live_session *session)
1306 {
1307 struct lttng_viewer_cmd cmd;
1308 struct lttng_viewer_new_streams_request rq;
1309 struct lttng_viewer_new_streams_response rp;
1310 struct lttng_live_msg_iter *lttng_live_msg_iter = session->lttng_live_msg_iter;
1311 enum lttng_live_viewer_status viewer_status;
1312 live_viewer_connection *viewer_connection = lttng_live_msg_iter->viewer_connection.get();
1313 uint32_t streams_count;
1314 const size_t cmd_buf_len = sizeof(cmd) + sizeof(rq);
1315 char cmd_buf[cmd_buf_len];
1316
1317 if (!session->new_streams_needed) {
1318 return LTTNG_LIVE_ITERATOR_STATUS_OK;
1319 }
1320
1321 BT_CPPLOGD_SPEC(viewer_connection->logger,
1322 "Requesting new streams for session: cmd={}, session-id={}",
1323 LTTNG_VIEWER_GET_NEW_STREAMS, session->id);
1324
1325 cmd.cmd = htobe32(LTTNG_VIEWER_GET_NEW_STREAMS);
1326 cmd.data_size = htobe64((uint64_t) sizeof(rq));
1327 cmd.cmd_version = htobe32(0);
1328
1329 memset(&rq, 0, sizeof(rq));
1330 rq.session_id = htobe64(session->id);
1331
1332 /*
1333 * Merge the cmd and connection request to prevent a write-write
1334 * sequence on the TCP socket. Otherwise, a delayed ACK will prevent the
1335 * second write to be performed quickly in presence of Nagle's algorithm.
1336 */
1337 memcpy(cmd_buf, &cmd, sizeof(cmd));
1338 memcpy(cmd_buf + sizeof(cmd), &rq, sizeof(rq));
1339
1340 viewer_status = lttng_live_send(viewer_connection, &cmd_buf, cmd_buf_len);
1341 if (viewer_status != LTTNG_LIVE_VIEWER_STATUS_OK) {
1342 viewer_handle_send_status(viewer_status, "get new streams command");
1343 return viewer_status_to_live_iterator_status(viewer_status);
1344 }
1345
1346 viewer_status = lttng_live_recv(viewer_connection, &rp, sizeof(rp));
1347 if (viewer_status != LTTNG_LIVE_VIEWER_STATUS_OK) {
1348 viewer_handle_recv_status(viewer_status, "get new streams reply");
1349 return viewer_status_to_live_iterator_status(viewer_status);
1350 }
1351
1352 streams_count = be32toh(rp.streams_count);
1353
1354 switch (be32toh(rp.status)) {
1355 case LTTNG_VIEWER_NEW_STREAMS_OK:
1356 session->new_streams_needed = false;
1357 break;
1358 case LTTNG_VIEWER_NEW_STREAMS_NO_NEW:
1359 session->new_streams_needed = false;
1360 return LTTNG_LIVE_ITERATOR_STATUS_OK;
1361 case LTTNG_VIEWER_NEW_STREAMS_HUP:
1362 session->new_streams_needed = false;
1363 session->closed = true;
1364 return LTTNG_LIVE_ITERATOR_STATUS_END;
1365 case LTTNG_VIEWER_NEW_STREAMS_ERR:
1366 BT_CPPLOGD_SPEC(viewer_connection->logger, "Received get_new_streams response: error");
1367 return LTTNG_LIVE_ITERATOR_STATUS_ERROR;
1368 default:
1369 BT_CPPLOGE_APPEND_CAUSE_SPEC(viewer_connection->logger,
1370 "Received get_new_streams response: Unknown return code {}",
1371 be32toh(rp.status));
1372 return LTTNG_LIVE_ITERATOR_STATUS_ERROR;
1373 }
1374
1375 viewer_status = receive_streams(session, streams_count);
1376 if (viewer_status != LTTNG_LIVE_VIEWER_STATUS_OK) {
1377 viewer_handle_recv_status(viewer_status, "new streams");
1378 return viewer_status_to_live_iterator_status(viewer_status);
1379 }
1380
1381 return LTTNG_LIVE_ITERATOR_STATUS_OK;
1382 }
1383
1384 enum lttng_live_viewer_status
1385 live_viewer_connection_create(const char *url, bool in_query,
1386 struct lttng_live_msg_iter *lttng_live_msg_iter,
1387 const bt2c::Logger& parentLogger, live_viewer_connection::UP& viewer)
1388 {
1389 auto viewer_connection = bt2s::make_unique<live_viewer_connection>(parentLogger);
1390
1391 if (bt_socket_init(viewer_connection->logger) != 0) {
1392 BT_CPPLOGE_APPEND_CAUSE_SPEC(viewer_connection->logger, "Failed to init socket");
1393 return LTTNG_LIVE_VIEWER_STATUS_ERROR;
1394 }
1395
1396 viewer_connection->control_sock = BT_INVALID_SOCKET;
1397 viewer_connection->port = -1;
1398 viewer_connection->in_query = in_query;
1399 viewer_connection->lttng_live_msg_iter = lttng_live_msg_iter;
1400 viewer_connection->url = url;
1401
1402 BT_CPPLOGD_SPEC(viewer_connection->logger, "Establishing connection to url \"{}\"...", url);
1403 const auto status = lttng_live_connect_viewer(viewer_connection.get());
1404
1405 /*
1406 * Only print error and append cause in case of error. not in case of
1407 * interruption.
1408 */
1409 if (status == LTTNG_LIVE_VIEWER_STATUS_ERROR) {
1410 BT_CPPLOGE_APPEND_CAUSE_SPEC(viewer_connection->logger,
1411 "Failed to establish connection: "
1412 "url=\"{}\"",
1413 url);
1414 return status;
1415 } else if (status == LTTNG_LIVE_VIEWER_STATUS_INTERRUPTED) {
1416 return status;
1417 }
1418 BT_CPPLOGD_SPEC(viewer_connection->logger, "Connection to url \"{}\" is established", url);
1419
1420 viewer = std::move(viewer_connection);
1421 return LTTNG_LIVE_VIEWER_STATUS_OK;
1422 }
1423
1424 live_viewer_connection::~live_viewer_connection()
1425 {
1426 BT_CPPLOGD_SPEC(this->logger, "Closing connection to relay: relay-url=\"{}\"", this->url);
1427
1428 viewer_connection_close_socket(this);
1429
1430 bt_socket_fini();
1431 }
This page took 0.059042 seconds and 5 git commands to generate.