smc.h 13 KB

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