smc.h 7.7 KB

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