| 1 | /* |
| 2 | * Copyright (C) 2012 David Goulet <dgoulet@efficios.com> |
| 3 | * |
| 4 | * SPDX-License-Identifier: GPL-2.0-only |
| 5 | * |
| 6 | */ |
| 7 | |
| 8 | #ifndef _CONSUMER_H |
| 9 | #define _CONSUMER_H |
| 10 | |
| 11 | #include <common/consumer/consumer.h> |
| 12 | #include <common/hashtable/hashtable.h> |
| 13 | #include <lttng/lttng.h> |
| 14 | #include <urcu/ref.h> |
| 15 | #include <algorithm> |
| 16 | |
| 17 | #include "snapshot.h" |
| 18 | |
| 19 | struct snapshot; |
| 20 | struct snapshot_output; |
| 21 | struct ltt_session; |
| 22 | |
| 23 | /* |
| 24 | * Needed until we use C++14, where std::max is constexpr. |
| 25 | * |
| 26 | * Use a static_assert so we remember to remove it when we upgrade to a newer |
| 27 | * C++. |
| 28 | */ |
| 29 | static_assert(__cplusplus == 201103L, ""); |
| 30 | template <typename T> |
| 31 | constexpr T max_constexpr(T l, T r) |
| 32 | { |
| 33 | return l > r ? l : r; |
| 34 | } |
| 35 | |
| 36 | enum consumer_dst_type { |
| 37 | CONSUMER_DST_LOCAL, |
| 38 | CONSUMER_DST_NET, |
| 39 | }; |
| 40 | |
| 41 | enum consumer_trace_chunk_exists_status { |
| 42 | CONSUMER_TRACE_CHUNK_EXISTS_STATUS_EXISTS_LOCAL, |
| 43 | CONSUMER_TRACE_CHUNK_EXISTS_STATUS_EXISTS_REMOTE, |
| 44 | CONSUMER_TRACE_CHUNK_EXISTS_STATUS_UNKNOWN_CHUNK, |
| 45 | }; |
| 46 | |
| 47 | struct consumer_socket { |
| 48 | /* |
| 49 | * File descriptor. This is just a reference to the consumer data meaning |
| 50 | * that every access must be locked and checked for a possible invalid |
| 51 | * value. |
| 52 | */ |
| 53 | int *fd_ptr; |
| 54 | |
| 55 | /* |
| 56 | * To use this socket (send/recv), this lock MUST be acquired. |
| 57 | */ |
| 58 | pthread_mutex_t *lock; |
| 59 | |
| 60 | /* |
| 61 | * Indicates if the socket was registered by a third part |
| 62 | * (REGISTER_CONSUMER) or is the spawn consumer of the session daemon. |
| 63 | * During the destroy phase of a consumer output, we close the socket if |
| 64 | * this flag is set to 1 since we don't need the fd anymore. |
| 65 | */ |
| 66 | unsigned int registered; |
| 67 | |
| 68 | /* Flag if network sockets were sent to the consumer. */ |
| 69 | unsigned int control_sock_sent; |
| 70 | unsigned int data_sock_sent; |
| 71 | |
| 72 | struct lttng_ht_node_ulong node; |
| 73 | |
| 74 | enum lttng_consumer_type type; |
| 75 | }; |
| 76 | |
| 77 | struct consumer_data { |
| 78 | consumer_data (lttng_consumer_type type_) |
| 79 | : type(type_) |
| 80 | {} |
| 81 | |
| 82 | enum lttng_consumer_type type; |
| 83 | |
| 84 | /* Mutex to control consumerd pid assignation */ |
| 85 | pthread_mutex_t pid_mutex = PTHREAD_MUTEX_INITIALIZER; |
| 86 | pid_t pid = 0; |
| 87 | |
| 88 | int err_sock = -1; |
| 89 | /* These two sockets uses the cmd_unix_sock_path. */ |
| 90 | int cmd_sock = -1; |
| 91 | /* |
| 92 | * Write-end of the channel monitoring pipe to be passed to the |
| 93 | * consumer. |
| 94 | */ |
| 95 | int channel_monitor_pipe = -1; |
| 96 | /* |
| 97 | * The metadata socket object is handled differently and only created |
| 98 | * locally in this object thus it's the only reference available in the |
| 99 | * session daemon. For that reason, a variable for the fd is required and |
| 100 | * the metadata socket fd points to it. |
| 101 | */ |
| 102 | int metadata_fd = 0; |
| 103 | struct consumer_socket metadata_sock {}; |
| 104 | |
| 105 | /* consumer error and command Unix socket path */ |
| 106 | const char *err_unix_sock_path = nullptr; |
| 107 | const char *cmd_unix_sock_path = nullptr; |
| 108 | |
| 109 | /* |
| 110 | * This lock has two purposes. It protects any change to the consumer |
| 111 | * socket and make sure only one thread uses this object for read/write |
| 112 | * operations. |
| 113 | */ |
| 114 | pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER; |
| 115 | }; |
| 116 | |
| 117 | /* |
| 118 | * Network URIs |
| 119 | */ |
| 120 | struct consumer_net { |
| 121 | /* |
| 122 | * Indicate if URI type is set. Those flags should only be set when the |
| 123 | * created URI is done AND valid. |
| 124 | */ |
| 125 | int control_isset; |
| 126 | int data_isset; |
| 127 | |
| 128 | /* |
| 129 | * The following two URIs MUST have the same destination address for |
| 130 | * network streaming to work. Network hop are not yet supported. |
| 131 | */ |
| 132 | |
| 133 | /* Control path for network streaming. */ |
| 134 | struct lttng_uri control; |
| 135 | |
| 136 | /* Data path for network streaming. */ |
| 137 | struct lttng_uri data; |
| 138 | |
| 139 | /* <hostname>/<session-name> */ |
| 140 | char base_dir[PATH_MAX]; |
| 141 | }; |
| 142 | |
| 143 | /* |
| 144 | * Consumer output object describing where and how to send data. |
| 145 | */ |
| 146 | struct consumer_output { |
| 147 | struct urcu_ref ref; /* Refcount */ |
| 148 | |
| 149 | /* If the consumer is enabled meaning that should be used */ |
| 150 | unsigned int enabled; |
| 151 | enum consumer_dst_type type; |
| 152 | |
| 153 | /* |
| 154 | * The net_seq_index is the index of the network stream on the consumer |
| 155 | * side. It tells the consumer which streams goes to which relayd with this |
| 156 | * index. The relayd sockets are index with it on the consumer side. |
| 157 | */ |
| 158 | uint64_t net_seq_index; |
| 159 | /* Store the relay protocol in use if the session is remote. */ |
| 160 | uint32_t relay_major_version; |
| 161 | uint32_t relay_minor_version; |
| 162 | |
| 163 | /* True if relayd supports the clear feature. */ |
| 164 | bool relay_allows_clear; |
| 165 | |
| 166 | /* |
| 167 | * Subdirectory path name used for both local and network |
| 168 | * consumer ("kernel", "ust", or empty). |
| 169 | */ |
| 170 | char domain_subdir[ |
| 171 | max_constexpr(sizeof(DEFAULT_KERNEL_TRACE_DIR), |
| 172 | sizeof(DEFAULT_UST_TRACE_DIR))]; |
| 173 | |
| 174 | /* |
| 175 | * Hashtable of consumer_socket index by the file descriptor value. For |
| 176 | * multiarch consumer support, we can have more than one consumer (ex: |
| 177 | * 32 and 64 bit). |
| 178 | */ |
| 179 | struct lttng_ht *socks; |
| 180 | |
| 181 | /* Tell if this output is used for snapshot. */ |
| 182 | unsigned int snapshot:1; |
| 183 | |
| 184 | union { |
| 185 | char session_root_path[LTTNG_PATH_MAX]; |
| 186 | struct consumer_net net; |
| 187 | } dst; |
| 188 | |
| 189 | /* |
| 190 | * Sub-directory below the session_root_path where the next chunk of |
| 191 | * trace will be stored (\0 before the first session rotation). |
| 192 | */ |
| 193 | char chunk_path[LTTNG_PATH_MAX]; |
| 194 | }; |
| 195 | |
| 196 | struct consumer_socket *consumer_find_socket(int key, |
| 197 | const struct consumer_output *consumer); |
| 198 | struct consumer_socket *consumer_find_socket_by_bitness(int bits, |
| 199 | const struct consumer_output *consumer); |
| 200 | struct consumer_socket *consumer_allocate_socket(int *fd); |
| 201 | void consumer_add_socket(struct consumer_socket *sock, |
| 202 | struct consumer_output *consumer); |
| 203 | void consumer_del_socket(struct consumer_socket *sock, |
| 204 | struct consumer_output *consumer); |
| 205 | void consumer_destroy_socket(struct consumer_socket *sock); |
| 206 | int consumer_copy_sockets(struct consumer_output *dst, |
| 207 | struct consumer_output *src); |
| 208 | void consumer_destroy_output_sockets(struct consumer_output *obj); |
| 209 | int consumer_socket_send(struct consumer_socket *socket, const void *msg, |
| 210 | size_t len); |
| 211 | int consumer_socket_recv(struct consumer_socket *socket, void *msg, |
| 212 | size_t len); |
| 213 | |
| 214 | struct consumer_output *consumer_create_output(enum consumer_dst_type type); |
| 215 | struct consumer_output *consumer_copy_output(struct consumer_output *obj); |
| 216 | void consumer_output_get(struct consumer_output *obj); |
| 217 | void consumer_output_put(struct consumer_output *obj); |
| 218 | int consumer_set_network_uri(const struct ltt_session *session, |
| 219 | struct consumer_output *obj, |
| 220 | struct lttng_uri *uri); |
| 221 | int consumer_send_fds(struct consumer_socket *sock, const int *fds, |
| 222 | size_t nb_fd); |
| 223 | int consumer_send_msg(struct consumer_socket *sock, |
| 224 | const struct lttcomm_consumer_msg *msg); |
| 225 | int consumer_send_stream(struct consumer_socket *sock, |
| 226 | struct consumer_output *dst, struct lttcomm_consumer_msg *msg, |
| 227 | const int *fds, size_t nb_fd); |
| 228 | int consumer_send_channel(struct consumer_socket *sock, |
| 229 | struct lttcomm_consumer_msg *msg); |
| 230 | int consumer_send_relayd_socket(struct consumer_socket *consumer_sock, |
| 231 | struct lttcomm_relayd_sock *rsock, struct consumer_output *consumer, |
| 232 | enum lttng_stream_type type, uint64_t session_id, |
| 233 | const char *session_name, const char *hostname, |
| 234 | const char *base_path, int session_live_timer, |
| 235 | const uint64_t *current_chunk_id, time_t session_creation_time, |
| 236 | bool session_name_contains_creation_time); |
| 237 | int consumer_send_channel_monitor_pipe(struct consumer_socket *consumer_sock, |
| 238 | int pipe); |
| 239 | int consumer_send_destroy_relayd(struct consumer_socket *sock, |
| 240 | struct consumer_output *consumer); |
| 241 | int consumer_recv_status_reply(struct consumer_socket *sock); |
| 242 | int consumer_recv_status_channel(struct consumer_socket *sock, |
| 243 | uint64_t *key, unsigned int *stream_count); |
| 244 | void consumer_output_send_destroy_relayd(struct consumer_output *consumer); |
| 245 | int consumer_create_socket(struct consumer_data *data, |
| 246 | struct consumer_output *output); |
| 247 | |
| 248 | void consumer_init_ask_channel_comm_msg(struct lttcomm_consumer_msg *msg, |
| 249 | uint64_t subbuf_size, |
| 250 | uint64_t num_subbuf, |
| 251 | int overwrite, |
| 252 | unsigned int switch_timer_interval, |
| 253 | unsigned int read_timer_interval, |
| 254 | unsigned int live_timer_interval, |
| 255 | bool is_in_live_session, |
| 256 | unsigned int monitor_timer_interval, |
| 257 | int output, |
| 258 | int type, |
| 259 | uint64_t session_id, |
| 260 | const char *pathname, |
| 261 | const char *name, |
| 262 | uint64_t relayd_id, |
| 263 | uint64_t key, |
| 264 | unsigned char *uuid, |
| 265 | uint32_t chan_id, |
| 266 | uint64_t tracefile_size, |
| 267 | uint64_t tracefile_count, |
| 268 | uint64_t session_id_per_pid, |
| 269 | unsigned int monitor, |
| 270 | uint32_t ust_app_uid, |
| 271 | int64_t blocking_timeout, |
| 272 | const char *root_shm_path, |
| 273 | const char *shm_path, |
| 274 | struct lttng_trace_chunk *trace_chunk, |
| 275 | const struct lttng_credentials *buffer_credentials); |
| 276 | void consumer_init_add_stream_comm_msg(struct lttcomm_consumer_msg *msg, |
| 277 | uint64_t channel_key, |
| 278 | uint64_t stream_key, |
| 279 | int32_t cpu); |
| 280 | void consumer_init_streams_sent_comm_msg(struct lttcomm_consumer_msg *msg, |
| 281 | enum lttng_consumer_command cmd, |
| 282 | uint64_t channel_key, uint64_t net_seq_idx); |
| 283 | void consumer_init_add_channel_comm_msg(struct lttcomm_consumer_msg *msg, |
| 284 | uint64_t channel_key, |
| 285 | uint64_t session_id, |
| 286 | const char *pathname, |
| 287 | uid_t uid, |
| 288 | gid_t gid, |
| 289 | uint64_t relayd_id, |
| 290 | const char *name, |
| 291 | unsigned int nb_init_streams, |
| 292 | enum lttng_event_output output, |
| 293 | int type, |
| 294 | uint64_t tracefile_size, |
| 295 | uint64_t tracefile_count, |
| 296 | unsigned int monitor, |
| 297 | unsigned int live_timer_interval, |
| 298 | bool is_in_live_session, |
| 299 | unsigned int monitor_timer_interval, |
| 300 | struct lttng_trace_chunk *trace_chunk); |
| 301 | int consumer_is_data_pending(uint64_t session_id, |
| 302 | struct consumer_output *consumer); |
| 303 | int consumer_close_metadata(struct consumer_socket *socket, |
| 304 | uint64_t metadata_key); |
| 305 | int consumer_setup_metadata(struct consumer_socket *socket, |
| 306 | uint64_t metadata_key); |
| 307 | int consumer_push_metadata(struct consumer_socket *socket, |
| 308 | uint64_t metadata_key, char *metadata_str, size_t len, |
| 309 | size_t target_offset, uint64_t version); |
| 310 | int consumer_flush_channel(struct consumer_socket *socket, uint64_t key); |
| 311 | int consumer_clear_quiescent_channel(struct consumer_socket *socket, uint64_t key); |
| 312 | int consumer_get_discarded_events(uint64_t session_id, uint64_t channel_key, |
| 313 | struct consumer_output *consumer, uint64_t *discarded); |
| 314 | int consumer_get_lost_packets(uint64_t session_id, uint64_t channel_key, |
| 315 | struct consumer_output *consumer, uint64_t *lost); |
| 316 | |
| 317 | /* Snapshot command. */ |
| 318 | enum lttng_error_code consumer_snapshot_channel(struct consumer_socket *socket, |
| 319 | uint64_t key, const struct consumer_output *output, int metadata, |
| 320 | uid_t uid, gid_t gid, const char *channel_path, int wait, |
| 321 | uint64_t nb_packets_per_stream); |
| 322 | |
| 323 | /* Rotation commands. */ |
| 324 | int consumer_rotate_channel(struct consumer_socket *socket, uint64_t key, |
| 325 | uid_t uid, gid_t gid, struct consumer_output *output, |
| 326 | bool is_metadata_channel); |
| 327 | int consumer_init(struct consumer_socket *socket, |
| 328 | const lttng_uuid sessiond_uuid); |
| 329 | |
| 330 | int consumer_create_trace_chunk(struct consumer_socket *socket, |
| 331 | uint64_t relayd_id, uint64_t session_id, |
| 332 | struct lttng_trace_chunk *chunk, |
| 333 | const char *domain_subdir); |
| 334 | int consumer_close_trace_chunk(struct consumer_socket *socket, |
| 335 | uint64_t relayd_id, uint64_t session_id, |
| 336 | struct lttng_trace_chunk *chunk, |
| 337 | char *closed_trace_chunk_path); |
| 338 | int consumer_trace_chunk_exists(struct consumer_socket *socket, |
| 339 | uint64_t relayd_id, uint64_t session_id, |
| 340 | struct lttng_trace_chunk *chunk, |
| 341 | enum consumer_trace_chunk_exists_status *result); |
| 342 | int consumer_open_channel_packets(struct consumer_socket *socket, uint64_t key); |
| 343 | |
| 344 | char *setup_channel_trace_path(struct consumer_output *consumer, |
| 345 | const char *session_path, size_t *consumer_path_offset); |
| 346 | |
| 347 | /* Clear command */ |
| 348 | int consumer_clear_channel(struct consumer_socket *socket, uint64_t key); |
| 349 | |
| 350 | #endif /* _CONSUMER_H */ |