smc.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  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 <[email protected]>
  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/genetlink.h>
  17. #include <net/sock.h>
  18. #include "smc_ib.h"
  19. #define SMC_V1 1 /* SMC version V1 */
  20. #define SMC_V2 2 /* SMC version V2 */
  21. #define SMC_RELEASE 0
  22. #define SMCPROTO_SMC 0 /* SMC protocol, IPv4 */
  23. #define SMCPROTO_SMC6 1 /* SMC protocol, IPv6 */
  24. #define SMC_MAX_ISM_DEVS 8 /* max # of proposed non-native ISM
  25. * devices
  26. */
  27. #define SMC_AUTOCORKING_DEFAULT_SIZE 0x10000 /* 64K by default */
  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. union {
  52. u8 type;
  53. #if defined(__BIG_ENDIAN_BITFIELD)
  54. struct {
  55. u8 llc_version:4,
  56. llc_type:4;
  57. };
  58. #elif defined(__LITTLE_ENDIAN_BITFIELD)
  59. struct {
  60. u8 llc_type:4,
  61. llc_version:4;
  62. };
  63. #endif
  64. };
  65. } __aligned(1);
  66. struct smc_cdc_conn_state_flags {
  67. #if defined(__BIG_ENDIAN_BITFIELD)
  68. u8 peer_done_writing : 1; /* Sending done indicator */
  69. u8 peer_conn_closed : 1; /* Peer connection closed indicator */
  70. u8 peer_conn_abort : 1; /* Abnormal close indicator */
  71. u8 reserved : 5;
  72. #elif defined(__LITTLE_ENDIAN_BITFIELD)
  73. u8 reserved : 5;
  74. u8 peer_conn_abort : 1;
  75. u8 peer_conn_closed : 1;
  76. u8 peer_done_writing : 1;
  77. #endif
  78. };
  79. struct smc_cdc_producer_flags {
  80. #if defined(__BIG_ENDIAN_BITFIELD)
  81. u8 write_blocked : 1; /* Writing Blocked, no rx buf space */
  82. u8 urg_data_pending : 1; /* Urgent Data Pending */
  83. u8 urg_data_present : 1; /* Urgent Data Present */
  84. u8 cons_curs_upd_req : 1; /* cursor update requested */
  85. u8 failover_validation : 1;/* message replay due to failover */
  86. u8 reserved : 3;
  87. #elif defined(__LITTLE_ENDIAN_BITFIELD)
  88. u8 reserved : 3;
  89. u8 failover_validation : 1;
  90. u8 cons_curs_upd_req : 1;
  91. u8 urg_data_present : 1;
  92. u8 urg_data_pending : 1;
  93. u8 write_blocked : 1;
  94. #endif
  95. };
  96. /* in host byte order */
  97. union smc_host_cursor { /* SMC cursor - an offset in an RMBE */
  98. struct {
  99. u16 reserved;
  100. u16 wrap; /* window wrap sequence number */
  101. u32 count; /* cursor (= offset) part */
  102. };
  103. #ifdef KERNEL_HAS_ATOMIC64
  104. atomic64_t acurs; /* for atomic processing */
  105. #else
  106. u64 acurs; /* for atomic processing */
  107. #endif
  108. } __aligned(8);
  109. /* in host byte order, except for flag bitfields in network byte order */
  110. struct smc_host_cdc_msg { /* Connection Data Control message */
  111. struct smc_wr_rx_hdr common; /* .type = 0xFE */
  112. u8 len; /* length = 44 */
  113. u16 seqno; /* connection seq # */
  114. u32 token; /* alert_token */
  115. union smc_host_cursor prod; /* producer cursor */
  116. union smc_host_cursor cons; /* consumer cursor,
  117. * piggy backed "ack"
  118. */
  119. struct smc_cdc_producer_flags prod_flags; /* conn. tx/rx status */
  120. struct smc_cdc_conn_state_flags conn_state_flags; /* peer conn. status*/
  121. u8 reserved[18];
  122. } __aligned(8);
  123. enum smc_urg_state {
  124. SMC_URG_VALID = 1, /* data present */
  125. SMC_URG_NOTYET = 2, /* data pending */
  126. SMC_URG_READ = 3, /* data was already read */
  127. };
  128. struct smc_mark_woken {
  129. bool woken;
  130. void *key;
  131. wait_queue_entry_t wait_entry;
  132. };
  133. struct smc_connection {
  134. struct rb_node alert_node;
  135. struct smc_link_group *lgr; /* link group of connection */
  136. struct smc_link *lnk; /* assigned SMC-R link */
  137. u32 alert_token_local; /* unique conn. id */
  138. u8 peer_rmbe_idx; /* from tcp handshake */
  139. int peer_rmbe_size; /* size of peer rx buffer */
  140. atomic_t peer_rmbe_space;/* remaining free bytes in peer
  141. * rmbe
  142. */
  143. int rtoken_idx; /* idx to peer RMB rkey/addr */
  144. struct smc_buf_desc *sndbuf_desc; /* send buffer descriptor */
  145. struct smc_buf_desc *rmb_desc; /* RMBE descriptor */
  146. int rmbe_size_comp; /* compressed notation */
  147. int rmbe_update_limit;
  148. /* lower limit for consumer
  149. * cursor update
  150. */
  151. struct smc_host_cdc_msg local_tx_ctrl; /* host byte order staging
  152. * buffer for CDC msg send
  153. * .prod cf. TCP snd_nxt
  154. * .cons cf. TCP sends ack
  155. */
  156. union smc_host_cursor local_tx_ctrl_fin;
  157. /* prod crsr - confirmed by peer
  158. */
  159. union smc_host_cursor tx_curs_prep; /* tx - prepared data
  160. * snd_max..wmem_alloc
  161. */
  162. union smc_host_cursor tx_curs_sent; /* tx - sent data
  163. * snd_nxt ?
  164. */
  165. union smc_host_cursor tx_curs_fin; /* tx - confirmed by peer
  166. * snd-wnd-begin ?
  167. */
  168. atomic_t sndbuf_space; /* remaining space in sndbuf */
  169. u16 tx_cdc_seq; /* sequence # for CDC send */
  170. u16 tx_cdc_seq_fin; /* sequence # - tx completed */
  171. spinlock_t send_lock; /* protect wr_sends */
  172. atomic_t cdc_pend_tx_wr; /* number of pending tx CDC wqe
  173. * - inc when post wqe,
  174. * - dec on polled tx cqe
  175. */
  176. wait_queue_head_t cdc_pend_tx_wq; /* wakeup on no cdc_pend_tx_wr*/
  177. atomic_t tx_pushing; /* nr_threads trying tx push */
  178. struct delayed_work tx_work; /* retry of smc_cdc_msg_send */
  179. u32 tx_off; /* base offset in peer rmb */
  180. struct smc_host_cdc_msg local_rx_ctrl; /* filled during event_handl.
  181. * .prod cf. TCP rcv_nxt
  182. * .cons cf. TCP snd_una
  183. */
  184. union smc_host_cursor rx_curs_confirmed; /* confirmed to peer
  185. * source of snd_una ?
  186. */
  187. union smc_host_cursor urg_curs; /* points at urgent byte */
  188. enum smc_urg_state urg_state;
  189. bool urg_tx_pend; /* urgent data staged */
  190. bool urg_rx_skip_pend;
  191. /* indicate urgent oob data
  192. * read, but previous regular
  193. * data still pending
  194. */
  195. char urg_rx_byte; /* urgent byte */
  196. bool tx_in_release_sock;
  197. /* flush pending tx data in
  198. * sock release_cb()
  199. */
  200. atomic_t bytes_to_rcv; /* arrived data,
  201. * not yet received
  202. */
  203. atomic_t splice_pending; /* number of spliced bytes
  204. * pending processing
  205. */
  206. #ifndef KERNEL_HAS_ATOMIC64
  207. spinlock_t acurs_lock; /* protect cursors */
  208. #endif
  209. struct work_struct close_work; /* peer sent some closing */
  210. struct work_struct abort_work; /* abort the connection */
  211. struct tasklet_struct rx_tsklet; /* Receiver tasklet for SMC-D */
  212. u8 rx_off; /* receive offset:
  213. * 0 for SMC-R, 32 for SMC-D
  214. */
  215. u64 peer_token; /* SMC-D token of peer */
  216. u8 killed : 1; /* abnormal termination */
  217. u8 freed : 1; /* normal termiation */
  218. u8 out_of_sync : 1; /* out of sync with peer */
  219. };
  220. struct smc_sock { /* smc sock container */
  221. struct sock sk;
  222. struct socket *clcsock; /* internal tcp socket */
  223. void (*clcsk_state_change)(struct sock *sk);
  224. /* original stat_change fct. */
  225. void (*clcsk_data_ready)(struct sock *sk);
  226. /* original data_ready fct. */
  227. void (*clcsk_write_space)(struct sock *sk);
  228. /* original write_space fct. */
  229. void (*clcsk_error_report)(struct sock *sk);
  230. /* original error_report fct. */
  231. struct smc_connection conn; /* smc connection */
  232. struct smc_sock *listen_smc; /* listen parent */
  233. struct work_struct connect_work; /* handle non-blocking connect*/
  234. struct work_struct tcp_listen_work;/* handle tcp socket accepts */
  235. struct work_struct smc_listen_work;/* prepare new accept socket */
  236. struct list_head accept_q; /* sockets to be accepted */
  237. spinlock_t accept_q_lock; /* protects accept_q */
  238. bool limit_smc_hs; /* put constraint on handshake */
  239. bool use_fallback; /* fallback to tcp */
  240. int fallback_rsn; /* reason for fallback */
  241. u32 peer_diagnosis; /* decline reason from peer */
  242. atomic_t queued_smc_hs; /* queued smc handshakes */
  243. struct inet_connection_sock_af_ops af_ops;
  244. const struct inet_connection_sock_af_ops *ori_af_ops;
  245. /* original af ops */
  246. int sockopt_defer_accept;
  247. /* sockopt TCP_DEFER_ACCEPT
  248. * value
  249. */
  250. u8 wait_close_tx_prepared : 1;
  251. /* shutdown wr or close
  252. * started, waiting for unsent
  253. * data to be sent
  254. */
  255. u8 connect_nonblock : 1;
  256. /* non-blocking connect in
  257. * flight
  258. */
  259. struct mutex clcsock_release_lock;
  260. /* protects clcsock of a listen
  261. * socket
  262. * */
  263. };
  264. static inline struct smc_sock *smc_sk(const struct sock *sk)
  265. {
  266. return (struct smc_sock *)sk;
  267. }
  268. static inline void smc_init_saved_callbacks(struct smc_sock *smc)
  269. {
  270. smc->clcsk_state_change = NULL;
  271. smc->clcsk_data_ready = NULL;
  272. smc->clcsk_write_space = NULL;
  273. smc->clcsk_error_report = NULL;
  274. }
  275. static inline struct smc_sock *smc_clcsock_user_data(const struct sock *clcsk)
  276. {
  277. return (struct smc_sock *)
  278. ((uintptr_t)clcsk->sk_user_data & ~SK_USER_DATA_NOCOPY);
  279. }
  280. /* save target_cb in saved_cb, and replace target_cb with new_cb */
  281. static inline void smc_clcsock_replace_cb(void (**target_cb)(struct sock *),
  282. void (*new_cb)(struct sock *),
  283. void (**saved_cb)(struct sock *))
  284. {
  285. /* only save once */
  286. if (!*saved_cb)
  287. *saved_cb = *target_cb;
  288. *target_cb = new_cb;
  289. }
  290. /* restore target_cb to saved_cb, and reset saved_cb to NULL */
  291. static inline void smc_clcsock_restore_cb(void (**target_cb)(struct sock *),
  292. void (**saved_cb)(struct sock *))
  293. {
  294. if (!*saved_cb)
  295. return;
  296. *target_cb = *saved_cb;
  297. *saved_cb = NULL;
  298. }
  299. extern struct workqueue_struct *smc_hs_wq; /* wq for handshake work */
  300. extern struct workqueue_struct *smc_close_wq; /* wq for close work */
  301. #define SMC_SYSTEMID_LEN 8
  302. extern u8 local_systemid[SMC_SYSTEMID_LEN]; /* unique system identifier */
  303. #define ntohll(x) be64_to_cpu(x)
  304. #define htonll(x) cpu_to_be64(x)
  305. /* convert an u32 value into network byte order, store it into a 3 byte field */
  306. static inline void hton24(u8 *net, u32 host)
  307. {
  308. __be32 t;
  309. t = cpu_to_be32(host);
  310. memcpy(net, ((u8 *)&t) + 1, 3);
  311. }
  312. /* convert a received 3 byte field into host byte order*/
  313. static inline u32 ntoh24(u8 *net)
  314. {
  315. __be32 t = 0;
  316. memcpy(((u8 *)&t) + 1, net, 3);
  317. return be32_to_cpu(t);
  318. }
  319. #ifdef CONFIG_XFRM
  320. static inline bool using_ipsec(struct smc_sock *smc)
  321. {
  322. return (smc->clcsock->sk->sk_policy[0] ||
  323. smc->clcsock->sk->sk_policy[1]) ? true : false;
  324. }
  325. #else
  326. static inline bool using_ipsec(struct smc_sock *smc)
  327. {
  328. return false;
  329. }
  330. #endif
  331. struct smc_gidlist;
  332. struct sock *smc_accept_dequeue(struct sock *parent, struct socket *new_sock);
  333. void smc_close_non_accepted(struct sock *sk);
  334. void smc_fill_gid_list(struct smc_link_group *lgr,
  335. struct smc_gidlist *gidlist,
  336. struct smc_ib_device *known_dev, u8 *known_gid);
  337. /* smc handshake limitation interface for netlink */
  338. int smc_nl_dump_hs_limitation(struct sk_buff *skb, struct netlink_callback *cb);
  339. int smc_nl_enable_hs_limitation(struct sk_buff *skb, struct genl_info *info);
  340. int smc_nl_disable_hs_limitation(struct sk_buff *skb, struct genl_info *info);
  341. static inline void smc_sock_set_flag(struct sock *sk, enum sock_flags flag)
  342. {
  343. set_bit(flag, &sk->sk_flags);
  344. }
  345. #endif /* __SMC_H */