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/idr.h>
42 #include <linux/inetdevice.h>
43 #include <linux/slab.h>
44 #include <linux/module.h>
45 #include <net/route.h>
47 #include <net/net_namespace.h>
48 #include <net/netns/generic.h>
51 #include <net/ip_fib.h>
52 #include <net/ip6_route.h>
54 #include <rdma/rdma_cm.h>
55 #include <rdma/rdma_cm_ib.h>
56 #include <rdma/rdma_netlink.h>
58 #include <rdma/ib_cache.h>
59 #include <rdma/ib_cm.h>
60 #include <rdma/ib_sa.h>
61 #include <rdma/iw_cm.h>
63 MODULE_AUTHOR("Sean Hefty");
64 MODULE_DESCRIPTION("Generic RDMA CM Agent");
65 MODULE_LICENSE("Dual BSD/GPL");
67 #define CMA_CM_RESPONSE_TIMEOUT 20
68 #define CMA_MAX_CM_RETRIES 15
69 #define CMA_CM_MRA_SETTING (IB_CM_MRA_FLAG_DELAY | 24)
70 #define CMA_IBOE_PACKET_LIFETIME 18
72 static const char * const cma_events
[] = {
73 [RDMA_CM_EVENT_ADDR_RESOLVED
] = "address resolved",
74 [RDMA_CM_EVENT_ADDR_ERROR
] = "address error",
75 [RDMA_CM_EVENT_ROUTE_RESOLVED
] = "route resolved ",
76 [RDMA_CM_EVENT_ROUTE_ERROR
] = "route error",
77 [RDMA_CM_EVENT_CONNECT_REQUEST
] = "connect request",
78 [RDMA_CM_EVENT_CONNECT_RESPONSE
] = "connect response",
79 [RDMA_CM_EVENT_CONNECT_ERROR
] = "connect error",
80 [RDMA_CM_EVENT_UNREACHABLE
] = "unreachable",
81 [RDMA_CM_EVENT_REJECTED
] = "rejected",
82 [RDMA_CM_EVENT_ESTABLISHED
] = "established",
83 [RDMA_CM_EVENT_DISCONNECTED
] = "disconnected",
84 [RDMA_CM_EVENT_DEVICE_REMOVAL
] = "device removal",
85 [RDMA_CM_EVENT_MULTICAST_JOIN
] = "multicast join",
86 [RDMA_CM_EVENT_MULTICAST_ERROR
] = "multicast error",
87 [RDMA_CM_EVENT_ADDR_CHANGE
] = "address change",
88 [RDMA_CM_EVENT_TIMEWAIT_EXIT
] = "timewait exit",
91 const char *__attribute_const__
rdma_event_msg(enum rdma_cm_event_type event
)
95 return (index
< ARRAY_SIZE(cma_events
) && cma_events
[index
]) ?
96 cma_events
[index
] : "unrecognized event";
98 EXPORT_SYMBOL(rdma_event_msg
);
100 static void cma_add_one(struct ib_device
*device
);
101 static void cma_remove_one(struct ib_device
*device
, void *client_data
);
103 static struct ib_client cma_client
= {
106 .remove
= cma_remove_one
109 static struct ib_sa_client sa_client
;
110 static struct rdma_addr_client addr_client
;
111 static LIST_HEAD(dev_list
);
112 static LIST_HEAD(listen_any_list
);
113 static DEFINE_MUTEX(lock
);
114 static struct workqueue_struct
*cma_wq
;
115 static int cma_pernet_id
;
124 static struct cma_pernet
*cma_pernet(struct net
*net
)
126 return net_generic(net
, cma_pernet_id
);
129 static struct idr
*cma_pernet_idr(struct net
*net
, enum rdma_port_space ps
)
131 struct cma_pernet
*pernet
= cma_pernet(net
);
135 return &pernet
->tcp_ps
;
137 return &pernet
->udp_ps
;
139 return &pernet
->ipoib_ps
;
141 return &pernet
->ib_ps
;
148 struct list_head list
;
149 struct ib_device
*device
;
150 struct completion comp
;
152 struct list_head id_list
;
155 struct rdma_bind_list
{
156 enum rdma_port_space ps
;
157 struct hlist_head owners
;
161 static int cma_ps_alloc(struct net
*net
, enum rdma_port_space ps
,
162 struct rdma_bind_list
*bind_list
, int snum
)
164 struct idr
*idr
= cma_pernet_idr(net
, ps
);
166 return idr_alloc(idr
, bind_list
, snum
, snum
+ 1, GFP_KERNEL
);
169 static struct rdma_bind_list
*cma_ps_find(struct net
*net
,
170 enum rdma_port_space ps
, int snum
)
172 struct idr
*idr
= cma_pernet_idr(net
, ps
);
174 return idr_find(idr
, snum
);
177 static void cma_ps_remove(struct net
*net
, enum rdma_port_space ps
, int snum
)
179 struct idr
*idr
= cma_pernet_idr(net
, ps
);
181 idr_remove(idr
, snum
);
189 * Device removal can occur at anytime, so we need extra handling to
190 * serialize notifying the user of device removal with other callbacks.
191 * We do this by disabling removal notification while a callback is in process,
192 * and reporting it after the callback completes.
194 struct rdma_id_private
{
195 struct rdma_cm_id id
;
197 struct rdma_bind_list
*bind_list
;
198 struct hlist_node node
;
199 struct list_head list
; /* listen_any_list or cma_device.list */
200 struct list_head listen_list
; /* per device listens */
201 struct cma_device
*cma_dev
;
202 struct list_head mc_list
;
205 enum rdma_cm_state state
;
207 struct mutex qp_mutex
;
209 struct completion comp
;
211 struct mutex handler_mutex
;
215 struct ib_sa_query
*query
;
233 struct cma_multicast
{
234 struct rdma_id_private
*id_priv
;
236 struct ib_sa_multicast
*ib
;
238 struct list_head list
;
240 struct sockaddr_storage addr
;
245 struct work_struct work
;
246 struct rdma_id_private
*id
;
247 enum rdma_cm_state old_state
;
248 enum rdma_cm_state new_state
;
249 struct rdma_cm_event event
;
252 struct cma_ndev_work
{
253 struct work_struct work
;
254 struct rdma_id_private
*id
;
255 struct rdma_cm_event event
;
258 struct iboe_mcast_work
{
259 struct work_struct work
;
260 struct rdma_id_private
*id
;
261 struct cma_multicast
*mc
;
274 u8 ip_version
; /* IP version: 7:4 */
276 union cma_ip_addr src_addr
;
277 union cma_ip_addr dst_addr
;
280 #define CMA_VERSION 0x00
282 struct cma_req_info
{
283 struct ib_device
*device
;
285 union ib_gid local_gid
;
291 static int cma_comp(struct rdma_id_private
*id_priv
, enum rdma_cm_state comp
)
296 spin_lock_irqsave(&id_priv
->lock
, flags
);
297 ret
= (id_priv
->state
== comp
);
298 spin_unlock_irqrestore(&id_priv
->lock
, flags
);
302 static int cma_comp_exch(struct rdma_id_private
*id_priv
,
303 enum rdma_cm_state comp
, enum rdma_cm_state exch
)
308 spin_lock_irqsave(&id_priv
->lock
, flags
);
309 if ((ret
= (id_priv
->state
== comp
)))
310 id_priv
->state
= exch
;
311 spin_unlock_irqrestore(&id_priv
->lock
, flags
);
315 static enum rdma_cm_state
cma_exch(struct rdma_id_private
*id_priv
,
316 enum rdma_cm_state exch
)
319 enum rdma_cm_state old
;
321 spin_lock_irqsave(&id_priv
->lock
, flags
);
322 old
= id_priv
->state
;
323 id_priv
->state
= exch
;
324 spin_unlock_irqrestore(&id_priv
->lock
, flags
);
328 static inline u8
cma_get_ip_ver(const struct cma_hdr
*hdr
)
330 return hdr
->ip_version
>> 4;
333 static inline void cma_set_ip_ver(struct cma_hdr
*hdr
, u8 ip_ver
)
335 hdr
->ip_version
= (ip_ver
<< 4) | (hdr
->ip_version
& 0xF);
338 static void cma_attach_to_dev(struct rdma_id_private
*id_priv
,
339 struct cma_device
*cma_dev
)
341 atomic_inc(&cma_dev
->refcount
);
342 id_priv
->cma_dev
= cma_dev
;
343 id_priv
->id
.device
= cma_dev
->device
;
344 id_priv
->id
.route
.addr
.dev_addr
.transport
=
345 rdma_node_get_transport(cma_dev
->device
->node_type
);
346 list_add_tail(&id_priv
->list
, &cma_dev
->id_list
);
349 static inline void cma_deref_dev(struct cma_device
*cma_dev
)
351 if (atomic_dec_and_test(&cma_dev
->refcount
))
352 complete(&cma_dev
->comp
);
355 static inline void release_mc(struct kref
*kref
)
357 struct cma_multicast
*mc
= container_of(kref
, struct cma_multicast
, mcref
);
359 kfree(mc
->multicast
.ib
);
363 static void cma_release_dev(struct rdma_id_private
*id_priv
)
366 list_del(&id_priv
->list
);
367 cma_deref_dev(id_priv
->cma_dev
);
368 id_priv
->cma_dev
= NULL
;
372 static inline struct sockaddr
*cma_src_addr(struct rdma_id_private
*id_priv
)
374 return (struct sockaddr
*) &id_priv
->id
.route
.addr
.src_addr
;
377 static inline struct sockaddr
*cma_dst_addr(struct rdma_id_private
*id_priv
)
379 return (struct sockaddr
*) &id_priv
->id
.route
.addr
.dst_addr
;
382 static inline unsigned short cma_family(struct rdma_id_private
*id_priv
)
384 return id_priv
->id
.route
.addr
.src_addr
.ss_family
;
387 static int cma_set_qkey(struct rdma_id_private
*id_priv
, u32 qkey
)
389 struct ib_sa_mcmember_rec rec
;
393 if (qkey
&& id_priv
->qkey
!= qkey
)
399 id_priv
->qkey
= qkey
;
403 switch (id_priv
->id
.ps
) {
406 id_priv
->qkey
= RDMA_UDP_QKEY
;
409 ib_addr_get_mgid(&id_priv
->id
.route
.addr
.dev_addr
, &rec
.mgid
);
410 ret
= ib_sa_get_mcmember_rec(id_priv
->id
.device
,
411 id_priv
->id
.port_num
, &rec
.mgid
,
414 id_priv
->qkey
= be32_to_cpu(rec
.qkey
);
422 static void cma_translate_ib(struct sockaddr_ib
*sib
, struct rdma_dev_addr
*dev_addr
)
424 dev_addr
->dev_type
= ARPHRD_INFINIBAND
;
425 rdma_addr_set_sgid(dev_addr
, (union ib_gid
*) &sib
->sib_addr
);
426 ib_addr_set_pkey(dev_addr
, ntohs(sib
->sib_pkey
));
429 static int cma_translate_addr(struct sockaddr
*addr
, struct rdma_dev_addr
*dev_addr
)
433 if (addr
->sa_family
!= AF_IB
) {
434 ret
= rdma_translate_ip(addr
, dev_addr
, NULL
);
436 cma_translate_ib((struct sockaddr_ib
*) addr
, dev_addr
);
443 static inline int cma_validate_port(struct ib_device
*device
, u8 port
,
444 union ib_gid
*gid
, int dev_type
,
448 struct net_device
*ndev
= NULL
;
450 if ((dev_type
== ARPHRD_INFINIBAND
) && !rdma_protocol_ib(device
, port
))
453 if ((dev_type
!= ARPHRD_INFINIBAND
) && rdma_protocol_ib(device
, port
))
456 if (dev_type
== ARPHRD_ETHER
)
457 ndev
= dev_get_by_index(&init_net
, bound_if_index
);
459 ret
= ib_find_cached_gid_by_port(device
, gid
, port
, ndev
, NULL
);
467 static int cma_acquire_dev(struct rdma_id_private
*id_priv
,
468 struct rdma_id_private
*listen_id_priv
)
470 struct rdma_dev_addr
*dev_addr
= &id_priv
->id
.route
.addr
.dev_addr
;
471 struct cma_device
*cma_dev
;
472 union ib_gid gid
, iboe_gid
, *gidp
;
476 if (dev_addr
->dev_type
!= ARPHRD_INFINIBAND
&&
477 id_priv
->id
.ps
== RDMA_PS_IPOIB
)
481 rdma_ip2gid((struct sockaddr
*)&id_priv
->id
.route
.addr
.src_addr
,
484 memcpy(&gid
, dev_addr
->src_dev_addr
+
485 rdma_addr_gid_offset(dev_addr
), sizeof gid
);
487 if (listen_id_priv
) {
488 cma_dev
= listen_id_priv
->cma_dev
;
489 port
= listen_id_priv
->id
.port_num
;
490 gidp
= rdma_protocol_roce(cma_dev
->device
, port
) ?
493 ret
= cma_validate_port(cma_dev
->device
, port
, gidp
,
495 dev_addr
->bound_dev_if
);
497 id_priv
->id
.port_num
= port
;
502 list_for_each_entry(cma_dev
, &dev_list
, list
) {
503 for (port
= 1; port
<= cma_dev
->device
->phys_port_cnt
; ++port
) {
504 if (listen_id_priv
&&
505 listen_id_priv
->cma_dev
== cma_dev
&&
506 listen_id_priv
->id
.port_num
== port
)
509 gidp
= rdma_protocol_roce(cma_dev
->device
, port
) ?
512 ret
= cma_validate_port(cma_dev
->device
, port
, gidp
,
514 dev_addr
->bound_dev_if
);
516 id_priv
->id
.port_num
= port
;
524 cma_attach_to_dev(id_priv
, cma_dev
);
531 * Select the source IB device and address to reach the destination IB address.
533 static int cma_resolve_ib_dev(struct rdma_id_private
*id_priv
)
535 struct cma_device
*cma_dev
, *cur_dev
;
536 struct sockaddr_ib
*addr
;
537 union ib_gid gid
, sgid
, *dgid
;
543 addr
= (struct sockaddr_ib
*) cma_dst_addr(id_priv
);
544 dgid
= (union ib_gid
*) &addr
->sib_addr
;
545 pkey
= ntohs(addr
->sib_pkey
);
547 list_for_each_entry(cur_dev
, &dev_list
, list
) {
548 for (p
= 1; p
<= cur_dev
->device
->phys_port_cnt
; ++p
) {
549 if (!rdma_cap_af_ib(cur_dev
->device
, p
))
552 if (ib_find_cached_pkey(cur_dev
->device
, p
, pkey
, &index
))
555 for (i
= 0; !ib_get_cached_gid(cur_dev
->device
, p
, i
,
558 if (!memcmp(&gid
, dgid
, sizeof(gid
))) {
561 id_priv
->id
.port_num
= p
;
565 if (!cma_dev
&& (gid
.global
.subnet_prefix
==
566 dgid
->global
.subnet_prefix
)) {
569 id_priv
->id
.port_num
= p
;
579 cma_attach_to_dev(id_priv
, cma_dev
);
580 addr
= (struct sockaddr_ib
*) cma_src_addr(id_priv
);
581 memcpy(&addr
->sib_addr
, &sgid
, sizeof sgid
);
582 cma_translate_ib(addr
, &id_priv
->id
.route
.addr
.dev_addr
);
586 static void cma_deref_id(struct rdma_id_private
*id_priv
)
588 if (atomic_dec_and_test(&id_priv
->refcount
))
589 complete(&id_priv
->comp
);
592 static int cma_disable_callback(struct rdma_id_private
*id_priv
,
593 enum rdma_cm_state state
)
595 mutex_lock(&id_priv
->handler_mutex
);
596 if (id_priv
->state
!= state
) {
597 mutex_unlock(&id_priv
->handler_mutex
);
603 struct rdma_cm_id
*rdma_create_id(struct net
*net
,
604 rdma_cm_event_handler event_handler
,
605 void *context
, enum rdma_port_space ps
,
606 enum ib_qp_type qp_type
)
608 struct rdma_id_private
*id_priv
;
610 id_priv
= kzalloc(sizeof *id_priv
, GFP_KERNEL
);
612 return ERR_PTR(-ENOMEM
);
614 id_priv
->owner
= task_pid_nr(current
);
615 id_priv
->state
= RDMA_CM_IDLE
;
616 id_priv
->id
.context
= context
;
617 id_priv
->id
.event_handler
= event_handler
;
619 id_priv
->id
.qp_type
= qp_type
;
620 spin_lock_init(&id_priv
->lock
);
621 mutex_init(&id_priv
->qp_mutex
);
622 init_completion(&id_priv
->comp
);
623 atomic_set(&id_priv
->refcount
, 1);
624 mutex_init(&id_priv
->handler_mutex
);
625 INIT_LIST_HEAD(&id_priv
->listen_list
);
626 INIT_LIST_HEAD(&id_priv
->mc_list
);
627 get_random_bytes(&id_priv
->seq_num
, sizeof id_priv
->seq_num
);
628 id_priv
->id
.route
.addr
.dev_addr
.net
= get_net(net
);
632 EXPORT_SYMBOL(rdma_create_id
);
634 static int cma_init_ud_qp(struct rdma_id_private
*id_priv
, struct ib_qp
*qp
)
636 struct ib_qp_attr qp_attr
;
637 int qp_attr_mask
, ret
;
639 qp_attr
.qp_state
= IB_QPS_INIT
;
640 ret
= rdma_init_qp_attr(&id_priv
->id
, &qp_attr
, &qp_attr_mask
);
644 ret
= ib_modify_qp(qp
, &qp_attr
, qp_attr_mask
);
648 qp_attr
.qp_state
= IB_QPS_RTR
;
649 ret
= ib_modify_qp(qp
, &qp_attr
, IB_QP_STATE
);
653 qp_attr
.qp_state
= IB_QPS_RTS
;
655 ret
= ib_modify_qp(qp
, &qp_attr
, IB_QP_STATE
| IB_QP_SQ_PSN
);
660 static int cma_init_conn_qp(struct rdma_id_private
*id_priv
, struct ib_qp
*qp
)
662 struct ib_qp_attr qp_attr
;
663 int qp_attr_mask
, ret
;
665 qp_attr
.qp_state
= IB_QPS_INIT
;
666 ret
= rdma_init_qp_attr(&id_priv
->id
, &qp_attr
, &qp_attr_mask
);
670 return ib_modify_qp(qp
, &qp_attr
, qp_attr_mask
);
673 int rdma_create_qp(struct rdma_cm_id
*id
, struct ib_pd
*pd
,
674 struct ib_qp_init_attr
*qp_init_attr
)
676 struct rdma_id_private
*id_priv
;
680 id_priv
= container_of(id
, struct rdma_id_private
, id
);
681 if (id
->device
!= pd
->device
)
684 qp
= ib_create_qp(pd
, qp_init_attr
);
688 if (id
->qp_type
== IB_QPT_UD
)
689 ret
= cma_init_ud_qp(id_priv
, qp
);
691 ret
= cma_init_conn_qp(id_priv
, qp
);
696 id_priv
->qp_num
= qp
->qp_num
;
697 id_priv
->srq
= (qp
->srq
!= NULL
);
703 EXPORT_SYMBOL(rdma_create_qp
);
705 void rdma_destroy_qp(struct rdma_cm_id
*id
)
707 struct rdma_id_private
*id_priv
;
709 id_priv
= container_of(id
, struct rdma_id_private
, id
);
710 mutex_lock(&id_priv
->qp_mutex
);
711 ib_destroy_qp(id_priv
->id
.qp
);
712 id_priv
->id
.qp
= NULL
;
713 mutex_unlock(&id_priv
->qp_mutex
);
715 EXPORT_SYMBOL(rdma_destroy_qp
);
717 static int cma_modify_qp_rtr(struct rdma_id_private
*id_priv
,
718 struct rdma_conn_param
*conn_param
)
720 struct ib_qp_attr qp_attr
;
721 int qp_attr_mask
, ret
;
724 mutex_lock(&id_priv
->qp_mutex
);
725 if (!id_priv
->id
.qp
) {
730 /* Need to update QP attributes from default values. */
731 qp_attr
.qp_state
= IB_QPS_INIT
;
732 ret
= rdma_init_qp_attr(&id_priv
->id
, &qp_attr
, &qp_attr_mask
);
736 ret
= ib_modify_qp(id_priv
->id
.qp
, &qp_attr
, qp_attr_mask
);
740 qp_attr
.qp_state
= IB_QPS_RTR
;
741 ret
= rdma_init_qp_attr(&id_priv
->id
, &qp_attr
, &qp_attr_mask
);
745 ret
= ib_query_gid(id_priv
->id
.device
, id_priv
->id
.port_num
,
746 qp_attr
.ah_attr
.grh
.sgid_index
, &sgid
, NULL
);
750 BUG_ON(id_priv
->cma_dev
->device
!= id_priv
->id
.device
);
753 qp_attr
.max_dest_rd_atomic
= conn_param
->responder_resources
;
754 ret
= ib_modify_qp(id_priv
->id
.qp
, &qp_attr
, qp_attr_mask
);
756 mutex_unlock(&id_priv
->qp_mutex
);
760 static int cma_modify_qp_rts(struct rdma_id_private
*id_priv
,
761 struct rdma_conn_param
*conn_param
)
763 struct ib_qp_attr qp_attr
;
764 int qp_attr_mask
, ret
;
766 mutex_lock(&id_priv
->qp_mutex
);
767 if (!id_priv
->id
.qp
) {
772 qp_attr
.qp_state
= IB_QPS_RTS
;
773 ret
= rdma_init_qp_attr(&id_priv
->id
, &qp_attr
, &qp_attr_mask
);
778 qp_attr
.max_rd_atomic
= conn_param
->initiator_depth
;
779 ret
= ib_modify_qp(id_priv
->id
.qp
, &qp_attr
, qp_attr_mask
);
781 mutex_unlock(&id_priv
->qp_mutex
);
785 static int cma_modify_qp_err(struct rdma_id_private
*id_priv
)
787 struct ib_qp_attr qp_attr
;
790 mutex_lock(&id_priv
->qp_mutex
);
791 if (!id_priv
->id
.qp
) {
796 qp_attr
.qp_state
= IB_QPS_ERR
;
797 ret
= ib_modify_qp(id_priv
->id
.qp
, &qp_attr
, IB_QP_STATE
);
799 mutex_unlock(&id_priv
->qp_mutex
);
803 static int cma_ib_init_qp_attr(struct rdma_id_private
*id_priv
,
804 struct ib_qp_attr
*qp_attr
, int *qp_attr_mask
)
806 struct rdma_dev_addr
*dev_addr
= &id_priv
->id
.route
.addr
.dev_addr
;
810 if (rdma_cap_eth_ah(id_priv
->id
.device
, id_priv
->id
.port_num
))
813 pkey
= ib_addr_get_pkey(dev_addr
);
815 ret
= ib_find_cached_pkey(id_priv
->id
.device
, id_priv
->id
.port_num
,
816 pkey
, &qp_attr
->pkey_index
);
820 qp_attr
->port_num
= id_priv
->id
.port_num
;
821 *qp_attr_mask
= IB_QP_STATE
| IB_QP_PKEY_INDEX
| IB_QP_PORT
;
823 if (id_priv
->id
.qp_type
== IB_QPT_UD
) {
824 ret
= cma_set_qkey(id_priv
, 0);
828 qp_attr
->qkey
= id_priv
->qkey
;
829 *qp_attr_mask
|= IB_QP_QKEY
;
831 qp_attr
->qp_access_flags
= 0;
832 *qp_attr_mask
|= IB_QP_ACCESS_FLAGS
;
837 int rdma_init_qp_attr(struct rdma_cm_id
*id
, struct ib_qp_attr
*qp_attr
,
840 struct rdma_id_private
*id_priv
;
843 id_priv
= container_of(id
, struct rdma_id_private
, id
);
844 if (rdma_cap_ib_cm(id
->device
, id
->port_num
)) {
845 if (!id_priv
->cm_id
.ib
|| (id_priv
->id
.qp_type
== IB_QPT_UD
))
846 ret
= cma_ib_init_qp_attr(id_priv
, qp_attr
, qp_attr_mask
);
848 ret
= ib_cm_init_qp_attr(id_priv
->cm_id
.ib
, qp_attr
,
851 if (qp_attr
->qp_state
== IB_QPS_RTR
)
852 qp_attr
->rq_psn
= id_priv
->seq_num
;
853 } else if (rdma_cap_iw_cm(id
->device
, id
->port_num
)) {
854 if (!id_priv
->cm_id
.iw
) {
855 qp_attr
->qp_access_flags
= 0;
856 *qp_attr_mask
= IB_QP_STATE
| IB_QP_ACCESS_FLAGS
;
858 ret
= iw_cm_init_qp_attr(id_priv
->cm_id
.iw
, qp_attr
,
865 EXPORT_SYMBOL(rdma_init_qp_attr
);
867 static inline int cma_zero_addr(struct sockaddr
*addr
)
869 switch (addr
->sa_family
) {
871 return ipv4_is_zeronet(((struct sockaddr_in
*)addr
)->sin_addr
.s_addr
);
873 return ipv6_addr_any(&((struct sockaddr_in6
*) addr
)->sin6_addr
);
875 return ib_addr_any(&((struct sockaddr_ib
*) addr
)->sib_addr
);
881 static inline int cma_loopback_addr(struct sockaddr
*addr
)
883 switch (addr
->sa_family
) {
885 return ipv4_is_loopback(((struct sockaddr_in
*) addr
)->sin_addr
.s_addr
);
887 return ipv6_addr_loopback(&((struct sockaddr_in6
*) addr
)->sin6_addr
);
889 return ib_addr_loopback(&((struct sockaddr_ib
*) addr
)->sib_addr
);
895 static inline int cma_any_addr(struct sockaddr
*addr
)
897 return cma_zero_addr(addr
) || cma_loopback_addr(addr
);
900 static int cma_addr_cmp(struct sockaddr
*src
, struct sockaddr
*dst
)
902 if (src
->sa_family
!= dst
->sa_family
)
905 switch (src
->sa_family
) {
907 return ((struct sockaddr_in
*) src
)->sin_addr
.s_addr
!=
908 ((struct sockaddr_in
*) dst
)->sin_addr
.s_addr
;
910 return ipv6_addr_cmp(&((struct sockaddr_in6
*) src
)->sin6_addr
,
911 &((struct sockaddr_in6
*) dst
)->sin6_addr
);
913 return ib_addr_cmp(&((struct sockaddr_ib
*) src
)->sib_addr
,
914 &((struct sockaddr_ib
*) dst
)->sib_addr
);
918 static __be16
cma_port(struct sockaddr
*addr
)
920 struct sockaddr_ib
*sib
;
922 switch (addr
->sa_family
) {
924 return ((struct sockaddr_in
*) addr
)->sin_port
;
926 return ((struct sockaddr_in6
*) addr
)->sin6_port
;
928 sib
= (struct sockaddr_ib
*) addr
;
929 return htons((u16
) (be64_to_cpu(sib
->sib_sid
) &
930 be64_to_cpu(sib
->sib_sid_mask
)));
936 static inline int cma_any_port(struct sockaddr
*addr
)
938 return !cma_port(addr
);
941 static void cma_save_ib_info(struct sockaddr
*src_addr
,
942 struct sockaddr
*dst_addr
,
943 struct rdma_cm_id
*listen_id
,
944 struct ib_sa_path_rec
*path
)
946 struct sockaddr_ib
*listen_ib
, *ib
;
948 listen_ib
= (struct sockaddr_ib
*) &listen_id
->route
.addr
.src_addr
;
950 ib
= (struct sockaddr_ib
*)src_addr
;
951 ib
->sib_family
= AF_IB
;
953 ib
->sib_pkey
= path
->pkey
;
954 ib
->sib_flowinfo
= path
->flow_label
;
955 memcpy(&ib
->sib_addr
, &path
->sgid
, 16);
956 ib
->sib_sid
= path
->service_id
;
957 ib
->sib_scope_id
= 0;
959 ib
->sib_pkey
= listen_ib
->sib_pkey
;
960 ib
->sib_flowinfo
= listen_ib
->sib_flowinfo
;
961 ib
->sib_addr
= listen_ib
->sib_addr
;
962 ib
->sib_sid
= listen_ib
->sib_sid
;
963 ib
->sib_scope_id
= listen_ib
->sib_scope_id
;
965 ib
->sib_sid_mask
= cpu_to_be64(0xffffffffffffffffULL
);
968 ib
= (struct sockaddr_ib
*)dst_addr
;
969 ib
->sib_family
= AF_IB
;
971 ib
->sib_pkey
= path
->pkey
;
972 ib
->sib_flowinfo
= path
->flow_label
;
973 memcpy(&ib
->sib_addr
, &path
->dgid
, 16);
978 static void cma_save_ip4_info(struct sockaddr
*src_addr
,
979 struct sockaddr
*dst_addr
,
983 struct sockaddr_in
*ip4
;
986 ip4
= (struct sockaddr_in
*)src_addr
;
987 ip4
->sin_family
= AF_INET
;
988 ip4
->sin_addr
.s_addr
= hdr
->dst_addr
.ip4
.addr
;
989 ip4
->sin_port
= local_port
;
993 ip4
= (struct sockaddr_in
*)dst_addr
;
994 ip4
->sin_family
= AF_INET
;
995 ip4
->sin_addr
.s_addr
= hdr
->src_addr
.ip4
.addr
;
996 ip4
->sin_port
= hdr
->port
;
1000 static void cma_save_ip6_info(struct sockaddr
*src_addr
,
1001 struct sockaddr
*dst_addr
,
1002 struct cma_hdr
*hdr
,
1005 struct sockaddr_in6
*ip6
;
1008 ip6
= (struct sockaddr_in6
*)src_addr
;
1009 ip6
->sin6_family
= AF_INET6
;
1010 ip6
->sin6_addr
= hdr
->dst_addr
.ip6
;
1011 ip6
->sin6_port
= local_port
;
1015 ip6
= (struct sockaddr_in6
*)dst_addr
;
1016 ip6
->sin6_family
= AF_INET6
;
1017 ip6
->sin6_addr
= hdr
->src_addr
.ip6
;
1018 ip6
->sin6_port
= hdr
->port
;
1022 static u16
cma_port_from_service_id(__be64 service_id
)
1024 return (u16
)be64_to_cpu(service_id
);
1027 static int cma_save_ip_info(struct sockaddr
*src_addr
,
1028 struct sockaddr
*dst_addr
,
1029 struct ib_cm_event
*ib_event
,
1032 struct cma_hdr
*hdr
;
1035 hdr
= ib_event
->private_data
;
1036 if (hdr
->cma_version
!= CMA_VERSION
)
1039 port
= htons(cma_port_from_service_id(service_id
));
1041 switch (cma_get_ip_ver(hdr
)) {
1043 cma_save_ip4_info(src_addr
, dst_addr
, hdr
, port
);
1046 cma_save_ip6_info(src_addr
, dst_addr
, hdr
, port
);
1049 return -EAFNOSUPPORT
;
1055 static int cma_save_net_info(struct sockaddr
*src_addr
,
1056 struct sockaddr
*dst_addr
,
1057 struct rdma_cm_id
*listen_id
,
1058 struct ib_cm_event
*ib_event
,
1059 sa_family_t sa_family
, __be64 service_id
)
1061 if (sa_family
== AF_IB
) {
1062 if (ib_event
->event
== IB_CM_REQ_RECEIVED
)
1063 cma_save_ib_info(src_addr
, dst_addr
, listen_id
,
1064 ib_event
->param
.req_rcvd
.primary_path
);
1065 else if (ib_event
->event
== IB_CM_SIDR_REQ_RECEIVED
)
1066 cma_save_ib_info(src_addr
, dst_addr
, listen_id
, NULL
);
1070 return cma_save_ip_info(src_addr
, dst_addr
, ib_event
, service_id
);
1073 static int cma_save_req_info(const struct ib_cm_event
*ib_event
,
1074 struct cma_req_info
*req
)
1076 const struct ib_cm_req_event_param
*req_param
=
1077 &ib_event
->param
.req_rcvd
;
1078 const struct ib_cm_sidr_req_event_param
*sidr_param
=
1079 &ib_event
->param
.sidr_req_rcvd
;
1081 switch (ib_event
->event
) {
1082 case IB_CM_REQ_RECEIVED
:
1083 req
->device
= req_param
->listen_id
->device
;
1084 req
->port
= req_param
->port
;
1085 memcpy(&req
->local_gid
, &req_param
->primary_path
->sgid
,
1086 sizeof(req
->local_gid
));
1087 req
->has_gid
= true;
1088 req
->service_id
= req_param
->primary_path
->service_id
;
1089 req
->pkey
= be16_to_cpu(req_param
->primary_path
->pkey
);
1091 case IB_CM_SIDR_REQ_RECEIVED
:
1092 req
->device
= sidr_param
->listen_id
->device
;
1093 req
->port
= sidr_param
->port
;
1094 req
->has_gid
= false;
1095 req
->service_id
= sidr_param
->service_id
;
1096 req
->pkey
= sidr_param
->pkey
;
1105 static bool validate_ipv4_net_dev(struct net_device
*net_dev
,
1106 const struct sockaddr_in
*dst_addr
,
1107 const struct sockaddr_in
*src_addr
)
1109 __be32 daddr
= dst_addr
->sin_addr
.s_addr
,
1110 saddr
= src_addr
->sin_addr
.s_addr
;
1111 struct fib_result res
;
1116 if (ipv4_is_multicast(saddr
) || ipv4_is_lbcast(saddr
) ||
1117 ipv4_is_lbcast(daddr
) || ipv4_is_zeronet(saddr
) ||
1118 ipv4_is_zeronet(daddr
) || ipv4_is_loopback(daddr
) ||
1119 ipv4_is_loopback(saddr
))
1122 memset(&fl4
, 0, sizeof(fl4
));
1123 fl4
.flowi4_iif
= net_dev
->ifindex
;
1128 err
= fib_lookup(dev_net(net_dev
), &fl4
, &res
, 0);
1129 ret
= err
== 0 && FIB_RES_DEV(res
) == net_dev
;
1135 static bool validate_ipv6_net_dev(struct net_device
*net_dev
,
1136 const struct sockaddr_in6
*dst_addr
,
1137 const struct sockaddr_in6
*src_addr
)
1139 #if IS_ENABLED(CONFIG_IPV6)
1140 const int strict
= ipv6_addr_type(&dst_addr
->sin6_addr
) &
1141 IPV6_ADDR_LINKLOCAL
;
1142 struct rt6_info
*rt
= rt6_lookup(dev_net(net_dev
), &dst_addr
->sin6_addr
,
1143 &src_addr
->sin6_addr
, net_dev
->ifindex
,
1150 ret
= rt
->rt6i_idev
->dev
== net_dev
;
1159 static bool validate_net_dev(struct net_device
*net_dev
,
1160 const struct sockaddr
*daddr
,
1161 const struct sockaddr
*saddr
)
1163 const struct sockaddr_in
*daddr4
= (const struct sockaddr_in
*)daddr
;
1164 const struct sockaddr_in
*saddr4
= (const struct sockaddr_in
*)saddr
;
1165 const struct sockaddr_in6
*daddr6
= (const struct sockaddr_in6
*)daddr
;
1166 const struct sockaddr_in6
*saddr6
= (const struct sockaddr_in6
*)saddr
;
1168 switch (daddr
->sa_family
) {
1170 return saddr
->sa_family
== AF_INET
&&
1171 validate_ipv4_net_dev(net_dev
, daddr4
, saddr4
);
1174 return saddr
->sa_family
== AF_INET6
&&
1175 validate_ipv6_net_dev(net_dev
, daddr6
, saddr6
);
1182 static struct net_device
*cma_get_net_dev(struct ib_cm_event
*ib_event
,
1183 const struct cma_req_info
*req
)
1185 struct sockaddr_storage listen_addr_storage
, src_addr_storage
;
1186 struct sockaddr
*listen_addr
= (struct sockaddr
*)&listen_addr_storage
,
1187 *src_addr
= (struct sockaddr
*)&src_addr_storage
;
1188 struct net_device
*net_dev
;
1189 const union ib_gid
*gid
= req
->has_gid
? &req
->local_gid
: NULL
;
1192 err
= cma_save_ip_info(listen_addr
, src_addr
, ib_event
,
1195 return ERR_PTR(err
);
1197 net_dev
= ib_get_net_dev_by_params(req
->device
, req
->port
, req
->pkey
,
1200 return ERR_PTR(-ENODEV
);
1202 if (!validate_net_dev(net_dev
, listen_addr
, src_addr
)) {
1204 return ERR_PTR(-EHOSTUNREACH
);
1210 static enum rdma_port_space
rdma_ps_from_service_id(__be64 service_id
)
1212 return (be64_to_cpu(service_id
) >> 16) & 0xffff;
1215 static bool cma_match_private_data(struct rdma_id_private
*id_priv
,
1216 const struct cma_hdr
*hdr
)
1218 struct sockaddr
*addr
= cma_src_addr(id_priv
);
1220 struct in6_addr ip6_addr
;
1222 if (cma_any_addr(addr
) && !id_priv
->afonly
)
1225 switch (addr
->sa_family
) {
1227 ip4_addr
= ((struct sockaddr_in
*)addr
)->sin_addr
.s_addr
;
1228 if (cma_get_ip_ver(hdr
) != 4)
1230 if (!cma_any_addr(addr
) &&
1231 hdr
->dst_addr
.ip4
.addr
!= ip4_addr
)
1235 ip6_addr
= ((struct sockaddr_in6
*)addr
)->sin6_addr
;
1236 if (cma_get_ip_ver(hdr
) != 6)
1238 if (!cma_any_addr(addr
) &&
1239 memcmp(&hdr
->dst_addr
.ip6
, &ip6_addr
, sizeof(ip6_addr
)))
1251 static bool cma_protocol_roce_dev_port(struct ib_device
*device
, int port_num
)
1253 enum rdma_link_layer ll
= rdma_port_get_link_layer(device
, port_num
);
1254 enum rdma_transport_type transport
=
1255 rdma_node_get_transport(device
->node_type
);
1257 return ll
== IB_LINK_LAYER_ETHERNET
&& transport
== RDMA_TRANSPORT_IB
;
1260 static bool cma_protocol_roce(const struct rdma_cm_id
*id
)
1262 struct ib_device
*device
= id
->device
;
1263 const int port_num
= id
->port_num
?: rdma_start_port(device
);
1265 return cma_protocol_roce_dev_port(device
, port_num
);
1268 static bool cma_match_net_dev(const struct rdma_id_private
*id_priv
,
1269 const struct net_device
*net_dev
)
1271 const struct rdma_addr
*addr
= &id_priv
->id
.route
.addr
;
1274 /* This request is an AF_IB request or a RoCE request */
1275 return addr
->src_addr
.ss_family
== AF_IB
||
1276 cma_protocol_roce(&id_priv
->id
);
1278 return !addr
->dev_addr
.bound_dev_if
||
1279 (net_eq(dev_net(net_dev
), addr
->dev_addr
.net
) &&
1280 addr
->dev_addr
.bound_dev_if
== net_dev
->ifindex
);
1283 static struct rdma_id_private
*cma_find_listener(
1284 const struct rdma_bind_list
*bind_list
,
1285 const struct ib_cm_id
*cm_id
,
1286 const struct ib_cm_event
*ib_event
,
1287 const struct cma_req_info
*req
,
1288 const struct net_device
*net_dev
)
1290 struct rdma_id_private
*id_priv
, *id_priv_dev
;
1293 return ERR_PTR(-EINVAL
);
1295 hlist_for_each_entry(id_priv
, &bind_list
->owners
, node
) {
1296 if (cma_match_private_data(id_priv
, ib_event
->private_data
)) {
1297 if (id_priv
->id
.device
== cm_id
->device
&&
1298 cma_match_net_dev(id_priv
, net_dev
))
1300 list_for_each_entry(id_priv_dev
,
1301 &id_priv
->listen_list
,
1303 if (id_priv_dev
->id
.device
== cm_id
->device
&&
1304 cma_match_net_dev(id_priv_dev
, net_dev
))
1310 return ERR_PTR(-EINVAL
);
1313 static struct rdma_id_private
*cma_id_from_event(struct ib_cm_id
*cm_id
,
1314 struct ib_cm_event
*ib_event
,
1315 struct net_device
**net_dev
)
1317 struct cma_req_info req
;
1318 struct rdma_bind_list
*bind_list
;
1319 struct rdma_id_private
*id_priv
;
1322 err
= cma_save_req_info(ib_event
, &req
);
1324 return ERR_PTR(err
);
1326 *net_dev
= cma_get_net_dev(ib_event
, &req
);
1327 if (IS_ERR(*net_dev
)) {
1328 if (PTR_ERR(*net_dev
) == -EAFNOSUPPORT
) {
1329 /* Assuming the protocol is AF_IB */
1331 } else if (cma_protocol_roce_dev_port(req
.device
, req
.port
)) {
1332 /* TODO find the net dev matching the request parameters
1333 * through the RoCE GID table */
1336 return ERR_CAST(*net_dev
);
1340 bind_list
= cma_ps_find(*net_dev
? dev_net(*net_dev
) : &init_net
,
1341 rdma_ps_from_service_id(req
.service_id
),
1342 cma_port_from_service_id(req
.service_id
));
1343 id_priv
= cma_find_listener(bind_list
, cm_id
, ib_event
, &req
, *net_dev
);
1344 if (IS_ERR(id_priv
) && *net_dev
) {
1352 static inline int cma_user_data_offset(struct rdma_id_private
*id_priv
)
1354 return cma_family(id_priv
) == AF_IB
? 0 : sizeof(struct cma_hdr
);
1357 static void cma_cancel_route(struct rdma_id_private
*id_priv
)
1359 if (rdma_cap_ib_sa(id_priv
->id
.device
, id_priv
->id
.port_num
)) {
1361 ib_sa_cancel_query(id_priv
->query_id
, id_priv
->query
);
1365 static void cma_cancel_listens(struct rdma_id_private
*id_priv
)
1367 struct rdma_id_private
*dev_id_priv
;
1370 * Remove from listen_any_list to prevent added devices from spawning
1371 * additional listen requests.
1374 list_del(&id_priv
->list
);
1376 while (!list_empty(&id_priv
->listen_list
)) {
1377 dev_id_priv
= list_entry(id_priv
->listen_list
.next
,
1378 struct rdma_id_private
, listen_list
);
1379 /* sync with device removal to avoid duplicate destruction */
1380 list_del_init(&dev_id_priv
->list
);
1381 list_del(&dev_id_priv
->listen_list
);
1382 mutex_unlock(&lock
);
1384 rdma_destroy_id(&dev_id_priv
->id
);
1387 mutex_unlock(&lock
);
1390 static void cma_cancel_operation(struct rdma_id_private
*id_priv
,
1391 enum rdma_cm_state state
)
1394 case RDMA_CM_ADDR_QUERY
:
1395 rdma_addr_cancel(&id_priv
->id
.route
.addr
.dev_addr
);
1397 case RDMA_CM_ROUTE_QUERY
:
1398 cma_cancel_route(id_priv
);
1400 case RDMA_CM_LISTEN
:
1401 if (cma_any_addr(cma_src_addr(id_priv
)) && !id_priv
->cma_dev
)
1402 cma_cancel_listens(id_priv
);
1409 static void cma_release_port(struct rdma_id_private
*id_priv
)
1411 struct rdma_bind_list
*bind_list
= id_priv
->bind_list
;
1412 struct net
*net
= id_priv
->id
.route
.addr
.dev_addr
.net
;
1418 hlist_del(&id_priv
->node
);
1419 if (hlist_empty(&bind_list
->owners
)) {
1420 cma_ps_remove(net
, bind_list
->ps
, bind_list
->port
);
1423 mutex_unlock(&lock
);
1426 static void cma_leave_mc_groups(struct rdma_id_private
*id_priv
)
1428 struct cma_multicast
*mc
;
1430 while (!list_empty(&id_priv
->mc_list
)) {
1431 mc
= container_of(id_priv
->mc_list
.next
,
1432 struct cma_multicast
, list
);
1433 list_del(&mc
->list
);
1434 if (rdma_cap_ib_mcast(id_priv
->cma_dev
->device
,
1435 id_priv
->id
.port_num
)) {
1436 ib_sa_free_multicast(mc
->multicast
.ib
);
1439 kref_put(&mc
->mcref
, release_mc
);
1443 void rdma_destroy_id(struct rdma_cm_id
*id
)
1445 struct rdma_id_private
*id_priv
;
1446 enum rdma_cm_state state
;
1448 id_priv
= container_of(id
, struct rdma_id_private
, id
);
1449 state
= cma_exch(id_priv
, RDMA_CM_DESTROYING
);
1450 cma_cancel_operation(id_priv
, state
);
1453 * Wait for any active callback to finish. New callbacks will find
1454 * the id_priv state set to destroying and abort.
1456 mutex_lock(&id_priv
->handler_mutex
);
1457 mutex_unlock(&id_priv
->handler_mutex
);
1459 if (id_priv
->cma_dev
) {
1460 if (rdma_cap_ib_cm(id_priv
->id
.device
, 1)) {
1461 if (id_priv
->cm_id
.ib
)
1462 ib_destroy_cm_id(id_priv
->cm_id
.ib
);
1463 } else if (rdma_cap_iw_cm(id_priv
->id
.device
, 1)) {
1464 if (id_priv
->cm_id
.iw
)
1465 iw_destroy_cm_id(id_priv
->cm_id
.iw
);
1467 cma_leave_mc_groups(id_priv
);
1468 cma_release_dev(id_priv
);
1471 cma_release_port(id_priv
);
1472 cma_deref_id(id_priv
);
1473 wait_for_completion(&id_priv
->comp
);
1475 if (id_priv
->internal_id
)
1476 cma_deref_id(id_priv
->id
.context
);
1478 kfree(id_priv
->id
.route
.path_rec
);
1479 put_net(id_priv
->id
.route
.addr
.dev_addr
.net
);
1482 EXPORT_SYMBOL(rdma_destroy_id
);
1484 static int cma_rep_recv(struct rdma_id_private
*id_priv
)
1488 ret
= cma_modify_qp_rtr(id_priv
, NULL
);
1492 ret
= cma_modify_qp_rts(id_priv
, NULL
);
1496 ret
= ib_send_cm_rtu(id_priv
->cm_id
.ib
, NULL
, 0);
1502 cma_modify_qp_err(id_priv
);
1503 ib_send_cm_rej(id_priv
->cm_id
.ib
, IB_CM_REJ_CONSUMER_DEFINED
,
1508 static void cma_set_rep_event_data(struct rdma_cm_event
*event
,
1509 struct ib_cm_rep_event_param
*rep_data
,
1512 event
->param
.conn
.private_data
= private_data
;
1513 event
->param
.conn
.private_data_len
= IB_CM_REP_PRIVATE_DATA_SIZE
;
1514 event
->param
.conn
.responder_resources
= rep_data
->responder_resources
;
1515 event
->param
.conn
.initiator_depth
= rep_data
->initiator_depth
;
1516 event
->param
.conn
.flow_control
= rep_data
->flow_control
;
1517 event
->param
.conn
.rnr_retry_count
= rep_data
->rnr_retry_count
;
1518 event
->param
.conn
.srq
= rep_data
->srq
;
1519 event
->param
.conn
.qp_num
= rep_data
->remote_qpn
;
1522 static int cma_ib_handler(struct ib_cm_id
*cm_id
, struct ib_cm_event
*ib_event
)
1524 struct rdma_id_private
*id_priv
= cm_id
->context
;
1525 struct rdma_cm_event event
;
1528 if ((ib_event
->event
!= IB_CM_TIMEWAIT_EXIT
&&
1529 cma_disable_callback(id_priv
, RDMA_CM_CONNECT
)) ||
1530 (ib_event
->event
== IB_CM_TIMEWAIT_EXIT
&&
1531 cma_disable_callback(id_priv
, RDMA_CM_DISCONNECT
)))
1534 memset(&event
, 0, sizeof event
);
1535 switch (ib_event
->event
) {
1536 case IB_CM_REQ_ERROR
:
1537 case IB_CM_REP_ERROR
:
1538 event
.event
= RDMA_CM_EVENT_UNREACHABLE
;
1539 event
.status
= -ETIMEDOUT
;
1541 case IB_CM_REP_RECEIVED
:
1542 if (id_priv
->id
.qp
) {
1543 event
.status
= cma_rep_recv(id_priv
);
1544 event
.event
= event
.status
? RDMA_CM_EVENT_CONNECT_ERROR
:
1545 RDMA_CM_EVENT_ESTABLISHED
;
1547 event
.event
= RDMA_CM_EVENT_CONNECT_RESPONSE
;
1549 cma_set_rep_event_data(&event
, &ib_event
->param
.rep_rcvd
,
1550 ib_event
->private_data
);
1552 case IB_CM_RTU_RECEIVED
:
1553 case IB_CM_USER_ESTABLISHED
:
1554 event
.event
= RDMA_CM_EVENT_ESTABLISHED
;
1556 case IB_CM_DREQ_ERROR
:
1557 event
.status
= -ETIMEDOUT
; /* fall through */
1558 case IB_CM_DREQ_RECEIVED
:
1559 case IB_CM_DREP_RECEIVED
:
1560 if (!cma_comp_exch(id_priv
, RDMA_CM_CONNECT
,
1561 RDMA_CM_DISCONNECT
))
1563 event
.event
= RDMA_CM_EVENT_DISCONNECTED
;
1565 case IB_CM_TIMEWAIT_EXIT
:
1566 event
.event
= RDMA_CM_EVENT_TIMEWAIT_EXIT
;
1568 case IB_CM_MRA_RECEIVED
:
1571 case IB_CM_REJ_RECEIVED
:
1572 cma_modify_qp_err(id_priv
);
1573 event
.status
= ib_event
->param
.rej_rcvd
.reason
;
1574 event
.event
= RDMA_CM_EVENT_REJECTED
;
1575 event
.param
.conn
.private_data
= ib_event
->private_data
;
1576 event
.param
.conn
.private_data_len
= IB_CM_REJ_PRIVATE_DATA_SIZE
;
1579 printk(KERN_ERR
"RDMA CMA: unexpected IB CM event: %d\n",
1584 ret
= id_priv
->id
.event_handler(&id_priv
->id
, &event
);
1586 /* Destroy the CM ID by returning a non-zero value. */
1587 id_priv
->cm_id
.ib
= NULL
;
1588 cma_exch(id_priv
, RDMA_CM_DESTROYING
);
1589 mutex_unlock(&id_priv
->handler_mutex
);
1590 rdma_destroy_id(&id_priv
->id
);
1594 mutex_unlock(&id_priv
->handler_mutex
);
1598 static struct rdma_id_private
*cma_new_conn_id(struct rdma_cm_id
*listen_id
,
1599 struct ib_cm_event
*ib_event
,
1600 struct net_device
*net_dev
)
1602 struct rdma_id_private
*id_priv
;
1603 struct rdma_cm_id
*id
;
1604 struct rdma_route
*rt
;
1605 const sa_family_t ss_family
= listen_id
->route
.addr
.src_addr
.ss_family
;
1606 const __be64 service_id
=
1607 ib_event
->param
.req_rcvd
.primary_path
->service_id
;
1610 id
= rdma_create_id(listen_id
->route
.addr
.dev_addr
.net
,
1611 listen_id
->event_handler
, listen_id
->context
,
1612 listen_id
->ps
, ib_event
->param
.req_rcvd
.qp_type
);
1616 id_priv
= container_of(id
, struct rdma_id_private
, id
);
1617 if (cma_save_net_info((struct sockaddr
*)&id
->route
.addr
.src_addr
,
1618 (struct sockaddr
*)&id
->route
.addr
.dst_addr
,
1619 listen_id
, ib_event
, ss_family
, service_id
))
1623 rt
->num_paths
= ib_event
->param
.req_rcvd
.alternate_path
? 2 : 1;
1624 rt
->path_rec
= kmalloc(sizeof *rt
->path_rec
* rt
->num_paths
,
1629 rt
->path_rec
[0] = *ib_event
->param
.req_rcvd
.primary_path
;
1630 if (rt
->num_paths
== 2)
1631 rt
->path_rec
[1] = *ib_event
->param
.req_rcvd
.alternate_path
;
1634 ret
= rdma_copy_addr(&rt
->addr
.dev_addr
, net_dev
, NULL
);
1638 if (!cma_protocol_roce(listen_id
) &&
1639 cma_any_addr(cma_src_addr(id_priv
))) {
1640 rt
->addr
.dev_addr
.dev_type
= ARPHRD_INFINIBAND
;
1641 rdma_addr_set_sgid(&rt
->addr
.dev_addr
, &rt
->path_rec
[0].sgid
);
1642 ib_addr_set_pkey(&rt
->addr
.dev_addr
, be16_to_cpu(rt
->path_rec
[0].pkey
));
1643 } else if (!cma_any_addr(cma_src_addr(id_priv
))) {
1644 ret
= cma_translate_addr(cma_src_addr(id_priv
), &rt
->addr
.dev_addr
);
1649 rdma_addr_set_dgid(&rt
->addr
.dev_addr
, &rt
->path_rec
[0].dgid
);
1651 id_priv
->state
= RDMA_CM_CONNECT
;
1655 rdma_destroy_id(id
);
1659 static struct rdma_id_private
*cma_new_udp_id(struct rdma_cm_id
*listen_id
,
1660 struct ib_cm_event
*ib_event
,
1661 struct net_device
*net_dev
)
1663 struct rdma_id_private
*id_priv
;
1664 struct rdma_cm_id
*id
;
1665 const sa_family_t ss_family
= listen_id
->route
.addr
.src_addr
.ss_family
;
1666 struct net
*net
= listen_id
->route
.addr
.dev_addr
.net
;
1669 id
= rdma_create_id(net
, listen_id
->event_handler
, listen_id
->context
,
1670 listen_id
->ps
, IB_QPT_UD
);
1674 id_priv
= container_of(id
, struct rdma_id_private
, id
);
1675 if (cma_save_net_info((struct sockaddr
*)&id
->route
.addr
.src_addr
,
1676 (struct sockaddr
*)&id
->route
.addr
.dst_addr
,
1677 listen_id
, ib_event
, ss_family
,
1678 ib_event
->param
.sidr_req_rcvd
.service_id
))
1682 ret
= rdma_copy_addr(&id
->route
.addr
.dev_addr
, net_dev
, NULL
);
1686 if (!cma_any_addr(cma_src_addr(id_priv
))) {
1687 ret
= cma_translate_addr(cma_src_addr(id_priv
),
1688 &id
->route
.addr
.dev_addr
);
1694 id_priv
->state
= RDMA_CM_CONNECT
;
1697 rdma_destroy_id(id
);
1701 static void cma_set_req_event_data(struct rdma_cm_event
*event
,
1702 struct ib_cm_req_event_param
*req_data
,
1703 void *private_data
, int offset
)
1705 event
->param
.conn
.private_data
= private_data
+ offset
;
1706 event
->param
.conn
.private_data_len
= IB_CM_REQ_PRIVATE_DATA_SIZE
- offset
;
1707 event
->param
.conn
.responder_resources
= req_data
->responder_resources
;
1708 event
->param
.conn
.initiator_depth
= req_data
->initiator_depth
;
1709 event
->param
.conn
.flow_control
= req_data
->flow_control
;
1710 event
->param
.conn
.retry_count
= req_data
->retry_count
;
1711 event
->param
.conn
.rnr_retry_count
= req_data
->rnr_retry_count
;
1712 event
->param
.conn
.srq
= req_data
->srq
;
1713 event
->param
.conn
.qp_num
= req_data
->remote_qpn
;
1716 static int cma_check_req_qp_type(struct rdma_cm_id
*id
, struct ib_cm_event
*ib_event
)
1718 return (((ib_event
->event
== IB_CM_REQ_RECEIVED
) &&
1719 (ib_event
->param
.req_rcvd
.qp_type
== id
->qp_type
)) ||
1720 ((ib_event
->event
== IB_CM_SIDR_REQ_RECEIVED
) &&
1721 (id
->qp_type
== IB_QPT_UD
)) ||
1725 static int cma_req_handler(struct ib_cm_id
*cm_id
, struct ib_cm_event
*ib_event
)
1727 struct rdma_id_private
*listen_id
, *conn_id
;
1728 struct rdma_cm_event event
;
1729 struct net_device
*net_dev
;
1732 listen_id
= cma_id_from_event(cm_id
, ib_event
, &net_dev
);
1733 if (IS_ERR(listen_id
))
1734 return PTR_ERR(listen_id
);
1736 if (!cma_check_req_qp_type(&listen_id
->id
, ib_event
)) {
1741 if (cma_disable_callback(listen_id
, RDMA_CM_LISTEN
)) {
1742 ret
= -ECONNABORTED
;
1746 memset(&event
, 0, sizeof event
);
1747 offset
= cma_user_data_offset(listen_id
);
1748 event
.event
= RDMA_CM_EVENT_CONNECT_REQUEST
;
1749 if (ib_event
->event
== IB_CM_SIDR_REQ_RECEIVED
) {
1750 conn_id
= cma_new_udp_id(&listen_id
->id
, ib_event
, net_dev
);
1751 event
.param
.ud
.private_data
= ib_event
->private_data
+ offset
;
1752 event
.param
.ud
.private_data_len
=
1753 IB_CM_SIDR_REQ_PRIVATE_DATA_SIZE
- offset
;
1755 conn_id
= cma_new_conn_id(&listen_id
->id
, ib_event
, net_dev
);
1756 cma_set_req_event_data(&event
, &ib_event
->param
.req_rcvd
,
1757 ib_event
->private_data
, offset
);
1764 mutex_lock_nested(&conn_id
->handler_mutex
, SINGLE_DEPTH_NESTING
);
1765 ret
= cma_acquire_dev(conn_id
, listen_id
);
1769 conn_id
->cm_id
.ib
= cm_id
;
1770 cm_id
->context
= conn_id
;
1771 cm_id
->cm_handler
= cma_ib_handler
;
1774 * Protect against the user destroying conn_id from another thread
1775 * until we're done accessing it.
1777 atomic_inc(&conn_id
->refcount
);
1778 ret
= conn_id
->id
.event_handler(&conn_id
->id
, &event
);
1782 * Acquire mutex to prevent user executing rdma_destroy_id()
1783 * while we're accessing the cm_id.
1786 if (cma_comp(conn_id
, RDMA_CM_CONNECT
) &&
1787 (conn_id
->id
.qp_type
!= IB_QPT_UD
))
1788 ib_send_cm_mra(cm_id
, CMA_CM_MRA_SETTING
, NULL
, 0);
1789 mutex_unlock(&lock
);
1790 mutex_unlock(&conn_id
->handler_mutex
);
1791 mutex_unlock(&listen_id
->handler_mutex
);
1792 cma_deref_id(conn_id
);
1798 cma_deref_id(conn_id
);
1799 /* Destroy the CM ID by returning a non-zero value. */
1800 conn_id
->cm_id
.ib
= NULL
;
1802 cma_exch(conn_id
, RDMA_CM_DESTROYING
);
1803 mutex_unlock(&conn_id
->handler_mutex
);
1805 mutex_unlock(&listen_id
->handler_mutex
);
1807 rdma_destroy_id(&conn_id
->id
);
1816 __be64
rdma_get_service_id(struct rdma_cm_id
*id
, struct sockaddr
*addr
)
1818 if (addr
->sa_family
== AF_IB
)
1819 return ((struct sockaddr_ib
*) addr
)->sib_sid
;
1821 return cpu_to_be64(((u64
)id
->ps
<< 16) + be16_to_cpu(cma_port(addr
)));
1823 EXPORT_SYMBOL(rdma_get_service_id
);
1825 static int cma_iw_handler(struct iw_cm_id
*iw_id
, struct iw_cm_event
*iw_event
)
1827 struct rdma_id_private
*id_priv
= iw_id
->context
;
1828 struct rdma_cm_event event
;
1830 struct sockaddr
*laddr
= (struct sockaddr
*)&iw_event
->local_addr
;
1831 struct sockaddr
*raddr
= (struct sockaddr
*)&iw_event
->remote_addr
;
1833 if (cma_disable_callback(id_priv
, RDMA_CM_CONNECT
))
1836 memset(&event
, 0, sizeof event
);
1837 switch (iw_event
->event
) {
1838 case IW_CM_EVENT_CLOSE
:
1839 event
.event
= RDMA_CM_EVENT_DISCONNECTED
;
1841 case IW_CM_EVENT_CONNECT_REPLY
:
1842 memcpy(cma_src_addr(id_priv
), laddr
,
1843 rdma_addr_size(laddr
));
1844 memcpy(cma_dst_addr(id_priv
), raddr
,
1845 rdma_addr_size(raddr
));
1846 switch (iw_event
->status
) {
1848 event
.event
= RDMA_CM_EVENT_ESTABLISHED
;
1849 event
.param
.conn
.initiator_depth
= iw_event
->ird
;
1850 event
.param
.conn
.responder_resources
= iw_event
->ord
;
1854 event
.event
= RDMA_CM_EVENT_REJECTED
;
1857 event
.event
= RDMA_CM_EVENT_UNREACHABLE
;
1860 event
.event
= RDMA_CM_EVENT_CONNECT_ERROR
;
1864 case IW_CM_EVENT_ESTABLISHED
:
1865 event
.event
= RDMA_CM_EVENT_ESTABLISHED
;
1866 event
.param
.conn
.initiator_depth
= iw_event
->ird
;
1867 event
.param
.conn
.responder_resources
= iw_event
->ord
;
1873 event
.status
= iw_event
->status
;
1874 event
.param
.conn
.private_data
= iw_event
->private_data
;
1875 event
.param
.conn
.private_data_len
= iw_event
->private_data_len
;
1876 ret
= id_priv
->id
.event_handler(&id_priv
->id
, &event
);
1878 /* Destroy the CM ID by returning a non-zero value. */
1879 id_priv
->cm_id
.iw
= NULL
;
1880 cma_exch(id_priv
, RDMA_CM_DESTROYING
);
1881 mutex_unlock(&id_priv
->handler_mutex
);
1882 rdma_destroy_id(&id_priv
->id
);
1886 mutex_unlock(&id_priv
->handler_mutex
);
1890 static int iw_conn_req_handler(struct iw_cm_id
*cm_id
,
1891 struct iw_cm_event
*iw_event
)
1893 struct rdma_cm_id
*new_cm_id
;
1894 struct rdma_id_private
*listen_id
, *conn_id
;
1895 struct rdma_cm_event event
;
1897 struct ib_device_attr attr
;
1898 struct sockaddr
*laddr
= (struct sockaddr
*)&iw_event
->local_addr
;
1899 struct sockaddr
*raddr
= (struct sockaddr
*)&iw_event
->remote_addr
;
1901 listen_id
= cm_id
->context
;
1902 if (cma_disable_callback(listen_id
, RDMA_CM_LISTEN
))
1903 return -ECONNABORTED
;
1905 /* Create a new RDMA id for the new IW CM ID */
1906 new_cm_id
= rdma_create_id(listen_id
->id
.route
.addr
.dev_addr
.net
,
1907 listen_id
->id
.event_handler
,
1908 listen_id
->id
.context
,
1909 RDMA_PS_TCP
, IB_QPT_RC
);
1910 if (IS_ERR(new_cm_id
)) {
1914 conn_id
= container_of(new_cm_id
, struct rdma_id_private
, id
);
1915 mutex_lock_nested(&conn_id
->handler_mutex
, SINGLE_DEPTH_NESTING
);
1916 conn_id
->state
= RDMA_CM_CONNECT
;
1918 ret
= rdma_translate_ip(laddr
, &conn_id
->id
.route
.addr
.dev_addr
, NULL
);
1920 mutex_unlock(&conn_id
->handler_mutex
);
1921 rdma_destroy_id(new_cm_id
);
1925 ret
= cma_acquire_dev(conn_id
, listen_id
);
1927 mutex_unlock(&conn_id
->handler_mutex
);
1928 rdma_destroy_id(new_cm_id
);
1932 conn_id
->cm_id
.iw
= cm_id
;
1933 cm_id
->context
= conn_id
;
1934 cm_id
->cm_handler
= cma_iw_handler
;
1936 memcpy(cma_src_addr(conn_id
), laddr
, rdma_addr_size(laddr
));
1937 memcpy(cma_dst_addr(conn_id
), raddr
, rdma_addr_size(raddr
));
1939 ret
= ib_query_device(conn_id
->id
.device
, &attr
);
1941 mutex_unlock(&conn_id
->handler_mutex
);
1942 rdma_destroy_id(new_cm_id
);
1946 memset(&event
, 0, sizeof event
);
1947 event
.event
= RDMA_CM_EVENT_CONNECT_REQUEST
;
1948 event
.param
.conn
.private_data
= iw_event
->private_data
;
1949 event
.param
.conn
.private_data_len
= iw_event
->private_data_len
;
1950 event
.param
.conn
.initiator_depth
= iw_event
->ird
;
1951 event
.param
.conn
.responder_resources
= iw_event
->ord
;
1954 * Protect against the user destroying conn_id from another thread
1955 * until we're done accessing it.
1957 atomic_inc(&conn_id
->refcount
);
1958 ret
= conn_id
->id
.event_handler(&conn_id
->id
, &event
);
1960 /* User wants to destroy the CM ID */
1961 conn_id
->cm_id
.iw
= NULL
;
1962 cma_exch(conn_id
, RDMA_CM_DESTROYING
);
1963 mutex_unlock(&conn_id
->handler_mutex
);
1964 cma_deref_id(conn_id
);
1965 rdma_destroy_id(&conn_id
->id
);
1969 mutex_unlock(&conn_id
->handler_mutex
);
1970 cma_deref_id(conn_id
);
1973 mutex_unlock(&listen_id
->handler_mutex
);
1977 static int cma_ib_listen(struct rdma_id_private
*id_priv
)
1979 struct sockaddr
*addr
;
1980 struct ib_cm_id
*id
;
1983 addr
= cma_src_addr(id_priv
);
1984 svc_id
= rdma_get_service_id(&id_priv
->id
, addr
);
1985 id
= ib_cm_insert_listen(id_priv
->id
.device
, cma_req_handler
, svc_id
);
1988 id_priv
->cm_id
.ib
= id
;
1993 static int cma_iw_listen(struct rdma_id_private
*id_priv
, int backlog
)
1996 struct iw_cm_id
*id
;
1998 id
= iw_create_cm_id(id_priv
->id
.device
,
1999 iw_conn_req_handler
,
2004 id
->tos
= id_priv
->tos
;
2005 id_priv
->cm_id
.iw
= id
;
2007 memcpy(&id_priv
->cm_id
.iw
->local_addr
, cma_src_addr(id_priv
),
2008 rdma_addr_size(cma_src_addr(id_priv
)));
2010 ret
= iw_cm_listen(id_priv
->cm_id
.iw
, backlog
);
2013 iw_destroy_cm_id(id_priv
->cm_id
.iw
);
2014 id_priv
->cm_id
.iw
= NULL
;
2020 static int cma_listen_handler(struct rdma_cm_id
*id
,
2021 struct rdma_cm_event
*event
)
2023 struct rdma_id_private
*id_priv
= id
->context
;
2025 id
->context
= id_priv
->id
.context
;
2026 id
->event_handler
= id_priv
->id
.event_handler
;
2027 return id_priv
->id
.event_handler(id
, event
);
2030 static void cma_listen_on_dev(struct rdma_id_private
*id_priv
,
2031 struct cma_device
*cma_dev
)
2033 struct rdma_id_private
*dev_id_priv
;
2034 struct rdma_cm_id
*id
;
2035 struct net
*net
= id_priv
->id
.route
.addr
.dev_addr
.net
;
2038 if (cma_family(id_priv
) == AF_IB
&& !rdma_cap_ib_cm(cma_dev
->device
, 1))
2041 id
= rdma_create_id(net
, cma_listen_handler
, id_priv
, id_priv
->id
.ps
,
2042 id_priv
->id
.qp_type
);
2046 dev_id_priv
= container_of(id
, struct rdma_id_private
, id
);
2048 dev_id_priv
->state
= RDMA_CM_ADDR_BOUND
;
2049 memcpy(cma_src_addr(dev_id_priv
), cma_src_addr(id_priv
),
2050 rdma_addr_size(cma_src_addr(id_priv
)));
2052 cma_attach_to_dev(dev_id_priv
, cma_dev
);
2053 list_add_tail(&dev_id_priv
->listen_list
, &id_priv
->listen_list
);
2054 atomic_inc(&id_priv
->refcount
);
2055 dev_id_priv
->internal_id
= 1;
2056 dev_id_priv
->afonly
= id_priv
->afonly
;
2058 ret
= rdma_listen(id
, id_priv
->backlog
);
2060 printk(KERN_WARNING
"RDMA CMA: cma_listen_on_dev, error %d, "
2061 "listening on device %s\n", ret
, cma_dev
->device
->name
);
2064 static void cma_listen_on_all(struct rdma_id_private
*id_priv
)
2066 struct cma_device
*cma_dev
;
2069 list_add_tail(&id_priv
->list
, &listen_any_list
);
2070 list_for_each_entry(cma_dev
, &dev_list
, list
)
2071 cma_listen_on_dev(id_priv
, cma_dev
);
2072 mutex_unlock(&lock
);
2075 void rdma_set_service_type(struct rdma_cm_id
*id
, int tos
)
2077 struct rdma_id_private
*id_priv
;
2079 id_priv
= container_of(id
, struct rdma_id_private
, id
);
2080 id_priv
->tos
= (u8
) tos
;
2082 EXPORT_SYMBOL(rdma_set_service_type
);
2084 static void cma_query_handler(int status
, struct ib_sa_path_rec
*path_rec
,
2087 struct cma_work
*work
= context
;
2088 struct rdma_route
*route
;
2090 route
= &work
->id
->id
.route
;
2093 route
->num_paths
= 1;
2094 *route
->path_rec
= *path_rec
;
2096 work
->old_state
= RDMA_CM_ROUTE_QUERY
;
2097 work
->new_state
= RDMA_CM_ADDR_RESOLVED
;
2098 work
->event
.event
= RDMA_CM_EVENT_ROUTE_ERROR
;
2099 work
->event
.status
= status
;
2102 queue_work(cma_wq
, &work
->work
);
2105 static int cma_query_ib_route(struct rdma_id_private
*id_priv
, int timeout_ms
,
2106 struct cma_work
*work
)
2108 struct rdma_dev_addr
*dev_addr
= &id_priv
->id
.route
.addr
.dev_addr
;
2109 struct ib_sa_path_rec path_rec
;
2110 ib_sa_comp_mask comp_mask
;
2111 struct sockaddr_in6
*sin6
;
2112 struct sockaddr_ib
*sib
;
2114 memset(&path_rec
, 0, sizeof path_rec
);
2115 rdma_addr_get_sgid(dev_addr
, &path_rec
.sgid
);
2116 rdma_addr_get_dgid(dev_addr
, &path_rec
.dgid
);
2117 path_rec
.pkey
= cpu_to_be16(ib_addr_get_pkey(dev_addr
));
2118 path_rec
.numb_path
= 1;
2119 path_rec
.reversible
= 1;
2120 path_rec
.service_id
= rdma_get_service_id(&id_priv
->id
, cma_dst_addr(id_priv
));
2122 comp_mask
= IB_SA_PATH_REC_DGID
| IB_SA_PATH_REC_SGID
|
2123 IB_SA_PATH_REC_PKEY
| IB_SA_PATH_REC_NUMB_PATH
|
2124 IB_SA_PATH_REC_REVERSIBLE
| IB_SA_PATH_REC_SERVICE_ID
;
2126 switch (cma_family(id_priv
)) {
2128 path_rec
.qos_class
= cpu_to_be16((u16
) id_priv
->tos
);
2129 comp_mask
|= IB_SA_PATH_REC_QOS_CLASS
;
2132 sin6
= (struct sockaddr_in6
*) cma_src_addr(id_priv
);
2133 path_rec
.traffic_class
= (u8
) (be32_to_cpu(sin6
->sin6_flowinfo
) >> 20);
2134 comp_mask
|= IB_SA_PATH_REC_TRAFFIC_CLASS
;
2137 sib
= (struct sockaddr_ib
*) cma_src_addr(id_priv
);
2138 path_rec
.traffic_class
= (u8
) (be32_to_cpu(sib
->sib_flowinfo
) >> 20);
2139 comp_mask
|= IB_SA_PATH_REC_TRAFFIC_CLASS
;
2143 id_priv
->query_id
= ib_sa_path_rec_get(&sa_client
, id_priv
->id
.device
,
2144 id_priv
->id
.port_num
, &path_rec
,
2145 comp_mask
, timeout_ms
,
2146 GFP_KERNEL
, cma_query_handler
,
2147 work
, &id_priv
->query
);
2149 return (id_priv
->query_id
< 0) ? id_priv
->query_id
: 0;
2152 static void cma_work_handler(struct work_struct
*_work
)
2154 struct cma_work
*work
= container_of(_work
, struct cma_work
, work
);
2155 struct rdma_id_private
*id_priv
= work
->id
;
2158 mutex_lock(&id_priv
->handler_mutex
);
2159 if (!cma_comp_exch(id_priv
, work
->old_state
, work
->new_state
))
2162 if (id_priv
->id
.event_handler(&id_priv
->id
, &work
->event
)) {
2163 cma_exch(id_priv
, RDMA_CM_DESTROYING
);
2167 mutex_unlock(&id_priv
->handler_mutex
);
2168 cma_deref_id(id_priv
);
2170 rdma_destroy_id(&id_priv
->id
);
2174 static void cma_ndev_work_handler(struct work_struct
*_work
)
2176 struct cma_ndev_work
*work
= container_of(_work
, struct cma_ndev_work
, work
);
2177 struct rdma_id_private
*id_priv
= work
->id
;
2180 mutex_lock(&id_priv
->handler_mutex
);
2181 if (id_priv
->state
== RDMA_CM_DESTROYING
||
2182 id_priv
->state
== RDMA_CM_DEVICE_REMOVAL
)
2185 if (id_priv
->id
.event_handler(&id_priv
->id
, &work
->event
)) {
2186 cma_exch(id_priv
, RDMA_CM_DESTROYING
);
2191 mutex_unlock(&id_priv
->handler_mutex
);
2192 cma_deref_id(id_priv
);
2194 rdma_destroy_id(&id_priv
->id
);
2198 static int cma_resolve_ib_route(struct rdma_id_private
*id_priv
, int timeout_ms
)
2200 struct rdma_route
*route
= &id_priv
->id
.route
;
2201 struct cma_work
*work
;
2204 work
= kzalloc(sizeof *work
, GFP_KERNEL
);
2209 INIT_WORK(&work
->work
, cma_work_handler
);
2210 work
->old_state
= RDMA_CM_ROUTE_QUERY
;
2211 work
->new_state
= RDMA_CM_ROUTE_RESOLVED
;
2212 work
->event
.event
= RDMA_CM_EVENT_ROUTE_RESOLVED
;
2214 route
->path_rec
= kmalloc(sizeof *route
->path_rec
, GFP_KERNEL
);
2215 if (!route
->path_rec
) {
2220 ret
= cma_query_ib_route(id_priv
, timeout_ms
, work
);
2226 kfree(route
->path_rec
);
2227 route
->path_rec
= NULL
;
2233 int rdma_set_ib_paths(struct rdma_cm_id
*id
,
2234 struct ib_sa_path_rec
*path_rec
, int num_paths
)
2236 struct rdma_id_private
*id_priv
;
2239 id_priv
= container_of(id
, struct rdma_id_private
, id
);
2240 if (!cma_comp_exch(id_priv
, RDMA_CM_ADDR_RESOLVED
,
2241 RDMA_CM_ROUTE_RESOLVED
))
2244 id
->route
.path_rec
= kmemdup(path_rec
, sizeof *path_rec
* num_paths
,
2246 if (!id
->route
.path_rec
) {
2251 id
->route
.num_paths
= num_paths
;
2254 cma_comp_exch(id_priv
, RDMA_CM_ROUTE_RESOLVED
, RDMA_CM_ADDR_RESOLVED
);
2257 EXPORT_SYMBOL(rdma_set_ib_paths
);
2259 static int cma_resolve_iw_route(struct rdma_id_private
*id_priv
, int timeout_ms
)
2261 struct cma_work
*work
;
2263 work
= kzalloc(sizeof *work
, GFP_KERNEL
);
2268 INIT_WORK(&work
->work
, cma_work_handler
);
2269 work
->old_state
= RDMA_CM_ROUTE_QUERY
;
2270 work
->new_state
= RDMA_CM_ROUTE_RESOLVED
;
2271 work
->event
.event
= RDMA_CM_EVENT_ROUTE_RESOLVED
;
2272 queue_work(cma_wq
, &work
->work
);
2276 static int iboe_tos_to_sl(struct net_device
*ndev
, int tos
)
2279 struct net_device
*dev
;
2281 prio
= rt_tos2priority(tos
);
2282 dev
= ndev
->priv_flags
& IFF_802_1Q_VLAN
?
2283 vlan_dev_real_dev(ndev
) : ndev
;
2286 return netdev_get_prio_tc_map(dev
, prio
);
2288 #if IS_ENABLED(CONFIG_VLAN_8021Q)
2289 if (ndev
->priv_flags
& IFF_802_1Q_VLAN
)
2290 return (vlan_dev_get_egress_qos_mask(ndev
, prio
) &
2291 VLAN_PRIO_MASK
) >> VLAN_PRIO_SHIFT
;
2296 static int cma_resolve_iboe_route(struct rdma_id_private
*id_priv
)
2298 struct rdma_route
*route
= &id_priv
->id
.route
;
2299 struct rdma_addr
*addr
= &route
->addr
;
2300 struct cma_work
*work
;
2302 struct net_device
*ndev
= NULL
;
2305 work
= kzalloc(sizeof *work
, GFP_KERNEL
);
2310 INIT_WORK(&work
->work
, cma_work_handler
);
2312 route
->path_rec
= kzalloc(sizeof *route
->path_rec
, GFP_KERNEL
);
2313 if (!route
->path_rec
) {
2318 route
->num_paths
= 1;
2320 if (addr
->dev_addr
.bound_dev_if
) {
2321 ndev
= dev_get_by_index(&init_net
, addr
->dev_addr
.bound_dev_if
);
2322 route
->path_rec
->net
= &init_net
;
2323 route
->path_rec
->ifindex
= addr
->dev_addr
.bound_dev_if
;
2330 memcpy(route
->path_rec
->dmac
, addr
->dev_addr
.dst_dev_addr
, ETH_ALEN
);
2332 rdma_ip2gid((struct sockaddr
*)&id_priv
->id
.route
.addr
.src_addr
,
2333 &route
->path_rec
->sgid
);
2334 rdma_ip2gid((struct sockaddr
*)&id_priv
->id
.route
.addr
.dst_addr
,
2335 &route
->path_rec
->dgid
);
2337 route
->path_rec
->hop_limit
= 1;
2338 route
->path_rec
->reversible
= 1;
2339 route
->path_rec
->pkey
= cpu_to_be16(0xffff);
2340 route
->path_rec
->mtu_selector
= IB_SA_EQ
;
2341 route
->path_rec
->sl
= iboe_tos_to_sl(ndev
, id_priv
->tos
);
2342 route
->path_rec
->mtu
= iboe_get_mtu(ndev
->mtu
);
2343 route
->path_rec
->rate_selector
= IB_SA_EQ
;
2344 route
->path_rec
->rate
= iboe_get_rate(ndev
);
2346 route
->path_rec
->packet_life_time_selector
= IB_SA_EQ
;
2347 route
->path_rec
->packet_life_time
= CMA_IBOE_PACKET_LIFETIME
;
2348 if (!route
->path_rec
->mtu
) {
2353 work
->old_state
= RDMA_CM_ROUTE_QUERY
;
2354 work
->new_state
= RDMA_CM_ROUTE_RESOLVED
;
2355 work
->event
.event
= RDMA_CM_EVENT_ROUTE_RESOLVED
;
2356 work
->event
.status
= 0;
2358 queue_work(cma_wq
, &work
->work
);
2363 kfree(route
->path_rec
);
2364 route
->path_rec
= NULL
;
2370 int rdma_resolve_route(struct rdma_cm_id
*id
, int timeout_ms
)
2372 struct rdma_id_private
*id_priv
;
2375 id_priv
= container_of(id
, struct rdma_id_private
, id
);
2376 if (!cma_comp_exch(id_priv
, RDMA_CM_ADDR_RESOLVED
, RDMA_CM_ROUTE_QUERY
))
2379 atomic_inc(&id_priv
->refcount
);
2380 if (rdma_cap_ib_sa(id
->device
, id
->port_num
))
2381 ret
= cma_resolve_ib_route(id_priv
, timeout_ms
);
2382 else if (rdma_protocol_roce(id
->device
, id
->port_num
))
2383 ret
= cma_resolve_iboe_route(id_priv
);
2384 else if (rdma_protocol_iwarp(id
->device
, id
->port_num
))
2385 ret
= cma_resolve_iw_route(id_priv
, timeout_ms
);
2394 cma_comp_exch(id_priv
, RDMA_CM_ROUTE_QUERY
, RDMA_CM_ADDR_RESOLVED
);
2395 cma_deref_id(id_priv
);
2398 EXPORT_SYMBOL(rdma_resolve_route
);
2400 static void cma_set_loopback(struct sockaddr
*addr
)
2402 switch (addr
->sa_family
) {
2404 ((struct sockaddr_in
*) addr
)->sin_addr
.s_addr
= htonl(INADDR_LOOPBACK
);
2407 ipv6_addr_set(&((struct sockaddr_in6
*) addr
)->sin6_addr
,
2411 ib_addr_set(&((struct sockaddr_ib
*) addr
)->sib_addr
,
2417 static int cma_bind_loopback(struct rdma_id_private
*id_priv
)
2419 struct cma_device
*cma_dev
, *cur_dev
;
2420 struct ib_port_attr port_attr
;
2428 list_for_each_entry(cur_dev
, &dev_list
, list
) {
2429 if (cma_family(id_priv
) == AF_IB
&&
2430 !rdma_cap_ib_cm(cur_dev
->device
, 1))
2436 for (p
= 1; p
<= cur_dev
->device
->phys_port_cnt
; ++p
) {
2437 if (!ib_query_port(cur_dev
->device
, p
, &port_attr
) &&
2438 port_attr
.state
== IB_PORT_ACTIVE
) {
2453 ret
= ib_get_cached_gid(cma_dev
->device
, p
, 0, &gid
, NULL
);
2457 ret
= ib_get_cached_pkey(cma_dev
->device
, p
, 0, &pkey
);
2461 id_priv
->id
.route
.addr
.dev_addr
.dev_type
=
2462 (rdma_protocol_ib(cma_dev
->device
, p
)) ?
2463 ARPHRD_INFINIBAND
: ARPHRD_ETHER
;
2465 rdma_addr_set_sgid(&id_priv
->id
.route
.addr
.dev_addr
, &gid
);
2466 ib_addr_set_pkey(&id_priv
->id
.route
.addr
.dev_addr
, pkey
);
2467 id_priv
->id
.port_num
= p
;
2468 cma_attach_to_dev(id_priv
, cma_dev
);
2469 cma_set_loopback(cma_src_addr(id_priv
));
2471 mutex_unlock(&lock
);
2475 static void addr_handler(int status
, struct sockaddr
*src_addr
,
2476 struct rdma_dev_addr
*dev_addr
, void *context
)
2478 struct rdma_id_private
*id_priv
= context
;
2479 struct rdma_cm_event event
;
2481 memset(&event
, 0, sizeof event
);
2482 mutex_lock(&id_priv
->handler_mutex
);
2483 if (!cma_comp_exch(id_priv
, RDMA_CM_ADDR_QUERY
,
2484 RDMA_CM_ADDR_RESOLVED
))
2487 memcpy(cma_src_addr(id_priv
), src_addr
, rdma_addr_size(src_addr
));
2488 if (!status
&& !id_priv
->cma_dev
)
2489 status
= cma_acquire_dev(id_priv
, NULL
);
2492 if (!cma_comp_exch(id_priv
, RDMA_CM_ADDR_RESOLVED
,
2493 RDMA_CM_ADDR_BOUND
))
2495 event
.event
= RDMA_CM_EVENT_ADDR_ERROR
;
2496 event
.status
= status
;
2498 event
.event
= RDMA_CM_EVENT_ADDR_RESOLVED
;
2500 if (id_priv
->id
.event_handler(&id_priv
->id
, &event
)) {
2501 cma_exch(id_priv
, RDMA_CM_DESTROYING
);
2502 mutex_unlock(&id_priv
->handler_mutex
);
2503 cma_deref_id(id_priv
);
2504 rdma_destroy_id(&id_priv
->id
);
2508 mutex_unlock(&id_priv
->handler_mutex
);
2509 cma_deref_id(id_priv
);
2512 static int cma_resolve_loopback(struct rdma_id_private
*id_priv
)
2514 struct cma_work
*work
;
2518 work
= kzalloc(sizeof *work
, GFP_KERNEL
);
2522 if (!id_priv
->cma_dev
) {
2523 ret
= cma_bind_loopback(id_priv
);
2528 rdma_addr_get_sgid(&id_priv
->id
.route
.addr
.dev_addr
, &gid
);
2529 rdma_addr_set_dgid(&id_priv
->id
.route
.addr
.dev_addr
, &gid
);
2532 INIT_WORK(&work
->work
, cma_work_handler
);
2533 work
->old_state
= RDMA_CM_ADDR_QUERY
;
2534 work
->new_state
= RDMA_CM_ADDR_RESOLVED
;
2535 work
->event
.event
= RDMA_CM_EVENT_ADDR_RESOLVED
;
2536 queue_work(cma_wq
, &work
->work
);
2543 static int cma_resolve_ib_addr(struct rdma_id_private
*id_priv
)
2545 struct cma_work
*work
;
2548 work
= kzalloc(sizeof *work
, GFP_KERNEL
);
2552 if (!id_priv
->cma_dev
) {
2553 ret
= cma_resolve_ib_dev(id_priv
);
2558 rdma_addr_set_dgid(&id_priv
->id
.route
.addr
.dev_addr
, (union ib_gid
*)
2559 &(((struct sockaddr_ib
*) &id_priv
->id
.route
.addr
.dst_addr
)->sib_addr
));
2562 INIT_WORK(&work
->work
, cma_work_handler
);
2563 work
->old_state
= RDMA_CM_ADDR_QUERY
;
2564 work
->new_state
= RDMA_CM_ADDR_RESOLVED
;
2565 work
->event
.event
= RDMA_CM_EVENT_ADDR_RESOLVED
;
2566 queue_work(cma_wq
, &work
->work
);
2573 static int cma_bind_addr(struct rdma_cm_id
*id
, struct sockaddr
*src_addr
,
2574 struct sockaddr
*dst_addr
)
2576 if (!src_addr
|| !src_addr
->sa_family
) {
2577 src_addr
= (struct sockaddr
*) &id
->route
.addr
.src_addr
;
2578 src_addr
->sa_family
= dst_addr
->sa_family
;
2579 if (dst_addr
->sa_family
== AF_INET6
) {
2580 struct sockaddr_in6
*src_addr6
= (struct sockaddr_in6
*) src_addr
;
2581 struct sockaddr_in6
*dst_addr6
= (struct sockaddr_in6
*) dst_addr
;
2582 src_addr6
->sin6_scope_id
= dst_addr6
->sin6_scope_id
;
2583 if (ipv6_addr_type(&dst_addr6
->sin6_addr
) & IPV6_ADDR_LINKLOCAL
)
2584 id
->route
.addr
.dev_addr
.bound_dev_if
= dst_addr6
->sin6_scope_id
;
2585 } else if (dst_addr
->sa_family
== AF_IB
) {
2586 ((struct sockaddr_ib
*) src_addr
)->sib_pkey
=
2587 ((struct sockaddr_ib
*) dst_addr
)->sib_pkey
;
2590 return rdma_bind_addr(id
, src_addr
);
2593 int rdma_resolve_addr(struct rdma_cm_id
*id
, struct sockaddr
*src_addr
,
2594 struct sockaddr
*dst_addr
, int timeout_ms
)
2596 struct rdma_id_private
*id_priv
;
2599 id_priv
= container_of(id
, struct rdma_id_private
, id
);
2600 if (id_priv
->state
== RDMA_CM_IDLE
) {
2601 ret
= cma_bind_addr(id
, src_addr
, dst_addr
);
2606 if (cma_family(id_priv
) != dst_addr
->sa_family
)
2609 if (!cma_comp_exch(id_priv
, RDMA_CM_ADDR_BOUND
, RDMA_CM_ADDR_QUERY
))
2612 atomic_inc(&id_priv
->refcount
);
2613 memcpy(cma_dst_addr(id_priv
), dst_addr
, rdma_addr_size(dst_addr
));
2614 if (cma_any_addr(dst_addr
)) {
2615 ret
= cma_resolve_loopback(id_priv
);
2617 if (dst_addr
->sa_family
== AF_IB
) {
2618 ret
= cma_resolve_ib_addr(id_priv
);
2620 ret
= rdma_resolve_ip(&addr_client
, cma_src_addr(id_priv
),
2621 dst_addr
, &id
->route
.addr
.dev_addr
,
2622 timeout_ms
, addr_handler
, id_priv
);
2630 cma_comp_exch(id_priv
, RDMA_CM_ADDR_QUERY
, RDMA_CM_ADDR_BOUND
);
2631 cma_deref_id(id_priv
);
2634 EXPORT_SYMBOL(rdma_resolve_addr
);
2636 int rdma_set_reuseaddr(struct rdma_cm_id
*id
, int reuse
)
2638 struct rdma_id_private
*id_priv
;
2639 unsigned long flags
;
2642 id_priv
= container_of(id
, struct rdma_id_private
, id
);
2643 spin_lock_irqsave(&id_priv
->lock
, flags
);
2644 if (reuse
|| id_priv
->state
== RDMA_CM_IDLE
) {
2645 id_priv
->reuseaddr
= reuse
;
2650 spin_unlock_irqrestore(&id_priv
->lock
, flags
);
2653 EXPORT_SYMBOL(rdma_set_reuseaddr
);
2655 int rdma_set_afonly(struct rdma_cm_id
*id
, int afonly
)
2657 struct rdma_id_private
*id_priv
;
2658 unsigned long flags
;
2661 id_priv
= container_of(id
, struct rdma_id_private
, id
);
2662 spin_lock_irqsave(&id_priv
->lock
, flags
);
2663 if (id_priv
->state
== RDMA_CM_IDLE
|| id_priv
->state
== RDMA_CM_ADDR_BOUND
) {
2664 id_priv
->options
|= (1 << CMA_OPTION_AFONLY
);
2665 id_priv
->afonly
= afonly
;
2670 spin_unlock_irqrestore(&id_priv
->lock
, flags
);
2673 EXPORT_SYMBOL(rdma_set_afonly
);
2675 static void cma_bind_port(struct rdma_bind_list
*bind_list
,
2676 struct rdma_id_private
*id_priv
)
2678 struct sockaddr
*addr
;
2679 struct sockaddr_ib
*sib
;
2683 addr
= cma_src_addr(id_priv
);
2684 port
= htons(bind_list
->port
);
2686 switch (addr
->sa_family
) {
2688 ((struct sockaddr_in
*) addr
)->sin_port
= port
;
2691 ((struct sockaddr_in6
*) addr
)->sin6_port
= port
;
2694 sib
= (struct sockaddr_ib
*) addr
;
2695 sid
= be64_to_cpu(sib
->sib_sid
);
2696 mask
= be64_to_cpu(sib
->sib_sid_mask
);
2697 sib
->sib_sid
= cpu_to_be64((sid
& mask
) | (u64
) ntohs(port
));
2698 sib
->sib_sid_mask
= cpu_to_be64(~0ULL);
2701 id_priv
->bind_list
= bind_list
;
2702 hlist_add_head(&id_priv
->node
, &bind_list
->owners
);
2705 static int cma_alloc_port(enum rdma_port_space ps
,
2706 struct rdma_id_private
*id_priv
, unsigned short snum
)
2708 struct rdma_bind_list
*bind_list
;
2711 bind_list
= kzalloc(sizeof *bind_list
, GFP_KERNEL
);
2715 ret
= cma_ps_alloc(id_priv
->id
.route
.addr
.dev_addr
.net
, ps
, bind_list
,
2721 bind_list
->port
= (unsigned short)ret
;
2722 cma_bind_port(bind_list
, id_priv
);
2726 return ret
== -ENOSPC
? -EADDRNOTAVAIL
: ret
;
2729 static int cma_alloc_any_port(enum rdma_port_space ps
,
2730 struct rdma_id_private
*id_priv
)
2732 static unsigned int last_used_port
;
2733 int low
, high
, remaining
;
2735 struct net
*net
= id_priv
->id
.route
.addr
.dev_addr
.net
;
2737 inet_get_local_port_range(net
, &low
, &high
);
2738 remaining
= (high
- low
) + 1;
2739 rover
= prandom_u32() % remaining
+ low
;
2741 if (last_used_port
!= rover
&&
2742 !cma_ps_find(net
, ps
, (unsigned short)rover
)) {
2743 int ret
= cma_alloc_port(ps
, id_priv
, rover
);
2745 * Remember previously used port number in order to avoid
2746 * re-using same port immediately after it is closed.
2749 last_used_port
= rover
;
2750 if (ret
!= -EADDRNOTAVAIL
)
2755 if ((rover
< low
) || (rover
> high
))
2759 return -EADDRNOTAVAIL
;
2763 * Check that the requested port is available. This is called when trying to
2764 * bind to a specific port, or when trying to listen on a bound port. In
2765 * the latter case, the provided id_priv may already be on the bind_list, but
2766 * we still need to check that it's okay to start listening.
2768 static int cma_check_port(struct rdma_bind_list
*bind_list
,
2769 struct rdma_id_private
*id_priv
, uint8_t reuseaddr
)
2771 struct rdma_id_private
*cur_id
;
2772 struct sockaddr
*addr
, *cur_addr
;
2774 addr
= cma_src_addr(id_priv
);
2775 hlist_for_each_entry(cur_id
, &bind_list
->owners
, node
) {
2776 if (id_priv
== cur_id
)
2779 if ((cur_id
->state
!= RDMA_CM_LISTEN
) && reuseaddr
&&
2783 cur_addr
= cma_src_addr(cur_id
);
2784 if (id_priv
->afonly
&& cur_id
->afonly
&&
2785 (addr
->sa_family
!= cur_addr
->sa_family
))
2788 if (cma_any_addr(addr
) || cma_any_addr(cur_addr
))
2789 return -EADDRNOTAVAIL
;
2791 if (!cma_addr_cmp(addr
, cur_addr
))
2797 static int cma_use_port(enum rdma_port_space ps
,
2798 struct rdma_id_private
*id_priv
)
2800 struct rdma_bind_list
*bind_list
;
2801 unsigned short snum
;
2804 snum
= ntohs(cma_port(cma_src_addr(id_priv
)));
2805 if (snum
< PROT_SOCK
&& !capable(CAP_NET_BIND_SERVICE
))
2808 bind_list
= cma_ps_find(id_priv
->id
.route
.addr
.dev_addr
.net
, ps
, snum
);
2810 ret
= cma_alloc_port(ps
, id_priv
, snum
);
2812 ret
= cma_check_port(bind_list
, id_priv
, id_priv
->reuseaddr
);
2814 cma_bind_port(bind_list
, id_priv
);
2819 static int cma_bind_listen(struct rdma_id_private
*id_priv
)
2821 struct rdma_bind_list
*bind_list
= id_priv
->bind_list
;
2825 if (bind_list
->owners
.first
->next
)
2826 ret
= cma_check_port(bind_list
, id_priv
, 0);
2827 mutex_unlock(&lock
);
2831 static enum rdma_port_space
cma_select_inet_ps(
2832 struct rdma_id_private
*id_priv
)
2834 switch (id_priv
->id
.ps
) {
2839 return id_priv
->id
.ps
;
2846 static enum rdma_port_space
cma_select_ib_ps(struct rdma_id_private
*id_priv
)
2848 enum rdma_port_space ps
= 0;
2849 struct sockaddr_ib
*sib
;
2850 u64 sid_ps
, mask
, sid
;
2852 sib
= (struct sockaddr_ib
*) cma_src_addr(id_priv
);
2853 mask
= be64_to_cpu(sib
->sib_sid_mask
) & RDMA_IB_IP_PS_MASK
;
2854 sid
= be64_to_cpu(sib
->sib_sid
) & mask
;
2856 if ((id_priv
->id
.ps
== RDMA_PS_IB
) && (sid
== (RDMA_IB_IP_PS_IB
& mask
))) {
2857 sid_ps
= RDMA_IB_IP_PS_IB
;
2859 } else if (((id_priv
->id
.ps
== RDMA_PS_IB
) || (id_priv
->id
.ps
== RDMA_PS_TCP
)) &&
2860 (sid
== (RDMA_IB_IP_PS_TCP
& mask
))) {
2861 sid_ps
= RDMA_IB_IP_PS_TCP
;
2863 } else if (((id_priv
->id
.ps
== RDMA_PS_IB
) || (id_priv
->id
.ps
== RDMA_PS_UDP
)) &&
2864 (sid
== (RDMA_IB_IP_PS_UDP
& mask
))) {
2865 sid_ps
= RDMA_IB_IP_PS_UDP
;
2870 sib
->sib_sid
= cpu_to_be64(sid_ps
| ntohs(cma_port((struct sockaddr
*) sib
)));
2871 sib
->sib_sid_mask
= cpu_to_be64(RDMA_IB_IP_PS_MASK
|
2872 be64_to_cpu(sib
->sib_sid_mask
));
2877 static int cma_get_port(struct rdma_id_private
*id_priv
)
2879 enum rdma_port_space ps
;
2882 if (cma_family(id_priv
) != AF_IB
)
2883 ps
= cma_select_inet_ps(id_priv
);
2885 ps
= cma_select_ib_ps(id_priv
);
2887 return -EPROTONOSUPPORT
;
2890 if (cma_any_port(cma_src_addr(id_priv
)))
2891 ret
= cma_alloc_any_port(ps
, id_priv
);
2893 ret
= cma_use_port(ps
, id_priv
);
2894 mutex_unlock(&lock
);
2899 static int cma_check_linklocal(struct rdma_dev_addr
*dev_addr
,
2900 struct sockaddr
*addr
)
2902 #if IS_ENABLED(CONFIG_IPV6)
2903 struct sockaddr_in6
*sin6
;
2905 if (addr
->sa_family
!= AF_INET6
)
2908 sin6
= (struct sockaddr_in6
*) addr
;
2910 if (!(ipv6_addr_type(&sin6
->sin6_addr
) & IPV6_ADDR_LINKLOCAL
))
2913 if (!sin6
->sin6_scope_id
)
2916 dev_addr
->bound_dev_if
= sin6
->sin6_scope_id
;
2921 int rdma_listen(struct rdma_cm_id
*id
, int backlog
)
2923 struct rdma_id_private
*id_priv
;
2926 id_priv
= container_of(id
, struct rdma_id_private
, id
);
2927 if (id_priv
->state
== RDMA_CM_IDLE
) {
2928 id
->route
.addr
.src_addr
.ss_family
= AF_INET
;
2929 ret
= rdma_bind_addr(id
, cma_src_addr(id_priv
));
2934 if (!cma_comp_exch(id_priv
, RDMA_CM_ADDR_BOUND
, RDMA_CM_LISTEN
))
2937 if (id_priv
->reuseaddr
) {
2938 ret
= cma_bind_listen(id_priv
);
2943 id_priv
->backlog
= backlog
;
2945 if (rdma_cap_ib_cm(id
->device
, 1)) {
2946 ret
= cma_ib_listen(id_priv
);
2949 } else if (rdma_cap_iw_cm(id
->device
, 1)) {
2950 ret
= cma_iw_listen(id_priv
, backlog
);
2958 cma_listen_on_all(id_priv
);
2962 id_priv
->backlog
= 0;
2963 cma_comp_exch(id_priv
, RDMA_CM_LISTEN
, RDMA_CM_ADDR_BOUND
);
2966 EXPORT_SYMBOL(rdma_listen
);
2968 int rdma_bind_addr(struct rdma_cm_id
*id
, struct sockaddr
*addr
)
2970 struct rdma_id_private
*id_priv
;
2973 if (addr
->sa_family
!= AF_INET
&& addr
->sa_family
!= AF_INET6
&&
2974 addr
->sa_family
!= AF_IB
)
2975 return -EAFNOSUPPORT
;
2977 id_priv
= container_of(id
, struct rdma_id_private
, id
);
2978 if (!cma_comp_exch(id_priv
, RDMA_CM_IDLE
, RDMA_CM_ADDR_BOUND
))
2981 ret
= cma_check_linklocal(&id
->route
.addr
.dev_addr
, addr
);
2985 memcpy(cma_src_addr(id_priv
), addr
, rdma_addr_size(addr
));
2986 if (!cma_any_addr(addr
)) {
2987 ret
= cma_translate_addr(addr
, &id
->route
.addr
.dev_addr
);
2991 ret
= cma_acquire_dev(id_priv
, NULL
);
2996 if (!(id_priv
->options
& (1 << CMA_OPTION_AFONLY
))) {
2997 if (addr
->sa_family
== AF_INET
)
2998 id_priv
->afonly
= 1;
2999 #if IS_ENABLED(CONFIG_IPV6)
3000 else if (addr
->sa_family
== AF_INET6
) {
3001 struct net
*net
= id_priv
->id
.route
.addr
.dev_addr
.net
;
3003 id_priv
->afonly
= net
->ipv6
.sysctl
.bindv6only
;
3007 ret
= cma_get_port(id_priv
);
3013 if (id_priv
->cma_dev
)
3014 cma_release_dev(id_priv
);
3016 cma_comp_exch(id_priv
, RDMA_CM_ADDR_BOUND
, RDMA_CM_IDLE
);
3019 EXPORT_SYMBOL(rdma_bind_addr
);
3021 static int cma_format_hdr(void *hdr
, struct rdma_id_private
*id_priv
)
3023 struct cma_hdr
*cma_hdr
;
3026 cma_hdr
->cma_version
= CMA_VERSION
;
3027 if (cma_family(id_priv
) == AF_INET
) {
3028 struct sockaddr_in
*src4
, *dst4
;
3030 src4
= (struct sockaddr_in
*) cma_src_addr(id_priv
);
3031 dst4
= (struct sockaddr_in
*) cma_dst_addr(id_priv
);
3033 cma_set_ip_ver(cma_hdr
, 4);
3034 cma_hdr
->src_addr
.ip4
.addr
= src4
->sin_addr
.s_addr
;
3035 cma_hdr
->dst_addr
.ip4
.addr
= dst4
->sin_addr
.s_addr
;
3036 cma_hdr
->port
= src4
->sin_port
;
3037 } else if (cma_family(id_priv
) == AF_INET6
) {
3038 struct sockaddr_in6
*src6
, *dst6
;
3040 src6
= (struct sockaddr_in6
*) cma_src_addr(id_priv
);
3041 dst6
= (struct sockaddr_in6
*) cma_dst_addr(id_priv
);
3043 cma_set_ip_ver(cma_hdr
, 6);
3044 cma_hdr
->src_addr
.ip6
= src6
->sin6_addr
;
3045 cma_hdr
->dst_addr
.ip6
= dst6
->sin6_addr
;
3046 cma_hdr
->port
= src6
->sin6_port
;
3051 static int cma_sidr_rep_handler(struct ib_cm_id
*cm_id
,
3052 struct ib_cm_event
*ib_event
)
3054 struct rdma_id_private
*id_priv
= cm_id
->context
;
3055 struct rdma_cm_event event
;
3056 struct ib_cm_sidr_rep_event_param
*rep
= &ib_event
->param
.sidr_rep_rcvd
;
3059 if (cma_disable_callback(id_priv
, RDMA_CM_CONNECT
))
3062 memset(&event
, 0, sizeof event
);
3063 switch (ib_event
->event
) {
3064 case IB_CM_SIDR_REQ_ERROR
:
3065 event
.event
= RDMA_CM_EVENT_UNREACHABLE
;
3066 event
.status
= -ETIMEDOUT
;
3068 case IB_CM_SIDR_REP_RECEIVED
:
3069 event
.param
.ud
.private_data
= ib_event
->private_data
;
3070 event
.param
.ud
.private_data_len
= IB_CM_SIDR_REP_PRIVATE_DATA_SIZE
;
3071 if (rep
->status
!= IB_SIDR_SUCCESS
) {
3072 event
.event
= RDMA_CM_EVENT_UNREACHABLE
;
3073 event
.status
= ib_event
->param
.sidr_rep_rcvd
.status
;
3076 ret
= cma_set_qkey(id_priv
, rep
->qkey
);
3078 event
.event
= RDMA_CM_EVENT_ADDR_ERROR
;
3082 ib_init_ah_from_path(id_priv
->id
.device
, id_priv
->id
.port_num
,
3083 id_priv
->id
.route
.path_rec
,
3084 &event
.param
.ud
.ah_attr
);
3085 event
.param
.ud
.qp_num
= rep
->qpn
;
3086 event
.param
.ud
.qkey
= rep
->qkey
;
3087 event
.event
= RDMA_CM_EVENT_ESTABLISHED
;
3091 printk(KERN_ERR
"RDMA CMA: unexpected IB CM event: %d\n",
3096 ret
= id_priv
->id
.event_handler(&id_priv
->id
, &event
);
3098 /* Destroy the CM ID by returning a non-zero value. */
3099 id_priv
->cm_id
.ib
= NULL
;
3100 cma_exch(id_priv
, RDMA_CM_DESTROYING
);
3101 mutex_unlock(&id_priv
->handler_mutex
);
3102 rdma_destroy_id(&id_priv
->id
);
3106 mutex_unlock(&id_priv
->handler_mutex
);
3110 static int cma_resolve_ib_udp(struct rdma_id_private
*id_priv
,
3111 struct rdma_conn_param
*conn_param
)
3113 struct ib_cm_sidr_req_param req
;
3114 struct ib_cm_id
*id
;
3118 memset(&req
, 0, sizeof req
);
3119 offset
= cma_user_data_offset(id_priv
);
3120 req
.private_data_len
= offset
+ conn_param
->private_data_len
;
3121 if (req
.private_data_len
< conn_param
->private_data_len
)
3124 if (req
.private_data_len
) {
3125 private_data
= kzalloc(req
.private_data_len
, GFP_ATOMIC
);
3129 private_data
= NULL
;
3132 if (conn_param
->private_data
&& conn_param
->private_data_len
)
3133 memcpy(private_data
+ offset
, conn_param
->private_data
,
3134 conn_param
->private_data_len
);
3137 ret
= cma_format_hdr(private_data
, id_priv
);
3140 req
.private_data
= private_data
;
3143 id
= ib_create_cm_id(id_priv
->id
.device
, cma_sidr_rep_handler
,
3149 id_priv
->cm_id
.ib
= id
;
3151 req
.path
= id_priv
->id
.route
.path_rec
;
3152 req
.service_id
= rdma_get_service_id(&id_priv
->id
, cma_dst_addr(id_priv
));
3153 req
.timeout_ms
= 1 << (CMA_CM_RESPONSE_TIMEOUT
- 8);
3154 req
.max_cm_retries
= CMA_MAX_CM_RETRIES
;
3156 ret
= ib_send_cm_sidr_req(id_priv
->cm_id
.ib
, &req
);
3158 ib_destroy_cm_id(id_priv
->cm_id
.ib
);
3159 id_priv
->cm_id
.ib
= NULL
;
3162 kfree(private_data
);
3166 static int cma_connect_ib(struct rdma_id_private
*id_priv
,
3167 struct rdma_conn_param
*conn_param
)
3169 struct ib_cm_req_param req
;
3170 struct rdma_route
*route
;
3172 struct ib_cm_id
*id
;
3175 memset(&req
, 0, sizeof req
);
3176 offset
= cma_user_data_offset(id_priv
);
3177 req
.private_data_len
= offset
+ conn_param
->private_data_len
;
3178 if (req
.private_data_len
< conn_param
->private_data_len
)
3181 if (req
.private_data_len
) {
3182 private_data
= kzalloc(req
.private_data_len
, GFP_ATOMIC
);
3186 private_data
= NULL
;
3189 if (conn_param
->private_data
&& conn_param
->private_data_len
)
3190 memcpy(private_data
+ offset
, conn_param
->private_data
,
3191 conn_param
->private_data_len
);
3193 id
= ib_create_cm_id(id_priv
->id
.device
, cma_ib_handler
, id_priv
);
3198 id_priv
->cm_id
.ib
= id
;
3200 route
= &id_priv
->id
.route
;
3202 ret
= cma_format_hdr(private_data
, id_priv
);
3205 req
.private_data
= private_data
;
3208 req
.primary_path
= &route
->path_rec
[0];
3209 if (route
->num_paths
== 2)
3210 req
.alternate_path
= &route
->path_rec
[1];
3212 req
.service_id
= rdma_get_service_id(&id_priv
->id
, cma_dst_addr(id_priv
));
3213 req
.qp_num
= id_priv
->qp_num
;
3214 req
.qp_type
= id_priv
->id
.qp_type
;
3215 req
.starting_psn
= id_priv
->seq_num
;
3216 req
.responder_resources
= conn_param
->responder_resources
;
3217 req
.initiator_depth
= conn_param
->initiator_depth
;
3218 req
.flow_control
= conn_param
->flow_control
;
3219 req
.retry_count
= min_t(u8
, 7, conn_param
->retry_count
);
3220 req
.rnr_retry_count
= min_t(u8
, 7, conn_param
->rnr_retry_count
);
3221 req
.remote_cm_response_timeout
= CMA_CM_RESPONSE_TIMEOUT
;
3222 req
.local_cm_response_timeout
= CMA_CM_RESPONSE_TIMEOUT
;
3223 req
.max_cm_retries
= CMA_MAX_CM_RETRIES
;
3224 req
.srq
= id_priv
->srq
? 1 : 0;
3226 ret
= ib_send_cm_req(id_priv
->cm_id
.ib
, &req
);
3228 if (ret
&& !IS_ERR(id
)) {
3229 ib_destroy_cm_id(id
);
3230 id_priv
->cm_id
.ib
= NULL
;
3233 kfree(private_data
);
3237 static int cma_connect_iw(struct rdma_id_private
*id_priv
,
3238 struct rdma_conn_param
*conn_param
)
3240 struct iw_cm_id
*cm_id
;
3242 struct iw_cm_conn_param iw_param
;
3244 cm_id
= iw_create_cm_id(id_priv
->id
.device
, cma_iw_handler
, id_priv
);
3246 return PTR_ERR(cm_id
);
3248 cm_id
->tos
= id_priv
->tos
;
3249 id_priv
->cm_id
.iw
= cm_id
;
3251 memcpy(&cm_id
->local_addr
, cma_src_addr(id_priv
),
3252 rdma_addr_size(cma_src_addr(id_priv
)));
3253 memcpy(&cm_id
->remote_addr
, cma_dst_addr(id_priv
),
3254 rdma_addr_size(cma_dst_addr(id_priv
)));
3256 ret
= cma_modify_qp_rtr(id_priv
, conn_param
);
3261 iw_param
.ord
= conn_param
->initiator_depth
;
3262 iw_param
.ird
= conn_param
->responder_resources
;
3263 iw_param
.private_data
= conn_param
->private_data
;
3264 iw_param
.private_data_len
= conn_param
->private_data_len
;
3265 iw_param
.qpn
= id_priv
->id
.qp
? id_priv
->qp_num
: conn_param
->qp_num
;
3267 memset(&iw_param
, 0, sizeof iw_param
);
3268 iw_param
.qpn
= id_priv
->qp_num
;
3270 ret
= iw_cm_connect(cm_id
, &iw_param
);
3273 iw_destroy_cm_id(cm_id
);
3274 id_priv
->cm_id
.iw
= NULL
;
3279 int rdma_connect(struct rdma_cm_id
*id
, struct rdma_conn_param
*conn_param
)
3281 struct rdma_id_private
*id_priv
;
3284 id_priv
= container_of(id
, struct rdma_id_private
, id
);
3285 if (!cma_comp_exch(id_priv
, RDMA_CM_ROUTE_RESOLVED
, RDMA_CM_CONNECT
))
3289 id_priv
->qp_num
= conn_param
->qp_num
;
3290 id_priv
->srq
= conn_param
->srq
;
3293 if (rdma_cap_ib_cm(id
->device
, id
->port_num
)) {
3294 if (id
->qp_type
== IB_QPT_UD
)
3295 ret
= cma_resolve_ib_udp(id_priv
, conn_param
);
3297 ret
= cma_connect_ib(id_priv
, conn_param
);
3298 } else if (rdma_cap_iw_cm(id
->device
, id
->port_num
))
3299 ret
= cma_connect_iw(id_priv
, conn_param
);
3307 cma_comp_exch(id_priv
, RDMA_CM_CONNECT
, RDMA_CM_ROUTE_RESOLVED
);
3310 EXPORT_SYMBOL(rdma_connect
);
3312 static int cma_accept_ib(struct rdma_id_private
*id_priv
,
3313 struct rdma_conn_param
*conn_param
)
3315 struct ib_cm_rep_param rep
;
3318 ret
= cma_modify_qp_rtr(id_priv
, conn_param
);
3322 ret
= cma_modify_qp_rts(id_priv
, conn_param
);
3326 memset(&rep
, 0, sizeof rep
);
3327 rep
.qp_num
= id_priv
->qp_num
;
3328 rep
.starting_psn
= id_priv
->seq_num
;
3329 rep
.private_data
= conn_param
->private_data
;
3330 rep
.private_data_len
= conn_param
->private_data_len
;
3331 rep
.responder_resources
= conn_param
->responder_resources
;
3332 rep
.initiator_depth
= conn_param
->initiator_depth
;
3333 rep
.failover_accepted
= 0;
3334 rep
.flow_control
= conn_param
->flow_control
;
3335 rep
.rnr_retry_count
= min_t(u8
, 7, conn_param
->rnr_retry_count
);
3336 rep
.srq
= id_priv
->srq
? 1 : 0;
3338 ret
= ib_send_cm_rep(id_priv
->cm_id
.ib
, &rep
);
3343 static int cma_accept_iw(struct rdma_id_private
*id_priv
,
3344 struct rdma_conn_param
*conn_param
)
3346 struct iw_cm_conn_param iw_param
;
3349 ret
= cma_modify_qp_rtr(id_priv
, conn_param
);
3353 iw_param
.ord
= conn_param
->initiator_depth
;
3354 iw_param
.ird
= conn_param
->responder_resources
;
3355 iw_param
.private_data
= conn_param
->private_data
;
3356 iw_param
.private_data_len
= conn_param
->private_data_len
;
3357 if (id_priv
->id
.qp
) {
3358 iw_param
.qpn
= id_priv
->qp_num
;
3360 iw_param
.qpn
= conn_param
->qp_num
;
3362 return iw_cm_accept(id_priv
->cm_id
.iw
, &iw_param
);
3365 static int cma_send_sidr_rep(struct rdma_id_private
*id_priv
,
3366 enum ib_cm_sidr_status status
, u32 qkey
,
3367 const void *private_data
, int private_data_len
)
3369 struct ib_cm_sidr_rep_param rep
;
3372 memset(&rep
, 0, sizeof rep
);
3373 rep
.status
= status
;
3374 if (status
== IB_SIDR_SUCCESS
) {
3375 ret
= cma_set_qkey(id_priv
, qkey
);
3378 rep
.qp_num
= id_priv
->qp_num
;
3379 rep
.qkey
= id_priv
->qkey
;
3381 rep
.private_data
= private_data
;
3382 rep
.private_data_len
= private_data_len
;
3384 return ib_send_cm_sidr_rep(id_priv
->cm_id
.ib
, &rep
);
3387 int rdma_accept(struct rdma_cm_id
*id
, struct rdma_conn_param
*conn_param
)
3389 struct rdma_id_private
*id_priv
;
3392 id_priv
= container_of(id
, struct rdma_id_private
, id
);
3394 id_priv
->owner
= task_pid_nr(current
);
3396 if (!cma_comp(id_priv
, RDMA_CM_CONNECT
))
3399 if (!id
->qp
&& conn_param
) {
3400 id_priv
->qp_num
= conn_param
->qp_num
;
3401 id_priv
->srq
= conn_param
->srq
;
3404 if (rdma_cap_ib_cm(id
->device
, id
->port_num
)) {
3405 if (id
->qp_type
== IB_QPT_UD
) {
3407 ret
= cma_send_sidr_rep(id_priv
, IB_SIDR_SUCCESS
,
3409 conn_param
->private_data
,
3410 conn_param
->private_data_len
);
3412 ret
= cma_send_sidr_rep(id_priv
, IB_SIDR_SUCCESS
,
3416 ret
= cma_accept_ib(id_priv
, conn_param
);
3418 ret
= cma_rep_recv(id_priv
);
3420 } else if (rdma_cap_iw_cm(id
->device
, id
->port_num
))
3421 ret
= cma_accept_iw(id_priv
, conn_param
);
3430 cma_modify_qp_err(id_priv
);
3431 rdma_reject(id
, NULL
, 0);
3434 EXPORT_SYMBOL(rdma_accept
);
3436 int rdma_notify(struct rdma_cm_id
*id
, enum ib_event_type event
)
3438 struct rdma_id_private
*id_priv
;
3441 id_priv
= container_of(id
, struct rdma_id_private
, id
);
3442 if (!id_priv
->cm_id
.ib
)
3445 switch (id
->device
->node_type
) {
3446 case RDMA_NODE_IB_CA
:
3447 ret
= ib_cm_notify(id_priv
->cm_id
.ib
, event
);
3455 EXPORT_SYMBOL(rdma_notify
);
3457 int rdma_reject(struct rdma_cm_id
*id
, const void *private_data
,
3458 u8 private_data_len
)
3460 struct rdma_id_private
*id_priv
;
3463 id_priv
= container_of(id
, struct rdma_id_private
, id
);
3464 if (!id_priv
->cm_id
.ib
)
3467 if (rdma_cap_ib_cm(id
->device
, id
->port_num
)) {
3468 if (id
->qp_type
== IB_QPT_UD
)
3469 ret
= cma_send_sidr_rep(id_priv
, IB_SIDR_REJECT
, 0,
3470 private_data
, private_data_len
);
3472 ret
= ib_send_cm_rej(id_priv
->cm_id
.ib
,
3473 IB_CM_REJ_CONSUMER_DEFINED
, NULL
,
3474 0, private_data
, private_data_len
);
3475 } else if (rdma_cap_iw_cm(id
->device
, id
->port_num
)) {
3476 ret
= iw_cm_reject(id_priv
->cm_id
.iw
,
3477 private_data
, private_data_len
);
3483 EXPORT_SYMBOL(rdma_reject
);
3485 int rdma_disconnect(struct rdma_cm_id
*id
)
3487 struct rdma_id_private
*id_priv
;
3490 id_priv
= container_of(id
, struct rdma_id_private
, id
);
3491 if (!id_priv
->cm_id
.ib
)
3494 if (rdma_cap_ib_cm(id
->device
, id
->port_num
)) {
3495 ret
= cma_modify_qp_err(id_priv
);
3498 /* Initiate or respond to a disconnect. */
3499 if (ib_send_cm_dreq(id_priv
->cm_id
.ib
, NULL
, 0))
3500 ib_send_cm_drep(id_priv
->cm_id
.ib
, NULL
, 0);
3501 } else if (rdma_cap_iw_cm(id
->device
, id
->port_num
)) {
3502 ret
= iw_cm_disconnect(id_priv
->cm_id
.iw
, 0);
3509 EXPORT_SYMBOL(rdma_disconnect
);
3511 static int cma_ib_mc_handler(int status
, struct ib_sa_multicast
*multicast
)
3513 struct rdma_id_private
*id_priv
;
3514 struct cma_multicast
*mc
= multicast
->context
;
3515 struct rdma_cm_event event
;
3518 id_priv
= mc
->id_priv
;
3519 if (cma_disable_callback(id_priv
, RDMA_CM_ADDR_BOUND
) &&
3520 cma_disable_callback(id_priv
, RDMA_CM_ADDR_RESOLVED
))
3524 status
= cma_set_qkey(id_priv
, be32_to_cpu(multicast
->rec
.qkey
));
3525 mutex_lock(&id_priv
->qp_mutex
);
3526 if (!status
&& id_priv
->id
.qp
)
3527 status
= ib_attach_mcast(id_priv
->id
.qp
, &multicast
->rec
.mgid
,
3528 be16_to_cpu(multicast
->rec
.mlid
));
3529 mutex_unlock(&id_priv
->qp_mutex
);
3531 memset(&event
, 0, sizeof event
);
3532 event
.status
= status
;
3533 event
.param
.ud
.private_data
= mc
->context
;
3535 event
.event
= RDMA_CM_EVENT_MULTICAST_JOIN
;
3536 ib_init_ah_from_mcmember(id_priv
->id
.device
,
3537 id_priv
->id
.port_num
, &multicast
->rec
,
3538 &event
.param
.ud
.ah_attr
);
3539 event
.param
.ud
.qp_num
= 0xFFFFFF;
3540 event
.param
.ud
.qkey
= be32_to_cpu(multicast
->rec
.qkey
);
3542 event
.event
= RDMA_CM_EVENT_MULTICAST_ERROR
;
3544 ret
= id_priv
->id
.event_handler(&id_priv
->id
, &event
);
3546 cma_exch(id_priv
, RDMA_CM_DESTROYING
);
3547 mutex_unlock(&id_priv
->handler_mutex
);
3548 rdma_destroy_id(&id_priv
->id
);
3552 mutex_unlock(&id_priv
->handler_mutex
);
3556 static void cma_set_mgid(struct rdma_id_private
*id_priv
,
3557 struct sockaddr
*addr
, union ib_gid
*mgid
)
3559 unsigned char mc_map
[MAX_ADDR_LEN
];
3560 struct rdma_dev_addr
*dev_addr
= &id_priv
->id
.route
.addr
.dev_addr
;
3561 struct sockaddr_in
*sin
= (struct sockaddr_in
*) addr
;
3562 struct sockaddr_in6
*sin6
= (struct sockaddr_in6
*) addr
;
3564 if (cma_any_addr(addr
)) {
3565 memset(mgid
, 0, sizeof *mgid
);
3566 } else if ((addr
->sa_family
== AF_INET6
) &&
3567 ((be32_to_cpu(sin6
->sin6_addr
.s6_addr32
[0]) & 0xFFF0FFFF) ==
3569 /* IPv6 address is an SA assigned MGID. */
3570 memcpy(mgid
, &sin6
->sin6_addr
, sizeof *mgid
);
3571 } else if (addr
->sa_family
== AF_IB
) {
3572 memcpy(mgid
, &((struct sockaddr_ib
*) addr
)->sib_addr
, sizeof *mgid
);
3573 } else if ((addr
->sa_family
== AF_INET6
)) {
3574 ipv6_ib_mc_map(&sin6
->sin6_addr
, dev_addr
->broadcast
, mc_map
);
3575 if (id_priv
->id
.ps
== RDMA_PS_UDP
)
3576 mc_map
[7] = 0x01; /* Use RDMA CM signature */
3577 *mgid
= *(union ib_gid
*) (mc_map
+ 4);
3579 ip_ib_mc_map(sin
->sin_addr
.s_addr
, dev_addr
->broadcast
, mc_map
);
3580 if (id_priv
->id
.ps
== RDMA_PS_UDP
)
3581 mc_map
[7] = 0x01; /* Use RDMA CM signature */
3582 *mgid
= *(union ib_gid
*) (mc_map
+ 4);
3586 static int cma_join_ib_multicast(struct rdma_id_private
*id_priv
,
3587 struct cma_multicast
*mc
)
3589 struct ib_sa_mcmember_rec rec
;
3590 struct rdma_dev_addr
*dev_addr
= &id_priv
->id
.route
.addr
.dev_addr
;
3591 ib_sa_comp_mask comp_mask
;
3594 ib_addr_get_mgid(dev_addr
, &rec
.mgid
);
3595 ret
= ib_sa_get_mcmember_rec(id_priv
->id
.device
, id_priv
->id
.port_num
,
3600 ret
= cma_set_qkey(id_priv
, 0);
3604 cma_set_mgid(id_priv
, (struct sockaddr
*) &mc
->addr
, &rec
.mgid
);
3605 rec
.qkey
= cpu_to_be32(id_priv
->qkey
);
3606 rdma_addr_get_sgid(dev_addr
, &rec
.port_gid
);
3607 rec
.pkey
= cpu_to_be16(ib_addr_get_pkey(dev_addr
));
3610 comp_mask
= IB_SA_MCMEMBER_REC_MGID
| IB_SA_MCMEMBER_REC_PORT_GID
|
3611 IB_SA_MCMEMBER_REC_PKEY
| IB_SA_MCMEMBER_REC_JOIN_STATE
|
3612 IB_SA_MCMEMBER_REC_QKEY
| IB_SA_MCMEMBER_REC_SL
|
3613 IB_SA_MCMEMBER_REC_FLOW_LABEL
|
3614 IB_SA_MCMEMBER_REC_TRAFFIC_CLASS
;
3616 if (id_priv
->id
.ps
== RDMA_PS_IPOIB
)
3617 comp_mask
|= IB_SA_MCMEMBER_REC_RATE
|
3618 IB_SA_MCMEMBER_REC_RATE_SELECTOR
|
3619 IB_SA_MCMEMBER_REC_MTU_SELECTOR
|
3620 IB_SA_MCMEMBER_REC_MTU
|
3621 IB_SA_MCMEMBER_REC_HOP_LIMIT
;
3623 mc
->multicast
.ib
= ib_sa_join_multicast(&sa_client
, id_priv
->id
.device
,
3624 id_priv
->id
.port_num
, &rec
,
3625 comp_mask
, GFP_KERNEL
,
3626 cma_ib_mc_handler
, mc
);
3627 return PTR_ERR_OR_ZERO(mc
->multicast
.ib
);
3630 static void iboe_mcast_work_handler(struct work_struct
*work
)
3632 struct iboe_mcast_work
*mw
= container_of(work
, struct iboe_mcast_work
, work
);
3633 struct cma_multicast
*mc
= mw
->mc
;
3634 struct ib_sa_multicast
*m
= mc
->multicast
.ib
;
3636 mc
->multicast
.ib
->context
= mc
;
3637 cma_ib_mc_handler(0, m
);
3638 kref_put(&mc
->mcref
, release_mc
);
3642 static void cma_iboe_set_mgid(struct sockaddr
*addr
, union ib_gid
*mgid
)
3644 struct sockaddr_in
*sin
= (struct sockaddr_in
*)addr
;
3645 struct sockaddr_in6
*sin6
= (struct sockaddr_in6
*)addr
;
3647 if (cma_any_addr(addr
)) {
3648 memset(mgid
, 0, sizeof *mgid
);
3649 } else if (addr
->sa_family
== AF_INET6
) {
3650 memcpy(mgid
, &sin6
->sin6_addr
, sizeof *mgid
);
3652 mgid
->raw
[0] = 0xff;
3653 mgid
->raw
[1] = 0x0e;
3662 mgid
->raw
[10] = 0xff;
3663 mgid
->raw
[11] = 0xff;
3664 *(__be32
*)(&mgid
->raw
[12]) = sin
->sin_addr
.s_addr
;
3668 static int cma_iboe_join_multicast(struct rdma_id_private
*id_priv
,
3669 struct cma_multicast
*mc
)
3671 struct iboe_mcast_work
*work
;
3672 struct rdma_dev_addr
*dev_addr
= &id_priv
->id
.route
.addr
.dev_addr
;
3674 struct sockaddr
*addr
= (struct sockaddr
*)&mc
->addr
;
3675 struct net_device
*ndev
= NULL
;
3677 if (cma_zero_addr((struct sockaddr
*)&mc
->addr
))
3680 work
= kzalloc(sizeof *work
, GFP_KERNEL
);
3684 mc
->multicast
.ib
= kzalloc(sizeof(struct ib_sa_multicast
), GFP_KERNEL
);
3685 if (!mc
->multicast
.ib
) {
3690 cma_iboe_set_mgid(addr
, &mc
->multicast
.ib
->rec
.mgid
);
3692 mc
->multicast
.ib
->rec
.pkey
= cpu_to_be16(0xffff);
3693 if (id_priv
->id
.ps
== RDMA_PS_UDP
)
3694 mc
->multicast
.ib
->rec
.qkey
= cpu_to_be32(RDMA_UDP_QKEY
);
3696 if (dev_addr
->bound_dev_if
)
3697 ndev
= dev_get_by_index(&init_net
, dev_addr
->bound_dev_if
);
3702 mc
->multicast
.ib
->rec
.rate
= iboe_get_rate(ndev
);
3703 mc
->multicast
.ib
->rec
.hop_limit
= 1;
3704 mc
->multicast
.ib
->rec
.mtu
= iboe_get_mtu(ndev
->mtu
);
3706 if (!mc
->multicast
.ib
->rec
.mtu
) {
3710 rdma_ip2gid((struct sockaddr
*)&id_priv
->id
.route
.addr
.src_addr
,
3711 &mc
->multicast
.ib
->rec
.port_gid
);
3714 INIT_WORK(&work
->work
, iboe_mcast_work_handler
);
3715 kref_get(&mc
->mcref
);
3716 queue_work(cma_wq
, &work
->work
);
3721 kfree(mc
->multicast
.ib
);
3727 int rdma_join_multicast(struct rdma_cm_id
*id
, struct sockaddr
*addr
,
3730 struct rdma_id_private
*id_priv
;
3731 struct cma_multicast
*mc
;
3734 id_priv
= container_of(id
, struct rdma_id_private
, id
);
3735 if (!cma_comp(id_priv
, RDMA_CM_ADDR_BOUND
) &&
3736 !cma_comp(id_priv
, RDMA_CM_ADDR_RESOLVED
))
3739 mc
= kmalloc(sizeof *mc
, GFP_KERNEL
);
3743 memcpy(&mc
->addr
, addr
, rdma_addr_size(addr
));
3744 mc
->context
= context
;
3745 mc
->id_priv
= id_priv
;
3747 spin_lock(&id_priv
->lock
);
3748 list_add(&mc
->list
, &id_priv
->mc_list
);
3749 spin_unlock(&id_priv
->lock
);
3751 if (rdma_protocol_roce(id
->device
, id
->port_num
)) {
3752 kref_init(&mc
->mcref
);
3753 ret
= cma_iboe_join_multicast(id_priv
, mc
);
3754 } else if (rdma_cap_ib_mcast(id
->device
, id
->port_num
))
3755 ret
= cma_join_ib_multicast(id_priv
, mc
);
3760 spin_lock_irq(&id_priv
->lock
);
3761 list_del(&mc
->list
);
3762 spin_unlock_irq(&id_priv
->lock
);
3767 EXPORT_SYMBOL(rdma_join_multicast
);
3769 void rdma_leave_multicast(struct rdma_cm_id
*id
, struct sockaddr
*addr
)
3771 struct rdma_id_private
*id_priv
;
3772 struct cma_multicast
*mc
;
3774 id_priv
= container_of(id
, struct rdma_id_private
, id
);
3775 spin_lock_irq(&id_priv
->lock
);
3776 list_for_each_entry(mc
, &id_priv
->mc_list
, list
) {
3777 if (!memcmp(&mc
->addr
, addr
, rdma_addr_size(addr
))) {
3778 list_del(&mc
->list
);
3779 spin_unlock_irq(&id_priv
->lock
);
3782 ib_detach_mcast(id
->qp
,
3783 &mc
->multicast
.ib
->rec
.mgid
,
3784 be16_to_cpu(mc
->multicast
.ib
->rec
.mlid
));
3786 BUG_ON(id_priv
->cma_dev
->device
!= id
->device
);
3788 if (rdma_cap_ib_mcast(id
->device
, id
->port_num
)) {
3789 ib_sa_free_multicast(mc
->multicast
.ib
);
3791 } else if (rdma_protocol_roce(id
->device
, id
->port_num
))
3792 kref_put(&mc
->mcref
, release_mc
);
3797 spin_unlock_irq(&id_priv
->lock
);
3799 EXPORT_SYMBOL(rdma_leave_multicast
);
3801 static int cma_netdev_change(struct net_device
*ndev
, struct rdma_id_private
*id_priv
)
3803 struct rdma_dev_addr
*dev_addr
;
3804 struct cma_ndev_work
*work
;
3806 dev_addr
= &id_priv
->id
.route
.addr
.dev_addr
;
3808 if ((dev_addr
->bound_dev_if
== ndev
->ifindex
) &&
3809 (net_eq(dev_net(ndev
), dev_addr
->net
)) &&
3810 memcmp(dev_addr
->src_dev_addr
, ndev
->dev_addr
, ndev
->addr_len
)) {
3811 printk(KERN_INFO
"RDMA CM addr change for ndev %s used by id %p\n",
3812 ndev
->name
, &id_priv
->id
);
3813 work
= kzalloc(sizeof *work
, GFP_KERNEL
);
3817 INIT_WORK(&work
->work
, cma_ndev_work_handler
);
3819 work
->event
.event
= RDMA_CM_EVENT_ADDR_CHANGE
;
3820 atomic_inc(&id_priv
->refcount
);
3821 queue_work(cma_wq
, &work
->work
);
3827 static int cma_netdev_callback(struct notifier_block
*self
, unsigned long event
,
3830 struct net_device
*ndev
= netdev_notifier_info_to_dev(ptr
);
3831 struct cma_device
*cma_dev
;
3832 struct rdma_id_private
*id_priv
;
3833 int ret
= NOTIFY_DONE
;
3835 if (event
!= NETDEV_BONDING_FAILOVER
)
3838 if (!(ndev
->flags
& IFF_MASTER
) || !(ndev
->priv_flags
& IFF_BONDING
))
3842 list_for_each_entry(cma_dev
, &dev_list
, list
)
3843 list_for_each_entry(id_priv
, &cma_dev
->id_list
, list
) {
3844 ret
= cma_netdev_change(ndev
, id_priv
);
3850 mutex_unlock(&lock
);
3854 static struct notifier_block cma_nb
= {
3855 .notifier_call
= cma_netdev_callback
3858 static void cma_add_one(struct ib_device
*device
)
3860 struct cma_device
*cma_dev
;
3861 struct rdma_id_private
*id_priv
;
3863 cma_dev
= kmalloc(sizeof *cma_dev
, GFP_KERNEL
);
3867 cma_dev
->device
= device
;
3869 init_completion(&cma_dev
->comp
);
3870 atomic_set(&cma_dev
->refcount
, 1);
3871 INIT_LIST_HEAD(&cma_dev
->id_list
);
3872 ib_set_client_data(device
, &cma_client
, cma_dev
);
3875 list_add_tail(&cma_dev
->list
, &dev_list
);
3876 list_for_each_entry(id_priv
, &listen_any_list
, list
)
3877 cma_listen_on_dev(id_priv
, cma_dev
);
3878 mutex_unlock(&lock
);
3881 static int cma_remove_id_dev(struct rdma_id_private
*id_priv
)
3883 struct rdma_cm_event event
;
3884 enum rdma_cm_state state
;
3887 /* Record that we want to remove the device */
3888 state
= cma_exch(id_priv
, RDMA_CM_DEVICE_REMOVAL
);
3889 if (state
== RDMA_CM_DESTROYING
)
3892 cma_cancel_operation(id_priv
, state
);
3893 mutex_lock(&id_priv
->handler_mutex
);
3895 /* Check for destruction from another callback. */
3896 if (!cma_comp(id_priv
, RDMA_CM_DEVICE_REMOVAL
))
3899 memset(&event
, 0, sizeof event
);
3900 event
.event
= RDMA_CM_EVENT_DEVICE_REMOVAL
;
3901 ret
= id_priv
->id
.event_handler(&id_priv
->id
, &event
);
3903 mutex_unlock(&id_priv
->handler_mutex
);
3907 static void cma_process_remove(struct cma_device
*cma_dev
)
3909 struct rdma_id_private
*id_priv
;
3913 while (!list_empty(&cma_dev
->id_list
)) {
3914 id_priv
= list_entry(cma_dev
->id_list
.next
,
3915 struct rdma_id_private
, list
);
3917 list_del(&id_priv
->listen_list
);
3918 list_del_init(&id_priv
->list
);
3919 atomic_inc(&id_priv
->refcount
);
3920 mutex_unlock(&lock
);
3922 ret
= id_priv
->internal_id
? 1 : cma_remove_id_dev(id_priv
);
3923 cma_deref_id(id_priv
);
3925 rdma_destroy_id(&id_priv
->id
);
3929 mutex_unlock(&lock
);
3931 cma_deref_dev(cma_dev
);
3932 wait_for_completion(&cma_dev
->comp
);
3935 static void cma_remove_one(struct ib_device
*device
, void *client_data
)
3937 struct cma_device
*cma_dev
= client_data
;
3943 list_del(&cma_dev
->list
);
3944 mutex_unlock(&lock
);
3946 cma_process_remove(cma_dev
);
3950 static int cma_get_id_stats(struct sk_buff
*skb
, struct netlink_callback
*cb
)
3952 struct nlmsghdr
*nlh
;
3953 struct rdma_cm_id_stats
*id_stats
;
3954 struct rdma_id_private
*id_priv
;
3955 struct rdma_cm_id
*id
= NULL
;
3956 struct cma_device
*cma_dev
;
3957 int i_dev
= 0, i_id
= 0;
3960 * We export all of the IDs as a sequence of messages. Each
3961 * ID gets its own netlink message.
3965 list_for_each_entry(cma_dev
, &dev_list
, list
) {
3966 if (i_dev
< cb
->args
[0]) {
3972 list_for_each_entry(id_priv
, &cma_dev
->id_list
, list
) {
3973 if (i_id
< cb
->args
[1]) {
3978 id_stats
= ibnl_put_msg(skb
, &nlh
, cb
->nlh
->nlmsg_seq
,
3979 sizeof *id_stats
, RDMA_NL_RDMA_CM
,
3980 RDMA_NL_RDMA_CM_ID_STATS
,
3985 memset(id_stats
, 0, sizeof *id_stats
);
3987 id_stats
->node_type
= id
->route
.addr
.dev_addr
.dev_type
;
3988 id_stats
->port_num
= id
->port_num
;
3989 id_stats
->bound_dev_if
=
3990 id
->route
.addr
.dev_addr
.bound_dev_if
;
3992 if (ibnl_put_attr(skb
, nlh
,
3993 rdma_addr_size(cma_src_addr(id_priv
)),
3994 cma_src_addr(id_priv
),
3995 RDMA_NL_RDMA_CM_ATTR_SRC_ADDR
))
3997 if (ibnl_put_attr(skb
, nlh
,
3998 rdma_addr_size(cma_src_addr(id_priv
)),
3999 cma_dst_addr(id_priv
),
4000 RDMA_NL_RDMA_CM_ATTR_DST_ADDR
))
4003 id_stats
->pid
= id_priv
->owner
;
4004 id_stats
->port_space
= id
->ps
;
4005 id_stats
->cm_state
= id_priv
->state
;
4006 id_stats
->qp_num
= id_priv
->qp_num
;
4007 id_stats
->qp_type
= id
->qp_type
;
4017 mutex_unlock(&lock
);
4018 cb
->args
[0] = i_dev
;
4024 static const struct ibnl_client_cbs cma_cb_table
[] = {
4025 [RDMA_NL_RDMA_CM_ID_STATS
] = { .dump
= cma_get_id_stats
,
4026 .module
= THIS_MODULE
},
4029 static int cma_init_net(struct net
*net
)
4031 struct cma_pernet
*pernet
= cma_pernet(net
);
4033 idr_init(&pernet
->tcp_ps
);
4034 idr_init(&pernet
->udp_ps
);
4035 idr_init(&pernet
->ipoib_ps
);
4036 idr_init(&pernet
->ib_ps
);
4041 static void cma_exit_net(struct net
*net
)
4043 struct cma_pernet
*pernet
= cma_pernet(net
);
4045 idr_destroy(&pernet
->tcp_ps
);
4046 idr_destroy(&pernet
->udp_ps
);
4047 idr_destroy(&pernet
->ipoib_ps
);
4048 idr_destroy(&pernet
->ib_ps
);
4051 static struct pernet_operations cma_pernet_operations
= {
4052 .init
= cma_init_net
,
4053 .exit
= cma_exit_net
,
4054 .id
= &cma_pernet_id
,
4055 .size
= sizeof(struct cma_pernet
),
4058 static int __init
cma_init(void)
4062 cma_wq
= create_singlethread_workqueue("rdma_cm");
4066 ret
= register_pernet_subsys(&cma_pernet_operations
);
4070 ib_sa_register_client(&sa_client
);
4071 rdma_addr_register_client(&addr_client
);
4072 register_netdevice_notifier(&cma_nb
);
4074 ret
= ib_register_client(&cma_client
);
4078 if (ibnl_add_client(RDMA_NL_RDMA_CM
, RDMA_NL_RDMA_CM_NUM_OPS
, cma_cb_table
))
4079 printk(KERN_WARNING
"RDMA CMA: failed to add netlink callback\n");
4084 unregister_netdevice_notifier(&cma_nb
);
4085 rdma_addr_unregister_client(&addr_client
);
4086 ib_sa_unregister_client(&sa_client
);
4088 destroy_workqueue(cma_wq
);
4092 static void __exit
cma_cleanup(void)
4094 ibnl_remove_client(RDMA_NL_RDMA_CM
);
4095 ib_unregister_client(&cma_client
);
4096 unregister_netdevice_notifier(&cma_nb
);
4097 rdma_addr_unregister_client(&addr_client
);
4098 ib_sa_unregister_client(&sa_client
);
4099 unregister_pernet_subsys(&cma_pernet_operations
);
4100 destroy_workqueue(cma_wq
);
4103 module_init(cma_init
);
4104 module_exit(cma_cleanup
);