mirror of
https://github.com/edera-dev/krata.git
synced 2025-08-03 21:21:32 +00:00
krata: reorganize crates
This commit is contained in:
26
crates/krata/Cargo.toml
Normal file
26
crates/krata/Cargo.toml
Normal file
@ -0,0 +1,26 @@
|
||||
[package]
|
||||
name = "krata"
|
||||
version.workspace = true
|
||||
edition = "2021"
|
||||
resolver = "2"
|
||||
|
||||
[dependencies]
|
||||
anyhow = { workspace = true }
|
||||
libc = { workspace = true }
|
||||
log = { workspace = true }
|
||||
nix = { workspace = true, features = ["ioctl", "socket"] }
|
||||
prost = { workspace = true }
|
||||
serde = { workspace = true }
|
||||
tokio = { workspace = true }
|
||||
tonic = { workspace = true }
|
||||
url = { workspace = true }
|
||||
|
||||
[build-dependencies]
|
||||
tonic-build = { workspace = true }
|
||||
|
||||
[lib]
|
||||
name = "krata"
|
||||
|
||||
[[example]]
|
||||
name = "ethtool"
|
||||
path = "examples/ethtool.rs"
|
5
crates/krata/build.rs
Normal file
5
crates/krata/build.rs
Normal file
@ -0,0 +1,5 @@
|
||||
fn main() {
|
||||
tonic_build::configure()
|
||||
.compile(&["proto/krata/control.proto"], &["proto"])
|
||||
.unwrap();
|
||||
}
|
13
crates/krata/examples/ethtool.rs
Normal file
13
crates/krata/examples/ethtool.rs
Normal file
@ -0,0 +1,13 @@
|
||||
use std::env;
|
||||
|
||||
use anyhow::Result;
|
||||
use krata::ethtool::EthtoolHandle;
|
||||
|
||||
fn main() -> Result<()> {
|
||||
let args = env::args().collect::<Vec<String>>();
|
||||
let interface = args.get(1).unwrap();
|
||||
let mut handle = EthtoolHandle::new()?;
|
||||
handle.set_gso(interface, false)?;
|
||||
handle.set_tso(interface, false)?;
|
||||
Ok(())
|
||||
}
|
79
crates/krata/proto/krata/control.proto
Normal file
79
crates/krata/proto/krata/control.proto
Normal file
@ -0,0 +1,79 @@
|
||||
syntax = "proto3";
|
||||
|
||||
option java_multiple_files = true;
|
||||
option java_package = "dev.krata.proto.control";
|
||||
option java_outer_classname = "ControlProto";
|
||||
|
||||
package krata.control;
|
||||
|
||||
message GuestInfo {
|
||||
string id = 1;
|
||||
string image = 2;
|
||||
string ipv4 = 3;
|
||||
string ipv6 = 4;
|
||||
}
|
||||
|
||||
message LaunchGuestRequest {
|
||||
string image = 1;
|
||||
uint32 vcpus = 2;
|
||||
uint64 mem = 3;
|
||||
repeated string env = 4;
|
||||
repeated string run = 5;
|
||||
}
|
||||
|
||||
message LaunchGuestReply {
|
||||
GuestInfo guest = 1;
|
||||
}
|
||||
|
||||
message ListGuestsRequest {}
|
||||
|
||||
message ListGuestsReply {
|
||||
repeated GuestInfo guests = 1;
|
||||
}
|
||||
|
||||
message DestroyGuestRequest {
|
||||
string guest_id = 1;
|
||||
}
|
||||
|
||||
message DestroyGuestReply {}
|
||||
|
||||
message ConsoleDataRequest {
|
||||
string guest_id = 1;
|
||||
bytes data = 2;
|
||||
}
|
||||
|
||||
message ConsoleDataReply {
|
||||
bytes data = 1;
|
||||
}
|
||||
|
||||
message WatchEventsRequest {}
|
||||
|
||||
message GuestLaunchedEvent {
|
||||
string guest_id = 1;
|
||||
}
|
||||
|
||||
message GuestDestroyedEvent {
|
||||
string guest_id = 1;
|
||||
}
|
||||
|
||||
message GuestExitedEvent {
|
||||
string guest_id = 1;
|
||||
int32 code = 2;
|
||||
}
|
||||
|
||||
message WatchEventsReply {
|
||||
oneof event {
|
||||
GuestLaunchedEvent guest_launched = 1;
|
||||
GuestDestroyedEvent guest_destroyed = 2;
|
||||
GuestExitedEvent guest_exited = 3;
|
||||
}
|
||||
}
|
||||
|
||||
service ControlService {
|
||||
rpc LaunchGuest(LaunchGuestRequest) returns (LaunchGuestReply);
|
||||
rpc DestroyGuest(DestroyGuestRequest) returns (DestroyGuestReply);
|
||||
rpc ListGuests(ListGuestsRequest) returns (ListGuestsReply);
|
||||
rpc ConsoleData(stream ConsoleDataRequest) returns (stream ConsoleDataReply);
|
||||
|
||||
rpc WatchEvents(WatchEventsRequest) returns (stream WatchEventsReply);
|
||||
}
|
1
crates/krata/src/control.rs
Normal file
1
crates/krata/src/control.rs
Normal file
@ -0,0 +1 @@
|
||||
tonic::include_proto!("krata.control");
|
100
crates/krata/src/dial.rs
Normal file
100
crates/krata/src/dial.rs
Normal file
@ -0,0 +1,100 @@
|
||||
use std::{fmt::Display, str::FromStr};
|
||||
|
||||
use anyhow::anyhow;
|
||||
use url::{Host, Url};
|
||||
|
||||
pub const KRATA_DEFAULT_TCP_PORT: u16 = 4350;
|
||||
pub const KRATA_DEFAULT_TLS_PORT: u16 = 4353;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub enum ControlDialAddress {
|
||||
UnixSocket {
|
||||
path: String,
|
||||
},
|
||||
Tcp {
|
||||
host: String,
|
||||
port: u16,
|
||||
},
|
||||
Tls {
|
||||
host: String,
|
||||
port: u16,
|
||||
insecure: bool,
|
||||
},
|
||||
}
|
||||
|
||||
impl FromStr for ControlDialAddress {
|
||||
type Err = anyhow::Error;
|
||||
|
||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||
let url: Url = s.parse()?;
|
||||
|
||||
let host = url.host().unwrap_or(Host::Domain("localhost")).to_string();
|
||||
|
||||
match url.scheme() {
|
||||
"unix" => Ok(ControlDialAddress::UnixSocket {
|
||||
path: url.path().to_string(),
|
||||
}),
|
||||
|
||||
"tcp" => {
|
||||
let port = url.port().unwrap_or(KRATA_DEFAULT_TCP_PORT);
|
||||
Ok(ControlDialAddress::Tcp { host, port })
|
||||
}
|
||||
|
||||
"tls" | "tls-insecure" => {
|
||||
let insecure = url.scheme() == "tls-insecure";
|
||||
let port = url.port().unwrap_or(KRATA_DEFAULT_TLS_PORT);
|
||||
Ok(ControlDialAddress::Tls {
|
||||
host,
|
||||
port,
|
||||
insecure,
|
||||
})
|
||||
}
|
||||
|
||||
_ => Err(anyhow!("unknown control address scheme: {}", url.scheme())),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<ControlDialAddress> for Url {
|
||||
fn from(val: ControlDialAddress) -> Self {
|
||||
match val {
|
||||
ControlDialAddress::UnixSocket { path } => {
|
||||
let mut url = Url::parse("unix:///").unwrap();
|
||||
url.set_path(&path);
|
||||
url
|
||||
}
|
||||
|
||||
ControlDialAddress::Tcp { host, port } => {
|
||||
let mut url = Url::parse("tcp://").unwrap();
|
||||
url.set_host(Some(&host)).unwrap();
|
||||
if port != KRATA_DEFAULT_TCP_PORT {
|
||||
url.set_port(Some(port)).unwrap();
|
||||
}
|
||||
url
|
||||
}
|
||||
|
||||
ControlDialAddress::Tls {
|
||||
host,
|
||||
port,
|
||||
insecure,
|
||||
} => {
|
||||
let mut url = Url::parse("tls://").unwrap();
|
||||
if insecure {
|
||||
url.set_scheme("tls-insecure").unwrap();
|
||||
}
|
||||
url.set_host(Some(&host)).unwrap();
|
||||
if port != KRATA_DEFAULT_TLS_PORT {
|
||||
url.set_port(Some(port)).unwrap();
|
||||
}
|
||||
url
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Display for ControlDialAddress {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
let url: Url = self.clone().into();
|
||||
write!(f, "{}", url)
|
||||
}
|
||||
}
|
81
crates/krata/src/ethtool.rs
Normal file
81
crates/krata/src/ethtool.rs
Normal file
@ -0,0 +1,81 @@
|
||||
use std::{
|
||||
os::fd::{AsRawFd, FromRawFd, OwnedFd},
|
||||
ptr::addr_of_mut,
|
||||
};
|
||||
|
||||
use anyhow::Result;
|
||||
use libc::{ioctl, socket, AF_INET, SOCK_DGRAM};
|
||||
|
||||
#[repr(C)]
|
||||
struct EthtoolValue {
|
||||
cmd: u32,
|
||||
data: u32,
|
||||
}
|
||||
|
||||
const ETHTOOL_SGSO: u32 = 0x00000024;
|
||||
const ETHTOOL_STSO: u32 = 0x0000001f;
|
||||
|
||||
#[cfg(not(target_env = "musl"))]
|
||||
const SIOCETHTOOL: libc::c_ulong = libc::SIOCETHTOOL;
|
||||
#[cfg(target_env = "musl")]
|
||||
const SIOCETHTOOL: libc::c_int = libc::SIOCETHTOOL as i32;
|
||||
|
||||
#[repr(C)]
|
||||
#[derive(Debug)]
|
||||
struct EthtoolIfreq {
|
||||
ifr_name: [libc::c_char; libc::IF_NAMESIZE],
|
||||
ifr_data: libc::uintptr_t,
|
||||
}
|
||||
|
||||
impl EthtoolIfreq {
|
||||
fn new(interface: &str) -> EthtoolIfreq {
|
||||
let mut ifreq = EthtoolIfreq {
|
||||
ifr_name: [0; libc::IF_NAMESIZE],
|
||||
ifr_data: 0,
|
||||
};
|
||||
for (i, byte) in interface.as_bytes().iter().enumerate() {
|
||||
ifreq.ifr_name[i] = *byte as libc::c_char
|
||||
}
|
||||
ifreq
|
||||
}
|
||||
|
||||
fn set_value(&mut self, ptr: *mut libc::c_void) {
|
||||
self.ifr_data = ptr as libc::uintptr_t;
|
||||
}
|
||||
}
|
||||
|
||||
pub struct EthtoolHandle {
|
||||
fd: OwnedFd,
|
||||
}
|
||||
|
||||
impl EthtoolHandle {
|
||||
pub fn new() -> Result<EthtoolHandle> {
|
||||
let fd = unsafe { socket(AF_INET, SOCK_DGRAM, 0) };
|
||||
if fd == -1 {
|
||||
return Err(std::io::Error::last_os_error().into());
|
||||
}
|
||||
|
||||
Ok(EthtoolHandle {
|
||||
fd: unsafe { OwnedFd::from_raw_fd(fd) },
|
||||
})
|
||||
}
|
||||
|
||||
pub fn set_gso(&mut self, interface: &str, value: bool) -> Result<()> {
|
||||
self.set_value(interface, ETHTOOL_SGSO, if value { 1 } else { 0 })
|
||||
}
|
||||
|
||||
pub fn set_tso(&mut self, interface: &str, value: bool) -> Result<()> {
|
||||
self.set_value(interface, ETHTOOL_STSO, if value { 1 } else { 0 })
|
||||
}
|
||||
|
||||
fn set_value(&mut self, interface: &str, cmd: u32, value: u32) -> Result<()> {
|
||||
let mut ifreq = EthtoolIfreq::new(interface);
|
||||
let mut value = EthtoolValue { cmd, data: value };
|
||||
ifreq.set_value(addr_of_mut!(value) as *mut libc::c_void);
|
||||
let result = unsafe { ioctl(self.fd.as_raw_fd(), SIOCETHTOOL, addr_of_mut!(ifreq) as u64) };
|
||||
if result == -1 {
|
||||
return Err(std::io::Error::last_os_error().into());
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
33
crates/krata/src/launchcfg.rs
Normal file
33
crates/krata/src/launchcfg.rs
Normal file
@ -0,0 +1,33 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
pub struct LaunchNetworkIpv4 {
|
||||
pub address: String,
|
||||
pub gateway: String,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
pub struct LaunchNetworkIpv6 {
|
||||
pub address: String,
|
||||
pub gateway: String,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
pub struct LaunchNetworkResolver {
|
||||
pub nameservers: Vec<String>,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
pub struct LaunchNetwork {
|
||||
pub link: String,
|
||||
pub ipv4: LaunchNetworkIpv4,
|
||||
pub ipv6: LaunchNetworkIpv6,
|
||||
pub resolver: LaunchNetworkResolver,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
pub struct LaunchInfo {
|
||||
pub network: Option<LaunchNetwork>,
|
||||
pub env: Option<Vec<String>>,
|
||||
pub run: Option<Vec<String>>,
|
||||
}
|
4
crates/krata/src/lib.rs
Normal file
4
crates/krata/src/lib.rs
Normal file
@ -0,0 +1,4 @@
|
||||
pub mod control;
|
||||
pub mod dial;
|
||||
pub mod ethtool;
|
||||
pub mod launchcfg;
|
Reference in New Issue
Block a user