4

我是一个相当新手的程序员,我遇到了一个我认为我理解但不知道如何解决的问题。我正在尝试使用 Rust FFI 与英特尔的 DPDK进行交互,这一切都在 C 中。我的第一次尝试是重新创建 helloworld 示例应用程序。

我遇到了一个编译错误,我认为这是由于 DPDK 中的函数是静态的,不能直接从库中获得。我的 FFI 接口在这里:

use libc::{c_uint, c_int, c_void, c_char};

pub type LcoreFunctionT =
    extern "C" fn(arg1: *mut c_void) -> c_int;

extern {
    pub fn rte_eal_init(argc: c_int,
                        argv: *mut *mut c_char) -> c_int;
    pub fn rte_eal_remote_launch(f: *mut LcoreFunctionT,
                                    arg: *mut c_void,
                                    slave_id: c_uint) -> c_int;
    pub fn rte_eal_mp_wait_lcore() -> ();
    pub fn rte_lcore_id() -> c_uint;
    pub fn rte_get_next_lcore(i: c_uint,
                                skip_master: c_int,
                                wrap: c_int) -> c_uint;
}

我还有一个引用它并包装函数的库:

extern crate libc;

use libc::{c_uint, c_int, c_char, c_void};
use std::ffi::CString;
use std::ptr;

mod ffi_rte_eal;

pub fn dpdk_rte_eal_init(argc: i32, argv: Vec<String>) -> i32 {
    let mut args: Vec<*mut c_char> =
        argv.iter().map(|x| CString::new(x.clone()).unwrap().into_raw()).collect();
    let retc: c_int = unsafe {ffi_rte_eal::rte_eal_init(argc as c_int, args.as_mut_ptr())};
    let ret: i32 = retc as i32;
    ret
}

pub fn dpdk_rte_eal_remote_launch(f: extern "C" fn(*mut c_void) -> i32,
                                    slave_id: u32 ) -> i32 {
    let mut fc: ffi_rte_eal::LcoreFunctionT = f;
    let retc: c_int = unsafe {ffi_rte_eal::rte_eal_remote_launch(&mut fc,
                                                                ptr::null_mut() as *mut c_void,
                                                                slave_id as c_uint)};
    let ret: i32 = retc as i32;
    ret
}

pub fn dpdk_rte_eal_mp_wait_lcore() -> (){
    unsafe {
        ffi_rte_eal::rte_eal_mp_wait_lcore();
    }
}

pub fn dpdk_rte_lcore_id() -> u32 {
    let retc: c_uint = unsafe {ffi_rte_eal::rte_lcore_id()};
    let ret: u32 = retc as u32;
    ret
}

pub fn dpdk_rte_get_next_lcore(i: u32,
                                skip_master: i32,
                                wrap: i32) -> u32 {
    let retc: c_uint = unsafe {ffi_rte_eal::rte_get_next_lcore(i as c_uint,
                                                               skip_master as c_int,
                                                               wrap as c_int)};
    let ret: u32 = retc as u32;
    ret
}

还有一个用于链接库的 build.rs 文件 -

//build.rs

fn main() {
    println!("cargo:rustc-link-lib=static=rte_eal");
    println!("cargo:rustc-link-search=native=/usr/local/lib");
    println!("cargo:rustc-link-lib=static=rte_mempool");
    println!("cargo:rustc-link-search=native=/usr/local/lib");
    println!("cargo:rustc-link-lib=static=rte_ring");
    println!("cargo:rustc-link-search=native=/usr/local/lib");
}

当我尝试针对 FFI 接口编译我自己的应用程序时,我不断收到有关未定义引用rte_lcore_idrte_get_next_lcore. 根据 DPDK 的 API 文档,这些函数是 librte_eal 库的一部分,但在 rte_lcore.h 中定义为静态函数。我假设这些是静态函数,我将无法从 Rust 中看到它们。

在与 DPDK 捆绑的 helloworld 示例应用程序中,它们直接导入 rte_lcore.h。我认为这就是为什么他们可以访问这些函数而无需仅引用 librte_eal?

是否有一种方法可以在 Rust 中访问它,或者在 C 中是否必须有类似 shim 的东西让我包装这些函数并通过 FFI 使它们可用?

4

1 回答 1

7

如您所见,如果您打开相应的头文件,这些函数是直接在那里声明的。这意味着这些函数将包含在每个 .c包含此头文件的/.cpp文件中,但由于它们是static,链接器不会为它们创建符号,因此它们实际上并不存在于库的编译版本中。这里描述了为什么需要这样做,但不幸的是这样的设计对 FFI 不是很友好。

您可以做的是创建一个存根 C 库,其中包含完全相同的函数,这些函数从标头委托给静态函数,但它们本身不是静态的。像这样:

#include <rte_lcore.h>

unsigned my_rte_lcore_count(void) {
    return rte_lcore_count();
}

// and so forth for every function you need

然后,您可以使用自定义编写的 Makefile 或Cargo 构建脚本将此存根文件编译为静态库,并将最终程序链接到它。然后,自然地,您应该在extern块中编写这些函数,而不是原始函数:

extern {
    fn my_rte_lcore_count() -> libc::c_uint;
}

我认为没有更简单,更正确的方法。

更新:哦,我没有注意到你在问题中的最后一句话。是的,你是对的,写这样一个垫片是正确的方法。

于 2016-06-17T13:55:08.877 回答