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