rtrs-pri.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. /* SPDX-License-Identifier: GPL-2.0-or-later */
  2. /*
  3. * RDMA Transport Layer
  4. *
  5. * Copyright (c) 2014 - 2018 ProfitBricks GmbH. All rights reserved.
  6. * Copyright (c) 2018 - 2019 1&1 IONOS Cloud GmbH. All rights reserved.
  7. * Copyright (c) 2019 - 2020 1&1 IONOS SE. All rights reserved.
  8. */
  9. #ifndef RTRS_PRI_H
  10. #define RTRS_PRI_H
  11. #include <linux/uuid.h>
  12. #include <rdma/rdma_cm.h>
  13. #include <rdma/ib_verbs.h>
  14. #include <rdma/ib.h>
  15. #include "rtrs.h"
  16. #define RTRS_PROTO_VER_MAJOR 2
  17. #define RTRS_PROTO_VER_MINOR 0
  18. #define RTRS_PROTO_VER_STRING __stringify(RTRS_PROTO_VER_MAJOR) "." \
  19. __stringify(RTRS_PROTO_VER_MINOR)
  20. /*
  21. * Max IB immediate data size is 2^28 (MAX_IMM_PAYL_BITS)
  22. * and the minimum chunk size is 4096 (2^12).
  23. * So the maximum sess_queue_depth is 65535 (2^16 - 1) in theory
  24. * since queue_depth in rtrs_msg_conn_rsp is defined as le16.
  25. * Therefore the pratical max value of sess_queue_depth is
  26. * somewhere between 1 and 65535 and it depends on the system.
  27. */
  28. #define MAX_SESS_QUEUE_DEPTH 65535
  29. enum rtrs_imm_const {
  30. MAX_IMM_TYPE_BITS = 4,
  31. MAX_IMM_TYPE_MASK = ((1 << MAX_IMM_TYPE_BITS) - 1),
  32. MAX_IMM_PAYL_BITS = 28,
  33. MAX_IMM_PAYL_MASK = ((1 << MAX_IMM_PAYL_BITS) - 1),
  34. };
  35. enum rtrs_imm_type {
  36. RTRS_IO_REQ_IMM = 0, /* client to server */
  37. RTRS_IO_RSP_IMM = 1, /* server to client */
  38. RTRS_IO_RSP_W_INV_IMM = 2, /* server to client */
  39. RTRS_HB_MSG_IMM = 8, /* HB: HeartBeat */
  40. RTRS_HB_ACK_IMM = 9,
  41. RTRS_LAST_IMM,
  42. };
  43. enum {
  44. SERVICE_CON_QUEUE_DEPTH = 512,
  45. MAX_PATHS_NUM = 128,
  46. MIN_CHUNK_SIZE = 8192,
  47. RTRS_HB_INTERVAL_MS = 5000,
  48. RTRS_HB_MISSED_MAX = 5,
  49. RTRS_MAGIC = 0x1BBD,
  50. RTRS_PROTO_VER = (RTRS_PROTO_VER_MAJOR << 8) | RTRS_PROTO_VER_MINOR,
  51. };
  52. struct rtrs_ib_dev;
  53. struct rtrs_rdma_dev_pd_ops {
  54. int (*init)(struct rtrs_ib_dev *dev);
  55. void (*deinit)(struct rtrs_ib_dev *dev);
  56. };
  57. struct rtrs_rdma_dev_pd {
  58. struct mutex mutex;
  59. struct list_head list;
  60. enum ib_pd_flags pd_flags;
  61. const struct rtrs_rdma_dev_pd_ops *ops;
  62. };
  63. struct rtrs_ib_dev {
  64. struct ib_device *ib_dev;
  65. struct ib_pd *ib_pd;
  66. struct kref ref;
  67. struct list_head entry;
  68. struct rtrs_rdma_dev_pd *pool;
  69. struct ib_event_handler event_handler;
  70. };
  71. struct rtrs_con {
  72. struct rtrs_path *path;
  73. struct ib_qp *qp;
  74. struct ib_cq *cq;
  75. struct rdma_cm_id *cm_id;
  76. unsigned int cid;
  77. int nr_cqe;
  78. atomic_t wr_cnt;
  79. atomic_t sq_wr_avail;
  80. };
  81. struct rtrs_path {
  82. struct list_head entry;
  83. struct sockaddr_storage dst_addr;
  84. struct sockaddr_storage src_addr;
  85. char sessname[NAME_MAX];
  86. uuid_t uuid;
  87. struct rtrs_con **con;
  88. unsigned int con_num;
  89. unsigned int irq_con_num;
  90. unsigned int recon_cnt;
  91. unsigned int signal_interval;
  92. struct rtrs_ib_dev *dev;
  93. int dev_ref;
  94. struct ib_cqe *hb_cqe;
  95. void (*hb_err_handler)(struct rtrs_con *con);
  96. struct workqueue_struct *hb_wq;
  97. struct delayed_work hb_dwork;
  98. unsigned int hb_interval_ms;
  99. unsigned int hb_missed_cnt;
  100. unsigned int hb_missed_max;
  101. ktime_t hb_last_sent;
  102. ktime_t hb_cur_latency;
  103. };
  104. /* rtrs information unit */
  105. struct rtrs_iu {
  106. struct ib_cqe cqe;
  107. dma_addr_t dma_addr;
  108. void *buf;
  109. size_t size;
  110. enum dma_data_direction direction;
  111. };
  112. /**
  113. * enum rtrs_msg_types - RTRS message types, see also rtrs/README
  114. * @RTRS_MSG_INFO_REQ: Client additional info request to the server
  115. * @RTRS_MSG_INFO_RSP: Server additional info response to the client
  116. * @RTRS_MSG_WRITE: Client writes data per RDMA to server
  117. * @RTRS_MSG_READ: Client requests data transfer from server
  118. * @RTRS_MSG_RKEY_RSP: Server refreshed rkey for rbuf
  119. */
  120. enum rtrs_msg_types {
  121. RTRS_MSG_INFO_REQ,
  122. RTRS_MSG_INFO_RSP,
  123. RTRS_MSG_WRITE,
  124. RTRS_MSG_READ,
  125. RTRS_MSG_RKEY_RSP,
  126. };
  127. /**
  128. * enum rtrs_msg_flags - RTRS message flags.
  129. * @RTRS_NEED_INVAL: Send invalidation in response.
  130. * @RTRS_MSG_NEW_RKEY_F: Send refreshed rkey in response.
  131. */
  132. enum rtrs_msg_flags {
  133. RTRS_MSG_NEED_INVAL_F = 1 << 0,
  134. RTRS_MSG_NEW_RKEY_F = 1 << 1,
  135. };
  136. /**
  137. * struct rtrs_sg_desc - RDMA-Buffer entry description
  138. * @addr: Address of RDMA destination buffer
  139. * @key: Authorization rkey to write to the buffer
  140. * @len: Size of the buffer
  141. */
  142. struct rtrs_sg_desc {
  143. __le64 addr;
  144. __le32 key;
  145. __le32 len;
  146. };
  147. /**
  148. * struct rtrs_msg_conn_req - Client connection request to the server
  149. * @magic: RTRS magic
  150. * @version: RTRS protocol version
  151. * @cid: Current connection id
  152. * @cid_num: Number of connections per session
  153. * @recon_cnt: Reconnections counter
  154. * @sess_uuid: UUID of a session (path)
  155. * @paths_uuid: UUID of a group of sessions (paths)
  156. *
  157. * NOTE: max size 56 bytes, see man rdma_connect().
  158. */
  159. struct rtrs_msg_conn_req {
  160. /* Is set to 0 by cma.c in case of AF_IB, do not touch that.
  161. * see https://www.spinics.net/lists/linux-rdma/msg22397.html
  162. */
  163. u8 __cma_version;
  164. /* On sender side that should be set to 0, or cma_save_ip_info()
  165. * extract garbage and will fail.
  166. */
  167. u8 __ip_version;
  168. __le16 magic;
  169. __le16 version;
  170. __le16 cid;
  171. __le16 cid_num;
  172. __le16 recon_cnt;
  173. uuid_t sess_uuid;
  174. uuid_t paths_uuid;
  175. u8 first_conn : 1;
  176. u8 reserved_bits : 7;
  177. u8 reserved[11];
  178. };
  179. /**
  180. * struct rtrs_msg_conn_rsp - Server connection response to the client
  181. * @magic: RTRS magic
  182. * @version: RTRS protocol version
  183. * @errno: If rdma_accept() then 0, if rdma_reject() indicates error
  184. * @queue_depth: max inflight messages (queue-depth) in this session
  185. * @max_io_size: max io size server supports
  186. * @max_hdr_size: max msg header size server supports
  187. *
  188. * NOTE: size is 56 bytes, max possible is 136 bytes, see man rdma_accept().
  189. */
  190. struct rtrs_msg_conn_rsp {
  191. __le16 magic;
  192. __le16 version;
  193. __le16 errno;
  194. __le16 queue_depth;
  195. __le32 max_io_size;
  196. __le32 max_hdr_size;
  197. __le32 flags;
  198. u8 reserved[36];
  199. };
  200. /**
  201. * struct rtrs_msg_info_req
  202. * @type: @RTRS_MSG_INFO_REQ
  203. * @pathname: Path name chosen by client
  204. */
  205. struct rtrs_msg_info_req {
  206. __le16 type;
  207. u8 pathname[NAME_MAX];
  208. u8 reserved[15];
  209. };
  210. /**
  211. * struct rtrs_msg_info_rsp
  212. * @type: @RTRS_MSG_INFO_RSP
  213. * @sg_cnt: Number of @desc entries
  214. * @desc: RDMA buffers where the client can write to server
  215. */
  216. struct rtrs_msg_info_rsp {
  217. __le16 type;
  218. __le16 sg_cnt;
  219. u8 reserved[4];
  220. struct rtrs_sg_desc desc[];
  221. };
  222. /**
  223. * struct rtrs_msg_rkey_rsp
  224. * @type: @RTRS_MSG_RKEY_RSP
  225. * @buf_id: RDMA buf_id of the new rkey
  226. * @rkey: new remote key for RDMA buffers id from server
  227. */
  228. struct rtrs_msg_rkey_rsp {
  229. __le16 type;
  230. __le16 buf_id;
  231. __le32 rkey;
  232. };
  233. /**
  234. * struct rtrs_msg_rdma_read - RDMA data transfer request from client
  235. * @type: always @RTRS_MSG_READ
  236. * @usr_len: length of user payload
  237. * @sg_cnt: number of @desc entries
  238. * @desc: RDMA buffers where the server can write the result to
  239. */
  240. struct rtrs_msg_rdma_read {
  241. __le16 type;
  242. __le16 usr_len;
  243. __le16 flags;
  244. __le16 sg_cnt;
  245. struct rtrs_sg_desc desc[];
  246. };
  247. /**
  248. * struct_msg_rdma_write - Message transferred to server with RDMA-Write
  249. * @type: always @RTRS_MSG_WRITE
  250. * @usr_len: length of user payload
  251. */
  252. struct rtrs_msg_rdma_write {
  253. __le16 type;
  254. __le16 usr_len;
  255. };
  256. /**
  257. * struct_msg_rdma_hdr - header for read or write request
  258. * @type: @RTRS_MSG_WRITE | @RTRS_MSG_READ
  259. */
  260. struct rtrs_msg_rdma_hdr {
  261. __le16 type;
  262. };
  263. /* rtrs.c */
  264. struct rtrs_iu *rtrs_iu_alloc(u32 queue_num, size_t size, gfp_t t,
  265. struct ib_device *dev, enum dma_data_direction,
  266. void (*done)(struct ib_cq *cq, struct ib_wc *wc));
  267. void rtrs_iu_free(struct rtrs_iu *iu, struct ib_device *dev, u32 queue_num);
  268. int rtrs_iu_post_recv(struct rtrs_con *con, struct rtrs_iu *iu);
  269. int rtrs_iu_post_send(struct rtrs_con *con, struct rtrs_iu *iu, size_t size,
  270. struct ib_send_wr *head);
  271. int rtrs_iu_post_rdma_write_imm(struct rtrs_con *con, struct rtrs_iu *iu,
  272. struct ib_sge *sge, unsigned int num_sge,
  273. u32 rkey, u64 rdma_addr, u32 imm_data,
  274. enum ib_send_flags flags,
  275. struct ib_send_wr *head,
  276. struct ib_send_wr *tail);
  277. int rtrs_post_recv_empty(struct rtrs_con *con, struct ib_cqe *cqe);
  278. int rtrs_cq_qp_create(struct rtrs_path *path, struct rtrs_con *con,
  279. u32 max_send_sge, int cq_vector, int nr_cqe,
  280. u32 max_send_wr, u32 max_recv_wr,
  281. enum ib_poll_context poll_ctx);
  282. void rtrs_cq_qp_destroy(struct rtrs_con *con);
  283. void rtrs_init_hb(struct rtrs_path *path, struct ib_cqe *cqe,
  284. unsigned int interval_ms, unsigned int missed_max,
  285. void (*err_handler)(struct rtrs_con *con),
  286. struct workqueue_struct *wq);
  287. void rtrs_start_hb(struct rtrs_path *path);
  288. void rtrs_stop_hb(struct rtrs_path *path);
  289. void rtrs_send_hb_ack(struct rtrs_path *path);
  290. void rtrs_rdma_dev_pd_init(enum ib_pd_flags pd_flags,
  291. struct rtrs_rdma_dev_pd *pool);
  292. void rtrs_rdma_dev_pd_deinit(struct rtrs_rdma_dev_pd *pool);
  293. struct rtrs_ib_dev *rtrs_ib_dev_find_or_add(struct ib_device *ib_dev,
  294. struct rtrs_rdma_dev_pd *pool);
  295. int rtrs_ib_dev_put(struct rtrs_ib_dev *dev);
  296. static inline u32 rtrs_to_imm(u32 type, u32 payload)
  297. {
  298. BUILD_BUG_ON(MAX_IMM_PAYL_BITS + MAX_IMM_TYPE_BITS != 32);
  299. BUILD_BUG_ON(RTRS_LAST_IMM > (1<<MAX_IMM_TYPE_BITS));
  300. return ((type & MAX_IMM_TYPE_MASK) << MAX_IMM_PAYL_BITS) |
  301. (payload & MAX_IMM_PAYL_MASK);
  302. }
  303. static inline void rtrs_from_imm(u32 imm, u32 *type, u32 *payload)
  304. {
  305. *payload = imm & MAX_IMM_PAYL_MASK;
  306. *type = imm >> MAX_IMM_PAYL_BITS;
  307. }
  308. static inline u32 rtrs_to_io_req_imm(u32 addr)
  309. {
  310. return rtrs_to_imm(RTRS_IO_REQ_IMM, addr);
  311. }
  312. static inline u32 rtrs_to_io_rsp_imm(u32 msg_id, int errno, bool w_inval)
  313. {
  314. enum rtrs_imm_type type;
  315. u32 payload;
  316. /* 9 bits for errno, 19 bits for msg_id */
  317. payload = (abs(errno) & 0x1ff) << 19 | (msg_id & 0x7ffff);
  318. type = w_inval ? RTRS_IO_RSP_W_INV_IMM : RTRS_IO_RSP_IMM;
  319. return rtrs_to_imm(type, payload);
  320. }
  321. static inline void rtrs_from_io_rsp_imm(u32 payload, u32 *msg_id, int *errno)
  322. {
  323. /* 9 bits for errno, 19 bits for msg_id */
  324. *msg_id = payload & 0x7ffff;
  325. *errno = -(int)((payload >> 19) & 0x1ff);
  326. }
  327. #define STAT_STORE_FUNC(type, set_value, reset) \
  328. static ssize_t set_value##_store(struct kobject *kobj, \
  329. struct kobj_attribute *attr, \
  330. const char *buf, size_t count) \
  331. { \
  332. int ret = -EINVAL; \
  333. type *stats = container_of(kobj, type, kobj_stats); \
  334. \
  335. if (sysfs_streq(buf, "1")) \
  336. ret = reset(stats, true); \
  337. else if (sysfs_streq(buf, "0")) \
  338. ret = reset(stats, false); \
  339. if (ret) \
  340. return ret; \
  341. \
  342. return count; \
  343. }
  344. #define STAT_SHOW_FUNC(type, get_value, print) \
  345. static ssize_t get_value##_show(struct kobject *kobj, \
  346. struct kobj_attribute *attr, \
  347. char *page) \
  348. { \
  349. type *stats = container_of(kobj, type, kobj_stats); \
  350. \
  351. return print(stats, page); \
  352. }
  353. #define STAT_ATTR(type, stat, print, reset) \
  354. STAT_STORE_FUNC(type, stat, reset) \
  355. STAT_SHOW_FUNC(type, stat, print) \
  356. static struct kobj_attribute stat##_attr = __ATTR_RW(stat)
  357. #endif /* RTRS_PRI_H */