qos_core 0.10.0

Core components and logic for QuorumOS applications
Documentation
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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
//! Abstractions to handle connection based socket streams.

use std::{io::ErrorKind, pin::Pin};

use tokio::{
	io::{AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt},
	net::{UnixListener, UnixSocket, UnixStream},
};
#[cfg(not(target_os = "macos"))]
use tokio_vsock::{VsockListener, VsockStream};

use super::{IOError, SocketAddress};

const MIB: usize = 1024 * 1024;

/// Maximum payload size for a single recv / send call. We're being generous with 128MiB.
/// The goal here is to avoid server crashes if the payload size exceeds the available system memory.
pub const MAX_PAYLOAD_SIZE: usize = 128 * MIB;

#[derive(Debug)]
enum InnerListener {
	Unix(UnixListener),
	#[cfg(not(target_os = "macos"))]
	Vsock(VsockListener),
}

#[derive(Debug)]
enum InnerStream {
	Unix(UnixStream),
	#[cfg(not(target_os = "macos"))]
	Vsock(VsockStream),
}

/// Handle on a stream
#[derive(Debug)]
pub struct Stream {
	address: Option<SocketAddress>,
	inner: Option<InnerStream>,
}

impl From<&Stream> for Stream {
	/// Construct a not yet connected Stream from another Stream.
	fn from(other: &Stream) -> Self {
		Self { address: other.address.clone(), inner: None }
	}
}

impl Stream {
	// accept a new connection, used by server side
	fn unix_accepted(stream: UnixStream) -> Self {
		Self { address: None, inner: Some(InnerStream::Unix(stream)) }
	}

	// accept a new connection, used by server side
	#[cfg(not(target_os = "macos"))]
	fn vsock_accepted(stream: VsockStream) -> Self {
		Self { address: None, inner: Some(InnerStream::Vsock(stream)) }
	}

	/// Create a new `Stream` with known `SocketAddress`. The stream starts disconnected
	/// and will connect on the first `call`.
	#[must_use]
	pub fn new(address: &SocketAddress) -> Self {
		Self { address: Some(address.clone()), inner: None }
	}

	/// Connect `Stream` to `SocketAddress`.
	/// Sets `inner` to the new stream.
	///
	/// # Errors
	///
	/// Returns [`IOError`] if the address is invalid or the connection
	/// fails.
	pub async fn connect(&mut self) -> Result<(), IOError> {
		let addr = self.address()?;

		match self.address()? {
			SocketAddress::Unix(_uaddr) => {
				let inner = unix_connect(addr).await?;

				self.inner = Some(InnerStream::Unix(inner));
			}
			#[cfg(not(target_os = "macos"))]
			SocketAddress::Vsock(_vaddr) => {
				let inner = vsock_connect(addr).await?;

				self.inner = Some(InnerStream::Vsock(inner));
			}
		}

		Ok(())
	}

	/// Reconnects this `Stream` by calling `connect` again on the underlying
	/// socket.
	///
	/// # Errors
	///
	/// Returns [`IOError`] if the address is invalid or the connection
	/// fails.
	pub async fn reconnect(&mut self) -> Result<(), IOError> {
		let addr = self.address()?.clone();

		match &mut self.inner_mut()? {
			InnerStream::Unix(s) => {
				*s = unix_connect(&addr).await?;
			}
			#[cfg(not(target_os = "macos"))]
			InnerStream::Vsock(s) => {
				*s = vsock_connect(&addr).await?;
			}
		}
		Ok(())
	}

	/// Sends a buffer over the underlying socket using async.
	///
	/// # Errors
	///
	/// Returns [`IOError`] if the stream is disconnected or the write
	/// fails.
	pub async fn send(&mut self, buf: &[u8]) -> Result<(), IOError> {
		match &mut self.inner_mut()? {
			InnerStream::Unix(s) => send(s, buf).await,
			#[cfg(not(target_os = "macos"))]
			InnerStream::Vsock(s) => send(s, buf).await,
		}
	}

	/// Receive from the underlying socket using async.
	///
	/// # Errors
	///
	/// Returns [`IOError`] if the stream is disconnected or the read fails.
	pub async fn recv(&mut self) -> Result<Vec<u8>, IOError> {
		match &mut self.inner_mut()? {
			InnerStream::Unix(s) => recv(s).await,
			#[cfg(not(target_os = "macos"))]
			InnerStream::Vsock(s) => recv(s).await,
		}
	}

	/// Perform a "call" by sending the `req_buf` bytes and waiting for a
	/// reply on the same socket.
	///
	/// # Errors
	///
	/// Returns [`IOError`] if connecting, sending, or receiving fails.
	pub async fn call(&mut self, req_buf: &[u8]) -> Result<Vec<u8>, IOError> {
		// first time? connect
		if self.inner.is_none() {
			self.connect().await?;
		} else {
			eprintln!("SocketStream already connected, call proceeding");
		}

		self.send(req_buf).await?;
		self.recv().await
	}

	/// Get the address of the socket or error out in case it's invalid.
	///
	/// # Errors
	///
	/// Returns [`IOError::ConnectAddressInvalid`] if no address is set.
	pub fn address(&self) -> Result<&SocketAddress, IOError> {
		self.address.as_ref().ok_or(IOError::ConnectAddressInvalid)
	}

	fn inner_mut(&mut self) -> Result<&mut InnerStream, IOError> {
		self.inner.as_mut().ok_or(IOError::DisconnectedStream)
	}

	/// Resets the inner stream, forcing a re-connect next `call`
	pub fn reset(&mut self) {
		self.inner = None;
	}

	/// Checks if we're in `connected` state.
	/// NOTE: this does NOT mean that the connection is currently OK. It just means we've
	/// connected in the past, and our `inner` field is active.
	pub fn is_connected(&self) -> bool {
		self.inner.is_some()
	}
}

async fn send<S: AsyncWriteExt + Unpin>(
	stream: &mut S,
	buf: &[u8],
) -> Result<(), IOError> {
	// NOTE: due to this kernel bug: https://github.com/cloud-hypervisor/cloud-hypervisor/issues/7672
	// we need to write in chunks of < 32kiB each
	const MAX_WRITE_SIZE: usize = 31234;

	let length = buf.len();

	if length > MAX_PAYLOAD_SIZE {
		return Err(IOError::OversizedPayload(length));
	}

	// First, send the length of the buffer
	let len_buf: [u8; size_of::<u64>()] = (length as u64).to_le_bytes();

	// send the header
	stream.write_all(&len_buf).await?;

	// Send the actual contents of the buffer
	let mut total = 0;

	while total < length {
		total += stream
			.write(
				&buf[total..std::cmp::min(buf.len(), total + MAX_WRITE_SIZE)],
			)
			.await?;
	}

	Ok(())
}

async fn recv<S: AsyncReadExt + Unpin>(
	stream: &mut S,
) -> Result<Vec<u8>, IOError> {
	let length: usize = {
		let mut buf = [0u8; size_of::<u64>()];

		stream.read_exact(&mut buf).await.map_err(|e| match e.kind() {
			ErrorKind::UnexpectedEof => IOError::RecvConnectionClosed,
			_ => IOError::StdIoError(e),
		})?;

		u64::from_le_bytes(buf)
			.try_into()
			// Should only be possible if we are on 32bit architecture
			.map_err(|_| IOError::ArithmeticSaturation)?
	};

	if length > MAX_PAYLOAD_SIZE {
		return Err(IOError::OversizedPayload(length));
	}

	// Read the buffer
	let mut buf = vec![0; length];
	stream.read_exact(&mut buf).await.map_err(|e| match e.kind() {
		ErrorKind::UnexpectedEof => IOError::RecvConnectionClosed,
		_ => IOError::StdIoError(e),
	})?;

	Ok(buf)
}

impl From<IOError> for std::io::Error {
	fn from(value: IOError) -> Self {
		match value {
			IOError::DisconnectedStream => std::io::Error::new(
				std::io::ErrorKind::NotFound,
				"connection not found",
			),
			_ => std::io::Error::other("unknown error"),
		}
	}
}

impl AsyncRead for Stream {
	fn poll_read(
		mut self: std::pin::Pin<&mut Self>,
		cx: &mut std::task::Context<'_>,
		buf: &mut tokio::io::ReadBuf<'_>,
	) -> std::task::Poll<std::io::Result<()>> {
		match &mut self.inner_mut()? {
			InnerStream::Unix(s) => Pin::new(s).poll_read(cx, buf),
			#[cfg(not(target_os = "macos"))]
			InnerStream::Vsock(s) => Pin::new(s).poll_read(cx, buf),
		}
	}
}

impl AsyncWrite for Stream {
	fn poll_write(
		mut self: std::pin::Pin<&mut Self>,
		cx: &mut std::task::Context<'_>,
		buf: &[u8],
	) -> std::task::Poll<Result<usize, std::io::Error>> {
		match &mut self.inner_mut()? {
			InnerStream::Unix(s) => Pin::new(s).poll_write(cx, buf),
			#[cfg(not(target_os = "macos"))]
			InnerStream::Vsock(s) => Pin::new(s).poll_write(cx, buf),
		}
	}

	fn poll_flush(
		mut self: std::pin::Pin<&mut Self>,
		cx: &mut std::task::Context<'_>,
	) -> std::task::Poll<Result<(), std::io::Error>> {
		match &mut self.inner_mut()? {
			InnerStream::Unix(s) => Pin::new(s).poll_flush(cx),
			#[cfg(not(target_os = "macos"))]
			InnerStream::Vsock(s) => Pin::new(s).poll_flush(cx),
		}
	}

	fn poll_shutdown(
		mut self: std::pin::Pin<&mut Self>,
		cx: &mut std::task::Context<'_>,
	) -> std::task::Poll<Result<(), std::io::Error>> {
		match &mut self.inner_mut()? {
			InnerStream::Unix(s) => Pin::new(s).poll_shutdown(cx),
			#[cfg(not(target_os = "macos"))]
			InnerStream::Vsock(s) => Pin::new(s).poll_shutdown(cx),
		}
	}
}

/// Abstraction to listen for incoming stream connections.
pub struct Listener {
	inner: InnerListener,
	addr: SocketAddress, // just for info
}

impl Listener {
	/// Bind and listen on the given address.
	pub(crate) fn listen(addr: &SocketAddress) -> Result<Self, IOError> {
		let listener = match *addr {
			SocketAddress::Unix(uaddr) => {
				let path =
					uaddr.path().ok_or(IOError::ConnectAddressInvalid)?;
				if path.exists() {
					// attempt cleanup, this mostly happens from tests/panics
					_ = std::fs::remove_file(path);
				}
				let inner = InnerListener::Unix(UnixListener::bind(path)?);
				Self { inner, addr: addr.clone() }
			}
			#[cfg(not(target_os = "macos"))]
			SocketAddress::Vsock(vaddr) => {
				let inner = InnerListener::Vsock(VsockListener::bind(vaddr)?);
				Self { inner, addr: addr.clone() }
			}
		};

		Ok(listener)
	}

	/// Accept a new connection.
	///
	/// # Errors
	///
	/// Returns [`IOError`] if accepting the connection fails.
	pub async fn accept(&self) -> Result<Stream, IOError> {
		let stream = match &self.inner {
			InnerListener::Unix(l) => {
				let (s, _) = l.accept().await?;
				Stream::unix_accepted(s)
			}
			#[cfg(not(target_os = "macos"))]
			InnerListener::Vsock(l) => {
				let (s, _) = l.accept().await?;
				Stream::vsock_accepted(s)
			}
		};

		Ok(stream)
	}

	/// Return the listener's address
	pub fn addr(&self) -> &SocketAddress {
		&self.addr
	}
}

impl Drop for Listener {
	fn drop(&mut self) {
		match &mut self.inner {
			InnerListener::Unix(usock) => match usock.local_addr() {
				Ok(addr) => {
					if let Some(path) = addr.as_pathname() {
						_ = std::fs::remove_file(path);
					} else {
						eprintln!("unable to path the usock"); // do not crash in Drop
					}
				}
				Err(e) => eprintln!("{e}"), // do not crash in Drop
			},
			#[cfg(not(target_os = "macos"))]
			InnerListener::Vsock(_vsock) => {} // vsock's drop will clear this
		}
	}
}

async fn unix_connect(
	addr: &SocketAddress,
) -> Result<UnixStream, std::io::Error> {
	let addr = addr.usock();
	let path = addr.path().ok_or(IOError::ConnectAddressInvalid)?;

	let socket = UnixSocket::new_stream()?;
	socket.connect(path).await
}

// raw vsock socket connect
#[cfg(not(target_os = "macos"))]
async fn vsock_connect(
	addr: &SocketAddress,
) -> Result<VsockStream, std::io::Error> {
	let addr = addr.vsock();
	VsockStream::connect(*addr).await
}

#[cfg(test)]
mod test {

	use super::*;
	use crate::{client::SocketClient, io::StreamPool};
	use std::{str::from_utf8, time::Duration};

	/// Wait for a given usock file to exist and be connectible with a timeout of 5s.
	///
	/// # Panics
	/// Panics if `fs::exists` errors.
	pub async fn wait_for_usock(path: &str) {
		let addr = SocketAddress::new_unix(path);
		let pool = StreamPool::new(addr, 1).unwrap().shared();
		let client = SocketClient::new(pool, Duration::from_millis(50));

		for _ in 0..50 {
			if std::fs::exists(path).unwrap()
				&& client.try_connect().await.is_ok()
			{
				break;
			}

			tokio::time::sleep(Duration::from_millis(100)).await;
		}
	}

	// A simple test socket server which says "PONG" when you send "PING".
	// Then it kills itself.
	pub struct HarakiriPongServer {
		path: String,
	}

	impl Drop for HarakiriPongServer {
		fn drop(&mut self) {
			let _ = std::fs::remove_file(&self.path);
		}
	}

	impl HarakiriPongServer {
		pub fn new(path: String) -> Self {
			// in case of panics, cleanup beforehand too
			let _ = std::fs::remove_file(&path);
			Self { path }
		}

		pub async fn start(&mut self) {
			let listener = UnixListener::bind(&self.path).unwrap();

			// first time accept is from "wait_for_usock" above, just ignore
			let (_stream, _peer_addr) = listener.accept().await.unwrap();

			// second accept should be for the test
			let (mut stream, _peer_addr) = listener.accept().await.unwrap();

			// Read 4 bytes ("PING")
			let mut buf = [0u8; 4];
			let r = stream.read_exact(&mut buf).await;
			eprintln!("BYTES: {buf:?}");
			r.unwrap();

			// Send "PONG" if "PING" was sent
			if from_utf8(&buf).unwrap() == "PING" {
				let _ = stream.write(b"PONG").await.unwrap();
			}
		}
	}

	#[tokio::test]
	async fn stream_integration_test() {
		// Ensure concurrent tests do not listen at the same path
		let addr: SocketAddress =
			SocketAddress::new_unix("/tmp/stream_integration_test.sock");
		let listener: Listener = Listener::listen(&addr).unwrap();
		let mut client = Stream::new(&addr);
		client.connect().await.unwrap();
		let mut server = listener.accept().await.unwrap();

		let data = vec![1, 2, 3, 4, 5, 6, 6, 6];
		client.send(&data).await.unwrap();

		let resp = server.recv().await.unwrap();

		assert_eq!(data, resp);
	}

	#[tokio::test]
	async fn stream_implements_read_write_traits() {
		let socket_server_path =
			"/tmp/stream_implements_read_write_traits.sock";

		// Start a simple socket server which replies "PONG" to any incoming
		// request
		let mut server =
			HarakiriPongServer::new(socket_server_path.to_string());

		// Start the server in its own thread
		tokio::spawn(async move {
			server.start().await;
		});

		wait_for_usock(socket_server_path).await; // wait for server

		// Now create a stream connecting to this mini-server
		let addr = SocketAddress::new_unix(socket_server_path);
		let mut pong_stream = Stream::new(&addr);
		pong_stream.connect().await.unwrap();

		// Write "PING"
		let written = pong_stream.write(b"PING").await.unwrap();
		assert_eq!(written, 4);

		// Read, and expect "PONG"
		let mut resp = [0u8; 4];
		let res = pong_stream.read(&mut resp).await.unwrap();
		assert_eq!(res, 4);
		assert_eq!(from_utf8(&resp).unwrap(), "PONG");
	}

	#[tokio::test]
	async fn listener_accept_test() {
		// Ensure concurrent tests are not attempting to listen at the same
		// address
		let addr = SocketAddress::new_unix("./listener_iterator_test.sock");

		let listener = Listener::listen(&addr).unwrap();

		let handler = tokio::spawn(async move {
			if let Ok(mut stream) = listener.accept().await {
				let req = stream.recv().await.unwrap();
				stream.send(&req).await.unwrap();
			}
		});

		let mut client = Stream::new(&addr);
		client.connect().await.unwrap();

		let data = vec![1, 2, 3, 4, 5, 6, 6, 6];
		client.send(&data).await.unwrap();
		let resp = client.recv().await.unwrap();
		assert_eq!(data, resp);

		handler.await.unwrap();
	}

	#[tokio::test]
	async fn limit_sized_payload() {
		// Ensure concurrent tests are not attempting to listen on the same socket
		let unix_addr =
			nix::sys::socket::UnixAddr::new("./limit_sized_payload.sock")
				.unwrap();
		let addr = SocketAddress::Unix(unix_addr);

		let listener = Listener::listen(&addr).unwrap();
		let handler = tokio::spawn(async move {
			if let Ok(mut stream) = listener.accept().await {
				let req = stream.recv().await.unwrap();
				stream.send(&req.clone()).await.unwrap();
			}
		});

		// Sending a request that is exactly the max size should work
		// (the response will be exactly max size)
		let mut client = Stream::new(&addr);
		client.connect().await.unwrap();

		let req = vec![1u8; MAX_PAYLOAD_SIZE];
		client.send(&req).await.unwrap();
		let resp = client.recv().await.unwrap();
		assert_eq!(resp.len(), MAX_PAYLOAD_SIZE);
		handler.await.unwrap();
	}

	#[tokio::test]
	async fn oversized_payload() {
		// Ensure concurrent tests are not attempting to listen on the same socket
		let addr = SocketAddress::new_unix("./oversized_payload.sock");
		let listener = Listener::listen(&addr).unwrap();

		// accept-only handler
		let handler = tokio::spawn(async move {
			if let Err(err) = listener.accept().await {
				panic!("{err:?}");
			}
		});

		let mut client = Stream::new(&addr);
		client.connect().await.unwrap();

		// Sending with the limit payload size + 1 will fail
		let req = vec![1u8; MAX_PAYLOAD_SIZE + 1];

		match client.send(&req).await.unwrap_err() {
			IOError::OversizedPayload(size) => {
				assert_eq!(size, MAX_PAYLOAD_SIZE + 1);
			}
			other => {
				panic!("test failed: unexpected error variant ({other:?})");
			}
		}

		handler.await.unwrap();
	}
}