2 * Copyright (C) 2013 - Julien Desfossez <jdesfossez@efficios.com>
3 * David Goulet <dgoulet@efficios.com>
4 * 2015 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License, version 2 only, as
8 * published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc., 51
17 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 #include <common/common.h>
22 #include <urcu/rculist.h>
24 #include "connection.h"
26 #include "viewer-session.h"
28 bool connection_get(struct relay_connection
*conn
)
32 pthread_mutex_lock(&conn
->reflock
);
33 if (conn
->ref
.refcount
!= 0) {
35 urcu_ref_get(&conn
->ref
);
37 pthread_mutex_unlock(&conn
->reflock
);
42 struct relay_connection
*connection_get_by_sock(struct lttng_ht
*relay_connections_ht
,
45 struct lttng_ht_node_ulong
*node
;
46 struct lttng_ht_iter iter
;
47 struct relay_connection
*conn
= NULL
;
52 lttng_ht_lookup(relay_connections_ht
, (void *)((unsigned long) sock
),
54 node
= lttng_ht_iter_get_node_ulong(&iter
);
56 DBG2("Relay connection by sock %d not found", sock
);
59 conn
= caa_container_of(node
, struct relay_connection
, sock_n
);
60 if (!connection_get(conn
)) {
68 int connection_reset_protocol_state(struct relay_connection
*connection
)
72 switch (connection
->type
) {
74 connection
->protocol
.data
.state_id
=
75 DATA_CONNECTION_STATE_RECEIVE_HEADER
;
76 memset(&connection
->protocol
.data
.state
.receive_header
,
78 sizeof(connection
->protocol
.data
.state
.receive_header
));
79 connection
->protocol
.data
.state
.receive_header
.left_to_receive
=
80 sizeof(struct lttcomm_relayd_data_hdr
);
83 connection
->protocol
.ctrl
.state_id
=
84 CTRL_CONNECTION_STATE_RECEIVE_HEADER
;
85 memset(&connection
->protocol
.ctrl
.state
.receive_header
,
87 sizeof(connection
->protocol
.ctrl
.state
.receive_header
));
88 connection
->protocol
.data
.state
.receive_header
.left_to_receive
=
89 sizeof(struct lttcomm_relayd_hdr
);
90 ret
= lttng_dynamic_buffer_set_size(
91 &connection
->protocol
.ctrl
.reception_buffer
,
92 sizeof(struct lttcomm_relayd_hdr
));
94 ERR("Failed to reinitialize control connection reception buffer size to %zu bytes.", sizeof(struct lttcomm_relayd_hdr
));
101 DBG("Reset communication state of relay connection (fd = %i)",
102 connection
->sock
->fd
);
107 struct relay_connection
*connection_create(struct lttcomm_sock
*sock
,
108 enum connection_type type
)
110 struct relay_connection
*conn
;
112 conn
= zmalloc(sizeof(*conn
));
114 PERROR("zmalloc relay connection");
117 pthread_mutex_init(&conn
->reflock
, NULL
);
118 urcu_ref_init(&conn
->ref
);
121 lttng_ht_node_init_ulong(&conn
->sock_n
, (unsigned long) conn
->sock
->fd
);
122 if (conn
->type
== RELAY_CONTROL
) {
123 lttng_dynamic_buffer_init(&conn
->protocol
.ctrl
.reception_buffer
);
125 connection_reset_protocol_state(conn
);
130 static void rcu_free_connection(struct rcu_head
*head
)
132 struct relay_connection
*conn
=
133 caa_container_of(head
, struct relay_connection
, rcu_node
);
135 lttcomm_destroy_sock(conn
->sock
);
136 if (conn
->viewer_session
) {
137 viewer_session_destroy(conn
->viewer_session
);
138 conn
->viewer_session
= NULL
;
143 static void destroy_connection(struct relay_connection
*conn
)
145 call_rcu(&conn
->rcu_node
, rcu_free_connection
);
148 static void connection_release(struct urcu_ref
*ref
)
150 struct relay_connection
*conn
=
151 caa_container_of(ref
, struct relay_connection
, ref
);
153 if (conn
->in_socket_ht
) {
154 struct lttng_ht_iter iter
;
157 iter
.iter
.node
= &conn
->sock_n
.node
;
158 ret
= lttng_ht_del(conn
->socket_ht
, &iter
);
163 if (session_close(conn
->session
)) {
164 ERR("session_close");
166 conn
->session
= NULL
;
168 if (conn
->viewer_session
) {
169 viewer_session_close(conn
->viewer_session
);
171 destroy_connection(conn
);
174 void connection_put(struct relay_connection
*conn
)
177 pthread_mutex_lock(&conn
->reflock
);
178 urcu_ref_put(&conn
->ref
, connection_release
);
179 pthread_mutex_unlock(&conn
->reflock
);
183 void connection_ht_add(struct lttng_ht
*relay_connections_ht
,
184 struct relay_connection
*conn
)
186 assert(!conn
->in_socket_ht
);
187 lttng_ht_add_unique_ulong(relay_connections_ht
, &conn
->sock_n
);
188 conn
->in_socket_ht
= 1;
189 conn
->socket_ht
= relay_connections_ht
;
192 int connection_set_session(struct relay_connection
*conn
,
193 struct relay_session
*session
)
199 assert(!conn
->session
);
201 if (connection_get(conn
)) {
202 if (session_get(session
)) {
203 conn
->session
= session
;
205 ERR("Failed to get session reference in connection_set_session()");
208 connection_put(conn
);
210 ERR("Failed to get connection reference in connection_set_session()");