| 1 | #ifndef _CONNECTION_H |
| 2 | #define _CONNECTION_H |
| 3 | |
| 4 | /* |
| 5 | * Copyright (C) 2013 - Julien Desfossez <jdesfossez@efficios.com> |
| 6 | * David Goulet <dgoulet@efficios.com> |
| 7 | * 2015 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify it |
| 10 | * under the terms of the GNU General Public License, version 2 only, as |
| 11 | * published by the Free Software Foundation. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, but WITHOUT |
| 14 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 15 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 16 | * more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License along with |
| 19 | * this program; if not, write to the Free Software Foundation, Inc., 51 |
| 20 | * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 21 | */ |
| 22 | |
| 23 | #include <limits.h> |
| 24 | #include <inttypes.h> |
| 25 | #include <pthread.h> |
| 26 | #include <urcu.h> |
| 27 | #include <urcu/wfcqueue.h> |
| 28 | #include <urcu/list.h> |
| 29 | |
| 30 | #include <common/hashtable/hashtable.h> |
| 31 | #include <common/sessiond-comm/sessiond-comm.h> |
| 32 | #include <common/sessiond-comm/relayd.h> |
| 33 | #include <common/dynamic-buffer.h> |
| 34 | |
| 35 | #include "session.h" |
| 36 | |
| 37 | enum connection_type { |
| 38 | RELAY_CONNECTION_UNKNOWN = 0, |
| 39 | RELAY_DATA = 1, |
| 40 | RELAY_CONTROL = 2, |
| 41 | RELAY_VIEWER_COMMAND = 3, |
| 42 | RELAY_VIEWER_NOTIFICATION = 4, |
| 43 | }; |
| 44 | |
| 45 | enum data_connection_state { |
| 46 | DATA_CONNECTION_STATE_RECEIVE_HEADER = 0, |
| 47 | DATA_CONNECTION_STATE_RECEIVE_PAYLOAD = 1, |
| 48 | }; |
| 49 | |
| 50 | enum ctrl_connection_state { |
| 51 | CTRL_CONNECTION_STATE_RECEIVE_HEADER = 0, |
| 52 | CTRL_CONNECTION_STATE_RECEIVE_PAYLOAD = 1, |
| 53 | }; |
| 54 | |
| 55 | struct data_connection_state_receive_header { |
| 56 | uint64_t received, left_to_receive; |
| 57 | char header_reception_buffer[sizeof(struct lttcomm_relayd_data_hdr)]; |
| 58 | }; |
| 59 | |
| 60 | struct data_connection_state_receive_payload { |
| 61 | uint64_t received, left_to_receive; |
| 62 | struct lttcomm_relayd_data_hdr header; |
| 63 | bool rotate_index; |
| 64 | }; |
| 65 | |
| 66 | struct ctrl_connection_state_receive_header { |
| 67 | uint64_t received, left_to_receive; |
| 68 | }; |
| 69 | |
| 70 | struct ctrl_connection_state_receive_payload { |
| 71 | uint64_t received, left_to_receive; |
| 72 | struct lttcomm_relayd_hdr header; |
| 73 | }; |
| 74 | |
| 75 | /* |
| 76 | * Internal structure to map a socket with the corresponding session. |
| 77 | * A hashtable indexed on the socket FD is used for the lookups. |
| 78 | * |
| 79 | * Connections are assumed to be accessed from a single thread. Live |
| 80 | * connections between the relay and a live client are only accessed |
| 81 | * from the live worker thread. |
| 82 | * |
| 83 | * The connections between the consumerd/sessiond and the relayd are only |
| 84 | * handled by the "main" worker thread (as in, the worker thread in main.c). |
| 85 | * |
| 86 | * This is why there are no back references to connections from the |
| 87 | * sessions and session list. |
| 88 | */ |
| 89 | struct relay_connection { |
| 90 | struct lttcomm_sock *sock; |
| 91 | struct cds_wfcq_node qnode; |
| 92 | |
| 93 | enum connection_type type; |
| 94 | /* |
| 95 | * session is only ever set for RELAY_CONTROL connection type. |
| 96 | */ |
| 97 | struct relay_session *session; |
| 98 | /* |
| 99 | * viewer_session is only ever set for RELAY_VIEWER_COMMAND |
| 100 | * connection type. |
| 101 | */ |
| 102 | struct relay_viewer_session *viewer_session; |
| 103 | |
| 104 | /* |
| 105 | * Protocol version to use for this connection. Only valid for |
| 106 | * RELAY_CONTROL connection type. |
| 107 | */ |
| 108 | uint32_t major; |
| 109 | uint32_t minor; |
| 110 | |
| 111 | struct urcu_ref ref; |
| 112 | |
| 113 | bool version_check_done; |
| 114 | |
| 115 | /* |
| 116 | * Node member of connection within global socket hash table. |
| 117 | */ |
| 118 | struct lttng_ht_node_ulong sock_n; |
| 119 | bool in_socket_ht; |
| 120 | struct lttng_ht *socket_ht; /* HACK: Contained within this hash table. */ |
| 121 | struct rcu_head rcu_node; /* For call_rcu teardown. */ |
| 122 | |
| 123 | union { |
| 124 | struct { |
| 125 | enum data_connection_state state_id; |
| 126 | union { |
| 127 | struct data_connection_state_receive_header receive_header; |
| 128 | struct data_connection_state_receive_payload receive_payload; |
| 129 | } state; |
| 130 | } data; |
| 131 | struct { |
| 132 | enum ctrl_connection_state state_id; |
| 133 | union { |
| 134 | struct ctrl_connection_state_receive_header receive_header; |
| 135 | struct ctrl_connection_state_receive_payload receive_payload; |
| 136 | } state; |
| 137 | struct lttng_dynamic_buffer reception_buffer; |
| 138 | } ctrl; |
| 139 | } protocol; |
| 140 | }; |
| 141 | |
| 142 | struct relay_connection *connection_create(struct lttcomm_sock *sock, |
| 143 | enum connection_type type); |
| 144 | struct relay_connection *connection_get_by_sock(struct lttng_ht *relay_connections_ht, |
| 145 | int sock); |
| 146 | int connection_reset_protocol_state(struct relay_connection *connection); |
| 147 | bool connection_get(struct relay_connection *connection); |
| 148 | void connection_put(struct relay_connection *connection); |
| 149 | void connection_ht_add(struct lttng_ht *relay_connections_ht, |
| 150 | struct relay_connection *conn); |
| 151 | int connection_set_session(struct relay_connection *conn, |
| 152 | struct relay_session *session); |
| 153 | |
| 154 | #endif /* _CONNECTION_H */ |