| 1 | /* |
| 2 | * Copyright (C) 2012 - David Goulet <dgoulet@efficios.com> |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify it |
| 5 | * under the terms of the GNU General Public License, version 2 only, as |
| 6 | * published by the Free Software Foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, but WITHOUT |
| 9 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 10 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 11 | * more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public License along with |
| 14 | * this program; if not, write to the Free Software Foundation, Inc., 51 |
| 15 | * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 16 | */ |
| 17 | |
| 18 | #define _LGPL_SOURCE |
| 19 | #include <assert.h> |
| 20 | #include <stdio.h> |
| 21 | #include <stdlib.h> |
| 22 | #include <string.h> |
| 23 | #include <sys/stat.h> |
| 24 | #include <inttypes.h> |
| 25 | |
| 26 | #include <common/common.h> |
| 27 | #include <common/defaults.h> |
| 28 | #include <common/compat/endian.h> |
| 29 | #include <common/sessiond-comm/relayd.h> |
| 30 | #include <common/index/ctf-index.h> |
| 31 | |
| 32 | #include "relayd.h" |
| 33 | |
| 34 | /* |
| 35 | * Send command. Fill up the header and append the data. |
| 36 | */ |
| 37 | static int send_command(struct lttcomm_relayd_sock *rsock, |
| 38 | enum lttcomm_relayd_command cmd, void *data, size_t size, |
| 39 | int flags) |
| 40 | { |
| 41 | int ret; |
| 42 | struct lttcomm_relayd_hdr header; |
| 43 | char *buf; |
| 44 | uint64_t buf_size = sizeof(header); |
| 45 | |
| 46 | if (rsock->sock.fd < 0) { |
| 47 | return -ECONNRESET; |
| 48 | } |
| 49 | |
| 50 | if (data) { |
| 51 | buf_size += size; |
| 52 | } |
| 53 | |
| 54 | buf = zmalloc(buf_size); |
| 55 | if (buf == NULL) { |
| 56 | PERROR("zmalloc relayd send command buf"); |
| 57 | ret = -1; |
| 58 | goto alloc_error; |
| 59 | } |
| 60 | |
| 61 | memset(&header, 0, sizeof(header)); |
| 62 | header.cmd = htobe32(cmd); |
| 63 | header.data_size = htobe64(size); |
| 64 | |
| 65 | /* Zeroed for now since not used. */ |
| 66 | header.cmd_version = 0; |
| 67 | header.circuit_id = 0; |
| 68 | |
| 69 | /* Prepare buffer to send. */ |
| 70 | memcpy(buf, &header, sizeof(header)); |
| 71 | if (data) { |
| 72 | memcpy(buf + sizeof(header), data, size); |
| 73 | } |
| 74 | |
| 75 | ret = rsock->sock.ops->sendmsg(&rsock->sock, buf, buf_size, flags); |
| 76 | if (ret < 0) { |
| 77 | ret = -errno; |
| 78 | goto error; |
| 79 | } |
| 80 | |
| 81 | DBG3("Relayd sending command %d of size %" PRIu64, cmd, buf_size); |
| 82 | |
| 83 | error: |
| 84 | free(buf); |
| 85 | alloc_error: |
| 86 | return ret; |
| 87 | } |
| 88 | |
| 89 | /* |
| 90 | * Receive reply data on socket. This MUST be call after send_command or else |
| 91 | * could result in unexpected behavior(s). |
| 92 | */ |
| 93 | static int recv_reply(struct lttcomm_relayd_sock *rsock, void *data, size_t size) |
| 94 | { |
| 95 | int ret; |
| 96 | |
| 97 | if (rsock->sock.fd < 0) { |
| 98 | return -ECONNRESET; |
| 99 | } |
| 100 | |
| 101 | DBG3("Relayd waiting for reply of size %zu", size); |
| 102 | |
| 103 | ret = rsock->sock.ops->recvmsg(&rsock->sock, data, size, 0); |
| 104 | if (ret <= 0 || ret != size) { |
| 105 | if (ret == 0) { |
| 106 | /* Orderly shutdown. */ |
| 107 | DBG("Socket %d has performed an orderly shutdown", rsock->sock.fd); |
| 108 | } else { |
| 109 | DBG("Receiving reply failed on sock %d for size %zu with ret %d", |
| 110 | rsock->sock.fd, size, ret); |
| 111 | } |
| 112 | /* Always return -1 here and the caller can use errno. */ |
| 113 | ret = -1; |
| 114 | goto error; |
| 115 | } |
| 116 | |
| 117 | error: |
| 118 | return ret; |
| 119 | } |
| 120 | |
| 121 | /* |
| 122 | * Starting at 2.4, RELAYD_CREATE_SESSION takes additional parameters to |
| 123 | * support the live reading capability. |
| 124 | */ |
| 125 | static int relayd_create_session_2_4(struct lttcomm_relayd_sock *rsock, |
| 126 | uint64_t *session_id, char *session_name, char *hostname, |
| 127 | int session_live_timer, unsigned int snapshot) |
| 128 | { |
| 129 | int ret; |
| 130 | struct lttcomm_relayd_create_session_2_4 msg; |
| 131 | |
| 132 | if (strlen(session_name) >= sizeof(msg.session_name)) { |
| 133 | ret = -1; |
| 134 | goto error; |
| 135 | } |
| 136 | strncpy(msg.session_name, session_name, sizeof(msg.session_name)); |
| 137 | if (strlen(hostname) >= sizeof(msg.hostname)) { |
| 138 | ret = -1; |
| 139 | goto error; |
| 140 | } |
| 141 | strncpy(msg.hostname, hostname, sizeof(msg.hostname)); |
| 142 | msg.live_timer = htobe32(session_live_timer); |
| 143 | msg.snapshot = htobe32(snapshot); |
| 144 | |
| 145 | /* Send command */ |
| 146 | ret = send_command(rsock, RELAYD_CREATE_SESSION, &msg, sizeof(msg), 0); |
| 147 | if (ret < 0) { |
| 148 | goto error; |
| 149 | } |
| 150 | |
| 151 | error: |
| 152 | return ret; |
| 153 | } |
| 154 | |
| 155 | /* |
| 156 | * RELAYD_CREATE_SESSION from 2.1 to 2.3. |
| 157 | */ |
| 158 | static int relayd_create_session_2_1(struct lttcomm_relayd_sock *rsock, |
| 159 | uint64_t *session_id) |
| 160 | { |
| 161 | int ret; |
| 162 | |
| 163 | /* Send command */ |
| 164 | ret = send_command(rsock, RELAYD_CREATE_SESSION, NULL, 0, 0); |
| 165 | if (ret < 0) { |
| 166 | goto error; |
| 167 | } |
| 168 | |
| 169 | error: |
| 170 | return ret; |
| 171 | } |
| 172 | |
| 173 | /* |
| 174 | * Send a RELAYD_CREATE_SESSION command to the relayd with the given socket and |
| 175 | * set session_id of the relayd if we have a successful reply from the relayd. |
| 176 | * |
| 177 | * On success, return 0 else a negative value which is either an errno error or |
| 178 | * a lttng error code from the relayd. |
| 179 | */ |
| 180 | int relayd_create_session(struct lttcomm_relayd_sock *rsock, uint64_t *session_id, |
| 181 | char *session_name, char *hostname, int session_live_timer, |
| 182 | unsigned int snapshot) |
| 183 | { |
| 184 | int ret; |
| 185 | struct lttcomm_relayd_status_session reply; |
| 186 | |
| 187 | assert(rsock); |
| 188 | assert(session_id); |
| 189 | |
| 190 | DBG("Relayd create session"); |
| 191 | |
| 192 | switch(rsock->minor) { |
| 193 | case 1: |
| 194 | case 2: |
| 195 | case 3: |
| 196 | ret = relayd_create_session_2_1(rsock, session_id); |
| 197 | break; |
| 198 | case 4: |
| 199 | default: |
| 200 | ret = relayd_create_session_2_4(rsock, session_id, session_name, |
| 201 | hostname, session_live_timer, snapshot); |
| 202 | break; |
| 203 | } |
| 204 | |
| 205 | if (ret < 0) { |
| 206 | goto error; |
| 207 | } |
| 208 | |
| 209 | /* Receive response */ |
| 210 | ret = recv_reply(rsock, (void *) &reply, sizeof(reply)); |
| 211 | if (ret < 0) { |
| 212 | goto error; |
| 213 | } |
| 214 | |
| 215 | reply.session_id = be64toh(reply.session_id); |
| 216 | reply.ret_code = be32toh(reply.ret_code); |
| 217 | |
| 218 | /* Return session id or negative ret code. */ |
| 219 | if (reply.ret_code != LTTNG_OK) { |
| 220 | ret = -1; |
| 221 | ERR("Relayd create session replied error %d", reply.ret_code); |
| 222 | goto error; |
| 223 | } else { |
| 224 | ret = 0; |
| 225 | *session_id = reply.session_id; |
| 226 | } |
| 227 | |
| 228 | DBG("Relayd session created with id %" PRIu64, reply.session_id); |
| 229 | |
| 230 | error: |
| 231 | return ret; |
| 232 | } |
| 233 | |
| 234 | /* |
| 235 | * Add stream on the relayd and assign stream handle to the stream_id argument. |
| 236 | * |
| 237 | * On success return 0 else return ret_code negative value. |
| 238 | */ |
| 239 | int relayd_add_stream(struct lttcomm_relayd_sock *rsock, const char *channel_name, |
| 240 | const char *pathname, uint64_t *stream_id, |
| 241 | uint64_t tracefile_size, uint64_t tracefile_count) |
| 242 | { |
| 243 | int ret; |
| 244 | struct lttcomm_relayd_add_stream msg; |
| 245 | struct lttcomm_relayd_add_stream_2_2 msg_2_2; |
| 246 | struct lttcomm_relayd_status_stream reply; |
| 247 | |
| 248 | /* Code flow error. Safety net. */ |
| 249 | assert(rsock); |
| 250 | assert(channel_name); |
| 251 | assert(pathname); |
| 252 | |
| 253 | DBG("Relayd adding stream for channel name %s", channel_name); |
| 254 | |
| 255 | /* Compat with relayd 2.1 */ |
| 256 | if (rsock->minor == 1) { |
| 257 | memset(&msg, 0, sizeof(msg)); |
| 258 | if (strlen(channel_name) >= sizeof(msg.channel_name)) { |
| 259 | ret = -1; |
| 260 | goto error; |
| 261 | } |
| 262 | strncpy(msg.channel_name, channel_name, sizeof(msg.channel_name)); |
| 263 | if (strlen(pathname) >= sizeof(msg.pathname)) { |
| 264 | ret = -1; |
| 265 | goto error; |
| 266 | } |
| 267 | strncpy(msg.pathname, pathname, sizeof(msg.pathname)); |
| 268 | |
| 269 | /* Send command */ |
| 270 | ret = send_command(rsock, RELAYD_ADD_STREAM, (void *) &msg, sizeof(msg), 0); |
| 271 | if (ret < 0) { |
| 272 | goto error; |
| 273 | } |
| 274 | } else { |
| 275 | memset(&msg_2_2, 0, sizeof(msg_2_2)); |
| 276 | /* Compat with relayd 2.2+ */ |
| 277 | if (strlen(channel_name) >= sizeof(msg_2_2.channel_name)) { |
| 278 | ret = -1; |
| 279 | goto error; |
| 280 | } |
| 281 | strncpy(msg_2_2.channel_name, channel_name, sizeof(msg_2_2.channel_name)); |
| 282 | if (strlen(pathname) >= sizeof(msg_2_2.pathname)) { |
| 283 | ret = -1; |
| 284 | goto error; |
| 285 | } |
| 286 | strncpy(msg_2_2.pathname, pathname, sizeof(msg_2_2.pathname)); |
| 287 | msg_2_2.tracefile_size = htobe64(tracefile_size); |
| 288 | msg_2_2.tracefile_count = htobe64(tracefile_count); |
| 289 | |
| 290 | /* Send command */ |
| 291 | ret = send_command(rsock, RELAYD_ADD_STREAM, (void *) &msg_2_2, sizeof(msg_2_2), 0); |
| 292 | if (ret < 0) { |
| 293 | goto error; |
| 294 | } |
| 295 | } |
| 296 | |
| 297 | /* Waiting for reply */ |
| 298 | ret = recv_reply(rsock, (void *) &reply, sizeof(reply)); |
| 299 | if (ret < 0) { |
| 300 | goto error; |
| 301 | } |
| 302 | |
| 303 | /* Back to host bytes order. */ |
| 304 | reply.handle = be64toh(reply.handle); |
| 305 | reply.ret_code = be32toh(reply.ret_code); |
| 306 | |
| 307 | /* Return session id or negative ret code. */ |
| 308 | if (reply.ret_code != LTTNG_OK) { |
| 309 | ret = -1; |
| 310 | ERR("Relayd add stream replied error %d", reply.ret_code); |
| 311 | } else { |
| 312 | /* Success */ |
| 313 | ret = 0; |
| 314 | *stream_id = reply.handle; |
| 315 | } |
| 316 | |
| 317 | DBG("Relayd stream added successfully with handle %" PRIu64, |
| 318 | reply.handle); |
| 319 | |
| 320 | error: |
| 321 | return ret; |
| 322 | } |
| 323 | |
| 324 | /* |
| 325 | * Inform the relay that all the streams for the current channel has been sent. |
| 326 | * |
| 327 | * On success return 0 else return ret_code negative value. |
| 328 | */ |
| 329 | int relayd_streams_sent(struct lttcomm_relayd_sock *rsock) |
| 330 | { |
| 331 | int ret; |
| 332 | struct lttcomm_relayd_generic_reply reply; |
| 333 | |
| 334 | /* Code flow error. Safety net. */ |
| 335 | assert(rsock); |
| 336 | |
| 337 | DBG("Relayd sending streams sent."); |
| 338 | |
| 339 | /* This feature was introduced in 2.4, ignore it for earlier versions. */ |
| 340 | if (rsock->minor < 4) { |
| 341 | ret = 0; |
| 342 | goto end; |
| 343 | } |
| 344 | |
| 345 | /* Send command */ |
| 346 | ret = send_command(rsock, RELAYD_STREAMS_SENT, NULL, 0, 0); |
| 347 | if (ret < 0) { |
| 348 | goto error; |
| 349 | } |
| 350 | |
| 351 | /* Waiting for reply */ |
| 352 | ret = recv_reply(rsock, (void *) &reply, sizeof(reply)); |
| 353 | if (ret < 0) { |
| 354 | goto error; |
| 355 | } |
| 356 | |
| 357 | /* Back to host bytes order. */ |
| 358 | reply.ret_code = be32toh(reply.ret_code); |
| 359 | |
| 360 | /* Return session id or negative ret code. */ |
| 361 | if (reply.ret_code != LTTNG_OK) { |
| 362 | ret = -1; |
| 363 | ERR("Relayd streams sent replied error %d", reply.ret_code); |
| 364 | goto error; |
| 365 | } else { |
| 366 | /* Success */ |
| 367 | ret = 0; |
| 368 | } |
| 369 | |
| 370 | DBG("Relayd streams sent success"); |
| 371 | |
| 372 | error: |
| 373 | end: |
| 374 | return ret; |
| 375 | } |
| 376 | |
| 377 | /* |
| 378 | * Check version numbers on the relayd. |
| 379 | * If major versions are compatible, we assign minor_to_use to the |
| 380 | * minor version of the procotol we are going to use for this session. |
| 381 | * |
| 382 | * Return 0 if compatible else negative value. |
| 383 | */ |
| 384 | int relayd_version_check(struct lttcomm_relayd_sock *rsock) |
| 385 | { |
| 386 | int ret; |
| 387 | struct lttcomm_relayd_version msg; |
| 388 | |
| 389 | /* Code flow error. Safety net. */ |
| 390 | assert(rsock); |
| 391 | |
| 392 | DBG("Relayd version check for major.minor %u.%u", rsock->major, |
| 393 | rsock->minor); |
| 394 | |
| 395 | memset(&msg, 0, sizeof(msg)); |
| 396 | /* Prepare network byte order before transmission. */ |
| 397 | msg.major = htobe32(rsock->major); |
| 398 | msg.minor = htobe32(rsock->minor); |
| 399 | |
| 400 | /* Send command */ |
| 401 | ret = send_command(rsock, RELAYD_VERSION, (void *) &msg, sizeof(msg), 0); |
| 402 | if (ret < 0) { |
| 403 | goto error; |
| 404 | } |
| 405 | |
| 406 | /* Receive response */ |
| 407 | ret = recv_reply(rsock, (void *) &msg, sizeof(msg)); |
| 408 | if (ret < 0) { |
| 409 | goto error; |
| 410 | } |
| 411 | |
| 412 | /* Set back to host bytes order */ |
| 413 | msg.major = be32toh(msg.major); |
| 414 | msg.minor = be32toh(msg.minor); |
| 415 | |
| 416 | /* |
| 417 | * Only validate the major version. If the other side is higher, |
| 418 | * communication is not possible. Only major version equal can talk to each |
| 419 | * other. If the minor version differs, the lowest version is used by both |
| 420 | * sides. |
| 421 | */ |
| 422 | if (msg.major != rsock->major) { |
| 423 | /* Not compatible */ |
| 424 | ret = -1; |
| 425 | DBG2("Relayd version is NOT compatible. Relayd version %u != %u (us)", |
| 426 | msg.major, rsock->major); |
| 427 | goto error; |
| 428 | } |
| 429 | |
| 430 | /* |
| 431 | * If the relayd's minor version is higher, it will adapt to our version so |
| 432 | * we can continue to use the latest relayd communication data structure. |
| 433 | * If the received minor version is higher, the relayd should adapt to us. |
| 434 | */ |
| 435 | if (rsock->minor > msg.minor) { |
| 436 | rsock->minor = msg.minor; |
| 437 | } |
| 438 | |
| 439 | /* Version number compatible */ |
| 440 | DBG2("Relayd version is compatible, using protocol version %u.%u", |
| 441 | rsock->major, rsock->minor); |
| 442 | ret = 0; |
| 443 | |
| 444 | error: |
| 445 | return ret; |
| 446 | } |
| 447 | |
| 448 | /* |
| 449 | * Add stream on the relayd and assign stream handle to the stream_id argument. |
| 450 | * |
| 451 | * On success return 0 else return ret_code negative value. |
| 452 | */ |
| 453 | int relayd_send_metadata(struct lttcomm_relayd_sock *rsock, size_t len) |
| 454 | { |
| 455 | int ret; |
| 456 | |
| 457 | /* Code flow error. Safety net. */ |
| 458 | assert(rsock); |
| 459 | |
| 460 | DBG("Relayd sending metadata of size %zu", len); |
| 461 | |
| 462 | /* Send command */ |
| 463 | ret = send_command(rsock, RELAYD_SEND_METADATA, NULL, len, 0); |
| 464 | if (ret < 0) { |
| 465 | goto error; |
| 466 | } |
| 467 | |
| 468 | DBG2("Relayd metadata added successfully"); |
| 469 | |
| 470 | /* |
| 471 | * After that call, the metadata data MUST be sent to the relayd so the |
| 472 | * receive size on the other end matches the len of the metadata packet |
| 473 | * header. This is why we don't wait for a reply here. |
| 474 | */ |
| 475 | |
| 476 | error: |
| 477 | return ret; |
| 478 | } |
| 479 | |
| 480 | /* |
| 481 | * Connect to relay daemon with an allocated lttcomm_relayd_sock. |
| 482 | */ |
| 483 | int relayd_connect(struct lttcomm_relayd_sock *rsock) |
| 484 | { |
| 485 | /* Code flow error. Safety net. */ |
| 486 | assert(rsock); |
| 487 | |
| 488 | if (!rsock->sock.ops) { |
| 489 | /* |
| 490 | * Attempting a connect on a non-initialized socket. |
| 491 | */ |
| 492 | return -ECONNRESET; |
| 493 | } |
| 494 | |
| 495 | DBG3("Relayd connect ..."); |
| 496 | |
| 497 | return rsock->sock.ops->connect(&rsock->sock); |
| 498 | } |
| 499 | |
| 500 | /* |
| 501 | * Close relayd socket with an allocated lttcomm_relayd_sock. |
| 502 | * |
| 503 | * If no socket operations are found, simply return 0 meaning that everything |
| 504 | * is fine. Without operations, the socket can not possibly be opened or used. |
| 505 | * This is possible if the socket was allocated but not created. However, the |
| 506 | * caller could simply use it to store a valid file descriptor for instance |
| 507 | * passed over a Unix socket and call this to cleanup but still without a valid |
| 508 | * ops pointer. |
| 509 | * |
| 510 | * Return the close returned value. On error, a negative value is usually |
| 511 | * returned back from close(2). |
| 512 | */ |
| 513 | int relayd_close(struct lttcomm_relayd_sock *rsock) |
| 514 | { |
| 515 | int ret; |
| 516 | |
| 517 | /* Code flow error. Safety net. */ |
| 518 | assert(rsock); |
| 519 | |
| 520 | /* An invalid fd is fine, return success. */ |
| 521 | if (rsock->sock.fd < 0) { |
| 522 | ret = 0; |
| 523 | goto end; |
| 524 | } |
| 525 | |
| 526 | DBG3("Relayd closing socket %d", rsock->sock.fd); |
| 527 | |
| 528 | if (rsock->sock.ops) { |
| 529 | ret = rsock->sock.ops->close(&rsock->sock); |
| 530 | } else { |
| 531 | /* Default call if no specific ops found. */ |
| 532 | ret = close(rsock->sock.fd); |
| 533 | if (ret < 0) { |
| 534 | PERROR("relayd_close default close"); |
| 535 | } |
| 536 | } |
| 537 | rsock->sock.fd = -1; |
| 538 | |
| 539 | end: |
| 540 | return ret; |
| 541 | } |
| 542 | |
| 543 | /* |
| 544 | * Send data header structure to the relayd. |
| 545 | */ |
| 546 | int relayd_send_data_hdr(struct lttcomm_relayd_sock *rsock, |
| 547 | struct lttcomm_relayd_data_hdr *hdr, size_t size) |
| 548 | { |
| 549 | int ret; |
| 550 | |
| 551 | /* Code flow error. Safety net. */ |
| 552 | assert(rsock); |
| 553 | assert(hdr); |
| 554 | |
| 555 | if (rsock->sock.fd < 0) { |
| 556 | return -ECONNRESET; |
| 557 | } |
| 558 | |
| 559 | DBG3("Relayd sending data header of size %zu", size); |
| 560 | |
| 561 | /* Again, safety net */ |
| 562 | if (size == 0) { |
| 563 | size = sizeof(struct lttcomm_relayd_data_hdr); |
| 564 | } |
| 565 | |
| 566 | /* Only send data header. */ |
| 567 | ret = rsock->sock.ops->sendmsg(&rsock->sock, hdr, size, 0); |
| 568 | if (ret < 0) { |
| 569 | ret = -errno; |
| 570 | goto error; |
| 571 | } |
| 572 | |
| 573 | /* |
| 574 | * The data MUST be sent right after that command for the receive on the |
| 575 | * other end to match the size in the header. |
| 576 | */ |
| 577 | |
| 578 | error: |
| 579 | return ret; |
| 580 | } |
| 581 | |
| 582 | /* |
| 583 | * Send close stream command to the relayd. |
| 584 | */ |
| 585 | int relayd_send_close_stream(struct lttcomm_relayd_sock *rsock, uint64_t stream_id, |
| 586 | uint64_t last_net_seq_num) |
| 587 | { |
| 588 | int ret; |
| 589 | struct lttcomm_relayd_close_stream msg; |
| 590 | struct lttcomm_relayd_generic_reply reply; |
| 591 | |
| 592 | /* Code flow error. Safety net. */ |
| 593 | assert(rsock); |
| 594 | |
| 595 | DBG("Relayd closing stream id %" PRIu64, stream_id); |
| 596 | |
| 597 | memset(&msg, 0, sizeof(msg)); |
| 598 | msg.stream_id = htobe64(stream_id); |
| 599 | msg.last_net_seq_num = htobe64(last_net_seq_num); |
| 600 | |
| 601 | /* Send command */ |
| 602 | ret = send_command(rsock, RELAYD_CLOSE_STREAM, (void *) &msg, sizeof(msg), 0); |
| 603 | if (ret < 0) { |
| 604 | goto error; |
| 605 | } |
| 606 | |
| 607 | /* Receive response */ |
| 608 | ret = recv_reply(rsock, (void *) &reply, sizeof(reply)); |
| 609 | if (ret < 0) { |
| 610 | goto error; |
| 611 | } |
| 612 | |
| 613 | reply.ret_code = be32toh(reply.ret_code); |
| 614 | |
| 615 | /* Return session id or negative ret code. */ |
| 616 | if (reply.ret_code != LTTNG_OK) { |
| 617 | ret = -1; |
| 618 | ERR("Relayd close stream replied error %d", reply.ret_code); |
| 619 | } else { |
| 620 | /* Success */ |
| 621 | ret = 0; |
| 622 | } |
| 623 | |
| 624 | DBG("Relayd close stream id %" PRIu64 " successfully", stream_id); |
| 625 | |
| 626 | error: |
| 627 | return ret; |
| 628 | } |
| 629 | |
| 630 | /* |
| 631 | * Check for data availability for a given stream id. |
| 632 | * |
| 633 | * Return 0 if NOT pending, 1 if so and a negative value on error. |
| 634 | */ |
| 635 | int relayd_data_pending(struct lttcomm_relayd_sock *rsock, uint64_t stream_id, |
| 636 | uint64_t last_net_seq_num) |
| 637 | { |
| 638 | int ret; |
| 639 | struct lttcomm_relayd_data_pending msg; |
| 640 | struct lttcomm_relayd_generic_reply reply; |
| 641 | |
| 642 | /* Code flow error. Safety net. */ |
| 643 | assert(rsock); |
| 644 | |
| 645 | DBG("Relayd data pending for stream id %" PRIu64, stream_id); |
| 646 | |
| 647 | memset(&msg, 0, sizeof(msg)); |
| 648 | msg.stream_id = htobe64(stream_id); |
| 649 | msg.last_net_seq_num = htobe64(last_net_seq_num); |
| 650 | |
| 651 | /* Send command */ |
| 652 | ret = send_command(rsock, RELAYD_DATA_PENDING, (void *) &msg, |
| 653 | sizeof(msg), 0); |
| 654 | if (ret < 0) { |
| 655 | goto error; |
| 656 | } |
| 657 | |
| 658 | /* Receive response */ |
| 659 | ret = recv_reply(rsock, (void *) &reply, sizeof(reply)); |
| 660 | if (ret < 0) { |
| 661 | goto error; |
| 662 | } |
| 663 | |
| 664 | reply.ret_code = be32toh(reply.ret_code); |
| 665 | |
| 666 | /* Return session id or negative ret code. */ |
| 667 | if (reply.ret_code >= LTTNG_OK) { |
| 668 | ERR("Relayd data pending replied error %d", reply.ret_code); |
| 669 | } |
| 670 | |
| 671 | /* At this point, the ret code is either 1 or 0 */ |
| 672 | ret = reply.ret_code; |
| 673 | |
| 674 | DBG("Relayd data is %s pending for stream id %" PRIu64, |
| 675 | ret == 1 ? "" : "NOT", stream_id); |
| 676 | |
| 677 | error: |
| 678 | return ret; |
| 679 | } |
| 680 | |
| 681 | /* |
| 682 | * Check on the relayd side for a quiescent state on the control socket. |
| 683 | */ |
| 684 | int relayd_quiescent_control(struct lttcomm_relayd_sock *rsock, |
| 685 | uint64_t metadata_stream_id) |
| 686 | { |
| 687 | int ret; |
| 688 | struct lttcomm_relayd_quiescent_control msg; |
| 689 | struct lttcomm_relayd_generic_reply reply; |
| 690 | |
| 691 | /* Code flow error. Safety net. */ |
| 692 | assert(rsock); |
| 693 | |
| 694 | DBG("Relayd checking quiescent control state"); |
| 695 | |
| 696 | memset(&msg, 0, sizeof(msg)); |
| 697 | msg.stream_id = htobe64(metadata_stream_id); |
| 698 | |
| 699 | /* Send command */ |
| 700 | ret = send_command(rsock, RELAYD_QUIESCENT_CONTROL, &msg, sizeof(msg), 0); |
| 701 | if (ret < 0) { |
| 702 | goto error; |
| 703 | } |
| 704 | |
| 705 | /* Receive response */ |
| 706 | ret = recv_reply(rsock, (void *) &reply, sizeof(reply)); |
| 707 | if (ret < 0) { |
| 708 | goto error; |
| 709 | } |
| 710 | |
| 711 | reply.ret_code = be32toh(reply.ret_code); |
| 712 | |
| 713 | /* Return session id or negative ret code. */ |
| 714 | if (reply.ret_code != LTTNG_OK) { |
| 715 | ret = -1; |
| 716 | ERR("Relayd quiescent control replied error %d", reply.ret_code); |
| 717 | goto error; |
| 718 | } |
| 719 | |
| 720 | /* Control socket is quiescent */ |
| 721 | return 0; |
| 722 | |
| 723 | error: |
| 724 | return ret; |
| 725 | } |
| 726 | |
| 727 | /* |
| 728 | * Begin a data pending command for a specific session id. |
| 729 | */ |
| 730 | int relayd_begin_data_pending(struct lttcomm_relayd_sock *rsock, uint64_t id) |
| 731 | { |
| 732 | int ret; |
| 733 | struct lttcomm_relayd_begin_data_pending msg; |
| 734 | struct lttcomm_relayd_generic_reply reply; |
| 735 | |
| 736 | /* Code flow error. Safety net. */ |
| 737 | assert(rsock); |
| 738 | |
| 739 | DBG("Relayd begin data pending"); |
| 740 | |
| 741 | memset(&msg, 0, sizeof(msg)); |
| 742 | msg.session_id = htobe64(id); |
| 743 | |
| 744 | /* Send command */ |
| 745 | ret = send_command(rsock, RELAYD_BEGIN_DATA_PENDING, &msg, sizeof(msg), 0); |
| 746 | if (ret < 0) { |
| 747 | goto error; |
| 748 | } |
| 749 | |
| 750 | /* Receive response */ |
| 751 | ret = recv_reply(rsock, (void *) &reply, sizeof(reply)); |
| 752 | if (ret < 0) { |
| 753 | goto error; |
| 754 | } |
| 755 | |
| 756 | reply.ret_code = be32toh(reply.ret_code); |
| 757 | |
| 758 | /* Return session id or negative ret code. */ |
| 759 | if (reply.ret_code != LTTNG_OK) { |
| 760 | ret = -1; |
| 761 | ERR("Relayd begin data pending replied error %d", reply.ret_code); |
| 762 | goto error; |
| 763 | } |
| 764 | |
| 765 | return 0; |
| 766 | |
| 767 | error: |
| 768 | return ret; |
| 769 | } |
| 770 | |
| 771 | /* |
| 772 | * End a data pending command for a specific session id. |
| 773 | * |
| 774 | * Return 0 on success and set is_data_inflight to 0 if no data is being |
| 775 | * streamed or 1 if it is the case. |
| 776 | */ |
| 777 | int relayd_end_data_pending(struct lttcomm_relayd_sock *rsock, uint64_t id, |
| 778 | unsigned int *is_data_inflight) |
| 779 | { |
| 780 | int ret, recv_ret; |
| 781 | struct lttcomm_relayd_end_data_pending msg; |
| 782 | struct lttcomm_relayd_generic_reply reply; |
| 783 | |
| 784 | /* Code flow error. Safety net. */ |
| 785 | assert(rsock); |
| 786 | |
| 787 | DBG("Relayd end data pending"); |
| 788 | |
| 789 | memset(&msg, 0, sizeof(msg)); |
| 790 | msg.session_id = htobe64(id); |
| 791 | |
| 792 | /* Send command */ |
| 793 | ret = send_command(rsock, RELAYD_END_DATA_PENDING, &msg, sizeof(msg), 0); |
| 794 | if (ret < 0) { |
| 795 | goto error; |
| 796 | } |
| 797 | |
| 798 | /* Receive response */ |
| 799 | ret = recv_reply(rsock, (void *) &reply, sizeof(reply)); |
| 800 | if (ret < 0) { |
| 801 | goto error; |
| 802 | } |
| 803 | |
| 804 | recv_ret = be32toh(reply.ret_code); |
| 805 | if (recv_ret < 0) { |
| 806 | ret = recv_ret; |
| 807 | goto error; |
| 808 | } |
| 809 | |
| 810 | *is_data_inflight = recv_ret; |
| 811 | |
| 812 | DBG("Relayd end data pending is data inflight: %d", recv_ret); |
| 813 | |
| 814 | return 0; |
| 815 | |
| 816 | error: |
| 817 | return ret; |
| 818 | } |
| 819 | |
| 820 | /* |
| 821 | * Send index to the relayd. |
| 822 | */ |
| 823 | int relayd_send_index(struct lttcomm_relayd_sock *rsock, |
| 824 | struct ctf_packet_index *index, uint64_t relay_stream_id, |
| 825 | uint64_t net_seq_num) |
| 826 | { |
| 827 | int ret; |
| 828 | struct lttcomm_relayd_index msg; |
| 829 | struct lttcomm_relayd_generic_reply reply; |
| 830 | |
| 831 | /* Code flow error. Safety net. */ |
| 832 | assert(rsock); |
| 833 | |
| 834 | if (rsock->minor < 4) { |
| 835 | DBG("Not sending indexes before protocol 2.4"); |
| 836 | ret = 0; |
| 837 | goto error; |
| 838 | } |
| 839 | |
| 840 | DBG("Relayd sending index for stream ID %" PRIu64, relay_stream_id); |
| 841 | |
| 842 | memset(&msg, 0, sizeof(msg)); |
| 843 | msg.relay_stream_id = htobe64(relay_stream_id); |
| 844 | msg.net_seq_num = htobe64(net_seq_num); |
| 845 | |
| 846 | /* The index is already in big endian. */ |
| 847 | msg.packet_size = index->packet_size; |
| 848 | msg.content_size = index->content_size; |
| 849 | msg.timestamp_begin = index->timestamp_begin; |
| 850 | msg.timestamp_end = index->timestamp_end; |
| 851 | msg.events_discarded = index->events_discarded; |
| 852 | msg.stream_id = index->stream_id; |
| 853 | |
| 854 | if (rsock->minor >= 8) { |
| 855 | msg.stream_instance_id = index->stream_instance_id; |
| 856 | msg.packet_seq_num = index->packet_seq_num; |
| 857 | } |
| 858 | |
| 859 | /* Send command */ |
| 860 | ret = send_command(rsock, RELAYD_SEND_INDEX, &msg, sizeof(msg), 0); |
| 861 | if (ret < 0) { |
| 862 | goto error; |
| 863 | } |
| 864 | |
| 865 | /* Receive response */ |
| 866 | ret = recv_reply(rsock, (void *) &reply, sizeof(reply)); |
| 867 | if (ret < 0) { |
| 868 | goto error; |
| 869 | } |
| 870 | |
| 871 | reply.ret_code = be32toh(reply.ret_code); |
| 872 | |
| 873 | /* Return session id or negative ret code. */ |
| 874 | if (reply.ret_code != LTTNG_OK) { |
| 875 | ret = -1; |
| 876 | ERR("Relayd send index replied error %d", reply.ret_code); |
| 877 | } else { |
| 878 | /* Success */ |
| 879 | ret = 0; |
| 880 | } |
| 881 | |
| 882 | error: |
| 883 | return ret; |
| 884 | } |
| 885 | |
| 886 | /* |
| 887 | * Ask the relay to reset the metadata trace file (regeneration). |
| 888 | */ |
| 889 | int relayd_reset_metadata(struct lttcomm_relayd_sock *rsock, |
| 890 | uint64_t stream_id, uint64_t version) |
| 891 | { |
| 892 | int ret; |
| 893 | struct lttcomm_relayd_reset_metadata msg; |
| 894 | struct lttcomm_relayd_generic_reply reply; |
| 895 | |
| 896 | /* Code flow error. Safety net. */ |
| 897 | assert(rsock); |
| 898 | |
| 899 | /* Should have been prevented by the sessiond. */ |
| 900 | if (rsock->minor < 8) { |
| 901 | ERR("Metadata regeneration unsupported before 2.8"); |
| 902 | ret = -1; |
| 903 | goto error; |
| 904 | } |
| 905 | |
| 906 | DBG("Relayd reset metadata stream id %" PRIu64, stream_id); |
| 907 | |
| 908 | memset(&msg, 0, sizeof(msg)); |
| 909 | msg.stream_id = htobe64(stream_id); |
| 910 | msg.version = htobe64(version); |
| 911 | |
| 912 | /* Send command */ |
| 913 | ret = send_command(rsock, RELAYD_RESET_METADATA, (void *) &msg, sizeof(msg), 0); |
| 914 | if (ret < 0) { |
| 915 | goto error; |
| 916 | } |
| 917 | |
| 918 | /* Receive response */ |
| 919 | ret = recv_reply(rsock, (void *) &reply, sizeof(reply)); |
| 920 | if (ret < 0) { |
| 921 | goto error; |
| 922 | } |
| 923 | |
| 924 | reply.ret_code = be32toh(reply.ret_code); |
| 925 | |
| 926 | /* Return session id or negative ret code. */ |
| 927 | if (reply.ret_code != LTTNG_OK) { |
| 928 | ret = -1; |
| 929 | ERR("Relayd reset metadata replied error %d", reply.ret_code); |
| 930 | } else { |
| 931 | /* Success */ |
| 932 | ret = 0; |
| 933 | } |
| 934 | |
| 935 | DBG("Relayd reset metadata stream id %" PRIu64 " successfully", stream_id); |
| 936 | |
| 937 | error: |
| 938 | return ret; |
| 939 | } |