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