Skip to content

Commit

Permalink
Tidy some things
Browse files Browse the repository at this point in the history
make it compile & run on FreeBSD which doesn't have
libc::__errno_location()
Fix typos in some API call names
check for completeness of API & tests in apt-test-coverage
  • Loading branch information
chrissie-c authored and fabbione committed Jun 15, 2021
1 parent 5057127 commit aa2227f
Show file tree
Hide file tree
Showing 16 changed files with 171 additions and 83 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Expand Up @@ -59,4 +59,4 @@ doxyxml
*.3
cov*
*/target/*
*/Cargo.lock
*/Cargo.lock
8 changes: 4 additions & 4 deletions libknet/bindings/rust/Cargo.toml
@@ -1,11 +1,11 @@
[package]
name = "rust-kronosnet"
name = "libknet"
version = "0.1.0"
authors = ["Christine Caulfield <ccaulfie@redhat.com>"]
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"]
Expand Down
28 changes: 2 additions & 26 deletions 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");
}
2 changes: 1 addition & 1 deletion 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()
{
Expand Down
7 changes: 7 additions & 0 deletions libknet/bindings/rust/src/lib.rs
@@ -1,3 +1,10 @@
// Copyright (C) 2021 Red Hat, Inc. All rights reserved.
//
// Authors: Christine Caulfield <ccaulfie@redhat.com>
//
// 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.
Expand Down
51 changes: 43 additions & 8 deletions libknet/bindings/rust/src/libknet.rs
Expand Up @@ -132,6 +132,15 @@ lazy_static! {
static ref HANDLE_HASH: Mutex<HashMap<u64, PrivHandle>> = 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,
Expand Down Expand Up @@ -598,8 +607,7 @@ pub fn recv(handle: Handle, buf: &[u8], channel: i8) -> Result<isize>
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())
Expand All @@ -619,8 +627,7 @@ pub fn send(handle: Handle, buf: &[u8], channel: i8) -> Result<isize>
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())
Expand All @@ -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())
Expand Down Expand Up @@ -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<u32>
{
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,
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion libknet/bindings/rust/tests/Cargo.toml
Expand Up @@ -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"
Expand Down
16 changes: 11 additions & 5 deletions 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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
74 changes: 74 additions & 0 deletions libknet/tests/api-test-coverage
Expand Up @@ -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"
Expand All @@ -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
10 changes: 5 additions & 5 deletions libnozzle/bindings/rust/Cargo.toml
@@ -1,12 +1,12 @@
[package]
name = "rust-kronosnet"
name = "libnozzle"
version = "0.1.0"
authors = ["Christine Caulfield <ccaulfie@redhat.com>"]
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"]

Expand Down
28 changes: 2 additions & 26 deletions 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");
}
2 changes: 1 addition & 1 deletion 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()
{
Expand Down
7 changes: 7 additions & 0 deletions libnozzle/bindings/rust/src/lib.rs
@@ -1,3 +1,10 @@
// Copyright (C) 2021 Red Hat, Inc. All rights reserved.
//
// Authors: Christine Caulfield <ccaulfie@redhat.com>
//
// 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.
Expand Down
2 changes: 1 addition & 1 deletion libnozzle/bindings/rust/tests/Cargo.toml
Expand Up @@ -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"
Expand Down

0 comments on commit aa2227f

Please sign in to comment.