blob: abf7ceb6690b13dd4e7cf7ebea7b0bfb80b6fb5a [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
Ursula Braune7b7a642017-12-07 13:38:49 +010025/* check if received message has a correct header length and contains valid
26 * heading and trailing eyecatchers
27 */
28static bool smc_clc_msg_hdr_valid(struct smc_clc_msg_hdr *clcm)
29{
30 struct smc_clc_msg_proposal_prefix *pclc_prfx;
31 struct smc_clc_msg_accept_confirm *clc;
32 struct smc_clc_msg_proposal *pclc;
33 struct smc_clc_msg_decline *dclc;
34 struct smc_clc_msg_trail *trl;
35
36 if (memcmp(clcm->eyecatcher, SMC_EYECATCHER, sizeof(SMC_EYECATCHER)))
37 return false;
38 switch (clcm->type) {
39 case SMC_CLC_PROPOSAL:
40 pclc = (struct smc_clc_msg_proposal *)clcm;
41 pclc_prfx = smc_clc_proposal_get_prefix(pclc);
42 if (ntohs(pclc->hdr.length) !=
43 sizeof(*pclc) + ntohs(pclc->iparea_offset) +
44 sizeof(*pclc_prfx) +
45 pclc_prfx->ipv6_prefixes_cnt *
46 sizeof(struct smc_clc_ipv6_prefix) +
47 sizeof(*trl))
48 return false;
49 trl = (struct smc_clc_msg_trail *)
50 ((u8 *)pclc + ntohs(pclc->hdr.length) - sizeof(*trl));
51 break;
52 case SMC_CLC_ACCEPT:
53 case SMC_CLC_CONFIRM:
54 clc = (struct smc_clc_msg_accept_confirm *)clcm;
55 if (ntohs(clc->hdr.length) != sizeof(*clc))
56 return false;
57 trl = &clc->trl;
58 break;
59 case SMC_CLC_DECLINE:
60 dclc = (struct smc_clc_msg_decline *)clcm;
61 if (ntohs(dclc->hdr.length) != sizeof(*dclc))
62 return false;
63 trl = &dclc->trl;
64 break;
65 default:
66 return false;
67 }
68 if (memcmp(trl->eyecatcher, SMC_EYECATCHER, sizeof(SMC_EYECATCHER)))
69 return false;
70 return true;
71}
72
Ursula Brauna046d572017-01-09 16:55:16 +010073/* Wait for data on the tcp-socket, analyze received data
74 * Returns:
75 * 0 if success and it was not a decline that we received.
76 * SMC_CLC_DECL_REPLY if decline received for fallback w/o another decl send.
77 * clcsock error, -EINTR, -ECONNRESET, -EPROTO otherwise.
78 */
79int smc_clc_wait_msg(struct smc_sock *smc, void *buf, int buflen,
80 u8 expected_type)
81{
82 struct sock *clc_sk = smc->clcsock->sk;
83 struct smc_clc_msg_hdr *clcm = buf;
84 struct msghdr msg = {NULL, 0};
85 int reason_code = 0;
86 struct kvec vec;
87 int len, datlen;
88 int krflags;
89
90 /* peek the first few bytes to determine length of data to receive
91 * so we don't consume any subsequent CLC message or payload data
92 * in the TCP byte stream
93 */
94 vec.iov_base = buf;
95 vec.iov_len = buflen;
96 krflags = MSG_PEEK | MSG_WAITALL;
97 smc->clcsock->sk->sk_rcvtimeo = CLC_WAIT_TIME;
98 len = kernel_recvmsg(smc->clcsock, &msg, &vec, 1,
99 sizeof(struct smc_clc_msg_hdr), krflags);
100 if (signal_pending(current)) {
101 reason_code = -EINTR;
102 clc_sk->sk_err = EINTR;
103 smc->sk.sk_err = EINTR;
104 goto out;
105 }
106 if (clc_sk->sk_err) {
107 reason_code = -clc_sk->sk_err;
108 smc->sk.sk_err = clc_sk->sk_err;
109 goto out;
110 }
111 if (!len) { /* peer has performed orderly shutdown */
112 smc->sk.sk_err = ECONNRESET;
113 reason_code = -ECONNRESET;
114 goto out;
115 }
116 if (len < 0) {
117 smc->sk.sk_err = -len;
118 reason_code = len;
119 goto out;
120 }
121 datlen = ntohs(clcm->length);
122 if ((len < sizeof(struct smc_clc_msg_hdr)) ||
Ursula Braune7b7a642017-12-07 13:38:49 +0100123 (datlen > buflen) ||
Ursula Brauna046d572017-01-09 16:55:16 +0100124 ((clcm->type != SMC_CLC_DECLINE) &&
125 (clcm->type != expected_type))) {
126 smc->sk.sk_err = EPROTO;
127 reason_code = -EPROTO;
128 goto out;
129 }
130
131 /* receive the complete CLC message */
132 vec.iov_base = buf;
133 vec.iov_len = buflen;
134 memset(&msg, 0, sizeof(struct msghdr));
135 krflags = MSG_WAITALL;
136 smc->clcsock->sk->sk_rcvtimeo = CLC_WAIT_TIME;
137 len = kernel_recvmsg(smc->clcsock, &msg, &vec, 1, datlen, krflags);
Ursula Braune7b7a642017-12-07 13:38:49 +0100138 if (len < datlen || !smc_clc_msg_hdr_valid(clcm)) {
Ursula Brauna046d572017-01-09 16:55:16 +0100139 smc->sk.sk_err = EPROTO;
140 reason_code = -EPROTO;
141 goto out;
142 }
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100143 if (clcm->type == SMC_CLC_DECLINE) {
Ursula Brauna046d572017-01-09 16:55:16 +0100144 reason_code = SMC_CLC_DECL_REPLY;
Ursula Braunbfbedfd2017-09-21 09:16:32 +0200145 if (((struct smc_clc_msg_decline *)buf)->hdr.flag) {
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100146 smc->conn.lgr->sync_err = true;
Ursula Braunbfbedfd2017-09-21 09:16:32 +0200147 smc_lgr_terminate(smc->conn.lgr);
148 }
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100149 }
150
Ursula Brauna046d572017-01-09 16:55:16 +0100151out:
152 return reason_code;
153}
154
155/* send CLC DECLINE message across internal TCP socket */
Ursula Braunbfbedfd2017-09-21 09:16:32 +0200156int smc_clc_send_decline(struct smc_sock *smc, u32 peer_diag_info)
Ursula Brauna046d572017-01-09 16:55:16 +0100157{
158 struct smc_clc_msg_decline dclc;
159 struct msghdr msg;
160 struct kvec vec;
161 int len;
162
163 memset(&dclc, 0, sizeof(dclc));
164 memcpy(dclc.hdr.eyecatcher, SMC_EYECATCHER, sizeof(SMC_EYECATCHER));
165 dclc.hdr.type = SMC_CLC_DECLINE;
166 dclc.hdr.length = htons(sizeof(struct smc_clc_msg_decline));
167 dclc.hdr.version = SMC_CLC_V1;
Ursula Braunbfbedfd2017-09-21 09:16:32 +0200168 dclc.hdr.flag = (peer_diag_info == SMC_CLC_DECL_SYNCERR) ? 1 : 0;
Ursula Brauna046d572017-01-09 16:55:16 +0100169 memcpy(dclc.id_for_peer, local_systemid, sizeof(local_systemid));
170 dclc.peer_diagnosis = htonl(peer_diag_info);
171 memcpy(dclc.trl.eyecatcher, SMC_EYECATCHER, sizeof(SMC_EYECATCHER));
172
173 memset(&msg, 0, sizeof(msg));
174 vec.iov_base = &dclc;
175 vec.iov_len = sizeof(struct smc_clc_msg_decline);
176 len = kernel_sendmsg(smc->clcsock, &msg, &vec, 1,
177 sizeof(struct smc_clc_msg_decline));
178 if (len < sizeof(struct smc_clc_msg_decline))
179 smc->sk.sk_err = EPROTO;
180 if (len < 0)
181 smc->sk.sk_err = -len;
Ursula Braun0c9f1512017-12-07 13:38:45 +0100182 return sock_error(&smc->sk);
Ursula Brauna046d572017-01-09 16:55:16 +0100183}
184
185/* send CLC PROPOSAL message across internal TCP socket */
186int smc_clc_send_proposal(struct smc_sock *smc,
187 struct smc_ib_device *smcibdev,
188 u8 ibport)
189{
Ursula Braune7b7a642017-12-07 13:38:49 +0100190 struct smc_clc_msg_proposal_prefix pclc_prfx;
Ursula Brauna046d572017-01-09 16:55:16 +0100191 struct smc_clc_msg_proposal pclc;
Ursula Braune7b7a642017-12-07 13:38:49 +0100192 struct smc_clc_msg_trail trl;
Ursula Brauna046d572017-01-09 16:55:16 +0100193 int reason_code = 0;
Ursula Braune7b7a642017-12-07 13:38:49 +0100194 struct kvec vec[3];
Ursula Brauna046d572017-01-09 16:55:16 +0100195 struct msghdr msg;
Ursula Braune7b7a642017-12-07 13:38:49 +0100196 int len, plen, rc;
Ursula Brauna046d572017-01-09 16:55:16 +0100197
198 /* send SMC Proposal CLC message */
Ursula Braune7b7a642017-12-07 13:38:49 +0100199 plen = sizeof(pclc) + sizeof(pclc_prfx) + sizeof(trl);
Ursula Brauna046d572017-01-09 16:55:16 +0100200 memset(&pclc, 0, sizeof(pclc));
201 memcpy(pclc.hdr.eyecatcher, SMC_EYECATCHER, sizeof(SMC_EYECATCHER));
202 pclc.hdr.type = SMC_CLC_PROPOSAL;
Ursula Braune7b7a642017-12-07 13:38:49 +0100203 pclc.hdr.length = htons(plen);
Ursula Brauna046d572017-01-09 16:55:16 +0100204 pclc.hdr.version = SMC_CLC_V1; /* SMC version */
205 memcpy(pclc.lcl.id_for_peer, local_systemid, sizeof(local_systemid));
206 memcpy(&pclc.lcl.gid, &smcibdev->gid[ibport - 1], SMC_GID_SIZE);
Ursula Braun143c0172017-01-12 14:57:15 +0100207 memcpy(&pclc.lcl.mac, &smcibdev->mac[ibport - 1], ETH_ALEN);
Ursula Braune7b7a642017-12-07 13:38:49 +0100208 pclc.iparea_offset = htons(0);
Ursula Brauna046d572017-01-09 16:55:16 +0100209
Ursula Braune7b7a642017-12-07 13:38:49 +0100210 memset(&pclc_prfx, 0, sizeof(pclc_prfx));
Ursula Brauna046d572017-01-09 16:55:16 +0100211 /* determine subnet and mask from internal TCP socket */
Ursula Braune7b7a642017-12-07 13:38:49 +0100212 rc = smc_netinfo_by_tcpsk(smc->clcsock, &pclc_prfx.outgoing_subnet,
213 &pclc_prfx.prefix_len);
Ursula Brauna046d572017-01-09 16:55:16 +0100214 if (rc)
215 return SMC_CLC_DECL_CNFERR; /* configuration error */
Ursula Braune7b7a642017-12-07 13:38:49 +0100216 pclc_prfx.ipv6_prefixes_cnt = 0;
217 memcpy(trl.eyecatcher, SMC_EYECATCHER, sizeof(SMC_EYECATCHER));
Ursula Brauna046d572017-01-09 16:55:16 +0100218 memset(&msg, 0, sizeof(msg));
Ursula Braune7b7a642017-12-07 13:38:49 +0100219 vec[0].iov_base = &pclc;
220 vec[0].iov_len = sizeof(pclc);
221 vec[1].iov_base = &pclc_prfx;
222 vec[1].iov_len = sizeof(pclc_prfx);
223 vec[2].iov_base = &trl;
224 vec[2].iov_len = sizeof(trl);
Ursula Brauna046d572017-01-09 16:55:16 +0100225 /* due to the few bytes needed for clc-handshake this cannot block */
Ursula Braune7b7a642017-12-07 13:38:49 +0100226 len = kernel_sendmsg(smc->clcsock, &msg, vec, 3, plen);
Ursula Brauna046d572017-01-09 16:55:16 +0100227 if (len < sizeof(pclc)) {
228 if (len >= 0) {
229 reason_code = -ENETUNREACH;
230 smc->sk.sk_err = -reason_code;
231 } else {
232 smc->sk.sk_err = smc->clcsock->sk->sk_err;
233 reason_code = -smc->sk.sk_err;
234 }
235 }
236
237 return reason_code;
238}
239
240/* send CLC CONFIRM message across internal TCP socket */
241int smc_clc_send_confirm(struct smc_sock *smc)
242{
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100243 struct smc_connection *conn = &smc->conn;
Ursula Brauna046d572017-01-09 16:55:16 +0100244 struct smc_clc_msg_accept_confirm cclc;
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100245 struct smc_link *link;
Ursula Brauna046d572017-01-09 16:55:16 +0100246 int reason_code = 0;
247 struct msghdr msg;
248 struct kvec vec;
249 int len;
250
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100251 link = &conn->lgr->lnk[SMC_SINGLE_LINK];
Ursula Brauna046d572017-01-09 16:55:16 +0100252 /* send SMC Confirm CLC msg */
253 memset(&cclc, 0, sizeof(cclc));
254 memcpy(cclc.hdr.eyecatcher, SMC_EYECATCHER, sizeof(SMC_EYECATCHER));
255 cclc.hdr.type = SMC_CLC_CONFIRM;
256 cclc.hdr.length = htons(sizeof(cclc));
257 cclc.hdr.version = SMC_CLC_V1; /* SMC version */
258 memcpy(cclc.lcl.id_for_peer, local_systemid, sizeof(local_systemid));
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100259 memcpy(&cclc.lcl.gid, &link->smcibdev->gid[link->ibport - 1],
260 SMC_GID_SIZE);
Ursula Braun143c0172017-01-12 14:57:15 +0100261 memcpy(&cclc.lcl.mac, &link->smcibdev->mac[link->ibport - 1], ETH_ALEN);
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100262 hton24(cclc.qpn, link->roce_qp->qp_num);
Ursula Braunbd4ad572017-01-09 16:55:20 +0100263 cclc.rmb_rkey =
Ursula Braun897e1c22017-07-28 13:56:16 +0200264 htonl(conn->rmb_desc->mr_rx[SMC_SINGLE_LINK]->rkey);
Ursula Brauna046d572017-01-09 16:55:16 +0100265 cclc.conn_idx = 1; /* for now: 1 RMB = 1 RMBE */
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100266 cclc.rmbe_alert_token = htonl(conn->alert_token_local);
267 cclc.qp_mtu = min(link->path_mtu, link->peer_mtu);
Ursula Braunbd4ad572017-01-09 16:55:20 +0100268 cclc.rmbe_size = conn->rmbe_size_short;
Ursula Brauna3fe3d02017-07-28 13:56:15 +0200269 cclc.rmb_dma_addr = cpu_to_be64(
270 (u64)sg_dma_address(conn->rmb_desc->sgt[SMC_SINGLE_LINK].sgl));
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100271 hton24(cclc.psn, link->psn_initial);
Ursula Brauna046d572017-01-09 16:55:16 +0100272
273 memcpy(cclc.trl.eyecatcher, SMC_EYECATCHER, sizeof(SMC_EYECATCHER));
274
275 memset(&msg, 0, sizeof(msg));
276 vec.iov_base = &cclc;
277 vec.iov_len = sizeof(cclc);
278 len = kernel_sendmsg(smc->clcsock, &msg, &vec, 1, sizeof(cclc));
279 if (len < sizeof(cclc)) {
280 if (len >= 0) {
281 reason_code = -ENETUNREACH;
282 smc->sk.sk_err = -reason_code;
283 } else {
284 smc->sk.sk_err = smc->clcsock->sk->sk_err;
285 reason_code = -smc->sk.sk_err;
286 }
287 }
288 return reason_code;
289}
290
291/* send CLC ACCEPT message across internal TCP socket */
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100292int smc_clc_send_accept(struct smc_sock *new_smc, int srv_first_contact)
Ursula Brauna046d572017-01-09 16:55:16 +0100293{
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100294 struct smc_connection *conn = &new_smc->conn;
Ursula Brauna046d572017-01-09 16:55:16 +0100295 struct smc_clc_msg_accept_confirm aclc;
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100296 struct smc_link *link;
Ursula Brauna046d572017-01-09 16:55:16 +0100297 struct msghdr msg;
298 struct kvec vec;
299 int rc = 0;
300 int len;
301
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100302 link = &conn->lgr->lnk[SMC_SINGLE_LINK];
Ursula Brauna046d572017-01-09 16:55:16 +0100303 memset(&aclc, 0, sizeof(aclc));
304 memcpy(aclc.hdr.eyecatcher, SMC_EYECATCHER, sizeof(SMC_EYECATCHER));
305 aclc.hdr.type = SMC_CLC_ACCEPT;
306 aclc.hdr.length = htons(sizeof(aclc));
307 aclc.hdr.version = SMC_CLC_V1; /* SMC version */
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100308 if (srv_first_contact)
309 aclc.hdr.flag = 1;
Ursula Brauna046d572017-01-09 16:55:16 +0100310 memcpy(aclc.lcl.id_for_peer, local_systemid, sizeof(local_systemid));
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100311 memcpy(&aclc.lcl.gid, &link->smcibdev->gid[link->ibport - 1],
312 SMC_GID_SIZE);
Ursula Braun143c0172017-01-12 14:57:15 +0100313 memcpy(&aclc.lcl.mac, link->smcibdev->mac[link->ibport - 1], ETH_ALEN);
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100314 hton24(aclc.qpn, link->roce_qp->qp_num);
Ursula Braunbd4ad572017-01-09 16:55:20 +0100315 aclc.rmb_rkey =
Ursula Braun897e1c22017-07-28 13:56:16 +0200316 htonl(conn->rmb_desc->mr_rx[SMC_SINGLE_LINK]->rkey);
Ursula Brauna046d572017-01-09 16:55:16 +0100317 aclc.conn_idx = 1; /* as long as 1 RMB = 1 RMBE */
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100318 aclc.rmbe_alert_token = htonl(conn->alert_token_local);
319 aclc.qp_mtu = link->path_mtu;
Ursula Brauncd6851f2017-01-09 16:55:18 +0100320 aclc.rmbe_size = conn->rmbe_size_short,
Ursula Brauna3fe3d02017-07-28 13:56:15 +0200321 aclc.rmb_dma_addr = cpu_to_be64(
322 (u64)sg_dma_address(conn->rmb_desc->sgt[SMC_SINGLE_LINK].sgl));
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100323 hton24(aclc.psn, link->psn_initial);
Ursula Brauna046d572017-01-09 16:55:16 +0100324 memcpy(aclc.trl.eyecatcher, SMC_EYECATCHER, sizeof(SMC_EYECATCHER));
325
326 memset(&msg, 0, sizeof(msg));
327 vec.iov_base = &aclc;
328 vec.iov_len = sizeof(aclc);
329 len = kernel_sendmsg(new_smc->clcsock, &msg, &vec, 1, sizeof(aclc));
330 if (len < sizeof(aclc)) {
331 if (len >= 0)
332 new_smc->sk.sk_err = EPROTO;
333 else
334 new_smc->sk.sk_err = new_smc->clcsock->sk->sk_err;
335 rc = sock_error(&new_smc->sk);
336 }
337
338 return rc;
339}