| 1 | /* |
| 2 | * Copyright (C) 2017 - Jérémie Galarneau <jeremie.galarneau@efficios.com> |
| 3 | * |
| 4 | * This library is free software; you can redistribute it and/or modify it |
| 5 | * under the terms of the GNU Lesser General Public License, version 2.1 only, |
| 6 | * as published by the Free Software Foundation. |
| 7 | * |
| 8 | * This library 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 Lesser General Public License |
| 11 | * for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU Lesser General Public License |
| 14 | * along with this library; if not, write to the Free Software Foundation, |
| 15 | * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 16 | */ |
| 17 | |
| 18 | #ifndef LTTNG_NOTIFICATION_CHANNEL_INTERNAL_H |
| 19 | #define LTTNG_NOTIFICATION_CHANNEL_INTERNAL_H |
| 20 | |
| 21 | #include <lttng/notification/channel.h> |
| 22 | #include <common/macros.h> |
| 23 | #include <common/dynamic-buffer.h> |
| 24 | #include <stdint.h> |
| 25 | #include <stdbool.h> |
| 26 | #include <pthread.h> |
| 27 | #include <urcu/list.h> |
| 28 | |
| 29 | /* |
| 30 | * Protocol version change log: |
| 31 | * - v1.0 |
| 32 | * - Initial implementation of the notification channel protocol, |
| 33 | * - Supported conditions are LOW/HIGH buffer usage conditions, |
| 34 | * - v1.1 |
| 35 | * - New condition type "LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE" added, |
| 36 | * - New condition type "LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING" added, |
| 37 | * - New condition type "LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED" added, |
| 38 | */ |
| 39 | #define LTTNG_NOTIFICATION_CHANNEL_VERSION_MAJOR 1 |
| 40 | #define LTTNG_NOTIFICATION_CHANNEL_VERSION_MINOR 1 |
| 41 | |
| 42 | enum lttng_notification_channel_message_type { |
| 43 | LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNKNOWN = -1, |
| 44 | LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE = 0, |
| 45 | LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_SUBSCRIBE = 1, |
| 46 | LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNSUBSCRIBE = 2, |
| 47 | LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_COMMAND_REPLY = 3, |
| 48 | LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_NOTIFICATION = 4, |
| 49 | LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_NOTIFICATION_DROPPED = 5, |
| 50 | }; |
| 51 | |
| 52 | struct lttng_notification_channel_message { |
| 53 | /* enum lttng_notification_channel_message_type */ |
| 54 | int8_t type; |
| 55 | /* Size of the payload following this field. */ |
| 56 | uint32_t size; |
| 57 | char payload[]; |
| 58 | } LTTNG_PACKED; |
| 59 | |
| 60 | struct lttng_notification_channel_command_handshake { |
| 61 | uint8_t major; |
| 62 | uint8_t minor; |
| 63 | } LTTNG_PACKED; |
| 64 | |
| 65 | struct lttng_notification_channel_command_reply { |
| 66 | /* enum lttng_notification_channel_status */ |
| 67 | int8_t status; |
| 68 | } LTTNG_PACKED; |
| 69 | |
| 70 | struct pending_notification { |
| 71 | /* NULL means "notification dropped". */ |
| 72 | struct lttng_notification *notification; |
| 73 | struct cds_list_head node; |
| 74 | }; |
| 75 | |
| 76 | /* |
| 77 | * The notification channel protocol is bidirectional and accommodates |
| 78 | * synchronous and asynchronous communication modes: |
| 79 | * |
| 80 | * - Synchronous: commands emitted by the client to which a reply is expected |
| 81 | * (e.g. subscribing/unsubscribing to conditions), |
| 82 | * - Asynchronous: notifications which are sent by the lttng_endpoint to the |
| 83 | * client as one of the subscribed condition has occurred. |
| 84 | * |
| 85 | * The nature of this hybrid communication mode means that asynchronous messages |
| 86 | * (e.g. notifications) may be interleaved between synchronous messages (e.g. a |
| 87 | * command and its reply). |
| 88 | * |
| 89 | * Notifications that are received between a command and its reply and enqueued |
| 90 | * in the pending_notifications list. |
| 91 | */ |
| 92 | struct lttng_notification_channel { |
| 93 | pthread_mutex_t lock; |
| 94 | int socket; |
| 95 | struct { |
| 96 | /* Count of pending notifications. */ |
| 97 | unsigned int count; |
| 98 | /* List of struct pending_notification. */ |
| 99 | struct cds_list_head list; |
| 100 | } pending_notifications; |
| 101 | struct lttng_dynamic_buffer reception_buffer; |
| 102 | /* Sessiond notification protocol version. */ |
| 103 | struct { |
| 104 | bool set; |
| 105 | int8_t major, minor; |
| 106 | } version; |
| 107 | }; |
| 108 | |
| 109 | #endif /* LTTNG_NOTIFICATION_CHANNEL_INTERNAL_H */ |