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