Fix: don't create index on snapshot
[lttng-tools.git] / src / common / relayd / relayd.c
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 case 4:
188 default:
189 ret = relayd_create_session_2_4(rsock, session_id, session_name,
190 hostname, session_live_timer, snapshot);
191 }
192
193 if (ret < 0) {
194 goto error;
195 }
196
197 /* Receive response */
198 ret = recv_reply(rsock, (void *) &reply, sizeof(reply));
199 if (ret < 0) {
200 goto error;
201 }
202
203 reply.session_id = be64toh(reply.session_id);
204 reply.ret_code = be32toh(reply.ret_code);
205
206 /* Return session id or negative ret code. */
207 if (reply.ret_code != LTTNG_OK) {
208 ret = -1;
209 ERR("Relayd create session replied error %d", reply.ret_code);
210 goto error;
211 } else {
212 ret = 0;
213 *session_id = reply.session_id;
214 }
215
216 DBG("Relayd session created with id %" PRIu64, reply.session_id);
217
218 error:
219 return ret;
220 }
221
222 /*
223 * Add stream on the relayd and assign stream handle to the stream_id argument.
224 *
225 * On success return 0 else return ret_code negative value.
226 */
227 int relayd_add_stream(struct lttcomm_relayd_sock *rsock, const char *channel_name,
228 const char *pathname, uint64_t *stream_id,
229 uint64_t tracefile_size, uint64_t tracefile_count)
230 {
231 int ret;
232 struct lttcomm_relayd_add_stream msg;
233 struct lttcomm_relayd_add_stream_2_2 msg_2_2;
234 struct lttcomm_relayd_status_stream reply;
235
236 /* Code flow error. Safety net. */
237 assert(rsock);
238 assert(channel_name);
239 assert(pathname);
240
241 DBG("Relayd adding stream for channel name %s", channel_name);
242
243 /* Compat with relayd 2.1 */
244 if (rsock->minor == 1) {
245 strncpy(msg.channel_name, channel_name, sizeof(msg.channel_name));
246 strncpy(msg.pathname, pathname, sizeof(msg.pathname));
247
248 /* Send command */
249 ret = send_command(rsock, RELAYD_ADD_STREAM, (void *) &msg, sizeof(msg), 0);
250 if (ret < 0) {
251 goto error;
252 }
253 } else {
254 /* Compat with relayd 2.2+ */
255 strncpy(msg_2_2.channel_name, channel_name, sizeof(msg_2_2.channel_name));
256 strncpy(msg_2_2.pathname, pathname, sizeof(msg_2_2.pathname));
257 msg_2_2.tracefile_size = htobe64(tracefile_size);
258 msg_2_2.tracefile_count = htobe64(tracefile_count);
259
260 /* Send command */
261 ret = send_command(rsock, RELAYD_ADD_STREAM, (void *) &msg_2_2, sizeof(msg_2_2), 0);
262 if (ret < 0) {
263 goto error;
264 }
265 }
266
267 /* Waiting for reply */
268 ret = recv_reply(rsock, (void *) &reply, sizeof(reply));
269 if (ret < 0) {
270 goto error;
271 }
272
273 /* Back to host bytes order. */
274 reply.handle = be64toh(reply.handle);
275 reply.ret_code = be32toh(reply.ret_code);
276
277 /* Return session id or negative ret code. */
278 if (reply.ret_code != LTTNG_OK) {
279 ret = -1;
280 ERR("Relayd add stream replied error %d", reply.ret_code);
281 } else {
282 /* Success */
283 ret = 0;
284 *stream_id = reply.handle;
285 }
286
287 DBG("Relayd stream added successfully with handle %" PRIu64,
288 reply.handle);
289
290 error:
291 return ret;
292 }
293
294 /*
295 * Check version numbers on the relayd.
296 * If major versions are compatible, we assign minor_to_use to the
297 * minor version of the procotol we are going to use for this session.
298 *
299 * Return 0 if compatible else negative value.
300 */
301 int relayd_version_check(struct lttcomm_relayd_sock *rsock)
302 {
303 int ret;
304 struct lttcomm_relayd_version msg;
305
306 /* Code flow error. Safety net. */
307 assert(rsock);
308
309 DBG("Relayd version check for major.minor %u.%u", rsock->major,
310 rsock->minor);
311
312 /* Prepare network byte order before transmission. */
313 msg.major = htobe32(rsock->major);
314 msg.minor = htobe32(rsock->minor);
315
316 /* Send command */
317 ret = send_command(rsock, RELAYD_VERSION, (void *) &msg, sizeof(msg), 0);
318 if (ret < 0) {
319 goto error;
320 }
321
322 /* Receive response */
323 ret = recv_reply(rsock, (void *) &msg, sizeof(msg));
324 if (ret < 0) {
325 goto error;
326 }
327
328 /* Set back to host bytes order */
329 msg.major = be32toh(msg.major);
330 msg.minor = be32toh(msg.minor);
331
332 /*
333 * Only validate the major version. If the other side is higher,
334 * communication is not possible. Only major version equal can talk to each
335 * other. If the minor version differs, the lowest version is used by both
336 * sides.
337 */
338 if (msg.major != rsock->major) {
339 /* Not compatible */
340 ret = -1;
341 DBG2("Relayd version is NOT compatible. Relayd version %u != %u (us)",
342 msg.major, rsock->major);
343 goto error;
344 }
345
346 /*
347 * If the relayd's minor version is higher, it will adapt to our version so
348 * we can continue to use the latest relayd communication data structure.
349 * If the received minor version is higher, the relayd should adapt to us.
350 */
351 if (rsock->minor > msg.minor) {
352 rsock->minor = msg.minor;
353 }
354
355 /* Version number compatible */
356 DBG2("Relayd version is compatible, using protocol version %u.%u",
357 rsock->major, rsock->minor);
358 ret = 0;
359
360 error:
361 return ret;
362 }
363
364 /*
365 * Add stream on the relayd and assign stream handle to the stream_id argument.
366 *
367 * On success return 0 else return ret_code negative value.
368 */
369 int relayd_send_metadata(struct lttcomm_relayd_sock *rsock, size_t len)
370 {
371 int ret;
372
373 /* Code flow error. Safety net. */
374 assert(rsock);
375
376 DBG("Relayd sending metadata of size %zu", len);
377
378 /* Send command */
379 ret = send_command(rsock, RELAYD_SEND_METADATA, NULL, len, 0);
380 if (ret < 0) {
381 goto error;
382 }
383
384 DBG2("Relayd metadata added successfully");
385
386 /*
387 * After that call, the metadata data MUST be sent to the relayd so the
388 * receive size on the other end matches the len of the metadata packet
389 * header. This is why we don't wait for a reply here.
390 */
391
392 error:
393 return ret;
394 }
395
396 /*
397 * Connect to relay daemon with an allocated lttcomm_relayd_sock.
398 */
399 int relayd_connect(struct lttcomm_relayd_sock *rsock)
400 {
401 /* Code flow error. Safety net. */
402 assert(rsock);
403
404 if (!rsock->sock.ops) {
405 /*
406 * Attempting a connect on a non-initialized socket.
407 */
408 return -ECONNRESET;
409 }
410
411 DBG3("Relayd connect ...");
412
413 return rsock->sock.ops->connect(&rsock->sock);
414 }
415
416 /*
417 * Close relayd socket with an allocated lttcomm_relayd_sock.
418 *
419 * If no socket operations are found, simply return 0 meaning that everything
420 * is fine. Without operations, the socket can not possibly be opened or used.
421 * This is possible if the socket was allocated but not created. However, the
422 * caller could simply use it to store a valid file descriptor for instance
423 * passed over a Unix socket and call this to cleanup but still without a valid
424 * ops pointer.
425 *
426 * Return the close returned value. On error, a negative value is usually
427 * returned back from close(2).
428 */
429 int relayd_close(struct lttcomm_relayd_sock *rsock)
430 {
431 int ret;
432
433 /* Code flow error. Safety net. */
434 assert(rsock);
435
436 /* An invalid fd is fine, return success. */
437 if (rsock->sock.fd < 0) {
438 ret = 0;
439 goto end;
440 }
441
442 DBG3("Relayd closing socket %d", rsock->sock.fd);
443
444 if (rsock->sock.ops) {
445 ret = rsock->sock.ops->close(&rsock->sock);
446 } else {
447 /* Default call if no specific ops found. */
448 ret = close(rsock->sock.fd);
449 if (ret < 0) {
450 PERROR("relayd_close default close");
451 }
452 }
453 rsock->sock.fd = -1;
454
455 end:
456 return ret;
457 }
458
459 /*
460 * Send data header structure to the relayd.
461 */
462 int relayd_send_data_hdr(struct lttcomm_relayd_sock *rsock,
463 struct lttcomm_relayd_data_hdr *hdr, size_t size)
464 {
465 int ret;
466
467 /* Code flow error. Safety net. */
468 assert(rsock);
469 assert(hdr);
470
471 if (rsock->sock.fd < 0) {
472 return -ECONNRESET;
473 }
474
475 DBG3("Relayd sending data header of size %zu", size);
476
477 /* Again, safety net */
478 if (size == 0) {
479 size = sizeof(struct lttcomm_relayd_data_hdr);
480 }
481
482 /* Only send data header. */
483 ret = rsock->sock.ops->sendmsg(&rsock->sock, hdr, size, 0);
484 if (ret < 0) {
485 ret = -errno;
486 goto error;
487 }
488
489 /*
490 * The data MUST be sent right after that command for the receive on the
491 * other end to match the size in the header.
492 */
493
494 error:
495 return ret;
496 }
497
498 /*
499 * Send close stream command to the relayd.
500 */
501 int relayd_send_close_stream(struct lttcomm_relayd_sock *rsock, uint64_t stream_id,
502 uint64_t last_net_seq_num)
503 {
504 int ret;
505 struct lttcomm_relayd_close_stream msg;
506 struct lttcomm_relayd_generic_reply reply;
507
508 /* Code flow error. Safety net. */
509 assert(rsock);
510
511 DBG("Relayd closing stream id %" PRIu64, stream_id);
512
513 msg.stream_id = htobe64(stream_id);
514 msg.last_net_seq_num = htobe64(last_net_seq_num);
515
516 /* Send command */
517 ret = send_command(rsock, RELAYD_CLOSE_STREAM, (void *) &msg, sizeof(msg), 0);
518 if (ret < 0) {
519 goto error;
520 }
521
522 /* Receive response */
523 ret = recv_reply(rsock, (void *) &reply, sizeof(reply));
524 if (ret < 0) {
525 goto error;
526 }
527
528 reply.ret_code = be32toh(reply.ret_code);
529
530 /* Return session id or negative ret code. */
531 if (reply.ret_code != LTTNG_OK) {
532 ret = -1;
533 ERR("Relayd close stream replied error %d", reply.ret_code);
534 } else {
535 /* Success */
536 ret = 0;
537 }
538
539 DBG("Relayd close stream id %" PRIu64 " successfully", stream_id);
540
541 error:
542 return ret;
543 }
544
545 /*
546 * Check for data availability for a given stream id.
547 *
548 * Return 0 if NOT pending, 1 if so and a negative value on error.
549 */
550 int relayd_data_pending(struct lttcomm_relayd_sock *rsock, uint64_t stream_id,
551 uint64_t last_net_seq_num)
552 {
553 int ret;
554 struct lttcomm_relayd_data_pending msg;
555 struct lttcomm_relayd_generic_reply reply;
556
557 /* Code flow error. Safety net. */
558 assert(rsock);
559
560 DBG("Relayd data pending for stream id %" PRIu64, stream_id);
561
562 msg.stream_id = htobe64(stream_id);
563 msg.last_net_seq_num = htobe64(last_net_seq_num);
564
565 /* Send command */
566 ret = send_command(rsock, RELAYD_DATA_PENDING, (void *) &msg,
567 sizeof(msg), 0);
568 if (ret < 0) {
569 goto error;
570 }
571
572 /* Receive response */
573 ret = recv_reply(rsock, (void *) &reply, sizeof(reply));
574 if (ret < 0) {
575 goto error;
576 }
577
578 reply.ret_code = be32toh(reply.ret_code);
579
580 /* Return session id or negative ret code. */
581 if (reply.ret_code >= LTTNG_OK) {
582 ERR("Relayd data pending replied error %d", reply.ret_code);
583 }
584
585 /* At this point, the ret code is either 1 or 0 */
586 ret = reply.ret_code;
587
588 DBG("Relayd data is %s pending for stream id %" PRIu64,
589 ret == 1 ? "" : "NOT", stream_id);
590
591 error:
592 return ret;
593 }
594
595 /*
596 * Check on the relayd side for a quiescent state on the control socket.
597 */
598 int relayd_quiescent_control(struct lttcomm_relayd_sock *rsock,
599 uint64_t metadata_stream_id)
600 {
601 int ret;
602 struct lttcomm_relayd_quiescent_control msg;
603 struct lttcomm_relayd_generic_reply reply;
604
605 /* Code flow error. Safety net. */
606 assert(rsock);
607
608 DBG("Relayd checking quiescent control state");
609
610 msg.stream_id = htobe64(metadata_stream_id);
611
612 /* Send command */
613 ret = send_command(rsock, RELAYD_QUIESCENT_CONTROL, &msg, sizeof(msg), 0);
614 if (ret < 0) {
615 goto error;
616 }
617
618 /* Receive response */
619 ret = recv_reply(rsock, (void *) &reply, sizeof(reply));
620 if (ret < 0) {
621 goto error;
622 }
623
624 reply.ret_code = be32toh(reply.ret_code);
625
626 /* Return session id or negative ret code. */
627 if (reply.ret_code != LTTNG_OK) {
628 ret = -1;
629 ERR("Relayd quiescent control replied error %d", reply.ret_code);
630 goto error;
631 }
632
633 /* Control socket is quiescent */
634 return 0;
635
636 error:
637 return ret;
638 }
639
640 /*
641 * Begin a data pending command for a specific session id.
642 */
643 int relayd_begin_data_pending(struct lttcomm_relayd_sock *rsock, uint64_t id)
644 {
645 int ret;
646 struct lttcomm_relayd_begin_data_pending msg;
647 struct lttcomm_relayd_generic_reply reply;
648
649 /* Code flow error. Safety net. */
650 assert(rsock);
651
652 DBG("Relayd begin data pending");
653
654 msg.session_id = htobe64(id);
655
656 /* Send command */
657 ret = send_command(rsock, RELAYD_BEGIN_DATA_PENDING, &msg, sizeof(msg), 0);
658 if (ret < 0) {
659 goto error;
660 }
661
662 /* Receive response */
663 ret = recv_reply(rsock, (void *) &reply, sizeof(reply));
664 if (ret < 0) {
665 goto error;
666 }
667
668 reply.ret_code = be32toh(reply.ret_code);
669
670 /* Return session id or negative ret code. */
671 if (reply.ret_code != LTTNG_OK) {
672 ret = -1;
673 ERR("Relayd begin data pending replied error %d", reply.ret_code);
674 goto error;
675 }
676
677 return 0;
678
679 error:
680 return ret;
681 }
682
683 /*
684 * End a data pending command for a specific session id.
685 *
686 * Return 0 on success and set is_data_inflight to 0 if no data is being
687 * streamed or 1 if it is the case.
688 */
689 int relayd_end_data_pending(struct lttcomm_relayd_sock *rsock, uint64_t id,
690 unsigned int *is_data_inflight)
691 {
692 int ret;
693 struct lttcomm_relayd_end_data_pending msg;
694 struct lttcomm_relayd_generic_reply reply;
695
696 /* Code flow error. Safety net. */
697 assert(rsock);
698
699 DBG("Relayd end data pending");
700
701 msg.session_id = htobe64(id);
702
703 /* Send command */
704 ret = send_command(rsock, RELAYD_END_DATA_PENDING, &msg, sizeof(msg), 0);
705 if (ret < 0) {
706 goto error;
707 }
708
709 /* Receive response */
710 ret = recv_reply(rsock, (void *) &reply, sizeof(reply));
711 if (ret < 0) {
712 goto error;
713 }
714
715 reply.ret_code = be32toh(reply.ret_code);
716 if (reply.ret_code < 0) {
717 ret = reply.ret_code;
718 goto error;
719 }
720
721 *is_data_inflight = reply.ret_code;
722
723 DBG("Relayd end data pending is data inflight: %d", reply.ret_code);
724
725 return 0;
726
727 error:
728 return ret;
729 }
730
731 /*
732 * Send index to the relayd.
733 */
734 int relayd_send_index(struct lttcomm_relayd_sock *rsock,
735 struct lttng_packet_index *index, uint64_t relay_stream_id,
736 uint64_t net_seq_num)
737 {
738 int ret;
739 struct lttcomm_relayd_index msg;
740 struct lttcomm_relayd_generic_reply reply;
741
742 /* Code flow error. Safety net. */
743 assert(rsock);
744
745 if (rsock->minor < 4) {
746 DBG("Not sending indexes before protocol 2.4");
747 ret = 0;
748 goto error;
749 }
750
751 DBG("Relayd sending index for stream ID %" PRIu64, relay_stream_id);
752
753 msg.relay_stream_id = htobe64(relay_stream_id);
754 msg.net_seq_num = htobe64(net_seq_num);
755
756 /* The index is already in big endian. */
757 msg.packet_size = index->packet_size;
758 msg.content_size = index->content_size;
759 msg.timestamp_begin = index->timestamp_begin;
760 msg.timestamp_end = index->timestamp_end;
761 msg.events_discarded = index->events_discarded;
762 msg.stream_id = index->stream_id;
763
764 /* Send command */
765 ret = send_command(rsock, RELAYD_SEND_INDEX, &msg, sizeof(msg), 0);
766 if (ret < 0) {
767 goto error;
768 }
769
770 /* Receive response */
771 ret = recv_reply(rsock, (void *) &reply, sizeof(reply));
772 if (ret < 0) {
773 goto error;
774 }
775
776 reply.ret_code = be32toh(reply.ret_code);
777
778 /* Return session id or negative ret code. */
779 if (reply.ret_code != LTTNG_OK) {
780 ret = -1;
781 ERR("Relayd send index replied error %d", reply.ret_code);
782 } else {
783 /* Success */
784 ret = 0;
785 }
786
787 error:
788 return ret;
789 }
This page took 0.046622 seconds and 6 git commands to generate.