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>
48 #include <rdma/rdma_cm.h>
49 #include <rdma/rdma_cm_ib.h>
50 #include <rdma/rdma_netlink.h>
51 #include <rdma/ib_cache.h>
52 #include <rdma/ib_cm.h>
53 #include <rdma/ib_sa.h>
54 #include <rdma/iw_cm.h>
56 MODULE_AUTHOR("Sean Hefty");
57 MODULE_DESCRIPTION("Generic RDMA CM Agent");
58 MODULE_LICENSE("Dual BSD/GPL");
60 #define CMA_CM_RESPONSE_TIMEOUT 20
61 #define CMA_MAX_CM_RETRIES 15
62 #define CMA_CM_MRA_SETTING (IB_CM_MRA_FLAG_DELAY | 24)
63 #define CMA_IBOE_PACKET_LIFETIME 18
65 static void cma_add_one(struct ib_device
*device
);
66 static void cma_remove_one(struct ib_device
*device
);
68 static struct ib_client cma_client
= {
71 .remove
= cma_remove_one
74 static struct ib_sa_client sa_client
;
75 static struct rdma_addr_client addr_client
;
76 static LIST_HEAD(dev_list
);
77 static LIST_HEAD(listen_any_list
);
78 static DEFINE_MUTEX(lock
);
79 static struct workqueue_struct
*cma_wq
;
80 static DEFINE_IDR(sdp_ps
);
81 static DEFINE_IDR(tcp_ps
);
82 static DEFINE_IDR(udp_ps
);
83 static DEFINE_IDR(ipoib_ps
);
86 struct list_head list
;
87 struct ib_device
*device
;
88 struct completion comp
;
90 struct list_head id_list
;
93 struct rdma_bind_list
{
95 struct hlist_head owners
;
100 * Device removal can occur at anytime, so we need extra handling to
101 * serialize notifying the user of device removal with other callbacks.
102 * We do this by disabling removal notification while a callback is in process,
103 * and reporting it after the callback completes.
105 struct rdma_id_private
{
106 struct rdma_cm_id id
;
108 struct rdma_bind_list
*bind_list
;
109 struct hlist_node node
;
110 struct list_head list
; /* listen_any_list or cma_device.list */
111 struct list_head listen_list
; /* per device listens */
112 struct cma_device
*cma_dev
;
113 struct list_head mc_list
;
116 enum rdma_cm_state state
;
118 struct mutex qp_mutex
;
120 struct completion comp
;
122 struct mutex handler_mutex
;
126 struct ib_sa_query
*query
;
142 struct cma_multicast
{
143 struct rdma_id_private
*id_priv
;
145 struct ib_sa_multicast
*ib
;
147 struct list_head list
;
149 struct sockaddr_storage addr
;
154 struct work_struct work
;
155 struct rdma_id_private
*id
;
156 enum rdma_cm_state old_state
;
157 enum rdma_cm_state new_state
;
158 struct rdma_cm_event event
;
161 struct cma_ndev_work
{
162 struct work_struct work
;
163 struct rdma_id_private
*id
;
164 struct rdma_cm_event event
;
167 struct iboe_mcast_work
{
168 struct work_struct work
;
169 struct rdma_id_private
*id
;
170 struct cma_multicast
*mc
;
183 u8 ip_version
; /* IP version: 7:4 */
185 union cma_ip_addr src_addr
;
186 union cma_ip_addr dst_addr
;
191 u8 sdp_version
; /* Major version: 7:4 */
192 u8 ip_version
; /* IP version: 7:4 */
193 u8 sdp_specific1
[10];
195 __be16 sdp_specific2
;
196 union cma_ip_addr src_addr
;
197 union cma_ip_addr dst_addr
;
205 #define CMA_VERSION 0x00
206 #define SDP_MAJ_VERSION 0x2
208 static int cma_comp(struct rdma_id_private
*id_priv
, enum rdma_cm_state comp
)
213 spin_lock_irqsave(&id_priv
->lock
, flags
);
214 ret
= (id_priv
->state
== comp
);
215 spin_unlock_irqrestore(&id_priv
->lock
, flags
);
219 static int cma_comp_exch(struct rdma_id_private
*id_priv
,
220 enum rdma_cm_state comp
, enum rdma_cm_state exch
)
225 spin_lock_irqsave(&id_priv
->lock
, flags
);
226 if ((ret
= (id_priv
->state
== comp
)))
227 id_priv
->state
= exch
;
228 spin_unlock_irqrestore(&id_priv
->lock
, flags
);
232 static enum rdma_cm_state
cma_exch(struct rdma_id_private
*id_priv
,
233 enum rdma_cm_state exch
)
236 enum rdma_cm_state old
;
238 spin_lock_irqsave(&id_priv
->lock
, flags
);
239 old
= id_priv
->state
;
240 id_priv
->state
= exch
;
241 spin_unlock_irqrestore(&id_priv
->lock
, flags
);
245 static inline u8
cma_get_ip_ver(struct cma_hdr
*hdr
)
247 return hdr
->ip_version
>> 4;
250 static inline void cma_set_ip_ver(struct cma_hdr
*hdr
, u8 ip_ver
)
252 hdr
->ip_version
= (ip_ver
<< 4) | (hdr
->ip_version
& 0xF);
255 static inline u8
sdp_get_majv(u8 sdp_version
)
257 return sdp_version
>> 4;
260 static inline u8
sdp_get_ip_ver(struct sdp_hh
*hh
)
262 return hh
->ip_version
>> 4;
265 static inline void sdp_set_ip_ver(struct sdp_hh
*hh
, u8 ip_ver
)
267 hh
->ip_version
= (ip_ver
<< 4) | (hh
->ip_version
& 0xF);
270 static void cma_attach_to_dev(struct rdma_id_private
*id_priv
,
271 struct cma_device
*cma_dev
)
273 atomic_inc(&cma_dev
->refcount
);
274 id_priv
->cma_dev
= cma_dev
;
275 id_priv
->id
.device
= cma_dev
->device
;
276 id_priv
->id
.route
.addr
.dev_addr
.transport
=
277 rdma_node_get_transport(cma_dev
->device
->node_type
);
278 list_add_tail(&id_priv
->list
, &cma_dev
->id_list
);
281 static inline void cma_deref_dev(struct cma_device
*cma_dev
)
283 if (atomic_dec_and_test(&cma_dev
->refcount
))
284 complete(&cma_dev
->comp
);
287 static inline void release_mc(struct kref
*kref
)
289 struct cma_multicast
*mc
= container_of(kref
, struct cma_multicast
, mcref
);
291 kfree(mc
->multicast
.ib
);
295 static void cma_release_dev(struct rdma_id_private
*id_priv
)
298 list_del(&id_priv
->list
);
299 cma_deref_dev(id_priv
->cma_dev
);
300 id_priv
->cma_dev
= NULL
;
304 static int cma_set_qkey(struct rdma_id_private
*id_priv
)
306 struct ib_sa_mcmember_rec rec
;
312 switch (id_priv
->id
.ps
) {
314 id_priv
->qkey
= RDMA_UDP_QKEY
;
317 ib_addr_get_mgid(&id_priv
->id
.route
.addr
.dev_addr
, &rec
.mgid
);
318 ret
= ib_sa_get_mcmember_rec(id_priv
->id
.device
,
319 id_priv
->id
.port_num
, &rec
.mgid
,
322 id_priv
->qkey
= be32_to_cpu(rec
.qkey
);
330 static int find_gid_port(struct ib_device
*device
, union ib_gid
*gid
, u8 port_num
)
334 struct ib_port_attr props
;
337 err
= ib_query_port(device
, port_num
, &props
);
341 for (i
= 0; i
< props
.gid_tbl_len
; ++i
) {
342 err
= ib_query_gid(device
, port_num
, i
, &tmp
);
345 if (!memcmp(&tmp
, gid
, sizeof tmp
))
352 static int cma_acquire_dev(struct rdma_id_private
*id_priv
)
354 struct rdma_dev_addr
*dev_addr
= &id_priv
->id
.route
.addr
.dev_addr
;
355 struct cma_device
*cma_dev
;
356 union ib_gid gid
, iboe_gid
;
359 enum rdma_link_layer dev_ll
= dev_addr
->dev_type
== ARPHRD_INFINIBAND
?
360 IB_LINK_LAYER_INFINIBAND
: IB_LINK_LAYER_ETHERNET
;
362 if (dev_ll
!= IB_LINK_LAYER_INFINIBAND
&&
363 id_priv
->id
.ps
== RDMA_PS_IPOIB
)
367 iboe_addr_get_sgid(dev_addr
, &iboe_gid
);
368 memcpy(&gid
, dev_addr
->src_dev_addr
+
369 rdma_addr_gid_offset(dev_addr
), sizeof gid
);
370 list_for_each_entry(cma_dev
, &dev_list
, list
) {
371 for (port
= 1; port
<= cma_dev
->device
->phys_port_cnt
; ++port
) {
372 if (rdma_port_get_link_layer(cma_dev
->device
, port
) == dev_ll
) {
373 if (rdma_node_get_transport(cma_dev
->device
->node_type
) == RDMA_TRANSPORT_IB
&&
374 rdma_port_get_link_layer(cma_dev
->device
, port
) == IB_LINK_LAYER_ETHERNET
)
375 ret
= find_gid_port(cma_dev
->device
, &iboe_gid
, port
);
377 ret
= find_gid_port(cma_dev
->device
, &gid
, port
);
380 id_priv
->id
.port_num
= port
;
390 cma_attach_to_dev(id_priv
, cma_dev
);
396 static void cma_deref_id(struct rdma_id_private
*id_priv
)
398 if (atomic_dec_and_test(&id_priv
->refcount
))
399 complete(&id_priv
->comp
);
402 static int cma_disable_callback(struct rdma_id_private
*id_priv
,
403 enum rdma_cm_state state
)
405 mutex_lock(&id_priv
->handler_mutex
);
406 if (id_priv
->state
!= state
) {
407 mutex_unlock(&id_priv
->handler_mutex
);
413 struct rdma_cm_id
*rdma_create_id(rdma_cm_event_handler event_handler
,
414 void *context
, enum rdma_port_space ps
,
415 enum ib_qp_type qp_type
)
417 struct rdma_id_private
*id_priv
;
419 id_priv
= kzalloc(sizeof *id_priv
, GFP_KERNEL
);
421 return ERR_PTR(-ENOMEM
);
423 id_priv
->owner
= task_pid_nr(current
);
424 id_priv
->state
= RDMA_CM_IDLE
;
425 id_priv
->id
.context
= context
;
426 id_priv
->id
.event_handler
= event_handler
;
428 id_priv
->id
.qp_type
= qp_type
;
429 spin_lock_init(&id_priv
->lock
);
430 mutex_init(&id_priv
->qp_mutex
);
431 init_completion(&id_priv
->comp
);
432 atomic_set(&id_priv
->refcount
, 1);
433 mutex_init(&id_priv
->handler_mutex
);
434 INIT_LIST_HEAD(&id_priv
->listen_list
);
435 INIT_LIST_HEAD(&id_priv
->mc_list
);
436 get_random_bytes(&id_priv
->seq_num
, sizeof id_priv
->seq_num
);
440 EXPORT_SYMBOL(rdma_create_id
);
442 static int cma_init_ud_qp(struct rdma_id_private
*id_priv
, struct ib_qp
*qp
)
444 struct ib_qp_attr qp_attr
;
445 int qp_attr_mask
, ret
;
447 qp_attr
.qp_state
= IB_QPS_INIT
;
448 ret
= rdma_init_qp_attr(&id_priv
->id
, &qp_attr
, &qp_attr_mask
);
452 ret
= ib_modify_qp(qp
, &qp_attr
, qp_attr_mask
);
456 qp_attr
.qp_state
= IB_QPS_RTR
;
457 ret
= ib_modify_qp(qp
, &qp_attr
, IB_QP_STATE
);
461 qp_attr
.qp_state
= IB_QPS_RTS
;
463 ret
= ib_modify_qp(qp
, &qp_attr
, IB_QP_STATE
| IB_QP_SQ_PSN
);
468 static int cma_init_conn_qp(struct rdma_id_private
*id_priv
, struct ib_qp
*qp
)
470 struct ib_qp_attr qp_attr
;
471 int qp_attr_mask
, ret
;
473 qp_attr
.qp_state
= IB_QPS_INIT
;
474 ret
= rdma_init_qp_attr(&id_priv
->id
, &qp_attr
, &qp_attr_mask
);
478 return ib_modify_qp(qp
, &qp_attr
, qp_attr_mask
);
481 int rdma_create_qp(struct rdma_cm_id
*id
, struct ib_pd
*pd
,
482 struct ib_qp_init_attr
*qp_init_attr
)
484 struct rdma_id_private
*id_priv
;
488 id_priv
= container_of(id
, struct rdma_id_private
, id
);
489 if (id
->device
!= pd
->device
)
492 qp
= ib_create_qp(pd
, qp_init_attr
);
496 if (id
->qp_type
== IB_QPT_UD
)
497 ret
= cma_init_ud_qp(id_priv
, qp
);
499 ret
= cma_init_conn_qp(id_priv
, qp
);
504 id_priv
->qp_num
= qp
->qp_num
;
505 id_priv
->srq
= (qp
->srq
!= NULL
);
511 EXPORT_SYMBOL(rdma_create_qp
);
513 void rdma_destroy_qp(struct rdma_cm_id
*id
)
515 struct rdma_id_private
*id_priv
;
517 id_priv
= container_of(id
, struct rdma_id_private
, id
);
518 mutex_lock(&id_priv
->qp_mutex
);
519 ib_destroy_qp(id_priv
->id
.qp
);
520 id_priv
->id
.qp
= NULL
;
521 mutex_unlock(&id_priv
->qp_mutex
);
523 EXPORT_SYMBOL(rdma_destroy_qp
);
525 static int cma_modify_qp_rtr(struct rdma_id_private
*id_priv
,
526 struct rdma_conn_param
*conn_param
)
528 struct ib_qp_attr qp_attr
;
529 int qp_attr_mask
, ret
;
531 mutex_lock(&id_priv
->qp_mutex
);
532 if (!id_priv
->id
.qp
) {
537 /* Need to update QP attributes from default values. */
538 qp_attr
.qp_state
= IB_QPS_INIT
;
539 ret
= rdma_init_qp_attr(&id_priv
->id
, &qp_attr
, &qp_attr_mask
);
543 ret
= ib_modify_qp(id_priv
->id
.qp
, &qp_attr
, qp_attr_mask
);
547 qp_attr
.qp_state
= IB_QPS_RTR
;
548 ret
= rdma_init_qp_attr(&id_priv
->id
, &qp_attr
, &qp_attr_mask
);
553 qp_attr
.max_dest_rd_atomic
= conn_param
->responder_resources
;
554 ret
= ib_modify_qp(id_priv
->id
.qp
, &qp_attr
, qp_attr_mask
);
556 mutex_unlock(&id_priv
->qp_mutex
);
560 static int cma_modify_qp_rts(struct rdma_id_private
*id_priv
,
561 struct rdma_conn_param
*conn_param
)
563 struct ib_qp_attr qp_attr
;
564 int qp_attr_mask
, ret
;
566 mutex_lock(&id_priv
->qp_mutex
);
567 if (!id_priv
->id
.qp
) {
572 qp_attr
.qp_state
= IB_QPS_RTS
;
573 ret
= rdma_init_qp_attr(&id_priv
->id
, &qp_attr
, &qp_attr_mask
);
578 qp_attr
.max_rd_atomic
= conn_param
->initiator_depth
;
579 ret
= ib_modify_qp(id_priv
->id
.qp
, &qp_attr
, qp_attr_mask
);
581 mutex_unlock(&id_priv
->qp_mutex
);
585 static int cma_modify_qp_err(struct rdma_id_private
*id_priv
)
587 struct ib_qp_attr qp_attr
;
590 mutex_lock(&id_priv
->qp_mutex
);
591 if (!id_priv
->id
.qp
) {
596 qp_attr
.qp_state
= IB_QPS_ERR
;
597 ret
= ib_modify_qp(id_priv
->id
.qp
, &qp_attr
, IB_QP_STATE
);
599 mutex_unlock(&id_priv
->qp_mutex
);
603 static int cma_ib_init_qp_attr(struct rdma_id_private
*id_priv
,
604 struct ib_qp_attr
*qp_attr
, int *qp_attr_mask
)
606 struct rdma_dev_addr
*dev_addr
= &id_priv
->id
.route
.addr
.dev_addr
;
610 if (rdma_port_get_link_layer(id_priv
->id
.device
, id_priv
->id
.port_num
) ==
611 IB_LINK_LAYER_INFINIBAND
)
612 pkey
= ib_addr_get_pkey(dev_addr
);
616 ret
= ib_find_cached_pkey(id_priv
->id
.device
, id_priv
->id
.port_num
,
617 pkey
, &qp_attr
->pkey_index
);
621 qp_attr
->port_num
= id_priv
->id
.port_num
;
622 *qp_attr_mask
= IB_QP_STATE
| IB_QP_PKEY_INDEX
| IB_QP_PORT
;
624 if (id_priv
->id
.qp_type
== IB_QPT_UD
) {
625 ret
= cma_set_qkey(id_priv
);
629 qp_attr
->qkey
= id_priv
->qkey
;
630 *qp_attr_mask
|= IB_QP_QKEY
;
632 qp_attr
->qp_access_flags
= 0;
633 *qp_attr_mask
|= IB_QP_ACCESS_FLAGS
;
638 int rdma_init_qp_attr(struct rdma_cm_id
*id
, struct ib_qp_attr
*qp_attr
,
641 struct rdma_id_private
*id_priv
;
644 id_priv
= container_of(id
, struct rdma_id_private
, id
);
645 switch (rdma_node_get_transport(id_priv
->id
.device
->node_type
)) {
646 case RDMA_TRANSPORT_IB
:
647 if (!id_priv
->cm_id
.ib
|| (id_priv
->id
.qp_type
== IB_QPT_UD
))
648 ret
= cma_ib_init_qp_attr(id_priv
, qp_attr
, qp_attr_mask
);
650 ret
= ib_cm_init_qp_attr(id_priv
->cm_id
.ib
, qp_attr
,
652 if (qp_attr
->qp_state
== IB_QPS_RTR
)
653 qp_attr
->rq_psn
= id_priv
->seq_num
;
655 case RDMA_TRANSPORT_IWARP
:
656 if (!id_priv
->cm_id
.iw
) {
657 qp_attr
->qp_access_flags
= 0;
658 *qp_attr_mask
= IB_QP_STATE
| IB_QP_ACCESS_FLAGS
;
660 ret
= iw_cm_init_qp_attr(id_priv
->cm_id
.iw
, qp_attr
,
670 EXPORT_SYMBOL(rdma_init_qp_attr
);
672 static inline int cma_zero_addr(struct sockaddr
*addr
)
674 struct in6_addr
*ip6
;
676 if (addr
->sa_family
== AF_INET
)
677 return ipv4_is_zeronet(
678 ((struct sockaddr_in
*)addr
)->sin_addr
.s_addr
);
680 ip6
= &((struct sockaddr_in6
*) addr
)->sin6_addr
;
681 return (ip6
->s6_addr32
[0] | ip6
->s6_addr32
[1] |
682 ip6
->s6_addr32
[2] | ip6
->s6_addr32
[3]) == 0;
686 static inline int cma_loopback_addr(struct sockaddr
*addr
)
688 if (addr
->sa_family
== AF_INET
)
689 return ipv4_is_loopback(
690 ((struct sockaddr_in
*) addr
)->sin_addr
.s_addr
);
692 return ipv6_addr_loopback(
693 &((struct sockaddr_in6
*) addr
)->sin6_addr
);
696 static inline int cma_any_addr(struct sockaddr
*addr
)
698 return cma_zero_addr(addr
) || cma_loopback_addr(addr
);
701 static int cma_addr_cmp(struct sockaddr
*src
, struct sockaddr
*dst
)
703 if (src
->sa_family
!= dst
->sa_family
)
706 switch (src
->sa_family
) {
708 return ((struct sockaddr_in
*) src
)->sin_addr
.s_addr
!=
709 ((struct sockaddr_in
*) dst
)->sin_addr
.s_addr
;
711 return ipv6_addr_cmp(&((struct sockaddr_in6
*) src
)->sin6_addr
,
712 &((struct sockaddr_in6
*) dst
)->sin6_addr
);
716 static inline __be16
cma_port(struct sockaddr
*addr
)
718 if (addr
->sa_family
== AF_INET
)
719 return ((struct sockaddr_in
*) addr
)->sin_port
;
721 return ((struct sockaddr_in6
*) addr
)->sin6_port
;
724 static inline int cma_any_port(struct sockaddr
*addr
)
726 return !cma_port(addr
);
729 static int cma_get_net_info(void *hdr
, enum rdma_port_space ps
,
730 u8
*ip_ver
, __be16
*port
,
731 union cma_ip_addr
**src
, union cma_ip_addr
**dst
)
735 if (sdp_get_majv(((struct sdp_hh
*) hdr
)->sdp_version
) !=
739 *ip_ver
= sdp_get_ip_ver(hdr
);
740 *port
= ((struct sdp_hh
*) hdr
)->port
;
741 *src
= &((struct sdp_hh
*) hdr
)->src_addr
;
742 *dst
= &((struct sdp_hh
*) hdr
)->dst_addr
;
745 if (((struct cma_hdr
*) hdr
)->cma_version
!= CMA_VERSION
)
748 *ip_ver
= cma_get_ip_ver(hdr
);
749 *port
= ((struct cma_hdr
*) hdr
)->port
;
750 *src
= &((struct cma_hdr
*) hdr
)->src_addr
;
751 *dst
= &((struct cma_hdr
*) hdr
)->dst_addr
;
755 if (*ip_ver
!= 4 && *ip_ver
!= 6)
760 static void cma_save_net_info(struct rdma_addr
*addr
,
761 struct rdma_addr
*listen_addr
,
762 u8 ip_ver
, __be16 port
,
763 union cma_ip_addr
*src
, union cma_ip_addr
*dst
)
765 struct sockaddr_in
*listen4
, *ip4
;
766 struct sockaddr_in6
*listen6
, *ip6
;
770 listen4
= (struct sockaddr_in
*) &listen_addr
->src_addr
;
771 ip4
= (struct sockaddr_in
*) &addr
->src_addr
;
772 ip4
->sin_family
= listen4
->sin_family
;
773 ip4
->sin_addr
.s_addr
= dst
->ip4
.addr
;
774 ip4
->sin_port
= listen4
->sin_port
;
776 ip4
= (struct sockaddr_in
*) &addr
->dst_addr
;
777 ip4
->sin_family
= listen4
->sin_family
;
778 ip4
->sin_addr
.s_addr
= src
->ip4
.addr
;
779 ip4
->sin_port
= port
;
782 listen6
= (struct sockaddr_in6
*) &listen_addr
->src_addr
;
783 ip6
= (struct sockaddr_in6
*) &addr
->src_addr
;
784 ip6
->sin6_family
= listen6
->sin6_family
;
785 ip6
->sin6_addr
= dst
->ip6
;
786 ip6
->sin6_port
= listen6
->sin6_port
;
788 ip6
= (struct sockaddr_in6
*) &addr
->dst_addr
;
789 ip6
->sin6_family
= listen6
->sin6_family
;
790 ip6
->sin6_addr
= src
->ip6
;
791 ip6
->sin6_port
= port
;
798 static inline int cma_user_data_offset(enum rdma_port_space ps
)
804 return sizeof(struct cma_hdr
);
808 static void cma_cancel_route(struct rdma_id_private
*id_priv
)
810 switch (rdma_port_get_link_layer(id_priv
->id
.device
, id_priv
->id
.port_num
)) {
811 case IB_LINK_LAYER_INFINIBAND
:
813 ib_sa_cancel_query(id_priv
->query_id
, id_priv
->query
);
820 static void cma_cancel_listens(struct rdma_id_private
*id_priv
)
822 struct rdma_id_private
*dev_id_priv
;
825 * Remove from listen_any_list to prevent added devices from spawning
826 * additional listen requests.
829 list_del(&id_priv
->list
);
831 while (!list_empty(&id_priv
->listen_list
)) {
832 dev_id_priv
= list_entry(id_priv
->listen_list
.next
,
833 struct rdma_id_private
, listen_list
);
834 /* sync with device removal to avoid duplicate destruction */
835 list_del_init(&dev_id_priv
->list
);
836 list_del(&dev_id_priv
->listen_list
);
839 rdma_destroy_id(&dev_id_priv
->id
);
845 static void cma_cancel_operation(struct rdma_id_private
*id_priv
,
846 enum rdma_cm_state state
)
849 case RDMA_CM_ADDR_QUERY
:
850 rdma_addr_cancel(&id_priv
->id
.route
.addr
.dev_addr
);
852 case RDMA_CM_ROUTE_QUERY
:
853 cma_cancel_route(id_priv
);
856 if (cma_any_addr((struct sockaddr
*) &id_priv
->id
.route
.addr
.src_addr
)
857 && !id_priv
->cma_dev
)
858 cma_cancel_listens(id_priv
);
865 static void cma_release_port(struct rdma_id_private
*id_priv
)
867 struct rdma_bind_list
*bind_list
= id_priv
->bind_list
;
873 hlist_del(&id_priv
->node
);
874 if (hlist_empty(&bind_list
->owners
)) {
875 idr_remove(bind_list
->ps
, bind_list
->port
);
881 static void cma_leave_mc_groups(struct rdma_id_private
*id_priv
)
883 struct cma_multicast
*mc
;
885 while (!list_empty(&id_priv
->mc_list
)) {
886 mc
= container_of(id_priv
->mc_list
.next
,
887 struct cma_multicast
, list
);
889 switch (rdma_port_get_link_layer(id_priv
->cma_dev
->device
, id_priv
->id
.port_num
)) {
890 case IB_LINK_LAYER_INFINIBAND
:
891 ib_sa_free_multicast(mc
->multicast
.ib
);
894 case IB_LINK_LAYER_ETHERNET
:
895 kref_put(&mc
->mcref
, release_mc
);
903 void rdma_destroy_id(struct rdma_cm_id
*id
)
905 struct rdma_id_private
*id_priv
;
906 enum rdma_cm_state state
;
908 id_priv
= container_of(id
, struct rdma_id_private
, id
);
909 state
= cma_exch(id_priv
, RDMA_CM_DESTROYING
);
910 cma_cancel_operation(id_priv
, state
);
913 * Wait for any active callback to finish. New callbacks will find
914 * the id_priv state set to destroying and abort.
916 mutex_lock(&id_priv
->handler_mutex
);
917 mutex_unlock(&id_priv
->handler_mutex
);
919 if (id_priv
->cma_dev
) {
920 switch (rdma_node_get_transport(id_priv
->id
.device
->node_type
)) {
921 case RDMA_TRANSPORT_IB
:
922 if (id_priv
->cm_id
.ib
)
923 ib_destroy_cm_id(id_priv
->cm_id
.ib
);
925 case RDMA_TRANSPORT_IWARP
:
926 if (id_priv
->cm_id
.iw
)
927 iw_destroy_cm_id(id_priv
->cm_id
.iw
);
932 cma_leave_mc_groups(id_priv
);
933 cma_release_dev(id_priv
);
936 cma_release_port(id_priv
);
937 cma_deref_id(id_priv
);
938 wait_for_completion(&id_priv
->comp
);
940 if (id_priv
->internal_id
)
941 cma_deref_id(id_priv
->id
.context
);
943 kfree(id_priv
->id
.route
.path_rec
);
946 EXPORT_SYMBOL(rdma_destroy_id
);
948 static int cma_rep_recv(struct rdma_id_private
*id_priv
)
952 ret
= cma_modify_qp_rtr(id_priv
, NULL
);
956 ret
= cma_modify_qp_rts(id_priv
, NULL
);
960 ret
= ib_send_cm_rtu(id_priv
->cm_id
.ib
, NULL
, 0);
966 cma_modify_qp_err(id_priv
);
967 ib_send_cm_rej(id_priv
->cm_id
.ib
, IB_CM_REJ_CONSUMER_DEFINED
,
972 static int cma_verify_rep(struct rdma_id_private
*id_priv
, void *data
)
974 if (id_priv
->id
.ps
== RDMA_PS_SDP
&&
975 sdp_get_majv(((struct sdp_hah
*) data
)->sdp_version
) !=
982 static void cma_set_rep_event_data(struct rdma_cm_event
*event
,
983 struct ib_cm_rep_event_param
*rep_data
,
986 event
->param
.conn
.private_data
= private_data
;
987 event
->param
.conn
.private_data_len
= IB_CM_REP_PRIVATE_DATA_SIZE
;
988 event
->param
.conn
.responder_resources
= rep_data
->responder_resources
;
989 event
->param
.conn
.initiator_depth
= rep_data
->initiator_depth
;
990 event
->param
.conn
.flow_control
= rep_data
->flow_control
;
991 event
->param
.conn
.rnr_retry_count
= rep_data
->rnr_retry_count
;
992 event
->param
.conn
.srq
= rep_data
->srq
;
993 event
->param
.conn
.qp_num
= rep_data
->remote_qpn
;
996 static int cma_ib_handler(struct ib_cm_id
*cm_id
, struct ib_cm_event
*ib_event
)
998 struct rdma_id_private
*id_priv
= cm_id
->context
;
999 struct rdma_cm_event event
;
1002 if ((ib_event
->event
!= IB_CM_TIMEWAIT_EXIT
&&
1003 cma_disable_callback(id_priv
, RDMA_CM_CONNECT
)) ||
1004 (ib_event
->event
== IB_CM_TIMEWAIT_EXIT
&&
1005 cma_disable_callback(id_priv
, RDMA_CM_DISCONNECT
)))
1008 memset(&event
, 0, sizeof event
);
1009 switch (ib_event
->event
) {
1010 case IB_CM_REQ_ERROR
:
1011 case IB_CM_REP_ERROR
:
1012 event
.event
= RDMA_CM_EVENT_UNREACHABLE
;
1013 event
.status
= -ETIMEDOUT
;
1015 case IB_CM_REP_RECEIVED
:
1016 event
.status
= cma_verify_rep(id_priv
, ib_event
->private_data
);
1018 event
.event
= RDMA_CM_EVENT_CONNECT_ERROR
;
1019 else if (id_priv
->id
.qp
&& id_priv
->id
.ps
!= RDMA_PS_SDP
) {
1020 event
.status
= cma_rep_recv(id_priv
);
1021 event
.event
= event
.status
? RDMA_CM_EVENT_CONNECT_ERROR
:
1022 RDMA_CM_EVENT_ESTABLISHED
;
1024 event
.event
= RDMA_CM_EVENT_CONNECT_RESPONSE
;
1025 cma_set_rep_event_data(&event
, &ib_event
->param
.rep_rcvd
,
1026 ib_event
->private_data
);
1028 case IB_CM_RTU_RECEIVED
:
1029 case IB_CM_USER_ESTABLISHED
:
1030 event
.event
= RDMA_CM_EVENT_ESTABLISHED
;
1032 case IB_CM_DREQ_ERROR
:
1033 event
.status
= -ETIMEDOUT
; /* fall through */
1034 case IB_CM_DREQ_RECEIVED
:
1035 case IB_CM_DREP_RECEIVED
:
1036 if (!cma_comp_exch(id_priv
, RDMA_CM_CONNECT
,
1037 RDMA_CM_DISCONNECT
))
1039 event
.event
= RDMA_CM_EVENT_DISCONNECTED
;
1041 case IB_CM_TIMEWAIT_EXIT
:
1042 event
.event
= RDMA_CM_EVENT_TIMEWAIT_EXIT
;
1044 case IB_CM_MRA_RECEIVED
:
1047 case IB_CM_REJ_RECEIVED
:
1048 cma_modify_qp_err(id_priv
);
1049 event
.status
= ib_event
->param
.rej_rcvd
.reason
;
1050 event
.event
= RDMA_CM_EVENT_REJECTED
;
1051 event
.param
.conn
.private_data
= ib_event
->private_data
;
1052 event
.param
.conn
.private_data_len
= IB_CM_REJ_PRIVATE_DATA_SIZE
;
1055 printk(KERN_ERR
"RDMA CMA: unexpected IB CM event: %d\n",
1060 ret
= id_priv
->id
.event_handler(&id_priv
->id
, &event
);
1062 /* Destroy the CM ID by returning a non-zero value. */
1063 id_priv
->cm_id
.ib
= NULL
;
1064 cma_exch(id_priv
, RDMA_CM_DESTROYING
);
1065 mutex_unlock(&id_priv
->handler_mutex
);
1066 rdma_destroy_id(&id_priv
->id
);
1070 mutex_unlock(&id_priv
->handler_mutex
);
1074 static struct rdma_id_private
*cma_new_conn_id(struct rdma_cm_id
*listen_id
,
1075 struct ib_cm_event
*ib_event
)
1077 struct rdma_id_private
*id_priv
;
1078 struct rdma_cm_id
*id
;
1079 struct rdma_route
*rt
;
1080 union cma_ip_addr
*src
, *dst
;
1085 if (cma_get_net_info(ib_event
->private_data
, listen_id
->ps
,
1086 &ip_ver
, &port
, &src
, &dst
))
1089 id
= rdma_create_id(listen_id
->event_handler
, listen_id
->context
,
1090 listen_id
->ps
, ib_event
->param
.req_rcvd
.qp_type
);
1094 cma_save_net_info(&id
->route
.addr
, &listen_id
->route
.addr
,
1095 ip_ver
, port
, src
, dst
);
1098 rt
->num_paths
= ib_event
->param
.req_rcvd
.alternate_path
? 2 : 1;
1099 rt
->path_rec
= kmalloc(sizeof *rt
->path_rec
* rt
->num_paths
,
1104 rt
->path_rec
[0] = *ib_event
->param
.req_rcvd
.primary_path
;
1105 if (rt
->num_paths
== 2)
1106 rt
->path_rec
[1] = *ib_event
->param
.req_rcvd
.alternate_path
;
1108 if (cma_any_addr((struct sockaddr
*) &rt
->addr
.src_addr
)) {
1109 rt
->addr
.dev_addr
.dev_type
= ARPHRD_INFINIBAND
;
1110 rdma_addr_set_sgid(&rt
->addr
.dev_addr
, &rt
->path_rec
[0].sgid
);
1111 ib_addr_set_pkey(&rt
->addr
.dev_addr
, rt
->path_rec
[0].pkey
);
1113 ret
= rdma_translate_ip((struct sockaddr
*) &rt
->addr
.src_addr
,
1114 &rt
->addr
.dev_addr
);
1118 rdma_addr_set_dgid(&rt
->addr
.dev_addr
, &rt
->path_rec
[0].dgid
);
1120 id_priv
= container_of(id
, struct rdma_id_private
, id
);
1121 id_priv
->state
= RDMA_CM_CONNECT
;
1125 rdma_destroy_id(id
);
1129 static struct rdma_id_private
*cma_new_udp_id(struct rdma_cm_id
*listen_id
,
1130 struct ib_cm_event
*ib_event
)
1132 struct rdma_id_private
*id_priv
;
1133 struct rdma_cm_id
*id
;
1134 union cma_ip_addr
*src
, *dst
;
1139 id
= rdma_create_id(listen_id
->event_handler
, listen_id
->context
,
1140 listen_id
->ps
, IB_QPT_UD
);
1145 if (cma_get_net_info(ib_event
->private_data
, listen_id
->ps
,
1146 &ip_ver
, &port
, &src
, &dst
))
1149 cma_save_net_info(&id
->route
.addr
, &listen_id
->route
.addr
,
1150 ip_ver
, port
, src
, dst
);
1152 if (!cma_any_addr((struct sockaddr
*) &id
->route
.addr
.src_addr
)) {
1153 ret
= rdma_translate_ip((struct sockaddr
*) &id
->route
.addr
.src_addr
,
1154 &id
->route
.addr
.dev_addr
);
1159 id_priv
= container_of(id
, struct rdma_id_private
, id
);
1160 id_priv
->state
= RDMA_CM_CONNECT
;
1163 rdma_destroy_id(id
);
1167 static void cma_set_req_event_data(struct rdma_cm_event
*event
,
1168 struct ib_cm_req_event_param
*req_data
,
1169 void *private_data
, int offset
)
1171 event
->param
.conn
.private_data
= private_data
+ offset
;
1172 event
->param
.conn
.private_data_len
= IB_CM_REQ_PRIVATE_DATA_SIZE
- offset
;
1173 event
->param
.conn
.responder_resources
= req_data
->responder_resources
;
1174 event
->param
.conn
.initiator_depth
= req_data
->initiator_depth
;
1175 event
->param
.conn
.flow_control
= req_data
->flow_control
;
1176 event
->param
.conn
.retry_count
= req_data
->retry_count
;
1177 event
->param
.conn
.rnr_retry_count
= req_data
->rnr_retry_count
;
1178 event
->param
.conn
.srq
= req_data
->srq
;
1179 event
->param
.conn
.qp_num
= req_data
->remote_qpn
;
1182 static int cma_req_handler(struct ib_cm_id
*cm_id
, struct ib_cm_event
*ib_event
)
1184 struct rdma_id_private
*listen_id
, *conn_id
;
1185 struct rdma_cm_event event
;
1188 listen_id
= cm_id
->context
;
1189 if (cma_disable_callback(listen_id
, RDMA_CM_LISTEN
))
1190 return -ECONNABORTED
;
1192 memset(&event
, 0, sizeof event
);
1193 offset
= cma_user_data_offset(listen_id
->id
.ps
);
1194 event
.event
= RDMA_CM_EVENT_CONNECT_REQUEST
;
1195 if (listen_id
->id
.qp_type
== IB_QPT_UD
) {
1196 conn_id
= cma_new_udp_id(&listen_id
->id
, ib_event
);
1197 event
.param
.ud
.private_data
= ib_event
->private_data
+ offset
;
1198 event
.param
.ud
.private_data_len
=
1199 IB_CM_SIDR_REQ_PRIVATE_DATA_SIZE
- offset
;
1201 conn_id
= cma_new_conn_id(&listen_id
->id
, ib_event
);
1202 cma_set_req_event_data(&event
, &ib_event
->param
.req_rcvd
,
1203 ib_event
->private_data
, offset
);
1210 mutex_lock_nested(&conn_id
->handler_mutex
, SINGLE_DEPTH_NESTING
);
1211 ret
= cma_acquire_dev(conn_id
);
1213 goto release_conn_id
;
1215 conn_id
->cm_id
.ib
= cm_id
;
1216 cm_id
->context
= conn_id
;
1217 cm_id
->cm_handler
= cma_ib_handler
;
1220 * Protect against the user destroying conn_id from another thread
1221 * until we're done accessing it.
1223 atomic_inc(&conn_id
->refcount
);
1224 ret
= conn_id
->id
.event_handler(&conn_id
->id
, &event
);
1227 * Acquire mutex to prevent user executing rdma_destroy_id()
1228 * while we're accessing the cm_id.
1231 if (cma_comp(conn_id
, RDMA_CM_CONNECT
) && (conn_id
->id
.qp_type
!= IB_QPT_UD
))
1232 ib_send_cm_mra(cm_id
, CMA_CM_MRA_SETTING
, NULL
, 0);
1233 mutex_unlock(&lock
);
1234 mutex_unlock(&conn_id
->handler_mutex
);
1235 cma_deref_id(conn_id
);
1238 cma_deref_id(conn_id
);
1240 /* Destroy the CM ID by returning a non-zero value. */
1241 conn_id
->cm_id
.ib
= NULL
;
1244 cma_exch(conn_id
, RDMA_CM_DESTROYING
);
1245 mutex_unlock(&conn_id
->handler_mutex
);
1246 rdma_destroy_id(&conn_id
->id
);
1249 mutex_unlock(&listen_id
->handler_mutex
);
1253 static __be64
cma_get_service_id(enum rdma_port_space ps
, struct sockaddr
*addr
)
1255 return cpu_to_be64(((u64
)ps
<< 16) + be16_to_cpu(cma_port(addr
)));
1258 static void cma_set_compare_data(enum rdma_port_space ps
, struct sockaddr
*addr
,
1259 struct ib_cm_compare_data
*compare
)
1261 struct cma_hdr
*cma_data
, *cma_mask
;
1262 struct sdp_hh
*sdp_data
, *sdp_mask
;
1264 struct in6_addr ip6_addr
;
1266 memset(compare
, 0, sizeof *compare
);
1267 cma_data
= (void *) compare
->data
;
1268 cma_mask
= (void *) compare
->mask
;
1269 sdp_data
= (void *) compare
->data
;
1270 sdp_mask
= (void *) compare
->mask
;
1272 switch (addr
->sa_family
) {
1274 ip4_addr
= ((struct sockaddr_in
*) addr
)->sin_addr
.s_addr
;
1275 if (ps
== RDMA_PS_SDP
) {
1276 sdp_set_ip_ver(sdp_data
, 4);
1277 sdp_set_ip_ver(sdp_mask
, 0xF);
1278 sdp_data
->dst_addr
.ip4
.addr
= ip4_addr
;
1279 sdp_mask
->dst_addr
.ip4
.addr
= htonl(~0);
1281 cma_set_ip_ver(cma_data
, 4);
1282 cma_set_ip_ver(cma_mask
, 0xF);
1283 cma_data
->dst_addr
.ip4
.addr
= ip4_addr
;
1284 cma_mask
->dst_addr
.ip4
.addr
= htonl(~0);
1288 ip6_addr
= ((struct sockaddr_in6
*) addr
)->sin6_addr
;
1289 if (ps
== RDMA_PS_SDP
) {
1290 sdp_set_ip_ver(sdp_data
, 6);
1291 sdp_set_ip_ver(sdp_mask
, 0xF);
1292 sdp_data
->dst_addr
.ip6
= ip6_addr
;
1293 memset(&sdp_mask
->dst_addr
.ip6
, 0xFF,
1294 sizeof sdp_mask
->dst_addr
.ip6
);
1296 cma_set_ip_ver(cma_data
, 6);
1297 cma_set_ip_ver(cma_mask
, 0xF);
1298 cma_data
->dst_addr
.ip6
= ip6_addr
;
1299 memset(&cma_mask
->dst_addr
.ip6
, 0xFF,
1300 sizeof cma_mask
->dst_addr
.ip6
);
1308 static int cma_iw_handler(struct iw_cm_id
*iw_id
, struct iw_cm_event
*iw_event
)
1310 struct rdma_id_private
*id_priv
= iw_id
->context
;
1311 struct rdma_cm_event event
;
1312 struct sockaddr_in
*sin
;
1315 if (cma_disable_callback(id_priv
, RDMA_CM_CONNECT
))
1318 memset(&event
, 0, sizeof event
);
1319 switch (iw_event
->event
) {
1320 case IW_CM_EVENT_CLOSE
:
1321 event
.event
= RDMA_CM_EVENT_DISCONNECTED
;
1323 case IW_CM_EVENT_CONNECT_REPLY
:
1324 sin
= (struct sockaddr_in
*) &id_priv
->id
.route
.addr
.src_addr
;
1325 *sin
= iw_event
->local_addr
;
1326 sin
= (struct sockaddr_in
*) &id_priv
->id
.route
.addr
.dst_addr
;
1327 *sin
= iw_event
->remote_addr
;
1328 switch (iw_event
->status
) {
1330 event
.event
= RDMA_CM_EVENT_ESTABLISHED
;
1334 event
.event
= RDMA_CM_EVENT_REJECTED
;
1337 event
.event
= RDMA_CM_EVENT_UNREACHABLE
;
1340 event
.event
= RDMA_CM_EVENT_CONNECT_ERROR
;
1344 case IW_CM_EVENT_ESTABLISHED
:
1345 event
.event
= RDMA_CM_EVENT_ESTABLISHED
;
1351 event
.status
= iw_event
->status
;
1352 event
.param
.conn
.private_data
= iw_event
->private_data
;
1353 event
.param
.conn
.private_data_len
= iw_event
->private_data_len
;
1354 ret
= id_priv
->id
.event_handler(&id_priv
->id
, &event
);
1356 /* Destroy the CM ID by returning a non-zero value. */
1357 id_priv
->cm_id
.iw
= NULL
;
1358 cma_exch(id_priv
, RDMA_CM_DESTROYING
);
1359 mutex_unlock(&id_priv
->handler_mutex
);
1360 rdma_destroy_id(&id_priv
->id
);
1364 mutex_unlock(&id_priv
->handler_mutex
);
1368 static int iw_conn_req_handler(struct iw_cm_id
*cm_id
,
1369 struct iw_cm_event
*iw_event
)
1371 struct rdma_cm_id
*new_cm_id
;
1372 struct rdma_id_private
*listen_id
, *conn_id
;
1373 struct sockaddr_in
*sin
;
1374 struct net_device
*dev
= NULL
;
1375 struct rdma_cm_event event
;
1377 struct ib_device_attr attr
;
1379 listen_id
= cm_id
->context
;
1380 if (cma_disable_callback(listen_id
, RDMA_CM_LISTEN
))
1381 return -ECONNABORTED
;
1383 /* Create a new RDMA id for the new IW CM ID */
1384 new_cm_id
= rdma_create_id(listen_id
->id
.event_handler
,
1385 listen_id
->id
.context
,
1386 RDMA_PS_TCP
, IB_QPT_RC
);
1387 if (IS_ERR(new_cm_id
)) {
1391 conn_id
= container_of(new_cm_id
, struct rdma_id_private
, id
);
1392 mutex_lock_nested(&conn_id
->handler_mutex
, SINGLE_DEPTH_NESTING
);
1393 conn_id
->state
= RDMA_CM_CONNECT
;
1395 dev
= ip_dev_find(&init_net
, iw_event
->local_addr
.sin_addr
.s_addr
);
1397 ret
= -EADDRNOTAVAIL
;
1398 mutex_unlock(&conn_id
->handler_mutex
);
1399 rdma_destroy_id(new_cm_id
);
1402 ret
= rdma_copy_addr(&conn_id
->id
.route
.addr
.dev_addr
, dev
, NULL
);
1404 mutex_unlock(&conn_id
->handler_mutex
);
1405 rdma_destroy_id(new_cm_id
);
1409 ret
= cma_acquire_dev(conn_id
);
1411 mutex_unlock(&conn_id
->handler_mutex
);
1412 rdma_destroy_id(new_cm_id
);
1416 conn_id
->cm_id
.iw
= cm_id
;
1417 cm_id
->context
= conn_id
;
1418 cm_id
->cm_handler
= cma_iw_handler
;
1420 sin
= (struct sockaddr_in
*) &new_cm_id
->route
.addr
.src_addr
;
1421 *sin
= iw_event
->local_addr
;
1422 sin
= (struct sockaddr_in
*) &new_cm_id
->route
.addr
.dst_addr
;
1423 *sin
= iw_event
->remote_addr
;
1425 ret
= ib_query_device(conn_id
->id
.device
, &attr
);
1427 mutex_unlock(&conn_id
->handler_mutex
);
1428 rdma_destroy_id(new_cm_id
);
1432 memset(&event
, 0, sizeof event
);
1433 event
.event
= RDMA_CM_EVENT_CONNECT_REQUEST
;
1434 event
.param
.conn
.private_data
= iw_event
->private_data
;
1435 event
.param
.conn
.private_data_len
= iw_event
->private_data_len
;
1436 event
.param
.conn
.initiator_depth
= attr
.max_qp_init_rd_atom
;
1437 event
.param
.conn
.responder_resources
= attr
.max_qp_rd_atom
;
1440 * Protect against the user destroying conn_id from another thread
1441 * until we're done accessing it.
1443 atomic_inc(&conn_id
->refcount
);
1444 ret
= conn_id
->id
.event_handler(&conn_id
->id
, &event
);
1446 /* User wants to destroy the CM ID */
1447 conn_id
->cm_id
.iw
= NULL
;
1448 cma_exch(conn_id
, RDMA_CM_DESTROYING
);
1449 mutex_unlock(&conn_id
->handler_mutex
);
1450 cma_deref_id(conn_id
);
1451 rdma_destroy_id(&conn_id
->id
);
1455 mutex_unlock(&conn_id
->handler_mutex
);
1456 cma_deref_id(conn_id
);
1461 mutex_unlock(&listen_id
->handler_mutex
);
1465 static int cma_ib_listen(struct rdma_id_private
*id_priv
)
1467 struct ib_cm_compare_data compare_data
;
1468 struct sockaddr
*addr
;
1469 struct ib_cm_id
*id
;
1473 id
= ib_create_cm_id(id_priv
->id
.device
, cma_req_handler
, id_priv
);
1477 id_priv
->cm_id
.ib
= id
;
1479 addr
= (struct sockaddr
*) &id_priv
->id
.route
.addr
.src_addr
;
1480 svc_id
= cma_get_service_id(id_priv
->id
.ps
, addr
);
1481 if (cma_any_addr(addr
))
1482 ret
= ib_cm_listen(id_priv
->cm_id
.ib
, svc_id
, 0, NULL
);
1484 cma_set_compare_data(id_priv
->id
.ps
, addr
, &compare_data
);
1485 ret
= ib_cm_listen(id_priv
->cm_id
.ib
, svc_id
, 0, &compare_data
);
1489 ib_destroy_cm_id(id_priv
->cm_id
.ib
);
1490 id_priv
->cm_id
.ib
= NULL
;
1496 static int cma_iw_listen(struct rdma_id_private
*id_priv
, int backlog
)
1499 struct sockaddr_in
*sin
;
1500 struct iw_cm_id
*id
;
1502 id
= iw_create_cm_id(id_priv
->id
.device
,
1503 iw_conn_req_handler
,
1508 id_priv
->cm_id
.iw
= id
;
1510 sin
= (struct sockaddr_in
*) &id_priv
->id
.route
.addr
.src_addr
;
1511 id_priv
->cm_id
.iw
->local_addr
= *sin
;
1513 ret
= iw_cm_listen(id_priv
->cm_id
.iw
, backlog
);
1516 iw_destroy_cm_id(id_priv
->cm_id
.iw
);
1517 id_priv
->cm_id
.iw
= NULL
;
1523 static int cma_listen_handler(struct rdma_cm_id
*id
,
1524 struct rdma_cm_event
*event
)
1526 struct rdma_id_private
*id_priv
= id
->context
;
1528 id
->context
= id_priv
->id
.context
;
1529 id
->event_handler
= id_priv
->id
.event_handler
;
1530 return id_priv
->id
.event_handler(id
, event
);
1533 static void cma_listen_on_dev(struct rdma_id_private
*id_priv
,
1534 struct cma_device
*cma_dev
)
1536 struct rdma_id_private
*dev_id_priv
;
1537 struct rdma_cm_id
*id
;
1540 id
= rdma_create_id(cma_listen_handler
, id_priv
, id_priv
->id
.ps
,
1541 id_priv
->id
.qp_type
);
1545 dev_id_priv
= container_of(id
, struct rdma_id_private
, id
);
1547 dev_id_priv
->state
= RDMA_CM_ADDR_BOUND
;
1548 memcpy(&id
->route
.addr
.src_addr
, &id_priv
->id
.route
.addr
.src_addr
,
1549 ip_addr_size((struct sockaddr
*) &id_priv
->id
.route
.addr
.src_addr
));
1551 cma_attach_to_dev(dev_id_priv
, cma_dev
);
1552 list_add_tail(&dev_id_priv
->listen_list
, &id_priv
->listen_list
);
1553 atomic_inc(&id_priv
->refcount
);
1554 dev_id_priv
->internal_id
= 1;
1556 ret
= rdma_listen(id
, id_priv
->backlog
);
1558 printk(KERN_WARNING
"RDMA CMA: cma_listen_on_dev, error %d, "
1559 "listening on device %s\n", ret
, cma_dev
->device
->name
);
1562 static void cma_listen_on_all(struct rdma_id_private
*id_priv
)
1564 struct cma_device
*cma_dev
;
1567 list_add_tail(&id_priv
->list
, &listen_any_list
);
1568 list_for_each_entry(cma_dev
, &dev_list
, list
)
1569 cma_listen_on_dev(id_priv
, cma_dev
);
1570 mutex_unlock(&lock
);
1573 void rdma_set_service_type(struct rdma_cm_id
*id
, int tos
)
1575 struct rdma_id_private
*id_priv
;
1577 id_priv
= container_of(id
, struct rdma_id_private
, id
);
1578 id_priv
->tos
= (u8
) tos
;
1580 EXPORT_SYMBOL(rdma_set_service_type
);
1582 static void cma_query_handler(int status
, struct ib_sa_path_rec
*path_rec
,
1585 struct cma_work
*work
= context
;
1586 struct rdma_route
*route
;
1588 route
= &work
->id
->id
.route
;
1591 route
->num_paths
= 1;
1592 *route
->path_rec
= *path_rec
;
1594 work
->old_state
= RDMA_CM_ROUTE_QUERY
;
1595 work
->new_state
= RDMA_CM_ADDR_RESOLVED
;
1596 work
->event
.event
= RDMA_CM_EVENT_ROUTE_ERROR
;
1597 work
->event
.status
= status
;
1600 queue_work(cma_wq
, &work
->work
);
1603 static int cma_query_ib_route(struct rdma_id_private
*id_priv
, int timeout_ms
,
1604 struct cma_work
*work
)
1606 struct rdma_addr
*addr
= &id_priv
->id
.route
.addr
;
1607 struct ib_sa_path_rec path_rec
;
1608 ib_sa_comp_mask comp_mask
;
1609 struct sockaddr_in6
*sin6
;
1611 memset(&path_rec
, 0, sizeof path_rec
);
1612 rdma_addr_get_sgid(&addr
->dev_addr
, &path_rec
.sgid
);
1613 rdma_addr_get_dgid(&addr
->dev_addr
, &path_rec
.dgid
);
1614 path_rec
.pkey
= cpu_to_be16(ib_addr_get_pkey(&addr
->dev_addr
));
1615 path_rec
.numb_path
= 1;
1616 path_rec
.reversible
= 1;
1617 path_rec
.service_id
= cma_get_service_id(id_priv
->id
.ps
,
1618 (struct sockaddr
*) &addr
->dst_addr
);
1620 comp_mask
= IB_SA_PATH_REC_DGID
| IB_SA_PATH_REC_SGID
|
1621 IB_SA_PATH_REC_PKEY
| IB_SA_PATH_REC_NUMB_PATH
|
1622 IB_SA_PATH_REC_REVERSIBLE
| IB_SA_PATH_REC_SERVICE_ID
;
1624 if (addr
->src_addr
.ss_family
== AF_INET
) {
1625 path_rec
.qos_class
= cpu_to_be16((u16
) id_priv
->tos
);
1626 comp_mask
|= IB_SA_PATH_REC_QOS_CLASS
;
1628 sin6
= (struct sockaddr_in6
*) &addr
->src_addr
;
1629 path_rec
.traffic_class
= (u8
) (be32_to_cpu(sin6
->sin6_flowinfo
) >> 20);
1630 comp_mask
|= IB_SA_PATH_REC_TRAFFIC_CLASS
;
1633 id_priv
->query_id
= ib_sa_path_rec_get(&sa_client
, id_priv
->id
.device
,
1634 id_priv
->id
.port_num
, &path_rec
,
1635 comp_mask
, timeout_ms
,
1636 GFP_KERNEL
, cma_query_handler
,
1637 work
, &id_priv
->query
);
1639 return (id_priv
->query_id
< 0) ? id_priv
->query_id
: 0;
1642 static void cma_work_handler(struct work_struct
*_work
)
1644 struct cma_work
*work
= container_of(_work
, struct cma_work
, work
);
1645 struct rdma_id_private
*id_priv
= work
->id
;
1648 mutex_lock(&id_priv
->handler_mutex
);
1649 if (!cma_comp_exch(id_priv
, work
->old_state
, work
->new_state
))
1652 if (id_priv
->id
.event_handler(&id_priv
->id
, &work
->event
)) {
1653 cma_exch(id_priv
, RDMA_CM_DESTROYING
);
1657 mutex_unlock(&id_priv
->handler_mutex
);
1658 cma_deref_id(id_priv
);
1660 rdma_destroy_id(&id_priv
->id
);
1664 static void cma_ndev_work_handler(struct work_struct
*_work
)
1666 struct cma_ndev_work
*work
= container_of(_work
, struct cma_ndev_work
, work
);
1667 struct rdma_id_private
*id_priv
= work
->id
;
1670 mutex_lock(&id_priv
->handler_mutex
);
1671 if (id_priv
->state
== RDMA_CM_DESTROYING
||
1672 id_priv
->state
== RDMA_CM_DEVICE_REMOVAL
)
1675 if (id_priv
->id
.event_handler(&id_priv
->id
, &work
->event
)) {
1676 cma_exch(id_priv
, RDMA_CM_DESTROYING
);
1681 mutex_unlock(&id_priv
->handler_mutex
);
1682 cma_deref_id(id_priv
);
1684 rdma_destroy_id(&id_priv
->id
);
1688 static int cma_resolve_ib_route(struct rdma_id_private
*id_priv
, int timeout_ms
)
1690 struct rdma_route
*route
= &id_priv
->id
.route
;
1691 struct cma_work
*work
;
1694 work
= kzalloc(sizeof *work
, GFP_KERNEL
);
1699 INIT_WORK(&work
->work
, cma_work_handler
);
1700 work
->old_state
= RDMA_CM_ROUTE_QUERY
;
1701 work
->new_state
= RDMA_CM_ROUTE_RESOLVED
;
1702 work
->event
.event
= RDMA_CM_EVENT_ROUTE_RESOLVED
;
1704 route
->path_rec
= kmalloc(sizeof *route
->path_rec
, GFP_KERNEL
);
1705 if (!route
->path_rec
) {
1710 ret
= cma_query_ib_route(id_priv
, timeout_ms
, work
);
1716 kfree(route
->path_rec
);
1717 route
->path_rec
= NULL
;
1723 int rdma_set_ib_paths(struct rdma_cm_id
*id
,
1724 struct ib_sa_path_rec
*path_rec
, int num_paths
)
1726 struct rdma_id_private
*id_priv
;
1729 id_priv
= container_of(id
, struct rdma_id_private
, id
);
1730 if (!cma_comp_exch(id_priv
, RDMA_CM_ADDR_RESOLVED
,
1731 RDMA_CM_ROUTE_RESOLVED
))
1734 id
->route
.path_rec
= kmemdup(path_rec
, sizeof *path_rec
* num_paths
,
1736 if (!id
->route
.path_rec
) {
1741 id
->route
.num_paths
= num_paths
;
1744 cma_comp_exch(id_priv
, RDMA_CM_ROUTE_RESOLVED
, RDMA_CM_ADDR_RESOLVED
);
1747 EXPORT_SYMBOL(rdma_set_ib_paths
);
1749 static int cma_resolve_iw_route(struct rdma_id_private
*id_priv
, int timeout_ms
)
1751 struct cma_work
*work
;
1753 work
= kzalloc(sizeof *work
, GFP_KERNEL
);
1758 INIT_WORK(&work
->work
, cma_work_handler
);
1759 work
->old_state
= RDMA_CM_ROUTE_QUERY
;
1760 work
->new_state
= RDMA_CM_ROUTE_RESOLVED
;
1761 work
->event
.event
= RDMA_CM_EVENT_ROUTE_RESOLVED
;
1762 queue_work(cma_wq
, &work
->work
);
1766 static int cma_resolve_iboe_route(struct rdma_id_private
*id_priv
)
1768 struct rdma_route
*route
= &id_priv
->id
.route
;
1769 struct rdma_addr
*addr
= &route
->addr
;
1770 struct cma_work
*work
;
1772 struct sockaddr_in
*src_addr
= (struct sockaddr_in
*)&route
->addr
.src_addr
;
1773 struct sockaddr_in
*dst_addr
= (struct sockaddr_in
*)&route
->addr
.dst_addr
;
1774 struct net_device
*ndev
= NULL
;
1777 if (src_addr
->sin_family
!= dst_addr
->sin_family
)
1780 work
= kzalloc(sizeof *work
, GFP_KERNEL
);
1785 INIT_WORK(&work
->work
, cma_work_handler
);
1787 route
->path_rec
= kzalloc(sizeof *route
->path_rec
, GFP_KERNEL
);
1788 if (!route
->path_rec
) {
1793 route
->num_paths
= 1;
1795 if (addr
->dev_addr
.bound_dev_if
)
1796 ndev
= dev_get_by_index(&init_net
, addr
->dev_addr
.bound_dev_if
);
1802 vid
= rdma_vlan_dev_vlan_id(ndev
);
1804 iboe_mac_vlan_to_ll(&route
->path_rec
->sgid
, addr
->dev_addr
.src_dev_addr
, vid
);
1805 iboe_mac_vlan_to_ll(&route
->path_rec
->dgid
, addr
->dev_addr
.dst_dev_addr
, vid
);
1807 route
->path_rec
->hop_limit
= 1;
1808 route
->path_rec
->reversible
= 1;
1809 route
->path_rec
->pkey
= cpu_to_be16(0xffff);
1810 route
->path_rec
->mtu_selector
= IB_SA_EQ
;
1811 route
->path_rec
->sl
= id_priv
->tos
>> 5;
1813 route
->path_rec
->mtu
= iboe_get_mtu(ndev
->mtu
);
1814 route
->path_rec
->rate_selector
= IB_SA_EQ
;
1815 route
->path_rec
->rate
= iboe_get_rate(ndev
);
1817 route
->path_rec
->packet_life_time_selector
= IB_SA_EQ
;
1818 route
->path_rec
->packet_life_time
= CMA_IBOE_PACKET_LIFETIME
;
1819 if (!route
->path_rec
->mtu
) {
1824 work
->old_state
= RDMA_CM_ROUTE_QUERY
;
1825 work
->new_state
= RDMA_CM_ROUTE_RESOLVED
;
1826 work
->event
.event
= RDMA_CM_EVENT_ROUTE_RESOLVED
;
1827 work
->event
.status
= 0;
1829 queue_work(cma_wq
, &work
->work
);
1834 kfree(route
->path_rec
);
1835 route
->path_rec
= NULL
;
1841 int rdma_resolve_route(struct rdma_cm_id
*id
, int timeout_ms
)
1843 struct rdma_id_private
*id_priv
;
1846 id_priv
= container_of(id
, struct rdma_id_private
, id
);
1847 if (!cma_comp_exch(id_priv
, RDMA_CM_ADDR_RESOLVED
, RDMA_CM_ROUTE_QUERY
))
1850 atomic_inc(&id_priv
->refcount
);
1851 switch (rdma_node_get_transport(id
->device
->node_type
)) {
1852 case RDMA_TRANSPORT_IB
:
1853 switch (rdma_port_get_link_layer(id
->device
, id
->port_num
)) {
1854 case IB_LINK_LAYER_INFINIBAND
:
1855 ret
= cma_resolve_ib_route(id_priv
, timeout_ms
);
1857 case IB_LINK_LAYER_ETHERNET
:
1858 ret
= cma_resolve_iboe_route(id_priv
);
1864 case RDMA_TRANSPORT_IWARP
:
1865 ret
= cma_resolve_iw_route(id_priv
, timeout_ms
);
1876 cma_comp_exch(id_priv
, RDMA_CM_ROUTE_QUERY
, RDMA_CM_ADDR_RESOLVED
);
1877 cma_deref_id(id_priv
);
1880 EXPORT_SYMBOL(rdma_resolve_route
);
1882 static int cma_bind_loopback(struct rdma_id_private
*id_priv
)
1884 struct cma_device
*cma_dev
;
1885 struct ib_port_attr port_attr
;
1892 if (list_empty(&dev_list
)) {
1896 list_for_each_entry(cma_dev
, &dev_list
, list
)
1897 for (p
= 1; p
<= cma_dev
->device
->phys_port_cnt
; ++p
)
1898 if (!ib_query_port(cma_dev
->device
, p
, &port_attr
) &&
1899 port_attr
.state
== IB_PORT_ACTIVE
)
1903 cma_dev
= list_entry(dev_list
.next
, struct cma_device
, list
);
1906 ret
= ib_get_cached_gid(cma_dev
->device
, p
, 0, &gid
);
1910 ret
= ib_get_cached_pkey(cma_dev
->device
, p
, 0, &pkey
);
1914 id_priv
->id
.route
.addr
.dev_addr
.dev_type
=
1915 (rdma_port_get_link_layer(cma_dev
->device
, p
) == IB_LINK_LAYER_INFINIBAND
) ?
1916 ARPHRD_INFINIBAND
: ARPHRD_ETHER
;
1918 rdma_addr_set_sgid(&id_priv
->id
.route
.addr
.dev_addr
, &gid
);
1919 ib_addr_set_pkey(&id_priv
->id
.route
.addr
.dev_addr
, pkey
);
1920 id_priv
->id
.port_num
= p
;
1921 cma_attach_to_dev(id_priv
, cma_dev
);
1923 mutex_unlock(&lock
);
1927 static void addr_handler(int status
, struct sockaddr
*src_addr
,
1928 struct rdma_dev_addr
*dev_addr
, void *context
)
1930 struct rdma_id_private
*id_priv
= context
;
1931 struct rdma_cm_event event
;
1933 memset(&event
, 0, sizeof event
);
1934 mutex_lock(&id_priv
->handler_mutex
);
1935 if (!cma_comp_exch(id_priv
, RDMA_CM_ADDR_QUERY
,
1936 RDMA_CM_ADDR_RESOLVED
))
1939 if (!status
&& !id_priv
->cma_dev
)
1940 status
= cma_acquire_dev(id_priv
);
1943 if (!cma_comp_exch(id_priv
, RDMA_CM_ADDR_RESOLVED
,
1944 RDMA_CM_ADDR_BOUND
))
1946 event
.event
= RDMA_CM_EVENT_ADDR_ERROR
;
1947 event
.status
= status
;
1949 memcpy(&id_priv
->id
.route
.addr
.src_addr
, src_addr
,
1950 ip_addr_size(src_addr
));
1951 event
.event
= RDMA_CM_EVENT_ADDR_RESOLVED
;
1954 if (id_priv
->id
.event_handler(&id_priv
->id
, &event
)) {
1955 cma_exch(id_priv
, RDMA_CM_DESTROYING
);
1956 mutex_unlock(&id_priv
->handler_mutex
);
1957 cma_deref_id(id_priv
);
1958 rdma_destroy_id(&id_priv
->id
);
1962 mutex_unlock(&id_priv
->handler_mutex
);
1963 cma_deref_id(id_priv
);
1966 static int cma_resolve_loopback(struct rdma_id_private
*id_priv
)
1968 struct cma_work
*work
;
1969 struct sockaddr
*src
, *dst
;
1973 work
= kzalloc(sizeof *work
, GFP_KERNEL
);
1977 if (!id_priv
->cma_dev
) {
1978 ret
= cma_bind_loopback(id_priv
);
1983 rdma_addr_get_sgid(&id_priv
->id
.route
.addr
.dev_addr
, &gid
);
1984 rdma_addr_set_dgid(&id_priv
->id
.route
.addr
.dev_addr
, &gid
);
1986 src
= (struct sockaddr
*) &id_priv
->id
.route
.addr
.src_addr
;
1987 if (cma_zero_addr(src
)) {
1988 dst
= (struct sockaddr
*) &id_priv
->id
.route
.addr
.dst_addr
;
1989 if ((src
->sa_family
= dst
->sa_family
) == AF_INET
) {
1990 ((struct sockaddr_in
*) src
)->sin_addr
.s_addr
=
1991 ((struct sockaddr_in
*) dst
)->sin_addr
.s_addr
;
1993 ipv6_addr_copy(&((struct sockaddr_in6
*) src
)->sin6_addr
,
1994 &((struct sockaddr_in6
*) dst
)->sin6_addr
);
1999 INIT_WORK(&work
->work
, cma_work_handler
);
2000 work
->old_state
= RDMA_CM_ADDR_QUERY
;
2001 work
->new_state
= RDMA_CM_ADDR_RESOLVED
;
2002 work
->event
.event
= RDMA_CM_EVENT_ADDR_RESOLVED
;
2003 queue_work(cma_wq
, &work
->work
);
2010 static int cma_bind_addr(struct rdma_cm_id
*id
, struct sockaddr
*src_addr
,
2011 struct sockaddr
*dst_addr
)
2013 if (!src_addr
|| !src_addr
->sa_family
) {
2014 src_addr
= (struct sockaddr
*) &id
->route
.addr
.src_addr
;
2015 if ((src_addr
->sa_family
= dst_addr
->sa_family
) == AF_INET6
) {
2016 ((struct sockaddr_in6
*) src_addr
)->sin6_scope_id
=
2017 ((struct sockaddr_in6
*) dst_addr
)->sin6_scope_id
;
2020 return rdma_bind_addr(id
, src_addr
);
2023 int rdma_resolve_addr(struct rdma_cm_id
*id
, struct sockaddr
*src_addr
,
2024 struct sockaddr
*dst_addr
, int timeout_ms
)
2026 struct rdma_id_private
*id_priv
;
2029 id_priv
= container_of(id
, struct rdma_id_private
, id
);
2030 if (id_priv
->state
== RDMA_CM_IDLE
) {
2031 ret
= cma_bind_addr(id
, src_addr
, dst_addr
);
2036 if (!cma_comp_exch(id_priv
, RDMA_CM_ADDR_BOUND
, RDMA_CM_ADDR_QUERY
))
2039 atomic_inc(&id_priv
->refcount
);
2040 memcpy(&id
->route
.addr
.dst_addr
, dst_addr
, ip_addr_size(dst_addr
));
2041 if (cma_any_addr(dst_addr
))
2042 ret
= cma_resolve_loopback(id_priv
);
2044 ret
= rdma_resolve_ip(&addr_client
, (struct sockaddr
*) &id
->route
.addr
.src_addr
,
2045 dst_addr
, &id
->route
.addr
.dev_addr
,
2046 timeout_ms
, addr_handler
, id_priv
);
2052 cma_comp_exch(id_priv
, RDMA_CM_ADDR_QUERY
, RDMA_CM_ADDR_BOUND
);
2053 cma_deref_id(id_priv
);
2056 EXPORT_SYMBOL(rdma_resolve_addr
);
2058 int rdma_set_reuseaddr(struct rdma_cm_id
*id
, int reuse
)
2060 struct rdma_id_private
*id_priv
;
2061 unsigned long flags
;
2064 id_priv
= container_of(id
, struct rdma_id_private
, id
);
2065 spin_lock_irqsave(&id_priv
->lock
, flags
);
2066 if (id_priv
->state
== RDMA_CM_IDLE
) {
2067 id_priv
->reuseaddr
= reuse
;
2072 spin_unlock_irqrestore(&id_priv
->lock
, flags
);
2075 EXPORT_SYMBOL(rdma_set_reuseaddr
);
2077 static void cma_bind_port(struct rdma_bind_list
*bind_list
,
2078 struct rdma_id_private
*id_priv
)
2080 struct sockaddr_in
*sin
;
2082 sin
= (struct sockaddr_in
*) &id_priv
->id
.route
.addr
.src_addr
;
2083 sin
->sin_port
= htons(bind_list
->port
);
2084 id_priv
->bind_list
= bind_list
;
2085 hlist_add_head(&id_priv
->node
, &bind_list
->owners
);
2088 static int cma_alloc_port(struct idr
*ps
, struct rdma_id_private
*id_priv
,
2089 unsigned short snum
)
2091 struct rdma_bind_list
*bind_list
;
2094 bind_list
= kzalloc(sizeof *bind_list
, GFP_KERNEL
);
2099 ret
= idr_get_new_above(ps
, bind_list
, snum
, &port
);
2100 } while ((ret
== -EAGAIN
) && idr_pre_get(ps
, GFP_KERNEL
));
2106 ret
= -EADDRNOTAVAIL
;
2111 bind_list
->port
= (unsigned short) port
;
2112 cma_bind_port(bind_list
, id_priv
);
2115 idr_remove(ps
, port
);
2121 static int cma_alloc_any_port(struct idr
*ps
, struct rdma_id_private
*id_priv
)
2123 static unsigned int last_used_port
;
2124 int low
, high
, remaining
;
2127 inet_get_local_port_range(&low
, &high
);
2128 remaining
= (high
- low
) + 1;
2129 rover
= net_random() % remaining
+ low
;
2131 if (last_used_port
!= rover
&&
2132 !idr_find(ps
, (unsigned short) rover
)) {
2133 int ret
= cma_alloc_port(ps
, id_priv
, rover
);
2135 * Remember previously used port number in order to avoid
2136 * re-using same port immediately after it is closed.
2139 last_used_port
= rover
;
2140 if (ret
!= -EADDRNOTAVAIL
)
2145 if ((rover
< low
) || (rover
> high
))
2149 return -EADDRNOTAVAIL
;
2153 * Check that the requested port is available. This is called when trying to
2154 * bind to a specific port, or when trying to listen on a bound port. In
2155 * the latter case, the provided id_priv may already be on the bind_list, but
2156 * we still need to check that it's okay to start listening.
2158 static int cma_check_port(struct rdma_bind_list
*bind_list
,
2159 struct rdma_id_private
*id_priv
, uint8_t reuseaddr
)
2161 struct rdma_id_private
*cur_id
;
2162 struct sockaddr
*addr
, *cur_addr
;
2163 struct hlist_node
*node
;
2165 addr
= (struct sockaddr
*) &id_priv
->id
.route
.addr
.src_addr
;
2166 if (cma_any_addr(addr
) && !reuseaddr
)
2167 return -EADDRNOTAVAIL
;
2169 hlist_for_each_entry(cur_id
, node
, &bind_list
->owners
, node
) {
2170 if (id_priv
== cur_id
)
2173 if ((cur_id
->state
== RDMA_CM_LISTEN
) ||
2174 !reuseaddr
|| !cur_id
->reuseaddr
) {
2175 cur_addr
= (struct sockaddr
*) &cur_id
->id
.route
.addr
.src_addr
;
2176 if (cma_any_addr(cur_addr
))
2177 return -EADDRNOTAVAIL
;
2179 if (!cma_addr_cmp(addr
, cur_addr
))
2186 static int cma_use_port(struct idr
*ps
, struct rdma_id_private
*id_priv
)
2188 struct rdma_bind_list
*bind_list
;
2189 unsigned short snum
;
2192 snum
= ntohs(cma_port((struct sockaddr
*) &id_priv
->id
.route
.addr
.src_addr
));
2193 if (snum
< PROT_SOCK
&& !capable(CAP_NET_BIND_SERVICE
))
2196 bind_list
= idr_find(ps
, snum
);
2198 ret
= cma_alloc_port(ps
, id_priv
, snum
);
2200 ret
= cma_check_port(bind_list
, id_priv
, id_priv
->reuseaddr
);
2202 cma_bind_port(bind_list
, id_priv
);
2207 static int cma_bind_listen(struct rdma_id_private
*id_priv
)
2209 struct rdma_bind_list
*bind_list
= id_priv
->bind_list
;
2213 if (bind_list
->owners
.first
->next
)
2214 ret
= cma_check_port(bind_list
, id_priv
, 0);
2215 mutex_unlock(&lock
);
2219 static int cma_get_port(struct rdma_id_private
*id_priv
)
2224 switch (id_priv
->id
.ps
) {
2238 return -EPROTONOSUPPORT
;
2242 if (cma_any_port((struct sockaddr
*) &id_priv
->id
.route
.addr
.src_addr
))
2243 ret
= cma_alloc_any_port(ps
, id_priv
);
2245 ret
= cma_use_port(ps
, id_priv
);
2246 mutex_unlock(&lock
);
2251 static int cma_check_linklocal(struct rdma_dev_addr
*dev_addr
,
2252 struct sockaddr
*addr
)
2254 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
2255 struct sockaddr_in6
*sin6
;
2257 if (addr
->sa_family
!= AF_INET6
)
2260 sin6
= (struct sockaddr_in6
*) addr
;
2261 if ((ipv6_addr_type(&sin6
->sin6_addr
) & IPV6_ADDR_LINKLOCAL
) &&
2262 !sin6
->sin6_scope_id
)
2265 dev_addr
->bound_dev_if
= sin6
->sin6_scope_id
;
2270 int rdma_listen(struct rdma_cm_id
*id
, int backlog
)
2272 struct rdma_id_private
*id_priv
;
2275 id_priv
= container_of(id
, struct rdma_id_private
, id
);
2276 if (id_priv
->state
== RDMA_CM_IDLE
) {
2277 ((struct sockaddr
*) &id
->route
.addr
.src_addr
)->sa_family
= AF_INET
;
2278 ret
= rdma_bind_addr(id
, (struct sockaddr
*) &id
->route
.addr
.src_addr
);
2283 if (!cma_comp_exch(id_priv
, RDMA_CM_ADDR_BOUND
, RDMA_CM_LISTEN
))
2286 if (id_priv
->reuseaddr
) {
2287 ret
= cma_bind_listen(id_priv
);
2292 id_priv
->backlog
= backlog
;
2294 switch (rdma_node_get_transport(id
->device
->node_type
)) {
2295 case RDMA_TRANSPORT_IB
:
2296 ret
= cma_ib_listen(id_priv
);
2300 case RDMA_TRANSPORT_IWARP
:
2301 ret
= cma_iw_listen(id_priv
, backlog
);
2310 cma_listen_on_all(id_priv
);
2314 id_priv
->backlog
= 0;
2315 cma_comp_exch(id_priv
, RDMA_CM_LISTEN
, RDMA_CM_ADDR_BOUND
);
2318 EXPORT_SYMBOL(rdma_listen
);
2320 int rdma_bind_addr(struct rdma_cm_id
*id
, struct sockaddr
*addr
)
2322 struct rdma_id_private
*id_priv
;
2325 if (addr
->sa_family
!= AF_INET
&& addr
->sa_family
!= AF_INET6
)
2326 return -EAFNOSUPPORT
;
2328 id_priv
= container_of(id
, struct rdma_id_private
, id
);
2329 if (!cma_comp_exch(id_priv
, RDMA_CM_IDLE
, RDMA_CM_ADDR_BOUND
))
2332 ret
= cma_check_linklocal(&id
->route
.addr
.dev_addr
, addr
);
2336 if (!cma_any_addr(addr
)) {
2337 ret
= rdma_translate_ip(addr
, &id
->route
.addr
.dev_addr
);
2341 ret
= cma_acquire_dev(id_priv
);
2346 memcpy(&id
->route
.addr
.src_addr
, addr
, ip_addr_size(addr
));
2347 ret
= cma_get_port(id_priv
);
2353 if (id_priv
->cma_dev
)
2354 cma_release_dev(id_priv
);
2356 cma_comp_exch(id_priv
, RDMA_CM_ADDR_BOUND
, RDMA_CM_IDLE
);
2359 EXPORT_SYMBOL(rdma_bind_addr
);
2361 static int cma_format_hdr(void *hdr
, enum rdma_port_space ps
,
2362 struct rdma_route
*route
)
2364 struct cma_hdr
*cma_hdr
;
2365 struct sdp_hh
*sdp_hdr
;
2367 if (route
->addr
.src_addr
.ss_family
== AF_INET
) {
2368 struct sockaddr_in
*src4
, *dst4
;
2370 src4
= (struct sockaddr_in
*) &route
->addr
.src_addr
;
2371 dst4
= (struct sockaddr_in
*) &route
->addr
.dst_addr
;
2376 if (sdp_get_majv(sdp_hdr
->sdp_version
) != SDP_MAJ_VERSION
)
2378 sdp_set_ip_ver(sdp_hdr
, 4);
2379 sdp_hdr
->src_addr
.ip4
.addr
= src4
->sin_addr
.s_addr
;
2380 sdp_hdr
->dst_addr
.ip4
.addr
= dst4
->sin_addr
.s_addr
;
2381 sdp_hdr
->port
= src4
->sin_port
;
2385 cma_hdr
->cma_version
= CMA_VERSION
;
2386 cma_set_ip_ver(cma_hdr
, 4);
2387 cma_hdr
->src_addr
.ip4
.addr
= src4
->sin_addr
.s_addr
;
2388 cma_hdr
->dst_addr
.ip4
.addr
= dst4
->sin_addr
.s_addr
;
2389 cma_hdr
->port
= src4
->sin_port
;
2393 struct sockaddr_in6
*src6
, *dst6
;
2395 src6
= (struct sockaddr_in6
*) &route
->addr
.src_addr
;
2396 dst6
= (struct sockaddr_in6
*) &route
->addr
.dst_addr
;
2401 if (sdp_get_majv(sdp_hdr
->sdp_version
) != SDP_MAJ_VERSION
)
2403 sdp_set_ip_ver(sdp_hdr
, 6);
2404 sdp_hdr
->src_addr
.ip6
= src6
->sin6_addr
;
2405 sdp_hdr
->dst_addr
.ip6
= dst6
->sin6_addr
;
2406 sdp_hdr
->port
= src6
->sin6_port
;
2410 cma_hdr
->cma_version
= CMA_VERSION
;
2411 cma_set_ip_ver(cma_hdr
, 6);
2412 cma_hdr
->src_addr
.ip6
= src6
->sin6_addr
;
2413 cma_hdr
->dst_addr
.ip6
= dst6
->sin6_addr
;
2414 cma_hdr
->port
= src6
->sin6_port
;
2421 static int cma_sidr_rep_handler(struct ib_cm_id
*cm_id
,
2422 struct ib_cm_event
*ib_event
)
2424 struct rdma_id_private
*id_priv
= cm_id
->context
;
2425 struct rdma_cm_event event
;
2426 struct ib_cm_sidr_rep_event_param
*rep
= &ib_event
->param
.sidr_rep_rcvd
;
2429 if (cma_disable_callback(id_priv
, RDMA_CM_CONNECT
))
2432 memset(&event
, 0, sizeof event
);
2433 switch (ib_event
->event
) {
2434 case IB_CM_SIDR_REQ_ERROR
:
2435 event
.event
= RDMA_CM_EVENT_UNREACHABLE
;
2436 event
.status
= -ETIMEDOUT
;
2438 case IB_CM_SIDR_REP_RECEIVED
:
2439 event
.param
.ud
.private_data
= ib_event
->private_data
;
2440 event
.param
.ud
.private_data_len
= IB_CM_SIDR_REP_PRIVATE_DATA_SIZE
;
2441 if (rep
->status
!= IB_SIDR_SUCCESS
) {
2442 event
.event
= RDMA_CM_EVENT_UNREACHABLE
;
2443 event
.status
= ib_event
->param
.sidr_rep_rcvd
.status
;
2446 ret
= cma_set_qkey(id_priv
);
2448 event
.event
= RDMA_CM_EVENT_ADDR_ERROR
;
2449 event
.status
= -EINVAL
;
2452 if (id_priv
->qkey
!= rep
->qkey
) {
2453 event
.event
= RDMA_CM_EVENT_UNREACHABLE
;
2454 event
.status
= -EINVAL
;
2457 ib_init_ah_from_path(id_priv
->id
.device
, id_priv
->id
.port_num
,
2458 id_priv
->id
.route
.path_rec
,
2459 &event
.param
.ud
.ah_attr
);
2460 event
.param
.ud
.qp_num
= rep
->qpn
;
2461 event
.param
.ud
.qkey
= rep
->qkey
;
2462 event
.event
= RDMA_CM_EVENT_ESTABLISHED
;
2466 printk(KERN_ERR
"RDMA CMA: unexpected IB CM event: %d\n",
2471 ret
= id_priv
->id
.event_handler(&id_priv
->id
, &event
);
2473 /* Destroy the CM ID by returning a non-zero value. */
2474 id_priv
->cm_id
.ib
= NULL
;
2475 cma_exch(id_priv
, RDMA_CM_DESTROYING
);
2476 mutex_unlock(&id_priv
->handler_mutex
);
2477 rdma_destroy_id(&id_priv
->id
);
2481 mutex_unlock(&id_priv
->handler_mutex
);
2485 static int cma_resolve_ib_udp(struct rdma_id_private
*id_priv
,
2486 struct rdma_conn_param
*conn_param
)
2488 struct ib_cm_sidr_req_param req
;
2489 struct rdma_route
*route
;
2490 struct ib_cm_id
*id
;
2493 req
.private_data_len
= sizeof(struct cma_hdr
) +
2494 conn_param
->private_data_len
;
2495 req
.private_data
= kzalloc(req
.private_data_len
, GFP_ATOMIC
);
2496 if (!req
.private_data
)
2499 if (conn_param
->private_data
&& conn_param
->private_data_len
)
2500 memcpy((void *) req
.private_data
+ sizeof(struct cma_hdr
),
2501 conn_param
->private_data
, conn_param
->private_data_len
);
2503 route
= &id_priv
->id
.route
;
2504 ret
= cma_format_hdr((void *) req
.private_data
, id_priv
->id
.ps
, route
);
2508 id
= ib_create_cm_id(id_priv
->id
.device
, cma_sidr_rep_handler
,
2514 id_priv
->cm_id
.ib
= id
;
2516 req
.path
= route
->path_rec
;
2517 req
.service_id
= cma_get_service_id(id_priv
->id
.ps
,
2518 (struct sockaddr
*) &route
->addr
.dst_addr
);
2519 req
.timeout_ms
= 1 << (CMA_CM_RESPONSE_TIMEOUT
- 8);
2520 req
.max_cm_retries
= CMA_MAX_CM_RETRIES
;
2522 ret
= ib_send_cm_sidr_req(id_priv
->cm_id
.ib
, &req
);
2524 ib_destroy_cm_id(id_priv
->cm_id
.ib
);
2525 id_priv
->cm_id
.ib
= NULL
;
2528 kfree(req
.private_data
);
2532 static int cma_connect_ib(struct rdma_id_private
*id_priv
,
2533 struct rdma_conn_param
*conn_param
)
2535 struct ib_cm_req_param req
;
2536 struct rdma_route
*route
;
2538 struct ib_cm_id
*id
;
2541 memset(&req
, 0, sizeof req
);
2542 offset
= cma_user_data_offset(id_priv
->id
.ps
);
2543 req
.private_data_len
= offset
+ conn_param
->private_data_len
;
2544 private_data
= kzalloc(req
.private_data_len
, GFP_ATOMIC
);
2548 if (conn_param
->private_data
&& conn_param
->private_data_len
)
2549 memcpy(private_data
+ offset
, conn_param
->private_data
,
2550 conn_param
->private_data_len
);
2552 id
= ib_create_cm_id(id_priv
->id
.device
, cma_ib_handler
, id_priv
);
2557 id_priv
->cm_id
.ib
= id
;
2559 route
= &id_priv
->id
.route
;
2560 ret
= cma_format_hdr(private_data
, id_priv
->id
.ps
, route
);
2563 req
.private_data
= private_data
;
2565 req
.primary_path
= &route
->path_rec
[0];
2566 if (route
->num_paths
== 2)
2567 req
.alternate_path
= &route
->path_rec
[1];
2569 req
.service_id
= cma_get_service_id(id_priv
->id
.ps
,
2570 (struct sockaddr
*) &route
->addr
.dst_addr
);
2571 req
.qp_num
= id_priv
->qp_num
;
2572 req
.qp_type
= IB_QPT_RC
;
2573 req
.starting_psn
= id_priv
->seq_num
;
2574 req
.responder_resources
= conn_param
->responder_resources
;
2575 req
.initiator_depth
= conn_param
->initiator_depth
;
2576 req
.flow_control
= conn_param
->flow_control
;
2577 req
.retry_count
= conn_param
->retry_count
;
2578 req
.rnr_retry_count
= conn_param
->rnr_retry_count
;
2579 req
.remote_cm_response_timeout
= CMA_CM_RESPONSE_TIMEOUT
;
2580 req
.local_cm_response_timeout
= CMA_CM_RESPONSE_TIMEOUT
;
2581 req
.max_cm_retries
= CMA_MAX_CM_RETRIES
;
2582 req
.srq
= id_priv
->srq
? 1 : 0;
2584 ret
= ib_send_cm_req(id_priv
->cm_id
.ib
, &req
);
2586 if (ret
&& !IS_ERR(id
)) {
2587 ib_destroy_cm_id(id
);
2588 id_priv
->cm_id
.ib
= NULL
;
2591 kfree(private_data
);
2595 static int cma_connect_iw(struct rdma_id_private
*id_priv
,
2596 struct rdma_conn_param
*conn_param
)
2598 struct iw_cm_id
*cm_id
;
2599 struct sockaddr_in
* sin
;
2601 struct iw_cm_conn_param iw_param
;
2603 cm_id
= iw_create_cm_id(id_priv
->id
.device
, cma_iw_handler
, id_priv
);
2605 return PTR_ERR(cm_id
);
2607 id_priv
->cm_id
.iw
= cm_id
;
2609 sin
= (struct sockaddr_in
*) &id_priv
->id
.route
.addr
.src_addr
;
2610 cm_id
->local_addr
= *sin
;
2612 sin
= (struct sockaddr_in
*) &id_priv
->id
.route
.addr
.dst_addr
;
2613 cm_id
->remote_addr
= *sin
;
2615 ret
= cma_modify_qp_rtr(id_priv
, conn_param
);
2619 iw_param
.ord
= conn_param
->initiator_depth
;
2620 iw_param
.ird
= conn_param
->responder_resources
;
2621 iw_param
.private_data
= conn_param
->private_data
;
2622 iw_param
.private_data_len
= conn_param
->private_data_len
;
2624 iw_param
.qpn
= id_priv
->qp_num
;
2626 iw_param
.qpn
= conn_param
->qp_num
;
2627 ret
= iw_cm_connect(cm_id
, &iw_param
);
2630 iw_destroy_cm_id(cm_id
);
2631 id_priv
->cm_id
.iw
= NULL
;
2636 int rdma_connect(struct rdma_cm_id
*id
, struct rdma_conn_param
*conn_param
)
2638 struct rdma_id_private
*id_priv
;
2641 id_priv
= container_of(id
, struct rdma_id_private
, id
);
2642 if (!cma_comp_exch(id_priv
, RDMA_CM_ROUTE_RESOLVED
, RDMA_CM_CONNECT
))
2646 id_priv
->qp_num
= conn_param
->qp_num
;
2647 id_priv
->srq
= conn_param
->srq
;
2650 switch (rdma_node_get_transport(id
->device
->node_type
)) {
2651 case RDMA_TRANSPORT_IB
:
2652 if (id
->qp_type
== IB_QPT_UD
)
2653 ret
= cma_resolve_ib_udp(id_priv
, conn_param
);
2655 ret
= cma_connect_ib(id_priv
, conn_param
);
2657 case RDMA_TRANSPORT_IWARP
:
2658 ret
= cma_connect_iw(id_priv
, conn_param
);
2669 cma_comp_exch(id_priv
, RDMA_CM_CONNECT
, RDMA_CM_ROUTE_RESOLVED
);
2672 EXPORT_SYMBOL(rdma_connect
);
2674 static int cma_accept_ib(struct rdma_id_private
*id_priv
,
2675 struct rdma_conn_param
*conn_param
)
2677 struct ib_cm_rep_param rep
;
2680 ret
= cma_modify_qp_rtr(id_priv
, conn_param
);
2684 ret
= cma_modify_qp_rts(id_priv
, conn_param
);
2688 memset(&rep
, 0, sizeof rep
);
2689 rep
.qp_num
= id_priv
->qp_num
;
2690 rep
.starting_psn
= id_priv
->seq_num
;
2691 rep
.private_data
= conn_param
->private_data
;
2692 rep
.private_data_len
= conn_param
->private_data_len
;
2693 rep
.responder_resources
= conn_param
->responder_resources
;
2694 rep
.initiator_depth
= conn_param
->initiator_depth
;
2695 rep
.failover_accepted
= 0;
2696 rep
.flow_control
= conn_param
->flow_control
;
2697 rep
.rnr_retry_count
= conn_param
->rnr_retry_count
;
2698 rep
.srq
= id_priv
->srq
? 1 : 0;
2700 ret
= ib_send_cm_rep(id_priv
->cm_id
.ib
, &rep
);
2705 static int cma_accept_iw(struct rdma_id_private
*id_priv
,
2706 struct rdma_conn_param
*conn_param
)
2708 struct iw_cm_conn_param iw_param
;
2711 ret
= cma_modify_qp_rtr(id_priv
, conn_param
);
2715 iw_param
.ord
= conn_param
->initiator_depth
;
2716 iw_param
.ird
= conn_param
->responder_resources
;
2717 iw_param
.private_data
= conn_param
->private_data
;
2718 iw_param
.private_data_len
= conn_param
->private_data_len
;
2719 if (id_priv
->id
.qp
) {
2720 iw_param
.qpn
= id_priv
->qp_num
;
2722 iw_param
.qpn
= conn_param
->qp_num
;
2724 return iw_cm_accept(id_priv
->cm_id
.iw
, &iw_param
);
2727 static int cma_send_sidr_rep(struct rdma_id_private
*id_priv
,
2728 enum ib_cm_sidr_status status
,
2729 const void *private_data
, int private_data_len
)
2731 struct ib_cm_sidr_rep_param rep
;
2734 memset(&rep
, 0, sizeof rep
);
2735 rep
.status
= status
;
2736 if (status
== IB_SIDR_SUCCESS
) {
2737 ret
= cma_set_qkey(id_priv
);
2740 rep
.qp_num
= id_priv
->qp_num
;
2741 rep
.qkey
= id_priv
->qkey
;
2743 rep
.private_data
= private_data
;
2744 rep
.private_data_len
= private_data_len
;
2746 return ib_send_cm_sidr_rep(id_priv
->cm_id
.ib
, &rep
);
2749 int rdma_accept(struct rdma_cm_id
*id
, struct rdma_conn_param
*conn_param
)
2751 struct rdma_id_private
*id_priv
;
2754 id_priv
= container_of(id
, struct rdma_id_private
, id
);
2756 id_priv
->owner
= task_pid_nr(current
);
2758 if (!cma_comp(id_priv
, RDMA_CM_CONNECT
))
2761 if (!id
->qp
&& conn_param
) {
2762 id_priv
->qp_num
= conn_param
->qp_num
;
2763 id_priv
->srq
= conn_param
->srq
;
2766 switch (rdma_node_get_transport(id
->device
->node_type
)) {
2767 case RDMA_TRANSPORT_IB
:
2768 if (id
->qp_type
== IB_QPT_UD
)
2769 ret
= cma_send_sidr_rep(id_priv
, IB_SIDR_SUCCESS
,
2770 conn_param
->private_data
,
2771 conn_param
->private_data_len
);
2772 else if (conn_param
)
2773 ret
= cma_accept_ib(id_priv
, conn_param
);
2775 ret
= cma_rep_recv(id_priv
);
2777 case RDMA_TRANSPORT_IWARP
:
2778 ret
= cma_accept_iw(id_priv
, conn_param
);
2790 cma_modify_qp_err(id_priv
);
2791 rdma_reject(id
, NULL
, 0);
2794 EXPORT_SYMBOL(rdma_accept
);
2796 int rdma_notify(struct rdma_cm_id
*id
, enum ib_event_type event
)
2798 struct rdma_id_private
*id_priv
;
2801 id_priv
= container_of(id
, struct rdma_id_private
, id
);
2802 if (!id_priv
->cm_id
.ib
)
2805 switch (id
->device
->node_type
) {
2806 case RDMA_NODE_IB_CA
:
2807 ret
= ib_cm_notify(id_priv
->cm_id
.ib
, event
);
2815 EXPORT_SYMBOL(rdma_notify
);
2817 int rdma_reject(struct rdma_cm_id
*id
, const void *private_data
,
2818 u8 private_data_len
)
2820 struct rdma_id_private
*id_priv
;
2823 id_priv
= container_of(id
, struct rdma_id_private
, id
);
2824 if (!id_priv
->cm_id
.ib
)
2827 switch (rdma_node_get_transport(id
->device
->node_type
)) {
2828 case RDMA_TRANSPORT_IB
:
2829 if (id
->qp_type
== IB_QPT_UD
)
2830 ret
= cma_send_sidr_rep(id_priv
, IB_SIDR_REJECT
,
2831 private_data
, private_data_len
);
2833 ret
= ib_send_cm_rej(id_priv
->cm_id
.ib
,
2834 IB_CM_REJ_CONSUMER_DEFINED
, NULL
,
2835 0, private_data
, private_data_len
);
2837 case RDMA_TRANSPORT_IWARP
:
2838 ret
= iw_cm_reject(id_priv
->cm_id
.iw
,
2839 private_data
, private_data_len
);
2847 EXPORT_SYMBOL(rdma_reject
);
2849 int rdma_disconnect(struct rdma_cm_id
*id
)
2851 struct rdma_id_private
*id_priv
;
2854 id_priv
= container_of(id
, struct rdma_id_private
, id
);
2855 if (!id_priv
->cm_id
.ib
)
2858 switch (rdma_node_get_transport(id
->device
->node_type
)) {
2859 case RDMA_TRANSPORT_IB
:
2860 ret
= cma_modify_qp_err(id_priv
);
2863 /* Initiate or respond to a disconnect. */
2864 if (ib_send_cm_dreq(id_priv
->cm_id
.ib
, NULL
, 0))
2865 ib_send_cm_drep(id_priv
->cm_id
.ib
, NULL
, 0);
2867 case RDMA_TRANSPORT_IWARP
:
2868 ret
= iw_cm_disconnect(id_priv
->cm_id
.iw
, 0);
2877 EXPORT_SYMBOL(rdma_disconnect
);
2879 static int cma_ib_mc_handler(int status
, struct ib_sa_multicast
*multicast
)
2881 struct rdma_id_private
*id_priv
;
2882 struct cma_multicast
*mc
= multicast
->context
;
2883 struct rdma_cm_event event
;
2886 id_priv
= mc
->id_priv
;
2887 if (cma_disable_callback(id_priv
, RDMA_CM_ADDR_BOUND
) &&
2888 cma_disable_callback(id_priv
, RDMA_CM_ADDR_RESOLVED
))
2891 mutex_lock(&id_priv
->qp_mutex
);
2892 if (!status
&& id_priv
->id
.qp
)
2893 status
= ib_attach_mcast(id_priv
->id
.qp
, &multicast
->rec
.mgid
,
2894 multicast
->rec
.mlid
);
2895 mutex_unlock(&id_priv
->qp_mutex
);
2897 memset(&event
, 0, sizeof event
);
2898 event
.status
= status
;
2899 event
.param
.ud
.private_data
= mc
->context
;
2901 event
.event
= RDMA_CM_EVENT_MULTICAST_JOIN
;
2902 ib_init_ah_from_mcmember(id_priv
->id
.device
,
2903 id_priv
->id
.port_num
, &multicast
->rec
,
2904 &event
.param
.ud
.ah_attr
);
2905 event
.param
.ud
.qp_num
= 0xFFFFFF;
2906 event
.param
.ud
.qkey
= be32_to_cpu(multicast
->rec
.qkey
);
2908 event
.event
= RDMA_CM_EVENT_MULTICAST_ERROR
;
2910 ret
= id_priv
->id
.event_handler(&id_priv
->id
, &event
);
2912 cma_exch(id_priv
, RDMA_CM_DESTROYING
);
2913 mutex_unlock(&id_priv
->handler_mutex
);
2914 rdma_destroy_id(&id_priv
->id
);
2918 mutex_unlock(&id_priv
->handler_mutex
);
2922 static void cma_set_mgid(struct rdma_id_private
*id_priv
,
2923 struct sockaddr
*addr
, union ib_gid
*mgid
)
2925 unsigned char mc_map
[MAX_ADDR_LEN
];
2926 struct rdma_dev_addr
*dev_addr
= &id_priv
->id
.route
.addr
.dev_addr
;
2927 struct sockaddr_in
*sin
= (struct sockaddr_in
*) addr
;
2928 struct sockaddr_in6
*sin6
= (struct sockaddr_in6
*) addr
;
2930 if (cma_any_addr(addr
)) {
2931 memset(mgid
, 0, sizeof *mgid
);
2932 } else if ((addr
->sa_family
== AF_INET6
) &&
2933 ((be32_to_cpu(sin6
->sin6_addr
.s6_addr32
[0]) & 0xFFF0FFFF) ==
2935 /* IPv6 address is an SA assigned MGID. */
2936 memcpy(mgid
, &sin6
->sin6_addr
, sizeof *mgid
);
2937 } else if ((addr
->sa_family
== AF_INET6
)) {
2938 ipv6_ib_mc_map(&sin6
->sin6_addr
, dev_addr
->broadcast
, mc_map
);
2939 if (id_priv
->id
.ps
== RDMA_PS_UDP
)
2940 mc_map
[7] = 0x01; /* Use RDMA CM signature */
2941 *mgid
= *(union ib_gid
*) (mc_map
+ 4);
2943 ip_ib_mc_map(sin
->sin_addr
.s_addr
, dev_addr
->broadcast
, mc_map
);
2944 if (id_priv
->id
.ps
== RDMA_PS_UDP
)
2945 mc_map
[7] = 0x01; /* Use RDMA CM signature */
2946 *mgid
= *(union ib_gid
*) (mc_map
+ 4);
2950 static int cma_join_ib_multicast(struct rdma_id_private
*id_priv
,
2951 struct cma_multicast
*mc
)
2953 struct ib_sa_mcmember_rec rec
;
2954 struct rdma_dev_addr
*dev_addr
= &id_priv
->id
.route
.addr
.dev_addr
;
2955 ib_sa_comp_mask comp_mask
;
2958 ib_addr_get_mgid(dev_addr
, &rec
.mgid
);
2959 ret
= ib_sa_get_mcmember_rec(id_priv
->id
.device
, id_priv
->id
.port_num
,
2964 cma_set_mgid(id_priv
, (struct sockaddr
*) &mc
->addr
, &rec
.mgid
);
2965 if (id_priv
->id
.ps
== RDMA_PS_UDP
)
2966 rec
.qkey
= cpu_to_be32(RDMA_UDP_QKEY
);
2967 rdma_addr_get_sgid(dev_addr
, &rec
.port_gid
);
2968 rec
.pkey
= cpu_to_be16(ib_addr_get_pkey(dev_addr
));
2971 comp_mask
= IB_SA_MCMEMBER_REC_MGID
| IB_SA_MCMEMBER_REC_PORT_GID
|
2972 IB_SA_MCMEMBER_REC_PKEY
| IB_SA_MCMEMBER_REC_JOIN_STATE
|
2973 IB_SA_MCMEMBER_REC_QKEY
| IB_SA_MCMEMBER_REC_SL
|
2974 IB_SA_MCMEMBER_REC_FLOW_LABEL
|
2975 IB_SA_MCMEMBER_REC_TRAFFIC_CLASS
;
2977 if (id_priv
->id
.ps
== RDMA_PS_IPOIB
)
2978 comp_mask
|= IB_SA_MCMEMBER_REC_RATE
|
2979 IB_SA_MCMEMBER_REC_RATE_SELECTOR
;
2981 mc
->multicast
.ib
= ib_sa_join_multicast(&sa_client
, id_priv
->id
.device
,
2982 id_priv
->id
.port_num
, &rec
,
2983 comp_mask
, GFP_KERNEL
,
2984 cma_ib_mc_handler
, mc
);
2985 if (IS_ERR(mc
->multicast
.ib
))
2986 return PTR_ERR(mc
->multicast
.ib
);
2991 static void iboe_mcast_work_handler(struct work_struct
*work
)
2993 struct iboe_mcast_work
*mw
= container_of(work
, struct iboe_mcast_work
, work
);
2994 struct cma_multicast
*mc
= mw
->mc
;
2995 struct ib_sa_multicast
*m
= mc
->multicast
.ib
;
2997 mc
->multicast
.ib
->context
= mc
;
2998 cma_ib_mc_handler(0, m
);
2999 kref_put(&mc
->mcref
, release_mc
);
3003 static void cma_iboe_set_mgid(struct sockaddr
*addr
, union ib_gid
*mgid
)
3005 struct sockaddr_in
*sin
= (struct sockaddr_in
*)addr
;
3006 struct sockaddr_in6
*sin6
= (struct sockaddr_in6
*)addr
;
3008 if (cma_any_addr(addr
)) {
3009 memset(mgid
, 0, sizeof *mgid
);
3010 } else if (addr
->sa_family
== AF_INET6
) {
3011 memcpy(mgid
, &sin6
->sin6_addr
, sizeof *mgid
);
3013 mgid
->raw
[0] = 0xff;
3014 mgid
->raw
[1] = 0x0e;
3023 mgid
->raw
[10] = 0xff;
3024 mgid
->raw
[11] = 0xff;
3025 *(__be32
*)(&mgid
->raw
[12]) = sin
->sin_addr
.s_addr
;
3029 static int cma_iboe_join_multicast(struct rdma_id_private
*id_priv
,
3030 struct cma_multicast
*mc
)
3032 struct iboe_mcast_work
*work
;
3033 struct rdma_dev_addr
*dev_addr
= &id_priv
->id
.route
.addr
.dev_addr
;
3035 struct sockaddr
*addr
= (struct sockaddr
*)&mc
->addr
;
3036 struct net_device
*ndev
= NULL
;
3038 if (cma_zero_addr((struct sockaddr
*)&mc
->addr
))
3041 work
= kzalloc(sizeof *work
, GFP_KERNEL
);
3045 mc
->multicast
.ib
= kzalloc(sizeof(struct ib_sa_multicast
), GFP_KERNEL
);
3046 if (!mc
->multicast
.ib
) {
3051 cma_iboe_set_mgid(addr
, &mc
->multicast
.ib
->rec
.mgid
);
3053 mc
->multicast
.ib
->rec
.pkey
= cpu_to_be16(0xffff);
3054 if (id_priv
->id
.ps
== RDMA_PS_UDP
)
3055 mc
->multicast
.ib
->rec
.qkey
= cpu_to_be32(RDMA_UDP_QKEY
);
3057 if (dev_addr
->bound_dev_if
)
3058 ndev
= dev_get_by_index(&init_net
, dev_addr
->bound_dev_if
);
3063 mc
->multicast
.ib
->rec
.rate
= iboe_get_rate(ndev
);
3064 mc
->multicast
.ib
->rec
.hop_limit
= 1;
3065 mc
->multicast
.ib
->rec
.mtu
= iboe_get_mtu(ndev
->mtu
);
3067 if (!mc
->multicast
.ib
->rec
.mtu
) {
3071 iboe_addr_get_sgid(dev_addr
, &mc
->multicast
.ib
->rec
.port_gid
);
3074 INIT_WORK(&work
->work
, iboe_mcast_work_handler
);
3075 kref_get(&mc
->mcref
);
3076 queue_work(cma_wq
, &work
->work
);
3081 kfree(mc
->multicast
.ib
);
3087 int rdma_join_multicast(struct rdma_cm_id
*id
, struct sockaddr
*addr
,
3090 struct rdma_id_private
*id_priv
;
3091 struct cma_multicast
*mc
;
3094 id_priv
= container_of(id
, struct rdma_id_private
, id
);
3095 if (!cma_comp(id_priv
, RDMA_CM_ADDR_BOUND
) &&
3096 !cma_comp(id_priv
, RDMA_CM_ADDR_RESOLVED
))
3099 mc
= kmalloc(sizeof *mc
, GFP_KERNEL
);
3103 memcpy(&mc
->addr
, addr
, ip_addr_size(addr
));
3104 mc
->context
= context
;
3105 mc
->id_priv
= id_priv
;
3107 spin_lock(&id_priv
->lock
);
3108 list_add(&mc
->list
, &id_priv
->mc_list
);
3109 spin_unlock(&id_priv
->lock
);
3111 switch (rdma_node_get_transport(id
->device
->node_type
)) {
3112 case RDMA_TRANSPORT_IB
:
3113 switch (rdma_port_get_link_layer(id
->device
, id
->port_num
)) {
3114 case IB_LINK_LAYER_INFINIBAND
:
3115 ret
= cma_join_ib_multicast(id_priv
, mc
);
3117 case IB_LINK_LAYER_ETHERNET
:
3118 kref_init(&mc
->mcref
);
3119 ret
= cma_iboe_join_multicast(id_priv
, mc
);
3131 spin_lock_irq(&id_priv
->lock
);
3132 list_del(&mc
->list
);
3133 spin_unlock_irq(&id_priv
->lock
);
3138 EXPORT_SYMBOL(rdma_join_multicast
);
3140 void rdma_leave_multicast(struct rdma_cm_id
*id
, struct sockaddr
*addr
)
3142 struct rdma_id_private
*id_priv
;
3143 struct cma_multicast
*mc
;
3145 id_priv
= container_of(id
, struct rdma_id_private
, id
);
3146 spin_lock_irq(&id_priv
->lock
);
3147 list_for_each_entry(mc
, &id_priv
->mc_list
, list
) {
3148 if (!memcmp(&mc
->addr
, addr
, ip_addr_size(addr
))) {
3149 list_del(&mc
->list
);
3150 spin_unlock_irq(&id_priv
->lock
);
3153 ib_detach_mcast(id
->qp
,
3154 &mc
->multicast
.ib
->rec
.mgid
,
3155 mc
->multicast
.ib
->rec
.mlid
);
3156 if (rdma_node_get_transport(id_priv
->cma_dev
->device
->node_type
) == RDMA_TRANSPORT_IB
) {
3157 switch (rdma_port_get_link_layer(id
->device
, id
->port_num
)) {
3158 case IB_LINK_LAYER_INFINIBAND
:
3159 ib_sa_free_multicast(mc
->multicast
.ib
);
3162 case IB_LINK_LAYER_ETHERNET
:
3163 kref_put(&mc
->mcref
, release_mc
);
3172 spin_unlock_irq(&id_priv
->lock
);
3174 EXPORT_SYMBOL(rdma_leave_multicast
);
3176 static int cma_netdev_change(struct net_device
*ndev
, struct rdma_id_private
*id_priv
)
3178 struct rdma_dev_addr
*dev_addr
;
3179 struct cma_ndev_work
*work
;
3181 dev_addr
= &id_priv
->id
.route
.addr
.dev_addr
;
3183 if ((dev_addr
->bound_dev_if
== ndev
->ifindex
) &&
3184 memcmp(dev_addr
->src_dev_addr
, ndev
->dev_addr
, ndev
->addr_len
)) {
3185 printk(KERN_INFO
"RDMA CM addr change for ndev %s used by id %p\n",
3186 ndev
->name
, &id_priv
->id
);
3187 work
= kzalloc(sizeof *work
, GFP_KERNEL
);
3191 INIT_WORK(&work
->work
, cma_ndev_work_handler
);
3193 work
->event
.event
= RDMA_CM_EVENT_ADDR_CHANGE
;
3194 atomic_inc(&id_priv
->refcount
);
3195 queue_work(cma_wq
, &work
->work
);
3201 static int cma_netdev_callback(struct notifier_block
*self
, unsigned long event
,
3204 struct net_device
*ndev
= (struct net_device
*)ctx
;
3205 struct cma_device
*cma_dev
;
3206 struct rdma_id_private
*id_priv
;
3207 int ret
= NOTIFY_DONE
;
3209 if (dev_net(ndev
) != &init_net
)
3212 if (event
!= NETDEV_BONDING_FAILOVER
)
3215 if (!(ndev
->flags
& IFF_MASTER
) || !(ndev
->priv_flags
& IFF_BONDING
))
3219 list_for_each_entry(cma_dev
, &dev_list
, list
)
3220 list_for_each_entry(id_priv
, &cma_dev
->id_list
, list
) {
3221 ret
= cma_netdev_change(ndev
, id_priv
);
3227 mutex_unlock(&lock
);
3231 static struct notifier_block cma_nb
= {
3232 .notifier_call
= cma_netdev_callback
3235 static void cma_add_one(struct ib_device
*device
)
3237 struct cma_device
*cma_dev
;
3238 struct rdma_id_private
*id_priv
;
3240 cma_dev
= kmalloc(sizeof *cma_dev
, GFP_KERNEL
);
3244 cma_dev
->device
= device
;
3246 init_completion(&cma_dev
->comp
);
3247 atomic_set(&cma_dev
->refcount
, 1);
3248 INIT_LIST_HEAD(&cma_dev
->id_list
);
3249 ib_set_client_data(device
, &cma_client
, cma_dev
);
3252 list_add_tail(&cma_dev
->list
, &dev_list
);
3253 list_for_each_entry(id_priv
, &listen_any_list
, list
)
3254 cma_listen_on_dev(id_priv
, cma_dev
);
3255 mutex_unlock(&lock
);
3258 static int cma_remove_id_dev(struct rdma_id_private
*id_priv
)
3260 struct rdma_cm_event event
;
3261 enum rdma_cm_state state
;
3264 /* Record that we want to remove the device */
3265 state
= cma_exch(id_priv
, RDMA_CM_DEVICE_REMOVAL
);
3266 if (state
== RDMA_CM_DESTROYING
)
3269 cma_cancel_operation(id_priv
, state
);
3270 mutex_lock(&id_priv
->handler_mutex
);
3272 /* Check for destruction from another callback. */
3273 if (!cma_comp(id_priv
, RDMA_CM_DEVICE_REMOVAL
))
3276 memset(&event
, 0, sizeof event
);
3277 event
.event
= RDMA_CM_EVENT_DEVICE_REMOVAL
;
3278 ret
= id_priv
->id
.event_handler(&id_priv
->id
, &event
);
3280 mutex_unlock(&id_priv
->handler_mutex
);
3284 static void cma_process_remove(struct cma_device
*cma_dev
)
3286 struct rdma_id_private
*id_priv
;
3290 while (!list_empty(&cma_dev
->id_list
)) {
3291 id_priv
= list_entry(cma_dev
->id_list
.next
,
3292 struct rdma_id_private
, list
);
3294 list_del(&id_priv
->listen_list
);
3295 list_del_init(&id_priv
->list
);
3296 atomic_inc(&id_priv
->refcount
);
3297 mutex_unlock(&lock
);
3299 ret
= id_priv
->internal_id
? 1 : cma_remove_id_dev(id_priv
);
3300 cma_deref_id(id_priv
);
3302 rdma_destroy_id(&id_priv
->id
);
3306 mutex_unlock(&lock
);
3308 cma_deref_dev(cma_dev
);
3309 wait_for_completion(&cma_dev
->comp
);
3312 static void cma_remove_one(struct ib_device
*device
)
3314 struct cma_device
*cma_dev
;
3316 cma_dev
= ib_get_client_data(device
, &cma_client
);
3321 list_del(&cma_dev
->list
);
3322 mutex_unlock(&lock
);
3324 cma_process_remove(cma_dev
);
3328 static int cma_get_id_stats(struct sk_buff
*skb
, struct netlink_callback
*cb
)
3330 struct nlmsghdr
*nlh
;
3331 struct rdma_cm_id_stats
*id_stats
;
3332 struct rdma_id_private
*id_priv
;
3333 struct rdma_cm_id
*id
= NULL
;
3334 struct cma_device
*cma_dev
;
3335 int i_dev
= 0, i_id
= 0;
3338 * We export all of the IDs as a sequence of messages. Each
3339 * ID gets its own netlink message.
3343 list_for_each_entry(cma_dev
, &dev_list
, list
) {
3344 if (i_dev
< cb
->args
[0]) {
3350 list_for_each_entry(id_priv
, &cma_dev
->id_list
, list
) {
3351 if (i_id
< cb
->args
[1]) {
3356 id_stats
= ibnl_put_msg(skb
, &nlh
, cb
->nlh
->nlmsg_seq
,
3357 sizeof *id_stats
, RDMA_NL_RDMA_CM
,
3358 RDMA_NL_RDMA_CM_ID_STATS
);
3362 memset(id_stats
, 0, sizeof *id_stats
);
3364 id_stats
->node_type
= id
->route
.addr
.dev_addr
.dev_type
;
3365 id_stats
->port_num
= id
->port_num
;
3366 id_stats
->bound_dev_if
=
3367 id
->route
.addr
.dev_addr
.bound_dev_if
;
3369 if (id
->route
.addr
.src_addr
.ss_family
== AF_INET
) {
3370 if (ibnl_put_attr(skb
, nlh
,
3371 sizeof(struct sockaddr_in
),
3372 &id
->route
.addr
.src_addr
,
3373 RDMA_NL_RDMA_CM_ATTR_SRC_ADDR
)) {
3376 if (ibnl_put_attr(skb
, nlh
,
3377 sizeof(struct sockaddr_in
),
3378 &id
->route
.addr
.dst_addr
,
3379 RDMA_NL_RDMA_CM_ATTR_DST_ADDR
)) {
3382 } else if (id
->route
.addr
.src_addr
.ss_family
== AF_INET6
) {
3383 if (ibnl_put_attr(skb
, nlh
,
3384 sizeof(struct sockaddr_in6
),
3385 &id
->route
.addr
.src_addr
,
3386 RDMA_NL_RDMA_CM_ATTR_SRC_ADDR
)) {
3389 if (ibnl_put_attr(skb
, nlh
,
3390 sizeof(struct sockaddr_in6
),
3391 &id
->route
.addr
.dst_addr
,
3392 RDMA_NL_RDMA_CM_ATTR_DST_ADDR
)) {
3397 id_stats
->pid
= id_priv
->owner
;
3398 id_stats
->port_space
= id
->ps
;
3399 id_stats
->cm_state
= id_priv
->state
;
3400 id_stats
->qp_num
= id_priv
->qp_num
;
3401 id_stats
->qp_type
= id
->qp_type
;
3411 mutex_unlock(&lock
);
3412 cb
->args
[0] = i_dev
;
3418 static const struct ibnl_client_cbs cma_cb_table
[] = {
3419 [RDMA_NL_RDMA_CM_ID_STATS
] = { .dump
= cma_get_id_stats
},
3422 static int __init
cma_init(void)
3426 cma_wq
= create_singlethread_workqueue("rdma_cm");
3430 ib_sa_register_client(&sa_client
);
3431 rdma_addr_register_client(&addr_client
);
3432 register_netdevice_notifier(&cma_nb
);
3434 ret
= ib_register_client(&cma_client
);
3438 if (ibnl_add_client(RDMA_NL_RDMA_CM
, RDMA_NL_RDMA_CM_NUM_OPS
, cma_cb_table
))
3439 printk(KERN_WARNING
"RDMA CMA: failed to add netlink callback\n");
3444 unregister_netdevice_notifier(&cma_nb
);
3445 rdma_addr_unregister_client(&addr_client
);
3446 ib_sa_unregister_client(&sa_client
);
3447 destroy_workqueue(cma_wq
);
3451 static void __exit
cma_cleanup(void)
3453 ibnl_remove_client(RDMA_NL_RDMA_CM
);
3454 ib_unregister_client(&cma_client
);
3455 unregister_netdevice_notifier(&cma_nb
);
3456 rdma_addr_unregister_client(&addr_client
);
3457 ib_sa_unregister_client(&sa_client
);
3458 destroy_workqueue(cma_wq
);
3459 idr_destroy(&sdp_ps
);
3460 idr_destroy(&tcp_ps
);
3461 idr_destroy(&udp_ps
);
3462 idr_destroy(&ipoib_ps
);
3465 module_init(cma_init
);
3466 module_exit(cma_cleanup
);