main.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#![allow(dead_code)] | |
use std::os::fd::AsRawFd; | |
extern"C"{ | |
pubfnioctl(fildes:i32,request:i32, ...) -> i32; | |
} | |
constVNIC_IOC_INFO:i32 = 0x1710003; | |
constMAXMACADDRLEN:usize = 20; | |
constMAXPATHLEN:usize = 1024; | |
constMPT_MAXCID:usize = 32; | |
constMPT_MAXIPADDR:usize = 32; | |
constMPT_MAXCIDLEN:usize = 256; | |
constMRP_NCPUS:usize = 256; | |
#[derive(Copy,Clone,Debug,Default)] | |
#[repr(C)] | |
structVnicInfoIoc{ | |
pubvnic_id:u32, | |
publink_id:u32, | |
pubmac_addr_type:VnicMacAddrType, | |
pubmac_len:u32, | |
pubmac_addr:[u8;MAXMACADDRLEN], | |
pubmac_slot:u32, | |
pubmac_prefix_len:u32, | |
pubvid:u16, | |
pubvrid:u32, | |
pubaf:u32, | |
pubforce:bool, | |
pubresource_props:MacResourceProps, | |
} | |
#[derive(Copy,Clone,Debug,Default)] | |
#[repr(i32)] | |
enumVnicMacAddrType{ | |
#[default] | |
Unknown = -1, | |
Fixed, | |
Random, | |
Factory, | |
Auto, | |
Primary, | |
Vrid, | |
} | |
#[derive(Copy,Clone,Debug)] | |
#[repr(C, packed(4))] | |
structMacResourceProps{ | |
pubmask:u32, | |
pubmaxbw:u64, | |
pubpriority:MacPriorityLevel, | |
pubcpus:MacCPUsProps, | |
pubprotect:MacProtect, | |
pubnrxrings:u32, | |
pubntxrings:u32, | |
pubpool:[u8;MAXPATHLEN], | |
} | |
implDefaultforMacResourceProps{ | |
fndefault() -> Self{ | |
Self{ | |
mask:0, | |
maxbw:0, | |
priority:MacPriorityLevel::Low, | |
cpus:MacCPUsProps::default(), | |
protect:MacProtect::default(), | |
nrxrings:0, | |
ntxrings:0, | |
pool:[0;MAXPATHLEN], | |
} | |
} | |
} | |
#[derive(Copy,Clone,Debug)] | |
#[repr(i32)] | |
enumMacPriorityLevel{ | |
Low, | |
Medium, | |
High, | |
Reset, | |
} | |
#[derive(Copy,Clone,Debug)] | |
#[repr(C)] | |
structMacCPUsProps{ | |
pubncpus:u32, | |
pubcpus:[u32;MRP_NCPUS], | |
pubrx_fanout_cnt:u32, | |
pubrx_fanout_cpus:[u32;MRP_NCPUS], | |
pubrx_pollid:u32, | |
pubrx_workerid:u32, | |
pubrx_intr_cpu:i32, | |
pubtx_fanout_cpus:[i32;MRP_NCPUS], | |
pubtx_intr_cpus:MacTxIntrCPUs, | |
pubfanout_mode:MacCpuMode, | |
} | |
implDefaultforMacCPUsProps{ | |
fndefault() -> Self{ | |
Self{ | |
ncpus:0, | |
cpus:[0;MRP_NCPUS], | |
rx_fanout_cnt:0, | |
rx_fanout_cpus:[0;MRP_NCPUS], | |
rx_pollid:0, | |
rx_workerid:0, | |
rx_intr_cpu:0, | |
tx_fanout_cpus:[0;MRP_NCPUS], | |
tx_intr_cpus:MacTxIntrCPUs::default(), | |
fanout_mode:MacCpuMode::Fanout, | |
} | |
} | |
} | |
#[derive(Copy,Clone,Debug)] | |
#[repr(C)] | |
structMacTxIntrCPUs{ | |
pubmtc_intr_cpu:[i32;MRP_NCPUS], | |
pubmtc_retargeted_cpu:[i32;MRP_NCPUS], | |
} | |
implDefaultforMacTxIntrCPUs{ | |
fndefault() -> Self{ | |
Self{ | |
mtc_intr_cpu:[0;MRP_NCPUS], | |
mtc_retargeted_cpu:[0;MRP_NCPUS], | |
} | |
} | |
} | |
#[derive(Copy,Clone,Debug)] | |
#[repr(i32)] | |
enumMacCpuMode{ | |
Fanout = 1, | |
Cpus, | |
} | |
#[derive(Copy,Clone,Debug)] | |
#[repr(C)] | |
structMacProtect{ | |
pubtypes:u32, | |
pubipaddrcnt:u32, | |
pubipaddrs:[MacIpaddr;MPT_MAXIPADDR], | |
pubcidcnt:u32, | |
pubcids:[MacDhcpCid;MPT_MAXCID], | |
} | |
implDefaultforMacProtect{ | |
fndefault() -> Self{ | |
Self{ | |
types:0, | |
ipaddrcnt:0, | |
ipaddrs:[MacIpaddr::default();MPT_MAXIPADDR], | |
cidcnt:0, | |
cids:[MacDhcpCid::default();MPT_MAXCID], | |
} | |
} | |
} | |
#[derive(Copy,Clone,Debug,Default)] | |
#[repr(C)] | |
structMacIpaddr{ | |
pubversion:u32, | |
pubaddr:In6Addr, | |
pubnetmask:u8, | |
} | |
#[derive(Copy,Clone,Debug)] | |
#[repr(C)] | |
structIn6Addr{ | |
pubdata:In6Data, | |
} | |
implDefaultforIn6Addr{ | |
fndefault() -> Self{ | |
Self{ | |
data:In6Data{parts:[0;8]}, | |
} | |
} | |
} | |
#[repr(C)] | |
unionIn6Data{ | |
pubparts:[u16;8], | |
pubalign:u32, | |
} | |
implCopyforIn6Data{} | |
implCloneforIn6Data{ | |
fnclone(&self) -> Self{ | |
unsafe{ | |
letmut c = In6Data{parts:[0;8]}; | |
for(i, x)inself.parts.iter().enumerate(){ | |
c.parts[i] = *x; | |
} | |
c | |
} | |
} | |
} | |
impl std::fmt::DebugforIn6Data{ | |
fnfmt(&self,f:&mut std::fmt::Formatter) -> std::fmt::Result{ | |
write!(f, "In6Data {{ parts: {:?} }}", unsafe{self.parts }) | |
} | |
} | |
#[derive(Copy,Clone,Debug)] | |
#[repr(C)] | |
structMacDhcpCid{ | |
pubid:[u8;MPT_MAXCIDLEN], | |
publen:u32, | |
pubform:MacDhcpCidFrom, | |
} | |
implDefaultforMacDhcpCid{ | |
fndefault() -> Self{ | |
Self{ | |
id:[0;MPT_MAXCIDLEN], | |
len:0, | |
form:MacDhcpCidFrom::Typed, | |
} | |
} | |
} | |
#[derive(Copy,Clone,Debug)] | |
#[repr(i32)] | |
enumMacDhcpCidFrom{ | |
Typed = 1, | |
Hex, | |
Str, | |
} | |
fndld_fd() -> Result<std::fs::File, std::io::Error>{ | |
std::fs::OpenOptions::new() | |
.read(true) | |
.write(true) | |
.open("/dev/dld") | |
} | |
fnget_vnic_info(link_id:u32) -> Result<VnicInfoIoc, std::io::Error>{ | |
let fd = dld_fd()?; | |
unsafe{ | |
let arg = VnicInfoIoc{ | |
vnic_id: link_id, | |
..Default::default() | |
}; | |
let res = ioctl(fd.as_raw_fd(),VNIC_IOC_INFO,&arg); | |
eprintln!("ioctl returned {res}"); | |
if res == -1{ | |
let err = std::io::Error::last_os_error(); | |
eprintln!("ioctl failed: {err}"); | |
returnErr(err); | |
} | |
Ok(arg) | |
} | |
} | |
fnmain(){ | |
letSome(link) = std::env::args().nth(1)else{ | |
eprintln!("Usage: linkinfo <link id>"); | |
std::process::exit(1); | |
}; | |
letOk(link_id) = link.parse::<u32>()else{ | |
eprintln!("invalid link id"); | |
std::process::exit(1); | |
}; | |
matchget_vnic_info(link_id){ | |
Ok(info) => { | |
println!("{info:?}"); | |
} | |
Err(err) => { | |
eprintln!("Failed to get vnic info: {err}"); | |
} | |
} | |
} |