From 98e47aa19615d9c101f9fe3afd104b0592f6efa6 Mon Sep 17 00:00:00 2001 From: Dmitrii Dolgov <9erthalion6@gmail.com> Date: Tue, 9 Apr 2024 14:56:32 +0200 Subject: [PATCH] Add server side to network worker Run a simple listening echo server, which can serve as a real-syscalls backend for benchmarking. --- src/worker/network.rs | 53 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/src/worker/network.rs b/src/worker/network.rs index bf655c8..e1010bb 100644 --- a/src/worker/network.rs +++ b/src/worker/network.rs @@ -35,6 +35,53 @@ impl NetworkWorker { workload: workload, } } + + /// Start a simple server. The client side is going to be a networking + /// worker as well, so for convenience of troubleshooting do not error + /// out if something unexpected happened, log and proceed instead. + fn start_server( + &self, + addr: Ipv4Address, + target_port: u16, + ) -> Result<(), WorkerError> { + let listener = + TcpListener::bind((addr.to_string(), target_port)).unwrap(); + + for stream in listener.incoming() { + let mut stream = stream.unwrap(); + loop { + let mut buf_reader = BufReader::new(&stream); + let mut buffer = String::new(); + + match buf_reader.read_line(&mut buffer) { + Ok(0) => { + // EOF, exit + break; + } + Ok(_n) => { + trace!("Received {:?}", buffer); + + let response = "hello\n"; + match stream.write_all(response.as_bytes()) { + Ok(_) => { + // Response is sent, handle the next one + break; + } + Err(e) => { + trace!("ERROR: sending response, {}", e); + break; + } + } + } + Err(e) => { + trace!("ERROR: reading a line, {}", e) + } + } + } + } + + Ok(()) + } } impl Worker for NetworkWorker { @@ -53,6 +100,12 @@ impl Worker for NetworkWorker { unreachable!() }; + let ip_addr = Ipv4Address([address.0, address.1, address.2, address.3]); + + if server { + let _ = self.start_server(ip_addr, target_port); + } + Ok(()) } }