1 // SPDX-License-Identifier: GPL-2.0
3 * Shared Memory Communications over RDMA (SMC-R) and RoCE
5 * CLC (connection layer control) handshake over initial TCP socket to
6 * prepare for RDMA traffic
8 * Copyright IBM Corp. 2016, 2018
10 * Author(s): Ursula Braun <ubraun@linux.vnet.ibm.com>
14 #include <linux/inetdevice.h>
15 #include <linux/if_ether.h>
16 #include <linux/sched/signal.h>
17 #include <linux/utsname.h>
18 #include <linux/ctype.h>
20 #include <net/addrconf.h>
29 #include "smc_netlink.h"
31 #define SMCR_CLC_ACCEPT_CONFIRM_LEN 68
32 #define SMCD_CLC_ACCEPT_CONFIRM_LEN 48
33 #define SMCD_CLC_ACCEPT_CONFIRM_LEN_V2 78
34 #define SMCR_CLC_ACCEPT_CONFIRM_LEN_V2 108
35 #define SMC_CLC_RECV_BUF_LEN 100
37 /* eye catcher "SMCR" EBCDIC for CLC messages */
38 static const char SMC_EYECATCHER[4] = {'\xe2', '\xd4', '\xc3', '\xd9'};
39 /* eye catcher "SMCD" EBCDIC for CLC messages */
40 static const char SMCD_EYECATCHER[4] = {'\xe2', '\xd4', '\xc3', '\xc4'};
42 static u8 smc_hostname[SMC_MAX_HOSTNAME_LEN];
44 struct smc_clc_eid_table {
46 struct list_head list;
51 static struct smc_clc_eid_table smc_clc_eid_table;
53 struct smc_clc_eid_entry {
54 struct list_head list;
55 u8 eid[SMC_MAX_EID_LEN];
58 /* The size of a user EID is 32 characters.
59 * Valid characters should be (single-byte character set) A-Z, 0-9, '.' and '-'.
60 * Blanks should only be used to pad to the expected size.
61 * First character must be alphanumeric.
63 static bool smc_clc_ueid_valid(char *ueid)
65 char *end = ueid + SMC_MAX_EID_LEN;
67 while (--end >= ueid && isspace(*end))
71 if (!isalnum(*ueid) || islower(*ueid))
74 if ((!isalnum(*ueid) || islower(*ueid)) && *ueid != '.' &&
82 static int smc_clc_ueid_add(char *ueid)
84 struct smc_clc_eid_entry *new_ueid, *tmp_ueid;
87 if (!smc_clc_ueid_valid(ueid))
90 /* add a new ueid entry to the ueid table if there isn't one */
91 new_ueid = kzalloc(sizeof(*new_ueid), GFP_KERNEL);
94 memcpy(new_ueid->eid, ueid, SMC_MAX_EID_LEN);
96 write_lock(&smc_clc_eid_table.lock);
97 if (smc_clc_eid_table.ueid_cnt >= SMC_MAX_UEID) {
101 list_for_each_entry(tmp_ueid, &smc_clc_eid_table.list, list) {
102 if (!memcmp(tmp_ueid->eid, ueid, SMC_MAX_EID_LEN)) {
107 list_add_tail(&new_ueid->list, &smc_clc_eid_table.list);
108 smc_clc_eid_table.ueid_cnt++;
109 write_unlock(&smc_clc_eid_table.lock);
113 write_unlock(&smc_clc_eid_table.lock);
118 int smc_clc_ueid_count(void)
122 read_lock(&smc_clc_eid_table.lock);
123 count = smc_clc_eid_table.ueid_cnt;
124 read_unlock(&smc_clc_eid_table.lock);
129 int smc_nl_add_ueid(struct sk_buff *skb, struct genl_info *info)
131 struct nlattr *nla_ueid = info->attrs[SMC_NLA_EID_TABLE_ENTRY];
134 if (!nla_ueid || nla_len(nla_ueid) != SMC_MAX_EID_LEN + 1)
136 ueid = (char *)nla_data(nla_ueid);
138 return smc_clc_ueid_add(ueid);
141 /* remove one or all ueid entries from the table */
142 static int smc_clc_ueid_remove(char *ueid)
144 struct smc_clc_eid_entry *lst_ueid, *tmp_ueid;
147 /* remove table entry */
148 write_lock(&smc_clc_eid_table.lock);
149 list_for_each_entry_safe(lst_ueid, tmp_ueid, &smc_clc_eid_table.list,
151 if (!ueid || !memcmp(lst_ueid->eid, ueid, SMC_MAX_EID_LEN)) {
152 list_del(&lst_ueid->list);
153 smc_clc_eid_table.ueid_cnt--;
158 if (!rc && !smc_clc_eid_table.ueid_cnt) {
159 smc_clc_eid_table.seid_enabled = 1;
160 rc = -EAGAIN; /* indicate success and enabling of seid */
162 write_unlock(&smc_clc_eid_table.lock);
166 int smc_nl_remove_ueid(struct sk_buff *skb, struct genl_info *info)
168 struct nlattr *nla_ueid = info->attrs[SMC_NLA_EID_TABLE_ENTRY];
171 if (!nla_ueid || nla_len(nla_ueid) != SMC_MAX_EID_LEN + 1)
173 ueid = (char *)nla_data(nla_ueid);
175 return smc_clc_ueid_remove(ueid);
178 int smc_nl_flush_ueid(struct sk_buff *skb, struct genl_info *info)
180 smc_clc_ueid_remove(NULL);
184 static int smc_nl_ueid_dumpinfo(struct sk_buff *skb, u32 portid, u32 seq,
185 u32 flags, char *ueid)
187 char ueid_str[SMC_MAX_EID_LEN + 1];
190 hdr = genlmsg_put(skb, portid, seq, &smc_gen_nl_family,
191 flags, SMC_NETLINK_DUMP_UEID);
194 memcpy(ueid_str, ueid, SMC_MAX_EID_LEN);
195 ueid_str[SMC_MAX_EID_LEN] = 0;
196 if (nla_put_string(skb, SMC_NLA_EID_TABLE_ENTRY, ueid_str)) {
197 genlmsg_cancel(skb, hdr);
200 genlmsg_end(skb, hdr);
204 static int _smc_nl_ueid_dump(struct sk_buff *skb, u32 portid, u32 seq,
207 struct smc_clc_eid_entry *lst_ueid;
210 read_lock(&smc_clc_eid_table.lock);
211 list_for_each_entry(lst_ueid, &smc_clc_eid_table.list, list) {
212 if (idx++ < start_idx)
214 if (smc_nl_ueid_dumpinfo(skb, portid, seq, NLM_F_MULTI,
220 read_unlock(&smc_clc_eid_table.lock);
224 int smc_nl_dump_ueid(struct sk_buff *skb, struct netlink_callback *cb)
226 struct smc_nl_dmp_ctx *cb_ctx = smc_nl_dmp_ctx(cb);
229 idx = _smc_nl_ueid_dump(skb, NETLINK_CB(cb->skb).portid,
230 cb->nlh->nlmsg_seq, cb_ctx->pos[0]);
232 cb_ctx->pos[0] = idx;
236 int smc_nl_dump_seid(struct sk_buff *skb, struct netlink_callback *cb)
238 struct smc_nl_dmp_ctx *cb_ctx = smc_nl_dmp_ctx(cb);
239 char seid_str[SMC_MAX_EID_LEN + 1];
247 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
248 &smc_gen_nl_family, NLM_F_MULTI,
249 SMC_NETLINK_DUMP_SEID);
252 if (!smc_ism_is_v2_capable())
255 smc_ism_get_system_eid(&seid);
256 memcpy(seid_str, seid, SMC_MAX_EID_LEN);
257 seid_str[SMC_MAX_EID_LEN] = 0;
258 if (nla_put_string(skb, SMC_NLA_SEID_ENTRY, seid_str))
260 read_lock(&smc_clc_eid_table.lock);
261 seid_enabled = smc_clc_eid_table.seid_enabled;
262 read_unlock(&smc_clc_eid_table.lock);
263 if (nla_put_u8(skb, SMC_NLA_SEID_ENABLED, seid_enabled))
266 genlmsg_end(skb, hdr);
270 genlmsg_cancel(skb, hdr);
274 int smc_nl_enable_seid(struct sk_buff *skb, struct genl_info *info)
276 write_lock(&smc_clc_eid_table.lock);
277 smc_clc_eid_table.seid_enabled = 1;
278 write_unlock(&smc_clc_eid_table.lock);
282 int smc_nl_disable_seid(struct sk_buff *skb, struct genl_info *info)
286 write_lock(&smc_clc_eid_table.lock);
287 if (!smc_clc_eid_table.ueid_cnt)
290 smc_clc_eid_table.seid_enabled = 0;
291 write_unlock(&smc_clc_eid_table.lock);
295 static bool _smc_clc_match_ueid(u8 *peer_ueid)
297 struct smc_clc_eid_entry *tmp_ueid;
299 list_for_each_entry(tmp_ueid, &smc_clc_eid_table.list, list) {
300 if (!memcmp(tmp_ueid->eid, peer_ueid, SMC_MAX_EID_LEN))
306 bool smc_clc_match_eid(u8 *negotiated_eid,
307 struct smc_clc_v2_extension *smc_v2_ext,
308 u8 *peer_eid, u8 *local_eid)
313 negotiated_eid[0] = 0;
314 read_lock(&smc_clc_eid_table.lock);
315 if (peer_eid && local_eid &&
316 smc_clc_eid_table.seid_enabled &&
317 smc_v2_ext->hdr.flag.seid &&
318 !memcmp(peer_eid, local_eid, SMC_MAX_EID_LEN)) {
319 memcpy(negotiated_eid, peer_eid, SMC_MAX_EID_LEN);
324 for (i = 0; i < smc_v2_ext->hdr.eid_cnt; i++) {
325 if (_smc_clc_match_ueid(smc_v2_ext->user_eids[i])) {
326 memcpy(negotiated_eid, smc_v2_ext->user_eids[i],
333 read_unlock(&smc_clc_eid_table.lock);
337 /* check arriving CLC proposal */
338 static bool smc_clc_msg_prop_valid(struct smc_clc_msg_proposal *pclc)
340 struct smc_clc_msg_proposal_prefix *pclc_prfx;
341 struct smc_clc_smcd_v2_extension *smcd_v2_ext;
342 struct smc_clc_msg_hdr *hdr = &pclc->hdr;
343 struct smc_clc_v2_extension *v2_ext;
345 v2_ext = smc_get_clc_v2_ext(pclc);
346 pclc_prfx = smc_clc_proposal_get_prefix(pclc);
347 if (hdr->version == SMC_V1) {
348 if (hdr->typev1 == SMC_TYPE_N)
350 if (ntohs(hdr->length) !=
351 sizeof(*pclc) + ntohs(pclc->iparea_offset) +
353 pclc_prfx->ipv6_prefixes_cnt *
354 sizeof(struct smc_clc_ipv6_prefix) +
355 sizeof(struct smc_clc_msg_trail))
358 if (ntohs(hdr->length) !=
360 sizeof(struct smc_clc_msg_smcd) +
361 (hdr->typev1 != SMC_TYPE_N ?
363 pclc_prfx->ipv6_prefixes_cnt *
364 sizeof(struct smc_clc_ipv6_prefix) : 0) +
365 (hdr->typev2 != SMC_TYPE_N ?
367 v2_ext->hdr.eid_cnt * SMC_MAX_EID_LEN : 0) +
368 (smcd_indicated(hdr->typev2) ?
369 sizeof(*smcd_v2_ext) + v2_ext->hdr.ism_gid_cnt *
370 sizeof(struct smc_clc_smcd_gid_chid) :
372 sizeof(struct smc_clc_msg_trail))
378 /* check arriving CLC accept or confirm */
380 smc_clc_msg_acc_conf_valid(struct smc_clc_msg_accept_confirm_v2 *clc_v2)
382 struct smc_clc_msg_hdr *hdr = &clc_v2->hdr;
384 if (hdr->typev1 != SMC_TYPE_R && hdr->typev1 != SMC_TYPE_D)
386 if (hdr->version == SMC_V1) {
387 if ((hdr->typev1 == SMC_TYPE_R &&
388 ntohs(hdr->length) != SMCR_CLC_ACCEPT_CONFIRM_LEN) ||
389 (hdr->typev1 == SMC_TYPE_D &&
390 ntohs(hdr->length) != SMCD_CLC_ACCEPT_CONFIRM_LEN))
393 if (hdr->typev1 == SMC_TYPE_D &&
394 ntohs(hdr->length) < SMCD_CLC_ACCEPT_CONFIRM_LEN_V2)
396 if (hdr->typev1 == SMC_TYPE_R &&
397 ntohs(hdr->length) < SMCR_CLC_ACCEPT_CONFIRM_LEN_V2)
403 /* check arriving CLC decline */
405 smc_clc_msg_decl_valid(struct smc_clc_msg_decline *dclc)
407 struct smc_clc_msg_hdr *hdr = &dclc->hdr;
409 if (hdr->typev1 != SMC_TYPE_R && hdr->typev1 != SMC_TYPE_D)
411 if (hdr->version == SMC_V1) {
412 if (ntohs(hdr->length) != sizeof(struct smc_clc_msg_decline))
415 if (ntohs(hdr->length) != sizeof(struct smc_clc_msg_decline_v2))
421 static int smc_clc_fill_fce(struct smc_clc_first_contact_ext_v2x *fce,
422 struct smc_init_info *ini)
424 int ret = sizeof(*fce);
426 memset(fce, 0, sizeof(*fce));
427 fce->fce_v2_base.os_type = SMC_CLC_OS_LINUX;
428 fce->fce_v2_base.release = ini->release_nr;
429 memcpy(fce->fce_v2_base.hostname, smc_hostname, sizeof(smc_hostname));
430 if (ini->is_smcd && ini->release_nr < SMC_RELEASE_1) {
431 ret = sizeof(struct smc_clc_first_contact_ext);
435 if (ini->release_nr >= SMC_RELEASE_1) {
437 fce->max_conns = ini->max_conns;
438 fce->max_links = ini->max_links;
446 /* check if received message has a correct header length and contains valid
447 * heading and trailing eyecatchers
449 static bool smc_clc_msg_hdr_valid(struct smc_clc_msg_hdr *clcm, bool check_trl)
451 struct smc_clc_msg_accept_confirm_v2 *clc_v2;
452 struct smc_clc_msg_proposal *pclc;
453 struct smc_clc_msg_decline *dclc;
454 struct smc_clc_msg_trail *trl;
456 if (memcmp(clcm->eyecatcher, SMC_EYECATCHER, sizeof(SMC_EYECATCHER)) &&
457 memcmp(clcm->eyecatcher, SMCD_EYECATCHER, sizeof(SMCD_EYECATCHER)))
459 switch (clcm->type) {
460 case SMC_CLC_PROPOSAL:
461 pclc = (struct smc_clc_msg_proposal *)clcm;
462 if (!smc_clc_msg_prop_valid(pclc))
464 trl = (struct smc_clc_msg_trail *)
465 ((u8 *)pclc + ntohs(pclc->hdr.length) - sizeof(*trl));
468 case SMC_CLC_CONFIRM:
469 clc_v2 = (struct smc_clc_msg_accept_confirm_v2 *)clcm;
470 if (!smc_clc_msg_acc_conf_valid(clc_v2))
472 trl = (struct smc_clc_msg_trail *)
473 ((u8 *)clc_v2 + ntohs(clc_v2->hdr.length) -
476 case SMC_CLC_DECLINE:
477 dclc = (struct smc_clc_msg_decline *)clcm;
478 if (!smc_clc_msg_decl_valid(dclc))
486 memcmp(trl->eyecatcher, SMC_EYECATCHER, sizeof(SMC_EYECATCHER)) &&
487 memcmp(trl->eyecatcher, SMCD_EYECATCHER, sizeof(SMCD_EYECATCHER)))
492 /* find ipv4 addr on device and get the prefix len, fill CLC proposal msg */
493 static int smc_clc_prfx_set4_rcu(struct dst_entry *dst, __be32 ipv4,
494 struct smc_clc_msg_proposal_prefix *prop)
496 struct in_device *in_dev = __in_dev_get_rcu(dst->dev);
497 const struct in_ifaddr *ifa;
502 in_dev_for_each_ifa_rcu(ifa, in_dev) {
503 if (!inet_ifa_match(ipv4, ifa))
505 prop->prefix_len = inet_mask_len(ifa->ifa_mask);
506 prop->outgoing_subnet = ifa->ifa_address & ifa->ifa_mask;
507 /* prop->ipv6_prefixes_cnt = 0; already done by memset before */
513 /* fill CLC proposal msg with ipv6 prefixes from device */
514 static int smc_clc_prfx_set6_rcu(struct dst_entry *dst,
515 struct smc_clc_msg_proposal_prefix *prop,
516 struct smc_clc_ipv6_prefix *ipv6_prfx)
518 #if IS_ENABLED(CONFIG_IPV6)
519 struct inet6_dev *in6_dev = __in6_dev_get(dst->dev);
520 struct inet6_ifaddr *ifa;
525 /* use a maximum of 8 IPv6 prefixes from device */
526 list_for_each_entry(ifa, &in6_dev->addr_list, if_list) {
527 if (ipv6_addr_type(&ifa->addr) & IPV6_ADDR_LINKLOCAL)
529 ipv6_addr_prefix(&ipv6_prfx[cnt].prefix,
530 &ifa->addr, ifa->prefix_len);
531 ipv6_prfx[cnt].prefix_len = ifa->prefix_len;
533 if (cnt == SMC_CLC_MAX_V6_PREFIX)
536 prop->ipv6_prefixes_cnt = cnt;
543 /* retrieve and set prefixes in CLC proposal msg */
544 static int smc_clc_prfx_set(struct socket *clcsock,
545 struct smc_clc_msg_proposal_prefix *prop,
546 struct smc_clc_ipv6_prefix *ipv6_prfx)
548 struct dst_entry *dst = sk_dst_get(clcsock->sk);
549 struct sockaddr_storage addrs;
550 struct sockaddr_in6 *addr6;
551 struct sockaddr_in *addr;
562 /* get address to which the internal TCP socket is bound */
563 if (kernel_getsockname(clcsock, (struct sockaddr *)&addrs) < 0)
565 /* analyze IP specific data of net_device belonging to TCP socket */
566 addr6 = (struct sockaddr_in6 *)&addrs;
568 if (addrs.ss_family == PF_INET) {
570 addr = (struct sockaddr_in *)&addrs;
571 rc = smc_clc_prfx_set4_rcu(dst, addr->sin_addr.s_addr, prop);
572 } else if (ipv6_addr_v4mapped(&addr6->sin6_addr)) {
573 /* mapped IPv4 address - peer is IPv4 only */
574 rc = smc_clc_prfx_set4_rcu(dst, addr6->sin6_addr.s6_addr32[3],
578 rc = smc_clc_prfx_set6_rcu(dst, prop, ipv6_prfx);
587 /* match ipv4 addrs of dev against addr in CLC proposal */
588 static int smc_clc_prfx_match4_rcu(struct net_device *dev,
589 struct smc_clc_msg_proposal_prefix *prop)
591 struct in_device *in_dev = __in_dev_get_rcu(dev);
592 const struct in_ifaddr *ifa;
596 in_dev_for_each_ifa_rcu(ifa, in_dev) {
597 if (prop->prefix_len == inet_mask_len(ifa->ifa_mask) &&
598 inet_ifa_match(prop->outgoing_subnet, ifa))
605 /* match ipv6 addrs of dev against addrs in CLC proposal */
606 static int smc_clc_prfx_match6_rcu(struct net_device *dev,
607 struct smc_clc_msg_proposal_prefix *prop)
609 #if IS_ENABLED(CONFIG_IPV6)
610 struct inet6_dev *in6_dev = __in6_dev_get(dev);
611 struct smc_clc_ipv6_prefix *ipv6_prfx;
612 struct inet6_ifaddr *ifa;
617 /* ipv6 prefix list starts behind smc_clc_msg_proposal_prefix */
618 ipv6_prfx = (struct smc_clc_ipv6_prefix *)((u8 *)prop + sizeof(*prop));
619 max = min_t(u8, prop->ipv6_prefixes_cnt, SMC_CLC_MAX_V6_PREFIX);
620 list_for_each_entry(ifa, &in6_dev->addr_list, if_list) {
621 if (ipv6_addr_type(&ifa->addr) & IPV6_ADDR_LINKLOCAL)
623 for (i = 0; i < max; i++) {
624 if (ifa->prefix_len == ipv6_prfx[i].prefix_len &&
625 ipv6_prefix_equal(&ifa->addr, &ipv6_prfx[i].prefix,
634 /* check if proposed prefixes match one of our device prefixes */
635 int smc_clc_prfx_match(struct socket *clcsock,
636 struct smc_clc_msg_proposal_prefix *prop)
638 struct dst_entry *dst = sk_dst_get(clcsock->sk);
650 if (!prop->ipv6_prefixes_cnt)
651 rc = smc_clc_prfx_match4_rcu(dst->dev, prop);
653 rc = smc_clc_prfx_match6_rcu(dst->dev, prop);
661 /* Wait for data on the tcp-socket, analyze received data
663 * 0 if success and it was not a decline that we received.
664 * SMC_CLC_DECL_REPLY if decline received for fallback w/o another decl send.
665 * clcsock error, -EINTR, -ECONNRESET, -EPROTO otherwise.
667 int smc_clc_wait_msg(struct smc_sock *smc, void *buf, int buflen,
668 u8 expected_type, unsigned long timeout)
670 long rcvtimeo = smc->clcsock->sk->sk_rcvtimeo;
671 struct sock *clc_sk = smc->clcsock->sk;
672 struct smc_clc_msg_hdr *clcm = buf;
673 struct msghdr msg = {NULL, 0};
675 struct kvec vec = {buf, buflen};
676 int len, datlen, recvlen;
677 bool check_trl = true;
680 /* peek the first few bytes to determine length of data to receive
681 * so we don't consume any subsequent CLC message or payload data
682 * in the TCP byte stream
685 * Caller must make sure that buflen is no less than
686 * sizeof(struct smc_clc_msg_hdr)
688 krflags = MSG_PEEK | MSG_WAITALL;
689 clc_sk->sk_rcvtimeo = timeout;
690 iov_iter_kvec(&msg.msg_iter, ITER_DEST, &vec, 1,
691 sizeof(struct smc_clc_msg_hdr));
692 len = sock_recvmsg(smc->clcsock, &msg, krflags);
693 if (signal_pending(current)) {
694 reason_code = -EINTR;
695 clc_sk->sk_err = EINTR;
696 smc->sk.sk_err = EINTR;
699 if (clc_sk->sk_err) {
700 reason_code = -clc_sk->sk_err;
701 if (clc_sk->sk_err == EAGAIN &&
702 expected_type == SMC_CLC_DECLINE)
703 clc_sk->sk_err = 0; /* reset for fallback usage */
705 smc->sk.sk_err = clc_sk->sk_err;
708 if (!len) { /* peer has performed orderly shutdown */
709 smc->sk.sk_err = ECONNRESET;
710 reason_code = -ECONNRESET;
714 if (len != -EAGAIN || expected_type != SMC_CLC_DECLINE)
715 smc->sk.sk_err = -len;
719 datlen = ntohs(clcm->length);
720 if ((len < sizeof(struct smc_clc_msg_hdr)) ||
721 (clcm->version < SMC_V1) ||
722 ((clcm->type != SMC_CLC_DECLINE) &&
723 (clcm->type != expected_type))) {
724 smc->sk.sk_err = EPROTO;
725 reason_code = -EPROTO;
729 /* receive the complete CLC message */
730 memset(&msg, 0, sizeof(struct msghdr));
731 if (datlen > buflen) {
737 iov_iter_kvec(&msg.msg_iter, ITER_DEST, &vec, 1, recvlen);
738 krflags = MSG_WAITALL;
739 len = sock_recvmsg(smc->clcsock, &msg, krflags);
740 if (len < recvlen || !smc_clc_msg_hdr_valid(clcm, check_trl)) {
741 smc->sk.sk_err = EPROTO;
742 reason_code = -EPROTO;
747 u8 tmp[SMC_CLC_RECV_BUF_LEN];
750 vec.iov_len = SMC_CLC_RECV_BUF_LEN;
751 /* receive remaining proposal message */
752 recvlen = datlen > SMC_CLC_RECV_BUF_LEN ?
753 SMC_CLC_RECV_BUF_LEN : datlen;
754 iov_iter_kvec(&msg.msg_iter, ITER_DEST, &vec, 1, recvlen);
755 len = sock_recvmsg(smc->clcsock, &msg, krflags);
758 if (clcm->type == SMC_CLC_DECLINE) {
759 struct smc_clc_msg_decline *dclc;
761 dclc = (struct smc_clc_msg_decline *)clcm;
762 reason_code = SMC_CLC_DECL_PEERDECL;
763 smc->peer_diagnosis = ntohl(dclc->peer_diagnosis);
764 if (((struct smc_clc_msg_decline *)buf)->hdr.typev2 &
765 SMC_FIRST_CONTACT_MASK) {
766 smc->conn.lgr->sync_err = 1;
767 smc_lgr_terminate_sched(smc->conn.lgr);
772 clc_sk->sk_rcvtimeo = rcvtimeo;
776 /* send CLC DECLINE message across internal TCP socket */
777 int smc_clc_send_decline(struct smc_sock *smc, u32 peer_diag_info, u8 version)
779 struct smc_clc_msg_decline *dclc_v1;
780 struct smc_clc_msg_decline_v2 dclc;
785 dclc_v1 = (struct smc_clc_msg_decline *)&dclc;
786 memset(&dclc, 0, sizeof(dclc));
787 memcpy(dclc.hdr.eyecatcher, SMC_EYECATCHER, sizeof(SMC_EYECATCHER));
788 dclc.hdr.type = SMC_CLC_DECLINE;
789 dclc.hdr.version = version;
790 dclc.os_type = version == SMC_V1 ? 0 : SMC_CLC_OS_LINUX;
791 dclc.hdr.typev2 = (peer_diag_info == SMC_CLC_DECL_SYNCERR) ?
792 SMC_FIRST_CONTACT_MASK : 0;
793 if ((!smc_conn_lgr_valid(&smc->conn) || !smc->conn.lgr->is_smcd) &&
794 smc_ib_is_valid_local_systemid())
795 memcpy(dclc.id_for_peer, local_systemid,
796 sizeof(local_systemid));
797 dclc.peer_diagnosis = htonl(peer_diag_info);
798 if (version == SMC_V1) {
799 memcpy(dclc_v1->trl.eyecatcher, SMC_EYECATCHER,
800 sizeof(SMC_EYECATCHER));
801 send_len = sizeof(*dclc_v1);
803 memcpy(dclc.trl.eyecatcher, SMC_EYECATCHER,
804 sizeof(SMC_EYECATCHER));
805 send_len = sizeof(dclc);
807 dclc.hdr.length = htons(send_len);
809 memset(&msg, 0, sizeof(msg));
810 vec.iov_base = &dclc;
811 vec.iov_len = send_len;
812 len = kernel_sendmsg(smc->clcsock, &msg, &vec, 1, send_len);
813 if (len < 0 || len < send_len)
815 return len > 0 ? 0 : len;
818 /* send CLC PROPOSAL message across internal TCP socket */
819 int smc_clc_send_proposal(struct smc_sock *smc, struct smc_init_info *ini)
821 struct smc_clc_smcd_v2_extension *smcd_v2_ext;
822 struct smc_clc_msg_proposal_prefix *pclc_prfx;
823 struct smc_clc_msg_proposal *pclc_base;
824 struct smc_clc_smcd_gid_chid *gidchids;
825 struct smc_clc_msg_proposal_area *pclc;
826 struct smc_clc_ipv6_prefix *ipv6_prfx;
827 struct smc_clc_v2_extension *v2_ext;
828 struct smc_clc_msg_smcd *pclc_smcd;
829 struct smc_clc_msg_trail *trl;
830 struct smcd_dev *smcd;
831 int len, i, plen, rc;
836 pclc = kzalloc(sizeof(*pclc), GFP_KERNEL);
840 pclc_base = &pclc->pclc_base;
841 pclc_smcd = &pclc->pclc_smcd;
842 pclc_prfx = &pclc->pclc_prfx;
843 ipv6_prfx = pclc->pclc_prfx_ipv6;
844 v2_ext = &pclc->pclc_v2_ext;
845 smcd_v2_ext = &pclc->pclc_smcd_v2_ext;
846 gidchids = pclc->pclc_gidchids;
847 trl = &pclc->pclc_trl;
849 pclc_base->hdr.version = SMC_V2;
850 pclc_base->hdr.typev1 = ini->smc_type_v1;
851 pclc_base->hdr.typev2 = ini->smc_type_v2;
852 plen = sizeof(*pclc_base) + sizeof(*pclc_smcd) + sizeof(*trl);
854 /* retrieve ip prefixes for CLC proposal msg */
855 if (ini->smc_type_v1 != SMC_TYPE_N) {
856 rc = smc_clc_prfx_set(smc->clcsock, pclc_prfx, ipv6_prfx);
858 if (ini->smc_type_v2 == SMC_TYPE_N) {
860 return SMC_CLC_DECL_CNFERR;
862 pclc_base->hdr.typev1 = SMC_TYPE_N;
864 pclc_base->iparea_offset = htons(sizeof(*pclc_smcd));
865 plen += sizeof(*pclc_prfx) +
866 pclc_prfx->ipv6_prefixes_cnt *
867 sizeof(ipv6_prfx[0]);
871 /* build SMC Proposal CLC message */
872 memcpy(pclc_base->hdr.eyecatcher, SMC_EYECATCHER,
873 sizeof(SMC_EYECATCHER));
874 pclc_base->hdr.type = SMC_CLC_PROPOSAL;
875 if (smcr_indicated(ini->smc_type_v1)) {
876 /* add SMC-R specifics */
877 memcpy(pclc_base->lcl.id_for_peer, local_systemid,
878 sizeof(local_systemid));
879 memcpy(pclc_base->lcl.gid, ini->ib_gid, SMC_GID_SIZE);
880 memcpy(pclc_base->lcl.mac, &ini->ib_dev->mac[ini->ib_port - 1],
883 if (smcd_indicated(ini->smc_type_v1)) {
884 /* add SMC-D specifics */
885 if (ini->ism_dev[0]) {
886 smcd = ini->ism_dev[0];
888 htonll(smcd->ops->get_local_gid(smcd));
889 pclc_smcd->ism.chid =
890 htons(smc_ism_get_chid(ini->ism_dev[0]));
893 if (ini->smc_type_v2 == SMC_TYPE_N) {
894 pclc_smcd->v2_ext_offset = 0;
896 struct smc_clc_eid_entry *ueident;
899 v2_ext->hdr.flag.release = SMC_RELEASE;
900 v2_ext_offset = sizeof(*pclc_smcd) -
901 offsetofend(struct smc_clc_msg_smcd, v2_ext_offset);
902 if (ini->smc_type_v1 != SMC_TYPE_N)
903 v2_ext_offset += sizeof(*pclc_prfx) +
904 pclc_prfx->ipv6_prefixes_cnt *
905 sizeof(ipv6_prfx[0]);
906 pclc_smcd->v2_ext_offset = htons(v2_ext_offset);
907 plen += sizeof(*v2_ext);
909 read_lock(&smc_clc_eid_table.lock);
910 v2_ext->hdr.eid_cnt = smc_clc_eid_table.ueid_cnt;
911 plen += smc_clc_eid_table.ueid_cnt * SMC_MAX_EID_LEN;
913 list_for_each_entry(ueident, &smc_clc_eid_table.list, list) {
914 memcpy(v2_ext->user_eids[i++], ueident->eid,
915 sizeof(ueident->eid));
917 read_unlock(&smc_clc_eid_table.lock);
919 if (smcd_indicated(ini->smc_type_v2)) {
922 v2_ext->hdr.flag.seid = smc_clc_eid_table.seid_enabled;
923 v2_ext->hdr.ism_gid_cnt = ini->ism_offered_cnt;
924 v2_ext->hdr.smcd_v2_ext_offset = htons(sizeof(*v2_ext) -
925 offsetofend(struct smc_clnt_opts_area_hdr,
926 smcd_v2_ext_offset) +
927 v2_ext->hdr.eid_cnt * SMC_MAX_EID_LEN);
928 smc_ism_get_system_eid(&eid);
929 if (eid && v2_ext->hdr.flag.seid)
930 memcpy(smcd_v2_ext->system_eid, eid, SMC_MAX_EID_LEN);
931 plen += sizeof(*smcd_v2_ext);
932 if (ini->ism_offered_cnt) {
933 for (i = 1; i <= ini->ism_offered_cnt; i++) {
934 smcd = ini->ism_dev[i];
935 gidchids[i - 1].gid =
936 htonll(smcd->ops->get_local_gid(smcd));
937 gidchids[i - 1].chid =
938 htons(smc_ism_get_chid(ini->ism_dev[i]));
940 plen += ini->ism_offered_cnt *
941 sizeof(struct smc_clc_smcd_gid_chid);
944 if (smcr_indicated(ini->smc_type_v2)) {
945 memcpy(v2_ext->roce, ini->smcrv2.ib_gid_v2, SMC_GID_SIZE);
946 v2_ext->max_conns = SMC_CONN_PER_LGR_PREFER;
947 v2_ext->max_links = SMC_LINKS_PER_LGR_MAX_PREFER;
950 pclc_base->hdr.length = htons(plen);
951 memcpy(trl->eyecatcher, SMC_EYECATCHER, sizeof(SMC_EYECATCHER));
953 /* send SMC Proposal CLC message */
954 memset(&msg, 0, sizeof(msg));
956 vec[i].iov_base = pclc_base;
957 vec[i++].iov_len = sizeof(*pclc_base);
958 vec[i].iov_base = pclc_smcd;
959 vec[i++].iov_len = sizeof(*pclc_smcd);
960 if (ini->smc_type_v1 != SMC_TYPE_N) {
961 vec[i].iov_base = pclc_prfx;
962 vec[i++].iov_len = sizeof(*pclc_prfx);
963 if (pclc_prfx->ipv6_prefixes_cnt > 0) {
964 vec[i].iov_base = ipv6_prfx;
965 vec[i++].iov_len = pclc_prfx->ipv6_prefixes_cnt *
966 sizeof(ipv6_prfx[0]);
969 if (ini->smc_type_v2 != SMC_TYPE_N) {
970 vec[i].iov_base = v2_ext;
971 vec[i++].iov_len = sizeof(*v2_ext) +
972 (v2_ext->hdr.eid_cnt * SMC_MAX_EID_LEN);
973 if (smcd_indicated(ini->smc_type_v2)) {
974 vec[i].iov_base = smcd_v2_ext;
975 vec[i++].iov_len = sizeof(*smcd_v2_ext);
976 if (ini->ism_offered_cnt) {
977 vec[i].iov_base = gidchids;
978 vec[i++].iov_len = ini->ism_offered_cnt *
979 sizeof(struct smc_clc_smcd_gid_chid);
983 vec[i].iov_base = trl;
984 vec[i++].iov_len = sizeof(*trl);
985 /* due to the few bytes needed for clc-handshake this cannot block */
986 len = kernel_sendmsg(smc->clcsock, &msg, vec, i, plen);
988 smc->sk.sk_err = smc->clcsock->sk->sk_err;
989 reason_code = -smc->sk.sk_err;
990 } else if (len < ntohs(pclc_base->hdr.length)) {
991 reason_code = -ENETUNREACH;
992 smc->sk.sk_err = -reason_code;
999 /* build and send CLC CONFIRM / ACCEPT message */
1000 static int smc_clc_send_confirm_accept(struct smc_sock *smc,
1001 struct smc_clc_msg_accept_confirm_v2 *clc_v2,
1002 int first_contact, u8 version,
1003 u8 *eid, struct smc_init_info *ini)
1005 struct smc_connection *conn = &smc->conn;
1006 struct smc_clc_first_contact_ext_v2x fce;
1007 struct smc_clc_msg_accept_confirm *clc;
1008 struct smc_clc_fce_gid_ext gle;
1009 struct smc_clc_msg_trail trl;
1010 int i, len, fce_len;
1014 /* send SMC Confirm CLC msg */
1015 clc = (struct smc_clc_msg_accept_confirm *)clc_v2;
1016 clc->hdr.version = version; /* SMC version */
1018 clc->hdr.typev2 |= SMC_FIRST_CONTACT_MASK;
1019 if (conn->lgr->is_smcd) {
1020 /* SMC-D specific settings */
1021 memcpy(clc->hdr.eyecatcher, SMCD_EYECATCHER,
1022 sizeof(SMCD_EYECATCHER));
1023 clc->hdr.typev1 = SMC_TYPE_D;
1025 conn->lgr->smcd->ops->get_local_gid(conn->lgr->smcd);
1026 clc->d0.token = conn->rmb_desc->token;
1027 clc->d0.dmbe_size = conn->rmbe_size_comp;
1028 clc->d0.dmbe_idx = 0;
1029 memcpy(&clc->d0.linkid, conn->lgr->id, SMC_LGR_ID_SIZE);
1030 if (version == SMC_V1) {
1031 clc->hdr.length = htons(SMCD_CLC_ACCEPT_CONFIRM_LEN);
1034 htons(smc_ism_get_chid(conn->lgr->smcd));
1036 memcpy(clc_v2->d1.eid, eid, SMC_MAX_EID_LEN);
1037 len = SMCD_CLC_ACCEPT_CONFIRM_LEN_V2;
1038 if (first_contact) {
1039 fce_len = smc_clc_fill_fce(&fce, ini);
1042 clc_v2->hdr.length = htons(len);
1044 memcpy(trl.eyecatcher, SMCD_EYECATCHER,
1045 sizeof(SMCD_EYECATCHER));
1047 struct smc_link *link = conn->lnk;
1049 /* SMC-R specific settings */
1050 memcpy(clc->hdr.eyecatcher, SMC_EYECATCHER,
1051 sizeof(SMC_EYECATCHER));
1052 clc->hdr.typev1 = SMC_TYPE_R;
1053 clc->hdr.length = htons(SMCR_CLC_ACCEPT_CONFIRM_LEN);
1054 memcpy(clc->r0.lcl.id_for_peer, local_systemid,
1055 sizeof(local_systemid));
1056 memcpy(&clc->r0.lcl.gid, link->gid, SMC_GID_SIZE);
1057 memcpy(&clc->r0.lcl.mac, &link->smcibdev->mac[link->ibport - 1],
1059 hton24(clc->r0.qpn, link->roce_qp->qp_num);
1061 htonl(conn->rmb_desc->mr[link->link_idx]->rkey);
1062 clc->r0.rmbe_idx = 1; /* for now: 1 RMB = 1 RMBE */
1063 clc->r0.rmbe_alert_token = htonl(conn->alert_token_local);
1064 switch (clc->hdr.type) {
1065 case SMC_CLC_ACCEPT:
1066 clc->r0.qp_mtu = link->path_mtu;
1068 case SMC_CLC_CONFIRM:
1069 clc->r0.qp_mtu = min(link->path_mtu, link->peer_mtu);
1072 clc->r0.rmbe_size = conn->rmbe_size_comp;
1073 clc->r0.rmb_dma_addr = conn->rmb_desc->is_vm ?
1074 cpu_to_be64((uintptr_t)conn->rmb_desc->cpu_addr) :
1075 cpu_to_be64((u64)sg_dma_address
1076 (conn->rmb_desc->sgt[link->link_idx].sgl));
1077 hton24(clc->r0.psn, link->psn_initial);
1078 if (version == SMC_V1) {
1079 clc->hdr.length = htons(SMCR_CLC_ACCEPT_CONFIRM_LEN);
1082 memcpy(clc_v2->r1.eid, eid, SMC_MAX_EID_LEN);
1083 len = SMCR_CLC_ACCEPT_CONFIRM_LEN_V2;
1084 if (first_contact) {
1085 fce_len = smc_clc_fill_fce(&fce, ini);
1087 fce.fce_v2_base.v2_direct = !link->lgr->uses_gateway;
1088 if (clc->hdr.type == SMC_CLC_CONFIRM) {
1089 memset(&gle, 0, sizeof(gle));
1090 gle.gid_cnt = ini->smcrv2.gidlist.len;
1092 len += gle.gid_cnt * sizeof(gle.gid[0]);
1095 clc_v2->hdr.length = htons(len);
1097 memcpy(trl.eyecatcher, SMC_EYECATCHER, sizeof(SMC_EYECATCHER));
1100 memset(&msg, 0, sizeof(msg));
1102 vec[i].iov_base = clc_v2;
1103 if (version > SMC_V1)
1104 vec[i++].iov_len = (clc->hdr.typev1 == SMC_TYPE_D ?
1105 SMCD_CLC_ACCEPT_CONFIRM_LEN_V2 :
1106 SMCR_CLC_ACCEPT_CONFIRM_LEN_V2) -
1109 vec[i++].iov_len = (clc->hdr.typev1 == SMC_TYPE_D ?
1110 SMCD_CLC_ACCEPT_CONFIRM_LEN :
1111 SMCR_CLC_ACCEPT_CONFIRM_LEN) -
1113 if (version > SMC_V1 && first_contact) {
1114 vec[i].iov_base = &fce;
1115 vec[i++].iov_len = fce_len;
1116 if (!conn->lgr->is_smcd) {
1117 if (clc->hdr.type == SMC_CLC_CONFIRM) {
1118 vec[i].iov_base = &gle;
1119 vec[i++].iov_len = sizeof(gle);
1120 vec[i].iov_base = &ini->smcrv2.gidlist.list;
1121 vec[i++].iov_len = gle.gid_cnt *
1126 vec[i].iov_base = &trl;
1127 vec[i++].iov_len = sizeof(trl);
1128 return kernel_sendmsg(smc->clcsock, &msg, vec, 1,
1129 ntohs(clc->hdr.length));
1132 /* send CLC CONFIRM message across internal TCP socket */
1133 int smc_clc_send_confirm(struct smc_sock *smc, bool clnt_first_contact,
1134 u8 version, u8 *eid, struct smc_init_info *ini)
1136 struct smc_clc_msg_accept_confirm_v2 cclc_v2;
1137 int reason_code = 0;
1140 /* send SMC Confirm CLC msg */
1141 memset(&cclc_v2, 0, sizeof(cclc_v2));
1142 cclc_v2.hdr.type = SMC_CLC_CONFIRM;
1143 len = smc_clc_send_confirm_accept(smc, &cclc_v2, clnt_first_contact,
1145 if (len < ntohs(cclc_v2.hdr.length)) {
1147 reason_code = -ENETUNREACH;
1148 smc->sk.sk_err = -reason_code;
1150 smc->sk.sk_err = smc->clcsock->sk->sk_err;
1151 reason_code = -smc->sk.sk_err;
1157 /* send CLC ACCEPT message across internal TCP socket */
1158 int smc_clc_send_accept(struct smc_sock *new_smc, bool srv_first_contact,
1159 u8 version, u8 *negotiated_eid, struct smc_init_info *ini)
1161 struct smc_clc_msg_accept_confirm_v2 aclc_v2;
1164 memset(&aclc_v2, 0, sizeof(aclc_v2));
1165 aclc_v2.hdr.type = SMC_CLC_ACCEPT;
1166 len = smc_clc_send_confirm_accept(new_smc, &aclc_v2, srv_first_contact,
1167 version, negotiated_eid, ini);
1168 if (len < ntohs(aclc_v2.hdr.length))
1169 len = len >= 0 ? -EPROTO : -new_smc->clcsock->sk->sk_err;
1171 return len > 0 ? 0 : len;
1174 int smc_clc_srv_v2x_features_validate(struct smc_clc_msg_proposal *pclc,
1175 struct smc_init_info *ini)
1177 struct smc_clc_v2_extension *pclc_v2_ext;
1179 ini->max_conns = SMC_CONN_PER_LGR_MAX;
1180 ini->max_links = SMC_LINKS_ADD_LNK_MAX;
1182 if ((!(ini->smcd_version & SMC_V2) && !(ini->smcr_version & SMC_V2)) ||
1183 ini->release_nr < SMC_RELEASE_1)
1186 pclc_v2_ext = smc_get_clc_v2_ext(pclc);
1188 return SMC_CLC_DECL_NOV2EXT;
1190 if (ini->smcr_version & SMC_V2) {
1191 ini->max_conns = min_t(u8, pclc_v2_ext->max_conns, SMC_CONN_PER_LGR_PREFER);
1192 if (ini->max_conns < SMC_CONN_PER_LGR_MIN)
1193 return SMC_CLC_DECL_MAXCONNERR;
1195 ini->max_links = min_t(u8, pclc_v2_ext->max_links, SMC_LINKS_PER_LGR_MAX_PREFER);
1196 if (ini->max_links < SMC_LINKS_ADD_LNK_MIN)
1197 return SMC_CLC_DECL_MAXLINKERR;
1203 int smc_clc_clnt_v2x_features_validate(struct smc_clc_first_contact_ext *fce,
1204 struct smc_init_info *ini)
1206 struct smc_clc_first_contact_ext_v2x *fce_v2x =
1207 (struct smc_clc_first_contact_ext_v2x *)fce;
1209 if (ini->release_nr < SMC_RELEASE_1)
1212 if (!ini->is_smcd) {
1213 if (fce_v2x->max_conns < SMC_CONN_PER_LGR_MIN)
1214 return SMC_CLC_DECL_MAXCONNERR;
1215 ini->max_conns = fce_v2x->max_conns;
1217 if (fce_v2x->max_links > SMC_LINKS_ADD_LNK_MAX ||
1218 fce_v2x->max_links < SMC_LINKS_ADD_LNK_MIN)
1219 return SMC_CLC_DECL_MAXLINKERR;
1220 ini->max_links = fce_v2x->max_links;
1226 int smc_clc_v2x_features_confirm_check(struct smc_clc_msg_accept_confirm *cclc,
1227 struct smc_init_info *ini)
1229 struct smc_clc_msg_accept_confirm_v2 *clc_v2 =
1230 (struct smc_clc_msg_accept_confirm_v2 *)cclc;
1231 struct smc_clc_first_contact_ext *fce =
1232 smc_get_clc_first_contact_ext(clc_v2, ini->is_smcd);
1233 struct smc_clc_first_contact_ext_v2x *fce_v2x =
1234 (struct smc_clc_first_contact_ext_v2x *)fce;
1236 if (cclc->hdr.version == SMC_V1 ||
1237 !(cclc->hdr.typev2 & SMC_FIRST_CONTACT_MASK))
1240 if (ini->release_nr != fce->release)
1241 return SMC_CLC_DECL_RELEASEERR;
1243 if (fce->release < SMC_RELEASE_1)
1246 if (!ini->is_smcd) {
1247 if (fce_v2x->max_conns != ini->max_conns)
1248 return SMC_CLC_DECL_MAXCONNERR;
1249 if (fce_v2x->max_links != ini->max_links)
1250 return SMC_CLC_DECL_MAXLINKERR;
1256 void smc_clc_get_hostname(u8 **host)
1258 *host = &smc_hostname[0];
1261 void __init smc_clc_init(void)
1263 struct new_utsname *u;
1265 memset(smc_hostname, _S, sizeof(smc_hostname)); /* ASCII blanks */
1267 memcpy(smc_hostname, u->nodename,
1268 min_t(size_t, strlen(u->nodename), sizeof(smc_hostname)));
1270 INIT_LIST_HEAD(&smc_clc_eid_table.list);
1271 rwlock_init(&smc_clc_eid_table.lock);
1272 smc_clc_eid_table.ueid_cnt = 0;
1273 smc_clc_eid_table.seid_enabled = 1;
1276 void smc_clc_exit(void)
1278 smc_clc_ueid_remove(NULL);