Skip to content

Commit

Permalink
wip : unix socket
Browse files Browse the repository at this point in the history
  • Loading branch information
Maxtho8 committed Mar 31, 2023
1 parent 91fa6ad commit ce272e0
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 51 deletions.
52 changes: 24 additions & 28 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{io::Read, os::unix::net::UnixListener, thread::sleep, u32};
use std::{io::Read, os::unix::net::UnixListener, path::Path, thread::sleep, u32};

use clap::Parser;
use vmm::{devices::Writer, VMM};
Expand All @@ -25,6 +25,9 @@ struct VMMOpts {
/// Stdout console file path
#[clap(long)]
console: Option<String>,

#[clap(long)]
socket: bool,
}

#[derive(Debug)]
Expand All @@ -39,37 +42,31 @@ pub enum Error {
fn main() -> Result<(), Error> {
let opts: VMMOpts = VMMOpts::parse();

let socket_path = "lambdo.sock";

if std::fs::metadata(socket_path).is_ok() {
println!("A socket is already present. Deleting...");
std::fs::remove_file(socket_path).unwrap();
}
let console = opts.console.unwrap();
if opts.socket {
let path = Path::new(console.as_str());
if std::fs::metadata(path).is_ok() {
std::fs::remove_file(path).unwrap();
}

let unix_listener = UnixListener::bind(socket_path).unwrap();
println!("Socket path: {}", path.to_str().unwrap());
let unix_listener = UnixListener::bind(path).unwrap();

std::thread::spawn(move || {
loop {
let (mut unix_stream, socket_address) = unix_listener.accept().unwrap();
// print stream
let mut message = String::new();
unix_stream.read_to_string(&mut message).unwrap();
std::thread::spawn(move || {
loop {
let (mut unix_stream, socket_address) = unix_listener.accept().unwrap();
// print stream
let mut message = String::new();
unix_stream.read_to_string(&mut message).unwrap();

print!("{}", message);
}
});
print!("{}.", message);
}
});
}

// Create a new VMM
let mut vmm = VMM::new().map_err(Error::VmmNew)?;

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

// Configure the VMM:
// * Number of virtual CPUs
// * Memory size (in MB)
Expand All @@ -79,12 +76,11 @@ fn main() -> Result<(), Error> {
opts.cpus,
opts.memory,
&opts.kernel,
opts.console,
Some(writer),
Some(console),
opts.socket,
)
.map_err(Error::VmmConfigure)?;


// Run the VMM
vmm.run().map_err(Error::VmmRun)?;

Expand Down
12 changes: 5 additions & 7 deletions src/vmm/src/devices/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,14 @@ use std::os::unix::net::UnixStream;
pub(crate) mod serial;

pub struct Writer {
unix_stream: UnixStream,
}

impl Write for Writer {
fn write(&mut self, buf: &[u8]) -> Result<usize> {
let mut unix_stream = UnixStream::connect("lambdo.sock").unwrap();
let s = String::from_utf8_lossy(buf).to_string();
let _ = &self.unix_stream.write(s.as_bytes()).unwrap();

if buf.len() > 0 && (buf[0] != 10 && buf[0] != 13) {
let s = String::from_utf8_lossy(buf).to_string();
unix_stream.write(s.as_bytes()).unwrap();
}
Ok(buf.len())
}

Expand All @@ -25,7 +23,7 @@ impl Write for Writer {
}

impl Writer {
pub fn new() -> Self {
Writer { }
pub fn new(unix_stream: UnixStream) -> Self {
Writer { unix_stream }
}
}
40 changes: 24 additions & 16 deletions src/vmm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ extern crate vm_superio;
use std::fs::File;
use std::io::stdout;
use std::os::unix::io::AsRawFd;
use std::os::unix::net::UnixStream;
use std::os::unix::prelude::RawFd;
use std::sync::{Arc, Mutex};
use std::thread;
Expand Down Expand Up @@ -167,21 +168,29 @@ impl VMM {
Ok(())
}

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<()> {
pub fn configure_console(
&mut self,
console_path: Option<String>,
is_socket: bool,
) -> 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)?;
if is_socket {
println!("Connecting to socket: {}", console_path);
let unix_stream = UnixStream::connect(console_path).unwrap();

// create writer

let writer = Writer::new(unix_stream);
let mut serial = self.serial.lock().unwrap();

let mut serial = self.serial.lock().unwrap();
*serial = LumperSerial::new(Box::new(file)).map_err(Error::SerialCreation)?;
*serial = LumperSerial::new(Box::new(writer)).map_err(Error::SerialCreation)?;
} else {
// We create the file if it does not exist, else we open
let file = File::create(&console_path).map_err(Error::ConsoleError)?;

let mut serial = self.serial.lock().unwrap();
*serial = LumperSerial::new(Box::new(file)).map_err(Error::SerialCreation)?;
}
}

Ok(())
Expand Down Expand Up @@ -280,10 +289,9 @@ impl VMM {
mem_size_mb: u32,
kernel_path: &str,
console: Option<String>,
writer: Option<Writer>,
is_socket: bool,
) -> Result<()> {
self.configure_console(console)?;
self.configure_writer(writer);
self.configure_console(console, is_socket)?;
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 ce272e0

Please sign in to comment.