-
Notifications
You must be signed in to change notification settings - Fork 991
/
util.rs
352 lines (271 loc) · 9.86 KB
/
util.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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
// Copyright (c) 2021 Intel Corporation
//
// SPDX-License-Identifier: Apache-2.0
//
use anyhow::{anyhow, Result};
use futures::StreamExt;
use std::io;
use std::io::ErrorKind;
use std::os::unix::io::{FromRawFd, RawFd};
use tokio::io::{AsyncReadExt, AsyncWriteExt};
use tokio::sync::watch::Receiver;
use tokio_vsock::{Incoming, VsockListener, VsockStream};
use tracing::instrument;
// Size of I/O read buffer
const BUF_SIZE: usize = 8192;
// Interruptable I/O copy using readers and writers
// (an interruptable version of "io::copy()").
pub async fn interruptable_io_copier<R: Sized, W: Sized>(
mut reader: R,
mut writer: W,
mut shutdown: Receiver<bool>,
) -> io::Result<u64>
where
R: tokio::io::AsyncRead + Unpin,
W: tokio::io::AsyncWrite + Unpin,
{
let mut total_bytes: u64 = 0;
let mut buf: [u8; BUF_SIZE] = [0; BUF_SIZE];
loop {
tokio::select! {
_ = shutdown.changed() => {
eprintln!("INFO: interruptable_io_copier: got shutdown request");
break;
},
result = reader.read(&mut buf) => {
let bytes = match result {
Ok(0) => return Ok(total_bytes),
Ok(len) => len,
Err(ref e) if e.kind() == ErrorKind::Interrupted => continue,
Err(e) => return Err(e),
};
total_bytes += bytes as u64;
// Actually copy the data ;)
writer.write_all(&buf[..bytes]).await?;
},
};
}
Ok(total_bytes)
}
#[instrument]
pub fn get_vsock_incoming(fd: RawFd) -> Incoming {
unsafe { VsockListener::from_raw_fd(fd).incoming() }
}
#[instrument]
pub async fn get_vsock_stream(fd: RawFd) -> Result<VsockStream> {
let stream = get_vsock_incoming(fd)
.next()
.await
.ok_or_else(|| anyhow!("cannot handle incoming vsock connection"))?;
Ok(stream?)
}
#[cfg(test)]
mod tests {
use super::*;
use std::io;
use std::io::Cursor;
use std::io::Write;
use std::pin::Pin;
use std::sync::{Arc, Mutex};
use std::task::{Context, Poll, Poll::Ready};
use tokio::pin;
use tokio::select;
use tokio::sync::watch::channel;
use tokio::task::JoinError;
use tokio::time::Duration;
#[derive(Debug, Default, Clone)]
struct BufWriter {
data: Arc<Mutex<Vec<u8>>>,
write_delay: Duration,
}
impl BufWriter {
fn new() -> Self {
BufWriter {
data: Arc::new(Mutex::new(Vec::<u8>::new())),
write_delay: Duration::new(0, 0),
}
}
fn write_vec(&mut self, buf: &[u8]) -> io::Result<usize> {
let vec_ref = self.data.clone();
let mut vec_locked = vec_ref.lock();
let mut v = vec_locked.as_deref_mut().unwrap();
if self.write_delay.as_nanos() > 0 {
std::thread::sleep(self.write_delay);
}
std::io::Write::write(&mut v, buf)
}
}
impl Write for BufWriter {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
self.write_vec(buf)
}
fn flush(&mut self) -> io::Result<()> {
let vec_ref = self.data.clone();
let mut vec_locked = vec_ref.lock();
let v = vec_locked
.as_deref_mut()
.map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e.to_string()))?;
std::io::Write::flush(v)
}
}
impl tokio::io::AsyncWrite for BufWriter {
fn poll_write(
mut self: Pin<&mut Self>,
_cx: &mut Context<'_>,
buf: &[u8],
) -> Poll<Result<usize, io::Error>> {
let result = self.write_vec(buf);
Ready(result)
}
fn poll_flush(
self: Pin<&mut Self>,
_cx: &mut Context<'_>,
) -> Poll<Result<(), std::io::Error>> {
// NOP
Ready(Ok(()))
}
fn poll_shutdown(
self: Pin<&mut Self>,
_cx: &mut Context<'_>,
) -> Poll<Result<(), std::io::Error>> {
// NOP
Ready(Ok(()))
}
}
impl ToString for BufWriter {
fn to_string(&self) -> String {
let data_ref = self.data.clone();
let output = data_ref.lock().unwrap();
let s = (*output).clone();
String::from_utf8(s).unwrap()
}
}
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn test_interruptable_io_copier_reader() {
#[derive(Debug)]
struct TestData {
reader_value: String,
}
let tests = &[
TestData {
reader_value: "".into(),
},
TestData {
reader_value: "a".into(),
},
TestData {
reader_value: "foo".into(),
},
TestData {
reader_value: "b".repeat(BUF_SIZE - 1),
},
TestData {
reader_value: "c".repeat(BUF_SIZE),
},
TestData {
reader_value: "d".repeat(BUF_SIZE + 1),
},
TestData {
reader_value: "e".repeat((2 * BUF_SIZE) - 1),
},
TestData {
reader_value: "f".repeat(2 * BUF_SIZE),
},
TestData {
reader_value: "g".repeat((2 * BUF_SIZE) + 1),
},
];
for (i, d) in tests.iter().enumerate() {
// Create a string containing details of the test
let msg = format!("test[{}]: {:?}", i, d);
let (tx, rx) = channel(true);
let reader = Cursor::new(d.reader_value.clone());
let writer = BufWriter::new();
// XXX: Pass a copy of the writer to the copier to allow the
// result of the write operation to be checked below.
let handle = tokio::spawn(interruptable_io_copier(reader, writer.clone(), rx));
// Allow time for the thread to be spawned.
tokio::time::sleep(Duration::from_secs(1)).await;
let timeout = tokio::time::sleep(Duration::from_secs(1));
pin!(timeout);
// Since the readers only specify a small number of bytes, the
// copier will quickly read zero and kill the task, closing the
// Receiver.
assert!(tx.is_closed(), "{}", msg);
let spawn_result: std::result::Result<
std::result::Result<u64, std::io::Error>,
JoinError,
>;
let result: std::result::Result<u64, std::io::Error>;
select! {
res = handle => spawn_result = res,
_ = &mut timeout => panic!("timed out"),
}
assert!(spawn_result.is_ok());
result = spawn_result.unwrap();
assert!(result.is_ok());
let byte_count = result.unwrap() as usize;
assert_eq!(byte_count, d.reader_value.len(), "{}", msg);
let value = writer.to_string();
assert_eq!(value, d.reader_value, "{}", msg);
}
}
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn test_interruptable_io_copier_eof() {
// Create an async reader that always returns EOF
let reader = tokio::io::empty();
let (tx, rx) = channel(true);
let writer = BufWriter::new();
let handle = tokio::spawn(interruptable_io_copier(reader, writer.clone(), rx));
// Allow time for the thread to be spawned.
tokio::time::sleep(Duration::from_secs(1)).await;
let timeout = tokio::time::sleep(Duration::from_secs(1));
pin!(timeout);
assert!(tx.is_closed());
let spawn_result: std::result::Result<std::result::Result<u64, std::io::Error>, JoinError>;
let result: std::result::Result<u64, std::io::Error>;
select! {
res = handle => spawn_result = res,
_ = &mut timeout => panic!("timed out"),
}
assert!(spawn_result.is_ok());
result = spawn_result.unwrap();
assert!(result.is_ok());
let byte_count = result.unwrap();
assert_eq!(byte_count, 0);
}
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn test_interruptable_io_copier_shutdown() {
// Create an async reader that creates an infinite stream of bytes
// (which allows us to interrupt it, since we know it is always busy ;)
const REPEAT_CHAR: u8 = b'r';
let reader = tokio::io::repeat(REPEAT_CHAR);
let (tx, rx) = channel(true);
let writer = BufWriter::new();
let handle = tokio::spawn(interruptable_io_copier(reader, writer.clone(), rx));
// Allow time for the thread to be spawned.
tokio::time::sleep(Duration::from_secs(1)).await;
let timeout = tokio::time::sleep(Duration::from_secs(1));
pin!(timeout);
assert!(!tx.is_closed());
tx.send(true).expect("failed to request shutdown");
let spawn_result: std::result::Result<std::result::Result<u64, std::io::Error>, JoinError>;
let result: std::result::Result<u64, std::io::Error>;
select! {
res = handle => spawn_result = res,
_ = &mut timeout => panic!("timed out"),
}
assert!(spawn_result.is_ok());
result = spawn_result.unwrap();
assert!(result.is_ok());
let byte_count = result.unwrap();
let value = writer.to_string();
let writer_byte_count = value.len() as u64;
assert_eq!(byte_count, writer_byte_count);
// Remove the char used as a payload. If anything else remins,
// something went wrong.
let mut remainder = value;
remainder.retain(|c| c != REPEAT_CHAR as char);
assert_eq!(remainder.len(), 0);
}
}