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