Ursula Braun | 5f08318 | 2017-01-09 16:55:22 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Shared Memory Communications over RDMA (SMC-R) and RoCE |
| 3 | * |
| 4 | * Connection Data Control (CDC) |
| 5 | * handles flow control |
| 6 | * |
| 7 | * Copyright IBM Corp. 2016 |
| 8 | * |
| 9 | * Author(s): Ursula Braun <ubraun@linux.vnet.ibm.com> |
| 10 | */ |
| 11 | |
| 12 | #include <linux/spinlock.h> |
| 13 | |
| 14 | #include "smc.h" |
| 15 | #include "smc_wr.h" |
| 16 | #include "smc_cdc.h" |
Ursula Braun | e6727f3 | 2017-01-09 16:55:23 +0100 | [diff] [blame] | 17 | #include "smc_tx.h" |
Ursula Braun | 952310c | 2017-01-09 16:55:24 +0100 | [diff] [blame] | 18 | #include "smc_rx.h" |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 19 | #include "smc_close.h" |
Ursula Braun | 5f08318 | 2017-01-09 16:55:22 +0100 | [diff] [blame] | 20 | |
| 21 | /********************************** send *************************************/ |
| 22 | |
| 23 | struct smc_cdc_tx_pend { |
| 24 | struct smc_connection *conn; /* socket connection */ |
| 25 | union smc_host_cursor cursor; /* tx sndbuf cursor sent */ |
| 26 | union smc_host_cursor p_cursor; /* rx RMBE cursor produced */ |
| 27 | u16 ctrl_seq; /* conn. tx sequence # */ |
| 28 | }; |
| 29 | |
| 30 | /* handler for send/transmission completion of a CDC msg */ |
| 31 | static void smc_cdc_tx_handler(struct smc_wr_tx_pend_priv *pnd_snd, |
| 32 | struct smc_link *link, |
| 33 | enum ib_wc_status wc_status) |
| 34 | { |
| 35 | struct smc_cdc_tx_pend *cdcpend = (struct smc_cdc_tx_pend *)pnd_snd; |
| 36 | struct smc_sock *smc; |
| 37 | int diff; |
| 38 | |
| 39 | if (!cdcpend->conn) |
| 40 | /* already dismissed */ |
| 41 | return; |
| 42 | |
| 43 | smc = container_of(cdcpend->conn, struct smc_sock, conn); |
| 44 | bh_lock_sock(&smc->sk); |
| 45 | if (!wc_status) { |
| 46 | diff = smc_curs_diff(cdcpend->conn->sndbuf_size, |
| 47 | &cdcpend->conn->tx_curs_fin, |
| 48 | &cdcpend->cursor); |
| 49 | /* sndbuf_space is decreased in smc_sendmsg */ |
| 50 | smp_mb__before_atomic(); |
| 51 | atomic_add(diff, &cdcpend->conn->sndbuf_space); |
| 52 | /* guarantee 0 <= sndbuf_space <= sndbuf_size */ |
| 53 | smp_mb__after_atomic(); |
| 54 | smc_curs_write(&cdcpend->conn->tx_curs_fin, |
| 55 | smc_curs_read(&cdcpend->cursor, cdcpend->conn), |
| 56 | cdcpend->conn); |
| 57 | } |
Ursula Braun | e6727f3 | 2017-01-09 16:55:23 +0100 | [diff] [blame] | 58 | smc_tx_sndbuf_nonfull(smc); |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 59 | if (smc->sk.sk_state != SMC_ACTIVE) |
| 60 | /* wake up smc_close_wait_tx_pends() */ |
| 61 | smc->sk.sk_state_change(&smc->sk); |
Ursula Braun | 5f08318 | 2017-01-09 16:55:22 +0100 | [diff] [blame] | 62 | bh_unlock_sock(&smc->sk); |
| 63 | } |
| 64 | |
| 65 | int smc_cdc_get_free_slot(struct smc_link *link, |
| 66 | struct smc_wr_buf **wr_buf, |
| 67 | struct smc_cdc_tx_pend **pend) |
| 68 | { |
| 69 | return smc_wr_tx_get_free_slot(link, smc_cdc_tx_handler, wr_buf, |
| 70 | (struct smc_wr_tx_pend_priv **)pend); |
| 71 | } |
| 72 | |
| 73 | static inline void smc_cdc_add_pending_send(struct smc_connection *conn, |
| 74 | struct smc_cdc_tx_pend *pend) |
| 75 | { |
| 76 | BUILD_BUG_ON_MSG( |
| 77 | sizeof(struct smc_cdc_msg) > SMC_WR_BUF_SIZE, |
| 78 | "must increase SMC_WR_BUF_SIZE to at least sizeof(struct smc_cdc_msg)"); |
| 79 | BUILD_BUG_ON_MSG( |
| 80 | offsetof(struct smc_cdc_msg, reserved) > SMC_WR_TX_SIZE, |
| 81 | "must adapt SMC_WR_TX_SIZE to sizeof(struct smc_cdc_msg); if not all smc_wr upper layer protocols use the same message size any more, must start to set link->wr_tx_sges[i].length on each individual smc_wr_tx_send()"); |
| 82 | BUILD_BUG_ON_MSG( |
| 83 | sizeof(struct smc_cdc_tx_pend) > SMC_WR_TX_PEND_PRIV_SIZE, |
| 84 | "must increase SMC_WR_TX_PEND_PRIV_SIZE to at least sizeof(struct smc_cdc_tx_pend)"); |
| 85 | pend->conn = conn; |
| 86 | pend->cursor = conn->tx_curs_sent; |
| 87 | pend->p_cursor = conn->local_tx_ctrl.prod; |
| 88 | pend->ctrl_seq = conn->tx_cdc_seq; |
| 89 | } |
| 90 | |
| 91 | int smc_cdc_msg_send(struct smc_connection *conn, |
| 92 | struct smc_wr_buf *wr_buf, |
| 93 | struct smc_cdc_tx_pend *pend) |
| 94 | { |
| 95 | struct smc_link *link; |
| 96 | int rc; |
| 97 | |
| 98 | link = &conn->lgr->lnk[SMC_SINGLE_LINK]; |
| 99 | |
| 100 | smc_cdc_add_pending_send(conn, pend); |
| 101 | |
| 102 | conn->tx_cdc_seq++; |
| 103 | conn->local_tx_ctrl.seqno = conn->tx_cdc_seq; |
| 104 | smc_host_msg_to_cdc((struct smc_cdc_msg *)wr_buf, |
| 105 | &conn->local_tx_ctrl, conn); |
| 106 | rc = smc_wr_tx_send(link, (struct smc_wr_tx_pend_priv *)pend); |
| 107 | if (!rc) |
| 108 | smc_curs_write(&conn->rx_curs_confirmed, |
| 109 | smc_curs_read(&conn->local_tx_ctrl.cons, conn), |
| 110 | conn); |
| 111 | |
| 112 | return rc; |
| 113 | } |
| 114 | |
| 115 | int smc_cdc_get_slot_and_msg_send(struct smc_connection *conn) |
| 116 | { |
| 117 | struct smc_cdc_tx_pend *pend; |
| 118 | struct smc_wr_buf *wr_buf; |
| 119 | int rc; |
| 120 | |
| 121 | rc = smc_cdc_get_free_slot(&conn->lgr->lnk[SMC_SINGLE_LINK], &wr_buf, |
| 122 | &pend); |
| 123 | if (rc) |
| 124 | return rc; |
| 125 | |
| 126 | return smc_cdc_msg_send(conn, wr_buf, pend); |
| 127 | } |
| 128 | |
| 129 | static bool smc_cdc_tx_filter(struct smc_wr_tx_pend_priv *tx_pend, |
| 130 | unsigned long data) |
| 131 | { |
| 132 | struct smc_connection *conn = (struct smc_connection *)data; |
| 133 | struct smc_cdc_tx_pend *cdc_pend = |
| 134 | (struct smc_cdc_tx_pend *)tx_pend; |
| 135 | |
| 136 | return cdc_pend->conn == conn; |
| 137 | } |
| 138 | |
| 139 | static void smc_cdc_tx_dismisser(struct smc_wr_tx_pend_priv *tx_pend) |
| 140 | { |
| 141 | struct smc_cdc_tx_pend *cdc_pend = |
| 142 | (struct smc_cdc_tx_pend *)tx_pend; |
| 143 | |
| 144 | cdc_pend->conn = NULL; |
| 145 | } |
| 146 | |
| 147 | void smc_cdc_tx_dismiss_slots(struct smc_connection *conn) |
| 148 | { |
| 149 | struct smc_link *link = &conn->lgr->lnk[SMC_SINGLE_LINK]; |
| 150 | |
| 151 | smc_wr_tx_dismiss_slots(link, SMC_CDC_MSG_TYPE, |
| 152 | smc_cdc_tx_filter, smc_cdc_tx_dismisser, |
| 153 | (unsigned long)conn); |
| 154 | } |
| 155 | |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 156 | bool smc_cdc_tx_has_pending(struct smc_connection *conn) |
| 157 | { |
| 158 | struct smc_link *link = &conn->lgr->lnk[SMC_SINGLE_LINK]; |
| 159 | |
| 160 | return smc_wr_tx_has_pending(link, SMC_CDC_MSG_TYPE, |
| 161 | smc_cdc_tx_filter, (unsigned long)conn); |
| 162 | } |
| 163 | |
Ursula Braun | 5f08318 | 2017-01-09 16:55:22 +0100 | [diff] [blame] | 164 | /********************************* receive ***********************************/ |
| 165 | |
| 166 | static inline bool smc_cdc_before(u16 seq1, u16 seq2) |
| 167 | { |
| 168 | return (s16)(seq1 - seq2) < 0; |
| 169 | } |
| 170 | |
| 171 | static void smc_cdc_msg_recv_action(struct smc_sock *smc, |
| 172 | struct smc_link *link, |
| 173 | struct smc_cdc_msg *cdc) |
| 174 | { |
| 175 | union smc_host_cursor cons_old, prod_old; |
| 176 | struct smc_connection *conn = &smc->conn; |
| 177 | int diff_cons, diff_prod; |
| 178 | |
| 179 | if (!cdc->prod_flags.failover_validation) { |
| 180 | if (smc_cdc_before(ntohs(cdc->seqno), |
| 181 | conn->local_rx_ctrl.seqno)) |
| 182 | /* received seqno is old */ |
| 183 | return; |
| 184 | } |
| 185 | smc_curs_write(&prod_old, |
| 186 | smc_curs_read(&conn->local_rx_ctrl.prod, conn), |
| 187 | conn); |
| 188 | smc_curs_write(&cons_old, |
| 189 | smc_curs_read(&conn->local_rx_ctrl.cons, conn), |
| 190 | conn); |
| 191 | smc_cdc_msg_to_host(&conn->local_rx_ctrl, cdc, conn); |
| 192 | |
| 193 | diff_cons = smc_curs_diff(conn->peer_rmbe_size, &cons_old, |
| 194 | &conn->local_rx_ctrl.cons); |
| 195 | if (diff_cons) { |
| 196 | /* peer_rmbe_space is decreased during data transfer with RDMA |
| 197 | * write |
| 198 | */ |
| 199 | smp_mb__before_atomic(); |
| 200 | atomic_add(diff_cons, &conn->peer_rmbe_space); |
| 201 | /* guarantee 0 <= peer_rmbe_space <= peer_rmbe_size */ |
| 202 | smp_mb__after_atomic(); |
| 203 | } |
| 204 | |
| 205 | diff_prod = smc_curs_diff(conn->rmbe_size, &prod_old, |
| 206 | &conn->local_rx_ctrl.prod); |
| 207 | if (diff_prod) { |
| 208 | /* bytes_to_rcv is decreased in smc_recvmsg */ |
| 209 | smp_mb__before_atomic(); |
| 210 | atomic_add(diff_prod, &conn->bytes_to_rcv); |
| 211 | /* guarantee 0 <= bytes_to_rcv <= rmbe_size */ |
| 212 | smp_mb__after_atomic(); |
Ursula Braun | 952310c | 2017-01-09 16:55:24 +0100 | [diff] [blame] | 213 | smc->sk.sk_data_ready(&smc->sk); |
Ursula Braun | 5f08318 | 2017-01-09 16:55:22 +0100 | [diff] [blame] | 214 | } |
| 215 | |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 216 | if (conn->local_rx_ctrl.conn_state_flags.peer_conn_abort) { |
Ursula Braun | 5f08318 | 2017-01-09 16:55:22 +0100 | [diff] [blame] | 217 | smc->sk.sk_err = ECONNRESET; |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 218 | conn->local_tx_ctrl.conn_state_flags.peer_conn_abort = 1; |
| 219 | } |
Ursula Braun | 5f08318 | 2017-01-09 16:55:22 +0100 | [diff] [blame] | 220 | if (smc_cdc_rxed_any_close_or_senddone(conn)) |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 221 | smc_close_passive_received(smc); |
Ursula Braun | 5f08318 | 2017-01-09 16:55:22 +0100 | [diff] [blame] | 222 | |
| 223 | /* piggy backed tx info */ |
Ursula Braun | e6727f3 | 2017-01-09 16:55:23 +0100 | [diff] [blame] | 224 | /* trigger sndbuf consumer: RDMA write into peer RMBE and CDC */ |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 225 | if (diff_cons && smc_tx_prepared_sends(conn)) { |
Ursula Braun | e6727f3 | 2017-01-09 16:55:23 +0100 | [diff] [blame] | 226 | smc_tx_sndbuf_nonempty(conn); |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 227 | /* trigger socket release if connection closed */ |
| 228 | smc_close_wake_tx_prepared(smc); |
| 229 | } |
Ursula Braun | 5f08318 | 2017-01-09 16:55:22 +0100 | [diff] [blame] | 230 | |
Ursula Braun | 5f08318 | 2017-01-09 16:55:22 +0100 | [diff] [blame] | 231 | /* socket connected but not accepted */ |
| 232 | if (!smc->sk.sk_socket) |
| 233 | return; |
| 234 | |
| 235 | /* data available */ |
Ursula Braun | 952310c | 2017-01-09 16:55:24 +0100 | [diff] [blame] | 236 | if ((conn->local_rx_ctrl.prod_flags.write_blocked) || |
| 237 | (conn->local_rx_ctrl.prod_flags.cons_curs_upd_req)) |
| 238 | smc_tx_consumer_update(conn); |
Ursula Braun | 5f08318 | 2017-01-09 16:55:22 +0100 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | /* called under tasklet context */ |
| 242 | static inline void smc_cdc_msg_recv(struct smc_cdc_msg *cdc, |
| 243 | struct smc_link *link, u64 wr_id) |
| 244 | { |
| 245 | struct smc_link_group *lgr = container_of(link, struct smc_link_group, |
| 246 | lnk[SMC_SINGLE_LINK]); |
| 247 | struct smc_connection *connection; |
| 248 | struct smc_sock *smc; |
| 249 | |
| 250 | /* lookup connection */ |
| 251 | read_lock_bh(&lgr->conns_lock); |
| 252 | connection = smc_lgr_find_conn(ntohl(cdc->token), lgr); |
| 253 | if (!connection) { |
| 254 | read_unlock_bh(&lgr->conns_lock); |
| 255 | return; |
| 256 | } |
| 257 | smc = container_of(connection, struct smc_sock, conn); |
| 258 | sock_hold(&smc->sk); |
| 259 | read_unlock_bh(&lgr->conns_lock); |
| 260 | bh_lock_sock(&smc->sk); |
| 261 | smc_cdc_msg_recv_action(smc, link, cdc); |
| 262 | bh_unlock_sock(&smc->sk); |
| 263 | sock_put(&smc->sk); /* no free sk in softirq-context */ |
| 264 | } |
| 265 | |
| 266 | /***************************** init, exit, misc ******************************/ |
| 267 | |
| 268 | static void smc_cdc_rx_handler(struct ib_wc *wc, void *buf) |
| 269 | { |
| 270 | struct smc_link *link = (struct smc_link *)wc->qp->qp_context; |
| 271 | struct smc_cdc_msg *cdc = buf; |
| 272 | |
| 273 | if (wc->byte_len < offsetof(struct smc_cdc_msg, reserved)) |
| 274 | return; /* short message */ |
| 275 | if (cdc->len != sizeof(*cdc)) |
| 276 | return; /* invalid message */ |
| 277 | smc_cdc_msg_recv(cdc, link, wc->wr_id); |
| 278 | } |
| 279 | |
| 280 | static struct smc_wr_rx_handler smc_cdc_rx_handlers[] = { |
| 281 | { |
| 282 | .handler = smc_cdc_rx_handler, |
| 283 | .type = SMC_CDC_MSG_TYPE |
| 284 | }, |
| 285 | { |
| 286 | .handler = NULL, |
| 287 | } |
| 288 | }; |
| 289 | |
| 290 | int __init smc_cdc_init(void) |
| 291 | { |
| 292 | struct smc_wr_rx_handler *handler; |
| 293 | int rc = 0; |
| 294 | |
| 295 | for (handler = smc_cdc_rx_handlers; handler->handler; handler++) { |
| 296 | INIT_HLIST_NODE(&handler->list); |
| 297 | rc = smc_wr_rx_register_handler(handler); |
| 298 | if (rc) |
| 299 | break; |
| 300 | } |
| 301 | return rc; |
| 302 | } |