---
# System prepended metadata

title: Receive Request(吳侑珉)
tags: [Session one]

---

---
tags : Session one
title : Receive Request(吳侑珉)
---
# Session one RDMA Foundation
## Receive Request
ibv_post_recv() posts a linked list of Work Requests (WRs) to the Receive Queue(RQ) of a Queue Pair (QP).
![](https://pic1.zhimg.com/80/v2-ea7615096a651042d6ff0758d85ad698_1440w.jpg)
```cpp
int ibv_post_recv(struct ibv_qp *qp, struct ibv_recv_wr *wr,
                  struct ibv_recv_wr **bad_wr);

struct ibv_recv_wr {
	uint64_t		wr_id;
	struct ibv_recv_wr     *next;
	struct ibv_sge	       *sg_list; // Array of scatter elements
	int			num_sge;
};

struct ibv_sge {
	uint64_t		addr;   // buffer address
	uint32_t		length; // buffer length
	uint32_t		lkey;   // local key
};

struct ibv_qp {
	struct ibv_context     *context;
	void		       *qp_context;
	struct ibv_pd	       *pd;
	struct ibv_cq	       *send_cq;
	struct ibv_cq	       *recv_cq;
	struct ibv_srq	       *srq;
	uint32_t		handle;
	uint32_t		qp_num;
	enum ibv_qp_state      state;
	enum ibv_qp_type	qp_type;

	pthread_mutex_t		mutex;
	pthread_cond_t		cond;
	uint32_t		events_completed;
};
```
```mermaid
graph LR
	subgraph ivb_sge
		buffer[Memery buffer address]
		bsize(buffer size)
		lkey(lkey)
	end
	subgraph ibv_recv_wr			
		WR_0(WR_0) --> WR_1(WR_0)
		WR_1(WR_1)--> WR_2(WR_2)
		WR_2(WR_2) --> ...
		... --> WR_NULL
	end
	A(Machina A)
	B(Machina B)
	A --"Communication Manager(CM)"--- B
```
```cpp=
struct ibv_sge sg;
struct ibv_recv_wr wr;
struct ibv_recv_wr *bad_wr;
 
memset(&sg, 0, sizeof(sg));
sg.addr	  = (uintptr_t)buf_addr;
sg.length = buf_size;
sg.lkey	  = mr->lkey;
 
memset(&wr, 0, sizeof(wr));
wr.wr_id      = 0;
wr.sg_list    = &sg;
wr.num_sge    = 1;
 
if (ibv_post_recv(qp, &wr, &bad_wr)) {
	fprintf(stderr, "Error, ibv_post_recv() failed\n");
	return -1;
}
```
## General tips on libibverbs
![](https://i.imgur.com/x3597f2.jpg)
```bash=
sudo apt update
sudo apt install libibverbs-dev -y
#sudo apt install rdma-core -y
#sudo apt install librdmacm-dev -y
```
## Reference :
[Struct Defination](https://github.com/linux-rdma/rdma-core/blob/v15/libibverbs/verbs.h#L1044)
[ibv_post_recv() ](https://www.rdmamojo.com/2013/02/02/ibv_post_recv/)