| 1 | /* |
| 2 | * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca> |
| 3 | * Julien Desfossez <julien.desfossez@polymtl.ca> |
| 4 | * Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License, version 2 only, |
| 8 | * as published by the Free Software Foundation. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, but WITHOUT |
| 11 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 12 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 13 | * more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License along |
| 16 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | */ |
| 19 | |
| 20 | /* |
| 21 | * This header is meant for liblttng and libust internal use ONLY. These |
| 22 | * declarations should NOT be considered stable API. |
| 23 | */ |
| 24 | |
| 25 | #ifndef _LTTNG_SESSIOND_COMM_H |
| 26 | #define _LTTNG_SESSIOND_COMM_H |
| 27 | |
| 28 | #define _GNU_SOURCE |
| 29 | #include <limits.h> |
| 30 | #include <lttng/lttng.h> |
| 31 | #include <common/compat/socket.h> |
| 32 | #include <common/uri.h> |
| 33 | #include <common/defaults.h> |
| 34 | #include <common/compat/uuid.h> |
| 35 | |
| 36 | #include <arpa/inet.h> |
| 37 | #include <netinet/in.h> |
| 38 | #include <sys/un.h> |
| 39 | |
| 40 | #include "inet.h" |
| 41 | #include "inet6.h" |
| 42 | #include "unix.h" |
| 43 | |
| 44 | /* Queue size of listen(2) */ |
| 45 | #define LTTNG_SESSIOND_COMM_MAX_LISTEN 64 |
| 46 | |
| 47 | /* Maximum number of FDs that can be sent over a Unix socket */ |
| 48 | #define LTTCOMM_MAX_SEND_FDS 4 |
| 49 | |
| 50 | /* |
| 51 | * Get the error code index from 0 since LTTCOMM_OK start at 1000 |
| 52 | */ |
| 53 | #define LTTCOMM_ERR_INDEX(code) (code - LTTCOMM_CONSUMERD_COMMAND_SOCK_READY) |
| 54 | |
| 55 | enum lttcomm_sessiond_command { |
| 56 | /* Tracer command */ |
| 57 | LTTNG_ADD_CONTEXT = 0, |
| 58 | LTTNG_CALIBRATE = 1, |
| 59 | LTTNG_DISABLE_CHANNEL = 2, |
| 60 | LTTNG_DISABLE_EVENT = 3, |
| 61 | LTTNG_DISABLE_ALL_EVENT = 4, |
| 62 | LTTNG_ENABLE_CHANNEL = 5, |
| 63 | LTTNG_ENABLE_EVENT = 6, |
| 64 | LTTNG_ENABLE_ALL_EVENT = 7, |
| 65 | /* Session daemon command */ |
| 66 | LTTNG_CREATE_SESSION = 8, |
| 67 | LTTNG_DESTROY_SESSION = 9, |
| 68 | LTTNG_LIST_CHANNELS = 10, |
| 69 | LTTNG_LIST_DOMAINS = 11, |
| 70 | LTTNG_LIST_EVENTS = 12, |
| 71 | LTTNG_LIST_SESSIONS = 13, |
| 72 | LTTNG_LIST_TRACEPOINTS = 14, |
| 73 | LTTNG_REGISTER_CONSUMER = 15, |
| 74 | LTTNG_START_TRACE = 16, |
| 75 | LTTNG_STOP_TRACE = 17, |
| 76 | LTTNG_LIST_TRACEPOINT_FIELDS = 18, |
| 77 | |
| 78 | /* Consumer */ |
| 79 | LTTNG_DISABLE_CONSUMER = 19, |
| 80 | LTTNG_ENABLE_CONSUMER = 20, |
| 81 | LTTNG_SET_CONSUMER_URI = 21, |
| 82 | LTTNG_ENABLE_EVENT_WITH_FILTER = 22, |
| 83 | LTTNG_HEALTH_CHECK = 23, |
| 84 | LTTNG_DATA_PENDING = 24, |
| 85 | }; |
| 86 | |
| 87 | enum lttcomm_relayd_command { |
| 88 | RELAYD_ADD_STREAM = 1, |
| 89 | RELAYD_CREATE_SESSION = 2, |
| 90 | RELAYD_START_DATA = 3, |
| 91 | RELAYD_UPDATE_SYNC_INFO = 4, |
| 92 | RELAYD_VERSION = 5, |
| 93 | RELAYD_SEND_METADATA = 6, |
| 94 | RELAYD_CLOSE_STREAM = 7, |
| 95 | RELAYD_DATA_PENDING = 8, |
| 96 | RELAYD_QUIESCENT_CONTROL = 9, |
| 97 | RELAYD_BEGIN_DATA_PENDING = 10, |
| 98 | RELAYD_END_DATA_PENDING = 11, |
| 99 | }; |
| 100 | |
| 101 | /* |
| 102 | * lttcomm error code. |
| 103 | */ |
| 104 | enum lttcomm_return_code { |
| 105 | LTTCOMM_CONSUMERD_COMMAND_SOCK_READY = 1, /* Command socket ready */ |
| 106 | LTTCOMM_CONSUMERD_SUCCESS_RECV_FD, /* Success on receiving fds */ |
| 107 | LTTCOMM_CONSUMERD_ERROR_RECV_FD, /* Error on receiving fds */ |
| 108 | LTTCOMM_CONSUMERD_ERROR_RECV_CMD, /* Error on receiving command */ |
| 109 | LTTCOMM_CONSUMERD_POLL_ERROR, /* Error in polling thread */ |
| 110 | LTTCOMM_CONSUMERD_POLL_NVAL, /* Poll on closed fd */ |
| 111 | LTTCOMM_CONSUMERD_POLL_HUP, /* All fds have hungup */ |
| 112 | LTTCOMM_CONSUMERD_EXIT_SUCCESS, /* Consumerd exiting normally */ |
| 113 | LTTCOMM_CONSUMERD_EXIT_FAILURE, /* Consumerd exiting on error */ |
| 114 | LTTCOMM_CONSUMERD_OUTFD_ERROR, /* Error opening the tracefile */ |
| 115 | LTTCOMM_CONSUMERD_SPLICE_EBADF, /* EBADF from splice(2) */ |
| 116 | LTTCOMM_CONSUMERD_SPLICE_EINVAL, /* EINVAL from splice(2) */ |
| 117 | LTTCOMM_CONSUMERD_SPLICE_ENOMEM, /* ENOMEM from splice(2) */ |
| 118 | LTTCOMM_CONSUMERD_SPLICE_ESPIPE, /* ESPIPE from splice(2) */ |
| 119 | LTTCOMM_CONSUMERD_ENOMEM, /* Consumer is out of memory */ |
| 120 | |
| 121 | /* MUST be last element */ |
| 122 | LTTCOMM_NR, /* Last element */ |
| 123 | }; |
| 124 | |
| 125 | /* lttng socket protocol. */ |
| 126 | enum lttcomm_sock_proto { |
| 127 | LTTCOMM_SOCK_UDP, |
| 128 | LTTCOMM_SOCK_TCP, |
| 129 | }; |
| 130 | |
| 131 | /* |
| 132 | * Index in the net_families array below. Please keep in sync! |
| 133 | */ |
| 134 | enum lttcomm_sock_domain { |
| 135 | LTTCOMM_INET = 0, |
| 136 | LTTCOMM_INET6 = 1, |
| 137 | }; |
| 138 | |
| 139 | struct lttcomm_sockaddr { |
| 140 | enum lttcomm_sock_domain type; |
| 141 | union { |
| 142 | struct sockaddr_in sin; |
| 143 | struct sockaddr_in6 sin6; |
| 144 | } addr; |
| 145 | } LTTNG_PACKED; |
| 146 | |
| 147 | struct lttcomm_sock { |
| 148 | int fd; |
| 149 | enum lttcomm_sock_proto proto; |
| 150 | struct lttcomm_sockaddr sockaddr; |
| 151 | const struct lttcomm_proto_ops *ops; |
| 152 | } LTTNG_PACKED; |
| 153 | |
| 154 | struct lttcomm_net_family { |
| 155 | int family; |
| 156 | int (*create) (struct lttcomm_sock *sock, int type, int proto); |
| 157 | }; |
| 158 | |
| 159 | struct lttcomm_proto_ops { |
| 160 | int (*bind) (struct lttcomm_sock *sock); |
| 161 | int (*close) (struct lttcomm_sock *sock); |
| 162 | int (*connect) (struct lttcomm_sock *sock); |
| 163 | struct lttcomm_sock *(*accept) (struct lttcomm_sock *sock); |
| 164 | int (*listen) (struct lttcomm_sock *sock, int backlog); |
| 165 | ssize_t (*recvmsg) (struct lttcomm_sock *sock, void *buf, size_t len, |
| 166 | int flags); |
| 167 | ssize_t (*sendmsg) (struct lttcomm_sock *sock, void *buf, size_t len, |
| 168 | int flags); |
| 169 | }; |
| 170 | |
| 171 | /* |
| 172 | * Data structure received from lttng client to session daemon. |
| 173 | */ |
| 174 | struct lttcomm_session_msg { |
| 175 | uint32_t cmd_type; /* enum lttcomm_sessiond_command */ |
| 176 | struct lttng_session session; |
| 177 | struct lttng_domain domain; |
| 178 | union { |
| 179 | struct { |
| 180 | char channel_name[LTTNG_SYMBOL_NAME_LEN]; |
| 181 | char name[NAME_MAX]; |
| 182 | } LTTNG_PACKED disable; |
| 183 | /* Event data */ |
| 184 | struct { |
| 185 | char channel_name[LTTNG_SYMBOL_NAME_LEN]; |
| 186 | struct lttng_event event; |
| 187 | /* Length of following bytecode for filter. */ |
| 188 | uint32_t bytecode_len; |
| 189 | } LTTNG_PACKED enable; |
| 190 | /* Create channel */ |
| 191 | struct { |
| 192 | struct lttng_channel chan; |
| 193 | } LTTNG_PACKED channel; |
| 194 | /* Context */ |
| 195 | struct { |
| 196 | char channel_name[LTTNG_SYMBOL_NAME_LEN]; |
| 197 | struct lttng_event_context ctx; |
| 198 | } LTTNG_PACKED context; |
| 199 | /* Use by register_consumer */ |
| 200 | struct { |
| 201 | char path[PATH_MAX]; |
| 202 | } LTTNG_PACKED reg; |
| 203 | /* List */ |
| 204 | struct { |
| 205 | char channel_name[LTTNG_SYMBOL_NAME_LEN]; |
| 206 | } LTTNG_PACKED list; |
| 207 | struct lttng_calibrate calibrate; |
| 208 | /* Used by the set_consumer_url and used by create_session also call */ |
| 209 | struct { |
| 210 | /* Number of lttng_uri following */ |
| 211 | uint32_t size; |
| 212 | } LTTNG_PACKED uri; |
| 213 | } u; |
| 214 | } LTTNG_PACKED; |
| 215 | |
| 216 | #define LTTNG_FILTER_MAX_LEN 65536 |
| 217 | |
| 218 | /* |
| 219 | * Filter bytecode data. The reloc table is located at the end of the |
| 220 | * bytecode. It is made of tuples: (uint16_t, var. len. string). It |
| 221 | * starts at reloc_table_offset. |
| 222 | */ |
| 223 | #define LTTNG_FILTER_PADDING 32 |
| 224 | struct lttng_filter_bytecode { |
| 225 | uint32_t len; /* len of data */ |
| 226 | uint32_t reloc_table_offset; |
| 227 | uint64_t seqnum; |
| 228 | char padding[LTTNG_FILTER_PADDING]; |
| 229 | char data[0]; |
| 230 | } LTTNG_PACKED; |
| 231 | |
| 232 | /* |
| 233 | * Data structure for the response from sessiond to the lttng client. |
| 234 | */ |
| 235 | struct lttcomm_lttng_msg { |
| 236 | uint32_t cmd_type; /* enum lttcomm_sessiond_command */ |
| 237 | uint32_t ret_code; /* enum lttcomm_return_code */ |
| 238 | uint32_t pid; /* pid_t */ |
| 239 | uint32_t data_size; |
| 240 | /* Contains: trace_name + data */ |
| 241 | char payload[]; |
| 242 | } LTTNG_PACKED; |
| 243 | |
| 244 | struct lttcomm_health_msg { |
| 245 | uint32_t component; |
| 246 | uint32_t cmd; |
| 247 | } LTTNG_PACKED; |
| 248 | |
| 249 | struct lttcomm_health_data { |
| 250 | uint32_t ret_code; |
| 251 | } LTTNG_PACKED; |
| 252 | |
| 253 | /* |
| 254 | * lttcomm_consumer_msg is the message sent from sessiond to consumerd |
| 255 | * to either add a channel, add a stream, update a stream, or stop |
| 256 | * operation. |
| 257 | */ |
| 258 | struct lttcomm_consumer_msg { |
| 259 | uint32_t cmd_type; /* enum consumerd_command */ |
| 260 | union { |
| 261 | struct { |
| 262 | int channel_key; |
| 263 | uint64_t session_id; |
| 264 | char pathname[PATH_MAX]; |
| 265 | uid_t uid; |
| 266 | gid_t gid; |
| 267 | int relayd_id; |
| 268 | /* nb_init_streams is the number of streams open initially. */ |
| 269 | unsigned int nb_init_streams; |
| 270 | char name[LTTNG_SYMBOL_NAME_LEN]; |
| 271 | /* Use splice or mmap to consume this fd */ |
| 272 | enum lttng_event_output output; |
| 273 | int type; /* Per cpu or metadata. */ |
| 274 | } LTTNG_PACKED channel; /* Only used by Kernel. */ |
| 275 | struct { |
| 276 | int stream_key; |
| 277 | int channel_key; |
| 278 | int cpu; /* On which CPU this stream is assigned. */ |
| 279 | } LTTNG_PACKED stream; /* Only used by Kernel. */ |
| 280 | struct { |
| 281 | int net_index; |
| 282 | enum lttng_stream_type type; |
| 283 | /* Open socket to the relayd */ |
| 284 | struct lttcomm_sock sock; |
| 285 | /* Tracing session id associated to the relayd. */ |
| 286 | uint64_t session_id; |
| 287 | } LTTNG_PACKED relayd_sock; |
| 288 | struct { |
| 289 | uint64_t net_seq_idx; |
| 290 | } LTTNG_PACKED destroy_relayd; |
| 291 | struct { |
| 292 | uint64_t session_id; |
| 293 | } LTTNG_PACKED data_pending; |
| 294 | struct { |
| 295 | uint64_t subbuf_size; /* bytes */ |
| 296 | uint64_t num_subbuf; /* power of 2 */ |
| 297 | int overwrite; /* 1: overwrite, 0: discard */ |
| 298 | unsigned int switch_timer_interval; /* usec */ |
| 299 | unsigned int read_timer_interval; /* usec */ |
| 300 | int output; /* splice, mmap */ |
| 301 | int type; /* metadata or per_cpu */ |
| 302 | uint64_t session_id; /* Tracing session id */ |
| 303 | char pathname[PATH_MAX]; /* Channel file path. */ |
| 304 | char name[LTTNG_SYMBOL_NAME_LEN]; /* Channel name. */ |
| 305 | uid_t uid; /* User ID of the session */ |
| 306 | gid_t gid; /* Group ID ot the session */ |
| 307 | int relayd_id; /* Relayd id if apply. */ |
| 308 | unsigned long key; /* Unique channel key. */ |
| 309 | unsigned char uuid[UUID_STR_LEN]; /* uuid for ust tracer. */ |
| 310 | } LTTNG_PACKED ask_channel; |
| 311 | struct { |
| 312 | unsigned long key; |
| 313 | } LTTNG_PACKED get_channel; |
| 314 | struct { |
| 315 | unsigned long key; |
| 316 | } LTTNG_PACKED destroy_channel; |
| 317 | } u; |
| 318 | } LTTNG_PACKED; |
| 319 | |
| 320 | /* |
| 321 | * Status message returned to the sessiond after a received command. |
| 322 | */ |
| 323 | struct lttcomm_consumer_status_msg { |
| 324 | enum lttng_error_code ret_code; |
| 325 | } LTTNG_PACKED; |
| 326 | |
| 327 | struct lttcomm_consumer_status_channel { |
| 328 | enum lttng_error_code ret_code; |
| 329 | unsigned long key; |
| 330 | unsigned int stream_count; |
| 331 | } LTTNG_PACKED; |
| 332 | |
| 333 | #ifdef HAVE_LIBLTTNG_UST_CTL |
| 334 | |
| 335 | #include <lttng/ust-abi.h> |
| 336 | |
| 337 | /* |
| 338 | * Data structure for the commands sent from sessiond to UST. |
| 339 | */ |
| 340 | struct lttcomm_ust_msg { |
| 341 | uint32_t handle; |
| 342 | uint32_t cmd; |
| 343 | union { |
| 344 | struct lttng_ust_channel channel; |
| 345 | struct lttng_ust_stream stream; |
| 346 | struct lttng_ust_event event; |
| 347 | struct lttng_ust_context context; |
| 348 | struct lttng_ust_tracer_version version; |
| 349 | } u; |
| 350 | } LTTNG_PACKED; |
| 351 | |
| 352 | /* |
| 353 | * Data structure for the response from UST to the session daemon. |
| 354 | * cmd_type is sent back in the reply for validation. |
| 355 | */ |
| 356 | struct lttcomm_ust_reply { |
| 357 | uint32_t handle; |
| 358 | uint32_t cmd; |
| 359 | uint32_t ret_code; /* enum lttcomm_return_code */ |
| 360 | uint32_t ret_val; /* return value */ |
| 361 | union { |
| 362 | struct { |
| 363 | uint64_t memory_map_size; |
| 364 | } LTTNG_PACKED channel; |
| 365 | struct { |
| 366 | uint64_t memory_map_size; |
| 367 | } LTTNG_PACKED stream; |
| 368 | struct lttng_ust_tracer_version version; |
| 369 | } u; |
| 370 | } LTTNG_PACKED; |
| 371 | |
| 372 | #endif /* HAVE_LIBLTTNG_UST_CTL */ |
| 373 | |
| 374 | extern const char *lttcomm_get_readable_code(enum lttcomm_return_code code); |
| 375 | |
| 376 | extern int lttcomm_init_inet_sockaddr(struct lttcomm_sockaddr *sockaddr, |
| 377 | const char *ip, unsigned int port); |
| 378 | extern int lttcomm_init_inet6_sockaddr(struct lttcomm_sockaddr *sockaddr, |
| 379 | const char *ip, unsigned int port); |
| 380 | |
| 381 | extern struct lttcomm_sock *lttcomm_alloc_sock(enum lttcomm_sock_proto proto); |
| 382 | extern int lttcomm_create_sock(struct lttcomm_sock *sock); |
| 383 | extern struct lttcomm_sock *lttcomm_alloc_sock_from_uri(struct lttng_uri *uri); |
| 384 | extern void lttcomm_destroy_sock(struct lttcomm_sock *sock); |
| 385 | extern struct lttcomm_sock *lttcomm_alloc_copy_sock(struct lttcomm_sock *src); |
| 386 | extern void lttcomm_copy_sock(struct lttcomm_sock *dst, |
| 387 | struct lttcomm_sock *src); |
| 388 | |
| 389 | #endif /* _LTTNG_SESSIOND_COMM_H */ |