1 /* SPDX-License-Identifier: GPL-2.0 */
3 * Shared Memory Communications over RDMA (SMC-R) and RoCE
5 * Definitions for the SMC module (socket related)
7 * Copyright IBM Corp. 2016
9 * Author(s): Ursula Braun <ubraun@linux.vnet.ibm.com>
14 #include <linux/socket.h>
15 #include <linux/types.h>
16 #include <linux/compiler.h> /* __aligned */
17 #include <net/genetlink.h>
22 #define SMC_V1 1 /* SMC version V1 */
23 #define SMC_V2 2 /* SMC version V2 */
26 #define SMCPROTO_SMC 0 /* SMC protocol, IPv4 */
27 #define SMCPROTO_SMC6 1 /* SMC protocol, IPv6 */
29 #define SMC_MAX_ISM_DEVS 8 /* max # of proposed non-native ISM
32 #define SMC_AUTOCORKING_DEFAULT_SIZE 0x10000 /* 64K by default */
34 extern struct proto smc_proto;
35 extern struct proto smc_proto6;
38 #define KERNEL_HAS_ATOMIC64
41 enum smc_state { /* possible states of an SMC socket */
47 SMC_PEERCLOSEWAIT1 = 20,
48 SMC_PEERCLOSEWAIT2 = 21,
49 SMC_APPFINCLOSEWAIT = 24,
50 SMC_APPCLOSEWAIT1 = 22,
51 SMC_APPCLOSEWAIT2 = 23,
52 SMC_PEERFINCLOSEWAIT = 25,
54 SMC_PEERABORTWAIT = 26,
55 SMC_PROCESSABORT = 27,
58 struct smc_link_group;
60 struct smc_wr_rx_hdr { /* common prefix part of LLC and CDC to demultiplex */
63 #if defined(__BIG_ENDIAN_BITFIELD)
68 #elif defined(__LITTLE_ENDIAN_BITFIELD)
77 struct smc_cdc_conn_state_flags {
78 #if defined(__BIG_ENDIAN_BITFIELD)
79 u8 peer_done_writing : 1; /* Sending done indicator */
80 u8 peer_conn_closed : 1; /* Peer connection closed indicator */
81 u8 peer_conn_abort : 1; /* Abnormal close indicator */
83 #elif defined(__LITTLE_ENDIAN_BITFIELD)
85 u8 peer_conn_abort : 1;
86 u8 peer_conn_closed : 1;
87 u8 peer_done_writing : 1;
91 struct smc_cdc_producer_flags {
92 #if defined(__BIG_ENDIAN_BITFIELD)
93 u8 write_blocked : 1; /* Writing Blocked, no rx buf space */
94 u8 urg_data_pending : 1; /* Urgent Data Pending */
95 u8 urg_data_present : 1; /* Urgent Data Present */
96 u8 cons_curs_upd_req : 1; /* cursor update requested */
97 u8 failover_validation : 1;/* message replay due to failover */
99 #elif defined(__LITTLE_ENDIAN_BITFIELD)
101 u8 failover_validation : 1;
102 u8 cons_curs_upd_req : 1;
103 u8 urg_data_present : 1;
104 u8 urg_data_pending : 1;
105 u8 write_blocked : 1;
109 /* in host byte order */
110 union smc_host_cursor { /* SMC cursor - an offset in an RMBE */
113 u16 wrap; /* window wrap sequence number */
114 u32 count; /* cursor (= offset) part */
116 #ifdef KERNEL_HAS_ATOMIC64
117 atomic64_t acurs; /* for atomic processing */
119 u64 acurs; /* for atomic processing */
123 /* in host byte order, except for flag bitfields in network byte order */
124 struct smc_host_cdc_msg { /* Connection Data Control message */
125 struct smc_wr_rx_hdr common; /* .type = 0xFE */
126 u8 len; /* length = 44 */
127 u16 seqno; /* connection seq # */
128 u32 token; /* alert_token */
129 union smc_host_cursor prod; /* producer cursor */
130 union smc_host_cursor cons; /* consumer cursor,
133 struct smc_cdc_producer_flags prod_flags; /* conn. tx/rx status */
134 struct smc_cdc_conn_state_flags conn_state_flags; /* peer conn. status*/
139 SMC_URG_VALID = 1, /* data present */
140 SMC_URG_NOTYET = 2, /* data pending */
141 SMC_URG_READ = 3, /* data was already read */
144 struct smc_mark_woken {
147 wait_queue_entry_t wait_entry;
150 struct smc_connection {
151 struct rb_node alert_node;
152 struct smc_link_group *lgr; /* link group of connection */
153 struct smc_link *lnk; /* assigned SMC-R link */
154 u32 alert_token_local; /* unique conn. id */
155 u8 peer_rmbe_idx; /* from tcp handshake */
156 int peer_rmbe_size; /* size of peer rx buffer */
157 atomic_t peer_rmbe_space;/* remaining free bytes in peer
160 int rtoken_idx; /* idx to peer RMB rkey/addr */
162 struct smc_buf_desc *sndbuf_desc; /* send buffer descriptor */
163 struct smc_buf_desc *rmb_desc; /* RMBE descriptor */
164 int rmbe_size_short;/* compressed notation */
165 int rmbe_update_limit;
166 /* lower limit for consumer
170 struct smc_host_cdc_msg local_tx_ctrl; /* host byte order staging
171 * buffer for CDC msg send
172 * .prod cf. TCP snd_nxt
173 * .cons cf. TCP sends ack
175 union smc_host_cursor local_tx_ctrl_fin;
176 /* prod crsr - confirmed by peer
178 union smc_host_cursor tx_curs_prep; /* tx - prepared data
179 * snd_max..wmem_alloc
181 union smc_host_cursor tx_curs_sent; /* tx - sent data
184 union smc_host_cursor tx_curs_fin; /* tx - confirmed by peer
187 atomic_t sndbuf_space; /* remaining space in sndbuf */
188 u16 tx_cdc_seq; /* sequence # for CDC send */
189 u16 tx_cdc_seq_fin; /* sequence # - tx completed */
190 spinlock_t send_lock; /* protect wr_sends */
191 atomic_t cdc_pend_tx_wr; /* number of pending tx CDC wqe
192 * - inc when post wqe,
193 * - dec on polled tx cqe
195 wait_queue_head_t cdc_pend_tx_wq; /* wakeup on no cdc_pend_tx_wr*/
196 atomic_t tx_pushing; /* nr_threads trying tx push */
197 struct delayed_work tx_work; /* retry of smc_cdc_msg_send */
198 u32 tx_off; /* base offset in peer rmb */
200 struct smc_host_cdc_msg local_rx_ctrl; /* filled during event_handl.
201 * .prod cf. TCP rcv_nxt
202 * .cons cf. TCP snd_una
204 union smc_host_cursor rx_curs_confirmed; /* confirmed to peer
205 * source of snd_una ?
207 union smc_host_cursor urg_curs; /* points at urgent byte */
208 enum smc_urg_state urg_state;
209 bool urg_tx_pend; /* urgent data staged */
210 bool urg_rx_skip_pend;
211 /* indicate urgent oob data
212 * read, but previous regular
215 char urg_rx_byte; /* urgent byte */
216 bool tx_in_release_sock;
217 /* flush pending tx data in
220 atomic_t bytes_to_rcv; /* arrived data,
223 atomic_t splice_pending; /* number of spliced bytes
226 #ifndef KERNEL_HAS_ATOMIC64
227 spinlock_t acurs_lock; /* protect cursors */
229 struct work_struct close_work; /* peer sent some closing */
230 struct work_struct abort_work; /* abort the connection */
231 struct tasklet_struct rx_tsklet; /* Receiver tasklet for SMC-D */
232 u8 rx_off; /* receive offset:
233 * 0 for SMC-R, 32 for SMC-D
235 u64 peer_token; /* SMC-D token of peer */
236 u8 killed : 1; /* abnormal termination */
237 u8 freed : 1; /* normal termiation */
238 u8 out_of_sync : 1; /* out of sync with peer */
241 struct smc_sock { /* smc sock container */
243 struct socket *clcsock; /* internal tcp socket */
244 void (*clcsk_state_change)(struct sock *sk);
245 /* original stat_change fct. */
246 void (*clcsk_data_ready)(struct sock *sk);
247 /* original data_ready fct. */
248 void (*clcsk_write_space)(struct sock *sk);
249 /* original write_space fct. */
250 void (*clcsk_error_report)(struct sock *sk);
251 /* original error_report fct. */
252 struct smc_connection conn; /* smc connection */
253 struct smc_sock *listen_smc; /* listen parent */
254 struct work_struct connect_work; /* handle non-blocking connect*/
255 struct work_struct tcp_listen_work;/* handle tcp socket accepts */
256 struct work_struct smc_listen_work;/* prepare new accept socket */
257 struct list_head accept_q; /* sockets to be accepted */
258 spinlock_t accept_q_lock; /* protects accept_q */
259 bool limit_smc_hs; /* put constraint on handshake */
260 bool use_fallback; /* fallback to tcp */
261 int fallback_rsn; /* reason for fallback */
262 u32 peer_diagnosis; /* decline reason from peer */
263 atomic_t queued_smc_hs; /* queued smc handshakes */
264 struct inet_connection_sock_af_ops af_ops;
265 const struct inet_connection_sock_af_ops *ori_af_ops;
266 /* original af ops */
267 int sockopt_defer_accept;
268 /* sockopt TCP_DEFER_ACCEPT
271 u8 wait_close_tx_prepared : 1;
272 /* shutdown wr or close
273 * started, waiting for unsent
276 u8 connect_nonblock : 1;
277 /* non-blocking connect in
280 struct mutex clcsock_release_lock;
281 /* protects clcsock of a listen
286 static inline struct smc_sock *smc_sk(const struct sock *sk)
288 return (struct smc_sock *)sk;
291 static inline void smc_init_saved_callbacks(struct smc_sock *smc)
293 smc->clcsk_state_change = NULL;
294 smc->clcsk_data_ready = NULL;
295 smc->clcsk_write_space = NULL;
296 smc->clcsk_error_report = NULL;
299 static inline struct smc_sock *smc_clcsock_user_data(const struct sock *clcsk)
301 return (struct smc_sock *)
302 ((uintptr_t)clcsk->sk_user_data & ~SK_USER_DATA_NOCOPY);
305 /* save target_cb in saved_cb, and replace target_cb with new_cb */
306 static inline void smc_clcsock_replace_cb(void (**target_cb)(struct sock *),
307 void (*new_cb)(struct sock *),
308 void (**saved_cb)(struct sock *))
312 *saved_cb = *target_cb;
316 /* restore target_cb to saved_cb, and reset saved_cb to NULL */
317 static inline void smc_clcsock_restore_cb(void (**target_cb)(struct sock *),
318 void (**saved_cb)(struct sock *))
322 *target_cb = *saved_cb;
326 extern struct workqueue_struct *smc_hs_wq; /* wq for handshake work */
327 extern struct workqueue_struct *smc_close_wq; /* wq for close work */
329 #define SMC_SYSTEMID_LEN 8
331 extern u8 local_systemid[SMC_SYSTEMID_LEN]; /* unique system identifier */
333 #define ntohll(x) be64_to_cpu(x)
334 #define htonll(x) cpu_to_be64(x)
336 /* convert an u32 value into network byte order, store it into a 3 byte field */
337 static inline void hton24(u8 *net, u32 host)
341 t = cpu_to_be32(host);
342 memcpy(net, ((u8 *)&t) + 1, 3);
345 /* convert a received 3 byte field into host byte order*/
346 static inline u32 ntoh24(u8 *net)
350 memcpy(((u8 *)&t) + 1, net, 3);
351 return be32_to_cpu(t);
355 static inline bool using_ipsec(struct smc_sock *smc)
357 return (smc->clcsock->sk->sk_policy[0] ||
358 smc->clcsock->sk->sk_policy[1]) ? true : false;
361 static inline bool using_ipsec(struct smc_sock *smc)
369 struct sock *smc_accept_dequeue(struct sock *parent, struct socket *new_sock);
370 void smc_close_non_accepted(struct sock *sk);
371 void smc_fill_gid_list(struct smc_link_group *lgr,
372 struct smc_gidlist *gidlist,
373 struct smc_ib_device *known_dev, u8 *known_gid);
375 /* smc handshake limitation interface for netlink */
376 int smc_nl_dump_hs_limitation(struct sk_buff *skb, struct netlink_callback *cb);
377 int smc_nl_enable_hs_limitation(struct sk_buff *skb, struct genl_info *info);
378 int smc_nl_disable_hs_limitation(struct sk_buff *skb, struct genl_info *info);