| 1 | /* SPDX-License-Identifier: (GPL-2.0-only or LGPL-2.1-only) |
| 2 | * |
| 3 | * lttng-ring-buffer-client.h |
| 4 | * |
| 5 | * LTTng lib ring buffer client template. |
| 6 | * |
| 7 | * Copyright (C) 2010-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
| 8 | */ |
| 9 | |
| 10 | #include <linux/module.h> |
| 11 | #include <linux/types.h> |
| 12 | #include <wrapper/vmalloc.h> /* for wrapper_vmalloc_sync_mappings() */ |
| 13 | #include <lttng/events.h> |
| 14 | #include <lttng/events-internal.h> |
| 15 | #include <lttng/tracer.h> |
| 16 | |
| 17 | static struct lttng_transport lttng_relay_transport; |
| 18 | |
| 19 | struct metadata_packet_header { |
| 20 | uint32_t magic; /* 0x75D11D57 */ |
| 21 | uint8_t uuid[16]; /* Unique Universal Identifier */ |
| 22 | uint32_t checksum; /* 0 if unused */ |
| 23 | uint32_t content_size; /* in bits */ |
| 24 | uint32_t packet_size; /* in bits */ |
| 25 | uint8_t compression_scheme; /* 0 if unused */ |
| 26 | uint8_t encryption_scheme; /* 0 if unused */ |
| 27 | uint8_t checksum_scheme; /* 0 if unused */ |
| 28 | uint8_t major; /* CTF spec major version number */ |
| 29 | uint8_t minor; /* CTF spec minor version number */ |
| 30 | uint8_t header_end[0]; |
| 31 | }; |
| 32 | |
| 33 | struct metadata_record_header { |
| 34 | uint8_t header_end[0]; /* End of header */ |
| 35 | }; |
| 36 | |
| 37 | static const struct lttng_kernel_ring_buffer_config client_config; |
| 38 | |
| 39 | static inline |
| 40 | u64 lib_ring_buffer_clock_read(struct lttng_kernel_ring_buffer_channel *chan) |
| 41 | { |
| 42 | return 0; |
| 43 | } |
| 44 | |
| 45 | static inline |
| 46 | size_t record_header_size(const struct lttng_kernel_ring_buffer_config *config, |
| 47 | struct lttng_kernel_ring_buffer_channel *chan, size_t offset, |
| 48 | size_t *pre_header_padding, |
| 49 | struct lttng_kernel_ring_buffer_ctx *ctx, |
| 50 | void *client_ctx) |
| 51 | { |
| 52 | return 0; |
| 53 | } |
| 54 | |
| 55 | #include <ringbuffer/api.h> |
| 56 | |
| 57 | static u64 client_ring_buffer_clock_read(struct lttng_kernel_ring_buffer_channel *chan) |
| 58 | { |
| 59 | return 0; |
| 60 | } |
| 61 | |
| 62 | static |
| 63 | size_t client_record_header_size(const struct lttng_kernel_ring_buffer_config *config, |
| 64 | struct lttng_kernel_ring_buffer_channel *chan, size_t offset, |
| 65 | size_t *pre_header_padding, |
| 66 | struct lttng_kernel_ring_buffer_ctx *ctx, |
| 67 | void *client_ctx) |
| 68 | { |
| 69 | return 0; |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * client_packet_header_size - called on buffer-switch to a new sub-buffer |
| 74 | * |
| 75 | * Return header size without padding after the structure. Don't use packed |
| 76 | * structure because gcc generates inefficient code on some architectures |
| 77 | * (powerpc, mips..) |
| 78 | */ |
| 79 | static size_t client_packet_header_size(void) |
| 80 | { |
| 81 | return offsetof(struct metadata_packet_header, header_end); |
| 82 | } |
| 83 | |
| 84 | static void client_buffer_begin(struct lttng_kernel_ring_buffer *buf, u64 timestamp, |
| 85 | unsigned int subbuf_idx) |
| 86 | { |
| 87 | struct lttng_kernel_ring_buffer_channel *chan = buf->backend.chan; |
| 88 | struct metadata_packet_header *header = |
| 89 | (struct metadata_packet_header *) |
| 90 | lib_ring_buffer_offset_address(&buf->backend, |
| 91 | subbuf_idx * chan->backend.subbuf_size); |
| 92 | struct lttng_metadata_cache *metadata_cache = |
| 93 | channel_get_private(chan); |
| 94 | |
| 95 | header->magic = TSDL_MAGIC_NUMBER; |
| 96 | memcpy(header->uuid, metadata_cache->uuid.b, |
| 97 | sizeof(metadata_cache->uuid)); |
| 98 | header->checksum = 0; /* 0 if unused */ |
| 99 | header->content_size = 0xFFFFFFFF; /* in bits, for debugging */ |
| 100 | header->packet_size = 0xFFFFFFFF; /* in bits, for debugging */ |
| 101 | header->compression_scheme = 0; /* 0 if unused */ |
| 102 | header->encryption_scheme = 0; /* 0 if unused */ |
| 103 | header->checksum_scheme = 0; /* 0 if unused */ |
| 104 | header->major = CTF_SPEC_MAJOR; |
| 105 | header->minor = CTF_SPEC_MINOR; |
| 106 | } |
| 107 | |
| 108 | /* |
| 109 | * offset is assumed to never be 0 here : never deliver a completely empty |
| 110 | * subbuffer. data_size is between 1 and subbuf_size. |
| 111 | */ |
| 112 | static void client_buffer_end(struct lttng_kernel_ring_buffer *buf, u64 timestamp, |
| 113 | unsigned int subbuf_idx, unsigned long data_size, |
| 114 | const struct lttng_kernel_ring_buffer_ctx *ctx) |
| 115 | { |
| 116 | struct lttng_kernel_ring_buffer_channel *chan = buf->backend.chan; |
| 117 | struct metadata_packet_header *header = |
| 118 | (struct metadata_packet_header *) |
| 119 | lib_ring_buffer_offset_address(&buf->backend, |
| 120 | subbuf_idx * chan->backend.subbuf_size); |
| 121 | unsigned long records_lost = 0; |
| 122 | |
| 123 | header->content_size = data_size * CHAR_BIT; /* in bits */ |
| 124 | header->packet_size = PAGE_ALIGN(data_size) * CHAR_BIT; /* in bits */ |
| 125 | /* |
| 126 | * We do not care about the records lost count, because the metadata |
| 127 | * channel waits and retry. |
| 128 | */ |
| 129 | (void) lib_ring_buffer_get_records_lost_full(&client_config, ctx); |
| 130 | records_lost += lib_ring_buffer_get_records_lost_wrap(&client_config, ctx); |
| 131 | records_lost += lib_ring_buffer_get_records_lost_big(&client_config, ctx); |
| 132 | WARN_ON_ONCE(records_lost != 0); |
| 133 | } |
| 134 | |
| 135 | static int client_buffer_create(struct lttng_kernel_ring_buffer *buf, void *priv, |
| 136 | int cpu, const char *name) |
| 137 | { |
| 138 | return 0; |
| 139 | } |
| 140 | |
| 141 | static void client_buffer_finalize(struct lttng_kernel_ring_buffer *buf, void *priv, int cpu) |
| 142 | { |
| 143 | } |
| 144 | |
| 145 | static int client_timestamp_begin(const struct lttng_kernel_ring_buffer_config *config, |
| 146 | struct lttng_kernel_ring_buffer *buf, uint64_t *timestamp_begin) |
| 147 | { |
| 148 | return -ENOSYS; |
| 149 | } |
| 150 | |
| 151 | static int client_timestamp_end(const struct lttng_kernel_ring_buffer_config *config, |
| 152 | struct lttng_kernel_ring_buffer *bufb, |
| 153 | uint64_t *timestamp_end) |
| 154 | { |
| 155 | return -ENOSYS; |
| 156 | } |
| 157 | |
| 158 | static int client_events_discarded(const struct lttng_kernel_ring_buffer_config *config, |
| 159 | struct lttng_kernel_ring_buffer *bufb, |
| 160 | uint64_t *events_discarded) |
| 161 | { |
| 162 | return -ENOSYS; |
| 163 | } |
| 164 | |
| 165 | static int client_current_timestamp(const struct lttng_kernel_ring_buffer_config *config, |
| 166 | struct lttng_kernel_ring_buffer *bufb, |
| 167 | uint64_t *ts) |
| 168 | { |
| 169 | return -ENOSYS; |
| 170 | } |
| 171 | |
| 172 | static int client_content_size(const struct lttng_kernel_ring_buffer_config *config, |
| 173 | struct lttng_kernel_ring_buffer *bufb, |
| 174 | uint64_t *content_size) |
| 175 | { |
| 176 | return -ENOSYS; |
| 177 | } |
| 178 | |
| 179 | static int client_packet_size(const struct lttng_kernel_ring_buffer_config *config, |
| 180 | struct lttng_kernel_ring_buffer *bufb, |
| 181 | uint64_t *packet_size) |
| 182 | { |
| 183 | return -ENOSYS; |
| 184 | } |
| 185 | |
| 186 | static int client_stream_id(const struct lttng_kernel_ring_buffer_config *config, |
| 187 | struct lttng_kernel_ring_buffer *bufb, |
| 188 | uint64_t *stream_id) |
| 189 | { |
| 190 | return -ENOSYS; |
| 191 | } |
| 192 | |
| 193 | static int client_sequence_number(const struct lttng_kernel_ring_buffer_config *config, |
| 194 | struct lttng_kernel_ring_buffer *bufb, |
| 195 | uint64_t *seq) |
| 196 | { |
| 197 | return -ENOSYS; |
| 198 | } |
| 199 | |
| 200 | static |
| 201 | int client_instance_id(const struct lttng_kernel_ring_buffer_config *config, |
| 202 | struct lttng_kernel_ring_buffer *bufb, |
| 203 | uint64_t *id) |
| 204 | { |
| 205 | return -ENOSYS; |
| 206 | } |
| 207 | |
| 208 | static const struct lttng_kernel_ring_buffer_config client_config = { |
| 209 | .cb.ring_buffer_clock_read = client_ring_buffer_clock_read, |
| 210 | .cb.record_header_size = client_record_header_size, |
| 211 | .cb.subbuffer_header_size = client_packet_header_size, |
| 212 | .cb.buffer_begin = client_buffer_begin, |
| 213 | .cb.buffer_end = client_buffer_end, |
| 214 | .cb.buffer_create = client_buffer_create, |
| 215 | .cb.buffer_finalize = client_buffer_finalize, |
| 216 | |
| 217 | .timestamp_bits = 0, |
| 218 | .alloc = RING_BUFFER_ALLOC_GLOBAL, |
| 219 | .sync = RING_BUFFER_SYNC_GLOBAL, |
| 220 | .mode = RING_BUFFER_MODE_TEMPLATE, |
| 221 | .backend = RING_BUFFER_PAGE, |
| 222 | .output = RING_BUFFER_OUTPUT_TEMPLATE, |
| 223 | .oops = RING_BUFFER_OOPS_CONSISTENCY, |
| 224 | .ipi = RING_BUFFER_IPI_BARRIER, |
| 225 | .wakeup = RING_BUFFER_WAKEUP_BY_TIMER, |
| 226 | }; |
| 227 | |
| 228 | static |
| 229 | void release_priv_ops(void *priv_ops) |
| 230 | { |
| 231 | module_put(THIS_MODULE); |
| 232 | } |
| 233 | |
| 234 | static |
| 235 | void lttng_channel_destroy(struct lttng_kernel_ring_buffer_channel *chan) |
| 236 | { |
| 237 | channel_destroy(chan); |
| 238 | } |
| 239 | |
| 240 | static |
| 241 | struct lttng_kernel_ring_buffer_channel *_channel_create(const char *name, |
| 242 | void *priv, void *buf_addr, |
| 243 | size_t subbuf_size, size_t num_subbuf, |
| 244 | unsigned int switch_timer_interval, |
| 245 | unsigned int read_timer_interval) |
| 246 | { |
| 247 | struct lttng_kernel_channel_buffer *lttng_chan = priv; |
| 248 | struct lttng_kernel_ring_buffer_channel *chan; |
| 249 | |
| 250 | chan = channel_create(&client_config, name, |
| 251 | lttng_chan->parent.session->priv->metadata_cache, buf_addr, |
| 252 | subbuf_size, num_subbuf, switch_timer_interval, |
| 253 | read_timer_interval); |
| 254 | if (chan) { |
| 255 | /* |
| 256 | * Ensure this module is not unloaded before we finish |
| 257 | * using lttng_relay_transport.ops. |
| 258 | */ |
| 259 | if (!try_module_get(THIS_MODULE)) { |
| 260 | printk(KERN_WARNING "LTTng: Can't lock transport module.\n"); |
| 261 | goto error; |
| 262 | } |
| 263 | chan->backend.priv_ops = <tng_relay_transport.ops; |
| 264 | chan->backend.release_priv_ops = release_priv_ops; |
| 265 | } |
| 266 | return chan; |
| 267 | |
| 268 | error: |
| 269 | lttng_channel_destroy(chan); |
| 270 | return NULL; |
| 271 | } |
| 272 | |
| 273 | static |
| 274 | struct lttng_kernel_ring_buffer *lttng_buffer_read_open(struct lttng_kernel_ring_buffer_channel *chan) |
| 275 | { |
| 276 | struct lttng_kernel_ring_buffer *buf; |
| 277 | |
| 278 | buf = channel_get_ring_buffer(&client_config, chan, 0); |
| 279 | if (!lib_ring_buffer_open_read(buf)) |
| 280 | return buf; |
| 281 | return NULL; |
| 282 | } |
| 283 | |
| 284 | static |
| 285 | int lttng_buffer_has_read_closed_stream(struct lttng_kernel_ring_buffer_channel *chan) |
| 286 | { |
| 287 | struct lttng_kernel_ring_buffer *buf; |
| 288 | int cpu; |
| 289 | |
| 290 | for_each_channel_cpu(cpu, chan) { |
| 291 | buf = channel_get_ring_buffer(&client_config, chan, cpu); |
| 292 | if (!atomic_long_read(&buf->active_readers)) |
| 293 | return 1; |
| 294 | } |
| 295 | return 0; |
| 296 | } |
| 297 | |
| 298 | static |
| 299 | void lttng_buffer_read_close(struct lttng_kernel_ring_buffer *buf) |
| 300 | { |
| 301 | lib_ring_buffer_release_read(buf); |
| 302 | } |
| 303 | |
| 304 | static |
| 305 | int lttng_event_reserve(struct lttng_kernel_ring_buffer_ctx *ctx) |
| 306 | { |
| 307 | struct lttng_kernel_ring_buffer_channel *chan = ctx->client_priv; |
| 308 | int ret; |
| 309 | |
| 310 | memset(&ctx->priv, 0, sizeof(ctx->priv)); |
| 311 | ctx->priv.chan = chan; |
| 312 | |
| 313 | ret = lib_ring_buffer_reserve(&client_config, ctx, NULL); |
| 314 | if (ret) |
| 315 | return ret; |
| 316 | lib_ring_buffer_backend_get_pages(&client_config, ctx, |
| 317 | &ctx->priv.backend_pages); |
| 318 | return 0; |
| 319 | |
| 320 | } |
| 321 | |
| 322 | static |
| 323 | void lttng_event_commit(struct lttng_kernel_ring_buffer_ctx *ctx) |
| 324 | { |
| 325 | lib_ring_buffer_commit(&client_config, ctx); |
| 326 | } |
| 327 | |
| 328 | static |
| 329 | void lttng_event_write(struct lttng_kernel_ring_buffer_ctx *ctx, const void *src, |
| 330 | size_t len, size_t alignment) |
| 331 | { |
| 332 | lib_ring_buffer_align_ctx(ctx, alignment); |
| 333 | lib_ring_buffer_write(&client_config, ctx, src, len); |
| 334 | } |
| 335 | |
| 336 | static |
| 337 | void lttng_event_write_from_user(struct lttng_kernel_ring_buffer_ctx *ctx, |
| 338 | const void __user *src, size_t len, size_t alignment) |
| 339 | { |
| 340 | lib_ring_buffer_align_ctx(ctx, alignment); |
| 341 | lib_ring_buffer_copy_from_user_inatomic(&client_config, ctx, src, len); |
| 342 | } |
| 343 | |
| 344 | static |
| 345 | void lttng_event_memset(struct lttng_kernel_ring_buffer_ctx *ctx, |
| 346 | int c, size_t len) |
| 347 | { |
| 348 | lib_ring_buffer_memset(&client_config, ctx, c, len); |
| 349 | } |
| 350 | |
| 351 | static |
| 352 | void lttng_event_strcpy(struct lttng_kernel_ring_buffer_ctx *ctx, const char *src, |
| 353 | size_t len) |
| 354 | { |
| 355 | lib_ring_buffer_strcpy(&client_config, ctx, src, len, '#'); |
| 356 | } |
| 357 | |
| 358 | static |
| 359 | void lttng_event_pstrcpy_pad(struct lttng_kernel_ring_buffer_ctx *ctx, const char *src, |
| 360 | size_t len) |
| 361 | { |
| 362 | lib_ring_buffer_pstrcpy(&client_config, ctx, src, len, '\0'); |
| 363 | } |
| 364 | |
| 365 | static |
| 366 | size_t lttng_packet_avail_size(struct lttng_kernel_ring_buffer_channel *chan) |
| 367 | { |
| 368 | unsigned long o_begin; |
| 369 | struct lttng_kernel_ring_buffer *buf; |
| 370 | |
| 371 | buf = chan->backend.buf; /* Only for global buffer ! */ |
| 372 | o_begin = v_read(&client_config, &buf->offset); |
| 373 | if (subbuf_offset(o_begin, chan) != 0) { |
| 374 | return chan->backend.subbuf_size - subbuf_offset(o_begin, chan); |
| 375 | } else { |
| 376 | return chan->backend.subbuf_size - subbuf_offset(o_begin, chan) |
| 377 | - sizeof(struct metadata_packet_header); |
| 378 | } |
| 379 | } |
| 380 | |
| 381 | static |
| 382 | wait_queue_head_t *lttng_get_writer_buf_wait_queue(struct lttng_kernel_ring_buffer_channel *chan, int cpu) |
| 383 | { |
| 384 | struct lttng_kernel_ring_buffer *buf = channel_get_ring_buffer(&client_config, |
| 385 | chan, cpu); |
| 386 | return &buf->write_wait; |
| 387 | } |
| 388 | |
| 389 | static |
| 390 | wait_queue_head_t *lttng_get_hp_wait_queue(struct lttng_kernel_ring_buffer_channel *chan) |
| 391 | { |
| 392 | return &chan->hp_wait; |
| 393 | } |
| 394 | |
| 395 | static |
| 396 | int lttng_is_finalized(struct lttng_kernel_ring_buffer_channel *chan) |
| 397 | { |
| 398 | return lib_ring_buffer_channel_is_finalized(chan); |
| 399 | } |
| 400 | |
| 401 | static |
| 402 | int lttng_is_disabled(struct lttng_kernel_ring_buffer_channel *chan) |
| 403 | { |
| 404 | return lib_ring_buffer_channel_is_disabled(chan); |
| 405 | } |
| 406 | |
| 407 | static struct lttng_transport lttng_relay_transport = { |
| 408 | .name = "relay-" RING_BUFFER_MODE_TEMPLATE_STRING, |
| 409 | .owner = THIS_MODULE, |
| 410 | .ops = { |
| 411 | .priv = __LTTNG_COMPOUND_LITERAL(struct lttng_kernel_channel_buffer_ops_private, { |
| 412 | .pub = <tng_relay_transport.ops, |
| 413 | .channel_create = _channel_create, |
| 414 | .channel_destroy = lttng_channel_destroy, |
| 415 | .buffer_read_open = lttng_buffer_read_open, |
| 416 | .buffer_has_read_closed_stream = |
| 417 | lttng_buffer_has_read_closed_stream, |
| 418 | .buffer_read_close = lttng_buffer_read_close, |
| 419 | .packet_avail_size = lttng_packet_avail_size, |
| 420 | .get_writer_buf_wait_queue = lttng_get_writer_buf_wait_queue, |
| 421 | .get_hp_wait_queue = lttng_get_hp_wait_queue, |
| 422 | .is_finalized = lttng_is_finalized, |
| 423 | .is_disabled = lttng_is_disabled, |
| 424 | .timestamp_begin = client_timestamp_begin, |
| 425 | .timestamp_end = client_timestamp_end, |
| 426 | .events_discarded = client_events_discarded, |
| 427 | .content_size = client_content_size, |
| 428 | .packet_size = client_packet_size, |
| 429 | .stream_id = client_stream_id, |
| 430 | .current_timestamp = client_current_timestamp, |
| 431 | .sequence_number = client_sequence_number, |
| 432 | .instance_id = client_instance_id, |
| 433 | }), |
| 434 | .event_reserve = lttng_event_reserve, |
| 435 | .event_commit = lttng_event_commit, |
| 436 | .event_write_from_user = lttng_event_write_from_user, |
| 437 | .event_memset = lttng_event_memset, |
| 438 | .event_write = lttng_event_write, |
| 439 | .event_strcpy = lttng_event_strcpy, |
| 440 | .event_pstrcpy_pad = lttng_event_pstrcpy_pad, |
| 441 | }, |
| 442 | }; |
| 443 | |
| 444 | static int __init lttng_ring_buffer_client_init(void) |
| 445 | { |
| 446 | /* |
| 447 | * This vmalloc sync all also takes care of the lib ring buffer |
| 448 | * vmalloc'd module pages when it is built as a module into LTTng. |
| 449 | */ |
| 450 | wrapper_vmalloc_sync_mappings(); |
| 451 | lttng_transport_register(<tng_relay_transport); |
| 452 | return 0; |
| 453 | } |
| 454 | |
| 455 | module_init(lttng_ring_buffer_client_init); |
| 456 | |
| 457 | static void __exit lttng_ring_buffer_client_exit(void) |
| 458 | { |
| 459 | lttng_transport_unregister(<tng_relay_transport); |
| 460 | } |
| 461 | |
| 462 | module_exit(lttng_ring_buffer_client_exit); |
| 463 | |
| 464 | MODULE_LICENSE("GPL and additional rights"); |
| 465 | MODULE_AUTHOR("Mathieu Desnoyers <mathieu.desnoyers@efficios.com>"); |
| 466 | MODULE_DESCRIPTION("LTTng ring buffer " RING_BUFFER_MODE_TEMPLATE_STRING |
| 467 | " client"); |
| 468 | MODULE_VERSION(__stringify(LTTNG_MODULES_MAJOR_VERSION) "." |
| 469 | __stringify(LTTNG_MODULES_MINOR_VERSION) "." |
| 470 | __stringify(LTTNG_MODULES_PATCHLEVEL_VERSION) |
| 471 | LTTNG_MODULES_EXTRAVERSION); |