Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 2 | /* |
| 3 | * Shared Memory Communications over RDMA (SMC-R) and RoCE |
| 4 | * |
| 5 | * CLC (connection layer control) handshake over initial TCP socket to |
| 6 | * prepare for RDMA traffic |
| 7 | * |
| 8 | * Copyright IBM Corp. 2016 |
| 9 | * |
| 10 | * Author(s): Ursula Braun <ubraun@linux.vnet.ibm.com> |
| 11 | */ |
| 12 | |
| 13 | #include <linux/in.h> |
Ursula Braun | 143c017 | 2017-01-12 14:57:15 +0100 | [diff] [blame] | 14 | #include <linux/if_ether.h> |
Ingo Molnar | c3edc40 | 2017-02-02 08:35:14 +0100 | [diff] [blame] | 15 | #include <linux/sched/signal.h> |
| 16 | |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 17 | #include <net/sock.h> |
| 18 | #include <net/tcp.h> |
| 19 | |
| 20 | #include "smc.h" |
Ursula Braun | 0cfdd8f | 2017-01-09 16:55:17 +0100 | [diff] [blame] | 21 | #include "smc_core.h" |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 22 | #include "smc_clc.h" |
| 23 | #include "smc_ib.h" |
| 24 | |
| 25 | /* Wait for data on the tcp-socket, analyze received data |
| 26 | * Returns: |
| 27 | * 0 if success and it was not a decline that we received. |
| 28 | * SMC_CLC_DECL_REPLY if decline received for fallback w/o another decl send. |
| 29 | * clcsock error, -EINTR, -ECONNRESET, -EPROTO otherwise. |
| 30 | */ |
| 31 | int smc_clc_wait_msg(struct smc_sock *smc, void *buf, int buflen, |
| 32 | u8 expected_type) |
| 33 | { |
| 34 | struct sock *clc_sk = smc->clcsock->sk; |
| 35 | struct smc_clc_msg_hdr *clcm = buf; |
| 36 | struct msghdr msg = {NULL, 0}; |
| 37 | int reason_code = 0; |
Al Viro | d63d271 | 2017-09-20 20:21:22 -0400 | [diff] [blame^] | 38 | struct kvec vec = {buf, buflen}; |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 39 | int len, datlen; |
| 40 | int krflags; |
| 41 | |
| 42 | /* peek the first few bytes to determine length of data to receive |
| 43 | * so we don't consume any subsequent CLC message or payload data |
| 44 | * in the TCP byte stream |
| 45 | */ |
Al Viro | d63d271 | 2017-09-20 20:21:22 -0400 | [diff] [blame^] | 46 | /* |
| 47 | * Caller must make sure that buflen is no less than |
| 48 | * sizeof(struct smc_clc_msg_hdr) |
| 49 | */ |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 50 | krflags = MSG_PEEK | MSG_WAITALL; |
| 51 | smc->clcsock->sk->sk_rcvtimeo = CLC_WAIT_TIME; |
Al Viro | d63d271 | 2017-09-20 20:21:22 -0400 | [diff] [blame^] | 52 | iov_iter_kvec(&msg.msg_iter, READ | ITER_KVEC, &vec, 1, |
| 53 | sizeof(struct smc_clc_msg_hdr)); |
| 54 | len = sock_recvmsg(smc->clcsock, &msg, krflags); |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 55 | if (signal_pending(current)) { |
| 56 | reason_code = -EINTR; |
| 57 | clc_sk->sk_err = EINTR; |
| 58 | smc->sk.sk_err = EINTR; |
| 59 | goto out; |
| 60 | } |
| 61 | if (clc_sk->sk_err) { |
| 62 | reason_code = -clc_sk->sk_err; |
| 63 | smc->sk.sk_err = clc_sk->sk_err; |
| 64 | goto out; |
| 65 | } |
| 66 | if (!len) { /* peer has performed orderly shutdown */ |
| 67 | smc->sk.sk_err = ECONNRESET; |
| 68 | reason_code = -ECONNRESET; |
| 69 | goto out; |
| 70 | } |
| 71 | if (len < 0) { |
| 72 | smc->sk.sk_err = -len; |
| 73 | reason_code = len; |
| 74 | goto out; |
| 75 | } |
| 76 | datlen = ntohs(clcm->length); |
| 77 | if ((len < sizeof(struct smc_clc_msg_hdr)) || |
| 78 | (datlen < sizeof(struct smc_clc_msg_decline)) || |
| 79 | (datlen > sizeof(struct smc_clc_msg_accept_confirm)) || |
| 80 | memcmp(clcm->eyecatcher, SMC_EYECATCHER, sizeof(SMC_EYECATCHER)) || |
| 81 | ((clcm->type != SMC_CLC_DECLINE) && |
| 82 | (clcm->type != expected_type))) { |
| 83 | smc->sk.sk_err = EPROTO; |
| 84 | reason_code = -EPROTO; |
| 85 | goto out; |
| 86 | } |
| 87 | |
| 88 | /* receive the complete CLC message */ |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 89 | memset(&msg, 0, sizeof(struct msghdr)); |
Al Viro | d63d271 | 2017-09-20 20:21:22 -0400 | [diff] [blame^] | 90 | iov_iter_kvec(&msg.msg_iter, READ | ITER_KVEC, &vec, 1, buflen); |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 91 | krflags = MSG_WAITALL; |
| 92 | smc->clcsock->sk->sk_rcvtimeo = CLC_WAIT_TIME; |
Al Viro | d63d271 | 2017-09-20 20:21:22 -0400 | [diff] [blame^] | 93 | len = sock_recvmsg(smc->clcsock, &msg, krflags); |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 94 | if (len < datlen) { |
| 95 | smc->sk.sk_err = EPROTO; |
| 96 | reason_code = -EPROTO; |
| 97 | goto out; |
| 98 | } |
Ursula Braun | 0cfdd8f | 2017-01-09 16:55:17 +0100 | [diff] [blame] | 99 | if (clcm->type == SMC_CLC_DECLINE) { |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 100 | reason_code = SMC_CLC_DECL_REPLY; |
Ursula Braun | bfbedfd | 2017-09-21 09:16:32 +0200 | [diff] [blame] | 101 | if (((struct smc_clc_msg_decline *)buf)->hdr.flag) { |
Ursula Braun | 0cfdd8f | 2017-01-09 16:55:17 +0100 | [diff] [blame] | 102 | smc->conn.lgr->sync_err = true; |
Ursula Braun | bfbedfd | 2017-09-21 09:16:32 +0200 | [diff] [blame] | 103 | smc_lgr_terminate(smc->conn.lgr); |
| 104 | } |
Ursula Braun | 0cfdd8f | 2017-01-09 16:55:17 +0100 | [diff] [blame] | 105 | } |
| 106 | |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 107 | out: |
| 108 | return reason_code; |
| 109 | } |
| 110 | |
| 111 | /* send CLC DECLINE message across internal TCP socket */ |
Ursula Braun | bfbedfd | 2017-09-21 09:16:32 +0200 | [diff] [blame] | 112 | int smc_clc_send_decline(struct smc_sock *smc, u32 peer_diag_info) |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 113 | { |
| 114 | struct smc_clc_msg_decline dclc; |
| 115 | struct msghdr msg; |
| 116 | struct kvec vec; |
| 117 | int len; |
| 118 | |
| 119 | memset(&dclc, 0, sizeof(dclc)); |
| 120 | memcpy(dclc.hdr.eyecatcher, SMC_EYECATCHER, sizeof(SMC_EYECATCHER)); |
| 121 | dclc.hdr.type = SMC_CLC_DECLINE; |
| 122 | dclc.hdr.length = htons(sizeof(struct smc_clc_msg_decline)); |
| 123 | dclc.hdr.version = SMC_CLC_V1; |
Ursula Braun | bfbedfd | 2017-09-21 09:16:32 +0200 | [diff] [blame] | 124 | dclc.hdr.flag = (peer_diag_info == SMC_CLC_DECL_SYNCERR) ? 1 : 0; |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 125 | memcpy(dclc.id_for_peer, local_systemid, sizeof(local_systemid)); |
| 126 | dclc.peer_diagnosis = htonl(peer_diag_info); |
| 127 | memcpy(dclc.trl.eyecatcher, SMC_EYECATCHER, sizeof(SMC_EYECATCHER)); |
| 128 | |
| 129 | memset(&msg, 0, sizeof(msg)); |
| 130 | vec.iov_base = &dclc; |
| 131 | vec.iov_len = sizeof(struct smc_clc_msg_decline); |
| 132 | len = kernel_sendmsg(smc->clcsock, &msg, &vec, 1, |
| 133 | sizeof(struct smc_clc_msg_decline)); |
| 134 | if (len < sizeof(struct smc_clc_msg_decline)) |
| 135 | smc->sk.sk_err = EPROTO; |
| 136 | if (len < 0) |
| 137 | smc->sk.sk_err = -len; |
| 138 | return len; |
| 139 | } |
| 140 | |
| 141 | /* send CLC PROPOSAL message across internal TCP socket */ |
| 142 | int smc_clc_send_proposal(struct smc_sock *smc, |
| 143 | struct smc_ib_device *smcibdev, |
| 144 | u8 ibport) |
| 145 | { |
| 146 | struct smc_clc_msg_proposal pclc; |
| 147 | int reason_code = 0; |
| 148 | struct msghdr msg; |
| 149 | struct kvec vec; |
| 150 | int len, rc; |
| 151 | |
| 152 | /* send SMC Proposal CLC message */ |
| 153 | memset(&pclc, 0, sizeof(pclc)); |
| 154 | memcpy(pclc.hdr.eyecatcher, SMC_EYECATCHER, sizeof(SMC_EYECATCHER)); |
| 155 | pclc.hdr.type = SMC_CLC_PROPOSAL; |
| 156 | pclc.hdr.length = htons(sizeof(pclc)); |
| 157 | pclc.hdr.version = SMC_CLC_V1; /* SMC version */ |
| 158 | memcpy(pclc.lcl.id_for_peer, local_systemid, sizeof(local_systemid)); |
| 159 | memcpy(&pclc.lcl.gid, &smcibdev->gid[ibport - 1], SMC_GID_SIZE); |
Ursula Braun | 143c017 | 2017-01-12 14:57:15 +0100 | [diff] [blame] | 160 | memcpy(&pclc.lcl.mac, &smcibdev->mac[ibport - 1], ETH_ALEN); |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 161 | |
| 162 | /* determine subnet and mask from internal TCP socket */ |
| 163 | rc = smc_netinfo_by_tcpsk(smc->clcsock, &pclc.outgoing_subnet, |
| 164 | &pclc.prefix_len); |
| 165 | if (rc) |
| 166 | return SMC_CLC_DECL_CNFERR; /* configuration error */ |
| 167 | memcpy(pclc.trl.eyecatcher, SMC_EYECATCHER, sizeof(SMC_EYECATCHER)); |
| 168 | memset(&msg, 0, sizeof(msg)); |
| 169 | vec.iov_base = &pclc; |
| 170 | vec.iov_len = sizeof(pclc); |
| 171 | /* due to the few bytes needed for clc-handshake this cannot block */ |
| 172 | len = kernel_sendmsg(smc->clcsock, &msg, &vec, 1, sizeof(pclc)); |
| 173 | if (len < sizeof(pclc)) { |
| 174 | if (len >= 0) { |
| 175 | reason_code = -ENETUNREACH; |
| 176 | smc->sk.sk_err = -reason_code; |
| 177 | } else { |
| 178 | smc->sk.sk_err = smc->clcsock->sk->sk_err; |
| 179 | reason_code = -smc->sk.sk_err; |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | return reason_code; |
| 184 | } |
| 185 | |
| 186 | /* send CLC CONFIRM message across internal TCP socket */ |
| 187 | int smc_clc_send_confirm(struct smc_sock *smc) |
| 188 | { |
Ursula Braun | 0cfdd8f | 2017-01-09 16:55:17 +0100 | [diff] [blame] | 189 | struct smc_connection *conn = &smc->conn; |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 190 | struct smc_clc_msg_accept_confirm cclc; |
Ursula Braun | 0cfdd8f | 2017-01-09 16:55:17 +0100 | [diff] [blame] | 191 | struct smc_link *link; |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 192 | int reason_code = 0; |
| 193 | struct msghdr msg; |
| 194 | struct kvec vec; |
| 195 | int len; |
| 196 | |
Ursula Braun | 0cfdd8f | 2017-01-09 16:55:17 +0100 | [diff] [blame] | 197 | link = &conn->lgr->lnk[SMC_SINGLE_LINK]; |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 198 | /* send SMC Confirm CLC msg */ |
| 199 | memset(&cclc, 0, sizeof(cclc)); |
| 200 | memcpy(cclc.hdr.eyecatcher, SMC_EYECATCHER, sizeof(SMC_EYECATCHER)); |
| 201 | cclc.hdr.type = SMC_CLC_CONFIRM; |
| 202 | cclc.hdr.length = htons(sizeof(cclc)); |
| 203 | cclc.hdr.version = SMC_CLC_V1; /* SMC version */ |
| 204 | memcpy(cclc.lcl.id_for_peer, local_systemid, sizeof(local_systemid)); |
Ursula Braun | 0cfdd8f | 2017-01-09 16:55:17 +0100 | [diff] [blame] | 205 | memcpy(&cclc.lcl.gid, &link->smcibdev->gid[link->ibport - 1], |
| 206 | SMC_GID_SIZE); |
Ursula Braun | 143c017 | 2017-01-12 14:57:15 +0100 | [diff] [blame] | 207 | memcpy(&cclc.lcl.mac, &link->smcibdev->mac[link->ibport - 1], ETH_ALEN); |
Ursula Braun | 0cfdd8f | 2017-01-09 16:55:17 +0100 | [diff] [blame] | 208 | hton24(cclc.qpn, link->roce_qp->qp_num); |
Ursula Braun | bd4ad57 | 2017-01-09 16:55:20 +0100 | [diff] [blame] | 209 | cclc.rmb_rkey = |
Ursula Braun | 897e1c2 | 2017-07-28 13:56:16 +0200 | [diff] [blame] | 210 | htonl(conn->rmb_desc->mr_rx[SMC_SINGLE_LINK]->rkey); |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 211 | cclc.conn_idx = 1; /* for now: 1 RMB = 1 RMBE */ |
Ursula Braun | 0cfdd8f | 2017-01-09 16:55:17 +0100 | [diff] [blame] | 212 | cclc.rmbe_alert_token = htonl(conn->alert_token_local); |
| 213 | cclc.qp_mtu = min(link->path_mtu, link->peer_mtu); |
Ursula Braun | bd4ad57 | 2017-01-09 16:55:20 +0100 | [diff] [blame] | 214 | cclc.rmbe_size = conn->rmbe_size_short; |
Ursula Braun | a3fe3d0 | 2017-07-28 13:56:15 +0200 | [diff] [blame] | 215 | cclc.rmb_dma_addr = cpu_to_be64( |
| 216 | (u64)sg_dma_address(conn->rmb_desc->sgt[SMC_SINGLE_LINK].sgl)); |
Ursula Braun | 0cfdd8f | 2017-01-09 16:55:17 +0100 | [diff] [blame] | 217 | hton24(cclc.psn, link->psn_initial); |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 218 | |
| 219 | memcpy(cclc.trl.eyecatcher, SMC_EYECATCHER, sizeof(SMC_EYECATCHER)); |
| 220 | |
| 221 | memset(&msg, 0, sizeof(msg)); |
| 222 | vec.iov_base = &cclc; |
| 223 | vec.iov_len = sizeof(cclc); |
| 224 | len = kernel_sendmsg(smc->clcsock, &msg, &vec, 1, sizeof(cclc)); |
| 225 | if (len < sizeof(cclc)) { |
| 226 | if (len >= 0) { |
| 227 | reason_code = -ENETUNREACH; |
| 228 | smc->sk.sk_err = -reason_code; |
| 229 | } else { |
| 230 | smc->sk.sk_err = smc->clcsock->sk->sk_err; |
| 231 | reason_code = -smc->sk.sk_err; |
| 232 | } |
| 233 | } |
| 234 | return reason_code; |
| 235 | } |
| 236 | |
| 237 | /* send CLC ACCEPT message across internal TCP socket */ |
Ursula Braun | 0cfdd8f | 2017-01-09 16:55:17 +0100 | [diff] [blame] | 238 | int smc_clc_send_accept(struct smc_sock *new_smc, int srv_first_contact) |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 239 | { |
Ursula Braun | 0cfdd8f | 2017-01-09 16:55:17 +0100 | [diff] [blame] | 240 | struct smc_connection *conn = &new_smc->conn; |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 241 | struct smc_clc_msg_accept_confirm aclc; |
Ursula Braun | 0cfdd8f | 2017-01-09 16:55:17 +0100 | [diff] [blame] | 242 | struct smc_link *link; |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 243 | struct msghdr msg; |
| 244 | struct kvec vec; |
| 245 | int rc = 0; |
| 246 | int len; |
| 247 | |
Ursula Braun | 0cfdd8f | 2017-01-09 16:55:17 +0100 | [diff] [blame] | 248 | link = &conn->lgr->lnk[SMC_SINGLE_LINK]; |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 249 | memset(&aclc, 0, sizeof(aclc)); |
| 250 | memcpy(aclc.hdr.eyecatcher, SMC_EYECATCHER, sizeof(SMC_EYECATCHER)); |
| 251 | aclc.hdr.type = SMC_CLC_ACCEPT; |
| 252 | aclc.hdr.length = htons(sizeof(aclc)); |
| 253 | aclc.hdr.version = SMC_CLC_V1; /* SMC version */ |
Ursula Braun | 0cfdd8f | 2017-01-09 16:55:17 +0100 | [diff] [blame] | 254 | if (srv_first_contact) |
| 255 | aclc.hdr.flag = 1; |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 256 | memcpy(aclc.lcl.id_for_peer, local_systemid, sizeof(local_systemid)); |
Ursula Braun | 0cfdd8f | 2017-01-09 16:55:17 +0100 | [diff] [blame] | 257 | memcpy(&aclc.lcl.gid, &link->smcibdev->gid[link->ibport - 1], |
| 258 | SMC_GID_SIZE); |
Ursula Braun | 143c017 | 2017-01-12 14:57:15 +0100 | [diff] [blame] | 259 | memcpy(&aclc.lcl.mac, link->smcibdev->mac[link->ibport - 1], ETH_ALEN); |
Ursula Braun | 0cfdd8f | 2017-01-09 16:55:17 +0100 | [diff] [blame] | 260 | hton24(aclc.qpn, link->roce_qp->qp_num); |
Ursula Braun | bd4ad57 | 2017-01-09 16:55:20 +0100 | [diff] [blame] | 261 | aclc.rmb_rkey = |
Ursula Braun | 897e1c2 | 2017-07-28 13:56:16 +0200 | [diff] [blame] | 262 | htonl(conn->rmb_desc->mr_rx[SMC_SINGLE_LINK]->rkey); |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 263 | aclc.conn_idx = 1; /* as long as 1 RMB = 1 RMBE */ |
Ursula Braun | 0cfdd8f | 2017-01-09 16:55:17 +0100 | [diff] [blame] | 264 | aclc.rmbe_alert_token = htonl(conn->alert_token_local); |
| 265 | aclc.qp_mtu = link->path_mtu; |
Ursula Braun | cd6851f | 2017-01-09 16:55:18 +0100 | [diff] [blame] | 266 | aclc.rmbe_size = conn->rmbe_size_short, |
Ursula Braun | a3fe3d0 | 2017-07-28 13:56:15 +0200 | [diff] [blame] | 267 | aclc.rmb_dma_addr = cpu_to_be64( |
| 268 | (u64)sg_dma_address(conn->rmb_desc->sgt[SMC_SINGLE_LINK].sgl)); |
Ursula Braun | 0cfdd8f | 2017-01-09 16:55:17 +0100 | [diff] [blame] | 269 | hton24(aclc.psn, link->psn_initial); |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 270 | memcpy(aclc.trl.eyecatcher, SMC_EYECATCHER, sizeof(SMC_EYECATCHER)); |
| 271 | |
| 272 | memset(&msg, 0, sizeof(msg)); |
| 273 | vec.iov_base = &aclc; |
| 274 | vec.iov_len = sizeof(aclc); |
| 275 | len = kernel_sendmsg(new_smc->clcsock, &msg, &vec, 1, sizeof(aclc)); |
| 276 | if (len < sizeof(aclc)) { |
| 277 | if (len >= 0) |
| 278 | new_smc->sk.sk_err = EPROTO; |
| 279 | else |
| 280 | new_smc->sk.sk_err = new_smc->clcsock->sk->sk_err; |
| 281 | rc = sock_error(&new_smc->sk); |
| 282 | } |
| 283 | |
| 284 | return rc; |
| 285 | } |