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"
66 MODULE_AUTHOR("Sean Hefty");
67 MODULE_DESCRIPTION("Generic RDMA CM Agent");
68 MODULE_LICENSE("Dual BSD/GPL");
70 #define CMA_CM_RESPONSE_TIMEOUT 20
71 #define CMA_QUERY_CLASSPORT_INFO_TIMEOUT 3000
72 #define CMA_MAX_CM_RETRIES 15
73 #define CMA_CM_MRA_SETTING (IB_CM_MRA_FLAG_DELAY | 24)
74 #define CMA_IBOE_PACKET_LIFETIME 18
75 #define CMA_PREFERRED_ROCE_GID_TYPE IB_GID_TYPE_ROCE_UDP_ENCAP
77 static const char * const cma_events
[] = {
78 [RDMA_CM_EVENT_ADDR_RESOLVED
] = "address resolved",
79 [RDMA_CM_EVENT_ADDR_ERROR
] = "address error",
80 [RDMA_CM_EVENT_ROUTE_RESOLVED
] = "route resolved ",
81 [RDMA_CM_EVENT_ROUTE_ERROR
] = "route error",
82 [RDMA_CM_EVENT_CONNECT_REQUEST
] = "connect request",
83 [RDMA_CM_EVENT_CONNECT_RESPONSE
] = "connect response",
84 [RDMA_CM_EVENT_CONNECT_ERROR
] = "connect error",
85 [RDMA_CM_EVENT_UNREACHABLE
] = "unreachable",
86 [RDMA_CM_EVENT_REJECTED
] = "rejected",
87 [RDMA_CM_EVENT_ESTABLISHED
] = "established",
88 [RDMA_CM_EVENT_DISCONNECTED
] = "disconnected",
89 [RDMA_CM_EVENT_DEVICE_REMOVAL
] = "device removal",
90 [RDMA_CM_EVENT_MULTICAST_JOIN
] = "multicast join",
91 [RDMA_CM_EVENT_MULTICAST_ERROR
] = "multicast error",
92 [RDMA_CM_EVENT_ADDR_CHANGE
] = "address change",
93 [RDMA_CM_EVENT_TIMEWAIT_EXIT
] = "timewait exit",
96 const char *__attribute_const__
rdma_event_msg(enum rdma_cm_event_type event
)
100 return (index
< ARRAY_SIZE(cma_events
) && cma_events
[index
]) ?
101 cma_events
[index
] : "unrecognized event";
103 EXPORT_SYMBOL(rdma_event_msg
);
105 const char *__attribute_const__
rdma_reject_msg(struct rdma_cm_id
*id
,
108 if (rdma_ib_or_roce(id
->device
, id
->port_num
))
109 return ibcm_reject_msg(reason
);
111 if (rdma_protocol_iwarp(id
->device
, id
->port_num
))
112 return iwcm_reject_msg(reason
);
115 return "unrecognized transport";
117 EXPORT_SYMBOL(rdma_reject_msg
);
119 bool rdma_is_consumer_reject(struct rdma_cm_id
*id
, int reason
)
121 if (rdma_ib_or_roce(id
->device
, id
->port_num
))
122 return reason
== IB_CM_REJ_CONSUMER_DEFINED
;
124 if (rdma_protocol_iwarp(id
->device
, id
->port_num
))
125 return reason
== -ECONNREFUSED
;
130 EXPORT_SYMBOL(rdma_is_consumer_reject
);
132 const void *rdma_consumer_reject_data(struct rdma_cm_id
*id
,
133 struct rdma_cm_event
*ev
, u8
*data_len
)
137 if (rdma_is_consumer_reject(id
, ev
->status
)) {
138 *data_len
= ev
->param
.conn
.private_data_len
;
139 p
= ev
->param
.conn
.private_data
;
146 EXPORT_SYMBOL(rdma_consumer_reject_data
);
148 static void cma_add_one(struct ib_device
*device
);
149 static void cma_remove_one(struct ib_device
*device
, void *client_data
);
151 static struct ib_client cma_client
= {
154 .remove
= cma_remove_one
157 static struct ib_sa_client sa_client
;
158 static struct rdma_addr_client addr_client
;
159 static LIST_HEAD(dev_list
);
160 static LIST_HEAD(listen_any_list
);
161 static DEFINE_MUTEX(lock
);
162 static struct workqueue_struct
*cma_wq
;
163 static unsigned int cma_pernet_id
;
172 static struct cma_pernet
*cma_pernet(struct net
*net
)
174 return net_generic(net
, cma_pernet_id
);
177 static struct idr
*cma_pernet_idr(struct net
*net
, enum rdma_port_space ps
)
179 struct cma_pernet
*pernet
= cma_pernet(net
);
183 return &pernet
->tcp_ps
;
185 return &pernet
->udp_ps
;
187 return &pernet
->ipoib_ps
;
189 return &pernet
->ib_ps
;
196 struct list_head list
;
197 struct ib_device
*device
;
198 struct completion comp
;
200 struct list_head id_list
;
201 enum ib_gid_type
*default_gid_type
;
202 u8
*default_roce_tos
;
205 struct rdma_bind_list
{
206 enum rdma_port_space ps
;
207 struct hlist_head owners
;
211 struct class_port_info_context
{
212 struct ib_class_port_info
*class_port_info
;
213 struct ib_device
*device
;
214 struct completion done
;
215 struct ib_sa_query
*sa_query
;
219 static int cma_ps_alloc(struct net
*net
, enum rdma_port_space ps
,
220 struct rdma_bind_list
*bind_list
, int snum
)
222 struct idr
*idr
= cma_pernet_idr(net
, ps
);
224 return idr_alloc(idr
, bind_list
, snum
, snum
+ 1, GFP_KERNEL
);
227 static struct rdma_bind_list
*cma_ps_find(struct net
*net
,
228 enum rdma_port_space ps
, int snum
)
230 struct idr
*idr
= cma_pernet_idr(net
, ps
);
232 return idr_find(idr
, snum
);
235 static void cma_ps_remove(struct net
*net
, enum rdma_port_space ps
, int snum
)
237 struct idr
*idr
= cma_pernet_idr(net
, ps
);
239 idr_remove(idr
, snum
);
246 void cma_ref_dev(struct cma_device
*cma_dev
)
248 atomic_inc(&cma_dev
->refcount
);
251 struct cma_device
*cma_enum_devices_by_ibdev(cma_device_filter filter
,
254 struct cma_device
*cma_dev
;
255 struct cma_device
*found_cma_dev
= NULL
;
259 list_for_each_entry(cma_dev
, &dev_list
, list
)
260 if (filter(cma_dev
->device
, cookie
)) {
261 found_cma_dev
= cma_dev
;
266 cma_ref_dev(found_cma_dev
);
268 return found_cma_dev
;
271 int cma_get_default_gid_type(struct cma_device
*cma_dev
,
274 if (!rdma_is_port_valid(cma_dev
->device
, port
))
277 return cma_dev
->default_gid_type
[port
- rdma_start_port(cma_dev
->device
)];
280 int cma_set_default_gid_type(struct cma_device
*cma_dev
,
282 enum ib_gid_type default_gid_type
)
284 unsigned long supported_gids
;
286 if (!rdma_is_port_valid(cma_dev
->device
, port
))
289 supported_gids
= roce_gid_type_mask_support(cma_dev
->device
, port
);
291 if (!(supported_gids
& 1 << default_gid_type
))
294 cma_dev
->default_gid_type
[port
- rdma_start_port(cma_dev
->device
)] =
300 int cma_get_default_roce_tos(struct cma_device
*cma_dev
, unsigned int port
)
302 if (!rdma_is_port_valid(cma_dev
->device
, port
))
305 return cma_dev
->default_roce_tos
[port
- rdma_start_port(cma_dev
->device
)];
308 int cma_set_default_roce_tos(struct cma_device
*cma_dev
, unsigned int port
,
311 if (!rdma_is_port_valid(cma_dev
->device
, port
))
314 cma_dev
->default_roce_tos
[port
- rdma_start_port(cma_dev
->device
)] =
319 struct ib_device
*cma_get_ib_dev(struct cma_device
*cma_dev
)
321 return cma_dev
->device
;
325 * Device removal can occur at anytime, so we need extra handling to
326 * serialize notifying the user of device removal with other callbacks.
327 * We do this by disabling removal notification while a callback is in process,
328 * and reporting it after the callback completes.
330 struct rdma_id_private
{
331 struct rdma_cm_id id
;
333 struct rdma_bind_list
*bind_list
;
334 struct hlist_node node
;
335 struct list_head list
; /* listen_any_list or cma_device.list */
336 struct list_head listen_list
; /* per device listens */
337 struct cma_device
*cma_dev
;
338 struct list_head mc_list
;
341 enum rdma_cm_state state
;
343 struct mutex qp_mutex
;
345 struct completion comp
;
347 struct mutex handler_mutex
;
351 struct ib_sa_query
*query
;
368 enum ib_gid_type gid_type
;
371 struct cma_multicast
{
372 struct rdma_id_private
*id_priv
;
374 struct ib_sa_multicast
*ib
;
376 struct list_head list
;
378 struct sockaddr_storage addr
;
385 struct work_struct work
;
386 struct rdma_id_private
*id
;
387 enum rdma_cm_state old_state
;
388 enum rdma_cm_state new_state
;
389 struct rdma_cm_event event
;
392 struct cma_ndev_work
{
393 struct work_struct work
;
394 struct rdma_id_private
*id
;
395 struct rdma_cm_event event
;
398 struct iboe_mcast_work
{
399 struct work_struct work
;
400 struct rdma_id_private
*id
;
401 struct cma_multicast
*mc
;
414 u8 ip_version
; /* IP version: 7:4 */
416 union cma_ip_addr src_addr
;
417 union cma_ip_addr dst_addr
;
420 #define CMA_VERSION 0x00
422 struct cma_req_info
{
423 struct ib_device
*device
;
425 union ib_gid local_gid
;
431 static int cma_comp(struct rdma_id_private
*id_priv
, enum rdma_cm_state comp
)
436 spin_lock_irqsave(&id_priv
->lock
, flags
);
437 ret
= (id_priv
->state
== comp
);
438 spin_unlock_irqrestore(&id_priv
->lock
, flags
);
442 static int cma_comp_exch(struct rdma_id_private
*id_priv
,
443 enum rdma_cm_state comp
, enum rdma_cm_state exch
)
448 spin_lock_irqsave(&id_priv
->lock
, flags
);
449 if ((ret
= (id_priv
->state
== comp
)))
450 id_priv
->state
= exch
;
451 spin_unlock_irqrestore(&id_priv
->lock
, flags
);
455 static enum rdma_cm_state
cma_exch(struct rdma_id_private
*id_priv
,
456 enum rdma_cm_state exch
)
459 enum rdma_cm_state old
;
461 spin_lock_irqsave(&id_priv
->lock
, flags
);
462 old
= id_priv
->state
;
463 id_priv
->state
= exch
;
464 spin_unlock_irqrestore(&id_priv
->lock
, flags
);
468 static inline u8
cma_get_ip_ver(const struct cma_hdr
*hdr
)
470 return hdr
->ip_version
>> 4;
473 static inline void cma_set_ip_ver(struct cma_hdr
*hdr
, u8 ip_ver
)
475 hdr
->ip_version
= (ip_ver
<< 4) | (hdr
->ip_version
& 0xF);
478 static int cma_igmp_send(struct net_device
*ndev
, union ib_gid
*mgid
, bool join
)
480 struct in_device
*in_dev
= NULL
;
484 in_dev
= __in_dev_get_rtnl(ndev
);
487 ip_mc_inc_group(in_dev
,
488 *(__be32
*)(mgid
->raw
+ 12));
490 ip_mc_dec_group(in_dev
,
491 *(__be32
*)(mgid
->raw
+ 12));
495 return (in_dev
) ? 0 : -ENODEV
;
498 static void _cma_attach_to_dev(struct rdma_id_private
*id_priv
,
499 struct cma_device
*cma_dev
)
501 cma_ref_dev(cma_dev
);
502 id_priv
->cma_dev
= cma_dev
;
503 id_priv
->gid_type
= 0;
504 id_priv
->id
.device
= cma_dev
->device
;
505 id_priv
->id
.route
.addr
.dev_addr
.transport
=
506 rdma_node_get_transport(cma_dev
->device
->node_type
);
507 list_add_tail(&id_priv
->list
, &cma_dev
->id_list
);
510 static void cma_attach_to_dev(struct rdma_id_private
*id_priv
,
511 struct cma_device
*cma_dev
)
513 _cma_attach_to_dev(id_priv
, cma_dev
);
515 cma_dev
->default_gid_type
[id_priv
->id
.port_num
-
516 rdma_start_port(cma_dev
->device
)];
519 void cma_deref_dev(struct cma_device
*cma_dev
)
521 if (atomic_dec_and_test(&cma_dev
->refcount
))
522 complete(&cma_dev
->comp
);
525 static inline void release_mc(struct kref
*kref
)
527 struct cma_multicast
*mc
= container_of(kref
, struct cma_multicast
, mcref
);
529 kfree(mc
->multicast
.ib
);
533 static void cma_release_dev(struct rdma_id_private
*id_priv
)
536 list_del(&id_priv
->list
);
537 cma_deref_dev(id_priv
->cma_dev
);
538 id_priv
->cma_dev
= NULL
;
542 static inline struct sockaddr
*cma_src_addr(struct rdma_id_private
*id_priv
)
544 return (struct sockaddr
*) &id_priv
->id
.route
.addr
.src_addr
;
547 static inline struct sockaddr
*cma_dst_addr(struct rdma_id_private
*id_priv
)
549 return (struct sockaddr
*) &id_priv
->id
.route
.addr
.dst_addr
;
552 static inline unsigned short cma_family(struct rdma_id_private
*id_priv
)
554 return id_priv
->id
.route
.addr
.src_addr
.ss_family
;
557 static int cma_set_qkey(struct rdma_id_private
*id_priv
, u32 qkey
)
559 struct ib_sa_mcmember_rec rec
;
563 if (qkey
&& id_priv
->qkey
!= qkey
)
569 id_priv
->qkey
= qkey
;
573 switch (id_priv
->id
.ps
) {
576 id_priv
->qkey
= RDMA_UDP_QKEY
;
579 ib_addr_get_mgid(&id_priv
->id
.route
.addr
.dev_addr
, &rec
.mgid
);
580 ret
= ib_sa_get_mcmember_rec(id_priv
->id
.device
,
581 id_priv
->id
.port_num
, &rec
.mgid
,
584 id_priv
->qkey
= be32_to_cpu(rec
.qkey
);
592 static void cma_translate_ib(struct sockaddr_ib
*sib
, struct rdma_dev_addr
*dev_addr
)
594 dev_addr
->dev_type
= ARPHRD_INFINIBAND
;
595 rdma_addr_set_sgid(dev_addr
, (union ib_gid
*) &sib
->sib_addr
);
596 ib_addr_set_pkey(dev_addr
, ntohs(sib
->sib_pkey
));
599 static int cma_translate_addr(struct sockaddr
*addr
, struct rdma_dev_addr
*dev_addr
)
603 if (addr
->sa_family
!= AF_IB
) {
604 ret
= rdma_translate_ip(addr
, dev_addr
);
606 cma_translate_ib((struct sockaddr_ib
*) addr
, dev_addr
);
613 static inline int cma_validate_port(struct ib_device
*device
, u8 port
,
614 enum ib_gid_type gid_type
,
616 struct rdma_id_private
*id_priv
)
618 struct rdma_dev_addr
*dev_addr
= &id_priv
->id
.route
.addr
.dev_addr
;
619 int bound_if_index
= dev_addr
->bound_dev_if
;
620 int dev_type
= dev_addr
->dev_type
;
621 struct net_device
*ndev
= NULL
;
624 if ((dev_type
== ARPHRD_INFINIBAND
) && !rdma_protocol_ib(device
, port
))
627 if ((dev_type
!= ARPHRD_INFINIBAND
) && rdma_protocol_ib(device
, port
))
630 if (dev_type
== ARPHRD_ETHER
&& rdma_protocol_roce(device
, port
)) {
631 ndev
= dev_get_by_index(dev_addr
->net
, bound_if_index
);
635 gid_type
= IB_GID_TYPE_IB
;
638 ret
= ib_find_cached_gid_by_port(device
, gid
, gid_type
, port
,
647 static int cma_acquire_dev(struct rdma_id_private
*id_priv
,
648 struct rdma_id_private
*listen_id_priv
)
650 struct rdma_dev_addr
*dev_addr
= &id_priv
->id
.route
.addr
.dev_addr
;
651 struct cma_device
*cma_dev
;
652 union ib_gid gid
, iboe_gid
, *gidp
;
656 if (dev_addr
->dev_type
!= ARPHRD_INFINIBAND
&&
657 id_priv
->id
.ps
== RDMA_PS_IPOIB
)
661 rdma_ip2gid((struct sockaddr
*)&id_priv
->id
.route
.addr
.src_addr
,
664 memcpy(&gid
, dev_addr
->src_dev_addr
+
665 rdma_addr_gid_offset(dev_addr
), sizeof gid
);
667 if (listen_id_priv
) {
668 cma_dev
= listen_id_priv
->cma_dev
;
669 port
= listen_id_priv
->id
.port_num
;
670 gidp
= rdma_protocol_roce(cma_dev
->device
, port
) ?
673 ret
= cma_validate_port(cma_dev
->device
, port
,
674 rdma_protocol_ib(cma_dev
->device
, port
) ?
676 listen_id_priv
->gid_type
, gidp
,
679 id_priv
->id
.port_num
= port
;
684 list_for_each_entry(cma_dev
, &dev_list
, list
) {
685 for (port
= 1; port
<= cma_dev
->device
->phys_port_cnt
; ++port
) {
686 if (listen_id_priv
&&
687 listen_id_priv
->cma_dev
== cma_dev
&&
688 listen_id_priv
->id
.port_num
== port
)
691 gidp
= rdma_protocol_roce(cma_dev
->device
, port
) ?
694 ret
= cma_validate_port(cma_dev
->device
, port
,
695 rdma_protocol_ib(cma_dev
->device
, port
) ?
697 cma_dev
->default_gid_type
[port
- 1],
700 id_priv
->id
.port_num
= port
;
708 cma_attach_to_dev(id_priv
, cma_dev
);
715 * Select the source IB device and address to reach the destination IB address.
717 static int cma_resolve_ib_dev(struct rdma_id_private
*id_priv
)
719 struct cma_device
*cma_dev
, *cur_dev
;
720 struct sockaddr_ib
*addr
;
721 union ib_gid gid
, sgid
, *dgid
;
724 enum ib_port_state port_state
;
728 addr
= (struct sockaddr_ib
*) cma_dst_addr(id_priv
);
729 dgid
= (union ib_gid
*) &addr
->sib_addr
;
730 pkey
= ntohs(addr
->sib_pkey
);
732 list_for_each_entry(cur_dev
, &dev_list
, list
) {
733 for (p
= 1; p
<= cur_dev
->device
->phys_port_cnt
; ++p
) {
734 if (!rdma_cap_af_ib(cur_dev
->device
, p
))
737 if (ib_find_cached_pkey(cur_dev
->device
, p
, pkey
, &index
))
740 if (ib_get_cached_port_state(cur_dev
->device
, p
, &port_state
))
742 for (i
= 0; !ib_get_cached_gid(cur_dev
->device
, p
, i
,
745 if (!memcmp(&gid
, dgid
, sizeof(gid
))) {
748 id_priv
->id
.port_num
= p
;
752 if (!cma_dev
&& (gid
.global
.subnet_prefix
==
753 dgid
->global
.subnet_prefix
) &&
754 port_state
== IB_PORT_ACTIVE
) {
757 id_priv
->id
.port_num
= p
;
767 cma_attach_to_dev(id_priv
, cma_dev
);
768 addr
= (struct sockaddr_ib
*) cma_src_addr(id_priv
);
769 memcpy(&addr
->sib_addr
, &sgid
, sizeof sgid
);
770 cma_translate_ib(addr
, &id_priv
->id
.route
.addr
.dev_addr
);
774 static void cma_deref_id(struct rdma_id_private
*id_priv
)
776 if (atomic_dec_and_test(&id_priv
->refcount
))
777 complete(&id_priv
->comp
);
780 struct rdma_cm_id
*rdma_create_id(struct net
*net
,
781 rdma_cm_event_handler event_handler
,
782 void *context
, enum rdma_port_space ps
,
783 enum ib_qp_type qp_type
)
785 struct rdma_id_private
*id_priv
;
787 id_priv
= kzalloc(sizeof *id_priv
, GFP_KERNEL
);
789 return ERR_PTR(-ENOMEM
);
791 id_priv
->owner
= task_pid_nr(current
);
792 id_priv
->state
= RDMA_CM_IDLE
;
793 id_priv
->id
.context
= context
;
794 id_priv
->id
.event_handler
= event_handler
;
796 id_priv
->id
.qp_type
= qp_type
;
797 id_priv
->tos_set
= false;
798 spin_lock_init(&id_priv
->lock
);
799 mutex_init(&id_priv
->qp_mutex
);
800 init_completion(&id_priv
->comp
);
801 atomic_set(&id_priv
->refcount
, 1);
802 mutex_init(&id_priv
->handler_mutex
);
803 INIT_LIST_HEAD(&id_priv
->listen_list
);
804 INIT_LIST_HEAD(&id_priv
->mc_list
);
805 get_random_bytes(&id_priv
->seq_num
, sizeof id_priv
->seq_num
);
806 id_priv
->id
.route
.addr
.dev_addr
.net
= get_net(net
);
807 id_priv
->seq_num
&= 0x00ffffff;
811 EXPORT_SYMBOL(rdma_create_id
);
813 static int cma_init_ud_qp(struct rdma_id_private
*id_priv
, struct ib_qp
*qp
)
815 struct ib_qp_attr qp_attr
;
816 int qp_attr_mask
, ret
;
818 qp_attr
.qp_state
= IB_QPS_INIT
;
819 ret
= rdma_init_qp_attr(&id_priv
->id
, &qp_attr
, &qp_attr_mask
);
823 ret
= ib_modify_qp(qp
, &qp_attr
, qp_attr_mask
);
827 qp_attr
.qp_state
= IB_QPS_RTR
;
828 ret
= ib_modify_qp(qp
, &qp_attr
, IB_QP_STATE
);
832 qp_attr
.qp_state
= IB_QPS_RTS
;
834 ret
= ib_modify_qp(qp
, &qp_attr
, IB_QP_STATE
| IB_QP_SQ_PSN
);
839 static int cma_init_conn_qp(struct rdma_id_private
*id_priv
, struct ib_qp
*qp
)
841 struct ib_qp_attr qp_attr
;
842 int qp_attr_mask
, ret
;
844 qp_attr
.qp_state
= IB_QPS_INIT
;
845 ret
= rdma_init_qp_attr(&id_priv
->id
, &qp_attr
, &qp_attr_mask
);
849 return ib_modify_qp(qp
, &qp_attr
, qp_attr_mask
);
852 int rdma_create_qp(struct rdma_cm_id
*id
, struct ib_pd
*pd
,
853 struct ib_qp_init_attr
*qp_init_attr
)
855 struct rdma_id_private
*id_priv
;
859 id_priv
= container_of(id
, struct rdma_id_private
, id
);
860 if (id
->device
!= pd
->device
)
863 qp_init_attr
->port_num
= id
->port_num
;
864 qp
= ib_create_qp(pd
, qp_init_attr
);
868 if (id
->qp_type
== IB_QPT_UD
)
869 ret
= cma_init_ud_qp(id_priv
, qp
);
871 ret
= cma_init_conn_qp(id_priv
, qp
);
876 id_priv
->qp_num
= qp
->qp_num
;
877 id_priv
->srq
= (qp
->srq
!= NULL
);
883 EXPORT_SYMBOL(rdma_create_qp
);
885 void rdma_destroy_qp(struct rdma_cm_id
*id
)
887 struct rdma_id_private
*id_priv
;
889 id_priv
= container_of(id
, struct rdma_id_private
, id
);
890 mutex_lock(&id_priv
->qp_mutex
);
891 ib_destroy_qp(id_priv
->id
.qp
);
892 id_priv
->id
.qp
= NULL
;
893 mutex_unlock(&id_priv
->qp_mutex
);
895 EXPORT_SYMBOL(rdma_destroy_qp
);
897 static int cma_modify_qp_rtr(struct rdma_id_private
*id_priv
,
898 struct rdma_conn_param
*conn_param
)
900 struct ib_qp_attr qp_attr
;
901 int qp_attr_mask
, ret
;
904 mutex_lock(&id_priv
->qp_mutex
);
905 if (!id_priv
->id
.qp
) {
910 /* Need to update QP attributes from default values. */
911 qp_attr
.qp_state
= IB_QPS_INIT
;
912 ret
= rdma_init_qp_attr(&id_priv
->id
, &qp_attr
, &qp_attr_mask
);
916 ret
= ib_modify_qp(id_priv
->id
.qp
, &qp_attr
, qp_attr_mask
);
920 qp_attr
.qp_state
= IB_QPS_RTR
;
921 ret
= rdma_init_qp_attr(&id_priv
->id
, &qp_attr
, &qp_attr_mask
);
925 ret
= ib_query_gid(id_priv
->id
.device
, id_priv
->id
.port_num
,
926 rdma_ah_read_grh(&qp_attr
.ah_attr
)->sgid_index
,
931 BUG_ON(id_priv
->cma_dev
->device
!= id_priv
->id
.device
);
934 qp_attr
.max_dest_rd_atomic
= conn_param
->responder_resources
;
935 ret
= ib_modify_qp(id_priv
->id
.qp
, &qp_attr
, qp_attr_mask
);
937 mutex_unlock(&id_priv
->qp_mutex
);
941 static int cma_modify_qp_rts(struct rdma_id_private
*id_priv
,
942 struct rdma_conn_param
*conn_param
)
944 struct ib_qp_attr qp_attr
;
945 int qp_attr_mask
, ret
;
947 mutex_lock(&id_priv
->qp_mutex
);
948 if (!id_priv
->id
.qp
) {
953 qp_attr
.qp_state
= IB_QPS_RTS
;
954 ret
= rdma_init_qp_attr(&id_priv
->id
, &qp_attr
, &qp_attr_mask
);
959 qp_attr
.max_rd_atomic
= conn_param
->initiator_depth
;
960 ret
= ib_modify_qp(id_priv
->id
.qp
, &qp_attr
, qp_attr_mask
);
962 mutex_unlock(&id_priv
->qp_mutex
);
966 static int cma_modify_qp_err(struct rdma_id_private
*id_priv
)
968 struct ib_qp_attr qp_attr
;
971 mutex_lock(&id_priv
->qp_mutex
);
972 if (!id_priv
->id
.qp
) {
977 qp_attr
.qp_state
= IB_QPS_ERR
;
978 ret
= ib_modify_qp(id_priv
->id
.qp
, &qp_attr
, IB_QP_STATE
);
980 mutex_unlock(&id_priv
->qp_mutex
);
984 static int cma_ib_init_qp_attr(struct rdma_id_private
*id_priv
,
985 struct ib_qp_attr
*qp_attr
, int *qp_attr_mask
)
987 struct rdma_dev_addr
*dev_addr
= &id_priv
->id
.route
.addr
.dev_addr
;
991 if (rdma_cap_eth_ah(id_priv
->id
.device
, id_priv
->id
.port_num
))
994 pkey
= ib_addr_get_pkey(dev_addr
);
996 ret
= ib_find_cached_pkey(id_priv
->id
.device
, id_priv
->id
.port_num
,
997 pkey
, &qp_attr
->pkey_index
);
1001 qp_attr
->port_num
= id_priv
->id
.port_num
;
1002 *qp_attr_mask
= IB_QP_STATE
| IB_QP_PKEY_INDEX
| IB_QP_PORT
;
1004 if (id_priv
->id
.qp_type
== IB_QPT_UD
) {
1005 ret
= cma_set_qkey(id_priv
, 0);
1009 qp_attr
->qkey
= id_priv
->qkey
;
1010 *qp_attr_mask
|= IB_QP_QKEY
;
1012 qp_attr
->qp_access_flags
= 0;
1013 *qp_attr_mask
|= IB_QP_ACCESS_FLAGS
;
1018 int rdma_init_qp_attr(struct rdma_cm_id
*id
, struct ib_qp_attr
*qp_attr
,
1021 struct rdma_id_private
*id_priv
;
1024 id_priv
= container_of(id
, struct rdma_id_private
, id
);
1025 if (rdma_cap_ib_cm(id
->device
, id
->port_num
)) {
1026 if (!id_priv
->cm_id
.ib
|| (id_priv
->id
.qp_type
== IB_QPT_UD
))
1027 ret
= cma_ib_init_qp_attr(id_priv
, qp_attr
, qp_attr_mask
);
1029 ret
= ib_cm_init_qp_attr(id_priv
->cm_id
.ib
, qp_attr
,
1032 if (qp_attr
->qp_state
== IB_QPS_RTR
)
1033 qp_attr
->rq_psn
= id_priv
->seq_num
;
1034 } else if (rdma_cap_iw_cm(id
->device
, id
->port_num
)) {
1035 if (!id_priv
->cm_id
.iw
) {
1036 qp_attr
->qp_access_flags
= 0;
1037 *qp_attr_mask
= IB_QP_STATE
| IB_QP_ACCESS_FLAGS
;
1039 ret
= iw_cm_init_qp_attr(id_priv
->cm_id
.iw
, qp_attr
,
1041 qp_attr
->port_num
= id_priv
->id
.port_num
;
1042 *qp_attr_mask
|= IB_QP_PORT
;
1048 EXPORT_SYMBOL(rdma_init_qp_attr
);
1050 static inline int cma_zero_addr(struct sockaddr
*addr
)
1052 switch (addr
->sa_family
) {
1054 return ipv4_is_zeronet(((struct sockaddr_in
*)addr
)->sin_addr
.s_addr
);
1056 return ipv6_addr_any(&((struct sockaddr_in6
*) addr
)->sin6_addr
);
1058 return ib_addr_any(&((struct sockaddr_ib
*) addr
)->sib_addr
);
1064 static inline int cma_loopback_addr(struct sockaddr
*addr
)
1066 switch (addr
->sa_family
) {
1068 return ipv4_is_loopback(((struct sockaddr_in
*) addr
)->sin_addr
.s_addr
);
1070 return ipv6_addr_loopback(&((struct sockaddr_in6
*) addr
)->sin6_addr
);
1072 return ib_addr_loopback(&((struct sockaddr_ib
*) addr
)->sib_addr
);
1078 static inline int cma_any_addr(struct sockaddr
*addr
)
1080 return cma_zero_addr(addr
) || cma_loopback_addr(addr
);
1083 static int cma_addr_cmp(struct sockaddr
*src
, struct sockaddr
*dst
)
1085 if (src
->sa_family
!= dst
->sa_family
)
1088 switch (src
->sa_family
) {
1090 return ((struct sockaddr_in
*) src
)->sin_addr
.s_addr
!=
1091 ((struct sockaddr_in
*) dst
)->sin_addr
.s_addr
;
1093 return ipv6_addr_cmp(&((struct sockaddr_in6
*) src
)->sin6_addr
,
1094 &((struct sockaddr_in6
*) dst
)->sin6_addr
);
1096 return ib_addr_cmp(&((struct sockaddr_ib
*) src
)->sib_addr
,
1097 &((struct sockaddr_ib
*) dst
)->sib_addr
);
1101 static __be16
cma_port(struct sockaddr
*addr
)
1103 struct sockaddr_ib
*sib
;
1105 switch (addr
->sa_family
) {
1107 return ((struct sockaddr_in
*) addr
)->sin_port
;
1109 return ((struct sockaddr_in6
*) addr
)->sin6_port
;
1111 sib
= (struct sockaddr_ib
*) addr
;
1112 return htons((u16
) (be64_to_cpu(sib
->sib_sid
) &
1113 be64_to_cpu(sib
->sib_sid_mask
)));
1119 static inline int cma_any_port(struct sockaddr
*addr
)
1121 return !cma_port(addr
);
1124 static void cma_save_ib_info(struct sockaddr
*src_addr
,
1125 struct sockaddr
*dst_addr
,
1126 struct rdma_cm_id
*listen_id
,
1127 struct sa_path_rec
*path
)
1129 struct sockaddr_ib
*listen_ib
, *ib
;
1131 listen_ib
= (struct sockaddr_ib
*) &listen_id
->route
.addr
.src_addr
;
1133 ib
= (struct sockaddr_ib
*)src_addr
;
1134 ib
->sib_family
= AF_IB
;
1136 ib
->sib_pkey
= path
->pkey
;
1137 ib
->sib_flowinfo
= path
->flow_label
;
1138 memcpy(&ib
->sib_addr
, &path
->sgid
, 16);
1139 ib
->sib_sid
= path
->service_id
;
1140 ib
->sib_scope_id
= 0;
1142 ib
->sib_pkey
= listen_ib
->sib_pkey
;
1143 ib
->sib_flowinfo
= listen_ib
->sib_flowinfo
;
1144 ib
->sib_addr
= listen_ib
->sib_addr
;
1145 ib
->sib_sid
= listen_ib
->sib_sid
;
1146 ib
->sib_scope_id
= listen_ib
->sib_scope_id
;
1148 ib
->sib_sid_mask
= cpu_to_be64(0xffffffffffffffffULL
);
1151 ib
= (struct sockaddr_ib
*)dst_addr
;
1152 ib
->sib_family
= AF_IB
;
1154 ib
->sib_pkey
= path
->pkey
;
1155 ib
->sib_flowinfo
= path
->flow_label
;
1156 memcpy(&ib
->sib_addr
, &path
->dgid
, 16);
1161 static void cma_save_ip4_info(struct sockaddr_in
*src_addr
,
1162 struct sockaddr_in
*dst_addr
,
1163 struct cma_hdr
*hdr
,
1167 *src_addr
= (struct sockaddr_in
) {
1168 .sin_family
= AF_INET
,
1169 .sin_addr
.s_addr
= hdr
->dst_addr
.ip4
.addr
,
1170 .sin_port
= local_port
,
1175 *dst_addr
= (struct sockaddr_in
) {
1176 .sin_family
= AF_INET
,
1177 .sin_addr
.s_addr
= hdr
->src_addr
.ip4
.addr
,
1178 .sin_port
= hdr
->port
,
1183 static void cma_save_ip6_info(struct sockaddr_in6
*src_addr
,
1184 struct sockaddr_in6
*dst_addr
,
1185 struct cma_hdr
*hdr
,
1189 *src_addr
= (struct sockaddr_in6
) {
1190 .sin6_family
= AF_INET6
,
1191 .sin6_addr
= hdr
->dst_addr
.ip6
,
1192 .sin6_port
= local_port
,
1197 *dst_addr
= (struct sockaddr_in6
) {
1198 .sin6_family
= AF_INET6
,
1199 .sin6_addr
= hdr
->src_addr
.ip6
,
1200 .sin6_port
= hdr
->port
,
1205 static u16
cma_port_from_service_id(__be64 service_id
)
1207 return (u16
)be64_to_cpu(service_id
);
1210 static int cma_save_ip_info(struct sockaddr
*src_addr
,
1211 struct sockaddr
*dst_addr
,
1212 struct ib_cm_event
*ib_event
,
1215 struct cma_hdr
*hdr
;
1218 hdr
= ib_event
->private_data
;
1219 if (hdr
->cma_version
!= CMA_VERSION
)
1222 port
= htons(cma_port_from_service_id(service_id
));
1224 switch (cma_get_ip_ver(hdr
)) {
1226 cma_save_ip4_info((struct sockaddr_in
*)src_addr
,
1227 (struct sockaddr_in
*)dst_addr
, hdr
, port
);
1230 cma_save_ip6_info((struct sockaddr_in6
*)src_addr
,
1231 (struct sockaddr_in6
*)dst_addr
, hdr
, port
);
1234 return -EAFNOSUPPORT
;
1240 static int cma_save_net_info(struct sockaddr
*src_addr
,
1241 struct sockaddr
*dst_addr
,
1242 struct rdma_cm_id
*listen_id
,
1243 struct ib_cm_event
*ib_event
,
1244 sa_family_t sa_family
, __be64 service_id
)
1246 if (sa_family
== AF_IB
) {
1247 if (ib_event
->event
== IB_CM_REQ_RECEIVED
)
1248 cma_save_ib_info(src_addr
, dst_addr
, listen_id
,
1249 ib_event
->param
.req_rcvd
.primary_path
);
1250 else if (ib_event
->event
== IB_CM_SIDR_REQ_RECEIVED
)
1251 cma_save_ib_info(src_addr
, dst_addr
, listen_id
, NULL
);
1255 return cma_save_ip_info(src_addr
, dst_addr
, ib_event
, service_id
);
1258 static int cma_save_req_info(const struct ib_cm_event
*ib_event
,
1259 struct cma_req_info
*req
)
1261 const struct ib_cm_req_event_param
*req_param
=
1262 &ib_event
->param
.req_rcvd
;
1263 const struct ib_cm_sidr_req_event_param
*sidr_param
=
1264 &ib_event
->param
.sidr_req_rcvd
;
1266 switch (ib_event
->event
) {
1267 case IB_CM_REQ_RECEIVED
:
1268 req
->device
= req_param
->listen_id
->device
;
1269 req
->port
= req_param
->port
;
1270 memcpy(&req
->local_gid
, &req_param
->primary_path
->sgid
,
1271 sizeof(req
->local_gid
));
1272 req
->has_gid
= true;
1273 req
->service_id
= req_param
->primary_path
->service_id
;
1274 req
->pkey
= be16_to_cpu(req_param
->primary_path
->pkey
);
1275 if (req
->pkey
!= req_param
->bth_pkey
)
1276 pr_warn_ratelimited("RDMA CMA: got different BTH P_Key (0x%x) and primary path P_Key (0x%x)\n"
1277 "RDMA CMA: in the future this may cause the request to be dropped\n",
1278 req_param
->bth_pkey
, req
->pkey
);
1280 case IB_CM_SIDR_REQ_RECEIVED
:
1281 req
->device
= sidr_param
->listen_id
->device
;
1282 req
->port
= sidr_param
->port
;
1283 req
->has_gid
= false;
1284 req
->service_id
= sidr_param
->service_id
;
1285 req
->pkey
= sidr_param
->pkey
;
1286 if (req
->pkey
!= sidr_param
->bth_pkey
)
1287 pr_warn_ratelimited("RDMA CMA: got different BTH P_Key (0x%x) and SIDR request payload P_Key (0x%x)\n"
1288 "RDMA CMA: in the future this may cause the request to be dropped\n",
1289 sidr_param
->bth_pkey
, req
->pkey
);
1298 static bool validate_ipv4_net_dev(struct net_device
*net_dev
,
1299 const struct sockaddr_in
*dst_addr
,
1300 const struct sockaddr_in
*src_addr
)
1302 __be32 daddr
= dst_addr
->sin_addr
.s_addr
,
1303 saddr
= src_addr
->sin_addr
.s_addr
;
1304 struct fib_result res
;
1309 if (ipv4_is_multicast(saddr
) || ipv4_is_lbcast(saddr
) ||
1310 ipv4_is_lbcast(daddr
) || ipv4_is_zeronet(saddr
) ||
1311 ipv4_is_zeronet(daddr
) || ipv4_is_loopback(daddr
) ||
1312 ipv4_is_loopback(saddr
))
1315 memset(&fl4
, 0, sizeof(fl4
));
1316 fl4
.flowi4_iif
= net_dev
->ifindex
;
1321 err
= fib_lookup(dev_net(net_dev
), &fl4
, &res
, 0);
1322 ret
= err
== 0 && FIB_RES_DEV(res
) == net_dev
;
1328 static bool validate_ipv6_net_dev(struct net_device
*net_dev
,
1329 const struct sockaddr_in6
*dst_addr
,
1330 const struct sockaddr_in6
*src_addr
)
1332 #if IS_ENABLED(CONFIG_IPV6)
1333 const int strict
= ipv6_addr_type(&dst_addr
->sin6_addr
) &
1334 IPV6_ADDR_LINKLOCAL
;
1335 struct rt6_info
*rt
= rt6_lookup(dev_net(net_dev
), &dst_addr
->sin6_addr
,
1336 &src_addr
->sin6_addr
, net_dev
->ifindex
,
1343 ret
= rt
->rt6i_idev
->dev
== net_dev
;
1352 static bool validate_net_dev(struct net_device
*net_dev
,
1353 const struct sockaddr
*daddr
,
1354 const struct sockaddr
*saddr
)
1356 const struct sockaddr_in
*daddr4
= (const struct sockaddr_in
*)daddr
;
1357 const struct sockaddr_in
*saddr4
= (const struct sockaddr_in
*)saddr
;
1358 const struct sockaddr_in6
*daddr6
= (const struct sockaddr_in6
*)daddr
;
1359 const struct sockaddr_in6
*saddr6
= (const struct sockaddr_in6
*)saddr
;
1361 switch (daddr
->sa_family
) {
1363 return saddr
->sa_family
== AF_INET
&&
1364 validate_ipv4_net_dev(net_dev
, daddr4
, saddr4
);
1367 return saddr
->sa_family
== AF_INET6
&&
1368 validate_ipv6_net_dev(net_dev
, daddr6
, saddr6
);
1375 static struct net_device
*cma_get_net_dev(struct ib_cm_event
*ib_event
,
1376 const struct cma_req_info
*req
)
1378 struct sockaddr_storage listen_addr_storage
, src_addr_storage
;
1379 struct sockaddr
*listen_addr
= (struct sockaddr
*)&listen_addr_storage
,
1380 *src_addr
= (struct sockaddr
*)&src_addr_storage
;
1381 struct net_device
*net_dev
;
1382 const union ib_gid
*gid
= req
->has_gid
? &req
->local_gid
: NULL
;
1385 err
= cma_save_ip_info(listen_addr
, src_addr
, ib_event
,
1388 return ERR_PTR(err
);
1390 net_dev
= ib_get_net_dev_by_params(req
->device
, req
->port
, req
->pkey
,
1393 return ERR_PTR(-ENODEV
);
1395 if (!validate_net_dev(net_dev
, listen_addr
, src_addr
)) {
1397 return ERR_PTR(-EHOSTUNREACH
);
1403 static enum rdma_port_space
rdma_ps_from_service_id(__be64 service_id
)
1405 return (be64_to_cpu(service_id
) >> 16) & 0xffff;
1408 static bool cma_match_private_data(struct rdma_id_private
*id_priv
,
1409 const struct cma_hdr
*hdr
)
1411 struct sockaddr
*addr
= cma_src_addr(id_priv
);
1413 struct in6_addr ip6_addr
;
1415 if (cma_any_addr(addr
) && !id_priv
->afonly
)
1418 switch (addr
->sa_family
) {
1420 ip4_addr
= ((struct sockaddr_in
*)addr
)->sin_addr
.s_addr
;
1421 if (cma_get_ip_ver(hdr
) != 4)
1423 if (!cma_any_addr(addr
) &&
1424 hdr
->dst_addr
.ip4
.addr
!= ip4_addr
)
1428 ip6_addr
= ((struct sockaddr_in6
*)addr
)->sin6_addr
;
1429 if (cma_get_ip_ver(hdr
) != 6)
1431 if (!cma_any_addr(addr
) &&
1432 memcmp(&hdr
->dst_addr
.ip6
, &ip6_addr
, sizeof(ip6_addr
)))
1444 static bool cma_protocol_roce_dev_port(struct ib_device
*device
, int port_num
)
1446 enum rdma_link_layer ll
= rdma_port_get_link_layer(device
, port_num
);
1447 enum rdma_transport_type transport
=
1448 rdma_node_get_transport(device
->node_type
);
1450 return ll
== IB_LINK_LAYER_ETHERNET
&& transport
== RDMA_TRANSPORT_IB
;
1453 static bool cma_protocol_roce(const struct rdma_cm_id
*id
)
1455 struct ib_device
*device
= id
->device
;
1456 const int port_num
= id
->port_num
?: rdma_start_port(device
);
1458 return cma_protocol_roce_dev_port(device
, port_num
);
1461 static bool cma_match_net_dev(const struct rdma_cm_id
*id
,
1462 const struct net_device
*net_dev
,
1465 const struct rdma_addr
*addr
= &id
->route
.addr
;
1468 /* This request is an AF_IB request or a RoCE request */
1469 return (!id
->port_num
|| id
->port_num
== port_num
) &&
1470 (addr
->src_addr
.ss_family
== AF_IB
||
1471 cma_protocol_roce_dev_port(id
->device
, port_num
));
1473 return !addr
->dev_addr
.bound_dev_if
||
1474 (net_eq(dev_net(net_dev
), addr
->dev_addr
.net
) &&
1475 addr
->dev_addr
.bound_dev_if
== net_dev
->ifindex
);
1478 static struct rdma_id_private
*cma_find_listener(
1479 const struct rdma_bind_list
*bind_list
,
1480 const struct ib_cm_id
*cm_id
,
1481 const struct ib_cm_event
*ib_event
,
1482 const struct cma_req_info
*req
,
1483 const struct net_device
*net_dev
)
1485 struct rdma_id_private
*id_priv
, *id_priv_dev
;
1488 return ERR_PTR(-EINVAL
);
1490 hlist_for_each_entry(id_priv
, &bind_list
->owners
, node
) {
1491 if (cma_match_private_data(id_priv
, ib_event
->private_data
)) {
1492 if (id_priv
->id
.device
== cm_id
->device
&&
1493 cma_match_net_dev(&id_priv
->id
, net_dev
, req
->port
))
1495 list_for_each_entry(id_priv_dev
,
1496 &id_priv
->listen_list
,
1498 if (id_priv_dev
->id
.device
== cm_id
->device
&&
1499 cma_match_net_dev(&id_priv_dev
->id
, net_dev
, req
->port
))
1505 return ERR_PTR(-EINVAL
);
1508 static struct rdma_id_private
*cma_id_from_event(struct ib_cm_id
*cm_id
,
1509 struct ib_cm_event
*ib_event
,
1510 struct net_device
**net_dev
)
1512 struct cma_req_info req
;
1513 struct rdma_bind_list
*bind_list
;
1514 struct rdma_id_private
*id_priv
;
1517 err
= cma_save_req_info(ib_event
, &req
);
1519 return ERR_PTR(err
);
1521 *net_dev
= cma_get_net_dev(ib_event
, &req
);
1522 if (IS_ERR(*net_dev
)) {
1523 if (PTR_ERR(*net_dev
) == -EAFNOSUPPORT
) {
1524 /* Assuming the protocol is AF_IB */
1526 } else if (cma_protocol_roce_dev_port(req
.device
, req
.port
)) {
1527 /* TODO find the net dev matching the request parameters
1528 * through the RoCE GID table */
1531 return ERR_CAST(*net_dev
);
1535 bind_list
= cma_ps_find(*net_dev
? dev_net(*net_dev
) : &init_net
,
1536 rdma_ps_from_service_id(req
.service_id
),
1537 cma_port_from_service_id(req
.service_id
));
1538 id_priv
= cma_find_listener(bind_list
, cm_id
, ib_event
, &req
, *net_dev
);
1539 if (IS_ERR(id_priv
) && *net_dev
) {
1547 static inline u8
cma_user_data_offset(struct rdma_id_private
*id_priv
)
1549 return cma_family(id_priv
) == AF_IB
? 0 : sizeof(struct cma_hdr
);
1552 static void cma_cancel_route(struct rdma_id_private
*id_priv
)
1554 if (rdma_cap_ib_sa(id_priv
->id
.device
, id_priv
->id
.port_num
)) {
1556 ib_sa_cancel_query(id_priv
->query_id
, id_priv
->query
);
1560 static void cma_cancel_listens(struct rdma_id_private
*id_priv
)
1562 struct rdma_id_private
*dev_id_priv
;
1565 * Remove from listen_any_list to prevent added devices from spawning
1566 * additional listen requests.
1569 list_del(&id_priv
->list
);
1571 while (!list_empty(&id_priv
->listen_list
)) {
1572 dev_id_priv
= list_entry(id_priv
->listen_list
.next
,
1573 struct rdma_id_private
, listen_list
);
1574 /* sync with device removal to avoid duplicate destruction */
1575 list_del_init(&dev_id_priv
->list
);
1576 list_del(&dev_id_priv
->listen_list
);
1577 mutex_unlock(&lock
);
1579 rdma_destroy_id(&dev_id_priv
->id
);
1582 mutex_unlock(&lock
);
1585 static void cma_cancel_operation(struct rdma_id_private
*id_priv
,
1586 enum rdma_cm_state state
)
1589 case RDMA_CM_ADDR_QUERY
:
1590 rdma_addr_cancel(&id_priv
->id
.route
.addr
.dev_addr
);
1592 case RDMA_CM_ROUTE_QUERY
:
1593 cma_cancel_route(id_priv
);
1595 case RDMA_CM_LISTEN
:
1596 if (cma_any_addr(cma_src_addr(id_priv
)) && !id_priv
->cma_dev
)
1597 cma_cancel_listens(id_priv
);
1604 static void cma_release_port(struct rdma_id_private
*id_priv
)
1606 struct rdma_bind_list
*bind_list
= id_priv
->bind_list
;
1607 struct net
*net
= id_priv
->id
.route
.addr
.dev_addr
.net
;
1613 hlist_del(&id_priv
->node
);
1614 if (hlist_empty(&bind_list
->owners
)) {
1615 cma_ps_remove(net
, bind_list
->ps
, bind_list
->port
);
1618 mutex_unlock(&lock
);
1621 static void cma_leave_mc_groups(struct rdma_id_private
*id_priv
)
1623 struct cma_multicast
*mc
;
1625 while (!list_empty(&id_priv
->mc_list
)) {
1626 mc
= container_of(id_priv
->mc_list
.next
,
1627 struct cma_multicast
, list
);
1628 list_del(&mc
->list
);
1629 if (rdma_cap_ib_mcast(id_priv
->cma_dev
->device
,
1630 id_priv
->id
.port_num
)) {
1631 ib_sa_free_multicast(mc
->multicast
.ib
);
1634 if (mc
->igmp_joined
) {
1635 struct rdma_dev_addr
*dev_addr
=
1636 &id_priv
->id
.route
.addr
.dev_addr
;
1637 struct net_device
*ndev
= NULL
;
1639 if (dev_addr
->bound_dev_if
)
1640 ndev
= dev_get_by_index(&init_net
,
1641 dev_addr
->bound_dev_if
);
1644 &mc
->multicast
.ib
->rec
.mgid
,
1649 kref_put(&mc
->mcref
, release_mc
);
1654 void rdma_destroy_id(struct rdma_cm_id
*id
)
1656 struct rdma_id_private
*id_priv
;
1657 enum rdma_cm_state state
;
1659 id_priv
= container_of(id
, struct rdma_id_private
, id
);
1660 state
= cma_exch(id_priv
, RDMA_CM_DESTROYING
);
1661 cma_cancel_operation(id_priv
, state
);
1664 * Wait for any active callback to finish. New callbacks will find
1665 * the id_priv state set to destroying and abort.
1667 mutex_lock(&id_priv
->handler_mutex
);
1668 mutex_unlock(&id_priv
->handler_mutex
);
1670 if (id_priv
->cma_dev
) {
1671 if (rdma_cap_ib_cm(id_priv
->id
.device
, 1)) {
1672 if (id_priv
->cm_id
.ib
)
1673 ib_destroy_cm_id(id_priv
->cm_id
.ib
);
1674 } else if (rdma_cap_iw_cm(id_priv
->id
.device
, 1)) {
1675 if (id_priv
->cm_id
.iw
)
1676 iw_destroy_cm_id(id_priv
->cm_id
.iw
);
1678 cma_leave_mc_groups(id_priv
);
1679 cma_release_dev(id_priv
);
1682 cma_release_port(id_priv
);
1683 cma_deref_id(id_priv
);
1684 wait_for_completion(&id_priv
->comp
);
1686 if (id_priv
->internal_id
)
1687 cma_deref_id(id_priv
->id
.context
);
1689 kfree(id_priv
->id
.route
.path_rec
);
1690 put_net(id_priv
->id
.route
.addr
.dev_addr
.net
);
1693 EXPORT_SYMBOL(rdma_destroy_id
);
1695 static int cma_rep_recv(struct rdma_id_private
*id_priv
)
1699 ret
= cma_modify_qp_rtr(id_priv
, NULL
);
1703 ret
= cma_modify_qp_rts(id_priv
, NULL
);
1707 ret
= ib_send_cm_rtu(id_priv
->cm_id
.ib
, NULL
, 0);
1713 pr_debug_ratelimited("RDMA CM: CONNECT_ERROR: failed to handle reply. status %d\n", ret
);
1714 cma_modify_qp_err(id_priv
);
1715 ib_send_cm_rej(id_priv
->cm_id
.ib
, IB_CM_REJ_CONSUMER_DEFINED
,
1720 static void cma_set_rep_event_data(struct rdma_cm_event
*event
,
1721 struct ib_cm_rep_event_param
*rep_data
,
1724 event
->param
.conn
.private_data
= private_data
;
1725 event
->param
.conn
.private_data_len
= IB_CM_REP_PRIVATE_DATA_SIZE
;
1726 event
->param
.conn
.responder_resources
= rep_data
->responder_resources
;
1727 event
->param
.conn
.initiator_depth
= rep_data
->initiator_depth
;
1728 event
->param
.conn
.flow_control
= rep_data
->flow_control
;
1729 event
->param
.conn
.rnr_retry_count
= rep_data
->rnr_retry_count
;
1730 event
->param
.conn
.srq
= rep_data
->srq
;
1731 event
->param
.conn
.qp_num
= rep_data
->remote_qpn
;
1734 static int cma_ib_handler(struct ib_cm_id
*cm_id
, struct ib_cm_event
*ib_event
)
1736 struct rdma_id_private
*id_priv
= cm_id
->context
;
1737 struct rdma_cm_event event
;
1740 mutex_lock(&id_priv
->handler_mutex
);
1741 if ((ib_event
->event
!= IB_CM_TIMEWAIT_EXIT
&&
1742 id_priv
->state
!= RDMA_CM_CONNECT
) ||
1743 (ib_event
->event
== IB_CM_TIMEWAIT_EXIT
&&
1744 id_priv
->state
!= RDMA_CM_DISCONNECT
))
1747 memset(&event
, 0, sizeof event
);
1748 switch (ib_event
->event
) {
1749 case IB_CM_REQ_ERROR
:
1750 case IB_CM_REP_ERROR
:
1751 event
.event
= RDMA_CM_EVENT_UNREACHABLE
;
1752 event
.status
= -ETIMEDOUT
;
1754 case IB_CM_REP_RECEIVED
:
1755 if (cma_comp(id_priv
, RDMA_CM_CONNECT
) &&
1756 (id_priv
->id
.qp_type
!= IB_QPT_UD
))
1757 ib_send_cm_mra(cm_id
, CMA_CM_MRA_SETTING
, NULL
, 0);
1758 if (id_priv
->id
.qp
) {
1759 event
.status
= cma_rep_recv(id_priv
);
1760 event
.event
= event
.status
? RDMA_CM_EVENT_CONNECT_ERROR
:
1761 RDMA_CM_EVENT_ESTABLISHED
;
1763 event
.event
= RDMA_CM_EVENT_CONNECT_RESPONSE
;
1765 cma_set_rep_event_data(&event
, &ib_event
->param
.rep_rcvd
,
1766 ib_event
->private_data
);
1768 case IB_CM_RTU_RECEIVED
:
1769 case IB_CM_USER_ESTABLISHED
:
1770 event
.event
= RDMA_CM_EVENT_ESTABLISHED
;
1772 case IB_CM_DREQ_ERROR
:
1773 event
.status
= -ETIMEDOUT
; /* fall through */
1774 case IB_CM_DREQ_RECEIVED
:
1775 case IB_CM_DREP_RECEIVED
:
1776 if (!cma_comp_exch(id_priv
, RDMA_CM_CONNECT
,
1777 RDMA_CM_DISCONNECT
))
1779 event
.event
= RDMA_CM_EVENT_DISCONNECTED
;
1781 case IB_CM_TIMEWAIT_EXIT
:
1782 event
.event
= RDMA_CM_EVENT_TIMEWAIT_EXIT
;
1784 case IB_CM_MRA_RECEIVED
:
1787 case IB_CM_REJ_RECEIVED
:
1788 pr_debug_ratelimited("RDMA CM: REJECTED: %s\n", rdma_reject_msg(&id_priv
->id
,
1789 ib_event
->param
.rej_rcvd
.reason
));
1790 cma_modify_qp_err(id_priv
);
1791 event
.status
= ib_event
->param
.rej_rcvd
.reason
;
1792 event
.event
= RDMA_CM_EVENT_REJECTED
;
1793 event
.param
.conn
.private_data
= ib_event
->private_data
;
1794 event
.param
.conn
.private_data_len
= IB_CM_REJ_PRIVATE_DATA_SIZE
;
1797 pr_err("RDMA CMA: unexpected IB CM event: %d\n",
1802 ret
= id_priv
->id
.event_handler(&id_priv
->id
, &event
);
1804 /* Destroy the CM ID by returning a non-zero value. */
1805 id_priv
->cm_id
.ib
= NULL
;
1806 cma_exch(id_priv
, RDMA_CM_DESTROYING
);
1807 mutex_unlock(&id_priv
->handler_mutex
);
1808 rdma_destroy_id(&id_priv
->id
);
1812 mutex_unlock(&id_priv
->handler_mutex
);
1816 static struct rdma_id_private
*cma_new_conn_id(struct rdma_cm_id
*listen_id
,
1817 struct ib_cm_event
*ib_event
,
1818 struct net_device
*net_dev
)
1820 struct rdma_id_private
*id_priv
;
1821 struct rdma_cm_id
*id
;
1822 struct rdma_route
*rt
;
1823 const sa_family_t ss_family
= listen_id
->route
.addr
.src_addr
.ss_family
;
1824 struct sa_path_rec
*path
= ib_event
->param
.req_rcvd
.primary_path
;
1825 const __be64 service_id
=
1826 ib_event
->param
.req_rcvd
.primary_path
->service_id
;
1829 id
= rdma_create_id(listen_id
->route
.addr
.dev_addr
.net
,
1830 listen_id
->event_handler
, listen_id
->context
,
1831 listen_id
->ps
, ib_event
->param
.req_rcvd
.qp_type
);
1835 id_priv
= container_of(id
, struct rdma_id_private
, id
);
1836 if (cma_save_net_info((struct sockaddr
*)&id
->route
.addr
.src_addr
,
1837 (struct sockaddr
*)&id
->route
.addr
.dst_addr
,
1838 listen_id
, ib_event
, ss_family
, service_id
))
1842 rt
->num_paths
= ib_event
->param
.req_rcvd
.alternate_path
? 2 : 1;
1843 rt
->path_rec
= kmalloc(sizeof *rt
->path_rec
* rt
->num_paths
,
1848 rt
->path_rec
[0] = *path
;
1849 if (rt
->num_paths
== 2)
1850 rt
->path_rec
[1] = *ib_event
->param
.req_rcvd
.alternate_path
;
1853 rdma_copy_addr(&rt
->addr
.dev_addr
, net_dev
, NULL
);
1855 if (!cma_protocol_roce(listen_id
) &&
1856 cma_any_addr(cma_src_addr(id_priv
))) {
1857 rt
->addr
.dev_addr
.dev_type
= ARPHRD_INFINIBAND
;
1858 rdma_addr_set_sgid(&rt
->addr
.dev_addr
, &rt
->path_rec
[0].sgid
);
1859 ib_addr_set_pkey(&rt
->addr
.dev_addr
, be16_to_cpu(rt
->path_rec
[0].pkey
));
1860 } else if (!cma_any_addr(cma_src_addr(id_priv
))) {
1861 ret
= cma_translate_addr(cma_src_addr(id_priv
), &rt
->addr
.dev_addr
);
1866 rdma_addr_set_dgid(&rt
->addr
.dev_addr
, &rt
->path_rec
[0].dgid
);
1868 id_priv
->state
= RDMA_CM_CONNECT
;
1872 rdma_destroy_id(id
);
1876 static struct rdma_id_private
*cma_new_udp_id(struct rdma_cm_id
*listen_id
,
1877 struct ib_cm_event
*ib_event
,
1878 struct net_device
*net_dev
)
1880 struct rdma_id_private
*id_priv
;
1881 struct rdma_cm_id
*id
;
1882 const sa_family_t ss_family
= listen_id
->route
.addr
.src_addr
.ss_family
;
1883 struct net
*net
= listen_id
->route
.addr
.dev_addr
.net
;
1886 id
= rdma_create_id(net
, listen_id
->event_handler
, listen_id
->context
,
1887 listen_id
->ps
, IB_QPT_UD
);
1891 id_priv
= container_of(id
, struct rdma_id_private
, id
);
1892 if (cma_save_net_info((struct sockaddr
*)&id
->route
.addr
.src_addr
,
1893 (struct sockaddr
*)&id
->route
.addr
.dst_addr
,
1894 listen_id
, ib_event
, ss_family
,
1895 ib_event
->param
.sidr_req_rcvd
.service_id
))
1899 rdma_copy_addr(&id
->route
.addr
.dev_addr
, net_dev
, NULL
);
1901 if (!cma_any_addr(cma_src_addr(id_priv
))) {
1902 ret
= cma_translate_addr(cma_src_addr(id_priv
),
1903 &id
->route
.addr
.dev_addr
);
1909 id_priv
->state
= RDMA_CM_CONNECT
;
1912 rdma_destroy_id(id
);
1916 static void cma_set_req_event_data(struct rdma_cm_event
*event
,
1917 struct ib_cm_req_event_param
*req_data
,
1918 void *private_data
, int offset
)
1920 event
->param
.conn
.private_data
= private_data
+ offset
;
1921 event
->param
.conn
.private_data_len
= IB_CM_REQ_PRIVATE_DATA_SIZE
- offset
;
1922 event
->param
.conn
.responder_resources
= req_data
->responder_resources
;
1923 event
->param
.conn
.initiator_depth
= req_data
->initiator_depth
;
1924 event
->param
.conn
.flow_control
= req_data
->flow_control
;
1925 event
->param
.conn
.retry_count
= req_data
->retry_count
;
1926 event
->param
.conn
.rnr_retry_count
= req_data
->rnr_retry_count
;
1927 event
->param
.conn
.srq
= req_data
->srq
;
1928 event
->param
.conn
.qp_num
= req_data
->remote_qpn
;
1931 static int cma_check_req_qp_type(struct rdma_cm_id
*id
, struct ib_cm_event
*ib_event
)
1933 return (((ib_event
->event
== IB_CM_REQ_RECEIVED
) &&
1934 (ib_event
->param
.req_rcvd
.qp_type
== id
->qp_type
)) ||
1935 ((ib_event
->event
== IB_CM_SIDR_REQ_RECEIVED
) &&
1936 (id
->qp_type
== IB_QPT_UD
)) ||
1940 static int cma_req_handler(struct ib_cm_id
*cm_id
, struct ib_cm_event
*ib_event
)
1942 struct rdma_id_private
*listen_id
, *conn_id
= NULL
;
1943 struct rdma_cm_event event
;
1944 struct net_device
*net_dev
;
1948 listen_id
= cma_id_from_event(cm_id
, ib_event
, &net_dev
);
1949 if (IS_ERR(listen_id
))
1950 return PTR_ERR(listen_id
);
1952 if (!cma_check_req_qp_type(&listen_id
->id
, ib_event
)) {
1957 mutex_lock(&listen_id
->handler_mutex
);
1958 if (listen_id
->state
!= RDMA_CM_LISTEN
) {
1959 ret
= -ECONNABORTED
;
1963 memset(&event
, 0, sizeof event
);
1964 offset
= cma_user_data_offset(listen_id
);
1965 event
.event
= RDMA_CM_EVENT_CONNECT_REQUEST
;
1966 if (ib_event
->event
== IB_CM_SIDR_REQ_RECEIVED
) {
1967 conn_id
= cma_new_udp_id(&listen_id
->id
, ib_event
, net_dev
);
1968 event
.param
.ud
.private_data
= ib_event
->private_data
+ offset
;
1969 event
.param
.ud
.private_data_len
=
1970 IB_CM_SIDR_REQ_PRIVATE_DATA_SIZE
- offset
;
1972 conn_id
= cma_new_conn_id(&listen_id
->id
, ib_event
, net_dev
);
1973 cma_set_req_event_data(&event
, &ib_event
->param
.req_rcvd
,
1974 ib_event
->private_data
, offset
);
1981 mutex_lock_nested(&conn_id
->handler_mutex
, SINGLE_DEPTH_NESTING
);
1982 ret
= cma_acquire_dev(conn_id
, listen_id
);
1986 conn_id
->cm_id
.ib
= cm_id
;
1987 cm_id
->context
= conn_id
;
1988 cm_id
->cm_handler
= cma_ib_handler
;
1991 * Protect against the user destroying conn_id from another thread
1992 * until we're done accessing it.
1994 atomic_inc(&conn_id
->refcount
);
1995 ret
= conn_id
->id
.event_handler(&conn_id
->id
, &event
);
1999 * Acquire mutex to prevent user executing rdma_destroy_id()
2000 * while we're accessing the cm_id.
2003 if (cma_comp(conn_id
, RDMA_CM_CONNECT
) &&
2004 (conn_id
->id
.qp_type
!= IB_QPT_UD
))
2005 ib_send_cm_mra(cm_id
, CMA_CM_MRA_SETTING
, NULL
, 0);
2006 mutex_unlock(&lock
);
2007 mutex_unlock(&conn_id
->handler_mutex
);
2008 mutex_unlock(&listen_id
->handler_mutex
);
2009 cma_deref_id(conn_id
);
2015 cma_deref_id(conn_id
);
2016 /* Destroy the CM ID by returning a non-zero value. */
2017 conn_id
->cm_id
.ib
= NULL
;
2019 cma_exch(conn_id
, RDMA_CM_DESTROYING
);
2020 mutex_unlock(&conn_id
->handler_mutex
);
2022 mutex_unlock(&listen_id
->handler_mutex
);
2024 rdma_destroy_id(&conn_id
->id
);
2033 __be64
rdma_get_service_id(struct rdma_cm_id
*id
, struct sockaddr
*addr
)
2035 if (addr
->sa_family
== AF_IB
)
2036 return ((struct sockaddr_ib
*) addr
)->sib_sid
;
2038 return cpu_to_be64(((u64
)id
->ps
<< 16) + be16_to_cpu(cma_port(addr
)));
2040 EXPORT_SYMBOL(rdma_get_service_id
);
2042 void rdma_read_gids(struct rdma_cm_id
*cm_id
, union ib_gid
*sgid
,
2045 struct rdma_addr
*addr
= &cm_id
->route
.addr
;
2047 if (!cm_id
->device
) {
2049 memset(sgid
, 0, sizeof(*sgid
));
2051 memset(dgid
, 0, sizeof(*dgid
));
2055 if (rdma_protocol_roce(cm_id
->device
, cm_id
->port_num
)) {
2057 rdma_ip2gid((struct sockaddr
*)&addr
->src_addr
, sgid
);
2059 rdma_ip2gid((struct sockaddr
*)&addr
->dst_addr
, dgid
);
2062 rdma_addr_get_sgid(&addr
->dev_addr
, sgid
);
2064 rdma_addr_get_dgid(&addr
->dev_addr
, dgid
);
2067 EXPORT_SYMBOL(rdma_read_gids
);
2069 static int cma_iw_handler(struct iw_cm_id
*iw_id
, struct iw_cm_event
*iw_event
)
2071 struct rdma_id_private
*id_priv
= iw_id
->context
;
2072 struct rdma_cm_event event
;
2074 struct sockaddr
*laddr
= (struct sockaddr
*)&iw_event
->local_addr
;
2075 struct sockaddr
*raddr
= (struct sockaddr
*)&iw_event
->remote_addr
;
2077 mutex_lock(&id_priv
->handler_mutex
);
2078 if (id_priv
->state
!= RDMA_CM_CONNECT
)
2081 memset(&event
, 0, sizeof event
);
2082 switch (iw_event
->event
) {
2083 case IW_CM_EVENT_CLOSE
:
2084 event
.event
= RDMA_CM_EVENT_DISCONNECTED
;
2086 case IW_CM_EVENT_CONNECT_REPLY
:
2087 memcpy(cma_src_addr(id_priv
), laddr
,
2088 rdma_addr_size(laddr
));
2089 memcpy(cma_dst_addr(id_priv
), raddr
,
2090 rdma_addr_size(raddr
));
2091 switch (iw_event
->status
) {
2093 event
.event
= RDMA_CM_EVENT_ESTABLISHED
;
2094 event
.param
.conn
.initiator_depth
= iw_event
->ird
;
2095 event
.param
.conn
.responder_resources
= iw_event
->ord
;
2099 event
.event
= RDMA_CM_EVENT_REJECTED
;
2102 event
.event
= RDMA_CM_EVENT_UNREACHABLE
;
2105 event
.event
= RDMA_CM_EVENT_CONNECT_ERROR
;
2109 case IW_CM_EVENT_ESTABLISHED
:
2110 event
.event
= RDMA_CM_EVENT_ESTABLISHED
;
2111 event
.param
.conn
.initiator_depth
= iw_event
->ird
;
2112 event
.param
.conn
.responder_resources
= iw_event
->ord
;
2118 event
.status
= iw_event
->status
;
2119 event
.param
.conn
.private_data
= iw_event
->private_data
;
2120 event
.param
.conn
.private_data_len
= iw_event
->private_data_len
;
2121 ret
= id_priv
->id
.event_handler(&id_priv
->id
, &event
);
2123 /* Destroy the CM ID by returning a non-zero value. */
2124 id_priv
->cm_id
.iw
= NULL
;
2125 cma_exch(id_priv
, RDMA_CM_DESTROYING
);
2126 mutex_unlock(&id_priv
->handler_mutex
);
2127 rdma_destroy_id(&id_priv
->id
);
2132 mutex_unlock(&id_priv
->handler_mutex
);
2136 static int iw_conn_req_handler(struct iw_cm_id
*cm_id
,
2137 struct iw_cm_event
*iw_event
)
2139 struct rdma_cm_id
*new_cm_id
;
2140 struct rdma_id_private
*listen_id
, *conn_id
;
2141 struct rdma_cm_event event
;
2142 int ret
= -ECONNABORTED
;
2143 struct sockaddr
*laddr
= (struct sockaddr
*)&iw_event
->local_addr
;
2144 struct sockaddr
*raddr
= (struct sockaddr
*)&iw_event
->remote_addr
;
2146 listen_id
= cm_id
->context
;
2148 mutex_lock(&listen_id
->handler_mutex
);
2149 if (listen_id
->state
!= RDMA_CM_LISTEN
)
2152 /* Create a new RDMA id for the new IW CM ID */
2153 new_cm_id
= rdma_create_id(listen_id
->id
.route
.addr
.dev_addr
.net
,
2154 listen_id
->id
.event_handler
,
2155 listen_id
->id
.context
,
2156 RDMA_PS_TCP
, IB_QPT_RC
);
2157 if (IS_ERR(new_cm_id
)) {
2161 conn_id
= container_of(new_cm_id
, struct rdma_id_private
, id
);
2162 mutex_lock_nested(&conn_id
->handler_mutex
, SINGLE_DEPTH_NESTING
);
2163 conn_id
->state
= RDMA_CM_CONNECT
;
2165 ret
= rdma_translate_ip(laddr
, &conn_id
->id
.route
.addr
.dev_addr
);
2167 mutex_unlock(&conn_id
->handler_mutex
);
2168 rdma_destroy_id(new_cm_id
);
2172 ret
= cma_acquire_dev(conn_id
, listen_id
);
2174 mutex_unlock(&conn_id
->handler_mutex
);
2175 rdma_destroy_id(new_cm_id
);
2179 conn_id
->cm_id
.iw
= cm_id
;
2180 cm_id
->context
= conn_id
;
2181 cm_id
->cm_handler
= cma_iw_handler
;
2183 memcpy(cma_src_addr(conn_id
), laddr
, rdma_addr_size(laddr
));
2184 memcpy(cma_dst_addr(conn_id
), raddr
, rdma_addr_size(raddr
));
2186 memset(&event
, 0, sizeof event
);
2187 event
.event
= RDMA_CM_EVENT_CONNECT_REQUEST
;
2188 event
.param
.conn
.private_data
= iw_event
->private_data
;
2189 event
.param
.conn
.private_data_len
= iw_event
->private_data_len
;
2190 event
.param
.conn
.initiator_depth
= iw_event
->ird
;
2191 event
.param
.conn
.responder_resources
= iw_event
->ord
;
2194 * Protect against the user destroying conn_id from another thread
2195 * until we're done accessing it.
2197 atomic_inc(&conn_id
->refcount
);
2198 ret
= conn_id
->id
.event_handler(&conn_id
->id
, &event
);
2200 /* User wants to destroy the CM ID */
2201 conn_id
->cm_id
.iw
= NULL
;
2202 cma_exch(conn_id
, RDMA_CM_DESTROYING
);
2203 mutex_unlock(&conn_id
->handler_mutex
);
2204 cma_deref_id(conn_id
);
2205 rdma_destroy_id(&conn_id
->id
);
2209 mutex_unlock(&conn_id
->handler_mutex
);
2210 cma_deref_id(conn_id
);
2213 mutex_unlock(&listen_id
->handler_mutex
);
2217 static int cma_ib_listen(struct rdma_id_private
*id_priv
)
2219 struct sockaddr
*addr
;
2220 struct ib_cm_id
*id
;
2223 addr
= cma_src_addr(id_priv
);
2224 svc_id
= rdma_get_service_id(&id_priv
->id
, addr
);
2225 id
= ib_cm_insert_listen(id_priv
->id
.device
, cma_req_handler
, svc_id
);
2228 id_priv
->cm_id
.ib
= id
;
2233 static int cma_iw_listen(struct rdma_id_private
*id_priv
, int backlog
)
2236 struct iw_cm_id
*id
;
2238 id
= iw_create_cm_id(id_priv
->id
.device
,
2239 iw_conn_req_handler
,
2244 id
->tos
= id_priv
->tos
;
2245 id_priv
->cm_id
.iw
= id
;
2247 memcpy(&id_priv
->cm_id
.iw
->local_addr
, cma_src_addr(id_priv
),
2248 rdma_addr_size(cma_src_addr(id_priv
)));
2250 ret
= iw_cm_listen(id_priv
->cm_id
.iw
, backlog
);
2253 iw_destroy_cm_id(id_priv
->cm_id
.iw
);
2254 id_priv
->cm_id
.iw
= NULL
;
2260 static int cma_listen_handler(struct rdma_cm_id
*id
,
2261 struct rdma_cm_event
*event
)
2263 struct rdma_id_private
*id_priv
= id
->context
;
2265 id
->context
= id_priv
->id
.context
;
2266 id
->event_handler
= id_priv
->id
.event_handler
;
2267 return id_priv
->id
.event_handler(id
, event
);
2270 static void cma_listen_on_dev(struct rdma_id_private
*id_priv
,
2271 struct cma_device
*cma_dev
)
2273 struct rdma_id_private
*dev_id_priv
;
2274 struct rdma_cm_id
*id
;
2275 struct net
*net
= id_priv
->id
.route
.addr
.dev_addr
.net
;
2278 if (cma_family(id_priv
) == AF_IB
&& !rdma_cap_ib_cm(cma_dev
->device
, 1))
2281 id
= rdma_create_id(net
, cma_listen_handler
, id_priv
, id_priv
->id
.ps
,
2282 id_priv
->id
.qp_type
);
2286 dev_id_priv
= container_of(id
, struct rdma_id_private
, id
);
2288 dev_id_priv
->state
= RDMA_CM_ADDR_BOUND
;
2289 memcpy(cma_src_addr(dev_id_priv
), cma_src_addr(id_priv
),
2290 rdma_addr_size(cma_src_addr(id_priv
)));
2292 _cma_attach_to_dev(dev_id_priv
, cma_dev
);
2293 list_add_tail(&dev_id_priv
->listen_list
, &id_priv
->listen_list
);
2294 atomic_inc(&id_priv
->refcount
);
2295 dev_id_priv
->internal_id
= 1;
2296 dev_id_priv
->afonly
= id_priv
->afonly
;
2298 ret
= rdma_listen(id
, id_priv
->backlog
);
2300 pr_warn("RDMA CMA: cma_listen_on_dev, error %d, listening on device %s\n",
2301 ret
, cma_dev
->device
->name
);
2304 static void cma_listen_on_all(struct rdma_id_private
*id_priv
)
2306 struct cma_device
*cma_dev
;
2309 list_add_tail(&id_priv
->list
, &listen_any_list
);
2310 list_for_each_entry(cma_dev
, &dev_list
, list
)
2311 cma_listen_on_dev(id_priv
, cma_dev
);
2312 mutex_unlock(&lock
);
2315 void rdma_set_service_type(struct rdma_cm_id
*id
, int tos
)
2317 struct rdma_id_private
*id_priv
;
2319 id_priv
= container_of(id
, struct rdma_id_private
, id
);
2320 id_priv
->tos
= (u8
) tos
;
2321 id_priv
->tos_set
= true;
2323 EXPORT_SYMBOL(rdma_set_service_type
);
2325 static void cma_query_handler(int status
, struct sa_path_rec
*path_rec
,
2328 struct cma_work
*work
= context
;
2329 struct rdma_route
*route
;
2331 route
= &work
->id
->id
.route
;
2334 route
->num_paths
= 1;
2335 *route
->path_rec
= *path_rec
;
2337 work
->old_state
= RDMA_CM_ROUTE_QUERY
;
2338 work
->new_state
= RDMA_CM_ADDR_RESOLVED
;
2339 work
->event
.event
= RDMA_CM_EVENT_ROUTE_ERROR
;
2340 work
->event
.status
= status
;
2341 pr_debug_ratelimited("RDMA CM: ROUTE_ERROR: failed to query path. status %d\n",
2345 queue_work(cma_wq
, &work
->work
);
2348 static int cma_query_ib_route(struct rdma_id_private
*id_priv
, int timeout_ms
,
2349 struct cma_work
*work
)
2351 struct rdma_dev_addr
*dev_addr
= &id_priv
->id
.route
.addr
.dev_addr
;
2352 struct sa_path_rec path_rec
;
2353 ib_sa_comp_mask comp_mask
;
2354 struct sockaddr_in6
*sin6
;
2355 struct sockaddr_ib
*sib
;
2357 memset(&path_rec
, 0, sizeof path_rec
);
2359 if (rdma_cap_opa_ah(id_priv
->id
.device
, id_priv
->id
.port_num
))
2360 path_rec
.rec_type
= SA_PATH_REC_TYPE_OPA
;
2362 path_rec
.rec_type
= SA_PATH_REC_TYPE_IB
;
2363 rdma_addr_get_sgid(dev_addr
, &path_rec
.sgid
);
2364 rdma_addr_get_dgid(dev_addr
, &path_rec
.dgid
);
2365 path_rec
.pkey
= cpu_to_be16(ib_addr_get_pkey(dev_addr
));
2366 path_rec
.numb_path
= 1;
2367 path_rec
.reversible
= 1;
2368 path_rec
.service_id
= rdma_get_service_id(&id_priv
->id
,
2369 cma_dst_addr(id_priv
));
2371 comp_mask
= IB_SA_PATH_REC_DGID
| IB_SA_PATH_REC_SGID
|
2372 IB_SA_PATH_REC_PKEY
| IB_SA_PATH_REC_NUMB_PATH
|
2373 IB_SA_PATH_REC_REVERSIBLE
| IB_SA_PATH_REC_SERVICE_ID
;
2375 switch (cma_family(id_priv
)) {
2377 path_rec
.qos_class
= cpu_to_be16((u16
) id_priv
->tos
);
2378 comp_mask
|= IB_SA_PATH_REC_QOS_CLASS
;
2381 sin6
= (struct sockaddr_in6
*) cma_src_addr(id_priv
);
2382 path_rec
.traffic_class
= (u8
) (be32_to_cpu(sin6
->sin6_flowinfo
) >> 20);
2383 comp_mask
|= IB_SA_PATH_REC_TRAFFIC_CLASS
;
2386 sib
= (struct sockaddr_ib
*) cma_src_addr(id_priv
);
2387 path_rec
.traffic_class
= (u8
) (be32_to_cpu(sib
->sib_flowinfo
) >> 20);
2388 comp_mask
|= IB_SA_PATH_REC_TRAFFIC_CLASS
;
2392 id_priv
->query_id
= ib_sa_path_rec_get(&sa_client
, id_priv
->id
.device
,
2393 id_priv
->id
.port_num
, &path_rec
,
2394 comp_mask
, timeout_ms
,
2395 GFP_KERNEL
, cma_query_handler
,
2396 work
, &id_priv
->query
);
2398 return (id_priv
->query_id
< 0) ? id_priv
->query_id
: 0;
2401 static void cma_work_handler(struct work_struct
*_work
)
2403 struct cma_work
*work
= container_of(_work
, struct cma_work
, work
);
2404 struct rdma_id_private
*id_priv
= work
->id
;
2407 mutex_lock(&id_priv
->handler_mutex
);
2408 if (!cma_comp_exch(id_priv
, work
->old_state
, work
->new_state
))
2411 if (id_priv
->id
.event_handler(&id_priv
->id
, &work
->event
)) {
2412 cma_exch(id_priv
, RDMA_CM_DESTROYING
);
2416 mutex_unlock(&id_priv
->handler_mutex
);
2417 cma_deref_id(id_priv
);
2419 rdma_destroy_id(&id_priv
->id
);
2423 static void cma_ndev_work_handler(struct work_struct
*_work
)
2425 struct cma_ndev_work
*work
= container_of(_work
, struct cma_ndev_work
, work
);
2426 struct rdma_id_private
*id_priv
= work
->id
;
2429 mutex_lock(&id_priv
->handler_mutex
);
2430 if (id_priv
->state
== RDMA_CM_DESTROYING
||
2431 id_priv
->state
== RDMA_CM_DEVICE_REMOVAL
)
2434 if (id_priv
->id
.event_handler(&id_priv
->id
, &work
->event
)) {
2435 cma_exch(id_priv
, RDMA_CM_DESTROYING
);
2440 mutex_unlock(&id_priv
->handler_mutex
);
2441 cma_deref_id(id_priv
);
2443 rdma_destroy_id(&id_priv
->id
);
2447 static void cma_init_resolve_route_work(struct cma_work
*work
,
2448 struct rdma_id_private
*id_priv
)
2451 INIT_WORK(&work
->work
, cma_work_handler
);
2452 work
->old_state
= RDMA_CM_ROUTE_QUERY
;
2453 work
->new_state
= RDMA_CM_ROUTE_RESOLVED
;
2454 work
->event
.event
= RDMA_CM_EVENT_ROUTE_RESOLVED
;
2457 static void cma_init_resolve_addr_work(struct cma_work
*work
,
2458 struct rdma_id_private
*id_priv
)
2461 INIT_WORK(&work
->work
, cma_work_handler
);
2462 work
->old_state
= RDMA_CM_ADDR_QUERY
;
2463 work
->new_state
= RDMA_CM_ADDR_RESOLVED
;
2464 work
->event
.event
= RDMA_CM_EVENT_ADDR_RESOLVED
;
2467 static int cma_resolve_ib_route(struct rdma_id_private
*id_priv
, int timeout_ms
)
2469 struct rdma_route
*route
= &id_priv
->id
.route
;
2470 struct cma_work
*work
;
2473 work
= kzalloc(sizeof *work
, GFP_KERNEL
);
2477 cma_init_resolve_route_work(work
, id_priv
);
2479 route
->path_rec
= kmalloc(sizeof *route
->path_rec
, GFP_KERNEL
);
2480 if (!route
->path_rec
) {
2485 ret
= cma_query_ib_route(id_priv
, timeout_ms
, work
);
2491 kfree(route
->path_rec
);
2492 route
->path_rec
= NULL
;
2498 static enum ib_gid_type
cma_route_gid_type(enum rdma_network_type network_type
,
2499 unsigned long supported_gids
,
2500 enum ib_gid_type default_gid
)
2502 if ((network_type
== RDMA_NETWORK_IPV4
||
2503 network_type
== RDMA_NETWORK_IPV6
) &&
2504 test_bit(IB_GID_TYPE_ROCE_UDP_ENCAP
, &supported_gids
))
2505 return IB_GID_TYPE_ROCE_UDP_ENCAP
;
2511 * cma_iboe_set_path_rec_l2_fields() is helper function which sets
2512 * path record type based on GID type.
2513 * It also sets up other L2 fields which includes destination mac address
2514 * netdev ifindex, of the path record.
2515 * It returns the netdev of the bound interface for this path record entry.
2517 static struct net_device
*
2518 cma_iboe_set_path_rec_l2_fields(struct rdma_id_private
*id_priv
)
2520 struct rdma_route
*route
= &id_priv
->id
.route
;
2521 enum ib_gid_type gid_type
= IB_GID_TYPE_ROCE
;
2522 struct rdma_addr
*addr
= &route
->addr
;
2523 unsigned long supported_gids
;
2524 struct net_device
*ndev
;
2526 if (!addr
->dev_addr
.bound_dev_if
)
2529 ndev
= dev_get_by_index(addr
->dev_addr
.net
,
2530 addr
->dev_addr
.bound_dev_if
);
2534 supported_gids
= roce_gid_type_mask_support(id_priv
->id
.device
,
2535 id_priv
->id
.port_num
);
2536 gid_type
= cma_route_gid_type(addr
->dev_addr
.network
,
2539 /* Use the hint from IP Stack to select GID Type */
2540 if (gid_type
< ib_network_to_gid_type(addr
->dev_addr
.network
))
2541 gid_type
= ib_network_to_gid_type(addr
->dev_addr
.network
);
2542 route
->path_rec
->rec_type
= sa_conv_gid_to_pathrec_type(gid_type
);
2544 sa_path_set_ndev(route
->path_rec
, addr
->dev_addr
.net
);
2545 sa_path_set_ifindex(route
->path_rec
, ndev
->ifindex
);
2546 sa_path_set_dmac(route
->path_rec
, addr
->dev_addr
.dst_dev_addr
);
2550 int rdma_set_ib_path(struct rdma_cm_id
*id
,
2551 struct sa_path_rec
*path_rec
)
2553 struct rdma_id_private
*id_priv
;
2554 struct net_device
*ndev
;
2557 id_priv
= container_of(id
, struct rdma_id_private
, id
);
2558 if (!cma_comp_exch(id_priv
, RDMA_CM_ADDR_RESOLVED
,
2559 RDMA_CM_ROUTE_RESOLVED
))
2562 id
->route
.path_rec
= kmemdup(path_rec
, sizeof(*path_rec
),
2564 if (!id
->route
.path_rec
) {
2569 if (rdma_protocol_roce(id
->device
, id
->port_num
)) {
2570 ndev
= cma_iboe_set_path_rec_l2_fields(id_priv
);
2578 id
->route
.num_paths
= 1;
2582 kfree(id
->route
.path_rec
);
2583 id
->route
.path_rec
= NULL
;
2585 cma_comp_exch(id_priv
, RDMA_CM_ROUTE_RESOLVED
, RDMA_CM_ADDR_RESOLVED
);
2588 EXPORT_SYMBOL(rdma_set_ib_path
);
2590 static int cma_resolve_iw_route(struct rdma_id_private
*id_priv
, int timeout_ms
)
2592 struct cma_work
*work
;
2594 work
= kzalloc(sizeof *work
, GFP_KERNEL
);
2598 cma_init_resolve_route_work(work
, id_priv
);
2599 queue_work(cma_wq
, &work
->work
);
2603 static int iboe_tos_to_sl(struct net_device
*ndev
, int tos
)
2606 struct net_device
*dev
;
2608 prio
= rt_tos2priority(tos
);
2609 dev
= is_vlan_dev(ndev
) ? vlan_dev_real_dev(ndev
) : ndev
;
2611 return netdev_get_prio_tc_map(dev
, prio
);
2613 #if IS_ENABLED(CONFIG_VLAN_8021Q)
2614 if (is_vlan_dev(ndev
))
2615 return (vlan_dev_get_egress_qos_mask(ndev
, prio
) &
2616 VLAN_PRIO_MASK
) >> VLAN_PRIO_SHIFT
;
2621 static int cma_resolve_iboe_route(struct rdma_id_private
*id_priv
)
2623 struct rdma_route
*route
= &id_priv
->id
.route
;
2624 struct rdma_addr
*addr
= &route
->addr
;
2625 struct cma_work
*work
;
2627 struct net_device
*ndev
;
2629 u8 default_roce_tos
= id_priv
->cma_dev
->default_roce_tos
[id_priv
->id
.port_num
-
2630 rdma_start_port(id_priv
->cma_dev
->device
)];
2631 u8 tos
= id_priv
->tos_set
? id_priv
->tos
: default_roce_tos
;
2634 work
= kzalloc(sizeof *work
, GFP_KERNEL
);
2638 route
->path_rec
= kzalloc(sizeof *route
->path_rec
, GFP_KERNEL
);
2639 if (!route
->path_rec
) {
2644 route
->num_paths
= 1;
2646 ndev
= cma_iboe_set_path_rec_l2_fields(id_priv
);
2652 rdma_ip2gid((struct sockaddr
*)&id_priv
->id
.route
.addr
.src_addr
,
2653 &route
->path_rec
->sgid
);
2654 rdma_ip2gid((struct sockaddr
*)&id_priv
->id
.route
.addr
.dst_addr
,
2655 &route
->path_rec
->dgid
);
2657 if (((struct sockaddr
*)&id_priv
->id
.route
.addr
.dst_addr
)->sa_family
!= AF_IB
)
2658 /* TODO: get the hoplimit from the inet/inet6 device */
2659 route
->path_rec
->hop_limit
= addr
->dev_addr
.hoplimit
;
2661 route
->path_rec
->hop_limit
= 1;
2662 route
->path_rec
->reversible
= 1;
2663 route
->path_rec
->pkey
= cpu_to_be16(0xffff);
2664 route
->path_rec
->mtu_selector
= IB_SA_EQ
;
2665 route
->path_rec
->sl
= iboe_tos_to_sl(ndev
, tos
);
2666 route
->path_rec
->traffic_class
= tos
;
2667 route
->path_rec
->mtu
= iboe_get_mtu(ndev
->mtu
);
2668 route
->path_rec
->rate_selector
= IB_SA_EQ
;
2669 route
->path_rec
->rate
= iboe_get_rate(ndev
);
2671 route
->path_rec
->packet_life_time_selector
= IB_SA_EQ
;
2672 route
->path_rec
->packet_life_time
= CMA_IBOE_PACKET_LIFETIME
;
2673 if (!route
->path_rec
->mtu
) {
2678 cma_init_resolve_route_work(work
, id_priv
);
2679 queue_work(cma_wq
, &work
->work
);
2684 kfree(route
->path_rec
);
2685 route
->path_rec
= NULL
;
2691 int rdma_resolve_route(struct rdma_cm_id
*id
, int timeout_ms
)
2693 struct rdma_id_private
*id_priv
;
2696 id_priv
= container_of(id
, struct rdma_id_private
, id
);
2697 if (!cma_comp_exch(id_priv
, RDMA_CM_ADDR_RESOLVED
, RDMA_CM_ROUTE_QUERY
))
2700 atomic_inc(&id_priv
->refcount
);
2701 if (rdma_cap_ib_sa(id
->device
, id
->port_num
))
2702 ret
= cma_resolve_ib_route(id_priv
, timeout_ms
);
2703 else if (rdma_protocol_roce(id
->device
, id
->port_num
))
2704 ret
= cma_resolve_iboe_route(id_priv
);
2705 else if (rdma_protocol_iwarp(id
->device
, id
->port_num
))
2706 ret
= cma_resolve_iw_route(id_priv
, timeout_ms
);
2715 cma_comp_exch(id_priv
, RDMA_CM_ROUTE_QUERY
, RDMA_CM_ADDR_RESOLVED
);
2716 cma_deref_id(id_priv
);
2719 EXPORT_SYMBOL(rdma_resolve_route
);
2721 static void cma_set_loopback(struct sockaddr
*addr
)
2723 switch (addr
->sa_family
) {
2725 ((struct sockaddr_in
*) addr
)->sin_addr
.s_addr
= htonl(INADDR_LOOPBACK
);
2728 ipv6_addr_set(&((struct sockaddr_in6
*) addr
)->sin6_addr
,
2732 ib_addr_set(&((struct sockaddr_ib
*) addr
)->sib_addr
,
2738 static int cma_bind_loopback(struct rdma_id_private
*id_priv
)
2740 struct cma_device
*cma_dev
, *cur_dev
;
2742 enum ib_port_state port_state
;
2749 list_for_each_entry(cur_dev
, &dev_list
, list
) {
2750 if (cma_family(id_priv
) == AF_IB
&&
2751 !rdma_cap_ib_cm(cur_dev
->device
, 1))
2757 for (p
= 1; p
<= cur_dev
->device
->phys_port_cnt
; ++p
) {
2758 if (!ib_get_cached_port_state(cur_dev
->device
, p
, &port_state
) &&
2759 port_state
== IB_PORT_ACTIVE
) {
2774 ret
= ib_get_cached_gid(cma_dev
->device
, p
, 0, &gid
, NULL
);
2778 ret
= ib_get_cached_pkey(cma_dev
->device
, p
, 0, &pkey
);
2782 id_priv
->id
.route
.addr
.dev_addr
.dev_type
=
2783 (rdma_protocol_ib(cma_dev
->device
, p
)) ?
2784 ARPHRD_INFINIBAND
: ARPHRD_ETHER
;
2786 rdma_addr_set_sgid(&id_priv
->id
.route
.addr
.dev_addr
, &gid
);
2787 ib_addr_set_pkey(&id_priv
->id
.route
.addr
.dev_addr
, pkey
);
2788 id_priv
->id
.port_num
= p
;
2789 cma_attach_to_dev(id_priv
, cma_dev
);
2790 cma_set_loopback(cma_src_addr(id_priv
));
2792 mutex_unlock(&lock
);
2796 static void addr_handler(int status
, struct sockaddr
*src_addr
,
2797 struct rdma_dev_addr
*dev_addr
, void *context
)
2799 struct rdma_id_private
*id_priv
= context
;
2800 struct rdma_cm_event event
;
2802 memset(&event
, 0, sizeof event
);
2803 mutex_lock(&id_priv
->handler_mutex
);
2804 if (!cma_comp_exch(id_priv
, RDMA_CM_ADDR_QUERY
,
2805 RDMA_CM_ADDR_RESOLVED
))
2808 memcpy(cma_src_addr(id_priv
), src_addr
, rdma_addr_size(src_addr
));
2809 if (!status
&& !id_priv
->cma_dev
) {
2810 status
= cma_acquire_dev(id_priv
, NULL
);
2812 pr_debug_ratelimited("RDMA CM: ADDR_ERROR: failed to acquire device. status %d\n",
2815 pr_debug_ratelimited("RDMA CM: ADDR_ERROR: failed to resolve IP. status %d\n", status
);
2819 if (!cma_comp_exch(id_priv
, RDMA_CM_ADDR_RESOLVED
,
2820 RDMA_CM_ADDR_BOUND
))
2822 event
.event
= RDMA_CM_EVENT_ADDR_ERROR
;
2823 event
.status
= status
;
2825 event
.event
= RDMA_CM_EVENT_ADDR_RESOLVED
;
2827 if (id_priv
->id
.event_handler(&id_priv
->id
, &event
)) {
2828 cma_exch(id_priv
, RDMA_CM_DESTROYING
);
2829 mutex_unlock(&id_priv
->handler_mutex
);
2830 cma_deref_id(id_priv
);
2831 rdma_destroy_id(&id_priv
->id
);
2835 mutex_unlock(&id_priv
->handler_mutex
);
2836 cma_deref_id(id_priv
);
2839 static int cma_resolve_loopback(struct rdma_id_private
*id_priv
)
2841 struct cma_work
*work
;
2845 work
= kzalloc(sizeof *work
, GFP_KERNEL
);
2849 if (!id_priv
->cma_dev
) {
2850 ret
= cma_bind_loopback(id_priv
);
2855 rdma_addr_get_sgid(&id_priv
->id
.route
.addr
.dev_addr
, &gid
);
2856 rdma_addr_set_dgid(&id_priv
->id
.route
.addr
.dev_addr
, &gid
);
2858 cma_init_resolve_addr_work(work
, id_priv
);
2859 queue_work(cma_wq
, &work
->work
);
2866 static int cma_resolve_ib_addr(struct rdma_id_private
*id_priv
)
2868 struct cma_work
*work
;
2871 work
= kzalloc(sizeof *work
, GFP_KERNEL
);
2875 if (!id_priv
->cma_dev
) {
2876 ret
= cma_resolve_ib_dev(id_priv
);
2881 rdma_addr_set_dgid(&id_priv
->id
.route
.addr
.dev_addr
, (union ib_gid
*)
2882 &(((struct sockaddr_ib
*) &id_priv
->id
.route
.addr
.dst_addr
)->sib_addr
));
2884 cma_init_resolve_addr_work(work
, id_priv
);
2885 queue_work(cma_wq
, &work
->work
);
2892 static int cma_bind_addr(struct rdma_cm_id
*id
, struct sockaddr
*src_addr
,
2893 struct sockaddr
*dst_addr
)
2895 if (!src_addr
|| !src_addr
->sa_family
) {
2896 src_addr
= (struct sockaddr
*) &id
->route
.addr
.src_addr
;
2897 src_addr
->sa_family
= dst_addr
->sa_family
;
2898 if (IS_ENABLED(CONFIG_IPV6
) &&
2899 dst_addr
->sa_family
== AF_INET6
) {
2900 struct sockaddr_in6
*src_addr6
= (struct sockaddr_in6
*) src_addr
;
2901 struct sockaddr_in6
*dst_addr6
= (struct sockaddr_in6
*) dst_addr
;
2902 src_addr6
->sin6_scope_id
= dst_addr6
->sin6_scope_id
;
2903 if (ipv6_addr_type(&dst_addr6
->sin6_addr
) & IPV6_ADDR_LINKLOCAL
)
2904 id
->route
.addr
.dev_addr
.bound_dev_if
= dst_addr6
->sin6_scope_id
;
2905 } else if (dst_addr
->sa_family
== AF_IB
) {
2906 ((struct sockaddr_ib
*) src_addr
)->sib_pkey
=
2907 ((struct sockaddr_ib
*) dst_addr
)->sib_pkey
;
2910 return rdma_bind_addr(id
, src_addr
);
2913 int rdma_resolve_addr(struct rdma_cm_id
*id
, struct sockaddr
*src_addr
,
2914 struct sockaddr
*dst_addr
, int timeout_ms
)
2916 struct rdma_id_private
*id_priv
;
2919 id_priv
= container_of(id
, struct rdma_id_private
, id
);
2920 memcpy(cma_dst_addr(id_priv
), dst_addr
, rdma_addr_size(dst_addr
));
2921 if (id_priv
->state
== RDMA_CM_IDLE
) {
2922 ret
= cma_bind_addr(id
, src_addr
, dst_addr
);
2924 memset(cma_dst_addr(id_priv
), 0, rdma_addr_size(dst_addr
));
2929 if (cma_family(id_priv
) != dst_addr
->sa_family
) {
2930 memset(cma_dst_addr(id_priv
), 0, rdma_addr_size(dst_addr
));
2934 if (!cma_comp_exch(id_priv
, RDMA_CM_ADDR_BOUND
, RDMA_CM_ADDR_QUERY
)) {
2935 memset(cma_dst_addr(id_priv
), 0, rdma_addr_size(dst_addr
));
2939 atomic_inc(&id_priv
->refcount
);
2940 if (cma_any_addr(dst_addr
)) {
2941 ret
= cma_resolve_loopback(id_priv
);
2943 if (dst_addr
->sa_family
== AF_IB
) {
2944 ret
= cma_resolve_ib_addr(id_priv
);
2946 ret
= rdma_resolve_ip(&addr_client
, cma_src_addr(id_priv
),
2947 dst_addr
, &id
->route
.addr
.dev_addr
,
2948 timeout_ms
, addr_handler
, id_priv
);
2956 cma_comp_exch(id_priv
, RDMA_CM_ADDR_QUERY
, RDMA_CM_ADDR_BOUND
);
2957 cma_deref_id(id_priv
);
2960 EXPORT_SYMBOL(rdma_resolve_addr
);
2962 int rdma_set_reuseaddr(struct rdma_cm_id
*id
, int reuse
)
2964 struct rdma_id_private
*id_priv
;
2965 unsigned long flags
;
2968 id_priv
= container_of(id
, struct rdma_id_private
, id
);
2969 spin_lock_irqsave(&id_priv
->lock
, flags
);
2970 if (reuse
|| id_priv
->state
== RDMA_CM_IDLE
) {
2971 id_priv
->reuseaddr
= reuse
;
2976 spin_unlock_irqrestore(&id_priv
->lock
, flags
);
2979 EXPORT_SYMBOL(rdma_set_reuseaddr
);
2981 int rdma_set_afonly(struct rdma_cm_id
*id
, int afonly
)
2983 struct rdma_id_private
*id_priv
;
2984 unsigned long flags
;
2987 id_priv
= container_of(id
, struct rdma_id_private
, id
);
2988 spin_lock_irqsave(&id_priv
->lock
, flags
);
2989 if (id_priv
->state
== RDMA_CM_IDLE
|| id_priv
->state
== RDMA_CM_ADDR_BOUND
) {
2990 id_priv
->options
|= (1 << CMA_OPTION_AFONLY
);
2991 id_priv
->afonly
= afonly
;
2996 spin_unlock_irqrestore(&id_priv
->lock
, flags
);
2999 EXPORT_SYMBOL(rdma_set_afonly
);
3001 static void cma_bind_port(struct rdma_bind_list
*bind_list
,
3002 struct rdma_id_private
*id_priv
)
3004 struct sockaddr
*addr
;
3005 struct sockaddr_ib
*sib
;
3009 addr
= cma_src_addr(id_priv
);
3010 port
= htons(bind_list
->port
);
3012 switch (addr
->sa_family
) {
3014 ((struct sockaddr_in
*) addr
)->sin_port
= port
;
3017 ((struct sockaddr_in6
*) addr
)->sin6_port
= port
;
3020 sib
= (struct sockaddr_ib
*) addr
;
3021 sid
= be64_to_cpu(sib
->sib_sid
);
3022 mask
= be64_to_cpu(sib
->sib_sid_mask
);
3023 sib
->sib_sid
= cpu_to_be64((sid
& mask
) | (u64
) ntohs(port
));
3024 sib
->sib_sid_mask
= cpu_to_be64(~0ULL);
3027 id_priv
->bind_list
= bind_list
;
3028 hlist_add_head(&id_priv
->node
, &bind_list
->owners
);
3031 static int cma_alloc_port(enum rdma_port_space ps
,
3032 struct rdma_id_private
*id_priv
, unsigned short snum
)
3034 struct rdma_bind_list
*bind_list
;
3037 bind_list
= kzalloc(sizeof *bind_list
, GFP_KERNEL
);
3041 ret
= cma_ps_alloc(id_priv
->id
.route
.addr
.dev_addr
.net
, ps
, bind_list
,
3047 bind_list
->port
= (unsigned short)ret
;
3048 cma_bind_port(bind_list
, id_priv
);
3052 return ret
== -ENOSPC
? -EADDRNOTAVAIL
: ret
;
3055 static int cma_port_is_unique(struct rdma_bind_list
*bind_list
,
3056 struct rdma_id_private
*id_priv
)
3058 struct rdma_id_private
*cur_id
;
3059 struct sockaddr
*daddr
= cma_dst_addr(id_priv
);
3060 struct sockaddr
*saddr
= cma_src_addr(id_priv
);
3061 __be16 dport
= cma_port(daddr
);
3063 hlist_for_each_entry(cur_id
, &bind_list
->owners
, node
) {
3064 struct sockaddr
*cur_daddr
= cma_dst_addr(cur_id
);
3065 struct sockaddr
*cur_saddr
= cma_src_addr(cur_id
);
3066 __be16 cur_dport
= cma_port(cur_daddr
);
3068 if (id_priv
== cur_id
)
3071 /* different dest port -> unique */
3072 if (!cma_any_port(cur_daddr
) &&
3073 (dport
!= cur_dport
))
3076 /* different src address -> unique */
3077 if (!cma_any_addr(saddr
) &&
3078 !cma_any_addr(cur_saddr
) &&
3079 cma_addr_cmp(saddr
, cur_saddr
))
3082 /* different dst address -> unique */
3083 if (!cma_any_addr(cur_daddr
) &&
3084 cma_addr_cmp(daddr
, cur_daddr
))
3087 return -EADDRNOTAVAIL
;
3092 static int cma_alloc_any_port(enum rdma_port_space ps
,
3093 struct rdma_id_private
*id_priv
)
3095 static unsigned int last_used_port
;
3096 int low
, high
, remaining
;
3098 struct net
*net
= id_priv
->id
.route
.addr
.dev_addr
.net
;
3100 inet_get_local_port_range(net
, &low
, &high
);
3101 remaining
= (high
- low
) + 1;
3102 rover
= prandom_u32() % remaining
+ low
;
3104 if (last_used_port
!= rover
) {
3105 struct rdma_bind_list
*bind_list
;
3108 bind_list
= cma_ps_find(net
, ps
, (unsigned short)rover
);
3111 ret
= cma_alloc_port(ps
, id_priv
, rover
);
3113 ret
= cma_port_is_unique(bind_list
, id_priv
);
3115 cma_bind_port(bind_list
, id_priv
);
3118 * Remember previously used port number in order to avoid
3119 * re-using same port immediately after it is closed.
3122 last_used_port
= rover
;
3123 if (ret
!= -EADDRNOTAVAIL
)
3128 if ((rover
< low
) || (rover
> high
))
3132 return -EADDRNOTAVAIL
;
3136 * Check that the requested port is available. This is called when trying to
3137 * bind to a specific port, or when trying to listen on a bound port. In
3138 * the latter case, the provided id_priv may already be on the bind_list, but
3139 * we still need to check that it's okay to start listening.
3141 static int cma_check_port(struct rdma_bind_list
*bind_list
,
3142 struct rdma_id_private
*id_priv
, uint8_t reuseaddr
)
3144 struct rdma_id_private
*cur_id
;
3145 struct sockaddr
*addr
, *cur_addr
;
3147 addr
= cma_src_addr(id_priv
);
3148 hlist_for_each_entry(cur_id
, &bind_list
->owners
, node
) {
3149 if (id_priv
== cur_id
)
3152 if ((cur_id
->state
!= RDMA_CM_LISTEN
) && reuseaddr
&&
3156 cur_addr
= cma_src_addr(cur_id
);
3157 if (id_priv
->afonly
&& cur_id
->afonly
&&
3158 (addr
->sa_family
!= cur_addr
->sa_family
))
3161 if (cma_any_addr(addr
) || cma_any_addr(cur_addr
))
3162 return -EADDRNOTAVAIL
;
3164 if (!cma_addr_cmp(addr
, cur_addr
))
3170 static int cma_use_port(enum rdma_port_space ps
,
3171 struct rdma_id_private
*id_priv
)
3173 struct rdma_bind_list
*bind_list
;
3174 unsigned short snum
;
3177 snum
= ntohs(cma_port(cma_src_addr(id_priv
)));
3178 if (snum
< PROT_SOCK
&& !capable(CAP_NET_BIND_SERVICE
))
3181 bind_list
= cma_ps_find(id_priv
->id
.route
.addr
.dev_addr
.net
, ps
, snum
);
3183 ret
= cma_alloc_port(ps
, id_priv
, snum
);
3185 ret
= cma_check_port(bind_list
, id_priv
, id_priv
->reuseaddr
);
3187 cma_bind_port(bind_list
, id_priv
);
3192 static int cma_bind_listen(struct rdma_id_private
*id_priv
)
3194 struct rdma_bind_list
*bind_list
= id_priv
->bind_list
;
3198 if (bind_list
->owners
.first
->next
)
3199 ret
= cma_check_port(bind_list
, id_priv
, 0);
3200 mutex_unlock(&lock
);
3204 static enum rdma_port_space
cma_select_inet_ps(
3205 struct rdma_id_private
*id_priv
)
3207 switch (id_priv
->id
.ps
) {
3212 return id_priv
->id
.ps
;
3219 static enum rdma_port_space
cma_select_ib_ps(struct rdma_id_private
*id_priv
)
3221 enum rdma_port_space ps
= 0;
3222 struct sockaddr_ib
*sib
;
3223 u64 sid_ps
, mask
, sid
;
3225 sib
= (struct sockaddr_ib
*) cma_src_addr(id_priv
);
3226 mask
= be64_to_cpu(sib
->sib_sid_mask
) & RDMA_IB_IP_PS_MASK
;
3227 sid
= be64_to_cpu(sib
->sib_sid
) & mask
;
3229 if ((id_priv
->id
.ps
== RDMA_PS_IB
) && (sid
== (RDMA_IB_IP_PS_IB
& mask
))) {
3230 sid_ps
= RDMA_IB_IP_PS_IB
;
3232 } else if (((id_priv
->id
.ps
== RDMA_PS_IB
) || (id_priv
->id
.ps
== RDMA_PS_TCP
)) &&
3233 (sid
== (RDMA_IB_IP_PS_TCP
& mask
))) {
3234 sid_ps
= RDMA_IB_IP_PS_TCP
;
3236 } else if (((id_priv
->id
.ps
== RDMA_PS_IB
) || (id_priv
->id
.ps
== RDMA_PS_UDP
)) &&
3237 (sid
== (RDMA_IB_IP_PS_UDP
& mask
))) {
3238 sid_ps
= RDMA_IB_IP_PS_UDP
;
3243 sib
->sib_sid
= cpu_to_be64(sid_ps
| ntohs(cma_port((struct sockaddr
*) sib
)));
3244 sib
->sib_sid_mask
= cpu_to_be64(RDMA_IB_IP_PS_MASK
|
3245 be64_to_cpu(sib
->sib_sid_mask
));
3250 static int cma_get_port(struct rdma_id_private
*id_priv
)
3252 enum rdma_port_space ps
;
3255 if (cma_family(id_priv
) != AF_IB
)
3256 ps
= cma_select_inet_ps(id_priv
);
3258 ps
= cma_select_ib_ps(id_priv
);
3260 return -EPROTONOSUPPORT
;
3263 if (cma_any_port(cma_src_addr(id_priv
)))
3264 ret
= cma_alloc_any_port(ps
, id_priv
);
3266 ret
= cma_use_port(ps
, id_priv
);
3267 mutex_unlock(&lock
);
3272 static int cma_check_linklocal(struct rdma_dev_addr
*dev_addr
,
3273 struct sockaddr
*addr
)
3275 #if IS_ENABLED(CONFIG_IPV6)
3276 struct sockaddr_in6
*sin6
;
3278 if (addr
->sa_family
!= AF_INET6
)
3281 sin6
= (struct sockaddr_in6
*) addr
;
3283 if (!(ipv6_addr_type(&sin6
->sin6_addr
) & IPV6_ADDR_LINKLOCAL
))
3286 if (!sin6
->sin6_scope_id
)
3289 dev_addr
->bound_dev_if
= sin6
->sin6_scope_id
;
3294 int rdma_listen(struct rdma_cm_id
*id
, int backlog
)
3296 struct rdma_id_private
*id_priv
;
3299 id_priv
= container_of(id
, struct rdma_id_private
, id
);
3300 if (id_priv
->state
== RDMA_CM_IDLE
) {
3301 id
->route
.addr
.src_addr
.ss_family
= AF_INET
;
3302 ret
= rdma_bind_addr(id
, cma_src_addr(id_priv
));
3307 if (!cma_comp_exch(id_priv
, RDMA_CM_ADDR_BOUND
, RDMA_CM_LISTEN
))
3310 if (id_priv
->reuseaddr
) {
3311 ret
= cma_bind_listen(id_priv
);
3316 id_priv
->backlog
= backlog
;
3318 if (rdma_cap_ib_cm(id
->device
, 1)) {
3319 ret
= cma_ib_listen(id_priv
);
3322 } else if (rdma_cap_iw_cm(id
->device
, 1)) {
3323 ret
= cma_iw_listen(id_priv
, backlog
);
3331 cma_listen_on_all(id_priv
);
3335 id_priv
->backlog
= 0;
3336 cma_comp_exch(id_priv
, RDMA_CM_LISTEN
, RDMA_CM_ADDR_BOUND
);
3339 EXPORT_SYMBOL(rdma_listen
);
3341 int rdma_bind_addr(struct rdma_cm_id
*id
, struct sockaddr
*addr
)
3343 struct rdma_id_private
*id_priv
;
3345 struct sockaddr
*daddr
;
3347 if (addr
->sa_family
!= AF_INET
&& addr
->sa_family
!= AF_INET6
&&
3348 addr
->sa_family
!= AF_IB
)
3349 return -EAFNOSUPPORT
;
3351 id_priv
= container_of(id
, struct rdma_id_private
, id
);
3352 if (!cma_comp_exch(id_priv
, RDMA_CM_IDLE
, RDMA_CM_ADDR_BOUND
))
3355 ret
= cma_check_linklocal(&id
->route
.addr
.dev_addr
, addr
);
3359 memcpy(cma_src_addr(id_priv
), addr
, rdma_addr_size(addr
));
3360 if (!cma_any_addr(addr
)) {
3361 ret
= cma_translate_addr(addr
, &id
->route
.addr
.dev_addr
);
3365 ret
= cma_acquire_dev(id_priv
, NULL
);
3370 if (!(id_priv
->options
& (1 << CMA_OPTION_AFONLY
))) {
3371 if (addr
->sa_family
== AF_INET
)
3372 id_priv
->afonly
= 1;
3373 #if IS_ENABLED(CONFIG_IPV6)
3374 else if (addr
->sa_family
== AF_INET6
) {
3375 struct net
*net
= id_priv
->id
.route
.addr
.dev_addr
.net
;
3377 id_priv
->afonly
= net
->ipv6
.sysctl
.bindv6only
;
3381 ret
= cma_get_port(id_priv
);
3385 daddr
= cma_dst_addr(id_priv
);
3386 daddr
->sa_family
= addr
->sa_family
;
3390 if (id_priv
->cma_dev
)
3391 cma_release_dev(id_priv
);
3393 cma_comp_exch(id_priv
, RDMA_CM_ADDR_BOUND
, RDMA_CM_IDLE
);
3396 EXPORT_SYMBOL(rdma_bind_addr
);
3398 static int cma_format_hdr(void *hdr
, struct rdma_id_private
*id_priv
)
3400 struct cma_hdr
*cma_hdr
;
3403 cma_hdr
->cma_version
= CMA_VERSION
;
3404 if (cma_family(id_priv
) == AF_INET
) {
3405 struct sockaddr_in
*src4
, *dst4
;
3407 src4
= (struct sockaddr_in
*) cma_src_addr(id_priv
);
3408 dst4
= (struct sockaddr_in
*) cma_dst_addr(id_priv
);
3410 cma_set_ip_ver(cma_hdr
, 4);
3411 cma_hdr
->src_addr
.ip4
.addr
= src4
->sin_addr
.s_addr
;
3412 cma_hdr
->dst_addr
.ip4
.addr
= dst4
->sin_addr
.s_addr
;
3413 cma_hdr
->port
= src4
->sin_port
;
3414 } else if (cma_family(id_priv
) == AF_INET6
) {
3415 struct sockaddr_in6
*src6
, *dst6
;
3417 src6
= (struct sockaddr_in6
*) cma_src_addr(id_priv
);
3418 dst6
= (struct sockaddr_in6
*) cma_dst_addr(id_priv
);
3420 cma_set_ip_ver(cma_hdr
, 6);
3421 cma_hdr
->src_addr
.ip6
= src6
->sin6_addr
;
3422 cma_hdr
->dst_addr
.ip6
= dst6
->sin6_addr
;
3423 cma_hdr
->port
= src6
->sin6_port
;
3428 static int cma_sidr_rep_handler(struct ib_cm_id
*cm_id
,
3429 struct ib_cm_event
*ib_event
)
3431 struct rdma_id_private
*id_priv
= cm_id
->context
;
3432 struct rdma_cm_event event
;
3433 struct ib_cm_sidr_rep_event_param
*rep
= &ib_event
->param
.sidr_rep_rcvd
;
3436 mutex_lock(&id_priv
->handler_mutex
);
3437 if (id_priv
->state
!= RDMA_CM_CONNECT
)
3440 memset(&event
, 0, sizeof event
);
3441 switch (ib_event
->event
) {
3442 case IB_CM_SIDR_REQ_ERROR
:
3443 event
.event
= RDMA_CM_EVENT_UNREACHABLE
;
3444 event
.status
= -ETIMEDOUT
;
3446 case IB_CM_SIDR_REP_RECEIVED
:
3447 event
.param
.ud
.private_data
= ib_event
->private_data
;
3448 event
.param
.ud
.private_data_len
= IB_CM_SIDR_REP_PRIVATE_DATA_SIZE
;
3449 if (rep
->status
!= IB_SIDR_SUCCESS
) {
3450 event
.event
= RDMA_CM_EVENT_UNREACHABLE
;
3451 event
.status
= ib_event
->param
.sidr_rep_rcvd
.status
;
3452 pr_debug_ratelimited("RDMA CM: UNREACHABLE: bad SIDR reply. status %d\n",
3456 ret
= cma_set_qkey(id_priv
, rep
->qkey
);
3458 pr_debug_ratelimited("RDMA CM: ADDR_ERROR: failed to set qkey. status %d\n", ret
);
3459 event
.event
= RDMA_CM_EVENT_ADDR_ERROR
;
3463 ib_init_ah_attr_from_path(id_priv
->id
.device
,
3464 id_priv
->id
.port_num
,
3465 id_priv
->id
.route
.path_rec
,
3466 &event
.param
.ud
.ah_attr
);
3467 event
.param
.ud
.qp_num
= rep
->qpn
;
3468 event
.param
.ud
.qkey
= rep
->qkey
;
3469 event
.event
= RDMA_CM_EVENT_ESTABLISHED
;
3473 pr_err("RDMA CMA: unexpected IB CM event: %d\n",
3478 ret
= id_priv
->id
.event_handler(&id_priv
->id
, &event
);
3480 /* Destroy the CM ID by returning a non-zero value. */
3481 id_priv
->cm_id
.ib
= NULL
;
3482 cma_exch(id_priv
, RDMA_CM_DESTROYING
);
3483 mutex_unlock(&id_priv
->handler_mutex
);
3484 rdma_destroy_id(&id_priv
->id
);
3488 mutex_unlock(&id_priv
->handler_mutex
);
3492 static int cma_resolve_ib_udp(struct rdma_id_private
*id_priv
,
3493 struct rdma_conn_param
*conn_param
)
3495 struct ib_cm_sidr_req_param req
;
3496 struct ib_cm_id
*id
;
3501 memset(&req
, 0, sizeof req
);
3502 offset
= cma_user_data_offset(id_priv
);
3503 req
.private_data_len
= offset
+ conn_param
->private_data_len
;
3504 if (req
.private_data_len
< conn_param
->private_data_len
)
3507 if (req
.private_data_len
) {
3508 private_data
= kzalloc(req
.private_data_len
, GFP_ATOMIC
);
3512 private_data
= NULL
;
3515 if (conn_param
->private_data
&& conn_param
->private_data_len
)
3516 memcpy(private_data
+ offset
, conn_param
->private_data
,
3517 conn_param
->private_data_len
);
3520 ret
= cma_format_hdr(private_data
, id_priv
);
3523 req
.private_data
= private_data
;
3526 id
= ib_create_cm_id(id_priv
->id
.device
, cma_sidr_rep_handler
,
3532 id_priv
->cm_id
.ib
= id
;
3534 req
.path
= id_priv
->id
.route
.path_rec
;
3535 req
.service_id
= rdma_get_service_id(&id_priv
->id
, cma_dst_addr(id_priv
));
3536 req
.timeout_ms
= 1 << (CMA_CM_RESPONSE_TIMEOUT
- 8);
3537 req
.max_cm_retries
= CMA_MAX_CM_RETRIES
;
3539 ret
= ib_send_cm_sidr_req(id_priv
->cm_id
.ib
, &req
);
3541 ib_destroy_cm_id(id_priv
->cm_id
.ib
);
3542 id_priv
->cm_id
.ib
= NULL
;
3545 kfree(private_data
);
3549 static int cma_connect_ib(struct rdma_id_private
*id_priv
,
3550 struct rdma_conn_param
*conn_param
)
3552 struct ib_cm_req_param req
;
3553 struct rdma_route
*route
;
3555 struct ib_cm_id
*id
;
3559 memset(&req
, 0, sizeof req
);
3560 offset
= cma_user_data_offset(id_priv
);
3561 req
.private_data_len
= offset
+ conn_param
->private_data_len
;
3562 if (req
.private_data_len
< conn_param
->private_data_len
)
3565 if (req
.private_data_len
) {
3566 private_data
= kzalloc(req
.private_data_len
, GFP_ATOMIC
);
3570 private_data
= NULL
;
3573 if (conn_param
->private_data
&& conn_param
->private_data_len
)
3574 memcpy(private_data
+ offset
, conn_param
->private_data
,
3575 conn_param
->private_data_len
);
3577 id
= ib_create_cm_id(id_priv
->id
.device
, cma_ib_handler
, id_priv
);
3582 id_priv
->cm_id
.ib
= id
;
3584 route
= &id_priv
->id
.route
;
3586 ret
= cma_format_hdr(private_data
, id_priv
);
3589 req
.private_data
= private_data
;
3592 req
.primary_path
= &route
->path_rec
[0];
3593 if (route
->num_paths
== 2)
3594 req
.alternate_path
= &route
->path_rec
[1];
3596 req
.service_id
= rdma_get_service_id(&id_priv
->id
, cma_dst_addr(id_priv
));
3597 req
.qp_num
= id_priv
->qp_num
;
3598 req
.qp_type
= id_priv
->id
.qp_type
;
3599 req
.starting_psn
= id_priv
->seq_num
;
3600 req
.responder_resources
= conn_param
->responder_resources
;
3601 req
.initiator_depth
= conn_param
->initiator_depth
;
3602 req
.flow_control
= conn_param
->flow_control
;
3603 req
.retry_count
= min_t(u8
, 7, conn_param
->retry_count
);
3604 req
.rnr_retry_count
= min_t(u8
, 7, conn_param
->rnr_retry_count
);
3605 req
.remote_cm_response_timeout
= CMA_CM_RESPONSE_TIMEOUT
;
3606 req
.local_cm_response_timeout
= CMA_CM_RESPONSE_TIMEOUT
;
3607 req
.max_cm_retries
= CMA_MAX_CM_RETRIES
;
3608 req
.srq
= id_priv
->srq
? 1 : 0;
3610 ret
= ib_send_cm_req(id_priv
->cm_id
.ib
, &req
);
3612 if (ret
&& !IS_ERR(id
)) {
3613 ib_destroy_cm_id(id
);
3614 id_priv
->cm_id
.ib
= NULL
;
3617 kfree(private_data
);
3621 static int cma_connect_iw(struct rdma_id_private
*id_priv
,
3622 struct rdma_conn_param
*conn_param
)
3624 struct iw_cm_id
*cm_id
;
3626 struct iw_cm_conn_param iw_param
;
3628 cm_id
= iw_create_cm_id(id_priv
->id
.device
, cma_iw_handler
, id_priv
);
3630 return PTR_ERR(cm_id
);
3632 cm_id
->tos
= id_priv
->tos
;
3633 id_priv
->cm_id
.iw
= cm_id
;
3635 memcpy(&cm_id
->local_addr
, cma_src_addr(id_priv
),
3636 rdma_addr_size(cma_src_addr(id_priv
)));
3637 memcpy(&cm_id
->remote_addr
, cma_dst_addr(id_priv
),
3638 rdma_addr_size(cma_dst_addr(id_priv
)));
3640 ret
= cma_modify_qp_rtr(id_priv
, conn_param
);
3645 iw_param
.ord
= conn_param
->initiator_depth
;
3646 iw_param
.ird
= conn_param
->responder_resources
;
3647 iw_param
.private_data
= conn_param
->private_data
;
3648 iw_param
.private_data_len
= conn_param
->private_data_len
;
3649 iw_param
.qpn
= id_priv
->id
.qp
? id_priv
->qp_num
: conn_param
->qp_num
;
3651 memset(&iw_param
, 0, sizeof iw_param
);
3652 iw_param
.qpn
= id_priv
->qp_num
;
3654 ret
= iw_cm_connect(cm_id
, &iw_param
);
3657 iw_destroy_cm_id(cm_id
);
3658 id_priv
->cm_id
.iw
= NULL
;
3663 int rdma_connect(struct rdma_cm_id
*id
, struct rdma_conn_param
*conn_param
)
3665 struct rdma_id_private
*id_priv
;
3668 id_priv
= container_of(id
, struct rdma_id_private
, id
);
3669 if (!cma_comp_exch(id_priv
, RDMA_CM_ROUTE_RESOLVED
, RDMA_CM_CONNECT
))
3673 id_priv
->qp_num
= conn_param
->qp_num
;
3674 id_priv
->srq
= conn_param
->srq
;
3677 if (rdma_cap_ib_cm(id
->device
, id
->port_num
)) {
3678 if (id
->qp_type
== IB_QPT_UD
)
3679 ret
= cma_resolve_ib_udp(id_priv
, conn_param
);
3681 ret
= cma_connect_ib(id_priv
, conn_param
);
3682 } else if (rdma_cap_iw_cm(id
->device
, id
->port_num
))
3683 ret
= cma_connect_iw(id_priv
, conn_param
);
3691 cma_comp_exch(id_priv
, RDMA_CM_CONNECT
, RDMA_CM_ROUTE_RESOLVED
);
3694 EXPORT_SYMBOL(rdma_connect
);
3696 static int cma_accept_ib(struct rdma_id_private
*id_priv
,
3697 struct rdma_conn_param
*conn_param
)
3699 struct ib_cm_rep_param rep
;
3702 ret
= cma_modify_qp_rtr(id_priv
, conn_param
);
3706 ret
= cma_modify_qp_rts(id_priv
, conn_param
);
3710 memset(&rep
, 0, sizeof rep
);
3711 rep
.qp_num
= id_priv
->qp_num
;
3712 rep
.starting_psn
= id_priv
->seq_num
;
3713 rep
.private_data
= conn_param
->private_data
;
3714 rep
.private_data_len
= conn_param
->private_data_len
;
3715 rep
.responder_resources
= conn_param
->responder_resources
;
3716 rep
.initiator_depth
= conn_param
->initiator_depth
;
3717 rep
.failover_accepted
= 0;
3718 rep
.flow_control
= conn_param
->flow_control
;
3719 rep
.rnr_retry_count
= min_t(u8
, 7, conn_param
->rnr_retry_count
);
3720 rep
.srq
= id_priv
->srq
? 1 : 0;
3722 ret
= ib_send_cm_rep(id_priv
->cm_id
.ib
, &rep
);
3727 static int cma_accept_iw(struct rdma_id_private
*id_priv
,
3728 struct rdma_conn_param
*conn_param
)
3730 struct iw_cm_conn_param iw_param
;
3736 ret
= cma_modify_qp_rtr(id_priv
, conn_param
);
3740 iw_param
.ord
= conn_param
->initiator_depth
;
3741 iw_param
.ird
= conn_param
->responder_resources
;
3742 iw_param
.private_data
= conn_param
->private_data
;
3743 iw_param
.private_data_len
= conn_param
->private_data_len
;
3744 if (id_priv
->id
.qp
) {
3745 iw_param
.qpn
= id_priv
->qp_num
;
3747 iw_param
.qpn
= conn_param
->qp_num
;
3749 return iw_cm_accept(id_priv
->cm_id
.iw
, &iw_param
);
3752 static int cma_send_sidr_rep(struct rdma_id_private
*id_priv
,
3753 enum ib_cm_sidr_status status
, u32 qkey
,
3754 const void *private_data
, int private_data_len
)
3756 struct ib_cm_sidr_rep_param rep
;
3759 memset(&rep
, 0, sizeof rep
);
3760 rep
.status
= status
;
3761 if (status
== IB_SIDR_SUCCESS
) {
3762 ret
= cma_set_qkey(id_priv
, qkey
);
3765 rep
.qp_num
= id_priv
->qp_num
;
3766 rep
.qkey
= id_priv
->qkey
;
3768 rep
.private_data
= private_data
;
3769 rep
.private_data_len
= private_data_len
;
3771 return ib_send_cm_sidr_rep(id_priv
->cm_id
.ib
, &rep
);
3774 int rdma_accept(struct rdma_cm_id
*id
, struct rdma_conn_param
*conn_param
)
3776 struct rdma_id_private
*id_priv
;
3779 id_priv
= container_of(id
, struct rdma_id_private
, id
);
3781 id_priv
->owner
= task_pid_nr(current
);
3783 if (!cma_comp(id_priv
, RDMA_CM_CONNECT
))
3786 if (!id
->qp
&& conn_param
) {
3787 id_priv
->qp_num
= conn_param
->qp_num
;
3788 id_priv
->srq
= conn_param
->srq
;
3791 if (rdma_cap_ib_cm(id
->device
, id
->port_num
)) {
3792 if (id
->qp_type
== IB_QPT_UD
) {
3794 ret
= cma_send_sidr_rep(id_priv
, IB_SIDR_SUCCESS
,
3796 conn_param
->private_data
,
3797 conn_param
->private_data_len
);
3799 ret
= cma_send_sidr_rep(id_priv
, IB_SIDR_SUCCESS
,
3803 ret
= cma_accept_ib(id_priv
, conn_param
);
3805 ret
= cma_rep_recv(id_priv
);
3807 } else if (rdma_cap_iw_cm(id
->device
, id
->port_num
))
3808 ret
= cma_accept_iw(id_priv
, conn_param
);
3817 cma_modify_qp_err(id_priv
);
3818 rdma_reject(id
, NULL
, 0);
3821 EXPORT_SYMBOL(rdma_accept
);
3823 int rdma_notify(struct rdma_cm_id
*id
, enum ib_event_type event
)
3825 struct rdma_id_private
*id_priv
;
3828 id_priv
= container_of(id
, struct rdma_id_private
, id
);
3829 if (!id_priv
->cm_id
.ib
)
3832 switch (id
->device
->node_type
) {
3833 case RDMA_NODE_IB_CA
:
3834 ret
= ib_cm_notify(id_priv
->cm_id
.ib
, event
);
3842 EXPORT_SYMBOL(rdma_notify
);
3844 int rdma_reject(struct rdma_cm_id
*id
, const void *private_data
,
3845 u8 private_data_len
)
3847 struct rdma_id_private
*id_priv
;
3850 id_priv
= container_of(id
, struct rdma_id_private
, id
);
3851 if (!id_priv
->cm_id
.ib
)
3854 if (rdma_cap_ib_cm(id
->device
, id
->port_num
)) {
3855 if (id
->qp_type
== IB_QPT_UD
)
3856 ret
= cma_send_sidr_rep(id_priv
, IB_SIDR_REJECT
, 0,
3857 private_data
, private_data_len
);
3859 ret
= ib_send_cm_rej(id_priv
->cm_id
.ib
,
3860 IB_CM_REJ_CONSUMER_DEFINED
, NULL
,
3861 0, private_data
, private_data_len
);
3862 } else if (rdma_cap_iw_cm(id
->device
, id
->port_num
)) {
3863 ret
= iw_cm_reject(id_priv
->cm_id
.iw
,
3864 private_data
, private_data_len
);
3870 EXPORT_SYMBOL(rdma_reject
);
3872 int rdma_disconnect(struct rdma_cm_id
*id
)
3874 struct rdma_id_private
*id_priv
;
3877 id_priv
= container_of(id
, struct rdma_id_private
, id
);
3878 if (!id_priv
->cm_id
.ib
)
3881 if (rdma_cap_ib_cm(id
->device
, id
->port_num
)) {
3882 ret
= cma_modify_qp_err(id_priv
);
3885 /* Initiate or respond to a disconnect. */
3886 if (ib_send_cm_dreq(id_priv
->cm_id
.ib
, NULL
, 0))
3887 ib_send_cm_drep(id_priv
->cm_id
.ib
, NULL
, 0);
3888 } else if (rdma_cap_iw_cm(id
->device
, id
->port_num
)) {
3889 ret
= iw_cm_disconnect(id_priv
->cm_id
.iw
, 0);
3896 EXPORT_SYMBOL(rdma_disconnect
);
3898 static int cma_ib_mc_handler(int status
, struct ib_sa_multicast
*multicast
)
3900 struct rdma_id_private
*id_priv
;
3901 struct cma_multicast
*mc
= multicast
->context
;
3902 struct rdma_cm_event event
;
3905 id_priv
= mc
->id_priv
;
3906 mutex_lock(&id_priv
->handler_mutex
);
3907 if (id_priv
->state
!= RDMA_CM_ADDR_BOUND
&&
3908 id_priv
->state
!= RDMA_CM_ADDR_RESOLVED
)
3912 status
= cma_set_qkey(id_priv
, be32_to_cpu(multicast
->rec
.qkey
));
3914 pr_debug_ratelimited("RDMA CM: MULTICAST_ERROR: failed to join multicast. status %d\n",
3916 mutex_lock(&id_priv
->qp_mutex
);
3917 if (!status
&& id_priv
->id
.qp
) {
3918 status
= ib_attach_mcast(id_priv
->id
.qp
, &multicast
->rec
.mgid
,
3919 be16_to_cpu(multicast
->rec
.mlid
));
3921 pr_debug_ratelimited("RDMA CM: MULTICAST_ERROR: failed to attach QP. status %d\n",
3924 mutex_unlock(&id_priv
->qp_mutex
);
3926 memset(&event
, 0, sizeof event
);
3927 event
.status
= status
;
3928 event
.param
.ud
.private_data
= mc
->context
;
3930 struct rdma_dev_addr
*dev_addr
=
3931 &id_priv
->id
.route
.addr
.dev_addr
;
3932 struct net_device
*ndev
=
3933 dev_get_by_index(dev_addr
->net
, dev_addr
->bound_dev_if
);
3934 enum ib_gid_type gid_type
=
3935 id_priv
->cma_dev
->default_gid_type
[id_priv
->id
.port_num
-
3936 rdma_start_port(id_priv
->cma_dev
->device
)];
3938 event
.event
= RDMA_CM_EVENT_MULTICAST_JOIN
;
3939 ib_init_ah_from_mcmember(id_priv
->id
.device
,
3940 id_priv
->id
.port_num
, &multicast
->rec
,
3942 &event
.param
.ud
.ah_attr
);
3943 event
.param
.ud
.qp_num
= 0xFFFFFF;
3944 event
.param
.ud
.qkey
= be32_to_cpu(multicast
->rec
.qkey
);
3948 event
.event
= RDMA_CM_EVENT_MULTICAST_ERROR
;
3950 ret
= id_priv
->id
.event_handler(&id_priv
->id
, &event
);
3952 cma_exch(id_priv
, RDMA_CM_DESTROYING
);
3953 mutex_unlock(&id_priv
->handler_mutex
);
3954 rdma_destroy_id(&id_priv
->id
);
3959 mutex_unlock(&id_priv
->handler_mutex
);
3963 static void cma_set_mgid(struct rdma_id_private
*id_priv
,
3964 struct sockaddr
*addr
, union ib_gid
*mgid
)
3966 unsigned char mc_map
[MAX_ADDR_LEN
];
3967 struct rdma_dev_addr
*dev_addr
= &id_priv
->id
.route
.addr
.dev_addr
;
3968 struct sockaddr_in
*sin
= (struct sockaddr_in
*) addr
;
3969 struct sockaddr_in6
*sin6
= (struct sockaddr_in6
*) addr
;
3971 if (cma_any_addr(addr
)) {
3972 memset(mgid
, 0, sizeof *mgid
);
3973 } else if ((addr
->sa_family
== AF_INET6
) &&
3974 ((be32_to_cpu(sin6
->sin6_addr
.s6_addr32
[0]) & 0xFFF0FFFF) ==
3976 /* IPv6 address is an SA assigned MGID. */
3977 memcpy(mgid
, &sin6
->sin6_addr
, sizeof *mgid
);
3978 } else if (addr
->sa_family
== AF_IB
) {
3979 memcpy(mgid
, &((struct sockaddr_ib
*) addr
)->sib_addr
, sizeof *mgid
);
3980 } else if ((addr
->sa_family
== AF_INET6
)) {
3981 ipv6_ib_mc_map(&sin6
->sin6_addr
, dev_addr
->broadcast
, mc_map
);
3982 if (id_priv
->id
.ps
== RDMA_PS_UDP
)
3983 mc_map
[7] = 0x01; /* Use RDMA CM signature */
3984 *mgid
= *(union ib_gid
*) (mc_map
+ 4);
3986 ip_ib_mc_map(sin
->sin_addr
.s_addr
, dev_addr
->broadcast
, mc_map
);
3987 if (id_priv
->id
.ps
== RDMA_PS_UDP
)
3988 mc_map
[7] = 0x01; /* Use RDMA CM signature */
3989 *mgid
= *(union ib_gid
*) (mc_map
+ 4);
3993 static int cma_join_ib_multicast(struct rdma_id_private
*id_priv
,
3994 struct cma_multicast
*mc
)
3996 struct ib_sa_mcmember_rec rec
;
3997 struct rdma_dev_addr
*dev_addr
= &id_priv
->id
.route
.addr
.dev_addr
;
3998 ib_sa_comp_mask comp_mask
;
4001 ib_addr_get_mgid(dev_addr
, &rec
.mgid
);
4002 ret
= ib_sa_get_mcmember_rec(id_priv
->id
.device
, id_priv
->id
.port_num
,
4007 ret
= cma_set_qkey(id_priv
, 0);
4011 cma_set_mgid(id_priv
, (struct sockaddr
*) &mc
->addr
, &rec
.mgid
);
4012 rec
.qkey
= cpu_to_be32(id_priv
->qkey
);
4013 rdma_addr_get_sgid(dev_addr
, &rec
.port_gid
);
4014 rec
.pkey
= cpu_to_be16(ib_addr_get_pkey(dev_addr
));
4015 rec
.join_state
= mc
->join_state
;
4017 if ((rec
.join_state
== BIT(SENDONLY_FULLMEMBER_JOIN
)) &&
4018 (!ib_sa_sendonly_fullmem_support(&sa_client
,
4020 id_priv
->id
.port_num
))) {
4021 pr_warn("RDMA CM: %s port %u Unable to multicast join\n"
4022 "RDMA CM: SM doesn't support Send Only Full Member option\n",
4023 id_priv
->id
.device
->name
, id_priv
->id
.port_num
);
4027 comp_mask
= IB_SA_MCMEMBER_REC_MGID
| IB_SA_MCMEMBER_REC_PORT_GID
|
4028 IB_SA_MCMEMBER_REC_PKEY
| IB_SA_MCMEMBER_REC_JOIN_STATE
|
4029 IB_SA_MCMEMBER_REC_QKEY
| IB_SA_MCMEMBER_REC_SL
|
4030 IB_SA_MCMEMBER_REC_FLOW_LABEL
|
4031 IB_SA_MCMEMBER_REC_TRAFFIC_CLASS
;
4033 if (id_priv
->id
.ps
== RDMA_PS_IPOIB
)
4034 comp_mask
|= IB_SA_MCMEMBER_REC_RATE
|
4035 IB_SA_MCMEMBER_REC_RATE_SELECTOR
|
4036 IB_SA_MCMEMBER_REC_MTU_SELECTOR
|
4037 IB_SA_MCMEMBER_REC_MTU
|
4038 IB_SA_MCMEMBER_REC_HOP_LIMIT
;
4040 mc
->multicast
.ib
= ib_sa_join_multicast(&sa_client
, id_priv
->id
.device
,
4041 id_priv
->id
.port_num
, &rec
,
4042 comp_mask
, GFP_KERNEL
,
4043 cma_ib_mc_handler
, mc
);
4044 return PTR_ERR_OR_ZERO(mc
->multicast
.ib
);
4047 static void iboe_mcast_work_handler(struct work_struct
*work
)
4049 struct iboe_mcast_work
*mw
= container_of(work
, struct iboe_mcast_work
, work
);
4050 struct cma_multicast
*mc
= mw
->mc
;
4051 struct ib_sa_multicast
*m
= mc
->multicast
.ib
;
4053 mc
->multicast
.ib
->context
= mc
;
4054 cma_ib_mc_handler(0, m
);
4055 kref_put(&mc
->mcref
, release_mc
);
4059 static void cma_iboe_set_mgid(struct sockaddr
*addr
, union ib_gid
*mgid
,
4060 enum ib_gid_type gid_type
)
4062 struct sockaddr_in
*sin
= (struct sockaddr_in
*)addr
;
4063 struct sockaddr_in6
*sin6
= (struct sockaddr_in6
*)addr
;
4065 if (cma_any_addr(addr
)) {
4066 memset(mgid
, 0, sizeof *mgid
);
4067 } else if (addr
->sa_family
== AF_INET6
) {
4068 memcpy(mgid
, &sin6
->sin6_addr
, sizeof *mgid
);
4071 (gid_type
== IB_GID_TYPE_ROCE_UDP_ENCAP
) ? 0 : 0xff;
4073 (gid_type
== IB_GID_TYPE_ROCE_UDP_ENCAP
) ? 0 : 0x0e;
4082 mgid
->raw
[10] = 0xff;
4083 mgid
->raw
[11] = 0xff;
4084 *(__be32
*)(&mgid
->raw
[12]) = sin
->sin_addr
.s_addr
;
4088 static int cma_iboe_join_multicast(struct rdma_id_private
*id_priv
,
4089 struct cma_multicast
*mc
)
4091 struct iboe_mcast_work
*work
;
4092 struct rdma_dev_addr
*dev_addr
= &id_priv
->id
.route
.addr
.dev_addr
;
4094 struct sockaddr
*addr
= (struct sockaddr
*)&mc
->addr
;
4095 struct net_device
*ndev
= NULL
;
4096 enum ib_gid_type gid_type
;
4099 send_only
= mc
->join_state
== BIT(SENDONLY_FULLMEMBER_JOIN
);
4101 if (cma_zero_addr((struct sockaddr
*)&mc
->addr
))
4104 work
= kzalloc(sizeof *work
, GFP_KERNEL
);
4108 mc
->multicast
.ib
= kzalloc(sizeof(struct ib_sa_multicast
), GFP_KERNEL
);
4109 if (!mc
->multicast
.ib
) {
4114 gid_type
= id_priv
->cma_dev
->default_gid_type
[id_priv
->id
.port_num
-
4115 rdma_start_port(id_priv
->cma_dev
->device
)];
4116 cma_iboe_set_mgid(addr
, &mc
->multicast
.ib
->rec
.mgid
, gid_type
);
4118 mc
->multicast
.ib
->rec
.pkey
= cpu_to_be16(0xffff);
4119 if (id_priv
->id
.ps
== RDMA_PS_UDP
)
4120 mc
->multicast
.ib
->rec
.qkey
= cpu_to_be32(RDMA_UDP_QKEY
);
4122 if (dev_addr
->bound_dev_if
)
4123 ndev
= dev_get_by_index(dev_addr
->net
, dev_addr
->bound_dev_if
);
4128 mc
->multicast
.ib
->rec
.rate
= iboe_get_rate(ndev
);
4129 mc
->multicast
.ib
->rec
.hop_limit
= 1;
4130 mc
->multicast
.ib
->rec
.mtu
= iboe_get_mtu(ndev
->mtu
);
4132 if (addr
->sa_family
== AF_INET
) {
4133 if (gid_type
== IB_GID_TYPE_ROCE_UDP_ENCAP
) {
4134 mc
->multicast
.ib
->rec
.hop_limit
= IPV6_DEFAULT_HOPLIMIT
;
4136 err
= cma_igmp_send(ndev
, &mc
->multicast
.ib
->rec
.mgid
,
4139 mc
->igmp_joined
= true;
4143 if (gid_type
== IB_GID_TYPE_ROCE_UDP_ENCAP
)
4147 if (err
|| !mc
->multicast
.ib
->rec
.mtu
) {
4152 rdma_ip2gid((struct sockaddr
*)&id_priv
->id
.route
.addr
.src_addr
,
4153 &mc
->multicast
.ib
->rec
.port_gid
);
4156 INIT_WORK(&work
->work
, iboe_mcast_work_handler
);
4157 kref_get(&mc
->mcref
);
4158 queue_work(cma_wq
, &work
->work
);
4163 kfree(mc
->multicast
.ib
);
4169 int rdma_join_multicast(struct rdma_cm_id
*id
, struct sockaddr
*addr
,
4170 u8 join_state
, void *context
)
4172 struct rdma_id_private
*id_priv
;
4173 struct cma_multicast
*mc
;
4176 id_priv
= container_of(id
, struct rdma_id_private
, id
);
4177 if (!cma_comp(id_priv
, RDMA_CM_ADDR_BOUND
) &&
4178 !cma_comp(id_priv
, RDMA_CM_ADDR_RESOLVED
))
4181 mc
= kmalloc(sizeof *mc
, GFP_KERNEL
);
4185 memcpy(&mc
->addr
, addr
, rdma_addr_size(addr
));
4186 mc
->context
= context
;
4187 mc
->id_priv
= id_priv
;
4188 mc
->igmp_joined
= false;
4189 mc
->join_state
= join_state
;
4190 spin_lock(&id_priv
->lock
);
4191 list_add(&mc
->list
, &id_priv
->mc_list
);
4192 spin_unlock(&id_priv
->lock
);
4194 if (rdma_protocol_roce(id
->device
, id
->port_num
)) {
4195 kref_init(&mc
->mcref
);
4196 ret
= cma_iboe_join_multicast(id_priv
, mc
);
4197 } else if (rdma_cap_ib_mcast(id
->device
, id
->port_num
))
4198 ret
= cma_join_ib_multicast(id_priv
, mc
);
4203 spin_lock_irq(&id_priv
->lock
);
4204 list_del(&mc
->list
);
4205 spin_unlock_irq(&id_priv
->lock
);
4210 EXPORT_SYMBOL(rdma_join_multicast
);
4212 void rdma_leave_multicast(struct rdma_cm_id
*id
, struct sockaddr
*addr
)
4214 struct rdma_id_private
*id_priv
;
4215 struct cma_multicast
*mc
;
4217 id_priv
= container_of(id
, struct rdma_id_private
, id
);
4218 spin_lock_irq(&id_priv
->lock
);
4219 list_for_each_entry(mc
, &id_priv
->mc_list
, list
) {
4220 if (!memcmp(&mc
->addr
, addr
, rdma_addr_size(addr
))) {
4221 list_del(&mc
->list
);
4222 spin_unlock_irq(&id_priv
->lock
);
4225 ib_detach_mcast(id
->qp
,
4226 &mc
->multicast
.ib
->rec
.mgid
,
4227 be16_to_cpu(mc
->multicast
.ib
->rec
.mlid
));
4229 BUG_ON(id_priv
->cma_dev
->device
!= id
->device
);
4231 if (rdma_cap_ib_mcast(id
->device
, id
->port_num
)) {
4232 ib_sa_free_multicast(mc
->multicast
.ib
);
4234 } else if (rdma_protocol_roce(id
->device
, id
->port_num
)) {
4235 if (mc
->igmp_joined
) {
4236 struct rdma_dev_addr
*dev_addr
=
4237 &id
->route
.addr
.dev_addr
;
4238 struct net_device
*ndev
= NULL
;
4240 if (dev_addr
->bound_dev_if
)
4241 ndev
= dev_get_by_index(dev_addr
->net
,
4242 dev_addr
->bound_dev_if
);
4245 &mc
->multicast
.ib
->rec
.mgid
,
4249 mc
->igmp_joined
= false;
4251 kref_put(&mc
->mcref
, release_mc
);
4256 spin_unlock_irq(&id_priv
->lock
);
4258 EXPORT_SYMBOL(rdma_leave_multicast
);
4260 static int cma_netdev_change(struct net_device
*ndev
, struct rdma_id_private
*id_priv
)
4262 struct rdma_dev_addr
*dev_addr
;
4263 struct cma_ndev_work
*work
;
4265 dev_addr
= &id_priv
->id
.route
.addr
.dev_addr
;
4267 if ((dev_addr
->bound_dev_if
== ndev
->ifindex
) &&
4268 (net_eq(dev_net(ndev
), dev_addr
->net
)) &&
4269 memcmp(dev_addr
->src_dev_addr
, ndev
->dev_addr
, ndev
->addr_len
)) {
4270 pr_info("RDMA CM addr change for ndev %s used by id %p\n",
4271 ndev
->name
, &id_priv
->id
);
4272 work
= kzalloc(sizeof *work
, GFP_KERNEL
);
4276 INIT_WORK(&work
->work
, cma_ndev_work_handler
);
4278 work
->event
.event
= RDMA_CM_EVENT_ADDR_CHANGE
;
4279 atomic_inc(&id_priv
->refcount
);
4280 queue_work(cma_wq
, &work
->work
);
4286 static int cma_netdev_callback(struct notifier_block
*self
, unsigned long event
,
4289 struct net_device
*ndev
= netdev_notifier_info_to_dev(ptr
);
4290 struct cma_device
*cma_dev
;
4291 struct rdma_id_private
*id_priv
;
4292 int ret
= NOTIFY_DONE
;
4294 if (event
!= NETDEV_BONDING_FAILOVER
)
4297 if (!netif_is_bond_master(ndev
))
4301 list_for_each_entry(cma_dev
, &dev_list
, list
)
4302 list_for_each_entry(id_priv
, &cma_dev
->id_list
, list
) {
4303 ret
= cma_netdev_change(ndev
, id_priv
);
4309 mutex_unlock(&lock
);
4313 static struct notifier_block cma_nb
= {
4314 .notifier_call
= cma_netdev_callback
4317 static void cma_add_one(struct ib_device
*device
)
4319 struct cma_device
*cma_dev
;
4320 struct rdma_id_private
*id_priv
;
4322 unsigned long supported_gids
= 0;
4324 cma_dev
= kmalloc(sizeof *cma_dev
, GFP_KERNEL
);
4328 cma_dev
->device
= device
;
4329 cma_dev
->default_gid_type
= kcalloc(device
->phys_port_cnt
,
4330 sizeof(*cma_dev
->default_gid_type
),
4332 if (!cma_dev
->default_gid_type
)
4335 cma_dev
->default_roce_tos
= kcalloc(device
->phys_port_cnt
,
4336 sizeof(*cma_dev
->default_roce_tos
),
4338 if (!cma_dev
->default_roce_tos
)
4341 for (i
= rdma_start_port(device
); i
<= rdma_end_port(device
); i
++) {
4342 supported_gids
= roce_gid_type_mask_support(device
, i
);
4343 WARN_ON(!supported_gids
);
4344 if (supported_gids
& (1 << CMA_PREFERRED_ROCE_GID_TYPE
))
4345 cma_dev
->default_gid_type
[i
- rdma_start_port(device
)] =
4346 CMA_PREFERRED_ROCE_GID_TYPE
;
4348 cma_dev
->default_gid_type
[i
- rdma_start_port(device
)] =
4349 find_first_bit(&supported_gids
, BITS_PER_LONG
);
4350 cma_dev
->default_roce_tos
[i
- rdma_start_port(device
)] = 0;
4353 init_completion(&cma_dev
->comp
);
4354 atomic_set(&cma_dev
->refcount
, 1);
4355 INIT_LIST_HEAD(&cma_dev
->id_list
);
4356 ib_set_client_data(device
, &cma_client
, cma_dev
);
4359 list_add_tail(&cma_dev
->list
, &dev_list
);
4360 list_for_each_entry(id_priv
, &listen_any_list
, list
)
4361 cma_listen_on_dev(id_priv
, cma_dev
);
4362 mutex_unlock(&lock
);
4367 kfree(cma_dev
->default_gid_type
);
4375 static int cma_remove_id_dev(struct rdma_id_private
*id_priv
)
4377 struct rdma_cm_event event
;
4378 enum rdma_cm_state state
;
4381 /* Record that we want to remove the device */
4382 state
= cma_exch(id_priv
, RDMA_CM_DEVICE_REMOVAL
);
4383 if (state
== RDMA_CM_DESTROYING
)
4386 cma_cancel_operation(id_priv
, state
);
4387 mutex_lock(&id_priv
->handler_mutex
);
4389 /* Check for destruction from another callback. */
4390 if (!cma_comp(id_priv
, RDMA_CM_DEVICE_REMOVAL
))
4393 memset(&event
, 0, sizeof event
);
4394 event
.event
= RDMA_CM_EVENT_DEVICE_REMOVAL
;
4395 ret
= id_priv
->id
.event_handler(&id_priv
->id
, &event
);
4397 mutex_unlock(&id_priv
->handler_mutex
);
4401 static void cma_process_remove(struct cma_device
*cma_dev
)
4403 struct rdma_id_private
*id_priv
;
4407 while (!list_empty(&cma_dev
->id_list
)) {
4408 id_priv
= list_entry(cma_dev
->id_list
.next
,
4409 struct rdma_id_private
, list
);
4411 list_del(&id_priv
->listen_list
);
4412 list_del_init(&id_priv
->list
);
4413 atomic_inc(&id_priv
->refcount
);
4414 mutex_unlock(&lock
);
4416 ret
= id_priv
->internal_id
? 1 : cma_remove_id_dev(id_priv
);
4417 cma_deref_id(id_priv
);
4419 rdma_destroy_id(&id_priv
->id
);
4423 mutex_unlock(&lock
);
4425 cma_deref_dev(cma_dev
);
4426 wait_for_completion(&cma_dev
->comp
);
4429 static void cma_remove_one(struct ib_device
*device
, void *client_data
)
4431 struct cma_device
*cma_dev
= client_data
;
4437 list_del(&cma_dev
->list
);
4438 mutex_unlock(&lock
);
4440 cma_process_remove(cma_dev
);
4441 kfree(cma_dev
->default_roce_tos
);
4442 kfree(cma_dev
->default_gid_type
);
4446 static int cma_get_id_stats(struct sk_buff
*skb
, struct netlink_callback
*cb
)
4448 struct nlmsghdr
*nlh
;
4449 struct rdma_cm_id_stats
*id_stats
;
4450 struct rdma_id_private
*id_priv
;
4451 struct rdma_cm_id
*id
= NULL
;
4452 struct cma_device
*cma_dev
;
4453 int i_dev
= 0, i_id
= 0;
4456 * We export all of the IDs as a sequence of messages. Each
4457 * ID gets its own netlink message.
4461 list_for_each_entry(cma_dev
, &dev_list
, list
) {
4462 if (i_dev
< cb
->args
[0]) {
4468 list_for_each_entry(id_priv
, &cma_dev
->id_list
, list
) {
4469 if (i_id
< cb
->args
[1]) {
4474 id_stats
= ibnl_put_msg(skb
, &nlh
, cb
->nlh
->nlmsg_seq
,
4475 sizeof *id_stats
, RDMA_NL_RDMA_CM
,
4476 RDMA_NL_RDMA_CM_ID_STATS
,
4481 memset(id_stats
, 0, sizeof *id_stats
);
4483 id_stats
->node_type
= id
->route
.addr
.dev_addr
.dev_type
;
4484 id_stats
->port_num
= id
->port_num
;
4485 id_stats
->bound_dev_if
=
4486 id
->route
.addr
.dev_addr
.bound_dev_if
;
4488 if (ibnl_put_attr(skb
, nlh
,
4489 rdma_addr_size(cma_src_addr(id_priv
)),
4490 cma_src_addr(id_priv
),
4491 RDMA_NL_RDMA_CM_ATTR_SRC_ADDR
))
4493 if (ibnl_put_attr(skb
, nlh
,
4494 rdma_addr_size(cma_dst_addr(id_priv
)),
4495 cma_dst_addr(id_priv
),
4496 RDMA_NL_RDMA_CM_ATTR_DST_ADDR
))
4499 id_stats
->pid
= id_priv
->owner
;
4500 id_stats
->port_space
= id
->ps
;
4501 id_stats
->cm_state
= id_priv
->state
;
4502 id_stats
->qp_num
= id_priv
->qp_num
;
4503 id_stats
->qp_type
= id
->qp_type
;
4506 nlmsg_end(skb
, nlh
);
4514 mutex_unlock(&lock
);
4515 cb
->args
[0] = i_dev
;
4521 static const struct rdma_nl_cbs cma_cb_table
[RDMA_NL_RDMA_CM_NUM_OPS
] = {
4522 [RDMA_NL_RDMA_CM_ID_STATS
] = { .dump
= cma_get_id_stats
},
4525 static int cma_init_net(struct net
*net
)
4527 struct cma_pernet
*pernet
= cma_pernet(net
);
4529 idr_init(&pernet
->tcp_ps
);
4530 idr_init(&pernet
->udp_ps
);
4531 idr_init(&pernet
->ipoib_ps
);
4532 idr_init(&pernet
->ib_ps
);
4537 static void cma_exit_net(struct net
*net
)
4539 struct cma_pernet
*pernet
= cma_pernet(net
);
4541 idr_destroy(&pernet
->tcp_ps
);
4542 idr_destroy(&pernet
->udp_ps
);
4543 idr_destroy(&pernet
->ipoib_ps
);
4544 idr_destroy(&pernet
->ib_ps
);
4547 static struct pernet_operations cma_pernet_operations
= {
4548 .init
= cma_init_net
,
4549 .exit
= cma_exit_net
,
4550 .id
= &cma_pernet_id
,
4551 .size
= sizeof(struct cma_pernet
),
4554 static int __init
cma_init(void)
4558 cma_wq
= alloc_ordered_workqueue("rdma_cm", WQ_MEM_RECLAIM
);
4562 ret
= register_pernet_subsys(&cma_pernet_operations
);
4566 ib_sa_register_client(&sa_client
);
4567 rdma_addr_register_client(&addr_client
);
4568 register_netdevice_notifier(&cma_nb
);
4570 ret
= ib_register_client(&cma_client
);
4574 rdma_nl_register(RDMA_NL_RDMA_CM
, cma_cb_table
);
4575 cma_configfs_init();
4580 unregister_netdevice_notifier(&cma_nb
);
4581 rdma_addr_unregister_client(&addr_client
);
4582 ib_sa_unregister_client(&sa_client
);
4584 destroy_workqueue(cma_wq
);
4588 static void __exit
cma_cleanup(void)
4590 cma_configfs_exit();
4591 rdma_nl_unregister(RDMA_NL_RDMA_CM
);
4592 ib_unregister_client(&cma_client
);
4593 unregister_netdevice_notifier(&cma_nb
);
4594 rdma_addr_unregister_client(&addr_client
);
4595 ib_sa_unregister_client(&sa_client
);
4596 unregister_pernet_subsys(&cma_pernet_operations
);
4597 destroy_workqueue(cma_wq
);
4600 MODULE_ALIAS_RDMA_NETLINK(RDMA_NL_RDMA_CM
, 1);
4602 module_init(cma_init
);
4603 module_exit(cma_cleanup
);