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