2 * Copyright (C) 2013 Julien Desfossez <jdesfossez@efficios.com>
3 * Copyright (C) 2013 David Goulet <dgoulet@efficios.com>
4 * Copyright (C) 2015 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
6 * SPDX-License-Identifier: GPL-2.0-only
11 #include <common/common.h>
12 #include <urcu/rculist.h>
14 #include "connection.h"
16 #include "viewer-session.h"
18 bool connection_get(struct relay_connection
*conn
)
20 return urcu_ref_get_unless_zero(&conn
->ref
);
23 struct relay_connection
*connection_get_by_sock(struct lttng_ht
*relay_connections_ht
,
26 struct lttng_ht_node_ulong
*node
;
27 struct lttng_ht_iter iter
;
28 struct relay_connection
*conn
= NULL
;
33 lttng_ht_lookup(relay_connections_ht
, (void *)((unsigned long) sock
),
35 node
= lttng_ht_iter_get_node_ulong(&iter
);
37 DBG2("Relay connection by sock %d not found", sock
);
40 conn
= caa_container_of(node
, struct relay_connection
, sock_n
);
41 if (!connection_get(conn
)) {
49 int connection_reset_protocol_state(struct relay_connection
*connection
)
53 switch (connection
->type
) {
55 connection
->protocol
.data
.state_id
=
56 DATA_CONNECTION_STATE_RECEIVE_HEADER
;
57 memset(&connection
->protocol
.data
.state
.receive_header
,
59 sizeof(connection
->protocol
.data
.state
.receive_header
));
60 connection
->protocol
.data
.state
.receive_header
.left_to_receive
=
61 sizeof(struct lttcomm_relayd_data_hdr
);
64 connection
->protocol
.ctrl
.state_id
=
65 CTRL_CONNECTION_STATE_RECEIVE_HEADER
;
66 memset(&connection
->protocol
.ctrl
.state
.receive_header
,
68 sizeof(connection
->protocol
.ctrl
.state
.receive_header
));
69 connection
->protocol
.data
.state
.receive_header
.left_to_receive
=
70 sizeof(struct lttcomm_relayd_hdr
);
71 ret
= lttng_dynamic_buffer_set_size(
72 &connection
->protocol
.ctrl
.reception_buffer
,
73 sizeof(struct lttcomm_relayd_hdr
));
75 ERR("Failed to reinitialize control connection reception buffer size to %zu bytes.", sizeof(struct lttcomm_relayd_hdr
));
82 DBG("Reset communication state of relay connection (fd = %i)",
83 connection
->sock
->fd
);
88 struct relay_connection
*connection_create(struct lttcomm_sock
*sock
,
89 enum connection_type type
)
91 struct relay_connection
*conn
;
93 conn
= zmalloc(sizeof(*conn
));
95 PERROR("zmalloc relay connection");
98 urcu_ref_init(&conn
->ref
);
101 lttng_ht_node_init_ulong(&conn
->sock_n
, (unsigned long) conn
->sock
->fd
);
102 if (conn
->type
== RELAY_CONTROL
) {
103 lttng_dynamic_buffer_init(&conn
->protocol
.ctrl
.reception_buffer
);
105 connection_reset_protocol_state(conn
);
110 static void rcu_free_connection(struct rcu_head
*head
)
112 struct relay_connection
*conn
=
113 caa_container_of(head
, struct relay_connection
, rcu_node
);
115 lttcomm_destroy_sock(conn
->sock
);
116 if (conn
->viewer_session
) {
117 viewer_session_destroy(conn
->viewer_session
);
118 conn
->viewer_session
= NULL
;
120 if (conn
->type
== RELAY_CONTROL
) {
121 lttng_dynamic_buffer_reset(
122 &conn
->protocol
.ctrl
.reception_buffer
);
127 static void destroy_connection(struct relay_connection
*conn
)
129 call_rcu(&conn
->rcu_node
, rcu_free_connection
);
132 static void connection_release(struct urcu_ref
*ref
)
134 struct relay_connection
*conn
=
135 caa_container_of(ref
, struct relay_connection
, ref
);
137 if (conn
->in_socket_ht
) {
138 struct lttng_ht_iter iter
;
141 iter
.iter
.node
= &conn
->sock_n
.node
;
142 ret
= lttng_ht_del(conn
->socket_ht
, &iter
);
147 if (session_close(conn
->session
)) {
148 ERR("session_close");
150 conn
->session
= NULL
;
152 if (conn
->viewer_session
) {
153 viewer_session_close(conn
->viewer_session
);
155 destroy_connection(conn
);
158 void connection_put(struct relay_connection
*conn
)
161 urcu_ref_put(&conn
->ref
, connection_release
);
165 void connection_ht_add(struct lttng_ht
*relay_connections_ht
,
166 struct relay_connection
*conn
)
168 assert(!conn
->in_socket_ht
);
169 lttng_ht_add_unique_ulong(relay_connections_ht
, &conn
->sock_n
);
170 conn
->in_socket_ht
= 1;
171 conn
->socket_ht
= relay_connections_ht
;
174 int connection_set_session(struct relay_connection
*conn
,
175 struct relay_session
*session
)
181 assert(!conn
->session
);
183 if (connection_get(conn
)) {
184 if (session_get(session
)) {
185 conn
->session
= session
;
187 ERR("Failed to get session reference in connection_set_session()");
190 connection_put(conn
);
192 ERR("Failed to get connection reference in connection_set_session()");