2 * Copyright (c) 2005 Voltaire Inc. All rights reserved.
3 * Copyright (c) 2002-2005, Network Appliance, Inc. All rights reserved.
4 * Copyright (c) 1999-2005, Mellanox Technologies, Inc. All rights reserved.
5 * Copyright (c) 2005-2006 Intel Corporation. All rights reserved.
7 * This software is available to you under a choice of one of two
8 * licenses. You may choose to be licensed under the terms of the GNU
9 * General Public License (GPL) Version 2, available from the file
10 * COPYING in the main directory of this source tree, or the
11 * OpenIB.org BSD license below:
13 * Redistribution and use in source and binary forms, with or
14 * without modification, are permitted provided that the following
17 * - Redistributions of source code must retain the above
18 * copyright notice, this list of conditions and the following
21 * - Redistributions in binary form must reproduce the above
22 * copyright notice, this list of conditions and the following
23 * disclaimer in the documentation and/or other materials
24 * provided with the distribution.
26 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
30 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
31 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
32 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
36 #include <linux/completion.h>
38 #include <linux/in6.h>
39 #include <linux/mutex.h>
40 #include <linux/random.h>
41 #include <linux/igmp.h>
42 #include <linux/idr.h>
43 #include <linux/inetdevice.h>
44 #include <linux/slab.h>
45 #include <linux/module.h>
46 #include <net/route.h>
48 #include <net/net_namespace.h>
49 #include <net/netns/generic.h>
52 #include <net/ip_fib.h>
53 #include <net/ip6_route.h>
55 #include <rdma/rdma_cm.h>
56 #include <rdma/rdma_cm_ib.h>
57 #include <rdma/rdma_netlink.h>
59 #include <rdma/ib_cache.h>
60 #include <rdma/ib_cm.h>
61 #include <rdma/ib_sa.h>
62 #include <rdma/iw_cm.h>
64 #include "core_priv.h"
67 MODULE_AUTHOR("Sean Hefty");
68 MODULE_DESCRIPTION("Generic RDMA CM Agent");
69 MODULE_LICENSE("Dual BSD/GPL");
71 #define CMA_CM_RESPONSE_TIMEOUT 20
72 #define CMA_QUERY_CLASSPORT_INFO_TIMEOUT 3000
73 #define CMA_MAX_CM_RETRIES 15
74 #define CMA_CM_MRA_SETTING (IB_CM_MRA_FLAG_DELAY | 24)
75 #define CMA_IBOE_PACKET_LIFETIME 18
76 #define CMA_PREFERRED_ROCE_GID_TYPE IB_GID_TYPE_ROCE_UDP_ENCAP
78 static const char * const cma_events
[] = {
79 [RDMA_CM_EVENT_ADDR_RESOLVED
] = "address resolved",
80 [RDMA_CM_EVENT_ADDR_ERROR
] = "address error",
81 [RDMA_CM_EVENT_ROUTE_RESOLVED
] = "route resolved ",
82 [RDMA_CM_EVENT_ROUTE_ERROR
] = "route error",
83 [RDMA_CM_EVENT_CONNECT_REQUEST
] = "connect request",
84 [RDMA_CM_EVENT_CONNECT_RESPONSE
] = "connect response",
85 [RDMA_CM_EVENT_CONNECT_ERROR
] = "connect error",
86 [RDMA_CM_EVENT_UNREACHABLE
] = "unreachable",
87 [RDMA_CM_EVENT_REJECTED
] = "rejected",
88 [RDMA_CM_EVENT_ESTABLISHED
] = "established",
89 [RDMA_CM_EVENT_DISCONNECTED
] = "disconnected",
90 [RDMA_CM_EVENT_DEVICE_REMOVAL
] = "device removal",
91 [RDMA_CM_EVENT_MULTICAST_JOIN
] = "multicast join",
92 [RDMA_CM_EVENT_MULTICAST_ERROR
] = "multicast error",
93 [RDMA_CM_EVENT_ADDR_CHANGE
] = "address change",
94 [RDMA_CM_EVENT_TIMEWAIT_EXIT
] = "timewait exit",
97 const char *__attribute_const__
rdma_event_msg(enum rdma_cm_event_type event
)
101 return (index
< ARRAY_SIZE(cma_events
) && cma_events
[index
]) ?
102 cma_events
[index
] : "unrecognized event";
104 EXPORT_SYMBOL(rdma_event_msg
);
106 const char *__attribute_const__
rdma_reject_msg(struct rdma_cm_id
*id
,
109 if (rdma_ib_or_roce(id
->device
, id
->port_num
))
110 return ibcm_reject_msg(reason
);
112 if (rdma_protocol_iwarp(id
->device
, id
->port_num
))
113 return iwcm_reject_msg(reason
);
116 return "unrecognized transport";
118 EXPORT_SYMBOL(rdma_reject_msg
);
120 bool rdma_is_consumer_reject(struct rdma_cm_id
*id
, int reason
)
122 if (rdma_ib_or_roce(id
->device
, id
->port_num
))
123 return reason
== IB_CM_REJ_CONSUMER_DEFINED
;
125 if (rdma_protocol_iwarp(id
->device
, id
->port_num
))
126 return reason
== -ECONNREFUSED
;
131 EXPORT_SYMBOL(rdma_is_consumer_reject
);
133 const void *rdma_consumer_reject_data(struct rdma_cm_id
*id
,
134 struct rdma_cm_event
*ev
, u8
*data_len
)
138 if (rdma_is_consumer_reject(id
, ev
->status
)) {
139 *data_len
= ev
->param
.conn
.private_data_len
;
140 p
= ev
->param
.conn
.private_data
;
147 EXPORT_SYMBOL(rdma_consumer_reject_data
);
150 * rdma_iw_cm_id() - return the iw_cm_id pointer for this cm_id.
151 * @id: Communication Identifier
153 struct iw_cm_id
*rdma_iw_cm_id(struct rdma_cm_id
*id
)
155 struct rdma_id_private
*id_priv
;
157 id_priv
= container_of(id
, struct rdma_id_private
, id
);
158 if (id
->device
->node_type
== RDMA_NODE_RNIC
)
159 return id_priv
->cm_id
.iw
;
162 EXPORT_SYMBOL(rdma_iw_cm_id
);
165 * rdma_res_to_id() - return the rdma_cm_id pointer for this restrack.
166 * @res: rdma resource tracking entry pointer
168 struct rdma_cm_id
*rdma_res_to_id(struct rdma_restrack_entry
*res
)
170 struct rdma_id_private
*id_priv
=
171 container_of(res
, struct rdma_id_private
, res
);
175 EXPORT_SYMBOL(rdma_res_to_id
);
177 static void cma_add_one(struct ib_device
*device
);
178 static void cma_remove_one(struct ib_device
*device
, void *client_data
);
180 static struct ib_client cma_client
= {
183 .remove
= cma_remove_one
186 static struct ib_sa_client sa_client
;
187 static LIST_HEAD(dev_list
);
188 static LIST_HEAD(listen_any_list
);
189 static DEFINE_MUTEX(lock
);
190 static struct workqueue_struct
*cma_wq
;
191 static unsigned int cma_pernet_id
;
200 static struct cma_pernet
*cma_pernet(struct net
*net
)
202 return net_generic(net
, cma_pernet_id
);
205 static struct idr
*cma_pernet_idr(struct net
*net
, enum rdma_ucm_port_space ps
)
207 struct cma_pernet
*pernet
= cma_pernet(net
);
211 return &pernet
->tcp_ps
;
213 return &pernet
->udp_ps
;
215 return &pernet
->ipoib_ps
;
217 return &pernet
->ib_ps
;
224 struct list_head list
;
225 struct ib_device
*device
;
226 struct completion comp
;
228 struct list_head id_list
;
229 enum ib_gid_type
*default_gid_type
;
230 u8
*default_roce_tos
;
233 struct rdma_bind_list
{
234 enum rdma_ucm_port_space ps
;
235 struct hlist_head owners
;
239 struct class_port_info_context
{
240 struct ib_class_port_info
*class_port_info
;
241 struct ib_device
*device
;
242 struct completion done
;
243 struct ib_sa_query
*sa_query
;
247 static int cma_ps_alloc(struct net
*net
, enum rdma_ucm_port_space ps
,
248 struct rdma_bind_list
*bind_list
, int snum
)
250 struct idr
*idr
= cma_pernet_idr(net
, ps
);
252 return idr_alloc(idr
, bind_list
, snum
, snum
+ 1, GFP_KERNEL
);
255 static struct rdma_bind_list
*cma_ps_find(struct net
*net
,
256 enum rdma_ucm_port_space ps
, int snum
)
258 struct idr
*idr
= cma_pernet_idr(net
, ps
);
260 return idr_find(idr
, snum
);
263 static void cma_ps_remove(struct net
*net
, enum rdma_ucm_port_space ps
,
266 struct idr
*idr
= cma_pernet_idr(net
, ps
);
268 idr_remove(idr
, snum
);
275 void cma_ref_dev(struct cma_device
*cma_dev
)
277 atomic_inc(&cma_dev
->refcount
);
280 struct cma_device
*cma_enum_devices_by_ibdev(cma_device_filter filter
,
283 struct cma_device
*cma_dev
;
284 struct cma_device
*found_cma_dev
= NULL
;
288 list_for_each_entry(cma_dev
, &dev_list
, list
)
289 if (filter(cma_dev
->device
, cookie
)) {
290 found_cma_dev
= cma_dev
;
295 cma_ref_dev(found_cma_dev
);
297 return found_cma_dev
;
300 int cma_get_default_gid_type(struct cma_device
*cma_dev
,
303 if (!rdma_is_port_valid(cma_dev
->device
, port
))
306 return cma_dev
->default_gid_type
[port
- rdma_start_port(cma_dev
->device
)];
309 int cma_set_default_gid_type(struct cma_device
*cma_dev
,
311 enum ib_gid_type default_gid_type
)
313 unsigned long supported_gids
;
315 if (!rdma_is_port_valid(cma_dev
->device
, port
))
318 supported_gids
= roce_gid_type_mask_support(cma_dev
->device
, port
);
320 if (!(supported_gids
& 1 << default_gid_type
))
323 cma_dev
->default_gid_type
[port
- rdma_start_port(cma_dev
->device
)] =
329 int cma_get_default_roce_tos(struct cma_device
*cma_dev
, unsigned int port
)
331 if (!rdma_is_port_valid(cma_dev
->device
, port
))
334 return cma_dev
->default_roce_tos
[port
- rdma_start_port(cma_dev
->device
)];
337 int cma_set_default_roce_tos(struct cma_device
*cma_dev
, unsigned int port
,
340 if (!rdma_is_port_valid(cma_dev
->device
, port
))
343 cma_dev
->default_roce_tos
[port
- rdma_start_port(cma_dev
->device
)] =
348 struct ib_device
*cma_get_ib_dev(struct cma_device
*cma_dev
)
350 return cma_dev
->device
;
354 * Device removal can occur at anytime, so we need extra handling to
355 * serialize notifying the user of device removal with other callbacks.
356 * We do this by disabling removal notification while a callback is in process,
357 * and reporting it after the callback completes.
360 struct cma_multicast
{
361 struct rdma_id_private
*id_priv
;
363 struct ib_sa_multicast
*ib
;
365 struct list_head list
;
367 struct sockaddr_storage addr
;
374 struct work_struct work
;
375 struct rdma_id_private
*id
;
376 enum rdma_cm_state old_state
;
377 enum rdma_cm_state new_state
;
378 struct rdma_cm_event event
;
381 struct cma_ndev_work
{
382 struct work_struct work
;
383 struct rdma_id_private
*id
;
384 struct rdma_cm_event event
;
387 struct iboe_mcast_work
{
388 struct work_struct work
;
389 struct rdma_id_private
*id
;
390 struct cma_multicast
*mc
;
403 u8 ip_version
; /* IP version: 7:4 */
405 union cma_ip_addr src_addr
;
406 union cma_ip_addr dst_addr
;
409 #define CMA_VERSION 0x00
411 struct cma_req_info
{
412 struct sockaddr_storage listen_addr_storage
;
413 struct sockaddr_storage src_addr_storage
;
414 struct ib_device
*device
;
416 union ib_gid local_gid
;
422 static int cma_comp(struct rdma_id_private
*id_priv
, enum rdma_cm_state comp
)
427 spin_lock_irqsave(&id_priv
->lock
, flags
);
428 ret
= (id_priv
->state
== comp
);
429 spin_unlock_irqrestore(&id_priv
->lock
, flags
);
433 static int cma_comp_exch(struct rdma_id_private
*id_priv
,
434 enum rdma_cm_state comp
, enum rdma_cm_state exch
)
439 spin_lock_irqsave(&id_priv
->lock
, flags
);
440 if ((ret
= (id_priv
->state
== comp
)))
441 id_priv
->state
= exch
;
442 spin_unlock_irqrestore(&id_priv
->lock
, flags
);
446 static enum rdma_cm_state
cma_exch(struct rdma_id_private
*id_priv
,
447 enum rdma_cm_state exch
)
450 enum rdma_cm_state old
;
452 spin_lock_irqsave(&id_priv
->lock
, flags
);
453 old
= id_priv
->state
;
454 id_priv
->state
= exch
;
455 spin_unlock_irqrestore(&id_priv
->lock
, flags
);
459 static inline u8
cma_get_ip_ver(const struct cma_hdr
*hdr
)
461 return hdr
->ip_version
>> 4;
464 static inline void cma_set_ip_ver(struct cma_hdr
*hdr
, u8 ip_ver
)
466 hdr
->ip_version
= (ip_ver
<< 4) | (hdr
->ip_version
& 0xF);
469 static int cma_igmp_send(struct net_device
*ndev
, union ib_gid
*mgid
, bool join
)
471 struct in_device
*in_dev
= NULL
;
475 in_dev
= __in_dev_get_rtnl(ndev
);
478 ip_mc_inc_group(in_dev
,
479 *(__be32
*)(mgid
->raw
+ 12));
481 ip_mc_dec_group(in_dev
,
482 *(__be32
*)(mgid
->raw
+ 12));
486 return (in_dev
) ? 0 : -ENODEV
;
489 static void _cma_attach_to_dev(struct rdma_id_private
*id_priv
,
490 struct cma_device
*cma_dev
)
492 cma_ref_dev(cma_dev
);
493 id_priv
->cma_dev
= cma_dev
;
494 id_priv
->gid_type
= 0;
495 id_priv
->id
.device
= cma_dev
->device
;
496 id_priv
->id
.route
.addr
.dev_addr
.transport
=
497 rdma_node_get_transport(cma_dev
->device
->node_type
);
498 list_add_tail(&id_priv
->list
, &cma_dev
->id_list
);
499 id_priv
->res
.type
= RDMA_RESTRACK_CM_ID
;
500 rdma_restrack_add(&id_priv
->res
);
503 static void cma_attach_to_dev(struct rdma_id_private
*id_priv
,
504 struct cma_device
*cma_dev
)
506 _cma_attach_to_dev(id_priv
, cma_dev
);
508 cma_dev
->default_gid_type
[id_priv
->id
.port_num
-
509 rdma_start_port(cma_dev
->device
)];
512 void cma_deref_dev(struct cma_device
*cma_dev
)
514 if (atomic_dec_and_test(&cma_dev
->refcount
))
515 complete(&cma_dev
->comp
);
518 static inline void release_mc(struct kref
*kref
)
520 struct cma_multicast
*mc
= container_of(kref
, struct cma_multicast
, mcref
);
522 kfree(mc
->multicast
.ib
);
526 static void cma_release_dev(struct rdma_id_private
*id_priv
)
529 list_del(&id_priv
->list
);
530 cma_deref_dev(id_priv
->cma_dev
);
531 id_priv
->cma_dev
= NULL
;
535 static inline struct sockaddr
*cma_src_addr(struct rdma_id_private
*id_priv
)
537 return (struct sockaddr
*) &id_priv
->id
.route
.addr
.src_addr
;
540 static inline struct sockaddr
*cma_dst_addr(struct rdma_id_private
*id_priv
)
542 return (struct sockaddr
*) &id_priv
->id
.route
.addr
.dst_addr
;
545 static inline unsigned short cma_family(struct rdma_id_private
*id_priv
)
547 return id_priv
->id
.route
.addr
.src_addr
.ss_family
;
550 static int cma_set_qkey(struct rdma_id_private
*id_priv
, u32 qkey
)
552 struct ib_sa_mcmember_rec rec
;
556 if (qkey
&& id_priv
->qkey
!= qkey
)
562 id_priv
->qkey
= qkey
;
566 switch (id_priv
->id
.ps
) {
569 id_priv
->qkey
= RDMA_UDP_QKEY
;
572 ib_addr_get_mgid(&id_priv
->id
.route
.addr
.dev_addr
, &rec
.mgid
);
573 ret
= ib_sa_get_mcmember_rec(id_priv
->id
.device
,
574 id_priv
->id
.port_num
, &rec
.mgid
,
577 id_priv
->qkey
= be32_to_cpu(rec
.qkey
);
585 static void cma_translate_ib(struct sockaddr_ib
*sib
, struct rdma_dev_addr
*dev_addr
)
587 dev_addr
->dev_type
= ARPHRD_INFINIBAND
;
588 rdma_addr_set_sgid(dev_addr
, (union ib_gid
*) &sib
->sib_addr
);
589 ib_addr_set_pkey(dev_addr
, ntohs(sib
->sib_pkey
));
592 static int cma_translate_addr(struct sockaddr
*addr
, struct rdma_dev_addr
*dev_addr
)
596 if (addr
->sa_family
!= AF_IB
) {
597 ret
= rdma_translate_ip(addr
, dev_addr
);
599 cma_translate_ib((struct sockaddr_ib
*) addr
, dev_addr
);
606 static inline int cma_validate_port(struct ib_device
*device
, u8 port
,
607 enum ib_gid_type gid_type
,
609 struct rdma_id_private
*id_priv
)
611 struct rdma_dev_addr
*dev_addr
= &id_priv
->id
.route
.addr
.dev_addr
;
612 int bound_if_index
= dev_addr
->bound_dev_if
;
613 int dev_type
= dev_addr
->dev_type
;
614 struct net_device
*ndev
= NULL
;
617 if ((dev_type
== ARPHRD_INFINIBAND
) && !rdma_protocol_ib(device
, port
))
620 if ((dev_type
!= ARPHRD_INFINIBAND
) && rdma_protocol_ib(device
, port
))
623 if (dev_type
== ARPHRD_ETHER
&& rdma_protocol_roce(device
, port
)) {
624 ndev
= dev_get_by_index(dev_addr
->net
, bound_if_index
);
628 gid_type
= IB_GID_TYPE_IB
;
631 ret
= ib_find_cached_gid_by_port(device
, gid
, gid_type
, port
,
640 static int cma_acquire_dev(struct rdma_id_private
*id_priv
,
641 struct rdma_id_private
*listen_id_priv
)
643 struct rdma_dev_addr
*dev_addr
= &id_priv
->id
.route
.addr
.dev_addr
;
644 struct cma_device
*cma_dev
;
645 union ib_gid gid
, iboe_gid
, *gidp
;
649 if (dev_addr
->dev_type
!= ARPHRD_INFINIBAND
&&
650 id_priv
->id
.ps
== RDMA_PS_IPOIB
)
654 rdma_ip2gid((struct sockaddr
*)&id_priv
->id
.route
.addr
.src_addr
,
657 memcpy(&gid
, dev_addr
->src_dev_addr
+
658 rdma_addr_gid_offset(dev_addr
), sizeof gid
);
660 if (listen_id_priv
) {
661 cma_dev
= listen_id_priv
->cma_dev
;
662 port
= listen_id_priv
->id
.port_num
;
663 gidp
= rdma_protocol_roce(cma_dev
->device
, port
) ?
666 ret
= cma_validate_port(cma_dev
->device
, port
,
667 rdma_protocol_ib(cma_dev
->device
, port
) ?
669 listen_id_priv
->gid_type
, gidp
,
672 id_priv
->id
.port_num
= port
;
677 list_for_each_entry(cma_dev
, &dev_list
, list
) {
678 for (port
= 1; port
<= cma_dev
->device
->phys_port_cnt
; ++port
) {
679 if (listen_id_priv
&&
680 listen_id_priv
->cma_dev
== cma_dev
&&
681 listen_id_priv
->id
.port_num
== port
)
684 gidp
= rdma_protocol_roce(cma_dev
->device
, port
) ?
687 ret
= cma_validate_port(cma_dev
->device
, port
,
688 rdma_protocol_ib(cma_dev
->device
, port
) ?
690 cma_dev
->default_gid_type
[port
- 1],
693 id_priv
->id
.port_num
= port
;
701 cma_attach_to_dev(id_priv
, cma_dev
);
708 * Select the source IB device and address to reach the destination IB address.
710 static int cma_resolve_ib_dev(struct rdma_id_private
*id_priv
)
712 struct cma_device
*cma_dev
, *cur_dev
;
713 struct sockaddr_ib
*addr
;
714 union ib_gid gid
, sgid
, *dgid
;
717 enum ib_port_state port_state
;
721 addr
= (struct sockaddr_ib
*) cma_dst_addr(id_priv
);
722 dgid
= (union ib_gid
*) &addr
->sib_addr
;
723 pkey
= ntohs(addr
->sib_pkey
);
725 list_for_each_entry(cur_dev
, &dev_list
, list
) {
726 for (p
= 1; p
<= cur_dev
->device
->phys_port_cnt
; ++p
) {
727 if (!rdma_cap_af_ib(cur_dev
->device
, p
))
730 if (ib_find_cached_pkey(cur_dev
->device
, p
, pkey
, &index
))
733 if (ib_get_cached_port_state(cur_dev
->device
, p
, &port_state
))
735 for (i
= 0; !ib_get_cached_gid(cur_dev
->device
, p
, i
,
738 if (!memcmp(&gid
, dgid
, sizeof(gid
))) {
741 id_priv
->id
.port_num
= p
;
745 if (!cma_dev
&& (gid
.global
.subnet_prefix
==
746 dgid
->global
.subnet_prefix
) &&
747 port_state
== IB_PORT_ACTIVE
) {
750 id_priv
->id
.port_num
= p
;
760 cma_attach_to_dev(id_priv
, cma_dev
);
761 addr
= (struct sockaddr_ib
*) cma_src_addr(id_priv
);
762 memcpy(&addr
->sib_addr
, &sgid
, sizeof sgid
);
763 cma_translate_ib(addr
, &id_priv
->id
.route
.addr
.dev_addr
);
767 static void cma_deref_id(struct rdma_id_private
*id_priv
)
769 if (atomic_dec_and_test(&id_priv
->refcount
))
770 complete(&id_priv
->comp
);
773 struct rdma_cm_id
*__rdma_create_id(struct net
*net
,
774 rdma_cm_event_handler event_handler
,
775 void *context
, enum rdma_ucm_port_space ps
,
776 enum ib_qp_type qp_type
, const char *caller
)
778 struct rdma_id_private
*id_priv
;
780 id_priv
= kzalloc(sizeof *id_priv
, GFP_KERNEL
);
782 return ERR_PTR(-ENOMEM
);
785 id_priv
->res
.kern_name
= caller
;
787 rdma_restrack_set_task(&id_priv
->res
, current
);
788 id_priv
->state
= RDMA_CM_IDLE
;
789 id_priv
->id
.context
= context
;
790 id_priv
->id
.event_handler
= event_handler
;
792 id_priv
->id
.qp_type
= qp_type
;
793 id_priv
->tos_set
= false;
794 spin_lock_init(&id_priv
->lock
);
795 mutex_init(&id_priv
->qp_mutex
);
796 init_completion(&id_priv
->comp
);
797 atomic_set(&id_priv
->refcount
, 1);
798 mutex_init(&id_priv
->handler_mutex
);
799 INIT_LIST_HEAD(&id_priv
->listen_list
);
800 INIT_LIST_HEAD(&id_priv
->mc_list
);
801 get_random_bytes(&id_priv
->seq_num
, sizeof id_priv
->seq_num
);
802 id_priv
->id
.route
.addr
.dev_addr
.net
= get_net(net
);
803 id_priv
->seq_num
&= 0x00ffffff;
807 EXPORT_SYMBOL(__rdma_create_id
);
809 static int cma_init_ud_qp(struct rdma_id_private
*id_priv
, struct ib_qp
*qp
)
811 struct ib_qp_attr qp_attr
;
812 int qp_attr_mask
, ret
;
814 qp_attr
.qp_state
= IB_QPS_INIT
;
815 ret
= rdma_init_qp_attr(&id_priv
->id
, &qp_attr
, &qp_attr_mask
);
819 ret
= ib_modify_qp(qp
, &qp_attr
, qp_attr_mask
);
823 qp_attr
.qp_state
= IB_QPS_RTR
;
824 ret
= ib_modify_qp(qp
, &qp_attr
, IB_QP_STATE
);
828 qp_attr
.qp_state
= IB_QPS_RTS
;
830 ret
= ib_modify_qp(qp
, &qp_attr
, IB_QP_STATE
| IB_QP_SQ_PSN
);
835 static int cma_init_conn_qp(struct rdma_id_private
*id_priv
, struct ib_qp
*qp
)
837 struct ib_qp_attr qp_attr
;
838 int qp_attr_mask
, ret
;
840 qp_attr
.qp_state
= IB_QPS_INIT
;
841 ret
= rdma_init_qp_attr(&id_priv
->id
, &qp_attr
, &qp_attr_mask
);
845 return ib_modify_qp(qp
, &qp_attr
, qp_attr_mask
);
848 int rdma_create_qp(struct rdma_cm_id
*id
, struct ib_pd
*pd
,
849 struct ib_qp_init_attr
*qp_init_attr
)
851 struct rdma_id_private
*id_priv
;
855 id_priv
= container_of(id
, struct rdma_id_private
, id
);
856 if (id
->device
!= pd
->device
)
859 qp_init_attr
->port_num
= id
->port_num
;
860 qp
= ib_create_qp(pd
, qp_init_attr
);
864 if (id
->qp_type
== IB_QPT_UD
)
865 ret
= cma_init_ud_qp(id_priv
, qp
);
867 ret
= cma_init_conn_qp(id_priv
, qp
);
872 id_priv
->qp_num
= qp
->qp_num
;
873 id_priv
->srq
= (qp
->srq
!= NULL
);
879 EXPORT_SYMBOL(rdma_create_qp
);
881 void rdma_destroy_qp(struct rdma_cm_id
*id
)
883 struct rdma_id_private
*id_priv
;
885 id_priv
= container_of(id
, struct rdma_id_private
, id
);
886 mutex_lock(&id_priv
->qp_mutex
);
887 ib_destroy_qp(id_priv
->id
.qp
);
888 id_priv
->id
.qp
= NULL
;
889 mutex_unlock(&id_priv
->qp_mutex
);
891 EXPORT_SYMBOL(rdma_destroy_qp
);
893 static int cma_modify_qp_rtr(struct rdma_id_private
*id_priv
,
894 struct rdma_conn_param
*conn_param
)
896 struct ib_qp_attr qp_attr
;
897 int qp_attr_mask
, ret
;
899 mutex_lock(&id_priv
->qp_mutex
);
900 if (!id_priv
->id
.qp
) {
905 /* Need to update QP attributes from default values. */
906 qp_attr
.qp_state
= IB_QPS_INIT
;
907 ret
= rdma_init_qp_attr(&id_priv
->id
, &qp_attr
, &qp_attr_mask
);
911 ret
= ib_modify_qp(id_priv
->id
.qp
, &qp_attr
, qp_attr_mask
);
915 qp_attr
.qp_state
= IB_QPS_RTR
;
916 ret
= rdma_init_qp_attr(&id_priv
->id
, &qp_attr
, &qp_attr_mask
);
920 BUG_ON(id_priv
->cma_dev
->device
!= id_priv
->id
.device
);
923 qp_attr
.max_dest_rd_atomic
= conn_param
->responder_resources
;
924 ret
= ib_modify_qp(id_priv
->id
.qp
, &qp_attr
, qp_attr_mask
);
926 mutex_unlock(&id_priv
->qp_mutex
);
930 static int cma_modify_qp_rts(struct rdma_id_private
*id_priv
,
931 struct rdma_conn_param
*conn_param
)
933 struct ib_qp_attr qp_attr
;
934 int qp_attr_mask
, ret
;
936 mutex_lock(&id_priv
->qp_mutex
);
937 if (!id_priv
->id
.qp
) {
942 qp_attr
.qp_state
= IB_QPS_RTS
;
943 ret
= rdma_init_qp_attr(&id_priv
->id
, &qp_attr
, &qp_attr_mask
);
948 qp_attr
.max_rd_atomic
= conn_param
->initiator_depth
;
949 ret
= ib_modify_qp(id_priv
->id
.qp
, &qp_attr
, qp_attr_mask
);
951 mutex_unlock(&id_priv
->qp_mutex
);
955 static int cma_modify_qp_err(struct rdma_id_private
*id_priv
)
957 struct ib_qp_attr qp_attr
;
960 mutex_lock(&id_priv
->qp_mutex
);
961 if (!id_priv
->id
.qp
) {
966 qp_attr
.qp_state
= IB_QPS_ERR
;
967 ret
= ib_modify_qp(id_priv
->id
.qp
, &qp_attr
, IB_QP_STATE
);
969 mutex_unlock(&id_priv
->qp_mutex
);
973 static int cma_ib_init_qp_attr(struct rdma_id_private
*id_priv
,
974 struct ib_qp_attr
*qp_attr
, int *qp_attr_mask
)
976 struct rdma_dev_addr
*dev_addr
= &id_priv
->id
.route
.addr
.dev_addr
;
980 if (rdma_cap_eth_ah(id_priv
->id
.device
, id_priv
->id
.port_num
))
983 pkey
= ib_addr_get_pkey(dev_addr
);
985 ret
= ib_find_cached_pkey(id_priv
->id
.device
, id_priv
->id
.port_num
,
986 pkey
, &qp_attr
->pkey_index
);
990 qp_attr
->port_num
= id_priv
->id
.port_num
;
991 *qp_attr_mask
= IB_QP_STATE
| IB_QP_PKEY_INDEX
| IB_QP_PORT
;
993 if (id_priv
->id
.qp_type
== IB_QPT_UD
) {
994 ret
= cma_set_qkey(id_priv
, 0);
998 qp_attr
->qkey
= id_priv
->qkey
;
999 *qp_attr_mask
|= IB_QP_QKEY
;
1001 qp_attr
->qp_access_flags
= 0;
1002 *qp_attr_mask
|= IB_QP_ACCESS_FLAGS
;
1007 int rdma_init_qp_attr(struct rdma_cm_id
*id
, struct ib_qp_attr
*qp_attr
,
1010 struct rdma_id_private
*id_priv
;
1013 id_priv
= container_of(id
, struct rdma_id_private
, id
);
1014 if (rdma_cap_ib_cm(id
->device
, id
->port_num
)) {
1015 if (!id_priv
->cm_id
.ib
|| (id_priv
->id
.qp_type
== IB_QPT_UD
))
1016 ret
= cma_ib_init_qp_attr(id_priv
, qp_attr
, qp_attr_mask
);
1018 ret
= ib_cm_init_qp_attr(id_priv
->cm_id
.ib
, qp_attr
,
1021 if (qp_attr
->qp_state
== IB_QPS_RTR
)
1022 qp_attr
->rq_psn
= id_priv
->seq_num
;
1023 } else if (rdma_cap_iw_cm(id
->device
, id
->port_num
)) {
1024 if (!id_priv
->cm_id
.iw
) {
1025 qp_attr
->qp_access_flags
= 0;
1026 *qp_attr_mask
= IB_QP_STATE
| IB_QP_ACCESS_FLAGS
;
1028 ret
= iw_cm_init_qp_attr(id_priv
->cm_id
.iw
, qp_attr
,
1030 qp_attr
->port_num
= id_priv
->id
.port_num
;
1031 *qp_attr_mask
|= IB_QP_PORT
;
1037 EXPORT_SYMBOL(rdma_init_qp_attr
);
1039 static inline int cma_zero_addr(struct sockaddr
*addr
)
1041 switch (addr
->sa_family
) {
1043 return ipv4_is_zeronet(((struct sockaddr_in
*)addr
)->sin_addr
.s_addr
);
1045 return ipv6_addr_any(&((struct sockaddr_in6
*) addr
)->sin6_addr
);
1047 return ib_addr_any(&((struct sockaddr_ib
*) addr
)->sib_addr
);
1053 static inline int cma_loopback_addr(struct sockaddr
*addr
)
1055 switch (addr
->sa_family
) {
1057 return ipv4_is_loopback(((struct sockaddr_in
*) addr
)->sin_addr
.s_addr
);
1059 return ipv6_addr_loopback(&((struct sockaddr_in6
*) addr
)->sin6_addr
);
1061 return ib_addr_loopback(&((struct sockaddr_ib
*) addr
)->sib_addr
);
1067 static inline int cma_any_addr(struct sockaddr
*addr
)
1069 return cma_zero_addr(addr
) || cma_loopback_addr(addr
);
1072 static int cma_addr_cmp(struct sockaddr
*src
, struct sockaddr
*dst
)
1074 if (src
->sa_family
!= dst
->sa_family
)
1077 switch (src
->sa_family
) {
1079 return ((struct sockaddr_in
*) src
)->sin_addr
.s_addr
!=
1080 ((struct sockaddr_in
*) dst
)->sin_addr
.s_addr
;
1082 return ipv6_addr_cmp(&((struct sockaddr_in6
*) src
)->sin6_addr
,
1083 &((struct sockaddr_in6
*) dst
)->sin6_addr
);
1085 return ib_addr_cmp(&((struct sockaddr_ib
*) src
)->sib_addr
,
1086 &((struct sockaddr_ib
*) dst
)->sib_addr
);
1090 static __be16
cma_port(struct sockaddr
*addr
)
1092 struct sockaddr_ib
*sib
;
1094 switch (addr
->sa_family
) {
1096 return ((struct sockaddr_in
*) addr
)->sin_port
;
1098 return ((struct sockaddr_in6
*) addr
)->sin6_port
;
1100 sib
= (struct sockaddr_ib
*) addr
;
1101 return htons((u16
) (be64_to_cpu(sib
->sib_sid
) &
1102 be64_to_cpu(sib
->sib_sid_mask
)));
1108 static inline int cma_any_port(struct sockaddr
*addr
)
1110 return !cma_port(addr
);
1113 static void cma_save_ib_info(struct sockaddr
*src_addr
,
1114 struct sockaddr
*dst_addr
,
1115 struct rdma_cm_id
*listen_id
,
1116 struct sa_path_rec
*path
)
1118 struct sockaddr_ib
*listen_ib
, *ib
;
1120 listen_ib
= (struct sockaddr_ib
*) &listen_id
->route
.addr
.src_addr
;
1122 ib
= (struct sockaddr_ib
*)src_addr
;
1123 ib
->sib_family
= AF_IB
;
1125 ib
->sib_pkey
= path
->pkey
;
1126 ib
->sib_flowinfo
= path
->flow_label
;
1127 memcpy(&ib
->sib_addr
, &path
->sgid
, 16);
1128 ib
->sib_sid
= path
->service_id
;
1129 ib
->sib_scope_id
= 0;
1131 ib
->sib_pkey
= listen_ib
->sib_pkey
;
1132 ib
->sib_flowinfo
= listen_ib
->sib_flowinfo
;
1133 ib
->sib_addr
= listen_ib
->sib_addr
;
1134 ib
->sib_sid
= listen_ib
->sib_sid
;
1135 ib
->sib_scope_id
= listen_ib
->sib_scope_id
;
1137 ib
->sib_sid_mask
= cpu_to_be64(0xffffffffffffffffULL
);
1140 ib
= (struct sockaddr_ib
*)dst_addr
;
1141 ib
->sib_family
= AF_IB
;
1143 ib
->sib_pkey
= path
->pkey
;
1144 ib
->sib_flowinfo
= path
->flow_label
;
1145 memcpy(&ib
->sib_addr
, &path
->dgid
, 16);
1150 static void cma_save_ip4_info(struct sockaddr_in
*src_addr
,
1151 struct sockaddr_in
*dst_addr
,
1152 struct cma_hdr
*hdr
,
1156 *src_addr
= (struct sockaddr_in
) {
1157 .sin_family
= AF_INET
,
1158 .sin_addr
.s_addr
= hdr
->dst_addr
.ip4
.addr
,
1159 .sin_port
= local_port
,
1164 *dst_addr
= (struct sockaddr_in
) {
1165 .sin_family
= AF_INET
,
1166 .sin_addr
.s_addr
= hdr
->src_addr
.ip4
.addr
,
1167 .sin_port
= hdr
->port
,
1172 static void cma_save_ip6_info(struct sockaddr_in6
*src_addr
,
1173 struct sockaddr_in6
*dst_addr
,
1174 struct cma_hdr
*hdr
,
1178 *src_addr
= (struct sockaddr_in6
) {
1179 .sin6_family
= AF_INET6
,
1180 .sin6_addr
= hdr
->dst_addr
.ip6
,
1181 .sin6_port
= local_port
,
1186 *dst_addr
= (struct sockaddr_in6
) {
1187 .sin6_family
= AF_INET6
,
1188 .sin6_addr
= hdr
->src_addr
.ip6
,
1189 .sin6_port
= hdr
->port
,
1194 static u16
cma_port_from_service_id(__be64 service_id
)
1196 return (u16
)be64_to_cpu(service_id
);
1199 static int cma_save_ip_info(struct sockaddr
*src_addr
,
1200 struct sockaddr
*dst_addr
,
1201 struct ib_cm_event
*ib_event
,
1204 struct cma_hdr
*hdr
;
1207 hdr
= ib_event
->private_data
;
1208 if (hdr
->cma_version
!= CMA_VERSION
)
1211 port
= htons(cma_port_from_service_id(service_id
));
1213 switch (cma_get_ip_ver(hdr
)) {
1215 cma_save_ip4_info((struct sockaddr_in
*)src_addr
,
1216 (struct sockaddr_in
*)dst_addr
, hdr
, port
);
1219 cma_save_ip6_info((struct sockaddr_in6
*)src_addr
,
1220 (struct sockaddr_in6
*)dst_addr
, hdr
, port
);
1223 return -EAFNOSUPPORT
;
1229 static int cma_save_net_info(struct sockaddr
*src_addr
,
1230 struct sockaddr
*dst_addr
,
1231 struct rdma_cm_id
*listen_id
,
1232 struct ib_cm_event
*ib_event
,
1233 sa_family_t sa_family
, __be64 service_id
)
1235 if (sa_family
== AF_IB
) {
1236 if (ib_event
->event
== IB_CM_REQ_RECEIVED
)
1237 cma_save_ib_info(src_addr
, dst_addr
, listen_id
,
1238 ib_event
->param
.req_rcvd
.primary_path
);
1239 else if (ib_event
->event
== IB_CM_SIDR_REQ_RECEIVED
)
1240 cma_save_ib_info(src_addr
, dst_addr
, listen_id
, NULL
);
1244 return cma_save_ip_info(src_addr
, dst_addr
, ib_event
, service_id
);
1247 static int cma_save_req_info(const struct ib_cm_event
*ib_event
,
1248 struct cma_req_info
*req
)
1250 const struct ib_cm_req_event_param
*req_param
=
1251 &ib_event
->param
.req_rcvd
;
1252 const struct ib_cm_sidr_req_event_param
*sidr_param
=
1253 &ib_event
->param
.sidr_req_rcvd
;
1255 switch (ib_event
->event
) {
1256 case IB_CM_REQ_RECEIVED
:
1257 req
->device
= req_param
->listen_id
->device
;
1258 req
->port
= req_param
->port
;
1259 memcpy(&req
->local_gid
, &req_param
->primary_path
->sgid
,
1260 sizeof(req
->local_gid
));
1261 req
->has_gid
= true;
1262 req
->service_id
= req_param
->primary_path
->service_id
;
1263 req
->pkey
= be16_to_cpu(req_param
->primary_path
->pkey
);
1264 if (req
->pkey
!= req_param
->bth_pkey
)
1265 pr_warn_ratelimited("RDMA CMA: got different BTH P_Key (0x%x) and primary path P_Key (0x%x)\n"
1266 "RDMA CMA: in the future this may cause the request to be dropped\n",
1267 req_param
->bth_pkey
, req
->pkey
);
1269 case IB_CM_SIDR_REQ_RECEIVED
:
1270 req
->device
= sidr_param
->listen_id
->device
;
1271 req
->port
= sidr_param
->port
;
1272 req
->has_gid
= false;
1273 req
->service_id
= sidr_param
->service_id
;
1274 req
->pkey
= sidr_param
->pkey
;
1275 if (req
->pkey
!= sidr_param
->bth_pkey
)
1276 pr_warn_ratelimited("RDMA CMA: got different BTH P_Key (0x%x) and SIDR request payload P_Key (0x%x)\n"
1277 "RDMA CMA: in the future this may cause the request to be dropped\n",
1278 sidr_param
->bth_pkey
, req
->pkey
);
1287 static bool validate_ipv4_net_dev(struct net_device
*net_dev
,
1288 const struct sockaddr_in
*dst_addr
,
1289 const struct sockaddr_in
*src_addr
)
1291 __be32 daddr
= dst_addr
->sin_addr
.s_addr
,
1292 saddr
= src_addr
->sin_addr
.s_addr
;
1293 struct fib_result res
;
1298 if (ipv4_is_multicast(saddr
) || ipv4_is_lbcast(saddr
) ||
1299 ipv4_is_lbcast(daddr
) || ipv4_is_zeronet(saddr
) ||
1300 ipv4_is_zeronet(daddr
) || ipv4_is_loopback(daddr
) ||
1301 ipv4_is_loopback(saddr
))
1304 memset(&fl4
, 0, sizeof(fl4
));
1305 fl4
.flowi4_iif
= net_dev
->ifindex
;
1310 err
= fib_lookup(dev_net(net_dev
), &fl4
, &res
, 0);
1311 ret
= err
== 0 && FIB_RES_DEV(res
) == net_dev
;
1317 static bool validate_ipv6_net_dev(struct net_device
*net_dev
,
1318 const struct sockaddr_in6
*dst_addr
,
1319 const struct sockaddr_in6
*src_addr
)
1321 #if IS_ENABLED(CONFIG_IPV6)
1322 const int strict
= ipv6_addr_type(&dst_addr
->sin6_addr
) &
1323 IPV6_ADDR_LINKLOCAL
;
1324 struct rt6_info
*rt
= rt6_lookup(dev_net(net_dev
), &dst_addr
->sin6_addr
,
1325 &src_addr
->sin6_addr
, net_dev
->ifindex
,
1332 ret
= rt
->rt6i_idev
->dev
== net_dev
;
1341 static bool validate_net_dev(struct net_device
*net_dev
,
1342 const struct sockaddr
*daddr
,
1343 const struct sockaddr
*saddr
)
1345 const struct sockaddr_in
*daddr4
= (const struct sockaddr_in
*)daddr
;
1346 const struct sockaddr_in
*saddr4
= (const struct sockaddr_in
*)saddr
;
1347 const struct sockaddr_in6
*daddr6
= (const struct sockaddr_in6
*)daddr
;
1348 const struct sockaddr_in6
*saddr6
= (const struct sockaddr_in6
*)saddr
;
1350 switch (daddr
->sa_family
) {
1352 return saddr
->sa_family
== AF_INET
&&
1353 validate_ipv4_net_dev(net_dev
, daddr4
, saddr4
);
1356 return saddr
->sa_family
== AF_INET6
&&
1357 validate_ipv6_net_dev(net_dev
, daddr6
, saddr6
);
1364 static struct net_device
*cma_get_net_dev(struct ib_cm_event
*ib_event
,
1365 struct cma_req_info
*req
)
1367 struct sockaddr
*listen_addr
=
1368 (struct sockaddr
*)&req
->listen_addr_storage
;
1369 struct sockaddr
*src_addr
= (struct sockaddr
*)&req
->src_addr_storage
;
1370 struct net_device
*net_dev
;
1371 const union ib_gid
*gid
= req
->has_gid
? &req
->local_gid
: NULL
;
1374 err
= cma_save_ip_info(listen_addr
, src_addr
, ib_event
,
1377 return ERR_PTR(err
);
1379 net_dev
= ib_get_net_dev_by_params(req
->device
, req
->port
, req
->pkey
,
1382 return ERR_PTR(-ENODEV
);
1387 static enum rdma_ucm_port_space
rdma_ps_from_service_id(__be64 service_id
)
1389 return (be64_to_cpu(service_id
) >> 16) & 0xffff;
1392 static bool cma_match_private_data(struct rdma_id_private
*id_priv
,
1393 const struct cma_hdr
*hdr
)
1395 struct sockaddr
*addr
= cma_src_addr(id_priv
);
1397 struct in6_addr ip6_addr
;
1399 if (cma_any_addr(addr
) && !id_priv
->afonly
)
1402 switch (addr
->sa_family
) {
1404 ip4_addr
= ((struct sockaddr_in
*)addr
)->sin_addr
.s_addr
;
1405 if (cma_get_ip_ver(hdr
) != 4)
1407 if (!cma_any_addr(addr
) &&
1408 hdr
->dst_addr
.ip4
.addr
!= ip4_addr
)
1412 ip6_addr
= ((struct sockaddr_in6
*)addr
)->sin6_addr
;
1413 if (cma_get_ip_ver(hdr
) != 6)
1415 if (!cma_any_addr(addr
) &&
1416 memcmp(&hdr
->dst_addr
.ip6
, &ip6_addr
, sizeof(ip6_addr
)))
1428 static bool cma_protocol_roce(const struct rdma_cm_id
*id
)
1430 struct ib_device
*device
= id
->device
;
1431 const int port_num
= id
->port_num
?: rdma_start_port(device
);
1433 return rdma_protocol_roce(device
, port_num
);
1436 static bool cma_match_net_dev(const struct rdma_cm_id
*id
,
1437 const struct net_device
*net_dev
,
1440 const struct rdma_addr
*addr
= &id
->route
.addr
;
1443 /* This request is an AF_IB request or a RoCE request */
1444 return (!id
->port_num
|| id
->port_num
== port_num
) &&
1445 (addr
->src_addr
.ss_family
== AF_IB
||
1446 rdma_protocol_roce(id
->device
, port_num
));
1448 return !addr
->dev_addr
.bound_dev_if
||
1449 (net_eq(dev_net(net_dev
), addr
->dev_addr
.net
) &&
1450 addr
->dev_addr
.bound_dev_if
== net_dev
->ifindex
);
1453 static struct rdma_id_private
*cma_find_listener(
1454 const struct rdma_bind_list
*bind_list
,
1455 const struct ib_cm_id
*cm_id
,
1456 const struct ib_cm_event
*ib_event
,
1457 const struct cma_req_info
*req
,
1458 const struct net_device
*net_dev
)
1460 struct rdma_id_private
*id_priv
, *id_priv_dev
;
1463 return ERR_PTR(-EINVAL
);
1465 hlist_for_each_entry(id_priv
, &bind_list
->owners
, node
) {
1466 if (cma_match_private_data(id_priv
, ib_event
->private_data
)) {
1467 if (id_priv
->id
.device
== cm_id
->device
&&
1468 cma_match_net_dev(&id_priv
->id
, net_dev
, req
->port
))
1470 list_for_each_entry(id_priv_dev
,
1471 &id_priv
->listen_list
,
1473 if (id_priv_dev
->id
.device
== cm_id
->device
&&
1474 cma_match_net_dev(&id_priv_dev
->id
, net_dev
, req
->port
))
1480 return ERR_PTR(-EINVAL
);
1483 static struct rdma_id_private
*cma_id_from_event(struct ib_cm_id
*cm_id
,
1484 struct ib_cm_event
*ib_event
,
1485 struct net_device
**net_dev
)
1487 struct cma_req_info req
;
1488 struct rdma_bind_list
*bind_list
;
1489 struct rdma_id_private
*id_priv
;
1492 err
= cma_save_req_info(ib_event
, &req
);
1494 return ERR_PTR(err
);
1496 *net_dev
= cma_get_net_dev(ib_event
, &req
);
1497 if (IS_ERR(*net_dev
)) {
1498 if (PTR_ERR(*net_dev
) == -EAFNOSUPPORT
) {
1499 /* Assuming the protocol is AF_IB */
1501 } else if (rdma_protocol_roce(req
.device
, req
.port
)) {
1502 /* TODO find the net dev matching the request parameters
1503 * through the RoCE GID table */
1506 return ERR_CAST(*net_dev
);
1511 * Net namespace might be getting deleted while route lookup,
1512 * cm_id lookup is in progress. Therefore, perform netdevice
1513 * validation, cm_id lookup under rcu lock.
1514 * RCU lock along with netdevice state check, synchronizes with
1515 * netdevice migrating to different net namespace and also avoids
1516 * case where net namespace doesn't get deleted while lookup is in
1518 * If the device state is not IFF_UP, its properties such as ifindex
1519 * and nd_net cannot be trusted to remain valid without rcu lock.
1520 * net/core/dev.c change_net_namespace() ensures to synchronize with
1521 * ongoing operations on net device after device is closed using
1522 * synchronize_net().
1527 * If netdevice is down, it is likely that it is administratively
1528 * down or it might be migrating to different namespace.
1529 * In that case avoid further processing, as the net namespace
1530 * or ifindex may change.
1532 if (((*net_dev
)->flags
& IFF_UP
) == 0) {
1533 id_priv
= ERR_PTR(-EHOSTUNREACH
);
1537 if (!validate_net_dev(*net_dev
,
1538 (struct sockaddr
*)&req
.listen_addr_storage
,
1539 (struct sockaddr
*)&req
.src_addr_storage
)) {
1540 id_priv
= ERR_PTR(-EHOSTUNREACH
);
1545 bind_list
= cma_ps_find(*net_dev
? dev_net(*net_dev
) : &init_net
,
1546 rdma_ps_from_service_id(req
.service_id
),
1547 cma_port_from_service_id(req
.service_id
));
1548 id_priv
= cma_find_listener(bind_list
, cm_id
, ib_event
, &req
, *net_dev
);
1551 if (IS_ERR(id_priv
) && *net_dev
) {
1558 static inline u8
cma_user_data_offset(struct rdma_id_private
*id_priv
)
1560 return cma_family(id_priv
) == AF_IB
? 0 : sizeof(struct cma_hdr
);
1563 static void cma_cancel_route(struct rdma_id_private
*id_priv
)
1565 if (rdma_cap_ib_sa(id_priv
->id
.device
, id_priv
->id
.port_num
)) {
1567 ib_sa_cancel_query(id_priv
->query_id
, id_priv
->query
);
1571 static void cma_cancel_listens(struct rdma_id_private
*id_priv
)
1573 struct rdma_id_private
*dev_id_priv
;
1576 * Remove from listen_any_list to prevent added devices from spawning
1577 * additional listen requests.
1580 list_del(&id_priv
->list
);
1582 while (!list_empty(&id_priv
->listen_list
)) {
1583 dev_id_priv
= list_entry(id_priv
->listen_list
.next
,
1584 struct rdma_id_private
, listen_list
);
1585 /* sync with device removal to avoid duplicate destruction */
1586 list_del_init(&dev_id_priv
->list
);
1587 list_del(&dev_id_priv
->listen_list
);
1588 mutex_unlock(&lock
);
1590 rdma_destroy_id(&dev_id_priv
->id
);
1593 mutex_unlock(&lock
);
1596 static void cma_cancel_operation(struct rdma_id_private
*id_priv
,
1597 enum rdma_cm_state state
)
1600 case RDMA_CM_ADDR_QUERY
:
1601 rdma_addr_cancel(&id_priv
->id
.route
.addr
.dev_addr
);
1603 case RDMA_CM_ROUTE_QUERY
:
1604 cma_cancel_route(id_priv
);
1606 case RDMA_CM_LISTEN
:
1607 if (cma_any_addr(cma_src_addr(id_priv
)) && !id_priv
->cma_dev
)
1608 cma_cancel_listens(id_priv
);
1615 static void cma_release_port(struct rdma_id_private
*id_priv
)
1617 struct rdma_bind_list
*bind_list
= id_priv
->bind_list
;
1618 struct net
*net
= id_priv
->id
.route
.addr
.dev_addr
.net
;
1624 hlist_del(&id_priv
->node
);
1625 if (hlist_empty(&bind_list
->owners
)) {
1626 cma_ps_remove(net
, bind_list
->ps
, bind_list
->port
);
1629 mutex_unlock(&lock
);
1632 static void cma_leave_mc_groups(struct rdma_id_private
*id_priv
)
1634 struct cma_multicast
*mc
;
1636 while (!list_empty(&id_priv
->mc_list
)) {
1637 mc
= container_of(id_priv
->mc_list
.next
,
1638 struct cma_multicast
, list
);
1639 list_del(&mc
->list
);
1640 if (rdma_cap_ib_mcast(id_priv
->cma_dev
->device
,
1641 id_priv
->id
.port_num
)) {
1642 ib_sa_free_multicast(mc
->multicast
.ib
);
1645 if (mc
->igmp_joined
) {
1646 struct rdma_dev_addr
*dev_addr
=
1647 &id_priv
->id
.route
.addr
.dev_addr
;
1648 struct net_device
*ndev
= NULL
;
1650 if (dev_addr
->bound_dev_if
)
1651 ndev
= dev_get_by_index(&init_net
,
1652 dev_addr
->bound_dev_if
);
1655 &mc
->multicast
.ib
->rec
.mgid
,
1660 kref_put(&mc
->mcref
, release_mc
);
1665 void rdma_destroy_id(struct rdma_cm_id
*id
)
1667 struct rdma_id_private
*id_priv
;
1668 enum rdma_cm_state state
;
1670 id_priv
= container_of(id
, struct rdma_id_private
, id
);
1671 state
= cma_exch(id_priv
, RDMA_CM_DESTROYING
);
1672 cma_cancel_operation(id_priv
, state
);
1675 * Wait for any active callback to finish. New callbacks will find
1676 * the id_priv state set to destroying and abort.
1678 mutex_lock(&id_priv
->handler_mutex
);
1679 mutex_unlock(&id_priv
->handler_mutex
);
1681 if (id_priv
->cma_dev
) {
1682 rdma_restrack_del(&id_priv
->res
);
1683 if (rdma_cap_ib_cm(id_priv
->id
.device
, 1)) {
1684 if (id_priv
->cm_id
.ib
)
1685 ib_destroy_cm_id(id_priv
->cm_id
.ib
);
1686 } else if (rdma_cap_iw_cm(id_priv
->id
.device
, 1)) {
1687 if (id_priv
->cm_id
.iw
)
1688 iw_destroy_cm_id(id_priv
->cm_id
.iw
);
1690 cma_leave_mc_groups(id_priv
);
1691 cma_release_dev(id_priv
);
1694 cma_release_port(id_priv
);
1695 cma_deref_id(id_priv
);
1696 wait_for_completion(&id_priv
->comp
);
1698 if (id_priv
->internal_id
)
1699 cma_deref_id(id_priv
->id
.context
);
1701 kfree(id_priv
->id
.route
.path_rec
);
1702 put_net(id_priv
->id
.route
.addr
.dev_addr
.net
);
1705 EXPORT_SYMBOL(rdma_destroy_id
);
1707 static int cma_rep_recv(struct rdma_id_private
*id_priv
)
1711 ret
= cma_modify_qp_rtr(id_priv
, NULL
);
1715 ret
= cma_modify_qp_rts(id_priv
, NULL
);
1719 ret
= ib_send_cm_rtu(id_priv
->cm_id
.ib
, NULL
, 0);
1725 pr_debug_ratelimited("RDMA CM: CONNECT_ERROR: failed to handle reply. status %d\n", ret
);
1726 cma_modify_qp_err(id_priv
);
1727 ib_send_cm_rej(id_priv
->cm_id
.ib
, IB_CM_REJ_CONSUMER_DEFINED
,
1732 static void cma_set_rep_event_data(struct rdma_cm_event
*event
,
1733 struct ib_cm_rep_event_param
*rep_data
,
1736 event
->param
.conn
.private_data
= private_data
;
1737 event
->param
.conn
.private_data_len
= IB_CM_REP_PRIVATE_DATA_SIZE
;
1738 event
->param
.conn
.responder_resources
= rep_data
->responder_resources
;
1739 event
->param
.conn
.initiator_depth
= rep_data
->initiator_depth
;
1740 event
->param
.conn
.flow_control
= rep_data
->flow_control
;
1741 event
->param
.conn
.rnr_retry_count
= rep_data
->rnr_retry_count
;
1742 event
->param
.conn
.srq
= rep_data
->srq
;
1743 event
->param
.conn
.qp_num
= rep_data
->remote_qpn
;
1746 static int cma_ib_handler(struct ib_cm_id
*cm_id
, struct ib_cm_event
*ib_event
)
1748 struct rdma_id_private
*id_priv
= cm_id
->context
;
1749 struct rdma_cm_event event
;
1752 mutex_lock(&id_priv
->handler_mutex
);
1753 if ((ib_event
->event
!= IB_CM_TIMEWAIT_EXIT
&&
1754 id_priv
->state
!= RDMA_CM_CONNECT
) ||
1755 (ib_event
->event
== IB_CM_TIMEWAIT_EXIT
&&
1756 id_priv
->state
!= RDMA_CM_DISCONNECT
))
1759 memset(&event
, 0, sizeof event
);
1760 switch (ib_event
->event
) {
1761 case IB_CM_REQ_ERROR
:
1762 case IB_CM_REP_ERROR
:
1763 event
.event
= RDMA_CM_EVENT_UNREACHABLE
;
1764 event
.status
= -ETIMEDOUT
;
1766 case IB_CM_REP_RECEIVED
:
1767 if (cma_comp(id_priv
, RDMA_CM_CONNECT
) &&
1768 (id_priv
->id
.qp_type
!= IB_QPT_UD
))
1769 ib_send_cm_mra(cm_id
, CMA_CM_MRA_SETTING
, NULL
, 0);
1770 if (id_priv
->id
.qp
) {
1771 event
.status
= cma_rep_recv(id_priv
);
1772 event
.event
= event
.status
? RDMA_CM_EVENT_CONNECT_ERROR
:
1773 RDMA_CM_EVENT_ESTABLISHED
;
1775 event
.event
= RDMA_CM_EVENT_CONNECT_RESPONSE
;
1777 cma_set_rep_event_data(&event
, &ib_event
->param
.rep_rcvd
,
1778 ib_event
->private_data
);
1780 case IB_CM_RTU_RECEIVED
:
1781 case IB_CM_USER_ESTABLISHED
:
1782 event
.event
= RDMA_CM_EVENT_ESTABLISHED
;
1784 case IB_CM_DREQ_ERROR
:
1785 event
.status
= -ETIMEDOUT
; /* fall through */
1786 case IB_CM_DREQ_RECEIVED
:
1787 case IB_CM_DREP_RECEIVED
:
1788 if (!cma_comp_exch(id_priv
, RDMA_CM_CONNECT
,
1789 RDMA_CM_DISCONNECT
))
1791 event
.event
= RDMA_CM_EVENT_DISCONNECTED
;
1793 case IB_CM_TIMEWAIT_EXIT
:
1794 event
.event
= RDMA_CM_EVENT_TIMEWAIT_EXIT
;
1796 case IB_CM_MRA_RECEIVED
:
1799 case IB_CM_REJ_RECEIVED
:
1800 pr_debug_ratelimited("RDMA CM: REJECTED: %s\n", rdma_reject_msg(&id_priv
->id
,
1801 ib_event
->param
.rej_rcvd
.reason
));
1802 cma_modify_qp_err(id_priv
);
1803 event
.status
= ib_event
->param
.rej_rcvd
.reason
;
1804 event
.event
= RDMA_CM_EVENT_REJECTED
;
1805 event
.param
.conn
.private_data
= ib_event
->private_data
;
1806 event
.param
.conn
.private_data_len
= IB_CM_REJ_PRIVATE_DATA_SIZE
;
1809 pr_err("RDMA CMA: unexpected IB CM event: %d\n",
1814 ret
= id_priv
->id
.event_handler(&id_priv
->id
, &event
);
1816 /* Destroy the CM ID by returning a non-zero value. */
1817 id_priv
->cm_id
.ib
= NULL
;
1818 cma_exch(id_priv
, RDMA_CM_DESTROYING
);
1819 mutex_unlock(&id_priv
->handler_mutex
);
1820 rdma_destroy_id(&id_priv
->id
);
1824 mutex_unlock(&id_priv
->handler_mutex
);
1828 static struct rdma_id_private
*cma_new_conn_id(struct rdma_cm_id
*listen_id
,
1829 struct ib_cm_event
*ib_event
,
1830 struct net_device
*net_dev
)
1832 struct rdma_id_private
*listen_id_priv
;
1833 struct rdma_id_private
*id_priv
;
1834 struct rdma_cm_id
*id
;
1835 struct rdma_route
*rt
;
1836 const sa_family_t ss_family
= listen_id
->route
.addr
.src_addr
.ss_family
;
1837 struct sa_path_rec
*path
= ib_event
->param
.req_rcvd
.primary_path
;
1838 const __be64 service_id
=
1839 ib_event
->param
.req_rcvd
.primary_path
->service_id
;
1842 listen_id_priv
= container_of(listen_id
, struct rdma_id_private
, id
);
1843 id
= __rdma_create_id(listen_id
->route
.addr
.dev_addr
.net
,
1844 listen_id
->event_handler
, listen_id
->context
,
1845 listen_id
->ps
, ib_event
->param
.req_rcvd
.qp_type
,
1846 listen_id_priv
->res
.kern_name
);
1850 id_priv
= container_of(id
, struct rdma_id_private
, id
);
1851 if (cma_save_net_info((struct sockaddr
*)&id
->route
.addr
.src_addr
,
1852 (struct sockaddr
*)&id
->route
.addr
.dst_addr
,
1853 listen_id
, ib_event
, ss_family
, service_id
))
1857 rt
->num_paths
= ib_event
->param
.req_rcvd
.alternate_path
? 2 : 1;
1858 rt
->path_rec
= kmalloc_array(rt
->num_paths
, sizeof(*rt
->path_rec
),
1863 rt
->path_rec
[0] = *path
;
1864 if (rt
->num_paths
== 2)
1865 rt
->path_rec
[1] = *ib_event
->param
.req_rcvd
.alternate_path
;
1868 rdma_copy_addr(&rt
->addr
.dev_addr
, net_dev
, NULL
);
1870 if (!cma_protocol_roce(listen_id
) &&
1871 cma_any_addr(cma_src_addr(id_priv
))) {
1872 rt
->addr
.dev_addr
.dev_type
= ARPHRD_INFINIBAND
;
1873 rdma_addr_set_sgid(&rt
->addr
.dev_addr
, &rt
->path_rec
[0].sgid
);
1874 ib_addr_set_pkey(&rt
->addr
.dev_addr
, be16_to_cpu(rt
->path_rec
[0].pkey
));
1875 } else if (!cma_any_addr(cma_src_addr(id_priv
))) {
1876 ret
= cma_translate_addr(cma_src_addr(id_priv
), &rt
->addr
.dev_addr
);
1881 rdma_addr_set_dgid(&rt
->addr
.dev_addr
, &rt
->path_rec
[0].dgid
);
1883 id_priv
->state
= RDMA_CM_CONNECT
;
1887 rdma_destroy_id(id
);
1891 static struct rdma_id_private
*cma_new_udp_id(struct rdma_cm_id
*listen_id
,
1892 struct ib_cm_event
*ib_event
,
1893 struct net_device
*net_dev
)
1895 struct rdma_id_private
*listen_id_priv
;
1896 struct rdma_id_private
*id_priv
;
1897 struct rdma_cm_id
*id
;
1898 const sa_family_t ss_family
= listen_id
->route
.addr
.src_addr
.ss_family
;
1899 struct net
*net
= listen_id
->route
.addr
.dev_addr
.net
;
1902 listen_id_priv
= container_of(listen_id
, struct rdma_id_private
, id
);
1903 id
= __rdma_create_id(net
, listen_id
->event_handler
, listen_id
->context
,
1904 listen_id
->ps
, IB_QPT_UD
,
1905 listen_id_priv
->res
.kern_name
);
1909 id_priv
= container_of(id
, struct rdma_id_private
, id
);
1910 if (cma_save_net_info((struct sockaddr
*)&id
->route
.addr
.src_addr
,
1911 (struct sockaddr
*)&id
->route
.addr
.dst_addr
,
1912 listen_id
, ib_event
, ss_family
,
1913 ib_event
->param
.sidr_req_rcvd
.service_id
))
1917 rdma_copy_addr(&id
->route
.addr
.dev_addr
, net_dev
, NULL
);
1919 if (!cma_any_addr(cma_src_addr(id_priv
))) {
1920 ret
= cma_translate_addr(cma_src_addr(id_priv
),
1921 &id
->route
.addr
.dev_addr
);
1927 id_priv
->state
= RDMA_CM_CONNECT
;
1930 rdma_destroy_id(id
);
1934 static void cma_set_req_event_data(struct rdma_cm_event
*event
,
1935 struct ib_cm_req_event_param
*req_data
,
1936 void *private_data
, int offset
)
1938 event
->param
.conn
.private_data
= private_data
+ offset
;
1939 event
->param
.conn
.private_data_len
= IB_CM_REQ_PRIVATE_DATA_SIZE
- offset
;
1940 event
->param
.conn
.responder_resources
= req_data
->responder_resources
;
1941 event
->param
.conn
.initiator_depth
= req_data
->initiator_depth
;
1942 event
->param
.conn
.flow_control
= req_data
->flow_control
;
1943 event
->param
.conn
.retry_count
= req_data
->retry_count
;
1944 event
->param
.conn
.rnr_retry_count
= req_data
->rnr_retry_count
;
1945 event
->param
.conn
.srq
= req_data
->srq
;
1946 event
->param
.conn
.qp_num
= req_data
->remote_qpn
;
1949 static int cma_check_req_qp_type(struct rdma_cm_id
*id
, struct ib_cm_event
*ib_event
)
1951 return (((ib_event
->event
== IB_CM_REQ_RECEIVED
) &&
1952 (ib_event
->param
.req_rcvd
.qp_type
== id
->qp_type
)) ||
1953 ((ib_event
->event
== IB_CM_SIDR_REQ_RECEIVED
) &&
1954 (id
->qp_type
== IB_QPT_UD
)) ||
1958 static int cma_req_handler(struct ib_cm_id
*cm_id
, struct ib_cm_event
*ib_event
)
1960 struct rdma_id_private
*listen_id
, *conn_id
= NULL
;
1961 struct rdma_cm_event event
;
1962 struct net_device
*net_dev
;
1966 listen_id
= cma_id_from_event(cm_id
, ib_event
, &net_dev
);
1967 if (IS_ERR(listen_id
))
1968 return PTR_ERR(listen_id
);
1970 if (!cma_check_req_qp_type(&listen_id
->id
, ib_event
)) {
1975 mutex_lock(&listen_id
->handler_mutex
);
1976 if (listen_id
->state
!= RDMA_CM_LISTEN
) {
1977 ret
= -ECONNABORTED
;
1981 memset(&event
, 0, sizeof event
);
1982 offset
= cma_user_data_offset(listen_id
);
1983 event
.event
= RDMA_CM_EVENT_CONNECT_REQUEST
;
1984 if (ib_event
->event
== IB_CM_SIDR_REQ_RECEIVED
) {
1985 conn_id
= cma_new_udp_id(&listen_id
->id
, ib_event
, net_dev
);
1986 event
.param
.ud
.private_data
= ib_event
->private_data
+ offset
;
1987 event
.param
.ud
.private_data_len
=
1988 IB_CM_SIDR_REQ_PRIVATE_DATA_SIZE
- offset
;
1990 conn_id
= cma_new_conn_id(&listen_id
->id
, ib_event
, net_dev
);
1991 cma_set_req_event_data(&event
, &ib_event
->param
.req_rcvd
,
1992 ib_event
->private_data
, offset
);
1999 mutex_lock_nested(&conn_id
->handler_mutex
, SINGLE_DEPTH_NESTING
);
2000 ret
= cma_acquire_dev(conn_id
, listen_id
);
2004 conn_id
->cm_id
.ib
= cm_id
;
2005 cm_id
->context
= conn_id
;
2006 cm_id
->cm_handler
= cma_ib_handler
;
2009 * Protect against the user destroying conn_id from another thread
2010 * until we're done accessing it.
2012 atomic_inc(&conn_id
->refcount
);
2013 ret
= conn_id
->id
.event_handler(&conn_id
->id
, &event
);
2017 * Acquire mutex to prevent user executing rdma_destroy_id()
2018 * while we're accessing the cm_id.
2021 if (cma_comp(conn_id
, RDMA_CM_CONNECT
) &&
2022 (conn_id
->id
.qp_type
!= IB_QPT_UD
))
2023 ib_send_cm_mra(cm_id
, CMA_CM_MRA_SETTING
, NULL
, 0);
2024 mutex_unlock(&lock
);
2025 mutex_unlock(&conn_id
->handler_mutex
);
2026 mutex_unlock(&listen_id
->handler_mutex
);
2027 cma_deref_id(conn_id
);
2033 cma_deref_id(conn_id
);
2034 /* Destroy the CM ID by returning a non-zero value. */
2035 conn_id
->cm_id
.ib
= NULL
;
2037 cma_exch(conn_id
, RDMA_CM_DESTROYING
);
2038 mutex_unlock(&conn_id
->handler_mutex
);
2040 mutex_unlock(&listen_id
->handler_mutex
);
2042 rdma_destroy_id(&conn_id
->id
);
2051 __be64
rdma_get_service_id(struct rdma_cm_id
*id
, struct sockaddr
*addr
)
2053 if (addr
->sa_family
== AF_IB
)
2054 return ((struct sockaddr_ib
*) addr
)->sib_sid
;
2056 return cpu_to_be64(((u64
)id
->ps
<< 16) + be16_to_cpu(cma_port(addr
)));
2058 EXPORT_SYMBOL(rdma_get_service_id
);
2060 void rdma_read_gids(struct rdma_cm_id
*cm_id
, union ib_gid
*sgid
,
2063 struct rdma_addr
*addr
= &cm_id
->route
.addr
;
2065 if (!cm_id
->device
) {
2067 memset(sgid
, 0, sizeof(*sgid
));
2069 memset(dgid
, 0, sizeof(*dgid
));
2073 if (rdma_protocol_roce(cm_id
->device
, cm_id
->port_num
)) {
2075 rdma_ip2gid((struct sockaddr
*)&addr
->src_addr
, sgid
);
2077 rdma_ip2gid((struct sockaddr
*)&addr
->dst_addr
, dgid
);
2080 rdma_addr_get_sgid(&addr
->dev_addr
, sgid
);
2082 rdma_addr_get_dgid(&addr
->dev_addr
, dgid
);
2085 EXPORT_SYMBOL(rdma_read_gids
);
2087 static int cma_iw_handler(struct iw_cm_id
*iw_id
, struct iw_cm_event
*iw_event
)
2089 struct rdma_id_private
*id_priv
= iw_id
->context
;
2090 struct rdma_cm_event event
;
2092 struct sockaddr
*laddr
= (struct sockaddr
*)&iw_event
->local_addr
;
2093 struct sockaddr
*raddr
= (struct sockaddr
*)&iw_event
->remote_addr
;
2095 mutex_lock(&id_priv
->handler_mutex
);
2096 if (id_priv
->state
!= RDMA_CM_CONNECT
)
2099 memset(&event
, 0, sizeof event
);
2100 switch (iw_event
->event
) {
2101 case IW_CM_EVENT_CLOSE
:
2102 event
.event
= RDMA_CM_EVENT_DISCONNECTED
;
2104 case IW_CM_EVENT_CONNECT_REPLY
:
2105 memcpy(cma_src_addr(id_priv
), laddr
,
2106 rdma_addr_size(laddr
));
2107 memcpy(cma_dst_addr(id_priv
), raddr
,
2108 rdma_addr_size(raddr
));
2109 switch (iw_event
->status
) {
2111 event
.event
= RDMA_CM_EVENT_ESTABLISHED
;
2112 event
.param
.conn
.initiator_depth
= iw_event
->ird
;
2113 event
.param
.conn
.responder_resources
= iw_event
->ord
;
2117 event
.event
= RDMA_CM_EVENT_REJECTED
;
2120 event
.event
= RDMA_CM_EVENT_UNREACHABLE
;
2123 event
.event
= RDMA_CM_EVENT_CONNECT_ERROR
;
2127 case IW_CM_EVENT_ESTABLISHED
:
2128 event
.event
= RDMA_CM_EVENT_ESTABLISHED
;
2129 event
.param
.conn
.initiator_depth
= iw_event
->ird
;
2130 event
.param
.conn
.responder_resources
= iw_event
->ord
;
2136 event
.status
= iw_event
->status
;
2137 event
.param
.conn
.private_data
= iw_event
->private_data
;
2138 event
.param
.conn
.private_data_len
= iw_event
->private_data_len
;
2139 ret
= id_priv
->id
.event_handler(&id_priv
->id
, &event
);
2141 /* Destroy the CM ID by returning a non-zero value. */
2142 id_priv
->cm_id
.iw
= NULL
;
2143 cma_exch(id_priv
, RDMA_CM_DESTROYING
);
2144 mutex_unlock(&id_priv
->handler_mutex
);
2145 rdma_destroy_id(&id_priv
->id
);
2150 mutex_unlock(&id_priv
->handler_mutex
);
2154 static int iw_conn_req_handler(struct iw_cm_id
*cm_id
,
2155 struct iw_cm_event
*iw_event
)
2157 struct rdma_cm_id
*new_cm_id
;
2158 struct rdma_id_private
*listen_id
, *conn_id
;
2159 struct rdma_cm_event event
;
2160 int ret
= -ECONNABORTED
;
2161 struct sockaddr
*laddr
= (struct sockaddr
*)&iw_event
->local_addr
;
2162 struct sockaddr
*raddr
= (struct sockaddr
*)&iw_event
->remote_addr
;
2164 listen_id
= cm_id
->context
;
2166 mutex_lock(&listen_id
->handler_mutex
);
2167 if (listen_id
->state
!= RDMA_CM_LISTEN
)
2170 /* Create a new RDMA id for the new IW CM ID */
2171 new_cm_id
= __rdma_create_id(listen_id
->id
.route
.addr
.dev_addr
.net
,
2172 listen_id
->id
.event_handler
,
2173 listen_id
->id
.context
,
2174 RDMA_PS_TCP
, IB_QPT_RC
,
2175 listen_id
->res
.kern_name
);
2176 if (IS_ERR(new_cm_id
)) {
2180 conn_id
= container_of(new_cm_id
, struct rdma_id_private
, id
);
2181 mutex_lock_nested(&conn_id
->handler_mutex
, SINGLE_DEPTH_NESTING
);
2182 conn_id
->state
= RDMA_CM_CONNECT
;
2184 ret
= rdma_translate_ip(laddr
, &conn_id
->id
.route
.addr
.dev_addr
);
2186 mutex_unlock(&conn_id
->handler_mutex
);
2187 rdma_destroy_id(new_cm_id
);
2191 ret
= cma_acquire_dev(conn_id
, listen_id
);
2193 mutex_unlock(&conn_id
->handler_mutex
);
2194 rdma_destroy_id(new_cm_id
);
2198 conn_id
->cm_id
.iw
= cm_id
;
2199 cm_id
->context
= conn_id
;
2200 cm_id
->cm_handler
= cma_iw_handler
;
2202 memcpy(cma_src_addr(conn_id
), laddr
, rdma_addr_size(laddr
));
2203 memcpy(cma_dst_addr(conn_id
), raddr
, rdma_addr_size(raddr
));
2205 memset(&event
, 0, sizeof event
);
2206 event
.event
= RDMA_CM_EVENT_CONNECT_REQUEST
;
2207 event
.param
.conn
.private_data
= iw_event
->private_data
;
2208 event
.param
.conn
.private_data_len
= iw_event
->private_data_len
;
2209 event
.param
.conn
.initiator_depth
= iw_event
->ird
;
2210 event
.param
.conn
.responder_resources
= iw_event
->ord
;
2213 * Protect against the user destroying conn_id from another thread
2214 * until we're done accessing it.
2216 atomic_inc(&conn_id
->refcount
);
2217 ret
= conn_id
->id
.event_handler(&conn_id
->id
, &event
);
2219 /* User wants to destroy the CM ID */
2220 conn_id
->cm_id
.iw
= NULL
;
2221 cma_exch(conn_id
, RDMA_CM_DESTROYING
);
2222 mutex_unlock(&conn_id
->handler_mutex
);
2223 cma_deref_id(conn_id
);
2224 rdma_destroy_id(&conn_id
->id
);
2228 mutex_unlock(&conn_id
->handler_mutex
);
2229 cma_deref_id(conn_id
);
2232 mutex_unlock(&listen_id
->handler_mutex
);
2236 static int cma_ib_listen(struct rdma_id_private
*id_priv
)
2238 struct sockaddr
*addr
;
2239 struct ib_cm_id
*id
;
2242 addr
= cma_src_addr(id_priv
);
2243 svc_id
= rdma_get_service_id(&id_priv
->id
, addr
);
2244 id
= ib_cm_insert_listen(id_priv
->id
.device
, cma_req_handler
, svc_id
);
2247 id_priv
->cm_id
.ib
= id
;
2252 static int cma_iw_listen(struct rdma_id_private
*id_priv
, int backlog
)
2255 struct iw_cm_id
*id
;
2257 id
= iw_create_cm_id(id_priv
->id
.device
,
2258 iw_conn_req_handler
,
2263 id
->tos
= id_priv
->tos
;
2264 id_priv
->cm_id
.iw
= id
;
2266 memcpy(&id_priv
->cm_id
.iw
->local_addr
, cma_src_addr(id_priv
),
2267 rdma_addr_size(cma_src_addr(id_priv
)));
2269 ret
= iw_cm_listen(id_priv
->cm_id
.iw
, backlog
);
2272 iw_destroy_cm_id(id_priv
->cm_id
.iw
);
2273 id_priv
->cm_id
.iw
= NULL
;
2279 static int cma_listen_handler(struct rdma_cm_id
*id
,
2280 struct rdma_cm_event
*event
)
2282 struct rdma_id_private
*id_priv
= id
->context
;
2284 id
->context
= id_priv
->id
.context
;
2285 id
->event_handler
= id_priv
->id
.event_handler
;
2286 return id_priv
->id
.event_handler(id
, event
);
2289 static void cma_listen_on_dev(struct rdma_id_private
*id_priv
,
2290 struct cma_device
*cma_dev
)
2292 struct rdma_id_private
*dev_id_priv
;
2293 struct rdma_cm_id
*id
;
2294 struct net
*net
= id_priv
->id
.route
.addr
.dev_addr
.net
;
2297 if (cma_family(id_priv
) == AF_IB
&& !rdma_cap_ib_cm(cma_dev
->device
, 1))
2300 id
= __rdma_create_id(net
, cma_listen_handler
, id_priv
, id_priv
->id
.ps
,
2301 id_priv
->id
.qp_type
, id_priv
->res
.kern_name
);
2305 dev_id_priv
= container_of(id
, struct rdma_id_private
, id
);
2307 dev_id_priv
->state
= RDMA_CM_ADDR_BOUND
;
2308 memcpy(cma_src_addr(dev_id_priv
), cma_src_addr(id_priv
),
2309 rdma_addr_size(cma_src_addr(id_priv
)));
2311 _cma_attach_to_dev(dev_id_priv
, cma_dev
);
2312 list_add_tail(&dev_id_priv
->listen_list
, &id_priv
->listen_list
);
2313 atomic_inc(&id_priv
->refcount
);
2314 dev_id_priv
->internal_id
= 1;
2315 dev_id_priv
->afonly
= id_priv
->afonly
;
2317 ret
= rdma_listen(id
, id_priv
->backlog
);
2319 pr_warn("RDMA CMA: cma_listen_on_dev, error %d, listening on device %s\n",
2320 ret
, cma_dev
->device
->name
);
2323 static void cma_listen_on_all(struct rdma_id_private
*id_priv
)
2325 struct cma_device
*cma_dev
;
2328 list_add_tail(&id_priv
->list
, &listen_any_list
);
2329 list_for_each_entry(cma_dev
, &dev_list
, list
)
2330 cma_listen_on_dev(id_priv
, cma_dev
);
2331 mutex_unlock(&lock
);
2334 void rdma_set_service_type(struct rdma_cm_id
*id
, int tos
)
2336 struct rdma_id_private
*id_priv
;
2338 id_priv
= container_of(id
, struct rdma_id_private
, id
);
2339 id_priv
->tos
= (u8
) tos
;
2340 id_priv
->tos_set
= true;
2342 EXPORT_SYMBOL(rdma_set_service_type
);
2344 static void cma_query_handler(int status
, struct sa_path_rec
*path_rec
,
2347 struct cma_work
*work
= context
;
2348 struct rdma_route
*route
;
2350 route
= &work
->id
->id
.route
;
2353 route
->num_paths
= 1;
2354 *route
->path_rec
= *path_rec
;
2356 work
->old_state
= RDMA_CM_ROUTE_QUERY
;
2357 work
->new_state
= RDMA_CM_ADDR_RESOLVED
;
2358 work
->event
.event
= RDMA_CM_EVENT_ROUTE_ERROR
;
2359 work
->event
.status
= status
;
2360 pr_debug_ratelimited("RDMA CM: ROUTE_ERROR: failed to query path. status %d\n",
2364 queue_work(cma_wq
, &work
->work
);
2367 static int cma_query_ib_route(struct rdma_id_private
*id_priv
, int timeout_ms
,
2368 struct cma_work
*work
)
2370 struct rdma_dev_addr
*dev_addr
= &id_priv
->id
.route
.addr
.dev_addr
;
2371 struct sa_path_rec path_rec
;
2372 ib_sa_comp_mask comp_mask
;
2373 struct sockaddr_in6
*sin6
;
2374 struct sockaddr_ib
*sib
;
2376 memset(&path_rec
, 0, sizeof path_rec
);
2378 if (rdma_cap_opa_ah(id_priv
->id
.device
, id_priv
->id
.port_num
))
2379 path_rec
.rec_type
= SA_PATH_REC_TYPE_OPA
;
2381 path_rec
.rec_type
= SA_PATH_REC_TYPE_IB
;
2382 rdma_addr_get_sgid(dev_addr
, &path_rec
.sgid
);
2383 rdma_addr_get_dgid(dev_addr
, &path_rec
.dgid
);
2384 path_rec
.pkey
= cpu_to_be16(ib_addr_get_pkey(dev_addr
));
2385 path_rec
.numb_path
= 1;
2386 path_rec
.reversible
= 1;
2387 path_rec
.service_id
= rdma_get_service_id(&id_priv
->id
,
2388 cma_dst_addr(id_priv
));
2390 comp_mask
= IB_SA_PATH_REC_DGID
| IB_SA_PATH_REC_SGID
|
2391 IB_SA_PATH_REC_PKEY
| IB_SA_PATH_REC_NUMB_PATH
|
2392 IB_SA_PATH_REC_REVERSIBLE
| IB_SA_PATH_REC_SERVICE_ID
;
2394 switch (cma_family(id_priv
)) {
2396 path_rec
.qos_class
= cpu_to_be16((u16
) id_priv
->tos
);
2397 comp_mask
|= IB_SA_PATH_REC_QOS_CLASS
;
2400 sin6
= (struct sockaddr_in6
*) cma_src_addr(id_priv
);
2401 path_rec
.traffic_class
= (u8
) (be32_to_cpu(sin6
->sin6_flowinfo
) >> 20);
2402 comp_mask
|= IB_SA_PATH_REC_TRAFFIC_CLASS
;
2405 sib
= (struct sockaddr_ib
*) cma_src_addr(id_priv
);
2406 path_rec
.traffic_class
= (u8
) (be32_to_cpu(sib
->sib_flowinfo
) >> 20);
2407 comp_mask
|= IB_SA_PATH_REC_TRAFFIC_CLASS
;
2411 id_priv
->query_id
= ib_sa_path_rec_get(&sa_client
, id_priv
->id
.device
,
2412 id_priv
->id
.port_num
, &path_rec
,
2413 comp_mask
, timeout_ms
,
2414 GFP_KERNEL
, cma_query_handler
,
2415 work
, &id_priv
->query
);
2417 return (id_priv
->query_id
< 0) ? id_priv
->query_id
: 0;
2420 static void cma_work_handler(struct work_struct
*_work
)
2422 struct cma_work
*work
= container_of(_work
, struct cma_work
, work
);
2423 struct rdma_id_private
*id_priv
= work
->id
;
2426 mutex_lock(&id_priv
->handler_mutex
);
2427 if (!cma_comp_exch(id_priv
, work
->old_state
, work
->new_state
))
2430 if (id_priv
->id
.event_handler(&id_priv
->id
, &work
->event
)) {
2431 cma_exch(id_priv
, RDMA_CM_DESTROYING
);
2435 mutex_unlock(&id_priv
->handler_mutex
);
2436 cma_deref_id(id_priv
);
2438 rdma_destroy_id(&id_priv
->id
);
2442 static void cma_ndev_work_handler(struct work_struct
*_work
)
2444 struct cma_ndev_work
*work
= container_of(_work
, struct cma_ndev_work
, work
);
2445 struct rdma_id_private
*id_priv
= work
->id
;
2448 mutex_lock(&id_priv
->handler_mutex
);
2449 if (id_priv
->state
== RDMA_CM_DESTROYING
||
2450 id_priv
->state
== RDMA_CM_DEVICE_REMOVAL
)
2453 if (id_priv
->id
.event_handler(&id_priv
->id
, &work
->event
)) {
2454 cma_exch(id_priv
, RDMA_CM_DESTROYING
);
2459 mutex_unlock(&id_priv
->handler_mutex
);
2460 cma_deref_id(id_priv
);
2462 rdma_destroy_id(&id_priv
->id
);
2466 static void cma_init_resolve_route_work(struct cma_work
*work
,
2467 struct rdma_id_private
*id_priv
)
2470 INIT_WORK(&work
->work
, cma_work_handler
);
2471 work
->old_state
= RDMA_CM_ROUTE_QUERY
;
2472 work
->new_state
= RDMA_CM_ROUTE_RESOLVED
;
2473 work
->event
.event
= RDMA_CM_EVENT_ROUTE_RESOLVED
;
2476 static void cma_init_resolve_addr_work(struct cma_work
*work
,
2477 struct rdma_id_private
*id_priv
)
2480 INIT_WORK(&work
->work
, cma_work_handler
);
2481 work
->old_state
= RDMA_CM_ADDR_QUERY
;
2482 work
->new_state
= RDMA_CM_ADDR_RESOLVED
;
2483 work
->event
.event
= RDMA_CM_EVENT_ADDR_RESOLVED
;
2486 static int cma_resolve_ib_route(struct rdma_id_private
*id_priv
, int timeout_ms
)
2488 struct rdma_route
*route
= &id_priv
->id
.route
;
2489 struct cma_work
*work
;
2492 work
= kzalloc(sizeof *work
, GFP_KERNEL
);
2496 cma_init_resolve_route_work(work
, id_priv
);
2498 route
->path_rec
= kmalloc(sizeof *route
->path_rec
, GFP_KERNEL
);
2499 if (!route
->path_rec
) {
2504 ret
= cma_query_ib_route(id_priv
, timeout_ms
, work
);
2510 kfree(route
->path_rec
);
2511 route
->path_rec
= NULL
;
2517 static enum ib_gid_type
cma_route_gid_type(enum rdma_network_type network_type
,
2518 unsigned long supported_gids
,
2519 enum ib_gid_type default_gid
)
2521 if ((network_type
== RDMA_NETWORK_IPV4
||
2522 network_type
== RDMA_NETWORK_IPV6
) &&
2523 test_bit(IB_GID_TYPE_ROCE_UDP_ENCAP
, &supported_gids
))
2524 return IB_GID_TYPE_ROCE_UDP_ENCAP
;
2530 * cma_iboe_set_path_rec_l2_fields() is helper function which sets
2531 * path record type based on GID type.
2532 * It also sets up other L2 fields which includes destination mac address
2533 * netdev ifindex, of the path record.
2534 * It returns the netdev of the bound interface for this path record entry.
2536 static struct net_device
*
2537 cma_iboe_set_path_rec_l2_fields(struct rdma_id_private
*id_priv
)
2539 struct rdma_route
*route
= &id_priv
->id
.route
;
2540 enum ib_gid_type gid_type
= IB_GID_TYPE_ROCE
;
2541 struct rdma_addr
*addr
= &route
->addr
;
2542 unsigned long supported_gids
;
2543 struct net_device
*ndev
;
2545 if (!addr
->dev_addr
.bound_dev_if
)
2548 ndev
= dev_get_by_index(addr
->dev_addr
.net
,
2549 addr
->dev_addr
.bound_dev_if
);
2553 supported_gids
= roce_gid_type_mask_support(id_priv
->id
.device
,
2554 id_priv
->id
.port_num
);
2555 gid_type
= cma_route_gid_type(addr
->dev_addr
.network
,
2558 /* Use the hint from IP Stack to select GID Type */
2559 if (gid_type
< ib_network_to_gid_type(addr
->dev_addr
.network
))
2560 gid_type
= ib_network_to_gid_type(addr
->dev_addr
.network
);
2561 route
->path_rec
->rec_type
= sa_conv_gid_to_pathrec_type(gid_type
);
2563 route
->path_rec
->roce
.route_resolved
= true;
2564 sa_path_set_ndev(route
->path_rec
, addr
->dev_addr
.net
);
2565 sa_path_set_ifindex(route
->path_rec
, ndev
->ifindex
);
2566 sa_path_set_dmac(route
->path_rec
, addr
->dev_addr
.dst_dev_addr
);
2570 int rdma_set_ib_path(struct rdma_cm_id
*id
,
2571 struct sa_path_rec
*path_rec
)
2573 struct rdma_id_private
*id_priv
;
2574 struct net_device
*ndev
;
2577 id_priv
= container_of(id
, struct rdma_id_private
, id
);
2578 if (!cma_comp_exch(id_priv
, RDMA_CM_ADDR_RESOLVED
,
2579 RDMA_CM_ROUTE_RESOLVED
))
2582 id
->route
.path_rec
= kmemdup(path_rec
, sizeof(*path_rec
),
2584 if (!id
->route
.path_rec
) {
2589 if (rdma_protocol_roce(id
->device
, id
->port_num
)) {
2590 ndev
= cma_iboe_set_path_rec_l2_fields(id_priv
);
2598 id
->route
.num_paths
= 1;
2602 kfree(id
->route
.path_rec
);
2603 id
->route
.path_rec
= NULL
;
2605 cma_comp_exch(id_priv
, RDMA_CM_ROUTE_RESOLVED
, RDMA_CM_ADDR_RESOLVED
);
2608 EXPORT_SYMBOL(rdma_set_ib_path
);
2610 static int cma_resolve_iw_route(struct rdma_id_private
*id_priv
, int timeout_ms
)
2612 struct cma_work
*work
;
2614 work
= kzalloc(sizeof *work
, GFP_KERNEL
);
2618 cma_init_resolve_route_work(work
, id_priv
);
2619 queue_work(cma_wq
, &work
->work
);
2623 static int iboe_tos_to_sl(struct net_device
*ndev
, int tos
)
2626 struct net_device
*dev
;
2628 prio
= rt_tos2priority(tos
);
2629 dev
= is_vlan_dev(ndev
) ? vlan_dev_real_dev(ndev
) : ndev
;
2631 return netdev_get_prio_tc_map(dev
, prio
);
2633 #if IS_ENABLED(CONFIG_VLAN_8021Q)
2634 if (is_vlan_dev(ndev
))
2635 return (vlan_dev_get_egress_qos_mask(ndev
, prio
) &
2636 VLAN_PRIO_MASK
) >> VLAN_PRIO_SHIFT
;
2641 static int cma_resolve_iboe_route(struct rdma_id_private
*id_priv
)
2643 struct rdma_route
*route
= &id_priv
->id
.route
;
2644 struct rdma_addr
*addr
= &route
->addr
;
2645 struct cma_work
*work
;
2647 struct net_device
*ndev
;
2649 u8 default_roce_tos
= id_priv
->cma_dev
->default_roce_tos
[id_priv
->id
.port_num
-
2650 rdma_start_port(id_priv
->cma_dev
->device
)];
2651 u8 tos
= id_priv
->tos_set
? id_priv
->tos
: default_roce_tos
;
2654 work
= kzalloc(sizeof *work
, GFP_KERNEL
);
2658 route
->path_rec
= kzalloc(sizeof *route
->path_rec
, GFP_KERNEL
);
2659 if (!route
->path_rec
) {
2664 route
->num_paths
= 1;
2666 ndev
= cma_iboe_set_path_rec_l2_fields(id_priv
);
2672 rdma_ip2gid((struct sockaddr
*)&id_priv
->id
.route
.addr
.src_addr
,
2673 &route
->path_rec
->sgid
);
2674 rdma_ip2gid((struct sockaddr
*)&id_priv
->id
.route
.addr
.dst_addr
,
2675 &route
->path_rec
->dgid
);
2677 if (((struct sockaddr
*)&id_priv
->id
.route
.addr
.dst_addr
)->sa_family
!= AF_IB
)
2678 /* TODO: get the hoplimit from the inet/inet6 device */
2679 route
->path_rec
->hop_limit
= addr
->dev_addr
.hoplimit
;
2681 route
->path_rec
->hop_limit
= 1;
2682 route
->path_rec
->reversible
= 1;
2683 route
->path_rec
->pkey
= cpu_to_be16(0xffff);
2684 route
->path_rec
->mtu_selector
= IB_SA_EQ
;
2685 route
->path_rec
->sl
= iboe_tos_to_sl(ndev
, tos
);
2686 route
->path_rec
->traffic_class
= tos
;
2687 route
->path_rec
->mtu
= iboe_get_mtu(ndev
->mtu
);
2688 route
->path_rec
->rate_selector
= IB_SA_EQ
;
2689 route
->path_rec
->rate
= iboe_get_rate(ndev
);
2691 route
->path_rec
->packet_life_time_selector
= IB_SA_EQ
;
2692 route
->path_rec
->packet_life_time
= CMA_IBOE_PACKET_LIFETIME
;
2693 if (!route
->path_rec
->mtu
) {
2698 cma_init_resolve_route_work(work
, id_priv
);
2699 queue_work(cma_wq
, &work
->work
);
2704 kfree(route
->path_rec
);
2705 route
->path_rec
= NULL
;
2711 int rdma_resolve_route(struct rdma_cm_id
*id
, int timeout_ms
)
2713 struct rdma_id_private
*id_priv
;
2716 id_priv
= container_of(id
, struct rdma_id_private
, id
);
2717 if (!cma_comp_exch(id_priv
, RDMA_CM_ADDR_RESOLVED
, RDMA_CM_ROUTE_QUERY
))
2720 atomic_inc(&id_priv
->refcount
);
2721 if (rdma_cap_ib_sa(id
->device
, id
->port_num
))
2722 ret
= cma_resolve_ib_route(id_priv
, timeout_ms
);
2723 else if (rdma_protocol_roce(id
->device
, id
->port_num
))
2724 ret
= cma_resolve_iboe_route(id_priv
);
2725 else if (rdma_protocol_iwarp(id
->device
, id
->port_num
))
2726 ret
= cma_resolve_iw_route(id_priv
, timeout_ms
);
2735 cma_comp_exch(id_priv
, RDMA_CM_ROUTE_QUERY
, RDMA_CM_ADDR_RESOLVED
);
2736 cma_deref_id(id_priv
);
2739 EXPORT_SYMBOL(rdma_resolve_route
);
2741 static void cma_set_loopback(struct sockaddr
*addr
)
2743 switch (addr
->sa_family
) {
2745 ((struct sockaddr_in
*) addr
)->sin_addr
.s_addr
= htonl(INADDR_LOOPBACK
);
2748 ipv6_addr_set(&((struct sockaddr_in6
*) addr
)->sin6_addr
,
2752 ib_addr_set(&((struct sockaddr_ib
*) addr
)->sib_addr
,
2758 static int cma_bind_loopback(struct rdma_id_private
*id_priv
)
2760 struct cma_device
*cma_dev
, *cur_dev
;
2762 enum ib_port_state port_state
;
2769 list_for_each_entry(cur_dev
, &dev_list
, list
) {
2770 if (cma_family(id_priv
) == AF_IB
&&
2771 !rdma_cap_ib_cm(cur_dev
->device
, 1))
2777 for (p
= 1; p
<= cur_dev
->device
->phys_port_cnt
; ++p
) {
2778 if (!ib_get_cached_port_state(cur_dev
->device
, p
, &port_state
) &&
2779 port_state
== IB_PORT_ACTIVE
) {
2794 ret
= ib_get_cached_gid(cma_dev
->device
, p
, 0, &gid
, NULL
);
2798 ret
= ib_get_cached_pkey(cma_dev
->device
, p
, 0, &pkey
);
2802 id_priv
->id
.route
.addr
.dev_addr
.dev_type
=
2803 (rdma_protocol_ib(cma_dev
->device
, p
)) ?
2804 ARPHRD_INFINIBAND
: ARPHRD_ETHER
;
2806 rdma_addr_set_sgid(&id_priv
->id
.route
.addr
.dev_addr
, &gid
);
2807 ib_addr_set_pkey(&id_priv
->id
.route
.addr
.dev_addr
, pkey
);
2808 id_priv
->id
.port_num
= p
;
2809 cma_attach_to_dev(id_priv
, cma_dev
);
2810 cma_set_loopback(cma_src_addr(id_priv
));
2812 mutex_unlock(&lock
);
2816 static void addr_handler(int status
, struct sockaddr
*src_addr
,
2817 struct rdma_dev_addr
*dev_addr
, void *context
)
2819 struct rdma_id_private
*id_priv
= context
;
2820 struct rdma_cm_event event
;
2822 memset(&event
, 0, sizeof event
);
2823 mutex_lock(&id_priv
->handler_mutex
);
2824 if (!cma_comp_exch(id_priv
, RDMA_CM_ADDR_QUERY
,
2825 RDMA_CM_ADDR_RESOLVED
))
2828 memcpy(cma_src_addr(id_priv
), src_addr
, rdma_addr_size(src_addr
));
2829 if (!status
&& !id_priv
->cma_dev
) {
2830 status
= cma_acquire_dev(id_priv
, NULL
);
2832 pr_debug_ratelimited("RDMA CM: ADDR_ERROR: failed to acquire device. status %d\n",
2835 pr_debug_ratelimited("RDMA CM: ADDR_ERROR: failed to resolve IP. status %d\n", status
);
2839 if (!cma_comp_exch(id_priv
, RDMA_CM_ADDR_RESOLVED
,
2840 RDMA_CM_ADDR_BOUND
))
2842 event
.event
= RDMA_CM_EVENT_ADDR_ERROR
;
2843 event
.status
= status
;
2845 event
.event
= RDMA_CM_EVENT_ADDR_RESOLVED
;
2847 if (id_priv
->id
.event_handler(&id_priv
->id
, &event
)) {
2848 cma_exch(id_priv
, RDMA_CM_DESTROYING
);
2849 mutex_unlock(&id_priv
->handler_mutex
);
2850 cma_deref_id(id_priv
);
2851 rdma_destroy_id(&id_priv
->id
);
2855 mutex_unlock(&id_priv
->handler_mutex
);
2856 cma_deref_id(id_priv
);
2859 static int cma_resolve_loopback(struct rdma_id_private
*id_priv
)
2861 struct cma_work
*work
;
2865 work
= kzalloc(sizeof *work
, GFP_KERNEL
);
2869 if (!id_priv
->cma_dev
) {
2870 ret
= cma_bind_loopback(id_priv
);
2875 rdma_addr_get_sgid(&id_priv
->id
.route
.addr
.dev_addr
, &gid
);
2876 rdma_addr_set_dgid(&id_priv
->id
.route
.addr
.dev_addr
, &gid
);
2878 cma_init_resolve_addr_work(work
, id_priv
);
2879 queue_work(cma_wq
, &work
->work
);
2886 static int cma_resolve_ib_addr(struct rdma_id_private
*id_priv
)
2888 struct cma_work
*work
;
2891 work
= kzalloc(sizeof *work
, GFP_KERNEL
);
2895 if (!id_priv
->cma_dev
) {
2896 ret
= cma_resolve_ib_dev(id_priv
);
2901 rdma_addr_set_dgid(&id_priv
->id
.route
.addr
.dev_addr
, (union ib_gid
*)
2902 &(((struct sockaddr_ib
*) &id_priv
->id
.route
.addr
.dst_addr
)->sib_addr
));
2904 cma_init_resolve_addr_work(work
, id_priv
);
2905 queue_work(cma_wq
, &work
->work
);
2912 static int cma_bind_addr(struct rdma_cm_id
*id
, struct sockaddr
*src_addr
,
2913 struct sockaddr
*dst_addr
)
2915 if (!src_addr
|| !src_addr
->sa_family
) {
2916 src_addr
= (struct sockaddr
*) &id
->route
.addr
.src_addr
;
2917 src_addr
->sa_family
= dst_addr
->sa_family
;
2918 if (IS_ENABLED(CONFIG_IPV6
) &&
2919 dst_addr
->sa_family
== AF_INET6
) {
2920 struct sockaddr_in6
*src_addr6
= (struct sockaddr_in6
*) src_addr
;
2921 struct sockaddr_in6
*dst_addr6
= (struct sockaddr_in6
*) dst_addr
;
2922 src_addr6
->sin6_scope_id
= dst_addr6
->sin6_scope_id
;
2923 if (ipv6_addr_type(&dst_addr6
->sin6_addr
) & IPV6_ADDR_LINKLOCAL
)
2924 id
->route
.addr
.dev_addr
.bound_dev_if
= dst_addr6
->sin6_scope_id
;
2925 } else if (dst_addr
->sa_family
== AF_IB
) {
2926 ((struct sockaddr_ib
*) src_addr
)->sib_pkey
=
2927 ((struct sockaddr_ib
*) dst_addr
)->sib_pkey
;
2930 return rdma_bind_addr(id
, src_addr
);
2933 int rdma_resolve_addr(struct rdma_cm_id
*id
, struct sockaddr
*src_addr
,
2934 struct sockaddr
*dst_addr
, int timeout_ms
)
2936 struct rdma_id_private
*id_priv
;
2939 id_priv
= container_of(id
, struct rdma_id_private
, id
);
2940 memcpy(cma_dst_addr(id_priv
), dst_addr
, rdma_addr_size(dst_addr
));
2941 if (id_priv
->state
== RDMA_CM_IDLE
) {
2942 ret
= cma_bind_addr(id
, src_addr
, dst_addr
);
2944 memset(cma_dst_addr(id_priv
), 0, rdma_addr_size(dst_addr
));
2949 if (cma_family(id_priv
) != dst_addr
->sa_family
) {
2950 memset(cma_dst_addr(id_priv
), 0, rdma_addr_size(dst_addr
));
2954 if (!cma_comp_exch(id_priv
, RDMA_CM_ADDR_BOUND
, RDMA_CM_ADDR_QUERY
)) {
2955 memset(cma_dst_addr(id_priv
), 0, rdma_addr_size(dst_addr
));
2959 atomic_inc(&id_priv
->refcount
);
2960 if (cma_any_addr(dst_addr
)) {
2961 ret
= cma_resolve_loopback(id_priv
);
2963 if (dst_addr
->sa_family
== AF_IB
) {
2964 ret
= cma_resolve_ib_addr(id_priv
);
2966 ret
= rdma_resolve_ip(cma_src_addr(id_priv
),
2967 dst_addr
, &id
->route
.addr
.dev_addr
,
2968 timeout_ms
, addr_handler
, id_priv
);
2976 cma_comp_exch(id_priv
, RDMA_CM_ADDR_QUERY
, RDMA_CM_ADDR_BOUND
);
2977 cma_deref_id(id_priv
);
2980 EXPORT_SYMBOL(rdma_resolve_addr
);
2982 int rdma_set_reuseaddr(struct rdma_cm_id
*id
, int reuse
)
2984 struct rdma_id_private
*id_priv
;
2985 unsigned long flags
;
2988 id_priv
= container_of(id
, struct rdma_id_private
, id
);
2989 spin_lock_irqsave(&id_priv
->lock
, flags
);
2990 if (reuse
|| id_priv
->state
== RDMA_CM_IDLE
) {
2991 id_priv
->reuseaddr
= reuse
;
2996 spin_unlock_irqrestore(&id_priv
->lock
, flags
);
2999 EXPORT_SYMBOL(rdma_set_reuseaddr
);
3001 int rdma_set_afonly(struct rdma_cm_id
*id
, int afonly
)
3003 struct rdma_id_private
*id_priv
;
3004 unsigned long flags
;
3007 id_priv
= container_of(id
, struct rdma_id_private
, id
);
3008 spin_lock_irqsave(&id_priv
->lock
, flags
);
3009 if (id_priv
->state
== RDMA_CM_IDLE
|| id_priv
->state
== RDMA_CM_ADDR_BOUND
) {
3010 id_priv
->options
|= (1 << CMA_OPTION_AFONLY
);
3011 id_priv
->afonly
= afonly
;
3016 spin_unlock_irqrestore(&id_priv
->lock
, flags
);
3019 EXPORT_SYMBOL(rdma_set_afonly
);
3021 static void cma_bind_port(struct rdma_bind_list
*bind_list
,
3022 struct rdma_id_private
*id_priv
)
3024 struct sockaddr
*addr
;
3025 struct sockaddr_ib
*sib
;
3029 addr
= cma_src_addr(id_priv
);
3030 port
= htons(bind_list
->port
);
3032 switch (addr
->sa_family
) {
3034 ((struct sockaddr_in
*) addr
)->sin_port
= port
;
3037 ((struct sockaddr_in6
*) addr
)->sin6_port
= port
;
3040 sib
= (struct sockaddr_ib
*) addr
;
3041 sid
= be64_to_cpu(sib
->sib_sid
);
3042 mask
= be64_to_cpu(sib
->sib_sid_mask
);
3043 sib
->sib_sid
= cpu_to_be64((sid
& mask
) | (u64
) ntohs(port
));
3044 sib
->sib_sid_mask
= cpu_to_be64(~0ULL);
3047 id_priv
->bind_list
= bind_list
;
3048 hlist_add_head(&id_priv
->node
, &bind_list
->owners
);
3051 static int cma_alloc_port(enum rdma_ucm_port_space ps
,
3052 struct rdma_id_private
*id_priv
, unsigned short snum
)
3054 struct rdma_bind_list
*bind_list
;
3057 bind_list
= kzalloc(sizeof *bind_list
, GFP_KERNEL
);
3061 ret
= cma_ps_alloc(id_priv
->id
.route
.addr
.dev_addr
.net
, ps
, bind_list
,
3067 bind_list
->port
= (unsigned short)ret
;
3068 cma_bind_port(bind_list
, id_priv
);
3072 return ret
== -ENOSPC
? -EADDRNOTAVAIL
: ret
;
3075 static int cma_port_is_unique(struct rdma_bind_list
*bind_list
,
3076 struct rdma_id_private
*id_priv
)
3078 struct rdma_id_private
*cur_id
;
3079 struct sockaddr
*daddr
= cma_dst_addr(id_priv
);
3080 struct sockaddr
*saddr
= cma_src_addr(id_priv
);
3081 __be16 dport
= cma_port(daddr
);
3083 hlist_for_each_entry(cur_id
, &bind_list
->owners
, node
) {
3084 struct sockaddr
*cur_daddr
= cma_dst_addr(cur_id
);
3085 struct sockaddr
*cur_saddr
= cma_src_addr(cur_id
);
3086 __be16 cur_dport
= cma_port(cur_daddr
);
3088 if (id_priv
== cur_id
)
3091 /* different dest port -> unique */
3092 if (!cma_any_port(daddr
) &&
3093 !cma_any_port(cur_daddr
) &&
3094 (dport
!= cur_dport
))
3097 /* different src address -> unique */
3098 if (!cma_any_addr(saddr
) &&
3099 !cma_any_addr(cur_saddr
) &&
3100 cma_addr_cmp(saddr
, cur_saddr
))
3103 /* different dst address -> unique */
3104 if (!cma_any_addr(daddr
) &&
3105 !cma_any_addr(cur_daddr
) &&
3106 cma_addr_cmp(daddr
, cur_daddr
))
3109 return -EADDRNOTAVAIL
;
3114 static int cma_alloc_any_port(enum rdma_ucm_port_space ps
,
3115 struct rdma_id_private
*id_priv
)
3117 static unsigned int last_used_port
;
3118 int low
, high
, remaining
;
3120 struct net
*net
= id_priv
->id
.route
.addr
.dev_addr
.net
;
3122 inet_get_local_port_range(net
, &low
, &high
);
3123 remaining
= (high
- low
) + 1;
3124 rover
= prandom_u32() % remaining
+ low
;
3126 if (last_used_port
!= rover
) {
3127 struct rdma_bind_list
*bind_list
;
3130 bind_list
= cma_ps_find(net
, ps
, (unsigned short)rover
);
3133 ret
= cma_alloc_port(ps
, id_priv
, rover
);
3135 ret
= cma_port_is_unique(bind_list
, id_priv
);
3137 cma_bind_port(bind_list
, id_priv
);
3140 * Remember previously used port number in order to avoid
3141 * re-using same port immediately after it is closed.
3144 last_used_port
= rover
;
3145 if (ret
!= -EADDRNOTAVAIL
)
3150 if ((rover
< low
) || (rover
> high
))
3154 return -EADDRNOTAVAIL
;
3158 * Check that the requested port is available. This is called when trying to
3159 * bind to a specific port, or when trying to listen on a bound port. In
3160 * the latter case, the provided id_priv may already be on the bind_list, but
3161 * we still need to check that it's okay to start listening.
3163 static int cma_check_port(struct rdma_bind_list
*bind_list
,
3164 struct rdma_id_private
*id_priv
, uint8_t reuseaddr
)
3166 struct rdma_id_private
*cur_id
;
3167 struct sockaddr
*addr
, *cur_addr
;
3169 addr
= cma_src_addr(id_priv
);
3170 hlist_for_each_entry(cur_id
, &bind_list
->owners
, node
) {
3171 if (id_priv
== cur_id
)
3174 if ((cur_id
->state
!= RDMA_CM_LISTEN
) && reuseaddr
&&
3178 cur_addr
= cma_src_addr(cur_id
);
3179 if (id_priv
->afonly
&& cur_id
->afonly
&&
3180 (addr
->sa_family
!= cur_addr
->sa_family
))
3183 if (cma_any_addr(addr
) || cma_any_addr(cur_addr
))
3184 return -EADDRNOTAVAIL
;
3186 if (!cma_addr_cmp(addr
, cur_addr
))
3192 static int cma_use_port(enum rdma_ucm_port_space ps
,
3193 struct rdma_id_private
*id_priv
)
3195 struct rdma_bind_list
*bind_list
;
3196 unsigned short snum
;
3199 snum
= ntohs(cma_port(cma_src_addr(id_priv
)));
3200 if (snum
< PROT_SOCK
&& !capable(CAP_NET_BIND_SERVICE
))
3203 bind_list
= cma_ps_find(id_priv
->id
.route
.addr
.dev_addr
.net
, ps
, snum
);
3205 ret
= cma_alloc_port(ps
, id_priv
, snum
);
3207 ret
= cma_check_port(bind_list
, id_priv
, id_priv
->reuseaddr
);
3209 cma_bind_port(bind_list
, id_priv
);
3214 static int cma_bind_listen(struct rdma_id_private
*id_priv
)
3216 struct rdma_bind_list
*bind_list
= id_priv
->bind_list
;
3220 if (bind_list
->owners
.first
->next
)
3221 ret
= cma_check_port(bind_list
, id_priv
, 0);
3222 mutex_unlock(&lock
);
3226 static enum rdma_ucm_port_space
3227 cma_select_inet_ps(struct rdma_id_private
*id_priv
)
3229 switch (id_priv
->id
.ps
) {
3234 return id_priv
->id
.ps
;
3241 static enum rdma_ucm_port_space
3242 cma_select_ib_ps(struct rdma_id_private
*id_priv
)
3244 enum rdma_ucm_port_space ps
= 0;
3245 struct sockaddr_ib
*sib
;
3246 u64 sid_ps
, mask
, sid
;
3248 sib
= (struct sockaddr_ib
*) cma_src_addr(id_priv
);
3249 mask
= be64_to_cpu(sib
->sib_sid_mask
) & RDMA_IB_IP_PS_MASK
;
3250 sid
= be64_to_cpu(sib
->sib_sid
) & mask
;
3252 if ((id_priv
->id
.ps
== RDMA_PS_IB
) && (sid
== (RDMA_IB_IP_PS_IB
& mask
))) {
3253 sid_ps
= RDMA_IB_IP_PS_IB
;
3255 } else if (((id_priv
->id
.ps
== RDMA_PS_IB
) || (id_priv
->id
.ps
== RDMA_PS_TCP
)) &&
3256 (sid
== (RDMA_IB_IP_PS_TCP
& mask
))) {
3257 sid_ps
= RDMA_IB_IP_PS_TCP
;
3259 } else if (((id_priv
->id
.ps
== RDMA_PS_IB
) || (id_priv
->id
.ps
== RDMA_PS_UDP
)) &&
3260 (sid
== (RDMA_IB_IP_PS_UDP
& mask
))) {
3261 sid_ps
= RDMA_IB_IP_PS_UDP
;
3266 sib
->sib_sid
= cpu_to_be64(sid_ps
| ntohs(cma_port((struct sockaddr
*) sib
)));
3267 sib
->sib_sid_mask
= cpu_to_be64(RDMA_IB_IP_PS_MASK
|
3268 be64_to_cpu(sib
->sib_sid_mask
));
3273 static int cma_get_port(struct rdma_id_private
*id_priv
)
3275 enum rdma_ucm_port_space ps
;
3278 if (cma_family(id_priv
) != AF_IB
)
3279 ps
= cma_select_inet_ps(id_priv
);
3281 ps
= cma_select_ib_ps(id_priv
);
3283 return -EPROTONOSUPPORT
;
3286 if (cma_any_port(cma_src_addr(id_priv
)))
3287 ret
= cma_alloc_any_port(ps
, id_priv
);
3289 ret
= cma_use_port(ps
, id_priv
);
3290 mutex_unlock(&lock
);
3295 static int cma_check_linklocal(struct rdma_dev_addr
*dev_addr
,
3296 struct sockaddr
*addr
)
3298 #if IS_ENABLED(CONFIG_IPV6)
3299 struct sockaddr_in6
*sin6
;
3301 if (addr
->sa_family
!= AF_INET6
)
3304 sin6
= (struct sockaddr_in6
*) addr
;
3306 if (!(ipv6_addr_type(&sin6
->sin6_addr
) & IPV6_ADDR_LINKLOCAL
))
3309 if (!sin6
->sin6_scope_id
)
3312 dev_addr
->bound_dev_if
= sin6
->sin6_scope_id
;
3317 int rdma_listen(struct rdma_cm_id
*id
, int backlog
)
3319 struct rdma_id_private
*id_priv
;
3322 id_priv
= container_of(id
, struct rdma_id_private
, id
);
3323 if (id_priv
->state
== RDMA_CM_IDLE
) {
3324 id
->route
.addr
.src_addr
.ss_family
= AF_INET
;
3325 ret
= rdma_bind_addr(id
, cma_src_addr(id_priv
));
3330 if (!cma_comp_exch(id_priv
, RDMA_CM_ADDR_BOUND
, RDMA_CM_LISTEN
))
3333 if (id_priv
->reuseaddr
) {
3334 ret
= cma_bind_listen(id_priv
);
3339 id_priv
->backlog
= backlog
;
3341 if (rdma_cap_ib_cm(id
->device
, 1)) {
3342 ret
= cma_ib_listen(id_priv
);
3345 } else if (rdma_cap_iw_cm(id
->device
, 1)) {
3346 ret
= cma_iw_listen(id_priv
, backlog
);
3354 cma_listen_on_all(id_priv
);
3358 id_priv
->backlog
= 0;
3359 cma_comp_exch(id_priv
, RDMA_CM_LISTEN
, RDMA_CM_ADDR_BOUND
);
3362 EXPORT_SYMBOL(rdma_listen
);
3364 int rdma_bind_addr(struct rdma_cm_id
*id
, struct sockaddr
*addr
)
3366 struct rdma_id_private
*id_priv
;
3368 struct sockaddr
*daddr
;
3370 if (addr
->sa_family
!= AF_INET
&& addr
->sa_family
!= AF_INET6
&&
3371 addr
->sa_family
!= AF_IB
)
3372 return -EAFNOSUPPORT
;
3374 id_priv
= container_of(id
, struct rdma_id_private
, id
);
3375 if (!cma_comp_exch(id_priv
, RDMA_CM_IDLE
, RDMA_CM_ADDR_BOUND
))
3378 ret
= cma_check_linklocal(&id
->route
.addr
.dev_addr
, addr
);
3382 memcpy(cma_src_addr(id_priv
), addr
, rdma_addr_size(addr
));
3383 if (!cma_any_addr(addr
)) {
3384 ret
= cma_translate_addr(addr
, &id
->route
.addr
.dev_addr
);
3388 ret
= cma_acquire_dev(id_priv
, NULL
);
3393 if (!(id_priv
->options
& (1 << CMA_OPTION_AFONLY
))) {
3394 if (addr
->sa_family
== AF_INET
)
3395 id_priv
->afonly
= 1;
3396 #if IS_ENABLED(CONFIG_IPV6)
3397 else if (addr
->sa_family
== AF_INET6
) {
3398 struct net
*net
= id_priv
->id
.route
.addr
.dev_addr
.net
;
3400 id_priv
->afonly
= net
->ipv6
.sysctl
.bindv6only
;
3404 daddr
= cma_dst_addr(id_priv
);
3405 daddr
->sa_family
= addr
->sa_family
;
3407 ret
= cma_get_port(id_priv
);
3413 if (id_priv
->cma_dev
) {
3414 rdma_restrack_del(&id_priv
->res
);
3415 cma_release_dev(id_priv
);
3418 cma_comp_exch(id_priv
, RDMA_CM_ADDR_BOUND
, RDMA_CM_IDLE
);
3421 EXPORT_SYMBOL(rdma_bind_addr
);
3423 static int cma_format_hdr(void *hdr
, struct rdma_id_private
*id_priv
)
3425 struct cma_hdr
*cma_hdr
;
3428 cma_hdr
->cma_version
= CMA_VERSION
;
3429 if (cma_family(id_priv
) == AF_INET
) {
3430 struct sockaddr_in
*src4
, *dst4
;
3432 src4
= (struct sockaddr_in
*) cma_src_addr(id_priv
);
3433 dst4
= (struct sockaddr_in
*) cma_dst_addr(id_priv
);
3435 cma_set_ip_ver(cma_hdr
, 4);
3436 cma_hdr
->src_addr
.ip4
.addr
= src4
->sin_addr
.s_addr
;
3437 cma_hdr
->dst_addr
.ip4
.addr
= dst4
->sin_addr
.s_addr
;
3438 cma_hdr
->port
= src4
->sin_port
;
3439 } else if (cma_family(id_priv
) == AF_INET6
) {
3440 struct sockaddr_in6
*src6
, *dst6
;
3442 src6
= (struct sockaddr_in6
*) cma_src_addr(id_priv
);
3443 dst6
= (struct sockaddr_in6
*) cma_dst_addr(id_priv
);
3445 cma_set_ip_ver(cma_hdr
, 6);
3446 cma_hdr
->src_addr
.ip6
= src6
->sin6_addr
;
3447 cma_hdr
->dst_addr
.ip6
= dst6
->sin6_addr
;
3448 cma_hdr
->port
= src6
->sin6_port
;
3453 static int cma_sidr_rep_handler(struct ib_cm_id
*cm_id
,
3454 struct ib_cm_event
*ib_event
)
3456 struct rdma_id_private
*id_priv
= cm_id
->context
;
3457 struct rdma_cm_event event
;
3458 struct ib_cm_sidr_rep_event_param
*rep
= &ib_event
->param
.sidr_rep_rcvd
;
3461 mutex_lock(&id_priv
->handler_mutex
);
3462 if (id_priv
->state
!= RDMA_CM_CONNECT
)
3465 memset(&event
, 0, sizeof event
);
3466 switch (ib_event
->event
) {
3467 case IB_CM_SIDR_REQ_ERROR
:
3468 event
.event
= RDMA_CM_EVENT_UNREACHABLE
;
3469 event
.status
= -ETIMEDOUT
;
3471 case IB_CM_SIDR_REP_RECEIVED
:
3472 event
.param
.ud
.private_data
= ib_event
->private_data
;
3473 event
.param
.ud
.private_data_len
= IB_CM_SIDR_REP_PRIVATE_DATA_SIZE
;
3474 if (rep
->status
!= IB_SIDR_SUCCESS
) {
3475 event
.event
= RDMA_CM_EVENT_UNREACHABLE
;
3476 event
.status
= ib_event
->param
.sidr_rep_rcvd
.status
;
3477 pr_debug_ratelimited("RDMA CM: UNREACHABLE: bad SIDR reply. status %d\n",
3481 ret
= cma_set_qkey(id_priv
, rep
->qkey
);
3483 pr_debug_ratelimited("RDMA CM: ADDR_ERROR: failed to set qkey. status %d\n", ret
);
3484 event
.event
= RDMA_CM_EVENT_ADDR_ERROR
;
3488 ib_init_ah_attr_from_path(id_priv
->id
.device
,
3489 id_priv
->id
.port_num
,
3490 id_priv
->id
.route
.path_rec
,
3491 &event
.param
.ud
.ah_attr
);
3492 event
.param
.ud
.qp_num
= rep
->qpn
;
3493 event
.param
.ud
.qkey
= rep
->qkey
;
3494 event
.event
= RDMA_CM_EVENT_ESTABLISHED
;
3498 pr_err("RDMA CMA: unexpected IB CM event: %d\n",
3503 ret
= id_priv
->id
.event_handler(&id_priv
->id
, &event
);
3505 /* Destroy the CM ID by returning a non-zero value. */
3506 id_priv
->cm_id
.ib
= NULL
;
3507 cma_exch(id_priv
, RDMA_CM_DESTROYING
);
3508 mutex_unlock(&id_priv
->handler_mutex
);
3509 rdma_destroy_id(&id_priv
->id
);
3513 mutex_unlock(&id_priv
->handler_mutex
);
3517 static int cma_resolve_ib_udp(struct rdma_id_private
*id_priv
,
3518 struct rdma_conn_param
*conn_param
)
3520 struct ib_cm_sidr_req_param req
;
3521 struct ib_cm_id
*id
;
3526 memset(&req
, 0, sizeof req
);
3527 offset
= cma_user_data_offset(id_priv
);
3528 req
.private_data_len
= offset
+ conn_param
->private_data_len
;
3529 if (req
.private_data_len
< conn_param
->private_data_len
)
3532 if (req
.private_data_len
) {
3533 private_data
= kzalloc(req
.private_data_len
, GFP_ATOMIC
);
3537 private_data
= NULL
;
3540 if (conn_param
->private_data
&& conn_param
->private_data_len
)
3541 memcpy(private_data
+ offset
, conn_param
->private_data
,
3542 conn_param
->private_data_len
);
3545 ret
= cma_format_hdr(private_data
, id_priv
);
3548 req
.private_data
= private_data
;
3551 id
= ib_create_cm_id(id_priv
->id
.device
, cma_sidr_rep_handler
,
3557 id_priv
->cm_id
.ib
= id
;
3559 req
.path
= id_priv
->id
.route
.path_rec
;
3560 req
.service_id
= rdma_get_service_id(&id_priv
->id
, cma_dst_addr(id_priv
));
3561 req
.timeout_ms
= 1 << (CMA_CM_RESPONSE_TIMEOUT
- 8);
3562 req
.max_cm_retries
= CMA_MAX_CM_RETRIES
;
3564 ret
= ib_send_cm_sidr_req(id_priv
->cm_id
.ib
, &req
);
3566 ib_destroy_cm_id(id_priv
->cm_id
.ib
);
3567 id_priv
->cm_id
.ib
= NULL
;
3570 kfree(private_data
);
3574 static int cma_connect_ib(struct rdma_id_private
*id_priv
,
3575 struct rdma_conn_param
*conn_param
)
3577 struct ib_cm_req_param req
;
3578 struct rdma_route
*route
;
3580 struct ib_cm_id
*id
;
3584 memset(&req
, 0, sizeof req
);
3585 offset
= cma_user_data_offset(id_priv
);
3586 req
.private_data_len
= offset
+ conn_param
->private_data_len
;
3587 if (req
.private_data_len
< conn_param
->private_data_len
)
3590 if (req
.private_data_len
) {
3591 private_data
= kzalloc(req
.private_data_len
, GFP_ATOMIC
);
3595 private_data
= NULL
;
3598 if (conn_param
->private_data
&& conn_param
->private_data_len
)
3599 memcpy(private_data
+ offset
, conn_param
->private_data
,
3600 conn_param
->private_data_len
);
3602 id
= ib_create_cm_id(id_priv
->id
.device
, cma_ib_handler
, id_priv
);
3607 id_priv
->cm_id
.ib
= id
;
3609 route
= &id_priv
->id
.route
;
3611 ret
= cma_format_hdr(private_data
, id_priv
);
3614 req
.private_data
= private_data
;
3617 req
.primary_path
= &route
->path_rec
[0];
3618 if (route
->num_paths
== 2)
3619 req
.alternate_path
= &route
->path_rec
[1];
3621 req
.service_id
= rdma_get_service_id(&id_priv
->id
, cma_dst_addr(id_priv
));
3622 req
.qp_num
= id_priv
->qp_num
;
3623 req
.qp_type
= id_priv
->id
.qp_type
;
3624 req
.starting_psn
= id_priv
->seq_num
;
3625 req
.responder_resources
= conn_param
->responder_resources
;
3626 req
.initiator_depth
= conn_param
->initiator_depth
;
3627 req
.flow_control
= conn_param
->flow_control
;
3628 req
.retry_count
= min_t(u8
, 7, conn_param
->retry_count
);
3629 req
.rnr_retry_count
= min_t(u8
, 7, conn_param
->rnr_retry_count
);
3630 req
.remote_cm_response_timeout
= CMA_CM_RESPONSE_TIMEOUT
;
3631 req
.local_cm_response_timeout
= CMA_CM_RESPONSE_TIMEOUT
;
3632 req
.max_cm_retries
= CMA_MAX_CM_RETRIES
;
3633 req
.srq
= id_priv
->srq
? 1 : 0;
3635 ret
= ib_send_cm_req(id_priv
->cm_id
.ib
, &req
);
3637 if (ret
&& !IS_ERR(id
)) {
3638 ib_destroy_cm_id(id
);
3639 id_priv
->cm_id
.ib
= NULL
;
3642 kfree(private_data
);
3646 static int cma_connect_iw(struct rdma_id_private
*id_priv
,
3647 struct rdma_conn_param
*conn_param
)
3649 struct iw_cm_id
*cm_id
;
3651 struct iw_cm_conn_param iw_param
;
3653 cm_id
= iw_create_cm_id(id_priv
->id
.device
, cma_iw_handler
, id_priv
);
3655 return PTR_ERR(cm_id
);
3657 cm_id
->tos
= id_priv
->tos
;
3658 id_priv
->cm_id
.iw
= cm_id
;
3660 memcpy(&cm_id
->local_addr
, cma_src_addr(id_priv
),
3661 rdma_addr_size(cma_src_addr(id_priv
)));
3662 memcpy(&cm_id
->remote_addr
, cma_dst_addr(id_priv
),
3663 rdma_addr_size(cma_dst_addr(id_priv
)));
3665 ret
= cma_modify_qp_rtr(id_priv
, conn_param
);
3670 iw_param
.ord
= conn_param
->initiator_depth
;
3671 iw_param
.ird
= conn_param
->responder_resources
;
3672 iw_param
.private_data
= conn_param
->private_data
;
3673 iw_param
.private_data_len
= conn_param
->private_data_len
;
3674 iw_param
.qpn
= id_priv
->id
.qp
? id_priv
->qp_num
: conn_param
->qp_num
;
3676 memset(&iw_param
, 0, sizeof iw_param
);
3677 iw_param
.qpn
= id_priv
->qp_num
;
3679 ret
= iw_cm_connect(cm_id
, &iw_param
);
3682 iw_destroy_cm_id(cm_id
);
3683 id_priv
->cm_id
.iw
= NULL
;
3688 int rdma_connect(struct rdma_cm_id
*id
, struct rdma_conn_param
*conn_param
)
3690 struct rdma_id_private
*id_priv
;
3693 id_priv
= container_of(id
, struct rdma_id_private
, id
);
3694 if (!cma_comp_exch(id_priv
, RDMA_CM_ROUTE_RESOLVED
, RDMA_CM_CONNECT
))
3698 id_priv
->qp_num
= conn_param
->qp_num
;
3699 id_priv
->srq
= conn_param
->srq
;
3702 if (rdma_cap_ib_cm(id
->device
, id
->port_num
)) {
3703 if (id
->qp_type
== IB_QPT_UD
)
3704 ret
= cma_resolve_ib_udp(id_priv
, conn_param
);
3706 ret
= cma_connect_ib(id_priv
, conn_param
);
3707 } else if (rdma_cap_iw_cm(id
->device
, id
->port_num
))
3708 ret
= cma_connect_iw(id_priv
, conn_param
);
3716 cma_comp_exch(id_priv
, RDMA_CM_CONNECT
, RDMA_CM_ROUTE_RESOLVED
);
3719 EXPORT_SYMBOL(rdma_connect
);
3721 static int cma_accept_ib(struct rdma_id_private
*id_priv
,
3722 struct rdma_conn_param
*conn_param
)
3724 struct ib_cm_rep_param rep
;
3727 ret
= cma_modify_qp_rtr(id_priv
, conn_param
);
3731 ret
= cma_modify_qp_rts(id_priv
, conn_param
);
3735 memset(&rep
, 0, sizeof rep
);
3736 rep
.qp_num
= id_priv
->qp_num
;
3737 rep
.starting_psn
= id_priv
->seq_num
;
3738 rep
.private_data
= conn_param
->private_data
;
3739 rep
.private_data_len
= conn_param
->private_data_len
;
3740 rep
.responder_resources
= conn_param
->responder_resources
;
3741 rep
.initiator_depth
= conn_param
->initiator_depth
;
3742 rep
.failover_accepted
= 0;
3743 rep
.flow_control
= conn_param
->flow_control
;
3744 rep
.rnr_retry_count
= min_t(u8
, 7, conn_param
->rnr_retry_count
);
3745 rep
.srq
= id_priv
->srq
? 1 : 0;
3747 ret
= ib_send_cm_rep(id_priv
->cm_id
.ib
, &rep
);
3752 static int cma_accept_iw(struct rdma_id_private
*id_priv
,
3753 struct rdma_conn_param
*conn_param
)
3755 struct iw_cm_conn_param iw_param
;
3761 ret
= cma_modify_qp_rtr(id_priv
, conn_param
);
3765 iw_param
.ord
= conn_param
->initiator_depth
;
3766 iw_param
.ird
= conn_param
->responder_resources
;
3767 iw_param
.private_data
= conn_param
->private_data
;
3768 iw_param
.private_data_len
= conn_param
->private_data_len
;
3769 if (id_priv
->id
.qp
) {
3770 iw_param
.qpn
= id_priv
->qp_num
;
3772 iw_param
.qpn
= conn_param
->qp_num
;
3774 return iw_cm_accept(id_priv
->cm_id
.iw
, &iw_param
);
3777 static int cma_send_sidr_rep(struct rdma_id_private
*id_priv
,
3778 enum ib_cm_sidr_status status
, u32 qkey
,
3779 const void *private_data
, int private_data_len
)
3781 struct ib_cm_sidr_rep_param rep
;
3784 memset(&rep
, 0, sizeof rep
);
3785 rep
.status
= status
;
3786 if (status
== IB_SIDR_SUCCESS
) {
3787 ret
= cma_set_qkey(id_priv
, qkey
);
3790 rep
.qp_num
= id_priv
->qp_num
;
3791 rep
.qkey
= id_priv
->qkey
;
3793 rep
.private_data
= private_data
;
3794 rep
.private_data_len
= private_data_len
;
3796 return ib_send_cm_sidr_rep(id_priv
->cm_id
.ib
, &rep
);
3799 int __rdma_accept(struct rdma_cm_id
*id
, struct rdma_conn_param
*conn_param
,
3802 struct rdma_id_private
*id_priv
;
3805 id_priv
= container_of(id
, struct rdma_id_private
, id
);
3808 id_priv
->res
.kern_name
= caller
;
3810 rdma_restrack_set_task(&id_priv
->res
, current
);
3812 if (!cma_comp(id_priv
, RDMA_CM_CONNECT
))
3815 if (!id
->qp
&& conn_param
) {
3816 id_priv
->qp_num
= conn_param
->qp_num
;
3817 id_priv
->srq
= conn_param
->srq
;
3820 if (rdma_cap_ib_cm(id
->device
, id
->port_num
)) {
3821 if (id
->qp_type
== IB_QPT_UD
) {
3823 ret
= cma_send_sidr_rep(id_priv
, IB_SIDR_SUCCESS
,
3825 conn_param
->private_data
,
3826 conn_param
->private_data_len
);
3828 ret
= cma_send_sidr_rep(id_priv
, IB_SIDR_SUCCESS
,
3832 ret
= cma_accept_ib(id_priv
, conn_param
);
3834 ret
= cma_rep_recv(id_priv
);
3836 } else if (rdma_cap_iw_cm(id
->device
, id
->port_num
))
3837 ret
= cma_accept_iw(id_priv
, conn_param
);
3846 cma_modify_qp_err(id_priv
);
3847 rdma_reject(id
, NULL
, 0);
3850 EXPORT_SYMBOL(__rdma_accept
);
3852 int rdma_notify(struct rdma_cm_id
*id
, enum ib_event_type event
)
3854 struct rdma_id_private
*id_priv
;
3857 id_priv
= container_of(id
, struct rdma_id_private
, id
);
3858 if (!id_priv
->cm_id
.ib
)
3861 switch (id
->device
->node_type
) {
3862 case RDMA_NODE_IB_CA
:
3863 ret
= ib_cm_notify(id_priv
->cm_id
.ib
, event
);
3871 EXPORT_SYMBOL(rdma_notify
);
3873 int rdma_reject(struct rdma_cm_id
*id
, const void *private_data
,
3874 u8 private_data_len
)
3876 struct rdma_id_private
*id_priv
;
3879 id_priv
= container_of(id
, struct rdma_id_private
, id
);
3880 if (!id_priv
->cm_id
.ib
)
3883 if (rdma_cap_ib_cm(id
->device
, id
->port_num
)) {
3884 if (id
->qp_type
== IB_QPT_UD
)
3885 ret
= cma_send_sidr_rep(id_priv
, IB_SIDR_REJECT
, 0,
3886 private_data
, private_data_len
);
3888 ret
= ib_send_cm_rej(id_priv
->cm_id
.ib
,
3889 IB_CM_REJ_CONSUMER_DEFINED
, NULL
,
3890 0, private_data
, private_data_len
);
3891 } else if (rdma_cap_iw_cm(id
->device
, id
->port_num
)) {
3892 ret
= iw_cm_reject(id_priv
->cm_id
.iw
,
3893 private_data
, private_data_len
);
3899 EXPORT_SYMBOL(rdma_reject
);
3901 int rdma_disconnect(struct rdma_cm_id
*id
)
3903 struct rdma_id_private
*id_priv
;
3906 id_priv
= container_of(id
, struct rdma_id_private
, id
);
3907 if (!id_priv
->cm_id
.ib
)
3910 if (rdma_cap_ib_cm(id
->device
, id
->port_num
)) {
3911 ret
= cma_modify_qp_err(id_priv
);
3914 /* Initiate or respond to a disconnect. */
3915 if (ib_send_cm_dreq(id_priv
->cm_id
.ib
, NULL
, 0))
3916 ib_send_cm_drep(id_priv
->cm_id
.ib
, NULL
, 0);
3917 } else if (rdma_cap_iw_cm(id
->device
, id
->port_num
)) {
3918 ret
= iw_cm_disconnect(id_priv
->cm_id
.iw
, 0);
3925 EXPORT_SYMBOL(rdma_disconnect
);
3927 static int cma_ib_mc_handler(int status
, struct ib_sa_multicast
*multicast
)
3929 struct rdma_id_private
*id_priv
;
3930 struct cma_multicast
*mc
= multicast
->context
;
3931 struct rdma_cm_event event
;
3934 id_priv
= mc
->id_priv
;
3935 mutex_lock(&id_priv
->handler_mutex
);
3936 if (id_priv
->state
!= RDMA_CM_ADDR_BOUND
&&
3937 id_priv
->state
!= RDMA_CM_ADDR_RESOLVED
)
3941 status
= cma_set_qkey(id_priv
, be32_to_cpu(multicast
->rec
.qkey
));
3943 pr_debug_ratelimited("RDMA CM: MULTICAST_ERROR: failed to join multicast. status %d\n",
3945 mutex_lock(&id_priv
->qp_mutex
);
3946 if (!status
&& id_priv
->id
.qp
) {
3947 status
= ib_attach_mcast(id_priv
->id
.qp
, &multicast
->rec
.mgid
,
3948 be16_to_cpu(multicast
->rec
.mlid
));
3950 pr_debug_ratelimited("RDMA CM: MULTICAST_ERROR: failed to attach QP. status %d\n",
3953 mutex_unlock(&id_priv
->qp_mutex
);
3955 memset(&event
, 0, sizeof event
);
3956 event
.status
= status
;
3957 event
.param
.ud
.private_data
= mc
->context
;
3959 struct rdma_dev_addr
*dev_addr
=
3960 &id_priv
->id
.route
.addr
.dev_addr
;
3961 struct net_device
*ndev
=
3962 dev_get_by_index(dev_addr
->net
, dev_addr
->bound_dev_if
);
3963 enum ib_gid_type gid_type
=
3964 id_priv
->cma_dev
->default_gid_type
[id_priv
->id
.port_num
-
3965 rdma_start_port(id_priv
->cma_dev
->device
)];
3967 event
.event
= RDMA_CM_EVENT_MULTICAST_JOIN
;
3968 ret
= ib_init_ah_from_mcmember(id_priv
->id
.device
,
3969 id_priv
->id
.port_num
,
3972 &event
.param
.ud
.ah_attr
);
3974 event
.event
= RDMA_CM_EVENT_MULTICAST_ERROR
;
3976 event
.param
.ud
.qp_num
= 0xFFFFFF;
3977 event
.param
.ud
.qkey
= be32_to_cpu(multicast
->rec
.qkey
);
3981 event
.event
= RDMA_CM_EVENT_MULTICAST_ERROR
;
3983 ret
= id_priv
->id
.event_handler(&id_priv
->id
, &event
);
3985 cma_exch(id_priv
, RDMA_CM_DESTROYING
);
3986 mutex_unlock(&id_priv
->handler_mutex
);
3987 rdma_destroy_id(&id_priv
->id
);
3992 mutex_unlock(&id_priv
->handler_mutex
);
3996 static void cma_set_mgid(struct rdma_id_private
*id_priv
,
3997 struct sockaddr
*addr
, union ib_gid
*mgid
)
3999 unsigned char mc_map
[MAX_ADDR_LEN
];
4000 struct rdma_dev_addr
*dev_addr
= &id_priv
->id
.route
.addr
.dev_addr
;
4001 struct sockaddr_in
*sin
= (struct sockaddr_in
*) addr
;
4002 struct sockaddr_in6
*sin6
= (struct sockaddr_in6
*) addr
;
4004 if (cma_any_addr(addr
)) {
4005 memset(mgid
, 0, sizeof *mgid
);
4006 } else if ((addr
->sa_family
== AF_INET6
) &&
4007 ((be32_to_cpu(sin6
->sin6_addr
.s6_addr32
[0]) & 0xFFF0FFFF) ==
4009 /* IPv6 address is an SA assigned MGID. */
4010 memcpy(mgid
, &sin6
->sin6_addr
, sizeof *mgid
);
4011 } else if (addr
->sa_family
== AF_IB
) {
4012 memcpy(mgid
, &((struct sockaddr_ib
*) addr
)->sib_addr
, sizeof *mgid
);
4013 } else if ((addr
->sa_family
== AF_INET6
)) {
4014 ipv6_ib_mc_map(&sin6
->sin6_addr
, dev_addr
->broadcast
, mc_map
);
4015 if (id_priv
->id
.ps
== RDMA_PS_UDP
)
4016 mc_map
[7] = 0x01; /* Use RDMA CM signature */
4017 *mgid
= *(union ib_gid
*) (mc_map
+ 4);
4019 ip_ib_mc_map(sin
->sin_addr
.s_addr
, dev_addr
->broadcast
, mc_map
);
4020 if (id_priv
->id
.ps
== RDMA_PS_UDP
)
4021 mc_map
[7] = 0x01; /* Use RDMA CM signature */
4022 *mgid
= *(union ib_gid
*) (mc_map
+ 4);
4026 static int cma_join_ib_multicast(struct rdma_id_private
*id_priv
,
4027 struct cma_multicast
*mc
)
4029 struct ib_sa_mcmember_rec rec
;
4030 struct rdma_dev_addr
*dev_addr
= &id_priv
->id
.route
.addr
.dev_addr
;
4031 ib_sa_comp_mask comp_mask
;
4034 ib_addr_get_mgid(dev_addr
, &rec
.mgid
);
4035 ret
= ib_sa_get_mcmember_rec(id_priv
->id
.device
, id_priv
->id
.port_num
,
4040 ret
= cma_set_qkey(id_priv
, 0);
4044 cma_set_mgid(id_priv
, (struct sockaddr
*) &mc
->addr
, &rec
.mgid
);
4045 rec
.qkey
= cpu_to_be32(id_priv
->qkey
);
4046 rdma_addr_get_sgid(dev_addr
, &rec
.port_gid
);
4047 rec
.pkey
= cpu_to_be16(ib_addr_get_pkey(dev_addr
));
4048 rec
.join_state
= mc
->join_state
;
4050 if ((rec
.join_state
== BIT(SENDONLY_FULLMEMBER_JOIN
)) &&
4051 (!ib_sa_sendonly_fullmem_support(&sa_client
,
4053 id_priv
->id
.port_num
))) {
4054 pr_warn("RDMA CM: %s port %u Unable to multicast join\n"
4055 "RDMA CM: SM doesn't support Send Only Full Member option\n",
4056 id_priv
->id
.device
->name
, id_priv
->id
.port_num
);
4060 comp_mask
= IB_SA_MCMEMBER_REC_MGID
| IB_SA_MCMEMBER_REC_PORT_GID
|
4061 IB_SA_MCMEMBER_REC_PKEY
| IB_SA_MCMEMBER_REC_JOIN_STATE
|
4062 IB_SA_MCMEMBER_REC_QKEY
| IB_SA_MCMEMBER_REC_SL
|
4063 IB_SA_MCMEMBER_REC_FLOW_LABEL
|
4064 IB_SA_MCMEMBER_REC_TRAFFIC_CLASS
;
4066 if (id_priv
->id
.ps
== RDMA_PS_IPOIB
)
4067 comp_mask
|= IB_SA_MCMEMBER_REC_RATE
|
4068 IB_SA_MCMEMBER_REC_RATE_SELECTOR
|
4069 IB_SA_MCMEMBER_REC_MTU_SELECTOR
|
4070 IB_SA_MCMEMBER_REC_MTU
|
4071 IB_SA_MCMEMBER_REC_HOP_LIMIT
;
4073 mc
->multicast
.ib
= ib_sa_join_multicast(&sa_client
, id_priv
->id
.device
,
4074 id_priv
->id
.port_num
, &rec
,
4075 comp_mask
, GFP_KERNEL
,
4076 cma_ib_mc_handler
, mc
);
4077 return PTR_ERR_OR_ZERO(mc
->multicast
.ib
);
4080 static void iboe_mcast_work_handler(struct work_struct
*work
)
4082 struct iboe_mcast_work
*mw
= container_of(work
, struct iboe_mcast_work
, work
);
4083 struct cma_multicast
*mc
= mw
->mc
;
4084 struct ib_sa_multicast
*m
= mc
->multicast
.ib
;
4086 mc
->multicast
.ib
->context
= mc
;
4087 cma_ib_mc_handler(0, m
);
4088 kref_put(&mc
->mcref
, release_mc
);
4092 static void cma_iboe_set_mgid(struct sockaddr
*addr
, union ib_gid
*mgid
,
4093 enum ib_gid_type gid_type
)
4095 struct sockaddr_in
*sin
= (struct sockaddr_in
*)addr
;
4096 struct sockaddr_in6
*sin6
= (struct sockaddr_in6
*)addr
;
4098 if (cma_any_addr(addr
)) {
4099 memset(mgid
, 0, sizeof *mgid
);
4100 } else if (addr
->sa_family
== AF_INET6
) {
4101 memcpy(mgid
, &sin6
->sin6_addr
, sizeof *mgid
);
4104 (gid_type
== IB_GID_TYPE_ROCE_UDP_ENCAP
) ? 0 : 0xff;
4106 (gid_type
== IB_GID_TYPE_ROCE_UDP_ENCAP
) ? 0 : 0x0e;
4115 mgid
->raw
[10] = 0xff;
4116 mgid
->raw
[11] = 0xff;
4117 *(__be32
*)(&mgid
->raw
[12]) = sin
->sin_addr
.s_addr
;
4121 static int cma_iboe_join_multicast(struct rdma_id_private
*id_priv
,
4122 struct cma_multicast
*mc
)
4124 struct iboe_mcast_work
*work
;
4125 struct rdma_dev_addr
*dev_addr
= &id_priv
->id
.route
.addr
.dev_addr
;
4127 struct sockaddr
*addr
= (struct sockaddr
*)&mc
->addr
;
4128 struct net_device
*ndev
= NULL
;
4129 enum ib_gid_type gid_type
;
4132 send_only
= mc
->join_state
== BIT(SENDONLY_FULLMEMBER_JOIN
);
4134 if (cma_zero_addr((struct sockaddr
*)&mc
->addr
))
4137 work
= kzalloc(sizeof *work
, GFP_KERNEL
);
4141 mc
->multicast
.ib
= kzalloc(sizeof(struct ib_sa_multicast
), GFP_KERNEL
);
4142 if (!mc
->multicast
.ib
) {
4147 gid_type
= id_priv
->cma_dev
->default_gid_type
[id_priv
->id
.port_num
-
4148 rdma_start_port(id_priv
->cma_dev
->device
)];
4149 cma_iboe_set_mgid(addr
, &mc
->multicast
.ib
->rec
.mgid
, gid_type
);
4151 mc
->multicast
.ib
->rec
.pkey
= cpu_to_be16(0xffff);
4152 if (id_priv
->id
.ps
== RDMA_PS_UDP
)
4153 mc
->multicast
.ib
->rec
.qkey
= cpu_to_be32(RDMA_UDP_QKEY
);
4155 if (dev_addr
->bound_dev_if
)
4156 ndev
= dev_get_by_index(dev_addr
->net
, dev_addr
->bound_dev_if
);
4161 mc
->multicast
.ib
->rec
.rate
= iboe_get_rate(ndev
);
4162 mc
->multicast
.ib
->rec
.hop_limit
= 1;
4163 mc
->multicast
.ib
->rec
.mtu
= iboe_get_mtu(ndev
->mtu
);
4165 if (addr
->sa_family
== AF_INET
) {
4166 if (gid_type
== IB_GID_TYPE_ROCE_UDP_ENCAP
) {
4167 mc
->multicast
.ib
->rec
.hop_limit
= IPV6_DEFAULT_HOPLIMIT
;
4169 err
= cma_igmp_send(ndev
, &mc
->multicast
.ib
->rec
.mgid
,
4172 mc
->igmp_joined
= true;
4176 if (gid_type
== IB_GID_TYPE_ROCE_UDP_ENCAP
)
4180 if (err
|| !mc
->multicast
.ib
->rec
.mtu
) {
4185 rdma_ip2gid((struct sockaddr
*)&id_priv
->id
.route
.addr
.src_addr
,
4186 &mc
->multicast
.ib
->rec
.port_gid
);
4189 INIT_WORK(&work
->work
, iboe_mcast_work_handler
);
4190 kref_get(&mc
->mcref
);
4191 queue_work(cma_wq
, &work
->work
);
4196 kfree(mc
->multicast
.ib
);
4202 int rdma_join_multicast(struct rdma_cm_id
*id
, struct sockaddr
*addr
,
4203 u8 join_state
, void *context
)
4205 struct rdma_id_private
*id_priv
;
4206 struct cma_multicast
*mc
;
4212 id_priv
= container_of(id
, struct rdma_id_private
, id
);
4213 if (!cma_comp(id_priv
, RDMA_CM_ADDR_BOUND
) &&
4214 !cma_comp(id_priv
, RDMA_CM_ADDR_RESOLVED
))
4217 mc
= kmalloc(sizeof *mc
, GFP_KERNEL
);
4221 memcpy(&mc
->addr
, addr
, rdma_addr_size(addr
));
4222 mc
->context
= context
;
4223 mc
->id_priv
= id_priv
;
4224 mc
->igmp_joined
= false;
4225 mc
->join_state
= join_state
;
4226 spin_lock(&id_priv
->lock
);
4227 list_add(&mc
->list
, &id_priv
->mc_list
);
4228 spin_unlock(&id_priv
->lock
);
4230 if (rdma_protocol_roce(id
->device
, id
->port_num
)) {
4231 kref_init(&mc
->mcref
);
4232 ret
= cma_iboe_join_multicast(id_priv
, mc
);
4233 } else if (rdma_cap_ib_mcast(id
->device
, id
->port_num
))
4234 ret
= cma_join_ib_multicast(id_priv
, mc
);
4239 spin_lock_irq(&id_priv
->lock
);
4240 list_del(&mc
->list
);
4241 spin_unlock_irq(&id_priv
->lock
);
4246 EXPORT_SYMBOL(rdma_join_multicast
);
4248 void rdma_leave_multicast(struct rdma_cm_id
*id
, struct sockaddr
*addr
)
4250 struct rdma_id_private
*id_priv
;
4251 struct cma_multicast
*mc
;
4253 id_priv
= container_of(id
, struct rdma_id_private
, id
);
4254 spin_lock_irq(&id_priv
->lock
);
4255 list_for_each_entry(mc
, &id_priv
->mc_list
, list
) {
4256 if (!memcmp(&mc
->addr
, addr
, rdma_addr_size(addr
))) {
4257 list_del(&mc
->list
);
4258 spin_unlock_irq(&id_priv
->lock
);
4261 ib_detach_mcast(id
->qp
,
4262 &mc
->multicast
.ib
->rec
.mgid
,
4263 be16_to_cpu(mc
->multicast
.ib
->rec
.mlid
));
4265 BUG_ON(id_priv
->cma_dev
->device
!= id
->device
);
4267 if (rdma_cap_ib_mcast(id
->device
, id
->port_num
)) {
4268 ib_sa_free_multicast(mc
->multicast
.ib
);
4270 } else if (rdma_protocol_roce(id
->device
, id
->port_num
)) {
4271 if (mc
->igmp_joined
) {
4272 struct rdma_dev_addr
*dev_addr
=
4273 &id
->route
.addr
.dev_addr
;
4274 struct net_device
*ndev
= NULL
;
4276 if (dev_addr
->bound_dev_if
)
4277 ndev
= dev_get_by_index(dev_addr
->net
,
4278 dev_addr
->bound_dev_if
);
4281 &mc
->multicast
.ib
->rec
.mgid
,
4285 mc
->igmp_joined
= false;
4287 kref_put(&mc
->mcref
, release_mc
);
4292 spin_unlock_irq(&id_priv
->lock
);
4294 EXPORT_SYMBOL(rdma_leave_multicast
);
4296 static int cma_netdev_change(struct net_device
*ndev
, struct rdma_id_private
*id_priv
)
4298 struct rdma_dev_addr
*dev_addr
;
4299 struct cma_ndev_work
*work
;
4301 dev_addr
= &id_priv
->id
.route
.addr
.dev_addr
;
4303 if ((dev_addr
->bound_dev_if
== ndev
->ifindex
) &&
4304 (net_eq(dev_net(ndev
), dev_addr
->net
)) &&
4305 memcmp(dev_addr
->src_dev_addr
, ndev
->dev_addr
, ndev
->addr_len
)) {
4306 pr_info("RDMA CM addr change for ndev %s used by id %p\n",
4307 ndev
->name
, &id_priv
->id
);
4308 work
= kzalloc(sizeof *work
, GFP_KERNEL
);
4312 INIT_WORK(&work
->work
, cma_ndev_work_handler
);
4314 work
->event
.event
= RDMA_CM_EVENT_ADDR_CHANGE
;
4315 atomic_inc(&id_priv
->refcount
);
4316 queue_work(cma_wq
, &work
->work
);
4322 static int cma_netdev_callback(struct notifier_block
*self
, unsigned long event
,
4325 struct net_device
*ndev
= netdev_notifier_info_to_dev(ptr
);
4326 struct cma_device
*cma_dev
;
4327 struct rdma_id_private
*id_priv
;
4328 int ret
= NOTIFY_DONE
;
4330 if (event
!= NETDEV_BONDING_FAILOVER
)
4333 if (!netif_is_bond_master(ndev
))
4337 list_for_each_entry(cma_dev
, &dev_list
, list
)
4338 list_for_each_entry(id_priv
, &cma_dev
->id_list
, list
) {
4339 ret
= cma_netdev_change(ndev
, id_priv
);
4345 mutex_unlock(&lock
);
4349 static struct notifier_block cma_nb
= {
4350 .notifier_call
= cma_netdev_callback
4353 static void cma_add_one(struct ib_device
*device
)
4355 struct cma_device
*cma_dev
;
4356 struct rdma_id_private
*id_priv
;
4358 unsigned long supported_gids
= 0;
4360 cma_dev
= kmalloc(sizeof *cma_dev
, GFP_KERNEL
);
4364 cma_dev
->device
= device
;
4365 cma_dev
->default_gid_type
= kcalloc(device
->phys_port_cnt
,
4366 sizeof(*cma_dev
->default_gid_type
),
4368 if (!cma_dev
->default_gid_type
)
4371 cma_dev
->default_roce_tos
= kcalloc(device
->phys_port_cnt
,
4372 sizeof(*cma_dev
->default_roce_tos
),
4374 if (!cma_dev
->default_roce_tos
)
4377 for (i
= rdma_start_port(device
); i
<= rdma_end_port(device
); i
++) {
4378 supported_gids
= roce_gid_type_mask_support(device
, i
);
4379 WARN_ON(!supported_gids
);
4380 if (supported_gids
& (1 << CMA_PREFERRED_ROCE_GID_TYPE
))
4381 cma_dev
->default_gid_type
[i
- rdma_start_port(device
)] =
4382 CMA_PREFERRED_ROCE_GID_TYPE
;
4384 cma_dev
->default_gid_type
[i
- rdma_start_port(device
)] =
4385 find_first_bit(&supported_gids
, BITS_PER_LONG
);
4386 cma_dev
->default_roce_tos
[i
- rdma_start_port(device
)] = 0;
4389 init_completion(&cma_dev
->comp
);
4390 atomic_set(&cma_dev
->refcount
, 1);
4391 INIT_LIST_HEAD(&cma_dev
->id_list
);
4392 ib_set_client_data(device
, &cma_client
, cma_dev
);
4395 list_add_tail(&cma_dev
->list
, &dev_list
);
4396 list_for_each_entry(id_priv
, &listen_any_list
, list
)
4397 cma_listen_on_dev(id_priv
, cma_dev
);
4398 mutex_unlock(&lock
);
4403 kfree(cma_dev
->default_gid_type
);
4411 static int cma_remove_id_dev(struct rdma_id_private
*id_priv
)
4413 struct rdma_cm_event event
;
4414 enum rdma_cm_state state
;
4417 /* Record that we want to remove the device */
4418 state
= cma_exch(id_priv
, RDMA_CM_DEVICE_REMOVAL
);
4419 if (state
== RDMA_CM_DESTROYING
)
4422 cma_cancel_operation(id_priv
, state
);
4423 mutex_lock(&id_priv
->handler_mutex
);
4425 /* Check for destruction from another callback. */
4426 if (!cma_comp(id_priv
, RDMA_CM_DEVICE_REMOVAL
))
4429 memset(&event
, 0, sizeof event
);
4430 event
.event
= RDMA_CM_EVENT_DEVICE_REMOVAL
;
4431 ret
= id_priv
->id
.event_handler(&id_priv
->id
, &event
);
4433 mutex_unlock(&id_priv
->handler_mutex
);
4437 static void cma_process_remove(struct cma_device
*cma_dev
)
4439 struct rdma_id_private
*id_priv
;
4443 while (!list_empty(&cma_dev
->id_list
)) {
4444 id_priv
= list_entry(cma_dev
->id_list
.next
,
4445 struct rdma_id_private
, list
);
4447 list_del(&id_priv
->listen_list
);
4448 list_del_init(&id_priv
->list
);
4449 atomic_inc(&id_priv
->refcount
);
4450 mutex_unlock(&lock
);
4452 ret
= id_priv
->internal_id
? 1 : cma_remove_id_dev(id_priv
);
4453 cma_deref_id(id_priv
);
4455 rdma_destroy_id(&id_priv
->id
);
4459 mutex_unlock(&lock
);
4461 cma_deref_dev(cma_dev
);
4462 wait_for_completion(&cma_dev
->comp
);
4465 static void cma_remove_one(struct ib_device
*device
, void *client_data
)
4467 struct cma_device
*cma_dev
= client_data
;
4473 list_del(&cma_dev
->list
);
4474 mutex_unlock(&lock
);
4476 cma_process_remove(cma_dev
);
4477 kfree(cma_dev
->default_roce_tos
);
4478 kfree(cma_dev
->default_gid_type
);
4482 static int cma_get_id_stats(struct sk_buff
*skb
, struct netlink_callback
*cb
)
4484 struct nlmsghdr
*nlh
;
4485 struct rdma_cm_id_stats
*id_stats
;
4486 struct rdma_id_private
*id_priv
;
4487 struct rdma_cm_id
*id
= NULL
;
4488 struct cma_device
*cma_dev
;
4489 int i_dev
= 0, i_id
= 0;
4492 * We export all of the IDs as a sequence of messages. Each
4493 * ID gets its own netlink message.
4497 list_for_each_entry(cma_dev
, &dev_list
, list
) {
4498 if (i_dev
< cb
->args
[0]) {
4504 list_for_each_entry(id_priv
, &cma_dev
->id_list
, list
) {
4505 if (i_id
< cb
->args
[1]) {
4510 id_stats
= ibnl_put_msg(skb
, &nlh
, cb
->nlh
->nlmsg_seq
,
4511 sizeof *id_stats
, RDMA_NL_RDMA_CM
,
4512 RDMA_NL_RDMA_CM_ID_STATS
,
4517 memset(id_stats
, 0, sizeof *id_stats
);
4519 id_stats
->node_type
= id
->route
.addr
.dev_addr
.dev_type
;
4520 id_stats
->port_num
= id
->port_num
;
4521 id_stats
->bound_dev_if
=
4522 id
->route
.addr
.dev_addr
.bound_dev_if
;
4524 if (ibnl_put_attr(skb
, nlh
,
4525 rdma_addr_size(cma_src_addr(id_priv
)),
4526 cma_src_addr(id_priv
),
4527 RDMA_NL_RDMA_CM_ATTR_SRC_ADDR
))
4529 if (ibnl_put_attr(skb
, nlh
,
4530 rdma_addr_size(cma_dst_addr(id_priv
)),
4531 cma_dst_addr(id_priv
),
4532 RDMA_NL_RDMA_CM_ATTR_DST_ADDR
))
4535 id_stats
->pid
= task_pid_vnr(id_priv
->res
.task
);
4536 id_stats
->port_space
= id
->ps
;
4537 id_stats
->cm_state
= id_priv
->state
;
4538 id_stats
->qp_num
= id_priv
->qp_num
;
4539 id_stats
->qp_type
= id
->qp_type
;
4542 nlmsg_end(skb
, nlh
);
4550 mutex_unlock(&lock
);
4551 cb
->args
[0] = i_dev
;
4557 static const struct rdma_nl_cbs cma_cb_table
[RDMA_NL_RDMA_CM_NUM_OPS
] = {
4558 [RDMA_NL_RDMA_CM_ID_STATS
] = { .dump
= cma_get_id_stats
},
4561 static int cma_init_net(struct net
*net
)
4563 struct cma_pernet
*pernet
= cma_pernet(net
);
4565 idr_init(&pernet
->tcp_ps
);
4566 idr_init(&pernet
->udp_ps
);
4567 idr_init(&pernet
->ipoib_ps
);
4568 idr_init(&pernet
->ib_ps
);
4573 static void cma_exit_net(struct net
*net
)
4575 struct cma_pernet
*pernet
= cma_pernet(net
);
4577 idr_destroy(&pernet
->tcp_ps
);
4578 idr_destroy(&pernet
->udp_ps
);
4579 idr_destroy(&pernet
->ipoib_ps
);
4580 idr_destroy(&pernet
->ib_ps
);
4583 static struct pernet_operations cma_pernet_operations
= {
4584 .init
= cma_init_net
,
4585 .exit
= cma_exit_net
,
4586 .id
= &cma_pernet_id
,
4587 .size
= sizeof(struct cma_pernet
),
4590 static int __init
cma_init(void)
4594 cma_wq
= alloc_ordered_workqueue("rdma_cm", WQ_MEM_RECLAIM
);
4598 ret
= register_pernet_subsys(&cma_pernet_operations
);
4602 ib_sa_register_client(&sa_client
);
4603 register_netdevice_notifier(&cma_nb
);
4605 ret
= ib_register_client(&cma_client
);
4609 rdma_nl_register(RDMA_NL_RDMA_CM
, cma_cb_table
);
4610 cma_configfs_init();
4615 unregister_netdevice_notifier(&cma_nb
);
4616 ib_sa_unregister_client(&sa_client
);
4618 destroy_workqueue(cma_wq
);
4622 static void __exit
cma_cleanup(void)
4624 cma_configfs_exit();
4625 rdma_nl_unregister(RDMA_NL_RDMA_CM
);
4626 ib_unregister_client(&cma_client
);
4627 unregister_netdevice_notifier(&cma_nb
);
4628 ib_sa_unregister_client(&sa_client
);
4629 unregister_pernet_subsys(&cma_pernet_operations
);
4630 destroy_workqueue(cma_wq
);
4633 MODULE_ALIAS_RDMA_NETLINK(RDMA_NL_RDMA_CM
, 1);
4635 module_init(cma_init
);
4636 module_exit(cma_cleanup
);