qualcomm/qbox
Loading...
Searching...
No Matches
char_backend_socket.h
1/*
2 * Copyright (c) 2022 Qualcomm Innovation Center, Inc. All Rights Reserved.
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
11
12#ifndef _GS_UART_BACKEND_SOCKET_H_
13#define _GS_UART_BACKEND_SOCKET_H_
14
15#include <unistd.h>
16#include <cstring>
17#include <stdio.h>
18
19#include <asio.hpp>
20#include <thread>
21#include <chrono>
22#include <atomic>
23
24#include <scp/report.h>
25
26#include <async_event.h>
27#include <uutils.h>
28#include <ports/biflow-socket.h>
29#include <module_factory_registery.h>
30
31using asio::ip::tcp;
32class char_backend_socket : public sc_core::sc_module
33{
34protected:
35 cci::cci_param<std::string> p_address;
36 cci::cci_param<bool> p_server;
37 cci::cci_param<bool> p_nowait;
38 cci::cci_param<bool> p_sigquit;
39
40private:
41 asio::io_context m_io_context;
42 tcp::acceptor m_acceptor;
43 tcp::socket m_asio_socket;
44 std::thread m_rcv_thread;
45
46 tcp::endpoint m_local_endpoint;
47
48 static constexpr size_t m_buffer_size = 1024;
49 std::array<char, m_buffer_size> m_buffer;
50
51 // Allow only 1 connection at a time
52 const int m_tcp_backlog = 1;
53
54 // Flag to signal end of operation to receive thread
55 std::atomic<bool> m_stop_rcv_thread = false;
56
57 const std::chrono::milliseconds m_retry_delay = std::chrono::milliseconds(100);
58
59 SCP_LOGGER();
60
61public:
63
64 char_backend_socket(sc_core::sc_module_name name)
65 : sc_core::sc_module(name)
66 , p_address("address", "127.0.0.1:4001", "socket address IP:Port")
67 , p_server("server", true, "type of socket: true if server - false if client")
68 , p_nowait("nowait", true, "setting socket in non-blocking mode")
69 , p_sigquit("sigquit", false, "Interpret 0x1c in the data stream as a sigquit")
70 , m_biflow_socket("biflow_socket")
71 , m_acceptor(m_io_context)
72 , m_asio_socket(m_io_context)
73 {
74 SCP_TRACE(()) << "char_backend_socket constructor";
75
76 if (!set_endpoint()) {
77 SCP_FATAL(()) << "Failed to set local endpoint";
78 }
79
80 m_biflow_socket.register_b_transport(this, &char_backend_socket::writefn);
81 }
82
83 ~char_backend_socket() { cleanup_receive_thread(); }
84
85 void end_of_simulation() { cleanup_receive_thread(); }
86
87 void cleanup_receive_thread()
88 {
89 m_stop_rcv_thread = true;
90 // The receive loops poll the stop flag. Do not close an Asio object
91 // while the receive thread may still be operating on it.
92 if (m_rcv_thread.joinable()) m_rcv_thread.join();
93
94 asio::error_code ignored_error;
95 m_asio_socket.close(ignored_error);
96 m_acceptor.close(ignored_error);
97 }
98
99 bool set_endpoint()
100 {
101 std::string param_ip, param_port;
102 asio::ip::address ip;
103 unsigned short port;
104
105 size_t count = std::count(p_address.get_value().begin(), p_address.get_value().end(), ':');
106 if (count == 1) {
107 size_t first = p_address.get_value().find_first_of(':');
108 param_ip = p_address.get_value().substr(0, first);
109 param_port = p_address.get_value().substr(first + 1);
110 } else {
111 SCP_ERR(()) << "malformed address, expecting IP:PORT (e.g. 127.0.0.1:4001)";
112 return false;
113 }
114
115 asio::error_code ec;
116 ip = asio::ip::make_address(param_ip, ec);
117 if (ec) {
118 SCP_ERR(()) << "Invalid IP address: " << param_ip << ", error: " << ec.message();
119 return false;
120 }
121
122 try {
123 port = static_cast<unsigned short>(std::stoi(param_port));
124 } catch (const std::invalid_argument& e) {
125 SCP_ERR(()) << "Invalid port: " << param_port << " (not a number)";
126 return false;
127 } catch (const std::out_of_range& e) {
128 SCP_ERR(()) << "Port out of range: " << param_port;
129 return false;
130 }
131
132 m_local_endpoint = tcp::endpoint(ip, port);
133
134 return true;
135 }
136
137 void start_of_simulation()
138 {
139 SCP_DEBUG(()) << "IP: " << m_local_endpoint.address() << ", PORT: " << m_local_endpoint.port();
140
141 if (p_server) {
142 setup_tcp_server();
143 }
144
145 m_rcv_thread = std::thread(&char_backend_socket::rcv_thread, this);
146 }
147
148 void end_of_elaboration() { m_biflow_socket.can_receive_any(); }
149
150 void setup_tcp_server()
151 {
152 asio::error_code ec;
153
154 m_acceptor.open(m_local_endpoint.protocol(), ec);
155 if (ec) {
156 SCP_ERR(()) << "Failed to open acceptor: " << ec.message();
157 return;
158 }
159
160 m_acceptor.set_option(asio::socket_base::reuse_address(true), ec);
161 if (ec) {
162 SCP_ERR(()) << "Failed to set reuse_address option: " << ec.message();
163 m_acceptor.close();
164 return;
165 }
166
167 m_acceptor.bind(m_local_endpoint, ec);
168 if (ec) {
169 SCP_ERR(()) << "Failed to bind acceptor: " << ec.message();
170 m_acceptor.close();
171 return;
172 }
173
174 m_acceptor.listen(m_tcp_backlog, ec);
175 if (ec) {
176 SCP_ERR(()) << "Failed to listen on acceptor: " << ec.message();
177 m_acceptor.close();
178 return;
179 }
180
181 m_acceptor.non_blocking(true, ec);
182 if (ec) {
183 SCP_ERR(()) << "Failed to set acceptor to non-blocking: " << ec.message();
184 m_acceptor.close();
185 return;
186 }
187 }
188
189 void writefn(tlm::tlm_generic_payload& txn, sc_core::sc_time& t)
190 {
191 while (!m_asio_socket.is_open()) {
192 if (p_nowait) {
193 return;
194 }
195 SCP_WARN(()) << "waiting for socket connection on IP address: " << p_address.get_value();
196 std::this_thread::sleep_for(m_retry_delay);
197 }
198
199 asio::error_code ec;
200
201 uint8_t* data = txn.get_data_ptr();
202 size_t bytes_to_send = txn.get_streaming_width();
203 size_t bytes_transferred = asio::write(m_asio_socket, asio::buffer(data, bytes_to_send), ec);
204 if (ec || (bytes_transferred != bytes_to_send)) {
205 if (p_nowait) {
206 SCP_WARN(())("(Non blocking) socket closed");
207 return;
208 } else {
209 SCP_WARN(())("(Blocking) socket closed.");
210 }
211 }
212 }
213
214 void rcv_thread()
215 {
216 try {
217 do_receive();
218 } catch (const asio::system_error& e) {
219 std::cout << "Caught exception: " << e.what() << std::endl;
220 }
221 }
222
223 void configure_socket()
224 {
225 asio::error_code ec;
226
227 m_asio_socket.non_blocking(true, ec);
228 if (ec) {
229 SCP_FATAL(()) << " Failed to set non blocking mode: " << ec.message();
230 }
231
232 // Disable Nagle's Algorithm
233 m_asio_socket.set_option(asio::ip::tcp::no_delay(true));
234 if (ec) {
235 SCP_FATAL(()) << " Failed to disable Nagle's Algorithm: " << ec.message();
236 }
237 }
238
239 void do_receive()
240 {
241 asio::error_code ec;
242
243 while (!m_stop_rcv_thread) {
244 if (p_server) {
245 // Server mode
246 m_acceptor.accept(m_asio_socket, ec);
247 switch (ec.value()) {
248 case 0: // Success
249 configure_socket();
250 SCP_DEBUG(()) << "Accepted connection from " << m_asio_socket.remote_endpoint().address() << ":"
251 << m_asio_socket.remote_endpoint().port();
252 break;
253 case asio::error::would_block:
254 SCP_INFO(()) << "Accept failed for non blocking:" << ec.message();
255 std::this_thread::sleep_for(m_retry_delay);
256 break;
257 default:
258 SCP_WARN(()) << "Accept connection failed with error:" << ec.message();
259 std::this_thread::sleep_for(m_retry_delay);
260 break;
261 }
262 } else if (!p_server) {
263 // Client mode
264 m_asio_socket.connect(m_local_endpoint, ec);
265 if (!ec) {
266 configure_socket();
267 } else {
268 SCP_INFO(()) << "failed to connect to " << m_local_endpoint.address() << ":"
269 << m_local_endpoint.port() << " " << ec.message();
270 m_asio_socket.close();
271 std::this_thread::sleep_for(m_retry_delay);
272 continue;
273 }
274 }
275
276 if (m_asio_socket.is_open()) receive_loop();
277 }
278
279 SCP_DEBUG(()) << "Leaving receive thread";
280 }
281
282 void receive_loop()
283 {
284 asio::error_code ec;
285
286 while (!m_stop_rcv_thread && m_asio_socket.is_open()) {
287 size_t read_count = m_asio_socket.read_some(asio::buffer(m_buffer), ec);
288 switch (ec.value()) {
289 case 0: // Success
290 if (!m_stop_rcv_thread) forward_incoming_data(read_count);
291 break;
292 case asio::error::would_block:
293 // Retry reading after a short delay
294 std::this_thread::sleep_for(m_retry_delay);
295 break;
296 case asio::error::eof:
297 SCP_DEBUG(()) << "Remote endpoint disconnected";
298 m_asio_socket.close();
299 break;
300 default:
301 SCP_WARN(()) << "Read failed: " << ec.message();
302 m_asio_socket.close();
303 break;
304 }
305 }
306
307 SCP_DEBUG(()) << "Leaving receive loop";
308 }
309
310 void forward_incoming_data(size_t data_length)
311 {
312 for (size_t i = 0; i < data_length; i++) {
313 unsigned char c = m_buffer[i];
314 if (p_sigquit && c == 0x1c) {
315 sc_core::sc_stop();
316 }
317 m_biflow_socket.enqueue(c);
318 }
319 }
320};
321extern "C" void module_register();
322#endif
Definition biflow-socket.h:73