Skip to content

Commit b2263a8

Browse files
committed
pldm-file: client: Use application provided buffer
Part size has a large effect on performance, so allow applications to provide a larger buffer. The buffer must match the size provided in multipart negotiation, otherwise messages will be dropped. Signed-off-by: Matt Johnston <[email protected]>
1 parent 69d16b7 commit b2263a8

File tree

2 files changed

+25
-16
lines changed

2 files changed

+25
-16
lines changed

pldm-file/examples/client.rs

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -38,19 +38,21 @@ fn main() -> Result<()> {
3838

3939
let mut buf = Vec::new();
4040
let req_len = 4096;
41+
let mut part_buf = [0u8; 512 + 18];
4142

4243
println!("Reading...");
43-
let res = df_read_with(&mut req, fd, 0, req_len, |part| {
44-
println!(" {} bytes", part.len());
45-
if buf.len() + part.len() > req_len {
46-
println!(" data overflow!");
47-
Err(PldmError::NoSpace)
48-
} else {
49-
buf.extend_from_slice(part);
50-
Ok(())
51-
}
52-
})
53-
.await;
44+
let res =
45+
df_read_with(&mut req, fd, 0, req_len, &mut part_buf, |part| {
46+
println!(" {} bytes", part.len());
47+
if buf.len() + part.len() > req_len {
48+
println!(" data overflow!");
49+
Err(PldmError::NoSpace)
50+
} else {
51+
buf.extend_from_slice(part);
52+
Ok(())
53+
}
54+
})
55+
.await;
5456

5557
println!("Read: {res:?}");
5658

pldm-file/src/client.rs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -109,21 +109,25 @@ pub async fn df_close(
109109
Ok(())
110110
}
111111

112-
const PART_SIZE: usize = 1024;
112+
pub const PART_SIZE: usize = 4096;
113113

114114
/// Read a file from the host.
115115
///
116116
/// The total file size read is returned.
117+
///
118+
/// `part_buf` must be sized as at least `multipart_size + 18`, where multipart_size
119+
/// is the size passed to [`pldm::control::requester::negotiate_transfer_paraemters`].
117120
pub async fn df_read(
118121
comm: &mut impl mctp::AsyncReqChannel,
119122
file: FileDescriptor,
120123
offset: usize,
121124
buf: &mut [u8],
125+
part_buf: &mut [u8],
122126
) -> Result<usize> {
123127
let len = buf.len();
124128
let mut v = pldm::util::SliceWriter::new(buf);
125129

126-
df_read_with(comm, file, offset, len, |b| {
130+
df_read_with(comm, file, offset, len, part_buf, |b| {
127131
let r = v.extend(b);
128132
debug_assert!(r.is_some(), "provided data should be <= len");
129133
Ok(())
@@ -135,11 +139,15 @@ pub async fn df_read(
135139
///
136140
/// The `out` closure will be called repeatedly with sequential buffer chunks
137141
/// sent from the host. Any error returned by `out` will be returned from `df_read_with`.
142+
///
143+
/// `part_buf` must be sized as at least `multipart_size + 18`, where multipart_size
144+
/// is the size passed to [`pldm::control::requester::negotiate_transfer_paraemters`].
138145
pub async fn df_read_with<F>(
139146
comm: &mut impl mctp::AsyncReqChannel,
140147
file: FileDescriptor,
141148
offset: usize,
142149
len: usize,
150+
part_buf: &mut [u8],
143151
mut out: F,
144152
) -> Result<usize>
145153
where
@@ -180,9 +188,8 @@ where
180188
tx_buf,
181189
);
182190

183-
// todo: negotiated length
184-
let mut rx_buf = [0u8; 14 + PART_SIZE + 4];
185-
let resp = pldm_xfer_buf_async(comm, pldm_req, &mut rx_buf).await?;
191+
// todo: can part_buf.len() be checked against negotiated length?
192+
let resp = pldm_xfer_buf_async(comm, pldm_req, part_buf).await?;
186193

187194
let ((rest, _), read_resp) =
188195
MultipartReceiveResp::from_bytes((&resp.data, 0)).map_err(|e| {

0 commit comments

Comments
 (0)