xref: /linux/net/smc/smc.h (revision 06b9cce42634a50f2840777a66553b02320db5ef)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  *  Shared Memory Communications over RDMA (SMC-R) and RoCE
4  *
5  *  Definitions for the SMC module (socket related)
6  *
7  *  Copyright IBM Corp. 2016
8  *
9  *  Author(s):  Ursula Braun <ubraun@linux.vnet.ibm.com>
10  */
11 #ifndef __SMC_H
12 #define __SMC_H
13 
14 #include <linux/socket.h>
15 #include <linux/types.h>
16 #include <linux/compiler.h> /* __aligned */
17 #include <net/genetlink.h>
18 #include <net/sock.h>
19 
20 #include "smc_ib.h"
21 
22 #define SMC_V1		1		/* SMC version V1 */
23 #define SMC_V2		2		/* SMC version V2 */
24 #define SMC_RELEASE	0
25 
26 #define SMCPROTO_SMC		0	/* SMC protocol, IPv4 */
27 #define SMCPROTO_SMC6		1	/* SMC protocol, IPv6 */
28 
29 #define SMC_MAX_ISM_DEVS	8	/* max # of proposed non-native ISM
30 					 * devices
31 					 */
32 
33 extern struct proto smc_proto;
34 extern struct proto smc_proto6;
35 
36 #ifdef ATOMIC64_INIT
37 #define KERNEL_HAS_ATOMIC64
38 #endif
39 
40 enum smc_state {		/* possible states of an SMC socket */
41 	SMC_ACTIVE	= 1,
42 	SMC_INIT	= 2,
43 	SMC_CLOSED	= 7,
44 	SMC_LISTEN	= 10,
45 	/* normal close */
46 	SMC_PEERCLOSEWAIT1	= 20,
47 	SMC_PEERCLOSEWAIT2	= 21,
48 	SMC_APPFINCLOSEWAIT	= 24,
49 	SMC_APPCLOSEWAIT1	= 22,
50 	SMC_APPCLOSEWAIT2	= 23,
51 	SMC_PEERFINCLOSEWAIT	= 25,
52 	/* abnormal close */
53 	SMC_PEERABORTWAIT	= 26,
54 	SMC_PROCESSABORT	= 27,
55 };
56 
57 struct smc_link_group;
58 
59 struct smc_wr_rx_hdr {	/* common prefix part of LLC and CDC to demultiplex */
60 	union {
61 		u8 type;
62 #if defined(__BIG_ENDIAN_BITFIELD)
63 		struct {
64 			u8 llc_version:4,
65 			   llc_type:4;
66 		};
67 #elif defined(__LITTLE_ENDIAN_BITFIELD)
68 		struct {
69 			u8 llc_type:4,
70 			   llc_version:4;
71 		};
72 #endif
73 	};
74 } __aligned(1);
75 
76 struct smc_cdc_conn_state_flags {
77 #if defined(__BIG_ENDIAN_BITFIELD)
78 	u8	peer_done_writing : 1;	/* Sending done indicator */
79 	u8	peer_conn_closed : 1;	/* Peer connection closed indicator */
80 	u8	peer_conn_abort : 1;	/* Abnormal close indicator */
81 	u8	reserved : 5;
82 #elif defined(__LITTLE_ENDIAN_BITFIELD)
83 	u8	reserved : 5;
84 	u8	peer_conn_abort : 1;
85 	u8	peer_conn_closed : 1;
86 	u8	peer_done_writing : 1;
87 #endif
88 };
89 
90 struct smc_cdc_producer_flags {
91 #if defined(__BIG_ENDIAN_BITFIELD)
92 	u8	write_blocked : 1;	/* Writing Blocked, no rx buf space */
93 	u8	urg_data_pending : 1;	/* Urgent Data Pending */
94 	u8	urg_data_present : 1;	/* Urgent Data Present */
95 	u8	cons_curs_upd_req : 1;	/* cursor update requested */
96 	u8	failover_validation : 1;/* message replay due to failover */
97 	u8	reserved : 3;
98 #elif defined(__LITTLE_ENDIAN_BITFIELD)
99 	u8	reserved : 3;
100 	u8	failover_validation : 1;
101 	u8	cons_curs_upd_req : 1;
102 	u8	urg_data_present : 1;
103 	u8	urg_data_pending : 1;
104 	u8	write_blocked : 1;
105 #endif
106 };
107 
108 /* in host byte order */
109 union smc_host_cursor {	/* SMC cursor - an offset in an RMBE */
110 	struct {
111 		u16	reserved;
112 		u16	wrap;		/* window wrap sequence number */
113 		u32	count;		/* cursor (= offset) part */
114 	};
115 #ifdef KERNEL_HAS_ATOMIC64
116 	atomic64_t		acurs;	/* for atomic processing */
117 #else
118 	u64			acurs;	/* for atomic processing */
119 #endif
120 } __aligned(8);
121 
122 /* in host byte order, except for flag bitfields in network byte order */
123 struct smc_host_cdc_msg {		/* Connection Data Control message */
124 	struct smc_wr_rx_hdr		common; /* .type = 0xFE */
125 	u8				len;	/* length = 44 */
126 	u16				seqno;	/* connection seq # */
127 	u32				token;	/* alert_token */
128 	union smc_host_cursor		prod;		/* producer cursor */
129 	union smc_host_cursor		cons;		/* consumer cursor,
130 							 * piggy backed "ack"
131 							 */
132 	struct smc_cdc_producer_flags	prod_flags;	/* conn. tx/rx status */
133 	struct smc_cdc_conn_state_flags	conn_state_flags; /* peer conn. status*/
134 	u8				reserved[18];
135 } __aligned(8);
136 
137 enum smc_urg_state {
138 	SMC_URG_VALID	= 1,			/* data present */
139 	SMC_URG_NOTYET	= 2,			/* data pending */
140 	SMC_URG_READ	= 3,			/* data was already read */
141 };
142 
143 struct smc_mark_woken {
144 	bool woken;
145 	void *key;
146 	wait_queue_entry_t wait_entry;
147 };
148 
149 struct smc_connection {
150 	struct rb_node		alert_node;
151 	struct smc_link_group	*lgr;		/* link group of connection */
152 	struct smc_link		*lnk;		/* assigned SMC-R link */
153 	u32			alert_token_local; /* unique conn. id */
154 	u8			peer_rmbe_idx;	/* from tcp handshake */
155 	int			peer_rmbe_size;	/* size of peer rx buffer */
156 	atomic_t		peer_rmbe_space;/* remaining free bytes in peer
157 						 * rmbe
158 						 */
159 	int			rtoken_idx;	/* idx to peer RMB rkey/addr */
160 
161 	struct smc_buf_desc	*sndbuf_desc;	/* send buffer descriptor */
162 	struct smc_buf_desc	*rmb_desc;	/* RMBE descriptor */
163 	int			rmbe_size_short;/* compressed notation */
164 	int			rmbe_update_limit;
165 						/* lower limit for consumer
166 						 * cursor update
167 						 */
168 
169 	struct smc_host_cdc_msg	local_tx_ctrl;	/* host byte order staging
170 						 * buffer for CDC msg send
171 						 * .prod cf. TCP snd_nxt
172 						 * .cons cf. TCP sends ack
173 						 */
174 	union smc_host_cursor	local_tx_ctrl_fin;
175 						/* prod crsr - confirmed by peer
176 						 */
177 	union smc_host_cursor	tx_curs_prep;	/* tx - prepared data
178 						 * snd_max..wmem_alloc
179 						 */
180 	union smc_host_cursor	tx_curs_sent;	/* tx - sent data
181 						 * snd_nxt ?
182 						 */
183 	union smc_host_cursor	tx_curs_fin;	/* tx - confirmed by peer
184 						 * snd-wnd-begin ?
185 						 */
186 	atomic_t		sndbuf_space;	/* remaining space in sndbuf */
187 	u16			tx_cdc_seq;	/* sequence # for CDC send */
188 	u16			tx_cdc_seq_fin;	/* sequence # - tx completed */
189 	spinlock_t		send_lock;	/* protect wr_sends */
190 	atomic_t		cdc_pend_tx_wr; /* number of pending tx CDC wqe
191 						 * - inc when post wqe,
192 						 * - dec on polled tx cqe
193 						 */
194 	wait_queue_head_t	cdc_pend_tx_wq; /* wakeup on no cdc_pend_tx_wr*/
195 	struct delayed_work	tx_work;	/* retry of smc_cdc_msg_send */
196 	u32			tx_off;		/* base offset in peer rmb */
197 
198 	struct smc_host_cdc_msg	local_rx_ctrl;	/* filled during event_handl.
199 						 * .prod cf. TCP rcv_nxt
200 						 * .cons cf. TCP snd_una
201 						 */
202 	union smc_host_cursor	rx_curs_confirmed; /* confirmed to peer
203 						    * source of snd_una ?
204 						    */
205 	union smc_host_cursor	urg_curs;	/* points at urgent byte */
206 	enum smc_urg_state	urg_state;
207 	bool			urg_tx_pend;	/* urgent data staged */
208 	bool			urg_rx_skip_pend;
209 						/* indicate urgent oob data
210 						 * read, but previous regular
211 						 * data still pending
212 						 */
213 	char			urg_rx_byte;	/* urgent byte */
214 	atomic_t		bytes_to_rcv;	/* arrived data,
215 						 * not yet received
216 						 */
217 	atomic_t		splice_pending;	/* number of spliced bytes
218 						 * pending processing
219 						 */
220 #ifndef KERNEL_HAS_ATOMIC64
221 	spinlock_t		acurs_lock;	/* protect cursors */
222 #endif
223 	struct work_struct	close_work;	/* peer sent some closing */
224 	struct work_struct	abort_work;	/* abort the connection */
225 	struct tasklet_struct	rx_tsklet;	/* Receiver tasklet for SMC-D */
226 	u8			rx_off;		/* receive offset:
227 						 * 0 for SMC-R, 32 for SMC-D
228 						 */
229 	u64			peer_token;	/* SMC-D token of peer */
230 	u8			killed : 1;	/* abnormal termination */
231 	u8			freed : 1;	/* normal termiation */
232 	u8			out_of_sync : 1; /* out of sync with peer */
233 };
234 
235 struct smc_sock {				/* smc sock container */
236 	struct sock		sk;
237 	struct socket		*clcsock;	/* internal tcp socket */
238 	void			(*clcsk_state_change)(struct sock *sk);
239 						/* original stat_change fct. */
240 	void			(*clcsk_data_ready)(struct sock *sk);
241 						/* original data_ready fct. */
242 	void			(*clcsk_write_space)(struct sock *sk);
243 						/* original write_space fct. */
244 	void			(*clcsk_error_report)(struct sock *sk);
245 						/* original error_report fct. */
246 	struct smc_connection	conn;		/* smc connection */
247 	struct smc_sock		*listen_smc;	/* listen parent */
248 	struct work_struct	connect_work;	/* handle non-blocking connect*/
249 	struct work_struct	tcp_listen_work;/* handle tcp socket accepts */
250 	struct work_struct	smc_listen_work;/* prepare new accept socket */
251 	struct list_head	accept_q;	/* sockets to be accepted */
252 	spinlock_t		accept_q_lock;	/* protects accept_q */
253 	bool			limit_smc_hs;	/* put constraint on handshake */
254 	bool			use_fallback;	/* fallback to tcp */
255 	int			fallback_rsn;	/* reason for fallback */
256 	u32			peer_diagnosis; /* decline reason from peer */
257 	atomic_t                queued_smc_hs;  /* queued smc handshakes */
258 	struct inet_connection_sock_af_ops		af_ops;
259 	const struct inet_connection_sock_af_ops	*ori_af_ops;
260 						/* original af ops */
261 	int			sockopt_defer_accept;
262 						/* sockopt TCP_DEFER_ACCEPT
263 						 * value
264 						 */
265 	u8			wait_close_tx_prepared : 1;
266 						/* shutdown wr or close
267 						 * started, waiting for unsent
268 						 * data to be sent
269 						 */
270 	u8			connect_nonblock : 1;
271 						/* non-blocking connect in
272 						 * flight
273 						 */
274 	struct mutex            clcsock_release_lock;
275 						/* protects clcsock of a listen
276 						 * socket
277 						 * */
278 };
279 
280 static inline struct smc_sock *smc_sk(const struct sock *sk)
281 {
282 	return (struct smc_sock *)sk;
283 }
284 
285 static inline struct smc_sock *smc_clcsock_user_data(const struct sock *clcsk)
286 {
287 	return (struct smc_sock *)
288 	       ((uintptr_t)clcsk->sk_user_data & ~SK_USER_DATA_NOCOPY);
289 }
290 
291 extern struct workqueue_struct	*smc_hs_wq;	/* wq for handshake work */
292 extern struct workqueue_struct	*smc_close_wq;	/* wq for close work */
293 
294 #define SMC_SYSTEMID_LEN		8
295 
296 extern u8	local_systemid[SMC_SYSTEMID_LEN]; /* unique system identifier */
297 
298 #define ntohll(x) be64_to_cpu(x)
299 #define htonll(x) cpu_to_be64(x)
300 
301 /* convert an u32 value into network byte order, store it into a 3 byte field */
302 static inline void hton24(u8 *net, u32 host)
303 {
304 	__be32 t;
305 
306 	t = cpu_to_be32(host);
307 	memcpy(net, ((u8 *)&t) + 1, 3);
308 }
309 
310 /* convert a received 3 byte field into host byte order*/
311 static inline u32 ntoh24(u8 *net)
312 {
313 	__be32 t = 0;
314 
315 	memcpy(((u8 *)&t) + 1, net, 3);
316 	return be32_to_cpu(t);
317 }
318 
319 #ifdef CONFIG_XFRM
320 static inline bool using_ipsec(struct smc_sock *smc)
321 {
322 	return (smc->clcsock->sk->sk_policy[0] ||
323 		smc->clcsock->sk->sk_policy[1]) ? true : false;
324 }
325 #else
326 static inline bool using_ipsec(struct smc_sock *smc)
327 {
328 	return false;
329 }
330 #endif
331 
332 struct smc_gidlist;
333 
334 struct sock *smc_accept_dequeue(struct sock *parent, struct socket *new_sock);
335 void smc_close_non_accepted(struct sock *sk);
336 void smc_fill_gid_list(struct smc_link_group *lgr,
337 		       struct smc_gidlist *gidlist,
338 		       struct smc_ib_device *known_dev, u8 *known_gid);
339 
340 /* smc handshake limitation interface for netlink  */
341 int smc_nl_dump_hs_limitation(struct sk_buff *skb, struct netlink_callback *cb);
342 int smc_nl_enable_hs_limitation(struct sk_buff *skb, struct genl_info *info);
343 int smc_nl_disable_hs_limitation(struct sk_buff *skb, struct genl_info *info);
344 
345 #endif	/* __SMC_H */
346