smc.h 7.8 KB

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