xref: /linux/net/smc/smc.h (revision e58e871becec2d3b04ed91c0c16fe8deac9c9dfa)
1 /*
2  *  Shared Memory Communications over RDMA (SMC-R) and RoCE
3  *
4  *  Definitions for the SMC module (socket related)
5  *
6  *  Copyright IBM Corp. 2016
7  *
8  *  Author(s):  Ursula Braun <ubraun@linux.vnet.ibm.com>
9  */
10 #ifndef __SMC_H
11 #define __SMC_H
12 
13 #include <linux/socket.h>
14 #include <linux/types.h>
15 #include <linux/compiler.h> /* __aligned */
16 #include <net/sock.h>
17 
18 #include "smc_ib.h"
19 
20 #define SMCPROTO_SMC		0	/* SMC protocol */
21 
22 #define SMC_MAX_PORTS		2	/* Max # of ports */
23 
24 extern struct proto smc_proto;
25 
26 #ifdef ATOMIC64_INIT
27 #define KERNEL_HAS_ATOMIC64
28 #endif
29 
30 enum smc_state {		/* possible states of an SMC socket */
31 	SMC_ACTIVE	= 1,
32 	SMC_INIT	= 2,
33 	SMC_CLOSED	= 7,
34 	SMC_LISTEN	= 10,
35 	/* normal close */
36 	SMC_PEERCLOSEWAIT1	= 20,
37 	SMC_PEERCLOSEWAIT2	= 21,
38 	SMC_APPFINCLOSEWAIT	= 24,
39 	SMC_APPCLOSEWAIT1	= 22,
40 	SMC_APPCLOSEWAIT2	= 23,
41 	SMC_PEERFINCLOSEWAIT	= 25,
42 	/* abnormal close */
43 	SMC_PEERABORTWAIT	= 26,
44 	SMC_PROCESSABORT	= 27,
45 };
46 
47 struct smc_link_group;
48 
49 struct smc_wr_rx_hdr {	/* common prefix part of LLC and CDC to demultiplex */
50 	u8			type;
51 } __aligned(1);
52 
53 struct smc_cdc_conn_state_flags {
54 #if defined(__BIG_ENDIAN_BITFIELD)
55 	u8	peer_done_writing : 1;	/* Sending done indicator */
56 	u8	peer_conn_closed : 1;	/* Peer connection closed indicator */
57 	u8	peer_conn_abort : 1;	/* Abnormal close indicator */
58 	u8	reserved : 5;
59 #elif defined(__LITTLE_ENDIAN_BITFIELD)
60 	u8	reserved : 5;
61 	u8	peer_conn_abort : 1;
62 	u8	peer_conn_closed : 1;
63 	u8	peer_done_writing : 1;
64 #endif
65 };
66 
67 struct smc_cdc_producer_flags {
68 #if defined(__BIG_ENDIAN_BITFIELD)
69 	u8	write_blocked : 1;	/* Writing Blocked, no rx buf space */
70 	u8	urg_data_pending : 1;	/* Urgent Data Pending */
71 	u8	urg_data_present : 1;	/* Urgent Data Present */
72 	u8	cons_curs_upd_req : 1;	/* cursor update requested */
73 	u8	failover_validation : 1;/* message replay due to failover */
74 	u8	reserved : 3;
75 #elif defined(__LITTLE_ENDIAN_BITFIELD)
76 	u8	reserved : 3;
77 	u8	failover_validation : 1;
78 	u8	cons_curs_upd_req : 1;
79 	u8	urg_data_present : 1;
80 	u8	urg_data_pending : 1;
81 	u8	write_blocked : 1;
82 #endif
83 };
84 
85 /* in host byte order */
86 union smc_host_cursor {	/* SMC cursor - an offset in an RMBE */
87 	struct {
88 		u16	reserved;
89 		u16	wrap;		/* window wrap sequence number */
90 		u32	count;		/* cursor (= offset) part */
91 	};
92 #ifdef KERNEL_HAS_ATOMIC64
93 	atomic64_t		acurs;	/* for atomic processing */
94 #else
95 	u64			acurs;	/* for atomic processing */
96 #endif
97 } __aligned(8);
98 
99 /* in host byte order, except for flag bitfields in network byte order */
100 struct smc_host_cdc_msg {		/* Connection Data Control message */
101 	struct smc_wr_rx_hdr		common; /* .type = 0xFE */
102 	u8				len;	/* length = 44 */
103 	u16				seqno;	/* connection seq # */
104 	u32				token;	/* alert_token */
105 	union smc_host_cursor		prod;		/* producer cursor */
106 	union smc_host_cursor		cons;		/* consumer cursor,
107 							 * piggy backed "ack"
108 							 */
109 	struct smc_cdc_producer_flags	prod_flags;	/* conn. tx/rx status */
110 	struct smc_cdc_conn_state_flags	conn_state_flags; /* peer conn. status*/
111 	u8				reserved[18];
112 } __aligned(8);
113 
114 struct smc_connection {
115 	struct rb_node		alert_node;
116 	struct smc_link_group	*lgr;		/* link group of connection */
117 	u32			alert_token_local; /* unique conn. id */
118 	u8			peer_conn_idx;	/* from tcp handshake */
119 	int			peer_rmbe_size;	/* size of peer rx buffer */
120 	atomic_t		peer_rmbe_space;/* remaining free bytes in peer
121 						 * rmbe
122 						 */
123 	int			rtoken_idx;	/* idx to peer RMB rkey/addr */
124 
125 	struct smc_buf_desc	*sndbuf_desc;	/* send buffer descriptor */
126 	int			sndbuf_size;	/* sndbuf size <== sock wmem */
127 	struct smc_buf_desc	*rmb_desc;	/* RMBE descriptor */
128 	int			rmbe_size;	/* RMBE size <== sock rmem */
129 	int			rmbe_size_short;/* compressed notation */
130 	int			rmbe_update_limit;
131 						/* lower limit for consumer
132 						 * cursor update
133 						 */
134 
135 	struct smc_host_cdc_msg	local_tx_ctrl;	/* host byte order staging
136 						 * buffer for CDC msg send
137 						 * .prod cf. TCP snd_nxt
138 						 * .cons cf. TCP sends ack
139 						 */
140 	union smc_host_cursor	tx_curs_prep;	/* tx - prepared data
141 						 * snd_max..wmem_alloc
142 						 */
143 	union smc_host_cursor	tx_curs_sent;	/* tx - sent data
144 						 * snd_nxt ?
145 						 */
146 	union smc_host_cursor	tx_curs_fin;	/* tx - confirmed by peer
147 						 * snd-wnd-begin ?
148 						 */
149 	atomic_t		sndbuf_space;	/* remaining space in sndbuf */
150 	u16			tx_cdc_seq;	/* sequence # for CDC send */
151 	spinlock_t		send_lock;	/* protect wr_sends */
152 	struct work_struct	tx_work;	/* retry of smc_cdc_msg_send */
153 
154 	struct smc_host_cdc_msg	local_rx_ctrl;	/* filled during event_handl.
155 						 * .prod cf. TCP rcv_nxt
156 						 * .cons cf. TCP snd_una
157 						 */
158 	union smc_host_cursor	rx_curs_confirmed; /* confirmed to peer
159 						    * source of snd_una ?
160 						    */
161 	atomic_t		bytes_to_rcv;	/* arrived data,
162 						 * not yet received
163 						 */
164 #ifndef KERNEL_HAS_ATOMIC64
165 	spinlock_t		acurs_lock;	/* protect cursors */
166 #endif
167 	struct work_struct	close_work;	/* peer sent some closing */
168 };
169 
170 struct smc_sock {				/* smc sock container */
171 	struct sock		sk;
172 	struct socket		*clcsock;	/* internal tcp socket */
173 	struct smc_connection	conn;		/* smc connection */
174 	struct sockaddr		*addr;		/* inet connect address */
175 	struct smc_sock		*listen_smc;	/* listen parent */
176 	struct work_struct	tcp_listen_work;/* handle tcp socket accepts */
177 	struct work_struct	smc_listen_work;/* prepare new accept socket */
178 	struct list_head	accept_q;	/* sockets to be accepted */
179 	spinlock_t		accept_q_lock;	/* protects accept_q */
180 	struct delayed_work	sock_put_work;	/* final socket freeing */
181 	bool			use_fallback;	/* fallback to tcp */
182 	u8			wait_close_tx_prepared : 1;
183 						/* shutdown wr or close
184 						 * started, waiting for unsent
185 						 * data to be sent
186 						 */
187 };
188 
189 static inline struct smc_sock *smc_sk(const struct sock *sk)
190 {
191 	return (struct smc_sock *)sk;
192 }
193 
194 #define SMC_SYSTEMID_LEN		8
195 
196 extern u8	local_systemid[SMC_SYSTEMID_LEN]; /* unique system identifier */
197 
198 /* convert an u32 value into network byte order, store it into a 3 byte field */
199 static inline void hton24(u8 *net, u32 host)
200 {
201 	__be32 t;
202 
203 	t = cpu_to_be32(host);
204 	memcpy(net, ((u8 *)&t) + 1, 3);
205 }
206 
207 /* convert a received 3 byte field into host byte order*/
208 static inline u32 ntoh24(u8 *net)
209 {
210 	__be32 t = 0;
211 
212 	memcpy(((u8 *)&t) + 1, net, 3);
213 	return be32_to_cpu(t);
214 }
215 
216 #define SMC_BUF_MIN_SIZE 16384		/* minimum size of an RMB */
217 
218 #define SMC_RMBE_SIZES	16	/* number of distinct sizes for an RMBE */
219 /* theoretically, the RFC states that largest size would be 512K,
220  * i.e. compressed 5 and thus 6 sizes (0..5), despite
221  * struct smc_clc_msg_accept_confirm.rmbe_size being a 4 bit value (0..15)
222  */
223 
224 /* convert the RMB size into the compressed notation - minimum 16K.
225  * In contrast to plain ilog2, this rounds towards the next power of 2,
226  * so the socket application gets at least its desired sndbuf / rcvbuf size.
227  */
228 static inline u8 smc_compress_bufsize(int size)
229 {
230 	u8 compressed;
231 
232 	if (size <= SMC_BUF_MIN_SIZE)
233 		return 0;
234 
235 	size = (size - 1) >> 14;
236 	compressed = ilog2(size) + 1;
237 	if (compressed >= SMC_RMBE_SIZES)
238 		compressed = SMC_RMBE_SIZES - 1;
239 	return compressed;
240 }
241 
242 /* convert the RMB size from compressed notation into integer */
243 static inline int smc_uncompress_bufsize(u8 compressed)
244 {
245 	u32 size;
246 
247 	size = 0x00000001 << (((int)compressed) + 14);
248 	return (int)size;
249 }
250 
251 #ifdef CONFIG_XFRM
252 static inline bool using_ipsec(struct smc_sock *smc)
253 {
254 	return (smc->clcsock->sk->sk_policy[0] ||
255 		smc->clcsock->sk->sk_policy[1]) ? 1 : 0;
256 }
257 #else
258 static inline bool using_ipsec(struct smc_sock *smc)
259 {
260 	return 0;
261 }
262 #endif
263 
264 struct smc_clc_msg_local;
265 
266 int smc_netinfo_by_tcpsk(struct socket *clcsock, __be32 *subnet,
267 			 u8 *prefix_len);
268 void smc_conn_free(struct smc_connection *conn);
269 int smc_conn_create(struct smc_sock *smc, __be32 peer_in_addr,
270 		    struct smc_ib_device *smcibdev, u8 ibport,
271 		    struct smc_clc_msg_local *lcl, int srv_first_contact);
272 struct sock *smc_accept_dequeue(struct sock *parent, struct socket *new_sock);
273 void smc_close_non_accepted(struct sock *sk);
274 
275 #endif	/* __SMC_H */
276