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
)
30 return urcu_ref_get_unless_zero(&conn
->ref
);
33 struct relay_connection
*connection_get_by_sock(struct lttng_ht
*relay_connections_ht
,
36 struct lttng_ht_node_ulong
*node
;
37 struct lttng_ht_iter iter
;
38 struct relay_connection
*conn
= NULL
;
43 lttng_ht_lookup(relay_connections_ht
, (void *)((unsigned long) sock
),
45 node
= lttng_ht_iter_get_node_ulong(&iter
);
47 DBG2("Relay connection by sock %d not found", sock
);
50 conn
= caa_container_of(node
, struct relay_connection
, sock_n
);
51 if (!connection_get(conn
)) {
59 int connection_reset_protocol_state(struct relay_connection
*connection
)
63 switch (connection
->type
) {
65 connection
->protocol
.data
.state_id
=
66 DATA_CONNECTION_STATE_RECEIVE_HEADER
;
67 memset(&connection
->protocol
.data
.state
.receive_header
,
69 sizeof(connection
->protocol
.data
.state
.receive_header
));
70 connection
->protocol
.data
.state
.receive_header
.left_to_receive
=
71 sizeof(struct lttcomm_relayd_data_hdr
);
74 connection
->protocol
.ctrl
.state_id
=
75 CTRL_CONNECTION_STATE_RECEIVE_HEADER
;
76 memset(&connection
->protocol
.ctrl
.state
.receive_header
,
78 sizeof(connection
->protocol
.ctrl
.state
.receive_header
));
79 connection
->protocol
.data
.state
.receive_header
.left_to_receive
=
80 sizeof(struct lttcomm_relayd_hdr
);
81 ret
= lttng_dynamic_buffer_set_size(
82 &connection
->protocol
.ctrl
.reception_buffer
,
83 sizeof(struct lttcomm_relayd_hdr
));
85 ERR("Failed to reinitialize control connection reception buffer size to %zu bytes.", sizeof(struct lttcomm_relayd_hdr
));
92 DBG("Reset communication state of relay connection (fd = %i)",
93 connection
->sock
->fd
);
98 struct relay_connection
*connection_create(struct lttcomm_sock
*sock
,
99 enum connection_type type
)
101 struct relay_connection
*conn
;
103 conn
= zmalloc(sizeof(*conn
));
105 PERROR("zmalloc relay connection");
108 urcu_ref_init(&conn
->ref
);
111 lttng_ht_node_init_ulong(&conn
->sock_n
, (unsigned long) conn
->sock
->fd
);
112 if (conn
->type
== RELAY_CONTROL
) {
113 lttng_dynamic_buffer_init(&conn
->protocol
.ctrl
.reception_buffer
);
115 connection_reset_protocol_state(conn
);
120 static void rcu_free_connection(struct rcu_head
*head
)
122 struct relay_connection
*conn
=
123 caa_container_of(head
, struct relay_connection
, rcu_node
);
125 lttcomm_destroy_sock(conn
->sock
);
126 if (conn
->viewer_session
) {
127 viewer_session_destroy(conn
->viewer_session
);
128 conn
->viewer_session
= NULL
;
133 static void destroy_connection(struct relay_connection
*conn
)
135 call_rcu(&conn
->rcu_node
, rcu_free_connection
);
138 static void connection_release(struct urcu_ref
*ref
)
140 struct relay_connection
*conn
=
141 caa_container_of(ref
, struct relay_connection
, ref
);
143 if (conn
->in_socket_ht
) {
144 struct lttng_ht_iter iter
;
147 iter
.iter
.node
= &conn
->sock_n
.node
;
148 ret
= lttng_ht_del(conn
->socket_ht
, &iter
);
153 if (session_close(conn
->session
)) {
154 ERR("session_close");
156 conn
->session
= NULL
;
158 if (conn
->viewer_session
) {
159 viewer_session_close(conn
->viewer_session
);
161 destroy_connection(conn
);
164 void connection_put(struct relay_connection
*conn
)
167 urcu_ref_put(&conn
->ref
, connection_release
);
171 void connection_ht_add(struct lttng_ht
*relay_connections_ht
,
172 struct relay_connection
*conn
)
174 assert(!conn
->in_socket_ht
);
175 lttng_ht_add_unique_ulong(relay_connections_ht
, &conn
->sock_n
);
176 conn
->in_socket_ht
= 1;
177 conn
->socket_ht
= relay_connections_ht
;
180 int connection_set_session(struct relay_connection
*conn
,
181 struct relay_session
*session
)
187 assert(!conn
->session
);
189 if (connection_get(conn
)) {
190 if (session_get(session
)) {
191 conn
->session
= session
;
193 ERR("Failed to get session reference in connection_set_session()");
196 connection_put(conn
);
198 ERR("Failed to get connection reference in connection_set_session()");