diff --git a/rust/Cargo.lock b/rust/Cargo.lock index 85502d3..52c6fef 100644 --- a/rust/Cargo.lock +++ b/rust/Cargo.lock @@ -34,6 +34,24 @@ dependencies = [ "linked_list_allocator", ] +[[package]] +name = "proc-macro2" +version = "1.0.86" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5e719e8df665df0d1c8fbfd238015744736151d4445ec0836b8e628aae103b77" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.36" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0fa76aaf39101c457836aec0ce2316dbdc3ab723cdda1c6bd4e6ad4208acaca7" +dependencies = [ + "proc-macro2", +] + [[package]] name = "scopeguard" version = "1.2.0" @@ -49,12 +67,38 @@ dependencies = [ "lock_api", ] +[[package]] +name = "syn" +version = "1.0.109" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "72b64191b275b66ffe2469e8af2c1cfe3bafa67b529ead792a6d0160888b4237" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + [[package]] name = "testbed" version = "0.1.0" +dependencies = [ + "mammoth", + "yellowstone", +] + +[[package]] +name = "unicode-ident" +version = "1.0.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" + +[[package]] +name = "yellowstone" +version = "0.1.0" dependencies = [ "mammoth", "yunq", + "yunq-derive", ] [[package]] @@ -62,4 +106,14 @@ name = "yunq" version = "0.1.0" dependencies = [ "mammoth", + "yunq-derive", +] + +[[package]] +name = "yunq-derive" +version = "0.1.0" +dependencies = [ + "proc-macro2", + "quote", + "syn", ] diff --git a/rust/Cargo.toml b/rust/Cargo.toml index 0eb17a7..f35d122 100644 --- a/rust/Cargo.toml +++ b/rust/Cargo.toml @@ -1,7 +1,7 @@ [workspace] members = [ - "lib/mammoth", "lib/yunq", "usr/testbed", + "lib/mammoth", "lib/yellowstone", "lib/yunq", "lib/yunq-derive", "usr/testbed", ] # the profile used for `cargo build` diff --git a/rust/lib/yellowstone/Cargo.toml b/rust/lib/yellowstone/Cargo.toml new file mode 100644 index 0000000..c464a9e --- /dev/null +++ b/rust/lib/yellowstone/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "yellowstone" +version = "0.1.0" +edition = "2021" + +[dependencies] +mammoth = { path = "../mammoth" } +yunq = {path = "../yunq"} +yunq-derive = {path = "../yunq-derive"} + diff --git a/rust/lib/yellowstone/src/lib.rs b/rust/lib/yellowstone/src/lib.rs new file mode 100644 index 0000000..d5e025f --- /dev/null +++ b/rust/lib/yellowstone/src/lib.rs @@ -0,0 +1,38 @@ +#![no_std] + +extern crate alloc; + +use alloc::string::String; +use alloc::string::ToString; +use mammoth::syscall::z_cap_t; +use mammoth::syscall::ZError; +use yunq::ByteBuffer; +use yunq::YunqMessage; +use yunq_derive::YunqMessage; + +#[derive(YunqMessage)] +pub struct GetEndpointRequest { + pub endpoint_name: String, +} + +#[derive(YunqMessage)] +pub struct Endpoint { + pub endpoint: z_cap_t, +} + +pub struct YellowstoneClient { + endpoint_cap: z_cap_t, + byte_buffer: ByteBuffer<0x1000>, +} + +impl YellowstoneClient { + pub fn new(endpoint_cap: z_cap_t) -> Self { + Self { + endpoint_cap, + byte_buffer: ByteBuffer::new(), + } + } + pub fn get_endpoint(&mut self, req: &GetEndpointRequest) -> Result { + yunq::client::call_endpoint(req, &mut self.byte_buffer, self.endpoint_cap) + } +} diff --git a/rust/lib/yunq-derive/Cargo.toml b/rust/lib/yunq-derive/Cargo.toml new file mode 100644 index 0000000..c17a506 --- /dev/null +++ b/rust/lib/yunq-derive/Cargo.toml @@ -0,0 +1,12 @@ +[package] +name = "yunq-derive" +version = "0.1.0" +edition = "2021" + +[lib] +proc-macro = true + +[dependencies] +proc-macro2 = "1.0" +quote = "1.0" +syn = { version = "1.0", features = ["full", "extra-traits", "printing"] } diff --git a/rust/lib/yunq-derive/src/lib.rs b/rust/lib/yunq-derive/src/lib.rs new file mode 100644 index 0000000..8311244 --- /dev/null +++ b/rust/lib/yunq-derive/src/lib.rs @@ -0,0 +1,156 @@ +use proc_macro::{self, TokenStream}; +use quote::{quote, ToTokens}; +use syn::{parse_macro_input, Data, DataStruct, DeriveInput, Fields, Ident, Path, Type, TypePath}; + +fn serialize_field(name: &Ident, ind: usize, path: &Path) -> proc_macro2::TokenStream { + if path.is_ident("String") { + quote! { + { + let str_offset = next_extension; + let str_length = self.#name.len() as u32; + + buf.write_str_at(offset + next_extension as usize, &self.#name)?; + next_extension += str_length; + + buf.write_at(yunq::message::field_offset(offset, #ind), str_offset)?; + buf.write_at(yunq::message::field_offset(offset, #ind) + 4, str_length)?; + } + + } + } else if path.is_ident("z_cap_t") { + quote! { + { + let cap_ind = caps.len(); + caps.push(self.#name); + + buf.write_at(yunq::message::field_offset(offset, #ind), cap_ind as u64)?; + } + } + } else { + panic!( + "Serialization not implemented for: {}", + path.to_token_stream() + ) + } +} + +fn parse_field(name: &Ident, ind: usize, path: &Path) -> proc_macro2::TokenStream { + if path.is_ident("String") { + quote! { + let #name = { + let str_offset = buf.at::(yunq::message::field_offset(offset, #ind))?; + let str_length = buf.at::(yunq::message::field_offset(offset, #ind) + 4)?; + + buf.str_at(offset + str_offset as usize, str_length as usize)?.to_string() + }; + } + } else if path.is_ident("z_cap_t") { + quote! { + let #name = { + let cap_ind = buf.at::(yunq::message::field_offset(offset, #ind))?; + caps[cap_ind as usize] + }; + } + } else { + panic!("Parsing not implemented for: {}", path.into_token_stream()); + } +} + +fn field_names(name: &Ident, _ind: usize, _path: &Path) -> proc_macro2::TokenStream { + quote! { #name } +} + +fn apply_to_struct_fields(input: &DeriveInput, func: fn(&Ident, usize, &Path) -> T) -> Vec { + match &input.data { + Data::Struct(DataStruct { + fields: Fields::Named(fields), + .. + }) => fields + .named + .iter() + .enumerate() + .map(|(ind, field)| match &field.ty { + Type::Path(TypePath { path, .. }) => { + func(&field.ident.as_ref().unwrap(), ind, path) + } + _ => { + panic!("Unrecognized type: {:?}", field) + } + }) + .collect(), + _ => { + panic!("Unrecognized input (Expected Struct): {:?}", input.data) + } + } +} + +#[proc_macro_derive(YunqMessage)] +pub fn derive_client(input_tokens: TokenStream) -> TokenStream { + let input: DeriveInput = parse_macro_input!(input_tokens); + let ident = input.ident.clone(); + + let prelude = quote! { + impl YunqMessage for #ident + }; + + let num_fields = apply_to_struct_fields(&input, |_, _, _| ()).len(); + + let serializers = apply_to_struct_fields(&input, serialize_field); + let serialize = quote! { + fn serialize( + &self, + buf: &mut yunq::ByteBuffer, + offset: usize, + caps: &mut alloc::vec::Vec, + ) -> Result { + let num_fields = #num_fields; + let core_size: u32 = (yunq::message::MESSAGE_HEADER_SIZE + 8 * num_fields) as u32; + let mut next_extension = core_size; + + #(#serializers)* + + buf.write_at(offset + 0, yunq::message::MESSAGE_IDENT)?; + buf.write_at(offset + 4, core_size)?; + buf.write_at(offset + 8, next_extension)?; + buf.write_at(offset + 12, 0 as u32)?; + Ok(next_extension as usize) + } + }; + + let field_names = apply_to_struct_fields(&input, field_names); + let parsers = apply_to_struct_fields(&input, parse_field); + let parse = quote! { + fn parse( + buf: &yunq::ByteBuffer, + offset: usize, + caps: &alloc::vec::Vec, + ) -> Result + where + Self: Sized, + { + if buf.at::(offset + 0)? != yunq::message::MESSAGE_IDENT { + return Err(ZError::INVALID_ARGUMENT); + } + // TODO: Parse core size. + // TODO: Parse extension size. + // TODO: Check CRC32 + // TODO: Parse options. + + #(#parsers)* + + Ok(Self { + #(#field_names),* + }) + + } + }; + + let output = quote! { + #prelude { + #serialize + + #parse + } + }; + output.into() +} diff --git a/rust/lib/yunq/Cargo.toml b/rust/lib/yunq/Cargo.toml index cdb0249..f442771 100644 --- a/rust/lib/yunq/Cargo.toml +++ b/rust/lib/yunq/Cargo.toml @@ -5,3 +5,4 @@ edition = "2021" [dependencies] mammoth = {path = "../mammoth"} +yunq-derive = {path = "../yunq-derive"} diff --git a/rust/lib/yunq/src/lib.rs b/rust/lib/yunq/src/lib.rs index 900cfd7..515760b 100644 --- a/rust/lib/yunq/src/lib.rs +++ b/rust/lib/yunq/src/lib.rs @@ -4,110 +4,8 @@ extern crate alloc; mod buffer; -mod client; -mod message; +pub mod client; +pub mod message; -use alloc::string::String; -use alloc::vec::Vec; pub use buffer::ByteBuffer; -use mammoth::syscall::z_cap_t; -use mammoth::syscall::ZError; pub use message::YunqMessage; - -const MESSAGE_HEADER_SIZE: usize = 24; // 4x uint32, 1x uint64 - -pub struct GetEndpointRequest { - pub endpoint_name: String, -} - -impl YunqMessage for GetEndpointRequest { - fn serialize( - &self, - buf: &mut buffer::ByteBuffer, - offset: usize, - _caps: &mut Vec, - ) -> Result { - let core_size: u32 = (MESSAGE_HEADER_SIZE + 8 * 1) as u32; - let mut next_extension = core_size; - - let endpoint_name_offset = next_extension; - let endpoint_name_length = self.endpoint_name.len() as u32; - - buf.write_str_at(offset + next_extension as usize, &self.endpoint_name)?; - next_extension += endpoint_name_length; - - buf.write_at(field_offset(offset, 0), endpoint_name_offset)?; - buf.write_at(field_offset(offset, 0) + 4, endpoint_name_length)?; - - buf.write_at(offset + 0, message::MESSAGE_IDENT)?; - buf.write_at(offset + 4, core_size)?; - buf.write_at(offset + 8, next_extension)?; - buf.write_at(offset + 12, 0 as u32)?; - Ok(next_extension as usize) - } - - fn parse( - _buf: &ByteBuffer, - _offset: usize, - _caps: &Vec, - ) -> Result - where - Self: Sized, - { - todo!() - } -} - -pub struct Endpoint { - pub endpoint: z_cap_t, -} - -fn field_offset(offset: usize, field_index: usize) -> usize { - offset + MESSAGE_HEADER_SIZE + (8 * field_index) -} - -impl YunqMessage for Endpoint { - fn parse( - buf: &ByteBuffer, - offset: usize, - caps: &Vec, - ) -> Result { - if buf.at::(offset + 0)? != message::MESSAGE_IDENT { - return Err(ZError::INVALID_ARGUMENT); - } - // TODO: Parse core size. - // TODO: Parse extension size. - // TODO: Check CRC32 - // TODO: Parse options. - - let endpoint_ptr = buf.at::(field_offset(offset, 0))?; - let endpoint = caps[endpoint_ptr as usize]; - Ok(Endpoint { endpoint }) - } - - fn serialize( - &self, - _buf: &mut buffer::ByteBuffer, - _offset: usize, - _caps: &mut Vec, - ) -> Result { - todo!() - } -} - -pub struct YellowstoneClient { - endpoint_cap: z_cap_t, - byte_buffer: ByteBuffer<0x1000>, -} - -impl YellowstoneClient { - pub fn new(endpoint_cap: z_cap_t) -> Self { - Self { - endpoint_cap, - byte_buffer: ByteBuffer::new(), - } - } - pub fn get_endpoint(&mut self, req: &GetEndpointRequest) -> Result { - client::call_endpoint(req, &mut self.byte_buffer, self.endpoint_cap) - } -} diff --git a/rust/lib/yunq/src/message.rs b/rust/lib/yunq/src/message.rs index 82b2c42..e114343 100644 --- a/rust/lib/yunq/src/message.rs +++ b/rust/lib/yunq/src/message.rs @@ -4,6 +4,11 @@ use mammoth::syscall::z_cap_t; use mammoth::syscall::ZError; pub const MESSAGE_IDENT: u32 = 0x33441122; +pub const MESSAGE_HEADER_SIZE: usize = 24; // 4x uint32, 1x uint64 + +pub fn field_offset(offset: usize, field_index: usize) -> usize { + offset + MESSAGE_HEADER_SIZE + (8 * field_index) +} pub trait YunqMessage { fn parse( diff --git a/rust/usr/testbed/Cargo.toml b/rust/usr/testbed/Cargo.toml index 65e5146..66b00a9 100644 --- a/rust/usr/testbed/Cargo.toml +++ b/rust/usr/testbed/Cargo.toml @@ -5,4 +5,4 @@ edition = "2021" [dependencies] mammoth = { path = "../../lib/mammoth" } -yunq = { path = "../../lib/yunq" } +yellowstone = { path = "../../lib/yellowstone" } diff --git a/rust/usr/testbed/src/main.rs b/rust/usr/testbed/src/main.rs index ce34571..79f9e2e 100644 --- a/rust/usr/testbed/src/main.rs +++ b/rust/usr/testbed/src/main.rs @@ -9,6 +9,8 @@ use mammoth::debug; use mammoth::define_entry; use mammoth::syscall::debug; use mammoth::syscall::z_err_t; +use yellowstone::GetEndpointRequest; +use yellowstone::YellowstoneClient; define_entry!(); @@ -28,11 +30,11 @@ pub extern "C" fn main() -> z_err_t { let mut yellowstone; unsafe { - yellowstone = yunq::YellowstoneClient::new(mammoth::INIT_ENDPOINT); + yellowstone = YellowstoneClient::new(mammoth::INIT_ENDPOINT); } let endpoint = yellowstone - .get_endpoint(&yunq::GetEndpointRequest { + .get_endpoint(&GetEndpointRequest { endpoint_name: "denali".to_string(), }) .expect("Failed to get endpoint");