On Tue, Apr 08, 2025 at 03:58:38PM +0100, Bruce Richardson wrote:
> Add a Cargo.toml file in the root folder and a number of other scripts
> and rust-related files into buildtools/rust, which then enables DPDK to
> be cloned and built as a rust crate - all-be-it one with only two
> functions: rte_eal_init and rte_eal_cleanup.
> 
> Signed-off-by: Bruce Richardson <bruce.richard...@intel.com>
> ---
> 
> This RFC is proposed as an alternative approach to enabling rust support
> in DPDK. The key difference vs previous is that we are taking the whole
> DPDK project here as a rust "crate", which can then be used by other
> higher-level crates as a dependency. Building the crate does a
> (minimal) build of DPDK and statically links it in, so there is no
> "install" step or anything needed - the Rust app just adds DPDK to their
> Cargo.toml file and then should have everything they need.
> 
> ---
>  Cargo.toml                                | 16 +++++++
>  buildtools/meson.build                    |  1 +
>  buildtools/rust/build.rs                  | 45 ++++++++++++++++++++
>  buildtools/rust/gen-cargo-bindgen-info.py | 52 +++++++++++++++++++++++
>  buildtools/rust/lib.rs                    | 25 +++++++++++
>  buildtools/rust/wrapper.h                 |  5 +++
>  meson.build                               |  3 ++
>  7 files changed, 147 insertions(+)
>  create mode 100644 Cargo.toml
>  create mode 100644 buildtools/rust/build.rs
>  create mode 100644 buildtools/rust/gen-cargo-bindgen-info.py
>  create mode 100644 buildtools/rust/lib.rs
>  create mode 100644 buildtools/rust/wrapper.h
> 

<snip>

> diff --git a/buildtools/rust/lib.rs b/buildtools/rust/lib.rs
> new file mode 100644
> index 0000000000..f99c54ac42
> --- /dev/null
> +++ b/buildtools/rust/lib.rs
> @@ -0,0 +1,25 @@
> +/* SPDX-License-Identifier: BSD-3-Clause
> + * Copyright(c) 2025 Intel Corporation
> + */
> +
> +#![allow(non_upper_case_globals)]
> +#![allow(non_camel_case_types)]
> +#![allow(non_snake_case)]
> +
> +include!(concat!(env!("OUT_DIR"), "/bindings.rs"));
> +
> +#[cfg(test)]
> +mod tests {
> +    use super::*;
> +
> +    #[test]
> +    fn test_helloworld() {
> +     let appname = std::ffi::CString::new("test-rs").unwrap();
> +     let mut argv = [appname.into_raw()];
> +     let ret = unsafe {
> +             rte_eal_init(argv.len().try_into().unwrap(), argv.as_mut_ptr())
> +     };
> +     assert!(ret >= 0, "rte_eal_init failed");
> +        unsafe { rte_eal_cleanup() };
> +    }
> +}

This test code is included in the patch to make it easy to verify that DPDK
can be built and run with rust - at least doing EAL init and cleanup.

To test, once rust is available on your system (e.g. using rustup), apply
this patch and then just do "cargo test" in the root of your DPDK
directory. After waiting a minute for things to build you should see the
test run output of DPDK initialization.

The whole approach for now I'm deliberately keeping extremely minimal so as
a) not to invest too much effort before we know what way we want to go and
b) to make it easier to review and comment upon.

For anyone wanting to try this out in a standalone app or play with
developing a higher-level crate, I've pushed this patch to my github
account. This is the "Cargo.toml" file entry I've used to pull this down
when investigating wrapping eal_init into a "higher-level" rust-style init
API:

[dependencies]
dpdk-c = { git = "https://github.com/bruce-richardson/dpdk";, branch = 
"cargo-build" }

Regards,

/Bruce

Reply via email to