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