Skip to content

Commit

Permalink
Merge pull request #126 from blocklessnetwork/feature/socket_fd
Browse files Browse the repository at this point in the history
Feature/socket fd
  • Loading branch information
Joinhack authored Nov 19, 2024
2 parents b2f1c1d + 885e43f commit 9a17cb9
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 11 deletions.
19 changes: 16 additions & 3 deletions blockless/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ mod context;
pub mod error;
mod modules;

use anyhow::Context;
use anyhow::{bail, Context};
use blockless_drivers::{CdylibDriver, DriverConetxt};
use blockless_env;
pub use blockless_multiaddr::MultiAddr;
Expand Down Expand Up @@ -130,20 +130,33 @@ impl BlocklessConfig2Preview1WasiBuilder for BlocklessConfig {
args.extend_from_slice(&b_conf.stdin_args_ref()[..]);
builder.args(&args[..])?;
builder.envs(&b_conf.envs_ref()[..])?;
let mut max_fd = 3;
// map host to guest dir in runtime.
for (host, guest) in b_conf.dirs.iter() {
let host = Dir::open_ambient_dir(host, ambient_authority())?;
builder.preopened_dir(host, guest)?;
max_fd += 1;
}
// map root fs
if let Some(d) = root_dir {
builder.preopened_dir(d, "/")?;
max_fd += 1;
}
//set the tcp listener.
for l in b_conf.tcp_listens.iter() {
for (l, fd) in b_conf.tcp_listens.iter() {
let fd = if let Some(fd) = fd {
if *fd < max_fd {
bail!("the invalid fd{fd} for listenfd.");
}
*fd
} else {
let fd = max_fd;
max_fd += 1;
fd
};
let l = std::net::TcpListener::bind(l)?;
let l = TcpListener::from_std(l);
builder.push_prepush_socket(l)?;
builder.preopened_socket(fd, l)?;
}

anyhow::Ok(builder)
Expand Down
25 changes: 18 additions & 7 deletions bls-runtime/src/cli_clap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use clap::{
use std::{
collections::HashMap,
net::{IpAddr, SocketAddr, TcpListener, ToSocketAddrs},
option,
path::{Path, PathBuf},
str::FromStr,
};
Expand Down Expand Up @@ -62,7 +63,7 @@ const V86_HELP: &str =
const THREAD_SUPPORT_HELP: &str =
"the thread support flag when the flag setting the runtime will support multi-threads.";

const TCP_LISTEN_HELP: &str = "grant access to the given TCP listen socket";
const TCP_LISTEN_HELP: &str = "grant access to the given TCP listen socket. ";

fn parse_envs(envs: &str) -> Result<(String, String)> {
let parts: Vec<_> = envs.splitn(2, "=").collect();
Expand Down Expand Up @@ -144,10 +145,20 @@ fn parse_stdin(stdin: &str) -> Result<Stdin> {
}
}

fn parse_listen(s: &str) -> Result<SocketAddr> {
let addrs = s.to_socket_addrs()?;
for addr in addrs {
return Ok(addr);
fn parse_listen(s: &str) -> Result<(SocketAddr, Option<u32>)> {
let splitn = s.splitn(2, "::");
let saddrs = splitn.collect::<Vec<_>>();
if saddrs.len() < 2 {
let addrs = s.to_socket_addrs()?;
for addr in addrs {
return Ok((addr, None));
}
} else {
let port: u32 = saddrs[1].parse()?;
let addrs = saddrs[0].to_socket_addrs()?;
for addr in addrs {
return Ok((addr, Some(port)));
}
}
bail!("could not resolve to any addresses")
}
Expand Down Expand Up @@ -237,8 +248,8 @@ pub(crate) struct CliCommandOpts {
#[clap(long = "module", value_name = "MODULE-NAME=MODULE-PATH", help = MODULES_HELP, value_parser = parse_module)]
modules: Vec<BlocklessModule>,

#[clap(long = "tcplisten", help = TCP_LISTEN_HELP, value_parser = parse_listen)]
tcp_listens: Vec<SocketAddr>,
#[clap(long = "tcplisten", value_name = "TCPLISTEN[::LISTENFD]", help = TCP_LISTEN_HELP, value_parser = parse_listen)]
tcp_listens: Vec<(SocketAddr, Option<u32>)>,

#[clap(value_name = "ARGS", help = APP_ARGS_HELP)]
args: Vec<String>,
Expand Down
3 changes: 2 additions & 1 deletion crates/wasi-common/src/blockless/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ pub struct BlocklessConfig {
pub drivers: Vec<DriverConfig>,
pub store_limited: StoreLimited,
pub envs: Vec<(String, String)>,
pub tcp_listens: Vec<SocketAddr>,
pub tcp_listens: Vec<(SocketAddr, Option<u32>)>,
pub permisions: Vec<Permission>,
pub dirs: Vec<(String, String)>,
pub fs_root_path: Option<String>,
Expand Down Expand Up @@ -413,6 +413,7 @@ impl BlocklessConfig {
//vm instruction limit.
limited_fuel: None,
limited_time: None,
// define the base fd
tcp_listens: Vec::new(),
stdin_args: Vec::new(),
//memory limit, 1 page = 64k.
Expand Down

0 comments on commit 9a17cb9

Please sign in to comment.