-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathudp-pktgen.rs
315 lines (275 loc) · 9.52 KB
/
udp-pktgen.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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
// 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,
/// Remote socket IPv4 address.
remote: SocketAddrV4,
/// Buffer size (in bytes).
bufsize: usize,
/// Injection rate (in micro-seconds).
messages: u64,
}
/// Associate functions for Program Arguments
impl ProgramArguments {
// Default buffer size.
const DEFAULT_BUFSIZE: usize = 1024;
/// Default local address.
const DEFAULT_LOCAL: &'static str = "127.0.0.1:12345";
// Default number of messages.
const DEFAULT_MESSAGES: u64 = 100;
/// Default host address.
const DEFAULT_REMOTE: &'static str = "127.0.0.1:23456";
/// 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("local")
.long("local")
.value_parser(clap::value_parser!(String))
.required(false)
.value_name("ADDRESS:PORT")
.help("Sets local address"),
)
.arg(
Arg::new("remote")
.long("remote")
.value_parser(clap::value_parser!(String))
.required(true)
.value_name("ADDRESS:PORT")
.help("Sets remote address"),
)
.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"),
)
.get_matches();
// Default arguments.
let mut args: ProgramArguments = ProgramArguments {
local: SocketAddrV4::from_str(Self::DEFAULT_LOCAL)?,
remote: SocketAddrV4::from_str(Self::DEFAULT_REMOTE)?,
bufsize: Self::DEFAULT_BUFSIZE,
messages: Self::DEFAULT_MESSAGES,
};
// Local address.
if let Some(addr) = matches.get_one::<String>("local") {
args.set_local_addr(addr)?;
}
// Remote address.
if let Some(addr) = matches.get_one::<String>("remote") {
args.set_remote_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") {
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
}
/// Returns the remote endpoint address parameter stored in the target program arguments.
pub fn get_remote(&self) -> SocketAddrV4 {
self.remote
}
/// Returns the buffer size parameter stored in the target program arguments.
pub fn get_bufsize(&self) -> usize {
self.bufsize
}
/// Returns the injection rate parameter stored in the target program arguments.
pub fn get_messages(&self) -> u64 {
self.messages
}
/// 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(())
}
/// Sets the remote address and port number parameters in the target program arguments.
fn set_remote_addr(&mut self, addr: &str) -> Result<()> {
self.remote = SocketAddrV4::from_str(addr)?;
Ok(())
}
/// Sets the buffer size parameter in the target program arguments.
fn set_bufsize(&mut self, bufsize_str: &str) -> Result<()> {
let bufsize: usize = 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 injection rate")
}
}
}
//==============================================================================
// Application
//==============================================================================
/// Application
struct Application {
/// Underlying libOS.
libos: LibOS,
// Local socket descriptor.
sockqd: QDesc,
/// Remote endpoint.
remote: SocketAddrV4,
/// Buffer size.
bufsize: usize,
/// 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 remote: SocketAddrV4 = args.get_remote();
let bufsize: usize = args.get_bufsize();
let messages: u64 = args.get_messages();
// Create UDP socket.
let sockqd: QDesc = match libos.socket(libc::AF_INET, libc::SOCK_DGRAM, 1) {
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!("Remote Address: {:?}", remote);
println!("Expected messages: {}", messages);
println!("Message size: {}", bufsize);
Self {
libos,
sockqd,
remote,
bufsize,
messages,
}
}
/// Runs the target application.
pub fn run(&mut self) {
let start: Instant = Instant::now();
let mut nbytes: usize = 0;
let mut num_pkt: u64 = 0;
let mut last_push: Instant = Instant::now();
let mut last_log: Instant = Instant::now();
let data: Vec<u8> = Self::mkbuf(self.bufsize, 0x65);
while num_pkt < self.messages {
// // Dump statistics.
// if last_log.elapsed() > Duration::from_secs(Self::LOG_INTERVAL) {
// let elapsed: Duration = Instant::now() - start;
// println!("{:?} B / {:?} us", nbytes, elapsed.as_micros());
// last_log = Instant::now();
// }
// Push packet.
// if last_push.elapsed() > Duration::from_nanos(self.messages) {
let qt: QToken = match self.libos.pushto2(self.sockqd, &data, self.remote) {
Ok(qt) => qt,
Err(e) => panic!("failed to push data to socket: {:?}", e.cause),
};
match self.libos.wait2(qt) {
Ok((_, OperationResult::Push)) => (),
Err(e) => panic!("operation failed: {:?}", e.cause),
_ => panic!("unexpected result"),
};
num_pkt += 1;
// nbytes += self.bufsize;
// last_push = Instant::now();
// }
}
}
/// Makes a buffer.
fn mkbuf(bufsize: usize, fill_char: u8) -> Vec<u8> {
let mut data: Vec<u8> = Vec::<u8>::with_capacity(bufsize);
for _ in 0..bufsize {
data.push(fill_char);
}
data
}
}
//==============================================================================
/// Drives the application.
fn main() -> Result<()> {
let args: ProgramArguments = ProgramArguments::new(
"udp-pktgen",
"Pedro Henrique Penna <[email protected]>",
"Generates UDP traffic.",
)?;
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(())
}