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_cm_id
*id
,
1269 const struct net_device
*net_dev
,
1272 const struct rdma_addr
*addr
= &id
->route
.addr
;
1275 /* This request is an AF_IB request or a RoCE request */
1276 return (!id
->port_num
|| id
->port_num
== port_num
) &&
1277 (addr
->src_addr
.ss_family
== AF_IB
||
1278 cma_protocol_roce_dev_port(id
->device
, port_num
));
1280 return !addr
->dev_addr
.bound_dev_if
||
1281 (net_eq(dev_net(net_dev
), addr
->dev_addr
.net
) &&
1282 addr
->dev_addr
.bound_dev_if
== net_dev
->ifindex
);
1285 static struct rdma_id_private
*cma_find_listener(
1286 const struct rdma_bind_list
*bind_list
,
1287 const struct ib_cm_id
*cm_id
,
1288 const struct ib_cm_event
*ib_event
,
1289 const struct cma_req_info
*req
,
1290 const struct net_device
*net_dev
)
1292 struct rdma_id_private
*id_priv
, *id_priv_dev
;
1295 return ERR_PTR(-EINVAL
);
1297 hlist_for_each_entry(id_priv
, &bind_list
->owners
, node
) {
1298 if (cma_match_private_data(id_priv
, ib_event
->private_data
)) {
1299 if (id_priv
->id
.device
== cm_id
->device
&&
1300 cma_match_net_dev(&id_priv
->id
, net_dev
, req
->port
))
1302 list_for_each_entry(id_priv_dev
,
1303 &id_priv
->listen_list
,
1305 if (id_priv_dev
->id
.device
== cm_id
->device
&&
1306 cma_match_net_dev(&id_priv_dev
->id
, net_dev
, req
->port
))
1312 return ERR_PTR(-EINVAL
);
1315 static struct rdma_id_private
*cma_id_from_event(struct ib_cm_id
*cm_id
,
1316 struct ib_cm_event
*ib_event
,
1317 struct net_device
**net_dev
)
1319 struct cma_req_info req
;
1320 struct rdma_bind_list
*bind_list
;
1321 struct rdma_id_private
*id_priv
;
1324 err
= cma_save_req_info(ib_event
, &req
);
1326 return ERR_PTR(err
);
1328 *net_dev
= cma_get_net_dev(ib_event
, &req
);
1329 if (IS_ERR(*net_dev
)) {
1330 if (PTR_ERR(*net_dev
) == -EAFNOSUPPORT
) {
1331 /* Assuming the protocol is AF_IB */
1333 } else if (cma_protocol_roce_dev_port(req
.device
, req
.port
)) {
1334 /* TODO find the net dev matching the request parameters
1335 * through the RoCE GID table */
1338 return ERR_CAST(*net_dev
);
1342 bind_list
= cma_ps_find(*net_dev
? dev_net(*net_dev
) : &init_net
,
1343 rdma_ps_from_service_id(req
.service_id
),
1344 cma_port_from_service_id(req
.service_id
));
1345 id_priv
= cma_find_listener(bind_list
, cm_id
, ib_event
, &req
, *net_dev
);
1346 if (IS_ERR(id_priv
) && *net_dev
) {
1354 static inline int cma_user_data_offset(struct rdma_id_private
*id_priv
)
1356 return cma_family(id_priv
) == AF_IB
? 0 : sizeof(struct cma_hdr
);
1359 static void cma_cancel_route(struct rdma_id_private
*id_priv
)
1361 if (rdma_cap_ib_sa(id_priv
->id
.device
, id_priv
->id
.port_num
)) {
1363 ib_sa_cancel_query(id_priv
->query_id
, id_priv
->query
);
1367 static void cma_cancel_listens(struct rdma_id_private
*id_priv
)
1369 struct rdma_id_private
*dev_id_priv
;
1372 * Remove from listen_any_list to prevent added devices from spawning
1373 * additional listen requests.
1376 list_del(&id_priv
->list
);
1378 while (!list_empty(&id_priv
->listen_list
)) {
1379 dev_id_priv
= list_entry(id_priv
->listen_list
.next
,
1380 struct rdma_id_private
, listen_list
);
1381 /* sync with device removal to avoid duplicate destruction */
1382 list_del_init(&dev_id_priv
->list
);
1383 list_del(&dev_id_priv
->listen_list
);
1384 mutex_unlock(&lock
);
1386 rdma_destroy_id(&dev_id_priv
->id
);
1389 mutex_unlock(&lock
);
1392 static void cma_cancel_operation(struct rdma_id_private
*id_priv
,
1393 enum rdma_cm_state state
)
1396 case RDMA_CM_ADDR_QUERY
:
1397 rdma_addr_cancel(&id_priv
->id
.route
.addr
.dev_addr
);
1399 case RDMA_CM_ROUTE_QUERY
:
1400 cma_cancel_route(id_priv
);
1402 case RDMA_CM_LISTEN
:
1403 if (cma_any_addr(cma_src_addr(id_priv
)) && !id_priv
->cma_dev
)
1404 cma_cancel_listens(id_priv
);
1411 static void cma_release_port(struct rdma_id_private
*id_priv
)
1413 struct rdma_bind_list
*bind_list
= id_priv
->bind_list
;
1414 struct net
*net
= id_priv
->id
.route
.addr
.dev_addr
.net
;
1420 hlist_del(&id_priv
->node
);
1421 if (hlist_empty(&bind_list
->owners
)) {
1422 cma_ps_remove(net
, bind_list
->ps
, bind_list
->port
);
1425 mutex_unlock(&lock
);
1428 static void cma_leave_mc_groups(struct rdma_id_private
*id_priv
)
1430 struct cma_multicast
*mc
;
1432 while (!list_empty(&id_priv
->mc_list
)) {
1433 mc
= container_of(id_priv
->mc_list
.next
,
1434 struct cma_multicast
, list
);
1435 list_del(&mc
->list
);
1436 if (rdma_cap_ib_mcast(id_priv
->cma_dev
->device
,
1437 id_priv
->id
.port_num
)) {
1438 ib_sa_free_multicast(mc
->multicast
.ib
);
1441 kref_put(&mc
->mcref
, release_mc
);
1445 void rdma_destroy_id(struct rdma_cm_id
*id
)
1447 struct rdma_id_private
*id_priv
;
1448 enum rdma_cm_state state
;
1450 id_priv
= container_of(id
, struct rdma_id_private
, id
);
1451 state
= cma_exch(id_priv
, RDMA_CM_DESTROYING
);
1452 cma_cancel_operation(id_priv
, state
);
1455 * Wait for any active callback to finish. New callbacks will find
1456 * the id_priv state set to destroying and abort.
1458 mutex_lock(&id_priv
->handler_mutex
);
1459 mutex_unlock(&id_priv
->handler_mutex
);
1461 if (id_priv
->cma_dev
) {
1462 if (rdma_cap_ib_cm(id_priv
->id
.device
, 1)) {
1463 if (id_priv
->cm_id
.ib
)
1464 ib_destroy_cm_id(id_priv
->cm_id
.ib
);
1465 } else if (rdma_cap_iw_cm(id_priv
->id
.device
, 1)) {
1466 if (id_priv
->cm_id
.iw
)
1467 iw_destroy_cm_id(id_priv
->cm_id
.iw
);
1469 cma_leave_mc_groups(id_priv
);
1470 cma_release_dev(id_priv
);
1473 cma_release_port(id_priv
);
1474 cma_deref_id(id_priv
);
1475 wait_for_completion(&id_priv
->comp
);
1477 if (id_priv
->internal_id
)
1478 cma_deref_id(id_priv
->id
.context
);
1480 kfree(id_priv
->id
.route
.path_rec
);
1481 put_net(id_priv
->id
.route
.addr
.dev_addr
.net
);
1484 EXPORT_SYMBOL(rdma_destroy_id
);
1486 static int cma_rep_recv(struct rdma_id_private
*id_priv
)
1490 ret
= cma_modify_qp_rtr(id_priv
, NULL
);
1494 ret
= cma_modify_qp_rts(id_priv
, NULL
);
1498 ret
= ib_send_cm_rtu(id_priv
->cm_id
.ib
, NULL
, 0);
1504 cma_modify_qp_err(id_priv
);
1505 ib_send_cm_rej(id_priv
->cm_id
.ib
, IB_CM_REJ_CONSUMER_DEFINED
,
1510 static void cma_set_rep_event_data(struct rdma_cm_event
*event
,
1511 struct ib_cm_rep_event_param
*rep_data
,
1514 event
->param
.conn
.private_data
= private_data
;
1515 event
->param
.conn
.private_data_len
= IB_CM_REP_PRIVATE_DATA_SIZE
;
1516 event
->param
.conn
.responder_resources
= rep_data
->responder_resources
;
1517 event
->param
.conn
.initiator_depth
= rep_data
->initiator_depth
;
1518 event
->param
.conn
.flow_control
= rep_data
->flow_control
;
1519 event
->param
.conn
.rnr_retry_count
= rep_data
->rnr_retry_count
;
1520 event
->param
.conn
.srq
= rep_data
->srq
;
1521 event
->param
.conn
.qp_num
= rep_data
->remote_qpn
;
1524 static int cma_ib_handler(struct ib_cm_id
*cm_id
, struct ib_cm_event
*ib_event
)
1526 struct rdma_id_private
*id_priv
= cm_id
->context
;
1527 struct rdma_cm_event event
;
1530 if ((ib_event
->event
!= IB_CM_TIMEWAIT_EXIT
&&
1531 cma_disable_callback(id_priv
, RDMA_CM_CONNECT
)) ||
1532 (ib_event
->event
== IB_CM_TIMEWAIT_EXIT
&&
1533 cma_disable_callback(id_priv
, RDMA_CM_DISCONNECT
)))
1536 memset(&event
, 0, sizeof event
);
1537 switch (ib_event
->event
) {
1538 case IB_CM_REQ_ERROR
:
1539 case IB_CM_REP_ERROR
:
1540 event
.event
= RDMA_CM_EVENT_UNREACHABLE
;
1541 event
.status
= -ETIMEDOUT
;
1543 case IB_CM_REP_RECEIVED
:
1544 if (id_priv
->id
.qp
) {
1545 event
.status
= cma_rep_recv(id_priv
);
1546 event
.event
= event
.status
? RDMA_CM_EVENT_CONNECT_ERROR
:
1547 RDMA_CM_EVENT_ESTABLISHED
;
1549 event
.event
= RDMA_CM_EVENT_CONNECT_RESPONSE
;
1551 cma_set_rep_event_data(&event
, &ib_event
->param
.rep_rcvd
,
1552 ib_event
->private_data
);
1554 case IB_CM_RTU_RECEIVED
:
1555 case IB_CM_USER_ESTABLISHED
:
1556 event
.event
= RDMA_CM_EVENT_ESTABLISHED
;
1558 case IB_CM_DREQ_ERROR
:
1559 event
.status
= -ETIMEDOUT
; /* fall through */
1560 case IB_CM_DREQ_RECEIVED
:
1561 case IB_CM_DREP_RECEIVED
:
1562 if (!cma_comp_exch(id_priv
, RDMA_CM_CONNECT
,
1563 RDMA_CM_DISCONNECT
))
1565 event
.event
= RDMA_CM_EVENT_DISCONNECTED
;
1567 case IB_CM_TIMEWAIT_EXIT
:
1568 event
.event
= RDMA_CM_EVENT_TIMEWAIT_EXIT
;
1570 case IB_CM_MRA_RECEIVED
:
1573 case IB_CM_REJ_RECEIVED
:
1574 cma_modify_qp_err(id_priv
);
1575 event
.status
= ib_event
->param
.rej_rcvd
.reason
;
1576 event
.event
= RDMA_CM_EVENT_REJECTED
;
1577 event
.param
.conn
.private_data
= ib_event
->private_data
;
1578 event
.param
.conn
.private_data_len
= IB_CM_REJ_PRIVATE_DATA_SIZE
;
1581 printk(KERN_ERR
"RDMA CMA: unexpected IB CM event: %d\n",
1586 ret
= id_priv
->id
.event_handler(&id_priv
->id
, &event
);
1588 /* Destroy the CM ID by returning a non-zero value. */
1589 id_priv
->cm_id
.ib
= NULL
;
1590 cma_exch(id_priv
, RDMA_CM_DESTROYING
);
1591 mutex_unlock(&id_priv
->handler_mutex
);
1592 rdma_destroy_id(&id_priv
->id
);
1596 mutex_unlock(&id_priv
->handler_mutex
);
1600 static struct rdma_id_private
*cma_new_conn_id(struct rdma_cm_id
*listen_id
,
1601 struct ib_cm_event
*ib_event
,
1602 struct net_device
*net_dev
)
1604 struct rdma_id_private
*id_priv
;
1605 struct rdma_cm_id
*id
;
1606 struct rdma_route
*rt
;
1607 const sa_family_t ss_family
= listen_id
->route
.addr
.src_addr
.ss_family
;
1608 const __be64 service_id
=
1609 ib_event
->param
.req_rcvd
.primary_path
->service_id
;
1612 id
= rdma_create_id(listen_id
->route
.addr
.dev_addr
.net
,
1613 listen_id
->event_handler
, listen_id
->context
,
1614 listen_id
->ps
, ib_event
->param
.req_rcvd
.qp_type
);
1618 id_priv
= container_of(id
, struct rdma_id_private
, id
);
1619 if (cma_save_net_info((struct sockaddr
*)&id
->route
.addr
.src_addr
,
1620 (struct sockaddr
*)&id
->route
.addr
.dst_addr
,
1621 listen_id
, ib_event
, ss_family
, service_id
))
1625 rt
->num_paths
= ib_event
->param
.req_rcvd
.alternate_path
? 2 : 1;
1626 rt
->path_rec
= kmalloc(sizeof *rt
->path_rec
* rt
->num_paths
,
1631 rt
->path_rec
[0] = *ib_event
->param
.req_rcvd
.primary_path
;
1632 if (rt
->num_paths
== 2)
1633 rt
->path_rec
[1] = *ib_event
->param
.req_rcvd
.alternate_path
;
1636 ret
= rdma_copy_addr(&rt
->addr
.dev_addr
, net_dev
, NULL
);
1640 if (!cma_protocol_roce(listen_id
) &&
1641 cma_any_addr(cma_src_addr(id_priv
))) {
1642 rt
->addr
.dev_addr
.dev_type
= ARPHRD_INFINIBAND
;
1643 rdma_addr_set_sgid(&rt
->addr
.dev_addr
, &rt
->path_rec
[0].sgid
);
1644 ib_addr_set_pkey(&rt
->addr
.dev_addr
, be16_to_cpu(rt
->path_rec
[0].pkey
));
1645 } else if (!cma_any_addr(cma_src_addr(id_priv
))) {
1646 ret
= cma_translate_addr(cma_src_addr(id_priv
), &rt
->addr
.dev_addr
);
1651 rdma_addr_set_dgid(&rt
->addr
.dev_addr
, &rt
->path_rec
[0].dgid
);
1653 id_priv
->state
= RDMA_CM_CONNECT
;
1657 rdma_destroy_id(id
);
1661 static struct rdma_id_private
*cma_new_udp_id(struct rdma_cm_id
*listen_id
,
1662 struct ib_cm_event
*ib_event
,
1663 struct net_device
*net_dev
)
1665 struct rdma_id_private
*id_priv
;
1666 struct rdma_cm_id
*id
;
1667 const sa_family_t ss_family
= listen_id
->route
.addr
.src_addr
.ss_family
;
1668 struct net
*net
= listen_id
->route
.addr
.dev_addr
.net
;
1671 id
= rdma_create_id(net
, listen_id
->event_handler
, listen_id
->context
,
1672 listen_id
->ps
, IB_QPT_UD
);
1676 id_priv
= container_of(id
, struct rdma_id_private
, id
);
1677 if (cma_save_net_info((struct sockaddr
*)&id
->route
.addr
.src_addr
,
1678 (struct sockaddr
*)&id
->route
.addr
.dst_addr
,
1679 listen_id
, ib_event
, ss_family
,
1680 ib_event
->param
.sidr_req_rcvd
.service_id
))
1684 ret
= rdma_copy_addr(&id
->route
.addr
.dev_addr
, net_dev
, NULL
);
1688 if (!cma_any_addr(cma_src_addr(id_priv
))) {
1689 ret
= cma_translate_addr(cma_src_addr(id_priv
),
1690 &id
->route
.addr
.dev_addr
);
1696 id_priv
->state
= RDMA_CM_CONNECT
;
1699 rdma_destroy_id(id
);
1703 static void cma_set_req_event_data(struct rdma_cm_event
*event
,
1704 struct ib_cm_req_event_param
*req_data
,
1705 void *private_data
, int offset
)
1707 event
->param
.conn
.private_data
= private_data
+ offset
;
1708 event
->param
.conn
.private_data_len
= IB_CM_REQ_PRIVATE_DATA_SIZE
- offset
;
1709 event
->param
.conn
.responder_resources
= req_data
->responder_resources
;
1710 event
->param
.conn
.initiator_depth
= req_data
->initiator_depth
;
1711 event
->param
.conn
.flow_control
= req_data
->flow_control
;
1712 event
->param
.conn
.retry_count
= req_data
->retry_count
;
1713 event
->param
.conn
.rnr_retry_count
= req_data
->rnr_retry_count
;
1714 event
->param
.conn
.srq
= req_data
->srq
;
1715 event
->param
.conn
.qp_num
= req_data
->remote_qpn
;
1718 static int cma_check_req_qp_type(struct rdma_cm_id
*id
, struct ib_cm_event
*ib_event
)
1720 return (((ib_event
->event
== IB_CM_REQ_RECEIVED
) &&
1721 (ib_event
->param
.req_rcvd
.qp_type
== id
->qp_type
)) ||
1722 ((ib_event
->event
== IB_CM_SIDR_REQ_RECEIVED
) &&
1723 (id
->qp_type
== IB_QPT_UD
)) ||
1727 static int cma_req_handler(struct ib_cm_id
*cm_id
, struct ib_cm_event
*ib_event
)
1729 struct rdma_id_private
*listen_id
, *conn_id
;
1730 struct rdma_cm_event event
;
1731 struct net_device
*net_dev
;
1734 listen_id
= cma_id_from_event(cm_id
, ib_event
, &net_dev
);
1735 if (IS_ERR(listen_id
))
1736 return PTR_ERR(listen_id
);
1738 if (!cma_check_req_qp_type(&listen_id
->id
, ib_event
)) {
1743 if (cma_disable_callback(listen_id
, RDMA_CM_LISTEN
)) {
1744 ret
= -ECONNABORTED
;
1748 memset(&event
, 0, sizeof event
);
1749 offset
= cma_user_data_offset(listen_id
);
1750 event
.event
= RDMA_CM_EVENT_CONNECT_REQUEST
;
1751 if (ib_event
->event
== IB_CM_SIDR_REQ_RECEIVED
) {
1752 conn_id
= cma_new_udp_id(&listen_id
->id
, ib_event
, net_dev
);
1753 event
.param
.ud
.private_data
= ib_event
->private_data
+ offset
;
1754 event
.param
.ud
.private_data_len
=
1755 IB_CM_SIDR_REQ_PRIVATE_DATA_SIZE
- offset
;
1757 conn_id
= cma_new_conn_id(&listen_id
->id
, ib_event
, net_dev
);
1758 cma_set_req_event_data(&event
, &ib_event
->param
.req_rcvd
,
1759 ib_event
->private_data
, offset
);
1766 mutex_lock_nested(&conn_id
->handler_mutex
, SINGLE_DEPTH_NESTING
);
1767 ret
= cma_acquire_dev(conn_id
, listen_id
);
1771 conn_id
->cm_id
.ib
= cm_id
;
1772 cm_id
->context
= conn_id
;
1773 cm_id
->cm_handler
= cma_ib_handler
;
1776 * Protect against the user destroying conn_id from another thread
1777 * until we're done accessing it.
1779 atomic_inc(&conn_id
->refcount
);
1780 ret
= conn_id
->id
.event_handler(&conn_id
->id
, &event
);
1784 * Acquire mutex to prevent user executing rdma_destroy_id()
1785 * while we're accessing the cm_id.
1788 if (cma_comp(conn_id
, RDMA_CM_CONNECT
) &&
1789 (conn_id
->id
.qp_type
!= IB_QPT_UD
))
1790 ib_send_cm_mra(cm_id
, CMA_CM_MRA_SETTING
, NULL
, 0);
1791 mutex_unlock(&lock
);
1792 mutex_unlock(&conn_id
->handler_mutex
);
1793 mutex_unlock(&listen_id
->handler_mutex
);
1794 cma_deref_id(conn_id
);
1800 cma_deref_id(conn_id
);
1801 /* Destroy the CM ID by returning a non-zero value. */
1802 conn_id
->cm_id
.ib
= NULL
;
1804 cma_exch(conn_id
, RDMA_CM_DESTROYING
);
1805 mutex_unlock(&conn_id
->handler_mutex
);
1807 mutex_unlock(&listen_id
->handler_mutex
);
1809 rdma_destroy_id(&conn_id
->id
);
1818 __be64
rdma_get_service_id(struct rdma_cm_id
*id
, struct sockaddr
*addr
)
1820 if (addr
->sa_family
== AF_IB
)
1821 return ((struct sockaddr_ib
*) addr
)->sib_sid
;
1823 return cpu_to_be64(((u64
)id
->ps
<< 16) + be16_to_cpu(cma_port(addr
)));
1825 EXPORT_SYMBOL(rdma_get_service_id
);
1827 static int cma_iw_handler(struct iw_cm_id
*iw_id
, struct iw_cm_event
*iw_event
)
1829 struct rdma_id_private
*id_priv
= iw_id
->context
;
1830 struct rdma_cm_event event
;
1832 struct sockaddr
*laddr
= (struct sockaddr
*)&iw_event
->local_addr
;
1833 struct sockaddr
*raddr
= (struct sockaddr
*)&iw_event
->remote_addr
;
1835 if (cma_disable_callback(id_priv
, RDMA_CM_CONNECT
))
1838 memset(&event
, 0, sizeof event
);
1839 switch (iw_event
->event
) {
1840 case IW_CM_EVENT_CLOSE
:
1841 event
.event
= RDMA_CM_EVENT_DISCONNECTED
;
1843 case IW_CM_EVENT_CONNECT_REPLY
:
1844 memcpy(cma_src_addr(id_priv
), laddr
,
1845 rdma_addr_size(laddr
));
1846 memcpy(cma_dst_addr(id_priv
), raddr
,
1847 rdma_addr_size(raddr
));
1848 switch (iw_event
->status
) {
1850 event
.event
= RDMA_CM_EVENT_ESTABLISHED
;
1851 event
.param
.conn
.initiator_depth
= iw_event
->ird
;
1852 event
.param
.conn
.responder_resources
= iw_event
->ord
;
1856 event
.event
= RDMA_CM_EVENT_REJECTED
;
1859 event
.event
= RDMA_CM_EVENT_UNREACHABLE
;
1862 event
.event
= RDMA_CM_EVENT_CONNECT_ERROR
;
1866 case IW_CM_EVENT_ESTABLISHED
:
1867 event
.event
= RDMA_CM_EVENT_ESTABLISHED
;
1868 event
.param
.conn
.initiator_depth
= iw_event
->ird
;
1869 event
.param
.conn
.responder_resources
= iw_event
->ord
;
1875 event
.status
= iw_event
->status
;
1876 event
.param
.conn
.private_data
= iw_event
->private_data
;
1877 event
.param
.conn
.private_data_len
= iw_event
->private_data_len
;
1878 ret
= id_priv
->id
.event_handler(&id_priv
->id
, &event
);
1880 /* Destroy the CM ID by returning a non-zero value. */
1881 id_priv
->cm_id
.iw
= NULL
;
1882 cma_exch(id_priv
, RDMA_CM_DESTROYING
);
1883 mutex_unlock(&id_priv
->handler_mutex
);
1884 rdma_destroy_id(&id_priv
->id
);
1888 mutex_unlock(&id_priv
->handler_mutex
);
1892 static int iw_conn_req_handler(struct iw_cm_id
*cm_id
,
1893 struct iw_cm_event
*iw_event
)
1895 struct rdma_cm_id
*new_cm_id
;
1896 struct rdma_id_private
*listen_id
, *conn_id
;
1897 struct rdma_cm_event event
;
1899 struct ib_device_attr attr
;
1900 struct sockaddr
*laddr
= (struct sockaddr
*)&iw_event
->local_addr
;
1901 struct sockaddr
*raddr
= (struct sockaddr
*)&iw_event
->remote_addr
;
1903 listen_id
= cm_id
->context
;
1904 if (cma_disable_callback(listen_id
, RDMA_CM_LISTEN
))
1905 return -ECONNABORTED
;
1907 /* Create a new RDMA id for the new IW CM ID */
1908 new_cm_id
= rdma_create_id(listen_id
->id
.route
.addr
.dev_addr
.net
,
1909 listen_id
->id
.event_handler
,
1910 listen_id
->id
.context
,
1911 RDMA_PS_TCP
, IB_QPT_RC
);
1912 if (IS_ERR(new_cm_id
)) {
1916 conn_id
= container_of(new_cm_id
, struct rdma_id_private
, id
);
1917 mutex_lock_nested(&conn_id
->handler_mutex
, SINGLE_DEPTH_NESTING
);
1918 conn_id
->state
= RDMA_CM_CONNECT
;
1920 ret
= rdma_translate_ip(laddr
, &conn_id
->id
.route
.addr
.dev_addr
, NULL
);
1922 mutex_unlock(&conn_id
->handler_mutex
);
1923 rdma_destroy_id(new_cm_id
);
1927 ret
= cma_acquire_dev(conn_id
, listen_id
);
1929 mutex_unlock(&conn_id
->handler_mutex
);
1930 rdma_destroy_id(new_cm_id
);
1934 conn_id
->cm_id
.iw
= cm_id
;
1935 cm_id
->context
= conn_id
;
1936 cm_id
->cm_handler
= cma_iw_handler
;
1938 memcpy(cma_src_addr(conn_id
), laddr
, rdma_addr_size(laddr
));
1939 memcpy(cma_dst_addr(conn_id
), raddr
, rdma_addr_size(raddr
));
1941 ret
= ib_query_device(conn_id
->id
.device
, &attr
);
1943 mutex_unlock(&conn_id
->handler_mutex
);
1944 rdma_destroy_id(new_cm_id
);
1948 memset(&event
, 0, sizeof event
);
1949 event
.event
= RDMA_CM_EVENT_CONNECT_REQUEST
;
1950 event
.param
.conn
.private_data
= iw_event
->private_data
;
1951 event
.param
.conn
.private_data_len
= iw_event
->private_data_len
;
1952 event
.param
.conn
.initiator_depth
= iw_event
->ird
;
1953 event
.param
.conn
.responder_resources
= iw_event
->ord
;
1956 * Protect against the user destroying conn_id from another thread
1957 * until we're done accessing it.
1959 atomic_inc(&conn_id
->refcount
);
1960 ret
= conn_id
->id
.event_handler(&conn_id
->id
, &event
);
1962 /* User wants to destroy the CM ID */
1963 conn_id
->cm_id
.iw
= NULL
;
1964 cma_exch(conn_id
, RDMA_CM_DESTROYING
);
1965 mutex_unlock(&conn_id
->handler_mutex
);
1966 cma_deref_id(conn_id
);
1967 rdma_destroy_id(&conn_id
->id
);
1971 mutex_unlock(&conn_id
->handler_mutex
);
1972 cma_deref_id(conn_id
);
1975 mutex_unlock(&listen_id
->handler_mutex
);
1979 static int cma_ib_listen(struct rdma_id_private
*id_priv
)
1981 struct sockaddr
*addr
;
1982 struct ib_cm_id
*id
;
1985 addr
= cma_src_addr(id_priv
);
1986 svc_id
= rdma_get_service_id(&id_priv
->id
, addr
);
1987 id
= ib_cm_insert_listen(id_priv
->id
.device
, cma_req_handler
, svc_id
);
1990 id_priv
->cm_id
.ib
= id
;
1995 static int cma_iw_listen(struct rdma_id_private
*id_priv
, int backlog
)
1998 struct iw_cm_id
*id
;
2000 id
= iw_create_cm_id(id_priv
->id
.device
,
2001 iw_conn_req_handler
,
2006 id
->tos
= id_priv
->tos
;
2007 id_priv
->cm_id
.iw
= id
;
2009 memcpy(&id_priv
->cm_id
.iw
->local_addr
, cma_src_addr(id_priv
),
2010 rdma_addr_size(cma_src_addr(id_priv
)));
2012 ret
= iw_cm_listen(id_priv
->cm_id
.iw
, backlog
);
2015 iw_destroy_cm_id(id_priv
->cm_id
.iw
);
2016 id_priv
->cm_id
.iw
= NULL
;
2022 static int cma_listen_handler(struct rdma_cm_id
*id
,
2023 struct rdma_cm_event
*event
)
2025 struct rdma_id_private
*id_priv
= id
->context
;
2027 id
->context
= id_priv
->id
.context
;
2028 id
->event_handler
= id_priv
->id
.event_handler
;
2029 return id_priv
->id
.event_handler(id
, event
);
2032 static void cma_listen_on_dev(struct rdma_id_private
*id_priv
,
2033 struct cma_device
*cma_dev
)
2035 struct rdma_id_private
*dev_id_priv
;
2036 struct rdma_cm_id
*id
;
2037 struct net
*net
= id_priv
->id
.route
.addr
.dev_addr
.net
;
2040 if (cma_family(id_priv
) == AF_IB
&& !rdma_cap_ib_cm(cma_dev
->device
, 1))
2043 id
= rdma_create_id(net
, cma_listen_handler
, id_priv
, id_priv
->id
.ps
,
2044 id_priv
->id
.qp_type
);
2048 dev_id_priv
= container_of(id
, struct rdma_id_private
, id
);
2050 dev_id_priv
->state
= RDMA_CM_ADDR_BOUND
;
2051 memcpy(cma_src_addr(dev_id_priv
), cma_src_addr(id_priv
),
2052 rdma_addr_size(cma_src_addr(id_priv
)));
2054 cma_attach_to_dev(dev_id_priv
, cma_dev
);
2055 list_add_tail(&dev_id_priv
->listen_list
, &id_priv
->listen_list
);
2056 atomic_inc(&id_priv
->refcount
);
2057 dev_id_priv
->internal_id
= 1;
2058 dev_id_priv
->afonly
= id_priv
->afonly
;
2060 ret
= rdma_listen(id
, id_priv
->backlog
);
2062 printk(KERN_WARNING
"RDMA CMA: cma_listen_on_dev, error %d, "
2063 "listening on device %s\n", ret
, cma_dev
->device
->name
);
2066 static void cma_listen_on_all(struct rdma_id_private
*id_priv
)
2068 struct cma_device
*cma_dev
;
2071 list_add_tail(&id_priv
->list
, &listen_any_list
);
2072 list_for_each_entry(cma_dev
, &dev_list
, list
)
2073 cma_listen_on_dev(id_priv
, cma_dev
);
2074 mutex_unlock(&lock
);
2077 void rdma_set_service_type(struct rdma_cm_id
*id
, int tos
)
2079 struct rdma_id_private
*id_priv
;
2081 id_priv
= container_of(id
, struct rdma_id_private
, id
);
2082 id_priv
->tos
= (u8
) tos
;
2084 EXPORT_SYMBOL(rdma_set_service_type
);
2086 static void cma_query_handler(int status
, struct ib_sa_path_rec
*path_rec
,
2089 struct cma_work
*work
= context
;
2090 struct rdma_route
*route
;
2092 route
= &work
->id
->id
.route
;
2095 route
->num_paths
= 1;
2096 *route
->path_rec
= *path_rec
;
2098 work
->old_state
= RDMA_CM_ROUTE_QUERY
;
2099 work
->new_state
= RDMA_CM_ADDR_RESOLVED
;
2100 work
->event
.event
= RDMA_CM_EVENT_ROUTE_ERROR
;
2101 work
->event
.status
= status
;
2104 queue_work(cma_wq
, &work
->work
);
2107 static int cma_query_ib_route(struct rdma_id_private
*id_priv
, int timeout_ms
,
2108 struct cma_work
*work
)
2110 struct rdma_dev_addr
*dev_addr
= &id_priv
->id
.route
.addr
.dev_addr
;
2111 struct ib_sa_path_rec path_rec
;
2112 ib_sa_comp_mask comp_mask
;
2113 struct sockaddr_in6
*sin6
;
2114 struct sockaddr_ib
*sib
;
2116 memset(&path_rec
, 0, sizeof path_rec
);
2117 rdma_addr_get_sgid(dev_addr
, &path_rec
.sgid
);
2118 rdma_addr_get_dgid(dev_addr
, &path_rec
.dgid
);
2119 path_rec
.pkey
= cpu_to_be16(ib_addr_get_pkey(dev_addr
));
2120 path_rec
.numb_path
= 1;
2121 path_rec
.reversible
= 1;
2122 path_rec
.service_id
= rdma_get_service_id(&id_priv
->id
, cma_dst_addr(id_priv
));
2124 comp_mask
= IB_SA_PATH_REC_DGID
| IB_SA_PATH_REC_SGID
|
2125 IB_SA_PATH_REC_PKEY
| IB_SA_PATH_REC_NUMB_PATH
|
2126 IB_SA_PATH_REC_REVERSIBLE
| IB_SA_PATH_REC_SERVICE_ID
;
2128 switch (cma_family(id_priv
)) {
2130 path_rec
.qos_class
= cpu_to_be16((u16
) id_priv
->tos
);
2131 comp_mask
|= IB_SA_PATH_REC_QOS_CLASS
;
2134 sin6
= (struct sockaddr_in6
*) cma_src_addr(id_priv
);
2135 path_rec
.traffic_class
= (u8
) (be32_to_cpu(sin6
->sin6_flowinfo
) >> 20);
2136 comp_mask
|= IB_SA_PATH_REC_TRAFFIC_CLASS
;
2139 sib
= (struct sockaddr_ib
*) cma_src_addr(id_priv
);
2140 path_rec
.traffic_class
= (u8
) (be32_to_cpu(sib
->sib_flowinfo
) >> 20);
2141 comp_mask
|= IB_SA_PATH_REC_TRAFFIC_CLASS
;
2145 id_priv
->query_id
= ib_sa_path_rec_get(&sa_client
, id_priv
->id
.device
,
2146 id_priv
->id
.port_num
, &path_rec
,
2147 comp_mask
, timeout_ms
,
2148 GFP_KERNEL
, cma_query_handler
,
2149 work
, &id_priv
->query
);
2151 return (id_priv
->query_id
< 0) ? id_priv
->query_id
: 0;
2154 static void cma_work_handler(struct work_struct
*_work
)
2156 struct cma_work
*work
= container_of(_work
, struct cma_work
, work
);
2157 struct rdma_id_private
*id_priv
= work
->id
;
2160 mutex_lock(&id_priv
->handler_mutex
);
2161 if (!cma_comp_exch(id_priv
, work
->old_state
, work
->new_state
))
2164 if (id_priv
->id
.event_handler(&id_priv
->id
, &work
->event
)) {
2165 cma_exch(id_priv
, RDMA_CM_DESTROYING
);
2169 mutex_unlock(&id_priv
->handler_mutex
);
2170 cma_deref_id(id_priv
);
2172 rdma_destroy_id(&id_priv
->id
);
2176 static void cma_ndev_work_handler(struct work_struct
*_work
)
2178 struct cma_ndev_work
*work
= container_of(_work
, struct cma_ndev_work
, work
);
2179 struct rdma_id_private
*id_priv
= work
->id
;
2182 mutex_lock(&id_priv
->handler_mutex
);
2183 if (id_priv
->state
== RDMA_CM_DESTROYING
||
2184 id_priv
->state
== RDMA_CM_DEVICE_REMOVAL
)
2187 if (id_priv
->id
.event_handler(&id_priv
->id
, &work
->event
)) {
2188 cma_exch(id_priv
, RDMA_CM_DESTROYING
);
2193 mutex_unlock(&id_priv
->handler_mutex
);
2194 cma_deref_id(id_priv
);
2196 rdma_destroy_id(&id_priv
->id
);
2200 static int cma_resolve_ib_route(struct rdma_id_private
*id_priv
, int timeout_ms
)
2202 struct rdma_route
*route
= &id_priv
->id
.route
;
2203 struct cma_work
*work
;
2206 work
= kzalloc(sizeof *work
, GFP_KERNEL
);
2211 INIT_WORK(&work
->work
, cma_work_handler
);
2212 work
->old_state
= RDMA_CM_ROUTE_QUERY
;
2213 work
->new_state
= RDMA_CM_ROUTE_RESOLVED
;
2214 work
->event
.event
= RDMA_CM_EVENT_ROUTE_RESOLVED
;
2216 route
->path_rec
= kmalloc(sizeof *route
->path_rec
, GFP_KERNEL
);
2217 if (!route
->path_rec
) {
2222 ret
= cma_query_ib_route(id_priv
, timeout_ms
, work
);
2228 kfree(route
->path_rec
);
2229 route
->path_rec
= NULL
;
2235 int rdma_set_ib_paths(struct rdma_cm_id
*id
,
2236 struct ib_sa_path_rec
*path_rec
, int num_paths
)
2238 struct rdma_id_private
*id_priv
;
2241 id_priv
= container_of(id
, struct rdma_id_private
, id
);
2242 if (!cma_comp_exch(id_priv
, RDMA_CM_ADDR_RESOLVED
,
2243 RDMA_CM_ROUTE_RESOLVED
))
2246 id
->route
.path_rec
= kmemdup(path_rec
, sizeof *path_rec
* num_paths
,
2248 if (!id
->route
.path_rec
) {
2253 id
->route
.num_paths
= num_paths
;
2256 cma_comp_exch(id_priv
, RDMA_CM_ROUTE_RESOLVED
, RDMA_CM_ADDR_RESOLVED
);
2259 EXPORT_SYMBOL(rdma_set_ib_paths
);
2261 static int cma_resolve_iw_route(struct rdma_id_private
*id_priv
, int timeout_ms
)
2263 struct cma_work
*work
;
2265 work
= kzalloc(sizeof *work
, GFP_KERNEL
);
2270 INIT_WORK(&work
->work
, cma_work_handler
);
2271 work
->old_state
= RDMA_CM_ROUTE_QUERY
;
2272 work
->new_state
= RDMA_CM_ROUTE_RESOLVED
;
2273 work
->event
.event
= RDMA_CM_EVENT_ROUTE_RESOLVED
;
2274 queue_work(cma_wq
, &work
->work
);
2278 static int iboe_tos_to_sl(struct net_device
*ndev
, int tos
)
2281 struct net_device
*dev
;
2283 prio
= rt_tos2priority(tos
);
2284 dev
= ndev
->priv_flags
& IFF_802_1Q_VLAN
?
2285 vlan_dev_real_dev(ndev
) : ndev
;
2288 return netdev_get_prio_tc_map(dev
, prio
);
2290 #if IS_ENABLED(CONFIG_VLAN_8021Q)
2291 if (ndev
->priv_flags
& IFF_802_1Q_VLAN
)
2292 return (vlan_dev_get_egress_qos_mask(ndev
, prio
) &
2293 VLAN_PRIO_MASK
) >> VLAN_PRIO_SHIFT
;
2298 static int cma_resolve_iboe_route(struct rdma_id_private
*id_priv
)
2300 struct rdma_route
*route
= &id_priv
->id
.route
;
2301 struct rdma_addr
*addr
= &route
->addr
;
2302 struct cma_work
*work
;
2304 struct net_device
*ndev
= NULL
;
2307 work
= kzalloc(sizeof *work
, GFP_KERNEL
);
2312 INIT_WORK(&work
->work
, cma_work_handler
);
2314 route
->path_rec
= kzalloc(sizeof *route
->path_rec
, GFP_KERNEL
);
2315 if (!route
->path_rec
) {
2320 route
->num_paths
= 1;
2322 if (addr
->dev_addr
.bound_dev_if
) {
2323 ndev
= dev_get_by_index(&init_net
, addr
->dev_addr
.bound_dev_if
);
2324 route
->path_rec
->net
= &init_net
;
2325 route
->path_rec
->ifindex
= addr
->dev_addr
.bound_dev_if
;
2332 memcpy(route
->path_rec
->dmac
, addr
->dev_addr
.dst_dev_addr
, ETH_ALEN
);
2334 rdma_ip2gid((struct sockaddr
*)&id_priv
->id
.route
.addr
.src_addr
,
2335 &route
->path_rec
->sgid
);
2336 rdma_ip2gid((struct sockaddr
*)&id_priv
->id
.route
.addr
.dst_addr
,
2337 &route
->path_rec
->dgid
);
2339 route
->path_rec
->hop_limit
= 1;
2340 route
->path_rec
->reversible
= 1;
2341 route
->path_rec
->pkey
= cpu_to_be16(0xffff);
2342 route
->path_rec
->mtu_selector
= IB_SA_EQ
;
2343 route
->path_rec
->sl
= iboe_tos_to_sl(ndev
, id_priv
->tos
);
2344 route
->path_rec
->mtu
= iboe_get_mtu(ndev
->mtu
);
2345 route
->path_rec
->rate_selector
= IB_SA_EQ
;
2346 route
->path_rec
->rate
= iboe_get_rate(ndev
);
2348 route
->path_rec
->packet_life_time_selector
= IB_SA_EQ
;
2349 route
->path_rec
->packet_life_time
= CMA_IBOE_PACKET_LIFETIME
;
2350 if (!route
->path_rec
->mtu
) {
2355 work
->old_state
= RDMA_CM_ROUTE_QUERY
;
2356 work
->new_state
= RDMA_CM_ROUTE_RESOLVED
;
2357 work
->event
.event
= RDMA_CM_EVENT_ROUTE_RESOLVED
;
2358 work
->event
.status
= 0;
2360 queue_work(cma_wq
, &work
->work
);
2365 kfree(route
->path_rec
);
2366 route
->path_rec
= NULL
;
2372 int rdma_resolve_route(struct rdma_cm_id
*id
, int timeout_ms
)
2374 struct rdma_id_private
*id_priv
;
2377 id_priv
= container_of(id
, struct rdma_id_private
, id
);
2378 if (!cma_comp_exch(id_priv
, RDMA_CM_ADDR_RESOLVED
, RDMA_CM_ROUTE_QUERY
))
2381 atomic_inc(&id_priv
->refcount
);
2382 if (rdma_cap_ib_sa(id
->device
, id
->port_num
))
2383 ret
= cma_resolve_ib_route(id_priv
, timeout_ms
);
2384 else if (rdma_protocol_roce(id
->device
, id
->port_num
))
2385 ret
= cma_resolve_iboe_route(id_priv
);
2386 else if (rdma_protocol_iwarp(id
->device
, id
->port_num
))
2387 ret
= cma_resolve_iw_route(id_priv
, timeout_ms
);
2396 cma_comp_exch(id_priv
, RDMA_CM_ROUTE_QUERY
, RDMA_CM_ADDR_RESOLVED
);
2397 cma_deref_id(id_priv
);
2400 EXPORT_SYMBOL(rdma_resolve_route
);
2402 static void cma_set_loopback(struct sockaddr
*addr
)
2404 switch (addr
->sa_family
) {
2406 ((struct sockaddr_in
*) addr
)->sin_addr
.s_addr
= htonl(INADDR_LOOPBACK
);
2409 ipv6_addr_set(&((struct sockaddr_in6
*) addr
)->sin6_addr
,
2413 ib_addr_set(&((struct sockaddr_ib
*) addr
)->sib_addr
,
2419 static int cma_bind_loopback(struct rdma_id_private
*id_priv
)
2421 struct cma_device
*cma_dev
, *cur_dev
;
2422 struct ib_port_attr port_attr
;
2430 list_for_each_entry(cur_dev
, &dev_list
, list
) {
2431 if (cma_family(id_priv
) == AF_IB
&&
2432 !rdma_cap_ib_cm(cur_dev
->device
, 1))
2438 for (p
= 1; p
<= cur_dev
->device
->phys_port_cnt
; ++p
) {
2439 if (!ib_query_port(cur_dev
->device
, p
, &port_attr
) &&
2440 port_attr
.state
== IB_PORT_ACTIVE
) {
2455 ret
= ib_get_cached_gid(cma_dev
->device
, p
, 0, &gid
, NULL
);
2459 ret
= ib_get_cached_pkey(cma_dev
->device
, p
, 0, &pkey
);
2463 id_priv
->id
.route
.addr
.dev_addr
.dev_type
=
2464 (rdma_protocol_ib(cma_dev
->device
, p
)) ?
2465 ARPHRD_INFINIBAND
: ARPHRD_ETHER
;
2467 rdma_addr_set_sgid(&id_priv
->id
.route
.addr
.dev_addr
, &gid
);
2468 ib_addr_set_pkey(&id_priv
->id
.route
.addr
.dev_addr
, pkey
);
2469 id_priv
->id
.port_num
= p
;
2470 cma_attach_to_dev(id_priv
, cma_dev
);
2471 cma_set_loopback(cma_src_addr(id_priv
));
2473 mutex_unlock(&lock
);
2477 static void addr_handler(int status
, struct sockaddr
*src_addr
,
2478 struct rdma_dev_addr
*dev_addr
, void *context
)
2480 struct rdma_id_private
*id_priv
= context
;
2481 struct rdma_cm_event event
;
2483 memset(&event
, 0, sizeof event
);
2484 mutex_lock(&id_priv
->handler_mutex
);
2485 if (!cma_comp_exch(id_priv
, RDMA_CM_ADDR_QUERY
,
2486 RDMA_CM_ADDR_RESOLVED
))
2489 memcpy(cma_src_addr(id_priv
), src_addr
, rdma_addr_size(src_addr
));
2490 if (!status
&& !id_priv
->cma_dev
)
2491 status
= cma_acquire_dev(id_priv
, NULL
);
2494 if (!cma_comp_exch(id_priv
, RDMA_CM_ADDR_RESOLVED
,
2495 RDMA_CM_ADDR_BOUND
))
2497 event
.event
= RDMA_CM_EVENT_ADDR_ERROR
;
2498 event
.status
= status
;
2500 event
.event
= RDMA_CM_EVENT_ADDR_RESOLVED
;
2502 if (id_priv
->id
.event_handler(&id_priv
->id
, &event
)) {
2503 cma_exch(id_priv
, RDMA_CM_DESTROYING
);
2504 mutex_unlock(&id_priv
->handler_mutex
);
2505 cma_deref_id(id_priv
);
2506 rdma_destroy_id(&id_priv
->id
);
2510 mutex_unlock(&id_priv
->handler_mutex
);
2511 cma_deref_id(id_priv
);
2514 static int cma_resolve_loopback(struct rdma_id_private
*id_priv
)
2516 struct cma_work
*work
;
2520 work
= kzalloc(sizeof *work
, GFP_KERNEL
);
2524 if (!id_priv
->cma_dev
) {
2525 ret
= cma_bind_loopback(id_priv
);
2530 rdma_addr_get_sgid(&id_priv
->id
.route
.addr
.dev_addr
, &gid
);
2531 rdma_addr_set_dgid(&id_priv
->id
.route
.addr
.dev_addr
, &gid
);
2534 INIT_WORK(&work
->work
, cma_work_handler
);
2535 work
->old_state
= RDMA_CM_ADDR_QUERY
;
2536 work
->new_state
= RDMA_CM_ADDR_RESOLVED
;
2537 work
->event
.event
= RDMA_CM_EVENT_ADDR_RESOLVED
;
2538 queue_work(cma_wq
, &work
->work
);
2545 static int cma_resolve_ib_addr(struct rdma_id_private
*id_priv
)
2547 struct cma_work
*work
;
2550 work
= kzalloc(sizeof *work
, GFP_KERNEL
);
2554 if (!id_priv
->cma_dev
) {
2555 ret
= cma_resolve_ib_dev(id_priv
);
2560 rdma_addr_set_dgid(&id_priv
->id
.route
.addr
.dev_addr
, (union ib_gid
*)
2561 &(((struct sockaddr_ib
*) &id_priv
->id
.route
.addr
.dst_addr
)->sib_addr
));
2564 INIT_WORK(&work
->work
, cma_work_handler
);
2565 work
->old_state
= RDMA_CM_ADDR_QUERY
;
2566 work
->new_state
= RDMA_CM_ADDR_RESOLVED
;
2567 work
->event
.event
= RDMA_CM_EVENT_ADDR_RESOLVED
;
2568 queue_work(cma_wq
, &work
->work
);
2575 static int cma_bind_addr(struct rdma_cm_id
*id
, struct sockaddr
*src_addr
,
2576 struct sockaddr
*dst_addr
)
2578 if (!src_addr
|| !src_addr
->sa_family
) {
2579 src_addr
= (struct sockaddr
*) &id
->route
.addr
.src_addr
;
2580 src_addr
->sa_family
= dst_addr
->sa_family
;
2581 if (dst_addr
->sa_family
== AF_INET6
) {
2582 struct sockaddr_in6
*src_addr6
= (struct sockaddr_in6
*) src_addr
;
2583 struct sockaddr_in6
*dst_addr6
= (struct sockaddr_in6
*) dst_addr
;
2584 src_addr6
->sin6_scope_id
= dst_addr6
->sin6_scope_id
;
2585 if (ipv6_addr_type(&dst_addr6
->sin6_addr
) & IPV6_ADDR_LINKLOCAL
)
2586 id
->route
.addr
.dev_addr
.bound_dev_if
= dst_addr6
->sin6_scope_id
;
2587 } else if (dst_addr
->sa_family
== AF_IB
) {
2588 ((struct sockaddr_ib
*) src_addr
)->sib_pkey
=
2589 ((struct sockaddr_ib
*) dst_addr
)->sib_pkey
;
2592 return rdma_bind_addr(id
, src_addr
);
2595 int rdma_resolve_addr(struct rdma_cm_id
*id
, struct sockaddr
*src_addr
,
2596 struct sockaddr
*dst_addr
, int timeout_ms
)
2598 struct rdma_id_private
*id_priv
;
2601 id_priv
= container_of(id
, struct rdma_id_private
, id
);
2602 if (id_priv
->state
== RDMA_CM_IDLE
) {
2603 ret
= cma_bind_addr(id
, src_addr
, dst_addr
);
2608 if (cma_family(id_priv
) != dst_addr
->sa_family
)
2611 if (!cma_comp_exch(id_priv
, RDMA_CM_ADDR_BOUND
, RDMA_CM_ADDR_QUERY
))
2614 atomic_inc(&id_priv
->refcount
);
2615 memcpy(cma_dst_addr(id_priv
), dst_addr
, rdma_addr_size(dst_addr
));
2616 if (cma_any_addr(dst_addr
)) {
2617 ret
= cma_resolve_loopback(id_priv
);
2619 if (dst_addr
->sa_family
== AF_IB
) {
2620 ret
= cma_resolve_ib_addr(id_priv
);
2622 ret
= rdma_resolve_ip(&addr_client
, cma_src_addr(id_priv
),
2623 dst_addr
, &id
->route
.addr
.dev_addr
,
2624 timeout_ms
, addr_handler
, id_priv
);
2632 cma_comp_exch(id_priv
, RDMA_CM_ADDR_QUERY
, RDMA_CM_ADDR_BOUND
);
2633 cma_deref_id(id_priv
);
2636 EXPORT_SYMBOL(rdma_resolve_addr
);
2638 int rdma_set_reuseaddr(struct rdma_cm_id
*id
, int reuse
)
2640 struct rdma_id_private
*id_priv
;
2641 unsigned long flags
;
2644 id_priv
= container_of(id
, struct rdma_id_private
, id
);
2645 spin_lock_irqsave(&id_priv
->lock
, flags
);
2646 if (reuse
|| id_priv
->state
== RDMA_CM_IDLE
) {
2647 id_priv
->reuseaddr
= reuse
;
2652 spin_unlock_irqrestore(&id_priv
->lock
, flags
);
2655 EXPORT_SYMBOL(rdma_set_reuseaddr
);
2657 int rdma_set_afonly(struct rdma_cm_id
*id
, int afonly
)
2659 struct rdma_id_private
*id_priv
;
2660 unsigned long flags
;
2663 id_priv
= container_of(id
, struct rdma_id_private
, id
);
2664 spin_lock_irqsave(&id_priv
->lock
, flags
);
2665 if (id_priv
->state
== RDMA_CM_IDLE
|| id_priv
->state
== RDMA_CM_ADDR_BOUND
) {
2666 id_priv
->options
|= (1 << CMA_OPTION_AFONLY
);
2667 id_priv
->afonly
= afonly
;
2672 spin_unlock_irqrestore(&id_priv
->lock
, flags
);
2675 EXPORT_SYMBOL(rdma_set_afonly
);
2677 static void cma_bind_port(struct rdma_bind_list
*bind_list
,
2678 struct rdma_id_private
*id_priv
)
2680 struct sockaddr
*addr
;
2681 struct sockaddr_ib
*sib
;
2685 addr
= cma_src_addr(id_priv
);
2686 port
= htons(bind_list
->port
);
2688 switch (addr
->sa_family
) {
2690 ((struct sockaddr_in
*) addr
)->sin_port
= port
;
2693 ((struct sockaddr_in6
*) addr
)->sin6_port
= port
;
2696 sib
= (struct sockaddr_ib
*) addr
;
2697 sid
= be64_to_cpu(sib
->sib_sid
);
2698 mask
= be64_to_cpu(sib
->sib_sid_mask
);
2699 sib
->sib_sid
= cpu_to_be64((sid
& mask
) | (u64
) ntohs(port
));
2700 sib
->sib_sid_mask
= cpu_to_be64(~0ULL);
2703 id_priv
->bind_list
= bind_list
;
2704 hlist_add_head(&id_priv
->node
, &bind_list
->owners
);
2707 static int cma_alloc_port(enum rdma_port_space ps
,
2708 struct rdma_id_private
*id_priv
, unsigned short snum
)
2710 struct rdma_bind_list
*bind_list
;
2713 bind_list
= kzalloc(sizeof *bind_list
, GFP_KERNEL
);
2717 ret
= cma_ps_alloc(id_priv
->id
.route
.addr
.dev_addr
.net
, ps
, bind_list
,
2723 bind_list
->port
= (unsigned short)ret
;
2724 cma_bind_port(bind_list
, id_priv
);
2728 return ret
== -ENOSPC
? -EADDRNOTAVAIL
: ret
;
2731 static int cma_alloc_any_port(enum rdma_port_space ps
,
2732 struct rdma_id_private
*id_priv
)
2734 static unsigned int last_used_port
;
2735 int low
, high
, remaining
;
2737 struct net
*net
= id_priv
->id
.route
.addr
.dev_addr
.net
;
2739 inet_get_local_port_range(net
, &low
, &high
);
2740 remaining
= (high
- low
) + 1;
2741 rover
= prandom_u32() % remaining
+ low
;
2743 if (last_used_port
!= rover
&&
2744 !cma_ps_find(net
, ps
, (unsigned short)rover
)) {
2745 int ret
= cma_alloc_port(ps
, id_priv
, rover
);
2747 * Remember previously used port number in order to avoid
2748 * re-using same port immediately after it is closed.
2751 last_used_port
= rover
;
2752 if (ret
!= -EADDRNOTAVAIL
)
2757 if ((rover
< low
) || (rover
> high
))
2761 return -EADDRNOTAVAIL
;
2765 * Check that the requested port is available. This is called when trying to
2766 * bind to a specific port, or when trying to listen on a bound port. In
2767 * the latter case, the provided id_priv may already be on the bind_list, but
2768 * we still need to check that it's okay to start listening.
2770 static int cma_check_port(struct rdma_bind_list
*bind_list
,
2771 struct rdma_id_private
*id_priv
, uint8_t reuseaddr
)
2773 struct rdma_id_private
*cur_id
;
2774 struct sockaddr
*addr
, *cur_addr
;
2776 addr
= cma_src_addr(id_priv
);
2777 hlist_for_each_entry(cur_id
, &bind_list
->owners
, node
) {
2778 if (id_priv
== cur_id
)
2781 if ((cur_id
->state
!= RDMA_CM_LISTEN
) && reuseaddr
&&
2785 cur_addr
= cma_src_addr(cur_id
);
2786 if (id_priv
->afonly
&& cur_id
->afonly
&&
2787 (addr
->sa_family
!= cur_addr
->sa_family
))
2790 if (cma_any_addr(addr
) || cma_any_addr(cur_addr
))
2791 return -EADDRNOTAVAIL
;
2793 if (!cma_addr_cmp(addr
, cur_addr
))
2799 static int cma_use_port(enum rdma_port_space ps
,
2800 struct rdma_id_private
*id_priv
)
2802 struct rdma_bind_list
*bind_list
;
2803 unsigned short snum
;
2806 snum
= ntohs(cma_port(cma_src_addr(id_priv
)));
2807 if (snum
< PROT_SOCK
&& !capable(CAP_NET_BIND_SERVICE
))
2810 bind_list
= cma_ps_find(id_priv
->id
.route
.addr
.dev_addr
.net
, ps
, snum
);
2812 ret
= cma_alloc_port(ps
, id_priv
, snum
);
2814 ret
= cma_check_port(bind_list
, id_priv
, id_priv
->reuseaddr
);
2816 cma_bind_port(bind_list
, id_priv
);
2821 static int cma_bind_listen(struct rdma_id_private
*id_priv
)
2823 struct rdma_bind_list
*bind_list
= id_priv
->bind_list
;
2827 if (bind_list
->owners
.first
->next
)
2828 ret
= cma_check_port(bind_list
, id_priv
, 0);
2829 mutex_unlock(&lock
);
2833 static enum rdma_port_space
cma_select_inet_ps(
2834 struct rdma_id_private
*id_priv
)
2836 switch (id_priv
->id
.ps
) {
2841 return id_priv
->id
.ps
;
2848 static enum rdma_port_space
cma_select_ib_ps(struct rdma_id_private
*id_priv
)
2850 enum rdma_port_space ps
= 0;
2851 struct sockaddr_ib
*sib
;
2852 u64 sid_ps
, mask
, sid
;
2854 sib
= (struct sockaddr_ib
*) cma_src_addr(id_priv
);
2855 mask
= be64_to_cpu(sib
->sib_sid_mask
) & RDMA_IB_IP_PS_MASK
;
2856 sid
= be64_to_cpu(sib
->sib_sid
) & mask
;
2858 if ((id_priv
->id
.ps
== RDMA_PS_IB
) && (sid
== (RDMA_IB_IP_PS_IB
& mask
))) {
2859 sid_ps
= RDMA_IB_IP_PS_IB
;
2861 } else if (((id_priv
->id
.ps
== RDMA_PS_IB
) || (id_priv
->id
.ps
== RDMA_PS_TCP
)) &&
2862 (sid
== (RDMA_IB_IP_PS_TCP
& mask
))) {
2863 sid_ps
= RDMA_IB_IP_PS_TCP
;
2865 } else if (((id_priv
->id
.ps
== RDMA_PS_IB
) || (id_priv
->id
.ps
== RDMA_PS_UDP
)) &&
2866 (sid
== (RDMA_IB_IP_PS_UDP
& mask
))) {
2867 sid_ps
= RDMA_IB_IP_PS_UDP
;
2872 sib
->sib_sid
= cpu_to_be64(sid_ps
| ntohs(cma_port((struct sockaddr
*) sib
)));
2873 sib
->sib_sid_mask
= cpu_to_be64(RDMA_IB_IP_PS_MASK
|
2874 be64_to_cpu(sib
->sib_sid_mask
));
2879 static int cma_get_port(struct rdma_id_private
*id_priv
)
2881 enum rdma_port_space ps
;
2884 if (cma_family(id_priv
) != AF_IB
)
2885 ps
= cma_select_inet_ps(id_priv
);
2887 ps
= cma_select_ib_ps(id_priv
);
2889 return -EPROTONOSUPPORT
;
2892 if (cma_any_port(cma_src_addr(id_priv
)))
2893 ret
= cma_alloc_any_port(ps
, id_priv
);
2895 ret
= cma_use_port(ps
, id_priv
);
2896 mutex_unlock(&lock
);
2901 static int cma_check_linklocal(struct rdma_dev_addr
*dev_addr
,
2902 struct sockaddr
*addr
)
2904 #if IS_ENABLED(CONFIG_IPV6)
2905 struct sockaddr_in6
*sin6
;
2907 if (addr
->sa_family
!= AF_INET6
)
2910 sin6
= (struct sockaddr_in6
*) addr
;
2912 if (!(ipv6_addr_type(&sin6
->sin6_addr
) & IPV6_ADDR_LINKLOCAL
))
2915 if (!sin6
->sin6_scope_id
)
2918 dev_addr
->bound_dev_if
= sin6
->sin6_scope_id
;
2923 int rdma_listen(struct rdma_cm_id
*id
, int backlog
)
2925 struct rdma_id_private
*id_priv
;
2928 id_priv
= container_of(id
, struct rdma_id_private
, id
);
2929 if (id_priv
->state
== RDMA_CM_IDLE
) {
2930 id
->route
.addr
.src_addr
.ss_family
= AF_INET
;
2931 ret
= rdma_bind_addr(id
, cma_src_addr(id_priv
));
2936 if (!cma_comp_exch(id_priv
, RDMA_CM_ADDR_BOUND
, RDMA_CM_LISTEN
))
2939 if (id_priv
->reuseaddr
) {
2940 ret
= cma_bind_listen(id_priv
);
2945 id_priv
->backlog
= backlog
;
2947 if (rdma_cap_ib_cm(id
->device
, 1)) {
2948 ret
= cma_ib_listen(id_priv
);
2951 } else if (rdma_cap_iw_cm(id
->device
, 1)) {
2952 ret
= cma_iw_listen(id_priv
, backlog
);
2960 cma_listen_on_all(id_priv
);
2964 id_priv
->backlog
= 0;
2965 cma_comp_exch(id_priv
, RDMA_CM_LISTEN
, RDMA_CM_ADDR_BOUND
);
2968 EXPORT_SYMBOL(rdma_listen
);
2970 int rdma_bind_addr(struct rdma_cm_id
*id
, struct sockaddr
*addr
)
2972 struct rdma_id_private
*id_priv
;
2975 if (addr
->sa_family
!= AF_INET
&& addr
->sa_family
!= AF_INET6
&&
2976 addr
->sa_family
!= AF_IB
)
2977 return -EAFNOSUPPORT
;
2979 id_priv
= container_of(id
, struct rdma_id_private
, id
);
2980 if (!cma_comp_exch(id_priv
, RDMA_CM_IDLE
, RDMA_CM_ADDR_BOUND
))
2983 ret
= cma_check_linklocal(&id
->route
.addr
.dev_addr
, addr
);
2987 memcpy(cma_src_addr(id_priv
), addr
, rdma_addr_size(addr
));
2988 if (!cma_any_addr(addr
)) {
2989 ret
= cma_translate_addr(addr
, &id
->route
.addr
.dev_addr
);
2993 ret
= cma_acquire_dev(id_priv
, NULL
);
2998 if (!(id_priv
->options
& (1 << CMA_OPTION_AFONLY
))) {
2999 if (addr
->sa_family
== AF_INET
)
3000 id_priv
->afonly
= 1;
3001 #if IS_ENABLED(CONFIG_IPV6)
3002 else if (addr
->sa_family
== AF_INET6
) {
3003 struct net
*net
= id_priv
->id
.route
.addr
.dev_addr
.net
;
3005 id_priv
->afonly
= net
->ipv6
.sysctl
.bindv6only
;
3009 ret
= cma_get_port(id_priv
);
3015 if (id_priv
->cma_dev
)
3016 cma_release_dev(id_priv
);
3018 cma_comp_exch(id_priv
, RDMA_CM_ADDR_BOUND
, RDMA_CM_IDLE
);
3021 EXPORT_SYMBOL(rdma_bind_addr
);
3023 static int cma_format_hdr(void *hdr
, struct rdma_id_private
*id_priv
)
3025 struct cma_hdr
*cma_hdr
;
3028 cma_hdr
->cma_version
= CMA_VERSION
;
3029 if (cma_family(id_priv
) == AF_INET
) {
3030 struct sockaddr_in
*src4
, *dst4
;
3032 src4
= (struct sockaddr_in
*) cma_src_addr(id_priv
);
3033 dst4
= (struct sockaddr_in
*) cma_dst_addr(id_priv
);
3035 cma_set_ip_ver(cma_hdr
, 4);
3036 cma_hdr
->src_addr
.ip4
.addr
= src4
->sin_addr
.s_addr
;
3037 cma_hdr
->dst_addr
.ip4
.addr
= dst4
->sin_addr
.s_addr
;
3038 cma_hdr
->port
= src4
->sin_port
;
3039 } else if (cma_family(id_priv
) == AF_INET6
) {
3040 struct sockaddr_in6
*src6
, *dst6
;
3042 src6
= (struct sockaddr_in6
*) cma_src_addr(id_priv
);
3043 dst6
= (struct sockaddr_in6
*) cma_dst_addr(id_priv
);
3045 cma_set_ip_ver(cma_hdr
, 6);
3046 cma_hdr
->src_addr
.ip6
= src6
->sin6_addr
;
3047 cma_hdr
->dst_addr
.ip6
= dst6
->sin6_addr
;
3048 cma_hdr
->port
= src6
->sin6_port
;
3053 static int cma_sidr_rep_handler(struct ib_cm_id
*cm_id
,
3054 struct ib_cm_event
*ib_event
)
3056 struct rdma_id_private
*id_priv
= cm_id
->context
;
3057 struct rdma_cm_event event
;
3058 struct ib_cm_sidr_rep_event_param
*rep
= &ib_event
->param
.sidr_rep_rcvd
;
3061 if (cma_disable_callback(id_priv
, RDMA_CM_CONNECT
))
3064 memset(&event
, 0, sizeof event
);
3065 switch (ib_event
->event
) {
3066 case IB_CM_SIDR_REQ_ERROR
:
3067 event
.event
= RDMA_CM_EVENT_UNREACHABLE
;
3068 event
.status
= -ETIMEDOUT
;
3070 case IB_CM_SIDR_REP_RECEIVED
:
3071 event
.param
.ud
.private_data
= ib_event
->private_data
;
3072 event
.param
.ud
.private_data_len
= IB_CM_SIDR_REP_PRIVATE_DATA_SIZE
;
3073 if (rep
->status
!= IB_SIDR_SUCCESS
) {
3074 event
.event
= RDMA_CM_EVENT_UNREACHABLE
;
3075 event
.status
= ib_event
->param
.sidr_rep_rcvd
.status
;
3078 ret
= cma_set_qkey(id_priv
, rep
->qkey
);
3080 event
.event
= RDMA_CM_EVENT_ADDR_ERROR
;
3084 ib_init_ah_from_path(id_priv
->id
.device
, id_priv
->id
.port_num
,
3085 id_priv
->id
.route
.path_rec
,
3086 &event
.param
.ud
.ah_attr
);
3087 event
.param
.ud
.qp_num
= rep
->qpn
;
3088 event
.param
.ud
.qkey
= rep
->qkey
;
3089 event
.event
= RDMA_CM_EVENT_ESTABLISHED
;
3093 printk(KERN_ERR
"RDMA CMA: unexpected IB CM event: %d\n",
3098 ret
= id_priv
->id
.event_handler(&id_priv
->id
, &event
);
3100 /* Destroy the CM ID by returning a non-zero value. */
3101 id_priv
->cm_id
.ib
= NULL
;
3102 cma_exch(id_priv
, RDMA_CM_DESTROYING
);
3103 mutex_unlock(&id_priv
->handler_mutex
);
3104 rdma_destroy_id(&id_priv
->id
);
3108 mutex_unlock(&id_priv
->handler_mutex
);
3112 static int cma_resolve_ib_udp(struct rdma_id_private
*id_priv
,
3113 struct rdma_conn_param
*conn_param
)
3115 struct ib_cm_sidr_req_param req
;
3116 struct ib_cm_id
*id
;
3120 memset(&req
, 0, sizeof req
);
3121 offset
= cma_user_data_offset(id_priv
);
3122 req
.private_data_len
= offset
+ conn_param
->private_data_len
;
3123 if (req
.private_data_len
< conn_param
->private_data_len
)
3126 if (req
.private_data_len
) {
3127 private_data
= kzalloc(req
.private_data_len
, GFP_ATOMIC
);
3131 private_data
= NULL
;
3134 if (conn_param
->private_data
&& conn_param
->private_data_len
)
3135 memcpy(private_data
+ offset
, conn_param
->private_data
,
3136 conn_param
->private_data_len
);
3139 ret
= cma_format_hdr(private_data
, id_priv
);
3142 req
.private_data
= private_data
;
3145 id
= ib_create_cm_id(id_priv
->id
.device
, cma_sidr_rep_handler
,
3151 id_priv
->cm_id
.ib
= id
;
3153 req
.path
= id_priv
->id
.route
.path_rec
;
3154 req
.service_id
= rdma_get_service_id(&id_priv
->id
, cma_dst_addr(id_priv
));
3155 req
.timeout_ms
= 1 << (CMA_CM_RESPONSE_TIMEOUT
- 8);
3156 req
.max_cm_retries
= CMA_MAX_CM_RETRIES
;
3158 ret
= ib_send_cm_sidr_req(id_priv
->cm_id
.ib
, &req
);
3160 ib_destroy_cm_id(id_priv
->cm_id
.ib
);
3161 id_priv
->cm_id
.ib
= NULL
;
3164 kfree(private_data
);
3168 static int cma_connect_ib(struct rdma_id_private
*id_priv
,
3169 struct rdma_conn_param
*conn_param
)
3171 struct ib_cm_req_param req
;
3172 struct rdma_route
*route
;
3174 struct ib_cm_id
*id
;
3177 memset(&req
, 0, sizeof req
);
3178 offset
= cma_user_data_offset(id_priv
);
3179 req
.private_data_len
= offset
+ conn_param
->private_data_len
;
3180 if (req
.private_data_len
< conn_param
->private_data_len
)
3183 if (req
.private_data_len
) {
3184 private_data
= kzalloc(req
.private_data_len
, GFP_ATOMIC
);
3188 private_data
= NULL
;
3191 if (conn_param
->private_data
&& conn_param
->private_data_len
)
3192 memcpy(private_data
+ offset
, conn_param
->private_data
,
3193 conn_param
->private_data_len
);
3195 id
= ib_create_cm_id(id_priv
->id
.device
, cma_ib_handler
, id_priv
);
3200 id_priv
->cm_id
.ib
= id
;
3202 route
= &id_priv
->id
.route
;
3204 ret
= cma_format_hdr(private_data
, id_priv
);
3207 req
.private_data
= private_data
;
3210 req
.primary_path
= &route
->path_rec
[0];
3211 if (route
->num_paths
== 2)
3212 req
.alternate_path
= &route
->path_rec
[1];
3214 req
.service_id
= rdma_get_service_id(&id_priv
->id
, cma_dst_addr(id_priv
));
3215 req
.qp_num
= id_priv
->qp_num
;
3216 req
.qp_type
= id_priv
->id
.qp_type
;
3217 req
.starting_psn
= id_priv
->seq_num
;
3218 req
.responder_resources
= conn_param
->responder_resources
;
3219 req
.initiator_depth
= conn_param
->initiator_depth
;
3220 req
.flow_control
= conn_param
->flow_control
;
3221 req
.retry_count
= min_t(u8
, 7, conn_param
->retry_count
);
3222 req
.rnr_retry_count
= min_t(u8
, 7, conn_param
->rnr_retry_count
);
3223 req
.remote_cm_response_timeout
= CMA_CM_RESPONSE_TIMEOUT
;
3224 req
.local_cm_response_timeout
= CMA_CM_RESPONSE_TIMEOUT
;
3225 req
.max_cm_retries
= CMA_MAX_CM_RETRIES
;
3226 req
.srq
= id_priv
->srq
? 1 : 0;
3228 ret
= ib_send_cm_req(id_priv
->cm_id
.ib
, &req
);
3230 if (ret
&& !IS_ERR(id
)) {
3231 ib_destroy_cm_id(id
);
3232 id_priv
->cm_id
.ib
= NULL
;
3235 kfree(private_data
);
3239 static int cma_connect_iw(struct rdma_id_private
*id_priv
,
3240 struct rdma_conn_param
*conn_param
)
3242 struct iw_cm_id
*cm_id
;
3244 struct iw_cm_conn_param iw_param
;
3246 cm_id
= iw_create_cm_id(id_priv
->id
.device
, cma_iw_handler
, id_priv
);
3248 return PTR_ERR(cm_id
);
3250 cm_id
->tos
= id_priv
->tos
;
3251 id_priv
->cm_id
.iw
= cm_id
;
3253 memcpy(&cm_id
->local_addr
, cma_src_addr(id_priv
),
3254 rdma_addr_size(cma_src_addr(id_priv
)));
3255 memcpy(&cm_id
->remote_addr
, cma_dst_addr(id_priv
),
3256 rdma_addr_size(cma_dst_addr(id_priv
)));
3258 ret
= cma_modify_qp_rtr(id_priv
, conn_param
);
3263 iw_param
.ord
= conn_param
->initiator_depth
;
3264 iw_param
.ird
= conn_param
->responder_resources
;
3265 iw_param
.private_data
= conn_param
->private_data
;
3266 iw_param
.private_data_len
= conn_param
->private_data_len
;
3267 iw_param
.qpn
= id_priv
->id
.qp
? id_priv
->qp_num
: conn_param
->qp_num
;
3269 memset(&iw_param
, 0, sizeof iw_param
);
3270 iw_param
.qpn
= id_priv
->qp_num
;
3272 ret
= iw_cm_connect(cm_id
, &iw_param
);
3275 iw_destroy_cm_id(cm_id
);
3276 id_priv
->cm_id
.iw
= NULL
;
3281 int rdma_connect(struct rdma_cm_id
*id
, struct rdma_conn_param
*conn_param
)
3283 struct rdma_id_private
*id_priv
;
3286 id_priv
= container_of(id
, struct rdma_id_private
, id
);
3287 if (!cma_comp_exch(id_priv
, RDMA_CM_ROUTE_RESOLVED
, RDMA_CM_CONNECT
))
3291 id_priv
->qp_num
= conn_param
->qp_num
;
3292 id_priv
->srq
= conn_param
->srq
;
3295 if (rdma_cap_ib_cm(id
->device
, id
->port_num
)) {
3296 if (id
->qp_type
== IB_QPT_UD
)
3297 ret
= cma_resolve_ib_udp(id_priv
, conn_param
);
3299 ret
= cma_connect_ib(id_priv
, conn_param
);
3300 } else if (rdma_cap_iw_cm(id
->device
, id
->port_num
))
3301 ret
= cma_connect_iw(id_priv
, conn_param
);
3309 cma_comp_exch(id_priv
, RDMA_CM_CONNECT
, RDMA_CM_ROUTE_RESOLVED
);
3312 EXPORT_SYMBOL(rdma_connect
);
3314 static int cma_accept_ib(struct rdma_id_private
*id_priv
,
3315 struct rdma_conn_param
*conn_param
)
3317 struct ib_cm_rep_param rep
;
3320 ret
= cma_modify_qp_rtr(id_priv
, conn_param
);
3324 ret
= cma_modify_qp_rts(id_priv
, conn_param
);
3328 memset(&rep
, 0, sizeof rep
);
3329 rep
.qp_num
= id_priv
->qp_num
;
3330 rep
.starting_psn
= id_priv
->seq_num
;
3331 rep
.private_data
= conn_param
->private_data
;
3332 rep
.private_data_len
= conn_param
->private_data_len
;
3333 rep
.responder_resources
= conn_param
->responder_resources
;
3334 rep
.initiator_depth
= conn_param
->initiator_depth
;
3335 rep
.failover_accepted
= 0;
3336 rep
.flow_control
= conn_param
->flow_control
;
3337 rep
.rnr_retry_count
= min_t(u8
, 7, conn_param
->rnr_retry_count
);
3338 rep
.srq
= id_priv
->srq
? 1 : 0;
3340 ret
= ib_send_cm_rep(id_priv
->cm_id
.ib
, &rep
);
3345 static int cma_accept_iw(struct rdma_id_private
*id_priv
,
3346 struct rdma_conn_param
*conn_param
)
3348 struct iw_cm_conn_param iw_param
;
3351 ret
= cma_modify_qp_rtr(id_priv
, conn_param
);
3355 iw_param
.ord
= conn_param
->initiator_depth
;
3356 iw_param
.ird
= conn_param
->responder_resources
;
3357 iw_param
.private_data
= conn_param
->private_data
;
3358 iw_param
.private_data_len
= conn_param
->private_data_len
;
3359 if (id_priv
->id
.qp
) {
3360 iw_param
.qpn
= id_priv
->qp_num
;
3362 iw_param
.qpn
= conn_param
->qp_num
;
3364 return iw_cm_accept(id_priv
->cm_id
.iw
, &iw_param
);
3367 static int cma_send_sidr_rep(struct rdma_id_private
*id_priv
,
3368 enum ib_cm_sidr_status status
, u32 qkey
,
3369 const void *private_data
, int private_data_len
)
3371 struct ib_cm_sidr_rep_param rep
;
3374 memset(&rep
, 0, sizeof rep
);
3375 rep
.status
= status
;
3376 if (status
== IB_SIDR_SUCCESS
) {
3377 ret
= cma_set_qkey(id_priv
, qkey
);
3380 rep
.qp_num
= id_priv
->qp_num
;
3381 rep
.qkey
= id_priv
->qkey
;
3383 rep
.private_data
= private_data
;
3384 rep
.private_data_len
= private_data_len
;
3386 return ib_send_cm_sidr_rep(id_priv
->cm_id
.ib
, &rep
);
3389 int rdma_accept(struct rdma_cm_id
*id
, struct rdma_conn_param
*conn_param
)
3391 struct rdma_id_private
*id_priv
;
3394 id_priv
= container_of(id
, struct rdma_id_private
, id
);
3396 id_priv
->owner
= task_pid_nr(current
);
3398 if (!cma_comp(id_priv
, RDMA_CM_CONNECT
))
3401 if (!id
->qp
&& conn_param
) {
3402 id_priv
->qp_num
= conn_param
->qp_num
;
3403 id_priv
->srq
= conn_param
->srq
;
3406 if (rdma_cap_ib_cm(id
->device
, id
->port_num
)) {
3407 if (id
->qp_type
== IB_QPT_UD
) {
3409 ret
= cma_send_sidr_rep(id_priv
, IB_SIDR_SUCCESS
,
3411 conn_param
->private_data
,
3412 conn_param
->private_data_len
);
3414 ret
= cma_send_sidr_rep(id_priv
, IB_SIDR_SUCCESS
,
3418 ret
= cma_accept_ib(id_priv
, conn_param
);
3420 ret
= cma_rep_recv(id_priv
);
3422 } else if (rdma_cap_iw_cm(id
->device
, id
->port_num
))
3423 ret
= cma_accept_iw(id_priv
, conn_param
);
3432 cma_modify_qp_err(id_priv
);
3433 rdma_reject(id
, NULL
, 0);
3436 EXPORT_SYMBOL(rdma_accept
);
3438 int rdma_notify(struct rdma_cm_id
*id
, enum ib_event_type event
)
3440 struct rdma_id_private
*id_priv
;
3443 id_priv
= container_of(id
, struct rdma_id_private
, id
);
3444 if (!id_priv
->cm_id
.ib
)
3447 switch (id
->device
->node_type
) {
3448 case RDMA_NODE_IB_CA
:
3449 ret
= ib_cm_notify(id_priv
->cm_id
.ib
, event
);
3457 EXPORT_SYMBOL(rdma_notify
);
3459 int rdma_reject(struct rdma_cm_id
*id
, const void *private_data
,
3460 u8 private_data_len
)
3462 struct rdma_id_private
*id_priv
;
3465 id_priv
= container_of(id
, struct rdma_id_private
, id
);
3466 if (!id_priv
->cm_id
.ib
)
3469 if (rdma_cap_ib_cm(id
->device
, id
->port_num
)) {
3470 if (id
->qp_type
== IB_QPT_UD
)
3471 ret
= cma_send_sidr_rep(id_priv
, IB_SIDR_REJECT
, 0,
3472 private_data
, private_data_len
);
3474 ret
= ib_send_cm_rej(id_priv
->cm_id
.ib
,
3475 IB_CM_REJ_CONSUMER_DEFINED
, NULL
,
3476 0, private_data
, private_data_len
);
3477 } else if (rdma_cap_iw_cm(id
->device
, id
->port_num
)) {
3478 ret
= iw_cm_reject(id_priv
->cm_id
.iw
,
3479 private_data
, private_data_len
);
3485 EXPORT_SYMBOL(rdma_reject
);
3487 int rdma_disconnect(struct rdma_cm_id
*id
)
3489 struct rdma_id_private
*id_priv
;
3492 id_priv
= container_of(id
, struct rdma_id_private
, id
);
3493 if (!id_priv
->cm_id
.ib
)
3496 if (rdma_cap_ib_cm(id
->device
, id
->port_num
)) {
3497 ret
= cma_modify_qp_err(id_priv
);
3500 /* Initiate or respond to a disconnect. */
3501 if (ib_send_cm_dreq(id_priv
->cm_id
.ib
, NULL
, 0))
3502 ib_send_cm_drep(id_priv
->cm_id
.ib
, NULL
, 0);
3503 } else if (rdma_cap_iw_cm(id
->device
, id
->port_num
)) {
3504 ret
= iw_cm_disconnect(id_priv
->cm_id
.iw
, 0);
3511 EXPORT_SYMBOL(rdma_disconnect
);
3513 static int cma_ib_mc_handler(int status
, struct ib_sa_multicast
*multicast
)
3515 struct rdma_id_private
*id_priv
;
3516 struct cma_multicast
*mc
= multicast
->context
;
3517 struct rdma_cm_event event
;
3520 id_priv
= mc
->id_priv
;
3521 if (cma_disable_callback(id_priv
, RDMA_CM_ADDR_BOUND
) &&
3522 cma_disable_callback(id_priv
, RDMA_CM_ADDR_RESOLVED
))
3526 status
= cma_set_qkey(id_priv
, be32_to_cpu(multicast
->rec
.qkey
));
3527 mutex_lock(&id_priv
->qp_mutex
);
3528 if (!status
&& id_priv
->id
.qp
)
3529 status
= ib_attach_mcast(id_priv
->id
.qp
, &multicast
->rec
.mgid
,
3530 be16_to_cpu(multicast
->rec
.mlid
));
3531 mutex_unlock(&id_priv
->qp_mutex
);
3533 memset(&event
, 0, sizeof event
);
3534 event
.status
= status
;
3535 event
.param
.ud
.private_data
= mc
->context
;
3537 event
.event
= RDMA_CM_EVENT_MULTICAST_JOIN
;
3538 ib_init_ah_from_mcmember(id_priv
->id
.device
,
3539 id_priv
->id
.port_num
, &multicast
->rec
,
3540 &event
.param
.ud
.ah_attr
);
3541 event
.param
.ud
.qp_num
= 0xFFFFFF;
3542 event
.param
.ud
.qkey
= be32_to_cpu(multicast
->rec
.qkey
);
3544 event
.event
= RDMA_CM_EVENT_MULTICAST_ERROR
;
3546 ret
= id_priv
->id
.event_handler(&id_priv
->id
, &event
);
3548 cma_exch(id_priv
, RDMA_CM_DESTROYING
);
3549 mutex_unlock(&id_priv
->handler_mutex
);
3550 rdma_destroy_id(&id_priv
->id
);
3554 mutex_unlock(&id_priv
->handler_mutex
);
3558 static void cma_set_mgid(struct rdma_id_private
*id_priv
,
3559 struct sockaddr
*addr
, union ib_gid
*mgid
)
3561 unsigned char mc_map
[MAX_ADDR_LEN
];
3562 struct rdma_dev_addr
*dev_addr
= &id_priv
->id
.route
.addr
.dev_addr
;
3563 struct sockaddr_in
*sin
= (struct sockaddr_in
*) addr
;
3564 struct sockaddr_in6
*sin6
= (struct sockaddr_in6
*) addr
;
3566 if (cma_any_addr(addr
)) {
3567 memset(mgid
, 0, sizeof *mgid
);
3568 } else if ((addr
->sa_family
== AF_INET6
) &&
3569 ((be32_to_cpu(sin6
->sin6_addr
.s6_addr32
[0]) & 0xFFF0FFFF) ==
3571 /* IPv6 address is an SA assigned MGID. */
3572 memcpy(mgid
, &sin6
->sin6_addr
, sizeof *mgid
);
3573 } else if (addr
->sa_family
== AF_IB
) {
3574 memcpy(mgid
, &((struct sockaddr_ib
*) addr
)->sib_addr
, sizeof *mgid
);
3575 } else if ((addr
->sa_family
== AF_INET6
)) {
3576 ipv6_ib_mc_map(&sin6
->sin6_addr
, dev_addr
->broadcast
, mc_map
);
3577 if (id_priv
->id
.ps
== RDMA_PS_UDP
)
3578 mc_map
[7] = 0x01; /* Use RDMA CM signature */
3579 *mgid
= *(union ib_gid
*) (mc_map
+ 4);
3581 ip_ib_mc_map(sin
->sin_addr
.s_addr
, dev_addr
->broadcast
, mc_map
);
3582 if (id_priv
->id
.ps
== RDMA_PS_UDP
)
3583 mc_map
[7] = 0x01; /* Use RDMA CM signature */
3584 *mgid
= *(union ib_gid
*) (mc_map
+ 4);
3588 static int cma_join_ib_multicast(struct rdma_id_private
*id_priv
,
3589 struct cma_multicast
*mc
)
3591 struct ib_sa_mcmember_rec rec
;
3592 struct rdma_dev_addr
*dev_addr
= &id_priv
->id
.route
.addr
.dev_addr
;
3593 ib_sa_comp_mask comp_mask
;
3596 ib_addr_get_mgid(dev_addr
, &rec
.mgid
);
3597 ret
= ib_sa_get_mcmember_rec(id_priv
->id
.device
, id_priv
->id
.port_num
,
3602 ret
= cma_set_qkey(id_priv
, 0);
3606 cma_set_mgid(id_priv
, (struct sockaddr
*) &mc
->addr
, &rec
.mgid
);
3607 rec
.qkey
= cpu_to_be32(id_priv
->qkey
);
3608 rdma_addr_get_sgid(dev_addr
, &rec
.port_gid
);
3609 rec
.pkey
= cpu_to_be16(ib_addr_get_pkey(dev_addr
));
3612 comp_mask
= IB_SA_MCMEMBER_REC_MGID
| IB_SA_MCMEMBER_REC_PORT_GID
|
3613 IB_SA_MCMEMBER_REC_PKEY
| IB_SA_MCMEMBER_REC_JOIN_STATE
|
3614 IB_SA_MCMEMBER_REC_QKEY
| IB_SA_MCMEMBER_REC_SL
|
3615 IB_SA_MCMEMBER_REC_FLOW_LABEL
|
3616 IB_SA_MCMEMBER_REC_TRAFFIC_CLASS
;
3618 if (id_priv
->id
.ps
== RDMA_PS_IPOIB
)
3619 comp_mask
|= IB_SA_MCMEMBER_REC_RATE
|
3620 IB_SA_MCMEMBER_REC_RATE_SELECTOR
|
3621 IB_SA_MCMEMBER_REC_MTU_SELECTOR
|
3622 IB_SA_MCMEMBER_REC_MTU
|
3623 IB_SA_MCMEMBER_REC_HOP_LIMIT
;
3625 mc
->multicast
.ib
= ib_sa_join_multicast(&sa_client
, id_priv
->id
.device
,
3626 id_priv
->id
.port_num
, &rec
,
3627 comp_mask
, GFP_KERNEL
,
3628 cma_ib_mc_handler
, mc
);
3629 return PTR_ERR_OR_ZERO(mc
->multicast
.ib
);
3632 static void iboe_mcast_work_handler(struct work_struct
*work
)
3634 struct iboe_mcast_work
*mw
= container_of(work
, struct iboe_mcast_work
, work
);
3635 struct cma_multicast
*mc
= mw
->mc
;
3636 struct ib_sa_multicast
*m
= mc
->multicast
.ib
;
3638 mc
->multicast
.ib
->context
= mc
;
3639 cma_ib_mc_handler(0, m
);
3640 kref_put(&mc
->mcref
, release_mc
);
3644 static void cma_iboe_set_mgid(struct sockaddr
*addr
, union ib_gid
*mgid
)
3646 struct sockaddr_in
*sin
= (struct sockaddr_in
*)addr
;
3647 struct sockaddr_in6
*sin6
= (struct sockaddr_in6
*)addr
;
3649 if (cma_any_addr(addr
)) {
3650 memset(mgid
, 0, sizeof *mgid
);
3651 } else if (addr
->sa_family
== AF_INET6
) {
3652 memcpy(mgid
, &sin6
->sin6_addr
, sizeof *mgid
);
3654 mgid
->raw
[0] = 0xff;
3655 mgid
->raw
[1] = 0x0e;
3664 mgid
->raw
[10] = 0xff;
3665 mgid
->raw
[11] = 0xff;
3666 *(__be32
*)(&mgid
->raw
[12]) = sin
->sin_addr
.s_addr
;
3670 static int cma_iboe_join_multicast(struct rdma_id_private
*id_priv
,
3671 struct cma_multicast
*mc
)
3673 struct iboe_mcast_work
*work
;
3674 struct rdma_dev_addr
*dev_addr
= &id_priv
->id
.route
.addr
.dev_addr
;
3676 struct sockaddr
*addr
= (struct sockaddr
*)&mc
->addr
;
3677 struct net_device
*ndev
= NULL
;
3679 if (cma_zero_addr((struct sockaddr
*)&mc
->addr
))
3682 work
= kzalloc(sizeof *work
, GFP_KERNEL
);
3686 mc
->multicast
.ib
= kzalloc(sizeof(struct ib_sa_multicast
), GFP_KERNEL
);
3687 if (!mc
->multicast
.ib
) {
3692 cma_iboe_set_mgid(addr
, &mc
->multicast
.ib
->rec
.mgid
);
3694 mc
->multicast
.ib
->rec
.pkey
= cpu_to_be16(0xffff);
3695 if (id_priv
->id
.ps
== RDMA_PS_UDP
)
3696 mc
->multicast
.ib
->rec
.qkey
= cpu_to_be32(RDMA_UDP_QKEY
);
3698 if (dev_addr
->bound_dev_if
)
3699 ndev
= dev_get_by_index(&init_net
, dev_addr
->bound_dev_if
);
3704 mc
->multicast
.ib
->rec
.rate
= iboe_get_rate(ndev
);
3705 mc
->multicast
.ib
->rec
.hop_limit
= 1;
3706 mc
->multicast
.ib
->rec
.mtu
= iboe_get_mtu(ndev
->mtu
);
3708 if (!mc
->multicast
.ib
->rec
.mtu
) {
3712 rdma_ip2gid((struct sockaddr
*)&id_priv
->id
.route
.addr
.src_addr
,
3713 &mc
->multicast
.ib
->rec
.port_gid
);
3716 INIT_WORK(&work
->work
, iboe_mcast_work_handler
);
3717 kref_get(&mc
->mcref
);
3718 queue_work(cma_wq
, &work
->work
);
3723 kfree(mc
->multicast
.ib
);
3729 int rdma_join_multicast(struct rdma_cm_id
*id
, struct sockaddr
*addr
,
3732 struct rdma_id_private
*id_priv
;
3733 struct cma_multicast
*mc
;
3736 id_priv
= container_of(id
, struct rdma_id_private
, id
);
3737 if (!cma_comp(id_priv
, RDMA_CM_ADDR_BOUND
) &&
3738 !cma_comp(id_priv
, RDMA_CM_ADDR_RESOLVED
))
3741 mc
= kmalloc(sizeof *mc
, GFP_KERNEL
);
3745 memcpy(&mc
->addr
, addr
, rdma_addr_size(addr
));
3746 mc
->context
= context
;
3747 mc
->id_priv
= id_priv
;
3749 spin_lock(&id_priv
->lock
);
3750 list_add(&mc
->list
, &id_priv
->mc_list
);
3751 spin_unlock(&id_priv
->lock
);
3753 if (rdma_protocol_roce(id
->device
, id
->port_num
)) {
3754 kref_init(&mc
->mcref
);
3755 ret
= cma_iboe_join_multicast(id_priv
, mc
);
3756 } else if (rdma_cap_ib_mcast(id
->device
, id
->port_num
))
3757 ret
= cma_join_ib_multicast(id_priv
, mc
);
3762 spin_lock_irq(&id_priv
->lock
);
3763 list_del(&mc
->list
);
3764 spin_unlock_irq(&id_priv
->lock
);
3769 EXPORT_SYMBOL(rdma_join_multicast
);
3771 void rdma_leave_multicast(struct rdma_cm_id
*id
, struct sockaddr
*addr
)
3773 struct rdma_id_private
*id_priv
;
3774 struct cma_multicast
*mc
;
3776 id_priv
= container_of(id
, struct rdma_id_private
, id
);
3777 spin_lock_irq(&id_priv
->lock
);
3778 list_for_each_entry(mc
, &id_priv
->mc_list
, list
) {
3779 if (!memcmp(&mc
->addr
, addr
, rdma_addr_size(addr
))) {
3780 list_del(&mc
->list
);
3781 spin_unlock_irq(&id_priv
->lock
);
3784 ib_detach_mcast(id
->qp
,
3785 &mc
->multicast
.ib
->rec
.mgid
,
3786 be16_to_cpu(mc
->multicast
.ib
->rec
.mlid
));
3788 BUG_ON(id_priv
->cma_dev
->device
!= id
->device
);
3790 if (rdma_cap_ib_mcast(id
->device
, id
->port_num
)) {
3791 ib_sa_free_multicast(mc
->multicast
.ib
);
3793 } else if (rdma_protocol_roce(id
->device
, id
->port_num
))
3794 kref_put(&mc
->mcref
, release_mc
);
3799 spin_unlock_irq(&id_priv
->lock
);
3801 EXPORT_SYMBOL(rdma_leave_multicast
);
3803 static int cma_netdev_change(struct net_device
*ndev
, struct rdma_id_private
*id_priv
)
3805 struct rdma_dev_addr
*dev_addr
;
3806 struct cma_ndev_work
*work
;
3808 dev_addr
= &id_priv
->id
.route
.addr
.dev_addr
;
3810 if ((dev_addr
->bound_dev_if
== ndev
->ifindex
) &&
3811 (net_eq(dev_net(ndev
), dev_addr
->net
)) &&
3812 memcmp(dev_addr
->src_dev_addr
, ndev
->dev_addr
, ndev
->addr_len
)) {
3813 printk(KERN_INFO
"RDMA CM addr change for ndev %s used by id %p\n",
3814 ndev
->name
, &id_priv
->id
);
3815 work
= kzalloc(sizeof *work
, GFP_KERNEL
);
3819 INIT_WORK(&work
->work
, cma_ndev_work_handler
);
3821 work
->event
.event
= RDMA_CM_EVENT_ADDR_CHANGE
;
3822 atomic_inc(&id_priv
->refcount
);
3823 queue_work(cma_wq
, &work
->work
);
3829 static int cma_netdev_callback(struct notifier_block
*self
, unsigned long event
,
3832 struct net_device
*ndev
= netdev_notifier_info_to_dev(ptr
);
3833 struct cma_device
*cma_dev
;
3834 struct rdma_id_private
*id_priv
;
3835 int ret
= NOTIFY_DONE
;
3837 if (event
!= NETDEV_BONDING_FAILOVER
)
3840 if (!(ndev
->flags
& IFF_MASTER
) || !(ndev
->priv_flags
& IFF_BONDING
))
3844 list_for_each_entry(cma_dev
, &dev_list
, list
)
3845 list_for_each_entry(id_priv
, &cma_dev
->id_list
, list
) {
3846 ret
= cma_netdev_change(ndev
, id_priv
);
3852 mutex_unlock(&lock
);
3856 static struct notifier_block cma_nb
= {
3857 .notifier_call
= cma_netdev_callback
3860 static void cma_add_one(struct ib_device
*device
)
3862 struct cma_device
*cma_dev
;
3863 struct rdma_id_private
*id_priv
;
3865 cma_dev
= kmalloc(sizeof *cma_dev
, GFP_KERNEL
);
3869 cma_dev
->device
= device
;
3871 init_completion(&cma_dev
->comp
);
3872 atomic_set(&cma_dev
->refcount
, 1);
3873 INIT_LIST_HEAD(&cma_dev
->id_list
);
3874 ib_set_client_data(device
, &cma_client
, cma_dev
);
3877 list_add_tail(&cma_dev
->list
, &dev_list
);
3878 list_for_each_entry(id_priv
, &listen_any_list
, list
)
3879 cma_listen_on_dev(id_priv
, cma_dev
);
3880 mutex_unlock(&lock
);
3883 static int cma_remove_id_dev(struct rdma_id_private
*id_priv
)
3885 struct rdma_cm_event event
;
3886 enum rdma_cm_state state
;
3889 /* Record that we want to remove the device */
3890 state
= cma_exch(id_priv
, RDMA_CM_DEVICE_REMOVAL
);
3891 if (state
== RDMA_CM_DESTROYING
)
3894 cma_cancel_operation(id_priv
, state
);
3895 mutex_lock(&id_priv
->handler_mutex
);
3897 /* Check for destruction from another callback. */
3898 if (!cma_comp(id_priv
, RDMA_CM_DEVICE_REMOVAL
))
3901 memset(&event
, 0, sizeof event
);
3902 event
.event
= RDMA_CM_EVENT_DEVICE_REMOVAL
;
3903 ret
= id_priv
->id
.event_handler(&id_priv
->id
, &event
);
3905 mutex_unlock(&id_priv
->handler_mutex
);
3909 static void cma_process_remove(struct cma_device
*cma_dev
)
3911 struct rdma_id_private
*id_priv
;
3915 while (!list_empty(&cma_dev
->id_list
)) {
3916 id_priv
= list_entry(cma_dev
->id_list
.next
,
3917 struct rdma_id_private
, list
);
3919 list_del(&id_priv
->listen_list
);
3920 list_del_init(&id_priv
->list
);
3921 atomic_inc(&id_priv
->refcount
);
3922 mutex_unlock(&lock
);
3924 ret
= id_priv
->internal_id
? 1 : cma_remove_id_dev(id_priv
);
3925 cma_deref_id(id_priv
);
3927 rdma_destroy_id(&id_priv
->id
);
3931 mutex_unlock(&lock
);
3933 cma_deref_dev(cma_dev
);
3934 wait_for_completion(&cma_dev
->comp
);
3937 static void cma_remove_one(struct ib_device
*device
, void *client_data
)
3939 struct cma_device
*cma_dev
= client_data
;
3945 list_del(&cma_dev
->list
);
3946 mutex_unlock(&lock
);
3948 cma_process_remove(cma_dev
);
3952 static int cma_get_id_stats(struct sk_buff
*skb
, struct netlink_callback
*cb
)
3954 struct nlmsghdr
*nlh
;
3955 struct rdma_cm_id_stats
*id_stats
;
3956 struct rdma_id_private
*id_priv
;
3957 struct rdma_cm_id
*id
= NULL
;
3958 struct cma_device
*cma_dev
;
3959 int i_dev
= 0, i_id
= 0;
3962 * We export all of the IDs as a sequence of messages. Each
3963 * ID gets its own netlink message.
3967 list_for_each_entry(cma_dev
, &dev_list
, list
) {
3968 if (i_dev
< cb
->args
[0]) {
3974 list_for_each_entry(id_priv
, &cma_dev
->id_list
, list
) {
3975 if (i_id
< cb
->args
[1]) {
3980 id_stats
= ibnl_put_msg(skb
, &nlh
, cb
->nlh
->nlmsg_seq
,
3981 sizeof *id_stats
, RDMA_NL_RDMA_CM
,
3982 RDMA_NL_RDMA_CM_ID_STATS
,
3987 memset(id_stats
, 0, sizeof *id_stats
);
3989 id_stats
->node_type
= id
->route
.addr
.dev_addr
.dev_type
;
3990 id_stats
->port_num
= id
->port_num
;
3991 id_stats
->bound_dev_if
=
3992 id
->route
.addr
.dev_addr
.bound_dev_if
;
3994 if (ibnl_put_attr(skb
, nlh
,
3995 rdma_addr_size(cma_src_addr(id_priv
)),
3996 cma_src_addr(id_priv
),
3997 RDMA_NL_RDMA_CM_ATTR_SRC_ADDR
))
3999 if (ibnl_put_attr(skb
, nlh
,
4000 rdma_addr_size(cma_src_addr(id_priv
)),
4001 cma_dst_addr(id_priv
),
4002 RDMA_NL_RDMA_CM_ATTR_DST_ADDR
))
4005 id_stats
->pid
= id_priv
->owner
;
4006 id_stats
->port_space
= id
->ps
;
4007 id_stats
->cm_state
= id_priv
->state
;
4008 id_stats
->qp_num
= id_priv
->qp_num
;
4009 id_stats
->qp_type
= id
->qp_type
;
4019 mutex_unlock(&lock
);
4020 cb
->args
[0] = i_dev
;
4026 static const struct ibnl_client_cbs cma_cb_table
[] = {
4027 [RDMA_NL_RDMA_CM_ID_STATS
] = { .dump
= cma_get_id_stats
,
4028 .module
= THIS_MODULE
},
4031 static int cma_init_net(struct net
*net
)
4033 struct cma_pernet
*pernet
= cma_pernet(net
);
4035 idr_init(&pernet
->tcp_ps
);
4036 idr_init(&pernet
->udp_ps
);
4037 idr_init(&pernet
->ipoib_ps
);
4038 idr_init(&pernet
->ib_ps
);
4043 static void cma_exit_net(struct net
*net
)
4045 struct cma_pernet
*pernet
= cma_pernet(net
);
4047 idr_destroy(&pernet
->tcp_ps
);
4048 idr_destroy(&pernet
->udp_ps
);
4049 idr_destroy(&pernet
->ipoib_ps
);
4050 idr_destroy(&pernet
->ib_ps
);
4053 static struct pernet_operations cma_pernet_operations
= {
4054 .init
= cma_init_net
,
4055 .exit
= cma_exit_net
,
4056 .id
= &cma_pernet_id
,
4057 .size
= sizeof(struct cma_pernet
),
4060 static int __init
cma_init(void)
4064 cma_wq
= create_singlethread_workqueue("rdma_cm");
4068 ret
= register_pernet_subsys(&cma_pernet_operations
);
4072 ib_sa_register_client(&sa_client
);
4073 rdma_addr_register_client(&addr_client
);
4074 register_netdevice_notifier(&cma_nb
);
4076 ret
= ib_register_client(&cma_client
);
4080 if (ibnl_add_client(RDMA_NL_RDMA_CM
, RDMA_NL_RDMA_CM_NUM_OPS
, cma_cb_table
))
4081 printk(KERN_WARNING
"RDMA CMA: failed to add netlink callback\n");
4086 unregister_netdevice_notifier(&cma_nb
);
4087 rdma_addr_unregister_client(&addr_client
);
4088 ib_sa_unregister_client(&sa_client
);
4090 destroy_workqueue(cma_wq
);
4094 static void __exit
cma_cleanup(void)
4096 ibnl_remove_client(RDMA_NL_RDMA_CM
);
4097 ib_unregister_client(&cma_client
);
4098 unregister_netdevice_notifier(&cma_nb
);
4099 rdma_addr_unregister_client(&addr_client
);
4100 ib_sa_unregister_client(&sa_client
);
4101 unregister_pernet_subsys(&cma_pernet_operations
);
4102 destroy_workqueue(cma_wq
);
4105 module_init(cma_init
);
4106 module_exit(cma_cleanup
);