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
// Copyright 2017 The ChromiumOS Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
use std::fs::File;
use base::ioctl_with_ref;
use base::AsRawDescriptor;
use base::RawDescriptor;
use virtio_sys::VHOST_VSOCK_SET_GUEST_CID;
use virtio_sys::VHOST_VSOCK_SET_RUNNING;
use super::ioctl_result;
use super::Result;
use super::Vhost;
/// Handle for running VHOST_VSOCK ioctls.
pub struct Vsock {
    descriptor: File,
}
impl Vsock {
    /// Open a handle to a new VHOST_VSOCK instance.
    pub fn new(vhost_vsock_file: File) -> Vsock {
        Vsock {
            descriptor: vhost_vsock_file,
        }
    }
    /// Set the CID for the guest.  This number is used for routing all data destined for
    /// programs
    /// running in the guest.
    ///
    /// # Arguments
    /// * `cid` - CID to assign to the guest
    pub fn set_cid(&self, cid: u64) -> Result<()> {
        // SAFETY: Safe because descriptor is valid and the return value is checked.
        let ret = unsafe { ioctl_with_ref(&self.descriptor, VHOST_VSOCK_SET_GUEST_CID, &cid) };
        if ret < 0 {
            return ioctl_result();
        }
        Ok(())
    }
    /// Tell the VHOST driver to start performing data transfer.
    pub fn start(&self) -> Result<()> {
        self.set_running(true)
    }
    /// Tell the VHOST driver to stop performing data transfer.
    pub fn stop(&self) -> Result<()> {
        self.set_running(false)
    }
    fn set_running(&self, running: bool) -> Result<()> {
        let on = ::std::os::raw::c_int::from(running);
        // SAFETY: Safe because descriptor is valid and the return value is checked.
        let ret = unsafe { ioctl_with_ref(&self.descriptor, VHOST_VSOCK_SET_RUNNING, &on) };
        if ret < 0 {
            return ioctl_result();
        }
        Ok(())
    }
}
impl Vhost for Vsock {}
impl AsRawDescriptor for Vsock {
    fn as_raw_descriptor(&self) -> RawDescriptor {
        self.descriptor.as_raw_descriptor()
    }
}