blob: 511548085d168bcb5ec334627442cb077ce1033c [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Ursula Brauna046d572017-01-09 16:55:16 +01002/*
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 Braun143c0172017-01-12 14:57:15 +010014#include <linux/if_ether.h>
Ingo Molnarc3edc402017-02-02 08:35:14 +010015#include <linux/sched/signal.h>
16
Ursula Brauna046d572017-01-09 16:55:16 +010017#include <net/sock.h>
18#include <net/tcp.h>
19
20#include "smc.h"
Ursula Braun0cfdd8f2017-01-09 16:55:17 +010021#include "smc_core.h"
Ursula Brauna046d572017-01-09 16:55:16 +010022#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 */
31int 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 Virod63d2712017-09-20 20:21:22 -040038 struct kvec vec = {buf, buflen};
Ursula Brauna046d572017-01-09 16:55:16 +010039 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 Virod63d2712017-09-20 20:21:22 -040046 /*
47 * Caller must make sure that buflen is no less than
48 * sizeof(struct smc_clc_msg_hdr)
49 */
Ursula Brauna046d572017-01-09 16:55:16 +010050 krflags = MSG_PEEK | MSG_WAITALL;
51 smc->clcsock->sk->sk_rcvtimeo = CLC_WAIT_TIME;
Al Virod63d2712017-09-20 20:21:22 -040052 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 Brauna046d572017-01-09 16:55:16 +010055 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 Brauna046d572017-01-09 16:55:16 +010089 memset(&msg, 0, sizeof(struct msghdr));
Al Virod63d2712017-09-20 20:21:22 -040090 iov_iter_kvec(&msg.msg_iter, READ | ITER_KVEC, &vec, 1, buflen);
Ursula Brauna046d572017-01-09 16:55:16 +010091 krflags = MSG_WAITALL;
92 smc->clcsock->sk->sk_rcvtimeo = CLC_WAIT_TIME;
Al Virod63d2712017-09-20 20:21:22 -040093 len = sock_recvmsg(smc->clcsock, &msg, krflags);
Ursula Brauna046d572017-01-09 16:55:16 +010094 if (len < datlen) {
95 smc->sk.sk_err = EPROTO;
96 reason_code = -EPROTO;
97 goto out;
98 }
Ursula Braun0cfdd8f2017-01-09 16:55:17 +010099 if (clcm->type == SMC_CLC_DECLINE) {
Ursula Brauna046d572017-01-09 16:55:16 +0100100 reason_code = SMC_CLC_DECL_REPLY;
Ursula Braunbfbedfd2017-09-21 09:16:32 +0200101 if (((struct smc_clc_msg_decline *)buf)->hdr.flag) {
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100102 smc->conn.lgr->sync_err = true;
Ursula Braunbfbedfd2017-09-21 09:16:32 +0200103 smc_lgr_terminate(smc->conn.lgr);
104 }
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100105 }
106
Ursula Brauna046d572017-01-09 16:55:16 +0100107out:
108 return reason_code;
109}
110
111/* send CLC DECLINE message across internal TCP socket */
Ursula Braunbfbedfd2017-09-21 09:16:32 +0200112int smc_clc_send_decline(struct smc_sock *smc, u32 peer_diag_info)
Ursula Brauna046d572017-01-09 16:55:16 +0100113{
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 Braunbfbedfd2017-09-21 09:16:32 +0200124 dclc.hdr.flag = (peer_diag_info == SMC_CLC_DECL_SYNCERR) ? 1 : 0;
Ursula Brauna046d572017-01-09 16:55:16 +0100125 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 */
142int 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 Braun143c0172017-01-12 14:57:15 +0100160 memcpy(&pclc.lcl.mac, &smcibdev->mac[ibport - 1], ETH_ALEN);
Ursula Brauna046d572017-01-09 16:55:16 +0100161
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 */
187int smc_clc_send_confirm(struct smc_sock *smc)
188{
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100189 struct smc_connection *conn = &smc->conn;
Ursula Brauna046d572017-01-09 16:55:16 +0100190 struct smc_clc_msg_accept_confirm cclc;
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100191 struct smc_link *link;
Ursula Brauna046d572017-01-09 16:55:16 +0100192 int reason_code = 0;
193 struct msghdr msg;
194 struct kvec vec;
195 int len;
196
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100197 link = &conn->lgr->lnk[SMC_SINGLE_LINK];
Ursula Brauna046d572017-01-09 16:55:16 +0100198 /* 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 Braun0cfdd8f2017-01-09 16:55:17 +0100205 memcpy(&cclc.lcl.gid, &link->smcibdev->gid[link->ibport - 1],
206 SMC_GID_SIZE);
Ursula Braun143c0172017-01-12 14:57:15 +0100207 memcpy(&cclc.lcl.mac, &link->smcibdev->mac[link->ibport - 1], ETH_ALEN);
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100208 hton24(cclc.qpn, link->roce_qp->qp_num);
Ursula Braunbd4ad572017-01-09 16:55:20 +0100209 cclc.rmb_rkey =
Ursula Braun897e1c22017-07-28 13:56:16 +0200210 htonl(conn->rmb_desc->mr_rx[SMC_SINGLE_LINK]->rkey);
Ursula Brauna046d572017-01-09 16:55:16 +0100211 cclc.conn_idx = 1; /* for now: 1 RMB = 1 RMBE */
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100212 cclc.rmbe_alert_token = htonl(conn->alert_token_local);
213 cclc.qp_mtu = min(link->path_mtu, link->peer_mtu);
Ursula Braunbd4ad572017-01-09 16:55:20 +0100214 cclc.rmbe_size = conn->rmbe_size_short;
Ursula Brauna3fe3d02017-07-28 13:56:15 +0200215 cclc.rmb_dma_addr = cpu_to_be64(
216 (u64)sg_dma_address(conn->rmb_desc->sgt[SMC_SINGLE_LINK].sgl));
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100217 hton24(cclc.psn, link->psn_initial);
Ursula Brauna046d572017-01-09 16:55:16 +0100218
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 Braun0cfdd8f2017-01-09 16:55:17 +0100238int smc_clc_send_accept(struct smc_sock *new_smc, int srv_first_contact)
Ursula Brauna046d572017-01-09 16:55:16 +0100239{
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100240 struct smc_connection *conn = &new_smc->conn;
Ursula Brauna046d572017-01-09 16:55:16 +0100241 struct smc_clc_msg_accept_confirm aclc;
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100242 struct smc_link *link;
Ursula Brauna046d572017-01-09 16:55:16 +0100243 struct msghdr msg;
244 struct kvec vec;
245 int rc = 0;
246 int len;
247
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100248 link = &conn->lgr->lnk[SMC_SINGLE_LINK];
Ursula Brauna046d572017-01-09 16:55:16 +0100249 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 Braun0cfdd8f2017-01-09 16:55:17 +0100254 if (srv_first_contact)
255 aclc.hdr.flag = 1;
Ursula Brauna046d572017-01-09 16:55:16 +0100256 memcpy(aclc.lcl.id_for_peer, local_systemid, sizeof(local_systemid));
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100257 memcpy(&aclc.lcl.gid, &link->smcibdev->gid[link->ibport - 1],
258 SMC_GID_SIZE);
Ursula Braun143c0172017-01-12 14:57:15 +0100259 memcpy(&aclc.lcl.mac, link->smcibdev->mac[link->ibport - 1], ETH_ALEN);
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100260 hton24(aclc.qpn, link->roce_qp->qp_num);
Ursula Braunbd4ad572017-01-09 16:55:20 +0100261 aclc.rmb_rkey =
Ursula Braun897e1c22017-07-28 13:56:16 +0200262 htonl(conn->rmb_desc->mr_rx[SMC_SINGLE_LINK]->rkey);
Ursula Brauna046d572017-01-09 16:55:16 +0100263 aclc.conn_idx = 1; /* as long as 1 RMB = 1 RMBE */
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100264 aclc.rmbe_alert_token = htonl(conn->alert_token_local);
265 aclc.qp_mtu = link->path_mtu;
Ursula Brauncd6851f2017-01-09 16:55:18 +0100266 aclc.rmbe_size = conn->rmbe_size_short,
Ursula Brauna3fe3d02017-07-28 13:56:15 +0200267 aclc.rmb_dma_addr = cpu_to_be64(
268 (u64)sg_dma_address(conn->rmb_desc->sgt[SMC_SINGLE_LINK].sgl));
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100269 hton24(aclc.psn, link->psn_initial);
Ursula Brauna046d572017-01-09 16:55:16 +0100270 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}