1 /* SPDX-License-Identifier: GPL-2.0-or-later */
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.
13 #include <linux/uuid.h>
14 #include <rdma/rdma_cm.h>
15 #include <rdma/ib_verbs.h>
20 #define RTRS_PROTO_VER_MAJOR 2
21 #define RTRS_PROTO_VER_MINOR 0
23 #define RTRS_PROTO_VER_STRING __stringify(RTRS_PROTO_VER_MAJOR) "." \
24 __stringify(RTRS_PROTO_VER_MINOR)
27 MAX_IMM_TYPE_BITS = 4,
28 MAX_IMM_TYPE_MASK = ((1 << MAX_IMM_TYPE_BITS) - 1),
29 MAX_IMM_PAYL_BITS = 28,
30 MAX_IMM_PAYL_MASK = ((1 << MAX_IMM_PAYL_BITS) - 1),
34 RTRS_IO_REQ_IMM = 0, /* client to server */
35 RTRS_IO_RSP_IMM = 1, /* server to client */
36 RTRS_IO_RSP_W_INV_IMM = 2, /* server to client */
38 RTRS_HB_MSG_IMM = 8, /* HB: HeartBeat */
45 SERVICE_CON_QUEUE_DEPTH = 512,
50 * With the size of struct rtrs_permit allocated on the client, 4K
51 * is the maximum number of rtrs_permits we can allocate. This number is
52 * also used on the client to allocate the IU for the user connection
53 * to receive the RDMA addresses from the server.
55 MAX_SESS_QUEUE_DEPTH = 4096,
57 RTRS_HB_INTERVAL_MS = 5000,
58 RTRS_HB_MISSED_MAX = 5,
61 RTRS_PROTO_VER = (RTRS_PROTO_VER_MAJOR << 8) | RTRS_PROTO_VER_MINOR,
66 struct rtrs_rdma_dev_pd_ops {
67 struct rtrs_ib_dev *(*alloc)(void);
68 void (*free)(struct rtrs_ib_dev *dev);
69 int (*init)(struct rtrs_ib_dev *dev);
70 void (*deinit)(struct rtrs_ib_dev *dev);
73 struct rtrs_rdma_dev_pd {
75 struct list_head list;
76 enum ib_pd_flags pd_flags;
77 const struct rtrs_rdma_dev_pd_ops *ops;
81 struct ib_device *ib_dev;
84 struct list_head entry;
85 struct rtrs_rdma_dev_pd *pool;
89 struct rtrs_sess *sess;
92 struct rdma_cm_id *cm_id;
98 struct list_head entry;
99 struct sockaddr_storage dst_addr;
100 struct sockaddr_storage src_addr;
101 char sessname[NAME_MAX];
103 struct rtrs_con **con;
104 unsigned int con_num;
105 unsigned int recon_cnt;
106 struct rtrs_ib_dev *dev;
108 struct ib_cqe *hb_cqe;
109 void (*hb_err_handler)(struct rtrs_con *con);
110 struct workqueue_struct *hb_wq;
111 struct delayed_work hb_dwork;
112 unsigned int hb_interval_ms;
113 unsigned int hb_missed_cnt;
114 unsigned int hb_missed_max;
117 /* rtrs information unit */
123 enum dma_data_direction direction;
127 * enum rtrs_msg_types - RTRS message types, see also rtrs/README
128 * @RTRS_MSG_INFO_REQ: Client additional info request to the server
129 * @RTRS_MSG_INFO_RSP: Server additional info response to the client
130 * @RTRS_MSG_WRITE: Client writes data per RDMA to server
131 * @RTRS_MSG_READ: Client requests data transfer from server
132 * @RTRS_MSG_RKEY_RSP: Server refreshed rkey for rbuf
134 enum rtrs_msg_types {
143 * enum rtrs_msg_flags - RTRS message flags.
144 * @RTRS_NEED_INVAL: Send invalidation in response.
145 * @RTRS_MSG_NEW_RKEY_F: Send refreshed rkey in response.
147 enum rtrs_msg_flags {
148 RTRS_MSG_NEED_INVAL_F = 1 << 0,
149 RTRS_MSG_NEW_RKEY_F = 1 << 1,
153 * struct rtrs_sg_desc - RDMA-Buffer entry description
154 * @addr: Address of RDMA destination buffer
155 * @key: Authorization rkey to write to the buffer
156 * @len: Size of the buffer
158 struct rtrs_sg_desc {
165 * struct rtrs_msg_conn_req - Client connection request to the server
167 * @version: RTRS protocol version
168 * @cid: Current connection id
169 * @cid_num: Number of connections per session
170 * @recon_cnt: Reconnections counter
171 * @sess_uuid: UUID of a session (path)
172 * @paths_uuid: UUID of a group of sessions (paths)
174 * NOTE: max size 56 bytes, see man rdma_connect().
176 struct rtrs_msg_conn_req {
177 /* Is set to 0 by cma.c in case of AF_IB, do not touch that.
178 * see https://www.spinics.net/lists/linux-rdma/msg22397.html
181 /* On sender side that should be set to 0, or cma_save_ip_info()
182 * extract garbage and will fail.
193 u8 reserved_bits : 7;
198 * struct rtrs_msg_conn_rsp - Server connection response to the client
200 * @version: RTRS protocol version
201 * @errno: If rdma_accept() then 0, if rdma_reject() indicates error
202 * @queue_depth: max inflight messages (queue-depth) in this session
203 * @max_io_size: max io size server supports
204 * @max_hdr_size: max msg header size server supports
206 * NOTE: size is 56 bytes, max possible is 136 bytes, see man rdma_accept().
208 struct rtrs_msg_conn_rsp {
220 * struct rtrs_msg_info_req
221 * @type: @RTRS_MSG_INFO_REQ
222 * @sessname: Session name chosen by client
224 struct rtrs_msg_info_req {
226 u8 sessname[NAME_MAX];
231 * struct rtrs_msg_info_rsp
232 * @type: @RTRS_MSG_INFO_RSP
233 * @sg_cnt: Number of @desc entries
234 * @desc: RDMA buffers where the client can write to server
236 struct rtrs_msg_info_rsp {
240 struct rtrs_sg_desc desc[];
244 * struct rtrs_msg_rkey_rsp
245 * @type: @RTRS_MSG_RKEY_RSP
246 * @buf_id: RDMA buf_id of the new rkey
247 * @rkey: new remote key for RDMA buffers id from server
249 struct rtrs_msg_rkey_rsp {
256 * struct rtrs_msg_rdma_read - RDMA data transfer request from client
257 * @type: always @RTRS_MSG_READ
258 * @usr_len: length of user payload
259 * @sg_cnt: number of @desc entries
260 * @desc: RDMA buffers where the server can write the result to
262 struct rtrs_msg_rdma_read {
267 struct rtrs_sg_desc desc[];
271 * struct_msg_rdma_write - Message transferred to server with RDMA-Write
272 * @type: always @RTRS_MSG_WRITE
273 * @usr_len: length of user payload
275 struct rtrs_msg_rdma_write {
281 * struct_msg_rdma_hdr - header for read or write request
282 * @type: @RTRS_MSG_WRITE | @RTRS_MSG_READ
284 struct rtrs_msg_rdma_hdr {
290 struct rtrs_iu *rtrs_iu_alloc(u32 queue_size, size_t size, gfp_t t,
291 struct ib_device *dev, enum dma_data_direction,
292 void (*done)(struct ib_cq *cq, struct ib_wc *wc));
293 void rtrs_iu_free(struct rtrs_iu *iu, struct ib_device *dev, u32 queue_size);
294 int rtrs_iu_post_recv(struct rtrs_con *con, struct rtrs_iu *iu);
295 int rtrs_iu_post_send(struct rtrs_con *con, struct rtrs_iu *iu, size_t size,
296 struct ib_send_wr *head);
297 int rtrs_iu_post_rdma_write_imm(struct rtrs_con *con, struct rtrs_iu *iu,
298 struct ib_sge *sge, unsigned int num_sge,
299 u32 rkey, u64 rdma_addr, u32 imm_data,
300 enum ib_send_flags flags,
301 struct ib_send_wr *head);
303 int rtrs_post_recv_empty(struct rtrs_con *con, struct ib_cqe *cqe);
304 int rtrs_post_rdma_write_imm_empty(struct rtrs_con *con, struct ib_cqe *cqe,
305 u32 imm_data, enum ib_send_flags flags,
306 struct ib_send_wr *head);
308 int rtrs_cq_qp_create(struct rtrs_sess *rtrs_sess, struct rtrs_con *con,
309 u32 max_send_sge, int cq_vector, int cq_size,
310 u32 max_send_wr, u32 max_recv_wr,
311 enum ib_poll_context poll_ctx);
312 void rtrs_cq_qp_destroy(struct rtrs_con *con);
314 void rtrs_init_hb(struct rtrs_sess *sess, struct ib_cqe *cqe,
315 unsigned int interval_ms, unsigned int missed_max,
316 void (*err_handler)(struct rtrs_con *con),
317 struct workqueue_struct *wq);
318 void rtrs_start_hb(struct rtrs_sess *sess);
319 void rtrs_stop_hb(struct rtrs_sess *sess);
320 void rtrs_send_hb_ack(struct rtrs_sess *sess);
322 void rtrs_rdma_dev_pd_init(enum ib_pd_flags pd_flags,
323 struct rtrs_rdma_dev_pd *pool);
324 void rtrs_rdma_dev_pd_deinit(struct rtrs_rdma_dev_pd *pool);
326 struct rtrs_ib_dev *rtrs_ib_dev_find_or_add(struct ib_device *ib_dev,
327 struct rtrs_rdma_dev_pd *pool);
328 int rtrs_ib_dev_put(struct rtrs_ib_dev *dev);
330 static inline u32 rtrs_to_imm(u32 type, u32 payload)
332 BUILD_BUG_ON(MAX_IMM_PAYL_BITS + MAX_IMM_TYPE_BITS != 32);
333 BUILD_BUG_ON(RTRS_LAST_IMM > (1<<MAX_IMM_TYPE_BITS));
334 return ((type & MAX_IMM_TYPE_MASK) << MAX_IMM_PAYL_BITS) |
335 (payload & MAX_IMM_PAYL_MASK);
338 static inline void rtrs_from_imm(u32 imm, u32 *type, u32 *payload)
340 *payload = imm & MAX_IMM_PAYL_MASK;
341 *type = imm >> MAX_IMM_PAYL_BITS;
344 static inline u32 rtrs_to_io_req_imm(u32 addr)
346 return rtrs_to_imm(RTRS_IO_REQ_IMM, addr);
349 static inline u32 rtrs_to_io_rsp_imm(u32 msg_id, int errno, bool w_inval)
351 enum rtrs_imm_type type;
354 /* 9 bits for errno, 19 bits for msg_id */
355 payload = (abs(errno) & 0x1ff) << 19 | (msg_id & 0x7ffff);
356 type = w_inval ? RTRS_IO_RSP_W_INV_IMM : RTRS_IO_RSP_IMM;
358 return rtrs_to_imm(type, payload);
361 static inline void rtrs_from_io_rsp_imm(u32 payload, u32 *msg_id, int *errno)
363 /* 9 bits for errno, 19 bits for msg_id */
364 *msg_id = payload & 0x7ffff;
365 *errno = -(int)((payload >> 19) & 0x1ff);
368 #define STAT_STORE_FUNC(type, set_value, reset) \
369 static ssize_t set_value##_store(struct kobject *kobj, \
370 struct kobj_attribute *attr, \
371 const char *buf, size_t count) \
374 type *stats = container_of(kobj, type, kobj_stats); \
376 if (sysfs_streq(buf, "1")) \
377 ret = reset(stats, true); \
378 else if (sysfs_streq(buf, "0")) \
379 ret = reset(stats, false); \
386 #define STAT_SHOW_FUNC(type, get_value, print) \
387 static ssize_t get_value##_show(struct kobject *kobj, \
388 struct kobj_attribute *attr, \
391 type *stats = container_of(kobj, type, kobj_stats); \
393 return print(stats, page, PAGE_SIZE); \
396 #define STAT_ATTR(type, stat, print, reset) \
397 STAT_STORE_FUNC(type, stat, reset) \
398 STAT_SHOW_FUNC(type, stat, print) \
399 static struct kobj_attribute stat##_attr = __ATTR_RW(stat)
401 #endif /* RTRS_PRI_H */