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