Derecho  0.9
Distributed systems toolkit for RDMA
verbs_helper.hpp
Go to the documentation of this file.
1 #ifndef VERBS_HELPER_HPP
2 #define VERBS_HELPER_HPP
3 
4 #include <cstdint>
5 #include <optional>
6 #include <functional>
7 #include <map>
8 #include <memory>
9 #include <string>
10 #include <vector>
11 
12 struct ibv_mr;
13 struct ibv_qp;
14 struct ibv_cq;
15 
22 namespace rdma {
23 // Various classes of exceptions
24 class exception {};
25 class invalid_args : public exception {};
26 class connection_broken : public exception {};
27 class creation_failure : public exception {};
28 class mr_creation_failure : public creation_failure {};
29 class cq_creation_failure : public creation_failure {};
31 class message_types_exhausted : public exception {};
32 class unsupported_feature : public exception {};
33 
40 class memory_region {
41  std::unique_ptr<ibv_mr, std::function<void(ibv_mr*)>> mr;
42  std::unique_ptr<char[]> allocated_buffer;
43 
44  memory_region(size_t size, bool contiguous);
45  friend class queue_pair;
46  friend class task;
47 
48 public:
49  memory_region(size_t size);
50  memory_region(char* buffer, size_t size);
51  uint32_t get_rkey() const;
52 
53  char* const buffer;
54  const size_t size;
55 };
56 
58 public:
59  remote_memory_region(uint64_t remote_address, size_t length,
60  uint32_t remote_key)
61  : buffer(remote_address), size(length), rkey(remote_key) {}
62 
63  const uint64_t buffer;
64  const size_t size;
65  const uint32_t rkey;
66 };
67 
68 class completion_queue {
69  std::unique_ptr<ibv_cq, std::function<void(ibv_cq*)>> cq;
70  friend class managed_queue_pair;
71  friend class task;
72 
73 public:
74  explicit completion_queue(bool cross_channel);
75 };
76 
77 typedef std::function<void(uint64_t tag, uint32_t immediate, size_t length)>
79 
80 class message_type {
81 public:
82  typedef uint8_t tag_type;
83  static constexpr unsigned int shift_bits = 64 - 8 * sizeof(tag_type);
84 
85 private:
86  std::optional<tag_type> tag;
87  message_type(tag_type t) : tag(t) {}
88 
89  friend class queue_pair;
90  friend class task;
91 
92 public:
93  message_type(const std::string& name, completion_handler send_handler,
94  completion_handler recv_handler,
95  completion_handler write_handler = nullptr);
97 
98  static message_type ignored();
99 };
100 
106 class queue_pair {
107 protected:
108  std::unique_ptr<ibv_qp, std::function<void(ibv_qp*)>> qp;
109  explicit queue_pair() {}
110 
111  friend class task;
112 
113 public:
114  ~queue_pair();
115  explicit queue_pair(size_t remote_index);
116  queue_pair(size_t remote_index,
117  std::function<void(queue_pair*)> post_recvs);
118  queue_pair(queue_pair&&) = default;
119  bool post_send(const memory_region& mr, size_t offset, size_t length,
120  uint64_t wr_id, uint32_t immediate,
121  const message_type& type);
122  bool post_recv(const memory_region& mr, size_t offset, size_t length,
123  uint64_t wr_id, const message_type& type);
124 
125  bool post_empty_send(uint64_t wr_id, uint32_t immediate,
126  const message_type& type);
127  bool post_empty_recv(uint64_t wr_id, const message_type& type);
128 
129  bool post_write(const memory_region& mr, size_t offset, size_t length,
130  uint64_t wr_id, remote_memory_region remote_mr,
131  size_t remote_offset, const message_type& type,
132  bool signaled = false, bool send_inline = false);
133 };
134 
136 public:
138  managed_queue_pair(size_t remote_index,
139  std::function<void(managed_queue_pair*)> post_recvs);
140 };
141 
143 public:
144  explicit manager_queue_pair();
145 };
146 
147 class task {
148 protected:
149  struct task_impl;
150  std::unique_ptr<task_impl> impl;
151  std::shared_ptr<manager_queue_pair> mqp;
152 
153 public:
154  task(std::shared_ptr<manager_queue_pair> manager_qp);
155  virtual ~task();
156 
157  void append_wait(const completion_queue& cq, int count, bool signaled,
158  bool last, uint64_t wr_id, const message_type& type);
159  void append_enable_send(const managed_queue_pair& qp, int count);
160  void append_send(const managed_queue_pair& qp, const memory_region& mr,
161  size_t offset, size_t length, uint32_t immediate);
162  void append_recv(const managed_queue_pair& qp, const memory_region& mr,
163  size_t offset, size_t length);
164  bool post() __attribute__((warn_unused_result));
165 };
166 
167 struct feature_set {
170 };
172 
173 namespace impl {
174 bool verbs_initialize(const std::map<uint32_t, std::string>& node_addresses,
175  uint32_t node_rank);
176 bool verbs_add_connection(uint32_t index, const std::string& address,
177  uint32_t node_rank);
178 bool verbs_remove_connection(uint32_t index);
179 void verbs_destroy();
180 // int poll_for_completions(int num, ibv_wc* wcs,
181 // std::atomic<bool>& shutdown_flag);
182 
183 // This function exchanges memory regions with all other connected nodes which
184 // enables us to do one-sided RDMA operations between them. Due to its nature,
185 // the function requires that it is called simultaneously on all nodes and that
186 // only one execution is active at any time.
187 std::map<uint32_t, remote_memory_region> verbs_exchange_memory_regions(
188  const std::vector<uint32_t>& members, uint32_t node_rank,
189  const memory_region& mr);
190 
191 bool set_interrupt_mode(bool enabled);
192 bool set_contiguous_memory_mode(bool enabled);
193 
194 } /* namespace impl */
195 } /* namespace rdma */
196 #endif /* VERBS_HELPER_HPP */
std::unique_ptr< ibv_mr, std::function< void(ibv_mr *)> > mr
partial_wrapped< Tag, Ret, NewClass, Args... > tag(Ret(NewClass::*fun)(Args...))
User-facing entry point for the series of functions that binds a FunctionTag to a class&#39;s member func...
std::function< void(uint64_t tag, uint32_t immediate, size_t length)> completion_handler
Definition: lf_helper.hpp:131
std::map< uint32_t, remote_memory_region > verbs_exchange_memory_regions(const std::vector< uint32_t > &members, uint32_t node_rank, const memory_region &mr)
A wrapper for fi_close.
Definition: lf_helper.hpp:53
bool verbs_add_connection(uint32_t index, const std::string &address, uint32_t node_rank)
struct rdma::completion_handler_set __attribute__
A C++ wrapper for the libfabric fid_cq struct and its associated functions.
Definition: lf_helper.hpp:115
uint32_t node_rank
Definition: experiment.cpp:45
A C++ wrapper for the IB Verbs ibv_qp struct and its associated functions.
uint32_t rkey
Remote key.
Definition: verbs.hpp:290
Contains functions and classes for low-level RDMA operations, such as setting up memory regions and q...
Definition: lf_helper.hpp:28
bool verbs_initialize(const std::map< uint32_t, std::string > &node_addresses, uint32_t node_rank)
std::shared_ptr< manager_queue_pair > mqp
remote_memory_region(uint64_t remote_address, size_t length, uint32_t remote_key)
message_type(tag_type t)
bool set_contiguous_memory_mode(bool enabled)
void verbs_destroy()
std::unique_ptr< ibv_cq, std::function< void(ibv_cq *)> > cq
bool set_interrupt_mode(bool enabled)
Definition: lf_helper.cpp:752
feature_set get_supported_features()
bool verbs_remove_connection(uint32_t index)
std::unique_ptr< ibv_qp, std::function< void(ibv_qp *)> > qp