1use std::fs::File;
6
7use base::ioctl_with_ref;
8use base::AsRawDescriptor;
9use base::RawDescriptor;
10use virtio_sys::VHOST_VSOCK_SET_GUEST_CID;
11use virtio_sys::VHOST_VSOCK_SET_RUNNING;
12
13use super::ioctl_result;
14use super::Result;
15use super::Vhost;
16
17pub struct Vsock {
19 descriptor: File,
20}
21
22impl Vsock {
23 pub fn new(vhost_vsock_file: File) -> Vsock {
25 Vsock {
26 descriptor: vhost_vsock_file,
27 }
28 }
29
30 pub fn set_cid(&self, cid: u64) -> Result<()> {
37 let ret = unsafe { ioctl_with_ref(&self.descriptor, VHOST_VSOCK_SET_GUEST_CID, &cid) };
39 if ret < 0 {
40 return ioctl_result();
41 }
42 Ok(())
43 }
44
45 pub fn start(&self) -> Result<()> {
47 self.set_running(true)
48 }
49
50 pub fn stop(&self) -> Result<()> {
52 self.set_running(false)
53 }
54
55 fn set_running(&self, running: bool) -> Result<()> {
56 let on = ::std::os::raw::c_int::from(running);
57 let ret = unsafe { ioctl_with_ref(&self.descriptor, VHOST_VSOCK_SET_RUNNING, &on) };
59
60 if ret < 0 {
61 return ioctl_result();
62 }
63 Ok(())
64 }
65}
66
67impl Vhost for Vsock {}
68
69impl AsRawDescriptor for Vsock {
70 fn as_raw_descriptor(&self) -> RawDescriptor {
71 self.descriptor.as_raw_descriptor()
72 }
73}