summaryrefslogtreecommitdiff
path: root/src/vhost_user/slave.rs
diff options
context:
space:
mode:
authorLiu Jiang <gerry@linux.alibaba.com>2019-04-29 14:30:04 +0800
committerAndreea Florescu <andreea.florescu15@gmail.com>2020-09-04 17:59:53 +0300
commit7c1cd619eb659ed91e5060ba888813ac6c795cc6 (patch)
tree3d97217e4530da35e0f01e62c9d56bea615d5f8e /src/vhost_user/slave.rs
parentac2c67a6ae3fb1b257ba46fae49841907bf7f22d (diff)
downloadvmm_vhost-7c1cd619eb659ed91e5060ba888813ac6c795cc6.tar.gz
Implement vhost-user slave side endpoint
Signed-off-by: Liu Jiang <gerry@linux.alibaba.com> Signed-off-by: Dr. David Alan Gilbert <dgilbert@redhat.com> Signed-off-by: Daniel Prilik <daniel@prilik.com>
Diffstat (limited to 'src/vhost_user/slave.rs')
-rw-r--r--src/vhost_user/slave.rs48
1 files changed, 48 insertions, 0 deletions
diff --git a/src/vhost_user/slave.rs b/src/vhost_user/slave.rs
new file mode 100644
index 0000000..3f097b8
--- /dev/null
+++ b/src/vhost_user/slave.rs
@@ -0,0 +1,48 @@
+// Copyright (C) 2019 Alibaba Cloud Computing. All rights reserved.
+// SPDX-License-Identifier: Apache-2.0
+
+//! Traits and Structs for vhost-user slave.
+
+use std::sync::{Arc, Mutex};
+
+use super::connection::{Endpoint, Listener};
+use super::message::*;
+use super::{Result, SlaveReqHandler, VhostUserSlaveReqHandler};
+
+/// Vhost-user slave side connection listener.
+pub struct SlaveListener<S: VhostUserSlaveReqHandler> {
+ listener: Listener,
+ backend: Option<Arc<Mutex<S>>>,
+}
+
+/// Sets up a listener for incoming master connections, and handles construction
+/// of a Slave on success.
+impl<S: VhostUserSlaveReqHandler> SlaveListener<S> {
+ /// Create a unix domain socket for incoming master connections.
+ ///
+ /// Be careful, the file at `path` will be unlinked if unlink is true
+ pub fn new(path: &str, unlink: bool, backend: Arc<Mutex<S>>) -> Result<Self> {
+ Ok(SlaveListener {
+ listener: Listener::new(path, unlink)?,
+ backend: Some(backend),
+ })
+ }
+
+ /// Accept an incoming connection from the master, returning Some(Slave) on
+ /// success, or None if the socket is nonblocking and no incoming connection
+ /// was detected
+ pub fn accept(&mut self) -> Result<Option<SlaveReqHandler<S>>> {
+ if let Some(fd) = self.listener.accept()? {
+ return Ok(Some(SlaveReqHandler::new(
+ Endpoint::<MasterReq>::from_stream(fd),
+ self.backend.take().unwrap(),
+ )));
+ }
+ Ok(None)
+ }
+
+ /// Change blocking status on the listener.
+ pub fn set_nonblocking(&self, block: bool) -> Result<()> {
+ self.listener.set_nonblocking(block)
+ }
+}