diff --git a/.gitignore b/.gitignore index 532c90d4..90550c7e 100644 --- a/.gitignore +++ b/.gitignore @@ -59,4 +59,4 @@ doxyxml *.3 cov* */target/* -*/Cargo.lock \ No newline at end of file +*/Cargo.lock diff --git a/libknet/bindings/rust/Cargo.toml b/libknet/bindings/rust/Cargo.toml index 564accf5..ec82b5e3 100644 --- a/libknet/bindings/rust/Cargo.toml +++ b/libknet/bindings/rust/Cargo.toml @@ -1,11 +1,11 @@ [package] -name = "rust-kronosnet" +name = "libknet" version = "0.1.0" authors = ["Christine Caulfield "] edition = "2018" -readme = "README.md" -license = "MIT OR Apache-2.0" -repository = "https://github.com/chrissie-c/rust-kronosnet" +readme = "README" +license = "LGPL-2.1" +repository = "https://github.com/kronosnet/kronosnet" description = "Rust bindings for Kronosnet libraries" categories = ["api-bindings"] keywords = ["cluster", "high-availability"] diff --git a/libknet/bindings/rust/build.rs b/libknet/bindings/rust/build.rs index ebc95967..ff3d811d 100644 --- a/libknet/bindings/rust/build.rs +++ b/libknet/bindings/rust/build.rs @@ -1,30 +1,6 @@ extern crate pkg_config; fn main() { - if let Err(e) = pkg_config::probe_library("libknet") { - match e { - pkg_config::Error::Failure { .. } => panic! ( - "Pkg-config failed - usually this is because knet development headers are not installed.\n\n\ - For Fedora users:\n# dnf install libknet1-devel\n\n\ - For Debian/Ubuntu users:\n# apt-get install libknet1-dev\n\n\ - pkg_config details:\n{}", - e - ), - _ => panic!("{}", e) - } - } - - if let Err(e) = pkg_config::probe_library("libnozzle") { - match e { - pkg_config::Error::Failure { .. } => panic! ( - "Pkg-config failed - usually this is because knet development headers are not installed.\n\n\ - For Fedora users:\n# dnf install libnozzle1-devel\n\n\ - For Debian/Ubuntu users:\n# apt-get install libnozzle1-dev\n\n\ - pkg_config details:\n{}", - e - ), - _ => panic!("{}", e) - } - } - + println!("cargo:rustc-link-search=native=../../"); + println!("cargo:rustc-link-lib=knet"); } diff --git a/libknet/bindings/rust/regenerate-sys.sh b/libknet/bindings/rust/regenerate-sys.sh index 0de22f17..b8574e30 100644 --- a/libknet/bindings/rust/regenerate-sys.sh +++ b/libknet/bindings/rust/regenerate-sys.sh @@ -1,5 +1,5 @@ # -# Regerate the FFI bindings in src/sys from the current Corosync headers +# Regerate the FFI bindings in src/sys from the current knet headers # regen() { diff --git a/libknet/bindings/rust/src/lib.rs b/libknet/bindings/rust/src/lib.rs index 8efd7c9a..0b184c33 100644 --- a/libknet/bindings/rust/src/lib.rs +++ b/libknet/bindings/rust/src/lib.rs @@ -1,3 +1,10 @@ +// Copyright (C) 2021 Red Hat, Inc. All rights reserved. +// +// Authors: Christine Caulfield +// +// This software licensed under LGPL-2.0+ +// + //! This crate provides access to the kronosnet libknet //! from Rust. They are a fairly thin layer around the actual API calls but with Rust data types //! and iterators. diff --git a/libknet/bindings/rust/src/libknet.rs b/libknet/bindings/rust/src/libknet.rs index b9deb8be..7a61465d 100644 --- a/libknet/bindings/rust/src/libknet.rs +++ b/libknet/bindings/rust/src/libknet.rs @@ -132,6 +132,15 @@ lazy_static! { static ref HANDLE_HASH: Mutex> = Mutex::new(HashMap::new()); } +fn get_errno() -> i32 +{ + match Error::last_os_error().raw_os_error() { + Some(e) => e, + None => libc::EINVAL, + } +} + + /// Callback from [handle_enable_sock_notify] pub type SockNotifyFn = fn(private_data: u64, datafd: i32, @@ -598,8 +607,7 @@ pub fn recv(handle: Handle, buf: &[u8], channel: i8) -> Result if res >= 0 { Ok(res) } else { - let errno = unsafe {*libc::__errno_location()}; - if errno == libc::EAGAIN { + if get_errno() == libc::EAGAIN { Err(Error::new(ErrorKind::WouldBlock, "Try again")) } else { Err(Error::last_os_error()) @@ -619,8 +627,7 @@ pub fn send(handle: Handle, buf: &[u8], channel: i8) -> Result if res >= 0 { Ok(res) } else { - let errno = unsafe {*libc::__errno_location()}; - if errno == libc::EAGAIN { + if get_errno() == libc::EAGAIN { Err(Error::new(ErrorKind::WouldBlock, "Try again")) } else { Err(Error::last_os_error()) @@ -640,8 +647,7 @@ pub fn send_sync(handle: Handle, buf: &[u8], channel: i8) -> Result<()> if res == 0 { Ok(()) } else { - let errno = unsafe {*libc::__errno_location()}; - if errno == libc::EAGAIN { + if get_errno() == libc::EAGAIN { Err(Error::new(ErrorKind::WouldBlock, "Try again")) } else { Err(Error::last_os_error()) @@ -684,8 +690,37 @@ pub fn handle_enable_filter(handle: Handle, } } +/// Set timer resolution +pub fn handle_set_threads_timer_res(handle: Handle, timeres: u32) -> Result<()> +{ + let res = unsafe { + ffi::knet_handle_set_threads_timer_res(handle.knet_handle as ffi::knet_handle_t, timeres) + }; + if res == 0 { + Ok(()) + } else { + Err(Error::last_os_error()) + } +} + +/// Get timer resolution +pub fn handle_get_threads_timer_res(handle: Handle) -> Result +{ + let mut c_timeres: u32 = 0; + let res = unsafe { + ffi::knet_handle_get_threads_timer_res(handle.knet_handle as ffi::knet_handle_t, &mut c_timeres) + }; + if res == 0 { + Ok(c_timeres) + } else { + Err(Error::last_os_error()) + } +} + + + /// Starts traffic moving. You must call this before knet will do anything. -pub fn handle_set_fwd(handle: Handle, enabled: bool) -> Result<()> +pub fn handle_setfwd(handle: Handle, enabled: bool) -> Result<()> { let res = unsafe { ffi::knet_handle_setfwd(handle.knet_handle as ffi::knet_handle_t, @@ -713,7 +748,7 @@ pub fn handle_enable_access_lists(handle: Handle, enabled: bool) -> Result<()> } /// Set frequency that PMTUd will check for MTU changes. value in milliseconds -pub fn handle_pmtud_sefreq(handle: Handle, interval: u32) -> Result<()> +pub fn handle_pmtud_setfreq(handle: Handle, interval: u32) -> Result<()> { let res = unsafe { ffi::knet_handle_pmtud_setfreq(handle.knet_handle as ffi::knet_handle_t, diff --git a/libknet/bindings/rust/tests/Cargo.toml b/libknet/bindings/rust/tests/Cargo.toml index 532142bd..130c4ece 100644 --- a/libknet/bindings/rust/tests/Cargo.toml +++ b/libknet/bindings/rust/tests/Cargo.toml @@ -7,7 +7,7 @@ edition = "2018" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -rust-kronosnet = { path = ".." } +libknet = { path = ".." } [[bin]] name = "knet-test" diff --git a/libknet/bindings/rust/tests/src/bin/knet-test.rs b/libknet/bindings/rust/tests/src/bin/knet-test.rs index 52c5633e..e6253255 100644 --- a/libknet/bindings/rust/tests/src/bin/knet-test.rs +++ b/libknet/bindings/rust/tests/src/bin/knet-test.rs @@ -1,7 +1,13 @@ - // Testing the Knet Rust APIs -extern crate rust_kronosnet as kronosnet; -use kronosnet::libknet as knet; +// +// Copyright (c) 2021 Red Hat, Inc. +// +// All rights reserved. +// +// Author: Christine Caulfield (ccaulfi@redhat.com) +// + +use libknet::libknet as knet; use std::net::{SocketAddr, IpAddr,Ipv4Addr}; use std::thread::spawn; use std::sync::mpsc::Receiver; @@ -186,7 +192,7 @@ fn setup_node(our_hostid: &knet::HostId, other_hostid: &knet::HostId) -> Result< } - match knet::handle_set_fwd(knet_handle, true) { + match knet::handle_setfwd(knet_handle, true) { Ok(_) => {}, Err(e) => { println!("Error from setfwd(true): {}", e); @@ -234,7 +240,7 @@ fn close_handle(handle: knet::Handle, remnode: u16) -> Result<()> { let other_hostid = knet::HostId::new(remnode); - match knet::handle_set_fwd(handle, false) { + match knet::handle_setfwd(handle, false) { Ok(_) => {}, Err(e) => { println!("Error from setfwd 1 (false): {}", e); diff --git a/libknet/tests/api-test-coverage b/libknet/tests/api-test-coverage index d30c2ff9..534a0159 100755 --- a/libknet/tests/api-test-coverage +++ b/libknet/tests/api-test-coverage @@ -77,6 +77,56 @@ for i in $headerapicalls; do fi done +# Check Rust bindings coverage +rust_found=0 +rust_missing=0 +deliberately_missing="knet_strtoaddr knet_addrtostr knet_get_transport_name_by_id knet_get_transport_id_by_name" +rustapicalls=$numapicalls +for i in $headerapicalls; do + rustcall=`echo $i|awk '{print substr($0, 6)}'` + grep "^pub fn ${rustcall}(" $1/libknet/bindings/rust/src/libknet.rs > /dev/null 2>/dev/null + if [ $? = 0 ] + then + rust_found=$((rust_found+1)) + else + echo $deliberately_missing | grep $i 2>/dev/null >/dev/null + if [ $? != 0 ] + then + echo "$i Missing from Rust API" + rust_missing=$((rust_missing+1)) + else + rustapicalls=$((rustapicalls-1)) + fi + fi +done + + + +# Check Rust test coverage +rust_test_found=0 +rust_test_missing=0 +deliberately_missing="knet_strtoaddr knet_addrtostr knet_get_transport_name_by_id knet_get_transport_id_by_name" +rust_testapicalls=$numapicalls +for i in $headerapicalls; do + rustcall=`echo $i|awk '{print substr($0, 6)}'` + grep "knet::${rustcall}(" $1/libknet/bindings/rust/tests/src/bin/knet-test.rs > /dev/null 2>/dev/null + if [ $? = 0 ] + then + rust_test_found=$((rust_test_found+1)) + else + echo $deliberately_missing | grep $i 2>/dev/null >/dev/null + if [ $? != 0 ] + then + echo "$i Missing from Rust test" + rust_test_missing=$((rust_test_missing+1)) + else + rust_testapicalls=$((rust_testapicalls-1)) + fi + fi +done + + + echo "Summary" echo "-------" echo "Found : $found" @@ -86,4 +136,28 @@ which bc > /dev/null 2>&1 && { coverage=$(echo "scale=3; $found / $numapicalls * 100" | bc -l) echo "Coverage: $coverage%" } + +echo +echo "Rust API Summary" +echo "----------------" +echo "Found : $rust_found" +echo "Missing : $rust_missing" +echo "Total : $rustapicalls" +which bc > /dev/null 2>&1 && { + coverage=$(echo "scale=3; $rust_found / $rustapicalls * 100" | bc -l) + echo "Coverage: $coverage%" +} + + +echo +echo "Rust test Summary" +echo "----------------" +echo "Found : $rust_test_found" +echo "Missing : $rust_test_missing" +echo "Total : $rustapicalls" +which bc > /dev/null 2>&1 && { + coverage=$(echo "scale=3; $rust_test_found / $rust_testapicalls * 100" | bc -l) + echo "Coverage: $coverage%" +} + exit 0 diff --git a/libnozzle/bindings/rust/Cargo.toml b/libnozzle/bindings/rust/Cargo.toml index 564accf5..2c1a95f1 100644 --- a/libnozzle/bindings/rust/Cargo.toml +++ b/libnozzle/bindings/rust/Cargo.toml @@ -1,12 +1,12 @@ [package] -name = "rust-kronosnet" +name = "libnozzle" version = "0.1.0" authors = ["Christine Caulfield "] edition = "2018" -readme = "README.md" -license = "MIT OR Apache-2.0" -repository = "https://github.com/chrissie-c/rust-kronosnet" -description = "Rust bindings for Kronosnet libraries" +readme = "README" +license = "LGPL-2.1" +repository = "https://github.com/kronosnet/kronosnet" +description = "Rust bindings for libnozzle library" categories = ["api-bindings"] keywords = ["cluster", "high-availability"] diff --git a/libnozzle/bindings/rust/build.rs b/libnozzle/bindings/rust/build.rs index ebc95967..0c321b75 100644 --- a/libnozzle/bindings/rust/build.rs +++ b/libnozzle/bindings/rust/build.rs @@ -1,30 +1,6 @@ extern crate pkg_config; fn main() { - if let Err(e) = pkg_config::probe_library("libknet") { - match e { - pkg_config::Error::Failure { .. } => panic! ( - "Pkg-config failed - usually this is because knet development headers are not installed.\n\n\ - For Fedora users:\n# dnf install libknet1-devel\n\n\ - For Debian/Ubuntu users:\n# apt-get install libknet1-dev\n\n\ - pkg_config details:\n{}", - e - ), - _ => panic!("{}", e) - } - } - - if let Err(e) = pkg_config::probe_library("libnozzle") { - match e { - pkg_config::Error::Failure { .. } => panic! ( - "Pkg-config failed - usually this is because knet development headers are not installed.\n\n\ - For Fedora users:\n# dnf install libnozzle1-devel\n\n\ - For Debian/Ubuntu users:\n# apt-get install libnozzle1-dev\n\n\ - pkg_config details:\n{}", - e - ), - _ => panic!("{}", e) - } - } - + println!("cargo:rustc-link-search=native=../../"); + println!("cargo:rustc-link-lib=nozzle"); } diff --git a/libnozzle/bindings/rust/regenerate-sys.sh b/libnozzle/bindings/rust/regenerate-sys.sh index abbb14c4..7918c498 100644 --- a/libnozzle/bindings/rust/regenerate-sys.sh +++ b/libnozzle/bindings/rust/regenerate-sys.sh @@ -1,5 +1,5 @@ # -# Regerate the FFI bindings in src/sys from the current Corosync headers +# Regerate the FFI bindings in src/sys from the current headers # regen() { diff --git a/libnozzle/bindings/rust/src/lib.rs b/libnozzle/bindings/rust/src/lib.rs index f85d17a0..61fab342 100644 --- a/libnozzle/bindings/rust/src/lib.rs +++ b/libnozzle/bindings/rust/src/lib.rs @@ -1,3 +1,10 @@ +// Copyright (C) 2021 Red Hat, Inc. All rights reserved. +// +// Authors: Christine Caulfield +// +// This software licensed under LGPL-2.0+ +// + //! This crate provides access to the kronosnet libraries libknet and libnozzle //! from Rust. They are a fairly thin layer around the actual API calls but with Rust data types //! and iterators. diff --git a/libnozzle/bindings/rust/tests/Cargo.toml b/libnozzle/bindings/rust/tests/Cargo.toml index a09d08bd..e12d1614 100644 --- a/libnozzle/bindings/rust/tests/Cargo.toml +++ b/libnozzle/bindings/rust/tests/Cargo.toml @@ -7,7 +7,7 @@ edition = "2018" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -rust-kronosnet = { path = ".." } +libnozzle = { path = ".." } [[bin]] name = "nozzle-test" diff --git a/libnozzle/bindings/rust/tests/src/bin/nozzle-test.rs b/libnozzle/bindings/rust/tests/src/bin/nozzle-test.rs index 24285eef..73af5447 100644 --- a/libnozzle/bindings/rust/tests/src/bin/nozzle-test.rs +++ b/libnozzle/bindings/rust/tests/src/bin/nozzle-test.rs @@ -1,14 +1,21 @@ - // Testing the Nozzle Rust APIs -extern crate rust_kronosnet as kronosnet; -use kronosnet::libnozzle as nozzle; +// +// Copyright (c) 2021 Red Hat, Inc. +// +// All rights reserved. +// +// Author: Christine Caulfield (ccaulfi@redhat.com) +// + +use libnozzle::libnozzle as nozzle; use std::io::{Result, Error, ErrorKind}; use std::env; use std::{thread, time}; fn main() -> Result<()> { - let mut nozzle_name = String::from("rustnoz"); + // Name must be tapXX for it to work on FreeBSD + let mut nozzle_name = String::from("tap33"); let handle = match nozzle::open(&mut nozzle_name, &String::from(env::current_dir().unwrap().to_str().unwrap())) { Ok(h) => { println!("Opened device {}", nozzle_name); diff --git a/libnozzle/bindings/rust/tests/up.d/rustnoz b/libnozzle/bindings/rust/tests/up.d/tap33 similarity index 100% rename from libnozzle/bindings/rust/tests/up.d/rustnoz rename to libnozzle/bindings/rust/tests/up.d/tap33