Skip to content

Commit 5ca8729

Browse files
Fix Rust query_xfer_backend API
- return string instead of raw handle - add nixl_capi_query_xfer_backend_type and use it in Rust implementation - add C++ Agent::getBackendType helper API and use it in C wrapper
1 parent a7851fd commit 5ca8729

File tree

7 files changed

+89
-11
lines changed

7 files changed

+89
-11
lines changed

src/api/cpp/nixl.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,16 @@ class nixlAgent {
106106
const nixl_b_params_t &params,
107107
nixlBackendH* &backend);
108108
/**
109+
* @brief Get the type of a backend
110+
*
111+
* @param backend Backend handle
112+
* @param type [out] Backend type
113+
* @return nixl_status_t Error code if call was not successful
114+
*/
115+
nixl_status_t
116+
getBackendType(const nixlBackendH* backend,
117+
nixl_backend_t &type) const;
118+
/**
109119
* @brief Register a memory/storage with NIXL. If a list of backends hints is provided
110120
* (via extra_params), the registration is limited to the specified backends.
111121
*

src/bindings/rust/src/agent.rs

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -931,23 +931,45 @@ impl Agent {
931931
/// * `req` - Transfer request handle after `post_xfer_req`
932932
///
933933
/// # Returns
934-
/// A handle to the backend used for the transfer
934+
/// Name of the backend used for the transfer
935935
///
936936
/// # Errors
937937
/// Returns a NixlError if the operation fails
938-
pub fn query_xfer_backend(&self, req: &XferRequest) -> Result<Backend, NixlError> {
939-
let mut backend = std::ptr::null_mut();
938+
pub fn query_xfer_backend(&self, req: &XferRequest) -> Result<String, NixlError> {
939+
let mut backend_type_ptr: *mut std::ffi::c_void = std::ptr::null_mut();
940+
let mut backend_type_size: usize = 0;
941+
940942
let inner_guard = self.inner.write().unwrap();
941943
let status = unsafe {
942-
nixl_capi_query_xfer_backend(
944+
nixl_capi_query_xfer_backend_type(
943945
inner_guard.handle.as_ptr(),
944946
req.handle(),
945-
&mut backend
947+
&mut backend_type_ptr,
948+
&mut backend_type_size,
946949
)
947950
};
948951
match status {
949952
NIXL_CAPI_SUCCESS => {
950-
Ok(Backend{ inner: NonNull::new(backend).ok_or(NixlError::FailedToCreateBackend)? })
953+
if backend_type_ptr.is_null() || backend_type_size == 0 {
954+
return Err(NixlError::BackendError);
955+
}
956+
957+
// Extract the backend type string from the binary data
958+
let backend_type = unsafe {
959+
let slice = std::slice::from_raw_parts(
960+
backend_type_ptr as *const u8,
961+
backend_type_size
962+
);
963+
// Handle potential embedded nulls and convert to String
964+
String::from_utf8_lossy(slice).to_string()
965+
};
966+
967+
// Verify this backend type exists in our backends map
968+
if inner_guard.backends.contains_key(&backend_type) {
969+
Ok(backend_type)
970+
} else {
971+
Err(NixlError::BackendError)
972+
}
951973
}
952974
NIXL_CAPI_ERROR_INVALID_PARAM => Err(NixlError::InvalidParam),
953975
_ => Err(NixlError::BackendError),

src/bindings/rust/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,8 @@ use bindings::{
7171
nixl_capi_query_resp_list_size, nixl_capi_query_resp_list_has_value,
7272
nixl_capi_query_resp_list_get_params, nixl_capi_prep_xfer_dlist, nixl_capi_release_xfer_dlist_handle,
7373
nixl_capi_make_xfer_req, nixl_capi_get_local_partial_md,
74-
nixl_capi_send_local_partial_md, nixl_capi_query_xfer_backend, nixl_capi_opt_args_set_ip_addr,
75-
nixl_capi_opt_args_set_port
74+
nixl_capi_send_local_partial_md, nixl_capi_opt_args_set_ip_addr,
75+
nixl_capi_opt_args_set_port, nixl_capi_query_xfer_backend_type
7676
};
7777

7878
// Re-export status codes

src/bindings/rust/tests/tests.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1324,10 +1324,10 @@ fn test_query_xfer_backend_success() {
13241324
None
13251325
).expect("Failed to create transfer request");
13261326
// Query which backend will be used for this transfer
1327-
let result: Result<Backend, NixlError> = agent1.query_xfer_backend(&xfer_req);
1327+
let result: Result<String, NixlError> = agent1.query_xfer_backend(&xfer_req);
13281328
assert!(result.is_ok(), "query_xfer_backend failed with error: {:?}", result.err());
1329-
let backend = result.unwrap();
1330-
println!("Transfer will use backend: {:?}", backend);
1329+
let backend_name = result.unwrap();
1330+
println!("Transfer will use backend: {}", backend_name);
13311331
}
13321332
}
13331333
#[test]

src/bindings/rust/wrapper.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1507,6 +1507,31 @@ nixl_capi_query_xfer_backend(nixl_capi_agent_t agent,
15071507
}
15081508
}
15091509

1510+
nixl_capi_status_t
1511+
nixl_capi_query_xfer_backend_type(nixl_capi_agent_t agent,
1512+
nixl_capi_xfer_req_t req_hndl,
1513+
void** backend_type, size_t *backend_type_size) {
1514+
if (!agent || !req_hndl || !backend_type || !backend_type_size) {
1515+
return NIXL_CAPI_ERROR_INVALID_PARAM;
1516+
}
1517+
1518+
nixl_capi_backend_t backend_handle;
1519+
auto ret = nixl_capi_query_xfer_backend(agent, req_hndl, &backend_handle);
1520+
if (ret != NIXL_CAPI_SUCCESS) {
1521+
return ret;
1522+
}
1523+
1524+
static thread_local nixl_backend_t type;
1525+
auto nixl_ret = agent->inner->getBackendType(backend_handle->backend, type);
1526+
if (nixl_ret != NIXL_SUCCESS) {
1527+
return NIXL_CAPI_ERROR_BACKEND;
1528+
}
1529+
1530+
*backend_type = type.data();
1531+
*backend_type_size = type.size();
1532+
return NIXL_CAPI_SUCCESS;
1533+
}
1534+
15101535
nixl_capi_status_t
15111536
nixl_capi_destroy_xfer_req(nixl_capi_xfer_req_t req)
15121537
{

src/bindings/rust/wrapper.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,10 @@ nixl_capi_query_xfer_backend(nixl_capi_agent_t agent,
243243
nixl_capi_xfer_req_t req_hndl,
244244
nixl_capi_backend_t *backend);
245245

246+
nixl_capi_status_t nixl_capi_query_xfer_backend_type(nixl_capi_agent_t agent,
247+
nixl_capi_xfer_req_t req_hndl,
248+
void** backend_type, size_t *backend_type_size);
249+
246250
nixl_capi_status_t nixl_capi_release_xfer_req(nixl_capi_agent_t agent, nixl_capi_xfer_req_t req);
247251

248252
nixl_capi_status_t nixl_capi_destroy_xfer_req(nixl_capi_xfer_req_t req);

src/core/nixl_agent.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,23 @@ nixlAgent::createBackend(const nixl_backend_t &type,
396396
return NIXL_ERR_BACKEND;
397397
}
398398

399+
nixl_status_t
400+
nixlAgent::getBackendType(const nixlBackendH* backend,
401+
nixl_backend_t &type) const {
402+
if (!backend) {
403+
NIXL_ERROR_FUNC << "backend handle is not provided";
404+
return NIXL_ERR_INVALID_PARAM;
405+
}
406+
407+
try {
408+
type = backend->getType();
409+
return NIXL_SUCCESS;
410+
}
411+
catch (...) {
412+
return NIXL_ERR_BACKEND;
413+
}
414+
}
415+
399416
nixl_status_t
400417
nixlAgent::queryMem(const nixl_reg_dlist_t &descs,
401418
std::vector<nixl_query_resp_t> &resp,

0 commit comments

Comments
 (0)