Skip to content

Commit

Permalink
feat : add writer implementation
Browse files Browse the repository at this point in the history
Signed-off-by: Maxime <[email protected]>
  • Loading branch information
Maxtho8 committed Mar 4, 2023
1 parent 47e294f commit 2cc0e25
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 9 deletions.
20 changes: 17 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ struct VMMOpts {

/// Stdout console file path
#[clap(long)]
console: Option<String>
console: Option<String>,
}

#[derive(Debug)]
Expand All @@ -47,8 +47,22 @@ fn main() -> Result<(), Error> {
// * Memory size (in MB)
// * Path to a Linux kernel
// * Optional path to console file
vmm.configure(opts.cpus, opts.memory, &opts.kernel, opts.console)
.map_err(Error::VmmConfigure)?;
vmm.configure(
opts.cpus,
opts.memory,
&opts.kernel,
opts.console,
Option::None,
)
.map_err(Error::VmmConfigure)?;

// To use Writer with serial device :
// * Create mpsc channel :
// let (tx, rx) = std::sync::mpsc::channel();
// * Create a new Writer
// let writer = Writer::new(tx);
// * Add the Writer when configuring the VMM
// * Use the rx receiver to read the data

// Run the VMM
vmm.run().map_err(Error::VmmRun)?;
Expand Down
27 changes: 27 additions & 0 deletions src/vmm/src/devices/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,30 @@
// SPDX-License-Identifier: Apache-2.0

use std::io::{Result, Write};
use std::sync::mpsc;

pub(crate) mod serial;

pub struct Writer {
tx: mpsc::Sender<String>,
}

impl Write for Writer {
fn write(&mut self, buf: &[u8]) -> Result<usize> {
let s = String::from_utf8_lossy(buf);
self.tx
.send(s.to_string())
.map_err(|_| std::io::Error::new(std::io::ErrorKind::Other, "Error sending data"))?;
Ok(buf.len())
}

fn flush(&mut self) -> Result<()> {
Ok(())
}
}

impl Writer {
pub fn new(tx: mpsc::Sender<String>) -> Self {
Writer { tx }
}
}
28 changes: 22 additions & 6 deletions src/vmm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@ extern crate linux_loader;
extern crate vm_memory;
extern crate vm_superio;

use std::fs::File;
use std::io::stdout;
use std::os::unix::io::AsRawFd;
use std::os::unix::prelude::RawFd;
use std::sync::{Arc, Mutex};
use std::thread;
use std::{io, path::PathBuf};
use std::fs::File;

use devices::Writer;
use kvm_bindings::{kvm_userspace_memory_region, KVM_MAX_CPUID_ENTRIES};
use kvm_ioctls::{Kvm, VmFd};
use linux_loader::loader::{self, KernelLoaderResult};
Expand Down Expand Up @@ -66,6 +67,8 @@ pub enum Error {
TerminalConfigure(kvm_ioctls::Error),
/// Console configuration error
ConsoleError(io::Error),
/// Writer configuration error
WriterError(io::Error),
}

/// Dedicated [`Result`](https://doc.rust-lang.org/std/result/) type.
Expand Down Expand Up @@ -164,10 +167,15 @@ impl VMM {
Ok(())
}

pub fn configure_console(
&mut self,
console_path: Option<String>
) -> Result<()> {
pub fn configure_writer(&mut self, writer: Option<Writer>) -> Result<()> {
if let Some(writer) = writer {
let mut serial = self.serial.lock().unwrap();
*serial = LumperSerial::new(Box::new(writer)).map_err(Error::WriterError)?;
}
Ok(())
}

pub fn configure_console(&mut self, console_path: Option<String>) -> Result<()> {
if let Some(console_path) = console_path {
// We create the file if it does not exist, else we open
let file = File::create(&console_path).map_err(Error::ConsoleError)?;
Expand Down Expand Up @@ -266,8 +274,16 @@ impl VMM {
}
}

pub fn configure(&mut self, num_vcpus: u8, mem_size_mb: u32, kernel_path: &str, console: Option<String>) -> Result<()> {
pub fn configure(
&mut self,
num_vcpus: u8,
mem_size_mb: u32,
kernel_path: &str,
console: Option<String>,
writer: Option<Writer>,
) -> Result<()> {
self.configure_console(console)?;
self.configure_writer(writer);
self.configure_memory(mem_size_mb)?;
let kernel_load = kernel::kernel_setup(&self.guest_memory, PathBuf::from(kernel_path))?;
self.configure_io()?;
Expand Down

0 comments on commit 2cc0e25

Please sign in to comment.