Skip to content

Commit

Permalink
feat: send code through cli and api to vm
Browse files Browse the repository at this point in the history
Signed-off-by: Mauran <thomas.mauran@etu.umontpellier.fr>
  • Loading branch information
thomas-mauran committed May 1, 2024
1 parent 60956dc commit 992ff55
Show file tree
Hide file tree
Showing 10 changed files with 81 additions and 42 deletions.
7 changes: 3 additions & 4 deletions proto/vmm.proto
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,9 @@ service VmmService {
}

message RunVmmRequest {

Language language = 1;
string code = 2;
string env = 3;
string workload_name = 1;
Language language = 2;
string code = 3;
LogLevel log_level = 4;

}
Expand Down
2 changes: 1 addition & 1 deletion src/agent/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use tonic::transport::Server;

#[derive(Debug, Parser)]
struct Args {
#[clap(long, env, default_value = "localhost")]
#[clap(long, env, default_value = "0.0.0.0")]
grpc_server_address: String,
#[clap(long, env, default_value = "50051")]
grpc_server_port: u16,
Expand Down
13 changes: 10 additions & 3 deletions src/api/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use actix_web::{post, web, HttpResponse, Responder};
use actix_web_lab::sse;
use async_stream::stream;
use serde::Serialize;
use shared_models::CloudletDtoRequest;
use shared_models::{CloudletDtoRequest, Language};
use tokio_stream::StreamExt;
use tonic::Streaming;

Expand All @@ -16,10 +16,17 @@ pub async fn run(req_body: web::Json<CloudletDtoRequest>) -> impl Responder {

let mut client = VmmClient::new().await.unwrap();

println!("Request: {:?}", req);

let vmm_request = RunVmmRequest {
workload_name: req.workload_name,
code: req.code,
env: req.env,
language: req.language as i32,
language: match req.language {
Language::PYTHON => 0,
Language::NODE => 1,
Language::RUST => 2,
_ => unreachable!("Invalid language")
},
log_level: req.log_level as i32,
};

Expand Down
17 changes: 9 additions & 8 deletions src/cli/src/services.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,25 @@ use std::{error::Error, path::PathBuf};
#[derive(Deserialize)]
struct TomlConfig {
#[serde(rename = "workload-name")]
_workload_name: String,
workload_name: String,
language: Language,
_action: String,
_server: ServerConfig,
action: String,
server: ServerConfig,
build: BuildConfig,
}

#[derive(Deserialize)]
struct ServerConfig {
_address: String,
_port: u16,
address: String,
port: u16,
}

#[derive(Deserialize)]
struct BuildConfig {
#[serde(rename = "source-code-path")]
source_code_path: PathBuf,
_release: bool,
#[serde(rename = "release")]
release: bool,
}

pub struct CloudletClient {}
Expand All @@ -34,15 +35,15 @@ impl CloudletClient {
let config: TomlConfig =
toml::from_str(&config).expect("Error while parsing the config file");

let workload_name = config.workload_name;
let code: String = ConfigFileHandler::read_file(&config.build.source_code_path)
.expect("Error while reading the code file");
let env = "";

let language = config.language;
CloudletDtoRequest {
workload_name,
language,
code,
env: env.to_string(),
log_level: shared_models::LogLevel::INFO,
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/shared-models/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,16 @@ pub enum LogLevel {

#[derive(Deserialize, Debug)]
pub struct TomlClientConfigFile {
pub worklaod_name: String,
pub language: Language,
pub env_path: PathBuf,
pub code_path: PathBuf,
pub log_level: LogLevel,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct CloudletDtoRequest {
pub workload_name: String,
pub language: Language,
pub env: String,
pub code: String,
pub log_level: LogLevel,
}
Expand Down
10 changes: 7 additions & 3 deletions src/vmm/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,15 @@ pub struct CliArguments {

/// IPv4 address of the host tap interface.
#[clap(long, env, required = true)]
pub network_host_ip: Ipv4Addr,
pub iface_host_addr: Ipv4Addr,

/// Subnet mask of the host tap interface.
/// Subnet mask for network.
#[clap(long, env, required = true)]
pub network_host_netmask: Ipv4Addr,
pub netmask: Ipv4Addr,

/// IPv4 address of the guest eth0 interface.
#[clap(long, env, required = true)]
pub iface_guest_addr: Ipv4Addr,

/// Verbosity level.
#[command(flatten)]
Expand Down
17 changes: 12 additions & 5 deletions src/vmm/src/core/devices/virtio/net/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,9 @@ impl Net {
mem: Arc<GuestMemoryMmap>,
device_mgr: Arc<Mutex<IoManager>>,
mmio_cfg: MmioConfig,
ip_addr: Ipv4Addr,
mask: Ipv4Addr,
tap_addr: Ipv4Addr,
netmask: Ipv4Addr,
iface_guest_addr: Ipv4Addr,
irq: u32,
endpoint: RemoteEndpoint<Subscriber>,
vm_fd: Arc<VmFd>,
Expand Down Expand Up @@ -73,7 +74,7 @@ impl Net {

// Set offload flags to match the relevant virtio features of the device (for now,
// statically set in the constructor.
let tap = open_tap(None, Some(ip_addr), Some(mask), &mut None, None, None)
let tap = open_tap(None, Some(tap_addr), Some(netmask), &mut None, None, None)
.map_err(Error::TunTap)?;

// The layout of the header is specified in the standard and is 12 bytes in size. We
Expand All @@ -87,9 +88,15 @@ impl Net {
tap: Arc::new(Mutex::new(tap)),
}));

let param = register_mmio_device(mmio_cfg, device_mgr, irq, None, net.clone())
let vmmio_param = register_mmio_device(mmio_cfg, device_mgr, irq, None, net.clone())
.map_err(Error::Virtio)?;
cmdline_extra_parameters.push(param);
let ip_pnp_param: String = format!(
"ip={}::{}:{}::eth0:off",
iface_guest_addr, tap_addr, netmask
);

cmdline_extra_parameters.push(vmmio_param);
cmdline_extra_parameters.push(ip_pnp_param);

Ok(net)
}
Expand Down
17 changes: 10 additions & 7 deletions src/vmm/src/core/vmm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,9 @@ pub struct VMM {
event_mgr: EventMgr,
vcpus: Vec<Vcpu>,

tap_ip_addr: Ipv4Addr,
tap_netmask: Ipv4Addr,
tap_addr: Ipv4Addr,
netmask: Ipv4Addr,
iface_guest_addr: Ipv4Addr,
net_devices: Vec<Arc<Mutex<Net>>>,
serial: Arc<Mutex<LumperSerial<Stdout>>>,
slip_pty: Arc<Mutex<SlipPty>>,
Expand All @@ -68,7 +69,7 @@ pub struct VMM {

impl VMM {
/// Create a new VMM.
pub fn new(tap_ip_addr: Ipv4Addr, tap_netmask: Ipv4Addr) -> Result<Self> {
pub fn new(tap_addr: Ipv4Addr, netmask: Ipv4Addr, iface_guest_addr: Ipv4Addr) -> Result<Self> {
// Open /dev/kvm and get a file descriptor to it.
let kvm = Kvm::new().map_err(Error::KvmIoctl)?;

Expand Down Expand Up @@ -110,8 +111,9 @@ impl VMM {
)),
slip_pty: Arc::new(Mutex::new(slip_pty)),
epoll,
tap_ip_addr,
tap_netmask,
tap_addr,
netmask,
iface_guest_addr,
net_devices: Vec::new(),
};

Expand Down Expand Up @@ -380,8 +382,9 @@ impl VMM {
mem,
self.device_mgr.clone(),
mmio_cfg,
self.tap_ip_addr,
self.tap_netmask,
self.tap_addr,
self.netmask,
self.iface_guest_addr,
irq,
self.event_mgr.lock().unwrap().remote_endpoint(),
self.vm_fd.clone(),
Expand Down
26 changes: 20 additions & 6 deletions src/vmm/src/grpc/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,12 @@ impl VmmServiceTrait for VmmService {
type RunStream =
ReceiverStream<std::result::Result<vmmorchestrator::ExecuteResponse, tonic::Status>>;

async fn run(&self, _request: Request<RunVmmRequest>) -> Result<Self::RunStream> {
async fn run(&self, request: Request<RunVmmRequest>) -> Result<Self::RunStream> {
let (tx, rx) = tokio::sync::mpsc::channel(4);

const HOST_IP: Ipv4Addr = Ipv4Addr::new(172, 29, 0, 1);
const VM_IP: Ipv4Addr = Ipv4Addr::new(172, 29, 0, 2);
const HOST_NETMASK: Ipv4Addr = Ipv4Addr::new(255, 255, 0, 0);
const GUEST_IP: Ipv4Addr = Ipv4Addr::new(172, 29, 0, 2);

// Check if the kernel is on the system, else build it
if !Path::new("./tools/kernel/linux-cloud-hypervisor/arch/x86/boot/compressed/vmlinux.bin")
Expand Down Expand Up @@ -77,7 +77,7 @@ impl VmmServiceTrait for VmmService {
initramfs_path.push("./tools/rootfs/initramfs.img");

// // Create a new VMM
let mut vmm = VMM::new(HOST_IP, HOST_NETMASK).map_err(VmmErrors::VmmNew)?;
let mut vmm = VMM::new(HOST_IP, HOST_NETMASK, GUEST_IP).map_err(VmmErrors::VmmNew)?;

// Configure the VMM parameters might need to be calculated rather than hardcoded
vmm.configure(1, 512, kernel_path, &Some(initramfs_path))
Expand All @@ -96,20 +96,34 @@ impl VmmServiceTrait for VmmService {
tokio::time::sleep(Duration::from_secs(2)).await;
println!("Connecting to Agent service");

WorkloadClient::new(VM_IP, 50051).await
WorkloadClient::new(GUEST_IP, 50051).await
})
.await
.unwrap();

// Send the grpc request to start the agent
let execute_request = ExecuteRequest {};
let vmm_request = request.into_inner();
println!("HERRREEEE {}", vmm_request.language);
let agent_request = ExecuteRequest {
workload_name: vmm_request.workload_name,
language: match vmm_request.language {
0 => "python".to_string(),
1 => "node".to_string(),
2 => "rust".to_string(),
_ => unreachable!("Invalid language")
},
action: 2, // Prepare and run
code: vmm_request.code,
config_str: "[build]\nrelease = true".to_string(),
};


match grpc_client {
Ok(mut client) => {
info!("Successfully connected to Agent service");

// Start the execution
let mut response_stream = client.execute(execute_request).await?;
let mut response_stream = client.execute(agent_request).await?;

// Process each message as it arrives
while let Some(response) = response_stream.message().await? {
Expand Down
10 changes: 7 additions & 3 deletions src/vmm/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,13 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
.with_max_level(cli_args.convert_log_to_tracing())
.init();
// Create a new VMM
let mut vmm = VMM::new(cli_args.network_host_ip, cli_args.network_host_netmask)
.map_err(VmmErrors::VmmNew)
.unwrap();
let mut vmm = VMM::new(
cli_args.iface_host_addr,
cli_args.netmask,
cli_args.iface_guest_addr,
)
.map_err(VmmErrors::VmmNew)
.unwrap();

vmm.configure(
cli_args.cpus,
Expand Down

0 comments on commit 992ff55

Please sign in to comment.