smc.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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. #include <linux/socket.h>
  14. #include <linux/types.h>
  15. #include <linux/compiler.h> /* __aligned */
  16. #include <net/sock.h>
  17. #include "smc_ib.h"
  18. #define SMC_V1 1 /* SMC version V1 */
  19. #define SMC_V2 2 /* SMC version V2 */
  20. #define SMC_RELEASE 0
  21. #define SMCPROTO_SMC 0 /* SMC protocol, IPv4 */
  22. #define SMCPROTO_SMC6 1 /* SMC protocol, IPv6 */
  23. #define SMC_MAX_ISM_DEVS 8 /* max # of proposed non-native ISM
  24. * devices
  25. */
  26. #define SMC_MAX_HOSTNAME_LEN 32
  27. #define SMC_MAX_EID_LEN 32
  28. extern struct proto smc_proto;
  29. extern struct proto smc_proto6;
  30. #ifdef ATOMIC64_INIT
  31. #define KERNEL_HAS_ATOMIC64
  32. #endif
  33. enum smc_state { /* possible states of an SMC socket */
  34. SMC_ACTIVE = 1,
  35. SMC_INIT = 2,
  36. SMC_CLOSED = 7,
  37. SMC_LISTEN = 10,
  38. /* normal close */
  39. SMC_PEERCLOSEWAIT1 = 20,
  40. SMC_PEERCLOSEWAIT2 = 21,
  41. SMC_APPFINCLOSEWAIT = 24,
  42. SMC_APPCLOSEWAIT1 = 22,
  43. SMC_APPCLOSEWAIT2 = 23,
  44. SMC_PEERFINCLOSEWAIT = 25,
  45. /* abnormal close */
  46. SMC_PEERABORTWAIT = 26,
  47. SMC_PROCESSABORT = 27,
  48. };
  49. struct smc_link_group;
  50. struct smc_wr_rx_hdr { /* common prefix part of LLC and CDC to demultiplex */
  51. u8 type;
  52. } __aligned(1);
  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. struct smc_cdc_producer_flags {
  67. #if defined(__BIG_ENDIAN_BITFIELD)
  68. u8 write_blocked : 1; /* Writing Blocked, no rx buf space */
  69. u8 urg_data_pending : 1; /* Urgent Data Pending */
  70. u8 urg_data_present : 1; /* Urgent Data Present */
  71. u8 cons_curs_upd_req : 1; /* cursor update requested */
  72. u8 failover_validation : 1;/* message replay due to failover */
  73. u8 reserved : 3;
  74. #elif defined(__LITTLE_ENDIAN_BITFIELD)
  75. u8 reserved : 3;
  76. u8 failover_validation : 1;
  77. u8 cons_curs_upd_req : 1;
  78. u8 urg_data_present : 1;
  79. u8 urg_data_pending : 1;
  80. u8 write_blocked : 1;
  81. #endif
  82. };
  83. /* in host byte order */
  84. union smc_host_cursor { /* SMC cursor - an offset in an RMBE */
  85. struct {
  86. u16 reserved;
  87. u16 wrap; /* window wrap sequence number */
  88. u32 count; /* cursor (= offset) part */
  89. };
  90. #ifdef KERNEL_HAS_ATOMIC64
  91. atomic64_t acurs; /* for atomic processing */
  92. #else
  93. u64 acurs; /* for atomic processing */
  94. #endif
  95. } __aligned(8);
  96. /* in host byte order, except for flag bitfields in network byte order */
  97. struct smc_host_cdc_msg { /* Connection Data Control message */
  98. struct smc_wr_rx_hdr common; /* .type = 0xFE */
  99. u8 len; /* length = 44 */
  100. u16 seqno; /* connection seq # */
  101. u32 token; /* alert_token */
  102. union smc_host_cursor prod; /* producer cursor */
  103. union smc_host_cursor cons; /* consumer cursor,
  104. * piggy backed "ack"
  105. */
  106. struct smc_cdc_producer_flags prod_flags; /* conn. tx/rx status */
  107. struct smc_cdc_conn_state_flags conn_state_flags; /* peer conn. status*/
  108. u8 reserved[18];
  109. } __aligned(8);
  110. enum smc_urg_state {
  111. SMC_URG_VALID = 1, /* data present */
  112. SMC_URG_NOTYET = 2, /* data pending */
  113. SMC_URG_READ = 3, /* data was already read */
  114. };
  115. struct smc_connection {
  116. struct rb_node alert_node;
  117. struct smc_link_group *lgr; /* link group of connection */
  118. struct smc_link *lnk; /* assigned SMC-R link */
  119. u32 alert_token_local; /* unique conn. id */
  120. u8 peer_rmbe_idx; /* from tcp handshake */
  121. int peer_rmbe_size; /* size of peer rx buffer */
  122. atomic_t peer_rmbe_space;/* remaining free bytes in peer
  123. * rmbe
  124. */
  125. int rtoken_idx; /* idx to peer RMB rkey/addr */
  126. struct smc_buf_desc *sndbuf_desc; /* send buffer descriptor */
  127. struct smc_buf_desc *rmb_desc; /* RMBE descriptor */
  128. int rmbe_size_short;/* compressed notation */
  129. int rmbe_update_limit;
  130. /* lower limit for consumer
  131. * cursor update
  132. */
  133. struct smc_host_cdc_msg local_tx_ctrl; /* host byte order staging
  134. * buffer for CDC msg send
  135. * .prod cf. TCP snd_nxt
  136. * .cons cf. TCP sends ack
  137. */
  138. union smc_host_cursor local_tx_ctrl_fin;
  139. /* prod crsr - confirmed by peer
  140. */
  141. union smc_host_cursor tx_curs_prep; /* tx - prepared data
  142. * snd_max..wmem_alloc
  143. */
  144. union smc_host_cursor tx_curs_sent; /* tx - sent data
  145. * snd_nxt ?
  146. */
  147. union smc_host_cursor tx_curs_fin; /* tx - confirmed by peer
  148. * snd-wnd-begin ?
  149. */
  150. atomic_t sndbuf_space; /* remaining space in sndbuf */
  151. u16 tx_cdc_seq; /* sequence # for CDC send */
  152. u16 tx_cdc_seq_fin; /* sequence # - tx completed */
  153. spinlock_t send_lock; /* protect wr_sends */
  154. atomic_t cdc_pend_tx_wr; /* number of pending tx CDC wqe
  155. * - inc when post wqe,
  156. * - dec on polled tx cqe
  157. */
  158. wait_queue_head_t cdc_pend_tx_wq; /* wakeup on no cdc_pend_tx_wr*/
  159. struct delayed_work tx_work; /* retry of smc_cdc_msg_send */
  160. u32 tx_off; /* base offset in peer rmb */
  161. struct smc_host_cdc_msg local_rx_ctrl; /* filled during event_handl.
  162. * .prod cf. TCP rcv_nxt
  163. * .cons cf. TCP snd_una
  164. */
  165. union smc_host_cursor rx_curs_confirmed; /* confirmed to peer
  166. * source of snd_una ?
  167. */
  168. union smc_host_cursor urg_curs; /* points at urgent byte */
  169. enum smc_urg_state urg_state;
  170. bool urg_tx_pend; /* urgent data staged */
  171. bool urg_rx_skip_pend;
  172. /* indicate urgent oob data
  173. * read, but previous regular
  174. * data still pending
  175. */
  176. char urg_rx_byte; /* urgent byte */
  177. atomic_t bytes_to_rcv; /* arrived data,
  178. * not yet received
  179. */
  180. atomic_t splice_pending; /* number of spliced bytes
  181. * pending processing
  182. */
  183. #ifndef KERNEL_HAS_ATOMIC64
  184. spinlock_t acurs_lock; /* protect cursors */
  185. #endif
  186. struct work_struct close_work; /* peer sent some closing */
  187. struct work_struct abort_work; /* abort the connection */
  188. struct tasklet_struct rx_tsklet; /* Receiver tasklet for SMC-D */
  189. u8 rx_off; /* receive offset:
  190. * 0 for SMC-R, 32 for SMC-D
  191. */
  192. u64 peer_token; /* SMC-D token of peer */
  193. u8 killed : 1; /* abnormal termination */
  194. u8 out_of_sync : 1; /* out of sync with peer */
  195. };
  196. struct smc_sock { /* smc sock container */
  197. struct sock sk;
  198. struct socket *clcsock; /* internal tcp socket */
  199. void (*clcsk_data_ready)(struct sock *sk);
  200. /* original data_ready fct. **/
  201. struct smc_connection conn; /* smc connection */
  202. struct smc_sock *listen_smc; /* listen parent */
  203. struct work_struct connect_work; /* handle non-blocking connect*/
  204. struct work_struct tcp_listen_work;/* handle tcp socket accepts */
  205. struct work_struct smc_listen_work;/* prepare new accept socket */
  206. struct list_head accept_q; /* sockets to be accepted */
  207. spinlock_t accept_q_lock; /* protects accept_q */
  208. bool use_fallback; /* fallback to tcp */
  209. int fallback_rsn; /* reason for fallback */
  210. u32 peer_diagnosis; /* decline reason from peer */
  211. int sockopt_defer_accept;
  212. /* sockopt TCP_DEFER_ACCEPT
  213. * value
  214. */
  215. u8 wait_close_tx_prepared : 1;
  216. /* shutdown wr or close
  217. * started, waiting for unsent
  218. * data to be sent
  219. */
  220. u8 connect_nonblock : 1;
  221. /* non-blocking connect in
  222. * flight
  223. */
  224. struct mutex clcsock_release_lock;
  225. /* protects clcsock of a listen
  226. * socket
  227. * */
  228. };
  229. static inline struct smc_sock *smc_sk(const struct sock *sk)
  230. {
  231. return (struct smc_sock *)sk;
  232. }
  233. extern struct workqueue_struct *smc_hs_wq; /* wq for handshake work */
  234. extern struct workqueue_struct *smc_close_wq; /* wq for close work */
  235. #define SMC_SYSTEMID_LEN 8
  236. extern u8 local_systemid[SMC_SYSTEMID_LEN]; /* unique system identifier */
  237. #define ntohll(x) be64_to_cpu(x)
  238. #define htonll(x) cpu_to_be64(x)
  239. /* convert an u32 value into network byte order, store it into a 3 byte field */
  240. static inline void hton24(u8 *net, u32 host)
  241. {
  242. __be32 t;
  243. t = cpu_to_be32(host);
  244. memcpy(net, ((u8 *)&t) + 1, 3);
  245. }
  246. /* convert a received 3 byte field into host byte order*/
  247. static inline u32 ntoh24(u8 *net)
  248. {
  249. __be32 t = 0;
  250. memcpy(((u8 *)&t) + 1, net, 3);
  251. return be32_to_cpu(t);
  252. }
  253. #ifdef CONFIG_XFRM
  254. static inline bool using_ipsec(struct smc_sock *smc)
  255. {
  256. return (smc->clcsock->sk->sk_policy[0] ||
  257. smc->clcsock->sk->sk_policy[1]) ? true : false;
  258. }
  259. #else
  260. static inline bool using_ipsec(struct smc_sock *smc)
  261. {
  262. return false;
  263. }
  264. #endif
  265. struct sock *smc_accept_dequeue(struct sock *parent, struct socket *new_sock);
  266. void smc_close_non_accepted(struct sock *sk);
  267. #endif /* __SMC_H */