1 // SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
3 * Copyright (c) 2005 Voltaire Inc. All rights reserved.
4 * Copyright (c) 2002-2005, Network Appliance, Inc. All rights reserved.
5 * Copyright (c) 1999-2019, Mellanox Technologies, Inc. All rights reserved.
6 * Copyright (c) 2005-2006 Intel Corporation. All rights reserved.
9 #include <linux/completion.h>
11 #include <linux/in6.h>
12 #include <linux/mutex.h>
13 #include <linux/random.h>
14 #include <linux/igmp.h>
15 #include <linux/xarray.h>
16 #include <linux/inetdevice.h>
17 #include <linux/slab.h>
18 #include <linux/module.h>
19 #include <net/route.h>
21 #include <net/net_namespace.h>
22 #include <net/netns/generic.h>
25 #include <net/ip_fib.h>
26 #include <net/ip6_route.h>
28 #include <rdma/rdma_cm.h>
29 #include <rdma/rdma_cm_ib.h>
30 #include <rdma/rdma_netlink.h>
32 #include <rdma/ib_cache.h>
33 #include <rdma/ib_cm.h>
34 #include <rdma/ib_sa.h>
35 #include <rdma/iw_cm.h>
37 #include "core_priv.h"
39 #include "cma_trace.h"
41 MODULE_AUTHOR("Sean Hefty");
42 MODULE_DESCRIPTION("Generic RDMA CM Agent");
43 MODULE_LICENSE("Dual BSD/GPL");
45 #define CMA_CM_RESPONSE_TIMEOUT 20
46 #define CMA_QUERY_CLASSPORT_INFO_TIMEOUT 3000
47 #define CMA_MAX_CM_RETRIES 15
48 #define CMA_CM_MRA_SETTING (IB_CM_MRA_FLAG_DELAY | 24)
49 #define CMA_IBOE_PACKET_LIFETIME 18
50 #define CMA_PREFERRED_ROCE_GID_TYPE IB_GID_TYPE_ROCE_UDP_ENCAP
52 static const char * const cma_events
[] = {
53 [RDMA_CM_EVENT_ADDR_RESOLVED
] = "address resolved",
54 [RDMA_CM_EVENT_ADDR_ERROR
] = "address error",
55 [RDMA_CM_EVENT_ROUTE_RESOLVED
] = "route resolved ",
56 [RDMA_CM_EVENT_ROUTE_ERROR
] = "route error",
57 [RDMA_CM_EVENT_CONNECT_REQUEST
] = "connect request",
58 [RDMA_CM_EVENT_CONNECT_RESPONSE
] = "connect response",
59 [RDMA_CM_EVENT_CONNECT_ERROR
] = "connect error",
60 [RDMA_CM_EVENT_UNREACHABLE
] = "unreachable",
61 [RDMA_CM_EVENT_REJECTED
] = "rejected",
62 [RDMA_CM_EVENT_ESTABLISHED
] = "established",
63 [RDMA_CM_EVENT_DISCONNECTED
] = "disconnected",
64 [RDMA_CM_EVENT_DEVICE_REMOVAL
] = "device removal",
65 [RDMA_CM_EVENT_MULTICAST_JOIN
] = "multicast join",
66 [RDMA_CM_EVENT_MULTICAST_ERROR
] = "multicast error",
67 [RDMA_CM_EVENT_ADDR_CHANGE
] = "address change",
68 [RDMA_CM_EVENT_TIMEWAIT_EXIT
] = "timewait exit",
71 static void cma_set_mgid(struct rdma_id_private
*id_priv
, struct sockaddr
*addr
,
74 const char *__attribute_const__
rdma_event_msg(enum rdma_cm_event_type event
)
78 return (index
< ARRAY_SIZE(cma_events
) && cma_events
[index
]) ?
79 cma_events
[index
] : "unrecognized event";
81 EXPORT_SYMBOL(rdma_event_msg
);
83 const char *__attribute_const__
rdma_reject_msg(struct rdma_cm_id
*id
,
86 if (rdma_ib_or_roce(id
->device
, id
->port_num
))
87 return ibcm_reject_msg(reason
);
89 if (rdma_protocol_iwarp(id
->device
, id
->port_num
))
90 return iwcm_reject_msg(reason
);
93 return "unrecognized transport";
95 EXPORT_SYMBOL(rdma_reject_msg
);
98 * rdma_is_consumer_reject - return true if the consumer rejected the connect
100 * @id: Communication identifier that received the REJECT event.
101 * @reason: Value returned in the REJECT event status field.
103 static bool rdma_is_consumer_reject(struct rdma_cm_id
*id
, int reason
)
105 if (rdma_ib_or_roce(id
->device
, id
->port_num
))
106 return reason
== IB_CM_REJ_CONSUMER_DEFINED
;
108 if (rdma_protocol_iwarp(id
->device
, id
->port_num
))
109 return reason
== -ECONNREFUSED
;
115 const void *rdma_consumer_reject_data(struct rdma_cm_id
*id
,
116 struct rdma_cm_event
*ev
, u8
*data_len
)
120 if (rdma_is_consumer_reject(id
, ev
->status
)) {
121 *data_len
= ev
->param
.conn
.private_data_len
;
122 p
= ev
->param
.conn
.private_data
;
129 EXPORT_SYMBOL(rdma_consumer_reject_data
);
132 * rdma_iw_cm_id() - return the iw_cm_id pointer for this cm_id.
133 * @id: Communication Identifier
135 struct iw_cm_id
*rdma_iw_cm_id(struct rdma_cm_id
*id
)
137 struct rdma_id_private
*id_priv
;
139 id_priv
= container_of(id
, struct rdma_id_private
, id
);
140 if (id
->device
->node_type
== RDMA_NODE_RNIC
)
141 return id_priv
->cm_id
.iw
;
144 EXPORT_SYMBOL(rdma_iw_cm_id
);
147 * rdma_res_to_id() - return the rdma_cm_id pointer for this restrack.
148 * @res: rdma resource tracking entry pointer
150 struct rdma_cm_id
*rdma_res_to_id(struct rdma_restrack_entry
*res
)
152 struct rdma_id_private
*id_priv
=
153 container_of(res
, struct rdma_id_private
, res
);
157 EXPORT_SYMBOL(rdma_res_to_id
);
159 static int cma_add_one(struct ib_device
*device
);
160 static void cma_remove_one(struct ib_device
*device
, void *client_data
);
162 static struct ib_client cma_client
= {
165 .remove
= cma_remove_one
168 static struct ib_sa_client sa_client
;
169 static LIST_HEAD(dev_list
);
170 static LIST_HEAD(listen_any_list
);
171 static DEFINE_MUTEX(lock
);
172 static struct workqueue_struct
*cma_wq
;
173 static unsigned int cma_pernet_id
;
176 struct xarray tcp_ps
;
177 struct xarray udp_ps
;
178 struct xarray ipoib_ps
;
182 static struct cma_pernet
*cma_pernet(struct net
*net
)
184 return net_generic(net
, cma_pernet_id
);
188 struct xarray
*cma_pernet_xa(struct net
*net
, enum rdma_ucm_port_space ps
)
190 struct cma_pernet
*pernet
= cma_pernet(net
);
194 return &pernet
->tcp_ps
;
196 return &pernet
->udp_ps
;
198 return &pernet
->ipoib_ps
;
200 return &pernet
->ib_ps
;
207 struct list_head list
;
208 struct ib_device
*device
;
209 struct completion comp
;
211 struct list_head id_list
;
212 enum ib_gid_type
*default_gid_type
;
213 u8
*default_roce_tos
;
216 struct rdma_bind_list
{
217 enum rdma_ucm_port_space ps
;
218 struct hlist_head owners
;
222 struct class_port_info_context
{
223 struct ib_class_port_info
*class_port_info
;
224 struct ib_device
*device
;
225 struct completion done
;
226 struct ib_sa_query
*sa_query
;
230 static int cma_ps_alloc(struct net
*net
, enum rdma_ucm_port_space ps
,
231 struct rdma_bind_list
*bind_list
, int snum
)
233 struct xarray
*xa
= cma_pernet_xa(net
, ps
);
235 return xa_insert(xa
, snum
, bind_list
, GFP_KERNEL
);
238 static struct rdma_bind_list
*cma_ps_find(struct net
*net
,
239 enum rdma_ucm_port_space ps
, int snum
)
241 struct xarray
*xa
= cma_pernet_xa(net
, ps
);
243 return xa_load(xa
, snum
);
246 static void cma_ps_remove(struct net
*net
, enum rdma_ucm_port_space ps
,
249 struct xarray
*xa
= cma_pernet_xa(net
, ps
);
258 void cma_dev_get(struct cma_device
*cma_dev
)
260 refcount_inc(&cma_dev
->refcount
);
263 void cma_dev_put(struct cma_device
*cma_dev
)
265 if (refcount_dec_and_test(&cma_dev
->refcount
))
266 complete(&cma_dev
->comp
);
269 struct cma_device
*cma_enum_devices_by_ibdev(cma_device_filter filter
,
272 struct cma_device
*cma_dev
;
273 struct cma_device
*found_cma_dev
= NULL
;
277 list_for_each_entry(cma_dev
, &dev_list
, list
)
278 if (filter(cma_dev
->device
, cookie
)) {
279 found_cma_dev
= cma_dev
;
284 cma_dev_get(found_cma_dev
);
286 return found_cma_dev
;
289 int cma_get_default_gid_type(struct cma_device
*cma_dev
,
292 if (!rdma_is_port_valid(cma_dev
->device
, port
))
295 return cma_dev
->default_gid_type
[port
- rdma_start_port(cma_dev
->device
)];
298 int cma_set_default_gid_type(struct cma_device
*cma_dev
,
300 enum ib_gid_type default_gid_type
)
302 unsigned long supported_gids
;
304 if (!rdma_is_port_valid(cma_dev
->device
, port
))
307 if (default_gid_type
== IB_GID_TYPE_IB
&&
308 rdma_protocol_roce_eth_encap(cma_dev
->device
, port
))
309 default_gid_type
= IB_GID_TYPE_ROCE
;
311 supported_gids
= roce_gid_type_mask_support(cma_dev
->device
, port
);
313 if (!(supported_gids
& 1 << default_gid_type
))
316 cma_dev
->default_gid_type
[port
- rdma_start_port(cma_dev
->device
)] =
322 int cma_get_default_roce_tos(struct cma_device
*cma_dev
, unsigned int port
)
324 if (!rdma_is_port_valid(cma_dev
->device
, port
))
327 return cma_dev
->default_roce_tos
[port
- rdma_start_port(cma_dev
->device
)];
330 int cma_set_default_roce_tos(struct cma_device
*cma_dev
, unsigned int port
,
333 if (!rdma_is_port_valid(cma_dev
->device
, port
))
336 cma_dev
->default_roce_tos
[port
- rdma_start_port(cma_dev
->device
)] =
341 struct ib_device
*cma_get_ib_dev(struct cma_device
*cma_dev
)
343 return cma_dev
->device
;
347 * Device removal can occur at anytime, so we need extra handling to
348 * serialize notifying the user of device removal with other callbacks.
349 * We do this by disabling removal notification while a callback is in process,
350 * and reporting it after the callback completes.
353 struct cma_multicast
{
354 struct rdma_id_private
*id_priv
;
355 struct ib_sa_multicast
*sa_mc
;
356 struct list_head list
;
358 struct sockaddr_storage addr
;
363 struct work_struct work
;
364 struct rdma_id_private
*id
;
365 enum rdma_cm_state old_state
;
366 enum rdma_cm_state new_state
;
367 struct rdma_cm_event event
;
380 u8 ip_version
; /* IP version: 7:4 */
382 union cma_ip_addr src_addr
;
383 union cma_ip_addr dst_addr
;
386 #define CMA_VERSION 0x00
388 struct cma_req_info
{
389 struct sockaddr_storage listen_addr_storage
;
390 struct sockaddr_storage src_addr_storage
;
391 struct ib_device
*device
;
392 union ib_gid local_gid
;
399 static int cma_comp_exch(struct rdma_id_private
*id_priv
,
400 enum rdma_cm_state comp
, enum rdma_cm_state exch
)
406 * The FSM uses a funny double locking where state is protected by both
407 * the handler_mutex and the spinlock. State is not allowed to change
408 * to/from a handler_mutex protected value without also holding
411 if (comp
== RDMA_CM_CONNECT
|| exch
== RDMA_CM_CONNECT
)
412 lockdep_assert_held(&id_priv
->handler_mutex
);
414 spin_lock_irqsave(&id_priv
->lock
, flags
);
415 if ((ret
= (id_priv
->state
== comp
)))
416 id_priv
->state
= exch
;
417 spin_unlock_irqrestore(&id_priv
->lock
, flags
);
421 static inline u8
cma_get_ip_ver(const struct cma_hdr
*hdr
)
423 return hdr
->ip_version
>> 4;
426 static inline void cma_set_ip_ver(struct cma_hdr
*hdr
, u8 ip_ver
)
428 hdr
->ip_version
= (ip_ver
<< 4) | (hdr
->ip_version
& 0xF);
431 static int cma_igmp_send(struct net_device
*ndev
, union ib_gid
*mgid
, bool join
)
433 struct in_device
*in_dev
= NULL
;
437 in_dev
= __in_dev_get_rtnl(ndev
);
440 ip_mc_inc_group(in_dev
,
441 *(__be32
*)(mgid
->raw
+ 12));
443 ip_mc_dec_group(in_dev
,
444 *(__be32
*)(mgid
->raw
+ 12));
448 return (in_dev
) ? 0 : -ENODEV
;
451 static void _cma_attach_to_dev(struct rdma_id_private
*id_priv
,
452 struct cma_device
*cma_dev
)
454 cma_dev_get(cma_dev
);
455 id_priv
->cma_dev
= cma_dev
;
456 id_priv
->id
.device
= cma_dev
->device
;
457 id_priv
->id
.route
.addr
.dev_addr
.transport
=
458 rdma_node_get_transport(cma_dev
->device
->node_type
);
459 list_add_tail(&id_priv
->list
, &cma_dev
->id_list
);
460 rdma_restrack_add(&id_priv
->res
);
462 trace_cm_id_attach(id_priv
, cma_dev
->device
);
465 static void cma_attach_to_dev(struct rdma_id_private
*id_priv
,
466 struct cma_device
*cma_dev
)
468 _cma_attach_to_dev(id_priv
, cma_dev
);
470 cma_dev
->default_gid_type
[id_priv
->id
.port_num
-
471 rdma_start_port(cma_dev
->device
)];
474 static void cma_release_dev(struct rdma_id_private
*id_priv
)
477 list_del(&id_priv
->list
);
478 cma_dev_put(id_priv
->cma_dev
);
479 id_priv
->cma_dev
= NULL
;
480 if (id_priv
->id
.route
.addr
.dev_addr
.sgid_attr
) {
481 rdma_put_gid_attr(id_priv
->id
.route
.addr
.dev_addr
.sgid_attr
);
482 id_priv
->id
.route
.addr
.dev_addr
.sgid_attr
= NULL
;
487 static inline struct sockaddr
*cma_src_addr(struct rdma_id_private
*id_priv
)
489 return (struct sockaddr
*) &id_priv
->id
.route
.addr
.src_addr
;
492 static inline struct sockaddr
*cma_dst_addr(struct rdma_id_private
*id_priv
)
494 return (struct sockaddr
*) &id_priv
->id
.route
.addr
.dst_addr
;
497 static inline unsigned short cma_family(struct rdma_id_private
*id_priv
)
499 return id_priv
->id
.route
.addr
.src_addr
.ss_family
;
502 static int cma_set_qkey(struct rdma_id_private
*id_priv
, u32 qkey
)
504 struct ib_sa_mcmember_rec rec
;
508 if (qkey
&& id_priv
->qkey
!= qkey
)
514 id_priv
->qkey
= qkey
;
518 switch (id_priv
->id
.ps
) {
521 id_priv
->qkey
= RDMA_UDP_QKEY
;
524 ib_addr_get_mgid(&id_priv
->id
.route
.addr
.dev_addr
, &rec
.mgid
);
525 ret
= ib_sa_get_mcmember_rec(id_priv
->id
.device
,
526 id_priv
->id
.port_num
, &rec
.mgid
,
529 id_priv
->qkey
= be32_to_cpu(rec
.qkey
);
537 static void cma_translate_ib(struct sockaddr_ib
*sib
, struct rdma_dev_addr
*dev_addr
)
539 dev_addr
->dev_type
= ARPHRD_INFINIBAND
;
540 rdma_addr_set_sgid(dev_addr
, (union ib_gid
*) &sib
->sib_addr
);
541 ib_addr_set_pkey(dev_addr
, ntohs(sib
->sib_pkey
));
544 static int cma_translate_addr(struct sockaddr
*addr
, struct rdma_dev_addr
*dev_addr
)
548 if (addr
->sa_family
!= AF_IB
) {
549 ret
= rdma_translate_ip(addr
, dev_addr
);
551 cma_translate_ib((struct sockaddr_ib
*) addr
, dev_addr
);
558 static const struct ib_gid_attr
*
559 cma_validate_port(struct ib_device
*device
, u8 port
,
560 enum ib_gid_type gid_type
,
562 struct rdma_id_private
*id_priv
)
564 struct rdma_dev_addr
*dev_addr
= &id_priv
->id
.route
.addr
.dev_addr
;
565 int bound_if_index
= dev_addr
->bound_dev_if
;
566 const struct ib_gid_attr
*sgid_attr
;
567 int dev_type
= dev_addr
->dev_type
;
568 struct net_device
*ndev
= NULL
;
570 if (!rdma_dev_access_netns(device
, id_priv
->id
.route
.addr
.dev_addr
.net
))
571 return ERR_PTR(-ENODEV
);
573 if ((dev_type
== ARPHRD_INFINIBAND
) && !rdma_protocol_ib(device
, port
))
574 return ERR_PTR(-ENODEV
);
576 if ((dev_type
!= ARPHRD_INFINIBAND
) && rdma_protocol_ib(device
, port
))
577 return ERR_PTR(-ENODEV
);
579 if (dev_type
== ARPHRD_ETHER
&& rdma_protocol_roce(device
, port
)) {
580 ndev
= dev_get_by_index(dev_addr
->net
, bound_if_index
);
582 return ERR_PTR(-ENODEV
);
584 gid_type
= IB_GID_TYPE_IB
;
587 sgid_attr
= rdma_find_gid_by_port(device
, gid
, gid_type
, port
, ndev
);
593 static void cma_bind_sgid_attr(struct rdma_id_private
*id_priv
,
594 const struct ib_gid_attr
*sgid_attr
)
596 WARN_ON(id_priv
->id
.route
.addr
.dev_addr
.sgid_attr
);
597 id_priv
->id
.route
.addr
.dev_addr
.sgid_attr
= sgid_attr
;
601 * cma_acquire_dev_by_src_ip - Acquire cma device, port, gid attribute
602 * based on source ip address.
603 * @id_priv: cm_id which should be bound to cma device
605 * cma_acquire_dev_by_src_ip() binds cm id to cma device, port and GID attribute
606 * based on source IP address. It returns 0 on success or error code otherwise.
607 * It is applicable to active and passive side cm_id.
609 static int cma_acquire_dev_by_src_ip(struct rdma_id_private
*id_priv
)
611 struct rdma_dev_addr
*dev_addr
= &id_priv
->id
.route
.addr
.dev_addr
;
612 const struct ib_gid_attr
*sgid_attr
;
613 union ib_gid gid
, iboe_gid
, *gidp
;
614 struct cma_device
*cma_dev
;
615 enum ib_gid_type gid_type
;
619 if (dev_addr
->dev_type
!= ARPHRD_INFINIBAND
&&
620 id_priv
->id
.ps
== RDMA_PS_IPOIB
)
623 rdma_ip2gid((struct sockaddr
*)&id_priv
->id
.route
.addr
.src_addr
,
626 memcpy(&gid
, dev_addr
->src_dev_addr
+
627 rdma_addr_gid_offset(dev_addr
), sizeof(gid
));
630 list_for_each_entry(cma_dev
, &dev_list
, list
) {
631 rdma_for_each_port (cma_dev
->device
, port
) {
632 gidp
= rdma_protocol_roce(cma_dev
->device
, port
) ?
634 gid_type
= cma_dev
->default_gid_type
[port
- 1];
635 sgid_attr
= cma_validate_port(cma_dev
->device
, port
,
636 gid_type
, gidp
, id_priv
);
637 if (!IS_ERR(sgid_attr
)) {
638 id_priv
->id
.port_num
= port
;
639 cma_bind_sgid_attr(id_priv
, sgid_attr
);
640 cma_attach_to_dev(id_priv
, cma_dev
);
652 * cma_ib_acquire_dev - Acquire cma device, port and SGID attribute
653 * @id_priv: cm id to bind to cma device
654 * @listen_id_priv: listener cm id to match against
655 * @req: Pointer to req structure containaining incoming
656 * request information
657 * cma_ib_acquire_dev() acquires cma device, port and SGID attribute when
658 * rdma device matches for listen_id and incoming request. It also verifies
659 * that a GID table entry is present for the source address.
660 * Returns 0 on success, or returns error code otherwise.
662 static int cma_ib_acquire_dev(struct rdma_id_private
*id_priv
,
663 const struct rdma_id_private
*listen_id_priv
,
664 struct cma_req_info
*req
)
666 struct rdma_dev_addr
*dev_addr
= &id_priv
->id
.route
.addr
.dev_addr
;
667 const struct ib_gid_attr
*sgid_attr
;
668 enum ib_gid_type gid_type
;
671 if (dev_addr
->dev_type
!= ARPHRD_INFINIBAND
&&
672 id_priv
->id
.ps
== RDMA_PS_IPOIB
)
675 if (rdma_protocol_roce(req
->device
, req
->port
))
676 rdma_ip2gid((struct sockaddr
*)&id_priv
->id
.route
.addr
.src_addr
,
679 memcpy(&gid
, dev_addr
->src_dev_addr
+
680 rdma_addr_gid_offset(dev_addr
), sizeof(gid
));
682 gid_type
= listen_id_priv
->cma_dev
->default_gid_type
[req
->port
- 1];
683 sgid_attr
= cma_validate_port(req
->device
, req
->port
,
684 gid_type
, &gid
, id_priv
);
685 if (IS_ERR(sgid_attr
))
686 return PTR_ERR(sgid_attr
);
688 id_priv
->id
.port_num
= req
->port
;
689 cma_bind_sgid_attr(id_priv
, sgid_attr
);
690 /* Need to acquire lock to protect against reader
691 * of cma_dev->id_list such as cma_netdev_callback() and
692 * cma_process_remove().
695 cma_attach_to_dev(id_priv
, listen_id_priv
->cma_dev
);
700 static int cma_iw_acquire_dev(struct rdma_id_private
*id_priv
,
701 const struct rdma_id_private
*listen_id_priv
)
703 struct rdma_dev_addr
*dev_addr
= &id_priv
->id
.route
.addr
.dev_addr
;
704 const struct ib_gid_attr
*sgid_attr
;
705 struct cma_device
*cma_dev
;
706 enum ib_gid_type gid_type
;
711 if (dev_addr
->dev_type
!= ARPHRD_INFINIBAND
&&
712 id_priv
->id
.ps
== RDMA_PS_IPOIB
)
715 memcpy(&gid
, dev_addr
->src_dev_addr
+
716 rdma_addr_gid_offset(dev_addr
), sizeof(gid
));
720 cma_dev
= listen_id_priv
->cma_dev
;
721 port
= listen_id_priv
->id
.port_num
;
722 gid_type
= listen_id_priv
->gid_type
;
723 sgid_attr
= cma_validate_port(cma_dev
->device
, port
,
724 gid_type
, &gid
, id_priv
);
725 if (!IS_ERR(sgid_attr
)) {
726 id_priv
->id
.port_num
= port
;
727 cma_bind_sgid_attr(id_priv
, sgid_attr
);
732 list_for_each_entry(cma_dev
, &dev_list
, list
) {
733 rdma_for_each_port (cma_dev
->device
, port
) {
734 if (listen_id_priv
->cma_dev
== cma_dev
&&
735 listen_id_priv
->id
.port_num
== port
)
738 gid_type
= cma_dev
->default_gid_type
[port
- 1];
739 sgid_attr
= cma_validate_port(cma_dev
->device
, port
,
740 gid_type
, &gid
, id_priv
);
741 if (!IS_ERR(sgid_attr
)) {
742 id_priv
->id
.port_num
= port
;
743 cma_bind_sgid_attr(id_priv
, sgid_attr
);
752 cma_attach_to_dev(id_priv
, cma_dev
);
759 * Select the source IB device and address to reach the destination IB address.
761 static int cma_resolve_ib_dev(struct rdma_id_private
*id_priv
)
763 struct cma_device
*cma_dev
, *cur_dev
;
764 struct sockaddr_ib
*addr
;
765 union ib_gid gid
, sgid
, *dgid
;
768 enum ib_port_state port_state
;
772 addr
= (struct sockaddr_ib
*) cma_dst_addr(id_priv
);
773 dgid
= (union ib_gid
*) &addr
->sib_addr
;
774 pkey
= ntohs(addr
->sib_pkey
);
777 list_for_each_entry(cur_dev
, &dev_list
, list
) {
778 rdma_for_each_port (cur_dev
->device
, p
) {
779 if (!rdma_cap_af_ib(cur_dev
->device
, p
))
782 if (ib_find_cached_pkey(cur_dev
->device
, p
, pkey
, &index
))
785 if (ib_get_cached_port_state(cur_dev
->device
, p
, &port_state
))
787 for (i
= 0; !rdma_query_gid(cur_dev
->device
,
790 if (!memcmp(&gid
, dgid
, sizeof(gid
))) {
793 id_priv
->id
.port_num
= p
;
797 if (!cma_dev
&& (gid
.global
.subnet_prefix
==
798 dgid
->global
.subnet_prefix
) &&
799 port_state
== IB_PORT_ACTIVE
) {
802 id_priv
->id
.port_num
= p
;
812 cma_attach_to_dev(id_priv
, cma_dev
);
814 addr
= (struct sockaddr_ib
*)cma_src_addr(id_priv
);
815 memcpy(&addr
->sib_addr
, &sgid
, sizeof(sgid
));
816 cma_translate_ib(addr
, &id_priv
->id
.route
.addr
.dev_addr
);
820 static void cma_id_get(struct rdma_id_private
*id_priv
)
822 refcount_inc(&id_priv
->refcount
);
825 static void cma_id_put(struct rdma_id_private
*id_priv
)
827 if (refcount_dec_and_test(&id_priv
->refcount
))
828 complete(&id_priv
->comp
);
831 static struct rdma_id_private
*
832 __rdma_create_id(struct net
*net
, rdma_cm_event_handler event_handler
,
833 void *context
, enum rdma_ucm_port_space ps
,
834 enum ib_qp_type qp_type
, const struct rdma_id_private
*parent
)
836 struct rdma_id_private
*id_priv
;
838 id_priv
= kzalloc(sizeof *id_priv
, GFP_KERNEL
);
840 return ERR_PTR(-ENOMEM
);
842 id_priv
->state
= RDMA_CM_IDLE
;
843 id_priv
->id
.context
= context
;
844 id_priv
->id
.event_handler
= event_handler
;
846 id_priv
->id
.qp_type
= qp_type
;
847 id_priv
->tos_set
= false;
848 id_priv
->timeout_set
= false;
849 id_priv
->gid_type
= IB_GID_TYPE_IB
;
850 spin_lock_init(&id_priv
->lock
);
851 mutex_init(&id_priv
->qp_mutex
);
852 init_completion(&id_priv
->comp
);
853 refcount_set(&id_priv
->refcount
, 1);
854 mutex_init(&id_priv
->handler_mutex
);
855 INIT_LIST_HEAD(&id_priv
->listen_list
);
856 INIT_LIST_HEAD(&id_priv
->mc_list
);
857 get_random_bytes(&id_priv
->seq_num
, sizeof id_priv
->seq_num
);
858 id_priv
->id
.route
.addr
.dev_addr
.net
= get_net(net
);
859 id_priv
->seq_num
&= 0x00ffffff;
861 rdma_restrack_new(&id_priv
->res
, RDMA_RESTRACK_CM_ID
);
863 rdma_restrack_parent_name(&id_priv
->res
, &parent
->res
);
869 __rdma_create_kernel_id(struct net
*net
, rdma_cm_event_handler event_handler
,
870 void *context
, enum rdma_ucm_port_space ps
,
871 enum ib_qp_type qp_type
, const char *caller
)
873 struct rdma_id_private
*ret
;
875 ret
= __rdma_create_id(net
, event_handler
, context
, ps
, qp_type
, NULL
);
877 return ERR_CAST(ret
);
879 rdma_restrack_set_name(&ret
->res
, caller
);
882 EXPORT_SYMBOL(__rdma_create_kernel_id
);
884 struct rdma_cm_id
*rdma_create_user_id(rdma_cm_event_handler event_handler
,
886 enum rdma_ucm_port_space ps
,
887 enum ib_qp_type qp_type
)
889 struct rdma_id_private
*ret
;
891 ret
= __rdma_create_id(current
->nsproxy
->net_ns
, event_handler
, context
,
894 return ERR_CAST(ret
);
896 rdma_restrack_set_name(&ret
->res
, NULL
);
899 EXPORT_SYMBOL(rdma_create_user_id
);
901 static int cma_init_ud_qp(struct rdma_id_private
*id_priv
, struct ib_qp
*qp
)
903 struct ib_qp_attr qp_attr
;
904 int qp_attr_mask
, ret
;
906 qp_attr
.qp_state
= IB_QPS_INIT
;
907 ret
= rdma_init_qp_attr(&id_priv
->id
, &qp_attr
, &qp_attr_mask
);
911 ret
= ib_modify_qp(qp
, &qp_attr
, qp_attr_mask
);
915 qp_attr
.qp_state
= IB_QPS_RTR
;
916 ret
= ib_modify_qp(qp
, &qp_attr
, IB_QP_STATE
);
920 qp_attr
.qp_state
= IB_QPS_RTS
;
922 ret
= ib_modify_qp(qp
, &qp_attr
, IB_QP_STATE
| IB_QP_SQ_PSN
);
927 static int cma_init_conn_qp(struct rdma_id_private
*id_priv
, struct ib_qp
*qp
)
929 struct ib_qp_attr qp_attr
;
930 int qp_attr_mask
, ret
;
932 qp_attr
.qp_state
= IB_QPS_INIT
;
933 ret
= rdma_init_qp_attr(&id_priv
->id
, &qp_attr
, &qp_attr_mask
);
937 return ib_modify_qp(qp
, &qp_attr
, qp_attr_mask
);
940 int rdma_create_qp(struct rdma_cm_id
*id
, struct ib_pd
*pd
,
941 struct ib_qp_init_attr
*qp_init_attr
)
943 struct rdma_id_private
*id_priv
;
947 id_priv
= container_of(id
, struct rdma_id_private
, id
);
948 if (id
->device
!= pd
->device
) {
953 qp_init_attr
->port_num
= id
->port_num
;
954 qp
= ib_create_qp(pd
, qp_init_attr
);
960 if (id
->qp_type
== IB_QPT_UD
)
961 ret
= cma_init_ud_qp(id_priv
, qp
);
963 ret
= cma_init_conn_qp(id_priv
, qp
);
968 id_priv
->qp_num
= qp
->qp_num
;
969 id_priv
->srq
= (qp
->srq
!= NULL
);
970 trace_cm_qp_create(id_priv
, pd
, qp_init_attr
, 0);
975 trace_cm_qp_create(id_priv
, pd
, qp_init_attr
, ret
);
978 EXPORT_SYMBOL(rdma_create_qp
);
980 void rdma_destroy_qp(struct rdma_cm_id
*id
)
982 struct rdma_id_private
*id_priv
;
984 id_priv
= container_of(id
, struct rdma_id_private
, id
);
985 trace_cm_qp_destroy(id_priv
);
986 mutex_lock(&id_priv
->qp_mutex
);
987 ib_destroy_qp(id_priv
->id
.qp
);
988 id_priv
->id
.qp
= NULL
;
989 mutex_unlock(&id_priv
->qp_mutex
);
991 EXPORT_SYMBOL(rdma_destroy_qp
);
993 static int cma_modify_qp_rtr(struct rdma_id_private
*id_priv
,
994 struct rdma_conn_param
*conn_param
)
996 struct ib_qp_attr qp_attr
;
997 int qp_attr_mask
, ret
;
999 mutex_lock(&id_priv
->qp_mutex
);
1000 if (!id_priv
->id
.qp
) {
1005 /* Need to update QP attributes from default values. */
1006 qp_attr
.qp_state
= IB_QPS_INIT
;
1007 ret
= rdma_init_qp_attr(&id_priv
->id
, &qp_attr
, &qp_attr_mask
);
1011 ret
= ib_modify_qp(id_priv
->id
.qp
, &qp_attr
, qp_attr_mask
);
1015 qp_attr
.qp_state
= IB_QPS_RTR
;
1016 ret
= rdma_init_qp_attr(&id_priv
->id
, &qp_attr
, &qp_attr_mask
);
1020 BUG_ON(id_priv
->cma_dev
->device
!= id_priv
->id
.device
);
1023 qp_attr
.max_dest_rd_atomic
= conn_param
->responder_resources
;
1024 ret
= ib_modify_qp(id_priv
->id
.qp
, &qp_attr
, qp_attr_mask
);
1026 mutex_unlock(&id_priv
->qp_mutex
);
1030 static int cma_modify_qp_rts(struct rdma_id_private
*id_priv
,
1031 struct rdma_conn_param
*conn_param
)
1033 struct ib_qp_attr qp_attr
;
1034 int qp_attr_mask
, ret
;
1036 mutex_lock(&id_priv
->qp_mutex
);
1037 if (!id_priv
->id
.qp
) {
1042 qp_attr
.qp_state
= IB_QPS_RTS
;
1043 ret
= rdma_init_qp_attr(&id_priv
->id
, &qp_attr
, &qp_attr_mask
);
1048 qp_attr
.max_rd_atomic
= conn_param
->initiator_depth
;
1049 ret
= ib_modify_qp(id_priv
->id
.qp
, &qp_attr
, qp_attr_mask
);
1051 mutex_unlock(&id_priv
->qp_mutex
);
1055 static int cma_modify_qp_err(struct rdma_id_private
*id_priv
)
1057 struct ib_qp_attr qp_attr
;
1060 mutex_lock(&id_priv
->qp_mutex
);
1061 if (!id_priv
->id
.qp
) {
1066 qp_attr
.qp_state
= IB_QPS_ERR
;
1067 ret
= ib_modify_qp(id_priv
->id
.qp
, &qp_attr
, IB_QP_STATE
);
1069 mutex_unlock(&id_priv
->qp_mutex
);
1073 static int cma_ib_init_qp_attr(struct rdma_id_private
*id_priv
,
1074 struct ib_qp_attr
*qp_attr
, int *qp_attr_mask
)
1076 struct rdma_dev_addr
*dev_addr
= &id_priv
->id
.route
.addr
.dev_addr
;
1080 if (rdma_cap_eth_ah(id_priv
->id
.device
, id_priv
->id
.port_num
))
1083 pkey
= ib_addr_get_pkey(dev_addr
);
1085 ret
= ib_find_cached_pkey(id_priv
->id
.device
, id_priv
->id
.port_num
,
1086 pkey
, &qp_attr
->pkey_index
);
1090 qp_attr
->port_num
= id_priv
->id
.port_num
;
1091 *qp_attr_mask
= IB_QP_STATE
| IB_QP_PKEY_INDEX
| IB_QP_PORT
;
1093 if (id_priv
->id
.qp_type
== IB_QPT_UD
) {
1094 ret
= cma_set_qkey(id_priv
, 0);
1098 qp_attr
->qkey
= id_priv
->qkey
;
1099 *qp_attr_mask
|= IB_QP_QKEY
;
1101 qp_attr
->qp_access_flags
= 0;
1102 *qp_attr_mask
|= IB_QP_ACCESS_FLAGS
;
1107 int rdma_init_qp_attr(struct rdma_cm_id
*id
, struct ib_qp_attr
*qp_attr
,
1110 struct rdma_id_private
*id_priv
;
1113 id_priv
= container_of(id
, struct rdma_id_private
, id
);
1114 if (rdma_cap_ib_cm(id
->device
, id
->port_num
)) {
1115 if (!id_priv
->cm_id
.ib
|| (id_priv
->id
.qp_type
== IB_QPT_UD
))
1116 ret
= cma_ib_init_qp_attr(id_priv
, qp_attr
, qp_attr_mask
);
1118 ret
= ib_cm_init_qp_attr(id_priv
->cm_id
.ib
, qp_attr
,
1121 if (qp_attr
->qp_state
== IB_QPS_RTR
)
1122 qp_attr
->rq_psn
= id_priv
->seq_num
;
1123 } else if (rdma_cap_iw_cm(id
->device
, id
->port_num
)) {
1124 if (!id_priv
->cm_id
.iw
) {
1125 qp_attr
->qp_access_flags
= 0;
1126 *qp_attr_mask
= IB_QP_STATE
| IB_QP_ACCESS_FLAGS
;
1128 ret
= iw_cm_init_qp_attr(id_priv
->cm_id
.iw
, qp_attr
,
1130 qp_attr
->port_num
= id_priv
->id
.port_num
;
1131 *qp_attr_mask
|= IB_QP_PORT
;
1135 if ((*qp_attr_mask
& IB_QP_TIMEOUT
) && id_priv
->timeout_set
)
1136 qp_attr
->timeout
= id_priv
->timeout
;
1140 EXPORT_SYMBOL(rdma_init_qp_attr
);
1142 static inline bool cma_zero_addr(const struct sockaddr
*addr
)
1144 switch (addr
->sa_family
) {
1146 return ipv4_is_zeronet(((struct sockaddr_in
*)addr
)->sin_addr
.s_addr
);
1148 return ipv6_addr_any(&((struct sockaddr_in6
*)addr
)->sin6_addr
);
1150 return ib_addr_any(&((struct sockaddr_ib
*)addr
)->sib_addr
);
1156 static inline bool cma_loopback_addr(const struct sockaddr
*addr
)
1158 switch (addr
->sa_family
) {
1160 return ipv4_is_loopback(
1161 ((struct sockaddr_in
*)addr
)->sin_addr
.s_addr
);
1163 return ipv6_addr_loopback(
1164 &((struct sockaddr_in6
*)addr
)->sin6_addr
);
1166 return ib_addr_loopback(
1167 &((struct sockaddr_ib
*)addr
)->sib_addr
);
1173 static inline bool cma_any_addr(const struct sockaddr
*addr
)
1175 return cma_zero_addr(addr
) || cma_loopback_addr(addr
);
1178 static int cma_addr_cmp(const struct sockaddr
*src
, const struct sockaddr
*dst
)
1180 if (src
->sa_family
!= dst
->sa_family
)
1183 switch (src
->sa_family
) {
1185 return ((struct sockaddr_in
*)src
)->sin_addr
.s_addr
!=
1186 ((struct sockaddr_in
*)dst
)->sin_addr
.s_addr
;
1188 struct sockaddr_in6
*src_addr6
= (struct sockaddr_in6
*)src
;
1189 struct sockaddr_in6
*dst_addr6
= (struct sockaddr_in6
*)dst
;
1192 if (ipv6_addr_cmp(&src_addr6
->sin6_addr
,
1193 &dst_addr6
->sin6_addr
))
1195 link_local
= ipv6_addr_type(&dst_addr6
->sin6_addr
) &
1196 IPV6_ADDR_LINKLOCAL
;
1197 /* Link local must match their scope_ids */
1198 return link_local
? (src_addr6
->sin6_scope_id
!=
1199 dst_addr6
->sin6_scope_id
) :
1204 return ib_addr_cmp(&((struct sockaddr_ib
*) src
)->sib_addr
,
1205 &((struct sockaddr_ib
*) dst
)->sib_addr
);
1209 static __be16
cma_port(const struct sockaddr
*addr
)
1211 struct sockaddr_ib
*sib
;
1213 switch (addr
->sa_family
) {
1215 return ((struct sockaddr_in
*) addr
)->sin_port
;
1217 return ((struct sockaddr_in6
*) addr
)->sin6_port
;
1219 sib
= (struct sockaddr_ib
*) addr
;
1220 return htons((u16
) (be64_to_cpu(sib
->sib_sid
) &
1221 be64_to_cpu(sib
->sib_sid_mask
)));
1227 static inline int cma_any_port(const struct sockaddr
*addr
)
1229 return !cma_port(addr
);
1232 static void cma_save_ib_info(struct sockaddr
*src_addr
,
1233 struct sockaddr
*dst_addr
,
1234 const struct rdma_cm_id
*listen_id
,
1235 const struct sa_path_rec
*path
)
1237 struct sockaddr_ib
*listen_ib
, *ib
;
1239 listen_ib
= (struct sockaddr_ib
*) &listen_id
->route
.addr
.src_addr
;
1241 ib
= (struct sockaddr_ib
*)src_addr
;
1242 ib
->sib_family
= AF_IB
;
1244 ib
->sib_pkey
= path
->pkey
;
1245 ib
->sib_flowinfo
= path
->flow_label
;
1246 memcpy(&ib
->sib_addr
, &path
->sgid
, 16);
1247 ib
->sib_sid
= path
->service_id
;
1248 ib
->sib_scope_id
= 0;
1250 ib
->sib_pkey
= listen_ib
->sib_pkey
;
1251 ib
->sib_flowinfo
= listen_ib
->sib_flowinfo
;
1252 ib
->sib_addr
= listen_ib
->sib_addr
;
1253 ib
->sib_sid
= listen_ib
->sib_sid
;
1254 ib
->sib_scope_id
= listen_ib
->sib_scope_id
;
1256 ib
->sib_sid_mask
= cpu_to_be64(0xffffffffffffffffULL
);
1259 ib
= (struct sockaddr_ib
*)dst_addr
;
1260 ib
->sib_family
= AF_IB
;
1262 ib
->sib_pkey
= path
->pkey
;
1263 ib
->sib_flowinfo
= path
->flow_label
;
1264 memcpy(&ib
->sib_addr
, &path
->dgid
, 16);
1269 static void cma_save_ip4_info(struct sockaddr_in
*src_addr
,
1270 struct sockaddr_in
*dst_addr
,
1271 struct cma_hdr
*hdr
,
1275 *src_addr
= (struct sockaddr_in
) {
1276 .sin_family
= AF_INET
,
1277 .sin_addr
.s_addr
= hdr
->dst_addr
.ip4
.addr
,
1278 .sin_port
= local_port
,
1283 *dst_addr
= (struct sockaddr_in
) {
1284 .sin_family
= AF_INET
,
1285 .sin_addr
.s_addr
= hdr
->src_addr
.ip4
.addr
,
1286 .sin_port
= hdr
->port
,
1291 static void cma_save_ip6_info(struct sockaddr_in6
*src_addr
,
1292 struct sockaddr_in6
*dst_addr
,
1293 struct cma_hdr
*hdr
,
1297 *src_addr
= (struct sockaddr_in6
) {
1298 .sin6_family
= AF_INET6
,
1299 .sin6_addr
= hdr
->dst_addr
.ip6
,
1300 .sin6_port
= local_port
,
1305 *dst_addr
= (struct sockaddr_in6
) {
1306 .sin6_family
= AF_INET6
,
1307 .sin6_addr
= hdr
->src_addr
.ip6
,
1308 .sin6_port
= hdr
->port
,
1313 static u16
cma_port_from_service_id(__be64 service_id
)
1315 return (u16
)be64_to_cpu(service_id
);
1318 static int cma_save_ip_info(struct sockaddr
*src_addr
,
1319 struct sockaddr
*dst_addr
,
1320 const struct ib_cm_event
*ib_event
,
1323 struct cma_hdr
*hdr
;
1326 hdr
= ib_event
->private_data
;
1327 if (hdr
->cma_version
!= CMA_VERSION
)
1330 port
= htons(cma_port_from_service_id(service_id
));
1332 switch (cma_get_ip_ver(hdr
)) {
1334 cma_save_ip4_info((struct sockaddr_in
*)src_addr
,
1335 (struct sockaddr_in
*)dst_addr
, hdr
, port
);
1338 cma_save_ip6_info((struct sockaddr_in6
*)src_addr
,
1339 (struct sockaddr_in6
*)dst_addr
, hdr
, port
);
1342 return -EAFNOSUPPORT
;
1348 static int cma_save_net_info(struct sockaddr
*src_addr
,
1349 struct sockaddr
*dst_addr
,
1350 const struct rdma_cm_id
*listen_id
,
1351 const struct ib_cm_event
*ib_event
,
1352 sa_family_t sa_family
, __be64 service_id
)
1354 if (sa_family
== AF_IB
) {
1355 if (ib_event
->event
== IB_CM_REQ_RECEIVED
)
1356 cma_save_ib_info(src_addr
, dst_addr
, listen_id
,
1357 ib_event
->param
.req_rcvd
.primary_path
);
1358 else if (ib_event
->event
== IB_CM_SIDR_REQ_RECEIVED
)
1359 cma_save_ib_info(src_addr
, dst_addr
, listen_id
, NULL
);
1363 return cma_save_ip_info(src_addr
, dst_addr
, ib_event
, service_id
);
1366 static int cma_save_req_info(const struct ib_cm_event
*ib_event
,
1367 struct cma_req_info
*req
)
1369 const struct ib_cm_req_event_param
*req_param
=
1370 &ib_event
->param
.req_rcvd
;
1371 const struct ib_cm_sidr_req_event_param
*sidr_param
=
1372 &ib_event
->param
.sidr_req_rcvd
;
1374 switch (ib_event
->event
) {
1375 case IB_CM_REQ_RECEIVED
:
1376 req
->device
= req_param
->listen_id
->device
;
1377 req
->port
= req_param
->port
;
1378 memcpy(&req
->local_gid
, &req_param
->primary_path
->sgid
,
1379 sizeof(req
->local_gid
));
1380 req
->has_gid
= true;
1381 req
->service_id
= req_param
->primary_path
->service_id
;
1382 req
->pkey
= be16_to_cpu(req_param
->primary_path
->pkey
);
1383 if (req
->pkey
!= req_param
->bth_pkey
)
1384 pr_warn_ratelimited("RDMA CMA: got different BTH P_Key (0x%x) and primary path P_Key (0x%x)\n"
1385 "RDMA CMA: in the future this may cause the request to be dropped\n",
1386 req_param
->bth_pkey
, req
->pkey
);
1388 case IB_CM_SIDR_REQ_RECEIVED
:
1389 req
->device
= sidr_param
->listen_id
->device
;
1390 req
->port
= sidr_param
->port
;
1391 req
->has_gid
= false;
1392 req
->service_id
= sidr_param
->service_id
;
1393 req
->pkey
= sidr_param
->pkey
;
1394 if (req
->pkey
!= sidr_param
->bth_pkey
)
1395 pr_warn_ratelimited("RDMA CMA: got different BTH P_Key (0x%x) and SIDR request payload P_Key (0x%x)\n"
1396 "RDMA CMA: in the future this may cause the request to be dropped\n",
1397 sidr_param
->bth_pkey
, req
->pkey
);
1406 static bool validate_ipv4_net_dev(struct net_device
*net_dev
,
1407 const struct sockaddr_in
*dst_addr
,
1408 const struct sockaddr_in
*src_addr
)
1410 __be32 daddr
= dst_addr
->sin_addr
.s_addr
,
1411 saddr
= src_addr
->sin_addr
.s_addr
;
1412 struct fib_result res
;
1417 if (ipv4_is_multicast(saddr
) || ipv4_is_lbcast(saddr
) ||
1418 ipv4_is_lbcast(daddr
) || ipv4_is_zeronet(saddr
) ||
1419 ipv4_is_zeronet(daddr
) || ipv4_is_loopback(daddr
) ||
1420 ipv4_is_loopback(saddr
))
1423 memset(&fl4
, 0, sizeof(fl4
));
1424 fl4
.flowi4_iif
= net_dev
->ifindex
;
1429 err
= fib_lookup(dev_net(net_dev
), &fl4
, &res
, 0);
1430 ret
= err
== 0 && FIB_RES_DEV(res
) == net_dev
;
1436 static bool validate_ipv6_net_dev(struct net_device
*net_dev
,
1437 const struct sockaddr_in6
*dst_addr
,
1438 const struct sockaddr_in6
*src_addr
)
1440 #if IS_ENABLED(CONFIG_IPV6)
1441 const int strict
= ipv6_addr_type(&dst_addr
->sin6_addr
) &
1442 IPV6_ADDR_LINKLOCAL
;
1443 struct rt6_info
*rt
= rt6_lookup(dev_net(net_dev
), &dst_addr
->sin6_addr
,
1444 &src_addr
->sin6_addr
, net_dev
->ifindex
,
1451 ret
= rt
->rt6i_idev
->dev
== net_dev
;
1460 static bool validate_net_dev(struct net_device
*net_dev
,
1461 const struct sockaddr
*daddr
,
1462 const struct sockaddr
*saddr
)
1464 const struct sockaddr_in
*daddr4
= (const struct sockaddr_in
*)daddr
;
1465 const struct sockaddr_in
*saddr4
= (const struct sockaddr_in
*)saddr
;
1466 const struct sockaddr_in6
*daddr6
= (const struct sockaddr_in6
*)daddr
;
1467 const struct sockaddr_in6
*saddr6
= (const struct sockaddr_in6
*)saddr
;
1469 switch (daddr
->sa_family
) {
1471 return saddr
->sa_family
== AF_INET
&&
1472 validate_ipv4_net_dev(net_dev
, daddr4
, saddr4
);
1475 return saddr
->sa_family
== AF_INET6
&&
1476 validate_ipv6_net_dev(net_dev
, daddr6
, saddr6
);
1483 static struct net_device
*
1484 roce_get_net_dev_by_cm_event(const struct ib_cm_event
*ib_event
)
1486 const struct ib_gid_attr
*sgid_attr
= NULL
;
1487 struct net_device
*ndev
;
1489 if (ib_event
->event
== IB_CM_REQ_RECEIVED
)
1490 sgid_attr
= ib_event
->param
.req_rcvd
.ppath_sgid_attr
;
1491 else if (ib_event
->event
== IB_CM_SIDR_REQ_RECEIVED
)
1492 sgid_attr
= ib_event
->param
.sidr_req_rcvd
.sgid_attr
;
1498 ndev
= rdma_read_gid_attr_ndev_rcu(sgid_attr
);
1507 static struct net_device
*cma_get_net_dev(const struct ib_cm_event
*ib_event
,
1508 struct cma_req_info
*req
)
1510 struct sockaddr
*listen_addr
=
1511 (struct sockaddr
*)&req
->listen_addr_storage
;
1512 struct sockaddr
*src_addr
= (struct sockaddr
*)&req
->src_addr_storage
;
1513 struct net_device
*net_dev
;
1514 const union ib_gid
*gid
= req
->has_gid
? &req
->local_gid
: NULL
;
1517 err
= cma_save_ip_info(listen_addr
, src_addr
, ib_event
,
1520 return ERR_PTR(err
);
1522 if (rdma_protocol_roce(req
->device
, req
->port
))
1523 net_dev
= roce_get_net_dev_by_cm_event(ib_event
);
1525 net_dev
= ib_get_net_dev_by_params(req
->device
, req
->port
,
1529 return ERR_PTR(-ENODEV
);
1534 static enum rdma_ucm_port_space
rdma_ps_from_service_id(__be64 service_id
)
1536 return (be64_to_cpu(service_id
) >> 16) & 0xffff;
1539 static bool cma_match_private_data(struct rdma_id_private
*id_priv
,
1540 const struct cma_hdr
*hdr
)
1542 struct sockaddr
*addr
= cma_src_addr(id_priv
);
1544 struct in6_addr ip6_addr
;
1546 if (cma_any_addr(addr
) && !id_priv
->afonly
)
1549 switch (addr
->sa_family
) {
1551 ip4_addr
= ((struct sockaddr_in
*)addr
)->sin_addr
.s_addr
;
1552 if (cma_get_ip_ver(hdr
) != 4)
1554 if (!cma_any_addr(addr
) &&
1555 hdr
->dst_addr
.ip4
.addr
!= ip4_addr
)
1559 ip6_addr
= ((struct sockaddr_in6
*)addr
)->sin6_addr
;
1560 if (cma_get_ip_ver(hdr
) != 6)
1562 if (!cma_any_addr(addr
) &&
1563 memcmp(&hdr
->dst_addr
.ip6
, &ip6_addr
, sizeof(ip6_addr
)))
1575 static bool cma_protocol_roce(const struct rdma_cm_id
*id
)
1577 struct ib_device
*device
= id
->device
;
1578 const int port_num
= id
->port_num
?: rdma_start_port(device
);
1580 return rdma_protocol_roce(device
, port_num
);
1583 static bool cma_is_req_ipv6_ll(const struct cma_req_info
*req
)
1585 const struct sockaddr
*daddr
=
1586 (const struct sockaddr
*)&req
->listen_addr_storage
;
1587 const struct sockaddr_in6
*daddr6
= (const struct sockaddr_in6
*)daddr
;
1589 /* Returns true if the req is for IPv6 link local */
1590 return (daddr
->sa_family
== AF_INET6
&&
1591 (ipv6_addr_type(&daddr6
->sin6_addr
) & IPV6_ADDR_LINKLOCAL
));
1594 static bool cma_match_net_dev(const struct rdma_cm_id
*id
,
1595 const struct net_device
*net_dev
,
1596 const struct cma_req_info
*req
)
1598 const struct rdma_addr
*addr
= &id
->route
.addr
;
1601 /* This request is an AF_IB request */
1602 return (!id
->port_num
|| id
->port_num
== req
->port
) &&
1603 (addr
->src_addr
.ss_family
== AF_IB
);
1606 * If the request is not for IPv6 link local, allow matching
1607 * request to any netdevice of the one or multiport rdma device.
1609 if (!cma_is_req_ipv6_ll(req
))
1612 * Net namespaces must match, and if the listner is listening
1613 * on a specific netdevice than netdevice must match as well.
1615 if (net_eq(dev_net(net_dev
), addr
->dev_addr
.net
) &&
1616 (!!addr
->dev_addr
.bound_dev_if
==
1617 (addr
->dev_addr
.bound_dev_if
== net_dev
->ifindex
)))
1623 static struct rdma_id_private
*cma_find_listener(
1624 const struct rdma_bind_list
*bind_list
,
1625 const struct ib_cm_id
*cm_id
,
1626 const struct ib_cm_event
*ib_event
,
1627 const struct cma_req_info
*req
,
1628 const struct net_device
*net_dev
)
1630 struct rdma_id_private
*id_priv
, *id_priv_dev
;
1632 lockdep_assert_held(&lock
);
1635 return ERR_PTR(-EINVAL
);
1637 hlist_for_each_entry(id_priv
, &bind_list
->owners
, node
) {
1638 if (cma_match_private_data(id_priv
, ib_event
->private_data
)) {
1639 if (id_priv
->id
.device
== cm_id
->device
&&
1640 cma_match_net_dev(&id_priv
->id
, net_dev
, req
))
1642 list_for_each_entry(id_priv_dev
,
1643 &id_priv
->listen_list
,
1645 if (id_priv_dev
->id
.device
== cm_id
->device
&&
1646 cma_match_net_dev(&id_priv_dev
->id
,
1653 return ERR_PTR(-EINVAL
);
1656 static struct rdma_id_private
*
1657 cma_ib_id_from_event(struct ib_cm_id
*cm_id
,
1658 const struct ib_cm_event
*ib_event
,
1659 struct cma_req_info
*req
,
1660 struct net_device
**net_dev
)
1662 struct rdma_bind_list
*bind_list
;
1663 struct rdma_id_private
*id_priv
;
1666 err
= cma_save_req_info(ib_event
, req
);
1668 return ERR_PTR(err
);
1670 *net_dev
= cma_get_net_dev(ib_event
, req
);
1671 if (IS_ERR(*net_dev
)) {
1672 if (PTR_ERR(*net_dev
) == -EAFNOSUPPORT
) {
1673 /* Assuming the protocol is AF_IB */
1676 return ERR_CAST(*net_dev
);
1682 * Net namespace might be getting deleted while route lookup,
1683 * cm_id lookup is in progress. Therefore, perform netdevice
1684 * validation, cm_id lookup under rcu lock.
1685 * RCU lock along with netdevice state check, synchronizes with
1686 * netdevice migrating to different net namespace and also avoids
1687 * case where net namespace doesn't get deleted while lookup is in
1689 * If the device state is not IFF_UP, its properties such as ifindex
1690 * and nd_net cannot be trusted to remain valid without rcu lock.
1691 * net/core/dev.c change_net_namespace() ensures to synchronize with
1692 * ongoing operations on net device after device is closed using
1693 * synchronize_net().
1698 * If netdevice is down, it is likely that it is administratively
1699 * down or it might be migrating to different namespace.
1700 * In that case avoid further processing, as the net namespace
1701 * or ifindex may change.
1703 if (((*net_dev
)->flags
& IFF_UP
) == 0) {
1704 id_priv
= ERR_PTR(-EHOSTUNREACH
);
1708 if (!validate_net_dev(*net_dev
,
1709 (struct sockaddr
*)&req
->listen_addr_storage
,
1710 (struct sockaddr
*)&req
->src_addr_storage
)) {
1711 id_priv
= ERR_PTR(-EHOSTUNREACH
);
1716 bind_list
= cma_ps_find(*net_dev
? dev_net(*net_dev
) : &init_net
,
1717 rdma_ps_from_service_id(req
->service_id
),
1718 cma_port_from_service_id(req
->service_id
));
1719 id_priv
= cma_find_listener(bind_list
, cm_id
, ib_event
, req
, *net_dev
);
1722 mutex_unlock(&lock
);
1723 if (IS_ERR(id_priv
) && *net_dev
) {
1730 static inline u8
cma_user_data_offset(struct rdma_id_private
*id_priv
)
1732 return cma_family(id_priv
) == AF_IB
? 0 : sizeof(struct cma_hdr
);
1735 static void cma_cancel_route(struct rdma_id_private
*id_priv
)
1737 if (rdma_cap_ib_sa(id_priv
->id
.device
, id_priv
->id
.port_num
)) {
1739 ib_sa_cancel_query(id_priv
->query_id
, id_priv
->query
);
1743 static void cma_cancel_listens(struct rdma_id_private
*id_priv
)
1745 struct rdma_id_private
*dev_id_priv
;
1748 * Remove from listen_any_list to prevent added devices from spawning
1749 * additional listen requests.
1752 list_del(&id_priv
->list
);
1754 while (!list_empty(&id_priv
->listen_list
)) {
1755 dev_id_priv
= list_entry(id_priv
->listen_list
.next
,
1756 struct rdma_id_private
, listen_list
);
1757 /* sync with device removal to avoid duplicate destruction */
1758 list_del_init(&dev_id_priv
->list
);
1759 list_del(&dev_id_priv
->listen_list
);
1760 mutex_unlock(&lock
);
1762 rdma_destroy_id(&dev_id_priv
->id
);
1765 mutex_unlock(&lock
);
1768 static void cma_cancel_operation(struct rdma_id_private
*id_priv
,
1769 enum rdma_cm_state state
)
1772 case RDMA_CM_ADDR_QUERY
:
1773 rdma_addr_cancel(&id_priv
->id
.route
.addr
.dev_addr
);
1775 case RDMA_CM_ROUTE_QUERY
:
1776 cma_cancel_route(id_priv
);
1778 case RDMA_CM_LISTEN
:
1779 if (cma_any_addr(cma_src_addr(id_priv
)) && !id_priv
->cma_dev
)
1780 cma_cancel_listens(id_priv
);
1787 static void cma_release_port(struct rdma_id_private
*id_priv
)
1789 struct rdma_bind_list
*bind_list
= id_priv
->bind_list
;
1790 struct net
*net
= id_priv
->id
.route
.addr
.dev_addr
.net
;
1796 hlist_del(&id_priv
->node
);
1797 if (hlist_empty(&bind_list
->owners
)) {
1798 cma_ps_remove(net
, bind_list
->ps
, bind_list
->port
);
1801 mutex_unlock(&lock
);
1804 static void destroy_mc(struct rdma_id_private
*id_priv
,
1805 struct cma_multicast
*mc
)
1807 if (rdma_cap_ib_mcast(id_priv
->id
.device
, id_priv
->id
.port_num
))
1808 ib_sa_free_multicast(mc
->sa_mc
);
1810 if (rdma_protocol_roce(id_priv
->id
.device
, id_priv
->id
.port_num
)) {
1811 struct rdma_dev_addr
*dev_addr
=
1812 &id_priv
->id
.route
.addr
.dev_addr
;
1813 struct net_device
*ndev
= NULL
;
1815 if (dev_addr
->bound_dev_if
)
1816 ndev
= dev_get_by_index(dev_addr
->net
,
1817 dev_addr
->bound_dev_if
);
1821 cma_set_mgid(id_priv
, (struct sockaddr
*)&mc
->addr
,
1823 cma_igmp_send(ndev
, &mgid
, false);
1830 static void cma_leave_mc_groups(struct rdma_id_private
*id_priv
)
1832 struct cma_multicast
*mc
;
1834 while (!list_empty(&id_priv
->mc_list
)) {
1835 mc
= list_first_entry(&id_priv
->mc_list
, struct cma_multicast
,
1837 list_del(&mc
->list
);
1838 destroy_mc(id_priv
, mc
);
1842 static void _destroy_id(struct rdma_id_private
*id_priv
,
1843 enum rdma_cm_state state
)
1845 cma_cancel_operation(id_priv
, state
);
1847 if (id_priv
->cma_dev
) {
1848 if (rdma_cap_ib_cm(id_priv
->id
.device
, 1)) {
1849 if (id_priv
->cm_id
.ib
)
1850 ib_destroy_cm_id(id_priv
->cm_id
.ib
);
1851 } else if (rdma_cap_iw_cm(id_priv
->id
.device
, 1)) {
1852 if (id_priv
->cm_id
.iw
)
1853 iw_destroy_cm_id(id_priv
->cm_id
.iw
);
1855 cma_leave_mc_groups(id_priv
);
1856 cma_release_dev(id_priv
);
1859 cma_release_port(id_priv
);
1860 cma_id_put(id_priv
);
1861 wait_for_completion(&id_priv
->comp
);
1863 if (id_priv
->internal_id
)
1864 cma_id_put(id_priv
->id
.context
);
1866 kfree(id_priv
->id
.route
.path_rec
);
1868 put_net(id_priv
->id
.route
.addr
.dev_addr
.net
);
1869 rdma_restrack_del(&id_priv
->res
);
1874 * destroy an ID from within the handler_mutex. This ensures that no other
1875 * handlers can start running concurrently.
1877 static void destroy_id_handler_unlock(struct rdma_id_private
*id_priv
)
1878 __releases(&idprv
->handler_mutex
)
1880 enum rdma_cm_state state
;
1881 unsigned long flags
;
1883 trace_cm_id_destroy(id_priv
);
1886 * Setting the state to destroyed under the handler mutex provides a
1887 * fence against calling handler callbacks. If this is invoked due to
1888 * the failure of a handler callback then it guarentees that no future
1889 * handlers will be called.
1891 lockdep_assert_held(&id_priv
->handler_mutex
);
1892 spin_lock_irqsave(&id_priv
->lock
, flags
);
1893 state
= id_priv
->state
;
1894 id_priv
->state
= RDMA_CM_DESTROYING
;
1895 spin_unlock_irqrestore(&id_priv
->lock
, flags
);
1896 mutex_unlock(&id_priv
->handler_mutex
);
1897 _destroy_id(id_priv
, state
);
1900 void rdma_destroy_id(struct rdma_cm_id
*id
)
1902 struct rdma_id_private
*id_priv
=
1903 container_of(id
, struct rdma_id_private
, id
);
1905 mutex_lock(&id_priv
->handler_mutex
);
1906 destroy_id_handler_unlock(id_priv
);
1908 EXPORT_SYMBOL(rdma_destroy_id
);
1910 static int cma_rep_recv(struct rdma_id_private
*id_priv
)
1914 ret
= cma_modify_qp_rtr(id_priv
, NULL
);
1918 ret
= cma_modify_qp_rts(id_priv
, NULL
);
1922 trace_cm_send_rtu(id_priv
);
1923 ret
= ib_send_cm_rtu(id_priv
->cm_id
.ib
, NULL
, 0);
1929 pr_debug_ratelimited("RDMA CM: CONNECT_ERROR: failed to handle reply. status %d\n", ret
);
1930 cma_modify_qp_err(id_priv
);
1931 trace_cm_send_rej(id_priv
);
1932 ib_send_cm_rej(id_priv
->cm_id
.ib
, IB_CM_REJ_CONSUMER_DEFINED
,
1937 static void cma_set_rep_event_data(struct rdma_cm_event
*event
,
1938 const struct ib_cm_rep_event_param
*rep_data
,
1941 event
->param
.conn
.private_data
= private_data
;
1942 event
->param
.conn
.private_data_len
= IB_CM_REP_PRIVATE_DATA_SIZE
;
1943 event
->param
.conn
.responder_resources
= rep_data
->responder_resources
;
1944 event
->param
.conn
.initiator_depth
= rep_data
->initiator_depth
;
1945 event
->param
.conn
.flow_control
= rep_data
->flow_control
;
1946 event
->param
.conn
.rnr_retry_count
= rep_data
->rnr_retry_count
;
1947 event
->param
.conn
.srq
= rep_data
->srq
;
1948 event
->param
.conn
.qp_num
= rep_data
->remote_qpn
;
1950 event
->ece
.vendor_id
= rep_data
->ece
.vendor_id
;
1951 event
->ece
.attr_mod
= rep_data
->ece
.attr_mod
;
1954 static int cma_cm_event_handler(struct rdma_id_private
*id_priv
,
1955 struct rdma_cm_event
*event
)
1959 lockdep_assert_held(&id_priv
->handler_mutex
);
1961 trace_cm_event_handler(id_priv
, event
);
1962 ret
= id_priv
->id
.event_handler(&id_priv
->id
, event
);
1963 trace_cm_event_done(id_priv
, event
, ret
);
1967 static int cma_ib_handler(struct ib_cm_id
*cm_id
,
1968 const struct ib_cm_event
*ib_event
)
1970 struct rdma_id_private
*id_priv
= cm_id
->context
;
1971 struct rdma_cm_event event
= {};
1972 enum rdma_cm_state state
;
1975 mutex_lock(&id_priv
->handler_mutex
);
1976 state
= READ_ONCE(id_priv
->state
);
1977 if ((ib_event
->event
!= IB_CM_TIMEWAIT_EXIT
&&
1978 state
!= RDMA_CM_CONNECT
) ||
1979 (ib_event
->event
== IB_CM_TIMEWAIT_EXIT
&&
1980 state
!= RDMA_CM_DISCONNECT
))
1983 switch (ib_event
->event
) {
1984 case IB_CM_REQ_ERROR
:
1985 case IB_CM_REP_ERROR
:
1986 event
.event
= RDMA_CM_EVENT_UNREACHABLE
;
1987 event
.status
= -ETIMEDOUT
;
1989 case IB_CM_REP_RECEIVED
:
1990 if (state
== RDMA_CM_CONNECT
&&
1991 (id_priv
->id
.qp_type
!= IB_QPT_UD
)) {
1992 trace_cm_send_mra(id_priv
);
1993 ib_send_cm_mra(cm_id
, CMA_CM_MRA_SETTING
, NULL
, 0);
1995 if (id_priv
->id
.qp
) {
1996 event
.status
= cma_rep_recv(id_priv
);
1997 event
.event
= event
.status
? RDMA_CM_EVENT_CONNECT_ERROR
:
1998 RDMA_CM_EVENT_ESTABLISHED
;
2000 event
.event
= RDMA_CM_EVENT_CONNECT_RESPONSE
;
2002 cma_set_rep_event_data(&event
, &ib_event
->param
.rep_rcvd
,
2003 ib_event
->private_data
);
2005 case IB_CM_RTU_RECEIVED
:
2006 case IB_CM_USER_ESTABLISHED
:
2007 event
.event
= RDMA_CM_EVENT_ESTABLISHED
;
2009 case IB_CM_DREQ_ERROR
:
2010 event
.status
= -ETIMEDOUT
;
2012 case IB_CM_DREQ_RECEIVED
:
2013 case IB_CM_DREP_RECEIVED
:
2014 if (!cma_comp_exch(id_priv
, RDMA_CM_CONNECT
,
2015 RDMA_CM_DISCONNECT
))
2017 event
.event
= RDMA_CM_EVENT_DISCONNECTED
;
2019 case IB_CM_TIMEWAIT_EXIT
:
2020 event
.event
= RDMA_CM_EVENT_TIMEWAIT_EXIT
;
2022 case IB_CM_MRA_RECEIVED
:
2025 case IB_CM_REJ_RECEIVED
:
2026 pr_debug_ratelimited("RDMA CM: REJECTED: %s\n", rdma_reject_msg(&id_priv
->id
,
2027 ib_event
->param
.rej_rcvd
.reason
));
2028 cma_modify_qp_err(id_priv
);
2029 event
.status
= ib_event
->param
.rej_rcvd
.reason
;
2030 event
.event
= RDMA_CM_EVENT_REJECTED
;
2031 event
.param
.conn
.private_data
= ib_event
->private_data
;
2032 event
.param
.conn
.private_data_len
= IB_CM_REJ_PRIVATE_DATA_SIZE
;
2035 pr_err("RDMA CMA: unexpected IB CM event: %d\n",
2040 ret
= cma_cm_event_handler(id_priv
, &event
);
2042 /* Destroy the CM ID by returning a non-zero value. */
2043 id_priv
->cm_id
.ib
= NULL
;
2044 destroy_id_handler_unlock(id_priv
);
2048 mutex_unlock(&id_priv
->handler_mutex
);
2052 static struct rdma_id_private
*
2053 cma_ib_new_conn_id(const struct rdma_cm_id
*listen_id
,
2054 const struct ib_cm_event
*ib_event
,
2055 struct net_device
*net_dev
)
2057 struct rdma_id_private
*listen_id_priv
;
2058 struct rdma_id_private
*id_priv
;
2059 struct rdma_cm_id
*id
;
2060 struct rdma_route
*rt
;
2061 const sa_family_t ss_family
= listen_id
->route
.addr
.src_addr
.ss_family
;
2062 struct sa_path_rec
*path
= ib_event
->param
.req_rcvd
.primary_path
;
2063 const __be64 service_id
=
2064 ib_event
->param
.req_rcvd
.primary_path
->service_id
;
2067 listen_id_priv
= container_of(listen_id
, struct rdma_id_private
, id
);
2068 id_priv
= __rdma_create_id(listen_id
->route
.addr
.dev_addr
.net
,
2069 listen_id
->event_handler
, listen_id
->context
,
2071 ib_event
->param
.req_rcvd
.qp_type
,
2073 if (IS_ERR(id_priv
))
2077 if (cma_save_net_info((struct sockaddr
*)&id
->route
.addr
.src_addr
,
2078 (struct sockaddr
*)&id
->route
.addr
.dst_addr
,
2079 listen_id
, ib_event
, ss_family
, service_id
))
2083 rt
->num_paths
= ib_event
->param
.req_rcvd
.alternate_path
? 2 : 1;
2084 rt
->path_rec
= kmalloc_array(rt
->num_paths
, sizeof(*rt
->path_rec
),
2089 rt
->path_rec
[0] = *path
;
2090 if (rt
->num_paths
== 2)
2091 rt
->path_rec
[1] = *ib_event
->param
.req_rcvd
.alternate_path
;
2094 rdma_copy_src_l2_addr(&rt
->addr
.dev_addr
, net_dev
);
2096 if (!cma_protocol_roce(listen_id
) &&
2097 cma_any_addr(cma_src_addr(id_priv
))) {
2098 rt
->addr
.dev_addr
.dev_type
= ARPHRD_INFINIBAND
;
2099 rdma_addr_set_sgid(&rt
->addr
.dev_addr
, &rt
->path_rec
[0].sgid
);
2100 ib_addr_set_pkey(&rt
->addr
.dev_addr
, be16_to_cpu(rt
->path_rec
[0].pkey
));
2101 } else if (!cma_any_addr(cma_src_addr(id_priv
))) {
2102 ret
= cma_translate_addr(cma_src_addr(id_priv
), &rt
->addr
.dev_addr
);
2107 rdma_addr_set_dgid(&rt
->addr
.dev_addr
, &rt
->path_rec
[0].dgid
);
2109 id_priv
->state
= RDMA_CM_CONNECT
;
2113 rdma_destroy_id(id
);
2117 static struct rdma_id_private
*
2118 cma_ib_new_udp_id(const struct rdma_cm_id
*listen_id
,
2119 const struct ib_cm_event
*ib_event
,
2120 struct net_device
*net_dev
)
2122 const struct rdma_id_private
*listen_id_priv
;
2123 struct rdma_id_private
*id_priv
;
2124 struct rdma_cm_id
*id
;
2125 const sa_family_t ss_family
= listen_id
->route
.addr
.src_addr
.ss_family
;
2126 struct net
*net
= listen_id
->route
.addr
.dev_addr
.net
;
2129 listen_id_priv
= container_of(listen_id
, struct rdma_id_private
, id
);
2130 id_priv
= __rdma_create_id(net
, listen_id
->event_handler
,
2131 listen_id
->context
, listen_id
->ps
, IB_QPT_UD
,
2133 if (IS_ERR(id_priv
))
2137 if (cma_save_net_info((struct sockaddr
*)&id
->route
.addr
.src_addr
,
2138 (struct sockaddr
*)&id
->route
.addr
.dst_addr
,
2139 listen_id
, ib_event
, ss_family
,
2140 ib_event
->param
.sidr_req_rcvd
.service_id
))
2144 rdma_copy_src_l2_addr(&id
->route
.addr
.dev_addr
, net_dev
);
2146 if (!cma_any_addr(cma_src_addr(id_priv
))) {
2147 ret
= cma_translate_addr(cma_src_addr(id_priv
),
2148 &id
->route
.addr
.dev_addr
);
2154 id_priv
->state
= RDMA_CM_CONNECT
;
2157 rdma_destroy_id(id
);
2161 static void cma_set_req_event_data(struct rdma_cm_event
*event
,
2162 const struct ib_cm_req_event_param
*req_data
,
2163 void *private_data
, int offset
)
2165 event
->param
.conn
.private_data
= private_data
+ offset
;
2166 event
->param
.conn
.private_data_len
= IB_CM_REQ_PRIVATE_DATA_SIZE
- offset
;
2167 event
->param
.conn
.responder_resources
= req_data
->responder_resources
;
2168 event
->param
.conn
.initiator_depth
= req_data
->initiator_depth
;
2169 event
->param
.conn
.flow_control
= req_data
->flow_control
;
2170 event
->param
.conn
.retry_count
= req_data
->retry_count
;
2171 event
->param
.conn
.rnr_retry_count
= req_data
->rnr_retry_count
;
2172 event
->param
.conn
.srq
= req_data
->srq
;
2173 event
->param
.conn
.qp_num
= req_data
->remote_qpn
;
2175 event
->ece
.vendor_id
= req_data
->ece
.vendor_id
;
2176 event
->ece
.attr_mod
= req_data
->ece
.attr_mod
;
2179 static int cma_ib_check_req_qp_type(const struct rdma_cm_id
*id
,
2180 const struct ib_cm_event
*ib_event
)
2182 return (((ib_event
->event
== IB_CM_REQ_RECEIVED
) &&
2183 (ib_event
->param
.req_rcvd
.qp_type
== id
->qp_type
)) ||
2184 ((ib_event
->event
== IB_CM_SIDR_REQ_RECEIVED
) &&
2185 (id
->qp_type
== IB_QPT_UD
)) ||
2189 static int cma_ib_req_handler(struct ib_cm_id
*cm_id
,
2190 const struct ib_cm_event
*ib_event
)
2192 struct rdma_id_private
*listen_id
, *conn_id
= NULL
;
2193 struct rdma_cm_event event
= {};
2194 struct cma_req_info req
= {};
2195 struct net_device
*net_dev
;
2199 listen_id
= cma_ib_id_from_event(cm_id
, ib_event
, &req
, &net_dev
);
2200 if (IS_ERR(listen_id
))
2201 return PTR_ERR(listen_id
);
2203 trace_cm_req_handler(listen_id
, ib_event
->event
);
2204 if (!cma_ib_check_req_qp_type(&listen_id
->id
, ib_event
)) {
2209 mutex_lock(&listen_id
->handler_mutex
);
2210 if (READ_ONCE(listen_id
->state
) != RDMA_CM_LISTEN
) {
2211 ret
= -ECONNABORTED
;
2215 offset
= cma_user_data_offset(listen_id
);
2216 event
.event
= RDMA_CM_EVENT_CONNECT_REQUEST
;
2217 if (ib_event
->event
== IB_CM_SIDR_REQ_RECEIVED
) {
2218 conn_id
= cma_ib_new_udp_id(&listen_id
->id
, ib_event
, net_dev
);
2219 event
.param
.ud
.private_data
= ib_event
->private_data
+ offset
;
2220 event
.param
.ud
.private_data_len
=
2221 IB_CM_SIDR_REQ_PRIVATE_DATA_SIZE
- offset
;
2223 conn_id
= cma_ib_new_conn_id(&listen_id
->id
, ib_event
, net_dev
);
2224 cma_set_req_event_data(&event
, &ib_event
->param
.req_rcvd
,
2225 ib_event
->private_data
, offset
);
2232 mutex_lock_nested(&conn_id
->handler_mutex
, SINGLE_DEPTH_NESTING
);
2233 ret
= cma_ib_acquire_dev(conn_id
, listen_id
, &req
);
2235 destroy_id_handler_unlock(conn_id
);
2239 conn_id
->cm_id
.ib
= cm_id
;
2240 cm_id
->context
= conn_id
;
2241 cm_id
->cm_handler
= cma_ib_handler
;
2243 ret
= cma_cm_event_handler(conn_id
, &event
);
2245 /* Destroy the CM ID by returning a non-zero value. */
2246 conn_id
->cm_id
.ib
= NULL
;
2247 mutex_unlock(&listen_id
->handler_mutex
);
2248 destroy_id_handler_unlock(conn_id
);
2252 if (READ_ONCE(conn_id
->state
) == RDMA_CM_CONNECT
&&
2253 conn_id
->id
.qp_type
!= IB_QPT_UD
) {
2254 trace_cm_send_mra(cm_id
->context
);
2255 ib_send_cm_mra(cm_id
, CMA_CM_MRA_SETTING
, NULL
, 0);
2257 mutex_unlock(&conn_id
->handler_mutex
);
2260 mutex_unlock(&listen_id
->handler_mutex
);
2269 __be64
rdma_get_service_id(struct rdma_cm_id
*id
, struct sockaddr
*addr
)
2271 if (addr
->sa_family
== AF_IB
)
2272 return ((struct sockaddr_ib
*) addr
)->sib_sid
;
2274 return cpu_to_be64(((u64
)id
->ps
<< 16) + be16_to_cpu(cma_port(addr
)));
2276 EXPORT_SYMBOL(rdma_get_service_id
);
2278 void rdma_read_gids(struct rdma_cm_id
*cm_id
, union ib_gid
*sgid
,
2281 struct rdma_addr
*addr
= &cm_id
->route
.addr
;
2283 if (!cm_id
->device
) {
2285 memset(sgid
, 0, sizeof(*sgid
));
2287 memset(dgid
, 0, sizeof(*dgid
));
2291 if (rdma_protocol_roce(cm_id
->device
, cm_id
->port_num
)) {
2293 rdma_ip2gid((struct sockaddr
*)&addr
->src_addr
, sgid
);
2295 rdma_ip2gid((struct sockaddr
*)&addr
->dst_addr
, dgid
);
2298 rdma_addr_get_sgid(&addr
->dev_addr
, sgid
);
2300 rdma_addr_get_dgid(&addr
->dev_addr
, dgid
);
2303 EXPORT_SYMBOL(rdma_read_gids
);
2305 static int cma_iw_handler(struct iw_cm_id
*iw_id
, struct iw_cm_event
*iw_event
)
2307 struct rdma_id_private
*id_priv
= iw_id
->context
;
2308 struct rdma_cm_event event
= {};
2310 struct sockaddr
*laddr
= (struct sockaddr
*)&iw_event
->local_addr
;
2311 struct sockaddr
*raddr
= (struct sockaddr
*)&iw_event
->remote_addr
;
2313 mutex_lock(&id_priv
->handler_mutex
);
2314 if (READ_ONCE(id_priv
->state
) != RDMA_CM_CONNECT
)
2317 switch (iw_event
->event
) {
2318 case IW_CM_EVENT_CLOSE
:
2319 event
.event
= RDMA_CM_EVENT_DISCONNECTED
;
2321 case IW_CM_EVENT_CONNECT_REPLY
:
2322 memcpy(cma_src_addr(id_priv
), laddr
,
2323 rdma_addr_size(laddr
));
2324 memcpy(cma_dst_addr(id_priv
), raddr
,
2325 rdma_addr_size(raddr
));
2326 switch (iw_event
->status
) {
2328 event
.event
= RDMA_CM_EVENT_ESTABLISHED
;
2329 event
.param
.conn
.initiator_depth
= iw_event
->ird
;
2330 event
.param
.conn
.responder_resources
= iw_event
->ord
;
2334 event
.event
= RDMA_CM_EVENT_REJECTED
;
2337 event
.event
= RDMA_CM_EVENT_UNREACHABLE
;
2340 event
.event
= RDMA_CM_EVENT_CONNECT_ERROR
;
2344 case IW_CM_EVENT_ESTABLISHED
:
2345 event
.event
= RDMA_CM_EVENT_ESTABLISHED
;
2346 event
.param
.conn
.initiator_depth
= iw_event
->ird
;
2347 event
.param
.conn
.responder_resources
= iw_event
->ord
;
2353 event
.status
= iw_event
->status
;
2354 event
.param
.conn
.private_data
= iw_event
->private_data
;
2355 event
.param
.conn
.private_data_len
= iw_event
->private_data_len
;
2356 ret
= cma_cm_event_handler(id_priv
, &event
);
2358 /* Destroy the CM ID by returning a non-zero value. */
2359 id_priv
->cm_id
.iw
= NULL
;
2360 destroy_id_handler_unlock(id_priv
);
2365 mutex_unlock(&id_priv
->handler_mutex
);
2369 static int iw_conn_req_handler(struct iw_cm_id
*cm_id
,
2370 struct iw_cm_event
*iw_event
)
2372 struct rdma_id_private
*listen_id
, *conn_id
;
2373 struct rdma_cm_event event
= {};
2374 int ret
= -ECONNABORTED
;
2375 struct sockaddr
*laddr
= (struct sockaddr
*)&iw_event
->local_addr
;
2376 struct sockaddr
*raddr
= (struct sockaddr
*)&iw_event
->remote_addr
;
2378 event
.event
= RDMA_CM_EVENT_CONNECT_REQUEST
;
2379 event
.param
.conn
.private_data
= iw_event
->private_data
;
2380 event
.param
.conn
.private_data_len
= iw_event
->private_data_len
;
2381 event
.param
.conn
.initiator_depth
= iw_event
->ird
;
2382 event
.param
.conn
.responder_resources
= iw_event
->ord
;
2384 listen_id
= cm_id
->context
;
2386 mutex_lock(&listen_id
->handler_mutex
);
2387 if (READ_ONCE(listen_id
->state
) != RDMA_CM_LISTEN
)
2390 /* Create a new RDMA id for the new IW CM ID */
2391 conn_id
= __rdma_create_id(listen_id
->id
.route
.addr
.dev_addr
.net
,
2392 listen_id
->id
.event_handler
,
2393 listen_id
->id
.context
, RDMA_PS_TCP
,
2394 IB_QPT_RC
, listen_id
);
2395 if (IS_ERR(conn_id
)) {
2399 mutex_lock_nested(&conn_id
->handler_mutex
, SINGLE_DEPTH_NESTING
);
2400 conn_id
->state
= RDMA_CM_CONNECT
;
2402 ret
= rdma_translate_ip(laddr
, &conn_id
->id
.route
.addr
.dev_addr
);
2404 mutex_unlock(&listen_id
->handler_mutex
);
2405 destroy_id_handler_unlock(conn_id
);
2409 ret
= cma_iw_acquire_dev(conn_id
, listen_id
);
2411 mutex_unlock(&listen_id
->handler_mutex
);
2412 destroy_id_handler_unlock(conn_id
);
2416 conn_id
->cm_id
.iw
= cm_id
;
2417 cm_id
->context
= conn_id
;
2418 cm_id
->cm_handler
= cma_iw_handler
;
2420 memcpy(cma_src_addr(conn_id
), laddr
, rdma_addr_size(laddr
));
2421 memcpy(cma_dst_addr(conn_id
), raddr
, rdma_addr_size(raddr
));
2423 ret
= cma_cm_event_handler(conn_id
, &event
);
2425 /* User wants to destroy the CM ID */
2426 conn_id
->cm_id
.iw
= NULL
;
2427 mutex_unlock(&listen_id
->handler_mutex
);
2428 destroy_id_handler_unlock(conn_id
);
2432 mutex_unlock(&conn_id
->handler_mutex
);
2435 mutex_unlock(&listen_id
->handler_mutex
);
2439 static int cma_ib_listen(struct rdma_id_private
*id_priv
)
2441 struct sockaddr
*addr
;
2442 struct ib_cm_id
*id
;
2445 addr
= cma_src_addr(id_priv
);
2446 svc_id
= rdma_get_service_id(&id_priv
->id
, addr
);
2447 id
= ib_cm_insert_listen(id_priv
->id
.device
,
2448 cma_ib_req_handler
, svc_id
);
2451 id_priv
->cm_id
.ib
= id
;
2456 static int cma_iw_listen(struct rdma_id_private
*id_priv
, int backlog
)
2459 struct iw_cm_id
*id
;
2461 id
= iw_create_cm_id(id_priv
->id
.device
,
2462 iw_conn_req_handler
,
2467 id
->tos
= id_priv
->tos
;
2468 id
->tos_set
= id_priv
->tos_set
;
2469 id_priv
->cm_id
.iw
= id
;
2471 memcpy(&id_priv
->cm_id
.iw
->local_addr
, cma_src_addr(id_priv
),
2472 rdma_addr_size(cma_src_addr(id_priv
)));
2474 ret
= iw_cm_listen(id_priv
->cm_id
.iw
, backlog
);
2477 iw_destroy_cm_id(id_priv
->cm_id
.iw
);
2478 id_priv
->cm_id
.iw
= NULL
;
2484 static int cma_listen_handler(struct rdma_cm_id
*id
,
2485 struct rdma_cm_event
*event
)
2487 struct rdma_id_private
*id_priv
= id
->context
;
2489 /* Listening IDs are always destroyed on removal */
2490 if (event
->event
== RDMA_CM_EVENT_DEVICE_REMOVAL
)
2493 id
->context
= id_priv
->id
.context
;
2494 id
->event_handler
= id_priv
->id
.event_handler
;
2495 trace_cm_event_handler(id_priv
, event
);
2496 return id_priv
->id
.event_handler(id
, event
);
2499 static int cma_listen_on_dev(struct rdma_id_private
*id_priv
,
2500 struct cma_device
*cma_dev
,
2501 struct rdma_id_private
**to_destroy
)
2503 struct rdma_id_private
*dev_id_priv
;
2504 struct net
*net
= id_priv
->id
.route
.addr
.dev_addr
.net
;
2507 lockdep_assert_held(&lock
);
2510 if (cma_family(id_priv
) == AF_IB
&& !rdma_cap_ib_cm(cma_dev
->device
, 1))
2514 __rdma_create_id(net
, cma_listen_handler
, id_priv
,
2515 id_priv
->id
.ps
, id_priv
->id
.qp_type
, id_priv
);
2516 if (IS_ERR(dev_id_priv
))
2517 return PTR_ERR(dev_id_priv
);
2519 dev_id_priv
->state
= RDMA_CM_ADDR_BOUND
;
2520 memcpy(cma_src_addr(dev_id_priv
), cma_src_addr(id_priv
),
2521 rdma_addr_size(cma_src_addr(id_priv
)));
2523 _cma_attach_to_dev(dev_id_priv
, cma_dev
);
2524 cma_id_get(id_priv
);
2525 dev_id_priv
->internal_id
= 1;
2526 dev_id_priv
->afonly
= id_priv
->afonly
;
2527 dev_id_priv
->tos_set
= id_priv
->tos_set
;
2528 dev_id_priv
->tos
= id_priv
->tos
;
2530 ret
= rdma_listen(&dev_id_priv
->id
, id_priv
->backlog
);
2533 list_add_tail(&dev_id_priv
->listen_list
, &id_priv
->listen_list
);
2536 /* Caller must destroy this after releasing lock */
2537 *to_destroy
= dev_id_priv
;
2538 dev_warn(&cma_dev
->device
->dev
, "RDMA CMA: %s, error %d\n", __func__
, ret
);
2542 static int cma_listen_on_all(struct rdma_id_private
*id_priv
)
2544 struct rdma_id_private
*to_destroy
;
2545 struct cma_device
*cma_dev
;
2549 list_add_tail(&id_priv
->list
, &listen_any_list
);
2550 list_for_each_entry(cma_dev
, &dev_list
, list
) {
2551 ret
= cma_listen_on_dev(id_priv
, cma_dev
, &to_destroy
);
2553 /* Prevent racing with cma_process_remove() */
2555 list_del_init(&to_destroy
->list
);
2559 mutex_unlock(&lock
);
2563 list_del(&id_priv
->list
);
2564 mutex_unlock(&lock
);
2566 rdma_destroy_id(&to_destroy
->id
);
2570 void rdma_set_service_type(struct rdma_cm_id
*id
, int tos
)
2572 struct rdma_id_private
*id_priv
;
2574 id_priv
= container_of(id
, struct rdma_id_private
, id
);
2575 id_priv
->tos
= (u8
) tos
;
2576 id_priv
->tos_set
= true;
2578 EXPORT_SYMBOL(rdma_set_service_type
);
2581 * rdma_set_ack_timeout() - Set the ack timeout of QP associated
2582 * with a connection identifier.
2583 * @id: Communication identifier to associated with service type.
2584 * @timeout: Ack timeout to set a QP, expressed as 4.096 * 2^(timeout) usec.
2586 * This function should be called before rdma_connect() on active side,
2587 * and on passive side before rdma_accept(). It is applicable to primary
2588 * path only. The timeout will affect the local side of the QP, it is not
2589 * negotiated with remote side and zero disables the timer. In case it is
2590 * set before rdma_resolve_route, the value will also be used to determine
2591 * PacketLifeTime for RoCE.
2593 * Return: 0 for success
2595 int rdma_set_ack_timeout(struct rdma_cm_id
*id
, u8 timeout
)
2597 struct rdma_id_private
*id_priv
;
2599 if (id
->qp_type
!= IB_QPT_RC
)
2602 id_priv
= container_of(id
, struct rdma_id_private
, id
);
2603 id_priv
->timeout
= timeout
;
2604 id_priv
->timeout_set
= true;
2608 EXPORT_SYMBOL(rdma_set_ack_timeout
);
2610 static void cma_query_handler(int status
, struct sa_path_rec
*path_rec
,
2613 struct cma_work
*work
= context
;
2614 struct rdma_route
*route
;
2616 route
= &work
->id
->id
.route
;
2619 route
->num_paths
= 1;
2620 *route
->path_rec
= *path_rec
;
2622 work
->old_state
= RDMA_CM_ROUTE_QUERY
;
2623 work
->new_state
= RDMA_CM_ADDR_RESOLVED
;
2624 work
->event
.event
= RDMA_CM_EVENT_ROUTE_ERROR
;
2625 work
->event
.status
= status
;
2626 pr_debug_ratelimited("RDMA CM: ROUTE_ERROR: failed to query path. status %d\n",
2630 queue_work(cma_wq
, &work
->work
);
2633 static int cma_query_ib_route(struct rdma_id_private
*id_priv
,
2634 unsigned long timeout_ms
, struct cma_work
*work
)
2636 struct rdma_dev_addr
*dev_addr
= &id_priv
->id
.route
.addr
.dev_addr
;
2637 struct sa_path_rec path_rec
;
2638 ib_sa_comp_mask comp_mask
;
2639 struct sockaddr_in6
*sin6
;
2640 struct sockaddr_ib
*sib
;
2642 memset(&path_rec
, 0, sizeof path_rec
);
2644 if (rdma_cap_opa_ah(id_priv
->id
.device
, id_priv
->id
.port_num
))
2645 path_rec
.rec_type
= SA_PATH_REC_TYPE_OPA
;
2647 path_rec
.rec_type
= SA_PATH_REC_TYPE_IB
;
2648 rdma_addr_get_sgid(dev_addr
, &path_rec
.sgid
);
2649 rdma_addr_get_dgid(dev_addr
, &path_rec
.dgid
);
2650 path_rec
.pkey
= cpu_to_be16(ib_addr_get_pkey(dev_addr
));
2651 path_rec
.numb_path
= 1;
2652 path_rec
.reversible
= 1;
2653 path_rec
.service_id
= rdma_get_service_id(&id_priv
->id
,
2654 cma_dst_addr(id_priv
));
2656 comp_mask
= IB_SA_PATH_REC_DGID
| IB_SA_PATH_REC_SGID
|
2657 IB_SA_PATH_REC_PKEY
| IB_SA_PATH_REC_NUMB_PATH
|
2658 IB_SA_PATH_REC_REVERSIBLE
| IB_SA_PATH_REC_SERVICE_ID
;
2660 switch (cma_family(id_priv
)) {
2662 path_rec
.qos_class
= cpu_to_be16((u16
) id_priv
->tos
);
2663 comp_mask
|= IB_SA_PATH_REC_QOS_CLASS
;
2666 sin6
= (struct sockaddr_in6
*) cma_src_addr(id_priv
);
2667 path_rec
.traffic_class
= (u8
) (be32_to_cpu(sin6
->sin6_flowinfo
) >> 20);
2668 comp_mask
|= IB_SA_PATH_REC_TRAFFIC_CLASS
;
2671 sib
= (struct sockaddr_ib
*) cma_src_addr(id_priv
);
2672 path_rec
.traffic_class
= (u8
) (be32_to_cpu(sib
->sib_flowinfo
) >> 20);
2673 comp_mask
|= IB_SA_PATH_REC_TRAFFIC_CLASS
;
2677 id_priv
->query_id
= ib_sa_path_rec_get(&sa_client
, id_priv
->id
.device
,
2678 id_priv
->id
.port_num
, &path_rec
,
2679 comp_mask
, timeout_ms
,
2680 GFP_KERNEL
, cma_query_handler
,
2681 work
, &id_priv
->query
);
2683 return (id_priv
->query_id
< 0) ? id_priv
->query_id
: 0;
2686 static void cma_work_handler(struct work_struct
*_work
)
2688 struct cma_work
*work
= container_of(_work
, struct cma_work
, work
);
2689 struct rdma_id_private
*id_priv
= work
->id
;
2691 mutex_lock(&id_priv
->handler_mutex
);
2692 if (READ_ONCE(id_priv
->state
) == RDMA_CM_DESTROYING
||
2693 READ_ONCE(id_priv
->state
) == RDMA_CM_DEVICE_REMOVAL
)
2695 if (work
->old_state
!= 0 || work
->new_state
!= 0) {
2696 if (!cma_comp_exch(id_priv
, work
->old_state
, work
->new_state
))
2700 if (cma_cm_event_handler(id_priv
, &work
->event
)) {
2701 cma_id_put(id_priv
);
2702 destroy_id_handler_unlock(id_priv
);
2707 mutex_unlock(&id_priv
->handler_mutex
);
2708 cma_id_put(id_priv
);
2710 if (work
->event
.event
== RDMA_CM_EVENT_MULTICAST_JOIN
)
2711 rdma_destroy_ah_attr(&work
->event
.param
.ud
.ah_attr
);
2715 static void cma_init_resolve_route_work(struct cma_work
*work
,
2716 struct rdma_id_private
*id_priv
)
2719 INIT_WORK(&work
->work
, cma_work_handler
);
2720 work
->old_state
= RDMA_CM_ROUTE_QUERY
;
2721 work
->new_state
= RDMA_CM_ROUTE_RESOLVED
;
2722 work
->event
.event
= RDMA_CM_EVENT_ROUTE_RESOLVED
;
2725 static void enqueue_resolve_addr_work(struct cma_work
*work
,
2726 struct rdma_id_private
*id_priv
)
2728 /* Balances with cma_id_put() in cma_work_handler */
2729 cma_id_get(id_priv
);
2732 INIT_WORK(&work
->work
, cma_work_handler
);
2733 work
->old_state
= RDMA_CM_ADDR_QUERY
;
2734 work
->new_state
= RDMA_CM_ADDR_RESOLVED
;
2735 work
->event
.event
= RDMA_CM_EVENT_ADDR_RESOLVED
;
2737 queue_work(cma_wq
, &work
->work
);
2740 static int cma_resolve_ib_route(struct rdma_id_private
*id_priv
,
2741 unsigned long timeout_ms
)
2743 struct rdma_route
*route
= &id_priv
->id
.route
;
2744 struct cma_work
*work
;
2747 work
= kzalloc(sizeof *work
, GFP_KERNEL
);
2751 cma_init_resolve_route_work(work
, id_priv
);
2753 route
->path_rec
= kmalloc(sizeof *route
->path_rec
, GFP_KERNEL
);
2754 if (!route
->path_rec
) {
2759 ret
= cma_query_ib_route(id_priv
, timeout_ms
, work
);
2765 kfree(route
->path_rec
);
2766 route
->path_rec
= NULL
;
2772 static enum ib_gid_type
cma_route_gid_type(enum rdma_network_type network_type
,
2773 unsigned long supported_gids
,
2774 enum ib_gid_type default_gid
)
2776 if ((network_type
== RDMA_NETWORK_IPV4
||
2777 network_type
== RDMA_NETWORK_IPV6
) &&
2778 test_bit(IB_GID_TYPE_ROCE_UDP_ENCAP
, &supported_gids
))
2779 return IB_GID_TYPE_ROCE_UDP_ENCAP
;
2785 * cma_iboe_set_path_rec_l2_fields() is helper function which sets
2786 * path record type based on GID type.
2787 * It also sets up other L2 fields which includes destination mac address
2788 * netdev ifindex, of the path record.
2789 * It returns the netdev of the bound interface for this path record entry.
2791 static struct net_device
*
2792 cma_iboe_set_path_rec_l2_fields(struct rdma_id_private
*id_priv
)
2794 struct rdma_route
*route
= &id_priv
->id
.route
;
2795 enum ib_gid_type gid_type
= IB_GID_TYPE_ROCE
;
2796 struct rdma_addr
*addr
= &route
->addr
;
2797 unsigned long supported_gids
;
2798 struct net_device
*ndev
;
2800 if (!addr
->dev_addr
.bound_dev_if
)
2803 ndev
= dev_get_by_index(addr
->dev_addr
.net
,
2804 addr
->dev_addr
.bound_dev_if
);
2808 supported_gids
= roce_gid_type_mask_support(id_priv
->id
.device
,
2809 id_priv
->id
.port_num
);
2810 gid_type
= cma_route_gid_type(addr
->dev_addr
.network
,
2813 /* Use the hint from IP Stack to select GID Type */
2814 if (gid_type
< ib_network_to_gid_type(addr
->dev_addr
.network
))
2815 gid_type
= ib_network_to_gid_type(addr
->dev_addr
.network
);
2816 route
->path_rec
->rec_type
= sa_conv_gid_to_pathrec_type(gid_type
);
2818 route
->path_rec
->roce
.route_resolved
= true;
2819 sa_path_set_dmac(route
->path_rec
, addr
->dev_addr
.dst_dev_addr
);
2823 int rdma_set_ib_path(struct rdma_cm_id
*id
,
2824 struct sa_path_rec
*path_rec
)
2826 struct rdma_id_private
*id_priv
;
2827 struct net_device
*ndev
;
2830 id_priv
= container_of(id
, struct rdma_id_private
, id
);
2831 if (!cma_comp_exch(id_priv
, RDMA_CM_ADDR_RESOLVED
,
2832 RDMA_CM_ROUTE_RESOLVED
))
2835 id
->route
.path_rec
= kmemdup(path_rec
, sizeof(*path_rec
),
2837 if (!id
->route
.path_rec
) {
2842 if (rdma_protocol_roce(id
->device
, id
->port_num
)) {
2843 ndev
= cma_iboe_set_path_rec_l2_fields(id_priv
);
2851 id
->route
.num_paths
= 1;
2855 kfree(id
->route
.path_rec
);
2856 id
->route
.path_rec
= NULL
;
2858 cma_comp_exch(id_priv
, RDMA_CM_ROUTE_RESOLVED
, RDMA_CM_ADDR_RESOLVED
);
2861 EXPORT_SYMBOL(rdma_set_ib_path
);
2863 static int cma_resolve_iw_route(struct rdma_id_private
*id_priv
)
2865 struct cma_work
*work
;
2867 work
= kzalloc(sizeof *work
, GFP_KERNEL
);
2871 cma_init_resolve_route_work(work
, id_priv
);
2872 queue_work(cma_wq
, &work
->work
);
2876 static int get_vlan_ndev_tc(struct net_device
*vlan_ndev
, int prio
)
2878 struct net_device
*dev
;
2880 dev
= vlan_dev_real_dev(vlan_ndev
);
2882 return netdev_get_prio_tc_map(dev
, prio
);
2884 return (vlan_dev_get_egress_qos_mask(vlan_ndev
, prio
) &
2885 VLAN_PRIO_MASK
) >> VLAN_PRIO_SHIFT
;
2888 struct iboe_prio_tc_map
{
2894 static int get_lower_vlan_dev_tc(struct net_device
*dev
,
2895 struct netdev_nested_priv
*priv
)
2897 struct iboe_prio_tc_map
*map
= (struct iboe_prio_tc_map
*)priv
->data
;
2899 if (is_vlan_dev(dev
))
2900 map
->output_tc
= get_vlan_ndev_tc(dev
, map
->input_prio
);
2901 else if (dev
->num_tc
)
2902 map
->output_tc
= netdev_get_prio_tc_map(dev
, map
->input_prio
);
2905 /* We are interested only in first level VLAN device, so always
2906 * return 1 to stop iterating over next level devices.
2912 static int iboe_tos_to_sl(struct net_device
*ndev
, int tos
)
2914 struct iboe_prio_tc_map prio_tc_map
= {};
2915 int prio
= rt_tos2priority(tos
);
2916 struct netdev_nested_priv priv
;
2918 /* If VLAN device, get it directly from the VLAN netdev */
2919 if (is_vlan_dev(ndev
))
2920 return get_vlan_ndev_tc(ndev
, prio
);
2922 prio_tc_map
.input_prio
= prio
;
2923 priv
.data
= (void *)&prio_tc_map
;
2925 netdev_walk_all_lower_dev_rcu(ndev
,
2926 get_lower_vlan_dev_tc
,
2929 /* If map is found from lower device, use it; Otherwise
2930 * continue with the current netdevice to get priority to tc map.
2932 if (prio_tc_map
.found
)
2933 return prio_tc_map
.output_tc
;
2934 else if (ndev
->num_tc
)
2935 return netdev_get_prio_tc_map(ndev
, prio
);
2940 static __be32
cma_get_roce_udp_flow_label(struct rdma_id_private
*id_priv
)
2942 struct sockaddr_in6
*addr6
;
2946 addr6
= (struct sockaddr_in6
*)cma_src_addr(id_priv
);
2947 fl
= be32_to_cpu(addr6
->sin6_flowinfo
) & IB_GRH_FLOWLABEL_MASK
;
2948 if ((cma_family(id_priv
) != AF_INET6
) || !fl
) {
2949 dport
= be16_to_cpu(cma_port(cma_dst_addr(id_priv
)));
2950 sport
= be16_to_cpu(cma_port(cma_src_addr(id_priv
)));
2951 hash
= (u32
)sport
* 31 + dport
;
2952 fl
= hash
& IB_GRH_FLOWLABEL_MASK
;
2955 return cpu_to_be32(fl
);
2958 static int cma_resolve_iboe_route(struct rdma_id_private
*id_priv
)
2960 struct rdma_route
*route
= &id_priv
->id
.route
;
2961 struct rdma_addr
*addr
= &route
->addr
;
2962 struct cma_work
*work
;
2964 struct net_device
*ndev
;
2966 u8 default_roce_tos
= id_priv
->cma_dev
->default_roce_tos
[id_priv
->id
.port_num
-
2967 rdma_start_port(id_priv
->cma_dev
->device
)];
2968 u8 tos
= id_priv
->tos_set
? id_priv
->tos
: default_roce_tos
;
2971 work
= kzalloc(sizeof *work
, GFP_KERNEL
);
2975 route
->path_rec
= kzalloc(sizeof *route
->path_rec
, GFP_KERNEL
);
2976 if (!route
->path_rec
) {
2981 route
->num_paths
= 1;
2983 ndev
= cma_iboe_set_path_rec_l2_fields(id_priv
);
2989 rdma_ip2gid((struct sockaddr
*)&id_priv
->id
.route
.addr
.src_addr
,
2990 &route
->path_rec
->sgid
);
2991 rdma_ip2gid((struct sockaddr
*)&id_priv
->id
.route
.addr
.dst_addr
,
2992 &route
->path_rec
->dgid
);
2994 if (((struct sockaddr
*)&id_priv
->id
.route
.addr
.dst_addr
)->sa_family
!= AF_IB
)
2995 /* TODO: get the hoplimit from the inet/inet6 device */
2996 route
->path_rec
->hop_limit
= addr
->dev_addr
.hoplimit
;
2998 route
->path_rec
->hop_limit
= 1;
2999 route
->path_rec
->reversible
= 1;
3000 route
->path_rec
->pkey
= cpu_to_be16(0xffff);
3001 route
->path_rec
->mtu_selector
= IB_SA_EQ
;
3002 route
->path_rec
->sl
= iboe_tos_to_sl(ndev
, tos
);
3003 route
->path_rec
->traffic_class
= tos
;
3004 route
->path_rec
->mtu
= iboe_get_mtu(ndev
->mtu
);
3005 route
->path_rec
->rate_selector
= IB_SA_EQ
;
3006 route
->path_rec
->rate
= iboe_get_rate(ndev
);
3008 route
->path_rec
->packet_life_time_selector
= IB_SA_EQ
;
3009 /* In case ACK timeout is set, use this value to calculate
3010 * PacketLifeTime. As per IBTA 12.7.34,
3011 * local ACK timeout = (2 * PacketLifeTime + Local CA’s ACK delay).
3012 * Assuming a negligible local ACK delay, we can use
3013 * PacketLifeTime = local ACK timeout/2
3014 * as a reasonable approximation for RoCE networks.
3016 route
->path_rec
->packet_life_time
= id_priv
->timeout_set
?
3017 id_priv
->timeout
- 1 : CMA_IBOE_PACKET_LIFETIME
;
3019 if (!route
->path_rec
->mtu
) {
3024 if (rdma_protocol_roce_udp_encap(id_priv
->id
.device
,
3025 id_priv
->id
.port_num
))
3026 route
->path_rec
->flow_label
=
3027 cma_get_roce_udp_flow_label(id_priv
);
3029 cma_init_resolve_route_work(work
, id_priv
);
3030 queue_work(cma_wq
, &work
->work
);
3035 kfree(route
->path_rec
);
3036 route
->path_rec
= NULL
;
3037 route
->num_paths
= 0;
3043 int rdma_resolve_route(struct rdma_cm_id
*id
, unsigned long timeout_ms
)
3045 struct rdma_id_private
*id_priv
;
3048 id_priv
= container_of(id
, struct rdma_id_private
, id
);
3049 if (!cma_comp_exch(id_priv
, RDMA_CM_ADDR_RESOLVED
, RDMA_CM_ROUTE_QUERY
))
3052 cma_id_get(id_priv
);
3053 if (rdma_cap_ib_sa(id
->device
, id
->port_num
))
3054 ret
= cma_resolve_ib_route(id_priv
, timeout_ms
);
3055 else if (rdma_protocol_roce(id
->device
, id
->port_num
))
3056 ret
= cma_resolve_iboe_route(id_priv
);
3057 else if (rdma_protocol_iwarp(id
->device
, id
->port_num
))
3058 ret
= cma_resolve_iw_route(id_priv
);
3067 cma_comp_exch(id_priv
, RDMA_CM_ROUTE_QUERY
, RDMA_CM_ADDR_RESOLVED
);
3068 cma_id_put(id_priv
);
3071 EXPORT_SYMBOL(rdma_resolve_route
);
3073 static void cma_set_loopback(struct sockaddr
*addr
)
3075 switch (addr
->sa_family
) {
3077 ((struct sockaddr_in
*) addr
)->sin_addr
.s_addr
= htonl(INADDR_LOOPBACK
);
3080 ipv6_addr_set(&((struct sockaddr_in6
*) addr
)->sin6_addr
,
3084 ib_addr_set(&((struct sockaddr_ib
*) addr
)->sib_addr
,
3090 static int cma_bind_loopback(struct rdma_id_private
*id_priv
)
3092 struct cma_device
*cma_dev
, *cur_dev
;
3094 enum ib_port_state port_state
;
3101 list_for_each_entry(cur_dev
, &dev_list
, list
) {
3102 if (cma_family(id_priv
) == AF_IB
&&
3103 !rdma_cap_ib_cm(cur_dev
->device
, 1))
3109 rdma_for_each_port (cur_dev
->device
, p
) {
3110 if (!ib_get_cached_port_state(cur_dev
->device
, p
, &port_state
) &&
3111 port_state
== IB_PORT_ACTIVE
) {
3126 ret
= rdma_query_gid(cma_dev
->device
, p
, 0, &gid
);
3130 ret
= ib_get_cached_pkey(cma_dev
->device
, p
, 0, &pkey
);
3134 id_priv
->id
.route
.addr
.dev_addr
.dev_type
=
3135 (rdma_protocol_ib(cma_dev
->device
, p
)) ?
3136 ARPHRD_INFINIBAND
: ARPHRD_ETHER
;
3138 rdma_addr_set_sgid(&id_priv
->id
.route
.addr
.dev_addr
, &gid
);
3139 ib_addr_set_pkey(&id_priv
->id
.route
.addr
.dev_addr
, pkey
);
3140 id_priv
->id
.port_num
= p
;
3141 cma_attach_to_dev(id_priv
, cma_dev
);
3142 cma_set_loopback(cma_src_addr(id_priv
));
3144 mutex_unlock(&lock
);
3148 static void addr_handler(int status
, struct sockaddr
*src_addr
,
3149 struct rdma_dev_addr
*dev_addr
, void *context
)
3151 struct rdma_id_private
*id_priv
= context
;
3152 struct rdma_cm_event event
= {};
3153 struct sockaddr
*addr
;
3154 struct sockaddr_storage old_addr
;
3156 mutex_lock(&id_priv
->handler_mutex
);
3157 if (!cma_comp_exch(id_priv
, RDMA_CM_ADDR_QUERY
,
3158 RDMA_CM_ADDR_RESOLVED
))
3162 * Store the previous src address, so that if we fail to acquire
3163 * matching rdma device, old address can be restored back, which helps
3164 * to cancel the cma listen operation correctly.
3166 addr
= cma_src_addr(id_priv
);
3167 memcpy(&old_addr
, addr
, rdma_addr_size(addr
));
3168 memcpy(addr
, src_addr
, rdma_addr_size(src_addr
));
3169 if (!status
&& !id_priv
->cma_dev
) {
3170 status
= cma_acquire_dev_by_src_ip(id_priv
);
3172 pr_debug_ratelimited("RDMA CM: ADDR_ERROR: failed to acquire device. status %d\n",
3174 } else if (status
) {
3175 pr_debug_ratelimited("RDMA CM: ADDR_ERROR: failed to resolve IP. status %d\n", status
);
3179 memcpy(addr
, &old_addr
,
3180 rdma_addr_size((struct sockaddr
*)&old_addr
));
3181 if (!cma_comp_exch(id_priv
, RDMA_CM_ADDR_RESOLVED
,
3182 RDMA_CM_ADDR_BOUND
))
3184 event
.event
= RDMA_CM_EVENT_ADDR_ERROR
;
3185 event
.status
= status
;
3187 event
.event
= RDMA_CM_EVENT_ADDR_RESOLVED
;
3189 if (cma_cm_event_handler(id_priv
, &event
)) {
3190 destroy_id_handler_unlock(id_priv
);
3194 mutex_unlock(&id_priv
->handler_mutex
);
3197 static int cma_resolve_loopback(struct rdma_id_private
*id_priv
)
3199 struct cma_work
*work
;
3203 work
= kzalloc(sizeof *work
, GFP_KERNEL
);
3207 if (!id_priv
->cma_dev
) {
3208 ret
= cma_bind_loopback(id_priv
);
3213 rdma_addr_get_sgid(&id_priv
->id
.route
.addr
.dev_addr
, &gid
);
3214 rdma_addr_set_dgid(&id_priv
->id
.route
.addr
.dev_addr
, &gid
);
3216 enqueue_resolve_addr_work(work
, id_priv
);
3223 static int cma_resolve_ib_addr(struct rdma_id_private
*id_priv
)
3225 struct cma_work
*work
;
3228 work
= kzalloc(sizeof *work
, GFP_KERNEL
);
3232 if (!id_priv
->cma_dev
) {
3233 ret
= cma_resolve_ib_dev(id_priv
);
3238 rdma_addr_set_dgid(&id_priv
->id
.route
.addr
.dev_addr
, (union ib_gid
*)
3239 &(((struct sockaddr_ib
*) &id_priv
->id
.route
.addr
.dst_addr
)->sib_addr
));
3241 enqueue_resolve_addr_work(work
, id_priv
);
3248 static int cma_bind_addr(struct rdma_cm_id
*id
, struct sockaddr
*src_addr
,
3249 const struct sockaddr
*dst_addr
)
3251 if (!src_addr
|| !src_addr
->sa_family
) {
3252 src_addr
= (struct sockaddr
*) &id
->route
.addr
.src_addr
;
3253 src_addr
->sa_family
= dst_addr
->sa_family
;
3254 if (IS_ENABLED(CONFIG_IPV6
) &&
3255 dst_addr
->sa_family
== AF_INET6
) {
3256 struct sockaddr_in6
*src_addr6
= (struct sockaddr_in6
*) src_addr
;
3257 struct sockaddr_in6
*dst_addr6
= (struct sockaddr_in6
*) dst_addr
;
3258 src_addr6
->sin6_scope_id
= dst_addr6
->sin6_scope_id
;
3259 if (ipv6_addr_type(&dst_addr6
->sin6_addr
) & IPV6_ADDR_LINKLOCAL
)
3260 id
->route
.addr
.dev_addr
.bound_dev_if
= dst_addr6
->sin6_scope_id
;
3261 } else if (dst_addr
->sa_family
== AF_IB
) {
3262 ((struct sockaddr_ib
*) src_addr
)->sib_pkey
=
3263 ((struct sockaddr_ib
*) dst_addr
)->sib_pkey
;
3266 return rdma_bind_addr(id
, src_addr
);
3270 * If required, resolve the source address for bind and leave the id_priv in
3271 * state RDMA_CM_ADDR_BOUND. This oddly uses the state to determine the prior
3272 * calls made by ULP, a previously bound ID will not be re-bound and src_addr is
3275 static int resolve_prepare_src(struct rdma_id_private
*id_priv
,
3276 struct sockaddr
*src_addr
,
3277 const struct sockaddr
*dst_addr
)
3281 memcpy(cma_dst_addr(id_priv
), dst_addr
, rdma_addr_size(dst_addr
));
3282 if (!cma_comp_exch(id_priv
, RDMA_CM_ADDR_BOUND
, RDMA_CM_ADDR_QUERY
)) {
3283 /* For a well behaved ULP state will be RDMA_CM_IDLE */
3284 ret
= cma_bind_addr(&id_priv
->id
, src_addr
, dst_addr
);
3287 if (WARN_ON(!cma_comp_exch(id_priv
, RDMA_CM_ADDR_BOUND
,
3288 RDMA_CM_ADDR_QUERY
))) {
3294 if (cma_family(id_priv
) != dst_addr
->sa_family
) {
3301 cma_comp_exch(id_priv
, RDMA_CM_ADDR_QUERY
, RDMA_CM_ADDR_BOUND
);
3303 memset(cma_dst_addr(id_priv
), 0, rdma_addr_size(dst_addr
));
3307 int rdma_resolve_addr(struct rdma_cm_id
*id
, struct sockaddr
*src_addr
,
3308 const struct sockaddr
*dst_addr
, unsigned long timeout_ms
)
3310 struct rdma_id_private
*id_priv
=
3311 container_of(id
, struct rdma_id_private
, id
);
3314 ret
= resolve_prepare_src(id_priv
, src_addr
, dst_addr
);
3318 if (cma_any_addr(dst_addr
)) {
3319 ret
= cma_resolve_loopback(id_priv
);
3321 if (dst_addr
->sa_family
== AF_IB
) {
3322 ret
= cma_resolve_ib_addr(id_priv
);
3324 ret
= rdma_resolve_ip(cma_src_addr(id_priv
), dst_addr
,
3325 &id
->route
.addr
.dev_addr
,
3326 timeout_ms
, addr_handler
,
3335 cma_comp_exch(id_priv
, RDMA_CM_ADDR_QUERY
, RDMA_CM_ADDR_BOUND
);
3338 EXPORT_SYMBOL(rdma_resolve_addr
);
3340 int rdma_set_reuseaddr(struct rdma_cm_id
*id
, int reuse
)
3342 struct rdma_id_private
*id_priv
;
3343 unsigned long flags
;
3346 id_priv
= container_of(id
, struct rdma_id_private
, id
);
3347 spin_lock_irqsave(&id_priv
->lock
, flags
);
3348 if ((reuse
&& id_priv
->state
!= RDMA_CM_LISTEN
) ||
3349 id_priv
->state
== RDMA_CM_IDLE
) {
3350 id_priv
->reuseaddr
= reuse
;
3355 spin_unlock_irqrestore(&id_priv
->lock
, flags
);
3358 EXPORT_SYMBOL(rdma_set_reuseaddr
);
3360 int rdma_set_afonly(struct rdma_cm_id
*id
, int afonly
)
3362 struct rdma_id_private
*id_priv
;
3363 unsigned long flags
;
3366 id_priv
= container_of(id
, struct rdma_id_private
, id
);
3367 spin_lock_irqsave(&id_priv
->lock
, flags
);
3368 if (id_priv
->state
== RDMA_CM_IDLE
|| id_priv
->state
== RDMA_CM_ADDR_BOUND
) {
3369 id_priv
->options
|= (1 << CMA_OPTION_AFONLY
);
3370 id_priv
->afonly
= afonly
;
3375 spin_unlock_irqrestore(&id_priv
->lock
, flags
);
3378 EXPORT_SYMBOL(rdma_set_afonly
);
3380 static void cma_bind_port(struct rdma_bind_list
*bind_list
,
3381 struct rdma_id_private
*id_priv
)
3383 struct sockaddr
*addr
;
3384 struct sockaddr_ib
*sib
;
3388 lockdep_assert_held(&lock
);
3390 addr
= cma_src_addr(id_priv
);
3391 port
= htons(bind_list
->port
);
3393 switch (addr
->sa_family
) {
3395 ((struct sockaddr_in
*) addr
)->sin_port
= port
;
3398 ((struct sockaddr_in6
*) addr
)->sin6_port
= port
;
3401 sib
= (struct sockaddr_ib
*) addr
;
3402 sid
= be64_to_cpu(sib
->sib_sid
);
3403 mask
= be64_to_cpu(sib
->sib_sid_mask
);
3404 sib
->sib_sid
= cpu_to_be64((sid
& mask
) | (u64
) ntohs(port
));
3405 sib
->sib_sid_mask
= cpu_to_be64(~0ULL);
3408 id_priv
->bind_list
= bind_list
;
3409 hlist_add_head(&id_priv
->node
, &bind_list
->owners
);
3412 static int cma_alloc_port(enum rdma_ucm_port_space ps
,
3413 struct rdma_id_private
*id_priv
, unsigned short snum
)
3415 struct rdma_bind_list
*bind_list
;
3418 lockdep_assert_held(&lock
);
3420 bind_list
= kzalloc(sizeof *bind_list
, GFP_KERNEL
);
3424 ret
= cma_ps_alloc(id_priv
->id
.route
.addr
.dev_addr
.net
, ps
, bind_list
,
3430 bind_list
->port
= snum
;
3431 cma_bind_port(bind_list
, id_priv
);
3435 return ret
== -ENOSPC
? -EADDRNOTAVAIL
: ret
;
3438 static int cma_port_is_unique(struct rdma_bind_list
*bind_list
,
3439 struct rdma_id_private
*id_priv
)
3441 struct rdma_id_private
*cur_id
;
3442 struct sockaddr
*daddr
= cma_dst_addr(id_priv
);
3443 struct sockaddr
*saddr
= cma_src_addr(id_priv
);
3444 __be16 dport
= cma_port(daddr
);
3446 lockdep_assert_held(&lock
);
3448 hlist_for_each_entry(cur_id
, &bind_list
->owners
, node
) {
3449 struct sockaddr
*cur_daddr
= cma_dst_addr(cur_id
);
3450 struct sockaddr
*cur_saddr
= cma_src_addr(cur_id
);
3451 __be16 cur_dport
= cma_port(cur_daddr
);
3453 if (id_priv
== cur_id
)
3456 /* different dest port -> unique */
3457 if (!cma_any_port(daddr
) &&
3458 !cma_any_port(cur_daddr
) &&
3459 (dport
!= cur_dport
))
3462 /* different src address -> unique */
3463 if (!cma_any_addr(saddr
) &&
3464 !cma_any_addr(cur_saddr
) &&
3465 cma_addr_cmp(saddr
, cur_saddr
))
3468 /* different dst address -> unique */
3469 if (!cma_any_addr(daddr
) &&
3470 !cma_any_addr(cur_daddr
) &&
3471 cma_addr_cmp(daddr
, cur_daddr
))
3474 return -EADDRNOTAVAIL
;
3479 static int cma_alloc_any_port(enum rdma_ucm_port_space ps
,
3480 struct rdma_id_private
*id_priv
)
3482 static unsigned int last_used_port
;
3483 int low
, high
, remaining
;
3485 struct net
*net
= id_priv
->id
.route
.addr
.dev_addr
.net
;
3487 lockdep_assert_held(&lock
);
3489 inet_get_local_port_range(net
, &low
, &high
);
3490 remaining
= (high
- low
) + 1;
3491 rover
= prandom_u32() % remaining
+ low
;
3493 if (last_used_port
!= rover
) {
3494 struct rdma_bind_list
*bind_list
;
3497 bind_list
= cma_ps_find(net
, ps
, (unsigned short)rover
);
3500 ret
= cma_alloc_port(ps
, id_priv
, rover
);
3502 ret
= cma_port_is_unique(bind_list
, id_priv
);
3504 cma_bind_port(bind_list
, id_priv
);
3507 * Remember previously used port number in order to avoid
3508 * re-using same port immediately after it is closed.
3511 last_used_port
= rover
;
3512 if (ret
!= -EADDRNOTAVAIL
)
3517 if ((rover
< low
) || (rover
> high
))
3521 return -EADDRNOTAVAIL
;
3525 * Check that the requested port is available. This is called when trying to
3526 * bind to a specific port, or when trying to listen on a bound port. In
3527 * the latter case, the provided id_priv may already be on the bind_list, but
3528 * we still need to check that it's okay to start listening.
3530 static int cma_check_port(struct rdma_bind_list
*bind_list
,
3531 struct rdma_id_private
*id_priv
, uint8_t reuseaddr
)
3533 struct rdma_id_private
*cur_id
;
3534 struct sockaddr
*addr
, *cur_addr
;
3536 lockdep_assert_held(&lock
);
3538 addr
= cma_src_addr(id_priv
);
3539 hlist_for_each_entry(cur_id
, &bind_list
->owners
, node
) {
3540 if (id_priv
== cur_id
)
3543 if (reuseaddr
&& cur_id
->reuseaddr
)
3546 cur_addr
= cma_src_addr(cur_id
);
3547 if (id_priv
->afonly
&& cur_id
->afonly
&&
3548 (addr
->sa_family
!= cur_addr
->sa_family
))
3551 if (cma_any_addr(addr
) || cma_any_addr(cur_addr
))
3552 return -EADDRNOTAVAIL
;
3554 if (!cma_addr_cmp(addr
, cur_addr
))
3560 static int cma_use_port(enum rdma_ucm_port_space ps
,
3561 struct rdma_id_private
*id_priv
)
3563 struct rdma_bind_list
*bind_list
;
3564 unsigned short snum
;
3567 lockdep_assert_held(&lock
);
3569 snum
= ntohs(cma_port(cma_src_addr(id_priv
)));
3570 if (snum
< PROT_SOCK
&& !capable(CAP_NET_BIND_SERVICE
))
3573 bind_list
= cma_ps_find(id_priv
->id
.route
.addr
.dev_addr
.net
, ps
, snum
);
3575 ret
= cma_alloc_port(ps
, id_priv
, snum
);
3577 ret
= cma_check_port(bind_list
, id_priv
, id_priv
->reuseaddr
);
3579 cma_bind_port(bind_list
, id_priv
);
3584 static enum rdma_ucm_port_space
3585 cma_select_inet_ps(struct rdma_id_private
*id_priv
)
3587 switch (id_priv
->id
.ps
) {
3592 return id_priv
->id
.ps
;
3599 static enum rdma_ucm_port_space
3600 cma_select_ib_ps(struct rdma_id_private
*id_priv
)
3602 enum rdma_ucm_port_space ps
= 0;
3603 struct sockaddr_ib
*sib
;
3604 u64 sid_ps
, mask
, sid
;
3606 sib
= (struct sockaddr_ib
*) cma_src_addr(id_priv
);
3607 mask
= be64_to_cpu(sib
->sib_sid_mask
) & RDMA_IB_IP_PS_MASK
;
3608 sid
= be64_to_cpu(sib
->sib_sid
) & mask
;
3610 if ((id_priv
->id
.ps
== RDMA_PS_IB
) && (sid
== (RDMA_IB_IP_PS_IB
& mask
))) {
3611 sid_ps
= RDMA_IB_IP_PS_IB
;
3613 } else if (((id_priv
->id
.ps
== RDMA_PS_IB
) || (id_priv
->id
.ps
== RDMA_PS_TCP
)) &&
3614 (sid
== (RDMA_IB_IP_PS_TCP
& mask
))) {
3615 sid_ps
= RDMA_IB_IP_PS_TCP
;
3617 } else if (((id_priv
->id
.ps
== RDMA_PS_IB
) || (id_priv
->id
.ps
== RDMA_PS_UDP
)) &&
3618 (sid
== (RDMA_IB_IP_PS_UDP
& mask
))) {
3619 sid_ps
= RDMA_IB_IP_PS_UDP
;
3624 sib
->sib_sid
= cpu_to_be64(sid_ps
| ntohs(cma_port((struct sockaddr
*) sib
)));
3625 sib
->sib_sid_mask
= cpu_to_be64(RDMA_IB_IP_PS_MASK
|
3626 be64_to_cpu(sib
->sib_sid_mask
));
3631 static int cma_get_port(struct rdma_id_private
*id_priv
)
3633 enum rdma_ucm_port_space ps
;
3636 if (cma_family(id_priv
) != AF_IB
)
3637 ps
= cma_select_inet_ps(id_priv
);
3639 ps
= cma_select_ib_ps(id_priv
);
3641 return -EPROTONOSUPPORT
;
3644 if (cma_any_port(cma_src_addr(id_priv
)))
3645 ret
= cma_alloc_any_port(ps
, id_priv
);
3647 ret
= cma_use_port(ps
, id_priv
);
3648 mutex_unlock(&lock
);
3653 static int cma_check_linklocal(struct rdma_dev_addr
*dev_addr
,
3654 struct sockaddr
*addr
)
3656 #if IS_ENABLED(CONFIG_IPV6)
3657 struct sockaddr_in6
*sin6
;
3659 if (addr
->sa_family
!= AF_INET6
)
3662 sin6
= (struct sockaddr_in6
*) addr
;
3664 if (!(ipv6_addr_type(&sin6
->sin6_addr
) & IPV6_ADDR_LINKLOCAL
))
3667 if (!sin6
->sin6_scope_id
)
3670 dev_addr
->bound_dev_if
= sin6
->sin6_scope_id
;
3675 int rdma_listen(struct rdma_cm_id
*id
, int backlog
)
3677 struct rdma_id_private
*id_priv
=
3678 container_of(id
, struct rdma_id_private
, id
);
3681 if (!cma_comp_exch(id_priv
, RDMA_CM_ADDR_BOUND
, RDMA_CM_LISTEN
)) {
3682 /* For a well behaved ULP state will be RDMA_CM_IDLE */
3683 id
->route
.addr
.src_addr
.ss_family
= AF_INET
;
3684 ret
= rdma_bind_addr(id
, cma_src_addr(id_priv
));
3687 if (WARN_ON(!cma_comp_exch(id_priv
, RDMA_CM_ADDR_BOUND
,
3693 * Once the ID reaches RDMA_CM_LISTEN it is not allowed to be reusable
3694 * any more, and has to be unique in the bind list.
3696 if (id_priv
->reuseaddr
) {
3698 ret
= cma_check_port(id_priv
->bind_list
, id_priv
, 0);
3700 id_priv
->reuseaddr
= 0;
3701 mutex_unlock(&lock
);
3706 id_priv
->backlog
= backlog
;
3708 if (rdma_cap_ib_cm(id
->device
, 1)) {
3709 ret
= cma_ib_listen(id_priv
);
3712 } else if (rdma_cap_iw_cm(id
->device
, 1)) {
3713 ret
= cma_iw_listen(id_priv
, backlog
);
3721 ret
= cma_listen_on_all(id_priv
);
3728 id_priv
->backlog
= 0;
3730 * All the failure paths that lead here will not allow the req_handler's
3733 cma_comp_exch(id_priv
, RDMA_CM_LISTEN
, RDMA_CM_ADDR_BOUND
);
3736 EXPORT_SYMBOL(rdma_listen
);
3738 int rdma_bind_addr(struct rdma_cm_id
*id
, struct sockaddr
*addr
)
3740 struct rdma_id_private
*id_priv
;
3742 struct sockaddr
*daddr
;
3744 if (addr
->sa_family
!= AF_INET
&& addr
->sa_family
!= AF_INET6
&&
3745 addr
->sa_family
!= AF_IB
)
3746 return -EAFNOSUPPORT
;
3748 id_priv
= container_of(id
, struct rdma_id_private
, id
);
3749 if (!cma_comp_exch(id_priv
, RDMA_CM_IDLE
, RDMA_CM_ADDR_BOUND
))
3752 ret
= cma_check_linklocal(&id
->route
.addr
.dev_addr
, addr
);
3756 memcpy(cma_src_addr(id_priv
), addr
, rdma_addr_size(addr
));
3757 if (!cma_any_addr(addr
)) {
3758 ret
= cma_translate_addr(addr
, &id
->route
.addr
.dev_addr
);
3762 ret
= cma_acquire_dev_by_src_ip(id_priv
);
3767 if (!(id_priv
->options
& (1 << CMA_OPTION_AFONLY
))) {
3768 if (addr
->sa_family
== AF_INET
)
3769 id_priv
->afonly
= 1;
3770 #if IS_ENABLED(CONFIG_IPV6)
3771 else if (addr
->sa_family
== AF_INET6
) {
3772 struct net
*net
= id_priv
->id
.route
.addr
.dev_addr
.net
;
3774 id_priv
->afonly
= net
->ipv6
.sysctl
.bindv6only
;
3778 daddr
= cma_dst_addr(id_priv
);
3779 daddr
->sa_family
= addr
->sa_family
;
3781 ret
= cma_get_port(id_priv
);
3787 if (id_priv
->cma_dev
)
3788 cma_release_dev(id_priv
);
3790 cma_comp_exch(id_priv
, RDMA_CM_ADDR_BOUND
, RDMA_CM_IDLE
);
3793 EXPORT_SYMBOL(rdma_bind_addr
);
3795 static int cma_format_hdr(void *hdr
, struct rdma_id_private
*id_priv
)
3797 struct cma_hdr
*cma_hdr
;
3800 cma_hdr
->cma_version
= CMA_VERSION
;
3801 if (cma_family(id_priv
) == AF_INET
) {
3802 struct sockaddr_in
*src4
, *dst4
;
3804 src4
= (struct sockaddr_in
*) cma_src_addr(id_priv
);
3805 dst4
= (struct sockaddr_in
*) cma_dst_addr(id_priv
);
3807 cma_set_ip_ver(cma_hdr
, 4);
3808 cma_hdr
->src_addr
.ip4
.addr
= src4
->sin_addr
.s_addr
;
3809 cma_hdr
->dst_addr
.ip4
.addr
= dst4
->sin_addr
.s_addr
;
3810 cma_hdr
->port
= src4
->sin_port
;
3811 } else if (cma_family(id_priv
) == AF_INET6
) {
3812 struct sockaddr_in6
*src6
, *dst6
;
3814 src6
= (struct sockaddr_in6
*) cma_src_addr(id_priv
);
3815 dst6
= (struct sockaddr_in6
*) cma_dst_addr(id_priv
);
3817 cma_set_ip_ver(cma_hdr
, 6);
3818 cma_hdr
->src_addr
.ip6
= src6
->sin6_addr
;
3819 cma_hdr
->dst_addr
.ip6
= dst6
->sin6_addr
;
3820 cma_hdr
->port
= src6
->sin6_port
;
3825 static int cma_sidr_rep_handler(struct ib_cm_id
*cm_id
,
3826 const struct ib_cm_event
*ib_event
)
3828 struct rdma_id_private
*id_priv
= cm_id
->context
;
3829 struct rdma_cm_event event
= {};
3830 const struct ib_cm_sidr_rep_event_param
*rep
=
3831 &ib_event
->param
.sidr_rep_rcvd
;
3834 mutex_lock(&id_priv
->handler_mutex
);
3835 if (READ_ONCE(id_priv
->state
) != RDMA_CM_CONNECT
)
3838 switch (ib_event
->event
) {
3839 case IB_CM_SIDR_REQ_ERROR
:
3840 event
.event
= RDMA_CM_EVENT_UNREACHABLE
;
3841 event
.status
= -ETIMEDOUT
;
3843 case IB_CM_SIDR_REP_RECEIVED
:
3844 event
.param
.ud
.private_data
= ib_event
->private_data
;
3845 event
.param
.ud
.private_data_len
= IB_CM_SIDR_REP_PRIVATE_DATA_SIZE
;
3846 if (rep
->status
!= IB_SIDR_SUCCESS
) {
3847 event
.event
= RDMA_CM_EVENT_UNREACHABLE
;
3848 event
.status
= ib_event
->param
.sidr_rep_rcvd
.status
;
3849 pr_debug_ratelimited("RDMA CM: UNREACHABLE: bad SIDR reply. status %d\n",
3853 ret
= cma_set_qkey(id_priv
, rep
->qkey
);
3855 pr_debug_ratelimited("RDMA CM: ADDR_ERROR: failed to set qkey. status %d\n", ret
);
3856 event
.event
= RDMA_CM_EVENT_ADDR_ERROR
;
3860 ib_init_ah_attr_from_path(id_priv
->id
.device
,
3861 id_priv
->id
.port_num
,
3862 id_priv
->id
.route
.path_rec
,
3863 &event
.param
.ud
.ah_attr
,
3865 event
.param
.ud
.qp_num
= rep
->qpn
;
3866 event
.param
.ud
.qkey
= rep
->qkey
;
3867 event
.event
= RDMA_CM_EVENT_ESTABLISHED
;
3871 pr_err("RDMA CMA: unexpected IB CM event: %d\n",
3876 ret
= cma_cm_event_handler(id_priv
, &event
);
3878 rdma_destroy_ah_attr(&event
.param
.ud
.ah_attr
);
3880 /* Destroy the CM ID by returning a non-zero value. */
3881 id_priv
->cm_id
.ib
= NULL
;
3882 destroy_id_handler_unlock(id_priv
);
3886 mutex_unlock(&id_priv
->handler_mutex
);
3890 static int cma_resolve_ib_udp(struct rdma_id_private
*id_priv
,
3891 struct rdma_conn_param
*conn_param
)
3893 struct ib_cm_sidr_req_param req
;
3894 struct ib_cm_id
*id
;
3899 memset(&req
, 0, sizeof req
);
3900 offset
= cma_user_data_offset(id_priv
);
3901 req
.private_data_len
= offset
+ conn_param
->private_data_len
;
3902 if (req
.private_data_len
< conn_param
->private_data_len
)
3905 if (req
.private_data_len
) {
3906 private_data
= kzalloc(req
.private_data_len
, GFP_ATOMIC
);
3910 private_data
= NULL
;
3913 if (conn_param
->private_data
&& conn_param
->private_data_len
)
3914 memcpy(private_data
+ offset
, conn_param
->private_data
,
3915 conn_param
->private_data_len
);
3918 ret
= cma_format_hdr(private_data
, id_priv
);
3921 req
.private_data
= private_data
;
3924 id
= ib_create_cm_id(id_priv
->id
.device
, cma_sidr_rep_handler
,
3930 id_priv
->cm_id
.ib
= id
;
3932 req
.path
= id_priv
->id
.route
.path_rec
;
3933 req
.sgid_attr
= id_priv
->id
.route
.addr
.dev_addr
.sgid_attr
;
3934 req
.service_id
= rdma_get_service_id(&id_priv
->id
, cma_dst_addr(id_priv
));
3935 req
.timeout_ms
= 1 << (CMA_CM_RESPONSE_TIMEOUT
- 8);
3936 req
.max_cm_retries
= CMA_MAX_CM_RETRIES
;
3938 trace_cm_send_sidr_req(id_priv
);
3939 ret
= ib_send_cm_sidr_req(id_priv
->cm_id
.ib
, &req
);
3941 ib_destroy_cm_id(id_priv
->cm_id
.ib
);
3942 id_priv
->cm_id
.ib
= NULL
;
3945 kfree(private_data
);
3949 static int cma_connect_ib(struct rdma_id_private
*id_priv
,
3950 struct rdma_conn_param
*conn_param
)
3952 struct ib_cm_req_param req
;
3953 struct rdma_route
*route
;
3955 struct ib_cm_id
*id
;
3959 memset(&req
, 0, sizeof req
);
3960 offset
= cma_user_data_offset(id_priv
);
3961 req
.private_data_len
= offset
+ conn_param
->private_data_len
;
3962 if (req
.private_data_len
< conn_param
->private_data_len
)
3965 if (req
.private_data_len
) {
3966 private_data
= kzalloc(req
.private_data_len
, GFP_ATOMIC
);
3970 private_data
= NULL
;
3973 if (conn_param
->private_data
&& conn_param
->private_data_len
)
3974 memcpy(private_data
+ offset
, conn_param
->private_data
,
3975 conn_param
->private_data_len
);
3977 id
= ib_create_cm_id(id_priv
->id
.device
, cma_ib_handler
, id_priv
);
3982 id_priv
->cm_id
.ib
= id
;
3984 route
= &id_priv
->id
.route
;
3986 ret
= cma_format_hdr(private_data
, id_priv
);
3989 req
.private_data
= private_data
;
3992 req
.primary_path
= &route
->path_rec
[0];
3993 if (route
->num_paths
== 2)
3994 req
.alternate_path
= &route
->path_rec
[1];
3996 req
.ppath_sgid_attr
= id_priv
->id
.route
.addr
.dev_addr
.sgid_attr
;
3997 /* Alternate path SGID attribute currently unsupported */
3998 req
.service_id
= rdma_get_service_id(&id_priv
->id
, cma_dst_addr(id_priv
));
3999 req
.qp_num
= id_priv
->qp_num
;
4000 req
.qp_type
= id_priv
->id
.qp_type
;
4001 req
.starting_psn
= id_priv
->seq_num
;
4002 req
.responder_resources
= conn_param
->responder_resources
;
4003 req
.initiator_depth
= conn_param
->initiator_depth
;
4004 req
.flow_control
= conn_param
->flow_control
;
4005 req
.retry_count
= min_t(u8
, 7, conn_param
->retry_count
);
4006 req
.rnr_retry_count
= min_t(u8
, 7, conn_param
->rnr_retry_count
);
4007 req
.remote_cm_response_timeout
= CMA_CM_RESPONSE_TIMEOUT
;
4008 req
.local_cm_response_timeout
= CMA_CM_RESPONSE_TIMEOUT
;
4009 req
.max_cm_retries
= CMA_MAX_CM_RETRIES
;
4010 req
.srq
= id_priv
->srq
? 1 : 0;
4011 req
.ece
.vendor_id
= id_priv
->ece
.vendor_id
;
4012 req
.ece
.attr_mod
= id_priv
->ece
.attr_mod
;
4014 trace_cm_send_req(id_priv
);
4015 ret
= ib_send_cm_req(id_priv
->cm_id
.ib
, &req
);
4017 if (ret
&& !IS_ERR(id
)) {
4018 ib_destroy_cm_id(id
);
4019 id_priv
->cm_id
.ib
= NULL
;
4022 kfree(private_data
);
4026 static int cma_connect_iw(struct rdma_id_private
*id_priv
,
4027 struct rdma_conn_param
*conn_param
)
4029 struct iw_cm_id
*cm_id
;
4031 struct iw_cm_conn_param iw_param
;
4033 cm_id
= iw_create_cm_id(id_priv
->id
.device
, cma_iw_handler
, id_priv
);
4035 return PTR_ERR(cm_id
);
4037 cm_id
->tos
= id_priv
->tos
;
4038 cm_id
->tos_set
= id_priv
->tos_set
;
4039 id_priv
->cm_id
.iw
= cm_id
;
4041 memcpy(&cm_id
->local_addr
, cma_src_addr(id_priv
),
4042 rdma_addr_size(cma_src_addr(id_priv
)));
4043 memcpy(&cm_id
->remote_addr
, cma_dst_addr(id_priv
),
4044 rdma_addr_size(cma_dst_addr(id_priv
)));
4046 ret
= cma_modify_qp_rtr(id_priv
, conn_param
);
4051 iw_param
.ord
= conn_param
->initiator_depth
;
4052 iw_param
.ird
= conn_param
->responder_resources
;
4053 iw_param
.private_data
= conn_param
->private_data
;
4054 iw_param
.private_data_len
= conn_param
->private_data_len
;
4055 iw_param
.qpn
= id_priv
->id
.qp
? id_priv
->qp_num
: conn_param
->qp_num
;
4057 memset(&iw_param
, 0, sizeof iw_param
);
4058 iw_param
.qpn
= id_priv
->qp_num
;
4060 ret
= iw_cm_connect(cm_id
, &iw_param
);
4063 iw_destroy_cm_id(cm_id
);
4064 id_priv
->cm_id
.iw
= NULL
;
4070 * rdma_connect_locked - Initiate an active connection request.
4071 * @id: Connection identifier to connect.
4072 * @conn_param: Connection information used for connected QPs.
4074 * Same as rdma_connect() but can only be called from the
4075 * RDMA_CM_EVENT_ROUTE_RESOLVED handler callback.
4077 int rdma_connect_locked(struct rdma_cm_id
*id
,
4078 struct rdma_conn_param
*conn_param
)
4080 struct rdma_id_private
*id_priv
=
4081 container_of(id
, struct rdma_id_private
, id
);
4084 if (!cma_comp_exch(id_priv
, RDMA_CM_ROUTE_RESOLVED
, RDMA_CM_CONNECT
))
4088 id_priv
->qp_num
= conn_param
->qp_num
;
4089 id_priv
->srq
= conn_param
->srq
;
4092 if (rdma_cap_ib_cm(id
->device
, id
->port_num
)) {
4093 if (id
->qp_type
== IB_QPT_UD
)
4094 ret
= cma_resolve_ib_udp(id_priv
, conn_param
);
4096 ret
= cma_connect_ib(id_priv
, conn_param
);
4097 } else if (rdma_cap_iw_cm(id
->device
, id
->port_num
))
4098 ret
= cma_connect_iw(id_priv
, conn_param
);
4105 cma_comp_exch(id_priv
, RDMA_CM_CONNECT
, RDMA_CM_ROUTE_RESOLVED
);
4108 EXPORT_SYMBOL(rdma_connect_locked
);
4111 * rdma_connect - Initiate an active connection request.
4112 * @id: Connection identifier to connect.
4113 * @conn_param: Connection information used for connected QPs.
4115 * Users must have resolved a route for the rdma_cm_id to connect with by having
4116 * called rdma_resolve_route before calling this routine.
4118 * This call will either connect to a remote QP or obtain remote QP information
4119 * for unconnected rdma_cm_id's. The actual operation is based on the
4120 * rdma_cm_id's port space.
4122 int rdma_connect(struct rdma_cm_id
*id
, struct rdma_conn_param
*conn_param
)
4124 struct rdma_id_private
*id_priv
=
4125 container_of(id
, struct rdma_id_private
, id
);
4128 mutex_lock(&id_priv
->handler_mutex
);
4129 ret
= rdma_connect_locked(id
, conn_param
);
4130 mutex_unlock(&id_priv
->handler_mutex
);
4133 EXPORT_SYMBOL(rdma_connect
);
4136 * rdma_connect_ece - Initiate an active connection request with ECE data.
4137 * @id: Connection identifier to connect.
4138 * @conn_param: Connection information used for connected QPs.
4139 * @ece: ECE parameters
4141 * See rdma_connect() explanation.
4143 int rdma_connect_ece(struct rdma_cm_id
*id
, struct rdma_conn_param
*conn_param
,
4144 struct rdma_ucm_ece
*ece
)
4146 struct rdma_id_private
*id_priv
=
4147 container_of(id
, struct rdma_id_private
, id
);
4149 id_priv
->ece
.vendor_id
= ece
->vendor_id
;
4150 id_priv
->ece
.attr_mod
= ece
->attr_mod
;
4152 return rdma_connect(id
, conn_param
);
4154 EXPORT_SYMBOL(rdma_connect_ece
);
4156 static int cma_accept_ib(struct rdma_id_private
*id_priv
,
4157 struct rdma_conn_param
*conn_param
)
4159 struct ib_cm_rep_param rep
;
4162 ret
= cma_modify_qp_rtr(id_priv
, conn_param
);
4166 ret
= cma_modify_qp_rts(id_priv
, conn_param
);
4170 memset(&rep
, 0, sizeof rep
);
4171 rep
.qp_num
= id_priv
->qp_num
;
4172 rep
.starting_psn
= id_priv
->seq_num
;
4173 rep
.private_data
= conn_param
->private_data
;
4174 rep
.private_data_len
= conn_param
->private_data_len
;
4175 rep
.responder_resources
= conn_param
->responder_resources
;
4176 rep
.initiator_depth
= conn_param
->initiator_depth
;
4177 rep
.failover_accepted
= 0;
4178 rep
.flow_control
= conn_param
->flow_control
;
4179 rep
.rnr_retry_count
= min_t(u8
, 7, conn_param
->rnr_retry_count
);
4180 rep
.srq
= id_priv
->srq
? 1 : 0;
4181 rep
.ece
.vendor_id
= id_priv
->ece
.vendor_id
;
4182 rep
.ece
.attr_mod
= id_priv
->ece
.attr_mod
;
4184 trace_cm_send_rep(id_priv
);
4185 ret
= ib_send_cm_rep(id_priv
->cm_id
.ib
, &rep
);
4190 static int cma_accept_iw(struct rdma_id_private
*id_priv
,
4191 struct rdma_conn_param
*conn_param
)
4193 struct iw_cm_conn_param iw_param
;
4199 ret
= cma_modify_qp_rtr(id_priv
, conn_param
);
4203 iw_param
.ord
= conn_param
->initiator_depth
;
4204 iw_param
.ird
= conn_param
->responder_resources
;
4205 iw_param
.private_data
= conn_param
->private_data
;
4206 iw_param
.private_data_len
= conn_param
->private_data_len
;
4207 if (id_priv
->id
.qp
) {
4208 iw_param
.qpn
= id_priv
->qp_num
;
4210 iw_param
.qpn
= conn_param
->qp_num
;
4212 return iw_cm_accept(id_priv
->cm_id
.iw
, &iw_param
);
4215 static int cma_send_sidr_rep(struct rdma_id_private
*id_priv
,
4216 enum ib_cm_sidr_status status
, u32 qkey
,
4217 const void *private_data
, int private_data_len
)
4219 struct ib_cm_sidr_rep_param rep
;
4222 memset(&rep
, 0, sizeof rep
);
4223 rep
.status
= status
;
4224 if (status
== IB_SIDR_SUCCESS
) {
4225 ret
= cma_set_qkey(id_priv
, qkey
);
4228 rep
.qp_num
= id_priv
->qp_num
;
4229 rep
.qkey
= id_priv
->qkey
;
4231 rep
.ece
.vendor_id
= id_priv
->ece
.vendor_id
;
4232 rep
.ece
.attr_mod
= id_priv
->ece
.attr_mod
;
4235 rep
.private_data
= private_data
;
4236 rep
.private_data_len
= private_data_len
;
4238 trace_cm_send_sidr_rep(id_priv
);
4239 return ib_send_cm_sidr_rep(id_priv
->cm_id
.ib
, &rep
);
4243 * rdma_accept - Called to accept a connection request or response.
4244 * @id: Connection identifier associated with the request.
4245 * @conn_param: Information needed to establish the connection. This must be
4246 * provided if accepting a connection request. If accepting a connection
4247 * response, this parameter must be NULL.
4249 * Typically, this routine is only called by the listener to accept a connection
4250 * request. It must also be called on the active side of a connection if the
4251 * user is performing their own QP transitions.
4253 * In the case of error, a reject message is sent to the remote side and the
4254 * state of the qp associated with the id is modified to error, such that any
4255 * previously posted receive buffers would be flushed.
4257 * This function is for use by kernel ULPs and must be called from under the
4260 int rdma_accept(struct rdma_cm_id
*id
, struct rdma_conn_param
*conn_param
)
4262 struct rdma_id_private
*id_priv
=
4263 container_of(id
, struct rdma_id_private
, id
);
4266 lockdep_assert_held(&id_priv
->handler_mutex
);
4268 if (READ_ONCE(id_priv
->state
) != RDMA_CM_CONNECT
)
4271 if (!id
->qp
&& conn_param
) {
4272 id_priv
->qp_num
= conn_param
->qp_num
;
4273 id_priv
->srq
= conn_param
->srq
;
4276 if (rdma_cap_ib_cm(id
->device
, id
->port_num
)) {
4277 if (id
->qp_type
== IB_QPT_UD
) {
4279 ret
= cma_send_sidr_rep(id_priv
, IB_SIDR_SUCCESS
,
4281 conn_param
->private_data
,
4282 conn_param
->private_data_len
);
4284 ret
= cma_send_sidr_rep(id_priv
, IB_SIDR_SUCCESS
,
4288 ret
= cma_accept_ib(id_priv
, conn_param
);
4290 ret
= cma_rep_recv(id_priv
);
4292 } else if (rdma_cap_iw_cm(id
->device
, id
->port_num
))
4293 ret
= cma_accept_iw(id_priv
, conn_param
);
4302 cma_modify_qp_err(id_priv
);
4303 rdma_reject(id
, NULL
, 0, IB_CM_REJ_CONSUMER_DEFINED
);
4306 EXPORT_SYMBOL(rdma_accept
);
4308 int rdma_accept_ece(struct rdma_cm_id
*id
, struct rdma_conn_param
*conn_param
,
4309 struct rdma_ucm_ece
*ece
)
4311 struct rdma_id_private
*id_priv
=
4312 container_of(id
, struct rdma_id_private
, id
);
4314 id_priv
->ece
.vendor_id
= ece
->vendor_id
;
4315 id_priv
->ece
.attr_mod
= ece
->attr_mod
;
4317 return rdma_accept(id
, conn_param
);
4319 EXPORT_SYMBOL(rdma_accept_ece
);
4321 void rdma_lock_handler(struct rdma_cm_id
*id
)
4323 struct rdma_id_private
*id_priv
=
4324 container_of(id
, struct rdma_id_private
, id
);
4326 mutex_lock(&id_priv
->handler_mutex
);
4328 EXPORT_SYMBOL(rdma_lock_handler
);
4330 void rdma_unlock_handler(struct rdma_cm_id
*id
)
4332 struct rdma_id_private
*id_priv
=
4333 container_of(id
, struct rdma_id_private
, id
);
4335 mutex_unlock(&id_priv
->handler_mutex
);
4337 EXPORT_SYMBOL(rdma_unlock_handler
);
4339 int rdma_notify(struct rdma_cm_id
*id
, enum ib_event_type event
)
4341 struct rdma_id_private
*id_priv
;
4344 id_priv
= container_of(id
, struct rdma_id_private
, id
);
4345 if (!id_priv
->cm_id
.ib
)
4348 switch (id
->device
->node_type
) {
4349 case RDMA_NODE_IB_CA
:
4350 ret
= ib_cm_notify(id_priv
->cm_id
.ib
, event
);
4358 EXPORT_SYMBOL(rdma_notify
);
4360 int rdma_reject(struct rdma_cm_id
*id
, const void *private_data
,
4361 u8 private_data_len
, u8 reason
)
4363 struct rdma_id_private
*id_priv
;
4366 id_priv
= container_of(id
, struct rdma_id_private
, id
);
4367 if (!id_priv
->cm_id
.ib
)
4370 if (rdma_cap_ib_cm(id
->device
, id
->port_num
)) {
4371 if (id
->qp_type
== IB_QPT_UD
) {
4372 ret
= cma_send_sidr_rep(id_priv
, IB_SIDR_REJECT
, 0,
4373 private_data
, private_data_len
);
4375 trace_cm_send_rej(id_priv
);
4376 ret
= ib_send_cm_rej(id_priv
->cm_id
.ib
, reason
, NULL
, 0,
4377 private_data
, private_data_len
);
4379 } else if (rdma_cap_iw_cm(id
->device
, id
->port_num
)) {
4380 ret
= iw_cm_reject(id_priv
->cm_id
.iw
,
4381 private_data
, private_data_len
);
4387 EXPORT_SYMBOL(rdma_reject
);
4389 int rdma_disconnect(struct rdma_cm_id
*id
)
4391 struct rdma_id_private
*id_priv
;
4394 id_priv
= container_of(id
, struct rdma_id_private
, id
);
4395 if (!id_priv
->cm_id
.ib
)
4398 if (rdma_cap_ib_cm(id
->device
, id
->port_num
)) {
4399 ret
= cma_modify_qp_err(id_priv
);
4402 /* Initiate or respond to a disconnect. */
4403 trace_cm_disconnect(id_priv
);
4404 if (ib_send_cm_dreq(id_priv
->cm_id
.ib
, NULL
, 0)) {
4405 if (!ib_send_cm_drep(id_priv
->cm_id
.ib
, NULL
, 0))
4406 trace_cm_sent_drep(id_priv
);
4408 trace_cm_sent_dreq(id_priv
);
4410 } else if (rdma_cap_iw_cm(id
->device
, id
->port_num
)) {
4411 ret
= iw_cm_disconnect(id_priv
->cm_id
.iw
, 0);
4418 EXPORT_SYMBOL(rdma_disconnect
);
4420 static void cma_make_mc_event(int status
, struct rdma_id_private
*id_priv
,
4421 struct ib_sa_multicast
*multicast
,
4422 struct rdma_cm_event
*event
,
4423 struct cma_multicast
*mc
)
4425 struct rdma_dev_addr
*dev_addr
;
4426 enum ib_gid_type gid_type
;
4427 struct net_device
*ndev
;
4430 status
= cma_set_qkey(id_priv
, be32_to_cpu(multicast
->rec
.qkey
));
4432 pr_debug_ratelimited("RDMA CM: MULTICAST_ERROR: failed to join multicast. status %d\n",
4435 event
->status
= status
;
4436 event
->param
.ud
.private_data
= mc
->context
;
4438 event
->event
= RDMA_CM_EVENT_MULTICAST_ERROR
;
4442 dev_addr
= &id_priv
->id
.route
.addr
.dev_addr
;
4443 ndev
= dev_get_by_index(dev_addr
->net
, dev_addr
->bound_dev_if
);
4446 ->default_gid_type
[id_priv
->id
.port_num
-
4448 id_priv
->cma_dev
->device
)];
4450 event
->event
= RDMA_CM_EVENT_MULTICAST_JOIN
;
4451 if (ib_init_ah_from_mcmember(id_priv
->id
.device
, id_priv
->id
.port_num
,
4452 &multicast
->rec
, ndev
, gid_type
,
4453 &event
->param
.ud
.ah_attr
)) {
4454 event
->event
= RDMA_CM_EVENT_MULTICAST_ERROR
;
4458 event
->param
.ud
.qp_num
= 0xFFFFFF;
4459 event
->param
.ud
.qkey
= be32_to_cpu(multicast
->rec
.qkey
);
4466 static int cma_ib_mc_handler(int status
, struct ib_sa_multicast
*multicast
)
4468 struct cma_multicast
*mc
= multicast
->context
;
4469 struct rdma_id_private
*id_priv
= mc
->id_priv
;
4470 struct rdma_cm_event event
= {};
4473 mutex_lock(&id_priv
->handler_mutex
);
4474 if (READ_ONCE(id_priv
->state
) == RDMA_CM_DEVICE_REMOVAL
||
4475 READ_ONCE(id_priv
->state
) == RDMA_CM_DESTROYING
)
4478 cma_make_mc_event(status
, id_priv
, multicast
, &event
, mc
);
4479 ret
= cma_cm_event_handler(id_priv
, &event
);
4480 rdma_destroy_ah_attr(&event
.param
.ud
.ah_attr
);
4482 destroy_id_handler_unlock(id_priv
);
4487 mutex_unlock(&id_priv
->handler_mutex
);
4491 static void cma_set_mgid(struct rdma_id_private
*id_priv
,
4492 struct sockaddr
*addr
, union ib_gid
*mgid
)
4494 unsigned char mc_map
[MAX_ADDR_LEN
];
4495 struct rdma_dev_addr
*dev_addr
= &id_priv
->id
.route
.addr
.dev_addr
;
4496 struct sockaddr_in
*sin
= (struct sockaddr_in
*) addr
;
4497 struct sockaddr_in6
*sin6
= (struct sockaddr_in6
*) addr
;
4499 if (cma_any_addr(addr
)) {
4500 memset(mgid
, 0, sizeof *mgid
);
4501 } else if ((addr
->sa_family
== AF_INET6
) &&
4502 ((be32_to_cpu(sin6
->sin6_addr
.s6_addr32
[0]) & 0xFFF0FFFF) ==
4504 /* IPv6 address is an SA assigned MGID. */
4505 memcpy(mgid
, &sin6
->sin6_addr
, sizeof *mgid
);
4506 } else if (addr
->sa_family
== AF_IB
) {
4507 memcpy(mgid
, &((struct sockaddr_ib
*) addr
)->sib_addr
, sizeof *mgid
);
4508 } else if (addr
->sa_family
== AF_INET6
) {
4509 ipv6_ib_mc_map(&sin6
->sin6_addr
, dev_addr
->broadcast
, mc_map
);
4510 if (id_priv
->id
.ps
== RDMA_PS_UDP
)
4511 mc_map
[7] = 0x01; /* Use RDMA CM signature */
4512 *mgid
= *(union ib_gid
*) (mc_map
+ 4);
4514 ip_ib_mc_map(sin
->sin_addr
.s_addr
, dev_addr
->broadcast
, mc_map
);
4515 if (id_priv
->id
.ps
== RDMA_PS_UDP
)
4516 mc_map
[7] = 0x01; /* Use RDMA CM signature */
4517 *mgid
= *(union ib_gid
*) (mc_map
+ 4);
4521 static int cma_join_ib_multicast(struct rdma_id_private
*id_priv
,
4522 struct cma_multicast
*mc
)
4524 struct ib_sa_mcmember_rec rec
;
4525 struct rdma_dev_addr
*dev_addr
= &id_priv
->id
.route
.addr
.dev_addr
;
4526 ib_sa_comp_mask comp_mask
;
4529 ib_addr_get_mgid(dev_addr
, &rec
.mgid
);
4530 ret
= ib_sa_get_mcmember_rec(id_priv
->id
.device
, id_priv
->id
.port_num
,
4535 ret
= cma_set_qkey(id_priv
, 0);
4539 cma_set_mgid(id_priv
, (struct sockaddr
*) &mc
->addr
, &rec
.mgid
);
4540 rec
.qkey
= cpu_to_be32(id_priv
->qkey
);
4541 rdma_addr_get_sgid(dev_addr
, &rec
.port_gid
);
4542 rec
.pkey
= cpu_to_be16(ib_addr_get_pkey(dev_addr
));
4543 rec
.join_state
= mc
->join_state
;
4545 if ((rec
.join_state
== BIT(SENDONLY_FULLMEMBER_JOIN
)) &&
4546 (!ib_sa_sendonly_fullmem_support(&sa_client
,
4548 id_priv
->id
.port_num
))) {
4550 &id_priv
->id
.device
->dev
,
4551 "RDMA CM: port %u Unable to multicast join: SM doesn't support Send Only Full Member option\n",
4552 id_priv
->id
.port_num
);
4556 comp_mask
= IB_SA_MCMEMBER_REC_MGID
| IB_SA_MCMEMBER_REC_PORT_GID
|
4557 IB_SA_MCMEMBER_REC_PKEY
| IB_SA_MCMEMBER_REC_JOIN_STATE
|
4558 IB_SA_MCMEMBER_REC_QKEY
| IB_SA_MCMEMBER_REC_SL
|
4559 IB_SA_MCMEMBER_REC_FLOW_LABEL
|
4560 IB_SA_MCMEMBER_REC_TRAFFIC_CLASS
;
4562 if (id_priv
->id
.ps
== RDMA_PS_IPOIB
)
4563 comp_mask
|= IB_SA_MCMEMBER_REC_RATE
|
4564 IB_SA_MCMEMBER_REC_RATE_SELECTOR
|
4565 IB_SA_MCMEMBER_REC_MTU_SELECTOR
|
4566 IB_SA_MCMEMBER_REC_MTU
|
4567 IB_SA_MCMEMBER_REC_HOP_LIMIT
;
4569 mc
->sa_mc
= ib_sa_join_multicast(&sa_client
, id_priv
->id
.device
,
4570 id_priv
->id
.port_num
, &rec
, comp_mask
,
4571 GFP_KERNEL
, cma_ib_mc_handler
, mc
);
4572 return PTR_ERR_OR_ZERO(mc
->sa_mc
);
4575 static void cma_iboe_set_mgid(struct sockaddr
*addr
, union ib_gid
*mgid
,
4576 enum ib_gid_type gid_type
)
4578 struct sockaddr_in
*sin
= (struct sockaddr_in
*)addr
;
4579 struct sockaddr_in6
*sin6
= (struct sockaddr_in6
*)addr
;
4581 if (cma_any_addr(addr
)) {
4582 memset(mgid
, 0, sizeof *mgid
);
4583 } else if (addr
->sa_family
== AF_INET6
) {
4584 memcpy(mgid
, &sin6
->sin6_addr
, sizeof *mgid
);
4587 (gid_type
== IB_GID_TYPE_ROCE_UDP_ENCAP
) ? 0 : 0xff;
4589 (gid_type
== IB_GID_TYPE_ROCE_UDP_ENCAP
) ? 0 : 0x0e;
4598 mgid
->raw
[10] = 0xff;
4599 mgid
->raw
[11] = 0xff;
4600 *(__be32
*)(&mgid
->raw
[12]) = sin
->sin_addr
.s_addr
;
4604 static int cma_iboe_join_multicast(struct rdma_id_private
*id_priv
,
4605 struct cma_multicast
*mc
)
4607 struct cma_work
*work
;
4608 struct rdma_dev_addr
*dev_addr
= &id_priv
->id
.route
.addr
.dev_addr
;
4610 struct sockaddr
*addr
= (struct sockaddr
*)&mc
->addr
;
4611 struct net_device
*ndev
= NULL
;
4612 struct ib_sa_multicast ib
;
4613 enum ib_gid_type gid_type
;
4616 send_only
= mc
->join_state
== BIT(SENDONLY_FULLMEMBER_JOIN
);
4618 if (cma_zero_addr(addr
))
4621 work
= kzalloc(sizeof *work
, GFP_KERNEL
);
4625 gid_type
= id_priv
->cma_dev
->default_gid_type
[id_priv
->id
.port_num
-
4626 rdma_start_port(id_priv
->cma_dev
->device
)];
4627 cma_iboe_set_mgid(addr
, &ib
.rec
.mgid
, gid_type
);
4629 ib
.rec
.pkey
= cpu_to_be16(0xffff);
4630 if (id_priv
->id
.ps
== RDMA_PS_UDP
)
4631 ib
.rec
.qkey
= cpu_to_be32(RDMA_UDP_QKEY
);
4633 if (dev_addr
->bound_dev_if
)
4634 ndev
= dev_get_by_index(dev_addr
->net
, dev_addr
->bound_dev_if
);
4639 ib
.rec
.rate
= iboe_get_rate(ndev
);
4640 ib
.rec
.hop_limit
= 1;
4641 ib
.rec
.mtu
= iboe_get_mtu(ndev
->mtu
);
4643 if (addr
->sa_family
== AF_INET
) {
4644 if (gid_type
== IB_GID_TYPE_ROCE_UDP_ENCAP
) {
4645 ib
.rec
.hop_limit
= IPV6_DEFAULT_HOPLIMIT
;
4647 err
= cma_igmp_send(ndev
, &ib
.rec
.mgid
,
4652 if (gid_type
== IB_GID_TYPE_ROCE_UDP_ENCAP
)
4656 if (err
|| !ib
.rec
.mtu
) {
4661 rdma_ip2gid((struct sockaddr
*)&id_priv
->id
.route
.addr
.src_addr
,
4664 INIT_WORK(&work
->work
, cma_work_handler
);
4665 cma_make_mc_event(0, id_priv
, &ib
, &work
->event
, mc
);
4666 /* Balances with cma_id_put() in cma_work_handler */
4667 cma_id_get(id_priv
);
4668 queue_work(cma_wq
, &work
->work
);
4676 int rdma_join_multicast(struct rdma_cm_id
*id
, struct sockaddr
*addr
,
4677 u8 join_state
, void *context
)
4679 struct rdma_id_private
*id_priv
=
4680 container_of(id
, struct rdma_id_private
, id
);
4681 struct cma_multicast
*mc
;
4684 /* Not supported for kernel QPs */
4685 if (WARN_ON(id
->qp
))
4688 /* ULP is calling this wrong. */
4689 if (!id
->device
|| (READ_ONCE(id_priv
->state
) != RDMA_CM_ADDR_BOUND
&&
4690 READ_ONCE(id_priv
->state
) != RDMA_CM_ADDR_RESOLVED
))
4693 mc
= kzalloc(sizeof(*mc
), GFP_KERNEL
);
4697 memcpy(&mc
->addr
, addr
, rdma_addr_size(addr
));
4698 mc
->context
= context
;
4699 mc
->id_priv
= id_priv
;
4700 mc
->join_state
= join_state
;
4702 if (rdma_protocol_roce(id
->device
, id
->port_num
)) {
4703 ret
= cma_iboe_join_multicast(id_priv
, mc
);
4706 } else if (rdma_cap_ib_mcast(id
->device
, id
->port_num
)) {
4707 ret
= cma_join_ib_multicast(id_priv
, mc
);
4715 spin_lock(&id_priv
->lock
);
4716 list_add(&mc
->list
, &id_priv
->mc_list
);
4717 spin_unlock(&id_priv
->lock
);
4724 EXPORT_SYMBOL(rdma_join_multicast
);
4726 void rdma_leave_multicast(struct rdma_cm_id
*id
, struct sockaddr
*addr
)
4728 struct rdma_id_private
*id_priv
;
4729 struct cma_multicast
*mc
;
4731 id_priv
= container_of(id
, struct rdma_id_private
, id
);
4732 spin_lock_irq(&id_priv
->lock
);
4733 list_for_each_entry(mc
, &id_priv
->mc_list
, list
) {
4734 if (memcmp(&mc
->addr
, addr
, rdma_addr_size(addr
)) != 0)
4736 list_del(&mc
->list
);
4737 spin_unlock_irq(&id_priv
->lock
);
4739 WARN_ON(id_priv
->cma_dev
->device
!= id
->device
);
4740 destroy_mc(id_priv
, mc
);
4743 spin_unlock_irq(&id_priv
->lock
);
4745 EXPORT_SYMBOL(rdma_leave_multicast
);
4747 static int cma_netdev_change(struct net_device
*ndev
, struct rdma_id_private
*id_priv
)
4749 struct rdma_dev_addr
*dev_addr
;
4750 struct cma_work
*work
;
4752 dev_addr
= &id_priv
->id
.route
.addr
.dev_addr
;
4754 if ((dev_addr
->bound_dev_if
== ndev
->ifindex
) &&
4755 (net_eq(dev_net(ndev
), dev_addr
->net
)) &&
4756 memcmp(dev_addr
->src_dev_addr
, ndev
->dev_addr
, ndev
->addr_len
)) {
4757 pr_info("RDMA CM addr change for ndev %s used by id %p\n",
4758 ndev
->name
, &id_priv
->id
);
4759 work
= kzalloc(sizeof *work
, GFP_KERNEL
);
4763 INIT_WORK(&work
->work
, cma_work_handler
);
4765 work
->event
.event
= RDMA_CM_EVENT_ADDR_CHANGE
;
4766 cma_id_get(id_priv
);
4767 queue_work(cma_wq
, &work
->work
);
4773 static int cma_netdev_callback(struct notifier_block
*self
, unsigned long event
,
4776 struct net_device
*ndev
= netdev_notifier_info_to_dev(ptr
);
4777 struct cma_device
*cma_dev
;
4778 struct rdma_id_private
*id_priv
;
4779 int ret
= NOTIFY_DONE
;
4781 if (event
!= NETDEV_BONDING_FAILOVER
)
4784 if (!netif_is_bond_master(ndev
))
4788 list_for_each_entry(cma_dev
, &dev_list
, list
)
4789 list_for_each_entry(id_priv
, &cma_dev
->id_list
, list
) {
4790 ret
= cma_netdev_change(ndev
, id_priv
);
4796 mutex_unlock(&lock
);
4800 static struct notifier_block cma_nb
= {
4801 .notifier_call
= cma_netdev_callback
4804 static void cma_send_device_removal_put(struct rdma_id_private
*id_priv
)
4806 struct rdma_cm_event event
= { .event
= RDMA_CM_EVENT_DEVICE_REMOVAL
};
4807 enum rdma_cm_state state
;
4808 unsigned long flags
;
4810 mutex_lock(&id_priv
->handler_mutex
);
4811 /* Record that we want to remove the device */
4812 spin_lock_irqsave(&id_priv
->lock
, flags
);
4813 state
= id_priv
->state
;
4814 if (state
== RDMA_CM_DESTROYING
|| state
== RDMA_CM_DEVICE_REMOVAL
) {
4815 spin_unlock_irqrestore(&id_priv
->lock
, flags
);
4816 mutex_unlock(&id_priv
->handler_mutex
);
4817 cma_id_put(id_priv
);
4820 id_priv
->state
= RDMA_CM_DEVICE_REMOVAL
;
4821 spin_unlock_irqrestore(&id_priv
->lock
, flags
);
4823 if (cma_cm_event_handler(id_priv
, &event
)) {
4825 * At this point the ULP promises it won't call
4826 * rdma_destroy_id() concurrently
4828 cma_id_put(id_priv
);
4829 mutex_unlock(&id_priv
->handler_mutex
);
4830 trace_cm_id_destroy(id_priv
);
4831 _destroy_id(id_priv
, state
);
4834 mutex_unlock(&id_priv
->handler_mutex
);
4837 * If this races with destroy then the thread that first assigns state
4838 * to a destroying does the cancel.
4840 cma_cancel_operation(id_priv
, state
);
4841 cma_id_put(id_priv
);
4844 static void cma_process_remove(struct cma_device
*cma_dev
)
4847 while (!list_empty(&cma_dev
->id_list
)) {
4848 struct rdma_id_private
*id_priv
= list_first_entry(
4849 &cma_dev
->id_list
, struct rdma_id_private
, list
);
4851 list_del(&id_priv
->listen_list
);
4852 list_del_init(&id_priv
->list
);
4853 cma_id_get(id_priv
);
4854 mutex_unlock(&lock
);
4856 cma_send_device_removal_put(id_priv
);
4860 mutex_unlock(&lock
);
4862 cma_dev_put(cma_dev
);
4863 wait_for_completion(&cma_dev
->comp
);
4866 static int cma_add_one(struct ib_device
*device
)
4868 struct rdma_id_private
*to_destroy
;
4869 struct cma_device
*cma_dev
;
4870 struct rdma_id_private
*id_priv
;
4872 unsigned long supported_gids
= 0;
4875 cma_dev
= kmalloc(sizeof(*cma_dev
), GFP_KERNEL
);
4879 cma_dev
->device
= device
;
4880 cma_dev
->default_gid_type
= kcalloc(device
->phys_port_cnt
,
4881 sizeof(*cma_dev
->default_gid_type
),
4883 if (!cma_dev
->default_gid_type
) {
4888 cma_dev
->default_roce_tos
= kcalloc(device
->phys_port_cnt
,
4889 sizeof(*cma_dev
->default_roce_tos
),
4891 if (!cma_dev
->default_roce_tos
) {
4896 rdma_for_each_port (device
, i
) {
4897 supported_gids
= roce_gid_type_mask_support(device
, i
);
4898 WARN_ON(!supported_gids
);
4899 if (supported_gids
& (1 << CMA_PREFERRED_ROCE_GID_TYPE
))
4900 cma_dev
->default_gid_type
[i
- rdma_start_port(device
)] =
4901 CMA_PREFERRED_ROCE_GID_TYPE
;
4903 cma_dev
->default_gid_type
[i
- rdma_start_port(device
)] =
4904 find_first_bit(&supported_gids
, BITS_PER_LONG
);
4905 cma_dev
->default_roce_tos
[i
- rdma_start_port(device
)] = 0;
4908 init_completion(&cma_dev
->comp
);
4909 refcount_set(&cma_dev
->refcount
, 1);
4910 INIT_LIST_HEAD(&cma_dev
->id_list
);
4911 ib_set_client_data(device
, &cma_client
, cma_dev
);
4914 list_add_tail(&cma_dev
->list
, &dev_list
);
4915 list_for_each_entry(id_priv
, &listen_any_list
, list
) {
4916 ret
= cma_listen_on_dev(id_priv
, cma_dev
, &to_destroy
);
4920 mutex_unlock(&lock
);
4922 trace_cm_add_one(device
);
4926 list_del(&cma_dev
->list
);
4927 mutex_unlock(&lock
);
4929 /* cma_process_remove() will delete to_destroy */
4930 cma_process_remove(cma_dev
);
4931 kfree(cma_dev
->default_roce_tos
);
4933 kfree(cma_dev
->default_gid_type
);
4940 static void cma_remove_one(struct ib_device
*device
, void *client_data
)
4942 struct cma_device
*cma_dev
= client_data
;
4944 trace_cm_remove_one(device
);
4947 list_del(&cma_dev
->list
);
4948 mutex_unlock(&lock
);
4950 cma_process_remove(cma_dev
);
4951 kfree(cma_dev
->default_roce_tos
);
4952 kfree(cma_dev
->default_gid_type
);
4956 static int cma_init_net(struct net
*net
)
4958 struct cma_pernet
*pernet
= cma_pernet(net
);
4960 xa_init(&pernet
->tcp_ps
);
4961 xa_init(&pernet
->udp_ps
);
4962 xa_init(&pernet
->ipoib_ps
);
4963 xa_init(&pernet
->ib_ps
);
4968 static void cma_exit_net(struct net
*net
)
4970 struct cma_pernet
*pernet
= cma_pernet(net
);
4972 WARN_ON(!xa_empty(&pernet
->tcp_ps
));
4973 WARN_ON(!xa_empty(&pernet
->udp_ps
));
4974 WARN_ON(!xa_empty(&pernet
->ipoib_ps
));
4975 WARN_ON(!xa_empty(&pernet
->ib_ps
));
4978 static struct pernet_operations cma_pernet_operations
= {
4979 .init
= cma_init_net
,
4980 .exit
= cma_exit_net
,
4981 .id
= &cma_pernet_id
,
4982 .size
= sizeof(struct cma_pernet
),
4985 static int __init
cma_init(void)
4990 * There is a rare lock ordering dependency in cma_netdev_callback()
4991 * that only happens when bonding is enabled. Teach lockdep that rtnl
4992 * must never be nested under lock so it can find these without having
4993 * to test with bonding.
4995 if (IS_ENABLED(CONFIG_LOCKDEP
)) {
4998 mutex_unlock(&lock
);
5002 cma_wq
= alloc_ordered_workqueue("rdma_cm", WQ_MEM_RECLAIM
);
5006 ret
= register_pernet_subsys(&cma_pernet_operations
);
5010 ib_sa_register_client(&sa_client
);
5011 register_netdevice_notifier(&cma_nb
);
5013 ret
= ib_register_client(&cma_client
);
5017 ret
= cma_configfs_init();
5024 ib_unregister_client(&cma_client
);
5026 unregister_netdevice_notifier(&cma_nb
);
5027 ib_sa_unregister_client(&sa_client
);
5028 unregister_pernet_subsys(&cma_pernet_operations
);
5030 destroy_workqueue(cma_wq
);
5034 static void __exit
cma_cleanup(void)
5036 cma_configfs_exit();
5037 ib_unregister_client(&cma_client
);
5038 unregister_netdevice_notifier(&cma_nb
);
5039 ib_sa_unregister_client(&sa_client
);
5040 unregister_pernet_subsys(&cma_pernet_operations
);
5041 destroy_workqueue(cma_wq
);
5044 module_init(cma_init
);
5045 module_exit(cma_cleanup
);