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