Function rdma_post_send

Source
pub unsafe fn rdma_post_send(
    id: *mut rdma_cm_id,
    context: *mut c_void,
    addr: *mut c_void,
    length: usize,
    mr: *mut ibv_mr,
    flags: c_int,
) -> c_int
Examples found in repository?
examples/client.rs (lines 99-106)
17fn run(ip: &str, port: &str) -> i32 {
18    let mut send_msg = vec![1_u8; 16];
19    let mut recv_msg = vec![0_u8; 16];
20    let mut hints = unsafe { std::mem::zeroed::<rdma_addrinfo>() };
21    let mut res: *mut rdma_addrinfo = null_mut();
22
23    hints.ai_port_space = rdma_port_space::RDMA_PS_TCP as i32;
24    let mut ret =
25        unsafe { rdma_getaddrinfo(ip.as_ptr().cast(), port.as_ptr().cast(), &hints, &mut res) };
26
27    if ret != 0 {
28        println!("rdma_getaddrinfo");
29        return ret;
30    }
31
32    let mut attr = unsafe { std::mem::zeroed::<ibv_qp_init_attr>() };
33    let mut id: *mut rdma_cm_id = null_mut();
34    attr.cap.max_send_wr = 1;
35    attr.cap.max_recv_wr = 1;
36    attr.cap.max_send_sge = 1;
37    attr.cap.max_recv_sge = 1;
38    attr.cap.max_inline_data = 16;
39    attr.qp_context = id.cast();
40    attr.sq_sig_all = 1;
41    ret = unsafe { rdma_create_ep(&mut id, res, null_mut(), &mut attr) };
42    // Check to see if we got inline data allowed or not
43    let mut send_flags = 0_u32;
44    if attr.cap.max_inline_data >= 16 {
45        send_flags = ibv_send_flags::IBV_SEND_INLINE.0;
46    } else {
47        println!("rdma_client: device doesn't support IBV_SEND_INLINE, using sge sends");
48    }
49
50    if ret != 0 {
51        println!("rdma_create_ep");
52        unsafe {
53            rdma_freeaddrinfo(res);
54        }
55        return ret;
56    }
57
58    let mr = unsafe { rdma_reg_msgs(id, recv_msg.as_mut_ptr().cast(), 16) };
59    if mr.is_null() {
60        println!("rdma_reg_msgs for recv_msg");
61        unsafe {
62            rdma_destroy_ep(id);
63        }
64        return -1;
65    }
66
67    let mut send_mr = null_mut();
68    if (send_flags & ibv_send_flags::IBV_SEND_INLINE.0) as u32 == 0 {
69        println!("flags {:?}", send_flags);
70        send_mr = unsafe { rdma_reg_msgs(id, send_msg.as_mut_ptr().cast(), 16) };
71        if send_mr.is_null() {
72            println!("rdma_reg_msgs for send_msg");
73            unsafe {
74                rdma_dereg_mr(mr);
75            }
76            return -1;
77        }
78    }
79
80    ret = unsafe { rdma_post_recv(id, null_mut(), recv_msg.as_mut_ptr().cast(), 16, mr) };
81    if ret != 0 {
82        println!("rdma_post_recv");
83        if (send_flags & ibv_send_flags::IBV_SEND_INLINE.0) as u32 == 0 {
84            unsafe { rdma_dereg_mr(send_mr) };
85        }
86        return ret;
87    }
88
89    ret = unsafe { rdma_connect(id, null_mut()) };
90    if ret != 0 {
91        println!("rdma_connect");
92        unsafe {
93            rdma_disconnect(id);
94        }
95        return ret;
96    }
97
98    ret = unsafe {
99        rdma_post_send(
100            id,
101            null_mut(),
102            send_msg.as_mut_ptr().cast(),
103            16,
104            send_mr,
105            send_flags.try_into().unwrap(),
106        )
107    };
108    if ret != 0 {
109        println!("rdma_post_send");
110        unsafe {
111            rdma_disconnect(id);
112        }
113        return ret;
114    }
115
116    let mut wc = unsafe { std::mem::zeroed::<ibv_wc>() };
117    while ret == 0 {
118        ret = unsafe { rdma_get_send_comp(id, &mut wc) };
119    }
120    if ret < 0 {
121        println!("rdma_get_send_comp");
122        unsafe {
123            rdma_disconnect(id);
124        }
125        return ret;
126    }
127
128    ret = 0;
129    while ret == 0 {
130        ret = unsafe { rdma_get_recv_comp(id, &mut wc) };
131    }
132    println!("rdma_client: recv msg : {:?}", recv_msg);
133    if ret < 0 {
134        println!("rdma_get_recv_comp");
135    } else {
136        ret = 0;
137    }
138
139    ret
140}
More examples
Hide additional examples
examples/server.rs (lines 157-164)
20fn run() -> i32 {
21    let mut send_msg = vec![1_u8; 16];
22    let mut recv_msg = vec![0_u8; 16];
23    let mut hints = unsafe { std::mem::zeroed::<rdma_addrinfo>() };
24    let mut res: *mut rdma_addrinfo = null_mut();
25    hints.ai_flags = RAI_PASSIVE.try_into().unwrap();
26    hints.ai_port_space = rdma_port_space::RDMA_PS_TCP.try_into().unwrap();
27    let mut ret = unsafe {
28        rdma_getaddrinfo(
29            SERVER.as_ptr().cast(),
30            PORT.as_ptr().cast(),
31            &hints,
32            &mut res,
33        )
34    };
35
36    if ret != 0 {
37        println!("rdma_getaddrinfo");
38        return ret;
39    }
40
41    let mut listen_id = null_mut();
42    let mut id = null_mut();
43
44    let mut init_attr = unsafe { std::mem::zeroed::<ibv_qp_init_attr>() };
45    init_attr.cap.max_send_wr = 1;
46    init_attr.cap.max_recv_wr = 1;
47    init_attr.cap.max_send_sge = 1;
48    init_attr.cap.max_recv_sge = 1;
49    init_attr.cap.max_inline_data = 16;
50    init_attr.sq_sig_all = 1;
51    ret = unsafe { rdma_create_ep(&mut listen_id, res, null_mut(), &mut init_attr) };
52    // Check to see if we got inline data allowed or not
53    if ret != 0 {
54        println!("rdma_create_ep");
55        unsafe {
56            rdma_freeaddrinfo(res);
57        }
58        return ret;
59    }
60    ret = unsafe { rdma_listen(listen_id, 0) };
61    if ret != 0 {
62        println!("rdma_listen");
63        unsafe {
64            rdma_destroy_ep(listen_id);
65        }
66        return ret;
67    }
68
69    ret = unsafe { rdma_get_request(listen_id, &mut id) };
70    if ret != 0 {
71        println!("rdma_get_request");
72        unsafe {
73            rdma_destroy_ep(listen_id);
74        }
75        return ret;
76    }
77
78    let mut qp_attr = unsafe { std::mem::zeroed::<ibv_qp_attr>() };
79    ret = unsafe {
80        ibv_query_qp(
81            (*id).qp,
82            &mut qp_attr,
83            ibv_qp_attr_mask::IBV_QP_CAP.0.try_into().unwrap(),
84            &mut init_attr,
85        )
86    };
87
88    if ret != 0 {
89        println!("ibv_query_qp");
90        unsafe {
91            rdma_destroy_ep(id);
92        }
93        return ret;
94    }
95
96    let mut send_flags = 0_u32;
97    if init_attr.cap.max_inline_data >= 16 {
98        send_flags = ibv_send_flags::IBV_SEND_INLINE.0;
99    } else {
100        println!("rdma_server: device doesn't support IBV_SEND_INLINE, using sge sends");
101    }
102
103    let recv_mr = unsafe { rdma_reg_msgs(id, recv_msg.as_mut_ptr().cast(), 16) };
104    if recv_mr.is_null() {
105        ret = -1;
106        println!("rdma_reg_msgs for recv_msg");
107        unsafe {
108            rdma_dereg_mr(recv_mr);
109        }
110        return ret;
111    }
112
113    let mut send_mr = null_mut();
114    if (send_flags & ibv_send_flags::IBV_SEND_INLINE.0) == 0 {
115        send_mr = unsafe { rdma_reg_msgs(id, send_msg.as_mut_ptr().cast(), 16) };
116        if send_mr.is_null() {
117            ret = -1;
118            println!("rdma_reg_msgs for send_msg");
119            unsafe {
120                rdma_dereg_mr(recv_mr);
121            }
122            return ret;
123        }
124    }
125    ret = unsafe { rdma_post_recv(id, null_mut(), recv_msg.as_mut_ptr().cast(), 16, recv_mr) };
126
127    if ret != 0 {
128        println!("rdma_post_recv");
129        unsafe {
130            rdma_dereg_mr(recv_mr);
131        }
132        return ret;
133    }
134
135    ret = unsafe { rdma_accept(id, null_mut()) };
136    if ret != 0 {
137        println!("rdma_accept");
138        if (send_flags & ibv_send_flags::IBV_SEND_INLINE.0) == 0 {
139            unsafe { rdma_dereg_mr(send_mr) };
140        }
141        return ret;
142    }
143
144    let mut wc = unsafe { std::mem::zeroed::<ibv_wc>() };
145    while ret == 0 {
146        ret = unsafe { rdma_get_recv_comp(id, &mut wc) };
147    }
148    if ret < 0 {
149        println!("rdma_get_recv_comp");
150        unsafe {
151            rdma_disconnect(id);
152        }
153        return ret;
154    }
155    println!("rdma_server: recv msg : {:?}", recv_msg);
156    ret = unsafe {
157        rdma_post_send(
158            id,
159            null_mut(),
160            send_msg.as_mut_ptr().cast(),
161            16,
162            send_mr,
163            send_flags.try_into().unwrap(),
164        )
165    };
166    if ret != 0 {
167        println!("rdma_post_send");
168        unsafe {
169            rdma_disconnect(id);
170        }
171        return ret;
172    }
173
174    while ret == 0 {
175        ret = unsafe { rdma_get_send_comp(id, &mut wc) };
176    }
177    if ret < 0 {
178        println!("rdma_get_send_comp");
179    } else {
180        ret = 0;
181    }
182    ret
183}