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