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/credentials.h>
12 #include <lttng/notification/channel-internal.h>
13 #include <lttng/ref-internal.h>
16 #include <urcu/rculfhash.h>
18 #include <urcu/call-rcu.h>
19 #include "notification-thread.h"
21 struct lttng_evaluation
;
22 struct notification_thread_handle
;
26 enum lttng_domain_type domain
;
35 * Hashtable containing back-refs (weak) to all channels in this session.
36 * The hashtable's key is a hash of (struct channel_key) and
37 * the value is of type (struct channel_info *).
39 struct cds_lfht
*channel_infos_ht
;
40 struct lttng_session_trigger_list
*trigger_list
;
41 /* Node in the notification thread state's sessions_ht. */
42 struct cds_lfht_node sessions_ht_node
;
44 * Weak reference to the thread state's sessions_ht. Used for removal on
47 struct cds_lfht
*sessions_ht
;
48 uint64_t consumed_data_size
;
50 /* Whether a rotation is ongoing for this session. */
52 /* Identifier of the currently ongoing rotation. */
55 /* call_rcu delayed reclaim. */
56 struct rcu_head rcu_node
;
60 struct channel_key key
;
64 * A channel info holds a reference (lttng_ref) on session_info.
65 * session_info, in return, holds a weak reference to the channel.
67 struct session_info
*session_info
;
68 /* Node in the notification thread state's channels_ht. */
69 struct cds_lfht_node channels_ht_node
;
70 /* Node in the session_info's channels_ht. */
71 struct cds_lfht_node session_info_channels_ht_node
;
72 /* call_rcu delayed reclaim. */
73 struct rcu_head rcu_node
;
77 * Facilities to carry the different notifications type in the action
78 * processing code path.
80 struct lttng_trigger_notification
{
82 enum lttng_domain_type type
;
83 size_t capture_buf_size
;
87 struct notification_client_list_element
{
88 struct notification_client
*client
;
89 struct cds_list_head node
;
93 * Thread safety of notification_client and notification_client_list.
95 * The notification thread (main thread) and the action executor
96 * interact through client lists. Hence, when the action executor
97 * thread looks-up the list of clients subscribed to a given
98 * condition, it will acquire a reference to the list and lock it
99 * while attempting to communicate with the various clients.
101 * It is not necessary to reference-count clients as they are guaranteed
102 * to be 'alive' if they are present in a list and that list is locked. Indeed,
103 * removing references to the client from those subscription lists is part of
104 * the work performed on destruction of a client.
106 * No provision for other access scenarios are taken into account;
107 * this is the bare minimum to make these accesses safe and the
108 * notification thread's state is _not_ "thread-safe" in any general
111 struct notification_client_list
{
112 pthread_mutex_t lock
;
114 const struct lttng_trigger
*trigger
;
115 struct cds_list_head list
;
116 /* Weak reference to container. */
117 struct cds_lfht
*notification_trigger_clients_ht
;
118 struct cds_lfht_node notification_trigger_clients_ht_node
;
119 /* call_rcu delayed reclaim. */
120 struct rcu_head rcu_node
;
123 struct notification_client
{
124 /* Nests within the notification_client_list lock. */
125 pthread_mutex_t lock
;
126 notification_client_id id
;
128 /* Client protocol version. */
129 uint8_t major
, minor
;
133 * Indicates if the credentials and versions of the client have been
138 * Conditions to which the client's notification channel is subscribed.
139 * List of struct lttng_condition_list_node. The condition member is
140 * owned by the client.
142 struct cds_list_head condition_list
;
143 struct cds_lfht_node client_socket_ht_node
;
144 struct cds_lfht_node client_id_ht_node
;
147 * If a client's communication is inactive, it means that a
148 * fatal error has occurred (could be either a protocol error or
149 * the socket API returned a fatal error). No further
150 * communication should be attempted; the client is queued for
156 * During the reception of a message, the reception
157 * buffers' "size" is set to contain the current
158 * message's complete payload.
160 struct lttng_dynamic_buffer buffer
;
161 /* Bytes left to receive for the current message. */
162 size_t bytes_to_receive
;
163 /* Type of the message being received. */
164 enum lttng_notification_channel_message_type msg_type
;
166 * Indicates whether or not credentials are expected
171 * Indicates whether or not credentials were received
175 /* Only used during credentials reception. */
176 lttng_sock_cred creds
;
180 * Indicates whether or not a notification addressed to
181 * this client was dropped because a command reply was
184 * A notification is dropped whenever the buffer is not
187 bool dropped_notification
;
189 * Indicates whether or not a command reply is already
190 * buffered. In this case, it means that the client is
191 * not consuming command replies before emitting a new
192 * one. This could be caused by a protocol error or a
193 * misbehaving/malicious client.
195 bool queued_command_reply
;
196 struct lttng_dynamic_buffer buffer
;
199 /* call_rcu delayed reclaim. */
200 struct rcu_head rcu_node
;
203 enum client_transmission_status
{
204 CLIENT_TRANSMISSION_STATUS_COMPLETE
,
205 CLIENT_TRANSMISSION_STATUS_QUEUED
,
206 /* Communication failure. */
207 CLIENT_TRANSMISSION_STATUS_FAIL
,
209 CLIENT_TRANSMISSION_STATUS_ERROR
,
213 bool notification_client_list_get(struct notification_client_list
*list
);
216 void notification_client_list_put(struct notification_client_list
*list
);
218 typedef int (*report_client_transmission_result_cb
)(
219 struct notification_client
*client
,
220 enum client_transmission_status status
,
224 int notification_client_list_send_evaluation(
225 struct notification_client_list
*list
,
226 const struct lttng_condition
*condition
,
227 const struct lttng_evaluation
*evaluation
,
228 const struct lttng_credentials
*trigger_creds
,
229 const struct lttng_credentials
*source_object_creds
,
230 report_client_transmission_result_cb client_report
,
234 int notification_thread_client_communication_update(
235 struct notification_thread_handle
*handle
,
236 notification_client_id id
,
237 enum client_transmission_status transmission_status
);
239 * Takes ownership of the payload if present.
242 struct lttng_trigger_notification
*lttng_trigger_notification_create(
244 enum lttng_domain_type domain
,
246 size_t payload_size
);
249 void lttng_trigger_notification_destroy(
250 struct lttng_trigger_notification
*trigger_notification
);
252 #endif /* NOTIFICATION_THREAD_INTERNAL_H */