forked from jonhoo/rust-ibverbs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathloopback.rs
More file actions
55 lines (49 loc) · 1.5 KB
/
loopback.rs
File metadata and controls
55 lines (49 loc) · 1.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
fn main() {
let ctx = ibverbs::devices()
.unwrap()
.iter()
.next()
.expect("no rdma device available")
.open()
.unwrap();
let cq = ctx.create_cq(16, 0).unwrap();
let pd = ctx.alloc_pd().unwrap();
let qp_builder = pd
.create_qp(&cq, &cq, ibverbs::ibv_qp_type::IBV_QPT_RC)
.unwrap()
.set_gid_index(1)
.build()
.unwrap();
let endpoint = qp_builder.endpoint().unwrap();
let mut qp = qp_builder.handshake(endpoint).unwrap();
let mut mr = pd.allocate(16).unwrap();
mr.inner()[9] = 0x42;
unsafe { qp.post_receive(&[mr.slice(..8)], 2) }.unwrap();
unsafe { qp.post_send(&[mr.slice(8..)], 1) }.unwrap();
let mut sent = false;
let mut received = false;
let mut completions = [ibverbs::ibv_wc::default(); 16];
while !sent || !received {
let completed = cq.poll(&mut completions[..]).unwrap();
if completed.is_empty() {
continue;
}
assert!(completed.len() <= 2);
for wr in completed {
match wr.wr_id() {
1 => {
assert!(!sent);
sent = true;
println!("sent");
}
2 => {
assert!(!received);
received = true;
assert_eq!(mr.inner()[1], 0x42);
println!("received");
}
_ => unreachable!(),
}
}
}
}