smc.h 7.9 KB

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