relayd: do not prioritize control events over data.
[lttng-tools.git] / src / bin / lttng-relayd / connection.c
CommitLineData
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 28bool connection_get(struct relay_connection *conn)
58eb9381 29{
7591bab1 30 bool has_ref = false;
58eb9381 31
7591bab1
MD
32 pthread_mutex_lock(&conn->reflock);
33 if (conn->ref.refcount != 0) {
34 has_ref = true;
35 urcu_ref_get(&conn->ref);
36 }
37 pthread_mutex_unlock(&conn->reflock);
38
39 return has_ref;
58eb9381
DG
40}
41
7591bab1
MD
42struct relay_connection *connection_get_by_sock(struct lttng_ht *relay_connections_ht,
43 int sock)
58eb9381
DG
44{
45 struct lttng_ht_node_ulong *node;
46 struct lttng_ht_iter iter;
47 struct relay_connection *conn = NULL;
48
58eb9381
DG
49 assert(sock >= 0);
50
7591bab1
MD
51 rcu_read_lock();
52 lttng_ht_lookup(relay_connections_ht, (void *)((unsigned long) sock),
53 &iter);
58eb9381
DG
54 node = lttng_ht_iter_get_node_ulong(&iter);
55 if (!node) {
56 DBG2("Relay connection by sock %d not found", sock);
57 goto end;
58 }
59 conn = caa_container_of(node, struct relay_connection, sock_n);
7591bab1
MD
60 if (!connection_get(conn)) {
61 conn = NULL;
62 }
58eb9381 63end:
7591bab1 64 rcu_read_unlock();
58eb9381
DG
65 return conn;
66}
67
44a2fbf1
JG
68int connection_reset_protocol_state(struct relay_connection *connection)
69{
70 int ret = 0;
71
72 switch (connection->type) {
73 case RELAY_DATA:
74 connection->protocol.data.state_id =
75 DATA_CONNECTION_STATE_RECEIVE_HEADER;
76 memset(&connection->protocol.data.state.receive_header,
77 0,
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);
81 break;
82 case RELAY_CONTROL:
83 connection->protocol.ctrl.state_id =
84 CTRL_CONNECTION_STATE_RECEIVE_HEADER;
85 memset(&connection->protocol.ctrl.state.receive_header,
86 0,
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));
93 if (ret) {
94 ERR("Failed to reinitialize control connection reception buffer size to %zu bytes.", sizeof(struct lttcomm_relayd_hdr));
95 goto end;
96 }
97 break;
98 default:
99 goto end;
100 }
101 DBG("Reset communication state of relay connection (fd = %i)",
102 connection->sock->fd);
103end:
104 return ret;
105}
106
7591bab1
MD
107struct relay_connection *connection_create(struct lttcomm_sock *sock,
108 enum connection_type type)
58eb9381 109{
7591bab1 110 struct relay_connection *conn;
58eb9381 111
7591bab1
MD
112 conn = zmalloc(sizeof(*conn));
113 if (!conn) {
114 PERROR("zmalloc relay connection");
115 goto end;
116 }
117 pthread_mutex_init(&conn->reflock, NULL);
118 urcu_ref_init(&conn->ref);
119 conn->type = type;
120 conn->sock = sock;
121 lttng_ht_node_init_ulong(&conn->sock_n, (unsigned long) conn->sock->fd);
44a2fbf1
JG
122 if (conn->type == RELAY_CONTROL) {
123 lttng_dynamic_buffer_init(&conn->protocol.ctrl.reception_buffer);
124 }
d9b9088d 125 conn->activity_phase = 0;
44a2fbf1 126 connection_reset_protocol_state(conn);
7591bab1
MD
127end:
128 return conn;
58eb9381
DG
129}
130
7591bab1 131static void rcu_free_connection(struct rcu_head *head)
58eb9381 132{
7591bab1
MD
133 struct relay_connection *conn =
134 caa_container_of(head, struct relay_connection, rcu_node);
58eb9381 135
7591bab1
MD
136 lttcomm_destroy_sock(conn->sock);
137 if (conn->viewer_session) {
138 viewer_session_destroy(conn->viewer_session);
139 conn->viewer_session = NULL;
140 }
141 free(conn);
142}
143
144static void destroy_connection(struct relay_connection *conn)
145{
58eb9381
DG
146 call_rcu(&conn->rcu_node, rcu_free_connection);
147}
148
7591bab1 149static void connection_release(struct urcu_ref *ref)
58eb9381 150{
7591bab1
MD
151 struct relay_connection *conn =
152 caa_container_of(ref, struct relay_connection, ref);
58eb9381 153
7591bab1
MD
154 if (conn->in_socket_ht) {
155 struct lttng_ht_iter iter;
156 int ret;
157
158 iter.iter.node = &conn->sock_n.node;
159 ret = lttng_ht_del(conn->socket_ht, &iter);
160 assert(!ret);
58eb9381
DG
161 }
162
7591bab1
MD
163 if (conn->session) {
164 if (session_close(conn->session)) {
165 ERR("session_close");
166 }
167 conn->session = NULL;
168 }
169 if (conn->viewer_session) {
170 viewer_session_close(conn->viewer_session);
171 }
172 destroy_connection(conn);
58eb9381
DG
173}
174
7591bab1 175void connection_put(struct relay_connection *conn)
58eb9381 176{
7591bab1
MD
177 rcu_read_lock();
178 pthread_mutex_lock(&conn->reflock);
179 urcu_ref_put(&conn->ref, connection_release);
180 pthread_mutex_unlock(&conn->reflock);
181 rcu_read_unlock();
58eb9381
DG
182}
183
7591bab1
MD
184void connection_ht_add(struct lttng_ht *relay_connections_ht,
185 struct relay_connection *conn)
58eb9381 186{
7591bab1
MD
187 assert(!conn->in_socket_ht);
188 lttng_ht_add_unique_ulong(relay_connections_ht, &conn->sock_n);
189 conn->in_socket_ht = 1;
190 conn->socket_ht = relay_connections_ht;
58eb9381 191}
44a2fbf1
JG
192
193int connection_set_session(struct relay_connection *conn,
194 struct relay_session *session)
195{
196 int ret = 0;
197
198 assert(conn);
199 assert(session);
200 assert(!conn->session);
201
202 if (connection_get(conn)) {
203 if (session_get(session)) {
204 conn->session = session;
205 } else {
206 ERR("Failed to get session reference in connection_set_session()");
207 ret = -1;
208 }
209 connection_put(conn);
210 } else {
211 ERR("Failed to get connection reference in connection_set_session()");
212 ret = -1;
213 }
214 return ret;
215}
This page took 0.050269 seconds and 5 git commands to generate.