smc.h 7.1 KB

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