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