-
Notifications
You must be signed in to change notification settings - Fork 0
/
udp-echo.rs
270 lines (238 loc) · 7.91 KB
/
udp-echo.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
#![cfg_attr(feature = "strict", deny(warnings))]
#![deny(clippy::all)]
//==============================================================================
// Imports
//==============================================================================
use ::anyhow::{
bail,
Result,
};
use ::clap::{
Arg,
ArgMatches,
Command,
};
use ::demikernel::{
LibOS,
LibOSName,
OperationResult,
QDesc,
QToken,
};
use ::std::{
net::SocketAddrV4,
str::FromStr,
time::{
Duration,
Instant,
},
};
//==============================================================================
// Program Arguments
//==============================================================================
/// Program Arguments
#[derive(Debug)]
pub struct ProgramArguments {
/// Local socket IPv4 address.
local: SocketAddrV4,
/// Buffer size (in bytes).
bufsize: u64,
/// Injection rate (in micro-seconds).
messages: u64,
}
/// Associate functions for Program Arguments
impl ProgramArguments {
// Default buffer size.
const DEFAULT_BUFSIZE: u64 = 1024;
/// Default local address.
const DEFAULT_LOCAL: &'static str = "127.0.0.1:12345";
// Default injection rate.
const DEFAULT_MESSAGES: u64 = 1000;
/// Parses the program arguments from the command line interface.
pub fn new(app_name: &'static str, app_author: &'static str, app_about: &'static str) -> Result<Self> {
let matches: ArgMatches = Command::new(app_name)
.author(app_author)
.about(app_about)
.arg(
Arg::new("bufsize")
.long("bufsize")
.value_parser(clap::value_parser!(String))
.required(true)
.value_name("SIZE")
.help("Sets buffer size"),
)
.arg(
Arg::new("messages")
.long("messages")
.value_parser(clap::value_parser!(String))
.required(true)
.value_name("MSGS")
.help("Sets packet injection rate"),
)
.arg(
Arg::new("local")
.long("local")
.value_parser(clap::value_parser!(String))
.required(true)
.value_name("ADDRESS:PORT")
.help("Sets local address"),
)
.get_matches();
// Default arguments.
let mut args: ProgramArguments = ProgramArguments {
local: SocketAddrV4::from_str(Self::DEFAULT_LOCAL)?,
bufsize: Self::DEFAULT_BUFSIZE,
messages: Self::DEFAULT_MESSAGES,
};
// Local address.
if let Some(addr) = matches.get_one::<String>("local") {
args.set_local_addr(addr)?;
}
// Buffer size.
if let Some(bufsize) = matches.get_one::<String>("bufsize") {
args.set_bufsize(bufsize)?;
}
// Number of messages.
if let Some(messages) = matches.get_one::<String>("messages") {
println!("Message size passed: {}", messages);
args.set_messages(messages)?;
}
Ok(args)
}
/// Returns the local endpoint address parameter stored in the target program arguments.
pub fn get_local(&self) -> SocketAddrV4 {
self.local
}
/// Sets the local address and port number parameters in the target program arguments.
fn set_local_addr(&mut self, addr: &str) -> Result<()> {
self.local = SocketAddrV4::from_str(addr)?;
Ok(())
}
/// Returns the buffer size parameter stored in the target program arguments.
pub fn get_bufsize(&self) -> u64 {
self.bufsize
}
/// Returns the injection rate parameter stored in the target program arguments.
pub fn get_messages(&self) -> u64 {
self.messages
}
/// Sets the buffer size parameter in the target program arguments.
fn set_bufsize(&mut self, bufsize_str: &str) -> Result<()> {
let bufsize: u64 = bufsize_str.parse()?;
if bufsize > 0 {
self.bufsize = bufsize;
Ok(())
} else {
bail!("invalid buffer size")
}
}
/// Sets the injection rate parameter in the target program arguments.
fn set_messages(&mut self, messages_str: &str) -> Result<()> {
let messages: u64 = messages_str.parse()?;
if messages > 0 {
self.messages = messages;
Ok(())
} else {
bail!("invalid number of messages")
}
}
}
//==============================================================================
// Application
//==============================================================================
/// Application
struct Application {
/// Underlying libOS.
libos: LibOS,
// Local socket descriptor.
sockqd: QDesc,
/// Buffer size.
bufsize: u64,
/// Injection rate
messages: u64,
}
/// Associated Functions for the Application
impl Application {
/// Logging interval (in seconds).
const LOG_INTERVAL: u64 = 5;
/// Instantiates the application.
pub fn new(mut libos: LibOS, args: &ProgramArguments) -> Self {
// Extract arguments.
let local: SocketAddrV4 = args.get_local();
let bufsize: u64 = args.get_bufsize();
let messages: u64 = args.get_messages();
// Create UDP socket.
let sockqd: QDesc = match libos.socket(libc::AF_INET, libc::SOCK_DGRAM, 0) {
Ok(qd) => qd,
Err(e) => panic!("failed to create socket: {:?}", e.cause),
};
// Bind to local address.
match libos.bind(sockqd, local) {
Ok(()) => (),
Err(e) => panic!("failed to bind socket: {:?}", e.cause),
};
println!("Local Address: {:?}", local);
println!("Expected messages: {}", messages);
println!("Message size: {}", bufsize);
Self {
libos,
sockqd,
bufsize,
messages,
}
}
/// Runs the target echo server.
pub fn run(&mut self) {
let mut start_time = Instant::now();
let mut count: u64 = 0;
while count < self.messages {
// Push the receive request
let qt: QToken = match self.libos.pop(self.sockqd) {
Ok(qt) => qt,
Err(e) => panic!("failed to pop: {:?}", e.cause),
};
// Wait to receive
match self.libos.wait2(qt) {
Ok(_) => {
if count == 0 {
start_time = Instant::now();
}
count += 1;
},
Err(e) => panic!("operation failed: {:?}", e.cause),
};
}
let elapsed_time_ns = start_time.elapsed().as_nanos();
// Compute and print results
let mbps = ((count * self.bufsize * 8) as f64 * 1000.0) / elapsed_time_ns as f64;
let throughput = (count as f64 * 1000.0) / elapsed_time_ns as f64;
println!(
"{},{},{:.3},{:.3},{:.3}",
count,
self.bufsize,
elapsed_time_ns as f64 / 1000.0,
throughput,
mbps
);
}
}
//==============================================================================
fn main() -> Result<()> {
let args: ProgramArguments = ProgramArguments::new(
"udp-echo",
"Pedro Henrique Penna <[email protected]>",
"Echoes UDP packets.",
)?;
let libos_name: LibOSName = match LibOSName::from_env() {
Ok(libos_name) => libos_name.into(),
Err(e) => panic!("{:?}", e),
};
let libos: LibOS = match LibOS::new(libos_name) {
Ok(libos) => libos,
Err(e) => panic!("failed to initialize libos: {:?}", e.cause),
};
Application::new(libos, &args).run();
Ok(())
}