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