2 * Copyright (C) 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 * SPDX-License-Identifier: GPL-2.0-only
8 #ifndef NOTIFICATION_THREAD_INTERNAL_H
9 #define NOTIFICATION_THREAD_INTERNAL_H
11 #include "notification-thread.hpp"
13 #include <common/compat/socket.hpp>
14 #include <common/credentials.hpp>
15 #include <common/payload.hpp>
17 #include <lttng/notification/channel-internal.hpp>
18 #include <lttng/ref-internal.hpp>
22 #include <urcu/call-rcu.h>
23 #include <urcu/rculfhash.h>
26 struct lttng_evaluation;
27 struct notification_thread_handle;
31 enum lttng_domain_type domain;
34 struct session_state_sample {
35 uint64_t consumed_data_size;
37 /* Whether a rotation is ongoing for this session. */
39 /* Identifier of the currently ongoing rotation. */
41 /* Location of last completed rotation. */
42 struct lttng_trace_archive_location *location;
53 * Hashtable containing back-refs (weak) to all channels in this session.
54 * The hashtable's key is a hash of (struct channel_key) and
55 * the value is of type (struct channel_info *).
57 struct cds_lfht *channel_infos_ht;
58 struct lttng_session_trigger_list *trigger_list;
59 /* Node in the notification thread state's sessions_ht. */
60 struct cds_lfht_node sessions_ht_node;
62 * Weak reference to the thread state's sessions_ht. Used for removal on
65 struct cds_lfht *sessions_ht;
66 /* Session's state as of the latest update. */
67 struct session_state_sample last_state_sample;
68 /* call_rcu delayed reclaim. */
69 struct rcu_head rcu_node;
73 struct channel_key key;
77 * A channel info holds a reference (lttng_ref) on session_info.
78 * session_info, in return, holds a weak reference to the channel.
80 struct session_info *session_info;
81 /* Node in the notification thread state's channels_ht. */
82 struct cds_lfht_node channels_ht_node;
83 /* Node in the session_info's channels_ht. */
84 struct cds_lfht_node session_info_channels_ht_node;
85 /* call_rcu delayed reclaim. */
86 struct rcu_head rcu_node;
90 * Facilities to carry the different notifications type in the action
91 * processing code path.
93 struct lttng_event_notifier_notification {
94 uint64_t tracer_token;
95 enum lttng_domain_type type;
96 size_t capture_buf_size;
100 struct notification_client_list_element {
101 struct notification_client *client;
102 struct cds_list_head node;
106 * Thread safety of notification_client and notification_client_list.
108 * The notification thread (main thread) and the action executor
109 * interact through client lists. Hence, when the action executor
110 * thread looks-up the list of clients subscribed to a given
111 * condition, it will acquire a reference to the list and lock it
112 * while attempting to communicate with the various clients.
114 * It is not necessary to reference-count clients as they are guaranteed
115 * to be 'alive' if they are present in a list and that list is locked. Indeed,
116 * removing references to the client from those subscription lists is part of
117 * the work performed on destruction of a client.
119 * No provision for other access scenarios are taken into account;
120 * this is the bare minimum to make these accesses safe and the
121 * notification thread's state is _not_ "thread-safe" in any general
124 struct notification_client_list {
125 pthread_mutex_t lock;
127 struct lttng_condition *condition;
128 /* List of triggers that have an identical condition than `condition`. */
129 struct cds_list_head triggers_list;
130 struct cds_list_head clients_list;
131 /* Weak reference to container. */
132 struct cds_lfht *notification_trigger_clients_ht;
133 struct cds_lfht_node notification_trigger_clients_ht_node;
134 /* call_rcu delayed reclaim. */
135 struct rcu_head rcu_node;
138 struct notification_client {
140 * Nests within the notification_client_list lock.
142 * Protects the outbound communication and the active flag which
143 * is used by both the notification and action executor threads.
145 * The remaining fields of the object can be used without any
146 * synchronization as they are either immutable (id, creds, version) or
147 * only accessed by the notification thread.
149 pthread_mutex_t lock;
150 notification_client_id id;
152 /* Client protocol version. */
153 uint8_t major, minor;
158 * Indicates if the credentials and versions of the client have been
163 * Conditions to which the client's notification channel is subscribed.
164 * List of struct lttng_condition_list_node. The condition member is
165 * owned by the client.
167 struct cds_list_head condition_list;
168 struct cds_lfht_node client_socket_ht_node;
169 struct cds_lfht_node client_id_ht_node;
172 * If a client's communication is inactive, it means that a
173 * fatal error has occurred (could be either a protocol error or
174 * the socket API returned a fatal error). No further
175 * communication should be attempted; the client is queued for
179 int current_poll_events;
182 * During the reception of a message, the reception
183 * buffers' "size" is set to contain the current
184 * message's complete payload.
186 struct lttng_payload payload;
187 /* Bytes left to receive for the current message. */
188 size_t bytes_to_receive;
189 /* FDs left to receive for the current message. */
191 /* Type of the message being received. */
192 enum lttng_notification_channel_message_type msg_type;
194 * Indicates whether or not credentials are expected
199 * Indicates whether or not credentials were received
203 /* Only used during credentials reception. */
204 lttng_sock_cred creds;
208 * Indicates whether or not a notification addressed to
209 * this client was dropped because a command reply was
212 * A notification is dropped whenever the buffer is not
215 bool dropped_notification;
217 * Indicates whether or not a command reply is already
218 * buffered. In this case, it means that the client is
219 * not consuming command replies before emitting a new
220 * one. This could be caused by a protocol error or a
221 * misbehaving/malicious client.
223 bool queued_command_reply;
224 struct lttng_payload payload;
227 /* call_rcu delayed reclaim. */
228 struct rcu_head rcu_node;
231 enum client_transmission_status {
232 CLIENT_TRANSMISSION_STATUS_COMPLETE,
233 CLIENT_TRANSMISSION_STATUS_QUEUED,
234 /* Communication failure. */
235 CLIENT_TRANSMISSION_STATUS_FAIL,
237 CLIENT_TRANSMISSION_STATUS_ERROR,
240 bool notification_client_list_get(struct notification_client_list *list);
242 void notification_client_list_put(struct notification_client_list *list);
244 /* Only returns a non-zero value if a fatal error occurred. */
245 using report_client_transmission_result_cb = int (*)(struct notification_client *,
246 enum client_transmission_status,
249 int notification_client_list_send_evaluation(struct notification_client_list *list,
250 const struct lttng_trigger *trigger,
251 const struct lttng_evaluation *evaluation,
252 const struct lttng_credentials *source_object_creds,
253 report_client_transmission_result_cb client_report,
256 int notification_thread_client_communication_update(
257 struct notification_thread_handle *handle,
258 notification_client_id id,
259 enum client_transmission_status transmission_status);
262 * Takes ownership of the payload if present.
264 struct lttng_event_notifier_notification *lttng_event_notifier_notification_create(
265 uint64_t tracer_token, enum lttng_domain_type domain, char *payload, size_t payload_size);
267 void lttng_event_notifier_notification_destroy(
268 struct lttng_event_notifier_notification *event_notifier_notification);
270 #endif /* NOTIFICATION_THREAD_INTERNAL_H */