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