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 <common/compat/socket.hpp>
12 #include <common/credentials.hpp>
13 #include <common/payload.hpp>
14 #include <lttng/notification/channel-internal.hpp>
15 #include <lttng/ref-internal.hpp>
18 #include <urcu/rculfhash.h>
20 #include <urcu/call-rcu.h>
21 #include "notification-thread.hpp"
23 struct lttng_evaluation;
24 struct notification_thread_handle;
28 enum lttng_domain_type domain;
31 struct session_state_sample {
32 uint64_t consumed_data_size;
34 /* Whether a rotation is ongoing for this session. */
36 /* Identifier of the currently ongoing rotation. */
38 /* Location of last completed rotation. */
39 struct lttng_trace_archive_location *location;
50 * Hashtable containing back-refs (weak) to all channels in this session.
51 * The hashtable's key is a hash of (struct channel_key) and
52 * the value is of type (struct channel_info *).
54 struct cds_lfht *channel_infos_ht;
55 struct lttng_session_trigger_list *trigger_list;
56 /* Node in the notification thread state's sessions_ht. */
57 struct cds_lfht_node sessions_ht_node;
59 * Weak reference to the thread state's sessions_ht. Used for removal on
62 struct cds_lfht *sessions_ht;
63 /* Session's state as of the latest update. */
64 struct session_state_sample last_state_sample;
65 /* call_rcu delayed reclaim. */
66 struct rcu_head rcu_node;
70 struct channel_key key;
74 * A channel info holds a reference (lttng_ref) on session_info.
75 * session_info, in return, holds a weak reference to the channel.
77 struct session_info *session_info;
78 /* Node in the notification thread state's channels_ht. */
79 struct cds_lfht_node channels_ht_node;
80 /* Node in the session_info's channels_ht. */
81 struct cds_lfht_node session_info_channels_ht_node;
82 /* call_rcu delayed reclaim. */
83 struct rcu_head rcu_node;
87 * Facilities to carry the different notifications type in the action
88 * processing code path.
90 struct lttng_event_notifier_notification {
91 uint64_t tracer_token;
92 enum lttng_domain_type type;
93 size_t capture_buf_size;
97 struct notification_client_list_element {
98 struct notification_client *client;
99 struct cds_list_head node;
103 * Thread safety of notification_client and notification_client_list.
105 * The notification thread (main thread) and the action executor
106 * interact through client lists. Hence, when the action executor
107 * thread looks-up the list of clients subscribed to a given
108 * condition, it will acquire a reference to the list and lock it
109 * while attempting to communicate with the various clients.
111 * It is not necessary to reference-count clients as they are guaranteed
112 * to be 'alive' if they are present in a list and that list is locked. Indeed,
113 * removing references to the client from those subscription lists is part of
114 * the work performed on destruction of a client.
116 * No provision for other access scenarios are taken into account;
117 * this is the bare minimum to make these accesses safe and the
118 * notification thread's state is _not_ "thread-safe" in any general
121 struct notification_client_list {
122 pthread_mutex_t lock;
124 struct lttng_condition *condition;
125 /* List of triggers that have an identical condition than `condition`. */
126 struct cds_list_head triggers_list;
127 struct cds_list_head clients_list;
128 /* Weak reference to container. */
129 struct cds_lfht *notification_trigger_clients_ht;
130 struct cds_lfht_node notification_trigger_clients_ht_node;
131 /* call_rcu delayed reclaim. */
132 struct rcu_head rcu_node;
135 struct notification_client {
137 * Nests within the notification_client_list lock.
139 * Protects the outbound communication and the active flag which
140 * is used by both the notification and action executor threads.
142 * The remaining fields of the object can be used without any
143 * synchronization as they are either immutable (id, creds, version) or
144 * only accessed by the notification thread.
146 pthread_mutex_t lock;
147 notification_client_id id;
149 /* Client protocol version. */
150 uint8_t major, minor;
155 * Indicates if the credentials and versions of the client have been
160 * Conditions to which the client's notification channel is subscribed.
161 * List of struct lttng_condition_list_node. The condition member is
162 * owned by the client.
164 struct cds_list_head condition_list;
165 struct cds_lfht_node client_socket_ht_node;
166 struct cds_lfht_node client_id_ht_node;
169 * If a client's communication is inactive, it means that a
170 * fatal error has occurred (could be either a protocol error or
171 * the socket API returned a fatal error). No further
172 * communication should be attempted; the client is queued for
176 int current_poll_events;
179 * During the reception of a message, the reception
180 * buffers' "size" is set to contain the current
181 * message's complete payload.
183 struct lttng_payload payload;
184 /* Bytes left to receive for the current message. */
185 size_t bytes_to_receive;
186 /* FDs left to receive for the current message. */
188 /* Type of the message being received. */
189 enum lttng_notification_channel_message_type msg_type;
191 * Indicates whether or not credentials are expected
196 * Indicates whether or not credentials were received
200 /* Only used during credentials reception. */
201 lttng_sock_cred creds;
205 * Indicates whether or not a notification addressed to
206 * this client was dropped because a command reply was
209 * A notification is dropped whenever the buffer is not
212 bool dropped_notification;
214 * Indicates whether or not a command reply is already
215 * buffered. In this case, it means that the client is
216 * not consuming command replies before emitting a new
217 * one. This could be caused by a protocol error or a
218 * misbehaving/malicious client.
220 bool queued_command_reply;
221 struct lttng_payload payload;
224 /* call_rcu delayed reclaim. */
225 struct rcu_head rcu_node;
228 enum client_transmission_status {
229 CLIENT_TRANSMISSION_STATUS_COMPLETE,
230 CLIENT_TRANSMISSION_STATUS_QUEUED,
231 /* Communication failure. */
232 CLIENT_TRANSMISSION_STATUS_FAIL,
234 CLIENT_TRANSMISSION_STATUS_ERROR,
237 bool notification_client_list_get(struct notification_client_list *list);
239 void notification_client_list_put(struct notification_client_list *list);
241 /* Only returns a non-zero value if a fatal error occurred. */
242 typedef int (*report_client_transmission_result_cb)(
243 struct notification_client *client,
244 enum client_transmission_status status,
247 int notification_client_list_send_evaluation(
248 struct notification_client_list *list,
249 const struct lttng_trigger *trigger,
250 const struct lttng_evaluation *evaluation,
251 const struct lttng_credentials *source_object_creds,
252 report_client_transmission_result_cb client_report,
255 int notification_thread_client_communication_update(
256 struct notification_thread_handle *handle,
257 notification_client_id id,
258 enum client_transmission_status transmission_status);
261 * Takes ownership of the payload if present.
263 struct lttng_event_notifier_notification *lttng_event_notifier_notification_create(
264 uint64_t tracer_token,
265 enum lttng_domain_type domain,
267 size_t payload_size);
269 void lttng_event_notifier_notification_destroy(
270 struct lttng_event_notifier_notification *event_notifier_notification);
272 #endif /* NOTIFICATION_THREAD_INTERNAL_H */