Commit | Line | Data |
---|---|---|
58eb9381 DG |
1 | /* |
2 | * Copyright (C) 2013 - Julien Desfossez <jdesfossez@efficios.com> | |
3 | * David Goulet <dgoulet@efficios.com> | |
7591bab1 | 4 | * 2015 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
58eb9381 DG |
5 | * |
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. | |
9 | * | |
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 | |
13 | * more details. | |
14 | * | |
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. | |
18 | */ | |
19 | ||
6c1c0768 | 20 | #define _LGPL_SOURCE |
58eb9381 | 21 | #include <common/common.h> |
7591bab1 | 22 | #include <urcu/rculist.h> |
58eb9381 DG |
23 | |
24 | #include "connection.h" | |
25 | #include "stream.h" | |
7591bab1 | 26 | #include "viewer-session.h" |
58eb9381 | 27 | |
7591bab1 | 28 | bool connection_get(struct relay_connection *conn) |
58eb9381 | 29 | { |
ce4d4083 | 30 | return urcu_ref_get_unless_zero(&conn->ref); |
58eb9381 DG |
31 | } |
32 | ||
7591bab1 MD |
33 | struct relay_connection *connection_get_by_sock(struct lttng_ht *relay_connections_ht, |
34 | int sock) | |
58eb9381 DG |
35 | { |
36 | struct lttng_ht_node_ulong *node; | |
37 | struct lttng_ht_iter iter; | |
38 | struct relay_connection *conn = NULL; | |
39 | ||
58eb9381 DG |
40 | assert(sock >= 0); |
41 | ||
7591bab1 MD |
42 | rcu_read_lock(); |
43 | lttng_ht_lookup(relay_connections_ht, (void *)((unsigned long) sock), | |
44 | &iter); | |
58eb9381 DG |
45 | node = lttng_ht_iter_get_node_ulong(&iter); |
46 | if (!node) { | |
47 | DBG2("Relay connection by sock %d not found", sock); | |
48 | goto end; | |
49 | } | |
50 | conn = caa_container_of(node, struct relay_connection, sock_n); | |
7591bab1 MD |
51 | if (!connection_get(conn)) { |
52 | conn = NULL; | |
53 | } | |
58eb9381 | 54 | end: |
7591bab1 | 55 | rcu_read_unlock(); |
58eb9381 DG |
56 | return conn; |
57 | } | |
58 | ||
5312a3ed JG |
59 | int connection_reset_protocol_state(struct relay_connection *connection) |
60 | { | |
61 | int ret = 0; | |
62 | ||
63 | switch (connection->type) { | |
64 | case RELAY_DATA: | |
65 | connection->protocol.data.state_id = | |
66 | DATA_CONNECTION_STATE_RECEIVE_HEADER; | |
67 | memset(&connection->protocol.data.state.receive_header, | |
68 | 0, | |
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); | |
72 | break; | |
73 | case RELAY_CONTROL: | |
74 | connection->protocol.ctrl.state_id = | |
75 | CTRL_CONNECTION_STATE_RECEIVE_HEADER; | |
76 | memset(&connection->protocol.ctrl.state.receive_header, | |
77 | 0, | |
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)); | |
84 | if (ret) { | |
85 | ERR("Failed to reinitialize control connection reception buffer size to %zu bytes.", sizeof(struct lttcomm_relayd_hdr)); | |
86 | goto end; | |
87 | } | |
88 | break; | |
89 | default: | |
90 | goto end; | |
91 | } | |
92 | DBG("Reset communication state of relay connection (fd = %i)", | |
93 | connection->sock->fd); | |
94 | end: | |
95 | return ret; | |
96 | } | |
97 | ||
7591bab1 MD |
98 | struct relay_connection *connection_create(struct lttcomm_sock *sock, |
99 | enum connection_type type) | |
58eb9381 | 100 | { |
7591bab1 | 101 | struct relay_connection *conn; |
58eb9381 | 102 | |
7591bab1 MD |
103 | conn = zmalloc(sizeof(*conn)); |
104 | if (!conn) { | |
105 | PERROR("zmalloc relay connection"); | |
106 | goto end; | |
107 | } | |
7591bab1 MD |
108 | urcu_ref_init(&conn->ref); |
109 | conn->type = type; | |
110 | conn->sock = sock; | |
111 | lttng_ht_node_init_ulong(&conn->sock_n, (unsigned long) conn->sock->fd); | |
5312a3ed JG |
112 | if (conn->type == RELAY_CONTROL) { |
113 | lttng_dynamic_buffer_init(&conn->protocol.ctrl.reception_buffer); | |
114 | } | |
115 | connection_reset_protocol_state(conn); | |
7591bab1 MD |
116 | end: |
117 | return conn; | |
58eb9381 DG |
118 | } |
119 | ||
7591bab1 | 120 | static void rcu_free_connection(struct rcu_head *head) |
58eb9381 | 121 | { |
7591bab1 MD |
122 | struct relay_connection *conn = |
123 | caa_container_of(head, struct relay_connection, rcu_node); | |
58eb9381 | 124 | |
7591bab1 MD |
125 | lttcomm_destroy_sock(conn->sock); |
126 | if (conn->viewer_session) { | |
127 | viewer_session_destroy(conn->viewer_session); | |
128 | conn->viewer_session = NULL; | |
129 | } | |
35be817a JG |
130 | if (conn->type == RELAY_CONTROL) { |
131 | lttng_dynamic_buffer_reset( | |
132 | &conn->protocol.ctrl.reception_buffer); | |
133 | } | |
7591bab1 MD |
134 | free(conn); |
135 | } | |
136 | ||
137 | static void destroy_connection(struct relay_connection *conn) | |
138 | { | |
58eb9381 DG |
139 | call_rcu(&conn->rcu_node, rcu_free_connection); |
140 | } | |
141 | ||
7591bab1 | 142 | static void connection_release(struct urcu_ref *ref) |
58eb9381 | 143 | { |
7591bab1 MD |
144 | struct relay_connection *conn = |
145 | caa_container_of(ref, struct relay_connection, ref); | |
58eb9381 | 146 | |
7591bab1 MD |
147 | if (conn->in_socket_ht) { |
148 | struct lttng_ht_iter iter; | |
149 | int ret; | |
150 | ||
151 | iter.iter.node = &conn->sock_n.node; | |
152 | ret = lttng_ht_del(conn->socket_ht, &iter); | |
153 | assert(!ret); | |
58eb9381 DG |
154 | } |
155 | ||
7591bab1 MD |
156 | if (conn->session) { |
157 | if (session_close(conn->session)) { | |
158 | ERR("session_close"); | |
159 | } | |
160 | conn->session = NULL; | |
161 | } | |
162 | if (conn->viewer_session) { | |
163 | viewer_session_close(conn->viewer_session); | |
164 | } | |
165 | destroy_connection(conn); | |
58eb9381 DG |
166 | } |
167 | ||
7591bab1 | 168 | void connection_put(struct relay_connection *conn) |
58eb9381 | 169 | { |
7591bab1 | 170 | rcu_read_lock(); |
7591bab1 | 171 | urcu_ref_put(&conn->ref, connection_release); |
7591bab1 | 172 | rcu_read_unlock(); |
58eb9381 DG |
173 | } |
174 | ||
7591bab1 MD |
175 | void connection_ht_add(struct lttng_ht *relay_connections_ht, |
176 | struct relay_connection *conn) | |
58eb9381 | 177 | { |
7591bab1 MD |
178 | assert(!conn->in_socket_ht); |
179 | lttng_ht_add_unique_ulong(relay_connections_ht, &conn->sock_n); | |
180 | conn->in_socket_ht = 1; | |
181 | conn->socket_ht = relay_connections_ht; | |
58eb9381 | 182 | } |
fd0f1e3e JR |
183 | |
184 | int connection_set_session(struct relay_connection *conn, | |
185 | struct relay_session *session) | |
186 | { | |
187 | int ret = 0; | |
188 | ||
189 | assert(conn); | |
190 | assert(session); | |
191 | assert(!conn->session); | |
192 | ||
193 | if (connection_get(conn)) { | |
194 | if (session_get(session)) { | |
195 | conn->session = session; | |
196 | } else { | |
197 | ERR("Failed to get session reference in connection_set_session()"); | |
198 | ret = -1; | |
199 | } | |
200 | connection_put(conn); | |
201 | } else { | |
202 | ERR("Failed to get connection reference in connection_set_session()"); | |
203 | ret = -1; | |
204 | } | |
205 | return ret; | |
206 | } |