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